Skip to content

fix: lazy-load ai package to honor optional peer dependency#337

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-top-level-import-ai-dependency
Closed

fix: lazy-load ai package to honor optional peer dependency#337
Copilot wants to merge 2 commits intomainfrom
copilot/fix-top-level-import-ai-dependency

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 19, 2026

ai was declared as an optional peer dependency but a static top-level import in tool.ts caused an immediate crash on module load when ai wasn't installed — before any SDK methods were called.

Changes

  • src/tool.ts: Convert import { jsonSchema } from 'ai' to import type (erased at compile time, no runtime cost)
  • toClaudeAgentSdkTool(): Replace direct jsonSchema() call with tryImport('ai', ...), consistent with the existing pattern in toAISDK()
// Before — crashes on load if `ai` not installed
import { type JSONSchema7 as AISDKJSONSchema, jsonSchema } from 'ai';
// ...
const inputSchema = jsonSchema(this.toJsonSchema());

// After — only loads `ai` when method is actually called
import type { JSONSchema7 as AISDKJSONSchema } from 'ai';
// ...
const ai = await tryImport<typeof import('ai')>('ai', `npm install ai (requires ${peerDependencies.ai})`);
const inputSchema = ai.jsonSchema(this.toJsonSchema());
Original prompt

This section details on the original issue you should resolve

<issue_title>ai peer dependency is not truly optional — top-level static import in tool.mjs crashes on load</issue_title>
<issue_description>@stackone/ai declares ai as an optional peer dependency in peerDependenciesMeta, but dist/src/tool.mjs has a top-level static import:

  import { jsonSchema } from "ai";

This causes an immediate crash when the module is loaded without ai installed, even if you never call any method that uses it.

Reproduction:

  mkdir repro && cd repro
  npm init -y
  npm install @stackone/ai zod
  # **Note: do NOT install `ai`**
  node -e "async function main() { const { StackOneToolSet } = await import('@stackone/ai'); console.log('ok'); } main()"

Expected: The import succeeds since ai is marked as optional and I'm not using any AI SDK adapter methods.

Actual:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ai' imported from
.../node_modules/@stackone/ai/dist/src/tool.mjs

Analysis:

The other optional peer deps (@anthropic-ai/sdk, @anthropic-ai/claude-agent-sdk, openai) are correctly loaded lazily via tryImport() in dist/src/utils/try-import.mjs. But
jsonSchema from ai is imported statically at the top of tool.mjs and used in BaseTool.toClaudeAgentSdkTool().

Suggested fix:

Use tryImport for ai in toClaudeAgentSdkTool(), consistent with how the other optional peers are handled:

  async toClaudeAgentSdkTool() {
    const { jsonSchema } = await tryImport("ai", `npm install ai (requires ${peerDependencies.ai})`);
    const inputSchema = jsonSchema(this.toJsonSchema());
    // ...
  }

Versions:

  • @stackone/ai: 2.4.0
  • Node.js: v22.x</issue_description>

Comments on the Issue (you are @copilot in this section)

@glebedel Automated benchmark test comment. Please ignore.

📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.


Summary by cubic

Fixes crashes when importing @stackone/ai without ai installed by removing the top‑level runtime import and loading ai only inside toClaudeAgentSdkTool. You can now import the package without ai unless you use the Claude Agent SDK adapter.

  • Bug Fixes
    • Switched import { jsonSchema } from 'ai' to import type only.
    • Load ai via tryImport inside BaseTool.toClaudeAgentSdkTool().
    • Added a clear install hint with the required version when ai is needed.

Written for commit f51049c. Summary will update on new commits.

…ort crash

Co-authored-by: glebedel <10488548+glebedel@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ai peer dependency issue causing crashes on load fix: lazy-load ai package to honor optional peer dependency Mar 19, 2026
Copilot AI requested a review from glebedel March 19, 2026 20:10
@glebedel glebedel closed this Mar 19, 2026
@glebedel glebedel deleted the copilot/fix-top-level-import-ai-dependency branch March 19, 2026 21:19
@glebedel glebedel restored the copilot/fix-top-level-import-ai-dependency branch March 26, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ai peer dependency is not truly optional — top-level static import in tool.mjs crashes on load

2 participants