Files
nanoclaw/scripts/delete-cli-agent.ts
T
gabi-simons d86051805b feat(setup): delete scratch agent after ping-pong, simplify flow
The "Terminal Agent" created for the connection test is now silently
deleted after a successful ping. If the user chooses to chat, a new
agent is auto-created as "{name}'s Terminal" — no name prompt needed.
Condensed the three-line ping section into a single "Connection verified."
status line.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-29 14:10:53 +00:00

75 lines
2.0 KiB
TypeScript

/**
* Delete the scratch CLI agent created during setup's ping-pong test.
*
* Removes the agent group, its messaging_group_agents wiring, any
* agent_destinations rows, and the groups/<folder>/ directory. Leaves the
* CLI messaging group intact so it can be reused for a new agent.
*
* Usage:
* pnpm exec tsx scripts/delete-cli-agent.ts --folder <folder-name>
*/
import fs from 'fs';
import path from 'path';
import { DATA_DIR } from '../src/config.js';
import { getAgentGroupByFolder, deleteAgentGroup } from '../src/db/agent-groups.js';
import { initDb } from '../src/db/connection.js';
import { runMigrations } from '../src/db/migrations/index.js';
interface Args {
folder: string;
}
function parseArgs(): Args {
const argv = process.argv.slice(2);
let folder = '';
for (let i = 0; i < argv.length; i++) {
if (argv[i] === '--folder' && argv[i + 1]) folder = argv[++i];
}
if (!folder) {
console.error('usage: pnpm exec tsx scripts/delete-cli-agent.ts --folder <folder-name>');
process.exit(1);
}
return { folder };
}
const args = parseArgs();
const db = initDb(path.join(DATA_DIR, 'v2.db'));
runMigrations(db);
const ag = getAgentGroupByFolder(args.folder);
if (!ag) {
console.log(`No agent group with folder "${args.folder}" — nothing to delete.`);
process.exit(0);
}
// Delete all rows referencing this agent group, in dependency order.
const fkTables = [
'messaging_group_agents',
'agent_destinations',
'agent_group_members',
'pending_sender_approvals',
'channel_registrations',
'user_roles',
'sessions',
];
for (const table of fkTables) {
const exists = db
.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?")
.get(table);
if (exists) {
db.prepare(`DELETE FROM ${table} WHERE agent_group_id = ?`).run(ag.id);
}
}
deleteAgentGroup(ag.id);
// Remove the groups/<folder>/ directory.
const groupDir = path.join(process.cwd(), 'groups', args.folder);
if (fs.existsSync(groupDir)) {
fs.rmSync(groupDir, { recursive: true });
}
console.log(`Deleted agent group ${ag.id} (${args.folder}).`);