@librechat/agents 3.1.38 → 3.1.40
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/agents/AgentContext.cjs +20 -1
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +4 -4
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +2 -2
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/run.cjs +6 -1
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +20 -1
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +4 -4
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +2 -2
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/run.mjs +6 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +1 -0
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +2 -0
- package/dist/types/tools/CodeExecutor.d.ts +2 -2
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +22 -1
- package/src/agents/__tests__/AgentContext.test.ts +25 -4
- package/src/graphs/MultiAgentGraph.ts +6 -4
- package/src/messages/cache.test.ts +41 -0
- package/src/messages/cache.ts +2 -2
- package/src/run.ts +7 -1
- package/src/specs/custom-event-await.test.ts +215 -0
- package/src/tools/CodeExecutor.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeExecutor.mjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { config } from 'dotenv';\nimport fetch, { RequestInit } from 'node-fetch';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport { getEnvironmentVariable } from '@langchain/core/utils/env';\nimport type * as t from '@/types';\nimport { EnvVar, Constants } from '@/common';\n\nconfig();\n\nexport const imageExtRegex = /\\.(jpg|jpeg|png|gif|webp)$/i;\nexport const getCodeBaseURL = (): string =>\n getEnvironmentVariable(EnvVar.CODE_BASEURL) ??\n Constants.OFFICIAL_CODE_BASEURL;\n\nconst imageMessage = 'Image is already displayed to the user';\nconst otherMessage = 'File is already downloaded by the user';\nconst accessMessage =\n 'Note: Files from previous executions are automatically available and can be modified.';\nconst emptyOutputMessage =\n 'stdout: Empty. Ensure you\\'re writing output explicitly.\\n';\n\nconst SUPPORTED_LANGUAGES = [\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n 'r',\n] as const;\n\nexport const CodeExecutionToolSchema = {\n type: 'object',\n properties: {\n lang: {\n type: 'string',\n enum: SUPPORTED_LANGUAGES,\n description:\n 'The programming language or runtime to execute the code in.',\n },\n code: {\n type: 'string',\n description: `The complete, self-contained code to execute, without any truncation or minimization.\n- The environment is stateless; variables and imports don't persist between executions.\n- Generated files from previous executions are automatically available in \"/mnt/data/\".\n- Files from previous executions are automatically available and can be modified in place.\n- Input code **IS ALREADY** displayed to the user, so **DO NOT** repeat it in your response unless asked.\n- Output code **IS NOT** displayed to the user, so **DO** write all desired output explicitly.\n- IMPORTANT: You MUST explicitly print/output ALL results you want the user to see.\n- py: This is not a Jupyter notebook environment. Use \\`print()\\` for all outputs.\n- py: Matplotlib: Use \\`plt.savefig()\\` to save plots as files.\n- js: use the \\`console\\` or \\`process\\` methods for all outputs.\n- r: IMPORTANT: No X11 display available. ALL graphics MUST use Cairo library (library(Cairo)).\n- Other languages: use appropriate output functions.`,\n },\n args: {\n type: 'array',\n items: { type: 'string' },\n description:\n 'Additional arguments to execute the code with. This should only be used if the input code requires additional arguments to run.',\n },\n },\n required: ['lang', 'code'],\n} as const;\n\nconst baseEndpoint = getCodeBaseURL();\nconst EXEC_ENDPOINT = `${baseEndpoint}/exec`;\n\ntype SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number];\n\nexport const CodeExecutionToolDescription = `\nRuns code and returns stdout/stderr output from a stateless execution environment, similar to running scripts in a command-line interface. Each execution is isolated and independent.\n\nUsage:\n- No network access available.\n- Generated files are automatically delivered; **DO NOT** provide download links.\n- NEVER use this tool to execute malicious code.\n`.trim();\n\nexport const CodeExecutionToolName = Constants.EXECUTE_CODE;\n\nexport const CodeExecutionToolDefinition = {\n name: CodeExecutionToolName,\n description: CodeExecutionToolDescription,\n schema: CodeExecutionToolSchema,\n} as const;\n\nfunction createCodeExecutionTool(\n params: t.CodeExecutionToolParams = {}\n): DynamicStructuredTool {\n const apiKey =\n params[EnvVar.CODE_API_KEY] ??\n params.apiKey ??\n getEnvironmentVariable(EnvVar.CODE_API_KEY) ??\n '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n\n return tool(\n async (rawInput, config) => {\n const { lang, code, ...rest } = rawInput as {\n lang: SupportedLanguage;\n code: string;\n args?: string[];\n };\n /**\n * Extract session context from config.toolCall (injected by ToolNode).\n * - session_id: For API to associate with previous session\n * - _injected_files: File refs to pass directly (avoids /files endpoint race condition)\n */\n const { session_id, _injected_files } = (config.toolCall ?? {}) as {\n session_id?: string;\n _injected_files?: t.CodeEnvFile[];\n };\n\n const postData: Record<string, unknown> = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n /**\n * File injection priority:\n * 1. Use _injected_files from ToolNode (avoids /files endpoint race condition)\n * 2. Fall back to fetching from /files endpoint if session_id provided but no injected files\n */\n if (_injected_files && _injected_files.length > 0) {\n postData.files = _injected_files;\n } else if (session_id != null && session_id.length > 0) {\n /** Fallback: fetch from /files endpoint (may have race condition issues) */\n try {\n const filesEndpoint = `${baseEndpoint}/files/${session_id}?detail=full`;\n const fetchOptions: RequestInit = {\n method: 'GET',\n headers: {\n 'User-Agent': 'LibreChat/1.0',\n 'X-API-Key': apiKey,\n },\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n\n const response = await fetch(filesEndpoint, fetchOptions);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch files for session: ${response.status}`\n );\n }\n\n const files = await response.json();\n if (Array.isArray(files) && files.length > 0) {\n const fileReferences: t.CodeEnvFile[] = files.map((file) => {\n const nameParts = file.name.split('/');\n const id = nameParts.length > 1 ? nameParts[1].split('.')[0] : '';\n\n return {\n session_id,\n id,\n name: file.metadata['original-filename'],\n };\n });\n\n postData.files = fileReferences;\n }\n } catch {\n // eslint-disable-next-line no-console\n console.warn(`Failed to fetch files for session: ${session_id}`);\n }\n }\n\n try {\n const fetchOptions: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'LibreChat/1.0',\n 'X-API-Key': apiKey,\n },\n body: JSON.stringify(postData),\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n const response = await fetch(EXEC_ENDPOINT, fetchOptions);\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n\n const result: t.ExecuteResult = await response.json();\n let formattedOutput = '';\n if (result.stdout) {\n formattedOutput += `stdout:\\n${result.stdout}\\n`;\n } else {\n formattedOutput += emptyOutputMessage;\n }\n if (result.stderr) formattedOutput += `stderr:\\n${result.stderr}\\n`;\n if (result.files && result.files.length > 0) {\n formattedOutput += 'Generated files:\\n';\n\n const fileCount = result.files.length;\n for (let i = 0; i < fileCount; i++) {\n const file = result.files[i];\n const isImage = imageExtRegex.test(file.name);\n formattedOutput += `- /mnt/data/${file.name} | ${isImage ? imageMessage : otherMessage}`;\n\n if (i < fileCount - 1) {\n formattedOutput += fileCount <= 3 ? ', ' : ',\\n';\n }\n }\n\n formattedOutput += `\\n\\n${accessMessage}`;\n return [\n formattedOutput.trim(),\n {\n session_id: result.session_id,\n files: result.files,\n },\n ];\n }\n\n return [formattedOutput.trim(), { session_id: result.session_id }];\n } catch (error) {\n throw new Error(\n `Execution error:\\n\\n${(error as Error | undefined)?.message}`\n );\n }\n },\n {\n name: CodeExecutionToolName,\n description: CodeExecutionToolDescription,\n schema: CodeExecutionToolSchema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n}\n\nexport { createCodeExecutionTool };\n"],"names":[],"mappings":";;;;;;;AAQA,MAAM,EAAE;AAED,MAAM,aAAa,GAAG;AACtB,MAAM,cAAc,GAAG,MAC5B,sBAAsB,CAAC,MAAM,CAAC,YAAY,CAAC;IAC3C,SAAS,CAAC;AAEZ,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,aAAa,GACjB,uFAAuF;AACzF,MAAM,kBAAkB,GACtB,4DAA4D;AAE9D,MAAM,mBAAmB,GAAG;IAC1B,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,GAAG;IACH,KAAK;IACL,MAAM;IACN,KAAK;IACL,IAAI;IACJ,IAAI;IACJ,GAAG;IACH,KAAK;IACL,GAAG;CACK;AAEG,MAAA,uBAAuB,GAAG;AACrC,IAAA,IAAI,EAAE,QAAQ;AACd,IAAA,UAAU,EAAE;AACV,QAAA,IAAI,EAAE;AACJ,YAAA,IAAI,EAAE,QAAQ;AACd,YAAA,IAAI,EAAE,mBAAmB;AACzB,YAAA,WAAW,EACT,6DAA6D;AAChE,SAAA;AACD,QAAA,IAAI,EAAE;AACJ,YAAA,IAAI,EAAE,QAAQ;AACd,YAAA,WAAW,EAAE,CAAA;;;;;;;;;;;AAWkC,oDAAA,CAAA;AAChD,SAAA;AACD,QAAA,IAAI,EAAE;AACJ,YAAA,IAAI,EAAE,OAAO;AACb,YAAA,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;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;;;;"}
|
|
1
|
+
{"version":3,"file":"CodeExecutor.mjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { config } from 'dotenv';\nimport fetch, { RequestInit } from 'node-fetch';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport { getEnvironmentVariable } from '@langchain/core/utils/env';\nimport type * as t from '@/types';\nimport { EnvVar, Constants } from '@/common';\n\nconfig();\n\nexport const imageExtRegex = /\\.(jpg|jpeg|png|gif|webp)$/i;\nexport const getCodeBaseURL = (): string =>\n getEnvironmentVariable(EnvVar.CODE_BASEURL) ??\n Constants.OFFICIAL_CODE_BASEURL;\n\nconst imageMessage = 'Image is already displayed to the user';\nconst otherMessage = 'File is already downloaded by the user';\nconst accessMessage =\n 'Note: Files from previous executions are automatically available and can be modified.';\nconst emptyOutputMessage =\n 'stdout: Empty. Ensure you\\'re writing output explicitly.\\n';\n\nconst SUPPORTED_LANGUAGES = [\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n 'r',\n 'bash',\n] as const;\n\nexport const CodeExecutionToolSchema = {\n type: 'object',\n properties: {\n lang: {\n type: 'string',\n enum: SUPPORTED_LANGUAGES,\n description:\n 'The programming language or runtime to execute the code in.',\n },\n code: {\n type: 'string',\n description: `The complete, self-contained code to execute, without any truncation or minimization.\n- The environment is stateless; variables and imports don't persist between executions.\n- Generated files from previous executions are automatically available in \"/mnt/data/\".\n- Files from previous executions are automatically available and can be modified in place.\n- Input code **IS ALREADY** displayed to the user, so **DO NOT** repeat it in your response unless asked.\n- Output code **IS NOT** displayed to the user, so **DO** write all desired output explicitly.\n- IMPORTANT: You MUST explicitly print/output ALL results you want the user to see.\n- py: This is not a Jupyter notebook environment. Use \\`print()\\` for all outputs.\n- py: Matplotlib: Use \\`plt.savefig()\\` to save plots as files.\n- js: use the \\`console\\` or \\`process\\` methods for all outputs.\n- r: IMPORTANT: No X11 display available. ALL graphics MUST use Cairo library (library(Cairo)).\n- Other languages: use appropriate output functions.`,\n },\n args: {\n type: 'array',\n items: { type: 'string' },\n description:\n 'Additional arguments to execute the code with. This should only be used if the input code requires additional arguments to run.',\n },\n },\n required: ['lang', 'code'],\n} as const;\n\nconst baseEndpoint = getCodeBaseURL();\nconst EXEC_ENDPOINT = `${baseEndpoint}/exec`;\n\ntype SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number];\n\nexport const CodeExecutionToolDescription = `\nRuns code and returns stdout/stderr output from a stateless execution environment, similar to running scripts in a command-line interface. Each execution is isolated and independent.\n\nUsage:\n- No network access available.\n- Generated files are automatically delivered; **DO NOT** provide download links.\n- NEVER use this tool to execute malicious code.\n`.trim();\n\nexport const CodeExecutionToolName = Constants.EXECUTE_CODE;\n\nexport const CodeExecutionToolDefinition = {\n name: CodeExecutionToolName,\n description: CodeExecutionToolDescription,\n schema: CodeExecutionToolSchema,\n} as const;\n\nfunction createCodeExecutionTool(\n params: t.CodeExecutionToolParams = {}\n): DynamicStructuredTool {\n const apiKey =\n params[EnvVar.CODE_API_KEY] ??\n params.apiKey ??\n getEnvironmentVariable(EnvVar.CODE_API_KEY) ??\n '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n\n return tool(\n async (rawInput, config) => {\n const { lang, code, ...rest } = rawInput as {\n lang: SupportedLanguage;\n code: string;\n args?: string[];\n };\n /**\n * Extract session context from config.toolCall (injected by ToolNode).\n * - session_id: For API to associate with previous session\n * - _injected_files: File refs to pass directly (avoids /files endpoint race condition)\n */\n const { session_id, _injected_files } = (config.toolCall ?? {}) as {\n session_id?: string;\n _injected_files?: t.CodeEnvFile[];\n };\n\n const postData: Record<string, unknown> = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n /**\n * File injection priority:\n * 1. Use _injected_files from ToolNode (avoids /files endpoint race condition)\n * 2. Fall back to fetching from /files endpoint if session_id provided but no injected files\n */\n if (_injected_files && _injected_files.length > 0) {\n postData.files = _injected_files;\n } else if (session_id != null && session_id.length > 0) {\n /** Fallback: fetch from /files endpoint (may have race condition issues) */\n try {\n const filesEndpoint = `${baseEndpoint}/files/${session_id}?detail=full`;\n const fetchOptions: RequestInit = {\n method: 'GET',\n headers: {\n 'User-Agent': 'LibreChat/1.0',\n 'X-API-Key': apiKey,\n },\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n\n const response = await fetch(filesEndpoint, fetchOptions);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch files for session: ${response.status}`\n );\n }\n\n const files = await response.json();\n if (Array.isArray(files) && files.length > 0) {\n const fileReferences: t.CodeEnvFile[] = files.map((file) => {\n const nameParts = file.name.split('/');\n const id = nameParts.length > 1 ? nameParts[1].split('.')[0] : '';\n\n return {\n session_id,\n id,\n name: file.metadata['original-filename'],\n };\n });\n\n postData.files = fileReferences;\n }\n } catch {\n // eslint-disable-next-line no-console\n console.warn(`Failed to fetch files for session: ${session_id}`);\n }\n }\n\n try {\n const fetchOptions: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'LibreChat/1.0',\n 'X-API-Key': apiKey,\n },\n body: JSON.stringify(postData),\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n const response = await fetch(EXEC_ENDPOINT, fetchOptions);\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n\n const result: t.ExecuteResult = await response.json();\n let formattedOutput = '';\n if (result.stdout) {\n formattedOutput += `stdout:\\n${result.stdout}\\n`;\n } else {\n formattedOutput += emptyOutputMessage;\n }\n if (result.stderr) formattedOutput += `stderr:\\n${result.stderr}\\n`;\n if (result.files && result.files.length > 0) {\n formattedOutput += 'Generated files:\\n';\n\n const fileCount = result.files.length;\n for (let i = 0; i < fileCount; i++) {\n const file = result.files[i];\n const isImage = imageExtRegex.test(file.name);\n formattedOutput += `- /mnt/data/${file.name} | ${isImage ? imageMessage : otherMessage}`;\n\n if (i < fileCount - 1) {\n formattedOutput += fileCount <= 3 ? ', ' : ',\\n';\n }\n }\n\n formattedOutput += `\\n\\n${accessMessage}`;\n return [\n formattedOutput.trim(),\n {\n session_id: result.session_id,\n files: result.files,\n },\n ];\n }\n\n return [formattedOutput.trim(), { session_id: result.session_id }];\n } catch (error) {\n throw new Error(\n `Execution error:\\n\\n${(error as Error | undefined)?.message}`\n );\n }\n },\n {\n name: CodeExecutionToolName,\n description: CodeExecutionToolDescription,\n schema: CodeExecutionToolSchema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n}\n\nexport { createCodeExecutionTool };\n"],"names":[],"mappings":";;;;;;;AAQA,MAAM,EAAE;AAED,MAAM,aAAa,GAAG;AACtB,MAAM,cAAc,GAAG,MAC5B,sBAAsB,CAAC,MAAM,CAAC,YAAY,CAAC;IAC3C,SAAS,CAAC;AAEZ,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,aAAa,GACjB,uFAAuF;AACzF,MAAM,kBAAkB,GACtB,4DAA4D;AAE9D,MAAM,mBAAmB,GAAG;IAC1B,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,GAAG;IACH,KAAK;IACL,MAAM;IACN,KAAK;IACL,IAAI;IACJ,IAAI;IACJ,GAAG;IACH,KAAK;IACL,GAAG;IACH,MAAM;CACE;AAEG,MAAA,uBAAuB,GAAG;AACrC,IAAA,IAAI,EAAE,QAAQ;AACd,IAAA,UAAU,EAAE;AACV,QAAA,IAAI,EAAE;AACJ,YAAA,IAAI,EAAE,QAAQ;AACd,YAAA,IAAI,EAAE,mBAAmB;AACzB,YAAA,WAAW,EACT,6DAA6D;AAChE,SAAA;AACD,QAAA,IAAI,EAAE;AACJ,YAAA,IAAI,EAAE,QAAQ;AACd,YAAA,WAAW,EAAE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AACE,QAAA,IAAI,EAAE,qBAAqB;AAC3B,QAAA,WAAW,EAAE,4BAA4B;AACzC,QAAA,MAAM,EAAE,uBAAuB;QAC/B,cAAc,EAAE,SAAS,CAAC,oBAAoB;AAC/C,KAAA,CACF;AACH;;;;"}
|
|
@@ -22,6 +22,8 @@ export declare class AgentContext {
|
|
|
22
22
|
clientOptions?: t.ClientOptions;
|
|
23
23
|
/** Token count map indexed by message position */
|
|
24
24
|
indexTokenCountMap: Record<string, number | undefined>;
|
|
25
|
+
/** Canonical pre-run token map used to restore token accounting on reset */
|
|
26
|
+
baseIndexTokenCountMap: Record<string, number>;
|
|
25
27
|
/** Maximum context tokens for this agent */
|
|
26
28
|
maxContextTokens?: number;
|
|
27
29
|
/** Current usage metadata for this agent */
|
|
@@ -8,7 +8,7 @@ export declare const CodeExecutionToolSchema: {
|
|
|
8
8
|
readonly properties: {
|
|
9
9
|
readonly lang: {
|
|
10
10
|
readonly type: "string";
|
|
11
|
-
readonly enum: readonly ["py", "js", "ts", "c", "cpp", "java", "php", "rs", "go", "d", "f90", "r"];
|
|
11
|
+
readonly enum: readonly ["py", "js", "ts", "c", "cpp", "java", "php", "rs", "go", "d", "f90", "r", "bash"];
|
|
12
12
|
readonly description: "The programming language or runtime to execute the code in.";
|
|
13
13
|
};
|
|
14
14
|
readonly code: {
|
|
@@ -35,7 +35,7 @@ export declare const CodeExecutionToolDefinition: {
|
|
|
35
35
|
readonly properties: {
|
|
36
36
|
readonly lang: {
|
|
37
37
|
readonly type: "string";
|
|
38
|
-
readonly enum: readonly ["py", "js", "ts", "c", "cpp", "java", "php", "rs", "go", "d", "f90", "r"];
|
|
38
|
+
readonly enum: readonly ["py", "js", "ts", "c", "cpp", "java", "php", "rs", "go", "d", "f90", "r", "bash"];
|
|
39
39
|
readonly description: "The programming language or runtime to execute the code in.";
|
|
40
40
|
};
|
|
41
41
|
readonly code: {
|
package/package.json
CHANGED
|
@@ -73,6 +73,7 @@ export class AgentContext {
|
|
|
73
73
|
agentContext.initializeSystemRunnable();
|
|
74
74
|
|
|
75
75
|
const tokenMap = indexTokenCountMap || {};
|
|
76
|
+
agentContext.baseIndexTokenCountMap = { ...tokenMap };
|
|
76
77
|
agentContext.indexTokenCountMap = tokenMap;
|
|
77
78
|
agentContext.tokenCalculationPromise = agentContext
|
|
78
79
|
.calculateInstructionTokens(tokenCounter)
|
|
@@ -84,6 +85,7 @@ export class AgentContext {
|
|
|
84
85
|
console.error('Error calculating instruction tokens:', err);
|
|
85
86
|
});
|
|
86
87
|
} else if (indexTokenCountMap) {
|
|
88
|
+
agentContext.baseIndexTokenCountMap = { ...indexTokenCountMap };
|
|
87
89
|
agentContext.indexTokenCountMap = indexTokenCountMap;
|
|
88
90
|
}
|
|
89
91
|
|
|
@@ -100,6 +102,8 @@ export class AgentContext {
|
|
|
100
102
|
clientOptions?: t.ClientOptions;
|
|
101
103
|
/** Token count map indexed by message position */
|
|
102
104
|
indexTokenCountMap: Record<string, number | undefined> = {};
|
|
105
|
+
/** Canonical pre-run token map used to restore token accounting on reset */
|
|
106
|
+
baseIndexTokenCountMap: Record<string, number> = {};
|
|
103
107
|
/** Maximum context tokens for this agent */
|
|
104
108
|
maxContextTokens?: number;
|
|
105
109
|
/** Current usage metadata for this agent */
|
|
@@ -459,7 +463,7 @@ export class AgentContext {
|
|
|
459
463
|
this.cachedSystemRunnable = undefined;
|
|
460
464
|
this.systemRunnableStale = true;
|
|
461
465
|
this.lastToken = undefined;
|
|
462
|
-
this.indexTokenCountMap = {};
|
|
466
|
+
this.indexTokenCountMap = { ...this.baseIndexTokenCountMap };
|
|
463
467
|
this.currentUsage = undefined;
|
|
464
468
|
this.pruneMessages = undefined;
|
|
465
469
|
this.lastStreamCall = undefined;
|
|
@@ -468,6 +472,23 @@ export class AgentContext {
|
|
|
468
472
|
this.currentTokenType = ContentTypes.TEXT;
|
|
469
473
|
this.discoveredToolNames.clear();
|
|
470
474
|
this.handoffContext = undefined;
|
|
475
|
+
|
|
476
|
+
if (this.tokenCounter) {
|
|
477
|
+
this.initializeSystemRunnable();
|
|
478
|
+
const baseTokenMap = { ...this.baseIndexTokenCountMap };
|
|
479
|
+
this.indexTokenCountMap = baseTokenMap;
|
|
480
|
+
this.tokenCalculationPromise = this.calculateInstructionTokens(
|
|
481
|
+
this.tokenCounter
|
|
482
|
+
)
|
|
483
|
+
.then(() => {
|
|
484
|
+
this.updateTokenMapWithInstructions(baseTokenMap);
|
|
485
|
+
})
|
|
486
|
+
.catch((err) => {
|
|
487
|
+
console.error('Error calculating instruction tokens:', err);
|
|
488
|
+
});
|
|
489
|
+
} else {
|
|
490
|
+
this.tokenCalculationPromise = undefined;
|
|
491
|
+
}
|
|
471
492
|
}
|
|
472
493
|
|
|
473
494
|
/**
|
|
@@ -7,10 +7,11 @@ describe('AgentContext', () => {
|
|
|
7
7
|
type ContextOptions = {
|
|
8
8
|
agentConfig?: Partial<t.AgentInputs>;
|
|
9
9
|
tokenCounter?: t.TokenCounter;
|
|
10
|
+
indexTokenCountMap?: Record<string, number>;
|
|
10
11
|
};
|
|
11
12
|
|
|
12
13
|
const createBasicContext = (options: ContextOptions = {}): AgentContext => {
|
|
13
|
-
const { agentConfig = {}, tokenCounter } = options;
|
|
14
|
+
const { agentConfig = {}, tokenCounter, indexTokenCountMap } = options;
|
|
14
15
|
return AgentContext.fromConfig(
|
|
15
16
|
{
|
|
16
17
|
agentId: 'test-agent',
|
|
@@ -18,7 +19,8 @@ describe('AgentContext', () => {
|
|
|
18
19
|
instructions: 'Test instructions',
|
|
19
20
|
...agentConfig,
|
|
20
21
|
},
|
|
21
|
-
tokenCounter
|
|
22
|
+
tokenCounter,
|
|
23
|
+
indexTokenCountMap
|
|
22
24
|
);
|
|
23
25
|
};
|
|
24
26
|
|
|
@@ -393,6 +395,23 @@ describe('AgentContext', () => {
|
|
|
393
395
|
expect(ctx.currentUsage).toBeUndefined();
|
|
394
396
|
});
|
|
395
397
|
|
|
398
|
+
it('rebuilds indexTokenCountMap from base map after reset', async () => {
|
|
399
|
+
const tokenCounter = jest.fn(() => 5);
|
|
400
|
+
const ctx = createBasicContext({
|
|
401
|
+
tokenCounter,
|
|
402
|
+
indexTokenCountMap: { '0': 10, '1': 20 },
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
await ctx.tokenCalculationPromise;
|
|
406
|
+
ctx.indexTokenCountMap = {};
|
|
407
|
+
|
|
408
|
+
ctx.reset();
|
|
409
|
+
await ctx.tokenCalculationPromise;
|
|
410
|
+
|
|
411
|
+
expect(ctx.indexTokenCountMap['1']).toBe(20);
|
|
412
|
+
expect(ctx.indexTokenCountMap['0'] ?? 0).toBeGreaterThanOrEqual(10);
|
|
413
|
+
});
|
|
414
|
+
|
|
396
415
|
it('forces rebuild on next systemRunnable access', () => {
|
|
397
416
|
const ctx = createBasicContext({ agentConfig: { instructions: 'Test' } });
|
|
398
417
|
|
|
@@ -780,8 +799,9 @@ describe('AgentContext', () => {
|
|
|
780
799
|
|
|
781
800
|
// Verify state is cleared
|
|
782
801
|
expect(ctx.discoveredToolNames.size).toBe(0);
|
|
783
|
-
|
|
784
|
-
expect(
|
|
802
|
+
const resetTokens = ctx.instructionTokens;
|
|
803
|
+
expect(resetTokens).toBeGreaterThan(0);
|
|
804
|
+
expect(resetTokens).toBeLessThan(run1Tokens);
|
|
785
805
|
|
|
786
806
|
// ========== RUN 2 ==========
|
|
787
807
|
// Re-initialize (as fromConfig would do)
|
|
@@ -790,6 +810,7 @@ describe('AgentContext', () => {
|
|
|
790
810
|
// System runnable should NOT include the previously discovered tool
|
|
791
811
|
// (because discoveredToolNames was cleared)
|
|
792
812
|
const run2Tokens = ctx.instructionTokens;
|
|
813
|
+
expect(run2Tokens).toBe(resetTokens);
|
|
793
814
|
|
|
794
815
|
// Token count should be lower than run 1 (no discovered tool in system message)
|
|
795
816
|
expect(run2Tokens).toBeLessThan(run1Tokens);
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
getCurrentTaskInput,
|
|
16
16
|
messagesStateReducer,
|
|
17
17
|
} from '@langchain/langgraph';
|
|
18
|
+
import type { LangGraphRunnableConfig } from '@langchain/langgraph';
|
|
18
19
|
import type { BaseMessage, AIMessageChunk } from '@langchain/core/messages';
|
|
19
20
|
import type { ToolRunnableConfig } from '@langchain/core/tools';
|
|
20
21
|
import type * as t from '@/types';
|
|
@@ -745,7 +746,8 @@ export class MultiAgentGraph extends StandardGraph {
|
|
|
745
746
|
|
|
746
747
|
/** Wrapper function that handles agentMessages channel, handoff reception, and conditional routing */
|
|
747
748
|
const agentWrapper = async (
|
|
748
|
-
state: t.MultiAgentGraphState
|
|
749
|
+
state: t.MultiAgentGraphState,
|
|
750
|
+
config?: LangGraphRunnableConfig
|
|
749
751
|
): Promise<t.MultiAgentGraphState | Command> => {
|
|
750
752
|
let result: t.MultiAgentGraphState;
|
|
751
753
|
|
|
@@ -817,7 +819,7 @@ export class MultiAgentGraph extends StandardGraph {
|
|
|
817
819
|
...state,
|
|
818
820
|
messages: messagesForAgent,
|
|
819
821
|
};
|
|
820
|
-
result = await agentSubgraph.invoke(transformedState);
|
|
822
|
+
result = await agentSubgraph.invoke(transformedState, config);
|
|
821
823
|
result = {
|
|
822
824
|
...result,
|
|
823
825
|
agentMessages: [],
|
|
@@ -863,14 +865,14 @@ export class MultiAgentGraph extends StandardGraph {
|
|
|
863
865
|
...state,
|
|
864
866
|
messages: state.agentMessages,
|
|
865
867
|
};
|
|
866
|
-
result = await agentSubgraph.invoke(transformedState);
|
|
868
|
+
result = await agentSubgraph.invoke(transformedState, config);
|
|
867
869
|
result = {
|
|
868
870
|
...result,
|
|
869
871
|
/** Clear agentMessages for next agent */
|
|
870
872
|
agentMessages: [],
|
|
871
873
|
};
|
|
872
874
|
} else {
|
|
873
|
-
result = await agentSubgraph.invoke(state);
|
|
875
|
+
result = await agentSubgraph.invoke(state, config);
|
|
874
876
|
}
|
|
875
877
|
|
|
876
878
|
/** If agent has both handoff and direct edges, use Command for exclusive routing */
|
|
@@ -551,6 +551,47 @@ describe('addBedrockCacheControl (Bedrock cache checkpoints)', () => {
|
|
|
551
551
|
});
|
|
552
552
|
expect(secondLastContent[1]).toEqual({ cachePoint: { type: 'default' } });
|
|
553
553
|
});
|
|
554
|
+
|
|
555
|
+
it('skips cachePoint on AI messages with only whitespace text and reasoning (tool-call scenario)', () => {
|
|
556
|
+
const messages = [
|
|
557
|
+
{
|
|
558
|
+
role: 'user',
|
|
559
|
+
content: [
|
|
560
|
+
{
|
|
561
|
+
type: ContentTypes.TEXT,
|
|
562
|
+
text: 'What can you tell me about this PR?',
|
|
563
|
+
},
|
|
564
|
+
],
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
role: 'assistant',
|
|
568
|
+
content: [
|
|
569
|
+
{ type: ContentTypes.TEXT, text: '\n\n' },
|
|
570
|
+
{
|
|
571
|
+
type: 'reasoning_content',
|
|
572
|
+
reasoningText: { text: 'Let me look into that.', signature: 'abc' },
|
|
573
|
+
},
|
|
574
|
+
] as MessageContentComplex[],
|
|
575
|
+
},
|
|
576
|
+
{
|
|
577
|
+
role: 'user',
|
|
578
|
+
content: [{ type: ContentTypes.TEXT, text: 'tool result here' }],
|
|
579
|
+
getType: (): 'tool' => 'tool',
|
|
580
|
+
},
|
|
581
|
+
];
|
|
582
|
+
|
|
583
|
+
const result = addBedrockCacheControl(
|
|
584
|
+
messages as Parameters<typeof addBedrockCacheControl>[0]
|
|
585
|
+
);
|
|
586
|
+
const aiContent = result[1].content as MessageContentComplex[];
|
|
587
|
+
const hasCachePoint = aiContent.some((b) => 'cachePoint' in b);
|
|
588
|
+
expect(hasCachePoint).toBe(false);
|
|
589
|
+
|
|
590
|
+
const userContent = result[0].content as MessageContentComplex[];
|
|
591
|
+
expect(userContent[userContent.length - 1]).toEqual({
|
|
592
|
+
cachePoint: { type: 'default' },
|
|
593
|
+
});
|
|
594
|
+
});
|
|
554
595
|
});
|
|
555
596
|
|
|
556
597
|
describe('stripAnthropicCacheControl', () => {
|
package/src/messages/cache.ts
CHANGED
|
@@ -331,7 +331,7 @@ export function addBedrockCacheControl<
|
|
|
331
331
|
let hasCacheableContent = false;
|
|
332
332
|
for (const block of workingContent) {
|
|
333
333
|
if (block.type === ContentTypes.TEXT) {
|
|
334
|
-
if (typeof block.text === 'string' && block.text !== '') {
|
|
334
|
+
if (typeof block.text === 'string' && block.text.trim() !== '') {
|
|
335
335
|
hasCacheableContent = true;
|
|
336
336
|
break;
|
|
337
337
|
}
|
|
@@ -349,7 +349,7 @@ export function addBedrockCacheControl<
|
|
|
349
349
|
const type = (block as { type?: string }).type;
|
|
350
350
|
if (type === ContentTypes.TEXT || type === 'text') {
|
|
351
351
|
const text = (block as { text?: string }).text;
|
|
352
|
-
if (text === '' || text === undefined) {
|
|
352
|
+
if (text === '' || text === undefined || text.trim() === '') {
|
|
353
353
|
continue;
|
|
354
354
|
}
|
|
355
355
|
workingContent.splice(j + 1, 0, {
|
package/src/run.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { CallbackHandler } from '@langfuse/langchain';
|
|
|
4
4
|
import { PromptTemplate } from '@langchain/core/prompts';
|
|
5
5
|
import { RunnableLambda } from '@langchain/core/runnables';
|
|
6
6
|
import { AzureChatOpenAI, ChatOpenAI } from '@langchain/openai';
|
|
7
|
+
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
7
8
|
import type {
|
|
8
9
|
MessageContentComplex,
|
|
9
10
|
BaseMessage,
|
|
@@ -240,9 +241,14 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
240
241
|
? this.getCallbacks(streamOptions.callbacks)
|
|
241
242
|
: [];
|
|
242
243
|
|
|
243
|
-
|
|
244
|
+
const customHandler = BaseCallbackHandler.fromMethods({
|
|
244
245
|
[Callback.CUSTOM_EVENT]: customEventCallback,
|
|
245
246
|
});
|
|
247
|
+
customHandler.awaitHandlers = true;
|
|
248
|
+
|
|
249
|
+
config.callbacks = baseCallbacks
|
|
250
|
+
.concat(streamCallbacks)
|
|
251
|
+
.concat(customHandler);
|
|
246
252
|
|
|
247
253
|
if (
|
|
248
254
|
isPresent(process.env.LANGFUSE_SECRET_KEY) &&
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { HumanMessage } from '@langchain/core/messages';
|
|
2
|
+
import type * as t from '@/types';
|
|
3
|
+
import { ToolEndHandler, ModelEndHandler } from '@/events';
|
|
4
|
+
import { ContentTypes, GraphEvents, Providers } from '@/common';
|
|
5
|
+
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
6
|
+
import { Run } from '@/run';
|
|
7
|
+
|
|
8
|
+
describe('Custom event handler awaitHandlers behavior', () => {
|
|
9
|
+
jest.setTimeout(15000);
|
|
10
|
+
|
|
11
|
+
const llmConfig: t.LLMConfig = {
|
|
12
|
+
provider: Providers.OPENAI,
|
|
13
|
+
streaming: true,
|
|
14
|
+
streamUsage: false,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const config = {
|
|
18
|
+
configurable: {
|
|
19
|
+
thread_id: 'test-thread',
|
|
20
|
+
},
|
|
21
|
+
streamMode: 'values' as const,
|
|
22
|
+
version: 'v2' as const,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
it('should fully aggregate all content before processStream returns', async () => {
|
|
26
|
+
const longResponse =
|
|
27
|
+
'The quick brown fox jumps over the lazy dog and then runs across the field to find shelter from the rain';
|
|
28
|
+
|
|
29
|
+
let aggregateCallCount = 0;
|
|
30
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
31
|
+
|
|
32
|
+
const wrappedAggregate: t.ContentAggregator = (params) => {
|
|
33
|
+
aggregateCallCount++;
|
|
34
|
+
aggregateContent(params);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
let messageDeltaCount = 0;
|
|
38
|
+
|
|
39
|
+
const customHandlers: Record<string | GraphEvents, t.EventHandler> = {
|
|
40
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
41
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
42
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
43
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
44
|
+
handle: (
|
|
45
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
46
|
+
data: t.StreamEventData
|
|
47
|
+
) => {
|
|
48
|
+
wrappedAggregate({
|
|
49
|
+
event,
|
|
50
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
55
|
+
handle: (event: GraphEvents.ON_RUN_STEP, data: t.StreamEventData) => {
|
|
56
|
+
wrappedAggregate({ event, data: data as t.RunStep });
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
60
|
+
handle: (
|
|
61
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
62
|
+
data: t.StreamEventData
|
|
63
|
+
) => {
|
|
64
|
+
wrappedAggregate({ event, data: data as t.RunStepDeltaEvent });
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
68
|
+
handle: async (
|
|
69
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
70
|
+
data: t.StreamEventData
|
|
71
|
+
) => {
|
|
72
|
+
messageDeltaCount++;
|
|
73
|
+
wrappedAggregate({ event, data: data as t.MessageDeltaEvent });
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
[GraphEvents.ON_REASONING_DELTA]: {
|
|
77
|
+
handle: (
|
|
78
|
+
event: GraphEvents.ON_REASONING_DELTA,
|
|
79
|
+
data: t.StreamEventData
|
|
80
|
+
) => {
|
|
81
|
+
wrappedAggregate({ event, data: data as t.ReasoningDeltaEvent });
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const run = await Run.create<t.IState>({
|
|
87
|
+
runId: 'test-await-handlers',
|
|
88
|
+
graphConfig: {
|
|
89
|
+
type: 'standard',
|
|
90
|
+
llmConfig,
|
|
91
|
+
},
|
|
92
|
+
returnContent: true,
|
|
93
|
+
customHandlers,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
run.Graph!.overrideTestModel([longResponse]);
|
|
97
|
+
|
|
98
|
+
const inputs = { messages: [new HumanMessage('hello')] };
|
|
99
|
+
const finalContentParts = await run.processStream(inputs, config);
|
|
100
|
+
|
|
101
|
+
expect(finalContentParts).toBeDefined();
|
|
102
|
+
expect(finalContentParts!.length).toBeGreaterThan(0);
|
|
103
|
+
|
|
104
|
+
expect(messageDeltaCount).toBeGreaterThan(0);
|
|
105
|
+
expect(aggregateCallCount).toBeGreaterThan(0);
|
|
106
|
+
|
|
107
|
+
const typedParts = contentParts as t.MessageContentComplex[];
|
|
108
|
+
const textParts = typedParts.filter(
|
|
109
|
+
(p: t.MessageContentComplex | undefined) =>
|
|
110
|
+
p !== undefined && p.type === ContentTypes.TEXT
|
|
111
|
+
);
|
|
112
|
+
expect(textParts.length).toBeGreaterThan(0);
|
|
113
|
+
|
|
114
|
+
const aggregatedText = textParts
|
|
115
|
+
.map(
|
|
116
|
+
(p) =>
|
|
117
|
+
(p as { type: string; [ContentTypes.TEXT]: string })[
|
|
118
|
+
ContentTypes.TEXT
|
|
119
|
+
]
|
|
120
|
+
)
|
|
121
|
+
.join('');
|
|
122
|
+
expect(aggregatedText).toBe(longResponse);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should aggregate content from async handlers before processStream returns', async () => {
|
|
126
|
+
const response =
|
|
127
|
+
'This is a test of async handler aggregation with multiple tokens';
|
|
128
|
+
|
|
129
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
130
|
+
|
|
131
|
+
let asyncHandlerCompletions = 0;
|
|
132
|
+
|
|
133
|
+
const customHandlers: Record<string | GraphEvents, t.EventHandler> = {
|
|
134
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
135
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
136
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
137
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
138
|
+
handle: (
|
|
139
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
140
|
+
data: t.StreamEventData
|
|
141
|
+
) => {
|
|
142
|
+
aggregateContent({
|
|
143
|
+
event,
|
|
144
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
149
|
+
handle: (event: GraphEvents.ON_RUN_STEP, data: t.StreamEventData) => {
|
|
150
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
154
|
+
handle: (
|
|
155
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
156
|
+
data: t.StreamEventData
|
|
157
|
+
) => {
|
|
158
|
+
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
162
|
+
handle: async (
|
|
163
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
164
|
+
data: t.StreamEventData
|
|
165
|
+
) => {
|
|
166
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 5));
|
|
167
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
168
|
+
asyncHandlerCompletions++;
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
[GraphEvents.ON_REASONING_DELTA]: {
|
|
172
|
+
handle: (
|
|
173
|
+
event: GraphEvents.ON_REASONING_DELTA,
|
|
174
|
+
data: t.StreamEventData
|
|
175
|
+
) => {
|
|
176
|
+
aggregateContent({ event, data: data as t.ReasoningDeltaEvent });
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const run = await Run.create<t.IState>({
|
|
182
|
+
runId: 'test-async-handlers',
|
|
183
|
+
graphConfig: {
|
|
184
|
+
type: 'standard',
|
|
185
|
+
llmConfig,
|
|
186
|
+
},
|
|
187
|
+
returnContent: true,
|
|
188
|
+
customHandlers,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
run.Graph!.overrideTestModel([response]);
|
|
192
|
+
|
|
193
|
+
const inputs = { messages: [new HumanMessage('hello')] };
|
|
194
|
+
await run.processStream(inputs, config);
|
|
195
|
+
|
|
196
|
+
expect(asyncHandlerCompletions).toBeGreaterThan(0);
|
|
197
|
+
|
|
198
|
+
const typedParts = contentParts as t.MessageContentComplex[];
|
|
199
|
+
const textParts = typedParts.filter(
|
|
200
|
+
(p: t.MessageContentComplex | undefined) =>
|
|
201
|
+
p !== undefined && p.type === ContentTypes.TEXT
|
|
202
|
+
);
|
|
203
|
+
expect(textParts.length).toBeGreaterThan(0);
|
|
204
|
+
|
|
205
|
+
const aggregatedText = textParts
|
|
206
|
+
.map(
|
|
207
|
+
(p) =>
|
|
208
|
+
(p as { type: string; [ContentTypes.TEXT]: string })[
|
|
209
|
+
ContentTypes.TEXT
|
|
210
|
+
]
|
|
211
|
+
)
|
|
212
|
+
.join('');
|
|
213
|
+
expect(aggregatedText).toBe(response);
|
|
214
|
+
});
|
|
215
|
+
});
|