enhanced the AgentService.process method

- emit specialized error from failed context builds
- emit specialized error from failed workflow loops
This commit is contained in:
Rob Colbert 2026-05-05 13:19:54 -04:00
parent 09ebacc711
commit b94fe24287

View File

@ -4,7 +4,11 @@
import { Types } from "@gadget/api";
import { Socket } from "socket.io-client";
import { IAiChatOptions, type IContextChatMessage } from "@gadget/ai";
import {
IAiChatOptions,
IAiStreamChunk,
type IContextChatMessage,
} from "@gadget/ai";
import {
IChatSession,
IChatTurn,
@ -32,6 +36,11 @@ export interface IAgentWorkOrder {
context: IChatTurn[];
}
interface IAgentWorkflow {
chatOptions: IAiChatOptions;
context: IContextChatMessage[];
}
type DroneSocket = Socket<ServerToClientEvents, ClientToServerEvents>;
class AgentService extends GadgetService {
@ -55,18 +64,38 @@ class AgentService extends GadgetService {
socket: DroneSocket,
): Promise<void> {
const { turn } = workOrder;
const task: IAgentWorkflow = {
chatOptions: {},
context: [],
};
async function aiCallTool(name: string, args: string) {
return "[all tool calls are stubbed out]";
}
const context = this.buildSessionContext(workOrder);
const chatOptions: IAiChatOptions = {
systemPrompt: turn.prompts.system,
context,
userPrompt: turn.prompts.user,
const onStreamChunk = async (chunk: IAiStreamChunk): Promise<void> => {
this.log.debug("stream chunk received", { chunk });
};
try {
task.context = this.buildSessionContext(workOrder);
task.chatOptions = {
systemPrompt: turn.prompts.system,
context: task.context,
userPrompt: turn.prompts.user,
};
} catch (cause) {
socket.emit(
"workOrderComplete",
turn._id,
false,
`failed to build session context: ${(cause as Error).message}`,
);
const error = new Error("failed to build session context", { cause });
throw error;
}
try {
let keepProcessing = true;
do {
const response = await AiService.chat(
@ -80,7 +109,8 @@ class AgentService extends GadgetService {
topK: 40,
},
},
chatOptions,
task.chatOptions,
onStreamChunk,
);
// Emit thinking content if present
@ -99,7 +129,7 @@ class AgentService extends GadgetService {
toolCall.function.name,
toolCall.function.arguments,
);
context.push({
task.context.push({
createdAt: new Date(),
role: "tool",
callId: toolCall.callId,
@ -116,6 +146,18 @@ class AgentService extends GadgetService {
);
}
} while (keepProcessing);
} catch (cause) {
socket.emit(
"workOrderComplete",
turn._id,
false,
`failed to process agentic workflow loop: ${(cause as Error).message}`,
);
const error = new Error("failed to process agentic workflow loop", {
cause,
});
throw error;
}
// Emit work order complete
socket.emit("workOrderComplete", turn._id, true);