Skip to content

An open-source, faster Devin alternative

TypeScript

cli: send memory declarations with deployment registration

Igor Zalutskic8114278b31a
diff --git a/cli/src/api.ts b/cli/src/api.ts
index 1e05116b..ce0351f9 100644
--- a/cli/src/api.ts
+++ b/cli/src/api.ts
@@ -1,7 +1,7 @@
import { createHash } from "node:crypto";
import type { ResolvedConfig } from "./config.js";
-import type { ProjectResourceManifest } from "./project.js";
+import type { MemoryDeclaration, ProjectResourceManifest } from "./project.js";
export interface OpenComputerIdentity {
user_id: string | null;
@@ -731,6 +731,7 @@ export class OpenComputerClient {
pathPrefix?: string;
redirectOrigins?: Array<{ origin: string; pathPrefix?: string }>;
}>;
+ memory: MemoryDeclaration[];
projectDeployment?: {
id: string;
digest: string;
diff --git a/cli/src/dev.test.ts b/cli/src/dev.test.ts
index c3ec159f..61d5d2a8 100644
--- a/cli/src/dev.test.ts
+++ b/cli/src/dev.test.ts
@@ -143,3 +143,112 @@ test("development publish synchronizes every configured project agent", async ()
await rm(parent, { recursive: true, force: true });
}
});
+
+const WORKSHOP_MEMORY = (maxBytes: string) => `import { defineMemory, documentMemory } from "@opencomputer/agent";
+
+export const requirements = defineMemory({
+ id: "requirements",
+ description: "Verified requirements for a workshop.",
+ provider: documentMemory({ maxBytes: ${maxBytes} }),
+});
+`;
+
+const WORKSHOP_AGENT = `import { useMemory } from "@opencomputer/agent";
+import { requirements } from "./memory";
+
+export default function Agent() {
+ return "Requirements: " + useMemory(requirements).text;
+}
+`;
+
+test("development publish registers memory declarations with the deployment", async () => {
+ const parent = await mkdtemp(resolve(tmpdir(), "opencomputer-dev-memory-"));
+ try {
+ const initialized = await initializeAgentProject(resolve(parent, "app"), {
+ id: "prj_test",
+ name: "Test project",
+ agentId: "hello-agent",
+ });
+ await writeFile(resolve(initialized.agentRoot, "memory.ts"), WORKSHOP_MEMORY("8_192"));
+ await writeFile(resolve(initialized.agentRoot, "agent.ts"), WORKSHOP_AGENT);
+ let input:
+ | Parameters<
+ Parameters<typeof publishDevelopment>[0]["registerDeployment"]
+ >[0]
+ | undefined;
+ const client = {
+ async registerDeployment(value: NonNullable<typeof input>) {
+ input = value;
+ return {
+ id: `${value.agentId}:${value.source.digest}`,
+ agentId: value.agentId,
+ alias: value.alias,
+ createdAt: new Date(0).toISOString(),
+ };
+ },
+ };
+ await publishDevelopment(client, initialized.agentRoot, "hello-agent");
+ assert.deepEqual(input?.memory, [
+ {
+ id: "requirements",
+ description: "Verified requirements for a workshop.",
+ provider: { kind: "document", maxBytes: 8192 },
+ },
+ ]);
+ assert.deepEqual(JSON.parse(JSON.stringify(input)).memory, input?.memory);
+ } finally {
+ await rm(parent, { recursive: true, force: true });
+ }
+});
+
+test("project publish requires agents to agree on a shared memory resource", async () => {
+ const parent = await mkdtemp(resolve(tmpdir(), "opencomputer-dev-memory-project-"));
+ try {
+ const initialized = await initializeAgentProject(resolve(parent, "app"));
+ await writeFile(resolve(initialized.agentRoot, "memory.ts"), WORKSHOP_MEMORY("8_192"));
+ await writeFile(resolve(initialized.agentRoot, "agent.ts"), WORKSHOP_AGENT);
+ const echoRoot = resolve(initialized.root, "opencomputer", "agents", "echo");
+ await mkdir(echoRoot, { recursive: true });
+ await writeFile(resolve(echoRoot, "memory.ts"), WORKSHOP_MEMORY("8_192"));
+ await writeFile(resolve(echoRoot, "agent.ts"), WORKSHOP_AGENT);
+ await writeFile(
+ resolve(initialized.root, "opencomputer", "project.ts"),
+ 'export default { name: "app", agents: ["hello-world", "echo"] };\n',
+ );
+ const published: string[][] = [];
+ const client = {
+ async registerDeployment(
+ value: Parameters<
+ Parameters<typeof publishDevelopment>[0]["registerDeployment"]
+ >[0],
+ ) {
+ published.push(value.memory.map((declaration) => declaration.id));
+ return {
+ id: `${value.agentId}:${value.source.digest}`,
+ agentId: value.agentId,
+ alias: value.alias,
+ createdAt: new Date(0).toISOString(),
+ };
+ },
+ };
+ const binding = {
+ version: 1 as const,
+ apiUrl: "https://app.opencomputer.dev",
+ projectId: "prj_test",
+ projectName: "Test",
+ agentId: "test-agent",
+ };
+ await publishProjectDevelopment(client, initialized.root, binding);
+ assert.deepEqual(published, [["requirements"], ["requirements"]]);
+
+ await writeFile(resolve(echoRoot, "memory.ts"), WORKSHOP_MEMORY("4_096"));
+ published.length = 0;
+ await assert.rejects(
+ publishProjectDevelopment(client, initialized.root, binding),
+ /Memory "requirements" is declared with different configuration in agent hello-world and agent echo/,
+ );
+ assert.deepEqual(published, []);
+ } finally {
+ await rm(parent, { recursive: true, force: true });
+ }
+});
diff --git a/cli/src/dev.ts b/cli/src/dev.ts
index 0ee5ef6b..74c1cce8 100644
--- a/cli/src/dev.ts
+++ b/cli/src/dev.ts
@@ -15,6 +15,7 @@ import {
import { startGateway } from "./local.js";
import {
buildAgentArtifact,
+ mergeMemoryDeclarations,
readProjectAgents,
readProjectResources,
type BuiltAgentArtifact,
@@ -64,6 +65,7 @@ async function registerBuiltDeployment(
channels: built.channels,
connections: built.connections,
httpConnections: built.httpConnections,
+ memory: built.memory,
...(projectDeployment ? { projectDeployment } : {}),
source: {
digest: built.digest,
@@ -114,6 +116,14 @@ export async function publishProjectDeployment(
),
});
}
+ // Agents of one project share memory resources by id, so their
+ // declarations must agree before any of them registers.
+ mergeMemoryDeclarations(
+ builtAgents.map(({ source, built }) => ({
+ origin: `agent ${source.localId}`,
+ memory: built.memory,
+ })),
+ );
const digest = createHash("sha256")
.update(
JSON.stringify({
diff --git a/cli/src/template.ts b/cli/src/template.ts
index 7ebe9d1e..985f7402 100644
--- a/cli/src/template.ts
+++ b/cli/src/template.ts
@@ -2,9 +2,11 @@ import { readFile, stat } from "node:fs/promises";
import { resolve } from "node:path";
import {
buildAgentArtifact,
+ mergeMemoryDeclarations,
readProjectAgents,
readProjectResources,
type HttpConnectionManifest,
+ type MemoryDeclaration,
type ProjectResourceManifest,
} from "./project.js";
@@ -47,6 +49,7 @@ export interface TemplateBuildArtifact {
body: string;
connections: string[];
httpConnections: HttpConnectionManifest[];
+ memory: MemoryDeclaration[];
}
export interface TemplateBuildBundle {
@@ -323,6 +326,7 @@ export async function buildTemplateProject(
body: built.body.toString("utf8"),
connections: built.connections,
httpConnections: built.httpConnections,
+ memory: built.memory,
});
for (const connection of built.connections)
compiledConnections.add(connection);
@@ -366,6 +370,12 @@ export async function buildTemplateProject(
);
}
}
+ mergeMemoryDeclarations(
+ artifacts.map((artifact) => ({
+ origin: `agent ${artifact.localAgentId}`,
+ memory: artifact.memory,
+ })),
+ );
if (
template.template.firstRun &&
!sources.some(
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index c79882a0..16bf266a 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -1407,6 +1407,13 @@ describe("managed agents proxy", () => {
},
},
],
+ memory: [
+ {
+ id: "requirements",
+ description: "Verified requirements.",
+ provider: { kind: "document", maxBytes: 8192 },
+ },
+ ],
source: {
digest,
size: source.length,
@@ -1445,6 +1452,13 @@ describe("managed agents proxy", () => {
origin: "https://api.github.com",
}),
],
+ memory: [
+ {
+ id: "requirements",
+ description: "Verified requirements.",
+ provider: { kind: "document", maxBytes: 8192 },
+ },
+ ],
});
expect(JSON.stringify(await response.json())).not.toMatch(
/bucket|imageArn|arn:aws|uploads\.test/i,
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index a916bb5d..3fb7b87d 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -1102,6 +1102,7 @@ async function deploySourceAgent(
httpConnections: Array.isArray(body.httpConnections)
? body.httpConnections
: [],
+ memory: Array.isArray(body.memory) ? body.memory : [],
...(body.projectDeployment && typeof body.projectDeployment === "object"
? { projectDeployment: body.projectDeployment }
: {}),