@dudousxd/nestjs-agent-telescope 0.3.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,39 +1,90 @@
1
1
  import * as _dudousxd_nestjs_telescope from '@dudousxd/nestjs-telescope';
2
2
  import { Watcher, WatcherContext, DashboardSpec, DataProvider } from '@dudousxd/nestjs-telescope';
3
- import { GovernanceRange, ActorSpendRow, ModelSpendRow, UsageTrendPoint } from '@dudousxd/nestjs-agent-core';
3
+ import { GovernanceRange, ActorSpendRow, ModelSpendRow, PendingApprovalRow, RecentRunRow, ThreadActivityRow, ToolCallActivityRow, RunAgentBreakdownRow, RunErrorBreakdownRow, RunTrendPoint, ThreadSpendRow, ToolStatRow, UsageTrendPoint } from '@dudousxd/nestjs-agent-core';
4
4
 
5
5
  /**
6
6
  * The first-class Telescope extension for nestjs-agent: an "Agent" tab fed by two sources —
7
7
  * the `aviary:agent:*` diagnostics channel (live runs, tool calls) via the watcher, and the
8
- * authoritative `AGENT_GOVERNANCE_QUERIES` read-model (historical spend/usage) via the governance
9
- * providers. The extension `name`, entry-type id, dashboard id, and every provider name share the
10
- * `agent` prefix so the registry's global-uniqueness namespaces never collide with sibling extensions.
8
+ * authoritative `AGENT_GOVERNANCE_QUERIES` read-model (historical spend/usage, run reliability,
9
+ * tool activity, the approvals inbox) via the governance providers. The extension `name`,
10
+ * entry-type id, dashboard id, and every provider name share the `agent` prefix so the registry's
11
+ * global-uniqueness namespaces never collide with sibling extensions.
11
12
  *
12
- * Host wiring: the governance (Spend/Models/Actors) panels resolve `AGENT_GOVERNANCE_QUERIES` from
13
- * the host DI container at request time (via `ctx.moduleRef`). The host must bind that token — from
14
- * its store adapter (e.g. `store-mikro-orm` / `store-drizzle` / `testing`) — in the same module that
15
- * registers `TelescopeModule.forRoot({ extensions: [agentTelescopeExtension()] })`. If the binding is
16
- * absent, those panels render an empty state; the live watcher-fed panels keep working regardless.
13
+ * `threadHref`/`runHref` deep-link a table row's `threadId`/`runId` cell out to the host's own
14
+ * thread/run viewer — passed straight through to {@link agentDashboard}, mirroring
15
+ * `durableTelescopeExtension`'s `runHref` option.
16
+ *
17
+ * Host wiring: the governance panels (Spend/Models/Actors/Reliability/Runs/Threads/Approvals/Tool
18
+ * stats/Recent tool calls) resolve `AGENT_GOVERNANCE_QUERIES` from the host DI container at
19
+ * request time (via `ctx.moduleRef`). The host must bind that token — from its store adapter
20
+ * (e.g. `store-mikro-orm` / `store-drizzle` / `testing`) — in the same module that registers
21
+ * `TelescopeModule.forRoot({ extensions: [agentTelescopeExtension()] })`. If the binding is
22
+ * absent, those panels render an empty state; the live watcher-fed panels (Runs/Tokens stats and
23
+ * the Tool-call status breakdown) keep working regardless.
17
24
  */
18
- declare function agentTelescopeExtension(): _dudousxd_nestjs_telescope.TelescopeExtension;
25
+ declare function agentTelescopeExtension(opts?: {
26
+ threadHref?: string;
27
+ runHref?: string;
28
+ }): _dudousxd_nestjs_telescope.TelescopeExtension;
19
29
 
20
30
  /**
21
31
  * Records `aviary:agent:*` diagnostics events as Telescope entries of type `agent`. It depends
22
32
  * only on the diagnostics channel — not on the agent runtime — so it stays fully decoupled.
33
+ *
34
+ * Iterates {@link AGENT_DIAGNOSTIC_EVENTS} (all 8 events on `ChannelRegistry['agent']`) rather
35
+ * than a hand-written literal, so `run.failed`/`delegated`/`retrieved` are recorded and tagged —
36
+ * filterable in the Telescope UI — like every other agent event.
37
+ *
38
+ * **Superseded by `@dudousxd/nestjs-diagnostics-telescope`'s generic watcher,** which
39
+ * auto-captures every `aviary:agent:*` channel registered in the diagnostics registry — prefer
40
+ * that when the generic bridge is already in use; pass `agentDiagnosticKey(event)` keys to its
41
+ * `exclude` option to mute a noisy one. This watcher is kept for standalone use without the
42
+ * diagnostics telescope bridge.
43
+ *
44
+ * Register it with the telescope module's watcher list.
23
45
  */
