# Memory

> Memories: named notes any agent of their scope can attach.

```python
company = noclick.Memory.from_name("company", create_if_missing=True)
company.save("refunds", "Within 30 days, to the original card.", description="Our refund rules")
support = noclick.Agent.from_name("support", create_if_missing=True, instructions="...",
                                  memories=[company])              # it reads company
closer = noclick.Agent.from_name("closer", create_if_missing=True, instructions="...",
                                 memories=[company.writable()])     # it reads and writes it
```

Every agent also has a memory of its own, `agent.memory`, which it always
reads and writes. A memory attached to it is read-only unless attached with
`writable()`; every turn starts with the names and descriptions of every
entry it can read, its own memory's first.

## Memory

```typescript
class Memory
```

Named notes any agent of its scope can attach: the account's (every user's agents may read them)
or one user's. Its entries are what `agent.memory` holds: a name, a one-line description, Markdown
content and a version. A memory of the account keeps two layers, as an account agent's own memory
does: its own entries, and each user's, which only that user's conversations see.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `name` | `string | null` | Its name, unique in its scope (null for an agent's own memory). |
| `description` | `string | null` | One line saying what it holds. |
| `agentId` | `string | null` | The agent whose own memory this is (null for a memory with a name). |
| `attachedTo` | `MemoryAttachment[]` | The agents that attach it besides, and whether each writes it. |
| `entryCount` | `number | null` | How many entries it holds, in all its layers. |
| `userId` | `string | null` |  |
| `createdAt` | `Date | null` |  |
| `updatedAt` | `Date | null` |  |

Types:

```typescript
/** An agent that attaches a memory, and whether it may write it. */
interface MemoryAttachment {
  id: string;
  name: string;
  writes: boolean;
}
```

### Memory.create

```typescript
static create(
  name: string,
  options?: CallOptions & AllowExisting & { description?: string },
): Promise<Memory>
```

Create an empty memory (1-64 letters, digits and `. _ -`), unique by name per user, or for the
account outside `asUser` (a taken name throws `Conflict` with `existingId`, or with `allowExisting: true`
returns the memory already there, entries and description as they are).

Parameters:

- `name` (`string`, required)
- `options.description` (`string`)
- `options.allowExisting` (`boolean`): A taken name returns the object already there, as it is: nothing about it changes.
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: The new, empty memory.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.fromName

```typescript
static fromName(
  name: string,
  options?: CallOptions & { createIfMissing?: boolean; description?: string },
): Memory
```

A lazy reference to the memory called `name`. Acting for a user, that user's own memory is found
first, then the account's. With `createIfMissing` the current (or given) user's is created on first use.

Parameters:

- `name` (`string`, required)
- `options.createIfMissing` (`boolean`)
- `options.description` (`string`)
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: A memory that loads (or is created) the first time it's used. Nothing is sent yet.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.fromId

```typescript
static fromId(memoryId: string, options?: CallOptions): Memory
```

A lazy reference to a memory by id, such as an agent's own (`agent.memoryId`).

Parameters:

- `memoryId` (`string`, required)
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.

Returns: A memory that loads the first time you read a field. Nothing is sent yet.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.get

```typescript
static get(memoryId: string, options?: CallOptions): Promise<Memory>
```

Load a memory by id now; an unknown id raises `noclick.NotFound`.

Parameters:

- `memoryId` (`string`, required)
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: The memory, loaded: who attaches it and how many entries it holds.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.list

```typescript
static list(
  options?: ReadOptions & { limit?: number; agent?: string | ApiObject },
): Promise<Memory[]>
```

The current user's named memories and the account's (or those `userFilter` covers); an agent's own is
`agent.memory`. With `agent`, what that agent reads: its own memory first, then those it attaches, in order.

Parameters:

- `options.agent` (`string | ApiObject`)
- `options.limit` (`number`): Return at most this many.
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: Every matching memory, newest first, fetched page by page.

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;
```

### Memory.listPage

```typescript
static listPage(options?: ReadOptions & PageOptions): Promise<Page<Memory>>
```

One page of named memories; pass `nextCursor` back as `cursor`.

Parameters:

- `options.cursor` (`string | null`): The `next_cursor` of the page before; omit it for the first page.
- `options.limit` (`number`): Return at most this many.
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: A page whose `data` holds the memories and whose `next_cursor` is `None` on the last page.

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;
```

### Memory.writable

```typescript
writable(): MemoryLink
```

This memory, for an agent's `memories`, with writes: the agent may save to it and forget from it,
not only read it. Only a call acting for the memory's own scope can attach it so.

Returns: What `memories=` takes for a memory the agent writes.

### Memory.update

```typescript
update(changes: { name?: string; description?: string }): Promise<Memory>
```

Rename it, or change its description; an agent's own memory takes no name.

Parameters:

- `changes` (`{ name?: string; description?: string }`, required)

Returns: This memory, as it is now.

### Memory.delete

```typescript
delete(): Promise<TrashItem>
```

Delete it and every entry in it: it goes to the trash, restorable for 30 days, and comes off every agent that
attaches it (restoring puts it back on them); an agent's own memory goes only with its agent.

Returns: Its `TrashItem`: `restore()` puts it back.

### Memory.entries

```typescript
entries(options?: { user?: UserOption }): Promise<MemoryEntry[]>
```

The entries of one layer (the account's outside `asUser`, else the user's), by name, without their content.

Parameters:

- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: Each entry's name, description, version and when it last changed, by name; `content` is `None`
here, so `read` an entry for its body.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.search

```typescript
search(
  query: string,
  options?: { limit?: number; user?: UserOption },
): Promise<MemoryEntry[]>
```

Search it the way an agent does (a user's entries and the account's): entries whose name, description
or content holds any of the query's words, best first, with content (10 unless `limit` says otherwise).

Parameters:

- `query` (`string`, required)
- `options.limit` (`number`): Return at most this many.
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: The best matches first (10 unless `limit` says otherwise, at most 100), with their `content`.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.read

```typescript
read(name: string, options?: { user?: UserOption }): Promise<MemoryEntry>
```

One entry in full; a name with no entry throws `NotFound`.

Parameters:

- `name` (`string`, required)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: The entry with its `content` and the `version` to pass back when you change it.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.save

```typescript
save(
  name: string,
  content: string,
  options?: { description?: string; version?: number; user?: UserOption },
): Promise<MemoryEntry>
```

Write an entry, creating it if the name is new. `description` is the line every agent attaching the
memory sees each turn (required for a new entry, kept when omitted). With `version` the save only lands
if the entry is still at that version, else `Conflict`.

Parameters:

- `name` (`string`, required)
- `content` (`string`, required)
- `options.description` (`string`)
- `options.version` (`number`)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: The entry as saved, with its new `version`.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.versions

```typescript
versions(
  name: string,
  options?: { user?: UserOption },
): Promise<MemoryEntryVersion[]>
```

An entry's versions, newest first: the one it holds now, then those kept from earlier saves (its
last 50 in all), without their content. A name with no entry throws `NotFound`.

