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