Spaces:
Paused
Paused
File size: 4,946 Bytes
5d0a52f ebe8094 5d0a52f 759fe9e 5d0a52f 759fe9e 5d0a52f 5416ffb 759fe9e 53d3b3b 759fe9e 5d0a52f ab2754a 5d0a52f 759fe9e 7366e72 ab2754a 5d0a52f 759fe9e 5d0a52f fadda70 5d0a52f ab2754a 5d0a52f 759fe9e 7366e72 ab2754a 5d0a52f 759fe9e 5d0a52f fadda70 5d0a52f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | /**
* OpenAI API types for /v1/chat/completions compatibility
*/
import { z } from "zod";
// --- Request ---
const ContentPartSchema = z.object({
type: z.string(),
text: z.string().optional(),
}).passthrough();
export const ChatMessageSchema = z.object({
role: z.enum(["system", "developer", "user", "assistant", "tool", "function"]),
content: z.union([z.string(), z.array(ContentPartSchema)]).nullable().optional(),
name: z.string().optional(),
// New format: tool_calls (array, on assistant messages)
tool_calls: z.array(z.object({
id: z.string(),
type: z.literal("function"),
function: z.object({
name: z.string(),
arguments: z.string(),
}),
})).optional(),
tool_call_id: z.string().optional(),
// Legacy format: function_call (single object, on assistant messages)
function_call: z.object({
name: z.string(),
arguments: z.string(),
}).optional(),
});
export const ChatCompletionRequestSchema = z.object({
model: z.string(),
messages: z.array(ChatMessageSchema).min(1),
stream: z.boolean().optional().default(false),
n: z.number().optional().default(1),
temperature: z.number().optional(),
top_p: z.number().optional(),
max_tokens: z.number().optional(),
presence_penalty: z.number().optional(),
frequency_penalty: z.number().optional(),
stop: z.union([z.string(), z.array(z.string())]).optional(),
user: z.string().optional(),
// Codex-specific extensions
reasoning_effort: z.enum(["low", "medium", "high", "xhigh"]).optional(),
service_tier: z.enum(["fast", "flex"]).nullable().optional(),
// New tool format (accepted for compatibility, not forwarded to Codex)
tools: z.array(z.object({
type: z.literal("function"),
function: z.object({
name: z.string(),
description: z.string().optional(),
parameters: z.record(z.unknown()).optional(),
}),
})).optional(),
tool_choice: z.union([
z.enum(["none", "auto", "required"]),
z.object({ type: z.literal("function"), function: z.object({ name: z.string() }) }),
]).optional(),
parallel_tool_calls: z.boolean().optional(),
// Structured output format (JSON mode / JSON Schema)
response_format: z.object({
type: z.enum(["text", "json_object", "json_schema"]),
json_schema: z.object({
name: z.string(),
schema: z.record(z.unknown()),
strict: z.boolean().optional(),
}).optional(),
}).optional(),
// Legacy function format (accepted for compatibility, not forwarded to Codex)
functions: z.array(z.object({
name: z.string(),
description: z.string().optional(),
parameters: z.record(z.unknown()).optional(),
})).optional(),
function_call: z.union([
z.enum(["none", "auto"]),
z.object({ name: z.string() }),
]).optional(),
});
export type ChatMessage = z.infer<typeof ChatMessageSchema>;
export type ChatCompletionRequest = z.infer<typeof ChatCompletionRequestSchema>;
// --- Response (non-streaming) ---
export interface ChatCompletionToolCall {
id: string;
type: "function";
function: {
name: string;
arguments: string;
};
}
export interface ChatCompletionChoice {
index: number;
message: {
role: "assistant";
content: string | null;
reasoning_content?: string | null;
tool_calls?: ChatCompletionToolCall[];
};
finish_reason: "stop" | "length" | "tool_calls" | "function_call" | null;
}
export interface ChatCompletionUsage {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
prompt_tokens_details?: {
cached_tokens?: number;
};
completion_tokens_details?: {
reasoning_tokens?: number;
};
}
export interface ChatCompletionResponse {
id: string;
object: "chat.completion";
created: number;
model: string;
choices: ChatCompletionChoice[];
usage: ChatCompletionUsage;
}
// --- Response (streaming) ---
export interface ChatCompletionChunkToolCall {
index: number;
id?: string;
type?: "function";
function?: {
name?: string;
arguments?: string;
};
}
export interface ChatCompletionChunkDelta {
role?: "assistant";
content?: string | null;
reasoning_content?: string | null;
tool_calls?: ChatCompletionChunkToolCall[];
}
export interface ChatCompletionChunkChoice {
index: number;
delta: ChatCompletionChunkDelta;
finish_reason: "stop" | "length" | "tool_calls" | "function_call" | null;
}
export interface ChatCompletionChunk {
id: string;
object: "chat.completion.chunk";
created: number;
model: string;
choices: ChatCompletionChunkChoice[];
usage?: ChatCompletionUsage | null;
}
// --- Error ---
export interface OpenAIErrorBody {
error: {
message: string;
type: string;
param: string | null;
code: string | null;
};
}
// --- Models ---
export interface OpenAIModel {
id: string;
object: "model";
created: number;
owned_by: string;
}
export interface OpenAIModelList {
object: "list";
data: OpenAIModel[];
}
|