@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.
- package/dist/cjs/constants.d.ts +18 -6
- package/dist/cjs/index.cjs +202 -28
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/operate/adapters/XaiAdapter.d.ts +11 -0
- package/dist/cjs/operate/adapters/index.d.ts +1 -0
- package/dist/cjs/operate/index.d.ts +1 -1
- package/dist/cjs/providers/xai/XaiProvider.class.d.ts +21 -0
- package/dist/cjs/providers/xai/index.d.ts +1 -0
- package/dist/cjs/providers/xai/utils.d.ts +5 -0
- package/dist/esm/constants.d.ts +18 -6
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +202 -29
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/operate/adapters/XaiAdapter.d.ts +11 -0
- package/dist/esm/operate/adapters/index.d.ts +1 -0
- package/dist/esm/operate/index.d.ts +1 -1
- package/dist/esm/providers/xai/XaiProvider.class.d.ts +21 -0
- package/dist/esm/providers/xai/index.d.ts +1 -0
- package/dist/esm/providers/xai/utils.d.ts +5 -0
- package/package.json +1 -1
package/dist/cjs/constants.d.ts
CHANGED
|
@@ -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
|
};
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -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$
|
|
511
|
-
const log$1 = getLogger$
|
|
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;
|
|
@@ -3149,6 +3200,23 @@ class OpenRouterAdapter extends BaseProviderAdapter {
|
|
|
3149
3200
|
// Export singleton instance
|
|
3150
3201
|
const openRouterAdapter = new OpenRouterAdapter();
|
|
3151
3202
|
|
|
3203
|
+
/**
|
|
3204
|
+
* XaiAdapter extends OpenAiAdapter since xAI (Grok) uses an OpenAI-compatible API.
|
|
3205
|
+
* Only the name and default model are overridden; all request building, response parsing,
|
|
3206
|
+
* error classification, tool handling, and streaming are inherited.
|
|
3207
|
+
*/
|
|
3208
|
+
class XaiAdapter extends OpenAiAdapter {
|
|
3209
|
+
constructor() {
|
|
3210
|
+
super(...arguments);
|
|
3211
|
+
// @ts-expect-error Narrowing override: xAI name differs from parent's literal "openai"
|
|
3212
|
+
this.name = PROVIDER.XAI.NAME;
|
|
3213
|
+
// @ts-expect-error Narrowing override: xAI default model differs from parent's literal
|
|
3214
|
+
this.defaultModel = PROVIDER.XAI.MODEL.DEFAULT;
|
|
3215
|
+
}
|
|
3216
|
+
}
|
|
3217
|
+
// Export singleton instance
|
|
3218
|
+
const xaiAdapter = new XaiAdapter();
|
|
3219
|
+
|
|
3152
3220
|
const DEFAULT_TOOL_TYPE = "function";
|
|
3153
3221
|
const log = log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
|
|
3154
3222
|
function logToolMessage(message, context) {
|
|
@@ -4867,10 +4935,10 @@ async function loadSdk$2() {
|
|
|
4867
4935
|
}
|
|
4868
4936
|
}
|
|
4869
4937
|
// Logger
|
|
4870
|
-
const getLogger$
|
|
4938
|
+
const getLogger$4 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
|
|
4871
4939
|
// Client initialization
|
|
4872
|
-
async function initializeClient$
|
|
4873
|
-
const logger = getLogger$
|
|
4940
|
+
async function initializeClient$4({ apiKey, } = {}) {
|
|
4941
|
+
const logger = getLogger$4();
|
|
4874
4942
|
const resolvedApiKey = apiKey || (await aws.getEnvSecret("ANTHROPIC_API_KEY"));
|
|
4875
4943
|
if (!resolvedApiKey) {
|
|
4876
4944
|
throw new errors.ConfigurationError("The application could not resolve the required API key: ANTHROPIC_API_KEY");
|
|
@@ -4896,7 +4964,7 @@ function formatUserMessage$3(message, { data, placeholders } = {}) {
|
|
|
4896
4964
|
};
|
|
4897
4965
|
}
|
|
4898
4966
|
function prepareMessages$3(message, { data, placeholders } = {}) {
|
|
4899
|
-
const logger = getLogger$
|
|
4967
|
+
const logger = getLogger$4();
|
|
4900
4968
|
const messages = [];
|
|
4901
4969
|
// Add user message (necessary for all requests)
|
|
4902
4970
|
const userMessage = formatUserMessage$3(message, { data, placeholders });
|
|
@@ -4985,7 +5053,7 @@ async function createStructuredCompletion$1(client, messages, model, responseSch
|
|
|
4985
5053
|
// Main class implementation
|
|
4986
5054
|
class AnthropicProvider {
|
|
4987
5055
|
constructor(model = PROVIDER.ANTHROPIC.MODEL.DEFAULT, { apiKey } = {}) {
|
|
4988
|
-
this.log = getLogger$
|
|
5056
|
+
this.log = getLogger$4();
|
|
4989
5057
|
this.conversationHistory = [];
|
|
4990
5058
|
this.model = model;
|
|
4991
5059
|
this.apiKey = apiKey;
|
|
@@ -4994,7 +5062,7 @@ class AnthropicProvider {
|
|
|
4994
5062
|
if (this._client) {
|
|
4995
5063
|
return this._client;
|
|
4996
5064
|
}
|
|
4997
|
-
this._client = await initializeClient$
|
|
5065
|
+
this._client = await initializeClient$4({ apiKey: this.apiKey });
|
|
4998
5066
|
return this._client;
|
|
4999
5067
|
}
|
|
5000
5068
|
async getOperateLoop() {
|
|
@@ -5083,10 +5151,10 @@ async function loadSdk$1() {
|
|
|
5083
5151
|
}
|
|
5084
5152
|
}
|
|
5085
5153
|
// Logger
|
|
5086
|
-
const getLogger$
|
|
5154
|
+
const getLogger$3 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
|
|
5087
5155
|
// Client initialization
|
|
5088
|
-
async function initializeClient$
|
|
5089
|
-
const logger = getLogger$
|
|
5156
|
+
async function initializeClient$3({ apiKey, } = {}) {
|
|
5157
|
+
const logger = getLogger$3();
|
|
5090
5158
|
const resolvedApiKey = apiKey || (await aws.getEnvSecret("GEMINI_API_KEY"));
|
|
5091
5159
|
if (!resolvedApiKey) {
|
|
5092
5160
|
throw new errors.ConfigurationError("The application could not resolve the requested keys");
|
|
@@ -5106,7 +5174,7 @@ function formatUserMessage$2(message, { data, placeholders } = {}) {
|
|
|
5106
5174
|
};
|
|
5107
5175
|
}
|
|
5108
5176
|
function prepareMessages$2(message, { data, placeholders } = {}) {
|
|
5109
|
-
const logger = getLogger$
|
|
5177
|
+
const logger = getLogger$3();
|
|
5110
5178
|
const messages = [];
|
|
5111
5179
|
let systemInstruction;
|
|
5112
5180
|
// Note: Gemini handles system prompts differently via systemInstruction config
|
|
@@ -5120,7 +5188,7 @@ function prepareMessages$2(message, { data, placeholders } = {}) {
|
|
|
5120
5188
|
|
|
5121
5189
|
class GeminiProvider {
|
|
5122
5190
|
constructor(model = PROVIDER.GEMINI.MODEL.DEFAULT, { apiKey } = {}) {
|
|
5123
|
-
this.log = getLogger$
|
|
5191
|
+
this.log = getLogger$3();
|
|
5124
5192
|
this.conversationHistory = [];
|
|
5125
5193
|
this.model = model;
|
|
5126
5194
|
this.apiKey = apiKey;
|
|
@@ -5129,7 +5197,7 @@ class GeminiProvider {
|
|
|
5129
5197
|
if (this._client) {
|
|
5130
5198
|
return this._client;
|
|
5131
5199
|
}
|
|
5132
|
-
this._client = await initializeClient$
|
|
5200
|
+
this._client = await initializeClient$3({ apiKey: this.apiKey });
|
|
5133
5201
|
return this._client;
|
|
5134
5202
|
}
|
|
5135
5203
|
async getOperateLoop() {
|
|
@@ -5234,10 +5302,10 @@ class GeminiProvider {
|
|
|
5234
5302
|
}
|
|
5235
5303
|
|
|
5236
5304
|
// Logger
|
|
5237
|
-
const getLogger$
|
|
5305
|
+
const getLogger$2 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
|
|
5238
5306
|
// Client initialization
|
|
5239
|
-
async function initializeClient$
|
|
5240
|
-
const logger = getLogger$
|
|
5307
|
+
async function initializeClient$2({ apiKey, } = {}) {
|
|
5308
|
+
const logger = getLogger$2();
|
|
5241
5309
|
const resolvedApiKey = apiKey || (await aws.getEnvSecret("OPENAI_API_KEY"));
|
|
5242
5310
|
if (!resolvedApiKey) {
|
|
5243
5311
|
throw new errors.ConfigurationError("The application could not resolve the requested keys");
|
|
@@ -5265,7 +5333,7 @@ function formatUserMessage$1(message, { data, placeholders } = {}) {
|
|
|
5265
5333
|
};
|
|
5266
5334
|
}
|
|
5267
5335
|
function prepareMessages$1(message, { system, data, placeholders } = {}) {
|
|
5268
|
-
const logger = getLogger$
|
|
5336
|
+
const logger = getLogger$2();
|
|
5269
5337
|
const messages = [];
|
|
5270
5338
|
if (system) {
|
|
5271
5339
|
const systemMessage = formatSystemMessage$1(system, { data, placeholders });
|
|
@@ -5279,7 +5347,7 @@ function prepareMessages$1(message, { system, data, placeholders } = {}) {
|
|
|
5279
5347
|
}
|
|
5280
5348
|
// Completion requests
|
|
5281
5349
|
async function createStructuredCompletion(client, { messages, responseSchema, model, }) {
|
|
5282
|
-
const logger = getLogger$
|
|
5350
|
+
const logger = getLogger$2();
|
|
5283
5351
|
logger.trace("Using structured output");
|
|
5284
5352
|
const zodSchema = responseSchema instanceof v4.z.ZodType
|
|
5285
5353
|
? responseSchema
|
|
@@ -5310,7 +5378,7 @@ async function createStructuredCompletion(client, { messages, responseSchema, mo
|
|
|
5310
5378
|
return completion.choices[0].message.parsed;
|
|
5311
5379
|
}
|
|
5312
5380
|
async function createTextCompletion(client, { messages, model, }) {
|
|
5313
|
-
const logger = getLogger$
|
|
5381
|
+
const logger = getLogger$2();
|
|
5314
5382
|
logger.trace("Using text output (unstructured)");
|
|
5315
5383
|
const completion = await client.chat.completions.create({
|
|
5316
5384
|
messages,
|
|
@@ -5322,7 +5390,7 @@ async function createTextCompletion(client, { messages, model, }) {
|
|
|
5322
5390
|
|
|
5323
5391
|
class OpenAiProvider {
|
|
5324
5392
|
constructor(model = PROVIDER.OPENAI.MODEL.DEFAULT, { apiKey } = {}) {
|
|
5325
|
-
this.log = getLogger$
|
|
5393
|
+
this.log = getLogger$2();
|
|
5326
5394
|
this.conversationHistory = [];
|
|
5327
5395
|
this.model = model;
|
|
5328
5396
|
this.apiKey = apiKey;
|
|
@@ -5331,7 +5399,7 @@ class OpenAiProvider {
|
|
|
5331
5399
|
if (this._client) {
|
|
5332
5400
|
return this._client;
|
|
5333
5401
|
}
|
|
5334
|
-
this._client = await initializeClient$
|
|
5402
|
+
this._client = await initializeClient$2({ apiKey: this.apiKey });
|
|
5335
5403
|
return this._client;
|
|
5336
5404
|
}
|
|
5337
5405
|
async getOperateLoop() {
|
|
@@ -5418,10 +5486,10 @@ async function loadSdk() {
|
|
|
5418
5486
|
}
|
|
5419
5487
|
}
|
|
5420
5488
|
// Logger
|
|
5421
|
-
const getLogger = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
|
|
5489
|
+
const getLogger$1 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
|
|
5422
5490
|
// Client initialization
|
|
5423
|
-
async function initializeClient({ apiKey, } = {}) {
|
|
5424
|
-
const logger = getLogger();
|
|
5491
|
+
async function initializeClient$1({ apiKey, } = {}) {
|
|
5492
|
+
const logger = getLogger$1();
|
|
5425
5493
|
const resolvedApiKey = apiKey || (await aws.getEnvSecret("OPENROUTER_API_KEY"));
|
|
5426
5494
|
if (!resolvedApiKey) {
|
|
5427
5495
|
throw new errors.ConfigurationError("The application could not resolve the requested keys");
|
|
@@ -5454,7 +5522,7 @@ function formatUserMessage(message, { data, placeholders } = {}) {
|
|
|
5454
5522
|
};
|
|
5455
5523
|
}
|
|
5456
5524
|
function prepareMessages(message, { system, data, placeholders } = {}) {
|
|
5457
|
-
const logger = getLogger();
|
|
5525
|
+
const logger = getLogger$1();
|
|
5458
5526
|
const messages = [];
|
|
5459
5527
|
if (system) {
|
|
5460
5528
|
const systemMessage = formatSystemMessage(system, { data, placeholders });
|
|
@@ -5469,7 +5537,7 @@ function prepareMessages(message, { system, data, placeholders } = {}) {
|
|
|
5469
5537
|
|
|
5470
5538
|
class OpenRouterProvider {
|
|
5471
5539
|
constructor(model = getDefaultModel(), { apiKey } = {}) {
|
|
5472
|
-
this.log = getLogger();
|
|
5540
|
+
this.log = getLogger$1();
|
|
5473
5541
|
this.conversationHistory = [];
|
|
5474
5542
|
this.model = model;
|
|
5475
5543
|
this.apiKey = apiKey;
|
|
@@ -5478,7 +5546,7 @@ class OpenRouterProvider {
|
|
|
5478
5546
|
if (this._client) {
|
|
5479
5547
|
return this._client;
|
|
5480
5548
|
}
|
|
5481
|
-
this._client = await initializeClient({ apiKey: this.apiKey });
|
|
5549
|
+
this._client = await initializeClient$1({ apiKey: this.apiKey });
|
|
5482
5550
|
return this._client;
|
|
5483
5551
|
}
|
|
5484
5552
|
async getOperateLoop() {
|
|
@@ -5566,6 +5634,107 @@ class OpenRouterProvider {
|
|
|
5566
5634
|
}
|
|
5567
5635
|
}
|
|
5568
5636
|
|
|
5637
|
+
// Logger
|
|
5638
|
+
const getLogger = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
|
|
5639
|
+
// Client initialization
|
|
5640
|
+
async function initializeClient({ apiKey, } = {}) {
|
|
5641
|
+
const logger = getLogger();
|
|
5642
|
+
const resolvedApiKey = apiKey || (await aws.getEnvSecret(PROVIDER.XAI.API_KEY));
|
|
5643
|
+
if (!resolvedApiKey) {
|
|
5644
|
+
throw new errors.ConfigurationError("The application could not resolve the requested keys");
|
|
5645
|
+
}
|
|
5646
|
+
const client = new openai.OpenAI({
|
|
5647
|
+
apiKey: resolvedApiKey,
|
|
5648
|
+
baseURL: PROVIDER.XAI.BASE_URL,
|
|
5649
|
+
});
|
|
5650
|
+
logger.trace("Initialized xAI client");
|
|
5651
|
+
return client;
|
|
5652
|
+
}
|
|
5653
|
+
|
|
5654
|
+
class XaiProvider {
|
|
5655
|
+
constructor(model = PROVIDER.XAI.MODEL.DEFAULT, { apiKey } = {}) {
|
|
5656
|
+
this.log = getLogger$2();
|
|
5657
|
+
this.conversationHistory = [];
|
|
5658
|
+
this.model = model;
|
|
5659
|
+
this.apiKey = apiKey;
|
|
5660
|
+
}
|
|
5661
|
+
async getClient() {
|
|
5662
|
+
if (this._client) {
|
|
5663
|
+
return this._client;
|
|
5664
|
+
}
|
|
5665
|
+
this._client = await initializeClient({ apiKey: this.apiKey });
|
|
5666
|
+
return this._client;
|
|
5667
|
+
}
|
|
5668
|
+
async getOperateLoop() {
|
|
5669
|
+
if (this._operateLoop) {
|
|
5670
|
+
return this._operateLoop;
|
|
5671
|
+
}
|
|
5672
|
+
const client = await this.getClient();
|
|
5673
|
+
this._operateLoop = createOperateLoop({
|
|
5674
|
+
adapter: xaiAdapter,
|
|
5675
|
+
client,
|
|
5676
|
+
});
|
|
5677
|
+
return this._operateLoop;
|
|
5678
|
+
}
|
|
5679
|
+
async getStreamLoop() {
|
|
5680
|
+
if (this._streamLoop) {
|
|
5681
|
+
return this._streamLoop;
|
|
5682
|
+
}
|
|
5683
|
+
const client = await this.getClient();
|
|
5684
|
+
this._streamLoop = createStreamLoop({
|
|
5685
|
+
adapter: xaiAdapter,
|
|
5686
|
+
client,
|
|
5687
|
+
});
|
|
5688
|
+
return this._streamLoop;
|
|
5689
|
+
}
|
|
5690
|
+
async send(message, options) {
|
|
5691
|
+
const client = await this.getClient();
|
|
5692
|
+
const messages = prepareMessages$1(message, options || {});
|
|
5693
|
+
const modelToUse = options?.model || this.model;
|
|
5694
|
+
if (options?.response) {
|
|
5695
|
+
return createStructuredCompletion(client, {
|
|
5696
|
+
messages,
|
|
5697
|
+
responseSchema: options.response,
|
|
5698
|
+
model: modelToUse,
|
|
5699
|
+
});
|
|
5700
|
+
}
|
|
5701
|
+
return createTextCompletion(client, {
|
|
5702
|
+
messages,
|
|
5703
|
+
model: modelToUse,
|
|
5704
|
+
});
|
|
5705
|
+
}
|
|
5706
|
+
async operate(input, options = {}) {
|
|
5707
|
+
const operateLoop = await this.getOperateLoop();
|
|
5708
|
+
const mergedOptions = { ...options, model: options.model ?? this.model };
|
|
5709
|
+
// Create a merged history including both the tracked history and any explicitly provided history
|
|
5710
|
+
if (this.conversationHistory.length > 0) {
|
|
5711
|
+
// If options.history exists, merge with instance history, otherwise use instance history
|
|
5712
|
+
mergedOptions.history = options.history
|
|
5713
|
+
? [...this.conversationHistory, ...options.history]
|
|
5714
|
+
: [...this.conversationHistory];
|
|
5715
|
+
}
|
|
5716
|
+
// Execute operate loop
|
|
5717
|
+
const response = await operateLoop.execute(input, mergedOptions);
|
|
5718
|
+
// Update conversation history with the new history from the response
|
|
5719
|
+
if (response.history && response.history.length > 0) {
|
|
5720
|
+
this.conversationHistory = response.history;
|
|
5721
|
+
}
|
|
5722
|
+
return response;
|
|
5723
|
+
}
|
|
5724
|
+
async *stream(input, options = {}) {
|
|
5725
|
+
const streamLoop = await this.getStreamLoop();
|
|
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
|
+
mergedOptions.history = options.history
|
|
5730
|
+
? [...this.conversationHistory, ...options.history]
|
|
5731
|
+
: [...this.conversationHistory];
|
|
5732
|
+
}
|
|
5733
|
+
// Execute stream loop
|
|
5734
|
+
yield* streamLoop.execute(input, mergedOptions);
|
|
5735
|
+
}
|
|
5736
|
+
}
|
|
5737
|
+
|
|
5569
5738
|
class Llm {
|
|
5570
5739
|
constructor(providerName = DEFAULT.PROVIDER.NAME, options = {}) {
|
|
5571
5740
|
const { fallback, model } = options;
|
|
@@ -5627,6 +5796,10 @@ class Llm {
|
|
|
5627
5796
|
return new OpenRouterProvider(model || PROVIDER.OPENROUTER.MODEL.DEFAULT, {
|
|
5628
5797
|
apiKey,
|
|
5629
5798
|
});
|
|
5799
|
+
case PROVIDER.XAI.NAME:
|
|
5800
|
+
return new XaiProvider(model || PROVIDER.XAI.MODEL.DEFAULT, {
|
|
5801
|
+
apiKey,
|
|
5802
|
+
});
|
|
5630
5803
|
default:
|
|
5631
5804
|
throw new errors.ConfigurationError(`Unsupported provider: ${providerName}`);
|
|
5632
5805
|
}
|
|
@@ -6071,6 +6244,7 @@ exports.LLM = constants;
|
|
|
6071
6244
|
exports.Llm = Llm;
|
|
6072
6245
|
exports.OpenRouterProvider = OpenRouterProvider;
|
|
6073
6246
|
exports.Toolkit = Toolkit;
|
|
6247
|
+
exports.XaiProvider = XaiProvider;
|
|
6074
6248
|
exports.extractReasoning = extractReasoning;
|
|
6075
6249
|
exports.isLlmOperateInput = isLlmOperateInput;
|
|
6076
6250
|
exports.isLlmOperateInputContent = isLlmOperateInputContent;
|