# 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.

## noclick.Memory

```python
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. A user's memory keeps one, its user's.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `str` |  |
| `name` | `str | None` | Its name, unique in its scope (`None` for an agent's own memory). |
| `description` | `str | None` | One line saying what it holds. |
| `agent_id` | `str | None` | The agent whose own memory this is (`None` for a memory with a name). |
| `attached_to` | `list[dict[str, Any]]` | The agents that attach it besides: each one's `id`, `name` and whether it `writes`. |
| `entry_count` | `int | None` | How many entries it holds, in all its layers. |
| `user_id` | `str | None` |  |
| `created_at` | `Any` |  |
| `updated_at` | `Any` |  |

### Memory.create

```python
noclick.Memory.create(
    name, *, description=None, allow_existing=False, user=..., client=None
) -> Memory
```

Create an empty memory called `name` (1-64 letters, digits and
`. _ -`), unique to its user, or to the account outside `as_user` (a
taken name raises `noclick.Conflict` with `existing_id`, or with
`allow_existing=True` returns the memory already there, entries and description as they are).

Parameters:

- `name` (`str`, required)
- `description` (`str | None`, default `None`)
- `allow_existing` (`bool`, default `False`)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Memory` The new, empty memory.

Usage:

```python
company = noclick.Memory.create("company", description="What every agent should know")
company.save("tone", "Warm and brief.", description="How we write")
```

### Memory.from_name

```python
noclick.Memory.from_name(
    name, *, create_if_missing=False, description=None, user=..., client=None
) -> 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
`create_if_missing=True` the current (or given) user's is created on
first use.

Parameters:

- `name` (`str`, required)
- `create_if_missing` (`bool`, default `False`)
- `description` (`str | None`, default `None`)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

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

Usage:

```python
company = noclick.Memory.from_name("company", create_if_missing=True)
support = noclick.Agent.from_name("support", create_if_missing=True,
                                  instructions="Answer billing questions.", memories=[company])
```

### Memory.from_id

```python
noclick.Memory.from_id(memory_id, *, client=None, user=...) -> Memory
```

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

Parameters:

- `memory_id` (`str`, required)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

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

Usage:

```python
support = noclick.Agent.from_name("support")
own = noclick.Memory.from_id(support.memory_id)
print(own.entry_count)
```

### Memory.get

```python
noclick.Memory.get(memory_id, *, user=..., client=None) -> Memory
```

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

Parameters:

- `memory_id` (`str`, required)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

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

Usage:

```python
company = noclick.Memory.get("5d1c9a3e-7b2f-4e60-8a19-3c4d6e8f0b21")
print(company.name, company.entry_count, company.attached_to)
```

### Memory.list

```python
noclick.Memory.list(
    *, agent=None, limit=None, user_filter=None, user=..., client=None
) -> list[Memory]
```

The current user's named memories and the account's (or those `user_filter` covers).
An agent's own memory isn't listed: it's `agent.memory`. With `agent` (an `Agent` or its id),
what that agent reads: its own memory first, then those it attaches, in its order.

Parameters:

- `agent` (`Any`, default `None`)
- `limit` (`int | None`, default `None`): Return at most this many.
- `user_filter` (`Any`, default `None`): Which users a read covers: `noclick.ALL_USERS`, `noclick.NO_USER`, a `User`, or your id for one. It filters without acting as that user.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `list[Memory]` Every matching memory, newest first, fetched page by page.

Usage:

```python
for memory in noclick.Memory.list():
    print(memory.name, memory.entry_count, memory.user_id)   # user_id is None for the account's
reads = noclick.Memory.list(agent=noclick.Agent.from_name("support"))
```

### Memory.list_page

```python
noclick.Memory.list_page(
    *, cursor=None, limit=None, user_filter=None, user=..., client=None
) -> Page
```

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

Parameters:

- `cursor` (`str | None`, default `None`): The `next_cursor` of the page before; omit it for the first page.
- `limit` (`int | None`, default `None`): Return at most this many.
- `user_filter` (`Any`, default `None`): Which users a read covers: `noclick.ALL_USERS`, `noclick.NO_USER`, a `User`, or your id for one. It filters without acting as that user.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

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

Usage:

```python
page = noclick.Memory.list_page(limit=50)
for memory in page.data:
    print(memory.name)
