@librechat/agents 1.6.6 → 1.6.8
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/main.cjs +1 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +11 -2
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/esm/main.mjs +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +11 -3
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/types/tools/CodeExecutor.d.ts +1 -0
- package/package.json +1 -1
- package/src/tools/CodeExecutor.ts +11 -2
package/dist/cjs/main.cjs
CHANGED
|
@@ -30,6 +30,7 @@ exports.Graph = Graph.Graph;
|
|
|
30
30
|
exports.StandardGraph = Graph.StandardGraph;
|
|
31
31
|
exports.createCodeExecutionTool = CodeExecutor.createCodeExecutionTool;
|
|
32
32
|
exports.getCodeBaseURL = CodeExecutor.getCodeBaseURL;
|
|
33
|
+
exports.imageExtRegex = CodeExecutor.imageExtRegex;
|
|
33
34
|
Object.defineProperty(exports, "Callback", {
|
|
34
35
|
enumerable: true,
|
|
35
36
|
get: function () { return _enum.Callback; }
|
package/dist/cjs/main.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -7,7 +7,10 @@ var env = require('@langchain/core/utils/env');
|
|
|
7
7
|
var _enum = require('../common/enum.cjs');
|
|
8
8
|
|
|
9
9
|
dotenv.config();
|
|
10
|
+
const imageExtRegex = /\.(jpg|jpeg|png|gif|webp)$/i;
|
|
10
11
|
const getCodeBaseURL = () => env.getEnvironmentVariable(_enum.EnvVar.CODE_BASEURL) ?? _enum.Constants.OFFICIAL_CODE_BASEURL;
|
|
12
|
+
const imageMessage = ' - the image is already displayed to the user';
|
|
13
|
+
const otherMessage = ' - the file is already downloaded by the user';
|
|
11
14
|
const CodeExecutionToolSchema = zod.z.object({
|
|
12
15
|
lang: zod.z.enum([
|
|
13
16
|
'py',
|
|
@@ -68,7 +71,9 @@ function createCodeExecutionTool(params = {}) {
|
|
|
68
71
|
formattedOutput += 'Generated files:\n';
|
|
69
72
|
const fileCount = result.files.length;
|
|
70
73
|
for (let i = 0; i < fileCount; i++) {
|
|
71
|
-
|
|
74
|
+
const filename = result.files[i].name;
|
|
75
|
+
const isImage = imageExtRegex.test(filename);
|
|
76
|
+
formattedOutput += isImage ? `${filename}${imageMessage}` : `${filename}${otherMessage}`;
|
|
72
77
|
if (i < fileCount - 1) {
|
|
73
78
|
formattedOutput += fileCount <= 3 ? ', ' : ',\n';
|
|
74
79
|
}
|
|
@@ -97,7 +102,10 @@ function createCodeExecutionTool(params = {}) {
|
|
|
97
102
|
- For \`py\`, use the \`print()\` function.
|
|
98
103
|
- For \`js\` and \`ts\`, use the \`console\` or \`process\` methods.
|
|
99
104
|
- For other languages, use the appropriate output functions.
|
|
100
|
-
-
|
|
105
|
+
- There is no network access.
|
|
106
|
+
- NEVER provide a link to download any generated files.
|
|
107
|
+
- Files are automatically delivered to the user.
|
|
108
|
+
- NEVER use this tool to execute malicious code.`,
|
|
101
109
|
schema: CodeExecutionToolSchema,
|
|
102
110
|
responseFormat: 'content_and_artifact',
|
|
103
111
|
});
|
|
@@ -105,4 +113,5 @@ function createCodeExecutionTool(params = {}) {
|
|
|
105
113
|
|
|
106
114
|
exports.createCodeExecutionTool = createCodeExecutionTool;
|
|
107
115
|
exports.getCodeBaseURL = getCodeBaseURL;
|
|
116
|
+
exports.imageExtRegex = imageExtRegex;
|
|
108
117
|
//# sourceMappingURL=CodeExecutor.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeExecutor.cjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { z } from 'zod';\nimport { config } from 'dotenv';\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 getCodeBaseURL = (): string => getEnvironmentVariable(EnvVar.CODE_BASEURL) ?? Constants.OFFICIAL_CODE_BASEURL;\n\nconst CodeExecutionToolSchema = z.object({\n lang: z.enum([\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n ])\n .describe('The programming language or runtime to execute the code in.'),\n code: z.string()\n .describe('The complete, self-contained code to execute, without any truncation or minimization.'),\n args: z.array(z.string()).optional()\n .describe('Additional arguments to execute the code with.'),\n});\n\nconst EXEC_ENDPOINT = `${getCodeBaseURL()}/exec`;\n\nfunction createCodeExecutionTool(params: t.CodeExecutionToolParams = {}): DynamicStructuredTool<typeof CodeExecutionToolSchema> {\n const apiKey = params[EnvVar.CODE_API_KEY] ?? params.apiKey ?? getEnvironmentVariable(EnvVar.CODE_API_KEY) ?? '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n return tool<typeof CodeExecutionToolSchema>(\n async ({ lang, code, ...rest }) => {\n const postData = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n try {\n const response = await fetch(EXEC_ENDPOINT, {\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 (!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 += 'stdout: Empty. To output values, write explicitly to stdout.\\n';\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
|
|
1
|
+
{"version":3,"file":"CodeExecutor.cjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { z } from 'zod';\nimport { config } from 'dotenv';\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 => getEnvironmentVariable(EnvVar.CODE_BASEURL) ?? Constants.OFFICIAL_CODE_BASEURL;\n\nconst imageMessage = ' - the image is already displayed to the user';\nconst otherMessage = ' - the file is already downloaded by the user';\n\nconst CodeExecutionToolSchema = z.object({\n lang: z.enum([\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n ])\n .describe('The programming language or runtime to execute the code in.'),\n code: z.string()\n .describe('The complete, self-contained code to execute, without any truncation or minimization.'),\n args: z.array(z.string()).optional()\n .describe('Additional arguments to execute the code with.'),\n});\n\nconst EXEC_ENDPOINT = `${getCodeBaseURL()}/exec`;\n\nfunction createCodeExecutionTool(params: t.CodeExecutionToolParams = {}): DynamicStructuredTool<typeof CodeExecutionToolSchema> {\n const apiKey = params[EnvVar.CODE_API_KEY] ?? params.apiKey ?? getEnvironmentVariable(EnvVar.CODE_API_KEY) ?? '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n return tool<typeof CodeExecutionToolSchema>(\n async ({ lang, code, ...rest }) => {\n const postData = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n try {\n const response = await fetch(EXEC_ENDPOINT, {\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 (!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 += 'stdout: Empty. To output values, write explicitly to stdout.\\n';\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 filename = result.files[i].name;\n const isImage = imageExtRegex.test(filename);\n formattedOutput += isImage ? `${filename}${imageMessage}` : `${filename}${otherMessage}`;\n\n if (i < fileCount - 1) {\n formattedOutput += fileCount <= 3 ? ', ' : ',\\n';\n }\n }\n\n return [formattedOutput.trim(), {\n session_id: result.session_id,\n files: result.files,\n }];\n }\n\n return [formattedOutput.trim(), { session_id: result.session_id }];\n } catch (error) {\n return `Calling tool with arguments:\\n\\n${JSON.stringify({\n lang,\n code,\n ...rest,\n })}\\n\\nraised the following error:\\n\\n${(error as Error | undefined)?.message}`;\n }\n },\n {\n name: 'execute_code',\n description: `Executes code in various programming languages, returning stdout/stderr output.\n\n# Usage\n- Input code is automatically displayed to the user, so don't repeat it in your response unless asked.\n- All desired output must be explicitly written to stdout; e.g.:\n - For \\`py\\`, use the \\`print()\\` function.\n - For \\`js\\` and \\`ts\\`, use the \\`console\\` or \\`process\\` methods.\n - For other languages, use the appropriate output functions.\n- There is no network access.\n- NEVER provide a link to download any generated files.\n - Files are automatically delivered to the user.\n- NEVER use this tool to execute malicious code.`,\n schema: CodeExecutionToolSchema,\n responseFormat: 'content_and_artifact',\n }\n );\n}\n\nexport { createCodeExecutionTool };"],"names":["config","getEnvironmentVariable","EnvVar","Constants","z","tool"],"mappings":";;;;;;;;AAOAA,aAAM,EAA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CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;oBACtC,MAAM,OAAO,GAAG,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC7C,oBAAA,eAAe,IAAI,OAAO,GAAG,CAAA,EAAG,QAAQ,CAAG,EAAA,YAAY,CAAE,CAAA,GAAG,CAAA,EAAG,QAAQ,CAAG,EAAA,YAAY,EAAE,CAAC;AAEzF,oBAAA,IAAI,CAAC,GAAG,SAAS,GAAG,CAAC,EAAE;AACrB,wBAAA,eAAe,IAAI,SAAS,IAAI,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;qBAClD;iBACF;AAED,gBAAA,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,EAAE;wBAC9B,UAAU,EAAE,MAAM,CAAC,UAAU;wBAC7B,KAAK,EAAE,MAAM,CAAC,KAAK;AACpB,qBAAA,CAAC,CAAC;aACJ;AAED,YAAA,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;SACpE;QAAC,OAAO,KAAK,EAAE;AACd,YAAA,OAAO,CAAmC,gCAAA,EAAA,IAAI,CAAC,SAAS,CAAC;gBACvD,IAAI;gBACJ,IAAI;AACJ,gBAAA,GAAG,IAAI;AACR,aAAA,CAAC,CAAuC,mCAAA,EAAA,KAA2B,EAAE,OAAO,EAAE,CAAC;SACjF;AACH,KAAC,EACD;AACE,QAAA,IAAI,EAAE,cAAc;AACpB,QAAA,WAAW,EAAE,CAAA;;;;;;;;;;;AAW8B,gDAAA,CAAA;AAC3C,QAAA,MAAM,EAAE,uBAAuB;AAC/B,QAAA,cAAc,EAAE,sBAAsB;AACvC,KAAA,CACF,CAAC;AACJ;;;;;;"}
|
package/dist/esm/main.mjs
CHANGED
|
@@ -3,7 +3,7 @@ export { ChatModelStreamHandler, createContentAggregator } from './stream.mjs';
|
|
|
3
3
|
export { HandlerRegistry, LLMStreamHandler, ModelEndHandler, TestChatStreamHandler, TestLLMStreamHandler, ToolEndHandler, createMetadataAggregator } from './events.mjs';
|
|
4
4
|
export { convertMessagesToContent, formatAnthropicMessage, getConverseOverrideMessage, modifyDeltaProperties } from './messages.mjs';
|
|
5
5
|
export { Graph, StandardGraph } from './graphs/Graph.mjs';
|
|
6
|
-
export { createCodeExecutionTool, getCodeBaseURL } from './tools/CodeExecutor.mjs';
|
|
6
|
+
export { createCodeExecutionTool, getCodeBaseURL, imageExtRegex } from './tools/CodeExecutor.mjs';
|
|
7
7
|
export { Callback, CommonEvents, Constants, ContentTypes, EnvVar, GraphEvents, GraphNodeActions, GraphNodeKeys, Providers, StepTypes, ToolCallTypes } from './common/enum.mjs';
|
|
8
8
|
export { joinKeys, resetIfNotEmpty } from './utils/graph.mjs';
|
|
9
9
|
export { RunnableCallable, sleep } from './utils/run.mjs';
|
|
@@ -5,7 +5,10 @@ import { getEnvironmentVariable } from '@langchain/core/utils/env';
|
|
|
5
5
|
import { EnvVar, Constants } from '../common/enum.mjs';
|
|
6
6
|
|
|
7
7
|
config();
|
|
8
|
+
const imageExtRegex = /\.(jpg|jpeg|png|gif|webp)$/i;
|
|
8
9
|
const getCodeBaseURL = () => getEnvironmentVariable(EnvVar.CODE_BASEURL) ?? Constants.OFFICIAL_CODE_BASEURL;
|
|
10
|
+
const imageMessage = ' - the image is already displayed to the user';
|
|
11
|
+
const otherMessage = ' - the file is already downloaded by the user';
|
|
9
12
|
const CodeExecutionToolSchema = z.object({
|
|
10
13
|
lang: z.enum([
|
|
11
14
|
'py',
|
|
@@ -66,7 +69,9 @@ function createCodeExecutionTool(params = {}) {
|
|
|
66
69
|
formattedOutput += 'Generated files:\n';
|
|
67
70
|
const fileCount = result.files.length;
|
|
68
71
|
for (let i = 0; i < fileCount; i++) {
|
|
69
|
-
|
|
72
|
+
const filename = result.files[i].name;
|
|
73
|
+
const isImage = imageExtRegex.test(filename);
|
|
74
|
+
formattedOutput += isImage ? `${filename}${imageMessage}` : `${filename}${otherMessage}`;
|
|
70
75
|
if (i < fileCount - 1) {
|
|
71
76
|
formattedOutput += fileCount <= 3 ? ', ' : ',\n';
|
|
72
77
|
}
|
|
@@ -95,11 +100,14 @@ function createCodeExecutionTool(params = {}) {
|
|
|
95
100
|
- For \`py\`, use the \`print()\` function.
|
|
96
101
|
- For \`js\` and \`ts\`, use the \`console\` or \`process\` methods.
|
|
97
102
|
- For other languages, use the appropriate output functions.
|
|
98
|
-
-
|
|
103
|
+
- There is no network access.
|
|
104
|
+
- NEVER provide a link to download any generated files.
|
|
105
|
+
- Files are automatically delivered to the user.
|
|
106
|
+
- NEVER use this tool to execute malicious code.`,
|
|
99
107
|
schema: CodeExecutionToolSchema,
|
|
100
108
|
responseFormat: 'content_and_artifact',
|
|
101
109
|
});
|
|
102
110
|
}
|
|
103
111
|
|
|
104
|
-
export { createCodeExecutionTool, getCodeBaseURL };
|
|
112
|
+
export { createCodeExecutionTool, getCodeBaseURL, imageExtRegex };
|
|
105
113
|
//# sourceMappingURL=CodeExecutor.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeExecutor.mjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { z } from 'zod';\nimport { config } from 'dotenv';\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 getCodeBaseURL = (): string => getEnvironmentVariable(EnvVar.CODE_BASEURL) ?? Constants.OFFICIAL_CODE_BASEURL;\n\nconst CodeExecutionToolSchema = z.object({\n lang: z.enum([\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n ])\n .describe('The programming language or runtime to execute the code in.'),\n code: z.string()\n .describe('The complete, self-contained code to execute, without any truncation or minimization.'),\n args: z.array(z.string()).optional()\n .describe('Additional arguments to execute the code with.'),\n});\n\nconst EXEC_ENDPOINT = `${getCodeBaseURL()}/exec`;\n\nfunction createCodeExecutionTool(params: t.CodeExecutionToolParams = {}): DynamicStructuredTool<typeof CodeExecutionToolSchema> {\n const apiKey = params[EnvVar.CODE_API_KEY] ?? params.apiKey ?? getEnvironmentVariable(EnvVar.CODE_API_KEY) ?? '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n return tool<typeof CodeExecutionToolSchema>(\n async ({ lang, code, ...rest }) => {\n const postData = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n try {\n const response = await fetch(EXEC_ENDPOINT, {\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 (!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 += 'stdout: Empty. To output values, write explicitly to stdout.\\n';\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
|
|
1
|
+
{"version":3,"file":"CodeExecutor.mjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { z } from 'zod';\nimport { config } from 'dotenv';\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 => getEnvironmentVariable(EnvVar.CODE_BASEURL) ?? Constants.OFFICIAL_CODE_BASEURL;\n\nconst imageMessage = ' - the image is already displayed to the user';\nconst otherMessage = ' - the file is already downloaded by the user';\n\nconst CodeExecutionToolSchema = z.object({\n lang: z.enum([\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n ])\n .describe('The programming language or runtime to execute the code in.'),\n code: z.string()\n .describe('The complete, self-contained code to execute, without any truncation or minimization.'),\n args: z.array(z.string()).optional()\n .describe('Additional arguments to execute the code with.'),\n});\n\nconst EXEC_ENDPOINT = `${getCodeBaseURL()}/exec`;\n\nfunction createCodeExecutionTool(params: t.CodeExecutionToolParams = {}): DynamicStructuredTool<typeof CodeExecutionToolSchema> {\n const apiKey = params[EnvVar.CODE_API_KEY] ?? params.apiKey ?? getEnvironmentVariable(EnvVar.CODE_API_KEY) ?? '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n return tool<typeof CodeExecutionToolSchema>(\n async ({ lang, code, ...rest }) => {\n const postData = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n try {\n const response = await fetch(EXEC_ENDPOINT, {\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 (!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 += 'stdout: Empty. To output values, write explicitly to stdout.\\n';\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 filename = result.files[i].name;\n const isImage = imageExtRegex.test(filename);\n formattedOutput += isImage ? `${filename}${imageMessage}` : `${filename}${otherMessage}`;\n\n if (i < fileCount - 1) {\n formattedOutput += fileCount <= 3 ? ', ' : ',\\n';\n }\n }\n\n return [formattedOutput.trim(), {\n session_id: result.session_id,\n files: result.files,\n }];\n }\n\n return [formattedOutput.trim(), { session_id: result.session_id }];\n } catch (error) {\n return `Calling tool with arguments:\\n\\n${JSON.stringify({\n lang,\n code,\n ...rest,\n })}\\n\\nraised the following error:\\n\\n${(error as Error | undefined)?.message}`;\n }\n },\n {\n name: 'execute_code',\n description: `Executes code in various programming languages, returning stdout/stderr output.\n\n# Usage\n- Input code is automatically displayed to the user, so don't repeat it in your response unless asked.\n- All desired output must be explicitly written to stdout; e.g.:\n - For \\`py\\`, use the \\`print()\\` function.\n - For \\`js\\` and \\`ts\\`, use the \\`console\\` or \\`process\\` methods.\n - For other languages, use the appropriate output functions.\n- There is no network access.\n- NEVER provide a link to download any generated files.\n - Files are automatically delivered to the user.\n- NEVER use this tool to execute malicious code.`,\n schema: CodeExecutionToolSchema,\n responseFormat: 'content_and_artifact',\n }\n );\n}\n\nexport { createCodeExecutionTool };"],"names":[],"mappings":";;;;;;AAOA,MAAM,EAAE,CAAC;AAEF,MAAM,aAAa,GAAG,8BAA8B;AAC9C,MAAA,cAAc,GAAG,MAAc,sBAAsB,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,SAAS,CAAC,sBAAsB;AAE3H,MAAM,YAAY,GAAG,+CAA+C,CAAC;AACrE,MAAM,YAAY,GAAG,+CAA+C,CAAC;AAErE,MAAM,uBAAuB,GAAG,CAAC,CAAC,MAAM,CAAC;AACvC,IAAA,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC;QACX,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,GAAG;QACH,KAAK;QACL,MAAM;QACN,KAAK;QACL,IAAI;QACJ,IAAI;QACJ,GAAG;QACH,KAAK;KACN,CAAC;SACC,QAAQ,CAAC,6DAA6D,CAAC;AAC1E,IAAA,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;SACb,QAAQ,CAAC,uFAAuF,CAAC;AACpG,IAAA,IAAI,EAAE,CAAC,CAAC,KAAK,CAA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EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;SACpE;QAAC,OAAO,KAAK,EAAE;AACd,YAAA,OAAO,CAAmC,gCAAA,EAAA,IAAI,CAAC,SAAS,CAAC;gBACvD,IAAI;gBACJ,IAAI;AACJ,gBAAA,GAAG,IAAI;AACR,aAAA,CAAC,CAAuC,mCAAA,EAAA,KAA2B,EAAE,OAAO,EAAE,CAAC;SACjF;AACH,KAAC,EACD;AACE,QAAA,IAAI,EAAE,cAAc;AACpB,QAAA,WAAW,EAAE,CAAA;;;;;;;;;;;AAW8B,gDAAA,CAAA;AAC3C,QAAA,MAAM,EAAE,uBAAuB;AAC/B,QAAA,cAAc,EAAE,sBAAsB;AACvC,KAAA,CACF,CAAC;AACJ;;;;"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
3
3
|
import type * as t from '@/types';
|
|
4
|
+
export declare const imageExtRegex: RegExp;
|
|
4
5
|
export declare const getCodeBaseURL: () => string;
|
|
5
6
|
declare const CodeExecutionToolSchema: z.ZodObject<{
|
|
6
7
|
lang: z.ZodEnum<["py", "js", "ts", "c", "cpp", "java", "php", "rs", "go", "d", "f90"]>;
|
package/package.json
CHANGED
|
@@ -7,8 +7,12 @@ import { EnvVar, Constants } from '@/common';
|
|
|
7
7
|
|
|
8
8
|
config();
|
|
9
9
|
|
|
10
|
+
export const imageExtRegex = /\.(jpg|jpeg|png|gif|webp)$/i;
|
|
10
11
|
export const getCodeBaseURL = (): string => getEnvironmentVariable(EnvVar.CODE_BASEURL) ?? Constants.OFFICIAL_CODE_BASEURL;
|
|
11
12
|
|
|
13
|
+
const imageMessage = ' - the image is already displayed to the user';
|
|
14
|
+
const otherMessage = ' - the file is already downloaded by the user';
|
|
15
|
+
|
|
12
16
|
const CodeExecutionToolSchema = z.object({
|
|
13
17
|
lang: z.enum([
|
|
14
18
|
'py',
|
|
@@ -74,7 +78,9 @@ function createCodeExecutionTool(params: t.CodeExecutionToolParams = {}): Dynami
|
|
|
74
78
|
|
|
75
79
|
const fileCount = result.files.length;
|
|
76
80
|
for (let i = 0; i < fileCount; i++) {
|
|
77
|
-
|
|
81
|
+
const filename = result.files[i].name;
|
|
82
|
+
const isImage = imageExtRegex.test(filename);
|
|
83
|
+
formattedOutput += isImage ? `${filename}${imageMessage}` : `${filename}${otherMessage}`;
|
|
78
84
|
|
|
79
85
|
if (i < fileCount - 1) {
|
|
80
86
|
formattedOutput += fileCount <= 3 ? ', ' : ',\n';
|
|
@@ -106,7 +112,10 @@ function createCodeExecutionTool(params: t.CodeExecutionToolParams = {}): Dynami
|
|
|
106
112
|
- For \`py\`, use the \`print()\` function.
|
|
107
113
|
- For \`js\` and \`ts\`, use the \`console\` or \`process\` methods.
|
|
108
114
|
- For other languages, use the appropriate output functions.
|
|
109
|
-
-
|
|
115
|
+
- There is no network access.
|
|
116
|
+
- NEVER provide a link to download any generated files.
|
|
117
|
+
- Files are automatically delivered to the user.
|
|
118
|
+
- NEVER use this tool to execute malicious code.`,
|
|
110
119
|
schema: CodeExecutionToolSchema,
|
|
111
120
|
responseFormat: 'content_and_artifact',
|
|
112
121
|
}
|