Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/app/src/electro-bridge/ipc/agent-host-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const AGENT_HOST_CHANNELS = {
ENQUEUE: "agent-host:enqueue",
READY: "agent-host:ready",
LIST_JOBS: "agent-host:list-jobs",
LIST_TASKS: "agent-host:list-tasks",
CONTROL_TASK: "agent-host:control-task",
REDIRECT_TASK: "agent-host:redirect-task",
CANCEL: "agent-host:cancel",
RESPOND: "agent-host:respond",
REQUEST: "agent-host:request",
Expand All @@ -26,6 +29,12 @@ export function createAgentHostAPI(
ready: () => invoke(AGENT_HOST_CHANNELS.READY),
enqueue: (dispatch) => invoke(AGENT_HOST_CHANNELS.ENQUEUE, dispatch),
listJobs: () => invoke(AGENT_HOST_CHANNELS.LIST_JOBS),
listTasks: (agentMemberId) =>
invoke(AGENT_HOST_CHANNELS.LIST_TASKS, agentMemberId),
controlTask: (taskId, action) =>
invoke(AGENT_HOST_CHANNELS.CONTROL_TASK, taskId, action),
redirectTask: (taskId, instruction) =>
invoke(AGENT_HOST_CHANNELS.REDIRECT_TASK, taskId, instruction),
cancel: (jobId) => invoke(AGENT_HOST_CHANNELS.CANCEL, jobId),
respond: (response) => invoke(AGENT_HOST_CHANNELS.RESPOND, response),
onRequest: (callback) => {
Expand Down
44 changes: 44 additions & 0 deletions packages/app/src/electro-bridge/ipc/agent-host-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function mainIPC() {

const dispatch: AgentHostDispatch = {
channelId: "channel",
channelKind: "channel",
conversationId: "conversation",
triggerMessageId: "message",
contextMessageIds: ["message"],
Expand Down Expand Up @@ -129,4 +130,47 @@ describe("Agent Host IPC", () => {
expect(host.enqueue).not.toHaveBeenCalled();
expect(bridge.respond).not.toHaveBeenCalled();
});

it("routes task controls through validated IPC methods", async () => {
const sender = new FakeWebContents();
const host = {
listTasks: vi.fn(async () => [{ id: "task-1" }]),
pauseTask: vi.fn(async () => true),
resumeTask: vi.fn(async () => true),
cancelTask: vi.fn(async () => true),
redirectTask: vi.fn(async () => ({ id: "job-2" })),
} as unknown as AgentHost;
const { handlers, ipc } = mainIPC();
setupAgentHostIPC(
{ host, getAllowedWebContents: () => sender as never },
ipc as never,
);

expect(
await handlers.get(AGENT_HOST_CHANNELS.LIST_TASKS)?.(
event(sender),
"agent:fizz" as never,
),
).toEqual({ success: true, tasks: [{ id: "task-1" }] });
expect(
await handlers.get(AGENT_HOST_CHANNELS.CONTROL_TASK)?.(
event(sender),
"task-1" as never,
"pause" as never,
),
).toEqual({ success: true, changed: true });
expect(
await handlers.get(AGENT_HOST_CHANNELS.REDIRECT_TASK)?.(
event(sender),
"task-1" as never,
"Show the diff first" as never,
),
).toEqual({ success: true, job: { id: "job-2" } });
expect(host.listTasks).toHaveBeenCalledWith("agent:fizz");
expect(host.pauseTask).toHaveBeenCalledWith("task-1");
expect(host.redirectTask).toHaveBeenCalledWith(
"task-1",
"Show the diff first",
);
});
});
98 changes: 98 additions & 0 deletions packages/app/src/electro-bridge/ipc/agent-host-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AgentHostDispatch,
AgentHostRendererResponse,
AgentHostTaskAction,
} from "@/shared/types/agent-host";
import type { AgentHost } from "@/electron/agent-host/host";
import type { AgentHostRendererBridge } from "@/electron/agent-host/renderer-bridge";
Expand Down Expand Up @@ -38,6 +39,9 @@ export function setupAgentHostIPC(
AGENT_HOST_CHANNELS.ENQUEUE,
AGENT_HOST_CHANNELS.READY,
AGENT_HOST_CHANNELS.LIST_JOBS,
AGENT_HOST_CHANNELS.LIST_TASKS,
AGENT_HOST_CHANNELS.CONTROL_TASK,
AGENT_HOST_CHANNELS.REDIRECT_TASK,
AGENT_HOST_CHANNELS.CANCEL,
AGENT_HOST_CHANNELS.RESPOND,
]) {
Expand Down Expand Up @@ -89,6 +93,96 @@ export function setupAgentHostIPC(
}
});

