The per-session destination map was being written as a sidecar JSON file (/workspace/.nanoclaw-destinations.json) — inconsistent with the rest of v2, where all host↔container IO goes through inbound.db / outbound.db. Move it into a `destinations` table in INBOUND_SCHEMA. The host writes it before every container wake AND on demand (e.g. after create_agent) so the creator sees the new child destination mid-session without a restart. The container queries the table live on every lookup — no cache, no staleness window. - src/db/schema.ts: add `destinations` table to INBOUND_SCHEMA. - src/session-manager.ts: writeDestinationsFile → writeDestinations, writes via DELETE + INSERT inside a transaction. - src/delivery.ts: create_agent handler calls writeDestinations on the creator's session after inserting the new destination rows. - container/agent-runner/src/destinations.ts: queries inbound.db directly in every findByName/getAllDestinations/findByRouting call. No more cache. No setDestinationsForTest (obsolete). No fs import. - container/agent-runner/src/index.ts and mcp-tools/index.ts: remove loadDestinations() calls — no longer needed. - Test helper initTestSessionDb creates the destinations table. Integration test inserts a row directly instead of mocking the cache. No backwards compatibility: sessions predating the schema update must be recreated. This is fine on the v2 branch. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
118 lines
4.4 KiB
TypeScript
118 lines
4.4 KiB
TypeScript
/**
|
|
* Destination map — lives in inbound.db's `destinations` table.
|
|
*
|
|
* The host writes this table before every container wake AND on demand
|
|
* (e.g. when a new child agent is created mid-session). The container
|
|
* queries the table live on every lookup, so admin changes take effect
|
|
* immediately — no restart required.
|
|
*
|
|
* This table is BOTH the routing map and the container-visible ACL.
|
|
* The host re-validates on the delivery side against the central DB,
|
|
* so even if this table is stale the host's enforcement is authoritative.
|
|
*/
|
|
import { getInboundDb } from './db/connection.js';
|
|
|
|
export interface DestinationEntry {
|
|
name: string;
|
|
displayName: string;
|
|
type: 'channel' | 'agent';
|
|
channelType?: string;
|
|
platformId?: string;
|
|
agentGroupId?: string;
|
|
}
|
|
|
|
interface DestRow {
|
|
name: string;
|
|
display_name: string | null;
|
|
type: 'channel' | 'agent';
|
|
channel_type: string | null;
|
|
platform_id: string | null;
|
|
agent_group_id: string | null;
|
|
}
|
|
|
|
function rowToEntry(row: DestRow): DestinationEntry {
|
|
return {
|
|
name: row.name,
|
|
displayName: row.display_name ?? row.name,
|
|
type: row.type,
|
|
channelType: row.channel_type ?? undefined,
|
|
platformId: row.platform_id ?? undefined,
|
|
agentGroupId: row.agent_group_id ?? undefined,
|
|
};
|
|
}
|
|
|
|
export function getAllDestinations(): DestinationEntry[] {
|
|
const rows = getInboundDb().prepare('SELECT * FROM destinations ORDER BY name').all() as DestRow[];
|
|
return rows.map(rowToEntry);
|
|
}
|
|
|
|
export function findByName(name: string): DestinationEntry | undefined {
|
|
const row = getInboundDb().prepare('SELECT * FROM destinations WHERE name = ?').get(name) as DestRow | undefined;
|
|
return row ? rowToEntry(row) : undefined;
|
|
}
|
|
|
|
/**
|
|
* Reverse lookup: given routing fields from an inbound message, find
|
|
* which destination they correspond to (what does this agent call the sender?).
|
|
*/
|
|
export function findByRouting(
|
|
channelType: string | null | undefined,
|
|
platformId: string | null | undefined,
|
|
): DestinationEntry | undefined {
|
|
if (!channelType || !platformId) return undefined;
|
|
const db = getInboundDb();
|
|
const row =
|
|
channelType === 'agent'
|
|
? (db
|
|
.prepare("SELECT * FROM destinations WHERE type = 'agent' AND agent_group_id = ?")
|
|
.get(platformId) as DestRow | undefined)
|
|
: (db
|
|
.prepare("SELECT * FROM destinations WHERE type = 'channel' AND channel_type = ? AND platform_id = ?")
|
|
.get(channelType, platformId) as DestRow | undefined);
|
|
return row ? rowToEntry(row) : undefined;
|
|
}
|
|
|
|
/** Generate the system-prompt addendum describing destinations and syntax. */
|
|
export function buildSystemPromptAddendum(): string {
|
|
const all = getAllDestinations();
|
|
|
|
if (all.length === 0) {
|
|
return [
|
|
'## Sending messages',
|
|
'',
|
|
'You currently have no configured destinations. You cannot send messages until an admin wires one up.',
|
|
].join('\n');
|
|
}
|
|
|
|
// Single-destination shortcut: the agent just writes its response normally.
|
|
if (all.length === 1) {
|
|
const d = all[0];
|
|
const label = d.displayName && d.displayName !== d.name ? ` (${d.displayName})` : '';
|
|
return [
|
|
'## Sending messages',
|
|
'',
|
|
`Your messages are delivered to \`${d.name}\`${label}. Just write your response directly — no special wrapping needed.`,
|
|
'',
|
|
'To mark something as scratchpad (logged but not sent), wrap it in `<internal>...</internal>`.',
|
|
'',
|
|
'To send a message mid-response (e.g., an acknowledgment before a long task), call the `send_message` MCP tool.',
|
|
].join('\n');
|
|
}
|
|
|
|
const lines = ['## Sending messages', '', 'You can send messages to the following destinations:', ''];
|
|
for (const d of all) {
|
|
const label = d.displayName && d.displayName !== d.name ? ` (${d.displayName})` : '';
|
|
lines.push(`- \`${d.name}\`${label}`);
|
|
}
|
|
lines.push('');
|
|
lines.push('To send a message, wrap it in a `<message to="name">...</message>` block.');
|
|
lines.push('You can include multiple `<message>` blocks in one response to send to multiple destinations.');
|
|
lines.push('Text outside of `<message>` blocks is scratchpad — logged but not sent anywhere.');
|
|
lines.push('Use `<internal>...</internal>` to make scratchpad intent explicit.');
|
|
lines.push('');
|
|
lines.push(
|
|
'To send a message mid-response (e.g., an acknowledgment before a long task), call the `send_message` MCP tool with the `to` parameter set to a destination name.',
|
|
);
|
|
return lines.join('\n');
|
|
}
|