@davidorex/pi-jit-agents 0.14.6 → 0.26.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.
- package/dist/agent-spec.d.ts.map +1 -1
- package/dist/agent-spec.js +69 -5
- package/dist/agent-spec.js.map +1 -1
- package/dist/budget-enforcer.d.ts +43 -0
- package/dist/budget-enforcer.d.ts.map +1 -0
- package/dist/budget-enforcer.js +172 -0
- package/dist/budget-enforcer.js.map +1 -0
- package/dist/compile.d.ts +39 -0
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +357 -24
- package/dist/compile.js.map +1 -1
- package/dist/dispatch-inline.d.ts +25 -0
- package/dist/dispatch-inline.d.ts.map +1 -0
- package/dist/dispatch-inline.js +41 -0
- package/dist/dispatch-inline.js.map +1 -0
- package/dist/field-path.d.ts +32 -0
- package/dist/field-path.d.ts.map +1 -0
- package/dist/field-path.js +48 -0
- package/dist/field-path.js.map +1 -0
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/dist/jit-runtime.d.ts +3 -3
- package/dist/jit-runtime.d.ts.map +1 -1
- package/dist/jit-runtime.js +5 -5
- package/dist/jit-runtime.js.map +1 -1
- package/dist/markers.d.ts +28 -0
- package/dist/markers.d.ts.map +1 -0
- package/dist/markers.js +36 -0
- package/dist/markers.js.map +1 -0
- package/dist/renderer-registry.d.ts +44 -0
- package/dist/renderer-registry.d.ts.map +1 -0
- package/dist/renderer-registry.js +128 -0
- package/dist/renderer-registry.js.map +1 -0
- package/dist/template.d.ts.map +1 -1
- package/dist/template.js +10 -3
- package/dist/template.js.map +1 -1
- package/dist/trace-redactor.d.ts +1 -1
- package/dist/trace-redactor.js +1 -1
- package/dist/trace-writer.js +2 -2
- package/dist/types.d.ts +82 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -3
package/dist/agent-spec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-spec.d.ts","sourceRoot":"","sources":["../src/agent-spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent-spec.d.ts","sourceRoot":"","sources":["../src/agent-spec.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,SAAS,EAAmB,WAAW,EAAE,MAAM,YAAY,CAAC;AAuI1E;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAiD1D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,CAyB/E"}
|
package/dist/agent-spec.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import fs from "node:fs";
|
|
8
8
|
import os from "node:os";
|
|
9
9
|
import path from "node:path";
|
|
10
|
+
import { tryResolveContextDir } from "@davidorex/pi-context/context-dir";
|
|
10
11
|
import { parse as parseYaml } from "yaml";
|
|
11
12
|
import { AgentNotFoundError, AgentParseError } from "./errors.js";
|
|
12
13
|
/**
|
|
@@ -43,6 +44,60 @@ function resolveSpecPath(value, specDir) {
|
|
|
43
44
|
return value;
|
|
44
45
|
return path.resolve(specDir, value);
|
|
45
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Validate and normalise a single `contextBlocks` entry.
|
|
49
|
+
*
|
|
50
|
+
* Accepts:
|
|
51
|
+
* - a non-empty string (whole-block reference, the established surface), or
|
|
52
|
+
* - an object with a required string `name` and optional `item` (string),
|
|
53
|
+
* `focus` (record of string→string), and `depth` (non-negative number).
|
|
54
|
+
*
|
|
55
|
+
* Throws `AgentParseError` with a descriptive message naming the offending
|
|
56
|
+
* entry's index and the failing constraint. Plan 4 (Wave 2) consumes the
|
|
57
|
+
* resulting union shape; this helper does not assign any runtime semantics.
|
|
58
|
+
*/
|
|
59
|
+
function parseContextBlockEntry(entry, index, agentName, filePath) {
|
|
60
|
+
if (typeof entry === "string") {
|
|
61
|
+
if (entry.length === 0) {
|
|
62
|
+
throw new AgentParseError(agentName, filePath, new Error(`contextBlocks[${index}]: bare-string entry must be non-empty`));
|
|
63
|
+
}
|
|
64
|
+
return entry;
|
|
65
|
+
}
|
|
66
|
+
if (entry === null || typeof entry !== "object" || Array.isArray(entry)) {
|
|
67
|
+
throw new AgentParseError(agentName, filePath, new Error(`contextBlocks[${index}]: entry must be a string or an object with at least { name: string }; got ${Array.isArray(entry) ? "array" : entry === null ? "null" : typeof entry}`));
|
|
68
|
+
}
|
|
69
|
+
const obj = entry;
|
|
70
|
+
if (typeof obj.name !== "string" || obj.name.length === 0) {
|
|
71
|
+
throw new AgentParseError(agentName, filePath, new Error(`contextBlocks[${index}]: object form requires a non-empty string \`name\` field`));
|
|
72
|
+
}
|
|
73
|
+
const out = { name: obj.name };
|
|
74
|
+
if (obj.item !== undefined) {
|
|
75
|
+
if (typeof obj.item !== "string" || obj.item.length === 0) {
|
|
76
|
+
throw new AgentParseError(agentName, filePath, new Error(`contextBlocks[${index}].item: when present must be a non-empty string`));
|
|
77
|
+
}
|
|
78
|
+
out.item = obj.item;
|
|
79
|
+
}
|
|
80
|
+
if (obj.focus !== undefined) {
|
|
81
|
+
if (obj.focus === null || typeof obj.focus !== "object" || Array.isArray(obj.focus)) {
|
|
82
|
+
throw new AgentParseError(agentName, filePath, new Error(`contextBlocks[${index}].focus: when present must be an object mapping string keys to string values`));
|
|
83
|
+
}
|
|
84
|
+
const focus = {};
|
|
85
|
+
for (const [k, v] of Object.entries(obj.focus)) {
|
|
86
|
+
if (typeof v !== "string") {
|
|
87
|
+
throw new AgentParseError(agentName, filePath, new Error(`contextBlocks[${index}].focus.${k}: value must be a string; got ${typeof v}`));
|
|
88
|
+
}
|
|
89
|
+
focus[k] = v;
|
|
90
|
+
}
|
|
91
|
+
out.focus = focus;
|
|
92
|
+
}
|
|
93
|
+
if (obj.depth !== undefined) {
|
|
94
|
+
if (typeof obj.depth !== "number" || !Number.isFinite(obj.depth) || obj.depth < 0 || !Number.isInteger(obj.depth)) {
|
|
95
|
+
throw new AgentParseError(agentName, filePath, new Error(`contextBlocks[${index}].depth: when present must be a non-negative integer; got ${String(obj.depth)}`));
|
|
96
|
+
}
|
|
97
|
+
out.depth = obj.depth;
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
46
101
|
/**
|
|
47
102
|
* Parse a YAML agent spec file into a fully-resolved AgentSpec.
|
|
48
103
|
*
|
|
@@ -89,7 +144,9 @@ export function parseAgentYaml(filePath) {
|
|
|
89
144
|
inputSchema: spec.input,
|
|
90
145
|
outputFormat: spec.output?.format,
|
|
91
146
|
outputSchema: resolveSpecPath(spec.output?.schema, specDir),
|
|
92
|
-
contextBlocks: Array.isArray(spec.contextBlocks)
|
|
147
|
+
contextBlocks: Array.isArray(spec.contextBlocks)
|
|
148
|
+
? spec.contextBlocks.map((entry, index) => parseContextBlockEntry(entry, index, spec.name || name, filePath))
|
|
149
|
+
: undefined,
|
|
93
150
|
loadedFrom: specDir,
|
|
94
151
|
};
|
|
95
152
|
}
|
|
@@ -108,11 +165,18 @@ export function parseAgentYaml(filePath) {
|
|
|
108
165
|
*/
|
|
109
166
|
export function createAgentLoader(ctx) {
|
|
110
167
|
const userTier = ctx.userDir ?? path.join(os.homedir(), ".pi", "agent", "agents");
|
|
168
|
+
// Resolve the project tier once (FGAP-074 C3): pointer-less repos degrade by
|
|
169
|
+
// omitting the project-tier search path so the loader still searches the
|
|
170
|
+
// user/builtin tiers and ultimately throws its normal AgentNotFoundError
|
|
171
|
+
// (NOT BootstrapNotFoundError). `agentsDir(cwd)` was `<contextDir>/agents`,
|
|
172
|
+
// so the inline equivalent is `path.join(base, "agents", ...)`.
|
|
173
|
+
const base = tryResolveContextDir(ctx.cwd);
|
|
111
174
|
return (name) => {
|
|
112
|
-
const searchPaths = [
|
|
113
|
-
|
|
114
|
-
path.join(
|
|
115
|
-
|
|
175
|
+
const searchPaths = [];
|
|
176
|
+
if (base !== null) {
|
|
177
|
+
searchPaths.push(path.join(base, "agents", `${name}.agent.yaml`));
|
|
178
|
+
}
|
|
179
|
+
searchPaths.push(path.join(userTier, `${name}.agent.yaml`));
|
|
116
180
|
if (ctx.builtinDir) {
|
|
117
181
|
searchPaths.push(path.join(ctx.builtinDir, `${name}.agent.yaml`));
|
|
118
182
|
}
|
package/dist/agent-spec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-spec.js","sourceRoot":"","sources":["../src/agent-spec.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,KAAK,IAAI,SAAS,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGlE;;;;GAIG;AACH,SAAS,kBAAkB,CAAC,KAAc;IACzC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,UAAU,IAAI,KAAK,EAAE,CAAC;QACxE,OAAO,EAAE,QAAQ,EAAG,KAA8B,CAAC,QAAQ,EAAE,CAAC;IAC/D,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC/B,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;YACvG,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;QAC5B,CAAC;QACD,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAC1B,CAAC;IACD,OA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AAC,CAAC;IACjD,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render-time prompt-budget enforcement primitive.
|
|
3
|
+
*
|
|
4
|
+
* Reads `x-prompt-budget` annotations from a JSON Schema field and, when the
|
|
5
|
+
* rendered output exceeds the declared budget, returns tail-truncated output
|
|
6
|
+
* plus a structured warning. Annotation absence is pass-through; malformed
|
|
7
|
+
* annotations throw with the field path named in the error.
|
|
8
|
+
*
|
|
9
|
+
* Plan 5 of the per-item-macros work ships this primitive plus the schema
|
|
10
|
+
* annotations themselves (`.project/schemas/*.schema.json`). Per-item macros
|
|
11
|
+
* (Plans 6 and 7) consume the primitive to gate field rendering.
|
|
12
|
+
*
|
|
13
|
+
* Token measurement is intentionally a coarse approximation: whitespace +
|
|
14
|
+
* Unicode-punctuation split, not a tokenizer-accurate count tied to any
|
|
15
|
+
* specific model. The aim is a render-time signal, not a billing surface.
|
|
16
|
+
*/
|
|
17
|
+
export interface PromptBudget {
|
|
18
|
+
tokens?: number;
|
|
19
|
+
words?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface BudgetWarning {
|
|
22
|
+
field: string;
|
|
23
|
+
budget: PromptBudget;
|
|
24
|
+
actual: {
|
|
25
|
+
tokens: number;
|
|
26
|
+
words: number;
|
|
27
|
+
};
|
|
28
|
+
truncated: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface BudgetResult {
|
|
31
|
+
output: string;
|
|
32
|
+
warning: BudgetWarning | null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* If the schema field at `fieldPath` declares `x-prompt-budget`, measure
|
|
36
|
+
* `rendered` and return tail-truncated output + warning when over budget.
|
|
37
|
+
* If the field has no annotation, return `{ output: rendered, warning: null }`.
|
|
38
|
+
*
|
|
39
|
+
* Empty `rendered` short-circuits to pass-through regardless of annotation —
|
|
40
|
+
* there is nothing to measure or truncate.
|
|
41
|
+
*/
|
|
42
|
+
export declare function enforceBudget(rendered: string, schema: object, fieldPath: string): BudgetResult;
|
|
43
|
+
//# sourceMappingURL=budget-enforcer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget-enforcer.d.ts","sourceRoot":"","sources":["../src/budget-enforcer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,WAAW,YAAY;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,aAAa;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,YAAY,CAAC;IACrB,MAAM,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1C,SAAS,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,aAAa,GAAG,IAAI,CAAC;CAC9B;AA6HD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,YAAY,CAkC/F"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render-time prompt-budget enforcement primitive.
|
|
3
|
+
*
|
|
4
|
+
* Reads `x-prompt-budget` annotations from a JSON Schema field and, when the
|
|
5
|
+
* rendered output exceeds the declared budget, returns tail-truncated output
|
|
6
|
+
* plus a structured warning. Annotation absence is pass-through; malformed
|
|
7
|
+
* annotations throw with the field path named in the error.
|
|
8
|
+
*
|
|
9
|
+
* Plan 5 of the per-item-macros work ships this primitive plus the schema
|
|
10
|
+
* annotations themselves (`.project/schemas/*.schema.json`). Per-item macros
|
|
11
|
+
* (Plans 6 and 7) consume the primitive to gate field rendering.
|
|
12
|
+
*
|
|
13
|
+
* Token measurement is intentionally a coarse approximation: whitespace +
|
|
14
|
+
* Unicode-punctuation split, not a tokenizer-accurate count tied to any
|
|
15
|
+
* specific model. The aim is a render-time signal, not a billing surface.
|
|
16
|
+
*/
|
|
17
|
+
const TRUNCATION_MARKER = "[…truncated to budget]";
|
|
18
|
+
/**
|
|
19
|
+
* Approximate token count: split on Unicode whitespace + punctuation, drop empties.
|
|
20
|
+
* Not tokenizer-accurate; meant as a render-time proxy for prompt-size signal.
|
|
21
|
+
*/
|
|
22
|
+
function countTokens(text) {
|
|
23
|
+
if (text === "")
|
|
24
|
+
return 0;
|
|
25
|
+
return text.split(/[\s\p{P}]+/u).filter(Boolean).length;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Word count: whitespace split, drop empties.
|
|
29
|
+
*/
|
|
30
|
+
function countWords(text) {
|
|
31
|
+
if (text === "")
|
|
32
|
+
return 0;
|
|
33
|
+
return text.split(/\s+/).filter(Boolean).length;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Resolve a JSON-pointer-style path against a schema object.
|
|
37
|
+
*
|
|
38
|
+
* Path interpretation:
|
|
39
|
+
* - Leading "/" is optional; treated as the root delimiter.
|
|
40
|
+
* - Segments are split on "/" with no JSON Pointer escape handling
|
|
41
|
+
* (we do not encounter "~" or "/" inside schema keys in practice).
|
|
42
|
+
* - Each segment is looked up as a property key on the current node.
|
|
43
|
+
* - Returns null if any segment is missing.
|
|
44
|
+
*
|
|
45
|
+
* Example: "/properties/research/items/properties/findings_summary"
|
|
46
|
+
*/
|
|
47
|
+
function resolveSchemaField(schema, fieldPath) {
|
|
48
|
+
const trimmed = fieldPath.startsWith("/") ? fieldPath.slice(1) : fieldPath;
|
|
49
|
+
if (trimmed === "")
|
|
50
|
+
return schema;
|
|
51
|
+
const segments = trimmed.split("/");
|
|
52
|
+
let cursor = schema;
|
|
53
|
+
for (const segment of segments) {
|
|
54
|
+
if (cursor === null || typeof cursor !== "object")
|
|
55
|
+
return null;
|
|
56
|
+
const next = cursor[segment];
|
|
57
|
+
if (next === undefined)
|
|
58
|
+
return null;
|
|
59
|
+
cursor = next;
|
|
60
|
+
}
|
|
61
|
+
if (cursor === null || typeof cursor !== "object")
|
|
62
|
+
return null;
|
|
63
|
+
return cursor;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Validate a raw `x-prompt-budget` annotation into a typed PromptBudget.
|
|
67
|
+
* Throws AgentBudgetError-style Error naming the field on malformed input.
|
|
68
|
+
*/
|
|
69
|
+
function parseBudget(raw, fieldPath) {
|
|
70
|
+
if (raw === null || typeof raw !== "object") {
|
|
71
|
+
throw new Error(`x-prompt-budget at ${fieldPath} must be an object, got ${typeof raw}`);
|
|
72
|
+
}
|
|
73
|
+
const obj = raw;
|
|
74
|
+
const out = {};
|
|
75
|
+
if ("tokens" in obj) {
|
|
76
|
+
const t = obj.tokens;
|
|
77
|
+
if (typeof t !== "number" || !Number.isFinite(t) || t < 0 || !Number.isInteger(t)) {
|
|
78
|
+
throw new Error(`x-prompt-budget at ${fieldPath}: tokens must be a non-negative integer, got ${JSON.stringify(t)}`);
|
|
79
|
+
}
|
|
80
|
+
out.tokens = t;
|
|
81
|
+
}
|
|
82
|
+
if ("words" in obj) {
|
|
83
|
+
const w = obj.words;
|
|
84
|
+
if (typeof w !== "number" || !Number.isFinite(w) || w < 0 || !Number.isInteger(w)) {
|
|
85
|
+
throw new Error(`x-prompt-budget at ${fieldPath}: words must be a non-negative integer, got ${JSON.stringify(w)}`);
|
|
86
|
+
}
|
|
87
|
+
out.words = w;
|
|
88
|
+
}
|
|
89
|
+
if (out.tokens === undefined && out.words === undefined) {
|
|
90
|
+
throw new Error(`x-prompt-budget at ${fieldPath} declares neither tokens nor words; at least one must be present`);
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Tail-truncate `text` so that it fits both `maxTokens` and `maxWords` (when set).
|
|
96
|
+
* Strategy: binary-search the smallest character-prefix length whose token+word
|
|
97
|
+
* counts (with the marker appended) satisfy both budgets. The marker itself
|
|
98
|
+
* adds a small fixed cost that is included in the count.
|
|
99
|
+
*
|
|
100
|
+
* If `text` already fits, returns `text` unchanged with `truncated: false`.
|
|
101
|
+
*/
|
|
102
|
+
function tailTruncate(text, budget) {
|
|
103
|
+
const fitsBudget = (s) => {
|
|
104
|
+
if (budget.tokens !== undefined && countTokens(s) > budget.tokens)
|
|
105
|
+
return false;
|
|
106
|
+
if (budget.words !== undefined && countWords(s) > budget.words)
|
|
107
|
+
return false;
|
|
108
|
+
return true;
|
|
109
|
+
};
|
|
110
|
+
if (fitsBudget(text)) {
|
|
111
|
+
return { output: text, truncated: false };
|
|
112
|
+
}
|
|
113
|
+
// If even the marker alone is over budget, return just the marker (best effort).
|
|
114
|
+
if (!fitsBudget(TRUNCATION_MARKER)) {
|
|
115
|
+
return { output: TRUNCATION_MARKER, truncated: true };
|
|
116
|
+
}
|
|
117
|
+
// Binary-search the largest prefix length such that prefix + " " + marker fits.
|
|
118
|
+
let lo = 0;
|
|
119
|
+
let hi = text.length;
|
|
120
|
+
let best = 0;
|
|
121
|
+
while (lo <= hi) {
|
|
122
|
+
const mid = (lo + hi) >> 1;
|
|
123
|
+
const candidate = `${text.slice(0, mid).trimEnd()} ${TRUNCATION_MARKER}`;
|
|
124
|
+
if (fitsBudget(candidate)) {
|
|
125
|
+
best = mid;
|
|
126
|
+
lo = mid + 1;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
hi = mid - 1;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const output = `${text.slice(0, best).trimEnd()} ${TRUNCATION_MARKER}`;
|
|
133
|
+
return { output, truncated: true };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* If the schema field at `fieldPath` declares `x-prompt-budget`, measure
|
|
137
|
+
* `rendered` and return tail-truncated output + warning when over budget.
|
|
138
|
+
* If the field has no annotation, return `{ output: rendered, warning: null }`.
|
|
139
|
+
*
|
|
140
|
+
* Empty `rendered` short-circuits to pass-through regardless of annotation —
|
|
141
|
+
* there is nothing to measure or truncate.
|
|
142
|
+
*/
|
|
143
|
+
export function enforceBudget(rendered, schema, fieldPath) {
|
|
144
|
+
if (rendered === "") {
|
|
145
|
+
return { output: "", warning: null };
|
|
146
|
+
}
|
|
147
|
+
const fieldNode = resolveSchemaField(schema, fieldPath);
|
|
148
|
+
if (fieldNode === null) {
|
|
149
|
+
return { output: rendered, warning: null };
|
|
150
|
+
}
|
|
151
|
+
const annotation = fieldNode["x-prompt-budget"];
|
|
152
|
+
if (annotation === undefined) {
|
|
153
|
+
return { output: rendered, warning: null };
|
|
154
|
+
}
|
|
155
|
+
const budget = parseBudget(annotation, fieldPath);
|
|
156
|
+
const actualTokens = countTokens(rendered);
|
|
157
|
+
const actualWords = countWords(rendered);
|
|
158
|
+
const overTokens = budget.tokens !== undefined && actualTokens > budget.tokens;
|
|
159
|
+
const overWords = budget.words !== undefined && actualWords > budget.words;
|
|
160
|
+
if (!overTokens && !overWords) {
|
|
161
|
+
return { output: rendered, warning: null };
|
|
162
|
+
}
|
|
163
|
+
const { output, truncated } = tailTruncate(rendered, budget);
|
|
164
|
+
const warning = {
|
|
165
|
+
field: fieldPath,
|
|
166
|
+
budget,
|
|
167
|
+
actual: { tokens: actualTokens, words: actualWords },
|
|
168
|
+
truncated,
|
|
169
|
+
};
|
|
170
|
+
return { output, warning };
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=budget-enforcer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget-enforcer.js","sourceRoot":"","sources":["../src/budget-enforcer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAmBH,MAAM,iBAAiB,GAAG,wBAAwB,CAAC;AAEnD;;;GAGG;AACH,SAAS,WAAW,CAAC,IAAY;IAChC,IAAI,IAAI,KAAK,EAAE;QAAE,OAAO,CAAC,CAAC;IAC1B,OAAO,IAAI,CAAC,K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EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC5C,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC7D,MAAM,OAAO,GAAkB;QAC9B,KAAK,EAAE,SAAS;QAChB,MAAM;QACN,MAAM,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE;QACpD,SAAS;KACT,CAAC;IACF,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;AAC5B,CAAC"}
|
package/dist/compile.d.ts
CHANGED
|
@@ -1,4 +1,43 @@
|
|
|
1
|
+
import { type ItemLocation } from "@davidorex/pi-context";
|
|
2
|
+
import type nunjucks from "nunjucks";
|
|
3
|
+
import { type BudgetWarning } from "./budget-enforcer.js";
|
|
4
|
+
import type { RendererRegistry } from "./renderer-registry.js";
|
|
1
5
|
import type { AgentSpec, CompileContext, CompiledAgent } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Register the composition-time Nunjucks globals on a template environment.
|
|
8
|
+
*
|
|
9
|
+
* Three globals are registered, scoped to the closure-captured state passed in:
|
|
10
|
+
*
|
|
11
|
+
* - `resolve(id)` — looks up an item by ID via the lazy idIndex; returns
|
|
12
|
+
* ItemLocation or null. Used by macros to dispatch
|
|
13
|
+
* cross-block references.
|
|
14
|
+
* - `render_recursive(loc, depth)` — renders an item via the registered
|
|
15
|
+
* per-item macro (resolved through the renderer registry).
|
|
16
|
+
* Cycle-detected via the visited set.
|
|
17
|
+
* - `enforceBudget(rendered, blockName, fieldPathOrShorthand)` — measures
|
|
18
|
+
* rendered text against the field's `x-prompt-budget`
|
|
19
|
+
* annotation, returning truncated output when over budget;
|
|
20
|
+
* pass-through when annotation absent. Truncation warnings
|
|
21
|
+
* are appended to `warningsCollector` (closure-captured by
|
|
22
|
+
* the caller) so callers can surface them after compile
|
|
23
|
+
* returns.
|
|
24
|
+
*
|
|
25
|
+
* Used by both `compileAgent` (this module) and `renderItemById` in pi-workflows
|
|
26
|
+
* — the single source of truth for the composition-globals contract. Adding
|
|
27
|
+
* a new global, changing a signature, or adjusting a fallback means editing
|
|
28
|
+
* here and the contract propagates to every caller.
|
|
29
|
+
*
|
|
30
|
+
* The visited-set lifetime is one call to this function — sequential calls on
|
|
31
|
+
* the same env get isolated cycle scopes. addGlobal overwrites prior bindings,
|
|
32
|
+
* which is intentional (each composition pass owns its own scope).
|
|
33
|
+
*/
|
|
34
|
+
export declare function registerCompositionGlobals(opts: {
|
|
35
|
+
env: nunjucks.Environment;
|
|
36
|
+
cwd: string;
|
|
37
|
+
rendererRegistry: RendererRegistry | undefined;
|
|
38
|
+
getIdIndex: () => Map<string, ItemLocation>;
|
|
39
|
+
warningsCollector: BudgetWarning[];
|
|
40
|
+
}): void;
|
|
2
41
|
/**
|
|
3
42
|
* Compile an AgentSpec into a CompiledAgent.
|
|
4
43
|
*
|
package/dist/compile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../src/compile.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../src/compile.ts"],"names":[],"mappings":"AAcA,OAAO,EAAgB,KAAK,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAGxE,OAAO,KAAK,QAAQ,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,KAAK,aAAa,EAAiB,MAAM,sBAAsB,CAAC;AAKzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE/D,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,aAAa,EAAmB,MAAM,YAAY,CAAC;AAE5F;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE;IAChD,GAAG,EAAE,QAAQ,CAAC,WAAW,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAC/C,UAAU,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAC5C,iBAAiB,EAAE,aAAa,EAAE,CAAC;CACnC,GAAG,IAAI,CA+EP;AA8DD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,cAAc,GAAG,aAAa,CA2WhF"}
|