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.
Files changed (68) hide show
  1. package/dist/adapters/auto-routes.d.ts.map +1 -1
  2. package/dist/adapters/auto-routes.js +727 -17
  3. package/dist/adapters/base-handler.d.ts.map +1 -1
  4. package/dist/adapters/index.js +722 -16
  5. package/dist/adapters/nextjs.d.ts.map +1 -1
  6. package/dist/adapters/nextjs.js +722 -16
  7. package/dist/adapters/node.js +722 -16
  8. package/dist/adapters/svelte-kit.js +722 -16
  9. package/dist/adapters/tanstack-start.js +722 -16
  10. package/dist/ai/anthropic.d.ts +11 -0
  11. package/dist/ai/anthropic.d.ts.map +1 -1
  12. package/dist/ai/anthropic.js +552 -2
  13. package/dist/ai/google.d.ts +11 -0
  14. package/dist/ai/google.d.ts.map +1 -1
  15. package/dist/ai/google.js +561 -2
  16. package/dist/ai/index.js +648 -8
  17. package/dist/ai/openai.d.ts +11 -0
  18. package/dist/ai/openai.d.ts.map +1 -1
  19. package/dist/ai/openai.js +550 -2
  20. package/dist/ai/vercel-ai.d.ts +13 -0
  21. package/dist/ai/vercel-ai.d.ts.map +1 -1
  22. package/dist/ai/vercel-ai.js +536 -2
  23. package/dist/code-mode/executor.d.ts +99 -0
  24. package/dist/code-mode/executor.d.ts.map +1 -0
  25. package/dist/code-mode/executor.js +207 -0
  26. package/dist/code-mode/index.d.ts +12 -0
  27. package/dist/code-mode/index.d.ts.map +1 -0
  28. package/dist/code-mode/index.js +527 -0
  29. package/dist/code-mode/runtime-stub.d.ts +16 -0
  30. package/dist/code-mode/runtime-stub.d.ts.map +1 -0
  31. package/dist/code-mode/runtime-stub.js +72 -0
  32. package/dist/code-mode/tool-builder.d.ts +83 -0
  33. package/dist/code-mode/tool-builder.d.ts.map +1 -0
  34. package/dist/code-mode/tool-builder.js +524 -0
  35. package/dist/code-mode/type-generator.d.ts +22 -0
  36. package/dist/code-mode/type-generator.d.ts.map +1 -0
  37. package/dist/code-mode/type-generator.js +217 -0
  38. package/dist/index.js +722 -16
  39. package/dist/oauth.js +727 -17
  40. package/dist/server.d.ts +1 -0
  41. package/dist/server.d.ts.map +1 -1
  42. package/dist/server.js +733 -17
  43. package/dist/src/adapters/auto-routes.d.ts.map +1 -1
  44. package/dist/src/adapters/base-handler.d.ts.map +1 -1
  45. package/dist/src/adapters/nextjs.d.ts.map +1 -1
  46. package/dist/src/ai/anthropic.d.ts +11 -0
  47. package/dist/src/ai/anthropic.d.ts.map +1 -1
  48. package/dist/src/ai/google.d.ts +11 -0
  49. package/dist/src/ai/google.d.ts.map +1 -1
  50. package/dist/src/ai/openai.d.ts +11 -0
  51. package/dist/src/ai/openai.d.ts.map +1 -1
  52. package/dist/src/ai/vercel-ai.d.ts +13 -0
  53. package/dist/src/ai/vercel-ai.d.ts.map +1 -1
  54. package/dist/src/code-mode/executor.d.ts +99 -0
  55. package/dist/src/code-mode/executor.d.ts.map +1 -0
  56. package/dist/src/code-mode/index.d.ts +12 -0
  57. package/dist/src/code-mode/index.d.ts.map +1 -0
  58. package/dist/src/code-mode/runtime-stub.d.ts +16 -0
  59. package/dist/src/code-mode/runtime-stub.d.ts.map +1 -0
  60. package/dist/src/code-mode/tool-builder.d.ts +83 -0
  61. package/dist/src/code-mode/tool-builder.d.ts.map +1 -0
  62. package/dist/src/code-mode/type-generator.d.ts +22 -0
  63. package/dist/src/code-mode/type-generator.d.ts.map +1 -0
  64. package/dist/src/config/types.d.ts +55 -0
  65. package/dist/src/config/types.d.ts.map +1 -1
  66. package/dist/src/server.d.ts.map +1 -1
  67. package/package.json +15 -6
  68. package/server.ts +9 -0
