@theokit/sdk 4.9.0 → 4.10.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/cron.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- export { N as Cron } from './cron-26WH1n7o.cjs';
1
+ export { N as Cron } from './cron-DXEhagMa.cjs';
2
2
  import './run-DFM1H2jW.cjs';
3
3
  import 'zod';
package/dist/cron.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { N as Cron } from './cron-D_0p_gXd.js';
1
+ export { N as Cron } from './cron-BF_AI5NP.js';
2
2
  import './run-DFM1H2jW.js';
3
3
  import 'zod';
package/dist/cron.js CHANGED
@@ -12950,6 +12950,172 @@ function assistantMessage(message) {
12950
12950
  return result;
12951
12951
  }
12952
12952
 
12953
+ // src/internal/llm/pool-aware-client.ts
12954
+ init_errors();
12955
+
12956
+ // src/internal/resilience/circuit-breaker.ts
12957
+ var DEFAULT_MAX_TIMEOUTS = 3;
12958
+ var DEFAULT_COOLDOWN_MS2 = 6e4;
12959
+ var CircuitBreaker = class {
12960
+ constructor(opts = {}) {
12961
+ this.opts = opts;
12962
+ }
12963
+ opts;
12964
+ states = /* @__PURE__ */ new Map();
12965
+ /** @returns true when the breaker is open and the call should be skipped. */
12966
+ shouldSkip(key) {
12967
+ const state3 = this.states.get(key);
12968
+ if (state3 === void 0) return false;
12969
+ if (state3.cooldownUntilMs === 0) return false;
12970
+ if (this.now() < state3.cooldownUntilMs) return true;
12971
+ state3.cooldownUntilMs = 0;
12972
+ state3.consecutiveTimeouts = 0;
12973
+ return false;
12974
+ }
12975
+ recordSuccess(key) {
12976
+ const state3 = this.states.get(key);
12977
+ if (state3 === void 0) return;
12978
+ state3.consecutiveTimeouts = 0;
12979
+ state3.cooldownUntilMs = 0;
12980
+ }
12981
+ recordTimeout(key) {
12982
+ const state3 = this.states.get(key) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
12983
+ state3.consecutiveTimeouts += 1;
12984
+ if (state3.consecutiveTimeouts >= (this.opts.maxTimeouts ?? DEFAULT_MAX_TIMEOUTS)) {
12985
+ state3.cooldownUntilMs = this.now() + (this.opts.cooldownMs ?? DEFAULT_COOLDOWN_MS2);
12986
+ }
12987
+ this.states.set(key, state3);
12988
+ }
12989
+ /** @internal — tests inspect counter state. */
12990
+ inspect(key) {
12991
+ return this.states.get(key) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
12992
+ }
12993
+ now() {
12994
+ return this.opts.now?.() ?? Date.now();
12995
+ }
12996
+ };
12997
+
12998
+ // src/internal/llm/pool-aware-client.ts
12999
+ init_retry();
13000
+ var PoolAwareLlmClient = class {
13001
+ constructor(pool, buildClient2, waitForAvailableMs = 3e4, resilience = {}) {
13002
+ this.pool = pool;
13003
+ this.buildClient = buildClient2;
13004
+ this.waitForAvailableMs = waitForAvailableMs;
13005
+ this.name = `pool-aware:${pool.provider}`;
13006
+ this.breaker = resilience.breaker ?? new CircuitBreaker();
13007
+ this.backoffBaseMs = resilience.backoffBaseMs;
13008
+ this.rng = resilience.rng;
13009
+ this.onRateLimit = resilience.onRateLimit;
13010
+ }
13011
+ pool;
13012
+ buildClient;
13013
+ waitForAvailableMs;
13014
+ name;
13015
+ onRateLimit;
13016
+ /** M2 #60 — provider-level circuit breaker (consecutive-failure). */
13017
+ breaker;
13018
+ backoffBaseMs;
13019
+ rng;
13020
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: stream() must serialize pool-select → build client → first-event probe → classify → retry/rotate/propagate. Extracting helpers fragments the linear narrative; the comments above each branch keep it readable.
13021
+ async *stream(request, signal) {
13022
+ if (this.breaker.shouldSkip(this.pool.provider)) {
13023
+ throw new NetworkError(`${this.pool.provider} circuit open \u2014 failing fast`, {
13024
+ code: "circuit_open"
13025
+ });
13026
+ }
13027
+ let hasRetried429 = false;
13028
+ while (true) {
13029
+ if (signal.aborted) throw abortError2(signal);
13030
+ let entry = await this.pool.select();
13031
+ if (entry === null) {
13032
+ if (this.waitForAvailableMs > 0) {
13033
+ const available = await this.pool.waitForAvailable(signal, {
13034
+ maxWaitMs: this.waitForAvailableMs
13035
+ });
13036
+ if (available) {
13037
+ entry = await this.pool.select();
13038
+ }
13039
+ }
13040
+ if (entry === null) {
13041
+ this.breaker.recordTimeout(this.pool.provider);
13042
+ throw new CredentialPoolExhaustedError(
13043
+ `All ${this.pool.provider} credentials exhausted; next retry available at ${this.nextRetryHint() ?? "unknown"}`,
13044
+ { provider: this.pool.provider, nextRetryAt: this.nextRetryHint() }
13045
+ );
13046
+ }
13047
+ }
13048
+ const realClient = this.buildClient(entry.accessToken);
13049
+ const attempt = await tryFirstEvent(realClient, request, signal);
13050
+ if (attempt.kind === "ok") {
13051
+ this.breaker.recordSuccess(this.pool.provider);
13052
+ return yield* relayStream(attempt.generator, attempt.firstResult);
13053
+ }
13054
+ const decision = classifyAndDecide(attempt.error, hasRetried429);
13055
+ if (decision === "retry") {
13056
+ const backoffMs = computeBackoffMs({
13057
+ attempt: 0,
13058
+ ...this.backoffBaseMs !== void 0 ? { baseMs: this.backoffBaseMs } : {},
13059
+ ...this.rng !== void 0 ? { rng: this.rng } : {}
13060
+ });
13061
+ this.onRateLimit?.({ attempt: 1, retryAfterMs: backoffMs });
13062
+ await sleepWithAbort(backoffMs, signal);
13063
+ hasRetried429 = true;
13064
+ continue;
13065
+ }
13066
+ if (decision === "rotate") {
13067
+ try {
13068
+ await this.pool.markExhaustedAndRotate({
13069
+ entryId: entry.id,
13070
+ statusCode: attempt.error.metadata?.statusCode ?? inferStatusCode(attempt.error),
13071
+ ...parseRetryAfterMs(attempt.error) !== void 0 ? { resetAtMs: parseRetryAfterMs(attempt.error) } : {}
13072
+ });
13073
+ } catch (persistErr) {
13074
+ process.stderr.write(
13075
+ `[theokit-sdk] credential-pool: persist failed during rotate; continuing in-memory: ${persistErr instanceof Error ? persistErr.message : String(persistErr)}
13076
+ `
13077
+ );
13078
+ }
13079
+ hasRetried429 = false;
13080
+ continue;
13081
+ }
13082
+ this.breaker.recordTimeout(this.pool.provider);
13083
+ throw attempt.error;
13084
+ }
13085
+ }
13086
+ /**
13087
+ * Earliest epoch ms among entries' `lastErrorResetAt` — best estimate
13088
+ * for the caller's `CredentialPoolExhaustedError.nextRetryAt`.
13089
+ */
13090
+ nextRetryHint() {
13091
+ const resets = this.pool.list().map((e) => e.lastErrorResetAt).filter((v) => v !== void 0);
13092
+ return resets.length === 0 ? void 0 : Math.min(...resets);
13093
+ }
13094
+ };
13095
+ function classifyAndDecide(error, hasRetried429) {
13096
+ if (error instanceof NetworkError) return "propagate";
13097
+ if (error instanceof AuthenticationError) return "rotate";
13098
+ const status = error.metadata?.statusCode ?? 429;
13099
+ if (status === 402) return "rotate";
13100
+ return hasRetried429 ? "rotate" : "retry";
13101
+ }
13102
+ function parseRetryAfterMs(error) {
13103
+ const seconds = error.metadata?.retryAfter;
13104
+ if (typeof seconds === "number" && seconds > 0) {
13105
+ return Date.now() + seconds * 1e3;
13106
+ }
13107
+ return void 0;
13108
+ }
13109
+ function inferStatusCode(error) {
13110
+ if (error instanceof AuthenticationError) return 401;
13111
+ if (error instanceof RateLimitError) return 429;
13112
+ return 0;
13113
+ }
13114
+ function abortError2(signal) {
13115
+ if (signal.reason instanceof Error) return signal.reason;
13116
+ return new Error("AbortError");
13117
+ }
13118
+
12953
13119
  // src/internal/llm/responses.ts
