@superdurable/dex-agents 0.4.0 → 0.5.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 +506 -52
  2. package/dist/src/agent-context-compaction.d.ts +1 -1
  3. package/dist/src/agent-framework-runner.d.ts +122 -11
  4. package/dist/src/agent-framework-runner.js +4 -0
  5. package/dist/src/agent-framework-runner.js.map +1 -1
  6. package/dist/src/agent-local-directory-media-store.d.ts +111 -0
  7. package/dist/src/agent-local-directory-media-store.js +417 -0
  8. package/dist/src/agent-local-directory-media-store.js.map +1 -0
  9. package/dist/src/agent-media.d.ts +279 -0
  10. package/dist/src/agent-media.js +457 -0
  11. package/dist/src/agent-media.js.map +1 -0
  12. package/dist/src/agent-session-codecs.d.ts +3 -1
  13. package/dist/src/agent-session-codecs.js +45 -4
  14. package/dist/src/agent-session-codecs.js.map +1 -1
  15. package/dist/src/agent-session-failure.d.ts +4 -2
  16. package/dist/src/agent-session-failure.js +6 -2
  17. package/dist/src/agent-session-failure.js.map +1 -1
  18. package/dist/src/agent-session-flow-graph.d.ts +46 -0
  19. package/dist/src/agent-session-flow-graph.js +51 -0
  20. package/dist/src/agent-session-flow-graph.js.map +1 -1
  21. package/dist/src/agent-session-flow.d.ts +5 -3
  22. package/dist/src/agent-session-flow.js +10 -1
  23. package/dist/src/agent-session-flow.js.map +1 -1
  24. package/dist/src/agent-session-start.d.ts +1 -1
  25. package/dist/src/agent-session-start.js +2 -2
  26. package/dist/src/agent-session-start.js.map +1 -1
  27. package/dist/src/agent-session-steps.d.ts +2 -0
  28. package/dist/src/agent-session-steps.js +52 -3
  29. package/dist/src/agent-session-steps.js.map +1 -1
  30. package/dist/src/agent-session-tracer.js +1 -1
  31. package/dist/src/agent-session-tracer.js.map +1 -1
  32. package/dist/src/agent-session-transitions.d.ts +2 -0
  33. package/dist/src/agent-session-transitions.js +21 -1
  34. package/dist/src/agent-session-transitions.js.map +1 -1
  35. package/dist/src/agent-session-values.d.ts +76 -1
  36. package/dist/src/agent-session-values.js.map +1 -1
  37. package/dist/src/agent-session-wait-steps.js +14 -2
  38. package/dist/src/agent-session-wait-steps.js.map +1 -1
  39. package/dist/src/ai-sdk/ai-sdk-model-client.d.ts +15 -3
  40. package/dist/src/ai-sdk/ai-sdk-model-client.js +36 -7
  41. package/dist/src/ai-sdk/ai-sdk-model-client.js.map +1 -1
  42. package/dist/src/ai-sdk/index.d.ts +1 -1
  43. package/dist/src/ai-sdk/index.js +1 -1
  44. package/dist/src/ai-sdk/index.js.map +1 -1
  45. package/dist/src/{stepwise → builtin-loop}/agent-definition.d.ts +120 -5
  46. package/dist/src/builtin-loop/agent-definition.js.map +1 -0
  47. package/dist/src/builtin-loop/builtin-agent-session-flow.d.ts +69 -0
  48. package/dist/src/{stepwise/stepwise-agent-session-flow.js → builtin-loop/builtin-agent-session-flow.js} +15 -9
  49. package/dist/src/builtin-loop/builtin-agent-session-flow.js.map +1 -0
  50. package/dist/src/{stepwise/stepwise-agent-runner.d.ts → builtin-loop/builtin-loop-agent-runner.d.ts} +72 -14
  51. package/dist/src/{stepwise/stepwise-agent-runner.js → builtin-loop/builtin-loop-agent-runner.js} +284 -41
  52. package/dist/src/builtin-loop/builtin-loop-agent-runner.js.map +1 -0
  53. package/dist/src/{stepwise/stepwise-input-items.d.ts → builtin-loop/builtin-loop-input-items.d.ts} +7 -3
  54. package/dist/src/{stepwise/stepwise-input-items.js → builtin-loop/builtin-loop-input-items.js} +57 -10
  55. package/dist/src/builtin-loop/builtin-loop-input-items.js.map +1 -0
  56. package/dist/src/builtin-loop/function-tool.d.ts +282 -0
  57. package/dist/src/{stepwise → builtin-loop}/function-tool.js +63 -3
  58. package/dist/src/builtin-loop/function-tool.js.map +1 -0
  59. package/dist/src/builtin-loop/index.d.ts +8 -0
  60. package/dist/src/builtin-loop/index.js +13 -0
  61. package/dist/src/builtin-loop/index.js.map +1 -0
  62. package/dist/src/builtin-loop/json-schema-validation.js.map +1 -0
  63. package/dist/src/{stepwise → builtin-loop}/model-client.d.ts +36 -7
  64. package/dist/src/builtin-loop/model-client.js.map +1 -0
  65. package/dist/src/{stepwise → builtin-loop}/scripted-model-client.d.ts +8 -5
  66. package/dist/src/{stepwise → builtin-loop}/scripted-model-client.js +8 -5
  67. package/dist/src/builtin-loop/scripted-model-client.js.map +1 -0
  68. package/dist/src/{stepwise → builtin-loop}/tool-source.d.ts +2 -2
  69. package/dist/src/builtin-loop/tool-source.js.map +1 -0
  70. package/dist/src/connectors/connector-input-schema.js +1 -1
  71. package/dist/src/connectors/connector-input-schema.js.map +1 -1
  72. package/dist/src/connectors/connector-step-context.d.ts +19 -2
  73. package/dist/src/connectors/connector-step-context.js +61 -5
  74. package/dist/src/connectors/connector-step-context.js.map +1 -1
  75. package/dist/src/connectors/connector-tool-source.d.ts +61 -13
  76. package/dist/src/connectors/connector-tool-source.js +111 -24
  77. package/dist/src/connectors/connector-tool-source.js.map +1 -1
  78. package/dist/src/index.d.ts +6 -4
  79. package/dist/src/index.js +3 -1
  80. package/dist/src/index.js.map +1 -1
  81. package/dist/src/mcp/index.d.ts +1 -0
  82. package/dist/src/mcp/index.js +1 -0
  83. package/dist/src/mcp/index.js.map +1 -1
  84. package/dist/src/mcp/mcp-dex-conventions.d.ts +57 -0
  85. package/dist/src/mcp/mcp-dex-conventions.js +90 -0
  86. package/dist/src/mcp/mcp-dex-conventions.js.map +1 -0
  87. package/dist/src/mcp/mcp-image-content.d.ts +20 -0
  88. package/dist/src/mcp/mcp-image-content.js +82 -0
  89. package/dist/src/mcp/mcp-image-content.js.map +1 -0
  90. package/dist/src/mcp/mcp-tool-source.d.ts +178 -17
  91. package/dist/src/mcp/mcp-tool-source.js +237 -34
  92. package/dist/src/mcp/mcp-tool-source.js.map +1 -1
  93. package/dist/src/s3/index.d.ts +1 -0
  94. package/dist/src/s3/index.js +9 -0
  95. package/dist/src/s3/index.js.map +1 -0
  96. package/dist/src/s3/s3-media-store.d.ts +138 -0
  97. package/dist/src/s3/s3-media-store.js +255 -0
  98. package/dist/src/s3/s3-media-store.js.map +1 -0
  99. package/dist/src/stepwise/index.d.ts +41 -8
  100. package/dist/src/stepwise/index.js +35 -5
  101. package/dist/src/stepwise/index.js.map +1 -1
  102. package/dist/src/testing/agent-session-test-client.d.ts +6 -3
  103. package/dist/src/testing/agent-session-test-client.js +10 -3
  104. package/dist/src/testing/agent-session-test-client.js.map +1 -1
  105. package/dist/src/testing/scripted-model-client.d.ts +1 -1
  106. package/dist/src/testing/scripted-model-client.js +2 -2
  107. package/dist/src/testing/scripted-model-client.js.map +1 -1
  108. package/dist/src/tool-source-router.d.ts +3 -3
  109. package/dist/src/tool-source-router.js +2 -2
  110. package/dist/src/ui-stream/agent-event-json.d.ts +2 -1
  111. package/dist/src/ui-stream/agent-event-json.js +3 -1
  112. package/dist/src/ui-stream/agent-event-json.js.map +1 -1
  113. package/dist/src/ui-stream/agent-ui-messages.d.ts +11 -3
  114. package/dist/src/ui-stream/agent-ui-messages.js +29 -5
  115. package/dist/src/ui-stream/agent-ui-messages.js.map +1 -1
  116. package/package.json +16 -3
  117. package/dist/src/stepwise/agent-definition.js.map +0 -1
  118. package/dist/src/stepwise/function-tool.d.ts +0 -130
  119. package/dist/src/stepwise/function-tool.js.map +0 -1
  120. package/dist/src/stepwise/json-schema-validation.js.map +0 -1
  121. package/dist/src/stepwise/model-client.js.map +0 -1
  122. package/dist/src/stepwise/scripted-model-client.js.map +0 -1
  123. package/dist/src/stepwise/stepwise-agent-runner.js.map +0 -1
  124. package/dist/src/stepwise/stepwise-agent-session-flow.d.ts +0 -35
  125. package/dist/src/stepwise/stepwise-agent-session-flow.js.map +0 -1
  126. package/dist/src/stepwise/stepwise-input-items.js.map +0 -1
  127. package/dist/src/stepwise/tool-source.js.map +0 -1
  128. /package/dist/src/{stepwise → builtin-loop}/agent-definition.js +0 -0
  129. /package/dist/src/{stepwise → builtin-loop}/json-schema-validation.d.ts +0 -0
  130. /package/dist/src/{stepwise → builtin-loop}/json-schema-validation.js +0 -0
  131. /package/dist/src/{stepwise → builtin-loop}/model-client.js +0 -0
  132. /package/dist/src/{stepwise → builtin-loop}/tool-source.js +0 -0
