@stackfactor/agent-utils 1.0.11 → 1.0.13
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 +1 -1
- package/dist/cjs/langChain.d.ts.map +1 -1
- package/dist/cjs/langChain.js +123 -410
- package/dist/esm/langChain.d.ts +1 -1
- package/dist/esm/langChain.d.ts.map +1 -1
- package/dist/esm/langChain.js +123 -410
- package/package.json +1 -1
package/dist/cjs/langChain.js
CHANGED
|
@@ -12,34 +12,7 @@ const const_js_1 = __importDefault(require("./const.js"));
|
|
|
12
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
|
-
const zod_1 = require("zod");
|
|
16
15
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
17
|
-
const NDJSON_SYSTEM_PROMPT = `
|
|
18
|
-
You are a streaming JSON generator.
|
|
19
|
-
|
|
20
|
-
You MUST output NDJSON (newline-delimited JSON).
|
|
21
|
-
That means: ONE valid JSON object per line.
|
|
22
|
-
|
|
23
|
-
Allowed event types:
|
|
24
|
-
- "progress": short status update for the user
|
|
25
|
-
- "final": final complete result
|
|
26
|
-
|
|
27
|
-
Rules:
|
|
28
|
-
- Output ONLY NDJSON lines (no markdown, no extra text).
|
|
29
|
-
- Each line must be valid JSON.
|
|
30
|
-
- "progress" must include: type, message, pct (with values between {minPercent} and {maxPercent})
|
|
31
|
-
- "final" must include: type, content
|
|
32
|
-
- End with EXACTLY ONE "final" event.
|
|
33
|
-
- Do NOT reveal hidden chain-of-thought.
|
|
34
|
-
|
|
35
|
-
CRITICAL - Escape special characters in ALL string values:
|
|
36
|
-
- Newlines must be written as \\n (two characters: backslash + n)
|
|
37
|
-
- Tabs must be written as \\t (two characters: backslash + t)
|
|
38
|
-
- Carriage returns must be written as \\r (two characters: backslash + r)
|
|
39
|
-
- Double quotes inside strings must be written as \\"
|
|
40
|
-
- Backslashes must be written as \\\\
|
|
41
|
-
This ensures valid JSON output.
|
|
42
|
-
`.trim();
|
|
43
16
|
const JSON_ESCAPE_INSTRUCTION = `
|
|
44
17
|
CRITICAL - Your response must be valid JSON. Escape ALL special characters in string values:
|
|
45
18
|
- Newlines → \\n
|
|
@@ -49,45 +22,41 @@ CRITICAL - Your response must be valid JSON. Escape ALL special characters in st
|
|
|
49
22
|
- Backslashes → \\\\
|
|
50
23
|
Do NOT include raw newlines, tabs, or unescaped quotes inside JSON string values.
|
|
51
24
|
`.trim();
|
|
52
|
-
const REPORT_PROGRESS_TOOL = `
|
|
53
|
-
Report Progress to User
|
|
54
|
-
Before each task or step, call the tool report_progress with:
|
|
55
|
-
message: brief description of work you plan to do that makes sense for individuals in Learning and Development or Business Strategy roles. Don't include the word JSON in any of the messages.
|
|
56
|
-
percent: cumulative completion percentage with values between {minPercent}% and {maxPercent}%. Don't report percentage completion outside of this range.
|
|
57
|
-
`;
|
|
58
25
|
/**
|
|
59
26
|
* Converts a Zod validation error object into a human-readable multi-line string.
|
|
60
27
|
* Each failing field is described with its dot-notation path and a contextual message
|
|
61
28
|
* that depends on the Zod error code (e.g. `invalid_type`, `too_small`, `invalid_enum_value`).
|
|
62
29
|
* The output is prefixed with `"Schema validation failed:"` followed by a bulleted list.
|
|
63
|
-
* @param zodError - The Zod `ZodError` instance whose `
|
|
30
|
+
* @param zodError - The Zod `ZodError` instance whose `issues` array will be formatted
|
|
64
31
|
* @returns A formatted string describing all validation failures
|
|
65
32
|
*/
|
|
66
33
|
const formatZodErrors = (zodError) => {
|
|
67
|
-
const errors = zodError.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
+
: [];
|
|
91
60
|
return `Schema validation failed:\n - ${errors.join("\n - ")}`;
|
|
92
61
|
};
|
|
93
62
|
/**
|
|
@@ -222,82 +191,6 @@ const extractJSONFromResponse = (text) => {
|
|
|
222
191
|
}
|
|
223
192
|
return null;
|
|
224
193
|
};
|
|
225
|
-
/**
|
|
226
|
-
* Attempts to parse a JSON string without throwing. On failure it tries a second parse
|
|
227
|
-
* after sanitising Unicode control characters and normalising Windows-style line endings
|
|
228
|
-
* to `\n`. Returns `null` if both attempts fail, making it safe to call in stream
|
|
229
|
-
* processing loops where individual NDJSON lines may be malformed.
|
|
230
|
-
* @param line - A single line of text expected to contain a JSON value
|
|
231
|
-
* @returns The parsed value, or `null` if parsing failed after both attempts
|
|
232
|
-
*/
|
|
233
|
-
const safeJsonParse = (line) => {
|
|
234
|
-
try {
|
|
235
|
-
return JSON.parse(line);
|
|
236
|
-
}
|
|
237
|
-
catch {
|
|
238
|
-
// Try with special character handling
|
|
239
|
-
try {
|
|
240
|
-
const sanitized = line
|
|
241
|
-
.replace(/[\u0000-\u001F\u007F-\u009F]/g, " ")
|
|
242
|
-
.replace(/\r\n/g, "\\n")
|
|
243
|
-
.replace(/\r/g, "\\n");
|
|
244
|
-
return JSON.parse(sanitized);
|
|
245
|
-
}
|
|
246
|
-
catch {
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
/**
|
|
252
|
-
* Creates a LangChain tool named `report_progress` that agents can call to emit
|
|
253
|
-
* progress updates during multi-step execution. When the tool is invoked by the agent,
|
|
254
|
-
* it clamps the reported percentage to the `[minPercent, maxPercent]` range, forwards
|
|
255
|
-
* the update to the `onProgress` callback, and returns a JSON acknowledgement string to
|
|
256
|
-
* the agent. If the callback throws, the tool returns a JSON error acknowledgement
|
|
257
|
-
* instead of propagating the exception.
|
|
258
|
-
* @param onProgress - Async or sync callback invoked with `{ progress, message }` on
|
|
259
|
-
* each agent progress report
|
|
260
|
-
* @param minPercent - The lower bound of the percentage range the agent is allowed to
|
|
261
|
-
* report; defaults to `0`
|
|
262
|
-
* @param maxPercent - The upper bound of the percentage range the agent is allowed to
|
|
263
|
-
* report; defaults to `100`
|
|
264
|
-
* @returns A LangChain tool instance configured with a Zod schema for `{ stage, message, percent }`
|
|
265
|
-
*/
|
|
266
|
-
const getAIProgressTool = (onProgress, minPercent = 0, maxPercent = 100) => langchain_1.default.tool(async ({ stage, message, percent }) => {
|
|
267
|
-
let clampedPct = 0;
|
|
268
|
-
try {
|
|
269
|
-
// Coerce percent to number and clamp to be within minPercent and maxPercent bounds
|
|
270
|
-
const rawPct = typeof percent === "number" ? percent : parseFloat(percent) || 0;
|
|
271
|
-
clampedPct = Math.max(minPercent, Math.min(maxPercent, rawPct));
|
|
272
|
-
// NOTE: Monotonicity is not enforced here; implement if needed per session/context.
|
|
273
|
-
if (onProgress &&
|
|
274
|
-
minPercent <= clampedPct &&
|
|
275
|
-
clampedPct <= maxPercent) {
|
|
276
|
-
await onProgress({
|
|
277
|
-
progress: clampedPct,
|
|
278
|
-
message: message,
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
finally {
|
|
283
|
-
return JSON.stringify({
|
|
284
|
-
acknowledged: true,
|
|
285
|
-
receivedAt: Date.now(),
|
|
286
|
-
stage,
|
|
287
|
-
percent: clampedPct,
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
}, {
|
|
291
|
-
name: "report_progress",
|
|
292
|
-
description: `Mandatory after each phase: call with {stage, message, percent}. Percent must be between ${minPercent} and ${maxPercent} and monotonically increase.`,
|
|
293
|
-
schema: zod_1.z.object({
|
|
294
|
-
stage: zod_1.z.string().describe("Current stage of the process"),
|
|
295
|
-
message: zod_1.z.string().describe("Progress message to display"),
|
|
296
|
-
percent: zod_1.z
|
|
297
|
-
.number()
|
|
298
|
-
.describe(`Progress percentage (${minPercent}-${maxPercent})`),
|
|
299
|
-
}),
|
|
300
|
-
});
|
|
301
194
|
/**
|
|
302
195
|
* Instantiates and returns the appropriate LangChain chat model based on the model
|
|
303
196
|
* name prefix. `claude-` maps to `ChatAnthropic`, `gemini-` maps to
|
|
@@ -363,11 +256,7 @@ const getLLMModel = (modelName, config, schema = null) => {
|
|
|
363
256
|
};
|
|
364
257
|
/**
|
|
365
258
|
* Constructs a LangChain agent configured with a specified model, system prompt, and
|
|
366
|
-
* set of tools.
|
|
367
|
-
* reporting instruction block is appended to the system prompt and a `report_progress`
|
|
368
|
-
* tool is added to the tools array so the agent can emit incremental progress updates
|
|
369
|
-
* during execution. The percentage range for progress reporting is bounded by
|
|
370
|
-
* `minPercent` and `maxPercent`.
|
|
259
|
+
* set of tools.
|
|
371
260
|
* @param name - A human-readable display name for the agent
|
|
372
261
|
* @param modelName - The LLM identifier passed to `getLLMModel` (e.g. `"gpt-4o"`)
|
|
373
262
|
* @param systemPrompt - The base system prompt describing the agent's role and behaviour
|
|
@@ -376,30 +265,14 @@ const getLLMModel = (modelName, config, schema = null) => {
|
|
|
376
265
|
* @param responseFormat - Optional structured response format descriptor passed to the
|
|
377
266
|
* LangChain agent constructor
|
|
378
267
|
* @param config - Configuration object forwarded to `getLLMModel` (API keys, temperature, etc.)
|
|
379
|
-
* @
|
|
380
|
-
* progress tool is added and the system prompt is extended
|
|
381
|
-
* @param minPercent - Minimum progress percentage the agent is allowed to report;
|
|
382
|
-
* defaults to `0`
|
|
383
|
-
* @param maxPercent - Maximum progress percentage the agent is allowed to report;
|
|
384
|
-
* defaults to `100`
|
|
385
|
-
* @returns A configured LangChain agent instance ready to be run with `runAIAgent`
|
|
268
|
+
* @returns A configured LangChain agent instance ready to be run with `runAgent`
|
|
386
269
|
*/
|
|
387
|
-
const createAgent = (name, modelName, systemPrompt, tools = [], responseFormat, config
|
|
388
|
-
//Prepare the complete system prompt with progress reporting instructions if needed
|
|
389
|
-
const completeSystemPrompt = `
|
|
390
|
-
${systemPrompt}
|
|
391
|
-
${onReportProgress
|
|
392
|
-
? REPORT_PROGRESS_TOOL.replace(`{minPercent}`, minPercent).replace(`{maxPercent}`, maxPercent)
|
|
393
|
-
: ""}
|
|
394
|
-
`.trim();
|
|
395
|
-
// Create the agent with the specified model, system prompt, tools, and response format
|
|
270
|
+
const createAgent = (name, modelName, systemPrompt, tools = [], responseFormat, config) => {
|
|
396
271
|
const agent = langchain_1.default.createAgent({
|
|
397
272
|
name: name,
|
|
398
273
|
model: getLLMModel(modelName, config),
|
|
399
|
-
systemPrompt:
|
|
400
|
-
tools
|
|
401
|
-
? [...tools, getAIProgressTool(onReportProgress, minPercent, maxPercent)]
|
|
402
|
-
: tools,
|
|
274
|
+
systemPrompt: systemPrompt.trim(),
|
|
275
|
+
tools,
|
|
403
276
|
...(responseFormat ? { responseFormat: responseFormat } : {}),
|
|
404
277
|
});
|
|
405
278
|
return agent;
|
|
@@ -407,10 +280,9 @@ const createAgent = (name, modelName, systemPrompt, tools = [], responseFormat,
|
|
|
407
280
|
/**
|
|
408
281
|
* Executes a LangChain agent with a single user prompt and returns the raw agent
|
|
409
282
|
* response. When an `onProgress` callback is provided, LangChain callbacks are
|
|
410
|
-
* registered to forward `tool_start`, `
|
|
411
|
-
* from the `report_progress` tool), `agent_action`, and error events to the caller.
|
|
283
|
+
* registered to forward `tool_start`, `agent_action`, and error events to the caller.
|
|
412
284
|
* Execution time is logged at the info level on completion.
|
|
413
|
-
* @param agent - A LangChain agent instance created by `
|
|
285
|
+
* @param agent - A LangChain agent instance created by `createAgent`
|
|
414
286
|
* @param prompt - The user message string to send to the agent
|
|
415
287
|
* @param config - Configuration object; `config.recursionLimit` controls the maximum
|
|
416
288
|
* number of agent steps (defaults to `25` when not specified)
|
|
@@ -420,8 +292,6 @@ const createAgent = (name, modelName, systemPrompt, tools = [], responseFormat,
|
|
|
420
292
|
*/
|
|
421
293
|
const runAgent = async (agent, prompt, config, onProgress = null) => {
|
|
422
294
|
const startTime = Date.now();
|
|
423
|
-
// Build callbacks for progress reporting if onProgress is provided
|
|
424
|
-
// const agentDisplayName = `${agent.options?.name} AI Agent`;
|
|
425
295
|
const callbacks = onProgress
|
|
426
296
|
? [
|
|
427
297
|
{
|
|
@@ -433,24 +303,6 @@ const runAgent = async (agent, prompt, config, onProgress = null) => {
|
|
|
433
303
|
tool: toolInfo.name,
|
|
434
304
|
});
|
|
435
305
|
},
|
|
436
|
-
handleToolEnd: (output) => {
|
|
437
|
-
try {
|
|
438
|
-
const progressInfo = extractJSONFromResponse(output.content);
|
|
439
|
-
if (progressInfo &&
|
|
440
|
-
typeof progressInfo === "object" &&
|
|
441
|
-
progressInfo.percent &&
|
|
442
|
-
progressInfo.message) {
|
|
443
|
-
onProgress({
|
|
444
|
-
progress: progressInfo.percent,
|
|
445
|
-
message: progressInfo.message,
|
|
446
|
-
output: typeof output === "string"
|
|
447
|
-
? output
|
|
448
|
-
: JSON.stringify(output),
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
catch { }
|
|
453
|
-
},
|
|
454
306
|
handleAgentAction: (action) => {
|
|
455
307
|
onProgress({
|
|
456
308
|
type: "agent_action",
|
|
@@ -459,18 +311,6 @@ const runAgent = async (agent, prompt, config, onProgress = null) => {
|
|
|
459
311
|
input: action.toolInput,
|
|
460
312
|
});
|
|
461
313
|
},
|
|
462
|
-
// handleLLMStart: () => {
|
|
463
|
-
// onProgress({
|
|
464
|
-
// type: "llm_start",
|
|
465
|
-
// message: `${agentDisplayName} is thinking`,
|
|
466
|
-
// });
|
|
467
|
-
// },
|
|
468
|
-
// handleLLMEnd: () => {
|
|
469
|
-
// onProgress({
|
|
470
|
-
// type: "llm_end",
|
|
471
|
-
// message: `${agentDisplayName} has completed processing`,
|
|
472
|
-
// });
|
|
473
|
-
// },
|
|
474
314
|
handleChainError: (err) => {
|
|
475
315
|
onProgress({
|
|
476
316
|
type: "error",
|
|
@@ -574,26 +414,11 @@ const runPromptWithModel = async (modelName, config, prompt, onProgressReport, m
|
|
|
574
414
|
? `${JSON_ESCAPE_INSTRUCTION}\n\n---\n\n${systemPrompt}`
|
|
575
415
|
: JSON_ESCAPE_INSTRUCTION;
|
|
576
416
|
}
|
|
577
|
-
//
|
|
578
|
-
const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
trackingProgressCallback = (data) => {
|
|
583
|
-
if (typeof data.progress === "number") {
|
|
584
|
-
progressTracker.current = data.progress;
|
|
585
|
-
}
|
|
586
|
-
onProgressReport(data);
|
|
587
|
-
};
|
|
588
|
-
}
|
|
589
|
-
// Create the agent with tools and progress callback
|
|
590
|
-
const agent = createAgent(agentName, modelName, systemPrompt, agentTools, null, // responseFormat
|
|
591
|
-
config, trackingProgressCallback, minPercent, maxPercent);
|
|
592
|
-
// Run the agent with callback that includes current progress
|
|
593
|
-
const callbackWithProgress = onProgressReport
|
|
594
|
-
? (data) => onProgressReport({ ...data, progress: progressTracker.current })
|
|
595
|
-
: null;
|
|
596
|
-
const response = await runAgent(agent, userPrompt, config, callbackWithProgress);
|
|
417
|
+
// Create the agent with tools
|
|
418
|
+
const agent = createAgent(agentName, modelName, systemPrompt, [...tools], null, // responseFormat
|
|
419
|
+
config);
|
|
420
|
+
// Run the agent with progress callback
|
|
421
|
+
const response = await runAgent(agent, userPrompt, config, onProgressReport || null);
|
|
597
422
|
// Extract content from agent response
|
|
598
423
|
const messages = response?.messages || [];
|
|
599
424
|
if (messages.length === 0) {
|
|
@@ -666,224 +491,112 @@ const runPromptWithModel = async (modelName, config, prompt, onProgressReport, m
|
|
|
666
491
|
throw new Error("Prompt must be a string or array of messages");
|
|
667
492
|
}
|
|
668
493
|
if (onProgressReport) {
|
|
669
|
-
//
|
|
670
|
-
const
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
494
|
+
// Streaming mode: use server-side chunk-based progress for all models
|
|
495
|
+
const useNativeSchema = expectsJsonResponse && schema && modelName.startsWith("gpt-");
|
|
496
|
+
const llm = getLLMModel(modelName, config, useNativeSchema ? schema : null);
|
|
497
|
+
// Build messages with JSON instructions if needed
|
|
498
|
+
let messagesToSend;
|
|
499
|
+
if (expectsJsonResponse) {
|
|
500
|
+
let systemContent = useNativeSchema
|
|
501
|
+
? "Respond with valid JSON."
|
|
502
|
+
: JSON_ESCAPE_INSTRUCTION;
|
|
503
|
+
if (schema && !useNativeSchema) {
|
|
504
|
+
const jsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(schema, { target: "openApi3" });
|
|
505
|
+
systemContent += `\n\nYour response MUST conform to this JSON schema:\n${JSON.stringify(jsonSchema, null, 2)}`;
|
|
506
|
+
}
|
|
507
|
+
if (messages.length > 0 && messages[0].role === "system") {
|
|
508
|
+
messagesToSend = [
|
|
509
|
+
{
|
|
510
|
+
role: "system",
|
|
511
|
+
content: `${systemContent}\n\n---\n\n${messages[0].content}`,
|
|
512
|
+
},
|
|
513
|
+
...messages.slice(1),
|
|
679
514
|
];
|
|
680
|
-
let rawContent = "";
|
|
681
|
-
let lastProgressReport = 0;
|
|
682
|
-
const progressInterval = 1000; // Report progress every 1000 characters
|
|
683
|
-
const stream = await llm.stream(messagesToSend);
|
|
684
|
-
for await (const chunk of stream) {
|
|
685
|
-
const content = chunk?.content || chunk;
|
|
686
|
-
if (typeof content === "string") {
|
|
687
|
-
rawContent += content;
|
|
688
|
-
// Report progress based on content length
|
|
689
|
-
if (rawContent.length - lastProgressReport >= progressInterval) {
|
|
690
|
-
lastProgressReport = rawContent.length;
|
|
691
|
-
const progress = Math.min(maxPercent - 5, // Reserve last 5% for completion
|
|
692
|
-
minPercent +
|
|
693
|
-
Math.floor((rawContent.length / 5000) * (maxPercent - minPercent)));
|
|
694
|
-
await onProgressReport({
|
|
695
|
-
message: "Generating content...",
|
|
696
|
-
progress: progress,
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
515
|
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
// Parse and validate the response
|
|
707
|
-
if (rawContent) {
|
|
708
|
-
try {
|
|
709
|
-
const parsed = JSON.parse(rawContent.trim());
|
|
710
|
-
if (schema) {
|
|
711
|
-
validateWithSchema(parsed, schema);
|
|
712
|
-
}
|
|
713
|
-
return JSON.stringify(parsed);
|
|
714
|
-
}
|
|
715
|
-
catch (parseError) {
|
|
716
|
-
if (parseError?.code === const_js_1.default.HTTP_CODES.UNPROCESSABLE_ENTITY) {
|
|
717
|
-
throw parseError;
|
|
718
|
-
}
|
|
719
|
-
// Try to extract JSON
|
|
720
|
-
const extracted = extractJSONFromResponse(rawContent);
|
|
721
|
-
if (extracted) {
|
|
722
|
-
if (schema) {
|
|
723
|
-
validateWithSchema(extracted, schema);
|
|
724
|
-
}
|
|
725
|
-
return JSON.stringify(extracted);
|
|
726
|
-
}
|
|
727
|
-
if (expectsJsonResponse) {
|
|
728
|
-
let preview = "";
|
|
729
|
-
if (typeof rawContent === "string") {
|
|
730
|
-
preview = rawContent.substring(0, 100);
|
|
731
|
-
}
|
|
732
|
-
else if (typeof rawContent === "object" && rawContent !== null) {
|
|
733
|
-
preview = JSON.stringify(rawContent).substring(0, 100);
|
|
734
|
-
}
|
|
735
|
-
else if (rawContent !== undefined && rawContent !== null) {
|
|
736
|
-
preview = String(rawContent).substring(0, 100);
|
|
737
|
-
}
|
|
738
|
-
else {
|
|
739
|
-
preview = "[empty response]";
|
|
740
|
-
}
|
|
741
|
-
throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR, `LLM returned non-JSON response when JSON was expected: ${preview}...`);
|
|
742
|
-
}
|
|
743
|
-
return rawContent;
|
|
744
|
-
}
|
|
516
|
+
else {
|
|
517
|
+
messagesToSend = [
|
|
518
|
+
{ role: "system", content: systemContent },
|
|
519
|
+
...messages,
|
|
520
|
+
];
|
|
745
521
|
}
|
|
746
|
-
throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR, "LLM returned no content");
|
|
747
|
-
}
|
|
748
|
-
// Non-OpenAI or no schema: use NDJSON streaming approach
|
|
749
|
-
const llm = getLLMModel(modelName, config);
|
|
750
|
-
let ndjsonSystemContent = NDJSON_SYSTEM_PROMPT.replace(`{minPercent}`, minPercent).replace(`{maxPercent}`, maxPercent);
|
|
751
|
-
// If schema is provided, include it in the system prompt
|
|
752
|
-
if (expectsJsonResponse && schema) {
|
|
753
|
-
const jsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(schema, { target: "openApi3" });
|
|
754
|
-
ndjsonSystemContent += `\n\nThe "content" field in the "final" event MUST conform to this JSON schema:\n${JSON.stringify(jsonSchema, null, 2)}`;
|
|
755
|
-
}
|
|
756
|
-
let messagesWithNDJSON;
|
|
757
|
-
if (messages.length > 0 && messages[0].role === "system") {
|
|
758
|
-
messagesWithNDJSON = [
|
|
759
|
-
{
|
|
760
|
-
role: "system",
|
|
761
|
-
content: `${ndjsonSystemContent}\n\n---\n\n${messages[0].content}`,
|
|
762
|
-
},
|
|
763
|
-
...messages.slice(1),
|
|
764
|
-
];
|
|
765
522
|
}
|
|
766
523
|
else {
|
|
767
|
-
|
|
768
|
-
{
|
|
769
|
-
role: "system",
|
|
770
|
-
content: ndjsonSystemContent,
|
|
771
|
-
},
|
|
772
|
-
...messages,
|
|
773
|
-
];
|
|
524
|
+
messagesToSend = messages;
|
|
774
525
|
}
|
|
775
|
-
// Stream
|
|
776
|
-
let buffer = "";
|
|
777
|
-
let finalContent = "";
|
|
526
|
+
// Stream and report server-side progress based on time elapsed
|
|
778
527
|
let rawContent = "";
|
|
779
|
-
|
|
528
|
+
let chunkCount = 0;
|
|
529
|
+
const progressReportInterval = 10; // Report every N chunks
|
|
530
|
+
const startTime = Date.now();
|
|
531
|
+
// Use a time-based asymptotic curve: progress approaches maxPercent but never
|
|
532
|
+
// overshoots. This avoids the magic "expected length" constant — longer responses
|
|
533
|
+
// simply slow the curve down rather than exceeding the range.
|
|
534
|
+
const expectedDurationMs = 15_000; // Tune: expected typical response time
|
|
535
|
+
const stream = await llm.stream(messagesToSend);
|
|
780
536
|
for await (const chunk of stream) {
|
|
781
537
|
const content = chunk?.content || chunk;
|
|
782
538
|
if (typeof content === "string") {
|
|
783
|
-
buffer += content;
|
|
784
539
|
rawContent += content;
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
message: parsed.message,
|
|
797
|
-
progress: clampedProgress,
|
|
798
|
-
});
|
|
799
|
-
}
|
|
800
|
-
else if (parsed.type === "final") {
|
|
801
|
-
if (parsed.content) {
|
|
802
|
-
finalContent = parsed.content;
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
}
|
|
540
|
+
chunkCount++;
|
|
541
|
+
if (chunkCount % progressReportInterval === 0) {
|
|
542
|
+
const elapsed = Date.now() - startTime;
|
|
543
|
+
// Asymptotic curve: fast early progress that slows as it approaches max
|
|
544
|
+
const progress = Math.round(minPercent +
|
|
545
|
+
(maxPercent - minPercent - 5) *
|
|
546
|
+
(1 - Math.exp(-elapsed / expectedDurationMs)));
|
|
547
|
+
await onProgressReport({
|
|
548
|
+
message: "Generating content...",
|
|
549
|
+
progress: Math.min(progress, maxPercent - 5),
|
|
550
|
+
});
|
|
806
551
|
}
|
|
807
552
|
}
|
|
808
553
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
554
|
+
await onProgressReport({
|
|
555
|
+
message: "Processing complete",
|
|
556
|
+
progress: maxPercent,
|
|
557
|
+
});
|
|
558
|
+
if (!rawContent) {
|
|
559
|
+
throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR, "LLM returned no content");
|
|
814
560
|
}
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
validateWithSchema(finalContent, schema);
|
|
819
|
-
}
|
|
820
|
-
return JSON.stringify(finalContent);
|
|
821
|
-
}
|
|
822
|
-
if (typeof finalContent === "string") {
|
|
823
|
-
try {
|
|
824
|
-
const parsed = JSON.parse(finalContent);
|
|
825
|
-
if (expectsJsonResponse && schema) {
|
|
826
|
-
validateWithSchema(parsed, schema);
|
|
827
|
-
}
|
|
828
|
-
return JSON.stringify(parsed);
|
|
829
|
-
}
|
|
830
|
-
catch (parseError) {
|
|
831
|
-
if (parseError?.code === const_js_1.default.HTTP_CODES.UNPROCESSABLE_ENTITY) {
|
|
832
|
-
throw parseError;
|
|
833
|
-
}
|
|
834
|
-
return finalContent;
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
return finalContent;
|
|
561
|
+
// If not expecting JSON, return raw content directly
|
|
562
|
+
if (!expectsJsonResponse) {
|
|
563
|
+
return rawContent;
|
|
838
564
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
if (extractedContent) {
|
|
850
|
-
try {
|
|
851
|
-
const parsed = JSON.parse(extractedContent);
|
|
852
|
-
if (expectsJsonResponse && schema) {
|
|
853
|
-
validateWithSchema(parsed, schema);
|
|
854
|
-
}
|
|
855
|
-
return JSON.stringify(parsed);
|
|
856
|
-
}
|
|
857
|
-
catch {
|
|
858
|
-
return extractedContent;
|
|
565
|
+
// Parse and validate JSON response
|
|
566
|
+
const trimmed = rawContent.trim();
|
|
567
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
568
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
569
|
+
try {
|
|
570
|
+
const parsed = JSON.parse(trimmed);
|
|
571
|
+
if (schema) {
|
|
572
|
+
validateWithSchema(parsed, schema);
|
|
859
573
|
}
|
|
574
|
+
return JSON.stringify(parsed);
|
|
860
575
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
validateWithSchema(extracted, schema);
|
|
576
|
+
catch (parseError) {
|
|
577
|
+
if (parseError?.code === const_js_1.default.HTTP_CODES.UNPROCESSABLE_ENTITY) {
|
|
578
|
+
throw parseError;
|
|
865
579
|
}
|
|
866
|
-
return JSON.stringify(extracted);
|
|
867
580
|
}
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
else if (typeof rawContent === "object" && rawContent !== null) {
|
|
874
|
-
preview = JSON.stringify(rawContent).substring(0, 100);
|
|
875
|
-
}
|
|
876
|
-
else if (rawContent !== undefined && rawContent !== null) {
|
|
877
|
-
preview = String(rawContent).substring(0, 100);
|
|
878
|
-
}
|
|
879
|
-
else {
|
|
880
|
-
preview = "[empty response]";
|
|
881
|
-
}
|
|
882
|
-
throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR, `LLM returned non-JSON response when JSON was expected: ${preview}...`);
|
|
581
|
+
}
|
|
582
|
+
const extracted = extractJSONFromResponse(rawContent);
|
|
583
|
+
if (extracted) {
|
|
584
|
+
if (schema) {
|
|
585
|
+
validateWithSchema(extracted, schema);
|
|
883
586
|
}
|
|
884
|
-
return
|
|
587
|
+
return JSON.stringify(extracted);
|
|
588
|
+
}
|
|
589
|
+
let preview = "";
|
|
590
|
+
if (typeof rawContent === "string") {
|
|
591
|
+
preview = rawContent.substring(0, 100);
|
|
592
|
+
}
|
|
593
|
+
else if (typeof rawContent === "object" && rawContent !== null) {
|
|
594
|
+
preview = JSON.stringify(rawContent).substring(0, 100);
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
preview = "[empty response]";
|
|
885
598
|
}
|
|
886
|
-
throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR,
|
|
599
|
+
throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR, `LLM returned non-JSON response when JSON was expected: ${preview}...`);
|
|
887
600
|
}
|
|
888
601
|
else {
|
|
889
602
|
// Non-streaming mode: use native response_format for OpenAI when schema is provided
|
package/dist/esm/langChain.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
|
-
createAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any
|
|
2
|
+
createAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any) => any;
|
|
3
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>;
|