```

### Memory.writable

```python
memory.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: `MemoryLink` What `memories=` takes for a memory the agent writes.

Usage:

```python
team = noclick.Memory.from_name("team-notes", create_if_missing=True)
noclick.Agent.from_name("scribe", create_if_missing=True, instructions="Keep the team's notes.",
                        memories=[team.writable()])
```

### Memory.update

```python
memory.update(*, name=None, description=None) -> Memory
```

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

Parameters:

- `name` (`str | None`, default `None`)
- `description` (`str | None`, default `None`)

Returns: `Memory` This memory, as it is now.

Usage:

```python
noclick.Memory.from_name("company").update(name="handbook")
```

### Memory.delete

```python
memory.delete() -> TrashItem
```

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

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

Usage:

```python
noclick.Memory.from_name("old-notes").delete()
```

### Memory.entries

```python
memory.entries(*, user=...) -> list[MemoryEntry]
```

The entries of one layer: the account's outside `as_user`, else the user's.

Parameters:

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

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

Usage:

```python
for entry in noclick.Memory.from_name("company").entries():
    print(entry.name, entry.description)
```

### Memory.search

```python
memory.search(query, *, limit=None, user=...) -> list[MemoryEntry]
```

Search the memory the way an agent does: a user's entries and the account's. An entry matches when
its name, description or content holds any of the query's words (`refund` finds `refunds`), or its
name or description holds the query.

Parameters:

- `query` (`str`, required)
- `limit` (`int | None`, default `None`): Return at most this many.
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

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

Usage:

```python
for entry in noclick.Memory.from_name("company").search("refund policy"):
    print(entry.name, entry.content)
```

### Memory.read

```python
memory.read(name, *, user=...) -> MemoryEntry
```

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

Parameters:

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

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

Usage:

```python
tone = noclick.Memory.from_name("company").read("tone")
print(tone.version, tone.content)
```

### Memory.save

```python
memory.save(name, content, *, description=None, version=None, user=...) -> 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 you leave it out). With `version`, the save only lands
if the entry is still at that version, else `noclick.Conflict`.

Parameters:

- `name` (`str`, required)
- `content` (`str`, required)
- `description` (`str | None`, default `None`)
- `version` (`int | None`, default `None`)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

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

Usage:

```python
company = noclick.Memory.from_name("company", create_if_missing=True)
company.save("tone", "Warm and brief, no jargon.", description="How we write")
```

### Memory.versions

```python
memory.versions(name, *, user=...) -> list[MemoryEntryVersion]
```

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

Parameters:

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

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

Usage:

```python
for v in noclick.Memory.from_name("company").versions("tone"):
    print(v.version, v.current, v.written_by.type, v.saved_at)
```

### Memory.get_version

```python
memory.get_version(name, version, *, user=...) -> MemoryEntryVersion
```

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

Parameters:

- `name` (`str`, required)
- `version` (`int`, required)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: `MemoryEntryVersion` That version in full.

Usage:

```python
first = noclick.Memory.from_name("company").get_version("tone", 1)
print(first.content)
```

### Memory.restore

```python
memory.restore(name, version, *, user=...) -> 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` (`str`, required)
- `version` (`int`, required)
- `user` (`Any`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

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

Usage:

```python
noclick.Memory.from_name("company").restore("tone", 1)
```

### Memory.forget

```python
memory.forget(name, *, version=None, user=...) -> None
```

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

Parameters:

- `name` (`str`, required)
- `version` (`int | None`, default `None`)
- `user` (`Any`, 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`.

Usage:

```python
noclick.Memory.from_name("company").forget("old-pricing")
```

## noclick.MemoryLink

```python
class MemoryLink
```

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

| Attribute | Type | Description |
| --- | --- | --- |
| `memory` | `Any` |  |
| `writes` | `bool` |  |
