# run()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/local-agent/run/
Language: zh

`agent.run({ message | inputs, history?, onEvent?, edit? }) → Promise<Result>`

一种交换。它循环到模型停止要求工具, 将所有东西都通过 Event 流到 Event 上, 并返回它结束的结果。

Status: the loop · onEvent cannot break it · one at a time

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `message` | string |  | 文本. 简简形式 `inputs`. |
| `inputs` | array |  | `[{ type: "text", content }, { type: "image", content: base64, mime }]`. |
| `history` | array |  | `[{ role, content }]` - 之前的谈话, 对于一个代理没有治疗。 |
| `onEvent` | function |  | 接收每个 [事件事件](/zh/sdk/agent/events)从未等待过;扔的东西被吞没。 |
| `edit` | integer |  | 先回到该信件, 然后用新文本运行 。 |

其中一个 `message` 和 `inputs` 既无所需要,也无所需要,而且抛掷 `inputs_required`.

## 发送图像 {#images}

```javascript
await agent.run({
  inputs: [
    { type: "text", content: "What is wrong with this layout?" },
    { type: "image", content: base64, mime: "image/png" },
  ],
});
```

## 返回值 {#returns}

```javascript
{
  text: "I read the four notes. Two are unfinished: …",
  todos: [{ content: "Check the figures", status: "completed" }],
  usage: { credits: 31, turns: 6, toolCalls: 9 },
  stopReason: "end_turn",      // "stopped" | "max_steps" | "budget"
  model: "gemini-3.5-flash"
}
```

> **使用文本前读取停止站**
>
> 仅 end_turn 意思是模型完成了 说它必须说的话。 max_steps 或预算, 文本是它上个写过的东西—— 可能是计划中。

## 历史, 对于没有会话的代理 {#history}

和他谈话的种子 和他没有关系 `memory: true` 讨论会本身的历史获胜,而这一点却被忽视了——这是对话属于你们数据库而不是我们数据库的正确形式。

## 编辑: 修改早期消息 {#edit}

```javascript
await agent.run({
  edit: 2,
  message: "Actually, rename them to kebab-case.",
});
```

内容在信息2——包括文件——发出之前 `rewound`,然后用新文本运行。它需要会话和检查点,如果其中之一缺失,在开始之前就会扔掉。

## 是什么使它拒绝 {#errors}

每一次重试后,每个模式都失败的转折、不合适的背景、未经授权的会话或拒绝信贷,其他所有东西——一个扔工具、一个被拒绝的工具——都在循环中处理,这里从来不浮出水面。

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

try {
  await agent.run({ message });
} catch (err) {
  if (err instanceof NotEnoughCredits) { /* never retried, on any model */ }
  else if (err instanceof IaGenifyError && err.code === "context_too_large") await agent.compact();
  else throw err;
}
```

- [执行循环](/zh/sdk/agent/loop): 多么一个步骤。
- [事件](/zh/sdk/agent/events): 都收到Event收到的一切
- [stop()](/zh/sdk/ref/local-agent/stop): 早点结束

```python
agent.run(inputs=[
    {"type": "text", "content": "What is wrong with this layout?"},
    {"type": "image", "content": base64_png, "mime": "image/png"},
])
```

```python
{
    "text": "I read the four notes. Two are unfinished: …",
    "todos": [{"content": "Check the figures", "status": "completed"}],
    "usage": {"credits": 31, "turns": 6, "toolCalls": 9},
    "stopReason": "end_turn",      # "stopped" | "max_steps" | "budget"
    "model": "gemini-3.5-flash",
    "context": {},                 # the context meter, as in agent.context
}
```

```python
agent.run(
    edit=2,
    message="Actually, rename them to kebab-case.",
)
```

```python
from iagenify import IaGenifyError, NotEnoughCredits

try:
    agent.run(message=message)
except NotEnoughCredits:
    pass  # never retried, on any model
except IaGenifyError as error:
    if error.code != "context_too_large":
        raise
    agent.compact()
```
