@theokit/sdk 4.8.0 → 4.9.1

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 CHANGED
@@ -15716,6 +15716,201 @@ function abortError2(signal) {
15716
15716
  return new Error("AbortError");
15717
15717
  }
15718
15718
 
15719
+ // src/internal/llm/responses.ts
15720
+ function messageToInputItems(message) {
15721
+ const items = [];
15722
+ if (message.role === "user") {
15723
+ const content = [];
15724
+ for (const part of message.content) {
15725
+ if (part.type === "text") {
15726
+ content.push({ type: "input_text", text: part.text });
15727
+ } else if (part.type === "image") {
15728
+ const url = part.source.type === "base64" ? `data:${part.source.media_type};base64,${part.source.data}` : part.source.url;
15729
+ content.push({ type: "input_image", image_url: url });
15730
+ } else if (part.type === "tool_result") {
15731
+ items.push({
15732
+ type: "function_call_output",
15733
+ call_id: part.toolUseId,
15734
+ output: toStringToolResultContent(part.content, "openai-responses")
15735
+ });
15736
+ }
15737
+ }
15738
+ if (content.length > 0) items.push({ role: "user", content });
15739
+ return items;
15740
+ }
15741
+ if (message.role === "assistant") {
15742
+ const content = [];
15743
+ for (const part of message.content) {
15744
+ if (part.type === "text") {
15745
+ content.push({ type: "output_text", text: part.text });
15746
+ } else if (part.type === "tool_use") {
15747
+ items.push({
15748
+ type: "function_call",
15749
+ call_id: part.id,
15750
+ name: part.name,
15751
+ arguments: JSON.stringify(part.input)
15752
+ });
15753
+ }
15754
+ }
15755
+ if (content.length > 0) items.push({ role: "assistant", content });
15756
+ return items;
15757
+ }
15758
+ const text = message.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
15759
+ if (text.length > 0) items.push({ role: "system", content: text });
15760
+ return items;
15761
+ }
15762
+ function buildResponsesBody(request) {
15763
+ const input = [];
15764
+ for (const message of request.messages) {
15765
+ for (const item of messageToInputItems(message)) input.push(item);
15766
+ }
15767
+ const slash = request.model.lastIndexOf("/");
15768
+ const model = slash >= 0 ? request.model.slice(slash + 1) : request.model;
15769
+ const body = { model, input, stream: true, store: false };
15770
+ const instructions = collapseSystemText(request.system);
15771
+ if (instructions.length > 0) body.instructions = instructions;
15772
+ if (request.maxTokens !== void 0) body.max_output_tokens = request.maxTokens;
15773
+ if (request.temperature !== void 0) body.temperature = request.temperature;
15774
+ const tools = (request.tools ?? []).map((tool) => ({
15775
+ type: "function",
15776
+ name: tool.name,
15777
+ description: tool.description,
15778
+ parameters: tool.inputSchema,
15779
+ strict: false
15780
+ }));
15781
+ if (tools.length > 0) body.tools = tools;
15782
+ if (request.reasoning !== void 0) body.reasoning = { effort: request.reasoning.effort };
15783
+ return body;
15784
+ }
15785
+ var ResponsesApiClient = class {
15786
+ constructor(options) {
15787
+ this.options = options;
15788
+ this.name = options.providerName ?? "openai-responses";
15789
+ this.baseUrl = (options.baseUrl ?? "https://api.openai.com/v1").replace(/\/+$/, "");
15790
+ this.fetchImpl = options.fetch ?? fetch;
15791
+ }
15792
+ options;
15793
+ name;
15794
+ baseUrl;
15795
+ fetchImpl;
15796
+ // 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.
15797
+ async *stream(request, signal) {
15798
+ const providerId = this.options.providerName ?? "openai";
15799
+ const headers = {
15800
+ "content-type": "application/json",
15801
+ accept: "text/event-stream",
15802
+ authorization: `Bearer ${this.options.apiKey}`,
15803
+ ...this.options.extraHeaders ?? {}
15804
+ };
15805
+ const url = `${this.baseUrl}/responses`;
15806
+ const response = await this.fetchImpl(url, {
15807
+ method: "POST",
15808
+ signal,
15809
+ headers,
15810
+ body: JSON.stringify(buildResponsesBody(request))
15811
+ });
15812
+ if (!response.ok) {
15813
+ const text2 = await response.text().catch(() => "");
15814
+ let body = text2;
15815
+ try {
15816
+ body = JSON.parse(text2);
15817
+ } catch {
15818
+ }
15819
+ throw mapOpenAICompatibleError({
15820
+ providerId,
15821
+ status: response.status,
15822
+ body,
15823
+ headers: response.headers,
15824
+ endpoint: "/responses"
15825
+ });
15826
+ }
15827
+ let text = "";
15828
+ const toolCalls = [];
15829
+ let stopReason = "end_turn";
15830
+ let inputTokens;
15831
+ let outputTokens;
15832
+ let reasoningTokens;
15833
+ const pending = {};
15834
+ if (response.body !== null) {
15835
+ for await (const record of parseSseStream(response.body, signal)) {
15836
+ if (record.data === "[DONE]") break;
15837
+ let event;
15838
+ try {
15839
+ event = JSON.parse(record.data);
15840
+ } catch {
15841
+ continue;
15842
+ }
15843
+ const t = event.type;
15844
+ if (t === "response.output_text.delta") {
15845
+ const d = event.delta ?? "";
15846
+ if (d.length > 0) {
15847
+ text += d;
15848
+ yield { type: "text_delta", text: d };
15849
+ }
15850
+ } else if (t === "response.reasoning_summary_text.delta" || t === "response.reasoning_text.delta") {
15851
+ const d = event.delta ?? "";
15852
+ if (d.length > 0) yield { type: "reasoning_delta", text: d };
15853
+ } else if (t === "response.output_item.added" && event.item?.type === "function_call") {
15854
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
15855
+ pending[id] = {
15856
+ callId: event.item.call_id ?? id,
15857
+ name: event.item.name ?? "",
15858
+ args: event.item.arguments ?? ""
15859
+ };
15860
+ } else if (t === "response.function_call_arguments.delta") {
15861
+ const c = event.item_id !== void 0 ? pending[event.item_id] : void 0;
15862
+ if (c !== void 0) c.args += event.delta ?? "";
15863
+ } else if (t === "response.output_item.done" && event.item?.type === "function_call") {
15864
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
15865
+ const c = pending[id] ?? {
15866
+ callId: event.item.call_id ?? id,
15867
+ name: event.item.name ?? "",
15868
+ args: event.item.arguments ?? ""
15869
+ };
15870
+ const rawArgs = event.item.arguments ?? c.args;
15871
+ const call = {
15872
+ type: "tool_use",
15873
+ id: c.callId,
15874
+ name: c.name.length > 0 ? c.name : event.item.name ?? "",
15875
+ input: parseToolArguments(rawArgs)
15876
+ };
15877
+ toolCalls.push(call);
15878
+ delete pending[id];
15879
+ yield { type: "tool_use", id: call.id, name: call.name, input: call.input };
15880
+ } else if (t === "response.completed" || t === "response.incomplete") {
15881
+ const usage = event.response?.usage;
15882
+ if (usage !== void 0) {
15883
+ inputTokens = usage.input_tokens;
15884
+ outputTokens = usage.output_tokens;
15885
+ reasoningTokens = usage.output_tokens_details?.reasoning_tokens;
15886
+ }
15887
+ stopReason = t === "response.incomplete" ? "max_tokens" : "end_turn";
15888
+ } else if (t === "response.failed" || t === "error") {
15889
+ const msg = event.response?.error?.message ?? event.message ?? "responses stream failed";
15890
+ yield { type: "error", message: msg };
15891
+ throw mapOpenAICompatibleError({
15892
+ providerId,
15893
+ status: 502,
15894
+ body: { error: { message: msg } },
15895
+ headers: response.headers,
15896
+ endpoint: "/responses"
15897
+ });
15898
+ }
15899
+ }
15900
+ }
15901
+ if (toolCalls.length > 0 && stopReason === "end_turn") stopReason = "tool_use";
15902
+ yield { type: "stop", reason: stopReason };
15903
+ return makeLlmFinish({
15904
+ stopReason,
15905
+ text,
15906
+ toolCalls,
15907
+ inputTokens,
15908
+ outputTokens,
15909
+ reasoningTokens
15910
+ });
15911
+ }
15912
+ };
15913
+
15719
15914
  // src/internal/llm/vertex-anthropic.ts
15720
15915
  init_errors();
15721
15916
 
@@ -16118,6 +16313,14 @@ function selectTransport(profile, apiKey) {
16118
16313
  const realKey = apiKey === "__bedrock_lazy_token__" ? void 0 : apiKey;
16119
16314
  return new BedrockAnthropicClient(realKey !== void 0 ? { apiKey: realKey } : {});
16120
16315
  }
16316
+ if (profile.apiMode === "responses_api") {
16317
+ return new ResponsesApiClient({
16318
+ apiKey,
16319
+ ...profile.baseUrl !== void 0 ? { baseUrl: profile.baseUrl } : {},
16320
+ ...profile.extraHeaders !== void 0 ? { extraHeaders: profile.extraHeaders } : {},
16321
+ providerName: profile.name
16322
+ });
16323
+ }
16121
16324
  throw new exports.ConfigurationError(
16122
16325
  `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".`,
16123
16326
  { code: "transport_unavailable" }