wotann 0.5.39 → 0.5.41
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/autopilot/completion-oracle.d.ts +71 -0
- package/dist/autopilot/completion-oracle.d.ts.map +1 -1
- package/dist/autopilot/completion-oracle.js +69 -2
- package/dist/autopilot/completion-oracle.js.map +1 -1
- package/dist/context/compaction-floor.d.ts +100 -0
- package/dist/context/compaction-floor.d.ts.map +1 -0
- package/dist/context/compaction-floor.js +94 -0
- package/dist/context/compaction-floor.js.map +1 -0
- package/dist/context/compaction.d.ts +27 -1
- package/dist/context/compaction.d.ts.map +1 -1
- package/dist/context/compaction.js +31 -0
- package/dist/context/compaction.js.map +1 -1
- package/dist/daemon/kairos-rpc.d.ts +1 -0
- package/dist/daemon/kairos-rpc.d.ts.map +1 -1
- package/dist/daemon/kairos-rpc.js +24 -0
- package/dist/daemon/kairos-rpc.js.map +1 -1
- package/dist/daemon/rpc-handlers/ports-rpc.d.ts +100 -0
- package/dist/daemon/rpc-handlers/ports-rpc.d.ts.map +1 -0
- package/dist/daemon/rpc-handlers/ports-rpc.js +278 -0
- package/dist/daemon/rpc-handlers/ports-rpc.js.map +1 -0
- package/dist/hooks/agentmemory-event-taxonomy.d.ts +144 -0
- package/dist/hooks/agentmemory-event-taxonomy.d.ts.map +1 -0
- package/dist/hooks/agentmemory-event-taxonomy.js +85 -0
- package/dist/hooks/agentmemory-event-taxonomy.js.map +1 -0
- package/dist/hooks/built-in.d.ts.map +1 -1
- package/dist/hooks/built-in.js +7 -3
- package/dist/hooks/built-in.js.map +1 -1
- package/dist/hooks/engine.d.ts +14 -0
- package/dist/hooks/engine.d.ts.map +1 -1
- package/dist/hooks/engine.js +62 -0
- package/dist/hooks/engine.js.map +1 -1
- package/dist/index.js +91 -2
- package/dist/index.js.map +1 -1
- package/dist/lib.d.ts +1 -1
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +1 -1
- package/dist/lib.js.map +1 -1
- package/dist/memory/injection-scanner.d.ts +78 -0
- package/dist/memory/injection-scanner.d.ts.map +1 -0
- package/dist/memory/injection-scanner.js +204 -0
- package/dist/memory/injection-scanner.js.map +1 -0
- package/dist/middleware/output-truncation.d.ts +18 -5
- package/dist/middleware/output-truncation.d.ts.map +1 -1
- package/dist/middleware/output-truncation.js +73 -1
- package/dist/middleware/output-truncation.js.map +1 -1
- package/dist/middleware/ttsr.d.ts +79 -3
- package/dist/middleware/ttsr.d.ts.map +1 -1
- package/dist/middleware/ttsr.js +136 -16
- package/dist/middleware/ttsr.js.map +1 -1
- package/dist/orchestration/autonomous.d.ts +182 -0
- package/dist/orchestration/autonomous.d.ts.map +1 -1
- package/dist/orchestration/autonomous.js +288 -1
- package/dist/orchestration/autonomous.js.map +1 -1
- package/dist/orchestration/ports-bridge.d.ts +325 -0
- package/dist/orchestration/ports-bridge.d.ts.map +1 -0
- package/dist/orchestration/ports-bridge.js +712 -0
- package/dist/orchestration/ports-bridge.js.map +1 -0
- package/dist/orchestration/textgrad-refinement.d.ts +123 -0
- package/dist/orchestration/textgrad-refinement.d.ts.map +1 -0
- package/dist/orchestration/textgrad-refinement.js +111 -0
- package/dist/orchestration/textgrad-refinement.js.map +1 -0
- package/dist/prompt/engine.d.ts +15 -0
- package/dist/prompt/engine.d.ts.map +1 -1
- package/dist/prompt/engine.js +20 -7
- package/dist/prompt/engine.js.map +1 -1
- package/dist/prompt/modules/capabilities.d.ts.map +1 -1
- package/dist/prompt/modules/capabilities.js +1 -0
- package/dist/prompt/modules/capabilities.js.map +1 -1
- package/dist/prompt/modules/caveman.d.ts.map +1 -1
- package/dist/prompt/modules/caveman.js +1 -0
- package/dist/prompt/modules/caveman.js.map +1 -1
- package/dist/prompt/modules/conventions.d.ts.map +1 -1
- package/dist/prompt/modules/conventions.js +1 -0
- package/dist/prompt/modules/conventions.js.map +1 -1
- package/dist/prompt/modules/identity.d.ts.map +1 -1
- package/dist/prompt/modules/identity.js +1 -0
- package/dist/prompt/modules/identity.js.map +1 -1
- package/dist/prompt/modules/index.d.ts +29 -0
- package/dist/prompt/modules/index.d.ts.map +1 -1
- package/dist/prompt/modules/index.js +51 -9
- package/dist/prompt/modules/index.js.map +1 -1
- package/dist/prompt/modules/llms-txt.d.ts.map +1 -1
- package/dist/prompt/modules/llms-txt.js +2 -4
- package/dist/prompt/modules/llms-txt.js.map +1 -1
- package/dist/prompt/modules/safety.d.ts.map +1 -1
- package/dist/prompt/modules/safety.js +1 -0
- package/dist/prompt/modules/safety.js.map +1 -1
- package/dist/prompt/modules/security.d.ts.map +1 -1
- package/dist/prompt/modules/security.js +1 -0
- package/dist/prompt/modules/security.js.map +1 -1
- package/dist/prompt/modules/skills.d.ts.map +1 -1
- package/dist/prompt/modules/skills.js +1 -0
- package/dist/prompt/modules/skills.js.map +1 -1
- package/dist/prompt/modules/tools.d.ts.map +1 -1
- package/dist/prompt/modules/tools.js +1 -0
- package/dist/prompt/modules/tools.js.map +1 -1
- package/dist/prompt/modules/user.d.ts.map +1 -1
- package/dist/prompt/modules/user.js +1 -0
- package/dist/prompt/modules/user.js.map +1 -1
- package/dist/providers/credential-pool.d.ts +45 -1
- package/dist/providers/credential-pool.d.ts.map +1 -1
- package/dist/providers/credential-pool.js +94 -1
- package/dist/providers/credential-pool.js.map +1 -1
- package/dist/providers/sticky-rotation-wire.d.ts +133 -0
- package/dist/providers/sticky-rotation-wire.d.ts.map +1 -0
- package/dist/providers/sticky-rotation-wire.js +185 -0
- package/dist/providers/sticky-rotation-wire.js.map +1 -0
- package/dist/runtime-hooks/ttsr-rule.d.ts +82 -0
- package/dist/runtime-hooks/ttsr-rule.d.ts.map +1 -0
- package/dist/runtime-hooks/ttsr-rule.js +207 -0
- package/dist/runtime-hooks/ttsr-rule.js.map +1 -0
- package/dist/runtime-hooks/ttsr-runner.d.ts +129 -0
- package/dist/runtime-hooks/ttsr-runner.d.ts.map +1 -0
- package/dist/runtime-hooks/ttsr-runner.js +193 -0
- package/dist/runtime-hooks/ttsr-runner.js.map +1 -0
- package/dist/runtime-hooks/ttsr-scope.d.ts +115 -0
- package/dist/runtime-hooks/ttsr-scope.d.ts.map +1 -0
- package/dist/runtime-hooks/ttsr-scope.js +378 -0
- package/dist/runtime-hooks/ttsr-scope.js.map +1 -0
- package/dist/sandbox/unified-exec.d.ts.map +1 -1
- package/dist/sandbox/unified-exec.js +6 -1
- package/dist/sandbox/unified-exec.js.map +1 -1
- package/dist/skills/cli-anything.d.ts.map +1 -1
- package/dist/skills/cli-anything.js +4 -1
- package/dist/skills/cli-anything.js.map +1 -1
- package/dist/skills/limits.d.ts +86 -0
- package/dist/skills/limits.d.ts.map +1 -0
- package/dist/skills/limits.js +140 -0
- package/dist/skills/limits.js.map +1 -0
- package/dist/skills/loader.d.ts +9 -0
- package/dist/skills/loader.d.ts.map +1 -1
- package/dist/skills/loader.js +29 -1
- package/dist/skills/loader.js.map +1 -1
- package/dist/storage/session-artifacts.d.ts +105 -0
- package/dist/storage/session-artifacts.d.ts.map +1 -0
- package/dist/storage/session-artifacts.js +198 -0
- package/dist/storage/session-artifacts.js.map +1 -0
- package/dist/tools/workspace-pack.d.ts +97 -0
- package/dist/tools/workspace-pack.d.ts.map +1 -0
- package/dist/tools/workspace-pack.js +228 -0
- package/dist/tools/workspace-pack.js.map +1 -0
- package/dist/tui/composer-history.d.ts +99 -0
- package/dist/tui/composer-history.d.ts.map +1 -0
- package/dist/tui/composer-history.js +169 -0
- package/dist/tui/composer-history.js.map +1 -0
- package/dist/ui/components/PromptInput.d.ts.map +1 -1
- package/dist/ui/components/PromptInput.js +80 -9
- package/dist/ui/components/PromptInput.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"output-truncation.d.ts","sourceRoot":"","sources":["../../src/middleware/output-truncation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAkC,MAAM,YAAY,CAAC;AAC7E,OAAO,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SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GACd;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;IAoBvE,kDAAkD;IAClD,qBAAqB,IAAI,MAAM;IAI/B,qEAAqE;IACrE,gBAAgB,IAAI,gBAAgB;IAIpC;;;;;;;;OAQG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG;QACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;QAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;KAC/B;IAwDD,+EAA+E;IAC/E,iBAAiB,IAAI,oBAAoB;IAIzC,oEAAoE;IACpE,iBAAiB,IAAI,MAAM;IAI3B;;OAEG;IACH,QAAQ,IAAI,eAAe;IAQ3B;;OAEG;IACH,KAAK,IAAI,IAAI;CAKd;AAmBD,wBAAgB,gCAAgC,CAAC,QAAQ,EAAE,0BAA0B,GAAG,UAAU,CA6CjG"}
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* Runs in the `after` phase at order 6.5 (after ToolError, before Summarization).
|
|
11
11
|
*/
|
|
12
12
|
import { formatIsolatedPreview, OutputIsolationStore } from "../sandbox/output-isolator.js";
|
|
13
|
+
import { SessionArtifacts } from "../storage/session-artifacts.js";
|
|
13
14
|
const DEFAULT_CONFIG = {
|
|
14
15
|
maxToolOutputChars: 8000,
|
|
15
16
|
maxToolOutputLines: 200,
|
|
@@ -39,9 +40,49 @@ export class OutputTruncationMiddleware {
|
|
|
39
40
|
// via getIsolationStore() to fetch full content by handle.
|
|
40
41
|
isolationStore = new OutputIsolationStore();
|
|
41
42
|
totalIsolations = 0;
|
|
43
|
+
// V9 ZW-4: persistent artifact spill. Stores oversize tool outputs at
|
|
44
|
+
// ~/.wotann/artifacts/{sessionId}/ so the transcript carries a small
|
|
45
|
+
// reference instead of the full payload. Survives process restart,
|
|
46
|
+
// unlike the in-process isolation store.
|
|
47
|
+
artifactStore = new SessionArtifacts();
|
|
48
|
+
totalArtifactSpills = 0;
|
|
42
49
|
constructor(config) {
|
|
43
50
|
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
44
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* V9 ZW-4: spill a tool output to durable storage and return an inline
|
|
54
|
+
* reference. Used by the middleware adapter when a `sessionId` is
|
|
55
|
+
* available on the result. Best-effort — if spill fails we return null
|
|
56
|
+
* and the caller falls through to plain truncation.
|
|
57
|
+
*/
|
|
58
|
+
spillToolOutput(sessionId, toolName, content) {
|
|
59
|
+
try {
|
|
60
|
+
// Use the tool name as the artifact name. SessionArtifacts.spill
|
|
61
|
+
// sanitises it internally so unsafe chars become `_`.
|
|
62
|
+
const record = this.artifactStore.spill(sessionId, toolName, content);
|
|
63
|
+
this.totalArtifactSpills++;
|
|
64
|
+
this.totalTruncations++;
|
|
65
|
+
const linesDropped = content.split("\n").length;
|
|
66
|
+
this.totalLinesDropped += linesDropped;
|
|
67
|
+
this.totalCharsDropped += content.length;
|
|
68
|
+
// Inline reference replaces the full payload. The model can ask for
|
|
69
|
+
// the full content by tool-name later.
|
|
70
|
+
const ref = `[Tool output spilled to artifact: ${record.path} (${record.sizeBytes} bytes, sha=${record.sha.slice(0, 8)})]`;
|
|
71
|
+
return { content: ref, truncated: true, linesDropped };
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// Honest fallback — never block the agent on a spill I/O hiccup.
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** V9 ZW-4: artifact spill count this session. */
|
|
79
|
+
getArtifactSpillCount() {
|
|
80
|
+
return this.totalArtifactSpills;
|
|
81
|
+
}
|
|
82
|
+
/** V9 ZW-4: expose the artifact store for handle-based retrieval. */
|
|
83
|
+
getArtifactStore() {
|
|
84
|
+
return this.artifactStore;
|
|
85
|
+
}
|
|
45
86
|
/**
|
|
46
87
|
* Truncate content if it exceeds configured limits.
|
|
47
88
|
* Returns the original content if within limits, or a truncated version
|
|
@@ -130,13 +171,44 @@ export class OutputTruncationMiddleware {
|
|
|
130
171
|
* Runs at order 6.5 (between ToolError at 6 and Summarization at 7).
|
|
131
172
|
* Operates in the `after` phase to inspect and truncate tool results.
|
|
132
173
|
*/
|
|
174
|
+
/**
|
|
175
|
+
* V9 ZW-4 artifact-spill threshold. Tool outputs above this size go to
|
|
176
|
+
* durable storage instead of being summarised by the in-context isolator.
|
|
177
|
+
* The threshold is intentionally higher than ISOLATION_SIZE_BYTES so the
|
|
178
|
+
* isolator handles the common case and the artifact spill catches the
|
|
179
|
+
* truly huge outputs (full repo dumps, multi-MB log slices) that benefit
|
|
180
|
+
* from cross-process persistence.
|
|
181
|
+
*/
|
|
182
|
+
const ARTIFACT_SPILL_THRESHOLD_BYTES = 200 * 1024; // 200 KB
|
|
133
183
|
export function createOutputTruncationMiddleware(instance) {
|
|
134
184
|
return {
|
|
135
185
|
name: "OutputTruncation",
|
|
136
186
|
order: 6.5,
|
|
137
|
-
after(
|
|
187
|
+
after(ctx, result) {
|
|
138
188
|
if (!result.content)
|
|
139
189
|
return result;
|
|
190
|
+
// V9 ZW-4: for very large outputs spill the content to
|
|
191
|
+
// ~/.wotann/artifacts/{sessionId}/ and replace inline with a
|
|
192
|
+
// reference. Best-effort: failures fall through to plain truncation.
|
|
193
|
+
// SessionArtifacts validates sessionId at the boundary; if the
|
|
194
|
+
// pipeline ever passes a malformed id we silently fall through
|
|
195
|
+
// rather than crash a tool result.
|
|
196
|
+
if (typeof ctx.sessionId === "string" &&
|
|
197
|
+
ctx.sessionId.length > 0 &&
|
|
198
|
+
/^[A-Za-z0-9_-]+$/.test(ctx.sessionId) &&
|
|
199
|
+
result.content.length >= ARTIFACT_SPILL_THRESHOLD_BYTES) {
|
|
200
|
+
const toolName = result.toolName ?? "unknown";
|
|
201
|
+
const spill = instance.spillToolOutput(ctx.sessionId, toolName, result.content);
|
|
202
|
+
if (spill !== null) {
|
|
203
|
+
const traceNote = `[OutputTruncation] Spilled ${spill.linesDropped} lines from ${toolName} to artifact storage`;
|
|
204
|
+
return {
|
|
205
|
+
...result,
|
|
206
|
+
content: spill.content,
|
|
207
|
+
followUp: result.followUp ? `${result.followUp}\n\n${traceNote}` : traceNote,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
// Spill failed — fall through to existing isolation/truncate path.
|
|
211
|
+
}
|
|
140
212
|
const { content, truncated, linesDropped } = instance.truncate(result.content);
|
|
141
213
|
if (!truncated)
|
|
142
214
|
return result;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"output-truncation.js","sourceRoot":"","sources":["../../src/middleware/output-truncation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"output-truncation.js","sourceRoot":"","sources":["../../src/middleware/output-truncation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAC5F,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAiBnE,MAAM,cAAc,GAAqB;IACvC,kBAAkB,EAAE,IAAI;IACxB,kBAAkB,EAAE,GAAG;IACvB,iBAAiB,EAAE,mCAAmC;IACtD,YAAY,EAAE,EAAE;IAChB,YAAY,EAAE,EAAE;CACjB,CAAC;AAUF,6EAA6E;AAE7E;;;GAGG;AACH;;;;;GAKG;AACH,MAAM,oBAAoB,GAAG,EAAE,GAAG,IAAI,CAAC;AAEvC,MAAM,OAAO,0BAA0B;IACpB,MAAM,CAAmB;IAClC,gBAAgB,GAAG,CAAC,CAAC;IACrB,iBAAiB,GAAG,CAAC,CAAC;IACtB,iBAAiB,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D,CAAC;gBACD,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,SAAS,CAAC;gBAC9C,MAAM,KAAK,GAAG,QAAQ,CAAC,eAAe,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;gBAChF,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;oBACnB,MAAM,SAAS,GAAG,8BAA8B,KAAK,CAAC,YAAY,eAAe,QAAQ,sBAAsB,CAAC;oBAChH,OAAO;wBACL,GAAG,MAAM;wBACT,OAAO,EAAE,KAAK,CAAC,OAAO;wBACtB,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,QAAQ,OAAO,SAAS,EAAE,CAAC,CAAC,CAAC,SAAS;qBAC7E,CAAC;gBACJ,CAAC;gBACD,mEAAmE;YACrE,CAAC;YAED,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAE/E,IAAI,CAAC,SAAS;gBAAE,OAAO,MAAM,CAAC;YAE9B,MAAM,SAAS,GAAG,gCAAgC,YAAY,eAAe,MAAM,CAAC,QAAQ,IAAI,SAAS,SAAS,CAAC;YAEnH,OAAO;gBACL,GAAG,MAAM;gBACT,OAAO;gBACP,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,QAAQ,OAAO,SAAS,EAAE,CAAC,CAAC,CAAC,SAAS;aAC7E,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -3,7 +3,30 @@
|
|
|
3
3
|
* Intercepts model output mid-stream via regex triggers.
|
|
4
4
|
* Rules fire DURING generation, not before or after.
|
|
5
5
|
* Zero upfront context cost — rules only consume tokens when triggered.
|
|
6
|
+
*
|
|
7
|
+
* ── Surface split (do not collapse these into one class) ──────────────
|
|
8
|
+
*
|
|
9
|
+
* TTSREngine ← legacy: severity-tiered regex-only scan with
|
|
10
|
+
* per-rule fire caps. Used by tests/unit/ttsr.test.ts
|
|
11
|
+
* and existing callers that don't know about stream
|
|
12
|
+
* scopes. Kept stable so downstream wires keep
|
|
13
|
+
* working unchanged.
|
|
14
|
+
*
|
|
15
|
+
* TTSRRunner ← new: scope-aware, per-token, one-shot-by-default
|
|
16
|
+
* runner ported from oh-my-pi. Lives in
|
|
17
|
+
* src/runtime-hooks/ttsr-runner.ts. The orchestrator
|
|
18
|
+
* (not the engine) decides abort/retry.
|
|
19
|
+
*
|
|
20
|
+
* The engine OPTIONALLY hosts a runner — when constructed with one,
|
|
21
|
+
* `processChunk` consults the runner and surfaces structured triggers
|
|
22
|
+
* via the same `injections` channel so legacy callers stay
|
|
23
|
+
* compatible. Orchestrators that want full scope-aware semantics use
|
|
24
|
+
* the new `processScopedChunk` path instead, which preserves the
|
|
25
|
+
* `TTSRTrigger` shape end to end.
|
|
6
26
|
*/
|
|
27
|
+
import type { TTSRRule as ScopedTTSRRule } from "../runtime-hooks/ttsr-rule.js";
|
|
28
|
+
import { TTSRRunner, type TTSRTrigger } from "../runtime-hooks/ttsr-runner.js";
|
|
29
|
+
import type { StreamContext } from "../runtime-hooks/ttsr-scope.js";
|
|
7
30
|
export type TTSRSeverity = "critical" | "warning" | "info";
|
|
8
31
|
export interface TTSRRule {
|
|
9
32
|
readonly trigger: RegExp;
|
|
@@ -17,18 +40,70 @@ export interface TTSRResult {
|
|
|
17
40
|
readonly injections: readonly string[];
|
|
18
41
|
readonly shouldAbort: boolean;
|
|
19
42
|
readonly retrySystemMessage?: string;
|
|
43
|
+
/** Structured triggers from the scope-aware runner (if attached). */
|
|
44
|
+
readonly triggers?: readonly TTSRTrigger[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Result of a scope-aware chunk evaluation. Carries the same fields
|
|
48
|
+
* as `TTSRResult` plus the trigger list. `shouldAbort` is true when
|
|
49
|
+
* any scoped trigger fired — the orchestrator decides whether to
|
|
50
|
+
* actually abort.
|
|
51
|
+
*/
|
|
52
|
+
export interface TTSRScopedResult extends TTSRResult {
|
|
53
|
+
readonly triggers: readonly TTSRTrigger[];
|
|
20
54
|
}
|
|
21
55
|
export declare class TTSREngine {
|
|
22
56
|
private readonly rules;
|
|
23
|
-
|
|
57
|
+
private readonly runner?;
|
|
58
|
+
constructor(customRules?: readonly TTSRRule[], runner?: TTSRRunner);
|
|
59
|
+
/**
|
|
60
|
+
* Build an engine that owns a scope-aware `TTSRRunner` populated
|
|
61
|
+
* with the supplied scoped rules.
|
|
62
|
+
*
|
|
63
|
+
* Pass `legacyRules: false` to disable the legacy severity-tiered
|
|
64
|
+
* regex rules (only the runner fires).
|
|
65
|
+
*/
|
|
66
|
+
static withScopedRules(scopedRules: readonly ScopedTTSRRule[], options?: {
|
|
67
|
+
legacyRules?: boolean;
|
|
68
|
+
}): TTSREngine;
|
|
69
|
+
/** Read-only access to the underlying scoped runner, if attached. */
|
|
70
|
+
getRunner(): TTSRRunner | undefined;
|
|
24
71
|
static defaultRules(): TTSRRule[];
|
|
25
72
|
/**
|
|
26
|
-
* Process a chunk of streaming model output.
|
|
73
|
+
* Process a chunk of streaming model output (legacy path).
|
|
74
|
+
*
|
|
27
75
|
* Returns the chunk (unmodified) plus any triggered injections.
|
|
76
|
+
*
|
|
77
|
+
* If a scoped runner is attached, the chunk is ALSO fed to it
|
|
78
|
+
* with a permissive `{ kind: "text" }` context so legacy callers
|
|
79
|
+
* receive any runner-emitted triggers via the same `injections`
|
|
80
|
+
* channel. Scope-aware callers should use `processScopedChunk`
|
|
81
|
+
* instead so they can supply real stream context (thinking
|
|
82
|
+
* channel, tool-call args, file paths).
|
|
28
83
|
*/
|
|
29
84
|
processChunk(chunk: string): TTSRResult;
|
|
30
85
|
/**
|
|
31
|
-
*
|
|
86
|
+
* Scope-aware streaming evaluation. Use this when you have real
|
|
87
|
+
* stream context (text vs thinking vs a specific tool call) and
|
|
88
|
+
* you want only scoped rules to fire.
|
|
89
|
+
*
|
|
90
|
+
* Returns a `TTSRScopedResult` whose `triggers` field is the
|
|
91
|
+
* primary signal — `injections` and `shouldAbort` are present
|
|
92
|
+
* for compatibility with legacy callers.
|
|
93
|
+
*
|
|
94
|
+
* NOTE: This method does NOT run the legacy severity rules; if
|
|
95
|
+
* you want both, call `processChunk` after this and merge.
|
|
96
|
+
*/
|
|
97
|
+
processScopedChunk(chunk: string, ctx: StreamContext): TTSRScopedResult;
|
|
98
|
+
/**
|
|
99
|
+
* Acknowledge that the orchestrator processed the trigger for
|
|
100
|
+
* `ruleId`. Forwards to the runner so one-shot rules stop
|
|
101
|
+
* firing.
|
|
102
|
+
*/
|
|
103
|
+
acknowledgeFired(ruleId: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Reset fire counts for a new session. Also clears scoped
|
|
106
|
+
* runner state when attached (buffers AND fired records).
|
|
32
107
|
*/
|
|
33
108
|
reset(): void;
|
|
34
109
|
/**
|
|
@@ -37,4 +112,5 @@ export declare class TTSREngine {
|
|
|
37
112
|
addRule(rule: TTSRRule): void;
|
|
38
113
|
getRules(): readonly TTSRRule[];
|
|
39
114
|
}
|
|
115
|
+
export type { TTSRTrigger } from "../runtime-hooks/ttsr-runner.js";
|
|
40
116
|
//# sourceMappingURL=ttsr.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ttsr.d.ts","sourceRoot":"","sources":["../../src/middleware/ttsr.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"ttsr.d.ts","sourceRoot":"","sources":["../../src/middleware/ttsr.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC/E,OAAO,KAAK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;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU;IA2DvC;;;;;;;;;;;OAWG;IACH,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,aAAa,GAAG,gBAAgB;IAkCvE;;;;OAIG;IACH,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAItC;;;OAGG;IACH,KAAK,IAAI,IAAI;IAOb;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI;IAI7B,QAAQ,IAAI,SAAS,QAAQ,EAAE;CAGhC;AAED,YAAY,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC"}
|
package/dist/middleware/ttsr.js
CHANGED
|
@@ -3,13 +3,52 @@
|
|
|
3
3
|
* Intercepts model output mid-stream via regex triggers.
|
|
4
4
|
* Rules fire DURING generation, not before or after.
|
|
5
5
|
* Zero upfront context cost — rules only consume tokens when triggered.
|
|
6
|
+
*
|
|
7
|
+
* ── Surface split (do not collapse these into one class) ──────────────
|
|
8
|
+
*
|
|
9
|
+
* TTSREngine ← legacy: severity-tiered regex-only scan with
|
|
10
|
+
* per-rule fire caps. Used by tests/unit/ttsr.test.ts
|
|
11
|
+
* and existing callers that don't know about stream
|
|
12
|
+
* scopes. Kept stable so downstream wires keep
|
|
13
|
+
* working unchanged.
|
|
14
|
+
*
|
|
15
|
+
* TTSRRunner ← new: scope-aware, per-token, one-shot-by-default
|
|
16
|
+
* runner ported from oh-my-pi. Lives in
|
|
17
|
+
* src/runtime-hooks/ttsr-runner.ts. The orchestrator
|
|
18
|
+
* (not the engine) decides abort/retry.
|
|
19
|
+
*
|
|
20
|
+
* The engine OPTIONALLY hosts a runner — when constructed with one,
|
|
21
|
+
* `processChunk` consults the runner and surfaces structured triggers
|
|
22
|
+
* via the same `injections` channel so legacy callers stay
|
|
23
|
+
* compatible. Orchestrators that want full scope-aware semantics use
|
|
24
|
+
* the new `processScopedChunk` path instead, which preserves the
|
|
25
|
+
* `TTSRTrigger` shape end to end.
|
|
6
26
|
*/
|
|
27
|
+
import { TTSRRunner } from "../runtime-hooks/ttsr-runner.js";
|
|
7
28
|
export class TTSREngine {
|
|
8
29
|
rules;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
30
|
+
runner;
|
|
31
|
+
constructor(customRules, runner) {
|
|
32
|
+
this.rules = customRules ? [...customRules] : TTSREngine.defaultRules();
|
|
33
|
+
if (runner) {
|
|
34
|
+
this.runner = runner;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Build an engine that owns a scope-aware `TTSRRunner` populated
|
|
39
|
+
* with the supplied scoped rules.
|
|
40
|
+
*
|
|
41
|
+
* Pass `legacyRules: false` to disable the legacy severity-tiered
|
|
42
|
+
* regex rules (only the runner fires).
|
|
43
|
+
*/
|
|
44
|
+
static withScopedRules(scopedRules, options) {
|
|
45
|
+
const runner = new TTSRRunner({ rules: scopedRules });
|
|
46
|
+
const legacyRules = options?.legacyRules ?? true;
|
|
47
|
+
return new TTSREngine(legacyRules ? undefined : [], runner);
|
|
48
|
+
}
|
|
49
|
+
/** Read-only access to the underlying scoped runner, if attached. */
|
|
50
|
+
getRunner() {
|
|
51
|
+
return this.runner;
|
|
13
52
|
}
|
|
14
53
|
static defaultRules() {
|
|
15
54
|
return [
|
|
@@ -53,8 +92,16 @@ export class TTSREngine {
|
|
|
53
92
|
];
|
|
54
93
|
}
|
|
55
94
|
/**
|
|
56
|
-
* Process a chunk of streaming model output.
|
|
95
|
+
* Process a chunk of streaming model output (legacy path).
|
|
96
|
+
*
|
|
57
97
|
* Returns the chunk (unmodified) plus any triggered injections.
|
|
98
|
+
*
|
|
99
|
+
* If a scoped runner is attached, the chunk is ALSO fed to it
|
|
100
|
+
* with a permissive `{ kind: "text" }` context so legacy callers
|
|
101
|
+
* receive any runner-emitted triggers via the same `injections`
|
|
102
|
+
* channel. Scope-aware callers should use `processScopedChunk`
|
|
103
|
+
* instead so they can supply real stream context (thinking
|
|
104
|
+
* channel, tool-call args, file paths).
|
|
58
105
|
*/
|
|
59
106
|
processChunk(chunk) {
|
|
60
107
|
const injections = [];
|
|
@@ -70,29 +117,102 @@ export class TTSREngine {
|
|
|
70
117
|
}
|
|
71
118
|
}
|
|
72
119
|
}
|
|
120
|
+
const triggers = [];
|
|
121
|
+
if (this.runner) {
|
|
122
|
+
const scoped = this.runner.observeAll(chunk, { kind: "text" });
|
|
123
|
+
for (const t of scoped) {
|
|
124
|
+
injections.push(t.rule.body);
|
|
125
|
+
triggers.push(t);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
73
128
|
const hasCritical = criticalInjections.length > 0;
|
|
74
|
-
//
|
|
129
|
+
// Scoped triggers also count as abort-worthy: they represent a
|
|
130
|
+
// hard rule (file-typed, scope-gated) that the orchestrator
|
|
131
|
+
// explicitly opted into via withScopedRules.
|
|
132
|
+
const shouldAbort = hasCritical || triggers.length > 0;
|
|
133
|
+
const retryLines = [];
|
|
134
|
+
if (hasCritical) {
|
|
135
|
+
retryLines.push("TTSR RETRY SYSTEM MESSAGE", "The previous streaming attempt was aborted because it emitted one of the following critical patterns:", ...criticalInjections);
|
|
136
|
+
}
|
|
137
|
+
if (triggers.length > 0) {
|
|
138
|
+
if (retryLines.length === 0) {
|
|
139
|
+
retryLines.push("TTSR RETRY SYSTEM MESSAGE");
|
|
140
|
+
}
|
|
141
|
+
retryLines.push("The previous streaming attempt was aborted by scoped rules:", ...triggers.map((t) => `- ${t.rule.id}: ${t.rule.body}`));
|
|
142
|
+
}
|
|
143
|
+
if (retryLines.length > 0) {
|
|
144
|
+
retryLines.push("Restart the response and do not repeat the flagged patterns.");
|
|
145
|
+
}
|
|
75
146
|
return {
|
|
76
147
|
modified: chunk,
|
|
77
148
|
injections,
|
|
78
|
-
shouldAbort
|
|
79
|
-
retrySystemMessage:
|
|
80
|
-
|
|
81
|
-
"TTSR RETRY SYSTEM MESSAGE",
|
|
82
|
-
"The previous streaming attempt was aborted because it emitted one of the following critical patterns:",
|
|
83
|
-
...criticalInjections,
|
|
84
|
-
"Restart the response from scratch and do not repeat the flagged patterns.",
|
|
85
|
-
].join("\n")
|
|
86
|
-
: undefined,
|
|
149
|
+
shouldAbort,
|
|
150
|
+
retrySystemMessage: retryLines.length > 0 ? retryLines.join("\n") : undefined,
|
|
151
|
+
...(triggers.length > 0 ? { triggers } : {}),
|
|
87
152
|
};
|
|
88
153
|
}
|
|
89
154
|
/**
|
|
90
|
-
*
|
|
155
|
+
* Scope-aware streaming evaluation. Use this when you have real
|
|
156
|
+
* stream context (text vs thinking vs a specific tool call) and
|
|
157
|
+
* you want only scoped rules to fire.
|
|
158
|
+
*
|
|
159
|
+
* Returns a `TTSRScopedResult` whose `triggers` field is the
|
|
160
|
+
* primary signal — `injections` and `shouldAbort` are present
|
|
161
|
+
* for compatibility with legacy callers.
|
|
162
|
+
*
|
|
163
|
+
* NOTE: This method does NOT run the legacy severity rules; if
|
|
164
|
+
* you want both, call `processChunk` after this and merge.
|
|
165
|
+
*/
|
|
166
|
+
processScopedChunk(chunk, ctx) {
|
|
167
|
+
if (!this.runner) {
|
|
168
|
+
return {
|
|
169
|
+
modified: chunk,
|
|
170
|
+
injections: [],
|
|
171
|
+
shouldAbort: false,
|
|
172
|
+
triggers: [],
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const triggers = this.runner.observeAll(chunk, ctx);
|
|
176
|
+
const injections = triggers.map((t) => t.rule.body);
|
|
177
|
+
if (triggers.length === 0) {
|
|
178
|
+
return {
|
|
179
|
+
modified: chunk,
|
|
180
|
+
injections,
|
|
181
|
+
shouldAbort: false,
|
|
182
|
+
triggers,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const retry = [
|
|
186
|
+
"TTSR RETRY SYSTEM MESSAGE",
|
|
187
|
+
"The previous streaming attempt was aborted by scoped rules:",
|
|
188
|
+
...triggers.map((t) => `- ${t.rule.id}: ${t.rule.body}`),
|
|
189
|
+
"Restart the response and do not repeat the flagged patterns.",
|
|
190
|
+
].join("\n");
|
|
191
|
+
return {
|
|
192
|
+
modified: chunk,
|
|
193
|
+
injections,
|
|
194
|
+
shouldAbort: true,
|
|
195
|
+
retrySystemMessage: retry,
|
|
196
|
+
triggers,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Acknowledge that the orchestrator processed the trigger for
|
|
201
|
+
* `ruleId`. Forwards to the runner so one-shot rules stop
|
|
202
|
+
* firing.
|
|
203
|
+
*/
|
|
204
|
+
acknowledgeFired(ruleId) {
|
|
205
|
+
this.runner?.acknowledgeFired(ruleId);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Reset fire counts for a new session. Also clears scoped
|
|
209
|
+
* runner state when attached (buffers AND fired records).
|
|
91
210
|
*/
|
|
92
211
|
reset() {
|
|
93
212
|
for (const rule of this.rules) {
|
|
94
213
|
rule.firedCount = 0;
|
|
95
214
|
}
|
|
215
|
+
this.runner?.reset();
|
|
96
216
|
}
|
|
97
217
|
/**
|
|
98
218
|
* Add a custom rule at runtime.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ttsr.js","sourceRoot":"","sources":["../../src/middleware/ttsr.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"ttsr.js","sourceRoot":"","sources":["../../src/middleware/ttsr.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAGH,OAAO,EAAE,UAAU,EAAoB,MAAM,iCAAiC,CAAC;AAgC/E,MAAM,OAAO,UAAU;IACJ,KAAK,CAAa;IAClB,MAAM,CAAc;IAErC,YAAY,WAAiC,EAAE,MAAmB;QAChE,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,YAAY,EAAE,CAAC;QACxE,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,C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