67e1aa40bf
- Added `resolve` tool to apply or discard pending AST edit previews with required reasoning. - Changed `ast_edit` tool to always return previews by default; removed `preview` parameter. - Added `getToolChoice` callback option to dynamically override tool choice per LLM call. - Implemented PendingActionStore for managing deferred tool action state across agent sessions. - Updated agent loop to support dynamic tool choice resolution via optional callback.
119 lines
4.5 KiB
TypeScript
119 lines
4.5 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import * as fs from "node:fs/promises";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { adaptSchemaForStrict } from "@oh-my-pi/pi-ai/utils/schema";
|
|
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
|
import { createTools, type ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
|
|
import { PendingActionStore } from "@oh-my-pi/pi-coding-agent/tools/pending-action";
|
|
|
|
function createTestSession(cwd = "/tmp/test", overrides: Partial<ToolSession> = {}): ToolSession {
|
|
return {
|
|
cwd,
|
|
hasUI: true,
|
|
getSessionFile: () => null,
|
|
getSessionSpawns: () => "*",
|
|
settings: Settings.isolated(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function asSchemaObject(value: unknown): Record<string, unknown> {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
throw new Error("Expected object schema");
|
|
}
|
|
return value as Record<string, unknown>;
|
|
}
|
|
|
|
describe("ast_edit tool schema", () => {
|
|
it("uses op entries as [{ pat, out }]", async () => {
|
|
const tools = await createTools(createTestSession());
|
|
const tool = tools.find(entry => entry.name === "ast_edit");
|
|
expect(tool).toBeDefined();
|
|
const schema = asSchemaObject(tool?.parameters);
|
|
const properties = asSchemaObject(schema.properties);
|
|
const ops = asSchemaObject(properties.ops);
|
|
|
|
expect(ops.type).toBe("array");
|
|
const items = asSchemaObject(ops.items);
|
|
expect(items.type).toBe("object");
|
|
expect(items.required).toEqual(["pat", "out"]);
|
|
const itemProperties = asSchemaObject(items.properties);
|
|
expect(asSchemaObject(itemProperties.pat).type).toBe("string");
|
|
expect(asSchemaObject(itemProperties.out).type).toBe("string");
|
|
expect(properties.preview).toBeUndefined();
|
|
});
|
|
|
|
it("remains strict-representable after strict adaptation", async () => {
|
|
const tools = await createTools(createTestSession());
|
|
const tool = tools.find(entry => entry.name === "ast_edit");
|
|
expect(tool).toBeDefined();
|
|
const schema = asSchemaObject(tool?.parameters);
|
|
|
|
const strict = adaptSchemaForStrict(schema, true);
|
|
expect(strict.strict).toBe(true);
|
|
});
|
|
|
|
it("renders +/- lines with aligned hashline prefixes", async () => {
|
|
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ast-edit-render-"));
|
|
try {
|
|
const filePath = path.join(tempDir, "legacy.ts");
|
|
await Bun.write(filePath, "legacyWrap(x, value)\n");
|
|
|
|
const tools = await createTools(createTestSession(tempDir));
|
|
const tool = tools.find(entry => entry.name === "ast_edit");
|
|
expect(tool).toBeDefined();
|
|
|
|
const result = await tool!.execute("ast-edit-test", {
|
|
ops: [{ pat: "legacyWrap($A, $B)", out: "modernWrap($A, $B)" }],
|
|
lang: "typescript",
|
|
path: filePath,
|
|
});
|
|
const text = result.content.find(content => content.type === "text")?.text ?? "";
|
|
const lines = text.split("\n");
|
|
const removedLine = lines.find(line => line.startsWith("-"));
|
|
const addedLine = lines.find(line => line.startsWith("+"));
|
|
|
|
expect(removedLine).toBeDefined();
|
|
expect(addedLine).toBeDefined();
|
|
expect(removedLine).toMatch(/^-\d+#\w+:/);
|
|
expect(addedLine).toMatch(/^\+\d+#\w+:/);
|
|
expect(removedLine?.split(":", 1)[0].length).toBe(addedLine?.split(":", 1)[0].length);
|
|
} finally {
|
|
await fs.rm(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("registers a pending action that apply writes changes", async () => {
|
|
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ast-edit-pending-"));
|
|
try {
|
|
const filePath = path.join(tempDir, "legacy.ts");
|
|
await Bun.write(filePath, "legacyWrap(x, value)\n");
|
|
const pendingActionStore = new PendingActionStore();
|
|
|
|
const tools = await createTools(createTestSession(tempDir, { pendingActionStore }));
|
|
const tool = tools.find(entry => entry.name === "ast_edit");
|
|
expect(tool).toBeDefined();
|
|
|
|
const previewResult = await tool!.execute("ast-edit-preview", {
|
|
ops: [{ pat: "legacyWrap($A, $B)", out: "modernWrap($A, $B)" }],
|
|
lang: "typescript",
|
|
path: filePath,
|
|
});
|
|
expect(previewResult.details).toBeDefined();
|
|
expect((previewResult.details as { applied?: boolean }).applied).toBe(false);
|
|
|
|
const pending = pendingActionStore.get();
|
|
expect(pending).not.toBeNull();
|
|
if (!pending) throw new Error("Expected pending action to be registered");
|
|
expect(pending.sourceToolName).toBe("ast_edit");
|
|
|
|
await pending.apply();
|
|
const updated = await Bun.file(filePath).text();
|
|
expect(updated).toContain("modernWrap(x, value)");
|
|
} finally {
|
|
await fs.rm(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|