agent-inspect 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 +21 -0
- package/README.md +339 -0
- package/package.json +82 -0
- package/packages/cli/dist/index.cjs +888 -0
- package/packages/cli/dist/index.cjs.map +1 -0
- package/packages/cli/dist/index.d.cts +7 -0
- package/packages/cli/dist/index.d.ts +7 -0
- package/packages/cli/dist/index.mjs +878 -0
- package/packages/cli/dist/index.mjs.map +1 -0
- package/packages/core/dist/index.cjs +979 -0
- package/packages/core/dist/index.cjs.map +1 -0
- package/packages/core/dist/index.d.cts +324 -0
- package/packages/core/dist/index.d.ts +324 -0
- package/packages/core/dist/index.mjs +919 -0
- package/packages/core/dist/index.mjs.map +1 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discriminator for what kind of work a {@link Step} represents.
|
|
3
|
+
* `"decision"` captures agent branching/choices; other values cover runs, LLM calls, tools, and user-defined steps.
|
|
4
|
+
*/
|
|
5
|
+
type StepType = "run" | "llm" | "tool" | "decision" | "logic" | "state" | "custom";
|
|
6
|
+
/** Lifecycle state of a single {@link Step}. */
|
|
7
|
+
type StepStatus = "running" | "success" | "error";
|
|
8
|
+
/** Lifecycle state of an entire {@link Run}. */
|
|
9
|
+
type RunStatus = "running" | "success" | "error";
|
|
10
|
+
/** Structured error attached to a run or step when status is `"error"`. */
|
|
11
|
+
interface ErrorInfo {
|
|
12
|
+
message: string;
|
|
13
|
+
stack?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Optional token counts for a step (e.g. LLM usage).
|
|
17
|
+
* Reserved for future roadmap; MVP does not compute or persist token usage.
|
|
18
|
+
*/
|
|
19
|
+
interface TokenMetadata {
|
|
20
|
+
input?: number;
|
|
21
|
+
output?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Arbitrary structured fields for a step; safe extensions use string keys. */
|
|
24
|
+
interface StepMetadata {
|
|
25
|
+
model?: string;
|
|
26
|
+
toolName?: string;
|
|
27
|
+
tokens?: TokenMetadata;
|
|
28
|
+
retryCount?: number;
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* One traced agent run (root of an execution tree).
|
|
33
|
+
* MVP intentionally omits `input` / `output` on the run to limit PII, secrets, and serialization risk.
|
|
34
|
+
*/
|
|
35
|
+
interface Run {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string;
|
|
38
|
+
status: RunStatus;
|
|
39
|
+
startTime: number;
|
|
40
|
+
endTime?: number;
|
|
41
|
+
durationMs?: number;
|
|
42
|
+
error?: ErrorInfo;
|
|
43
|
+
metadata?: Record<string, unknown>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* One node in the execution tree under a {@link Run}.
|
|
47
|
+
* MVP intentionally omits `input` / `output`; capture/redaction may come in a later version.
|
|
48
|
+
*/
|
|
49
|
+
interface Step {
|
|
50
|
+
id: string;
|
|
51
|
+
runId: string;
|
|
52
|
+
parentId?: string;
|
|
53
|
+
name: string;
|
|
54
|
+
type: StepType;
|
|
55
|
+
status: StepStatus;
|
|
56
|
+
startTime: number;
|
|
57
|
+
endTime?: number;
|
|
58
|
+
durationMs?: number;
|
|
59
|
+
error?: ErrorInfo;
|
|
60
|
+
metadata?: StepMetadata;
|
|
61
|
+
}
|
|
62
|
+
/** Version of the JSONL trace line schema consumed by AgentInspect tooling. */
|
|
63
|
+
type TraceSchemaVersion = "0.1";
|
|
64
|
+
/** Fields shared by every persisted trace event line. */
|
|
65
|
+
interface TraceEventBase {
|
|
66
|
+
schemaVersion: TraceSchemaVersion;
|
|
67
|
+
event: string;
|
|
68
|
+
timestamp: number;
|
|
69
|
+
}
|
|
70
|
+
/** Emitted when a run begins. */
|
|
71
|
+
interface RunStartedEvent extends TraceEventBase {
|
|
72
|
+
event: "run_started";
|
|
73
|
+
runId: string;
|
|
74
|
+
name: string;
|
|
75
|
+
startTime: number;
|
|
76
|
+
metadata?: Record<string, unknown>;
|
|
77
|
+
}
|
|
78
|
+
/** Emitted when a run finishes successfully or with an error. */
|
|
79
|
+
interface RunCompletedEvent extends TraceEventBase {
|
|
80
|
+
event: "run_completed";
|
|
81
|
+
runId: string;
|
|
82
|
+
status: "success" | "error";
|
|
83
|
+
endTime: number;
|
|
84
|
+
durationMs: number;
|
|
85
|
+
error?: ErrorInfo;
|
|
86
|
+
}
|
|
87
|
+
/** Emitted when a step begins (including nested steps under `parentId`). */
|
|
88
|
+
interface StepStartedEvent extends TraceEventBase {
|
|
89
|
+
event: "step_started";
|
|
90
|
+
runId: string;
|
|
91
|
+
stepId: string;
|
|
92
|
+
parentId?: string;
|
|
93
|
+
name: string;
|
|
94
|
+
type: StepType;
|
|
95
|
+
startTime: number;
|
|
96
|
+
metadata?: StepMetadata;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Emitted when a step finishes (success or failure).
|
|
100
|
+
* Failures use `status: "error"` and optional {@link ErrorInfo}; there is no separate `step_failed` event in MVP.
|
|
101
|
+
*/
|
|
102
|
+
interface StepCompletedEvent extends TraceEventBase {
|
|
103
|
+
event: "step_completed";
|
|
104
|
+
runId: string;
|
|
105
|
+
stepId: string;
|
|
106
|
+
status: "success" | "error";
|
|
107
|
+
endTime: number;
|
|
108
|
+
durationMs: number;
|
|
109
|
+
error?: ErrorInfo;
|
|
110
|
+
}
|
|
111
|
+
/** Discriminated union of all MVP trace events written as JSONL lines. */
|
|
112
|
+
type TraceEvent = RunStartedEvent | RunCompletedEvent | StepStartedEvent | StepCompletedEvent;
|
|
113
|
+
/** Options for `inspectRun()` (implemented in a later step). */
|
|
114
|
+
interface InspectRunOptions {
|
|
115
|
+
traceDir?: string;
|
|
116
|
+
silent?: boolean;
|
|
117
|
+
metadata?: Record<string, unknown>;
|
|
118
|
+
}
|
|
119
|
+
/** Options passed when opening a logical step (implemented in a later step). */
|
|
120
|
+
interface StepOptions {
|
|
121
|
+
type?: StepType;
|
|
122
|
+
metadata?: StepMetadata;
|
|
123
|
+
}
|
|
124
|
+
/** Options for `observe()` — same surface as {@link InspectRunOptions} in MVP. */
|
|
125
|
+
type ObserveOptions = InspectRunOptions;
|
|
126
|
+
/**
|
|
127
|
+
* Resolved settings for the active run while tracing is enabled.
|
|
128
|
+
* Populated by runtime code in a later step; defined here for `context.ts`.
|
|
129
|
+
*/
|
|
130
|
+
interface ExecutionContext {
|
|
131
|
+
runId: string;
|
|
132
|
+
runName: string;
|
|
133
|
+
traceDir: string;
|
|
134
|
+
silent: boolean;
|
|
135
|
+
metadata?: Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
/** Stack position of the step currently executing (used by future context tracking). */
|
|
138
|
+
interface ActiveStepContext {
|
|
139
|
+
stepId: string;
|
|
140
|
+
parentId?: string;
|
|
141
|
+
depth: number;
|
|
142
|
+
}
|
|
143
|
+
/** Returns true if `value` is one of the MVP {@link StepType} literals. */
|
|
144
|
+
declare function isStepType(value: unknown): value is StepType;
|
|
145
|
+
/** Returns true if `value` is one of the MVP {@link StepStatus} literals. */
|
|
146
|
+
declare function isStepStatus(value: unknown): value is StepStatus;
|
|
147
|
+
/**
|
|
148
|
+
* Narrowing guard for a {@link TraceEvent} object with required MVP fields.
|
|
149
|
+
* Does not deeply validate optional `metadata` shapes.
|
|
150
|
+
*/
|
|
151
|
+
declare function isTraceEvent(value: unknown): value is TraceEvent;
|
|
152
|
+
|
|
153
|
+
/** Default folder under the user home for AgentInspect data. */
|
|
154
|
+
declare const DEFAULT_TRACE_DIR_NAME = ".agent-inspect";
|
|
155
|
+
/** Subfolder where JSONL run traces are stored. */
|
|
156
|
+
declare const RUNS_DIR_NAME = "runs";
|
|
157
|
+
/** Writable trace root when the default home path cannot be used. */
|
|
158
|
+
declare const FALLBACK_TRACE_DIR: string;
|
|
159
|
+
/** Maximum display length for run/step names before truncation. */
|
|
160
|
+
declare const MAX_NAME_LENGTH = 100;
|
|
161
|
+
/** Returns `run_` + a 10-character nanoid segment. */
|
|
162
|
+
declare function createRunId(): string;
|
|
163
|
+
/** Returns `step_` + a 10-character nanoid segment. */
|
|
164
|
+
declare function createStepId(): string;
|
|
165
|
+
/**
|
|
166
|
+
* Formats a duration for CLI display.
|
|
167
|
+
* Under 1000 ms uses whole milliseconds; from 1000 ms uses seconds with one decimal.
|
|
168
|
+
*/
|
|
169
|
+
declare function formatDuration(ms: number): string;
|
|
170
|
+
/**
|
|
171
|
+
* Formats a Unix timestamp (ms) as local `YYYY-MM-DD HH:mm:ss`.
|
|
172
|
+
* Invalid values yield `"Invalid date"` (no throw).
|
|
173
|
+
*/
|
|
174
|
+
declare function formatTimestamp(timestamp: number): string;
|
|
175
|
+
/**
|
|
176
|
+
* Default directory for trace files: `~/DEFAULT_TRACE_DIR_NAME/RUNS_DIR_NAME`.
|
|
177
|
+
* Falls back to {@link FALLBACK_TRACE_DIR} when home cannot be resolved.
|
|
178
|
+
*/
|
|
179
|
+
declare function getDefaultTraceDir(): string;
|
|
180
|
+
/**
|
|
181
|
+
* Full path to the JSONL trace file for a run.
|
|
182
|
+
* `runId` is passed through `path.basename` to avoid traversal; empty ids become `run_unknown`.
|
|
183
|
+
*/
|
|
184
|
+
declare function getTraceFilePath(runId: string, traceDir?: string): string;
|
|
185
|
+
/**
|
|
186
|
+
* Ensures a trace directory exists (recursive). Tries {@link FALLBACK_TRACE_DIR} on failure.
|
|
187
|
+
* Returns the directory path that callers should prefer: primary, fallback, or original if both mkdir attempts fail.
|
|
188
|
+
* Emits concise `[AgentInspect]` warnings on failure; never throws.
|
|
189
|
+
*/
|
|
190
|
+
declare function ensureTraceDir(traceDir: string): Promise<string>;
|
|
191
|
+
/**
|
|
192
|
+
* Normalizes any thrown/caught value into {@link ErrorInfo}.
|
|
193
|
+
* Never throws (circular structures and non-JSON values fall back to a generic message).
|
|
194
|
+
*/
|
|
195
|
+
declare function formatError(error: unknown): ErrorInfo;
|
|
196
|
+
/**
|
|
197
|
+
* Truncates a display name to `maxLength`, appending `"..."` when shortened.
|
|
198
|
+
* Empty or non-string input becomes `"unnamed"`.
|
|
199
|
+
*/
|
|
200
|
+
declare function truncateName(name: string, maxLength?: number): string;
|
|
201
|
+
/**
|
|
202
|
+
* Instrumentation-only warning to stderr. Not a general-purpose logger.
|
|
203
|
+
* Optional `error` is summarized via {@link formatError} (message only).
|
|
204
|
+
*/
|
|
205
|
+
declare function warn(message: string, error?: unknown): void;
|
|
206
|
+
|
|
207
|
+
/** Returns the active run context, without internal step fields. */
|
|
208
|
+
declare function getCurrentContext(): ExecutionContext | undefined;
|
|
209
|
+
/** Active `runId` when inside `runWithContext`, else `undefined`. */
|
|
210
|
+
declare function getCurrentRunId(): string | undefined;
|
|
211
|
+
/** Active `runName` when inside `runWithContext`, else `undefined`. */
|
|
212
|
+
declare function getCurrentRunName(): string | undefined;
|
|
213
|
+
/**
|
|
214
|
+
* Active step id in this async scope (parent for nested `step()` calls).
|
|
215
|
+
* `undefined` at run root or outside any run.
|
|
216
|
+
*/
|
|
217
|
+
declare function getCurrentStepId(): string | undefined;
|
|
218
|
+
/** Alias of {@link getCurrentStepId} for readability in step instrumentation. */
|
|
219
|
+
declare function getParentStepId(): string | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* Nesting depth: `0` at run root, increments by one per nested `runWithStepContext`.
|
|
222
|
+
* Returns `0` outside any run.
|
|
223
|
+
*/
|
|
224
|
+
declare function getCurrentDepth(): number;
|
|
225
|
+
declare function hasActiveContext(): boolean;
|
|
226
|
+
declare function getTraceDirFromContext(): string | undefined;
|
|
227
|
+
declare function isSilentContext(): boolean;
|
|
228
|
+
/**
|
|
229
|
+
* Runs `fn` with a fresh AgentInspect run context (depth 0, no active step).
|
|
230
|
+
* Propagates sync/async results and rejections; does not swallow user errors.
|
|
231
|
+
*/
|
|
232
|
+
declare function runWithContext<T>(context: ExecutionContext, fn: () => Promise<T> | T): Promise<T>;
|
|
233
|
+
/**
|
|
234
|
+
* Runs `fn` with `stepId` as the active step (incremented depth).
|
|
235
|
+
* If no run is active, runs `fn` without altering async context.
|
|
236
|
+
*/
|
|
237
|
+
declare function runWithStepContext<T>(stepId: string, fn: () => Promise<T> | T): Promise<T>;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Strict MVP validation before writing JSONL. Rejects empty ids, non-finite times, and malformed payloads.
|
|
241
|
+
*/
|
|
242
|
+
declare function validateEvent(event: unknown): event is TraceEvent;
|
|
243
|
+
/** Serializes a trace line as compact JSON without a trailing newline. */
|
|
244
|
+
declare function serializeEvent(event: TraceEvent): string;
|
|
245
|
+
/**
|
|
246
|
+
* Creates (or truncates) an empty JSONL file for a run. Uses {@link ensureTraceDir} then {@link getTraceFilePath}.
|
|
247
|
+
* On failure, retries once under {@link FALLBACK_TRACE_DIR}.
|
|
248
|
+
*/
|
|
249
|
+
declare function initializeTraceFile(runId: string, traceDir: string): Promise<string | undefined>;
|
|
250
|
+
/**
|
|
251
|
+
* Appends one validated JSONL line for `event.runId`. Falls back to {@link FALLBACK_TRACE_DIR} on append failure.
|
|
252
|
+
*/
|
|
253
|
+
declare function writeTraceEvent(event: TraceEvent, traceDir: string): Promise<void>;
|
|
254
|
+
/**
|
|
255
|
+
* Reads raw JSONL file contents for a run, or `undefined` if missing or unreadable.
|
|
256
|
+
*/
|
|
257
|
+
declare function readTraceFile(runId: string, traceDir: string): Promise<string | undefined>;
|
|
258
|
+
/** Parses JSONL into validated {@link TraceEvent} rows; invalid lines are skipped with a warning. */
|
|
259
|
+
declare function readTraceEvents(runId: string, traceDir: string): Promise<TraceEvent[]>;
|
|
260
|
+
/**
|
|
261
|
+
* Lists `.jsonl` file names in `traceDir`, newest by mtime first (name sort as tie-breaker).
|
|
262
|
+
*/
|
|
263
|
+
declare function listTraceFiles(traceDir: string): Promise<string[]>;
|
|
264
|
+
/** Maps a `.jsonl` file name to its run id (basename only; no traversal). */
|
|
265
|
+
declare function getRunIdFromTraceFileName(fileName: string): string | undefined;
|
|
266
|
+
|
|
267
|
+
/** Two spaces per nesting level in terminal output. */
|
|
268
|
+
declare const TERMINAL_INDENT = " ";
|
|
269
|
+
/** Max display length for names in terminal output. */
|
|
270
|
+
declare const MAX_TERMINAL_NAME_LENGTH = 80;
|
|
271
|
+
/** Max nesting depth used for indentation (prevents huge indents). */
|
|
272
|
+
declare const MAX_TERMINAL_DEPTH = 10;
|
|
273
|
+
/** Indentation string for a nesting depth (capped, never negative). */
|
|
274
|
+
declare function getIndent(depth: number): string;
|
|
275
|
+
/** Truncates a display name for terminal use; invalid input becomes `"unnamed"`. */
|
|
276
|
+
declare function formatTerminalName(name: string): string;
|
|
277
|
+
/** Renders a single step line (colored); does not consult silent mode. */
|
|
278
|
+
declare function renderStepLine(name: string, durationMs: number | undefined, status: StepStatus, depth?: number): string;
|
|
279
|
+
/** Renders an error summary line (no stack in MVP). */
|
|
280
|
+
declare function renderErrorLine(error: ErrorInfo, depth?: number): string;
|
|
281
|
+
/** Plain-text run summary lines (no chalk) for stable testing and CLI reuse. */
|
|
282
|
+
declare function renderRunSummary(durationMs: number, status: RunStatus, traceFilePath?: string): string[];
|
|
283
|
+
/** Prints run header with icon and dim run id. */
|
|
284
|
+
declare function printRunStart(runId: string, name: string): void;
|
|
285
|
+
/** Prints a running step line. */
|
|
286
|
+
declare function printStepStart(name: string, depth?: number): void;
|
|
287
|
+
/** Prints a completed step line with duration and status icon. */
|
|
288
|
+
declare function printStepComplete(name: string, durationMs: number, status: StepStatus, depth?: number): void;
|
|
289
|
+
/** Prints a structured error line (message only, no stack). */
|
|
290
|
+
declare function printError(error: ErrorInfo, depth?: number): void;
|
|
291
|
+
/** Prints run completion summary and optional trace path. */
|
|
292
|
+
declare function printRunComplete(_name: string, _runId: string, durationMs: number, status: RunStatus, traceFilePath?: string): void;
|
|
293
|
+
/** Prints which step failed. */
|
|
294
|
+
declare function printFailedAt(stepName: string): void;
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Runs `fn` inside an AgentInspect trace: JSONL `run_started` / `run_completed`, optional terminal output,
|
|
298
|
+
* and {@link ExecutionContext} for nested APIs. Instrumentation failures are swallowed; user errors are re-thrown.
|
|
299
|
+
*/
|
|
300
|
+
declare function inspectRun<T>(name: string, fn: () => Promise<T> | T, options?: InspectRunOptions): Promise<T>;
|
|
301
|
+
|
|
302
|
+
/** Callable step tracer plus {@link step.llm} and {@link step.tool} shortcuts. */
|
|
303
|
+
type StepFunction = {
|
|
304
|
+
<T>(name: string, fn: () => Promise<T> | T, options?: StepOptions): Promise<T>;
|
|
305
|
+
llm: <T>(model: string, fn: () => Promise<T> | T) => Promise<T>;
|
|
306
|
+
tool: <T>(toolName: string, fn: () => Promise<T> | T) => Promise<T>;
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* Traces a named unit of work inside `inspectRun` (`step_started` / `step_completed`, optional terminal).
|
|
310
|
+
* Outside a run, executes `fn` with a warn only. Preserves return values and rethrows user errors unchanged.
|
|
311
|
+
*
|
|
312
|
+
* - `step.llm(model, fn)` — `type: "llm"`, `metadata.model` (no SDK calls or token counting).
|
|
313
|
+
* - `step.tool(toolName, fn)` — `type: "tool"`, `metadata.toolName` (no framework interception).
|
|
314
|
+
*/
|
|
315
|
+
declare const step: StepFunction;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Returns a Proxy that traces top-level `run`, `execute`, and `invoke` via {@link inspectRun}.
|
|
319
|
+
* Other properties pass through; function members are bound to the real target so class private fields work.
|
|
320
|
+
* Invalid agents are returned unchanged (with a warn); this function never throws.
|
|
321
|
+
*/
|
|
322
|
+
declare function observe<T>(agent: T, options?: ObserveOptions): T;
|
|
323
|
+
|
|
324
|
+
export { type ActiveStepContext, DEFAULT_TRACE_DIR_NAME, type ErrorInfo, type ExecutionContext, FALLBACK_TRACE_DIR, type InspectRunOptions, MAX_NAME_LENGTH, MAX_TERMINAL_DEPTH, MAX_TERMINAL_NAME_LENGTH, type ObserveOptions, RUNS_DIR_NAME, type Run, type RunCompletedEvent, type RunStartedEvent, type RunStatus, type Step, type StepCompletedEvent, type StepMetadata, type StepOptions, type StepStartedEvent, type StepStatus, type StepType, TERMINAL_INDENT, type TokenMetadata, type TraceEvent, type TraceEventBase, type TraceSchemaVersion, createRunId, createStepId, ensureTraceDir, formatDuration, formatError, formatTerminalName, formatTimestamp, getCurrentContext, getCurrentDepth, getCurrentRunId, getCurrentRunName, getCurrentStepId, getDefaultTraceDir, getIndent, getParentStepId, getRunIdFromTraceFileName, getTraceDirFromContext, getTraceFilePath, hasActiveContext, initializeTraceFile, inspectRun, isSilentContext, isStepStatus, isStepType, isTraceEvent, listTraceFiles, observe, printError, printFailedAt, printRunComplete, printRunStart, printStepComplete, printStepStart, readTraceEvents, readTraceFile, renderErrorLine, renderRunSummary, renderStepLine, runWithContext, runWithStepContext, serializeEvent, step, truncateName, validateEvent, warn, writeTraceEvent };
|