-
-
Notifications
You must be signed in to change notification settings - Fork 87
feat: add Claude Agent SDK adapter for TanStack AI #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johann-taberlet
wants to merge
6
commits into
TanStack:main
Choose a base branch
from
johann-taberlet:001-claude-agent-sdk-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
96335e1
feat: add Claude Agent SDK adapter for TanStack AI
johann-taberlet 398d408
fix: address linting and test issues for Claude Agent SDK adapter
johann-taberlet ba501a5
fix: address CodeRabbit review comments
johann-taberlet 08fc91f
refactor: address CodeRabbit nitpick suggestions
johann-taberlet b9d6e72
fix: correct model metadata to match Claude 4.5 specs
johann-taberlet 26fbee7
fix: add prompt validation and fix tool call index calculation
johann-taberlet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| "@tanstack/ai-claude-agent-sdk": minor | ||
| --- | ||
|
|
||
| feat: add Claude Agent SDK adapter for TanStack AI | ||
|
|
||
| This adds a new adapter package that integrates the Claude Agent SDK with TanStack AI, enabling: | ||
|
|
||
| - Streaming chat completions via Claude Agent SDK's agentic runtime | ||
| - Extended thinking support with configurable token budgets | ||
| - Tool integration (both custom TanStack AI tools and built-in Claude Code tools) | ||
| - Multimodal content support (images, documents) | ||
| - Full type safety with per-model provider options | ||
| - Automatic authentication via Claude Max subscription or API key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| { | ||
| "name": "@tanstack/ai-claude-agent-sdk", | ||
| "version": "0.0.1", | ||
| "description": "Claude Agent SDK adapter for TanStack AI - Use Claude Max subscription for AI development", | ||
| "author": "", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/TanStack/ai.git", | ||
| "directory": "packages/typescript/ai-claude-agent-sdk" | ||
| }, | ||
| "keywords": [ | ||
| "ai", | ||
| "anthropic", | ||
| "claude", | ||
| "claude-agent-sdk", | ||
| "claude-max", | ||
| "tanstack", | ||
| "adapter" | ||
| ], | ||
| "type": "module", | ||
| "module": "./dist/esm/packages/typescript/ai-claude-agent-sdk/src/index.js", | ||
| "types": "./dist/esm/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/esm/index.d.ts", | ||
| "import": "./dist/esm/packages/typescript/ai-claude-agent-sdk/src/index.js" | ||
| }, | ||
| "./tools": { | ||
| "types": "./dist/esm/tools/index.d.ts", | ||
| "import": "./dist/esm/packages/typescript/ai-claude-agent-sdk/src/tools/index.js" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "src" | ||
| ], | ||
| "scripts": { | ||
| "build": "vite build", | ||
| "clean": "premove ./build ./dist", | ||
| "lint:fix": "eslint ./src --fix", | ||
| "test:build": "publint --strict", | ||
| "test:eslint": "eslint ./src", | ||
| "test:lib": "vitest", | ||
| "test:lib:dev": "pnpm test:lib --watch", | ||
| "test:types": "tsc" | ||
| }, | ||
| "dependencies": { | ||
| "@anthropic-ai/claude-agent-sdk": "^0.1.69", | ||
| "@tanstack/ai": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@vitest/coverage-v8": "4.0.14", | ||
| "zod": "^4.1.13" | ||
| }, | ||
| "peerDependencies": { | ||
| "@tanstack/ai": "workspace:*" | ||
| } | ||
| } | ||
271 changes: 271 additions & 0 deletions
271
packages/typescript/ai-claude-agent-sdk/src/builtin-tools.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| /** | ||
| * Marker to identify built-in Claude Code tools. | ||
| * These tools are executed by Claude Code directly, not client-side. | ||
| */ | ||
| export const BUILTIN_TOOL_MARKER = Symbol.for('claude-agent-sdk-builtin-tool') | ||
|
|
||
| /** | ||
| * Built-in tool definition type. | ||
| */ | ||
| export interface BuiltinToolDefinition { | ||
| readonly [BUILTIN_TOOL_MARKER]: true | ||
| readonly name: string | ||
| readonly description: string | ||
| readonly inputSchema: z.ZodType | ||
| } | ||
|
|
||
| /** | ||
| * Helper to create a built-in tool definition. | ||
| */ | ||
| function createBuiltinTool<T extends z.ZodType>( | ||
| name: string, | ||
| description: string, | ||
| inputSchema: T, | ||
| ): BuiltinToolDefinition { | ||
| return { | ||
| [BUILTIN_TOOL_MARKER]: true, | ||
| name, | ||
| description, | ||
| inputSchema, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if a tool is a built-in Claude Code tool. | ||
| */ | ||
| export function isBuiltinTool(tool: unknown): tool is BuiltinToolDefinition { | ||
| if (typeof tool !== 'object' || tool === null) { | ||
| return false | ||
| } | ||
| return ( | ||
| BUILTIN_TOOL_MARKER in tool && | ||
| Boolean((tool as Record<symbol, unknown>)[BUILTIN_TOOL_MARKER]) | ||
| ) | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Built-in Claude Code Tools | ||
| // ============================================================================ | ||
|
|
||
| /** | ||
| * Read tool - Read files from the filesystem. | ||
| */ | ||
| export const Read = createBuiltinTool( | ||
| 'Read', | ||
| 'Read a file from the filesystem. Can read text files, images, PDFs, and Jupyter notebooks.', | ||
| z.object({ | ||
| file_path: z.string().describe('The absolute path to the file to read'), | ||
| offset: z.number().optional().describe('Line number to start reading from'), | ||
| limit: z.number().optional().describe('Number of lines to read'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * Write tool - Write files to the filesystem. | ||
| */ | ||
| export const Write = createBuiltinTool( | ||
| 'Write', | ||
| 'Write content to a file. Overwrites existing files.', | ||
| z.object({ | ||
| file_path: z.string().describe('The absolute path to the file to write'), | ||
| content: z.string().describe('The content to write to the file'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * Edit tool - Edit files using string replacement. | ||
| */ | ||
| export const Edit = createBuiltinTool( | ||
| 'Edit', | ||
| 'Edit a file by replacing text. The old_string must be unique in the file.', | ||
| z.object({ | ||
| file_path: z.string().describe('The absolute path to the file to modify'), | ||
| old_string: z.string().describe('The text to replace'), | ||
| new_string: z.string().describe('The replacement text'), | ||
| replace_all: z.boolean().optional().describe('Replace all occurrences'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * Bash tool - Execute shell commands. | ||
| */ | ||
| export const Bash = createBuiltinTool( | ||
| 'Bash', | ||
| 'Execute a bash command in a persistent shell session.', | ||
| z.object({ | ||
| command: z.string().describe('The command to execute'), | ||
| description: z.string().optional().describe('Description of what this command does'), | ||
| timeout: z.number().optional().describe('Optional timeout in milliseconds'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * Glob tool - Find files by pattern. | ||
| */ | ||
| export const Glob = createBuiltinTool( | ||
| 'Glob', | ||
| 'Find files matching a glob pattern.', | ||
| z.object({ | ||
| pattern: z.string().describe('The glob pattern to match files against'), | ||
| path: z.string().optional().describe('The directory to search in'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * Grep tool - Search file contents. | ||
| */ | ||
| export const Grep = createBuiltinTool( | ||
| 'Grep', | ||
| 'Search for text patterns in files using ripgrep.', | ||
| z.object({ | ||
| pattern: z.string().describe('The regex pattern to search for'), | ||
| path: z.string().optional().describe('File or directory to search in'), | ||
| glob: z.string().optional().describe('Glob pattern to filter files'), | ||
| type: z.string().optional().describe('File type to search (js, py, etc.)'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * WebFetch tool - Fetch and process web content. | ||
| */ | ||
| export const WebFetch = createBuiltinTool( | ||
| 'WebFetch', | ||
| 'Fetch content from a URL and process it.', | ||
| z.object({ | ||
| url: z.string().describe('The URL to fetch content from'), | ||
| prompt: z.string().describe('Prompt to run on the fetched content'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * WebSearch tool - Search the web. | ||
| */ | ||
| export const WebSearch = createBuiltinTool( | ||
| 'WebSearch', | ||
| 'Search the web for information.', | ||
| z.object({ | ||
| query: z.string().describe('The search query'), | ||
| allowed_domains: z.array(z.string()).optional().describe('Only include results from these domains'), | ||
| blocked_domains: z.array(z.string()).optional().describe('Exclude results from these domains'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * Task tool - Launch subagents for complex tasks. | ||
| */ | ||
| export const Task = createBuiltinTool( | ||
| 'Task', | ||
| 'Launch a subagent to handle complex, multi-step tasks.', | ||
| z.object({ | ||
| description: z.string().describe('Short description of the task'), | ||
| prompt: z.string().describe('The task for the agent to perform'), | ||
| subagent_type: z.string().describe('The type of agent to use'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * TodoWrite tool - Manage task lists. | ||
| */ | ||
| export const TodoWrite = createBuiltinTool( | ||
| 'TodoWrite', | ||
| 'Create and manage a structured task list.', | ||
| z.object({ | ||
| todos: z.array( | ||
| z.object({ | ||
| content: z.string().describe('The task description'), | ||
| status: z.enum(['pending', 'in_progress', 'completed']).describe('Task status'), | ||
| activeForm: z.string().describe('Present continuous form of the task'), | ||
| }), | ||
| ).describe('The todo list'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * NotebookEdit tool - Edit Jupyter notebooks. | ||
| */ | ||
| export const NotebookEdit = createBuiltinTool( | ||
| 'NotebookEdit', | ||
| 'Edit cells in a Jupyter notebook.', | ||
| z.object({ | ||
| notebook_path: z.string().describe('Absolute path to the notebook'), | ||
| new_source: z.string().describe('New source for the cell'), | ||
| cell_id: z.string().optional().describe('ID of the cell to edit'), | ||
| cell_type: z.enum(['code', 'markdown']).optional().describe('Type of cell'), | ||
| edit_mode: z.enum(['replace', 'insert', 'delete']).optional().describe('Edit mode'), | ||
| }), | ||
| ) | ||
|
|
||
| /** | ||
| * AskUserQuestion tool - Ask the user a question. | ||
| */ | ||
| export const AskUserQuestion = createBuiltinTool( | ||
| 'AskUserQuestion', | ||
| 'Ask the user a question with multiple choice options.', | ||
| z.object({ | ||
| questions: z.array( | ||
| z.object({ | ||
| question: z.string().describe('The question to ask'), | ||
| header: z.string().describe('Short label for the question'), | ||
| options: z.array( | ||
| z.object({ | ||
| label: z.string().describe('Display text for this option'), | ||
| description: z.string().describe('Explanation of this option'), | ||
| }), | ||
| ).describe('Available choices'), | ||
| multiSelect: z.boolean().describe('Allow multiple selections'), | ||
| }), | ||
| ).describe('Questions to ask'), | ||
| }), | ||
| ) | ||
|
|
||
| // ============================================================================ | ||
| // Exports | ||
| // ============================================================================ | ||
|
|
||
| /** | ||
| * All built-in Claude Code tools. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import { claudeAgentSdk, builtinTools } from '@tanstack/ai-claude-agent-sdk' | ||
| * | ||
| * const adapter = claudeAgentSdk() | ||
| * | ||
| * const result = await chat({ | ||
| * adapter, | ||
| * model: 'sonnet', | ||
| * messages: [{ role: 'user', content: 'List files in current directory' }], | ||
| * tools: [ | ||
| * builtinTools.Bash, | ||
| * builtinTools.Read, | ||
| * builtinTools.Glob, | ||
| * ] | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export const builtinTools = { | ||
| Read, | ||
| Write, | ||
| Edit, | ||
| Bash, | ||
| Glob, | ||
| Grep, | ||
| WebFetch, | ||
| WebSearch, | ||
| Task, | ||
| TodoWrite, | ||
| NotebookEdit, | ||
| AskUserQuestion, | ||
| } as const | ||
|
|
||
| /** | ||
| * Type for all built-in tool names. | ||
| */ | ||
| export type BuiltinToolName = keyof typeof builtinTools | ||
|
|
||
| /** | ||
| * Array of all built-in tool names. | ||
| */ | ||
| export const BUILTIN_TOOL_NAMES = Object.keys(builtinTools) as Array<BuiltinToolName> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @anthropic-ai/claude-agent-sdk dependency can use a newer patch version.
The latest version is 0.1.70 (published 13 hours ago), while the package.json specifies
^0.1.69. The caret constraint already permits 0.1.70, so ensure your lockfile is updated to pull this version during the next npm install if you want the latest patch.π€ Prompt for AI Agents