Shoraky commited on
Commit
6ae077e
·
1 Parent(s): 89c7f9c

Authenticate requests to private agent Space

Browse files
Files changed (4) hide show
  1. .env.example +3 -1
  2. src/agent-session.ts +7 -1
  3. src/config.ts +1 -0
  4. src/server.ts +3 -1
.env.example CHANGED
@@ -5,8 +5,10 @@ FRONTEND_URL=http://localhost:8080
5
  BACKEND_URL=http://localhost:4000
6
  ANALYSIS_PROXY_SECRET=replace-with-a-random-secret-of-at-least-32-characters
7
  # Deploy the stateless Agent service separately and use its private URL/key here.
8
- AGENT_API_URL=https://your-agent-service.example.com
9
  AGENT_API_KEY=replace-with-the-Agent-service-secret
 
 
10
  # Optional: use the same random secret in the Colab notebook to register its worker.
11
  # COLAB_WORKER_SECRET=replace-with-a-different-random-secret-of-at-least-32-characters
12
  SUPABASE_URL=https://your-project.supabase.co
 
5
  BACKEND_URL=http://localhost:4000
6
  ANALYSIS_PROXY_SECRET=replace-with-a-random-secret-of-at-least-32-characters
7
  # Deploy the stateless Agent service separately and use its private URL/key here.
8
+ AGENT_API_URL=https://shoraky-sporalize-agent.hf.space
9
  AGENT_API_KEY=replace-with-the-Agent-service-secret
10
+ # Read-scoped Hugging Face token for the private Agent Space.
11
+ AGENT_HF_TOKEN=hf_read_token_for_the_private_Agent_Space
12
  # Optional: use the same random secret in the Colab notebook to register its worker.
13
  # COLAB_WORKER_SECRET=replace-with-a-different-random-secret-of-at-least-32-characters
14
  SUPABASE_URL=https://your-project.supabase.co
src/agent-session.ts CHANGED
@@ -73,9 +73,15 @@ export async function generateSessionFeedback(db: SupabaseClient, sessionId: str
73
  results: { metrics: current.inActionMetrics ?? [], analytics: current.rawAnalytics ?? {} }, references });
74
  }
75
  if (!actions.length) return;
 
 
 
 
 
 
76
  const response = await fetch(new URL("/api/session/generate", env.AGENT_API_URL), {
77
  method: "POST",
78
- headers: { "content-type": "application/json", "x-api-key": env.AGENT_API_KEY, "x-user-id": userId },
79
  body: JSON.stringify({ session_id: sessionId, player, actions }),
80
  signal: AbortSignal.timeout(150000),
81
  });
 
73
  results: { metrics: current.inActionMetrics ?? [], analytics: current.rawAnalytics ?? {} }, references });
74
  }
75
  if (!actions.length) return;
76
+ const agentHeaders: Record<string, string> = {
77
+ "content-type": "application/json",
78
+ "x-api-key": env.AGENT_API_KEY,
79
+ "x-user-id": userId,
80
+ };
81
+ if (env.AGENT_HF_TOKEN) agentHeaders.authorization = `Bearer ${env.AGENT_HF_TOKEN}`;
82
  const response = await fetch(new URL("/api/session/generate", env.AGENT_API_URL), {
83
  method: "POST",
84
+ headers: agentHeaders,
85
  body: JSON.stringify({ session_id: sessionId, player, actions }),
86
  signal: AbortSignal.timeout(150000),
87
  });
src/config.ts CHANGED
@@ -10,6 +10,7 @@ const envSchema = z.object({
10
  ANALYSIS_API_URL: z.string().url().default("http://127.0.0.1:8000"),
11
  AGENT_API_URL: z.string().url().optional(),
12
  AGENT_API_KEY: z.string().min(16).optional(),
 
13
  ANALYSIS_PROXY_SECRET: z.string().min(32),
14
  COLAB_WORKER_SECRET: z.string().optional(),
15
  SUPABASE_URL: z.string().url(),
 
10
  ANALYSIS_API_URL: z.string().url().default("http://127.0.0.1:8000"),
11
  AGENT_API_URL: z.string().url().optional(),
12
  AGENT_API_KEY: z.string().min(16).optional(),
13
+ AGENT_HF_TOKEN: z.string().optional(),
14
  ANALYSIS_PROXY_SECRET: z.string().min(32),
15
  COLAB_WORKER_SECRET: z.string().optional(),
16
  SUPABASE_URL: z.string().url(),
src/server.ts CHANGED
@@ -790,8 +790,10 @@ app.post("/api/agent/chat", async (req, res, next) => {
790
  if (!player) return res.sendStatus(404);
791
  context.player = player;
792
  }
 
 
793
  const upstream = await fetch(new URL("/api/chat", env.AGENT_API_URL), {
794
- method: "POST", headers: { "content-type": "application/json", "x-api-key": env.AGENT_API_KEY, "x-user-id": current.user.id },
795
  body: JSON.stringify({ actions: [], context, message: input.message, history: input.history }),
796
  signal: AbortSignal.timeout(95000),
797
  });
 
790
  if (!player) return res.sendStatus(404);
791
  context.player = player;
792
  }
793
+ const agentHeaders: Record<string, string> = { "content-type": "application/json", "x-api-key": env.AGENT_API_KEY, "x-user-id": current.user.id };
794
+ if (env.AGENT_HF_TOKEN) agentHeaders.authorization = `Bearer ${env.AGENT_HF_TOKEN}`;
795
  const upstream = await fetch(new URL("/api/chat", env.AGENT_API_URL), {
796
+ method: "POST", headers: agentHeaders,
797
  body: JSON.stringify({ actions: [], context, message: input.message, history: input.history }),
798
  signal: AbortSignal.timeout(95000),
799
  });