Parameters:

- `name` (`string`, required)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: Each version's number, description, who wrote it and when, without its `content`.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.getVersion

```typescript
getVersion(
  name: string,
  version: number,
  options?: { user?: UserOption },
): Promise<MemoryEntryVersion>
```

One version of an entry, with its content; one it no longer keeps throws `NotFound`.

Parameters:

- `name` (`string`, required)
- `version` (`number`, required)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: That version in full.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.restore

```typescript
restore(
  name: string,
  version: number,
  options?: { user?: UserOption },
): Promise<MemoryEntry>
```

Put an entry back as it was at `version`: a new version holding that one's content and
description, so the one it replaces is kept too.

Parameters:

- `name` (`string`, required)
- `version` (`number`, required)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: The entry as restored, at its new `version`.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Memory.forget

```typescript
forget(
  name: string,
  options?: { version?: number; user?: UserOption },
): Promise<void>
```

Forget an entry; with `version`, only if it is still at that version (else `Conflict`).

Parameters:

- `name` (`string`, required)
- `options.version` (`number`)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: None once it's gone; a name with no entry raises `noclick.NotFound`.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

## MemoryLink

```typescript
class MemoryLink
```

A memory attached with writes (`memory.writable()`): the agent may change it as well as read it.

| Attribute | Type | Description |
| --- | --- | --- |
| `memory` | `Memory | AgentMemory` |  |
| `writes` | `true` |  |
