feat(harness): manage Pi agent sessions
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import * as harness from "../src/index.js";
|
||||
|
||||
describe("PiEventAdapter", () => {
|
||||
it("maps assistant deltas and completion with one stable message id", async () => {
|
||||
const store = new harness.InMemoryEventStore();
|
||||
const Adapter = Reflect.get(harness, "PiEventAdapter") as unknown as new (options: Record<string, unknown>) => {
|
||||
observe(event: unknown): Promise<void>;
|
||||
};
|
||||
expect(Adapter, "PiEventAdapter must be exported").toBeDefined();
|
||||
const adapter = new Adapter({ sessionId: "ses_1", runId: "run_1", eventStore: store });
|
||||
|
||||
await adapter.observe({ type: "message_update", message: { role: "assistant" }, assistantMessageEvent: { type: "text_delta", delta: "你好" } });
|
||||
await adapter.observe({ type: "message_end", message: { role: "assistant" } });
|
||||
|
||||
expect(store.listAfter("ses_1").events.map(({ type, payload }) => ({ type, payload }))).toEqual([
|
||||
{ type: "assistant.delta", payload: { messageId: "msg_run_1", delta: "你好" } },
|
||||
{ type: "assistant.completed", payload: { messageId: "msg_run_1" } },
|
||||
]);
|
||||
});
|
||||
|
||||
it("maps tool lifecycle, redacts payloads, and correlates successful reads to Skill inference", async () => {
|
||||
const store = new harness.InMemoryEventStore();
|
||||
const requested = vi.fn(() => Promise.resolve());
|
||||
const completed = vi.fn();
|
||||
const Adapter = Reflect.get(harness, "PiEventAdapter") as unknown as new (options: Record<string, unknown>) => {
|
||||
observe(event: unknown): Promise<void>;
|
||||
};
|
||||
const adapter = new Adapter({
|
||||
sessionId: "ses_1",
|
||||
runId: "run_1",
|
||||
eventStore: store,
|
||||
skillInference: { onReadRequested: requested, onReadCompleted: completed },
|
||||
});
|
||||
|
||||
await adapter.observe({ type: "tool_execution_start", toolCallId: "call_1", toolName: "read", args: { path: "/skill/SKILL.md", token: "hidden" } });
|
||||
await adapter.observe({ type: "tool_execution_end", toolCallId: "call_1", toolName: "read", result: { content: "ok", apiKey: "hidden" }, isError: false });
|
||||
await adapter.observe({ type: "tool_execution_end", toolCallId: "call_2", toolName: "grep", result: "failed", isError: true });
|
||||
await expect(adapter.observe({ type: "future_event", anything: true })).resolves.toBeUndefined();
|
||||
|
||||
expect(requested).toHaveBeenCalledWith({ toolCallId: "call_1", path: "/skill/SKILL.md" });
|
||||
expect(completed).toHaveBeenCalledWith({ toolCallId: "call_1", success: true });
|
||||
expect(store.listAfter("ses_1").events.map((event) => event.type)).toEqual([
|
||||
"tool.requested",
|
||||
"tool.completed",
|
||||
"tool.failed",
|
||||
]);
|
||||
expect(JSON.stringify(store.listAfter("ses_1"))).not.toContain("hidden");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user