diff --git a/apps/web/src/lib/ai-agent/tool-executor-agent.ts b/apps/web/src/lib/ai-agent/tool-executor-agent.ts index a08ffaa..83cad96 100644 --- a/apps/web/src/lib/ai-agent/tool-executor-agent.ts +++ b/apps/web/src/lib/ai-agent/tool-executor-agent.ts @@ -159,6 +159,15 @@ export function calculateTokenBreakdown( }; } +/** + * Sanitize npm package name to valid OpenAI tool name + * OpenAI tool names must match: ^[a-zA-Z0-9_-]+ + * Converts: @tpmjs/createblogpost -> tpmjs-createblogpost + */ +function sanitizeToolName(npmPackageName: string): string { + return npmPackageName.replace(/[@/]/g, '-').replace(/^-+/, ''); +} + /** * Execute tool with AI agent and streaming */ @@ -169,6 +178,7 @@ export async function executeToolWithAgent( onTokenUpdate?: (tokens: Partial) => void ) { const toolDef = createToolDefinition(tool); + const sanitizedToolName = sanitizeToolName(tool.npmPackageName); const messages: CoreMessage[] = [ { role: 'user', @@ -183,7 +193,7 @@ export async function executeToolWithAgent( model: openai('gpt-4-turbo'), messages, tools: { - [tool.npmPackageName]: toolDef, + [sanitizedToolName]: toolDef, }, // biome-ignore lint/suspicious/noExplicitAny: AI SDK v5 chunk type compatibility onChunk: ({ chunk }: { chunk: any }) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9f0f744..d0e13d1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -101,9 +101,6 @@ importers: remark-gfm: specifier: ^4.0.1 version: 4.0.1 - tiktoken: - specifier: ^1.0.22 - version: 1.0.22 zod: specifier: ^3.25.76 version: 3.25.76 @@ -4627,9 +4624,6 @@ packages: thenify@3.3.1: resolution: {integrity: sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==} - tiktoken@1.0.22: - resolution: {integrity: sha512-PKvy1rVF1RibfF3JlXBSP0Jrcw2uq3yXdgcEXtKTYn3QJ/cBRBHDnrJ5jHky+MENZ6DIPwNUGWpkVx+7joCpNA==} - tiny-invariant@1.3.3: resolution: {integrity: sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==} @@ -9920,8 +9914,6 @@ snapshots: dependencies: any-promise: 1.3.0 - tiktoken@1.0.22: {} - tiny-invariant@1.3.3: {} tinybench@2.9.0: {}