@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.
- package/dist/cjs/langChain.d.ts.map +1 -1
- package/dist/cjs/langChain.js +77 -31
- package/dist/esm/langChain.d.ts.map +1 -1
- package/dist/esm/langChain.js +77 -31
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";
|
|
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"}
|
package/dist/cjs/langChain.js
CHANGED
|
@@ -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 `
|
|
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.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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`.
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
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`
|
|
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":";
|
|
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"}
|
package/dist/esm/langChain.js
CHANGED
|
@@ -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 `
|
|
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.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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`.
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
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`
|
|
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
|
/**
|