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