@jaypie/llm 1.2.15 → 1.2.16

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.
@@ -10,3 +10,4 @@ export { JaypieToolkit, toolkit, Toolkit, tools } from "./tools/index.js";
10
10
  export { extractReasoning } from "./util/extractReasoning.js";
11
11
  export { GeminiProvider } from "./providers/gemini/index.js";
12
12
  export { OpenRouterProvider } from "./providers/openrouter/index.js";
13
+ export { XaiProvider } from "./providers/xai/index.js";
@@ -0,0 +1,11 @@
1
+ import { OpenAiAdapter } from "./OpenAiAdapter.js";
2
+ /**
3
+ * XaiAdapter extends OpenAiAdapter since xAI (Grok) uses an OpenAI-compatible API.
4
+ * Only the name and default model are overridden; all request building, response parsing,
5
+ * error classification, tool handling, and streaming are inherited.
6
+ */
7
+ export declare class XaiAdapter extends OpenAiAdapter {
8
+ readonly name: "xai";
9
+ readonly defaultModel: "grok-4-1-fast-reasoning";
10
+ }
11
+ export declare const xaiAdapter: XaiAdapter;
@@ -4,3 +4,4 @@ export { AnthropicAdapter, anthropicAdapter } from "./AnthropicAdapter.js";
4
4
  export { GeminiAdapter, geminiAdapter } from "./GeminiAdapter.js";
5
5
  export { OpenAiAdapter, openAiAdapter } from "./OpenAiAdapter.js";
6
6
  export { OpenRouterAdapter, openRouterAdapter } from "./OpenRouterAdapter.js";
7
+ export { XaiAdapter, xaiAdapter } from "./XaiAdapter.js";
@@ -1,4 +1,4 @@
1
- export { AnthropicAdapter, anthropicAdapter, BaseProviderAdapter, GeminiAdapter, geminiAdapter, OpenAiAdapter, openAiAdapter, OpenRouterAdapter, openRouterAdapter, } from "./adapters/index.js";
1
+ export { AnthropicAdapter, anthropicAdapter, BaseProviderAdapter, GeminiAdapter, geminiAdapter, OpenAiAdapter, openAiAdapter, OpenRouterAdapter, openRouterAdapter, XaiAdapter, xaiAdapter, } from "./adapters/index.js";
2
2
  export type { ProviderAdapter } from "./adapters/index.js";
3
3
  export { createOperateLoop, OperateLoop } from "./OperateLoop.js";
4
4
  export type { OperateLoopConfig } from "./OperateLoop.js";
@@ -0,0 +1,21 @@
1
+ import { JsonObject } from "@jaypie/types";
2
+ import { LlmHistory, LlmInputMessage, LlmMessageOptions, LlmOperateOptions, LlmOperateResponse, LlmProvider } from "../../types/LlmProvider.interface.js";
3
+ import { LlmStreamChunk } from "../../types/LlmStreamChunk.interface.js";
4
+ export declare class XaiProvider implements LlmProvider {
5
+ private model;
6
+ private _client?;
7
+ private _operateLoop?;
8
+ private _streamLoop?;
9
+ private apiKey?;
10
+ private log;
11
+ private conversationHistory;
12
+ constructor(model?: string, { apiKey }?: {
13
+ apiKey?: string;
14
+ });
15
+ private getClient;
16
+ private getOperateLoop;
17
+ private getStreamLoop;
18
+ send(message: string, options?: LlmMessageOptions): Promise<string | JsonObject>;
19
+ operate(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
20
+ stream(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): AsyncIterable<LlmStreamChunk>;
21
+ }
@@ -0,0 +1 @@
1
+ export { XaiProvider } from "./XaiProvider.class.js";
@@ -0,0 +1,5 @@
1
+ import { OpenAI } from "openai";
2
+ export declare const getLogger: () => any;
3
+ export declare function initializeClient({ apiKey, }?: {
4
+ apiKey?: string;
5
+ }): Promise<OpenAI>;
@@ -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
  };
