@agentsmarket/pipeline-runtime 0.5.0 → 0.5.2

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 (88) hide show
  1. package/README.md +86 -1
  2. package/dist/adapters/minimax.d.ts +16 -8
  3. package/dist/adapters/minimax.js +105 -26
  4. package/dist/adapters/multimodal-convert.d.ts +54 -0
  5. package/dist/adapters/multimodal-convert.js +105 -0
  6. package/dist/adapters/openrouter.d.ts +5 -0
  7. package/dist/adapters/openrouter.js +53 -6
  8. package/dist/index.d.ts +20 -4
  9. package/dist/index.js +15 -3
  10. package/dist/loop.d.ts +90 -0
  11. package/dist/loop.js +200 -0
  12. package/dist/memory.d.ts +73 -0
  13. package/dist/memory.js +119 -0
  14. package/dist/model-resolver.d.ts +52 -1
  15. package/dist/multimodal.d.ts +43 -0
  16. package/dist/multimodal.js +23 -0
  17. package/dist/output-errors.d.ts +93 -0
  18. package/dist/output-errors.js +128 -0
  19. package/dist/output-schema-validate.d.ts +10 -0
  20. package/dist/output-schema-validate.js +149 -0
  21. package/dist/output-schema.d.ts +66 -0
  22. package/dist/output-schema.js +141 -0
  23. package/dist/pipeline-refs.d.ts +15 -0
  24. package/dist/pipeline-refs.js +155 -0
  25. package/dist/provider-registry.d.ts +22 -1
  26. package/dist/provider-registry.js +62 -2
  27. package/dist/providers/anthropic.d.ts +28 -0
  28. package/dist/providers/anthropic.js +118 -0
  29. package/dist/providers/openai-chat.d.ts +28 -0
  30. package/dist/providers/openai-chat.js +106 -0
  31. package/dist/providers/openai-dalle.d.ts +19 -0
  32. package/dist/providers/openai-dalle.js +41 -0
  33. package/dist/providers/openai-tts.d.ts +19 -0
  34. package/dist/providers/openai-tts.js +46 -0
  35. package/dist/providers/openai-vision.d.ts +15 -0
  36. package/dist/providers/openai-vision.js +43 -0
  37. package/dist/providers/openai-whisper.d.ts +16 -0
  38. package/dist/providers/openai-whisper.js +43 -0
  39. package/dist/providers/replicate.d.ts +19 -0
  40. package/dist/providers/replicate.js +61 -0
  41. package/dist/providers/types.d.ts +23 -0
  42. package/dist/providers/types.js +31 -0
  43. package/dist/runtime-constraints.d.ts +2 -1
  44. package/dist/stages.d.ts +14 -2
  45. package/dist/stages.js +339 -34
  46. package/dist/streaming-events.d.ts +17 -2
  47. package/dist/streaming-events.js +12 -0
  48. package/dist/testing/assertions.d.ts +17 -0
  49. package/dist/testing/assertions.js +149 -0
  50. package/dist/testing/conformance.d.ts +3 -0
  51. package/dist/testing/conformance.js +28 -0
  52. package/dist/testing/coverage.d.ts +11 -0
  53. package/dist/testing/coverage.js +35 -0
  54. package/dist/testing/index.d.ts +9 -0
  55. package/dist/testing/index.js +9 -0
  56. package/dist/testing/reporters/json.d.ts +5 -0
  57. package/dist/testing/reporters/json.js +23 -0
  58. package/dist/testing/reporters/junit.d.ts +5 -0
  59. package/dist/testing/reporters/junit.js +31 -0
  60. package/dist/testing/reporters/text.d.ts +5 -0
  61. package/dist/testing/reporters/text.js +36 -0
  62. package/dist/testing/runner.d.ts +25 -0
  63. package/dist/testing/runner.js +222 -0
  64. package/dist/testing/spec-parser.d.ts +366 -0
  65. package/dist/testing/spec-parser.js +60 -0
  66. package/dist/testing/stage-extractor.d.ts +16 -0
  67. package/dist/testing/stage-extractor.js +25 -0
  68. package/dist/tools/audio-transcribe.d.ts +11 -0
  69. package/dist/tools/audio-transcribe.js +45 -0
  70. package/dist/tools/image-describe.d.ts +12 -0
  71. package/dist/tools/image-describe.js +47 -0
  72. package/dist/tools/image-generate.d.ts +12 -0
  73. package/dist/tools/image-generate.js +47 -0
  74. package/dist/tools/memory-recall.d.ts +13 -0
  75. package/dist/tools/memory-recall.js +63 -0
  76. package/dist/tools/memory-shared.d.ts +8 -0
  77. package/dist/tools/memory-shared.js +8 -0
  78. package/dist/tools/memory-store.d.ts +14 -0
  79. package/dist/tools/memory-store.js +68 -0
  80. package/dist/tools/tool-skills.js +14 -0
  81. package/dist/tools/tts-synthesize.d.ts +12 -0
  82. package/dist/tools/tts-synthesize.js +50 -0
  83. package/dist/tools/video-generate.d.ts +12 -0
  84. package/dist/tools/video-generate.js +47 -0
  85. package/dist/types.d.ts +8 -0
  86. package/dist/validate-inputs.d.ts +115 -0
  87. package/dist/validate-inputs.js +273 -0
  88. package/package.json +3 -3
