@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/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { T as TheokitAgentError, B as BudgetOptions, a as BudgetHandle, b as BudgetSnapshot, E as ErrorMetadata } from './errors-gE8612p9.cjs';
2
2
  export { A as AgentDisposedError, c as AgentRunError, d as AgentRunErrorCode, e as AuthenticationError, f as BudgetExceedEvent, g as BudgetExceededError, h as BudgetLimit, i as BudgetMode, j as BudgetScope, k as BudgetThresholdEvent, l as BudgetWindow, C as ConfigurationError, m as ErrorCode, I as IntegrationNotConnectedError, n as InvalidTaskIdError, M as MemoryAdapterError, o as MemoryAdapterErrorCode, N as NetworkError, R as RateLimitError, p as TaskNotFoundError, U as UnknownAgentError, q as UnsupportedBudgetOperationError, r as UnsupportedRunOperationError, s as UnsupportedTaskOperationError, t as isTransientError } from './errors-gE8612p9.cjs';
3
- import { A as AgentOptions, L as LocalOptions, P as ProviderRoutingSettings, S as SystemPromptResolver, C as CloudOptions, M as MemorySettings, a as AgentDefinition, b as ContextSettings, c as PluginsSettings, d as SkillsSettings, e as SDKAgent, f as ListAgentsOptions, g as ListResult, h as SDKAgentInfo, G as GetAgentOptions, i as ListRunsOptions, j as GetRunOptions, k as AgentOperationOptions, l as Plugin$1, m as ProviderProfile, I as InlineSkill, B as BudgetTracker, n as MemoryProvider, o as MemoryId, p as PreToolCallDecision, q as StepResult, r as SDKProvider } from './cron-26WH1n7o.cjs';
4
- export { s as ActiveMemoryPassArgs, t as ActiveMemoryPassResult, u as AgentMemory, v as BudgetCheck, w as BudgetTotal, x as BudgetUsageEvent, y as CloudEnv, z as CloudRepo, D as ContextBudget, E as ContextManagerKind, F as ContextSnapshot, H as ContextSource, J as ContextSourceStatus, K as CreateSkillSpec, N as Cron, O as CronCreateOptions, Q as CronGetOptions, R as CronJob, T as CronJobStatus, U as CronListOptions, V as CronOperationOptions, W as CronRunOptions, X as CronRuntime, Y as CronSchedulerStatus, Z as CronStartOptions, _ as GoalEvent, $ as GoalOptions, a0 as GoalResult, a1 as HookName, a2 as InvalidateCacheOptions, a3 as MemoryAdapter, a4 as MemoryAdapterCapabilities, a5 as MemoryContext, a6 as MemoryFact, a7 as MemoryProviderHandle, a8 as MemoryProviderInitOptions, a9 as MemoryRevision, aa as MemoryToolSchema, ab as MemoryTurnMessage, ac as PersonalityPreset, ad as PluginContext, ae as PostAssistantReplyContext, af as PreToolCallContext, ag as PreUserSendContext, ah as PreUserSendResult, ai as ProviderCapability, aj as ProviderRoute, ak as RecordSessionSummaryArgs, al as ResolvedProviderRoute, am as RunUntilIterator, an as SDKAgentPlugins, ao as SDKAgentSkillDetail, ap as SDKAgentSkills, aq as SDKArtifact, ar as SDKContextManager, as as SDKPluginMetadata, at as SDKProvidersManager, au as SessionRecord, av as SessionStore, aw as SettingSource, ax as Skill, ay as SkillsResolver, az as SkillsResolverContext, aA as SystemPromptContext, aB as SystemPromptMemoryFact, aC as SystemPromptSkillRef, aD as TelemetrySettings } from './cron-26WH1n7o.cjs';
3
+ import { A as AgentOptions, L as LocalOptions, P as ProviderRoutingSettings, S as SystemPromptResolver, C as CloudOptions, M as MemorySettings, a as AgentDefinition, b as ContextSettings, c as PluginsSettings, d as SkillsSettings, e as SDKAgent, f as ListAgentsOptions, g as ListResult, h as SDKAgentInfo, G as GetAgentOptions, i as ListRunsOptions, j as GetRunOptions, k as AgentOperationOptions, l as Plugin$1, m as ProviderProfile, I as InlineSkill, B as BudgetTracker, n as MemoryProvider, o as MemoryId, p as PreToolCallDecision, q as StepResult, r as SDKProvider } from './cron-DXEhagMa.cjs';
4
+ export { s as ActiveMemoryPassArgs, t as ActiveMemoryPassResult, u as AgentMemory, v as BudgetCheck, w as BudgetTotal, x as BudgetUsageEvent, y as CloudEnv, z as CloudRepo, D as ContextBudget, E as ContextManagerKind, F as ContextSnapshot, H as ContextSource, J as ContextSourceStatus, K as CreateSkillSpec, N as Cron, O as CronCreateOptions, Q as CronGetOptions, R as CronJob, T as CronJobStatus, U as CronListOptions, V as CronOperationOptions, W as CronRunOptions, X as CronRuntime, Y as CronSchedulerStatus, Z as CronStartOptions, _ as GoalEvent, $ as GoalOptions, a0 as GoalResult, a1 as HookName, a2 as InvalidateCacheOptions, a3 as MemoryAdapter, a4 as MemoryAdapterCapabilities, a5 as MemoryContext, a6 as MemoryFact, a7 as MemoryProviderHandle, a8 as MemoryProviderInitOptions, a9 as MemoryRevision, aa as MemoryToolSchema, ab as MemoryTurnMessage, ac as PersonalityPreset, ad as PluginContext, ae as PostAssistantReplyContext, af as PreToolCallContext, ag as PreUserSendContext, ah as PreUserSendResult, ai as ProviderCapability, aj as ProviderRoute, ak as RecordSessionSummaryArgs, al as ResolvedProviderRoute, am as RunUntilIterator, an as SDKAgentPlugins, ao as SDKAgentSkillDetail, ap as SDKAgentSkills, aq as SDKArtifact, ar as SDKContextManager, as as SDKPluginMetadata, at as SDKProvidersManager, au as SessionRecord, av as SessionStore, aw as SettingSource, ax as Skill, ay as SkillsResolver, az as SkillsResolverContext, aA as SystemPromptContext, aB as SystemPromptMemoryFact, aC as SystemPromptSkillRef, aD as TelemetrySettings } from './cron-DXEhagMa.cjs';
5
5
  import { R as RunResult, M as ModelSelection, C as CustomTool, a as McpServerConfig, b as Run, P as Processor, T as ToolResultContentBlock, c as PermissionMode, d as PermissionEngine, e as RunEventSink, S as SDKMessage } from './run-DFM1H2jW.cjs';
