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