24
46
  declare class AgentTelescopeWatcher implements Watcher {
25
47
  readonly type = "agent";
48
+ private readonly disposers;
26
49
  register(ctx: WatcherContext): void;
50
+ /** Detach all channel subscriptions (e.g. on module destroy). */
51
+ dispose(): void;
27
52
  }
28
53
 
29
- /** The "Agent" overview dashboard. Panels bind to the `agent.*` data providers. */
30
- declare function agentDashboard(): DashboardSpec;
54
+ /**
55
+ * The "Agent" overview dashboard. Panels bind to the `agent.*` data providers.
56
+ *
57
+ * `threadHref`/`runHref` are URL templates for deep-linking a `{threadId}`/`{runId}` cell out to
58
+ * the host's own thread/run viewer (e.g. the standalone `@dudousxd/nestjs-agent-dashboard` SPA),
59
+ * mirroring `durableTelescopeExtension`'s `runHref` option. Every table whose rows carry a
60
+ * `threadId`/`runId` gets a `Column.link` for it (via {@link col}); omit an option to leave that
61
+ * column plain text.
62
+ */
63
+ declare function agentDashboard(opts?: {
64
+ threadHref?: string;
65
+ runHref?: string;
66
+ }): DashboardSpec;
31
67
 
32
68
  /** stat → total agent runs (run.finished entries). */
33
69
  declare function agentRunsProvider(): DataProvider;
34
70
  /** stat → total tokens across all finished runs. */
35
71
  declare function agentTokensProvider(): DataProvider;
36
- /** table → recent tool calls. */
72
+ /**
73
+ * table → recent tool calls, from Telescope's own ephemeral event storage.
74
+ *
75
+ * @deprecated Superseded in the shipped dashboard by `agentRecentToolCallsTableProvider`
76
+ * (`agent.tools.recent`, in `agent-governance-providers.ts`), which reads the durable,
77
+ * restart-surviving `AGENT_GOVERNANCE_QUERIES.recentToolCalls` read-model. That durable route is
78
+ * never behind this ephemeral one: `agent-loop.ts` always awaits `store.recordToolCall` /
79
+ * `store.updateToolCall` (the durable write) BEFORE calling `publishAgentToolCall` (the event this
80
+ * provider reads), so a row is durably queryable strictly before the ephemeral entry exists. The
81
+ * durable route also captures the `pending_approval` state this ephemeral channel never emits at
82
+ * all (no `publishAgentToolCall` call sits between `recordToolCall(status: 'pending_approval')`
83
+ * and the eventual terminal transition) — so there's no in-flight state left for this table to
84
+ * uniquely show. Kept exported (not removed — that would be a breaking export change) for hosts
85
+ * that use `agentTelescopeExtension()` without wiring `AGENT_GOVERNANCE_QUERIES`, or that compose
86
+ * a custom extension from these lower-level provider functions directly.
87
+ */
37
88
  declare function agentToolsProvider(): DataProvider;
38
89
  /** breakdown → tool-call status distribution. */
39
90
  declare function agentToolStatusProvider(): DataProvider;
@@ -58,6 +109,14 @@ interface ActorSpendTableRow {
58
109
  totalTokens: number;
59
110
  costUsd: number;
60
111
  }
112
+ /** One row of the top-threads-by-cost table. */
113
+ interface ThreadSpendTableRow {
114
+ title: string;
115
+ actorRef: string;
116
+ requests: number;
117
+ totalTokens: number;
118
+ costUsd: number;
119
+ }
61
120
  /** One point of the timeseries trend (Telescope core: `{ label } & Record<string, number>`). */