mainIPC.handle(
AGENT_HOST_CHANNELS.LIST_TASKS,
async (event, agentMemberId: unknown) => {
if (!allowed(event, options)) {
return {
success: false,
error: "Agent Host IPC sender is not allowed.",
};
}
if (!options.host) {
return { success: false, error: "Agent Host is unavailable." };
}
if (agentMemberId !== undefined && typeof agentMemberId !== "string") {
return { success: false, error: "Agent member id must be a string." };
}
try {
return {
success: true,
tasks: await options.host.listTasks(agentMemberId),
};
} catch (error) {
return { success: false, error: errorMessage(error) };
}
},
);

mainIPC.handle(
AGENT_HOST_CHANNELS.CONTROL_TASK,
async (event, taskId: unknown, action: unknown) => {
if (!allowed(event, options)) {
return {
success: false,
error: "Agent Host IPC sender is not allowed.",
};
}
if (!options.host) {
return { success: false, error: "Agent Host is unavailable." };
}
if (typeof taskId !== "string" || !taskId) {
return { success: false, error: "A task id is required." };
}
if (!isTaskAction(action)) {
return {
success: false,
error: "Task action must be pause, resume, or cancel.",
};
}
try {
const changed =
action === "pause"
? await options.host.pauseTask(taskId)
: action === "resume"
? await options.host.resumeTask(taskId)
: await options.host.cancelTask(taskId);
return { success: true, changed };
} catch (error) {
return { success: false, error: errorMessage(error) };
}
},
);

mainIPC.handle(
AGENT_HOST_CHANNELS.REDIRECT_TASK,
async (event, taskId: unknown, instruction: unknown) => {
if (!allowed(event, options)) {
return {
success: false,
error: "Agent Host IPC sender is not allowed.",
};
}
if (!options.host) {
return { success: false, error: "Agent Host is unavailable." };
}
if (typeof taskId !== "string" || !taskId) {
return { success: false, error: "A task id is required." };
}
if (typeof instruction !== "string") {
return { success: false, error: "Task guidance must be a string." };
}
try {
return {
success: true,
job: await options.host.redirectTask(taskId, instruction),
};
} catch (error) {
return { success: false, error: errorMessage(error) };
}
},
);

mainIPC.handle(AGENT_HOST_CHANNELS.CANCEL, async (event, jobId: unknown) => {
if (!allowed(event, options)) {
return { success: false, error: "Agent Host IPC sender is not allowed." };
Expand Down Expand Up @@ -128,3 +222,7 @@ export function setupAgentHostIPC(
},
);
}

function isTaskAction(value: unknown): value is AgentHostTaskAction {
return value === "pause" || value === "resume" || value === "cancel";
}
8 changes: 8 additions & 0 deletions packages/app/src/electron/agent-host/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { LocalAiAgentHostExecutor } from "./executor";

const job: AgentHostJob = {
id: "job",
taskId: "job",
channelId: "channel",
channelKind: "channel",
conversationId: "trusted-conversation",
triggerMessageId: "message",
contextMessageIds: ["message"],
Expand All @@ -18,6 +20,7 @@ const job: AgentHostJob = {
agentId: "trusted",
agentMemberId: "agent:trusted",
chain: { hops: 0, invoked: ["agent:trusted"] },
controlInstructions: [],
status: "running",
attempts: 1,
createdAt: new Date().toISOString(),
Expand Down Expand Up @@ -66,6 +69,11 @@ describe("LocalAiAgentHostExecutor", () => {
expect.objectContaining({
conversationId: "trusted-conversation",
concurrent: true,
agentHost: {
jobId: "job",
taskId: "job",
channelKind: "channel",
},
agent: { id: "trusted", memberId: "agent:trusted" },
}),
expect.any(Function),
Expand Down
5 changes: 5 additions & 0 deletions packages/app/src/electron/agent-host/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export class LocalAiAgentHostExecutor implements AgentHostExecutor {
...prepared.request,
conversationId: job.conversationId,
concurrent: true,
agentHost: {
jobId: job.id,
taskId: job.taskId,
channelKind: job.channelKind,
},
agent: {
...prepared.request.agent,
id: job.agentId,
Expand Down
Loading
Loading