@tanstack/ai-isolate-daytona 0.0.1
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/LICENSE +21 -0
- package/README.md +91 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/isolate-driver.d.ts +20 -0
- package/dist/esm/isolate-driver.js +311 -0
- package/dist/esm/isolate-driver.js.map +1 -0
- package/dist/esm/types.d.ts +65 -0
- package/dist/esm/wrap-code.d.ts +4 -0
- package/dist/esm/wrap-code.js +200 -0
- package/dist/esm/wrap-code.js.map +1 -0
- package/package.json +62 -0
- package/src/index.ts +36 -0
- package/src/isolate-driver.ts +555 -0
- package/src/types.ts +85 -0
- package/src/wrap-code.ts +218 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
//#region src/wrap-code.ts
|
|
2
|
+
var DAYTONA_RESULT_MARKER = "__TANSTACK_AI_CODE_MODE_RESULT__";
|
|
3
|
+
var VALID_TOOL_NAME = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
4
|
+
var RESERVED_TOOL_NAMES = /* @__PURE__ */ new Set([
|
|
5
|
+
"break",
|
|
6
|
+
"case",
|
|
7
|
+
"catch",
|
|
8
|
+
"class",
|
|
9
|
+
"const",
|
|
10
|
+
"continue",
|
|
11
|
+
"debugger",
|
|
12
|
+
"default",
|
|
13
|
+
"delete",
|
|
14
|
+
"do",
|
|
15
|
+
"else",
|
|
16
|
+
"enum",
|
|
17
|
+
"export",
|
|
18
|
+
"extends",
|
|
19
|
+
"false",
|
|
20
|
+
"finally",
|
|
21
|
+
"for",
|
|
22
|
+
"function",
|
|
23
|
+
"if",
|
|
24
|
+
"import",
|
|
25
|
+
"in",
|
|
26
|
+
"instanceof",
|
|
27
|
+
"new",
|
|
28
|
+
"null",
|
|
29
|
+
"return",
|
|
30
|
+
"super",
|
|
31
|
+
"switch",
|
|
32
|
+
"this",
|
|
33
|
+
"throw",
|
|
34
|
+
"true",
|
|
35
|
+
"try",
|
|
36
|
+
"typeof",
|
|
37
|
+
"var",
|
|
38
|
+
"void",
|
|
39
|
+
"while",
|
|
40
|
+
"with",
|
|
41
|
+
"yield",
|
|
42
|
+
"let",
|
|
43
|
+
"static",
|
|
44
|
+
"implements",
|
|
45
|
+
"interface",
|
|
46
|
+
"package",
|
|
47
|
+
"private",
|
|
48
|
+
"protected",
|
|
49
|
+
"public",
|
|
50
|
+
"await",
|
|
51
|
+
"async"
|
|
52
|
+
]);
|
|
53
|
+
function assertSafeToolName(name) {
|
|
54
|
+
if (!VALID_TOOL_NAME.test(name)) throw new Error(`Invalid tool name '${name}': must match ${VALID_TOOL_NAME} (letters, digits, _, $; cannot start with a digit)`);
|
|
55
|
+
if (RESERVED_TOOL_NAMES.has(name)) throw new Error(`Invalid tool name '${name}': reserved JavaScript keyword`);
|
|
56
|
+
}
|
|
57
|
+
function generateToolWrappers(tools) {
|
|
58
|
+
const wrappers = [];
|
|
59
|
+
for (const tool of tools) {
|
|
60
|
+
assertSafeToolName(tool.name);
|
|
61
|
+
wrappers.push(`
|
|
62
|
+
async function ${tool.name}(input) {
|
|
63
|
+
const callId = 'tc_' + (__toolCallIdx++);
|
|
64
|
+
const result = __toolResults[callId];
|
|
65
|
+
if (!result) {
|
|
66
|
+
__pendingToolCalls.push({ id: callId, name: '${tool.name}', args: input });
|
|
67
|
+
throw new __ToolCallNeeded(callId);
|
|
68
|
+
}
|
|
69
|
+
if (!result.success) {
|
|
70
|
+
throw new Error(result.error || 'Tool call failed');
|
|
71
|
+
}
|
|
72
|
+
return result.value;
|
|
73
|
+
}
|
|
74
|
+
`);
|
|
75
|
+
}
|
|
76
|
+
return wrappers.join("\n");
|
|
77
|
+
}
|
|
78
|
+
function wrapCode(code, tools, toolResults, resultMarker = DAYTONA_RESULT_MARKER) {
|
|
79
|
+
const toolWrappers = generateToolWrappers(tools);
|
|
80
|
+
return `
|
|
81
|
+
const __hostConsole = globalThis.console;
|
|
82
|
+
|
|
83
|
+
function __stringProperty(__value, __key) {
|
|
84
|
+
try {
|
|
85
|
+
const __property = __value && typeof __value === 'object' ? __value[__key] : undefined;
|
|
86
|
+
return typeof __property === 'string' ? __property : undefined;
|
|
87
|
+
} catch {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function __normalizeError(__error) {
|
|
93
|
+
if (__error instanceof Error) {
|
|
94
|
+
return {
|
|
95
|
+
name: __error.name || 'Error',
|
|
96
|
+
message: __error.message || String(__error),
|
|
97
|
+
stack: __error.stack,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (__error && typeof __error === 'object') {
|
|
102
|
+
return {
|
|
103
|
+
name: __stringProperty(__error, 'name') || 'Error',
|
|
104
|
+
message: __stringProperty(__error, 'message') || 'Error',
|
|
105
|
+
stack: __stringProperty(__error, 'stack'),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
name: 'Error',
|
|
111
|
+
message: String(__error),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function __formatConsoleArg(__arg) {
|
|
116
|
+
if (typeof __arg === 'string') {
|
|
117
|
+
return __arg;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const __json = JSON.stringify(__arg);
|
|
122
|
+
if (__json !== undefined) {
|
|
123
|
+
return __json;
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
// Fall through to String(); logging must not fail user code.
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
return String(__arg);
|
|
131
|
+
} catch {
|
|
132
|
+
return '[Unserializable]';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
(async function() {
|
|
137
|
+
let __toolCallIdx = 0;
|
|
138
|
+
const __pendingToolCalls = [];
|
|
139
|
+
const __toolResults = ${toolResults ? JSON.stringify(toolResults) : "{}"};
|
|
140
|
+
const __logs = [];
|
|
141
|
+
|
|
142
|
+
class __ToolCallNeeded extends Error {
|
|
143
|
+
constructor(callId) {
|
|
144
|
+
super('Tool call needed: ' + callId);
|
|
145
|
+
this.callId = callId;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const console = {
|
|
150
|
+
log: (...args) => __logs.push(args.map(__formatConsoleArg).join(' ')),
|
|
151
|
+
error: (...args) => __logs.push('ERROR: ' + args.map(__formatConsoleArg).join(' ')),
|
|
152
|
+
warn: (...args) => __logs.push('WARN: ' + args.map(__formatConsoleArg).join(' ')),
|
|
153
|
+
info: (...args) => __logs.push('INFO: ' + args.map(__formatConsoleArg).join(' ')),
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
${toolWrappers}
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
const __userResult = await (async function() {
|
|
160
|
+
${code}
|
|
161
|
+
})();
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
status: 'done',
|
|
165
|
+
success: true,
|
|
166
|
+
value: __userResult,
|
|
167
|
+
logs: __logs,
|
|
168
|
+
};
|
|
169
|
+
} catch (__error) {
|
|
170
|
+
if (__error instanceof __ToolCallNeeded) {
|
|
171
|
+
return {
|
|
172
|
+
status: 'need_tools',
|
|
173
|
+
toolCalls: __pendingToolCalls,
|
|
174
|
+
logs: __logs,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
status: 'done',
|
|
180
|
+
success: false,
|
|
181
|
+
error: __normalizeError(__error),
|
|
182
|
+
logs: __logs,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
})()
|
|
186
|
+
.then((__envelope) => {
|
|
187
|
+
__hostConsole.log(${JSON.stringify(resultMarker)} + JSON.stringify(__envelope));
|
|
188
|
+
})
|
|
189
|
+
.catch((__error) => {
|
|
190
|
+
__hostConsole.log(${JSON.stringify(resultMarker)} + JSON.stringify({
|
|
191
|
+
status: 'error',
|
|
192
|
+
error: __normalizeError(__error),
|
|
193
|
+
}));
|
|
194
|
+
});
|
|
195
|
+
`;
|
|
196
|
+
}
|
|
197
|
+
//#endregion
|
|
198
|
+
export { DAYTONA_RESULT_MARKER, generateToolWrappers, wrapCode };
|
|
199
|
+
|
|
200
|
+
//# sourceMappingURL=wrap-code.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrap-code.js","names":[],"sources":["../../src/wrap-code.ts"],"sourcesContent":["import type { ToolResultPayload, ToolSchema } from './types'\n\nexport const DAYTONA_RESULT_MARKER = '__TANSTACK_AI_CODE_MODE_RESULT__'\n\nconst VALID_TOOL_NAME = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/\n\nconst RESERVED_TOOL_NAMES = new Set([\n 'break',\n 'case',\n 'catch',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'else',\n 'enum',\n 'export',\n 'extends',\n 'false',\n 'finally',\n 'for',\n 'function',\n 'if',\n 'import',\n 'in',\n 'instanceof',\n 'new',\n 'null',\n 'return',\n 'super',\n 'switch',\n 'this',\n 'throw',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'while',\n 'with',\n 'yield',\n 'let',\n 'static',\n 'implements',\n 'interface',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'await',\n 'async',\n])\n\nfunction assertSafeToolName(name: string): void {\n if (!VALID_TOOL_NAME.test(name)) {\n throw new Error(\n `Invalid tool name '${name}': must match ${VALID_TOOL_NAME} (letters, digits, _, $; cannot start with a digit)`,\n )\n }\n if (RESERVED_TOOL_NAMES.has(name)) {\n throw new Error(`Invalid tool name '${name}': reserved JavaScript keyword`)\n }\n}\n\nexport function generateToolWrappers(tools: Array<ToolSchema>): string {\n const wrappers: Array<string> = []\n\n for (const tool of tools) {\n assertSafeToolName(tool.name)\n\n wrappers.push(`\n async function ${tool.name}(input) {\n const callId = 'tc_' + (__toolCallIdx++);\n const result = __toolResults[callId];\n if (!result) {\n __pendingToolCalls.push({ id: callId, name: '${tool.name}', args: input });\n throw new __ToolCallNeeded(callId);\n }\n if (!result.success) {\n throw new Error(result.error || 'Tool call failed');\n }\n return result.value;\n }\n `)\n }\n\n return wrappers.join('\\n')\n}\n\nexport function wrapCode(\n code: string,\n tools: Array<ToolSchema>,\n toolResults?: Record<string, ToolResultPayload>,\n resultMarker = DAYTONA_RESULT_MARKER,\n): string {\n const toolWrappers = generateToolWrappers(tools)\n const toolResultsJson = toolResults ? JSON.stringify(toolResults) : '{}'\n\n return `\n const __hostConsole = globalThis.console;\n\n function __stringProperty(__value, __key) {\n try {\n const __property = __value && typeof __value === 'object' ? __value[__key] : undefined;\n return typeof __property === 'string' ? __property : undefined;\n } catch {\n return undefined;\n }\n }\n\n function __normalizeError(__error) {\n if (__error instanceof Error) {\n return {\n name: __error.name || 'Error',\n message: __error.message || String(__error),\n stack: __error.stack,\n };\n }\n\n if (__error && typeof __error === 'object') {\n return {\n name: __stringProperty(__error, 'name') || 'Error',\n message: __stringProperty(__error, 'message') || 'Error',\n stack: __stringProperty(__error, 'stack'),\n };\n }\n\n return {\n name: 'Error',\n message: String(__error),\n };\n }\n\n function __formatConsoleArg(__arg) {\n if (typeof __arg === 'string') {\n return __arg;\n }\n\n try {\n const __json = JSON.stringify(__arg);\n if (__json !== undefined) {\n return __json;\n }\n } catch {\n // Fall through to String(); logging must not fail user code.\n }\n\n try {\n return String(__arg);\n } catch {\n return '[Unserializable]';\n }\n }\n\n (async function() {\n let __toolCallIdx = 0;\n const __pendingToolCalls = [];\n const __toolResults = ${toolResultsJson};\n const __logs = [];\n\n class __ToolCallNeeded extends Error {\n constructor(callId) {\n super('Tool call needed: ' + callId);\n this.callId = callId;\n }\n }\n\n const console = {\n log: (...args) => __logs.push(args.map(__formatConsoleArg).join(' ')),\n error: (...args) => __logs.push('ERROR: ' + args.map(__formatConsoleArg).join(' ')),\n warn: (...args) => __logs.push('WARN: ' + args.map(__formatConsoleArg).join(' ')),\n info: (...args) => __logs.push('INFO: ' + args.map(__formatConsoleArg).join(' ')),\n };\n\n ${toolWrappers}\n\n try {\n const __userResult = await (async function() {\n ${code}\n })();\n\n return {\n status: 'done',\n success: true,\n value: __userResult,\n logs: __logs,\n };\n } catch (__error) {\n if (__error instanceof __ToolCallNeeded) {\n return {\n status: 'need_tools',\n toolCalls: __pendingToolCalls,\n logs: __logs,\n };\n }\n\n return {\n status: 'done',\n success: false,\n error: __normalizeError(__error),\n logs: __logs,\n };\n }\n })()\n .then((__envelope) => {\n __hostConsole.log(${JSON.stringify(resultMarker)} + JSON.stringify(__envelope));\n })\n .catch((__error) => {\n __hostConsole.log(${JSON.stringify(resultMarker)} + JSON.stringify({\n status: 'error',\n error: __normalizeError(__error),\n }));\n });\n `\n}\n"],"mappings":";AAEA,IAAa,wBAAwB;AAErC,IAAM,kBAAkB;AAExB,IAAM,sCAAsB,IAAI,IAAI;CAClC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,SAAS,mBAAmB,MAAoB;CAC9C,IAAI,CAAC,gBAAgB,KAAK,IAAI,GAC5B,MAAM,IAAI,MACR,sBAAsB,KAAK,gBAAgB,gBAAgB,oDAC7D;CAEF,IAAI,oBAAoB,IAAI,IAAI,GAC9B,MAAM,IAAI,MAAM,sBAAsB,KAAK,+BAA+B;AAE9E;AAEA,SAAgB,qBAAqB,OAAkC;CACrE,MAAM,WAA0B,CAAC;CAEjC,KAAK,MAAM,QAAQ,OAAO;EACxB,mBAAmB,KAAK,IAAI;EAE5B,SAAS,KAAK;uBACK,KAAK,KAAK;;;;yDAIwB,KAAK,KAAK;;;;;;;;KAQ9D;CACH;CAEA,OAAO,SAAS,KAAK,IAAI;AAC3B;AAEA,SAAgB,SACd,MACA,OACA,aACA,eAAe,uBACP;CACR,MAAM,eAAe,qBAAqB,KAAK;CAG/C,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;8BAFiB,cAAc,KAAK,UAAU,WAAW,IAAI,KA6DxB;;;;;;;;;;;;;;;;;QAiBtC,aAAa;;;;YAIT,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;4BA2BW,KAAK,UAAU,YAAY,EAAE;;;4BAG7B,KAAK,UAAU,YAAY,EAAE;;;;;;AAMzD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tanstack/ai-isolate-daytona",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"description": "Daytona sandbox driver for TanStack AI Code Mode TypeScript execution.",
|
|
8
|
+
"author": "",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/TanStack/ai.git",
|
|
13
|
+
"directory": "packages/ai-isolate-daytona"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"module": "./dist/esm/index.js",
|
|
17
|
+
"types": "./dist/esm/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/esm/index.d.ts",
|
|
21
|
+
"import": "./dist/esm/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
32
|
+
"keywords": [
|
|
33
|
+
"ai",
|
|
34
|
+
"ai-sdk",
|
|
35
|
+
"typescript",
|
|
36
|
+
"tanstack",
|
|
37
|
+
"code-mode",
|
|
38
|
+
"daytona",
|
|
39
|
+
"sandbox",
|
|
40
|
+
"isolate",
|
|
41
|
+
"code-execution"
|
|
42
|
+
],
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@tanstack/ai-code-mode": "0.3.10"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@daytona/sdk": "^0.191.0",
|
|
48
|
+
"@vitest/coverage-v8": "4.0.14",
|
|
49
|
+
"@tanstack/ai-code-mode": "0.3.10"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "vite build",
|
|
53
|
+
"clean": "premove ./build ./dist",
|
|
54
|
+
"lint:fix": "oxlint src --type-aware --fix",
|
|
55
|
+
"test:build": "publint --strict",
|
|
56
|
+
"test:oxlint": "oxlint src --type-aware",
|
|
57
|
+
"test:live": "vitest --run tests/live.test.ts",
|
|
58
|
+
"test:lib": "vitest --passWithNoTests",
|
|
59
|
+
"test:lib:dev": "pnpm test:lib --watch",
|
|
60
|
+
"test:types": "tsc"
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tanstack/ai-isolate-daytona
|
|
3
|
+
*
|
|
4
|
+
* Daytona sandbox driver for TanStack AI Code Mode.
|
|
5
|
+
* Execute LLM-generated code inside a caller-provided Daytona sandbox.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createDaytonaIsolateDriver } from '@tanstack/ai-isolate-daytona'
|
|
10
|
+
*
|
|
11
|
+
* const driver = createDaytonaIsolateDriver({ sandbox })
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
createDaytonaIsolateDriver,
|
|
19
|
+
type DaytonaIsolateDriverConfig,
|
|
20
|
+
} from './isolate-driver'
|
|
21
|
+
|
|
22
|
+
export type {
|
|
23
|
+
DaytonaCodeRunArtifacts,
|
|
24
|
+
DaytonaCodeRunParams,
|
|
25
|
+
DaytonaCodeRunResponse,
|
|
26
|
+
DaytonaProcessLike,
|
|
27
|
+
DaytonaSandboxLike,
|
|
28
|
+
} from './types'
|
|
29
|
+
|
|
30
|
+
export type {
|
|
31
|
+
ExecutionResult,
|
|
32
|
+
IsolateConfig,
|
|
33
|
+
IsolateContext,
|
|
34
|
+
IsolateDriver,
|
|
35
|
+
NormalizedError,
|
|
36
|
+
} from '@tanstack/ai-code-mode'
|