@stackfactor/agent-utils 1.0.12 → 1.0.14

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.
@@ -1 +1 @@
1
- {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAkSQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCAufF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCA5YO,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDAisBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CAjwB8B,GAAG,KAAG,MAAM;;AA4zBzD,wBAOE"}
1
+ {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAmVQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCAmfF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCAxYO,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDA6rBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CA7vB8B,GAAG,KAAG,MAAM;;AAwzBzD,wBAOE"}
@@ -27,34 +27,36 @@ Do NOT include raw newlines, tabs, or unescaped quotes inside JSON string values
27
27
  * Each failing field is described with its dot-notation path and a contextual message
28
28
  * that depends on the Zod error code (e.g. `invalid_type`, `too_small`, `invalid_enum_value`).
29
29
  * The output is prefixed with `"Schema validation failed:"` followed by a bulleted list.
30
- * @param zodError - The Zod `ZodError` instance whose `errors` array will be formatted
30
+ * @param zodError - The Zod `ZodError` instance whose `issues` array will be formatted
31
31
  * @returns A formatted string describing all validation failures
32
32
  */
33
33
  const formatZodErrors = (zodError) => {
34
- const errors = zodError.errors.map((err) => {
35
- const path = err.path.length > 0 ? err.path.join(".") : "root";
36
- // Provide more context based on error type
37
- switch (err.code) {
38
- case "invalid_type":
39
- return `Field "${path}": Expected ${err.expected}, but received ${err.received}`;
40
- case "invalid_literal":
41
- return `Field "${path}": Expected literal value ${JSON.stringify(err.expected)}, but received ${JSON.stringify(err.received)}`;
42
- case "unrecognized_keys":
43
- return `Field "${path}": Unrecognized keys: ${err.keys.join(", ")}`;
44
- case "invalid_union":
45
- return `Field "${path}": Invalid union - none of the expected types matched`;
46
- case "invalid_enum_value":
47
- return `Field "${path}": Invalid enum value. Expected one of: ${err.options.join(", ")}`;
48
- case "invalid_string":
49
- return `Field "${path}": Invalid string format (${err.validation})`;
50
- case "too_small":
51
- return `Field "${path}": Value is too small (minimum: ${err.minimum})`;
52
- case "too_big":
53
- return `Field "${path}": Value is too large (maximum: ${err.maximum})`;
54
- default:
55
- return `Field "${path}": ${err.message}`;
56
- }
57
- });
34
+ const errors = zodError.issues
35
+ ? zodError.issues.map((err) => {
36
+ const path = err.path.length > 0 ? err.path.join(".") : "root";
37
+ // Provide more context based on error type
38
+ switch (err.code) {
39
+ case "invalid_type":
40
+ return `Field "${path}": Expected ${err.expected}, but received ${err.received}`;
41
+ case "invalid_literal":
42
+ return `Field "${path}": Expected literal value ${JSON.stringify(err.expected)}, but received ${JSON.stringify(err.received)}`;
43
+ case "unrecognized_keys":
44
+ return `Field "${path}": Unrecognized keys: ${err.keys.join(", ")}`;
45
+ case "invalid_union":
46
+ return `Field "${path}": Invalid union - none of the expected types matched`;
47
+ case "invalid_enum_value":
48
+ return `Field "${path}": Invalid enum value. Expected one of: ${err.options.join(", ")}`;
49
+ case "invalid_string":
50
+ return `Field "${path}": Invalid string format (${err.validation})`;
51
+ case "too_small":
52
+ return `Field "${path}": Value is too small (minimum: ${err.minimum})`;
53
+ case "too_big":
54
+ return `Field "${path}": Value is too large (maximum: ${err.maximum})`;
55
+ default:
56
+ return `Field "${path}": ${err.message}`;
57
+ }
58
+ })
59
+ : [];
58
60
  return `Schema validation failed:\n - ${errors.join("\n - ")}`;
59
61
  };
60
62
  /**
@@ -189,17 +191,50 @@ const extractJSONFromResponse = (text) => {
189
191
  }
190
192
  return null;
191
193
  };
194
+ /**
195
+ * Maps a model name prefix to its OpenAI-compatible provider configuration. DeepSeek,
196
+ * Kimi (Moonshot), and GLM (Zhipu) all expose OpenAI-compatible chat completion APIs
197
+ * and can be routed through `ChatOpenAI` with a custom base URL and API key.
198
+ * @param modelName - The model identifier to inspect
199
+ * @param config - Configuration object containing provider-specific API keys
200
+ * @returns An object with `apiKey` and `baseURL` when the model matches a known
201
+ * OpenAI-compatible provider, or `null` otherwise
202
+ */
203
+ const getOpenAICompatibleProvider = (modelName, config) => {
204
+ if (modelName.startsWith("deepseek-")) {
205
+ return {
206
+ apiKey: config.deepSeekAPIKey,
207
+ baseURL: "https://api.deepseek.com/v1",
208
+ };
209
+ }
210
+ if (modelName.startsWith("kimi-") || modelName.startsWith("moonshot-")) {
211
+ return {
212
+ apiKey: config.kimiAPIKey,
213
+ baseURL: "https://api.moonshot.ai/v1",
214
+ };
215
+ }
216
+ if (modelName.startsWith("glm-")) {
217
+ return {
218
+ apiKey: config.glmAPIKey,
219
+ baseURL: "https://open.bigmodel.cn/api/paas/v4/",
220
+ };
221
+ }
222
+ return null;
223
+ };
192
224
  /**
193
225
  * Instantiates and returns the appropriate LangChain chat model based on the model
194
226
  * name prefix. `claude-` maps to `ChatAnthropic`, `gemini-` maps to
195
- * `ChatGoogleGenerativeAI`, and `gpt-` maps to `ChatOpenAI`. When a Zod `schema` is
196
- * provided for a GPT model, native `response_format` with `json_schema` is configured
197
- * on the OpenAI instance for structured output. Throws a `BAD_REQUEST` error for
198
- * unrecognised model names.
227
+ * `ChatGoogleGenerativeAI`, and `gpt-` maps to `ChatOpenAI`. DeepSeek (`deepseek-`),
228
+ * Kimi/Moonshot (`kimi-`, `moonshot-`), and GLM/Zhipu (`glm-`) models are routed
229
+ * through `ChatOpenAI` against each provider's OpenAI-compatible endpoint. When a Zod
230
+ * `schema` is provided for a GPT model, native `response_format` with `json_schema` is
231
+ * configured for structured output. Throws a `BAD_REQUEST` error for unrecognised
232
+ * model names.
199
233
  * @param modelName - The model identifier, e.g. `"gpt-4o"`, `"claude-3-5-sonnet"`,
200
- * `"gemini-1.5-pro"`
234
+ * `"gemini-1.5-pro"`, `"deepseek-chat"`, `"kimi-k2-0905-preview"`, `"glm-4.6"`
201
235
  * @param config - Configuration object containing API keys (`openAIAPIKey`,
202
- * `anthropicAPIKey`, `googleAPIKey`), optional `maxTokens`, and optional `temperature`
236
+ * `anthropicAPIKey`, `googleAPIKey`, `deepSeekAPIKey`, `kimiAPIKey`, `glmAPIKey`),
237
+ * optional `maxTokens`, and optional `temperature`
203
238
  * @param schema - Optional Zod schema used to configure structured JSON output for
204
239
  * OpenAI GPT models via `response_format`; ignored for other providers
205
240
  * @returns A configured LangChain chat model instance
@@ -250,6 +285,17 @@ const getLLMModel = (modelName, config, schema = null) => {
250
285
  }
251
286
  return new openai_1.ChatOpenAI(openAISettings);
252
287
  }
288
+ // OpenAI-compatible providers: DeepSeek, Kimi (Moonshot), GLM (Zhipu)
289
+ const openAICompatible = getOpenAICompatibleProvider(modelName, config);
290
+ if (openAICompatible) {
291
+ return new openai_1.ChatOpenAI({
292
+ apiKey: openAICompatible.apiKey,
293
+ maxTokens: config.maxTokens || 200000,
294
+ modelName: modelName,
295
+ configuration: { baseURL: openAICompatible.baseURL },
296
+ ...modelSettings,
297
+ });
298
+ }
253
299
  throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.BAD_REQUEST, const_js_1.default.ERROR.UNSUPPORTED_MODEL + ": " + modelName);
254
300
  };
255
301
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAkSQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCAufF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCA5YO,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDAisBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CAjwB8B,GAAG,KAAG,MAAM;;AA4zBzD,wBAOE"}
1
+ {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAmVQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCAmfF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCAxYO,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDA6rBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CA7vB8B,GAAG,KAAG,MAAM;;AAwzBzD,wBAOE"}
@@ -22,34 +22,36 @@ Do NOT include raw newlines, tabs, or unescaped quotes inside JSON string values
22
22
  * Each failing field is described with its dot-notation path and a contextual message
23
23
  * that depends on the Zod error code (e.g. `invalid_type`, `too_small`, `invalid_enum_value`).
24
24
  * The output is prefixed with `"Schema validation failed:"` followed by a bulleted list.
25
- * @param zodError - The Zod `ZodError` instance whose `errors` array will be formatted
25
+ * @param zodError - The Zod `ZodError` instance whose `issues` array will be formatted
26
26
  * @returns A formatted string describing all validation failures
27
27
  */
28
28
  const formatZodErrors = (zodError) => {
29
- const errors = zodError.errors.map((err) => {
30
- const path = err.path.length > 0 ? err.path.join(".") : "root";
31
- // Provide more context based on error type
32
- switch (err.code) {
33
- case "invalid_type":
34
- return `Field "${path}": Expected ${err.expected}, but received ${err.received}`;
35
- case "invalid_literal":
36
- return `Field "${path}": Expected literal value ${JSON.stringify(err.expected)}, but received ${JSON.stringify(err.received)}`;
37
- case "unrecognized_keys":
38
- return `Field "${path}": Unrecognized keys: ${err.keys.join(", ")}`;
39
- case "invalid_union":
40
- return `Field "${path}": Invalid union - none of the expected types matched`;
41
- case "invalid_enum_value":
42
- return `Field "${path}": Invalid enum value. Expected one of: ${err.options.join(", ")}`;
43
- case "invalid_string":
44
- return `Field "${path}": Invalid string format (${err.validation})`;
45
- case "too_small":
46
- return `Field "${path}": Value is too small (minimum: ${err.minimum})`;
47
- case "too_big":
48
- return `Field "${path}": Value is too large (maximum: ${err.maximum})`;
49
- default:
50
- return `Field "${path}": ${err.message}`;
51
- }
52
- });
29
+ const errors = zodError.issues
30
+ ? zodError.issues.map((err) => {
31
+ const path = err.path.length > 0 ? err.path.join(".") : "root";
32
+ // Provide more context based on error type
33
+ switch (err.code) {
34
+ case "invalid_type":
35
+ return `Field "${path}": Expected ${err.expected}, but received ${err.received}`;
36
+ case "invalid_literal":
37
+ return `Field "${path}": Expected literal value ${JSON.stringify(err.expected)}, but received ${JSON.stringify(err.received)}`;
38
+ case "unrecognized_keys":
39
+ return `Field "${path}": Unrecognized keys: ${err.keys.join(", ")}`;
40
+ case "invalid_union":
41
+ return `Field "${path}": Invalid union - none of the expected types matched`;
42
+ case "invalid_enum_value":
43
+ return `Field "${path}": Invalid enum value. Expected one of: ${err.options.join(", ")}`;
44
+ case "invalid_string":
45
+ return `Field "${path}": Invalid string format (${err.validation})`;
46
+ case "too_small":
47
+ return `Field "${path}": Value is too small (minimum: ${err.minimum})`;
48
+ case "too_big":
49
+ return `Field "${path}": Value is too large (maximum: ${err.maximum})`;
50
+ default:
51
+ return `Field "${path}": ${err.message}`;
52
+ }
53
+ })
54
+ : [];
53
55
  return `Schema validation failed:\n - ${errors.join("\n - ")}`;
54
56
  };
55
57
  /**
@@ -184,17 +186,50 @@ const extractJSONFromResponse = (text) => {
184
186
  }
185
187
  return null;
186
188
  };
189
+ /**
190
+ * Maps a model name prefix to its OpenAI-compatible provider configuration. DeepSeek,
191
+ * Kimi (Moonshot), and GLM (Zhipu) all expose OpenAI-compatible chat completion APIs
192
+ * and can be routed through `ChatOpenAI` with a custom base URL and API key.
193
+ * @param modelName - The model identifier to inspect
194
+ * @param config - Configuration object containing provider-specific API keys
195
+ * @returns An object with `apiKey` and `baseURL` when the model matches a known
196
+ * OpenAI-compatible provider, or `null` otherwise
197
+ */
198
+ const getOpenAICompatibleProvider = (modelName, config) => {
199
+ if (modelName.startsWith("deepseek-")) {
200
+ return {
201
+ apiKey: config.deepSeekAPIKey,
202
+ baseURL: "https://api.deepseek.com/v1",
203
+ };
204
+ }
205
+ if (modelName.startsWith("kimi-") || modelName.startsWith("moonshot-")) {
206
+ return {
207
+ apiKey: config.kimiAPIKey,
208
+ baseURL: "https://api.moonshot.ai/v1",
209
+ };
210
+ }
211
+ if (modelName.startsWith("glm-")) {
212
+ return {
213
+ apiKey: config.glmAPIKey,
214
+ baseURL: "https://open.bigmodel.cn/api/paas/v4/",
215
+ };
216
+ }
217
+ return null;
218
+ };
187
219
  /**
188
220
  * Instantiates and returns the appropriate LangChain chat model based on the model
189
221
  * name prefix. `claude-` maps to `ChatAnthropic`, `gemini-` maps to
190
- * `ChatGoogleGenerativeAI`, and `gpt-` maps to `ChatOpenAI`. When a Zod `schema` is
191
- * provided for a GPT model, native `response_format` with `json_schema` is configured
192
- * on the OpenAI instance for structured output. Throws a `BAD_REQUEST` error for
193
- * unrecognised model names.
222
+ * `ChatGoogleGenerativeAI`, and `gpt-` maps to `ChatOpenAI`. DeepSeek (`deepseek-`),
223
+ * Kimi/Moonshot (`kimi-`, `moonshot-`), and GLM/Zhipu (`glm-`) models are routed
224
+ * through `ChatOpenAI` against each provider's OpenAI-compatible endpoint. When a Zod
225
+ * `schema` is provided for a GPT model, native `response_format` with `json_schema` is
226
+ * configured for structured output. Throws a `BAD_REQUEST` error for unrecognised
227
+ * model names.
194
228
  * @param modelName - The model identifier, e.g. `"gpt-4o"`, `"claude-3-5-sonnet"`,
195
- * `"gemini-1.5-pro"`
229
+ * `"gemini-1.5-pro"`, `"deepseek-chat"`, `"kimi-k2-0905-preview"`, `"glm-4.6"`
196
230
  * @param config - Configuration object containing API keys (`openAIAPIKey`,
197
- * `anthropicAPIKey`, `googleAPIKey`), optional `maxTokens`, and optional `temperature`
231
+ * `anthropicAPIKey`, `googleAPIKey`, `deepSeekAPIKey`, `kimiAPIKey`, `glmAPIKey`),
232
+ * optional `maxTokens`, and optional `temperature`
198
233
  * @param schema - Optional Zod schema used to configure structured JSON output for
199
234
  * OpenAI GPT models via `response_format`; ignored for other providers
200
235
  * @returns A configured LangChain chat model instance
@@ -245,6 +280,17 @@ const getLLMModel = (modelName, config, schema = null) => {
245
280
  }
246
281
  return new ChatOpenAI(openAISettings);
247
282
  }
283
+ // OpenAI-compatible providers: DeepSeek, Kimi (Moonshot), GLM (Zhipu)
284
+ const openAICompatible = getOpenAICompatibleProvider(modelName, config);
285
+ if (openAICompatible) {
286
+ return new ChatOpenAI({
287
+ apiKey: openAICompatible.apiKey,
288
+ maxTokens: config.maxTokens || 200000,
289
+ modelName: modelName,
290
+ configuration: { baseURL: openAICompatible.baseURL },
291
+ ...modelSettings,
292
+ });
293
+ }
248
294
  throw errorHandlingHelper.create(constants.HTTP_CODES.BAD_REQUEST, constants.ERROR.UNSUPPORTED_MODEL + ": " + modelName);
249
295
  };
250
296
  /**
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "restricted"
5
5
  },
6
- "version": "1.0.12",
6
+ "version": "1.0.14",
7
7
  "description": "",
8
8
  "main": "dist/cjs/index.js",
9
9
  "module": "dist/esm/index.js",