driverkit 0.1.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.
- driverkit/__init__.py +93 -0
- driverkit/asgi.py +277 -0
- driverkit/auth.py +508 -0
- driverkit/client.py +413 -0
- driverkit/fastmcp_server.py +374 -0
- driverkit/mcp_core.py +988 -0
- driverkit/mcp_server.py +262 -0
- driverkit/relay.py +1531 -0
- driverkit/render.py +486 -0
- driverkit/serve_relay.py +670 -0
- driverkit/types.py +943 -0
- driverkit-0.1.0.dist-info/METADATA +11 -0
- driverkit-0.1.0.dist-info/RECORD +15 -0
- driverkit-0.1.0.dist-info/WHEEL +4 -0
- driverkit-0.1.0.dist-info/entry_points.txt +4 -0
driverkit/types.py
ADDED
|
@@ -0,0 +1,943 @@
|
|
|
1
|
+
"""
|
|
2
|
+
driverkit — the types, mirroring ``packages/driverkit/src/core/types.ts``.
|
|
3
|
+
|
|
4
|
+
These dataclasses are not a convenience layer over dicts; they are the Python
|
|
5
|
+
end of a wire contract. Every field name here matches the TypeScript interface
|
|
6
|
+
it mirrors, and ``to_dict``/``from_dict`` are the only places a name is spelled
|
|
7
|
+
twice. The reason to be strict about it: the manifest an agent caches was
|
|
8
|
+
serialised by the TS core, and a Python client that quietly renames a field
|
|
9
|
+
produces a tool list that no longer describes the app it is driving.
|
|
10
|
+
|
|
11
|
+
Two shapes read differently in Python than in TypeScript and are called out
|
|
12
|
+
where they are defined:
|
|
13
|
+
|
|
14
|
+
* ``Row`` is ``{ id, ...columns }`` in TS — an index signature. Python has no
|
|
15
|
+
such thing, so the extra columns live in a dict and are flattened back out on
|
|
16
|
+
the way to JSON.
|
|
17
|
+
* ``Result`` is a discriminated union in TS. Python gets one dataclass with an
|
|
18
|
+
``ok`` flag, because a union of two dataclasses would make every caller
|
|
19
|
+
``isinstance``-check before reading ``snapshot``, which both arms carry.
|
|
20
|
+
|
|
21
|
+
Unknown fields are ignored on the way in. PROTOCOL.md promises a field may be
|
|
22
|
+
added without a version bump, so a strict parser here would turn an additive
|
|
23
|
+
change on the relay into a broken client.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
from dataclasses import dataclass, field
|
|
29
|
+
from typing import Any, Mapping, Sequence
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"Scalar",
|
|
33
|
+
"VerbKind",
|
|
34
|
+
"VERB_KINDS",
|
|
35
|
+
"RELAY_REFUSALS",
|
|
36
|
+
"UNKNOWN_OUTCOME",
|
|
37
|
+
"ArgSpec",
|
|
38
|
+
"VerbSpec",
|
|
39
|
+
"ScreenSpec",
|
|
40
|
+
"FlowStep",
|
|
41
|
+
"FlowSpec",
|
|
42
|
+
"Manifest",
|
|
43
|
+
"Row",
|
|
44
|
+
"Page",
|
|
45
|
+
"Snapshot",
|
|
46
|
+
"Result",
|
|
47
|
+
"Tab",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
#: A value an agent can pass to a verb. The TS type is the same four: anything
|
|
51
|
+
#: that cannot survive JSON cannot cross the relay.
|
|
52
|
+
Scalar = str | int | float | bool | None
|
|
53
|
+
|
|
54
|
+
#: What a verb does to the world, declared in the cached half so an agent knows
|
|
55
|
+
#: before it calls: ``read`` changes no data and reports no count (it may still
|
|
56
|
+
#: move the person's screen — navigation is a read), ``describe`` changes what
|
|
57
|
+
#: is selected and reports a ``count``, ``commit`` changes data and may be
|
|
58
|
+
#: irreversible. Mirrors ``VerbKind``, whose comment carries the argument for
|
|
59
|
+
#: why navigation is a ``read`` and not a fourth kind.
|
|
60
|
+
VerbKind = str
|
|
61
|
+
#: ``ask`` is the fourth, and only the core mints it: an app verb may not
|
|
62
|
+
#: declare it. See ``VerbKind`` in the TypeScript half for why a question is not
|
|
63
|
+
#: a ``read`` -- the three kinds are a ladder of what a wrong call costs you,
|
|
64
|
+
#: and a wrong question costs a person's attention, which is the one currency
|
|
65
|
+
#: the other three never spend.
|
|
66
|
+
VERB_KINDS = ("read", "describe", "commit", "ask")
|
|
67
|
+
|
|
68
|
+
#: Refusals that come from the relay rather than from an app's verb, as a
|
|
69
|
+
#: closed set. A client has to be able to tell "your app said no", which means
|
|
70
|
+
#: rephrase and retry, from "your call never landed", which means reconnect or
|
|
71
|
+
#: pick another tab. These three are reserved, along with the core's own
|
|
72
|
+
#: ``stale``, ``wrong-screen``, ``stale-plan``, ``unknown-verb``,
|
|
73
|
+
#: ``bad-argument``, ``unmounted``, ``threw`` and ``budget-spent``:
|
|
74
|
+
#: an app that names one of its verb refusals the same thing takes that
|
|
75
|
+
#: distinction away, and if ``timeout`` can mean either then it means neither.
|
|
76
|
+
#: See ``RelayRefusal`` and PROTOCOL.md, "When a call never reaches a tab".
|
|
77
|
+
RELAY_REFUSALS = ("no-such-tab", "tab-went-away", "timeout")
|
|
78
|
+
|
|
79
|
+
#: Refusals that come from the approval gate in the core's ``call()`` rather
|
|
80
|
+
#: than from an app's verb. Reserved like the rest, and listed here so a client
|
|
81
|
+
#: can tell them apart from the relay's.
|
|
82
|
+
#:
|
|
83
|
+
#: ``declined-by-user`` is a person saying no to a ``commit`` before it ran.
|
|
84
|
+
#: ``approval-pending`` is a call that arrived while somebody was being asked
|
|
85
|
+
#: about another one — nothing ran, and it is a refusal rather than a queue
|
|
86
|
+
#: because the call an agent is most likely to make in that window is the
|
|
87
|
+
#: Approve button of the dialog asking it (webmachinelearning/webmcp#288).
|
|
88
|
+
#:
|
|
89
|
+
#: ``approval-timeout`` must never be folded into the relay's ``timeout``, and
|
|
90
|
+
#: that is the whole reason it is a separate slug. The relay's means the call
|
|
91
|
+
#: was accepted and no answer came back, so the outcome is **unknown** and a
|
|
92
|
+
#: commit may already have run; this one means nobody answered the question, so
|
|
93
|
+
#: the verb was never reached and nothing changed. Opposite next moves: read
|
|
94
|
+
#: the screen before touching anything, versus ask a person and call again.
|
|
95
|
+
#: Which is why it is deliberately absent from :data:`UNKNOWN_OUTCOME`.
|
|
96
|
+
APPROVAL_REFUSALS = ("declined-by-user", "approval-pending", "approval-timeout")
|
|
97
|
+
|
|
98
|
+
#: The relay refusals that leave the outcome **unknown** rather than failed.
|
|
99
|
+
#:
|
|
100
|
+
#: Only ``no-such-tab`` proves the call did not happen: it was never forwarded,
|
|
101
|
+
#: because there was nowhere to forward it. The other two are both "the call
|
|
102
|
+
#: was accepted and no answer came back" — the tab went quiet past the
|
|
103
|
+
#: deadline, or its stream closed and the answer died with it. In either case
|
|
104
|
+
#: the verb may well have run.
|
|
105
|
+
#:
|
|
106
|
+
#: The reflex this exists to block is treating a non-answer as a failed step
|
|
107
|
+
#: and retrying it, which is the one move that turns one bulk accept into two.
|
|
108
|
+
UNKNOWN_OUTCOME = ("timeout", "tab-went-away")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _drop_none(d: dict[str, Any]) -> dict[str, Any]:
|
|
112
|
+
"""Omit absent optional fields rather than sending ``null``.
|
|
113
|
+
|
|
114
|
+
``{"optional": null}`` and an absent ``optional`` mean the same thing to a
|
|
115
|
+
tolerant reader, but they hash differently — and the manifest version in
|
|
116
|
+
the TS core is a fingerprint of the serialised verb surface. Emitting
|
|
117
|
+
explicit nulls would make a round-tripped manifest stop matching itself.
|
|
118
|
+
"""
|
|
119
|
+
return {k: v for k, v in d.items() if v is not None}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _duration(value: Any) -> int | None:
|
|
123
|
+
"""A duration off the wire, or None for anything that is not one.
|
|
124
|
+
|
|
125
|
+
``bool`` is excluded explicitly, as everywhere else in this file that reads
|
|
126
|
+
a number off JSON: ``True`` is an ``int`` in Python, and ``tookMs: true``
|
|
127
|
+
is not a measurement. A negative or non-finite figure is a broken clock
|
|
128
|
+
rather than a fast call, which is the judgement
|
|
129
|
+
:func:`driverkit.render.render_duration` already makes when it renders
|
|
130
|
+
silence for one, made here as well so the field never holds one. Whole
|
|
131
|
+
milliseconds, because a float survives JSON and nothing here wants tenths
|
|
132
|
+
of a millisecond.
|
|
133
|
+
|
|
134
|
+
Dropping rather than raising is the rule :meth:`Tab.from_dict` set for a
|
|
135
|
+
``focus`` that is not a string: a client that refused to parse a result
|
|
136
|
+
because one optional field was junk would lose the screen as well, and the
|
|
137
|
+
screen is the half that matters.
|
|
138
|
+
"""
|
|
139
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
140
|
+
return None
|
|
141
|
+
if value != value or value in (float("inf"), float("-inf")) or value < 0:
|
|
142
|
+
return None
|
|
143
|
+
return int(value)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _screens(value: Any) -> str | tuple[str, ...] | None:
|
|
147
|
+
"""A verb's ``screen`` off the wire, in the shape it was sent in.
|
|
148
|
+
|
|
149
|
+
One string, several, or nothing. The shape is preserved rather than
|
|
150
|
+
normalised because the manifest is the cached half and its bytes are what a
|
|
151
|
+
fingerprint is taken of: turning ``"contacts"`` into ``["contacts"]`` on a
|
|
152
|
+
round trip would move the version of every app that names one screen, and
|
|
153
|
+
tell every agent holding a cached copy that it had gone stale when nothing
|
|
154
|
+
about the app had changed.
|
|
155
|
+
"""
|
|
156
|
+
if isinstance(value, str):
|
|
157
|
+
return value or None
|
|
158
|
+
if isinstance(value, (list, tuple)):
|
|
159
|
+
named = tuple(s for s in value if isinstance(s, str) and s)
|
|
160
|
+
return named or None
|
|
161
|
+
return None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@dataclass(frozen=True)
|
|
165
|
+
class ArgSpec:
|
|
166
|
+
"""What a verb accepts. Mirrors ``ArgSpec``."""
|
|
167
|
+
|
|
168
|
+
type: str # "string" | "number" | "boolean"
|
|
169
|
+
describe: str
|
|
170
|
+
one_of: tuple[Scalar, ...] | None = None # wire name: oneOf
|
|
171
|
+
optional: bool | None = None # absent means required
|
|
172
|
+
|
|
173
|
+
@classmethod
|
|
174
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "ArgSpec":
|
|
175
|
+
one_of = d.get("oneOf")
|
|
176
|
+
return cls(
|
|
177
|
+
type=d["type"],
|
|
178
|
+
describe=d.get("describe", ""),
|
|
179
|
+
one_of=tuple(one_of) if one_of is not None else None,
|
|
180
|
+
optional=d.get("optional"),
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
def to_dict(self) -> dict[str, Any]:
|
|
184
|
+
return _drop_none(
|
|
185
|
+
{
|
|
186
|
+
"type": self.type,
|
|
187
|
+
"describe": self.describe,
|
|
188
|
+
"oneOf": list(self.one_of) if self.one_of is not None else None,
|
|
189
|
+
"optional": self.optional,
|
|
190
|
+
}
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
@property
|
|
194
|
+
def required(self) -> bool:
|
|
195
|
+
"""Absent ``optional`` means required — stated once, here, so no caller
|
|
196
|
+
has to remember that the default is the strict one."""
|
|
197
|
+
return not self.optional
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
@dataclass(frozen=True)
|
|
201
|
+
class VerbSpec:
|
|
202
|
+
"""One entry of ``Manifest.verbs``.
|
|
203
|
+
|
|
204
|
+
Note what this is *not*: it has no ``run``. The TS ``Verb`` carries a body
|
|
205
|
+
and this does not, because the body never crosses the wire — only the
|
|
206
|
+
surface an agent can observe does.
|
|
207
|
+
"""
|
|
208
|
+
|
|
209
|
+
name: str
|
|
210
|
+
describe: str
|
|
211
|
+
args: dict[str, ArgSpec] = field(default_factory=dict)
|
|
212
|
+
refuses: tuple[str, ...] = ()
|
|
213
|
+
#: ``read`` | ``describe`` | ``commit``, or None when the app did not say.
|
|
214
|
+
#:
|
|
215
|
+
#: None is kept distinct from every kind rather than defaulted to one,
|
|
216
|
+
#: because both defaults are wrong in a way that costs something. Reading
|
|
217
|
+
#: it as ``read`` tells an agent an irreversible verb is free to try.
|
|
218
|
+
#: Reading it as ``commit`` marks every verb of an app that predates the
|
|
219
|
+
#: field as dangerous, and a warning on everything is a warning on nothing.
|
|
220
|
+
#: Absent means absent: make no claim.
|
|
221
|
+
kind: VerbKind | None = None
|
|
222
|
+
#: The screen this verb belongs to, or the screens it belongs to, matched
|
|
223
|
+
#: against ``Snapshot.screen``; None for "anywhere".
|
|
224
|
+
#:
|
|
225
|
+
#: The tool list does NOT vary with it, and must not: a verb list that
|
|
226
|
+
#: changed on every navigation would bust the prompt prefix every time the
|
|
227
|
+
#: person moved, which is the cost argument the library is built on. The
|
|
228
|
+
#: mapping is static — which verb lives on which screen is the same for
|
|
229
|
+
#: every user forever — and only which screen is live is dynamic. So the
|
|
230
|
+
#: verb is offered everywhere and the core refuses it off its screens, with
|
|
231
|
+
#: the live one and all of the verb's own in the hint. See ``Verb.screen``.
|
|
232
|
+
#:
|
|
233
|
+
#: Several screens because one string made the worked example lie:
|
|
234
|
+
#: ``next-page`` pages both the contacts list and a company's contact list,
|
|
235
|
+
#: and declaring either would refuse a call on a screen still publishing a
|
|
236
|
+
#: ``page`` block. A tuple rather than a list for the reason ``refuses`` is
|
|
237
|
+
#: one — this dataclass is frozen and therefore hashable, and one mutable
|
|
238
|
+
#: field would take that away. ``from_dict`` accepts the wire's list and
|
|
239
|
+
#: ``to_dict`` writes one back.
|
|
240
|
+
screen: str | tuple[str, ...] | None = None
|
|
241
|
+
#: True on a ``commit`` the app says it can undo, else None for "the app
|
|
242
|
+
#: did not say".
|
|
243
|
+
#:
|
|
244
|
+
#: Not defaulted to either answer, for the reason ``kind`` is not:
|
|
245
|
+
#: assuming reversible tells an agent a bulk accept can be taken back, and
|
|
246
|
+
#: assuming irreversible marks every commit written before this field as
|
|
247
|
+
#: the dangerous kind, which is a warning on everything. Only read for
|
|
248
|
+
#: ``commit``; see ``Verb.reversible``.
|
|
249
|
+
reversible: bool | None = None
|
|
250
|
+
#: ``"plan"`` on a ``commit`` that may only be called with the token the
|
|
251
|
+
#: ``describe`` verb returned beside its ``count``, else None.
|
|
252
|
+
#:
|
|
253
|
+
#: The field that turns the describe/commit pairing from advice into a
|
|
254
|
+
#: guarantee. "A commit takes no predicate and acts on the current
|
|
255
|
+
#: selection" re-reads the selection at call time, so between the two calls
|
|
256
|
+
#: a person can tick a row or a websocket can land, and then the count that
|
|
257
|
+
#: was read and approved is not provably the set that moved. With this, the
|
|
258
|
+
#: core refuses the commit as ``stale-plan`` instead, naming both counts.
|
|
259
|
+
#: Absent makes no claim and the verb may be called bare, which is what
|
|
260
|
+
#: every commit written before this field does. See ``Verb.consumes``.
|
|
261
|
+
consumes: str | None = None
|
|
262
|
+
|
|
263
|
+
@classmethod
|
|
264
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "VerbSpec":
|
|
265
|
+
args = d.get("args") or {}
|
|
266
|
+
kind = d.get("kind")
|
|
267
|
+
reversible = d.get("reversible")
|
|
268
|
+
screen = d.get("screen")
|
|
269
|
+
return cls(
|
|
270
|
+
name=d["name"],
|
|
271
|
+
describe=d.get("describe", ""),
|
|
272
|
+
args={k: ArgSpec.from_dict(v) for k, v in args.items()},
|
|
273
|
+
refuses=tuple(d.get("refuses") or ()),
|
|
274
|
+
# An unrecognised kind is dropped rather than passed through. A
|
|
275
|
+
# future fourth kind means this code is out of date, and inventing
|
|
276
|
+
# a safety claim from a word it does not know is the one thing it
|
|
277
|
+
# must not do.
|
|
278
|
+
kind=kind if kind in VERB_KINDS else None,
|
|
279
|
+
# A string stays a string and a list becomes a tuple, so the shape
|
|
280
|
+
# the app declared survives the round trip: a client that reads
|
|
281
|
+
# `verbs[].screen` as a string goes on doing so for every
|
|
282
|
+
# single-screen app. Non-strings inside a list are dropped rather
|
|
283
|
+
# than coerced — `str(None)` is the screen id "None", which is a
|
|
284
|
+
# screen no app has — and a list left empty by that, or sent empty,
|
|
285
|
+
# becomes None: the core refuses `screen: []` at mount precisely
|
|
286
|
+
# because "works nowhere" is nobody's intention, so a reader that
|
|
287
|
+
# met one anyway would be inventing a claim from a mistake.
|
|
288
|
+
screen=_screens(screen),
|
|
289
|
+
# Only a literal True is a claim. The wire form is `true` or the
|
|
290
|
+
# field is absent, so anything else — 0, "", "no" — is a relay or
|
|
291
|
+
# an app getting it wrong, and the safe reading of a garbled
|
|
292
|
+
# "you can undo this" is that nobody said it.
|
|
293
|
+
reversible=True if reversible is True else None,
|
|
294
|
+
# Only the one value the core mints, for the reason an unrecognised
|
|
295
|
+
# `kind` is dropped: a word this code does not know is a manifest
|
|
296
|
+
# from a newer core, and the safe reading is that this client cannot
|
|
297
|
+
# satisfy the requirement rather than that it can. Dropping it means
|
|
298
|
+
# the tool is rendered without a `plan` property and the call comes
|
|
299
|
+
# back refused — which is visible — where honouring an unknown token
|
|
300
|
+
# name would have this client hoist a field nothing ever fills.
|
|
301
|
+
consumes="plan" if d.get("consumes") == "plan" else None,
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
def to_dict(self) -> dict[str, Any]:
|
|
305
|
+
out: dict[str, Any] = {"name": self.name, "describe": self.describe}
|
|
306
|
+
if self.kind is not None:
|
|
307
|
+
out["kind"] = self.kind
|
|
308
|
+
if self.screen is not None:
|
|
309
|
+
out["screen"] = self.screen if isinstance(self.screen, str) else list(self.screen)
|
|
310
|
+
if self.reversible:
|
|
311
|
+
out["reversible"] = True
|
|
312
|
+
if self.consumes is not None:
|
|
313
|
+
out["consumes"] = self.consumes
|
|
314
|
+
if self.args:
|
|
315
|
+
out["args"] = {k: v.to_dict() for k, v in self.args.items()}
|
|
316
|
+
if self.refuses:
|
|
317
|
+
out["refuses"] = list(self.refuses)
|
|
318
|
+
return out
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
@dataclass(frozen=True)
|
|
323
|
+
class ScreenSpec:
|
|
324
|
+
"""One entry of ``Manifest.screens``: a screen the app has.
|
|
325
|
+
|
|
326
|
+
Mirrors ``ScreenSpec``, whose comment carries the argument. The short
|
|
327
|
+
version: ``Snapshot.screen`` says which screen is live and ``Verb.screen``
|
|
328
|
+
says where a verb works, and between them an agent still could not learn
|
|
329
|
+
that a screen *exists* without standing on it — so the map is declared
|
|
330
|
+
once, in the cached half, because the names of an app's screens are the
|
|
331
|
+
same for every user forever.
|
|
332
|
+
|
|
333
|
+
The rule a reader should not have to go and look up: this is every screen
|
|
334
|
+
the APP has, never the screens the current user may reach. A list filtered
|
|
335
|
+
by role or plan is a per-user list in the cached half, which busts the
|
|
336
|
+
prompt prefix for everybody, and an absence is unfalsifiable — an agent
|
|
337
|
+
that meets one concludes the feature does not exist. "You may not open
|
|
338
|
+
that" is a refusal at the moment of the call.
|
|
339
|
+
"""
|
|
340
|
+
|
|
341
|
+
id: str
|
|
342
|
+
describe: str = ""
|
|
343
|
+
#: An RFC 6570 URI template, or None for a screen with no address. Absent
|
|
344
|
+
#: is not a defect: a modal, a wizard step and the whole of a router-less
|
|
345
|
+
#: app are screens, and ``goto`` reaches them by id like any other.
|
|
346
|
+
url: str | None = None
|
|
347
|
+
#: What the template takes, by its own variable names. Typed so a wrong
|
|
348
|
+
#: value is refused before anybody navigates.
|
|
349
|
+
args: dict[str, ArgSpec] = field(default_factory=dict)
|
|
350
|
+
|
|
351
|
+
@classmethod
|
|
352
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "ScreenSpec":
|
|
353
|
+
url = d.get("url")
|
|
354
|
+
return cls(
|
|
355
|
+
id=str(d["id"]),
|
|
356
|
+
describe=str(d.get("describe", "")),
|
|
357
|
+
url=str(url) if isinstance(url, str) and url else None,
|
|
358
|
+
args={k: ArgSpec.from_dict(v) for k, v in (d.get("args") or {}).items()},
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
def to_dict(self) -> dict[str, Any]:
|
|
362
|
+
out: dict[str, Any] = {"id": self.id, "describe": self.describe}
|
|
363
|
+
if self.url is not None:
|
|
364
|
+
out["url"] = self.url
|
|
365
|
+
if self.args:
|
|
366
|
+
out["args"] = {k: v.to_dict() for k, v in self.args.items()}
|
|
367
|
+
return out
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
@dataclass(frozen=True)
|
|
371
|
+
class FlowStep:
|
|
372
|
+
"""One step of a flow: a verb, and a line about using it here.
|
|
373
|
+
|
|
374
|
+
The shape is webmachinelearning/webmcp#340's ``steps[].{section, tool,
|
|
375
|
+
hint}``, with ``tool`` spelled ``verb`` because that is what this library
|
|
376
|
+
calls the thing. Mirrors ``FlowStep``.
|
|
377
|
+
"""
|
|
378
|
+
|
|
379
|
+
verb: str
|
|
380
|
+
hint: str = ""
|
|
381
|
+
section: str | None = None
|
|
382
|
+
#: ``skipWhen`` on the wire: when this step may be skipped, in one line.
|
|
383
|
+
#: Present means the step is not load-bearing; None means take it.
|
|
384
|
+
#:
|
|
385
|
+
#: A SENTENCE AND NOT AN EXPRESSION, which is the design and not a
|
|
386
|
+
#: shortcut. Nothing in this library executes a flow — the model reads it
|
|
387
|
+
#: and follows it — so the only evaluator a predicate could have is the
|
|
388
|
+
#: reader it is written for, and a predicate read by a model is a sentence
|
|
389
|
+
#: with brackets round it: all of the precision in the notation and none of
|
|
390
|
+
#: it in the enforcement, which is worse than prose because it gets trusted
|
|
391
|
+
#: like a check. The conditions that must be ENFORCED have an enforced home
|
|
392
|
+
#: already: a count threshold before data moves is an approval hook, and
|
|
393
|
+
#: "do not commit a count you have not read" is ``consumes: "plan"``, both
|
|
394
|
+
#: held by the core rather than interpreted by the reader. See
|
|
395
|
+
#: ``FlowStep.skipWhen`` in the TS types for the whole argument.
|
|
396
|
+
skip_when: str | None = None
|
|
397
|
+
|
|
398
|
+
@classmethod
|
|
399
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "FlowStep":
|
|
400
|
+
section = d.get("section")
|
|
401
|
+
skip_when = d.get("skipWhen")
|
|
402
|
+
return cls(
|
|
403
|
+
verb=str(d.get("verb", "")),
|
|
404
|
+
hint=str(d.get("hint", "")),
|
|
405
|
+
section=str(section) if isinstance(section, str) and section else None,
|
|
406
|
+
skip_when=str(skip_when) if isinstance(skip_when, str) and skip_when else None,
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
def to_dict(self) -> dict[str, Any]:
|
|
410
|
+
out: dict[str, Any] = {"verb": self.verb, "hint": self.hint}
|
|
411
|
+
if self.section is not None:
|
|
412
|
+
out["section"] = self.section
|
|
413
|
+
if self.skip_when is not None:
|
|
414
|
+
# Spelled `skipWhen` going out, because the wire name is the
|
|
415
|
+
# contract and `skip_when` is only how Python spells it.
|
|
416
|
+
out["skipWhen"] = self.skip_when
|
|
417
|
+
return out
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
@dataclass(frozen=True)
|
|
421
|
+
class FlowSpec:
|
|
422
|
+
"""One entry of ``Manifest.flows``: a declared, static, readable recipe.
|
|
423
|
+
|
|
424
|
+
**The agent reads it and follows it with ordinary verbs. Nothing calls
|
|
425
|
+
it**, and there is no tool that runs one — see ``FlowSpec`` in the TS
|
|
426
|
+
types for why that is the design and not a missing feature. ``describe``
|
|
427
|
+
is the field a model selects on, which is why it is bounded to one line.
|
|
428
|
+
"""
|
|
429
|
+
|
|
430
|
+
id: str
|
|
431
|
+
describe: str = ""
|
|
432
|
+
steps: tuple[FlowStep, ...] = ()
|
|
433
|
+
|
|
434
|
+
@classmethod
|
|
435
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "FlowSpec":
|
|
436
|
+
return cls(
|
|
437
|
+
id=str(d["id"]),
|
|
438
|
+
describe=str(d.get("describe", "")),
|
|
439
|
+
steps=tuple(FlowStep.from_dict(s) for s in d.get("steps") or ()),
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
def to_dict(self) -> dict[str, Any]:
|
|
443
|
+
return {
|
|
444
|
+
"id": self.id,
|
|
445
|
+
"describe": self.describe,
|
|
446
|
+
"steps": [s.to_dict() for s in self.steps],
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
@dataclass(frozen=True)
|
|
451
|
+
class Manifest:
|
|
452
|
+
"""The static half. Cache this; its ``version`` is how you learn you may
|
|
453
|
+
not."""
|
|
454
|
+
|
|
455
|
+
version: str
|
|
456
|
+
app: str
|
|
457
|
+
verbs: tuple[VerbSpec, ...] = ()
|
|
458
|
+
#: The app's own briefing, in its own words, from ``MountOptions``.
|
|
459
|
+
#:
|
|
460
|
+
#: Static by definition, which is why it rides in the cached half and is
|
|
461
|
+
#: folded into the MCP server instructions rather than into every snapshot.
|
|
462
|
+
#: Before this field the two halves of the library disagreed: a widget
|
|
463
|
+
#: could be handed ``instructions`` and an agent arriving over the relay
|
|
464
|
+
#: could not, so an app that had something to say about itself to both had
|
|
465
|
+
#: to repeat it in ``Snapshot.note`` and pay for it on every turn.
|
|
466
|
+
instructions: str | None = None
|
|
467
|
+
#: Every screen the app has, with an optional RFC 6570 url template each.
|
|
468
|
+
#: Static, cached once, and empty for an app that does not describe its
|
|
469
|
+
#: screens — which is not a claim that it has one. See :class:`ScreenSpec`.
|
|
470
|
+
screens: tuple[ScreenSpec, ...] = ()
|
|
471
|
+
#: The app's multi-step recipes, for an agent to read and follow. Nothing
|
|
472
|
+
#: executes them. See :class:`FlowSpec`.
|
|
473
|
+
flows: tuple[FlowSpec, ...] = ()
|
|
474
|
+
|
|
475
|
+
@classmethod
|
|
476
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "Manifest":
|
|
477
|
+
instructions = d.get("instructions")
|
|
478
|
+
return cls(
|
|
479
|
+
version=d.get("version", ""),
|
|
480
|
+
app=d.get("app", ""),
|
|
481
|
+
verbs=tuple(VerbSpec.from_dict(v) for v in d.get("verbs") or ()),
|
|
482
|
+
instructions=str(instructions) if instructions else None,
|
|
483
|
+
# A screen with no `id` is dropped rather than coerced: the id is
|
|
484
|
+
# the string `Verb.screen` and `Snapshot.screen` are compared
|
|
485
|
+
# against, and a screen that cannot be matched is worse than a
|
|
486
|
+
# screen that is not listed. The core refuses one at mount, so a
|
|
487
|
+
# reader that meets one is reading a hand-rolled manifest.
|
|
488
|
+
screens=tuple(
|
|
489
|
+
ScreenSpec.from_dict(s) for s in d.get("screens") or () if s.get("id")
|
|
490
|
+
),
|
|
491
|
+
flows=tuple(FlowSpec.from_dict(f) for f in d.get("flows") or () if f.get("id")),
|
|
492
|
+
)
|
|
493
|
+
|
|
494
|
+
def to_dict(self) -> dict[str, Any]:
|
|
495
|
+
out: dict[str, Any] = {
|
|
496
|
+
"version": self.version,
|
|
497
|
+
"app": self.app,
|
|
498
|
+
"verbs": [v.to_dict() for v in self.verbs],
|
|
499
|
+
}
|
|
500
|
+
# Omitted rather than sent as null, like every other absent optional
|
|
501
|
+
# here: the TS core computes the manifest version from a shape that
|
|
502
|
+
# leaves it out, and a round trip through this class must not turn
|
|
503
|
+
# "the app said nothing" into a field.
|
|
504
|
+
if self.instructions is not None:
|
|
505
|
+
out["instructions"] = self.instructions
|
|
506
|
+
# Omitted when empty, for the reason `instructions` is: the TS core
|
|
507
|
+
# fingerprints a shape that leaves them out, and a round trip through
|
|
508
|
+
# this class must not turn "the app does not describe its screens" into
|
|
509
|
+
# an empty list that says it has none.
|
|
510
|
+
if self.screens:
|
|
511
|
+
out["screens"] = [s.to_dict() for s in self.screens]
|
|
512
|
+
if self.flows:
|
|
513
|
+
out["flows"] = [f.to_dict() for f in self.flows]
|
|
514
|
+
return out
|
|
515
|
+
|
|
516
|
+
def screen(self, screen_id: str) -> "ScreenSpec | None":
|
|
517
|
+
for s in self.screens:
|
|
518
|
+
if s.id == screen_id:
|
|
519
|
+
return s
|
|
520
|
+
return None
|
|
521
|
+
|
|
522
|
+
def flow(self, flow_id: str) -> "FlowSpec | None":
|
|
523
|
+
for f in self.flows:
|
|
524
|
+
if f.id == flow_id:
|
|
525
|
+
return f
|
|
526
|
+
return None
|
|
527
|
+
|
|
528
|
+
def verb(self, name: str) -> VerbSpec | None:
|
|
529
|
+
for v in self.verbs:
|
|
530
|
+
if v.name == name:
|
|
531
|
+
return v
|
|
532
|
+
return None
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
@dataclass(frozen=True)
|
|
536
|
+
class Row:
|
|
537
|
+
"""One row of whatever the screen is showing.
|
|
538
|
+
|
|
539
|
+
TS writes this as ``{ id: string; [column: string]: Scalar | undefined }``.
|
|
540
|
+
Python cannot express an index signature on a dataclass, so the other
|
|
541
|
+
columns live in ``columns`` and ``to_dict`` flattens them back beside
|
|
542
|
+
``id``. The flat form is what goes over the wire; the nested one exists
|
|
543
|
+
only inside this process.
|
|
544
|
+
"""
|
|
545
|
+
|
|
546
|
+
id: str
|
|
547
|
+
columns: dict[str, Scalar] = field(default_factory=dict)
|
|
548
|
+
|
|
549
|
+
@classmethod
|
|
550
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "Row":
|
|
551
|
+
return cls(id=str(d.get("id", "")), columns={k: v for k, v in d.items() if k != "id"})
|
|
552
|
+
|
|
553
|
+
def to_dict(self) -> dict[str, Any]:
|
|
554
|
+
return {"id": self.id, **self.columns}
|
|
555
|
+
|
|
556
|
+
def __getitem__(self, column: str) -> Scalar:
|
|
557
|
+
return self.id if column == "id" else self.columns.get(column)
|
|
558
|
+
|
|
559
|
+
def get(self, column: str, default: Scalar = None) -> Scalar:
|
|
560
|
+
if column == "id":
|
|
561
|
+
return self.id
|
|
562
|
+
return self.columns.get(column, default)
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
@dataclass(frozen=True)
|
|
566
|
+
class Page:
|
|
567
|
+
"""``Snapshot.page``: the window these rows are a view of.
|
|
568
|
+
|
|
569
|
+
``total`` is OPTIONAL, and the three states it makes are the ones the TS
|
|
570
|
+
``Snapshot.page`` comment pins:
|
|
571
|
+
|
|
572
|
+
page absent these are all the rows there are
|
|
573
|
+
page, total given a window, and you know how big the whole is
|
|
574
|
+
page, total absent a window, and you do NOT know how big the whole is
|
|
575
|
+
|
|
576
|
+
The third is not an edge case, it is the normal answer for any app whose
|
|
577
|
+
rows are read out of a rendered table: the DOM cannot tell a virtualised
|
|
578
|
+
grid from a short list, so code that had to supply a total could only omit
|
|
579
|
+
``page`` — asserting completeness it cannot know — or invent one. Requiring it here made the field unrepresentable from the wire
|
|
580
|
+
anyway: ``int(d["total"])`` raised ``KeyError`` on the commonest snapshot a
|
|
581
|
+
DOM-adapted app produces, before the renderer was ever reached.
|
|
582
|
+
"""
|
|
583
|
+
|
|
584
|
+
index: int
|
|
585
|
+
size: int
|
|
586
|
+
total: int | None = None
|
|
587
|
+
|
|
588
|
+
@classmethod
|
|
589
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "Page":
|
|
590
|
+
total = d.get("total")
|
|
591
|
+
return cls(
|
|
592
|
+
index=int(d["index"]),
|
|
593
|
+
size=int(d["size"]),
|
|
594
|
+
# `is None` rather than falsy: a total of 0 is a real answer — an
|
|
595
|
+
# empty window onto an empty whole — and distinct from "I do not
|
|
596
|
+
# count", which is what absence means.
|
|
597
|
+
total=int(total) if total is not None else None,
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
def to_dict(self) -> dict[str, Any]:
|
|
601
|
+
out: dict[str, Any] = {"index": self.index, "size": self.size}
|
|
602
|
+
# Omitted rather than sent as null, so a round trip through this class
|
|
603
|
+
# cannot turn "unknown" into a field a stricter parser rejects.
|
|
604
|
+
if self.total is not None:
|
|
605
|
+
out["total"] = self.total
|
|
606
|
+
return out
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
@dataclass(frozen=True)
|
|
610
|
+
class Snapshot:
|
|
611
|
+
"""What a human could see right now. Mirrors ``Snapshot``.
|
|
612
|
+
|
|
613
|
+
``page`` stays ``None`` when the relay did not send one, and that absence
|
|
614
|
+
is meaningful rather than missing data: per the TS comment it is a claim
|
|
615
|
+
that the agent is seeing everything. Defaulting it to a synthetic
|
|
616
|
+
``{0, len(rows), len(rows)}`` would manufacture that claim, so we don't.
|
|
617
|
+
"""
|
|
618
|
+
|
|
619
|
+
where: str
|
|
620
|
+
rev: int
|
|
621
|
+
rows: tuple[Row, ...] | None = None
|
|
622
|
+
page: Page | None = None
|
|
623
|
+
selected: tuple[str, ...] | None = None
|
|
624
|
+
#: ``selectedCount`` on the wire: how many rows are selected in all.
|
|
625
|
+
#:
|
|
626
|
+
#: ``selected`` carries only the ids ON SCREEN, exactly like ``rows``, so
|
|
627
|
+
#: that ``select-where`` can select thousands without putting tens of
|
|
628
|
+
#: kilobytes of ids into the uncached half of every later result — which on
|
|
629
|
+
#: a big table goes straight through the relay's 1 MiB body limit. This is
|
|
630
|
+
#: the honest size that ``selected`` therefore cannot be.
|
|
631
|
+
#:
|
|
632
|
+
#: ``None`` means "I do not count" and is distinct from ``0``, which is a
|
|
633
|
+
#: real answer. Dropping it here was not a cosmetic loss: an agent handed
|
|
634
|
+
#: three ids and a bulk commit, with no count, believes three rows are
|
|
635
|
+
#: about to change.
|
|
636
|
+
selected_count: int | None = None
|
|
637
|
+
note: str | None = None
|
|
638
|
+
#: Which screen that is, as a stable kebab-case id: ``contacts``,
|
|
639
|
+
#: ``company``. The partner of ``where``, which is prose for a human and
|
|
640
|
+
#: carries the filter, the sort and a drill-down's record description — so
|
|
641
|
+
#: it moves constantly for reasons that are not navigation, and "did I
|
|
642
|
+
#: navigate?" cannot be answered by diffing it. This can be compared with
|
|
643
|
+
#: ``==``, and that is its whole job.
|
|
644
|
+
#:
|
|
645
|
+
#: Dynamic, which is why it is here: the names of an app's screens and
|
|
646
|
+
#: which verb belongs to which are static and live on ``VerbSpec.screen``;
|
|
647
|
+
#: which one is live is as per-turn as a row. None means the app does not
|
|
648
|
+
#: describe its screens, not that it has one.
|
|
649
|
+
#:
|
|
650
|
+
#: Declared LAST although it renders FIRST, and beside ``where`` in the TS
|
|
651
|
+
#: `Snapshot`. Every field of this dataclass is positional, so inserting
|
|
652
|
+
#: one after ``rev`` would silently reinterpret ``Snapshot("contacts", 7,
|
|
653
|
+
#: rows)`` — written against yesterday's version — as a screen id. New
|
|
654
|
+
#: fields go on the end here, whatever order they are documented in.
|
|
655
|
+
screen: str | None = None
|
|
656
|
+
#: An opaque fingerprint of the WHOLE current selection, off-screen rows
|
|
657
|
+
#: included: the one input the core's plan token is built from.
|
|
658
|
+
#:
|
|
659
|
+
#: Equal when the selection has not changed and different when it has,
|
|
660
|
+
#: including when one row was swapped for another — which is the case
|
|
661
|
+
#: ``selected_count`` cannot see and ``selected``, which by design holds
|
|
662
|
+
#: only the ids on screen, cannot identify. Nothing parses it, nothing
|
|
663
|
+
#: orders it and no model is shown it. See ``Snapshot.selectionId``.
|
|
664
|
+
#:
|
|
665
|
+
#: On the end, like ``screen`` and for the same reason: every field of this
|
|
666
|
+
#: dataclass is positional, so a field inserted in the middle silently
|
|
667
|
+
#: reinterprets a ``Snapshot(...)`` written against an earlier version.
|
|
668
|
+
selection_id: str | None = None
|
|
669
|
+
|
|
670
|
+
@classmethod
|
|
671
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "Snapshot":
|
|
672
|
+
rows = d.get("rows")
|
|
673
|
+
selected = d.get("selected")
|
|
674
|
+
page = d.get("page")
|
|
675
|
+
count = d.get("selectedCount")
|
|
676
|
+
screen = d.get("screen")
|
|
677
|
+
return cls(
|
|
678
|
+
where=d.get("where", ""),
|
|
679
|
+
rev=int(d.get("rev", 0)),
|
|
680
|
+
screen=str(screen) if screen else None,
|
|
681
|
+
rows=tuple(Row.from_dict(r) for r in rows) if rows is not None else None,
|
|
682
|
+
page=Page.from_dict(page) if page else None,
|
|
683
|
+
selected=tuple(str(s) for s in selected) if selected is not None else None,
|
|
684
|
+
selected_count=int(count) if count is not None else None,
|
|
685
|
+
note=d.get("note"),
|
|
686
|
+
# Carried through as a string and never interpreted. It is the
|
|
687
|
+
# app's own fingerprint and only the core that minted a token
|
|
688
|
+
# against it compares it, so a client that read anything out of it
|
|
689
|
+
# would be inventing a second opinion about staleness.
|
|
690
|
+
selection_id=str(d["selectionId"]) if d.get("selectionId") else None,
|
|
691
|
+
)
|
|
692
|
+
|
|
693
|
+
def to_dict(self) -> dict[str, Any]:
|
|
694
|
+
out: dict[str, Any] = {"where": self.where, "rev": self.rev}
|
|
695
|
+
if self.screen is not None:
|
|
696
|
+
out["screen"] = self.screen
|
|
697
|
+
if self.rows is not None:
|
|
698
|
+
out["rows"] = [r.to_dict() for r in self.rows]
|
|
699
|
+
if self.page is not None:
|
|
700
|
+
out["page"] = self.page.to_dict()
|
|
701
|
+
if self.selected is not None:
|
|
702
|
+
out["selected"] = list(self.selected)
|
|
703
|
+
if self.selected_count is not None:
|
|
704
|
+
# Spelled `selectedCount` going out, because the wire name is the
|
|
705
|
+
# contract and `selected_count` is only how Python spells it.
|
|
706
|
+
out["selectedCount"] = self.selected_count
|
|
707
|
+
if self.selection_id is not None:
|
|
708
|
+
out["selectionId"] = self.selection_id
|
|
709
|
+
if self.note is not None:
|
|
710
|
+
out["note"] = self.note
|
|
711
|
+
return out
|
|
712
|
+
|
|
713
|
+
def columns(self) -> list[str]:
|
|
714
|
+
"""The column order for rendering: ``id`` first, then every other key
|
|
715
|
+
in the order it was first seen. Rows are not required to agree on their
|
|
716
|
+
keys, so this is a union and not ``rows[0].keys()``."""
|
|
717
|
+
seen: list[str] = ["id"]
|
|
718
|
+
for row in self.rows or ():
|
|
719
|
+
for key in row.columns:
|
|
720
|
+
if key not in seen:
|
|
721
|
+
seen.append(key)
|
|
722
|
+
return seen
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
@dataclass(frozen=True)
|
|
726
|
+
class Result:
|
|
727
|
+
"""What the agent gets back. Mirrors ``Result``, widened to ``WireResult``.
|
|
728
|
+
|
|
729
|
+
``snapshot`` is the screen AFTER the call — on purpose, so acting and
|
|
730
|
+
observing are one round trip. Anything that renders a Result for a model
|
|
731
|
+
and drops this has thrown away the reason the shape is like that.
|
|
732
|
+
|
|
733
|
+
``count`` is how a ``describe`` verb reports what it matched. It is the
|
|
734
|
+
number a human would have to see before pressing a bulk button, and the
|
|
735
|
+
whole reason ``describe`` and ``commit`` are separate verbs, so a renderer
|
|
736
|
+
that buries it has removed the safety the split was bought with.
|
|
737
|
+
|
|
738
|
+
``snapshot`` is typed optional, where the TS ``Result`` has it required.
|
|
739
|
+
That is not a weakening of the contract: a driver mounted in a page always
|
|
740
|
+
fills it, and this type is the ``WireResult`` an agent receives, where a
|
|
741
|
+
relay refusal about a call that never reached a tab has no screen to
|
|
742
|
+
report. Its absence means "nothing ran and nobody looked", never "the
|
|
743
|
+
screen is empty" — see :meth:`relay_refusal`.
|
|
744
|
+
"""
|
|
745
|
+
|
|
746
|
+
ok: bool
|
|
747
|
+
snapshot: Snapshot | None = None
|
|
748
|
+
note: str | None = None # ok=True only
|
|
749
|
+
count: int | None = None # ok=True only, and only from a `describe` verb
|
|
750
|
+
#: The plan token the core minted for the selection this call left behind:
|
|
751
|
+
#: ok=True only, and only from a ``describe`` in an app that has a verb
|
|
752
|
+
#: consuming them. Pass it back as ``call(..., plan=...)``.
|
|
753
|
+
#:
|
|
754
|
+
#: It travels beside ``count`` because it means nothing without it — the
|
|
755
|
+
#: count is the number a person approves, and this is what binds that number
|
|
756
|
+
#: to the set the next commit touches. Opaque: stored and forwarded, never
|
|
757
|
+
#: parsed.
|
|
758
|
+
plan: str | None = None # ok=True only
|
|
759
|
+
refused: str | None = None # ok=False only
|
|
760
|
+
hint: str | None = None # ok=False only
|
|
761
|
+
#: How long the TAB's own work took, in whole milliseconds, with any
|
|
762
|
+
#: person's time already taken out of it — the figure the relay carried
|
|
763
|
+
#: from the ``result`` frame, measured in the page around its one call into
|
|
764
|
+
#: the driver.
|
|
765
|
+
#:
|
|
766
|
+
#: ``None`` IS UNKNOWN AND NOT ZERO, the rule :attr:`Tab.focus` already
|
|
767
|
+
#: has. A tab from before the field existed, a hand-written ``fetch`` loop,
|
|
768
|
+
#: and a tab that could not isolate a person's wait all say nothing — and a
|
|
769
|
+
#: client that read silence as zero would tell a model that every one of
|
|
770
|
+
#: those apps is instant. Render nothing for it; see
|
|
771
|
+
#: :func:`driverkit.render.render_duration`, which this is the one honest
|
|
772
|
+
#: input to over the relay.
|
|
773
|
+
took_ms: int | None = None # wire name: tookMs
|
|
774
|
+
#: How much of the wall clock was a PERSON, as the amount already
|
|
775
|
+
#: subtracted from :attr:`took_ms`. Present only when somebody was actually
|
|
776
|
+
#: waited on, and never added back: it is here so the gap between this
|
|
777
|
+
#: client's own round trip and ``took_ms`` is accounted for rather than
|
|
778
|
+
#: guessed at, and a renderer that added the two would be reporting a
|
|
779
|
+
#: confirmation dialog as the app being slow.
|
|
780
|
+
waited_ms: int | None = None # wire name: waitedMs
|
|
781
|
+
|
|
782
|
+
@classmethod
|
|
783
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "Result":
|
|
784
|
+
snapshot = d.get("snapshot")
|
|
785
|
+
ok = bool(d.get("ok"))
|
|
786
|
+
count = d.get("count")
|
|
787
|
+
plan = d.get("plan")
|
|
788
|
+
took_ms = _duration(d.get("tookMs"))
|
|
789
|
+
return cls(
|
|
790
|
+
ok=ok,
|
|
791
|
+
snapshot=Snapshot.from_dict(snapshot) if isinstance(snapshot, Mapping) else None,
|
|
792
|
+
note=d.get("note") if ok else None,
|
|
793
|
+
count=int(count) if ok and isinstance(count, (int, float)) and not isinstance(count, bool) else None,
|
|
794
|
+
plan=str(plan) if ok and isinstance(plan, str) and plan else None,
|
|
795
|
+
refused=None if ok else str(d.get("refused", "unknown")),
|
|
796
|
+
hint=d.get("hint") if not ok else None,
|
|
797
|
+
took_ms=took_ms,
|
|
798
|
+
# Only beside a `took_ms`, never on its own: it is defined as the
|
|
799
|
+
# amount already subtracted from that figure, so without one it is a
|
|
800
|
+
# quantity relative to nothing and a reader might subtract it twice.
|
|
801
|
+
waited_ms=_duration(d.get("waitedMs")) if took_ms is not None else None,
|
|
802
|
+
)
|
|
803
|
+
|
|
804
|
+
def to_dict(self) -> dict[str, Any]:
|
|
805
|
+
out: dict[str, Any] = {"ok": self.ok}
|
|
806
|
+
if self.ok:
|
|
807
|
+
if self.note is not None:
|
|
808
|
+
out["note"] = self.note
|
|
809
|
+
if self.count is not None:
|
|
810
|
+
out["count"] = self.count
|
|
811
|
+
if self.plan is not None:
|
|
812
|
+
out["plan"] = self.plan
|
|
813
|
+
else:
|
|
814
|
+
out["refused"] = self.refused
|
|
815
|
+
if self.hint is not None:
|
|
816
|
+
out["hint"] = self.hint
|
|
817
|
+
if self.snapshot is not None:
|
|
818
|
+
out["snapshot"] = self.snapshot.to_dict()
|
|
819
|
+
# Written beside `ok` rather than inside anything, because that is where
|
|
820
|
+
# the relay puts them: they are the relay's copy of the tab's own
|
|
821
|
+
# measurement and are reserved on this body, never the app's to fill in.
|
|
822
|
+
# See PROTOCOL.md on the `result` frame.
|
|
823
|
+
if self.took_ms is not None:
|
|
824
|
+
out["tookMs"] = self.took_ms
|
|
825
|
+
if self.waited_ms is not None:
|
|
826
|
+
out["waitedMs"] = self.waited_ms
|
|
827
|
+
return out
|
|
828
|
+
|
|
829
|
+
@property
|
|
830
|
+
def relay_refusal(self) -> str | None:
|
|
831
|
+
"""The relay refusal slug, when this is one, else None.
|
|
832
|
+
|
|
833
|
+
Both documented signals are accepted: a slug from the closed set, and a
|
|
834
|
+
missing snapshot. A conforming relay sends them together. Requiring
|
|
835
|
+
both would mean one sloppy relay turns "your call never landed" into
|
|
836
|
+
"your app said no", which is the single most expensive confusion this
|
|
837
|
+
client can make — it is the difference between retrying and not knowing
|
|
838
|
+
whether a commit already happened.
|
|
839
|
+
"""
|
|
840
|
+
if self.ok:
|
|
841
|
+
return None
|
|
842
|
+
if self.refused in RELAY_REFUSALS:
|
|
843
|
+
return self.refused
|
|
844
|
+
if self.snapshot is None:
|
|
845
|
+
return self.refused
|
|
846
|
+
return None
|
|
847
|
+
|
|
848
|
+
@property
|
|
849
|
+
def landed(self) -> bool:
|
|
850
|
+
"""False only when the call provably never reached a tab.
|
|
851
|
+
|
|
852
|
+
That is ``no-such-tab`` alone. ``timeout`` and ``tab-went-away`` are
|
|
853
|
+
both deliberately absent: each means the call was accepted and no
|
|
854
|
+
answer came back, and the verb may well have run — the tab went quiet,
|
|
855
|
+
or its stream closed and the answer died with it. Collapsing unknown
|
|
856
|
+
into failed is how a bulk accept gets made twice.
|
|
857
|
+
"""
|
|
858
|
+
return self.relay_refusal not in RELAY_REFUSALS or self.relay_refusal in UNKNOWN_OUTCOME
|
|
859
|
+
|
|
860
|
+
@property
|
|
861
|
+
def outcome_unknown(self) -> bool:
|
|
862
|
+
"""True when nobody can say whether the verb ran."""
|
|
863
|
+
return self.relay_refusal in UNKNOWN_OUTCOME
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
@dataclass(frozen=True)
|
|
867
|
+
class Tab:
|
|
868
|
+
"""One row of ``GET /dk/tabs``.
|
|
869
|
+
|
|
870
|
+
The listing is now spelled out in PROTOCOL.md: the ``{"tabs": [...]}``
|
|
871
|
+
envelope, the id field named ``tab`` — the same name the ``hello`` frame
|
|
872
|
+
uses, one name for one thing — and ``manifestVersion`` lifted to the top
|
|
873
|
+
level so a client can tell whether its cached manifest is current without
|
|
874
|
+
fetching the whole thing.
|
|
875
|
+
|
|
876
|
+
``from_dict`` still reads the spellings a relay author might plausibly have
|
|
877
|
+
picked before that was written. Tolerance here costs a few lines and buys
|
|
878
|
+
compatibility with relays built against the earlier prose; it is not a
|
|
879
|
+
licence to emit them, and ``to_dict`` writes only the specified shape.
|
|
880
|
+
"""
|
|
881
|
+
|
|
882
|
+
id: str
|
|
883
|
+
app: str = ""
|
|
884
|
+
title: str = ""
|
|
885
|
+
url: str = ""
|
|
886
|
+
manifest_version: str = "" # wire name: manifestVersion
|
|
887
|
+
#: The tab's current ``rev``, so a client can see the screen moved without
|
|
888
|
+
#: reading the snapshot.
|
|
889
|
+
rev: int | None = None
|
|
890
|
+
#: Where this tab stands in the person's attention, as the page itself last
|
|
891
|
+
#: reported it: ``"focused"`` (the active tab of the frontmost window — the
|
|
892
|
+
#: person is looking at this), ``"visible"`` (the active tab of its window,
|
|
893
|
+
#: but the person is in another application) or ``"hidden"`` (a background
|
|
894
|
+
#: tab).
|
|
895
|
+
#:
|
|
896
|
+
#: ``None`` IS UNKNOWN AND NOT HIDDEN. A tab driven by a hand-written loop,
|
|
897
|
+
#: or a page that opted out, says nothing — and a client that read silence
|
|
898
|
+
#: as "not the one" would quietly stop being able to drive it. See
|
|
899
|
+
#: :func:`driverkit.mcp_core.pick_tab`, which is the one place that rule is
|
|
900
|
+
#: applied.
|
|
901
|
+
focus: str | None = None
|
|
902
|
+
#: When the tab connected, ISO-8601, as the relay reported it. Kept as a
|
|
903
|
+
#: string rather than parsed: nothing here needs it as a datetime, and
|
|
904
|
+
#: parsing would invent a failure mode for a field only ever shown to a
|
|
905
|
+
#: human choosing between two tabs.
|
|
906
|
+
since: str | None = None
|
|
907
|
+
raw: dict[str, Any] = field(default_factory=dict, repr=False, compare=False)
|
|
908
|
+
|
|
909
|
+
@classmethod
|
|
910
|
+
def from_dict(cls, d: Mapping[str, Any]) -> "Tab":
|
|
911
|
+
version = d.get("manifestVersion") or d.get("manifest_version")
|
|
912
|
+
if version is None:
|
|
913
|
+
nested = d.get("manifest")
|
|
914
|
+
if isinstance(nested, Mapping):
|
|
915
|
+
version = nested.get("version")
|
|
916
|
+
rev = d.get("rev")
|
|
917
|
+
return cls(
|
|
918
|
+
id=str(d.get("tab") or d.get("id") or ""),
|
|
919
|
+
app=str(d.get("app") or ""),
|
|
920
|
+
title=str(d.get("title") or ""),
|
|
921
|
+
url=str(d.get("url") or ""),
|
|
922
|
+
manifest_version=str(version or ""),
|
|
923
|
+
rev=int(rev) if isinstance(rev, (int, float)) and not isinstance(rev, bool) else None,
|
|
924
|
+
focus=d.get("focus") if isinstance(d.get("focus"), str) else None,
|
|
925
|
+
since=d.get("since"),
|
|
926
|
+
raw=dict(d),
|
|
927
|
+
)
|
|
928
|
+
|
|
929
|
+
def to_dict(self) -> dict[str, Any]:
|
|
930
|
+
out: dict[str, Any] = {
|
|
931
|
+
"tab": self.id,
|
|
932
|
+
"app": self.app,
|
|
933
|
+
"title": self.title,
|
|
934
|
+
"url": self.url,
|
|
935
|
+
"manifestVersion": self.manifest_version,
|
|
936
|
+
}
|
|
937
|
+
if self.rev is not None:
|
|
938
|
+
out["rev"] = self.rev
|
|
939
|
+
if self.focus is not None:
|
|
940
|
+
out["focus"] = self.focus
|
|
941
|
+
if self.since is not None:
|
|
942
|
+
out["since"] = self.since
|
|
943
|
+
return out
|