feat(codemode): add TanStack AI integration#1149
Conversation
Add a new TanStack AI entry point and related refactorings. - New entry: @cloudflare/codemode/tanstack-ai (implements createCodeTool, tanstackTools, generateTypes, resolveProvider) so codemode can be used with @tanstack/ai's chat(). - Add framework-agnostic resolve.ts and shared.ts to extract provider resolution and shared constants/helpers (so the main entry no longer pulls in AI SDK runtime deps). - Implement tanstack-ai.ts with type generation, tool wrapping, and ServerTool creation plus comprehensive tests for tanstack integration, resolve, and shared helpers. - Update package.json exports/peerDeps and build script to include the new entrypoint and optional @tanstack/ai peer dep. - Update README with usage docs and examples for TanStack AI. This change adds the TanStack AI integration and internal cleanup to avoid pulling AI SDK dependencies into the core entry point.
The shared.ts refactor moved these types to an internal chunk, causing downstream packages (e.g. @cloudflare/think) to fail with TS2742 when inferring return types of createCodeTool. Made-with: Cursor
🦋 Changeset detectedLatest commit: ad0c214 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
| provider.types ?? generateTypesFromRecord(filtered, providerName); | ||
| typeBlocks.push(types); | ||
|
|
||
| resolvedProviders.push({ name: providerName, fns: extractFns(filtered) }); |
There was a problem hiding this comment.
🔴 positionalArgs dropped when building ResolvedProviders in createCodeTool
The new createCodeTool in tanstack-ai.ts builds ResolvedProvider objects at line 247 without forwarding the positionalArgs flag from the source ToolProvider. The DynamicWorkerExecutor relies on positionalArgs to choose between a single-arg proxy (async (args) =>, executor.ts:271) and a rest-args proxy (async (...args) =>, executor.ts:260). Without it, providers like stateTools from @cloudflare/shell/workers (which sets positionalArgs: true at packages/shell/src/workers.ts:34) will use the wrong proxy: multi-argument calls like state.writeFile("/path", "content") will silently lose all arguments after the first, because the single-arg proxy only captures one parameter.
Pre-existing identical bug in AI SDK version
The same bug exists at packages/codemode/src/tool.ts:105 (resolvedProviders.push({ name, fns: extractFns(filtered) })) — this is pre-existing but actively triggered by packages/think/src/tools/execute.ts:163-166 which passes stateToolsFromBackend() through createCodeTool. Both resolveProvider functions (tool.ts:82 and resolve.ts:63) correctly forward positionalArgs, but neither createCodeTool implementation uses resolveProvider internally.
| resolvedProviders.push({ name: providerName, fns: extractFns(filtered) }); | |
| const resolved: ResolvedProvider = { name: providerName, fns: extractFns(filtered) }; | |
| if (provider.positionalArgs) resolved.positionalArgs = true; | |
| resolvedProviders.push(resolved); |
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
@cloudflare/codemode/tanstack-ai— a new entry point for using codemode with TanStack AI'schat()instead of the Vercel AI SDK'sstreamText()resolveProviderintoresolve.tsso the main entry (@cloudflare/codemode) no longer pulls in theaipackage at runtimeCodeInput/CodeOutputtypes from@cloudflare/codemode/aito fix downstream TS2742 errors (e.g. in@cloudflare/think)New exports from
@cloudflare/codemode/tanstack-aicreateCodeTool(options)ServerToolviatoolDefinition().server()tanstackTools(tools, name?)TanStackTool[]into aToolProviderwith pre-generated typesgenerateTypes(tools, namespace?)resolveProvider(provider)Internal refactoring
src/resolve.ts— framework-agnosticfilterTools,extractFns,resolveProvider(noaidep)src/shared.ts—DEFAULT_DESCRIPTION,CreateCodeToolOptions,CodeInput,CodeOutput,normalizeProviderssrc/tool.ts— now imports base helpers fromresolve.ts, layers AI SDK schema validation on topTest plan
npm run checkpasses (sherif, export checks, oxfmt, oxlint, typecheck — all 65 projects)@cloudflare/thinkbuilds successfully (verified TS2742 fix)resolve.test.ts(13 tests),tanstack-ai.test.ts(31 tests),shared.test.ts(4 tests)Made with Cursor