Unify TUI slash commands and harden tool inventory responses
This commit is contained in:
@@ -1062,6 +1062,78 @@ describe('daemon external backend integration', () => {
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'external backend response' }));
|
||||
});
|
||||
|
||||
it('forces native processing for capability/tool inventory queries', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
|
||||
.mockResolvedValue('Available tools (authoritative):\n- file.read');
|
||||
const history: Array<{ role: 'user' | 'assistant'; content: string }> = [];
|
||||
const session = {
|
||||
id: 'telegram:external-tools-query',
|
||||
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 () => 'external backend 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,
|
||||
},
|
||||
compaction: { enabled: false },
|
||||
models: { default: { provider: 'anthropic', model: 'claude' } },
|
||||
} as unknown as MessageRouterDeps['config'],
|
||||
externalBackends: { codex: externalBackend } as unknown as MessageRouterDeps['externalBackends'],
|
||||
defaultName: 'codex',
|
||||
});
|
||||
|
||||
const reply = vi.fn(async (_message: OutboundMessage) => {});
|
||||
await router.handler({
|
||||
id: 'm-external-tools-query',
|
||||
channel: 'telegram',
|
||||
senderId: 'external-tools-query',
|
||||
text: 'check your available tools',
|
||||
timestamp: Date.now(),
|
||||
} as MessageRouterInput, reply);
|
||||
|
||||
expect(externalBackend.process).not.toHaveBeenCalled();
|
||||
expect(processSpy).toHaveBeenCalled();
|
||||
expect(reply).toHaveBeenCalledWith(expect.objectContaining({ text: 'Available tools (authoritative):\n- file.read' }));
|
||||
});
|
||||
|
||||
it('falls back to native processing when external backend fails', async () => {
|
||||
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process')
|
||||
.mockResolvedValue('native fallback response');
|
||||
|
||||
+36
-2
@@ -140,6 +140,24 @@ function parseResearchPrefix(text: string): string | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function shouldForceNativeForCapabilityQuery(text: string): boolean {
|
||||
const normalized = text.trim().toLowerCase();
|
||||
if (!normalized) {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
normalized.includes('available tools')
|
||||
|| normalized.includes('what tools')
|
||||
|| normalized.includes('which tools')
|
||||
|| normalized.includes('tool list')
|
||||
|| normalized.includes('list tools')
|
||||
|| normalized.includes('your tools')
|
||||
|| normalized.includes('what can you do')
|
||||
|| normalized.includes('can you do')
|
||||
|| normalized.includes('capabilities')
|
||||
);
|
||||
}
|
||||
|
||||
function isTtsEnabledForChannel(config: Config, channel: string): boolean {
|
||||
if (!config.tts?.enabled) {
|
||||
return false;
|
||||
@@ -649,6 +667,21 @@ export function createMessageRouter(deps: {
|
||||
: 'native';
|
||||
return `Flynn is running. Active model tier: ${agent.getModelTier()}. Backend: ${backend}`;
|
||||
},
|
||||
getTools: () => {
|
||||
const names = new Set(deps.toolRegistry.list().map((tool: Tool) => tool.name));
|
||||
names.add('media.send');
|
||||
if (deps.agentConfigRegistry && deps.agentConfigRegistry.list().length > 0) {
|
||||
names.add('agent.delegate');
|
||||
if (deps.config.councils?.enabled) {
|
||||
names.add('council.run');
|
||||
}
|
||||
}
|
||||
const sorted = [...names].sort();
|
||||
return [
|
||||
`Available tools (${sorted.length}):`,
|
||||
...sorted.map((name) => `- ${name}`),
|
||||
].join('\n');
|
||||
},
|
||||
getUsage: () => {
|
||||
const usage = agent.getUsage();
|
||||
const lines = [
|
||||
@@ -1260,11 +1293,12 @@ export function createMessageRouter(deps: {
|
||||
// buildUserMessage() in the agent will create native audio content parts
|
||||
|
||||
const requestedBackend = agentConfig?.backend ?? deps.defaultName;
|
||||
const forceNativeForCapabilityQuery = shouldForceNativeForCapabilityQuery(messageText);
|
||||
const sessionIdForAudit = `${msg.channel}:${msg.senderId}`;
|
||||
const selectedBackend = requestedBackend && requestedBackend !== 'native'
|
||||
? deps.externalBackends?.[requestedBackend]
|
||||
: undefined;
|
||||
const selectedBackendForAudit: 'native' | ExternalBackendName = selectedBackend && requestedBackend
|
||||
const selectedBackendForAudit: 'native' | ExternalBackendName = selectedBackend && requestedBackend && !forceNativeForCapabilityQuery
|
||||
? requestedBackend
|
||||
: 'native';
|
||||
|
||||
@@ -1280,7 +1314,7 @@ export function createMessageRouter(deps: {
|
||||
: 'native',
|
||||
});
|
||||
|
||||
if (selectedBackend && (!attachments || attachments.length === 0)) {
|
||||
if (selectedBackend && (!attachments || attachments.length === 0) && !forceNativeForCapabilityQuery) {
|
||||
try {
|
||||
const history = toExternalHistory(session.getHistory());
|
||||
session.addMessage({ role: 'user', content: messageText });
|
||||
|
||||
Reference in New Issue
Block a user