@adhd/agent-engine-orchestrator 2.1.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/package.json +64 -0
- package/src/clients/http-client.d.ts +24 -0
- package/src/clients/http-client.d.ts.map +1 -0
- package/src/clients/http-client.js +96 -0
- package/src/clients/http-client.js.map +1 -0
- package/src/clients/in-process.d.ts +19 -0
- package/src/clients/in-process.d.ts.map +1 -0
- package/src/clients/in-process.js +25 -0
- package/src/clients/in-process.js.map +1 -0
- package/src/clients/index.d.ts +9 -0
- package/src/clients/index.d.ts.map +1 -0
- package/src/clients/index.js +6 -0
- package/src/clients/index.js.map +1 -0
- package/src/clients/registry.d.ts +30 -0
- package/src/clients/registry.d.ts.map +1 -0
- package/src/clients/registry.js +173 -0
- package/src/clients/registry.js.map +1 -0
- package/src/clients/stdio-client.d.ts +19 -0
- package/src/clients/stdio-client.d.ts.map +1 -0
- package/src/clients/stdio-client.js +91 -0
- package/src/clients/stdio-client.js.map +1 -0
- package/src/clients/tool-naming.d.ts +8 -0
- package/src/clients/tool-naming.d.ts.map +1 -0
- package/src/clients/tool-naming.js +26 -0
- package/src/clients/tool-naming.js.map +1 -0
- package/src/clients/types.d.ts +7 -0
- package/src/clients/types.d.ts.map +1 -0
- package/src/clients/types.js +2 -0
- package/src/clients/types.js.map +1 -0
- package/src/engine/context-window.d.ts +74 -0
- package/src/engine/context-window.d.ts.map +1 -0
- package/src/engine/context-window.js +156 -0
- package/src/engine/context-window.js.map +1 -0
- package/src/engine/dag-engine.d.ts +69 -0
- package/src/engine/dag-engine.d.ts.map +1 -0
- package/src/engine/dag-engine.js +103 -0
- package/src/engine/dag-engine.js.map +1 -0
- package/src/engine/hooks.d.ts +10 -0
- package/src/engine/hooks.d.ts.map +1 -0
- package/src/engine/hooks.js +39 -0
- package/src/engine/hooks.js.map +1 -0
- package/src/engine/orchestrator.d.ts +59 -0
- package/src/engine/orchestrator.d.ts.map +1 -0
- package/src/engine/orchestrator.js +665 -0
- package/src/engine/orchestrator.js.map +1 -0
- package/src/engine/policy.d.ts +22 -0
- package/src/engine/policy.d.ts.map +1 -0
- package/src/engine/policy.js +73 -0
- package/src/engine/policy.js.map +1 -0
- package/src/engine/prompt-resolver.d.ts +24 -0
- package/src/engine/prompt-resolver.d.ts.map +1 -0
- package/src/engine/prompt-resolver.js +55 -0
- package/src/engine/prompt-resolver.js.map +1 -0
- package/src/engine/queue.d.ts +12 -0
- package/src/engine/queue.d.ts.map +1 -0
- package/src/engine/queue.js +31 -0
- package/src/engine/queue.js.map +1 -0
- package/src/engine/tool-advertisement.d.ts +5 -0
- package/src/engine/tool-advertisement.d.ts.map +1 -0
- package/src/engine/tool-advertisement.js +70 -0
- package/src/engine/tool-advertisement.js.map +1 -0
- package/src/index.d.ts +50 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +39 -0
- package/src/index.js.map +1 -0
- package/src/interfaces.d.ts +70 -0
- package/src/interfaces.d.ts.map +1 -0
- package/src/interfaces.js +9 -0
- package/src/interfaces.js.map +1 -0
- package/src/plugins/index.d.ts +4 -0
- package/src/plugins/index.d.ts.map +1 -0
- package/src/plugins/index.js +3 -0
- package/src/plugins/index.js.map +1 -0
- package/src/plugins/loader.d.ts +47 -0
- package/src/plugins/loader.d.ts.map +1 -0
- package/src/plugins/loader.js +174 -0
- package/src/plugins/loader.js.map +1 -0
- package/src/plugins/usage-plugin.d.ts +15 -0
- package/src/plugins/usage-plugin.d.ts.map +1 -0
- package/src/plugins/usage-plugin.js +140 -0
- package/src/plugins/usage-plugin.js.map +1 -0
- package/src/providers/anthropic.d.ts +35 -0
- package/src/providers/anthropic.d.ts.map +1 -0
- package/src/providers/anthropic.js +244 -0
- package/src/providers/anthropic.js.map +1 -0
- package/src/providers/claudecli.d.ts +36 -0
- package/src/providers/claudecli.d.ts.map +1 -0
- package/src/providers/claudecli.js +324 -0
- package/src/providers/claudecli.js.map +1 -0
- package/src/providers/factory.d.ts +5 -0
- package/src/providers/factory.d.ts.map +1 -0
- package/src/providers/factory.js +18 -0
- package/src/providers/factory.js.map +1 -0
- package/src/providers/index.d.ts +6 -0
- package/src/providers/index.d.ts.map +1 -0
- package/src/providers/index.js +5 -0
- package/src/providers/index.js.map +1 -0
- package/src/providers/openai.d.ts +30 -0
- package/src/providers/openai.d.ts.map +1 -0
- package/src/providers/openai.js +177 -0
- package/src/providers/openai.js.map +1 -0
- package/src/providers/types.d.ts +22 -0
- package/src/providers/types.d.ts.map +1 -0
- package/src/providers/types.js +2 -0
- package/src/providers/types.js.map +1 -0
- package/src/tools/agent-crud.d.ts +29 -0
- package/src/tools/agent-crud.d.ts.map +1 -0
- package/src/tools/agent-crud.js +28 -0
- package/src/tools/agent-crud.js.map +1 -0
- package/src/tools/session.d.ts +35 -0
- package/src/tools/session.d.ts.map +1 -0
- package/src/tools/session.js +52 -0
- package/src/tools/session.js.map +1 -0
- package/src/tools/task.d.ts +94 -0
- package/src/tools/task.d.ts.map +1 -0
- package/src/tools/task.js +336 -0
- package/src/tools/task.js.map +1 -0
- package/src/tools/usage.d.ts +19 -0
- package/src/tools/usage.d.ts.map +1 -0
- package/src/tools/usage.js +152 -0
- package/src/tools/usage.js.map +1 -0
- package/src/utils/ids.d.ts +2 -0
- package/src/utils/ids.d.ts.map +1 -0
- package/src/utils/ids.js +5 -0
- package/src/utils/ids.js.map +1 -0
- package/src/utils/timestamps.d.ts +2 -0
- package/src/utils/timestamps.d.ts.map +1 -0
- package/src/utils/timestamps.js +4 -0
- package/src/utils/timestamps.js.map +1 -0
- package/src/validation/agent.d.ts +556 -0
- package/src/validation/agent.d.ts.map +1 -0
- package/src/validation/agent.js +175 -0
- package/src/validation/agent.js.map +1 -0
- package/src/validation/agents.d.ts +2 -0
- package/src/validation/agents.d.ts.map +1 -0
- package/src/validation/agents.js +3 -0
- package/src/validation/agents.js.map +1 -0
- package/src/validation/errors.d.ts +32 -0
- package/src/validation/errors.d.ts.map +1 -0
- package/src/validation/errors.js +34 -0
- package/src/validation/errors.js.map +1 -0
- package/src/validation/execution.d.ts +101 -0
- package/src/validation/execution.d.ts.map +1 -0
- package/src/validation/execution.js +14 -0
- package/src/validation/execution.js.map +1 -0
- package/src/validation/index.d.ts +9 -0
- package/src/validation/index.d.ts.map +1 -0
- package/src/validation/index.js +9 -0
- package/src/validation/index.js.map +1 -0
- package/src/validation/mcp.d.ts +54 -0
- package/src/validation/mcp.d.ts.map +1 -0
- package/src/validation/mcp.js +33 -0
- package/src/validation/mcp.js.map +1 -0
- package/src/validation/message.d.ts +43 -0
- package/src/validation/message.d.ts.map +1 -0
- package/src/validation/message.js +28 -0
- package/src/validation/message.js.map +1 -0
- package/src/validation/session.d.ts +49 -0
- package/src/validation/session.d.ts.map +1 -0
- package/src/validation/session.js +32 -0
- package/src/validation/session.js.map +1 -0
- package/src/validation/task.d.ts +156 -0
- package/src/validation/task.d.ts.map +1 -0
- package/src/validation/task.js +84 -0
- package/src/validation/task.js.map +1 -0
- package/src/validation/tasks.d.ts +2 -0
- package/src/validation/tasks.d.ts.map +1 -0
- package/src/validation/tasks.js +3 -0
- package/src/validation/tasks.js.map +1 -0
- package/src/validation/usage.d.ts +75 -0
- package/src/validation/usage.d.ts.map +1 -0
- package/src/validation/usage.js +51 -0
- package/src/validation/usage.js.map +1 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const TOOL_NAME_SEPARATOR = "__";
|
|
2
|
+
export function normalizeToolName(name) {
|
|
3
|
+
return name.replace(/[^A-Za-z0-9_]/g, "_");
|
|
4
|
+
}
|
|
5
|
+
function splitToolName(name) {
|
|
6
|
+
const i = name.indexOf(TOOL_NAME_SEPARATOR);
|
|
7
|
+
return i === -1
|
|
8
|
+
? { server: name, tool: name }
|
|
9
|
+
: { server: name.slice(0, i), tool: name.slice(i + TOOL_NAME_SEPARATOR.length) };
|
|
10
|
+
}
|
|
11
|
+
export function resolveToolCallName(rawName, advertised = []) {
|
|
12
|
+
if (rawName.includes(TOOL_NAME_SEPARATOR))
|
|
13
|
+
return splitToolName(rawName);
|
|
14
|
+
const norm = normalizeToolName(rawName);
|
|
15
|
+
const candidates = Array.from(new Set(advertised)).filter((a) => {
|
|
16
|
+
const { tool } = splitToolName(a);
|
|
17
|
+
return tool === rawName || normalizeToolName(tool) === norm;
|
|
18
|
+
});
|
|
19
|
+
if (candidates.length === 1)
|
|
20
|
+
return splitToolName(candidates[0]);
|
|
21
|
+
if (candidates.length > 1) {
|
|
22
|
+
throw new Error(`Ambiguous tool name '${rawName}': multiple servers expose it — qualify with a server prefix, one of: ${candidates.join(", ")}`);
|
|
23
|
+
}
|
|
24
|
+
return splitToolName(rawName);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=tool-naming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-naming.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/clients/tool-naming.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAExC,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC1C,OAAO,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;AAC/C,CAAC;AAOD,SAAS,aAAa,CAAC,IAAY;IAC/B,MAAM,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC;IAC5C,OAAO,CAAC,KAAK,CAAC,CAAC;QACX,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;QAC9B,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,mBAAmB,CAAC,MAAM,CAAC,EAAE,CAAC;AACzF,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,OAAe,EAAE,aAAgC,EAAE;IACnF,IAAI,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAC;QAAE,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC;IAEzE,MAAM,IAAI,GAAG,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACxC,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE;QAC5D,MAAM,EAAE,IAAI,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;QAClC,OAAO,IAAI,KAAK,OAAO,IAAI,iBAAiB,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC;IAChE,CAAC,CAAC,CAAC;IAEH,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,aAAa,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IACjE,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CACX,wBAAwB,OAAO,yEAAyE,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAClI,CAAC;IACN,CAAC;IACD,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ToolDefinition } from "../providers/types.js";
|
|
2
|
+
export interface IMcpClient {
|
|
3
|
+
listTools(): Promise<ToolDefinition[]>;
|
|
4
|
+
callTool(toolName: string, args: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
5
|
+
close(): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/clients/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,WAAW,UAAU;IACvB,SAAS,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAClF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/clients/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Message } from '../validation/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Context-window management for the tool-calling loop.
|
|
4
|
+
*
|
|
5
|
+
* Chat APIs are stateless: the whole history is re-sent every call, and providers make
|
|
6
|
+
* that affordable with PREFIX caching (an unchanged leading prefix bills at a ~50x
|
|
7
|
+
* discount). The old enforcement — drop the oldest message every call — mutated that
|
|
8
|
+
* prefix and forced the entire context to be re-billed at full price on every trim
|
|
9
|
+
* (BUG-ORCH-008), which on one measured run was ~70% of total spend.
|
|
10
|
+
*
|
|
11
|
+
* The cache-preserving strategy here (mirroring Anthropic's server-side compaction and
|
|
12
|
+
* OpenAI Realtime's retention_ratio) is:
|
|
13
|
+
* 1. keep tool defs + system prompt as a permanently stable head,
|
|
14
|
+
* 2. let history grow append-only (so the prefix is stable and caches between calls),
|
|
15
|
+
* 3. only when the REAL context (provider-reported prompt_tokens) approaches the model's
|
|
16
|
+
* TRUE window, compact a large contiguous middle chunk into one summary message —
|
|
17
|
+
* ONCE, rarely, not a little every turn.
|
|
18
|
+
*/
|
|
19
|
+
/** Fraction of the true context window at which we compact. */
|
|
20
|
+
export declare const DEFAULT_COMPACTION_TRIGGER_FRACTION = 0.75;
|
|
21
|
+
/** Resolve a model's true input context window (tokens). */
|
|
22
|
+
export declare function contextWindowFor(model: string | undefined): number;
|
|
23
|
+
/**
|
|
24
|
+
* Estimate a message's cost at ~4 chars/token, over its full serialized wire form —
|
|
25
|
+
* content + toolCalls arguments + toolResults payload. Counting only `content` scores
|
|
26
|
+
* every tool result as zero, which is the payload that actually blows up context.
|
|
27
|
+
*/
|
|
28
|
+
export declare function estimateMessageTokens(message: Message): number;
|
|
29
|
+
/**
|
|
30
|
+
* Group messages into atomic units that must be kept or dropped together.
|
|
31
|
+
*
|
|
32
|
+
* An `assistant` message bearing `toolCalls` and the `tool` messages answering it form
|
|
33
|
+
* one indivisible unit: the wire format requires every `tool` message to be preceded by
|
|
34
|
+
* the `assistant` message carrying the matching `tool_calls`, so splitting the pair
|
|
35
|
+
* produces a provider 400 (BUG-ORCH-003).
|
|
36
|
+
*/
|
|
37
|
+
export declare function groupIntoAtomicUnits(messages: Message[]): Message[][];
|
|
38
|
+
export interface CompactionDecision {
|
|
39
|
+
/** Should we compact before the next model call? */
|
|
40
|
+
shouldCompact: boolean;
|
|
41
|
+
/** The window used for the decision (tokens). */
|
|
42
|
+
window: number;
|
|
43
|
+
/** The trigger threshold (tokens). */
|
|
44
|
+
threshold: number;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Decide whether to compact, driven by the PROVIDER-REPORTED context size — the exact,
|
|
48
|
+
* free ground truth in every response's `prompt_tokens` — rather than the local estimator
|
|
49
|
+
* (which historically undercounted by ignoring the tools array; BUG-ORCH-006).
|
|
50
|
+
*
|
|
51
|
+
* `reportedContextTokens` is the last call's true input size, or undefined before the
|
|
52
|
+
* first call (in which case we fall back to a local estimate of the pending messages).
|
|
53
|
+
*/
|
|
54
|
+
export declare function decideCompaction(reportedContextTokens: number | undefined, pendingMessages: Message[], model: string | undefined, triggerFraction?: number): CompactionDecision;
|
|
55
|
+
export interface CompactMessagesOptions {
|
|
56
|
+
/** Atomic units to keep verbatim at the tail (most recent turns). Default 4. */
|
|
57
|
+
keepRecentUnits?: number;
|
|
58
|
+
/** Don't compact if fewer than this many middle units would be folded. Default 2. */
|
|
59
|
+
minUnitsToCompact?: number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Summarise a contiguous middle chunk of history into ONE synthetic message, preserving
|
|
63
|
+
* the stable system-prompt head and the most-recent turns verbatim.
|
|
64
|
+
*
|
|
65
|
+
* `summarise` is injected so this is unit-testable without a live model, and so the
|
|
66
|
+
* orchestrator can route the summary call through the same provider. If it throws, we
|
|
67
|
+
* return the messages UNCHANGED (compaction is best-effort — a failed summary must never
|
|
68
|
+
* drop history or crash the task; the provider's own context error is the backstop).
|
|
69
|
+
*
|
|
70
|
+
* Cache note: compaction changes the prefix from the summary point onward, so it MUST be
|
|
71
|
+
* rare. Between compactions the head stays byte-identical and the cache survives.
|
|
72
|
+
*/
|
|
73
|
+
export declare function compactMessages(messages: Message[], summarise: (toSummarise: Message[]) => Promise<string>, opts?: CompactMessagesOptions): Promise<Message[]>;
|
|
74
|
+
//# sourceMappingURL=context-window.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-window.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/engine/context-window.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAItD;;;;;;;;;;;;;;;;GAgBG;AAEH,+DAA+D;AAC/D,eAAO,MAAM,mCAAmC,OAAO,CAAC;AAwBxD,4DAA4D;AAC5D,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAWlE;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAS9D;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,EAAE,CAiBrE;AAED,MAAM,WAAW,kBAAkB;IAC/B,oDAAoD;IACpD,aAAa,EAAE,OAAO,CAAC;IACvB,iDAAiD;IACjD,MAAM,EAAE,MAAM,CAAC;IACf,sCAAsC;IACtC,SAAS,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC5B,qBAAqB,EAAE,MAAM,GAAG,SAAS,EACzC,eAAe,EAAE,OAAO,EAAE,EAC1B,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,eAAe,GAAE,MAA4C,GAC9D,kBAAkB,CAOpB;AAED,MAAM,WAAW,sBAAsB;IACnC,gFAAgF;IAChF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,qFAAqF;IACrF,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,eAAe,CACjC,QAAQ,EAAE,OAAO,EAAE,EACnB,SAAS,EAAE,CAAC,WAAW,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,EACtD,IAAI,GAAE,sBAA2B,GAClC,OAAO,CAAC,OAAO,EAAE,CAAC,CAqCpB"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { generateId } from '../utils/ids.js';
|
|
2
|
+
import { nowIso } from '../utils/timestamps.js';
|
|
3
|
+
/**
|
|
4
|
+
* Context-window management for the tool-calling loop.
|
|
5
|
+
*
|
|
6
|
+
* Chat APIs are stateless: the whole history is re-sent every call, and providers make
|
|
7
|
+
* that affordable with PREFIX caching (an unchanged leading prefix bills at a ~50x
|
|
8
|
+
* discount). The old enforcement — drop the oldest message every call — mutated that
|
|
9
|
+
* prefix and forced the entire context to be re-billed at full price on every trim
|
|
10
|
+
* (BUG-ORCH-008), which on one measured run was ~70% of total spend.
|
|
11
|
+
*
|
|
12
|
+
* The cache-preserving strategy here (mirroring Anthropic's server-side compaction and
|
|
13
|
+
* OpenAI Realtime's retention_ratio) is:
|
|
14
|
+
* 1. keep tool defs + system prompt as a permanently stable head,
|
|
15
|
+
* 2. let history grow append-only (so the prefix is stable and caches between calls),
|
|
16
|
+
* 3. only when the REAL context (provider-reported prompt_tokens) approaches the model's
|
|
17
|
+
* TRUE window, compact a large contiguous middle chunk into one summary message —
|
|
18
|
+
* ONCE, rarely, not a little every turn.
|
|
19
|
+
*/
|
|
20
|
+
/** Fraction of the true context window at which we compact. */
|
|
21
|
+
export const DEFAULT_COMPACTION_TRIGGER_FRACTION = 0.75;
|
|
22
|
+
/**
|
|
23
|
+
* True input context window (tokens) per model family. Used to decide WHEN to compact.
|
|
24
|
+
* Deliberately conservative — an unknown model falls back to a safe small window rather
|
|
25
|
+
* than never compacting. Longest-prefix match wins.
|
|
26
|
+
*/
|
|
27
|
+
const CONTEXT_WINDOWS = [
|
|
28
|
+
['deepseek-v4', 1_000_000],
|
|
29
|
+
['deepseek', 128_000],
|
|
30
|
+
['claude-opus-4', 200_000],
|
|
31
|
+
['claude-sonnet-4', 200_000],
|
|
32
|
+
['claude-haiku-4', 200_000],
|
|
33
|
+
['claude-fable', 200_000],
|
|
34
|
+
['claude-3', 200_000],
|
|
35
|
+
['gpt-4o', 128_000],
|
|
36
|
+
['gpt-4.1', 1_000_000],
|
|
37
|
+
['gpt-5', 400_000],
|
|
38
|
+
['o1', 200_000],
|
|
39
|
+
['o3', 200_000],
|
|
40
|
+
];
|
|
41
|
+
const FALLBACK_CONTEXT_WINDOW = 128_000;
|
|
42
|
+
/** Resolve a model's true input context window (tokens). */
|
|
43
|
+
export function contextWindowFor(model) {
|
|
44
|
+
if (!model)
|
|
45
|
+
return FALLBACK_CONTEXT_WINDOW;
|
|
46
|
+
let best;
|
|
47
|
+
let bestLen = -1;
|
|
48
|
+
for (const [prefix, window] of CONTEXT_WINDOWS) {
|
|
49
|
+
if (model.startsWith(prefix) && prefix.length > bestLen) {
|
|
50
|
+
best = window;
|
|
51
|
+
bestLen = prefix.length;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return best ?? FALLBACK_CONTEXT_WINDOW;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Estimate a message's cost at ~4 chars/token, over its full serialized wire form —
|
|
58
|
+
* content + toolCalls arguments + toolResults payload. Counting only `content` scores
|
|
59
|
+
* every tool result as zero, which is the payload that actually blows up context.
|
|
60
|
+
*/
|
|
61
|
+
export function estimateMessageTokens(message) {
|
|
62
|
+
let chars = message.content?.length ?? 0;
|
|
63
|
+
for (const toolCall of message.toolCalls ?? []) {
|
|
64
|
+
chars += JSON.stringify(toolCall.arguments ?? {}).length;
|
|
65
|
+
}
|
|
66
|
+
for (const toolResult of message.toolResults ?? []) {
|
|
67
|
+
chars += JSON.stringify(toolResult.result ?? null).length;
|
|
68
|
+
}
|
|
69
|
+
return chars / 4;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Group messages into atomic units that must be kept or dropped together.
|
|
73
|
+
*
|
|
74
|
+
* An `assistant` message bearing `toolCalls` and the `tool` messages answering it form
|
|
75
|
+
* one indivisible unit: the wire format requires every `tool` message to be preceded by
|
|
76
|
+
* the `assistant` message carrying the matching `tool_calls`, so splitting the pair
|
|
77
|
+
* produces a provider 400 (BUG-ORCH-003).
|
|
78
|
+
*/
|
|
79
|
+
export function groupIntoAtomicUnits(messages) {
|
|
80
|
+
const units = [];
|
|
81
|
+
for (const message of messages) {
|
|
82
|
+
const current = units[units.length - 1];
|
|
83
|
+
const isToolReplyToOpenUnit = message.role === 'tool' &&
|
|
84
|
+
current !== undefined &&
|
|
85
|
+
current[0].role === 'assistant' &&
|
|
86
|
+
(current[0].toolCalls?.length ?? 0) > 0;
|
|
87
|
+
if (isToolReplyToOpenUnit) {
|
|
88
|
+
current.push(message);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
units.push([message]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return units;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Decide whether to compact, driven by the PROVIDER-REPORTED context size — the exact,
|
|
98
|
+
* free ground truth in every response's `prompt_tokens` — rather than the local estimator
|
|
99
|
+
* (which historically undercounted by ignoring the tools array; BUG-ORCH-006).
|
|
100
|
+
*
|
|
101
|
+
* `reportedContextTokens` is the last call's true input size, or undefined before the
|
|
102
|
+
* first call (in which case we fall back to a local estimate of the pending messages).
|
|
103
|
+
*/
|
|
104
|
+
export function decideCompaction(reportedContextTokens, pendingMessages, model, triggerFraction = DEFAULT_COMPACTION_TRIGGER_FRACTION) {
|
|
105
|
+
const window = contextWindowFor(model);
|
|
106
|
+
const threshold = Math.floor(window * triggerFraction);
|
|
107
|
+
const realContext = reportedContextTokens ??
|
|
108
|
+
Math.ceil(pendingMessages.reduce((n, m) => n + estimateMessageTokens(m), 0));
|
|
109
|
+
return { shouldCompact: realContext > threshold, window, threshold };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Summarise a contiguous middle chunk of history into ONE synthetic message, preserving
|
|
113
|
+
* the stable system-prompt head and the most-recent turns verbatim.
|
|
114
|
+
*
|
|
115
|
+
* `summarise` is injected so this is unit-testable without a live model, and so the
|
|
116
|
+
* orchestrator can route the summary call through the same provider. If it throws, we
|
|
117
|
+
* return the messages UNCHANGED (compaction is best-effort — a failed summary must never
|
|
118
|
+
* drop history or crash the task; the provider's own context error is the backstop).
|
|
119
|
+
*
|
|
120
|
+
* Cache note: compaction changes the prefix from the summary point onward, so it MUST be
|
|
121
|
+
* rare. Between compactions the head stays byte-identical and the cache survives.
|
|
122
|
+
*/
|
|
123
|
+
export async function compactMessages(messages, summarise, opts = {}) {
|
|
124
|
+
const keepRecentUnits = opts.keepRecentUnits ?? 4;
|
|
125
|
+
const minUnitsToCompact = opts.minUnitsToCompact ?? 2;
|
|
126
|
+
// Preserve all leading system messages as the stable head.
|
|
127
|
+
let headEnd = 0;
|
|
128
|
+
while (headEnd < messages.length && messages[headEnd].role === 'system')
|
|
129
|
+
headEnd++;
|
|
130
|
+
const head = messages.slice(0, headEnd);
|
|
131
|
+
const units = groupIntoAtomicUnits(messages.slice(headEnd));
|
|
132
|
+
const middleCount = units.length - keepRecentUnits;
|
|
133
|
+
if (middleCount < minUnitsToCompact) {
|
|
134
|
+
// Not enough to fold — leave history untouched.
|
|
135
|
+
return messages;
|
|
136
|
+
}
|
|
137
|
+
const middle = units.slice(0, middleCount).flat();
|
|
138
|
+
const tail = units.slice(middleCount).flat();
|
|
139
|
+
let summaryText;
|
|
140
|
+
try {
|
|
141
|
+
summaryText = await summarise(middle);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return messages; // best-effort: never lose history on a failed summary
|
|
145
|
+
}
|
|
146
|
+
const summaryMessage = {
|
|
147
|
+
id: generateId(),
|
|
148
|
+
sessionId: messages[0]?.sessionId ?? '',
|
|
149
|
+
role: 'user',
|
|
150
|
+
content: `[Earlier conversation compacted to fit the context window. ` +
|
|
151
|
+
`Summary of ${middle.length} prior message(s):]\n\n${summaryText}`,
|
|
152
|
+
createdAt: nowIso(),
|
|
153
|
+
};
|
|
154
|
+
return [...head, summaryMessage, ...tail];
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=context-window.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-window.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/engine/context-window.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAEhD;;;;;;;;;;;;;;;;GAgBG;AAEH,+DAA+D;AAC/D,MAAM,CAAC,MAAM,mCAAmC,GAAG,IAAI,CAAC;AAExD;;;;GAIG;AACH,MAAM,eAAe,GAAuC;IACxD,CAAC,aAAa,EAAE,SAAS,CAAC;IAC1B,CAAC,UAAU,EAAE,OAAO,CAAC;IACrB,CAAC,eAAe,EAAE,OAAO,CAAC;IAC1B,CAAC,iBAAiB,EAAE,OAAO,CAAC;IAC5B,CAAC,gBAAgB,EAAE,OAAO,CAAC;IAC3B,CAAC,cAAc,EAAE,OAAO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AAC,CAAC,CAAC,EAAE,SAAS,IAAI,EAAE;QACvC,IAAI,EAAE,MAAM;QACZ,OAAO,EACH,6DAA6D;YAC7D,cAAc,MAAM,CAAC,MAAM,0BAA0B,WAAW,EAAE;QACtE,SAAS,EAAE,MAAM,EAAE;KACtB,CAAC;IAEF,OAAO,CAAC,GAAG,IAAI,EAAE,cAAc,EAAE,GAAG,IAAI,CAAC,CAAC;AAC9C,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
|
|
2
|
+
import type { EngineLogger } from "../interfaces.js";
|
|
3
|
+
export interface DagTaskStore {
|
|
4
|
+
create(input: {
|
|
5
|
+
id?: string;
|
|
6
|
+
sessionId: string | null;
|
|
7
|
+
prompt: string;
|
|
8
|
+
parentTaskId?: string;
|
|
9
|
+
recursionDepth?: number;
|
|
10
|
+
dependsOn?: string[];
|
|
11
|
+
onUpstreamFailure?: "fail" | "skip";
|
|
12
|
+
isEphemeral?: boolean;
|
|
13
|
+
}): {
|
|
14
|
+
id: string;
|
|
15
|
+
status: string;
|
|
16
|
+
result?: string;
|
|
17
|
+
error?: string;
|
|
18
|
+
dependsOn?: string[] | null;
|
|
19
|
+
inputs?: Record<string, string> | null;
|
|
20
|
+
onUpstreamFailure?: string | null;
|
|
21
|
+
};
|
|
22
|
+
read(taskId: string): {
|
|
23
|
+
id: string;
|
|
24
|
+
status: string;
|
|
25
|
+
result?: string;
|
|
26
|
+
error?: string;
|
|
27
|
+
dependsOn?: string[] | null;
|
|
28
|
+
inputs?: Record<string, string> | null;
|
|
29
|
+
onUpstreamFailure?: string | null;
|
|
30
|
+
sessionId?: string | null;
|
|
31
|
+
isEphemeral?: boolean;
|
|
32
|
+
prompt: string;
|
|
33
|
+
recursionDepth: number;
|
|
34
|
+
};
|
|
35
|
+
updateStatus(taskId: string, status: string, fields?: Record<string, unknown>): void;
|
|
36
|
+
list(filter?: Record<string, unknown>): Array<{
|
|
37
|
+
id: string;
|
|
38
|
+
status: string;
|
|
39
|
+
result?: string;
|
|
40
|
+
error?: string;
|
|
41
|
+
dependsOn?: string[] | null;
|
|
42
|
+
inputs?: Record<string, string> | null;
|
|
43
|
+
onUpstreamFailure?: string | null;
|
|
44
|
+
}>;
|
|
45
|
+
registerCancellation(taskId: string, controller: AbortController): void;
|
|
46
|
+
unregisterCancellation(taskId: string): void;
|
|
47
|
+
cancel(taskId: string): void;
|
|
48
|
+
appendEvent(evt: {
|
|
49
|
+
taskId: string;
|
|
50
|
+
type: string;
|
|
51
|
+
payload?: unknown;
|
|
52
|
+
}): void;
|
|
53
|
+
}
|
|
54
|
+
export interface DagQueue {
|
|
55
|
+
enqueue(taskId: string, fn: () => Promise<void>): void;
|
|
56
|
+
pending: number;
|
|
57
|
+
size: number;
|
|
58
|
+
}
|
|
59
|
+
export declare class DagEngine {
|
|
60
|
+
private readonly db;
|
|
61
|
+
private readonly queue;
|
|
62
|
+
private readonly taskStore;
|
|
63
|
+
private readonly dispatchFn;
|
|
64
|
+
private readonly logger;
|
|
65
|
+
constructor(db: BetterSQLite3Database<Record<string, never>>, queue: DagQueue, taskStore: DagTaskStore, dispatchFn: (taskId: string) => Promise<void>, logger?: EngineLogger);
|
|
66
|
+
validateNoCycle(newTaskId: string, dependsOn: string[]): void;
|
|
67
|
+
dispatchReady(completedTaskId: string): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=dag-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dag-engine.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/engine/dag-engine.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAKxE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAErD,MAAM,WAAW,YAAY;IACzB,MAAM,CAAC,KAAK,EAAE;QACV,EAAE,CAA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gBAGjB,EAAE,EAAE,qBAAqB,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,EAChD,KAAK,EAAE,QAAQ,EACf,SAAS,EAAE,YAAY,EACvB,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,EAC9D,MAAM,CAAC,EAAE,YAAY;IAKzB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI;IAiCvD,aAAa,CAAC,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAmF9D"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { and, eq, inArray } from "drizzle-orm";
|
|
2
|
+
import { tasksTable } from "@adhd/agent-store-runtime";
|
|
3
|
+
import { ToolError } from "../validation/errors.js";
|
|
4
|
+
import { nowIso } from "../utils/timestamps.js";
|
|
5
|
+
export class DagEngine {
|
|
6
|
+
db;
|
|
7
|
+
queue;
|
|
8
|
+
taskStore;
|
|
9
|
+
dispatchFn;
|
|
10
|
+
logger;
|
|
11
|
+
constructor(db, queue, taskStore, dispatchFn, logger) {
|
|
12
|
+
this.db = db;
|
|
13
|
+
this.queue = queue;
|
|
14
|
+
this.taskStore = taskStore;
|
|
15
|
+
this.dispatchFn = dispatchFn;
|
|
16
|
+
this.logger = logger ?? { info: () => { }, warn: () => { }, error: () => { }, debug: () => { } };
|
|
17
|
+
}
|
|
18
|
+
validateNoCycle(newTaskId, dependsOn) {
|
|
19
|
+
const visited = new Set();
|
|
20
|
+
const queue = [...dependsOn];
|
|
21
|
+
while (queue.length > 0) {
|
|
22
|
+
const id = queue.shift();
|
|
23
|
+
if (!id)
|
|
24
|
+
throw new Error('Empty queue in validateNoCycle');
|
|
25
|
+
if (id === newTaskId) {
|
|
26
|
+
throw new ToolError("VALIDATION_ERROR", `Dependency cycle detected: task ${newTaskId} would depend on itself via its dependency chain`);
|
|
27
|
+
}
|
|
28
|
+
if (!visited.has(id)) {
|
|
29
|
+
visited.add(id);
|
|
30
|
+
const row = this.db
|
|
31
|
+
.select({ dependsOn: tasksTable.depends_on })
|
|
32
|
+
.from(tasksTable)
|
|
33
|
+
.where(eq(tasksTable.id, id))
|
|
34
|
+
.get();
|
|
35
|
+
const upstreamDeps = row?.dependsOn
|
|
36
|
+
? JSON.parse(row.dependsOn)
|
|
37
|
+
: [];
|
|
38
|
+
queue.push(...upstreamDeps);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async dispatchReady(completedTaskId) {
|
|
43
|
+
const waitingTasks = this.db
|
|
44
|
+
.select()
|
|
45
|
+
.from(tasksTable)
|
|
46
|
+
.where(eq(tasksTable.status, "waiting"))
|
|
47
|
+
.all();
|
|
48
|
+
for (const task of waitingTasks) {
|
|
49
|
+
const deps = task.depends_on
|
|
50
|
+
? JSON.parse(task.depends_on)
|
|
51
|
+
: [];
|
|
52
|
+
if (!deps.includes(completedTaskId))
|
|
53
|
+
continue;
|
|
54
|
+
if (deps.length === 0)
|
|
55
|
+
continue;
|
|
56
|
+
const depRows = this.db
|
|
57
|
+
.select({
|
|
58
|
+
id: tasksTable.id,
|
|
59
|
+
status: tasksTable.status,
|
|
60
|
+
result: tasksTable.result,
|
|
61
|
+
})
|
|
62
|
+
.from(tasksTable)
|
|
63
|
+
.where(inArray(tasksTable.id, deps))
|
|
64
|
+
.all();
|
|
65
|
+
const terminalStatuses = new Set(["completed", "failed", "cancelled"]);
|
|
66
|
+
const allTerminal = depRows.every(r => terminalStatuses.has(r.status));
|
|
67
|
+
if (!allTerminal)
|
|
68
|
+
continue;
|
|
69
|
+
const policy = (task.on_upstream_failure ?? "fail");
|
|
70
|
+
const failedDep = depRows.find(r => r.status === "failed" || r.status === "cancelled");
|
|
71
|
+
if (failedDep && policy === "fail") {
|
|
72
|
+
this.logger.info({ taskId: task.id, failedDepId: failedDep.id, status: failedDep.status }, "Propagating upstream failure to waiting task");
|
|
73
|
+
this.taskStore.updateStatus(task.id, "failed", {
|
|
74
|
+
error: `Upstream task ${failedDep.id} ${failedDep.status}`,
|
|
75
|
+
});
|
|
76
|
+
await this.dispatchReady(task.id);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const inputs = {};
|
|
80
|
+
for (const dep of depRows) {
|
|
81
|
+
if (dep.status === "completed" && dep.result != null) {
|
|
82
|
+
inputs[dep.id] = dep.result;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const updated = this.db
|
|
86
|
+
.update(tasksTable)
|
|
87
|
+
.set({
|
|
88
|
+
status: "pending",
|
|
89
|
+
inputs: JSON.stringify(inputs),
|
|
90
|
+
updatedAt: nowIso(),
|
|
91
|
+
})
|
|
92
|
+
.where(and(eq(tasksTable.id, task.id), eq(tasksTable.status, "waiting")))
|
|
93
|
+
.run();
|
|
94
|
+
if (updated.changes === 0) {
|
|
95
|
+
this.logger.debug({ taskId: task.id }, "DagEngine: lost optimistic lock, skipping enqueue");
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
this.logger.info({ taskId: task.id, deps, inputs: Object.keys(inputs) }, "DagEngine: dispatching ready task");
|
|
99
|
+
await this.dispatchFn(task.id);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=dag-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dag-engine.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/engine/dag-engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AA6BhD,MAAM,OAAO,SAAS;IAIG;IACA;I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AAC,UAAU,CAAC;iBAClB,GAAG,CAAC;gBACD,MAAM,EAAE,SAAS;gBACjB,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;gBAC9B,SAAS,EAAE,MAAM,EAAE;aACtB,CAAC;iBACD,KAAK,CACF,GAAG,CACC,EAAE,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,CAAC,EAC1B,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,SAAS,CAAC,CACnC,CACJ;iBACA,GAAG,EAAE,CAAC;YAEX,IAAI,OAAO,CAAC,OAAO,KAAK,CAAC,EAAE,CAAC;gBACxB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,EAAE,mDAAmD,CAAC,CAAC;gBAC5F,SAAS;YACb,CAAC;YAED,IAAI,CAAC,MAAM,CAAC,IAAI,CACZ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,EACtD,mCAAmC,CACtC,CAAC;YAEF,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACnC,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { HookEvent, HookEventMap, HookHandler, IHookRegistry, EnforcementEvent, EnforcementHandler } from '@adhd/agent-base-types';
|
|
2
|
+
export declare class HookRegistry implements IHookRegistry {
|
|
3
|
+
private readonly handlers;
|
|
4
|
+
private readonly enforcementHandlers;
|
|
5
|
+
register<E extends HookEvent>(event: E, handler: HookHandler<E>): void;
|
|
6
|
+
emit<E extends HookEvent>(event: E, payload: HookEventMap[E]): Promise<void>;
|
|
7
|
+
registerEnforcement<E extends EnforcementEvent>(event: E, handler: EnforcementHandler<E>): void;
|
|
8
|
+
enforce<E extends EnforcementEvent>(event: E, payload: HookEventMap[E]): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/engine/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,YAAY,EACZ,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EACnB,MAAM,wBAAwB,CAAC;AAEhC,qBAAa,YAAa,YAAW,aAAa;IAChD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkD;IAC3E,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAGhC;IAEJ,QAAQ,CAAC,CAAC,SAAS,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI;IAMhE,IAAI,CAAC,CAAC,SAAS,SAAS,EAC5B,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,IAAI,CAAC;IAehB,mBAAmB,CAAC,CAAC,SAAS,gBAAgB,EAC5C,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAC7B,IAAI;IAMD,OAAO,CAAC,CAAC,SAAS,gBAAgB,EACtC,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,IAAI,CAAC;CAOjB"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export class HookRegistry {
|
|
2
|
+
handlers = new Map();
|
|
3
|
+
enforcementHandlers = new Map();
|
|
4
|
+
register(event, handler) {
|
|
5
|
+
const list = this.handlers.get(event) ?? [];
|
|
6
|
+
list.push(handler);
|
|
7
|
+
this.handlers.set(event, list);
|
|
8
|
+
}
|
|
9
|
+
async emit(event, payload) {
|
|
10
|
+
const list = this.handlers.get(event);
|
|
11
|
+
if (!list?.length)
|
|
12
|
+
return;
|
|
13
|
+
for (const handler of list) {
|
|
14
|
+
try {
|
|
15
|
+
await handler(payload);
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
console.warn('[HookRegistry] hook handler error (swallowed)', {
|
|
19
|
+
event,
|
|
20
|
+
err,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
registerEnforcement(event, handler) {
|
|
26
|
+
const list = this.enforcementHandlers.get(event) ?? [];
|
|
27
|
+
list.push(handler);
|
|
28
|
+
this.enforcementHandlers.set(event, list);
|
|
29
|
+
}
|
|
30
|
+
async enforce(event, payload) {
|
|
31
|
+
const list = this.enforcementHandlers.get(event);
|
|
32
|
+
if (!list?.length)
|
|
33
|
+
return;
|
|
34
|
+
for (const handler of list) {
|
|
35
|
+
await handler(payload);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/engine/hooks.ts"],"names":[],"mappings":"AASA,MAAM,OAAO,YAAY;IACN,QAAQ,GAAG,IAAI,GAAG,EAAuC,CAAC;IAC1D,mBAAmB,GAAG,IAAI,GAAG,EAG3C,CAAC;IAEJ,QAAQ,CAAsB,KAAQ,EAAE,OAAuB;QAC7D,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QAC5C,IAAI,CAAC,IAAI,CAAC,OAAiC,CAAC,CAAC;QAC7C,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,IAAI,CACR,KAAQ,EACR,OAAwB;QAExB,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,EAAE,MAAM;YAAE,OAAO;QAC1B,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;YAC3B,IAAI,CAAC;gBACH,MAAO,OAA0B,CAAC,OAAO,CAAC,CAAC;YAC7C,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,CAAC,IAAI,CAAC,+CAA+C,EAAE;oBAC5D,KAAK;oBACL,GAAG;iBACJ,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IAED,mBAAmB,CACjB,KAAQ,EACR,OAA8B;QAE9B,MAAM,IAAI,GAAG,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QACvD,IAAI,CAAC,IAAI,CAAC,OAA+C,CAAC,CAAC;QAC3D,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,OAAO,CACX,KAAQ,EACR,OAAwB;QAExB,MAAM,IAAI,GAAG,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,EAAE,MAAM;YAAE,OAAO;QAC1B,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;YAC3B,MAAO,OAAiC,CAAC,OAAO,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { LLMProvider } from '../providers/types.js';
|
|
2
|
+
import type { ExecutionContext, Message } from '../validation/index.js';
|
|
3
|
+
import type { IHookRegistry } from '@adhd/agent-base-types';
|
|
4
|
+
import type { EngineConfig, EngineLogger } from '../interfaces.js';
|
|
5
|
+
import type { McpClientRegistry } from '../clients/registry.js';
|
|
6
|
+
import type { PolicyEngine } from './policy.js';
|
|
7
|
+
/** TaskStore-like interface for orchestrator consumption */
|
|
8
|
+
export interface OrchestratorTaskStore {
|
|
9
|
+
updateStatus(taskId: string, status: string, fields?: Record<string, unknown>): void;
|
|
10
|
+
appendEvent(evt: {
|
|
11
|
+
taskId: string;
|
|
12
|
+
type: string;
|
|
13
|
+
payload?: unknown;
|
|
14
|
+
}): void;
|
|
15
|
+
unregisterCancellation(taskId: string): void;
|
|
16
|
+
}
|
|
17
|
+
/** SessionStore-like interface for orchestrator consumption */
|
|
18
|
+
export interface OrchestratorSessionStore {
|
|
19
|
+
appendMessage(sessionId: string, message: Message): Promise<void>;
|
|
20
|
+
close(sessionId: string): void;
|
|
21
|
+
}
|
|
22
|
+
export declare function resolveHitl(taskId: string, userInput: string): boolean;
|
|
23
|
+
export interface OrchestratorRunInput {
|
|
24
|
+
executionContext: ExecutionContext;
|
|
25
|
+
messages: Message[];
|
|
26
|
+
registry: McpClientRegistry;
|
|
27
|
+
provider: LLMProvider;
|
|
28
|
+
policy: PolicyEngine;
|
|
29
|
+
taskStore: OrchestratorTaskStore;
|
|
30
|
+
sessionStore: OrchestratorSessionStore;
|
|
31
|
+
signal: AbortSignal;
|
|
32
|
+
taskId: string;
|
|
33
|
+
hooks?: IHookRegistry;
|
|
34
|
+
isEphemeral?: boolean;
|
|
35
|
+
/** Injected emitTaskEvent callback (replaces streaming/event-bus import). */
|
|
36
|
+
emitTaskEvent?: (event: {
|
|
37
|
+
type: string;
|
|
38
|
+
taskId: string;
|
|
39
|
+
status?: string;
|
|
40
|
+
result?: string | null;
|
|
41
|
+
error?: string | null;
|
|
42
|
+
toolName?: string;
|
|
43
|
+
toolCallId?: string;
|
|
44
|
+
input?: unknown;
|
|
45
|
+
content?: unknown;
|
|
46
|
+
}) => void;
|
|
47
|
+
/** Injected logger (replaces logger.js import). */
|
|
48
|
+
logger?: EngineLogger;
|
|
49
|
+
/** Injected config (replaces config.js import). */
|
|
50
|
+
config?: EngineConfig;
|
|
51
|
+
}
|
|
52
|
+
export interface OrchestratorRunResult {
|
|
53
|
+
result: string;
|
|
54
|
+
}
|
|
55
|
+
export declare class Orchestrator {
|
|
56
|
+
run(input: OrchestratorRunInput): Promise<OrchestratorRunResult>;
|
|
57
|
+
}
|
|
58
|
+
export declare function windowMessages(messages: Message[], limit: number): Message[];
|
|
59
|
+
//# sourceMappingURL=orchestrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../../../../../../packages/agent/agent-engine-orchestrator/src/engine/orchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACxE,OAAO,KAAK,EACV,aAAa,EAId,MAAM,wBAAwB,CAAC;AAIhC,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAmChD,4DAA4D;AAC5D,MAAM,WAAW,qBAAqB;IAClC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACrF,WAAW,CAAC,GAAG,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI,CAAC;IAC5E,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CAChD;AAED,+DAA+D;AAC/D,MAAM,WAAW,wBAAwB;IACrC,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI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