Open History to see the last commit
1import { createServerFn } from '@tanstack/react-start'
2import { boolean, choice, decide, score } from '@tanstack/ai'
3import { ollayaDecider } from '@tanstack/ai-ollaya'
4import { typesafeDecider } from '@tanstack/ai-typesafe'
5import { openRouterDecider } from '@tanstack/ai-openrouter'
6import { vercelGatewayDecider } from '@tanstack/ai-vercel-gateway'
7import { cloudflareDecider } from '@tanstack/ai-cloudflare'
8import { isProvider } from './models'
9import type { EvaluateResult } from '@tanstack/ai'
10import type { Provider } from './models'
11
12interface EvaluateInput {
13 ticket: string
14 provider: Provider
15}
16
17const TICKET_QUESTIONS = {
18 queue: choice({
19 instructions: 'Which team should handle this ticket?',
20 options: {
21 billing: 'Payments, invoices, refunds',
22 tech: 'Bugs, outages, integrations',
23 sales: 'Pricing, upgrades, new accounts',
24 },
25 }),
26 urgency: score({
27 instructions: 'How urgent is this ticket?',
28 levels: ['low', 'medium', 'high'],
29 }),
30 refund: boolean({
31 instructions: 'Is the customer asking for a refund?',
32 }),
33}
34
35export type TicketEvaluateResult = EvaluateResult<typeof TICKET_QUESTIONS>
36
37/**
38 * Asks three typed questions about a support ticket.
39 *
40 * The API key never leaves the server. Hosted adapters read their key from
41 * the environment inside this handler. Ollaya uses the local server. Set
42 * `OLLAYA_BASE_URL` to point that adapter at another host.
43 *
44 * The branches call the same `decide()` with a different adapter. They
45 * are written out separately rather than sharing an `adapter` variable so
46 * each call site keeps the adapter's literal model type.
47 */
48export const evaluateTicketFn = createServerFn({ method: 'POST' })
49 .inputValidator((data: EvaluateInput) => {
50 if (!data.ticket.trim()) throw new Error('Ticket text is required')
51 if (!isProvider(data.provider)) {
52 throw new Error(`Unknown provider: ${data.provider}`)
53 }
54 return data
55 })
56 .handler(async ({ data }) => {
57 const ticket = data.ticket
58 const questions = TICKET_QUESTIONS
59
60 switch (data.provider) {
61 case 'ollaya':
62 // Local Ollaya server — no key. Set OLLAYA_BASE_URL to point elsewhere.
63 return await decide({
64 adapter: ollayaDecider('laya:latest', {
65 baseURL: process.env.OLLAYA_BASE_URL?.trim() || undefined,
66 }),
67 state: ticket,
68 questions,
69 })
70 case 'typesafe':
71 return await decide({
72 adapter: typesafeDecider('jev-latest'),
73 state: ticket,
74 questions,
75 })
76 case 'openrouter':
77 return await decide({
78 adapter: openRouterDecider('~typesafe/jev-latest'),
79 state: ticket,
80 questions,
81 })
82 case 'vercel':
83 return await decide({
84 adapter: vercelGatewayDecider('typesafe-ai/jev'),
85 state: ticket,
86 questions,
87 })
88 case 'cloudflare':
89 const res = await decide({
90 adapter: cloudflareDecider('typesafe/jev'),
91 state: ticket,
92 questions,
93 })
94
95 return res
96 default: {
97 const exhaustive: never = data.provider
98 throw new Error(`Unknown provider: ${exhaustive}`)
99 }
100 }
101 })