Helpers and testing¶
session.run result¶
RunResult
dataclass
¶
RunResult(
session: SessionInfo,
messages: list[SessionMessageInfo] = list(),
)
What :meth:SessionHelpers.run returns.
session
instance-attribute
¶
session: SessionInfo
The session as it was after the run. session.cost and session.tokens cover the whole session.
messages
class-attribute
instance-attribute
¶
messages: list[SessionMessageInfo] = field(
default_factory=list
)
Messages this run added, oldest first, starting with the prompt.
assistant_messages
property
¶
assistant_messages: list[SessionMessageAssistant]
The assistant messages of this run, oldest first.
outcome
property
¶
outcome: str | None
succeeded, failed or interrupted, from the run's idle marker or the session.
error
property
¶
error: SessionStructuredError | None
Why the run failed, from its idle marker or last assistant message, or None.
tokens
property
¶
tokens: TokenUsageInfo
Token usage of this run, summed over its assistant messages.
Fake server¶
FakeServer
¶
FakeServer(
*,
reply: Reply = "OK",
agents: Iterable[str] = ("build", "plan"),
mcp_servers: Mapping[str, str] | None = None,
directory: str = "/fake/project",
cost: float = 0.001,
tokens: tuple[int, int] = (10, 5),
)
Bases: MockTransport
An in-memory OpenCode server, usable as the transport of :class:opencode.Opencode and
:class:opencode.AsyncOpencode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reply
|
Reply
|
What the assistant answers to each prompt: text, or a function of the prompt text. |
'OK'
|
agents
|
Iterable[str]
|
Agent ids :meth: |
('build', 'plan')
|
mcp_servers
|
Mapping[str, str] | None
|
MCP server names mapped to their status ( |
None
|
directory
|
str
|
Project directory reported as every session's location. |
'/fake/project'
|
cost
|
float
|
Cost in USD reported for each assistant reply. |
0.001
|
tokens
|
tuple[int, int]
|
Input and output tokens reported for each assistant reply. |
(10, 5)
|
Attributes:
| Name | Type | Description |
|---|---|---|
sessions |
dict[str, SessionInfo]
|
Sessions by id. |
messages |
dict[str, list[BaseModel]]
|
Messages of each session, oldest first. |
events |
list[BaseModel]
|
Events the stream replays after |
requests |
list[Request]
|
Every request received, in order. |
add_agent
¶
Lists agent once GET /api/agent has been called after_polls more times, like an agent that
loads after boot.
set_mcp_status
¶
Reports MCP server name with status once GET /api/mcp has been called after_polls more
times.
fail
¶
fail(
method: str,
path: str,
*,
status: int = 500,
body: object = None,
exception: BaseException | None = None,
times: int | None = 1,
) -> None
Makes matching requests fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP method, such as |
required |
path
|
str
|
Regular expression matched against the whole request path, such as |
required |
status
|
int
|
Status of the error response. |
500
|
body
|
object
|
JSON error body. Defaults to an untagged body, so the error class follows |
None
|
exception
|
BaseException | None
|
Raise this from the transport instead of answering, for example |
None
|
times
|
int | None
|
How many requests fail; |
1
|