mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 13:05:01 +00:00
refactor(agents): unify subagent announce delivery pipeline
Co-authored-by: Smith Labs <SmithLabsLLC@users.noreply.github.com> Co-authored-by: Do Cao Hieu <docaohieu2808@users.noreply.github.com>
This commit is contained in:
104
src/agents/subagent-announce-dispatch.ts
Normal file
104
src/agents/subagent-announce-dispatch.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
export type SubagentDeliveryPath = "queued" | "steered" | "direct" | "none";
|
||||
|
||||
export type SubagentAnnounceQueueOutcome = "steered" | "queued" | "none";
|
||||
|
||||
export type SubagentAnnounceDeliveryResult = {
|
||||
delivered: boolean;
|
||||
path: SubagentDeliveryPath;
|
||||
error?: string;
|
||||
phases?: SubagentAnnounceDispatchPhaseResult[];
|
||||
};
|
||||
|
||||
export type SubagentAnnounceDispatchPhase = "queue-primary" | "direct-primary" | "queue-fallback";
|
||||
|
||||
export type SubagentAnnounceDispatchPhaseResult = {
|
||||
phase: SubagentAnnounceDispatchPhase;
|
||||
delivered: boolean;
|
||||
path: SubagentDeliveryPath;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export function mapQueueOutcomeToDeliveryResult(
|
||||
outcome: SubagentAnnounceQueueOutcome,
|
||||
): SubagentAnnounceDeliveryResult {
|
||||
if (outcome === "steered") {
|
||||
return {
|
||||
delivered: true,
|
||||
path: "steered",
|
||||
};
|
||||
}
|
||||
if (outcome === "queued") {
|
||||
return {
|
||||
delivered: true,
|
||||
path: "queued",
|
||||
};
|
||||
}
|
||||
return {
|
||||
delivered: false,
|
||||
path: "none",
|
||||
};
|
||||
}
|
||||
|
||||
export async function runSubagentAnnounceDispatch(params: {
|
||||
expectsCompletionMessage: boolean;
|
||||
signal?: AbortSignal;
|
||||
queue: () => Promise<SubagentAnnounceQueueOutcome>;
|
||||
direct: () => Promise<SubagentAnnounceDeliveryResult>;
|
||||
}): Promise<SubagentAnnounceDeliveryResult> {
|
||||
const phases: SubagentAnnounceDispatchPhaseResult[] = [];
|
||||
const appendPhase = (
|
||||
phase: SubagentAnnounceDispatchPhase,
|
||||
result: SubagentAnnounceDeliveryResult,
|
||||
) => {
|
||||
phases.push({
|
||||
phase,
|
||||
delivered: result.delivered,
|
||||
path: result.path,
|
||||
error: result.error,
|
||||
});
|
||||
};
|
||||
const withPhases = (result: SubagentAnnounceDeliveryResult): SubagentAnnounceDeliveryResult => ({
|
||||
...result,
|
||||
phases,
|
||||
});
|
||||
|
||||
if (params.signal?.aborted) {
|
||||
return withPhases({
|
||||
delivered: false,
|
||||
path: "none",
|
||||
});
|
||||
}
|
||||
|
||||
if (!params.expectsCompletionMessage) {
|
||||
const primaryQueue = mapQueueOutcomeToDeliveryResult(await params.queue());
|
||||
appendPhase("queue-primary", primaryQueue);
|
||||
if (primaryQueue.delivered) {
|
||||
return withPhases(primaryQueue);
|
||||
}
|
||||
|
||||
const primaryDirect = await params.direct();
|
||||
appendPhase("direct-primary", primaryDirect);
|
||||
return withPhases(primaryDirect);
|
||||
}
|
||||
|
||||
const primaryDirect = await params.direct();
|
||||
appendPhase("direct-primary", primaryDirect);
|
||||
if (primaryDirect.delivered) {
|
||||
return withPhases(primaryDirect);
|
||||
}
|
||||
|
||||
if (params.signal?.aborted) {
|
||||
return withPhases({
|
||||
delivered: false,
|
||||
path: "none",
|
||||
});
|
||||
}
|
||||
|
||||
const fallbackQueue = mapQueueOutcomeToDeliveryResult(await params.queue());
|
||||
appendPhase("queue-fallback", fallbackQueue);
|
||||
if (fallbackQueue.delivered) {
|
||||
return withPhases(fallbackQueue);
|
||||
}
|
||||
|
||||
return withPhases(primaryDirect);
|
||||
}
|
||||
Reference in New Issue
Block a user