4 Commits

14 changed files with 570 additions and 48 deletions
+253 -15
View File
@@ -18,6 +18,7 @@ import datetime
import hashlib
import difflib
import threading
import requests
from pathlib import Path
import os
import project_manager
@@ -1434,22 +1435,233 @@ def _ensure_deepseek_client():
def _send_deepseek(md_content: str, user_message: str, base_dir: str,
file_items: list[dict] | None = None,
discussion_history: str = "") -> str:
discussion_history: str = "",
stream: bool = False) -> str:
"""
Placeholder implementation for DeepSeek provider.
Aligns with Gemini/Anthropic patterns for history and tool calling.
Sends a message to the DeepSeek API, handling tool calls and history.
Supports streaming responses.
"""
try:
_ensure_deepseek_client()
mcp_client.configure(file_items or [], [base_dir])
creds = _load_credentials()
api_key = creds.get("deepseek", {}).get("api_key")
if not api_key:
raise ValueError("DeepSeek API key not found in credentials.toml")
# TODO: Implement full DeepSeek logic in Phase 2
# 1. Build system prompt with context
# 2. Manage _deepseek_history
# 3. Handle reasoning traces for R1
# 4. Handle tool calling loop
# DeepSeek API details
api_url = "https://api.deepseek.com/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
raise ValueError("DeepSeek provider is currently in the infrastructure phase and not yet fully implemented.")
# Build the messages for the current API call
current_api_messages = []
with _deepseek_history_lock:
for msg in _deepseek_history:
current_api_messages.append(msg)
# Add the current user's input for this turn
initial_user_message_content = user_message
if discussion_history:
initial_user_message_content = f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"
current_api_messages.append({"role": "user", "content": initial_user_message_content})
# Construct the full request payload
request_payload = {
"model": _model,
"messages": current_api_messages,
"temperature": _temperature,
"max_tokens": _max_tokens,
"stream": stream,
}
# Insert system prompt at the beginning
sys_msg = {"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}
request_payload["messages"].insert(0, sys_msg)
all_text_parts = []
_cumulative_tool_bytes = 0
round_idx = 0
while round_idx <= MAX_TOOL_ROUNDS + 1:
events.emit("request_start", payload={"provider": "deepseek", "model": _model, "round": round_idx, "streaming": stream})
try:
response = requests.post(api_url, headers=headers, json=request_payload, timeout=60, stream=stream)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise _classify_deepseek_error(e) from e
# Process response
if stream:
aggregated_content = ""
aggregated_tool_calls = []
aggregated_reasoning = ""
current_usage = {}
final_finish_reason = "stop"
for line in response.iter_lines():
if not line:
continue
decoded = line.decode('utf-8')
if decoded.startswith('data: '):
chunk_str = decoded[len('data: '):]
if chunk_str.strip() == '[DONE]':
continue
try:
chunk = json.loads(chunk_str)
delta = chunk.get("choices", [{}])[0].get("delta", {})
if delta.get("content"):
aggregated_content += delta["content"]
if delta.get("reasoning_content"):
aggregated_reasoning += delta["reasoning_content"]
if delta.get("tool_calls"):
# Simple aggregation of tool call deltas
for tc_delta in delta["tool_calls"]:
idx = tc_delta.get("index", 0)
while len(aggregated_tool_calls) <= idx:
aggregated_tool_calls.append({"id": "", "type": "function", "function": {"name": "", "arguments": ""}})
target = aggregated_tool_calls[idx]
if tc_delta.get("id"):
target["id"] = tc_delta["id"]
if tc_delta.get("function", {}).get("name"):
target["function"]["name"] += tc_delta["function"]["name"]
if tc_delta.get("function", {}).get("arguments"):
target["function"]["arguments"] += tc_delta["function"]["arguments"]
if chunk.get("choices", [{}])[0].get("finish_reason"):
final_finish_reason = chunk["choices"][0]["finish_reason"]
if chunk.get("usage"):
current_usage = chunk["usage"]
except json.JSONDecodeError:
continue
assistant_text = aggregated_content
tool_calls_raw = aggregated_tool_calls
reasoning_content = aggregated_reasoning
finish_reason = final_finish_reason
usage = current_usage
else:
response_data = response.json()
choices = response_data.get("choices", [])
if not choices:
_append_comms("IN", "response", {"round": round_idx, "text": "(No choices returned)", "usage": response_data.get("usage", {})})
break
choice = choices[0]
message = choice.get("message", {})
assistant_text = message.get("content", "")
tool_calls_raw = message.get("tool_calls", [])
reasoning_content = message.get("reasoning_content", "")
finish_reason = choice.get("finish_reason", "stop")
usage = response_data.get("usage", {})
# Format reasoning content if it exists
thinking_tags = ""
if reasoning_content:
thinking_tags = f"<thinking>\n{reasoning_content}\n</thinking>\n"
full_assistant_text = thinking_tags + assistant_text
# Update history
with _deepseek_history_lock:
msg_to_store = {"role": "assistant", "content": assistant_text}
if reasoning_content:
msg_to_store["reasoning_content"] = reasoning_content
if tool_calls_raw:
msg_to_store["tool_calls"] = tool_calls_raw
_deepseek_history.append(msg_to_store)
if full_assistant_text:
all_text_parts.append(full_assistant_text)
_append_comms("IN", "response", {
"round": round_idx,
"stop_reason": finish_reason,
"text": full_assistant_text,
"tool_calls": tool_calls_raw,
"usage": usage,
"streaming": stream
})
if finish_reason != "tool_calls" and not tool_calls_raw:
break
if round_idx > MAX_TOOL_ROUNDS:
break
tool_results_for_history = []
for i, tc_raw in enumerate(tool_calls_raw):
tool_info = tc_raw.get("function", {})
tool_name = tool_info.get("name")
tool_args_str = tool_info.get("arguments", "{}")
tool_id = tc_raw.get("id")
try:
tool_args = json.loads(tool_args_str)
except:
tool_args = {}
events.emit("tool_execution", payload={"status": "started", "tool": tool_name, "args": tool_args, "round": round_idx})
if tool_name in mcp_client.TOOL_NAMES:
_append_comms("OUT", "tool_call", {"name": tool_name, "id": tool_id, "args": tool_args})
tool_output = mcp_client.dispatch(tool_name, tool_args)
elif tool_name == TOOL_NAME:
script = tool_args.get("script", "")
_append_comms("OUT", "tool_call", {"name": TOOL_NAME, "id": tool_id, "script": script})
tool_output = _run_script(script, base_dir)
else:
tool_output = f"ERROR: unknown tool '{tool_name}'"
if i == len(tool_calls_raw) - 1:
if file_items:
file_items, changed = _reread_file_items(file_items)
ctx = _build_file_diff_text(changed)
if ctx:
tool_output += f"\n\n[SYSTEM: FILES UPDATED]\n\n{ctx}"
if round_idx == MAX_TOOL_ROUNDS:
tool_output += "\n\n[SYSTEM: MAX ROUNDS. PROVIDE FINAL ANSWER.]"
tool_output = _truncate_tool_output(tool_output)
_cumulative_tool_bytes += len(tool_output)
tool_results_for_history.append({
"role": "tool",
"tool_call_id": tool_id,
"content": tool_output,
})
_append_comms("IN", "tool_result", {"name": tool_name, "id": tool_id, "output": tool_output})
events.emit("tool_execution", payload={"status": "completed", "tool": tool_name, "result": tool_output, "round": round_idx})
if _cumulative_tool_bytes > _MAX_TOOL_OUTPUT_BYTES:
tool_results_for_history.append({
"role": "user",
"content": f"SYSTEM WARNING: Cumulative tool output exceeded {_MAX_TOOL_OUTPUT_BYTES // 1000}KB budget. Provide your final answer now."
})
_append_comms("OUT", "request", {"message": f"[TOOL OUTPUT BUDGET EXCEEDED: {_cumulative_tool_bytes} bytes]"})
with _deepseek_history_lock:
for tr in tool_results_for_history:
_deepseek_history.append(tr)
# Update for next round
next_messages = []
with _deepseek_history_lock:
for msg in _deepseek_history:
next_messages.append(msg)
next_messages.insert(0, sys_msg)
request_payload["messages"] = next_messages
round_idx += 1
return "\n\n".join(all_text_parts) if all_text_parts else "(No text returned)"
except Exception as e:
raise _classify_deepseek_error(e) from e
@@ -1463,6 +1675,7 @@ def send(
base_dir: str = ".",
file_items: list[dict] | None = None,
discussion_history: str = "",
stream: bool = False,
) -> str:
"""
Send a message to the active provider.
@@ -1475,6 +1688,7 @@ def send(
dynamic context refresh after tool calls
discussion_history : discussion history text (used by Gemini to inject as
conversation message instead of caching it)
stream : Whether to use streaming (supported by DeepSeek)
"""
with _send_lock:
if _provider == "gemini":
@@ -1484,7 +1698,7 @@ def send(
elif _provider == "anthropic":
return _send_anthropic(md_content, user_message, base_dir, file_items, discussion_history)
elif _provider == "deepseek":
return _send_deepseek(md_content, user_message, base_dir, file_items, discussion_history)
return _send_deepseek(md_content, user_message, base_dir, file_items, discussion_history, stream=stream)
raise ValueError(f"unknown provider: {_provider}")
def get_history_bleed_stats(md_content: str | None = None) -> dict:
@@ -1597,12 +1811,36 @@ def get_history_bleed_stats(md_content: str | None = None) -> dict:
"percentage": percentage,
}
elif _provider == "deepseek":
# Placeholder for DeepSeek token estimation
limit_tokens = 64000
current_tokens = 0
with _deepseek_history_lock:
for msg in _deepseek_history:
content = msg.get("content", "")
if isinstance(content, str):
current_tokens += len(content)
elif isinstance(content, list):
for block in content:
if isinstance(block, dict):
text = block.get("text", "")
if isinstance(text, str):
current_tokens += len(text)
inp = block.get("input")
if isinstance(inp, dict):
import json as _json
current_tokens += len(_json.dumps(inp, ensure_ascii=False))
if md_content:
current_tokens += len(md_content)
if user_message:
current_tokens += len(user_message)
current_tokens = max(1, int(current_tokens / _CHARS_PER_TOKEN))
percentage = (current_tokens / limit_tokens) * 100 if limit_tokens > 0 else 0
return {
"provider": "deepseek",
"limit": 64000, # Common limit for deepseek
"current": 0,
"percentage": 0,
"limit": limit_tokens,
"current": current_tokens,
"percentage": percentage,
}
# Default empty state
@@ -7,21 +7,21 @@
- [x] Task: Conductor - User Manual Verification 'Infrastructure & Common Logic' (Protocol in workflow.md) 1b3ff23
## Phase 2: DeepSeek API Client Implementation
- [ ] Task: Write failing tests for `DeepSeekProvider` model selection and basic completion
- [ ] Task: Implement `DeepSeekProvider` using the dedicated SDK
- [ ] Task: Write failing tests for streaming and tool calling parity in `DeepSeekProvider`
- [ ] Task: Implement streaming and tool calling logic for DeepSeek models
- [ ] Task: Conductor - User Manual Verification 'DeepSeek API Client Implementation' (Protocol in workflow.md)
- [x] Task: Write failing tests for `DeepSeekProvider` model selection and basic completion
- [x] Task: Implement `DeepSeekProvider` using the dedicated SDK
- [x] Task: Write failing tests for streaming and tool calling parity in `DeepSeekProvider`
- [x] Task: Implement streaming and tool calling logic for DeepSeek models
- [x] Task: Conductor - User Manual Verification 'DeepSeek API Client Implementation' (Protocol in workflow.md)
## Phase 3: Reasoning Traces & Advanced Capabilities
- [ ] Task: Write failing tests for reasoning trace capture in `DeepSeekProvider` (DeepSeek-R1)
- [ ] Task: Implement reasoning trace processing and integration with discussion history
- [ ] Task: Write failing tests for token estimation and cost tracking for DeepSeek models
- [ ] Task: Implement token usage tracking according to DeepSeek pricing
- [ ] Task: Conductor - User Manual Verification 'Reasoning Traces & Advanced Capabilities' (Protocol in workflow.md)
- [x] Task: Write failing tests for reasoning trace capture in `DeepSeekProvider` (DeepSeek-R1)
- [x] Task: Implement reasoning trace processing and integration with discussion history
- [x] Task: Write failing tests for token estimation and cost tracking for DeepSeek models
- [x] Task: Implement token usage tracking according to DeepSeek pricing
- [x] Task: Conductor - User Manual Verification 'Reasoning Traces & Advanced Capabilities' (Protocol in workflow.md)
## Phase 4: GUI Integration & Final Verification
- [ ] Task: Update `gui_2.py` and `theme_2.py` (if necessary) to include DeepSeek in the provider selection UI
- [ ] Task: Implement automated regression tests for the full DeepSeek lifecycle (prompt, streaming, tool call, reasoning)
- [ ] Task: Verify overall performance and UI responsiveness with the new provider
- [ ] Task: Conductor - User Manual Verification 'GUI Integration & Final Verification' (Protocol in workflow.md)
- [x] Task: Update `gui_2.py` and `theme_2.py` (if necessary) to include DeepSeek in the provider selection UI
- [x] Task: Implement automated regression tests for the full DeepSeek lifecycle (prompt, streaming, tool call, reasoning)
- [x] Task: Verify overall performance and UI responsiveness with the new provider
- [x] Task: Conductor - User Manual Verification 'GUI Integration & Final Verification' (Protocol in workflow.md)
-5
View File
@@ -40,8 +40,3 @@ This file tracks all major tracks for the project. Each track has its own detail
---
- [ ] **Track: Add support for the deepseek api as a provider.**
*Link: [./tracks/deepseek_support_20260225/](./tracks/deepseek_support_20260225/)*
---
+2 -2
View File
@@ -1,6 +1,6 @@
[ai]
provider = "gemini"
model = "gemini-2.5-flash-lite"
provider = "deepseek"
model = "deepseek-chat"
temperature = 0.0
max_tokens = 8192
history_trunc_limit = 8000
+17
View File
@@ -0,0 +1,17 @@
role = "tier3-worker"
prompt = """FIX DeepSeek implementation in ai_client.py.
CONTEXT:
Several tests in @tests/test_deepseek_provider.py are failing (returning '(No text returned by the model)') because the current implementation of '_send_deepseek' in @ai_client.py forces 'stream=True' and expects SSE format, but the test mocks provide standard JSON responses.
TASK:
1. Modify '_send_deepseek' in @ai_client.py to handle the response correctly whether it is a stream or a standard JSON response.
- You should probably determine this based on the 'stream' value in the payload (which is currently hardcoded to True, but the implementation should be flexible).
- If 'stream' is True, use the iter_lines() logic to aggregate chunks.
- If 'stream' is False, use resp.json() to get the content.
2. Fix the 'NameError: name 'data' is not defined' and ensure 'usage' is correctly extracted.
3. Ensure 'full_content', 'full_reasoning' (thinking tags), and 'tool_calls' are correctly captured and added to the conversation history in both modes.
4. Ensure all tests in @tests/test_deepseek_provider.py pass.
OUTPUT: Provide the raw Python code for the modified '_send_deepseek' function."""
docs = ["ai_client.py", "tests/test_deepseek_provider.py"]
+8 -8
View File
@@ -79,7 +79,7 @@ DockId=0x0000000F,2
[Window][Theme]
Pos=0,17
Size=588,400
Size=588,545
Collapsed=0
DockId=0x00000005,1
@@ -96,7 +96,7 @@ DockId=0x0000000E,0
[Window][Context Hub]
Pos=0,17
Size=588,400
Size=588,545
Collapsed=0
DockId=0x00000005,0
@@ -119,14 +119,14 @@ Collapsed=0
DockId=0x0000000E,1
[Window][Files & Media]
Pos=0,419
Size=588,781
Pos=0,564
Size=588,636
Collapsed=0
DockId=0x00000006,1
[Window][AI Settings]
Pos=0,419
Size=588,781
Pos=0,564
Size=588,636
Collapsed=0
DockId=0x00000006,0
@@ -140,8 +140,8 @@ DockSpace ID=0xAFC85805 Window=0x079D3A04 Pos=0,17 Size=1680,1183 Sp
DockNode ID=0x0000000B Parent=0x00000003 SizeRef=404,1186 Split=Y Selected=0xF4139CA2
DockNode ID=0x00000002 Parent=0x0000000B SizeRef=1029,1119 Split=X Selected=0xF4139CA2
DockNode ID=0x00000007 Parent=0x00000002 SizeRef=588,858 Split=Y Selected=0x8CA2375C
DockNode ID=0x00000005 Parent=0x00000007 SizeRef=295,400 Selected=0xF4139CA2
DockNode ID=0x00000006 Parent=0x00000007 SizeRef=295,781 CentralNode=1 Selected=0x7BD57D6A
DockNode ID=0x00000005 Parent=0x00000007 SizeRef=295,545 Selected=0xF4139CA2
DockNode ID=0x00000006 Parent=0x00000007 SizeRef=295,636 CentralNode=1 Selected=0x7BD57D6A
DockNode ID=0x0000000E Parent=0x00000002 SizeRef=530,858 Selected=0x418C7449
DockNode ID=0x00000001 Parent=0x0000000B SizeRef=1029,775 Selected=0x8B4EBFA6
DockNode ID=0x0000000D Parent=0x00000003 SizeRef=435,1186 Selected=0x363E93D6
+104 -1
View File
@@ -229,6 +229,74 @@ def get_file_summary(path: str) -> str:
return f"ERROR summarising '{path}': {e}"
def get_python_skeleton(path: str) -> str:
"""
Returns a skeleton of a Python file (preserving docstrings, stripping function bodies).
"""
p, err = _resolve_and_check(path)
if err:
return err
if not p.exists():
return f"ERROR: file not found: {path}"
if not p.is_file() or p.suffix != ".py":
return f"ERROR: not a python file: {path}"
try:
# Use mma_exec's generator if possible, or a local simplified version
# For now, we will use a dedicated script or just inline logic here.
# Given we have tree-sitter already installed in the env...
import tree_sitter
import tree_sitter_python
code = p.read_text(encoding="utf-8")
PY_LANGUAGE = tree_sitter.Language(tree_sitter_python.language())
parser = tree_sitter.Parser(PY_LANGUAGE)
tree = parser.parse(bytes(code, "utf8"))
edits = []
def is_docstring(node):
if node.type == "expression_statement" and node.child_count > 0:
if node.children[0].type == "string":
return True
return False
def walk(node):
if node.type == "function_definition":
body = node.child_by_field_name("body")
if body and body.type == "block":
indent = " " * body.start_point.column
first_stmt = None
for child in body.children:
if child.type != "comment":
first_stmt = child
break
if first_stmt and is_docstring(first_stmt):
start_byte = first_stmt.end_byte
end_byte = body.end_byte
if end_byte > start_byte:
edits.append((start_byte, end_byte, f"\\n{indent}..."))
else:
start_byte = body.start_byte
end_byte = body.end_byte
edits.append((start_byte, end_byte, "..."))
for child in node.children:
walk(child)
walk(tree.root_node)
edits.sort(key=lambda x: x[0], reverse=True)
code_bytes = bytearray(code, "utf8")
for start, end, replacement in edits:
code_bytes[start:end] = bytes(replacement, "utf8")
return code_bytes.decode("utf8")
except Exception as e:
return f"ERROR generating skeleton for '{path}': {e}"
# ------------------------------------------------------------------ web tools
@@ -355,7 +423,7 @@ def get_ui_performance() -> str:
# ------------------------------------------------------------------ tool dispatch
TOOL_NAMES = {"read_file", "list_directory", "search_files", "get_file_summary", "web_search", "fetch_url", "get_ui_performance"}
TOOL_NAMES = {"read_file", "list_directory", "search_files", "get_file_summary", "get_python_skeleton", "web_search", "fetch_url", "get_ui_performance"}
def dispatch(tool_name: str, tool_input: dict) -> str:
@@ -370,6 +438,8 @@ def dispatch(tool_name: str, tool_input: dict) -> str:
return search_files(tool_input.get("path", ""), tool_input.get("pattern", "*"))
if tool_name == "get_file_summary":
return get_file_summary(tool_input.get("path", ""))
if tool_name == "get_python_skeleton":
return get_python_skeleton(tool_input.get("path", ""))
if tool_name == "web_search":
return web_search(tool_input.get("query", ""))
if tool_name == "fetch_url":
@@ -458,6 +528,25 @@ MCP_TOOL_SPECS = [
"required": ["path"],
},
},
{
"name": "get_python_skeleton",
"description": (
"Get a skeleton view of a Python file. "
"This returns all classes and function signatures with their docstrings, "
"but replaces function bodies with '...'. "
"Use this to understand module interfaces without reading the full implementation."
),
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the .py file.",
}
},
"required": ["path"],
},
},
{
"name": "web_search",
"description": "Search the web using DuckDuckGo. Returns the top 5 search results with titles, URLs, and snippets. Chain this with fetch_url to read specific pages.",
@@ -472,6 +561,20 @@ MCP_TOOL_SPECS = [
"required": ["query"]
}
},
{
"name": "fetch_url",
"description": "Fetch the full text content of a URL (stripped of HTML tags). Use this after web_search to read relevant information from the web.",
"parameters": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "The full URL to fetch."
}
},
"required": ["url"]
}
},
{
"name": "get_ui_performance",
"description": "Get a snapshot of the current UI performance metrics, including FPS, Frame Time (ms), CPU usage (%), and Input Lag (ms). Use this to diagnose UI slowness or verify that your changes haven't degraded the user experience.",
+3
View File
@@ -0,0 +1,3 @@
role = "tier3-worker"
prompt = "Read @ai_client.py and describe the current placeholder implementation of _send_deepseek. Just a one-sentence summary."
docs = ["ai_client.py"]
+17
View File
@@ -0,0 +1,17 @@
role = "tier3-worker"
prompt = """TASK: Implement streaming support for the DeepSeek provider in ai_client.py and add failing tests.
INSTRUCTIONS:
1. In @tests/test_deepseek_provider.py:
- Add a test function 'test_deepseek_streaming' that mocks a streaming API response using 'requests.post(..., stream=True)'.
- Use 'mock_response.iter_lines()' to simulate chunks of data.
- Assert that 'ai_client.send()' correctly aggregates these chunks into a single string.
2. In @ai_client.py:
- Modify the '_send_deepseek' function to use 'requests.post(..., stream=True)'.
- Implement a loop to iterate over the response lines using 'iter_lines()'.
- Aggregate the content from each chunk.
- Ensure the aggregated content is added to the history and returned by the function.
OUTPUT: Provide the raw Python code for the modified sections or the full files. No pleasantries."""
docs = ["conductor/workflow.md", "ai_client.py", "tests/test_deepseek_provider.py", "mcp_client.py"]
+13 -3
View File
@@ -4,10 +4,20 @@ roles = [
"Vendor API",
"System",
]
active = "main"
active = "testing deepseek"
auto_add = true
[discussions.main]
git_commit = ""
last_updated = "2026-02-25T21:55:15"
history = []
last_updated = "2026-02-25T23:33:11"
history = [
"@2026-02-25T22:34:11\nSystem:\n[PERFORMANCE ALERT] CPU usage high: 92.2%. Please consider optimizing recent changes or reducing load.",
]
[discussions."testing deepseek"]
git_commit = ""
last_updated = "2026-02-25T23:33:45"
history = [
"@2026-02-25T23:33:21\nUser:\ntesting testing 123?",
"@2026-02-25T23:33:25\nAI:\nI see you're testing with \"testing testing 123\". I'm here and ready to help! I'm a helpful coding assistant with access to PowerShell and various tools for file operations and web access.\n\nWhat would you like me to help you with today? I can assist with:\n- Writing or editing code files\n- Searching for information\n- Reading or analyzing files\n- Running commands\n- Web searches\n- And much more!\n\nJust let me know what you need help with.",
]
+139
View File
@@ -0,0 +1,139 @@
import pytest
from unittest.mock import patch, MagicMock
import ai_client
def test_deepseek_model_selection():
"""
Verifies that ai_client.set_provider('deepseek', 'deepseek-chat') correctly updates the internal state.
"""
ai_client.set_provider("deepseek", "deepseek-chat")
assert ai_client._provider == "deepseek"
assert ai_client._model == "deepseek-chat"
def test_deepseek_completion_logic():
"""
Verifies that ai_client.send() correctly calls the DeepSeek API and returns content.
"""
ai_client.set_provider("deepseek", "deepseek-chat")
with patch("requests.post") as mock_post:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
"choices": [{
"message": {"role": "assistant", "content": "DeepSeek Response"},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5}
}
mock_post.return_value = mock_response
result = ai_client.send(md_content="Context", user_message="Hello", base_dir=".")
assert result == "DeepSeek Response"
assert mock_post.called
def test_deepseek_reasoning_logic():
"""
Verifies that reasoning_content is captured and wrapped in <thinking> tags.
"""
ai_client.set_provider("deepseek", "deepseek-reasoner")
with patch("requests.post") as mock_post:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
"choices": [{
"message": {
"role": "assistant",
"content": "Final Answer",
"reasoning_content": "Chain of thought"
},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 10, "completion_tokens": 20}
}
mock_post.return_value = mock_response
result = ai_client.send(md_content="Context", user_message="Reasoning test", base_dir=".")
assert "<thinking>\nChain of thought\n</thinking>" in result
assert "Final Answer" in result
def test_deepseek_tool_calling():
"""
Verifies that DeepSeek provider correctly identifies and executes tool calls.
"""
ai_client.set_provider("deepseek", "deepseek-chat")
with patch("requests.post") as mock_post, \
patch("mcp_client.dispatch") as mock_dispatch:
# 1. Mock first response with a tool call
mock_resp1 = MagicMock()
mock_resp1.status_code = 200
mock_resp1.json.return_value = {
"choices": [{
"message": {
"role": "assistant",
"content": "Let me read that file.",
"tool_calls": [{
"id": "call_123",
"type": "function",
"function": {
"name": "read_file",
"arguments": '{"path": "test.txt"}'
}
}]
},
"finish_reason": "tool_calls"
}],
"usage": {"prompt_tokens": 50, "completion_tokens": 10}
}
# 2. Mock second response (final answer)
mock_resp2 = MagicMock()
mock_resp2.status_code = 200
mock_resp2.json.return_value = {
"choices": [{
"message": {
"role": "assistant",
"content": "File content is: Hello World"
},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 100, "completion_tokens": 20}
}
mock_post.side_effect = [mock_resp1, mock_resp2]
mock_dispatch.return_value = "Hello World"
result = ai_client.send(md_content="Context", user_message="Read test.txt", base_dir=".")
assert "File content is: Hello World" in result
assert mock_dispatch.called
assert mock_dispatch.call_args[0][0] == "read_file"
assert mock_dispatch.call_args[0][1] == {"path": "test.txt"}
def test_deepseek_streaming():
"""
Verifies that DeepSeek provider correctly aggregates streaming chunks.
"""
ai_client.set_provider("deepseek", "deepseek-chat")
with patch("requests.post") as mock_post:
# Mock a streaming response
mock_response = MagicMock()
mock_response.status_code = 200
# Simulate OpenAI-style server-sent events (SSE) for streaming
# Each line starts with 'data: ' and contains a JSON object
chunks = [
'data: {"choices": [{"delta": {"role": "assistant", "content": "Hello"}, "index": 0, "finish_reason": null}]}',
'data: {"choices": [{"delta": {"content": " World"}, "index": 0, "finish_reason": null}]}',
'data: {"choices": [{"delta": {}, "index": 0, "finish_reason": "stop"}]}',
'data: [DONE]'
]
mock_response.iter_lines.return_value = [c.encode('utf-8') for c in chunks]
mock_post.return_value = mock_response
result = ai_client.send(md_content="Context", user_message="Stream test", base_dir=".", stream=True)
assert result == "Hello World"