Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/deepseek_cursor_proxy/reasoning_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def tool_call_ids(message: dict[str, Any]) -> list[str]:
return ids


def tool_call_names(message: dict[str, Any]) -> list[str]:
names: list[str] = []
for tool_call in message.get("tool_calls") or []:
if not isinstance(tool_call, dict):
continue
function = tool_call.get("function")
if isinstance(function, dict) and function.get("name"):
names.append(str(function["name"]))
return names


def message_signature(message: dict[str, Any]) -> str:
tool_calls = [
normalize_tool_call(tool_call)
Expand Down Expand Up @@ -125,6 +136,13 @@ def scoped_reasoning_keys(message: dict[str, Any], scope: str) -> list[str]:
for tool_call in (message.get("tool_calls") or [])
if isinstance(tool_call, dict)
)
# Recovery-of-last-resort key. Catches the case where a streaming response
# was interrupted (user pressed Stop) before the tool_call.id chunk arrived,
# so neither tool_call_id nor tool_call_signature (which canonicalizes
# arguments) survives the round-trip through Cursor's transcript.
keys.extend(
f"scope:{scope}:tool_name:{tool_name}" for tool_name in tool_call_names(message)
)
return keys


Expand Down Expand Up @@ -152,6 +170,10 @@ def portable_reasoning_keys(
for tool_call in (message.get("tool_calls") or [])
if isinstance(tool_call, dict)
)
keys.extend(
f"namespace:{cache_namespace}:turn:{turn_signature}:" f"tool_name:{tool_name}"
for tool_name in tool_call_names(message)
)
return keys


Expand Down
102 changes: 56 additions & 46 deletions src/deepseek_cursor_proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,54 +679,64 @@ def _proxy_streaming_response(
)
finalized = False
pending_recovery_notice = recovery_notice
while True:
try:
line = response.readline()
except (HTTPException, OSError) as exc:
LOG.warning("upstream streaming response read failed: %s", exc)
return ProxyResponseResult(False, usage)
if not line:
break
(
rewritten,
finalized,
pending_recovery_notice,
chunk_usage,
) = self._rewrite_sse_line(
line,
original_model,
accumulator,
cache_namespace,
response_contexts,
display_adapter,
pending_recovery_notice,
trace,
)
if chunk_usage is not None:
usage = chunk_usage
if trace is not None:
trace.record_stream_chunk(line, rewritten)
if not self._write_to_client(
rewritten, "sending streaming response chunk", flush=True
):
return ProxyResponseResult(False, usage)
if finalized:
break

if not finalized:
if self.config.verbose:
log_json("model streaming assistant messages", accumulator.messages())
stored = sum(
accumulator.store_reasoning(
self.reasoning_store,
scope,
try:
while True:
try:
line = response.readline()
except (HTTPException, OSError) as exc:
LOG.warning("upstream streaming response read failed: %s", exc)
return ProxyResponseResult(False, usage)
if not line:
break
(
rewritten,
finalized,
pending_recovery_notice,
chunk_usage,
) = self._rewrite_sse_line(
line,
original_model,
accumulator,
cache_namespace,
prior_messages,
response_contexts,
display_adapter,
pending_recovery_notice,
trace,
)
for scope, prior_messages in response_contexts
)
if self.config.verbose and stored:
LOG.info("stored %s streaming reasoning cache key(s)", stored)
if chunk_usage is not None:
usage = chunk_usage
if trace is not None:
trace.record_stream_chunk(line, rewritten)
if not self._write_to_client(
rewritten, "sending streaming response chunk", flush=True
):
return ProxyResponseResult(False, usage)
if finalized:
break
finally:
# Store partial reasoning whenever the stream exits without
# the upstream's [DONE] terminator (client disconnect, upstream
# read failure, exception). Without this, a Stop pressed mid-stream
# would discard any reasoning the proxy received but never cached.
if not finalized:
if self.config.verbose:
log_json(
"model streaming assistant messages", accumulator.messages()
)
stored = sum(
accumulator.store_reasoning(
self.reasoning_store,
ctx_scope,
cache_namespace,
prior_messages,
)
for ctx_scope, prior_messages in response_contexts
)
if self.config.verbose and stored:
LOG.info(
"stored %s streaming reasoning cache key(s) before exit",
stored,
)
return ProxyResponseResult(True, usage)

def _rewrite_sse_line(
Expand Down
25 changes: 25 additions & 0 deletions src/deepseek_cursor_proxy/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
conversation_scope,
message_signature,
tool_call_ids,
tool_call_names,
tool_call_signature,
turn_context_signature,
)
Expand Down Expand Up @@ -383,6 +384,16 @@ def reasoning_lookup_keys(
for tool_call in (message.get("tool_calls") or [])
if isinstance(tool_call, dict)
)
keys.extend(
{
"kind": "tool_name",
"function_name": tool_name,
"key": f"scope:{scope}:tool_name:{tool_name}",
"portable": False,
"hit": False,
}
for tool_name in tool_call_names(message)
)
if cache_namespace and prior_messages is not None:
turn_signature = turn_context_signature(prior_messages)
keys.append(
Expand Down Expand Up @@ -428,6 +439,20 @@ def reasoning_lookup_keys(
for tool_call in (message.get("tool_calls") or [])
if isinstance(tool_call, dict)
)
keys.extend(
{
"kind": "portable_tool_name",
"function_name": tool_name,
"key": (
f"namespace:{cache_namespace}:turn:{turn_signature}:"
f"tool_name:{tool_name}"
),
"turn_context_signature": turn_signature,
"portable": True,
"hit": False,
}
for tool_name in tool_call_names(message)
)
return keys


Expand Down
Loading