@simulacra-ai/openai 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +32 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +48 -4
- package/dist/index.d.ts +48 -4
- package/dist/index.js +32 -9
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -51,7 +51,8 @@ var OpenAIProvider = class _OpenAIProvider {
|
|
|
51
51
|
* @returns A promise that resolves when the request completes.
|
|
52
52
|
*/
|
|
53
53
|
async execute_request(request, receiver, cancellation) {
|
|
54
|
-
const { model, max_tokens, ...api_extras } = this.#config;
|
|
54
|
+
const { model, max_tokens, system_role, strict_tools, ...api_extras } = this.#config;
|
|
55
|
+
const emit_strict = resolve_strict_tools(model, strict_tools);
|
|
55
56
|
const params = {
|
|
56
57
|
...api_extras,
|
|
57
58
|
model,
|
|
@@ -59,10 +60,10 @@ var OpenAIProvider = class _OpenAIProvider {
|
|
|
59
60
|
max_tokens,
|
|
60
61
|
...request.tools.length > 0 ? {
|
|
61
62
|
tool_choice: "auto",
|
|
62
|
-
tools: request.tools.map((t) => to_openai_tool(t))
|
|
63
|
+
tools: request.tools.map((t) => to_openai_tool(t, emit_strict))
|
|
63
64
|
} : {},
|
|
64
65
|
messages: [
|
|
65
|
-
...get_system_context(model, request.system),
|
|
66
|
+
...get_system_context(model, request.system, system_role),
|
|
66
67
|
...request.messages.flatMap((m) => to_openai_messages(m))
|
|
67
68
|
],
|
|
68
69
|
stream_options: {
|
|
@@ -237,26 +238,48 @@ var OpenAIProvider = class _OpenAIProvider {
|
|
|
237
238
|
}
|
|
238
239
|
}
|
|
239
240
|
};
|
|
240
|
-
function get_system_context(model, system) {
|
|
241
|
+
function get_system_context(model, system, system_role = "auto") {
|
|
241
242
|
if (!system) {
|
|
242
243
|
return [];
|
|
243
244
|
}
|
|
244
|
-
|
|
245
|
+
const role = resolve_system_role(model, system_role);
|
|
246
|
+
if (role === "developer") {
|
|
245
247
|
return [
|
|
246
248
|
{
|
|
247
|
-
role: "
|
|
249
|
+
role: "developer",
|
|
248
250
|
content: system
|
|
249
251
|
}
|
|
250
252
|
];
|
|
251
253
|
}
|
|
252
254
|
return [
|
|
253
255
|
{
|
|
254
|
-
role: "
|
|
256
|
+
role: "system",
|
|
255
257
|
content: system
|
|
256
258
|
}
|
|
257
259
|
];
|
|
258
260
|
}
|
|
259
|
-
function
|
|
261
|
+
function resolve_system_role(model, system_role = "auto") {
|
|
262
|
+
if (system_role === "system" || system_role === "developer") {
|
|
263
|
+
return system_role;
|
|
264
|
+
}
|
|
265
|
+
if (model.startsWith("gpt")) {
|
|
266
|
+
return "system";
|
|
267
|
+
}
|
|
268
|
+
if (is_openai_reasoning_model(model)) {
|
|
269
|
+
return "developer";
|
|
270
|
+
}
|
|
271
|
+
return "system";
|
|
272
|
+
}
|
|
273
|
+
function resolve_strict_tools(model, strict_tools = "auto") {
|
|
274
|
+
if (strict_tools === "never") {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
return model.startsWith("gpt") || is_openai_reasoning_model(model);
|
|
278
|
+
}
|
|
279
|
+
function is_openai_reasoning_model(model) {
|
|
280
|
+
return /^o[1-9]\d*(-|$)/.test(model);
|
|
281
|
+
}
|
|
282
|
+
function to_openai_tool(tool, strict) {
|
|
260
283
|
function map_parameter_type(parameter) {
|
|
261
284
|
switch (parameter.type) {
|
|
262
285
|
case "object":
|
|
@@ -295,7 +318,7 @@ function to_openai_tool(tool) {
|
|
|
295
318
|
tool.parameters.map(({ name, ...parameter }) => [name, parameter])
|
|
296
319
|
)
|
|
297
320
|
}),
|
|
298
|
-
strict: true
|
|
321
|
+
...strict ? { strict: true } : {}
|
|
299
322
|
}
|
|
300
323
|
};
|
|
301
324
|
}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/openai-provider.ts"],"sourcesContent":["export { OpenAIProvider, type OpenAIProviderConfig } from \"./openai-provider.ts\";\n","import { OpenAI } from \"openai\";\n\nimport type {\n AssistantContent,\n AssistantMessage,\n CancellationToken,\n CompletionResponseData,\n Content,\n Message,\n ModelProvider,\n ModelRequest,\n ParameterType,\n ProviderContextTransformer,\n StreamReceiver,\n ToolContent,\n ToolDefinition,\n Usage,\n} from \"@simulacra-ai/core\";\n\ntype Prettify<T> = { [K in keyof T]: T[K] } & {};\n\nconst OPENAI_REASONING_DELTA_KEYS = [\"reasoning\", \"reasoning_content\", \"thinking\"] as const;\n\ntype OpenAIReasoningDeltaKey = (typeof OPENAI_REASONING_DELTA_KEYS)[number];\ntype OpenAICompletionDelta = OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta;\ntype OpenAIReasoningDelta = OpenAICompletionDelta &\n Partial<Record<OpenAIReasoningDeltaKey, string>>;\n\n/**\n * Configuration options for the OpenAI provider.\n */\nexport interface OpenAIProviderConfig extends Record<string, unknown> {\n /** The model identifier to use (e.g., \"gpt-4\", \"o1-preview\"). */\n model: string;\n /** The maximum number of tokens to generate in the response. */\n max_tokens?: number;\n}\n\n/**\n * Model provider implementation for OpenAI's chat completion models.\n *\n * This provider wraps the OpenAI SDK to provide streaming completions with support\n * for tool use and function calling. It handles message formatting, content streaming,\n * and usage tracking according to the ModelProvider interface. Supports both GPT models\n * (using system messages) and O-series models (using developer messages).\n */\nexport class OpenAIProvider implements ModelProvider {\n readonly #sdk: OpenAI;\n readonly #config: OpenAIProviderConfig;\n readonly context_transformers: ProviderContextTransformer[];\n\n /**\n * Creates a new OpenAI provider instance.\n *\n * @param sdk - The initialized OpenAI SDK client.\n * @param config - Configuration options for the provider.\n * @param context_transformers - Provider-level context transformers.\n */\n constructor(\n sdk: OpenAI,\n config: OpenAIProviderConfig,\n context_transformers: ProviderContextTransformer[] = [],\n ) {\n this.#sdk = sdk;\n this.#config = config;\n this.context_transformers = context_transformers;\n }\n\n /**\n * Executes a model request and streams the response through the provided receiver.\n *\n * @param request - The request containing messages, tools, and system prompt.\n * @param receiver - The receiver that handles streaming events.\n * @param cancellation - Token to signal cancellation of the request.\n * @returns A promise that resolves when the request completes.\n */\n async execute_request(\n request: ModelRequest,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ): Promise<void> {\n const { model, max_tokens, ...api_extras } = this.#config;\n const params: OpenAI.ChatCompletionCreateParamsStreaming = {\n ...api_extras,\n model,\n stream: true,\n max_tokens,\n ...(request.tools.length > 0\n ? {\n tool_choice: \"auto\",\n tools: request.tools.map((t) => to_openai_tool(t)),\n }\n : {}),\n messages: [\n ...get_system_context(model, request.system),\n ...request.messages.flatMap((m) => to_openai_messages(m)),\n ],\n stream_options: {\n include_usage: true,\n },\n };\n\n receiver.before_request({ params });\n receiver.request_raw(params);\n\n const stream = await this.#sdk.chat.completions.create(params);\n\n // Intentionally not awaited. Streaming is event-driven through the receiver.\n // The policy wraps only connection establishment; chunk processing flows\n // asynchronously via StreamListener events back to the conversation.\n this.#stream_response(stream, receiver, cancellation);\n }\n\n /**\n * Creates a clone of this provider with the same configuration.\n *\n * @returns A new provider instance with identical configuration.\n */\n clone(): ModelProvider {\n return new OpenAIProvider(this.#sdk, this.#config, this.context_transformers);\n }\n\n async #stream_response(\n stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ) {\n try {\n let response: OpenAI.Chat.Completions.ChatCompletionChunk | undefined;\n for await (const response_chunk of stream) {\n if (cancellation.is_cancellation_requested) {\n receiver.cancel();\n return;\n }\n receiver.stream_raw(response_chunk);\n\n const { choices: choices_chunk, ...rest } = response_chunk;\n response = {\n ...response,\n ...rest,\n choices: response?.choices ?? [],\n };\n\n for (const choice_chunk of choices_chunk) {\n if (!response.choices[choice_chunk.index]) {\n response.choices[choice_chunk.index] = choice_chunk;\n const message = from_openai_completion(response_chunk, choice_chunk);\n for (const content of message.content) {\n receiver.start_content({ content, message, usage: {} });\n }\n receiver.start_message({ message, usage: {} });\n continue;\n }\n\n const { delta: delta_chunk, ...rest } = choice_chunk;\n const choice = (response.choices[choice_chunk.index] = {\n ...response.choices[choice_chunk.index],\n ...rest,\n delta: {\n ...response.choices[choice_chunk.index]?.delta,\n },\n });\n\n if (delta_chunk.role) {\n choice.delta.role = delta_chunk.role;\n }\n if (delta_chunk.refusal) {\n if (!choice.delta.refusal) {\n choice.delta.refusal = \"\";\n }\n choice.delta.refusal += delta_chunk.refusal;\n }\n const reasoning_delta = get_openai_reasoning_delta(delta_chunk);\n if (reasoning_delta) {\n const choice_delta = choice.delta as OpenAIReasoningDelta;\n const existing = choice_delta[reasoning_delta.key];\n if (!existing) {\n choice_delta[reasoning_delta.key] = reasoning_delta.thought;\n receiver.start_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice_delta[reasoning_delta.key] = existing + reasoning_delta.thought;\n receiver.update_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.content) {\n if (!choice.delta.content) {\n choice.delta.content = delta_chunk.content;\n receiver.start_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice.delta.content += delta_chunk.content;\n receiver.update_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls) {\n choice.delta.tool_calls = [];\n }\n for (const tool_call_chunk of delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls[tool_call_chunk.index]) {\n choice.delta.tool_calls[tool_call_chunk.index] = tool_call_chunk;\n receiver.start_content({\n content: from_openai_tool_call(tool_call_chunk),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n const tool_call = choice.delta.tool_calls[tool_call_chunk.index];\n\n if (tool_call_chunk.id) {\n tool_call.id = tool_call_chunk.id;\n }\n if (tool_call_chunk.type) {\n tool_call.type = tool_call_chunk.type;\n }\n if (tool_call_chunk.function) {\n if (!tool_call.function) {\n tool_call.function = tool_call_chunk.function;\n } else {\n if (tool_call_chunk.function.name) {\n tool_call.function.name = tool_call_chunk.function.name;\n }\n if (tool_call_chunk.function.arguments) {\n if (!tool_call.function.arguments) {\n tool_call.function.arguments = \"\";\n }\n tool_call.function.arguments += tool_call_chunk.function.arguments;\n }\n }\n }\n receiver.update_content({\n content: from_openai_tool_call(tool_call),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n }\n }\n }\n if (!response || !response.choices?.[0]) {\n throw new Error(\"no data\");\n }\n receiver.response_raw({ ...response });\n\n const message = from_openai_completion(response, response.choices[0]);\n const usage = response?.usage ? from_openai_usage(response.usage) : {};\n for (const content of message.content) {\n receiver.complete_content({ content, message, usage });\n }\n receiver.complete_message({ message, usage, ...map_stop_reason(response) });\n } catch (error) {\n receiver.error(error);\n }\n }\n}\n\nfunction get_system_context(model: string, system?: string): OpenAI.ChatCompletionMessageParam[] {\n if (!system) {\n return [];\n }\n if (model.startsWith(\"gpt\")) {\n return [\n {\n role: \"system\",\n content: system,\n } as OpenAI.ChatCompletionSystemMessageParam,\n ];\n }\n return [\n {\n role: \"developer\",\n content: system,\n } as OpenAI.ChatCompletionDeveloperMessageParam,\n ];\n}\n\nfunction to_openai_tool(tool: ToolDefinition): OpenAI.Chat.ChatCompletionTool {\n function map_parameter_type(\n parameter: Prettify<ParameterType & { description?: string }>,\n ): OpenAI.FunctionParameters {\n switch (parameter.type) {\n case \"object\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n properties: Object.fromEntries(\n Object.entries(parameter.properties).map(([k, v]) => [k, map_parameter_type(v)]),\n ),\n additionalProperties: false,\n required: Object.entries(parameter.properties).map(([k]) => k),\n };\n case \"array\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n items: map_parameter_type(parameter.items),\n };\n default:\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description:\n parameter.default !== undefined\n ? parameter.description\n ? `${parameter.description} (default: ${parameter.default})`\n : `default: ${parameter.default}`\n : parameter.description,\n enum: \"enum\" in parameter ? parameter.enum : undefined,\n };\n }\n }\n return {\n type: \"function\",\n function: {\n name: tool.name,\n description: tool.description,\n parameters: map_parameter_type({\n type: \"object\",\n required: true,\n properties: Object.fromEntries(\n tool.parameters.map(({ name, ...parameter }) => [name, parameter]),\n ),\n }),\n strict: true,\n },\n };\n}\n\nfunction from_openai_completion(\n completion: OpenAI.Chat.Completions.ChatCompletionChunk,\n choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice,\n) {\n let contents: Content[] = [];\n const thinking = from_openai_thinking(choice.delta);\n if (thinking) {\n contents = [...contents, thinking];\n }\n const delta_record = choice.delta as Record<string, unknown>;\n for (const key of Object.keys(choice.delta)) {\n if (key === \"role\") {\n continue;\n }\n if (is_openai_reasoning_delta_key(key)) {\n continue;\n }\n if (key === \"content\") {\n if (choice.delta.content) {\n contents = [...contents, from_openai_content(choice.delta)];\n }\n continue;\n }\n if (key === \"refusal\") {\n if (choice.delta.refusal) {\n contents = [...contents, from_openai_refusal(choice.delta)];\n }\n continue;\n }\n if (key === \"tool_calls\") {\n if (choice.delta.tool_calls) {\n contents = [...contents, ...choice.delta.tool_calls.map((t) => from_openai_tool_call(t))];\n }\n continue;\n }\n if (delta_record[key] !== undefined && delta_record[key] !== null) {\n const data = delta_record[key];\n contents = [\n ...contents,\n {\n type: \"raw\",\n model_kind: \"openai\",\n data: JSON.stringify({ [key]: data }),\n },\n ];\n }\n }\n return {\n id: completion.id,\n timestamp: completion.created,\n role: map_role(choice),\n content: contents,\n } as AssistantMessage;\n}\n\nfunction get_openai_reasoning_delta(delta: OpenAICompletionDelta) {\n const record = delta as Partial<Record<OpenAIReasoningDeltaKey, unknown>>;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n const thought = record[key];\n if (typeof thought === \"string\" && thought.length > 0) {\n return { key, thought };\n }\n }\n return undefined;\n}\n\nfunction from_openai_thinking(content: OpenAICompletionDelta) {\n const reasoning = get_openai_reasoning_delta(content);\n if (!reasoning) {\n return undefined;\n }\n const extended = get_openai_delta_extended(content);\n return {\n type: \"thinking\",\n thought: reasoning.thought,\n extended: {\n ...extended,\n openai_reasoning_field: reasoning.key,\n },\n } as Content;\n}\n\nfunction is_openai_reasoning_delta_key(key: string): key is OpenAIReasoningDeltaKey {\n return OPENAI_REASONING_DELTA_KEYS.includes(key as OpenAIReasoningDeltaKey);\n}\n\nfunction from_openai_refusal(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.refusal,\n extended: {\n ...get_openai_delta_extended(content),\n openai_refusal: true,\n },\n } as Content;\n}\n\nfunction from_openai_content(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.content,\n extended: get_openai_delta_extended(content),\n } as Content;\n}\n\nfunction get_openai_delta_extended(content: OpenAICompletionDelta) {\n const extended = { ...(content as Record<string, unknown>) };\n delete extended.content;\n delete extended.tool_calls;\n delete extended.function_call;\n delete extended.refusal;\n delete extended.role;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n delete extended[key];\n }\n return extended;\n}\n\nfunction from_openai_tool_call(\n tool_call: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta.ToolCall,\n) {\n const { id: tool_request_id, function: fn, type: _, index: __, ...extended } = tool_call;\n let params: unknown;\n try {\n params = JSON.parse(fn?.arguments ?? \"{}\");\n } catch {\n params = fn?.arguments;\n }\n return {\n tool_request_id,\n type: \"tool\",\n tool: fn?.name,\n params,\n extended,\n } as ToolContent;\n}\n\nfunction to_openai_messages(message: Message) {\n if (message.role === \"assistant\") {\n return [to_openai_assistant_message(message)];\n }\n // Partition content so tool_result blocks come before non-tool_result blocks.\n // OpenAI requires all tool-role messages immediately after the assistant message\n // containing the corresponding tool_calls; interleaving user messages between\n // tool messages causes a validation error.\n const tool_result_content = message.content.filter((c) => c.type === \"tool_result\");\n const other_content = message.content.filter((c) => c.type !== \"tool_result\");\n const ordered_content = [...tool_result_content, ...other_content];\n\n const results: OpenAI.ChatCompletionMessageParam[] = [];\n let result: OpenAI.ChatCompletionMessageParam | undefined;\n for (const content of ordered_content) {\n if (content.type === \"text\") {\n if (!result) {\n result = {\n role: \"user\",\n content: content.text,\n };\n } else if (result.role === \"tool\") {\n results.push(result);\n result = {\n role: \"user\",\n content: content.text,\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = [\n {\n type: \"text\",\n text: content.text,\n },\n ];\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n } else if (content.type === \"tool_result\") {\n if (!result) {\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else if (result.role !== \"tool\" || result.tool_call_id !== content.tool_request_id) {\n results.push(result);\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n result.content.push({\n type: \"text\",\n text: JSON.stringify(content.result),\n });\n }\n } else if (content.type === \"raw\") {\n result = {\n ...(result ?? {}),\n ...JSON.parse(content.data),\n };\n }\n }\n if (result) {\n results.push(result);\n }\n return results;\n}\n\nfunction to_openai_assistant_message(message: AssistantMessage) {\n let result: OpenAI.ChatCompletionAssistantMessageParam = {\n role: \"assistant\",\n };\n for (const content of message.content) {\n switch (content.type) {\n case \"text\":\n if (content.extended && content.extended.openai_refusal === true) {\n result.refusal = content.text;\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.text;\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n break;\n case \"tool\":\n if (!result.tool_calls) {\n result.tool_calls = [];\n }\n result.tool_calls.push({\n id: content.tool_request_id,\n type: \"function\",\n function: {\n name: content.tool,\n arguments: JSON.stringify(content.params),\n },\n });\n break;\n case \"raw\":\n if (content.model_kind !== \"openai\") {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.data;\n } else {\n result.content.push({\n type: \"text\",\n text: content.data,\n });\n }\n break;\n }\n result = {\n ...result,\n ...JSON.parse(content.data),\n };\n break;\n case \"thinking\":\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.thought;\n } else {\n result.content.push({\n type: \"text\",\n text: content.thought,\n });\n }\n break;\n default:\n throw new Error(\"unexpected content type\");\n }\n }\n return result;\n}\n\nfunction from_openai_usage(usage: OpenAI.CompletionUsage | null | undefined) {\n return {\n input_tokens: usage?.prompt_tokens,\n output_tokens: usage?.completion_tokens,\n } as Usage;\n}\n\nfunction map_stop_reason(\n completion: OpenAI.ChatCompletionChunk,\n): Pick<CompletionResponseData, \"stop_reason\" | \"stop_details\"> {\n for (const choice of completion.choices) {\n switch (choice.finish_reason) {\n case \"content_filter\":\n return {\n stop_reason: \"error\",\n stop_details: choice.finish_reason,\n };\n case \"function_call\":\n return {\n stop_reason: \"tool_use\",\n };\n case \"length\":\n return {\n stop_reason: \"max_tokens\",\n };\n case \"stop\":\n return {\n stop_reason: \"end_turn\",\n };\n case \"tool_calls\":\n return {\n stop_reason: \"tool_use\",\n };\n default:\n return {\n stop_reason: \"other\",\n stop_details: `${choice.finish_reason}`,\n };\n }\n }\n return {\n stop_reason: \"other\",\n };\n}\n\nfunction map_role(choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice) {\n switch (choice.delta.role) {\n case \"user\":\n case \"developer\":\n case \"system\":\n return \"user\";\n case \"assistant\":\n case \"tool\":\n return \"assistant\";\n default:\n throw new Error(\"invalid role\");\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqBA,IAAM,8BAA8B,CAAC,aAAa,qBAAqB,UAAU;AAyB1E,IAAM,iBAAN,MAAM,gBAAwC;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAST,YACE,KACA,QACA,uBAAqD,CAAC,GACtD;AACA,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,uBAAuB;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,gBACJ,SACA,UACA,cACe;AACf,UAAM,EAAE,OAAO,YAAY,GAAG,WAAW,IAAI,KAAK;AAClD,UAAM,SAAqD;AAAA,MACzD,GAAG;AAAA,MACH;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA,GAAI,QAAQ,MAAM,SAAS,IACvB;AAAA,QACE,aAAa;AAAA,QACb,OAAO,QAAQ,MAAM,IAAI,CAAC,MAAM,eAAe,CAAC,CAAC;AAAA,MACnD,IACA,CAA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,SAAS,QAA4D;AAC5E,UAAQ,OAAO,MAAM,MAAM;AAAA,IACzB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT;AACE,YAAM,IAAI,MAAM,cAAc;AAAA,EAClC;AACF;","names":["message","rest"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/openai-provider.ts"],"sourcesContent":["export { OpenAIProvider, type OpenAIProviderConfig } from \"./openai-provider.ts\";\n","import { OpenAI } from \"openai\";\n\nimport type {\n AssistantContent,\n AssistantMessage,\n CancellationToken,\n CompletionResponseData,\n Content,\n Message,\n ModelProvider,\n ModelRequest,\n ParameterType,\n ProviderContextTransformer,\n StreamReceiver,\n ToolContent,\n ToolDefinition,\n Usage,\n} from \"@simulacra-ai/core\";\n\ntype Prettify<T> = { [K in keyof T]: T[K] } & {};\n\nconst OPENAI_REASONING_DELTA_KEYS = [\"reasoning\", \"reasoning_content\", \"thinking\"] as const;\n\ntype OpenAIReasoningDeltaKey = (typeof OPENAI_REASONING_DELTA_KEYS)[number];\ntype OpenAICompletionDelta = OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta;\ntype OpenAIReasoningDelta = OpenAICompletionDelta &\n Partial<Record<OpenAIReasoningDeltaKey, string>>;\n\n/**\n * Configuration options for the OpenAI provider.\n */\nexport interface OpenAIProviderConfig extends Record<string, unknown> {\n /** The model identifier to use (e.g., \"gpt-4\", \"o1-preview\"). */\n model: string;\n /** The maximum number of tokens to generate in the response. */\n max_tokens?: number;\n /**\n * Which role to use for the system prompt.\n *\n * The OpenAI Chat Completions spec allows `system` (legacy / most providers)\n * and `developer` (introduced for o-series reasoning models). Most\n * OpenAI-compatible endpoints (DeepSeek, OpenRouter relays, Anthropic-via-\n * compat, self-hosted gateways, etc.) only accept `system` and reject\n * `developer` with a 400.\n *\n * - `\"auto\"` (default): use the built-in heuristic — `gpt*` → `\"system\"`,\n * o-series (`o1`, `o3`, `o4`, ...) → `\"developer\"`, anything else →\n * `\"system\"` (the broadly-compatible default).\n * - `\"system\"` / `\"developer\"`: force the role regardless of model id.\n *\n * The heuristic matches on the bare model id and does not understand\n * relay-style prefixes (`openai/o3`, `anthropic/claude-3-5-sonnet`,\n * etc.) — set this explicitly when routing OpenAI models through a\n * relay that uses `vendor/model` ids.\n */\n system_role?: \"auto\" | \"system\" | \"developer\";\n /**\n * Whether to emit OpenAI's strict structured-output flag on tool\n * definitions (`function.strict: true`).\n *\n * `strict` is an OpenAI-specific extension that constrains tool arguments\n * to the supplied JSON Schema. Most non-OpenAI endpoints either ignore the\n * field or reject it.\n *\n * - `\"auto\"` (default): emit `strict: true` when the model id matches the\n * built-in OpenAI heuristic — `gpt*` or o-series (`o1`, `o3`, `o4`, ...).\n * Any other model id (deepseek, etc.) gets tool defs without `strict`.\n * The heuristic matches on the bare model id; relay-style prefixes\n * (`openai/gpt-4o`) are NOT recognized — set this explicitly when\n * routing OpenAI models through a relay.\n * - `\"never\"`: never emit `strict`.\n */\n strict_tools?: \"auto\" | \"never\";\n}\n\n/**\n * Model provider implementation for OpenAI's chat completion models.\n *\n * This provider wraps the OpenAI SDK to provide streaming completions with\n * support for tool use and function calling. It handles message formatting,\n * content streaming, and usage tracking according to the ModelProvider\n * interface.\n *\n * Works against OpenAI directly as well as OpenAI-compatible endpoints\n * (DeepSeek, OpenRouter, self-hosted gateways, etc.). The default behaviour\n * picks `system` vs. `developer` for the system prompt and decides whether\n * to emit OpenAI's `strict` flag on tool defs based on the model id; both\n * can be overridden through `OpenAIProviderConfig.system_role` and\n * `OpenAIProviderConfig.strict_tools` for endpoints whose behaviour differs.\n */\nexport class OpenAIProvider implements ModelProvider {\n readonly #sdk: OpenAI;\n readonly #config: OpenAIProviderConfig;\n readonly context_transformers: ProviderContextTransformer[];\n\n /**\n * Creates a new OpenAI provider instance.\n *\n * @param sdk - The initialized OpenAI SDK client.\n * @param config - Configuration options for the provider.\n * @param context_transformers - Provider-level context transformers.\n */\n constructor(\n sdk: OpenAI,\n config: OpenAIProviderConfig,\n context_transformers: ProviderContextTransformer[] = [],\n ) {\n this.#sdk = sdk;\n this.#config = config;\n this.context_transformers = context_transformers;\n }\n\n /**\n * Executes a model request and streams the response through the provided receiver.\n *\n * @param request - The request containing messages, tools, and system prompt.\n * @param receiver - The receiver that handles streaming events.\n * @param cancellation - Token to signal cancellation of the request.\n * @returns A promise that resolves when the request completes.\n */\n async execute_request(\n request: ModelRequest,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ): Promise<void> {\n const { model, max_tokens, system_role, strict_tools, ...api_extras } = this.#config;\n const emit_strict = resolve_strict_tools(model, strict_tools);\n const params: OpenAI.ChatCompletionCreateParamsStreaming = {\n ...api_extras,\n model,\n stream: true,\n max_tokens,\n ...(request.tools.length > 0\n ? {\n tool_choice: \"auto\",\n tools: request.tools.map((t) => to_openai_tool(t, emit_strict)),\n }\n : {}),\n messages: [\n ...get_system_context(model, request.system, system_role),\n ...request.messages.flatMap((m) => to_openai_messages(m)),\n ],\n stream_options: {\n include_usage: true,\n },\n };\n\n receiver.before_request({ params });\n receiver.request_raw(params);\n\n const stream = await this.#sdk.chat.completions.create(params);\n\n // Intentionally not awaited. Streaming is event-driven through the receiver.\n // The policy wraps only connection establishment; chunk processing flows\n // asynchronously via StreamListener events back to the conversation.\n this.#stream_response(stream, receiver, cancellation);\n }\n\n /**\n * Creates a clone of this provider with the same configuration.\n *\n * @returns A new provider instance with identical configuration.\n */\n clone(): ModelProvider {\n return new OpenAIProvider(this.#sdk, this.#config, this.context_transformers);\n }\n\n async #stream_response(\n stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ) {\n try {\n let response: OpenAI.Chat.Completions.ChatCompletionChunk | undefined;\n for await (const response_chunk of stream) {\n if (cancellation.is_cancellation_requested) {\n receiver.cancel();\n return;\n }\n receiver.stream_raw(response_chunk);\n\n const { choices: choices_chunk, ...rest } = response_chunk;\n response = {\n ...response,\n ...rest,\n choices: response?.choices ?? [],\n };\n\n for (const choice_chunk of choices_chunk) {\n if (!response.choices[choice_chunk.index]) {\n response.choices[choice_chunk.index] = choice_chunk;\n const message = from_openai_completion(response_chunk, choice_chunk);\n for (const content of message.content) {\n receiver.start_content({ content, message, usage: {} });\n }\n receiver.start_message({ message, usage: {} });\n continue;\n }\n\n const { delta: delta_chunk, ...rest } = choice_chunk;\n const choice = (response.choices[choice_chunk.index] = {\n ...response.choices[choice_chunk.index],\n ...rest,\n delta: {\n ...response.choices[choice_chunk.index]?.delta,\n },\n });\n\n if (delta_chunk.role) {\n choice.delta.role = delta_chunk.role;\n }\n if (delta_chunk.refusal) {\n if (!choice.delta.refusal) {\n choice.delta.refusal = \"\";\n }\n choice.delta.refusal += delta_chunk.refusal;\n }\n const reasoning_delta = get_openai_reasoning_delta(delta_chunk);\n if (reasoning_delta) {\n const choice_delta = choice.delta as OpenAIReasoningDelta;\n const existing = choice_delta[reasoning_delta.key];\n if (!existing) {\n choice_delta[reasoning_delta.key] = reasoning_delta.thought;\n receiver.start_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice_delta[reasoning_delta.key] = existing + reasoning_delta.thought;\n receiver.update_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.content) {\n if (!choice.delta.content) {\n choice.delta.content = delta_chunk.content;\n receiver.start_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice.delta.content += delta_chunk.content;\n receiver.update_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls) {\n choice.delta.tool_calls = [];\n }\n for (const tool_call_chunk of delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls[tool_call_chunk.index]) {\n choice.delta.tool_calls[tool_call_chunk.index] = tool_call_chunk;\n receiver.start_content({\n content: from_openai_tool_call(tool_call_chunk),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n const tool_call = choice.delta.tool_calls[tool_call_chunk.index];\n\n if (tool_call_chunk.id) {\n tool_call.id = tool_call_chunk.id;\n }\n if (tool_call_chunk.type) {\n tool_call.type = tool_call_chunk.type;\n }\n if (tool_call_chunk.function) {\n if (!tool_call.function) {\n tool_call.function = tool_call_chunk.function;\n } else {\n if (tool_call_chunk.function.name) {\n tool_call.function.name = tool_call_chunk.function.name;\n }\n if (tool_call_chunk.function.arguments) {\n if (!tool_call.function.arguments) {\n tool_call.function.arguments = \"\";\n }\n tool_call.function.arguments += tool_call_chunk.function.arguments;\n }\n }\n }\n receiver.update_content({\n content: from_openai_tool_call(tool_call),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n }\n }\n }\n if (!response || !response.choices?.[0]) {\n throw new Error(\"no data\");\n }\n receiver.response_raw({ ...response });\n\n const message = from_openai_completion(response, response.choices[0]);\n const usage = response?.usage ? from_openai_usage(response.usage) : {};\n for (const content of message.content) {\n receiver.complete_content({ content, message, usage });\n }\n receiver.complete_message({ message, usage, ...map_stop_reason(response) });\n } catch (error) {\n receiver.error(error);\n }\n }\n}\n\nfunction get_system_context(\n model: string,\n system: string | undefined,\n system_role: OpenAIProviderConfig[\"system_role\"] = \"auto\",\n): OpenAI.ChatCompletionMessageParam[] {\n if (!system) {\n return [];\n }\n const role = resolve_system_role(model, system_role);\n if (role === \"developer\") {\n return [\n {\n role: \"developer\",\n content: system,\n } as OpenAI.ChatCompletionDeveloperMessageParam,\n ];\n }\n return [\n {\n role: \"system\",\n content: system,\n } as OpenAI.ChatCompletionSystemMessageParam,\n ];\n}\n\nfunction resolve_system_role(\n model: string,\n system_role: OpenAIProviderConfig[\"system_role\"] = \"auto\",\n): \"system\" | \"developer\" {\n if (system_role === \"system\" || system_role === \"developer\") {\n return system_role;\n }\n if (model.startsWith(\"gpt\")) {\n return \"system\";\n }\n if (is_openai_reasoning_model(model)) {\n return \"developer\";\n }\n return \"system\";\n}\n\nfunction resolve_strict_tools(\n model: string,\n strict_tools: OpenAIProviderConfig[\"strict_tools\"] = \"auto\",\n): boolean {\n if (strict_tools === \"never\") {\n return false;\n }\n return model.startsWith(\"gpt\") || is_openai_reasoning_model(model);\n}\n\n// Best-effort match for OpenAI's o-series reasoning model ids (o1, o3,\n// o4-mini, etc.). The leading non-zero digit and end-of-string-or-hyphen\n// anchor narrow the pattern to what OpenAI actually ships. Two known\n// failure modes: a non-OpenAI vendor whose model id happens to follow the\n// same shape (e.g. `o2-fast`) gets matched as o-series, and a relay-style\n// id like `openai/o3` is NOT matched because the regex anchors at start\n// of string. Operators in either situation should set `system_role` and\n// `strict_tools` explicitly — the heuristic is a default, not a constraint.\nfunction is_openai_reasoning_model(model: string): boolean {\n return /^o[1-9]\\d*(-|$)/.test(model);\n}\n\nfunction to_openai_tool(tool: ToolDefinition, strict: boolean): OpenAI.Chat.ChatCompletionTool {\n function map_parameter_type(\n parameter: Prettify<ParameterType & { description?: string }>,\n ): OpenAI.FunctionParameters {\n switch (parameter.type) {\n case \"object\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n properties: Object.fromEntries(\n Object.entries(parameter.properties).map(([k, v]) => [k, map_parameter_type(v)]),\n ),\n additionalProperties: false,\n required: Object.entries(parameter.properties).map(([k]) => k),\n };\n case \"array\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n items: map_parameter_type(parameter.items),\n };\n default:\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description:\n parameter.default !== undefined\n ? parameter.description\n ? `${parameter.description} (default: ${parameter.default})`\n : `default: ${parameter.default}`\n : parameter.description,\n enum: \"enum\" in parameter ? parameter.enum : undefined,\n };\n }\n }\n return {\n type: \"function\",\n function: {\n name: tool.name,\n description: tool.description,\n parameters: map_parameter_type({\n type: \"object\",\n required: true,\n properties: Object.fromEntries(\n tool.parameters.map(({ name, ...parameter }) => [name, parameter]),\n ),\n }),\n ...(strict ? { strict: true } : {}),\n },\n };\n}\n\nfunction from_openai_completion(\n completion: OpenAI.Chat.Completions.ChatCompletionChunk,\n choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice,\n) {\n let contents: Content[] = [];\n const thinking = from_openai_thinking(choice.delta);\n if (thinking) {\n contents = [...contents, thinking];\n }\n const delta_record = choice.delta as Record<string, unknown>;\n for (const key of Object.keys(choice.delta)) {\n if (key === \"role\") {\n continue;\n }\n if (is_openai_reasoning_delta_key(key)) {\n continue;\n }\n if (key === \"content\") {\n if (choice.delta.content) {\n contents = [...contents, from_openai_content(choice.delta)];\n }\n continue;\n }\n if (key === \"refusal\") {\n if (choice.delta.refusal) {\n contents = [...contents, from_openai_refusal(choice.delta)];\n }\n continue;\n }\n if (key === \"tool_calls\") {\n if (choice.delta.tool_calls) {\n contents = [...contents, ...choice.delta.tool_calls.map((t) => from_openai_tool_call(t))];\n }\n continue;\n }\n if (delta_record[key] !== undefined && delta_record[key] !== null) {\n const data = delta_record[key];\n contents = [\n ...contents,\n {\n type: \"raw\",\n model_kind: \"openai\",\n data: JSON.stringify({ [key]: data }),\n },\n ];\n }\n }\n return {\n id: completion.id,\n timestamp: completion.created,\n role: map_role(choice),\n content: contents,\n } as AssistantMessage;\n}\n\nfunction get_openai_reasoning_delta(delta: OpenAICompletionDelta) {\n const record = delta as Partial<Record<OpenAIReasoningDeltaKey, unknown>>;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n const thought = record[key];\n if (typeof thought === \"string\" && thought.length > 0) {\n return { key, thought };\n }\n }\n return undefined;\n}\n\nfunction from_openai_thinking(content: OpenAICompletionDelta) {\n const reasoning = get_openai_reasoning_delta(content);\n if (!reasoning) {\n return undefined;\n }\n const extended = get_openai_delta_extended(content);\n return {\n type: \"thinking\",\n thought: reasoning.thought,\n extended: {\n ...extended,\n openai_reasoning_field: reasoning.key,\n },\n } as Content;\n}\n\nfunction is_openai_reasoning_delta_key(key: string): key is OpenAIReasoningDeltaKey {\n return OPENAI_REASONING_DELTA_KEYS.includes(key as OpenAIReasoningDeltaKey);\n}\n\nfunction from_openai_refusal(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.refusal,\n extended: {\n ...get_openai_delta_extended(content),\n openai_refusal: true,\n },\n } as Content;\n}\n\nfunction from_openai_content(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.content,\n extended: get_openai_delta_extended(content),\n } as Content;\n}\n\nfunction get_openai_delta_extended(content: OpenAICompletionDelta) {\n const extended = { ...(content as Record<string, unknown>) };\n delete extended.content;\n delete extended.tool_calls;\n delete extended.function_call;\n delete extended.refusal;\n delete extended.role;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n delete extended[key];\n }\n return extended;\n}\n\nfunction from_openai_tool_call(\n tool_call: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta.ToolCall,\n) {\n const { id: tool_request_id, function: fn, type: _, index: __, ...extended } = tool_call;\n let params: unknown;\n try {\n params = JSON.parse(fn?.arguments ?? \"{}\");\n } catch {\n params = fn?.arguments;\n }\n return {\n tool_request_id,\n type: \"tool\",\n tool: fn?.name,\n params,\n extended,\n } as ToolContent;\n}\n\nfunction to_openai_messages(message: Message) {\n if (message.role === \"assistant\") {\n return [to_openai_assistant_message(message)];\n }\n // Partition content so tool_result blocks come before non-tool_result blocks.\n // OpenAI requires all tool-role messages immediately after the assistant message\n // containing the corresponding tool_calls; interleaving user messages between\n // tool messages causes a validation error.\n const tool_result_content = message.content.filter((c) => c.type === \"tool_result\");\n const other_content = message.content.filter((c) => c.type !== \"tool_result\");\n const ordered_content = [...tool_result_content, ...other_content];\n\n const results: OpenAI.ChatCompletionMessageParam[] = [];\n let result: OpenAI.ChatCompletionMessageParam | undefined;\n for (const content of ordered_content) {\n if (content.type === \"text\") {\n if (!result) {\n result = {\n role: \"user\",\n content: content.text,\n };\n } else if (result.role === \"tool\") {\n results.push(result);\n result = {\n role: \"user\",\n content: content.text,\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = [\n {\n type: \"text\",\n text: content.text,\n },\n ];\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n } else if (content.type === \"tool_result\") {\n if (!result) {\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else if (result.role !== \"tool\" || result.tool_call_id !== content.tool_request_id) {\n results.push(result);\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n result.content.push({\n type: \"text\",\n text: JSON.stringify(content.result),\n });\n }\n } else if (content.type === \"raw\") {\n result = {\n ...(result ?? {}),\n ...JSON.parse(content.data),\n };\n }\n }\n if (result) {\n results.push(result);\n }\n return results;\n}\n\nfunction to_openai_assistant_message(message: AssistantMessage) {\n let result: OpenAI.ChatCompletionAssistantMessageParam = {\n role: \"assistant\",\n };\n for (const content of message.content) {\n switch (content.type) {\n case \"text\":\n if (content.extended && content.extended.openai_refusal === true) {\n result.refusal = content.text;\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.text;\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n break;\n case \"tool\":\n if (!result.tool_calls) {\n result.tool_calls = [];\n }\n result.tool_calls.push({\n id: content.tool_request_id,\n type: \"function\",\n function: {\n name: content.tool,\n arguments: JSON.stringify(content.params),\n },\n });\n break;\n case \"raw\":\n if (content.model_kind !== \"openai\") {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.data;\n } else {\n result.content.push({\n type: \"text\",\n text: content.data,\n });\n }\n break;\n }\n result = {\n ...result,\n ...JSON.parse(content.data),\n };\n break;\n case \"thinking\":\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.thought;\n } else {\n result.content.push({\n type: \"text\",\n text: content.thought,\n });\n }\n break;\n default:\n throw new Error(\"unexpected content type\");\n }\n }\n return result;\n}\n\nfunction from_openai_usage(usage: OpenAI.CompletionUsage | null | undefined) {\n return {\n input_tokens: usage?.prompt_tokens,\n output_tokens: usage?.completion_tokens,\n } as Usage;\n}\n\nfunction map_stop_reason(\n completion: OpenAI.ChatCompletionChunk,\n): Pick<CompletionResponseData, \"stop_reason\" | \"stop_details\"> {\n for (const choice of completion.choices) {\n switch (choice.finish_reason) {\n case \"content_filter\":\n return {\n stop_reason: \"error\",\n stop_details: choice.finish_reason,\n };\n case \"function_call\":\n return {\n stop_reason: \"tool_use\",\n };\n case \"length\":\n return {\n stop_reason: \"max_tokens\",\n };\n case \"stop\":\n return {\n stop_reason: \"end_turn\",\n };\n case \"tool_calls\":\n return {\n stop_reason: \"tool_use\",\n };\n default:\n return {\n stop_reason: \"other\",\n stop_details: `${choice.finish_reason}`,\n };\n }\n }\n return {\n stop_reason: \"other\",\n };\n}\n\nfunction map_role(choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice) {\n switch (choice.delta.role) {\n case \"user\":\n case \"developer\":\n case \"system\":\n return \"user\";\n case \"assistant\":\n case \"tool\":\n return \"assistant\";\n default:\n throw new Error(\"invalid role\");\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqBA,IAAM,8BAA8B,CAAC,aAAa,qBAAqB,UAAU;AAqE1E,IAAM,iBAAN,MAAM,gBAAwC;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAST,YACE,KACA,QACA,uBAAqD,CAAC,GACtD;AACA,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,uBAAuB;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,gBACJ,SACA,UACA,cACe;AACf,UAAM,EAAE,OAAO,YAAY,aAAa,cAAc,GAAG,WAAW,IAAI,KAAK;AAC7E,UAAM,cAAc,qBAAqB,OAAO,YAAY;AAC5D,UAAM,SAAqD;AAAA,MACzD,GAAG;AAAA,MACH;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA,GAAI,QAAQ,MAAM,SAAS,IACvB;AAAA,QACE,aAAa;AAAA,QACb,OAAO,QAAQ,MAAM,IAAI,CAAC,MAAM,eAAe,GAAG,WAAW,CAAC;AAAA,MAChE,IACA,CAAC;AAAA,MACL,UAAU;AAAA,QACR,GAAG,mBAAmB,OAAO,QAAQ,QAAQ,WAAW;AAAA,QACxD,GAAG,QAAQ,SAAS,QAAQ,CAAC,MAAM,mBAAmB,CAAC,CAAC;AAAA,MAC1D;AA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eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF;AACE,eAAO;AAAA,UACL,aAAa;AAAA,UACb,cAAc,GAAG,OAAO,aAAa;AAAA,QACvC;AAAA,IACJ;AAAA,EACF;AACA,SAAO;AAAA,IACL,aAAa;AAAA,EACf;AACF;AAEA,SAAS,SAAS,QAA4D;AAC5E,UAAQ,OAAO,MAAM,MAAM;AAAA,IACzB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT;AACE,YAAM,IAAI,MAAM,cAAc;AAAA,EAClC;AACF;","names":["message","rest"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -9,14 +9,58 @@ interface OpenAIProviderConfig extends Record<string, unknown> {
|
|
|
9
9
|
model: string;
|
|
10
10
|
/** The maximum number of tokens to generate in the response. */
|
|
11
11
|
max_tokens?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Which role to use for the system prompt.
|
|
14
|
+
*
|
|
15
|
+
* The OpenAI Chat Completions spec allows `system` (legacy / most providers)
|
|
16
|
+
* and `developer` (introduced for o-series reasoning models). Most
|
|
17
|
+
* OpenAI-compatible endpoints (DeepSeek, OpenRouter relays, Anthropic-via-
|
|
18
|
+
* compat, self-hosted gateways, etc.) only accept `system` and reject
|
|
19
|
+
* `developer` with a 400.
|
|
20
|
+
*
|
|
21
|
+
* - `"auto"` (default): use the built-in heuristic — `gpt*` → `"system"`,
|
|
22
|
+
* o-series (`o1`, `o3`, `o4`, ...) → `"developer"`, anything else →
|
|
23
|
+
* `"system"` (the broadly-compatible default).
|
|
24
|
+
* - `"system"` / `"developer"`: force the role regardless of model id.
|
|
25
|
+
*
|
|
26
|
+
* The heuristic matches on the bare model id and does not understand
|
|
27
|
+
* relay-style prefixes (`openai/o3`, `anthropic/claude-3-5-sonnet`,
|
|
28
|
+
* etc.) — set this explicitly when routing OpenAI models through a
|
|
29
|
+
* relay that uses `vendor/model` ids.
|
|
30
|
+
*/
|
|
31
|
+
system_role?: "auto" | "system" | "developer";
|
|
32
|
+
/**
|
|
33
|
+
* Whether to emit OpenAI's strict structured-output flag on tool
|
|
34
|
+
* definitions (`function.strict: true`).
|
|
35
|
+
*
|
|
36
|
+
* `strict` is an OpenAI-specific extension that constrains tool arguments
|
|
37
|
+
* to the supplied JSON Schema. Most non-OpenAI endpoints either ignore the
|
|
38
|
+
* field or reject it.
|
|
39
|
+
*
|
|
40
|
+
* - `"auto"` (default): emit `strict: true` when the model id matches the
|
|
41
|
+
* built-in OpenAI heuristic — `gpt*` or o-series (`o1`, `o3`, `o4`, ...).
|
|
42
|
+
* Any other model id (deepseek, etc.) gets tool defs without `strict`.
|
|
43
|
+
* The heuristic matches on the bare model id; relay-style prefixes
|
|
44
|
+
* (`openai/gpt-4o`) are NOT recognized — set this explicitly when
|
|
45
|
+
* routing OpenAI models through a relay.
|
|
46
|
+
* - `"never"`: never emit `strict`.
|
|
47
|
+
*/
|
|
48
|
+
strict_tools?: "auto" | "never";
|
|
12
49
|
}
|
|
13
50
|
/**
|
|
14
51
|
* Model provider implementation for OpenAI's chat completion models.
|
|
15
52
|
*
|
|
16
|
-
* This provider wraps the OpenAI SDK to provide streaming completions with
|
|
17
|
-
* for tool use and function calling. It handles message formatting,
|
|
18
|
-
* and usage tracking according to the ModelProvider
|
|
19
|
-
*
|
|
53
|
+
* This provider wraps the OpenAI SDK to provide streaming completions with
|
|
54
|
+
* support for tool use and function calling. It handles message formatting,
|
|
55
|
+
* content streaming, and usage tracking according to the ModelProvider
|
|
56
|
+
* interface.
|
|
57
|
+
*
|
|
58
|
+
* Works against OpenAI directly as well as OpenAI-compatible endpoints
|
|
59
|
+
* (DeepSeek, OpenRouter, self-hosted gateways, etc.). The default behaviour
|
|
60
|
+
* picks `system` vs. `developer` for the system prompt and decides whether
|
|
61
|
+
* to emit OpenAI's `strict` flag on tool defs based on the model id; both
|
|
62
|
+
* can be overridden through `OpenAIProviderConfig.system_role` and
|
|
63
|
+
* `OpenAIProviderConfig.strict_tools` for endpoints whose behaviour differs.
|
|
20
64
|
*/
|
|
21
65
|
declare class OpenAIProvider implements ModelProvider {
|
|
22
66
|
#private;
|
package/dist/index.d.ts
CHANGED
|
@@ -9,14 +9,58 @@ interface OpenAIProviderConfig extends Record<string, unknown> {
|
|
|
9
9
|
model: string;
|
|
10
10
|
/** The maximum number of tokens to generate in the response. */
|
|
11
11
|
max_tokens?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Which role to use for the system prompt.
|
|
14
|
+
*
|
|
15
|
+
* The OpenAI Chat Completions spec allows `system` (legacy / most providers)
|
|
16
|
+
* and `developer` (introduced for o-series reasoning models). Most
|
|
17
|
+
* OpenAI-compatible endpoints (DeepSeek, OpenRouter relays, Anthropic-via-
|
|
18
|
+
* compat, self-hosted gateways, etc.) only accept `system` and reject
|
|
19
|
+
* `developer` with a 400.
|
|
20
|
+
*
|
|
21
|
+
* - `"auto"` (default): use the built-in heuristic — `gpt*` → `"system"`,
|
|
22
|
+
* o-series (`o1`, `o3`, `o4`, ...) → `"developer"`, anything else →
|
|
23
|
+
* `"system"` (the broadly-compatible default).
|
|
24
|
+
* - `"system"` / `"developer"`: force the role regardless of model id.
|
|
25
|
+
*
|
|
26
|
+
* The heuristic matches on the bare model id and does not understand
|
|
27
|
+
* relay-style prefixes (`openai/o3`, `anthropic/claude-3-5-sonnet`,
|
|
28
|
+
* etc.) — set this explicitly when routing OpenAI models through a
|
|
29
|
+
* relay that uses `vendor/model` ids.
|
|
30
|
+
*/
|
|
31
|
+
system_role?: "auto" | "system" | "developer";
|
|
32
|
+
/**
|
|
33
|
+
* Whether to emit OpenAI's strict structured-output flag on tool
|
|
34
|
+
* definitions (`function.strict: true`).
|
|
35
|
+
*
|
|
36
|
+
* `strict` is an OpenAI-specific extension that constrains tool arguments
|
|
37
|
+
* to the supplied JSON Schema. Most non-OpenAI endpoints either ignore the
|
|
38
|
+
* field or reject it.
|
|
39
|
+
*
|
|
40
|
+
* - `"auto"` (default): emit `strict: true` when the model id matches the
|
|
41
|
+
* built-in OpenAI heuristic — `gpt*` or o-series (`o1`, `o3`, `o4`, ...).
|
|
42
|
+
* Any other model id (deepseek, etc.) gets tool defs without `strict`.
|
|
43
|
+
* The heuristic matches on the bare model id; relay-style prefixes
|
|
44
|
+
* (`openai/gpt-4o`) are NOT recognized — set this explicitly when
|
|
45
|
+
* routing OpenAI models through a relay.
|
|
46
|
+
* - `"never"`: never emit `strict`.
|
|
47
|
+
*/
|
|
48
|
+
strict_tools?: "auto" | "never";
|
|
12
49
|
}
|
|
13
50
|
/**
|
|
14
51
|
* Model provider implementation for OpenAI's chat completion models.
|
|
15
52
|
*
|
|
16
|
-
* This provider wraps the OpenAI SDK to provide streaming completions with
|
|
17
|
-
* for tool use and function calling. It handles message formatting,
|
|
18
|
-
* and usage tracking according to the ModelProvider
|
|
19
|
-
*
|
|
53
|
+
* This provider wraps the OpenAI SDK to provide streaming completions with
|
|
54
|
+
* support for tool use and function calling. It handles message formatting,
|
|
55
|
+
* content streaming, and usage tracking according to the ModelProvider
|
|
56
|
+
* interface.
|
|
57
|
+
*
|
|
58
|
+
* Works against OpenAI directly as well as OpenAI-compatible endpoints
|
|
59
|
+
* (DeepSeek, OpenRouter, self-hosted gateways, etc.). The default behaviour
|
|
60
|
+
* picks `system` vs. `developer` for the system prompt and decides whether
|
|
61
|
+
* to emit OpenAI's `strict` flag on tool defs based on the model id; both
|
|
62
|
+
* can be overridden through `OpenAIProviderConfig.system_role` and
|
|
63
|
+
* `OpenAIProviderConfig.strict_tools` for endpoints whose behaviour differs.
|
|
20
64
|
*/
|
|
21
65
|
declare class OpenAIProvider implements ModelProvider {
|
|
22
66
|
#private;
|
package/dist/index.js
CHANGED
|
@@ -25,7 +25,8 @@ var OpenAIProvider = class _OpenAIProvider {
|
|
|
25
25
|
* @returns A promise that resolves when the request completes.
|
|
26
26
|
*/
|
|
27
27
|
async execute_request(request, receiver, cancellation) {
|
|
28
|
-
const { model, max_tokens, ...api_extras } = this.#config;
|
|
28
|
+
const { model, max_tokens, system_role, strict_tools, ...api_extras } = this.#config;
|
|
29
|
+
const emit_strict = resolve_strict_tools(model, strict_tools);
|
|
29
30
|
const params = {
|
|
30
31
|
...api_extras,
|
|
31
32
|
model,
|
|
@@ -33,10 +34,10 @@ var OpenAIProvider = class _OpenAIProvider {
|
|
|
33
34
|
max_tokens,
|
|
34
35
|
...request.tools.length > 0 ? {
|
|
35
36
|
tool_choice: "auto",
|
|
36
|
-
tools: request.tools.map((t) => to_openai_tool(t))
|
|
37
|
+
tools: request.tools.map((t) => to_openai_tool(t, emit_strict))
|
|
37
38
|
} : {},
|
|
38
39
|
messages: [
|
|
39
|
-
...get_system_context(model, request.system),
|
|
40
|
+
...get_system_context(model, request.system, system_role),
|
|
40
41
|
...request.messages.flatMap((m) => to_openai_messages(m))
|
|
41
42
|
],
|
|
42
43
|
stream_options: {
|
|
@@ -211,26 +212,48 @@ var OpenAIProvider = class _OpenAIProvider {
|
|
|
211
212
|
}
|
|
212
213
|
}
|
|
213
214
|
};
|
|
214
|
-
function get_system_context(model, system) {
|
|
215
|
+
function get_system_context(model, system, system_role = "auto") {
|
|
215
216
|
if (!system) {
|
|
216
217
|
return [];
|
|
217
218
|
}
|
|
218
|
-
|
|
219
|
+
const role = resolve_system_role(model, system_role);
|
|
220
|
+
if (role === "developer") {
|
|
219
221
|
return [
|
|
220
222
|
{
|
|
221
|
-
role: "
|
|
223
|
+
role: "developer",
|
|
222
224
|
content: system
|
|
223
225
|
}
|
|
224
226
|
];
|
|
225
227
|
}
|
|
226
228
|
return [
|
|
227
229
|
{
|
|
228
|
-
role: "
|
|
230
|
+
role: "system",
|
|
229
231
|
content: system
|
|
230
232
|
}
|
|
231
233
|
];
|
|
232
234
|
}
|
|
233
|
-
function
|
|
235
|
+
function resolve_system_role(model, system_role = "auto") {
|
|
236
|
+
if (system_role === "system" || system_role === "developer") {
|
|
237
|
+
return system_role;
|
|
238
|
+
}
|
|
239
|
+
if (model.startsWith("gpt")) {
|
|
240
|
+
return "system";
|
|
241
|
+
}
|
|
242
|
+
if (is_openai_reasoning_model(model)) {
|
|
243
|
+
return "developer";
|
|
244
|
+
}
|
|
245
|
+
return "system";
|
|
246
|
+
}
|
|
247
|
+
function resolve_strict_tools(model, strict_tools = "auto") {
|
|
248
|
+
if (strict_tools === "never") {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
return model.startsWith("gpt") || is_openai_reasoning_model(model);
|
|
252
|
+
}
|
|
253
|
+
function is_openai_reasoning_model(model) {
|
|
254
|
+
return /^o[1-9]\d*(-|$)/.test(model);
|
|
255
|
+
}
|
|
256
|
+
function to_openai_tool(tool, strict) {
|
|
234
257
|
function map_parameter_type(parameter) {
|
|
235
258
|
switch (parameter.type) {
|
|
236
259
|
case "object":
|
|
@@ -269,7 +292,7 @@ function to_openai_tool(tool) {
|
|
|
269
292
|
tool.parameters.map(({ name, ...parameter }) => [name, parameter])
|
|
270
293
|
)
|
|
271
294
|
}),
|
|
272
|
-
strict: true
|
|
295
|
+
...strict ? { strict: true } : {}
|
|
273
296
|
}
|
|
274
297
|
};
|
|
275
298
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/openai-provider.ts"],"sourcesContent":["import { OpenAI } from \"openai\";\n\nimport type {\n AssistantContent,\n AssistantMessage,\n CancellationToken,\n CompletionResponseData,\n Content,\n Message,\n ModelProvider,\n ModelRequest,\n ParameterType,\n ProviderContextTransformer,\n StreamReceiver,\n ToolContent,\n ToolDefinition,\n Usage,\n} from \"@simulacra-ai/core\";\n\ntype Prettify<T> = { [K in keyof T]: T[K] } & {};\n\nconst OPENAI_REASONING_DELTA_KEYS = [\"reasoning\", \"reasoning_content\", \"thinking\"] as const;\n\ntype OpenAIReasoningDeltaKey = (typeof OPENAI_REASONING_DELTA_KEYS)[number];\ntype OpenAICompletionDelta = OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta;\ntype OpenAIReasoningDelta = OpenAICompletionDelta &\n Partial<Record<OpenAIReasoningDeltaKey, string>>;\n\n/**\n * Configuration options for the OpenAI provider.\n */\nexport interface OpenAIProviderConfig extends Record<string, unknown> {\n /** The model identifier to use (e.g., \"gpt-4\", \"o1-preview\"). */\n model: string;\n /** The maximum number of tokens to generate in the response. */\n max_tokens?: number;\n}\n\n/**\n * Model provider implementation for OpenAI's chat completion models.\n *\n * This provider wraps the OpenAI SDK to provide streaming completions with support\n * for tool use and function calling. It handles message formatting, content streaming,\n * and usage tracking according to the ModelProvider interface. Supports both GPT models\n * (using system messages) and O-series models (using developer messages).\n */\nexport class OpenAIProvider implements ModelProvider {\n readonly #sdk: OpenAI;\n readonly #config: OpenAIProviderConfig;\n readonly context_transformers: ProviderContextTransformer[];\n\n /**\n * Creates a new OpenAI provider instance.\n *\n * @param sdk - The initialized OpenAI SDK client.\n * @param config - Configuration options for the provider.\n * @param context_transformers - Provider-level context transformers.\n */\n constructor(\n sdk: OpenAI,\n config: OpenAIProviderConfig,\n context_transformers: ProviderContextTransformer[] = [],\n ) {\n this.#sdk = sdk;\n this.#config = config;\n this.context_transformers = context_transformers;\n }\n\n /**\n * Executes a model request and streams the response through the provided receiver.\n *\n * @param request - The request containing messages, tools, and system prompt.\n * @param receiver - The receiver that handles streaming events.\n * @param cancellation - Token to signal cancellation of the request.\n * @returns A promise that resolves when the request completes.\n */\n async execute_request(\n request: ModelRequest,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ): Promise<void> {\n const { model, max_tokens, ...api_extras } = this.#config;\n const params: OpenAI.ChatCompletionCreateParamsStreaming = {\n ...api_extras,\n model,\n stream: true,\n max_tokens,\n ...(request.tools.length > 0\n ? {\n tool_choice: \"auto\",\n tools: request.tools.map((t) => to_openai_tool(t)),\n }\n : {}),\n messages: [\n ...get_system_context(model, request.system),\n ...request.messages.flatMap((m) => to_openai_messages(m)),\n ],\n stream_options: {\n include_usage: true,\n },\n };\n\n receiver.before_request({ params });\n receiver.request_raw(params);\n\n const stream = await this.#sdk.chat.completions.create(params);\n\n // Intentionally not awaited. Streaming is event-driven through the receiver.\n // The policy wraps only connection establishment; chunk processing flows\n // asynchronously via StreamListener events back to the conversation.\n this.#stream_response(stream, receiver, cancellation);\n }\n\n /**\n * Creates a clone of this provider with the same configuration.\n *\n * @returns A new provider instance with identical configuration.\n */\n clone(): ModelProvider {\n return new OpenAIProvider(this.#sdk, this.#config, this.context_transformers);\n }\n\n async #stream_response(\n stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ) {\n try {\n let response: OpenAI.Chat.Completions.ChatCompletionChunk | undefined;\n for await (const response_chunk of stream) {\n if (cancellation.is_cancellation_requested) {\n receiver.cancel();\n return;\n }\n receiver.stream_raw(response_chunk);\n\n const { choices: choices_chunk, ...rest } = response_chunk;\n response = {\n ...response,\n ...rest,\n choices: response?.choices ?? [],\n };\n\n for (const choice_chunk of choices_chunk) {\n if (!response.choices[choice_chunk.index]) {\n response.choices[choice_chunk.index] = choice_chunk;\n const message = from_openai_completion(response_chunk, choice_chunk);\n for (const content of message.content) {\n receiver.start_content({ content, message, usage: {} });\n }\n receiver.start_message({ message, usage: {} });\n continue;\n }\n\n const { delta: delta_chunk, ...rest } = choice_chunk;\n const choice = (response.choices[choice_chunk.index] = {\n ...response.choices[choice_chunk.index],\n ...rest,\n delta: {\n ...response.choices[choice_chunk.index]?.delta,\n },\n });\n\n if (delta_chunk.role) {\n choice.delta.role = delta_chunk.role;\n }\n if (delta_chunk.refusal) {\n if (!choice.delta.refusal) {\n choice.delta.refusal = \"\";\n }\n choice.delta.refusal += delta_chunk.refusal;\n }\n const reasoning_delta = get_openai_reasoning_delta(delta_chunk);\n if (reasoning_delta) {\n const choice_delta = choice.delta as OpenAIReasoningDelta;\n const existing = choice_delta[reasoning_delta.key];\n if (!existing) {\n choice_delta[reasoning_delta.key] = reasoning_delta.thought;\n receiver.start_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice_delta[reasoning_delta.key] = existing + reasoning_delta.thought;\n receiver.update_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.content) {\n if (!choice.delta.content) {\n choice.delta.content = delta_chunk.content;\n receiver.start_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice.delta.content += delta_chunk.content;\n receiver.update_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls) {\n choice.delta.tool_calls = [];\n }\n for (const tool_call_chunk of delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls[tool_call_chunk.index]) {\n choice.delta.tool_calls[tool_call_chunk.index] = tool_call_chunk;\n receiver.start_content({\n content: from_openai_tool_call(tool_call_chunk),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n const tool_call = choice.delta.tool_calls[tool_call_chunk.index];\n\n if (tool_call_chunk.id) {\n tool_call.id = tool_call_chunk.id;\n }\n if (tool_call_chunk.type) {\n tool_call.type = tool_call_chunk.type;\n }\n if (tool_call_chunk.function) {\n if (!tool_call.function) {\n tool_call.function = tool_call_chunk.function;\n } else {\n if (tool_call_chunk.function.name) {\n tool_call.function.name = tool_call_chunk.function.name;\n }\n if (tool_call_chunk.function.arguments) {\n if (!tool_call.function.arguments) {\n tool_call.function.arguments = \"\";\n }\n tool_call.function.arguments += tool_call_chunk.function.arguments;\n }\n }\n }\n receiver.update_content({\n content: from_openai_tool_call(tool_call),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n }\n }\n }\n if (!response || !response.choices?.[0]) {\n throw new Error(\"no data\");\n }\n receiver.response_raw({ ...response });\n\n const message = from_openai_completion(response, response.choices[0]);\n const usage = response?.usage ? from_openai_usage(response.usage) : {};\n for (const content of message.content) {\n receiver.complete_content({ content, message, usage });\n }\n receiver.complete_message({ message, usage, ...map_stop_reason(response) });\n } catch (error) {\n receiver.error(error);\n }\n }\n}\n\nfunction get_system_context(model: string, system?: string): OpenAI.ChatCompletionMessageParam[] {\n if (!system) {\n return [];\n }\n if (model.startsWith(\"gpt\")) {\n return [\n {\n role: \"system\",\n content: system,\n } as OpenAI.ChatCompletionSystemMessageParam,\n ];\n }\n return [\n {\n role: \"developer\",\n content: system,\n } as OpenAI.ChatCompletionDeveloperMessageParam,\n ];\n}\n\nfunction to_openai_tool(tool: ToolDefinition): OpenAI.Chat.ChatCompletionTool {\n function map_parameter_type(\n parameter: Prettify<ParameterType & { description?: string }>,\n ): OpenAI.FunctionParameters {\n switch (parameter.type) {\n case \"object\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n properties: Object.fromEntries(\n Object.entries(parameter.properties).map(([k, v]) => [k, map_parameter_type(v)]),\n ),\n additionalProperties: false,\n required: Object.entries(parameter.properties).map(([k]) => k),\n };\n case \"array\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n items: map_parameter_type(parameter.items),\n };\n default:\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description:\n parameter.default !== undefined\n ? parameter.description\n ? `${parameter.description} (default: ${parameter.default})`\n : `default: ${parameter.default}`\n : parameter.description,\n enum: \"enum\" in parameter ? parameter.enum : undefined,\n };\n }\n }\n return {\n type: \"function\",\n function: {\n name: tool.name,\n description: tool.description,\n parameters: map_parameter_type({\n type: \"object\",\n required: true,\n properties: Object.fromEntries(\n tool.parameters.map(({ name, ...parameter }) => [name, parameter]),\n ),\n }),\n strict: true,\n },\n };\n}\n\nfunction from_openai_completion(\n completion: OpenAI.Chat.Completions.ChatCompletionChunk,\n choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice,\n) {\n let contents: Content[] = [];\n const thinking = from_openai_thinking(choice.delta);\n if (thinking) {\n contents = [...contents, thinking];\n }\n const delta_record = choice.delta as Record<string, unknown>;\n for (const key of Object.keys(choice.delta)) {\n if (key === \"role\") {\n continue;\n }\n if (is_openai_reasoning_delta_key(key)) {\n continue;\n }\n if (key === \"content\") {\n if (choice.delta.content) {\n contents = [...contents, from_openai_content(choice.delta)];\n }\n continue;\n }\n if (key === \"refusal\") {\n if (choice.delta.refusal) {\n contents = [...contents, from_openai_refusal(choice.delta)];\n }\n continue;\n }\n if (key === \"tool_calls\") {\n if (choice.delta.tool_calls) {\n contents = [...contents, ...choice.delta.tool_calls.map((t) => from_openai_tool_call(t))];\n }\n continue;\n }\n if (delta_record[key] !== undefined && delta_record[key] !== null) {\n const data = delta_record[key];\n contents = [\n ...contents,\n {\n type: \"raw\",\n model_kind: \"openai\",\n data: JSON.stringify({ [key]: data }),\n },\n ];\n }\n }\n return {\n id: completion.id,\n timestamp: completion.created,\n role: map_role(choice),\n content: contents,\n } as AssistantMessage;\n}\n\nfunction get_openai_reasoning_delta(delta: OpenAICompletionDelta) {\n const record = delta as Partial<Record<OpenAIReasoningDeltaKey, unknown>>;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n const thought = record[key];\n if (typeof thought === \"string\" && thought.length > 0) {\n return { key, thought };\n }\n }\n return undefined;\n}\n\nfunction from_openai_thinking(content: OpenAICompletionDelta) {\n const reasoning = get_openai_reasoning_delta(content);\n if (!reasoning) {\n return undefined;\n }\n const extended = get_openai_delta_extended(content);\n return {\n type: \"thinking\",\n thought: reasoning.thought,\n extended: {\n ...extended,\n openai_reasoning_field: reasoning.key,\n },\n } as Content;\n}\n\nfunction is_openai_reasoning_delta_key(key: string): key is OpenAIReasoningDeltaKey {\n return OPENAI_REASONING_DELTA_KEYS.includes(key as OpenAIReasoningDeltaKey);\n}\n\nfunction from_openai_refusal(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.refusal,\n extended: {\n ...get_openai_delta_extended(content),\n openai_refusal: true,\n },\n } as Content;\n}\n\nfunction from_openai_content(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.content,\n extended: get_openai_delta_extended(content),\n } as Content;\n}\n\nfunction get_openai_delta_extended(content: OpenAICompletionDelta) {\n const extended = { ...(content as Record<string, unknown>) };\n delete extended.content;\n delete extended.tool_calls;\n delete extended.function_call;\n delete extended.refusal;\n delete extended.role;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n delete extended[key];\n }\n return extended;\n}\n\nfunction from_openai_tool_call(\n tool_call: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta.ToolCall,\n) {\n const { id: tool_request_id, function: fn, type: _, index: __, ...extended } = tool_call;\n let params: unknown;\n try {\n params = JSON.parse(fn?.arguments ?? \"{}\");\n } catch {\n params = fn?.arguments;\n }\n return {\n tool_request_id,\n type: \"tool\",\n tool: fn?.name,\n params,\n extended,\n } as ToolContent;\n}\n\nfunction to_openai_messages(message: Message) {\n if (message.role === \"assistant\") {\n return [to_openai_assistant_message(message)];\n }\n // Partition content so tool_result blocks come before non-tool_result blocks.\n // OpenAI requires all tool-role messages immediately after the assistant message\n // containing the corresponding tool_calls; interleaving user messages between\n // tool messages causes a validation error.\n const tool_result_content = message.content.filter((c) => c.type === \"tool_result\");\n const other_content = message.content.filter((c) => c.type !== \"tool_result\");\n const ordered_content = [...tool_result_content, ...other_content];\n\n const results: OpenAI.ChatCompletionMessageParam[] = [];\n let result: OpenAI.ChatCompletionMessageParam | undefined;\n for (const content of ordered_content) {\n if (content.type === \"text\") {\n if (!result) {\n result = {\n role: \"user\",\n content: content.text,\n };\n } else if (result.role === \"tool\") {\n results.push(result);\n result = {\n role: \"user\",\n content: content.text,\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = [\n {\n type: \"text\",\n text: content.text,\n },\n ];\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n } else if (content.type === \"tool_result\") {\n if (!result) {\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else if (result.role !== \"tool\" || result.tool_call_id !== content.tool_request_id) {\n results.push(result);\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n result.content.push({\n type: \"text\",\n text: JSON.stringify(content.result),\n });\n }\n } else if (content.type === \"raw\") {\n result = {\n ...(result ?? {}),\n ...JSON.parse(content.data),\n };\n }\n }\n if (result) {\n results.push(result);\n }\n return results;\n}\n\nfunction to_openai_assistant_message(message: AssistantMessage) {\n let result: OpenAI.ChatCompletionAssistantMessageParam = {\n role: \"assistant\",\n };\n for (const content of message.content) {\n switch (content.type) {\n case \"text\":\n if (content.extended && content.extended.openai_refusal === true) {\n result.refusal = content.text;\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.text;\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n break;\n case \"tool\":\n if (!result.tool_calls) {\n result.tool_calls = [];\n }\n result.tool_calls.push({\n id: content.tool_request_id,\n type: \"function\",\n function: {\n name: content.tool,\n arguments: JSON.stringify(content.params),\n },\n });\n break;\n case \"raw\":\n if (content.model_kind !== \"openai\") {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.data;\n } else {\n result.content.push({\n type: \"text\",\n text: content.data,\n });\n }\n break;\n }\n result = {\n ...result,\n ...JSON.parse(content.data),\n };\n break;\n case \"thinking\":\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.thought;\n } else {\n result.content.push({\n type: \"text\",\n text: content.thought,\n });\n }\n break;\n default:\n throw new Error(\"unexpected content type\");\n }\n }\n return result;\n}\n\nfunction from_openai_usage(usage: OpenAI.CompletionUsage | null | undefined) {\n return {\n input_tokens: usage?.prompt_tokens,\n output_tokens: usage?.completion_tokens,\n } as Usage;\n}\n\nfunction map_stop_reason(\n completion: OpenAI.ChatCompletionChunk,\n): Pick<CompletionResponseData, \"stop_reason\" | \"stop_details\"> {\n for (const choice of completion.choices) {\n switch (choice.finish_reason) {\n case \"content_filter\":\n return {\n stop_reason: \"error\",\n stop_details: choice.finish_reason,\n };\n case \"function_call\":\n return {\n stop_reason: \"tool_use\",\n };\n case \"length\":\n return {\n stop_reason: \"max_tokens\",\n };\n case \"stop\":\n return {\n stop_reason: \"end_turn\",\n };\n case \"tool_calls\":\n return {\n stop_reason: \"tool_use\",\n };\n default:\n return {\n stop_reason: \"other\",\n stop_details: `${choice.finish_reason}`,\n };\n }\n }\n return {\n stop_reason: \"other\",\n };\n}\n\nfunction map_role(choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice) {\n switch (choice.delta.role) {\n case \"user\":\n case \"developer\":\n case \"system\":\n return \"user\";\n case \"assistant\":\n case \"tool\":\n return \"assistant\";\n default:\n throw new Error(\"invalid role\");\n }\n}\n"],"mappings":";AAqBA,IAAM,8BAA8B,CAAC,aAAa,qBAAqB,UAAU;AAyB1E,IAAM,iBAAN,MAAM,gBAAwC;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAST,YACE,KACA,QACA,uBAAqD,CAAC,GACtD;AACA,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,uBAAuB;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,gBACJ,SACA,UACA,cACe;AACf,UAAM,EAAE,OAAO,YAAY,GAAG,WAAW,IAAI,KAAK;AAClD,UAAM,SAAqD;AAAA,MACzD,GAAG;AAAA,MACH;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA,GAAI,QAAQ,MAAM,SAAS,IACvB;AAAA,QACE,aAAa;AAAA,QACb,OAAO,QAAQ,MAAM,IAAI,CAAC,MAAM,eAAe,CAAC,CAAC;AAAA,MACnD,IACA,CAAC;AAAA,MACL,UAAU;AAAA,QACR,GAAG,mBAAmB,OAAO,QAAQ,MAAM;AAAA,QAC3C,GAAG,QAAQ,SAAS,QAAQ,CAAC,MAAM,mBAAmB,CAAC,CAAC;AAAA,MAC1D;AAAA,MACA,gBAAgB;AAAA,QACd,eAAe;AAAA,MACjB;AAAA,IACF;AAEA,aAAS,eAAe,EAAE,OAAO,CAAC;AAClC,aAAS,YAAY,MAAM;AAE3B,UAAM,SAAS,MAAM,KAAK,KAAK,KAAK,YAAY,OAAO,MAAM;AAK7D,SAAK,iBAAiB,QAAQ,UAAU,YAA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U,QAAQ;AAAA,QAC3B,OAAO;AACL,iBAAO,QAAQ,KAAK;AAAA,YAClB,MAAM;AAAA,YACN,MAAM,QAAQ;AAAA,UAChB,CAAC;AAAA,QACH;AACA;AAAA,MACF;AACE,cAAM,IAAI,MAAM,yBAAyB;AAAA,IAC7C;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,kBAAkB,OAAkD;AAC3E,SAAO;AAAA,IACL,cAAc,OAAO;AAAA,IACrB,eAAe,OAAO;AAAA,EACxB;AACF;AAEA,SAAS,gBACP,YAC8D;AAC9D,aAAW,UAAU,WAAW,SAAS;AACvC,YAAQ,OAAO,eAAe;AAAA,MAC5B,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,UACb,cAAc,OAAO;AAAA,QACvB;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF,KAAK;AACH,eAAO;AAAA,UACL,aAAa;AAAA,QACf;AAAA,MACF;AACE,eAAO;AAAA,UACL,aAAa;AAAA,UACb,cAAc,GAAG,OAAO,aAAa;AAAA,QACvC;AAAA,IACJ;AAAA,EACF;AACA,SAAO;AAAA,IACL,aAAa;AAAA,EACf;AACF;AAEA,SAAS,SAAS,QAA4D;AAC5E,UAAQ,OAAO,MAAM,MAAM;AAAA,IACzB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT;AACE,YAAM,IAAI,MAAM,cAAc;AAAA,EAClC;AACF;","names":["message","rest"]}
|
|
1
|
+
{"version":3,"sources":["../src/openai-provider.ts"],"sourcesContent":["import { OpenAI } from \"openai\";\n\nimport type {\n AssistantContent,\n AssistantMessage,\n CancellationToken,\n CompletionResponseData,\n Content,\n Message,\n ModelProvider,\n ModelRequest,\n ParameterType,\n ProviderContextTransformer,\n StreamReceiver,\n ToolContent,\n ToolDefinition,\n Usage,\n} from \"@simulacra-ai/core\";\n\ntype Prettify<T> = { [K in keyof T]: T[K] } & {};\n\nconst OPENAI_REASONING_DELTA_KEYS = [\"reasoning\", \"reasoning_content\", \"thinking\"] as const;\n\ntype OpenAIReasoningDeltaKey = (typeof OPENAI_REASONING_DELTA_KEYS)[number];\ntype OpenAICompletionDelta = OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta;\ntype OpenAIReasoningDelta = OpenAICompletionDelta &\n Partial<Record<OpenAIReasoningDeltaKey, string>>;\n\n/**\n * Configuration options for the OpenAI provider.\n */\nexport interface OpenAIProviderConfig extends Record<string, unknown> {\n /** The model identifier to use (e.g., \"gpt-4\", \"o1-preview\"). */\n model: string;\n /** The maximum number of tokens to generate in the response. */\n max_tokens?: number;\n /**\n * Which role to use for the system prompt.\n *\n * The OpenAI Chat Completions spec allows `system` (legacy / most providers)\n * and `developer` (introduced for o-series reasoning models). Most\n * OpenAI-compatible endpoints (DeepSeek, OpenRouter relays, Anthropic-via-\n * compat, self-hosted gateways, etc.) only accept `system` and reject\n * `developer` with a 400.\n *\n * - `\"auto\"` (default): use the built-in heuristic — `gpt*` → `\"system\"`,\n * o-series (`o1`, `o3`, `o4`, ...) → `\"developer\"`, anything else →\n * `\"system\"` (the broadly-compatible default).\n * - `\"system\"` / `\"developer\"`: force the role regardless of model id.\n *\n * The heuristic matches on the bare model id and does not understand\n * relay-style prefixes (`openai/o3`, `anthropic/claude-3-5-sonnet`,\n * etc.) — set this explicitly when routing OpenAI models through a\n * relay that uses `vendor/model` ids.\n */\n system_role?: \"auto\" | \"system\" | \"developer\";\n /**\n * Whether to emit OpenAI's strict structured-output flag on tool\n * definitions (`function.strict: true`).\n *\n * `strict` is an OpenAI-specific extension that constrains tool arguments\n * to the supplied JSON Schema. Most non-OpenAI endpoints either ignore the\n * field or reject it.\n *\n * - `\"auto\"` (default): emit `strict: true` when the model id matches the\n * built-in OpenAI heuristic — `gpt*` or o-series (`o1`, `o3`, `o4`, ...).\n * Any other model id (deepseek, etc.) gets tool defs without `strict`.\n * The heuristic matches on the bare model id; relay-style prefixes\n * (`openai/gpt-4o`) are NOT recognized — set this explicitly when\n * routing OpenAI models through a relay.\n * - `\"never\"`: never emit `strict`.\n */\n strict_tools?: \"auto\" | \"never\";\n}\n\n/**\n * Model provider implementation for OpenAI's chat completion models.\n *\n * This provider wraps the OpenAI SDK to provide streaming completions with\n * support for tool use and function calling. It handles message formatting,\n * content streaming, and usage tracking according to the ModelProvider\n * interface.\n *\n * Works against OpenAI directly as well as OpenAI-compatible endpoints\n * (DeepSeek, OpenRouter, self-hosted gateways, etc.). The default behaviour\n * picks `system` vs. `developer` for the system prompt and decides whether\n * to emit OpenAI's `strict` flag on tool defs based on the model id; both\n * can be overridden through `OpenAIProviderConfig.system_role` and\n * `OpenAIProviderConfig.strict_tools` for endpoints whose behaviour differs.\n */\nexport class OpenAIProvider implements ModelProvider {\n readonly #sdk: OpenAI;\n readonly #config: OpenAIProviderConfig;\n readonly context_transformers: ProviderContextTransformer[];\n\n /**\n * Creates a new OpenAI provider instance.\n *\n * @param sdk - The initialized OpenAI SDK client.\n * @param config - Configuration options for the provider.\n * @param context_transformers - Provider-level context transformers.\n */\n constructor(\n sdk: OpenAI,\n config: OpenAIProviderConfig,\n context_transformers: ProviderContextTransformer[] = [],\n ) {\n this.#sdk = sdk;\n this.#config = config;\n this.context_transformers = context_transformers;\n }\n\n /**\n * Executes a model request and streams the response through the provided receiver.\n *\n * @param request - The request containing messages, tools, and system prompt.\n * @param receiver - The receiver that handles streaming events.\n * @param cancellation - Token to signal cancellation of the request.\n * @returns A promise that resolves when the request completes.\n */\n async execute_request(\n request: ModelRequest,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ): Promise<void> {\n const { model, max_tokens, system_role, strict_tools, ...api_extras } = this.#config;\n const emit_strict = resolve_strict_tools(model, strict_tools);\n const params: OpenAI.ChatCompletionCreateParamsStreaming = {\n ...api_extras,\n model,\n stream: true,\n max_tokens,\n ...(request.tools.length > 0\n ? {\n tool_choice: \"auto\",\n tools: request.tools.map((t) => to_openai_tool(t, emit_strict)),\n }\n : {}),\n messages: [\n ...get_system_context(model, request.system, system_role),\n ...request.messages.flatMap((m) => to_openai_messages(m)),\n ],\n stream_options: {\n include_usage: true,\n },\n };\n\n receiver.before_request({ params });\n receiver.request_raw(params);\n\n const stream = await this.#sdk.chat.completions.create(params);\n\n // Intentionally not awaited. Streaming is event-driven through the receiver.\n // The policy wraps only connection establishment; chunk processing flows\n // asynchronously via StreamListener events back to the conversation.\n this.#stream_response(stream, receiver, cancellation);\n }\n\n /**\n * Creates a clone of this provider with the same configuration.\n *\n * @returns A new provider instance with identical configuration.\n */\n clone(): ModelProvider {\n return new OpenAIProvider(this.#sdk, this.#config, this.context_transformers);\n }\n\n async #stream_response(\n stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ) {\n try {\n let response: OpenAI.Chat.Completions.ChatCompletionChunk | undefined;\n for await (const response_chunk of stream) {\n if (cancellation.is_cancellation_requested) {\n receiver.cancel();\n return;\n }\n receiver.stream_raw(response_chunk);\n\n const { choices: choices_chunk, ...rest } = response_chunk;\n response = {\n ...response,\n ...rest,\n choices: response?.choices ?? [],\n };\n\n for (const choice_chunk of choices_chunk) {\n if (!response.choices[choice_chunk.index]) {\n response.choices[choice_chunk.index] = choice_chunk;\n const message = from_openai_completion(response_chunk, choice_chunk);\n for (const content of message.content) {\n receiver.start_content({ content, message, usage: {} });\n }\n receiver.start_message({ message, usage: {} });\n continue;\n }\n\n const { delta: delta_chunk, ...rest } = choice_chunk;\n const choice = (response.choices[choice_chunk.index] = {\n ...response.choices[choice_chunk.index],\n ...rest,\n delta: {\n ...response.choices[choice_chunk.index]?.delta,\n },\n });\n\n if (delta_chunk.role) {\n choice.delta.role = delta_chunk.role;\n }\n if (delta_chunk.refusal) {\n if (!choice.delta.refusal) {\n choice.delta.refusal = \"\";\n }\n choice.delta.refusal += delta_chunk.refusal;\n }\n const reasoning_delta = get_openai_reasoning_delta(delta_chunk);\n if (reasoning_delta) {\n const choice_delta = choice.delta as OpenAIReasoningDelta;\n const existing = choice_delta[reasoning_delta.key];\n if (!existing) {\n choice_delta[reasoning_delta.key] = reasoning_delta.thought;\n receiver.start_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice_delta[reasoning_delta.key] = existing + reasoning_delta.thought;\n receiver.update_content({\n content: from_openai_thinking(choice_delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.content) {\n if (!choice.delta.content) {\n choice.delta.content = delta_chunk.content;\n receiver.start_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n choice.delta.content += delta_chunk.content;\n receiver.update_content({\n content: from_openai_content(choice.delta) as AssistantContent,\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls) {\n choice.delta.tool_calls = [];\n }\n for (const tool_call_chunk of delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls[tool_call_chunk.index]) {\n choice.delta.tool_calls[tool_call_chunk.index] = tool_call_chunk;\n receiver.start_content({\n content: from_openai_tool_call(tool_call_chunk),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n } else {\n const tool_call = choice.delta.tool_calls[tool_call_chunk.index];\n\n if (tool_call_chunk.id) {\n tool_call.id = tool_call_chunk.id;\n }\n if (tool_call_chunk.type) {\n tool_call.type = tool_call_chunk.type;\n }\n if (tool_call_chunk.function) {\n if (!tool_call.function) {\n tool_call.function = tool_call_chunk.function;\n } else {\n if (tool_call_chunk.function.name) {\n tool_call.function.name = tool_call_chunk.function.name;\n }\n if (tool_call_chunk.function.arguments) {\n if (!tool_call.function.arguments) {\n tool_call.function.arguments = \"\";\n }\n tool_call.function.arguments += tool_call_chunk.function.arguments;\n }\n }\n }\n receiver.update_content({\n content: from_openai_tool_call(tool_call),\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_openai_completion(response_chunk, choice),\n usage: response?.usage ? from_openai_usage(response.usage) : {},\n });\n }\n }\n }\n }\n }\n if (!response || !response.choices?.[0]) {\n throw new Error(\"no data\");\n }\n receiver.response_raw({ ...response });\n\n const message = from_openai_completion(response, response.choices[0]);\n const usage = response?.usage ? from_openai_usage(response.usage) : {};\n for (const content of message.content) {\n receiver.complete_content({ content, message, usage });\n }\n receiver.complete_message({ message, usage, ...map_stop_reason(response) });\n } catch (error) {\n receiver.error(error);\n }\n }\n}\n\nfunction get_system_context(\n model: string,\n system: string | undefined,\n system_role: OpenAIProviderConfig[\"system_role\"] = \"auto\",\n): OpenAI.ChatCompletionMessageParam[] {\n if (!system) {\n return [];\n }\n const role = resolve_system_role(model, system_role);\n if (role === \"developer\") {\n return [\n {\n role: \"developer\",\n content: system,\n } as OpenAI.ChatCompletionDeveloperMessageParam,\n ];\n }\n return [\n {\n role: \"system\",\n content: system,\n } as OpenAI.ChatCompletionSystemMessageParam,\n ];\n}\n\nfunction resolve_system_role(\n model: string,\n system_role: OpenAIProviderConfig[\"system_role\"] = \"auto\",\n): \"system\" | \"developer\" {\n if (system_role === \"system\" || system_role === \"developer\") {\n return system_role;\n }\n if (model.startsWith(\"gpt\")) {\n return \"system\";\n }\n if (is_openai_reasoning_model(model)) {\n return \"developer\";\n }\n return \"system\";\n}\n\nfunction resolve_strict_tools(\n model: string,\n strict_tools: OpenAIProviderConfig[\"strict_tools\"] = \"auto\",\n): boolean {\n if (strict_tools === \"never\") {\n return false;\n }\n return model.startsWith(\"gpt\") || is_openai_reasoning_model(model);\n}\n\n// Best-effort match for OpenAI's o-series reasoning model ids (o1, o3,\n// o4-mini, etc.). The leading non-zero digit and end-of-string-or-hyphen\n// anchor narrow the pattern to what OpenAI actually ships. Two known\n// failure modes: a non-OpenAI vendor whose model id happens to follow the\n// same shape (e.g. `o2-fast`) gets matched as o-series, and a relay-style\n// id like `openai/o3` is NOT matched because the regex anchors at start\n// of string. Operators in either situation should set `system_role` and\n// `strict_tools` explicitly — the heuristic is a default, not a constraint.\nfunction is_openai_reasoning_model(model: string): boolean {\n return /^o[1-9]\\d*(-|$)/.test(model);\n}\n\nfunction to_openai_tool(tool: ToolDefinition, strict: boolean): OpenAI.Chat.ChatCompletionTool {\n function map_parameter_type(\n parameter: Prettify<ParameterType & { description?: string }>,\n ): OpenAI.FunctionParameters {\n switch (parameter.type) {\n case \"object\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n properties: Object.fromEntries(\n Object.entries(parameter.properties).map(([k, v]) => [k, map_parameter_type(v)]),\n ),\n additionalProperties: false,\n required: Object.entries(parameter.properties).map(([k]) => k),\n };\n case \"array\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n items: map_parameter_type(parameter.items),\n };\n default:\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description:\n parameter.default !== undefined\n ? parameter.description\n ? `${parameter.description} (default: ${parameter.default})`\n : `default: ${parameter.default}`\n : parameter.description,\n enum: \"enum\" in parameter ? parameter.enum : undefined,\n };\n }\n }\n return {\n type: \"function\",\n function: {\n name: tool.name,\n description: tool.description,\n parameters: map_parameter_type({\n type: \"object\",\n required: true,\n properties: Object.fromEntries(\n tool.parameters.map(({ name, ...parameter }) => [name, parameter]),\n ),\n }),\n ...(strict ? { strict: true } : {}),\n },\n };\n}\n\nfunction from_openai_completion(\n completion: OpenAI.Chat.Completions.ChatCompletionChunk,\n choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice,\n) {\n let contents: Content[] = [];\n const thinking = from_openai_thinking(choice.delta);\n if (thinking) {\n contents = [...contents, thinking];\n }\n const delta_record = choice.delta as Record<string, unknown>;\n for (const key of Object.keys(choice.delta)) {\n if (key === \"role\") {\n continue;\n }\n if (is_openai_reasoning_delta_key(key)) {\n continue;\n }\n if (key === \"content\") {\n if (choice.delta.content) {\n contents = [...contents, from_openai_content(choice.delta)];\n }\n continue;\n }\n if (key === \"refusal\") {\n if (choice.delta.refusal) {\n contents = [...contents, from_openai_refusal(choice.delta)];\n }\n continue;\n }\n if (key === \"tool_calls\") {\n if (choice.delta.tool_calls) {\n contents = [...contents, ...choice.delta.tool_calls.map((t) => from_openai_tool_call(t))];\n }\n continue;\n }\n if (delta_record[key] !== undefined && delta_record[key] !== null) {\n const data = delta_record[key];\n contents = [\n ...contents,\n {\n type: \"raw\",\n model_kind: \"openai\",\n data: JSON.stringify({ [key]: data }),\n },\n ];\n }\n }\n return {\n id: completion.id,\n timestamp: completion.created,\n role: map_role(choice),\n content: contents,\n } as AssistantMessage;\n}\n\nfunction get_openai_reasoning_delta(delta: OpenAICompletionDelta) {\n const record = delta as Partial<Record<OpenAIReasoningDeltaKey, unknown>>;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n const thought = record[key];\n if (typeof thought === \"string\" && thought.length > 0) {\n return { key, thought };\n }\n }\n return undefined;\n}\n\nfunction from_openai_thinking(content: OpenAICompletionDelta) {\n const reasoning = get_openai_reasoning_delta(content);\n if (!reasoning) {\n return undefined;\n }\n const extended = get_openai_delta_extended(content);\n return {\n type: \"thinking\",\n thought: reasoning.thought,\n extended: {\n ...extended,\n openai_reasoning_field: reasoning.key,\n },\n } as Content;\n}\n\nfunction is_openai_reasoning_delta_key(key: string): key is OpenAIReasoningDeltaKey {\n return OPENAI_REASONING_DELTA_KEYS.includes(key as OpenAIReasoningDeltaKey);\n}\n\nfunction from_openai_refusal(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.refusal,\n extended: {\n ...get_openai_delta_extended(content),\n openai_refusal: true,\n },\n } as Content;\n}\n\nfunction from_openai_content(content: OpenAICompletionDelta) {\n return {\n type: \"text\",\n text: content.content,\n extended: get_openai_delta_extended(content),\n } as Content;\n}\n\nfunction get_openai_delta_extended(content: OpenAICompletionDelta) {\n const extended = { ...(content as Record<string, unknown>) };\n delete extended.content;\n delete extended.tool_calls;\n delete extended.function_call;\n delete extended.refusal;\n delete extended.role;\n for (const key of OPENAI_REASONING_DELTA_KEYS) {\n delete extended[key];\n }\n return extended;\n}\n\nfunction from_openai_tool_call(\n tool_call: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta.ToolCall,\n) {\n const { id: tool_request_id, function: fn, type: _, index: __, ...extended } = tool_call;\n let params: unknown;\n try {\n params = JSON.parse(fn?.arguments ?? \"{}\");\n } catch {\n params = fn?.arguments;\n }\n return {\n tool_request_id,\n type: \"tool\",\n tool: fn?.name,\n params,\n extended,\n } as ToolContent;\n}\n\nfunction to_openai_messages(message: Message) {\n if (message.role === \"assistant\") {\n return [to_openai_assistant_message(message)];\n }\n // Partition content so tool_result blocks come before non-tool_result blocks.\n // OpenAI requires all tool-role messages immediately after the assistant message\n // containing the corresponding tool_calls; interleaving user messages between\n // tool messages causes a validation error.\n const tool_result_content = message.content.filter((c) => c.type === \"tool_result\");\n const other_content = message.content.filter((c) => c.type !== \"tool_result\");\n const ordered_content = [...tool_result_content, ...other_content];\n\n const results: OpenAI.ChatCompletionMessageParam[] = [];\n let result: OpenAI.ChatCompletionMessageParam | undefined;\n for (const content of ordered_content) {\n if (content.type === \"text\") {\n if (!result) {\n result = {\n role: \"user\",\n content: content.text,\n };\n } else if (result.role === \"tool\") {\n results.push(result);\n result = {\n role: \"user\",\n content: content.text,\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = [\n {\n type: \"text\",\n text: content.text,\n },\n ];\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n } else if (content.type === \"tool_result\") {\n if (!result) {\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else if (result.role !== \"tool\" || result.tool_call_id !== content.tool_request_id) {\n results.push(result);\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n result.content.push({\n type: \"text\",\n text: JSON.stringify(content.result),\n });\n }\n } else if (content.type === \"raw\") {\n result = {\n ...(result ?? {}),\n ...JSON.parse(content.data),\n };\n }\n }\n if (result) {\n results.push(result);\n }\n return results;\n}\n\nfunction to_openai_assistant_message(message: AssistantMessage) {\n let result: OpenAI.ChatCompletionAssistantMessageParam = {\n role: \"assistant\",\n };\n for (const content of message.content) {\n switch (content.type) {\n case \"text\":\n if (content.extended && content.extended.openai_refusal === true) {\n result.refusal = content.text;\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.text;\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n break;\n case \"tool\":\n if (!result.tool_calls) {\n result.tool_calls = [];\n }\n result.tool_calls.push({\n id: content.tool_request_id,\n type: \"function\",\n function: {\n name: content.tool,\n arguments: JSON.stringify(content.params),\n },\n });\n break;\n case \"raw\":\n if (content.model_kind !== \"openai\") {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.data;\n } else {\n result.content.push({\n type: \"text\",\n text: content.data,\n });\n }\n break;\n }\n result = {\n ...result,\n ...JSON.parse(content.data),\n };\n break;\n case \"thinking\":\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.thought;\n } else {\n result.content.push({\n type: \"text\",\n text: content.thought,\n });\n }\n break;\n default:\n throw new Error(\"unexpected content type\");\n }\n }\n return result;\n}\n\nfunction from_openai_usage(usage: OpenAI.CompletionUsage | null | undefined) {\n return {\n input_tokens: usage?.prompt_tokens,\n output_tokens: usage?.completion_tokens,\n } as Usage;\n}\n\nfunction map_stop_reason(\n completion: OpenAI.ChatCompletionChunk,\n): Pick<CompletionResponseData, \"stop_reason\" | \"stop_details\"> {\n for (const choice of completion.choices) {\n switch (choice.finish_reason) {\n case \"content_filter\":\n return {\n stop_reason: \"error\",\n stop_details: choice.finish_reason,\n };\n case \"function_call\":\n return {\n stop_reason: \"tool_use\",\n };\n case \"length\":\n return {\n stop_reason: \"max_tokens\",\n };\n case \"stop\":\n return {\n stop_reason: \"end_turn\",\n };\n case \"tool_calls\":\n return {\n stop_reason: \"tool_use\",\n };\n default:\n return {\n stop_reason: \"other\",\n stop_details: `${choice.finish_reason}`,\n };\n }\n }\n return {\n stop_reason: \"other\",\n };\n}\n\nfunction map_role(choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice) {\n switch (choice.delta.role) {\n case \"user\":\n case \"developer\":\n case \"system\":\n return \"user\";\n case \"assistant\":\n case \"tool\":\n return \"assistant\";\n default:\n throw new Error(\"invalid role\");\n }\n}\n"],"mappings":";AAqBA,IAAM,8BAA8B,CAAC,aAAa,qBAAqB,UAAU;AAqE1E,IAAM,iBAAN,MAAM,gBAAwC;AAAA,EAC1C;AAAA,E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KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT;AACE,YAAM,IAAI,MAAM,cAAc;AAAA,EAClC;AACF;","names":["message","rest"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simulacra-ai/openai",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "OpenAI provider for the Simulacra conversation engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"test:watch": "vitest"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@simulacra-ai/core": "0.0.
|
|
28
|
+
"@simulacra-ai/core": "0.0.12",
|
|
29
29
|
"openai": ">=4.0.0 <7.0.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|