integrate-sdk 0.9.2-dev.0 → 0.9.4-dev.0
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/adapters/auto-routes.d.ts.map +1 -1
- package/dist/adapters/auto-routes.js +727 -17
- package/dist/adapters/base-handler.d.ts.map +1 -1
- package/dist/adapters/index.js +722 -16
- package/dist/adapters/nextjs.d.ts.map +1 -1
- package/dist/adapters/nextjs.js +722 -16
- package/dist/adapters/node.js +722 -16
- package/dist/adapters/svelte-kit.js +722 -16
- package/dist/adapters/tanstack-start.js +722 -16
- package/dist/ai/anthropic.d.ts +11 -0
- package/dist/ai/anthropic.d.ts.map +1 -1
- package/dist/ai/anthropic.js +552 -2
- package/dist/ai/google.d.ts +11 -0
- package/dist/ai/google.d.ts.map +1 -1
- package/dist/ai/google.js +561 -2
- package/dist/ai/index.js +648 -8
- package/dist/ai/openai.d.ts +11 -0
- package/dist/ai/openai.d.ts.map +1 -1
- package/dist/ai/openai.js +550 -2
- package/dist/ai/vercel-ai.d.ts +13 -0
- package/dist/ai/vercel-ai.d.ts.map +1 -1
- package/dist/ai/vercel-ai.js +536 -2
- package/dist/code-mode/executor.d.ts +99 -0
- package/dist/code-mode/executor.d.ts.map +1 -0
- package/dist/code-mode/executor.js +207 -0
- package/dist/code-mode/index.d.ts +12 -0
- package/dist/code-mode/index.d.ts.map +1 -0
- package/dist/code-mode/index.js +527 -0
- package/dist/code-mode/runtime-stub.d.ts +16 -0
- package/dist/code-mode/runtime-stub.d.ts.map +1 -0
- package/dist/code-mode/runtime-stub.js +72 -0
- package/dist/code-mode/tool-builder.d.ts +83 -0
- package/dist/code-mode/tool-builder.d.ts.map +1 -0
- package/dist/code-mode/tool-builder.js +524 -0
- package/dist/code-mode/type-generator.d.ts +22 -0
- package/dist/code-mode/type-generator.d.ts.map +1 -0
- package/dist/code-mode/type-generator.js +217 -0
- package/dist/index.js +722 -16
- package/dist/oauth.js +727 -17
- package/dist/server.d.ts +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +733 -17
- package/dist/src/adapters/auto-routes.d.ts.map +1 -1
- package/dist/src/adapters/base-handler.d.ts.map +1 -1
- package/dist/src/adapters/nextjs.d.ts.map +1 -1
- package/dist/src/ai/anthropic.d.ts +11 -0
- package/dist/src/ai/anthropic.d.ts.map +1 -1
- package/dist/src/ai/google.d.ts +11 -0
- package/dist/src/ai/google.d.ts.map +1 -1
- package/dist/src/ai/openai.d.ts +11 -0
- package/dist/src/ai/openai.d.ts.map +1 -1
- package/dist/src/ai/vercel-ai.d.ts +13 -0
- package/dist/src/ai/vercel-ai.d.ts.map +1 -1
- package/dist/src/code-mode/executor.d.ts +99 -0
- package/dist/src/code-mode/executor.d.ts.map +1 -0
- package/dist/src/code-mode/index.d.ts +12 -0
- package/dist/src/code-mode/index.d.ts.map +1 -0
- package/dist/src/code-mode/runtime-stub.d.ts +16 -0
- package/dist/src/code-mode/runtime-stub.d.ts.map +1 -0
- package/dist/src/code-mode/tool-builder.d.ts +83 -0
- package/dist/src/code-mode/tool-builder.d.ts.map +1 -0
- package/dist/src/code-mode/type-generator.d.ts +22 -0
- package/dist/src/code-mode/type-generator.d.ts.map +1 -0
- package/dist/src/config/types.d.ts +55 -0
- package/dist/src/config/types.d.ts.map +1 -1
- package/dist/src/server.d.ts.map +1 -1
- package/package.json +15 -6
- package/server.ts +9 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
// ../utils/naming.ts
|
|
2
|
+
function snakeToCamel(str) {
|
|
3
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
4
|
+
}
|
|
5
|
+
function toolNameToMethod(toolName) {
|
|
6
|
+
const withoutPrefix = toolName.replace(/^[^_]+_/, "");
|
|
7
|
+
return snakeToCamel(withoutPrefix);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// type-generator.ts
|
|
11
|
+
var RESERVED_TS = new Set([
|
|
12
|
+
"break",
|
|
13
|
+
"case",
|
|
14
|
+
"catch",
|
|
15
|
+
"class",
|
|
16
|
+
"const",
|
|
17
|
+
"continue",
|
|
18
|
+
"debugger",
|
|
19
|
+
"default",
|
|
20
|
+
"delete",
|
|
21
|
+
"do",
|
|
22
|
+
"else",
|
|
23
|
+
"enum",
|
|
24
|
+
"export",
|
|
25
|
+
"extends",
|
|
26
|
+
"false",
|
|
27
|
+
"finally",
|
|
28
|
+
"for",
|
|
29
|
+
"function",
|
|
30
|
+
"if",
|
|
31
|
+
"import",
|
|
32
|
+
"in",
|
|
33
|
+
"instanceof",
|
|
34
|
+
"new",
|
|
35
|
+
"null",
|
|
36
|
+
"return",
|
|
37
|
+
"super",
|
|
38
|
+
"switch",
|
|
39
|
+
"this",
|
|
40
|
+
"throw",
|
|
41
|
+
"true",
|
|
42
|
+
"try",
|
|
43
|
+
"typeof",
|
|
44
|
+
"var",
|
|
45
|
+
"void",
|
|
46
|
+
"while",
|
|
47
|
+
"with",
|
|
48
|
+
"as",
|
|
49
|
+
"implements",
|
|
50
|
+
"interface",
|
|
51
|
+
"let",
|
|
52
|
+
"package",
|
|
53
|
+
"private",
|
|
54
|
+
"protected",
|
|
55
|
+
"public",
|
|
56
|
+
"static",
|
|
57
|
+
"yield"
|
|
58
|
+
]);
|
|
59
|
+
function safeIdent(name) {
|
|
60
|
+
if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) || RESERVED_TS.has(name)) {
|
|
61
|
+
return JSON.stringify(name);
|
|
62
|
+
}
|
|
63
|
+
return name;
|
|
64
|
+
}
|
|
65
|
+
function integrationFromToolName(toolName) {
|
|
66
|
+
const idx = toolName.indexOf("_");
|
|
67
|
+
return idx === -1 ? toolName : toolName.slice(0, idx);
|
|
68
|
+
}
|
|
69
|
+
function jsonSchemaToTs(schema, indent) {
|
|
70
|
+
if (!schema || typeof schema !== "object")
|
|
71
|
+
return "unknown";
|
|
72
|
+
const s = schema;
|
|
73
|
+
if (Array.isArray(s.enum) && s.enum.length > 0) {
|
|
74
|
+
return s.enum.map((v) => typeof v === "string" ? JSON.stringify(v) : typeof v === "number" || typeof v === "boolean" ? String(v) : "unknown").join(" | ");
|
|
75
|
+
}
|
|
76
|
+
if (Array.isArray(s.type)) {
|
|
77
|
+
return s.type.map((t2) => jsonSchemaToTs({ ...s, type: t2 }, indent)).join(" | ");
|
|
78
|
+
}
|
|
79
|
+
const t = s.type;
|
|
80
|
+
switch (t) {
|
|
81
|
+
case "string":
|
|
82
|
+
return "string";
|
|
83
|
+
case "number":
|
|
84
|
+
case "integer":
|
|
85
|
+
return "number";
|
|
86
|
+
case "boolean":
|
|
87
|
+
return "boolean";
|
|
88
|
+
case "null":
|
|
89
|
+
return "null";
|
|
90
|
+
case "array": {
|
|
91
|
+
const items = s.items;
|
|
92
|
+
if (Array.isArray(items))
|
|
93
|
+
return "unknown[]";
|
|
94
|
+
const inner = jsonSchemaToTs(items, indent);
|
|
95
|
+
return /[|&]/.test(inner) ? `Array<${inner}>` : `${inner}[]`;
|
|
96
|
+
}
|
|
97
|
+
case "object":
|
|
98
|
+
return objectShape(s, indent);
|
|
99
|
+
default:
|
|
100
|
+
if (s.properties && typeof s.properties === "object")
|
|
101
|
+
return objectShape(s, indent);
|
|
102
|
+
return "unknown";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function objectShape(schema, indent) {
|
|
106
|
+
const props = schema.properties && typeof schema.properties === "object" ? schema.properties : {};
|
|
107
|
+
const keys = Object.keys(props);
|
|
108
|
+
if (keys.length === 0) {
|
|
109
|
+
return schema.additionalProperties === false ? "Record<string, never>" : "Record<string, unknown>";
|
|
110
|
+
}
|
|
111
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
112
|
+
const inner = indent + " ";
|
|
113
|
+
const lines = ["{"];
|
|
114
|
+
for (const key of keys) {
|
|
115
|
+
const prop = props[key];
|
|
116
|
+
const desc = prop && typeof prop.description === "string" ? prop.description : undefined;
|
|
117
|
+
if (desc)
|
|
118
|
+
lines.push(`${inner}/** ${desc.replace(/\*\//g, "*\\/")} */`);
|
|
119
|
+
const optional = required.has(key) ? "" : "?";
|
|
120
|
+
const type = jsonSchemaToTs(prop, inner);
|
|
121
|
+
lines.push(`${inner}${safeIdent(key)}${optional}: ${type};`);
|
|
122
|
+
}
|
|
123
|
+
lines.push(`${indent}}`);
|
|
124
|
+
return lines.join(`
|
|
125
|
+
`);
|
|
126
|
+
}
|
|
127
|
+
function argsType(schema) {
|
|
128
|
+
if (!schema)
|
|
129
|
+
return "Record<string, unknown>";
|
|
130
|
+
const hasProps = schema.properties && Object.keys(schema.properties).length > 0;
|
|
131
|
+
if (!hasProps)
|
|
132
|
+
return "Record<string, unknown>";
|
|
133
|
+
return objectShape(schema, " ");
|
|
134
|
+
}
|
|
135
|
+
function methodHasRequiredArgs(schema) {
|
|
136
|
+
if (!schema || !schema.properties)
|
|
137
|
+
return false;
|
|
138
|
+
const req = Array.isArray(schema.required) ? schema.required : [];
|
|
139
|
+
return req.length > 0;
|
|
140
|
+
}
|
|
141
|
+
function formatDescription(desc, indent) {
|
|
142
|
+
if (!desc)
|
|
143
|
+
return "";
|
|
144
|
+
const cleaned = desc.replace(/\*\//g, "*\\/").trim();
|
|
145
|
+
if (!cleaned.includes(`
|
|
146
|
+
`))
|
|
147
|
+
return `${indent}/** ${cleaned} */
|
|
148
|
+
`;
|
|
149
|
+
const lines = cleaned.split(`
|
|
150
|
+
`).map((l) => `${indent} * ${l}`).join(`
|
|
151
|
+
`);
|
|
152
|
+
return `${indent}/**
|
|
153
|
+
${lines}
|
|
154
|
+
${indent} */
|
|
155
|
+
`;
|
|
156
|
+
}
|
|
157
|
+
function generateCodeModeTypes(tools) {
|
|
158
|
+
const byIntegration = {};
|
|
159
|
+
for (const tool of tools) {
|
|
160
|
+
const integration = integrationFromToolName(tool.name);
|
|
161
|
+
(byIntegration[integration] ??= []).push(tool);
|
|
162
|
+
}
|
|
163
|
+
const methodMap = {};
|
|
164
|
+
const integrationCounts = {};
|
|
165
|
+
const sections = [];
|
|
166
|
+
const integrationIds = Object.keys(byIntegration).sort();
|
|
167
|
+
sections.push("/**");
|
|
168
|
+
sections.push(" * Integrate SDK — available APIs inside `execute_code`.");
|
|
169
|
+
sections.push(" * Every method is async and returns the MCP tool-call response.");
|
|
170
|
+
sections.push(" * Call them via the exported `client` object, e.g.");
|
|
171
|
+
sections.push(" * const repos = await client.github.listRepos();");
|
|
172
|
+
sections.push(" */");
|
|
173
|
+
sections.push("");
|
|
174
|
+
for (const integrationId of integrationIds) {
|
|
175
|
+
const integrationTools = byIntegration[integrationId].slice().sort((a, b) => a.name.localeCompare(b.name));
|
|
176
|
+
integrationCounts[integrationId] = integrationTools.length;
|
|
177
|
+
const interfaceName = pascalCase(integrationId) + "Client";
|
|
178
|
+
sections.push(`export interface ${interfaceName} {`);
|
|
179
|
+
for (const tool of integrationTools) {
|
|
180
|
+
const methodName = toolNameToMethod(tool.name);
|
|
181
|
+
methodMap[`${integrationId}.${methodName}`] = tool.name;
|
|
182
|
+
sections.push(formatDescription(tool.description, " "));
|
|
183
|
+
const argType = argsType(tool.inputSchema);
|
|
184
|
+
const argIsOptional = !methodHasRequiredArgs(tool.inputSchema);
|
|
185
|
+
const paramName = argIsOptional ? "args?" : "args";
|
|
186
|
+
sections.push(` ${safeIdent(methodName)}(${paramName}: ${argType}): Promise<ToolResult>;`);
|
|
187
|
+
}
|
|
188
|
+
sections.push("}");
|
|
189
|
+
sections.push("");
|
|
190
|
+
}
|
|
191
|
+
sections.push("export interface ToolResult {");
|
|
192
|
+
sections.push(" content: Array<{ type: 'text' | 'image' | 'resource'; text?: string; data?: string; mimeType?: string; [key: string]: unknown }>;");
|
|
193
|
+
sections.push(" isError?: boolean;");
|
|
194
|
+
sections.push(" structuredContent?: Record<string, unknown>;");
|
|
195
|
+
sections.push("}");
|
|
196
|
+
sections.push("");
|
|
197
|
+
sections.push("export interface Client {");
|
|
198
|
+
for (const integrationId of integrationIds) {
|
|
199
|
+
const interfaceName = pascalCase(integrationId) + "Client";
|
|
200
|
+
sections.push(` ${safeIdent(integrationId)}: ${interfaceName};`);
|
|
201
|
+
}
|
|
202
|
+
sections.push("}");
|
|
203
|
+
sections.push("");
|
|
204
|
+
sections.push("export declare const client: Client;");
|
|
205
|
+
return {
|
|
206
|
+
source: sections.filter((line, idx, arr) => !(line === "" && arr[idx - 1] === "")).join(`
|
|
207
|
+
`),
|
|
208
|
+
methodMap,
|
|
209
|
+
integrationCounts
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function pascalCase(id) {
|
|
213
|
+
return id.split(/[^A-Za-z0-9]/).filter(Boolean).map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join("") || "Unknown";
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// runtime-stub.ts
|
|
217
|
+
var RUNTIME_STUB_SOURCE = `// runtime.mjs — generated by integrate-sdk code mode
|
|
218
|
+
const MCP_URL = process.env.INTEGRATE_MCP_URL;
|
|
219
|
+
const SESSION_TOKEN = process.env.INTEGRATE_SESSION_TOKEN;
|
|
220
|
+
const PROVIDER_TOKENS = process.env.INTEGRATE_PROVIDER_TOKENS || '';
|
|
221
|
+
const INTEGRATIONS_HEADER = process.env.INTEGRATE_INTEGRATIONS || '';
|
|
222
|
+
const CONTEXT_JSON = process.env.INTEGRATE_CONTEXT || '';
|
|
223
|
+
|
|
224
|
+
if (!MCP_URL) {
|
|
225
|
+
throw new Error('INTEGRATE_MCP_URL is not set — the sandbox cannot reach the MCP route.');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function camelToSnake(str) {
|
|
229
|
+
return str.replace(/[A-Z]/g, (letter) => '_' + letter.toLowerCase());
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async function callTool(toolName, args) {
|
|
233
|
+
const headers = {
|
|
234
|
+
'Content-Type': 'application/json',
|
|
235
|
+
'x-integrate-code-mode': '1',
|
|
236
|
+
};
|
|
237
|
+
if (SESSION_TOKEN) headers['Authorization'] = 'Bearer ' + SESSION_TOKEN;
|
|
238
|
+
if (PROVIDER_TOKENS) headers['x-integrate-tokens'] = PROVIDER_TOKENS;
|
|
239
|
+
if (INTEGRATIONS_HEADER) headers['x-integrations'] = INTEGRATIONS_HEADER;
|
|
240
|
+
if (CONTEXT_JSON) headers['x-integrate-context'] = CONTEXT_JSON;
|
|
241
|
+
|
|
242
|
+
const res = await fetch(MCP_URL, {
|
|
243
|
+
method: 'POST',
|
|
244
|
+
headers,
|
|
245
|
+
body: JSON.stringify({ name: toolName, arguments: args || {} }),
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const text = await res.text();
|
|
249
|
+
let payload;
|
|
250
|
+
try {
|
|
251
|
+
payload = text ? JSON.parse(text) : null;
|
|
252
|
+
} catch {
|
|
253
|
+
payload = { content: [{ type: 'text', text }] };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!res.ok) {
|
|
257
|
+
const message = (payload && (payload.error || payload.message)) || 'Tool call failed: HTTP ' + res.status;
|
|
258
|
+
const err = new Error(message);
|
|
259
|
+
err.status = res.status;
|
|
260
|
+
err.toolName = toolName;
|
|
261
|
+
throw err;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return payload;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function createIntegrationProxy(integrationId) {
|
|
268
|
+
return new Proxy({}, {
|
|
269
|
+
get(_target, methodName) {
|
|
270
|
+
if (typeof methodName !== 'string') return undefined;
|
|
271
|
+
return (args) => callTool(integrationId + '_' + camelToSnake(methodName), args);
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export const client = new Proxy({}, {
|
|
277
|
+
get(_target, integrationId) {
|
|
278
|
+
if (typeof integrationId !== 'string') return undefined;
|
|
279
|
+
return createIntegrationProxy(integrationId);
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
export { callTool };
|
|
284
|
+
`;
|
|
285
|
+
|
|
286
|
+
// executor.ts
|
|
287
|
+
var sandboxFactoryOverride = null;
|
|
288
|
+
function __setSandboxFactoryForTests(factory) {
|
|
289
|
+
sandboxFactoryOverride = factory;
|
|
290
|
+
}
|
|
291
|
+
async function loadSandboxFactory() {
|
|
292
|
+
if (sandboxFactoryOverride)
|
|
293
|
+
return sandboxFactoryOverride;
|
|
294
|
+
try {
|
|
295
|
+
const dynamicImport = new Function("specifier", "return import(specifier)");
|
|
296
|
+
const pkg = "@" + "vercel/sandbox";
|
|
297
|
+
const mod = await dynamicImport(pkg);
|
|
298
|
+
return mod.Sandbox ?? mod.default?.Sandbox ?? mod;
|
|
299
|
+
} catch (err) {
|
|
300
|
+
throw new Error("Code Mode requires the optional peer dependency `@vercel/sandbox`. " + "Install it with `npm install @vercel/sandbox` (or `bun add @vercel/sandbox`).");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
var RESULT_SENTINEL = "__INTEGRATE_RESULT__";
|
|
304
|
+
function wrapUserCode(code) {
|
|
305
|
+
return `// user.mjs — wrapped by integrate-sdk code mode
|
|
306
|
+
import { client, callTool } from './runtime.mjs';
|
|
307
|
+
|
|
308
|
+
(async () => {
|
|
309
|
+
try {
|
|
310
|
+
const __result = await (async () => {
|
|
311
|
+
${code}
|
|
312
|
+
})();
|
|
313
|
+
if (typeof __result !== 'undefined') {
|
|
314
|
+
process.stdout.write('\\n' + ${JSON.stringify(RESULT_SENTINEL)} + JSON.stringify(__result) + '\\n');
|
|
315
|
+
}
|
|
316
|
+
} catch (err) {
|
|
317
|
+
const payload = {
|
|
318
|
+
message: err && err.message ? err.message : String(err),
|
|
319
|
+
name: err && err.name,
|
|
320
|
+
stack: err && err.stack,
|
|
321
|
+
toolName: err && err.toolName,
|
|
322
|
+
status: err && err.status,
|
|
323
|
+
};
|
|
324
|
+
process.stderr.write('\\n' + ${JSON.stringify(RESULT_SENTINEL)} + JSON.stringify({ error: payload }) + '\\n');
|
|
325
|
+
process.exit(1);
|
|
326
|
+
}
|
|
327
|
+
})();
|
|
328
|
+
`;
|
|
329
|
+
}
|
|
330
|
+
function defaultNetworkPolicy(mcpUrl) {
|
|
331
|
+
try {
|
|
332
|
+
const host = new URL(mcpUrl).hostname;
|
|
333
|
+
return { allow: [host] };
|
|
334
|
+
} catch {
|
|
335
|
+
return { allow: [] };
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function extractResult(stream) {
|
|
339
|
+
const idx = stream.lastIndexOf(RESULT_SENTINEL);
|
|
340
|
+
if (idx === -1)
|
|
341
|
+
return { cleaned: stream };
|
|
342
|
+
const before = stream.slice(0, idx).replace(/\n$/, "");
|
|
343
|
+
const rest = stream.slice(idx + RESULT_SENTINEL.length);
|
|
344
|
+
const newlineIdx = rest.indexOf(`
|
|
345
|
+
`);
|
|
346
|
+
const payload = newlineIdx === -1 ? rest : rest.slice(0, newlineIdx);
|
|
347
|
+
const after = newlineIdx === -1 ? "" : rest.slice(newlineIdx + 1);
|
|
348
|
+
try {
|
|
349
|
+
return { cleaned: (before + after).trimEnd(), result: JSON.parse(payload) };
|
|
350
|
+
} catch {
|
|
351
|
+
return { cleaned: stream };
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
async function executeSandboxCode(options) {
|
|
355
|
+
const startedAt = Date.now();
|
|
356
|
+
const runtime = options.runtime ?? "node22";
|
|
357
|
+
const timeoutMs = options.timeoutMs ?? 60000;
|
|
358
|
+
const networkPolicy = options.networkPolicy ?? defaultNetworkPolicy(options.mcpUrl);
|
|
359
|
+
let sandbox = null;
|
|
360
|
+
try {
|
|
361
|
+
const Sandbox = await loadSandboxFactory();
|
|
362
|
+
sandbox = await Sandbox.create({
|
|
363
|
+
runtime,
|
|
364
|
+
timeout: timeoutMs,
|
|
365
|
+
resources: options.vcpus ? { vcpus: options.vcpus } : undefined,
|
|
366
|
+
networkPolicy
|
|
367
|
+
});
|
|
368
|
+
const runtimeContent = Buffer.from(RUNTIME_STUB_SOURCE, "utf-8");
|
|
369
|
+
const userContent = Buffer.from(wrapUserCode(options.code), "utf-8");
|
|
370
|
+
await sandbox.writeFiles([
|
|
371
|
+
{ path: "runtime.mjs", content: runtimeContent },
|
|
372
|
+
{ path: "user.mjs", content: userContent }
|
|
373
|
+
]);
|
|
374
|
+
const env = {
|
|
375
|
+
INTEGRATE_MCP_URL: options.mcpUrl
|
|
376
|
+
};
|
|
377
|
+
if (options.sessionToken)
|
|
378
|
+
env.INTEGRATE_SESSION_TOKEN = options.sessionToken;
|
|
379
|
+
if (options.providerTokens && Object.keys(options.providerTokens).length > 0) {
|
|
380
|
+
env.INTEGRATE_PROVIDER_TOKENS = JSON.stringify(options.providerTokens);
|
|
381
|
+
}
|
|
382
|
+
if (options.integrationsHeader)
|
|
383
|
+
env.INTEGRATE_INTEGRATIONS = options.integrationsHeader;
|
|
384
|
+
if (options.context)
|
|
385
|
+
env.INTEGRATE_CONTEXT = JSON.stringify(options.context);
|
|
386
|
+
const cmd = await sandbox.runCommand({
|
|
387
|
+
cmd: "node",
|
|
388
|
+
args: ["user.mjs"],
|
|
389
|
+
env
|
|
390
|
+
});
|
|
391
|
+
const [stdoutRaw, stderrRaw] = await Promise.all([cmd.stdout(), cmd.stderr()]);
|
|
392
|
+
const stdoutExtract = extractResult(stdoutRaw);
|
|
393
|
+
const stderrExtract = extractResult(stderrRaw);
|
|
394
|
+
return {
|
|
395
|
+
success: cmd.exitCode === 0,
|
|
396
|
+
exitCode: cmd.exitCode,
|
|
397
|
+
result: stdoutExtract.result ?? stderrExtract.result,
|
|
398
|
+
stdout: stdoutExtract.cleaned,
|
|
399
|
+
stderr: stderrExtract.cleaned,
|
|
400
|
+
durationMs: Date.now() - startedAt
|
|
401
|
+
};
|
|
402
|
+
} catch (err) {
|
|
403
|
+
return {
|
|
404
|
+
success: false,
|
|
405
|
+
exitCode: -1,
|
|
406
|
+
stdout: "",
|
|
407
|
+
stderr: "",
|
|
408
|
+
durationMs: Date.now() - startedAt,
|
|
409
|
+
error: err instanceof Error ? err.message : String(err)
|
|
410
|
+
};
|
|
411
|
+
} finally {
|
|
412
|
+
if (sandbox) {
|
|
413
|
+
try {
|
|
414
|
+
await sandbox.stop();
|
|
415
|
+
} catch {}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// ../utils/env.ts
|
|
421
|
+
function getEnv(key) {
|
|
422
|
+
try {
|
|
423
|
+
const getImportMeta = new Function('return typeof import.meta !== "undefined" ? import.meta : undefined');
|
|
424
|
+
const importMeta = getImportMeta();
|
|
425
|
+
if (importMeta && typeof importMeta.env === "object" && importMeta.env !== null) {
|
|
426
|
+
const value = importMeta.env[key];
|
|
427
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
428
|
+
return String(value);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
} catch {}
|
|
432
|
+
if (typeof process !== "undefined" && process.env) {
|
|
433
|
+
const value = process.env[key];
|
|
434
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
435
|
+
return value;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// tool-builder.ts
|
|
442
|
+
var CODE_MODE_TOOL_NAME = "execute_code";
|
|
443
|
+
var DEFAULT_INSTRUCTIONS = [
|
|
444
|
+
"You are given a single tool: `execute_code`. Instead of calling individual MCP tools,",
|
|
445
|
+
"you write a short async TypeScript/JavaScript snippet that uses the typed `client`",
|
|
446
|
+
"object below, and the snippet runs in an isolated sandbox which dispatches the actual",
|
|
447
|
+
"tool calls. Chain multiple operations together in one snippet whenever possible —",
|
|
448
|
+
"that is the whole point of this tool.",
|
|
449
|
+
"",
|
|
450
|
+
"Rules:",
|
|
451
|
+
"- The snippet is the body of an `async` function. Use `await` freely.",
|
|
452
|
+
"- Use `return <value>` at the end to hand a structured result back to the caller;",
|
|
453
|
+
" the caller receives it as JSON.",
|
|
454
|
+
"- Use `console.log(...)` for intermediate observations you want to read later.",
|
|
455
|
+
"- Throw / let errors propagate; the runtime will surface them with a non-zero exit.",
|
|
456
|
+
"- Each method call returns an object of shape `ToolResult` (see types below).",
|
|
457
|
+
" The payload usually lives in `result.content[0].text` as JSON — parse it if needed.",
|
|
458
|
+
"- You cannot import npm packages. Only the pre-imported `client` and standard",
|
|
459
|
+
" globals (`fetch`, `console`, `JSON`, ...) are available.",
|
|
460
|
+
"",
|
|
461
|
+
"API surface:"
|
|
462
|
+
].join(`
|
|
463
|
+
`);
|
|
464
|
+
function resolveCodeModeClientConfig(client) {
|
|
465
|
+
const oauthConfig = client.__oauthConfig;
|
|
466
|
+
return oauthConfig?.codeMode ?? {};
|
|
467
|
+
}
|
|
468
|
+
function buildCodeModeTool(client, options) {
|
|
469
|
+
const { tools, providerTokens, context, integrationIds } = options;
|
|
470
|
+
const generated = generateCodeModeTypes(tools);
|
|
471
|
+
const serverCodeModeConfig = resolveCodeModeClientConfig(client);
|
|
472
|
+
const sandboxOverrides = options.sandbox ?? {};
|
|
473
|
+
const description = `${DEFAULT_INSTRUCTIONS}
|
|
474
|
+
|
|
475
|
+
\`\`\`typescript
|
|
476
|
+
${generated.source}
|
|
477
|
+
\`\`\``;
|
|
478
|
+
const execute = async ({ code }) => {
|
|
479
|
+
const publicUrl = sandboxOverrides.publicUrl ?? serverCodeModeConfig.publicUrl ?? getEnv("INTEGRATE_PUBLIC_URL");
|
|
480
|
+
if (!publicUrl) {
|
|
481
|
+
return {
|
|
482
|
+
success: false,
|
|
483
|
+
exitCode: -1,
|
|
484
|
+
stdout: "",
|
|
485
|
+
stderr: "",
|
|
486
|
+
durationMs: 0,
|
|
487
|
+
error: "Code Mode requires `codeMode.publicUrl` in createMCPServer config (or the INTEGRATE_PUBLIC_URL env var). " + "The sandbox uses it to call back into /api/integrate/mcp."
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
const mcpUrl = publicUrl.replace(/\/$/, "") + "/api/integrate/mcp";
|
|
491
|
+
return executeSandboxCode({
|
|
492
|
+
code,
|
|
493
|
+
mcpUrl,
|
|
494
|
+
providerTokens,
|
|
495
|
+
context,
|
|
496
|
+
integrationsHeader: integrationIds && integrationIds.length > 0 ? integrationIds.join(",") : undefined,
|
|
497
|
+
runtime: sandboxOverrides.runtime ?? serverCodeModeConfig.runtime,
|
|
498
|
+
timeoutMs: sandboxOverrides.timeoutMs ?? serverCodeModeConfig.timeoutMs,
|
|
499
|
+
vcpus: sandboxOverrides.vcpus ?? serverCodeModeConfig.vcpus,
|
|
500
|
+
networkPolicy: sandboxOverrides.networkPolicy ?? serverCodeModeConfig.networkPolicy
|
|
501
|
+
});
|
|
502
|
+
};
|
|
503
|
+
return {
|
|
504
|
+
name: CODE_MODE_TOOL_NAME,
|
|
505
|
+
description,
|
|
506
|
+
parameters: {
|
|
507
|
+
type: "object",
|
|
508
|
+
properties: {
|
|
509
|
+
code: {
|
|
510
|
+
type: "string",
|
|
511
|
+
description: "The TypeScript/JavaScript snippet to execute. It is wrapped in an async IIFE, so you may use top-level await and return a final value."
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
required: ["code"],
|
|
515
|
+
additionalProperties: false
|
|
516
|
+
},
|
|
517
|
+
execute
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
export {
|
|
521
|
+
generateCodeModeTypes,
|
|
522
|
+
executeSandboxCode,
|
|
523
|
+
buildCodeModeTool,
|
|
524
|
+
__setSandboxFactoryForTests,
|
|
525
|
+
RUNTIME_STUB_SOURCE,
|
|
526
|
+
CODE_MODE_TOOL_NAME
|
|
527
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code Mode: sandbox-side runtime stub
|
|
3
|
+
*
|
|
4
|
+
* This file exports a string constant containing the runtime module that gets
|
|
5
|
+
* written into the Vercel Sandbox as `runtime.mjs`. User code imports the
|
|
6
|
+
* `client` from this module and calls `client.<integration>.<method>(args)`.
|
|
7
|
+
* Each call proxies to `POST {INTEGRATE_MCP_URL}` with a Bearer token pulled
|
|
8
|
+
* from `INTEGRATE_SESSION_TOKEN`, reaching the existing `/api/integrate/mcp`
|
|
9
|
+
* route in the host server.
|
|
10
|
+
*
|
|
11
|
+
* The stub is plain ESM that runs under `node22`/`node24` with zero
|
|
12
|
+
* dependencies. It is NOT imported by the host SDK — it only ships as a
|
|
13
|
+
* literal payload to the sandbox.
|
|
14
|
+
*/
|
|
15
|
+
export declare const RUNTIME_STUB_SOURCE = "// runtime.mjs \u2014 generated by integrate-sdk code mode\nconst MCP_URL = process.env.INTEGRATE_MCP_URL;\nconst SESSION_TOKEN = process.env.INTEGRATE_SESSION_TOKEN;\nconst PROVIDER_TOKENS = process.env.INTEGRATE_PROVIDER_TOKENS || '';\nconst INTEGRATIONS_HEADER = process.env.INTEGRATE_INTEGRATIONS || '';\nconst CONTEXT_JSON = process.env.INTEGRATE_CONTEXT || '';\n\nif (!MCP_URL) {\n throw new Error('INTEGRATE_MCP_URL is not set \u2014 the sandbox cannot reach the MCP route.');\n}\n\nfunction camelToSnake(str) {\n return str.replace(/[A-Z]/g, (letter) => '_' + letter.toLowerCase());\n}\n\nasync function callTool(toolName, args) {\n const headers = {\n 'Content-Type': 'application/json',\n 'x-integrate-code-mode': '1',\n };\n if (SESSION_TOKEN) headers['Authorization'] = 'Bearer ' + SESSION_TOKEN;\n if (PROVIDER_TOKENS) headers['x-integrate-tokens'] = PROVIDER_TOKENS;\n if (INTEGRATIONS_HEADER) headers['x-integrations'] = INTEGRATIONS_HEADER;\n if (CONTEXT_JSON) headers['x-integrate-context'] = CONTEXT_JSON;\n\n const res = await fetch(MCP_URL, {\n method: 'POST',\n headers,\n body: JSON.stringify({ name: toolName, arguments: args || {} }),\n });\n\n const text = await res.text();\n let payload;\n try {\n payload = text ? JSON.parse(text) : null;\n } catch {\n payload = { content: [{ type: 'text', text }] };\n }\n\n if (!res.ok) {\n const message = (payload && (payload.error || payload.message)) || 'Tool call failed: HTTP ' + res.status;\n const err = new Error(message);\n err.status = res.status;\n err.toolName = toolName;\n throw err;\n }\n\n return payload;\n}\n\nfunction createIntegrationProxy(integrationId) {\n return new Proxy({}, {\n get(_target, methodName) {\n if (typeof methodName !== 'string') return undefined;\n return (args) => callTool(integrationId + '_' + camelToSnake(methodName), args);\n },\n });\n}\n\nexport const client = new Proxy({}, {\n get(_target, integrationId) {\n if (typeof integrationId !== 'string') return undefined;\n return createIntegrationProxy(integrationId);\n },\n});\n\nexport { callTool };\n";
|
|
16
|
+
//# sourceMappingURL=runtime-stub.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-stub.d.ts","sourceRoot":"","sources":["../../../src/code-mode/runtime-stub.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,eAAO,MAAM,mBAAmB,qmEAmE/B,CAAC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// runtime-stub.ts
|
|
2
|
+
var RUNTIME_STUB_SOURCE = `// runtime.mjs — generated by integrate-sdk code mode
|
|
3
|
+
const MCP_URL = process.env.INTEGRATE_MCP_URL;
|
|
4
|
+
const SESSION_TOKEN = process.env.INTEGRATE_SESSION_TOKEN;
|
|
5
|
+
const PROVIDER_TOKENS = process.env.INTEGRATE_PROVIDER_TOKENS || '';
|
|
6
|
+
const INTEGRATIONS_HEADER = process.env.INTEGRATE_INTEGRATIONS || '';
|
|
7
|
+
const CONTEXT_JSON = process.env.INTEGRATE_CONTEXT || '';
|
|
8
|
+
|
|
9
|
+
if (!MCP_URL) {
|
|
10
|
+
throw new Error('INTEGRATE_MCP_URL is not set — the sandbox cannot reach the MCP route.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function camelToSnake(str) {
|
|
14
|
+
return str.replace(/[A-Z]/g, (letter) => '_' + letter.toLowerCase());
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function callTool(toolName, args) {
|
|
18
|
+
const headers = {
|
|
19
|
+
'Content-Type': 'application/json',
|
|
20
|
+
'x-integrate-code-mode': '1',
|
|
21
|
+
};
|
|
22
|
+
if (SESSION_TOKEN) headers['Authorization'] = 'Bearer ' + SESSION_TOKEN;
|
|
23
|
+
if (PROVIDER_TOKENS) headers['x-integrate-tokens'] = PROVIDER_TOKENS;
|
|
24
|
+
if (INTEGRATIONS_HEADER) headers['x-integrations'] = INTEGRATIONS_HEADER;
|
|
25
|
+
if (CONTEXT_JSON) headers['x-integrate-context'] = CONTEXT_JSON;
|
|
26
|
+
|
|
27
|
+
const res = await fetch(MCP_URL, {
|
|
28
|
+
method: 'POST',
|
|
29
|
+
headers,
|
|
30
|
+
body: JSON.stringify({ name: toolName, arguments: args || {} }),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const text = await res.text();
|
|
34
|
+
let payload;
|
|
35
|
+
try {
|
|
36
|
+
payload = text ? JSON.parse(text) : null;
|
|
37
|
+
} catch {
|
|
38
|
+
payload = { content: [{ type: 'text', text }] };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!res.ok) {
|
|
42
|
+
const message = (payload && (payload.error || payload.message)) || 'Tool call failed: HTTP ' + res.status;
|
|
43
|
+
const err = new Error(message);
|
|
44
|
+
err.status = res.status;
|
|
45
|
+
err.toolName = toolName;
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return payload;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createIntegrationProxy(integrationId) {
|
|
53
|
+
return new Proxy({}, {
|
|
54
|
+
get(_target, methodName) {
|
|
55
|
+
if (typeof methodName !== 'string') return undefined;
|
|
56
|
+
return (args) => callTool(integrationId + '_' + camelToSnake(methodName), args);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const client = new Proxy({}, {
|
|
62
|
+
get(_target, integrationId) {
|
|
63
|
+
if (typeof integrationId !== 'string') return undefined;
|
|
64
|
+
return createIntegrationProxy(integrationId);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export { callTool };
|
|
69
|
+
`;
|
|
70
|
+
export {
|
|
71
|
+
RUNTIME_STUB_SOURCE
|
|
72
|
+
};
|