chore(rebase): realign duplicate backend/channel/schema files with main
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { configSchema } from '../config/schema.js';
|
||||
import { createConfiguredExternalBackend, createConfiguredExternalBackends, validateBackendConfig } from './index.js';
|
||||
|
||||
describe('createConfiguredExternalBackend', () => {
|
||||
const base = configSchema.parse({
|
||||
telegram: { bot_token: 'test', allowed_chat_ids: [1] },
|
||||
models: { default: { provider: 'anthropic', model: 'claude-3' } },
|
||||
});
|
||||
|
||||
it('returns undefined when no external backend is enabled', () => {
|
||||
const backend = createConfiguredExternalBackend(base);
|
||||
expect(backend).toBeUndefined();
|
||||
});
|
||||
|
||||
it('selects codex when enabled', () => {
|
||||
const cfg = {
|
||||
...base,
|
||||
backends: {
|
||||
...base.backends,
|
||||
codex: { enabled: true, path: '/usr/bin/codex', args: [], timeout_ms: 120000, retries: 0, retry_delay_ms: 300 },
|
||||
},
|
||||
};
|
||||
const backend = createConfiguredExternalBackend(cfg);
|
||||
expect(backend?.name).toBe('codex');
|
||||
});
|
||||
|
||||
it('selects gemini when enabled and higher-priority backends are disabled', () => {
|
||||
const cfg = {
|
||||
...base,
|
||||
backends: {
|
||||
...base.backends,
|
||||
gemini: { enabled: true, path: '/usr/bin/gemini', args: [], timeout_ms: 120000, retries: 0, retry_delay_ms: 300 },
|
||||
},
|
||||
};
|
||||
const backend = createConfiguredExternalBackend(cfg);
|
||||
expect(backend?.name).toBe('gemini');
|
||||
});
|
||||
|
||||
it('returns all enabled external backends and the default priority selection', () => {
|
||||
const cfg = {
|
||||
...base,
|
||||
backends: {
|
||||
...base.backends,
|
||||
codex: { enabled: true, path: '/usr/bin/codex', args: [], timeout_ms: 120000, retries: 0, retry_delay_ms: 300 },
|
||||
gemini: { enabled: true, path: '/usr/bin/gemini', args: [], timeout_ms: 120000, retries: 0, retry_delay_ms: 300 },
|
||||
},
|
||||
};
|
||||
const configured = createConfiguredExternalBackends(cfg);
|
||||
expect(configured.defaultName).toBe('codex');
|
||||
expect(configured.backends.codex?.name).toBe('codex');
|
||||
expect(configured.backends.gemini?.name).toBe('gemini');
|
||||
});
|
||||
|
||||
it('honors backends.default when that backend is enabled', () => {
|
||||
const cfg = {
|
||||
...base,
|
||||
backends: {
|
||||
...base.backends,
|
||||
default: 'gemini' as const,
|
||||
codex: { enabled: true, path: '/usr/bin/codex', args: [], timeout_ms: 120000, retries: 0, retry_delay_ms: 300 },
|
||||
gemini: { enabled: true, path: '/usr/bin/gemini', args: [], timeout_ms: 120000, retries: 0, retry_delay_ms: 300 },
|
||||
},
|
||||
};
|
||||
const configured = createConfiguredExternalBackends(cfg);
|
||||
expect(configured.defaultName).toBe('gemini');
|
||||
expect(configured.backends.codex?.name).toBe('codex');
|
||||
expect(configured.backends.gemini?.name).toBe('gemini');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateBackendConfig', () => {
|
||||
const base = configSchema.parse({
|
||||
telegram: { bot_token: 'test', allowed_chat_ids: [1] },
|
||||
models: { default: { provider: 'anthropic', model: 'claude-3' } },
|
||||
});
|
||||
|
||||
it('throws when no backend is enabled', () => {
|
||||
const cfg = {
|
||||
...base,
|
||||
backends: {
|
||||
...base.backends,
|
||||
native: { enabled: false },
|
||||
codex: { ...base.backends.codex, enabled: false },
|
||||
claude_code: { ...base.backends.claude_code, enabled: false },
|
||||
opencode: { ...base.backends.opencode, enabled: false },
|
||||
gemini: { ...base.backends.gemini, enabled: false },
|
||||
},
|
||||
};
|
||||
expect(() => validateBackendConfig(cfg)).toThrow('No backend enabled');
|
||||
});
|
||||
|
||||
it('throws when backends.default points to a disabled backend', () => {
|
||||
const cfg = {
|
||||
...base,
|
||||
backends: {
|
||||
...base.backends,
|
||||
default: 'gemini' as const,
|
||||
codex: { ...base.backends.codex, enabled: true },
|
||||
gemini: { ...base.backends.gemini, enabled: false },
|
||||
},
|
||||
};
|
||||
expect(() => validateBackendConfig(cfg)).toThrow('backends.default=gemini is not enabled');
|
||||
});
|
||||
});
|
||||
@@ -110,18 +110,7 @@ export interface StartDaemonOptions {
|
||||
configPath?: string;
|
||||
}
|
||||
|
||||
function validateUnsupportedConfig(config: Config): void {
|
||||
if (config.backends.claude_code.enabled) {
|
||||
throw new Error('backends.claude_code is not implemented yet. Set backends.claude_code.enabled=false.');
|
||||
}
|
||||
if (config.backends.opencode.enabled) {
|
||||
throw new Error('backends.opencode is not implemented yet. Set backends.opencode.enabled=false.');
|
||||
}
|
||||
}
|
||||
|
||||
export async function startDaemon(config: Config, options?: StartDaemonOptions): Promise<DaemonContext> {
|
||||
validateUnsupportedConfig(config);
|
||||
|
||||
// ── Log level ──
|
||||
setLogLevel(config.log_level);
|
||||
|
||||
|
||||
@@ -423,78 +423,6 @@ describe('daemon command fast-path integration', () => {
|
||||
expect(session.setConfig).toHaveBeenCalledWith('queue.mode', 'followup');
|
||||
});
|
||||
|
||||
it('includes external backend in status fast-path output', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
|
||||
const session = {
|
||||
id: 'telegram:user-status-backend',
|
||||
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),
|
||||
} 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,
|
||||
externalBackends: {
|
||||
codex: {
|
||||
name: 'codex',
|
||||
process: vi.fn(async () => 'unused'),
|
||||
},
|
||||
} as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultExternalBackendName: 'codex',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-status-backend',
|
||||
channel: 'telegram',
|
||||
senderId: 'user-status-backend',
|
||||
text: '/status',
|
||||
timestamp: Date.now(),
|
||||
metadata: { isCommand: true, command: 'status' },
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(processSpy).not.toHaveBeenCalled();
|
||||
const outbound = reply.mock.calls[0]?.[0] as OutboundMessage | undefined;
|
||||
expect(outbound?.text).toContain('Backend: codex');
|
||||
});
|
||||
|
||||
it('uses intent match to override agent target', async () => {
|
||||
const session = {
|
||||
id: 'telegram:user-2',
|
||||
@@ -1193,337 +1121,6 @@ describe('daemon auto-escalate integration', () => {
|
||||
expect(setModelTierSpy).toHaveBeenCalledWith('complex');
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'complex-tier response' }));
|
||||
});
|
||||
|
||||
it('falls back to native processing when external backend fails', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
|
||||
.mockResolvedValue('native fallback response');
|
||||
const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
|
||||
const session = {
|
||||
id: 'telegram:external-fail',
|
||||
addMessage: vi.fn((msg: { role: 'user' | 'assistant'; content: string }) => {
|
||||
history.push(msg);
|
||||
return msg;
|
||||
}),
|
||||
getHistory: vi.fn(() => [...history]),
|
||||
clear: vi.fn(),
|
||||
replaceHistory: vi.fn(),
|
||||
getConfig: vi.fn(() => undefined),
|
||||
setConfig: vi.fn(),
|
||||
deleteConfig: vi.fn(),
|
||||
};
|
||||
|
||||
const externalBackend = {
|
||||
name: 'codex',
|
||||
process: vi.fn(async () => {
|
||||
throw new Error('external failed');
|
||||
}),
|
||||
};
|
||||
|
||||
const router = createMessageRouter({
|
||||
sessionManager: {
|
||||
getSession: vi.fn(() => session),
|
||||
} 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,
|
||||
auto_escalate: false,
|
||||
},
|
||||
compaction: { enabled: false },
|
||||
models: { default: { provider: 'anthropic', model: 'claude' } },
|
||||
} as unknown as MessageRouterDeps['config'],
|
||||
externalBackends: { codex: externalBackend } as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultExternalBackendName: 'codex',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-external-fail',
|
||||
channel: 'telegram',
|
||||
senderId: 'external-fail',
|
||||
text: 'hello fallback',
|
||||
timestamp: Date.now(),
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(externalBackend.process).toHaveBeenCalled();
|
||||
expect(processSpy).toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native fallback response' }));
|
||||
});
|
||||
|
||||
it('fails over to another enabled external backend before native fallback', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
|
||||
.mockResolvedValue('native should not be used');
|
||||
const session = {
|
||||
id: 'telegram:external-failover',
|
||||
addMessage: vi.fn(),
|
||||
getHistory: vi.fn(() => []),
|
||||
clear: vi.fn(),
|
||||
replaceHistory: vi.fn(),
|
||||
getConfig: vi.fn(() => undefined),
|
||||
setConfig: vi.fn(),
|
||||
deleteConfig: vi.fn(),
|
||||
};
|
||||
|
||||
const codexBackend = {
|
||||
name: 'codex',
|
||||
process: vi.fn(async () => {
|
||||
throw new Error('codex failed');
|
||||
}),
|
||||
};
|
||||
const geminiBackend = {
|
||||
name: 'gemini',
|
||||
process: vi.fn(async () => 'gemini recovered'),
|
||||
};
|
||||
|
||||
const router = createMessageRouter({
|
||||
sessionManager: {
|
||||
getSession: vi.fn(() => session),
|
||||
} 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,
|
||||
auto_escalate: false,
|
||||
},
|
||||
compaction: { enabled: false },
|
||||
models: { default: { provider: 'anthropic', model: 'claude' } },
|
||||
} as unknown as MessageRouterDeps['config'],
|
||||
externalBackends: {
|
||||
codex: codexBackend,
|
||||
gemini: geminiBackend,
|
||||
} as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultExternalBackendName: 'codex',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-external-failover',
|
||||
channel: 'telegram',
|
||||
senderId: 'external-failover',
|
||||
text: 'hello failover',
|
||||
timestamp: Date.now(),
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(codexBackend.process).toHaveBeenCalled();
|
||||
expect(geminiBackend.process).toHaveBeenCalled();
|
||||
expect(processSpy).not.toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'gemini recovered' }));
|
||||
});
|
||||
|
||||
it('uses per-agent backend override instead of default external backend', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
|
||||
const session = {
|
||||
id: 'telegram:agent-backend-override',
|
||||
addMessage: vi.fn(),
|
||||
getHistory: vi.fn(() => []),
|
||||
clear: vi.fn(),
|
||||
replaceHistory: vi.fn(),
|
||||
getConfig: vi.fn(() => undefined),
|
||||
setConfig: vi.fn(),
|
||||
deleteConfig: vi.fn(),
|
||||
};
|
||||
|
||||
const codexBackend = {
|
||||
name: 'codex',
|
||||
process: vi.fn(async () => 'codex response'),
|
||||
};
|
||||
const geminiBackend = {
|
||||
name: 'gemini',
|
||||
process: vi.fn(async () => 'gemini response'),
|
||||
};
|
||||
|
||||
const agentConfigRegistry = new AgentConfigRegistry();
|
||||
agentConfigRegistry.loadFromConfig({
|
||||
coder: {
|
||||
model_tier: 'complex',
|
||||
backend: 'gemini',
|
||||
sandbox: false,
|
||||
},
|
||||
});
|
||||
const agentRouter = new AgentRouter({
|
||||
channels: { telegram: 'coder' },
|
||||
senders: {},
|
||||
});
|
||||
|
||||
const router = createMessageRouter({
|
||||
sessionManager: {
|
||||
getSession: vi.fn(() => session),
|
||||
} 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,
|
||||
auto_escalate: false,
|
||||
},
|
||||
compaction: { enabled: false },
|
||||
models: { default: { provider: 'anthropic', model: 'claude' } },
|
||||
} as unknown as MessageRouterDeps['config'],
|
||||
agentConfigRegistry,
|
||||
agentRouter,
|
||||
externalBackends: {
|
||||
codex: codexBackend,
|
||||
gemini: geminiBackend,
|
||||
} as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultExternalBackendName: 'codex',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-agent-backend-override',
|
||||
channel: 'telegram',
|
||||
senderId: 'user-agent-override',
|
||||
text: 'route to gemini backend',
|
||||
timestamp: Date.now(),
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(geminiBackend.process).toHaveBeenCalled();
|
||||
expect(codexBackend.process).not.toHaveBeenCalled();
|
||||
expect(processSpy).not.toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'gemini response' }));
|
||||
});
|
||||
|
||||
it('falls back to native when per-agent backend is configured but unavailable', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
|
||||
.mockResolvedValue('native response (missing backend)');
|
||||
const session = {
|
||||
id: 'telegram:missing-agent-backend',
|
||||
addMessage: vi.fn(),
|
||||
getHistory: vi.fn(() => []),
|
||||
clear: vi.fn(),
|
||||
replaceHistory: vi.fn(),
|
||||
getConfig: vi.fn(() => undefined),
|
||||
setConfig: vi.fn(),
|
||||
deleteConfig: vi.fn(),
|
||||
};
|
||||
|
||||
const agentConfigRegistry = new AgentConfigRegistry();
|
||||
agentConfigRegistry.loadFromConfig({
|
||||
coder: {
|
||||
model_tier: 'complex',
|
||||
backend: 'gemini',
|
||||
sandbox: false,
|
||||
},
|
||||
});
|
||||
const agentRouter = new AgentRouter({
|
||||
channels: { telegram: 'coder' },
|
||||
senders: {},
|
||||
});
|
||||
|
||||
const codexBackend = {
|
||||
name: 'codex',
|
||||
process: vi.fn(async () => 'codex response'),
|
||||
};
|
||||
|
||||
const router = createMessageRouter({
|
||||
sessionManager: {
|
||||
getSession: vi.fn(() => session),
|
||||
} 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,
|
||||
auto_escalate: false,
|
||||
},
|
||||
compaction: { enabled: false },
|
||||
models: { default: { provider: 'anthropic', model: 'claude' } },
|
||||
} as unknown as MessageRouterDeps['config'],
|
||||
agentConfigRegistry,
|
||||
agentRouter,
|
||||
externalBackends: {
|
||||
codex: codexBackend,
|
||||
} as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultExternalBackendName: 'codex',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-missing-agent-backend',
|
||||
channel: 'telegram',
|
||||
senderId: 'user-missing-backend',
|
||||
text: 'fall back to native',
|
||||
timestamp: Date.now(),
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(codexBackend.process).not.toHaveBeenCalled();
|
||||
expect(processSpy).toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'native response (missing backend)' }));
|
||||
});
|
||||
});
|
||||
|
||||
describe('daemon talk mode (voice wake) integration', () => {
|
||||
|
||||
Reference in New Issue
Block a user