@granular-software/sdk 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -41
- package/dist/cli/index.js +42 -42
- package/dist/index.d.mts +726 -3
- package/dist/index.d.ts +726 -3
- package/dist/index.js +53 -45
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -45
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -52
- package/dist/adapters/anthropic.d.mts +0 -64
- package/dist/adapters/anthropic.d.ts +0 -64
- package/dist/adapters/anthropic.js +0 -77
- package/dist/adapters/anthropic.js.map +0 -1
- package/dist/adapters/anthropic.mjs +0 -72
- package/dist/adapters/anthropic.mjs.map +0 -1
- package/dist/adapters/langchain.d.mts +0 -31
- package/dist/adapters/langchain.d.ts +0 -31
- package/dist/adapters/langchain.js +0 -49
- package/dist/adapters/langchain.js.map +0 -1
- package/dist/adapters/langchain.mjs +0 -46
- package/dist/adapters/langchain.mjs.map +0 -1
- package/dist/adapters/mastra.d.mts +0 -23
- package/dist/adapters/mastra.d.ts +0 -23
- package/dist/adapters/mastra.js +0 -26
- package/dist/adapters/mastra.js.map +0 -1
- package/dist/adapters/mastra.mjs +0 -24
- package/dist/adapters/mastra.mjs.map +0 -1
- package/dist/adapters/openai.d.mts +0 -76
- package/dist/adapters/openai.d.ts +0 -76
- package/dist/adapters/openai.js +0 -81
- package/dist/adapters/openai.js.map +0 -1
- package/dist/adapters/openai.mjs +0 -76
- package/dist/adapters/openai.mjs.map +0 -1
- package/dist/types-C0AVRsVR.d.mts +0 -726
- package/dist/types-C0AVRsVR.d.ts +0 -726
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@granular-software/sdk",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "TypeScript SDK and CLI for Granular - define, build, and deploy AI sandboxes",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -10,30 +10,10 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.mjs",
|
|
12
12
|
"require": "./dist/index.js"
|
|
13
|
-
},
|
|
14
|
-
"./mastra": {
|
|
15
|
-
"types": "./dist/adapters/mastra.d.ts",
|
|
16
|
-
"import": "./dist/adapters/mastra.mjs",
|
|
17
|
-
"require": "./dist/adapters/mastra.js"
|
|
18
|
-
},
|
|
19
|
-
"./langchain": {
|
|
20
|
-
"types": "./dist/adapters/langchain.d.ts",
|
|
21
|
-
"import": "./dist/adapters/langchain.mjs",
|
|
22
|
-
"require": "./dist/adapters/langchain.js"
|
|
23
|
-
},
|
|
24
|
-
"./openai": {
|
|
25
|
-
"types": "./dist/adapters/openai.d.ts",
|
|
26
|
-
"import": "./dist/adapters/openai.mjs",
|
|
27
|
-
"require": "./dist/adapters/openai.js"
|
|
28
|
-
},
|
|
29
|
-
"./anthropic": {
|
|
30
|
-
"types": "./dist/adapters/anthropic.d.ts",
|
|
31
|
-
"import": "./dist/adapters/anthropic.mjs",
|
|
32
|
-
"require": "./dist/adapters/anthropic.js"
|
|
33
13
|
}
|
|
34
14
|
},
|
|
35
15
|
"bin": {
|
|
36
|
-
"granular": "
|
|
16
|
+
"granular": "dist/cli/index.js"
|
|
37
17
|
},
|
|
38
18
|
"files": [
|
|
39
19
|
"dist",
|
|
@@ -57,17 +37,14 @@
|
|
|
57
37
|
"ai",
|
|
58
38
|
"agent",
|
|
59
39
|
"tools",
|
|
60
|
-
"sandbox"
|
|
61
|
-
"mastra",
|
|
62
|
-
"langchain",
|
|
63
|
-
"openai",
|
|
64
|
-
"anthropic"
|
|
40
|
+
"sandbox"
|
|
65
41
|
],
|
|
66
42
|
"author": "Granular Team",
|
|
67
43
|
"license": "MIT",
|
|
68
44
|
"repository": {
|
|
69
45
|
"type": "git",
|
|
70
|
-
"url": "https://github.com/
|
|
46
|
+
"url": "git+https://github.com/ahirel/granular-software.git",
|
|
47
|
+
"directory": "packages/granular-sdk"
|
|
71
48
|
},
|
|
72
49
|
"dependencies": {
|
|
73
50
|
"@automerge/automerge": "^3.2.1",
|
|
@@ -79,36 +56,13 @@
|
|
|
79
56
|
"zod-to-json-schema": "^3.25.1"
|
|
80
57
|
},
|
|
81
58
|
"devDependencies": {
|
|
82
|
-
"@anthropic-ai/sdk": "^0.10.0",
|
|
83
|
-
"@langchain/core": "^0.1.0",
|
|
84
59
|
"@types/commander": "^2.12.5",
|
|
85
60
|
"@types/node": "^25.2.3",
|
|
86
61
|
"@types/ws": "^8.18.1",
|
|
87
|
-
"
|
|
88
|
-
"tsup": "^8.0.0",
|
|
62
|
+
"tsup": "8.5.0",
|
|
89
63
|
"typescript": "^5.0.0",
|
|
90
64
|
"zod": "3.25.76"
|
|
91
65
|
},
|
|
92
|
-
"peerDependencies": {
|
|
93
|
-
"@mastra/core": ">=0.1.0",
|
|
94
|
-
"@langchain/core": ">=0.1.0",
|
|
95
|
-
"openai": ">=4.0.0",
|
|
96
|
-
"@anthropic-ai/sdk": ">=0.10.0"
|
|
97
|
-
},
|
|
98
|
-
"peerDependenciesMeta": {
|
|
99
|
-
"@mastra/core": {
|
|
100
|
-
"optional": true
|
|
101
|
-
},
|
|
102
|
-
"@langchain/core": {
|
|
103
|
-
"optional": true
|
|
104
|
-
},
|
|
105
|
-
"openai": {
|
|
106
|
-
"optional": true
|
|
107
|
-
},
|
|
108
|
-
"@anthropic-ai/sdk": {
|
|
109
|
-
"optional": true
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
66
|
"engines": {
|
|
113
67
|
"node": ">=18.0.0"
|
|
114
68
|
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { T as ToolWithHandler } from '../types-C0AVRsVR.mjs';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Anthropic tool_use block from message response
|
|
5
|
-
*/
|
|
6
|
-
interface AnthropicToolUseBlock {
|
|
7
|
-
type: 'tool_use';
|
|
8
|
-
id: string;
|
|
9
|
-
name: string;
|
|
10
|
-
input: unknown;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Anthropic tool definition
|
|
14
|
-
*/
|
|
15
|
-
interface AnthropicToolDefinition {
|
|
16
|
-
name: string;
|
|
17
|
-
description?: string;
|
|
18
|
-
input_schema: Record<string, unknown>;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Helper to convert Granular tools to Anthropic tool definitions
|
|
22
|
-
*
|
|
23
|
-
* @param tools - Array of Granular tools
|
|
24
|
-
* @returns Array of Anthropic tool definitions
|
|
25
|
-
*/
|
|
26
|
-
declare function toAnthropicTools(tools: ToolWithHandler[]): AnthropicToolDefinition[];
|
|
27
|
-
/**
|
|
28
|
-
* Helper to convert Anthropic tool calls to job code for Granular
|
|
29
|
-
*
|
|
30
|
-
* @param contentBlocks - Array of content blocks from Anthropic response
|
|
31
|
-
* @param customImports - Optional imports to add to top of job
|
|
32
|
-
* @returns generated job code, or null if no tool uses found
|
|
33
|
-
*/
|
|
34
|
-
declare function toolUsesToJobCode(contentBlocks: Array<{
|
|
35
|
-
type: string;
|
|
36
|
-
id?: string;
|
|
37
|
-
name?: string;
|
|
38
|
-
input?: unknown;
|
|
39
|
-
}>, customImports?: string): string | null;
|
|
40
|
-
/**
|
|
41
|
-
* Create a single "codegen tool" for LLM consumption.
|
|
42
|
-
*
|
|
43
|
-
* Instead of passing N individual tools to the LLM, this returns a single
|
|
44
|
-
* tool that accepts TypeScript code. The LLM writes code that uses the
|
|
45
|
-
* available tools, and the code is executed in the sandbox.
|
|
46
|
-
*
|
|
47
|
-
* @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())
|
|
48
|
-
* @returns A single Anthropic tool definition
|
|
49
|
-
*/
|
|
50
|
-
declare function createCodegenTool(domainDocumentation: string): AnthropicToolDefinition;
|
|
51
|
-
/**
|
|
52
|
-
* Helper to execute a codegen tool use block.
|
|
53
|
-
*
|
|
54
|
-
* @param toolUse - The tool_use block from the Anthropic response
|
|
55
|
-
* @param environment - The connected Granular environment
|
|
56
|
-
* @returns The job result
|
|
57
|
-
*/
|
|
58
|
-
declare function executeCodegenToolUse(toolUse: AnthropicToolUseBlock, environment: {
|
|
59
|
-
submitJob: (code: string) => Promise<{
|
|
60
|
-
result: Promise<unknown>;
|
|
61
|
-
}>;
|
|
62
|
-
}): Promise<unknown>;
|
|
63
|
-
|
|
64
|
-
export { type AnthropicToolDefinition, type AnthropicToolUseBlock, createCodegenTool, executeCodegenToolUse, toAnthropicTools, toolUsesToJobCode };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { T as ToolWithHandler } from '../types-C0AVRsVR.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Anthropic tool_use block from message response
|
|
5
|
-
*/
|
|
6
|
-
interface AnthropicToolUseBlock {
|
|
7
|
-
type: 'tool_use';
|
|
8
|
-
id: string;
|
|
9
|
-
name: string;
|
|
10
|
-
input: unknown;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Anthropic tool definition
|
|
14
|
-
*/
|
|
15
|
-
interface AnthropicToolDefinition {
|
|
16
|
-
name: string;
|
|
17
|
-
description?: string;
|
|
18
|
-
input_schema: Record<string, unknown>;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Helper to convert Granular tools to Anthropic tool definitions
|
|
22
|
-
*
|
|
23
|
-
* @param tools - Array of Granular tools
|
|
24
|
-
* @returns Array of Anthropic tool definitions
|
|
25
|
-
*/
|
|
26
|
-
declare function toAnthropicTools(tools: ToolWithHandler[]): AnthropicToolDefinition[];
|
|
27
|
-
/**
|
|
28
|
-
* Helper to convert Anthropic tool calls to job code for Granular
|
|
29
|
-
*
|
|
30
|
-
* @param contentBlocks - Array of content blocks from Anthropic response
|
|
31
|
-
* @param customImports - Optional imports to add to top of job
|
|
32
|
-
* @returns generated job code, or null if no tool uses found
|
|
33
|
-
*/
|
|
34
|
-
declare function toolUsesToJobCode(contentBlocks: Array<{
|
|
35
|
-
type: string;
|
|
36
|
-
id?: string;
|
|
37
|
-
name?: string;
|
|
38
|
-
input?: unknown;
|
|
39
|
-
}>, customImports?: string): string | null;
|
|
40
|
-
/**
|
|
41
|
-
* Create a single "codegen tool" for LLM consumption.
|
|
42
|
-
*
|
|
43
|
-
* Instead of passing N individual tools to the LLM, this returns a single
|
|
44
|
-
* tool that accepts TypeScript code. The LLM writes code that uses the
|
|
45
|
-
* available tools, and the code is executed in the sandbox.
|
|
46
|
-
*
|
|
47
|
-
* @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())
|
|
48
|
-
* @returns A single Anthropic tool definition
|
|
49
|
-
*/
|
|
50
|
-
declare function createCodegenTool(domainDocumentation: string): AnthropicToolDefinition;
|
|
51
|
-
/**
|
|
52
|
-
* Helper to execute a codegen tool use block.
|
|
53
|
-
*
|
|
54
|
-
* @param toolUse - The tool_use block from the Anthropic response
|
|
55
|
-
* @param environment - The connected Granular environment
|
|
56
|
-
* @returns The job result
|
|
57
|
-
*/
|
|
58
|
-
declare function executeCodegenToolUse(toolUse: AnthropicToolUseBlock, environment: {
|
|
59
|
-
submitJob: (code: string) => Promise<{
|
|
60
|
-
result: Promise<unknown>;
|
|
61
|
-
}>;
|
|
62
|
-
}): Promise<unknown>;
|
|
63
|
-
|
|
64
|
-
export { type AnthropicToolDefinition, type AnthropicToolUseBlock, createCodegenTool, executeCodegenToolUse, toAnthropicTools, toolUsesToJobCode };
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/adapters/anthropic.ts
|
|
4
|
-
function toAnthropicTools(tools) {
|
|
5
|
-
return tools.map((tool) => ({
|
|
6
|
-
name: tool.name,
|
|
7
|
-
description: tool.description,
|
|
8
|
-
input_schema: tool.inputSchema
|
|
9
|
-
}));
|
|
10
|
-
}
|
|
11
|
-
function toolUsesToJobCode(contentBlocks, customImports = "") {
|
|
12
|
-
const toolUses = contentBlocks.filter((b) => b.type === "tool_use");
|
|
13
|
-
if (toolUses.length === 0) {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
let code = `
|
|
17
|
-
import { tools } from './sandbox-tools';
|
|
18
|
-
${customImports}
|
|
19
|
-
const results = [];
|
|
20
|
-
`;
|
|
21
|
-
toolUses.forEach((use) => {
|
|
22
|
-
code += `
|
|
23
|
-
try {
|
|
24
|
-
const result_${use.id.replace(/-/g, "_")} = await tools.${use.name}(${JSON.stringify(use.input)});
|
|
25
|
-
results.push({
|
|
26
|
-
type: 'tool_result',
|
|
27
|
-
tool_use_id: "${use.id}",
|
|
28
|
-
content: result_${use.id.replace(/-/g, "_")}
|
|
29
|
-
});
|
|
30
|
-
} catch (error) {
|
|
31
|
-
results.push({
|
|
32
|
-
type: 'tool_result',
|
|
33
|
-
tool_use_id: "${use.id}",
|
|
34
|
-
content: String(error),
|
|
35
|
-
is_error: true
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
});
|
|
40
|
-
code += `return results;`;
|
|
41
|
-
return code;
|
|
42
|
-
}
|
|
43
|
-
function createCodegenTool(domainDocumentation) {
|
|
44
|
-
return {
|
|
45
|
-
name: "execute_sandbox_code",
|
|
46
|
-
description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from "./sandbox-tools":
|
|
47
|
-
|
|
48
|
-
${domainDocumentation}
|
|
49
|
-
|
|
50
|
-
Write complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,
|
|
51
|
-
input_schema: {
|
|
52
|
-
type: "object",
|
|
53
|
-
properties: {
|
|
54
|
-
code: {
|
|
55
|
-
type: "string",
|
|
56
|
-
description: 'TypeScript code to execute. Must import tools from "./sandbox-tools" and use await for all tool calls.'
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
required: ["code"]
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
async function executeCodegenToolUse(toolUse, environment) {
|
|
64
|
-
if (toolUse.name !== "execute_sandbox_code") {
|
|
65
|
-
throw new Error(`Unknown tool: ${toolUse.name}`);
|
|
66
|
-
}
|
|
67
|
-
const input = toolUse.input;
|
|
68
|
-
const job = await environment.submitJob(input.code);
|
|
69
|
-
return job.result;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
exports.createCodegenTool = createCodegenTool;
|
|
73
|
-
exports.executeCodegenToolUse = executeCodegenToolUse;
|
|
74
|
-
exports.toAnthropicTools = toAnthropicTools;
|
|
75
|
-
exports.toolUsesToJobCode = toolUsesToJobCode;
|
|
76
|
-
//# sourceMappingURL=anthropic.js.map
|
|
77
|
-
//# sourceMappingURL=anthropic.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":";;;AA2BO,SAAS,iBAAiB,KAAA,EAAqD;AAClF,EAAA,OAAO,KAAA,CAAM,IAAI,CAAA,IAAA,MAAS;AAAA,IACtB,MAAM,IAAA,CAAK,IAAA;AAAA,IACX,aAAa,IAAA,CAAK,WAAA;AAAA,IAClB,cAAc,IAAA,CAAK;AAAA,GACvB,CAAE,CAAA;AACN;AASO,SAAS,iBAAA,CAAkB,aAAA,EAAqF,aAAA,GAAgB,EAAA,EAAmB;AACtJ,EAAA,MAAM,WAAW,aAAA,CAAc,MAAA,CAAO,CAAA,CAAA,KAAK,CAAA,CAAE,SAAS,UAAU,CAAA;AAEhE,EAAA,IAAI,QAAA,CAAS,WAAW,CAAA,EAAG;AACvB,IAAA,OAAO,IAAA;AAAA,EACX;AAEA,EAAA,IAAI,IAAA,GAAO;AAAA;AAAA,QAAA,EAEL,aAAa;AAAA;AAAA,IAAA,CAAA;AAInB,EAAA,QAAA,CAAS,OAAA,CAAQ,CAAC,GAAA,KAAQ;AACtB,IAAA,IAAA,IAAQ;AAAA;AAAA,yBAAA,EAEW,GAAA,CAAI,EAAA,CAAG,OAAA,C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","sourcesContent":["import type { ToolWithHandler } from '../types';\n\n/**\n * Anthropic tool_use block from message response\n */\nexport interface AnthropicToolUseBlock {\n type: 'tool_use';\n id: string;\n name: string;\n input: unknown;\n}\n\n/**\n * Anthropic tool definition\n */\nexport interface AnthropicToolDefinition {\n name: string;\n description?: string;\n input_schema: Record<string, unknown>;\n}\n\n/**\n * Helper to convert Granular tools to Anthropic tool definitions\n * \n * @param tools - Array of Granular tools\n * @returns Array of Anthropic tool definitions\n */\nexport function toAnthropicTools(tools: ToolWithHandler[]): AnthropicToolDefinition[] {\n return tools.map(tool => ({\n name: tool.name,\n description: tool.description,\n input_schema: tool.inputSchema,\n }));\n}\n\n/**\n * Helper to convert Anthropic tool calls to job code for Granular\n * \n * @param contentBlocks - Array of content blocks from Anthropic response\n * @param customImports - Optional imports to add to top of job\n * @returns generated job code, or null if no tool uses found\n */\nexport function toolUsesToJobCode(contentBlocks: Array<{ type: string; id?: string; name?: string; input?: unknown }>, customImports = ''): string | null {\n const toolUses = contentBlocks.filter(b => b.type === 'tool_use') as AnthropicToolUseBlock[];\n\n if (toolUses.length === 0) {\n return null;\n }\n\n let code = `\n import { tools } from './sandbox-tools';\n ${customImports}\n const results = [];\n `;\n\n toolUses.forEach((use) => {\n code += `\n try {\n const result_${use.id.replace(/-/g, '_')} = await tools.${use.name}(${JSON.stringify(use.input)});\n results.push({ \n type: 'tool_result',\n tool_use_id: \"${use.id}\", \n content: result_${use.id.replace(/-/g, '_')} \n });\n } catch (error) {\n results.push({ \n type: 'tool_result',\n tool_use_id: \"${use.id}\", \n content: String(error),\n is_error: true\n });\n }\n `;\n });\n\n code += `return results;`;\n return code;\n}\n\n/**\n * Create a single \"codegen tool\" for LLM consumption.\n * \n * Instead of passing N individual tools to the LLM, this returns a single\n * tool that accepts TypeScript code. The LLM writes code that uses the\n * available tools, and the code is executed in the sandbox.\n * \n * @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())\n * @returns A single Anthropic tool definition\n */\nexport function createCodegenTool(domainDocumentation: string): AnthropicToolDefinition {\n return {\n name: 'execute_sandbox_code',\n description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from \"./sandbox-tools\":\n\n${domainDocumentation}\n\nWrite complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,\n input_schema: {\n type: 'object',\n properties: {\n code: {\n type: 'string',\n description: 'TypeScript code to execute. Must import tools from \"./sandbox-tools\" and use await for all tool calls.',\n },\n },\n required: ['code'],\n },\n };\n}\n\n/**\n * Helper to execute a codegen tool use block.\n * \n * @param toolUse - The tool_use block from the Anthropic response\n * @param environment - The connected Granular environment\n * @returns The job result\n */\nexport async function executeCodegenToolUse(\n toolUse: AnthropicToolUseBlock,\n environment: { submitJob: (code: string) => Promise<{ result: Promise<unknown> }> }\n): Promise<unknown> {\n if (toolUse.name !== 'execute_sandbox_code') {\n throw new Error(`Unknown tool: ${toolUse.name}`);\n }\n\n const input = toolUse.input as { code: string };\n const job = await environment.submitJob(input.code);\n return job.result;\n}\n"]}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// src/adapters/anthropic.ts
|
|
2
|
-
function toAnthropicTools(tools) {
|
|
3
|
-
return tools.map((tool) => ({
|
|
4
|
-
name: tool.name,
|
|
5
|
-
description: tool.description,
|
|
6
|
-
input_schema: tool.inputSchema
|
|
7
|
-
}));
|
|
8
|
-
}
|
|
9
|
-
function toolUsesToJobCode(contentBlocks, customImports = "") {
|
|
10
|
-
const toolUses = contentBlocks.filter((b) => b.type === "tool_use");
|
|
11
|
-
if (toolUses.length === 0) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
let code = `
|
|
15
|
-
import { tools } from './sandbox-tools';
|
|
16
|
-
${customImports}
|
|
17
|
-
const results = [];
|
|
18
|
-
`;
|
|
19
|
-
toolUses.forEach((use) => {
|
|
20
|
-
code += `
|
|
21
|
-
try {
|
|
22
|
-
const result_${use.id.replace(/-/g, "_")} = await tools.${use.name}(${JSON.stringify(use.input)});
|
|
23
|
-
results.push({
|
|
24
|
-
type: 'tool_result',
|
|
25
|
-
tool_use_id: "${use.id}",
|
|
26
|
-
content: result_${use.id.replace(/-/g, "_")}
|
|
27
|
-
});
|
|
28
|
-
} catch (error) {
|
|
29
|
-
results.push({
|
|
30
|
-
type: 'tool_result',
|
|
31
|
-
tool_use_id: "${use.id}",
|
|
32
|
-
content: String(error),
|
|
33
|
-
is_error: true
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
`;
|
|
37
|
-
});
|
|
38
|
-
code += `return results;`;
|
|
39
|
-
return code;
|
|
40
|
-
}
|
|
41
|
-
function createCodegenTool(domainDocumentation) {
|
|
42
|
-
return {
|
|
43
|
-
name: "execute_sandbox_code",
|
|
44
|
-
description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from "./sandbox-tools":
|
|
45
|
-
|
|
46
|
-
${domainDocumentation}
|
|
47
|
-
|
|
48
|
-
Write complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,
|
|
49
|
-
input_schema: {
|
|
50
|
-
type: "object",
|
|
51
|
-
properties: {
|
|
52
|
-
code: {
|
|
53
|
-
type: "string",
|
|
54
|
-
description: 'TypeScript code to execute. Must import tools from "./sandbox-tools" and use await for all tool calls.'
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
required: ["code"]
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
async function executeCodegenToolUse(toolUse, environment) {
|
|
62
|
-
if (toolUse.name !== "execute_sandbox_code") {
|
|
63
|
-
throw new Error(`Unknown tool: ${toolUse.name}`);
|
|
64
|
-
}
|
|
65
|
-
const input = toolUse.input;
|
|
66
|
-
const job = await environment.submitJob(input.code);
|
|
67
|
-
return job.result;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export { createCodegenTool, executeCodegenToolUse, toAnthropicTools, toolUsesToJobCode };
|
|
71
|
-
//# sourceMappingURL=anthropic.mjs.map
|
|
72
|
-
//# sourceM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,CAAA;AACR,EAAA,OAAO,IAAA;AACX;AAYO,SAAS,kBAAkB,mBAAA,EAAsD;AACpF,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,sBAAA;AAAA,IACN,WAAA,EAAa,CAAA;;AAAA,EAEnB,mBAAmB;;AAAA,sGAAA,CAAA;AAAA,IAGb,YAAA,EAAc;AAAA,MACV,IAAA,EAAM,QAAA;AAAA,MACN,UAAA,EAAY;AAAA,QACR,IAAA,EAAM;AAAA,UACF,IAAA,EAAM,QAAA;AAAA,UACN,WAAA,EAAa;AAAA;AACjB,OACJ;AAAA,MACA,QAAA,EAAU,CAAC,MAAM;AAAA;AACrB,GACJ;AACJ;AASA,eAAsB,qBAAA,CAClB,SACA,WAAA,EACgB;AAChB,EAAA,IAAI,OAAA,CAAQ,SAAS,sBAAA,EAAwB;AACzC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,cAAA,EAAiB,OAAA,CAAQ,IAAI,CAAA,CAAE,CAAA;AAAA,EACnD;AAEA,EAAA,MAAM,QAAQ,OAAA,CAAQ,KAAA;AACtB,EAAA,MAAM,GAAA,GAAM,MAAM,WAAA,CAAY,SAAA,CAAU,MAAM,IAAI,CAAA;AAClD,EAAA,OAAO,GAAA,CAAI,MAAA;AACf","file":"anthropic.mjs","sourcesContent":["import type { ToolWithHandler } from '../types';\n\n/**\n * Anthropic tool_use block from message response\n */\nexport interface AnthropicToolUseBlock {\n type: 'tool_use';\n id: string;\n name: string;\n input: unknown;\n}\n\n/**\n * Anthropic tool definition\n */\nexport interface AnthropicToolDefinition {\n name: string;\n description?: string;\n input_schema: Record<string, unknown>;\n}\n\n/**\n * Helper to convert Granular tools to Anthropic tool definitions\n * \n * @param tools - Array of Granular tools\n * @returns Array of Anthropic tool definitions\n */\nexport function toAnthropicTools(tools: ToolWithHandler[]): AnthropicToolDefinition[] {\n return tools.map(tool => ({\n name: tool.name,\n description: tool.description,\n input_schema: tool.inputSchema,\n }));\n}\n\n/**\n * Helper to convert Anthropic tool calls to job code for Granular\n * \n * @param contentBlocks - Array of content blocks from Anthropic response\n * @param customImports - Optional imports to add to top of job\n * @returns generated job code, or null if no tool uses found\n */\nexport function toolUsesToJobCode(contentBlocks: Array<{ type: string; id?: string; name?: string; input?: unknown }>, customImports = ''): string | null {\n const toolUses = contentBlocks.filter(b => b.type === 'tool_use') as AnthropicToolUseBlock[];\n\n if (toolUses.length === 0) {\n return null;\n }\n\n let code = `\n import { tools } from './sandbox-tools';\n ${customImports}\n const results = [];\n `;\n\n toolUses.forEach((use) => {\n code += `\n try {\n const result_${use.id.replace(/-/g, '_')} = await tools.${use.name}(${JSON.stringify(use.input)});\n results.push({ \n type: 'tool_result',\n tool_use_id: \"${use.id}\", \n content: result_${use.id.replace(/-/g, '_')} \n });\n } catch (error) {\n results.push({ \n type: 'tool_result',\n tool_use_id: \"${use.id}\", \n content: String(error),\n is_error: true\n });\n }\n `;\n });\n\n code += `return results;`;\n return code;\n}\n\n/**\n * Create a single \"codegen tool\" for LLM consumption.\n * \n * Instead of passing N individual tools to the LLM, this returns a single\n * tool that accepts TypeScript code. The LLM writes code that uses the\n * available tools, and the code is executed in the sandbox.\n * \n * @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())\n * @returns A single Anthropic tool definition\n */\nexport function createCodegenTool(domainDocumentation: string): AnthropicToolDefinition {\n return {\n name: 'execute_sandbox_code',\n description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from \"./sandbox-tools\":\n\n${domainDocumentation}\n\nWrite complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,\n input_schema: {\n type: 'object',\n properties: {\n code: {\n type: 'string',\n description: 'TypeScript code to execute. Must import tools from \"./sandbox-tools\" and use await for all tool calls.',\n },\n },\n required: ['code'],\n },\n };\n}\n\n/**\n * Helper to execute a codegen tool use block.\n * \n * @param toolUse - The tool_use block from the Anthropic response\n * @param environment - The connected Granular environment\n * @returns The job result\n */\nexport async function executeCodegenToolUse(\n toolUse: AnthropicToolUseBlock,\n environment: { submitJob: (code: string) => Promise<{ result: Promise<unknown> }> }\n): Promise<unknown> {\n if (toolUse.name !== 'execute_sandbox_code') {\n throw new Error(`Unknown tool: ${toolUse.name}`);\n }\n\n const input = toolUse.input as { code: string };\n const job = await environment.submitJob(input.code);\n return job.result;\n}\n"]}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { StructuredTool } from '@langchain/core/tools';
|
|
2
|
-
import { T as ToolWithHandler } from '../types-C0AVRsVR.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Helper to convert LangChain tools to Granular tools.
|
|
6
|
-
*
|
|
7
|
-
* Note: Since Granular tools execute handlers in the wrapper/client process
|
|
8
|
-
* via reverse-RPC, we can just wrap the LangChain tool's call method directly.
|
|
9
|
-
*
|
|
10
|
-
* @param langchainTools - Array of LangChain StructuredTools
|
|
11
|
-
* @returns Array of Granular ToolWithHandler
|
|
12
|
-
*/
|
|
13
|
-
declare function fromLangChainTools(langchainTools: StructuredTool[]): ToolWithHandler[];
|
|
14
|
-
/**
|
|
15
|
-
* Create a single "codegen tool" for LangChain agent consumption.
|
|
16
|
-
*
|
|
17
|
-
* Instead of passing N individual tools to the agent, this returns a single
|
|
18
|
-
* tool that accepts TypeScript code. The LLM writes code that uses the
|
|
19
|
-
* available tools, and the code is executed in the sandbox.
|
|
20
|
-
*
|
|
21
|
-
* @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())
|
|
22
|
-
* @param environment - The connected Granular environment
|
|
23
|
-
* @returns A LangChain StructuredTool
|
|
24
|
-
*/
|
|
25
|
-
declare function createCodegenTool(domainDocumentation: string, environment: {
|
|
26
|
-
submitJob: (code: string) => Promise<{
|
|
27
|
-
result: Promise<unknown>;
|
|
28
|
-
}>;
|
|
29
|
-
}): ToolWithHandler;
|
|
30
|
-
|
|
31
|
-
export { createCodegenTool, fromLangChainTools };
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { StructuredTool } from '@langchain/core/tools';
|
|
2
|
-
import { T as ToolWithHandler } from '../types-C0AVRsVR.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Helper to convert LangChain tools to Granular tools.
|
|
6
|
-
*
|
|
7
|
-
* Note: Since Granular tools execute handlers in the wrapper/client process
|
|
8
|
-
* via reverse-RPC, we can just wrap the LangChain tool's call method directly.
|
|
9
|
-
*
|
|
10
|
-
* @param langchainTools - Array of LangChain StructuredTools
|
|
11
|
-
* @returns Array of Granular ToolWithHandler
|
|
12
|
-
*/
|
|
13
|
-
declare function fromLangChainTools(langchainTools: StructuredTool[]): ToolWithHandler[];
|
|
14
|
-
/**
|
|
15
|
-
* Create a single "codegen tool" for LangChain agent consumption.
|
|
16
|
-
*
|
|
17
|
-
* Instead of passing N individual tools to the agent, this returns a single
|
|
18
|
-
* tool that accepts TypeScript code. The LLM writes code that uses the
|
|
19
|
-
* available tools, and the code is executed in the sandbox.
|
|
20
|
-
*
|
|
21
|
-
* @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())
|
|
22
|
-
* @param environment - The connected Granular environment
|
|
23
|
-
* @returns A LangChain StructuredTool
|
|
24
|
-
*/
|
|
25
|
-
declare function createCodegenTool(domainDocumentation: string, environment: {
|
|
26
|
-
submitJob: (code: string) => Promise<{
|
|
27
|
-
result: Promise<unknown>;
|
|
28
|
-
}>;
|
|
29
|
-
}): ToolWithHandler;
|
|
30
|
-
|
|
31
|
-
export { createCodegenTool, fromLangChainTools };
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var zodToJsonSchema = require('zod-to-json-schema');
|
|
4
|
-
|
|
5
|
-
// src/adapters/langchain.ts
|
|
6
|
-
function fromLangChainTools(langchainTools) {
|
|
7
|
-
return langchainTools.map((tool) => {
|
|
8
|
-
const jsonSchema = zodToJsonSchema.zodToJsonSchema(tool.schema);
|
|
9
|
-
const inputSchema = jsonSchema.properties ? jsonSchema : jsonSchema.definitions ? jsonSchema.definitions[Object.keys(jsonSchema.definitions)[0]] : jsonSchema;
|
|
10
|
-
return {
|
|
11
|
-
name: tool.name,
|
|
12
|
-
description: tool.description,
|
|
13
|
-
inputSchema,
|
|
14
|
-
handler: async (input) => {
|
|
15
|
-
return tool.invoke(input);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
function createCodegenTool(domainDocumentation, environment) {
|
|
21
|
-
return {
|
|
22
|
-
name: "sess",
|
|
23
|
-
description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from "./sandbox-tools":
|
|
24
|
-
|
|
25
|
-
${domainDocumentation}
|
|
26
|
-
|
|
27
|
-
Write complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,
|
|
28
|
-
inputSchema: {
|
|
29
|
-
type: "object",
|
|
30
|
-
properties: {
|
|
31
|
-
code: {
|
|
32
|
-
type: "string",
|
|
33
|
-
description: 'TypeScript code to execute. Must import tools from "./sandbox-tools" and use await for all tool calls.'
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
required: ["code"]
|
|
37
|
-
},
|
|
38
|
-
handler: async (input) => {
|
|
39
|
-
const { code } = input;
|
|
40
|
-
const job = await environment.submitJob(code);
|
|
41
|
-
return job.result;
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
exports.createCodegenTool = createCodegenTool;
|
|
47
|
-
exports.fromLangChainTools = fromLangChainTools;
|
|
48
|
-
//# sourceMappingURL=langchain.js.map
|
|
49
|
-
//# sourceMappingURL=langchain.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/langchain.ts"],"names":["zodToJsonSchema"],"mappings":";;;;;AAcO,SAAS,mBAAmB,cAAA,EAAqD;AACpF,EAAA,OAAO,cAAA,CAAe,GAAA,CAAI,CAAC,IAAA,KAAS;AAEhC,IAAA,MAAM,UAAA,GAAkBA,+BAAA,CAAgB,IAAA,CAAK,MAAM,CAAA;AAGnD,IAAA,MAAM,WAAA,GAAc,UAAA,CAAW,UAAA,GAAa,UAAA,GAAc,WAAW,WAAA,GAAc,UAAA,CAAW,WAAA,CAAY,MAAA,CAAO,KAAK,UAAA,CAAW,WAAW,CAAA,CAAE,CAAC,CAAC,CAAA,GAAI,UAAA;AAEpJ,IAAA,OAAO;AAAA,MACH,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,aAAa,IAAA,CAAK,WAAA;AAAA,MAClB,WAAA;AAAA,MACA,OAAA,EAAS,OAAO,KAAA,KAAe;AAG3B,QAAA,OAAO,IAAA,CAAK,OAAO,KAAY,CAAA;AAAA,MACnC;AAAA,KACJ;AAAA,EACJ,CAAC,CAAA;AACL;AAaO,SAAS,iBAAA,CACZ,qBACA,WAAA,EACe;AACf,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,MAAA;AAAA,IACN,WAAA,EAAa,CAAA;;AAAA,EAEnB,mBAAmB;;AAAA,sGAAA,CAAA;AAAA,IAGb,WAAA,EAAa;AAAA,MACT,IAAA,EAAM,QAAA;AAAA,MACN,UAAA,EAAY;AAAA,QACR,IAAA,EAAM;AAAA,UACF,IAAA,EAAM,QAAA;AAAA,UACN,WAAA,EAAa;AAAA;AACjB,OACJ;AAAA,MACA,QAAA,EAAU,CAAC,MAAM;AAAA,KACrB;AAAA,IACA,OAAA,EAAS,OAAO,KAAA,KAAe;AAC3B,MAAA,MAAM,EAAE,MAAK,GAAI,KAAA;AACjB,MAAA,MAAM,GAAA,GAAM,MAAM,WAAA,CAAY,SAAA,CAAU,IAAI,CAAA;AAC5C,MAAA,OAAO,GAAA,CAAI,MAAA;AAAA,IACf;AAAA,GACJ;AACJ","file":"langchain.js","sourcesContent":["import { z } from 'zod';\nimport { StructuredTool } from '@langchain/core/tools';\nimport type { ToolWithHandler } from '../types';\nimport { zodToJsonSchema } from 'zod-to-json-schema';\n\n/**\n * Helper to convert LangChain tools to Granular tools.\n * \n * Note: Since Granular tools execute handlers in the wrapper/client process\n * via reverse-RPC, we can just wrap the LangChain tool's call method directly.\n * \n * @param langchainTools - Array of LangChain StructuredTools\n * @returns Array of Granular ToolWithHandler\n */\nexport function fromLangChainTools(langchainTools: StructuredTool[]): ToolWithHandler[] {\n return langchainTools.map((tool) => {\n // Zod to JSON Schema conversion\n const jsonSchema: any = zodToJsonSchema(tool.schema);\n // Remove $schema and properties wrapper if present, simplify for tool definition\n // This is a simplified conversion, might need adjustment based on specific Zod schemas\n const inputSchema = jsonSchema.properties ? jsonSchema : (jsonSchema.definitions ? jsonSchema.definitions[Object.keys(jsonSchema.definitions)[0]] : jsonSchema);\n\n return {\n name: tool.name,\n description: tool.description,\n inputSchema: inputSchema as Record<string, unknown>,\n handler: async (input: any) => {\n // Determine if input is a string or object based on expectations\n // LangChain tools expect specific inputs\n return tool.invoke(input as any);\n },\n };\n });\n}\n\n/**\n * Create a single \"codegen tool\" for LangChain agent consumption.\n * \n * Instead of passing N individual tools to the agent, this returns a single\n * tool that accepts TypeScript code. The LLM writes code that uses the\n * available tools, and the code is executed in the sandbox.\n * \n * @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())\n * @param environment - The connected Granular environment\n * @returns A LangChain StructuredTool\n */\nexport function createCodegenTool(\n domainDocumentation: string,\n environment: { submitJob: (code: string) => Promise<{ result: Promise<unknown> }> }\n): ToolWithHandler {\n return {\n name: 'sess',\n description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from \"./sandbox-tools\":\n\n${domainDocumentation}\n\nWrite complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,\n inputSchema: {\n type: 'object',\n properties: {\n code: {\n type: 'string',\n description: 'TypeScript code to execute. Must import tools from \"./sandbox-tools\" and use await for all tool calls.',\n },\n },\n required: ['code'],\n },\n handler: async (input: any) => {\n const { code } = input as { code: string };\n const job = await environment.submitJob(code);\n return job.result;\n },\n };\n}\n"]}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
2
|
-
|
|
3
|
-
// src/adapters/langchain.ts
|
|
4
|
-
function fromLangChainTools(langchainTools) {
|
|
5
|
-
return langchainTools.map((tool) => {
|
|
6
|
-
const jsonSchema = zodToJsonSchema(tool.schema);
|
|
7
|
-
const inputSchema = jsonSchema.properties ? jsonSchema : jsonSchema.definitions ? jsonSchema.definitions[Object.keys(jsonSchema.definitions)[0]] : jsonSchema;
|
|
8
|
-
return {
|
|
9
|
-
name: tool.name,
|
|
10
|
-
description: tool.description,
|
|
11
|
-
inputSchema,
|
|
12
|
-
handler: async (input) => {
|
|
13
|
-
return tool.invoke(input);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
function createCodegenTool(domainDocumentation, environment) {
|
|
19
|
-
return {
|
|
20
|
-
name: "sess",
|
|
21
|
-
description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from "./sandbox-tools":
|
|
22
|
-
|
|
23
|
-
${domainDocumentation}
|
|
24
|
-
|
|
25
|
-
Write complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,
|
|
26
|
-
inputSchema: {
|
|
27
|
-
type: "object",
|
|
28
|
-
properties: {
|
|
29
|
-
code: {
|
|
30
|
-
type: "string",
|
|
31
|
-
description: 'TypeScript code to execute. Must import tools from "./sandbox-tools" and use await for all tool calls.'
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
required: ["code"]
|
|
35
|
-
},
|
|
36
|
-
handler: async (input) => {
|
|
37
|
-
const { code } = input;
|
|
38
|
-
const job = await environment.submitJob(code);
|
|
39
|
-
return job.result;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export { createCodegenTool, fromLangChainTools };
|
|
45
|
-
//# sourceMappingURL=langchain.mjs.map
|
|
46
|
-
//# sourceMappingURL=langchain.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3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ACJ;AACJ","file":"langchain.mjs","sourcesContent":["import { z } from 'zod';\nimport { StructuredTool } from '@langchain/core/tools';\nimport type { ToolWithHandler } from '../types';\nimport { zodToJsonSchema } from 'zod-to-json-schema';\n\n/**\n * Helper to convert LangChain tools to Granular tools.\n * \n * Note: Since Granular tools execute handlers in the wrapper/client process\n * via reverse-RPC, we can just wrap the LangChain tool's call method directly.\n * \n * @param langchainTools - Array of LangChain StructuredTools\n * @returns Array of Granular ToolWithHandler\n */\nexport function fromLangChainTools(langchainTools: StructuredTool[]): ToolWithHandler[] {\n return langchainTools.map((tool) => {\n // Zod to JSON Schema conversion\n const jsonSchema: any = zodToJsonSchema(tool.schema);\n // Remove $schema and properties wrapper if present, simplify for tool definition\n // This is a simplified conversion, might need adjustment based on specific Zod schemas\n const inputSchema = jsonSchema.properties ? jsonSchema : (jsonSchema.definitions ? jsonSchema.definitions[Object.keys(jsonSchema.definitions)[0]] : jsonSchema);\n\n return {\n name: tool.name,\n description: tool.description,\n inputSchema: inputSchema as Record<string, unknown>,\n handler: async (input: any) => {\n // Determine if input is a string or object based on expectations\n // LangChain tools expect specific inputs\n return tool.invoke(input as any);\n },\n };\n });\n}\n\n/**\n * Create a single \"codegen tool\" for LangChain agent consumption.\n * \n * Instead of passing N individual tools to the agent, this returns a single\n * tool that accepts TypeScript code. The LLM writes code that uses the\n * available tools, and the code is executed in the sandbox.\n * \n * @param domainDocumentation - Documentation of available tools (from environment.getDomainDocumentation())\n * @param environment - The connected Granular environment\n * @returns A LangChain StructuredTool\n */\nexport function createCodegenTool(\n domainDocumentation: string,\n environment: { submitJob: (code: string) => Promise<{ result: Promise<unknown> }> }\n): ToolWithHandler {\n return {\n name: 'sess',\n description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from \"./sandbox-tools\":\n\n${domainDocumentation}\n\nWrite complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,\n inputSchema: {\n type: 'object',\n properties: {\n code: {\n type: 'string',\n description: 'TypeScript code to execute. Must import tools from \"./sandbox-tools\" and use await for all tool calls.',\n },\n },\n required: ['code'],\n },\n handler: async (input: any) => {\n const { code } = input as { code: string };\n const job = await environment.submitJob(code);\n return job.result;\n },\n };\n}\n"]}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { T as ToolWithHandler } from '../types-C0AVRsVR.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Mastra tool definition interface
|
|
6
|
-
*/
|
|
7
|
-
interface MastraTool {
|
|
8
|
-
id: string;
|
|
9
|
-
description?: string;
|
|
10
|
-
inputSchema?: z.ZodType<unknown>;
|
|
11
|
-
execute?: (context: {
|
|
12
|
-
context: unknown;
|
|
13
|
-
}) => Promise<unknown>;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Helper to convert Mastra tools to Granular tools
|
|
17
|
-
*
|
|
18
|
-
* @param mastraTools - Array of Mastra tools
|
|
19
|
-
* @returns Array of Granular ToolWithHandler
|
|
20
|
-
*/
|
|
21
|
-
declare function fromMastraTools(mastraTools: MastraTool[]): ToolWithHandler[];
|
|
22
|
-
|
|
23
|
-
export { type MastraTool, fromMastraTools };
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { T as ToolWithHandler } from '../types-C0AVRsVR.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Mastra tool definition interface
|
|
6
|
-
*/
|
|
7
|
-
interface MastraTool {
|
|
8
|
-
id: string;
|
|
9
|
-
description?: string;
|
|
10
|
-
inputSchema?: z.ZodType<unknown>;
|
|
11
|
-
execute?: (context: {
|
|
12
|
-
context: unknown;
|
|
13
|
-
}) => Promise<unknown>;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Helper to convert Mastra tools to Granular tools
|
|
17
|
-
*
|
|
18
|
-
* @param mastraTools - Array of Mastra tools
|
|
19
|
-
* @returns Array of Granular ToolWithHandler
|
|
20
|
-
*/
|
|
21
|
-
declare function fromMastraTools(mastraTools: MastraTool[]): ToolWithHandler[];
|
|
22
|
-
|
|
23
|
-
export { type MastraTool, fromMastraTools };
|
package/dist/adapters/mastra.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var zodToJsonSchema = require('zod-to-json-schema');
|
|
4
|
-
|
|
5
|
-
// src/adapters/mastra.ts
|
|
6
|
-
function fromMastraTools(mastraTools) {
|
|
7
|
-
return mastraTools.map((tool) => {
|
|
8
|
-
const jsonSchema = tool.inputSchema ? zodToJsonSchema.zodToJsonSchema(tool.inputSchema) : {};
|
|
9
|
-
return {
|
|
10
|
-
name: tool.id,
|
|
11
|
-
// Mastra uses ID as name
|
|
12
|
-
description: tool.description || "",
|
|
13
|
-
inputSchema: jsonSchema,
|
|
14
|
-
handler: async (input) => {
|
|
15
|
-
if (tool.execute) {
|
|
16
|
-
return tool.execute({ context: input });
|
|
17
|
-
}
|
|
18
|
-
return {};
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
exports.fromMastraTools = fromMastraTools;
|
|
25
|
-
//# sourceMappingURL=mastra.js.map
|
|
26
|
-
//# sourceMappingURL=mastra.js.map
|