refactor(subagent): centralize finalize status values
This commit is contained in:
31
index.ts
31
index.ts
@@ -37,8 +37,13 @@ interface SubagentStatus {
|
||||
recentToolCalls: SubagentToolActivity[];
|
||||
}
|
||||
|
||||
enum FinalizeStatus {
|
||||
SUCCESS = "SUCCESS",
|
||||
ERROR = "ERROR",
|
||||
}
|
||||
|
||||
interface SubagentFinalizePayload {
|
||||
status: "SUCCESS" | "ERROR";
|
||||
status: FinalizeStatus;
|
||||
result?: string;
|
||||
error?: string;
|
||||
}
|
||||
@@ -85,7 +90,7 @@ function getSubagentMarkdownTheme(theme: Theme): MarkdownTheme {
|
||||
|
||||
// Format Tool Content - Some clients hide structured details from the model.
|
||||
function formatSubagentContent(
|
||||
status: "SUCCESS" | "ERROR",
|
||||
status: FinalizeStatus,
|
||||
sessionId: string,
|
||||
result?: string,
|
||||
error?: string,
|
||||
@@ -178,7 +183,7 @@ function buildSubagentPrompt(agent: PromptConfig): string {
|
||||
"You are running as a subagent.",
|
||||
`When the task is complete, call ${FINALIZE_TOOL_NAME} as your final action.`,
|
||||
"Do not provide the final answer as normal assistant text.",
|
||||
`${FINALIZE_TOOL_NAME} requires status SUCCESS with result, or status ERROR with error and optional result.`,
|
||||
`${FINALIZE_TOOL_NAME} requires status ${FinalizeStatus.SUCCESS} with result, or status ${FinalizeStatus.ERROR} with error and optional result.`,
|
||||
].join("\n");
|
||||
|
||||
return [agent.systemPrompt, finalizePrompt].filter(Boolean).join("\n\n");
|
||||
@@ -208,16 +213,16 @@ function validateFinalizePayload(
|
||||
): SubagentFinalizePayload | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const payload = value as Record<string, unknown>;
|
||||
if (payload.status === "SUCCESS") {
|
||||
if (payload.status === FinalizeStatus.SUCCESS) {
|
||||
return typeof payload.result === "string" && payload.result.trim()
|
||||
? { status: "SUCCESS", result: payload.result }
|
||||
? { status: FinalizeStatus.SUCCESS, result: payload.result }
|
||||
: null;
|
||||
}
|
||||
if (payload.status === "ERROR") {
|
||||
if (payload.status === FinalizeStatus.ERROR) {
|
||||
const result =
|
||||
typeof payload.result === "string" ? payload.result : undefined;
|
||||
return typeof payload.error === "string" && payload.error.trim()
|
||||
? { status: "ERROR", error: payload.error, result }
|
||||
? { status: FinalizeStatus.ERROR, error: payload.error, result }
|
||||
: null;
|
||||
}
|
||||
return null;
|
||||
@@ -592,7 +597,7 @@ export default function (pi: ExtensionAPI) {
|
||||
promptSnippet:
|
||||
"Call subagent_finalize as your final action when subagent work is complete.",
|
||||
parameters: Type.Object({
|
||||
status: Type.Union([Type.Literal("SUCCESS"), Type.Literal("ERROR")]),
|
||||
status: Type.Enum(FinalizeStatus),
|
||||
result: Type.Optional(Type.String()),
|
||||
error: Type.Optional(Type.String()),
|
||||
}),
|
||||
@@ -709,7 +714,7 @@ export default function (pi: ExtensionAPI) {
|
||||
{
|
||||
type: "text",
|
||||
text: formatSubagentContent(
|
||||
"ERROR",
|
||||
FinalizeStatus.ERROR,
|
||||
sessionId,
|
||||
undefined,
|
||||
"Subagent did not run.",
|
||||
@@ -731,7 +736,7 @@ export default function (pi: ExtensionAPI) {
|
||||
{
|
||||
type: "text",
|
||||
text: formatSubagentContent(
|
||||
"ERROR",
|
||||
FinalizeStatus.ERROR,
|
||||
sessionId,
|
||||
undefined,
|
||||
fallback,
|
||||
@@ -743,13 +748,13 @@ export default function (pi: ExtensionAPI) {
|
||||
};
|
||||
}
|
||||
|
||||
if (result.finalized.status === "ERROR") {
|
||||
if (result.finalized.status === FinalizeStatus.ERROR) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: formatSubagentContent(
|
||||
"ERROR",
|
||||
FinalizeStatus.ERROR,
|
||||
sessionId,
|
||||
result.finalized.result,
|
||||
result.finalized.error ?? "Subagent failed.",
|
||||
@@ -766,7 +771,7 @@ export default function (pi: ExtensionAPI) {
|
||||
{
|
||||
type: "text",
|
||||
text: formatSubagentContent(
|
||||
"SUCCESS",
|
||||
FinalizeStatus.SUCCESS,
|
||||
sessionId,
|
||||
result.finalized.result,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user