integrate-sdk 0.9.5-dev.0 → 0.9.9-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 (52) hide show
  1. package/dist/adapters/auto-routes.js +129 -10
  2. package/dist/adapters/index.js +129 -10
  3. package/dist/adapters/nextjs.js +129 -10
  4. package/dist/adapters/node.js +129 -10
  5. package/dist/adapters/svelte-kit.js +129 -10
  6. package/dist/adapters/tanstack-start.js +129 -10
  7. package/dist/ai/anthropic.d.ts +2 -2
  8. package/dist/ai/anthropic.d.ts.map +1 -1
  9. package/dist/ai/anthropic.js +64 -2
  10. package/dist/ai/google.d.ts +2 -2
  11. package/dist/ai/google.d.ts.map +1 -1
  12. package/dist/ai/google.js +64 -2
  13. package/dist/ai/index.js +103 -8
  14. package/dist/ai/openai.d.ts +2 -2
  15. package/dist/ai/openai.d.ts.map +1 -1
  16. package/dist/ai/openai.js +64 -2
  17. package/dist/ai/vercel-ai.d.ts +2 -2
  18. package/dist/ai/vercel-ai.d.ts.map +1 -1
  19. package/dist/ai/vercel-ai.js +64 -2
  20. package/dist/code-mode/executor.d.ts +5 -0
  21. package/dist/code-mode/executor.d.ts.map +1 -1
  22. package/dist/code-mode/executor.js +35 -0
  23. package/dist/code-mode/index.d.ts +1 -1
  24. package/dist/code-mode/index.d.ts.map +1 -1
  25. package/dist/code-mode/index.js +64 -0
  26. package/dist/code-mode/runtime-stub.d.ts +1 -1
  27. package/dist/code-mode/runtime-stub.d.ts.map +1 -1
  28. package/dist/code-mode/runtime-stub.js +2 -0
  29. package/dist/code-mode/tool-builder.d.ts +17 -0
  30. package/dist/code-mode/tool-builder.d.ts.map +1 -1
  31. package/dist/code-mode/tool-builder.js +67 -0
  32. package/dist/index.js +129 -10
  33. package/dist/oauth.js +129 -10
  34. package/dist/server.js +129 -10
  35. package/dist/src/ai/anthropic.d.ts +2 -2
  36. package/dist/src/ai/anthropic.d.ts.map +1 -1
  37. package/dist/src/ai/google.d.ts +2 -2
  38. package/dist/src/ai/google.d.ts.map +1 -1
  39. package/dist/src/ai/openai.d.ts +2 -2
  40. package/dist/src/ai/openai.d.ts.map +1 -1
  41. package/dist/src/ai/vercel-ai.d.ts +2 -2
  42. package/dist/src/ai/vercel-ai.d.ts.map +1 -1
  43. package/dist/src/code-mode/executor.d.ts +5 -0
  44. package/dist/src/code-mode/executor.d.ts.map +1 -1
  45. package/dist/src/code-mode/index.d.ts +1 -1
  46. package/dist/src/code-mode/index.d.ts.map +1 -1
  47. package/dist/src/code-mode/runtime-stub.d.ts +1 -1
  48. package/dist/src/code-mode/runtime-stub.d.ts.map +1 -1
  49. package/dist/src/code-mode/tool-builder.d.ts +17 -0
  50. package/dist/src/code-mode/tool-builder.d.ts.map +1 -1
  51. package/dist/src/server.d.ts.map +1 -1
  52. package/package.json +3 -6