@@ -4402,6 +4402,523 @@ function createTriggerTools(config, context) {
4402
4402
  };
4403
4403
  }
4404
4404
 
4405
+ // ../utils/naming.ts
4406
+ function snakeToCamel(str) {
4407
+ return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
4408
+ }
4409
+ function toolNameToMethod(toolName) {
4410
+ const withoutPrefix = toolName.replace(/^[^_]+_/, "");
4411
+ return snakeToCamel(withoutPrefix);
4412
+ }
4413
+
4414
+ // ../code-mode/type-generator.ts
4415
+ var RESERVED_TS = new Set([
4416
+ "break",
4417
+ "case",
4418
+ "catch",
4419
+ "class",
4420
+ "const",
4421
+ "continue",
4422
+ "debugger",
4423
+ "default",
4424
+ "delete",
4425
+ "do",
4426
+ "else",
4427
+ "enum",
4428
+ "export",
4429
+ "extends",
4430
+ "false",
4431
+ "finally",
4432
+ "for",
4433
+ "function",
4434
+ "if",
4435
+ "import",
4436
+ "in",
4437
+ "instanceof",
4438
+ "new",
4439
+ "null",
4440
+ "return",
4441
+ "super",
4442
+ "switch",
4443
+ "this",
4444
+ "throw",
4445
+ "true",
4446
+ "try",
4447
+ "typeof",
4448
+ "var",
4449
+ "void",
4450
+ "while",
4451
+ "with",
4452
+ "as",
4453
+ "implements",
4454
+ "interface",
4455
+ "let",
4456
+ "package",
4457
+ "private",
4458
+ "protected",
4459
+ "public",
4460
+ "static",
4461
+ "yield"
4462
+ ]);
4463
+ function safeIdent(name) {
4464
+ if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) || RESERVED_TS.has(name)) {
4465
+ return JSON.stringify(name);
4466
+ }
4467
+ return name;
4468
+ }
4469
+ function integrationFromToolName(toolName) {
4470
+ const idx = toolName.indexOf("_");
4471
+ return idx === -1 ? toolName : toolName.slice(0, idx);
4472
+ }
4473
+ function jsonSchemaToTs(schema, indent) {
4474
+ if (!schema || typeof schema !== "object")
4475
+ return "unknown";
4476
+ const s = schema;
4477
+ if (Array.isArray(s.enum) && s.enum.length > 0) {
4478
+ return s.enum.map((v) => typeof v === "string" ? JSON.stringify(v) : typeof v === "number" || typeof v === "boolean" ? String(v) : "unknown").join(" | ");
4479
+ }
4480
+ if (Array.isArray(s.type)) {
4481
+ return s.type.map((t2) => jsonSchemaToTs({ ...s, type: t2 }, indent)).join(" | ");
4482
+ }
4483
+ const t = s.type;
4484
+ switch (t) {
4485
+ case "string":
4486
+ return "string";
4487
+ case "number":
4488
+ case "integer":
4489
+ return "number";
4490
+ case "boolean":
4491
+ return "boolean";
4492
+ case "null":
4493
+ return "null";
4494
+ case "array": {
4495
+ const items = s.items;
4496
+ if (Array.isArray(items))
4497
+ return "unknown[]";
4498
+ const inner = jsonSchemaToTs(items, indent);
4499
+ return /[|&]/.test(inner) ? `Array<${inner}>` : `${inner}[]`;
4500
+ }
4501
+ case "object":
4502
+ return objectShape(s, indent);
4503
+ default:
4504
+ if (s.properties && typeof s.properties === "object")
4505
+ return objectShape(s, indent);
4506
+ return "unknown";
4507
+ }
4508
+ }
4509
+ function objectShape(schema, indent) {
4510
+ const props = schema.properties && typeof schema.properties === "object" ? schema.properties : {};
4511
+ const keys = Object.keys(props);
4512
+ if (keys.length === 0) {
4513
+ return schema.additionalProperties === false ? "Record<string, never>" : "Record<string, unknown>";
4514
+ }
4515
+ const required = new Set(Array.isArray(schema.required) ? schema.required : []);
4516
+ const inner = indent + " ";
4517
+ const lines = ["{"];
4518
+ for (const key of keys) {
4519
+ const prop = props[key];
4520
+ const desc = prop && typeof prop.description === "string" ? prop.description : undefined;
4521
+ if (desc)
4522
+ lines.push(`${inner}/** ${desc.replace(/\*\//g, "*\\/")} */`);
4523
+ const optional = required.has(key) ? "" : "?";
4524
+ const type = jsonSchemaToTs(prop, inner);
4525
+ lines.push(`${inner}${safeIdent(key)}${optional}: ${type};`);
4526
+ }
4527
+ lines.push(`${indent}}`);
4528
+ return lines.join(`
4529
+ `);
4530
+ }
4531
+ function argsType(schema) {
4532
+ if (!schema)
4533
+ return "Record<string, unknown>";
4534
+ const hasProps = schema.properties && Object.keys(schema.properties).length > 0;
4535
+ if (!hasProps)
4536
+ return "Record<string, unknown>";
4537
+ return objectShape(schema, " ");
4538
+ }
4539
+ function methodHasRequiredArgs(schema) {
4540
+ if (!schema || !schema.properties)
4541
+ return false;
4542
+ const req = Array.isArray(schema.required) ? schema.required : [];
4543
+ return req.length > 0;
4544
+ }
4545
+ function formatDescription(desc, indent) {
4546
+ if (!desc)
4547
+ return "";
4548
+ const cleaned = desc.replace(/\*\//g, "*\\/").trim();
4549
+ if (!cleaned.includes(`
4550
+ `))
4551
+ return `${indent}/** ${cleaned} */
4552
+ `;
4553
+ const lines = cleaned.split(`
4554
+ `).map((l) => `${indent} * ${l}`).join(`
4555
+ `);
4556
+ return `${indent}/**
4557
+ ${lines}
4558
+ ${indent} */
4559
+ `;
4560
+ }
4561
+ function generateCodeModeTypes(tools) {
4562
+ const byIntegration = {};
4563
+ for (const tool of tools) {
4564
+ const integration = integrationFromToolName(tool.name);
4565
+ (byIntegration[integration] ??= []).push(tool);
4566
+ }
4567
+ const methodMap = {};
4568
+ const integrationCounts = {};
4569
+ const sections = [];
4570
+ const integrationIds = Object.keys(byIntegration).sort();
4571
+ sections.push("/**");
4572
+ sections.push(" * Integrate SDK — available APIs inside `execute_code`.");
4573
+ sections.push(" * Every method is async and returns the MCP tool-call response.");
4574
+ sections.push(" * Call them via the exported `client` object, e.g.");
4575
+ sections.push(" * const repos = await client.github.listRepos();");
4576
+ sections.push(" */");
4577
+ sections.push("");
4578
+ for (const integrationId of integrationIds) {
4579
+ const integrationTools = byIntegration[integrationId].slice().sort((a, b) => a.name.localeCompare(b.name));
4580
+ integrationCounts[integrationId] = integrationTools.length;
4581
+ const interfaceName = pascalCase(integrationId) + "Client";
4582
+ sections.push(`export interface ${interfaceName} {`);
4583
+ for (const tool of integrationTools) {
4584
+ const methodName = toolNameToMethod(tool.name);
4585
+ methodMap[`${integrationId}.${methodName}`] = tool.name;
4586
+ sections.push(formatDescription(tool.description, " "));
4587
+ const argType = argsType(tool.inputSchema);
4588
+ const argIsOptional = !methodHasRequiredArgs(tool.inputSchema);
4589
+ const paramName = argIsOptional ? "args?" : "args";
4590
+ sections.push(` ${safeIdent(methodName)}(${paramName}: ${argType}): Promise<ToolResult>;`);
4591
+ }
4592
+ sections.push("}");
4593
+ sections.push("");
4594
+ }
4595
+ sections.push("export interface ToolResult {");
4596
+ sections.push(" content: Array<{ type: 'text' | 'image' | 'resource'; text?: string; data?: string; mimeType?: string; [key: string]: unknown }>;");
4597
+ sections.push(" isError?: boolean;");
4598
+ sections.push(" structuredContent?: Record<string, unknown>;");
4599
+ sections.push("}");
4600
+ sections.push("");
4601
+ sections.push("export interface Client {");
4602
+ for (const integrationId of integrationIds) {
4603
+ const interfaceName = pascalCase(integrationId) + "Client";
4604
+ sections.push(` ${safeIdent(integrationId)}: ${interfaceName};`);
4605
+ }
4606
+ sections.push("}");
4607
+ sections.push("");
4608
+ sections.push("export declare const client: Client;");
4609
+ return {
4610
+ source: sections.filter((line, idx, arr) => !(line === "" && arr[idx - 1] === "")).join(`
4611
+ `),
4612
+ methodMap,
4613
+ integrationCounts
4614
+ };
4615
+ }
4616
+ function pascalCase(id) {
4617
+ return id.split(/[^A-Za-z0-9]/).filter(Boolean).map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join("") || "Unknown";
4618
+ }
4619
+
4620
+ // ../code-mode/runtime-stub.ts
4621
+ var RUNTIME_STUB_SOURCE = `// runtime.mjs — generated by integrate-sdk code mode
4622
+ const MCP_URL = process.env.INTEGRATE_MCP_URL;
4623
+ const SESSION_TOKEN = process.env.INTEGRATE_SESSION_TOKEN;
4624
+ const PROVIDER_TOKENS = process.env.INTEGRATE_PROVIDER_TOKENS || '';
4625
+ const INTEGRATIONS_HEADER = process.env.INTEGRATE_INTEGRATIONS || '';
4626
+ const CONTEXT_JSON = process.env.INTEGRATE_CONTEXT || '';
4627
+
4628
+ if (!MCP_URL) {
4629
+ throw new Error('INTEGRATE_MCP_URL is not set — the sandbox cannot reach the MCP route.');
4630
+ }
4631
+
4632
+ function camelToSnake(str) {
4633
+ return str.replace(/[A-Z]/g, (letter) => '_' + letter.toLowerCase());
4634
+ }
4635
+
4636
+ async function callTool(toolName, args) {
4637
+ const headers = {
4638
+ 'Content-Type': 'application/json',
4639
+ 'x-integrate-code-mode': '1',
4640
+ };
4641
+ if (SESSION_TOKEN) headers['Authorization'] = 'Bearer ' + SESSION_TOKEN;
4642
+ if (PROVIDER_TOKENS) headers['x-integrate-tokens'] = PROVIDER_TOKENS;
4643
+ if (INTEGRATIONS_HEADER) headers['x-integrations'] = INTEGRATIONS_HEADER;
4644
+ if (CONTEXT_JSON) headers['x-integrate-context'] = CONTEXT_JSON;
4645
+
4646
+ const res = await fetch(MCP_URL, {
4647
+ method: 'POST',
4648
+ headers,
4649
+ body: JSON.stringify({ name: toolName, arguments: args || {} }),
4650
+ });
4651
+
4652
+ const text = await res.text();
4653
+ let payload;
4654
+ try {
4655
+ payload = text ? JSON.parse(text) : null;
4656
+ } catch {
4657
+ payload = { content: [{ type: 'text', text }] };
4658
+ }
4659
+
4660
+ if (!res.ok) {
4661
+ const message = (payload && (payload.error || payload.message)) || 'Tool call failed: HTTP ' + res.status;
4662
+ const err = new Error(message);
4663
+ err.status = res.status;
4664
+ err.toolName = toolName;
4665
+ throw err;
4666
+ }
4667
+
4668
+ return payload;
4669
+ }
4670
+
4671
+ function createIntegrationProxy(integrationId) {
4672
+ return new Proxy({}, {
4673
+ get(_target, methodName) {
4674
+ if (typeof methodName !== 'string') return undefined;
4675
+ return (args) => callTool(integrationId + '_' + camelToSnake(methodName), args);
4676
+ },
4677
+ });
4678
+ }
4679
+
4680
+ export const client = new Proxy({}, {
4681
+ get(_target, integrationId) {
4682
+ if (typeof integrationId !== 'string') return undefined;
4683
+ return createIntegrationProxy(integrationId);
4684
+ },
4685
+ });
4686
+
4687
+ export { callTool };
4688
+ `;
4689
+
4690
+ // ../code-mode/executor.ts
4691
+ var sandboxFactoryOverride = null;
4692
+ async function loadSandboxFactory() {
4693
+ if (sandboxFactoryOverride)
4694
+ return sandboxFactoryOverride;
4695
+ try {
4696
+ const dynamicImport = new Function("specifier", "return import(specifier)");
4697
+ const pkg = "@" + "vercel/sandbox";
4698
+ const mod = await dynamicImport(pkg);
4699
+ return mod.Sandbox ?? mod.default?.Sandbox ?? mod;
4700
+ } catch (err) {
4701
+ throw new Error("Code Mode requires the optional peer dependency `@vercel/sandbox`. " + "Install it with `npm install @vercel/sandbox` (or `bun add @vercel/sandbox`).");
4702
+ }
4703
+ }
4704
+ var RESULT_SENTINEL = "__INTEGRATE_RESULT__";
4705
+ function wrapUserCode(code) {
4706
+ return `// user.mjs — wrapped by integrate-sdk code mode
4707
+ import { client, callTool } from './runtime.mjs';
4708
+
4709
+ (async () => {
4710
+ try {
4711
+ const __result = await (async () => {
4712
+ ${code}
4713
+ })();
4714
+ if (typeof __result !== 'undefined') {
4715
+ process.stdout.write('\\n' + ${JSON.stringify(RESULT_SENTINEL)} + JSON.stringify(__result) + '\\n');
4716
+ }
4717
+ } catch (err) {
4718
+ const payload = {
4719
+ message: err && err.message ? err.message : String(err),
4720
+ name: err && err.name,
4721
+ stack: err && err.stack,
4722
+ toolName: err && err.toolName,
4723
+ status: err && err.status,
4724
+ };
4725
+ process.stderr.write('\\n' + ${JSON.stringify(RESULT_SENTINEL)} + JSON.stringify({ error: payload }) + '\\n');
4726
+ process.exit(1);
4727
+ }
4728
+ })();
4729
+ `;
4730
+ }
4731
+ function defaultNetworkPolicy(mcpUrl) {
4732
+ try {
4733
+ const host = new URL(mcpUrl).hostname;
4734
+ return { allow: [host] };
4735
+ } catch {
4736
+ return { allow: [] };
4737
+ }
4738
+ }
4739
+ function extractResult(stream) {
4740
+ const idx = stream.lastIndexOf(RESULT_SENTINEL);
4741
+ if (idx === -1)
4742
+ return { cleaned: stream };
4743
+ const before = stream.slice(0, idx).replace(/\n$/, "");
4744
+ const rest = stream.slice(idx + RESULT_SENTINEL.length);
4745
+ const newlineIdx = rest.indexOf(`
4746
+ `);
4747
+ const payload = newlineIdx === -1 ? rest : rest.slice(0, newlineIdx);
4748
+ const after = newlineIdx === -1 ? "" : rest.slice(newlineIdx + 1);
4749
+ try {
4750
+ return { cleaned: (before + after).trimEnd(), result: JSON.parse(payload) };
4751
+ } catch {
4752
+ return { cleaned: stream };
4753
+ }
4754
+ }
4755
+ async function executeSandboxCode(options) {
4756
+ const startedAt = Date.now();
4757
+ const runtime = options.runtime ?? "node22";
4758
+ const timeoutMs = options.timeoutMs ?? 60000;
4759
+ const networkPolicy = options.networkPolicy ?? defaultNetworkPolicy(options.mcpUrl);
4760
+ let sandbox = null;
4761
+ try {
4762
+ const Sandbox = await loadSandboxFactory();
4763
+ sandbox = await Sandbox.create({
4764
+ runtime,
4765
+ timeout: timeoutMs,
4766
+ resources: options.vcpus ? { vcpus: options.vcpus } : undefined,
4767
+ networkPolicy
4768
+ });
4769
+ const runtimeContent = Buffer.from(RUNTIME_STUB_SOURCE, "utf-8");
4770
+ const userContent = Buffer.from(wrapUserCode(options.code), "utf-8");
4771
+ await sandbox.writeFiles([
4772
+ { path: "runtime.mjs", content: runtimeContent },
4773
+ { path: "user.mjs", content: userContent }
4774
+ ]);
4775
+ const env = {
4776
+ INTEGRATE_MCP_URL: options.mcpUrl
4777
+ };
4778
+ if (options.sessionToken)
4779
+ env.INTEGRATE_SESSION_TOKEN = options.sessionToken;
4780
+ if (options.providerTokens && Object.keys(options.providerTokens).length > 0) {
4781
+ env.INTEGRATE_PROVIDER_TOKENS = JSON.stringify(options.providerTokens);
4782
+ }
4783
+ if (options.integrationsHeader)
4784
+ env.INTEGRATE_INTEGRATIONS = options.integrationsHeader;
4785
+ if (options.context)
4786
+ env.INTEGRATE_CONTEXT = JSON.stringify(options.context);
4787
+ const cmd = await sandbox.runCommand({
4788
+ cmd: "node",
4789
+ args: ["user.mjs"],
4790
+ env
4791
+ });
4792
+ const [stdoutRaw, stderrRaw] = await Promise.all([cmd.stdout(), cmd.stderr()]);
4793
+ const stdoutExtract = extractResult(stdoutRaw);
4794
+ const stderrExtract = extractResult(stderrRaw);
4795
+ return {
4796
+ success: cmd.exitCode === 0,
4797
+ exitCode: cmd.exitCode,
4798
+ result: stdoutExtract.result ?? stderrExtract.result,
4799
+ stdout: stdoutExtract.cleaned,
4800
+ stderr: stderrExtract.cleaned,
4801
+ durationMs: Date.now() - startedAt
4802
+ };
4803
+ } catch (err) {
4804
+ return {
4805
+ success: false,
4806
+ exitCode: -1,
4807
+ stdout: "",
4808
+ stderr: "",
4809
+ durationMs: Date.now() - startedAt,
4810
+ error: err instanceof Error ? err.message : String(err)
4811
+ };
4812
+ } finally {
4813
+ if (sandbox) {
4814
+ try {
4815
+ await sandbox.stop();
4816
+ } catch {}
4817
+ }
4818
+ }
4819
+ }
4820
+
4821
+ // ../utils/env.ts
4822
+ function getEnv(key) {
4823
+ try {
4824
+ const getImportMeta = new Function('return typeof import.meta !== "undefined" ? import.meta : undefined');
4825
+ const importMeta = getImportMeta();
4826
+ if (importMeta && typeof importMeta.env === "object" && importMeta.env !== null) {
4827
+ const value = importMeta.env[key];
4828
+ if (value !== undefined && value !== null && value !== "") {
4829
+ return String(value);
4830
+ }
4831
+ }
4832
+ } catch {}
4833
+ if (typeof process !== "undefined" && process.env) {
4834
+ const value = process.env[key];
4835
+ if (value !== undefined && value !== null && value !== "") {
4836
+ return value;
4837
+ }
4838
+ }
4839
+ return;
4840
+ }
4841
+
4842
+ // ../code-mode/tool-builder.ts
4843
+ var CODE_MODE_TOOL_NAME = "execute_code";
4844
+ var DEFAULT_INSTRUCTIONS = [
4845
+ "You are given a single tool: `execute_code`. Instead of calling individual MCP tools,",
4846
+ "you write a short async TypeScript/JavaScript snippet that uses the typed `client`",
4847
+ "object below, and the snippet runs in an isolated sandbox which dispatches the actual",
4848
+ "tool calls. Chain multiple operations together in one snippet whenever possible —",
4849
+ "that is the whole point of this tool.",
4850
+ "",
4851
+ "Rules:",
4852
+ "- The snippet is the body of an `async` function. Use `await` freely.",
4853
+ "- Use `return <value>` at the end to hand a structured result back to the caller;",
4854
+ " the caller receives it as JSON.",
4855
+ "- Use `console.log(...)` for intermediate observations you want to read later.",
4856
+ "- Throw / let errors propagate; the runtime will surface them with a non-zero exit.",
4857
+ "- Each method call returns an object of shape `ToolResult` (see types below).",
4858
+ " The payload usually lives in `result.content[0].text` as JSON — parse it if needed.",
4859
+ "- You cannot import npm packages. Only the pre-imported `client` and standard",
4860
+ " globals (`fetch`, `console`, `JSON`, ...) are available.",
4861
+ "",
4862
+ "API surface:"
4863
+ ].join(`
4864
+ `);
4865
+ function resolveCodeModeClientConfig(client) {
4866
+ const oauthConfig = client.__oauthConfig;
4867
+ return oauthConfig?.codeMode ?? {};
4868
+ }
4869
+ function buildCodeModeTool(client, options) {
4870
+ const { tools, providerTokens, context, integrationIds } = options;
4871
+ const generated = generateCodeModeTypes(tools);
4872
+ const serverCodeModeConfig = resolveCodeModeClientConfig(client);
4873
+ const sandboxOverrides = options.sandbox ?? {};
4874
+ const description = `${DEFAULT_INSTRUCTIONS}
4875
+
4876
+ \`\`\`typescript
4877
+ ${generated.source}
4878
+ \`\`\``;
4879
+ const execute = async ({ code }) => {
4880
+ const publicUrl = sandboxOverrides.publicUrl ?? serverCodeModeConfig.publicUrl ?? getEnv("INTEGRATE_PUBLIC_URL");
4881
+ if (!publicUrl) {
4882
+ return {
4883
+ success: false,
4884
+ exitCode: -1,
4885
+ stdout: "",
4886
+ stderr: "",
4887
+ durationMs: 0,
4888
+ 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."
4889
+ };
4890
+ }
4891
+ const mcpUrl = publicUrl.replace(/\/$/, "") + "/api/integrate/mcp";
4892
+ return executeSandboxCode({
4893
+ code,
4894
+ mcpUrl,
4895
+ providerTokens,
4896
+ context,
4897
+ integrationsHeader: integrationIds && integrationIds.length > 0 ? integrationIds.join(",") : undefined,
4898
+ runtime: sandboxOverrides.runtime ?? serverCodeModeConfig.runtime,
4899
+ timeoutMs: sandboxOverrides.timeoutMs ?? serverCodeModeConfig.timeoutMs,
4900
+ vcpus: sandboxOverrides.vcpus ?? serverCodeModeConfig.vcpus,
4901
+ networkPolicy: sandboxOverrides.networkPolicy ?? serverCodeModeConfig.networkPolicy
4902
+ });
4903
+ };
4904
+ return {
4905
+ name: CODE_MODE_TOOL_NAME,
4906
+ description,
4907
+ parameters: {
4908
+ type: "object",
4909
+ properties: {
4910
+ code: {
4911
+ type: "string",
4912
+ 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."
4913
+ }
4914
+ },
4915
+ required: ["code"],
4916
+ additionalProperties: false
4917
+ },
4918
+ execute
4919
+ };
4920
+ }
4921
+
4405
4922
  // vercel-ai.ts
4406
4923
  function convertMCPToolToVercelAI(mcpTool, client, options) {
4407
4924
  return {
@@ -4426,8 +4943,25 @@ async function getVercelAITools(client, options) {
4426
4943
  await ensureClientConnected(client);
4427
4944
  const mcpTools = await client.getEnabledToolsAsync();
4428
4945
  const vercelTools = {};
4429
- for (const mcpTool of mcpTools) {
4430
- vercelTools[mcpTool.name] = convertMCPToolToVercelAI(mcpTool, client, finalOptions);
4946
+ const mode = options?.mode ?? "code";
4947
+ if (mode === "code") {
4948
+ const codeTool = buildCodeModeTool(client, {
4949
+ tools: mcpTools,
4950
+ providerTokens,
4951
+ context: options?.context,
4952
+ integrationIds: client.__oauthConfig?.integrations?.map((i) => i.id) ?? client.integrations?.map?.((i) => i.id)
4953
+ });
4954
+ vercelTools[CODE_MODE_TOOL_NAME] = {
4955
+ description: codeTool.description,
4956
+ inputSchema: exports_external.object({
4957
+ code: exports_external.string().describe(codeTool.parameters.properties.code.description)
4958
+ }),
4959
+ execute: async (args) => codeTool.execute(args)
4960
+ };
4961
+ } else {
4962
+ for (const mcpTool of mcpTools) {
4963
+ vercelTools[mcpTool.name] = convertMCPToolToVercelAI(mcpTool, client, finalOptions);
4964
+ }
4431
4965
  }
4432
4966
  const triggerConfig = client.__triggerConfig;
4433
4967
  if (triggerConfig) {
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Code Mode: Vercel Sandbox executor
3
+ *
4
+ * Spins up a Vercel Sandbox microVM per `execute_code` call, writes the
5
+ * runtime stub and wrapped user code into it, runs node against the user
6
+ * code, and returns captured stdout/stderr + parsed result.
7
+ *
8
+ * `@vercel/sandbox` is loaded lazily so importing code-mode utilities on the
9
+ * client / from the type generator does not force the dependency to exist.
10
+ */
11
+ import type { MCPContext } from "../config/types.js";
12
+ /**
13
+ * Shape the executor depends on — mirrors the subset of `@vercel/sandbox`
14
+ * we actually use. Declared structurally so we don't force the real package
15
+ * to be installed at type-check time.
16
+ */
17
+ interface SandboxLike {
18
+ writeFiles(files: Array<{
19
+ path: string;
20
+ content: Buffer;
21
+ }>): Promise<void>;
22
+ runCommand(params: {
23
+ cmd: string;
24
+ args?: string[];
25
+ env?: Record<string, string>;
26
+ cwd?: string;
27
+ }): Promise<{
28
+ exitCode: number;
29
+ stdout(): Promise<string>;
30
+ stderr(): Promise<string>;
31
+ }>;
32
+ stop(): Promise<unknown>;
33
+ }
34
+ interface SandboxFactory {
35
+ create(options: {
36
+ runtime?: string;
37
+ timeout?: number;
38
+ resources?: {
39
+ vcpus?: number;
40
+ };
41
+ networkPolicy?: unknown;
42
+ }): Promise<SandboxLike>;
43
+ }
44
+ /** @internal — used by unit tests to stub the sandbox SDK. */
45
+ export declare function __setSandboxFactoryForTests(factory: SandboxFactory | null): void;
46
+ export interface ExecuteSandboxCodeOptions {
47
+ /** Source code the LLM produced. Treated as an async function body. */
48
+ code: string;
49
+ /** Fully-qualified MCP route URL, e.g. `https://myapp.com/api/integrate/mcp`. */
50
+ mcpUrl: string;
51
+ /** Session token forwarded as `Authorization: Bearer <token>` by the stub. */
52
+ sessionToken?: string;
53
+ /**
54
+ * Provider OAuth tokens to forward as `x-integrate-tokens`. Same format the
55
+ * existing AI helpers pass to the `/api/integrate/mcp` route today.
56
+ */
57
+ providerTokens?: Record<string, string>;
58
+ /** Comma-separated integration IDs (forwarded as `x-integrations`). */
59
+ integrationsHeader?: string;
60
+ /** Optional multi-tenant user context. Serialized to JSON for the sandbox. */
61
+ context?: MCPContext;
62
+ /** Sandbox runtime image. Defaults to `'node22'`. */
63
+ runtime?: "node24" | "node22";
64
+ /** Sandbox timeout in ms. Defaults to 60_000. */
65
+ timeoutMs?: number;
66
+ /** vCPUs allocated to the sandbox. Defaults to 2. */
67
+ vcpus?: number;
68
+ /** Egress firewall. Defaults to an allow-list containing only the MCP host. */
69
+ networkPolicy?: "allow-all" | "deny-all" | {
70
+ allow?: string[];
71
+ subnets?: {
72
+ allow?: string[];
73
+ deny?: string[];
74
+ };
75
+ };
76
+ }
77
+ export interface ExecuteSandboxCodeResult {
78
+ /** Whether the sandbox command exited with code 0. */
79
+ success: boolean;
80
+ /** Exit code from `node user.mjs`. */
81
+ exitCode: number;
82
+ /** Parsed return value, if the user code set one via `return`. */
83
+ result?: unknown;
84
+ /** Raw stdout stream (without the `__INTEGRATE_RESULT__` sentinel line). */
85
+ stdout: string;
86
+ /** Raw stderr stream. */
87
+ stderr: string;
88
+ /** Wall-clock duration in milliseconds. */
89
+ durationMs: number;
90
+ /** Error message if the sandbox itself failed (not user-code exit != 0). */
91
+ error?: string;
92
+ }
93
+ /**
94
+ * Execute LLM-authored TypeScript/JS code inside a fresh Vercel Sandbox.
95
+ * Creates one sandbox per call and stops it in `finally`.
96
+ */
97
+ export declare function executeSandboxCode(options: ExecuteSandboxCodeOptions): Promise<ExecuteSandboxCodeResult>;
98
+ export {};
99
+ //# sourceMappingURL=executor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/code-mode/executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGrD;;;;GAIG;AACH,UAAU,WAAW;IACnB,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3E,UAAU,CACR,MAAM,EAAE;Q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