@theokit/sdk 4.8.0 → 4.9.0

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/eval.js CHANGED
@@ -12945,6 +12945,192 @@ function assistantMessage(message) {
12945
12945
  return result;
12946
12946
  }
12947
12947
 
12948
+ // src/internal/llm/responses.ts
12949
+ function messageToInputItems(message) {
12950
+ const items = [];
12951
+ if (message.role === "user") {
12952
+ const content = [];
12953
+ for (const part of message.content) {
12954
+ if (part.type === "text") {
12955
+ content.push({ type: "input_text", text: part.text });
12956
+ } else if (part.type === "image") {
12957
+ const url = part.source.type === "base64" ? `data:${part.source.media_type};base64,${part.source.data}` : part.source.url;
12958
+ content.push({ type: "input_image", image_url: url });
12959
+ } else if (part.type === "tool_result") {
12960
+ items.push({
12961
+ type: "function_call_output",
12962
+ call_id: part.toolUseId,
12963
+ output: toStringToolResultContent(part.content, "openai-responses")
12964
+ });
12965
+ }
12966
+ }
12967
+ if (content.length > 0) items.push({ role: "user", content });
12968
+ return items;
12969
+ }
12970
+ if (message.role === "assistant") {
12971
+ const content = [];
12972
+ for (const part of message.content) {
12973
+ if (part.type === "text") {
12974
+ content.push({ type: "output_text", text: part.text });
12975
+ } else if (part.type === "tool_use") {
12976
+ items.push({
12977
+ type: "function_call",
12978
+ call_id: part.id,
12979
+ name: part.name,
12980
+ arguments: JSON.stringify(part.input)
12981
+ });
12982
+ }
12983
+ }
12984
+ if (content.length > 0) items.push({ role: "assistant", content });
12985
+ return items;
12986
+ }
12987
+ const text = message.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
12988
+ if (text.length > 0) items.push({ role: "system", content: text });
12989
+ return items;
12990
+ }
12991
+ function buildResponsesBody(request) {
12992
+ const input = [];
12993
+ for (const message of request.messages) {
12994
+ for (const item of messageToInputItems(message)) input.push(item);
12995
+ }
12996
+ const body = { model: request.model, input, stream: true, store: false };
12997
+ const instructions = collapseSystemText(request.system);
12998
+ if (instructions.length > 0) body.instructions = instructions;
12999
+ if (request.maxTokens !== void 0) body.max_output_tokens = request.maxTokens;
13000
+ if (request.temperature !== void 0) body.temperature = request.temperature;
13001
+ const tools = (request.tools ?? []).map((tool) => ({
13002
+ type: "function",
13003
+ name: tool.name,
13004
+ description: tool.description,
13005
+ parameters: tool.inputSchema,
13006
+ strict: false
13007
+ }));
13008
+ if (tools.length > 0) body.tools = tools;
13009
+ if (request.reasoning !== void 0) body.reasoning = { effort: request.reasoning.effort };
13010
+ return body;
13011
+ }
13012
+ var ResponsesApiClient = class {
13013
+ constructor(options) {
13014
+ this.options = options;
13015
+ this.name = options.providerName ?? "openai-responses";
13016
+ this.baseUrl = (options.baseUrl ?? "https://api.openai.com/v1").replace(/\/+$/, "");
13017
+ this.fetchImpl = options.fetch ?? fetch;
13018
+ }
13019
+ options;
13020
+ name;
13021
+ baseUrl;
13022
+ fetchImpl;
13023
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: the SSE dispatch (text / reasoning / tool-call add+delta+done / terminal+usage / error) is one cohesive state machine, mirroring OpenAIStreamAccumulator.consume.
13024
+ async *stream(request, signal) {
13025
+ const providerId = this.options.providerName ?? "openai";
13026
+ const headers = {
13027
+ "content-type": "application/json",
13028
+ accept: "text/event-stream",
13029
+ authorization: `Bearer ${this.options.apiKey}`,
13030
+ ...this.options.extraHeaders ?? {}
13031
+ };
13032
+ const url = `${this.baseUrl}/responses`;
13033
+ const response = await this.fetchImpl(url, {
13034
+ method: "POST",
13035
+ signal,
13036
+ headers,
13037
+ body: JSON.stringify(buildResponsesBody(request))
13038
+ });
13039
+ if (!response.ok) {
13040
+ const text2 = await response.text().catch(() => "");
13041
+ let body = text2;
13042
+ try {
13043
+ body = JSON.parse(text2);
13044
+ } catch {
13045
+ }
13046
+ throw mapOpenAICompatibleError({
13047
+ providerId,
13048
+ status: response.status,
13049
+ body,
13050
+ headers: response.headers,
13051
+ endpoint: "/responses"
13052
+ });
13053
+ }
13054
+ let text = "";
13055
+ const toolCalls = [];
13056
+ let stopReason = "end_turn";
13057
+ let inputTokens;
13058
+ let outputTokens;
13059
+ let reasoningTokens;
13060
+ const pending = {};
13061
+ if (response.body !== null) {
13062
+ for await (const record of parseSseStream(response.body, signal)) {
13063
+ if (record.data === "[DONE]") break;
13064
+ let event;
13065
+ try {
13066
+ event = JSON.parse(record.data);
13067
+ } catch {
13068
+ continue;
13069
+ }
13070
+ const t = event.type;
13071
+ if (t === "response.output_text.delta") {
13072
+ const d = event.delta ?? "";
13073
+ if (d.length > 0) {
13074
+ text += d;
13075
+ yield { type: "text_delta", text: d };
13076
+ }
13077
+ } else if (t === "response.reasoning_summary_text.delta" || t === "response.reasoning_text.delta") {
13078
+ const d = event.delta ?? "";
13079
+ if (d.length > 0) yield { type: "reasoning_delta", text: d };
13080
+ } else if (t === "response.output_item.added" && event.item?.type === "function_call") {
13081
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
13082
+ pending[id] = {
13083
+ callId: event.item.call_id ?? id,
13084
+ name: event.item.name ?? "",
13085
+ args: event.item.arguments ?? ""
13086
+ };
13087
+ } else if (t === "response.function_call_arguments.delta") {
13088
+ const c = event.item_id !== void 0 ? pending[event.item_id] : void 0;
13089
+ if (c !== void 0) c.args += event.delta ?? "";
13090
+ } else if (t === "response.output_item.done" && event.item?.type === "function_call") {
13091
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
13092
+ const c = pending[id] ?? {
13093
+ callId: event.item.call_id ?? id,
13094
+ name: event.item.name ?? "",
13095
+ args: event.item.arguments ?? ""
13096
+ };
13097
+ const rawArgs = event.item.arguments ?? c.args;
13098
+ const call = {
13099
+ type: "tool_use",
13100
+ id: c.callId,
13101
+ name: c.name.length > 0 ? c.name : event.item.name ?? "",
13102
+ input: parseToolArguments(rawArgs)
13103
+ };
13104
+ toolCalls.push(call);
13105
+ delete pending[id];
13106
+ yield { type: "tool_use", id: call.id, name: call.name, input: call.input };
13107
+ } else if (t === "response.completed" || t === "response.incomplete") {
13108
+ const usage = event.response?.usage;
13109
+ if (usage !== void 0) {
13110
+ inputTokens = usage.input_tokens;
13111
+ outputTokens = usage.output_tokens;
13112
+ reasoningTokens = usage.output_tokens_details?.reasoning_tokens;
13113
+ }
13114
+ stopReason = t === "response.incomplete" ? "max_tokens" : "end_turn";
13115
+ } else if (t === "response.failed" || t === "error") {
13116
+ const msg = event.response?.error?.message ?? event.message ?? "responses stream failed";
13117
+ yield { type: "error", message: msg };
13118
+ throw mapOpenAICompatibleError({
13119
+ providerId,
13120
+ status: 502,
13121
+ body: { error: { message: msg } },
13122
+ headers: response.headers,
13123
+ endpoint: "/responses"
13124
+ });
13125
+ }
13126
+ }
13127
+ }
13128
+ if (toolCalls.length > 0 && stopReason === "end_turn") stopReason = "tool_use";
13129
+ yield { type: "stop", reason: stopReason };
13130
+ return makeLlmFinish({ stopReason, text, toolCalls, inputTokens, outputTokens, reasoningTokens });
13131
+ }
13132
+ };
13133
+
12948
13134
  // src/internal/llm/pool-aware-client.ts
12949
13135
  init_errors();
12950
13136
 
@@ -13513,6 +13699,14 @@ function selectTransport(profile, apiKey) {
13513
13699
  const realKey = apiKey === "__bedrock_lazy_token__" ? void 0 : apiKey;
13514
13700
  return new BedrockAnthropicClient(realKey !== void 0 ? { apiKey: realKey } : {});
13515
13701
  }
13702
+ if (profile.apiMode === "responses_api") {
13703
+ return new ResponsesApiClient({
13704
+ apiKey,
13705
+ ...profile.baseUrl !== void 0 ? { baseUrl: profile.baseUrl } : {},
13706
+ ...profile.extraHeaders !== void 0 ? { extraHeaders: profile.extraHeaders } : {},
13707
+ providerName: profile.name
13708
+ });
13709
+ }
13516
13710
  throw new ConfigurationError(
13517
13711
  `Provider "${profile.name}" requires apiMode "${profile.apiMode}" but no transport is registered. Install a third-party transport plugin (@theokit-transport-${profile.apiMode}) or use a provider with apiMode "chat_completions" or "anthropic_messages".`,
13518
13712
  { code: "transport_unavailable" }