@agentsmarket/pipeline-runtime 0.1.5 → 0.1.7
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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/pipeline-refs.d.ts +35 -0
- package/dist/pipeline-refs.js +99 -0
- package/dist/stages.js +23 -1
- package/dist/tools/tool-skills.d.ts +37 -0
- package/dist/tools/tool-skills.js +98 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -36,6 +36,7 @@ export { createModelResolver, resolveModelSync, DEFAULT_MODEL, type ModelResolve
|
|
|
36
36
|
export { sha256Hex, computeSkillContentHash, } from './content-hash.js';
|
|
37
37
|
export { executeStage, runPipelineV2, makeWhenResolve, parseUsesEntry, parseUsesList, validateOutputFormat, StageNotFoundError, StageForbiddenError, StageBadConfigError, type StageExecutorDeps, type StageContext, type StageExecutionResult, type RunPipelineOptions, type RunPipelineResult, type NestedCallRecord, type ResolvedUse, type SkillInvocationDetail, type MCPValidationResult, type MCPDetail, } from './stages.js';
|
|
38
38
|
export { validateInputs, formatInputErrors, type InputDecl, type InputSpec, type InputValidationError, } from './validate-inputs.js';
|
|
39
|
+
export { resolveInputRefs, type PipelineRefContext, } from './pipeline-refs.js';
|
|
39
40
|
export interface PipelineStageSpec {
|
|
40
41
|
id: string;
|
|
41
42
|
model: string;
|
package/dist/index.js
CHANGED
|
@@ -23,6 +23,7 @@ export { createModelResolver, resolveModelSync, DEFAULT_MODEL, } from './model-r
|
|
|
23
23
|
export { sha256Hex, computeSkillContentHash, } from './content-hash.js';
|
|
24
24
|
export { executeStage, runPipelineV2, makeWhenResolve, parseUsesEntry, parseUsesList, validateOutputFormat, StageNotFoundError, StageForbiddenError, StageBadConfigError, } from './stages.js';
|
|
25
25
|
export { validateInputs, formatInputErrors, } from './validate-inputs.js';
|
|
26
|
+
export { resolveInputRefs, } from './pipeline-refs.js';
|
|
26
27
|
export async function runStage(stage, ctx) {
|
|
27
28
|
const vars = [];
|
|
28
29
|
for (const [key, val] of Object.entries(ctx.outputs)) {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve pipeline-level template references in skill chain input blocks.
|
|
3
|
+
*
|
|
4
|
+
* Supports two reference flavors (both with and without braces):
|
|
5
|
+
* - `$input.X` / `${input.X}` → ctx.inputs[X]
|
|
6
|
+
* - `$stages.X.output` / `${stages.X.output}` → ctx.previousOutputs[X]
|
|
7
|
+
*
|
|
8
|
+
* Scope (F26-min): only the skill-chain `input:` block is resolved.
|
|
9
|
+
* Prompts keep their existing behavior (LLM interprets literal refs
|
|
10
|
+
* as context markers — works because LLMs are smart).
|
|
11
|
+
*
|
|
12
|
+
* Native types are PRESERVED. When `$input.urls` resolves and `urls`
|
|
13
|
+
* is an array, the substituted value is the array, NOT a JSON string.
|
|
14
|
+
* Tool skills (web_fetch, etc.) need native arrays/objects to work.
|
|
15
|
+
* If a prompt needs the array stringified, the prompt author can do
|
|
16
|
+
* it inside the template (e.g. wrap in explicit JSON.stringify call
|
|
17
|
+
* is not available in YAML — but a downstream LLM can handle arrays
|
|
18
|
+
* shown as `[object Array]` if needed).
|
|
19
|
+
*
|
|
20
|
+
* Why "leave literal if missing" instead of throwing:
|
|
21
|
+
* The skill may receive partial input and choose to return an
|
|
22
|
+
* error JSON (which downstream stages can handle). Throwing
|
|
23
|
+
* would short-circuit that and bury the real cause one layer
|
|
24
|
+
* deeper. The pipeline author can still catch missing inputs at
|
|
25
|
+
* spec-validation time by adding `required: true` to spec.inputs.
|
|
26
|
+
*
|
|
27
|
+
* Recursion: objects and arrays are walked; primitives pass through.
|
|
28
|
+
* Strings get the regex substitution. Numbers, booleans, null return
|
|
29
|
+
* unchanged.
|
|
30
|
+
*/
|
|
31
|
+
export interface PipelineRefContext {
|
|
32
|
+
inputs: Record<string, unknown>;
|
|
33
|
+
previousOutputs: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
export declare function resolveInputRefs(value: unknown, ctx: PipelineRefContext): unknown;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve pipeline-level template references in skill chain input blocks.
|
|
3
|
+
*
|
|
4
|
+
* Supports two reference flavors (both with and without braces):
|
|
5
|
+
* - `$input.X` / `${input.X}` → ctx.inputs[X]
|
|
6
|
+
* - `$stages.X.output` / `${stages.X.output}` → ctx.previousOutputs[X]
|
|
7
|
+
*
|
|
8
|
+
* Scope (F26-min): only the skill-chain `input:` block is resolved.
|
|
9
|
+
* Prompts keep their existing behavior (LLM interprets literal refs
|
|
10
|
+
* as context markers — works because LLMs are smart).
|
|
11
|
+
*
|
|
12
|
+
* Native types are PRESERVED. When `$input.urls` resolves and `urls`
|
|
13
|
+
* is an array, the substituted value is the array, NOT a JSON string.
|
|
14
|
+
* Tool skills (web_fetch, etc.) need native arrays/objects to work.
|
|
15
|
+
* If a prompt needs the array stringified, the prompt author can do
|
|
16
|
+
* it inside the template (e.g. wrap in explicit JSON.stringify call
|
|
17
|
+
* is not available in YAML — but a downstream LLM can handle arrays
|
|
18
|
+
* shown as `[object Array]` if needed).
|
|
19
|
+
*
|
|
20
|
+
* Why "leave literal if missing" instead of throwing:
|
|
21
|
+
* The skill may receive partial input and choose to return an
|
|
22
|
+
* error JSON (which downstream stages can handle). Throwing
|
|
23
|
+
* would short-circuit that and bury the real cause one layer
|
|
24
|
+
* deeper. The pipeline author can still catch missing inputs at
|
|
25
|
+
* spec-validation time by adding `required: true` to spec.inputs.
|
|
26
|
+
*
|
|
27
|
+
* Recursion: objects and arrays are walked; primitives pass through.
|
|
28
|
+
* Strings get the regex substitution. Numbers, booleans, null return
|
|
29
|
+
* unchanged.
|
|
30
|
+
*/
|
|
31
|
+
const INPUT_REF_PATTERN = /\$\{\s*input\.([A-Za-z_][A-Za-z0-9_]*)\s*\}|\$input\.([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
32
|
+
const STAGE_OUTPUT_REF_PATTERN = /\$\{\s*stages\.([A-Za-z_][A-Za-z0-9_.-]*)\.output\s*\}|\$stages\.([A-Za-z_][A-Za-z0-9_.-]*)\.output/g;
|
|
33
|
+
export function resolveInputRefs(value, ctx) {
|
|
34
|
+
if (typeof value === 'string') {
|
|
35
|
+
return resolveStringRefs(value, ctx);
|
|
36
|
+
}
|
|
37
|
+
if (Array.isArray(value)) {
|
|
38
|
+
return value.map((v) => resolveInputRefs(v, ctx));
|
|
39
|
+
}
|
|
40
|
+
if (value && typeof value === 'object') {
|
|
41
|
+
const out = {};
|
|
42
|
+
for (const [k, v] of Object.entries(value)) {
|
|
43
|
+
out[k] = resolveInputRefs(v, ctx);
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
function resolveStringRefs(text, ctx) {
|
|
50
|
+
const trimmed = text.trim();
|
|
51
|
+
const inputMatches = [...trimmed.matchAll(/\$input\.([A-Za-z_][A-Za-z0-9_]*)|\$\{\s*input\.([A-Za-z_][A-Za-z0-9_]*)\s*\}/g)];
|
|
52
|
+
const stageMatches = [...trimmed.matchAll(/\$stages\.([A-Za-z_][A-Za-z0-9_.-]*)\.output|\$\{\s*stages\.([A-Za-z_][A-Za-z0-9_.-]*)\.output\s*\}/g)];
|
|
53
|
+
const isSingleInputRef = inputMatches.length === 1
|
|
54
|
+
&& stageMatches.length === 0
|
|
55
|
+
&& inputMatches[0][0] === trimmed;
|
|
56
|
+
const isSingleStageRef = stageMatches.length === 1
|
|
57
|
+
&& inputMatches.length === 0
|
|
58
|
+
&& stageMatches[0][0] === trimmed;
|
|
59
|
+
if (isSingleInputRef) {
|
|
60
|
+
const m = inputMatches[0];
|
|
61
|
+
const key = m[1] ?? m[2];
|
|
62
|
+
if (key !== undefined && Object.prototype.hasOwnProperty.call(ctx.inputs, key)) {
|
|
63
|
+
return ctx.inputs[key];
|
|
64
|
+
}
|
|
65
|
+
return text;
|
|
66
|
+
}
|
|
67
|
+
if (isSingleStageRef) {
|
|
68
|
+
const m = stageMatches[0];
|
|
69
|
+
const stageId = m[1] ?? m[2];
|
|
70
|
+
if (stageId !== undefined && Object.prototype.hasOwnProperty.call(ctx.previousOutputs, stageId)) {
|
|
71
|
+
return ctx.previousOutputs[stageId] ?? '';
|
|
72
|
+
}
|
|
73
|
+
return text;
|
|
74
|
+
}
|
|
75
|
+
let out = text;
|
|
76
|
+
out = out.replace(INPUT_REF_PATTERN, (match, key1, key2) => {
|
|
77
|
+
const key = key1 ?? key2;
|
|
78
|
+
if (key !== undefined && Object.prototype.hasOwnProperty.call(ctx.inputs, key)) {
|
|
79
|
+
const v = ctx.inputs[key];
|
|
80
|
+
return stringifyValue(v);
|
|
81
|
+
}
|
|
82
|
+
return match;
|
|
83
|
+
});
|
|
84
|
+
out = out.replace(STAGE_OUTPUT_REF_PATTERN, (match, sid1, sid2) => {
|
|
85
|
+
const stageId = sid1 ?? sid2;
|
|
86
|
+
if (stageId !== undefined && Object.prototype.hasOwnProperty.call(ctx.previousOutputs, stageId)) {
|
|
87
|
+
return ctx.previousOutputs[stageId] ?? '';
|
|
88
|
+
}
|
|
89
|
+
return match;
|
|
90
|
+
});
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
function stringifyValue(v) {
|
|
94
|
+
if (typeof v === 'string')
|
|
95
|
+
return v;
|
|
96
|
+
if (v === null || v === undefined)
|
|
97
|
+
return '';
|
|
98
|
+
return JSON.stringify(v);
|
|
99
|
+
}
|
package/dist/stages.js
CHANGED
|
@@ -16,6 +16,8 @@ import { StageOutputFormatError, StageSkillVersionMismatchError, StageMCPConfigE
|
|
|
16
16
|
import { createModelResolver, } from './model-resolver.js';
|
|
17
17
|
import { computeSkillContentHash } from './content-hash.js';
|
|
18
18
|
import { shouldSkipStage } from './when.js';
|
|
19
|
+
import { resolveInputRefs } from './pipeline-refs.js';
|
|
20
|
+
import { isToolSkill, invokeToolSkill } from './tools/tool-skills.js';
|
|
19
21
|
import { isRetryableStatus, computeBackoffMs, RetryExhaustedError, DEFAULT_RETRY, } from './retry.js';
|
|
20
22
|
import { NoopEventSink, makeStartedEvent as makePipelineStartedEvent, makeCompletedEvent as makePipelineCompletedEvent, makeFailedEvent as makePipelineFailedEvent, makeStageStartedEvent, makeStageCompletedEvent, makeStageSkippedEvent, makeStageFailedEvent, makeStageRetriedEvent, } from './streaming-events.js';
|
|
21
23
|
import { validateRuntimeConstraints, collectRuntimeWarnings, defaultRuntimeConstraints, } from './runtime-constraints.js';
|
|
@@ -361,7 +363,27 @@ export async function executeStage(stage, pipeline, ctx, deps) {
|
|
|
361
363
|
const stageProvider = resolveStageProvider(deps, stage, skillModel);
|
|
362
364
|
const resolvedInput = typeof chainedInput === 'string'
|
|
363
365
|
? resolveStageOutputRefs(chainedInput, ctx.previousOutputs)
|
|
364
|
-
: chainedInput;
|
|
366
|
+
: resolveInputRefs(chainedInput, { inputs: ctx.inputs, previousOutputs: ctx.previousOutputs });
|
|
367
|
+
if (isToolSkill(use.id)) {
|
|
368
|
+
const toolT0 = Date.now();
|
|
369
|
+
const toolResult = await invokeToolSkill(use.id, resolvedInput, {
|
|
370
|
+
agentId: deps.getCurrentAgentId(),
|
|
371
|
+
});
|
|
372
|
+
const toolDurationMs = Date.now() - toolT0;
|
|
373
|
+
deps.onLlmResponse?.(`${stage.id}:${use.id}`, toolResult.text, 'tool');
|
|
374
|
+
lastResult = {
|
|
375
|
+
text: toolResult.text,
|
|
376
|
+
usage: toolResult.usage,
|
|
377
|
+
};
|
|
378
|
+
chainedInput = { input_from_previous_skill: toolResult.text };
|
|
379
|
+
nestedCalls.push({
|
|
380
|
+
step_id: `${stage.id}:${i + 1}`,
|
|
381
|
+
skill_id: use.id,
|
|
382
|
+
duration_ms: toolDurationMs,
|
|
383
|
+
ok: true,
|
|
384
|
+
});
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
365
387
|
const { result, durationMs } = await invokeSkill(skill, resolvedInput, previousContext, label, stageProvider, stage.retry, retryCtx, stage.output_format);
|
|
366
388
|
deps.onLlmResponse?.(`${stage.id}:${use.id}`, result.text, skillModel);
|
|
367
389
|
lastResult = result;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in tool skills — execute natively in the runtime instead of going
|
|
3
|
+
* through LLM-as-skill composition.
|
|
4
|
+
*
|
|
5
|
+
* Why: F27. Skill execution in v0.2 was LLM-only (compose skill.full_md +
|
|
6
|
+
* Input JSON, send to LLM, treat response as skill output). That's fine for
|
|
7
|
+
* prompt-engineering tasks (summarize, translate, classify) but fails for
|
|
8
|
+
* side-effect tasks that need real I/O — web_fetch needs actual HTTP,
|
|
9
|
+
* not LLM roleplay.
|
|
10
|
+
*
|
|
11
|
+
* Architecture: tool skills are matched by `skill.id` (NOT by marketplace
|
|
12
|
+
* lookup). The pipeline can still declare `uses: web_fetch@1.0.0` so the
|
|
13
|
+
* skill appears in provenance + spec_hash, but the runtime intercepts
|
|
14
|
+
* the invocation and runs the native handler instead of going to the LLM.
|
|
15
|
+
*
|
|
16
|
+
* Add new tools here as the catalog grows.
|
|
17
|
+
*/
|
|
18
|
+
export interface ToolSkillContext {
|
|
19
|
+
/** Agent ID of the caller — for logging/tracing. */
|
|
20
|
+
agentId: string;
|
|
21
|
+
/** Abort signal — for cancellation on pipeline timeout. */
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
export interface ToolSkillResult {
|
|
25
|
+
/** String output that becomes the stage's final output (same shape as LLM skill returns). */
|
|
26
|
+
text: string;
|
|
27
|
+
/** Usage — reported as 0 tokens since no LLM was called. */
|
|
28
|
+
usage: {
|
|
29
|
+
inputTokens: number;
|
|
30
|
+
outputTokens: number;
|
|
31
|
+
};
|
|
32
|
+
/** Optional metadata for downstream stages or provenance. */
|
|
33
|
+
meta?: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
export type ToolSkillHandler = (input: unknown, ctx: ToolSkillContext) => Promise<ToolSkillResult>;
|
|
36
|
+
export declare function isToolSkill(skillId: string): boolean;
|
|
37
|
+
export declare function invokeToolSkill(skillId: string, input: unknown, ctx: ToolSkillContext): Promise<ToolSkillResult>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in tool skills — execute natively in the runtime instead of going
|
|
3
|
+
* through LLM-as-skill composition.
|
|
4
|
+
*
|
|
5
|
+
* Why: F27. Skill execution in v0.2 was LLM-only (compose skill.full_md +
|
|
6
|
+
* Input JSON, send to LLM, treat response as skill output). That's fine for
|
|
7
|
+
* prompt-engineering tasks (summarize, translate, classify) but fails for
|
|
8
|
+
* side-effect tasks that need real I/O — web_fetch needs actual HTTP,
|
|
9
|
+
* not LLM roleplay.
|
|
10
|
+
*
|
|
11
|
+
* Architecture: tool skills are matched by `skill.id` (NOT by marketplace
|
|
12
|
+
* lookup). The pipeline can still declare `uses: web_fetch@1.0.0` so the
|
|
13
|
+
* skill appears in provenance + spec_hash, but the runtime intercepts
|
|
14
|
+
* the invocation and runs the native handler instead of going to the LLM.
|
|
15
|
+
*
|
|
16
|
+
* Add new tools here as the catalog grows.
|
|
17
|
+
*/
|
|
18
|
+
const TOOL_SKILLS = {
|
|
19
|
+
web_fetch: webFetchHandler,
|
|
20
|
+
};
|
|
21
|
+
export function isToolSkill(skillId) {
|
|
22
|
+
return skillId in TOOL_SKILLS;
|
|
23
|
+
}
|
|
24
|
+
export async function invokeToolSkill(skillId, input, ctx) {
|
|
25
|
+
const handler = TOOL_SKILLS[skillId];
|
|
26
|
+
if (!handler) {
|
|
27
|
+
throw new Error(`Tool skill '${skillId}' is registered but has no handler`);
|
|
28
|
+
}
|
|
29
|
+
return handler(input, ctx);
|
|
30
|
+
}
|
|
31
|
+
async function webFetchHandler(input, ctx) {
|
|
32
|
+
const { urls, max_chars, max_per_article_chars } = parseFetchInput(input);
|
|
33
|
+
const cap = typeof max_chars === 'number' && max_chars > 0
|
|
34
|
+
? max_chars
|
|
35
|
+
: (typeof max_per_article_chars === 'number' && max_per_article_chars > 0
|
|
36
|
+
? max_per_article_chars
|
|
37
|
+
: 4000);
|
|
38
|
+
const articles = [];
|
|
39
|
+
for (const url of urls) {
|
|
40
|
+
try {
|
|
41
|
+
const res = await fetch(url, { signal: ctx.signal });
|
|
42
|
+
const raw = await res.text();
|
|
43
|
+
const content = stripHtmlToText(raw).slice(0, cap);
|
|
44
|
+
articles.push({ url, content, status: res.status });
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
articles.push({
|
|
48
|
+
url,
|
|
49
|
+
content: '',
|
|
50
|
+
status: 0,
|
|
51
|
+
error: e instanceof Error ? e.message : String(e),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const text = articles.map((a) => {
|
|
56
|
+
if (a.error) {
|
|
57
|
+
return `URL: ${a.url}\nSTATUS: error\nERROR: ${a.error}\nCONTENT:\n`;
|
|
58
|
+
}
|
|
59
|
+
return `URL: ${a.url}\nSTATUS: ${a.status}\nCONTENT:\n${a.content}\n`;
|
|
60
|
+
}).join('\n---\n\n');
|
|
61
|
+
return {
|
|
62
|
+
text,
|
|
63
|
+
usage: { inputTokens: 0, outputTokens: 0 },
|
|
64
|
+
meta: { articles, urls_requested: urls.length, cap_chars: cap },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function parseFetchInput(input) {
|
|
68
|
+
if (!input || typeof input !== 'object') {
|
|
69
|
+
throw new Error('web_fetch requires input object');
|
|
70
|
+
}
|
|
71
|
+
const obj = input;
|
|
72
|
+
const urlsRaw = obj.urls;
|
|
73
|
+
if (!Array.isArray(urlsRaw)) {
|
|
74
|
+
throw new Error('web_fetch requires `urls: string[]`');
|
|
75
|
+
}
|
|
76
|
+
const urls = urlsRaw.filter((u) => typeof u === 'string');
|
|
77
|
+
if (urls.length === 0) {
|
|
78
|
+
throw new Error('web_fetch requires non-empty `urls: string[]`');
|
|
79
|
+
}
|
|
80
|
+
const maxChars = typeof obj.max_chars === 'number' ? obj.max_chars : undefined;
|
|
81
|
+
const maxPerArticleChars = typeof obj.max_per_article_chars === 'number' ? obj.max_per_article_chars : undefined;
|
|
82
|
+
return { urls, max_chars: maxChars, max_per_article_chars: maxPerArticleChars };
|
|
83
|
+
}
|
|
84
|
+
function stripHtmlToText(html) {
|
|
85
|
+
return html
|
|
86
|
+
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
|
87
|
+
.replace(/<style[\s\S]*?<\/style>/gi, '')
|
|
88
|
+
.replace(/<noscript[\s\S]*?<\/noscript>/gi, '')
|
|
89
|
+
.replace(/<[^>]+>/g, ' ')
|
|
90
|
+
.replace(/ /g, ' ')
|
|
91
|
+
.replace(/&/g, '&')
|
|
92
|
+
.replace(/</g, '<')
|
|
93
|
+
.replace(/>/g, '>')
|
|
94
|
+
.replace(/"/g, '"')
|
|
95
|
+
.replace(/'/g, "'")
|
|
96
|
+
.replace(/\s+/g, ' ')
|
|
97
|
+
.trim();
|
|
98
|
+
}
|
package/package.json
CHANGED