Skip to content

An open-source, faster Devin alternative

TypeScript

agent: one memory contract module; drop cursor from the projection

Igor Zalutskid048702a0fc7
diff --git a/agent/src/index.test.ts b/agent/src/index.test.ts
index 1209ddf2..8b427123 100644
--- a/agent/src/index.test.ts
+++ b/agent/src/index.test.ts
@@ -270,6 +270,90 @@ test("defineMemory rejects providers that are not descriptors", () => {
);
});
+test("defineMemory normalizes a hand-written descriptor like the provider functions do", () => {
+ assert.deepEqual(
+ defineMemory({
+ id: "notes",
+ description: "Notes.",
+ provider: { kind: "document", maxBytes: 4_096 },
+ }).provider,
+ documentMemory({ maxBytes: 4_096 }),
+ );
+ assert.throws(
+ () =>
+ defineMemory({
+ id: "notes",
+ description: "Notes.",
+ provider: { kind: "document", maxBytes: 16_385 },
+ }),
+ /Memory notes maxBytes must be a whole number between 1 and 16384/,
+ );
+ assert.throws(
+ () =>
+ defineMemory({
+ id: "notes",
+ description: "Notes.",
+ provider: { kind: "http", connection: "", path: "/memory", maxBytes: 1, tools: [] },
+ }),
+ /Memory notes requires a defineConnection\(\) connection/,
+ );
+ assert.throws(
+ () =>
+ defineMemory({
+ id: "notes",
+ description: "Notes.",
+ provider: {
+ kind: "http",
+ connection: "memory-service",
+ path: "/memory",
+ maxBytes: 1,
+ tools: [{ name: "x", description: "X.", access: "read", idempotent: false, input: { type: "object", required: ["memory"] } }],
+ },
+ }),
+ /Memory notes tool x input cannot declare the reserved memory argument/,
+ );
+});
+
+test("httpMemory freezes tool descriptors deeply and copies the caller's schema", () => {
+ const open = defineConnection({
+ id: "open-memory",
+ origin: "https://memory.example.com",
+ });
+ const input = {
+ type: "object",
+ properties: { fact: { type: "string", maxLength: 2_000 } },
+ required: ["fact"],
+ };
+ const provider = httpMemory({
+ connection: open,
+ tools: [{ name: "remember", description: "Save.", access: "write", input }],
+ });
+ const tool = provider.tools[0]!;
+ assert.ok(Object.isFrozen(provider));
+ assert.ok(Object.isFrozen(provider.tools));
+ assert.ok(Object.isFrozen(tool));
+ assert.ok(Object.isFrozen(tool.input));
+ assert.ok(Object.isFrozen(tool.input.properties));
+ assert.ok(Object.isFrozen(tool.input.required));
+ assert.ok(
+ Object.isFrozen((tool.input.properties as Record<string, unknown>).fact),
+ );
+ assert.notEqual(tool.input, input);
+ input.properties.fact.maxLength = 1;
+ assert.equal(
+ (tool.input.properties as Record<string, { maxLength: number }>).fact!.maxLength,
+ 2_000,
+ );
+});
+
+test("a projection carries no provider read state", () => {
+ // Cursors stay with the host: the type is the promise, checked at build.
+ const keys: Array<keyof MemoryProjection> = ["text", "sources", "writable"];
+ // @ts-expect-error a cursor is not a hook value.
+ const cursor: keyof MemoryProjection = "cursor";
+ assert.deepEqual([...keys, cursor].length, 4);
+});
+
test("useMemory reads the host projection and records the selection", () => {
const requirements = defineMemory({
id: "requirements",
diff --git a/agent/src/index.ts b/agent/src/index.ts
index 3da96294..598e8f60 100644
--- a/agent/src/index.ts
+++ b/agent/src/index.ts
@@ -1,5 +1,11 @@
import { Cron } from "croner";
+import {
+ memoryId,
+ memoryProjection,
+ type MemoryProjection,
+} from "./memory.js";
+
export type DataValue =
| null
| boolean
@@ -308,78 +314,23 @@ export interface ToolDefinition<
run(context: ToolExecutionContext): Output | Promise<Output>;
}
-export interface MemoryReference extends ResourceReference {
- readonly kind: "memory";
-}
-
-/** Bounded text documents stored by OpenComputer. */
-export interface DocumentMemoryProvider {
- readonly kind: "document";
- /** Maximum UTF-8 size of each document's text, for every writer. */
- readonly maxBytes: number;
-}
-
-export type MemoryToolAccess = "read" | "write";
-
-/**
- * A tool an HTTP memory endpoint executes. The model sees it as
- * `memory_<name>` with the reserved `memory` argument naming the resource, so
- * `input` may not declare a `memory` property of its own.
- */
-export interface HttpMemoryToolDefinition {
- readonly name: string;
- readonly description: string;
- readonly access: MemoryToolAccess;
- /**
- * Only an idempotent write is retried after a transport failure or timeout.
- * Declare it only when the endpoint deduplicates `(partition, operationId)`.
- */
- readonly idempotent: boolean;
- readonly input: ToolInputSchema;
-}
-
-/** Recall and tools served by another service through a declared connection. */
-export interface HttpMemoryProvider {
- readonly kind: "http";
- readonly connection: string;
- readonly path: string;
- /** Maximum UTF-8 size of recalled text. */
- readonly maxBytes: number;
- readonly tools: readonly HttpMemoryToolDefinition[];
-}
-
-export type MemoryProvider = DocumentMemoryProvider | HttpMemoryProvider;
-
-/**
- * A memory resource, registered by the compiler and admitted per session.
- * The same id in every agent of a project names the same resource.
- */
-export interface MemoryDefinition extends MemoryReference {
- readonly version: 1;
- /** Model-facing guidance, shown in the resource's tool descriptions. */
- readonly description: string;
- readonly provider: MemoryProvider;
-}
-
-export interface MemorySource {
- readonly id: string;
- readonly title?: string;
- readonly revision?: string;
- readonly updatedAt?: string;
-}
-
-/**
- * What this model request knows from one bound memory resource. The host
- * resolves it before the render; the shape is the same for every provider.
- */
-export interface MemoryProjection {
- readonly text: string;
- readonly sources: readonly MemorySource[];
- /** Provider read state the host retains for this request's tools; opaque. */
- readonly cursor?: string;
- /** Whether this binding's save tool can commit right now. */
- readonly writable: boolean;
-}
+export type {
+ DocumentMemoryInput,
+ DocumentMemoryProvider,
+ HttpMemoryConnection,
+ HttpMemoryInput,
+ HttpMemoryProvider,
+ HttpMemoryToolDefinition,
+ HttpMemoryToolInput,
+ MemoryDefinition,
+ MemoryDefinitionInput,
+ MemoryProjection,
+ MemoryProvider,
+ MemoryReference,
+ MemorySource,
+ MemoryToolAccess,
+} from "./memory.js";
+export { defineMemory, documentMemory, httpMemory } from "./memory.js";
/**
* Implemented by the host that renders the agent. The host renders inside an
@@ -963,220 +914,6 @@ export function defineTool<Output extends DataValue = DataValue>(input: {
});
}
-const MEMORY_DEFAULT_MAX_BYTES = 8_192;
-const MEMORY_MAX_BYTES_CEILING = 16_384;
-const MEMORY_ID_MAX_LENGTH = 128;
-const HTTP_MEMORY_DEFAULT_PATH = "/memory";
-const HTTP_MEMORY_MAX_TOOLS = 8;
-const HTTP_MEMORY_TOOL_NAME_PATTERN = /^[a-z0-9_]{1,32}$/;
-/** Injected into every memory tool's model-facing schema to select the resource. */
-const MEMORY_RESERVED_ARGUMENT = "memory";
-
-function memoryMaxBytes(value: number | undefined, label: string): number {
- if (value === undefined) return MEMORY_DEFAULT_MAX_BYTES;
- if (
- !Number.isInteger(value) ||
- value < 1 ||
- value > MEMORY_MAX_BYTES_CEILING
- ) {
- throw new Error(
- `${label} maxBytes must be a whole number between 1 and ${MEMORY_MAX_BYTES_CEILING}`,
- );
- }
- return value;
-}
-
-function containsSchemaReference(value: unknown): boolean {
- if (Array.isArray(value)) return value.some(containsSchemaReference);
- if (!value || typeof value !== "object") return false;
- return Object.entries(value as Record<string, unknown>).some(
- ([key, nested]) => key === "$ref" || containsSchemaReference(nested),
- );
-}
-
-function httpMemoryToolInput(value: unknown, label: string): ToolInputSchema {
- if (!value || typeof value !== "object" || Array.isArray(value)) {
- throw new Error(`${label} input must be a JSON Schema object`);
- }
- const schema = value as Record<string, unknown>;
- if (schema.type !== "object") {
- throw new Error(`${label} input must be a JSON Schema with type "object"`);
- }
- if (containsSchemaReference(schema)) {
- throw new Error(`${label} input cannot use $ref`);
- }
- const properties = schema.properties;
- if (
- properties !== undefined &&
- (!properties || typeof properties !== "object" || Array.isArray(properties))
- ) {
- throw new Error(`${label} input properties must be an object`);
- }
- const required = schema.required;
- if (
- required !== undefined &&
- (!Array.isArray(required) ||
- required.some((name) => typeof name !== "string"))
- ) {
- throw new Error(`${label} input required must be an array of strings`);
- }
- if (
- (properties &&
- Object.prototype.hasOwnProperty.call(
- properties,
- MEMORY_RESERVED_ARGUMENT,
- )) ||
- (Array.isArray(required) && required.includes(MEMORY_RESERVED_ARGUMENT))
- ) {
- throw new Error(
- `${label} input cannot declare the reserved ${MEMORY_RESERVED_ARGUMENT} argument; OpenComputer adds it to select the resource`,
- );
- }
- let serialized: string | undefined;
- try {
- serialized = JSON.stringify(schema);
- } catch {
- serialized = undefined;
- }
- if (serialized === undefined) {
- throw new Error(`${label} input must be JSON-compatible`);
- }
- return Object.freeze(JSON.parse(serialized) as Record<string, unknown>);
-}
-
-export interface HttpMemoryToolInput {
- name: string;
- description: string;
- access: MemoryToolAccess;
- idempotent?: boolean;
- input: ToolInputSchema;
-}
-
-export function documentMemory(
- input: { maxBytes?: number } = {},
-): DocumentMemoryProvider {
- return Object.freeze({
- kind: "document" as const,
- maxBytes: memoryMaxBytes(input.maxBytes, "documentMemory"),
- });
-}
-
-export function httpMemory(input: {
- connection: ConnectionReference;
- path?: string;
- maxBytes?: number;
- tools?: readonly HttpMemoryToolInput[];
-}): HttpMemoryProvider {
- const connection = input.connection;
- if (!connection || typeof connection !== "object" || !connection.id) {
- throw new Error("httpMemory requires a defineConnection() connection");
- }
- const path = input.path ?? HTTP_MEMORY_DEFAULT_PATH;
- if (!/^\/(?!\/)[^?#\s]*$/.test(path)) {
- throw new Error(
- "httpMemory path must begin with a single / and contain no query or fragment",
- );
- }
- const policy = connection as Partial<HttpConnectionDefinition>;
- if (policy.pathPrefix && !path.startsWith(policy.pathPrefix)) {
- throw new Error(
- `httpMemory path ${path} is outside connection ${connection.id} pathPrefix ${policy.pathPrefix}`,
- );
- }
- if (policy.methods && !policy.methods.includes("POST")) {
- throw new Error(
- `httpMemory requires connection ${connection.id} to allow POST`,
- );
- }
- const tools = input.tools ?? [];
- if (tools.length > HTTP_MEMORY_MAX_TOOLS) {
- throw new Error(
- `httpMemory may declare at most ${HTTP_MEMORY_MAX_TOOLS} tools`,
- );
- }
- const names = new Set<string>();
- const normalized = tools.map((tool) => {
- const name = typeof tool.name === "string" ? tool.name.trim() : "";
- if (!HTTP_MEMORY_TOOL_NAME_PATTERN.test(name)) {
- throw new Error(
- "httpMemory tool names must use 1 to 32 lowercase letters, numbers, and underscores",
- );
- }
- const label = `httpMemory tool ${name}`;
- if (names.has(name)) throw new Error(`${label} is declared more than once`);
- names.add(name);
- const description =
- typeof tool.description === "string" ? tool.description.trim() : "";
- if (!description) throw new Error(`${label} requires a description`);
- if (tool.access !== "read" && tool.access !== "write") {
- throw new Error(`${label} access must be "read" or "write"`);
- }
- if (tool.idempotent !== undefined && typeof tool.idempotent !== "boolean") {
- throw new Error(`${label} idempotent must be true or false`);
- }
- return Object.freeze({
- name,
- description,
- access: tool.access,
- idempotent: tool.idempotent ?? false,
- input: httpMemoryToolInput(tool.input, label),
- });
- });
- return Object.freeze({
- kind: "http" as const,
- connection: connection.id,
- path,
- maxBytes: memoryMaxBytes(input.maxBytes, "httpMemory"),
- tools: Object.freeze(normalized),
- });
-}
-
-function memoryIdentifier(value: string, kind: string): string {
- const id = resourceIdentifier(value, kind);
- if (id.length > MEMORY_ID_MAX_LENGTH) {
- throw new Error(
- `${kind} IDs must contain at most ${MEMORY_ID_MAX_LENGTH} characters`,
- );
- }
- return id;
-}
-
-export function defineMemory(input: {
- id: string;
- description: string;
- provider?: MemoryProvider;
-}): MemoryDefinition {
- const id = memoryIdentifier(input.id, "defineMemory");
- const description =
- typeof input.description === "string" ? input.description.trim() : "";
- if (!description) {
- throw new Error(`Memory ${id} requires a non-empty description`);
- }
- const provider = input.provider ?? documentMemory();
- if (provider.kind !== "document" && provider.kind !== "http") {
- throw new Error(
- `Memory ${id} provider must be documentMemory() or httpMemory()`,
- );
- }
- return Object.freeze({
- kind: "memory" as const,
- version: 1 as const,
- id,
- description,
- provider,
- });
-}
-
-function isMemoryProjection(value: unknown): value is MemoryProjection {
- if (!value || typeof value !== "object") return false;
- const projection = value as Record<string, unknown>;
- return (
- typeof projection.text === "string" &&
- Array.isArray(projection.sources) &&
- typeof projection.writable === "boolean"
- );
-}
-
/**
* The projection the host recalled for this session's binding of `memory`.
* It never fetches: a session without that binding fails the render here,
@@ -1184,17 +921,11 @@ function isMemoryProjection(value: unknown): value is MemoryProjection {
* this model request; omitting it exposes none of them.
*/
export function useMemory(memory: string | ResourceReference): MemoryProjection {
- const id = memoryIdentifier(
+ const id = memoryId(
typeof memory === "string" ? memory : memory.id,
"useMemory",
);
- const projection = hooks().useMemory(id);
- if (!isMemoryProjection(projection)) {
- throw new Error(
- `Memory ${JSON.stringify(id)} is not bound to this session; create the session with a memory binding for it`,
- );
- }
- return projection;
+ return memoryProjection(id, hooks().useMemory(id));
}
export const useInput = (): Readonly<AgentInput> => hooks().useInput();
diff --git a/agent/src/memory.ts b/agent/src/memory.ts
new file mode 100644
index 00000000..0c695998
--- /dev/null
+++ b/agent/src/memory.ts
@@ -0,0 +1,420 @@
+/**
+ * The memory declaration contract: what `defineMemory()`, `documentMemory()`
+ * and `httpMemory()` accept and the normalized descriptor they produce.
+ *
+ * This module is shared verbatim by `@opencomputer/agent`
+ * (`agent/src/memory.ts`) and the CLI (`cli/src/memory.ts`), where the
+ * compiler runs it on the literal declarations it extracts from agent source
+ * and inlines its compiled form into the artifact's runtime. The two copies
+ * must stay byte-identical; a CLI test enforces it. Keep the module free of
+ * imports so it can be inlined.
+ */
+
+export const MEMORY_DEFAULT_MAX_BYTES = 8_192;
+export const MEMORY_MAX_BYTES_CEILING = 16_384;
+export const MEMORY_ID_MAX_LENGTH = 128;
+export const HTTP_MEMORY_DEFAULT_PATH = "/memory";
+export const HTTP_MEMORY_MAX_TOOLS = 8;
+/** Injected into every memory tool's model-facing schema to select a resource. */
+export const MEMORY_RESERVED_ARGUMENT = "memory";
+/** The built-in document provider's fixed tools, exposed as `memory_<name>`. */
+export const DOCUMENT_MEMORY_TOOLS: readonly string[] = Object.freeze([
+ "save",
+ "read",
+ "list",
+]);
+
+const MEMORY_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
+const HTTP_MEMORY_PATH_PATTERN = /^\/(?!\/)[^?#\s]*$/;
+const HTTP_MEMORY_TOOL_NAME_PATTERN = /^[a-z0-9_]{1,32}$/;
+
+export interface MemoryReference {
+ readonly kind: "memory";
+ readonly id: string;
+}
+
+/** Bounded text documents stored by OpenComputer. */
+export interface DocumentMemoryProvider {
+ readonly kind: "document";
+ /** Maximum UTF-8 size of each document's text, for every writer. */
+ readonly maxBytes: number;
+}
+
+export type MemoryToolAccess = "read" | "write";
+
+export type MemoryToolInputSchema = Readonly<Record<string, unknown>>;
+
+/**
+ * A tool an HTTP memory endpoint executes. The model sees it as
+ * `memory_<name>` with the reserved `memory` argument naming the resource, so
+ * `input` may not declare a `memory` property of its own.
+ */
+export interface HttpMemoryToolDefinition {
+ readonly name: string;
+ readonly description: string;
+ readonly access: MemoryToolAccess;
+ /**
+ * Only an idempotent write is retried after a transport failure or timeout.
+ * Declare it only when the endpoint deduplicates `(partition, operationId)`.
+ */
+ readonly idempotent: boolean;
+ readonly input: MemoryToolInputSchema;
+}
+
+/** Recall and tools served by another service through a declared connection. */
+export interface HttpMemoryProvider {
+ readonly kind: "http";
+ readonly connection: string;
+ readonly path: string;
+ /** Maximum UTF-8 size of recalled text. */
+ readonly maxBytes: number;
+ readonly tools: readonly HttpMemoryToolDefinition[];
+}
+
+export type MemoryProvider = DocumentMemoryProvider | HttpMemoryProvider;
+
+/**
+ * A memory resource, registered by the compiler and admitted per session.
+ * The same id in every agent of a project names the same resource.
+ */
+export interface MemoryDefinition extends MemoryReference {
+ readonly version: 1;
+ /** Model-facing guidance, shown in the resource's tool descriptions. */
+ readonly description: string;
+ readonly provider: MemoryProvider;
+}
+
+export interface MemorySource {
+ readonly id: string;
+ readonly title?: string;
+ readonly revision?: string;
+ readonly updatedAt?: string;
+}
+
+/**
+ * What this model request knows from one bound memory resource. The host
+ * resolves it before the render; the shape is the same for every provider.
+ * Provider read state stays with the host; it is not part of the projection.
+ */
+export interface MemoryProjection {
+ readonly text: string;
+ readonly sources: readonly MemorySource[];
+ /** Whether this binding's save tool can commit right now. */
+ readonly writable: boolean;
+}
+
+export interface DocumentMemoryInput {
+ maxBytes?: number;
+}
+
+export interface HttpMemoryToolInput {
+ name: string;
+ description: string;
+ access: MemoryToolAccess;
+ idempotent?: boolean;
+ input: MemoryToolInputSchema;
+}
+
+/** A `defineConnection()` result, or the policy the compiler resolved for it. */
+export interface HttpMemoryConnection {
+ readonly kind: "connection";
+ readonly id: string;
+ readonly methods?: readonly string[];
+ readonly pathPrefix?: string;
+}
+
+export interface HttpMemoryInput {
+ connection: HttpMemoryConnection;
+ path?: string;
+ maxBytes?: number;
+ tools?: readonly HttpMemoryToolInput[];
+}
+
+export interface MemoryDefinitionInput {
+ id: string;
+ description: string;
+ provider?: MemoryProvider;
+}
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return !!value && typeof value === "object" && !Array.isArray(value);
+}
+
+function deepFreeze<T>(value: T): T {
+ if (value && typeof value === "object" && !Object.isFrozen(value)) {
+ Object.freeze(value);
+ for (const nested of Object.values(value as Record<string, unknown>)) {
+ deepFreeze(nested);
+ }
+ }
+ return value;
+}
+
+export function memoryId(value: unknown, kind: string): string {
+ const id = typeof value === "string" ? value.trim() : "";
+ if (!id) throw new Error(`${kind} requires a non-empty id`);
+ if (!MEMORY_ID_PATTERN.test(id)) {
+ throw new Error(
+ `${kind} IDs must use lowercase letters, numbers, and single hyphens`,
+ );
+ }
+ if (id.length > MEMORY_ID_MAX_LENGTH) {
+ throw new Error(
+ `${kind} IDs must contain at most ${MEMORY_ID_MAX_LENGTH} characters`,
+ );
+ }
+ return id;
+}
+
+function memoryMaxBytes(value: unknown, label: string): number {
+ if (value === undefined) return MEMORY_DEFAULT_MAX_BYTES;
+ if (
+ typeof value !== "number" ||
+ !Number.isInteger(value) ||
+ value < 1 ||
+ value > MEMORY_MAX_BYTES_CEILING
+ ) {
+ throw new Error(
+ `${label} maxBytes must be a whole number between 1 and ${MEMORY_MAX_BYTES_CEILING}`,
+ );
+ }
+ return value;
+}
+
+function httpMemoryPath(value: unknown, label: string): string {
+ if (value === undefined) return HTTP_MEMORY_DEFAULT_PATH;
+ if (typeof value !== "string" || !HTTP_MEMORY_PATH_PATTERN.test(value)) {
+ throw new Error(
+ `${label} path must begin with a single / and contain no query or fragment`,
+ );
+ }
+ return value;
+}
+
+function containsSchemaReference(value: unknown): boolean {
+ if (Array.isArray(value)) return value.some(containsSchemaReference);
+ if (!isRecord(value)) return false;
+ return Object.entries(value).some(
+ ([key, nested]) => key === "$ref" || containsSchemaReference(nested),
+ );
+}
+
+function httpMemoryToolInput(
+ value: unknown,
+ label: string,
+): MemoryToolInputSchema {
+ if (!isRecord(value)) {
+ throw new Error(`${label} input must be a JSON Schema object`);
+ }
+ if (value.type !== "object") {
+ throw new Error(`${label} input must be a JSON Schema with type "object"`);
+ }
+ if (containsSchemaReference(value)) {
+ throw new Error(`${label} input cannot use $ref`);
+ }
+ const properties = value.properties;
+ if (properties !== undefined && !isRecord(properties)) {
+ throw new Error(`${label} input properties must be an object`);
+ }
+ const required = value.required;
+ if (
+ required !== undefined &&
+ (!Array.isArray(required) ||
+ required.some((name) => typeof name !== "string"))
+ ) {
+ throw new Error(`${label} input required must be an array of strings`);
+ }
+ if (
+ (properties &&
+ Object.prototype.hasOwnProperty.call(
+ properties,
+ MEMORY_RESERVED_ARGUMENT,
+ )) ||
+ (Array.isArray(required) && required.includes(MEMORY_RESERVED_ARGUMENT))
+ ) {
+ throw new Error(
+ `${label} input cannot declare the reserved ${MEMORY_RESERVED_ARGUMENT} argument; OpenComputer adds it to select the resource`,
+ );
+ }
+ let serialized: string | undefined;
+ try {
+ serialized = JSON.stringify(value);
+ } catch {
+ serialized = undefined;
+ }
+ if (serialized === undefined) {
+ throw new Error(`${label} input must be JSON-compatible`);
+ }
+ return deepFreeze(JSON.parse(serialized) as Record<string, unknown>);
+}
+
+function httpMemoryTools(
+ value: unknown,
+ label: string,
+): readonly HttpMemoryToolDefinition[] {
+ if (value === undefined) return Object.freeze([]);
+ if (!Array.isArray(value)) {
+ throw new Error(`${label} tools must be an array`);
+ }
+ if (value.length > HTTP_MEMORY_MAX_TOOLS) {
+ throw new Error(
+ `${label} may declare at most ${HTTP_MEMORY_MAX_TOOLS} tools`,
+ );
+ }
+ const names = new Set<string>();
+ const tools = value.map((tool: unknown): HttpMemoryToolDefinition => {
+ if (!isRecord(tool)) {
+ throw new Error(`${label} tools must contain tool objects`);
+ }
+ const name = typeof tool.name === "string" ? tool.name.trim() : "";
+ if (!HTTP_MEMORY_TOOL_NAME_PATTERN.test(name)) {
+ throw new Error(
+ `${label} tool names must use 1 to 32 lowercase letters, numbers, and underscores`,
+ );
+ }
+ const toolLabel = `${label} tool ${name}`;
+ if (names.has(name)) {
+ throw new Error(`${toolLabel} is declared more than once`);
+ }
+ names.add(name);
+ const description =
+ typeof tool.description === "string" ? tool.description.trim() : "";
+ if (!description) throw new Error(`${toolLabel} requires a description`);
+ if (tool.access !== "read" && tool.access !== "write") {
+ throw new Error(`${toolLabel} access must be "read" or "write"`);
+ }
+ if (tool.idempotent !== undefined && typeof tool.idempotent !== "boolean") {
+ throw new Error(`${toolLabel} idempotent must be true or false`);
+ }
+ return Object.freeze({
+ name,
+ description,
+ access: tool.access,
+ idempotent: tool.idempotent === true,
+ input: httpMemoryToolInput(tool.input, toolLabel),
+ });
+ });
+ return Object.freeze(tools);
+}
+
+/**
+ * Normalizes a provider descriptor. Every descriptor a definition carries
+ * passes through here, whether it came from `documentMemory()`,
+ * `httpMemory()` or was written by hand.
+ */
+function memoryProvider(value: unknown, label: string): MemoryProvider {
+ if (!isRecord(value)) {
+ throw new Error(`${label} provider must be documentMemory() or httpMemory()`);
+ }
+ if (value.kind === "document") {
+ return Object.freeze({
+ kind: "document" as const,
+ maxBytes: memoryMaxBytes(value.maxBytes, label),
+ });
+ }
+ if (value.kind === "http") {
+ const connection =
+ typeof value.connection === "string" ? value.connection.trim() : "";
+ if (!connection) {
+ throw new Error(`${label} requires a defineConnection() connection`);
+ }
+ return Object.freeze({
+ kind: "http" as const,
+ connection,
+ path: httpMemoryPath(value.path, label),
+ maxBytes: memoryMaxBytes(value.maxBytes, label),
+ tools: httpMemoryTools(value.tools, label),
+ });
+ }
+ throw new Error(`${label} provider must be documentMemory() or httpMemory()`);
+}
+
+export function documentMemory(
+ input: DocumentMemoryInput = {},
+): DocumentMemoryProvider {
+ const provider = memoryProvider(
+ { kind: "document", maxBytes: input.maxBytes },
+ "documentMemory",
+ );
+ return provider as DocumentMemoryProvider;
+}
+
+export function httpMemory(input: HttpMemoryInput): HttpMemoryProvider {
+ const connection = input.connection;
+ if (
+ !isRecord(connection) ||
+ typeof connection.id !== "string" ||
+ !connection.id
+ ) {
+ throw new Error("httpMemory requires a defineConnection() connection");
+ }
+ const provider = memoryProvider(
+ {
+ kind: "http",
+ connection: connection.id,
+ path: input.path,
+ maxBytes: input.maxBytes,
+ tools: input.tools,
+ },
+ "httpMemory",
+ ) as HttpMemoryProvider;
+ const policy = connection as HttpMemoryConnection;
+ if (policy.pathPrefix && !provider.path.startsWith(policy.pathPrefix)) {
+ throw new Error(
+ `httpMemory path ${provider.path} is outside connection ${provider.connection} pathPrefix ${policy.pathPrefix}`,
+ );
+ }
+ if (policy.methods && !policy.methods.includes("POST")) {
+ throw new Error(
+ `httpMemory requires connection ${provider.connection} to allow POST`,
+ );
+ }
+ return provider;
+}
+
+export function defineMemory(input: MemoryDefinitionInput): MemoryDefinition {
+ const id = memoryId(input.id, "defineMemory");
+ const description =
+ typeof input.description === "string" ? input.description.trim() : "";
+ if (!description) {
+ throw new Error(`Memory ${id} requires a non-empty description`);
+ }
+ const provider =
+ input.provider === undefined
+ ? documentMemory()
+ : memoryProvider(input.provider, `Memory ${id}`);
+ return Object.freeze({
+ kind: "memory" as const,
+ version: 1 as const,
+ id,
+ description,
+ provider,
+ });
+}
+
+/** The model-facing tool names a provider reserves: `memory_<name>`. */
+export function memoryToolNames(provider: MemoryProvider): string[] {
+ const names =
+ provider.kind === "document"
+ ? DOCUMENT_MEMORY_TOOLS
+ : provider.tools.map((tool) => tool.name);
+ return names.map((name) => `${MEMORY_RESERVED_ARGUMENT}_${name}`);
+}
+
+/**
+ * The projection the host resolved for the session's binding of `id`, or a
+ * render failure when the session has no such binding.
+ */
+export function memoryProjection(id: string, value: unknown): MemoryProjection {
+ if (
+ !isRecord(value) ||
+ typeof value.text !== "string" ||
+ !Array.isArray(value.sources) ||
+ typeof value.writable !== "boolean"
+ ) {
+ throw new Error(
+ `Memory ${JSON.stringify(id)} is not bound to this session; create the session with a memory binding for it`,
+ );
+ }
+ return value as unknown as MemoryProjection;
+}