@theokit/sdk 4.9.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 +177 -168
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.js +177 -168
- package/dist/cron.js.map +1 -1
- package/dist/eval.cjs +177 -168
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +177 -168
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +177 -168
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +177 -168
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -15547,6 +15547,172 @@ function assistantMessage(message) {
|
|
|
15547
15547
|
return result;
|
|
15548
15548
|
}
|
|
15549
15549
|
|
|
15550
|
+
// src/internal/llm/pool-aware-client.ts
|
|
15551
|
+
init_errors();
|
|
15552
|
+
|
|
15553
|
+
// src/internal/resilience/circuit-breaker.ts
|
|
15554
|
+
var DEFAULT_MAX_TIMEOUTS = 3;
|
|
15555
|
+
var DEFAULT_COOLDOWN_MS2 = 6e4;
|
|
15556
|
+
var CircuitBreaker = class {
|
|
15557
|
+
constructor(opts = {}) {
|
|
15558
|
+
this.opts = opts;
|
|
15559
|
+
}
|
|
15560
|
+
opts;
|
|
15561
|
+
states = /* @__PURE__ */ new Map();
|
|
15562
|
+
/** @returns true when the breaker is open and the call should be skipped. */
|
|
15563
|
+
shouldSkip(key2) {
|
|
15564
|
+
const state4 = this.states.get(key2);
|
|
15565
|
+
if (state4 === void 0) return false;
|
|
15566
|
+
if (state4.cooldownUntilMs === 0) return false;
|
|
15567
|
+
if (this.now() < state4.cooldownUntilMs) return true;
|
|
15568
|
+
state4.cooldownUntilMs = 0;
|
|
15569
|
+
state4.consecutiveTimeouts = 0;
|
|
15570
|
+
return false;
|
|
15571
|
+
}
|
|
15572
|
+
recordSuccess(key2) {
|
|
15573
|
+
const state4 = this.states.get(key2);
|
|
15574
|
+
if (state4 === void 0) return;
|
|
15575
|
+
state4.consecutiveTimeouts = 0;
|
|
15576
|
+
state4.cooldownUntilMs = 0;
|
|
15577
|
+
}
|
|
15578
|
+
recordTimeout(key2) {
|
|
15579
|
+
const state4 = this.states.get(key2) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
|
|
15580
|
+
state4.consecutiveTimeouts += 1;
|
|
15581
|
+
if (state4.consecutiveTimeouts >= (this.opts.maxTimeouts ?? DEFAULT_MAX_TIMEOUTS)) {
|
|
15582
|
+
state4.cooldownUntilMs = this.now() + (this.opts.cooldownMs ?? DEFAULT_COOLDOWN_MS2);
|
|
15583
|
+
}
|
|
15584
|
+
this.states.set(key2, state4);
|
|
15585
|
+
}
|
|
15586
|
+
/** @internal — tests inspect counter state. */
|
|
15587
|
+
inspect(key2) {
|
|
15588
|
+
return this.states.get(key2) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
|
|
15589
|
+
}
|
|
15590
|
+
now() {
|
|
15591
|
+
return this.opts.now?.() ?? Date.now();
|
|
15592
|
+
}
|
|
15593
|
+
};
|
|
15594
|
+
|
|
15595
|
+
// src/internal/llm/pool-aware-client.ts
|
|
15596
|
+
init_retry();
|
|
15597
|
+
var PoolAwareLlmClient = class {
|
|
15598
|
+
constructor(pool, buildClient2, waitForAvailableMs = 3e4, resilience = {}) {
|
|
15599
|
+
this.pool = pool;
|
|
15600
|
+
this.buildClient = buildClient2;
|
|
15601
|
+
this.waitForAvailableMs = waitForAvailableMs;
|
|
15602
|
+
this.name = `pool-aware:${pool.provider}`;
|
|
15603
|
+
this.breaker = resilience.breaker ?? new CircuitBreaker();
|
|
15604
|
+
this.backoffBaseMs = resilience.backoffBaseMs;
|
|
15605
|
+
this.rng = resilience.rng;
|
|
15606
|
+
this.onRateLimit = resilience.onRateLimit;
|
|
15607
|
+
}
|
|
15608
|
+
pool;
|
|
15609
|
+
buildClient;
|
|
15610
|
+
waitForAvailableMs;
|
|
15611
|
+
name;
|
|
15612
|
+
onRateLimit;
|
|
15613
|
+
/** M2 #60 — provider-level circuit breaker (consecutive-failure). */
|
|
15614
|
+
breaker;
|
|
15615
|
+
backoffBaseMs;
|
|
15616
|
+
rng;
|
|
15617
|
+
// 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.
|
|
15618
|
+
async *stream(request, signal) {
|
|
15619
|
+
if (this.breaker.shouldSkip(this.pool.provider)) {
|
|
15620
|
+
throw new NetworkError(`${this.pool.provider} circuit open \u2014 failing fast`, {
|
|
15621
|
+
code: "circuit_open"
|
|
15622
|
+
});
|
|
15623
|
+
}
|
|
15624
|
+
let hasRetried429 = false;
|
|
15625
|
+
while (true) {
|
|
15626
|
+
if (signal.aborted) throw abortError2(signal);
|
|
15627
|
+
let entry = await this.pool.select();
|
|
15628
|
+
if (entry === null) {
|
|
15629
|
+
if (this.waitForAvailableMs > 0) {
|
|
15630
|
+
const available = await this.pool.waitForAvailable(signal, {
|
|
15631
|
+
maxWaitMs: this.waitForAvailableMs
|
|
15632
|
+
});
|
|
15633
|
+
if (available) {
|
|
15634
|
+
entry = await this.pool.select();
|
|
15635
|
+
}
|
|
15636
|
+
}
|
|
15637
|
+
if (entry === null) {
|
|
15638
|
+
this.breaker.recordTimeout(this.pool.provider);
|
|
15639
|
+
throw new CredentialPoolExhaustedError(
|
|
15640
|
+
`All ${this.pool.provider} credentials exhausted; next retry available at ${this.nextRetryHint() ?? "unknown"}`,
|
|
15641
|
+
{ provider: this.pool.provider, nextRetryAt: this.nextRetryHint() }
|
|
15642
|
+
);
|
|
15643
|
+
}
|
|
15644
|
+
}
|
|
15645
|
+
const realClient = this.buildClient(entry.accessToken);
|
|
15646
|
+
const attempt = await tryFirstEvent(realClient, request, signal);
|
|
15647
|
+
if (attempt.kind === "ok") {
|
|
15648
|
+
this.breaker.recordSuccess(this.pool.provider);
|
|
15649
|
+
return yield* relayStream(attempt.generator, attempt.firstResult);
|
|
15650
|
+
}
|
|
15651
|
+
const decision = classifyAndDecide(attempt.error, hasRetried429);
|
|
15652
|
+
if (decision === "retry") {
|
|
15653
|
+
const backoffMs = computeBackoffMs({
|
|
15654
|
+
attempt: 0,
|
|
15655
|
+
...this.backoffBaseMs !== void 0 ? { baseMs: this.backoffBaseMs } : {},
|
|
15656
|
+
...this.rng !== void 0 ? { rng: this.rng } : {}
|
|
15657
|
+
});
|
|
15658
|
+
this.onRateLimit?.({ attempt: 1, retryAfterMs: backoffMs });
|
|
15659
|
+
await sleepWithAbort(backoffMs, signal);
|
|
15660
|
+
hasRetried429 = true;
|
|
15661
|
+
continue;
|
|
15662
|
+
}
|
|
15663
|
+
if (decision === "rotate") {
|
|
15664
|
+
try {
|
|
15665
|
+
await this.pool.markExhaustedAndRotate({
|
|
15666
|
+
entryId: entry.id,
|
|
15667
|
+
statusCode: attempt.error.metadata?.statusCode ?? inferStatusCode(attempt.error),
|
|
15668
|
+
...parseRetryAfterMs(attempt.error) !== void 0 ? { resetAtMs: parseRetryAfterMs(attempt.error) } : {}
|
|
15669
|
+
});
|
|
15670
|
+
} catch (persistErr) {
|
|
15671
|
+
process.stderr.write(
|
|
15672
|
+
`[theokit-sdk] credential-pool: persist failed during rotate; continuing in-memory: ${persistErr instanceof Error ? persistErr.message : String(persistErr)}
|
|
15673
|
+
`
|
|
15674
|
+
);
|
|
15675
|
+
}
|
|
15676
|
+
hasRetried429 = false;
|
|
15677
|
+
continue;
|
|
15678
|
+
}
|
|
15679
|
+
this.breaker.recordTimeout(this.pool.provider);
|
|
15680
|
+
throw attempt.error;
|
|
15681
|
+
}
|
|
15682
|
+
}
|
|
15683
|
+
/**
|
|
15684
|
+
* Earliest epoch ms among entries' `lastErrorResetAt` — best estimate
|
|
15685
|
+
* for the caller's `CredentialPoolExhaustedError.nextRetryAt`.
|
|
15686
|
+
*/
|
|
15687
|
+
nextRetryHint() {
|
|
15688
|
+
const resets = this.pool.list().map((e) => e.lastErrorResetAt).filter((v) => v !== void 0);
|
|
15689
|
+
return resets.length === 0 ? void 0 : Math.min(...resets);
|
|
15690
|
+
}
|
|
15691
|
+
};
|
|
15692
|
+
function classifyAndDecide(error, hasRetried429) {
|
|
15693
|
+
if (error instanceof NetworkError) return "propagate";
|
|
15694
|
+
if (error instanceof AuthenticationError) return "rotate";
|
|
15695
|
+
const status = error.metadata?.statusCode ?? 429;
|
|
15696
|
+
if (status === 402) return "rotate";
|
|
15697
|
+
return hasRetried429 ? "rotate" : "retry";
|
|
15698
|
+
}
|
|
15699
|
+
function parseRetryAfterMs(error) {
|
|
15700
|
+
const seconds = error.metadata?.retryAfter;
|
|
15701
|
+
if (typeof seconds === "number" && seconds > 0) {
|
|
15702
|
+
return Date.now() + seconds * 1e3;
|
|
15703
|
+
}
|
|
15704
|
+
return void 0;
|
|
15705
|
+
}
|
|
15706
|
+
function inferStatusCode(error) {
|
|
15707
|
+
if (error instanceof AuthenticationError) return 401;
|
|
15708
|
+
if (error instanceof RateLimitError) return 429;
|
|
15709
|
+
return 0;
|
|
15710
|
+
}
|
|
15711
|
+
function abortError2(signal) {
|
|
15712
|
+
if (signal.reason instanceof Error) return signal.reason;
|
|
15713
|
+
return new Error("AbortError");
|
|
15714
|
+
}
|
|
15715
|
+
|
|
15550
15716
|
// src/internal/llm/responses.ts
|
|
15551
15717
|
function messageToInputItems(message) {
|
|
15552
15718
|
const items = [];
|
|
@@ -15595,7 +15761,9 @@ function buildResponsesBody(request) {
|
|
|
15595
15761
|
for (const message of request.messages) {
|
|
15596
15762
|
for (const item of messageToInputItems(message)) input.push(item);
|
|
15597
15763
|
}
|
|
15598
|
-
const
|
|
15764
|
+
const slash = request.model.lastIndexOf("/");
|
|
15765
|
+
const model = slash >= 0 ? request.model.slice(slash + 1) : request.model;
|
|
15766
|
+
const body = { model, input, stream: true, store: false };
|
|
15599
15767
|
const instructions = collapseSystemText(request.system);
|
|
15600
15768
|
if (instructions.length > 0) body.instructions = instructions;
|
|
15601
15769
|
if (request.maxTokens !== void 0) body.max_output_tokens = request.maxTokens;
|
|
@@ -15729,175 +15897,16 @@ var ResponsesApiClient = class {
|
|
|
15729
15897
|
}
|
|
15730
15898
|
if (toolCalls.length > 0 && stopReason === "end_turn") stopReason = "tool_use";
|
|
15731
15899
|
yield { type: "stop", reason: stopReason };
|
|
15732
|
-
return makeLlmFinish({
|
|
15733
|
-
|
|
15734
|
-
|
|
15735
|
-
|
|
15736
|
-
|
|
15737
|
-
|
|
15738
|
-
|
|
15739
|
-
|
|
15740
|
-
var DEFAULT_MAX_TIMEOUTS = 3;
|
|
15741
|
-
var DEFAULT_COOLDOWN_MS2 = 6e4;
|
|
15742
|
-
var CircuitBreaker = class {
|
|
15743
|
-
constructor(opts = {}) {
|
|
15744
|
-
this.opts = opts;
|
|
15745
|
-
}
|
|
15746
|
-
opts;
|
|
15747
|
-
states = /* @__PURE__ */ new Map();
|
|
15748
|
-
/** @returns true when the breaker is open and the call should be skipped. */
|
|
15749
|
-
shouldSkip(key2) {
|
|
15750
|
-
const state4 = this.states.get(key2);
|
|
15751
|
-
if (state4 === void 0) return false;
|
|
15752
|
-
if (state4.cooldownUntilMs === 0) return false;
|
|
15753
|
-
if (this.now() < state4.cooldownUntilMs) return true;
|
|
15754
|
-
state4.cooldownUntilMs = 0;
|
|
15755
|
-
state4.consecutiveTimeouts = 0;
|
|
15756
|
-
return false;
|
|
15757
|
-
}
|
|
15758
|
-
recordSuccess(key2) {
|
|
15759
|
-
const state4 = this.states.get(key2);
|
|
15760
|
-
if (state4 === void 0) return;
|
|
15761
|
-
state4.consecutiveTimeouts = 0;
|
|
15762
|
-
state4.cooldownUntilMs = 0;
|
|
15763
|
-
}
|
|
15764
|
-
recordTimeout(key2) {
|
|
15765
|
-
const state4 = this.states.get(key2) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
|
|
15766
|
-
state4.consecutiveTimeouts += 1;
|
|
15767
|
-
if (state4.consecutiveTimeouts >= (this.opts.maxTimeouts ?? DEFAULT_MAX_TIMEOUTS)) {
|
|
15768
|
-
state4.cooldownUntilMs = this.now() + (this.opts.cooldownMs ?? DEFAULT_COOLDOWN_MS2);
|
|
15769
|
-
}
|
|
15770
|
-
this.states.set(key2, state4);
|
|
15771
|
-
}
|
|
15772
|
-
/** @internal — tests inspect counter state. */
|
|
15773
|
-
inspect(key2) {
|
|
15774
|
-
return this.states.get(key2) ?? { consecutiveTimeouts: 0, cooldownUntilMs: 0 };
|
|
15775
|
-
}
|
|
15776
|
-
now() {
|
|
15777
|
-
return this.opts.now?.() ?? Date.now();
|
|
15778
|
-
}
|
|
15779
|
-
};
|
|
15780
|
-
|
|
15781
|
-
// src/internal/llm/pool-aware-client.ts
|
|
15782
|
-
init_retry();
|
|
15783
|
-
var PoolAwareLlmClient = class {
|
|
15784
|
-
constructor(pool, buildClient2, waitForAvailableMs = 3e4, resilience = {}) {
|
|
15785
|
-
this.pool = pool;
|
|
15786
|
-
this.buildClient = buildClient2;
|
|
15787
|
-
this.waitForAvailableMs = waitForAvailableMs;
|
|
15788
|
-
this.name = `pool-aware:${pool.provider}`;
|
|
15789
|
-
this.breaker = resilience.breaker ?? new CircuitBreaker();
|
|
15790
|
-
this.backoffBaseMs = resilience.backoffBaseMs;
|
|
15791
|
-
this.rng = resilience.rng;
|
|
15792
|
-
this.onRateLimit = resilience.onRateLimit;
|
|
15793
|
-
}
|
|
15794
|
-
pool;
|
|
15795
|
-
buildClient;
|
|
15796
|
-
waitForAvailableMs;
|
|
15797
|
-
name;
|
|
15798
|
-
onRateLimit;
|
|
15799
|
-
/** M2 #60 — provider-level circuit breaker (consecutive-failure). */
|
|
15800
|
-
breaker;
|
|
15801
|
-
backoffBaseMs;
|
|
15802
|
-
rng;
|
|
15803
|
-
// 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.
|
|
15804
|
-
async *stream(request, signal) {
|
|
15805
|
-
if (this.breaker.shouldSkip(this.pool.provider)) {
|
|
15806
|
-
throw new NetworkError(`${this.pool.provider} circuit open \u2014 failing fast`, {
|
|
15807
|
-
code: "circuit_open"
|
|
15808
|
-
});
|
|
15809
|
-
}
|
|
15810
|
-
let hasRetried429 = false;
|
|
15811
|
-
while (true) {
|
|
15812
|
-
if (signal.aborted) throw abortError2(signal);
|
|
15813
|
-
let entry = await this.pool.select();
|
|
15814
|
-
if (entry === null) {
|
|
15815
|
-
if (this.waitForAvailableMs > 0) {
|
|
15816
|
-
const available = await this.pool.waitForAvailable(signal, {
|
|
15817
|
-
maxWaitMs: this.waitForAvailableMs
|
|
15818
|
-
});
|
|
15819
|
-
if (available) {
|
|
15820
|
-
entry = await this.pool.select();
|
|
15821
|
-
}
|
|
15822
|
-
}
|
|
15823
|
-
if (entry === null) {
|
|
15824
|
-
this.breaker.recordTimeout(this.pool.provider);
|
|
15825
|
-
throw new CredentialPoolExhaustedError(
|
|
15826
|
-
`All ${this.pool.provider} credentials exhausted; next retry available at ${this.nextRetryHint() ?? "unknown"}`,
|
|
15827
|
-
{ provider: this.pool.provider, nextRetryAt: this.nextRetryHint() }
|
|
15828
|
-
);
|
|
15829
|
-
}
|
|
15830
|
-
}
|
|
15831
|
-
const realClient = this.buildClient(entry.accessToken);
|
|
15832
|
-
const attempt = await tryFirstEvent(realClient, request, signal);
|
|
15833
|
-
if (attempt.kind === "ok") {
|
|
15834
|
-
this.breaker.recordSuccess(this.pool.provider);
|
|
15835
|
-
return yield* relayStream(attempt.generator, attempt.firstResult);
|
|
15836
|
-
}
|
|
15837
|
-
const decision = classifyAndDecide(attempt.error, hasRetried429);
|
|
15838
|
-
if (decision === "retry") {
|
|
15839
|
-
const backoffMs = computeBackoffMs({
|
|
15840
|
-
attempt: 0,
|
|
15841
|
-
...this.backoffBaseMs !== void 0 ? { baseMs: this.backoffBaseMs } : {},
|
|
15842
|
-
...this.rng !== void 0 ? { rng: this.rng } : {}
|
|
15843
|
-
});
|
|
15844
|
-
this.onRateLimit?.({ attempt: 1, retryAfterMs: backoffMs });
|
|
15845
|
-
await sleepWithAbort(backoffMs, signal);
|
|
15846
|
-
hasRetried429 = true;
|
|
15847
|
-
continue;
|
|
15848
|
-
}
|
|
15849
|
-
if (decision === "rotate") {
|
|
15850
|
-
try {
|
|
15851
|
-
await this.pool.markExhaustedAndRotate({
|
|
15852
|
-
entryId: entry.id,
|
|
15853
|
-
statusCode: attempt.error.metadata?.statusCode ?? inferStatusCode(attempt.error),
|
|
15854
|
-
...parseRetryAfterMs(attempt.error) !== void 0 ? { resetAtMs: parseRetryAfterMs(attempt.error) } : {}
|
|
15855
|
-
});
|
|
15856
|
-
} catch (persistErr) {
|
|
15857
|
-
process.stderr.write(
|
|
15858
|
-
`[theokit-sdk] credential-pool: persist failed during rotate; continuing in-memory: ${persistErr instanceof Error ? persistErr.message : String(persistErr)}
|
|
15859
|
-
`
|
|
15860
|
-
);
|
|
15861
|
-
}
|
|
15862
|
-
hasRetried429 = false;
|
|
15863
|
-
continue;
|
|
15864
|
-
}
|
|
15865
|
-
this.breaker.recordTimeout(this.pool.provider);
|
|
15866
|
-
throw attempt.error;
|
|
15867
|
-
}
|
|
15868
|
-
}
|
|
15869
|
-
/**
|
|
15870
|
-
* Earliest epoch ms among entries' `lastErrorResetAt` — best estimate
|
|
15871
|
-
* for the caller's `CredentialPoolExhaustedError.nextRetryAt`.
|
|
15872
|
-
*/
|
|
15873
|
-
nextRetryHint() {
|
|
15874
|
-
const resets = this.pool.list().map((e) => e.lastErrorResetAt).filter((v) => v !== void 0);
|
|
15875
|
-
return resets.length === 0 ? void 0 : Math.min(...resets);
|
|
15900
|
+
return makeLlmFinish({
|
|
15901
|
+
stopReason,
|
|
15902
|
+
text,
|
|
15903
|
+
toolCalls,
|
|
15904
|
+
inputTokens,
|
|
15905
|
+
outputTokens,
|
|
15906
|
+
reasoningTokens
|
|
15907
|
+
});
|
|
15876
15908
|
}
|
|
15877
15909
|
};
|
|
15878
|
-
function classifyAndDecide(error, hasRetried429) {
|
|
15879
|
-
if (error instanceof NetworkError) return "propagate";
|
|
15880
|
-
if (error instanceof AuthenticationError) return "rotate";
|
|
15881
|
-
const status = error.metadata?.statusCode ?? 429;
|
|
15882
|
-
if (status === 402) return "rotate";
|
|
15883
|
-
return hasRetried429 ? "rotate" : "retry";
|
|
15884
|
-
}
|
|
15885
|
-
function parseRetryAfterMs(error) {
|
|
15886
|
-
const seconds = error.metadata?.retryAfter;
|
|
15887
|
-
if (typeof seconds === "number" && seconds > 0) {
|
|
15888
|
-
return Date.now() + seconds * 1e3;
|
|
15889
|
-
}
|
|
15890
|
-
return void 0;
|
|
15891
|
-
}
|
|
15892
|
-
function inferStatusCode(error) {
|
|
15893
|
-
if (error instanceof AuthenticationError) return 401;
|
|
15894
|
-
if (error instanceof RateLimitError) return 429;
|
|
15895
|
-
return 0;
|
|
15896
|
-
}
|
|
15897
|
-
function abortError2(signal) {
|
|
15898
|
-
if (signal.reason instanceof Error) return signal.reason;
|
|
15899
|
-
return new Error("AbortError");
|
|
15900
|
-
}
|
|
15901
15910
|
|
|
15902
15911
|
// src/internal/llm/vertex-anthropic.ts
|
|
15903
15912
|
init_errors();
|