enhanced the AgentService.process method
- emit specialized error from failed context builds - emit specialized error from failed workflow loops
This commit is contained in:
parent
09ebacc711
commit
b94fe24287
@ -4,7 +4,11 @@
|
|||||||
|
|
||||||
import { Types } from "@gadget/api";
|
import { Types } from "@gadget/api";
|
||||||
import { Socket } from "socket.io-client";
|
import { Socket } from "socket.io-client";
|
||||||
import { IAiChatOptions, type IContextChatMessage } from "@gadget/ai";
|
import {
|
||||||
|
IAiChatOptions,
|
||||||
|
IAiStreamChunk,
|
||||||
|
type IContextChatMessage,
|
||||||
|
} from "@gadget/ai";
|
||||||
import {
|
import {
|
||||||
IChatSession,
|
IChatSession,
|
||||||
IChatTurn,
|
IChatTurn,
|
||||||
@ -32,6 +36,11 @@ export interface IAgentWorkOrder {
|
|||||||
context: IChatTurn[];
|
context: IChatTurn[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IAgentWorkflow {
|
||||||
|
chatOptions: IAiChatOptions;
|
||||||
|
context: IContextChatMessage[];
|
||||||
|
}
|
||||||
|
|
||||||
type DroneSocket = Socket<ServerToClientEvents, ClientToServerEvents>;
|
type DroneSocket = Socket<ServerToClientEvents, ClientToServerEvents>;
|
||||||
|
|
||||||
class AgentService extends GadgetService {
|
class AgentService extends GadgetService {
|
||||||
@ -55,18 +64,38 @@ class AgentService extends GadgetService {
|
|||||||
socket: DroneSocket,
|
socket: DroneSocket,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { turn } = workOrder;
|
const { turn } = workOrder;
|
||||||
|
const task: IAgentWorkflow = {
|
||||||
|
chatOptions: {},
|
||||||
|
context: [],
|
||||||
|
};
|
||||||
|
|
||||||
async function aiCallTool(name: string, args: string) {
|
async function aiCallTool(name: string, args: string) {
|
||||||
return "[all tool calls are stubbed out]";
|
return "[all tool calls are stubbed out]";
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = this.buildSessionContext(workOrder);
|
const onStreamChunk = async (chunk: IAiStreamChunk): Promise<void> => {
|
||||||
const chatOptions: IAiChatOptions = {
|
this.log.debug("stream chunk received", { chunk });
|
||||||
systemPrompt: turn.prompts.system,
|
|
||||||
context,
|
|
||||||
userPrompt: turn.prompts.user,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
let keepProcessing = true;
|
||||||
do {
|
do {
|
||||||
const response = await AiService.chat(
|
const response = await AiService.chat(
|
||||||
@ -80,7 +109,8 @@ class AgentService extends GadgetService {
|
|||||||
topK: 40,
|
topK: 40,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
chatOptions,
|
task.chatOptions,
|
||||||
|
onStreamChunk,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Emit thinking content if present
|
// Emit thinking content if present
|
||||||
@ -99,7 +129,7 @@ class AgentService extends GadgetService {
|
|||||||
toolCall.function.name,
|
toolCall.function.name,
|
||||||
toolCall.function.arguments,
|
toolCall.function.arguments,
|
||||||
);
|
);
|
||||||
context.push({
|
task.context.push({
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
role: "tool",
|
role: "tool",
|
||||||
callId: toolCall.callId,
|
callId: toolCall.callId,
|
||||||
@ -116,6 +146,18 @@ class AgentService extends GadgetService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} while (keepProcessing);
|
} 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
|
// Emit work order complete
|
||||||
socket.emit("workOrderComplete", turn._id, true);
|
socket.emit("workOrderComplete", turn._id, true);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user