@@ -10,3 +10,4 @@ export { JaypieToolkit, toolkit, Toolkit, tools } from "./tools/index.js";
10
10
  export { extractReasoning } from "./util/extractReasoning.js";
11
11
  export { GeminiProvider } from "./providers/gemini/index.js";
12
12
  export { OpenRouterProvider } from "./providers/openrouter/index.js";
13
+ export { XaiProvider } from "./providers/xai/index.js";
package/dist/esm/index.js CHANGED
@@ -36,6 +36,12 @@ const FIRST_CLASS_PROVIDER = {
36
36
  SMALL: "z-ai/glm-4.7",
37
37
  TINY: "z-ai/glm-4.7",
38
38
  },
39
+ XAI: {
40
+ DEFAULT: "grok-4-1-fast-reasoning",
41
+ LARGE: "grok-4-1-fast-reasoning",
42
+ SMALL: "grok-3",
43
+ TINY: "grok-3-mini",
44
+ },
39
45
  };
40
46
  const PROVIDER = {
41
47
  ANTHROPIC: {
@@ -116,6 +122,19 @@ const PROVIDER = {
116
122
  USER: "user",
117
123
  },
118
124
  },
125
+ XAI: {
126
+ // https://docs.x.ai/docs/models
127
+ API_KEY: "XAI_API_KEY",
128
+ BASE_URL: "https://api.x.ai/v1",
129
+ MODEL: {
130
+ DEFAULT: FIRST_CLASS_PROVIDER.XAI.DEFAULT,
131
+ LARGE: FIRST_CLASS_PROVIDER.XAI.LARGE,
132
+ SMALL: FIRST_CLASS_PROVIDER.XAI.SMALL,
133
+ TINY: FIRST_CLASS_PROVIDER.XAI.TINY,
134
+ },
135
+ MODEL_MATCH_WORDS: ["grok", "xai"],
136
+ NAME: "xai",
137
+ },
119
138
  };
120
139
  // Last: Defaults
121
140
  const DEFAULT = {
@@ -133,6 +152,7 @@ const ALL = {
133
152
  PROVIDER.ANTHROPIC.MODEL.DEFAULT,
134
153
  PROVIDER.GEMINI.MODEL.DEFAULT,
135
154
  PROVIDER.OPENAI.MODEL.DEFAULT,
155
+ PROVIDER.XAI.MODEL.DEFAULT,
136
156
  ],
137
157
  COMBINED: [
138
158
  PROVIDER.ANTHROPIC.MODEL.DEFAULT,
@@ -147,21 +167,28 @@ const ALL = {
147
167
  PROVIDER.OPENAI.MODEL.LARGE,
148
168
  PROVIDER.OPENAI.MODEL.SMALL,
149
169
  PROVIDER.OPENAI.MODEL.TINY,
170
+ PROVIDER.XAI.MODEL.DEFAULT,
171
+ PROVIDER.XAI.MODEL.LARGE,
172
+ PROVIDER.XAI.MODEL.SMALL,
173
+ PROVIDER.XAI.MODEL.TINY,
150
174
  ],
151
175
  LARGE: [
152
176
  PROVIDER.ANTHROPIC.MODEL.LARGE,
153
177
  PROVIDER.GEMINI.MODEL.LARGE,
154
178
  PROVIDER.OPENAI.MODEL.LARGE,
179
+ PROVIDER.XAI.MODEL.LARGE,
155
180
  ],
156
181
  SMALL: [
157
182
  PROVIDER.ANTHROPIC.MODEL.SMALL,
158
183
  PROVIDER.GEMINI.MODEL.SMALL,
159
184
  PROVIDER.OPENAI.MODEL.SMALL,
185
+ PROVIDER.XAI.MODEL.SMALL,
160
186
  ],
161
187
  TINY: [
162
188
  PROVIDER.ANTHROPIC.MODEL.TINY,
163
189
  PROVIDER.GEMINI.MODEL.TINY,
164
190
  PROVIDER.OPENAI.MODEL.TINY,
191
+ PROVIDER.XAI.MODEL.TINY,
165
192
  ],
166
193
  };
167
194
 
@@ -212,6 +239,12 @@ function determineModelProvider(input) {
212
239
  provider: PROVIDER.OPENROUTER.NAME,
213
240
  };
214
241
  }
