prompt-copilot-cli 0.2.3__tar.gz → 0.2.8__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.
- {prompt_copilot_cli-0.2.3/prompt_copilot_cli.egg-info → prompt_copilot_cli-0.2.8}/PKG-INFO +1 -1
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/main.py +292 -48
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8/prompt_copilot_cli.egg-info}/PKG-INFO +1 -1
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/pyproject.toml +1 -1
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/tests/test_cli_args.py +37 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/tests/test_file_tools.py +28 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/LICENSE +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/MANIFEST.in +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/README.md +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/README.zh-CN.md +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/SOURCES.txt +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/dependency_links.txt +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/entry_points.txt +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/requires.txt +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/top_level.txt +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/requirements.txt +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/setup.cfg +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/tests/test_command_timeout.py +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/tests/test_image_tool.py +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/tests/test_mcp_transport.py +0 -0
- {prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/tests/test_multimodal_messages.py +0 -0
|
@@ -96,10 +96,7 @@ def build_bottom_toolbar_text() -> str:
|
|
|
96
96
|
|
|
97
97
|
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
你是一个基于 CLI 的编程 Agent,你的名字是 Jason Li,专注于使用文件、命令、Python 脚本等工具完成开发任务。
|
|
101
|
-
|
|
102
|
-
工作原则:
|
|
99
|
+
DEFAULT_SYSTEM_WORKING_PRINCIPLES_ZH = """工作原则:
|
|
103
100
|
1、先检查工作目录并理解需求。
|
|
104
101
|
2、优先使用网络搜索类工具搜索网络实时信息,例如:天气、新闻、资讯等等。
|
|
105
102
|
3、所有代码文件生成、编辑、删除等操作均在工作目录中执行。如果用户没有指定具体目录,默认工作目录为工程的根目录。
|
|
@@ -110,18 +107,15 @@ ZH_SYSTEM_PROMPT = """
|
|
|
110
107
|
8、如果用户想“读取/识别其他二进制文件”,你必须明确告知:我无法直接读取或理解通用二进制文件内容。
|
|
111
108
|
9、如果用户想“查看图片内容”,应优先调用图片读取工具将图片转成 base64 或视觉消息格式,再将其发送给支持多模态的模型。
|
|
112
109
|
10、对于图片类任务,优先使用图片工具而不是尝试直接读取二进制原始内容。
|
|
113
|
-
11
|
|
110
|
+
11、当用户输入一条任务指令时,如果是详细的任务清单,你就理解用户要求逐步完成工作,同时执行每个步骤的时候明确说明当前环节与进度,实时反馈任务状态。
|
|
114
111
|
12、使用 execute_python_script 时必须在脚本内自行管理进程生命周期:若是 ls、pytest、build 等短命令用 subprocess.run 显式设置 timeout,若是 npm run dev、flask run 等常驻服务用 subprocess.Popen 脱离终端启动并在脚本内轮询健康检查 URL,确认就绪后打印 PID 并立即 sys.exit() 退出以防触发 360 秒强制超时。
|
|
115
112
|
13、无法调用官方联网检索工具而通过脚本抓取网络内容时需区分数据类型处理,抓取普通网页文本需剔除 HTML 标签、样式代码、广告碎片、无效注释、多余空行等冗余内容,仅留存有效正文且文本超出 8000 字符则截断,文本输出上限为 8000 字符,抓取程序源代码则无需过滤,完整保留原始代码、自带注释、缩进换行与原有格式,代码输出无字符数量限制。
|
|
116
113
|
14、脱离终端启动常驻服务时务必兼顾跨平台兼容:Linux/Mac 环境设置 start_new_session=True,Windows 环境设置 creationflags=subprocess.CREATE_NEW_PROCESS_GROUP。
|
|
117
114
|
15、凡耗时不可控的超长任务(如大文件下载、模型训练、全量测试、复杂构建等),严禁在脚本内同步阻塞,必须用 subprocess.Popen 跨平台脱离终端启动并将进度心跳写入本地日志文件,打印 PID 后立即 sys.exit(),工具 timeout_seconds 设为 15 秒仅验证启动;后续须通过检查日志监控进度,若发现任务完成或长时间无心跳更新,必须主动调用系统命令(如 kill -9 PID)清理后台进程以防孤儿进程耗尽资源;仅在任务自身支持超时参数或耗时可预估时,方可在脚本内同步执行并设合理 timeout_seconds。
|
|
118
|
-
|
|
115
|
+
16、如果需要搜索互联网信息时候优先使用中国国内搜索引擎优先,例如:bing、百度等。
|
|
119
116
|
"""
|
|
120
117
|
|
|
121
|
-
|
|
122
|
-
You are a CLI-based coding agent named Jason Li, focused on completing development tasks by using files, commands, Python scripts, and other tools.
|
|
123
|
-
|
|
124
|
-
Working principles:
|
|
118
|
+
DEFAULT_SYSTEM_WORKING_PRINCIPLES_EN = """Working principles:
|
|
125
119
|
1. First inspect the working directory and understand the requirement.
|
|
126
120
|
2. Prefer using network search tools for real-time information, such as weather, news, or other current topics.
|
|
127
121
|
3. All code file creation, editing, and deletion operations are performed in the working directory. If the user does not specify a directory, the project root is used by default.
|
|
@@ -132,35 +126,59 @@ Working principles:
|
|
|
132
126
|
8. If the user wants to read or recognize other binary files, you must clearly state that you cannot directly read or understand generic binary file contents.
|
|
133
127
|
9. If the user wants to inspect image content, you should first use the image-reading tool to convert the image into base64 or a visual-message format and then send it to a multimodal-capable model.
|
|
134
128
|
10. For image-related tasks, prefer the image tool rather than trying to read the raw binary contents directly.
|
|
135
|
-
11. When the user
|
|
129
|
+
11. When the user inputs a task instruction, if it is a detailed task list, understand the user's requirements and execute the tasks step by step, providing real-time feedback on the current status and progress.
|
|
136
130
|
12. When using the execute_python_script tool, manage the process lifecycle within the script: for short commands like ls, pytest, and build, use subprocess.run with an explicit timeout; for persistent commands like npm run dev and flask run, use subprocess.Popen to start them in the background and poll a health check URL to confirm they are ready before printing the PID and exiting immediately to avoid triggering the 360-second timeout.
|
|
137
|
-
13
|
|
138
|
-
14
|
|
139
|
-
15
|
|
140
|
-
|
|
131
|
+
13. When official network retrieval tools are unavailable and network content is crawled via scripts, differentiated processing shall be performed according to data types: for regular webpage texts, remove redundant contents including HTML tags, style codes, advertising fragments, invalid comments and redundant blank lines, retain only valid main texts and truncate the content if it exceeds 8000 characters with a strict upper limit of 8000 characters for text output; for program source codes, no filtering or cleaning is required, keep the original codes, built-in comments, indentations, line breaks and original formats completely with no character limit on code output.
|
|
132
|
+
14. When starting a persistent service in the background, ensure cross-platform compatibility: set start_new_session=True for Linux/Mac environments and creationflags=subprocess.CREATE_NEW_PROCESS_GROUP for Windows environments.
|
|
133
|
+
15. For long-running tasks with unpredictable durations (such as large file downloads, model training, full tests, complex builds, etc.), synchronous blocking execution within the script is strictly prohibited. Instead, use subprocess.Popen to start the task in the background across platforms and write progress heartbeats to a local log file. After printing the PID, immediately call sys.exit() and set the tool's timeout_seconds to 15 seconds for startup verification only. Subsequently, monitor progress by checking the log file. If the task completes or there is no heartbeat update for an extended period, proactively call system commands (like kill -9 PID) to clean up background processes and prevent orphan processes from consuming resources. Only when the task itself supports timeout parameters or has a predictable duration can synchronous execution be performed within the script with a reasonable timeout_seconds setting.
|
|
134
|
+
16. When searching the internet for information, prioritize domestic Chinese search engines such as Bing or Baidu.
|
|
141
135
|
"""
|
|
142
136
|
|
|
137
|
+
TASK_END_GUIDANCE_ZH = """你是提示词优化专家,负责将对话记录整理为更清晰、可执行的最终任务提示词。
|
|
138
|
+
|
|
139
|
+
请与默认系统提示词保持一致的工作原则与约束条件:
|
|
140
|
+
{principles}
|
|
141
|
+
|
|
142
|
+
生成最终提示词时,请严格遵循以下约束:
|
|
143
|
+
1. 只输出最终改进后的提示词文本,不要添加任何解释、元信息或注释。
|
|
144
|
+
2. 保留用户原始目标、关键上下文、澄清或变更后的要求、待执行步骤与预期产物。
|
|
145
|
+
3. 语言必须清晰、可执行、步骤化,适合直接给后续执行器使用。
|
|
146
|
+
4. 如果上下文中存在不确定信息,应显式写成“待确认”或“需验证”的约束项。
|
|
147
|
+
5. 输出长度控制在 18000 字符以内。"""
|
|
148
|
+
|
|
149
|
+
TASK_END_GUIDANCE_EN = """You are a prompt-optimization expert responsible for turning conversation history into a clearer and more actionable final task prompt.
|
|
143
150
|
|
|
144
|
-
|
|
151
|
+
Please follow the same working principles and constraints as the default system prompt:
|
|
152
|
+
{principles}
|
|
153
|
+
|
|
154
|
+
When generating the final prompt, strictly follow these constraints:
|
|
155
|
+
1. Return only the improved final prompt text. Do not add explanations, metadata, or comments.
|
|
156
|
+
2. Preserve the user's original goal, key context, clarified or changed requirements, pending steps, and expected deliverables.
|
|
157
|
+
3. Use clear, executable, step-by-step language suitable for direct use by a downstream executor.
|
|
158
|
+
4. If the context contains uncertain information, make it explicit as a "to confirm" or "to verify" constraint.
|
|
159
|
+
5. Keep the output within 18000 characters."""
|
|
160
|
+
|
|
161
|
+
ZH_SYSTEM_PROMPT = f"""
|
|
145
162
|
你是一个基于 CLI 的编程 Agent,你的名字是 Jason Li,专注于使用文件、命令、Python 脚本等工具完成开发任务。
|
|
146
163
|
|
|
147
|
-
|
|
164
|
+
{DEFAULT_SYSTEM_WORKING_PRINCIPLES_ZH}
|
|
165
|
+
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
EN_SYSTEM_PROMPT = f"""
|
|
169
|
+
You are a CLI-based coding agent named Jason Li, focused on completing development tasks by using files, commands, Python scripts, and other tools.
|
|
170
|
+
|
|
171
|
+
{DEFAULT_SYSTEM_WORKING_PRINCIPLES_EN}
|
|
172
|
+
|
|
173
|
+
"""
|
|
148
174
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
8、如果用户想“读取/识别其他二进制文件”,你必须明确告知:我无法直接读取或理解通用二进制文件内容。
|
|
157
|
-
9、如果用户想“查看图片内容”,应优先调用图片读取工具将图片转成 base64 或视觉消息格式,再将其发送给支持多模态的模型。
|
|
158
|
-
10、对于图片类任务,优先使用图片工具而不是尝试直接读取二进制原始内容。
|
|
159
|
-
11、当用户输入一条任务指令时,如果是详细的任务清单,你就理解用户要求逐步完成工作,同时每个步骤执行完毕要反馈状态和进度。
|
|
160
|
-
12、使用 execute_python_script 时必须在脚本内自行管理进程生命周期:若是 ls、pytest、build 等短命令用 subprocess.run 显式设置 timeout,若是 npm run dev、flask run 等常驻服务用 subprocess.Popen 脱离终端启动并在脚本内轮询健康检查 URL,确认就绪后打印 PID 并立即 sys.exit() 退出以防触发 360 秒强制超时。
|
|
161
|
-
13、无法调用官方联网检索工具而通过脚本抓取网络内容时需区分数据类型处理,抓取普通网页文本需剔除 HTML 标签、样式代码、广告碎片、无效注释、多余空行等冗余内容,仅留存有效正文且文本超出 8000 字符则截断,文本输出上限为 8000 字符,抓取程序源代码则无需过滤,完整保留原始代码、自带注释、缩进换行与原有格式,代码输出无字符数量限制。
|
|
162
|
-
14、脱离终端启动常驻服务时务必兼顾跨平台兼容:Linux/Mac 环境设置 start_new_session=True,Windows 环境设置 creationflags=subprocess.CREATE_NEW_PROCESS_GROUP。
|
|
163
|
-
15、凡耗时不可控的超长任务(如大文件下载、模型训练、全量测试、复杂构建等),严禁在脚本内同步阻塞,必须用 subprocess.Popen 跨平台脱离终端启动并将进度心跳写入本地日志文件,打印 PID 后立即 sys.exit(),工具 timeout_seconds 设为 15 秒仅验证启动;后续须通过检查日志监控进度,若发现任务完成或长时间无心跳更新,必须主动调用系统命令(如 kill -9 PID)清理后台进程以防孤儿进程耗尽资源;仅在任务自身支持超时参数或耗时可预估时,方可在脚本内同步执行并设合理 timeout_seconds。
|
|
175
|
+
|
|
176
|
+
PLANNING_PROMPT = f"""
|
|
177
|
+
你是一个基于 CLI 的编程 Agent,你的名字是 Jason Li,能够通过使用文件、命令、Python 脚本等工具完成开发任务,当前你只能够生成任务执行计划。
|
|
178
|
+
|
|
179
|
+
注意:你的输出只能是“任务执行计划”的文本说明。绝对不能真实执行任何命令、脚本、文件修改或调用任何工具方法。你是在“规划”而非“动手”。
|
|
180
|
+
|
|
181
|
+
在生成任务执行计划时,请遵循以下{DEFAULT_SYSTEM_WORKING_PRINCIPLES_ZH}
|
|
164
182
|
|
|
165
183
|
|
|
166
184
|
请严格按照以下格式输出结果:
|
|
@@ -182,6 +200,10 @@ TRANSLATIONS = {
|
|
|
182
200
|
"zh": ZH_SYSTEM_PROMPT,
|
|
183
201
|
"en": EN_SYSTEM_PROMPT,
|
|
184
202
|
},
|
|
203
|
+
"task_end_system_prompt": {
|
|
204
|
+
"zh": TASK_END_GUIDANCE_ZH.format(principles=DEFAULT_SYSTEM_WORKING_PRINCIPLES_ZH),
|
|
205
|
+
"en": TASK_END_GUIDANCE_EN.format(principles=DEFAULT_SYSTEM_WORKING_PRINCIPLES_EN),
|
|
206
|
+
},
|
|
185
207
|
"config_field_model": {"zh": "模型名称,例如 qwen2.5-7b-instruct", "en": "Model name, for example qwen2.5-7b-instruct"},
|
|
186
208
|
"config_field_base_url": {"zh": "OpenAI 兼容接口地址,例如 http://127.0.0.1:11434/v1", "en": "OpenAI-compatible base URL, for example http://127.0.0.1:11434/v1"},
|
|
187
209
|
"config_field_api_key": {"zh": "API Key;若是本地模型可填写任意非空字符串", "en": "API key; for a local model you can enter any non-empty string"},
|
|
@@ -197,6 +219,8 @@ TRANSLATIONS = {
|
|
|
197
219
|
"copy_file_desc": {"zh": "复制文件。", "en": "Copy a file."},
|
|
198
220
|
"read_image_as_base64_desc": {"zh": "读取图片文件并将其编码为 base64。", "en": "Read an image file and encode it as base64."},
|
|
199
221
|
"list_dir_desc": {"zh": "列出目录中的文件和子目录。", "en": "List files and subdirectories in the given directory."},
|
|
222
|
+
"search_code_desc": {"zh": "在目录中按文本模式搜索匹配内容。", "en": "Search for matching text within a directory."},
|
|
223
|
+
"edit_file_desc": {"zh": "在文件中替换指定文本。", "en": "Replace a specific string in a file."},
|
|
200
224
|
"execute_command_desc": {"zh": "在指定目录下执行 shell 命令。", "en": "Execute a shell command in the specified directory."},
|
|
201
225
|
"execute_python_script_desc": {"zh": "执行一个 Python 脚本或一段 Python 代码。", "en": "Execute a Python script or a block of Python code."},
|
|
202
226
|
"path_desc": {"zh": "要读取的文件路径。", "en": "Path of the file to read."},
|
|
@@ -289,7 +313,6 @@ TRANSLATIONS = {
|
|
|
289
313
|
"context_size_label": {"zh": "提交上下文大小", "en": "Submitted context size"},
|
|
290
314
|
"response_length_label": {"zh": "回复内容长度", "en": "Response content length"},
|
|
291
315
|
"recent_conversations_header": {"zh": "# 最近对话记录", "en": "# Recent conversations"},
|
|
292
|
-
"task_end_system_prompt": {"zh": "你是提示词优化专家(精通将对话记录转化为清晰、可执行的任务提示)。\n输入:首先是用户在输入 /task-start 后会给出第一条初始提示,随后是从该标记以来的对话记录(包括模型回复、工具调用与结果)。\n任务:阅读这些记录,理解用户起始提示与后续的澄清或更改,并结合这些信息生成一个改进后的、清晰且可直接用于执行的最终提示词。\n输出要求:只输出最终改进后的提示词文本,不要添加任何解释、元信息或注释。长度控制在 18000 字符以内。", "en": "You are a prompt-optimization expert skilled at turning conversation history into a clear and actionable task prompt.\nInput: first the user provides an initial prompt after /task-start, then the conversation history from that point onward including model replies, tool calls, and results.\nTask: read these records, understand the initial prompt and any follow-up clarifications or changes, and generate an improved final prompt that is clear, executable, and ready to use.\nOutput requirements: return only the final improved prompt text, with no explanation, metadata, or comments. Keep it within 18000 characters."},
|
|
293
316
|
"task_end_user_message": {"zh": "对话记录(从 /task-start 开始,按时间从旧到新):\n\n{compiled_text}", "en": "Conversation history (starting from /task-start, from oldest to newest):\n\n{compiled_text}"},
|
|
294
317
|
"task_end_generation_failed_log": {"zh": "调用模型生成最终提示失败", "en": "Failed to generate the final prompt with the model"},
|
|
295
318
|
"final_prompt_header": {"zh": "# 最终提示词", "en": "# Final prompt"},
|
|
@@ -305,7 +328,7 @@ def t(key: str, **kwargs: Any) -> str:
|
|
|
305
328
|
|
|
306
329
|
|
|
307
330
|
UI_SYSTEM_LANGUAGE = "en"
|
|
308
|
-
APPLICATION_VERSION = "0.2.
|
|
331
|
+
APPLICATION_VERSION = "0.2.8"
|
|
309
332
|
DEFAULT_SYSTEM_PROMPT = t("system_prompt")
|
|
310
333
|
WORKSPACE_DIR = ROOT / "workspace"
|
|
311
334
|
LOG_DIR = ROOT / "logs"
|
|
@@ -491,6 +514,39 @@ TOOL_DEFINITIONS = [
|
|
|
491
514
|
},
|
|
492
515
|
},
|
|
493
516
|
},
|
|
517
|
+
{
|
|
518
|
+
"type": "function",
|
|
519
|
+
"function": {
|
|
520
|
+
"name": "search_code",
|
|
521
|
+
"description": t("search_code_desc"),
|
|
522
|
+
"parameters": {
|
|
523
|
+
"type": "object",
|
|
524
|
+
"properties": {
|
|
525
|
+
"path": {"type": "string", "description": t("list_dir_path_desc")},
|
|
526
|
+
"pattern": {"type": "string", "description": "Text pattern to search for."},
|
|
527
|
+
"recursive": {"type": "boolean", "description": t("list_dir_recursive_desc")},
|
|
528
|
+
"max_results": {"type": "integer", "description": "Maximum number of matches to return."}
|
|
529
|
+
},
|
|
530
|
+
"required": ["path", "pattern"],
|
|
531
|
+
},
|
|
532
|
+
},
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
"type": "function",
|
|
536
|
+
"function": {
|
|
537
|
+
"name": "edit_file",
|
|
538
|
+
"description": t("edit_file_desc"),
|
|
539
|
+
"parameters": {
|
|
540
|
+
"type": "object",
|
|
541
|
+
"properties": {
|
|
542
|
+
"path": {"type": "string", "description": t("write_path_desc")},
|
|
543
|
+
"old_string": {"type": "string", "description": "The text to replace."},
|
|
544
|
+
"new_string": {"type": "string", "description": "The replacement text."}
|
|
545
|
+
},
|
|
546
|
+
"required": ["path", "old_string", "new_string"],
|
|
547
|
+
},
|
|
548
|
+
},
|
|
549
|
+
},
|
|
494
550
|
{
|
|
495
551
|
"type": "function",
|
|
496
552
|
"function": {
|
|
@@ -1605,6 +1661,94 @@ def execute_tool_call(tool_call: Any) -> dict[str, Any]:
|
|
|
1605
1661
|
logger.info("list_dir result: %s", result)
|
|
1606
1662
|
return result
|
|
1607
1663
|
|
|
1664
|
+
if name == "search_code":
|
|
1665
|
+
file_path = args.get("path") or args.get("file_path")
|
|
1666
|
+
pattern = args.get("pattern")
|
|
1667
|
+
if not file_path or not pattern:
|
|
1668
|
+
result = {"status": "error", "content": t("tool_missing_arg", name="search_code", arg="path/pattern")}
|
|
1669
|
+
logger.error("search_code missing arguments, raw args: %s", args)
|
|
1670
|
+
return result
|
|
1671
|
+
|
|
1672
|
+
path = Path(file_path).expanduser()
|
|
1673
|
+
if not path.is_absolute():
|
|
1674
|
+
path = Path.cwd() / path
|
|
1675
|
+
if not path.exists():
|
|
1676
|
+
result = {"status": "ok", "content": json.dumps([], ensure_ascii=False)}
|
|
1677
|
+
logger.info("search_code result: %s", result)
|
|
1678
|
+
return result
|
|
1679
|
+
|
|
1680
|
+
recursive = bool(args.get("recursive", True))
|
|
1681
|
+
max_results = args.get("max_results")
|
|
1682
|
+
try:
|
|
1683
|
+
max_results = int(max_results) if max_results is not None else 50
|
|
1684
|
+
except (TypeError, ValueError):
|
|
1685
|
+
max_results = 50
|
|
1686
|
+
|
|
1687
|
+
matches: list[dict[str, Any]] = []
|
|
1688
|
+
if path.is_file():
|
|
1689
|
+
candidates = [path]
|
|
1690
|
+
elif recursive:
|
|
1691
|
+
candidates = [item for item in sorted(path.rglob("*")) if item.is_file()]
|
|
1692
|
+
else:
|
|
1693
|
+
candidates = [item for item in sorted(path.iterdir()) if item.is_file()]
|
|
1694
|
+
|
|
1695
|
+
for candidate in candidates:
|
|
1696
|
+
try:
|
|
1697
|
+
content = candidate.read_text(encoding="utf-8")
|
|
1698
|
+
except (UnicodeDecodeError, OSError):
|
|
1699
|
+
continue
|
|
1700
|
+
for line_number, line in enumerate(content.splitlines(), start=1):
|
|
1701
|
+
if pattern in line:
|
|
1702
|
+
matches.append({
|
|
1703
|
+
"path": str(candidate.resolve()),
|
|
1704
|
+
"line": line_number,
|
|
1705
|
+
"content": line,
|
|
1706
|
+
})
|
|
1707
|
+
if len(matches) >= max_results:
|
|
1708
|
+
break
|
|
1709
|
+
if len(matches) >= max_results:
|
|
1710
|
+
break
|
|
1711
|
+
|
|
1712
|
+
result = {"status": "ok", "content": json.dumps(matches, ensure_ascii=False)}
|
|
1713
|
+
logger.info("search_code result: %s", result)
|
|
1714
|
+
return result
|
|
1715
|
+
|
|
1716
|
+
if name == "edit_file":
|
|
1717
|
+
file_path = args.get("path") or args.get("file_path")
|
|
1718
|
+
old_string = args.get("old_string")
|
|
1719
|
+
new_string = args.get("new_string")
|
|
1720
|
+
if not file_path or old_string is None or new_string is None:
|
|
1721
|
+
result = {"status": "error", "content": t("tool_missing_arg", name="edit_file", arg="path/old_string/new_string")}
|
|
1722
|
+
logger.error("edit_file missing arguments, raw args: %s", args)
|
|
1723
|
+
return result
|
|
1724
|
+
|
|
1725
|
+
path = Path(file_path).expanduser()
|
|
1726
|
+
if not path.is_absolute():
|
|
1727
|
+
path = Path.cwd() / path
|
|
1728
|
+
if not path.exists():
|
|
1729
|
+
result = {"status": "error", "content": f"File does not exist: {path}"}
|
|
1730
|
+
logger.info("edit_file result: %s", result)
|
|
1731
|
+
return result
|
|
1732
|
+
|
|
1733
|
+
try:
|
|
1734
|
+
original_text = path.read_text(encoding="utf-8")
|
|
1735
|
+
except Exception as exc:
|
|
1736
|
+
result = {"status": "error", "content": f"Failed to read file: {path}\n{exc}"}
|
|
1737
|
+
logger.exception("edit_file failed: %s", path)
|
|
1738
|
+
logger.info("edit_file result: %s", result)
|
|
1739
|
+
return result
|
|
1740
|
+
|
|
1741
|
+
if old_string not in original_text:
|
|
1742
|
+
result = {"status": "error", "content": f"Target text not found in file: {path}"}
|
|
1743
|
+
logger.info("edit_file result: %s", result)
|
|
1744
|
+
return result
|
|
1745
|
+
|
|
1746
|
+
updated_text = original_text.replace(old_string, new_string, 1)
|
|
1747
|
+
path.write_text(updated_text, encoding="utf-8")
|
|
1748
|
+
result = {"status": "ok", "content": f"Updated: {path}"}
|
|
1749
|
+
logger.info("edit_file result: %s", result)
|
|
1750
|
+
return result
|
|
1751
|
+
|
|
1608
1752
|
if name == "execute_python_script":
|
|
1609
1753
|
script = args.get("script")
|
|
1610
1754
|
if not script:
|
|
@@ -1698,10 +1842,12 @@ def show_tool_result(tool_call: Any, result: dict[str, Any]) -> None:
|
|
|
1698
1842
|
)
|
|
1699
1843
|
|
|
1700
1844
|
|
|
1701
|
-
def chat_once(client: OpenAI, model: str, messages: list[dict[str, Any]], temperature: float, debug_enabled: bool = False) -> Any:
|
|
1845
|
+
def chat_once(client: OpenAI, model: str, messages: list[dict[str, Any]], temperature: float, debug_enabled: bool = False, disable_tools:bool = False) -> Any:
|
|
1702
1846
|
ensure_not_interrupted()
|
|
1703
1847
|
wait_for_model_call_interval()
|
|
1704
1848
|
tool_definitions = TOOL_DEFINITIONS + ACTIVE_MCP_TOOL_DEFINITIONS
|
|
1849
|
+
if disable_tools:
|
|
1850
|
+
tool_definitions = []
|
|
1705
1851
|
request_payload = {
|
|
1706
1852
|
"model": model,
|
|
1707
1853
|
"messages": messages,
|
|
@@ -1898,14 +2044,38 @@ def plan_user_request(client: OpenAI, model: str, history: list[dict[str, Any]],
|
|
|
1898
2044
|
if is_special_command(user_text):
|
|
1899
2045
|
return user_text
|
|
1900
2046
|
|
|
2047
|
+
planning_prompt = f"""
|
|
2048
|
+
你是一个编程行业需求分析专家,你能够通过历史上下文和用户的输入分析出用户的真实意图,能够拆分步骤,并且能够生成任务执行清单。
|
|
2049
|
+
|
|
2050
|
+
### 请严格按照以下格式输出任务清单:
|
|
2051
|
+
|
|
2052
|
+
用户原始指令:......
|
|
2053
|
+
|
|
2054
|
+
结合上下文得到用户的完整意图:.....
|
|
2055
|
+
|
|
2056
|
+
接下来按照这个步骤逐步执行完成任务:
|
|
2057
|
+
1、第一步:......
|
|
2058
|
+
2、第二步:......
|
|
2059
|
+
......
|
|
2060
|
+
|
|
2061
|
+
"""
|
|
2062
|
+
|
|
2063
|
+
user_prompt = f"""请结合历史上下文分析用户输入并生成任务清单。
|
|
2064
|
+
### 历史上下文:
|
|
2065
|
+
{json.dumps(history, ensure_ascii=False)}
|
|
2066
|
+
|
|
2067
|
+
### 用户输入:
|
|
2068
|
+
{user_text}
|
|
2069
|
+
"""
|
|
2070
|
+
|
|
1901
2071
|
planning_messages = [
|
|
1902
|
-
{"role": "system", "content":
|
|
1903
|
-
{"role": "user", "content":
|
|
2072
|
+
{"role": "system", "content": planning_prompt},
|
|
2073
|
+
{"role": "user", "content": user_prompt},
|
|
1904
2074
|
]
|
|
1905
|
-
if history:
|
|
1906
|
-
|
|
2075
|
+
# if history:
|
|
2076
|
+
# planning_messages.insert(1, {"role": "user", "content": "以下是历史上下文:\n" + json.dumps(history, ensure_ascii=False)})
|
|
1907
2077
|
|
|
1908
|
-
assistant_message = chat_once(client, model, planning_messages, temperature=0.2, debug_enabled=debug_enabled)
|
|
2078
|
+
assistant_message = chat_once(client, model, planning_messages, temperature=0.2, debug_enabled=debug_enabled, disable_tools=True)
|
|
1909
2079
|
return (assistant_message.content or "").strip() or user_text
|
|
1910
2080
|
|
|
1911
2081
|
|
|
@@ -2034,6 +2204,68 @@ def run_agent(client: OpenAI, model: str, system_prompt: str, session_store: Ses
|
|
|
2034
2204
|
# if recorder:
|
|
2035
2205
|
# recorder.record_error(traceback.format_exc())
|
|
2036
2206
|
|
|
2207
|
+
import json
|
|
2208
|
+
from typing import List, Union
|
|
2209
|
+
|
|
2210
|
+
def get_content_from_tool_calls(tool_calls: List) -> str:
|
|
2211
|
+
"""
|
|
2212
|
+
Extract the `content` field from a list of tool call objects
|
|
2213
|
+
(as returned by OpenAI's ChatCompletionMessage.tool_calls).
|
|
2214
|
+
|
|
2215
|
+
Args:
|
|
2216
|
+
tool_calls: List[ChatCompletionMessageFunctionToolCall] or similar.
|
|
2217
|
+
Each item should have a `function.arguments` attribute
|
|
2218
|
+
which is a JSON string containing a dict with a `content` key.
|
|
2219
|
+
|
|
2220
|
+
Returns:
|
|
2221
|
+
The extracted `content` string if found, otherwise an empty string ("").
|
|
2222
|
+
"""
|
|
2223
|
+
if not tool_calls:
|
|
2224
|
+
return ""
|
|
2225
|
+
|
|
2226
|
+
for tool_call in tool_calls:
|
|
2227
|
+
try:
|
|
2228
|
+
# 兼容 dict 形式(比如有些 mock / 测试数据)
|
|
2229
|
+
if isinstance(tool_call, dict):
|
|
2230
|
+
func = tool_call.get("function")
|
|
2231
|
+
if not func:
|
|
2232
|
+
continue
|
|
2233
|
+
# func 可能是 dict,也可能是有 arguments 属性的对象
|
|
2234
|
+
if isinstance(func, dict):
|
|
2235
|
+
arguments_str = func.get("arguments")
|
|
2236
|
+
else:
|
|
2237
|
+
arguments_str = getattr(func, "arguments", None)
|
|
2238
|
+
else:
|
|
2239
|
+
# OpenAI SDK 对象:ChatCompletionMessageFunctionToolCall
|
|
2240
|
+
func = getattr(tool_call, "function", None)
|
|
2241
|
+
if func is None:
|
|
2242
|
+
continue
|
|
2243
|
+
# Function.arguments 是一个 JSON 字符串
|
|
2244
|
+
arguments_str = getattr(func, "arguments", None)
|
|
2245
|
+
|
|
2246
|
+
if not arguments_str:
|
|
2247
|
+
continue
|
|
2248
|
+
|
|
2249
|
+
# arguments 本身就是 JSON 字符串,需要 parse
|
|
2250
|
+
if isinstance(arguments_str, str):
|
|
2251
|
+
arguments = json.loads(arguments_str)
|
|
2252
|
+
elif isinstance(arguments_str, dict):
|
|
2253
|
+
arguments = arguments_str
|
|
2254
|
+
else:
|
|
2255
|
+
# 其他类型直接放弃
|
|
2256
|
+
continue
|
|
2257
|
+
|
|
2258
|
+
# 真正取 content 字段
|
|
2259
|
+
if isinstance(arguments, dict) and "content" in arguments:
|
|
2260
|
+
return arguments["content"]
|
|
2261
|
+
|
|
2262
|
+
except (json.JSONDecodeError, TypeError, AttributeError):
|
|
2263
|
+
# 解析失败或结构异常,忽略这条 tool_call
|
|
2264
|
+
continue
|
|
2265
|
+
|
|
2266
|
+
return ""
|
|
2267
|
+
|
|
2268
|
+
|
|
2037
2269
|
|
|
2038
2270
|
def handle_task_end_command(md_path: Path, client: OpenAI, model: str, system_prompt: str, workdir: Path, debug_enabled: bool = False) -> None:
|
|
2039
2271
|
"""Process recent_conversations.md to find the most recent /task-start and use the
|
|
@@ -2082,22 +2314,34 @@ def handle_task_end_command(md_path: Path, client: OpenAI, model: str, system_pr
|
|
|
2082
2314
|
compiled_text = "\n\n".join(["## Round " + r for r in selected])
|
|
2083
2315
|
|
|
2084
2316
|
# Build messages for the model
|
|
2085
|
-
sys_prompt =
|
|
2086
|
-
|
|
2087
|
-
)
|
|
2317
|
+
sys_prompt = f"""
|
|
2318
|
+
你是提示词优化专家,负责将对话记录整理为更清晰、可执行的最终任务提示词。
|
|
2088
2319
|
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2320
|
+
### 生成最终提示词时,请严格遵循以下约束:
|
|
2321
|
+
1. 只输出最终改进后的提示词文本,不要添加任何解释、元信息或注释。
|
|
2322
|
+
2. 保留用户原始目标、关键上下文、澄清或变更后的要求、待执行步骤与预期产物。
|
|
2323
|
+
3. 语言必须清晰、可执行、步骤化,适合直接给后续执行器使用。
|
|
2324
|
+
4. 如果上下文中存在不确定信息,应显式写成“待确认”或“需验证”的约束项。
|
|
2325
|
+
5. 输出长度控制在 18000 字符以内。
|
|
2326
|
+
|
|
2327
|
+
"""
|
|
2328
|
+
|
|
2329
|
+
user_prompt = f"""分析对话记录帮我生成最终提示词,把最终提示词内容输出给我。
|
|
2330
|
+
### 对话记录
|
|
2331
|
+
{compiled_text}
|
|
2332
|
+
"""
|
|
2092
2333
|
|
|
2093
2334
|
try:
|
|
2094
|
-
assistant_message = chat_once(client, model, [{"role": "system", "content": sys_prompt}, {"role": "user", "content":
|
|
2335
|
+
assistant_message = chat_once(client, model, [{"role": "system", "content": sys_prompt}, {"role": "user", "content": user_prompt}], temperature=0.2, debug_enabled=debug_enabled, disable_tools=True)
|
|
2095
2336
|
except Exception:
|
|
2096
2337
|
logger.exception(t("task_end_generation_failed_log"))
|
|
2097
2338
|
console.print(Panel.fit(traceback.format_exc(), title=t("task_end_generate_failed")))
|
|
2098
2339
|
return
|
|
2099
|
-
|
|
2340
|
+
prompt_call_tools = get_content_from_tool_calls(assistant_message.tool_calls)
|
|
2341
|
+
# console.print(f"{prompt_call_tools}")
|
|
2100
2342
|
final_prompt = (assistant_message.content or "").strip()
|
|
2343
|
+
if not final_prompt:
|
|
2344
|
+
final_prompt = prompt_call_tools
|
|
2101
2345
|
if not final_prompt:
|
|
2102
2346
|
console.print(Panel.fit(t("task_end_no_prompt"), title=t("task_end_result")))
|
|
2103
2347
|
return
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "prompt-copilot-cli"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.8"
|
|
8
8
|
description = "A lightweight terminal-based coding agent with file tools, command execution, multimodal image support, and MCP integration."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import contextlib
|
|
2
2
|
import io
|
|
3
3
|
import sys
|
|
4
|
+
import tempfile
|
|
4
5
|
import types
|
|
5
6
|
import unittest
|
|
6
7
|
from pathlib import Path
|
|
@@ -102,6 +103,42 @@ class CLITests(unittest.TestCase):
|
|
|
102
103
|
self.assertEqual(captured["messages"][0]["role"], "system")
|
|
103
104
|
self.assertEqual(captured["messages"][-1]["content"], "帮我修复这个 bug")
|
|
104
105
|
|
|
106
|
+
def test_task_end_command_uses_aligned_working_principles(self) -> None:
|
|
107
|
+
captured: dict[str, object] = {}
|
|
108
|
+
|
|
109
|
+
def fake_chat_once(client, model, messages, temperature, debug_enabled=False):
|
|
110
|
+
captured["messages"] = messages
|
|
111
|
+
return types.SimpleNamespace(content="final prompt")
|
|
112
|
+
|
|
113
|
+
original_chat_once = main.chat_once
|
|
114
|
+
original_lang = main.UI_SYSTEM_LANGUAGE
|
|
115
|
+
main.UI_SYSTEM_LANGUAGE = "zh"
|
|
116
|
+
main.chat_once = fake_chat_once
|
|
117
|
+
try:
|
|
118
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
|
119
|
+
md_path = Path(temp_dir) / "recent_conversations.md"
|
|
120
|
+
md_path.write_text(
|
|
121
|
+
"# Recent conversations\n\n## Round 1\n\n**User:** /task-start\n\nhello\n\n**Assistant:** hi",
|
|
122
|
+
encoding="utf-8",
|
|
123
|
+
)
|
|
124
|
+
main.handle_task_end_command(
|
|
125
|
+
md_path=md_path,
|
|
126
|
+
client=object(),
|
|
127
|
+
model="gpt-4o-mini",
|
|
128
|
+
system_prompt="ignored",
|
|
129
|
+
workdir=Path(temp_dir),
|
|
130
|
+
debug_enabled=False,
|
|
131
|
+
)
|
|
132
|
+
finally:
|
|
133
|
+
main.UI_SYSTEM_LANGUAGE = original_lang
|
|
134
|
+
main.chat_once = original_chat_once
|
|
135
|
+
|
|
136
|
+
self.assertIn("messages", captured)
|
|
137
|
+
system_prompt = captured["messages"][0]["content"]
|
|
138
|
+
self.assertIn("先检查工作目录并理解需求", system_prompt)
|
|
139
|
+
self.assertIn("生成最终提示词时,请严格遵循以下约束", system_prompt)
|
|
140
|
+
self.assertIn("\n\n", system_prompt)
|
|
141
|
+
|
|
105
142
|
def test_version_flag_prints_application_version_and_exits(self) -> None:
|
|
106
143
|
parser = main.build_cli_parser()
|
|
107
144
|
output = io.StringIO()
|
|
@@ -87,6 +87,34 @@ class FileToolTests(unittest.TestCase):
|
|
|
87
87
|
self.assertTrue(dest.exists())
|
|
88
88
|
self.assertEqual(dest.read_text(encoding="utf-8"), "hello")
|
|
89
89
|
|
|
90
|
+
def test_search_code_tool_finds_pattern_in_file(self) -> None:
|
|
91
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
92
|
+
root = Path(tmpdir)
|
|
93
|
+
target = root / "demo.py"
|
|
94
|
+
target.write_text("def greet():\n return 'hello'\n", encoding="utf-8")
|
|
95
|
+
|
|
96
|
+
result = main.execute_tool_call(DummyToolCall("search_code", {"path": str(root), "pattern": "return 'hello'"}))
|
|
97
|
+
|
|
98
|
+
self.assertEqual(result["status"], "ok")
|
|
99
|
+
matches = json.loads(result["content"])
|
|
100
|
+
self.assertTrue(matches)
|
|
101
|
+
self.assertEqual(matches[0]["path"], str(target))
|
|
102
|
+
|
|
103
|
+
def test_edit_file_tool_replaces_text(self) -> None:
|
|
104
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
105
|
+
target = Path(tmpdir) / "demo.py"
|
|
106
|
+
target.write_text("print('before')\n", encoding="utf-8")
|
|
107
|
+
|
|
108
|
+
result = main.execute_tool_call(
|
|
109
|
+
DummyToolCall(
|
|
110
|
+
"edit_file",
|
|
111
|
+
{"path": str(target), "old_string": "before", "new_string": "after"},
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
self.assertEqual(result["status"], "ok")
|
|
116
|
+
self.assertEqual(target.read_text(encoding="utf-8"), "print('after')\n")
|
|
117
|
+
|
|
90
118
|
|
|
91
119
|
if __name__ == "__main__":
|
|
92
120
|
unittest.main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/entry_points.txt
RENAMED
|
File without changes
|
{prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/requires.txt
RENAMED
|
File without changes
|
{prompt_copilot_cli-0.2.3 → prompt_copilot_cli-0.2.8}/prompt_copilot_cli.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|