6
6
  export { A as AgentConversationTurn, f as AssistantMessage, g as CompletionCheck, h as CompletionCheckResult, i as ConversationStep, j as ConversationTurn, k as CostBreakdown, l as CostSource, m as CostStatus, D as DoomLoopThresholds, G as GenerateOptions, n as GenerateRunResult, I as ImageBlock, o as InputProcessorContext, p as InteractionUpdate, q as McpAuthConfig, r as McpHttpServerConfig, s as McpOAuthConfig, t as McpStdioServerConfig, u as MessageOrigin, v as ModelParameterValue, O as OutputProcessorContext, w as PartialToolCallUpdate, x as PermissionAction, y as PermissionEngineOptions, z as PermissionRule, B as ProcessorControls, E as ProcessorTripwire, F as ProcessorViolation, H as RunCompactBoundaryEvent, J as RunCompletionCheckEvent, K as RunErrorDetail, L as RunEvent, N as RunGitInfo, Q as RunOperation, U as RunPermissionDeniedEvent, V as RunRateLimitEvent, W as RunStatus, X as RunTaskCompletedEvent, Y as RunTaskStartedEvent, Z as RunTaskUpdatedEvent, _ as RunToCompletionOptions, $ as RunToCompletionResult, a0 as RunToolProgressEvent, a1 as RunTripwireEvent, a2 as SDKAssistantMessage, a3 as SDKImage, a4 as SDKImageDimension, a5 as SDKObjectDelta, a6 as SDKRequestMessage, a7 as SDKStatusMessage, a8 as SDKSystemMessage, a9 as SDKTaskMessage, aa as SDKThinkingMessage, ab as SDKToolUseMessage, ac as SDKUserMessage, ad as SDKUserMessageEvent, ae as SendOptions, af as ShellCommand, ag as ShellConversationTurn, ah as ShellOutput, ai as ShellOutputDeltaUpdate, aj as StepCompletedUpdate, ak as StepStartedUpdate, al as StreamToCompletionResult, am as SummaryCompletedUpdate, an as SummaryStartedUpdate, ao as SummaryUpdate, ap as TextBlock, aq as TextDeltaUpdate, ar as ThinkingCompletedUpdate, as as ThinkingDeltaUpdate, at as ThinkingMessage, au as TokenDeltaUpdate, av as TokenUsage, aw as ToolCall, ax as ToolCallCompletedUpdate, ay as ToolCallStartedUpdate, az as ToolContextMessage, aA as ToolResult, aB as ToolResultGuardOptions, aC as ToolUseBlock, aD as TurnEndedUpdate, aE as UserMessage, aF as UserMessageAppendedUpdate, aG as applyMode, aH as emitRunEvent } from './run-DFM1H2jW.cjs';
