@otakumesi/heph 0.0.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.
@@ -0,0 +1,1140 @@
1
+ import { P as HephError, _ as createMessageId, b as createSkillBindingId, d as createApprovalRequestId, f as createDeferredToolOperationId, g as createMemoryId, h as createMcpBindingId, m as createInboxEventId, u as createAgentSessionId, v as createRunEventId, y as createRunId } from "./src-DeheByi4.mjs";
2
+ import { createRequire } from "node:module";
3
+ import { mkdirSync, writeFileSync } from "node:fs";
4
+ import { dirname, join } from "node:path";
5
+ //#region ../sqlite/src/index.ts
6
+ const BetterSqlite3 = createRequire(import.meta.url)("better-sqlite3");
7
+ const INITIAL_MIGRATION_VERSION = "0001_initial_heph_sqlite";
8
+ const DEFAULT_MIGRATION_OUTPUT_DIR = "migrations/heph";
9
+ function createSQLiteAdapters(options) {
10
+ return {
11
+ stores: createSQLiteHephStore(options),
12
+ queue: createSQLiteQueue({
13
+ ...options,
14
+ migrations: {
15
+ ...options.migrations,
16
+ mode: "none"
17
+ }
18
+ })
19
+ };
20
+ }
21
+ function createSQLiteHephStore(options) {
22
+ return new SQLiteHephStore(openDatabase(options));
23
+ }
24
+ function createSQLiteQueue(options) {
25
+ return new SQLiteQueue(openDatabase(options), options);
26
+ }
27
+ function writeSQLiteMigration(outputDir = DEFAULT_MIGRATION_OUTPUT_DIR) {
28
+ mkdirSync(outputDir, { recursive: true });
29
+ const file = join(outputDir, `${INITIAL_MIGRATION_VERSION}.sql`);
30
+ writeFileSync(file, INITIAL_MIGRATION_SQL, "utf8");
31
+ return file;
32
+ }
33
+ var SQLiteHephStore = class {
34
+ db;
35
+ state = this;
36
+ messages = this;
37
+ inbox = this;
38
+ events = this;
39
+ memory = this;
40
+ mcpBindings = this;
41
+ skillBindings = this;
42
+ approvals = this;
43
+ deferredToolOperations = this;
44
+ constructor(db) {
45
+ this.db = db;
46
+ }
47
+ close() {
48
+ this.db.close();
49
+ }
50
+ async createAgentSession(input) {
51
+ const now = /* @__PURE__ */ new Date();
52
+ const agent = {
53
+ id: input.id ?? createAgentSessionId(),
54
+ agentSpecId: input.agentSpecId,
55
+ agentSpecVersion: input.agentSpecVersion ?? null,
56
+ state: input.state ?? {},
57
+ activeRunId: null,
58
+ auth: input.auth ?? null,
59
+ metadata: input.metadata ?? {},
60
+ createdAt: now,
61
+ updatedAt: now
62
+ };
63
+ this.db.prepare(`insert into heph_agent_sessions
64
+ (id, agent_spec_id, agent_spec_version, state_json, active_run_id, auth_json, metadata_json, created_at, updated_at)
65
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(agent.id, agent.agentSpecId, agent.agentSpecVersion, stringifyJson(agent.state), agent.activeRunId, stringifyJson(agent.auth), stringifyJson(agent.metadata), toIso(agent.createdAt), toIso(agent.updatedAt));
66
+ return clonePlain(agent);
67
+ }
68
+ async getAgentSession(id) {
69
+ const row = this.db.prepare("select * from heph_agent_sessions where id = ?").get(id);
70
+ return row ? rowToAgentSession(row) : null;
71
+ }
72
+ async updateAgentSession(id, patch) {
73
+ const existing = await this.getAgentSession(id);
74
+ if (!existing) throw notFound("AgentSession not found", { agentId: id });
75
+ const updated = {
76
+ ...existing,
77
+ ...patch,
78
+ id,
79
+ createdAt: existing.createdAt,
80
+ updatedAt: /* @__PURE__ */ new Date()
81
+ };
82
+ this.db.prepare(`update heph_agent_sessions
83
+ set agent_spec_id = ?, agent_spec_version = ?, state_json = ?, active_run_id = ?, auth_json = ?, metadata_json = ?, updated_at = ?
84
+ where id = ?`).run(updated.agentSpecId, updated.agentSpecVersion, stringifyJson(updated.state), updated.activeRunId, stringifyJson(updated.auth), stringifyJson(updated.metadata), toIso(updated.updatedAt), id);
85
+ return clonePlain(updated);
86
+ }
87
+ async createRun(input) {
88
+ const now = /* @__PURE__ */ new Date();
89
+ const run = {
90
+ id: input.id ?? createRunId(),
91
+ agentId: input.agentId,
92
+ agentSpecId: input.agentSpecId,
93
+ agentSpecVersion: input.agentSpecVersion ?? null,
94
+ status: input.status,
95
+ input: input.input,
96
+ auth: input.auth ?? null,
97
+ contextManifest: null,
98
+ skillManifest: null,
99
+ toolManifest: null,
100
+ error: null,
101
+ metadata: input.metadata ?? {},
102
+ queuedAt: now,
103
+ startedAt: null,
104
+ completedAt: null,
105
+ createdAt: now,
106
+ updatedAt: now
107
+ };
108
+ this.db.prepare(`insert into heph_runs
109
+ (id, agent_id, agent_spec_id, agent_spec_version, status, input_json, auth_json, context_manifest_json,
110
+ skill_manifest_json, tool_manifest_json, error_json, metadata_json, queued_at, started_at, completed_at, created_at, updated_at)
111
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(run.id, run.agentId, run.agentSpecId, run.agentSpecVersion, run.status, stringifyJson(run.input), stringifyJson(run.auth), stringifyJson(run.contextManifest), stringifyJson(run.skillManifest), stringifyJson(run.toolManifest), stringifyJson(run.error), stringifyJson(run.metadata), toIso(run.queuedAt), toIsoOrNull(run.startedAt), toIsoOrNull(run.completedAt), toIso(run.createdAt), toIso(run.updatedAt));
112
+ return clonePlain(run);
113
+ }
114
+ async getRun(id) {
115
+ const row = this.db.prepare("select * from heph_runs where id = ?").get(id);
116
+ return row ? rowToRun(row) : null;
117
+ }
118
+ async updateRun(id, patch) {
119
+ const existing = await this.getRun(id);
120
+ if (!existing) throw notFound("Run not found", { runId: id });
121
+ const updated = {
122
+ ...existing,
123
+ ...patch,
124
+ id,
125
+ createdAt: existing.createdAt,
126
+ updatedAt: /* @__PURE__ */ new Date()
127
+ };
128
+ this.db.prepare(`update heph_runs
129
+ set agent_id = ?, agent_spec_id = ?, agent_spec_version = ?, status = ?, input_json = ?, auth_json = ?,
130
+ context_manifest_json = ?, skill_manifest_json = ?, tool_manifest_json = ?, error_json = ?, metadata_json = ?, queued_at = ?, started_at = ?,
131
+ completed_at = ?, updated_at = ?
132
+ where id = ?`).run(updated.agentId, updated.agentSpecId, updated.agentSpecVersion, updated.status, stringifyJson(updated.input), stringifyJson(updated.auth), stringifyJson(updated.contextManifest), stringifyJson(updated.skillManifest), stringifyJson(updated.toolManifest), stringifyJson(updated.error), stringifyJson(updated.metadata), toIso(updated.queuedAt), toIsoOrNull(updated.startedAt), toIsoOrNull(updated.completedAt), toIso(updated.updatedAt), id);
133
+ return clonePlain(updated);
134
+ }
135
+ async listRunsByAgent(agentId) {
136
+ return this.db.prepare("select * from heph_runs where agent_id = ? order by created_at asc, id asc").all(agentId).map((row) => rowToRun(row));
137
+ }
138
+ async appendMessage(input) {
139
+ const message = {
140
+ id: input.id ?? createMessageId(),
141
+ agentId: input.agentId,
142
+ role: input.role,
143
+ content: input.content,
144
+ sourceRunId: input.sourceRunId ?? null,
145
+ auth: input.auth ?? null,
146
+ metadata: input.metadata ?? {},
147
+ createdAt: /* @__PURE__ */ new Date()
148
+ };
149
+ this.db.prepare(`insert into heph_messages
150
+ (id, agent_id, role, content, source_run_id, auth_json, metadata_json, created_at)
151
+ values (?, ?, ?, ?, ?, ?, ?, ?)`).run(message.id, message.agentId, message.role, message.content, message.sourceRunId, stringifyJson(message.auth), stringifyJson(message.metadata), toIso(message.createdAt));
152
+ return clonePlain(message);
153
+ }
154
+ async listMessages(agentId, options = {}) {
155
+ const params = [agentId];
156
+ let sql = "select * from heph_messages where agent_id = ? order by created_at desc, id desc";
157
+ if (options.limit !== void 0) {
158
+ sql += " limit ?";
159
+ params.push(options.limit);
160
+ }
161
+ return this.db.prepare(sql).all(...params).map((row) => rowToMessage(row)).reverse();
162
+ }
163
+ async appendInboxEvent(input) {
164
+ const now = /* @__PURE__ */ new Date();
165
+ const event = {
166
+ id: input.id ?? createInboxEventId(),
167
+ agentId: input.agentId,
168
+ type: input.input.type,
169
+ input: input.input,
170
+ status: "pending",
171
+ runId: null,
172
+ auth: input.auth ?? null,
173
+ metadata: input.metadata ?? {},
174
+ createdAt: now,
175
+ updatedAt: now,
176
+ claimedAt: null,
177
+ processedAt: null,
178
+ failedAt: null,
179
+ error: null
180
+ };
181
+ this.db.prepare(`insert into heph_inbox_events
182
+ (id, agent_id, type, input_json, status, run_id, auth_json, metadata_json, created_at, updated_at,
183
+ claimed_at, claim_lease_until, processed_at, failed_at, error_json, attempt)
184
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(event.id, event.agentId, event.type, stringifyJson(event.input), event.status, event.runId, stringifyJson(event.auth), stringifyJson(event.metadata), toIso(event.createdAt), toIso(event.updatedAt), toIsoOrNull(event.claimedAt), null, toIsoOrNull(event.processedAt), toIsoOrNull(event.failedAt), stringifyJson(event.error), 0);
185
+ return clonePlain(event);
186
+ }
187
+ async getInboxEvent(id) {
188
+ const row = this.db.prepare("select * from heph_inbox_events where id = ?").get(id);
189
+ return row ? rowToInboxEvent(row) : null;
190
+ }
191
+ async listInboxEvents(agentId, options = {}) {
192
+ const where = ["agent_id = ?"];
193
+ const params = [agentId];
194
+ if (options.status !== void 0) {
195
+ where.push("status = ?");
196
+ params.push(options.status);
197
+ }
198
+ if (options.types !== void 0 && options.types.length > 0) {
199
+ where.push(`type in (${options.types.map(() => "?").join(", ")})`);
200
+ params.push(...options.types);
201
+ }
202
+ let sql = `select * from heph_inbox_events where ${where.join(" and ")} order by created_at asc, id asc`;
203
+ if (options.limit !== void 0) {
204
+ sql += " limit ?";
205
+ params.push(options.limit);
206
+ }
207
+ return this.db.prepare(sql).all(...params).map((row) => rowToInboxEvent(row));
208
+ }
209
+ async claimPendingInboxEvents(agentId, options = {}) {
210
+ return this.db.transaction(() => {
211
+ const now = /* @__PURE__ */ new Date();
212
+ const leaseUntil = new Date(now.getTime() + 3e4);
213
+ const where = ["agent_id = ?", "(status = 'pending' or (status = 'processing' and claim_lease_until <= ?))"];
214
+ const params = [agentId, toIso(now)];
215
+ if (options.types !== void 0 && options.types.length > 0) {
216
+ where.push(`type in (${options.types.map(() => "?").join(", ")})`);
217
+ params.push(...options.types);
218
+ }
219
+ let sql = `select * from heph_inbox_events where ${where.join(" and ")} order by created_at asc, id asc`;
220
+ if (options.limit !== void 0) {
221
+ sql += " limit ?";
222
+ params.push(options.limit);
223
+ }
224
+ const rows = this.db.prepare(sql).all(...params);
225
+ const update = this.db.prepare(`update heph_inbox_events
226
+ set status = 'processing', claimed_at = ?, claim_lease_until = ?, updated_at = ?, attempt = attempt + 1
227
+ where id = ?`);
228
+ for (const row of rows) update.run(toIso(now), toIso(leaseUntil), toIso(now), row.id);
229
+ return rows.map((row) => ({
230
+ ...row,
231
+ status: "processing",
232
+ claimed_at: toIso(now),
233
+ claim_lease_until: toIso(leaseUntil),
234
+ updated_at: toIso(now),
235
+ attempt: row.attempt + 1
236
+ }));
237
+ })().map(rowToInboxEvent);
238
+ }
239
+ async markInboxEventsProcessed(ids, runId) {
240
+ return this.db.transaction(() => {
241
+ const now = /* @__PURE__ */ new Date();
242
+ const update = this.db.prepare(`update heph_inbox_events
243
+ set status = 'processed', run_id = ?, processed_at = ?, updated_at = ?, error_json = null
244
+ where id = ?`);
245
+ for (const id of ids) update.run(runId, toIso(now), toIso(now), id);
246
+ return ids.map((id) => {
247
+ const row = this.db.prepare("select * from heph_inbox_events where id = ?").get(id);
248
+ if (!row) throw notFound("InboxEvent not found", { inboxEventId: id });
249
+ return row;
250
+ });
251
+ })().map(rowToInboxEvent);
252
+ }
253
+ async markInboxEventsFailed(ids, error) {
254
+ return this.db.transaction(() => {
255
+ const now = /* @__PURE__ */ new Date();
256
+ const update = this.db.prepare(`update heph_inbox_events
257
+ set status = 'failed', failed_at = ?, updated_at = ?, error_json = ?
258
+ where id = ?`);
259
+ for (const id of ids) update.run(toIso(now), toIso(now), stringifyJson(error), id);
260
+ return ids.map((id) => {
261
+ const row = this.db.prepare("select * from heph_inbox_events where id = ?").get(id);
262
+ if (!row) throw notFound("InboxEvent not found", { inboxEventId: id });
263
+ return row;
264
+ });
265
+ })().map(rowToInboxEvent);
266
+ }
267
+ async appendRunEvent(input) {
268
+ return clonePlain(this.db.transaction(() => {
269
+ const seqRow = this.db.prepare("select coalesce(max(seq), 0) + 1 as seq from heph_run_events where run_id = ?").get(input.runId);
270
+ const event = {
271
+ id: input.id ?? createRunEventId(),
272
+ runId: input.runId,
273
+ seq: seqRow.seq,
274
+ type: input.type,
275
+ payload: input.payload ?? {},
276
+ sourceRefs: input.sourceRefs ?? [],
277
+ createdAt: /* @__PURE__ */ new Date()
278
+ };
279
+ this.db.prepare(`insert into heph_run_events
280
+ (id, run_id, seq, type, payload_json, source_refs_json, created_at)
281
+ values (?, ?, ?, ?, ?, ?, ?)`).run(event.id, event.runId, event.seq, event.type, stringifyJson(event.payload), stringifyJson(event.sourceRefs), toIso(event.createdAt));
282
+ return event;
283
+ })());
284
+ }
285
+ async listRunEvents(runId, options = {}) {
286
+ const params = [runId, options.after ?? 0];
287
+ let sql = "select * from heph_run_events where run_id = ? and seq > ? order by seq asc";
288
+ if (options.limit !== void 0) {
289
+ sql += " limit ?";
290
+ params.push(options.limit);
291
+ }
292
+ return this.db.prepare(sql).all(...params).map((row) => rowToRunEvent(row));
293
+ }
294
+ async putMemory(input) {
295
+ const now = /* @__PURE__ */ new Date();
296
+ const existing = input.id ? this.db.prepare("select * from heph_memory_items where id = ?").get(input.id) ?? null : null;
297
+ const item = {
298
+ id: input.id ?? createMemoryId(),
299
+ scope: input.scope,
300
+ kind: input.kind,
301
+ content: input.content,
302
+ sourceRefs: input.sourceRefs,
303
+ importance: input.importance ?? null,
304
+ confidence: input.confidence ?? null,
305
+ embeddingRef: input.embeddingRef ?? null,
306
+ expiresAt: input.expiresAt ?? null,
307
+ createdAt: existing ? parseDate(existing.created_at) : now,
308
+ updatedAt: now
309
+ };
310
+ this.db.prepare(`insert into heph_memory_items
311
+ (id, scope_type, scope_id, kind, content, source_refs_json, importance, confidence, embedding_ref, expires_at, created_at, updated_at)
312
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
313
+ on conflict(id) do update set
314
+ scope_type = excluded.scope_type,
315
+ scope_id = excluded.scope_id,
316
+ kind = excluded.kind,
317
+ content = excluded.content,
318
+ source_refs_json = excluded.source_refs_json,
319
+ importance = excluded.importance,
320
+ confidence = excluded.confidence,
321
+ embedding_ref = excluded.embedding_ref,
322
+ expires_at = excluded.expires_at,
323
+ updated_at = excluded.updated_at`).run(item.id, item.scope.type, item.scope.id, item.kind, item.content, stringifyJson(item.sourceRefs), item.importance, item.confidence, item.embeddingRef, toIsoOrNull(item.expiresAt), toIso(item.createdAt), toIso(item.updatedAt));
324
+ return clonePlain(item);
325
+ }
326
+ async searchMemory(input) {
327
+ const rows = this.db.prepare("select * from heph_memory_items").all();
328
+ const now = Date.now();
329
+ const query = input.query?.trim().toLowerCase();
330
+ const topK = input.topK ?? 8;
331
+ const scopes = input.scopes ?? [];
332
+ return rows.map(rowToMemoryItem).filter((item) => item.expiresAt === null || item.expiresAt.getTime() > now).filter((item) => scopes.length === 0 || scopes.some((scope) => sameScope(scope, item.scope))).map((item) => ({
333
+ item,
334
+ score: scoreMemory(item, query)
335
+ })).filter(({ score }) => score > 0 || !query).sort((a, b) => b.score - a.score || b.item.updatedAt.getTime() - a.item.updatedAt.getTime()).slice(0, topK).map(({ item }) => clonePlain(item));
336
+ }
337
+ async createMcpBinding(input) {
338
+ const now = /* @__PURE__ */ new Date();
339
+ const binding = {
340
+ id: input.id ?? createMcpBindingId(),
341
+ agentId: input.agentId,
342
+ capabilityId: input.capabilityId,
343
+ accountRef: input.accountRef ?? null,
344
+ allowTools: clonePlain(input.allowTools),
345
+ status: "active",
346
+ metadata: input.metadata ?? {},
347
+ createdAt: now,
348
+ updatedAt: now,
349
+ removedAt: null
350
+ };
351
+ this.db.prepare(`insert into heph_mcp_bindings
352
+ (id, agent_id, capability_id, account_ref, allow_tools_json, status, metadata_json, created_at, updated_at, removed_at)
353
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(binding.id, binding.agentId, binding.capabilityId, binding.accountRef, stringifyJson(binding.allowTools), binding.status, stringifyJson(binding.metadata), toIso(binding.createdAt), toIso(binding.updatedAt), toIsoOrNull(binding.removedAt));
354
+ return clonePlain(binding);
355
+ }
356
+ async getMcpBinding(id) {
357
+ const row = this.db.prepare("select * from heph_mcp_bindings where id = ?").get(id);
358
+ return row ? rowToMcpBinding(row) : null;
359
+ }
360
+ async listMcpBindings(agentId, options = {}) {
361
+ const params = [agentId];
362
+ let sql = "select * from heph_mcp_bindings where agent_id = ?";
363
+ const status = options.status ?? "active";
364
+ if (status !== "all") {
365
+ sql += " and status = ?";
366
+ params.push(status);
367
+ }
368
+ sql += " order by created_at asc, id asc";
369
+ return this.db.prepare(sql).all(...params).map((row) => rowToMcpBinding(row));
370
+ }
371
+ async removeMcpBinding(id) {
372
+ const existing = await this.getMcpBinding(id);
373
+ if (!existing) throw notFound("McpBinding not found", { bindingId: id });
374
+ const now = /* @__PURE__ */ new Date();
375
+ const removedAt = existing.removedAt ?? now;
376
+ this.db.prepare("update heph_mcp_bindings set status = 'removed', updated_at = ?, removed_at = ? where id = ?").run(toIso(now), toIso(removedAt), id);
377
+ return {
378
+ ...existing,
379
+ status: "removed",
380
+ updatedAt: now,
381
+ removedAt
382
+ };
383
+ }
384
+ async createSkillBinding(input) {
385
+ const now = /* @__PURE__ */ new Date();
386
+ const binding = {
387
+ id: input.id ?? createSkillBindingId(),
388
+ agentId: input.agentId,
389
+ skillId: input.skillId,
390
+ name: input.name,
391
+ version: input.version ?? null,
392
+ source: clonePlain(input.source),
393
+ allowReferences: input.allowReferences === void 0 ? [] : clonePlain(input.allowReferences),
394
+ status: "active",
395
+ metadata: input.metadata ?? {},
396
+ createdAt: now,
397
+ updatedAt: now,
398
+ removedAt: null
399
+ };
400
+ this.db.prepare(`insert into heph_skill_bindings
401
+ (id, agent_id, skill_id, name, version, source_json, allow_references_json, status,
402
+ metadata_json, created_at, updated_at, removed_at)
403
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(binding.id, binding.agentId, binding.skillId, binding.name, binding.version, stringifyJson(binding.source), stringifyJson(binding.allowReferences), binding.status, stringifyJson(binding.metadata), toIso(binding.createdAt), toIso(binding.updatedAt), toIsoOrNull(binding.removedAt));
404
+ return clonePlain(binding);
405
+ }
406
+ async getSkillBinding(id) {
407
+ const row = this.db.prepare("select * from heph_skill_bindings where id = ?").get(id);
408
+ return row ? rowToSkillBinding(row) : null;
409
+ }
410
+ async listSkillBindings(agentId, options = {}) {
411
+ const params = [agentId];
412
+ let sql = "select * from heph_skill_bindings where agent_id = ?";
413
+ const status = options.status ?? "active";
414
+ if (status !== "all") {
415
+ sql += " and status = ?";
416
+ params.push(status);
417
+ }
418
+ sql += " order by created_at asc, id asc";
419
+ return this.db.prepare(sql).all(...params).map((row) => rowToSkillBinding(row));
420
+ }
421
+ async removeSkillBinding(id) {
422
+ const existing = await this.getSkillBinding(id);
423
+ if (!existing) throw notFound("SkillBinding not found", { bindingId: id });
424
+ const now = /* @__PURE__ */ new Date();
425
+ const removedAt = existing.removedAt ?? now;
426
+ this.db.prepare("update heph_skill_bindings set status = 'removed', updated_at = ?, removed_at = ? where id = ?").run(toIso(now), toIso(removedAt), id);
427
+ return {
428
+ ...existing,
429
+ status: "removed",
430
+ updatedAt: now,
431
+ removedAt
432
+ };
433
+ }
434
+ async createApprovalRequest(input) {
435
+ const now = /* @__PURE__ */ new Date();
436
+ const request = {
437
+ id: input.id ?? createApprovalRequestId(),
438
+ agentId: input.agentId,
439
+ runId: input.runId,
440
+ toolId: input.toolId,
441
+ input: clonePlain(input.input),
442
+ status: "pending",
443
+ requestedBy: input.requestedBy ?? null,
444
+ decidedBy: null,
445
+ metadata: input.metadata ?? {},
446
+ createdAt: now,
447
+ updatedAt: now,
448
+ decidedAt: null
449
+ };
450
+ this.db.prepare(`insert into heph_approval_requests
451
+ (id, agent_id, run_id, tool_id, input_json, status, requested_by_json, decided_by_json,
452
+ metadata_json, created_at, updated_at, decided_at)
453
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(request.id, request.agentId, request.runId, request.toolId, stringifyJson(request.input), request.status, stringifyJson(request.requestedBy), stringifyJson(request.decidedBy), stringifyJson(request.metadata), toIso(request.createdAt), toIso(request.updatedAt), toIsoOrNull(request.decidedAt));
454
+ return clonePlain(request);
455
+ }
456
+ async getApprovalRequest(id) {
457
+ const row = this.db.prepare("select * from heph_approval_requests where id = ?").get(id);
458
+ return row ? rowToApprovalRequest(row) : null;
459
+ }
460
+ async listApprovalRequests(runId, options = {}) {
461
+ const params = [runId];
462
+ let sql = "select * from heph_approval_requests where run_id = ?";
463
+ const status = options.status ?? "all";
464
+ if (status !== "all") {
465
+ sql += " and status = ?";
466
+ params.push(status);
467
+ }
468
+ sql += " order by created_at asc, id asc";
469
+ return this.db.prepare(sql).all(...params).map((row) => rowToApprovalRequest(row));
470
+ }
471
+ async decideApprovalRequest(input) {
472
+ const existing = await this.getApprovalRequest(input.id);
473
+ if (!existing) throw notFound("ApprovalRequest not found", { approvalRequestId: input.id });
474
+ const now = /* @__PURE__ */ new Date();
475
+ const updated = {
476
+ ...existing,
477
+ status: input.decision,
478
+ decidedBy: input.decidedBy ?? null,
479
+ metadata: {
480
+ ...existing.metadata,
481
+ ...input.metadata ?? {}
482
+ },
483
+ updatedAt: now,
484
+ decidedAt: now
485
+ };
486
+ this.db.prepare(`update heph_approval_requests
487
+ set status = ?, decided_by_json = ?, metadata_json = ?, updated_at = ?, decided_at = ?
488
+ where id = ?`).run(updated.status, stringifyJson(updated.decidedBy), stringifyJson(updated.metadata), toIso(updated.updatedAt), toIsoOrNull(updated.decidedAt), input.id);
489
+ return clonePlain(updated);
490
+ }
491
+ async createDeferredToolOperation(input) {
492
+ const now = /* @__PURE__ */ new Date();
493
+ const operation = {
494
+ id: input.id ?? createDeferredToolOperationId(),
495
+ agentId: input.agentId,
496
+ runId: input.runId,
497
+ toolId: input.toolId,
498
+ toolCallId: input.toolCallId ?? null,
499
+ status: "pending",
500
+ resumePolicy: input.resumePolicy ?? "auto",
501
+ auth: input.auth ?? null,
502
+ result: null,
503
+ error: null,
504
+ metadata: input.metadata ?? {},
505
+ createdAt: now,
506
+ updatedAt: now,
507
+ completedAt: null
508
+ };
509
+ this.db.prepare(`insert into heph_deferred_tool_operations
510
+ (id, agent_id, run_id, tool_id, tool_call_id, status, resume_policy, auth_json,
511
+ result_json, error_json, metadata_json, created_at, updated_at, completed_at)
512
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(operation.id, operation.agentId, operation.runId, operation.toolId, operation.toolCallId, operation.status, operation.resumePolicy, stringifyJson(operation.auth), stringifyJson(operation.result), stringifyJson(operation.error), stringifyJson(operation.metadata), toIso(operation.createdAt), toIso(operation.updatedAt), toIsoOrNull(operation.completedAt));
513
+ return clonePlain(operation);
514
+ }
515
+ async getDeferredToolOperation(id) {
516
+ const row = this.db.prepare("select * from heph_deferred_tool_operations where id = ?").get(id);
517
+ return row ? rowToDeferredToolOperation(row) : null;
518
+ }
519
+ async completeDeferredToolOperation(input) {
520
+ const existing = await this.getDeferredToolOperation(input.id);
521
+ if (!existing) throw notFound("DeferredToolOperation not found", { operationId: input.id });
522
+ if (existing.status !== "pending") return clonePlain(existing);
523
+ const now = /* @__PURE__ */ new Date();
524
+ const updated = {
525
+ ...existing,
526
+ status: input.status,
527
+ result: input.result ?? null,
528
+ error: input.error ?? null,
529
+ metadata: {
530
+ ...existing.metadata,
531
+ ...input.metadata ?? {}
532
+ },
533
+ updatedAt: now,
534
+ completedAt: now
535
+ };
536
+ this.db.prepare(`update heph_deferred_tool_operations
537
+ set status = ?, result_json = ?, error_json = ?, metadata_json = ?, updated_at = ?, completed_at = ?
538
+ where id = ?`).run(updated.status, stringifyJson(updated.result), stringifyJson(updated.error), stringifyJson(updated.metadata), toIso(updated.updatedAt), toIsoOrNull(updated.completedAt), input.id);
539
+ return clonePlain(updated);
540
+ }
541
+ };
542
+ var SQLiteQueue = class {
543
+ db;
544
+ concurrency;
545
+ pollIntervalMs;
546
+ leaseMs;
547
+ retryDelayMs;
548
+ maxAttempts;
549
+ onError;
550
+ activeAgents = /* @__PURE__ */ new Set();
551
+ idleResolvers = /* @__PURE__ */ new Set();
552
+ activeCount = 0;
553
+ handler = null;
554
+ timer = null;
555
+ constructor(db, options = { databasePath: ":memory:" }) {
556
+ this.db = db;
557
+ this.concurrency = options.concurrency ?? 4;
558
+ this.pollIntervalMs = options.pollIntervalMs ?? 50;
559
+ this.leaseMs = options.leaseMs ?? 3e4;
560
+ this.retryDelayMs = options.retryDelayMs ?? 1e3;
561
+ this.maxAttempts = options.maxAttempts ?? 5;
562
+ this.onError = options.onError ?? ((error, job) => {
563
+ console.error("Unhandled Heph SQLite queue error", {
564
+ error,
565
+ job
566
+ });
567
+ });
568
+ }
569
+ close() {
570
+ if (this.timer) {
571
+ clearTimeout(this.timer);
572
+ this.timer = null;
573
+ }
574
+ this.db.close();
575
+ }
576
+ async enqueue(job, options = {}) {
577
+ const now = /* @__PURE__ */ new Date();
578
+ const availableAt = new Date(now.getTime() + (options.delayMs ?? 0));
579
+ const idempotencyKey = options.idempotencyKey ?? null;
580
+ const id = idempotencyKey ?? `queue_${globalThis.crypto?.randomUUID?.() ?? `${Date.now()}_${Math.random()}`}`;
581
+ this.db.prepare(`insert into heph_queue_jobs
582
+ (id, idempotency_key, type, agent_id, run_id, job_json, status, attempt, max_attempts,
583
+ available_at, leased_until, created_at, updated_at, last_error_json)
584
+ values (?, ?, ?, ?, ?, ?, 'available', 0, ?, ?, null, ?, ?, null)
585
+ on conflict(id) do nothing`).run(id, idempotencyKey, job.type, job.agentId, "runId" in job ? job.runId : null, stringifyJson(job), this.maxAttempts, toIso(availableAt), toIso(now), toIso(now));
586
+ this.schedule(0);
587
+ }
588
+ async startConsumer(handler) {
589
+ this.handler = handler;
590
+ this.schedule(0);
591
+ }
592
+ async onIdle() {
593
+ if (this.isIdle()) return;
594
+ await new Promise((resolve) => {
595
+ this.idleResolvers.add(resolve);
596
+ });
597
+ }
598
+ schedule(delayMs = this.pollIntervalMs) {
599
+ if (!this.handler || this.timer) return;
600
+ this.timer = setTimeout(() => {
601
+ this.timer = null;
602
+ this.pump();
603
+ }, delayMs);
604
+ this.timer.unref?.();
605
+ }
606
+ pump() {
607
+ if (!this.handler) {
608
+ this.resolveIdleIfNeeded();
609
+ return;
610
+ }
611
+ while (this.activeCount < this.concurrency) {
612
+ const row = this.claimNextJob();
613
+ if (!row) break;
614
+ const job = parseJson(row.job_json);
615
+ this.activeCount += 1;
616
+ this.activeAgents.add(row.agent_id);
617
+ this.handler(job).then(() => {
618
+ this.markDone(row.id);
619
+ }).catch((error) => {
620
+ this.onError(error, job);
621
+ this.markRetryOrFailed(row, error);
622
+ }).finally(() => {
623
+ this.activeCount -= 1;
624
+ this.activeAgents.delete(row.agent_id);
625
+ this.resolveIdleIfNeeded();
626
+ this.schedule(0);
627
+ });
628
+ }
629
+ this.resolveIdleIfNeeded();
630
+ this.schedule();
631
+ }
632
+ claimNextJob() {
633
+ return this.db.transaction(() => {
634
+ const now = /* @__PURE__ */ new Date();
635
+ const row = this.db.prepare(`select * from heph_queue_jobs
636
+ where ((status = 'available' and available_at <= ?) or (status = 'leased' and leased_until <= ?))
637
+ and not exists (
638
+ select 1 from heph_queue_jobs active
639
+ where active.agent_id = heph_queue_jobs.agent_id
640
+ and active.status = 'leased'
641
+ and active.leased_until > ?
642
+ )
643
+ order by available_at asc, id asc
644
+ limit 50`).all(toIso(now), toIso(now), toIso(now)).find((candidate) => !this.activeAgents.has(candidate.agent_id));
645
+ if (!row) return null;
646
+ const leasedUntil = new Date(now.getTime() + this.leaseMs);
647
+ this.db.prepare(`update heph_queue_jobs
648
+ set status = 'leased', attempt = attempt + 1, leased_until = ?, updated_at = ?
649
+ where id = ?`).run(toIso(leasedUntil), toIso(now), row.id);
650
+ return {
651
+ ...row,
652
+ status: "leased",
653
+ attempt: row.attempt + 1,
654
+ leased_until: toIso(leasedUntil),
655
+ updated_at: toIso(now)
656
+ };
657
+ })();
658
+ }
659
+ markDone(id) {
660
+ this.db.prepare("update heph_queue_jobs set status = 'done', updated_at = ? where id = ?").run(toIso(/* @__PURE__ */ new Date()), id);
661
+ }
662
+ markRetryOrFailed(row, error) {
663
+ const now = /* @__PURE__ */ new Date();
664
+ const status = row.attempt >= row.max_attempts ? "failed" : "available";
665
+ const availableAt = row.attempt >= row.max_attempts ? row.available_at : toIso(new Date(now.getTime() + this.retryDelayMs));
666
+ this.db.prepare(`update heph_queue_jobs
667
+ set status = ?, available_at = ?, leased_until = null, updated_at = ?, last_error_json = ?
668
+ where id = ?`).run(status, availableAt, toIso(now), stringifyJson(toErrorDetails(error)), row.id);
669
+ }
670
+ isIdle() {
671
+ if (this.activeCount > 0) return false;
672
+ return this.db.prepare("select count(*) as count from heph_queue_jobs where status in ('available', 'leased')").get().count === 0;
673
+ }
674
+ resolveIdleIfNeeded() {
675
+ if (!this.isIdle()) return;
676
+ for (const resolve of this.idleResolvers) resolve();
677
+ this.idleResolvers.clear();
678
+ }
679
+ };
680
+ function openDatabase(options) {
681
+ if (options.databasePath !== ":memory:") mkdirSync(dirname(options.databasePath), { recursive: true });
682
+ const db = new BetterSqlite3(options.databasePath);
683
+ db.pragma("journal_mode = WAL");
684
+ db.pragma("foreign_keys = ON");
685
+ db.pragma("busy_timeout = 5000");
686
+ handleMigrations(db, options.migrations);
687
+ return db;
688
+ }
689
+ function handleMigrations(db, options) {
690
+ const mode = options?.mode ?? "apply";
691
+ if (mode === "none") return;
692
+ if (mode === "write_out") {
693
+ writeSQLiteMigration(options?.outputDir ?? DEFAULT_MIGRATION_OUTPUT_DIR);
694
+ return;
695
+ }
696
+ db.exec(INITIAL_MIGRATION_SQL);
697
+ ensureSQLiteSchemaCompatibility(db);
698
+ db.prepare(`insert into heph_schema_migrations (version, name, applied_at)
699
+ values (?, ?, ?)
700
+ on conflict(version) do nothing`).run(INITIAL_MIGRATION_VERSION, "Initial Heph SQLite schema", toIso(/* @__PURE__ */ new Date()));
701
+ }
702
+ function ensureSQLiteSchemaCompatibility(db) {
703
+ const runColumns = db.prepare("pragma table_info(heph_runs)").all();
704
+ if (!runColumns.some((column) => column.name === "skill_manifest_json")) db.exec("alter table heph_runs add column skill_manifest_json text");
705
+ if (!runColumns.some((column) => column.name === "tool_manifest_json")) db.exec("alter table heph_runs add column tool_manifest_json text");
706
+ db.exec(DEFERRED_TOOL_OPERATIONS_SQL);
707
+ }
708
+ function rowToAgentSession(row) {
709
+ return {
710
+ id: row.id,
711
+ agentSpecId: row.agent_spec_id,
712
+ agentSpecVersion: row.agent_spec_version,
713
+ state: parseJson(row.state_json),
714
+ activeRunId: row.active_run_id,
715
+ auth: parseNullableJson(row.auth_json),
716
+ metadata: parseJson(row.metadata_json),
717
+ createdAt: parseDate(row.created_at),
718
+ updatedAt: parseDate(row.updated_at)
719
+ };
720
+ }
721
+ function rowToRun(row) {
722
+ return {
723
+ id: row.id,
724
+ agentId: row.agent_id,
725
+ agentSpecId: row.agent_spec_id,
726
+ agentSpecVersion: row.agent_spec_version,
727
+ status: row.status,
728
+ input: parseJson(row.input_json),
729
+ auth: parseNullableJson(row.auth_json),
730
+ contextManifest: parseNullableJson(row.context_manifest_json),
731
+ skillManifest: parseSkillManifest(row.skill_manifest_json),
732
+ toolManifest: parseToolManifest(row.tool_manifest_json),
733
+ error: parseNullableJson(row.error_json),
734
+ metadata: parseJson(row.metadata_json),
735
+ queuedAt: parseDate(row.queued_at),
736
+ startedAt: parseNullableDate(row.started_at),
737
+ completedAt: parseNullableDate(row.completed_at),
738
+ createdAt: parseDate(row.created_at),
739
+ updatedAt: parseDate(row.updated_at)
740
+ };
741
+ }
742
+ function rowToMessage(row) {
743
+ return {
744
+ id: row.id,
745
+ agentId: row.agent_id,
746
+ role: row.role,
747
+ content: row.content,
748
+ sourceRunId: row.source_run_id,
749
+ auth: parseNullableJson(row.auth_json),
750
+ metadata: parseJson(row.metadata_json),
751
+ createdAt: parseDate(row.created_at)
752
+ };
753
+ }
754
+ function rowToInboxEvent(row) {
755
+ return {
756
+ id: row.id,
757
+ agentId: row.agent_id,
758
+ type: row.type,
759
+ input: parseJson(row.input_json),
760
+ status: row.status,
761
+ runId: row.run_id,
762
+ auth: parseNullableJson(row.auth_json),
763
+ metadata: parseJson(row.metadata_json),
764
+ createdAt: parseDate(row.created_at),
765
+ updatedAt: parseDate(row.updated_at),
766
+ claimedAt: parseNullableDate(row.claimed_at),
767
+ processedAt: parseNullableDate(row.processed_at),
768
+ failedAt: parseNullableDate(row.failed_at),
769
+ error: parseNullableJson(row.error_json)
770
+ };
771
+ }
772
+ function rowToRunEvent(row) {
773
+ return {
774
+ id: row.id,
775
+ runId: row.run_id,
776
+ seq: row.seq,
777
+ type: row.type,
778
+ payload: parseJson(row.payload_json),
779
+ sourceRefs: parseJson(row.source_refs_json),
780
+ createdAt: parseDate(row.created_at)
781
+ };
782
+ }
783
+ function rowToMemoryItem(row) {
784
+ return {
785
+ id: row.id,
786
+ scope: {
787
+ type: row.scope_type,
788
+ id: row.scope_id
789
+ },
790
+ kind: row.kind,
791
+ content: row.content,
792
+ sourceRefs: parseJson(row.source_refs_json),
793
+ importance: row.importance,
794
+ confidence: row.confidence,
795
+ embeddingRef: row.embedding_ref,
796
+ expiresAt: parseNullableDate(row.expires_at),
797
+ createdAt: parseDate(row.created_at),
798
+ updatedAt: parseDate(row.updated_at)
799
+ };
800
+ }
801
+ function rowToMcpBinding(row) {
802
+ return {
803
+ id: row.id,
804
+ agentId: row.agent_id,
805
+ capabilityId: row.capability_id,
806
+ accountRef: row.account_ref,
807
+ allowTools: parseJson(row.allow_tools_json),
808
+ status: row.status,
809
+ metadata: parseJson(row.metadata_json),
810
+ createdAt: parseDate(row.created_at),
811
+ updatedAt: parseDate(row.updated_at),
812
+ removedAt: parseNullableDate(row.removed_at)
813
+ };
814
+ }
815
+ function rowToSkillBinding(row) {
816
+ return {
817
+ id: row.id,
818
+ agentId: row.agent_id,
819
+ skillId: row.skill_id,
820
+ name: row.name,
821
+ version: row.version,
822
+ source: parseJson(row.source_json),
823
+ allowReferences: parseJson(row.allow_references_json),
824
+ status: row.status,
825
+ metadata: parseJson(row.metadata_json),
826
+ createdAt: parseDate(row.created_at),
827
+ updatedAt: parseDate(row.updated_at),
828
+ removedAt: parseNullableDate(row.removed_at)
829
+ };
830
+ }
831
+ function rowToApprovalRequest(row) {
832
+ return {
833
+ id: row.id,
834
+ agentId: row.agent_id,
835
+ runId: row.run_id,
836
+ toolId: row.tool_id,
837
+ input: parseNullableJson(row.input_json),
838
+ status: row.status,
839
+ requestedBy: parseNullableJson(row.requested_by_json),
840
+ decidedBy: parseNullableJson(row.decided_by_json),
841
+ metadata: parseJson(row.metadata_json),
842
+ createdAt: parseDate(row.created_at),
843
+ updatedAt: parseDate(row.updated_at),
844
+ decidedAt: parseNullableDate(row.decided_at)
845
+ };
846
+ }
847
+ function rowToDeferredToolOperation(row) {
848
+ return {
849
+ id: row.id,
850
+ agentId: row.agent_id,
851
+ runId: row.run_id,
852
+ toolId: row.tool_id,
853
+ toolCallId: row.tool_call_id,
854
+ status: row.status,
855
+ resumePolicy: row.resume_policy,
856
+ auth: parseNullableJson(row.auth_json),
857
+ result: parseNullableJson(row.result_json),
858
+ error: parseNullableJson(row.error_json),
859
+ metadata: parseJson(row.metadata_json),
860
+ createdAt: parseDate(row.created_at),
861
+ updatedAt: parseDate(row.updated_at),
862
+ completedAt: parseNullableDate(row.completed_at)
863
+ };
864
+ }
865
+ function scoreMemory(item, query) {
866
+ const importance = item.importance ?? .5;
867
+ const confidence = item.confidence ?? .5;
868
+ const content = item.content.toLowerCase();
869
+ const queryScore = query && content.includes(query) ? 2 : 0;
870
+ return importance + confidence + queryScore;
871
+ }
872
+ function sameScope(a, b) {
873
+ return a.type === b.type && a.id === b.id;
874
+ }
875
+ function notFound(message, details) {
876
+ return new HephError({
877
+ code: "HEPH5001",
878
+ title: message,
879
+ message,
880
+ status: 404,
881
+ details
882
+ });
883
+ }
884
+ function toErrorDetails(error) {
885
+ if (error instanceof Error) return {
886
+ name: error.name,
887
+ message: error.message
888
+ };
889
+ return { message: String(error) };
890
+ }
891
+ function stringifyJson(value) {
892
+ return value === null ? null : JSON.stringify(value);
893
+ }
894
+ function parseJson(value) {
895
+ return JSON.parse(value);
896
+ }
897
+ function parseNullableJson(value) {
898
+ return value === null ? null : parseJson(value);
899
+ }
900
+ function toIso(value) {
901
+ return value.toISOString();
902
+ }
903
+ function toIsoOrNull(value) {
904
+ return value ? value.toISOString() : null;
905
+ }
906
+ function parseDate(value) {
907
+ return new Date(value);
908
+ }
909
+ function parseNullableDate(value) {
910
+ return value ? parseDate(value) : null;
911
+ }
912
+ function parseToolManifest(value) {
913
+ const manifest = parseNullableJson(value);
914
+ if (!manifest) return null;
915
+ return {
916
+ ...manifest,
917
+ createdAt: new Date(manifest.createdAt)
918
+ };
919
+ }
920
+ function parseSkillManifest(value) {
921
+ const manifest = parseNullableJson(value);
922
+ if (!manifest) return null;
923
+ return {
924
+ ...manifest,
925
+ createdAt: new Date(manifest.createdAt)
926
+ };
927
+ }
928
+ function clonePlain(value) {
929
+ return structuredClone(value);
930
+ }
931
+ const DEFERRED_TOOL_OPERATIONS_SQL = `
932
+ create table if not exists heph_deferred_tool_operations (
933
+ id text primary key,
934
+ agent_id text not null,
935
+ run_id text not null,
936
+ tool_id text not null,
937
+ tool_call_id text,
938
+ status text not null,
939
+ resume_policy text not null,
940
+ auth_json text,
941
+ result_json text,
942
+ error_json text,
943
+ metadata_json text not null,
944
+ created_at text not null,
945
+ updated_at text not null,
946
+ completed_at text
947
+ );
948
+
949
+ create index if not exists idx_heph_deferred_tool_operations_run on heph_deferred_tool_operations(run_id, created_at);
950
+ create index if not exists idx_heph_deferred_tool_operations_agent_status on heph_deferred_tool_operations(agent_id, status, created_at);
951
+ `;
952
+ const INITIAL_MIGRATION_SQL = `
953
+ create table if not exists heph_schema_migrations (
954
+ version text primary key,
955
+ name text not null,
956
+ applied_at text not null
957
+ );
958
+
959
+ create table if not exists heph_agent_sessions (
960
+ id text primary key,
961
+ agent_spec_id text not null,
962
+ agent_spec_version text,
963
+ state_json text not null,
964
+ active_run_id text,
965
+ auth_json text,
966
+ metadata_json text not null,
967
+ created_at text not null,
968
+ updated_at text not null
969
+ );
970
+
971
+ create index if not exists idx_heph_agent_sessions_spec on heph_agent_sessions(agent_spec_id);
972
+
973
+ create table if not exists heph_runs (
974
+ id text primary key,
975
+ agent_id text not null,
976
+ agent_spec_id text not null,
977
+ agent_spec_version text,
978
+ status text not null,
979
+ input_json text not null,
980
+ auth_json text,
981
+ context_manifest_json text,
982
+ skill_manifest_json text,
983
+ tool_manifest_json text,
984
+ error_json text,
985
+ metadata_json text not null,
986
+ queued_at text not null,
987
+ started_at text,
988
+ completed_at text,
989
+ created_at text not null,
990
+ updated_at text not null
991
+ );
992
+
993
+ create index if not exists idx_heph_runs_agent_created on heph_runs(agent_id, created_at);
994
+ create index if not exists idx_heph_runs_status on heph_runs(status);
995
+
996
+ create table if not exists heph_messages (
997
+ id text primary key,
998
+ agent_id text not null,
999
+ role text not null,
1000
+ content text not null,
1001
+ source_run_id text,
1002
+ auth_json text,
1003
+ metadata_json text not null,
1004
+ created_at text not null
1005
+ );
1006
+
1007
+ create index if not exists idx_heph_messages_agent_created on heph_messages(agent_id, created_at);
1008
+
1009
+ create table if not exists heph_inbox_events (
1010
+ id text primary key,
1011
+ agent_id text not null,
1012
+ type text not null,
1013
+ input_json text not null,
1014
+ status text not null,
1015
+ run_id text,
1016
+ auth_json text,
1017
+ metadata_json text not null,
1018
+ created_at text not null,
1019
+ updated_at text not null,
1020
+ claimed_at text,
1021
+ claim_lease_until text,
1022
+ processed_at text,
1023
+ failed_at text,
1024
+ error_json text,
1025
+ attempt integer not null default 0
1026
+ );
1027
+
1028
+ create index if not exists idx_heph_inbox_agent_status_created on heph_inbox_events(agent_id, status, created_at);
1029
+ create index if not exists idx_heph_inbox_lease on heph_inbox_events(status, claim_lease_until);
1030
+
1031
+ create table if not exists heph_run_events (
1032
+ id text primary key,
1033
+ run_id text not null,
1034
+ seq integer not null,
1035
+ type text not null,
1036
+ payload_json text not null,
1037
+ source_refs_json text not null,
1038
+ created_at text not null,
1039
+ unique(run_id, seq)
1040
+ );
1041
+
1042
+ create index if not exists idx_heph_run_events_run_seq on heph_run_events(run_id, seq);
1043
+
1044
+ create table if not exists heph_memory_items (
1045
+ id text primary key,
1046
+ scope_type text not null,
1047
+ scope_id text not null,
1048
+ kind text not null,
1049
+ content text not null,
1050
+ source_refs_json text not null,
1051
+ importance real,
1052
+ confidence real,
1053
+ embedding_ref text,
1054
+ expires_at text,
1055
+ created_at text not null,
1056
+ updated_at text not null
1057
+ );
1058
+
1059
+ create index if not exists idx_heph_memory_scope on heph_memory_items(scope_type, scope_id);
1060
+ create index if not exists idx_heph_memory_expires on heph_memory_items(expires_at);
1061
+
1062
+ create table if not exists heph_mcp_bindings (
1063
+ id text primary key,
1064
+ agent_id text not null,
1065
+ capability_id text not null,
1066
+ account_ref text,
1067
+ allow_tools_json text not null,
1068
+ status text not null,
1069
+ metadata_json text not null,
1070
+ created_at text not null,
1071
+ updated_at text not null,
1072
+ removed_at text
1073
+ );
1074
+
1075
+ create index if not exists idx_heph_mcp_bindings_agent_status on heph_mcp_bindings(agent_id, status, created_at);
1076
+ create index if not exists idx_heph_mcp_bindings_capability on heph_mcp_bindings(capability_id);
1077
+
1078
+ create table if not exists heph_skill_bindings (
1079
+ id text primary key,
1080
+ agent_id text not null,
1081
+ skill_id text not null,
1082
+ name text not null,
1083
+ version text,
1084
+ source_json text not null,
1085
+ allow_references_json text not null,
1086
+ status text not null,
1087
+ metadata_json text not null,
1088
+ created_at text not null,
1089
+ updated_at text not null,
1090
+ removed_at text
1091
+ );
1092
+
1093
+ create index if not exists idx_heph_skill_bindings_agent_status on heph_skill_bindings(agent_id, status, created_at);
1094
+ create index if not exists idx_heph_skill_bindings_skill on heph_skill_bindings(skill_id);
1095
+
1096
+ create table if not exists heph_approval_requests (
1097
+ id text primary key,
1098
+ agent_id text not null,
1099
+ run_id text not null,
1100
+ tool_id text not null,
1101
+ input_json text,
1102
+ status text not null,
1103
+ requested_by_json text,
1104
+ decided_by_json text,
1105
+ metadata_json text not null,
1106
+ created_at text not null,
1107
+ updated_at text not null,
1108
+ decided_at text
1109
+ );
1110
+
1111
+ create index if not exists idx_heph_approval_requests_run_status on heph_approval_requests(run_id, status, created_at);
1112
+ create index if not exists idx_heph_approval_requests_agent on heph_approval_requests(agent_id, created_at);
1113
+
1114
+ ${DEFERRED_TOOL_OPERATIONS_SQL}
1115
+
1116
+ create table if not exists heph_queue_jobs (
1117
+ id text primary key,
1118
+ idempotency_key text unique,
1119
+ type text not null,
1120
+ agent_id text not null,
1121
+ run_id text,
1122
+ job_json text not null,
1123
+ status text not null,
1124
+ attempt integer not null default 0,
1125
+ max_attempts integer not null,
1126
+ available_at text not null,
1127
+ leased_until text,
1128
+ created_at text not null,
1129
+ updated_at text not null,
1130
+ last_error_json text
1131
+ );
1132
+
1133
+ create index if not exists idx_heph_queue_available on heph_queue_jobs(status, available_at);
1134
+ create index if not exists idx_heph_queue_lease on heph_queue_jobs(status, leased_until);
1135
+ create index if not exists idx_heph_queue_agent on heph_queue_jobs(agent_id, status);
1136
+ `;
1137
+ //#endregion
1138
+ export { SQLiteHephStore, SQLiteQueue, createSQLiteAdapters, createSQLiteHephStore, createSQLiteQueue, writeSQLiteMigration };
1139
+
1140
+ //# sourceMappingURL=sqlite.mjs.map