The Open Framework for Building Agents
fix(eve): accept existing packages as init targets (#3337)
OwenKephart15c7a2e76a97
diff --git a/.changeset/init-existing-packages.md b/.changeset/init-existing-packages.mdnew file mode 100644index 000000000..beafd88ac--- /dev/null+++ b/.changeset/init-existing-packages.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Allow `eve init` to add an agent to an existing package without requiring a literal `.` target. You can now initialize the current package or pass the path to an existing package directly.diff --git a/docs/reference/cli.md b/docs/reference/cli.mdindex 97974cbff..8f1277b9d 100644--- a/docs/reference/cli.md+++ b/docs/reference/cli.md@@ -47,14 +47,15 @@ eve init [target] [--model <provider/model-id>] [--reasoning <effort>] [--channeCreates a new agent app or adds an agent to an existing app. Always installs dependencies. New directories also initialize Git.-| Target | What happens |-| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |-| `eve init my-agent` | Creates an agent project in `my-agent/` |-| `eve init` or `eve init .` in an empty directory | Creates an agent project in the current directory |-| `eve init` or `eve init .` in a non-empty directory without `package.json` | Asks whether to scaffold in the current directory or a named subdirectory. Using the current directory preserves unrelated files but overwrites files at generated paths |-| `eve init .` in an existing project | Adds `agent/` plus missing `eve`, `ai`, and `zod` dependencies. Requires `package.json` and no existing `agent/` files |--Coding-agent launches and non-interactive terminals cannot answer the location prompt and fail before writing. Pass a new directory name, such as `eve init my-agent`, in those environments.+| Target | What happens |+| ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |+| `eve init my-agent` | Creates an agent project in `my-agent/` |+| `eve init` or `eve init .` in an empty directory | Creates an agent project in the current directory |+| `eve init` or `eve init .` in a directory with files other than environment metadata and no `package.json` | Refuses to overwrite the directory. Pass a new directory name, such as `eve init my-agent` |+| `eve init` or `eve init .` in an existing project | Adds `agent/` plus missing `eve`, `ai`, and `zod` dependencies. Requires `package.json` and no existing `agent/` files |+| `eve init path/to/app` | Adds an agent to the existing package at `path/to/app` |++Existing packages do not need a target-selection prompt: run `eve init` from the project directory or `eve init path/to/app`. New projects in non-interactive environments need a new directory name, such as `eve init my-agent`.After scaffolding, a human terminal usually continues into `eve dev`. Enable the self-modification subagent to edit your agent from the dev session. Otherwise, if a coding-agent REPL is on `PATH`, you can launch it to make changes instead of opening the TUI. Fresh projects use the parent workspace's package manager when there is one; otherwise they use the manager that launched `eve init`.diff --git a/packages/eve/src/cli/commands/agent-prompt/scaffold.md b/packages/eve/src/cli/commands/agent-prompt/scaffold.mdindex fd80700b1..15bff96a2 100644--- a/packages/eve/src/cli/commands/agent-prompt/scaffold.md+++ b/packages/eve/src/cli/commands/agent-prompt/scaffold.md@@ -16,9 +16,10 @@ the interactive terminal UI.If the user wants a reusable extension package instead of an agent, run`npx eve@latest extension init <name>` and follow that command's next steps.-For an existing app, run `npx eve@latest init .` from its directory. This adds the-agent and missing dependencies while leaving the existing Git repository and app-scripts alone. Append `--model provider/model-id` or `--reasoning effort` to-either init command when the user chose those settings. If init cannot be used,+For an existing app, run `npx eve@latest init` from its directory, or pass its+path with `npx eve@latest init path/to/app`. This adds the agent and missing+dependencies while leaving the existing Git repository and app scripts alone.+Append `--model provider/model-id` or `--reasoning effort` to either init command+when the user chose those settings. If init cannot be used,install by hand with`npm install eve@latest ai zod`; manual installation does not add package scripts.diff --git a/packages/eve/src/cli/commands/init-target.integration.test.ts b/packages/eve/src/cli/commands/init-target.integration.test.tsindex 011faa15b..369924e1c 100644--- a/packages/eve/src/cli/commands/init-target.integration.test.ts+++ b/packages/eve/src/cli/commands/init-target.integration.test.ts@@ -58,12 +58,26 @@ describe("resolveInitTarget", () => {});});- it("requires explicit current-directory syntax for an existing package", async () => {- const projectPath = await createScratchDirectory("eve-init-target-package-");+ it.each([undefined, ".", "./", "nested/../."] as const)(+ "adds to an existing package when target is %j",+ async (target) => {+ const projectPath = await createScratchDirectory("eve-init-target-package-");+ await writeFile(join(projectPath, "package.json"), "{}\n");++ await expect(resolveTarget(projectPath, target)).resolves.toEqual({+ kind: "existing",+ projectPath,+ });+ },+ );++ it("adds to an existing package addressed by a relative path", async () => {+ const parentDirectory = await createScratchDirectory("eve-init-target-parent-package-");+ const projectPath = join(parentDirectory, "existing-app");+ await mkdir(projectPath);await writeFile(join(projectPath, "package.json"), "{}\n");- await expect(resolveTarget(projectPath)).rejects.toThrow("explicit `eve init .`");- await expect(resolveTarget(projectPath, ".")).resolves.toEqual({+ await expect(resolveTarget(parentDirectory, "existing-app")).resolves.toEqual({kind: "existing",projectPath,});@@ -78,6 +92,18 @@ describe("resolveInitTarget", () => {await expect(resolveTarget(projectPath, ".")).rejects.toThrow("An eve project already exists");});+ it("stops when a path target is an eve agent workspace", async () => {+ const parentDirectory = await createScratchDirectory("eve-init-target-workspace-parent-");+ const projectPath = join(parentDirectory, "workspace");+ await mkdir(join(projectPath, "agents", "support", "agent"), { recursive: true });+ await writeFile(join(projectPath, "agents", "support", "agent", "agent.ts"), "export {};\n");+ await writeFile(join(projectPath, "package.json"), "{}\n");++ await expect(resolveTarget(parentDirectory, "workspace")).rejects.toThrow(+ "An eve project already exists",+ );+ });+it("lists arbitrary content instead of guessing how to integrate it", async () => {const projectPath = await createScratchDirectory("eve-init-target-content-");await writeFile(join(projectPath, "README.md"), "# Existing\n");diff --git a/packages/eve/src/cli/commands/init-target.ts b/packages/eve/src/cli/commands/init-target.tsindex 69556afb5..7c212d55e 100644--- a/packages/eve/src/cli/commands/init-target.ts+++ b/packages/eve/src/cli/commands/init-target.ts@@ -63,11 +63,25 @@ async function isExistingEveProject(entries: readonly string[],): Promise<boolean> {if (await isAgentRoot(projectPath)) return true;- return (+ if ((entries.includes("package.json") || entries.includes("vercel.json")) &&entries.includes("agent") &&(await isAgentRoot(resolve(projectPath, "agent")))- );+ ) {+ return true;+ }+ if (!entries.includes("agents")) return false;+ const agentDirectories = await readdir(resolve(projectPath, "agents"), {+ withFileTypes: true,+ }).catch(() => []);+ if (!entries.includes("package.json")) return false;+ for (const entry of agentDirectories) {+ if (!entry.isDirectory() || entry.name.startsWith(".")) continue;+ const agentRoot = resolve(projectPath, "agents", entry.name);+ if ((await pathKind(resolve(agentRoot, "agent"))) === "directory") return true;+ if (await isAgentRoot(agentRoot)) return true;+ }+ return false;}function listEntries(entries: readonly string[]): string {@@ -89,7 +103,6 @@ function assertTargetStaysWithinParent(/** Classifies the target itself without walking ancestor projects. */export async function resolveInitTarget(input: ResolveInitTargetInput): Promise<InitTarget> {const parentPath = resolve(input.parentDirectory);- const targetProvided = input.target !== undefined;const projectPath = resolve(parentPath, input.target ?? ".");assertTargetStaysWithinParent(parentPath, input.target, projectPath);const createInPlace = projectPath === parentPath;@@ -164,17 +177,11 @@ export async function resolveInitTarget(input: ResolveInitTargetInput): Promise<if (await isExistingEveProject(projectPath, entries)) {throw new InitTargetError("target_conflict",- `An eve project already exists at "${projectPath}". Run an existing-project command from that directory instead.`,+ `An eve project already exists at "${projectPath}". Run \`eve dev\` from that directory, or use an existing-project command.`,);}if (entries.includes("package.json")) {- if (!targetProvided || input.target !== ".") {- throw new InitTargetError(- "target_conflict",- `Adding eve to an existing package requires an explicit \`eve init .\` from "${projectPath}".`,- );- }return { kind: "existing", projectPath };}diff --git a/packages/eve/src/cli/commands/init.integration.test.ts b/packages/eve/src/cli/commands/init.integration.test.tsindex befaa8184..2e83cb9c9 100644--- a/packages/eve/src/cli/commands/init.integration.test.ts+++ b/packages/eve/src/cli/commands/init.integration.test.ts@@ -233,6 +233,26 @@ describe("runInitCommand", () => {expect(deps.runPackageManagerInstall).not.toHaveBeenCalled();});+ it("does not add a root agent when a path target is an eve workspace", async () => {+ const parentDirectory = await mkdtemp(join(tmpdir(), "eve-init-workspace-path-parent-"));+ const workspaceRoot = join(parentDirectory, "workspace");+ await mkdir(join(workspaceRoot, "agents", "support", "agent"), { recursive: true });+ await writeFile(join(workspaceRoot, "agents", "support", "agent", "agent.ts"), "export {};\n");+ await writeFile(+ join(workspaceRoot, "package.json"),+ '{"name":"workspace","dependencies":{"eve":"*"}}\n',+ );+ const output = logger();+ const deps = dependencies();++ await expect(runInitCommand(output, parentDirectory, "workspace", {}, deps)).rejects.toThrow(+ "An eve project already exists",+ );++ await expect(pathExists(join(workspaceRoot, "agent"))).resolves.toBe(false);+ expect(deps.runPackageManagerInstall).not.toHaveBeenCalled();+ });+it("creates the base agent with the runtime default model and invoking eve dependency", async () => {const parentDirectory = await mkdtemp(join(tmpdir(), "eve-init-base-"));const output = logger();@@ -472,6 +492,41 @@ describe("runInitCommand", () => {expect(packageJson.dependencies.eve).toBe("file:/tmp/eve-0.11.5.tgz");});+ it.each([undefined, "."] as const)(+ "adds eve to the current existing project when target is %j",+ async (target) => {+ const parentDirectory = await mkdtemp(join(tmpdir(), "eve-init-existing-current-"));+ const projectRoot = await createHostProject(parentDirectory);+ const output = logger();+ const deps = dependencies();++ await runInitCommand(output, projectRoot, target, {}, deps);++ await expect(pathExists(join(projectRoot, "agent", "agent.ts"))).resolves.toBe(true);+ expect(deps.runPackageManagerInstall).toHaveBeenCalledWith(+ "pnpm",+ projectRoot,+ expect.anything(),+ );+ },+ );++ it("adds eve to an existing project addressed by a relative path", async () => {+ const parentDirectory = await mkdtemp(join(tmpdir(), "eve-init-existing-path-"));+ const projectRoot = await createHostProject(parentDirectory);+ const output = logger();+ const deps = dependencies();++ await runInitCommand(output, parentDirectory, "host-app", {}, deps);++ await expect(pathExists(join(projectRoot, "agent", "agent.ts"))).resolves.toBe(true);+ expect(deps.runPackageManagerInstall).toHaveBeenCalledWith(+ "pnpm",+ projectRoot,+ expect.anything(),+ );+ });+it.each([undefined, ".", "./"] as const)("scaffolds the current empty directory when target is %j",async (target) => {@@ -1211,7 +1266,7 @@ describe("runInitCommand", () => {const deps = dependencies();await expect(runInitCommand(output, projectRoot, ".", {}, deps)).rejects.toMatchObject({- message: `An eve project already exists at "${projectRoot}". Run an existing-project command from that directory instead.`,+ message: `An eve project already exists at "${projectRoot}". Run \`eve dev\` from that directory, or use an existing-project command.`,});await expect(pathExists(join(projectRoot, "agent/agent.ts"))).resolves.toBe(false);