|
1 | | -import { createFileRoute } from '@tanstack/react-router' |
2 | | -import { anthropic } from '@ai-sdk/anthropic' |
3 | | -import { convertToModelMessages, stepCountIs, streamText } from 'ai' |
| 1 | +import { createFileRoute } from "@tanstack/react-router"; |
4 | 2 |
|
5 | | -import getTools from '@/utils/demo.tools' |
| 3 | +import { convertToModelMessages, stepCountIs, streamText } from "ai"; |
| 4 | + |
| 5 | +import getTools from "@/utils/demo.tools"; |
6 | 6 |
|
7 | 7 | const SYSTEM_PROMPT = `You are a helpful assistant for a store that sells guitars. |
8 | 8 |
|
9 | 9 | You can use the following tools to help the user: |
10 | 10 |
|
11 | 11 | - getGuitars: Get all guitars from the database |
12 | 12 | - recommendGuitar: Recommend a guitar to the user |
13 | | -` |
| 13 | +`; |
14 | 14 |
|
15 | | -export const Route = createFileRoute('/demo/api/tanchat')({ |
| 15 | +export const Route = createFileRoute("/demo/api/tanchat")({ |
16 | 16 | server: { |
17 | 17 | handlers: { |
18 | 18 | POST: async ({ request }) => { |
19 | 19 | try { |
20 | | - const { messages } = await request.json() |
| 20 | + const { messages } = await request.json(); |
21 | 21 |
|
22 | | - const tools = await getTools() |
| 22 | + const tools = await getTools(); |
23 | 23 |
|
24 | 24 | const result = await streamText({ |
25 | | - model: anthropic('claude-haiku-4-5'), |
| 25 | + model: "anthropic/claude-haiku-4-5", |
26 | 26 | messages: convertToModelMessages(messages), |
27 | 27 | temperature: 0.7, |
28 | 28 | stopWhen: stepCountIs(5), |
29 | 29 | system: SYSTEM_PROMPT, |
30 | 30 | tools, |
31 | | - }) |
| 31 | + }); |
32 | 32 |
|
33 | | - return result.toUIMessageStreamResponse() |
| 33 | + return result.toUIMessageStreamResponse(); |
34 | 34 | } catch (error) { |
35 | | - console.error('Chat API error:', error) |
| 35 | + console.error("Chat API error:", error); |
36 | 36 | return new Response( |
37 | | - JSON.stringify({ error: 'Failed to process chat request' }), |
| 37 | + JSON.stringify({ error: "Failed to process chat request" }), |
38 | 38 | { |
39 | 39 | status: 500, |
40 | | - headers: { 'Content-Type': 'application/json' }, |
41 | | - }, |
42 | | - ) |
| 40 | + headers: { "Content-Type": "application/json" }, |
| 41 | + } |
| 42 | + ); |
43 | 43 | } |
44 | 44 | }, |
45 | 45 | }, |
46 | 46 | }, |
47 | | -}) |
| 47 | +}); |
0 commit comments