feat: add automation reactions event-trigger layer

This commit is contained in:
William Valentin
2026-02-18 10:26:40 -08:00
parent a71aa5992d
commit f341149ac7
10 changed files with 483 additions and 1 deletions
+136
View File
@@ -1272,6 +1272,142 @@ describe('daemon tts routing integration', () => {
});
});
describe('daemon reactions routing integration', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('rewrites automation event prompts when a reaction rule matches', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
const session = {
id: 'gmail:reaction-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 router = createMessageRouter({
sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
modelRouter: {
getAvailableTiers: () => ['default'],
getAllLabels: () => ({ default: 'default' }),
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: 'default',
memory_extraction: 'default',
classification: 'default',
tool_summarisation: 'default',
complex_reasoning: 'default',
},
max_delegation_depth: 1,
max_iterations: 3,
},
automation: {
reactions: [{
name: 'boss-email',
enabled: true,
on: ['gmail'],
filter: { contains: 'boss@company.com' },
run: 'Summarize and suggest next steps:\n\n{{text}}',
}],
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as unknown as MessageRouterDeps['config'],
});
await router.handler({
id: 'r1',
channel: 'gmail',
senderId: 'reaction-user-1',
text: 'New email from boss@company.com: Please share timeline',
timestamp: Date.now(),
} as MessageRouterInput, vi.fn(async (_message: OutboundMessage) => {}));
expect(processSpy).toHaveBeenCalledTimes(1);
const [prompt] = processSpy.mock.calls[0] ?? [];
expect(prompt).toBe(
'Summarize and suggest next steps:\n\nNew email from boss@company.com: Please share timeline',
);
});
it('keeps original prompt when no reaction rule matches', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
const session = {
id: 'gmail:reaction-user-2',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const router = createMessageRouter({
sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
modelRouter: {
getAvailableTiers: () => ['default'],
getAllLabels: () => ({ default: 'default' }),
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: 'default',
memory_extraction: 'default',
classification: 'default',
tool_summarisation: 'default',
complex_reasoning: 'default',
},
max_delegation_depth: 1,
max_iterations: 3,
},
automation: {
reactions: [{
name: 'boss-email',
enabled: true,
on: ['gmail'],
filter: { contains: 'boss@company.com' },
run: 'Summarize: {{text}}',
}],
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as unknown as MessageRouterDeps['config'],
});
await router.handler({
id: 'r2',
channel: 'gmail',
senderId: 'reaction-user-2',
text: 'New email from teammate@company.com: FYI',
timestamp: Date.now(),
} as MessageRouterInput, vi.fn(async (_message: OutboundMessage) => {}));
expect(processSpy).toHaveBeenCalledTimes(1);
const [prompt] = processSpy.mock.calls[0] ?? [];
expect(prompt).toBe('New email from teammate@company.com: FYI');
});
});
describe('daemon auto-escalate integration', () => {
afterEach(() => {
vi.restoreAllMocks();
+17
View File
@@ -21,6 +21,7 @@ import type { CommandRegistry } from '../commands/index.js';
import type { ComponentRegistry } from '../intents/index.js';
import type { RoutingPolicy } from '../routing/index.js';
import { createClientFromConfig } from './models.js';
import { matchReactionPrompt } from '../automation/reactions.js';
import type { SkillRegistry } from '../skills/index.js';
import { auditLogger } from '../audit/index.js';
import { randomUUID } from 'crypto';
@@ -373,6 +374,7 @@ export function createMessageRouter(deps: {
const handler = async (msg: InboundMessage, reply: (response: OutboundMessage) => Promise<void>): Promise<void> => {
let incomingText = msg.text;
let matchedReactionName: string | undefined;
const talkMode = deps.config.audio?.talk_mode;
if (talkMode?.enabled && incomingText.trim().length > 0) {
const key = `${msg.channel}:${msg.senderId}`;
@@ -422,6 +424,20 @@ export function createMessageRouter(deps: {
}
}
const automationReactions = deps.config.automation?.reactions ?? [];
if (!msg.metadata?.isCommand && automationReactions.length > 0) {
const reactionMatch = matchReactionPrompt(automationReactions, {
channel: msg.channel,
senderId: msg.senderId,
text: incomingText,
metadata: msg.metadata,
});
if (reactionMatch) {
matchedReactionName = reactionMatch.name;
incomingText = reactionMatch.prompt;
}
}
let intentAgentOverride: string | undefined;
let intentSkillOverride: string | undefined;
if (!deps.config.intents?.enabled && deps.agentConfigRegistry?.get('research')) {
@@ -475,6 +491,7 @@ export function createMessageRouter(deps: {
const effectiveMetadata = {
...(msg.metadata ?? {}),
...(intentSkillOverride ? { skillOverride: intentSkillOverride } : {}),
...(matchedReactionName ? { automationReaction: matchedReactionName } : {}),
};
const agentConfigName = intentAgentOverride ?? deps.agentRouter?.resolve(msg.channel, msg.senderId);