mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 12:27:40 +00:00
refactor(agent): dedupe harness and command workflows
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
type SubscribeEmbeddedPiSession =
|
||||
typeof import("./pi-embedded-subscribe.js").subscribeEmbeddedPiSession;
|
||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||
import { expect } from "vitest";
|
||||
import { subscribeEmbeddedPiSession } from "./pi-embedded-subscribe.js";
|
||||
|
||||
type SubscribeEmbeddedPiSession = typeof subscribeEmbeddedPiSession;
|
||||
type SubscribeEmbeddedPiSessionParams = Parameters<SubscribeEmbeddedPiSession>[0];
|
||||
type PiSession = Parameters<SubscribeEmbeddedPiSession>[0]["session"];
|
||||
type OnBlockReply = NonNullable<SubscribeEmbeddedPiSessionParams["onBlockReply"]>;
|
||||
|
||||
export function createStubSessionHarness(): {
|
||||
session: PiSession;
|
||||
@@ -17,6 +22,47 @@ export function createStubSessionHarness(): {
|
||||
return { session, emit: (evt: unknown) => handler?.(evt) };
|
||||
}
|
||||
|
||||
export function createSubscribedSessionHarness(
|
||||
params: Omit<Parameters<SubscribeEmbeddedPiSession>[0], "session"> & {
|
||||
sessionExtras?: Partial<PiSession>;
|
||||
},
|
||||
): {
|
||||
emit: (evt: unknown) => void;
|
||||
session: PiSession;
|
||||
subscription: ReturnType<SubscribeEmbeddedPiSession>;
|
||||
} {
|
||||
const { sessionExtras, ...subscribeParams } = params;
|
||||
const { session, emit } = createStubSessionHarness();
|
||||
const mergedSession = Object.assign(session, sessionExtras ?? {});
|
||||
const subscription = subscribeEmbeddedPiSession({
|
||||
...subscribeParams,
|
||||
session: mergedSession,
|
||||
});
|
||||
return { emit, session: mergedSession, subscription };
|
||||
}
|
||||
|
||||
export function createParagraphChunkedBlockReplyHarness(params: {
|
||||
chunking: { minChars: number; maxChars: number };
|
||||
onBlockReply?: OnBlockReply;
|
||||
runId?: string;
|
||||
}): {
|
||||
emit: (evt: unknown) => void;
|
||||
onBlockReply: OnBlockReply;
|
||||
subscription: ReturnType<SubscribeEmbeddedPiSession>;
|
||||
} {
|
||||
const onBlockReply: OnBlockReply = params.onBlockReply ?? (() => {});
|
||||
const { emit, subscription } = createSubscribedSessionHarness({
|
||||
runId: params.runId ?? "run",
|
||||
onBlockReply,
|
||||
blockReplyBreak: "message_end",
|
||||
blockReplyChunking: {
|
||||
...params.chunking,
|
||||
breakPreference: "paragraph",
|
||||
},
|
||||
});
|
||||
return { emit, onBlockReply, subscription };
|
||||
}
|
||||
|
||||
export function extractAgentEventPayloads(calls: Array<unknown[]>): Array<Record<string, unknown>> {
|
||||
return calls
|
||||
.map((call) => {
|
||||
@@ -26,3 +72,60 @@ export function extractAgentEventPayloads(calls: Array<unknown[]>): Array<Record
|
||||
})
|
||||
.filter((value): value is Record<string, unknown> => Boolean(value));
|
||||
}
|
||||
|
||||
export function extractTextPayloads(calls: Array<unknown[]>): string[] {
|
||||
return calls
|
||||
.map((call) => {
|
||||
const payload = call?.[0] as { text?: unknown } | undefined;
|
||||
return typeof payload?.text === "string" ? payload.text : undefined;
|
||||
})
|
||||
.filter((text): text is string => Boolean(text));
|
||||
}
|
||||
|
||||
export function emitMessageStartAndEndForAssistantText(params: {
|
||||
emit: (evt: unknown) => void;
|
||||
text: string;
|
||||
}): void {
|
||||
const assistantMessage = {
|
||||
role: "assistant",
|
||||
content: [{ type: "text", text: params.text }],
|
||||
} as AssistantMessage;
|
||||
params.emit({ type: "message_start", message: assistantMessage });
|
||||
params.emit({ type: "message_end", message: assistantMessage });
|
||||
}
|
||||
|
||||
export function emitAssistantTextDeltaAndEnd(params: {
|
||||
emit: (evt: unknown) => void;
|
||||
text: string;
|
||||
}): void {
|
||||
params.emit({
|
||||
type: "message_update",
|
||||
message: { role: "assistant" },
|
||||
assistantMessageEvent: {
|
||||
type: "text_delta",
|
||||
delta: params.text,
|
||||
},
|
||||
});
|
||||
const assistantMessage = {
|
||||
role: "assistant",
|
||||
content: [{ type: "text", text: params.text }],
|
||||
} as AssistantMessage;
|
||||
params.emit({ type: "message_end", message: assistantMessage });
|
||||
}
|
||||
|
||||
export function expectFencedChunks(calls: Array<unknown[]>, expectedPrefix: string): void {
|
||||
expect(calls.length).toBeGreaterThan(1);
|
||||
for (const call of calls) {
|
||||
const chunk = (call[0] as { text?: unknown } | undefined)?.text;
|
||||
expect(typeof chunk === "string" && chunk.startsWith(expectedPrefix)).toBe(true);
|
||||
const fenceCount = typeof chunk === "string" ? (chunk.match(/```/g)?.length ?? 0) : 0;
|
||||
expect(fenceCount).toBeGreaterThanOrEqual(2);
|
||||
}
|
||||
}
|
||||
|
||||
export function expectSingleAgentEventText(calls: Array<unknown[]>, text: string): void {
|
||||
const payloads = extractAgentEventPayloads(calls);
|
||||
expect(payloads).toHaveLength(1);
|
||||
expect(payloads[0]?.text).toBe(text);
|
||||
expect(payloads[0]?.delta).toBe(text);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user