mercury-cli-sdk 0.1.0__tar.gz → 0.2.0__tar.gz
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.
- mercury_cli_sdk-0.2.0/PKG-INFO +326 -0
- mercury_cli_sdk-0.2.0/README.md +303 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/pyproject.toml +1 -1
- mercury_cli_sdk-0.2.0/src/mercury_cli_sdk/__init__.py +14 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/client.py +112 -33
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/errors.py +20 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/framing.py +7 -3
- mercury_cli_sdk-0.2.0/src/mercury_cli_sdk/generated.py +2043 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/helpers.py +17 -4
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/parse.py +2 -2
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/runner.py +98 -42
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/sync.py +11 -3
- mercury_cli_sdk-0.2.0/src/mercury_cli_sdk/version.py +1 -0
- mercury_cli_sdk-0.1.0/PKG-INFO +0 -161
- mercury_cli_sdk-0.1.0/README.md +0 -138
- mercury_cli_sdk-0.1.0/src/mercury_cli_sdk/__init__.py +0 -14
- mercury_cli_sdk-0.1.0/src/mercury_cli_sdk/generated.py +0 -1656
- mercury_cli_sdk-0.1.0/src/mercury_cli_sdk/version.py +0 -1
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/LICENSE +0 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/build_backend.py +0 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/executable.py +0 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/py.typed +0 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/structured.py +0 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/support.py +0 -0
- {mercury_cli_sdk-0.1.0 → mercury_cli_sdk-0.2.0}/src/mercury_cli_sdk/testing.py +0 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mercury-cli-sdk
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Mercury CLI clients for Python: typed run streams, hosted sessions and zero-cost fixtures.
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: mercury-cli,mercury
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Framework :: AsyncIO
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Project-URL: Homepage, https://mercury-cli.ai
|
|
22
|
+
Project-URL: Repository, https://github.com/Whq02/MercuryCLI
|
|
23
|
+
|
|
24
|
+
# Mercury CLI SDK
|
|
25
|
+
|
|
26
|
+
`mercury-cli-sdk` 0.2.0 is a zero-runtime-dependency Python client for Mercury CLI, requiring Python 3.11 or later. Install Mercury separately and sign in through the CLI; the SDK uses its configuration and credentials. The package is Apache-2.0 and versioned separately from the CLI.
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
python -m pip install mercury-cli-sdk
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
On Windows run that as `py -3 -m pip install mercury-cli-sdk` (or with the full path of `python.exe`): the `python3` name there is often the Store stub, which answers with exit code 9009. If `python -m venv` fails because `ensurepip` is missing, install `python3-venv` (Debian and Ubuntu: `apt install python3-venv`), or create the environment with `--without-pip` and install into it from an interpreter that has pip: `python -m pip --python .venv/bin/python install mercury-cli-sdk`.
|
|
33
|
+
|
|
34
|
+
## Quickstart
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from mercury_cli_sdk import run_sync
|
|
38
|
+
|
|
39
|
+
print(run_sync('Explain the README').outcome.get('answer'))
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
For asynchronous programs and streaming, use `run`.
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import asyncio
|
|
46
|
+
from mercury_cli_sdk import run
|
|
47
|
+
|
|
48
|
+
async def main():
|
|
49
|
+
task = await run('Explain the README', partial=True)
|
|
50
|
+
async for row in task:
|
|
51
|
+
if row['type'] == 'text_delta':
|
|
52
|
+
print(row['text'], end='', flush=True)
|
|
53
|
+
result = await task.outcome
|
|
54
|
+
print(result['status'], result.get('answer'))
|
|
55
|
+
asyncio.run(main())
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`run()` returns an async row iterator with listeners. Its `session`, `outcome` and `exit` are awaitable; `rows` retains received rows. Whole blocks arrive as `text`; `partial=True` also streams `text_delta`. `outcome` settles after the process exits and owned tool servers close. `run_sync` returns `RunResult(outcome, rows, exit)`. `connect_sync` returns a typed `SyncSession` with the same control methods and blocking row iteration. Use it as a context manager; host callbacks run on its background loop and must not call the synchronous wrapper recursively.
|
|
59
|
+
|
|
60
|
+
## Bound retries and show progress
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import asyncio
|
|
64
|
+
from mercury_cli_sdk import run
|
|
65
|
+
|
|
66
|
+
async def main():
|
|
67
|
+
task = await run('Explain the README', max_time='2s')
|
|
68
|
+
async for row in task:
|
|
69
|
+
if row['type'] == 'wait' and row['state'] == 'retry':
|
|
70
|
+
print('Retry', row.get('attempt'), 'in', row.get('delay_ms'), 'ms')
|
|
71
|
+
result = await task.outcome
|
|
72
|
+
print(result['status'])
|
|
73
|
+
asyncio.run(main())
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Without a time bound or cancellation, persistent rate limits keep the run waiting through the CLI's retries; no outcome is available while those retries continue.
|
|
77
|
+
|
|
78
|
+
## Model selection
|
|
79
|
+
|
|
80
|
+
`model` uses the CLI's model ids and credentials. For a named OpenAI-compatible endpoint, pass `compat/<endpoint>/<model>` to `run`, `run_sync` or `connect`, for example `run_sync('Explain the README', model='compat/my-server/model-id')`. Replace the example with an available row from `/model` or `mercury://models`; `/llmhost` registers a server as a named compat endpoint. The same route works in `define_agent({'description': 'Reader', 'prompt': 'Read the project', 'model': 'compat/my-server/model-id'})` and `await session.set_model('compat/my-server/model-id')`.
|
|
81
|
+
|
|
82
|
+
With Mercury 1.0.0-beta.42 or newer, `compat`, `local` and `huggingface` also select through the CLI's resolver for that family. An unavailable class or an unknown named compat endpoint/model is refused, not replaced by another provider. A resumed session retains its recorded model unless you explicitly supply another; an unavailable recorded model refuses the resume. Tool results, streamed rows and structured output use the same SDK interfaces on compat and Anthropic models. Reasoning and usage rows report what the engine supplies; model capabilities remain the engine's.
|
|
83
|
+
|
|
84
|
+
## Host a session
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
import asyncio
|
|
88
|
+
from mercury_cli_sdk import connect
|
|
89
|
+
|
|
90
|
+
async def main():
|
|
91
|
+
async with await connect(on_permission_request=lambda ask, context: {'outcome': 'deny', 'message': 'Read-only host'}) as session:
|
|
92
|
+
print((await session.prompt('Inspect the tests'))['status'])
|
|
93
|
+
print((await session.prompt('Explain the result')).get('answer'))
|
|
94
|
+
print((await session.facts())['usage']['total_cost_usd'])
|
|
95
|
+
asyncio.run(main())
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`prompt` awaits its own correlated turn. Only one prompt wait may be active; `steer(content, id=..., schema=...)` queues priority `now` and `follow_up(content, id=..., schema=...)` queues `later`; both keyword options are optional. `send_prompt`, `send_shell` and `send_note` expose ids and all priorities; `withdraw(id)` removes queued input, and a `prompt` waiting on it raises `MercuryError('prompt: withdrawn')`. Cancelling the awaiting task cancels the wait, not the turn; `interrupt()` ends the turn. A prompt sent while a turn is still running (after a timed-out or cancelled wait, for instance) is the CLI's to place: with no tool round left it waits and runs as the next turn, which `prompt` resolves with; at a tool-round boundary the CLI folds it into the running turn and marks its reply `in_reply_to` that prompt, and `prompt` resolves with that turn's outcome instead (a per-turn `schema` on a folded prompt is not applied). When that turn ends without any reply and the CLI's `facts` list the prompt neither queued nor in a turn, `prompt` raises within five seconds `MercuryError('prompt: turn N ended STATUS and the runner holds this prompt neither queued nor in a turn; …')` rather than waiting forever; `interrupt()` the running turn, or await its outcome row, before prompting again when you need a turn of its own. Typed `request(method, params, timeout=seconds)` and `notify` cover protocol 1, including pause, quiesce, leave, MCP and schedules. `timeout=None` disables its timer. `connect(timeout=seconds)` bounds a boot that never answers; cancelling the awaiting task ends the child too.
|
|
99
|
+
|
|
100
|
+
`on_permission_request` and `on_elicitation` receive `RequestContext` with `id` and a `cancelled` event; absent handlers deny and decline respectively. `on_read_text_file` and `on_write_text_file` route native file tools to host buffers and infer their capabilities. The equivalent `handlers` entries work too. Supplying all five `terminal/*` handlers infers terminal support; partial terminal sets do not. Explicit `capabilities` override inference.
|
|
101
|
+
|
|
102
|
+
### Checkpoints, warm workers and control
|
|
103
|
+
|
|
104
|
+
`connect` asks the CLI to capture file checkpoints (`file_checkpoints: true` on `initialize`); `checkpoints=False` opts out. The CLI answers with what it does: `session.file_checkpoints` (the `initialize` answer) and `(await session.facts())['file_checkpoints']` carry `capture` and `source`, where `initialize` means the request is honoured, `setting` that the CLI's file-checkpoint setting turned it off, `headless` that nothing asked, and `seat` or `interactive` name the CLI's own sessions; `facts` adds `restorable`, the user-message ids a rewind can return to. File checkpoints over `initialize` need Mercury 1.0.0-beta.41 or newer: an older runner answers no `file_checkpoints`, so `session.file_checkpoints` is `None`, `session.supports['file_checkpoints']` is False and `facts()['file_checkpoints']` reads `capture: False` with no `source`. `run` has no `initialize` and captures nothing. After any accepted prompt input, including `send_prompt`, `steer` and `follow_up`, `last_prompt_id` is its user-message id. Pass it to `rewind(user_message_id, 'both', dry_run=True)`, then omit `dry_run` to restore. Modes are `code`, `conversation` and `both`; the typed receipt reports changed files and removed conversation rows.
|
|
105
|
+
|
|
106
|
+
`prewarm(**options)` or `connect(warm=True)` returns an unclaimed runner. On `MercurySession` and `SyncSession`, `session.session_id` reflects `initialized['session_id']`: both are None until `claim(session_id=..., model=..., effort=..., mode=..., resume=...)` succeeds and updates them from the CLI's reply. Only `session_id` is required. Prompt input before a claim fails immediately with `MercuryError('session is unclaimed; call claim() before prompting')`. Warm workers reject boot-time identity flags; ordinary, resumed and continued sessions already have an identity. The unclaimed boot runs in the CLI's session-runner role, which withholds subagents and workflows unless the CLI setting `Crewmates while backgrounded` is on, offers the Contract tool and refuses `Sleep` beyond 300 seconds. `facts`, `rewind`, `stop_agent` and `resume_agent` return generated result types. Model, effort, permission mode and kit changes use `set_model`, `set_effort`, `set_mode` and `set_kit`.
|
|
107
|
+
|
|
108
|
+
### Session method reference
|
|
109
|
+
|
|
110
|
+
This example uses a fresh session. The absent agent, server and checkpoint ids demonstrate refusal receipts without changing existing resources; use ids from `facts()` in an application. `set_kit` takes the runner's kit object, and `request` also exposes `session/file_restore` and `schedule/roster` without a convenience wrapper. `SyncSession` exposes the same methods without `await`.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
import asyncio
|
|
114
|
+
from mercury_cli_sdk import connect, MercuryError
|
|
115
|
+
|
|
116
|
+
async def main():
|
|
117
|
+
async with await connect(ephemeral=True, timeout=30) as session:
|
|
118
|
+
async def attempt(operation):
|
|
119
|
+
try: print(await operation)
|
|
120
|
+
except MercuryError as error:
|
|
121
|
+
if error.code is None: raise
|
|
122
|
+
print(error.code, error)
|
|
123
|
+
async def turn(send):
|
|
124
|
+
outcome = asyncio.get_running_loop().create_future()
|
|
125
|
+
def done(row):
|
|
126
|
+
if 'parent_call_id' not in row and not outcome.done(): outcome.set_result(row)
|
|
127
|
+
session.on('outcome', done)
|
|
128
|
+
try:
|
|
129
|
+
await send
|
|
130
|
+
print((await asyncio.wait_for(outcome, 30)).get('answer'))
|
|
131
|
+
finally: session.off('outcome', done)
|
|
132
|
+
facts = await session.facts()
|
|
133
|
+
await session.set_model(facts['model']['effective'])
|
|
134
|
+
await session.set_effort(facts.get('effort_sent') or 'max')
|
|
135
|
+
await session.set_mode(facts['permission_mode'])
|
|
136
|
+
await session.set_spawn_switch('subagents', facts.get('spawn_switches', {}).get('subagents', {}).get('on', True))
|
|
137
|
+
await attempt(session.set_kit(None))
|
|
138
|
+
await session.pause_gate(False)
|
|
139
|
+
await session.quiesce('prepare', 'sdk-example-pause-token')
|
|
140
|
+
await session.quiesce('cancel', 'sdk-example-pause-token')
|
|
141
|
+
await turn(session.send({'type': 'prompt', 'content': 'Say hello'}))
|
|
142
|
+
await turn(session.send_prompt('Summarise that'))
|
|
143
|
+
await turn(session.steer('Name one next step'))
|
|
144
|
+
await turn(session.follow_up('Finish with a short summary'))
|
|
145
|
+
await session.withdraw('not-queued')
|
|
146
|
+
await session.send_shell('echo sdk')
|
|
147
|
+
await attempt(session.send_note('absent-agent', 'Hello'))
|
|
148
|
+
await session.interrupt()
|
|
149
|
+
await attempt(session.background_shell())
|
|
150
|
+
await session.send_now()
|
|
151
|
+
await attempt(session.stop_agent('absent-agent'))
|
|
152
|
+
await attempt(session.resume_agent('absent-agent'))
|
|
153
|
+
await session.mcp_tools('absent-server')
|
|
154
|
+
await session.mcp_action('absent-server', 'cancel-auth')
|
|
155
|
+
await session.rewind('absent-prompt', 'code', dry_run=True)
|
|
156
|
+
await session.request('session/file_restore', {'action': 'undo', 'user_message_id': 'absent-prompt', 'dry_run': True})
|
|
157
|
+
await session.notify('credentials/changed', {})
|
|
158
|
+
await session.notify_credentials_changed()
|
|
159
|
+
session.clear_rows()
|
|
160
|
+
await session.leave()
|
|
161
|
+
asyncio.run(main())
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
For an unclaimed worker, `prewarm` and `claim` separate process startup from session identity.
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
import asyncio
|
|
168
|
+
import uuid
|
|
169
|
+
from mercury_cli_sdk import prewarm, assert_completed
|
|
170
|
+
|
|
171
|
+
async def main():
|
|
172
|
+
session = await prewarm(ephemeral=True, timeout=30)
|
|
173
|
+
try:
|
|
174
|
+
await session.claim(session_id=str(uuid.uuid4()))
|
|
175
|
+
outcome = await session.prompt('Say hello', timeout=30)
|
|
176
|
+
print(assert_completed(outcome).get('answer'))
|
|
177
|
+
finally: await session.close()
|
|
178
|
+
asyncio.run(main())
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Inline tools and agents
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
import asyncio
|
|
185
|
+
from mercury_cli_sdk import define_tool, run
|
|
186
|
+
|
|
187
|
+
async def main():
|
|
188
|
+
greeting = define_tool('greeting', 'Return a greeting', {}, lambda _: 'Hello')
|
|
189
|
+
task = await run('Use greeting to say hello', tools=[greeting])
|
|
190
|
+
result = await task.outcome
|
|
191
|
+
print(result['status'], result.get('answer'))
|
|
192
|
+
asyncio.run(main())
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Both APIs own the authenticated loopback MCP server for `tools`, close it on exit or failed startup, and merge it with `mcp`. Tool names `mcp__sdk-tools__<name>` are added to `allowed_tools` and existing inline custom-agent `tools` allowlists without mutating your options. `auto_allow_tools=False` keeps serving but leaves permission grants to you; explicit denials still apply. An unanswered headless ask produces `blocked`. `define_agent` validates definitions passed in `agent_defs`. `serve_tools(tools, name)` remains available for manually owned servers. Tool callbacks may accept a second `ToolContext` with a `cancelled` event for cooperative cancellation; keep the event loop running. `define_tool` accepts JSON Schema, a mapping such as `{'query': str, 'limit': int}`, or a TypedDict with NotRequired, Required and Annotated descriptions.
|
|
196
|
+
|
|
197
|
+
## Inputs, output schemas and launch options
|
|
198
|
+
|
|
199
|
+
A prompt accepts text, `list[InputBlock]` or an `AsyncIterable[str | InputRow]`. Block and iterable prompts automatically use input rows and close stdin when complete. `text_block(text)` builds text; `image_block(data, media_type)` accepts base64 text, bytes or `pathlib.Path`. For manual streaming use `input='rows'`, `send(row)` and `close_input()`. On the rows interface the CLI writes nothing, not even `session`, until the first input row arrives, so `session`, `cancel` and `interrupt()` take effect once the first prompt is sent; `close_input()` on a run that sent no row ends it without an outcome. `outcome` is the final run outcome; listen to individual outcome rows for multi-turn input.
|
|
200
|
+
|
|
201
|
+
`structured_schema(json_schema, decode)` infers `TypedOutcome[T]` from the decoder when passed as `schema=` to `run`, `connect` or `session.prompt`; a `schema` also accepts a JSON Schema dictionary, JSON text or a schema file path. On `run` it is the `--schema` flag and shapes every turn; on `connect` it travels on `initialize` and as the `--schema` flag, so it shapes every turn of the session on every Mercury version; `prompt(content, schema=...)` sends that turn's schema on its prompt row and overrides the session's for that turn, so one session can ask for a different shape each turn, and a turn without one carries no `structured`. A per-turn schema needs Mercury 1.0.0-beta.41 or newer: an older runner ignores the field, so the package refuses it before sending with ``MercuryError('per-turn schema needs Mercury 1.0.0-beta.41 or newer; this runner is 1.0.0-beta.40 — run `mercury update`')``, and `session.supports['per_turn_schema']` beside `session.runner_version` says so in advance (`FEATURE_FLOORS` lists each floor; the version is read from the `initialize` answer, and an answer that already carries `file_checkpoints` counts as the newer wire). Where the runner honours the schema, Mercury holds the model to it and settles the turn with `structured`; a schema that is not a JSON Schema is refused before anything is sent — the package itself refuses an unknown `type`, non-object `properties` and the other keywords its reader indexes into (`schema_problem(schema)` returns the words), and the CLI refuses the rest with its own issue — so `run`, `prompt`, `steer`, `follow_up`, `send_prompt` or `connect` raises `MercurySchemaError` with the issue at path `schema` and `outcome` None, and no model turn is spent. A completed `structured` is decoded by the descriptor and read once more by the package (types, scalar constants/enums, object fields, array items, numeric ranges and string lengths), so a replayed fixture is held to the same shape; a mismatch raises `MercurySchemaError` with exact `issues` and the raw `outcome`. `validate_outcome` also validates recorded outcomes.
|
|
202
|
+
|
|
203
|
+
`RunOptions` includes `replay_to`, `restore_files`, `worktree`, `reasoning_mode`, `brief_file`, `brief_add_file` and `debug`. The keyword options of `run`, `run_sync`, `connect`, `connect_sync` and `prewarm` are typed by `RunKwargs` and `ConnectKwargs`, so a type checker reports a wrong name or value. `argv_of` previews flags; `extra_args` passes additional flags. Both APIs accept an explicit executable or prefix array such as `mercury=['ssh', 'host', 'mercury']`. Otherwise discovery uses `MERCURY_BIN`; then the managed install, read as Mercury's own command reads it: `current.txt` under `MERCURY_VERSIONS_DIR`, else `versions` in the Mercury home (`MERCURY_CONFIG_DIR`, `MERCURY_HOME` or `~/.mercury`; `%USERPROFILE%\.mercury` on Windows), whose version directory's `mercury.mjs` is run on the Node runtime vendored beside it; then `mercury` on PATH; then the stable command the installer writes (`~/.local/bin/mercury`; `%LOCALAPPDATA%\Mercury\bin\mercury.cmd` on Windows) and `~/.mercury/bin/mercury`; then, on macOS and Linux, the Homebrew command (`$HOMEBREW_PREFIX/bin/mercury`, `/opt/homebrew/bin/mercury`, `/usr/local/bin/mercury`, `/home/linuxbrew/.linuxbrew/bin/mercury`), for a host launched with a stripped PATH (an empty PATH skips this rung). No shell is involved: Mercury's managed command (`mercury`, `mercury.cmd`, `mercury.ps1`) resolves to that bundle, the Homebrew wrapper to the keg's `mercury.mjs` on its vendored runtime, an npm-style `.cmd` shim to its JavaScript entry point, and a `mercury.cmd` beside a `mercury.mjs` to that bundle; any other command shim requires an explicit `[node, script]` array, and a managed command whose install is missing is refused with the pointer file it looked for. A relative executable with a slash (`./bin/mercury`) is resolved against `cwd`; on Windows the `env` keys are read without regard to case, so a copy of the environment spelled `Path` and `Pathext` still resolves. A `.mjs`, `.cjs` or `.js` executable such as a built `mercury.mjs` runs on `MERCURY_NODE` when set (one that names no executable file is refused by name, as the launcher refuses it), else the runtime bundled beside it under `vendor/node`, else `node` from PATH.
|
|
204
|
+
|
|
205
|
+
## Test without a model
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
import asyncio
|
|
209
|
+
from mercury_cli_sdk import run
|
|
210
|
+
from mercury_cli_sdk.testing import start_fixture_server, fixture_session_row, fixture_outcome_row
|
|
211
|
+
|
|
212
|
+
async def main():
|
|
213
|
+
rows = [fixture_session_row(), fixture_outcome_row(answer='ok')]
|
|
214
|
+
async with await start_fixture_server({'rows': rows}) as fixture:
|
|
215
|
+
task = await run('test', mercury=fixture.mercury)
|
|
216
|
+
assert (await task.outcome)['answer'] == 'ok'
|
|
217
|
+
asyncio.run(main())
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
`fixture_turn(overrides, prompt_id)` builds a correlated turn/outcome pair. You can also replay `task.rows`. Fixtures validate rows and RPC replies before listening; `'validate': False` in the fixture dictionary is for deliberately malformed-wire tests. `methods` holds ordered `{ result, before?, after? }` or `{ error }` replies; before/after and `answers` contain RPC messages. `received` records inputs and `inputCount` delays replay. Missing methods return method-not-found. Fixtures use neither a CLI nor model credentials and do not simulate the CLI's internal behaviour.
|
|
221
|
+
|
|
222
|
+
## Inspect rows and schemas
|
|
223
|
+
|
|
224
|
+
Parsers return a `RowRead` with `ok`, `row` and `issues` rather than throwing; `assert_completed` and `validate_outcome` raise on an unsuccessful turn or a mismatched structured value respectively.
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
import asyncio
|
|
228
|
+
import json
|
|
229
|
+
import mercury_cli_sdk as sdk
|
|
230
|
+
|
|
231
|
+
async def main():
|
|
232
|
+
async with await sdk.run(input='rows') as task:
|
|
233
|
+
def listener(row):
|
|
234
|
+
if sdk.is_outcome(row): print(row.get('answer'))
|
|
235
|
+
task.on('row', listener)
|
|
236
|
+
await task.send_prompt([sdk.text_block('Say hello')])
|
|
237
|
+
task.close_input()
|
|
238
|
+
outcome = sdk.assert_completed(await task.outcome)
|
|
239
|
+
task.off('row', listener)
|
|
240
|
+
print(sdk.argv_of(sdk.RunOptions(prompt='Say hello')), sdk.outcome_failed(outcome), sdk.outcome_error_text(outcome), sdk.is_retryable_outcome(outcome))
|
|
241
|
+
print(sdk.parse_row(json.dumps(outcome)), sdk.row_of(outcome), sdk.input_row_of({'type': 'prompt', 'content': 'Hi'}))
|
|
242
|
+
print(sdk.is_row_type('text'), sdk.is_input_row_type('prompt'))
|
|
243
|
+
print([(sdk.is_main_thread(row), sdk.is_session_row(row), sdk.turn_opened(row), sdk.turn_waiting(row), sdk.main_thread_step(row)) for row in task.rows])
|
|
244
|
+
schema = sdk.structured_schema({'type': 'string'}, str)
|
|
245
|
+
print(sdk.schema_problem(schema.json_schema), sdk.validate(schema.json_schema, 'hello'))
|
|
246
|
+
print(sdk.validate_outcome({**outcome, 'structured': 'hello'}, schema)['structured'])
|
|
247
|
+
print(sdk.schema_refusal({'code': -32601, 'message': 'not a schema error'}))
|
|
248
|
+
print(sdk.image_block('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aCfQAAAAASUVORK5CYII=', 'image/png')['media_type'])
|
|
249
|
+
print(sdk.allow_permission(), sdk.deny_permission('Read-only host'))
|
|
250
|
+
print(sdk.define_agent({'description': 'Reader', 'prompt': 'Read and report; do not edit'}))
|
|
251
|
+
print(sdk.parse_version('1.0.0'), sdk.compare_versions('1.0.0', '1.0.1'), sdk.runner_support(None))
|
|
252
|
+
tools = await sdk.serve_tools([])
|
|
253
|
+
try: tools.cancel()
|
|
254
|
+
finally: await tools.close()
|
|
255
|
+
task.clear_rows()
|
|
256
|
+
asyncio.run(main())
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
`MercuryRun.send(row)`, `send_prompt`, `send_shell` and `send_note` accept the same input shapes as the session methods above. `interrupt()`, `terminate()` and `kill()` signal only that run's child; await `exit` afterward. For example, `task = await run('Inspect the project'); task.interrupt(); await task.exit` cancels a run. Replace `interrupt()` with `terminate()` or `kill()` when graceful interruption is not enough. `clear_rows()` clears retained rows and unread buffered rows, not listeners or the running turn.
|
|
260
|
+
|
|
261
|
+
The synchronous session also closes through its context manager.
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
from mercury_cli_sdk import connect_sync, assert_completed
|
|
265
|
+
|
|
266
|
+
with connect_sync(timeout=30) as session:
|
|
267
|
+
print(assert_completed(session.prompt('Say hello', timeout=30)).get('answer'))
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Rows, errors and cleanup
|
|
271
|
+
|
|
272
|
+
`parse_row`, `row_of`, `input_row_of` and `validate` use generated schemas. `is_main_thread` separates subagent rows. Unknown types become `unknown_row` with `original_type`, raw payload and envelope; known extra fields are preserved. Schema-version changes and malformed known rows are rejected.
|
|
273
|
+
|
|
274
|
+
A built-in refusal that fired is listed in `wards`, a permission denial in `denials`; a turn can complete with a ward fired and no denial.
|
|
275
|
+
|
|
276
|
+
A CLI outcome with `status: 'refused'` raises `MercuryRefusal`; provider refusals, authentication errors, budget stops and repeated invalid structured output instead resolve a non-completed outcome. Call `assert_completed(outcome)` to raise `MercuryOutcomeError` for a non-completed outcome, including an unanswered permission ask; a `refused` status remains `MercuryRefusal`. Both extend `MercuryError`. `MercuryOutcomeError` retains the original `outcome`, `error_class` and `detail`. Its message names the tool and how to grant permission or attach a host; the SDK does not grant permission automatically. Other specific subclasses are `MercuryUsageError`, `MercuryExitError`, `MercurySpawnError`, `MercuryWireError`, `MercurySchemaError`, `MercuryTimeoutError` and `MercuryConnectionError`. Exit/usage errors preserve the interface and exit receipt; RPC errors preserve code and data. Check status rather than only exit code.
|
|
277
|
+
|
|
278
|
+
```python
|
|
279
|
+
from mercury_cli_sdk import run_sync, assert_completed, MercuryOutcomeError
|
|
280
|
+
|
|
281
|
+
try:
|
|
282
|
+
outcome = run_sync('Write a greeting to hello.txt').outcome
|
|
283
|
+
print(assert_completed(outcome).get('answer'))
|
|
284
|
+
except MercuryOutcomeError as error:
|
|
285
|
+
print(error.outcome['status'], str(error))
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
`connect(timeout=seconds)` bounds initialization. `prompt(content, timeout=seconds)` bounds the whole wait, including queue acceptance; it does not stop the turn unless `interrupt_on_cancel=True` is supplied. A late acceptance still triggers that requested interrupt. `MercuryTimeoutError` carries `method` and `timeout`. Check `facts()` before resending work that may already have been accepted. Requests use the protocol's deadline unless `timeout` overrides it; initialization, prompts and `leave` have no default deadline. Use `request('session/leave', timeout=30)` to bound a leave. Python task cancellation remains `asyncio.CancelledError`.
|
|
289
|
+
|
|
290
|
+
`MercuryConnectionError` means the stream closed while the process remained alive, or a call was made during/after `close()`, including through `SyncSession`. Reconnect before sending more work. All pending requests and row iterators settle on a broken connection; normal `close()` ends iterators. Malformed known notifications raise `MercuryWireError`; unknown future notifications remain available through `on('notification', listener)`.
|
|
291
|
+
|
|
292
|
+
`stderr` holds what the child has written to standard error so far, read as text with `\r\n` normalised to `\n` (a Windows child's line endings included) so `exit.stderr`, the handle and the chunks agree; `on_stderr` (or an `on('stderr', …)` listener, also on `SyncSession`) receives each chunk as it arrives, on both APIs. `cancel=asyncio.Event()` interrupts a run when the event is set. `interrupt`, `terminate` and `kill` are synchronous (not awaited) and signal the child with SIGINT, SIGTERM and SIGKILL; the exit is awaited through `exit`. On Windows, which delivers no signal, they end the process the way Node's child_process does, and `exit.signal` names the one the package sent. An interrupt the package sent always settles the turn as an `interrupted` outcome: the CLI writes that row itself where SIGINT reaches it, and where the child ends by the signal before writing one the package completes the turn with an `interrupted` outcome row folded from the rows it received. A signal the package did not send, and `terminate` or `kill`, surface as `MercuryExitError` when the child ends without an outcome row; a CLI that catches the signal mid-turn (SIGTERM on macOS and Linux) writes its own `interrupted` outcome, and the run resolves with it. Close handles in finally or with `async with`; `close` ends stdin, escalates after five seconds to termination, and after ten to killing. Normal host exit sends SIGTERM to live children; an unhandled killing signal cannot run cleanup, so long-running hosts should handle SIGTERM and close sessions.
|
|
293
|
+
|
|
294
|
+
<!-- vocabulary:start -->
|
|
295
|
+
| Row | Required fields |
|
|
296
|
+
| --- | --- |
|
|
297
|
+
| `session` | `type`, `seq`, `timestamp`, `session_id`, `schema`, `version`, `cwd`, `model`, `mode`, `tools`, `mcp_servers`, `commands`, `agents`, `skills`, `extensions` |
|
|
298
|
+
| `turn` | `type`, `seq`, `timestamp`, `session_id`, `state`, `turn_id` |
|
|
299
|
+
| `text` | `type`, `seq`, `timestamp`, `session_id`, `message_id`, `block`, `text` |
|
|
300
|
+
| `reasoning` | `type`, `seq`, `timestamp`, `session_id`, `message_id`, `block`, `text` |
|
|
301
|
+
| `tool_call` | `type`, `seq`, `timestamp`, `session_id`, `call_id`, `tool`, `input`, `message_id`, `block` |
|
|
302
|
+
| `tool_result` | `type`, `seq`, `timestamp`, `session_id`, `call_id`, `status`, `output` |
|
|
303
|
+
| `tool_update` | `type`, `seq`, `timestamp`, `session_id`, `call_id`, `tick`, `source` |
|
|
304
|
+
| `step` | `type`, `seq`, `timestamp`, `session_id`, `message_id`, `model`, `usage` |
|
|
305
|
+
| `outcome` | `type`, `seq`, `timestamp`, `session_id`, `turn`, `schema`, `turn_id`, `status`, `steps`, `wall_ms`, `usage`, `models`, `denials` |
|
|
306
|
+
| `close` | `type`, `seq`, `timestamp`, `session_id`, `state` |
|
|
307
|
+
| `wait` | `type`, `seq`, `timestamp`, `session_id`, `state` |
|
|
308
|
+
| `heartbeat` | `type`, `seq`, `timestamp`, `session_id` |
|
|
309
|
+
| `compaction` | `type`, `seq`, `timestamp`, `session_id`, `state`, `trigger` |
|
|
310
|
+
| `mode` | `type`, `seq`, `timestamp`, `session_id`, `mode` |
|
|
311
|
+
| `rate_limit` | `type`, `seq`, `timestamp`, `session_id`, `status` |
|
|
312
|
+
| `task` | `type`, `seq`, `timestamp`, `session_id`, `state`, `task_id` |
|
|
313
|
+
| `notice` | `type`, `seq`, `timestamp`, `session_id`, `level`, `text` |
|
|
314
|
+
| `command_output` | `type`, `seq`, `timestamp`, `session_id`, `text` |
|
|
315
|
+
| `mission_updated` | `type`, `seq`, `timestamp`, `session_id` |
|
|
316
|
+
| `samples_updated` | `type`, `seq`, `timestamp`, `session_id` |
|
|
317
|
+
| `block_start` | `type`, `seq`, `timestamp`, `session_id`, `message_id`, `block`, `of` |
|
|
318
|
+
| `text_delta` | `type`, `seq`, `timestamp`, `session_id`, `message_id`, `block`, `text` |
|
|
319
|
+
| `reasoning_delta` | `type`, `seq`, `timestamp`, `session_id`, `message_id`, `block`, `text` |
|
|
320
|
+
| `tool_input_delta` | `type`, `seq`, `timestamp`, `session_id`, `message_id`, `block`, `json` |
|
|
321
|
+
| `retracted` | `type`, `seq`, `timestamp`, `session_id`, `message_id` |
|
|
322
|
+
<!-- vocabulary:end -->
|
|
323
|
+
|
|
324
|
+
## Licence
|
|
325
|
+
|
|
326
|
+
Licensed under the Apache License 2.0 (`Apache-2.0`); see [LICENSE](LICENSE). The Mercury CLI is licensed under the same licence.
|