@yourgpt/llm-sdk 2.1.5 → 2.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/index.d.mts +11 -37
- package/dist/adapters/index.d.ts +11 -37
- package/dist/adapters/index.js +41 -192
- package/dist/adapters/index.mjs +42 -192
- package/dist/{base-5n-UuPfS.d.mts → base-D-U61JaB.d.mts} +22 -2
- package/dist/{base-Di31iy_8.d.ts → base-iGi9Va6Z.d.ts} +22 -2
- package/dist/fallback/index.d.mts +3 -3
- package/dist/fallback/index.d.ts +3 -3
- package/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +96 -76
- package/dist/index.mjs +96 -76
- package/dist/providers/anthropic/index.d.mts +2 -2
- package/dist/providers/anthropic/index.d.ts +2 -2
- package/dist/providers/azure/index.d.mts +2 -2
- package/dist/providers/azure/index.d.ts +2 -2
- package/dist/providers/azure/index.js +4 -2
- package/dist/providers/azure/index.mjs +4 -2
- package/dist/providers/google/index.d.mts +2 -2
- package/dist/providers/google/index.d.ts +2 -2
- package/dist/providers/google/index.js +527 -339
- package/dist/providers/google/index.mjs +527 -339
- package/dist/providers/ollama/index.d.mts +3 -3
- package/dist/providers/ollama/index.d.ts +3 -3
- package/dist/providers/openai/index.d.mts +2 -2
- package/dist/providers/openai/index.d.ts +2 -2
- package/dist/providers/openai/index.js +34 -11
- package/dist/providers/openai/index.mjs +34 -11
- package/dist/providers/openrouter/index.d.mts +2 -2
- package/dist/providers/openrouter/index.d.ts +2 -2
- package/dist/providers/openrouter/index.js +34 -11
- package/dist/providers/openrouter/index.mjs +34 -11
- package/dist/providers/xai/index.d.mts +2 -2
- package/dist/providers/xai/index.d.ts +2 -2
- package/dist/providers/xai/index.js +355 -46
- package/dist/providers/xai/index.mjs +355 -46
- package/dist/{types-CNL8ZRne.d.ts → types-38yolWJn.d.ts} +1 -1
- package/dist/{types-C0vLXzuw.d.ts → types-BctsnC3g.d.ts} +1 -1
- package/dist/{types-BQl1suAv.d.mts → types-D4YfrQJR.d.mts} +1 -1
- package/dist/{types-VDgiUvH2.d.mts → types-DRqxMIjF.d.mts} +1 -1
- package/package.json +1 -1
|
@@ -239,6 +239,9 @@ function generateId(prefix = "id") {
|
|
|
239
239
|
function generateMessageId() {
|
|
240
240
|
return generateId("msg");
|
|
241
241
|
}
|
|
242
|
+
function generateToolCallId() {
|
|
243
|
+
return generateId("call");
|
|
244
|
+
}
|
|
242
245
|
|
|
243
246
|
// src/adapters/base.ts
|
|
244
247
|
function stringifyForDebug(value) {
|
|
@@ -307,6 +310,40 @@ function parameterToJsonSchema(param) {
|
|
|
307
310
|
}
|
|
308
311
|
return schema;
|
|
309
312
|
}
|
|
313
|
+
function normalizeObjectJsonSchema(schema) {
|
|
314
|
+
if (!schema || typeof schema !== "object") {
|
|
315
|
+
return {
|
|
316
|
+
type: "object",
|
|
317
|
+
properties: {},
|
|
318
|
+
required: [],
|
|
319
|
+
additionalProperties: false
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
const normalized = { ...schema };
|
|
323
|
+
const type = normalized.type;
|
|
324
|
+
if (type === "object") {
|
|
325
|
+
const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
|
|
326
|
+
normalized.properties = Object.fromEntries(
|
|
327
|
+
Object.entries(properties).map(([key, value]) => [
|
|
328
|
+
key,
|
|
329
|
+
normalizeObjectJsonSchema(value)
|
|
330
|
+
])
|
|
331
|
+
);
|
|
332
|
+
const propertyKeys = Object.keys(properties);
|
|
333
|
+
const required = Array.isArray(normalized.required) ? normalized.required.filter(
|
|
334
|
+
(value) => typeof value === "string"
|
|
335
|
+
) : [];
|
|
336
|
+
normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
|
|
337
|
+
if (normalized.additionalProperties === void 0) {
|
|
338
|
+
normalized.additionalProperties = false;
|
|
339
|
+
}
|
|
340
|
+
} else if (type === "array" && normalized.items && typeof normalized.items === "object") {
|
|
341
|
+
normalized.items = normalizeObjectJsonSchema(
|
|
342
|
+
normalized.items
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
return normalized;
|
|
346
|
+
}
|
|
310
347
|
function formatTools(actions) {
|
|
311
348
|
return actions.map((action) => ({
|
|
312
349
|
type: "function",
|
|
@@ -383,11 +420,13 @@ function formatMessagesForOpenAI(messages, systemPrompt) {
|
|
|
383
420
|
content: messageToOpenAIContent(msg)
|
|
384
421
|
});
|
|
385
422
|
} else if (msg.role === "assistant") {
|
|
423
|
+
const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
|
|
386
424
|
const assistantMsg = {
|
|
387
425
|
role: "assistant",
|
|
388
|
-
content:
|
|
426
|
+
// Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
|
|
427
|
+
content: hasToolCalls ? msg.content || null : msg.content
|
|
389
428
|
};
|
|
390
|
-
if (
|
|
429
|
+
if (hasToolCalls) {
|
|
391
430
|
assistantMsg.tool_calls = msg.tool_calls;
|
|
392
431
|
}
|
|
393
432
|
formatted.push(assistantMsg);
|
|
@@ -402,29 +441,183 @@ function formatMessagesForOpenAI(messages, systemPrompt) {
|
|
|
402
441
|
return formatted;
|
|
403
442
|
}
|
|
404
443
|
|
|
405
|
-
// src/adapters/
|
|
406
|
-
var
|
|
407
|
-
var XAIAdapter = class {
|
|
444
|
+
// src/adapters/openai.ts
|
|
445
|
+
var OpenAIAdapter = class _OpenAIAdapter {
|
|
408
446
|
constructor(config) {
|
|
409
|
-
this.provider = "xai";
|
|
410
447
|
this.config = config;
|
|
411
|
-
this.model = config.model || "
|
|
448
|
+
this.model = config.model || "gpt-4o";
|
|
449
|
+
this.provider = _OpenAIAdapter.resolveProviderName(config.baseUrl);
|
|
450
|
+
}
|
|
451
|
+
static resolveProviderName(baseUrl) {
|
|
452
|
+
if (!baseUrl) return "openai";
|
|
453
|
+
if (baseUrl.includes("generativelanguage.googleapis.com")) return "google";
|
|
454
|
+
if (baseUrl.includes("x.ai")) return "xai";
|
|
455
|
+
if (baseUrl.includes("azure")) return "azure";
|
|
456
|
+
return "openai";
|
|
412
457
|
}
|
|
413
458
|
async getClient() {
|
|
414
459
|
if (!this.client) {
|
|
415
460
|
const { default: OpenAI } = await import('openai');
|
|
416
461
|
this.client = new OpenAI({
|
|
417
462
|
apiKey: this.config.apiKey,
|
|
418
|
-
baseURL: this.config.baseUrl
|
|
463
|
+
baseURL: this.config.baseUrl
|
|
419
464
|
});
|
|
420
465
|
}
|
|
421
466
|
return this.client;
|
|
422
467
|
}
|
|
468
|
+
shouldUseResponsesApi(request) {
|
|
469
|
+
return request.providerToolOptions?.openai?.nativeToolSearch?.enabled === true && request.providerToolOptions.openai.nativeToolSearch.useResponsesApi !== false && Array.isArray(request.toolDefinitions) && request.toolDefinitions.length > 0;
|
|
470
|
+
}
|
|
471
|
+
buildResponsesInput(request) {
|
|
472
|
+
const sourceMessages = request.rawMessages && request.rawMessages.length > 0 ? request.rawMessages : formatMessagesForOpenAI(request.messages, void 0);
|
|
473
|
+
const input = [];
|
|
474
|
+
for (const message of sourceMessages) {
|
|
475
|
+
if (message.role === "system") {
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
if (message.role === "assistant") {
|
|
479
|
+
const content = typeof message.content === "string" ? message.content : Array.isArray(message.content) ? message.content : message.content ? JSON.stringify(message.content) : "";
|
|
480
|
+
if (content) {
|
|
481
|
+
input.push({
|
|
482
|
+
type: "message",
|
|
483
|
+
role: "assistant",
|
|
484
|
+
content
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
488
|
+
for (const toolCall of toolCalls) {
|
|
489
|
+
input.push({
|
|
490
|
+
type: "function_call",
|
|
491
|
+
call_id: toolCall.id,
|
|
492
|
+
name: toolCall.function?.name,
|
|
493
|
+
arguments: toolCall.function?.arguments ?? "{}"
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
if (message.role === "tool") {
|
|
499
|
+
input.push({
|
|
500
|
+
type: "function_call_output",
|
|
501
|
+
call_id: message.tool_call_id,
|
|
502
|
+
output: typeof message.content === "string" ? message.content : JSON.stringify(message.content ?? null)
|
|
503
|
+
});
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
input.push({
|
|
507
|
+
type: "message",
|
|
508
|
+
role: message.role === "developer" ? "developer" : "user",
|
|
509
|
+
content: typeof message.content === "string" ? message.content : Array.isArray(message.content) ? message.content : JSON.stringify(message.content ?? "")
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
return input;
|
|
513
|
+
}
|
|
514
|
+
buildResponsesTools(tools) {
|
|
515
|
+
const nativeTools = tools.filter((tool) => tool.available !== false).map((tool) => ({
|
|
516
|
+
type: "function",
|
|
517
|
+
name: tool.name,
|
|
518
|
+
description: tool.description,
|
|
519
|
+
parameters: normalizeObjectJsonSchema(
|
|
520
|
+
tool.inputSchema ?? {
|
|
521
|
+
type: "object",
|
|
522
|
+
properties: {},
|
|
523
|
+
required: []
|
|
524
|
+
}
|
|
525
|
+
),
|
|
526
|
+
strict: true,
|
|
527
|
+
defer_loading: tool.deferLoading === true
|
|
528
|
+
}));
|
|
529
|
+
return [{ type: "tool_search" }, ...nativeTools];
|
|
530
|
+
}
|
|
531
|
+
parseResponsesResult(response) {
|
|
532
|
+
const content = typeof response?.output_text === "string" ? response.output_text : "";
|
|
533
|
+
const toolCalls = Array.isArray(response?.output) ? response.output.filter((item) => item?.type === "function_call").map((item) => ({
|
|
534
|
+
id: item.call_id ?? item.id ?? generateToolCallId(),
|
|
535
|
+
name: item.name,
|
|
536
|
+
args: (() => {
|
|
537
|
+
try {
|
|
538
|
+
return JSON.parse(item.arguments ?? "{}");
|
|
539
|
+
} catch {
|
|
540
|
+
return {};
|
|
541
|
+
}
|
|
542
|
+
})()
|
|
543
|
+
})) : [];
|
|
544
|
+
return {
|
|
545
|
+
content,
|
|
546
|
+
toolCalls,
|
|
547
|
+
usage: response?.usage ? {
|
|
548
|
+
promptTokens: response.usage.input_tokens ?? 0,
|
|
549
|
+
completionTokens: response.usage.output_tokens ?? 0,
|
|
550
|
+
totalTokens: response.usage.total_tokens ?? (response.usage.input_tokens ?? 0) + (response.usage.output_tokens ?? 0)
|
|
551
|
+
} : void 0,
|
|
552
|
+
rawResponse: response
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
async completeWithResponses(request) {
|
|
556
|
+
const client = await this.getClient();
|
|
557
|
+
const openaiToolOptions = request.providerToolOptions?.openai;
|
|
558
|
+
const payload = {
|
|
559
|
+
model: request.config?.model || this.model,
|
|
560
|
+
instructions: request.systemPrompt,
|
|
561
|
+
input: this.buildResponsesInput(request),
|
|
562
|
+
tools: this.buildResponsesTools(request.toolDefinitions ?? []),
|
|
563
|
+
tool_choice: openaiToolOptions?.toolChoice === "required" ? "required" : openaiToolOptions?.toolChoice === "auto" ? "auto" : void 0,
|
|
564
|
+
parallel_tool_calls: openaiToolOptions?.parallelToolCalls,
|
|
565
|
+
temperature: request.config?.temperature ?? this.config.temperature,
|
|
566
|
+
max_output_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
567
|
+
stream: false
|
|
568
|
+
};
|
|
569
|
+
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
570
|
+
const response = await client.responses.create(payload);
|
|
571
|
+
logProviderPayload("openai", "response payload", response, request.debug);
|
|
572
|
+
return this.parseResponsesResult(response);
|
|
573
|
+
}
|
|
423
574
|
async *stream(request) {
|
|
575
|
+
if (this.shouldUseResponsesApi(request)) {
|
|
576
|
+
const messageId2 = generateMessageId();
|
|
577
|
+
yield { type: "message:start", id: messageId2 };
|
|
578
|
+
try {
|
|
579
|
+
const result = await this.completeWithResponses(request);
|
|
580
|
+
if (result.content) {
|
|
581
|
+
yield { type: "message:delta", content: result.content };
|
|
582
|
+
}
|
|
583
|
+
for (const toolCall of result.toolCalls) {
|
|
584
|
+
yield {
|
|
585
|
+
type: "action:start",
|
|
586
|
+
id: toolCall.id,
|
|
587
|
+
name: toolCall.name
|
|
588
|
+
};
|
|
589
|
+
yield {
|
|
590
|
+
type: "action:args",
|
|
591
|
+
id: toolCall.id,
|
|
592
|
+
args: JSON.stringify(toolCall.args)
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
yield { type: "message:end" };
|
|
596
|
+
yield {
|
|
597
|
+
type: "done",
|
|
598
|
+
usage: result.usage ? {
|
|
599
|
+
prompt_tokens: result.usage.promptTokens,
|
|
600
|
+
completion_tokens: result.usage.completionTokens,
|
|
601
|
+
total_tokens: result.usage.totalTokens
|
|
602
|
+
} : void 0
|
|
603
|
+
};
|
|
604
|
+
return;
|
|
605
|
+
} catch (error) {
|
|
606
|
+
yield {
|
|
607
|
+
type: "error",
|
|
608
|
+
message: error instanceof Error ? error.message : "Unknown error",
|
|
609
|
+
code: "OPENAI_RESPONSES_ERROR"
|
|
610
|
+
};
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
424
614
|
const client = await this.getClient();
|
|
425
615
|
let messages;
|
|
426
616
|
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
427
617
|
const processedMessages = request.rawMessages.map((msg) => {
|
|
618
|
+
if (msg.role === "assistant" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0 && msg.content === "") {
|
|
619
|
+
return { ...msg, content: null };
|
|
620
|
+
}
|
|
428
621
|
const hasAttachments = msg.attachments && Array.isArray(msg.attachments) && msg.attachments.length > 0;
|
|
429
622
|
if (hasAttachments) {
|
|
430
623
|
const content = [];
|
|
@@ -433,9 +626,13 @@ var XAIAdapter = class {
|
|
|
433
626
|
}
|
|
434
627
|
for (const attachment of msg.attachments) {
|
|
435
628
|
if (attachment.type === "image") {
|
|
436
|
-
let imageUrl
|
|
437
|
-
if (
|
|
438
|
-
imageUrl =
|
|
629
|
+
let imageUrl;
|
|
630
|
+
if (attachment.url) {
|
|
631
|
+
imageUrl = attachment.url;
|
|
632
|
+
} else if (attachment.data) {
|
|
633
|
+
imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
|
|
634
|
+
} else {
|
|
635
|
+
continue;
|
|
439
636
|
}
|
|
440
637
|
content.push({
|
|
441
638
|
type: "image_url",
|
|
@@ -466,30 +663,72 @@ var XAIAdapter = class {
|
|
|
466
663
|
request.systemPrompt
|
|
467
664
|
);
|
|
468
665
|
}
|
|
469
|
-
const tools = request.actions?.length ? formatTools(request.actions) :
|
|
666
|
+
const tools = request.actions?.length ? formatTools(request.actions) : [];
|
|
667
|
+
const webSearchConfig = request.webSearch ?? this.config.webSearch;
|
|
668
|
+
if (webSearchConfig) {
|
|
669
|
+
const webSearchTool = {
|
|
670
|
+
type: "web_search_preview"
|
|
671
|
+
};
|
|
672
|
+
const wsConfig = typeof webSearchConfig === "object" ? webSearchConfig : {};
|
|
673
|
+
if (wsConfig.userLocation) {
|
|
674
|
+
webSearchTool.search_context_size = "medium";
|
|
675
|
+
}
|
|
676
|
+
tools.push(webSearchTool);
|
|
677
|
+
}
|
|
470
678
|
const messageId = generateMessageId();
|
|
471
679
|
yield { type: "message:start", id: messageId };
|
|
472
680
|
try {
|
|
681
|
+
const openaiToolOptions = request.providerToolOptions?.openai;
|
|
682
|
+
const toolChoice = openaiToolOptions?.toolChoice && typeof openaiToolOptions.toolChoice === "object" ? {
|
|
683
|
+
type: "function",
|
|
684
|
+
function: {
|
|
685
|
+
name: openaiToolOptions.toolChoice.name
|
|
686
|
+
}
|
|
687
|
+
} : openaiToolOptions?.toolChoice;
|
|
473
688
|
const payload = {
|
|
474
689
|
model: request.config?.model || this.model,
|
|
475
690
|
messages,
|
|
476
|
-
tools,
|
|
691
|
+
tools: tools.length > 0 ? tools : void 0,
|
|
692
|
+
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
693
|
+
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
477
694
|
temperature: request.config?.temperature ?? this.config.temperature,
|
|
478
695
|
max_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
479
|
-
stream: true
|
|
696
|
+
stream: true,
|
|
697
|
+
stream_options: { include_usage: true }
|
|
480
698
|
};
|
|
481
|
-
logProviderPayload("
|
|
699
|
+
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
482
700
|
const stream = await client.chat.completions.create(payload);
|
|
483
701
|
let currentToolCall = null;
|
|
702
|
+
const collectedCitations = [];
|
|
703
|
+
let citationIndex = 0;
|
|
704
|
+
let usage;
|
|
484
705
|
for await (const chunk of stream) {
|
|
485
|
-
logProviderPayload("
|
|
706
|
+
logProviderPayload("openai", "stream chunk", chunk, request.debug);
|
|
486
707
|
if (request.signal?.aborted) {
|
|
487
708
|
break;
|
|
488
709
|
}
|
|
489
710
|
const delta = chunk.choices[0]?.delta;
|
|
711
|
+
const choice = chunk.choices[0];
|
|
490
712
|
if (delta?.content) {
|
|
491
713
|
yield { type: "message:delta", content: delta.content };
|
|
492
714
|
}
|
|
715
|
+
const annotations = delta?.annotations;
|
|
716
|
+
if (annotations && annotations.length > 0) {
|
|
717
|
+
for (const annotation of annotations) {
|
|
718
|
+
if (annotation.type === "url_citation" && annotation.url_citation?.url) {
|
|
719
|
+
citationIndex++;
|
|
720
|
+
const url = annotation.url_citation.url;
|
|
721
|
+
const domain = extractDomain(url);
|
|
722
|
+
collectedCitations.push({
|
|
723
|
+
index: citationIndex,
|
|
724
|
+
url,
|
|
725
|
+
title: annotation.url_citation.title || domain,
|
|
726
|
+
domain,
|
|
727
|
+
favicon: domain ? `https://www.google.com/s2/favicons?domain=${domain}&sz=32` : void 0
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
493
732
|
if (delta?.tool_calls) {
|
|
494
733
|
for (const toolCall of delta.tool_calls) {
|
|
495
734
|
if (toolCall.id) {
|
|
@@ -500,22 +739,32 @@ var XAIAdapter = class {
|
|
|
500
739
|
args: currentToolCall.arguments
|
|
501
740
|
};
|
|
502
741
|
}
|
|
742
|
+
const tcExtraContent = toolCall.extra_content;
|
|
503
743
|
currentToolCall = {
|
|
504
744
|
id: toolCall.id,
|
|
505
745
|
name: toolCall.function?.name || "",
|
|
506
|
-
arguments: toolCall.function?.arguments || ""
|
|
746
|
+
arguments: toolCall.function?.arguments || "",
|
|
747
|
+
...tcExtraContent ? { extra_content: tcExtraContent } : {}
|
|
507
748
|
};
|
|
508
749
|
yield {
|
|
509
750
|
type: "action:start",
|
|
510
751
|
id: currentToolCall.id,
|
|
511
|
-
name: currentToolCall.name
|
|
752
|
+
name: currentToolCall.name,
|
|
753
|
+
...currentToolCall.extra_content ? { extra_content: currentToolCall.extra_content } : {}
|
|
512
754
|
};
|
|
513
755
|
} else if (currentToolCall && toolCall.function?.arguments) {
|
|
514
756
|
currentToolCall.arguments += toolCall.function.arguments;
|
|
515
757
|
}
|
|
516
758
|
}
|
|
517
759
|
}
|
|
518
|
-
if (chunk.
|
|
760
|
+
if (chunk.usage) {
|
|
761
|
+
usage = {
|
|
762
|
+
prompt_tokens: chunk.usage.prompt_tokens,
|
|
763
|
+
completion_tokens: chunk.usage.completion_tokens,
|
|
764
|
+
total_tokens: chunk.usage.total_tokens
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
if (choice?.finish_reason) {
|
|
519
768
|
if (currentToolCall) {
|
|
520
769
|
yield {
|
|
521
770
|
type: "action:args",
|
|
@@ -525,32 +774,40 @@ var XAIAdapter = class {
|
|
|
525
774
|
}
|
|
526
775
|
}
|
|
527
776
|
}
|
|
777
|
+
if (collectedCitations.length > 0) {
|
|
778
|
+
const uniqueCitations = deduplicateCitations(collectedCitations);
|
|
779
|
+
yield { type: "citation", citations: uniqueCitations };
|
|
780
|
+
}
|
|
528
781
|
yield { type: "message:end" };
|
|
529
|
-
yield { type: "done" };
|
|
782
|
+
yield { type: "done", usage };
|
|
530
783
|
} catch (error) {
|
|
531
784
|
yield {
|
|
532
785
|
type: "error",
|
|
533
786
|
message: error instanceof Error ? error.message : "Unknown error",
|
|
534
|
-
code:
|
|
787
|
+
code: `${this.provider.toUpperCase()}_ERROR`
|
|
535
788
|
};
|
|
536
789
|
}
|
|
537
790
|
}
|
|
538
|
-
/**
|
|
539
|
-
* Non-streaming completion (optional, for debugging)
|
|
540
|
-
*/
|
|
541
791
|
async complete(request) {
|
|
792
|
+
if (this.shouldUseResponsesApi(request)) {
|
|
793
|
+
return this.completeWithResponses(request);
|
|
794
|
+
}
|
|
542
795
|
const client = await this.getClient();
|
|
543
796
|
let messages;
|
|
544
797
|
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
if (!hasSystem) {
|
|
549
|
-
messages = [
|
|
550
|
-
{ role: "system", content: request.systemPrompt },
|
|
551
|
-
...messages
|
|
552
|
-
];
|
|
798
|
+
const sanitized = request.rawMessages.map((msg) => {
|
|
799
|
+
if (msg.role === "assistant" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0 && msg.content === "") {
|
|
800
|
+
return { ...msg, content: null };
|
|
553
801
|
}
|
|
802
|
+
return msg;
|
|
803
|
+
});
|
|
804
|
+
if (request.systemPrompt && !sanitized.some((message2) => message2.role === "system")) {
|
|
805
|
+
messages = [
|
|
806
|
+
{ role: "system", content: request.systemPrompt },
|
|
807
|
+
...sanitized
|
|
808
|
+
];
|
|
809
|
+
} else {
|
|
810
|
+
messages = sanitized;
|
|
554
811
|
}
|
|
555
812
|
} else {
|
|
556
813
|
messages = formatMessagesForOpenAI(
|
|
@@ -558,33 +815,85 @@ var XAIAdapter = class {
|
|
|
558
815
|
request.systemPrompt
|
|
559
816
|
);
|
|
560
817
|
}
|
|
561
|
-
const tools = request.actions?.length ? formatTools(request.actions) :
|
|
818
|
+
const tools = request.actions?.length ? formatTools(request.actions) : [];
|
|
819
|
+
const openaiToolOptions = request.providerToolOptions?.openai;
|
|
820
|
+
const toolChoice = openaiToolOptions?.toolChoice && typeof openaiToolOptions.toolChoice === "object" ? {
|
|
821
|
+
type: "function",
|
|
822
|
+
function: {
|
|
823
|
+
name: openaiToolOptions.toolChoice.name
|
|
824
|
+
}
|
|
825
|
+
} : openaiToolOptions?.toolChoice;
|
|
562
826
|
const payload = {
|
|
563
827
|
model: request.config?.model || this.model,
|
|
564
828
|
messages,
|
|
565
|
-
tools,
|
|
829
|
+
tools: tools.length > 0 ? tools : void 0,
|
|
830
|
+
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
831
|
+
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
566
832
|
temperature: request.config?.temperature ?? this.config.temperature,
|
|
567
|
-
max_tokens: request.config?.maxTokens ?? this.config.maxTokens
|
|
833
|
+
max_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
834
|
+
stream: false
|
|
568
835
|
};
|
|
569
|
-
logProviderPayload("
|
|
836
|
+
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
570
837
|
const response = await client.chat.completions.create(payload);
|
|
571
|
-
logProviderPayload("
|
|
572
|
-
const choice = response.choices[0];
|
|
838
|
+
logProviderPayload("openai", "response payload", response, request.debug);
|
|
839
|
+
const choice = response.choices?.[0];
|
|
573
840
|
const message = choice?.message;
|
|
574
|
-
const toolCalls = (message?.tool_calls || []).map((tc) => ({
|
|
575
|
-
id: tc.id,
|
|
576
|
-
name: tc.function.name,
|
|
577
|
-
args: JSON.parse(tc.function.arguments || "{}")
|
|
578
|
-
}));
|
|
579
841
|
return {
|
|
580
|
-
content: message?.content
|
|
581
|
-
toolCalls
|
|
842
|
+
content: message?.content ?? "",
|
|
843
|
+
toolCalls: message?.tool_calls?.map((toolCall) => ({
|
|
844
|
+
id: toolCall.id ?? generateToolCallId(),
|
|
845
|
+
name: toolCall.function?.name ?? "",
|
|
846
|
+
args: (() => {
|
|
847
|
+
try {
|
|
848
|
+
return JSON.parse(toolCall.function?.arguments ?? "{}");
|
|
849
|
+
} catch {
|
|
850
|
+
return {};
|
|
851
|
+
}
|
|
852
|
+
})(),
|
|
853
|
+
...toolCall.extra_content ? { extra_content: toolCall.extra_content } : {}
|
|
854
|
+
})) ?? [],
|
|
855
|
+
usage: response.usage ? {
|
|
856
|
+
promptTokens: response.usage.prompt_tokens,
|
|
857
|
+
completionTokens: response.usage.completion_tokens,
|
|
858
|
+
totalTokens: response.usage.total_tokens
|
|
859
|
+
} : void 0,
|
|
582
860
|
rawResponse: response
|
|
583
861
|
};
|
|
584
862
|
}
|
|
585
863
|
};
|
|
864
|
+
function extractDomain(url) {
|
|
865
|
+
try {
|
|
866
|
+
const parsed = new URL(url);
|
|
867
|
+
return parsed.hostname;
|
|
868
|
+
} catch {
|
|
869
|
+
return "";
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
function deduplicateCitations(citations) {
|
|
873
|
+
const seen = /* @__PURE__ */ new Map();
|
|
874
|
+
let index = 0;
|
|
875
|
+
for (const citation of citations) {
|
|
876
|
+
if (!seen.has(citation.url)) {
|
|
877
|
+
index++;
|
|
878
|
+
seen.set(citation.url, { ...citation, index });
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return Array.from(seen.values());
|
|
882
|
+
}
|
|
883
|
+
function createOpenAIAdapter(config) {
|
|
884
|
+
return new OpenAIAdapter(config);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
// src/adapters/xai.ts
|
|
888
|
+
var XAI_BASE_URL = "https://api.x.ai/v1";
|
|
586
889
|
function createXAIAdapter(config) {
|
|
587
|
-
return
|
|
890
|
+
return createOpenAIAdapter({
|
|
891
|
+
apiKey: config.apiKey,
|
|
892
|
+
model: config.model || "grok-3",
|
|
893
|
+
baseUrl: config.baseUrl || XAI_BASE_URL,
|
|
894
|
+
temperature: config.temperature,
|
|
895
|
+
maxTokens: config.maxTokens
|
|
896
|
+
});
|
|
588
897
|
}
|
|
589
898
|
|
|
590
899
|
// src/providers/types.ts
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as LLMAdapter, T as ToolDefinition, U as UnifiedToolCall, h as UnifiedToolResult } from './base-
|
|
1
|
+
import { L as LLMAdapter, T as ToolDefinition, U as UnifiedToolCall, h as UnifiedToolResult } from './base-D-U61JaB.mjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Provider Types
|