mirror of
https://github.com/different-ai/openwork.git
synced 2026-11-27 06:40:50 +08:00
test: support turn-scoped model fixtures for design revisions
This commit is contained in:
4 files changed
+52
-8
No files matched your search
@@ -30,6 +30,8 @@ export interface MockAgentToolStep {
|
||||
}
|
||||
|
||||
export interface MockAgentWorkload {
|
||||
/** Chat Completions: match the latest user message and count only its tool rounds. */
|
||||
latestUserTurn?: boolean;
|
||||
promptMarker: string;
|
||||
finalReply: string;
|
||||
/** Stream the final reply as consecutive content deltas of this many characters instead of one. */
|
||||
|
||||
@@ -93,3 +93,39 @@ test("scripts and records deterministic OpenAI-compatible agent tool rounds", as
|
||||
assert.deepEqual(requests.map((request) => request.completedTools), [0, 1, 2]);
|
||||
assert.deepEqual(requests.map((request) => request.matchedMarkers), [[marker], [marker], [marker]]);
|
||||
});
|
||||
|
||||
test("turn-scoped workloads isolate revisions from earlier markers and tool rounds", async () => {
|
||||
await using mock = await startMockMcp({
|
||||
port: await allocateFreePort(),
|
||||
agentWorkloads: ["first sketch", "revise sketch"].map((promptMarker) => ({
|
||||
latestUserTurn: true,
|
||||
promptMarker,
|
||||
finalReply: `${promptMarker} complete`,
|
||||
steps: [{ tool: "write", arguments: { content: promptMarker } }],
|
||||
})),
|
||||
});
|
||||
const history = [
|
||||
{ role: "user", content: "first sketch" },
|
||||
{ role: "tool", tool_call_id: "old", content: "old result" },
|
||||
{ role: "assistant", content: "first sketch complete" },
|
||||
{ role: "user", content: "revise sketch" },
|
||||
];
|
||||
for (const completed of [false, true]) {
|
||||
const response = await fetch(`${mock.url}/v1/chat/completions`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
...completionBody("unused", 0),
|
||||
messages: [...history, ...(completed ? [{ role: "tool", tool_call_id: "new", content: "new result" }] : [])],
|
||||
}),
|
||||
});
|
||||
assert.equal(response.status, 200);
|
||||
const text = await response.text();
|
||||
assert.match(text, completed ? /revise sketch complete/ : /"name":"write"/);
|
||||
assert.doesNotMatch(text, /first sketch complete/);
|
||||
}
|
||||
const requests = await mock.agentRequests({ promptMarker: "revise sketch" });
|
||||
assert.deepEqual(requests.map((request) => request.kind), ["tool", "final"]);
|
||||
assert.deepEqual(requests.map((request) => request.completedTools), [0, 1]);
|
||||
assert.deepEqual(requests.map((request) => request.matchedMarkers), [["revise sketch"], ["revise sketch"]]);
|
||||
});
|
||||
@@ -1300,8 +1300,8 @@ export async function visualization(seed: Seed) {
|
||||
] }],
|
||||
};
|
||||
const mock = seed.mock({ agentWorkloads: [
|
||||
{ promptMarker: "Sketch a project overview", finalReply: "Your first sketch is ready.", steps: [{ tool: "openwork_visualization", arguments: design }] },
|
||||
{ promptMarker: "Create version 2", finalReply: "Your revised sketch is ready.", steps: [{ tool: "openwork_visualization", arguments: { ...design, revision: 2, description: "A calmer overview" } }] },
|
||||
{ latestUserTurn: true, promptMarker: "Sketch a project overview", finalReply: "Your first sketch is ready.", steps: [{ tool: "openwork_visualization", arguments: design }] },
|
||||
{ latestUserTurn: true, promptMarker: "Create version 2", finalReply: "Your revised sketch is ready.", steps: [{ tool: "openwork_visualization", arguments: { ...design, revision: 2, description: "A calmer overview" } }] },
|
||||
] });
|
||||
const den = await seed.den({ mocks: { agent: mock } });
|
||||
const app = await seed.desktop({ name: "visualization", model: `${providerId}/${modelId}` });
|
||||
|
||||
@@ -187,6 +187,9 @@ function validateAgentWorkloads(value) {
|
||||
if (finalReplyChunkSize !== null && (!Number.isInteger(finalReplyChunkSize) || finalReplyChunkSize < 1)) {
|
||||
throw new Error(`agent workload ${promptMarker} finalReplyChunkSize must be a positive integer`);
|
||||
}
|
||||
if (workload.latestUserTurn !== undefined && typeof workload.latestUserTurn !== "boolean") {
|
||||
throw new Error(`agent workload ${promptMarker} latestUserTurn must be a boolean`);
|
||||
}
|
||||
const steps = workload.steps.map((step) => {
|
||||
if (!step || typeof step !== "object" || typeof step.tool !== "string" || !step.tool.trim()) {
|
||||
throw new Error(`agent workload ${promptMarker} has an invalid tool step`);
|
||||
@@ -199,7 +202,7 @@ function validateAgentWorkloads(value) {
|
||||
}
|
||||
return { tool: step.tool.trim(), arguments: structuredClone(step.arguments), argumentsFrom: step.argumentsFrom };
|
||||
});
|
||||
return { promptMarker, finalReply, finalReplyChunkSize, steps };
|
||||
return { promptMarker, finalReply, finalReplyChunkSize, steps, latestUserTurn: workload.latestUserTurn === true };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -321,10 +324,14 @@ async function handleAgentCompletion(req, res, entry) {
|
||||
const model = typeof body.model === "string" ? body.model : "mock-agent-workload-model";
|
||||
const messages = Array.isArray(body.messages) ? body.messages : [];
|
||||
const conversationText = messages.map(agentContentText).join("\n");
|
||||
const matchedMarkers = agentWorkloads
|
||||
.filter((workload) => conversationText.includes(workload.promptMarker))
|
||||
.map((workload) => workload.promptMarker);
|
||||
const completedTools = messages.filter((message) => message && typeof message === "object" && message.role === "tool").length;
|
||||
const latestUserIndex = messages.findLastIndex((message) => message?.role === "user");
|
||||
const latestUserText = latestUserIndex < 0 ? "" : agentContentText(messages[latestUserIndex]);
|
||||
const matched = agentWorkloads.filter((workload) =>
|
||||
(workload.latestUserTurn ? latestUserText : conversationText).includes(workload.promptMarker));
|
||||
const matchedMarkers = matched.map((workload) => workload.promptMarker);
|
||||
const workload = matched[0];
|
||||
const scopedMessages = workload?.latestUserTurn ? messages.slice(latestUserIndex + 1) : messages;
|
||||
const completedTools = scopedMessages.filter((message) => message && typeof message === "object" && message.role === "tool").length;
|
||||
const baseRequest = { model, matchedMarkers, completedTools };
|
||||
|
||||
if (!Array.isArray(body.tools) || body.tools.length === 0) {
|
||||
@@ -341,7 +348,6 @@ async function handleAgentCompletion(req, res, entry) {
|
||||
json(res, 400, { error: { message: `expected one workload marker, found ${matchedMarkers.length}` } });
|
||||
return;
|
||||
}
|
||||
const workload = agentWorkloads.find((candidate) => candidate.promptMarker === matchedMarkers[0]);
|
||||
if (!workload) throw new Error("matched agent workload disappeared");
|
||||
if (completedTools >= workload.steps.length) {
|
||||
entry.agentCompletion = { ...baseRequest, kind: "final", promptMarker: workload.promptMarker, toolName: null, arguments: {} };
|
||||
|
||||
Reference in new issue
Block a user