@stackfactor/agent-utils 1.0.0 → 1.0.2
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 +302 -0
- 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
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# @stackfactor/agent-utils
|
|
2
|
+
|
|
3
|
+
Shared utilities for StackFactor AI agent services — LangChain helpers, structured logging, error handling, and constants.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @stackfactor/agent-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Modules
|
|
12
|
+
|
|
13
|
+
The package exports four modules:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import {
|
|
17
|
+
langChain,
|
|
18
|
+
logger,
|
|
19
|
+
errorHandling,
|
|
20
|
+
constants,
|
|
21
|
+
} from "@stackfactor/agent-utils";
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## `langChain`
|
|
27
|
+
|
|
28
|
+
Unified interface for running LLM prompts, managing LangChain agents, and generating images across OpenAI, Anthropic, and Google providers.
|
|
29
|
+
|
|
30
|
+
### `langChain.runPromptWithModel(modelName, config, prompt, onProgressReport?, minPercent?, maxPercent?, expectsJsonResponse?, schema?, agentName?, tools?)`
|
|
31
|
+
|
|
32
|
+
Sends a prompt to an LLM and returns the response. Supports three execution modes:
|
|
33
|
+
|
|
34
|
+
- **Agentic** (`config.agentic === true`) — creates and runs a LangChain agent with tools.
|
|
35
|
+
- **Streaming** (`onProgressReport` provided) — streams the response with progress callbacks. Uses native `response_format` for OpenAI+schema, or NDJSON for other providers.
|
|
36
|
+
- **Non-streaming** — direct invocation with JSON extraction.
|
|
37
|
+
|
|
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.
|
|
39
|
+
|
|
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) |
|
|
52
|
+
|
|
53
|
+
**Returns:** JSON string (when `expectsJsonResponse` is `true`) or raw content string.
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
const result = await langChain.runPromptWithModel(
|
|
57
|
+
"gpt-4o",
|
|
58
|
+
{ openAIAPIKey: "sk-..." },
|
|
59
|
+
"Generate a summary of this document.",
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
### `langChain.runChatPromptWithModel(modelName, config, prompt, onProgressReport?)`
|
|
66
|
+
|
|
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.
|
|
68
|
+
|
|
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 |
|
|
75
|
+
|
|
76
|
+
**Returns:** Raw HTML string.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const html = await langChain.runChatPromptWithModel(
|
|
80
|
+
"claude-3-5-sonnet",
|
|
81
|
+
{ anthropicAPIKey: "sk-ant-..." },
|
|
82
|
+
[
|
|
83
|
+
{ role: "system", content: "Topic: JavaScript closures" },
|
|
84
|
+
{ role: "user", content: "Explain closures with an example" },
|
|
85
|
+
],
|
|
86
|
+
);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
### `langChain.createAIAgent(name, modelName, systemPrompt, tools?, responseFormat?, config, onReportProgress?, minPercent?, maxPercent?)`
|
|
92
|
+
|
|
93
|
+
Constructs a LangChain agent with a model, system prompt, and tools. When `onReportProgress` is provided, a `report_progress` tool is automatically added.
|
|
94
|
+
|
|
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 |
|
|
106
|
+
|
|
107
|
+
**Returns:** A configured LangChain agent instance.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
### `langChain.runAIAgent(agent, prompt, config, onProgress?)`
|
|
112
|
+
|
|
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.
|
|
114
|
+
|
|
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 |
|
|
121
|
+
|
|
122
|
+
**Returns:** Raw response from the agent's `invoke` method.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
const agent = langChain.createAIAgent(
|
|
126
|
+
"Summarizer",
|
|
127
|
+
"gpt-4o",
|
|
128
|
+
"You summarize documents concisely.",
|
|
129
|
+
[],
|
|
130
|
+
null,
|
|
131
|
+
{ openAIAPIKey: "sk-..." },
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const response = await langChain.runAIAgent(
|
|
135
|
+
agent,
|
|
136
|
+
"Summarize this text...",
|
|
137
|
+
{},
|
|
138
|
+
);
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
### `langChain.runPromptWithModelForImageGeneration(modelName, config, prompt, options?)`
|
|
144
|
+
|
|
145
|
+
Generates an image using OpenAI or Google AI models.
|
|
146
|
+
|
|
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) |
|
|
153
|
+
|
|
154
|
+
**OpenAI options:** `size` (`"1024x1024"`, `"1792x1024"`, etc.), `style` (`"vivid"` or `"natural"`), `responseFormat` (`"url"` or `"b64_json"`), `n` (number of images).
|
|
155
|
+
|
|
156
|
+
**Google options:** `aspectRatio` (`"1:1"`, `"3:4"`, `"4:3"`, `"9:16"`, `"16:9"`), `numberOfImages`, `negativePrompt`.
|
|
157
|
+
|
|
158
|
+
**Returns:** `{ url?, b64_json?, revisedPrompt? }` for single images, or `{ images: [...] }` for multiple.
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const image = await langChain.runPromptWithModelForImageGeneration(
|
|
162
|
+
"dall-e-3",
|
|
163
|
+
{ openAIAPIKey: "sk-..." },
|
|
164
|
+
"A futuristic city skyline at sunset",
|
|
165
|
+
{ size: "1792x1024" },
|
|
166
|
+
);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
### `langChain.throwErrorIfNotSuccessful(response)`
|
|
172
|
+
|
|
173
|
+
Guards that a response is a non-empty string. Throws an `INTERNAL_SERVER_ERROR` if not.
|
|
174
|
+
|
|
175
|
+
| Parameter | Type | Description |
|
|
176
|
+
| ---------- | ----- | ----------------- |
|
|
177
|
+
| `response` | `any` | Value to validate |
|
|
178
|
+
|
|
179
|
+
**Returns:** The response string if valid.
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## `logger`
|
|
184
|
+
|
|
185
|
+
GCP-compatible structured logging via Winston with OpenTelemetry trace enrichment.
|
|
186
|
+
|
|
187
|
+
### `logger.log(request, level, message, options?)`
|
|
188
|
+
|
|
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.
|
|
190
|
+
|
|
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.) |
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
logger.log(req, logger.levels.info, "User signed in", { service: "auth" });
|
|
200
|
+
logger.log(null, logger.levels.error, "Connection failed");
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `logger.levels`
|
|
204
|
+
|
|
205
|
+
Enum-like object mapping level names to their string values:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
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"
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## `errorHandling`
|
|
220
|
+
|
|
221
|
+
Factory for consistently shaped error payloads.
|
|
222
|
+
|
|
223
|
+
### `errorHandling.create(errorCode, errorMessage, details?)`
|
|
224
|
+
|
|
225
|
+
Creates a structured error object with a stack trace.
|
|
226
|
+
|
|
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 |
|
|
232
|
+
|
|
233
|
+
**Returns:** `{ code, message, details, stack }`
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
throw errorHandling.create(404, "User not found");
|
|
237
|
+
throw errorHandling.create(400, "Validation failed", { field: "email" });
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## `constants`
|
|
243
|
+
|
|
244
|
+
Shared constants used across the package.
|
|
245
|
+
|
|
246
|
+
### `constants.HTTP_CODES`
|
|
247
|
+
|
|
248
|
+
| Constant | Value |
|
|
249
|
+
| ----------------------- | ----- |
|
|
250
|
+
| `BAD_REQUEST` | `400` |
|
|
251
|
+
| `UNPROCESSABLE_ENTITY` | `422` |
|
|
252
|
+
| `INTERNAL_SERVER_ERROR` | `500` |
|
|
253
|
+
| `BAD_GATEWAY` | `502` |
|
|
254
|
+
|
|
255
|
+
### `constants.ERROR`
|
|
256
|
+
|
|
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"` |
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
## Supported Models
|
|
266
|
+
|
|
267
|
+
### Text / Chat
|
|
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` |
|
|
274
|
+
|
|
275
|
+
### Image Generation
|
|
276
|
+
|
|
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` |
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
## Config Object
|
|
285
|
+
|
|
286
|
+
The `config` object accepted by LangChain methods supports the following keys:
|
|
287
|
+
|
|
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`) |
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## License
|
|
301
|
+
|
|
302
|
+
Not licensed — proprietary software of StackFactor Inc.
|
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,
|