7
7
  import * as zod from 'zod';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { T as TheokitAgentError, B as BudgetOptions, a as BudgetHandle, b as BudgetSnapshot, E as ErrorMetadata } from './errors-CG2RpeW-.js';
2
2
  export { A as AgentDisposedError, c as AgentRunError, d as AgentRunErrorCode, e as AuthenticationError, f as BudgetExceedEvent, g as BudgetExceededError, h as BudgetLimit, i as BudgetMode, j as BudgetScope, k as BudgetThresholdEvent, l as BudgetWindow, C as ConfigurationError, m as ErrorCode, I as IntegrationNotConnectedError, n as InvalidTaskIdError, M as MemoryAdapterError, o as MemoryAdapterErrorCode, N as NetworkError, R as RateLimitError, p as TaskNotFoundError, U as UnknownAgentError, q as UnsupportedBudgetOperationError, r as UnsupportedRunOperationError, s as UnsupportedTaskOperationError, t as isTransientError } from './errors-CG2RpeW-.js';
3
- import { A as AgentOptions, L as LocalOptions, P as ProviderRoutingSettings, S as SystemPromptResolver, C as CloudOptions, M as MemorySettings, a as AgentDefinition, b as ContextSettings, c as PluginsSettings, d as SkillsSettings, e as SDKAgent, f as ListAgentsOptions, g as ListResult, h as SDKAgentInfo, G as GetAgentOptions, i as ListRunsOptions, j as GetRunOptions, k as AgentOperationOptions, l as Plugin$1, m as ProviderProfile, I as InlineSkill, B as BudgetTracker, n as MemoryProvider, o as MemoryId, p as PreToolCallDecision, q as StepResult, r as SDKProvider } from './cron-D_0p_gXd.js';
4
- export { s as ActiveMemoryPassArgs, t as ActiveMemoryPassResult, u as AgentMemory, v as BudgetCheck, w as BudgetTotal, x as BudgetUsageEvent, y as CloudEnv, z as CloudRepo, D as ContextBudget, E as ContextManagerKind, F as ContextSnapshot, H as ContextSource, J as ContextSourceStatus, K as CreateSkillSpec, N as Cron, O as CronCreateOptions, Q as CronGetOptions, R as CronJob, T as CronJobStatus, U as CronListOptions, V as CronOperationOptions, W as CronRunOptions, X as CronRuntime, Y as CronSchedulerStatus, Z as CronStartOptions, _ as GoalEvent, $ as GoalOptions, a0 as GoalResult, a1 as HookName, a2 as InvalidateCacheOptions, a3 as MemoryAdapter, a4 as MemoryAdapterCapabilities, a5 as MemoryContext, a6 as MemoryFact, a7 as MemoryProviderHandle, a8 as MemoryProviderInitOptions, a9 as MemoryRevision, aa as MemoryToolSchema, ab as MemoryTurnMessage, ac as PersonalityPreset, ad as PluginContext, ae as PostAssistantReplyContext, af as PreToolCallContext, ag as PreUserSendContext, ah as PreUserSendResult, ai as ProviderCapability, aj as ProviderRoute, ak as RecordSessionSummaryArgs, al as ResolvedProviderRoute, am as RunUntilIterator, an as SDKAgentPlugins, ao as SDKAgentSkillDetail, ap as SDKAgentSkills, aq as SDKArtifact, ar as SDKContextManager, as as SDKPluginMetadata, at as SDKProvidersManager, au as SessionRecord, av as SessionStore, aw as SettingSource, ax as Skill, ay as SkillsResolver, az as SkillsResolverContext, aA as SystemPromptContext, aB as SystemPromptMemoryFact, aC as SystemPromptSkillRef, aD as TelemetrySettings } from './cron-D_0p_gXd.js';
3
+ import { A as AgentOptions, L as LocalOptions, P as ProviderRoutingSettings, S as SystemPromptResolver, C as CloudOptions, M as MemorySettings, a as AgentDefinition, b as ContextSettings, c as PluginsSettings, d as SkillsSettings, e as SDKAgent, f as ListAgentsOptions, g as ListResult, h as SDKAgentInfo, G as GetAgentOptions, i as ListRunsOptions, j as GetRunOptions, k as AgentOperationOptions, l as Plugin$1, m as ProviderProfile, I as InlineSkill, B as BudgetTracker, n as MemoryProvider, o as MemoryId, p as PreToolCallDecision, q as StepResult, r as SDKProvider } from './cron-BF_AI5NP.js';
4
+ export { s as ActiveMemoryPassArgs, t as ActiveMemoryPassResult, u as AgentMemory, v as BudgetCheck, w as BudgetTotal, x as BudgetUsageEvent, y as CloudEnv, z as CloudRepo, D as ContextBudget, E as ContextManagerKind, F as ContextSnapshot, H as ContextSource, J as ContextSourceStatus, K as CreateSkillSpec, N as Cron, O as CronCreateOptions, Q as CronGetOptions, R as CronJob, T as CronJobStatus, U as CronListOptions, V as CronOperationOptions, W as CronRunOptions, X as CronRuntime, Y as CronSchedulerStatus, Z as CronStartOptions, _ as GoalEvent, $ as GoalOptions, a0 as GoalResult, a1 as HookName, a2 as InvalidateCacheOptions, a3 as MemoryAdapter, a4 as MemoryAdapterCapabilities, a5 as MemoryContext, a6 as MemoryFact, a7 as MemoryProviderHandle, a8 as MemoryProviderInitOptions, a9 as MemoryRevision, aa as MemoryToolSchema, ab as MemoryTurnMessage, ac as PersonalityPreset, ad as PluginContext, ae as PostAssistantReplyContext, af as PreToolCallContext, ag as PreUserSendContext, ah as PreUserSendResult, ai as ProviderCapability, aj as ProviderRoute, ak as RecordSessionSummaryArgs, al as ResolvedProviderRoute, am as RunUntilIterator, an as SDKAgentPlugins, ao as SDKAgentSkillDetail, ap as SDKAgentSkills, aq as SDKArtifact, ar as SDKContextManager, as as SDKPluginMetadata, at as SDKProvidersManager, au as SessionRecord, av as SessionStore, aw as SettingSource, ax as Skill, ay as SkillsResolver, az as SkillsResolverContext, aA as SystemPromptContext, aB as SystemPromptMemoryFact, aC as SystemPromptSkillRef, aD as TelemetrySettings } from './cron-BF_AI5NP.js';
5
5
  import { R as RunResult, M as ModelSelection, C as CustomTool, a as McpServerConfig, b as Run, P as Processor, T as ToolResultContentBlock, c as PermissionMode, d as PermissionEngine, e as RunEventSink, S as SDKMessage } from './run-DFM1H2jW.js';
