Skip to content

Commit d253ad4

Browse files
authored
Update chat endpoint (#349)
1 parent a47dac6 commit d253ad4

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

bin/chat.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const colors = {
2121
}
2222

2323
/**
24-
* @import { ResponsesInput, ResponseInputItem } from './types.d.ts'
25-
* @param {ResponsesInput} chatInput
26-
* @returns {Promise<ResponseInputItem[]>}
24+
* @import { ChatInput, Message } from './types.d.ts'
25+
* @param {ChatInput} chatInput
26+
* @returns {Promise<Message[]>}
2727
*/
2828
async function sendToServer(chatInput) {
2929
// Send the request to the server
30-
const response = await fetch('https://hyperparam.app/api/functions/openai/responses', {
30+
const response = await fetch('https://hyperparam.app/api/functions/chat', {
3131
method: 'POST',
3232
headers: { 'Content-Type': 'application/json' },
3333
body: JSON.stringify(chatInput),
@@ -38,7 +38,7 @@ async function sendToServer(chatInput) {
3838
}
3939

4040
// Process the streaming response
41-
/** @type {ResponseInputItem[]} */
41+
/** @type {Message[]} */
4242
const incoming = []
4343
const reader = response.body?.getReader()
4444
if (!reader) throw new Error('No response body')
@@ -89,11 +89,13 @@ async function sendToServer(chatInput) {
8989
summary: chunk.item.summary,
9090
}
9191
incoming.push(reasoningItem)
92-
} else if (!chunk.key) {
93-
console.log('Unknown chunk', chunk)
92+
} else if (chunk.key || chunk.type === 'response.completed') {
93+
// ignore
94+
} else {
95+
console.log('\nUnknown chunk', chunk)
9496
}
9597
} catch (err) {
96-
console.error('Error parsing chunk', err)
98+
console.error('\nError parsing chunk', err)
9799
}
98100
}
99101
}
@@ -105,15 +107,15 @@ async function sendToServer(chatInput) {
105107
* Will mutate the messages array!
106108
*
107109
* @import { ResponseFunctionToolCall, ToolHandler } from './types.d.ts'
108-
* @param {ResponseInputItem[]} input
110+
* @param {Message[][]} messages
109111
* @returns {Promise<void>}
110112
*/
111-
async function sendMessages(input) {
112-
/** @type {ResponsesInput} */
113+
async function sendMessages(messages) {
114+
/** @type {ChatInput} */
113115
const chatInput = {
114116
model: 'gpt-5',
115117
instructions,
116-
input,
118+
messages,
117119
reasoning: {
118120
effort: 'low',
119121
},
@@ -174,13 +176,13 @@ async function sendMessages(input) {
174176
}
175177
}
176178

177-
input.push(...incoming)
179+
messages.push(incoming)
178180

179181
// send messages with tool results
180-
await sendMessages(input)
182+
await sendMessages(messages)
181183
} else {
182184
// no tool calls, just append incoming messages
183-
input.push(...incoming)
185+
messages.push(incoming)
184186
}
185187
}
186188

@@ -231,7 +233,7 @@ function writeWithColor() {
231233
}
232234

233235
export function chat() {
234-
/** @type {ResponseInputItem[]} */
236+
/** @type {Message[][]} */
235237
const messages = []
236238
process.stdin.setEncoding('utf-8')
237239

@@ -245,7 +247,7 @@ export function chat() {
245247
try {
246248
write(colors.user, 'answer: ', colors.normal)
247249
outputMode = 'text' // switch to text output mode
248-
messages.push({ role: 'user', content: input.trim() })
250+
messages.push([{ role: 'user', content: input.trim() }])
249251
await sendMessages(messages)
250252
} catch (error) {
251253
console.error(colors.error, '\n' + error)

bin/types.d.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
// Model Input
2-
// Based on ResponseCreateParamsStreaming from openai client
3-
export interface ResponsesInput {
2+
export interface ChatInput {
43
model: string
5-
input: ResponseInputItem[] // or string but we always use stateless messages
64
instructions?: string // system prompt
7-
background?: boolean
8-
include?: string[]
5+
messages: Message[][]
6+
tools?: ResponseTool[]
97
reasoning?: {
10-
effort?: 'low' | 'medium' | 'high'
8+
effort?: 'minimal' | 'low' | 'medium' | 'high'
119
summary?: 'auto' | 'concise' | 'detailed'
1210
}
13-
tools?: ResponseTool[]
14-
max_output_tokens?: number
1511
parallel_tool_calls?: boolean
16-
previous_response_id?: string
17-
// service_tier?: 'auto' | 'default' | 'flex'
18-
// store?: boolean // store response for later retrieval
19-
temperature?: number // 0..2
20-
text?: unknown
21-
tool_choice?: 'auto' | 'none' | 'required'
22-
top_p?: number // 0..1
23-
truncation?: 'auto' | 'disabled'
24-
user?: string
2512
}
2613

27-
export type ResponseInputItem =
14+
export type Message =
2815
| EasyInputMessage
2916
| ResponseFunctionToolCall
3017
| FunctionCallOutput

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,35 @@
5656
},
5757
"dependencies": {
5858
"hightable": "0.20.4",
59-
"hyparquet": "1.20.1",
59+
"hyparquet": "1.20.2",
6060
"hyparquet-compressors": "1.1.1",
6161
"icebird": "0.3.1"
6262
},
6363
"devDependencies": {
64-
"@eslint/js": "9.38.0",
65-
"@storybook/react-vite": "10.0.1",
64+
"@eslint/js": "9.39.1",
65+
"@storybook/react-vite": "10.0.4",
6666
"@testing-library/react": "16.3.0",
67-
"@types/node": "24.9.2",
67+
"@types/node": "24.10.0",
6868
"@types/react": "19.2.2",
6969
"@types/react-dom": "19.2.2",
7070
"@vitejs/plugin-react": "5.1.0",
71-
"@vitest/coverage-v8": "4.0.5",
72-
"eslint": "9.38.0",
71+
"@vitest/coverage-v8": "4.0.7",
72+
"eslint": "9.39.1",
7373
"eslint-plugin-react": "7.37.5",
7474
"eslint-plugin-react-hooks": "7.0.1",
7575
"eslint-plugin-react-refresh": "0.4.24",
76-
"eslint-plugin-storybook": "10.0.1",
77-
"globals": "16.4.0",
78-
"jsdom": "27.0.1",
76+
"eslint-plugin-storybook": "10.0.4",
77+
"globals": "16.5.0",
78+
"jsdom": "27.1.0",
7979
"nodemon": "3.1.10",
8080
"npm-run-all": "4.1.5",
8181
"react": "19.2.0",
8282
"react-dom": "19.2.0",
83-
"storybook": "10.0.1",
83+
"storybook": "10.0.4",
8484
"typescript": "5.9.3",
85-
"typescript-eslint": "8.46.2",
86-
"vite": "7.1.12",
87-
"vitest": "4.0.5"
85+
"typescript-eslint": "8.46.3",
86+
"vite": "7.2.0",
87+
"vitest": "4.0.7"
8888
},
8989
"peerDependencies": {
9090
"react": "^18.3.1 || ^19",

0 commit comments

Comments
 (0)