chump-server 0.0.1__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.
- chump_server-0.0.1/.gitignore +26 -0
- chump_server-0.0.1/PKG-INFO +60 -0
- chump_server-0.0.1/README.md +51 -0
- chump_server-0.0.1/chump_server/__init__.py +7 -0
- chump_server-0.0.1/chump_server/agent.py +500 -0
- chump_server-0.0.1/chump_server/config.py +145 -0
- chump_server-0.0.1/chump_server/main.py +150 -0
- chump_server-0.0.1/chump_server/patch_tool.py +276 -0
- chump_server-0.0.1/chump_server/safety.py +59 -0
- chump_server-0.0.1/chump_server/tools/__init__.py +3 -0
- chump_server-0.0.1/chump_server/tools/_mcp_exa.py +76 -0
- chump_server-0.0.1/chump_server/tools/_utils.py +151 -0
- chump_server-0.0.1/chump_server/tools/apply_patch.py +221 -0
- chump_server-0.0.1/chump_server/tools/bash.py +100 -0
- chump_server-0.0.1/chump_server/tools/builder.py +151 -0
- chump_server-0.0.1/chump_server/tools/read_file.py +63 -0
- chump_server-0.0.1/chump_server/tools/web_fetch.py +195 -0
- chump_server-0.0.1/chump_server/tools/website.py +88 -0
- chump_server-0.0.1/chump_server/tools/write_file.py +56 -0
- chump_server-0.0.1/pyproject.toml +39 -0
- chump_server-0.0.1/tests/test_tools.py +39 -0
- chump_server-0.0.1/uv.lock +626 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
.DS_Store
|
|
2
|
+
.env
|
|
3
|
+
.env.*
|
|
4
|
+
.plans/
|
|
5
|
+
.chump/
|
|
6
|
+
ai-query/
|
|
7
|
+
opencode/
|
|
8
|
+
|
|
9
|
+
client/node_modules/
|
|
10
|
+
client/dist/
|
|
11
|
+
client/.turbo/
|
|
12
|
+
client/.cache/
|
|
13
|
+
node_modules/
|
|
14
|
+
|
|
15
|
+
server/.venv/
|
|
16
|
+
server/.pytest_cache/
|
|
17
|
+
server/.mypy_cache/
|
|
18
|
+
server/.ruff_cache/
|
|
19
|
+
server/.coverage
|
|
20
|
+
server/.chump/
|
|
21
|
+
server/*.db
|
|
22
|
+
server/*.sqlite
|
|
23
|
+
server/*.sqlite3
|
|
24
|
+
|
|
25
|
+
__pycache__/
|
|
26
|
+
*.pyc
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chump-server
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python backend for the chump coding assistant
|
|
5
|
+
Keywords: agent,ai,backend,chump,cli
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: ai-query==1.7.31
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# chump-server
|
|
11
|
+
|
|
12
|
+
The backend hosts `ChumpAgent` instances over `ai-query`'s built-in agent
|
|
13
|
+
server routes.
|
|
14
|
+
|
|
15
|
+
## Run
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
uv sync
|
|
19
|
+
uv run chump-server
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
Once published, the backend package can be installed or run directly with `uv`:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
uv tool install chump-server
|
|
28
|
+
chump-server
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
or:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
uvx --from chump-server chump-server
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
During repository development, `uv` still uses the local editable `../ai-query`
|
|
38
|
+
source from `pyproject.toml`.
|
|
39
|
+
|
|
40
|
+
## Release
|
|
41
|
+
|
|
42
|
+
`chump-server` versions come from git tags through `hatch-vcs`.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
git tag chump-server-v0.1.0
|
|
46
|
+
git push origin chump-server-v0.1.0
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Pushing a `chump-server-v*` tag runs the PyPI job in `.github/workflows/release.yml`.
|
|
50
|
+
|
|
51
|
+
## Environment
|
|
52
|
+
|
|
53
|
+
- `CHUMP_HOST`: default `127.0.0.1`
|
|
54
|
+
- `CHUMP_PORT`: default `8080`
|
|
55
|
+
- `CHUMP_WORKSPACE_ROOT`: defaults to the parent directory of `server/`
|
|
56
|
+
- `CHUMP_DATA_DIR`: default `.chump`
|
|
57
|
+
- `CHUMP_PROVIDER`: default `openai`; also supports `workers_ai`
|
|
58
|
+
- `CHUMP_MODEL`: provider-specific default
|
|
59
|
+
- `CHUMP_MAX_STEPS`: default `64`
|
|
60
|
+
- `CHUMP_VERBOSE`: default `1`
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# chump-server
|
|
2
|
+
|
|
3
|
+
The backend hosts `ChumpAgent` instances over `ai-query`'s built-in agent
|
|
4
|
+
server routes.
|
|
5
|
+
|
|
6
|
+
## Run
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
uv sync
|
|
10
|
+
uv run chump-server
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
Once published, the backend package can be installed or run directly with `uv`:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
uv tool install chump-server
|
|
19
|
+
chump-server
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
or:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
uvx --from chump-server chump-server
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
During repository development, `uv` still uses the local editable `../ai-query`
|
|
29
|
+
source from `pyproject.toml`.
|
|
30
|
+
|
|
31
|
+
## Release
|
|
32
|
+
|
|
33
|
+
`chump-server` versions come from git tags through `hatch-vcs`.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
git tag chump-server-v0.1.0
|
|
37
|
+
git push origin chump-server-v0.1.0
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Pushing a `chump-server-v*` tag runs the PyPI job in `.github/workflows/release.yml`.
|
|
41
|
+
|
|
42
|
+
## Environment
|
|
43
|
+
|
|
44
|
+
- `CHUMP_HOST`: default `127.0.0.1`
|
|
45
|
+
- `CHUMP_PORT`: default `8080`
|
|
46
|
+
- `CHUMP_WORKSPACE_ROOT`: defaults to the parent directory of `server/`
|
|
47
|
+
- `CHUMP_DATA_DIR`: default `.chump`
|
|
48
|
+
- `CHUMP_PROVIDER`: default `openai`; also supports `workers_ai`
|
|
49
|
+
- `CHUMP_MODEL`: provider-specific default
|
|
50
|
+
- `CHUMP_MAX_STEPS`: default `64`
|
|
51
|
+
- `CHUMP_VERBOSE`: default `1`
|
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import time
|
|
6
|
+
from dataclasses import replace
|
|
7
|
+
from typing import Any, AsyncIterator
|
|
8
|
+
|
|
9
|
+
from ai_query import step_count_is, stream_text
|
|
10
|
+
from ai_query.agents import Agent, SQLiteStorage, action
|
|
11
|
+
from ai_query.providers import anthropic, google, openai, workers_ai
|
|
12
|
+
from ai_query.types import AbortController, AbortError, AbortSignal, Message
|
|
13
|
+
|
|
14
|
+
from .config import ChumpConfig, load_config
|
|
15
|
+
from .tools import build_tools
|
|
16
|
+
|
|
17
|
+
SYSTEM_PROMPT = """
|
|
18
|
+
|
|
19
|
+
You are Chump, an interactive CLI coding agent that helps users with software engineering tasks inside their local workspace.
|
|
20
|
+
|
|
21
|
+
You're currently inside of their project directory
|
|
22
|
+
|
|
23
|
+
you can ls -la to see the contents of the current directory.
|
|
24
|
+
|
|
25
|
+
IMPORTANT: You must NEVER generate or guess URLs for the user unless you are confident that the URLs are for helping the user with programming. You may use URLs provided by the user in their messages or local files.
|
|
26
|
+
|
|
27
|
+
You are a great coding agent when you behave like a careful engineer in a terminal:
|
|
28
|
+
- Build a map of the workspace before making claims about it.
|
|
29
|
+
- Use normal shell discovery habits: `pwd`, `ls`, `find`, `rg`, `git status`, package manifests, and README files.
|
|
30
|
+
- Prefer `rg` for content search and `find` for filename/path discovery.
|
|
31
|
+
- Read specific files only after you have found where they are.
|
|
32
|
+
- For broad requests like "what is this project?" or "make a deep wiki", inspect the repository structure first, then read the key files, then synthesize.
|
|
33
|
+
- Do not ask the user which obvious inspection step to take next. Continue until you have enough evidence or hit a real blocker.
|
|
34
|
+
- Do not guess paths such as `src` or `packages/*`; discover them.
|
|
35
|
+
- Prefer `apply_patch` for modifying existing files. Use `write_file` for full rewrites or creating a new file from scratch.
|
|
36
|
+
|
|
37
|
+
If the user asks for help or wants to give feedback inform them of the following:
|
|
38
|
+
- /help: Get help with using Chump
|
|
39
|
+
- To give feedback, users should report the issue at https://github.com/abdulmumin1/chump/issues
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# Tone and style
|
|
43
|
+
You should be concise, direct, and to the point. When you run a non-trivial bash command, you should explain what the command does and why you are running it, to make sure the user understands what you are doing (this is especially important when you are running a command that will make changes to the user's system).
|
|
44
|
+
Remember that your output will be displayed on a command line interface. Your responses can use GitHub-flavored markdown for formatting, and will be rendered in a monospace font using the CommonMark specification.
|
|
45
|
+
Output text to communicate with the user; all text you output outside of tool use is displayed to the user. Only use tools to complete tasks. Never use tools like Bash or code comments as means to communicate with the user during the session.
|
|
46
|
+
If you cannot or will not help the user with something, please do not say why or what it could lead to, since this comes across as preachy and annoying. Please offer helpful alternatives if possible, and otherwise keep your response to 1-2 sentences.
|
|
47
|
+
Only use emojis if the user explicitly requests it. Avoid using emojis in all communication unless asked.
|
|
48
|
+
IMPORTANT: You should minimize output tokens as much as possible while maintaining helpfulness, quality, and accuracy. Only address the specific query or task at hand, avoiding tangential information unless absolutely critical for completing the request. If you can answer in 1-3 sentences or a short paragraph, please do.
|
|
49
|
+
IMPORTANT: You should NOT answer with unnecessary preamble or postamble (such as explaining your code or summarizing your action), unless the user asks you to.
|
|
50
|
+
IMPORTANT: Keep your responses short, since they will be displayed on a command line interface. You MUST answer concisely with fewer than 4 lines (not including tool use or code generation), unless user asks for detail. Answer the user's question directly, without elaboration, explanation, or details. One word answers are best. Avoid introductions, conclusions, and explanations. You MUST avoid text before/after your response, such as "The answer is <answer>.", "Here is the content of the file..." or "Based on the information provided, the answer is..." or "Here is what I will do next...". Here are some examples to demonstrate appropriate verbosity:
|
|
51
|
+
<example>
|
|
52
|
+
user: 2 + 2
|
|
53
|
+
assistant: 4
|
|
54
|
+
</example>
|
|
55
|
+
|
|
56
|
+
<example>
|
|
57
|
+
user: what is 2+2?
|
|
58
|
+
assistant: 4
|
|
59
|
+
</example>
|
|
60
|
+
|
|
61
|
+
<example>
|
|
62
|
+
user: is 11 a prime number?
|
|
63
|
+
assistant: Yes
|
|
64
|
+
</example>
|
|
65
|
+
|
|
66
|
+
<example>
|
|
67
|
+
user: what command should I run to list files in the current directory?
|
|
68
|
+
assistant: ls
|
|
69
|
+
</example>
|
|
70
|
+
|
|
71
|
+
<example>
|
|
72
|
+
user: what command should I run to watch files in the current directory?
|
|
73
|
+
assistant: [use the ls tool to list the files in the current directory, then read docs/commands in the relevant file to find out how to watch files]
|
|
74
|
+
npm run dev
|
|
75
|
+
</example>
|
|
76
|
+
|
|
77
|
+
<example>
|
|
78
|
+
user: How many golf balls fit inside a jetta?
|
|
79
|
+
assistant: 150000
|
|
80
|
+
</example>
|
|
81
|
+
|
|
82
|
+
<example>
|
|
83
|
+
user: what files are in the directory src/?
|
|
84
|
+
assistant: [runs ls and sees foo.c, bar.c, baz.c]
|
|
85
|
+
user: which file contains the implementation of foo?
|
|
86
|
+
assistant: src/foo.c
|
|
87
|
+
</example>
|
|
88
|
+
|
|
89
|
+
<example>
|
|
90
|
+
user: write tests for new feature
|
|
91
|
+
assistant: [uses bash with rg/find to locate similar tests, reads relevant files, edits the test files, then runs the appropriate test command]
|
|
92
|
+
</example>
|
|
93
|
+
|
|
94
|
+
# Proactiveness
|
|
95
|
+
You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between:
|
|
96
|
+
1. Doing the right thing when asked, including taking actions and follow-up actions
|
|
97
|
+
2. Not surprising the user with actions you take without asking
|
|
98
|
+
For example, if the user asks you how to approach something, you should do your best to answer their question first, and not immediately jump into taking actions.
|
|
99
|
+
3. Do not add additional code explanation summary unless requested by the user. After working on a file, just stop, rather than providing an explanation of what you did.
|
|
100
|
+
|
|
101
|
+
# Following conventions
|
|
102
|
+
When making changes to files, first understand the file's code conventions. Mimic code style, use existing libraries and utilities, and follow existing patterns.
|
|
103
|
+
- NEVER assume that a given library is available, even if it is well known. Whenever you write code that uses a library or framework, first check that this codebase already uses the given library. For example, you might look at neighboring files, or check the package.json (or cargo.toml, and so on depending on the language).
|
|
104
|
+
- When you create a new component, first look at existing components to see how they're written; then consider framework choice, naming conventions, typing, and other conventions.
|
|
105
|
+
- When you edit a piece of code, first look at the code's surrounding context (especially its imports) to understand the code's choice of frameworks and libraries. Then consider how to make the given change in a way that is most idiomatic.
|
|
106
|
+
- Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to the repository.
|
|
107
|
+
|
|
108
|
+
# Code style
|
|
109
|
+
- IMPORTANT: DO NOT ADD ***ANY*** COMMENTS unless asked
|
|
110
|
+
|
|
111
|
+
# Doing tasks
|
|
112
|
+
The user will primarily request you perform software engineering tasks. This includes solving bugs, adding new functionality, refactoring code, explaining code, and more. For these tasks the following steps are recommended:
|
|
113
|
+
- Use bash with `rg`, `find`, `ls`, and related shell tools to understand the codebase and the user's query.
|
|
114
|
+
- Implement the solution using the available workspace operations.
|
|
115
|
+
- Verify the solution if possible with tests. NEVER assume specific test framework or test script. Check the README or search codebase to determine the testing approach.
|
|
116
|
+
- VERY IMPORTANT: When you have completed a task, you MUST run the lint and typecheck commands (e.g. npm run lint, npm run typecheck, ruff, etc.) with Bash if they were provided to you to ensure your code is correct. If you are unable to find the correct command, ask the user for the command to run and if they supply it, proactively suggest writing it to AGENTS.md so that you will know to run it next time.
|
|
117
|
+
NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
|
|
118
|
+
|
|
119
|
+
- Tool results and user messages may include <system-reminder> tags. <system-reminder> tags contain useful information and reminders. They are NOT part of the user's provided input or the tool result.
|
|
120
|
+
|
|
121
|
+
# Tool usage policy
|
|
122
|
+
- When doing file search, prefer bash with `rg` for content search and `find` for filename/path search.
|
|
123
|
+
- When multiple independent facts are needed, gather them efficiently before answering.
|
|
124
|
+
|
|
125
|
+
You MUST answer concisely with fewer than 4 lines of text (not including tool use or code generation), unless user asks for detail.
|
|
126
|
+
|
|
127
|
+
IMPORTANT: Before you begin work, think about what the code you're editing is supposed to do based on the filenames directory structure.
|
|
128
|
+
|
|
129
|
+
# Code References
|
|
130
|
+
|
|
131
|
+
When referencing specific functions or pieces of code include the pattern `file_path:line_number` to allow the user to easily navigate to the source code location.
|
|
132
|
+
|
|
133
|
+
<example>
|
|
134
|
+
user: Where are errors from the client handled?
|
|
135
|
+
assistant: Clients are marked as failed in the `connectToServer` function in src/services/process.ts:712.
|
|
136
|
+
</example>
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def resolve_model(config: ChumpConfig):
|
|
141
|
+
provider_name = config.provider.lower()
|
|
142
|
+
if provider_name == "openai":
|
|
143
|
+
return openai(
|
|
144
|
+
config.model,
|
|
145
|
+
base_url=os.environ.get("OPENAI_BASE_URL"),
|
|
146
|
+
organization=os.environ.get("OPENAI_ORGANIZATION"),
|
|
147
|
+
)
|
|
148
|
+
if provider_name == "google":
|
|
149
|
+
return google(config.model)
|
|
150
|
+
if provider_name == "anthropic":
|
|
151
|
+
return anthropic(config.model, base_url=os.environ.get("ANTHROPIC_BASE_URL"))
|
|
152
|
+
if provider_name == "workers_ai":
|
|
153
|
+
return workers_ai(config.model)
|
|
154
|
+
raise ValueError(f"unsupported provider: {config.provider}")
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class ChumpAgent(Agent[dict[str, Any]]):
|
|
158
|
+
enable_event_log = True
|
|
159
|
+
|
|
160
|
+
def __init__(self, id: str):
|
|
161
|
+
config = load_config()
|
|
162
|
+
config.data_dir.mkdir(parents=True, exist_ok=True)
|
|
163
|
+
now = time.time()
|
|
164
|
+
super().__init__(
|
|
165
|
+
id,
|
|
166
|
+
model=None,
|
|
167
|
+
system=SYSTEM_PROMPT,
|
|
168
|
+
storage=SQLiteStorage(str(config.data_dir / "chump.sqlite3")),
|
|
169
|
+
initial_state={
|
|
170
|
+
"workspace_root": str(config.workspace_root),
|
|
171
|
+
"title": None,
|
|
172
|
+
"created_at": now,
|
|
173
|
+
"updated_at": now,
|
|
174
|
+
"last_user_goal": None,
|
|
175
|
+
"files_touched": [],
|
|
176
|
+
"read_files": {},
|
|
177
|
+
"commands_run": [],
|
|
178
|
+
"notes": [],
|
|
179
|
+
},
|
|
180
|
+
tools={},
|
|
181
|
+
stop_when=step_count_is(config.max_steps),
|
|
182
|
+
reasoning=config.reasoning,
|
|
183
|
+
)
|
|
184
|
+
self._config = config
|
|
185
|
+
self.tools = build_tools(self, config)
|
|
186
|
+
self._last_step_records: list[dict[str, Any]] = []
|
|
187
|
+
self._current_abort_controller: AbortController | None = None
|
|
188
|
+
|
|
189
|
+
@action
|
|
190
|
+
async def status(self) -> dict[str, Any]:
|
|
191
|
+
return {
|
|
192
|
+
"agent_id": self.id,
|
|
193
|
+
"workspace_root": str(self._config.workspace_root),
|
|
194
|
+
"provider": self._config.provider,
|
|
195
|
+
"model": self._config.model,
|
|
196
|
+
"max_steps": self._config.max_steps,
|
|
197
|
+
"command_timeout": self._config.command_timeout,
|
|
198
|
+
"reasoning": self._config.reasoning,
|
|
199
|
+
"verbose": self._config.verbose,
|
|
200
|
+
"message_count": len(self.messages),
|
|
201
|
+
"title": self.state.get("title"),
|
|
202
|
+
"created_at": self.state.get("created_at"),
|
|
203
|
+
"updated_at": self.state.get("updated_at"),
|
|
204
|
+
"last_user_goal": self.state.get("last_user_goal"),
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
@action
|
|
208
|
+
async def clear_messages(self) -> dict[str, str]:
|
|
209
|
+
now = time.time()
|
|
210
|
+
await self.clear()
|
|
211
|
+
await self.update_state(last_user_goal=None, read_files={}, updated_at=now)
|
|
212
|
+
return {"status": "ok"}
|
|
213
|
+
|
|
214
|
+
@action
|
|
215
|
+
async def event_log(self) -> dict[str, Any]:
|
|
216
|
+
return {"events": list(self._event_log)}
|
|
217
|
+
|
|
218
|
+
@action
|
|
219
|
+
async def abort_current_turn(self) -> dict[str, Any]:
|
|
220
|
+
controller = self._current_abort_controller
|
|
221
|
+
if controller is None:
|
|
222
|
+
return {"status": "idle"}
|
|
223
|
+
controller.abort("aborted by user")
|
|
224
|
+
return {"status": "aborting"}
|
|
225
|
+
|
|
226
|
+
@action
|
|
227
|
+
async def set_model(self, provider: str, model: str) -> dict[str, Any]:
|
|
228
|
+
from .config import apply_auth_environment, load_auth_config, normalize_provider_name
|
|
229
|
+
|
|
230
|
+
provider_name = normalize_provider_name(provider)
|
|
231
|
+
if not model.strip():
|
|
232
|
+
raise ValueError("model is required")
|
|
233
|
+
apply_auth_environment(load_auth_config(), provider_name)
|
|
234
|
+
self._config = replace(self._config, provider=provider_name, model=model.strip())
|
|
235
|
+
self.model = resolve_model(self._config)
|
|
236
|
+
return await self.status()
|
|
237
|
+
|
|
238
|
+
@property
|
|
239
|
+
def current_abort_signal(self) -> AbortSignal | None:
|
|
240
|
+
controller = self._current_abort_controller
|
|
241
|
+
return controller.signal if controller else None
|
|
242
|
+
|
|
243
|
+
async def chat(
|
|
244
|
+
self,
|
|
245
|
+
message: str,
|
|
246
|
+
*,
|
|
247
|
+
signal: AbortSignal | None = None,
|
|
248
|
+
**kwargs: Any,
|
|
249
|
+
) -> str:
|
|
250
|
+
try:
|
|
251
|
+
result = await self._start_chat_stream(message, signal=signal, **kwargs)
|
|
252
|
+
chunks: list[str] = []
|
|
253
|
+
async for chunk in result.text_stream:
|
|
254
|
+
await self.emit("assistant_text", {"content": chunk})
|
|
255
|
+
chunks.append(chunk)
|
|
256
|
+
final_response = await self._finalize_chat_stream(result, "".join(chunks))
|
|
257
|
+
if not chunks and final_response:
|
|
258
|
+
await self.emit("assistant_text", {"content": final_response})
|
|
259
|
+
return final_response
|
|
260
|
+
except AbortError:
|
|
261
|
+
self._discard_last_user_message(message)
|
|
262
|
+
raise
|
|
263
|
+
|
|
264
|
+
async def stream(
|
|
265
|
+
self,
|
|
266
|
+
message: str,
|
|
267
|
+
*,
|
|
268
|
+
signal: AbortSignal | None = None,
|
|
269
|
+
**kwargs: Any,
|
|
270
|
+
) -> AsyncIterator[str]:
|
|
271
|
+
try:
|
|
272
|
+
result = await self._start_chat_stream(message, signal=signal, **kwargs)
|
|
273
|
+
full_response = ""
|
|
274
|
+
async for chunk in result.text_stream:
|
|
275
|
+
full_response += chunk
|
|
276
|
+
await self.emit("assistant_text", {"content": chunk})
|
|
277
|
+
yield chunk
|
|
278
|
+
|
|
279
|
+
final_response = await self._finalize_chat_stream(result, full_response)
|
|
280
|
+
if not full_response.strip():
|
|
281
|
+
await self.emit("assistant_text", {"content": final_response})
|
|
282
|
+
yield final_response
|
|
283
|
+
except AbortError:
|
|
284
|
+
self._discard_last_user_message(message)
|
|
285
|
+
raise
|
|
286
|
+
|
|
287
|
+
async def _start_chat_stream(
|
|
288
|
+
self,
|
|
289
|
+
message: str,
|
|
290
|
+
*,
|
|
291
|
+
signal: AbortSignal | None = None,
|
|
292
|
+
**kwargs: Any,
|
|
293
|
+
):
|
|
294
|
+
self._ensure_model()
|
|
295
|
+
self._last_step_records = []
|
|
296
|
+
self._log(f"chat start: {message}")
|
|
297
|
+
self._messages.append(Message(role="user", content=message))
|
|
298
|
+
await self.emit("user_message", {"content": message})
|
|
299
|
+
now = time.time()
|
|
300
|
+
created_at = self.state.get("created_at")
|
|
301
|
+
title = self.state.get("title")
|
|
302
|
+
await self.update_state(
|
|
303
|
+
title=title or build_session_title(message),
|
|
304
|
+
created_at=created_at if isinstance(created_at, (int, float)) else now,
|
|
305
|
+
updated_at=now,
|
|
306
|
+
last_user_goal=message,
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
return stream_text(
|
|
310
|
+
model=self.model,
|
|
311
|
+
system=self.system,
|
|
312
|
+
messages=self._messages,
|
|
313
|
+
tools=self.tools if self.tools else None,
|
|
314
|
+
stop_when=self.stop_when,
|
|
315
|
+
provider_options=self.provider_options,
|
|
316
|
+
reasoning=self.reasoning,
|
|
317
|
+
signal=signal,
|
|
318
|
+
on_reasoning_event=self._handle_reasoning_event,
|
|
319
|
+
on_step_start=self._on_step_start,
|
|
320
|
+
on_step_finish=self._on_step_finish,
|
|
321
|
+
**kwargs,
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
async def handle_request_stream(
|
|
325
|
+
self, request: dict[str, Any]
|
|
326
|
+
) -> AsyncIterator[str]:
|
|
327
|
+
if self._state is None:
|
|
328
|
+
await self.start()
|
|
329
|
+
|
|
330
|
+
if request.get("action", "chat") != "chat":
|
|
331
|
+
yield 'event: error\ndata: "Streaming not supported for this action"\n\n'
|
|
332
|
+
return
|
|
333
|
+
|
|
334
|
+
message = request.get("message", "")
|
|
335
|
+
controller = AbortController()
|
|
336
|
+
self._current_abort_controller = controller
|
|
337
|
+
|
|
338
|
+
try:
|
|
339
|
+
yield "event: start\ndata: \n\n"
|
|
340
|
+
|
|
341
|
+
full_text = ""
|
|
342
|
+
async for chunk in self.stream(message, signal=controller.signal):
|
|
343
|
+
full_text += chunk
|
|
344
|
+
yield f"event: chunk\ndata: {json.dumps(chunk)}\n\n"
|
|
345
|
+
|
|
346
|
+
yield f"event: end\ndata: {json.dumps(full_text)}\n\n"
|
|
347
|
+
except Exception as exc:
|
|
348
|
+
yield f"event: error\ndata: {json.dumps(str(exc))}\n\n"
|
|
349
|
+
finally:
|
|
350
|
+
if self._current_abort_controller is controller:
|
|
351
|
+
self._current_abort_controller = None
|
|
352
|
+
|
|
353
|
+
async def _finalize_chat_stream(self, result: Any, full_response: str) -> str:
|
|
354
|
+
if not full_response.strip():
|
|
355
|
+
full_response = await self._build_empty_response_fallback(result)
|
|
356
|
+
self._log(f"chat produced fallback response: {full_response}")
|
|
357
|
+
else:
|
|
358
|
+
self._log(f"chat complete with {len(full_response)} chars")
|
|
359
|
+
|
|
360
|
+
self._append_step_messages(await self._get_result_steps(result), full_response)
|
|
361
|
+
await self._persist_messages()
|
|
362
|
+
return full_response
|
|
363
|
+
|
|
364
|
+
async def _on_step_start(self, event) -> None:
|
|
365
|
+
self._log(f"step {event.step_number} start")
|
|
366
|
+
await self.emit("status", {"phase": "step_start", "step": event.step_number})
|
|
367
|
+
|
|
368
|
+
async def _on_step_finish(self, event) -> None:
|
|
369
|
+
record = {
|
|
370
|
+
"step": event.step_number,
|
|
371
|
+
"text": event.step.text,
|
|
372
|
+
"tool_calls": [
|
|
373
|
+
{"name": call.name, "arguments": call.arguments}
|
|
374
|
+
for call in event.step.tool_calls
|
|
375
|
+
],
|
|
376
|
+
"tool_results": [
|
|
377
|
+
{
|
|
378
|
+
"tool_name": result.tool_name,
|
|
379
|
+
"is_error": result.is_error,
|
|
380
|
+
"result": str(result.result),
|
|
381
|
+
}
|
|
382
|
+
for result in event.step.tool_results
|
|
383
|
+
],
|
|
384
|
+
"finish_reason": event.step.finish_reason,
|
|
385
|
+
}
|
|
386
|
+
self._last_step_records.append(record)
|
|
387
|
+
payload = {
|
|
388
|
+
"phase": "step_finish",
|
|
389
|
+
"step": event.step_number,
|
|
390
|
+
"tool_calls": record["tool_calls"],
|
|
391
|
+
"tool_results": [
|
|
392
|
+
{
|
|
393
|
+
"tool_name": result["tool_name"],
|
|
394
|
+
"is_error": result["is_error"],
|
|
395
|
+
}
|
|
396
|
+
for result in record["tool_results"]
|
|
397
|
+
],
|
|
398
|
+
}
|
|
399
|
+
self._log(
|
|
400
|
+
"step "
|
|
401
|
+
f"{event.step_number} finish: "
|
|
402
|
+
f"finish_reason={event.step.finish_reason!r} "
|
|
403
|
+
f"text_chars={len(event.step.text)} "
|
|
404
|
+
f"tool_calls={len(event.step.tool_calls)} "
|
|
405
|
+
f"tool_results={len(event.step.tool_results)}"
|
|
406
|
+
)
|
|
407
|
+
for tool_call in record["tool_calls"]:
|
|
408
|
+
self._log(
|
|
409
|
+
"tool call: "
|
|
410
|
+
f"{tool_call['name']} "
|
|
411
|
+
f"{json.dumps(tool_call['arguments'], ensure_ascii=True)}"
|
|
412
|
+
)
|
|
413
|
+
for tool_result in record["tool_results"]:
|
|
414
|
+
status = "error" if tool_result["is_error"] else "ok"
|
|
415
|
+
preview = tool_result["result"].replace("\n", " ")
|
|
416
|
+
if len(preview) > 240:
|
|
417
|
+
preview = preview[:237] + "..."
|
|
418
|
+
self._log(f"tool result [{status}] {tool_result['tool_name']}: {preview}")
|
|
419
|
+
await self.emit("status", payload)
|
|
420
|
+
|
|
421
|
+
def _ensure_model(self) -> None:
|
|
422
|
+
if self.model is None:
|
|
423
|
+
self.model = resolve_model(self._config)
|
|
424
|
+
|
|
425
|
+
async def _build_empty_response_fallback(self, result) -> str:
|
|
426
|
+
steps = await result.steps
|
|
427
|
+
if not steps and self._last_step_records:
|
|
428
|
+
if any(step["tool_results"] for step in self._last_step_records):
|
|
429
|
+
last_results = self._last_step_records[-1]["tool_results"]
|
|
430
|
+
if last_results:
|
|
431
|
+
last_tool_result = last_results[-1]
|
|
432
|
+
if last_tool_result["is_error"]:
|
|
433
|
+
return (
|
|
434
|
+
"I did not finish the request. The last tool failed: "
|
|
435
|
+
f"{last_tool_result['result']}"
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
tool_names = [
|
|
439
|
+
call["name"]
|
|
440
|
+
for step in self._last_step_records
|
|
441
|
+
for call in step["tool_calls"]
|
|
442
|
+
]
|
|
443
|
+
if tool_names:
|
|
444
|
+
recent_tools = ", ".join(tool_names[-5:])
|
|
445
|
+
return (
|
|
446
|
+
"I inspected the workspace but did not produce a final answer. "
|
|
447
|
+
f"Recent tool calls: {recent_tools}. "
|
|
448
|
+
"Check the server logs for the exact step trace."
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
if not steps:
|
|
452
|
+
return (
|
|
453
|
+
"I did not produce a response. The model returned no text and no"
|
|
454
|
+
" tool activity."
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
last_step = steps[-1]
|
|
458
|
+
if last_step.tool_results:
|
|
459
|
+
last_result = last_step.tool_results[-1]
|
|
460
|
+
if last_result.is_error:
|
|
461
|
+
return (
|
|
462
|
+
"I did not finish the request. The last tool failed: "
|
|
463
|
+
f"{last_result.result}"
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
tool_names = [call.name for step in steps for call in step.tool_calls]
|
|
467
|
+
if tool_names:
|
|
468
|
+
recent_tools = ", ".join(tool_names[-5:])
|
|
469
|
+
return (
|
|
470
|
+
"I inspected the workspace but did not produce a final answer. "
|
|
471
|
+
f"Recent tool calls: {recent_tools}. "
|
|
472
|
+
"Try `/events on` to watch tool activity, or raise "
|
|
473
|
+
"`CHUMP_MAX_STEPS` if the task needs a longer loop."
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
return (
|
|
477
|
+
"I did not produce a final answer for that request. "
|
|
478
|
+
"Try again or enable `/events on` for more visibility."
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
def _log(self, message: str) -> None:
|
|
482
|
+
if not self._config.verbose:
|
|
483
|
+
return
|
|
484
|
+
print(f"[chump:{self.id}] {message}", flush=True)
|
|
485
|
+
|
|
486
|
+
def _discard_last_user_message(self, message: str) -> None:
|
|
487
|
+
if not self._messages:
|
|
488
|
+
return
|
|
489
|
+
last = self._messages[-1]
|
|
490
|
+
if last.role == "user" and last.content == message:
|
|
491
|
+
self._messages.pop()
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
def build_session_title(message: str) -> str:
|
|
495
|
+
normalized = " ".join(message.strip().split())
|
|
496
|
+
if not normalized:
|
|
497
|
+
return "Untitled session"
|
|
498
|
+
if len(normalized) <= 72:
|
|
499
|
+
return normalized
|
|
500
|
+
return normalized[:69].rstrip() + "..."
|