@dudousxd/nestjs-agent-core 0.5.0 → 0.7.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.cjs +107 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +141 -2
- package/dist/index.d.ts +141 -2
- package/dist/index.js +101 -10
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { ChannelRegistry } from '@dudousxd/nestjs-diagnostics';
|
|
2
3
|
|
|
3
4
|
/** Who is driving the turn. Roles + tenant come from the host app (nestjs-context/authz). */
|
|
4
5
|
interface Actor {
|
|
@@ -119,6 +120,11 @@ interface QuotaView {
|
|
|
119
120
|
interface Decision {
|
|
120
121
|
approved: boolean;
|
|
121
122
|
reason?: string;
|
|
123
|
+
/**
|
|
124
|
+
* Opaque ref of WHO decided (e.g. a console admin). When absent, the run's own actor decided
|
|
125
|
+
* (the chat flow).
|
|
126
|
+
*/
|
|
127
|
+
executedByRef?: string;
|
|
122
128
|
}
|
|
123
129
|
type MessageRole = 'user' | 'assistant' | 'system';
|
|
124
130
|
/**
|
|
@@ -357,6 +363,11 @@ declare const AGENT_ACTOR_DIRECTORY: unique symbol;
|
|
|
357
363
|
* {@link import('./spi/attachment-staging.js').AttachmentStagingStore}.
|
|
358
364
|
*/
|
|
359
365
|
declare const AGENT_ATTACHMENT_STAGING: unique symbol;
|
|
366
|
+
/**
|
|
367
|
+
* Console-side HITL approve/reject for the cross-thread approvals inbox. Optional — see
|
|
368
|
+
* {@link import('./spi/approval-port.js').AgentApprovalPort}.
|
|
369
|
+
*/
|
|
370
|
+
declare const AGENT_APPROVAL_PORT: unique symbol;
|
|
360
371
|
|
|
361
372
|
/**
|
|
362
373
|
* Per-invocation context handed to a tool handler. Host-supplied bits are optional. Identity lives
|
|
@@ -578,6 +589,8 @@ interface RecordRunStartInput {
|
|
|
578
589
|
threadId: string;
|
|
579
590
|
actorRef: string;
|
|
580
591
|
agentName?: string;
|
|
592
|
+
/** sha256 hex of the run's resolved (pre-RAG) system prompt — identifies the prompt VERSION. */
|
|
593
|
+
promptHash?: string;
|
|
581
594
|
}
|
|
582
595
|
interface RecordRunEndInput {
|
|
583
596
|
runId: string;
|
|
@@ -921,6 +934,31 @@ interface RecentRunRow {
|
|
|
921
934
|
retries: number;
|
|
922
935
|
/** ISO timestamp. */
|
|
923
936
|
startedAt: string;
|
|
937
|
+
/** sha256 hex of the run's resolved (pre-RAG) system prompt; `null` for a run recorded before this shipped. */
|
|
938
|
+
promptHash: string | null;
|
|
939
|
+
}
|
|
940
|
+
/** One tool call awaiting a HITL decision, for the cross-thread approvals inbox. */
|
|
941
|
+
interface PendingApprovalRow {
|
|
942
|
+
toolCallId: string;
|
|
943
|
+
toolName: string;
|
|
944
|
+
input: unknown;
|
|
945
|
+
threadId: string;
|
|
946
|
+
threadTitle: string;
|
|
947
|
+
/** Who asked — the run's actor. */
|
|
948
|
+
actorRef: string;
|
|
949
|
+
agentName: string | null;
|
|
950
|
+
/** ISO timestamp. */
|
|
951
|
+
requestedAt: string;
|
|
952
|
+
}
|
|
953
|
+
/** Governance rollup for one tool over a range. */
|
|
954
|
+
interface ToolStatRow {
|
|
955
|
+
toolName: string;
|
|
956
|
+
toolType: string;
|
|
957
|
+
calls: number;
|
|
958
|
+
failed: number;
|
|
959
|
+
rejected: number;
|
|
960
|
+
/** p95 of executionMs across executed calls; null when none carry it. */
|
|
961
|
+
p95ExecutionMs: number | null;
|
|
924
962
|
}
|
|
925
963
|
/**
|
|
926
964
|
* The governance read-model. Cost is `inputTokens/1e6 * inputPricePer1m + outputTokens/1e6 *
|
|
@@ -940,6 +978,10 @@ interface AgentGovernanceQueries {
|
|
|
940
978
|
runErrors(range: GovernanceRange): Promise<RunErrorBreakdownRow[]>;
|
|
941
979
|
runTrend(range: GovernanceRange): Promise<RunTrendPoint[]>;
|
|
942
980
|
recentRuns(limit: number): Promise<RecentRunRow[]>;
|
|
981
|
+
/** Tool calls sitting `pending_approval`, oldest first — an inbox drains from the back. Capped at `limit`. */
|
|
982
|
+
pendingApprovals(limit: number): Promise<PendingApprovalRow[]>;
|
|
983
|
+
/** Per-tool call/failure/rejection/latency rollup over the range, highest call count first. */
|
|
984
|
+
toolStats(range: GovernanceRange): Promise<ToolStatRow[]>;
|
|
943
985
|
}
|
|
944
986
|
|
|
945
987
|
/**
|
|
@@ -981,6 +1023,21 @@ interface AttachmentStagingStore {
|
|
|
981
1023
|
stage(input: StageAttachmentInput): Promise<MessageAttachment>;
|
|
982
1024
|
}
|
|
983
1025
|
|
|
1026
|
+
/**
|
|
1027
|
+
* Console-side HITL decisions. Implemented by the agent runtime (the nestjs package binds it to
|
|
1028
|
+
* the same signal path chat approvals use); the dashboard injects it OPTIONALLY — absent = the
|
|
1029
|
+
* approvals inbox renders read-only.
|
|
1030
|
+
*/
|
|
1031
|
+
interface AgentApprovalPort {
|
|
1032
|
+
approve(toolCallId: string, opts?: {
|
|
1033
|
+
executedByRef?: string;
|
|
1034
|
+
}): Promise<void>;
|
|
1035
|
+
reject(toolCallId: string, opts?: {
|
|
1036
|
+
executedByRef?: string;
|
|
1037
|
+
reason?: string;
|
|
1038
|
+
}): Promise<void>;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
984
1041
|
/**
|
|
985
1042
|
* The pure aggregation core shared by every {@link import('../spi/governance-queries.js').AgentGovernanceQueries}
|
|
986
1043
|
* adapter (MikroORM, Drizzle, in-memory). An adapter's only job is the DB-specific row fetch; the
|
|
@@ -1201,6 +1258,22 @@ declare class QuotaExceededError extends Error {
|
|
|
1201
1258
|
}
|
|
1202
1259
|
/** Reject if `work` doesn't settle within `ms`. The underlying work is left to finish on its own. */
|
|
1203
1260
|
declare function withToolTimeout<T>(work: Promise<T>, ms: number, toolName: string): Promise<T>;
|
|
1261
|
+
/**
|
|
1262
|
+
* Span-wrap one model call (`aviary:agent:llm.turn`). Exported so the durable dispatched-step
|
|
1263
|
+
* handler (which executes the genuine remote llm step) can emit the same span — core cannot wrap
|
|
1264
|
+
* `hooks.dispatchLlm` itself, because that call also runs (from cache) on replay.
|
|
1265
|
+
*/
|
|
1266
|
+
declare function traceLlmTurn(runId: string, step: number, run: () => Promise<ModelTurnResult>): Promise<ModelTurnResult>;
|
|
1267
|
+
/**
|
|
1268
|
+
* Span-wrap one tool invocation (`aviary:agent:tool.execution`). The tool's raw output never
|
|
1269
|
+
* rides the span (only the start payload's name/type metadata + duration). Exported for the
|
|
1270
|
+
* durable dispatched-step handler, like {@link traceLlmTurn}.
|
|
1271
|
+
*/
|
|
1272
|
+
declare function traceToolExecution<T>(runId: string, call: {
|
|
1273
|
+
toolCallId: string;
|
|
1274
|
+
toolName: string;
|
|
1275
|
+
toolType: 'read' | 'action';
|
|
1276
|
+
}, run: () => Promise<T>): Promise<T>;
|
|
1204
1277
|
/**
|
|
1205
1278
|
* The provider-agnostic agent turn, reused by both the inline and durable runners.
|
|
1206
1279
|
* It drives the model→tools→model iteration; the runner supplies the `step`/`awaitApproval`
|
|
@@ -1260,7 +1333,35 @@ interface AgentRetrieved {
|
|
|
1260
1333
|
/** How many passages the retriever returned. */
|
|
1261
1334
|
count: number;
|
|
1262
1335
|
}
|
|
1263
|
-
/**
|
|
1336
|
+
/** START payload of an `aviary:agent:llm.turn:*` span — one model call within a run. */
|
|
1337
|
+
interface AgentLlmTurnSpan {
|
|
1338
|
+
runId: string;
|
|
1339
|
+
/** Zero-based model-call index within the run (the loop's step counter). */
|
|
1340
|
+
step: number;
|
|
1341
|
+
}
|
|
1342
|
+
/** START payload of an `aviary:agent:tool.execution:*` span — one tool invocation. */
|
|
1343
|
+
interface AgentToolExecutionSpan {
|
|
1344
|
+
runId: string;
|
|
1345
|
+
toolCallId: string;
|
|
1346
|
+
toolName: string;
|
|
1347
|
+
toolType: 'read' | 'action';
|
|
1348
|
+
}
|
|
1349
|
+
/** START payload of an `aviary:agent:retrieval:*` span — inject-mode RAG retrieval. */
|
|
1350
|
+
interface AgentRetrievalSpan {
|
|
1351
|
+
runId: string;
|
|
1352
|
+
/** Length of the retrieval query in characters — never the query text itself. */
|
|
1353
|
+
queryLength: number;
|
|
1354
|
+
topK: number;
|
|
1355
|
+
}
|
|
1356
|
+
/** START payload of an `aviary:agent:follow-ups:*` span — the extra follow-up-suggestions call. */
|
|
1357
|
+
interface AgentFollowUpsSpan {
|
|
1358
|
+
runId: string;
|
|
1359
|
+
/** Zero-based model-call index of the final turn the follow-ups ride on. */
|
|
1360
|
+
step: number;
|
|
1361
|
+
/** How many follow-up questions were requested. */
|
|
1362
|
+
count: number;
|
|
1363
|
+
}
|
|
1364
|
+
/** Declaration-merge so `emit('agent', ...)`, `trace('agent', ...)` and telescope infer the agent payloads. */
|
|
1264
1365
|
declare module '@dudousxd/nestjs-diagnostics' {
|
|
1265
1366
|
interface ChannelRegistry {
|
|
1266
1367
|
agent: {
|
|
@@ -1272,6 +1373,10 @@ declare module '@dudousxd/nestjs-diagnostics' {
|
|
|
1272
1373
|
'run.failed': AgentRunFailed;
|
|
1273
1374
|
delegated: AgentDelegated;
|
|
1274
1375
|
retrieved: AgentRetrieved;
|
|
1376
|
+
'llm.turn': AgentLlmTurnSpan;
|
|
1377
|
+
'tool.execution': AgentToolExecutionSpan;
|
|
1378
|
+
retrieval: AgentRetrievalSpan;
|
|
1379
|
+
'follow-ups': AgentFollowUpsSpan;
|
|
1275
1380
|
};
|
|
1276
1381
|
}
|
|
1277
1382
|
}
|
|
@@ -1283,5 +1388,39 @@ declare function publishAgentRunFinished(payload: AgentRunFinished): void;
|
|
|
1283
1388
|
declare function publishAgentRunFailed(payload: AgentRunFailed): void;
|
|
1284
1389
|
declare function publishAgentDelegated(payload: AgentDelegated): void;
|
|
1285
1390
|
declare function publishAgentRetrieved(payload: AgentRetrieved): void;
|
|
1391
|
+
/**
|
|
1392
|
+
* Events published ONLY as spans — via `trace('agent', ...)` on the five `:start`/`:end`/
|
|
1393
|
+
* `:asyncStart`/`:asyncEnd`/`:error` sub-channels — never as point events on the base channel.
|
|
1394
|
+
* They are deliberately NOT in {@link AGENT_DIAGNOSTIC_EVENTS}: the point-event watcher has
|
|
1395
|
+
* nothing to subscribe to on their base channels, and claiming their keys would be meaningless
|
|
1396
|
+
* (the generic bridge only records point traffic).
|
|
1397
|
+
*/
|
|
1398
|
+
type AgentSpanEvent = 'llm.turn' | 'tool.execution' | 'retrieval' | 'follow-ups';
|
|
1399
|
+
/** All span-only events, in a stable order — for a future span recorder to derive sub-channels from. */
|
|
1400
|
+
declare const AGENT_SPAN_EVENTS: readonly AgentSpanEvent[];
|
|
1401
|
+
/** Every POINT event key declared on `ChannelRegistry['agent']` above — derived, not hand-copied. */
|
|
1402
|
+
type AgentDiagnosticEvent = Exclude<keyof ChannelRegistry['agent'], AgentSpanEvent>;
|
|
1403
|
+
/**
|
|
1404
|
+
* All 8 point events on `ChannelRegistry['agent']`, in a stable order — handy for wiring
|
|
1405
|
+
* subscribers (mirrors nestjs-media's `MEDIA_DIAGNOSTIC_EVENTS`). Span-only events (see
|
|
1406
|
+
* {@link AgentSpanEvent}) are excluded. A drift between this list and the registry is a compile
|
|
1407
|
+
* error in both directions: an extra/misspelled entry fails this array's own
|
|
1408
|
+
* `readonly AgentDiagnosticEvent[]` annotation immediately; a missing entry fails the
|
|
1409
|
+
* {@link AgentDiagnosticEventsCoverAllKeys} check below.
|
|
1410
|
+
*/
|
|
1411
|
+
declare const AGENT_DIAGNOSTIC_EVENTS: readonly AgentDiagnosticEvent[];
|
|
1412
|
+
/**
|
|
1413
|
+
* The telescope key for an agent diagnostics channel — `agent:<event>`. This is the key the
|
|
1414
|
+
* `@dudousxd/nestjs-diagnostics-telescope` generic bridge matches its `exclude` option against,
|
|
1415
|
+
* and the label its "Busiest events" panel shows. Distinct from the `aviary:agent:<event>` channel
|
|
1416
|
+
* name used on the wire. Mirrors `mediaDiagnosticKey`.
|
|
1417
|
+
*/
|
|
1418
|
+
type AgentDiagnosticKey = `agent:${AgentDiagnosticEvent}`;
|
|
1419
|
+
/**
|
|
1420
|
+
* Compose the telescope key for an agent event, typed against {@link AgentDiagnosticEvent} so a
|
|
1421
|
+
* misspelled event is a compile error. Feed the result to `nestjsDiagnosticsTelescope({ exclude:
|
|
1422
|
+
* [...] })` to mute a noisy channel, e.g. `agentDiagnosticKey('message')`.
|
|
1423
|
+
*/
|
|
1424
|
+
declare function agentDiagnosticKey(event: AgentDiagnosticEvent): AgentDiagnosticKey;
|
|
1286
1425
|
|
|
1287
|
-
export { AGENT_ACTOR_DIRECTORY, AGENT_ACTOR_RESOLVER, AGENT_ATTACHMENT_STAGING, AGENT_DEPS_FACTORY, AGENT_DURABLE_RUNNER, AGENT_EMBEDDING_PROVIDER, AGENT_GOVERNANCE_QUERIES, AGENT_MODEL, AGENT_OPTIONS, AGENT_PRICING_STORE, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_RETRIEVER, AGENT_ROLES_POLICY, AGENT_RUNNER, AGENT_SINK, AGENT_STORE, AGENT_TOOL_REGISTRY, type Actor, type ActorDirectory, type ActorResolver, type ActorSpendRow, type AgentCatalogEntry, type AgentDefinition, type AgentDelegated, type AgentGovernanceQueries, type AgentLoopDeps, type AgentLoopHooks, type AgentMessageEvent, type AgentPricingStore, type AgentQuotaExceeded, AgentRegistry, type AgentRetrieved, type AgentRunFailed, type AgentRunFinished, type AgentRunInput, type AgentRunStarted, type AgentRunner, type AgentStore, AgentStreamError, type AgentStreamEvent, type AgentToolCallEvent, type AiToolCtx, type AppendMessageInput, type AttachmentStagingStore, type CostUsage, type CreateThreadInput, type CurrentModelPrice, type Decision, DefaultRolesPolicy, type EmbeddingProvider, type GovernanceRange, type GovernanceUsageInput, type LlmStepEnvelope, type MessageAttachment, type MessageRole, type MessageUsage, type ModelMessage, type ModelPrice, type ModelPriceInput, type ModelProvider, type ModelSpendRow, type ModelTurnArgs, type ModelTurnResult, type PageContext, type Passage, type PromptBuilder, type PromptContext, type PromptContributor, QuotaExceededError, type QuotaState, type QuotaStore, type QuotaView, type RecentRunRow, type RecordRunEndInput, type RecordRunStartInput, type RecordToolCallInput, type RecordUsageInput, type RerankOptions, type Reranker, type RetrieveOptions, type Retriever, type RolesPolicy, type RunAgentBreakdownRow, type RunErrorBreakdownRow, type RunMetrics, type RunTrendPoint, type SinkWriter, type StageAttachmentInput, type StoredMessage, type StreamError, type ThreadActivityRow, type ThreadDetail, type ThreadMeta, type ThreadSpendRow, type ThreadSummary, type TokenStreamSink, type ToolCallActivityRow, type ToolCallRequest, type ToolCallStatus, type ToolDefinition, ToolForbiddenError, type ToolHandler, ToolInputInvalidError, type ToolKind, ToolNotFoundError, ToolRegistry, type ToolResult, type ToolSpec, type ToolStepCtx, type ToolStepEnvelope, type UpdateThreadInput, type UpdateToolCallInput, type UsagePurpose, type UsageTrendPoint, bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, dayBoundsUtc, encodeStreamEvent, estimateCost, filterToolsByAllowList, filterToolsByRole, publishAgentDelegated, publishAgentMessage, publishAgentQuotaExceeded, publishAgentRetrieved, publishAgentRunFailed, publishAgentRunFinished, publishAgentRunStarted, publishAgentToolCall, runAgentLoop, seedModelPrices, withToolTimeout };
|
|
1426
|
+
export { AGENT_ACTOR_DIRECTORY, AGENT_ACTOR_RESOLVER, AGENT_APPROVAL_PORT, AGENT_ATTACHMENT_STAGING, AGENT_DEPS_FACTORY, AGENT_DIAGNOSTIC_EVENTS, AGENT_DURABLE_RUNNER, AGENT_EMBEDDING_PROVIDER, AGENT_GOVERNANCE_QUERIES, AGENT_MODEL, AGENT_OPTIONS, AGENT_PRICING_STORE, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_RETRIEVER, AGENT_ROLES_POLICY, AGENT_RUNNER, AGENT_SINK, AGENT_SPAN_EVENTS, AGENT_STORE, AGENT_TOOL_REGISTRY, type Actor, type ActorDirectory, type ActorResolver, type ActorSpendRow, type AgentApprovalPort, type AgentCatalogEntry, type AgentDefinition, type AgentDelegated, type AgentDiagnosticEvent, type AgentDiagnosticKey, type AgentFollowUpsSpan, type AgentGovernanceQueries, type AgentLlmTurnSpan, type AgentLoopDeps, type AgentLoopHooks, type AgentMessageEvent, type AgentPricingStore, type AgentQuotaExceeded, AgentRegistry, type AgentRetrievalSpan, type AgentRetrieved, type AgentRunFailed, type AgentRunFinished, type AgentRunInput, type AgentRunStarted, type AgentRunner, type AgentSpanEvent, type AgentStore, AgentStreamError, type AgentStreamEvent, type AgentToolCallEvent, type AgentToolExecutionSpan, type AiToolCtx, type AppendMessageInput, type AttachmentStagingStore, type CostUsage, type CreateThreadInput, type CurrentModelPrice, type Decision, DefaultRolesPolicy, type EmbeddingProvider, type GovernanceRange, type GovernanceUsageInput, type LlmStepEnvelope, type MessageAttachment, type MessageRole, type MessageUsage, type ModelMessage, type ModelPrice, type ModelPriceInput, type ModelProvider, type ModelSpendRow, type ModelTurnArgs, type ModelTurnResult, type PageContext, type Passage, type PendingApprovalRow, type PromptBuilder, type PromptContext, type PromptContributor, QuotaExceededError, type QuotaState, type QuotaStore, type QuotaView, type RecentRunRow, type RecordRunEndInput, type RecordRunStartInput, type RecordToolCallInput, type RecordUsageInput, type RerankOptions, type Reranker, type RetrieveOptions, type Retriever, type RolesPolicy, type RunAgentBreakdownRow, type RunErrorBreakdownRow, type RunMetrics, type RunTrendPoint, type SinkWriter, type StageAttachmentInput, type StoredMessage, type StreamError, type ThreadActivityRow, type ThreadDetail, type ThreadMeta, type ThreadSpendRow, type ThreadSummary, type TokenStreamSink, type ToolCallActivityRow, type ToolCallRequest, type ToolCallStatus, type ToolDefinition, ToolForbiddenError, type ToolHandler, ToolInputInvalidError, type ToolKind, ToolNotFoundError, ToolRegistry, type ToolResult, type ToolSpec, type ToolStatRow, type ToolStepCtx, type ToolStepEnvelope, type UpdateThreadInput, type UpdateToolCallInput, type UsagePurpose, type UsageTrendPoint, agentDiagnosticKey, bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, dayBoundsUtc, encodeStreamEvent, estimateCost, filterToolsByAllowList, filterToolsByRole, publishAgentDelegated, publishAgentMessage, publishAgentQuotaExceeded, publishAgentRetrieved, publishAgentRunFailed, publishAgentRunFinished, publishAgentRunStarted, publishAgentToolCall, runAgentLoop, seedModelPrices, traceLlmTurn, traceToolExecution, withToolTimeout };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { ChannelRegistry } from '@dudousxd/nestjs-diagnostics';
|
|
2
3
|
|
|
3
4
|
/** Who is driving the turn. Roles + tenant come from the host app (nestjs-context/authz). */
|
|
4
5
|
interface Actor {
|
|
@@ -119,6 +120,11 @@ interface QuotaView {
|
|
|
119
120
|
interface Decision {
|
|
120
121
|
approved: boolean;
|
|
121
122
|
reason?: string;
|
|
123
|
+
/**
|
|
124
|
+
* Opaque ref of WHO decided (e.g. a console admin). When absent, the run's own actor decided
|
|
125
|
+
* (the chat flow).
|
|
126
|
+
*/
|
|
127
|
+
executedByRef?: string;
|
|
122
128
|
}
|
|
123
129
|
type MessageRole = 'user' | 'assistant' | 'system';
|
|
124
130
|
/**
|
|
@@ -357,6 +363,11 @@ declare const AGENT_ACTOR_DIRECTORY: unique symbol;
|
|
|
357
363
|
* {@link import('./spi/attachment-staging.js').AttachmentStagingStore}.
|
|
358
364
|
*/
|
|
359
365
|
declare const AGENT_ATTACHMENT_STAGING: unique symbol;
|
|
366
|
+
/**
|
|
367
|
+
* Console-side HITL approve/reject for the cross-thread approvals inbox. Optional — see
|
|
368
|
+
* {@link import('./spi/approval-port.js').AgentApprovalPort}.
|
|
369
|
+
*/
|
|
370
|
+
declare const AGENT_APPROVAL_PORT: unique symbol;
|
|
360
371
|
|
|
361
372
|
/**
|
|
362
373
|
* Per-invocation context handed to a tool handler. Host-supplied bits are optional. Identity lives
|
|
@@ -578,6 +589,8 @@ interface RecordRunStartInput {
|
|
|
578
589
|
threadId: string;
|
|
579
590
|
actorRef: string;
|
|
580
591
|
agentName?: string;
|
|
592
|
+
/** sha256 hex of the run's resolved (pre-RAG) system prompt — identifies the prompt VERSION. */
|
|
593
|
+
promptHash?: string;
|
|
581
594
|
}
|
|
582
595
|
interface RecordRunEndInput {
|
|
583
596
|
runId: string;
|
|
@@ -921,6 +934,31 @@ interface RecentRunRow {
|
|
|
921
934
|
retries: number;
|
|
922
935
|
/** ISO timestamp. */
|
|
923
936
|
startedAt: string;
|
|
937
|
+
/** sha256 hex of the run's resolved (pre-RAG) system prompt; `null` for a run recorded before this shipped. */
|
|
938
|
+
promptHash: string | null;
|
|
939
|
+
}
|
|
940
|
+
/** One tool call awaiting a HITL decision, for the cross-thread approvals inbox. */
|
|
941
|
+
interface PendingApprovalRow {
|
|
942
|
+
toolCallId: string;
|
|
943
|
+
toolName: string;
|
|
944
|
+
input: unknown;
|
|
945
|
+
threadId: string;
|
|
946
|
+
threadTitle: string;
|
|
947
|
+
/** Who asked — the run's actor. */
|
|
948
|
+
actorRef: string;
|
|
949
|
+
agentName: string | null;
|
|
950
|
+
/** ISO timestamp. */
|
|
951
|
+
requestedAt: string;
|
|
952
|
+
}
|
|
953
|
+
/** Governance rollup for one tool over a range. */
|
|
954
|
+
interface ToolStatRow {
|
|
955
|
+
toolName: string;
|
|
956
|
+
toolType: string;
|
|
957
|
+
calls: number;
|
|
958
|
+
failed: number;
|
|
959
|
+
rejected: number;
|
|
960
|
+
/** p95 of executionMs across executed calls; null when none carry it. */
|
|
961
|
+
p95ExecutionMs: number | null;
|
|
924
962
|
}
|
|
925
963
|
/**
|
|
926
964
|
* The governance read-model. Cost is `inputTokens/1e6 * inputPricePer1m + outputTokens/1e6 *
|
|
@@ -940,6 +978,10 @@ interface AgentGovernanceQueries {
|
|
|
940
978
|
runErrors(range: GovernanceRange): Promise<RunErrorBreakdownRow[]>;
|
|
941
979
|
runTrend(range: GovernanceRange): Promise<RunTrendPoint[]>;
|
|
942
980
|
recentRuns(limit: number): Promise<RecentRunRow[]>;
|
|
981
|
+
/** Tool calls sitting `pending_approval`, oldest first — an inbox drains from the back. Capped at `limit`. */
|
|
982
|
+
pendingApprovals(limit: number): Promise<PendingApprovalRow[]>;
|
|
983
|
+
/** Per-tool call/failure/rejection/latency rollup over the range, highest call count first. */
|
|
984
|
+
toolStats(range: GovernanceRange): Promise<ToolStatRow[]>;
|
|
943
985
|
}
|
|
944
986
|
|
|
945
987
|
/**
|
|
@@ -981,6 +1023,21 @@ interface AttachmentStagingStore {
|
|
|
981
1023
|
stage(input: StageAttachmentInput): Promise<MessageAttachment>;
|
|
982
1024
|
}
|
|
983
1025
|
|
|
1026
|
+
/**
|
|
1027
|
+
* Console-side HITL decisions. Implemented by the agent runtime (the nestjs package binds it to
|
|
1028
|
+
* the same signal path chat approvals use); the dashboard injects it OPTIONALLY — absent = the
|
|
1029
|
+
* approvals inbox renders read-only.
|
|
1030
|
+
*/
|
|
1031
|
+
interface AgentApprovalPort {
|
|
1032
|
+
approve(toolCallId: string, opts?: {
|
|
1033
|
+
executedByRef?: string;
|
|
1034
|
+
}): Promise<void>;
|
|
1035
|
+
reject(toolCallId: string, opts?: {
|
|
1036
|
+
executedByRef?: string;
|
|
1037
|
+
reason?: string;
|
|
1038
|
+
}): Promise<void>;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
984
1041
|
/**
|
|
985
1042
|
* The pure aggregation core shared by every {@link import('../spi/governance-queries.js').AgentGovernanceQueries}
|
|
986
1043
|
* adapter (MikroORM, Drizzle, in-memory). An adapter's only job is the DB-specific row fetch; the
|
|
@@ -1201,6 +1258,22 @@ declare class QuotaExceededError extends Error {
|
|
|
1201
1258
|
}
|
|
1202
1259
|
/** Reject if `work` doesn't settle within `ms`. The underlying work is left to finish on its own. */
|
|
1203
1260
|
declare function withToolTimeout<T>(work: Promise<T>, ms: number, toolName: string): Promise<T>;
|
|
1261
|
+
/**
|
|
1262
|
+
* Span-wrap one model call (`aviary:agent:llm.turn`). Exported so the durable dispatched-step
|
|
1263
|
+
* handler (which executes the genuine remote llm step) can emit the same span — core cannot wrap
|
|
1264
|
+
* `hooks.dispatchLlm` itself, because that call also runs (from cache) on replay.
|
|
1265
|
+
*/
|
|
1266
|
+
declare function traceLlmTurn(runId: string, step: number, run: () => Promise<ModelTurnResult>): Promise<ModelTurnResult>;
|
|
1267
|
+
/**
|
|
1268
|
+
* Span-wrap one tool invocation (`aviary:agent:tool.execution`). The tool's raw output never
|
|
1269
|
+
* rides the span (only the start payload's name/type metadata + duration). Exported for the
|
|
1270
|
+
* durable dispatched-step handler, like {@link traceLlmTurn}.
|
|
1271
|
+
*/
|
|
1272
|
+
declare function traceToolExecution<T>(runId: string, call: {
|
|
1273
|
+
toolCallId: string;
|
|
1274
|
+
toolName: string;
|
|
1275
|
+
toolType: 'read' | 'action';
|
|
1276
|
+
}, run: () => Promise<T>): Promise<T>;
|
|
1204
1277
|
/**
|
|
1205
1278
|
* The provider-agnostic agent turn, reused by both the inline and durable runners.
|
|
1206
1279
|
* It drives the model→tools→model iteration; the runner supplies the `step`/`awaitApproval`
|
|
@@ -1260,7 +1333,35 @@ interface AgentRetrieved {
|
|
|
1260
1333
|
/** How many passages the retriever returned. */
|
|
1261
1334
|
count: number;
|
|
1262
1335
|
}
|
|
1263
|
-
/**
|
|
1336
|
+
/** START payload of an `aviary:agent:llm.turn:*` span — one model call within a run. */
|
|
1337
|
+
interface AgentLlmTurnSpan {
|
|
1338
|
+
runId: string;
|
|
1339
|
+
/** Zero-based model-call index within the run (the loop's step counter). */
|
|
1340
|
+
step: number;
|
|
1341
|
+
}
|
|
1342
|
+
/** START payload of an `aviary:agent:tool.execution:*` span — one tool invocation. */
|
|
1343
|
+
interface AgentToolExecutionSpan {
|
|
1344
|
+
runId: string;
|
|
1345
|
+
toolCallId: string;
|
|
1346
|
+
toolName: string;
|
|
1347
|
+
toolType: 'read' | 'action';
|
|
1348
|
+
}
|
|
1349
|
+
/** START payload of an `aviary:agent:retrieval:*` span — inject-mode RAG retrieval. */
|
|
1350
|
+
interface AgentRetrievalSpan {
|
|
1351
|
+
runId: string;
|
|
1352
|
+
/** Length of the retrieval query in characters — never the query text itself. */
|
|
1353
|
+
queryLength: number;
|
|
1354
|
+
topK: number;
|
|
1355
|
+
}
|
|
1356
|
+
/** START payload of an `aviary:agent:follow-ups:*` span — the extra follow-up-suggestions call. */
|
|
1357
|
+
interface AgentFollowUpsSpan {
|
|
1358
|
+
runId: string;
|
|
1359
|
+
/** Zero-based model-call index of the final turn the follow-ups ride on. */
|
|
1360
|
+
step: number;
|
|
1361
|
+
/** How many follow-up questions were requested. */
|
|
1362
|
+
count: number;
|
|
1363
|
+
}
|
|
1364
|
+
/** Declaration-merge so `emit('agent', ...)`, `trace('agent', ...)` and telescope infer the agent payloads. */
|
|
1264
1365
|
declare module '@dudousxd/nestjs-diagnostics' {
|
|
1265
1366
|
interface ChannelRegistry {
|
|
1266
1367
|
agent: {
|
|
@@ -1272,6 +1373,10 @@ declare module '@dudousxd/nestjs-diagnostics' {
|
|
|
1272
1373
|
'run.failed': AgentRunFailed;
|
|
1273
1374
|
delegated: AgentDelegated;
|
|
1274
1375
|
retrieved: AgentRetrieved;
|
|
1376
|
+
'llm.turn': AgentLlmTurnSpan;
|
|
1377
|
+
'tool.execution': AgentToolExecutionSpan;
|
|
1378
|
+
retrieval: AgentRetrievalSpan;
|
|
1379
|
+
'follow-ups': AgentFollowUpsSpan;
|
|
1275
1380
|
};
|
|
1276
1381
|
}
|
|
1277
1382
|
}
|
|
@@ -1283,5 +1388,39 @@ declare function publishAgentRunFinished(payload: AgentRunFinished): void;
|
|
|
1283
1388
|
declare function publishAgentRunFailed(payload: AgentRunFailed): void;
|
|
1284
1389
|
declare function publishAgentDelegated(payload: AgentDelegated): void;
|
|
1285
1390
|
declare function publishAgentRetrieved(payload: AgentRetrieved): void;
|
|
1391
|
+
/**
|
|
1392
|
+
* Events published ONLY as spans — via `trace('agent', ...)` on the five `:start`/`:end`/
|
|
1393
|
+
* `:asyncStart`/`:asyncEnd`/`:error` sub-channels — never as point events on the base channel.
|
|
1394
|
+
* They are deliberately NOT in {@link AGENT_DIAGNOSTIC_EVENTS}: the point-event watcher has
|
|
1395
|
+
* nothing to subscribe to on their base channels, and claiming their keys would be meaningless
|
|
1396
|
+
* (the generic bridge only records point traffic).
|
|
1397
|
+
*/
|
|
1398
|
+
type AgentSpanEvent = 'llm.turn' | 'tool.execution' | 'retrieval' | 'follow-ups';
|
|
1399
|
+
/** All span-only events, in a stable order — for a future span recorder to derive sub-channels from. */
|
|
1400
|
+
declare const AGENT_SPAN_EVENTS: readonly AgentSpanEvent[];
|
|
1401
|
+
/** Every POINT event key declared on `ChannelRegistry['agent']` above — derived, not hand-copied. */
|
|
1402
|
+
type AgentDiagnosticEvent = Exclude<keyof ChannelRegistry['agent'], AgentSpanEvent>;
|
|
1403
|
+
/**
|
|
1404
|
+
* All 8 point events on `ChannelRegistry['agent']`, in a stable order — handy for wiring
|
|
1405
|
+
* subscribers (mirrors nestjs-media's `MEDIA_DIAGNOSTIC_EVENTS`). Span-only events (see
|
|
1406
|
+
* {@link AgentSpanEvent}) are excluded. A drift between this list and the registry is a compile
|
|
1407
|
+
* error in both directions: an extra/misspelled entry fails this array's own
|
|
1408
|
+
* `readonly AgentDiagnosticEvent[]` annotation immediately; a missing entry fails the
|
|
1409
|
+
* {@link AgentDiagnosticEventsCoverAllKeys} check below.
|
|
1410
|
+
*/
|
|
1411
|
+
declare const AGENT_DIAGNOSTIC_EVENTS: readonly AgentDiagnosticEvent[];
|
|
1412
|
+
/**
|
|
1413
|
+
* The telescope key for an agent diagnostics channel — `agent:<event>`. This is the key the
|
|
1414
|
+
* `@dudousxd/nestjs-diagnostics-telescope` generic bridge matches its `exclude` option against,
|
|
1415
|
+
* and the label its "Busiest events" panel shows. Distinct from the `aviary:agent:<event>` channel
|
|
1416
|
+
* name used on the wire. Mirrors `mediaDiagnosticKey`.
|
|
1417
|
+
*/
|
|
1418
|
+
type AgentDiagnosticKey = `agent:${AgentDiagnosticEvent}`;
|
|
1419
|
+
/**
|
|
1420
|
+
* Compose the telescope key for an agent event, typed against {@link AgentDiagnosticEvent} so a
|
|
1421
|
+
* misspelled event is a compile error. Feed the result to `nestjsDiagnosticsTelescope({ exclude:
|
|
1422
|
+
* [...] })` to mute a noisy channel, e.g. `agentDiagnosticKey('message')`.
|
|
1423
|
+
*/
|
|
1424
|
+
declare function agentDiagnosticKey(event: AgentDiagnosticEvent): AgentDiagnosticKey;
|
|
1286
1425
|
|
|
1287
|
-
export { AGENT_ACTOR_DIRECTORY, AGENT_ACTOR_RESOLVER, AGENT_ATTACHMENT_STAGING, AGENT_DEPS_FACTORY, AGENT_DURABLE_RUNNER, AGENT_EMBEDDING_PROVIDER, AGENT_GOVERNANCE_QUERIES, AGENT_MODEL, AGENT_OPTIONS, AGENT_PRICING_STORE, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_RETRIEVER, AGENT_ROLES_POLICY, AGENT_RUNNER, AGENT_SINK, AGENT_STORE, AGENT_TOOL_REGISTRY, type Actor, type ActorDirectory, type ActorResolver, type ActorSpendRow, type AgentCatalogEntry, type AgentDefinition, type AgentDelegated, type AgentGovernanceQueries, type AgentLoopDeps, type AgentLoopHooks, type AgentMessageEvent, type AgentPricingStore, type AgentQuotaExceeded, AgentRegistry, type AgentRetrieved, type AgentRunFailed, type AgentRunFinished, type AgentRunInput, type AgentRunStarted, type AgentRunner, type AgentStore, AgentStreamError, type AgentStreamEvent, type AgentToolCallEvent, type AiToolCtx, type AppendMessageInput, type AttachmentStagingStore, type CostUsage, type CreateThreadInput, type CurrentModelPrice, type Decision, DefaultRolesPolicy, type EmbeddingProvider, type GovernanceRange, type GovernanceUsageInput, type LlmStepEnvelope, type MessageAttachment, type MessageRole, type MessageUsage, type ModelMessage, type ModelPrice, type ModelPriceInput, type ModelProvider, type ModelSpendRow, type ModelTurnArgs, type ModelTurnResult, type PageContext, type Passage, type PromptBuilder, type PromptContext, type PromptContributor, QuotaExceededError, type QuotaState, type QuotaStore, type QuotaView, type RecentRunRow, type RecordRunEndInput, type RecordRunStartInput, type RecordToolCallInput, type RecordUsageInput, type RerankOptions, type Reranker, type RetrieveOptions, type Retriever, type RolesPolicy, type RunAgentBreakdownRow, type RunErrorBreakdownRow, type RunMetrics, type RunTrendPoint, type SinkWriter, type StageAttachmentInput, type StoredMessage, type StreamError, type ThreadActivityRow, type ThreadDetail, type ThreadMeta, type ThreadSpendRow, type ThreadSummary, type TokenStreamSink, type ToolCallActivityRow, type ToolCallRequest, type ToolCallStatus, type ToolDefinition, ToolForbiddenError, type ToolHandler, ToolInputInvalidError, type ToolKind, ToolNotFoundError, ToolRegistry, type ToolResult, type ToolSpec, type ToolStepCtx, type ToolStepEnvelope, type UpdateThreadInput, type UpdateToolCallInput, type UsagePurpose, type UsageTrendPoint, bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, dayBoundsUtc, encodeStreamEvent, estimateCost, filterToolsByAllowList, filterToolsByRole, publishAgentDelegated, publishAgentMessage, publishAgentQuotaExceeded, publishAgentRetrieved, publishAgentRunFailed, publishAgentRunFinished, publishAgentRunStarted, publishAgentToolCall, runAgentLoop, seedModelPrices, withToolTimeout };
|
|
1426
|
+
export { AGENT_ACTOR_DIRECTORY, AGENT_ACTOR_RESOLVER, AGENT_APPROVAL_PORT, AGENT_ATTACHMENT_STAGING, AGENT_DEPS_FACTORY, AGENT_DIAGNOSTIC_EVENTS, AGENT_DURABLE_RUNNER, AGENT_EMBEDDING_PROVIDER, AGENT_GOVERNANCE_QUERIES, AGENT_MODEL, AGENT_OPTIONS, AGENT_PRICING_STORE, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_RETRIEVER, AGENT_ROLES_POLICY, AGENT_RUNNER, AGENT_SINK, AGENT_SPAN_EVENTS, AGENT_STORE, AGENT_TOOL_REGISTRY, type Actor, type ActorDirectory, type ActorResolver, type ActorSpendRow, type AgentApprovalPort, type AgentCatalogEntry, type AgentDefinition, type AgentDelegated, type AgentDiagnosticEvent, type AgentDiagnosticKey, type AgentFollowUpsSpan, type AgentGovernanceQueries, type AgentLlmTurnSpan, type AgentLoopDeps, type AgentLoopHooks, type AgentMessageEvent, type AgentPricingStore, type AgentQuotaExceeded, AgentRegistry, type AgentRetrievalSpan, type AgentRetrieved, type AgentRunFailed, type AgentRunFinished, type AgentRunInput, type AgentRunStarted, type AgentRunner, type AgentSpanEvent, type AgentStore, AgentStreamError, type AgentStreamEvent, type AgentToolCallEvent, type AgentToolExecutionSpan, type AiToolCtx, type AppendMessageInput, type AttachmentStagingStore, type CostUsage, type CreateThreadInput, type CurrentModelPrice, type Decision, DefaultRolesPolicy, type EmbeddingProvider, type GovernanceRange, type GovernanceUsageInput, type LlmStepEnvelope, type MessageAttachment, type MessageRole, type MessageUsage, type ModelMessage, type ModelPrice, type ModelPriceInput, type ModelProvider, type ModelSpendRow, type ModelTurnArgs, type ModelTurnResult, type PageContext, type Passage, type PendingApprovalRow, type PromptBuilder, type PromptContext, type PromptContributor, QuotaExceededError, type QuotaState, type QuotaStore, type QuotaView, type RecentRunRow, type RecordRunEndInput, type RecordRunStartInput, type RecordToolCallInput, type RecordUsageInput, type RerankOptions, type Reranker, type RetrieveOptions, type Retriever, type RolesPolicy, type RunAgentBreakdownRow, type RunErrorBreakdownRow, type RunMetrics, type RunTrendPoint, type SinkWriter, type StageAttachmentInput, type StoredMessage, type StreamError, type ThreadActivityRow, type ThreadDetail, type ThreadMeta, type ThreadSpendRow, type ThreadSummary, type TokenStreamSink, type ToolCallActivityRow, type ToolCallRequest, type ToolCallStatus, type ToolDefinition, ToolForbiddenError, type ToolHandler, ToolInputInvalidError, type ToolKind, ToolNotFoundError, ToolRegistry, type ToolResult, type ToolSpec, type ToolStatRow, type ToolStepCtx, type ToolStepEnvelope, type UpdateThreadInput, type UpdateToolCallInput, type UsagePurpose, type UsageTrendPoint, agentDiagnosticKey, bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, dayBoundsUtc, encodeStreamEvent, estimateCost, filterToolsByAllowList, filterToolsByRole, publishAgentDelegated, publishAgentMessage, publishAgentQuotaExceeded, publishAgentRetrieved, publishAgentRunFailed, publishAgentRunFinished, publishAgentRunStarted, publishAgentToolCall, runAgentLoop, seedModelPrices, traceLlmTurn, traceToolExecution, withToolTimeout };
|