12954
13120
  function messageToInputItems(message) {
12955
13121
  const items = [];
@@ -12998,7 +13164,9 @@ function buildResponsesBody(request) {
12998
13164
  for (const message of request.messages) {
12999
13165
  for (const item of messageToInputItems(message)) input.push(item);
13000
13166
  }
13001
- const body = { model: request.model, input, stream: true, store: false };
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 };
13002
13170
  const instructions = collapseSystemText(request.system);
13003
13171
  if (instructions.length > 0) body.instructions = instructions;
13004
13172
  if (request.maxTokens !== void 0) body.max_output_tokens = request.maxTokens;
@@ -13132,175 +13300,16 @@ var ResponsesApiClient = class {
13132
13300
  }
13133
13301
  if (toolCalls.length > 0 && stopReason === "end_turn") stopReason = "tool_use";
13134
13302
  yield { type: "stop", reason: stopReason };
13135
- return makeLlmFinish({ stopReason, text, toolCalls, inputTokens, outputTokens, reasoningTokens });
13136
- }
13137
- };
13138
-
13139
- // src/internal/llm/pool-aware-client.ts
13140
- init_errors();
13141
-
13142
- // src/internal/resilience/circuit-breaker.ts
13143
- var DEFAULT_MAX_TIMEOUTS = 3;
13144
- var DEFAULT_COOLDOWN_MS2 = 6e4;
13145
- var CircuitBreaker = class {
13146
- constructor(opts = {}) {
13147
- this.opts = opts;
13148
- }
13149
- opts;
13150
- states = /* @__PURE__ */ new Map();
13151
- /** @returns true when the breaker is open and the call should be skipped. */
13152
- shouldSkip(key) {
13153
- const state3 = this.states.get(key);
13154
- if (state3 === void 0) return false;
13155
- if (state3.cooldownUntilMs === 0) return false;
13156
- if (this.now() < state3.cooldownUntilMs) return true;
13157
- state3.cooldownUntilMs = 0;
13158
- state3.consecutiveTimeouts = 0;
13159
- return false;
13160
- }
13161
- recordSuccess(key) {
13162
- const state3 = this.states.get(key);
13163
- if (state3 === void 0) return;
13164
- state3.consecutiveTimeouts = 0;
13165
- state3.cooldownUntilMs = 0;
13166
- }
13167
- recordTimeout(key) {
13168
- const state3 = this.states.get(key) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
13169
- state3.consecutiveTimeouts += 1;
13170
- if (state3.consecutiveTimeouts >= (this.opts.maxTimeouts ?? DEFAULT_MAX_TIMEOUTS)) {
13171
- state3.cooldownUntilMs = this.now() + (this.opts.cooldownMs ?? DEFAULT_COOLDOWN_MS2);
13172
- }
13173
- this.states.set(key, state3);
13174
- }
13175
- /** @internal — tests inspect counter state. */
13176
- inspect(key) {
13177
- return this.states.get(key) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
13178
- }
13179
- now() {
13180
- return this.opts.now?.() ?? Date.now();
13181
- }
13182
- };
13183
-
13184
- // src/internal/llm/pool-aware-client.ts
13185
- init_retry();
13186
- var PoolAwareLlmClient = class {
13187
- constructor(pool, buildClient2, waitForAvailableMs = 3e4, resilience = {}) {
13188
- this.pool = pool;
13189
- this.buildClient = buildClient2;
13190
- this.waitForAvailableMs = waitForAvailableMs;
13191
- this.name = `pool-aware:${pool.provider}`;
13192
- this.breaker = resilience.breaker ?? new CircuitBreaker();
13193
- this.backoffBaseMs = resilience.backoffBaseMs;
13194
- this.rng = resilience.rng;
13195
- this.onRateLimit = resilience.onRateLimit;
13196
- }
13197
- pool;
13198
- buildClient;
13199
- waitForAvailableMs;
13200
- name;
13201
- onRateLimit;
13202
- /** M2 #60 — provider-level circuit breaker (consecutive-failure). */
13203
- breaker;
13204
- backoffBaseMs;
13205
- rng;
13206
- // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: stream() must serialize pool-select → build client → first-event probe → classify → retry/rotate/propagate. Extracting helpers fragments the linear narrative; the comments above each branch keep it readable.
13207
- async *stream(request, signal) {
13208
- if (this.breaker.shouldSkip(this.pool.provider)) {
13209
- throw new NetworkError(`${this.pool.provider} circuit open \u2014 failing fast`, {
13210
- code: "circuit_open"
13211
- });
13212
- }
13213
- let hasRetried429 = false;
13214
- while (true) {
13215
- if (signal.aborted) throw abortError2(signal);
13216
- let entry = await this.pool.select();
13217
- if (entry === null) {
13218
- if (this.waitForAvailableMs > 0) {
13219
- const available = await this.pool.waitForAvailable(signal, {
13220
- maxWaitMs: this.waitForAvailableMs
13221
- });
13222
- if (available) {
13223
- entry = await this.pool.select();
13224
- }
13225
- }
13226
- if (entry === null) {
13227
- this.breaker.recordTimeout(this.pool.provider);
13228
- throw new CredentialPoolExhaustedError(
13229
- `All ${this.pool.provider} credentials exhausted; next retry available at ${this.nextRetryHint() ?? "unknown"}`,
13230
- { provider: this.pool.provider, nextRetryAt: this.nextRetryHint() }
13231
- );
13232
- }
13233
- }
13234
- const realClient = this.buildClient(entry.accessToken);
13235
- const attempt = await tryFirstEvent(realClient, request, signal);
13236
- if (attempt.kind === "ok") {
13237
- this.breaker.recordSuccess(this.pool.provider);
13238
- return yield* relayStream(attempt.generator, attempt.firstResult);
13239
- }
13240
- const decision = classifyAndDecide(attempt.error, hasRetried429);
13241
- if (decision === "retry") {
13242
- const backoffMs = computeBackoffMs({
13243
- attempt: 0,
13244
- ...this.backoffBaseMs !== void 0 ? { baseMs: this.backoffBaseMs } : {},
13245
- ...this.rng !== void 0 ? { rng: this.rng } : {}
13246
- });
13247
- this.onRateLimit?.({ attempt: 1, retryAfterMs: backoffMs });
13248
- await sleepWithAbort(backoffMs, signal);
13249
- hasRetried429 = true;
13250
- continue;
13251
- }
13252
- if (decision === "rotate") {
13253
- try {
13254
- await this.pool.markExhaustedAndRotate({
13255
- entryId: entry.id,
13256
- statusCode: attempt.error.metadata?.statusCode ?? inferStatusCode(attempt.error),
13257
- ...parseRetryAfterMs(attempt.error) !== void 0 ? { resetAtMs: parseRetryAfterMs(attempt.error) } : {}
13258
- });
13259
- } catch (persistErr) {
13260
- process.stderr.write(
13261
- `[theokit-sdk] credential-pool: persist failed during rotate; continuing in-memory: ${persistErr instanceof Error ? persistErr.message : String(persistErr)}
13262
- `
13263
- );
13264
- }
13265
- hasRetried429 = false;
13266
- continue;
13267
- }
13268
- this.breaker.recordTimeout(this.pool.provider);
13269
- throw attempt.error;
13270
- }
13271
- }
13272
- /**
13273
- * Earliest epoch ms among entries' `lastErrorResetAt` — best estimate
13274
- * for the caller's `CredentialPoolExhaustedError.nextRetryAt`.
13275
- */
13276
- nextRetryHint() {
13277
- const resets = this.pool.list().map((e) => e.lastErrorResetAt).filter((v) => v !== void 0);
13278
- return resets.length === 0 ? void 0 : Math.min(...resets);
13303
+ return makeLlmFinish({
13304
+ stopReason,
13305
+ text,
13306
+ toolCalls,
13307
+ inputTokens,
13308
+ outputTokens,
13309
+ reasoningTokens
13310
+ });
13279
13311
  }
13280
13312
  };
