The Open Framework for Building Agents
fix(eve): resolve code-defined models from the active bundle module map (#156)
Rui10e9237af834
diff --git a/.changeset/dev-code-defined-model-module-map.md b/.changeset/dev-code-defined-model-module-map.mdnew file mode 100644index 000000000..3a2477ab4--- /dev/null+++ b/.changeset/dev-code-defined-model-module-map.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Fix code-defined models under `eve dev`, including NodeNext `.js` imports that target authored `.ts` files. Runtime model resolution now reuses the active agent bundle's module map and node scope, so child agents resolve their own models without rebuilding authored modules on each step.diff --git a/packages/eve/src/execution/node-step.test.ts b/packages/eve/src/execution/node-step.test.tsindex 374488235..3cd9f2612 100644--- a/packages/eve/src/execution/node-step.test.ts+++ b/packages/eve/src/execution/node-step.test.ts@@ -6,6 +6,7 @@ import { AuthKey, InitiatorAuthKey, SessionIdKey, SessionKey } from "#context/keimport { BundleKey, ChannelKey } from "#runtime/sessions/runtime-context-keys.js";import { getPendingRuntimeActionBatch } from "#harness/runtime-actions.js";import type { RuntimeTurnAgent } from "#runtime/agent/bootstrap.js";+import { resolveRuntimeModelReference } from "#runtime/agent/resolve-model.js";import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";import type { ResolvedRuntimeAgentNode } from "#runtime/graph.js";import { createEmptyHookRegistry } from "#runtime/hooks/registry.js";@@ -254,10 +255,14 @@ describe("createExecutionNodeStep", () => {toolRegistry,},);+ const modelResolutionScope = {+ moduleMap: { nodes: {} },+ nodeId: undefined,+ };const step = createExecutionNodeStep({- compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),createRuntime: () => createNoopRuntime(),mode: "task",+ modelResolutionScope,node: rootNode,});@@ -273,6 +278,10 @@ describe("createExecutionNodeStep", () => {);expect(result.next).toEqual({ done: true, output: "tool-output" });+ expect(resolveRuntimeModelReference).toHaveBeenCalledWith(+ rootNode.turnAgent.model,+ modelResolutionScope,+ );});it("records visible subagent tools as pending runtime actions", async () => {@@ -297,9 +306,12 @@ describe("createExecutionNodeStep", () => {}),);const step = createExecutionNodeStep({- compiledArtifactsSource: testCompiledArtifactsSource,createRuntime,mode: "task",+ modelResolutionScope: {+ moduleMap: { nodes: {} },+ nodeId: undefined,+ },node: rootNode,});diff --git a/packages/eve/src/execution/node-step.ts b/packages/eve/src/execution/node-step.tsindex ac929d37a..bcb341766 100644--- a/packages/eve/src/execution/node-step.ts+++ b/packages/eve/src/execution/node-step.ts@@ -9,7 +9,10 @@ import { createLogger } from "#internal/logging.js";import type { RuntimeIdentity } from "#protocol/message.js";import type { RunMode } from "#shared/run-mode.js";import { resolveCodeModeEnabled } from "#shared/code-mode.js";-import { resolveRuntimeModelReference } from "#runtime/agent/resolve-model.js";+import {+ resolveRuntimeModelReference,+ type RuntimeModelResolutionScope,+} from "#runtime/agent/resolve-model.js";import type { RuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";import type { ResolvedRuntimeAgentNode } from "#runtime/graph.js";@@ -44,7 +47,6 @@ export interface CreateExecutionNodeStepInput {* current run.*/readonly capabilities?: SessionCapabilities;- readonly compiledArtifactsSource: RuntimeCompiledArtifactsSource;/*** Runtime constructor used by the subagent tool executor to start* delegated child runs on the same workflow runtime as the parent.@@ -52,6 +54,7 @@ export interface CreateExecutionNodeStepInput {readonly createRuntime: CreateRuntime;readonly handleEvent?: HandleEventFn;readonly mode: RunMode;+ readonly modelResolutionScope: RuntimeModelResolutionScope;readonly node: ResolvedRuntimeAgentNode;}@@ -60,7 +63,7 @@ export interface CreateExecutionNodeStepInput {* tool, sandbox, and subagent wiring.*/export function createExecutionNodeStep(input: CreateExecutionNodeStepInput): StepFn {- const resolveModel = createRuntimeModelResolver(input.compiledArtifactsSource);+ const resolveModel = createRuntimeModelResolver(input.modelResolutionScope);const tools = createNodeHarnessTools({ node: input.node });return createToolLoopHarness({capabilities: input.capabilities,@@ -108,12 +111,9 @@ function buildRuntimeIdentity(node: ResolvedRuntimeAgentNode): RuntimeIdentity {}function createRuntimeModelResolver(- compiledArtifactsSource: RuntimeCompiledArtifactsSource,+ scope: RuntimeModelResolutionScope,): (modelReference: Parameters<typeof resolveRuntimeModelReference>[0]) => Promise<LanguageModel> {- return (modelReference) =>- resolveRuntimeModelReference(modelReference, {- compiledArtifactsSource,- });+ return (modelReference) => resolveRuntimeModelReference(modelReference, scope);}/**diff --git a/packages/eve/src/execution/workflow-steps.ts b/packages/eve/src/execution/workflow-steps.tsindex 218afd4da..4011c3267 100644--- a/packages/eve/src/execution/workflow-steps.ts+++ b/packages/eve/src/execution/workflow-steps.ts@@ -322,10 +322,13 @@ export async function turnStep(rawInput: TurnStepInput): Promise<DurableStepResuconst step = createExecutionNodeStep({capabilities,- compiledArtifactsSource: bundle.compiledArtifactsSource,createRuntime: createWorkflowRuntime,handleEvent,mode,+ modelResolutionScope: {+ moduleMap: bundle.moduleMap,+ nodeId: bundle.nodeId,+ },node: bundle.graph.root,});return step(refreshedSession, stepInput);diff --git a/packages/eve/src/runtime/agent/resolve-model.ts b/packages/eve/src/runtime/agent/resolve-model.tsindex ce3d33616..8229a85ae 100644--- a/packages/eve/src/runtime/agent/resolve-model.ts+++ b/packages/eve/src/runtime/agent/resolve-model.ts@@ -1,31 +1,28 @@import type { LanguageModel } from "ai";-import { ROOT_COMPILED_AGENT_NODE_ID } from "#compiler/manifest.js";+import type { CompiledModuleMap } from "#compiler/module-map.js";import { normalizeAgentDefinition } from "#internal/authored-definition/core.js";-import {- expectObjectRecord,- getAuthoredModuleExport,- materializeAuthoredModuleExport,-} from "#internal/authored-module.js";-import type { ModuleDefinitionExport } from "#public/definitions/source.js";-import type { RuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";-import { loadCompiledModuleMap } from "#runtime/loaders/module-map.js";import type { RuntimeModelReference } from "#runtime/agent/bootstrap.js";import { resolveBootstrapRuntimeModel } from "#runtime/agent/bootstrap-model.js";import {resolveMockAuthoredRuntimeModel,shouldMockAuthoredRuntimeModels,} from "#runtime/agent/mock-model-adapter.js";+import { loadResolvedModuleExport } from "#runtime/resolve-helpers.js";export { shouldMockAuthoredRuntimeModels };+/** Loaded compiled-module scope used to resolve source-backed runtime models. */+export interface RuntimeModelResolutionScope {+ readonly moduleMap: CompiledModuleMap;+ readonly nodeId: string | undefined;+}+/*** Resolves one runtime model reference into the active language model.*/export async function resolveRuntimeModelReference(reference: RuntimeModelReference,- input: {- readonly compiledArtifactsSource?: RuntimeCompiledArtifactsSource;- } = {},+ scope?: RuntimeModelResolutionScope,): Promise<LanguageModel> {const bootstrapModel = resolveBootstrapRuntimeModel(reference);@@ -40,7 +37,7 @@ export async function resolveRuntimeModelReference(}if (isSourceBackedRuntimeModelReference(reference)) {- return await loadSourceBackedRuntimeModelReference(reference, input);+ return await loadSourceBackedRuntimeModelReference(reference, scope);}return reference.id;@@ -50,30 +47,20 @@ async function loadSourceBackedRuntimeModelReference(reference: RuntimeModelReference & {readonly source: NonNullable<RuntimeModelReference["source"]>;},- input: {- readonly compiledArtifactsSource?: RuntimeCompiledArtifactsSource;- },+ scope: RuntimeModelResolutionScope | undefined,): Promise<LanguageModel> {- if (input.compiledArtifactsSource === undefined) {+ if (scope === undefined) {throw new Error(- `Expected an explicit compiled artifact source to resolve the authored runtime model "${reference.id}".`,+ `Expected a compiled module-map scope to resolve the authored runtime model "${reference.id}".`,);}- const moduleMap = await loadCompiledModuleMap({- compiledArtifactsSource: input.compiledArtifactsSource,+ const definition = await loadResolvedModuleExport({+ definition: reference.source,+ kindLabel: `runtime model "${reference.id}"`,+ moduleMap: scope.moduleMap,+ nodeId: scope.nodeId,});- const moduleNamespace =- moduleMap.nodes[ROOT_COMPILED_AGENT_NODE_ID]?.modules[reference.source.sourceId];-- const moduleRecord = expectObjectRecord(- moduleNamespace,- `Missing compiled agent config module for runtime model "${reference.id}" at "${reference.source.logicalPath}".`,- );- const exportValue = getAuthoredModuleExport(moduleRecord, reference.source);- const definition = await materializeAuthoredModuleExport(- exportValue as ModuleDefinitionExport<unknown>,- );const normalizedDefinition = normalizeAgentDefinition(definition,`Expected the authored agent config export "${reference.source.exportName ?? "default"}" from "${reference.source.logicalPath}" to match the public eve shape.`,@@ -86,11 +73,7 @@ async function loadSourceBackedRuntimeModelReference();}- if (typeof model === "string") {- return model;- }-- return model as LanguageModel;+ return model;}function isSourceBackedRuntimeModelReference(diff --git a/packages/eve/test/scenarios/runtime-model-resolution.scenario.test.ts b/packages/eve/test/scenarios/runtime-model-resolution.scenario.test.tsindex ecc51308e..5e0d4f482 100644--- a/packages/eve/test/scenarios/runtime-model-resolution.scenario.test.ts+++ b/packages/eve/test/scenarios/runtime-model-resolution.scenario.test.ts@@ -1,4 +1,4 @@-import { writeFile } from "node:fs/promises";+import { mkdir, writeFile } from "node:fs/promises";import { join } from "node:path";import { afterEach, describe, expect, it, vi } from "vitest";@@ -9,7 +9,7 @@ import { TEST_DEFAULT_MODEL_ID } from "../../src/internal/testing/app-harness.jsimport { resolveBootstrapRuntimeModel } from "../../src/runtime/agent/bootstrap-model.js";import { createMockAuthoredRuntimeModel } from "../../src/runtime/agent/mock-model-adapter.js";import { resolveRuntimeModelReference } from "../../src/runtime/agent/resolve-model.js";-import { createDiskRuntimeCompiledArtifactsSource } from "../../src/runtime/compiled-artifacts-source.js";+import { createAuthoredSourceRuntimeCompiledArtifactsSource } from "../../src/internal/application/runtime-compiled-artifacts-source.js";import { getCompiledRuntimeAgentBundle } from "../../src/runtime/sessions/compiled-agent-cache.js";import { useTemporaryAppRoots } from "../../src/internal/testing/use-temporary-app-roots.js";@@ -55,7 +55,7 @@ describe("runtime model resolution", () => {);});- it("rehydrates source-backed AI SDK model instances from the compiled agent config", async () => {+ it("rehydrates a source-backed model whose module uses a NodeNext TypeScript import", async () => {vi.stubEnv("NODE_ENV", "development");const { agentRoot, appRoot } = await createAppRoot(@@ -63,21 +63,12 @@ describe("runtime model resolution", () => {APP_ROOT_OPTIONS,);+ await mkdir(join(agentRoot, "lib"), { recursive: true });+ await writeFile(join(agentRoot, "lib", "model.ts"), createModelModuleSource("root"));await writeFile(- join(agentRoot, "agent.mjs"),+ join(agentRoot, "agent.ts"),[- "const weatherModel = {",- ' specificationVersion: "v3",',- ' provider: "openai",',- ' modelId: "gpt-4o-mini",',- " supportedUrls: {},",- " async doGenerate() {",- ' throw new Error("not implemented");',- " },",- " async doStream() {",- ' throw new Error("not implemented");',- " },",- "};",+ 'import { weatherModel } from "./lib/model.js";',"","export default {"," model: weatherModel,",@@ -99,7 +90,7 @@ describe("runtime model resolution", () => {startPath: appRoot,});- const compiledArtifactsSource = createDiskRuntimeCompiledArtifactsSource(appRoot);+ const compiledArtifactsSource = createAuthoredSourceRuntimeCompiledArtifactsSource(appRoot);const bundle = await getCompiledRuntimeAgentBundle({compiledArtifactsSource,});@@ -114,13 +105,14 @@ describe("runtime model resolution", () => {},source: {sourceKind: "module",- logicalPath: "agent.mjs",- sourceId: "agent.mjs",+ logicalPath: "agent.ts",+ sourceId: "agent.ts",},});const resolvedModel = await resolveRuntimeModelReference(bundle.turnAgent.model, {- compiledArtifactsSource,+ moduleMap: bundle.moduleMap,+ nodeId: bundle.nodeId,});expect(typeof resolvedModel).not.toBe("string");@@ -133,6 +125,83 @@ describe("runtime model resolution", () => {modelId: "gpt-4o-mini",provider: "openai",specificationVersion: "v3",+ testMarker: "root",+ });+ });++ it("resolves a child model from the active child module-map scope", async () => {+ vi.stubEnv("NODE_ENV", "development");++ const { agentRoot, appRoot } = await createAppRoot(+ "eve-runtime-child-model-resolution-",+ APP_ROOT_OPTIONS,+ );+ const childRoot = join(agentRoot, "subagents", "researcher");++ await mkdir(join(agentRoot, "lib"), { recursive: true });+ await mkdir(join(childRoot, "lib"), { recursive: true });+ await writeFile(join(agentRoot, "lib", "model.ts"), createModelModuleSource("root"));+ await writeFile(join(childRoot, "lib", "model.ts"), createModelModuleSource("child"));+ await writeFile(+ join(agentRoot, "agent.ts"),+ [+ 'import { weatherModel } from "./lib/model.js";',+ "",+ "export default { model: weatherModel };",+ "",+ ].join("\n"),+ );+ await writeFile(join(agentRoot, "instructions.md"), "You are the root agent.\n");+ await writeFile(+ join(childRoot, "agent.ts"),+ [+ 'import { weatherModel } from "./lib/model.js";',+ "",+ "export default {",+ ' description: "Research one topic.",',+ " model: weatherModel,",+ "};",+ "",+ ].join("\n"),+ );+ await writeFile(join(childRoot, "instructions.md"), "You are the research agent.\n");++ await compileAgent({ startPath: appRoot });++ const compiledArtifactsSource = createAuthoredSourceRuntimeCompiledArtifactsSource(appRoot);+ const childBundle = await getCompiledRuntimeAgentBundle({+ compiledArtifactsSource,+ nodeId: "subagents/researcher",+ });+ const resolvedModel = await resolveRuntimeModelReference(childBundle.turnAgent.model, {+ moduleMap: childBundle.moduleMap,+ nodeId: childBundle.nodeId,+ });++ expect(resolvedModel).toMatchObject({+ modelId: "gpt-4o-mini",+ provider: "openai",+ specificationVersion: "v3",+ testMarker: "child",});});});++function createModelModuleSource(testMarker: string): string {+ return [+ "export const weatherModel = {",+ ' specificationVersion: "v3",',+ ' provider: "openai",',+ ' modelId: "gpt-4o-mini",',+ ` testMarker: ${JSON.stringify(testMarker)},`,+ " supportedUrls: {},",+ " async doGenerate() {",+ ' throw new Error("not implemented");',+ " },",+ " async doStream() {",+ ' throw new Error("not implemented");',+ " },",+ "};",+ "",+ ].join("\n");+}