@rarebit-one/harness-kernel 0.15.0 → 0.19.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.
Files changed (132) hide show
  1. package/README.md +268 -17
  2. package/dist/agent.d.ts +24 -0
  3. package/dist/agent.d.ts.map +1 -1
  4. package/dist/agent.js +138 -30
  5. package/dist/agent.js.map +1 -1
  6. package/dist/connectors/auth.d.ts +32 -0
  7. package/dist/connectors/auth.d.ts.map +1 -0
  8. package/dist/connectors/auth.js +85 -0
  9. package/dist/connectors/auth.js.map +1 -0
  10. package/dist/connectors/mcpClient.d.ts +14 -4
  11. package/dist/connectors/mcpClient.d.ts.map +1 -1
  12. package/dist/connectors/mcpClient.js +32 -11
  13. package/dist/connectors/mcpClient.js.map +1 -1
  14. package/dist/engines/acp.d.ts.map +1 -1
  15. package/dist/engines/acp.js +2 -1
  16. package/dist/engines/acp.js.map +1 -1
  17. package/dist/engines/agentSdk.d.ts +11 -0
  18. package/dist/engines/agentSdk.d.ts.map +1 -0
  19. package/dist/engines/agentSdk.js +26 -0
  20. package/dist/engines/agentSdk.js.map +1 -0
  21. package/dist/engines/agentSdkMissing.d.ts +6 -0
  22. package/dist/engines/agentSdkMissing.d.ts.map +1 -0
  23. package/dist/engines/agentSdkMissing.js +12 -0
  24. package/dist/engines/agentSdkMissing.js.map +1 -0
  25. package/dist/engines/capability.d.ts +1 -1
  26. package/dist/engines/capability.js +1 -1
  27. package/dist/engines/capabilityMcp.d.ts +14 -4
  28. package/dist/engines/capabilityMcp.d.ts.map +1 -1
  29. package/dist/engines/capabilityMcp.js +5 -3
  30. package/dist/engines/capabilityMcp.js.map +1 -1
  31. package/dist/engines/claudeCode.d.ts.map +1 -1
  32. package/dist/engines/claudeCode.js +2 -1
  33. package/dist/engines/claudeCode.js.map +1 -1
  34. package/dist/engines/claudeCodeDriver.d.ts +4 -3
  35. package/dist/engines/claudeCodeDriver.d.ts.map +1 -1
  36. package/dist/engines/claudeCodeDriver.js +13 -8
  37. package/dist/engines/claudeCodeDriver.js.map +1 -1
  38. package/dist/engines/codex.d.ts +4 -4
  39. package/dist/engines/codex.d.ts.map +1 -1
  40. package/dist/engines/codex.js +6 -5
  41. package/dist/engines/codex.js.map +1 -1
  42. package/dist/engines/codexDriver.d.ts +1 -1
  43. package/dist/engines/codexDriver.js +1 -1
  44. package/dist/engines/connectorMcp.d.ts +13 -0
  45. package/dist/engines/connectorMcp.d.ts.map +1 -1
  46. package/dist/engines/connectorMcp.js +23 -0
  47. package/dist/engines/connectorMcp.js.map +1 -1
  48. package/dist/engines/gateway.d.ts +5 -2
  49. package/dist/engines/gateway.d.ts.map +1 -1
  50. package/dist/engines/gateway.js +7 -2
  51. package/dist/engines/gateway.js.map +1 -1
  52. package/dist/engines/native.d.ts.map +1 -1
  53. package/dist/engines/native.js +10 -3
  54. package/dist/engines/native.js.map +1 -1
  55. package/dist/engines/types.d.ts +10 -1
  56. package/dist/engines/types.d.ts.map +1 -1
  57. package/dist/events.d.ts +22 -8
  58. package/dist/events.d.ts.map +1 -1
  59. package/dist/events.js.map +1 -1
  60. package/dist/index.d.ts +14 -4
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js +7 -2
  63. package/dist/index.js.map +1 -1
  64. package/dist/loop.d.ts +23 -0
  65. package/dist/loop.d.ts.map +1 -1
  66. package/dist/loop.js +1 -0
  67. package/dist/loop.js.map +1 -1
  68. package/dist/models/chat.d.ts +12 -6
  69. package/dist/models/chat.d.ts.map +1 -1
  70. package/dist/models/chat.js +11 -6
  71. package/dist/models/chat.js.map +1 -1
  72. package/dist/models/retry.d.ts +252 -0
  73. package/dist/models/retry.d.ts.map +1 -0
  74. package/dist/models/retry.js +400 -0
  75. package/dist/models/retry.js.map +1 -0
  76. package/dist/models/types.d.ts +7 -0
  77. package/dist/models/types.d.ts.map +1 -1
  78. package/dist/models/types.js.map +1 -1
  79. package/dist/private.d.ts.map +1 -1
  80. package/dist/private.js +12 -2
  81. package/dist/private.js.map +1 -1
  82. package/dist/providers/anthropic.d.ts +1 -2
  83. package/dist/providers/anthropic.d.ts.map +1 -1
  84. package/dist/providers/anthropic.js +0 -12
  85. package/dist/providers/anthropic.js.map +1 -1
  86. package/dist/providers/index.d.ts +1 -1
  87. package/dist/providers/index.d.ts.map +1 -1
  88. package/dist/providers/mock.d.ts +1 -2
  89. package/dist/providers/mock.d.ts.map +1 -1
  90. package/dist/providers/mock.js +14 -18
  91. package/dist/providers/mock.js.map +1 -1
  92. package/dist/providers/openai.d.ts +1 -2
  93. package/dist/providers/openai.d.ts.map +1 -1
  94. package/dist/providers/openai.js +0 -14
  95. package/dist/providers/openai.js.map +1 -1
  96. package/dist/providers/scripted.d.ts +1 -3
  97. package/dist/providers/scripted.d.ts.map +1 -1
  98. package/dist/providers/scripted.js +0 -9
  99. package/dist/providers/scripted.js.map +1 -1
  100. package/dist/providers/types.d.ts +0 -6
  101. package/dist/providers/types.d.ts.map +1 -1
  102. package/dist/secrets.d.ts +17 -7
  103. package/dist/secrets.d.ts.map +1 -1
  104. package/dist/secrets.js +38 -24
  105. package/dist/secrets.js.map +1 -1
  106. package/dist/tools/concurrency.d.ts +48 -0
  107. package/dist/tools/concurrency.d.ts.map +1 -0
  108. package/dist/tools/concurrency.js +64 -0
  109. package/dist/tools/concurrency.js.map +1 -0
  110. package/dist/tools/metadata.d.ts +18 -3
  111. package/dist/tools/metadata.d.ts.map +1 -1
  112. package/dist/tools/metadata.js +7 -0
  113. package/dist/tools/metadata.js.map +1 -1
  114. package/dist/tools/registry.d.ts +6 -4
  115. package/dist/tools/registry.d.ts.map +1 -1
  116. package/dist/tools/registry.js +5 -3
  117. package/dist/tools/registry.js.map +1 -1
  118. package/dist/tools/terminal.d.ts +8 -6
  119. package/dist/tools/terminal.d.ts.map +1 -1
  120. package/dist/tools/terminal.js +8 -6
  121. package/dist/tools/terminal.js.map +1 -1
  122. package/dist/tools/validate.d.ts +49 -0
  123. package/dist/tools/validate.d.ts.map +1 -0
  124. package/dist/tools/validate.js +346 -0
  125. package/dist/tools/validate.js.map +1 -0
  126. package/dist/transcript.d.ts +53 -0
  127. package/dist/transcript.d.ts.map +1 -0
  128. package/dist/transcript.js +104 -0
  129. package/dist/transcript.js.map +1 -0
  130. package/dist/types.d.ts +44 -5
  131. package/dist/types.d.ts.map +1 -1
  132. package/package.json +13 -5
