Skip to content

Commit 68a8091

Browse files
authored
feat(agent): add conversation_id to run_turn (#11357)
* feat(agent): add conversation_id to run_turn * test: omit standalone conversation ID coverage
1 parent 39773d1 commit 68a8091

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

‎astrbot/core/agent_service.py‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import asyncio
6+
import warnings
67
from collections.abc import AsyncIterator, Sequence
78
from copy import deepcopy
89
from dataclasses import dataclass
@@ -187,7 +188,7 @@ async def follow_up(
187188
input_message=input_message,
188189
send_to_user=self.send_to_user if send_to_user is None else send_to_user,
189190
source_event=self._event,
190-
session_id=session.id,
191+
conversation_id=session.id,
191192
)
192193

193194
async def steer(
@@ -348,8 +349,9 @@ async def run_turn(
348349
input_message: Sequence[InputComponent],
349350
send_to_user: bool = True,
350351
source_event: AstrMessageEvent | None = None,
351-
session_id: str | None = None,
352+
conversation_id: str | None = None,
352353
metadata: dict[str, JsonValue] | None = None,
354+
session_id: str | None = None,
353355
) -> AgentRun:
354356
"""Submit a configured runtime turn without waiting for model execution.
355357
@@ -361,22 +363,35 @@ async def run_turn(
361363
through output.ready events instead. Explicit plugin sends are
362364
unaffected. History is saved regardless of this setting.
363365
source_event: Caller identity; defaults to the active same-UMO event.
364-
session_id: Bind an exact conversation, or select the current one.
366+
conversation_id: ID of the existing conversation to run in. Omit it
367+
to use the current conversation for `umo`.
365368
metadata: JSON-compatible plugin data, deep-copied into the wakeup
366369
event's "agent_metadata" extra before admission hooks run.
367370
Defaults to an empty dict; source-event metadata is not inherited.
368371
This data does not grant permissions or automatically enter the
369372
model prompt or conversation history.
373+
session_id: Deprecated alias for `conversation_id`. Use
374+
`conversation_id` in new code.
370375
371376
Returns:
372377
An SDK view of the runtime turn with independent output observation.
373378
374379
Raises:
375380
RuntimeError: If the service or selected runtime is unavailable.
376-
ValueError: If input or the explicit session target is invalid.
381+
ValueError: If input or the conversation target is invalid, or if
382+
`conversation_id` and the deprecated `session_id` differ.
377383
"""
378384
if self._closed:
379385
raise RuntimeError("Agent service is unavailable")
386+
if session_id is not None:
387+
if conversation_id is not None and conversation_id != session_id:
388+
raise ValueError("conversation_id and deprecated session_id must match")
389+
warnings.warn(
390+
"session_id is deprecated. Use conversation_id instead.",
391+
DeprecationWarning,
392+
stacklevel=2,
393+
)
394+
conversation_id = session_id
380395
if source_event is None:
381396
current = current_message_event.get()
382397
if (
@@ -393,8 +408,8 @@ async def run_turn(
393408
)
394409
if (
395410
target is not None
396-
and session_id is not None
397-
and target.session_id == session_id
411+
and conversation_id is not None
412+
and target.session_id == conversation_id
398413
):
399414
runtime_name = target.runtime
400415
else:
@@ -403,7 +418,7 @@ async def run_turn(
403418
raise RuntimeError(f"Agent runtime unavailable: {runtime_name}")
404419
options = (
405420
{"config": deepcopy(config.get("config", {}))}
406-
if session_id is None
421+
if conversation_id is None
407422
else None
408423
)
409424
content = tuple(input_message)
@@ -413,7 +428,7 @@ async def run_turn(
413428
)
414429
run = AgentRun(self, event, send_to_user, runtime_name, options, content)
415430
self._runs.add(run)
416-
run._task = asyncio.create_task(self._execute(run, session_id))
431+
run._task = asyncio.create_task(self._execute(run, conversation_id))
417432

418433
def completed(task: asyncio.Task[TurnResult]) -> None:
419434
"""Release ownership and settle submissions cancelled before startup.

‎docs/en/dev/star/guides/ai/agent.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ result = await run.result()
1818

1919
`run_turn()` submits a turn without waiting for completion. Turns in the same session execute serially. `await run.result()` waits for execution, history persistence, and output handling. AstrBot sends replies by default; do not send `result.text` again.
2020

21+
Pass `conversation_id=cid` to target an existing conversation that belongs to `umo`. Omitting it uses the current conversation.
22+
2123
Use `await run.cancel()` to stop execution. Cancelling a result waiter does not stop the turn.
2224

2325
### Pass metadata to hooks
@@ -46,7 +48,7 @@ companion = metadata.get("private_companion", {})
4648

4749
Use a plugin namespace and JSON-compatible values. Metadata is copied into `event.get_extra("agent_metadata")` before hooks run. It is not added to the model prompt or history; add model context explicitly through a hook if needed.
4850

49-
Metadata defaults to `{}` and is not inherited from `source_event` or by `follow_up()`. To pass metadata to another turn, call `run_turn()` with the target `session_id`.
51+
Metadata defaults to `{}` and is not inherited from `source_event` or by `follow_up()`. To pass metadata to another turn, call `run_turn()` with the target `conversation_id`.
5052

5153
### Collect messages before sending
5254

‎docs/zh/dev/star/guides/ai/agent.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ result = await run.result()
1818

1919
`run_turn()` 提交后即返回,不等待执行完成。同一 session 的 turn 串行执行。`await run.result()` 等待执行、历史保存和输出处理完成。AstrBot 默认发送回复,插件无需再次发送 `result.text`。
2020

21+
用 `conversation_id=cid` 指定一段属于该 `umo` 的已有对话。省略时使用当前对话。
22+
2123
停止执行请使用 `await run.cancel()`。取消结果等待者不会停止 turn。
2224

2325
### 向 hooks 传递 metadata
@@ -46,7 +48,7 @@ companion = metadata.get("private_companion", {})
4648

4749
请使用插件自己的命名空间和 JSON 兼容值。Metadata 会在 hooks 执行前复制到 `event.get_extra("agent_metadata")`,不会自动加入模型提示词或历史;需要传给模型的上下文应通过 hook 显式添加。
4850

49-
Metadata 默认为 `{}`,不会从 `source_event` 或 `follow_up()` 继承。后续 turn 需要 metadata 时,请指定目标 `session_id` 调用 `run_turn()`。
51+
Metadata 默认为 `{}`,不会从 `source_event` 或 `follow_up()` 继承。后续 turn 需要 metadata 时,请指定目标 `conversation_id` 调用 `run_turn()`。
5052

5153
### 收集待发送消息
5254

0 commit comments

Comments
 (0)