62
121
  interface UsageTrendTableRow {
63
122
  label: string;
@@ -81,10 +140,84 @@ declare function toModelSpendSegments(rows: ModelSpendRow[]): BreakdownSegment[]
81
140
  declare function toModelSpendRows(rows: ModelSpendRow[]): ModelSpendTableRow[];
82
141
  /** Spend-by-actor as table rows. */
83
142
  declare function toActorSpendRows(rows: ActorSpendRow[]): ActorSpendTableRow[];
143
+ /** Top-threads-by-cost as table rows. */
144
+ declare function toThreadSpendRows(rows: ThreadSpendRow[]): ThreadSpendTableRow[];
84
145
  /** Spend-by-actor as breakdown segments (actors with zero cost are dropped). */
85
146
  declare function toActorSpendSegments(rows: ActorSpendRow[]): BreakdownSegment[];
86
147
  /** Daily usage trend as timeseries rows keyed by `label` (the day). */
87
148
  declare function toUsageTrendRows(points: UsageTrendPoint[]): UsageTrendTableRow[];
149
+ /** Run/failure/retry rollup as table rows — `RunAgentBreakdownRow` is already table-shaped. */
150
+ declare function toRunAgentTableRows(rows: RunAgentBreakdownRow[]): Array<{
151
+ agentName: string;
152
+ runs: number;
153
+ failed: number;
154
+ retries: number;
155
+ }>;
156
+ /** Failed-run counts by error code as breakdown segments. */
157
+ declare function toRunErrorSegments(rows: RunErrorBreakdownRow[]): BreakdownSegment[];
158
+ /** One point of the run/failure trend (Telescope core: `{ label } & Record<string, number>`). */
159
+ interface RunTrendTableRow {
160
+ label: string;
161
+ runs: number;
162
+ failed: number;
163
+ }
164
+ /** Daily run/failure trend as timeseries rows keyed by `label` (the day). */
165
+ declare function toRunTrendRows(points: RunTrendPoint[]): RunTrendTableRow[];
166
+ /**
167
+ * Cap a run's error message to {@link ERROR_MESSAGE_CAP} characters before it leaves the provider.
168
+ * `DataProvider.resolve` output bypasses Telescope core's `redact()` pipeline — that only runs on
169
+ * entries a `Watcher` records via `ctx.record`, not on values a provider computes and returns
170
+ * directly to a panel — so an unbounded stack trace or secret-laden failure message would ride
171
+ * straight into a table cell with no truncation/redaction safety net. This cap is a stopgap
172
+ * mitigation, not a substitute for the (out-of-scope this wave) telescope-core redaction hook for
173
+ * DataProvider output.
174
+ */
175
+ declare function capErrorMessage(message: string | null): string | null;
176
+ /** Shorten a full sha256 promptHash to a compact chip for the table column. */
177
+ declare function shortPromptHash(promptHash: string | null): string | null;
178
+ /** One row of the recent-runs table — every nullable field falls back to {@link NO_VALUE}. */
179
+ interface RecentRunTableRow {
180
+ startedAt: string;
181
+ runId: string;
182
+ threadId: string;
183
+ actorRef: string;
184
+ agentName: string;
185
+ status: string;
186
+ durationMs: number | string;
187
+ retries: number;
188
+ errorCode: string;
189
+ errorMessage: string;
190
+ promptHash: string;
191
+ }
192
+ /** Recent runs as table rows: errorMessage capped, promptHash shortened to a chip. */
193
+ declare function toRecentRunTableRows(rows: RecentRunRow[]): RecentRunTableRow[];
194
+ /** Recent tool calls as table rows — `ToolCallActivityRow` is already table-shaped. */
195
+ declare function toRecentToolCallRows(rows: ToolCallActivityRow[]): ToolCallActivityRow[];
196
+ /** Recent threads as table rows — `ThreadActivityRow` is already table-shaped. */
197
+ declare function toRecentThreadTableRows(rows: ThreadActivityRow[]): ThreadActivityRow[];
198
+ /** One row of the pending-approvals inbox table, `input` stringified for display. */
199
+ interface PendingApprovalTableRow {
200
+ toolCallId: string;
201
+ toolName: string;
202
+ threadId: string;
203
+ threadTitle: string;
204
+ actorRef: string;
205
+ agentName: string;
206
+ requestedAt: string;
207
+ }
208
+ /** Pending-approvals rows as table rows (drops the raw `input` — not renderable in a table cell). */
209
+ declare function toPendingApprovalTableRows(rows: PendingApprovalRow[]): PendingApprovalTableRow[];
210
+ /** One row of the per-tool stats table, `p95ExecutionMs` falling back to {@link NO_VALUE}. */
211
+ interface ToolStatTableRow {
212
+ toolName: string;
213
+ toolType: string;
214
+ calls: number;
215
+ failed: number;
216
+ rejected: number;
217
+ p95ExecutionMs: number | string;
218
+ }
219
+ /** Per-tool call/failure/rejection/latency rollup as table rows. */
220
+ declare function toToolStatTableRows(rows: ToolStatRow[]): ToolStatTableRow[];
88
221
  /** stat → authoritative total spend (USD) over the range. */
89
222
  declare function agentSpendTotalProvider(): DataProvider;
90
223
  /** stat → authoritative total tokens (input + output) over the range. */
@@ -99,5 +232,52 @@ declare function agentUsageTrendProvider(): DataProvider;
99
232
  declare function agentActorSpendTableProvider(): DataProvider;
100
233
  /** breakdown → spend share per actor. */
101
234
  declare function agentSpendByActorProvider(): DataProvider;
235
+ /** table → top threads by cost (title, actor, requests, tokens, cost). */
236
+ declare function agentTopThreadsTableProvider(): DataProvider;
237
+ /** stat → total runs over the range. */
238
+ declare function agentRunsTotalProvider(): DataProvider;
239
+ /** stat → completed/total success rate over the range. */
240
+ declare function agentRunsSuccessRateProvider(): DataProvider;
241
+ /** stat → failed run count over the range. */
242
+ declare function agentRunsFailedProvider(): DataProvider;
243
+ /** stat → total llm-step retries across the range's runs. */
244
+ declare function agentRunsRetriesProvider(): DataProvider;
245
+ /**
246
+ * distribution → run duration. `RunMetrics` exposes only the two percentiles (no raw per-run
247
+ * samples), so there is nothing to bucket into a histogram — the panel renders its `p50`/`p95`
248
+ * markers over an empty bucket series. `exactOptionalPropertyTypes` means a `null` percentile is
249
+ * omitted from the result rather than carried as an explicit `undefined`.
250
+ */
251
+ declare function agentRunsDurationProvider(): DataProvider;
252
+ /** table → run/failure/retry rollup per agent. */
253
+ declare function agentRunsByAgentTableProvider(): DataProvider;
254
+ /** breakdown → failed runs by error code. */
255
+ declare function agentRunErrorsProvider(): DataProvider;
256
+ /** timeseries → daily runs + failures trend. */
257
+ declare function agentRunsTrendProvider(): DataProvider;
258
+ /**
259
+ * table → most recent runs (newest first), errorMessage capped and promptHash shortened to a chip
260
+ * — see {@link capErrorMessage} for why the cap happens here rather than downstream.
261
+ */
262
+ declare function agentRecentRunsTableProvider(): DataProvider;
263
+ /**
264
+ * table → most recent tool calls (newest first), from the durable read-model. Replaces the
265
+ * ephemeral, watcher-fed `agent.tools` provider in `agent-data-providers.ts` for the shipped
266
+ * dashboard — see that file's header comment for why.
267
+ */
268
+ declare function agentRecentToolCallsTableProvider(): DataProvider;
269
+ /** table → most recently active threads, with rolled-up message/token counts. */
270
+ declare function agentRecentThreadsTableProvider(): DataProvider;
271
+ /**
272
+ * stat → count of tool calls sitting `pending_approval` across every thread. The SPI's
273
+ * `pendingApprovals` only returns a capped list, not a true count, so this undercounts a backlog
274
+ * larger than {@link PENDING_APPROVALS_COUNT_LIMIT} — a backlog that size signals a bigger
275
+ * operational problem than an off-by-N stat, so the approximation is an acceptable tradeoff.
276
+ */
277
+ declare function agentPendingApprovalsCountProvider(): DataProvider;
278
+ /** table → the pending-approvals inbox, oldest request first. */
279
+ declare function agentPendingApprovalsTableProvider(): DataProvider;
280
+ /** table → per-tool call/failure/rejection/latency rollup over the range. */
281
+ declare function agentToolStatsTableProvider(): DataProvider;
102
282
 
103
- export { AgentTelescopeWatcher, agentActorSpendTableProvider, agentDashboard, agentModelSpendTableProvider, agentRunsProvider, agentSpendByActorProvider, agentSpendByModelProvider, agentSpendTotalProvider, agentTelescopeExtension, agentTokensProvider, agentTokensTotalProvider, agentToolStatusProvider, agentToolsProvider, agentUsageTrendProvider, resolveRange, shiftUtcDay, toActorSpendRows, toActorSpendSegments, toModelSpendRows, toModelSpendSegments, toUsageTrendRows, totalCostUsd, totalTokens };
283
+ export { AgentTelescopeWatcher, agentActorSpendTableProvider, agentDashboard, agentModelSpendTableProvider, agentPendingApprovalsCountProvider, agentPendingApprovalsTableProvider, agentRecentRunsTableProvider, agentRecentThreadsTableProvider, agentRecentToolCallsTableProvider, agentRunErrorsProvider, agentRunsByAgentTableProvider, agentRunsDurationProvider, agentRunsFailedProvider, agentRunsProvider, agentRunsRetriesProvider, agentRunsSuccessRateProvider, agentRunsTotalProvider, agentRunsTrendProvider, agentSpendByActorProvider, agentSpendByModelProvider, agentSpendTotalProvider, agentTelescopeExtension, agentTokensProvider, agentTokensTotalProvider, agentToolStatsTableProvider, agentToolStatusProvider, agentToolsProvider, agentTopThreadsTableProvider, agentUsageTrendProvider, capErrorMessage, resolveRange, shiftUtcDay, shortPromptHash, toActorSpendRows, toActorSpendSegments, toModelSpendRows, toModelSpendSegments, toPendingApprovalTableRows, toRecentRunTableRows, toRecentThreadTableRows, toRecentToolCallRows, toRunAgentTableRows, toRunErrorSegments, toRunTrendRows, toThreadSpendRows, toToolStatTableRows, toUsageTrendRows, totalCostUsd, totalTokens };