package/dist/index.js CHANGED
@@ -15,13 +15,15 @@ export { ScriptedProvider, ScriptExhaustedError } from "./providers/scripted.js"
15
15
  // ---------------------------------------------------------------------------
16
16
  // The model seam — the neutral arrow every model kind is invoked through
17
17
  // ---------------------------------------------------------------------------
18
- export { emitCompleteToolCall, invokeStreaming, modelIdentity, requireCapability, CapabilityError, } from "./models/types.js";
18
+ export { invokeStreaming, modelIdentity, requireCapability, CapabilityError, } from "./models/types.js";
19
19
  // The chat kind, and the adapter that puts an existing Provider on the seam.
20
20
  export { chatModel, asChatModel, isChatModel, chatRequest, CHAT_KIND } from "./models/chat.js";
21
21
  // Extension point 1 — bind kind+id to a concrete invocation.
22
22
  export { ModelRegistry, UnknownModelError } from "./models/registry.js";
23
23
  // Extension point 3 — cross-cutting concerns over every kind, uniformly.
24
24
  export { withMiddleware, correlationMiddleware, loggingMiddleware, healthTrackingMiddleware, errorRedactionMiddleware, HealthTracker, } from "./models/middleware.js";
25
+ // Opt-in retry and an explicit fallback chain around any model invocation.
26
+ export { withRetry, defaultRetryClassifier, RetryExhaustedError } from "./models/retry.js";
25
27
  // ---------------------------------------------------------------------------
26
28
  // Extension point 2 — route resolution (capability → model + prompt + tools)
27
29
  // ---------------------------------------------------------------------------
@@ -35,6 +37,7 @@ export { assembleContext, renderContext } from "./context/types.js";
35
37
  // The tool-use loop
36
38
  // ---------------------------------------------------------------------------
