Open History to see the last commit
1import { createServerFn } from "@tanstack/react-start";
2import { anthropic } from "@ai-sdk/anthropic";
3import { streamText } from "ai";
4
5import getTools from "./demo.tools";
6
7export interface Message {
8 id: string;
9 role: "user" | "assistant";
10 content: string;
11}
12
13const SYSTEM_PROMPT = `You are a helpful assistant for a store that sells motorcycles.
14
15You are also a motorcycle enthusiast who is passionate about our products and how they can bring joy to people's lives, the excitement of the open road and adventure. Emphasize these aspects in your responses.
16
17You can use the following tools to help the user:
18
19- getMotorcycles: Get all motorcycles from the database
20- recommendMotorcycle: Recommend a motorcycle to the user
21`;
22
23export const genAIResponse = createServerFn({ method: "POST", response: "raw" })
24 .validator(
25 (d: {
26 messages: Array<Message>;
27 systemPrompt?: { value: string; enabled: boolean };
28 }) => d
29 )
30 .handler(async ({ data }) => {
31 const messages = data.messages
32 .filter(
33 (msg) =>
34 msg.content.trim() !== "" &&
35 !msg.content.startsWith("Sorry, I encountered an error")
36 )
37 .map((msg) => ({
38 role: msg.role,
39 content: msg.content.trim(),
40 }));
41
42 const tools = await getTools();
43
44 try {
45 const result = streamText({
46 model: anthropic("claude-3-5-sonnet-latest"),
47 messages,
48 system: SYSTEM_PROMPT,
49 maxSteps: 10,
50 tools,
51 });
52
53 return result.toDataStreamResponse();
54 } catch (error) {
55 console.error("Error in genAIResponse:", error);
56 if (error instanceof Error && error.message.includes("rate limit")) {
57 return { error: "Rate limit exceeded. Please try again in a moment." };
58 }
59 return {
60 error:
61 error instanceof Error ? error.message : "Failed to get AI response",
62 };
63 }
64 });