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

log.ts

135 lines3.9 KB

1/**
2 * Print useful review-stream content to CI logs. Skip lifecycle noise.
3 */
4​
5const MAX_CHARS = 4000
6​
7function isRecord(value: unknown): value is Record<string, unknown> {
8 return typeof value === 'object' && value !== null && !Array.isArray(value)
9}
10​
11function clip(text: string) {
12 if (text.length <= MAX_CHARS) return text
13 return `${text.slice(0, MAX_CHARS)}\n… (${String(text.length - MAX_CHARS)} more chars)`
15​
16function pretty(value: unknown) {
17 if (typeof value === 'string') {
18 const trimmed = value.trim()
19 if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
20 try {
21 return JSON.stringify(JSON.parse(trimmed), null, 2)
22 } catch {
23 return value
24 }
25 }
26 return value
27 }
28 try {
29 return JSON.stringify(value, null, 2)
30 } catch {
31 return String(value)
32 }
34​
35function readString(value: unknown, key: string) {
36 if (!isRecord(value) || typeof value[key] !== 'string') return null
37 return value[key]
39​
40/**
41 * Buffer text/reasoning/tool args, then write one block per finished item.
42 */
43export function createReviewStreamLogger(
44 write: (line: string) => void = console.log,
45) {
46 let text = ''
47 let reasoning = ''
48 const toolName = new Map<string, string>()
49 const toolArgs = new Map<string, string>()
50​
51 function flush(label: string, buffer: string) {
52 const body = buffer.trim()
53 if (body.length === 0) return
54 write(`${label}:\n${clip(body)}`)
55 }
56​
57 return {
58 chunk(value: unknown) {
59 if (!isRecord(value) || typeof value.type !== 'string') return
60 const type = value.type
61 if (type === 'TEXT_MESSAGE_CONTENT' || type === 'TEXT_MESSAGE_CHUNK') {
62 const delta = readString(value, 'delta')
63 if (delta !== null) text += delta
64 return
65 }
66 if (type === 'TEXT_MESSAGE_END') {
67 flush('text', text)
68 text = ''
69 return
70 }
71 if (
72 type === 'REASONING_MESSAGE_CONTENT' ||
73 type === 'REASONING_MESSAGE_CHUNK' ||
74 type === 'THINKING_TEXT_MESSAGE_CONTENT'
75 ) {
76 const delta = readString(value, 'delta')
77 if (delta !== null) reasoning += delta
78 return
79 }
80 if (
81 type === 'REASONING_MESSAGE_END' ||
82 type === 'THINKING_TEXT_MESSAGE_END'
83 ) {
84 flush('reasoning', reasoning)
85 reasoning = ''
86 return
87 }
88 if (type === 'TOOL_CALL_START') {
89 const id = readString(value, 'toolCallId')
90 const name =
91 readString(value, 'toolCallName') ??
92 readString(value, 'toolName') ??
93 'tool'
94 if (id !== null) toolName.set(id, name)
95 return
96 }
97 if (type === 'TOOL_CALL_ARGS' || type === 'TOOL_CALL_CHUNK') {
98 const id = readString(value, 'toolCallId')
99 const delta = readString(value, 'delta')
100 if (id !== null && delta !== null) {
101 toolArgs.set(id, (toolArgs.get(id) ?? '') + delta)
102 }
103 return
104 }
105 if (type === 'TOOL_CALL_END') {
106 const id = readString(value, 'toolCallId')
107 if (id === null) return
108 const name = toolName.get(id) ?? 'tool'
109 const input =
110 'input' in value && value.input !== undefined
111 ? pretty(value.input)
112 : pretty(toolArgs.get(id) ?? '')
113 write(`tool ${name} input:\n${clip(input)}`)
114 return
115 }
116 if (type === 'TOOL_CALL_RESULT') {
117 const id = readString(value, 'toolCallId')
118 const name = (id !== null ? toolName.get(id) : undefined) ?? 'tool'
119 const content = 'content' in value ? pretty(value.content) : ''
120 write(`tool ${name} output:\n${clip(content)}`)
121 return
122 }
123 if (type === 'RUN_ERROR') {
124 const message = readString(value, 'message') ?? pretty(value)
125 write(`error: ${message}`)
126 }
127 },
128 flush() {
129 flush('text', text)
130 text = ''
131 flush('reasoning', reasoning)
132 reasoning = ''
133 },
134 }