@anureo/adapter-hermes 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/package.json +45 -4
- package/protocol/provenance.json +15 -0
- package/src/attach.d.ts +29 -0
- package/src/attach.js +150 -0
- package/src/events.d.ts +87 -0
- package/src/events.js +274 -0
- package/src/index.d.ts +81 -0
- package/src/index.js +169 -0
- package/src/interactions.d.ts +83 -0
- package/src/interactions.js +206 -0
- package/src/native.d.ts +97 -0
- package/src/native.js +138 -0
- package/src/options.d.ts +60 -0
- package/src/options.js +29 -0
- package/src/rpc.d.ts +94 -0
- package/src/rpc.js +266 -0
- package/src/session.d.ts +151 -0
- package/src/session.js +646 -0
- package/README.md +0 -3
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/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anureo/adapter-hermes",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Anureo SDK adapter for the Hermes canonical TUI gateway",
|
|
4
|
+
"version": "0.3.0",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"anureo",
|
|
8
|
+
"ahp",
|
|
9
|
+
"hermes",
|
|
10
|
+
"agent",
|
|
11
|
+
"gateway"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/hi-youichi/anureo-sdk#readme",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/hi-youichi/anureo-sdk.git",
|
|
17
|
+
"directory": "packages/adapter/hermes"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/hi-youichi/anureo-sdk/issues"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./src/index.d.ts",
|
|
29
|
+
"import": "./src/index.js",
|
|
30
|
+
"default": "./src/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@anureo/protocol": "0.3.0",
|
|
35
|
+
"@anureo/sdk": "0.3.0"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=22"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"src",
|
|
42
|
+
"bin",
|
|
43
|
+
"schema",
|
|
44
|
+
"protocol",
|
|
45
|
+
"examples"
|
|
46
|
+
]
|
|
47
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"repository": "https://github.com/NousResearch/hermes-agent",
|
|
3
|
+
"release": "v0.23.1",
|
|
4
|
+
"commit": "3b421578588243f920886e6fe79a6faa87ea026e",
|
|
5
|
+
"protocol": "hermes-gateway-v1",
|
|
6
|
+
"sources": [
|
|
7
|
+
"hermes_cli/gateway_runtime_cli.py",
|
|
8
|
+
"tui_gateway/contracts/canonical.py",
|
|
9
|
+
"tui_gateway/contracts/canonical_sessions.py",
|
|
10
|
+
"gateway/session_controls.py",
|
|
11
|
+
"gateway/session_pending_controls.py",
|
|
12
|
+
"gateway/session_server_requests.py",
|
|
13
|
+
"gateway/session_settlement_recovery.py"
|
|
14
|
+
]
|
|
15
|
+
}
|
package/src/attach.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { HermesAdapterOptions } from "./options.js";
|
|
2
|
+
/** Validated CLI evidence for one local WebSocket connection. */
|
|
3
|
+
export interface GatewayGrant {
|
|
4
|
+
url: string;
|
|
5
|
+
/** Wire protocol followed by the credential subprotocol. Keep private and mint a fresh grant for each retry. */
|
|
6
|
+
protocols: string[];
|
|
7
|
+
profileId: string;
|
|
8
|
+
instanceId: string;
|
|
9
|
+
/** Owner code hash when supplied, otherwise the wire protocol version. */
|
|
10
|
+
version: string;
|
|
11
|
+
}
|
|
12
|
+
/** CLI owner policy. Attach uses an existing owner, start ensures one, and recover requests owner recovery. */
|
|
13
|
+
export type AttachMode = 'start' | 'attach' | 'recover';
|
|
14
|
+
/**
|
|
15
|
+
* Mint a fresh local gateway ticket through the same-user CLI. HermesAdapter.probeRuntime and
|
|
16
|
+
* HermesSession.connect await this before opening a socket; failures prevent that connection.
|
|
17
|
+
* Resolve after stdout EOF, successful exit, grant validation, child disposal and stderr drain.
|
|
18
|
+
* A pre-aborted signal prevents spawning. While awaiting the grant, abort rejects with
|
|
19
|
+
* RUNTIME_DISPOSED and timeout rejects with retryable HARNESS_UNAVAILABLE. Both dispose the child.
|
|
20
|
+
* Spawn or cleanup failures can propagate directly. Attach submits no work and the caller owns
|
|
21
|
+
* the subsequent WebSocket.
|
|
22
|
+
*/
|
|
23
|
+
export declare function attachGateway(options: HermesAdapterOptions, mode: AttachMode, signal?: AbortSignal): Promise<GatewayGrant>;
|
|
24
|
+
/**
|
|
25
|
+
* Validate the CLI's canonical local endpoint, owner identity and matching ticket subprotocols.
|
|
26
|
+
* attachGateway returns this grant for connection; malformed data throws PROTOCOL_ERROR and
|
|
27
|
+
* differing client/owner code hashes throw HERMES_GATEWAY_CODE_MISMATCH without exposing tickets.
|
|
28
|
+
*/
|
|
29
|
+
export declare function validateGrant(value: unknown): GatewayGrant;
|
package/src/attach.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { HarnessSdkError } from '@anureo/sdk';
|
|
2
|
+
import { spawnHarnessProcess } from '@anureo/sdk/process';
|
|
3
|
+
import { array, GATEWAY_PROTOCOL, identity, object, protocolError } from "./native.js";
|
|
4
|
+
/**
|
|
5
|
+
* Mint a fresh local gateway ticket through the same-user CLI. HermesAdapter.probeRuntime and
|
|
6
|
+
* HermesSession.connect await this before opening a socket; failures prevent that connection.
|
|
7
|
+
* Resolve after stdout EOF, successful exit, grant validation, child disposal and stderr drain.
|
|
8
|
+
* A pre-aborted signal prevents spawning. While awaiting the grant, abort rejects with
|
|
9
|
+
* RUNTIME_DISPOSED and timeout rejects with retryable HARNESS_UNAVAILABLE. Both dispose the child.
|
|
10
|
+
* Spawn or cleanup failures can propagate directly. Attach submits no work and the caller owns
|
|
11
|
+
* the subsequent WebSocket.
|
|
12
|
+
*/
|
|
13
|
+
export async function attachGateway(options, mode, signal) {
|
|
14
|
+
if (signal?.aborted)
|
|
15
|
+
throw new HarnessSdkError('RUNTIME_DISPOSED');
|
|
16
|
+
const args = attachArguments(options, mode);
|
|
17
|
+
const child = await (options.processFactory ?? spawnHarnessProcess)({
|
|
18
|
+
command: options.command ?? 'hermes',
|
|
19
|
+
args,
|
|
20
|
+
env: options.env,
|
|
21
|
+
});
|
|
22
|
+
const stderr = drainAttachStderr(child.stderr, options);
|
|
23
|
+
try {
|
|
24
|
+
return await waitForGrant(child, options.timeoutMs ?? 30_000, signal);
|
|
25
|
+
}
|
|
26
|
+
catch (cause) {
|
|
27
|
+
// Attach has not submitted any work, even when its read/validation fails.
|
|
28
|
+
if (cause instanceof HarnessSdkError)
|
|
29
|
+
throw new HarnessSdkError(cause.code, cause.message, cause.retryable, false);
|
|
30
|
+
throw new HarnessSdkError('PROTOCOL_ERROR', 'Invalid Hermes attach response');
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
await child.dispose();
|
|
34
|
+
await stderr;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Validate the CLI's canonical local endpoint, owner identity and matching ticket subprotocols.
|
|
39
|
+
* attachGateway returns this grant for connection; malformed data throws PROTOCOL_ERROR and
|
|
40
|
+
* differing client/owner code hashes throw HERMES_GATEWAY_CODE_MISMATCH without exposing tickets.
|
|
41
|
+
*/
|
|
42
|
+
export function validateGrant(value) {
|
|
43
|
+
const data = object(value);
|
|
44
|
+
const endpoint = object(data.endpoint);
|
|
45
|
+
if (!['ready', 'draining'].includes(String(data.state)) || data.protocol !== GATEWAY_PROTOCOL)
|
|
46
|
+
throw protocolError();
|
|
47
|
+
if (typeof data.client_code_sha === 'string' && typeof endpoint.code_sha === 'string' && data.client_code_sha !== endpoint.code_sha) {
|
|
48
|
+
throw new HarnessSdkError('HERMES_GATEWAY_CODE_MISMATCH', 'Hermes gateway must be updated or restarted by its owner');
|
|
49
|
+
}
|
|
50
|
+
const address = gatewayAddress(data.url);
|
|
51
|
+
const protocols = array(data.protocols).map(identity);
|
|
52
|
+
const ticket = identity(data.ticket);
|
|
53
|
+
if (protocols.length !== 2 || protocols[0] !== data.protocol || protocols[1] !== `hermes-gateway-ticket.${ticket}`)
|
|
54
|
+
throw protocolError();
|
|
55
|
+
return {
|
|
56
|
+
url: address.href,
|
|
57
|
+
protocols,
|
|
58
|
+
profileId: identity(endpoint.profile_id),
|
|
59
|
+
instanceId: identity(endpoint.instance_id),
|
|
60
|
+
version: typeof endpoint.code_sha === 'string' ? endpoint.code_sha : GATEWAY_PROTOCOL,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Leave owner policy implicit for attach-only and round up for the CLI's whole-second timeout. */
|
|
64
|
+
function attachArguments(options, mode) {
|
|
65
|
+
return [
|
|
66
|
+
...(options.args ?? []),
|
|
67
|
+
...(options.profile ? ['-p', options.profile] : []),
|
|
68
|
+
'gateway', 'attach', '--json',
|
|
69
|
+
...(mode === 'attach' ? [] : [`--${mode}`]),
|
|
70
|
+
'--timeout', String(Math.max(1, Math.ceil((options.timeoutMs ?? 30_000) / 1000))),
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
/** Drain stderr concurrently so the CLI cannot block on its pipe; report only a fixed code and ignore drain failures. */
|
|
74
|
+
async function drainAttachStderr(stderr, options) {
|
|
75
|
+
try {
|
|
76
|
+
for await (const chunk of stderr)
|
|
77
|
+
if (chunk.byteLength)
|
|
78
|
+
options.diagnostic?.('HERMES_ATTACH_STDERR');
|
|
79
|
+
}
|
|
80
|
+
catch { /* Protocol stdout and process exit determine the outcome. */ }
|
|
81
|
+
}
|
|
82
|
+
/** Race the response against attach cancellation, removing the timer/listener before the caller disposes the child. */
|
|
83
|
+
async function waitForGrant(child, timeoutMs, signal) {
|
|
84
|
+
let timer;
|
|
85
|
+
let abort;
|
|
86
|
+
try {
|
|
87
|
+
return await Promise.race([
|
|
88
|
+
readAttachGrant(child),
|
|
89
|
+
new Promise((_, reject) => {
|
|
90
|
+
timer = setTimeout(() => reject(new HarnessSdkError('HARNESS_UNAVAILABLE', 'Hermes gateway attach timed out', true)), timeoutMs);
|
|
91
|
+
}),
|
|
92
|
+
new Promise((_, reject) => {
|
|
93
|
+
abort = () => reject(new HarnessSdkError('RUNTIME_DISPOSED'));
|
|
94
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
95
|
+
if (signal?.aborted)
|
|
96
|
+
abort();
|
|
97
|
+
}),
|
|
98
|
+
]);
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
clearTimeout(timer);
|
|
102
|
+
if (abort)
|
|
103
|
+
signal?.removeEventListener('abort', abort);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Read at most 64 KiB of strict UTF-8 through EOF before trusting a successful CLI exit and parsing its grant. */
|
|
107
|
+
async function readAttachGrant(child) {
|
|
108
|
+
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
109
|
+
let output = '';
|
|
110
|
+
let bytes = 0;
|
|
111
|
+
for await (const chunk of child.stdout) {
|
|
112
|
+
bytes += chunk.byteLength;
|
|
113
|
+
if (bytes > 64 * 1024)
|
|
114
|
+
throw new HarnessSdkError('FRAME_TOO_LARGE');
|
|
115
|
+
output += decoder.decode(chunk, { stream: true });
|
|
116
|
+
}
|
|
117
|
+
output += decoder.decode();
|
|
118
|
+
assertAttachExit(await child.exited);
|
|
119
|
+
let result;
|
|
120
|
+
try {
|
|
121
|
+
result = JSON.parse(output);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
throw protocolError();
|
|
125
|
+
}
|
|
126
|
+
return validateGrant(result);
|
|
127
|
+
}
|
|
128
|
+
/** Reject unsuccessful CLI exits using safe messages. Only exit 5 represents a retryable failure. */
|
|
129
|
+
function assertAttachExit({ code }) {
|
|
130
|
+
if (code === 0)
|
|
131
|
+
return;
|
|
132
|
+
if (code === 2)
|
|
133
|
+
throw new HarnessSdkError('HERMES_GATEWAY_UNSUPPORTED', 'Hermes gateway attach is required');
|
|
134
|
+
const failure = code === 3 ? 'HERMES_GATEWAY_UNSUPPORTED' : code === 4 ? 'AUTHORIZATION_FAILED' : 'HARNESS_UNAVAILABLE';
|
|
135
|
+
throw new HarnessSdkError(failure, 'Hermes gateway attach failed', code === 5);
|
|
136
|
+
}
|
|
137
|
+
/** Keep tickets on the loopback WebSocket path and out of URL credentials, query strings and fragments. */
|
|
138
|
+
function gatewayAddress(value) {
|
|
139
|
+
let address;
|
|
140
|
+
try {
|
|
141
|
+
address = new URL(identity(value));
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
throw protocolError();
|
|
145
|
+
}
|
|
146
|
+
if (!['ws:', 'wss:'].includes(address.protocol) || !['127.0.0.1', '[::1]', 'localhost'].includes(address.hostname)
|
|
147
|
+
|| address.username || address.password || address.search || address.hash || address.pathname !== '/api/ws')
|
|
148
|
+
throw protocolError();
|
|
149
|
+
return address;
|
|
150
|
+
}
|
package/src/events.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type SessionEventSink } from '@anureo/sdk';
|
|
2
|
+
import { type NativeEvent, type NativeObject } from "./native.js";
|
|
3
|
+
import type { HermesAdapterOptions } from "./options.js";
|
|
4
|
+
/**
|
|
5
|
+
* Publish cumulative session usage for HermesSession.apply and terminal turn projection.
|
|
6
|
+
* Omit nonfinite or negative counters and await sink publication.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sessionUsage(sink: SessionEventSink, payload: NativeObject): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Internal execution projection created by HermesSession.turn. Publish ordered SDK items through
|
|
11
|
+
* its sink, retaining bounded provisional state until native terminal evidence arrives. Native
|
|
12
|
+
* snapshots complete items; deltas only describe provisional progress. Sink failures reject to the
|
|
13
|
+
* session's receipt or event path; the session owns recovery and channel disposal.
|
|
14
|
+
*/
|
|
15
|
+
export declare class HermesTurn {
|
|
16
|
+
/** SDK identity shared by submission bindings, events and interrupt targeting. */
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/** Durable native admission used to reconcile this execution after reconnect. */
|
|
19
|
+
readonly admissionId: string;
|
|
20
|
+
/** Native authority paired with generation; HermesSession checks both when reusing this execution. */
|
|
21
|
+
readonly epoch: number;
|
|
22
|
+
/** Native execution generation within this authority, used to fence interrupt requests. */
|
|
23
|
+
readonly generation: number;
|
|
24
|
+
/** Set before publishing turn/started to suppress duplicate starts during association and event delivery. */
|
|
25
|
+
started: boolean;
|
|
26
|
+
/** Set before turn/completed publication. HermesSession uses it to stop treating this turn as active. */
|
|
27
|
+
terminal: boolean;
|
|
28
|
+
/** Set with terminal evidence before turn/completed publication, and retained after item content is released. */
|
|
29
|
+
status?: TerminalStatus;
|
|
30
|
+
/** Accept validated native execution identity and derive a local turn id stable across replay. */
|
|
31
|
+
constructor({ admissionId, epoch, generation, sessionId, sink, options }: {
|
|
32
|
+
admissionId: string;
|
|
33
|
+
epoch: number;
|
|
34
|
+
generation: number;
|
|
35
|
+
sessionId: string;
|
|
36
|
+
sink: SessionEventSink;
|
|
37
|
+
options: HermesAdapterOptions;
|
|
38
|
+
});
|
|
39
|
+
/** Publish turn/started at most once, before any binding or item publication. */
|
|
40
|
+
start(): Promise<void>;
|
|
41
|
+
/** HermesSession.associate binds native-claimed input once; late binding of a terminal turn adds no user item. */
|
|
42
|
+
bind(submissionId: string, input: string): Promise<void>;
|
|
43
|
+
/** Report incomplete progress once and stop appending to the current partial segment; native snapshots can still replace its text. */
|
|
44
|
+
gap(): Promise<void>;
|
|
45
|
+
/** Project the event supplied by HermesSession.applyTurnEvent. Ignore terminal replays and reject malformed payloads or sink failures. */
|
|
46
|
+
apply(event: NativeEvent): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* HermesSession.associate supplies receipt-only terminal evidence after reconnect or submit.
|
|
49
|
+
* Mark unavailable intermediate progress, then publish the final response; unsupported outcomes or
|
|
50
|
+
* non-string final_response reject as protocol errors. Missing final_response means empty text.
|
|
51
|
+
*/
|
|
52
|
+
fromReceipt(result: NativeObject, outcome: string | null): Promise<void>;
|
|
53
|
+
private readonly items;
|
|
54
|
+
private readonly completed;
|
|
55
|
+
private readonly submissions;
|
|
56
|
+
private segment;
|
|
57
|
+
private current?;
|
|
58
|
+
private lastText;
|
|
59
|
+
private reasoning;
|
|
60
|
+
private bytes;
|
|
61
|
+
private gapReported;
|
|
62
|
+
/** Replace streamed commentary with the native snapshot and seal the segment unless it is stale. */
|
|
63
|
+
private messageInterim;
|
|
64
|
+
/** Retain the accumulated reasoning summary while publishing only the new chunk as a delta. */
|
|
65
|
+
private reasoningDelta;
|
|
66
|
+
/**
|
|
67
|
+
* Project both tool phases under one native identity. Ignore duplicate starts, retain prior input
|
|
68
|
+
* when completion omits it, and convert native duration seconds to SDK milliseconds.
|
|
69
|
+
*/
|
|
70
|
+
private toolEvent;
|
|
71
|
+
/** Require a recognized terminal status. Use retained text only when both native text fields are absent or null. */
|
|
72
|
+
private messageComplete;
|
|
73
|
+
/** Retain a detached item within count/byte budgets and publish item/started only on first appearance. */
|
|
74
|
+
private put;
|
|
75
|
+
/** Publish item completion at most once per id, starting an unseen item first. */
|
|
76
|
+
private complete;
|
|
77
|
+
/** Seal the current commentary segment and retain its text as the fallback when a terminal frame omits text. */
|
|
78
|
+
private commentary;
|
|
79
|
+
/** Reconcile UTF-16 text offsets within a commentary segment; conflicting overlap rejects and a missing prefix marks a gap. */
|
|
80
|
+
private messageDelta;
|
|
81
|
+
/** Complete final/reasoning items before terminal publication, flag unfinished native items and release retained content after success. */
|
|
82
|
+
private finish;
|
|
83
|
+
private readonly sink;
|
|
84
|
+
private readonly options;
|
|
85
|
+
}
|
|
86
|
+
type TerminalStatus = 'completed' | 'interrupted' | 'failed';
|
|
87
|
+
export {};
|