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