@ap3x/observe 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AP3X
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # @ap3x/observe
2
+
3
+ Observability for agent and swarm runs: a hook engine plus typed event seams over `@ap3x/agent-core` and `@ap3x/swarms`.
4
+
5
+ Part of [AP3X](https://github.com/AP3X-Dev/AP3X) — a TypeScript multi-agent framework.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @ap3x/observe
11
+ ```
12
+
13
+ ## What's inside
14
+
15
+ - **Hook engine** — configurable lifecycle hooks with validation, fail-open/fail-closed modes, and dry-run support.
16
+ - **Swarm bridge** — bridges hooks onto swarm control seams (delegation gates, member run brackets).
17
+ - **Events** — typed run, member, and tool events suitable for tracing or metrics.
18
+
19
+ For OpenTelemetry export, see [`@ap3x/observe-otel`](https://www.npmjs.com/package/@ap3x/observe-otel).
20
+
21
+ ## Documentation
22
+
23
+ See the [AP3X repository](https://github.com/AP3X-Dev/AP3X) for architecture notes, the full package map, and examples.
24
+
25
+ ## License
26
+
27
+ MIT
@@ -0,0 +1,43 @@
1
+ import type { Usage } from "@ap3x/ai";
2
+ export type TraceSource = "agent" | "swarm" | "tool" | "hook";
3
+ export interface TraceEvent {
4
+ traceId: string;
5
+ spanId: string;
6
+ parentSpanId?: string;
7
+ /** Monotonic per trace, assigned by the pipeline, starts at 1. */
8
+ seq: number;
9
+ /** Epoch milliseconds. */
10
+ ts: number;
11
+ source: TraceSource;
12
+ name: string;
13
+ agent?: {
14
+ name?: string;
15
+ model?: string;
16
+ };
17
+ usage?: Usage;
18
+ data?: unknown;
19
+ }
20
+ /** Everything the emitter provides; the pipeline stamps ids, seq, and ts. */
21
+ export interface TraceEventInput {
22
+ source: TraceSource;
23
+ name: string;
24
+ agent?: {
25
+ name?: string;
26
+ model?: string;
27
+ };
28
+ usage?: Usage;
29
+ data?: unknown;
30
+ }
31
+ export interface TraceContext {
32
+ traceId: string;
33
+ spanId: string;
34
+ parentSpanId?: string;
35
+ }
36
+ export declare function newTraceContext(): TraceContext;
37
+ /** Child of `parent`, else of the ambient context, else a fresh root. */
38
+ export declare function childContext(parent?: TraceContext): TraceContext;
39
+ export declare function currentTraceContext(): TraceContext | undefined;
40
+ export declare function runInTrace<T>(ctx: TraceContext, fn: () => T): T;
41
+ /** Runs fn one span deeper than the ambient context. */
42
+ export declare function withSpan<T>(fn: () => T): T;
43
+ //# sourceMappingURL=envelope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEtC,MAAM,MAAM,WAAW,G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CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAE1C"}
@@ -0,0 +1,9 @@
1
+ import type { TraceSink } from "./pipeline";
2
+ /**
3
+ * A TraceSink with a flush barrier. Backends (e.g. OpenTelemetry) implement this
4
+ * in their own packages so @ap3x/observe stays free of third-party dependencies.
5
+ */
6
+ export interface Exporter extends TraceSink {
7
+ flush(): Promise<void>;
8
+ }
9
+ //# sourceMappingURL=exporter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exporter.d.ts","sourceRoot":"","sources":["../src/exporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C;;;GAGG;AACH,MAAM,WAAW,QAAS,SAAQ,SAAS;IACzC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Bridges a HookEngine onto agent-core's existing AgentHooks seams:
3
+ * PreToolUse -> beforeToolCall (block / re-validated arg rewrite)
4
+ * PostToolUse -> afterToolCall (field-by-field result override)
5
+ * RunStart -> first steering consult of a run (inject messages)
6
+ * TurnStart -> every steering consult (inject messages)
7
+ * Stop -> getFollowUpMessages (force continuation)
8
+ * PreCompact -> beforeCompaction (defer)
9
+ */
10
+ import type { AgentHooks, AgentMessage } from "@ap3x/agent-core";
11
+ import type { HookEngine } from "./engine";
12
+ export declare function userMessage(content: string): AgentMessage;
13
+ export type HookAgentHooks = AgentHooks & {
14
+ /**
15
+ * Re-arm RunStart for the next run. Self-arms when a Stop consult ends the
16
+ * run un-extended; call this explicitly when a run ends on error/abort
17
+ * (those stops never consult getFollowUpMessages).
18
+ */
19
+ reset(): void;
20
+ };
21
+ export declare function createHookAgentHooks(engine: HookEngine): HookAgentHooks;
22
+ //# sourceMappingURL=agent-bridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-bridge.d.ts","sourceRoot":"","sources":["../../src/hooks/agent-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAiC,MAAM,kBAAkB,CAAC;AAChG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAEzD;AAED,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG;IACxC;;;;OAIG;IACH,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,UAAU,GAAG,cAAc,CAwFvE"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * hooks.json loader. Trust boundary: the file is user-authored JSON, so it is
3
+ * schema-validated (TypeBox via @ap3x/ai — no new dependency) and malformed
4
+ * config fails LOUDLY at load time (file + detail), never mid-run.
5
+ *
6
+ * File shape: { "hooks": [ { "event", "matcher"?, "command", "timeout"?,
7
+ * "failMode"?, "name"? } ] }
8
+ */
9
+ import { type Static } from "@ap3x/ai";
10
+ import { type Hook, type HookEventName } from "./types";
11
+ declare const THookEntry: import("@sinclair/typebox").TObject<{
12
+ event: import("@sinclair/typebox").TUnion<import("@sinclair/typebox").TLiteral<HookEventName>[]>;
13
+ matcher: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
14
+ command: import("@sinclair/typebox").TString;
15
+ timeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
16
+ failMode: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"open">, import("@sinclair/typebox").TLiteral<"closed">]>>;
17
+ name: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
18
+ }>;
19
+ export type ShellHookEntry = Static<typeof THookEntry>;
20
+ export declare class HookConfigError extends Error {
21
+ readonly file: string;
22
+ constructor(file: string, detail: string);
23
+ }
24
+ export declare function parseHooksFile(file: string, content: string): ShellHookEntry[];
25
+ export interface LoadedHookConfig {
26
+ file: string;
27
+ entries: ShellHookEntry[];
28
+ }
29
+ /** Reads each path; missing files are skipped, malformed ones throw HookConfigError. */
30
+ export declare function loadHooksFiles(paths: string[]): LoadedHookConfig[];
31
+ /** Turn validated config rows into engine-registrable shell hooks. */
32
+ export declare function hooksFromEntries(entries: ShellHookEntry[]): Hook[];
33
+ export {};
34
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/hooks/config.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,KAAK,MAAM,EAA0C,MAAM,UAAU,CAAC;AAG/E,OAAO,EAAoB,KAAK,IAAI,EAAE,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAE1E,QAAA,MAAM,UAAU;;;;;;;EAOd,CAAC;AAKH,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;AAEvD,qBAAa,eAAgB,SAAQ,KAAK;IAEtC,QAAQ,CAAC,IAAI,EAAE,MAAM;gBAAZ,IAAI,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM;CAKjB;AAqBD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,cAAc,EAAE,CAW9E;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,cAAc,EAAE,CAAC;CAC3B;AAED,wFAAwF;AACxF,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,gBAAgB,EAAE,CAkBlE;AASD,sEAAsE;AACtE,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,cAAc,EAAE,GAAG,IAAI,EAAE,CAiBlE"}
@@ -0,0 +1,39 @@
1
+ import type { TracePipeline } from "../pipeline";
2
+ import type { Hook, HookDecisionMap, HookEventName, HookPayloadMap } from "./types";
3
+ export declare const DEFAULT_GATING_TIMEOUT_MS = 10000;
4
+ export declare const DEFAULT_HOOK_TIMEOUT_MS = 30000;
5
+ export declare class HookTimeoutError extends Error {
6
+ readonly timeoutMs: number;
7
+ constructor(timeoutMs: number);
8
+ }
9
+ /** Glob with `*` as the only wildcard, anchored at both ends. */
10
+ export declare function globMatch(pattern: string, value: string): boolean;
11
+ export interface HookOutcome<E extends HookEventName> {
12
+ /** One entry per matching hook that returned a decision, in registration order. */
13
+ decisions: Array<HookDecisionMap[E]>;
14
+ /** True when a failed hook had failMode "closed" — the gated action must be blocked. */
15
+ failedClosed: boolean;
16
+ /** Human-readable reason from the first fail-closed hook. */
17
+ failReason?: string;
18
+ }
19
+ export interface HookEngineOptions {
20
+ /** Hook executions/failures are emitted here as `source: "hook"` trace events. */
21
+ pipeline?: TracePipeline;
22
+ }
23
+ type StoredHook = Hook<HookEventName>;
24
+ /**
25
+ * Registry + executor for hooks. Hooks run sequentially in registration
26
+ * order; a fail-closed hook error short-circuits (the action is blocked
27
+ * regardless of later hooks). `run` never rejects.
28
+ */
29
+ export declare class HookEngine {
30
+ #private;
31
+ constructor(options?: HookEngineOptions);
32
+ register<E extends HookEventName>(hook: Hook<E>): () => void;
33
+ list(): readonly StoredHook[];
34
+ /** Cheap pre-check so bridges can skip work when nothing listens. */
35
+ hasHooks(event: HookEventName): boolean;
36
+ run<E extends HookEventName>(event: E, payload: HookPayloadMap[E], matchValue?: string): Promise<HookOutcome<E>>;
37
+ }
38
+ export {};
39
+ //# sourceMappingURL=engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/hooks/engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAY,IAAI,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAS9F,eAAO,MAAM,yBAAyB,QAAS,CAAC;AAChD,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAE9C,qBAAa,gBAAiB,SAAQ,KAAK;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM;gBAAjB,SAAS,EAAE,MAAM;CAIvC;AAMD,iEAAiE;AACjE,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAGjE;AAgBD,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,aAAa;IAClD,mFAAmF;IACnF,SAAS,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;IACrC,wFAAwF;IACxF,YAAY,EAAE,OAAO,CAAC;IACtB,6DAA6D;IAC7D,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,iBAAiB;IAChC,kFAAkF;IAClF,QAAQ,CAAC,EAAE,aAAa,CAAC;CAC1B;AAED,KAAK,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,CAAC;AAEtC;;;;GAIG;AACH,qBAAa,UAAU;;gBAIT,OAAO,CAAC,EAAE,iBAAiB;IAIvC,QAAQ,CAAC,CAAC,SAAS,aAAa,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAS5D,IAAI,IAAI,SAAS,UAAU,EAAE;IAI7B,qEAAqE;IACrE,QAAQ,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO;IAIjC,GAAG,CAAC,CAAC,SAAS,aAAa,EAC/B,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,EAC1B,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;CAsD3B"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Shell-command hook type. Protocol (spec, verbatim): event payload as JSON on
3
+ * stdin; exit 0 = allow (optional stdout JSON decision); exit 2 = block,
4
+ * stderr is the reason; any other exit = hook error routed to failMode.
5
+ * Non-JSON stdout = no decision (the exit code governs).
6
+ */
7
+ import type { HookDecisionMap, HookEventName } from "./types";
8
+ export declare class HookExecutionError extends Error {
9
+ constructor(message: string);
10
+ }
11
+ export interface ShellHookResult {
12
+ exitCode: number | null;
13
+ stdout: string;
14
+ stderr: string;
15
+ timedOut: boolean;
16
+ }
17
+ /** Never rejects; spawn failures surface as exitCode null + stderr. */
18
+ export declare function runShellHook(command: string, payloadJson: string, timeoutMs: number): Promise<ShellHookResult>;
19
+ /** Map one settled shell execution onto the event's decision shape. */
20
+ export declare function shellDecision<E extends HookEventName>(event: E, result: ShellHookResult): HookDecisionMap[E] | undefined;
21
+ //# sourceMappingURL=shell.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["../../src/hooks/shell.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE9D,qBAAa,kBAAmB,SAAQ,KAAK;gBAC/B,OAAO,EAAE,MAAM;CAI5B;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,uEAAuE;AACvE,wBAAgB,YAAY,CAC1B,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,eAAe,CAAC,CA0C1B;AA8DD,uEAAuE;AACvE,wBAAgB,aAAa,CAAC,CAAC,SAAS,aAAa,EACnD,KAAK,EAAE,CAAC,EACR,MAAM,EAAE,eAAe,GACtB,eAAe,CAAC,CAAC,CAAC,GAAG,SAAS,CAiEhC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Bridges a HookEngine onto the swarms control seams:
3
+ * SubagentStart -> delegation gate (block a delegation before it runs)
4
+ * SubagentStop -> member_run_end events (observe-only, fire-and-forget)
5
+ */
6
+ import type { HookEngine } from "./engine";
7
+ /**
8
+ * The delegation gate is a single process-global slot (last caller wins):
9
+ * attach ONE engine at a time, and detach it before attaching another —
10
+ * a stale detacher clears whichever gate is currently installed.
11
+ */
12
+ export declare function attachSwarmHooks(engine: HookEngine): () => void;
13
+ //# sourceMappingURL=swarm-bridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm-bridge.d.ts","sourceRoot":"","sources":["../../src/hooks/swarm-bridge.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,IAAI,CA4C/D"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Hook contract (observability spec P2). Full control parity: every hook
3
+ * point can block, rewrite, or force continuation — not merely observe.
4
+ * Payload/decision shapes are per-event; the engine stays generic and each
5
+ * bridge does its own event-specific decision merging.
6
+ */
7
+ import type { AgentMessage, AgentToolResult } from "@ap3x/agent-core";
8
+ export type HookEventName = "PreToolUse" | "PostToolUse" | "RunStart" | "TurnStart" | "Stop" | "PreCompact" | "SubagentStart" | "SubagentStop";
9
+ export declare const HOOK_EVENT_NAMES: readonly HookEventName[];
10
+ export interface PreToolUsePayload {
11
+ toolName: string;
12
+ toolCallId: string;
13
+ args: Record<string, unknown>;
14
+ }
15
+ export interface PostToolUsePayload {
16
+ toolName: string;
17
+ toolCallId: string;
18
+ args: Record<string, unknown>;
19
+ result: AgentToolResult;
20
+ isError: boolean;
21
+ }
22
+ /** RunStart fires with turn 0; TurnStart/Stop carry the current turn index. */
23
+ export interface TurnPayload {
24
+ turn: number;
25
+ }
26
+ export interface SubagentStartPayload {
27
+ agentName?: string;
28
+ mechanism: string;
29
+ depth: number;
30
+ task: string;
31
+ }
32
+ export interface SubagentStopPayload {
33
+ agentName: string;
34
+ mechanism: string;
35
+ depth: number;
36
+ durationMs: number;
37
+ isError: boolean;
38
+ }
39
+ export interface HookPayloadMap {
40
+ PreToolUse: PreToolUsePayload;
41
+ PostToolUse: PostToolUsePayload;
42
+ RunStart: TurnPayload;
43
+ TurnStart: TurnPayload;
44
+ Stop: TurnPayload;
45
+ PreCompact: Record<string, never>;
46
+ SubagentStart: SubagentStartPayload;
47
+ SubagentStop: SubagentStopPayload;
48
+ }
49
+ export interface PreToolUseDecision {
50
+ block?: boolean;
51
+ reason?: string;
52
+ /** Replacement args; re-validated against the tool schema by agent-core. */
53
+ updatedArgs?: Record<string, unknown>;
54
+ }
55
+ export interface PostToolUseDecision {
56
+ /** Field-by-field override of the tool result (redaction, replacement). */
57
+ override?: Partial<AgentToolResult>;
58
+ }
59
+ export interface InjectDecision {
60
+ /** Messages injected into the working context before the next turn. */
61
+ messages?: AgentMessage[];
62
+ }
63
+ export interface StopDecision {
64
+ /** Force the run to continue with `messages` as the next user turn. */
65
+ continue?: boolean;
66
+ messages?: AgentMessage[];
67
+ }
68
+ export interface PreCompactDecision {
69
+ /** Skip launching a compaction this turn; re-consulted next turn. */
70
+ defer?: boolean;
71
+ }
72
+ export interface SubagentStartDecision {
73
+ block?: boolean;
74
+ reason?: string;
75
+ }
76
+ export interface HookDecisionMap {
77
+ PreToolUse: PreToolUseDecision;
78
+ PostToolUse: PostToolUseDecision;
79
+ RunStart: InjectDecision;
80
+ TurnStart: InjectDecision;
81
+ Stop: StopDecision;
82
+ PreCompact: PreCompactDecision;
83
+ SubagentStart: SubagentStartDecision;
84
+ /** Observe-only: the member run already ended; nothing to control. */
85
+ SubagentStop: Record<string, never>;
86
+ }
87
+ /** What a hook timeout/crash means: "open" = proceed as if allowed (default), "closed" = block the gated action. */
88
+ export type FailMode = "open" | "closed";
89
+ export interface Hook<E extends HookEventName = HookEventName> {
90
+ event: E;
91
+ /**
92
+ * Glob (`*` wildcard) matched against the tool name (PreToolUse/PostToolUse)
93
+ * or agent name (SubagentStart/SubagentStop). Ignored at events with no
94
+ * match value. Absent = match everything.
95
+ */
96
+ matcher?: string;
97
+ /** Milliseconds. Default: 10s at gating points, 30s at others. */
98
+ timeout?: number;
99
+ /** Default "open". */
100
+ failMode?: FailMode;
101
+ /** Display label for traces and `ap3x hooks list`. */
102
+ name?: string;
103
+ handler: (payload: HookPayloadMap[E]) => HookDecisionMap[E] | undefined | Promise<HookDecisionMap[E] | undefined>;
104
+ }
105
+ /** Identity helper that pins the event's payload/decision types. */
106
+ export declare function defineHook<E extends HookEventName>(hook: Hook<E>): Hook<E>;
107
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/hooks/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEtE,MAAM,MAAM,aAAa,GACrB,YAAY,GACZ,aAAa,GACb,UAAU,GACV,WAAW,GACX,MAAM,GACN,YAAY,GACZ,eAAe,GACf,cAAc,CAAC;AAEnB,eAAO,MAAM,gBAAgB,EAAE,SAAS,aAAa,EASpD,CAAC;AAEF,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC/B;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,MAAM,EAAE,eAAe,CAAC;IACxB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,+EAA+E;AAC/E,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,oBA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AAqB;IACpC,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,kBAAkB,CAAC;IAC/B,WAAW,EAAE,mBAAmB,CAAC;IACjC,QAAQ,EAAE,cAAc,CAAC;IACzB,SAAS,EAAE,cAAc,CAAC;IAC1B,IAAI,EAAE,YAAY,CAAC;IACnB,UAAU,EAAE,kBAAkB,CAAC;IAC/B,aAAa,EAAE,qBAAqB,CAAC;IACrC,sEAAsE;IACtE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;CACrC;AAED,oHAAoH;AACpH,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEzC,MAAM,WAAW,IAAI,CAAC,CAAC,SAAS,aAAa,GAAG,aAAa;IAC3D,KAAK,EAAE,CAAC,CAAC;IACT;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,kEAAkE;IAClE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,sBAAsB;IACtB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,sDAAsD;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,CACP,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,KACvB,eAAe,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;CAC/E;AAED,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,CAAC,SAAS,aAAa,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAE1E"}
@@ -0,0 +1,15 @@
1
+ export { type TraceSource, type TraceEvent, type TraceEventInput, type TraceContext, newTraceContext, childContext, currentTraceContext, runInTrace, withSpan, } from "./envelope";
2
+ export { type TraceSink, type TracePipelineOptions, TracePipeline, } from "./pipeline";
3
+ export { type TraceWriterOptions, TraceWriter, } from "./trace-writer";
4
+ export { parseTraceFile, summarizeTrace, type ParsedTrace, type TraceHeader, type TraceFooter, type TraceSummary, type ToolStats, } from "./trace-file";
5
+ export { type QueuedSinkOptions, QueuedSink, } from "./queued-sink";
6
+ export type { Exporter } from "./exporter";
7
+ export { type AgentEventSource, type ObserveAgentOptions, observeAgent, } from "./taps/agent";
8
+ export { observeSwarms, enableDelegationSpans } from "./taps/swarm";
9
+ export { type HookEventName, HOOK_EVENT_NAMES, type HookPayloadMap, type HookDecisionMap, type PreToolUsePayload, type PostToolUsePayload, type TurnPayload, type SubagentStartPayload, type SubagentStopPayload, type PreToolUseDecision, type PostToolUseDecision, type InjectDecision, type StopDecision, type PreCompactDecision, type SubagentStartDecision, type FailMode, type Hook, defineHook, } from "./hooks/types";
10
+ export { DEFAULT_GATING_TIMEOUT_MS, DEFAULT_HOOK_TIMEOUT_MS, HookTimeoutError, globMatch, type HookOutcome, type HookEngineOptions, HookEngine, } from "./hooks/engine";
11
+ export { type HookAgentHooks, createHookAgentHooks, userMessage, } from "./hooks/agent-bridge";
12
+ export { attachSwarmHooks } from "./hooks/swarm-bridge";
13
+ export { type ShellHookResult, HookExecutionError, runShellHook, shellDecision, } from "./hooks/shell";
14
+ export { type ShellHookEntry, type LoadedHookConfig, HookConfigError, parseHooksFile, loadHooksFiles, hooksFromEntries, } from "./hooks/config";
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,eAAe,EACf,YAAY,EACZ,mBAAmB,EACnB,UAAU,EACV,QAAQ,GACT,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,KAAK,SAAS,EACd,KAAK,oBAAoB,EACzB,aAAa,GACd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