@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
@@ -0,0 +1,19 @@
1
+ /**
2
+ * OpenAI TTS provider — text-to-speech.
3
+ * Used by tts_synthesize tool skill.
4
+ *
5
+ * Env: OPENAI_API_KEY
6
+ * Cost: $15 / 1M characters ($0.000015/char)
7
+ * Limit: 4096 chars per request (we use 5000 for safety)
8
+ */
9
+ import { ToolSkillResult } from './types.js';
10
+ export declare const MAX_CHARS = 5000;
11
+ export type TtsVoice = 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer';
12
+ export type TtsFormat = 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm';
13
+ export interface OpenAITtsInput {
14
+ text: string;
15
+ voice?: TtsVoice;
16
+ format?: TtsFormat;
17
+ speed?: number;
18
+ }
19
+ export declare function openaiTtsSynthesize(input: OpenAITtsInput): Promise<ToolSkillResult>;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * OpenAI TTS provider — text-to-speech.
3
+ * Used by tts_synthesize tool skill.
4
+ *
5
+ * Env: OPENAI_API_KEY
6
+ * Cost: $15 / 1M characters ($0.000015/char)
7
+ * Limit: 4096 chars per request (we use 5000 for safety)
8
+ */
9
+ import { ProviderError, requireEnv } from './types.js';
10
+ export const MAX_CHARS = 5000;
11
+ export async function openaiTtsSynthesize(input) {
12
+ const apiKey = requireEnv('OPENAI_API_KEY');
13
+ if (input.text.length > MAX_CHARS) {
14
+ throw new ProviderError(`text too long: ${input.text.length} chars (max ${MAX_CHARS})`, 'openai-tts', 413);
15
+ }
16
+ const res = await fetch('https://api.openai.com/v1/audio/speech', {
17
+ method: 'POST',
18
+ headers: {
19
+ Authorization: `Bearer ${apiKey}`,
20
+ 'Content-Type': 'application/json',
21
+ },
22
+ body: JSON.stringify({
23
+ model: 'tts-1',
24
+ input: input.text,
25
+ voice: input.voice ?? 'alloy',
26
+ response_format: input.format ?? 'mp3',
27
+ speed: input.speed ?? 1.0,
28
+ }),
29
+ });
30
+ if (!res.ok) {
31
+ const errText = await res.text();
32
+ throw new ProviderError(`openai-tts error ${res.status}: ${errText.slice(0, 500)}`, 'openai-tts', res.status);
33
+ }
34
+ const arrayBuffer = await res.arrayBuffer();
35
+ const bytes = arrayBuffer.byteLength;
36
+ return {
37
+ text: JSON.stringify({ audio_size_bytes: bytes, format: input.format ?? 'mp3', ok: true }),
38
+ usage: { inputTokens: input.text.length, outputTokens: 0 },
39
+ meta: {
40
+ provider: 'openai',
41
+ model: 'tts-1',
42
+ voice: input.voice ?? 'alloy',
43
+ audio_size_bytes: bytes,
44
+ },
45
+ };
46
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * OpenAI Vision provider — gpt-4o-mini multimodal.
3
+ * Used by image_describe tool skill.
4
+ *
5
+ * Env: OPENAI_API_KEY
6
+ * Cost: ~$0.00015/image + $0.0006/1k output tokens
7
+ */
8
+ import { ToolSkillResult } from './types.js';
9
+ export interface OpenAIVisionInput {
10
+ image_url: string;
11
+ prompt: string;
12
+ detail?: 'low' | 'high' | 'auto';
13
+ max_tokens?: number;
14
+ }
15
+ export declare function openaiVisionDescribe(input: OpenAIVisionInput): Promise<ToolSkillResult>;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * OpenAI Vision provider — gpt-4o-mini multimodal.
3
+ * Used by image_describe tool skill.
4
+ *
5
+ * Env: OPENAI_API_KEY
6
+ * Cost: ~$0.00015/image + $0.0006/1k output tokens
7
+ */
8
+ import { ProviderError, requireEnv, safeJsonParse } from './types.js';
9
+ export async function openaiVisionDescribe(input) {
10
+ const apiKey = requireEnv('OPENAI_API_KEY');
11
+ const res = await fetch('https://api.openai.com/v1/chat/completions', {
12
+ method: 'POST',
13
+ headers: {
14
+ Authorization: `Bearer ${apiKey}`,
15
+ 'Content-Type': 'application/json',
16
+ },
17
+ body: JSON.stringify({
18
+ model: 'gpt-4o-mini',
19
+ messages: [
20
+ {
21
+ role: 'user',
22
+ content: [
23
+ { type: 'text', text: input.prompt || 'Describe this image in detail.' },
24
+ {
25
+ type: 'image_url',
26
+ image_url: { url: input.image_url, detail: input.detail ?? 'auto' },
27
+ },
28
+ ],
29
+ },
30
+ ],
31
+ max_tokens: input.max_tokens ?? 500,
32
+ }),
33
+ });
34
+ const data = await safeJsonParse(res, 'openai-vision');
35
+ const content = data.choices[0]?.message.content ?? '';
36
+ if (!content)
37
+ throw new ProviderError('openai-vision returned empty content', 'openai-vision', 200);
38
+ return {
39
+ text: JSON.stringify({ description: content, ok: true }),
40
+ usage: { inputTokens: data.usage.prompt_tokens, outputTokens: data.usage.completion_tokens },
41
+ meta: { provider: 'openai', model: 'gpt-4o-mini' },
42
+ };
43
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * OpenAI Whisper provider — speech-to-text.
3
+ * Used by audio_transcribe tool skill.
4
+ *
5
+ * Env: OPENAI_API_KEY
6
+ * Cost: $0.006 / minute of audio
7
+ * Limit: 25 MB max upload
8
+ */
9
+ import { ToolSkillResult } from './types.js';
10
+ export interface OpenAIWhisperInput {
11
+ audio_url: string;
12
+ language?: string;
13
+ response_format?: 'json' | 'text' | 'srt' | 'verbose_json' | 'vtt';
14
+ }
15
+ export declare const MAX_AUDIO_BYTES: number;
16
+ export declare function openaiWhisperTranscribe(input: OpenAIWhisperInput): Promise<ToolSkillResult>;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * OpenAI Whisper provider — speech-to-text.
3
+ * Used by audio_transcribe tool skill.
4
+ *
5
+ * Env: OPENAI_API_KEY
6
+ * Cost: $0.006 / minute of audio
7
+ * Limit: 25 MB max upload
8
+ */
9
+ import { ProviderError, requireEnv } from './types.js';
10
+ export const MAX_AUDIO_BYTES = 25 * 1024 * 1024;
11
+ export async function openaiWhisperTranscribe(input) {
12
+ const apiKey = requireEnv('OPENAI_API_KEY');
13
+ const audioRes = await fetch(input.audio_url);
14
+ if (!audioRes.ok) {
15
+ throw new ProviderError(`failed to fetch audio: ${audioRes.status}`, 'openai-whisper', audioRes.status);
16
+ }
17
+ const blob = await audioRes.blob();
18
+ if (blob.size > MAX_AUDIO_BYTES) {
19
+ throw new ProviderError(`audio too large: ${blob.size} bytes (max ${MAX_AUDIO_BYTES})`, 'openai-whisper', 413);
20
+ }
21
+ const form = new FormData();
22
+ const ext = input.audio_url.split('.').pop()?.toLowerCase() ?? 'mp3';
23
+ form.append('file', blob, `audio.${ext}`);
24
+ form.append('model', 'whisper-1');
25
+ if (input.language)
26
+ form.append('language', input.language);
27
+ form.append('response_format', input.response_format ?? 'verbose_json');
28
+ const res = await fetch('https://api.openai.com/v1/audio/transcriptions', {
29
+ method: 'POST',
30
+ headers: { Authorization: `Bearer ${apiKey}` },
31
+ body: form,
32
+ });
33
+ if (!res.ok) {
34
+ const errText = await res.text();
35
+ throw new ProviderError(`openai-whisper error ${res.status}: ${errText.slice(0, 500)}`, 'openai-whisper', res.status);
36
+ }
37
+ const data = (await res.json());
38
+ return {
39
+ text: JSON.stringify({ transcript: data.text, language: data.language, duration: data.duration, ok: true }),
40
+ usage: { inputTokens: 0, outputTokens: 0 },
41
+ meta: { provider: 'openai', model: 'whisper-1', duration_seconds: data.duration },
42
+ };
43
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Replicate provider — generic async polling for video/image models.
3
+ * Used by video_generate tool skill (Runway Gen-3 / Pika / Stable Video).
4
+ *
5
+ * Env: REPLICATE_API_KEY
6
+ * Flow: POST /v1/predictions → poll until status=succeeded → return output URLs
7
+ * Cost varies by model (~$0.10/sec for Runway Gen-3)
8
+ */
9
+ import { ToolSkillResult } from './types.js';
10
+ export type ReplicateModel = 'stability-ai/stable-video-diffusion' | 'minimax/video-01' | 'bytedance/seedance-1-pro';
11
+ export interface ReplicateVideoInput {
12
+ prompt: string;
13
+ duration_seconds?: number;
14
+ resolution?: '480p' | '720p' | '1080p';
15
+ model?: ReplicateModel;
16
+ max_wait_ms?: number;
17
+ }
18
+ export declare const MAX_DURATION_SECONDS = 30;
19
+ export declare function replicateGenerateVideo(input: ReplicateVideoInput): Promise<ToolSkillResult>;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Replicate provider — generic async polling for video/image models.
3
+ * Used by video_generate tool skill (Runway Gen-3 / Pika / Stable Video).
4
+ *
5
+ * Env: REPLICATE_API_KEY
6
+ * Flow: POST /v1/predictions → poll until status=succeeded → return output URLs
7
+ * Cost varies by model (~$0.10/sec for Runway Gen-3)
8
+ */
9
+ import { ProviderError, requireEnv } from './types.js';
10
+ const REPLICATE_BASE = 'https://api.replicate.com/v1';
11
+ const POLL_INTERVAL_MS = 2000;
12
+ const DEFAULT_MAX_WAIT_MS = 5 * 60 * 1000;
13
+ export const MAX_DURATION_SECONDS = 30;
14
+ export async function replicateGenerateVideo(input) {
15
+ const apiKey = requireEnv('REPLICATE_API_KEY');
16
+ const duration = Math.min(input.duration_seconds ?? 10, MAX_DURATION_SECONDS);
17
+ const model = input.model ?? 'stability-ai/stable-video-diffusion';
18
+ const createRes = await fetch(`${REPLICATE_BASE}/predictions`, {
19
+ method: 'POST',
20
+ headers: {
21
+ Authorization: `Token ${apiKey}`,
22
+ 'Content-Type': 'application/json',
23
+ },
24
+ body: JSON.stringify({
25
+ version: model,
26
+ input: {
27
+ prompt: input.prompt,
28
+ duration,
29
+ resolution: input.resolution ?? '720p',
30
+ },
31
+ }),
32
+ });
33
+ if (!createRes.ok) {
34
+ const errText = await createRes.text();
35
+ throw new ProviderError(`replicate error ${createRes.status}: ${errText.slice(0, 500)}`, 'replicate', createRes.status);
36
+ }
37
+ let prediction = (await createRes.json());
38
+ const deadline = Date.now() + (input.max_wait_ms ?? DEFAULT_MAX_WAIT_MS);
39
+ while (prediction.status !== 'succeeded' && prediction.status !== 'failed' && prediction.status !== 'canceled') {
40
+ if (Date.now() > deadline) {
41
+ throw new ProviderError(`replicate timed out after ${input.max_wait_ms}ms`, 'replicate', 504);
42
+ }
43
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
44
+ const pollRes = await fetch(`${REPLICATE_BASE}/predictions/${prediction.id}`, {
45
+ headers: { Authorization: `Token ${apiKey}` },
46
+ });
47
+ if (!pollRes.ok) {
48
+ throw new ProviderError(`replicate poll error ${pollRes.status}`, 'replicate', pollRes.status);
49
+ }
50
+ prediction = (await pollRes.json());
51
+ }
52
+ if (prediction.status === 'failed' || prediction.status === 'canceled') {
53
+ throw new ProviderError(`replicate prediction ${prediction.status}: ${prediction.error ?? 'unknown'}`, 'replicate');
54
+ }
55
+ const output = Array.isArray(prediction.output) ? prediction.output : prediction.output ? [prediction.output] : [];
56
+ return {
57
+ text: JSON.stringify({ videos: output, duration_seconds: duration, ok: true }),
58
+ usage: { inputTokens: 0, outputTokens: 0 },
59
+ meta: { provider: 'replicate', model, status: prediction.status, video_count: output.length },
60
+ };
61
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Shared types for tool skill providers.
3
+ *
4
+ * Each provider module exports a `call<X>(input): Promise<ToolSkillResult>` function.
5
+ * Tool skill handlers call providers; providers handle API key + HTTP + response parsing.
6
+ */
7
+ export interface ToolSkillUsage {
8
+ inputTokens: number;
9
+ outputTokens: number;
10
+ }
11
+ export interface ToolSkillResult {
12
+ text: string;
13
+ usage: ToolSkillUsage;
14
+ meta?: Record<string, unknown>;
15
+ }
16
+ export declare class ProviderError extends Error {
17
+ readonly provider: string;
18
+ readonly statusCode?: number | undefined;
19
+ readonly cause?: unknown | undefined;
20
+ constructor(message: string, provider: string, statusCode?: number | undefined, cause?: unknown | undefined);
21
+ }
22
+ export declare function requireEnv(name: string): string;
23
+ export declare function safeJsonParse<T>(res: Response, provider: string): Promise<T>;
@@ -0,0 +1,31 @@
1
+ export class ProviderError extends Error {
2
+ provider;
3
+ statusCode;
4
+ cause;
5
+ constructor(message, provider, statusCode, cause) {
6
+ super(message);
7
+ this.provider = provider;
8
+ this.statusCode = statusCode;
9
+ this.cause = cause;
10
+ this.name = 'ProviderError';
11
+ }
12
+ }
13
+ export function requireEnv(name) {
14
+ const v = process.env[name];
15
+ if (!v) {
16
+ throw new ProviderError(`${name} not set`, 'env');
17
+ }
18
+ return v;
19
+ }
20
+ export async function safeJsonParse(res, provider) {
21
+ const text = await res.text();
22
+ if (!res.ok) {
23
+ throw new ProviderError(`${provider} error ${res.status}: ${text.slice(0, 500)}`, provider, res.status);
24
+ }
25
+ try {
26
+ return JSON.parse(text);
27
+ }
28
+ catch (e) {
29
+ throw new ProviderError(`${provider} returned non-JSON: ${text.slice(0, 200)}`, provider, res.status, e);
30
+ }
31
+ }
@@ -31,7 +31,8 @@ export interface StageLikeForConstraints {
31
31
  readonly provider?: string;
32
32
  readonly uses?: string | string[];
33
33
  readonly system?: string;
34
- readonly prompt?: string;
34
+ readonly prompt?: string | readonly import('./multimodal.js').MultimodalMessage[];
35
+ readonly messages?: readonly import('./multimodal.js').MultimodalMessage[];
35
36
  }
36
37
  export declare function defaultRuntimeConstraints(): RuntimeConstraints;
37
38
  export declare function mergeRuntimeConstraints(a: RuntimeConstraints, b: RuntimeConstraints): RuntimeConstraints;
package/dist/stages.d.ts CHANGED
@@ -89,8 +89,13 @@ export interface StageExecutorDeps {
89
89
  * text and surface it when validation fails (e.g. "Output format
90
90
  * violations: takeaway_1: key missing" without `--debug` you never
91
91
  * see what the model actually returned).
92
+ *
93
+ * Awaited by the executor. The server's implementation writes cost
94
+ * telemetry and may throw `BalanceExhaustedError` to halt the run, which only
95
+ * works if the runtime waits for it — a fire-and-forget call turns the
96
+ * rejection into an unhandled rejection that never reaches the caller.
92
97
  */
93
- onLlmResponse?: (stageId: string, rawText: string, model: string) => void;
98
+ onLlmResponse?: (stageId: string, rawText: string, model: string) => void | Promise<void>;
94
99
  /**
95
100
  * Optional pipeline fetcher for composition refs (R10.7.b, ADR Q10).
96
101
  * When supplied, runPipelineV2 walks the spec once before the loop and
@@ -146,7 +151,14 @@ export declare class StageForbiddenError extends Error {
146
151
  export declare class StageBadConfigError extends Error {
147
152
  constructor(message: string);
148
153
  }
149
- export declare function executeStage(stage: PipelineStageLike, pipeline: PipelineLike | undefined, ctx: StageContext, deps: StageExecutorDeps): Promise<StageExecutionResult>;
154
+ export declare function executeStage(stage: PipelineStageLike, pipeline: PipelineLike | undefined, ctx: StageContext, deps: StageExecutorDeps,
155
+ /**
156
+ * Appended to the prompt on a validation retry. `output_retry` used to
157
+ * re-run the stage with byte-identical input, which only pays off when the
158
+ * failure was a one-off transport glitch; a stage whose output shape is
159
+ * wrong re-rolls the same wrong shape. See runStageWithOutputValidation.
160
+ */
161
+ correctionHint?: string): Promise<StageExecutionResult>;
150
162
  export interface RunPipelineOptions {
151
163
  spec: {
152
164
  name?: string;