@yourgpt/llm-sdk 2.1.4-alpha.3 → 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
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { L as LLMAdapter, W as WebSearchConfig, C as ChatCompletionRequest, S as StreamEvent, o as CompletionResult } from '../base-
|
|
2
|
-
export { e as AdapterFactory, B as AnthropicContentBlock, E as OpenAIContentBlock, y as attachmentToAnthropicDocument, x as attachmentToAnthropicImage, z as attachmentToOpenAIImage, p as formatMessages, r as formatMessagesForAnthropic, s as formatMessagesForOpenAI, q as formatTools, v as hasImageAttachments, w as hasMediaAttachments, t as messageToAnthropicContent, u as messageToOpenAIContent } from '../base-
|
|
3
|
-
import { d as OllamaModelOptions } from '../types-
|
|
1
|
+
import { L as LLMAdapter, W as WebSearchConfig, C as ChatCompletionRequest, S as StreamEvent, o as CompletionResult } from '../base-D-U61JaB.mjs';
|
|
2
|
+
export { e as AdapterFactory, B as AnthropicContentBlock, E as OpenAIContentBlock, y as attachmentToAnthropicDocument, x as attachmentToAnthropicImage, z as attachmentToOpenAIImage, p as formatMessages, r as formatMessagesForAnthropic, s as formatMessagesForOpenAI, q as formatTools, v as hasImageAttachments, w as hasMediaAttachments, t as messageToAnthropicContent, u as messageToOpenAIContent } from '../base-D-U61JaB.mjs';
|
|
3
|
+
import { d as OllamaModelOptions } from '../types-DRqxMIjF.mjs';
|
|
4
4
|
import '../types-CR8mi9I0.mjs';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
@@ -25,11 +25,12 @@ interface OpenAIAdapterConfig {
|
|
|
25
25
|
* Supports: GPT-4, GPT-4o, GPT-3.5-turbo, etc.
|
|
26
26
|
*/
|
|
27
27
|
declare class OpenAIAdapter implements LLMAdapter {
|
|
28
|
-
readonly provider
|
|
28
|
+
readonly provider: string;
|
|
29
29
|
readonly model: string;
|
|
30
30
|
private client;
|
|
31
31
|
private config;
|
|
32
32
|
constructor(config: OpenAIAdapterConfig);
|
|
33
|
+
private static resolveProviderName;
|
|
33
34
|
private getClient;
|
|
34
35
|
private shouldUseResponsesApi;
|
|
35
36
|
private buildResponsesInput;
|
|
@@ -194,17 +195,11 @@ declare class GoogleAdapter implements LLMAdapter {
|
|
|
194
195
|
declare function createGoogleAdapter(config: GoogleAdapterConfig): GoogleAdapter;
|
|
195
196
|
|
|
196
197
|
/**
|
|
197
|
-
* xAI Grok
|
|
198
|
+
* xAI Grok Adapter
|
|
198
199
|
*
|
|
199
|
-
* xAI uses an OpenAI-compatible API
|
|
200
|
-
* with
|
|
201
|
-
*
|
|
202
|
-
* Supports: Grok-2, Grok-2-mini, Grok-beta
|
|
203
|
-
* Features: Vision, Tools/Function Calling
|
|
204
|
-
*/
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* xAI adapter configuration
|
|
200
|
+
* xAI uses an OpenAI-compatible API — this is a thin factory
|
|
201
|
+
* that creates an OpenAIAdapter with the xAI endpoint baked in.
|
|
202
|
+
* No separate class needed.
|
|
208
203
|
*/
|
|
209
204
|
interface XAIAdapterConfig {
|
|
210
205
|
apiKey: string;
|
|
@@ -213,28 +208,7 @@ interface XAIAdapterConfig {
|
|
|
213
208
|
temperature?: number;
|
|
214
209
|
maxTokens?: number;
|
|
215
210
|
}
|
|
216
|
-
|
|
217
|
-
* xAI Grok LLM Adapter
|
|
218
|
-
*
|
|
219
|
-
* Uses OpenAI-compatible API with xAI's endpoint
|
|
220
|
-
*/
|
|
221
|
-
declare class XAIAdapter implements LLMAdapter {
|
|
222
|
-
readonly provider = "xai";
|
|
223
|
-
readonly model: string;
|
|
224
|
-
private client;
|
|
225
|
-
private config;
|
|
226
|
-
constructor(config: XAIAdapterConfig);
|
|
227
|
-
private getClient;
|
|
228
|
-
stream(request: ChatCompletionRequest): AsyncGenerator<StreamEvent>;
|
|
229
|
-
/**
|
|
230
|
-
* Non-streaming completion (optional, for debugging)
|
|
231
|
-
*/
|
|
232
|
-
complete(request: ChatCompletionRequest): Promise<CompletionResult>;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Create xAI Grok adapter
|
|
236
|
-
*/
|
|
237
|
-
declare function createXAIAdapter(config: XAIAdapterConfig): XAIAdapter;
|
|
211
|
+
declare function createXAIAdapter(config: XAIAdapterConfig): OpenAIAdapter;
|
|
238
212
|
|
|
239
213
|
/**
|
|
240
214
|
* Azure OpenAI LLM Adapter
|
|
@@ -286,4 +260,4 @@ declare class AzureAdapter implements LLMAdapter {
|
|
|
286
260
|
*/
|
|
287
261
|
declare function createAzureAdapter(config: AzureAdapterConfig): AzureAdapter;
|
|
288
262
|
|
|
289
|
-
export { AnthropicAdapter, type AnthropicAdapterConfig, AzureAdapter, type AzureAdapterConfig, ChatCompletionRequest, CompletionResult, GoogleAdapter, type GoogleAdapterConfig, LLMAdapter, OllamaAdapter, type OllamaAdapterConfig, OpenAIAdapter, type OpenAIAdapterConfig,
|
|
263
|
+
export { AnthropicAdapter, type AnthropicAdapterConfig, AzureAdapter, type AzureAdapterConfig, ChatCompletionRequest, CompletionResult, GoogleAdapter, type GoogleAdapterConfig, LLMAdapter, OllamaAdapter, type OllamaAdapterConfig, OpenAIAdapter, type OpenAIAdapterConfig, type XAIAdapterConfig, createAnthropicAdapter, createAzureAdapter, createGoogleAdapter, createOllamaAdapter, createOpenAIAdapter, createXAIAdapter };
|
package/dist/adapters/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { L as LLMAdapter, W as WebSearchConfig, C as ChatCompletionRequest, S as StreamEvent, o as CompletionResult } from '../base-
|
|
2
|
-
export { e as AdapterFactory, B as AnthropicContentBlock, E as OpenAIContentBlock, y as attachmentToAnthropicDocument, x as attachmentToAnthropicImage, z as attachmentToOpenAIImage, p as formatMessages, r as formatMessagesForAnthropic, s as formatMessagesForOpenAI, q as formatTools, v as hasImageAttachments, w as hasMediaAttachments, t as messageToAnthropicContent, u as messageToOpenAIContent } from '../base-
|
|
3
|
-
import { d as OllamaModelOptions } from '../types-
|
|
1
|
+
import { L as LLMAdapter, W as WebSearchConfig, C as ChatCompletionRequest, S as StreamEvent, o as CompletionResult } from '../base-iGi9Va6Z.js';
|
|
2
|
+
export { e as AdapterFactory, B as AnthropicContentBlock, E as OpenAIContentBlock, y as attachmentToAnthropicDocument, x as attachmentToAnthropicImage, z as attachmentToOpenAIImage, p as formatMessages, r as formatMessagesForAnthropic, s as formatMessagesForOpenAI, q as formatTools, v as hasImageAttachments, w as hasMediaAttachments, t as messageToAnthropicContent, u as messageToOpenAIContent } from '../base-iGi9Va6Z.js';
|
|
3
|
+
import { d as OllamaModelOptions } from '../types-BctsnC3g.js';
|
|
4
4
|
import '../types-CR8mi9I0.js';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
@@ -25,11 +25,12 @@ interface OpenAIAdapterConfig {
|
|
|
25
25
|
* Supports: GPT-4, GPT-4o, GPT-3.5-turbo, etc.
|
|
26
26
|
*/
|
|
27
27
|
declare class OpenAIAdapter implements LLMAdapter {
|
|
28
|
-
readonly provider
|
|
28
|
+
readonly provider: string;
|
|
29
29
|
readonly model: string;
|
|
30
30
|
private client;
|
|
31
31
|
private config;
|
|
32
32
|
constructor(config: OpenAIAdapterConfig);
|
|
33
|
+
private static resolveProviderName;
|
|
33
34
|
private getClient;
|
|
34
35
|
private shouldUseResponsesApi;
|
|
35
36
|
private buildResponsesInput;
|
|
@@ -194,17 +195,11 @@ declare class GoogleAdapter implements LLMAdapter {
|
|
|
194
195
|
declare function createGoogleAdapter(config: GoogleAdapterConfig): GoogleAdapter;
|
|
195
196
|
|
|
196
197
|
/**
|
|
197
|
-
* xAI Grok
|
|
198
|
+
* xAI Grok Adapter
|
|
198
199
|
*
|
|
199
|
-
* xAI uses an OpenAI-compatible API
|
|
200
|
-
* with
|
|
201
|
-
*
|
|
202
|
-
* Supports: Grok-2, Grok-2-mini, Grok-beta
|
|
203
|
-
* Features: Vision, Tools/Function Calling
|
|
204
|
-
*/
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* xAI adapter configuration
|
|
200
|
+
* xAI uses an OpenAI-compatible API — this is a thin factory
|
|
201
|
+
* that creates an OpenAIAdapter with the xAI endpoint baked in.
|
|
202
|
+
* No separate class needed.
|
|
208
203
|
*/
|
|
209
204
|
interface XAIAdapterConfig {
|
|
210
205
|
apiKey: string;
|
|
@@ -213,28 +208,7 @@ interface XAIAdapterConfig {
|
|
|
213
208
|
temperature?: number;
|
|
214
209
|
maxTokens?: number;
|
|
215
210
|
}
|
|
216
|
-
|
|
217
|
-
* xAI Grok LLM Adapter
|
|
218
|
-
*
|
|
219
|
-
* Uses OpenAI-compatible API with xAI's endpoint
|
|
220
|
-
*/
|
|
221
|
-
declare class XAIAdapter implements LLMAdapter {
|
|
222
|
-
readonly provider = "xai";
|
|
223
|
-
readonly model: string;
|
|
224
|
-
private client;
|
|
225
|
-
private config;
|
|
226
|
-
constructor(config: XAIAdapterConfig);
|
|
227
|
-
private getClient;
|
|
228
|
-
stream(request: ChatCompletionRequest): AsyncGenerator<StreamEvent>;
|
|
229
|
-
/**
|
|
230
|
-
* Non-streaming completion (optional, for debugging)
|
|
231
|
-
*/
|
|
232
|
-
complete(request: ChatCompletionRequest): Promise<CompletionResult>;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Create xAI Grok adapter
|
|
236
|
-
*/
|
|
237
|
-
declare function createXAIAdapter(config: XAIAdapterConfig): XAIAdapter;
|
|
211
|
+
declare function createXAIAdapter(config: XAIAdapterConfig): OpenAIAdapter;
|
|
238
212
|
|
|
239
213
|
/**
|
|
240
214
|
* Azure OpenAI LLM Adapter
|
|
@@ -286,4 +260,4 @@ declare class AzureAdapter implements LLMAdapter {
|
|
|
286
260
|
*/
|
|
287
261
|
declare function createAzureAdapter(config: AzureAdapterConfig): AzureAdapter;
|
|
288
262
|
|
|
289
|
-
export { AnthropicAdapter, type AnthropicAdapterConfig, AzureAdapter, type AzureAdapterConfig, ChatCompletionRequest, CompletionResult, GoogleAdapter, type GoogleAdapterConfig, LLMAdapter, OllamaAdapter, type OllamaAdapterConfig, OpenAIAdapter, type OpenAIAdapterConfig,
|
|
263
|
+
export { AnthropicAdapter, type AnthropicAdapterConfig, AzureAdapter, type AzureAdapterConfig, ChatCompletionRequest, CompletionResult, GoogleAdapter, type GoogleAdapterConfig, LLMAdapter, OllamaAdapter, type OllamaAdapterConfig, OpenAIAdapter, type OpenAIAdapterConfig, type XAIAdapterConfig, createAnthropicAdapter, createAzureAdapter, createGoogleAdapter, createOllamaAdapter, createOpenAIAdapter, createXAIAdapter };
|
package/dist/adapters/index.js
CHANGED
|
@@ -338,11 +338,13 @@ function formatMessagesForOpenAI(messages, systemPrompt) {
|
|
|
338
338
|
content: messageToOpenAIContent(msg)
|
|
339
339
|
});
|
|
340
340
|
} else if (msg.role === "assistant") {
|
|
341
|
+
const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
|
|
341
342
|
const assistantMsg = {
|
|
342
343
|
role: "assistant",
|
|
343
|
-
content:
|
|
344
|
+
// Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
|
|
345
|
+
content: hasToolCalls ? msg.content || null : msg.content
|
|
344
346
|
};
|
|
345
|
-
if (
|
|
347
|
+
if (hasToolCalls) {
|
|
346
348
|
assistantMsg.tool_calls = msg.tool_calls;
|
|
347
349
|
}
|
|
348
350
|
formatted.push(assistantMsg);
|
|
@@ -369,11 +371,18 @@ function generateToolCallId() {
|
|
|
369
371
|
}
|
|
370
372
|
|
|
371
373
|
// src/adapters/openai.ts
|
|
372
|
-
var OpenAIAdapter = class {
|
|
374
|
+
var OpenAIAdapter = class _OpenAIAdapter {
|
|
373
375
|
constructor(config) {
|
|
374
|
-
this.provider = "openai";
|
|
375
376
|
this.config = config;
|
|
376
377
|
this.model = config.model || "gpt-4o";
|
|
378
|
+
this.provider = _OpenAIAdapter.resolveProviderName(config.baseUrl);
|
|
379
|
+
}
|
|
380
|
+
static resolveProviderName(baseUrl) {
|
|
381
|
+
if (!baseUrl) return "openai";
|
|
382
|
+
if (baseUrl.includes("generativelanguage.googleapis.com")) return "google";
|
|
383
|
+
if (baseUrl.includes("x.ai")) return "xai";
|
|
384
|
+
if (baseUrl.includes("azure")) return "azure";
|
|
385
|
+
return "openai";
|
|
377
386
|
}
|
|
378
387
|
async getClient() {
|
|
379
388
|
if (!this.client) {
|
|
@@ -535,6 +544,9 @@ var OpenAIAdapter = class {
|
|
|
535
544
|
let messages;
|
|
536
545
|
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
537
546
|
const processedMessages = request.rawMessages.map((msg) => {
|
|
547
|
+
if (msg.role === "assistant" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0 && msg.content === "") {
|
|
548
|
+
return { ...msg, content: null };
|
|
549
|
+
}
|
|
538
550
|
const hasAttachments = msg.attachments && Array.isArray(msg.attachments) && msg.attachments.length > 0;
|
|
539
551
|
if (hasAttachments) {
|
|
540
552
|
const content = [];
|
|
@@ -656,15 +668,18 @@ var OpenAIAdapter = class {
|
|
|
656
668
|
args: currentToolCall.arguments
|
|
657
669
|
};
|
|
658
670
|
}
|
|
671
|
+
const tcExtraContent = toolCall.extra_content;
|
|
659
672
|
currentToolCall = {
|
|
660
673
|
id: toolCall.id,
|
|
661
674
|
name: toolCall.function?.name || "",
|
|
662
|
-
arguments: toolCall.function?.arguments || ""
|
|
675
|
+
arguments: toolCall.function?.arguments || "",
|
|
676
|
+
...tcExtraContent ? { extra_content: tcExtraContent } : {}
|
|
663
677
|
};
|
|
664
678
|
yield {
|
|
665
679
|
type: "action:start",
|
|
666
680
|
id: currentToolCall.id,
|
|
667
|
-
name: currentToolCall.name
|
|
681
|
+
name: currentToolCall.name,
|
|
682
|
+
...currentToolCall.extra_content ? { extra_content: currentToolCall.extra_content } : {}
|
|
668
683
|
};
|
|
669
684
|
} else if (currentToolCall && toolCall.function?.arguments) {
|
|
670
685
|
currentToolCall.arguments += toolCall.function.arguments;
|
|
@@ -698,7 +713,7 @@ var OpenAIAdapter = class {
|
|
|
698
713
|
yield {
|
|
699
714
|
type: "error",
|
|
700
715
|
message: error instanceof Error ? error.message : "Unknown error",
|
|
701
|
-
code:
|
|
716
|
+
code: `${this.provider.toUpperCase()}_ERROR`
|
|
702
717
|
};
|
|
703
718
|
}
|
|
704
719
|
}
|
|
@@ -709,12 +724,19 @@ var OpenAIAdapter = class {
|
|
|
709
724
|
const client = await this.getClient();
|
|
710
725
|
let messages;
|
|
711
726
|
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
712
|
-
|
|
713
|
-
|
|
727
|
+
const sanitized = request.rawMessages.map((msg) => {
|
|
728
|
+
if (msg.role === "assistant" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0 && msg.content === "") {
|
|
729
|
+
return { ...msg, content: null };
|
|
730
|
+
}
|
|
731
|
+
return msg;
|
|
732
|
+
});
|
|
733
|
+
if (request.systemPrompt && !sanitized.some((message2) => message2.role === "system")) {
|
|
714
734
|
messages = [
|
|
715
735
|
{ role: "system", content: request.systemPrompt },
|
|
716
|
-
...
|
|
736
|
+
...sanitized
|
|
717
737
|
];
|
|
738
|
+
} else {
|
|
739
|
+
messages = sanitized;
|
|
718
740
|
}
|
|
719
741
|
} else {
|
|
720
742
|
messages = formatMessagesForOpenAI(
|
|
@@ -756,7 +778,8 @@ var OpenAIAdapter = class {
|
|
|
756
778
|
} catch {
|
|
757
779
|
return {};
|
|
758
780
|
}
|
|
759
|
-
})()
|
|
781
|
+
})(),
|
|
782
|
+
...toolCall.extra_content ? { extra_content: toolCall.extra_content } : {}
|
|
760
783
|
})) ?? [],
|
|
761
784
|
usage: response.usage ? {
|
|
762
785
|
promptTokens: response.usage.prompt_tokens,
|
|
@@ -1987,187 +2010,14 @@ function extractDomain3(url) {
|
|
|
1987
2010
|
|
|
1988
2011
|
// src/adapters/xai.ts
|
|
1989
2012
|
var XAI_BASE_URL = "https://api.x.ai/v1";
|
|
1990
|
-
var XAIAdapter = class {
|
|
1991
|
-
constructor(config) {
|
|
1992
|
-
this.provider = "xai";
|
|
1993
|
-
this.config = config;
|
|
1994
|
-
this.model = config.model || "grok-2";
|
|
1995
|
-
}
|
|
1996
|
-
async getClient() {
|
|
1997
|
-
if (!this.client) {
|
|
1998
|
-
const { default: OpenAI } = await import('openai');
|
|
1999
|
-
this.client = new OpenAI({
|
|
2000
|
-
apiKey: this.config.apiKey,
|
|
2001
|
-
baseURL: this.config.baseUrl || XAI_BASE_URL
|
|
2002
|
-
});
|
|
2003
|
-
}
|
|
2004
|
-
return this.client;
|
|
2005
|
-
}
|
|
2006
|
-
async *stream(request) {
|
|
2007
|
-
const client = await this.getClient();
|
|
2008
|
-
let messages;
|
|
2009
|
-
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
2010
|
-
const processedMessages = request.rawMessages.map((msg) => {
|
|
2011
|
-
const hasAttachments = msg.attachments && Array.isArray(msg.attachments) && msg.attachments.length > 0;
|
|
2012
|
-
if (hasAttachments) {
|
|
2013
|
-
const content = [];
|
|
2014
|
-
if (msg.content) {
|
|
2015
|
-
content.push({ type: "text", text: msg.content });
|
|
2016
|
-
}
|
|
2017
|
-
for (const attachment of msg.attachments) {
|
|
2018
|
-
if (attachment.type === "image") {
|
|
2019
|
-
let imageUrl = attachment.data;
|
|
2020
|
-
if (!imageUrl.startsWith("data:")) {
|
|
2021
|
-
imageUrl = `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
|
|
2022
|
-
}
|
|
2023
|
-
content.push({
|
|
2024
|
-
type: "image_url",
|
|
2025
|
-
image_url: { url: imageUrl, detail: "auto" }
|
|
2026
|
-
});
|
|
2027
|
-
}
|
|
2028
|
-
}
|
|
2029
|
-
return { ...msg, content, attachments: void 0 };
|
|
2030
|
-
}
|
|
2031
|
-
return msg;
|
|
2032
|
-
});
|
|
2033
|
-
if (request.systemPrompt) {
|
|
2034
|
-
const hasSystem = processedMessages.some((m) => m.role === "system");
|
|
2035
|
-
if (!hasSystem) {
|
|
2036
|
-
messages = [
|
|
2037
|
-
{ role: "system", content: request.systemPrompt },
|
|
2038
|
-
...processedMessages
|
|
2039
|
-
];
|
|
2040
|
-
} else {
|
|
2041
|
-
messages = processedMessages;
|
|
2042
|
-
}
|
|
2043
|
-
} else {
|
|
2044
|
-
messages = processedMessages;
|
|
2045
|
-
}
|
|
2046
|
-
} else {
|
|
2047
|
-
messages = formatMessagesForOpenAI(
|
|
2048
|
-
request.messages,
|
|
2049
|
-
request.systemPrompt
|
|
2050
|
-
);
|
|
2051
|
-
}
|
|
2052
|
-
const tools = request.actions?.length ? formatTools(request.actions) : void 0;
|
|
2053
|
-
const messageId = generateMessageId();
|
|
2054
|
-
yield { type: "message:start", id: messageId };
|
|
2055
|
-
try {
|
|
2056
|
-
const payload = {
|
|
2057
|
-
model: request.config?.model || this.model,
|
|
2058
|
-
messages,
|
|
2059
|
-
tools,
|
|
2060
|
-
temperature: request.config?.temperature ?? this.config.temperature,
|
|
2061
|
-
max_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
2062
|
-
stream: true
|
|
2063
|
-
};
|
|
2064
|
-
logProviderPayload("xai", "request payload", payload, request.debug);
|
|
2065
|
-
const stream = await client.chat.completions.create(payload);
|
|
2066
|
-
let currentToolCall = null;
|
|
2067
|
-
for await (const chunk of stream) {
|
|
2068
|
-
logProviderPayload("xai", "stream chunk", chunk, request.debug);
|
|
2069
|
-
if (request.signal?.aborted) {
|
|
2070
|
-
break;
|
|
2071
|
-
}
|
|
2072
|
-
const delta = chunk.choices[0]?.delta;
|
|
2073
|
-
if (delta?.content) {
|
|
2074
|
-
yield { type: "message:delta", content: delta.content };
|
|
2075
|
-
}
|
|
2076
|
-
if (delta?.tool_calls) {
|
|
2077
|
-
for (const toolCall of delta.tool_calls) {
|
|
2078
|
-
if (toolCall.id) {
|
|
2079
|
-
if (currentToolCall) {
|
|
2080
|
-
yield {
|
|
2081
|
-
type: "action:args",
|
|
2082
|
-
id: currentToolCall.id,
|
|
2083
|
-
args: currentToolCall.arguments
|
|
2084
|
-
};
|
|
2085
|
-
}
|
|
2086
|
-
currentToolCall = {
|
|
2087
|
-
id: toolCall.id,
|
|
2088
|
-
name: toolCall.function?.name || "",
|
|
2089
|
-
arguments: toolCall.function?.arguments || ""
|
|
2090
|
-
};
|
|
2091
|
-
yield {
|
|
2092
|
-
type: "action:start",
|
|
2093
|
-
id: currentToolCall.id,
|
|
2094
|
-
name: currentToolCall.name
|
|
2095
|
-
};
|
|
2096
|
-
} else if (currentToolCall && toolCall.function?.arguments) {
|
|
2097
|
-
currentToolCall.arguments += toolCall.function.arguments;
|
|
2098
|
-
}
|
|
2099
|
-
}
|
|
2100
|
-
}
|
|
2101
|
-
if (chunk.choices[0]?.finish_reason) {
|
|
2102
|
-
if (currentToolCall) {
|
|
2103
|
-
yield {
|
|
2104
|
-
type: "action:args",
|
|
2105
|
-
id: currentToolCall.id,
|
|
2106
|
-
args: currentToolCall.arguments
|
|
2107
|
-
};
|
|
2108
|
-
}
|
|
2109
|
-
}
|
|
2110
|
-
}
|
|
2111
|
-
yield { type: "message:end" };
|
|
2112
|
-
yield { type: "done" };
|
|
2113
|
-
} catch (error) {
|
|
2114
|
-
yield {
|
|
2115
|
-
type: "error",
|
|
2116
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
2117
|
-
code: "XAI_ERROR"
|
|
2118
|
-
};
|
|
2119
|
-
}
|
|
2120
|
-
}
|
|
2121
|
-
/**
|
|
2122
|
-
* Non-streaming completion (optional, for debugging)
|
|
2123
|
-
*/
|
|
2124
|
-
async complete(request) {
|
|
2125
|
-
const client = await this.getClient();
|
|
2126
|
-
let messages;
|
|
2127
|
-
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
2128
|
-
messages = request.rawMessages;
|
|
2129
|
-
if (request.systemPrompt) {
|
|
2130
|
-
const hasSystem = messages.some((m) => m.role === "system");
|
|
2131
|
-
if (!hasSystem) {
|
|
2132
|
-
messages = [
|
|
2133
|
-
{ role: "system", content: request.systemPrompt },
|
|
2134
|
-
...messages
|
|
2135
|
-
];
|
|
2136
|
-
}
|
|
2137
|
-
}
|
|
2138
|
-
} else {
|
|
2139
|
-
messages = formatMessagesForOpenAI(
|
|
2140
|
-
request.messages,
|
|
2141
|
-
request.systemPrompt
|
|
2142
|
-
);
|
|
2143
|
-
}
|
|
2144
|
-
const tools = request.actions?.length ? formatTools(request.actions) : void 0;
|
|
2145
|
-
const payload = {
|
|
2146
|
-
model: request.config?.model || this.model,
|
|
2147
|
-
messages,
|
|
2148
|
-
tools,
|
|
2149
|
-
temperature: request.config?.temperature ?? this.config.temperature,
|
|
2150
|
-
max_tokens: request.config?.maxTokens ?? this.config.maxTokens
|
|
2151
|
-
};
|
|
2152
|
-
logProviderPayload("xai", "request payload", payload, request.debug);
|
|
2153
|
-
const response = await client.chat.completions.create(payload);
|
|
2154
|
-
logProviderPayload("xai", "response payload", response, request.debug);
|
|
2155
|
-
const choice = response.choices[0];
|
|
2156
|
-
const message = choice?.message;
|
|
2157
|
-
const toolCalls = (message?.tool_calls || []).map((tc) => ({
|
|
2158
|
-
id: tc.id,
|
|
2159
|
-
name: tc.function.name,
|
|
2160
|
-
args: JSON.parse(tc.function.arguments || "{}")
|
|
2161
|
-
}));
|
|
2162
|
-
return {
|
|
2163
|
-
content: message?.content || "",
|
|
2164
|
-
toolCalls,
|
|
2165
|
-
rawResponse: response
|
|
2166
|
-
};
|
|
2167
|
-
}
|
|
2168
|
-
};
|
|
2169
2013
|
function createXAIAdapter(config) {
|
|
2170
|
-
return
|
|
2014
|
+
return createOpenAIAdapter({
|
|
2015
|
+
apiKey: config.apiKey,
|
|
2016
|
+
model: config.model || "grok-3",
|
|
2017
|
+
baseUrl: config.baseUrl || XAI_BASE_URL,
|
|
2018
|
+
temperature: config.temperature,
|
|
2019
|
+
maxTokens: config.maxTokens
|
|
2020
|
+
});
|
|
2171
2021
|
}
|
|
2172
2022
|
|
|
2173
2023
|
// src/adapters/azure.ts
|
|
@@ -2370,7 +2220,6 @@ exports.AzureAdapter = AzureAdapter;
|
|
|
2370
2220
|
exports.GoogleAdapter = GoogleAdapter;
|
|
2371
2221
|
exports.OllamaAdapter = OllamaAdapter;
|
|
2372
2222
|
exports.OpenAIAdapter = OpenAIAdapter;
|
|
2373
|
-
exports.XAIAdapter = XAIAdapter;
|
|
2374
2223
|
exports.attachmentToAnthropicDocument = attachmentToAnthropicDocument;
|
|
2375
2224
|
exports.attachmentToAnthropicImage = attachmentToAnthropicImage;
|
|
2376
2225
|
exports.attachmentToOpenAIImage = attachmentToOpenAIImage;
|