@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/eval.cjs CHANGED
@@ -13114,6 +13114,201 @@ function abortError2(signal) {
13114
13114
  return new Error("AbortError");
13115
13115
  }
13116
13116
 
13117
+ // src/internal/llm/responses.ts
13118
+ function messageToInputItems(message) {
13119
+ const items = [];
13120
+ if (message.role === "user") {
13121
+ const content = [];
13122
+ for (const part of message.content) {
13123
+ if (part.type === "text") {
13124
+ content.push({ type: "input_text", text: part.text });
13125
+ } else if (part.type === "image") {
13126
+ const url = part.source.type === "base64" ? `data:${part.source.media_type};base64,${part.source.data}` : part.source.url;
13127
+ content.push({ type: "input_image", image_url: url });
13128
+ } else if (part.type === "tool_result") {
13129
+ items.push({
13130
+ type: "function_call_output",
13131
+ call_id: part.toolUseId,
13132
+ output: toStringToolResultContent(part.content, "openai-responses")
13133
+ });
13134
+ }
13135
+ }
13136
+ if (content.length > 0) items.push({ role: "user", content });
13137
+ return items;
13138
+ }
13139
+ if (message.role === "assistant") {
13140
+ const content = [];
13141
+ for (const part of message.content) {
13142
+ if (part.type === "text") {
13143
+ content.push({ type: "output_text", text: part.text });
13144
+ } else if (part.type === "tool_use") {
13145
+ items.push({
13146
+ type: "function_call",
13147
+ call_id: part.id,
13148
+ name: part.name,
13149
+ arguments: JSON.stringify(part.input)
13150
+ });
13151
+ }
13152
+ }
13153
+ if (content.length > 0) items.push({ role: "assistant", content });
13154
+ return items;
13155
+ }
13156
+ const text = message.content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
13157
+ if (text.length > 0) items.push({ role: "system", content: text });
13158
+ return items;
13159
+ }
13160
+ function buildResponsesBody(request) {
13161
+ const input = [];
13162
+ for (const message of request.messages) {
13163
+ for (const item of messageToInputItems(message)) input.push(item);
13164
+ }
13165
+ const slash = request.model.lastIndexOf("/");
13166
+ const model = slash >= 0 ? request.model.slice(slash + 1) : request.model;
13167
+ const body = { model, input, stream: true, store: false };
13168
+ const instructions = collapseSystemText(request.system);
13169
+ if (instructions.length > 0) body.instructions = instructions;
13170
+ if (request.maxTokens !== void 0) body.max_output_tokens = request.maxTokens;
13171
+ if (request.temperature !== void 0) body.temperature = request.temperature;
13172
+ const tools = (request.tools ?? []).map((tool) => ({
13173
+ type: "function",
13174
+ name: tool.name,
13175
+ description: tool.description,
13176
+ parameters: tool.inputSchema,
13177
+ strict: false
13178
+ }));
13179
+ if (tools.length > 0) body.tools = tools;
13180
+ if (request.reasoning !== void 0) body.reasoning = { effort: request.reasoning.effort };
13181
+ return body;
13182
+ }
13183
+ var ResponsesApiClient = class {
13184
+ constructor(options) {
13185
+ this.options = options;
13186
+ this.name = options.providerName ?? "openai-responses";
13187
+ this.baseUrl = (options.baseUrl ?? "https://api.openai.com/v1").replace(/\/+$/, "");
13188
+ this.fetchImpl = options.fetch ?? fetch;
13189
+ }
13190
+ options;
13191
+ name;
13192
+ baseUrl;
13193
+ fetchImpl;
13194
+ // 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.
13195
+ async *stream(request, signal) {
13196
+ const providerId = this.options.providerName ?? "openai";
13197
+ const headers = {
13198
+ "content-type": "application/json",
13199
+ accept: "text/event-stream",
13200
+ authorization: `Bearer ${this.options.apiKey}`,
13201
+ ...this.options.extraHeaders ?? {}
13202
+ };
13203
+ const url = `${this.baseUrl}/responses`;
13204
+ const response = await this.fetchImpl(url, {
13205
+ method: "POST",
13206
+ signal,
13207
+ headers,
13208
+ body: JSON.stringify(buildResponsesBody(request))
13209
+ });
13210
+ if (!response.ok) {
13211
+ const text2 = await response.text().catch(() => "");
13212
+ let body = text2;
13213
+ try {
13214
+ body = JSON.parse(text2);
13215
+ } catch {
13216
+ }
13217
+ throw mapOpenAICompatibleError({
13218
+ providerId,
13219
+ status: response.status,
13220
+ body,
13221
+ headers: response.headers,
13222
+ endpoint: "/responses"
13223
+ });
13224
+ }
13225
+ let text = "";
13226
+ const toolCalls = [];
13227
+ let stopReason = "end_turn";
13228
+ let inputTokens;
13229
+ let outputTokens;
13230
+ let reasoningTokens;
13231
+ const pending = {};
13232
+ if (response.body !== null) {
13233
+ for await (const record of parseSseStream(response.body, signal)) {
13234
+ if (record.data === "[DONE]") break;
13235
+ let event;
13236
+ try {
13237
+ event = JSON.parse(record.data);
13238
+ } catch {
13239
+ continue;
13240
+ }
13241
+ const t = event.type;
13242
+ if (t === "response.output_text.delta") {
13243
+ const d = event.delta ?? "";
13244
+ if (d.length > 0) {
13245
+ text += d;
13246
+ yield { type: "text_delta", text: d };
13247
+ }
13248
+ } else if (t === "response.reasoning_summary_text.delta" || t === "response.reasoning_text.delta") {
13249
+ const d = event.delta ?? "";
13250
+ if (d.length > 0) yield { type: "reasoning_delta", text: d };
13251
+ } else if (t === "response.output_item.added" && event.item?.type === "function_call") {
13252
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
13253
+ pending[id] = {
13254
+ callId: event.item.call_id ?? id,
13255
+ name: event.item.name ?? "",
13256
+ args: event.item.arguments ?? ""
13257
+ };
13258
+ } else if (t === "response.function_call_arguments.delta") {
13259
+ const c = event.item_id !== void 0 ? pending[event.item_id] : void 0;
13260
+ if (c !== void 0) c.args += event.delta ?? "";
13261
+ } else if (t === "response.output_item.done" && event.item?.type === "function_call") {
13262
+ const id = event.item.id ?? event.item.call_id ?? "call-0";
13263
+ const c = pending[id] ?? {
13264
+ callId: event.item.call_id ?? id,
13265
+ name: event.item.name ?? "",
13266
+ args: event.item.arguments ?? ""
13267
+ };
13268
+ const rawArgs = event.item.arguments ?? c.args;
13269
+ const call = {
13270
+ type: "tool_use",
13271
+ id: c.callId,
13272
+ name: c.name.length > 0 ? c.name : event.item.name ?? "",
13273
+ input: parseToolArguments(rawArgs)
13274
+ };
13275
+ toolCalls.push(call);
13276
+ delete pending[id];
13277
+ yield { type: "tool_use", id: call.id, name: call.name, input: call.input };
13278
+ } else if (t === "response.completed" || t === "response.incomplete") {
13279
+ const usage = event.response?.usage;
13280
+ if (usage !== void 0) {
13281
+ inputTokens = usage.input_tokens;
13282
+ outputTokens = usage.output_tokens;
13283
+ reasoningTokens = usage.output_tokens_details?.reasoning_tokens;
13284
+ }
13285
+ stopReason = t === "response.incomplete" ? "max_tokens" : "end_turn";
13286
+ } else if (t === "response.failed" || t === "error") {
13287
+ const msg = event.response?.error?.message ?? event.message ?? "responses stream failed";
13288
+ yield { type: "error", message: msg };
13289
+ throw mapOpenAICompatibleError({
13290
+ providerId,
13291
+ status: 502,
13292
+ body: { error: { message: msg } },
13293
+ headers: response.headers,
13294
+ endpoint: "/responses"
13295
+ });
13296
+ }
13297
+ }
13298
+ }
13299
+ if (toolCalls.length > 0 && stopReason === "end_turn") stopReason = "tool_use";
13300
+ yield { type: "stop", reason: stopReason };
13301
+ return makeLlmFinish({
13302
+ stopReason,
13303
+ text,
13304
+ toolCalls,
13305
+ inputTokens,
13306
+ outputTokens,
13307
+ reasoningTokens
13308
+ });
13309
+ }
13310
+ };
13311
+
13117
13312
  // src/internal/llm/vertex-anthropic.ts
13118
13313
  init_errors();
13119
13314
 
@@ -13516,6 +13711,14 @@ function selectTransport(profile, apiKey) {
13516
13711
  const realKey = apiKey === "__bedrock_lazy_token__" ? void 0 : apiKey;
13517
13712
  return new BedrockAnthropicClient(realKey !== void 0 ? { apiKey: realKey } : {});
13518
13713
  }
13714
+ if (profile.apiMode === "responses_api") {
13715
+ return new ResponsesApiClient({
13716
+ apiKey,
13717
+ ...profile.baseUrl !== void 0 ? { baseUrl: profile.baseUrl } : {},
13718
+ ...profile.extraHeaders !== void 0 ? { extraHeaders: profile.extraHeaders } : {},
13719
+ providerName: profile.name
13720
+ });
13721
+ }
13519
13722
  throw new ConfigurationError(
13520
13723
  `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".`,
13521
13724
  { code: "transport_unavailable" }