@@ -21,7 +21,14 @@
21
21
  * spec.inputs is missing or input has extra keys). Permissiveness on the
22
22
  * "extra" axis avoids breaking ad-hoc CLI invocations; strictness is only
23
23
  * on the "missing required" axis.
24
+ *
25
+ * M5 — `validateOutput()` is the symmetric output validator (server-side
26
+ * stage output validation against JSON Schema). Returns structured
27
+ * violations the executor uses to drive retry modes (none|single|loop).
24
28
  */
29
+ import { validateJsonSchema } from './output-schema.js';
30
+ import { validateAgainstSchema } from './output-schema-validate.js';
31
+ import { detectMime, isImageMime, isAudioMime, isVideoMime } from './adapters/multimodal-convert.js';
25
32
  export function validateInputs(specInputs, provided) {
26
33
  if (!specInputs)
27
34
  return [];
@@ -113,3 +120,269 @@ function typeMatches(expected, actual, value) {
113
120
  return true;
114
121
  return expected === actual;
115
122
  }
123
+ export function validateOutput(rawOutput, schema, opts = {}) {
124
+ const schemaError = validateJsonSchema(schema);
125
+ if (!schemaError.ok) {
126
+ return { ok: false, violations: [], schemaError };
127
+ }
128
+ const value = parseValue(rawOutput, schema, opts);
129
+ const violations = validateAgainstSchema(value, schema);
130
+ if (violations.length === 0)
131
+ return { ok: true, value };
132
+ return { ok: false, violations };
133
+ }
134
+ /**
135
+ * Strip a wrapping markdown code fence.
136
+ *
137
+ * A model asked for JSON routinely answers ```json … ``` no matter how
138
+ * plainly the prompt says not to. The generateJson paths have always coped —
139
+ * adapters/minimax.ts, adapters/openrouter.ts, providers/anthropic.ts and
140
+ * providers/openai-chat.ts each carried the same two-line regex — but the two
141
+ * output validators parsed the raw text. So a fenced answer failed
142
+ * `output_format: structured_json` and `output_schema` validation while the
143
+ * identical string parsed fine through generateJson, which is why every
144
+ * structured-output stage was exposed to this.
145
+ *
146
+ * Deliberately conservative: it only acts when the text actually begins with a
147
+ * fence, so prose that merely mentions backticks is untouched. Accepts a fence
148
+ * with a language tag or none, and an unterminated one.
149
+ */
150
+ export function stripJsonFence(text) {
151
+ const trimmed = text.trim();
152
+ if (!trimmed.startsWith('```'))
153
+ return trimmed;
154
+ return trimmed.replace(/^```[A-Za-z0-9_+-]*[ \t]*\r?\n?/, '').replace(/```\s*$/, '').trim();
155
+ }
156
+ /**
157
+ * The first balanced `{...}` / `[...]` region in `text`, or null if there is
158
+ * none. Brace counting respects string literals and escapes, so a `{` inside a
159
+ * JSON string value cannot throw the scan off.
160
+ *
161
+ * This exists because `stripJsonFence` requires the text to *begin* with a fence,
162
+ * which is a reasonable guard against prose that merely mentions backticks — but
163
+ * it misses the case production actually hit twice: the model answering
164
+ *
165
+ * Here is the JSON you asked for:
166
+ * ```json
167
+ * { ... }
168
+ * ```
169
+ *
170
+ * Prose, then a fence. Not stripped, so `JSON.parse` died on the backtick and
171
+ * the whole run failed. Measured in prod before this: 2 runs on exactly that
172
+ * shape, 2 more on prose with no fence at all.
173
+ */
174
+ export function extractJsonRegion(text) {
175
+ const start = text.search(/[{[]/);
176
+ if (start === -1)
177
+ return null;
178
+ const open = text[start];
179
+ const close = open === '{' ? '}' : ']';
180
+ let depth = 0;
181
+ let inString = false;
182
+ let escaped = false;
183
+ for (let i = start; i < text.length; i++) {
184
+ const ch = text[i];
185
+ if (escaped) {
186
+ escaped = false;
187
+ continue;
188
+ }
189
+ if (ch === '\\') {
190
+ escaped = true;
191
+ continue;
192
+ }
193
+ if (ch === '"') {
194
+ inString = !inString;
195
+ continue;
196
+ }
197
+ if (inString)
198
+ continue;
199
+ if (ch === '{' || ch === '[')
200
+ depth++;
201
+ else if (ch === '}' || ch === ']') {
202
+ depth--;
203
+ if (depth === 0)
204
+ return ch === close ? text.slice(start, i + 1) : null;
205
+ }
206
+ }
207
+ return null; // unbalanced — truncated output, or not JSON at all
208
+ }
209
+ /** Drop `,` that sits immediately before a closing brace/bracket.
210
+ *
211
+ * String-aware, so a comma inside a value is never touched. */
212
+ export function removeTrailingCommas(json) {
213
+ let out = '';
214
+ let inString = false;
215
+ let escaped = false;
216
+ for (let i = 0; i < json.length; i++) {
217
+ const ch = json[i];
218
+ if (escaped) {
219
+ out += ch;
220
+ escaped = false;
221
+ continue;
222
+ }
223
+ if (ch === '\\') {
224
+ out += ch;
225
+ escaped = true;
226
+ continue;
227
+ }
228
+ if (ch === '"') {
229
+ inString = !inString;
230
+ out += ch;
231
+ continue;
232
+ }
233
+ if (!inString && ch === ',') {
234
+ let j = i + 1;
235
+ while (j < json.length && /\s/.test(json[j]))
236
+ j++;
237
+ if (json[j] === '}' || json[j] === ']')
238
+ continue; // drop the comma
239
+ }
240
+ out += ch;
241
+ }
242
+ return out;
243
+ }
244
+ /**
245
+ * Max recursion depth for unwrapping double-encoded JSON. Models occasionally
246
+ * emit a JSON object as a STRING value (i.e. the answer is a quoted JSON
247
+ * document), which the strict parser sees as a plain string. Three levels is
248
+ * enough to cover the realistic case — one accidental re-quoting, maybe two —
249
+ * without turning a missing-bracket case into an infinite loop.
250
+ */
251
+ export const MAX_DOUBLE_UNWRAP_DEPTH = 3;
252
+ /**
253
+ * If `value` is a string that itself parses strictly as JSON, return the inner
254
+ * parsed value. Otherwise return `value` unchanged. Bounded depth so a
255
+ * pathological input cannot loop forever.
256
+ *
257
+ * Safety property — same shape as `parseJsonLenient`. The inner parse is
258
+ * strict: a string that does not parse is left as a string, and only the
259
+ * outermost string is "unwrapped" — there is no reshaping of structure, no
260
+ * invented keys, no coercion of numbers/booleans/null. A single level of
261
+ * unwrap is the common case; deeper levels only fire if each step strictly
262
+ * parses.
263
+ */
264
+ export function unwrapDoubleEncoded(value, depth = 0) {
265
+ if (depth >= MAX_DOUBLE_UNWRAP_DEPTH)
266
+ return value;
267
+ if (typeof value !== 'string')
268
+ return value;
269
+ let inner;
270
+ try {
271
+ inner = JSON.parse(value);
272
+ }
273
+ catch {
274
+ return value;
275
+ }
276
+ return unwrapDoubleEncoded(inner, depth + 1);
277
+ }
278
+ /**
279
+ * Parse JSON a model produced, tolerating the wrappers it actually adds.
280
+ *
281
+ * Strict parse first, and every fallback is only accepted if it parses strictly.
282
+ * That ordering is the whole safety argument: valid JSON is returned untouched,
283
+ * and anything unparseable still throws the ORIGINAL error from the strict
284
+ * attempt, so nothing is silently reshaped into "valid" and the error still
285
+ * points at what the model really said.
286
+ *
287
+ * Deliberately NOT attempted: rewriting quotes, adding missing braces, or
288
+ * coercing unquoted keys. Those change the meaning of string content, and a
289
+ * marketplace that guesses at a buyer's data is worse than one that fails.
290
+ *
291
+ * Production, before this existed, also failed on JSON that the model emitted
292
+ * as a quoted string — `"{\"summary\": \"...\"}"`. Strict parse returned a
293
+ * plain string, the schema asked for an object, and the run died with
294
+ * "$: expected type object, got string". `unwrapDoubleEncoded` unpacks such a
295
+ * shape recursively up to `MAX_DOUBLE_UNWRAP_DEPTH`, only ever when the inner
296
+ * parse is strict. This is unwrapping, not invention: the value the model
297
+ * returned is byte-for-byte the same JSON document, just wrapped one extra
298
+ * time in quotes.
299
+ */
300
+ export function parseJsonLenient(text) {
301
+ const attempt = (s) => {
302
+ try {
303
+ return { ok: true, value: JSON.parse(s) };
304
+ }
305
+ catch (e) {
306
+ return { ok: false, error: e };
307
+ }
308
+ };
309
+ const direct = attempt(text);
310
+ if (direct.ok)
311
+ return unwrapDoubleEncoded(direct.value);
312
+ const unfenced = attempt(stripJsonFence(text));
313
+ if (unfenced.ok)
314
+ return unwrapDoubleEncoded(unfenced.value);
315
+ const region = extractJsonRegion(text);
316
+ if (region !== null) {
317
+ const asRegion = attempt(region);
318
+ if (asRegion.ok)
319
+ return unwrapDoubleEncoded(asRegion.value);
320
+ const repaired = attempt(removeTrailingCommas(region));
321
+ if (repaired.ok)
322
+ return unwrapDoubleEncoded(repaired.value);
323
+ }
324
+ throw direct.error;
325
+ }
326
+ function parseValue(rawOutput, schema, opts) {
327
+ const parseJson = opts.parseJson !== false;
328
+ const isScalarType = typeof schema.type === 'string'
329
+ && (schema.type === 'string' || schema.type === 'number' || schema.type === 'integer'
330
+ || schema.type === 'boolean' || schema.type === 'null');
331
+ if (isScalarType)
332
+ return rawOutput;
333
+ if (!parseJson)
334
+ return rawOutput;
335
+ try {
336
+ return parseJsonLenient(rawOutput);
337
+ }
338
+ catch {
339
+ return rawOutput;
340
+ }
341
+ }
342
+ export function validateMultimodalInput(messages) {
343
+ const errors = [];
344
+ if (!Array.isArray(messages) || messages.length === 0) {
345
+ return { ok: false, errors: [{ messageIndex: 0, reason: 'empty_content' }] };
346
+ }
347
+ for (let i = 0; i < messages.length; i++) {
348
+ const msg = messages[i];
349
+ if (!msg || (msg.role !== 'user' && msg.role !== 'assistant' && msg.role !== 'system')) {
350
+ errors.push({ messageIndex: i, reason: 'invalid_role' });
351
+ continue;
352
+ }
353
+ if (typeof msg.content === 'string')
354
+ continue;
355
+ if (!Array.isArray(msg.content) || msg.content.length === 0) {
356
+ errors.push({ messageIndex: i, reason: 'empty_content' });
357
+ continue;
358
+ }
359
+ for (let j = 0; j < msg.content.length; j++) {
360
+ const part = msg.content[j];
361
+ if (part.type === 'text')
362
+ continue;
363
+ const url = part.type === 'image_url' ? part.image_url.url
364
+ : part.type === 'audio_url' ? part.audio_url.url
365
+ : part.video_url.url;
366
+ if (!url || typeof url !== 'string') {
367
+ errors.push({ messageIndex: i, partIndex: j, reason: 'invalid_url' });
368
+ continue;
369
+ }
370
+ if (!url.startsWith('data:') && !url.startsWith('https://') && !url.startsWith('http://')) {
371
+ errors.push({ messageIndex: i, partIndex: j, reason: 'unsupported_scheme', detail: url.slice(0, 24) });
372
+ continue;
373
+ }
374
+ const mime = detectMime(url);
375
+ if (!mime) {
376
+ errors.push({ messageIndex: i, partIndex: j, reason: 'unsupported_mime', detail: url.slice(0, 24) });
377
+ continue;
378
+ }
379
+ const ok = part.type === 'image_url' ? isImageMime(mime)
380
+ : part.type === 'audio_url' ? isAudioMime(mime)
381
+ : isVideoMime(mime);
382
+ if (!ok) {
383
+ errors.push({ messageIndex: i, partIndex: j, reason: 'unsupported_mime', detail: mime });
384
+ }
385
+ }
386
+ }
387
+ return errors.length === 0 ? { ok: true } : { ok: false, errors };
388
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentsmarket/pipeline-runtime",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Runtime for executing PIPELINE.md workflows (multi-skill orchestration). Provider-agnostic (MiniMax, Claude, OpenAI).",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -19,7 +19,6 @@
19
19
  "dependencies": {
20
20
  "@anthropic-ai/sdk": "^0.125.0",
21
21
  "@hono/node-server": "^1.13.0",
22
- "@scure/bip39": "^2.4.0",
23
22
  "ethers": "^6.17.0",
24
23
  "hono": "^4.6.0",
25
24
  "js-yaml": "^4.1.0",
@@ -41,5 +40,6 @@
41
40
  },
42
41
  "publishConfig": {
43
42
  "access": "public"
44
- }
43
+ },
44
+ "types": "./dist/index.d.ts"
45
45
  }