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