@stackfactor/agent-utils 1.0.1 → 1.0.3
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 +100 -91
- package/dist/cjs/langChain.d.ts +2 -2
- package/dist/cjs/langChain.d.ts.map +1 -1
- package/dist/cjs/langChain.js +9 -9
- package/dist/esm/langChain.d.ts +2 -2
- package/dist/esm/langChain.d.ts.map +1 -1
- package/dist/esm/langChain.js +9 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,12 @@ npm install @stackfactor/agent-utils
|
|
|
13
13
|
The package exports four modules:
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
langChain,
|
|
18
|
+
logger,
|
|
19
|
+
errorHandling,
|
|
20
|
+
constants,
|
|
21
|
+
} from "@stackfactor/agent-utils";
|
|
17
22
|
```
|
|
18
23
|
|
|
19
24
|
---
|
|
@@ -32,18 +37,18 @@ Sends a prompt to an LLM and returns the response. Supports three execution mode
|
|
|
32
37
|
|
|
33
38
|
When `expectsJsonResponse` is `true` (the default), JSON escape instructions are injected and the response is parsed. If a Zod `schema` is provided, the parsed result is validated.
|
|
34
39
|
|
|
35
|
-
| Parameter
|
|
36
|
-
|
|
37
|
-
| `modelName`
|
|
38
|
-
| `config`
|
|
39
|
-
| `prompt`
|
|
40
|
-
| `onProgressReport`
|
|
41
|
-
| `minPercent`
|
|
42
|
-
| `maxPercent`
|
|
43
|
-
| `expectsJsonResponse` | `boolean`
|
|
44
|
-
| `schema`
|
|
45
|
-
| `agentName`
|
|
46
|
-
| `tools`
|
|
40
|
+
| Parameter | Type | Default | Description |
|
|
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` |
|
|
44
|
+
| `prompt` | `string \| object[]` | — | Plain string or array of `{ role, content }` message objects |
|
|
45
|
+
| `onProgressReport` | `function \| null` | `null` | Async callback receiving `{ message, progress }` updates |
|
|
46
|
+
| `minPercent` | `number` | `0` | Lower bound for progress percentage |
|
|
47
|
+
| `maxPercent` | `number` | `100` | Upper bound for progress percentage |
|
|
48
|
+
| `expectsJsonResponse` | `boolean` | `true` | Parse response as JSON |
|
|
49
|
+
| `schema` | `ZodSchema \| null` | `null` | Zod schema for response validation |
|
|
50
|
+
| `agentName` | `string` | `"StackFactor"` | Display name for the agent (agentic mode) |
|
|
51
|
+
| `tools` | `any[]` | `[]` | LangChain tools available to the agent (agentic mode) |
|
|
47
52
|
|
|
48
53
|
**Returns:** JSON string (when `expectsJsonResponse` is `true`) or raw content string.
|
|
49
54
|
|
|
@@ -61,12 +66,12 @@ const result = await langChain.runPromptWithModel(
|
|
|
61
66
|
|
|
62
67
|
Sends a conversational chat prompt to an LLM and returns a plain HTML response. Built for the StackFactor Mentor chat feature — system messages are wrapped with topic-constraint instructions that decline off-topic questions.
|
|
63
68
|
|
|
64
|
-
| Parameter
|
|
65
|
-
|
|
66
|
-
| `modelName`
|
|
67
|
-
| `config`
|
|
68
|
-
| `prompt`
|
|
69
|
-
| `onProgressReport` | `function \| null`
|
|
69
|
+
| Parameter | Type | Default | Description |
|
|
70
|
+
| ------------------ | -------------------- | ------- | ----------------------------------------------------- |
|
|
71
|
+
| `modelName` | `string` | — | Model identifier |
|
|
72
|
+
| `config` | `object` | — | API keys and optional `temperature` |
|
|
73
|
+
| `prompt` | `string \| object[]` | — | Plain string or array of `{ role, content }` messages |
|
|
74
|
+
| `onProgressReport` | `function \| null` | `null` | Streaming progress callback |
|
|
70
75
|
|
|
71
76
|
**Returns:** Raw HTML string.
|
|
72
77
|
|
|
@@ -87,17 +92,17 @@ const html = await langChain.runChatPromptWithModel(
|
|
|
87
92
|
|
|
88
93
|
Constructs a LangChain agent with a model, system prompt, and tools. When `onReportProgress` is provided, a `report_progress` tool is automatically added.
|
|
89
94
|
|
|
90
|
-
| Parameter
|
|
91
|
-
|
|
92
|
-
| `name`
|
|
93
|
-
| `modelName`
|
|
94
|
-
| `systemPrompt`
|
|
95
|
-
| `tools`
|
|
96
|
-
| `responseFormat`
|
|
97
|
-
| `config`
|
|
98
|
-
| `onReportProgress` | `Function \| null` | `null`
|
|
99
|
-
| `minPercent`
|
|
100
|
-
| `maxPercent`
|
|
95
|
+
| Parameter | Type | Default | Description |
|
|
96
|
+
| ------------------ | ------------------ | ------- | ---------------------------------------- |
|
|
97
|
+
| `name` | `string` | — | Display name for the agent |
|
|
98
|
+
| `modelName` | `string` | — | LLM identifier |
|
|
99
|
+
| `systemPrompt` | `string` | — | System prompt describing agent behaviour |
|
|
100
|
+
| `tools` | `any[]` | `[]` | LangChain tool instances |
|
|
101
|
+
| `responseFormat` | `any` | — | Structured response format descriptor |
|
|
102
|
+
| `config` | `object` | — | API keys, temperature, etc. |
|
|
103
|
+
| `onReportProgress` | `Function \| null` | `null` | Progress callback |
|
|
104
|
+
| `minPercent` | `number` | `0` | Minimum reportable progress |
|
|
105
|
+
| `maxPercent` | `number` | `100` | Maximum reportable progress |
|
|
101
106
|
|
|
102
107
|
**Returns:** A configured LangChain agent instance.
|
|
103
108
|
|
|
@@ -107,12 +112,12 @@ Constructs a LangChain agent with a model, system prompt, and tools. When `onRep
|
|
|
107
112
|
|
|
108
113
|
Executes a LangChain agent with a user prompt. Registers callbacks for `tool_start`, `tool_end`, `agent_action`, and error events when `onProgress` is provided. Logs execution time on completion.
|
|
109
114
|
|
|
110
|
-
| Parameter
|
|
111
|
-
|
|
112
|
-
| `agent`
|
|
113
|
-
| `prompt`
|
|
114
|
-
| `config`
|
|
115
|
-
| `onProgress` | `Function \| null` | `null`
|
|
115
|
+
| Parameter | Type | Default | Description |
|
|
116
|
+
| ------------ | ------------------ | ------- | -------------------------------- |
|
|
117
|
+
| `agent` | `any` | — | Agent created by `createAIAgent` |
|
|
118
|
+
| `prompt` | `string` | — | User message to send |
|
|
119
|
+
| `config` | `object` | — | `recursionLimit` (default: `25`) |
|
|
120
|
+
| `onProgress` | `Function \| null` | `null` | Progress event callback |
|
|
116
121
|
|
|
117
122
|
**Returns:** Raw response from the agent's `invoke` method.
|
|
118
123
|
|
|
@@ -126,7 +131,11 @@ const agent = langChain.createAIAgent(
|
|
|
126
131
|
{ openAIAPIKey: "sk-..." },
|
|
127
132
|
);
|
|
128
133
|
|
|
129
|
-
const response = await langChain.runAIAgent(
|
|
134
|
+
const response = await langChain.runAIAgent(
|
|
135
|
+
agent,
|
|
136
|
+
"Summarize this text...",
|
|
137
|
+
{},
|
|
138
|
+
);
|
|
130
139
|
```
|
|
131
140
|
|
|
132
141
|
---
|
|
@@ -135,12 +144,12 @@ const response = await langChain.runAIAgent(agent, "Summarize this text...", {})
|
|
|
135
144
|
|
|
136
145
|
Generates an image using OpenAI or Google AI models.
|
|
137
146
|
|
|
138
|
-
| Parameter
|
|
139
|
-
|
|
140
|
-
| `modelName` | `string` | —
|
|
141
|
-
| `config`
|
|
142
|
-
| `prompt`
|
|
143
|
-
| `options`
|
|
147
|
+
| Parameter | Type | Default | Description |
|
|
148
|
+
| ----------- | -------- | ------- | --------------------------------------------------------------------------------------------------------- |
|
|
149
|
+
| `modelName` | `string` | — | Image model: `"dall-e-3"`, `"gpt-image-1.5"`, `"imagen-4.0-generate-001"`, `"gemini-3.0-pro-image"`, etc. |
|
|
150
|
+
| `config` | `object` | — | `openAIAPIKey` or `googleAPIKey` |
|
|
151
|
+
| `prompt` | `string` | — | Text prompt describing the image |
|
|
152
|
+
| `options` | `object` | `{}` | Provider-specific options (see below) |
|
|
144
153
|
|
|
145
154
|
**OpenAI options:** `size` (`"1024x1024"`, `"1792x1024"`, etc.), `style` (`"vivid"` or `"natural"`), `responseFormat` (`"url"` or `"b64_json"`), `n` (number of images).
|
|
146
155
|
|
|
@@ -163,8 +172,8 @@ const image = await langChain.runPromptWithModelForImageGeneration(
|
|
|
163
172
|
|
|
164
173
|
Guards that a response is a non-empty string. Throws an `INTERNAL_SERVER_ERROR` if not.
|
|
165
174
|
|
|
166
|
-
| Parameter
|
|
167
|
-
|
|
175
|
+
| Parameter | Type | Description |
|
|
176
|
+
| ---------- | ----- | ----------------- |
|
|
168
177
|
| `response` | `any` | Value to validate |
|
|
169
178
|
|
|
170
179
|
**Returns:** The response string if valid.
|
|
@@ -179,12 +188,12 @@ GCP-compatible structured logging via Winston with OpenTelemetry trace enrichmen
|
|
|
179
188
|
|
|
180
189
|
Writes a structured log entry. Automatically enriches with OpenTelemetry `traceId` and `spanId` when an active span exists. Prepends the user's email from the request object when available.
|
|
181
190
|
|
|
182
|
-
| Parameter | Type
|
|
183
|
-
|
|
184
|
-
| `request` | `any`
|
|
185
|
-
| `level`
|
|
186
|
-
| `message` | `string`
|
|
187
|
-
| `options` | `object`
|
|
191
|
+
| Parameter | Type | Default | Description |
|
|
192
|
+
| --------- | ---------- | ------- | -------------------------------------------------------------------------- |
|
|
193
|
+
| `request` | `any` | — | HTTP request object (reads `request.user.email`), or `null` |
|
|
194
|
+
| `level` | `LogLevel` | — | `"error"`, `"warn"`, `"info"`, `"http"`, `"verbose"`, `"debug"`, `"silly"` |
|
|
195
|
+
| `message` | `string` | — | Log message |
|
|
196
|
+
| `options` | `object` | `{}` | Additional structured fields (`service`, `requestId`, etc.) |
|
|
188
197
|
|
|
189
198
|
```typescript
|
|
190
199
|
logger.log(req, logger.levels.info, "User signed in", { service: "auth" });
|
|
@@ -196,13 +205,13 @@ logger.log(null, logger.levels.error, "Connection failed");
|
|
|
196
205
|
Enum-like object mapping level names to their string values:
|
|
197
206
|
|
|
198
207
|
```typescript
|
|
199
|
-
logger.levels.error
|
|
200
|
-
logger.levels.warn
|
|
201
|
-
logger.levels.info
|
|
202
|
-
logger.levels.http
|
|
203
|
-
logger.levels.verbose // "verbose"
|
|
204
|
-
logger.levels.debug
|
|
205
|
-
logger.levels.silly
|
|
208
|
+
logger.levels.error; // "error"
|
|
209
|
+
logger.levels.warn; // "warn"
|
|
210
|
+
logger.levels.info; // "info"
|
|
211
|
+
logger.levels.http; // "http"
|
|
212
|
+
logger.levels.verbose; // "verbose"
|
|
213
|
+
logger.levels.debug; // "debug"
|
|
214
|
+
logger.levels.silly; // "silly"
|
|
206
215
|
```
|
|
207
216
|
|
|
208
217
|
---
|
|
@@ -215,11 +224,11 @@ Factory for consistently shaped error payloads.
|
|
|
215
224
|
|
|
216
225
|
Creates a structured error object with a stack trace.
|
|
217
226
|
|
|
218
|
-
| Parameter
|
|
219
|
-
|
|
220
|
-
| `errorCode`
|
|
221
|
-
| `errorMessage` | `string` | —
|
|
222
|
-
| `details`
|
|
227
|
+
| Parameter | Type | Default | Description |
|
|
228
|
+
| -------------- | -------- | ------- | ------------------------------------------ |
|
|
229
|
+
| `errorCode` | `number` | — | HTTP status code or application error code |
|
|
230
|
+
| `errorMessage` | `string` | — | Human-readable error description |
|
|
231
|
+
| `details` | `any` | `null` | Optional supplementary information |
|
|
223
232
|
|
|
224
233
|
**Returns:** `{ code, message, details, stack }`
|
|
225
234
|
|
|
@@ -236,20 +245,20 @@ Shared constants used across the package.
|
|
|
236
245
|
|
|
237
246
|
### `constants.HTTP_CODES`
|
|
238
247
|
|
|
239
|
-
| Constant
|
|
240
|
-
|
|
241
|
-
| `BAD_REQUEST`
|
|
242
|
-
| `UNPROCESSABLE_ENTITY`
|
|
248
|
+
| Constant | Value |
|
|
249
|
+
| ----------------------- | ----- |
|
|
250
|
+
| `BAD_REQUEST` | `400` |
|
|
251
|
+
| `UNPROCESSABLE_ENTITY` | `422` |
|
|
243
252
|
| `INTERNAL_SERVER_ERROR` | `500` |
|
|
244
|
-
| `BAD_GATEWAY`
|
|
253
|
+
| `BAD_GATEWAY` | `502` |
|
|
245
254
|
|
|
246
255
|
### `constants.ERROR`
|
|
247
256
|
|
|
248
|
-
| Constant
|
|
249
|
-
|
|
250
|
-
| `UNABLE_TO_GENERATE_CONTENT` | `"Unable to generate content"`
|
|
251
|
-
| `UNEXPECTED_ERROR`
|
|
252
|
-
| `UNSUPPORTED_MODEL`
|
|
257
|
+
| Constant | Value |
|
|
258
|
+
| ---------------------------- | ---------------------------------------- |
|
|
259
|
+
| `UNABLE_TO_GENERATE_CONTENT` | `"Unable to generate content"` |
|
|
260
|
+
| `UNEXPECTED_ERROR` | `"An unexpected error occured..."` |
|
|
261
|
+
| `UNSUPPORTED_MODEL` | `"The specified model is not supported"` |
|
|
253
262
|
|
|
254
263
|
---
|
|
255
264
|
|
|
@@ -257,18 +266,18 @@ Shared constants used across the package.
|
|
|
257
266
|
|
|
258
267
|
### Text / Chat
|
|
259
268
|
|
|
260
|
-
| Provider
|
|
261
|
-
|
|
262
|
-
| OpenAI
|
|
263
|
-
| Anthropic | `claude-`
|
|
264
|
-
| Google
|
|
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` |
|
|
265
274
|
|
|
266
275
|
### Image Generation
|
|
267
276
|
|
|
268
|
-
| Provider | Model Prefix
|
|
269
|
-
|
|
270
|
-
| OpenAI
|
|
271
|
-
| Google
|
|
277
|
+
| Provider | Model Prefix | Example |
|
|
278
|
+
| -------- | ----------------------- | ------------------------------------------------- |
|
|
279
|
+
| OpenAI | `dall-e-`, `gpt-image-` | `dall-e-3`, `gpt-image-1.5` |
|
|
280
|
+
| Google | `imagen-`, `gemini-` | `imagen-4.0-generate-001`, `gemini-3.0-pro-image` |
|
|
272
281
|
|
|
273
282
|
---
|
|
274
283
|
|
|
@@ -276,15 +285,15 @@ Shared constants used across the package.
|
|
|
276
285
|
|
|
277
286
|
The `config` object accepted by LangChain methods supports the following keys:
|
|
278
287
|
|
|
279
|
-
| Key
|
|
280
|
-
|
|
281
|
-
| `openAIAPIKey`
|
|
282
|
-
| `anthropicAPIKey` | `string`
|
|
283
|
-
| `googleAPIKey`
|
|
284
|
-
| `temperature`
|
|
285
|
-
| `maxTokens`
|
|
286
|
-
| `agentic`
|
|
287
|
-
| `recursionLimit`
|
|
288
|
+
| Key | Type | Description |
|
|
289
|
+
| ----------------- | --------- | ------------------------------------------- |
|
|
290
|
+
| `openAIAPIKey` | `string` | OpenAI API key |
|
|
291
|
+
| `anthropicAPIKey` | `string` | Anthropic API key |
|
|
292
|
+
| `googleAPIKey` | `string` | Google AI API key |
|
|
293
|
+
| `temperature` | `number` | Sampling temperature |
|
|
294
|
+
| `maxTokens` | `number` | Maximum output tokens (default: `200000`) |
|
|
295
|
+
| `agentic` | `boolean` | Enable agentic mode in `runPromptWithModel` |
|
|
296
|
+
| `recursionLimit` | `number` | Max agent steps (default: `25`) |
|
|
288
297
|
|
|
289
298
|
---
|
|
290
299
|
|
package/dist/cjs/langChain.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
createAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any, onReportProgress?: Function | null, minPercent?: number, maxPercent?: number) => any;
|
|
3
|
+
runAgent: (agent: any, prompt: string, config: any, onProgress?: Function | null) => Promise<any>;
|
|
4
4
|
runChatPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any) => any;
|
|
5
5
|
runPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any, minPercent?: number, maxPercent?: number, expectsJsonResponse?: boolean, schema?: any, agentName?: string, tools?: any[]) => Promise<any>;
|
|
6
6
|
runPromptWithModelForImageGeneration: (modelName: string, config: any, prompt: string, options?: any) => Promise<any>;
|
|
@@ -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":";wBAoaQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,qBACO,QAAQ,GAAG,IAAI,eACrB,MAAM,eACN,MAAM,KACjB,GAAG;sBAyCG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCAioBF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCApfO,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;sDAyyBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CAz2B8B,GAAG,KAAG,MAAM;;AAo6BzD,wBAOE"}
|
package/dist/cjs/langChain.js
CHANGED
|
@@ -9,7 +9,7 @@ const google_genai_1 = require("@langchain/google-genai");
|
|
|
9
9
|
const openai_2 = require("openai");
|
|
10
10
|
const genai_1 = require("@google/genai");
|
|
11
11
|
const const_js_1 = __importDefault(require("./const.js"));
|
|
12
|
-
const langchain_1 = require("langchain");
|
|
12
|
+
const langchain_1 = __importDefault(require("langchain"));
|
|
13
13
|
const errorHandling_js_1 = __importDefault(require("./errorHandling.js"));
|
|
14
14
|
const logger_js_1 = __importDefault(require("./logger.js"));
|
|
15
15
|
const zod_1 = require("zod");
|
|
@@ -264,7 +264,7 @@ const safeJsonParse = (line) => {
|
|
|
264
264
|
* report; defaults to `100`
|
|
265
265
|
* @returns A LangChain tool instance configured with a Zod schema for `{ stage, message, percent }`
|
|
266
266
|
*/
|
|
267
|
-
const getAIProgressTool = (onProgress, minPercent = 0, maxPercent = 100) => langchain_1.tool(async ({ stage, message, percent }) => {
|
|
267
|
+
const getAIProgressTool = (onProgress, minPercent = 0, maxPercent = 100) => langchain_1.default.tool(async ({ stage, message, percent }) => {
|
|
268
268
|
try {
|
|
269
269
|
// Clamp percent to be within minPercent and maxPercent bounds
|
|
270
270
|
const rawPct = typeof percent === "number" ? percent : 0;
|
|
@@ -387,7 +387,7 @@ const getLLMModel = (modelName, config, schema = null) => {
|
|
|
387
387
|
* defaults to `100`
|
|
388
388
|
* @returns A configured LangChain agent instance ready to be run with `runAIAgent`
|
|
389
389
|
*/
|
|
390
|
-
const
|
|
390
|
+
const createAgent = (name, modelName, systemPrompt, tools = [], responseFormat, config, onReportProgress = null, minPercent = 0, maxPercent = 100) => {
|
|
391
391
|
//Prepare the complete system prompt with progress reporting instructions if needed
|
|
392
392
|
const completeSystemPrompt = `
|
|
393
393
|
${systemPrompt}
|
|
@@ -396,7 +396,7 @@ const createAIAgent = (name, modelName, systemPrompt, tools = [], responseFormat
|
|
|
396
396
|
: ""}
|
|
397
397
|
`.trim();
|
|
398
398
|
// Create the agent with the specified model, system prompt, tools, and response format
|
|
399
|
-
const agent =
|
|
399
|
+
const agent = langchain_1.default.createAgent({
|
|
400
400
|
name: name,
|
|
401
401
|
model: getLLMModel(modelName, config),
|
|
402
402
|
systemPrompt: completeSystemPrompt,
|
|
@@ -421,7 +421,7 @@ const createAIAgent = (name, modelName, systemPrompt, tools = [], responseFormat
|
|
|
421
421
|
* agent execution
|
|
422
422
|
* @returns The raw response object returned by the agent's `invoke` method
|
|
423
423
|
*/
|
|
424
|
-
const
|
|
424
|
+
const runAgent = async (agent, prompt, config, onProgress = null) => {
|
|
425
425
|
const startTime = Date.now();
|
|
426
426
|
// Build callbacks for progress reporting if onProgress is provided
|
|
427
427
|
// const agentDisplayName = `${agent.options?.name} AI Agent`;
|
|
@@ -592,14 +592,14 @@ const runPromptWithModel = async (modelName, config, prompt, onProgressReport, m
|
|
|
592
592
|
agentTools.push(getAIProgressTool(trackingProgressCallback, minPercent, maxPercent));
|
|
593
593
|
}
|
|
594
594
|
// Create the agent with tools
|
|
595
|
-
const agent =
|
|
595
|
+
const agent = createAgent(agentName, modelName, systemPrompt, agentTools, null, // responseFormat
|
|
596
596
|
config, null, // onReportProgress handled via tools array
|
|
597
597
|
minPercent, maxPercent);
|
|
598
598
|
// Run the agent with callback that includes current progress
|
|
599
599
|
const callbackWithProgress = onProgressReport
|
|
600
600
|
? (data) => onProgressReport({ ...data, progress: progressTracker.current })
|
|
601
601
|
: null;
|
|
602
|
-
const response = await
|
|
602
|
+
const response = await runAgent(agent, userPrompt, config, callbackWithProgress);
|
|
603
603
|
// Extract content from agent response
|
|
604
604
|
const messages = response?.messages || [];
|
|
605
605
|
if (messages.length === 0) {
|
|
@@ -1232,8 +1232,8 @@ const runPromptWithModelForImageGeneration = async (modelName, config, prompt, o
|
|
|
1232
1232
|
}
|
|
1233
1233
|
};
|
|
1234
1234
|
exports.default = {
|
|
1235
|
-
|
|
1236
|
-
|
|
1235
|
+
createAgent,
|
|
1236
|
+
runAgent,
|
|
1237
1237
|
runChatPromptWithModel,
|
|
1238
1238
|
runPromptWithModel,
|
|
1239
1239
|
runPromptWithModelForImageGeneration,
|
package/dist/esm/langChain.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
createAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any, onReportProgress?: Function | null, minPercent?: number, maxPercent?: number) => any;
|
|
3
|
+
runAgent: (agent: any, prompt: string, config: any, onProgress?: Function | null) => Promise<any>;
|
|
4
4
|
runChatPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any) => any;
|
|
5
5
|
runPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any, minPercent?: number, maxPercent?: number, expectsJsonResponse?: boolean, schema?: any, agentName?: string, tools?: any[]) => Promise<any>;
|
|
6
6
|
runPromptWithModelForImageGeneration: (modelName: string, config: any, prompt: string, options?: any) => Promise<any>;
|
|
@@ -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":";wBAoaQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,qBACO,QAAQ,GAAG,IAAI,eACrB,MAAM,eACN,MAAM,KACjB,GAAG;sBAyCG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCAioBF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCApfO,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;sDAyyBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CAz2B8B,GAAG,KAAG,MAAM;;AAo6BzD,wBAOE"}
|
package/dist/esm/langChain.js
CHANGED
|
@@ -4,7 +4,7 @@ import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
|
|
|
4
4
|
import { OpenAI } from "openai";
|
|
5
5
|
import { GoogleGenAI } from "@google/genai";
|
|
6
6
|
import constants from "./const.js";
|
|
7
|
-
import
|
|
7
|
+
import langChain from "langchain";
|
|
8
8
|
import errorHandlingHelper from "./errorHandling.js";
|
|
9
9
|
import logger from "./logger.js";
|
|
10
10
|
import { z } from "zod";
|
|
@@ -259,7 +259,7 @@ const safeJsonParse = (line) => {
|
|
|
259
259
|
* report; defaults to `100`
|
|
260
260
|
* @returns A LangChain tool instance configured with a Zod schema for `{ stage, message, percent }`
|
|
261
261
|
*/
|
|
262
|
-
const getAIProgressTool = (onProgress, minPercent = 0, maxPercent = 100) => tool(async ({ stage, message, percent }) => {
|
|
262
|
+
const getAIProgressTool = (onProgress, minPercent = 0, maxPercent = 100) => langChain.tool(async ({ stage, message, percent }) => {
|
|
263
263
|
try {
|
|
264
264
|
// Clamp percent to be within minPercent and maxPercent bounds
|
|
265
265
|
const rawPct = typeof percent === "number" ? percent : 0;
|
|
@@ -382,7 +382,7 @@ const getLLMModel = (modelName, config, schema = null) => {
|
|
|
382
382
|
* defaults to `100`
|
|
383
383
|
* @returns A configured LangChain agent instance ready to be run with `runAIAgent`
|
|
384
384
|
*/
|
|
385
|
-
const
|
|
385
|
+
const createAgent = (name, modelName, systemPrompt, tools = [], responseFormat, config, onReportProgress = null, minPercent = 0, maxPercent = 100) => {
|
|
386
386
|
//Prepare the complete system prompt with progress reporting instructions if needed
|
|
387
387
|
const completeSystemPrompt = `
|
|
388
388
|
${systemPrompt}
|
|
@@ -391,7 +391,7 @@ const createAIAgent = (name, modelName, systemPrompt, tools = [], responseFormat
|
|
|
391
391
|
: ""}
|
|
392
392
|
`.trim();
|
|
393
393
|
// Create the agent with the specified model, system prompt, tools, and response format
|
|
394
|
-
const agent = createAgent({
|
|
394
|
+
const agent = langChain.createAgent({
|
|
395
395
|
name: name,
|
|
396
396
|
model: getLLMModel(modelName, config),
|
|
397
397
|
systemPrompt: completeSystemPrompt,
|
|
@@ -416,7 +416,7 @@ const createAIAgent = (name, modelName, systemPrompt, tools = [], responseFormat
|
|
|
416
416
|
* agent execution
|
|
417
417
|
* @returns The raw response object returned by the agent's `invoke` method
|
|
418
418
|
*/
|
|
419
|
-
const
|
|
419
|
+
const runAgent = async (agent, prompt, config, onProgress = null) => {
|
|
420
420
|
const startTime = Date.now();
|
|
421
421
|
// Build callbacks for progress reporting if onProgress is provided
|
|
422
422
|
// const agentDisplayName = `${agent.options?.name} AI Agent`;
|
|
@@ -587,14 +587,14 @@ const runPromptWithModel = async (modelName, config, prompt, onProgressReport, m
|
|
|
587
587
|
agentTools.push(getAIProgressTool(trackingProgressCallback, minPercent, maxPercent));
|
|
588
588
|
}
|
|
589
589
|
// Create the agent with tools
|
|
590
|
-
const agent =
|
|
590
|
+
const agent = createAgent(agentName, modelName, systemPrompt, agentTools, null, // responseFormat
|
|
591
591
|
config, null, // onReportProgress handled via tools array
|
|
592
592
|
minPercent, maxPercent);
|
|
593
593
|
// Run the agent with callback that includes current progress
|
|
594
594
|
const callbackWithProgress = onProgressReport
|
|
595
595
|
? (data) => onProgressReport({ ...data, progress: progressTracker.current })
|
|
596
596
|
: null;
|
|
597
|
-
const response = await
|
|
597
|
+
const response = await runAgent(agent, userPrompt, config, callbackWithProgress);
|
|
598
598
|
// Extract content from agent response
|
|
599
599
|
const messages = response?.messages || [];
|
|
600
600
|
if (messages.length === 0) {
|
|
@@ -1227,8 +1227,8 @@ const runPromptWithModelForImageGeneration = async (modelName, config, prompt, o
|
|
|
1227
1227
|
}
|
|
1228
1228
|
};
|
|
1229
1229
|
export default {
|
|
1230
|
-
|
|
1231
|
-
|
|
1230
|
+
createAgent,
|
|
1231
|
+
runAgent,
|
|
1232
1232
|
runChatPromptWithModel,
|
|
1233
1233
|
runPromptWithModel,
|
|
1234
1234
|
runPromptWithModelForImageGeneration,
|