242
+ if (input === PROVIDER.XAI.NAME) {
243
+ return {
244
+ model: PROVIDER.XAI.MODEL.DEFAULT,
245
+ provider: PROVIDER.XAI.NAME,
246
+ };
247
+ }
215
248
  // Check if input matches an Anthropic model exactly
216
249
  for (const [, modelValue] of Object.entries(PROVIDER.ANTHROPIC.MODEL)) {
217
250
  if (input === modelValue) {
@@ -248,6 +281,15 @@ function determineModelProvider(input) {
248
281
  };
249
282
  }
250
283
  }
284
+ // Check if input matches an xAI model exactly
285
+ for (const [, modelValue] of Object.entries(PROVIDER.XAI.MODEL)) {
286
+ if (input === modelValue) {
287
+ return {
288
+ model: input,
289
+ provider: PROVIDER.XAI.NAME,
290
+ };
291
+ }
292
+ }
251
293
  // Assume OpenRouter for models containing "/" (e.g., "openai/gpt-4", "anthropic/claude-3-opus")
252
294
  // This check must come before match words so that "openai/gpt-4" is not matched by "openai" keyword
253
295
  if (input.includes("/")) {
@@ -294,6 +336,15 @@ function determineModelProvider(input) {
294
336
  }
295
337
  }
296
338
  }
339
+ // Check xAI match words
340
+ for (const matchWord of PROVIDER.XAI.MODEL_MATCH_WORDS) {
341
+ if (lowerInput.includes(matchWord)) {
342
+ return {
343
+ model: input,
344
+ provider: PROVIDER.XAI.NAME,
345
+ };
346
+ }
347
+ }
297
348
  // Check OpenRouter match words
