@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.
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/adapters/mastra.ts"],"names":["zodToJsonSchema"],"mappings":";;;;;AAoBO,SAAS,gBAAgB,WAAA,EAA8C;AAC1E,EAAA,OAAO,WAAA,CAAY,GAAA,CAAI,CAAC,IAAA,KAAS;AAC7B,IAAA,MAAM,aAAa,IAAA,CAAK,WAAA,GAAcA,gCAAgB,IAAA,CAAK,WAAW,IAAI,EAAC;AAE3E,IAAA,OAAO;AAAA,MACH,MAAM,IAAA,CAAK,EAAA;AAAA;AAAA,MACX,WAAA,EAAa,KAAK,WAAA,IAAe,EAAA;AAAA,MACjC,WAAA,EAAa,UAAA;AAAA,MACb,OAAA,EAAS,OAAO,KAAA,KAAe;AAC3B,QAAA,IAAI,KAAK,OAAA,EAAS;AACd,UAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,EAAE,OAAA,EAAS,OAAO,CAAA;AAAA,QAC1C;AACA,QAAA,OAAO,EAAC;AAAA,MACZ;AAAA,KACJ;AAAA,EACJ,CAAC,CAAA;AACL","file":"mastra.js","sourcesContent":["import { z } from 'zod';\nimport type { ToolWithHandler } from '../types';\nimport { zodToJsonSchema } from 'zod-to-json-schema';\n\n/**\n * Mastra tool definition interface\n */\nexport interface MastraTool {\n id: string;\n description?: string;\n inputSchema?: z.ZodType<unknown>;\n execute?: (context: { context: unknown }) => Promise<unknown>;\n}\n\n/**\n * Helper to convert Mastra tools to Granular tools\n * \n * @param mastraTools - Array of Mastra tools\n * @returns Array of Granular ToolWithHandler\n */\nexport function fromMastraTools(mastraTools: MastraTool[]): ToolWithHandler[] {\n return mastraTools.map((tool) => {\n const jsonSchema = tool.inputSchema ? zodToJsonSchema(tool.inputSchema) : {};\n\n return {\n name: tool.id, // Mastra uses ID as name\n description: tool.description || '',\n inputSchema: jsonSchema as Record<string, unknown>,\n handler: async (input: any) => {\n if (tool.execute) {\n return tool.execute({ context: input });\n }\n return {};\n },\n };\n });\n}\n"]}
@@ -1,24 +0,0 @@
1
- import { zodToJsonSchema } from 'zod-to-json-schema';
2
-
3
- // src/adapters/mastra.ts
4
- function fromMastraTools(mastraTools) {
5
- return mastraTools.map((tool) => {
6
- const jsonSchema = tool.inputSchema ? zodToJsonSchema(tool.inputSchema) : {};
7
- return {
8
- name: tool.id,
9
- // Mastra uses ID as name
10
- description: tool.description || "",
11
- inputSchema: jsonSchema,
12
- handler: async (input) => {
13
- if (tool.execute) {
14
- return tool.execute({ context: input });
15
- }
16
- return {};
17
- }
18
- };
19
- });
20
- }
21
-
22
- export { fromMastraTools };
23
- //# sourceMappingURL=mastra.mjs.map
24
- //# sourceMappingURL=mastra.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/adapters/mastra.ts"],"names":[],"mappings":";;;AAoBO,SAAS,gBAAgB,WAAA,EAA8C;AAC1E,EAAA,OAAO,WAAA,CAAY,GAAA,CAAI,CAAC,IAAA,KAAS;AAC7B,IAAA,MAAM,aAAa,IAAA,CAAK,WAAA,GAAc,gBAAgB,IAAA,CAAK,WAAW,IAAI,EAAC;AAE3E,IAAA,OAAO;AAAA,MACH,MAAM,IAAA,CAAK,EAAA;AAAA;AAAA,MACX,WAAA,EAAa,KAAK,WAAA,IAAe,EAAA;AAAA,MACjC,WAAA,EAAa,UAAA;AAAA,MACb,OAAA,EAAS,OAAO,KAAA,KAAe;AAC3B,QAAA,IAAI,KAAK,OAAA,EAAS;AACd,UAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,EAAE,OAAA,EAAS,OAAO,CAAA;AAAA,QAC1C;AACA,QAAA,OAAO,EAAC;AAAA,MACZ;AAAA,KACJ;AAAA,EACJ,CAAC,CAAA;AACL","file":"mastra.mjs","sourcesContent":["import { z } from 'zod';\nimport type { ToolWithHandler } from '../types';\nimport { zodToJsonSchema } from 'zod-to-json-schema';\n\n/**\n * Mastra tool definition interface\n */\nexport interface MastraTool {\n id: string;\n description?: string;\n inputSchema?: z.ZodType<unknown>;\n execute?: (context: { context: unknown }) => Promise<unknown>;\n}\n\n/**\n * Helper to convert Mastra tools to Granular tools\n * \n * @param mastraTools - Array of Mastra tools\n * @returns Array of Granular ToolWithHandler\n */\nexport function fromMastraTools(mastraTools: MastraTool[]): ToolWithHandler[] {\n return mastraTools.map((tool) => {\n const jsonSchema = tool.inputSchema ? zodToJsonSchema(tool.inputSchema) : {};\n\n return {\n name: tool.id, // Mastra uses ID as name\n description: tool.description || '',\n inputSchema: jsonSchema as Record<string, unknown>,\n handler: async (input: any) => {\n if (tool.execute) {\n return tool.execute({ context: input });\n }\n return {};\n },\n };\n });\n}\n"]}
@@ -1,76 +0,0 @@
1
- import { T as ToolWithHandler } from '../types-C0AVRsVR.mjs';
2
-
3
- /**
4
- * OpenAI tool call from chat completion response
5
- */
6
- interface OpenAIToolCall {
7
- id: string;
8
- type: 'function';
9
- function: {
10
- name: string;
11
- arguments: string;
12
- };
13
- }
14
- /**
15
- * OpenAI tool definition type
16
- */
17
- interface OpenAIToolDefinition {
18
- type: 'function';
19
- function: {
20
- name: string;
21
- description?: string;
22
- parameters?: Record<string, unknown>;
23
- };
24
- }
25
- /**
26
- * Helper to convert Granular tools to OpenAI tool definitions
27
- *
28
- * @param tools - Array of Granular tools
29
- * @returns Array of OpenAI tool definitions
30
- */
31
- declare function toOpenAITools(tools: ToolWithHandler[]): OpenAIToolDefinition[];
32
- /**
33
- * Helper to convert OpenAI tool calls to job code for Granular
34
- *
35
- * @param toolCalls - Array of tool calls from OpenAI response
36
- * @param customImports - Optional imports to add to top of job
37
- * @returns generated job code
38
- */
39
- declare function toolCallsToJobCode(toolCalls: OpenAIToolCall[], customImports?: string): string;
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 OpenAI tool definition
49
- *
50
- * @example
51
- * ```typescript
52
- * const docs = await environment.getDomainDocumentation();
53
- * const codegenTool = createCodegenTool(docs);
54
- *
55
- * const response = await openai.chat.completions.create({
56
- * model: 'gpt-4',
57
- * messages: [...],
58
- * tools: [codegenTool],
59
- * });
60
- * ```
61
- */
62
- declare function createCodegenTool(domainDocumentation: string): OpenAIToolDefinition;
63
- /**
64
- * Helper to execute a codegen tool call result.
65
- *
66
- * @param toolCall - The tool call from the OpenAI response
67
- * @param environment - The connected Granular environment
68
- * @returns The job result
69
- */
70
- declare function executeCodegenToolCall(toolCall: OpenAIToolCall, environment: {
71
- submitJob: (code: string) => Promise<{
72
- result: Promise<unknown>;
73
- }>;
74
- }): Promise<unknown>;
75
-
76
- export { type OpenAIToolCall, type OpenAIToolDefinition, createCodegenTool, executeCodegenToolCall, toOpenAITools, toolCallsToJobCode };
@@ -1,76 +0,0 @@
1
- import { T as ToolWithHandler } from '../types-C0AVRsVR.js';
2
-
3
- /**
4
- * OpenAI tool call from chat completion response
5
- */
6
- interface OpenAIToolCall {
7
- id: string;
8
- type: 'function';
9
- function: {
10
- name: string;
11
- arguments: string;
12
- };
13
- }
14
- /**
15
- * OpenAI tool definition type
16
- */
17
- interface OpenAIToolDefinition {
18
- type: 'function';
19
- function: {
20
- name: string;
21
- description?: string;
22
- parameters?: Record<string, unknown>;
23
- };
24
- }
25
- /**
26
- * Helper to convert Granular tools to OpenAI tool definitions
27
- *
28
- * @param tools - Array of Granular tools
29
- * @returns Array of OpenAI tool definitions
30
- */
31
- declare function toOpenAITools(tools: ToolWithHandler[]): OpenAIToolDefinition[];
32
- /**
33
- * Helper to convert OpenAI tool calls to job code for Granular
34
- *
35
- * @param toolCalls - Array of tool calls from OpenAI response
36
- * @param customImports - Optional imports to add to top of job
37
- * @returns generated job code
38
- */
39
- declare function toolCallsToJobCode(toolCalls: OpenAIToolCall[], customImports?: string): string;
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 OpenAI tool definition
49
- *
50
- * @example
51
- * ```typescript
52
- * const docs = await environment.getDomainDocumentation();
53
- * const codegenTool = createCodegenTool(docs);
54
- *
55
- * const response = await openai.chat.completions.create({
56
- * model: 'gpt-4',
57
- * messages: [...],
58
- * tools: [codegenTool],
59
- * });
60
- * ```
61
- */
62
- declare function createCodegenTool(domainDocumentation: string): OpenAIToolDefinition;
63
- /**
64
- * Helper to execute a codegen tool call result.
65
- *
66
- * @param toolCall - The tool call from the OpenAI response
67
- * @param environment - The connected Granular environment
68
- * @returns The job result
69
- */
70
- declare function executeCodegenToolCall(toolCall: OpenAIToolCall, environment: {
71
- submitJob: (code: string) => Promise<{
72
- result: Promise<unknown>;
73
- }>;
74
- }): Promise<unknown>;
75
-
76
- export { type OpenAIToolCall, type OpenAIToolDefinition, createCodegenTool, executeCodegenToolCall, toOpenAITools, toolCallsToJobCode };
@@ -1,81 +0,0 @@
1
- 'use strict';
2
-
3
- // src/adapters/openai.ts
4
- function toOpenAITools(tools) {
5
- return tools.map((tool) => ({
6
- type: "function",
7
- function: {
8
- name: tool.name,
9
- description: tool.description,
10
- parameters: tool.inputSchema
11
- }
12
- }));
13
- }
14
- function toolCallsToJobCode(toolCalls, customImports = "") {
15
- if (!toolCalls || toolCalls.length === 0) {
16
- return "";
17
- }
18
- let code = `
19
- import { tools } from './sandbox-tools';
20
- ${customImports}
21
- const results = [];
22
- `;
23
- toolCalls.forEach((call) => {
24
- code += `
25
- try {
26
- const result_${call.id.replace(/-/g, "_")} = await tools.${call.function.name}(${call.function.arguments});
27
- results.push({
28
- toolCallId: "${call.id}",
29
- tool: "${call.function.name}",
30
- result: result_${call.id.replace(/-/g, "_")}
31
- });
32
- } catch (error) {
33
- results.push({
34
- toolCallId: "${call.id}",
35
- tool: "${call.function.name}",
36
- error: String(error)
37
- });
38
- }
39
- `;
40
- });
41
- code += `return results;`;
42
- return code;
43
- }
44
- function createCodegenTool(domainDocumentation) {
45
- return {
46
- type: "function",
47
- function: {
48
- name: "execute_sandbox_code",
49
- description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from "./sandbox-tools":
50
-
51
- ${domainDocumentation}
52
-
53
- Write complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,
54
- parameters: {
55
- type: "object",
56
- properties: {
57
- code: {
58
- type: "string",
59
- description: 'TypeScript code to execute. Must import tools from "./sandbox-tools" and use await for all tool calls.'
60
- }
61
- },
62
- required: ["code"]
63
- }
64
- }
65
- };
66
- }
67
- async function executeCodegenToolCall(toolCall, environment) {
68
- if (toolCall.function.name !== "execute_sandbox_code") {
69
- throw new Error(`Unknown tool: ${toolCall.function.name}`);
70
- }
71
- const args = JSON.parse(toolCall.function.arguments);
72
- const job = await environment.submitJob(args.code);
73
- return job.result;
74
- }
75
-
76
- exports.createCodegenTool = createCodegenTool;
77
- exports.executeCodegenToolCall = executeCodegenToolCall;
78
- exports.toOpenAITools = toOpenAITools;
79
- exports.toolCallsToJobCode = toolCallsToJobCode;
80
- //# sourceMappingURL=openai.js.map
81
- //# sourceMappingURL=openai.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/adapters/openai.ts"],"names":[],"mappings":";;;AAgCO,SAAS,cAAc,KAAA,EAAkD;AAC5E,EAAA,OAAO,KAAA,CAAM,IAAI,CAAA,IAAA,MAAS;AAAA,IACtB,IAAA,EAAM,UAAA;AAAA,IACN,QAAA,EAAU;AAAA,MACN,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,aAAa,IAAA,CAAK,WAAA;AAAA,MAClB,YAAY,IAAA,CAAK;AAAA;AACrB,GACJ,CAAE,CAAA;AACN;AASO,SAAS,kBAAA,CAAmB,SAAA,EAA6B,aAAA,GAAgB,EAAA,EAAY;AACxF,EAAA,IAAI,CAAC,SAAA,IAAa,SAAA,CAAU,MAAA,KAAW,CAAA,EAAG;AACtC,IAAA,OAAO,EAAA;AAAA,EACX;AAEA,EAAA,IAAI,IAAA,GAAO;AAAA;AAAA,QAAA,EAEL,aAAa;AAAA;AAAA,IAAA,CAAA;AAInB,EAAA,SAAA,CAAU,OAAA,CAAQ,CAAC,IAAA,KAAS;AAGxB,IAAA,IAAA,IAAQ;AAAA;AAAA,yBAAA,EAEW,IAAA,CAAK,EAAA,CAAG,OAAA,CAAQ,IAAA,EAAM,GAAG,CAAC,CAAA,eAAA,EAAkB,IAAA,CAAK,QAAA,CAAS,IAAI,CAAA,CAAA,EAAI,IAAA,CAAK,SAAS,SAAS,CAAA;AAAA;AAAA,6BAAA,EAErF,KAAK,EAAE,CAAA;AAAA,uBAAA,EACb,IAAA,CAAK,SAAS,IAAI,CAAA;AAAA,+BAAA,EACV,IAAA,CAAK,EAAA,CAAG,OAAA,CAAQ,IAAA,EAAM,GAAG,CAAC,CAAA;AAAA;AAAA;AAAA;AAAA,6BAAA,EAI5B,KAAK,EAAE,CAAA;AAAA,uBAAA,EACb,IAAA,CAAK,SAAS,IAAI,CAAA;AAAA;AAAA;AAAA;AAAA,QAAA,CAAA;AAAA,EAKvC,CAAC,CAAA;AAED,EAAA,IAAA,IAAQ,CAAA,eAAA,CAAA;AACR,EAAA,OAAO,IAAA;AACX;AAwBO,SAAS,kBAAkB,mBAAA,EAAmD;AACjF,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,UAAA;AAAA,IACN,QAAA,EAAU;AAAA,MACN,IAAA,EAAM,sBAAA;AAAA,MACN,WAAA,EAAa,CAAA;;AAAA,EAEvB,mBAAmB;;AAAA,sGAAA,CAAA;AAAA,MAGT,UAAA,EAAY;AAAA,QACR,IAAA,EAAM,QAAA;AAAA,QACN,UAAA,EAAY;AAAA,UACR,IAAA,EAAM;AAAA,YACF,IAAA,EAAM,QAAA;AAAA,YACN,WAAA,EAAa;AAAA;AACjB,SACJ;AAAA,QACA,QAAA,EAAU,CAAC,MAAM;AAAA;AACrB;AACJ,GACJ;AACJ;AASA,eAAsB,sBAAA,CAClB,UACA,WAAA,EACgB;AAChB,EAAA,IAAI,QAAA,CAAS,QAAA,CAAS,IAAA,KAAS,sBAAA,EAAwB;AACnD,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,cAAA,EAAiB,QAAA,CAAS,QAAA,CAAS,IAAI,CAAA,CAAE,CAAA;AAAA,EAC7D;AAEA,EAAA,MAAM,IAAA,GAAO,IAAA,CAAK,KAAA,CAAM,QAAA,CAAS,SAAS,SAAS,CAAA;AACnD,EAAA,MAAM,GAAA,GAAM,MAAM,WAAA,CAAY,SAAA,CAAU,KAAK,IAAI,CAAA;AACjD,EAAA,OAAO,GAAA,CAAI,MAAA;AACf","file":"openai.js","sourcesContent":["import type { ToolWithHandler } from '../types';\n\n/**\n * OpenAI tool call from chat completion response\n */\nexport interface OpenAIToolCall {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n}\n\n/**\n * OpenAI tool definition type\n */\nexport interface OpenAIToolDefinition {\n type: 'function';\n function: {\n name: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n}\n\n/**\n * Helper to convert Granular tools to OpenAI tool definitions\n * \n * @param tools - Array of Granular tools\n * @returns Array of OpenAI tool definitions\n */\nexport function toOpenAITools(tools: ToolWithHandler[]): OpenAIToolDefinition[] {\n return tools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.inputSchema,\n },\n }));\n}\n\n/**\n * Helper to convert OpenAI tool calls to job code for Granular\n * \n * @param toolCalls - Array of tool calls from OpenAI response\n * @param customImports - Optional imports to add to top of job\n * @returns generated job code\n */\nexport function toolCallsToJobCode(toolCalls: OpenAIToolCall[], customImports = ''): string {\n if (!toolCalls || toolCalls.length === 0) {\n return '';\n }\n\n let code = `\n import { tools } from './sandbox-tools';\n ${customImports}\n const results = [];\n `;\n\n toolCalls.forEach((call) => {\n // Safe argument parsing\n // We trust OpenAI arguments are JSON, but we stringify them again for the job\n code += `\n try {\n const result_${call.id.replace(/-/g, '_')} = await tools.${call.function.name}(${call.function.arguments});\n results.push({ \n toolCallId: \"${call.id}\", \n tool: \"${call.function.name}\", \n result: result_${call.id.replace(/-/g, '_')} \n });\n } catch (error) {\n results.push({ \n toolCallId: \"${call.id}\", \n tool: \"${call.function.name}\", \n error: String(error) \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 OpenAI tool definition\n * \n * @example\n * ```typescript\n * const docs = await environment.getDomainDocumentation();\n * const codegenTool = createCodegenTool(docs);\n * \n * const response = await openai.chat.completions.create({\n * model: 'gpt-4',\n * messages: [...],\n * tools: [codegenTool],\n * });\n * ```\n */\nexport function createCodegenTool(domainDocumentation: string): OpenAIToolDefinition {\n return {\n type: 'function',\n function: {\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 parameters: {\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/**\n * Helper to execute a codegen tool call result.\n * \n * @param toolCall - The tool call from the OpenAI response\n * @param environment - The connected Granular environment\n * @returns The job result\n */\nexport async function executeCodegenToolCall(\n toolCall: OpenAIToolCall,\n environment: { submitJob: (code: string) => Promise<{ result: Promise<unknown> }> }\n): Promise<unknown> {\n if (toolCall.function.name !== 'execute_sandbox_code') {\n throw new Error(`Unknown tool: ${toolCall.function.name}`);\n }\n\n const args = JSON.parse(toolCall.function.arguments) as { code: string };\n const job = await environment.submitJob(args.code);\n return job.result;\n}\n"]}
@@ -1,76 +0,0 @@
1
- // src/adapters/openai.ts
2
- function toOpenAITools(tools) {
3
- return tools.map((tool) => ({
4
- type: "function",
5
- function: {
6
- name: tool.name,
7
- description: tool.description,
8
- parameters: tool.inputSchema
9
- }
10
- }));
11
- }
12
- function toolCallsToJobCode(toolCalls, customImports = "") {
13
- if (!toolCalls || toolCalls.length === 0) {
14
- return "";
15
- }
16
- let code = `
17
- import { tools } from './sandbox-tools';
18
- ${customImports}
19
- const results = [];
20
- `;
21
- toolCalls.forEach((call) => {
22
- code += `
23
- try {
24
- const result_${call.id.replace(/-/g, "_")} = await tools.${call.function.name}(${call.function.arguments});
25
- results.push({
26
- toolCallId: "${call.id}",
27
- tool: "${call.function.name}",
28
- result: result_${call.id.replace(/-/g, "_")}
29
- });
30
- } catch (error) {
31
- results.push({
32
- toolCallId: "${call.id}",
33
- tool: "${call.function.name}",
34
- error: String(error)
35
- });
36
- }
37
- `;
38
- });
39
- code += `return results;`;
40
- return code;
41
- }
42
- function createCodegenTool(domainDocumentation) {
43
- return {
44
- type: "function",
45
- function: {
46
- name: "execute_sandbox_code",
47
- description: `Execute TypeScript code in a secure sandbox environment. The code can use the following tools by importing from "./sandbox-tools":
48
-
49
- ${domainDocumentation}
50
-
51
- Write complete, executable TypeScript code. Use async/await for tool calls. Return results at the end.`,
52
- parameters: {
53
- type: "object",
54
- properties: {
55
- code: {
56
- type: "string",
57
- description: 'TypeScript code to execute. Must import tools from "./sandbox-tools" and use await for all tool calls.'
58
- }
59
- },
60
- required: ["code"]
61
- }
62
- }
63
- };
64
- }
65
- async function executeCodegenToolCall(toolCall, environment) {
66
- if (toolCall.function.name !== "execute_sandbox_code") {
67
- throw new Error(`Unknown tool: ${toolCall.function.name}`);
68
- }
69
- const args = JSON.parse(toolCall.function.arguments);
70
- const job = await environment.submitJob(args.code);
71
- return job.result;
72
- }
73
-
74
- export { createCodegenTool, executeCodegenToolCall, toOpenAITools, toolCallsToJobCode };
75
- //# sourceMappingURL=openai.mjs.map
76
- //# sourceMappingURL=openai.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/adapters/openai.ts"],"names":[],"mappings":";AAgCO,SAAS,cAAc,KAAA,EAAkD;AAC5E,EAAA,OAAO,KAAA,CAAM,IAAI,CAAA,IAAA,MAAS;AAAA,IACtB,IAAA,EAAM,UAAA;AAAA,IACN,QAAA,EAAU;AAAA,MACN,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,aAAa,IAAA,CAAK,WAAA;AAAA,MAClB,YAAY,IAAA,CAAK;AAAA;AACrB,GACJ,CAAE,CAAA;AACN;AASO,SAAS,kBAAA,CAAmB,SAAA,EAA6B,aAAA,GAAgB,EAAA,EAAY;AACxF,EAAA,IAAI,CAAC,SAAA,IAAa,SAAA,CAAU,MAAA,KAAW,CAAA,EAAG;AACtC,IAAA,OAAO,EAAA;AAAA,EACX;AAEA,EAAA,IAAI,IAAA,GAAO;AAAA;AAAA,QAAA,EAEL,aAAa;AAAA;AAAA,IAAA,CAAA;AAInB,EAAA,SAAA,CAAU,OAAA,CAAQ,CAAC,IAAA,KAAS;AAGxB,IAAA,IAAA,IAAQ;AAAA;AAAA,yBAAA,EAEW,IAAA,CAAK,EAAA,CAAG,OAAA,CAAQ,IAAA,EAAM,GAAG,CAAC,CAAA,eAAA,EAAkB,IAAA,CAAK,QAAA,CAAS,IAAI,CAAA,CAAA,EAAI,IAAA,CAAK,SAAS,SAAS,CAAA;AAAA;AAAA,6BAAA,EAErF,KAAK,EAAE,CAAA;AAAA,uBAAA,EACb,IAAA,CAAK,SAAS,IAAI,CAAA;AAAA,+BAAA,EACV,IAAA,CAAK,EAAA,CAAG,OAAA,CAAQ,IAAA,EAAM,GAAG,CAAC,CAAA;AAAA;AAAA;AAAA;AAAA,6BAAA,EAI5B,KAAK,EAAE,CAAA;AAAA,uBAAA,EACb,IAAA,CAAK,SAAS,IAAI,CAAA;AAAA;AAAA;AAAA;AAAA,QAAA,CAAA;AAAA,EAKvC,CAAC,CAAA;AAED,EAAA,IAAA,IAAQ,CAAA,eAAA,CAAA;AACR,EAAA,OAAO,IAAA;AACX;AAwBO,SAAS,kBAAkB,mBAAA,EAAmD;AACjF,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,UAAA;AAAA,IACN,QAAA,EAAU;AAAA,MACN,IAAA,EAAM,sBAAA;AAAA,MACN,WAAA,EAAa,CAAA;;AAAA,EAEvB,mBAAmB;;AAAA,sGAAA,CAAA;AAAA,MAGT,UAAA,EAAY;AAAA,QACR,IAAA,EAAM,QAAA;AAAA,QACN,UAAA,EAAY;AAAA,UACR,IAAA,EAAM;AAAA,YACF,IAAA,EAAM,QAAA;AAAA,YACN,WAAA,EAAa;AAAA;AACjB,SACJ;AAAA,QACA,QAAA,EAAU,CAAC,MAAM;AAAA;AACrB;AACJ,GACJ;AACJ;AASA,eAAsB,sBAAA,CAClB,UACA,WAAA,EACgB;AAChB,EAAA,IAAI,QAAA,CAAS,QAAA,CAAS,IAAA,KAAS,sBAAA,EAAwB;AACnD,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,cAAA,EAAiB,QAAA,CAAS,QAAA,CAAS,IAAI,CAAA,CAAE,CAAA;AAAA,EAC7D;AAEA,EAAA,MAAM,IAAA,GAAO,IAAA,CAAK,KAAA,CAAM,QAAA,CAAS,SAAS,SAAS,CAAA;AACnD,EAAA,MAAM,GAAA,GAAM,MAAM,WAAA,CAAY,SAAA,CAAU,KAAK,IAAI,CAAA;AACjD,EAAA,OAAO,GAAA,CAAI,MAAA;AACf","file":"openai.mjs","sourcesContent":["import type { ToolWithHandler } from '../types';\n\n/**\n * OpenAI tool call from chat completion response\n */\nexport interface OpenAIToolCall {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n}\n\n/**\n * OpenAI tool definition type\n */\nexport interface OpenAIToolDefinition {\n type: 'function';\n function: {\n name: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n}\n\n/**\n * Helper to convert Granular tools to OpenAI tool definitions\n * \n * @param tools - Array of Granular tools\n * @returns Array of OpenAI tool definitions\n */\nexport function toOpenAITools(tools: ToolWithHandler[]): OpenAIToolDefinition[] {\n return tools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.inputSchema,\n },\n }));\n}\n\n/**\n * Helper to convert OpenAI tool calls to job code for Granular\n * \n * @param toolCalls - Array of tool calls from OpenAI response\n * @param customImports - Optional imports to add to top of job\n * @returns generated job code\n */\nexport function toolCallsToJobCode(toolCalls: OpenAIToolCall[], customImports = ''): string {\n if (!toolCalls || toolCalls.length === 0) {\n return '';\n }\n\n let code = `\n import { tools } from './sandbox-tools';\n ${customImports}\n const results = [];\n `;\n\n toolCalls.forEach((call) => {\n // Safe argument parsing\n // We trust OpenAI arguments are JSON, but we stringify them again for the job\n code += `\n try {\n const result_${call.id.replace(/-/g, '_')} = await tools.${call.function.name}(${call.function.arguments});\n results.push({ \n toolCallId: \"${call.id}\", \n tool: \"${call.function.name}\", \n result: result_${call.id.replace(/-/g, '_')} \n });\n } catch (error) {\n results.push({ \n toolCallId: \"${call.id}\", \n tool: \"${call.function.name}\", \n error: String(error) \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 OpenAI tool definition\n * \n * @example\n * ```typescript\n * const docs = await environment.getDomainDocumentation();\n * const codegenTool = createCodegenTool(docs);\n * \n * const response = await openai.chat.completions.create({\n * model: 'gpt-4',\n * messages: [...],\n * tools: [codegenTool],\n * });\n * ```\n */\nexport function createCodegenTool(domainDocumentation: string): OpenAIToolDefinition {\n return {\n type: 'function',\n function: {\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 parameters: {\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/**\n * Helper to execute a codegen tool call result.\n * \n * @param toolCall - The tool call from the OpenAI response\n * @param environment - The connected Granular environment\n * @returns The job result\n */\nexport async function executeCodegenToolCall(\n toolCall: OpenAIToolCall,\n environment: { submitJob: (code: string) => Promise<{ result: Promise<unknown> }> }\n): Promise<unknown> {\n if (toolCall.function.name !== 'execute_sandbox_code') {\n throw new Error(`Unknown tool: ${toolCall.function.name}`);\n }\n\n const args = JSON.parse(toolCall.function.arguments) as { code: string };\n const job = await environment.submitJob(args.code);\n return job.result;\n}\n"]}