6
6
  export { A as AgentConversationTurn, f as AssistantMessage, g as CompletionCheck, h as CompletionCheckResult, i as ConversationStep, j as ConversationTurn, k as CostBreakdown, l as CostSource, m as CostStatus, D as DoomLoopThresholds, G as GenerateOptions, n as GenerateRunResult, I as ImageBlock, o as InputProcessorContext, p as InteractionUpdate, q as McpAuthConfig, r as McpHttpServerConfig, s as McpOAuthConfig, t as McpStdioServerConfig, u as MessageOrigin, v as ModelParameterValue, O as OutputProcessorContext, w as PartialToolCallUpdate, x as PermissionAction, y as PermissionEngineOptions, z as PermissionRule, B as ProcessorControls, E as ProcessorTripwire, F as ProcessorViolation, H as RunCompactBoundaryEvent, J as RunCompletionCheckEvent, K as RunErrorDetail, L as RunEvent, N as RunGitInfo, Q as RunOperation, U as RunPermissionDeniedEvent, V as RunRateLimitEvent, W as RunStatus, X as RunTaskCompletedEvent, Y as RunTaskStartedEvent, Z as RunTaskUpdatedEvent, _ as RunToCompletionOptions, $ as RunToCompletionResult, a0 as RunToolProgressEvent, a1 as RunTripwireEvent, a2 as SDKAssistantMessage, a3 as SDKImage, a4 as SDKImageDimension, a5 as SDKObjectDelta, a6 as SDKRequestMessage, a7 as SDKStatusMessage, a8 as SDKSystemMessage, a9 as SDKTaskMessage, aa as SDKThinkingMessage, ab as SDKToolUseMessage, ac as SDKUserMessage, ad as SDKUserMessageEvent, ae as SendOptions, af as ShellCommand, ag as ShellConversationTurn, ah as ShellOutput, ai as ShellOutputDeltaUpdate, aj as StepCompletedUpdate, ak as StepStartedUpdate, al as StreamToCompletionResult, am as SummaryCompletedUpdate, an as SummaryStartedUpdate, ao as SummaryUpdate, ap as TextBlock, aq as TextDeltaUpdate, ar as ThinkingCompletedUpdate, as as ThinkingDeltaUpdate, at as ThinkingMessage, au as TokenDeltaUpdate, av as TokenUsage, aw as ToolCall, ax as ToolCallCompletedUpdate, ay as ToolCallStartedUpdate, az as ToolContextMessage, aA as ToolResult, aB as ToolResultGuardOptions, aC as ToolUseBlock, aD as TurnEndedUpdate, aE as UserMessage, aF as UserMessageAppendedUpdate, aG as applyMode, aH as emitRunEvent } from './run-DFM1H2jW.js';
