# save()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/memory/save/
Language: zh

`api.memory.save(note) → Promise<Memory>`

创建便条,或用同名替换。名称是身份——没有单独的更新。

Status: POST /api/ai/memories · free · upsert by name

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `name` | string | 必填 | 小写、数字和连字符,最多为64。 其它任何文件都转换为文件;不能拒绝的文件。 |
| `content` | string | 必填 | 便条本身, 最多有4,000个字符。 空的已被拒绝 。 |
| `description` | string |  | 一条线,最多200。这就是代理商在选择时所看到的——见 [下 数](#description). |
| `type` | string |  | `preference` · `project` · `fact` · `correction` · `other`。未知值变为 `other`. |
| `source` | string |  | `user` 或 `agent`。任何非 `user` 成为 `agent`. |
| `sessionId` | string |  | 本届会议是在有一次研讨会时学到的。 |

## 姓名是身份 {#name}

保存时保存的名称已经存在, 取代了该注。 这是整个更新机制, 这就是为什么这个名称值得选择 : 这是以后您必须匹配的东西 。

```javascript
await api.memory.save({ name: "tone-of-voice", content: "Short answers.", source: "user" });
await api.memory.save({ name: "tone-of-voice", content: "Short answers, in French.", source: "user" });
// One note. The second content replaced the first.
```

| 你过 | 存储为 |
| --- | --- |
| 声音音音音音音音音 | 声音音调 |
| user_preferences | 用户首选项 |
| 下水! 下水! | d- 就业 |
| ! ! ! | 被拒绝 - invalid_name |

> **名称被转换为, 未经验证**
>
> 有两个不同的名称可以在一个上相交, 第二个保存后不说替换第一个注。 选择平坦、 稳定的名称, 并从用户输入中读回您创建时所返回的名称 。

## 写入代理的描述 {#description}

只有当注注的内容看起来相关时,才打开内容,而相关性仅根据描述来判断。所以描述应该说 *此时,这经文值得读读*而不是它所包含的东西

| 描述不严 | 更好 |
| --- | --- |
| “首选” | ”这个用户如何想被写到 |
| 数据库 | “使用哪个数据库, 以及永远别碰哪个数据库?” |
| “关于该项目的说明 ” | “该项目的部署规则,包括禁止的内容” |

## 返回值 {#returns}

```javascript
const note = await api.memory.save({
  name: "deploy-rules",
  description: "The deploy rules for this project, including what is forbidden",
  content: "No deploys on Friday. Staging first, always. Migrations run separately.",
  type: "project",
  source: "user",
});

note.name;        // "deploy-rules"
note.type;        // "project"
note.updatedAt;
```

保存的便条—— 在名称被转换后, 文本被剪短到它的极限。 读回去, 而不是假设你发送的东西是保存的 。

## 错误 {#errors}

| 代码 | 状态 | 触发条件 |
| --- | --- | --- |
| invalid_name | 400 | 转换后,没有留下任何有用的东西。 |
| content_required | 400 | 内容为空或仅是空白空格 。 |
| too_many_memories | 400 | 已经200便士了,这将是一个新的。 |

`too_many_memories` 仅适用于 *新* 注意: 替换一个现有的顶部工作, 因为它没有增加任何内容。 这是故意的, 满满不应该阻止你纠正错误。

```javascript
import { IaGenifyError } from "iagenify";

try {
  await api.memory.save({ name, content, description, type, source: "user" });
} catch (err) {
  if (err instanceof IaGenifyError && err.code === "too_many_memories") {
    // Let the user choose what goes. Do not evict on their behalf.
    return { needsPruning: await api.memory.list() };
  }
  throw err;
}
```

## 源代码, 以及为什么它值得设置 {#source}

任何非 `"user"` 记录为 `agent`诚实地说,这是让一个界面分开的东西,你告诉我这个,从我研究出来的“我研究出来的”——第二个值得更容易地质疑。

- [list()](/zh/sdk/ref/memory/list): 读取所有内容,包括内容。
- [delete()](/zh/sdk/ref/memory/delete): 正在删除一个。

```python
api.memory.save(name="tone-of-voice", content="Short answers.", source="user")
api.memory.save(name="tone-of-voice", content="Short answers, in French.", source="user")
# One note. The second content replaced the first.
```

```python
note = api.memory.save(
    name="deploy-rules",
    description="The deploy rules for this project, including what is forbidden",
    content="No deploys on Friday. Staging first, always. Migrations run separately.",
    type="project",
    source="user",
)

note["name"]        # "deploy-rules"
note["type"]        # "project"
note["updatedAt"]
```

```python
from iagenify import IaGenifyError

try:
    api.memory.save(name=name, content=content, description=description, type=kind, source="user")
except IaGenifyError as error:
    if error.code != "too_many_memories":
        raise
    # Let the user choose what goes. Do not evict on their behalf.
    needs_pruning = api.memory.list()
```
