@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/index.cjs CHANGED
@@ -15550,6 +15550,192 @@ function assistantMessage(message) {
15550
15550
  return result;
15551
15551
  }
15552
15552
 
15553
+ // src/internal/llm/responses.ts
15554
+ function messageToInputItems(message) {
15555
+ const items = [];
15556
+ if (message.role === "user") {
15557
+ const content = [];
15558
+ for (const part of message.content) {
15559
+ if (part.type === "text") {
15560
+ content.push({ type: "input_text", text: part.text });
15561
+ } else if (part.type === "image") {
15562
+ const url = part.source.type === "base64" ? `data:${part.source.media_type};base64,${part.source.data}` : part.source.url;
15563
+ content.push({ type: "input_image", image_url: url });
15564
+ } else if (part.type === "tool_result") {
15565
+ items.push({
15566
+ type: "function_call_output",
15567
+ call_id: part.toolUseId,
15568
+ output: toStringToolResultContent(part.content, "openai-responses")
15569
+ });
15570
+ }
15571
+ }
15572
+ if (content.length > 0) items.push({ role: "user", content });
15573
+ return items;
15574
+ }
15575
+ if (message.role === "assistant") {
15576
+ const content = [];
15577
+ for (const part of message.content) {
15578
+ if (part.type === "text") {
15579
+ content.push({ type: "output_text", text: part.text });
15580
+ } else if (part.type === "tool_use") {
15581
+ items.push({
15582
+ type: "function_call",
15583
+ call_id: part.id,
15584
+ name: part.name,
15585
+ arguments: JSON.stringify(part.input)
15586
+ });
15587
+ }
15588
+ }
15589
+ if (content.length > 0) items.push({ role: "assistant", content });
15590
+ return items;
15591
+ }
15592
+ const text = message.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
15593
+ if (text.length > 0) items.push({ role: "system", content: text });
15594
+ return items;
15595
+ }
15596
+ function buildResponsesBody(request) {
15597
+ const input = [];
15598
+ for (const message of request.messages) {
15599
+ for (const item of messageToInputItems(message)) input.push(item);
15600
+ }
15601
+ const body = { model: request.model, input, stream: true, store: false };
15602
+ const instructions = collapseSystemText(request.system);
15603
+ if (instructions.length > 0) body.instructions = instructions;
15604
+ if (request.maxTokens !== void 0) body.max_output_tokens = request.maxTokens;
15605
+ if (request.temperature !== void 0) body.temperature = request.temperature;
15606
+ const tools = (request.tools ?? []).map((tool) => ({
15607
+ type: "function",
15608
+ name: tool.name,
15609
+ description: tool.description,
15610
+ parameters: tool.inputSchema,
15611
+ strict: false
15612
+ }));
15613
+ if (tools.length > 0) body.tools = tools;
15614
+ if (request.reasoning !== void 0) body.reasoning = { effort: request.reasoning.effort };
15615
+ return body;
15616
+ }
15617
+ var ResponsesApiClient = class {
15618
+ constructor(options) {
15619
+ this.options = options;
15620
+ this.name = options.providerName ?? "openai-responses";
15621
+ this.baseUrl = (options.baseUrl ?? "https://api.openai.com/v1").replace(/\/+$/, "");
15622
+ this.fetchImpl = options.fetch ?? fetch;
15623
+ }
15624
+ options;
15625
+ name;
15626
+ baseUrl;
15627
+ fetchImpl;
15628
+ // 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.
15629
+ async *stream(request, signal) {
15630
+ const providerId = this.options.providerName ?? "openai";
15631
+ const headers = {
15632
+ "content-type": "application/json",
15633
+ accept: "text/event-stream",
15634
+ authorization: `Bearer ${this.options.apiKey}`,
15635
+ ...this.options.extraHeaders ?? {}
15636
+ };
15637
+ const url = `${this.baseUrl}/responses`;
15638
+ const response = await this.fetchImpl(url, {
15639
+ method: "POST",
15640
+ signal,
15641
+ headers,
15642
+ body: JSON.stringify(buildResponsesBody(request))
15643
+ });
15644
+ if (!response.ok) {
15645
+ const text2 = await response.text().catch(() => "");
15646
+ let body = text2;
15647
+ try {
15648
+ body = JSON.parse(text2);
15649
+ } catch {
15650
+ }
15651
+ throw mapOpenAICompatibleError({
15652
+ providerId,
15653
+ status: response.status,
15654
+ body,
15655
+ headers: response.headers,
15656
+ endpoint: "/responses"
15657
+ });
15658
+ }
15659
+ let text = "";
15660
+ const toolCalls = [];
15661
+ let stopReason = "end_turn";
15662
+ let inputTokens;
15663
+ let outputTokens;
15664
+ let reasoningTokens;
15665
+ const pending = {};
15666
+ if (response.body !== null) {
15667
+ for await (const record of parseSseStream(response.body, signal)) {
15668
+ if (record.data === "[DONE]") break;
15669
+ let event;
15670
+ try {
15671
+ event = JSON.parse(record.data);
15672
+ } catch {
15673
+ continue;
15674
+ }
15675
+ const t = event.type;
15676
+ if (t === "response.output_text.delta") {
15677
+ const d = event.delta ?? "";
15678
+ if (d.length > 0) {
15679
+ text += d;
15680
+ yield { type: "text_delta", text: d };
15681
+ }
15682
+ } else if (t === "response.reasoning_summary_text.delta" || t === "response.reasoning_text.delta") {
15683
+ const d = event.delta ?? "";
15684
+ if (d.length > 0) yield { type: "reasoning_delta", text: d };
15685
+ } else if (t === "response.output_item.added" && event.item?.type === "function_call") {
15686
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
15687
+ pending[id] = {
15688
+ callId: event.item.call_id ?? id,
15689
+ name: event.item.name ?? "",
15690
+ args: event.item.arguments ?? ""
15691
+ };
15692
+ } else if (t === "response.function_call_arguments.delta") {
15693
+ const c = event.item_id !== void 0 ? pending[event.item_id] : void 0;
15694
+ if (c !== void 0) c.args += event.delta ?? "";
15695
+ } else if (t === "response.output_item.done" && event.item?.type === "function_call") {
15696
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
15697
+ const c = pending[id] ?? {
15698
+ callId: event.item.call_id ?? id,
15699
+ name: event.item.name ?? "",
15700
+ args: event.item.arguments ?? ""
15701
+ };
15702
+ const rawArgs = event.item.arguments ?? c.args;
15703
+ const call = {
15704
+ type: "tool_use",
15705
+ id: c.callId,
15706
+ name: c.name.length > 0 ? c.name : event.item.name ?? "",
15707
+ input: parseToolArguments(rawArgs)
15708
+ };
15709
+ toolCalls.push(call);
15710
+ delete pending[id];
15711
+ yield { type: "tool_use", id: call.id, name: call.name, input: call.input };
15712
+ } else if (t === "response.completed" || t === "response.incomplete") {
15713
+ const usage = event.response?.usage;
15714
+ if (usage !== void 0) {
15715
+ inputTokens = usage.input_tokens;
15716
+ outputTokens = usage.output_tokens;
15717
+ reasoningTokens = usage.output_tokens_details?.reasoning_tokens;
15718
+ }
15719
+ stopReason = t === "response.incomplete" ? "max_tokens" : "end_turn";
15720
+ } else if (t === "response.failed" || t === "error") {
15721
+ const msg = event.response?.error?.message ?? event.message ?? "responses stream failed";
15722
+ yield { type: "error", message: msg };
15723
+ throw mapOpenAICompatibleError({
15724
+ providerId,
15725
+ status: 502,
15726
+ body: { error: { message: msg } },
15727
+ headers: response.headers,
15728
+ endpoint: "/responses"
15729
+ });
15730
+ }
15731
+ }
15732
+ }
15733
+ if (toolCalls.length > 0 && stopReason === "end_turn") stopReason = "tool_use";
15734
+ yield { type: "stop", reason: stopReason };
15735
+ return makeLlmFinish({ stopReason, text, toolCalls, inputTokens, outputTokens, reasoningTokens });
15736
+ }
15737
+ };
15738
+
15553
15739
  // src/internal/llm/pool-aware-client.ts
15554
15740
  init_errors();
15555
15741
 
@@ -16118,6 +16304,14 @@ function selectTransport(profile, apiKey) {
16118
16304
  const realKey = apiKey === "__bedrock_lazy_token__" ? void 0 : apiKey;
16119
16305
  return new BedrockAnthropicClient(realKey !== void 0 ? { apiKey: realKey } : {});
16120
16306
  }
16307
+ if (profile.apiMode === "responses_api") {
16308
+ return new ResponsesApiClient({
16309
+ apiKey,
16310
+ ...profile.baseUrl !== void 0 ? { baseUrl: profile.baseUrl } : {},
16311
+ ...profile.extraHeaders !== void 0 ? { extraHeaders: profile.extraHeaders } : {},
16312
+ providerName: profile.name
16313
+ });
16314
+ }
16121
16315
  throw new exports.ConfigurationError(
16122
16316
  `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
16317
  { code: "transport_unavailable" }