@theokit/agents 0.31.0 → 0.32.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.
@@ -21,7 +21,7 @@ import {
21
21
  getProjectContextConfig,
22
22
  getSkillsConfig,
23
23
  getSubAgents
24
- } from "./chunk-FI6ZG2YP.js";
24
+ } from "./chunk-3AX6M5TF.js";
25
25
  import {
26
26
  __name
27
27
  } from "./chunk-7QVYU63E.js";
@@ -1555,6 +1555,10 @@ __name(agent, "agent");
1555
1555
  function createHitlPlugin(wiring) {
1556
1556
  return {
1557
1557
  name: "theokit-hitl",
1558
+ version: "1.0.0",
1559
+ // `kind: 'general'` is load-bearing — without it the SDK's isCodePlugin() drops this plugin and
1560
+ // the HITL veto never fires (the run would proceed WITHOUT waiting for human approval).
1561
+ kind: "general",
1558
1562
  register(ctx) {
1559
1563
  ctx.on("pre_tool_call", async (c) => {
1560
1564
  const opts = wiring.gated.get(c.name);
@@ -1567,12 +1571,25 @@ function createHitlPlugin(wiring) {
1567
1571
  question: opts.question,
1568
1572
  input: c.args,
1569
1573
  callbackUrl: `approve/${approvalId}`,
1570
- timeoutMs: opts.timeout ?? 3e5
1574
+ timeoutMs: opts.timeout ?? 3e5,
1575
+ // M20 — carry the declared custom-payload schema so the UI knows what to collect.
1576
+ ...opts.payloadSchema !== void 0 ? {
1577
+ payloadSchema: opts.payloadSchema
1578
+ } : {}
1571
1579
  });
1572
- const approved = await wiring.awaitApproval(approvalId, opts, c.name);
1573
- return approved ? void 0 : {
1580
+ const raw = await wiring.awaitApproval(approvalId, opts, c.name);
1581
+ const decision = typeof raw === "boolean" ? {
1582
+ approved: raw
1583
+ } : raw;
1584
+ if (decision.approved) return void 0;
1585
+ let message = `Tool '${c.name}' denied by human approver`;
1586
+ if (decision.reason) message += `: ${decision.reason}`;
1587
+ if (decision.payload !== void 0) {
1588
+ message += ` (payload: ${JSON.stringify(decision.payload)})`;
1589
+ }
1590
+ return {
1574
1591
  block: true,
1575
- message: `Tool '${c.name}' denied by human approver`
1592
+ message
1576
1593
  };
1577
1594
  });
1578
1595
  }
@@ -2509,8 +2526,12 @@ __name(delegate, "delegate");
2509
2526
  function createToolHooksPlugin(hooks) {
2510
2527
  return {
2511
2528
  name: "theokit-tool-hooks",
2529
+ version: "1.0.0",
2530
+ // `kind: 'general'` is load-bearing — without it the SDK's isCodePlugin() drops this plugin and
2531
+ // no hook fires (M10/M19 latent bug, proven via a real OpenRouter run).
2532
+ kind: "general",
2512
2533
  register(ctx) {
2513
- const { beforeToolCall, afterToolCall, beforeLLMCall, afterLLMCall } = hooks;
2534
+ const { beforeToolCall, afterToolCall, beforeLLMCall, afterLLMCall, processInput } = hooks;
2514
2535
  if (beforeToolCall) {
2515
2536
  ctx.on("pre_tool_call", (c) => beforeToolCall({
2516
2537
  name: c.name ?? "",
@@ -2537,11 +2558,167 @@ function createToolHooksPlugin(hooks) {
2537
2558
  iteration: c.iteration
2538
2559
  }));
2539
2560
  }
2561
+ if (processInput) {
2562
+ ctx.on("pre_user_send", async (c) => {
2563
+ const injected = await processInput({
2564
+ prompt: c.prompt ?? "",
2565
+ agentId: c.agentId,
2566
+ runId: c.runId
2567
+ });
2568
+ return injected !== void 0 && injected.length > 0 ? {
2569
+ recalledContext: injected
2570
+ } : void 0;
2571
+ });
2572
+ }
2540
2573
  }
2541
2574
  };
2542
2575
  }
2543
2576
  __name(createToolHooksPlugin, "createToolHooksPlugin");
2544
2577
 
2578
+ // src/bridge/api-error-handler.ts
2579
+ var DEFAULT_MAX_ATTEMPTS = 3;
2580
+ async function runWithApiErrorHandling(thunk, policy) {
2581
+ const maxAttempts = policy.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
2582
+ let attempt = 0;
2583
+ for (; ; ) {
2584
+ attempt += 1;
2585
+ try {
2586
+ return await thunk();
2587
+ } catch (error) {
2588
+ const decision = await policy.processApiError({
2589
+ error,
2590
+ attempt
2591
+ });
2592
+ if (decision.retry && attempt < maxAttempts) continue;
2593
+ if (!decision.retry && "fallback" in decision && decision.fallback !== void 0) {
2594
+ return decision.fallback;
2595
+ }
2596
+ throw error;
2597
+ }
2598
+ }
2599
+ }
2600
+ __name(runWithApiErrorHandling, "runWithApiErrorHandling");
2601
+ function createApiErrorHandler(policy) {
2602
+ return (thunk) => runWithApiErrorHandling(thunk, policy);
2603
+ }
2604
+ __name(createApiErrorHandler, "createApiErrorHandler");
2605
+
2606
+ // src/bridge/delegation-scoring.ts
2607
+ function delegateBackground(subAgent, message, opts = {}) {
2608
+ const { delegateFn = delegate, ...delegateOpts } = opts;
2609
+ let isSettled = false;
2610
+ const promise = delegateFn(subAgent, message, delegateOpts).finally(() => {
2611
+ isSettled = true;
2612
+ });
2613
+ promise.catch(() => void 0);
2614
+ return {
2615
+ wait: /* @__PURE__ */ __name(() => promise, "wait"),
2616
+ settled: /* @__PURE__ */ __name(() => isSettled, "settled")
2617
+ };
2618
+ }
2619
+ __name(delegateBackground, "delegateBackground");
2620
+ var DEFAULT_MAX_ROUNDS = 3;
2621
+ function defaultFeedbackTemplate(message, feedback) {
2622
+ return `${message}
2623
+
2624
+ Feedback from the reviewer (address this): ${feedback}`;
2625
+ }
2626
+ __name(defaultFeedbackTemplate, "defaultFeedbackTemplate");
2627
+ async function delegateWithScoring(subAgent, message, opts) {
2628
+ const { scorer, maxRounds: maxRoundsOpt = DEFAULT_MAX_ROUNDS, delegateFn = delegate, feedbackTemplate = defaultFeedbackTemplate, ...delegateOpts } = opts;
2629
+ const maxRounds = Math.max(1, maxRoundsOpt);
2630
+ const verdicts = [];
2631
+ let currentMessage = message;
2632
+ let lastResult;
2633
+ for (let round = 1; round <= maxRounds; round++) {
2634
+ const result = await delegateFn(subAgent, currentMessage, delegateOpts);
2635
+ lastResult = result;
2636
+ const verdict = await scorer(result);
2637
+ verdicts.push(verdict);
2638
+ if (verdict.pass) {
2639
+ return {
2640
+ result,
2641
+ rounds: round,
2642
+ passed: true,
2643
+ verdicts
2644
+ };
2645
+ }
2646
+ if (verdict.feedback) currentMessage = feedbackTemplate(message, verdict.feedback);
2647
+ }
2648
+ return {
2649
+ result: lastResult,
2650
+ rounds: maxRounds,
2651
+ passed: false,
2652
+ verdicts
2653
+ };
2654
+ }
2655
+ __name(delegateWithScoring, "delegateWithScoring");
2656
+
2657
+ // src/bridge/mcp-resolver.ts
2658
+ async function resolveMcpServers(selection, ctx) {
2659
+ if (selection === void 0) return void 0;
2660
+ if (typeof selection !== "function") return selection;
2661
+ const resolved = await selection(ctx);
2662
+ if (typeof resolved !== "object" || resolved === null) {
2663
+ throw new Error("[@theokit/agents] MCP resolver must return an McpServersMap object");
2664
+ }
2665
+ return resolved;
2666
+ }
2667
+ __name(resolveMcpServers, "resolveMcpServers");
2668
+ function mcpRegistry(config) {
2669
+ const registry = config.registry;
2670
+ if (registry === "composio") {
2671
+ const apps = config.apps ?? [];
2672
+ return {
2673
+ composio: {
2674
+ command: "npx",
2675
+ args: [
2676
+ "-y",
2677
+ "@composio/mcp",
2678
+ ...apps.length > 0 ? [
2679
+ "--apps",
2680
+ apps.join(",")
2681
+ ] : []
2682
+ ],
2683
+ env: {
2684
+ COMPOSIO_API_KEY: config.apiKey
2685
+ }
2686
+ }
2687
+ };
2688
+ }
2689
+ if (registry === "mcp.run") {
2690
+ return {
2691
+ "mcp.run": {
2692
+ command: "npx",
2693
+ args: [
2694
+ "-y",
2695
+ "@mcp.run/cli",
2696
+ "serve",
2697
+ ...config.profile ? [
2698
+ "--profile",
2699
+ config.profile
2700
+ ] : []
2701
+ ],
2702
+ env: {
2703
+ MCP_RUN_API_KEY: config.apiKey
2704
+ }
2705
+ }
2706
+ };
2707
+ }
2708
+ throw new Error(`mcpRegistry: unknown registry ${JSON.stringify(registry)} (supported: 'composio', 'mcp.run').`);
2709
+ }
2710
+ __name(mcpRegistry, "mcpRegistry");
2711
+ function mcpToolApprovals(specs) {
2712
+ const out = {};
2713
+ for (const [tool, spec] of Object.entries(specs)) {
2714
+ out[tool] = typeof spec === "string" ? {
2715
+ question: spec
2716
+ } : spec;
2717
+ }
2718
+ return out;
2719
+ }
2720
+ __name(mcpToolApprovals, "mcpToolApprovals");
2721
+
2545
2722
  // src/manifest/agent-manifest.ts
2546
2723
  function generateAgentManifest(walkResults) {
2547
2724
  return {
@@ -2729,7 +2906,14 @@ export {
2729
2906
  AgentRunnerBuilder,
2730
2907
  delegate,
2731
2908
  createToolHooksPlugin,
2909
+ runWithApiErrorHandling,
2910
+ createApiErrorHandler,
2911
+ delegateBackground,
2912
+ delegateWithScoring,
2913
+ resolveMcpServers,
2914
+ mcpRegistry,
2915
+ mcpToolApprovals,
2732
2916
  generateAgentManifest,
2733
2917
  agentsPlugin
2734
2918
  };
2735
- //# sourceMappingURL=chunk-BWYBOMKR.js.map
2919
+ //# sourceMappingURL=chunk-5VIRIPVV.js.map