@stackfactor/agent-utils 1.0.13 → 1.0.15

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/README.md CHANGED
@@ -25,7 +25,7 @@ import {
25
25
 
26
26
  ## `langChain`
27
27
 
28
- Unified interface for running LLM prompts, managing LangChain agents, and generating images across OpenAI, Anthropic, and Google providers.
28
+ Unified interface for running LLM prompts, managing LangChain agents, and generating images across OpenAI, Anthropic, Google, DeepSeek, Kimi (Moonshot), and GLM (Zhipu) providers.
29
29
 
30
30
  ### `langChain.runPromptWithModel(modelName, config, prompt, onProgressReport?, minPercent?, maxPercent?, expectsJsonResponse?, schema?, agentName?, tools?)`
31
31
 
@@ -39,8 +39,8 @@ When `expectsJsonResponse` is `true` (the default), JSON escape instructions are
39
39
 
40
40
  | Parameter | Type | Default | Description |
41
41
  | --------------------- | -------------------- | --------------- | -------------------------------------------------------------------------------------------------------- |
42
- | `modelName` | `string` | — | Model identifier: `"gpt-4o"`, `"claude-3-5-sonnet"`, `"gemini-1.5-pro"`, etc. |
43
- | `config` | `object` | — | API keys (`openAIAPIKey`, `anthropicAPIKey`, `googleAPIKey`), `temperature`, `agentic`, `recursionLimit` |
42
+ | `modelName` | `string` | — | Model identifier: `"gpt-4o"`, `"claude-3-5-sonnet"`, `"gemini-1.5-pro"`, `"deepseek-chat"`, `"kimi-k2-0905-preview"`, `"glm-4.6"`, etc. |
43
+ | `config` | `object` | — | API keys (`openAIAPIKey`, `anthropicAPIKey`, `googleAPIKey`, `deepSeekAPIKey`, `kimiAPIKey`, `glmAPIKey`), `temperature`, `agentic`, `recursionLimit` |
44
44
  | `prompt` | `string \| object[]` | — | Plain string or array of `{ role, content }` message objects |
45
45
  | `onProgressReport` | `function \| null` | `null` | Async callback receiving `{ message, progress }` updates |
46
46
  | `minPercent` | `number` | `0` | Lower bound for progress percentage |
@@ -266,11 +266,16 @@ Shared constants used across the package.
266
266
 
267
267
  ### Text / Chat
268
268
 
269
- | Provider | Model Prefix | Example |
270
- | --------- | ------------ | ------------------------------------ |
271
- | OpenAI | `gpt-` | `gpt-4o`, `gpt-4o-mini` |
272
- | Anthropic | `claude-` | `claude-3-5-sonnet`, `claude-3-opus` |
273
- | Google | `gemini-` | `gemini-1.5-pro`, `gemini-2.0-flash` |
269
+ | Provider | Model Prefix | Example |
270
+ | ----------------- | ---------------------- | ------------------------------------ |
271
+ | OpenAI | `gpt-` | `gpt-4o`, `gpt-4o-mini` |
272
+ | Anthropic | `claude-` | `claude-3-5-sonnet`, `claude-3-opus` |
273
+ | Google | `gemini-` | `gemini-1.5-pro`, `gemini-2.0-flash` |
274
+ | DeepSeek | `deepseek-` | `deepseek-chat`, `deepseek-reasoner` |
275
+ | Kimi (Moonshot) | `kimi-`, `moonshot-` | `kimi-k2-0905-preview`, `moonshot-v1-8k` |
276
+ | GLM (Zhipu) | `glm-` | `glm-4.6`, `glm-4.5` |
277
+
278
+ DeepSeek, Kimi, and GLM are routed through their OpenAI-compatible endpoints. Native structured output via `response_format` with JSON schema is only applied to `gpt-*` models; all other providers (including these three) use prompt-based JSON instructions.
274
279
 
275
280
  ### Image Generation
276
281
 
@@ -290,8 +295,11 @@ The `config` object accepted by LangChain methods supports the following keys:
290
295
  | `openAIAPIKey` | `string` | OpenAI API key |
291
296
  | `anthropicAPIKey` | `string` | Anthropic API key |
292
297
  | `googleAPIKey` | `string` | Google AI API key |
298
+ | `deepSeekAPIKey` | `string` | DeepSeek API key |
299
+ | `kimiAPIKey` | `string` | Kimi (Moonshot) API key |
300
+ | `glmAPIKey` | `string` | GLM (Zhipu) API key |
293
301
  | `temperature` | `number` | Sampling temperature |
294
- | `maxTokens` | `number` | Maximum output tokens (default: `200000`) |
302
+ | `maxTokens` | `number` | Maximum output tokens (default: `8192` for Claude, `200000` for other providers) |
295
303
  | `agentic` | `boolean` | Enable agentic mode in `runPromptWithModel` |
296
304
  | `recursionLimit` | `number` | Max agent steps (default: `25`) |
297
305
 
@@ -1 +1 @@
1
- {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAmSQ,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"}
1
+ {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAsVQ,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"}
@@ -191,17 +191,50 @@ const extractJSONFromResponse = (text) => {
191
191
  }
192
192
  return null;
193
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
+ };
194
224
  /**
195
225
  * Instantiates and returns the appropriate LangChain chat model based on the model
196
226
  * name prefix. `claude-` maps to `ChatAnthropic`, `gemini-` maps to
197
- * `ChatGoogleGenerativeAI`, and `gpt-` maps to `ChatOpenAI`. When a Zod `schema` is
198
- * provided for a GPT model, native `response_format` with `json_schema` is configured
199
- * on the OpenAI instance for structured output. Throws a `BAD_REQUEST` error for
200
- * 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.
201
233
  * @param modelName - The model identifier, e.g. `"gpt-4o"`, `"claude-3-5-sonnet"`,
202
- * `"gemini-1.5-pro"`
234
+ * `"gemini-1.5-pro"`, `"deepseek-chat"`, `"kimi-k2-0905-preview"`, `"glm-4.6"`
203
235
  * @param config - Configuration object containing API keys (`openAIAPIKey`,
204
- * `anthropicAPIKey`, `googleAPIKey`), optional `maxTokens`, and optional `temperature`
236
+ * `anthropicAPIKey`, `googleAPIKey`, `deepSeekAPIKey`, `kimiAPIKey`, `glmAPIKey`),
237
+ * optional `maxTokens`, and optional `temperature`
205
238
  * @param schema - Optional Zod schema used to configure structured JSON output for
206
239
  * OpenAI GPT models via `response_format`; ignored for other providers
207
240
  * @returns A configured LangChain chat model instance
@@ -212,9 +245,12 @@ const getLLMModel = (modelName, config, schema = null) => {
212
245
  };
213
246
  // Claude models (Anthropic)
214
247
  if (modelName.startsWith("claude-")) {
248
+ // Anthropic's SDK rejects non-streamed requests when max_tokens is large
249
+ // enough that the operation could exceed 10 minutes. Cap the default well
250
+ // below that threshold; callers can override via config.maxTokens.
215
251
  return new anthropic_1.ChatAnthropic({
216
252
  apiKey: config.anthropicAPIKey,
217
- maxTokens: config.maxTokens || 200000,
253
+ maxTokens: config.maxTokens || 16384,
218
254
  modelName: modelName,
219
255
  ...modelSettings,
220
256
  });
@@ -252,6 +288,17 @@ const getLLMModel = (modelName, config, schema = null) => {
252
288
  }
253
289
  return new openai_1.ChatOpenAI(openAISettings);
254
290
  }
291
+ // OpenAI-compatible providers: DeepSeek, Kimi (Moonshot), GLM (Zhipu)
292
+ const openAICompatible = getOpenAICompatibleProvider(modelName, config);
293
+ if (openAICompatible) {
294
+ return new openai_1.ChatOpenAI({
295
+ apiKey: openAICompatible.apiKey,
296
+ maxTokens: config.maxTokens || 200000,
297
+ modelName: modelName,
298
+ configuration: { baseURL: openAICompatible.baseURL },
299
+ ...modelSettings,
300
+ });
301
+ }
255
302
  throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.BAD_REQUEST, const_js_1.default.ERROR.UNSUPPORTED_MODEL + ": " + modelName);
256
303
  };
257
304
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAmSQ,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"}
1
+ {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAsVQ,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"}
@@ -186,17 +186,50 @@ const extractJSONFromResponse = (text) => {
186
186
  }
187
187
  return null;
188
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
+ };
189
219
  /**
190
220
  * Instantiates and returns the appropriate LangChain chat model based on the model
191
221
  * name prefix. `claude-` maps to `ChatAnthropic`, `gemini-` maps to
192
- * `ChatGoogleGenerativeAI`, and `gpt-` maps to `ChatOpenAI`. When a Zod `schema` is
193
- * provided for a GPT model, native `response_format` with `json_schema` is configured
194
- * on the OpenAI instance for structured output. Throws a `BAD_REQUEST` error for
195
- * 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.
196
228
  * @param modelName - The model identifier, e.g. `"gpt-4o"`, `"claude-3-5-sonnet"`,
197
- * `"gemini-1.5-pro"`
229
+ * `"gemini-1.5-pro"`, `"deepseek-chat"`, `"kimi-k2-0905-preview"`, `"glm-4.6"`
198
230
  * @param config - Configuration object containing API keys (`openAIAPIKey`,
199
- * `anthropicAPIKey`, `googleAPIKey`), optional `maxTokens`, and optional `temperature`
231
+ * `anthropicAPIKey`, `googleAPIKey`, `deepSeekAPIKey`, `kimiAPIKey`, `glmAPIKey`),
232
+ * optional `maxTokens`, and optional `temperature`
200
233
  * @param schema - Optional Zod schema used to configure structured JSON output for
201
234
  * OpenAI GPT models via `response_format`; ignored for other providers
202
235
  * @returns A configured LangChain chat model instance
@@ -207,9 +240,12 @@ const getLLMModel = (modelName, config, schema = null) => {
207
240
  };
208
241
  // Claude models (Anthropic)
209
242
  if (modelName.startsWith("claude-")) {
243
+ // Anthropic's SDK rejects non-streamed requests when max_tokens is large
244
+ // enough that the operation could exceed 10 minutes. Cap the default well
245
+ // below that threshold; callers can override via config.maxTokens.
210
246
  return new ChatAnthropic({
211
247
  apiKey: config.anthropicAPIKey,
212
- maxTokens: config.maxTokens || 200000,
248
+ maxTokens: config.maxTokens || 16384,
213
249
  modelName: modelName,
214
250
  ...modelSettings,
215
251
  });
@@ -247,6 +283,17 @@ const getLLMModel = (modelName, config, schema = null) => {
247
283
  }
248
284
  return new ChatOpenAI(openAISettings);
249
285
  }
286
+ // OpenAI-compatible providers: DeepSeek, Kimi (Moonshot), GLM (Zhipu)
287
+ const openAICompatible = getOpenAICompatibleProvider(modelName, config);
288
+ if (openAICompatible) {
289
+ return new ChatOpenAI({
290
+ apiKey: openAICompatible.apiKey,
291
+ maxTokens: config.maxTokens || 200000,
292
+ modelName: modelName,
293
+ configuration: { baseURL: openAICompatible.baseURL },
294
+ ...modelSettings,
295
+ });
296
+ }
250
297
  throw errorHandlingHelper.create(constants.HTTP_CODES.BAD_REQUEST, constants.ERROR.UNSUPPORTED_MODEL + ": " + modelName);
251
298
  };
252
299
  /**
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "restricted"
5
5
  },
6
- "version": "1.0.13",
6
+ "version": "1.0.15",
7
7
  "description": "",
8
8
  "main": "dist/cjs/index.js",
9
9
  "module": "dist/esm/index.js",