mirror of
https://github.com/CopilotKit/CopilotKit.git
synced 2026-09-14 16:26:20 +08:00
fix(showcase): validate persisted setup IDs
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import type { AgentCreateParams } from "@anthropic-ai/sdk/resources/beta/agents/agents";
|
||||
|
||||
import { parseAgentIds, provisionAgentResources } from "./setup.ts";
|
||||
import {
|
||||
parseAgentIds,
|
||||
provisionAgentResources,
|
||||
readAgentIdsFile,
|
||||
} from "./setup.ts";
|
||||
|
||||
function createCapturingClient() {
|
||||
let agentParams: AgentCreateParams | undefined;
|
||||
@@ -84,6 +91,18 @@ test("accepts valid persisted agent IDs", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("rejects malformed persisted agent-ID files", (t) => {
|
||||
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "agent-ids-test-"));
|
||||
t.after(() => fs.rmSync(directory, { recursive: true }));
|
||||
const file = path.join(directory, "agent-ids.json");
|
||||
fs.writeFileSync(file, JSON.stringify({ environmentId: "env_test" }));
|
||||
|
||||
assert.throws(
|
||||
() => readAgentIdsFile(file),
|
||||
/expected non-empty environmentId and agentId strings/,
|
||||
);
|
||||
});
|
||||
|
||||
test("deletes the environment when agent creation fails", async () => {
|
||||
const failure = new Error("agent creation failed");
|
||||
const deletedEnvironmentIds: string[] = [];
|
||||
|
||||
@@ -47,6 +47,10 @@ export function parseAgentIds(value: unknown): AgentIds {
|
||||
return { environmentId, agentId };
|
||||
}
|
||||
|
||||
export function readAgentIdsFile(filePath = AGENT_IDS_PATH): AgentIds {
|
||||
return parseAgentIds(JSON.parse(fs.readFileSync(filePath, "utf8")));
|
||||
}
|
||||
|
||||
const DEFAULT_MODEL = "claude-fable-5";
|
||||
|
||||
const ASSISTANT_SYSTEM = `You are a careful, plain-spoken personal finance assistant. Your job is
|
||||
@@ -166,9 +170,7 @@ async function main() {
|
||||
`agent-ids.json already exists — agents are reusable, not per-run.`,
|
||||
);
|
||||
console.log(`Re-run with --force to re-provision.\n`);
|
||||
printEnvExports(
|
||||
JSON.parse(fs.readFileSync(AGENT_IDS_PATH, "utf8")) as AgentIds,
|
||||
);
|
||||
printEnvExports(readAgentIdsFile());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -221,5 +223,5 @@ export function loadAgentIds(): AgentIds {
|
||||
"ANTHROPIC_ENVIRONMENT_ID and ANTHROPIC_AGENT_ID.",
|
||||
);
|
||||
}
|
||||
return parseAgentIds(JSON.parse(fs.readFileSync(AGENT_IDS_PATH, "utf8")));
|
||||
return readAgentIdsFile();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user