← Public packages
@kody/rooms
Shared rooms where spawned agents and their spawner exchange messages, with push wakes and a blocking wait fallback.
Files
/** Minimal SQL surface shared by Kody `packageStorage()` and the test SQLite adapter. */
export type Db = {
sql(query: string, params?: Array<unknown>): Promise<{ rows: Array<Record<string, unknown>> }>
}
const schema = [
`CREATE TABLE IF NOT EXISTS rooms (
id TEXT PRIMARY KEY,
purpose TEXT,
creator_user_id TEXT NOT NULL,
creator_member_id TEXT,
status TEXT NOT NULL,
seq INTEGER NOT NULL DEFAULT 0,
caps_json TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
closed_at INTEGER
)`,
`CREATE INDEX IF NOT EXISTS rooms_creator ON rooms (creator_user_id, status)`,
`CREATE TABLE IF NOT EXISTS members (
id TEXT PRIMARY KEY,
room_id TEXT NOT NULL,
name TEXT NOT NULL,
role TEXT NOT NULL,
token_hash TEXT,
kody_user_id TEXT,
via TEXT NOT NULL,
joined_at INTEGER NOT NULL,
last_seen_at INTEGER NOT NULL,
listening_until INTEGER NOT NULL DEFAULT 0,
left_at INTEGER
)`,
`CREATE INDEX IF NOT EXISTS members_room ON members (room_id)`,
`CREATE UNIQUE INDEX IF NOT EXISTS members_token ON members (token_hash)`,
`CREATE TABLE IF NOT EXISTS invites (
code_hash TEXT PRIMARY KEY,
room_id TEXT NOT NULL,
created_by TEXT NOT NULL,
name_hint TEXT,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
used_at INTEGER,
used_by TEXT
)`,
`CREATE TABLE IF NOT EXISTS messages (
room_id TEXT NOT NULL,
seq INTEGER NOT NULL,
id TEXT NOT NULL,
at INTEGER NOT NULL,
from_id TEXT NOT NULL,
from_name TEXT NOT NULL,
kind TEXT NOT NULL,
body_json TEXT NOT NULL,
refs_json TEXT NOT NULL,
PRIMARY KEY (room_id, seq)
)`,
`CREATE INDEX IF NOT EXISTS messages_from ON messages (from_id, at)`,
`CREATE TABLE IF NOT EXISTS watches (
id TEXT PRIMARY KEY,
room_id TEXT NOT NULL,
member_id TEXT NOT NULL UNIQUE,
config_json TEXT NOT NULL,
sealed TEXT,
armed INTEGER NOT NULL DEFAULT 1,
fired_once INTEGER NOT NULL DEFAULT 0,
pending_count INTEGER NOT NULL DEFAULT 0,
last_fired_at INTEGER,
last_delivery_at INTEGER,
last_delivery_ok INTEGER,
consecutive_failures INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
)`,
`CREATE INDEX IF NOT EXISTS watches_room ON watches (room_id)`,
`CREATE TABLE IF NOT EXISTS deliveries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
watch_id TEXT NOT NULL,
room_id TEXT NOT NULL,
member_id TEXT NOT NULL,
target TEXT NOT NULL,
seq INTEGER NOT NULL,
at INTEGER NOT NULL,
ok INTEGER NOT NULL,
detail TEXT
)`,
`CREATE INDEX IF NOT EXISTS deliveries_member ON deliveries (member_id, id)`,
`CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value_json TEXT NOT NULL
)`,
]
const ready = new WeakSet<object>()
/** Create tables once per storage handle. */
export async function ensureSchema(db: Db) {
if (ready.has(db)) return
for (const statement of schema) await db.sql(statement)
ready.add(db)
}
export async function first(db: Db, query: string, params: Array<unknown> = []) {
return (await db.sql(query, params)).rows[0] ?? null
}
export const num = (value: unknown) => (value == null ? null : Number(value))
export const str = (value: unknown) => (value == null ? null : String(value))