@jaypie/llm 1.2.15 → 1.2.17

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.
@@ -64,8 +64,20 @@ export declare const PROVIDER: {
64
64
  readonly USER: "user";
65
65
  };
66
66
  };
67
+ readonly XAI: {
68
+ readonly API_KEY: "XAI_API_KEY";
69
+ readonly BASE_URL: "https://api.x.ai/v1";
70
+ readonly MODEL: {
71
+ readonly DEFAULT: "grok-4-1-fast-reasoning";
72
+ readonly LARGE: "grok-4-1-fast-reasoning";
73
+ readonly SMALL: "grok-3";
74
+ readonly TINY: "grok-3-mini";
75
+ };
76
+ readonly MODEL_MATCH_WORDS: readonly ["grok", "xai"];
77
+ readonly NAME: "xai";
78
+ };
67
79
  };
68
- export type LlmProviderName = typeof PROVIDER.ANTHROPIC.NAME | typeof PROVIDER.GEMINI.NAME | typeof PROVIDER.OPENAI.NAME | typeof PROVIDER.OPENROUTER.NAME;
80
+ export type LlmProviderName = typeof PROVIDER.ANTHROPIC.NAME | typeof PROVIDER.GEMINI.NAME | typeof PROVIDER.OPENAI.NAME | typeof PROVIDER.OPENROUTER.NAME | typeof PROVIDER.XAI.NAME;
69
81
  export declare const DEFAULT: {
70
82
  readonly MODEL: {
71
83
  readonly BASE: "gpt-5.2";
@@ -85,9 +97,9 @@ export declare const DEFAULT: {
85
97
  };
86
98
  };
87
99
  export declare const ALL: {
88
- readonly BASE: readonly ["claude-sonnet-4-5", "gemini-3-pro-preview", "gpt-5.2"];
89
- readonly COMBINED: readonly ["claude-sonnet-4-5", "claude-opus-4-5", "claude-sonnet-4-5", "claude-haiku-4-5", "gemini-3-pro-preview", "gemini-3-pro-preview", "gemini-3-flash-preview", "gemini-3-flash-preview", "gpt-5.2", "gpt-5.2-pro", "gpt-5-mini", "gpt-5-nano"];
90
- readonly LARGE: readonly ["claude-opus-4-5", "gemini-3-pro-preview", "gpt-5.2-pro"];
91
- readonly SMALL: readonly ["claude-sonnet-4-5", "gemini-3-flash-preview", "gpt-5-mini"];
92
- readonly TINY: readonly ["claude-haiku-4-5", "gemini-3-flash-preview", "gpt-5-nano"];
100
+ readonly BASE: readonly ["claude-sonnet-4-5", "gemini-3-pro-preview", "gpt-5.2", "grok-4-1-fast-reasoning"];
101
+ readonly COMBINED: readonly ["claude-sonnet-4-5", "claude-opus-4-5", "claude-sonnet-4-5", "claude-haiku-4-5", "gemini-3-pro-preview", "gemini-3-pro-preview", "gemini-3-flash-preview", "gemini-3-flash-preview", "gpt-5.2", "gpt-5.2-pro", "gpt-5-mini", "gpt-5-nano", "grok-4-1-fast-reasoning", "grok-4-1-fast-reasoning", "grok-3", "grok-3-mini"];
102
+ readonly LARGE: readonly ["claude-opus-4-5", "gemini-3-pro-preview", "gpt-5.2-pro", "grok-4-1-fast-reasoning"];
103
+ readonly SMALL: readonly ["claude-sonnet-4-5", "gemini-3-flash-preview", "gpt-5-mini", "grok-3"];
104
+ readonly TINY: readonly ["claude-haiku-4-5", "gemini-3-flash-preview", "gpt-5-nano", "grok-3-mini"];
93
105
  };
@@ -38,6 +38,12 @@ const FIRST_CLASS_PROVIDER = {
38
38
  SMALL: "z-ai/glm-4.7",
39
39
  TINY: "z-ai/glm-4.7",
40
40
  },
41
+ XAI: {
42
+ DEFAULT: "grok-4-1-fast-reasoning",
43
+ LARGE: "grok-4-1-fast-reasoning",
44
+ SMALL: "grok-3",
45
+ TINY: "grok-3-mini",
46
+ },
41
47
  };
