@superdurable/dex-agents 0.2.0 → 0.4.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 (92) hide show
  1. package/README.md +143 -6
  2. package/dist/src/agent-context-compaction.d.ts +150 -4
  3. package/dist/src/agent-context-compaction.js +253 -5
  4. package/dist/src/agent-context-compaction.js.map +1 -1
  5. package/dist/src/agent-framework-runner.d.ts +89 -0
  6. package/dist/src/agent-framework-runner.js +26 -0
  7. package/dist/src/agent-framework-runner.js.map +1 -1
  8. package/dist/src/agent-session-codecs.js +9 -0
  9. package/dist/src/agent-session-codecs.js.map +1 -1
  10. package/dist/src/agent-session-failure.d.ts +58 -0
  11. package/dist/src/agent-session-failure.js +126 -0
  12. package/dist/src/agent-session-failure.js.map +1 -0
  13. package/dist/src/agent-session-flow-graph-cli.d.ts +2 -0
  14. package/dist/src/agent-session-flow-graph-cli.js +47 -0
  15. package/dist/src/agent-session-flow-graph-cli.js.map +1 -0
  16. package/dist/src/agent-session-flow-graph.d.ts +38 -0
  17. package/dist/src/agent-session-flow-graph.js +54 -0
  18. package/dist/src/agent-session-flow-graph.js.map +1 -0
  19. package/dist/src/agent-session-flow-graph.json +2204 -0
  20. package/dist/src/agent-session-flow.d.ts +23 -2
  21. package/dist/src/agent-session-flow.js +5 -0
  22. package/dist/src/agent-session-flow.js.map +1 -1
  23. package/dist/src/agent-session-spend-limit.d.ts +156 -0
  24. package/dist/src/agent-session-spend-limit.js +42 -0
  25. package/dist/src/agent-session-spend-limit.js.map +1 -0
  26. package/dist/src/agent-session-start.d.ts +49 -0
  27. package/dist/src/agent-session-start.js +67 -0
  28. package/dist/src/agent-session-start.js.map +1 -0
  29. package/dist/src/agent-session-step-options.d.ts +52 -1
  30. package/dist/src/agent-session-step-options.js +34 -2
  31. package/dist/src/agent-session-step-options.js.map +1 -1
  32. package/dist/src/agent-session-steps.d.ts +9 -0
  33. package/dist/src/agent-session-steps.js +247 -50
  34. package/dist/src/agent-session-steps.js.map +1 -1
  35. package/dist/src/agent-session-transitions.d.ts +14 -1
  36. package/dist/src/agent-session-transitions.js +61 -1
  37. package/dist/src/agent-session-transitions.js.map +1 -1
  38. package/dist/src/agent-session-values.d.ts +43 -2
  39. package/dist/src/agent-session-values.js +6 -0
  40. package/dist/src/agent-session-values.js.map +1 -1
  41. package/dist/src/agent-session-wait-steps.js +3 -3
  42. package/dist/src/agent-session-wait-steps.js.map +1 -1
  43. package/dist/src/connectors/connector-input-schema.d.ts +19 -0
  44. package/dist/src/connectors/connector-input-schema.js +228 -0
  45. package/dist/src/connectors/connector-input-schema.js.map +1 -0
  46. package/dist/src/connectors/connector-step-context.d.ts +77 -0
  47. package/dist/src/connectors/connector-step-context.js +124 -0
  48. package/dist/src/connectors/connector-step-context.js.map +1 -0
  49. package/dist/src/connectors/connector-tool-source.d.ts +353 -0
  50. package/dist/src/connectors/connector-tool-source.js +541 -0
  51. package/dist/src/connectors/connector-tool-source.js.map +1 -0
  52. package/dist/src/connectors/index.d.ts +3 -0
  53. package/dist/src/connectors/index.js +11 -0
  54. package/dist/src/connectors/index.js.map +1 -0
  55. package/dist/src/index.d.ts +10 -3
  56. package/dist/src/index.js +8 -2
  57. package/dist/src/index.js.map +1 -1
  58. package/dist/src/stepwise/agent-definition.d.ts +8 -0
  59. package/dist/src/stepwise/index.d.ts +2 -1
  60. package/dist/src/stepwise/index.js +2 -1
  61. package/dist/src/stepwise/index.js.map +1 -1
  62. package/dist/src/stepwise/stepwise-agent-runner.d.ts +15 -32
  63. package/dist/src/stepwise/stepwise-agent-runner.js +37 -200
  64. package/dist/src/stepwise/stepwise-agent-runner.js.map +1 -1
  65. package/dist/src/stepwise/stepwise-agent-session-flow.js +1 -0
  66. package/dist/src/stepwise/stepwise-agent-session-flow.js.map +1 -1
  67. package/dist/src/stepwise/tool-source.d.ts +11 -1
  68. package/dist/src/testing/agent-session-test-client.js +2 -5
  69. package/dist/src/testing/agent-session-test-client.js.map +1 -1
  70. package/dist/src/testing/context-window.d.ts +194 -0
  71. package/dist/src/testing/context-window.js +360 -0
  72. package/dist/src/testing/context-window.js.map +1 -0
  73. package/dist/src/testing/index.d.ts +1 -0
  74. package/dist/src/testing/index.js +1 -0
  75. package/dist/src/testing/index.js.map +1 -1
  76. package/dist/src/tool-source-router.d.ts +211 -0
  77. package/dist/src/tool-source-router.js +380 -0
  78. package/dist/src/tool-source-router.js.map +1 -0
  79. package/dist/src/ui-stream/agent-event-sse.d.ts +3 -2
  80. package/dist/src/ui-stream/agent-event-sse.js +11 -3
  81. package/dist/src/ui-stream/agent-event-sse.js.map +1 -1
  82. package/dist/src/ui-stream/agent-session-record-reader.d.ts +2 -0
  83. package/dist/src/ui-stream/agent-session-record-reader.js +5 -0
  84. package/dist/src/ui-stream/agent-session-record-reader.js.map +1 -1
  85. package/dist/src/ui-stream/agent-ui-message-stream-responses.js +3 -2
  86. package/dist/src/ui-stream/agent-ui-message-stream-responses.js.map +1 -1
  87. package/dist/src/ui-stream/agent-ui-message-stream.js +17 -3
  88. package/dist/src/ui-stream/agent-ui-message-stream.js.map +1 -1
  89. package/dist/src/ui-stream/agent-ui-messages.d.ts +2 -0
  90. package/dist/src/ui-stream/agent-ui-messages.js +5 -3
  91. package/dist/src/ui-stream/agent-ui-messages.js.map +1 -1
  92. package/package.json +15 -3
