@dudousxd/nestjs-agent-telescope 0.3.5 → 0.5.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,89 @@
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 point events on `ChannelRegistry['agent']`)
35
+ * rather than a hand-written literal, so `run.failed`/`delegated`/`retrieved` are recorded and
36
+ * tagged — filterable in the Telescope UI — like every other agent event.
37
+ *
38
+ * Coexists with `@dudousxd/nestjs-diagnostics-telescope`'s generic watcher: `register()` CLAIMS
39
+ * every recorded key via `claimDiagnostics('agent', ...)` (diagnostics 0.7+), so the generic
40
+ * bridge skips them at record time instead of duplicating each event as a `diagnostic` entry —
41
+ * no `exclude` wiring needed anymore. `dispose()` releases the claim.
42
+ *
43
+ * Register it with the telescope module's watcher list.
23
44
  */
24
45
  declare class AgentTelescopeWatcher implements Watcher {
25
46
  readonly type = "agent";
47
+ private readonly disposers;
26
48
  register(ctx: WatcherContext): void;
49
+ /** Detach all channel subscriptions and release the diagnostics claim (e.g. on module destroy). */
50
+ dispose(): void;
27
51
  }
28
52
 
29
- /** The "Agent" overview dashboard. Panels bind to the `agent.*` data providers. */
30
- declare function agentDashboard(): DashboardSpec;
53
+ /**
54
+ * The "Agent" overview dashboard. Panels bind to the `agent.*` data providers.
55
+ *
56
+ * `threadHref`/`runHref` are URL templates for deep-linking a `{threadId}`/`{runId}` cell out to
57
+ * the host's own thread/run viewer (e.g. the standalone `@dudousxd/nestjs-agent-dashboard` SPA),
58
+ * mirroring `durableTelescopeExtension`'s `runHref` option. Every table whose rows carry a
59
+ * `threadId`/`runId` gets a `Column.link` for it (via {@link col}); omit an option to leave that
60
+ * column plain text.
61
+ */
62
+ declare function agentDashboard(opts?: {
63
+ threadHref?: string;
64
+ runHref?: string;
65
+ }): DashboardSpec;
31
66
 
32
67
  /** stat → total agent runs (run.finished entries). */
33
68
  declare function agentRunsProvider(): DataProvider;
34
69
  /** stat → total tokens across all finished runs. */
35
70
  declare function agentTokensProvider(): DataProvider;
36
- /** table → recent tool calls. */
71
+ /**
72
+ * table → recent tool calls, from Telescope's own ephemeral event storage.
73
+ *
74
+ * @deprecated Superseded in the shipped dashboard by `agentRecentToolCallsTableProvider`
75
+ * (`agent.tools.recent`, in `agent-governance-providers.ts`), which reads the durable,
76
+ * restart-surviving `AGENT_GOVERNANCE_QUERIES.recentToolCalls` read-model. That durable route is
77
+ * never behind this ephemeral one: `agent-loop.ts` always awaits `store.recordToolCall` /
78
+ * `store.updateToolCall` (the durable write) BEFORE calling `publishAgentToolCall` (the event this
79
+ * provider reads), so a row is durably queryable strictly before the ephemeral entry exists. The
80
+ * durable route also captures the `pending_approval` state this ephemeral channel never emits at
81
+ * all (no `publishAgentToolCall` call sits between `recordToolCall(status: 'pending_approval')`
82
+ * and the eventual terminal transition) — so there's no in-flight state left for this table to
83
+ * uniquely show. Kept exported (not removed — that would be a breaking export change) for hosts
84
+ * that use `agentTelescopeExtension()` without wiring `AGENT_GOVERNANCE_QUERIES`, or that compose
85
+ * a custom extension from these lower-level provider functions directly.
86
+ */
37
87
  declare function agentToolsProvider(): DataProvider;
38
88
  /** breakdown → tool-call status distribution. */
39
89
  declare function agentToolStatusProvider(): DataProvider;
@@ -58,6 +108,14 @@ interface ActorSpendTableRow {
58
108
  totalTokens: number;
59
109
  costUsd: number;
60
110
  }
111
+ /** One row of the top-threads-by-cost table. */
112
+ interface ThreadSpendTableRow {
113
+ title: string;
114
+ actorRef: string;
115
+ requests: number;
116
+ totalTokens: number;
117
+ costUsd: number;
118
+ }
61
119
  /** One point of the timeseries trend (Telescope core: `{ label } & Record<string, number>`). */