13281
- function classifyAndDecide(error, hasRetried429) {
13282
- if (error instanceof NetworkError) return "propagate";
13283
- if (error instanceof AuthenticationError) return "rotate";
13284
- const status = error.metadata?.statusCode ?? 429;
13285
- if (status === 402) return "rotate";
13286
- return hasRetried429 ? "rotate" : "retry";
13287
- }
13288
- function parseRetryAfterMs(error) {
13289
- const seconds = error.metadata?.retryAfter;
13290
- if (typeof seconds === "number" && seconds > 0) {
13291
- return Date.now() + seconds * 1e3;
13292
- }
13293
- return void 0;
13294
- }
13295
- function inferStatusCode(error) {
13296
- if (error instanceof AuthenticationError) return 401;
13297
- if (error instanceof RateLimitError) return 429;
13298
- return 0;
13299
- }
13300
- function abortError2(signal) {
13301
- if (signal.reason instanceof Error) return signal.reason;
13302
- return new Error("AbortError");
13303
- }
13304
13313
 
13305
13314
  // src/internal/llm/vertex-anthropic.ts
13306
13315
  init_errors();
@@ -13673,6 +13682,9 @@ function resolveApiKey2(envVars) {
13673
13682
  return void 0;
13674
13683
  }
13675
13684
  function selectTransport(profile, apiKey) {
13685
+ const transformCtx = { apiKey };
13686
+ const transformFetch = profile.transform?.fetch?.(transformCtx);
13687
+ const transformHeaders = profile.transform?.headers?.(transformCtx);
13676
13688
  if (profile.apiMode === "chat_completions") {
13677
13689
  if (profile.name === "ollama") {
13678
13690
  const ollamaBase = process.env.OLLAMA_HOST ?? profile.baseUrl;
@@ -13689,6 +13701,7 @@ function selectTransport(profile, apiKey) {
13689
13701
  }
13690
13702
  const envOverride = resolveBaseUrlEnvOverride(profile.name);
13691
13703
  if (envOverride !== void 0) opts.baseUrl = envOverride;
13704
+ if (transformFetch !== void 0) opts.fetch = transformFetch;
13692
13705
  return new OpenAIClient(opts);
13693
13706
  }
13694
13707
  if (profile.apiMode === "anthropic_messages") {
@@ -13705,10 +13718,12 @@ function selectTransport(profile, apiKey) {
13705
13718
  return new BedrockAnthropicClient(realKey !== void 0 ? { apiKey: realKey } : {});
13706
13719
  }
13707
13720
  if (profile.apiMode === "responses_api") {
13721
+ const mergedHeaders = profile.extraHeaders !== void 0 || transformHeaders !== void 0 ? { ...profile.extraHeaders, ...transformHeaders } : void 0;
13708
13722
  return new ResponsesApiClient({
13709
13723
  apiKey,
13710
13724
  ...profile.baseUrl !== void 0 ? { baseUrl: profile.baseUrl } : {},
13711
- ...profile.extraHeaders !== void 0 ? { extraHeaders: profile.extraHeaders } : {},
13725
+ ...mergedHeaders !== void 0 ? { extraHeaders: mergedHeaders } : {},
13726
+ ...transformFetch !== void 0 ? { fetch: transformFetch } : {},
13712
13727
  providerName: profile.name
13713
13728
  });
13714
13729
  }