diff --git a/docs/api/ai-vue.md b/docs/api/ai-vue.md new file mode 100644 index 00000000..1408d3ba --- /dev/null +++ b/docs/api/ai-vue.md @@ -0,0 +1,280 @@ +--- +title: TanStack AI Vue API +slug: /api/ai-vue +--- + +Vue hooks for TanStack AI, providing convenient Vue bindings for the headless client. + +## Installation + +```bash +npm install @tanstack/ai-vue +``` + +## `useChat(options?)` + +Main composable for managing chat state in Vue with full type safety. + +```vue + + + +``` + +### Options + +Extends `ChatClientOptions` from `@tanstack/ai-client`: + +- `connection` - Connection adapter (required) +- `tools?` - Array of client tool implementations (with `.client()` method) +- `initialMessages?` - Initial messages array +- `id?` - Unique identifier for this chat instance +- `body?` - Additional body parameters to send +- `onResponse?` - Callback when response is received +- `onChunk?` - Callback when stream chunk is received +- `onFinish?` - Callback when response finishes +- `onError?` - Callback when error occurs +- `streamProcessor?` - Stream processing configuration + +**Note:** Client tools are now automatically executed - no `onToolCall` callback needed! + +### Returns + +```typescript +interface UseChatReturn { + messages: DeepReadonly>>; + sendMessage: (content: string) => Promise; + append: (message: ModelMessage | UIMessage) => Promise; + addToolResult: (result: { + toolCallId: string; + tool: string; + output: any; + state?: "output-available" | "output-error"; + errorText?: string; + }) => Promise; + addToolApprovalResponse: (response: { + id: string; + approved: boolean; + }) => Promise; + reload: () => Promise; + stop: () => void; + isLoading: DeepReadonly>; + error: DeepReadonly>; + setMessages: (messages: UIMessage[]) => void; + clear: () => void; +} +``` + +## Connection Adapters + +Re-exported from `@tanstack/ai-client` for convenience: + +```typescript +import { + fetchServerSentEvents, + fetchHttpStream, + stream, + type ConnectionAdapter, +} from "@tanstack/ai-vue"; +``` + +## Example: Basic Chat + +```vue + + +``` + +## Example: Tool Approval + +```vue + + +``` + +## Example: Client Tools with Type Safety + +```vue + + +``` + +## `createChatClientOptions(options)` + +Helper to create typed chat options (re-exported from `@tanstack/ai-client`). + +```typescript +import { + clientTools, + createChatClientOptions, + type InferChatMessages +} from "@tanstack/ai-client"; + +// Create typed tools array (no 'as const' needed!) +const tools = clientTools(tool1, tool2); + +const chatOptions = createChatClientOptions({ + connection: fetchServerSentEvents("/api/chat"), + tools, +}); + +type Messages = InferChatMessages; +``` + +## Types + +Re-exported from `@tanstack/ai-client`: + +- `UIMessage` - Message type with tool type parameter +- `MessagePart` - Message part with tool type parameter +- `TextPart` - Text content part +- `ThinkingPart` - Thinking content part +- `ToolCallPart` - Tool call part (discriminated union) +- `ToolResultPart` - Tool result part +- `ChatClientOptions` - Chat client options +- `ConnectionAdapter` - Connection adapter interface +- `InferChatMessages` - Extract message type from options + +Re-exported from `@tanstack/ai`: + +- `toolDefinition()` - Create isomorphic tool definition +- `ToolDefinitionInstance` - Tool definition type +- `ClientTool` - Client tool type +- `ServerTool` - Server tool type + +## Next Steps + +- [Getting Started](../getting-started/quick-start) - Learn the basics +- [Tools Guide](../guides/tools) - Learn about the isomorphic tool system +- [Client Tools](../guides/client-tools) - Learn about client-side tools diff --git a/docs/config.json b/docs/config.json index 7cf47840..4b2259f2 100644 --- a/docs/config.json +++ b/docs/config.json @@ -19,7 +19,7 @@ }, { "label": "Devtools", - "to": "getting-started/devtools" + "to": "getting-started/devtools" } ] }, @@ -86,6 +86,10 @@ { "label": "@tanstack/ai-solid", "to": "api/ai-solid" + }, + { + "label": "@tanstack/ai-vue", + "to": "api/ai-vue" } ] }, @@ -542,4 +546,4 @@ ] } ] -} +} \ No newline at end of file