mirror of
https://github.com/qwibitai/nanoclaw.git
synced 2026-06-04 10:14:47 +08:00
aec7ddd099
- shared.ts: parseJid now recognizes raw Baileys WhatsApp JIDs (`<id>@s.whatsapp.net`, `@g.us`, etc.); v2PlatformId returns the raw JID for whatsapp to match what the runtime adapter emits. Without this, every WhatsApp group in a v1 install was silently skipped. - discord-resolver.ts: new helper that uses DISCORD_BOT_TOKEN to look up channelId → guildId via the Discord API, since v1 stored only the channel id but v2 needs `discord:<guildId>:<channelId>`. Best-effort: on missing/invalid token or network error, returns empty resolver and the affected groups are skipped with the reason surfaced per channel. - db.ts, tasks.ts: route Discord groups through the resolver; other channels go through v2PlatformId unchanged. Resolver only built when at least one Discord group exists, so non-Discord installs incur no network. - db.ts: when every v1 group is skipped, exit non-zero with a FAIL line instead of `OK:groups=N,...,skipped=N`, so the wrapper doesn't hide total failure under a successful-looking summary. - migrate-v2.sh: run_step now surfaces ERROR: lines from successful steps (with count + first 3 + raw log path); phase 2c install loop populates STEP_RESULTS so install failures show in handoff.json instead of silently passing. - sessions.ts: copyTree skips dangling symlinks (e.g. v1's `.claude/debug/latest`) instead of crashing the entire step. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
209 lines
6.6 KiB
TypeScript
209 lines
6.6 KiB
TypeScript
/**
|
|
* migrate-v2 step: db
|
|
*
|
|
* Seed v2.db from v1's registered_groups table.
|
|
* Creates agent_groups, messaging_groups, and messaging_group_agents.
|
|
*
|
|
* Does NOT seed users/user_roles — the /migrate-from-v1 skill handles that.
|
|
*
|
|
* Idempotent: re-running skips rows that already exist.
|
|
*
|
|
* Usage: pnpm exec tsx setup/migrate-v2/db.ts <v1-path>
|
|
*/
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import Database from 'better-sqlite3';
|
|
|
|
import { DATA_DIR } from '../../src/config.js';
|
|
import { createAgentGroup, getAgentGroupByFolder } from '../../src/db/agent-groups.js';
|
|
import { initDb } from '../../src/db/connection.js';
|
|
import {
|
|
createMessagingGroup,
|
|
createMessagingGroupAgent,
|
|
getMessagingGroupAgentByPair,
|
|
getMessagingGroupByPlatform,
|
|
} from '../../src/db/messaging-groups.js';
|
|
import { runMigrations } from '../../src/db/migrations/index.js';
|
|
import { readEnvFile } from '../../src/env.js';
|
|
import { buildDiscordResolver, type DiscordResolver } from './discord-resolver.js';
|
|
import {
|
|
generateId,
|
|
parseJid,
|
|
triggerToEngage,
|
|
v2PlatformId,
|
|
} from './shared.js';
|
|
|
|
interface V1Group {
|
|
jid: string;
|
|
name: string;
|
|
folder: string;
|
|
trigger_pattern: string | null;
|
|
requires_trigger: number | null;
|
|
is_main: number | null;
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const v1Path = process.argv[2];
|
|
if (!v1Path) {
|
|
console.error('Usage: tsx setup/migrate-v2/db.ts <v1-path>');
|
|
process.exit(1);
|
|
}
|
|
|
|
const v1DbPath = path.join(v1Path, 'store', 'messages.db');
|
|
if (!fs.existsSync(v1DbPath)) {
|
|
console.error(`v1 DB not found: ${v1DbPath}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Read v1 groups
|
|
const v1Db = new Database(v1DbPath, { readonly: true, fileMustExist: true });
|
|
|
|
// v1 schema varies — channel_name was a late addition. Query only the
|
|
// columns we know exist in all v1 installs.
|
|
const v1Groups = v1Db
|
|
.prepare('SELECT jid, name, folder, trigger_pattern, requires_trigger, is_main FROM registered_groups')
|
|
.all() as V1Group[];
|
|
v1Db.close();
|
|
|
|
if (v1Groups.length === 0) {
|
|
console.log('SKIPPED:no registered groups in v1');
|
|
process.exit(0);
|
|
}
|
|
|
|
// Init v2 DB
|
|
fs.mkdirSync(path.join(process.cwd(), 'data'), { recursive: true });
|
|
const v2Db = initDb(path.join(DATA_DIR, 'v2.db'));
|
|
runMigrations(v2Db);
|
|
|
|
let created = 0;
|
|
let reused = 0;
|
|
let skipped = 0;
|
|
const errors: string[] = [];
|
|
|
|
// v1 stored Discord groups as `dc:<channelId>` (no guildId). v2 needs
|
|
// `discord:<guildId>:<channelId>`. If there are any Discord groups, use
|
|
// the bot token (carried forward by 1a-env) to look up each channel's
|
|
// guild via the Discord API. On any failure the resolver returns null
|
|
// for every channel and the affected groups skip with a clear warning.
|
|
let discordResolver: DiscordResolver | null = null;
|
|
const hasDiscord = v1Groups.some((g) => parseJid(g.jid)?.channel_type === 'discord');
|
|
if (hasDiscord) {
|
|
const env = readEnvFile(['DISCORD_BOT_TOKEN']);
|
|
discordResolver = await buildDiscordResolver(env.DISCORD_BOT_TOKEN ?? '');
|
|
const stats = discordResolver.stats();
|
|
if (stats.reason) {
|
|
console.log(`WARN:discord resolver disabled: ${stats.reason}`);
|
|
} else {
|
|
console.log(`INFO:discord resolver: ${stats.guilds} guild(s), ${stats.channels} channel(s)`);
|
|
}
|
|
}
|
|
|
|
for (const g of v1Groups) {
|
|
const parsed = parseJid(g.jid);
|
|
if (!parsed) {
|
|
skipped++;
|
|
errors.push(`Could not parse JID: ${g.jid}`);
|
|
continue;
|
|
}
|
|
|
|
const channelType = parsed.channel_type;
|
|
let platformId: string;
|
|
if (channelType === 'discord') {
|
|
const resolved = discordResolver?.resolve(parsed.id) ?? null;
|
|
if (!resolved) {
|
|
const stats = discordResolver?.stats();
|
|
const why = stats?.reason
|
|
? `discord resolver unavailable (${stats.reason})`
|
|
: 'not found in any guild the bot can see — re-add the bot to that server and re-run, or rewire after migration';
|
|
skipped++;
|
|
errors.push(`Discord channel ${parsed.id} (${g.folder}): ${why}`);
|
|
continue;
|
|
}
|
|
platformId = resolved;
|
|
} else {
|
|
platformId = v2PlatformId(channelType, parsed.raw);
|
|
}
|
|
const createdAt = new Date().toISOString();
|
|
|
|
try {
|
|
// agent_group — one per folder
|
|
let ag = getAgentGroupByFolder(g.folder);
|
|
if (!ag) {
|
|
createAgentGroup({
|
|
id: generateId('ag'),
|
|
name: g.name || g.folder,
|
|
folder: g.folder,
|
|
agent_provider: null,
|
|
created_at: createdAt,
|
|
});
|
|
ag = getAgentGroupByFolder(g.folder)!;
|
|
}
|
|
|
|
// messaging_group — one per (channel_type, platform_id)
|
|
let mg = getMessagingGroupByPlatform(channelType, platformId);
|
|
if (!mg) {
|
|
createMessagingGroup({
|
|
id: generateId('mg'),
|
|
channel_type: channelType,
|
|
platform_id: platformId,
|
|
name: g.name || null,
|
|
is_group: 1,
|
|
unknown_sender_policy: 'public',
|
|
created_at: createdAt,
|
|
});
|
|
mg = getMessagingGroupByPlatform(channelType, platformId)!;
|
|
}
|
|
|
|
// messaging_group_agents — wire them
|
|
const existing = getMessagingGroupAgentByPair(mg.id, ag.id);
|
|
if (!existing) {
|
|
const engage = triggerToEngage({
|
|
trigger_pattern: g.trigger_pattern,
|
|
requires_trigger: g.requires_trigger,
|
|
});
|
|
createMessagingGroupAgent({
|
|
id: generateId('mga'),
|
|
messaging_group_id: mg.id,
|
|
agent_group_id: ag.id,
|
|
engage_mode: engage.engage_mode,
|
|
engage_pattern: engage.engage_pattern,
|
|
sender_scope: 'all',
|
|
ignored_message_policy: 'drop',
|
|
session_mode: 'shared',
|
|
priority: 0,
|
|
created_at: createdAt,
|
|
});
|
|
created++;
|
|
} else {
|
|
reused++;
|
|
}
|
|
} catch (err) {
|
|
skipped++;
|
|
errors.push(`${g.folder}: ${err instanceof Error ? err.message : String(err)}`);
|
|
}
|
|
}
|
|
|
|
v2Db.close();
|
|
|
|
// If every group was skipped, the migration didn't actually do anything.
|
|
// Treat that as failure so the wrapper script surfaces it instead of
|
|
// hiding it under an `OK:` line.
|
|
const totalDone = created + reused;
|
|
if (v1Groups.length > 0 && totalDone === 0) {
|
|
console.error(`FAIL:groups=${v1Groups.length},created=0,reused=0,skipped=${skipped}`);
|
|
for (const e of errors) console.error(`ERROR:${e}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`OK:groups=${v1Groups.length},created=${created},reused=${reused},skipped=${skipped}`);
|
|
if (errors.length > 0) {
|
|
for (const e of errors) console.log(`ERROR:${e}`);
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(`FAIL:${err instanceof Error ? err.message : String(err)}`);
|
|
process.exit(1);
|
|
});
|