42
48
  const PROVIDER = {
43
49
  ANTHROPIC: {
@@ -118,6 +124,19 @@ const PROVIDER = {
118
124
  USER: "user",
119
125
  },
120
126
  },
127
+ XAI: {
128
+ // https://docs.x.ai/docs/models
129
+ API_KEY: "XAI_API_KEY",
130
+ BASE_URL: "https://api.x.ai/v1",
131
+ MODEL: {
132
+ DEFAULT: FIRST_CLASS_PROVIDER.XAI.DEFAULT,
133
+ LARGE: FIRST_CLASS_PROVIDER.XAI.LARGE,
134
+ SMALL: FIRST_CLASS_PROVIDER.XAI.SMALL,
135
+ TINY: FIRST_CLASS_PROVIDER.XAI.TINY,
136
+ },
137
+ MODEL_MATCH_WORDS: ["grok", "xai"],
138
+ NAME: "xai",
139
+ },
121
140
  };
122
141
  // Last: Defaults
123
142
  const DEFAULT = {
@@ -135,6 +154,7 @@ const ALL = {
135
154
  PROVIDER.ANTHROPIC.MODEL.DEFAULT,
136
155
  PROVIDER.GEMINI.MODEL.DEFAULT,
137
156
  PROVIDER.OPENAI.MODEL.DEFAULT,
157
+ PROVIDER.XAI.MODEL.DEFAULT,
138
158
  ],
139
159
  COMBINED: [
140
160
  PROVIDER.ANTHROPIC.MODEL.DEFAULT,
@@ -149,21 +169,28 @@ const ALL = {
149
169
  PROVIDER.OPENAI.MODEL.LARGE,
150
170
  PROVIDER.OPENAI.MODEL.SMALL,
151
171
  PROVIDER.OPENAI.MODEL.TINY,
172
+ PROVIDER.XAI.MODEL.DEFAULT,
173
+ PROVIDER.XAI.MODEL.LARGE,
174
+ PROVIDER.XAI.MODEL.SMALL,
175
+ PROVIDER.XAI.MODEL.TINY,
152
176
  ],
153
177
  LARGE: [
154
178
  PROVIDER.ANTHROPIC.MODEL.LARGE,
155
179
  PROVIDER.GEMINI.MODEL.LARGE,
156
180
  PROVIDER.OPENAI.MODEL.LARGE,
181
+ PROVIDER.XAI.MODEL.LARGE,
157
182
  ],
158
183
  SMALL: [
159
184
  PROVIDER.ANTHROPIC.MODEL.SMALL,
160
185
  PROVIDER.GEMINI.MODEL.SMALL,
161
186
  PROVIDER.OPENAI.MODEL.SMALL,
187
+ PROVIDER.XAI.MODEL.SMALL,
162
188
  ],
163
189
  TINY: [
164
190
  PROVIDER.ANTHROPIC.MODEL.TINY,
165
191
  PROVIDER.GEMINI.MODEL.TINY,
166
192
  PROVIDER.OPENAI.MODEL.TINY,
193
+ PROVIDER.XAI.MODEL.TINY,
167
194
  ],
168
195
  };
169
196
 
@@ -214,6 +241,12 @@ function determineModelProvider(input) {
214
241
  provider: PROVIDER.OPENROUTER.NAME,
215
242
  };
216
243
  }
244
+ if (input === PROVIDER.XAI.NAME) {
245
+ return {
246
+ model: PROVIDER.XAI.MODEL.DEFAULT,
247
+ provider: PROVIDER.XAI.NAME,
248
+ };
249
+ }
217
250
  // Check if input matches an Anthropic model exactly
218
251
  for (const [, modelValue] of Object.entries(PROVIDER.ANTHROPIC.MODEL)) {
219
252
  if (input === modelValue) {
@@ -250,6 +283,15 @@ function determineModelProvider(input) {
250
283
  };
251
284
  }
252
285
  }
