@ggui-ai/protocol-reference-server 0.1.0-rc.1
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 +45 -0
- package/dist/action-router.d.ts +40 -0
- package/dist/action-router.d.ts.map +1 -0
- package/dist/action-router.js +283 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +110 -0
- package/dist/conformance-host.d.ts +44 -0
- package/dist/conformance-host.d.ts.map +1 -0
- package/dist/conformance-host.js +185 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/server.d.ts +59 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +224 -0
- package/dist/session.d.ts +142 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +134 -0
- package/dist/tool-registry.d.ts +63 -0
- package/dist/tool-registry.d.ts.map +1 -0
- package/dist/tool-registry.js +99 -0
- package/package.json +64 -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 describing the origin of the Work and
|
|
141
|
+
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 Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
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 Loqu, Inc. (Guuey)
|
|
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
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @ggui-ai/protocol-reference-server
|
|
2
|
+
|
|
3
|
+
Minimal reference implementation of the **ggui protocol** — the narrow WebSocket server
|
|
4
|
+
that `@ggui-ai/protocol-conformance` runs against to prove the protocol is vendor-neutral.
|
|
5
|
+
|
|
6
|
+
This package is **not a production server**. It implements exactly enough of the
|
|
7
|
+
ggui live-channel WebSocket wire to pass the conformance kit. Its deliberate design
|
|
8
|
+
constraint — zero runtime dependency on `@ggui-ai/mcp-server*` — is what makes the
|
|
9
|
+
vendor-neutrality claim real: an independent, from-scratch implementation passing the
|
|
10
|
+
kit grounds the claim empirically rather than by assertion.
|
|
11
|
+
|
|
12
|
+
## Scope
|
|
13
|
+
|
|
14
|
+
- WebSocket transport matching the ggui live-channel wire.
|
|
15
|
+
- In-memory session store (no persistence).
|
|
16
|
+
- `schemaVersion` handshake with `UPGRADE_REQUIRED` on mismatch.
|
|
17
|
+
- A 4-handler wired-action registry:
|
|
18
|
+
- `echo` — returns `{received: args}`.
|
|
19
|
+
- `throw` — rejects → `TOOL_THREW` emitted on `_ggui:contract-error`.
|
|
20
|
+
- `timeout` — never resolves → `TOOL_TIMEOUT` after 500ms.
|
|
21
|
+
- `malformed` — shape-mismatched return → `SCHEMA_VIOLATION`.
|
|
22
|
+
- `TOOL_NOT_FOUND` when an unregistered action is invoked.
|
|
23
|
+
|
|
24
|
+
## Non-scope
|
|
25
|
+
|
|
26
|
+
- No authentication (accepts any bearer verbatim).
|
|
27
|
+
- No bundle loading / embedded-ui.
|
|
28
|
+
- No persistence — restart drops all state.
|
|
29
|
+
- Not intended as a runtime for agents. Use `@ggui-ai/mcp-server` for that.
|
|
30
|
+
|
|
31
|
+
## Boot
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
npx @ggui-ai/protocol-reference-server --port 3100
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Prints `READY ws://127.0.0.1:3100/ws` when bound. Ctrl-C to stop.
|
|
38
|
+
|
|
39
|
+
## CI proof
|
|
40
|
+
|
|
41
|
+
`src/conformance.test.ts` boots this server and runs `@ggui-ai/protocol-conformance`
|
|
42
|
+
against it through a `ConformanceHost` adapter. Every drivable conformance fixture must
|
|
43
|
+
pass (bootstrap-success, wired-action-success, wired-action-tool-threw,
|
|
44
|
+
stream-schema-violation); directives outside this server's scope (renderer-url-override,
|
|
45
|
+
ui-initialize-response-override, and similar) skip cleanly per the kit's design.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Session } from './session.js';
|
|
2
|
+
import type { ToolRegistry } from './tool-registry.js';
|
|
3
|
+
/**
|
|
4
|
+
* One inbound action frame shape. Matches the fixtures' authored
|
|
5
|
+
* `inputEnvelope` for `wired-action-*` cases — the runner sends the
|
|
6
|
+
* envelope verbatim.
|
|
7
|
+
*/
|
|
8
|
+
interface IncomingActionFrame {
|
|
9
|
+
readonly type: 'action';
|
|
10
|
+
readonly channel?: number;
|
|
11
|
+
readonly sessionId: string;
|
|
12
|
+
readonly action: {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly data?: unknown;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Type-guard the action frame shape before dispatching. Anything
|
|
19
|
+
* malformed is dropped with a `no-op` warning — the matcher for
|
|
20
|
+
* `no-op` fixtures expects silence, so loud rejection would break
|
|
21
|
+
* them.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isActionFrame(frame: unknown): frame is IncomingActionFrame;
|
|
24
|
+
export interface DispatchContext {
|
|
25
|
+
readonly session: Session;
|
|
26
|
+
readonly tools: ToolRegistry;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Dispatch one action frame. Runs asynchronously; contract-error
|
|
30
|
+
* emissions are broadcast to every subscriber on the session via
|
|
31
|
+
* `session.subscribers`.
|
|
32
|
+
*
|
|
33
|
+
* Returns when the dispatch's observable outcome has been emitted
|
|
34
|
+
* (either a happy-path observability frame or a contract-error).
|
|
35
|
+
* The awaiting call site is the WS message handler — letting it
|
|
36
|
+
* await ensures the kit's observation window captures the emission.
|
|
37
|
+
*/
|
|
38
|
+
export declare function dispatchAction(frame: IncomingActionFrame, context: DispatchContext): Promise<void>;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=action-router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action-router.d.ts","sourceRoot":"","sources":["../src/action-router.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAMvD;;;;GAIG;AACH,UAAU,mBAAmB;IAC3B,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE;QACf,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;KACzB,CAAC;CACH;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,mBAAmB,CAS1E;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;CAC9B;AAED;;;;;;;;;GASG;AACH,wBAAsB,cAAc,CAClC,KAAK,EAAE,mBAAmB,EAC1B,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,IAAI,CAAC,CAsIf"}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wired-action dispatcher — consumes an inbound `action` frame and
|
|
3
|
+
* emits the matching `_ggui:contract-error` stream frame (plus happy-
|
|
4
|
+
* path effects) per SPEC §4.4 + Contract #3.
|
|
5
|
+
*
|
|
6
|
+
* This file owns every ContractErrorCode emission path the
|
|
7
|
+
* conformance kit's matcher asserts on:
|
|
8
|
+
*
|
|
9
|
+
* - TOOL_NOT_FOUND — dispatched action's tool is not registered
|
|
10
|
+
* - TOOL_THREW — handler throws
|
|
11
|
+
* - TOOL_TIMEOUT — handler exceeds `TIMEOUT_MS` (500ms)
|
|
12
|
+
* - SCHEMA_VIOLATION — handler returns shape not matching declared
|
|
13
|
+
* channel schema (currently: `malformed` kind
|
|
14
|
+
* always trips this path)
|
|
15
|
+
*
|
|
16
|
+
* Happy-path: echo handler's success return fires an observability
|
|
17
|
+
* signal (kit's `wired-action-success` fixture asserts the router
|
|
18
|
+
* dispatch observably happened). Since observability events are
|
|
19
|
+
* unmatchable-on-ws per the conformance kit's matcher, the
|
|
20
|
+
* happy-path emission here is best-effort: we emit a non-reserved
|
|
21
|
+
* stream frame on `_ggui:wired-tool-invoked` carrying
|
|
22
|
+
* `{toolName, actionName}` so an iframe-host kit can match the same
|
|
23
|
+
* tool invocation signal from either side. Pure WS kit currently
|
|
24
|
+
* treats this as SKIP.
|
|
25
|
+
*/
|
|
26
|
+
import { makeContractErrorPayload } from '@ggui-ai/protocol';
|
|
27
|
+
/** Timeout the `timeout` handler exceeds; the kit's matcher
|
|
28
|
+
* recognizes TOOL_TIMEOUT within this window. */
|
|
29
|
+
const TIMEOUT_MS = 500;
|
|
30
|
+
/**
|
|
31
|
+
* Type-guard the action frame shape before dispatching. Anything
|
|
32
|
+
* malformed is dropped with a `no-op` warning — the matcher for
|
|
33
|
+
* `no-op` fixtures expects silence, so loud rejection would break
|
|
34
|
+
* them.
|
|
35
|
+
*/
|
|
36
|
+
export function isActionFrame(frame) {
|
|
37
|
+
if (frame === null || typeof frame !== 'object')
|
|
38
|
+
return false;
|
|
39
|
+
const f = frame;
|
|
40
|
+
if (f['type'] !== 'action')
|
|
41
|
+
return false;
|
|
42
|
+
if (typeof f['sessionId'] !== 'string')
|
|
43
|
+
return false;
|
|
44
|
+
const action = f['action'];
|
|
45
|
+
if (action === null || typeof action !== 'object')
|
|
46
|
+
return false;
|
|
47
|
+
const a = action;
|
|
48
|
+
return typeof a['name'] === 'string';
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Dispatch one action frame. Runs asynchronously; contract-error
|
|
52
|
+
* emissions are broadcast to every subscriber on the session via
|
|
53
|
+
* `session.subscribers`.
|
|
54
|
+
*
|
|
55
|
+
* Returns when the dispatch's observable outcome has been emitted
|
|
56
|
+
* (either a happy-path observability frame or a contract-error).
|
|
57
|
+
* The awaiting call site is the WS message handler — letting it
|
|
58
|
+
* await ensures the kit's observation window captures the emission.
|
|
59
|
+
*/
|
|
60
|
+
export async function dispatchAction(frame, context) {
|
|
61
|
+
const { session, tools } = context;
|
|
62
|
+
const actionName = frame.action.name;
|
|
63
|
+
// Action → tool resolution, in priority order:
|
|
64
|
+
// 1. Explicit `register-actionspec` directive on this session.
|
|
65
|
+
// 2. Tool whose name equals the action name (1:1 convention).
|
|
66
|
+
//
|
|
67
|
+
// A real ggui server uses a blueprint's declared `actionSpec` to
|
|
68
|
+
// bind action names to tool names; the reference server honors the
|
|
69
|
+
// same binding via the fixture's explicit `register-actionspec`
|
|
70
|
+
// setup directive. If neither path resolves, emit TOOL_NOT_FOUND
|
|
71
|
+
// with `toolName = actionName` — the error payload names the
|
|
72
|
+
// missing tool by the identifier the dispatcher was asked to
|
|
73
|
+
// resolve, which is the action name itself.
|
|
74
|
+
const actionSpec = session.actionSpecs.get(actionName);
|
|
75
|
+
let toolName = actionSpec?.tool;
|
|
76
|
+
if (toolName === undefined && tools.has(actionName)) {
|
|
77
|
+
toolName = actionName;
|
|
78
|
+
}
|
|
79
|
+
if (toolName === undefined) {
|
|
80
|
+
emitContractError(session, {
|
|
81
|
+
code: 'TOOL_NOT_FOUND',
|
|
82
|
+
toolName: actionName,
|
|
83
|
+
actionName,
|
|
84
|
+
message: `no actionSpec bound action '${actionName}' and no tool of that name is registered`,
|
|
85
|
+
});
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const tool = tools.get(toolName);
|
|
89
|
+
if (tool === undefined) {
|
|
90
|
+
emitContractError(session, {
|
|
91
|
+
code: 'TOOL_NOT_FOUND',
|
|
92
|
+
toolName,
|
|
93
|
+
actionName,
|
|
94
|
+
message: `tool '${toolName}' is not registered`,
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// Malformed handler is defined to return the wrong shape — emit
|
|
99
|
+
// SCHEMA_VIOLATION instead of passing the bad return through.
|
|
100
|
+
if (tool.kind === 'malformed') {
|
|
101
|
+
emitContractError(session, {
|
|
102
|
+
code: 'SCHEMA_VIOLATION',
|
|
103
|
+
toolName: tool.name,
|
|
104
|
+
actionName,
|
|
105
|
+
message: `tool '${tool.name}' returned a shape that does not match the declared channel schema`,
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
// Run the handler with a timeout bound. `timeout` kind wins the
|
|
110
|
+
// timeout race deliberately; other handlers should resolve or
|
|
111
|
+
// throw well before 500ms.
|
|
112
|
+
let handlerOutcome;
|
|
113
|
+
try {
|
|
114
|
+
handlerOutcome = await Promise.race([
|
|
115
|
+
(async () => {
|
|
116
|
+
try {
|
|
117
|
+
const value = await tool.handler(frame.action.data);
|
|
118
|
+
return { kind: 'resolved', value };
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
return {
|
|
122
|
+
kind: 'rejected',
|
|
123
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
})(),
|
|
127
|
+
new Promise((done) => {
|
|
128
|
+
setTimeout(() => done({ kind: 'timeout' }), TIMEOUT_MS);
|
|
129
|
+
}),
|
|
130
|
+
]);
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
// Promise.race never rejects since we catch inside; defensive.
|
|
134
|
+
emitContractError(session, {
|
|
135
|
+
code: 'TOOL_THREW',
|
|
136
|
+
toolName: tool.name,
|
|
137
|
+
actionName,
|
|
138
|
+
message: err.message ?? String(err),
|
|
139
|
+
causedBy: err.stack?.split('\n').slice(0, 3).join('\n'),
|
|
140
|
+
});
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (handlerOutcome.kind === 'timeout') {
|
|
144
|
+
emitContractError(session, {
|
|
145
|
+
code: 'TOOL_TIMEOUT',
|
|
146
|
+
toolName: tool.name,
|
|
147
|
+
actionName,
|
|
148
|
+
message: `tool '${tool.name}' exceeded ${TIMEOUT_MS}ms`,
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (handlerOutcome.kind === 'rejected') {
|
|
153
|
+
emitContractError(session, {
|
|
154
|
+
code: 'TOOL_THREW',
|
|
155
|
+
toolName: tool.name,
|
|
156
|
+
actionName,
|
|
157
|
+
message: handlerOutcome.error.message,
|
|
158
|
+
causedBy: handlerOutcome.error.stack?.split('\n').slice(0, 3).join('\n'),
|
|
159
|
+
});
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
// Happy path — emit the observability signal so iframe-host kits
|
|
163
|
+
// can observe it. WS-only kit's matcher will return
|
|
164
|
+
// `unmatchable-on-ws` for `observability-event`, which the runner
|
|
165
|
+
// maps to SKIP (not FAIL). That's the intended behavior per the
|
|
166
|
+
// kit's design note at the top of `match-behavior.ts`.
|
|
167
|
+
broadcast(session, {
|
|
168
|
+
type: 'stream',
|
|
169
|
+
payload: {
|
|
170
|
+
channel: '_ggui:wired-tool-invoked',
|
|
171
|
+
value: { toolName: tool.name, actionName },
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
// Refresh-stream fan-out (SPEC §2.3 StreamSpec refresh triggers).
|
|
175
|
+
// After a successful wired-action dispatch, every streamSpec
|
|
176
|
+
// declared on the session fires its refresh tool and emits a
|
|
177
|
+
// stream-update on the bound channel. This is the wire-level
|
|
178
|
+
// proof of the refresh-after-action contract the kit's
|
|
179
|
+
// `stream-refresh-success` fixture asserts.
|
|
180
|
+
//
|
|
181
|
+
// Refresh-tool failures are emitted as contract-errors with
|
|
182
|
+
// `sourceAction: 'refresh-stream'` so the kit's matcher can
|
|
183
|
+
// distinguish action-path failures from refresh-path failures
|
|
184
|
+
// (and so `stream-schema-violation` has a path forward when its
|
|
185
|
+
// fixture flips off the known-failures list).
|
|
186
|
+
await dispatchRefreshStreams(session, tools);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Run every streamSpec's refresh tool and broadcast the result on
|
|
190
|
+
* its bound channel. Errors map to canonical `_ggui:contract-error`
|
|
191
|
+
* envelopes with `sourceAction: 'refresh-stream'`. Sequential
|
|
192
|
+
* dispatch keeps subscriber-frame ordering deterministic — the
|
|
193
|
+
* kit's matcher doesn't depend on order today, but a real ggui
|
|
194
|
+
* runtime emits in declaration order and the reference server
|
|
195
|
+
* preserves that contract.
|
|
196
|
+
*/
|
|
197
|
+
async function dispatchRefreshStreams(session, tools) {
|
|
198
|
+
for (const spec of session.streamSpecs.values()) {
|
|
199
|
+
const refreshTool = tools.get(spec.tool);
|
|
200
|
+
if (refreshTool === undefined) {
|
|
201
|
+
emitContractError(session, {
|
|
202
|
+
code: 'TOOL_NOT_FOUND',
|
|
203
|
+
toolName: spec.tool,
|
|
204
|
+
actionName: spec.channel,
|
|
205
|
+
message: `streamSpec for channel '${spec.channel}' references tool '${spec.tool}', which is not registered`,
|
|
206
|
+
sourceActionType: 'refresh-stream',
|
|
207
|
+
});
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (refreshTool.kind === 'malformed' || refreshTool.kind === 'malformed-stream') {
|
|
211
|
+
emitContractError(session, {
|
|
212
|
+
code: 'SCHEMA_VIOLATION',
|
|
213
|
+
toolName: refreshTool.name,
|
|
214
|
+
actionName: spec.channel,
|
|
215
|
+
message: `refresh tool '${refreshTool.name}' returned a shape that does not match channel '${spec.channel}'`,
|
|
216
|
+
sourceActionType: 'refresh-stream',
|
|
217
|
+
});
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
let value;
|
|
221
|
+
try {
|
|
222
|
+
value = await refreshTool.handler(undefined);
|
|
223
|
+
}
|
|
224
|
+
catch (err) {
|
|
225
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
226
|
+
emitContractError(session, {
|
|
227
|
+
code: 'TOOL_THREW',
|
|
228
|
+
toolName: refreshTool.name,
|
|
229
|
+
actionName: spec.channel,
|
|
230
|
+
message: error.message,
|
|
231
|
+
causedBy: error.stack?.split('\n').slice(0, 3).join('\n'),
|
|
232
|
+
sourceActionType: 'refresh-stream',
|
|
233
|
+
});
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
broadcast(session, {
|
|
237
|
+
type: 'stream',
|
|
238
|
+
payload: {
|
|
239
|
+
channel: spec.channel,
|
|
240
|
+
value,
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function emitContractError(session, input) {
|
|
246
|
+
// Canonical SPEC §4.4 `ContractErrorPayload` via the central
|
|
247
|
+
// builder. Nested `error: {code, message, causedBy}` alongside
|
|
248
|
+
// flat `toolName` / `actionName` / `sourceAction` / `timestamp`.
|
|
249
|
+
// The conformance kit's matcher reads this canonical shape
|
|
250
|
+
// directly — no dual-shape workaround needed.
|
|
251
|
+
const value = makeContractErrorPayload({
|
|
252
|
+
toolName: input.toolName,
|
|
253
|
+
actionName: input.actionName,
|
|
254
|
+
sourceAction: {
|
|
255
|
+
type: input.sourceActionType ?? 'wired-action',
|
|
256
|
+
dispatchedAt: new Date().toISOString(),
|
|
257
|
+
},
|
|
258
|
+
error: {
|
|
259
|
+
code: input.code,
|
|
260
|
+
message: input.message,
|
|
261
|
+
...(input.causedBy !== undefined ? { causedBy: input.causedBy } : {}),
|
|
262
|
+
},
|
|
263
|
+
timestamp: new Date().toISOString(),
|
|
264
|
+
});
|
|
265
|
+
broadcast(session, {
|
|
266
|
+
type: 'stream',
|
|
267
|
+
payload: {
|
|
268
|
+
channel: '_ggui:contract-error',
|
|
269
|
+
value,
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
function broadcast(session, frame) {
|
|
274
|
+
for (const subscriber of session.subscribers) {
|
|
275
|
+
try {
|
|
276
|
+
subscriber.send(frame);
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
// Subscriber lifecycle issues (closed socket, etc.) are the
|
|
280
|
+
// subscriber's problem — the router keeps broadcasting.
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
interface ParsedArgs {
|
|
3
|
+
readonly port: number;
|
|
4
|
+
readonly host: string;
|
|
5
|
+
readonly help: boolean;
|
|
6
|
+
}
|
|
7
|
+
declare function parseArgs(argv: readonly string[]): ParsedArgs;
|
|
8
|
+
declare function main(argv: readonly string[]): Promise<number>;
|
|
9
|
+
export { main, parseArgs };
|
|
10
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAiBA,UAAU,UAAU;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;CACxB;AAED,iBAAS,SAAS,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,UAAU,CAiCtD;AAkBD,iBAAe,IAAI,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CA4B5D;AAuBD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC"}
|