opencode-python-sdk 0.1.2__py3-none-any.whl → 0.2.0__py3-none-any.whl
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.
- opencode/__init__.py +62 -1
- opencode/_base_client.py +24 -3
- opencode/_client.py +90 -2
- opencode/_events.py +732 -0
- opencode/_rpc.py +489 -0
- opencode/_streaming.py +22 -1
- opencode/lib/__init__.py +5 -0
- opencode/lib/_ready.py +91 -0
- opencode/lib/_session.py +231 -0
- opencode/resources/agent.py +13 -11
- opencode/resources/command.py +9 -7
- opencode/resources/config.py +12 -10
- opencode/resources/credential.py +13 -11
- opencode/resources/debug.py +9 -7
- opencode/resources/event.py +9 -5
- opencode/resources/experimental.py +31 -29
- opencode/resources/file.py +15 -13
- opencode/resources/form.py +9 -7
- opencode/resources/generate.py +9 -7
- opencode/resources/integration.py +28 -26
- opencode/resources/location.py +9 -7
- opencode/resources/mcp.py +13 -11
- opencode/resources/message.py +9 -8
- opencode/resources/migration.py +10 -12
- opencode/resources/model.py +12 -10
- opencode/resources/permission.py +29 -27
- opencode/resources/plugin.py +12 -10
- opencode/resources/project.py +14 -12
- opencode/resources/provider.py +13 -11
- opencode/resources/pty.py +23 -21
- opencode/resources/reference.py +9 -7
- opencode/resources/rpc.py +9 -7
- opencode/resources/server.py +14 -12
- opencode/resources/session.py +92 -90
- opencode/resources/shell.py +19 -17
- opencode/resources/skill.py +9 -7
- opencode/resources/vcs.py +22 -20
- opencode/resources/voice.py +9 -7
- opencode/resources/websearch.py +13 -11
- opencode/resources/worktree.py +13 -11
- opencode/testing.py +508 -0
- {opencode_python_sdk-0.1.2.dist-info → opencode_python_sdk-0.2.0.dist-info}/METADATA +126 -1
- opencode_python_sdk-0.2.0.dist-info/RECORD +62 -0
- opencode_python_sdk-0.1.2.dist-info/RECORD +0 -56
- {opencode_python_sdk-0.1.2.dist-info → opencode_python_sdk-0.2.0.dist-info}/WHEEL +0 -0
- {opencode_python_sdk-0.1.2.dist-info → opencode_python_sdk-0.2.0.dist-info}/licenses/LICENSE +0 -0
opencode/_rpc.py
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
"""Typed calls to plugin RPCs.
|
|
2
|
+
|
|
3
|
+
A plugin registers an RPC with an id, methods and events. Declare the same shape once as a class, then call it
|
|
4
|
+
through ``client.rpc(Definition)``, the counterpart of ``client.rpc(definition)`` in the official TypeScript client::
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
from opencode import RpcDefinition, rpc_event, rpc_method
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AddInput(BaseModel):
|
|
11
|
+
amount: int
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Total(BaseModel):
|
|
15
|
+
total: int
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Counter(RpcDefinition, id="counter"):
|
|
19
|
+
add = rpc_method(AddInput, Total)
|
|
20
|
+
reset = rpc_method(None, Total)
|
|
21
|
+
changed = rpc_event(Total)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
counter = client.rpc(Counter)
|
|
25
|
+
print(counter.call(Counter.add, AddInput(amount=2)).total)
|
|
26
|
+
with counter.subscribe(Counter.changed) as changes:
|
|
27
|
+
for change in changes:
|
|
28
|
+
print(change.data.total)
|
|
29
|
+
|
|
30
|
+
Inputs are validated and serialized, and outputs and event data validated, with pydantic, so any type pydantic
|
|
31
|
+
accepts works: models, dataclasses, ``TypedDict``, ``list[int]``. RPC failures raise :class:`RpcCallError`.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
from __future__ import annotations
|
|
35
|
+
|
|
36
|
+
from dataclasses import dataclass
|
|
37
|
+
from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypeVar, cast, overload
|
|
38
|
+
|
|
39
|
+
import pydantic
|
|
40
|
+
from typing_extensions import Self
|
|
41
|
+
|
|
42
|
+
from ._base_client import type_adapter
|
|
43
|
+
from ._errors import RpcError, RpcInternalError
|
|
44
|
+
from ._exceptions import OpencodeError, validation_summary
|
|
45
|
+
from ._types import NOT_GIVEN, NotGiven
|
|
46
|
+
from .resources.rpc import AsyncRpcResource, RpcResource
|
|
47
|
+
|
|
48
|
+
if TYPE_CHECKING:
|
|
49
|
+
from collections.abc import Mapping
|
|
50
|
+
from types import TracebackType
|
|
51
|
+
|
|
52
|
+
from ._client import AsyncOpencode, Opencode
|
|
53
|
+
from ._events import AsyncSubscription, Subscription
|
|
54
|
+
from ._types import Headers, Timeout
|
|
55
|
+
from .types import Location, RpcEvent
|
|
56
|
+
|
|
57
|
+
__all__ = [
|
|
58
|
+
"AsyncRpcClient",
|
|
59
|
+
"AsyncRpcEvents",
|
|
60
|
+
"RpcCallError",
|
|
61
|
+
"RpcCallInternalError",
|
|
62
|
+
"RpcClient",
|
|
63
|
+
"RpcDefinition",
|
|
64
|
+
"RpcEventDefinition",
|
|
65
|
+
"RpcEventPayload",
|
|
66
|
+
"RpcEvents",
|
|
67
|
+
"RpcMethod",
|
|
68
|
+
"rpc_event",
|
|
69
|
+
"rpc_method",
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
_D = TypeVar("_D", bound="RpcDefinition")
|
|
73
|
+
_I = TypeVar("_I")
|
|
74
|
+
_O = TypeVar("_O")
|
|
75
|
+
_T = TypeVar("_T")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class RpcCallError(OpencodeError):
|
|
79
|
+
"""A plugin RPC method failed (the server answered with ``RpcError``).
|
|
80
|
+
|
|
81
|
+
Attributes:
|
|
82
|
+
rpc_id: The RPC that was called.
|
|
83
|
+
method: The method that was called.
|
|
84
|
+
type: The error type the plugin reported, for example ``"not_found"``.
|
|
85
|
+
message: The error message.
|
|
86
|
+
data: Error details, validated against the method's ``errors`` declaration when it names this type.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __init__(self, rpc_id: str, method: str, type: str, message: str, data: object = None) -> None:
|
|
90
|
+
super().__init__(f"{rpc_id}.{method} failed with {type}: {message}")
|
|
91
|
+
self.rpc_id = rpc_id
|
|
92
|
+
self.method = method
|
|
93
|
+
self.type = type
|
|
94
|
+
self.message = message
|
|
95
|
+
self.data = data
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class RpcCallInternalError(RpcCallError):
|
|
99
|
+
"""The plugin RPC crashed or returned output that does not match its declaration.
|
|
100
|
+
|
|
101
|
+
``type`` is ``"rpc.internal"`` or ``"rpc.invalid_output"``. Output this client cannot validate against the
|
|
102
|
+
method's declared output type is also reported as ``"rpc.invalid_output"``.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class RpcMethod(Generic[_I, _O]):
|
|
107
|
+
"""One method of an :class:`RpcDefinition`. Create it with :func:`rpc_method`."""
|
|
108
|
+
|
|
109
|
+
name: str
|
|
110
|
+
|
|
111
|
+
def __init__(self, input: Any, output: Any, *, name: str | None, errors: Mapping[str, Any] | None) -> None:
|
|
112
|
+
self.input = input
|
|
113
|
+
self.output = output
|
|
114
|
+
self.errors = dict(errors or {})
|
|
115
|
+
if name is not None:
|
|
116
|
+
self.name = name
|
|
117
|
+
|
|
118
|
+
def __set_name__(self, owner: type, name: str) -> None:
|
|
119
|
+
if not hasattr(self, "name"):
|
|
120
|
+
self.name = name
|
|
121
|
+
|
|
122
|
+
def encode(self, value: _I) -> object:
|
|
123
|
+
"""Validates ``value`` against the input type and returns its JSON form."""
|
|
124
|
+
if self.input is None:
|
|
125
|
+
return NOT_GIVEN if value is None else value
|
|
126
|
+
adapter = type_adapter(self.input)
|
|
127
|
+
return adapter.dump_python(adapter.validate_python(value), mode="json", by_alias=True)
|
|
128
|
+
|
|
129
|
+
def decode(self, rpc_id: str, value: object) -> _O:
|
|
130
|
+
"""Validates a method's output."""
|
|
131
|
+
if self.output is None:
|
|
132
|
+
return cast("_O", None)
|
|
133
|
+
try:
|
|
134
|
+
return cast("_O", type_adapter(self.output).validate_python(value))
|
|
135
|
+
except pydantic.ValidationError as exc:
|
|
136
|
+
raise RpcCallInternalError(rpc_id, self.name, "rpc.invalid_output", validation_summary(exc), value) from exc
|
|
137
|
+
|
|
138
|
+
def error(self, rpc_id: str, exc: RpcError | RpcInternalError) -> RpcCallError:
|
|
139
|
+
"""Maps a transport error to :class:`RpcCallError`, decoding declared error data."""
|
|
140
|
+
body = exc.error
|
|
141
|
+
data: object = body.data
|
|
142
|
+
if body.type in self.errors and data is not None:
|
|
143
|
+
try:
|
|
144
|
+
data = type_adapter(self.errors[body.type]).validate_python(data)
|
|
145
|
+
except pydantic.ValidationError:
|
|
146
|
+
data = body.data
|
|
147
|
+
cls = RpcCallInternalError if isinstance(exc, RpcInternalError) else RpcCallError
|
|
148
|
+
return cls(rpc_id, self.name, body.type, body.message, data)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class RpcEventDefinition(Generic[_T]):
|
|
152
|
+
"""One event of an :class:`RpcDefinition`. Create it with :func:`rpc_event`."""
|
|
153
|
+
|
|
154
|
+
name: str
|
|
155
|
+
|
|
156
|
+
def __init__(self, data: Any, *, name: str | None) -> None:
|
|
157
|
+
self.data = data
|
|
158
|
+
if name is not None:
|
|
159
|
+
self.name = name
|
|
160
|
+
|
|
161
|
+
def __set_name__(self, owner: type, name: str) -> None:
|
|
162
|
+
if not hasattr(self, "name"):
|
|
163
|
+
self.name = name
|
|
164
|
+
|
|
165
|
+
def parse(self, event: RpcEvent) -> RpcEventPayload[_T]:
|
|
166
|
+
"""Validates the data of a raw RPC event of this kind.
|
|
167
|
+
|
|
168
|
+
Raises:
|
|
169
|
+
pydantic.ValidationError: The data does not match the declared type.
|
|
170
|
+
"""
|
|
171
|
+
data = cast("_T", type_adapter(self.data).validate_python(event.data))
|
|
172
|
+
return RpcEventPayload(name=self.name, data=data, event=event)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
@overload
|
|
176
|
+
def rpc_method(
|
|
177
|
+
input: None, output: type[_O], *, name: str | None = None, errors: Mapping[str, Any] | None = None
|
|
178
|
+
) -> RpcMethod[None, _O]: ...
|
|
179
|
+
@overload
|
|
180
|
+
def rpc_method(
|
|
181
|
+
input: type[_I], output: type[_O], *, name: str | None = None, errors: Mapping[str, Any] | None = None
|
|
182
|
+
) -> RpcMethod[_I, _O]: ...
|
|
183
|
+
def rpc_method(
|
|
184
|
+
input: Any, output: Any, *, name: str | None = None, errors: Mapping[str, Any] | None = None
|
|
185
|
+
) -> RpcMethod[Any, Any]:
|
|
186
|
+
"""Declares an RPC method on an :class:`RpcDefinition`.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
input: The input type, or ``None`` for a method without input.
|
|
190
|
+
output: The output type. Use ``type(None)`` for a method that returns nothing.
|
|
191
|
+
name: The method name on the server. Defaults to the attribute name.
|
|
192
|
+
errors: Data types of the errors the method declares, by error type. Matching error data is validated
|
|
193
|
+
into these types on :attr:`RpcCallError.data`.
|
|
194
|
+
"""
|
|
195
|
+
return RpcMethod(input, output, name=name, errors=errors)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def rpc_event(data: type[_T], *, name: str | None = None) -> RpcEventDefinition[_T]:
|
|
199
|
+
"""Declares an RPC event on an :class:`RpcDefinition`.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
data: The type of the event's ``data``.
|
|
203
|
+
name: The event name on the server. Defaults to the attribute name.
|
|
204
|
+
"""
|
|
205
|
+
return RpcEventDefinition(data, name=name)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class RpcDefinition:
|
|
209
|
+
"""Base class for plugin RPC declarations. Pass the RPC id as a class keyword::
|
|
210
|
+
|
|
211
|
+
class Counter(RpcDefinition, id="counter"):
|
|
212
|
+
add = rpc_method(AddInput, Total)
|
|
213
|
+
changed = rpc_event(Total)
|
|
214
|
+
|
|
215
|
+
The class is only a declaration; bind it to a client with ``client.rpc(Counter)``.
|
|
216
|
+
"""
|
|
217
|
+
|
|
218
|
+
id: ClassVar[str]
|
|
219
|
+
methods: ClassVar[dict[str, RpcMethod[Any, Any]]]
|
|
220
|
+
events: ClassVar[dict[str, RpcEventDefinition[Any]]]
|
|
221
|
+
|
|
222
|
+
def __init_subclass__(cls, *, id: str | None = None, **kwargs: Any) -> None:
|
|
223
|
+
super().__init_subclass__(**kwargs)
|
|
224
|
+
if id is not None:
|
|
225
|
+
cls.id = id
|
|
226
|
+
elif not hasattr(cls, "id"):
|
|
227
|
+
raise TypeError(f"{cls.__name__} needs an RPC id: class {cls.__name__}(RpcDefinition, id='...')")
|
|
228
|
+
methods: dict[str, RpcMethod[Any, Any]] = {}
|
|
229
|
+
events: dict[str, RpcEventDefinition[Any]] = {}
|
|
230
|
+
for base in reversed(cls.__mro__):
|
|
231
|
+
for value in vars(base).values():
|
|
232
|
+
if isinstance(value, RpcMethod):
|
|
233
|
+
methods[value.name] = value
|
|
234
|
+
elif isinstance(value, RpcEventDefinition):
|
|
235
|
+
events[value.name] = value
|
|
236
|
+
cls.methods = methods
|
|
237
|
+
cls.events = events
|
|
238
|
+
|
|
239
|
+
@classmethod
|
|
240
|
+
def event_type(cls, event: RpcEventDefinition[Any] | str) -> str:
|
|
241
|
+
"""The wire ``type`` of an event of this RPC: ``rpc.<id>.<name>``."""
|
|
242
|
+
return f"rpc.{cls.id}.{event if isinstance(event, str) else event.name}"
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
@dataclass(frozen=True)
|
|
246
|
+
class RpcEventPayload(Generic[_T]):
|
|
247
|
+
"""A plugin RPC event with validated data.
|
|
248
|
+
|
|
249
|
+
Attributes:
|
|
250
|
+
name: The event name, without the ``rpc.<id>.`` prefix.
|
|
251
|
+
data: The validated event data.
|
|
252
|
+
event: The raw event, with its ``id``, ``created`` time and ``location``.
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
name: str
|
|
256
|
+
data: _T
|
|
257
|
+
event: RpcEvent
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def _check_method(definition: type[RpcDefinition], method: RpcMethod[Any, Any]) -> None:
|
|
261
|
+
if not any(known is method for known in definition.methods.values()):
|
|
262
|
+
raise ValueError(f"{getattr(method, 'name', method)!r} is not a method of {definition.__name__}")
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _check_event(definition: type[RpcDefinition], event: RpcEventDefinition[Any]) -> None:
|
|
266
|
+
if not any(known is event for known in definition.events.values()):
|
|
267
|
+
raise ValueError(f"{getattr(event, 'name', event)!r} is not an event of {definition.__name__}")
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class RpcClient(Generic[_D]):
|
|
271
|
+
"""A plugin RPC bound to a synchronous client. Get one with ``client.rpc(Definition)``."""
|
|
272
|
+
|
|
273
|
+
def __init__(self, client: Opencode, definition: type[_D]) -> None:
|
|
274
|
+
self._client = client
|
|
275
|
+
self.definition = definition
|
|
276
|
+
|
|
277
|
+
@overload
|
|
278
|
+
def call(
|
|
279
|
+
self,
|
|
280
|
+
method: RpcMethod[None, _O],
|
|
281
|
+
input: None = None,
|
|
282
|
+
*,
|
|
283
|
+
location: Location | NotGiven | None = NOT_GIVEN,
|
|
284
|
+
extra_headers: Headers | None = None,
|
|
285
|
+
timeout: Timeout | NotGiven = NOT_GIVEN,
|
|
286
|
+
) -> _O: ...
|
|
287
|
+
@overload
|
|
288
|
+
def call(
|
|
289
|
+
self,
|
|
290
|
+
method: RpcMethod[_I, _O],
|
|
291
|
+
input: _I,
|
|
292
|
+
*,
|
|
293
|
+
location: Location | NotGiven | None = NOT_GIVEN,
|
|
294
|
+
extra_headers: Headers | None = None,
|
|
295
|
+
timeout: Timeout | NotGiven = NOT_GIVEN,
|
|
296
|
+
) -> _O: ...
|
|
297
|
+
def call(
|
|
298
|
+
self,
|
|
299
|
+
method: RpcMethod[Any, Any],
|
|
300
|
+
input: Any = None,
|
|
301
|
+
*,
|
|
302
|
+
location: Location | NotGiven | None = NOT_GIVEN,
|
|
303
|
+
extra_headers: Headers | None = None,
|
|
304
|
+
timeout: Timeout | NotGiven = NOT_GIVEN,
|
|
305
|
+
) -> Any:
|
|
306
|
+
"""Calls a method of this RPC and returns its validated output.
|
|
307
|
+
|
|
308
|
+
Raises:
|
|
309
|
+
RpcCallError: The method failed.
|
|
310
|
+
RpcCallInternalError: The method crashed or returned invalid output.
|
|
311
|
+
"""
|
|
312
|
+
_check_method(self.definition, method)
|
|
313
|
+
rpc_id = self.definition.id
|
|
314
|
+
try:
|
|
315
|
+
result = self._client.rpc.call(
|
|
316
|
+
rpc_id,
|
|
317
|
+
method.name,
|
|
318
|
+
input=method.encode(input),
|
|
319
|
+
location=location,
|
|
320
|
+
extra_headers=extra_headers,
|
|
321
|
+
timeout=timeout,
|
|
322
|
+
)
|
|
323
|
+
except (RpcError, RpcInternalError) as exc:
|
|
324
|
+
raise method.error(rpc_id, exc) from exc
|
|
325
|
+
return method.decode(rpc_id, result.output)
|
|
326
|
+
|
|
327
|
+
def events(self) -> Subscription[RpcEvent]:
|
|
328
|
+
"""Subscribes to every event of this RPC, as raw events, on the client's shared event stream."""
|
|
329
|
+
from .types import RpcEvent
|
|
330
|
+
|
|
331
|
+
prefix = f"rpc.{self.definition.id}."
|
|
332
|
+
return self._client.events.of_type(RpcEvent).where(lambda event: event.type.startswith(prefix))
|
|
333
|
+
|
|
334
|
+
def subscribe(self, event: RpcEventDefinition[_T]) -> RpcEvents[_T]:
|
|
335
|
+
"""Subscribes to one event of this RPC, with validated data, on the client's shared event stream."""
|
|
336
|
+
from .types import RpcEvent
|
|
337
|
+
|
|
338
|
+
_check_event(self.definition, event)
|
|
339
|
+
wire = self.definition.event_type(event)
|
|
340
|
+
return RpcEvents(self._client.events.of_type(RpcEvent).where(lambda raw: raw.type == wire), event)
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
class AsyncRpcClient(Generic[_D]):
|
|
344
|
+
"""A plugin RPC bound to an asynchronous client. Get one with ``client.rpc(Definition)``."""
|
|
345
|
+
|
|
346
|
+
def __init__(self, client: AsyncOpencode, definition: type[_D]) -> None:
|
|
347
|
+
self._client = client
|
|
348
|
+
self.definition = definition
|
|
349
|
+
|
|
350
|
+
@overload
|
|
351
|
+
async def call(
|
|
352
|
+
self,
|
|
353
|
+
method: RpcMethod[None, _O],
|
|
354
|
+
input: None = None,
|
|
355
|
+
*,
|
|
356
|
+
location: Location | NotGiven | None = NOT_GIVEN,
|
|
357
|
+
extra_headers: Headers | None = None,
|
|
358
|
+
timeout: Timeout | NotGiven = NOT_GIVEN,
|
|
359
|
+
) -> _O: ...
|
|
360
|
+
@overload
|
|
361
|
+
async def call(
|
|
362
|
+
self,
|
|
363
|
+
method: RpcMethod[_I, _O],
|
|
364
|
+
input: _I,
|
|
365
|
+
*,
|
|
366
|
+
location: Location | NotGiven | None = NOT_GIVEN,
|
|
367
|
+
extra_headers: Headers | None = None,
|
|
368
|
+
timeout: Timeout | NotGiven = NOT_GIVEN,
|
|
369
|
+
) -> _O: ...
|
|
370
|
+
async def call(
|
|
371
|
+
self,
|
|
372
|
+
method: RpcMethod[Any, Any],
|
|
373
|
+
input: Any = None,
|
|
374
|
+
*,
|
|
375
|
+
location: Location | NotGiven | None = NOT_GIVEN,
|
|
376
|
+
extra_headers: Headers | None = None,
|
|
377
|
+
timeout: Timeout | NotGiven = NOT_GIVEN,
|
|
378
|
+
) -> Any:
|
|
379
|
+
"""Calls a method of this RPC and returns its validated output.
|
|
380
|
+
|
|
381
|
+
Raises:
|
|
382
|
+
RpcCallError: The method failed.
|
|
383
|
+
RpcCallInternalError: The method crashed or returned invalid output.
|
|
384
|
+
"""
|
|
385
|
+
_check_method(self.definition, method)
|
|
386
|
+
rpc_id = self.definition.id
|
|
387
|
+
try:
|
|
388
|
+
result = await self._client.rpc.call(
|
|
389
|
+
rpc_id,
|
|
390
|
+
method.name,
|
|
391
|
+
input=method.encode(input),
|
|
392
|
+
location=location,
|
|
393
|
+
extra_headers=extra_headers,
|
|
394
|
+
timeout=timeout,
|
|
395
|
+
)
|
|
396
|
+
except (RpcError, RpcInternalError) as exc:
|
|
397
|
+
raise method.error(rpc_id, exc) from exc
|
|
398
|
+
return method.decode(rpc_id, result.output)
|
|
399
|
+
|
|
400
|
+
def events(self) -> AsyncSubscription[RpcEvent]:
|
|
401
|
+
"""Subscribes to every event of this RPC, as raw events, on the client's shared event stream."""
|
|
402
|
+
from .types import RpcEvent
|
|
403
|
+
|
|
404
|
+
prefix = f"rpc.{self.definition.id}."
|
|
405
|
+
return self._client.events.of_type(RpcEvent).where(lambda event: event.type.startswith(prefix))
|
|
406
|
+
|
|
407
|
+
def subscribe(self, event: RpcEventDefinition[_T]) -> AsyncRpcEvents[_T]:
|
|
408
|
+
"""Subscribes to one event of this RPC, with validated data, on the client's shared event stream."""
|
|
409
|
+
from .types import RpcEvent
|
|
410
|
+
|
|
411
|
+
_check_event(self.definition, event)
|
|
412
|
+
wire = self.definition.event_type(event)
|
|
413
|
+
return AsyncRpcEvents(self._client.events.of_type(RpcEvent).where(lambda raw: raw.type == wire), event)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
class RpcEvents(Generic[_T]):
|
|
417
|
+
"""Events of one kind from a plugin RPC. Iterate with ``for``; close it, or use ``with``, to leave."""
|
|
418
|
+
|
|
419
|
+
def __init__(self, source: Subscription[RpcEvent], event: RpcEventDefinition[_T]) -> None:
|
|
420
|
+
self._source = source
|
|
421
|
+
self._event = event
|
|
422
|
+
|
|
423
|
+
def __iter__(self) -> Self:
|
|
424
|
+
return self
|
|
425
|
+
|
|
426
|
+
def __next__(self) -> RpcEventPayload[_T]:
|
|
427
|
+
return self._event.parse(next(self._source))
|
|
428
|
+
|
|
429
|
+
def get(self, timeout: float | None = None) -> RpcEventPayload[_T]:
|
|
430
|
+
"""Returns the next event, waiting up to ``timeout`` seconds. See :meth:`Subscription.get`."""
|
|
431
|
+
return self._event.parse(self._source.get(timeout))
|
|
432
|
+
|
|
433
|
+
def close(self) -> None:
|
|
434
|
+
"""Leaves the shared event stream."""
|
|
435
|
+
self._source.close()
|
|
436
|
+
|
|
437
|
+
def __enter__(self) -> Self:
|
|
438
|
+
return self
|
|
439
|
+
|
|
440
|
+
def __exit__(
|
|
441
|
+
self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None
|
|
442
|
+
) -> None:
|
|
443
|
+
self.close()
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
class AsyncRpcEvents(Generic[_T]):
|
|
447
|
+
"""Events of one kind from a plugin RPC. Iterate with ``async for``; close it, or use ``async with``."""
|
|
448
|
+
|
|
449
|
+
def __init__(self, source: AsyncSubscription[RpcEvent], event: RpcEventDefinition[_T]) -> None:
|
|
450
|
+
self._source = source
|
|
451
|
+
self._event = event
|
|
452
|
+
|
|
453
|
+
def __aiter__(self) -> Self:
|
|
454
|
+
return self
|
|
455
|
+
|
|
456
|
+
async def __anext__(self) -> RpcEventPayload[_T]:
|
|
457
|
+
return self._event.parse(await self._source.__anext__())
|
|
458
|
+
|
|
459
|
+
async def get(self) -> RpcEventPayload[_T]:
|
|
460
|
+
"""Returns the next event. See :meth:`AsyncSubscription.get`."""
|
|
461
|
+
return self._event.parse(await self._source.get())
|
|
462
|
+
|
|
463
|
+
async def close(self) -> None:
|
|
464
|
+
"""Leaves the shared event stream."""
|
|
465
|
+
await self._source.close()
|
|
466
|
+
|
|
467
|
+
async def __aenter__(self) -> Self:
|
|
468
|
+
return self
|
|
469
|
+
|
|
470
|
+
async def __aexit__(
|
|
471
|
+
self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None
|
|
472
|
+
) -> None:
|
|
473
|
+
await self.close()
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
class TypedRpcResource(RpcResource):
|
|
477
|
+
"""``client.rpc``: the generated ``call`` plus ``client.rpc(Definition)`` for typed access."""
|
|
478
|
+
|
|
479
|
+
def __call__(self, definition: type[_D]) -> RpcClient[_D]:
|
|
480
|
+
"""Binds a plugin RPC declaration to this client."""
|
|
481
|
+
return RpcClient(self._client, definition)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
class AsyncTypedRpcResource(AsyncRpcResource):
|
|
485
|
+
"""``client.rpc``: the generated ``call`` plus ``client.rpc(Definition)`` for typed access."""
|
|
486
|
+
|
|
487
|
+
def __call__(self, definition: type[_D]) -> AsyncRpcClient[_D]:
|
|
488
|
+
"""Binds a plugin RPC declaration to this client."""
|
|
489
|
+
return AsyncRpcClient(self._client, definition)
|
opencode/_streaming.py
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import contextlib
|
|
5
6
|
import json
|
|
6
7
|
import re
|
|
8
|
+
import socket
|
|
7
9
|
from dataclasses import dataclass, field
|
|
8
10
|
from typing import TYPE_CHECKING, Any, Generic, TypeVar
|
|
9
11
|
|
|
@@ -14,7 +16,7 @@ from typing_extensions import Self
|
|
|
14
16
|
from ._exceptions import APIConnectionError, APIResponseValidationError, validation_summary
|
|
15
17
|
|
|
16
18
|
if TYPE_CHECKING:
|
|
17
|
-
from collections.abc import AsyncGenerator, AsyncIterator, Generator, Iterator
|
|
19
|
+
from collections.abc import AsyncGenerator, AsyncIterator, Callable, Generator, Iterator
|
|
18
20
|
from types import TracebackType
|
|
19
21
|
|
|
20
22
|
__all__ = ["AsyncStream", "ServerSentEvent", "Stream"]
|
|
@@ -111,6 +113,12 @@ class _StreamBase(Generic[_T]):
|
|
|
111
113
|
def __init__(self, response: httpx.Response, adapter: pydantic.TypeAdapter[_T]) -> None:
|
|
112
114
|
self.response = response
|
|
113
115
|
self._adapter = adapter
|
|
116
|
+
self.on_activity: Callable[[], object] | None = None
|
|
117
|
+
"""Called for every chunk the stream receives, including keepalive comments that carry no event."""
|
|
118
|
+
|
|
119
|
+
def _activity(self) -> None:
|
|
120
|
+
if self.on_activity is not None:
|
|
121
|
+
self.on_activity()
|
|
114
122
|
|
|
115
123
|
def _decode(self, event: ServerSentEvent) -> _T:
|
|
116
124
|
try:
|
|
@@ -148,6 +156,7 @@ class Stream(_StreamBase[_T]):
|
|
|
148
156
|
try:
|
|
149
157
|
splitter = _LineSplitter()
|
|
150
158
|
for chunk in self.response.iter_text():
|
|
159
|
+
self._activity()
|
|
151
160
|
for line in splitter.feed(chunk):
|
|
152
161
|
if (event := decoder.feed_line(line)) is not None:
|
|
153
162
|
yield self._decode(event)
|
|
@@ -166,6 +175,17 @@ class Stream(_StreamBase[_T]):
|
|
|
166
175
|
self._iterator.close()
|
|
167
176
|
self.response.close()
|
|
168
177
|
|
|
178
|
+
def interrupt(self) -> None:
|
|
179
|
+
"""Unblocks a read in progress on another thread, which then ends with :class:`APIConnectionError`.
|
|
180
|
+
|
|
181
|
+
``close`` must run on the iterating thread. Call this from any other thread first to wake it.
|
|
182
|
+
"""
|
|
183
|
+
network_stream: Any = self.response.extensions.get("network_stream")
|
|
184
|
+
sock: Any = network_stream.get_extra_info("socket") if network_stream is not None else None
|
|
185
|
+
if isinstance(sock, socket.socket):
|
|
186
|
+
with contextlib.suppress(OSError):
|
|
187
|
+
sock.shutdown(socket.SHUT_RDWR)
|
|
188
|
+
|
|
169
189
|
def __enter__(self) -> Self:
|
|
170
190
|
return self
|
|
171
191
|
|
|
@@ -200,6 +220,7 @@ class AsyncStream(_StreamBase[_T]):
|
|
|
200
220
|
try:
|
|
201
221
|
splitter = _LineSplitter()
|
|
202
222
|
async for chunk in self.response.aiter_text():
|
|
223
|
+
self._activity()
|
|
203
224
|
for line in splitter.feed(chunk):
|
|
204
225
|
if (event := decoder.feed_line(line)) is not None:
|
|
205
226
|
yield self._decode(event)
|
opencode/lib/__init__.py
ADDED
opencode/lib/_ready.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""``client.wait_until_ready``: poll until agents and MCP servers have loaded.
|
|
2
|
+
|
|
3
|
+
OpenCode loads agents and connects MCP servers in the background after it starts listening, so a freshly booted
|
|
4
|
+
server can answer requests before the agent a caller needs exists.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import time
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
import anyio
|
|
13
|
+
|
|
14
|
+
from .._exceptions import APIConnectionError, ServiceUnavailableError
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from collections.abc import Iterable
|
|
18
|
+
|
|
19
|
+
from .._client import AsyncOpencode, Opencode
|
|
20
|
+
from ..types import AgentListResponse, McpListResponse
|
|
21
|
+
|
|
22
|
+
__all__ = ["POLL_INTERVAL", "async_wait_until_ready", "wait_until_ready"]
|
|
23
|
+
|
|
24
|
+
POLL_INTERVAL = 0.25
|
|
25
|
+
"""Seconds between polls."""
|
|
26
|
+
|
|
27
|
+
_TRANSIENT = (APIConnectionError, ServiceUnavailableError)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _missing(
|
|
31
|
+
agents: frozenset[str],
|
|
32
|
+
mcp_servers: frozenset[str],
|
|
33
|
+
agent_list: AgentListResponse | None,
|
|
34
|
+
mcp_list: McpListResponse | None,
|
|
35
|
+
) -> list[str]:
|
|
36
|
+
missing: list[str] = []
|
|
37
|
+
if agents:
|
|
38
|
+
present = {agent.id for agent in agent_list.data} if agent_list is not None else set[str]()
|
|
39
|
+
missing += [f"agent {name!r}" for name in sorted(agents - present)]
|
|
40
|
+
if mcp_servers:
|
|
41
|
+
connected = (
|
|
42
|
+
{server.name for server in mcp_list.data if server.status.status == "connected"}
|
|
43
|
+
if mcp_list is not None
|
|
44
|
+
else set[str]()
|
|
45
|
+
)
|
|
46
|
+
missing += [f"MCP server {name!r}" for name in sorted(mcp_servers - connected)]
|
|
47
|
+
return missing
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def wait_until_ready(
|
|
51
|
+
client: Opencode, *, agents: Iterable[str] = (), mcp_servers: Iterable[str] = (), timeout: float = 60
|
|
52
|
+
) -> None:
|
|
53
|
+
wanted_agents, wanted_mcp = frozenset(agents), frozenset(mcp_servers)
|
|
54
|
+
deadline = time.monotonic() + timeout
|
|
55
|
+
while True:
|
|
56
|
+
agent_list = mcp_list = None
|
|
57
|
+
missing = ["server"]
|
|
58
|
+
try:
|
|
59
|
+
client.server.info()
|
|
60
|
+
agent_list = client.agent.list() if wanted_agents else None
|
|
61
|
+
mcp_list = client.mcp.list() if wanted_mcp else None
|
|
62
|
+
missing = _missing(wanted_agents, wanted_mcp, agent_list, mcp_list)
|
|
63
|
+
except _TRANSIENT:
|
|
64
|
+
pass
|
|
65
|
+
if not missing:
|
|
66
|
+
return
|
|
67
|
+
if time.monotonic() >= deadline:
|
|
68
|
+
raise TimeoutError(f"OpenCode not ready after {timeout}s; still waiting for {', '.join(missing)}")
|
|
69
|
+
time.sleep(POLL_INTERVAL)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
async def async_wait_until_ready(
|
|
73
|
+
client: AsyncOpencode, *, agents: Iterable[str] = (), mcp_servers: Iterable[str] = (), timeout: float = 60
|
|
74
|
+
) -> None:
|
|
75
|
+
wanted_agents, wanted_mcp = frozenset(agents), frozenset(mcp_servers)
|
|
76
|
+
deadline = time.monotonic() + timeout
|
|
77
|
+
while True:
|
|
78
|
+
agent_list = mcp_list = None
|
|
79
|
+
missing = ["server"]
|
|
80
|
+
try:
|
|
81
|
+
await client.server.info()
|
|
82
|
+
agent_list = await client.agent.list() if wanted_agents else None
|
|
83
|
+
mcp_list = await client.mcp.list() if wanted_mcp else None
|
|
84
|
+
missing = _missing(wanted_agents, wanted_mcp, agent_list, mcp_list)
|
|
85
|
+
except _TRANSIENT:
|
|
86
|
+
pass
|
|
87
|
+
if not missing:
|
|
88
|
+
return
|
|
89
|
+
if time.monotonic() >= deadline:
|
|
90
|
+
raise TimeoutError(f"OpenCode not ready after {timeout}s; still waiting for {', '.join(missing)}")
|
|
91
|
+
await anyio.sleep(POLL_INTERVAL)
|