victor HF Staff commited on
Commit
dfa014d
·
1 Parent(s): 8bf0d0c

Add user token to OpenAI API requests

Browse files
src/lib/server/textGeneration/mcp/runMcpFlow.ts CHANGED
@@ -248,6 +248,7 @@ export async function* runMcpFlow({
248
  route: resolvedRoute,
249
  candidateModelId,
250
  toolCount: oaTools.length,
 
251
  },
252
  "[mcp] starting completion with tools"
253
  );
@@ -402,6 +403,7 @@ export async function* runMcpFlow({
402
  headers: {
403
  "ChatUI-Conversation-ID": conv._id.toString(),
404
  "X-use-cache": "false",
 
405
  },
406
  }
407
  );
@@ -523,7 +525,14 @@ export async function* runMcpFlow({
523
  console.debug({ loop }, "[mcp] missing tool_call id in stream; retrying non-stream to recover ids");
524
  const nonStream = await openai.chat.completions.create(
525
  { ...completionBase, messages: messagesOpenAI, stream: false },
526
- { signal: abortSignal }
 
 
 
 
 
 
 
527
  );
528
  const tc = nonStream.choices?.[0]?.message?.tool_calls ?? [];
529
  calls = tc.map((t) => ({
 
248
  route: resolvedRoute,
249
  candidateModelId,
250
  toolCount: oaTools.length,
251
+ hasUserToken: Boolean((locals as any)?.token),
252
  },
253
  "[mcp] starting completion with tools"
254
  );
 
403
  headers: {
404
  "ChatUI-Conversation-ID": conv._id.toString(),
405
  "X-use-cache": "false",
406
+ ...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
407
  },
408
  }
409
  );
 
525
  console.debug({ loop }, "[mcp] missing tool_call id in stream; retrying non-stream to recover ids");
526
  const nonStream = await openai.chat.completions.create(
527
  { ...completionBase, messages: messagesOpenAI, stream: false },
528
+ {
529
+ signal: abortSignal,
530
+ headers: {
531
+ "ChatUI-Conversation-ID": conv._id.toString(),
532
+ "X-use-cache": "false",
533
+ ...(locals?.token ? { Authorization: `Bearer ${locals.token}` } : {}),
534
+ },
535
+ }
536
  );
537
  const tc = nonStream.choices?.[0]?.message?.tool_calls ?? [];
538
  calls = tc.map((t) => ({