286
+ // Check if input matches an xAI model exactly
287
+ for (const [, modelValue] of Object.entries(PROVIDER.XAI.MODEL)) {
288
+ if (input === modelValue) {
289
+ return {
290
+ model: input,
291
+ provider: PROVIDER.XAI.NAME,
292
+ };
293
+ }
294
+ }
253
295
  // Assume OpenRouter for models containing "/" (e.g., "openai/gpt-4", "anthropic/claude-3-opus")
254
296
  // This check must come before match words so that "openai/gpt-4" is not matched by "openai" keyword
255
297
  if (input.includes("/")) {
@@ -296,6 +338,15 @@ function determineModelProvider(input) {
296
338
  }
297
339
  }
298
340
  }
341
+ // Check xAI match words
342
+ for (const matchWord of PROVIDER.XAI.MODEL_MATCH_WORDS) {
343
+ if (lowerInput.includes(matchWord)) {
344
+ return {
345
+ model: input,
346
+ provider: PROVIDER.XAI.NAME,
347
+ };
348
+ }
349
+ }
299
350
  // Check OpenRouter match words
300
351
  for (const matchWord of PROVIDER.OPENROUTER.MODEL_MATCH_WORDS) {
301
352
  if (lowerInput.includes(matchWord)) {
@@ -507,8 +558,8 @@ function formatOperateInput(input, options) {
507
558
  return [input];
508
559
  }
509
560
 
510
- const getLogger$4 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
511
- const log$1 = getLogger$4();
561
+ const getLogger$5 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
562
+ const log$1 = getLogger$5();
512
563
 
513
564
  // Turn policy constants
514
565
  const MAX_TURNS_ABSOLUTE_LIMIT = 72;
@@ -1630,6 +1681,11 @@ class GeminiAdapter extends BaseProviderAdapter {
1630
1681
  name: functionCall.name || "",
1631
1682
  arguments: functionCall.args || {},
1632
1683
  };
1684
+ // Preserve thoughtSignature for Gemini 3 models
1685
+ // Required to maintain tool call context between turns
1686
+ const metadata = part.thoughtSignature
1687
+ ? { thoughtSignature: part.thoughtSignature }
1688
+ : undefined;
1633
1689
  // Emit the function call immediately
1634
1690
  yield {
1635
1691
  type: exports.LlmStreamChunkType.ToolCall,
@@ -1637,6 +1693,7 @@ class GeminiAdapter extends BaseProviderAdapter {
1637
1693
  id: currentFunctionCall.id,
1638
1694
  name: currentFunctionCall.name,
1639
1695
  arguments: JSON.stringify(currentFunctionCall.arguments),
1696
+ metadata,
1640
1697
  },
1641
1698
  };
1642
1699
  currentFunctionCall = null;
@@ -2323,6 +2380,9 @@ class OpenAiAdapter extends BaseProviderAdapter {
2323
2380
  id: currentFunctionCall.callId,
2324
2381
  name: currentFunctionCall.name,
2325
2382
  arguments: currentFunctionCall.arguments,
2383
+ // Preserve the item ID (fc_...) separately from call_id (call_...)
2384
+ // OpenAI Responses API requires both with correct prefixes
2385
+ metadata: { itemId: currentFunctionCall.id },
2326
2386
  },
2327
2387
  };
2328
2388
  currentFunctionCall = null;
@@ -3149,6 +3209,23 @@ class OpenRouterAdapter extends BaseProviderAdapter {
3149
3209
  // Export singleton instance
3150
3210
  const openRouterAdapter = new OpenRouterAdapter();
3151
3211
 
3212
+ /**
3213
+ * XaiAdapter extends OpenAiAdapter since xAI (Grok) uses an OpenAI-compatible API.
3214
+ * Only the name and default model are overridden; all request building, response parsing,
3215
+ * error classification, tool handling, and streaming are inherited.
3216
+ */
3217
+ class XaiAdapter extends OpenAiAdapter {
3218
+ constructor() {
3219
+ super(...arguments);
3220
+ // @ts-expect-error Narrowing override: xAI name differs from parent's literal "openai"
3221
+ this.name = PROVIDER.XAI.NAME;
3222
+ // @ts-expect-error Narrowing override: xAI default model differs from parent's literal
3223
+ this.defaultModel = PROVIDER.XAI.MODEL.DEFAULT;
3224
+ }
3225
+ }
3226
+ // Export singleton instance
3227
+ const xaiAdapter = new XaiAdapter();
3228
+
3152
3229
  const DEFAULT_TOOL_TYPE = "function";
3153
3230
  const log = log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
3154
3231
  function logToolMessage(message, context) {
@@ -4725,13 +4802,22 @@ class StreamLoop {
4725
4802
  if (collectedToolCalls.length > 0 && state.toolkit && state.maxTurns > 1) {
4726
4803
  // Add tool calls to history
4727
4804
  for (const toolCall of collectedToolCalls) {
4728
- state.currentInput.push({
4805
+ // Extract provider-specific metadata from the stream chunk
4806
+ const metadata = toolCall.raw?.metadata;
4807
+ const historyItem = {
4729
4808
  type: exports.LlmMessageType.FunctionCall,
4730
4809
  name: toolCall.name,
4731
4810
  arguments: toolCall.arguments,
4732
4811
  call_id: toolCall.callId,
4733
- id: toolCall.callId,
4734
- });
4812
+ // Use provider item ID if available (e.g., OpenAI fc_... prefix),
4813
+ // otherwise fall back to callId
4814
+ id: metadata?.itemId || toolCall.callId,
4815
+ };
4816
+ // Preserve provider-specific fields (e.g., Gemini thoughtSignature)
4817
+ if (metadata?.thoughtSignature) {
4818
+ historyItem.thoughtSignature = metadata.thoughtSignature;
4819
+ }
4820
+ state.currentInput.push(historyItem);
4735
4821
  }
4736
4822
  return { shouldContinue: true, toolCalls: collectedToolCalls };
4737
4823
  }
@@ -4867,10 +4953,10 @@ async function loadSdk$2() {
4867
4953
  }
4868
4954
  }
4869
4955
  // Logger
4870
- const getLogger$3 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
4956
+ const getLogger$4 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
4871
4957
  // Client initialization
4872
- async function initializeClient$3({ apiKey, } = {}) {
4873
- const logger = getLogger$3();
4958
+ async function initializeClient$4({ apiKey, } = {}) {
4959
+ const logger = getLogger$4();
4874
4960
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("ANTHROPIC_API_KEY"));
4875
4961
  if (!resolvedApiKey) {
4876
4962
  throw new errors.ConfigurationError("The application could not resolve the required API key: ANTHROPIC_API_KEY");
@@ -4896,7 +4982,7 @@ function formatUserMessage$3(message, { data, placeholders } = {}) {
4896
4982
  };
4897
4983
  }
4898
4984
  function prepareMessages$3(message, { data, placeholders } = {}) {
4899
- const logger = getLogger$3();
4985
+ const logger = getLogger$4();
4900
4986
  const messages = [];
4901
4987
  // Add user message (necessary for all requests)
4902
4988
  const userMessage = formatUserMessage$3(message, { data, placeholders });
@@ -4985,7 +5071,7 @@ async function createStructuredCompletion$1(client, messages, model, responseSch
4985
5071
  // Main class implementation
4986
5072
  class AnthropicProvider {
4987
5073
  constructor(model = PROVIDER.ANTHROPIC.MODEL.DEFAULT, { apiKey } = {}) {
4988
- this.log = getLogger$3();
5074
+ this.log = getLogger$4();
4989
5075
  this.conversationHistory = [];
4990
5076
  this.model = model;
4991
5077
  this.apiKey = apiKey;
@@ -4994,7 +5080,7 @@ class AnthropicProvider {
4994
5080
  if (this._client) {
4995
5081
  return this._client;
4996
5082
  }
4997
- this._client = await initializeClient$3({ apiKey: this.apiKey });
5083
+ this._client = await initializeClient$4({ apiKey: this.apiKey });
4998
5084
  return this._client;
4999
5085
  }
5000
5086
  async getOperateLoop() {
@@ -5083,10 +5169,10 @@ async function loadSdk$1() {
5083
5169
  }
5084
5170
  }
5085
5171
  // Logger
5086
- const getLogger$2 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5172
+ const getLogger$3 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5087
5173
  // Client initialization
5088
- async function initializeClient$2({ apiKey, } = {}) {
5089
- const logger = getLogger$2();
5174
+ async function initializeClient$3({ apiKey, } = {}) {
5175
+ const logger = getLogger$3();
5090
5176
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("GEMINI_API_KEY"));
5091
5177
  if (!resolvedApiKey) {
5092
5178
  throw new errors.ConfigurationError("The application could not resolve the requested keys");
@@ -5106,7 +5192,7 @@ function formatUserMessage$2(message, { data, placeholders } = {}) {
5106
5192
  };
5107
5193
  }
5108
5194
  function prepareMessages$2(message, { data, placeholders } = {}) {
5109
- const logger = getLogger$2();
5195
+ const logger = getLogger$3();
5110
5196
  const messages = [];
5111
5197
  let systemInstruction;
5112
5198
  // Note: Gemini handles system prompts differently via systemInstruction config
@@ -5120,7 +5206,7 @@ function prepareMessages$2(message, { data, placeholders } = {}) {
5120
5206
 
5121
5207
  class GeminiProvider {
5122
5208
  constructor(model = PROVIDER.GEMINI.MODEL.DEFAULT, { apiKey } = {}) {
5123
- this.log = getLogger$2();
5209
+ this.log = getLogger$3();
5124
5210
  this.conversationHistory = [];
5125
5211
  this.model = model;
5126
5212
  this.apiKey = apiKey;
@@ -5129,7 +5215,7 @@ class GeminiProvider {
5129
5215
  if (this._client) {
5130
5216
  return this._client;
5131
5217
  }
5132
- this._client = await initializeClient$2({ apiKey: this.apiKey });
5218
+ this._client = await initializeClient$3({ apiKey: this.apiKey });
5133
5219
  return this._client;
5134
5220
  }
5135
5221
  async getOperateLoop() {
@@ -5234,10 +5320,10 @@ class GeminiProvider {
5234
5320
  }
5235
5321
 
5236
5322
  // Logger
5237
- const getLogger$1 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5323
+ const getLogger$2 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5238
5324
  // Client initialization
5239
- async function initializeClient$1({ apiKey, } = {}) {
5240
- const logger = getLogger$1();
5325
+ async function initializeClient$2({ apiKey, } = {}) {
5326
+ const logger = getLogger$2();
5241
5327
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("OPENAI_API_KEY"));
5242
5328
  if (!resolvedApiKey) {
5243
5329
  throw new errors.ConfigurationError("The application could not resolve the requested keys");
@@ -5265,7 +5351,7 @@ function formatUserMessage$1(message, { data, placeholders } = {}) {
5265
5351
  };
5266
5352
  }
5267
5353
  function prepareMessages$1(message, { system, data, placeholders } = {}) {
5268
- const logger = getLogger$1();
5354
+ const logger = getLogger$2();
5269
5355
  const messages = [];
5270
5356
  if (system) {
5271
5357
  const systemMessage = formatSystemMessage$1(system, { data, placeholders });
@@ -5279,7 +5365,7 @@ function prepareMessages$1(message, { system, data, placeholders } = {}) {
5279
5365
  }
5280
5366
  // Completion requests
5281
5367
  async function createStructuredCompletion(client, { messages, responseSchema, model, }) {
5282
- const logger = getLogger$1();
5368
+ const logger = getLogger$2();
5283
5369
  logger.trace("Using structured output");
5284
5370
  const zodSchema = responseSchema instanceof v4.z.ZodType
5285
5371
  ? responseSchema
@@ -5310,7 +5396,7 @@ async function createStructuredCompletion(client, { messages, responseSchema, mo
5310
5396
  return completion.choices[0].message.parsed;
5311
5397
  }
5312
5398
  async function createTextCompletion(client, { messages, model, }) {
5313
- const logger = getLogger$1();
5399
+ const logger = getLogger$2();
5314
5400
  logger.trace("Using text output (unstructured)");
5315
5401
  const completion = await client.chat.completions.create({
5316
5402
  messages,
@@ -5322,7 +5408,7 @@ async function createTextCompletion(client, { messages, model, }) {
5322
5408
 
5323
5409
  class OpenAiProvider {
5324
5410
  constructor(model = PROVIDER.OPENAI.MODEL.DEFAULT, { apiKey } = {}) {
5325
- this.log = getLogger$1();
5411
+ this.log = getLogger$2();
5326
5412
  this.conversationHistory = [];
5327
5413
  this.model = model;
5328
5414
  this.apiKey = apiKey;
@@ -5331,7 +5417,7 @@ class OpenAiProvider {
5331
5417
  if (this._client) {
5332
5418
  return this._client;
5333
5419
  }
5334
- this._client = await initializeClient$1({ apiKey: this.apiKey });
5420
+ this._client = await initializeClient$2({ apiKey: this.apiKey });
5335
5421
  return this._client;
5336
5422
  }
5337
5423
  async getOperateLoop() {
@@ -5418,10 +5504,10 @@ async function loadSdk() {
5418
5504
  }
5419
5505
  }
5420
5506
  // Logger
5421
- const getLogger = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5507
+ const getLogger$1 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5422
5508
  // Client initialization
5423
- async function initializeClient({ apiKey, } = {}) {
5424
- const logger = getLogger();
5509
+ async function initializeClient$1({ apiKey, } = {}) {
5510
+ const logger = getLogger$1();
5425
5511
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("OPENROUTER_API_KEY"));
5426
5512
  if (!resolvedApiKey) {
5427
5513
  throw new errors.ConfigurationError("The application could not resolve the requested keys");
@@ -5454,7 +5540,7 @@ function formatUserMessage(message, { data, placeholders } = {}) {
5454
5540
  };
5455
5541
  }
5456
5542
  function prepareMessages(message, { system, data, placeholders } = {}) {
5457
- const logger = getLogger();
5543
+ const logger = getLogger$1();
5458
5544
  const messages = [];
5459
5545
  if (system) {
5460
5546
  const systemMessage = formatSystemMessage(system, { data, placeholders });
@@ -5469,7 +5555,7 @@ function prepareMessages(message, { system, data, placeholders } = {}) {
5469
5555
 
5470
5556
  class OpenRouterProvider {
5471
5557
  constructor(model = getDefaultModel(), { apiKey } = {}) {
5472
- this.log = getLogger();
5558
+ this.log = getLogger$1();
5473
5559
  this.conversationHistory = [];
5474
5560
  this.model = model;
5475
5561
  this.apiKey = apiKey;
@@ -5478,7 +5564,7 @@ class OpenRouterProvider {
5478
5564
  if (this._client) {
5479
5565
  return this._client;
5480
5566
  }
5481
- this._client = await initializeClient({ apiKey: this.apiKey });
5567
+ this._client = await initializeClient$1({ apiKey: this.apiKey });
5482
5568
  return this._client;
5483
5569
  }
5484
5570
  async getOperateLoop() {
@@ -5566,6 +5652,107 @@ class OpenRouterProvider {
5566
5652
  }
5567
5653
  }
5568
5654
 
5655
+ // Logger
5656
+ const getLogger = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5657
+ // Client initialization
5658
+ async function initializeClient({ apiKey, } = {}) {
5659
+ const logger = getLogger();
5660
+ const resolvedApiKey = apiKey || (await aws.getEnvSecret(PROVIDER.XAI.API_KEY));
5661
+ if (!resolvedApiKey) {
5662
+ throw new errors.ConfigurationError("The application could not resolve the requested keys");
5663
+ }
5664
+ const client = new openai.OpenAI({
5665
+ apiKey: resolvedApiKey,
5666
+ baseURL: PROVIDER.XAI.BASE_URL,
5667
+ });
5668
+ logger.trace("Initialized xAI client");
5669
+ return client;
5670
+ }
5671
+
5672
+ class XaiProvider {
5673
+ constructor(model = PROVIDER.XAI.MODEL.DEFAULT, { apiKey } = {}) {
5674
+ this.log = getLogger$2();
5675
+ this.conversationHistory = [];
5676
+ this.model = model;
5677
+ this.apiKey = apiKey;
5678
+ }
5679
+ async getClient() {
5680
+ if (this._client) {
5681
+ return this._client;
5682
+ }
5683
+ this._client = await initializeClient({ apiKey: this.apiKey });
5684
+ return this._client;
5685
+ }
5686
+ async getOperateLoop() {
5687
+ if (this._operateLoop) {
5688
+ return this._operateLoop;
5689
+ }
5690
+ const client = await this.getClient();
5691
+ this._operateLoop = createOperateLoop({
5692
+ adapter: xaiAdapter,
5693
+ client,
5694
+ });
5695
+ return this._operateLoop;
5696
+ }
5697
+ async getStreamLoop() {
5698
+ if (this._streamLoop) {
5699
+ return this._streamLoop;
5700
+ }
5701
+ const client = await this.getClient();
5702
+ this._streamLoop = createStreamLoop({
5703
+ adapter: xaiAdapter,
5704
+ client,
5705
+ });
5706
+ return this._streamLoop;
5707
+ }
5708
+ async send(message, options) {
5709
+ const client = await this.getClient();
5710
+ const messages = prepareMessages$1(message, options || {});
5711
+ const modelToUse = options?.model || this.model;
5712
+ if (options?.response) {
5713
+ return createStructuredCompletion(client, {
5714
+ messages,
5715
+ responseSchema: options.response,
5716
+ model: modelToUse,
5717
+ });
5718
+ }
5719
+ return createTextCompletion(client, {
5720
+ messages,
5721
+ model: modelToUse,
5722
+ });
5723
+ }
5724
+ async operate(input, options = {}) {
5725
+ const operateLoop = await this.getOperateLoop();
5726
+ const mergedOptions = { ...options, model: options.model ?? this.model };
5727
+ // Create a merged history including both the tracked history and any explicitly provided history
5728
+ if (this.conversationHistory.length > 0) {
5729
+ // If options.history exists, merge with instance history, otherwise use instance history
5730
+ mergedOptions.history = options.history
5731
+ ? [...this.conversationHistory, ...options.history]
5732
+ : [...this.conversationHistory];
5733
+ }
5734
+ // Execute operate loop
5735
+ const response = await operateLoop.execute(input, mergedOptions);
5736
+ // Update conversation history with the new history from the response
5737
+ if (response.history && response.history.length > 0) {
5738
+ this.conversationHistory = response.history;
5739
+ }
5740
+ return response;
5741
+ }
5742
+ async *stream(input, options = {}) {
5743
+ const streamLoop = await this.getStreamLoop();
5744
+ const mergedOptions = { ...options, model: options.model ?? this.model };
5745
+ // Create a merged history including both the tracked history and any explicitly provided history
5746
+ if (this.conversationHistory.length > 0) {
5747
+ mergedOptions.history = options.history
5748
+ ? [...this.conversationHistory, ...options.history]
5749
+ : [...this.conversationHistory];
5750
+ }
5751
+ // Execute stream loop
5752
+ yield* streamLoop.execute(input, mergedOptions);
5753
+ }
5754
+ }
5755
+
5569
5756
  class Llm {
5570
5757
  constructor(providerName = DEFAULT.PROVIDER.NAME, options = {}) {
5571
5758
  const { fallback, model } = options;
@@ -5627,6 +5814,10 @@ class Llm {
5627
5814
  return new OpenRouterProvider(model || PROVIDER.OPENROUTER.MODEL.DEFAULT, {
5628
5815
  apiKey,
5629
5816
  });
5817
+ case PROVIDER.XAI.NAME:
5818
+ return new XaiProvider(model || PROVIDER.XAI.MODEL.DEFAULT, {
5819
+ apiKey,
5820
+ });
5630
5821
  default:
5631
5822
  throw new errors.ConfigurationError(`Unsupported provider: ${providerName}`);
5632
5823
  }
@@ -6071,6 +6262,7 @@ exports.LLM = constants;
6071
6262
  exports.Llm = Llm;
6072
6263
  exports.OpenRouterProvider = OpenRouterProvider;
6073
6264
  exports.Toolkit = Toolkit;
6265
+ exports.XaiProvider = XaiProvider;
6074
6266
  exports.extractReasoning = extractReasoning;
6075
6267
  exports.isLlmOperateInput = isLlmOperateInput;
6076
6268
  exports.isLlmOperateInputContent = isLlmOperateInputContent;