feat(agent): add controlled task execution
This commit is contained in:
@@ -3,6 +3,7 @@ import type { Event as OpencodeEvent, Part } from "@opencode-ai/sdk/v2";
|
||||
import { writeLlmRequestAuditLog } from "../audit/llmRequestAudit.js";
|
||||
import { type SupportedModel } from "../chat/models.js";
|
||||
import { config } from "../config.js";
|
||||
import { executionPlanSchema } from "./executionPlan.js";
|
||||
import { logger } from "../logger.js";
|
||||
import {
|
||||
type PermissionReply,
|
||||
@@ -115,6 +116,15 @@ const toRuntimeModel = (model?: SupportedModel) => {
|
||||
};
|
||||
};
|
||||
|
||||
const parseToolOutput = (output: unknown): unknown => {
|
||||
if (typeof output !== "string") return output;
|
||||
try {
|
||||
return JSON.parse(output);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const emitFallbackMessage = async (
|
||||
runtime: OpencodeRuntimeAdapter,
|
||||
sessionId: string,
|
||||
@@ -715,6 +725,17 @@ export const streamPromptResponse = async ({
|
||||
});
|
||||
}
|
||||
if (part.type === "tool") {
|
||||
if (part.tool === "publish_execution_plan" && part.state.status === "completed" && !emittedToolParts.has(part.id)) {
|
||||
const plan = executionPlanSchema.safeParse(part.state.input);
|
||||
const output = parseToolOutput(part.state.output);
|
||||
const serverPublished = output && typeof output === "object" &&
|
||||
typeof (output as Record<string, unknown>).plan_revision === "number";
|
||||
if (plan.success) write("execution_plan", {
|
||||
...plan.data,
|
||||
session_id: clientSessionId,
|
||||
...(serverPublished ? { server_published: true } : {}),
|
||||
});
|
||||
}
|
||||
if (!firstToolEventLogged) {
|
||||
firstToolEventLogged = true;
|
||||
logDevelopmentDebug("first tool event received", {
|
||||
@@ -792,7 +813,7 @@ export const streamPromptResponse = async ({
|
||||
});
|
||||
if (
|
||||
!emittedToolParts.has(part.id) &&
|
||||
(hasToolParams(toolParams) || isToolFinalState)
|
||||
(part.tool === "publish_execution_plan" ? isToolFinalState : (hasToolParams(toolParams) || isToolFinalState))
|
||||
) {
|
||||
emittedToolParts.add(part.id);
|
||||
toolCallCount += 1;
|
||||
|
||||
Reference in New Issue
Block a user