v2 phase 1: foundation — types, DB layer, logging

Add the v2 data layer: typed interfaces, central DB with migration
runner, per-entity CRUD, and agent-runner session DB operations.

- src/log.ts: concise message-first logging API
- src/types-v2.ts: AgentGroup, MessagingGroup, Session, MessageIn/Out
- src/db/: connection (WAL), migration runner, 001-initial schema,
  CRUD for agent_groups, messaging_groups, sessions, pending_questions
- container/agent-runner/src/db/: session DB connection, messages_in
  reads + status transitions, messages_out writes
- 31 new tests, all 277 tests pass

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-04-08 23:34:09 +03:00
parent 90acff28ad
commit 3f0451b7b0
15 changed files with 1267 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
import type { MessagingGroup, MessagingGroupAgent } from '../types-v2.js';
import { getDb } from './connection.js';
// ── Messaging Groups ──
export function createMessagingGroup(group: MessagingGroup): void {
getDb()
.prepare(
`INSERT INTO messaging_groups (id, channel_type, platform_id, name, is_group, admin_user_id, created_at)
VALUES (@id, @channel_type, @platform_id, @name, @is_group, @admin_user_id, @created_at)`,
)
.run(group);
}
export function getMessagingGroup(id: string): MessagingGroup | undefined {
return getDb().prepare('SELECT * FROM messaging_groups WHERE id = ?').get(id) as MessagingGroup | undefined;
}
export function getMessagingGroupByPlatform(channelType: string, platformId: string): MessagingGroup | undefined {
return getDb()
.prepare('SELECT * FROM messaging_groups WHERE channel_type = ? AND platform_id = ?')
.get(channelType, platformId) as MessagingGroup | undefined;
}
export function getAllMessagingGroups(): MessagingGroup[] {
return getDb().prepare('SELECT * FROM messaging_groups ORDER BY name').all() as MessagingGroup[];
}
export function updateMessagingGroup(
id: string,
updates: Partial<Pick<MessagingGroup, 'name' | 'is_group' | 'admin_user_id'>>,
): void {
const fields: string[] = [];
const values: Record<string, unknown> = { id };
for (const [key, value] of Object.entries(updates)) {
if (value !== undefined) {
fields.push(`${key} = @${key}`);
values[key] = value;
}
}
if (fields.length === 0) return;
getDb()
.prepare(`UPDATE messaging_groups SET ${fields.join(', ')} WHERE id = @id`)
.run(values);
}
export function deleteMessagingGroup(id: string): void {
getDb().prepare('DELETE FROM messaging_groups WHERE id = ?').run(id);
}
// ── Messaging Group Agents ──
export function createMessagingGroupAgent(mga: MessagingGroupAgent): void {
getDb()
.prepare(
`INSERT INTO messaging_group_agents (id, messaging_group_id, agent_group_id, trigger_rules, response_scope, session_mode, priority, created_at)
VALUES (@id, @messaging_group_id, @agent_group_id, @trigger_rules, @response_scope, @session_mode, @priority, @created_at)`,
)
.run(mga);
}
export function getMessagingGroupAgents(messagingGroupId: string): MessagingGroupAgent[] {
return getDb()
.prepare('SELECT * FROM messaging_group_agents WHERE messaging_group_id = ? ORDER BY priority DESC')
.all(messagingGroupId) as MessagingGroupAgent[];
}
export function getMessagingGroupAgent(id: string): MessagingGroupAgent | undefined {
return getDb().prepare('SELECT * FROM messaging_group_agents WHERE id = ?').get(id) as
| MessagingGroupAgent
| undefined;
}
export function updateMessagingGroupAgent(
id: string,
updates: Partial<Pick<MessagingGroupAgent, 'trigger_rules' | 'response_scope' | 'session_mode' | 'priority'>>,
): void {
const fields: string[] = [];
const values: Record<string, unknown> = { id };
for (const [key, value] of Object.entries(updates)) {
if (value !== undefined) {
fields.push(`${key} = @${key}`);
values[key] = value;
}
}
if (fields.length === 0) return;
getDb()
.prepare(`UPDATE messaging_group_agents SET ${fields.join(', ')} WHERE id = @id`)
.run(values);
}
export function deleteMessagingGroupAgent(id: string): void {
getDb().prepare('DELETE FROM messaging_group_agents WHERE id = ?').run(id);
}