37
39
  export { runAgent, runConversation, isTruncatedCall, TruncatedAnswerError, SuspendedRunError, maxToolResultBytes, resolveLoopLimits, remainingLimits, withToolResults, } from "./agent.js";
40
+ export { settleMessages, withUserTurn } from "./transcript.js";
38
41
  // Extension point 7 — the control loop itself. The kernel ships exactly one
39
42
  // (`nativeLoop`, the loop `runAgent` has always run); this is the interface
40
43
  // around it, so an application can change control flow without forking.
@@ -50,8 +53,9 @@ export { privateRunChannels, PrivateRunError, PRIVATE_REDACTION } from "./privat
50
53
  // MCP connectors, rich metadata + projections, and models-as-tools
51
54
  // ---------------------------------------------------------------------------
52
55
  export { primitiveTools, connectorTools, executeTool, namespacedToolName, } from "./tools/registry.js";
53
- export { selectTools, isToolVisible, toToolSpecs, toolsRequiringConfirmation, replaySafeTools, undoToolFor, toolDispatch, terminalTools, } from "./tools/metadata.js";
56
+ export { isToolVisible, toToolSpecs, toolsRequiringConfirmation, replaySafeTools, toolDispatch, isConcurrencySafe, } from "./tools/metadata.js";
54
57
  export { terminalTool } from "./tools/terminal.js";
58
+ export { validateToolInput } from "./tools/validate.js";
55
59
  export { modelAsTool } from "./tools/modelTool.js";
56
60
  // ---------------------------------------------------------------------------
57
61
  // Sandbox primitives (the executors the tools are built on)
@@ -83,6 +87,7 @@ export { materializeEnvironment, hasEnvironment, SKILLS_DIR } from "./engines/en
83
87
  export { writeFileCapability, denyCrossWorkspace, resolveWithin } from "./engines/capability.js";
84
88
  export { defaultCapabilityTools } from "./engines/claudeCode.js";
85
89
  export { capabilitySdkServer, capabilityToolIds, CAPABILITY_SERVER_NAME, } from "./engines/capabilityMcp.js";
90
+ export { AgentSdkMissingError } from "./engines/agentSdkMissing.js";
86
91
  export { buildCapabilityStdioServer, serveCapability, capabilityStdioConfigFromEnv, } from "./engines/capabilityStdio.js";
87
92
  // The emissions file: the generic seam for getting state back out of an
88
93
  // out-of-process engine, which cannot share memory with the run that spawned it.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,gFAAgF;AAChF,mBAAmB;AAOnB,8EAA8E;AAC9E,qDAAqD;AACrD,8EAA8E;AAC9E,OAAO,EACL,c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dist/loop.d.ts CHANGED
@@ -25,6 +25,7 @@ import type { TokenUsage } from "./models/types.js";
25
25
  import type { ProviderOptions } from "./providers/options.js";
26
26
  import type { AgentMessage, FinishReason, ToolCall } from "./providers/types.js";
27
27
  import type { Tool, ToolOutput } from "./tools/registry.js";
28
+ import type { ToolConcurrency } from "./tools/concurrency.js";
28
29
  /**
29
30
  * One run's materials, resolved.
30
31
  *
@@ -49,6 +50,16 @@ export interface LoopRequest {
49
50
  limits: LoopLimits;
50
51
  /** Sent on every model call this loop makes, the budget summary included. */
51
52
  providerOptions?: ProviderOptions;
53
+ /**
54
+ * Run a step's concurrency-safe tool calls in parallel. Absent (the default)
55
+ * runs every call in series, in the model's order. See {@link ToolConcurrency}.
56
+ */
57
+ toolConcurrency?: ToolConcurrency;
58
+ /**
59
+ * Check each call's arguments against its tool's `inputSchema` before it
60
+ * runs. Off by default. See `LoopRunOptions.validateToolInput`.
61
+ */
62
+ validateToolInput?: boolean;
52
63
  }
53
64
  /**
54
65
  * A loop's resolved budgets. `maxSteps` and `maxDurationMs` are always numbers
@@ -61,6 +72,18 @@ export interface LoopRequest {
61
72
  export interface LoopLimits {
62
73
  maxSteps: number;
63
74
  maxDurationMs: number;
75
+ /**
76
+ * The most the run may spend, in US dollars, as the model calls report it
77
+ * (`TokenUsage.reportedCost`). Absent means unlimited, and there is no
78
+ * default. Checked after each model turn against the run's summed reported
79
+ * cost: once it is reached, a turn that asked for tools ends the run with
80
+ * `cost_exhausted` instead of running them, and no summary call is made. A
81
+ * turn that reports no cost adds nothing (unknown is not free, but nor is it
82
+ * a number to compare); price unreported calls in a model middleware if the
83
+ * limit must cover them. Only the run's own model turns count: what a tool
84
+ * spends (a `modelAsTool` sub-model, a paid API) is outside it.
85
+ */
86
+ maxCostUsd?: number;
64
87
  budgetVisible?: boolean;
