@theokit/sdk 3.2.0 → 3.2.3

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/a2a/index.js CHANGED
@@ -4740,7 +4740,14 @@ var init_env_policy = __esm({
4740
4740
  /[_-]PWD/i,
4741
4741
  /CREDENTIAL/i,
4742
4742
  /PRIVATE/i,
4743
- /_AUTH/i
4743
+ /_AUTH/i,
4744
+ // #54-a — value-embedded-secret conventions (no generic `*_URL` — see keep-list test).
4745
+ /DSN/i,
4746
+ /WEBHOOK/i,
4747
+ /COOKIE/i,
4748
+ /CONNECTION[_-]?STRING/i,
4749
+ // Known DB / message-broker connection-string vars (carry `user:pass@`).
4750
+ /(?:^|[_-])(?:DATABASE|DB|REDIS|MONGO(?:DB)?|POSTGRES(?:QL)?|MYSQL|MARIADB|AMQP|RABBITMQ|CLICKHOUSE|ELASTIC(?:SEARCH)?|CASSANDRA|COUCHDB|MEMCACHED|NATS|KAFKA)[_-]?(?:URL|URI|DSN|CONNECTION)/i
4744
4751
  ];
4745
4752
  CORE_VARS = [
4746
4753
  "PATH",
@@ -5149,12 +5156,23 @@ var init_objective_coerce = __esm({
5149
5156
  // src/internal/persistence/pagination.ts
5150
5157
  function paginate(items, opts) {
5151
5158
  if (opts === void 0 || opts.offset === void 0 && opts.limit === void 0) return items;
5152
- const start = Math.max(0, opts.offset ?? 0);
5153
- const end = opts.limit === void 0 ? items.length : start + Math.max(0, opts.limit);
5159
+ const start = opts.offset === void 0 ? 0 : requireNonNegativeInt(opts.offset, "offset");
5160
+ const limit = opts.limit === void 0 ? void 0 : requireNonNegativeInt(opts.limit, "limit");
5161
+ const end = limit === void 0 ? items.length : start + limit;
5154
5162
  return items.slice(start, end);
5155
5163
  }
5164
+ function requireNonNegativeInt(value, field) {
5165
+ if (!Number.isInteger(value) || value < 0) {
5166
+ throw new ConfigurationError(
5167
+ `Invalid pagination ${field}: expected a non-negative integer, got ${value}`,
5168
+ { code: "pagination_invalid" }
5169
+ );
5170
+ }
5171
+ return value;
5172
+ }
5156
5173
  var init_pagination = __esm({
5157
5174
  "src/internal/persistence/pagination.ts"() {
5175
+ init_errors();
5158
5176
  }
5159
5177
  });
5160
5178
 
@@ -9495,7 +9513,10 @@ async function runAgentLoop(inputs) {
9495
9513
  }
9496
9514
  budget.consume();
9497
9515
  inputs.budgetTracker?.nextIteration?.();
9498
- if (inputs.signal?.aborted === true) break;
9516
+ if (inputs.signal?.aborted === true) {
9517
+ ctx.finalStatus = "cancelled";
9518
+ break;
9519
+ }
9499
9520
  }
9500
9521
  if (lastTurnDecision === "continue" && budget.shouldContinue() === false) {
9501
9522
  ctx.stoppedAtIterationLimit = true;
@@ -9680,15 +9701,15 @@ async function guardAndTransformToolResults(inputs, raw, ctx) {
9680
9701
  }
9681
9702
  async function continueOrTerminate(inputs, ctx, llmOutput) {
9682
9703
  if (llmOutput.errored) return "error";
9683
- if (llmOutput.text.length > 0) {
9684
- await emitAssistantTextStep(inputs, ctx, llmOutput.text);
9704
+ const tCtx = { agentId: inputs.agentId, runId: inputs.runId };
9705
+ const text = llmOutput.text.length > 0 ? await transformLlmOutputText(inputs, llmOutput.text, tCtx) : llmOutput.text;
9706
+ if (text.length > 0) {
9707
+ await emitAssistantTextStep(inputs, ctx, text);
9685
9708
  }
9686
9709
  if (llmOutput.stopReason !== "tool_use" || llmOutput.toolCalls.length === 0) {
9687
9710
  return finishOrReflect(inputs, ctx, llmOutput);
9688
9711
  }
9689
- const tCtx = { agentId: inputs.agentId, runId: inputs.runId };
9690
- const outText = await transformLlmOutputText(inputs, llmOutput.text, tCtx);
9691
- ctx.messages.push(buildAssistantTurn(outText, llmOutput.toolCalls));
9712
+ ctx.messages.push(buildAssistantTurn(text, llmOutput.toolCalls));
9692
9713
  const rawResults = await dispatchTools(
9693
9714
  // SE12 — forward a read-only text projection of the transcript-so-far to tool
9694
9715
  // handlers via `ctx.messages` (consumed by defineSubAgent's messageFilter).
@@ -10152,6 +10173,8 @@ function parseRetryAfter(headers) {
10152
10173
  if (raw === null) return void 0;
10153
10174
  const n = Number(raw);
10154
10175
  if (Number.isFinite(n) && n >= 0) return Math.ceil(n);
10176
+ const dateMs = Date.parse(raw);
10177
+ if (Number.isFinite(dateMs)) return Math.max(0, Math.ceil((dateMs - Date.now()) / 1e3));
10155
10178
  return void 0;
10156
10179
  }
10157
10180
  function truncateRaw(body) {
@@ -10472,6 +10495,7 @@ function buildAnthropicBody(request) {
10472
10495
  var AnthropicClient, AnthropicStreamAccumulator;
10473
10496
  var init_anthropic3 = __esm({
10474
10497
  "src/internal/llm/anthropic.ts"() {
10498
+ init_errors();
10475
10499
  init_anthropic2();
10476
10500
  init_anthropic_shared();
10477
10501
  init_finish();
@@ -10525,12 +10549,23 @@ var init_anthropic3 = __esm({
10525
10549
  const events = accumulator.consume(parsed);
10526
10550
  for (const event of events) yield event;
10527
10551
  }
10552
+ if (!accumulator.finishReasonSeen) {
10553
+ throw new NetworkError("Anthropic SSE stream truncated (no stop_reason)", {
10554
+ code: "stream_truncated"
10555
+ });
10556
+ }
10528
10557
  return accumulator.finish();
10529
10558
  }
10530
10559
  };
10531
10560
  AnthropicStreamAccumulator = class {
10532
10561
  text = "";
10533
10562
  stopReason = "end_turn";
10563
+ /**
10564
+ * M2 #61 — whether a `message_delta` carrying a real `stop_reason` was seen.
10565
+ * A stream that closes before it is a truncation (server FIN / proxy hiccup),
10566
+ * not a clean `end_turn` — the caller throws `stream_truncated` on `false`.
10567
+ */
10568
+ sawStopReason = false;
10534
10569
  inputTokens;
10535
10570
  outputTokens;
10536
10571
  cacheReadTokens;
@@ -10577,6 +10612,9 @@ var init_anthropic3 = __esm({
10577
10612
  * spinning the SSE parser.
10578
10613
  */
10579
10614
  handleMessageDelta(md) {
10615
+ if (md.delta.stop_reason !== void 0 && md.delta.stop_reason !== null) {
10616
+ this.sawStopReason = true;
10617
+ }
10580
10618
  this.stopReason = mapAnthropicStopReason(md.delta.stop_reason);
10581
10619
  if (md.usage?.input_tokens !== void 0) this.inputTokens = md.usage.input_tokens;
10582
10620
  if (md.usage?.output_tokens !== void 0) this.outputTokens = md.usage.output_tokens;
@@ -10587,6 +10625,10 @@ var init_anthropic3 = __esm({
10587
10625
  this.cacheReadTokens = md.usage.cache_read_input_tokens;
10588
10626
  }
10589
10627
  }
10628
+ /** M2 #61 — whether the terminal `message_delta` (stop_reason) was seen. */
10629
+ get finishReasonSeen() {
10630
+ return this.sawStopReason;
10631
+ }
10590
10632
  finish() {
10591
10633
  const toolCalls = [];
10592
10634
  for (const [index, tool] of this.toolCalls.entries()) {
@@ -12820,7 +12862,6 @@ var init_client = __esm({
12820
12862
  // concurrent request so parallel tool dispatch after a drop awaits one handshake
12821
12863
  // instead of racing (or spuriously failing with mcp_not_init).
12822
12864
  dropped = false;
12823
- reconnectAttempts = 0;
12824
12865
  reconnectPromise;
12825
12866
  get timeoutMs() {
12826
12867
  return this.config.requestTimeoutMs ?? DEFAULT_MCP_TIMEOUT_MS;
@@ -12873,15 +12914,22 @@ var init_client = __esm({
12873
12914
  return this.reconnectPromise;
12874
12915
  }
12875
12916
  async reconnect() {
12876
- if (this.reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
12877
- throw new NetworkError(`MCP ${this.name} reconnect exhausted`, { code: "mcp_disconnected" });
12917
+ let lastErr;
12918
+ for (let attempt = 0; attempt < MAX_RECONNECT_ATTEMPTS; attempt += 1) {
12919
+ await reconnectDelay(attempt);
12920
+ this.spawnChild();
12921
+ try {
12922
+ await super.initialize();
12923
+ this.dropped = false;
12924
+ return;
12925
+ } catch (err) {
12926
+ lastErr = err;
12927
+ }
12878
12928
  }
12879
- await reconnectDelay(this.reconnectAttempts);
12880
- this.reconnectAttempts += 1;
12881
- this.spawnChild();
12882
- await super.initialize();
12883
- this.dropped = false;
12884
- this.reconnectAttempts = 0;
12929
+ throw new NetworkError(`MCP ${this.name} reconnect exhausted`, {
12930
+ code: "mcp_disconnected",
12931
+ ...lastErr instanceof Error ? { cause: lastErr } : {}
12932
+ });
12885
12933
  }
12886
12934
  async close() {
12887
12935
  this.rejectAllPending(new NetworkError(`MCP ${this.name} closed`, { code: "mcp_closed" }));
@@ -13012,7 +13060,12 @@ var init_client = __esm({
13012
13060
  code: "mcp_http_error"
13013
13061
  });
13014
13062
  }
13015
- return await response.json();
13063
+ try {
13064
+ return await response.json();
13065
+ } catch (cause) {
13066
+ if (isAbortLike(cause)) throw mcpTimeoutError(this.name, timeoutMs);
13067
+ throw cause;
13068
+ }
13016
13069
  }
13017
13070
  };
13018
13071
  }
@@ -13252,9 +13305,11 @@ function declarativeSubagentTools(agentOptions, parentTools) {
13252
13305
  return subAgentToolsFromDefinitions(agents2, parentTools);
13253
13306
  }
13254
13307
  function bindParentCredentials(tools, agentOptions) {
13308
+ const parentPlugins = Array.isArray(agentOptions.plugins) ? agentOptions.plugins : void 0;
13255
13309
  const credentials = {
13256
13310
  ...agentOptions.apiKey !== void 0 ? { apiKey: agentOptions.apiKey } : {},
13257
- ...typeof agentOptions.model === "object" ? { model: agentOptions.model } : {}
13311
+ ...typeof agentOptions.model === "object" ? { model: agentOptions.model } : {},
13312
+ ...parentPlugins !== void 0 ? { plugins: parentPlugins } : {}
13258
13313
  };
13259
13314
  for (const tool of tools) inheritSubAgentCredentials(tool, credentials);
13260
13315
  }
@@ -15779,10 +15834,11 @@ var init_local_agent_memory = __esm({
15779
15834
  }
15780
15835
  buildTelemetryRecallArgs(telemetry) {
15781
15836
  const userId = typeof this.options.memoryContext?.userId === "string" ? this.options.memoryContext.userId : void 0;
15837
+ const tenantId = typeof this.options.memoryContext?.tenantId === "string" ? this.options.memoryContext.tenantId : void 0;
15782
15838
  return {
15783
15839
  ...telemetry !== void 0 ? { telemetry } : {},
15784
15840
  ...userId !== void 0 ? { userId } : {},
15785
- namespace: "default",
15841
+ namespace: tenantId ?? "default",
15786
15842
  scope: "session"
15787
15843
  };
15788
15844
  }
@@ -19116,15 +19172,19 @@ async function collectChildToolResults(run) {
19116
19172
  ${lines.join("\n")}
19117
19173
  </subagent-tool-results>`;
19118
19174
  }
19119
- async function runChildAgent(spec, input, signal, maxSteps, inherited) {
19120
- const { Agent: Agent2 } = await Promise.resolve().then(() => (init_agent(), agent_exports));
19175
+ function buildChildCreateOptions(spec, inherited) {
19121
19176
  const model = spec.model ? { id: spec.model } : inherited?.model;
19122
- const agent = await Agent2.create({
19177
+ return {
19123
19178
  ...inherited?.apiKey !== void 0 ? { apiKey: inherited.apiKey } : {},
19124
19179
  ...model !== void 0 ? { model } : {},
19180
+ ...inherited?.plugins !== void 0 ? { plugins: inherited.plugins } : {},
19125
19181
  systemPrompt: spec.instructions,
19126
19182
  tools: spec.tools ?? []
19127
- });
19183
+ };
19184
+ }
19185
+ async function runChildAgent(spec, input, signal, maxSteps, inherited) {
19186
+ const { Agent: Agent2 } = await Promise.resolve().then(() => (init_agent(), agent_exports));
19187
+ const agent = await Agent2.create(buildChildCreateOptions(spec, inherited));
19128
19188
  try {
19129
19189
  const sendOptions = {
19130
19190
  ...signal !== void 0 ? { signal } : {},