@mjczone/realtime-agui-client 0.0.0-stage → 0.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MJCZone, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,47 @@
1
+ import { AbstractAgent, type AgentConfig, type BaseEvent, type RunAgentInput } from '@ag-ui/client';
2
+ import { Observable } from 'rxjs';
3
+ import type { RealtimeClient } from '@mjczone/realtime-client';
4
+ /** What the run endpoint returns with 202, and what the agent stores to reattach. */
5
+ export interface RunRef {
6
+ runId: string;
7
+ threadId: string;
8
+ channel: string;
9
+ /** Stream position of the run's RUN_STARTED. */
10
+ offset: number;
11
+ epoch: string;
12
+ /** Subscription token for the channel, when the endpoint returns one. */
13
+ token?: string;
14
+ }
15
+ export interface CentrifugoAgentConfig extends AgentConfig {
16
+ client: RealtimeClient;
17
+ /** The agent service's run endpoint. It takes RunAgentInput and returns 202 with a RunRef. */
18
+ runUrl: string;
19
+ /** Extra headers for run and cancel requests, such as auth. */
20
+ headers?: () => Record<string, string> | Promise<Record<string, string>>;
21
+ /** Cancel endpoint for a run. When set, abortRun() POSTs to it. */
22
+ cancelUrl?: (run: RunRef) => string;
23
+ /** Where the active run is kept so a reload can reattach. Default sessionStorage when available; null turns it off. */
24
+ storage?: Pick<Storage, 'getItem' | 'setItem' | 'removeItem'> | null;
25
+ fetch?: typeof fetch;
26
+ }
27
+ /**
28
+ * An AG-UI agent whose events arrive over a realtime backbone subscription instead of an SSE response.
29
+ * runAgent() starts a run over HTTP; connectAgent() reattaches to the active run after a reload.
30
+ */
31
+ export declare class CentrifugoAgent extends AbstractAgent {
32
+ private readonly config;
33
+ private readonly storage;
34
+ private activeRun;
35
+ private attachTarget;
36
+ constructor(config: CentrifugoAgentConfig);
37
+ /** The run this agent would reattach to, if any. */
38
+ get storedRun(): RunRef | null;
39
+ /** Reattach to a specific run, such as one from a notice.run_started message. Then call connectAgent(). */
40
+ attach(run: RunRef): this;
41
+ run(input: RunAgentInput): Observable<BaseEvent>;
42
+ protected connect(): Observable<BaseEvent>;
43
+ abortRun(): void;
44
+ private stream;
45
+ private storageKey;
46
+ private save;
47
+ }
package/dist/agent.js ADDED
@@ -0,0 +1,146 @@
1
+ import { AbstractAgent, EventType } from '@ag-ui/client';
2
+ import { Observable } from 'rxjs';
3
+ const END_EVENTS = new Set([EventType.RUN_FINISHED, EventType.RUN_ERROR]);
4
+ function defaultStorage() {
5
+ try {
6
+ return typeof sessionStorage === 'undefined' ? null : sessionStorage;
7
+ }
8
+ catch {
9
+ return null;
10
+ }
11
+ }
12
+ /**
13
+ * An AG-UI agent whose events arrive over a realtime backbone subscription instead of an SSE response.
14
+ * runAgent() starts a run over HTTP; connectAgent() reattaches to the active run after a reload.
15
+ */
16
+ export class CentrifugoAgent extends AbstractAgent {
17
+ config;
18
+ storage;
19
+ activeRun = null;
20
+ attachTarget = null;
21
+ constructor(config) {
22
+ super(config);
23
+ this.config = config;
24
+ this.storage = config.storage === undefined ? defaultStorage() : config.storage;
25
+ }
26
+ /** The run this agent would reattach to, if any. */
27
+ get storedRun() {
28
+ try {
29
+ const raw = this.storage?.getItem(this.storageKey());
30
+ return raw ? JSON.parse(raw) : null;
31
+ }
32
+ catch {
33
+ return null;
34
+ }
35
+ }
36
+ /** Reattach to a specific run, such as one from a notice.run_started message. Then call connectAgent(). */
37
+ attach(run) {
38
+ this.attachTarget = run;
39
+ return this;
40
+ }
41
+ run(input) {
42
+ return new Observable((subscriber) => {
43
+ const abort = new AbortController();
44
+ let inner;
45
+ (async () => {
46
+ const res = await (this.config.fetch ?? fetch)(this.config.runUrl, {
47
+ method: 'POST',
48
+ headers: { 'Content-Type': 'application/json', ...(await this.config.headers?.()) },
49
+ body: JSON.stringify(input),
50
+ signal: abort.signal,
51
+ });
52
+ if (!res.ok)
53
+ throw new Error(`Run request failed: HTTP ${res.status}`);
54
+ const run = (await res.json());
55
+ if (abort.signal.aborted)
56
+ return;
57
+ inner = this.stream(run).subscribe(subscriber);
58
+ })().catch((e) => {
59
+ if (!abort.signal.aborted)
60
+ subscriber.error(e);
61
+ });
62
+ return () => {
63
+ abort.abort();
64
+ inner?.unsubscribe();
65
+ };
66
+ });
67
+ }
68
+ connect() {
69
+ const run = this.attachTarget ?? this.storedRun;
70
+ this.attachTarget = null;
71
+ if (!run)
72
+ return new Observable((s) => s.complete());
73
+ return this.stream({ ...run, token: undefined });
74
+ }
75
+ abortRun() {
76
+ const run = this.activeRun;
77
+ if (run && this.config.cancelUrl) {
78
+ void (async () => {
79
+ await (this.config.fetch ?? fetch)(this.config.cancelUrl(run), {
80
+ method: 'POST',
81
+ headers: { ...(await this.config.headers?.()) },
82
+ });
83
+ })().catch(() => { });
84
+ }
85
+ void this.detachActiveRun();
86
+ }
87
+ stream(run) {
88
+ return new Observable((subscriber) => {
89
+ this.activeRun = run;
90
+ this.save(run);
91
+ const finish = () => {
92
+ this.activeRun = null;
93
+ this.save(null);
94
+ };
95
+ const sub = this.config.client.subscribe(run.channel, (envelope) => {
96
+ if (!envelope.type.startsWith('agui.'))
97
+ return;
98
+ const event = envelope.data;
99
+ subscriber.next(event);
100
+ if (END_EVENTS.has(event.type)) {
101
+ // A run paused for the user (an interrupt) isn't done: keep it stored, so a reload replays it and the
102
+ // agent gets its pendingInterrupts back. The resume run replaces it.
103
+ if (event.outcome?.type === 'interrupt')
104
+ this.activeRun = null;
105
+ else
106
+ finish();
107
+ subscriber.complete();
108
+ }
109
+ }, {
110
+ since: { offset: run.offset - 1, epoch: run.epoch },
111
+ token: run.token,
112
+ onSubscribed: (info) => {
113
+ if (info.wasRecovering && !info.recovered) {
114
+ finish();
115
+ subscriber.next({
116
+ type: EventType.RUN_ERROR,
117
+ message: 'The run is no longer in the backbone history. Load the thread from the app.',
118
+ code: 'rtb.history_lost',
119
+ });
120
+ subscriber.complete();
121
+ }
122
+ },
123
+ onFailed: (err) => {
124
+ finish();
125
+ subscriber.error(new Error(`Subscription to ${run.channel} failed: ${err.code} ${err.message}`));
126
+ },
127
+ });
128
+ return () => sub.unsubscribe();
129
+ });
130
+ }
131
+ storageKey() {
132
+ return `rtb.agui.run.${this.agentId ?? 'agent'}.${this.threadId}`;
133
+ }
134
+ save(run) {
135
+ try {
136
+ if (run)
137
+ this.storage?.setItem(this.storageKey(), JSON.stringify({ ...run, token: undefined }));
138
+ else
139
+ this.storage?.removeItem(this.storageKey());
140
+ }
141
+ catch {
142
+ /* storage may be full or blocked */
143
+ }
144
+ }
145
+ }
146
+ //# sourceMappingURL=agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,SAAS,EAAwD,MAAM,eAAe,CAAC;AAC/G,OAAO,EAAE,UAAU,EAAE,MAAM,MA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AAC,CAAC,CAAC;gBACnG,CAAC;aACF,CACF,CAAC;YACF,OAAO,GAAG,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;QACjC,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,UAAU;QAChB,OAAO,gBAAgB,IAAI,CAAC,OAAO,IAAI,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;IACpE,CAAC;IAEO,IAAI,CAAC,GAAkB;QAC7B,IAAI,CAAC;YACH,IAAI,GAAG;gBAAE,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;;gBAC3F,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;QACnD,CAAC;QAAC,MAAM,CAAC;YACP,oCAAoC;QACtC,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1 @@
1
+ export * from './agent.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './agent.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@mjczone/realtime-agui-client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.0",
4
+ "description": "AG-UI agent whose transport is a realtime backbone subscription",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/mjczone/realtime-backbone.git",
9
+ "directory": "ts/agui-client"
10
+ },
11
+ "homepage": "https://github.com/mjczone/realtime-backbone#readme",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "sideEffects": false,
23
+ "dependencies": {
24
+ "@mjczone/realtime-client": "0.0.0",
25
+ "@mjczone/realtime-contract": "0.0.0"
26
+ },
27
+ "peerDependencies": {
28
+ "@ag-ui/client": "^1.0.2",
29
+ "rxjs": "^7.8.1"
30
+ },
31
+ "devDependencies": {
32
+ "@ag-ui/client": "1.0.2",
33
+ "rxjs": "7.8.1"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc -p tsconfig.json",
37
+ "test": "vitest run"
38
+ }
6
39
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.