deep-agent-cli 0.1.5__tar.gz → 0.1.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/PKG-INFO +15 -4
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/README.md +14 -3
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/app.py +73 -23
- deep_agent_cli-0.1.6/agent/control.py +316 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/llm.py +56 -2
- deep_agent_cli-0.1.6/agent/llm_stream.py +301 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/runner.py +80 -17
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/server/runtime.py +36 -10
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/pyproject.toml +1 -1
- deep_agent_cli-0.1.5/agent/control.py +0 -181
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/.gitignore +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/LICENSE +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/__init__.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/attachments.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/bootstrap.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cancel.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/__init__.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/clipboard.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/commands.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/gitinfo.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/input.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/interactions.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/main.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/pasted_content.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/previews.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/rendering.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/session_controller.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/skills.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/state.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/working_messages.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/cli/working_messages.yaml +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/config.example.yaml +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/config.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/factory.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/file_mutation.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/__init__.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/attachments.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/cancel_tools.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/pause.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/recovery.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/runtime_config.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/steering.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/tool_arg_hints.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/usage.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/workspace_filesystem.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/middleware/write_operation.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/network.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/permission.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/sandbox.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/sandbox_pool.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/sandbox_worker.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/server/__init__.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/server/app.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/server/config.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/server/main.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/server/store.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/session.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/session_lock.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/session_runtime.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/stream.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/tools/__init__.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/tools/examples.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/tools/execute.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/tools/human_input.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/tools/human_interaction.py +0 -0
- {deep_agent_cli-0.1.5 → deep_agent_cli-0.1.6}/agent/tools/web_search.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: deep-agent-cli
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.6
|
|
4
4
|
Summary: A sandboxed coding agent CLI for Linux and WSL2
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -33,7 +33,7 @@ Description-Content-Type: text/markdown
|
|
|
33
33
|
|
|
34
34
|
`deep-agent` 是一个在终端中运行的 Coding Agent,支持 Linux 和 WSL2。它把启动时所在的目录作为工作区,调用你配置的 OpenAI 兼容模型服务,并在 [Bubblewrap](https://github.com/containers/bubblewrap) 沙箱中执行命令。Agent 运行时基于 [Deep Agents](https://github.com/langchain-ai/deepagents) 和 LangGraph 构建。
|
|
35
35
|
|
|
36
|
-
当前版本 0.1.
|
|
36
|
+
当前版本 0.1.6,处于早期开发阶段。第一个稳定版之前,配置格式、会话存储格式和 Python API 都可能出现不兼容变更。
|
|
37
37
|
|
|
38
38
|
## 功能
|
|
39
39
|
|
|
@@ -43,7 +43,7 @@ Description-Content-Type: text/markdown
|
|
|
43
43
|
- **会话**:按工作区保存在本地 SQLite 中,可以随时恢复。
|
|
44
44
|
- **多模型**:在配置文件中定义多个模型,运行中切换模型和推理强度。支持 Chat Completions 和 Responses 两种接口。
|
|
45
45
|
- **流式输出**:思考内容和回答都流式显示;思考区默认保留按终端宽度折行后的末尾 5 行正文,`Ctrl+T` 展开。
|
|
46
|
-
-
|
|
46
|
+
- **运行中干预**:任务执行时可以追加指令、排队后续任务或取消。`Esc` 可主动取消内置模型的流式 HTTP 请求,即使 SSE 长时间没有新数据;HTTP 服务支持异步取消与明确的运行终态。
|
|
47
47
|
- **图片输入**:向声明支持图片的模型发送图片。
|
|
48
48
|
- **结构化提问**:Agent 需要你做决定时,会弹出单选、多选、是/否或文本输入。
|
|
49
49
|
- **上下文压缩**:接近模型上下文窗口时自动摘要旧对话,也可以手动压缩。
|
|
@@ -66,6 +66,8 @@ Debian / Ubuntu:
|
|
|
66
66
|
sudo apt install pipx bubblewrap
|
|
67
67
|
pipx ensurepath # 之后重新打开终端
|
|
68
68
|
pipx install deep-agent-cli
|
|
69
|
+
# 已安装旧版本时升级:
|
|
70
|
+
pipx upgrade deep-agent-cli
|
|
69
71
|
deep-agent --version
|
|
70
72
|
```
|
|
71
73
|
|
|
@@ -134,6 +136,15 @@ deep-agent resume 01a08aae # 按 id 或前缀恢复
|
|
|
134
136
|
|
|
135
137
|
恢复只加载会话状态,不会自动继续执行未完成的任务。
|
|
136
138
|
|
|
139
|
+
## 取消与会话恢复
|
|
140
|
+
|
|
141
|
+
- **CLI**:正在执行时按 `Esc` 请求取消当前轮次。内置的 OpenAI 兼容模型会主动取消流式请求并等待网络连接关闭,而不是只停止界面输出;`execute` 等工具仍按各自的取消机制收尾。
|
|
142
|
+
- **HTTP 服务**:`POST /v1/runs/{run_id}/cancel` 接受请求时返回 HTTP 202。它表示取消已受理,不表示任务已结束;应通过 Run 状态和 SSE `run_finished` 事件确认结果。若最终回答已完成并持久化,迟到的取消不会把 `completed` 覆盖成 `cancelled`。
|
|
143
|
+
- **恢复**:中断后的 Session 可以继续使用;意外退出且未完成收尾的运行以 `PENDING` 状态提示恢复检查。取消不会回滚已经写入文件或执行的工具副作用,继续前请核对工作区状态。
|
|
144
|
+
- **适用范围**:主动取消模型网络读取针对内置的流式 OpenAI 兼容模型。非流式调用和自定义 `BaseChatModel` 不保证能被强制取消;无法确认清理完成时会报告错误,不会直接释放正在使用的 Session Lock。
|
|
145
|
+
|
|
146
|
+
详细接口和状态语义见 [HTTP 服务](docs/server.md);本次变更见 [PR #3](https://github.com/ADaozz/deep_agent_cli/pull/3)。
|
|
147
|
+
|
|
137
148
|
## 常用操作
|
|
138
149
|
|
|
139
150
|
| 命令 | 作用 |
|
|
@@ -334,7 +345,7 @@ Xvfb 示例仅适用于宿主没有现成显示服务的情况;CLI 不会自
|
|
|
334
345
|
- [沙箱与权限](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/sandbox.md):隔离范围、网络、日志、Ubuntu / WSL2 排查
|
|
335
346
|
- [终端界面](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/tui.md):命令、快捷键、图片、会话、显示规则
|
|
336
347
|
- [作为 Python 库使用](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/library.md):`AgentRunner`、事件、自定义工具
|
|
337
|
-
- [HTTP
|
|
348
|
+
- [HTTP 服务与前端接入](docs/server.md):部署配置、请求/响应、业务状态、审批表单、SSE 续接、恢复及 Python / TypeScript 示例
|
|
338
349
|
- [架构](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/architecture.md):分层、中间件、会话存储、上下文压缩
|
|
339
350
|
- [开发](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/development.md):测试、冒烟脚本、CI 与发布
|
|
340
351
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
`deep-agent` 是一个在终端中运行的 Coding Agent,支持 Linux 和 WSL2。它把启动时所在的目录作为工作区,调用你配置的 OpenAI 兼容模型服务,并在 [Bubblewrap](https://github.com/containers/bubblewrap) 沙箱中执行命令。Agent 运行时基于 [Deep Agents](https://github.com/langchain-ai/deepagents) 和 LangGraph 构建。
|
|
4
4
|
|
|
5
|
-
当前版本 0.1.
|
|
5
|
+
当前版本 0.1.6,处于早期开发阶段。第一个稳定版之前,配置格式、会话存储格式和 Python API 都可能出现不兼容变更。
|
|
6
6
|
|
|
7
7
|
## 功能
|
|
8
8
|
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
- **会话**:按工作区保存在本地 SQLite 中,可以随时恢复。
|
|
13
13
|
- **多模型**:在配置文件中定义多个模型,运行中切换模型和推理强度。支持 Chat Completions 和 Responses 两种接口。
|
|
14
14
|
- **流式输出**:思考内容和回答都流式显示;思考区默认保留按终端宽度折行后的末尾 5 行正文,`Ctrl+T` 展开。
|
|
15
|
-
-
|
|
15
|
+
- **运行中干预**:任务执行时可以追加指令、排队后续任务或取消。`Esc` 可主动取消内置模型的流式 HTTP 请求,即使 SSE 长时间没有新数据;HTTP 服务支持异步取消与明确的运行终态。
|
|
16
16
|
- **图片输入**:向声明支持图片的模型发送图片。
|
|
17
17
|
- **结构化提问**:Agent 需要你做决定时,会弹出单选、多选、是/否或文本输入。
|
|
18
18
|
- **上下文压缩**:接近模型上下文窗口时自动摘要旧对话,也可以手动压缩。
|
|
@@ -35,6 +35,8 @@ Debian / Ubuntu:
|
|
|
35
35
|
sudo apt install pipx bubblewrap
|
|
36
36
|
pipx ensurepath # 之后重新打开终端
|
|
37
37
|
pipx install deep-agent-cli
|
|
38
|
+
# 已安装旧版本时升级:
|
|
39
|
+
pipx upgrade deep-agent-cli
|
|
38
40
|
deep-agent --version
|
|
39
41
|
```
|
|
40
42
|
|
|
@@ -103,6 +105,15 @@ deep-agent resume 01a08aae # 按 id 或前缀恢复
|
|
|
103
105
|
|
|
104
106
|
恢复只加载会话状态,不会自动继续执行未完成的任务。
|
|
105
107
|
|
|
108
|
+
## 取消与会话恢复
|
|
109
|
+
|
|
110
|
+
- **CLI**:正在执行时按 `Esc` 请求取消当前轮次。内置的 OpenAI 兼容模型会主动取消流式请求并等待网络连接关闭,而不是只停止界面输出;`execute` 等工具仍按各自的取消机制收尾。
|
|
111
|
+
- **HTTP 服务**:`POST /v1/runs/{run_id}/cancel` 接受请求时返回 HTTP 202。它表示取消已受理,不表示任务已结束;应通过 Run 状态和 SSE `run_finished` 事件确认结果。若最终回答已完成并持久化,迟到的取消不会把 `completed` 覆盖成 `cancelled`。
|
|
112
|
+
- **恢复**:中断后的 Session 可以继续使用;意外退出且未完成收尾的运行以 `PENDING` 状态提示恢复检查。取消不会回滚已经写入文件或执行的工具副作用,继续前请核对工作区状态。
|
|
113
|
+
- **适用范围**:主动取消模型网络读取针对内置的流式 OpenAI 兼容模型。非流式调用和自定义 `BaseChatModel` 不保证能被强制取消;无法确认清理完成时会报告错误,不会直接释放正在使用的 Session Lock。
|
|
114
|
+
|
|
115
|
+
详细接口和状态语义见 [HTTP 服务](docs/server.md);本次变更见 [PR #3](https://github.com/ADaozz/deep_agent_cli/pull/3)。
|
|
116
|
+
|
|
106
117
|
## 常用操作
|
|
107
118
|
|
|
108
119
|
| 命令 | 作用 |
|
|
@@ -303,7 +314,7 @@ Xvfb 示例仅适用于宿主没有现成显示服务的情况;CLI 不会自
|
|
|
303
314
|
- [沙箱与权限](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/sandbox.md):隔离范围、网络、日志、Ubuntu / WSL2 排查
|
|
304
315
|
- [终端界面](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/tui.md):命令、快捷键、图片、会话、显示规则
|
|
305
316
|
- [作为 Python 库使用](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/library.md):`AgentRunner`、事件、自定义工具
|
|
306
|
-
- [HTTP
|
|
317
|
+
- [HTTP 服务与前端接入](docs/server.md):部署配置、请求/响应、业务状态、审批表单、SSE 续接、恢复及 Python / TypeScript 示例
|
|
307
318
|
- [架构](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/architecture.md):分层、中间件、会话存储、上下文压缩
|
|
308
319
|
- [开发](https://github.com/ADaozz/deep_agent_cli/blob/main/docs/development.md):测试、冒烟脚本、CI 与发布
|
|
309
320
|
|
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import asyncio
|
|
4
4
|
from concurrent.futures import ThreadPoolExecutor
|
|
5
5
|
from contextlib import contextmanager
|
|
6
|
+
from contextvars import ContextVar, copy_context
|
|
6
7
|
from functools import partial
|
|
7
8
|
import logging
|
|
8
9
|
import os
|
|
@@ -84,6 +85,20 @@ INPUT_CLEARED_HINT = "Input cleared · press Ctrl+C again to exit"
|
|
|
84
85
|
CTRL_C_EXIT_WINDOW_SECONDS = 1.0
|
|
85
86
|
STATUS_NOTICE_SECONDS = 1.0
|
|
86
87
|
|
|
88
|
+
_RUN_EPOCH: ContextVar[int | None] = ContextVar("deep_agent_tui_run_epoch", default=None)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _call_in_run_epoch(epoch: int, func: Callable[..., Any], /, *args: Any, **kwargs: Any) -> Any:
|
|
92
|
+
"""Run a Runner operation in a context tagged with its run epoch.
|
|
93
|
+
|
|
94
|
+
LangGraph copies the context into node and tool threads, so every event
|
|
95
|
+
the operation emits carries the epoch back to the UI loop.
|
|
96
|
+
"""
|
|
97
|
+
def call() -> Any:
|
|
98
|
+
_RUN_EPOCH.set(epoch)
|
|
99
|
+
return func(*args, **kwargs)
|
|
100
|
+
return copy_context().run(call)
|
|
101
|
+
|
|
87
102
|
|
|
88
103
|
def format_context_window(window: int) -> str:
|
|
89
104
|
"""1000000 -> "1.0m", 200000 -> "200k"."""
|
|
@@ -366,6 +381,10 @@ class CliApplication:
|
|
|
366
381
|
self._io_executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="deep-agent-io")
|
|
367
382
|
self._loop: asyncio.AbstractEventLoop | None = None
|
|
368
383
|
self._run_task: asyncio.Task[None] | None = None
|
|
384
|
+
# Each dispatched run gets an epoch; its Runner events carry it so a
|
|
385
|
+
# late event of an earlier run cannot change the current run's UI.
|
|
386
|
+
self._run_epoch = 0
|
|
387
|
+
self._cancel_sent = False
|
|
369
388
|
self._compacting = False
|
|
370
389
|
self._last_ctrl_c: float | None = None
|
|
371
390
|
self._status_notice_handle: asyncio.TimerHandle | None = None
|
|
@@ -1761,6 +1780,11 @@ class CliApplication:
|
|
|
1761
1780
|
self.set_status("Wait for compaction to finish")
|
|
1762
1781
|
elif self.state.running:
|
|
1763
1782
|
restored = self._restore_queued_to_editor(event.current_buffer)
|
|
1783
|
+
if self._cancel_sent:
|
|
1784
|
+
self.set_status("Cancelling… waiting for the run to stop", transient=False)
|
|
1785
|
+
self.application.invalidate()
|
|
1786
|
+
return
|
|
1787
|
+
self._cancel_sent = True
|
|
1764
1788
|
self.runner.request_cancel()
|
|
1765
1789
|
if restored:
|
|
1766
1790
|
self.set_status("Cancelling… · queued messages restored", transient=False)
|
|
@@ -1936,29 +1960,41 @@ class CliApplication:
|
|
|
1936
1960
|
self.state.running = True
|
|
1937
1961
|
self._working_messages.reset()
|
|
1938
1962
|
self.set_status("Working… Esc to cancel", transient=False)
|
|
1963
|
+
self._run_epoch += 1
|
|
1964
|
+
epoch = self._run_epoch
|
|
1965
|
+
self._cancel_sent = False
|
|
1966
|
+
# Esc may arrive before the worker reaches begin_run; the reservation
|
|
1967
|
+
# keeps that cancel instead of letting begin_run clear it.
|
|
1968
|
+
reservation = self.runner.reserve_run()
|
|
1939
1969
|
|
|
1940
1970
|
async def work() -> None:
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
self.
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1971
|
+
try:
|
|
1972
|
+
if resume_call is None:
|
|
1973
|
+
try:
|
|
1974
|
+
result = await self._run_blocking(
|
|
1975
|
+
_call_in_run_epoch, epoch,
|
|
1976
|
+
self.runner.invoke_with_attachment_refs,
|
|
1977
|
+
text,
|
|
1978
|
+
image_refs=image_refs,
|
|
1979
|
+
on_event=self._on_event_thread,
|
|
1980
|
+
)
|
|
1981
|
+
except Exception as exc: # noqa: BLE001
|
|
1982
|
+
self.state.attachments[:] = image_refs
|
|
1983
|
+
self._apply_event(RunEvent(type="run_failed", content=str(exc)))
|
|
1984
|
+
return
|
|
1985
|
+
if result.status == "failed" and image_refs:
|
|
1986
|
+
self.state.attachments[:] = image_refs
|
|
1987
|
+
else:
|
|
1988
|
+
try:
|
|
1989
|
+
result = await self._run_blocking(_call_in_run_epoch, epoch, resume_call)
|
|
1990
|
+
except Exception as exc: # noqa: BLE001
|
|
1991
|
+
self._apply_event(RunEvent(type="run_failed", content=str(exc)))
|
|
1992
|
+
return
|
|
1993
|
+
self._handle_result(result)
|
|
1994
|
+
finally:
|
|
1995
|
+
self.runner.release_run_reservation(reservation)
|
|
1996
|
+
if epoch == self._run_epoch:
|
|
1997
|
+
self._cancel_sent = False
|
|
1962
1998
|
|
|
1963
1999
|
self._run_task = asyncio.create_task(work())
|
|
1964
2000
|
|
|
@@ -2082,10 +2118,17 @@ class CliApplication:
|
|
|
2082
2118
|
temp.unlink(missing_ok=True)
|
|
2083
2119
|
|
|
2084
2120
|
def _on_event_thread(self, event: RunEvent) -> None:
|
|
2121
|
+
epoch = _RUN_EPOCH.get()
|
|
2085
2122
|
if self._loop is not None:
|
|
2086
|
-
self._loop.call_soon_threadsafe(self.
|
|
2123
|
+
self._loop.call_soon_threadsafe(self._apply_run_event, event, epoch)
|
|
2087
2124
|
else:
|
|
2088
|
-
self.
|
|
2125
|
+
self._apply_run_event(event, epoch)
|
|
2126
|
+
|
|
2127
|
+
def _apply_run_event(self, event: RunEvent, epoch: int | None) -> None:
|
|
2128
|
+
# Events without an epoch come from the UI thread (steering, control).
|
|
2129
|
+
if epoch is not None and epoch != self._run_epoch:
|
|
2130
|
+
return
|
|
2131
|
+
self._apply_event(event)
|
|
2089
2132
|
|
|
2090
2133
|
def _apply_event(self, event: RunEvent, *, announce: bool = True) -> None:
|
|
2091
2134
|
previous_status = self.state.status
|
|
@@ -2120,6 +2163,13 @@ class CliApplication:
|
|
|
2120
2163
|
self.application.invalidate()
|
|
2121
2164
|
|
|
2122
2165
|
def _handle_result(self, result: RunResult) -> None:
|
|
2166
|
+
if result.status == "cancelled":
|
|
2167
|
+
self.state.running = False
|
|
2168
|
+
if result.error:
|
|
2169
|
+
self.state.add_system(result.error, error=True)
|
|
2170
|
+
self.set_status("Cancel cleanup not confirmed", transient=False)
|
|
2171
|
+
elif self.state.status != "Cancelled":
|
|
2172
|
+
self.set_status("Cancelled", transient=False)
|
|
2123
2173
|
if result.status == "paused":
|
|
2124
2174
|
self.state.running = False
|
|
2125
2175
|
self.state.add_system("Paused at a checkpoint. Submit /pause again is unnecessary; press Enter to resume.")
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"""Run-scoped control surface shared by the TUI, graph worker, and tool threads."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from collections import deque
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from itertools import count
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
from typing import Any, Callable
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
from langgraph.runtime import RunControl
|
|
13
|
+
|
|
14
|
+
from agent.cancel import ToolCancelContext, clear_cancel_context, set_cancel_context
|
|
15
|
+
|
|
16
|
+
EventEmitter = Callable[[str, dict[str, Any]], None]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class QueuedMessage:
|
|
21
|
+
text: str
|
|
22
|
+
mode: str # "steer" | "followUp"
|
|
23
|
+
order: int
|
|
24
|
+
id: str = field(default_factory=lambda: str(uuid4()))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(eq=False)
|
|
28
|
+
class _RunScope:
|
|
29
|
+
"""Cancel state of one run generation.
|
|
30
|
+
|
|
31
|
+
A scope is created either by ``reserve_run`` (the caller has dispatched an
|
|
32
|
+
operation that has not reached ``begin_run`` yet) or by ``begin_run``.
|
|
33
|
+
Cancelling a scope never affects another generation.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
generation: int
|
|
37
|
+
cancel_event: threading.Event = field(default_factory=threading.Event)
|
|
38
|
+
run_control: RunControl | None = None
|
|
39
|
+
model_streams: set[Any] = field(default_factory=set)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class RunController:
|
|
43
|
+
"""Thread-safe steering / follow-up queues, cancel state, and active tool registry."""
|
|
44
|
+
|
|
45
|
+
def __init__(self, *, on_control_event: EventEmitter | None = None) -> None:
|
|
46
|
+
self._lock = threading.RLock()
|
|
47
|
+
self._steering: deque[QueuedMessage] = deque()
|
|
48
|
+
self._follow_ups: deque[QueuedMessage] = deque()
|
|
49
|
+
self._queue_order = count()
|
|
50
|
+
self._pause_requested = False
|
|
51
|
+
self._generations = count(1)
|
|
52
|
+
self._active: _RunScope | None = None
|
|
53
|
+
self._reserved: _RunScope | None = None
|
|
54
|
+
self._last: _RunScope | None = None
|
|
55
|
+
self._stream_scopes: dict[Any, _RunScope] = {}
|
|
56
|
+
self._unclosed_streams: set[Any] = set()
|
|
57
|
+
self._active_tools: dict[str, ToolCancelContext] = {}
|
|
58
|
+
self._defer_steering = False
|
|
59
|
+
self._on_control_event = on_control_event
|
|
60
|
+
self._pending_config_check: Callable[[], bool] = lambda: False
|
|
61
|
+
|
|
62
|
+
def set_pending_config_check(self, check: Callable[[], bool]) -> None:
|
|
63
|
+
with self._lock:
|
|
64
|
+
self._pending_config_check = check
|
|
65
|
+
|
|
66
|
+
def has_pending_config(self) -> bool:
|
|
67
|
+
with self._lock:
|
|
68
|
+
check = self._pending_config_check
|
|
69
|
+
return check()
|
|
70
|
+
|
|
71
|
+
def reserve_run(self) -> int:
|
|
72
|
+
"""Reserve the generation of a run the caller is about to dispatch.
|
|
73
|
+
|
|
74
|
+
A cancel issued before that run reaches ``begin_run`` is kept on the
|
|
75
|
+
reservation instead of being cleared when the run starts.
|
|
76
|
+
"""
|
|
77
|
+
with self._lock:
|
|
78
|
+
if self._reserved is None:
|
|
79
|
+
self._reserved = _RunScope(next(self._generations))
|
|
80
|
+
return self._reserved.generation
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def reserved_generation(self) -> int | None:
|
|
84
|
+
with self._lock:
|
|
85
|
+
return self._reserved.generation if self._reserved is not None else None
|
|
86
|
+
|
|
87
|
+
def release_reservation(self, generation: int) -> None:
|
|
88
|
+
"""Drop the reservation of ``generation`` if no run has consumed it.
|
|
89
|
+
|
|
90
|
+
A reservation made later for another run has a different generation
|
|
91
|
+
and is left alone.
|
|
92
|
+
"""
|
|
93
|
+
with self._lock:
|
|
94
|
+
if self._reserved is not None and self._reserved.generation == generation:
|
|
95
|
+
self._reserved = None
|
|
96
|
+
|
|
97
|
+
def begin_run(self) -> RunControl:
|
|
98
|
+
with self._lock:
|
|
99
|
+
previous = self._active
|
|
100
|
+
scope = self._reserved or _RunScope(next(self._generations))
|
|
101
|
+
self._reserved = None
|
|
102
|
+
if previous is not None:
|
|
103
|
+
# begin_run without end_run continues the same operation
|
|
104
|
+
# (follow-up turns), so an accepted cancel carries over.
|
|
105
|
+
if previous.cancel_event.is_set():
|
|
106
|
+
scope.cancel_event.set()
|
|
107
|
+
self._retire_streams(previous)
|
|
108
|
+
scope.run_control = RunControl()
|
|
109
|
+
self._active = scope
|
|
110
|
+
self._last = scope
|
|
111
|
+
self._active_tools.clear()
|
|
112
|
+
cancelled = scope.cancel_event.is_set()
|
|
113
|
+
control = scope.run_control
|
|
114
|
+
if cancelled:
|
|
115
|
+
control.request_drain("cancelled")
|
|
116
|
+
return control
|
|
117
|
+
|
|
118
|
+
def end_run(self) -> None:
|
|
119
|
+
with self._lock:
|
|
120
|
+
scope = self._active
|
|
121
|
+
self._active = None
|
|
122
|
+
self._active_tools.clear()
|
|
123
|
+
if scope is not None:
|
|
124
|
+
self._retire_streams(scope)
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def run_control(self) -> RunControl | None:
|
|
128
|
+
with self._lock:
|
|
129
|
+
return self._active.run_control if self._active is not None else None
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def run_generation(self) -> int | None:
|
|
133
|
+
with self._lock:
|
|
134
|
+
return self._active.generation if self._active is not None else None
|
|
135
|
+
|
|
136
|
+
def _current_scope(self) -> _RunScope | None:
|
|
137
|
+
return self._active or self._reserved or self._last
|
|
138
|
+
|
|
139
|
+
@property
|
|
140
|
+
def cancel_requested(self) -> bool:
|
|
141
|
+
with self._lock:
|
|
142
|
+
scope = self._current_scope()
|
|
143
|
+
return scope is not None and scope.cancel_event.is_set()
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def pause_requested(self) -> bool:
|
|
147
|
+
with self._lock:
|
|
148
|
+
return self._pause_requested
|
|
149
|
+
|
|
150
|
+
def request_pause(self) -> None:
|
|
151
|
+
with self._lock:
|
|
152
|
+
self._pause_requested = True
|
|
153
|
+
|
|
154
|
+
def clear_pause(self) -> None:
|
|
155
|
+
with self._lock:
|
|
156
|
+
self._pause_requested = False
|
|
157
|
+
|
|
158
|
+
def set_event_handler(self, handler: EventEmitter | None) -> None:
|
|
159
|
+
with self._lock:
|
|
160
|
+
self._on_control_event = handler
|
|
161
|
+
|
|
162
|
+
def set_defer_steering(self, value: bool) -> None:
|
|
163
|
+
with self._lock:
|
|
164
|
+
self._defer_steering = value
|
|
165
|
+
|
|
166
|
+
def steer(self, text: str) -> QueuedMessage:
|
|
167
|
+
with self._lock:
|
|
168
|
+
message = QueuedMessage(text=text, mode="steer", order=next(self._queue_order))
|
|
169
|
+
self._steering.append(message)
|
|
170
|
+
self._emit("steering_queued", {"content": text, "mode": "steer", "id": message.id})
|
|
171
|
+
return message
|
|
172
|
+
|
|
173
|
+
def follow_up(self, text: str) -> QueuedMessage:
|
|
174
|
+
with self._lock:
|
|
175
|
+
message = QueuedMessage(text=text, mode="followUp", order=next(self._queue_order))
|
|
176
|
+
self._follow_ups.append(message)
|
|
177
|
+
self._emit("steering_queued", {"content": text, "mode": "followUp", "id": message.id})
|
|
178
|
+
return message
|
|
179
|
+
|
|
180
|
+
def pop_steering(self) -> str | None:
|
|
181
|
+
"""Consume one steering message at a model-safe boundary (pi one-at-a-time)."""
|
|
182
|
+
with self._lock:
|
|
183
|
+
if self._defer_steering or self.cancel_requested:
|
|
184
|
+
return None
|
|
185
|
+
if not self._steering:
|
|
186
|
+
return None
|
|
187
|
+
message = self._steering.popleft()
|
|
188
|
+
self._emit("steering_applied", {"content": message.text, "id": message.id})
|
|
189
|
+
return message.text
|
|
190
|
+
|
|
191
|
+
def pending_steering_count(self) -> int:
|
|
192
|
+
with self._lock:
|
|
193
|
+
return len(self._steering)
|
|
194
|
+
|
|
195
|
+
def pending_follow_up_count(self) -> int:
|
|
196
|
+
with self._lock:
|
|
197
|
+
return len(self._follow_ups)
|
|
198
|
+
|
|
199
|
+
def pop_follow_up(self) -> str | None:
|
|
200
|
+
with self._lock:
|
|
201
|
+
if not self._follow_ups:
|
|
202
|
+
return None
|
|
203
|
+
return self._follow_ups.popleft().text
|
|
204
|
+
|
|
205
|
+
def take_unapplied(self) -> list[QueuedMessage]:
|
|
206
|
+
"""Alt+Up: reclaim steering/follow-up that has not entered a checkpoint yet."""
|
|
207
|
+
with self._lock:
|
|
208
|
+
messages = sorted((*self._steering, *self._follow_ups), key=lambda item: item.order)
|
|
209
|
+
self._steering.clear()
|
|
210
|
+
self._follow_ups.clear()
|
|
211
|
+
return messages
|
|
212
|
+
|
|
213
|
+
def cancel(self) -> None:
|
|
214
|
+
"""Cancel the reserved (or else active) run; repeated calls are no-ops.
|
|
215
|
+
|
|
216
|
+
State changes happen under the lock; draining the graph, cancelling
|
|
217
|
+
model streams and tools, and emitting events happen after it.
|
|
218
|
+
"""
|
|
219
|
+
with self._lock:
|
|
220
|
+
# begin_run consumes any reservation, so one that is pending while
|
|
221
|
+
# a run is active was made for the next dispatched run (the active
|
|
222
|
+
# one is only finishing its cleanup); the cancel is meant for it.
|
|
223
|
+
scope = self._reserved or self._active
|
|
224
|
+
if scope is None:
|
|
225
|
+
# Idle: record the request without touching any future run.
|
|
226
|
+
scope = self._last
|
|
227
|
+
if scope is None:
|
|
228
|
+
scope = self._last = _RunScope(next(self._generations))
|
|
229
|
+
self._pause_requested = False
|
|
230
|
+
if scope.cancel_event.is_set():
|
|
231
|
+
return
|
|
232
|
+
scope.cancel_event.set()
|
|
233
|
+
control = scope.run_control
|
|
234
|
+
streams = list(scope.model_streams)
|
|
235
|
+
tools = list(self._active_tools.values())
|
|
236
|
+
for stream in streams:
|
|
237
|
+
stream.cancel()
|
|
238
|
+
if control is not None:
|
|
239
|
+
control.request_drain("cancelled")
|
|
240
|
+
for ctx in tools:
|
|
241
|
+
ctx.request_cancel()
|
|
242
|
+
self._emit("run_cancelling", {})
|
|
243
|
+
|
|
244
|
+
def register_model_stream(self, stream: Any) -> bool:
|
|
245
|
+
"""Attach a model stream to the active run; False when that run is cancelled or absent."""
|
|
246
|
+
with self._lock:
|
|
247
|
+
scope = self._active
|
|
248
|
+
if scope is None or scope.cancel_event.is_set():
|
|
249
|
+
return False
|
|
250
|
+
scope.model_streams.add(stream)
|
|
251
|
+
self._stream_scopes[stream] = scope
|
|
252
|
+
return True
|
|
253
|
+
|
|
254
|
+
def unregister_model_stream(self, stream: Any) -> None:
|
|
255
|
+
with self._lock:
|
|
256
|
+
scope = self._stream_scopes.pop(stream, None)
|
|
257
|
+
if scope is not None:
|
|
258
|
+
scope.model_streams.discard(stream)
|
|
259
|
+
if not stream.closed:
|
|
260
|
+
self._unclosed_streams.add(stream)
|
|
261
|
+
|
|
262
|
+
def unclosed_model_streams(self) -> int:
|
|
263
|
+
"""Finished-or-cancelled model streams whose HTTP request has not closed yet."""
|
|
264
|
+
with self._lock:
|
|
265
|
+
self._unclosed_streams = {item for item in self._unclosed_streams if not item.closed}
|
|
266
|
+
return len(self._unclosed_streams)
|
|
267
|
+
|
|
268
|
+
def wait_model_streams_closed(self, timeout: float) -> bool:
|
|
269
|
+
deadline = time.monotonic() + timeout
|
|
270
|
+
with self._lock:
|
|
271
|
+
streams = list(self._unclosed_streams)
|
|
272
|
+
for stream in streams:
|
|
273
|
+
if not stream.wait_closed(max(0.0, deadline - time.monotonic())):
|
|
274
|
+
return False
|
|
275
|
+
return self.unclosed_model_streams() == 0
|
|
276
|
+
|
|
277
|
+
def _retire_streams(self, scope: _RunScope) -> None:
|
|
278
|
+
for stream in scope.model_streams:
|
|
279
|
+
self._stream_scopes.pop(stream, None)
|
|
280
|
+
if not stream.closed:
|
|
281
|
+
self._unclosed_streams.add(stream)
|
|
282
|
+
scope.model_streams.clear()
|
|
283
|
+
|
|
284
|
+
def register_tool(self, ctx: ToolCancelContext) -> None:
|
|
285
|
+
with self._lock:
|
|
286
|
+
self._active_tools[ctx.tool_call_id or ctx.token] = ctx
|
|
287
|
+
|
|
288
|
+
def unregister_tool(self, ctx: ToolCancelContext) -> None:
|
|
289
|
+
with self._lock:
|
|
290
|
+
self._active_tools.pop(ctx.tool_call_id or ctx.token, None)
|
|
291
|
+
|
|
292
|
+
def open_tool_context(self, *, tool_name: str, tool_call_id: str) -> ToolCancelContext:
|
|
293
|
+
with self._lock:
|
|
294
|
+
scope = self._current_scope()
|
|
295
|
+
event = scope.cancel_event if scope is not None else threading.Event()
|
|
296
|
+
ctx = ToolCancelContext(
|
|
297
|
+
tool_name=tool_name,
|
|
298
|
+
tool_call_id=tool_call_id,
|
|
299
|
+
cancel_event=event,
|
|
300
|
+
)
|
|
301
|
+
if self.cancel_requested:
|
|
302
|
+
ctx.request_cancel()
|
|
303
|
+
self.register_tool(ctx)
|
|
304
|
+
set_cancel_context(ctx)
|
|
305
|
+
return ctx
|
|
306
|
+
|
|
307
|
+
def close_tool_context(self, ctx: ToolCancelContext) -> None:
|
|
308
|
+
self.unregister_tool(ctx)
|
|
309
|
+
clear_cancel_context()
|
|
310
|
+
|
|
311
|
+
def _emit(self, event_type: str, payload: dict[str, Any]) -> None:
|
|
312
|
+
if self._on_control_event is not None:
|
|
313
|
+
try:
|
|
314
|
+
self._on_control_event(event_type, payload)
|
|
315
|
+
except Exception: # noqa: BLE001
|
|
316
|
+
pass
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
from __future__ import annotations
|
|
3
3
|
|
|
4
4
|
import base64
|
|
5
|
-
from typing import Any, ClassVar
|
|
5
|
+
from typing import Any, ClassVar, Iterator
|
|
6
6
|
|
|
7
|
+
from langchain_core.callbacks import CallbackManagerForLLMRun
|
|
7
8
|
from langchain_core.messages import AIMessage, AIMessageChunk, BaseMessage, HumanMessage
|
|
8
9
|
from langchain_core.messages.content import create_image_block, create_text_block
|
|
9
10
|
from langchain_core.outputs import ChatGenerationChunk, ChatResult
|
|
@@ -12,6 +13,12 @@ from pydantic import PrivateAttr, model_validator
|
|
|
12
13
|
|
|
13
14
|
from agent.attachments import ATTACHMENT_META_KEY, AttachmentStore, refs_from_message
|
|
14
15
|
from agent.config import ModelProfile
|
|
16
|
+
from agent.llm_stream import (
|
|
17
|
+
CancellableStreamBridge,
|
|
18
|
+
ModelStreamCancelled,
|
|
19
|
+
current_stream_owner,
|
|
20
|
+
in_stream_loop_thread,
|
|
21
|
+
)
|
|
15
22
|
|
|
16
23
|
|
|
17
24
|
_REASONING_EVENT_TYPES = {
|
|
@@ -211,9 +218,15 @@ class _RawResponsesClient(_Delegate):
|
|
|
211
218
|
|
|
212
219
|
|
|
213
220
|
class ProtocolChatOpenAI(ChatOpenAI):
|
|
214
|
-
"""Shared request-local attachment resolution and proxy-free transport.
|
|
221
|
+
"""Shared request-local attachment resolution and proxy-free transport.
|
|
222
|
+
|
|
223
|
+
Synchronous streaming runs LangChain's async stream on the managed stream
|
|
224
|
+
loop (``agent.llm_stream``) so the run that owns the request can cancel a
|
|
225
|
+
blocked read. Non-streaming ``_generate`` keeps the synchronous client.
|
|
226
|
+
"""
|
|
215
227
|
_attachment_store: AttachmentStore | None = PrivateAttr(default=None)
|
|
216
228
|
materializes_attachment_refs: ClassVar[bool] = True
|
|
229
|
+
supports_stream_cancellation: ClassVar[bool] = True
|
|
217
230
|
|
|
218
231
|
@model_validator(mode="before")
|
|
219
232
|
@classmethod
|
|
@@ -221,6 +234,10 @@ class ProtocolChatOpenAI(ChatOpenAI):
|
|
|
221
234
|
"""Keep direct construction safe from inherited WSL SOCKS settings too."""
|
|
222
235
|
if not isinstance(values, dict):
|
|
223
236
|
return values
|
|
237
|
+
if "stream_chunk_timeout" in cls.model_fields and "stream_chunk_timeout" not in values:
|
|
238
|
+
# The async stream would otherwise abort after 120s without a
|
|
239
|
+
# content chunk, which the synchronous stream never did.
|
|
240
|
+
values = {**values, "stream_chunk_timeout": None}
|
|
224
241
|
if values.get("http_client") is None or values.get("http_async_client") is None:
|
|
225
242
|
import httpx
|
|
226
243
|
|
|
@@ -235,6 +252,43 @@ class ProtocolChatOpenAI(ChatOpenAI):
|
|
|
235
252
|
def set_attachment_store(self, store: AttachmentStore) -> None:
|
|
236
253
|
self._attachment_store = store
|
|
237
254
|
|
|
255
|
+
def _stream(
|
|
256
|
+
self,
|
|
257
|
+
messages: list[BaseMessage],
|
|
258
|
+
stop: list[str] | None = None,
|
|
259
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
260
|
+
**kwargs: Any,
|
|
261
|
+
) -> Iterator[ChatGenerationChunk]:
|
|
262
|
+
if in_stream_loop_thread():
|
|
263
|
+
# Blocking the loop thread on its own queue would deadlock.
|
|
264
|
+
yield from super()._stream(messages, stop=stop, run_manager=run_manager, **kwargs)
|
|
265
|
+
return
|
|
266
|
+
astream = super()._astream
|
|
267
|
+
bridge: CancellableStreamBridge[ChatGenerationChunk] = CancellableStreamBridge(
|
|
268
|
+
lambda: astream(messages, stop=stop, run_manager=None, **kwargs),
|
|
269
|
+
)
|
|
270
|
+
owner = current_stream_owner()
|
|
271
|
+
if owner is not None and not owner.register_model_stream(bridge):
|
|
272
|
+
raise ModelStreamCancelled("The run was cancelled before the model request started")
|
|
273
|
+
responses_api = self._use_responses_api({**kwargs, **self.model_kwargs})
|
|
274
|
+
try:
|
|
275
|
+
for chunk in bridge.iter_chunks():
|
|
276
|
+
# LangChain's async stream reports tokens to an async manager;
|
|
277
|
+
# the sync handlers are called here, on the consuming thread,
|
|
278
|
+
# with the same arguments the sync stream passes.
|
|
279
|
+
if run_manager is not None:
|
|
280
|
+
if responses_api:
|
|
281
|
+
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
|
|
282
|
+
else:
|
|
283
|
+
run_manager.on_llm_new_token(
|
|
284
|
+
chunk.text, chunk=chunk,
|
|
285
|
+
logprobs=(chunk.generation_info or {}).get("logprobs"),
|
|
286
|
+
)
|
|
287
|
+
yield chunk
|
|
288
|
+
finally:
|
|
289
|
+
if owner is not None:
|
|
290
|
+
owner.unregister_model_stream(bridge)
|
|
291
|
+
|
|
238
292
|
def _get_request_payload(self, input_: Any, *, stop=None, **kwargs) -> dict[str, Any]:
|
|
239
293
|
materialized = materialize_attachment_refs(input_, self._attachment_store)
|
|
240
294
|
return super()._get_request_payload(materialized, stop=stop, **kwargs)
|