65
88
  }
66
89
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../src/loop.ts"],"names":[],"mappings":"AAAA;;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A;IACZ,KAAK,EAAE,MAAM,CAAA;IACb;;;;;;;OAOG;IACH,OAAO,EAAE,UAAU,CAAA;IACnB,8DAA8D;IAC9D,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,YAAY,EAAE,CAAA;IACzB,iEAAiE;IACjE,KAAK,CAAC,EAAE,UAAU,CAAA;IAClB,kDAAkD;IAClD,QAAQ,CAAC,EAAE,YAAY,CAAA;IACvB,yFAAyF;IACzF,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iFAAiF;IACjF,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,qGAAqG;IACrG,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;IACtB,sFAAsF;IACtF,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iFAAiF;IACjF,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,QAAQ,EAAE,CAAA;IACpB,qDAAqD;IACrD,QAAQ,CAAC,EAAE,YAAY,CAAA;CACxB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,IAAI;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,GAAG,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;CAC7D;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,EAAE,IAGxB,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,WAAW,EAChB,GAAG,EAAE,UAAU,GACd,OAAO,CAAC,UAAU,CAAC,CA8BrB"}
package/dist/loop.js CHANGED
@@ -58,6 +58,7 @@ export async function runWithEvents(loop, req, ctx) {
58
58
  type: "run.started",
59
59
  maxSteps: req.limits.maxSteps,
60
60
  maxDurationMs: req.limits.maxDurationMs,
61
+ ...(req.limits.maxCostUsd !== undefined ? { maxCostUsd: req.limits.maxCostUsd } : {}),
61
62
  tools: req.tools.map((t) => t.spec.name),
62
63
  toolSchemaBytes: Buffer.byteLength(JSON.stringify(req.tools.map((t) => t.spec)), "utf8"),
63
64
  });
