feat(sdk): route content blocks through native commands

Reuse the existing run input for command images and ordinary queue/steer delivery.
Align Unicode command separators, bound goal observation, preserve plan input encoding,
and handle explicitly expected Windows cancellation disconnects in runtime tests.
This commit is contained in:
LogosVeritas committed 2026-10-26 12:23:58 +08:00
1 parent 5771a93f2e
commit dc311ba744
56 files changed
+2264 -326

No files matched your search

+31 -61
View File
@@ -3,7 +3,7 @@ from __future__ import annotations
import uuid
from dataclasses import dataclass, field
from pathlib import Path
from typing import Callable, Literal, overload
from typing import Callable, Literal
from .client import HarnessClient, HarnessConfig, UserQuestionHandler
from .errors import SdkProtocolError, TransportClosedError
@@ -167,19 +167,6 @@ class DeepSeekHarness:
self._client.resume_session(session_id)
return Session(self, session_id)
@overload
def run(
self, input: list[JsonObject], *, session_id: str | None = None,
on_notification: Callable[[Notification], None] | None = None,
) -> RunResult: ...
@overload
def run(
self, input: str | list[JsonObject], *, session_id: str | None = None,
on_notification: Callable[[Notification], None] | None = None,
delivery: Literal["queue", "steer"] = "queue",
) -> RunResult | CommandExecution: ...
def run(
self,
input: str | list[JsonObject],
@@ -188,7 +175,7 @@ class DeepSeekHarness:
on_notification: Callable[[Notification], None] | None = None,
delivery: Literal["queue", "steer"] = "queue",
) -> RunResult | CommandExecution:
"""Run text or a native command on a fresh or named Session."""
"""Run text or content blocks through native input routing on a fresh or named Session."""
return self.start_session(session_id).run(input, on_notification=on_notification, delivery=delivery)
@@ -207,19 +194,6 @@ class Session:
self.harness.start()
return self.harness.client.set_working_directory(self.id, path)
@overload
def run(
self, input: list[JsonObject], *,
on_notification: Callable[[Notification], None] | None = None,
) -> RunResult: ...
@overload
def run(
self, input: str | list[JsonObject], *,
on_notification: Callable[[Notification], None] | None = None,
delivery: Literal["queue", "steer"] = "queue",
) -> RunResult | CommandExecution: ...
def run(
self,
input: str | list[JsonObject],
@@ -227,49 +201,45 @@ class Session:
on_notification: Callable[[Notification], None] | None = None,
delivery: Literal["queue", "steer"] = "queue",
) -> RunResult | CommandExecution:
"""Run text through native input routing, or queue literal content blocks.
"""Run text or content blocks through native input routing.
Text returns the prompt's receipt-to-idle RunResult or a settled
Input returns the prompt's receipt-to-idle RunResult or a settled
CommandExecution; command completion does not wait for later model work.
Content blocks stay literal and accept only queue delivery. Notifications
retain wire order. Command callbacks replay Session-tree notifications
The first text block selects native commands. Matched commands require
one text block with only encoded images beside it; native handlers decide
which commands accept those images. Ordinary prompts retain block order
and use the requested queue or steer delivery. Notifications retain
wire order. Command callbacks replay Session-tree notifications
from subscription through their own command/done after the response,
including overlapping work. Handler errors remain command results;
routing, transport and observer failures raise without rolling back work.
"""
if delivery not in ("queue", "steer"):
raise ValueError("run delivery must be queue or steer")
if not isinstance(input, str) and delivery != "queue":
raise ValueError("content-block input requires queue delivery")
self.harness.start()
with self.harness.client.subscribe_session_notifications(self.id) as subscription:
if isinstance(input, str):
response = self.harness.client._request_raw(
"session/query", {"sessionId": self.id, "text": input, "delivery": delivery},
)
if not isinstance(response, dict):
raise SdkProtocolError("session/query response must be a JSON object")
if response.get("kind") == "command":
execution = CommandExecution.model_validate(response.get("execution"))
while True:
try:
notification = subscription._next_queued()
except TransportClosedError:
break
if notification is None:
break
if on_notification is not None:
on_notification(notification)
if _is_command_done(notification, self.id, execution.command_id):
break
return execution
if response.get("kind") != "prompt" or not isinstance(response.get("messageId"), str) or not response["messageId"]:
raise SdkProtocolError("session/query requires a prompt messageId or command execution result")
message_id = response["messageId"]
else:
message_id = self.harness.client.session_prompt(
self.id, input, notification_subscription=subscription,
)
response = self.harness.client._request_raw("session/query", {
"sessionId": self.id, "contentBlocks": normalize_input(input), "delivery": delivery,
})
if not isinstance(response, dict):
raise SdkProtocolError("session/query response must be a JSON object")
if response.get("kind") == "command":
execution = CommandExecution.model_validate(response.get("execution"))
while True:
try:
notification = subscription._next_queued()
except TransportClosedError:
break
if notification is None:
break
if on_notification is not None:
on_notification(notification)
if _is_command_done(notification, self.id, execution.command_id):
break
return execution
if response.get("kind") != "prompt" or not isinstance(response.get("messageId"), str) or not response["messageId"]:
raise SdkProtocolError("session/query requires a prompt messageId or command execution result")
message_id = response["messageId"]
activity = _PromptActivity(self.id, message_id)
while True:
notification = subscription.next()