Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

api.byteplus-encrypted-reasoning-wire.ts

68 lines2.1 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { chat } from '@tanstack/ai'
3import { createBytePlusText } from '@tanstack/ai-byteplus'
4​
5const DUMMY_KEY = 'ark-e2e-test-dummy-key'
6const MODEL = 'seed-2-0-pro-260328'
7​
8/** A thinking-summary turn as Ark streams it: reasoning, the blob, text. */
9const deltas = [
10 { reasoning_content: 'The user wants a greeting.' },
11 { content: '', reasoning_content: '', encrypted_content: 'ENC-e2e-blob' },
12 { content: 'Hello!' },
14​
15const arkStream: typeof fetch = async () =>
16 new Response(
17 [
18 ...deltas.map((delta, index) =>
19 JSON.stringify({
20 id: 'chatcmpl-e2e',
21 object: 'chat.completion.chunk',
22 created: 0,
23 model: MODEL,
24 choices: [
25 {
26 index: 0,
27 delta,
28 finish_reason: index === deltas.length - 1 ? 'stop' : null,
29 },
30 ],
31 }),
32 ),
33 '[DONE]',
34 ]
35 .map((data) => `data: ${data}\n\n`)
36 .join(''),
37 { status: 200, headers: { 'Content-Type': 'text/event-stream' } },
38 )
39​
40/**
41 * Ark sends `encrypted_content` for a thinking-summary model. The adapter
42 * must send it as a `REASONING_ENCRYPTED_VALUE` whose `entityId` is the
43 * reasoning message id: an AG-UI client attaches the value to the message
44 * with that id. A scripted `fetch` answers, because aimock does not stream
45 * `encrypted_content`.
46 */
47export const Route = createFileRoute('/api/byteplus-encrypted-reasoning-wire')({
48 server: {
49 handlers: {
50 POST: async () => {
51 const reasoningIds: Array<string> = []
52 const entityIds: Array<string> = []
53 for await (const chunk of chat({
54 adapter: createBytePlusText(MODEL, DUMMY_KEY, { fetch: arkStream }),
55 messages: [{ role: 'user', content: 'Say hi' }],
56 })) {
57 if (chunk.type === 'REASONING_MESSAGE_START') {
58 reasoningIds.push(chunk.messageId)
59 }
60 if (chunk.type === 'REASONING_ENCRYPTED_VALUE') {
61 entityIds.push(chunk.entityId)
62 }
63 }
64 return Response.json({ reasoningIds, entityIds })
65 },
66 },
67 },
68})