package/dist/loop.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"loop.js","sourceRoot":"","sources":["../src/loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AA6L3C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,UAAU,GAAS;IAC9B,IAAI,EAAE,QAAQ;IACd,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC;CAC3C,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,IAAU,EACV,GAAgB,EAChB,GAAe;IAEf,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;QACd,IAAI,EAAE,aAAa;QACnB,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,QAAQ;QAC7B,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,aAAa;QACvC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;QACxC,eAAe,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC;KACzF,CAAC,CAAA;IAEF,IAAI,MAAkB,CAAA;IACtB,IAAI,CAAC;QACH,MAAM,GAAG,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AED,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;QACd,IAAI,EAAE,cAAc;QACpB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,GAAG,CAAC,MAAM,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7E,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC,CAAA;IACF,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -35,7 +35,11 @@ export type ChatPart = {
35
35
  * valid `ChatRequest`.
36
36
  */
37
37
  export interface ChatRequest extends ConverseRequest {
38
- /** Attachments for the latest user turn. Requires `caps.multimodalInput`. */
38
+ /**
39
+ * Attachments for the latest user turn. Requires `caps.multimodalInput`, and
40
+ * a `chatModel` over a `Provider` refuses them outright: no adapter sends
41
+ * them yet.
42
+ */
39
43
  parts?: ChatPart[];
40
44
  }
41
45
  /** The `chat` kind's result: prose plus the tool calls that drive the loop. */
@@ -54,11 +58,13 @@ export declare const CHAT_KIND = "chat";
54
58
  *
55
59
  * Capabilities are reported conservatively, from what the `Provider` interface
56
60
  * can actually express: it has no streaming method and no attachment channel,
57
- * so those stay false. `usage` follows the provider's own `reportsUsage`
58
- * declaration: the SDK-backed adapters set it (their `ConverseResult.usage`
59
- * becomes `ModelResult.usage`), while a provider that declares nothing — the
60
- * mock, a custom provider — reports `usage: false` rather than promising
61
- * accounting it does not deliver.
61
+ * so those stay false. A request carrying `parts` is refused with a
62
+ * `CapabilityError` even when `multimodalInput` is passed in `caps`, because
63
+ * there is no channel to forward them on. `usage` follows the provider's own
64
+ * `reportsUsage` declaration: the SDK-backed adapters set it (their
65
+ * `ConverseResult.usage` becomes `ModelResult.usage`), while a provider that
66
+ * declares nothing — the mock, a custom provider — reports `usage: false`
67
+ * rather than promising accounting it does not deliver.
62
68
  */
63
69
  export declare function chatModel(provider: Provider, caps?: Partial<ModelCaps>): ChatModel;
64
70
  /** True when a value is already a model invocation rather than a bare `Provider`. */
@@ -1 +1 @@
1
- {"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../src/models/chat.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAC9D,OAAO,KAAK,EACV,YAAY,EACZ,eAAe,EACf,cAAc,EACd,QAAQ,EACR,QAAQ,EACT,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAIL,KAAK,SAAS,EACd,KAAK,eAAe,EAErB,MAAM,YAAY,CAAA;AAEnB;;;GAGG;AACH,MAAM,MAAM,SAAS,GACjB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACnD;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAA;AAEpD;;;;GAIG;AACH,MAAM,MAAM,QAAQ,GAChB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,SAAS,CAAA;CAAE,GACpC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,SAAS,CAAA;CAAE,GACpC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,SAAS,CAAA;CAAE,CAAA;AAEzC;;;;GAIG;AACH,MAAM,WAAW,WAAY,SAAQ,eAAe;IAClD,6EAA6E;IAC7E,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAA;CACnB;AAED,+EAA+E;AAC/E,MAAM,MAAM,YAAY,GAAG,cAAc,CAAA;AAEzC,yCAAyC;AACzC,MAAM,MAAM,SAAS,GAAG,eAAe,CAAC,WAAW,EAAE,YAAY,CAAC,CAAA;AAElE,wDAAwD;AACxD,eAAO,MAAM,SAAS,SAAS,CAAA;AAE/B;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CAAC;IAAC,MAAM,EAAE,SAAS,CAAA;CAAE,GACpC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,SAAS,CAAA;CAAE,CAAA;AAEzC;;;;GAIG;AACH,MAAM,WAAW,WAAY,SAAQ,eAAe;IAClD;;;;OAIG;IACH,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAA;CACnB;AAED,+EAA+E;AAC/E,MAAM,MAAM,YAAY,GAAG,cAAc,CAAA;AAEzC,yCAAyC;AACzC,MAAM,MAAM,SAAS,GAAG,eAAe,CAAC,WAAW,EAAE,YAAY,CAAC,CAAA;AAElE,wDAAwD;AACxD,eAAO,MAAM,SAAS,SAAS,CAAA;AAE/B;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,GAAE,OAAO,CAAC,SAAS,CAAM,GAAG,SAAS,CAoEtF;AAED,qFAAqF;AACrF,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,GAAG,KAAK,IAAI,SAAS,CAE3E;AAED,kFAAkF;AAClF,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,GAAG,SAAS,CAElE;AAED,4DAA4D;AAC5D,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,YAAY,EAAE,EACxB,KAAK,EAAE,QAAQ,EAAE,EACjB,KAAK,CAAC,EAAE,QAAQ,EAAE,EAClB,eAAe,CAAC,EAAE,eAAe,GAChC,WAAW,CAQb"}
@@ -1,4 +1,4 @@
1
- import { requireCapability, } from "./types.js";
1
+ import { CapabilityError, requireCapability, } from "./types.js";
2
2
  /** The kind string every chat model registers under. */
3
3
  export const CHAT_KIND = "chat";
4
4
  /**
@@ -11,11 +11,13 @@ export const CHAT_KIND = "chat";
11
11
  *
12
12
  * Capabilities are reported conservatively, from what the `Provider` interface
13
13
  * can actually express: it has no streaming method and no attachment channel,
14
- * so those stay false. `usage` follows the provider's own `reportsUsage`
15
- * declaration: the SDK-backed adapters set it (their `ConverseResult.usage`
16
- * becomes `ModelResult.usage`), while a provider that declares nothing — the
17
- * mock, a custom provider — reports `usage: false` rather than promising
18
- * accounting it does not deliver.
14
+ * so those stay false. A request carrying `parts` is refused with a
15
+ * `CapabilityError` even when `multimodalInput` is passed in `caps`, because
16
+ * there is no channel to forward them on. `usage` follows the provider's own
17
+ * `reportsUsage` declaration: the SDK-backed adapters set it (their
18
+ * `ConverseResult.usage` becomes `ModelResult.usage`), while a provider that
19
+ * declares nothing — the mock, a custom provider — reports `usage: false`
20
+ * rather than promising accounting it does not deliver.
19
21
  */
20
22
  export function chatModel(provider, caps = {}) {
21
23
  const resolved = {
@@ -32,6 +34,9 @@ export function chatModel(provider, caps = {}) {
32
34
  async invoke(req, ctx) {
33
35
  if (req.parts?.length) {
34
36
  requireCapability({ id: provider.name, kind: CHAT_KIND, caps: resolved }, "multimodalInput", "request carries parts");
37
+ // A declared capability is not a channel: `ConverseRequest` has nowhere
38
+ // to put an attachment, so forwarding would drop it. Refuse instead.
39
+ throw new CapabilityError({ id: provider.name, kind: CHAT_KIND, caps: resolved }, "multimodalInput", "declared, but not deliverable: the Provider interface has no attachment channel, so parts would be dropped");
35
40
  }
36
41
  if (req.tools.length > 0) {
37
42
  requireCapability({ id: provider.name, kind: CHAT_KIND, caps: resolved }, "tools", "request carries tool definitions");
@@ -1 +1 @@
1
- {"version":3,"file":"chat.js","sourceRoot":"","sources":["../../src/models/chat.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,iBAAiB,GAMlB,MAAM,YAAY,CAAA;AAoCnB,wDAAwD;AACxD,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAA;AAE/B;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,SAAS,CAAC,QAAkB,EAAE,OAA2B,EAAE;IACzE,MAAM,QAAQ,GAAc;QAC1B,SAAS,EAAE,KAAK;QAChB,KAAK,EAAE,I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5E,8CAA8C;QAC9C,KAAK;YACH,OAAO,OAAO,CAAC,OAAO,CAAC;gBACrB,MAAM,EAAE,SAAS;gBACjB,MAAM,EAAE,YAAY,QAAQ,CAAC,IAAI,6BAA6B;aAC/D,CAAC,CAAA;QACJ,CAAC;KACF,CAAA;AACH,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,WAAW,CAAC,KAA2B;IACrD,OAAO,QAAQ,IAAI,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,MAAM,IAAI,KAAK,CAAA;AAChE,CAAC;AAED,kFAAkF;AAClF,MAAM,UAAU,WAAW,CAAC,KAA2B;IACrD,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;AACtD,CAAC;AAED,4DAA4D;AAC5D,MAAM,UAAU,WAAW,CACzB,MAAc,EACd,QAAwB,EACxB,KAAiB,EACjB,KAAkB,EAClB,eAAiC;IAEjC,OAAO;QACL,MAAM;QACN,QAAQ;QACR,KAAK;QACL,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACnC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAChD,CAAA;AACH,CAAC"}
@@ -0,0 +1,252 @@
1
+ import { type ModelInvocation } from "./types.js";
2
+ /**
3
+ * Retry and fallback around a model invocation, as an opt-in decorator.
4
+ *
5
+ * The kernel leaves transient failures to the vendor SDKs by default. A caller
6
+ * that needs a policy those SDKs cannot express — a wait that knows the run's
7
+ * deadline, an honoured `Retry-After`, a named chain of other models to try
8
+ * once the primary is exhausted — wraps its model in {@link withRetry} and
9
+ * turns the SDK's own retries off (`providerOptions.maxRetries: 0`), so the two
10
+ * do not multiply.
11
+ *
12
+ * This is infrastructure, not a quality loop: an attempt is repeated only when
13
+ * the call FAILED (threw), never because its answer was judged poor. And it is
14
+ * explicit: there is no default fallback chain, so a call that ends up on
15
+ * another model does so because the caller listed that model.
16
+ *
17
+ * It wraps a {@link ModelInvocation} rather than being a `Middleware`
18
+ * because a fallback chain needs several invocations, and middleware is handed
19
+ * exactly one `next`. Being payload-generic, it covers every kind the same way
20
+ * middleware does: a chat model, a detector, a forecaster.
21
+ */
22
+ /**
23
+ * What to do after a failed attempt.
24
+ *
25
+ * - `retry` — a transient fault: try the same model again after a backoff, and
26
+ * move to the next model in the chain once its attempts run out.
27
+ * - `fallback` — this model will not answer soon: skip its remaining attempts
28
+ * and move to the next model at once.
29
+ * - `fatal` — a fault no other attempt can fix (a bad credential, an invalid
30
+ * request): rethrow it unchanged, trying nothing else.
31
+ */
32
+ export type RetryDecision = "retry" | "fallback" | "fatal";
33
+ /** One model in the chain giving up, or one wait before trying it again. */
34
+ export type RetryEvent = {
35
+ type: "retry";
36
+ /** The invocation id that failed and will be tried again. */
37
+ model: string;
38
+ /** The attempt on this model that just failed, from 1. */
39
+ attempt: number;
40
+ /** How long the wrapper waits before the next attempt. */
41
+ delayMs: number;
42
+ /** True when `delayMs` came from the server's `Retry-After`. */
43
+ retryAfter: boolean;
44
+ /** The failure. Absent when the wrapper was built with `private: true`. */
45
+ error?: unknown;
46
+ } | {
47
+ type: "fallback";
48
+ /** The invocation id given up on. */
49
+ from: string;
50
+ /** The next invocation id in the chain. */
51
+ to: string;
52
+ /**
53
+ * Why `from` was given up on: its retries or wait budget ran out
54
+ * (`exhausted`), the classifier said `fallback`, or it was skipped
55
+ * because an earlier call of this wrapper exhausted it (`sticky`).
56
+ */
57
+ reason: "exhausted" | "fallback" | "sticky";
58
+ error?: unknown;
59
+ };
60
+ /**
61
+ * What the wrapper did on a call that succeeded, reported as `meta.retry` on
62
+ * the result. Consumers audit which model actually served a call from it.
63
+ */
64
+ export interface RetryReport {
65
+ /**
66
+ * The id of the invocation that answered. When the chain entry that
67
+ * answered is itself a `withRetry` wrapper, this is the model inside it that
68
+ * answered, and `attempts`, `fellBackFrom` and `waitedMs` include its calls.
69
+ */
70
+ served: string;
71
+ /** The answering entry's position in this chain: 0 is the primary, n the n-th fallback. */
72
+ position: number;
73
+ /** Every call made for this invocation, including the one that answered. */
74
+ attempts: number;
75
+ /** Ids of the models tried and given up on, in order; empty when the primary served. */
76
+ fellBackFrom: string[];
77
+ /** Total time this invocation spent waiting between attempts. */
78
+ waitedMs: number;
79
+ }
80
+ /** Exponential backoff: `baseMs × 2^(retry−1)`, capped at `maxMs`. */
81
+ export interface BackoffOptions {
82
+ /** The first wait. Default 500. */
83
+ baseMs?: number;
84
+ /** The longest single wait. Default 8000. */
85
+ maxMs?: number;
86
+ /**
87
+ * Randomise each wait between half and all of its computed value, so many
88
+ * callers refused at once do not retry in step. Default true.
89
+ */
90
+ jitter?: boolean;
91
+ }
92
+ export interface RetryOptions<Req, Res> {
93
+ /**
94
+ * Calls per model, including the first. Default 3 (the SDK default this
95
+ * replaces: one call plus two retries). 1 means never retry, only fall back.
96
+ */
97
+ maxAttempts?: number;
98
+ /**
99
+ * The wait before retry `n` (from 1): exponential options, or a function for
100
+ * a fixed schedule (`(n) => [1000, 2000, 3000][n - 1] ?? 3000`).
101
+ */
102
+ backoff?: BackoffOptions | ((retry: number) => number);
103
+ /**
104
+ * Wait what the server's `Retry-After` (or `retry-after-ms`) header asks
105
+ * instead of the computed backoff. Default true. A server wait longer than
106
+ * the remaining budget is not shortened, since a shortened wait would only
107
+ * be refused again: that model is given up on and the chain moves on.
108
+ */
109
+ respectRetryAfter?: boolean;
110
+ /**
111
+ * The longest server-asked wait honoured. Default 60000, as the vendor SDKs
112
+ * do. A `Retry-After` above it is treated as a wait that does not fit: that
113
+ * model is given up on and the chain moves on, rather than sleeping for an
114
+ * implausible hour or day.
115
+ */
116
+ maxRetryAfterMs?: number;
117
+ /** Decide each failure. Default {@link defaultRetryClassifier}. */
118
+ classify?: (err: unknown) => RetryDecision;
119
+ /**
120
+ * The ordered models to try once the primary is given up on. Empty (the
121
+ * default) means retry only: the kernel never picks a fallback by itself.
122
+ */
123
+ fallbacks?: ModelInvocation<Req, Res>[];
124
+ /**
125
+ * The absolute deadline (epoch ms) every attempt must finish by, or a
126
+ * function returning it. The wrapper never starts a wait that would run past
127
+ * it. The call's context bounds it too: `InvokeContext.budget.deadlineAt`
128
+ * (which the native loop sets to the run's deadline, so a wrapped model in
129
+ * `runConversation` or `runAgent` knows how much of the run is left) and
130
+ * `budget.maxDurationMs` counted from the call's start. The earliest wins.
131
+ * Set this when the model is called outside the native loop, or when the
132
+ * caller's own deadline is earlier than the run's.
133
+ */
134
+ deadlineAt?: number | (() => number);
135
+ /**
136
+ * Time to leave for the call itself: a wait that would leave less than this
137
+ * before the deadline is not started. Default 0.
138
+ */
139
+ minCallMs?: number;
140
+ /**
141
+ * The most this wrapper waits in total, summed across every call made
142
+ * through it. Default unlimited.
143
+ *
144
+ * This budget belongs to the INSTANCE and never refills, so build one
145
+ * wrapper per run or turn when you set it. A wrapper registered once at
146
+ * startup and shared by every run would spend it on the first rate limit
147
+ * and never retry again.
148
+ */
149
+ waitBudgetMs?: number;
150
+ /**
151
+ * Once a model has been given up on because its retries or wait budget ran
152
+ * out, start every later call of this wrapper at the next model rather than
153
+ * asking it again (asking a rate-limited model again only adds a refusal
154
+ * under the same limit). A `fallback` decision does not make it sticky.
155
+ * Default false.
156
+ *
157
+ * Like `waitBudgetMs`, this is instance state that never resets: build one
158
+ * wrapper per run or turn when you set it. A wrapper with neither option
159
+ * keeps no state between calls and is safe to share.
160
+ */
161
+ stickyFallback?: boolean;
162
+ /**
163
+ * The run is private (`RunSpec.private`): its material must stay on the one
164
+ * model it was given. Retrying that model is allowed; a fallback chain is
165
+ * refused when the wrapper is built, because a fallback is exactly a way for
166
+ * the material to reach another model. That includes a primary that is
167
+ * itself a `withRetry` wrapper with fallbacks; a nested `withRetry` must also
168
+ * be built `private: true`, or it is refused, since it logs and reports on
169
+ * its own.
170
+ *
171
+ * An upstream error can quote the material, so none of its text leaves the
172
+ * wrapper by its own hand: `onEvent` receives no `error`, `ctx.log` lines
173
+ * name the HTTP status at most, and `RetryExhaustedError`'s `failures` and
174
+ * `cause` hold a narrowed error carrying only that status. A fatal error or
175
+ * an abort is still rethrown unchanged, exactly as the model threw it; that
176
+ * error is the caller's to keep off its own channels.
177
+ *
178
+ * Caller-declared today: the native engine selects its own provider and
179
+ * takes no injected model, so a private run cannot reach this wrapper. A
180
+ * hook that ever lets an engine wrap its model must pass `RunSpec.private`
181
+ * through here, or this refusal becomes a hope.
182
+ */
183
+ private?: boolean;
184
+ /** Told about each retry and fallback as it happens. A throwing observer is logged and skipped. */
185
+ onEvent?: (event: RetryEvent) => void;
186
+ /** Injectable for tests. Must reject with `signal.reason` when it aborts. */
187
+ sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
188
+ /** Injectable for tests. Default `Date.now`. */
189
+ now?: () => number;
190
+ /** Injectable for tests (jitter). Default `Math.random`. */
191
+ random?: () => number;
192
+ }
193
+ /**
194
+ * Thrown when every model in the chain failed without a fatal error: the last
195
+ * failure is its `cause`, every failure is in `failures`. A `fatal` error is
196
+ * never wrapped, and neither is an abort.
197
+ */
198
+ export declare class RetryExhaustedError extends Error {
199
+ /** Each failed call, in order. */
200
+ readonly failures: {
201
+ model: string;
202
+ error: unknown;
203
+ }[];
204
+ /** Why the chain stopped: every model was tried, or the deadline left no room for another attempt. */
205
+ readonly reason: "exhausted" | "deadline";
206
+ /** Total time spent waiting between those calls. */
207
+ readonly waitedMs: number;
208
+ constructor(failures: {
209
+ model: string;
210
+ error: unknown;
211
+ }[], reason: "exhausted" | "deadline", cause: unknown, waitedMs?: number);
212
+ }
213
+ /**
214
+ * The HTTP status a failed call carries: the SDK error's `status` (or
215
+ * `statusCode`), else a leading status in its message ("429 Rate limit
216
+ * exceeded"), which is how some gateways' errors arrive.
217
+ */
218
+ export declare function errorStatus(err: unknown): number | undefined;
219
+ /**
220
+ * The wait a failed call's server asked for, in ms: `retry-after-ms`, else
221
+ * `retry-after` as seconds or an HTTP date. Read from the error's `headers`,
222
+ * a `Headers` or a plain record, the way the SDKs attach them.
223
+ */
224
+ export declare function retryAfterMs(err: unknown, now?: number): number | undefined;
225
+ /**
226
+ * The default classification: rate limits, timeouts, server faults (but not
227
+ * 501/505) and dropped connections are retried; a nested `withRetry` that ran
228
+ * out moves this chain to its next model; everything else — auth (401/403), payment
229
+ * (402), an invalid request (400/404/422), a credential or option the kernel
230
+ * refused, and any error it cannot recognise — is fatal. Unknown errors are
231
+ * fatal on purpose: retrying what nobody classified hides it.
232
+ */
233
+ export declare function defaultRetryClassifier(err: unknown): RetryDecision;
234
+ /**
235
+ * Wrap a model in retry-and-fallback.
236
+ *
237
+ * The returned invocation keeps the primary's `id` and `kind`; its `caps` are
238
+ * what every model in the chain can do (a capability one fallback lacks is not
239
+ * advertised), and it streams only when all of them stream. `probe` asks the
240
+ * primary.
241
+ *
242
+ * On success the result is the serving model's, with `meta.retry` set to a
243
+ * {@link RetryReport}. A streaming call is never retried or sent to a fallback
244
+ * once its sink has received anything: the caller has already shown that
245
+ * output, and a second answer spliced after it would be a different reply.
246
+ *
247
+ * An abort (the call's `signal`) stops a wait at once and is rethrown as the
248
+ * signal's reason, the same as an abort mid-request, so the loop still reads
249
+ * it as a clean stop.
250
+ */
251
+ export declare function withRetry<Req, Res>(primary: ModelInvocation<Req, Res>, options?: RetryOptions<Req, Res>): ModelInvocation<Req, Res>;
252
+ //# sourceMappingURL=retry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/models/retry.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,eAAe,EAGrB,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;;;;;;;GASG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,UAAU,GAAG,OAAO,CAAA;AAE1D,4EAA4E;AAC5E,MAA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