# queue()

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

`agent.queue(message | { message?, inputs? }) → { queued: true, id, position } | { queued: false, reason }`

在代理工作时发送消息。它会等待正在运行的工具，并在模型的下一轮之前送达——由同一次运行来回答。SDK 0.5.4+。

Status: synchronous · read before the next turn · never lost

```javascript
const running = agent.run({ message: "Clean up the Notes folder" });

// Later, while a tool is still running:
agent.queue("Also keep a copy of plan.md");
// { queued: true, id: "u1", position: 1 }

const result = await running;   // the copy was made in the same run
```

## 消息何时被读取 {#when}

在模型的下一轮之前、当前轮次的工具都已返回之后。已在运行的工具绝不会因此被中断。即将结束的运行（模型已经答完）会再多进行一轮，因此最后一秒写下的消息也不会丢失。

## 当没有运行时 {#not-running}

它返回 `{ queued: false, reason: "not_running" }`，且不保留任何内容：在 `run()` 之前、运行结束之后，或在 `stop()` 之后。请用 `run()` 将文本作为新消息发送。

## 未被读取的内容 {#what-is-left}

如果运行在读取之前结束——被停止、预算用尽、步数用尽或失败——消息会出现在 `result.unsent` 中，形式为 `[{ id, text }]`。可以把它放回输入框，或作为下一条消息发送。

## 图像图像 {#images}

`{ message, inputs: [{ type: "image", content, mime }] }` 会把图片与文本一起加入队列。它们随消息进入对话，就像 `run()` 的输入一样。

## 在 Python 中 {#python}

`agent.run()` 会阻塞，因此 `agent.queue()` 要从另一个线程调用——例如界面或 `on_event` 回调。它是线程安全的。SDK Python 0.2.0+。

- [unqueue()](/zh/sdk/ref/local-agent/unqueue): 撤回一条消息。
- [user_message](/zh/sdk/agent/events/user-message): 消息被读取时的事件。
- [stop()](/zh/sdk/ref/local-agent/stop): 队列会怎样。

```python
import threading

results = []
worker = threading.Thread(target=lambda: results.append(agent.run(message="Clean up the Notes folder")))
worker.start()

# From another thread, while a tool is still running:
agent.queue("Also keep a copy of plan.md")
# {"queued": True, "id": "u1", "position": 1}

worker.join()   # the copy was made in the same run
```
