import { n as normalizeAgentId } from "./agent-id-CeT3w4ap.js";
import { Jt as getNodeSqliteKysely, Kt as executeSqliteQuerySync, i as openOpenClawStateDatabase, qt as executeSqliteQueryTakeFirstSync, s as runOpenClawStateWriteTransaction } from "./openclaw-state-db-Cb4e1jdd.js";
//#region src/state/agent-provenance.ts
const ensuredDatabases = /* @__PURE__ */ new WeakSet();
const AGENT_PROVENANCE_SCHEMA_SQL = `
CREATE TABLE IF NOT EXISTS agent_provenance (
  agent_id TEXT PRIMARY KEY,
  created_via TEXT NOT NULL CHECK (created_via IN ('operator', 'agent', 'claw')),
  creator_agent_id TEXT,
  created_at_ms INTEGER NOT NULL
) STRICT;
`;
function ensureAgentProvenanceSchema(options = {}) {
	const database = openOpenClawStateDatabase(options);
	if (ensuredDatabases.has(database.db)) return;
	runOpenClawStateWriteTransaction(({ db }) => {
		db.exec(AGENT_PROVENANCE_SCHEMA_SQL);
	}, options, { operationLabel: "agent-provenance.schema.ensure" });
	ensuredDatabases.add(database.db);
}
function fromRow(row) {
	let createdVia;
	switch (row.created_via) {
		case "operator":
		case "agent":
		case "claw":
			createdVia = row.created_via;
			break;
		default: throw new Error(`Invalid agent provenance created_via: ${row.created_via}`);
	}
	return {
		agentId: row.agent_id,
		createdVia,
		creatorAgentId: row.creator_agent_id,
		createdAtMs: row.created_at_ms
	};
}
function recordAgentProvenance(agentId, provenance, options = {}) {
	ensureAgentProvenanceSchema(options);
	const id = normalizeAgentId(agentId);
	const creatorAgentId = provenance.creatorAgentId ? normalizeAgentId(provenance.creatorAgentId) : null;
	const createdAtMs = options.nowMs ?? Date.now();
	runOpenClawStateWriteTransaction(({ db: sqlite }) => {
		executeSqliteQuerySync(sqlite, getNodeSqliteKysely(sqlite).insertInto("agent_provenance").values({
			agent_id: id,
			created_via: provenance.createdVia,
			creator_agent_id: creatorAgentId,
			created_at_ms: createdAtMs
		}).onConflict((conflict) => conflict.column("agent_id").doUpdateSet({
			created_via: provenance.createdVia,
			creator_agent_id: creatorAgentId,
			created_at_ms: createdAtMs
		})));
	}, options, { operationLabel: "agent-provenance.record" });
}
function readAgentProvenance(agentId, options = {}) {
	ensureAgentProvenanceSchema(options);
	const database = openOpenClawStateDatabase(options);
	const db = getNodeSqliteKysely(database.db);
	const row = executeSqliteQueryTakeFirstSync(database.db, db.selectFrom("agent_provenance").selectAll().where("agent_id", "=", normalizeAgentId(agentId)));
	return row ? fromRow(row) : void 0;
}
function listAgentProvenance(options = {}) {
	ensureAgentProvenanceSchema(options);
	const database = openOpenClawStateDatabase(options);
	const db = getNodeSqliteKysely(database.db);
	return executeSqliteQuerySync(database.db, db.selectFrom("agent_provenance").selectAll().orderBy("agent_id", "asc")).rows.map(fromRow);
}
/** Delete one row inside the caller's authoritative state transaction. */
function deleteAgentProvenanceForAgent(database, agentId) {
	executeSqliteQuerySync(database, getNodeSqliteKysely(database).deleteFrom("agent_provenance").where("agent_id", "=", normalizeAgentId(agentId)));
}
//#endregion
export { recordAgentProvenance as a, readAgentProvenance as i, ensureAgentProvenanceSchema as n, listAgentProvenance as r, deleteAgentProvenanceForAgent as t };
