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