feat(db): conversations.findOrCreateForSpace for the ambient companion

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
root
2026-06-01 18:08:34 +10:00
parent cf0510872e
commit f80fd278a5
2 changed files with 43 additions and 0 deletions

View File

@@ -42,3 +42,20 @@ export async function setSummary(id, summary) {
);
return r;
}
export async function findOrCreateForSpace(space_id, agent_id, actor) {
const { rows: [existing] } = await pool.query(
`SELECT * FROM conversations
WHERE space_id=$1 AND agent_id=$2 AND status='open'
ORDER BY started_at DESC LIMIT 1`,
[space_id, agent_id]
);
if (existing) return existing;
const { rows: [r] } = await pool.query(
`INSERT INTO conversations(title, space_id, agent_id, metadata)
VALUES($1,$2,$3,$4) RETURNING *`,
['Companion', space_id, agent_id, {}]
);
await recordAudit(actor, 'create', 'conversation', r.id, null, r);
return r;
}