@anureo/sdk 0.0.0-stage → 0.3.0
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.
- package/LICENSE +201 -0
- package/README.md +185 -2
- package/package.json +66 -4
- package/src/adapter/identity.d.ts +41 -0
- package/src/adapter/identity.js +71 -0
- package/src/client-options.d.ts +71 -0
- package/src/client-options.js +0 -0
- package/src/client.d.ts +106 -0
- package/src/client.js +254 -0
- package/src/diagnostics/errors.d.ts +15 -0
- package/src/diagnostics/errors.js +59 -0
- package/src/diagnostics/events.d.ts +46 -0
- package/src/diagnostics/events.js +47 -0
- package/src/diagnostics.d.ts +20 -0
- package/src/diagnostics.js +56 -0
- package/src/errors.d.ts +48 -0
- package/src/errors.js +63 -0
- package/src/index.d.ts +27 -0
- package/src/index.js +23 -0
- package/src/lifecycle/approval.d.ts +16 -0
- package/src/lifecycle/approval.js +18 -0
- package/src/lifecycle/deferred.d.ts +10 -0
- package/src/lifecycle/deferred.js +12 -0
- package/src/lifecycle/errors.d.ts +18 -0
- package/src/lifecycle/errors.js +45 -0
- package/src/lifecycle/events.d.ts +35 -0
- package/src/lifecycle/events.js +141 -0
- package/src/lifecycle/execution-observation.d.ts +33 -0
- package/src/lifecycle/execution-observation.js +0 -0
- package/src/lifecycle/immutable.d.ts +12 -0
- package/src/lifecycle/immutable.js +28 -0
- package/src/lifecycle/input.d.ts +9 -0
- package/src/lifecycle/input.js +19 -0
- package/src/lifecycle/interactions.d.ts +129 -0
- package/src/lifecycle/interactions.js +282 -0
- package/src/lifecycle/limits.d.ts +7 -0
- package/src/lifecycle/limits.js +13 -0
- package/src/lifecycle/result.d.ts +77 -0
- package/src/lifecycle/result.js +213 -0
- package/src/lifecycle/value.d.ts +4 -0
- package/src/lifecycle/value.js +5 -0
- package/src/ports.d.ts +224 -0
- package/src/ports.js +0 -0
- package/src/privacy/file-change-limits.d.ts +20 -0
- package/src/privacy/file-change-limits.js +124 -0
- package/src/privacy/secrets.d.ts +205 -0
- package/src/privacy/secrets.js +564 -0
- package/src/process.d.ts +92 -0
- package/src/process.js +331 -0
- package/src/queue.d.ts +143 -0
- package/src/queue.js +266 -0
- package/src/request.d.ts +28 -0
- package/src/request.js +81 -0
- package/src/requests/guard.d.ts +15 -0
- package/src/requests/guard.js +93 -0
- package/src/session.d.ts +459 -0
- package/src/session.js +1135 -0
- package/src/structured-output/adapter.d.ts +8 -0
- package/src/structured-output/adapter.js +54 -0
- package/src/structured-output/bridge.d.ts +81 -0
- package/src/structured-output/bridge.js +274 -0
- package/src/structured-output/errors.d.ts +17 -0
- package/src/structured-output/errors.js +31 -0
- package/src/structured-output/limits.d.ts +19 -0
- package/src/structured-output/limits.js +0 -0
- package/src/structured-output/schema.d.ts +20 -0
- package/src/structured-output/schema.js +122 -0
- package/src/structured-output/session.d.ts +329 -0
- package/src/structured-output/session.js +846 -0
- package/src/structured-output/stdio.mjs +83 -0
- package/src/structured-output/tool.d.ts +38 -0
- package/src/structured-output/tool.js +34 -0
- package/src/turn.d.ts +75 -0
- package/src/turn.js +95 -0
- package/src/types.d.ts +62 -0
- package/src/types.js +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Yoichi
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,186 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @anureo/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@anureo/sdk` runs agent sessions through a harness adapter. Applications use it
|
|
4
|
+
to submit input, stream events, collect results, and control native executions.
|
|
5
|
+
The SDK validates AHP requests and events, tracks which execution handles each
|
|
6
|
+
submission, and owns session observation and cleanup. The adapter provides the
|
|
7
|
+
native execution and reports its progress.
|
|
8
|
+
|
|
9
|
+
This guide describes the revised workspace API. The published 0.2.0 release has a
|
|
10
|
+
different lifecycle API. See the [workspace guide](../../README.md) and the
|
|
11
|
+
[lifecycle migration notes](../../draft/docs/native-harness-lifecycle.md).
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
This example requires an authenticated Codex CLI on PATH and the
|
|
16
|
+
[Codex adapter](../adapter/codex/README.md). For a streaming version, run
|
|
17
|
+
`bun run example` from the workspace root or read the
|
|
18
|
+
[greeting example](../../examples/greeting.ts).
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { createClient } from '@anureo/sdk'
|
|
22
|
+
import { createCodexAdapter } from '@anureo/adapter-codex'
|
|
23
|
+
|
|
24
|
+
const client = await createClient({
|
|
25
|
+
adapter: createCodexAdapter({ command: 'codex' }),
|
|
26
|
+
identity: { adapterId: 'codex', runtimeConfigId: 'local' },
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const session = await client.sessions.create({ cwd: process.cwd() })
|
|
31
|
+
const turn = await session.submit({ input: 'Say hello in one sentence.' })
|
|
32
|
+
const result = await turn.result()
|
|
33
|
+
|
|
34
|
+
if (result.status === 'failed') throw new Error(result.error.message)
|
|
35
|
+
if (result.status === 'interrupted') throw new Error('Turn interrupted')
|
|
36
|
+
console.log(result.text)
|
|
37
|
+
|
|
38
|
+
await session.close()
|
|
39
|
+
} finally {
|
|
40
|
+
await client.dispose()
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Application API
|
|
45
|
+
|
|
46
|
+
Import the application API and its types from `@anureo/sdk`.
|
|
47
|
+
|
|
48
|
+
### Client
|
|
49
|
+
|
|
50
|
+
| API | Purpose |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| `createClient(options, wait?)` | Initialize an adapter and return a `Client` ready to open sessions. |
|
|
53
|
+
| `new Client(options)`, `client.initialize(wait?)` | Construct a client without I/O, then negotiate adapter capabilities explicitly. |
|
|
54
|
+
| `client.identity` | Read the adapter and runtime configuration IDs used to validate session bindings. |
|
|
55
|
+
| `client.sessions.create(options, wait?)` | Open a new session with a fresh binding. |
|
|
56
|
+
| `client.sessions.resume(reference, wait?)` | Reopen the session identified by a saved `session.reference`, when supported. |
|
|
57
|
+
| `client.sessions.fork(reference, { boundary }, wait?)` | Open a new session from a specified boundary in an existing session, when supported. |
|
|
58
|
+
| `client.openSession(params, wait?)` | Open a session with explicit AHP open parameters and binding. |
|
|
59
|
+
| `client.dispose()` | Release the client's sessions and adapter. Call it in `finally`. |
|
|
60
|
+
|
|
61
|
+
[`ClientOptions`](src/client-options.ts) configures the adapter, client identity,
|
|
62
|
+
runtime hooks, interaction handlers, and retention limits.
|
|
63
|
+
[`RuntimeOptions`](src/ports.ts) configures timeouts, resource budgets, and host
|
|
64
|
+
hooks such as ID generation and history redaction.
|
|
65
|
+
|
|
66
|
+
### Session
|
|
67
|
+
|
|
68
|
+
A `Session` represents a conversation opened through the client.
|
|
69
|
+
|
|
70
|
+
| API | Purpose |
|
|
71
|
+
| --- | --- |
|
|
72
|
+
| `session.submit(options, wait?)` | Send input and return a `Turn` once the adapter associates the input with an execution. |
|
|
73
|
+
| `session.review({ target }, wait?)` | Start a supported native review and return its associated `Turn`. |
|
|
74
|
+
| `session.events(options?)` | Subscribe to future session events, including submission progress and interaction requests. Subscribe before submitting to observe those events. |
|
|
75
|
+
| `session.id`, `session.binding`, `session.reference` | Read the session ID, its adapter binding, or a reference for resuming or forking it. |
|
|
76
|
+
| `session.capabilities`, `session.snapshot` | Check the session's current capabilities or read its current privacy-filtered state. |
|
|
77
|
+
| `session.interrupt(options?, wait?)` | Request a session-wide interrupt. Pass `cancelQueued` only when supported. An acknowledgement alone cannot establish that execution stopped or that queued input was withdrawn. |
|
|
78
|
+
| `session.request(method, params, wait?)` | Send a typed, lower-level AHP session request for operations such as history reads or manual interaction responses. |
|
|
79
|
+
| `session.close(wait?)` | Request native close and release observation and backend resources after the receipt. The receipt reports `observationClosed` and `executionStopped` separately. |
|
|
80
|
+
| `session.dispose()` | Release local observation and backend resources without asserting that native execution stopped. |
|
|
81
|
+
|
|
82
|
+
`Input` accepts a string or an array of AHP input blocks. [`SubmitOptions`](src/client-options.ts)
|
|
83
|
+
also accepts capability-gated `delivery`, `overrides`, and `outputSchema`.
|
|
84
|
+
Delivery defaults to `native`, so a backend can queue input or merge it into an
|
|
85
|
+
existing execution. Two submissions can return handles with the same `turn.id`.
|
|
86
|
+
Count the execution and its usage once. Use `delivery: 'afterTurn'` when
|
|
87
|
+
`session.capabilities.input?.delivery` includes `afterTurn`.
|
|
88
|
+
The SDK tracks submission IDs internally. `submit()` waits for an execution
|
|
89
|
+
association, which can arrive before an acceptance receipt or after the execution
|
|
90
|
+
has already ended.
|
|
91
|
+
|
|
92
|
+
Pass a JSON Schema as `outputSchema` when `session.capabilities.turn?.outputSchema`
|
|
93
|
+
is enabled. A successful result carries JSON in `result.text`. The SDK uses native
|
|
94
|
+
schemas where available, a direct output tool on Pi, or a session-scoped MCP bridge
|
|
95
|
+
on supported ACP and Hermes sessions. The [structured output example](../../examples/structured-output.ts)
|
|
96
|
+
shows how to submit and parse a result.
|
|
97
|
+
|
|
98
|
+
Configure supported interactions through `ClientOptions.interactions`.
|
|
99
|
+
An `InteractionHandlers` function handles its kind automatically. An explicit
|
|
100
|
+
key with value `undefined` enables manual responses through session events and
|
|
101
|
+
`session.request('interaction/respond', params)`. An omitted kind is unsupported.
|
|
102
|
+
|
|
103
|
+
### Turn
|
|
104
|
+
|
|
105
|
+
| API | Purpose |
|
|
106
|
+
| --- | --- |
|
|
107
|
+
| `turn.id` | Read the execution ID shared by all handles observing that execution. |
|
|
108
|
+
| `turn.result(wait?)` | Wait for the execution's terminal result or read it if already retained. |
|
|
109
|
+
| `turn.events(options?)` | Subscribe to future events for this execution. |
|
|
110
|
+
| `turn.steer({ input }, wait?)` | Wait for native confirmation that input was appended to this exact execution. Requires `session.capabilities.input?.steer?.atomicMatch` and rejects stale targets. |
|
|
111
|
+
| `turn.interrupt(wait?)` | Request interruption of this exact execution when supported. Use `result()` to observe its terminal status. |
|
|
112
|
+
| `turn.observe()` | Create an independent handle for the same execution. |
|
|
113
|
+
| `turn.dispose()` | Stop this handle's waits and subscriptions. Native execution and other handles can continue. |
|
|
114
|
+
|
|
115
|
+
[`TurnResult`](src/types.ts) is an immutable snapshot with text, items,
|
|
116
|
+
and any available usage. Its status is `completed`, `interrupted`, or `failed`.
|
|
117
|
+
An interrupted result can carry a native error reason.
|
|
118
|
+
|
|
119
|
+
[`RequestOptions`](src/types.ts) sets a caller's wait deadline and abort signal.
|
|
120
|
+
A timeout after dispatch can leave the native outcome unknown. Check
|
|
121
|
+
`HarnessSdkError.outcomeUnknown` before retrying to avoid duplicate work.
|
|
122
|
+
[`EventSubscriptionOptions`](src/types.ts) controls cancellation and
|
|
123
|
+
per-subscription queue limits.
|
|
124
|
+
|
|
125
|
+
## Adapter API
|
|
126
|
+
|
|
127
|
+
Adapters import their contracts from `@anureo/sdk` and use the types in
|
|
128
|
+
[`@anureo/protocol`](../protocol/README.md) for AHP requests, results, and events.
|
|
129
|
+
|
|
130
|
+
| Contract | Who implements and calls it |
|
|
131
|
+
| --- | --- |
|
|
132
|
+
| `HarnessAdapter` | The adapter implements `initialize()`, `openSession()`, and `dispose()`. The client calls them to negotiate capabilities, open native sessions, and release client-wide resources. |
|
|
133
|
+
| `BackendSession` | The adapter returns this from `openSession()`. Core reads its binding, capabilities, and optional state, and calls `execute()` and `dispose()` for native requests and cleanup. `SessionMethod` identifies requests routed through an open session. |
|
|
134
|
+
| `SessionEventSink` | Core supplies this to the adapter at open. The adapter calls `emit()` or `tryEmit()` to publish events, `bindSubmission()` to associate input with an execution, and `fail()` to report observation failure. `EventScope` supplies turn or item context for an event. |
|
|
135
|
+
| `NativeInteractionResponder` | The adapter supplies this callback to `sink.requestInteraction()`. Core calls it to deliver a response or request cancellation. The adapter calls `sink.resolveInteraction()` when the native peer has already settled a request. |
|
|
136
|
+
| `StructuredOutputToolPort`, `StructuredOutputToolDefinition`, `StructuredOutputToolResult` | An adapter can expose a tool port on `BackendSession`. Core calls `configure()` to install or disable the tool. The adapter presents the definition and calls its `execute()` to validate a candidate. |
|
|
137
|
+
|
|
138
|
+
`HarnessAdapter.outputBridgeTransport()` lets an adapter opt into the session MCP
|
|
139
|
+
bridge. `SessionEventSink.projectOutput()` is an optional hook for privacy-filtering
|
|
140
|
+
output before final schema validation. See [the port definitions](src/ports.ts)
|
|
141
|
+
for ordering, failure, and ownership contracts.
|
|
142
|
+
|
|
143
|
+
## Supporting exports
|
|
144
|
+
|
|
145
|
+
| Purpose | Import path | Exports |
|
|
146
|
+
| --- | --- | --- |
|
|
147
|
+
| Errors and caller waits | `@anureo/sdk` | `HarnessSdkError` carries a safe code and outcome metadata. `sdkError()` normalizes a failure. `waitForRequest()` applies a caller-scoped wait using `RequestOptions`. |
|
|
148
|
+
| Unattended approvals | `@anureo/sdk` | `approveWithNarrowestScope()` can be explicitly registered as `interactions.approval`. It chooses allow-once, then session, then rule grants, and rejects when none is offered. Clients never enable it automatically. |
|
|
149
|
+
| Host logging | `@anureo/sdk` | LogTape's `configure`, `configureSync`, `getLogger`, `drain`, `dispose`, `disposeSync`, `reset`, and `resetSync`, plus `Config`, `LogLevel`, `LogRecord`, `Logger`, and `Sink`. The application configures logging sinks. |
|
|
150
|
+
| Native subprocesses | `@anureo/sdk/process` or `@anureo/sdk` | `spawnHarnessProcess()` launches and owns stdio subprocess I/O and cleanup. The module also exports `ProcessPortError`, `ProcessExit`, `HarnessProcess`, and `HarnessProcessOptions`. |
|
|
151
|
+
| Bounded buffering | `@anureo/sdk/queue` or `@anureo/sdk` | `BoundedEventQueue` provides a bounded, single-reader async queue. `QueueLimits` configures its budgets. |
|
|
152
|
+
| Model catalog diagnostics | `@anureo/sdk/diagnostics` | `catalogPhase()` reports probe timing and outcomes. `emitCatalogPhase()` publishes a diagnostic, and `safeCatalogErrorCode()` projects an error to a safe code. `CatalogProbeOptions` and `CatalogProbeEvent` describe observers and their events. |
|
|
153
|
+
|
|
154
|
+
### Implementation helpers
|
|
155
|
+
|
|
156
|
+
The root currently exports `NativeIdentityMap` for native-to-SDK ID mapping,
|
|
157
|
+
`InteractionRegistry` for tracking native interaction responses, and
|
|
158
|
+
`SecretRedactor` for filtering marked secrets.
|
|
159
|
+
`@anureo/sdk/structured-output-stdio` is an internal package entry that core
|
|
160
|
+
resolves to launch the stdio bridge subprocess.
|
|
161
|
+
|
|
162
|
+
## Code structure
|
|
163
|
+
|
|
164
|
+
| Source | Responsibility |
|
|
165
|
+
| --- | --- |
|
|
166
|
+
| [`package.json`](package.json), [`src/index.ts`](src/index.ts) | Define the package entry points and assemble the root exports. |
|
|
167
|
+
| [`src/client.ts`](src/client.ts), [`src/session.ts`](src/session.ts), [`src/turn.ts`](src/turn.ts) | Implement the application API for opening sessions, dispatching requests, and observing and controlling executions. |
|
|
168
|
+
| [`src/client-options.ts`](src/client-options.ts), [`src/types.ts`](src/types.ts), [`src/ports.ts`](src/ports.ts) | Define public configuration, input, result, and observation types, plus adapter contracts. |
|
|
169
|
+
| [`src/errors.ts`](src/errors.ts), [`src/request.ts`](src/request.ts) | Provide SDK errors, failure normalization, and caller-scoped waits. |
|
|
170
|
+
| [`src/lifecycle/`](src/lifecycle/) | Supports the application API with event subscriptions, result accumulation, and interaction settlement. |
|
|
171
|
+
| [`src/adapter/`](src/adapter/), [`src/requests/`](src/requests/) | Provide native-to-SDK ID mapping and request validation against session bindings and capabilities. |
|
|
172
|
+
| [`src/privacy/`](src/privacy/) | Redacts marked secrets from observed data and bounds retained file change content. |
|
|
173
|
+
| [`src/structured-output/`](src/structured-output/) | Wraps adapters for schema validation and manages the direct-tool and MCP fallback paths. |
|
|
174
|
+
| [`src/process.ts`](src/process.ts), [`src/queue.ts`](src/queue.ts), [`src/diagnostics.ts`](src/diagnostics.ts) | Implement the subprocess, bounded queue, and model catalog diagnostics subpaths. |
|
|
175
|
+
|
|
176
|
+
## Development
|
|
177
|
+
|
|
178
|
+
```sh
|
|
179
|
+
cd packages/core
|
|
180
|
+
bun run typecheck
|
|
181
|
+
bun test src
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
See the [architecture notes](../../draft/docs/architecture.md) for package
|
|
185
|
+
responsibilities and the [AHP v1 schema](../protocol/schema/v1/ahp.schema.json)
|
|
186
|
+
for wire contracts.
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anureo/sdk",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Runtime core of the Anureo SDK: unified HarnessRuntime and HarnessSession over any adapter, enforcing AHP semantics",
|
|
4
|
+
"version": "0.3.0",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"anureo",
|
|
8
|
+
"ahp",
|
|
9
|
+
"harness",
|
|
10
|
+
"agent",
|
|
11
|
+
"sdk",
|
|
12
|
+
"runtime"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/hi-youichi/anureo-sdk#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/hi-youichi/anureo-sdk.git",
|
|
18
|
+
"directory": "packages/core"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/hi-youichi/anureo-sdk/issues"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./src/index.d.ts",
|
|
30
|
+
"import": "./src/index.js",
|
|
31
|
+
"default": "./src/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./process": {
|
|
34
|
+
"types": "./src/process.d.ts",
|
|
35
|
+
"import": "./src/process.js",
|
|
36
|
+
"default": "./src/process.js"
|
|
37
|
+
},
|
|
38
|
+
"./queue": {
|
|
39
|
+
"types": "./src/queue.d.ts",
|
|
40
|
+
"import": "./src/queue.js",
|
|
41
|
+
"default": "./src/queue.js"
|
|
42
|
+
},
|
|
43
|
+
"./diagnostics": {
|
|
44
|
+
"types": "./src/diagnostics.d.ts",
|
|
45
|
+
"import": "./src/diagnostics.js",
|
|
46
|
+
"default": "./src/diagnostics.js"
|
|
47
|
+
},
|
|
48
|
+
"./structured-output-stdio": "./src/structured-output/stdio.mjs"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@anureo/protocol": "0.3.0",
|
|
52
|
+
"@modelcontextprotocol/sdk": "1.32.2",
|
|
53
|
+
"ajv": "8.20.0"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=22"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@logtape/logtape": ">=2.0.0 <3"
|
|
60
|
+
},
|
|
61
|
+
"files": [
|
|
62
|
+
"src",
|
|
63
|
+
"bin",
|
|
64
|
+
"schema",
|
|
65
|
+
"protocol",
|
|
66
|
+
"examples"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Translate opaque native turn and item IDs when implementing an adapter. Keep one
|
|
3
|
+
* map per native session and use it for events that share those identities. The
|
|
4
|
+
* adapter owns event ordering, failure handling and clearing the map once its
|
|
5
|
+
* identities are no longer needed.
|
|
6
|
+
*/
|
|
7
|
+
export declare class NativeIdentityMap {
|
|
8
|
+
private readonly createId;
|
|
9
|
+
/**
|
|
10
|
+
* Supply an allocator that returns unique, nonempty SDK item IDs. item() invokes it
|
|
11
|
+
* synchronously on a cache miss and propagates failures. The allocator owns uniqueness.
|
|
12
|
+
*/
|
|
13
|
+
constructor(createId: () => string);
|
|
14
|
+
/**
|
|
15
|
+
* Record a turn pair before translating it through turn() or nativeTurn(). Rebinding
|
|
16
|
+
* the same pair is harmless. A conflicting nonempty stored ID throws HarnessSdkError
|
|
17
|
+
* with code PROTOCOL_ERROR before either lookup changes. Empty stored values permit
|
|
18
|
+
* rebinding, which writes only the supplied pair. The adapter handles conflicts.
|
|
19
|
+
*/
|
|
20
|
+
bindTurn(turnId: string, nativeTurnId: string): void;
|
|
21
|
+
/** Adapters translate a native turn ID to its stored SDK ID, or undefined, without mutation or allocation. */
|
|
22
|
+
turn(nativeTurnId: string): string | undefined;
|
|
23
|
+
/** Adapters translate an SDK turn ID to its stored native ID, or undefined, without mutation or allocation. */
|
|
24
|
+
nativeTurn(turnId: string): string | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Reuse the SDK item ID for a native item within an SDK turn. The turn can be unbound.
|
|
27
|
+
* The allocator runs synchronously when the pair has no nonempty cached ID. An empty
|
|
28
|
+
* result is returned and triggers another allocation on the next lookup. Allocator
|
|
29
|
+
* failures propagate unchanged and leave the adapter free to retry.
|
|
30
|
+
*/
|
|
31
|
+
item(turnId: string, nativeItemId: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Forget both turn directions and all cached item IDs once pending events no longer
|
|
34
|
+
* need them. Later calls can bind and allocate again with the same allocator.
|
|
35
|
+
* Repeated clears are harmless. The adapter remains responsible for native cleanup.
|
|
36
|
+
*/
|
|
37
|
+
clear(): void;
|
|
38
|
+
private readonly turnIdsByNativeTurnId;
|
|
39
|
+
private readonly nativeTurnIdsByTurnId;
|
|
40
|
+
private readonly items;
|
|
41
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Maps opaque native identities to SDK identities within an adapter-owned session.
|
|
2
|
+
import { HarnessSdkError } from "../errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* Translate opaque native turn and item IDs when implementing an adapter. Keep one
|
|
5
|
+
* map per native session and use it for events that share those identities. The
|
|
6
|
+
* adapter owns event ordering, failure handling and clearing the map once its
|
|
7
|
+
* identities are no longer needed.
|
|
8
|
+
*/
|
|
9
|
+
export class NativeIdentityMap {
|
|
10
|
+
createId;
|
|
11
|
+
/**
|
|
12
|
+
* Supply an allocator that returns unique, nonempty SDK item IDs. item() invokes it
|
|
13
|
+
* synchronously on a cache miss and propagates failures. The allocator owns uniqueness.
|
|
14
|
+
*/
|
|
15
|
+
constructor(createId) {
|
|
16
|
+
this.createId = createId;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Record a turn pair before translating it through turn() or nativeTurn(). Rebinding
|
|
20
|
+
* the same pair is harmless. A conflicting nonempty stored ID throws HarnessSdkError
|
|
21
|
+
* with code PROTOCOL_ERROR before either lookup changes. Empty stored values permit
|
|
22
|
+
* rebinding, which writes only the supplied pair. The adapter handles conflicts.
|
|
23
|
+
*/
|
|
24
|
+
bindTurn(turnId, nativeTurnId) {
|
|
25
|
+
const existingTurnId = this.turnIdsByNativeTurnId.get(nativeTurnId);
|
|
26
|
+
const existingNativeTurnId = this.nativeTurnIdsByTurnId.get(turnId);
|
|
27
|
+
if ((existingTurnId && existingTurnId !== turnId) ||
|
|
28
|
+
(existingNativeTurnId && existingNativeTurnId !== nativeTurnId)) {
|
|
29
|
+
throw new HarnessSdkError("PROTOCOL_ERROR", "Conflicting native turn identity");
|
|
30
|
+
}
|
|
31
|
+
this.turnIdsByNativeTurnId.set(nativeTurnId, turnId);
|
|
32
|
+
this.nativeTurnIdsByTurnId.set(turnId, nativeTurnId);
|
|
33
|
+
}
|
|
34
|
+
/** Adapters translate a native turn ID to its stored SDK ID, or undefined, without mutation or allocation. */
|
|
35
|
+
turn(nativeTurnId) {
|
|
36
|
+
return this.turnIdsByNativeTurnId.get(nativeTurnId);
|
|
37
|
+
}
|
|
38
|
+
/** Adapters translate an SDK turn ID to its stored native ID, or undefined, without mutation or allocation. */
|
|
39
|
+
nativeTurn(turnId) {
|
|
40
|
+
return this.nativeTurnIdsByTurnId.get(turnId);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Reuse the SDK item ID for a native item within an SDK turn. The turn can be unbound.
|
|
44
|
+
* The allocator runs synchronously when the pair has no nonempty cached ID. An empty
|
|
45
|
+
* result is returned and triggers another allocation on the next lookup. Allocator
|
|
46
|
+
* failures propagate unchanged and leave the adapter free to retry.
|
|
47
|
+
*/
|
|
48
|
+
item(turnId, nativeItemId) {
|
|
49
|
+
// Encode the pair so delimiter characters inside opaque IDs cannot alias another pair.
|
|
50
|
+
const key = JSON.stringify([turnId, nativeItemId]);
|
|
51
|
+
let itemId = this.items.get(key);
|
|
52
|
+
if (!itemId) {
|
|
53
|
+
itemId = this.createId();
|
|
54
|
+
this.items.set(key, itemId);
|
|
55
|
+
}
|
|
56
|
+
return itemId;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Forget both turn directions and all cached item IDs once pending events no longer
|
|
60
|
+
* need them. Later calls can bind and allocate again with the same allocator.
|
|
61
|
+
* Repeated clears are harmless. The adapter remains responsible for native cleanup.
|
|
62
|
+
*/
|
|
63
|
+
clear() {
|
|
64
|
+
this.turnIdsByNativeTurnId.clear();
|
|
65
|
+
this.nativeTurnIdsByTurnId.clear();
|
|
66
|
+
this.items.clear();
|
|
67
|
+
}
|
|
68
|
+
turnIdsByNativeTurnId = new Map();
|
|
69
|
+
nativeTurnIdsByTurnId = new Map();
|
|
70
|
+
items = new Map();
|
|
71
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { AhpRequestMap, InteractionRequested, InteractionResponse, SessionBinding } from '@anureo/protocol';
|
|
2
|
+
import type { HarnessAdapter, RuntimeOptions } from "./ports.js";
|
|
3
|
+
import type { Input } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Register automatic interaction handlers in ClientOptions.interactions. An enumerable own key
|
|
6
|
+
* with value undefined advertises manual handling through interaction/requested events and
|
|
7
|
+
* Session.request('interaction/respond', ...). Omit a kind to leave it unsupported.
|
|
8
|
+
*
|
|
9
|
+
* Session.requestInteraction publishes interaction/requested before scheduling a handler with a
|
|
10
|
+
* detached native request, including any private fields needed to answer it. Resolve an answer
|
|
11
|
+
* matching the request kind for native delivery. Rejection triggers cancellation while actionable.
|
|
12
|
+
* Stop host work when the signal aborts on settlement or teardown. The SDK ignores late answers
|
|
13
|
+
* and holds the handler slot until the handler's promise settles.
|
|
14
|
+
*/
|
|
15
|
+
export type InteractionHandlers = {
|
|
16
|
+
[K in InteractionRequested['kind']]?: (request: Extract<InteractionRequested, {
|
|
17
|
+
kind: K;
|
|
18
|
+
}>, context: {
|
|
19
|
+
signal: AbortSignal;
|
|
20
|
+
}) => Promise<Extract<InteractionResponse, {
|
|
21
|
+
kind: K;
|
|
22
|
+
}>>;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Configure an SDK client and transfer adapter cleanup to it when construction succeeds.
|
|
26
|
+
* Client validates identity, interaction registrations and retention limits before native setup.
|
|
27
|
+
* Await Client.dispose to release owned resources. Host callbacks retain responsibility for their
|
|
28
|
+
* own effects and cleanup.
|
|
29
|
+
*/
|
|
30
|
+
export interface ClientOptions {
|
|
31
|
+
/** Client-owned native integration, initialized before opens and disposed after owned sessions. */
|
|
32
|
+
adapter: HarnessAdapter;
|
|
33
|
+
/** Nonblank adapter and runtime configuration IDs that every session binding must match. */
|
|
34
|
+
identity: Pick<SessionBinding, 'adapterId' | 'runtimeConfigId'>;
|
|
35
|
+
/** Host hooks and resource budgets used by this client and each opened session. */
|
|
36
|
+
runtime?: RuntimeOptions;
|
|
37
|
+
/** Interaction kinds and automatic handlers to advertise during initialization. */
|
|
38
|
+
interactions?: InteractionHandlers;
|
|
39
|
+
/** Positive safe-integer retention budgets; capacity checks reject rather than evict unresolved work. */
|
|
40
|
+
limits?: {
|
|
41
|
+
/** Maximum tracked executions per session, including active executions and pinned results. Defaults to 128. */
|
|
42
|
+
maxExecutions?: number;
|
|
43
|
+
/** Maximum retained submission/association records per session; defaults to 1024. */
|
|
44
|
+
maxSubmissions?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Budget for retained execution item snapshots in serialized UTF-8 bytes. Defaults to 16 MiB.
|
|
47
|
+
* Applies within each execution and across the session. Structured output uses a separate
|
|
48
|
+
* retention budget of the same size.
|
|
49
|
+
*/
|
|
50
|
+
maxResultBytes?: number;
|
|
51
|
+
/** Maximum distinct retained items per execution; defaults to 2048. */
|
|
52
|
+
maxItems?: number;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Supply input and per-submission policy to Session.submit. It validates the request before dispatch
|
|
57
|
+
* and resolves with a Turn when it observes the native execution association, which can precede
|
|
58
|
+
* the receipt or follow completion. Invalid input, unsupported policy and wait failures reject.
|
|
59
|
+
* Dispatch failure can reject an unresolved association. Use Turn.result for the terminal outcome,
|
|
60
|
+
* including structured-output validation, and dispose the Turn to release its local observations.
|
|
61
|
+
*/
|
|
62
|
+
export interface SubmitOptions {
|
|
63
|
+
/** Text or protocol blocks to detach and validate before native dispatch. */
|
|
64
|
+
input: Input;
|
|
65
|
+
/** Capability-gated delivery policy. Omission selects 'native', which can queue input or join an existing execution. */
|
|
66
|
+
delivery?: AhpRequestMap['input/submit']['delivery'];
|
|
67
|
+
/** Per-turn overrides checked against advertised capability keys. */
|
|
68
|
+
overrides?: AhpRequestMap['input/submit']['overrides'];
|
|
69
|
+
/** Require valid JSON matching this schema in the final result. Requires session.capabilities.turn.outputSchema. */
|
|
70
|
+
outputSchema?: AhpRequestMap['input/submit']['outputSchema'];
|
|
71
|
+
}
|
|
File without changes
|