298
349
  for (const matchWord of PROVIDER.OPENROUTER.MODEL_MATCH_WORDS) {
299
350
  if (lowerInput.includes(matchWord)) {
@@ -505,8 +556,8 @@ function formatOperateInput(input, options) {
505
556
  return [input];
506
557
  }
507
558
 
508
- const getLogger$4 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
509
- const log$1 = getLogger$4();
559
+ const getLogger$5 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
560
+ const log$1 = getLogger$5();
510
561
 
511
562
  // Turn policy constants
512
563
  const MAX_TURNS_ABSOLUTE_LIMIT = 72;
@@ -3147,6 +3198,23 @@ class OpenRouterAdapter extends BaseProviderAdapter {
3147
3198
  // Export singleton instance
3148
3199
  const openRouterAdapter = new OpenRouterAdapter();
3149
3200
 
3201
+ /**
3202
+ * XaiAdapter extends OpenAiAdapter since xAI (Grok) uses an OpenAI-compatible API.
3203
+ * Only the name and default model are overridden; all request building, response parsing,
3204
+ * error classification, tool handling, and streaming are inherited.
3205
+ */
3206
+ class XaiAdapter extends OpenAiAdapter {
3207
+ constructor() {
3208
+ super(...arguments);
3209
+ // @ts-expect-error Narrowing override: xAI name differs from parent's literal "openai"
3210
+ this.name = PROVIDER.XAI.NAME;
3211
+ // @ts-expect-error Narrowing override: xAI default model differs from parent's literal
3212
+ this.defaultModel = PROVIDER.XAI.MODEL.DEFAULT;
3213
+ }
3214
+ }
3215
+ // Export singleton instance
3216
+ const xaiAdapter = new XaiAdapter();
3217
+
3150
3218
  const DEFAULT_TOOL_TYPE = "function";
3151
3219
  const log = log$2.lib({ lib: JAYPIE.LIB.LLM });
3152
3220
  function logToolMessage(message, context) {
@@ -4865,10 +4933,10 @@ async function loadSdk$2() {
4865
4933
  }
4866
4934
  }
4867
4935
  // Logger
4868
- const getLogger$3 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
4936
+ const getLogger$4 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
4869
4937
  // Client initialization
4870
- async function initializeClient$3({ apiKey, } = {}) {
4871
- const logger = getLogger$3();
4938
+ async function initializeClient$4({ apiKey, } = {}) {
4939
+ const logger = getLogger$4();
4872
4940
  const resolvedApiKey = apiKey || (await getEnvSecret("ANTHROPIC_API_KEY"));
4873
4941
  if (!resolvedApiKey) {
4874
4942
  throw new ConfigurationError("The application could not resolve the required API key: ANTHROPIC_API_KEY");
@@ -4894,7 +4962,7 @@ function formatUserMessage$3(message, { data, placeholders: placeholders$1 } = {
4894
4962
  };
4895
4963
  }
4896
4964
  function prepareMessages$3(message, { data, placeholders } = {}) {
4897
- const logger = getLogger$3();
4965
+ const logger = getLogger$4();
4898
4966
  const messages = [];
4899
4967
  // Add user message (necessary for all requests)
4900
4968
  const userMessage = formatUserMessage$3(message, { data, placeholders });
@@ -4983,7 +5051,7 @@ async function createStructuredCompletion$1(client, messages, model, responseSch
4983
5051
  // Main class implementation
4984
5052
  class AnthropicProvider {
4985
5053
  constructor(model = PROVIDER.ANTHROPIC.MODEL.DEFAULT, { apiKey } = {}) {
4986
- this.log = getLogger$3();
5054
+ this.log = getLogger$4();
4987
5055
  this.conversationHistory = [];
4988
5056
  this.model = model;
4989
5057
  this.apiKey = apiKey;
@@ -4992,7 +5060,7 @@ class AnthropicProvider {
4992
5060
  if (this._client) {
4993
5061
  return this._client;
4994
5062
  }
4995
- this._client = await initializeClient$3({ apiKey: this.apiKey });
5063
+ this._client = await initializeClient$4({ apiKey: this.apiKey });
4996
5064
  return this._client;
4997
5065
  }
4998
5066
  async getOperateLoop() {
@@ -5081,10 +5149,10 @@ async function loadSdk$1() {
5081
5149
  }
5082
5150
  }
5083
5151
  // Logger
5084
- const getLogger$2 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
5152
+ const getLogger$3 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
5085
5153
  // Client initialization
5086
- async function initializeClient$2({ apiKey, } = {}) {
5087
- const logger = getLogger$2();
5154
+ async function initializeClient$3({ apiKey, } = {}) {
5155
+ const logger = getLogger$3();
5088
5156
  const resolvedApiKey = apiKey || (await getEnvSecret("GEMINI_API_KEY"));
5089
5157
  if (!resolvedApiKey) {
5090
5158
  throw new ConfigurationError("The application could not resolve the requested keys");
@@ -5104,7 +5172,7 @@ function formatUserMessage$2(message, { data, placeholders: placeholders$1 } = {
5104
5172
  };
5105
5173
  }
5106
5174
  function prepareMessages$2(message, { data, placeholders } = {}) {
5107
- const logger = getLogger$2();
5175
+ const logger = getLogger$3();
5108
5176
  const messages = [];
5109
5177
  let systemInstruction;
5110
5178
  // Note: Gemini handles system prompts differently via systemInstruction config
@@ -5118,7 +5186,7 @@ function prepareMessages$2(message, { data, placeholders } = {}) {
5118
5186
 
5119
5187
  class GeminiProvider {
5120
5188
  constructor(model = PROVIDER.GEMINI.MODEL.DEFAULT, { apiKey } = {}) {
5121
- this.log = getLogger$2();
5189
+ this.log = getLogger$3();
5122
5190
  this.conversationHistory = [];
5123
5191
  this.model = model;
5124
5192
  this.apiKey = apiKey;
@@ -5127,7 +5195,7 @@ class GeminiProvider {
5127
5195
  if (this._client) {
5128
5196
  return this._client;
5129
5197
  }
5130
- this._client = await initializeClient$2({ apiKey: this.apiKey });
5198
+ this._client = await initializeClient$3({ apiKey: this.apiKey });
5131
5199
  return this._client;
5132
5200
  }
5133
5201
  async getOperateLoop() {
@@ -5232,10 +5300,10 @@ class GeminiProvider {
5232
5300
  }
5233
5301
 
5234
5302
  // Logger
5235
- const getLogger$1 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
5303
+ const getLogger$2 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
5236
5304
  // Client initialization
5237
- async function initializeClient$1({ apiKey, } = {}) {
5238
- const logger = getLogger$1();
5305
+ async function initializeClient$2({ apiKey, } = {}) {
5306
+ const logger = getLogger$2();
5239
5307
  const resolvedApiKey = apiKey || (await getEnvSecret("OPENAI_API_KEY"));
5240
5308
  if (!resolvedApiKey) {
5241
5309
  throw new ConfigurationError("The application could not resolve the requested keys");
@@ -5263,7 +5331,7 @@ function formatUserMessage$1(message, { data, placeholders: placeholders$1 } = {
5263
5331
  };
5264
5332
  }
5265
5333
  function prepareMessages$1(message, { system, data, placeholders } = {}) {
5266
- const logger = getLogger$1();
5334
+ const logger = getLogger$2();
5267
5335
  const messages = [];
5268
5336
  if (system) {
5269
5337
  const systemMessage = formatSystemMessage$1(system, { data, placeholders });
@@ -5277,7 +5345,7 @@ function prepareMessages$1(message, { system, data, placeholders } = {}) {
5277
5345
  }
5278
5346
  // Completion requests
5279
5347
  async function createStructuredCompletion(client, { messages, responseSchema, model, }) {
5280
- const logger = getLogger$1();
5348
+ const logger = getLogger$2();
5281
5349
  logger.trace("Using structured output");
5282
5350
  const zodSchema = responseSchema instanceof z.ZodType
5283
5351
  ? responseSchema
@@ -5308,7 +5376,7 @@ async function createStructuredCompletion(client, { messages, responseSchema, mo
5308
5376
  return completion.choices[0].message.parsed;
5309
5377
  }
5310
5378
  async function createTextCompletion(client, { messages, model, }) {
5311
- const logger = getLogger$1();
5379
+ const logger = getLogger$2();
5312
5380
  logger.trace("Using text output (unstructured)");
5313
5381
  const completion = await client.chat.completions.create({
5314
5382
  messages,
@@ -5320,7 +5388,7 @@ async function createTextCompletion(client, { messages, model, }) {
5320
5388
 
5321
5389
  class OpenAiProvider {
5322
5390
  constructor(model = PROVIDER.OPENAI.MODEL.DEFAULT, { apiKey } = {}) {
5323
- this.log = getLogger$1();
5391
+ this.log = getLogger$2();
5324
5392
  this.conversationHistory = [];
5325
5393
  this.model = model;
5326
5394
  this.apiKey = apiKey;
@@ -5329,7 +5397,7 @@ class OpenAiProvider {
5329
5397
  if (this._client) {
5330
5398
  return this._client;
5331
5399
  }
5332
- this._client = await initializeClient$1({ apiKey: this.apiKey });
5400
+ this._client = await initializeClient$2({ apiKey: this.apiKey });
5333
5401
  return this._client;
5334
5402
  }
5335
5403
  async getOperateLoop() {
@@ -5416,10 +5484,10 @@ async function loadSdk() {
5416
5484
  }
5417
5485
  }
5418
5486
  // Logger
5419
- const getLogger = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
5487
+ const getLogger$1 = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
5420
5488
  // Client initialization
5421
- async function initializeClient({ apiKey, } = {}) {
5422
- const logger = getLogger();
5489
+ async function initializeClient$1({ apiKey, } = {}) {
5490
+ const logger = getLogger$1();
5423
5491
  const resolvedApiKey = apiKey || (await getEnvSecret("OPENROUTER_API_KEY"));
5424
5492
  if (!resolvedApiKey) {
5425
5493
  throw new ConfigurationError("The application could not resolve the requested keys");
@@ -5452,7 +5520,7 @@ function formatUserMessage(message, { data, placeholders: placeholders$1 } = {})
5452
5520
  };
5453
5521
  }
5454
5522
  function prepareMessages(message, { system, data, placeholders } = {}) {
5455
- const logger = getLogger();
5523
+ const logger = getLogger$1();
5456
5524
  const messages = [];
5457
5525
  if (system) {
5458
5526
  const systemMessage = formatSystemMessage(system, { data, placeholders });
@@ -5467,7 +5535,7 @@ function prepareMessages(message, { system, data, placeholders } = {}) {
5467
5535
 
5468
5536
  class OpenRouterProvider {
5469
5537
  constructor(model = getDefaultModel(), { apiKey } = {}) {
5470
- this.log = getLogger();
5538
+ this.log = getLogger$1();
5471
5539
  this.conversationHistory = [];
5472
5540
  this.model = model;
5473
5541
  this.apiKey = apiKey;
@@ -5476,7 +5544,7 @@ class OpenRouterProvider {
5476
5544
  if (this._client) {
5477
5545
  return this._client;
5478
5546
  }
5479
- this._client = await initializeClient({ apiKey: this.apiKey });
5547
+ this._client = await initializeClient$1({ apiKey: this.apiKey });
5480
5548
  return this._client;
5481
5549
  }
5482
5550
  async getOperateLoop() {
@@ -5564,6 +5632,107 @@ class OpenRouterProvider {
5564
5632
  }
5565
5633
  }
5566
5634
 
5635
+ // Logger
5636
+ const getLogger = () => log$2.lib({ lib: JAYPIE.LIB.LLM });
5637
+ // Client initialization
5638
+ async function initializeClient({ apiKey, } = {}) {
5639
+ const logger = getLogger();
5640
+ const resolvedApiKey = apiKey || (await getEnvSecret(PROVIDER.XAI.API_KEY));
5641
+ if (!resolvedApiKey) {
5642
+ throw new ConfigurationError("The application could not resolve the requested keys");
5643
+ }
5644
+ const client = new OpenAI({
5645
+ apiKey: resolvedApiKey,
5646
+ baseURL: PROVIDER.XAI.BASE_URL,
5647
+ });
5648
+ logger.trace("Initialized xAI client");
5649
+ return client;
5650
+ }
5651
+
5652
+ class XaiProvider {
5653
+ constructor(model = PROVIDER.XAI.MODEL.DEFAULT, { apiKey } = {}) {
5654
+ this.log = getLogger$2();
5655
+ this.conversationHistory = [];
5656
+ this.model = model;
5657
+ this.apiKey = apiKey;
5658
+ }
5659
+ async getClient() {
5660
+ if (this._client) {
5661
+ return this._client;
5662
+ }
5663
+ this._client = await initializeClient({ apiKey: this.apiKey });
5664
+ return this._client;
5665
+ }
5666
+ async getOperateLoop() {
5667
+ if (this._operateLoop) {
5668
+ return this._operateLoop;
5669
+ }
5670
+ const client = await this.getClient();
5671
+ this._operateLoop = createOperateLoop({
5672
+ adapter: xaiAdapter,
5673
+ client,
5674
+ });
5675
+ return this._operateLoop;
5676
+ }
5677
+ async getStreamLoop() {
5678
+ if (this._streamLoop) {
5679
+ return this._streamLoop;
5680
+ }
5681
+ const client = await this.getClient();
5682
+ this._streamLoop = createStreamLoop({
5683
+ adapter: xaiAdapter,
5684
+ client,
5685
+ });
5686
+ return this._streamLoop;
5687
+ }
5688
+ async send(message, options) {
5689
+ const client = await this.getClient();
5690
+ const messages = prepareMessages$1(message, options || {});
5691
+ const modelToUse = options?.model || this.model;
5692
+ if (options?.response) {
5693
+ return createStructuredCompletion(client, {
5694
+ messages,
5695
+ responseSchema: options.response,
5696
+ model: modelToUse,
5697
+ });
5698
+ }
5699
+ return createTextCompletion(client, {
5700
+ messages,
5701
+ model: modelToUse,
5702
+ });
5703
+ }
5704
+ async operate(input, options = {}) {
5705
+ const operateLoop = await this.getOperateLoop();
5706
+ const mergedOptions = { ...options, model: options.model ?? this.model };
5707
+ // Create a merged history including both the tracked history and any explicitly provided history
5708
+ if (this.conversationHistory.length > 0) {
5709
+ // If options.history exists, merge with instance history, otherwise use instance history
5710
+ mergedOptions.history = options.history
5711
+ ? [...this.conversationHistory, ...options.history]
5712
+ : [...this.conversationHistory];
5713
+ }
5714
+ // Execute operate loop
5715
+ const response = await operateLoop.execute(input, mergedOptions);
5716
+ // Update conversation history with the new history from the response
5717
+ if (response.history && response.history.length > 0) {
5718
+ this.conversationHistory = response.history;
5719
+ }
5720
+ return response;
5721
+ }
5722
+ async *stream(input, options = {}) {
5723
+ const streamLoop = await this.getStreamLoop();
5724
+ const mergedOptions = { ...options, model: options.model ?? this.model };
5725
+ // Create a merged history including both the tracked history and any explicitly provided history
5726
+ if (this.conversationHistory.length > 0) {
5727
+ mergedOptions.history = options.history
5728
+ ? [...this.conversationHistory, ...options.history]
5729
+ : [...this.conversationHistory];
5730
+ }
5731
+ // Execute stream loop
5732
+ yield* streamLoop.execute(input, mergedOptions);
5733
+ }
5734
+ }
5735
+
5567
5736
  class Llm {
5568
5737
  constructor(providerName = DEFAULT.PROVIDER.NAME, options = {}) {
5569
5738
  const { fallback, model } = options;
@@ -5625,6 +5794,10 @@ class Llm {
5625
5794
  return new OpenRouterProvider(model || PROVIDER.OPENROUTER.MODEL.DEFAULT, {
5626
5795
  apiKey,
5627
5796
  });
5797
+ case PROVIDER.XAI.NAME:
5798
+ return new XaiProvider(model || PROVIDER.XAI.MODEL.DEFAULT, {
5799
+ apiKey,
5800
+ });
5628
5801
  default:
5629
5802
  throw new ConfigurationError(`Unsupported provider: ${providerName}`);
5630
5803
  }
@@ -6063,5 +6236,5 @@ const toolkit = new JaypieToolkit(tools);
6063
6236
  [LlmMessageRole.Developer]: "user",
6064
6237
  });
6065
6238
 
6066
- export { GeminiProvider, JaypieToolkit, constants as LLM, Llm, LlmMessageRole, LlmMessageType, LlmStreamChunkType, OpenRouterProvider, Toolkit, extractReasoning, isLlmOperateInput, isLlmOperateInputContent, isLlmOperateInputFile, isLlmOperateInputImage, toolkit, tools };
6239
+ export { GeminiProvider, JaypieToolkit, constants as LLM, Llm, LlmMessageRole, LlmMessageType, LlmStreamChunkType, OpenRouterProvider, Toolkit, XaiProvider, extractReasoning, isLlmOperateInput, isLlmOperateInputContent, isLlmOperateInputFile, isLlmOperateInputImage, toolkit, tools };
6067
6240
  //# sourceMappingURL=index.js.map