@nt-ai-lab/deterministic-agent-workflow-event-store 0.2.1 → 0.3.1

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.
@@ -1,4 +1,4 @@
1
- import type { StoredEvent } from '@nt-ai-lab/deterministic-agent-workflow-engine';
1
+ import type { RecordReflectionInput, StoredEvent, StoredReflection } from '@nt-ai-lab/deterministic-agent-workflow-engine';
2
2
  import { type SqliteDatabase } from '../infra/external-clients/sqlite/sqlite-runtime';
3
3
  /** @riviere-role value-object */
4
4
  export type SqliteEventStore = {
@@ -6,6 +6,8 @@ export type SqliteEventStore = {
6
6
  readonly appendEvents: (sessionId: string, events: readonly StoredEvent[]) => void;
7
7
  readonly sessionExists: (sessionId: string) => boolean;
8
8
  readonly hasSessionStarted: (sessionId: string) => boolean;
9
+ readonly recordReflection: (sessionId: string, createdAt: string, input: RecordReflectionInput) => StoredReflection;
10
+ readonly listReflections: (sessionId: string) => readonly StoredReflection[];
9
11
  readonly listSessions: () => readonly string[];
10
12
  readonly db: SqliteDatabase;
11
13
  };
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod';
2
- import { stripEnvelopeKeys, WorkflowStateError, } from '@nt-ai-lab/deterministic-agent-workflow-engine';
2
+ import { recordReflectionInputSchema, storedReflectionSchema, stripEnvelopeKeys, WorkflowStateError, } from '@nt-ai-lab/deterministic-agent-workflow-engine';
3
3
  import { enableWalMode, openSqliteDatabase, } from '../infra/external-clients/sqlite/sqlite-runtime.js';
4
4
  const createTableSql = `
5
5
  CREATE TABLE IF NOT EXISTS events (
@@ -11,6 +11,21 @@ const createTableSql = `
11
11
  payload TEXT NOT NULL
12
12
  )
13
13
  `;
14
+ const createReflectionsTableSql = `
15
+ CREATE TABLE IF NOT EXISTS reflections (
16
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
17
+ session_id TEXT NOT NULL,
18
+ created_at TEXT NOT NULL,
19
+ label TEXT,
20
+ agent_name TEXT,
21
+ source_state TEXT,
22
+ payload_json TEXT NOT NULL
23
+ )
24
+ `;
25
+ const createReflectionsIndexSql = `
26
+ CREATE INDEX IF NOT EXISTS idx_reflections_session_created_at
27
+ ON reflections (session_id, created_at DESC, id DESC)
28
+ `;
14
29
  const eventRowSchema = z.array(z.object({
15
30
  type: z.string(),
16
31
  at: z.string(),
@@ -21,11 +36,23 @@ const rowWithSessionIdSchema = z.array(z.object({ session_id: z.string() }));
21
36
  const countFieldSchema = z.union([z.number(), z.bigint(), z.string()]);
22
37
  const countRowSchema = z.object({ count: countFieldSchema });
23
38
  const tableInfoRowSchema = z.array(z.object({ name: z.string() }));
39
+ const reflectionIdRowSchema = z.object({ id: countFieldSchema });
40
+ const reflectionRowsSchema = z.array(z.object({
41
+ id: z.number(),
42
+ session_id: z.string(),
43
+ created_at: z.string(),
44
+ label: z.string().nullable(),
45
+ agent_name: z.string().nullable(),
46
+ source_state: z.string().nullable(),
47
+ payload_json: z.string(),
48
+ }));
24
49
  /** @riviere-role domain-service */
25
50
  export function createStore(dbPath) {
26
51
  const db = openSqliteDatabase(dbPath);
27
52
  enableWalMode(db);
28
53
  db.exec(createTableSql);
54
+ db.exec(createReflectionsTableSql);
55
+ db.exec(createReflectionsIndexSql);
29
56
  ensureStateColumn(db);
30
57
  return {
31
58
  db,
@@ -56,6 +83,47 @@ export function createStore(dbPath) {
56
83
  hasSessionStarted(sessionId) {
57
84
  return readCount(db, "SELECT COUNT(1) AS count FROM events WHERE session_id = ? AND type = 'session-started'", sessionId) > 0;
58
85
  },
86
+ recordReflection(sessionId, createdAt, input) {
87
+ const parsedInput = recordReflectionInputSchema.parse(input);
88
+ const insert = db.prepare('INSERT INTO reflections (session_id, created_at, label, agent_name, source_state, payload_json) VALUES (?, ?, ?, ?, ?, ?)');
89
+ db.exec('BEGIN IMMEDIATE');
90
+ try {
91
+ insert.run(sessionId, createdAt, parsedInput.label ?? null, parsedInput.agentName ?? null, parsedInput.sourceState ?? null, JSON.stringify(parsedInput.reflection));
92
+ const rawId = db.prepare('SELECT last_insert_rowid() AS id').get();
93
+ const parsedId = reflectionIdRowSchema.parse(rawId);
94
+ const id = Number(parsedId.id);
95
+ db.exec('COMMIT');
96
+ return storedReflectionSchema.parse({
97
+ id,
98
+ sessionId,
99
+ createdAt,
100
+ ...(parsedInput.label === undefined ? {} : { label: parsedInput.label }),
101
+ ...(parsedInput.agentName === undefined ? {} : { agentName: parsedInput.agentName }),
102
+ ...(parsedInput.sourceState === undefined ? {} : { sourceState: parsedInput.sourceState }),
103
+ reflection: parsedInput.reflection,
104
+ });
105
+ }
106
+ catch (error) {
107
+ db.exec('ROLLBACK');
108
+ throw error;
109
+ }
110
+ },
111
+ listReflections(sessionId) {
112
+ const rawRows = db.prepare('SELECT id, session_id, created_at, label, agent_name, source_state, payload_json FROM reflections WHERE session_id = ? ORDER BY created_at DESC, id DESC').all(sessionId);
113
+ const rows = reflectionRowsSchema.parse(rawRows);
114
+ return rows.map((row) => {
115
+ const reflectionPayload = JSON.parse(row.payload_json);
116
+ return storedReflectionSchema.parse({
117
+ id: row.id,
118
+ sessionId: row.session_id,
119
+ createdAt: row.created_at,
120
+ ...(row.label === null ? {} : { label: row.label }),
121
+ ...(row.agent_name === null ? {} : { agentName: row.agent_name }),
122
+ ...(row.source_state === null ? {} : { sourceState: row.source_state }),
123
+ reflection: reflectionPayload,
124
+ });
125
+ });
126
+ },
59
127
  listSessions() {
60
128
  const rawRows = db.prepare('SELECT session_id FROM events GROUP BY session_id ORDER BY MIN(seq)').all();
61
129
  return rowWithSessionIdSchema.parse(rawRows).map((row) => row.session_id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nt-ai-lab/deterministic-agent-workflow-event-store",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -12,7 +12,7 @@
12
12
  ],
13
13
  "dependencies": {
14
14
  "zod": "^3.25.76",
15
- "@nt-ai-lab/deterministic-agent-workflow-engine": "0.2.1"
15
+ "@nt-ai-lab/deterministic-agent-workflow-engine": "0.3.1"
16
16
  },
17
17
  "publishConfig": {
18
18
  "access": "public"