feat: make /transfer bidirectional across telegram and tui

This commit is contained in:
William Valentin
2026-02-18 07:55:08 -08:00
parent d48adbe0b0
commit 16af5e75fd
13 changed files with 262 additions and 17 deletions
+70
View File
@@ -213,6 +213,76 @@ describe('daemon command fast-path integration', () => {
expect(session.deleteConfig).toHaveBeenCalledWith('modelTier');
});
it('handles /transfer command via command fast-path and copies session to TUI', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
const transferSpy = vi.fn();
const session = {
id: 'telegram:user-1',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const commandRegistry = new CommandRegistry();
registerBuiltinCommands(commandRegistry);
const router = createMessageRouter({
sessionManager: {
getSession: vi.fn(() => session),
transferSession: transferSpy,
} as unknown as MessageRouterDeps['sessionManager'],
modelRouter: {
getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
getLabel: (tier: string) => tier,
} as unknown as MessageRouterDeps['modelRouter'],
systemPrompt: 'test prompt',
toolRegistry: {
clone() { return this; },
register: vi.fn(),
} as unknown as MessageRouterDeps['toolRegistry'],
toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
config: {
agents: {
primary_tier: 'default',
delegation: {
compaction: 'fast',
memory_extraction: 'fast',
classification: 'fast',
tool_summarisation: 'fast',
complex_reasoning: 'complex',
},
max_delegation_depth: 3,
max_iterations: 10,
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as unknown as MessageRouterDeps['config'],
commandRegistry,
});
const reply = vi.fn(async (_message: OutboundMessage) => {});
await router.handler({
id: 'm-transfer',
channel: 'telegram',
senderId: 'user-1',
text: '/transfer tui',
timestamp: Date.now(),
metadata: { isCommand: true, command: 'transfer', commandArgs: 'tui' },
} as MessageRouterInput, reply);
expect(processSpy).not.toHaveBeenCalled();
expect(transferSpy).toHaveBeenCalledWith('telegram', 'user-1', 'tui', 'local');
expect(reply).toHaveBeenCalledWith({
text: 'Session transferred to TUI (local)',
replyTo: 'm-transfer',
});
});
it('emits user.action audit events for channel messages', async () => {
const mockAuditLogger = {
userAction: vi.fn(),
+40 -1
View File
@@ -274,7 +274,7 @@ export function createMessageRouter(deps: {
tier: effectiveTier,
autonomyLevel: deps.config.agents.autonomy_level ?? 'standard',
sensitiveMode: deps.config.agents.sensitive_mode,
immutableDenylist: deps.config.agents.immutable_denylist.map((rule) => ({
immutableDenylist: (deps.config.agents.immutable_denylist ?? []).map((rule) => ({
tool: rule.tool,
argsPattern: rule.args_pattern,
reason: rule.reason,
@@ -838,6 +838,45 @@ export function createMessageRouter(deps: {
session.deleteConfig('queue.summarize_overflow');
return 'Reset session queue overrides.';
},
transferSession: (targetRaw: string) => {
const target = targetRaw.trim().toLowerCase();
if (!target) {
return 'Usage: /transfer <tui|telegram>';
}
let toFrontend: string;
let toUserId: string;
let destinationLabel: string;
if (target === 'tui') {
toFrontend = 'tui';
toUserId = 'local';
destinationLabel = 'TUI (local)';
} else if (target === 'telegram') {
if (msg.channel === 'telegram') {
toFrontend = 'telegram';
toUserId = msg.senderId;
} else {
const chatId = deps.config.telegram?.allowed_chat_ids?.[0];
if (chatId === undefined) {
return 'Telegram not configured';
}
toFrontend = 'telegram';
toUserId = String(chatId);
}
destinationLabel = `Telegram (${toUserId})`;
} else {
return `Unknown transfer target: ${target}. Supported targets: tui, telegram`;
}
if (msg.channel === toFrontend && msg.senderId === toUserId) {
return `Session is already active on ${destinationLabel}`;
}
deps.sessionManager.transferSession(msg.channel, msg.senderId, toFrontend, toUserId);
return `Session transferred to ${destinationLabel}`;
},
},
});