@@ -0,0 +1,279 @@
1
+ import type { AgentTurnContext } from "./agent-framework-runner.js";
2
+ import type { AgentImageReference } from "./agent-session-values.js";
3
+ /**
4
+ * Largest image a media store accepts, in bytes. Its base64 text is 5,000,000 bytes, which fits the per-image limits
5
+ * of Amazon Bedrock (5 MB of base64) and the Claude API (10 MB of base64).
6
+ * @since 0.5.0
7
+ */
8
+ export declare const maxImageBytes = 3750000;
9
+ /**
10
+ * Most images one user message or one tool result may carry.
11
+ * @since 0.5.0
12
+ */
13
+ export declare const maxImagesPerMessage = 20;
14
+ /**
15
+ * Image types a media store accepts, detected from the bytes.
16
+ * @since 0.5.0
17
+ */
18
+ export declare const supportedImageMimeTypes: readonly string[];
19
+ /**
20
+ * Longest {@link AgentImageReference.uri} in bytes as UTF-8, the same unit in every core; S3 object keys are at most
21
+ * 1024 bytes.
22
+ * @since 0.5.0
23
+ */
24
+ export declare const maxImageUriLength = 1024;
25
+ /**
26
+ * Image bytes or an image reference that Dex agents refuse.
27
+ *
28
+ * `putImage` throws it for bytes that are empty, larger than {@link maxImageBytes}, or not PNG, JPEG, GIF, or WebP.
29
+ * {@link validateImageReference} and {@link validateImageReferences} throw it for a reference whose fields are
30
+ * invalid. The message says what is wrong.
31
+ * @since 0.5.0
32
+ */
33
+ export declare class ImageValidationError extends Error {
34
+ /**
35
+ * @param message - What is wrong with the image or reference.
36
+ */
37
+ constructor(message: string);
38
+ }
39
+ /**
40
+ * A media store cannot return the bytes of a valid reference.
41
+ *
42
+ * `getImage` throws it when the reference belongs to another store, when the store has no object for it, or when the
43
+ * stored bytes do not match the reference's SHA-256, size, and type.
44
+ * @since 0.5.0
45
+ */
46
+ export declare class MediaStoreError extends Error {
47
+ /**
48
+ * @param message - Which image could not be read, and why.
49
+ * @param options - Standard error options, such as the `cause` from the storage service.
50
+ */
51
+ constructor(message: string, options?: ErrorOptions);
52
+ }
53
+ /**
54
+ * Keeps image bytes outside session state, addressed by their content.
55
+ *
56
+ * Implementations key every object by the SHA-256 of its bytes, under a location of their own, so `putImage` is
57
+ * idempotent: the same bytes give the same reference and one object, however often a retried tool Step or client
58
+ * uploads them. Use `S3MediaStore` from `@superdurable/dex-agents/s3` for an S3-compatible bucket,
59
+ * `LocalDirectoryMediaStore` when every client and Worker runs on one machine, or {@link InMemoryMediaStore} in tests.
60
+ * Upload with `putImage` before a reference enters a message or a tool result, and read with `getImage` or
61
+ * {@link loadImages} when building model input.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const reference = await store.putImage(screenshotPng);
66
+ * return { output: "Took a screenshot.", images: [reference] };
67
+ * ```
68
+ * @since 0.5.0
69
+ */
70
+ export interface MediaStore {
71
+ /**
72
+ * Stores one image and returns its reference.
73
+ *
74
+ * @param data - PNG, JPEG, GIF, or WebP bytes, at most {@link maxImageBytes}.
75
+ * @returns The reference, whose `uri` ends in `/sha256/<sha256>`.
76
+ * @throws {ImageValidationError} When the bytes are empty, too large, or of another type. Nothing is stored.
77
+ * @throws The store's own error when the upload fails; uploading the same bytes again is safe.
78
+ */
79
+ putImage(data: Uint8Array): Promise<AgentImageReference>;
80
+ /**
81
+ * Returns the bytes of one image this store holds.
82
+ *
83
+ * @param reference - A reference that this store's `putImage` returned.
84
+ * @returns The exact bytes, checked with {@link verifyImageBytes} against `sha256`, `sizeBytes`, and `mimeType`.
85
+ * @throws {ImageValidationError} When the reference's fields are invalid.
86
+ * @throws {MediaStoreError} When the reference belongs to another store, the object is missing, or its bytes do
87
+ * not match the reference.
88
+ * @throws The store's own error when the read fails.
89
+ */
90
+ getImage(reference: AgentImageReference): Promise<Uint8Array>;
91
+ }
92
+ /**
93
+ * Keeps images in this process, for tests and single-process tools.
94
+ *
95
+ * References use the `memory://<name>` location. Two stores with the same name accept each other's references, but
96
+ * each holds only what was put into it, so a Worker in another process cannot read them.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const store = new InMemoryMediaStore();
101
+ * const reference = await store.putImage(pngBytes);
102
+ * assert.deepEqual(await store.getImage(reference), pngBytes);
103
+ * ```
104
+ * @since 0.5.0
105
+ */
106
+ export declare class InMemoryMediaStore implements MediaStore {
107
+ /** The location that every reference of this store starts with: `"memory://<name>"`. */
108
+ readonly storeUri: string;
109
+ private readonly images;
110
+ /**
111
+ * Creates an empty store.
112
+ *
113
+ * @param name - The location name in `memory://<name>`, `"default"` when omitted. It must be non-empty and contain
114
+ * no `/` or whitespace.
115
+ * @throws {TypeError} When `name` is empty or contains `/` or whitespace.
116
+ * @throws {RangeError} When `name` makes image URIs longer than {@link maxImageUriLength}.
117
+ */
118
+ constructor(name?: string);
119
+ /** How many distinct images the store holds; uploading the same bytes twice counts once. */
120
+ get imageCount(): number;
121
+ /**
122
+ * Stores one image and returns its reference. See {@link MediaStore.putImage}.
123
+ *
124
+ * @param data - PNG, JPEG, GIF, or WebP bytes, at most {@link maxImageBytes}.
125
+ * @returns The reference.
126
+ * @throws {ImageValidationError} When the bytes are empty, too large, or of another type.
127
+ */
128
+ putImage(data: Uint8Array): Promise<AgentImageReference>;
129
+ /**
130
+ * Returns the bytes of one image. See {@link MediaStore.getImage}.
131
+ *
132
+ * @param reference - A reference that this store's `putImage` returned.
133
+ * @returns A copy of the exact bytes; changing it leaves the stored image as it was.
134
+ * @throws {ImageValidationError} When the reference's fields are invalid.
135
+ * @throws {MediaStoreError} When the reference belongs to another store, the store does not hold the image, or its
136
+ * bytes do not match.
137
+ */
138
+ getImage(reference: AgentImageReference): Promise<Uint8Array>;
139
+ }
140
+ /**
141
+ * Returns the MIME type of image bytes, read from their structure.
142
+ *
143
+ * The signature picks the type, then a cheap structural check must hold; it does not decode the image. PNG needs its
144
+ * signature followed by the `IHDR` chunk. JPEG needs SOI, a marker right after it, and EOI as the last two bytes. GIF
145
+ * needs a `GIF87a` or `GIF89a` header and the logical screen descriptor. WebP needs a RIFF size equal to the byte count
146
+ * minus 8 and a first chunk of `VP8 `, `VP8L`, or `VP8X`.
147
+ *
148
+ * @param data - Image bytes.
149
+ * @returns `"image/png"`, `"image/jpeg"`, `"image/gif"`, or `"image/webp"`.
150
+ * @throws {ImageValidationError} When the bytes are empty, start with no supported signature, or fail the structural
151
+ * check of their type.
152
+ * @since 0.5.0
153
+ */
154
+ export declare function detectImageMimeType(data: Uint8Array): string;
155
+ /**
156
+ * Checks image bytes and returns the reference a store keeps them under.
157
+ *
158
+ * A {@link MediaStore} implementation calls it before it uploads: the reference names the object,
159
+ * `imageUri(storeUri, sha256)`.
160
+ *
161
+ * @param data - Image bytes.
162
+ * @param storeUri - The store's location, such as `"s3://media/agents"`.
163
+ * @returns The reference, with the detected MIME type, SHA-256, and size.
164
+ * @throws {ImageValidationError} When the bytes are empty, larger than {@link maxImageBytes}, or not a well-formed
165
+ * PNG, JPEG, GIF, or WebP, or when {@link validateImageReference} refuses the reference, such as for a URI longer than
166
+ * {@link maxImageUriLength}.
167
+ * @since 0.5.0
168
+ */
169
+ export declare function buildImageReference(data: Uint8Array, storeUri: string): AgentImageReference;
170
+ /**
171
+ * Returns the URI of the image with this SHA-256 in a store.
172
+ *
173
+ * @param storeUri - The store's location; trailing `/` characters are dropped.
174
+ * @param sha256 - SHA-256 of the bytes as 64 lowercase hex digits.
175
+ * @returns `"<storeUri>/sha256/<sha256>"`.
176
+ * @since 0.5.0
177
+ */
178
+ export declare function imageUri(storeUri: string, sha256: string): string;
179
+ /**
180
+ * Checks that every image URI of a store fits {@link maxImageUriLength}.
181
+ *
182
+ * A {@link MediaStore} implementation calls it in its constructor, so a long location fails at once instead of on the
183
+ * first upload.
184
+ *
185
+ * @param storeUri - The store's location.
186
+ * @throws {RangeError} When `imageUri(storeUri, sha256)` is longer than {@link maxImageUriLength} bytes as UTF-8.
187
+ * @since 0.5.0
188
+ */
189
+ export declare function requireShortImageUris(storeUri: string): void;
190
+ /**
191
+ * Checks the fields of one image reference.
192
+ *
193
+ * The checks run in order, and the first failure is thrown: `type` is `"image"`; `mimeType` is supported; `sha256` is
194
+ * 64 lowercase hex digits; `sizeBytes` is from 1 to {@link maxImageBytes}; `uri` is at most
195
+ * {@link maxImageUriLength} bytes as UTF-8, looks like `<scheme>://<location>/sha256/<sha256>`, and ends with the
196
+ * reference's own `sha256`. It does not read the store.
197
+ *
198
+ * @param reference - The reference, such as one decoded from a request.
199
+ * @throws {ImageValidationError} When a field is invalid.
200
+ * @since 0.5.0
201
+ */
202
+ export declare function validateImageReference(reference: AgentImageReference): void;
203
+ /**
204
+ * Checks the images of one user message or one tool result.
205
+ *
206
+ * @param references - The images, at most {@link maxImagesPerMessage}.
207
+ * @throws {ImageValidationError} When there are too many images, or with `"images[<index>]: "` before the reason
208
+ * when one is invalid.
209
+ * @since 0.5.0
210
+ */
211
+ export declare function validateImageReferences(references: readonly AgentImageReference[]): void;
212
+ /**
213
+ * Checks that a valid reference names an image of the store at `storeUri`.
214
+ *
215
+ * A {@link MediaStore} implementation calls it first in `getImage`, so a request cannot make the Worker read outside
216
+ * the store's location.
217
+ *
218
+ * @param reference - The reference to read.
219
+ * @param storeUri - The store's location.
220
+ * @throws {ImageValidationError} When the reference's fields are invalid.
221
+ * @throws {MediaStoreError} When its `uri` is not `imageUri(storeUri, sha256)`.
222
+ * @since 0.5.0
223
+ */
224
+ export declare function requireStoreImageReference(reference: AgentImageReference, storeUri: string): void;
225
+ /**
226
+ * Checks bytes read from a store against their reference.
227
+ *
228
+ * The SHA-256 binds the bytes, and {@link detectImageMimeType} must find the reference's `mimeType` in them, so a
229
+ * reference whose type was changed after upload fails here.
230
+ *
231
+ * @param reference - The reference that was read.
232
+ * @param data - The bytes the store returned.
233
+ * @returns `data`, unchanged.
234
+ * @throws {MediaStoreError} When the size or the SHA-256 differs, or when the bytes are not a well-formed image of
235
+ * `mimeType`.
236
+ * @since 0.5.0
237
+ */
238
+ export declare function verifyImageBytes(reference: AgentImageReference, data: Uint8Array): Uint8Array;
239
+ /**
240
+ * Reads the bytes of several images, each distinct image once.
241
+ *
242
+ * A runner calls it while building model input, with the references it chose from {@link collectTurnImages} or from
243
+ * its own replayed items. The reads run concurrently.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * const images = await loadImages(store, collectTurnImages(turn));
248
+ * const parts = (message.images ?? []).map((reference) => imagePart(reference.mimeType, images.get(reference.sha256)!));
249
+ * ```
250
+ * @param store - The store that holds the images.
251
+ * @param references - The images to read, in any order, with repeats allowed.
252
+ * @returns The bytes of each image, keyed by {@link AgentImageReference.sha256}.
253
+ * @throws {ImageValidationError} When a reference's fields are invalid.
254
+ * @throws {MediaStoreError} When the store cannot return an image's exact bytes.
255
+ * @throws The store's own error when a read fails.
256
+ * @since 0.5.0
257
+ */
258
+ export declare function loadImages(store: MediaStore, references: Iterable<AgentImageReference>): Promise<ReadonlyMap<string, Uint8Array>>;
259
+ /**
260
+ * Returns every image a model call of this turn may show, each once.
261
+ *
262
+ * It collects `AgentMessage.images` of `turn.messages` in sequence order, then `AgentToolCall.images` of the turn's
263
+ * records in deferral order, keeping the first reference to each SHA-256. Whether a model call shows earlier turns'
264
+ * images is the runner's choice; pass only the references it needs to {@link loadImages}.
265
+ *
266
+ * @param turn - The context of one `runModelStep` call.
267
+ * @returns The references, without repeats.
268
+ * @since 0.5.0
269
+ */
270
+ export declare function collectTurnImages(turn: AgentTurnContext): AgentImageReference[];
271
+ /** Reports whether {@link validateImageReferences} accepts the images; the RPCs answer `false` instead of throwing. */
272
+ export declare function areValidImageReferences(references: readonly AgentImageReference[]): boolean;
273
+ /**
274
+ * Returns the `images` field of a succeeded record from a tool's result. An invalid reference throws
275
+ * {@link ImageValidationError}, which fails the tool Step attempt.
276
+ */
277
+ export declare function toolResultImages(references: readonly AgentImageReference[] | undefined): {
278
+ readonly images?: readonly AgentImageReference[];
279
+ };