@gnldev/agui 0.1.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 +188 -0
- package/dist/convert.d.ts +36 -0
- package/dist/convert.js +133 -0
- package/dist/convert.js.map +1 -0
- package/dist/handler.d.ts +5 -0
- package/dist/handler.js +5 -0
- package/dist/handler.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/route.d.ts +53 -0
- package/dist/route.js +324 -0
- package/dist/route.js.map +1 -0
- package/dist/types.d.ts +88 -0
- package/dist/types.js +22 -0
- package/dist/types.js.map +1 -0
- package/package.json +63 -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 Karaca Yılmaz (https://gnl.dev)
|
|
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
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# @gnldev/agui
|
|
2
|
+
|
|
3
|
+
**AG-UI protocol adapter**: converts `@gnldev/server`'s SSE contract ([see `@gnldev/server` README](../server/README.md)) into [AG-UI](https://github.com/ag-ui-protocol/ag-ui) (CopilotKit's open agent↔UI event protocol) event sequences. **Zero `@ag-ui/*` dependency** — event types are hand-defined (AG-UI is an open SSE/JSON protocol, no SDK required).
|
|
4
|
+
|
|
5
|
+
> Install: `pnpm add @gnldev/agui` — or use it from a [repo clone](https://github.com/Karaca7/gnldev): `pnpm install && pnpm -r build`.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @gnldev/agui # dep: @gnldev/server, hono · peer: @gnldev/durable
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createAguiRoute } from '@gnldev/agui';
|
|
13
|
+
import { serve } from '@hono/node-server';
|
|
14
|
+
import { SqliteStorage } from '@gnldev/durable/sqlite';
|
|
15
|
+
|
|
16
|
+
const app = createAguiRoute({
|
|
17
|
+
journal: new SqliteStorage('runs.db').runs,
|
|
18
|
+
agents: { support: { model: 'anthropic/claude-opus-4-8', tools, guard, maxSteps: 8 } },
|
|
19
|
+
});
|
|
20
|
+
serve({ fetch: app.fetch, port: 3001 }); // POST /agents/:name/run → AG-UI SSE
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`createAguiRoute` returns a fetch handler, so it binds to any Node server through the bridge in
|
|
24
|
+
@gnldev/server — a package this one already depends on, so there is nothing extra to install:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { toNodeHandler } from '@gnldev/server/node';
|
|
28
|
+
|
|
29
|
+
express().use('/agui', toNodeHandler(route)); // mount BEFORE express.json()
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Connecting to the CopilotKit side
|
|
33
|
+
AG-UI's `HttpAgent` (`@ag-ui/client`) can POST directly to this endpoint:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { HttpAgent } from '@ag-ui/client';
|
|
37
|
+
|
|
38
|
+
const agent = new HttpAgent({ url: 'http://localhost:3001/agents/support/run' });
|
|
39
|
+
agent.runAgent({ runId: 'r1', threadId: 't1', prompt: 'hi' });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Honesty note**: this is a hand-extracted implementation of a subset of the AG-UI core events — it is NOT VERIFIED against the official
|
|
43
|
+
`@ag-ui/*` conformance test suite. Fields we're not sure about from the [upstream AG-UI
|
|
44
|
+
spec](https://github.com/ag-ui-protocol/ag-ui) are marked with comments in `types.ts`/`convert.ts`
|
|
45
|
+
(no made-up fields were added) — there is no vendored copy of the spec in this repo to check against.
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
- `createAguiRoute(config, opts?)` — a single-endpoint Hono router from `@gnldev/durable`'s `CreateGnlConfig`:
|
|
49
|
+
`POST /agents/:name/run {runId, prompt|messages, threadId?, approvals?}` → AG-UI SSE. Deliberately
|
|
50
|
+
small: NO auth/tenancy/budget gates. If these are needed, use `@gnldev/server`'s `createRestApi` to
|
|
51
|
+
produce a `gnl.stream(...)` result and pass it to `pipeAguiStream`.
|
|
52
|
+
- `pipeAguiStream(c, runId, result, opts?)` — the AG-UI-output counterpart of `pipeAgentStream` (takes a Hono `Context`
|
|
53
|
+
+ AI SDK `StreamTextResult`, starts with `RUN_STARTED`, ends with `RUN_FINISHED`/`RUN_ERROR`).
|
|
54
|
+
If `opts.threadId` is not given, `runId` is used.
|
|
55
|
+
- `toAguiEvents(gnlEvent, ctx, state?)` — a PURE (I/O-free) converter: converts a single GNL SSE event
|
|
56
|
+
(`{event, data}` — the schema from `@gnldev/server`'s W3 id contract) into an AG-UI event sequence. `state`
|
|
57
|
+
is threaded in from outside (`initialAguiConvertState`) — a SINGLE state object must be threaded
|
|
58
|
+
through from start to end for an ENTIRE run (this is required for text message framing START/END).
|
|
59
|
+
|
|
60
|
+
## Who is this request for? (`identity`)
|
|
61
|
+
|
|
62
|
+
This route declares auth out of scope, and that boundary is right. What it leaves you responsible for
|
|
63
|
+
is naming the end user each run acts for — the subject memory scopes on and every ownership gate
|
|
64
|
+
compares against.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { createAguiRoute } from '@gnldev/agui';
|
|
68
|
+
|
|
69
|
+
const route = createAguiRoute(config, {
|
|
70
|
+
// The SAME signature @gnldev/chat-adapter's route takes — write the function once, mount either adapter.
|
|
71
|
+
identity: (req) => {
|
|
72
|
+
const session = db.sessions.get(req.headers.get('cookie')); // YOUR session store
|
|
73
|
+
return session ? { resourceId: session.userId, threadId: session.conversationId } : undefined;
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
It receives the web `Request` (not the Hono context, so an Express or Fastify bridge can use it), is
|
|
79
|
+
called once per request, and may return `undefined`. **Read it from something the server trusts** — a
|
|
80
|
+
session cookie, a verified JWT — and never from the request body: the engine treats its reserved
|
|
81
|
+
context keys as "the server established this", and a body-supplied subject is the caller naming
|
|
82
|
+
whoever they like. The route always seals the context, so that forgery is closed either way; what
|
|
83
|
+
`identity` decides is whether the run has an owner **at all**.
|
|
84
|
+
|
|
85
|
+
Precedence, field by field: `resolveResourceId` / `resolveThreadId` win (the existing contract), then
|
|
86
|
+
`identity`, then the body. With none of them, runs are born ownerless — ownership gates stay
|
|
87
|
+
fail-open — and in `NODE_ENV=production` the route says so once with a `console.warn`. It never
|
|
88
|
+
throws.
|
|
89
|
+
|
|
90
|
+
`@gnldev/chat-adapter`'s README carries the long version of the same section, including the attack it was
|
|
91
|
+
measured against.
|
|
92
|
+
|
|
93
|
+
## Which run is this? (`workKey`, and the two regimes)
|
|
94
|
+
|
|
95
|
+
Every request names one of two things, and exactly one:
|
|
96
|
+
|
|
97
|
+
- **`workKey`** — your name for a unit of work. The engine derives the run's id from it, so the same
|
|
98
|
+
name always lands on the same run: durable replay, side effects once. It is a *business* name (the
|
|
99
|
+
invoice being issued, tonight's reconciliation), so keep sensitive data out of it — it is reflected
|
|
100
|
+
in error details and shown on Studio screens.
|
|
101
|
+
- **`runId`** — a raw id you already hold (a resume, a fork, an id you stored).
|
|
102
|
+
|
|
103
|
+
Sending both is a 400. Sending neither is a 400. `Idempotency-Key` is accepted when the body named
|
|
104
|
+
nothing itself.
|
|
105
|
+
|
|
106
|
+
**The derived id is not in a header.** `@gnldev/server` and `@gnldev/chat-adapter` return
|
|
107
|
+
`X-Gnl-Run-Id`; this route's answer is a stream of AG-UI events, so the id travels in the event
|
|
108
|
+
envelope where it belongs to the run rather than to the HTTP response.
|
|
109
|
+
|
|
110
|
+
### Two regimes, decided by whether the route can name a subject
|
|
111
|
+
|
|
112
|
+
Deriving an id from a name needs an **address** — otherwise the engine cannot tell whose job it is.
|
|
113
|
+
This route ships with no auth, so it has deployments that can name nobody, and the rules differ by
|
|
114
|
+
how the name arrived:
|
|
115
|
+
|
|
116
|
+
| What arrived | With a subject | With no subject |
|
|
117
|
+
| --- | --- | --- |
|
|
118
|
+
| `body.workKey` | Derived `run1_` id | **400** — fail-closed (the field is new; nobody loses anything) |
|
|
119
|
+
| `Idempotency-Key` | Derived `run1_` id | Stays a raw runId, as it has since FAZ-1 |
|
|
120
|
+
| `body.runId` | Raw id | Raw id |
|
|
121
|
+
|
|
122
|
+
The header is the forgiving one on purpose: it is usually stamped by a gateway, and turning a working
|
|
123
|
+
deployment's 200 into a 400 is not a fix. An `'org'` workScope is addressed by the organization, so
|
|
124
|
+
org-scoped work runs **without** a subject — that is the nightly-reconciliation case, not a hole. Pass
|
|
125
|
+
`orgId` from `identity` for it, and for parity with `@gnldev/server`: without it, org-scoped work
|
|
126
|
+
derives a *different* id here than it does through REST, which duplicates silently rather than
|
|
127
|
+
failing.
|
|
128
|
+
|
|
129
|
+
### Refusals
|
|
130
|
+
|
|
131
|
+
Errors thrown before the stream exists are typed, with the same codes `@gnldev/server` uses — a client
|
|
132
|
+
matches on `code`, never on the sentence.
|
|
133
|
+
|
|
134
|
+
**A conflict — 409, no `resumable`.** Something about the REQUEST has to change; no retry clears it.
|
|
135
|
+
All eight are `CALLER_CONFLICT_CODES` in `@gnldev/durable`, and this route returns every one of them:
|
|
136
|
+
|
|
137
|
+
| Code | Means |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| `run_thread_mismatch` | This id already belongs to a different conversation. |
|
|
140
|
+
| `run_input_mismatch` | Same derived id, different content — inside `run1_` this is unconditional. |
|
|
141
|
+
| `run_owner_mismatch` | The run belongs to a different subject. |
|
|
142
|
+
| `run_actor_mismatch` | Started by one actor, re-used by another — the first is frozen into the input, first-wins. |
|
|
143
|
+
| `thread_owner_mismatch` | The `resourceId` and the `threadId` belong to different people; nothing was appended. |
|
|
144
|
+
| `not_an_agent_run` | That id belongs to a workflow, a network or a batch item — `detail.kind` says which. |
|
|
145
|
+
| `run_swept` | Retention deleted the record; a `${runId}:swept` tombstone says it existed. |
|
|
146
|
+
| `batch_plan_mismatch` | A `batchId` carries one plan, and these items are not it. |
|
|
147
|
+
|
|
148
|
+
**Blocked — the request is right, the moment is not.** The three 409s carry `resumable: true`:
|
|
149
|
+
|
|
150
|
+
| Code | Status | `resumable` | Means |
|
|
151
|
+
| --- | --- | --- | --- |
|
|
152
|
+
| `run_busy` | 409 | yes | The same run is executing right now. The **only** code that also sends `Retry-After: 5`. |
|
|
153
|
+
| `side_effect_retry_blocked` | 409 | yes | A side effect's outcome is unknown; the engine refuses to guess. |
|
|
154
|
+
| `step_retry_blocked` | 409 | yes | A workflow step's side-effect claim was refused for the same reason. |
|
|
155
|
+
| `retry_limit_exceeded` | 422 | **no** | Retries are spent. 422 and no flag, because waiting is exactly what will not help. |
|
|
156
|
+
|
|
157
|
+
`resumable` answers one question — can waiting help? — which is why the conflicts never carry it and
|
|
158
|
+
why `retry_limit_exceeded` does not either, despite arriving through the same code path. Full pages
|
|
159
|
+
for each are in [`docs/errors`](../../docs/errors).
|
|
160
|
+
|
|
161
|
+
## Mapping table (GNL SSE → AG-UI)
|
|
162
|
+
| GNL event | AG-UI event(s) | Note |
|
|
163
|
+
|---|---|---|
|
|
164
|
+
| `text-delta` (first) | `TEXT_MESSAGE_START` + `TEXT_MESSAGE_CONTENT` | GNL has no separate "text started" event — synthesized on the first delta |
|
|
165
|
+
| `text-delta` (subsequent) | `TEXT_MESSAGE_CONTENT` | |
|
|
166
|
+
| `tool-call` | `TOOL_CALL_START` + `TOOL_CALL_ARGS` + `TOOL_CALL_END` | Arguments in a SINGLE delta (not streaming in GNL, arrives COMPLETE) |
|
|
167
|
+
| `tool-result` | `TOOL_CALL_RESULT` | `messageId = ${toolCallId}:result` |
|
|
168
|
+
| `error` | `RUN_ERROR` | `code` is carried; `detail`, if present, goes into `rawEvent` |
|
|
169
|
+
| `interrupt` (HITL/suspend) | `CUSTOM {name:'gnl.interrupt', value:{interrupts}}` | **UNCERTAIN mapping** — we're not sure whether AG-UI core has a dedicated event type for HITL; moved to the spec's `CUSTOM` escape hatch |
|
|
170
|
+
| `done` | `RUN_FINISHED` | `result: {finishReason, usage}` — we're not sure whether this field exists in the official spec (best-effort) |
|
|
171
|
+
|
|
172
|
+
If `tool-call`/`tool-result`/`interrupt`/`error`/`done` arrives while a text message is open,
|
|
173
|
+
`TEXT_MESSAGE_END` is written first (same state machine).
|
|
174
|
+
|
|
175
|
+
## How it works
|
|
176
|
+
`pipeAguiStream` keeps a small, independent copy of the fullStream-reading loop from `pipeAgentStream` in
|
|
177
|
+
`@gnldev/server/sse.ts` — it converts to the GNL `{event,data}` shape and
|
|
178
|
+
passes it to `toAguiEvents`. AG-UI SSE frames carry only a `data:` field (the type is inside the JSON) — the
|
|
179
|
+
`event:` field is not used.
|
|
180
|
+
|
|
181
|
+
## Known limits
|
|
182
|
+
- This adapter has **NO resumable stream (Last-Event-ID)** — `@gnldev/server`'s resumable-id contract is
|
|
183
|
+
not carried here, so a dropped connection restarts the turn rather than resuming it.
|
|
184
|
+
- `createAguiRoute` does not include auth/tenancy/budget (see the API note above).
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
Apache-2.0 — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type AguiEvent } from './types.js';
|
|
2
|
+
/** The SSE frame shape written by @gnldev/server's pipeAgentStream (event name + JSON data). */
|
|
3
|
+
export interface GnlSseEvent {
|
|
4
|
+
event: 'text-delta' | 'tool-call' | 'tool-result' | 'error' | 'interrupt' | 'done' | string;
|
|
5
|
+
data: any;
|
|
6
|
+
}
|
|
7
|
+
export interface AguiConvertContext {
|
|
8
|
+
threadId: string;
|
|
9
|
+
runId: string;
|
|
10
|
+
}
|
|
11
|
+
export interface AguiConvertState {
|
|
12
|
+
/** Whether a text message is currently open (START written, END not yet written). */
|
|
13
|
+
textOpen: boolean;
|
|
14
|
+
/** The id of the open text message (only defined while textOpen). */
|
|
15
|
+
textMessageId?: string;
|
|
16
|
+
/** The sequence number to assign to the next NEW text message (deterministic id: `${runId}:text:${seq}`). */
|
|
17
|
+
textSeq: number;
|
|
18
|
+
/**
|
|
19
|
+
* P0.1: toolCallIds whose START/ARGS were already streamed incrementally via tool-input-* events —
|
|
20
|
+
* when the complete `tool-call` event later arrives for the same id, it must NOT re-frame
|
|
21
|
+
* START/ARGS/END a second time (the UI would show a duplicate call). Optional so existing
|
|
22
|
+
* hand-constructed states keep working (absent = empty).
|
|
23
|
+
*/
|
|
24
|
+
streamedToolIds?: string[];
|
|
25
|
+
}
|
|
26
|
+
export declare const initialAguiConvertState: AguiConvertState;
|
|
27
|
+
export interface AguiConvertResult {
|
|
28
|
+
events: AguiEvent[];
|
|
29
|
+
state: AguiConvertState;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Converts a single GNL SSE event into an AG-UI event sequence.
|
|
33
|
+
* If `state` is not given, it starts fresh (`initialAguiConvertState`) — a single state object must
|
|
34
|
+
* be threaded from start to end for the ENTIRE run (see pipeAguiStream / usage in tests).
|
|
35
|
+
*/
|
|
36
|
+
export declare function toAguiEvents(gnlEvent: GnlSseEvent, ctx: AguiConvertContext, state?: AguiConvertState): AguiConvertResult;
|
package/dist/convert.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// PURE (side-effect-free) converter: converts a single event from the @gnldev/server SSE contract
|
|
2
|
+
// ({event, data} — text-delta/tool-call/tool-result/error/interrupt/done, see the header comment in
|
|
3
|
+
// packages/server/src/sse.ts) into an AG-UI event sequence. State is passed in/returned from OUTSIDE
|
|
4
|
+
// (no closed-over mutable state) → the same (gnlEvent, ctx, state) input ALWAYS produces the same
|
|
5
|
+
// output — compatible with deterministic replay.
|
|
6
|
+
//
|
|
7
|
+
// TEXT_MESSAGE_START/END framing of text-deltas is tracked here via state: GNL SSE has NO separate
|
|
8
|
+
// "text started/ended" event (only consecutive text-deltas) → START is synthesized on the first delta,
|
|
9
|
+
// END is closed when the next non-text event arrives (or on done/error).
|
|
10
|
+
import { EventType } from './types.js';
|
|
11
|
+
export const initialAguiConvertState = { textOpen: false, textSeq: 0 };
|
|
12
|
+
/** If a text message is open, close it with TEXT_MESSAGE_END (before tool-call/tool-result/interrupt/error/done). */
|
|
13
|
+
function closeText(state) {
|
|
14
|
+
if (!state.textOpen || !state.textMessageId)
|
|
15
|
+
return { events: [], state };
|
|
16
|
+
return {
|
|
17
|
+
events: [{ type: EventType.TEXT_MESSAGE_END, messageId: state.textMessageId }],
|
|
18
|
+
state: { ...state, textOpen: false, textMessageId: undefined },
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Converts a single GNL SSE event into an AG-UI event sequence.
|
|
23
|
+
* If `state` is not given, it starts fresh (`initialAguiConvertState`) — a single state object must
|
|
24
|
+
* be threaded from start to end for the ENTIRE run (see pipeAguiStream / usage in tests).
|
|
25
|
+
*/
|
|
26
|
+
export function toAguiEvents(gnlEvent, ctx, state = initialAguiConvertState) {
|
|
27
|
+
switch (gnlEvent.event) {
|
|
28
|
+
case 'text-delta': {
|
|
29
|
+
const text = gnlEvent.data?.text ?? '';
|
|
30
|
+
if (state.textOpen && state.textMessageId) {
|
|
31
|
+
return {
|
|
32
|
+
events: [{ type: EventType.TEXT_MESSAGE_CONTENT, messageId: state.textMessageId, delta: text }],
|
|
33
|
+
state,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const messageId = `${ctx.runId}:text:${state.textSeq}`;
|
|
37
|
+
return {
|
|
38
|
+
events: [
|
|
39
|
+
{ type: EventType.TEXT_MESSAGE_START, messageId, role: 'assistant' },
|
|
40
|
+
{ type: EventType.TEXT_MESSAGE_CONTENT, messageId, delta: text },
|
|
41
|
+
],
|
|
42
|
+
state: { textOpen: true, textMessageId: messageId, textSeq: state.textSeq + 1 },
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
// P0.1: REAL incremental args streaming (the spec's native TOOL_CALL_START→ARGS…→END framing) —
|
|
46
|
+
// the "single delta" simplification below now only applies when no tool-input-* events preceded.
|
|
47
|
+
case 'tool-input-start': {
|
|
48
|
+
const closed = closeText(state);
|
|
49
|
+
const { toolCallId, toolName } = gnlEvent.data ?? {};
|
|
50
|
+
return {
|
|
51
|
+
events: [...closed.events, { type: EventType.TOOL_CALL_START, toolCallId, toolCallName: toolName }],
|
|
52
|
+
state: { ...closed.state, streamedToolIds: [...(closed.state.streamedToolIds ?? []), toolCallId] },
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
case 'tool-input-delta': {
|
|
56
|
+
const { toolCallId, delta } = gnlEvent.data ?? {};
|
|
57
|
+
return { events: [{ type: EventType.TOOL_CALL_ARGS, toolCallId, delta: String(delta ?? '') }], state };
|
|
58
|
+
}
|
|
59
|
+
case 'tool-input-end': {
|
|
60
|
+
const { toolCallId } = gnlEvent.data ?? {};
|
|
61
|
+
return { events: [{ type: EventType.TOOL_CALL_END, toolCallId }], state };
|
|
62
|
+
}
|
|
63
|
+
case 'tool-call': {
|
|
64
|
+
const closed = closeText(state);
|
|
65
|
+
const { toolCallId, toolName, input } = gnlEvent.data ?? {};
|
|
66
|
+
// Already framed incrementally via tool-input-* → a second START/ARGS/END would duplicate the call in the UI.
|
|
67
|
+
if (closed.state.streamedToolIds?.includes(toolCallId))
|
|
68
|
+
return { events: closed.events, state: closed.state };
|
|
69
|
+
return {
|
|
70
|
+
events: [
|
|
71
|
+
...closed.events,
|
|
72
|
+
{ type: EventType.TOOL_CALL_START, toolCallId, toolCallName: toolName },
|
|
73
|
+
// Deliberate simplification (only on the non-streamed path): GNL's tool-call event gives
|
|
74
|
+
// arguments COMPLETE, so they travel here in a SINGLE delta.
|
|
75
|
+
{ type: EventType.TOOL_CALL_ARGS, toolCallId, delta: JSON.stringify(input ?? {}) },
|
|
76
|
+
{ type: EventType.TOOL_CALL_END, toolCallId },
|
|
77
|
+
],
|
|
78
|
+
state: closed.state,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
case 'tool-result': {
|
|
82
|
+
const closed = closeText(state);
|
|
83
|
+
const { toolCallId, output } = gnlEvent.data ?? {};
|
|
84
|
+
return {
|
|
85
|
+
events: [
|
|
86
|
+
...closed.events,
|
|
87
|
+
{
|
|
88
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
89
|
+
messageId: `${toolCallId}:result`,
|
|
90
|
+
toolCallId,
|
|
91
|
+
content: JSON.stringify(output ?? null),
|
|
92
|
+
role: 'tool',
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
state: closed.state,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
case 'interrupt': {
|
|
99
|
+
// UNCERTAIN: we're not sure whether the AG-UI core subset has a dedicated event type for
|
|
100
|
+
// HITL/suspend (we didn't make one up) → carried into the spec's CUSTOM escape hatch under the name `gnl.interrupt`.
|
|
101
|
+
const closed = closeText(state);
|
|
102
|
+
return {
|
|
103
|
+
events: [...closed.events, { type: EventType.CUSTOM, name: 'gnl.interrupt', value: gnlEvent.data }],
|
|
104
|
+
state: closed.state,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
case 'error': {
|
|
108
|
+
const closed = closeText(state);
|
|
109
|
+
const { error, code, detail } = gnlEvent.data ?? {};
|
|
110
|
+
const evt = { type: EventType.RUN_ERROR, message: String(error ?? 'unknown error') };
|
|
111
|
+
if (code)
|
|
112
|
+
evt.code = code;
|
|
113
|
+
if (detail !== undefined)
|
|
114
|
+
evt.rawEvent = detail;
|
|
115
|
+
return { events: [...closed.events, evt], state: closed.state };
|
|
116
|
+
}
|
|
117
|
+
case 'done': {
|
|
118
|
+
const closed = closeText(state);
|
|
119
|
+
const { finishReason, usage } = gnlEvent.data ?? {};
|
|
120
|
+
const evt = { type: EventType.RUN_FINISHED, threadId: ctx.threadId, runId: ctx.runId };
|
|
121
|
+
if (finishReason !== undefined || usage !== undefined)
|
|
122
|
+
evt.result = { finishReason, usage };
|
|
123
|
+
return { events: [...closed.events, evt], state: closed.state };
|
|
124
|
+
}
|
|
125
|
+
default:
|
|
126
|
+
// P0.1: NO silent drop — every GNL event without a native AG-UI mapping (reasoning-*/source/
|
|
127
|
+
// file/step-*/tool-error/raw and anything future) travels through the spec's CUSTOM escape hatch
|
|
128
|
+
// as `gnl.<event>` (the same pattern `gnl.interrupt` above already established). Deliberately
|
|
129
|
+
// does NOT close an open text frame: these are out-of-band annotations, not message boundaries.
|
|
130
|
+
return { events: [{ type: EventType.CUSTOM, name: `gnl.${gnlEvent.event}`, value: gnlEvent.data }], state };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=convert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convert.js","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,oGAAoG;AACpG,qGAAqG;AACrG,kGAAkG;AAClG,iDAAiD;AACjD,EAAE;AACF,mGAAmG;AACnG,uGAAuG;AACvG,yEAAyE;AACzE,OAAO,EAAE,SAAS,EAA6D,MAAM,YAAY,CAAC;AA6BlG,MAAM,CAAC,MAAM,uBAAuB,GAAqB,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;AAOzF,qHAAqH;AACrH,SAAS,SAAS,CAAC,KAAuB;IACxC,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,KAAK,CAAC,aAAa;QAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;IAC1E,OAAO;QACL,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,gBAAgB,EAAE,SAAS,EAAE,KAAK,CAAC,aAAa,EAAE,CAAC;QAC9E,KAAK,EAAE,EAAE,GAAG,KAAK,EAAE,QAAQ,E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CAAC;QACD;YACE,6FAA6F;YAC7F,iGAAiG;YACjG,8FAA8F;YAC9F,gGAAgG;YAChG,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,QAAQ,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC;IAChH,CAAC;AACH,CAAC","sourcesContent":["// PURE (side-effect-free) converter: converts a single event from the @gnldev/server SSE contract\n// ({event, data} — text-delta/tool-call/tool-result/error/interrupt/done, see the header comment in\n// packages/server/src/sse.ts) into an AG-UI event sequence. State is passed in/returned from OUTSIDE\n// (no closed-over mutable state) → the same (gnlEvent, ctx, state) input ALWAYS produces the same\n// output — compatible with deterministic replay.\n//\n// TEXT_MESSAGE_START/END framing of text-deltas is tracked here via state: GNL SSE has NO separate\n// \"text started/ended\" event (only consecutive text-deltas) → START is synthesized on the first delta,\n// END is closed when the next non-text event arrives (or on done/error).\nimport { EventType, type AguiEvent, type RunErrorEvent, type RunFinishedEvent } from './types.js';\n\n/** The SSE frame shape written by @gnldev/server's pipeAgentStream (event name + JSON data). */\nexport interface GnlSseEvent {\n event: 'text-delta' | 'tool-call' | 'tool-result' | 'error' | 'interrupt' | 'done' | string;\n data: any;\n}\n\nexport interface AguiConvertContext {\n threadId: string;\n runId: string;\n}\n\nexport interface AguiConvertState {\n /** Whether a text message is currently open (START written, END not yet written). */\n textOpen: boolean;\n /** The id of the open text message (only defined while textOpen). */\n textMessageId?: string;\n /** The sequence number to assign to the next NEW text message (deterministic id: `${runId}:text:${seq}`). */\n textSeq: number;\n /**\n * P0.1: toolCallIds whose START/ARGS were already streamed incrementally via tool-input-* events —\n * when the complete `tool-call` event later arrives for the same id, it must NOT re-frame\n * START/ARGS/END a second time (the UI would show a duplicate call). Optional so existing\n * hand-constructed states keep working (absent = empty).\n */\n streamedToolIds?: string[];\n}\n\nexport const initialAguiConvertState: AguiConvertState = { textOpen: false, textSeq: 0 };\n\nexport interface AguiConvertResult {\n events: AguiEvent[];\n state: AguiConvertState;\n}\n\n/** If a text message is open, close it with TEXT_MESSAGE_END (before tool-call/tool-result/interrupt/error/done). */\nfunction closeText(state: AguiConvertState): AguiConvertResult {\n if (!state.textOpen || !state.textMessageId) return { events: [], state };\n return {\n events: [{ type: EventType.TEXT_MESSAGE_END, messageId: state.textMessageId }],\n state: { ...state, textOpen: false, textMessageId: undefined },\n };\n}\n\n/**\n * Converts a single GNL SSE event into an AG-UI event sequence.\n * If `state` is not given, it starts fresh (`initialAguiConvertState`) — a single state object must\n * be threaded from start to end for the ENTIRE run (see pipeAguiStream / usage in tests).\n */\nexport function toAguiEvents(\n gnlEvent: GnlSseEvent,\n ctx: AguiConvertContext,\n state: AguiConvertState = initialAguiConvertState,\n): AguiConvertResult {\n switch (gnlEvent.event) {\n case 'text-delta': {\n const text: string = gnlEvent.data?.text ?? '';\n if (state.textOpen && state.textMessageId) {\n return {\n events: [{ type: EventType.TEXT_MESSAGE_CONTENT, messageId: state.textMessageId, delta: text }],\n state,\n };\n }\n const messageId = `${ctx.runId}:text:${state.textSeq}`;\n return {\n events: [\n { type: EventType.TEXT_MESSAGE_START, messageId, role: 'assistant' },\n { type: EventType.TEXT_MESSAGE_CONTENT, messageId, delta: text },\n ],\n state: { textOpen: true, textMessageId: messageId, textSeq: state.textSeq + 1 },\n };\n }\n // P0.1: REAL incremental args streaming (the spec's native TOOL_CALL_START→ARGS…→END framing) —\n // the \"single delta\" simplification below now only applies when no tool-input-* events preceded.\n case 'tool-input-start': {\n const closed = closeText(state);\n const { toolCallId, toolName } = gnlEvent.data ?? {};\n return {\n events: [...closed.events, { type: EventType.TOOL_CALL_START, toolCallId, toolCallName: toolName }],\n state: { ...closed.state, streamedToolIds: [...(closed.state.streamedToolIds ?? []), toolCallId] },\n };\n }\n case 'tool-input-delta': {\n const { toolCallId, delta } = gnlEvent.data ?? {};\n return { events: [{ type: EventType.TOOL_CALL_ARGS, toolCallId, delta: String(delta ?? '') }], state };\n }\n case 'tool-input-end': {\n const { toolCallId } = gnlEvent.data ?? {};\n return { events: [{ type: EventType.TOOL_CALL_END, toolCallId }], state };\n }\n case 'tool-call': {\n const closed = closeText(state);\n const { toolCallId, toolName, input } = gnlEvent.data ?? {};\n // Already framed incrementally via tool-input-* → a second START/ARGS/END would duplicate the call in the UI.\n if (closed.state.streamedToolIds?.includes(toolCallId)) return { events: closed.events, state: closed.state };\n return {\n events: [\n ...closed.events,\n { type: EventType.TOOL_CALL_START, toolCallId, toolCallName: toolName },\n // Deliberate simplification (only on the non-streamed path): GNL's tool-call event gives\n // arguments COMPLETE, so they travel here in a SINGLE delta.\n { type: EventType.TOOL_CALL_ARGS, toolCallId, delta: JSON.stringify(input ?? {}) },\n { type: EventType.TOOL_CALL_END, toolCallId },\n ],\n state: closed.state,\n };\n }\n case 'tool-result': {\n const closed = closeText(state);\n const { toolCallId, output } = gnlEvent.data ?? {};\n return {\n events: [\n ...closed.events,\n {\n type: EventType.TOOL_CALL_RESULT,\n messageId: `${toolCallId}:result`,\n toolCallId,\n content: JSON.stringify(output ?? null),\n role: 'tool',\n },\n ],\n state: closed.state,\n };\n }\n case 'interrupt': {\n // UNCERTAIN: we're not sure whether the AG-UI core subset has a dedicated event type for\n // HITL/suspend (we didn't make one up) → carried into the spec's CUSTOM escape hatch under the name `gnl.interrupt`.\n const closed = closeText(state);\n return {\n events: [...closed.events, { type: EventType.CUSTOM, name: 'gnl.interrupt', value: gnlEvent.data }],\n state: closed.state,\n };\n }\n case 'error': {\n const closed = closeText(state);\n const { error, code, detail } = gnlEvent.data ?? {};\n const evt: RunErrorEvent = { type: EventType.RUN_ERROR, message: String(error ?? 'unknown error') };\n if (code) evt.code = code;\n if (detail !== undefined) evt.rawEvent = detail;\n return { events: [...closed.events, evt], state: closed.state };\n }\n case 'done': {\n const closed = closeText(state);\n const { finishReason, usage } = gnlEvent.data ?? {};\n const evt: RunFinishedEvent = { type: EventType.RUN_FINISHED, threadId: ctx.threadId, runId: ctx.runId };\n if (finishReason !== undefined || usage !== undefined) evt.result = { finishReason, usage };\n return { events: [...closed.events, evt], state: closed.state };\n }\n default:\n // P0.1: NO silent drop — every GNL event without a native AG-UI mapping (reasoning-*/source/\n // file/step-*/tool-error/raw and anything future) travels through the spec's CUSTOM escape hatch\n // as `gnl.<event>` (the same pattern `gnl.interrupt` above already established). Deliberately\n // does NOT close an open text frame: these are out-of-band annotations, not message boundaries.\n return { events: [{ type: EventType.CUSTOM, name: `gnl.${gnlEvent.event}`, value: gnlEvent.data }], state };\n }\n}\n"]}
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,cAAc,CAAC,GAAS;IACtC,MAAM,IAAI,GAAG,CAAC,OAAgB,EAAE,GAAG,IAAe,EAAE,EAAE,CAAE,GAAG,CAAC,KAAa,CAAC,OAAO,EAAE,GAAG,IAAI,CAAsB,CAAC;IACjH,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9C,CAAC","sourcesContent":["// What this package hands a host: a request in, a response out — not a Hono instance.\n// See @gnldev/studio/src/handler.ts for the reasoning; the shape is deliberately identical, and\n// structurally compatible, without either package depending on the other.\nimport type { Hono } from 'hono';\n\nexport type FetchHandler = ((request: Request, ...rest: unknown[]) => Promise<Response>) & {\n fetch: (request: Request, ...rest: unknown[]) => Promise<Response>;\n};\n\nexport function toFetchHandler(app: Hono): FetchHandler {\n const call = (request: Request, ...rest: unknown[]) => (app.fetch as any)(request, ...rest) as Promise<Response>;\n return Object.assign(call, { fetch: call });\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { EventType } from './types.js';
|
|
2
|
+
export type { BaseAguiEvent, AguiEvent, RunStartedEvent, RunFinishedEvent, RunErrorEvent, TextMessageStartEvent, TextMessageContentEvent, TextMessageEndEvent, ToolCallStartEvent, ToolCallArgsEvent, ToolCallEndEvent, ToolCallResultEvent, CustomEvent, } from './types.js';
|
|
3
|
+
export { toAguiEvents, initialAguiConvertState } from './convert.js';
|
|
4
|
+
export type { GnlSseEvent, AguiConvertContext, AguiConvertState, AguiConvertResult } from './convert.js';
|
|
5
|
+
export { pipeAguiStream, createAguiRoute } from './route.js';
|
|
6
|
+
export type { PipeAguiStreamOptions, CreateAguiRouteOptions } from './route.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// @gnldev/agui — public export surface. AG-UI (CopilotKit) protocol adapter: zero @ag-ui/* dependency,
|
|
2
|
+
// event types hand-defined (see types.ts header note).
|
|
3
|
+
export { EventType } from './types.js';
|
|
4
|
+
export { toAguiEvents, initialAguiConvertState } from './convert.js';
|
|
5
|
+
export { pipeAguiStream, createAguiRoute } from './route.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uGAAuG;AACvG,uDAAuD;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAiBvC,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAGrE,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC","sourcesContent":["// @gnldev/agui — public export surface. AG-UI (CopilotKit) protocol adapter: zero @ag-ui/* dependency,\n// event types hand-defined (see types.ts header note).\nexport { EventType } from './types.js';\nexport type {\n BaseAguiEvent,\n AguiEvent,\n RunStartedEvent,\n RunFinishedEvent,\n RunErrorEvent,\n TextMessageStartEvent,\n TextMessageContentEvent,\n TextMessageEndEvent,\n ToolCallStartEvent,\n ToolCallArgsEvent,\n ToolCallEndEvent,\n ToolCallResultEvent,\n CustomEvent,\n} from './types.js';\n\nexport { toAguiEvents, initialAguiConvertState } from './convert.js';\nexport type { GnlSseEvent, AguiConvertContext, AguiConvertState, AguiConvertResult } from './convert.js';\n\nexport { pipeAguiStream, createAguiRoute } from './route.js';\nexport type { PipeAguiStreamOptions, CreateAguiRouteOptions } from './route.js';\n"]}
|