@nanobpm/agentic 0.4.0 → 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/README.md +1 -1
- package/dist/transcript/index.d.ts +2 -2
- package/dist/transcript/index.js +1 -1
- package/dist/transcript/schema.d.ts +23 -1
- package/dist/transcript/schema.js +34 -1
- package/dist/transcript/store.d.ts +93 -5
- package/dist/transcript/store.js +287 -6
- package/package.json +1 -1
- package/src/transcript/index.ts +8 -0
- package/src/transcript/schema.test.ts +31 -4
- package/src/transcript/schema.ts +36 -1
- package/src/transcript/store.ts +438 -6
- package/src/transcript/turns.test.ts +334 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ The **Nano agentic protocol** (ADR 0056): one app-tier channel carrying agent pr
|
|
|
13
13
|
| `@nanobpm/agentic/vocab` | Vocab resolver + core vocabulary (S3) |
|
|
14
14
|
| `@nanobpm/agentic/demand` | Demand×supply model (S4) |
|
|
15
15
|
| `@nanobpm/agentic/relay` | Relay ring + QoS scheduler (S5) |
|
|
16
|
-
| `@nanobpm/agentic/transcript` | Transcript store, retention-by-lifecycle (S6) |
|
|
16
|
+
| `@nanobpm/agentic/transcript` | Transcript store, retention-by-lifecycle (S6) + turn-structured view (Camunda `AgentHistoryRecordValue` parity) |
|
|
17
17
|
| `@nanobpm/agentic/blackboard` | Blackboard channel family (S7) |
|
|
18
18
|
| `@nanobpm/agentic/cockpit` | Operator visibility page — the cockpit (S8) |
|
|
19
19
|
| `@nanobpm/agentic/session` | Canonical `SessionEvent` + authoritative session log for durable agent-session resume (ADR 0062) |
|
|
@@ -14,5 +14,5 @@
|
|
|
14
14
|
* and kept in lockstep by a drift-guard test.
|
|
15
15
|
*/
|
|
16
16
|
export { TranscriptStore, TranscriptCorruptionError, TranscriptLifecycleError, systemClock } from "./store.ts";
|
|
17
|
-
export type { Clock, SqliteDb, TranscriptChunk, TranscriptLifecycle, TranscriptRing, TranscriptSlice, TranscriptStatus, TranscriptStoreOptions, TranscriptStream, } from "./store.ts";
|
|
18
|
-
export { TRANSCRIPT_CHUNK_TABLE, TRANSCRIPT_SCHEMA_SQL, TRANSCRIPT_STREAM_TABLE, } from "./schema.ts";
|
|
17
|
+
export type { Clock, SqliteDb, TranscriptChunk, TranscriptContentBlock, TranscriptContentType, TranscriptLifecycle, TranscriptRing, TranscriptSlice, TranscriptStatus, TranscriptStoreOptions, TranscriptStream, TranscriptToolCall, TranscriptTurn, TranscriptTurnMetrics, TranscriptTurnRole, } from "./store.ts";
|
|
18
|
+
export { TRANSCRIPT_CHUNK_TABLE, TRANSCRIPT_SCHEMA_SQL, TRANSCRIPT_STREAM_TABLE, TRANSCRIPT_TURN_SCHEMA_SQL, TRANSCRIPT_TURN_TABLE, } from "./schema.ts";
|
package/dist/transcript/index.js
CHANGED
|
@@ -14,4 +14,4 @@
|
|
|
14
14
|
* and kept in lockstep by a drift-guard test.
|
|
15
15
|
*/
|
|
16
16
|
export { TranscriptStore, TranscriptCorruptionError, TranscriptLifecycleError, systemClock } from "./store.js";
|
|
17
|
-
export { TRANSCRIPT_CHUNK_TABLE, TRANSCRIPT_SCHEMA_SQL, TRANSCRIPT_STREAM_TABLE, } from "./schema.js";
|
|
17
|
+
export { TRANSCRIPT_CHUNK_TABLE, TRANSCRIPT_SCHEMA_SQL, TRANSCRIPT_STREAM_TABLE, TRANSCRIPT_TURN_SCHEMA_SQL, TRANSCRIPT_TURN_TABLE, } from "./schema.js";
|
|
@@ -9,12 +9,15 @@
|
|
|
9
9
|
* statement-for-statement identical — divergence is a red test, not a silent
|
|
10
10
|
* production/boot mismatch.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
12
|
+
* Three tables back the store:
|
|
13
13
|
* - `agentic_transcript_stream` — one row per relay stream: its retention
|
|
14
14
|
* lifecycle (`ephemeral` vs `long-lived`), its status (`open`/`completed`),
|
|
15
15
|
* and the offset window (`first_offset` … `next_offset`) currently retained.
|
|
16
16
|
* - `agentic_transcript_chunk` — the durable chunks, keyed `(stream, chunk_offset)`
|
|
17
17
|
* so a flush/append is idempotent and reattach can slice from any offset.
|
|
18
|
+
* - `agentic_transcript_turn` — the additive turn-structured view (Camunda
|
|
19
|
+
* `AgentHistoryRecordValue` parity, issue #475), keyed `(stream, turn_sequence)`.
|
|
20
|
+
* It is layered over — never a replacement for — the raw chunk stream.
|
|
18
21
|
*
|
|
19
22
|
* `chunk_offset` (not `offset`) is deliberate: `OFFSET` is a SQLite keyword, so
|
|
20
23
|
* the column is named to avoid quoting it in every statement.
|
|
@@ -23,6 +26,8 @@
|
|
|
23
26
|
export declare const TRANSCRIPT_STREAM_TABLE = "agentic_transcript_stream";
|
|
24
27
|
/** The durable per-chunk table name. */
|
|
25
28
|
export declare const TRANSCRIPT_CHUNK_TABLE = "agentic_transcript_chunk";
|
|
29
|
+
/** The durable per-turn (structured-view) table name. */
|
|
30
|
+
export declare const TRANSCRIPT_TURN_TABLE = "agentic_transcript_turn";
|
|
26
31
|
/**
|
|
27
32
|
* The canonical transcript-store DDL. Forward-only and additive; every column
|
|
28
33
|
* added here must also be added to the boot migration (the drift guard enforces
|
|
@@ -30,3 +35,20 @@ export declare const TRANSCRIPT_CHUNK_TABLE = "agentic_transcript_chunk";
|
|
|
30
35
|
* it never rewrites a chunk.
|
|
31
36
|
*/
|
|
32
37
|
export declare const TRANSCRIPT_SCHEMA_SQL = "CREATE TABLE IF NOT EXISTS agentic_transcript_stream (\n stream TEXT PRIMARY KEY,\n lifecycle TEXT NOT NULL,\n status TEXT NOT NULL DEFAULT 'open',\n created_at TEXT NOT NULL,\n completed_at TEXT,\n first_offset INTEGER,\n next_offset INTEGER NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS agentic_transcript_chunk (\n stream TEXT NOT NULL,\n chunk_offset INTEGER NOT NULL,\n chunk TEXT NOT NULL,\n appended_at TEXT NOT NULL,\n PRIMARY KEY (stream, chunk_offset)\n);\nCREATE INDEX IF NOT EXISTS idx_agentic_transcript_stream_retention ON agentic_transcript_stream (lifecycle, status, completed_at);";
|
|
38
|
+
/**
|
|
39
|
+
* The turn-structured transcript DDL — the additive, Camunda-`AgentHistoryRecordValue`
|
|
40
|
+
* parity view layered over the raw chunk stream (issue #475). It ships as its own
|
|
41
|
+
* forward-only migration `db/migrations/008_agentic_transcript_turns.sql` (the raw
|
|
42
|
+
* chunk stream in {@link TRANSCRIPT_SCHEMA_SQL} is untouched — additive, no regression
|
|
43
|
+
* to existing readers), mirrored here as the single source of truth applied by
|
|
44
|
+
* {@link TranscriptStore.ensureSchema} and kept in lockstep by a drift-guard test.
|
|
45
|
+
*
|
|
46
|
+
* One row per structured turn, keyed `(stream, turn_sequence)` so an append/re-record
|
|
47
|
+
* is idempotent (exactly the `(stream, chunk_offset)` discipline of the chunk table).
|
|
48
|
+
* `turn_sequence` is the stream-local append order and idempotency key;
|
|
49
|
+
* `loop_iteration` is the agent-loop turn counter carried as data (Camunda allows
|
|
50
|
+
* several role-split records — e.g. ASSISTANT then TOOL_RESULT — within one iteration).
|
|
51
|
+
* `content`, `tool_calls` and `metrics` hold the typed content blocks, tool calls and
|
|
52
|
+
* per-turn metrics as JSON.
|
|
53
|
+
*/
|
|
54
|
+
export declare const TRANSCRIPT_TURN_SCHEMA_SQL = "CREATE TABLE IF NOT EXISTS agentic_transcript_turn (\n stream TEXT NOT NULL,\n turn_sequence INTEGER NOT NULL,\n loop_iteration INTEGER NOT NULL,\n role TEXT NOT NULL,\n content TEXT NOT NULL,\n tool_calls TEXT NOT NULL,\n metrics TEXT,\n produced_at INTEGER,\n recorded_at TEXT NOT NULL,\n PRIMARY KEY (stream, turn_sequence)\n);";
|
|
@@ -9,12 +9,15 @@
|
|
|
9
9
|
* statement-for-statement identical — divergence is a red test, not a silent
|
|
10
10
|
* production/boot mismatch.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
12
|
+
* Three tables back the store:
|
|
13
13
|
* - `agentic_transcript_stream` — one row per relay stream: its retention
|
|
14
14
|
* lifecycle (`ephemeral` vs `long-lived`), its status (`open`/`completed`),
|
|
15
15
|
* and the offset window (`first_offset` … `next_offset`) currently retained.
|
|
16
16
|
* - `agentic_transcript_chunk` — the durable chunks, keyed `(stream, chunk_offset)`
|
|
17
17
|
* so a flush/append is idempotent and reattach can slice from any offset.
|
|
18
|
+
* - `agentic_transcript_turn` — the additive turn-structured view (Camunda
|
|
19
|
+
* `AgentHistoryRecordValue` parity, issue #475), keyed `(stream, turn_sequence)`.
|
|
20
|
+
* It is layered over — never a replacement for — the raw chunk stream.
|
|
18
21
|
*
|
|
19
22
|
* `chunk_offset` (not `offset`) is deliberate: `OFFSET` is a SQLite keyword, so
|
|
20
23
|
* the column is named to avoid quoting it in every statement.
|
|
@@ -23,6 +26,8 @@
|
|
|
23
26
|
export const TRANSCRIPT_STREAM_TABLE = "agentic_transcript_stream";
|
|
24
27
|
/** The durable per-chunk table name. */
|
|
25
28
|
export const TRANSCRIPT_CHUNK_TABLE = "agentic_transcript_chunk";
|
|
29
|
+
/** The durable per-turn (structured-view) table name. */
|
|
30
|
+
export const TRANSCRIPT_TURN_TABLE = "agentic_transcript_turn";
|
|
26
31
|
/**
|
|
27
32
|
* The canonical transcript-store DDL. Forward-only and additive; every column
|
|
28
33
|
* added here must also be added to the boot migration (the drift guard enforces
|
|
@@ -46,3 +51,31 @@ CREATE TABLE IF NOT EXISTS ${TRANSCRIPT_CHUNK_TABLE} (
|
|
|
46
51
|
PRIMARY KEY (stream, chunk_offset)
|
|
47
52
|
);
|
|
48
53
|
CREATE INDEX IF NOT EXISTS idx_${TRANSCRIPT_STREAM_TABLE}_retention ON ${TRANSCRIPT_STREAM_TABLE} (lifecycle, status, completed_at);`;
|
|
54
|
+
/**
|
|
55
|
+
* The turn-structured transcript DDL — the additive, Camunda-`AgentHistoryRecordValue`
|
|
56
|
+
* parity view layered over the raw chunk stream (issue #475). It ships as its own
|
|
57
|
+
* forward-only migration `db/migrations/008_agentic_transcript_turns.sql` (the raw
|
|
58
|
+
* chunk stream in {@link TRANSCRIPT_SCHEMA_SQL} is untouched — additive, no regression
|
|
59
|
+
* to existing readers), mirrored here as the single source of truth applied by
|
|
60
|
+
* {@link TranscriptStore.ensureSchema} and kept in lockstep by a drift-guard test.
|
|
61
|
+
*
|
|
62
|
+
* One row per structured turn, keyed `(stream, turn_sequence)` so an append/re-record
|
|
63
|
+
* is idempotent (exactly the `(stream, chunk_offset)` discipline of the chunk table).
|
|
64
|
+
* `turn_sequence` is the stream-local append order and idempotency key;
|
|
65
|
+
* `loop_iteration` is the agent-loop turn counter carried as data (Camunda allows
|
|
66
|
+
* several role-split records — e.g. ASSISTANT then TOOL_RESULT — within one iteration).
|
|
67
|
+
* `content`, `tool_calls` and `metrics` hold the typed content blocks, tool calls and
|
|
68
|
+
* per-turn metrics as JSON.
|
|
69
|
+
*/
|
|
70
|
+
export const TRANSCRIPT_TURN_SCHEMA_SQL = `CREATE TABLE IF NOT EXISTS ${TRANSCRIPT_TURN_TABLE} (
|
|
71
|
+
stream TEXT NOT NULL,
|
|
72
|
+
turn_sequence INTEGER NOT NULL,
|
|
73
|
+
loop_iteration INTEGER NOT NULL,
|
|
74
|
+
role TEXT NOT NULL,
|
|
75
|
+
content TEXT NOT NULL,
|
|
76
|
+
tool_calls TEXT NOT NULL,
|
|
77
|
+
metrics TEXT,
|
|
78
|
+
produced_at INTEGER,
|
|
79
|
+
recorded_at TEXT NOT NULL,
|
|
80
|
+
PRIMARY KEY (stream, turn_sequence)
|
|
81
|
+
);`;
|
|
@@ -34,6 +34,73 @@ export interface TranscriptChunk {
|
|
|
34
34
|
readonly offset: number;
|
|
35
35
|
readonly chunk: string;
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* A structured turn's author role — the additive turn-structured view's parity
|
|
39
|
+
* with Camunda `AgentHistoryRole` (issue #475). One pass through the agent loop
|
|
40
|
+
* (model reasons → selects tools → evaluates results) is recorded as one or more
|
|
41
|
+
* role-tagged turns sharing a `loopIteration`.
|
|
42
|
+
*/
|
|
43
|
+
export type TranscriptTurnRole = "USER" | "ASSISTANT" | "TOOL_RESULT" | "CONFIGURATION" | "UNSPECIFIED";
|
|
44
|
+
/** Parity with Camunda `AgentHistoryContentType`: the type of a content block. */
|
|
45
|
+
export type TranscriptContentType = "TEXT" | "DOCUMENT" | "OBJECT" | "UNSPECIFIED";
|
|
46
|
+
/**
|
|
47
|
+
* A single typed content block in a turn's message, mirroring Camunda's
|
|
48
|
+
* `AgentHistoryMessageContentValue`. Exactly one payload is populated per the
|
|
49
|
+
* `contentType`: `text` for TEXT, `documentReference` for DOCUMENT, `object`
|
|
50
|
+
* (any JSON value) for OBJECT.
|
|
51
|
+
*/
|
|
52
|
+
export interface TranscriptContentBlock {
|
|
53
|
+
readonly contentType: TranscriptContentType;
|
|
54
|
+
/** Text payload; populated when `contentType` is TEXT. */
|
|
55
|
+
readonly text?: string;
|
|
56
|
+
/** Document reference; populated when `contentType` is DOCUMENT. */
|
|
57
|
+
readonly documentReference?: string;
|
|
58
|
+
/** JSON value payload; populated when `contentType` is OBJECT (any JSON type). */
|
|
59
|
+
readonly object?: unknown;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A tool call embedded in a turn, mirroring Camunda's
|
|
63
|
+
* `AgentHistoryEmbeddedToolCallValue`: `toolCallId`, `toolName`, the tool task's
|
|
64
|
+
* `elementId`, and the `arguments` passed to it.
|
|
65
|
+
*/
|
|
66
|
+
export interface TranscriptToolCall {
|
|
67
|
+
readonly toolCallId: string;
|
|
68
|
+
readonly toolName: string;
|
|
69
|
+
readonly elementId?: string;
|
|
70
|
+
readonly arguments: Readonly<Record<string, unknown>>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Per-turn metrics, mirroring Camunda's `AgentHistoryMetricsValue`: the token
|
|
74
|
+
* counts consumed/produced by the turn's LLM call and its wall-clock duration.
|
|
75
|
+
*/
|
|
76
|
+
export interface TranscriptTurnMetrics {
|
|
77
|
+
readonly inputTokens: number;
|
|
78
|
+
readonly outputTokens: number;
|
|
79
|
+
readonly reasoningTokenCount: number;
|
|
80
|
+
readonly cacheCreationTokenCount: number;
|
|
81
|
+
readonly cacheReadTokenCount: number;
|
|
82
|
+
readonly durationMs: number;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A structured transcript turn — the additive Camunda `AgentHistoryRecordValue`
|
|
86
|
+
* parity view (issue #475). `sequence` is the stream-local append order and the
|
|
87
|
+
* idempotency key (mirroring a chunk's `offset`); `loopIteration` is the
|
|
88
|
+
* agent-loop turn counter carried as data (several role-split turns can share one
|
|
89
|
+
* iteration). Recording turns never touches the raw chunk stream.
|
|
90
|
+
*/
|
|
91
|
+
export interface TranscriptTurn {
|
|
92
|
+
/** Stream-local append order + idempotency key (like a chunk's `offset`). */
|
|
93
|
+
readonly sequence: number;
|
|
94
|
+
/** The agent-loop turn counter (Camunda `loopIteration`). */
|
|
95
|
+
readonly loopIteration: number;
|
|
96
|
+
readonly role: TranscriptTurnRole;
|
|
97
|
+
readonly content: readonly TranscriptContentBlock[];
|
|
98
|
+
readonly toolCalls: readonly TranscriptToolCall[];
|
|
99
|
+
/** Per-turn metrics; undefined when the worker reported none for this turn. */
|
|
100
|
+
readonly metrics?: TranscriptTurnMetrics;
|
|
101
|
+
/** Epoch-millis timestamp the turn was produced; undefined when unreported. */
|
|
102
|
+
readonly producedAt?: number;
|
|
103
|
+
}
|
|
37
104
|
/** Per-stream transcript metadata. */
|
|
38
105
|
export interface TranscriptStream {
|
|
39
106
|
readonly stream: string;
|
|
@@ -110,9 +177,11 @@ export declare class TranscriptStore {
|
|
|
110
177
|
get ephemeralRetentionMs(): number;
|
|
111
178
|
/**
|
|
112
179
|
* Apply the canonical transcript DDL (idempotent). Callers that let the app
|
|
113
|
-
* DataLayer migration runner apply
|
|
114
|
-
*
|
|
115
|
-
*
|
|
180
|
+
* DataLayer migration runner apply the transcript migrations
|
|
181
|
+
* (`db/migrations/002_agentic_transcript.sql` for the chunk stream and
|
|
182
|
+
* `db/migrations/008_agentic_transcript_turns.sql` for the turn-structured
|
|
183
|
+
* view) do not need this — but it is provided so the store is usable against a
|
|
184
|
+
* bare source too. The DDL is identical to the migrations (drift-guarded).
|
|
116
185
|
*/
|
|
117
186
|
ensureSchema(): void;
|
|
118
187
|
/**
|
|
@@ -163,6 +232,24 @@ export declare class TranscriptStore {
|
|
|
163
232
|
since(stream: string, from: number): TranscriptSlice;
|
|
164
233
|
/** Read a stream's whole durable transcript in offset order. */
|
|
165
234
|
read(stream: string): TranscriptChunk[];
|
|
235
|
+
/**
|
|
236
|
+
* Record structured turns into a stream's additive turn-structured view — the
|
|
237
|
+
* Camunda `AgentHistoryRecordValue` parity layer (issue #475). Each turn is
|
|
238
|
+
* keyed `(stream, sequence)` so re-recording an already-stored sequence (a
|
|
239
|
+
* retry, a re-emit, an overlapping reattach) is a no-op — never a duplicate,
|
|
240
|
+
* exactly the idempotency the chunk stream gets from `(stream, offset)`.
|
|
241
|
+
*
|
|
242
|
+
* This is purely additive: it never reads or writes the raw chunk stream or the
|
|
243
|
+
* stream's offset window, so it cannot regress any existing chunk reader. It
|
|
244
|
+
* auto-opens the stream (default `long-lived`) so the turns hang off a stream
|
|
245
|
+
* row; a lifecycle mismatch throws a {@link TranscriptLifecycleError} before
|
|
246
|
+
* writing anything (lifecycle is first-wins). The batch is atomic: an invalid
|
|
247
|
+
* turn (or any failed write) partway through rolls the whole call back — it
|
|
248
|
+
* records every turn or none. Returns the number of newly-persisted turns.
|
|
249
|
+
*/
|
|
250
|
+
recordTurns(stream: string, turns: Iterable<TranscriptTurn>, lifecycle?: TranscriptLifecycle): number;
|
|
251
|
+
/** Read a stream's whole turn-structured transcript in `sequence` order. */
|
|
252
|
+
readTurns(stream: string): TranscriptTurn[];
|
|
166
253
|
/**
|
|
167
254
|
* Apply a rolling retention window to a long-lived stream: drop every chunk with
|
|
168
255
|
* `offset < before`. A subsequent {@link since} from an offset older than
|
|
@@ -174,8 +261,9 @@ export declare class TranscriptStore {
|
|
|
174
261
|
/**
|
|
175
262
|
* Retention sweep for completed ephemeral transcripts: drop every stream whose
|
|
176
263
|
* `status = 'completed'` and whose `completed_at` is older than the retention
|
|
177
|
-
* window, along with its chunks. Long-lived streams are
|
|
178
|
-
* are bounded by {@link truncateBefore} instead). Returns
|
|
264
|
+
* window, along with its chunks and structured turns. Long-lived streams are
|
|
265
|
+
* never time-swept (they are bounded by {@link truncateBefore} instead). Returns
|
|
266
|
+
* the removed stream ids.
|
|
179
267
|
*
|
|
180
268
|
* The selection and all deletes run inside a single SAVEPOINT (#atomic) so the
|
|
181
269
|
* sweep is all-or-nothing: if any delete throws mid-sweep the whole batch rolls
|
package/dist/transcript/store.js
CHANGED
|
@@ -22,9 +22,17 @@
|
|
|
22
22
|
* ({@link SqliteDb}), so it works against any app DataLayer source without pulling
|
|
23
23
|
* in the whole runtime.
|
|
24
24
|
*/
|
|
25
|
-
import { TRANSCRIPT_CHUNK_TABLE, TRANSCRIPT_SCHEMA_SQL, TRANSCRIPT_STREAM_TABLE } from "./schema.js";
|
|
25
|
+
import { TRANSCRIPT_CHUNK_TABLE, TRANSCRIPT_SCHEMA_SQL, TRANSCRIPT_STREAM_TABLE, TRANSCRIPT_TURN_SCHEMA_SQL, TRANSCRIPT_TURN_TABLE, } from "./schema.js";
|
|
26
26
|
/** The default clock: `Date.now()`. */
|
|
27
27
|
export const systemClock = { now: () => Date.now() };
|
|
28
|
+
const TURN_ROLES = [
|
|
29
|
+
"USER",
|
|
30
|
+
"ASSISTANT",
|
|
31
|
+
"TOOL_RESULT",
|
|
32
|
+
"CONFIGURATION",
|
|
33
|
+
"UNSPECIFIED",
|
|
34
|
+
];
|
|
35
|
+
const CONTENT_TYPES = ["TEXT", "DOCUMENT", "OBJECT", "UNSPECIFIED"];
|
|
28
36
|
const DEFAULT_EPHEMERAL_RETENTION_MS = 86_400_000;
|
|
29
37
|
function isNonNegInt(value) {
|
|
30
38
|
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
@@ -63,6 +71,208 @@ function toStream(row) {
|
|
|
63
71
|
out.firstOffset = row.first_offset;
|
|
64
72
|
return out;
|
|
65
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* A plain JSON-shaped object: an ordinary or null-prototype object, never an array or a
|
|
76
|
+
* class instance (e.g. `Date`, `Map`). Rejecting exotic instances at the boundary stops a
|
|
77
|
+
* value that would serialise via `toJSON()` into a non-object (making the stored turn
|
|
78
|
+
* unreadable) from ever being persisted.
|
|
79
|
+
*/
|
|
80
|
+
function isPlainObject(value) {
|
|
81
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
82
|
+
return false;
|
|
83
|
+
const proto = Object.getPrototypeOf(value);
|
|
84
|
+
return proto === Object.prototype || proto === null;
|
|
85
|
+
}
|
|
86
|
+
function toTurnRole(value) {
|
|
87
|
+
const match = TURN_ROLES.find((role) => role === value);
|
|
88
|
+
if (match !== undefined)
|
|
89
|
+
return match;
|
|
90
|
+
throw new TranscriptCorruptionError(`invalid transcript turn role: ${JSON.stringify(value)}`);
|
|
91
|
+
}
|
|
92
|
+
function toContentType(value) {
|
|
93
|
+
const match = CONTENT_TYPES.find((type) => type === value);
|
|
94
|
+
if (match !== undefined)
|
|
95
|
+
return match;
|
|
96
|
+
throw new TranscriptCorruptionError(`invalid transcript content type: ${JSON.stringify(value)}`);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The payload key each content type carries: TEXT→`text`, DOCUMENT→`documentReference`,
|
|
100
|
+
* OBJECT→`object`; UNSPECIFIED carries none. Drives the per-`contentType` payload
|
|
101
|
+
* invariant enforced by {@link toContentBlock}.
|
|
102
|
+
*/
|
|
103
|
+
const CONTENT_PAYLOAD_KEYS = ["text", "documentReference", "object"];
|
|
104
|
+
const CONTENT_TYPE_PAYLOAD = {
|
|
105
|
+
TEXT: "text",
|
|
106
|
+
DOCUMENT: "documentReference",
|
|
107
|
+
OBJECT: "object",
|
|
108
|
+
UNSPECIFIED: null,
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Validate a value into a canonical content block, enforcing the per-`contentType`
|
|
112
|
+
* payload invariant: a block carries exactly the one payload its type mandates
|
|
113
|
+
* (TEXT→text, DOCUMENT→documentReference, OBJECT→object) — never a missing, mismatched
|
|
114
|
+
* or multiple payload — and UNSPECIFIED carries none. This is the single validator both
|
|
115
|
+
* the write path (record) and the read path (deserialise) run through, so the two can
|
|
116
|
+
* never drift and a malformed block can neither persist nor round-trip. It emits only
|
|
117
|
+
* that one payload key, so serialise/deserialise round-trips exactly.
|
|
118
|
+
*/
|
|
119
|
+
function toContentBlock(value) {
|
|
120
|
+
if (!isPlainObject(value)) {
|
|
121
|
+
throw new TranscriptCorruptionError(`transcript content block must be an object, got ${JSON.stringify(value)}`);
|
|
122
|
+
}
|
|
123
|
+
const contentType = toContentType(value.contentType);
|
|
124
|
+
const present = CONTENT_PAYLOAD_KEYS.filter((key) => key in value && value[key] !== undefined);
|
|
125
|
+
const expected = CONTENT_TYPE_PAYLOAD[contentType];
|
|
126
|
+
if (expected === null) {
|
|
127
|
+
if (present.length > 0) {
|
|
128
|
+
throw new TranscriptCorruptionError(`${contentType} content block must carry no payload, got ${JSON.stringify(present)}`);
|
|
129
|
+
}
|
|
130
|
+
return { contentType };
|
|
131
|
+
}
|
|
132
|
+
if (present.length !== 1 || present[0] !== expected) {
|
|
133
|
+
throw new TranscriptCorruptionError(`${contentType} content block must carry exactly its ${expected} payload, got ${JSON.stringify(present)}`);
|
|
134
|
+
}
|
|
135
|
+
if (expected === "text") {
|
|
136
|
+
const text = value.text;
|
|
137
|
+
if (typeof text !== "string") {
|
|
138
|
+
throw new TranscriptCorruptionError(`content block text must be a string, got ${JSON.stringify(text)}`);
|
|
139
|
+
}
|
|
140
|
+
return { contentType, text };
|
|
141
|
+
}
|
|
142
|
+
if (expected === "documentReference") {
|
|
143
|
+
const documentReference = value.documentReference;
|
|
144
|
+
if (typeof documentReference !== "string") {
|
|
145
|
+
throw new TranscriptCorruptionError(`content block documentReference must be a string, got ${JSON.stringify(documentReference)}`);
|
|
146
|
+
}
|
|
147
|
+
return { contentType, documentReference };
|
|
148
|
+
}
|
|
149
|
+
return { contentType, object: value.object };
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Validate a value into a canonical tool call — the single validator both the write and
|
|
153
|
+
* read paths run through. Mirrors Camunda's `AgentHistoryEmbeddedToolCallValue`.
|
|
154
|
+
*/
|
|
155
|
+
function toToolCall(value) {
|
|
156
|
+
if (!isPlainObject(value)) {
|
|
157
|
+
throw new TranscriptCorruptionError(`transcript tool call must be an object, got ${JSON.stringify(value)}`);
|
|
158
|
+
}
|
|
159
|
+
const { toolCallId, toolName, elementId } = value;
|
|
160
|
+
if (typeof toolCallId !== "string") {
|
|
161
|
+
throw new TranscriptCorruptionError(`tool call toolCallId must be a string, got ${JSON.stringify(toolCallId)}`);
|
|
162
|
+
}
|
|
163
|
+
if (typeof toolName !== "string") {
|
|
164
|
+
throw new TranscriptCorruptionError(`tool call toolName must be a string, got ${JSON.stringify(toolName)}`);
|
|
165
|
+
}
|
|
166
|
+
const args = value.arguments;
|
|
167
|
+
if (!isPlainObject(args)) {
|
|
168
|
+
throw new TranscriptCorruptionError(`tool call arguments must be an object, got ${JSON.stringify(args)}`);
|
|
169
|
+
}
|
|
170
|
+
const out = {
|
|
171
|
+
toolCallId,
|
|
172
|
+
toolName,
|
|
173
|
+
arguments: args,
|
|
174
|
+
};
|
|
175
|
+
if (elementId !== undefined) {
|
|
176
|
+
if (typeof elementId !== "string") {
|
|
177
|
+
throw new TranscriptCorruptionError(`tool call elementId must be a string, got ${JSON.stringify(elementId)}`);
|
|
178
|
+
}
|
|
179
|
+
out.elementId = elementId;
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
function readMetric(source, key) {
|
|
184
|
+
const value = source[key];
|
|
185
|
+
if (!isNonNegInt(value)) {
|
|
186
|
+
throw new TranscriptCorruptionError(`transcript metric "${key}" must be a non-negative safe integer, got ${JSON.stringify(value)}`);
|
|
187
|
+
}
|
|
188
|
+
return value;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Validate a value into canonical per-turn metrics — the single validator both the write
|
|
192
|
+
* and read paths run through. Every field must be a non-negative safe integer: Camunda's
|
|
193
|
+
* `AgentHistoryMetricsValue` token counts and `durationMs` are integer-valued and cannot be
|
|
194
|
+
* negative, so an out-of-domain value is treated as corruption rather than round-tripped.
|
|
195
|
+
*/
|
|
196
|
+
function toMetrics(value) {
|
|
197
|
+
if (!isPlainObject(value)) {
|
|
198
|
+
throw new TranscriptCorruptionError(`transcript turn metrics must be an object, got ${JSON.stringify(value)}`);
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
inputTokens: readMetric(value, "inputTokens"),
|
|
202
|
+
outputTokens: readMetric(value, "outputTokens"),
|
|
203
|
+
reasoningTokenCount: readMetric(value, "reasoningTokenCount"),
|
|
204
|
+
cacheCreationTokenCount: readMetric(value, "cacheCreationTokenCount"),
|
|
205
|
+
cacheReadTokenCount: readMetric(value, "cacheReadTokenCount"),
|
|
206
|
+
durationMs: readMetric(value, "durationMs"),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Parse a JSON column, re-raising a malformed-JSON `SyntaxError` as a
|
|
211
|
+
* {@link TranscriptCorruptionError} so every read-back failure surfaces through the store's
|
|
212
|
+
* single corruption signal rather than a raw parse error.
|
|
213
|
+
*/
|
|
214
|
+
function parseJsonColumn(json, label) {
|
|
215
|
+
try {
|
|
216
|
+
return JSON.parse(json);
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
throw new TranscriptCorruptionError(`transcript turn ${label} is not valid JSON: ${err instanceof Error ? err.message : String(err)}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/** Parse a JSON array column, failing with a corruption error on invalid JSON or non-arrays. */
|
|
223
|
+
function parseJsonArray(json, label) {
|
|
224
|
+
const parsed = parseJsonColumn(json, label);
|
|
225
|
+
if (!Array.isArray(parsed)) {
|
|
226
|
+
throw new TranscriptCorruptionError(`transcript turn ${label} must be a JSON array, got ${JSON.stringify(parsed)}`);
|
|
227
|
+
}
|
|
228
|
+
return parsed;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Serialise a validated turn payload for storage, re-raising a non-serialisable value
|
|
232
|
+
* (e.g. a `bigint`, which makes `JSON.stringify` throw a `TypeError`) as a
|
|
233
|
+
* {@link TranscriptCorruptionError} so the untyped write boundary only ever surfaces the
|
|
234
|
+
* store's own error taxonomy.
|
|
235
|
+
*/
|
|
236
|
+
function stringifyJson(value, label) {
|
|
237
|
+
try {
|
|
238
|
+
return JSON.stringify(value);
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
throw new TranscriptCorruptionError(`transcript turn ${label} is not JSON-serialisable: ${err instanceof Error ? err.message : String(err)}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Reconstruct a structured turn from its DB row, validating every field so a corrupted
|
|
246
|
+
* or hand-edited row fails fast with a {@link TranscriptCorruptionError} rather than
|
|
247
|
+
* silently propagating an out-of-domain value. `turn_sequence`, `loop_iteration` and
|
|
248
|
+
* `produced_at` are held to the same non-negative-safe-integer domain the write path
|
|
249
|
+
* enforces.
|
|
250
|
+
*/
|
|
251
|
+
function toTurn(row) {
|
|
252
|
+
if (!isRecordableOffset(row.turn_sequence)) {
|
|
253
|
+
throw new TranscriptCorruptionError(`transcript turn sequence must be a non-negative safe integer, got ${JSON.stringify(row.turn_sequence)}`);
|
|
254
|
+
}
|
|
255
|
+
if (!isNonNegInt(row.loop_iteration)) {
|
|
256
|
+
throw new TranscriptCorruptionError(`transcript turn loopIteration must be a non-negative safe integer, got ${JSON.stringify(row.loop_iteration)}`);
|
|
257
|
+
}
|
|
258
|
+
const out = {
|
|
259
|
+
sequence: row.turn_sequence,
|
|
260
|
+
loopIteration: row.loop_iteration,
|
|
261
|
+
role: toTurnRole(row.role),
|
|
262
|
+
content: parseJsonArray(row.content, "content").map(toContentBlock),
|
|
263
|
+
toolCalls: parseJsonArray(row.tool_calls, "toolCalls").map(toToolCall),
|
|
264
|
+
};
|
|
265
|
+
if (row.metrics !== null) {
|
|
266
|
+
out.metrics = toMetrics(parseJsonColumn(row.metrics, "metrics"));
|
|
267
|
+
}
|
|
268
|
+
if (row.produced_at !== null) {
|
|
269
|
+
if (!isNonNegInt(row.produced_at)) {
|
|
270
|
+
throw new TranscriptCorruptionError(`transcript turn producedAt must be a non-negative safe integer, got ${JSON.stringify(row.produced_at)}`);
|
|
271
|
+
}
|
|
272
|
+
out.producedAt = row.produced_at;
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
}
|
|
66
276
|
/**
|
|
67
277
|
* Raised when a transcript row read back from storage holds a value outside its
|
|
68
278
|
* domain (e.g. an unknown `lifecycle`/`status`), signalling schema corruption or a
|
|
@@ -106,12 +316,15 @@ export class TranscriptStore {
|
|
|
106
316
|
}
|
|
107
317
|
/**
|
|
108
318
|
* Apply the canonical transcript DDL (idempotent). Callers that let the app
|
|
109
|
-
* DataLayer migration runner apply
|
|
110
|
-
*
|
|
111
|
-
*
|
|
319
|
+
* DataLayer migration runner apply the transcript migrations
|
|
320
|
+
* (`db/migrations/002_agentic_transcript.sql` for the chunk stream and
|
|
321
|
+
* `db/migrations/008_agentic_transcript_turns.sql` for the turn-structured
|
|
322
|
+
* view) do not need this — but it is provided so the store is usable against a
|
|
323
|
+
* bare source too. The DDL is identical to the migrations (drift-guarded).
|
|
112
324
|
*/
|
|
113
325
|
ensureSchema() {
|
|
114
326
|
this.#db.exec(TRANSCRIPT_SCHEMA_SQL);
|
|
327
|
+
this.#db.exec(TRANSCRIPT_TURN_SCHEMA_SQL);
|
|
115
328
|
}
|
|
116
329
|
/**
|
|
117
330
|
* Open (or fetch) a stream's transcript with the given lifecycle. Idempotent:
|
|
@@ -228,6 +441,72 @@ export class TranscriptStore {
|
|
|
228
441
|
.all(`SELECT chunk_offset, chunk FROM ${TRANSCRIPT_CHUNK_TABLE} WHERE stream = ? ORDER BY chunk_offset`, [stream])
|
|
229
442
|
.map((r) => ({ offset: r.chunk_offset, chunk: r.chunk }));
|
|
230
443
|
}
|
|
444
|
+
/**
|
|
445
|
+
* Record structured turns into a stream's additive turn-structured view — the
|
|
446
|
+
* Camunda `AgentHistoryRecordValue` parity layer (issue #475). Each turn is
|
|
447
|
+
* keyed `(stream, sequence)` so re-recording an already-stored sequence (a
|
|
448
|
+
* retry, a re-emit, an overlapping reattach) is a no-op — never a duplicate,
|
|
449
|
+
* exactly the idempotency the chunk stream gets from `(stream, offset)`.
|
|
450
|
+
*
|
|
451
|
+
* This is purely additive: it never reads or writes the raw chunk stream or the
|
|
452
|
+
* stream's offset window, so it cannot regress any existing chunk reader. It
|
|
453
|
+
* auto-opens the stream (default `long-lived`) so the turns hang off a stream
|
|
454
|
+
* row; a lifecycle mismatch throws a {@link TranscriptLifecycleError} before
|
|
455
|
+
* writing anything (lifecycle is first-wins). The batch is atomic: an invalid
|
|
456
|
+
* turn (or any failed write) partway through rolls the whole call back — it
|
|
457
|
+
* records every turn or none. Returns the number of newly-persisted turns.
|
|
458
|
+
*/
|
|
459
|
+
recordTurns(stream, turns, lifecycle = "long-lived") {
|
|
460
|
+
const meta = this.open(stream, lifecycle);
|
|
461
|
+
if (meta.lifecycle !== lifecycle) {
|
|
462
|
+
throw new TranscriptLifecycleError(stream, `refusing to record turns into "${stream}" with lifecycle=${lifecycle}; the stream is ${meta.lifecycle} (lifecycle is first-wins)`);
|
|
463
|
+
}
|
|
464
|
+
const at = new Date(this.#clock.now()).toISOString();
|
|
465
|
+
return this.#atomic(() => {
|
|
466
|
+
let written = 0;
|
|
467
|
+
for (const turn of turns) {
|
|
468
|
+
if (!isPlainObject(turn)) {
|
|
469
|
+
throw new TranscriptCorruptionError(`transcript turn must be an object, got ${JSON.stringify(turn)}`);
|
|
470
|
+
}
|
|
471
|
+
if (!isRecordableOffset(turn.sequence)) {
|
|
472
|
+
throw new RangeError(`transcript turn sequence must be a non-negative safe integer below Number.MAX_SAFE_INTEGER, got ${turn.sequence}`);
|
|
473
|
+
}
|
|
474
|
+
if (!isNonNegInt(turn.loopIteration)) {
|
|
475
|
+
throw new RangeError(`transcript turn loopIteration must be a non-negative safe integer, got ${turn.loopIteration}`);
|
|
476
|
+
}
|
|
477
|
+
const role = toTurnRole(turn.role);
|
|
478
|
+
if (!Array.isArray(turn.content)) {
|
|
479
|
+
throw new TranscriptCorruptionError(`transcript turn content must be an array, got ${JSON.stringify(turn.content)}`);
|
|
480
|
+
}
|
|
481
|
+
if (!Array.isArray(turn.toolCalls)) {
|
|
482
|
+
throw new TranscriptCorruptionError(`transcript turn toolCalls must be an array, got ${JSON.stringify(turn.toolCalls)}`);
|
|
483
|
+
}
|
|
484
|
+
const content = stringifyJson(turn.content.map(toContentBlock), "content");
|
|
485
|
+
const toolCalls = stringifyJson(turn.toolCalls.map(toToolCall), "toolCalls");
|
|
486
|
+
const metrics = turn.metrics === undefined ? null : stringifyJson(toMetrics(turn.metrics), "metrics");
|
|
487
|
+
let producedAt = null;
|
|
488
|
+
if (turn.producedAt !== undefined) {
|
|
489
|
+
if (!isNonNegInt(turn.producedAt)) {
|
|
490
|
+
throw new RangeError(`transcript turn producedAt must be a non-negative safe integer (epoch millis), got ${turn.producedAt}`);
|
|
491
|
+
}
|
|
492
|
+
producedAt = turn.producedAt;
|
|
493
|
+
}
|
|
494
|
+
const { changes } = this.#db.run(`INSERT INTO ${TRANSCRIPT_TURN_TABLE}
|
|
495
|
+
(stream, turn_sequence, loop_iteration, role, content, tool_calls, metrics, produced_at, recorded_at)
|
|
496
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
497
|
+
ON CONFLICT(stream, turn_sequence) DO NOTHING`, [stream, turn.sequence, turn.loopIteration, role, content, toolCalls, metrics, producedAt, at]);
|
|
498
|
+
written += changes;
|
|
499
|
+
}
|
|
500
|
+
return written;
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
/** Read a stream's whole turn-structured transcript in `sequence` order. */
|
|
504
|
+
readTurns(stream) {
|
|
505
|
+
return this.#db
|
|
506
|
+
.all(`SELECT turn_sequence, loop_iteration, role, content, tool_calls, metrics, produced_at
|
|
507
|
+
FROM ${TRANSCRIPT_TURN_TABLE} WHERE stream = ? ORDER BY turn_sequence`, [stream])
|
|
508
|
+
.map(toTurn);
|
|
509
|
+
}
|
|
231
510
|
/**
|
|
232
511
|
* Apply a rolling retention window to a long-lived stream: drop every chunk with
|
|
233
512
|
* `offset < before`. A subsequent {@link since} from an offset older than
|
|
@@ -253,8 +532,9 @@ export class TranscriptStore {
|
|
|
253
532
|
/**
|
|
254
533
|
* Retention sweep for completed ephemeral transcripts: drop every stream whose
|
|
255
534
|
* `status = 'completed'` and whose `completed_at` is older than the retention
|
|
256
|
-
* window, along with its chunks. Long-lived streams are
|
|
257
|
-
* are bounded by {@link truncateBefore} instead). Returns
|
|
535
|
+
* window, along with its chunks and structured turns. Long-lived streams are
|
|
536
|
+
* never time-swept (they are bounded by {@link truncateBefore} instead). Returns
|
|
537
|
+
* the removed stream ids.
|
|
258
538
|
*
|
|
259
539
|
* The selection and all deletes run inside a single SAVEPOINT (#atomic) so the
|
|
260
540
|
* sweep is all-or-nothing: if any delete throws mid-sweep the whole batch rolls
|
|
@@ -271,6 +551,7 @@ export class TranscriptStore {
|
|
|
271
551
|
.map((r) => r.stream);
|
|
272
552
|
for (const stream of removed) {
|
|
273
553
|
this.#db.run(`DELETE FROM ${TRANSCRIPT_CHUNK_TABLE} WHERE stream = ?`, [stream]);
|
|
554
|
+
this.#db.run(`DELETE FROM ${TRANSCRIPT_TURN_TABLE} WHERE stream = ?`, [stream]);
|
|
274
555
|
this.#db.run(`DELETE FROM ${TRANSCRIPT_STREAM_TABLE} WHERE stream = ?`, [stream]);
|
|
275
556
|
}
|
|
276
557
|
return removed;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nanobpm/agentic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "The Nano agentic protocol (ADR 0056): one app-tier channel carrying agent presence/registry, demand×supply, a shared blackboard and live terminal relay — with the wire contract, channel/hub, family modules and the operator cockpit, as subpath exports.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
package/src/transcript/index.ts
CHANGED
|
@@ -18,16 +18,24 @@ export type {
|
|
|
18
18
|
Clock,
|
|
19
19
|
SqliteDb,
|
|
20
20
|
TranscriptChunk,
|
|
21
|
+
TranscriptContentBlock,
|
|
22
|
+
TranscriptContentType,
|
|
21
23
|
TranscriptLifecycle,
|
|
22
24
|
TranscriptRing,
|
|
23
25
|
TranscriptSlice,
|
|
24
26
|
TranscriptStatus,
|
|
25
27
|
TranscriptStoreOptions,
|
|
26
28
|
TranscriptStream,
|
|
29
|
+
TranscriptToolCall,
|
|
30
|
+
TranscriptTurn,
|
|
31
|
+
TranscriptTurnMetrics,
|
|
32
|
+
TranscriptTurnRole,
|
|
27
33
|
} from "./store.ts";
|
|
28
34
|
|
|
29
35
|
export {
|
|
30
36
|
TRANSCRIPT_CHUNK_TABLE,
|
|
31
37
|
TRANSCRIPT_SCHEMA_SQL,
|
|
32
38
|
TRANSCRIPT_STREAM_TABLE,
|
|
39
|
+
TRANSCRIPT_TURN_SCHEMA_SQL,
|
|
40
|
+
TRANSCRIPT_TURN_TABLE,
|
|
33
41
|
} from "./schema.ts";
|