UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

403 lines • 15.9 kB
import { ensureDatabaseSchema, qualifiedName, qualifiedSql, validateSchema, } from '../stores/db-schema.js'; import { forDialect } from '../stores/lucid-schema.js'; import { isMySql } from '../stores/sql-dialect.js'; export class InMemoryPoppyConnectionStore { #connections = new Map(); #pending = new Map(); async listConnections(owner) { return [...this.#connections.values()].filter((c) => c.owner === owner).map((c) => ({ ...c })); } async getConnection(owner, issuer) { const found = this.#connections.get(`${owner}\n${issuer}`); return found ? { ...found } : null; } async saveConnection(record) { this.#connections.set(`${record.owner}\n${record.issuer}`, { ...record }); } async deleteConnection(owner, issuer) { this.#connections.delete(`${owner}\n${issuer}`); } async createPending(record) { if (!this.#pending.has(record.id)) this.#pending.set(record.id, structuredClone(record)); } async getPending(id) { const found = this.#pending.get(id); return found ? structuredClone(found) : null; } async updatePending(id, patch) { const found = this.#pending.get(id); if (found) Object.assign(found, patch); } async listSealed() { const out = []; for (const c of this.#connections.values()) { out.push({ ref: { table: 'connection', owner: c.owner, issuer: c.issuer }, value: c.state }); } for (const p of this.#pending.values()) { if (p.flow !== null) out.push({ ref: { table: 'pending', id: p.id }, value: p.flow }); } return out; } async replaceSealed(ref, from, to) { if (ref.table === 'connection') { const found = this.#connections.get(`${ref.owner}\n${ref.issuer}`); if (found?.state !== from) return false; found.state = to; return true; } const found = this.#pending.get(ref.id); if (found?.flow !== from) return false; found.flow = to; return true; } } export const POPPY_CONNECTION_TABLES = { connections: 'agent_poppy_connection', pending: 'agent_poppy_connect', }; export function poppyConnectionTableStatements(schema) { const t = POPPY_CONNECTION_TABLES; return [ `CREATE TABLE IF NOT EXISTS ${qualifiedSql(t.connections, schema)} ( "owner" VARCHAR(255) NOT NULL, "issuer" VARCHAR(1024) NOT NULL, "domain" VARCHAR(255) NOT NULL, "organization" VARCHAR(255) NOT NULL, "scope" TEXT NOT NULL, "status" VARCHAR(16) NOT NULL, "state" TEXT NOT NULL, "created_at" BIGINT NOT NULL, "updated_at" BIGINT NOT NULL, PRIMARY KEY ("owner", "issuer") )`, `CREATE TABLE IF NOT EXISTS ${qualifiedSql(t.pending, schema)} ( "id" VARCHAR(64) PRIMARY KEY NOT NULL, "owner" VARCHAR(255) NOT NULL, "domain" VARCHAR(255) NOT NULL, "issuer" VARCHAR(1024) NOT NULL, "organization" VARCHAR(255) NOT NULL, "scope" TEXT NOT NULL, "actor" TEXT NULL, "thread_id" VARCHAR(255) NULL, "run_id" VARCHAR(255) NULL, "tool_call_id" VARCHAR(255) NULL, "flow" TEXT NULL, "status" VARCHAR(16) NOT NULL, "expires_at" BIGINT NOT NULL, "created_at" BIGINT NOT NULL )`, ]; } /** * The connection table's primary key on MySQL. `("owner", "issuer")` is 255 + 1024 utf8mb4 * characters — 5116 bytes, past InnoDB's 3072-byte key limit, so the table could not be created * there. MySQL keys on a stored SHA-256 of the issuer instead, computed by the database itself * (`issuer_hash`, a generated column the store never writes): 32 bytes, and unique exactly when the * issuer's bytes are, as on Postgres and SQLite. Reads still filter on `issuer`. A prefix index would * have fit but made two issuers sharing their first characters collide. * * No install needs repairing: the old statement always failed on MySQL, so no table of that shape * was ever created there by this library. */ function withMySqlConnectionKey(statement) { return statement.replace('PRIMARY KEY ("owner", "issuer")', '"issuer_hash" BINARY(32) AS (UNHEX(SHA2("issuer", 256))) STORED,\n PRIMARY KEY ("owner", "issuer_hash")'); } /** * Create the connection tables if missing. Idempotent. `schema` puts them in that database schema * (created when missing); refused on SQLite. */ export async function ensurePoppyConnectionTables(db, options = {}) { const schema = validateSchema(db, options.schema, 'config/poppy_connections.ts'); const mysql = isMySql(db); if (schema !== undefined) await ensureDatabaseSchema(db, schema); for (const statement of poppyConnectionTableStatements(schema)) { await db.rawQuery(forDialect(mysql ? withMySqlConnectionKey(statement) : statement, mysql)); } } const int = (v) => (typeof v === 'number' ? v : Number(v ?? 0)); const str = (v) => (v == null ? null : String(v)); /** {@link PoppyConnectionStore} over Lucid (SQLite / Postgres / MySQL); tables created on first use. */ export class LucidPoppyConnectionStore { db; #ready = null; #autoCreate; #schema; /** The tables as the query builder names them (`schema.table` under a `schema`). */ #tables; constructor(db, options = {}) { this.db = db; this.#autoCreate = options.autoCreateTables ?? true; this.#schema = validateSchema(db, options.schema, 'config/poppy_connections.ts'); this.#tables = { connections: qualifiedName(POPPY_CONNECTION_TABLES.connections, this.#schema), pending: qualifiedName(POPPY_CONNECTION_TABLES.pending, this.#schema), }; } ready() { if (!this.#autoCreate) return Promise.resolve(); if (this.#ready === null) { this.#ready = ensurePoppyConnectionTables(this.db, { schema: this.#schema }); this.#ready.catch(() => { this.#ready = null; }); } return this.#ready; } #connection(row) { return { owner: String(row.owner), issuer: String(row.issuer), domain: String(row.domain), organization: String(row.organization), scope: String(row.scope ?? ''), status: String(row.status), state: String(row.state), createdAt: int(row.created_at), updatedAt: int(row.updated_at), }; } async listConnections(owner) { await this.ready(); const rows = await this.db .from(this.#tables.connections) .where('owner', owner) .orderBy('domain', 'asc') .select('*'); return rows.map((row) => this.#connection(row)); } async getConnection(owner, issuer) { await this.ready(); const row = await this.db .from(this.#tables.connections) .where('owner', owner) .where('issuer', issuer) .first(); return row ? this.#connection(row) : null; } async saveConnection(record) { await this.ready(); const columns = { domain: record.domain, organization: record.organization, scope: record.scope, status: record.status, state: record.state, updated_at: record.updatedAt, }; const existing = await this.db .from(this.#tables.connections) .where('owner', record.owner) .where('issuer', record.issuer) .first(); if (existing) { await this.db .from(this.#tables.connections) .where('owner', record.owner) .where('issuer', record.issuer) .update(columns); return; } await this.db.table(this.#tables.connections).insert({ owner: record.owner, issuer: record.issuer, created_at: record.createdAt, ...columns, }); } async deleteConnection(owner, issuer) { await this.ready(); await this.db .from(this.#tables.connections) .where('owner', owner) .where('issuer', issuer) .delete(); } async createPending(record) { await this.ready(); if (await this.getPending(record.id)) return; await this.db.table(this.#tables.pending).insert({ id: record.id, owner: record.owner, domain: record.domain, issuer: record.issuer, organization: record.organization, scope: record.scope, actor: record.actor === null ? null : JSON.stringify(record.actor), thread_id: record.threadId, run_id: record.runId, tool_call_id: record.toolCallId, flow: record.flow, status: record.status, expires_at: record.expiresAt, created_at: record.createdAt, }); } async getPending(id) { await this.ready(); const row = await this.db.from(this.#tables.pending).where('id', id).first(); if (!row) return null; return { id: String(row.id), owner: String(row.owner), domain: String(row.domain), issuer: String(row.issuer), organization: String(row.organization), scope: String(row.scope ?? ''), actor: row.actor == null ? null : JSON.parse(String(row.actor)), threadId: str(row.thread_id), runId: str(row.run_id), toolCallId: str(row.tool_call_id), flow: str(row.flow), status: String(row.status), expiresAt: int(row.expires_at), createdAt: int(row.created_at), }; } async updatePending(id, patch) { await this.ready(); const row = {}; if (patch.flow !== undefined) row.flow = patch.flow; if (patch.status !== undefined) row.status = patch.status; if (Object.keys(row).length === 0) return; await this.db.from(this.#tables.pending).where('id', id).update(row); } async listSealed() { await this.ready(); const connections = await this.db .from(this.#tables.connections) .select('owner', 'issuer', 'state'); const pending = (await this.db.from(this.#tables.pending).select('id', 'flow')).filter((row) => row.flow != null); return [ ...connections.map((row) => ({ ref: { table: 'connection', owner: String(row.owner), issuer: String(row.issuer) }, value: String(row.state), })), ...pending.map((row) => ({ ref: { table: 'pending', id: String(row.id) }, value: String(row.flow), })), ]; } async replaceSealed(ref, from, to) { await this.ready(); const affected = ref.table === 'connection' ? await this.db .from(this.#tables.connections) .where('owner', ref.owner) .where('issuer', ref.issuer) .where('state', from) .update({ state: to }) : await this.db .from(this.#tables.pending) .where('id', ref.id) .where('flow', from) .update({ flow: to }); return affectedRows(affected) > 0; } } /** knex answers an update with a count (or, on some drivers, an array). */ function affectedRows(result) { if (typeof result === 'number') return result; if (Array.isArray(result)) return typeof result[0] === 'number' ? result[0] : result.length; return Number(result ?? 0); } /** {@link PoppyConnectionStore} over Redis: JSON values, an index set per owner. */ export function redisPoppyConnectionStore(redis, options = {}) { const p = options.prefix ?? 'agent:poppy-connections:'; const key = (owner, issuer) => `${p}conn:${owner}\n${issuer}`; const index = (owner) => `${p}owner:${owner}`; const pendingKey = (id) => `${p}pending:${id}`; const get = async (k) => { const raw = await redis.get(k); return raw === null ? null : JSON.parse(raw); }; return { async listConnections(owner) { const issuers = await redis.smembers(index(owner)); const found = await Promise.all(issuers.map((i) => get(key(owner, i)))); return found.filter((c) => c !== null); }, getConnection: (owner, issuer) => get(key(owner, issuer)), async saveConnection(record) { await redis.set(key(record.owner, record.issuer), JSON.stringify(record)); await redis.sadd(index(record.owner), record.issuer); }, async deleteConnection(owner, issuer) { await redis.del(key(owner, issuer)); await redis.srem(index(owner), issuer); }, async createPending(record) { const ttl = Math.max(1, Math.ceil((record.expiresAt - Date.now()) / 1000) + 3600); await redis.set(pendingKey(record.id), JSON.stringify(record), 'EX', ttl, 'NX'); }, getPending: (id) => get(pendingKey(id)), async updatePending(id, patch) { const found = await get(pendingKey(id)); if (!found) return; await redis.set(pendingKey(id), JSON.stringify({ ...found, ...patch }), 'KEEPTTL'); }, ...(typeof redis.scan === 'function' ? { async listSealed() { const out = []; for (const k of await scanKeys(`${p}conn:*`)) { const c = await get(k); if (c) out.push({ ref: { table: 'connection', owner: c.owner, issuer: c.issuer }, value: c.state, }); } for (const k of await scanKeys(`${p}pending:*`)) { const found = await get(k); if (found?.flow) out.push({ ref: { table: 'pending', id: found.id }, value: found.flow }); } return out; }, } : {}), // Read, compare, write: not atomic, so a value written between the two is lost only if it // was written in that instant; `agent:crypto:reseal` is an operator command, not a hot path. async replaceSealed(ref, from, to) { if (ref.table === 'connection') { const found = await get(key(ref.owner, ref.issuer)); if (found?.state !== from) return false; await redis.set(key(ref.owner, ref.issuer), JSON.stringify({ ...found, state: to })); return true; } const found = await get(pendingKey(ref.id)); if (found?.flow !== from) return false; await redis.set(pendingKey(ref.id), JSON.stringify({ ...found, flow: to }), 'KEEPTTL'); return true; }, }; async function scanKeys(pattern) { const keys = []; let cursor = '0'; do { const [next, batch] = await redis.scan(cursor, 'MATCH', pattern, 'COUNT', 200); keys.push(...batch); cursor = next; } while (cursor !== '0'); return [...new Set(keys)]; } } //# sourceMappingURL=store.js.map