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