@agenticgraph/stream-react 0.0.0-stage → 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 +21 -0
- package/README.md +39 -2
- package/dist/index.cjs +145 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +123 -0
- package/dist/index.js.map +1 -0
- package/package.json +73 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) shahzad <shahzadsarwar.work@gmail.com>
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agenticgraph/stream-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React hooks for [AgenticGraph](https://github.com/shahzad-io/agenticgraph) streaming: `useAgenticStream` and `useStreamSelector`, on top of [@agenticgraph/stream-client](https://www.npmjs.com/package/@agenticgraph/stream-client).
|
|
4
|
+
|
|
5
|
+
**Pre-1.0:** minor versions may contain breaking changes. Read the [CHANGELOG](https://github.com/shahzad-io/agenticgraph-stream-client/blob/main/CHANGELOG.md) before you upgrade.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @agenticgraph/stream-client @agenticgraph/stream-react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { SSETransport } from '@agenticgraph/stream-client';
|
|
13
|
+
import { useAgenticStream } from '@agenticgraph/stream-react';
|
|
14
|
+
|
|
15
|
+
function Chat({ streamId }: { streamId: string }) {
|
|
16
|
+
const { text, status, error } = useAgenticStream({
|
|
17
|
+
transport: new SSETransport({ url: `/agentic-graph/streams/${streamId}` }),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return <p>{error ? `Error: ${error.kind}` : text} ({status})</p>;
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`text` holds every `text_delta` of the run (every step); `finalText` the current or last step, so after a tool run the final answer.
|
|
25
|
+
|
|
26
|
+
Requires React 18+. Guide: [agenticgraph.dev/docs/stream-getting-started](https://agenticgraph.dev/docs/stream-getting-started); more in the [repository README](https://github.com/shahzad-io/agenticgraph-stream-client#readme).
|
|
27
|
+
|
|
28
|
+
## Compatibility
|
|
29
|
+
|
|
30
|
+
The three packages are released together:
|
|
31
|
+
|
|
32
|
+
| Package | Version | Works with |
|
|
33
|
+
|---|---|---|
|
|
34
|
+
| `agenticgraph/agenticgraph` (core) | 0.2.x | PHP 8.3+, Laravel 12.61.1+ or 13.12+, Prism 0.102 |
|
|
35
|
+
| `agenticgraph/vector` | 0.1.x | PHP 8.3+, Laravel 12.61.1+ or 13.12+, Prism 0.102, PostgreSQL with pgvector (0.8+ for iterative scan). Core 0.2.x is optional (tracing) |
|
|
36
|
+
| `@agenticgraph/stream-client`, `@agenticgraph/stream-react`, `@agenticgraph/stream-vue` | 0.1.x | Core 0.2.x on the server (stream wire contract v1.1), Node.js 18+, React 18+ or Vue 3.4+ for the adapters |
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
activeNodeSelector: () => import_stream_client2.activeNodeSelector,
|
|
24
|
+
nodeTextSelector: () => import_stream_client2.nodeTextSelector,
|
|
25
|
+
thinkingSelector: () => import_stream_client2.thinkingSelector,
|
|
26
|
+
toolCallSelector: () => import_stream_client2.toolCallSelector,
|
|
27
|
+
useAgenticStream: () => useAgenticStream,
|
|
28
|
+
useStreamSelector: () => useStreamSelector
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/useAgenticStream.ts
|
|
33
|
+
var import_stream_client = require("@agenticgraph/stream-client");
|
|
34
|
+
var import_react = require("react");
|
|
35
|
+
function useAgenticStream(options) {
|
|
36
|
+
const { transport, onResume } = options;
|
|
37
|
+
const [text, setText] = (0, import_react.useState)("");
|
|
38
|
+
const [finalText, setFinalText] = (0, import_react.useState)("");
|
|
39
|
+
const [status, setStatus] = (0, import_react.useState)("idle");
|
|
40
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
41
|
+
const [interrupt, setInterrupt] = (0, import_react.useState)(null);
|
|
42
|
+
const [lastEvent, setLastEvent] = (0, import_react.useState)(null);
|
|
43
|
+
const [client, setClient] = (0, import_react.useState)(null);
|
|
44
|
+
const onResumeRef = (0, import_react.useRef)(onResume);
|
|
45
|
+
onResumeRef.current = onResume;
|
|
46
|
+
(0, import_react.useEffect)(() => {
|
|
47
|
+
let stream = null;
|
|
48
|
+
let unsubEvents;
|
|
49
|
+
let unsubStatus;
|
|
50
|
+
let cancelled = false;
|
|
51
|
+
setText("");
|
|
52
|
+
setFinalText("");
|
|
53
|
+
setStatus("idle");
|
|
54
|
+
setError(null);
|
|
55
|
+
setInterrupt(null);
|
|
56
|
+
setLastEvent(null);
|
|
57
|
+
setClient(null);
|
|
58
|
+
void (async () => {
|
|
59
|
+
try {
|
|
60
|
+
stream = await import_stream_client.AgenticStream.connect({ transport });
|
|
61
|
+
} catch (failure) {
|
|
62
|
+
if (!cancelled) {
|
|
63
|
+
setStatus("error");
|
|
64
|
+
setError({ kind: "connection", message: failure instanceof Error ? failure.message : String(failure) });
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (cancelled) {
|
|
69
|
+
await stream.disconnect();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
setClient(stream);
|
|
73
|
+
setStatus(stream.status);
|
|
74
|
+
setError(stream.error);
|
|
75
|
+
setInterrupt(stream.interrupt);
|
|
76
|
+
unsubEvents = stream.on((event) => {
|
|
77
|
+
setLastEvent(event);
|
|
78
|
+
setText((current) => (0, import_stream_client.appendTextDelta)(current, event));
|
|
79
|
+
setFinalText((current) => (0, import_stream_client.appendFinalTextDelta)(current, event));
|
|
80
|
+
});
|
|
81
|
+
unsubStatus = stream.onStatusChange((nextStatus, snapshot) => {
|
|
82
|
+
setStatus(nextStatus);
|
|
83
|
+
setError(snapshot.error);
|
|
84
|
+
setInterrupt(snapshot.interrupt);
|
|
85
|
+
});
|
|
86
|
+
})();
|
|
87
|
+
return () => {
|
|
88
|
+
cancelled = true;
|
|
89
|
+
unsubEvents?.();
|
|
90
|
+
unsubStatus?.();
|
|
91
|
+
void stream?.disconnect();
|
|
92
|
+
};
|
|
93
|
+
}, [transport]);
|
|
94
|
+
const resume = (0, import_react.useCallback)(
|
|
95
|
+
async (decision) => {
|
|
96
|
+
if (client === null) {
|
|
97
|
+
throw new Error("useAgenticStream: stream not connected");
|
|
98
|
+
}
|
|
99
|
+
await (0, import_stream_client.executeResume)(client, decision, onResumeRef.current);
|
|
100
|
+
},
|
|
101
|
+
[client]
|
|
102
|
+
);
|
|
103
|
+
return {
|
|
104
|
+
text,
|
|
105
|
+
finalText,
|
|
106
|
+
status,
|
|
107
|
+
error,
|
|
108
|
+
interrupt,
|
|
109
|
+
resume,
|
|
110
|
+
lastEvent,
|
|
111
|
+
client
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/useStreamSelector.ts
|
|
116
|
+
var import_react2 = require("react");
|
|
117
|
+
function useStreamSelector(client, selector) {
|
|
118
|
+
const [events, setEvents] = (0, import_react2.useState)([]);
|
|
119
|
+
const selectorRef = (0, import_react2.useRef)(selector);
|
|
120
|
+
selectorRef.current = selector;
|
|
121
|
+
(0, import_react2.useEffect)(() => {
|
|
122
|
+
if (client === null) {
|
|
123
|
+
setEvents([]);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const unsub = client.on((event) => {
|
|
127
|
+
setEvents((current) => [...current, event]);
|
|
128
|
+
});
|
|
129
|
+
return unsub;
|
|
130
|
+
}, [client]);
|
|
131
|
+
return (0, import_react2.useMemo)(() => selectorRef.current(events), [events]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/index.ts
|
|
135
|
+
var import_stream_client2 = require("@agenticgraph/stream-client");
|
|
136
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
137
|
+
0 && (module.exports = {
|
|
138
|
+
activeNodeSelector,
|
|
139
|
+
nodeTextSelector,
|
|
140
|
+
thinkingSelector,
|
|
141
|
+
toolCallSelector,
|
|
142
|
+
useAgenticStream,
|
|
143
|
+
useStreamSelector
|
|
144
|
+
});
|
|
145
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/useAgenticStream.ts","../src/useStreamSelector.ts"],"sourcesContent":["export { useAgenticStream } from './useAgenticStream.js';\nexport type { UseAgenticStreamOptions, UseAgenticStreamReturn } from './useAgenticStream.js';\nexport { useStreamSelector } from './useStreamSelector.js';\nexport {\n activeNodeSelector,\n nodeTextSelector,\n thinkingSelector,\n toolCallSelector,\n} from '@agenticgraph/stream-client';\nexport type { StreamSelector } from '@agenticgraph/stream-client';\n","import {\n AgenticStream,\n executeResume,\n appendFinalTextDelta,\n appendTextDelta,\n type ResumeHandler,\n type StreamErrorDetail,\n type StreamEvent,\n type StreamInterrupt,\n type StreamStatus,\n type StreamTransport,\n} from '@agenticgraph/stream-client';\nimport { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseAgenticStreamOptions {\n transport: StreamTransport;\n onResume?: ResumeHandler;\n}\n\nexport interface UseAgenticStreamReturn {\n /** Every text_delta of the run, every step (the narration). */\n text: string;\n /** Text of the current or last step; after a tool run, the final answer (LlmResponse::text on the server). */\n finalText: string;\n status: StreamStatus;\n error: StreamErrorDetail | null;\n interrupt: StreamInterrupt | null;\n resume: (decision: unknown) => Promise<void>;\n lastEvent: StreamEvent | null;\n client: AgenticStream | null;\n}\n\n/**\n * React hook bridging {@link AgenticStream} into reactive state.\n *\n * Text accumulation uses functional `setText` updates (one state slot, O(1) per delta).\n * Status/error/interrupt sync from the core via `onStatusChange` - not reimplemented here.\n * `lastEvent` updates on every event (intentional; use selectors for derived state).\n */\nexport function useAgenticStream(options: UseAgenticStreamOptions): UseAgenticStreamReturn {\n const { transport, onResume } = options;\n\n const [text, setText] = useState('');\n const [finalText, setFinalText] = useState('');\n const [status, setStatus] = useState<StreamStatus>('idle');\n const [error, setError] = useState<StreamErrorDetail | null>(null);\n const [interrupt, setInterrupt] = useState<StreamInterrupt | null>(null);\n const [lastEvent, setLastEvent] = useState<StreamEvent | null>(null);\n const [client, setClient] = useState<AgenticStream | null>(null);\n\n const onResumeRef = useRef(onResume);\n onResumeRef.current = onResume;\n\n useEffect(() => {\n let stream: AgenticStream | null = null;\n let unsubEvents: (() => void) | undefined;\n let unsubStatus: (() => void) | undefined;\n let cancelled = false;\n\n setText('');\n setFinalText('');\n setStatus('idle');\n setError(null);\n setInterrupt(null);\n setLastEvent(null);\n setClient(null);\n\n void (async () => {\n try {\n stream = await AgenticStream.connect({ transport });\n } catch (failure) {\n if (!cancelled) {\n setStatus('error');\n setError({ kind: 'connection', message: failure instanceof Error ? failure.message : String(failure) });\n }\n\n return;\n }\n\n if (cancelled) {\n await stream.disconnect();\n\n return;\n }\n\n setClient(stream);\n setStatus(stream.status);\n setError(stream.error);\n setInterrupt(stream.interrupt);\n\n unsubEvents = stream.on((event) => {\n setLastEvent(event);\n setText((current) => appendTextDelta(current, event));\n setFinalText((current) => appendFinalTextDelta(current, event));\n });\n\n unsubStatus = stream.onStatusChange((nextStatus, snapshot) => {\n setStatus(nextStatus);\n setError(snapshot.error);\n setInterrupt(snapshot.interrupt);\n });\n })();\n\n return () => {\n cancelled = true;\n unsubEvents?.();\n unsubStatus?.();\n void stream?.disconnect();\n };\n }, [transport]);\n\n const resume = useCallback(\n async (decision: unknown) => {\n if (client === null) {\n throw new Error('useAgenticStream: stream not connected');\n }\n\n await executeResume(client, decision, onResumeRef.current);\n },\n [client],\n );\n\n return {\n text,\n finalText,\n status,\n error,\n interrupt,\n resume,\n lastEvent,\n client,\n };\n}\n","import type { AgenticStream, StreamEvent, StreamSelector } from '@agenticgraph/stream-client';\nimport { useEffect, useMemo, useRef, useState } from 'react';\n\n/**\n * Derive reactive state from the stream's event history via a selector function.\n * Selector functions should be stable (imported, not inline) to avoid redundant recomputation.\n */\nexport function useStreamSelector<T>(\n client: AgenticStream | null,\n selector: StreamSelector<T>,\n): T {\n const [events, setEvents] = useState<StreamEvent[]>([]);\n const selectorRef = useRef(selector);\n selectorRef.current = selector;\n\n useEffect(() => {\n if (client === null) {\n setEvents([]);\n\n return;\n }\n\n const unsub = client.on((event) => {\n setEvents((current) => [...current, event]);\n });\n\n return unsub;\n }, [client]);\n\n return useMemo(() => selectorRef.current(events), [events]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,2BAWO;AACP,mBAAyD;AA2BlD,SAAS,iBAAiB,SAA0D;AACzF,QAAM,EAAE,WAAW,SAAS,IAAI;AAEhC,QAAM,CAAC,MAAM,OAAO,QAAI,uBAAS,EAAE;AACnC,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAS,EAAE;AAC7C,QAAM,CAAC,QAAQ,SAAS,QAAI,uBAAuB,MAAM;AACzD,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAmC,IAAI;AACjE,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAiC,IAAI;AACvE,QAAM,CAAC,WAAW,YAAY,QAAI,uBAA6B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aAAsB;AAC3B,UAAI,WAAW,MAAM;AACnB,cAAM,IAAI,MAAM,wCAAwC;AAAA,MAC1D;AAEA,gBAAM,oCAAc,QAAQ,UAAU,YAAY,OAAO;AAAA,IAC3D;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;;ACnIA,IAAAA,gBAAqD;AAM9C,SAAS,kBACd,QACA,UACG;AACH,QAAM,CAAC,QAAQ,SAAS,QAAI,wBAAwB,CAAC,CAAC;AACtD,QAAM,kBAAc,sBAAO,QAAQ;AACnC,cAAY,UAAU;AAEtB,+BAAU,MAAM;AACd,QAAI,WAAW,MAAM;AACnB,gBAAU,CAAC,CAAC;AAEZ;AAAA,IACF;AAEA,UAAM,QAAQ,OAAO,GAAG,CAAC,UAAU;AACjC,gBAAU,CAAC,YAAY,CAAC,GAAG,SAAS,KAAK,CAAC;AAAA,IAC5C,CAAC;AAED,WAAO;AAAA,EACT,GAAG,CAAC,MAAM,CAAC;AAEX,aAAO,uBAAQ,MAAM,YAAY,QAAQ,MAAM,GAAG,CAAC,MAAM,CAAC;AAC5D;;;AF3BA,IAAAC,wBAKO;","names":["import_react","import_stream_client"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { StreamTransport, ResumeHandler, StreamStatus, StreamErrorDetail, StreamInterrupt, StreamEvent, AgenticStream, StreamSelector } from '@agenticgraph/stream-client';
|
|
2
|
+
export { StreamSelector, activeNodeSelector, nodeTextSelector, thinkingSelector, toolCallSelector } from '@agenticgraph/stream-client';
|
|
3
|
+
|
|
4
|
+
interface UseAgenticStreamOptions {
|
|
5
|
+
transport: StreamTransport;
|
|
6
|
+
onResume?: ResumeHandler;
|
|
7
|
+
}
|
|
8
|
+
interface UseAgenticStreamReturn {
|
|
9
|
+
/** Every text_delta of the run, every step (the narration). */
|
|
10
|
+
text: string;
|
|
11
|
+
/** Text of the current or last step; after a tool run, the final answer (LlmResponse::text on the server). */
|
|
12
|
+
finalText: string;
|
|
13
|
+
status: StreamStatus;
|
|
14
|
+
error: StreamErrorDetail | null;
|
|
15
|
+
interrupt: StreamInterrupt | null;
|
|
16
|
+
resume: (decision: unknown) => Promise<void>;
|
|
17
|
+
lastEvent: StreamEvent | null;
|
|
18
|
+
client: AgenticStream | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* React hook bridging {@link AgenticStream} into reactive state.
|
|
22
|
+
*
|
|
23
|
+
* Text accumulation uses functional `setText` updates (one state slot, O(1) per delta).
|
|
24
|
+
* Status/error/interrupt sync from the core via `onStatusChange` - not reimplemented here.
|
|
25
|
+
* `lastEvent` updates on every event (intentional; use selectors for derived state).
|
|
26
|
+
*/
|
|
27
|
+
declare function useAgenticStream(options: UseAgenticStreamOptions): UseAgenticStreamReturn;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Derive reactive state from the stream's event history via a selector function.
|
|
31
|
+
* Selector functions should be stable (imported, not inline) to avoid redundant recomputation.
|
|
32
|
+
*/
|
|
33
|
+
declare function useStreamSelector<T>(client: AgenticStream | null, selector: StreamSelector<T>): T;
|
|
34
|
+
|
|
35
|
+
export { type UseAgenticStreamOptions, type UseAgenticStreamReturn, useAgenticStream, useStreamSelector };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { StreamTransport, ResumeHandler, StreamStatus, StreamErrorDetail, StreamInterrupt, StreamEvent, AgenticStream, StreamSelector } from '@agenticgraph/stream-client';
|
|
2
|
+
export { StreamSelector, activeNodeSelector, nodeTextSelector, thinkingSelector, toolCallSelector } from '@agenticgraph/stream-client';
|
|
3
|
+
|
|
4
|
+
interface UseAgenticStreamOptions {
|
|
5
|
+
transport: StreamTransport;
|
|
6
|
+
onResume?: ResumeHandler;
|
|
7
|
+
}
|
|
8
|
+
interface UseAgenticStreamReturn {
|
|
9
|
+
/** Every text_delta of the run, every step (the narration). */
|
|
10
|
+
text: string;
|
|
11
|
+
/** Text of the current or last step; after a tool run, the final answer (LlmResponse::text on the server). */
|
|
12
|
+
finalText: string;
|
|
13
|
+
status: StreamStatus;
|
|
14
|
+
error: StreamErrorDetail | null;
|
|
15
|
+
interrupt: StreamInterrupt | null;
|
|
16
|
+
resume: (decision: unknown) => Promise<void>;
|
|
17
|
+
lastEvent: StreamEvent | null;
|
|
18
|
+
client: AgenticStream | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* React hook bridging {@link AgenticStream} into reactive state.
|
|
22
|
+
*
|
|
23
|
+
* Text accumulation uses functional `setText` updates (one state slot, O(1) per delta).
|
|
24
|
+
* Status/error/interrupt sync from the core via `onStatusChange` - not reimplemented here.
|
|
25
|
+
* `lastEvent` updates on every event (intentional; use selectors for derived state).
|
|
26
|
+
*/
|
|
27
|
+
declare function useAgenticStream(options: UseAgenticStreamOptions): UseAgenticStreamReturn;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Derive reactive state from the stream's event history via a selector function.
|
|
31
|
+
* Selector functions should be stable (imported, not inline) to avoid redundant recomputation.
|
|
32
|
+
*/
|
|
33
|
+
declare function useStreamSelector<T>(client: AgenticStream | null, selector: StreamSelector<T>): T;
|
|
34
|
+
|
|
35
|
+
export { type UseAgenticStreamOptions, type UseAgenticStreamReturn, useAgenticStream, useStreamSelector };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// src/useAgenticStream.ts
|
|
2
|
+
import {
|
|
3
|
+
AgenticStream,
|
|
4
|
+
executeResume,
|
|
5
|
+
appendFinalTextDelta,
|
|
6
|
+
appendTextDelta
|
|
7
|
+
} from "@agenticgraph/stream-client";
|
|
8
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
9
|
+
function useAgenticStream(options) {
|
|
10
|
+
const { transport, onResume } = options;
|
|
11
|
+
const [text, setText] = useState("");
|
|
12
|
+
const [finalText, setFinalText] = useState("");
|
|
13
|
+
const [status, setStatus] = useState("idle");
|
|
14
|
+
const [error, setError] = useState(null);
|
|
15
|
+
const [interrupt, setInterrupt] = useState(null);
|
|
16
|
+
const [lastEvent, setLastEvent] = useState(null);
|
|
17
|
+
const [client, setClient] = useState(null);
|
|
18
|
+
const onResumeRef = useRef(onResume);
|
|
19
|
+
onResumeRef.current = onResume;
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
let stream = null;
|
|
22
|
+
let unsubEvents;
|
|
23
|
+
let unsubStatus;
|
|
24
|
+
let cancelled = false;
|
|
25
|
+
setText("");
|
|
26
|
+
setFinalText("");
|
|
27
|
+
setStatus("idle");
|
|
28
|
+
setError(null);
|
|
29
|
+
setInterrupt(null);
|
|
30
|
+
setLastEvent(null);
|
|
31
|
+
setClient(null);
|
|
32
|
+
void (async () => {
|
|
33
|
+
try {
|
|
34
|
+
stream = await AgenticStream.connect({ transport });
|
|
35
|
+
} catch (failure) {
|
|
36
|
+
if (!cancelled) {
|
|
37
|
+
setStatus("error");
|
|
38
|
+
setError({ kind: "connection", message: failure instanceof Error ? failure.message : String(failure) });
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (cancelled) {
|
|
43
|
+
await stream.disconnect();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
setClient(stream);
|
|
47
|
+
setStatus(stream.status);
|
|
48
|
+
setError(stream.error);
|
|
49
|
+
setInterrupt(stream.interrupt);
|
|
50
|
+
unsubEvents = stream.on((event) => {
|
|
51
|
+
setLastEvent(event);
|
|
52
|
+
setText((current) => appendTextDelta(current, event));
|
|
53
|
+
setFinalText((current) => appendFinalTextDelta(current, event));
|
|
54
|
+
});
|
|
55
|
+
unsubStatus = stream.onStatusChange((nextStatus, snapshot) => {
|
|
56
|
+
setStatus(nextStatus);
|
|
57
|
+
setError(snapshot.error);
|
|
58
|
+
setInterrupt(snapshot.interrupt);
|
|
59
|
+
});
|
|
60
|
+
})();
|
|
61
|
+
return () => {
|
|
62
|
+
cancelled = true;
|
|
63
|
+
unsubEvents?.();
|
|
64
|
+
unsubStatus?.();
|
|
65
|
+
void stream?.disconnect();
|
|
66
|
+
};
|
|
67
|
+
}, [transport]);
|
|
68
|
+
const resume = useCallback(
|
|
69
|
+
async (decision) => {
|
|
70
|
+
if (client === null) {
|
|
71
|
+
throw new Error("useAgenticStream: stream not connected");
|
|
72
|
+
}
|
|
73
|
+
await executeResume(client, decision, onResumeRef.current);
|
|
74
|
+
},
|
|
75
|
+
[client]
|
|
76
|
+
);
|
|
77
|
+
return {
|
|
78
|
+
text,
|
|
79
|
+
finalText,
|
|
80
|
+
status,
|
|
81
|
+
error,
|
|
82
|
+
interrupt,
|
|
83
|
+
resume,
|
|
84
|
+
lastEvent,
|
|
85
|
+
client
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/useStreamSelector.ts
|
|
90
|
+
import { useEffect as useEffect2, useMemo, useRef as useRef2, useState as useState2 } from "react";
|
|
91
|
+
function useStreamSelector(client, selector) {
|
|
92
|
+
const [events, setEvents] = useState2([]);
|
|
93
|
+
const selectorRef = useRef2(selector);
|
|
94
|
+
selectorRef.current = selector;
|
|
95
|
+
useEffect2(() => {
|
|
96
|
+
if (client === null) {
|
|
97
|
+
setEvents([]);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const unsub = client.on((event) => {
|
|
101
|
+
setEvents((current) => [...current, event]);
|
|
102
|
+
});
|
|
103
|
+
return unsub;
|
|
104
|
+
}, [client]);
|
|
105
|
+
return useMemo(() => selectorRef.current(events), [events]);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/index.ts
|
|
109
|
+
import {
|
|
110
|
+
activeNodeSelector,
|
|
111
|
+
nodeTextSelector,
|
|
112
|
+
thinkingSelector,
|
|
113
|
+
toolCallSelector
|
|
114
|
+
} from "@agenticgraph/stream-client";
|
|
115
|
+
export {
|
|
116
|
+
activeNodeSelector,
|
|
117
|
+
nodeTextSelector,
|
|
118
|
+
thinkingSelector,
|
|
119
|
+
toolCallSelector,
|
|
120
|
+
useAgenticStream,
|
|
121
|
+
useStreamSelector
|
|
122
|
+
};
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useAgenticStream.ts","../src/useStreamSelector.ts","../src/index.ts"],"sourcesContent":["import {\n AgenticStream,\n executeResume,\n appendFinalTextDelta,\n appendTextDelta,\n type ResumeHandler,\n type StreamErrorDetail,\n type StreamEvent,\n type StreamInterrupt,\n type StreamStatus,\n type StreamTransport,\n} from '@agenticgraph/stream-client';\nimport { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseAgenticStreamOptions {\n transport: StreamTransport;\n onResume?: ResumeHandler;\n}\n\nexport interface UseAgenticStreamReturn {\n /** Every text_delta of the run, every step (the narration). */\n text: string;\n /** Text of the current or last step; after a tool run, the final answer (LlmResponse::text on the server). */\n finalText: string;\n status: StreamStatus;\n error: StreamErrorDetail | null;\n interrupt: StreamInterrupt | null;\n resume: (decision: unknown) => Promise<void>;\n lastEvent: StreamEvent | null;\n client: AgenticStream | null;\n}\n\n/**\n * React hook bridging {@link AgenticStream} into reactive state.\n *\n * Text accumulation uses functional `setText` updates (one state slot, O(1) per delta).\n * Status/error/interrupt sync from the core via `onStatusChange` - not reimplemented here.\n * `lastEvent` updates on every event (intentional; use selectors for derived state).\n */\nexport function useAgenticStream(options: UseAgenticStreamOptions): UseAgenticStreamReturn {\n const { transport, onResume } = options;\n\n const [text, setText] = useState('');\n const [finalText, setFinalText] = useState('');\n const [status, setStatus] = useState<StreamStatus>('idle');\n const [error, setError] = useState<StreamErrorDetail | null>(null);\n const [interrupt, setInterrupt] = useState<StreamInterrupt | null>(null);\n const [lastEvent, setLastEvent] = useState<StreamEvent | null>(null);\n const [client, setClient] = useState<AgenticStream | null>(null);\n\n const onResumeRef = useRef(onResume);\n onResumeRef.current = onResume;\n\n useEffect(() => {\n let stream: AgenticStream | null = null;\n let unsubEvents: (() => void) | undefined;\n let unsubStatus: (() => void) | undefined;\n let cancelled = false;\n\n setText('');\n setFinalText('');\n setStatus('idle');\n setError(null);\n setInterrupt(null);\n setLastEvent(null);\n setClient(null);\n\n void (async () => {\n try {\n stream = await AgenticStream.connect({ transport });\n } catch (failure) {\n if (!cancelled) {\n setStatus('error');\n setError({ kind: 'connection', message: failure instanceof Error ? failure.message : String(failure) });\n }\n\n return;\n }\n\n if (cancelled) {\n await stream.disconnect();\n\n return;\n }\n\n setClient(stream);\n setStatus(stream.status);\n setError(stream.error);\n setInterrupt(stream.interrupt);\n\n unsubEvents = stream.on((event) => {\n setLastEvent(event);\n setText((current) => appendTextDelta(current, event));\n setFinalText((current) => appendFinalTextDelta(current, event));\n });\n\n unsubStatus = stream.onStatusChange((nextStatus, snapshot) => {\n setStatus(nextStatus);\n setError(snapshot.error);\n setInterrupt(snapshot.interrupt);\n });\n })();\n\n return () => {\n cancelled = true;\n unsubEvents?.();\n unsubStatus?.();\n void stream?.disconnect();\n };\n }, [transport]);\n\n const resume = useCallback(\n async (decision: unknown) => {\n if (client === null) {\n throw new Error('useAgenticStream: stream not connected');\n }\n\n await executeResume(client, decision, onResumeRef.current);\n },\n [client],\n );\n\n return {\n text,\n finalText,\n status,\n error,\n interrupt,\n resume,\n lastEvent,\n client,\n };\n}\n","import type { AgenticStream, StreamEvent, StreamSelector } from '@agenticgraph/stream-client';\nimport { useEffect, useMemo, useRef, useState } from 'react';\n\n/**\n * Derive reactive state from the stream's event history via a selector function.\n * Selector functions should be stable (imported, not inline) to avoid redundant recomputation.\n */\nexport function useStreamSelector<T>(\n client: AgenticStream | null,\n selector: StreamSelector<T>,\n): T {\n const [events, setEvents] = useState<StreamEvent[]>([]);\n const selectorRef = useRef(selector);\n selectorRef.current = selector;\n\n useEffect(() => {\n if (client === null) {\n setEvents([]);\n\n return;\n }\n\n const unsub = client.on((event) => {\n setEvents((current) => [...current, event]);\n });\n\n return unsub;\n }, [client]);\n\n return useMemo(() => selectorRef.current(events), [events]);\n}\n","export { useAgenticStream } from './useAgenticStream.js';\nexport type { UseAgenticStreamOptions, UseAgenticStreamReturn } from './useAgenticStream.js';\nexport { useStreamSelector } from './useStreamSelector.js';\nexport {\n activeNodeSelector,\n nodeTextSelector,\n thinkingSelector,\n toolCallSelector,\n} from '@agenticgraph/stream-client';\nexport type { StreamSelector } from '@agenticgraph/stream-client';\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAOK;AACP,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AA2BlD,SAAS,iBAAiB,SAA0D;AACzF,QAAM,EAAE,WAAW,SAAS,IAAI;AAEhC,QAAM,CAAC,MAAM,OAAO,IAAI,SAAS,EAAE;AACnC,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,EAAE;AAC7C,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAuB,MAAM;AACzD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAmC,IAAI;AACjE,QAAM,CAAC,WAAW,YAAY,IAAI,SAAiC,IAAI;AACvE,QAAM,CAAC,WAAW,YAAY,IAAI,SAA6B,IAAI;AACnE,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA+B,IAAI;AAE/D,QAAM,cAAc,OAAO,QAAQ;AACnC,cAAY,UAAU;AAEtB,YAAU,MAAM;AACd,QAAI,SAA+B;AACnC,QAAI;AACJ,QAAI;AACJ,QAAI,YAAY;AAEhB,YAAQ,EAAE;AACV,iBAAa,EAAE;AACf,cAAU,MAAM;AAChB,aAAS,IAAI;AACb,iBAAa,IAAI;AACjB,iBAAa,IAAI;AACjB,cAAU,IAAI;AAEd,UAAM,YAAY;AAChB,UAAI;AACF,iBAAS,MAAM,cAAc,QAAQ,EAAE,UAAU,CAAC;AAAA,MACpD,SAAS,SAAS;AAChB,YAAI,CAAC,WAAW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,QAAO,QAAQ;AACnC,cAAY,UAAU;AAEtB,EAAAD,WAAU,MAAM;AACd,QAAI,WAAW,MAAM;AACnB,gBAAU,CAAC,CAAC;AAEZ;AAAA,IACF;AAEA,UAAM,QAAQ,OAAO,GAAG,CAAC,UAAU;AACjC,gBAAU,CAAC,YAAY,CAAC,GAAG,SAAS,KAAK,CAAC;AAAA,IAC5C,CAAC;AAED,WAAO;AAAA,EACT,GAAG,CAAC,MAAM,CAAC;AAEX,SAAO,QAAQ,MAAM,YAAY,QAAQ,MAAM,GAAG,CAAC,MAAM,CAAC;AAC5D;;;AC3BA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":["useEffect","useRef","useState"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agenticgraph/stream-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React hooks and selectors for AgenticGraph streaming",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agenticgraph",
|
|
7
|
+
"laravel",
|
|
8
|
+
"streaming",
|
|
9
|
+
"sse",
|
|
10
|
+
"react",
|
|
11
|
+
"hooks",
|
|
12
|
+
"ai",
|
|
13
|
+
"llm"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://agenticgraph.dev/docs/stream-getting-started",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/shahzad-io/agenticgraph-stream-client/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/shahzad-io/agenticgraph-stream-client.git",
|
|
22
|
+
"directory": "packages/stream-react"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist",
|
|
43
|
+
"README.md",
|
|
44
|
+
"LICENSE"
|
|
45
|
+
],
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup",
|
|
48
|
+
"typecheck": "tsc --noEmit",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"prepublishOnly": "npm run build"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@agenticgraph/stream-client": "^0.1.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"react": ">=18"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@testing-library/react": "^16.3.0",
|
|
60
|
+
"@types/react": "^19.1.8",
|
|
61
|
+
"jsdom": "^26.1.0",
|
|
62
|
+
"react": "^19.1.0",
|
|
63
|
+
"react-dom": "^19.1.0",
|
|
64
|
+
"tsup": "^8.5.0",
|
|
65
|
+
"typescript": "^5.8.3",
|
|
66
|
+
"vitest": "^5.0.5"
|
|
67
|
+
},
|
|
68
|
+
"sideEffects": false,
|
|
69
|
+
"publishConfig": {
|
|
70
|
+
"access": "public"
|
|
71
|
+
},
|
|
72
|
+
"engines": {
|
|
73
|
+
"node": ">=18"
|
|
74
|
+
}
|
|
75
|
+
}
|