@@ -8541,6 +8541,7 @@ var init_type_generator = __esm(() => {
8541
8541
  var RUNTIME_STUB_SOURCE = `// runtime.mjs — generated by integrate-sdk code mode
8542
8542
  const MCP_URL = process.env.INTEGRATE_MCP_URL;
8543
8543
  const SESSION_TOKEN = process.env.INTEGRATE_SESSION_TOKEN;
8544
+ const API_KEY = process.env.INTEGRATE_API_KEY || '';
8544
8545
  const PROVIDER_TOKENS = process.env.INTEGRATE_PROVIDER_TOKENS || '';
8545
8546
  const INTEGRATIONS_HEADER = process.env.INTEGRATE_INTEGRATIONS || '';
8546
8547
  const CONTEXT_JSON = process.env.INTEGRATE_CONTEXT || '';
@@ -8559,6 +8560,7 @@ async function callTool(toolName, args) {
8559
8560
  'x-integrate-code-mode': '1',
8560
8561
  };
8561
8562
  if (SESSION_TOKEN) headers['Authorization'] = 'Bearer ' + SESSION_TOKEN;
8563
+ if (API_KEY) headers['x-integrate-api-key'] = API_KEY;
8562
8564
  if (PROVIDER_TOKENS) headers['x-integrate-tokens'] = PROVIDER_TOKENS;
8563
8565
  if (INTEGRATIONS_HEADER) headers['x-integrations'] = INTEGRATIONS_HEADER;
8564
8566
  if (CONTEXT_JSON) headers['x-integrate-context'] = CONTEXT_JSON;
@@ -8610,11 +8612,40 @@ export { callTool };
8610
8612
  // ../code-mode/executor.ts
8611
8613
  var exports_executor = {};
8612
8614
  __export(exports_executor, {
8615
+ isSandboxAvailable: () => isSandboxAvailable,
8613
8616
  executeSandboxCode: () => executeSandboxCode,
8617
+ __setSandboxUnavailableForTests: () => __setSandboxUnavailableForTests,
8614
8618
  __setSandboxFactoryForTests: () => __setSandboxFactoryForTests
8615
8619
  });
8616
8620
  function __setSandboxFactoryForTests(factory) {
8617
8621
  sandboxFactoryOverride = factory;
8622
+ _sandboxForcedUnavailableForTests = false;
8623
+ _sandboxAvailableCache = null;
8624
+ }
8625
+ function __setSandboxUnavailableForTests(force) {
8626
+ _sandboxForcedUnavailableForTests = force;
8627
+ _sandboxAvailableCache = null;
8628
+ }
8629
+ async function isSandboxAvailable() {
8630
+ if (_sandboxAvailableCache !== null)
8631
+ return _sandboxAvailableCache;
8632
+ if (_sandboxForcedUnavailableForTests) {
8633
+ _sandboxAvailableCache = false;
8634
+ return false;
8635
+ }
8636
+ if (sandboxFactoryOverride) {
8637
+ _sandboxAvailableCache = true;
8638
+ return true;
8639
+ }
8640
+ try {
8641
+ const dynamicImport = new Function("specifier", "return import(specifier)");
8642
+ await dynamicImport("@" + "vercel/sandbox");
8643
+ _sandboxAvailableCache = true;
8644
+ return true;
8645
+ } catch {
8646
+ _sandboxAvailableCache = false;
8647
+ return false;
8648
+ }
8618
8649
  }
8619
8650
  async function loadSandboxFactory() {
8620
8651
  if (sandboxFactoryOverride)
@@ -8701,6 +8732,8 @@ async function executeSandboxCode(options) {
8701
8732
  const env = {
8702
8733
  INTEGRATE_MCP_URL: options.mcpUrl
8703
8734
  };
8735
+ if (options.apiKey)
8736
+ env.INTEGRATE_API_KEY = options.apiKey;
8704
8737
  if (options.sessionToken)
8705
8738
  env.INTEGRATE_SESSION_TOKEN = options.sessionToken;
8706
8739
  if (options.providerTokens && Object.keys(options.providerTokens).length > 0) {
@@ -8743,7 +8776,7 @@ async function executeSandboxCode(options) {
8743
8776
  }
8744
8777
  }
8745
8778
  }
8746
- var sandboxFactoryOverride = null, RESULT_SENTINEL = "__INTEGRATE_RESULT__";
8779
+ var sandboxFactoryOverride = null, _sandboxAvailableCache = null, _sandboxForcedUnavailableForTests = false, RESULT_SENTINEL = "__INTEGRATE_RESULT__";
8747
8780
  var init_executor = () => {};
8748
8781
 
8749
8782
  // ../code-mode/tool-builder.ts
@@ -8751,6 +8784,23 @@ function resolveCodeModeClientConfig(client) {
8751
8784
  const oauthConfig = client.__oauthConfig;
8752
8785
  return oauthConfig?.codeMode ?? {};
8753
8786
  }
8787
+ async function diagnoseCodeMode(client) {
8788
+ if (!await isSandboxAvailable()) {
8789
+ return { available: false, reason: "sandbox-missing" };
8790
+ }
8791
+ const serverConfig = resolveCodeModeClientConfig(client);
8792
+ const publicUrl = serverConfig.publicUrl ?? getEnv("INTEGRATE_PUBLIC_URL");
8793
+ if (!publicUrl) {
8794
+ return { available: false, reason: "no-public-url" };
8795
+ }
8796
+ return { available: true };
8797
+ }
8798
+ function warnCodeModeFallback(reason) {
8799
+ if (warnedCodeModeReasons.has(reason))
8800
+ return;
8801
+ warnedCodeModeReasons.add(reason);
8802
+ console.warn(CODE_MODE_UNAVAILABLE_MESSAGES[reason]);
8803
+ }
8754
8804
  function buildCodeModeTool(client, options) {
8755
8805
  const { tools, providerTokens, context, integrationIds } = options;
8756
8806
  const generated = generateCodeModeTypes(tools);
@@ -8763,6 +8813,7 @@ ${generated.source}
8763
8813
  \`\`\``;
8764
8814
  const execute = async ({ code }) => {
8765
8815
  const publicUrl = sandboxOverrides.publicUrl ?? serverCodeModeConfig.publicUrl ?? getEnv("INTEGRATE_PUBLIC_URL");
8816
+ const apiKey = client.__oauthConfig?.apiKey;
8766
8817
  if (!publicUrl) {
8767
8818
  return {
8768
8819
  success: false,
@@ -8777,6 +8828,7 @@ ${generated.source}
8777
8828
  return executeSandboxCode({
8778
8829
  code,
8779
8830
  mcpUrl,
8831
+ apiKey,
8780
8832
  providerTokens,
8781
8833
  context,
8782
8834
  integrationsHeader: integrationIds && integrationIds.length > 0 ? integrationIds.join(",") : undefined,
@@ -8803,7 +8855,7 @@ ${generated.source}
8803
8855
  execute
8804
8856
  };
8805
8857
  }
8806
- var CODE_MODE_TOOL_NAME = "execute_code", DEFAULT_INSTRUCTIONS;
8858
+ var CODE_MODE_TOOL_NAME = "execute_code", DEFAULT_INSTRUCTIONS, CODE_MODE_UNAVAILABLE_MESSAGES, warnedCodeModeReasons;
8807
8859
  var init_tool_builder = __esm(() => {
8808
8860
  init_type_generator();
8809
8861
  init_executor();
@@ -8828,6 +8880,11 @@ var init_tool_builder = __esm(() => {
8828
8880
  "API surface:"
8829
8881
  ].join(`
8830
8882
  `);
8883
+ CODE_MODE_UNAVAILABLE_MESSAGES = {
8884
+ "sandbox-missing": "[integrate-sdk] Code Mode unavailable (reason: sandbox-missing) — falling back to tool mode. " + "Install `@vercel/sandbox` (e.g. `bun add @vercel/sandbox`) to enable Code Mode.",
8885
+ "no-public-url": "[integrate-sdk] Code Mode unavailable (reason: no-public-url) — falling back to tool mode. " + "Set `codeMode.publicUrl` on your server config or the `INTEGRATE_PUBLIC_URL` env var."
8886
+ };
8887
+ warnedCodeModeReasons = new Set;
8831
8888
  });
8832
8889
 
8833
8890
  // ../ai/vercel-ai.ts
@@ -8854,8 +8911,19 @@ async function getVercelAITools(client, options) {
8854
8911
  await ensureClientConnected(client);
8855
8912
  const mcpTools = await client.getEnabledToolsAsync();
8856
8913
  const vercelTools = {};
8857
- const mode = options?.mode ?? "code";
8858
- if (mode === "code") {
8914
+ let effectiveMode;
8915
+ if (options?.mode !== undefined) {
8916
+ effectiveMode = options.mode;
8917
+ } else {
8918
+ const diagnosis = await diagnoseCodeMode(client);
8919
+ if (diagnosis.available) {
8920
+ effectiveMode = "code";
8921
+ } else {
8922
+ warnCodeModeFallback(diagnosis.reason);
8923
+ effectiveMode = "tools";
8924
+ }
8925
+ }
8926
+ if (effectiveMode === "code") {
8859
8927
  const codeTool = buildCodeModeTool(client, {
8860
8928
  tools: mcpTools,
8861
8929
  providerTokens,
@@ -10313,8 +10381,19 @@ async function getOpenAITools(client, options) {
10313
10381
  const finalOptions = providerTokens ? { ...options, providerTokens } : options;
10314
10382
  await ensureClientConnected(client);
10315
10383
  const mcpTools = await client.getEnabledToolsAsync();
10316
- const mode = options?.mode ?? "code";
10317
- const openaiTools = mode === "code" ? (() => {
10384
+ let effectiveMode;
10385
+ if (options?.mode !== undefined) {
10386
+ effectiveMode = options.mode;
10387
+ } else {
10388
+ const diagnosis = await diagnoseCodeMode(client);
10389
+ if (diagnosis.available) {
10390
+ effectiveMode = "code";
10391
+ } else {
10392
+ warnCodeModeFallback(diagnosis.reason);
10393
+ effectiveMode = "tools";
10394
+ }
10395
+ }
10396
+ const openaiTools = effectiveMode === "code" ? (() => {
10318
10397
  const codeTool = buildCodeModeTool(client, {
10319
10398
  tools: mcpTools,
10320
10399
  providerTokens,
@@ -10493,8 +10572,19 @@ async function getAnthropicTools(client, options) {
10493
10572
  const finalOptions = providerTokens ? { ...options, providerTokens } : options;
10494
10573
  await ensureClientConnected(client);
10495
10574
  const mcpTools = await client.getEnabledToolsAsync();
10496
- const mode = options?.mode ?? "code";
10497
- const anthropicTools = mode === "code" ? (() => {
10575
+ let effectiveMode;
10576
+ if (options?.mode !== undefined) {
10577
+ effectiveMode = options.mode;
10578
+ } else {
10579
+ const diagnosis = await diagnoseCodeMode(client);
10580
+ if (diagnosis.available) {
10581
+ effectiveMode = "code";
10582
+ } else {
10583
+ warnCodeModeFallback(diagnosis.reason);
10584
+ effectiveMode = "tools";
10585
+ }
10586
+ }
10587
+ const anthropicTools = effectiveMode === "code" ? (() => {
10498
10588
  const codeTool = buildCodeModeTool(client, {
10499
10589
  tools: mcpTools,
10500
10590
  providerTokens,
@@ -10682,9 +10772,20 @@ async function getGoogleTools(client, options) {
10682
10772
  const finalOptions = providerTokens ? { ...options, providerTokens } : options;
10683
10773
  await ensureClientConnected(client);
10684
10774
  const mcpTools = await client.getEnabledToolsAsync();
10685
- const mode = options?.mode ?? "code";
10775
+ let effectiveMode;
10776
+ if (options?.mode !== undefined) {
10777
+ effectiveMode = options.mode;
10778
+ } else {
10779
+ const diagnosis = await diagnoseCodeMode(client);
10780
+ if (diagnosis.available) {
10781
+ effectiveMode = "code";
10782
+ } else {
10783
+ warnCodeModeFallback(diagnosis.reason);
10784
+ effectiveMode = "tools";
10785
+ }
10786
+ }
10686
10787
  let googleTools;
10687
- if (mode === "code") {
10788
+ if (effectiveMode === "code") {
10688
10789
  const TypeEnum = await getGoogleType();
10689
10790
  const codeTool = buildCodeModeTool(client, {
10690
10791
  tools: mcpTools,
@@ -11231,6 +11332,24 @@ function createMCPServer(config) {
11231
11332
  } catch {}
11232
11333
  }
11233
11334
  }
11335
+ if (!authHeader && config.getProviderToken) {
11336
+ const codeModeHeader = webRequest.headers.get("x-integrate-code-mode");
11337
+ const contextHeader = webRequest.headers.get("x-integrate-context");
11338
+ const callbackApiKey = webRequest.headers.get("x-integrate-api-key");
11339
+ const toolName = typeof body?.name === "string" ? body.name : "";
11340
+ if (codeModeHeader === "1" && contextHeader && toolName && config.apiKey && callbackApiKey === config.apiKey) {
11341
+ try {
11342
+ const context2 = JSON.parse(contextHeader);
11343
+ const provider = toolName.split("_")[0];
11344
+ if (provider) {
11345
+ const tokenData = await config.getProviderToken(provider, undefined, context2);
11346
+ if (tokenData?.accessToken) {
11347
+ authHeader = `Bearer ${tokenData.accessToken}`;
11348
+ }
11349
+ }
11350
+ } catch {}
11351
+ }
11352
+ }
11234
11353
  const { OAuthHandler } = await Promise.resolve().then(() => (init_base_handler(), exports_base_handler));
11235
11354
  const oauthHandler = new OAuthHandler({
11236
11355
  providers,
@@ -8541,6 +8541,7 @@ var init_type_generator = __esm(() => {
8541
8541
  var RUNTIME_STUB_SOURCE = `// runtime.mjs — generated by integrate-sdk code mode
8542
8542
  const MCP_URL = process.env.INTEGRATE_MCP_URL;
8543
8543
  const SESSION_TOKEN = process.env.INTEGRATE_SESSION_TOKEN;
8544
+ const API_KEY = process.env.INTEGRATE_API_KEY || '';
8544
8545
  const PROVIDER_TOKENS = process.env.INTEGRATE_PROVIDER_TOKENS || '';
8545
8546
  const INTEGRATIONS_HEADER = process.env.INTEGRATE_INTEGRATIONS || '';
8546
8547
  const CONTEXT_JSON = process.env.INTEGRATE_CONTEXT || '';
@@ -8559,6 +8560,7 @@ async function callTool(toolName, args) {
8559
8560
  'x-integrate-code-mode': '1',
8560
8561
  };
8561
8562
  if (SESSION_TOKEN) headers['Authorization'] = 'Bearer ' + SESSION_TOKEN;
8563
+ if (API_KEY) headers['x-integrate-api-key'] = API_KEY;
8562
8564
  if (PROVIDER_TOKENS) headers['x-integrate-tokens'] = PROVIDER_TOKENS;
8563
8565
  if (INTEGRATIONS_HEADER) headers['x-integrations'] = INTEGRATIONS_HEADER;
8564
8566
  if (CONTEXT_JSON) headers['x-integrate-context'] = CONTEXT_JSON;
@@ -8610,11 +8612,40 @@ export { callTool };
8610
8612
  // ../code-mode/executor.ts
8611
8613
  var exports_executor = {};
8612
8614
  __export(exports_executor, {
8615
+ isSandboxAvailable: () => isSandboxAvailable,
8613
8616
  executeSandboxCode: () => executeSandboxCode,
8617
+ __setSandboxUnavailableForTests: () => __setSandboxUnavailableForTests,
8614
8618
  __setSandboxFactoryForTests: () => __setSandboxFactoryForTests
8615
8619
  });
8616
8620
  function __setSandboxFactoryForTests(factory) {
8617
8621
  sandboxFactoryOverride = factory;
8622
+ _sandboxForcedUnavailableForTests = false;
8623
+ _sandboxAvailableCache = null;
8624
+ }
8625
+ function __setSandboxUnavailableForTests(force) {
8626
+ _sandboxForcedUnavailableForTests = force;
8627
+ _sandboxAvailableCache = null;
8628
+ }
8629
+ async function isSandboxAvailable() {
8630
+ if (_sandboxAvailableCache !== null)
8631
+ return _sandboxAvailableCache;
8632
+ if (_sandboxForcedUnavailableForTests) {
8633
+ _sandboxAvailableCache = false;
8634
+ return false;
8635
+ }
8636
+ if (sandboxFactoryOverride) {
8637
+ _sandboxAvailableCache = true;
8638
+ return true;
8639
+ }
8640
+ try {
8641
+ const dynamicImport = new Function("specifier", "return import(specifier)");
8642
+ await dynamicImport("@" + "vercel/sandbox");
8643
+ _sandboxAvailableCache = true;
8644
+ return true;
8645
+ } catch {
8646
+ _sandboxAvailableCache = false;
8647
+ return false;
8648
+ }
8618
8649
  }
8619
8650
  async function loadSandboxFactory() {
8620
8651
  if (sandboxFactoryOverride)
@@ -8701,6 +8732,8 @@ async function executeSandboxCode(options) {
8701
8732
  const env = {
8702
8733
  INTEGRATE_MCP_URL: options.mcpUrl
8703
8734
  };
8735
+ if (options.apiKey)
8736
+ env.INTEGRATE_API_KEY = options.apiKey;
8704
8737
  if (options.sessionToken)
8705
8738
  env.INTEGRATE_SESSION_TOKEN = options.sessionToken;
8706
8739
  if (options.providerTokens && Object.keys(options.providerTokens).length > 0) {
@@ -8743,7 +8776,7 @@ async function executeSandboxCode(options) {
8743
8776
  }
8744
8777
  }
8745
8778
  }
8746
- var sandboxFactoryOverride = null, RESULT_SENTINEL = "__INTEGRATE_RESULT__";
8779
+ var sandboxFactoryOverride = null, _sandboxAvailableCache = null, _sandboxForcedUnavailableForTests = false, RESULT_SENTINEL = "__INTEGRATE_RESULT__";
8747
8780
  var init_executor = () => {};
8748
8781
 
8749
8782
  // ../code-mode/tool-builder.ts
@@ -8751,6 +8784,23 @@ function resolveCodeModeClientConfig(client) {
8751
8784
  const oauthConfig = client.__oauthConfig;
8752
8785
  return oauthConfig?.codeMode ?? {};
8753
8786
  }
8787
+ async function diagnoseCodeMode(client) {
8788
+ if (!await isSandboxAvailable()) {
8789
+ return { available: false, reason: "sandbox-missing" };
8790
+ }
8791
+ const serverConfig = resolveCodeModeClientConfig(client);
8792
+ const publicUrl = serverConfig.publicUrl ?? getEnv("INTEGRATE_PUBLIC_URL");
8793
+ if (!publicUrl) {
8794
+ return { available: false, reason: "no-public-url" };
8795
+ }
8796
+ return { available: true };
8797
+ }
8798
+ function warnCodeModeFallback(reason) {
8799
+ if (warnedCodeModeReasons.has(reason))
8800
+ return;
8801
+ warnedCodeModeReasons.add(reason);
8802
+ console.warn(CODE_MODE_UNAVAILABLE_MESSAGES[reason]);
8803
+ }
8754
8804
  function buildCodeModeTool(client, options) {
8755
8805
  const { tools, providerTokens, context, integrationIds } = options;
8756
8806
  const generated = generateCodeModeTypes(tools);
@@ -8763,6 +8813,7 @@ ${generated.source}
8763
8813
  \`\`\``;
8764
8814
  const execute = async ({ code }) => {
8765
8815
  const publicUrl = sandboxOverrides.publicUrl ?? serverCodeModeConfig.publicUrl ?? getEnv("INTEGRATE_PUBLIC_URL");
8816
+ const apiKey = client.__oauthConfig?.apiKey;
8766
8817
  if (!publicUrl) {
8767
8818
  return {
8768
8819
  success: false,
@@ -8777,6 +8828,7 @@ ${generated.source}
8777
8828
  return executeSandboxCode({
8778
8829
  code,
8779
8830
  mcpUrl,
8831
+ apiKey,
8780
8832
  providerTokens,
8781
8833
  context,
8782
8834
  integrationsHeader: integrationIds && integrationIds.length > 0 ? integrationIds.join(",") : undefined,
@@ -8803,7 +8855,7 @@ ${generated.source}
8803
8855
  execute
8804
8856
  };
8805
8857
  }
8806
- var CODE_MODE_TOOL_NAME = "execute_code", DEFAULT_INSTRUCTIONS;
8858
+ var CODE_MODE_TOOL_NAME = "execute_code", DEFAULT_INSTRUCTIONS, CODE_MODE_UNAVAILABLE_MESSAGES, warnedCodeModeReasons;
8807
8859
  var init_tool_builder = __esm(() => {
8808
8860
  init_type_generator();
8809
8861
  init_executor();
@@ -8828,6 +8880,11 @@ var init_tool_builder = __esm(() => {
8828
8880
  "API surface:"
8829
8881
  ].join(`
8830
8882
  `);
8883
+ CODE_MODE_UNAVAILABLE_MESSAGES = {
8884
+ "sandbox-missing": "[integrate-sdk] Code Mode unavailable (reason: sandbox-missing) — falling back to tool mode. " + "Install `@vercel/sandbox` (e.g. `bun add @vercel/sandbox`) to enable Code Mode.",
8885
+ "no-public-url": "[integrate-sdk] Code Mode unavailable (reason: no-public-url) — falling back to tool mode. " + "Set `codeMode.publicUrl` on your server config or the `INTEGRATE_PUBLIC_URL` env var."
8886
+ };
8887
+ warnedCodeModeReasons = new Set;
8831
8888
  });
8832
8889
 
8833
8890
  // ../ai/vercel-ai.ts
@@ -8854,8 +8911,19 @@ async function getVercelAITools(client, options) {
8854
8911
  await ensureClientConnected(client);
8855
8912
  const mcpTools = await client.getEnabledToolsAsync();
8856
8913
  const vercelTools = {};
8857
- const mode = options?.mode ?? "code";
8858
- if (mode === "code") {
8914
+ let effectiveMode;
8915
+ if (options?.mode !== undefined) {
8916
+ effectiveMode = options.mode;
8917
+ } else {
8918
+ const diagnosis = await diagnoseCodeMode(client);
8919
+ if (diagnosis.available) {
8920
+ effectiveMode = "code";
8921
+ } else {
8922
+ warnCodeModeFallback(diagnosis.reason);
8923
+ effectiveMode = "tools";
8924
+ }
8925
+ }
8926
+ if (effectiveMode === "code") {
8859
8927
  const codeTool = buildCodeModeTool(client, {
8860
8928
  tools: mcpTools,
8861
8929
  providerTokens,
@@ -10313,8 +10381,19 @@ async function getOpenAITools(client, options) {
10313
10381
  const finalOptions = providerTokens ? { ...options, providerTokens } : options;
10314
10382
  await ensureClientConnected(client);
10315
10383
  const mcpTools = await client.getEnabledToolsAsync();
10316
- const mode = options?.mode ?? "code";
10317
- const openaiTools = mode === "code" ? (() => {
10384
+ let effectiveMode;
10385
+ if (options?.mode !== undefined) {
10386
+ effectiveMode = options.mode;
10387
+ } else {
10388
+ const diagnosis = await diagnoseCodeMode(client);
10389
+ if (diagnosis.available) {
10390
+ effectiveMode = "code";
10391
+ } else {
10392
+ warnCodeModeFallback(diagnosis.reason);
10393
+ effectiveMode = "tools";
10394
+ }
10395
+ }
10396
+ const openaiTools = effectiveMode === "code" ? (() => {
10318
10397
  const codeTool = buildCodeModeTool(client, {
10319
10398
  tools: mcpTools,
10320
10399
  providerTokens,
@@ -10493,8 +10572,19 @@ async function getAnthropicTools(client, options) {
10493
10572
  const finalOptions = providerTokens ? { ...options, providerTokens } : options;
10494
10573
  await ensureClientConnected(client);
10495
10574
  const mcpTools = await client.getEnabledToolsAsync();
10496
- const mode = options?.mode ?? "code";
10497
- const anthropicTools = mode === "code" ? (() => {
10575
+ let effectiveMode;
10576
+ if (options?.mode !== undefined) {
10577
+ effectiveMode = options.mode;
10578
+ } else {
10579
+ const diagnosis = await diagnoseCodeMode(client);
10580
+ if (diagnosis.available) {
10581
+ effectiveMode = "code";
10582
+ } else {
10583
+ warnCodeModeFallback(diagnosis.reason);
10584
+ effectiveMode = "tools";
10585
+ }
10586
+ }
10587
+ const anthropicTools = effectiveMode === "code" ? (() => {
10498
10588
  const codeTool = buildCodeModeTool(client, {
10499
10589
  tools: mcpTools,
10500
10590
  providerTokens,
@@ -10682,9 +10772,20 @@ async function getGoogleTools(client, options) {
10682
10772
  const finalOptions = providerTokens ? { ...options, providerTokens } : options;
10683
10773
  await ensureClientConnected(client);
10684
10774
  const mcpTools = await client.getEnabledToolsAsync();
10685
- const mode = options?.mode ?? "code";
10775
+ let effectiveMode;
10776
+ if (options?.mode !== undefined) {
10777
+ effectiveMode = options.mode;
10778
+ } else {
10779
+ const diagnosis = await diagnoseCodeMode(client);
10780
+ if (diagnosis.available) {
10781
+ effectiveMode = "code";
10782
+ } else {
10783
+ warnCodeModeFallback(diagnosis.reason);
10784
+ effectiveMode = "tools";
10785
+ }
10786
+ }
10686
10787
  let googleTools;
10687
- if (mode === "code") {
10788
+ if (effectiveMode === "code") {
10688
10789
  const TypeEnum = await getGoogleType();
10689
10790
  const codeTool = buildCodeModeTool(client, {
10690
10791
  tools: mcpTools,
@@ -11231,6 +11332,24 @@ function createMCPServer(config) {
11231
11332
  } catch {}
11232
11333
  }
11233
11334
  }
11335
+ if (!authHeader && config.getProviderToken) {
11336
+ const codeModeHeader = webRequest.headers.get("x-integrate-code-mode");
11337
+ const contextHeader = webRequest.headers.get("x-integrate-context");
11338
+ const callbackApiKey = webRequest.headers.get("x-integrate-api-key");
11339
+ const toolName = typeof body?.name === "string" ? body.name : "";
11340
+ if (codeModeHeader === "1" && contextHeader && toolName && config.apiKey && callbackApiKey === config.apiKey) {
11341
+ try {
11342
+ const context2 = JSON.parse(contextHeader);
11343
+ const provider = toolName.split("_")[0];
11344
+ if (provider) {
11345
+ const tokenData = await config.getProviderToken(provider, undefined, context2);
11346
+ if (tokenData?.accessToken) {
11347
+ authHeader = `Bearer ${tokenData.accessToken}`;
11348
+ }
11349
+ }
11350
+ } catch {}
11351
+ }
11352
+ }
11234
11353
  const { OAuthHandler } = await Promise.resolve().then(() => (init_base_handler(), exports_base_handler));
11235
11354
  const oauthHandler = new OAuthHandler({
11236
11355
  providers,