62
120
  interface UsageTrendTableRow {
63
121
  label: string;
@@ -81,10 +139,84 @@ declare function toModelSpendSegments(rows: ModelSpendRow[]): BreakdownSegment[]
81
139
  declare function toModelSpendRows(rows: ModelSpendRow[]): ModelSpendTableRow[];
82
140
  /** Spend-by-actor as table rows. */
83
141
  declare function toActorSpendRows(rows: ActorSpendRow[]): ActorSpendTableRow[];
142
+ /** Top-threads-by-cost as table rows. */
143
+ declare function toThreadSpendRows(rows: ThreadSpendRow[]): ThreadSpendTableRow[];
84
144
  /** Spend-by-actor as breakdown segments (actors with zero cost are dropped). */
85
145
  declare function toActorSpendSegments(rows: ActorSpendRow[]): BreakdownSegment[];
86
146
  /** Daily usage trend as timeseries rows keyed by `label` (the day). */
87
147
  declare function toUsageTrendRows(points: UsageTrendPoint[]): UsageTrendTableRow[];
148
+ /** Run/failure/retry rollup as table rows — `RunAgentBreakdownRow` is already table-shaped. */
149
+ declare function toRunAgentTableRows(rows: RunAgentBreakdownRow[]): Array<{
150
+ agentName: string;
151
+ runs: number;
152
+ failed: number;
153
+ retries: number;
154
+ }>;
155
+ /** Failed-run counts by error code as breakdown segments. */
156
+ declare function toRunErrorSegments(rows: RunErrorBreakdownRow[]): BreakdownSegment[];
157
+ /** One point of the run/failure trend (Telescope core: `{ label } & Record<string, number>`). */
158
+ interface RunTrendTableRow {
159
+ label: string;
160
+ runs: number;
161
+ failed: number;
162
+ }
163
+ /** Daily run/failure trend as timeseries rows keyed by `label` (the day). */
164
+ declare function toRunTrendRows(points: RunTrendPoint[]): RunTrendTableRow[];
165
+ /**
166
+ * Cap a run's error message to {@link ERROR_MESSAGE_CAP} characters before it leaves the provider.
167
+ * `DataProvider.resolve` output bypasses Telescope core's `redact()` pipeline — that only runs on
168
+ * entries a `Watcher` records via `ctx.record`, not on values a provider computes and returns
169
+ * directly to a panel — so an unbounded stack trace or secret-laden failure message would ride
170
+ * straight into a table cell with no truncation/redaction safety net. This cap is a stopgap
171
+ * mitigation, not a substitute for the (out-of-scope this wave) telescope-core redaction hook for
172
+ * DataProvider output.
173
+ */
174
+ declare function capErrorMessage(message: string | null): string | null;
175
+ /** Shorten a full sha256 promptHash to a compact chip for the table column. */
176
+ declare function shortPromptHash(promptHash: string | null): string | null;
177
+ /** One row of the recent-runs table — every nullable field falls back to {@link NO_VALUE}. */
178
+ interface RecentRunTableRow {
179
+ startedAt: string;
180
+ runId: string;
181
+ threadId: string;
182
+ actorRef: string;
183
+ agentName: string;
184
+ status: string;
185
+ durationMs: number | string;
186
+ retries: number;
187
+ errorCode: string;
188
+ errorMessage: string;
189
+ promptHash: string;
190
+ }
191
+ /** Recent runs as table rows: errorMessage capped, promptHash shortened to a chip. */
192
+ declare function toRecentRunTableRows(rows: RecentRunRow[]): RecentRunTableRow[];
193
+ /** Recent tool calls as table rows — `ToolCallActivityRow` is already table-shaped. */
194
+ declare function toRecentToolCallRows(rows: ToolCallActivityRow[]): ToolCallActivityRow[];
195
+ /** Recent threads as table rows — `ThreadActivityRow` is already table-shaped. */
196
+ declare function toRecentThreadTableRows(rows: ThreadActivityRow[]): ThreadActivityRow[];
197
+ /** One row of the pending-approvals inbox table, `input` stringified for display. */
198
+ interface PendingApprovalTableRow {
199
+ toolCallId: string;
200
+ toolName: string;
201
+ threadId: string;
202
+ threadTitle: string;
203
+ actorRef: string;
204
+ agentName: string;
205
+ requestedAt: string;
206
+ }
207
+ /** Pending-approvals rows as table rows (drops the raw `input` — not renderable in a table cell). */
208
+ declare function toPendingApprovalTableRows(rows: PendingApprovalRow[]): PendingApprovalTableRow[];
209
+ /** One row of the per-tool stats table, `p95ExecutionMs` falling back to {@link NO_VALUE}. */
210
+ interface ToolStatTableRow {
211
+ toolName: string;
212
+ toolType: string;
213
+ calls: number;
214
+ failed: number;
215
+ rejected: number;
216
+ p95ExecutionMs: number | string;
217
+ }
218
+ /** Per-tool call/failure/rejection/latency rollup as table rows. */
219
+ declare function toToolStatTableRows(rows: ToolStatRow[]): ToolStatTableRow[];
88
220
  /** stat → authoritative total spend (USD) over the range. */
89
221
  declare function agentSpendTotalProvider(): DataProvider;
90
222
  /** stat → authoritative total tokens (input + output) over the range. */
@@ -99,5 +231,53 @@ declare function agentUsageTrendProvider(): DataProvider;
99
231
  declare function agentActorSpendTableProvider(): DataProvider;
100
232
  /** breakdown → spend share per actor. */
101
233
  declare function agentSpendByActorProvider(): DataProvider;
234
+ /** table → top threads by cost (title, actor, requests, tokens, cost). */
235
+ declare function agentTopThreadsTableProvider(): DataProvider;
236
+ /** stat → total runs over the range. */
237
+ declare function agentRunsTotalProvider(): DataProvider;
238
+ /** stat → completed/total success rate over the range. */
239
+ declare function agentRunsSuccessRateProvider(): DataProvider;
240
+ /** stat → failed run count over the range. */
241
+ declare function agentRunsFailedProvider(): DataProvider;
242
+ /** stat → total llm-step retries across the range's runs. */
243
+ declare function agentRunsRetriesProvider(): DataProvider;
244
+ /**
245
+ * stat → run duration percentile, selected by `query.metric` (`'p95'` for p95, anything else —
246
+ * including omitted — for p50), mirroring `durable.duration`'s stat shortcut. NOT a
247
+ * `distribution` panel: `RunMetrics` exposes only the two percentiles (no raw per-run samples to
248
+ * bucket into a histogram), so a distribution here would render as a permanently-empty box. A
249
+ * `null` percentile (no settled runs in range) resolves to 0.
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 };