#!/usr/bin/env node /** * Removes a context-window suffix (`[1m]`) from project model names. * * The gateway accepts both spellings and normalises them in its reply, so the suffix was never * meaningful to it — but it does reach the CLI as an unknown model name, and it makes two projects * look like they use different models. This rewrites the stored name, nothing else. * * Dry run by default; pass --apply to write. Only `project` entities are read. */ import { Client } from 'pg'; const SUFFIX = /\[[0-9]+[a-z]?\]$/i; const strip = value => (typeof value === 'string' ? value.replace(SUFFIX, '') : value); const apply = process.argv.includes('--apply'); if (!process.env.DATABASE_URL) { console.error('DATABASE_URL is required'); process.exit(1); } const client = new Client({ connectionString: process.env.DATABASE_URL }); await client.connect(); try { const { rows } = await client.query("SELECT tenant_id, id, version, data FROM entities WHERE kind = 'project'"); let changed = 0; for (const row of rows) { const models = Array.isArray(row.data?.models) ? row.data.models : []; const normalized = models.map(strip); if (JSON.stringify(models) === JSON.stringify(normalized)) continue; changed += 1; console.log(`${row.id}\n before: ${models.join(', ')}\n after: ${normalized.join(', ')}`); if (!apply) continue; // A compare-and-set on the version keeps a concurrent edit from being overwritten. const updated = await client.query( `UPDATE entities SET data = jsonb_set(data, '{models}', $1::jsonb), version = version + 1 WHERE tenant_id = $2 AND kind = 'project' AND id = $3 AND version = $4`, [JSON.stringify(normalized), row.tenant_id, row.id, row.version], ); if (updated.rowCount !== 1) console.error(` skipped: ${row.id} changed while this ran`); } console.log(`\n${changed} project(s) ${apply ? 'updated' : 'would be updated'}.`); } finally { await client.end(); }