@@ -0,0 +1,124 @@
1
+ // Copyright (c) 2026 Super Durable, Inc.
2
+ //
3
+ // Licensed under the Sustainable Use License 1.0.
4
+ // You may not use this file except in compliance with the License.
5
+ // See the LICENSE file in the repository root.
6
+ //
7
+ // SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
8
+ import { compareCodePoints } from "./connector-input-schema.js";
9
+ // The source reads it on a Retry; a failed save leaves the earlier value.
10
+ const savedHeartbeatValues = new WeakMap();
11
+ /** Returns the last heartbeat value this context saved in this attempt, or `undefined`. Internal to the core. */
12
+ export function readSavedHeartbeatValue(context) {
13
+ return savedHeartbeatValues.get(context);
14
+ }
15
+ /**
16
+ * The Step context a connector operation sees inside an agent tool Step, with heartbeat values kept as text.
17
+ *
18
+ * The single-dispatch fence records markers as heartbeat values and reads the previous attempt's marker back. The
19
+ * agent session's tool Step keeps one text checkpoint per attempt and repeats it on every background heartbeat.
20
+ * This context stores each heartbeat value as that text checkpoint, as JSON with sorted keys, and parses it on the
21
+ * next attempt, so a marker survives the session's own heartbeats and a retry can read it. It exposes only the
22
+ * members the fence uses, plus the identities a Call needs; the agent core's conformance tests pin that list.
23
+ *
24
+ * `ConnectorToolSource` builds one per tool Step attempt. Applications do not construct it.
25
+ * @since 0.4.0
26
+ */
27
+ export class ConnectorStepContext {
28
+ context;
29
+ heartbeats;
30
+ /**
31
+ * Creates the context for one tool Step attempt.
32
+ * @param context - The tool Step's live context.
33
+ * @param heartbeats - The tool Step's checkpoint and heartbeat functions.
34
+ */
35
+ constructor(context, heartbeats) {
36
+ this.context = context;
37
+ this.heartbeats = heartbeats;
38
+ }
39
+ /** The Flow ID. */
40
+ get flowId() {
41
+ return this.context.flowId;
42
+ }
43
+ /** The Flow run ID. */
44
+ get runId() {
45
+ return this.context.runId;
46
+ }
47
+ /** The tool Step's execution ID, the same on every attempt. */
48
+ get stepExecutionId() {
49
+ return this.context.stepExecutionId;
50
+ }
51
+ /** The one-based attempt. */
52
+ get attempt() {
53
+ return this.context.attempt;
54
+ }
55
+ /** The attempt's deadline, or `undefined`. */
56
+ get deadline() {
57
+ return this.context.deadline;
58
+ }
59
+ /** The signal Dex aborts when it cancels the attempt. */
60
+ get cancellationSignal() {
61
+ return this.context.cancellationSignal;
62
+ }
63
+ /**
64
+ * Reports whether an earlier attempt of this tool Step saved a heartbeat value.
65
+ * @returns `true` when the session read a checkpoint for this attempt.
66
+ */
67
+ hasLastHeartbeatValue() {
68
+ return this.heartbeats.previousCheckpoint !== undefined;
69
+ }
70
+ /**
71
+ * Parses the earlier attempt's heartbeat value from its JSON text.
72
+ * @param codec - Must be omitted; values are JSON.
73
+ * @returns The parsed value, or `undefined` when there is none.
74
+ * @throws {TypeError} When a codec is given.
75
+ * @throws {SyntaxError} When the checkpoint is not JSON.
76
+ */
77
+ getLastHeartbeatValue(codec) {
78
+ if (codec !== undefined) {
79
+ throw new TypeError("ConnectorStepContext keeps heartbeat values as JSON and takes no codec");
80
+ }
81
+ const previous = this.heartbeats.previousCheckpoint;
82
+ return previous === undefined ? undefined : JSON.parse(previous);
83
+ }
84
+ /**
85
+ * Records a heartbeat, saving a value as the attempt's text checkpoint.
86
+ * @param value - The heartbeat value, such as a fence marker; `undefined` records a plain heartbeat, which
87
+ * repeats the saved checkpoint.
88
+ * @param codec - Must be omitted; values are JSON.
89
+ * @returns A Promise that resolves when the Worker accepted the heartbeat.
90
+ * @throws {TypeError} When a codec is given, or a value is given and the tool Step cannot save checkpoints.
91
+ */
92
+ async recordHeartbeat(value, codec) {
93
+ if (codec !== undefined) {
94
+ throw new TypeError("ConnectorStepContext keeps heartbeat values as JSON and takes no codec");
95
+ }
96
+ if (value === undefined) {
97
+ await this.heartbeats.heartbeat();
98
+ return;
99
+ }
100
+ if (this.heartbeats.saveCheckpoint === undefined) {
101
+ throw new TypeError("this tool Step cannot save heartbeat checkpoints");
102
+ }
103
+ await this.heartbeats.saveCheckpoint(stringifyWithSortedKeys(value));
104
+ savedHeartbeatValues.set(this, value);
105
+ }
106
+ }
107
+ /** Returns compact JSON with object keys sorted by code point, the text the Python core stores. */
108
+ export function stringifyWithSortedKeys(value) {
109
+ return JSON.stringify(sortKeys(value));
110
+ }
111
+ function sortKeys(value) {
112
+ if (Array.isArray(value)) {
113
+ return value.map(sortKeys);
114
+ }
115
+ if (typeof value === "object" && value !== null) {
116
+ const record = value;
117
+ return Object.fromEntries(Object.keys(record)
118
+ .filter((key) => record[key] !== undefined)
119
+ .sort(compareCodePoints)
120
+ .map((key) => [key, sortKeys(record[key])]));
121
+ }
122
+ return value;
123
+ }
124
+ //# sourceMappingURL=connector-step-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connector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C7B,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,MAAM,MAAM,GAAG,KAA0C,CAAC;QAC1D,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;aAChB,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC;aAC1C,IAAI,CAAC,iBAAiB,CAAC;aACvB,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAC9C,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,353 @@
1
+ import type { RetryPolicy } from "@superdurable/dex";
2
+ import { type Call, type ConnectionRef, type Mutation, type Query, type QueryResult } from "@superdurable/connectors-sdk";
3
+ import { type AgentTurnContext, type DeferredToolCall, type DeferredToolExecution, type ToolCallStepOptions, type ToolExecutionResult, type ToolTaskPoll, type ToolTaskPollOutcome } from "../agent-framework-runner.js";
4
+ import type { AgentToolCall, ToolEffect } from "../agent-session-values.js";
5
+ import type { SourceToolDefinition, ToolSource } from "../stepwise/tool-source.js";
6
+ import { ConnectorStepContext } from "./connector-step-context.js";
7
+ /**
8
+ * Default {@link ConnectorToolSource.sourceName}.
9
+ * @since 0.4.0
10
+ */
11
+ export declare const defaultConnectorSourceName = "connectors";
12
+ /**
13
+ * Default bound, in bytes of UTF-8 text, on the result one connector tool call gives the model: 64 KiB.
14
+ * @since 0.4.0
15
+ */
16
+ export declare const defaultConnectorResultMaxBytes: number;
17
+ /**
18
+ * Note appended to a truncated result; `{total_bytes}` is the size of the full result. The same text as the
19
+ * Python core.
20
+ * @since 0.4.0
21
+ */
22
+ export declare const connectorResultTruncatedNote = "[Dex] truncated, {total_bytes} bytes";
23
+ /**
24
+ * Error text of a single-attempt write whose fence proved no send before the connector's Retry; the same text as
25
+ * the Python core.
26
+ * @since 0.4.0
27
+ */
28
+ export declare const connectorNotSentRetryLater = "not sent; the provider asked to retry later";
29
+ /**
30
+ * Decides from a call's decoded JSON arguments whether that call waits for approval.
31
+ * @since 0.4.0
32
+ */
33
+ export type ApprovalPredicate = (toolArguments: Readonly<Record<string, unknown>>) => boolean;
34
+ /**
35
+ * What a connector tool call records for the branch its operation selected.
36
+ *
37
+ * - `succeeded`: a required branch, such as `sent` or `refunded`. The model reads the result value.
38
+ * - `rejected`: a branch the manifest lists as `rejected` under `agent.branches`, such as `providerRejected`.
39
+ * Nothing took effect. The record is `failed` with an error the model reads, and the session continues.
40
+ * - `failed`: `defect`, or an optional branch of a Query that `agent.branches` does not list.
41
+ * - `outcome_unknown`: `uncertain`, a branch listed as `unknown`, or an optional branch of a Mutation that
42
+ * `agent.branches` does not list. The write may have taken effect.
43
+ * @since 0.4.0
44
+ */
45
+ export type ConnectorBranchOutcome = "succeeded" | "rejected" | "failed" | "outcome_unknown";
46
+ /**
47
+ * The validator a Connector package exports for an operation's input, such as `CreateRefundInputSchema`. A zod
48
+ * schema satisfies it.
49
+ * @since 0.4.0
50
+ */
51
+ export interface ConnectorInputValidator<Input> {
52
+ /**
53
+ * Checks and converts decoded JSON arguments into the operation's input.
54
+ * @param value - The decoded arguments.
55
+ * @returns `success` and the input, or `success: false`.
56
+ */
57
+ safeParse(value: unknown): {
58
+ readonly success: true;
59
+ readonly data: Input;
60
+ } | {
61
+ readonly success: false;
62
+ };
63
+ }
64
+ /**
65
+ * The parts of a Connector package's `dex-connector.json` that `connectorTool` reads.
66
+ * @since 0.4.0
67
+ */
68
+ export interface ConnectorPackageManifest {
69
+ /** The Connector ID, such as `stripe`. */
70
+ readonly connectorId: string;
71
+ /** One entry per operation. */
72
+ readonly factories: readonly ConnectorManifestFactory[];
73
+ }
74
+ /**
75
+ * One operation's entry in `dex-connector.json`.
76
+ * @since 0.4.0
77
+ */
78
+ export interface ConnectorManifestFactory {
79
+ /** `query` or `mutation`. */
80
+ readonly kind: string;
81
+ /** The operation ID, such as `createRefund`. */
82
+ readonly operationId: string;
83
+ /** The input type's name, such as `CreateRefundInput`. */
84
+ readonly inputType?: string;
85
+ /** Model-facing description. */
86
+ readonly description?: string;
87
+ /** The JSON Schema of the input. */
88
+ readonly inputSchema?: Readonly<Record<string, unknown>>;
89
+ /** The agent review: `exposed`, `approval`, `retry`, and `branches`. */
90
+ readonly agent?: {
91
+ readonly exposed?: unknown;
92
+ readonly approval?: unknown;
93
+ readonly retry?: unknown;
94
+ readonly branches?: unknown;
95
+ };
96
+ }
97
+ /**
98
+ * Options of {@link connectorTool}.
99
+ * @typeParam Input - The operation's input type.
100
+ * @since 0.4.0
101
+ */
102
+ export interface ConnectorToolOptions<Input> {
103
+ /**
104
+ * The package's parsed `dex-connector.json`, such as
105
+ * `import manifest from "@superdurable/connector-stripe/dex-connector.json" with { type: "json" }`.
106
+ */
107
+ readonly manifest: ConnectorPackageManifest;
108
+ /** The validator the package exports for the operation's input, such as `CreateRefundInputSchema`. */
109
+ readonly inputValidator: ConnectorInputValidator<Input>;
110
+ /** Tool name; defaults to `<connectorId>_<operation in snake case>`, such as `stripe_create_refund`. */
111
+ readonly name?: string;
112
+ /** Model-facing description; defaults to the manifest's. */
113
+ readonly description?: string;
114
+ /**
115
+ * `true` or `false` overrides the manifest's `agent.approval` for every call; a predicate decides per call from
116
+ * the decoded JSON arguments, and a predicate that throws requires approval. Omit it to follow the manifest.
117
+ */
118
+ readonly requiresApproval?: boolean | ApprovalPredicate;
119
+ /** Bound on the result the model reads; defaults to the source's `maxResultBytes`. */
120
+ readonly maxResultBytes?: number;
121
+ }
122
+ /**
123
+ * One Connector operation offered to a Stepwise agent as a tool. Create it with {@link connectorTool}.
124
+ * @since 0.4.0
125
+ */
126
+ export interface ConnectorTool {
127
+ /** Tool name the model calls. */
128
+ readonly name: string;
129
+ /** Tool description for the model. */
130
+ readonly description: string;
131
+ /** JSON Schema of the arguments, the manifest's `inputSchema`. */
132
+ readonly inputSchema: Readonly<Record<string, unknown>>;
133
+ /** The Connector ID. */
134
+ readonly connectorId: string;
135
+ /** The operation ID. */
136
+ readonly operationId: string;
137
+ /** `query` or `mutation`. */
138
+ readonly kind: "query" | "mutation";
139
+ /**
140
+ * `read` for a Query, `idempotent_write` for a Mutation with `agent.retry: safe`, and `non_idempotent_write`
141
+ * for any other Mutation.
142
+ */
143
+ readonly effect: ToolEffect;
144
+ /** Whether the manifest says `agent.approval: recommended`. */
145
+ readonly isApprovalRecommended: boolean;
146
+ /** The application's approval choice, or `undefined` to follow the manifest. */
147
+ readonly requiresApproval: boolean | ApprovalPredicate | undefined;
148
+ /** Bound on the result the model reads, or `undefined` for the source's bound. */
149
+ readonly maxResultBytes: number | undefined;
150
+ /** The logical connection the operation runs with. */
151
+ readonly connection: ConnectionRef;
152
+ /** The manifest's `agent.branches`. */
153
+ readonly agentBranches: Readonly<Record<string, string>>;
154
+ /** The operation's branches that are not optional. */
155
+ readonly requiredBranches: ReadonlySet<string>;
156
+ /** The operation's Execute timeout in milliseconds, or `undefined`. */
157
+ readonly executeMethodTimeout: number | undefined;
158
+ /** The operation's Execute retry policy, which bounds a `CallTool` call's retries. */
159
+ readonly retryPolicy: RetryPolicy;
160
+ /** Validates, decodes, and runs one invocation. */
161
+ readonly invoke: (call: Call, toolArguments: Readonly<Record<string, unknown>>) => Promise<QueryResult<unknown>>;
162
+ }
163
+ /**
164
+ * Returns what a connector tool call records for one selected branch.
165
+ *
166
+ * The rule fails closed: an optional branch of a Mutation that the manifest does not classify is
167
+ * `outcome_unknown`, because the write may have happened.
168
+ *
169
+ * @example
170
+ * ```ts
171
+ * classifyConnectorBranch({ kind: "mutation", branch: "invalidResponse", requiredBranches: new Set(["refunded"]),
172
+ * agentBranches: {} }); // "outcome_unknown"
173
+ * ```
174
+ * @param options - The operation kind, the selected branch, the required branches, and `agent.branches`.
175
+ * @returns The outcome to record.
176
+ * @since 0.4.0
177
+ */
178
+ export declare function classifyConnectorBranch(options: {
179
+ readonly kind: "query" | "mutation";
180
+ readonly branch: string;
181
+ readonly requiredBranches: ReadonlySet<string>;
182
+ readonly agentBranches: Readonly<Record<string, string>>;
183
+ }): ConnectorBranchOutcome;
184
+ /**
185
+ * Offers one reviewed Connector operation to a Stepwise agent as a tool.
186
+ *
187
+ * The tool's name, description, and argument schema come from the operation's entry in its package's
188
+ * `dex-connector.json`. Only an operation whose entry says `agent.exposed: true` can become a tool; nothing is
189
+ * exposed by default, and the application still names each operation it offers.
190
+ *
191
+ * The manifest decides how the call runs. A Query is `read` and runs in `CallTool`. A Mutation with
192
+ * `agent.retry: safe` is `idempotent_write` and runs in `CallTool`, whose retries stay inside the operation's own
193
+ * Execute retry policy and timeout. Any other Mutation is `non_idempotent_write` and runs once in
194
+ * `CallSingleAttemptTool`. The effect always comes from the manifest; a session's `toolPolicies` entry for the
195
+ * tool changes only its approval.
196
+ *
197
+ * A call waits for `resolve_interrupt` when the manifest says `agent.approval: recommended`;
198
+ * {@link ConnectorToolOptions.requiresApproval} overrides that. Arguments that break the schema never run, so
199
+ * they never wait for approval.
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * import manifest from "@superdurable/connector-stripe/dex-connector.json" with { type: "json" };
204
+ * import { CreateRefundInputSchema, newProjectConnection } from "@superdurable/connector-stripe";
205
+ *
206
+ * const stripe = newProjectConnection(project, "billing-stripe");
207
+ * const refund = connectorTool(stripe.operations.createRefund(), stripe.reference, {
208
+ * manifest,
209
+ * inputValidator: CreateRefundInputSchema,
210
+ * requiresApproval: (args) => Number(args["amount"]) > 5000,
211
+ * });
212
+ * const source = new ConnectorToolSource([refund]);
213
+ * ```
214
+ * @typeParam Input - The operation's input type.
215
+ * @typeParam Output - The operation's result value type.
216
+ * @param operation - The connector operation, a Query or a Mutation.
217
+ * @param connection - The logical connection reference the call runs with. Only the reference is stored; the
218
+ * Worker resolves credentials.
219
+ * @param options - The manifest, the input validator, and the application's choices.
220
+ * @returns The tool.
221
+ * @throws {TypeError} When the manifest has no entry for the operation, the entry is not `agent.exposed: true`,
222
+ * its kind differs from the operation's, its `agent` block holds an unknown value or lists a branch it may not,
223
+ * or its `inputSchema` uses a keyword the agent core cannot check.
224
+ * @throws {RangeError} When `maxResultBytes` is not positive.
225
+ * @since 0.4.0
226
+ */
227
+ export declare function connectorTool<Input, Output>(operation: Query<Input, Output> | Mutation<Input, Output>, connection: ConnectionRef, options: ConnectorToolOptions<Input>): ConnectorTool;
228
+ /**
229
+ * Offers reviewed Connector operations to a Stepwise agent as tools.
230
+ *
231
+ * Put it in `AgentDefinition.toolSources`, as `McpToolSource` is. It lists exactly the tools the application
232
+ * passes; no Connector is exposed by default. Each call runs one operation invocation in its own tool Step, through
233
+ * the Connector SDK's `runQuery` or `runMutation`, so the connector's Call ID, idempotency key, credential
234
+ * handling, and single-dispatch fence apply. The Call ID is derived from the Flow ID and the tool Step's execution
235
+ * ID, so it is the same on every attempt of that Step.
236
+ *
237
+ * Before anything runs, the model's arguments are checked against the manifest's `inputSchema` and then decoded
238
+ * with the package's input validator. Either failure is a `failed` call the model can correct; the operation never
239
+ * runs.
240
+ *
241
+ * Connector writes of one model response run one at a time in the model's order, and reads run in parallel. A
242
+ * write whose approval is needed waits for it only when its turn comes. Once a write does not succeed, the later
243
+ * writes of that response are recorded `failed` with `skipped` set.
244
+ *
245
+ * The selected branch maps as {@link classifyConnectorBranch} describes. A connector retry throws through the
246
+ * Step, so the tool Step's retry policy applies. A single-attempt write that asks to retry ends `outcome_unknown`,
247
+ * unless its single-dispatch fence saved a `notSent` or `canResend` marker in this attempt: that proves the
248
+ * provider did not process the request, so the call records `failed` with {@link connectorNotSentRetryLater}.
249
+ * Either way it stops the later writes. A result longer than `maxResultBytes` reaches the model truncated, with
250
+ * {@link connectorResultTruncatedNote}, and the record's `truncatedOutputBytes` holds the full size.
251
+ *
252
+ * Install `@superdurable/connectors-sdk` next to this package; it is an optional peer dependency.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * const flow = createStepwiseAgentSessionFlow(
257
+ * { name: "support", instructions: "You help customers.", model,
258
+ * toolSources: [new ConnectorToolSource([refund, reply])] },
259
+ * "SupportAgent",
260
+ * );
261
+ * ```
262
+ * @since 0.4.0
263
+ */
264
+ export declare class ConnectorToolSource implements ToolSource {
265
+ /** The source's stable name, stored with each call; keep it across deploys. */
266
+ readonly sourceName: string;
267
+ private readonly toolsByName;
268
+ private readonly definitions;
269
+ private readonly maxResultBytes;
270
+ /**
271
+ * Creates the source.
272
+ * @param tools - The tools to offer, in the order the model sees them.
273
+ * @param options - `sourceName`, default {@link defaultConnectorSourceName}, and `maxResultBytes`, default
274
+ * {@link defaultConnectorResultMaxBytes}.
275
+ * @throws {TypeError} When two tools share a name or `sourceName` is blank.
276
+ * @throws {RangeError} When `maxResultBytes` is not positive.
277
+ */
278
+ constructor(tools: readonly ConnectorTool[], options?: {
279
+ readonly sourceName?: string;
280
+ readonly maxResultBytes?: number;
281
+ });
282
+ /**
283
+ * Returns the tools; the list is fixed when the source is built.
284
+ * @returns The tool definitions.
285
+ */
286
+ listTools(): Promise<readonly SourceToolDefinition[]>;
287
+ /**
288
+ * Returns the tools without I/O.
289
+ * @returns The tool definitions.
290
+ */
291
+ cachedTools(): readonly SourceToolDefinition[];
292
+ /**
293
+ * Returns every tool name, so the runner refuses a collision.
294
+ * @returns The names.
295
+ */
296
+ declaredToolNames(): ReadonlySet<string>;
297
+ /**
298
+ * Builds the call with the manifest's effect and the call's approval.
299
+ * @param turn - The `RunModel` context; a `toolPolicies` entry for the tool changes only its approval.
300
+ * @param call - The model's tool call.
301
+ * @returns The deferred call, ordered when it is a write.
302
+ */
303
+ deferredToolCall(turn: AgentTurnContext, call: {
304
+ readonly toolCallId: string;
305
+ readonly toolName: string;
306
+ readonly arguments: string;
307
+ readonly listedTool: SourceToolDefinition;
308
+ }): DeferredToolCall;
309
+ /**
310
+ * Returns the operation's timeout and retry policy for the call's tool Step.
311
+ * @param toolCall - The record whose tool Step starts.
312
+ * @returns The options, or `undefined` when this source no longer has the tool.
313
+ */
314
+ toolCallStepOptions(toolCall: AgentToolCall): ToolCallStepOptions | undefined;
315
+ /**
316
+ * Checks, decodes, and runs one call, then maps the selected branch.
317
+ * @param call - The approved call, with the tool Step's live context.
318
+ * @returns The result value as JSON text, bounded by `maxResultBytes`.
319
+ * @throws {ToolExecutionError} When the arguments break the schema or do not decode, the operation selected a
320
+ * `rejected` or failed branch, or a single-attempt write asked to retry after its fence recorded `notSent` or
321
+ * `canResend` in this attempt, which proves the provider did not process it ({@link connectorNotSentRetryLater}).
322
+ * @throws {ToolOutcomeUnknownError} When the operation selected an unknown branch.
323
+ * @throws {RangeError} When this source no longer has the tool, so the effect decides the outcome.
324
+ * @throws Any other error, such as a connector retry for the tool Step's retry policy.
325
+ */
326
+ executeDeferredToolCall(call: DeferredToolExecution): Promise<ToolExecutionResult>;
327
+ /**
328
+ * Refuses a poll: connector tools never start tool tasks.
329
+ * @param poll - The call.
330
+ * @returns Never.
331
+ * @throws {RangeError} Always.
332
+ */
333
+ pollToolTask(poll: ToolTaskPoll): Promise<ToolTaskPollOutcome>;
334
+ private requireTool;
335
+ }
336
+ /**
337
+ * Returns `output` whole, or its first `maxResultBytes` bytes and {@link connectorResultTruncatedNote}.
338
+ * @param output - The full result text.
339
+ * @param maxResultBytes - The bound, in bytes of UTF-8 text, on the kept part.
340
+ * @returns The result, with `truncatedOutputBytes` set when it was cut.
341
+ * @since 0.4.0
342
+ */
343
+ export declare function boundConnectorResult(output: string, maxResultBytes: number): ToolExecutionResult;
344
+ /**
345
+ * Returns the text the model reads for one result value, the same in every core: keys sorted by code point, no
346
+ * spaces, unescaped UTF-8, and a number with no fractional part written as an integer.
347
+ * @param jsonText - The result value as JSON text.
348
+ * @returns The canonical text.
349
+ * @since 0.4.0
350
+ */
351
+ export declare function formatConnectorResult(jsonText: string): string;
352
+ /** Reports whether this attempt's last saved fence marker proves the write was not sent. */
353
+ export declare function isProvenNotSent(call: Call, context: ConnectorStepContext): boolean;