7
7
  import * as zod from 'zod';
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 body = { model: request.model, input, stream: true, store: false };
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({ stopReason, text, toolCalls, inputTokens, outputTokens, reasoningTokens });
15733
- }
15734
- };
15735
-
15736
- // src/internal/llm/pool-aware-client.ts
15737
- init_errors();
15738
-
15739
- // src/internal/resilience/circuit-breaker.ts
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();
@@ -16270,6 +16279,9 @@ function resolveApiKey2(envVars) {
16270
16279
  return void 0;
16271
16280
  }
16272
16281
  function selectTransport(profile, apiKey) {
16282
+ const transformCtx = { apiKey };
16283
+ const transformFetch = profile.transform?.fetch?.(transformCtx);
16284
+ const transformHeaders = profile.transform?.headers?.(transformCtx);
16273
16285
  if (profile.apiMode === "chat_completions") {
16274
16286
  if (profile.name === "ollama") {
16275
16287
  const ollamaBase = process.env.OLLAMA_HOST ?? profile.baseUrl;
@@ -16286,6 +16298,7 @@ function selectTransport(profile, apiKey) {
16286
16298
  }
16287
16299
  const envOverride = resolveBaseUrlEnvOverride(profile.name);
16288
16300
  if (envOverride !== void 0) opts.baseUrl = envOverride;
16301
+ if (transformFetch !== void 0) opts.fetch = transformFetch;
16289
16302
  return new OpenAIClient(opts);
16290
16303
  }
16291
16304
  if (profile.apiMode === "anthropic_messages") {
@@ -16302,10 +16315,12 @@ function selectTransport(profile, apiKey) {
16302
16315
  return new BedrockAnthropicClient(realKey !== void 0 ? { apiKey: realKey } : {});
16303
16316
  }
16304
16317
  if (profile.apiMode === "responses_api") {
16318
+ const mergedHeaders = profile.extraHeaders !== void 0 || transformHeaders !== void 0 ? { ...profile.extraHeaders, ...transformHeaders } : void 0;
16305
16319
  return new ResponsesApiClient({
16306
16320
  apiKey,
16307
16321
  ...profile.baseUrl !== void 0 ? { baseUrl: profile.baseUrl } : {},
16308
- ...profile.extraHeaders !== void 0 ? { extraHeaders: profile.extraHeaders } : {},
16322
+ ...mergedHeaders !== void 0 ? { extraHeaders: mergedHeaders } : {},
16323
+ ...transformFetch !== void 0 ? { fetch: transformFetch } : {},
16309
16324
  providerName: profile.name
16310
16325
  });
16311
16326
  }