Skip to content

Commit df7240c

Browse files
committed
Add OpenAI Responses API support and non-streaming completions
- New responses.ts: full /v1/responses endpoint handler that translates Responses API input to ChatCompletionRequest for fixture matching, then converts fixture responses back to Responses API streaming/non-streaming format (text deltas, function_call_arguments, output_item events) - Includes item_id on all SSE delta events as required by @ai-sdk/openai's zod schema validation - Add non-streaming (stream=false) support for Chat Completions endpoint - Add ChatCompletion types and buildTextCompletion/buildToolCallCompletion helpers
1 parent 7e16609 commit df7240c

4 files changed

Lines changed: 684 additions & 5 deletions

File tree

‎src/helpers.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ErrorResponse,
77
SSEChunk,
88
ToolCall,
9+
ChatCompletion,
910
} from "./types.js";
1011

1112
export function generateId(prefix = "chatcmpl"): string {
@@ -150,3 +151,47 @@ export function buildToolCallChunks(
150151

151152
return chunks;
152153
}
154+
155+
// Non-streaming response builders
156+
157+
export function buildTextCompletion(content: string, model: string): ChatCompletion {
158+
return {
159+
id: generateId(),
160+
object: "chat.completion",
161+
created: Math.floor(Date.now() / 1000),
162+
model,
163+
choices: [
164+
{
165+
index: 0,
166+
message: { role: "assistant", content },
167+
finish_reason: "stop",
168+
},
169+
],
170+
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
171+
};
172+
}
173+
174+
export function buildToolCallCompletion(toolCalls: ToolCall[], model: string): ChatCompletion {
175+
return {
176+
id: generateId(),
177+
object: "chat.completion",
178+
created: Math.floor(Date.now() / 1000),
179+
model,
180+
choices: [
181+
{
182+
index: 0,
183+
message: {
184+
role: "assistant",
185+
content: null,
186+
tool_calls: toolCalls.map((tc, idx) => ({
187+
id: tc.id || generateToolCallId(),
188+
type: "function" as const,
189+
function: { name: tc.name, arguments: tc.arguments },
190+
})),
191+
},
192+
finish_reason: "tool_calls",
193+
},
194+
],
195+
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
196+
};
197+
}

0 commit comments

Comments
 (0)