@vinaverse/react 0.0.0-stage → 0.1.0-alpha.11
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 +202 -0
- package/README.md +43 -2
- package/dist/agent.cjs +174 -0
- package/dist/agent.cjs.map +1 -0
- package/dist/agent.d.cts +82 -0
- package/dist/agent.d.ts +82 -0
- package/dist/agent.js +3 -0
- package/dist/agent.js.map +1 -0
- package/dist/chunk-V4UBOVTK.js +160 -0
- package/dist/chunk-V4UBOVTK.js.map +1 -0
- package/dist/history.cjs +91 -0
- package/dist/history.cjs.map +1 -0
- package/dist/history.d.cts +55 -0
- package/dist/history.d.ts +55 -0
- package/dist/history.js +77 -0
- package/dist/history.js.map +1 -0
- package/dist/index.cjs +117 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -0
- package/package.json +139 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @vinaverse/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React bindings of the Vinaverse conversation store (`@vinaverse/client/conversation`), and the copy and formatting
|
|
4
|
+
every Vinaverse React UI shares. No `react-native` or DOM import: the same hooks serve React Native and the web. The
|
|
5
|
+
styled React Native kit is `@vinaverse/react-native`.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { createConversation } from '@vinaverse/client/conversation';
|
|
9
|
+
import { CopyProvider, useConversationSelector, useStreamingText } from '@vinaverse/react';
|
|
10
|
+
|
|
11
|
+
const conversation = createConversation({ getSession: (req) => api.vinaverseSession(req), locale: 'es-US' });
|
|
12
|
+
conversation.start();
|
|
13
|
+
|
|
14
|
+
function Messages() {
|
|
15
|
+
const items = useConversationSelector(conversation, (v) => v.items); // re-renders only when items change
|
|
16
|
+
return items.map((item) => <Row key={item.key} item={item} />);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function Streaming({ id }: { id: string }) {
|
|
20
|
+
return <Text>{useStreamingText(conversation, id)}</Text>; // only this re-renders as the reply arrives
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
- `useConversationSelector`, `useStreamingText`, `useConversation`: subscriptions, nothing more; every protocol rule
|
|
25
|
+
is in the store.
|
|
26
|
+
- `CopyProvider` / `useCopy`: the words in English, Spanish and Brazilian Portuguese with the tenant's overrides on
|
|
27
|
+
top, from `@vinaverse/tokens/copy` (shared with the Angular kit and re-exported here with `createTranslate`,
|
|
28
|
+
`formatMoney`, `formatTimeRange`, `formatDate` and `textDirection`). The AI disclosure is never reworded in a
|
|
29
|
+
built-in language, and in another language only by a translation that still discloses.
|
|
30
|
+
|
|
31
|
+
How it fits with the other kits: [docs/architecture/ui-kits.md](../../docs/architecture/ui-kits.md).
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
npm install @vinaverse/react@alpha @vinaverse/client@alpha
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Alphas are published under the `alpha` tag. Keep every `@vinaverse/*` package on the same version, so your app and the
|
|
40
|
+
hooks share one copy of `@vinaverse/client`.
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
Apache-2.0
|
package/dist/agent.cjs
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var agent = require('@vinaverse/client/agent');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
|
|
7
|
+
// src/agent.tsx
|
|
8
|
+
var RuntimeContext = /* @__PURE__ */ react.createContext(null);
|
|
9
|
+
function AgentRuntimeProvider(props) {
|
|
10
|
+
return /* @__PURE__ */ jsxRuntime.jsx(RuntimeContext.Provider, { value: props.runtime, children: props.children });
|
|
11
|
+
}
|
|
12
|
+
function useAgentRuntime() {
|
|
13
|
+
const runtime = react.useContext(RuntimeContext);
|
|
14
|
+
if (!runtime)
|
|
15
|
+
throw new Error("Agent hooks render inside <AgentRuntimeProvider>.");
|
|
16
|
+
return runtime;
|
|
17
|
+
}
|
|
18
|
+
function useAgentConversation(key, open = {}) {
|
|
19
|
+
const runtime = useAgentRuntime();
|
|
20
|
+
const keyRef = react.useRef(key);
|
|
21
|
+
keyRef.current = key;
|
|
22
|
+
const k = agent.agentKey(key);
|
|
23
|
+
const read = react.useCallback(() => runtime.peek(keyRef.current), [runtime, k]);
|
|
24
|
+
const conversation = react.useSyncExternalStore(runtime.subscribe, read, read);
|
|
25
|
+
const openRef = react.useRef(open);
|
|
26
|
+
openRef.current = open;
|
|
27
|
+
react.useLayoutEffect(() => conversation.show(openRef.current), [conversation]);
|
|
28
|
+
const { place, entryPoint } = open;
|
|
29
|
+
const screen = place?.screen;
|
|
30
|
+
const entityType = place?.entity?.type;
|
|
31
|
+
const entityId = place?.entity?.id;
|
|
32
|
+
const filters = place?.filters ? JSON.stringify(place.filters) : null;
|
|
33
|
+
const cleared = place === null;
|
|
34
|
+
react.useLayoutEffect(() => {
|
|
35
|
+
conversation.tell(openRef.current);
|
|
36
|
+
}, [
|
|
37
|
+
conversation,
|
|
38
|
+
screen,
|
|
39
|
+
entityType,
|
|
40
|
+
entityId,
|
|
41
|
+
filters,
|
|
42
|
+
cleared,
|
|
43
|
+
entryPoint
|
|
44
|
+
]);
|
|
45
|
+
const state = react.useSyncExternalStore(
|
|
46
|
+
conversation.subscribe,
|
|
47
|
+
conversation.getState,
|
|
48
|
+
conversation.getState
|
|
49
|
+
);
|
|
50
|
+
const startNew = react.useCallback(
|
|
51
|
+
() => runtime.startNew(keyRef.current),
|
|
52
|
+
[runtime]
|
|
53
|
+
);
|
|
54
|
+
return { conversation, state, startNew };
|
|
55
|
+
}
|
|
56
|
+
function useAgentStatus() {
|
|
57
|
+
const runtime = useAgentRuntime();
|
|
58
|
+
react.useEffect(() => {
|
|
59
|
+
runtime.status().catch(() => void 0);
|
|
60
|
+
}, [runtime]);
|
|
61
|
+
return react.useSyncExternalStore(
|
|
62
|
+
runtime.subscribe,
|
|
63
|
+
runtime.getStatus,
|
|
64
|
+
runtime.getStatus
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
function useAgentStatusState() {
|
|
68
|
+
const runtime = useAgentRuntime();
|
|
69
|
+
react.useEffect(() => {
|
|
70
|
+
runtime.status().catch(() => void 0);
|
|
71
|
+
}, [runtime]);
|
|
72
|
+
return react.useSyncExternalStore(
|
|
73
|
+
runtime.subscribe,
|
|
74
|
+
runtime.getStatusState,
|
|
75
|
+
runtime.getStatusState
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
function useAgentName(fallback) {
|
|
79
|
+
const runtime = useAgentRuntime();
|
|
80
|
+
return react.useSyncExternalStore(runtime.subscribe, runtime.getName, runtime.getName) ?? fallback;
|
|
81
|
+
}
|
|
82
|
+
function useAgentFeatures() {
|
|
83
|
+
const status = useAgentStatus();
|
|
84
|
+
return {
|
|
85
|
+
available: status?.available === true,
|
|
86
|
+
launcher: status?.launcher === true,
|
|
87
|
+
history: status?.features.history === true,
|
|
88
|
+
memory: status?.features.memory === true,
|
|
89
|
+
businessAgent: status?.features.businessAgent === true,
|
|
90
|
+
businessSetup: status?.features.businessSetup === true
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function useAgentProfile(fallback) {
|
|
94
|
+
const status = useAgentStatus();
|
|
95
|
+
return { name: useAgentName(fallback), avatarUrl: status?.avatarUrl ?? null };
|
|
96
|
+
}
|
|
97
|
+
async function forgetGreeting(storage, subject, modes, storageKey = agent.greetedKey) {
|
|
98
|
+
for (const mode of modes) await storage.removeItem(storageKey(subject, mode));
|
|
99
|
+
}
|
|
100
|
+
var MEMORY = /* @__PURE__ */ new Map();
|
|
101
|
+
var memory = {
|
|
102
|
+
getItem: (k) => MEMORY.get(k) ?? null,
|
|
103
|
+
setItem: (k, v) => void MEMORY.set(k, v),
|
|
104
|
+
removeItem: (k) => void MEMORY.delete(k)
|
|
105
|
+
};
|
|
106
|
+
function useAgentLauncher(options) {
|
|
107
|
+
const runtime = useAgentRuntime();
|
|
108
|
+
const status = useAgentStatus();
|
|
109
|
+
const { subject, mode, onOpen } = options;
|
|
110
|
+
const storage = options.storage ?? runtime.greetings ?? memory;
|
|
111
|
+
const shows = status?.launcher === true && options.visible !== false;
|
|
112
|
+
const key = (options.storageKey ?? agent.greetedKey)(subject, mode);
|
|
113
|
+
const [greeted, setGreeted] = react.useState(
|
|
114
|
+
null
|
|
115
|
+
);
|
|
116
|
+
const [due, setDue] = react.useState(null);
|
|
117
|
+
react.useEffect(() => {
|
|
118
|
+
let live = true;
|
|
119
|
+
Promise.resolve(storage.getItem(key)).then(
|
|
120
|
+
(value) => {
|
|
121
|
+
if (live) setGreeted({ key, done: value === "1" });
|
|
122
|
+
},
|
|
123
|
+
() => {
|
|
124
|
+
if (live) setGreeted({ key, done: false });
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
return () => {
|
|
128
|
+
live = false;
|
|
129
|
+
};
|
|
130
|
+
}, [storage, key]);
|
|
131
|
+
const pending = shows && greeted?.key === key && !greeted.done;
|
|
132
|
+
react.useEffect(() => {
|
|
133
|
+
if (!pending) return;
|
|
134
|
+
const timer = setTimeout(() => {
|
|
135
|
+
setDue(key);
|
|
136
|
+
runtime.emit({ type: "launcher_greeted" });
|
|
137
|
+
}, options.delayMs ?? 1200);
|
|
138
|
+
return () => clearTimeout(timer);
|
|
139
|
+
}, [pending, key, runtime, options.delayMs]);
|
|
140
|
+
const done = react.useCallback(() => {
|
|
141
|
+
setGreeted({ key, done: true });
|
|
142
|
+
void Promise.resolve(storage.setItem(key, "1")).catch(() => void 0);
|
|
143
|
+
}, [storage, key]);
|
|
144
|
+
const open = react.useCallback(() => {
|
|
145
|
+
done();
|
|
146
|
+
runtime.emit({ type: "launcher_opened" });
|
|
147
|
+
onOpen();
|
|
148
|
+
}, [done, runtime, onOpen]);
|
|
149
|
+
const dismiss = react.useCallback(() => {
|
|
150
|
+
done();
|
|
151
|
+
runtime.emit({ type: "greeting_closed" });
|
|
152
|
+
}, [done, runtime]);
|
|
153
|
+
react.useEffect(() => {
|
|
154
|
+
if (shows) runtime.emit({ type: "launcher_shown" });
|
|
155
|
+
}, [shows, runtime]);
|
|
156
|
+
return { shows, greets: pending && due === key, open, dismiss };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
Object.defineProperty(exports, "greetedKey", {
|
|
160
|
+
enumerable: true,
|
|
161
|
+
get: function () { return agent.greetedKey; }
|
|
162
|
+
});
|
|
163
|
+
exports.AgentRuntimeProvider = AgentRuntimeProvider;
|
|
164
|
+
exports.forgetGreeting = forgetGreeting;
|
|
165
|
+
exports.useAgentConversation = useAgentConversation;
|
|
166
|
+
exports.useAgentFeatures = useAgentFeatures;
|
|
167
|
+
exports.useAgentLauncher = useAgentLauncher;
|
|
168
|
+
exports.useAgentName = useAgentName;
|
|
169
|
+
exports.useAgentProfile = useAgentProfile;
|
|
170
|
+
exports.useAgentRuntime = useAgentRuntime;
|
|
171
|
+
exports.useAgentStatus = useAgentStatus;
|
|
172
|
+
exports.useAgentStatusState = useAgentStatusState;
|
|
173
|
+
//# sourceMappingURL=agent.cjs.map
|
|
174
|
+
//# sourceMappingURL=agent.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/agent.tsx"],"names":["jsx","useContext","useRef","agentKey","useCallback","useSyncExternalStore","useLayoutEffect","useEffect","greetedKey","useState"],"mappings":";;;;;;;AAyBA,IAAM,cAAA,uCAAoE,IAAI,CAAA;AAGvE,SAAS,qBAAqB,KAAA,EAGvB;AACZ,EAAA,uBACEA,cAAA,CAAC,eAAe,QAAA,EAAf,EAAwB,OAAO,KAAA,CAAM,OAAA,EACnC,gBAAM,QAAA,EACT,CAAA;AAEJ;AAEO,SAAS,eAAA,GAAgC;AAC9C,EAAA,MAAM,OAAA,GAAUC,iBAAW,cAAc,CAAA;AACzC,EAAA,IAAI,CAAC,OAAA;AACH,IAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AACrE,EAAA,OAAO,OAAA;AACT;AAaO,SAAS,oBAAA,CACd,GAAA,EACA,IAAA,GAGI,EAAC,EACiB;AACtB,EAAA,MAAM,UAAU,eAAA,EAAgB;AAChC,EAAA,MAAM,MAAA,GAASC,aAAO,GAAG,CAAA;AACzB,EAAA,MAAA,CAAO,OAAA,GAAU,GAAA;AACjB,EAAA,MAAM,CAAA,GAAIC,eAAS,GAAG,CAAA;AAEtB,EAAA,MAAM,IAAA,GAAOC,iBAAA,CAAY,MAAM,OAAA,CAAQ,IAAA,CAAK,MAAA,CAAO,OAAO,CAAA,EAAG,CAAC,OAAA,EAAS,CAAC,CAAC,CAAA;AACzE,EAAA,MAAM,YAAA,GAAeC,0BAAA,CAAqB,OAAA,CAAQ,SAAA,EAAW,MAAM,IAAI,CAAA;AACvE,EAAA,MAAM,OAAA,GAAUH,aAAO,IAAI,CAAA;AAC3B,EAAA,OAAA,CAAQ,OAAA,GAAU,IAAA;AAGlB,EAAAI,qBAAA,CAAgB,MAAM,aAAa,IAAA,CAAK,OAAA,CAAQ,OAAO,CAAA,EAAG,CAAC,YAAY,CAAC,CAAA;AACxE,EAAA,MAAM,EAAE,KAAA,EAAO,UAAA,EAAW,GAAI,IAAA;AAC9B,EAAA,MAAM,SAAS,KAAA,EAA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,IAAA,OAAA,CAAQ,IAAA,CAAK,EAAE,IAAA,EAAM,iBAAA,EAAmB,CAAA;AACxC,IAAA,MAAA,EAAO;AAAA,EACT,CAAA,EAAG,CAAC,IAAA,EAAM,OAAA,EAAS,MAAM,CAAC,CAAA;AAC1B,EAAA,MAAM,OAAA,GAAUA,kBAAY,MAAM;AAChC,IAAA,IAAA,EAAK;AACL,IAAA,OAAA,CAAQ,IAAA,CAAK,EAAE,IAAA,EAAM,iBAAA,EAAmB,CAAA;AAAA,EAC1C,CAAA,EAAG,CAAC,IAAA,EAAM,OAAO,CAAC,CAAA;AAClB,EAAAG,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,OAAO,OAAA,CAAQ,IAAA,CAAK,EAAE,IAAA,EAAM,kBAAkB,CAAA;AAAA,EACpD,CAAA,EAAG,CAAC,KAAA,EAAO,OAAO,CAAC,CAAA;AACnB,EAAA,OAAO,EAAE,KAAA,EAAO,MAAA,EAAQ,WAAW,GAAA,KAAQ,GAAA,EAAK,MAAM,OAAA,EAAQ;AAChE","file":"agent.cjs","sourcesContent":["// React bindings of the agent runtime (@vinaverse/client/agent): a provider and hooks, thin by design. The runtime holds\n// every rule; these subscribe, show a conversation while a component is mounted, and read the launcher's state. No\n// react-native or DOM import.\nimport type {\n AgentConversation,\n AgentConversationState,\n AgentKey,\n AgentPlace,\n AgentRuntime,\n AgentStatus,\n AgentStatusState,\n} from '@vinaverse/client/agent';\nimport { agentKey, greetedKey } from '@vinaverse/client/agent';\nimport {\n createContext,\n type ReactNode,\n useCallback,\n useContext,\n useEffect,\n useLayoutEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\n\nconst RuntimeContext = /* @__PURE__ */ createContext<AgentRuntime | null>(null);\n\n/** The app's agent runtime, made once (module level, or memoized) and given to everything below. */\nexport function AgentRuntimeProvider(props: {\n readonly runtime: AgentRuntime;\n readonly children?: ReactNode;\n}): ReactNode {\n return (\n <RuntimeContext.Provider value={props.runtime}>\n {props.children}\n </RuntimeContext.Provider>\n );\n}\n\nexport function useAgentRuntime(): AgentRuntime {\n const runtime = useContext(RuntimeContext);\n if (!runtime)\n throw new Error('Agent hooks render inside <AgentRuntimeProvider>.');\n return runtime;\n}\n\nexport interface UseAgentConversation {\n readonly conversation: AgentConversation;\n readonly state: AgentConversationState;\n /** A new conversation for this key (the old one let go and forgotten). */\n readonly startNew: () => void;\n}\n\n/**\n * The key's conversation, shown while the component is mounted (kept across screens; released when it unmounts), told\n * the place and the way in as they change. A start over or its end hands the component the key's next conversation.\n */\nexport function useAgentConversation(\n key: AgentKey,\n open: {\n readonly place?: AgentPlace | null;\n readonly entryPoint?: string | null;\n } = {},\n): UseAgentConversation {\n const runtime = useAgentRuntime();\n const keyRef = useRef(key);\n keyRef.current = key;\n const k = agentKey(key);\n // biome-ignore lint/correctness/useExhaustiveDependencies: the key's string reads it again when the key changes\n const read = useCallback(() => runtime.peek(keyRef.current), [runtime, k]);\n const conversation = useSyncExternalStore(runtime.subscribe, read, read);\n const openRef = useRef(open);\n openRef.current = open;\n // Layout effects: they run before any passive effect, a child's included, so a chat below that starts the\n // conversation early (`VinaverseChat`) finds it shown and told the place.\n useLayoutEffect(() => conversation.show(openRef.current), [conversation]);\n const { place, entryPoint } = open;\n const screen = place?.screen;\n const entityType = place?.entity?.type;\n const entityId = place?.entity?.id;\n const filters = place?.filters ? JSON.stringify(place.filters) : null;\n const cleared = place === null;\n // biome-ignore lint/correctness/useExhaustiveDependencies: told by value, so a place made again each render is the same\n useLayoutEffect(() => {\n conversation.tell(openRef.current);\n }, [\n conversation,\n screen,\n entityType,\n entityId,\n filters,\n cleared,\n entryPoint,\n ]);\n const state = useSyncExternalStore(\n conversation.subscribe,\n conversation.getState,\n conversation.getState,\n );\n const startNew = useCallback(\n () => runtime.startNew(keyRef.current),\n [runtime],\n );\n return { conversation, state, startNew };\n}\n\n/** The agent's status (read once, then at most every few minutes); null until the first read. */\nexport function useAgentStatus(): AgentStatus | null {\n const runtime = useAgentRuntime();\n useEffect(() => {\n runtime.status().catch(() => undefined); // an unread status is no agent: the launcher stays hidden\n }, [runtime]);\n return useSyncExternalStore(\n runtime.subscribe,\n runtime.getStatus,\n runtime.getStatus,\n );\n}\n\n/**\n * The status with where its read stands (`{ status, reading, failed }`): a screen tells \"still reading\" from \"could\n * not read\" and draws each its own way. Reads the status, as `useAgentStatus` does.\n */\nexport function useAgentStatusState(): AgentStatusState {\n const runtime = useAgentRuntime();\n useEffect(() => {\n runtime.status().catch(() => undefined); // said through `failed`\n }, [runtime]);\n return useSyncExternalStore(\n runtime.subscribe,\n runtime.getStatusState,\n runtime.getStatusState,\n );\n}\n\n/** The agent's name: the newest session's, else the status's, else `fallback`. */\nexport function useAgentName(fallback: string): string {\n const runtime = useAgentRuntime();\n return (\n useSyncExternalStore(runtime.subscribe, runtime.getName, runtime.getName) ??\n fallback\n );\n}\n\n/** What the agent can do, from the status (all off until it is read, and when the agent is off). */\nexport function useAgentFeatures(): AgentStatus['features'] & {\n readonly available: boolean;\n readonly launcher: boolean;\n} {\n const status = useAgentStatus();\n return {\n available: status?.available === true,\n launcher: status?.launcher === true,\n history: status?.features.history === true,\n memory: status?.features.memory === true,\n businessAgent: status?.features.businessAgent === true,\n businessSetup: status?.features.businessSetup === true,\n };\n}\n\n/** The agent as an entry shows it before any session: its name (`fallback` until known) and avatar. */\nexport function useAgentProfile(fallback: string): {\n readonly name: string;\n readonly avatarUrl: string | null;\n} {\n const status = useAgentStatus();\n return { name: useAgentName(fallback), avatarUrl: status?.avatarUrl ?? null };\n}\n\n/**\n * Where the launcher remembers that it greeted a person in a mode (sync or async; the app's preferences store). A\n * sign-out keeps it on purpose (a person is not greeted again after signing back in). Given to the runtime as\n * `greetingStorage`, `runtime.erase()` erases it; `forgetGreeting` is the low-level helper for a storage of the app's.\n */\nexport interface GreetingStorage {\n getItem(key: string): string | null | Promise<string | null>;\n setItem(key: string, value: string): void | Promise<void>;\n /** Erasure removes the record, never leaves a key that names the person. */\n removeItem(key: string): void | Promise<void>;\n}\n\n/** Where the greeting is remembered, per person and mode: the default the storage sees, unless the app maps its own. */\nexport { greetedKey };\n\n/** Removes the launcher's record of greeting this person in these modes (`runtime.erase()` does it for `greetingStorage`). */\nexport async function forgetGreeting(\n storage: GreetingStorage,\n subject: string | null,\n modes: readonly string[],\n storageKey: (subject: string | null, mode: string) => string = greetedKey,\n): Promise<void> {\n for (const mode of modes) await storage.removeItem(storageKey(subject, mode));\n}\n\nconst MEMORY = new Map<string, string>();\nconst memory: GreetingStorage = {\n getItem: (k) => MEMORY.get(k) ?? null,\n setItem: (k, v) => void MEMORY.set(k, v),\n removeItem: (k) => void MEMORY.delete(k),\n};\n\nexport interface UseAgentLauncher {\n /** The status offers the launcher, and the app shows it here. */\n readonly shows: boolean;\n /** Its greeting bubble: once per person and mode, a moment after the launcher shows. */\n readonly greets: boolean;\n /** The person opens the agent (the greeting is done too). */\n readonly open: () => void;\n /** The person closes the greeting. */\n readonly dismiss: () => void;\n}\n\n/** The floating launcher's state: whether it shows and greets, and its two actions. */\nexport function useAgentLauncher(options: {\n readonly subject: string | null;\n readonly mode: string;\n /** The app's own rule (the tabs it shows on, no keyboard up…); default true. */\n readonly visible?: boolean;\n readonly storage?: GreetingStorage;\n /** The storage's key for a person and mode, to reuse a record the app already keeps. Default `greetedKey`. */\n readonly storageKey?: (subject: string | null, mode: string) => string;\n /** How long after showing the greeting appears. Default 1200 ms. */\n readonly delayMs?: number;\n readonly onOpen: () => void;\n}): UseAgentLauncher {\n const runtime = useAgentRuntime();\n const status = useAgentStatus();\n const { subject, mode, onOpen } = options;\n // The app's own store, else the runtime's (`greetingStorage`: fenced by a sign-out, reached by `erase()`).\n const storage = options.storage ?? runtime.greetings ?? memory;\n const shows = status?.launcher === true && options.visible !== false;\n const key = (options.storageKey ?? greetedKey)(subject, mode);\n const [greeted, setGreeted] = useState<{ key: string; done: boolean } | null>(\n null,\n );\n const [due, setDue] = useState<string | null>(null);\n useEffect(() => {\n let live = true;\n Promise.resolve(storage.getItem(key)).then(\n (value) => {\n if (live) setGreeted({ key, done: value === '1' });\n },\n () => {\n if (live) setGreeted({ key, done: false });\n },\n );\n return () => {\n live = false;\n };\n }, [storage, key]);\n const pending = shows && greeted?.key === key && !greeted.done;\n useEffect(() => {\n if (!pending) return;\n const timer = setTimeout(() => {\n setDue(key);\n runtime.emit({ type: 'launcher_greeted' });\n }, options.delayMs ?? 1200);\n return () => clearTimeout(timer);\n }, [pending, key, runtime, options.delayMs]);\n const done = useCallback(() => {\n setGreeted({ key, done: true });\n void Promise.resolve(storage.setItem(key, '1')).catch(() => undefined);\n }, [storage, key]);\n const open = useCallback(() => {\n done();\n runtime.emit({ type: 'launcher_opened' });\n onOpen();\n }, [done, runtime, onOpen]);\n const dismiss = useCallback(() => {\n done();\n runtime.emit({ type: 'greeting_closed' });\n }, [done, runtime]);\n useEffect(() => {\n if (shows) runtime.emit({ type: 'launcher_shown' });\n }, [shows, runtime]);\n return { shows, greets: pending && due === key, open, dismiss };\n}\n"]}
|
package/dist/agent.d.cts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentPlace, AgentStatus, AgentStatusState } from '@vinaverse/client/agent';
|
|
2
|
+
export { greetedKey } from '@vinaverse/client/agent';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
|
|
5
|
+
/** The app's agent runtime, made once (module level, or memoized) and given to everything below. */
|
|
6
|
+
declare function AgentRuntimeProvider(props: {
|
|
7
|
+
readonly runtime: AgentRuntime;
|
|
8
|
+
readonly children?: ReactNode;
|
|
9
|
+
}): ReactNode;
|
|
10
|
+
declare function useAgentRuntime(): AgentRuntime;
|
|
11
|
+
interface UseAgentConversation {
|
|
12
|
+
readonly conversation: AgentConversation;
|
|
13
|
+
readonly state: AgentConversationState;
|
|
14
|
+
/** A new conversation for this key (the old one let go and forgotten). */
|
|
15
|
+
readonly startNew: () => void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The key's conversation, shown while the component is mounted (kept across screens; released when it unmounts), told
|
|
19
|
+
* the place and the way in as they change. A start over or its end hands the component the key's next conversation.
|
|
20
|
+
*/
|
|
21
|
+
declare function useAgentConversation(key: AgentKey, open?: {
|
|
22
|
+
readonly place?: AgentPlace | null;
|
|
23
|
+
readonly entryPoint?: string | null;
|
|
24
|
+
}): UseAgentConversation;
|
|
25
|
+
/** The agent's status (read once, then at most every few minutes); null until the first read. */
|
|
26
|
+
declare function useAgentStatus(): AgentStatus | null;
|
|
27
|
+
/**
|
|
28
|
+
* The status with where its read stands (`{ status, reading, failed }`): a screen tells "still reading" from "could
|
|
29
|
+
* not read" and draws each its own way. Reads the status, as `useAgentStatus` does.
|
|
30
|
+
*/
|
|
31
|
+
declare function useAgentStatusState(): AgentStatusState;
|
|
32
|
+
/** The agent's name: the newest session's, else the status's, else `fallback`. */
|
|
33
|
+
declare function useAgentName(fallback: string): string;
|
|
34
|
+
/** What the agent can do, from the status (all off until it is read, and when the agent is off). */
|
|
35
|
+
declare function useAgentFeatures(): AgentStatus['features'] & {
|
|
36
|
+
readonly available: boolean;
|
|
37
|
+
readonly launcher: boolean;
|
|
38
|
+
};
|
|
39
|
+
/** The agent as an entry shows it before any session: its name (`fallback` until known) and avatar. */
|
|
40
|
+
declare function useAgentProfile(fallback: string): {
|
|
41
|
+
readonly name: string;
|
|
42
|
+
readonly avatarUrl: string | null;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Where the launcher remembers that it greeted a person in a mode (sync or async; the app's preferences store). A
|
|
46
|
+
* sign-out keeps it on purpose (a person is not greeted again after signing back in). Given to the runtime as
|
|
47
|
+
* `greetingStorage`, `runtime.erase()` erases it; `forgetGreeting` is the low-level helper for a storage of the app's.
|
|
48
|
+
*/
|
|
49
|
+
interface GreetingStorage {
|
|
50
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
51
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
52
|
+
/** Erasure removes the record, never leaves a key that names the person. */
|
|
53
|
+
removeItem(key: string): void | Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Removes the launcher's record of greeting this person in these modes (`runtime.erase()` does it for `greetingStorage`). */
|
|
57
|
+
declare function forgetGreeting(storage: GreetingStorage, subject: string | null, modes: readonly string[], storageKey?: (subject: string | null, mode: string) => string): Promise<void>;
|
|
58
|
+
interface UseAgentLauncher {
|
|
59
|
+
/** The status offers the launcher, and the app shows it here. */
|
|
60
|
+
readonly shows: boolean;
|
|
61
|
+
/** Its greeting bubble: once per person and mode, a moment after the launcher shows. */
|
|
62
|
+
readonly greets: boolean;
|
|
63
|
+
/** The person opens the agent (the greeting is done too). */
|
|
64
|
+
readonly open: () => void;
|
|
65
|
+
/** The person closes the greeting. */
|
|
66
|
+
readonly dismiss: () => void;
|
|
67
|
+
}
|
|
68
|
+
/** The floating launcher's state: whether it shows and greets, and its two actions. */
|
|
69
|
+
declare function useAgentLauncher(options: {
|
|
70
|
+
readonly subject: string | null;
|
|
71
|
+
readonly mode: string;
|
|
72
|
+
/** The app's own rule (the tabs it shows on, no keyboard up…); default true. */
|
|
73
|
+
readonly visible?: boolean;
|
|
74
|
+
readonly storage?: GreetingStorage;
|
|
75
|
+
/** The storage's key for a person and mode, to reuse a record the app already keeps. Default `greetedKey`. */
|
|
76
|
+
readonly storageKey?: (subject: string | null, mode: string) => string;
|
|
77
|
+
/** How long after showing the greeting appears. Default 1200 ms. */
|
|
78
|
+
readonly delayMs?: number;
|
|
79
|
+
readonly onOpen: () => void;
|
|
80
|
+
}): UseAgentLauncher;
|
|
81
|
+
|
|
82
|
+
export { AgentRuntimeProvider, type GreetingStorage, type UseAgentConversation, type UseAgentLauncher, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState };
|
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentPlace, AgentStatus, AgentStatusState } from '@vinaverse/client/agent';
|
|
2
|
+
export { greetedKey } from '@vinaverse/client/agent';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
|
|
5
|
+
/** The app's agent runtime, made once (module level, or memoized) and given to everything below. */
|
|
6
|
+
declare function AgentRuntimeProvider(props: {
|
|
7
|
+
readonly runtime: AgentRuntime;
|
|
8
|
+
readonly children?: ReactNode;
|
|
9
|
+
}): ReactNode;
|
|
10
|
+
declare function useAgentRuntime(): AgentRuntime;
|
|
11
|
+
interface UseAgentConversation {
|
|
12
|
+
readonly conversation: AgentConversation;
|
|
13
|
+
readonly state: AgentConversationState;
|
|
14
|
+
/** A new conversation for this key (the old one let go and forgotten). */
|
|
15
|
+
readonly startNew: () => void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The key's conversation, shown while the component is mounted (kept across screens; released when it unmounts), told
|
|
19
|
+
* the place and the way in as they change. A start over or its end hands the component the key's next conversation.
|
|
20
|
+
*/
|
|
21
|
+
declare function useAgentConversation(key: AgentKey, open?: {
|
|
22
|
+
readonly place?: AgentPlace | null;
|
|
23
|
+
readonly entryPoint?: string | null;
|
|
24
|
+
}): UseAgentConversation;
|
|
25
|
+
/** The agent's status (read once, then at most every few minutes); null until the first read. */
|
|
26
|
+
declare function useAgentStatus(): AgentStatus | null;
|
|
27
|
+
/**
|
|
28
|
+
* The status with where its read stands (`{ status, reading, failed }`): a screen tells "still reading" from "could
|
|
29
|
+
* not read" and draws each its own way. Reads the status, as `useAgentStatus` does.
|
|
30
|
+
*/
|
|
31
|
+
declare function useAgentStatusState(): AgentStatusState;
|
|
32
|
+
/** The agent's name: the newest session's, else the status's, else `fallback`. */
|
|
33
|
+
declare function useAgentName(fallback: string): string;
|
|
34
|
+
/** What the agent can do, from the status (all off until it is read, and when the agent is off). */
|
|
35
|
+
declare function useAgentFeatures(): AgentStatus['features'] & {
|
|
36
|
+
readonly available: boolean;
|
|
37
|
+
readonly launcher: boolean;
|
|
38
|
+
};
|
|
39
|
+
/** The agent as an entry shows it before any session: its name (`fallback` until known) and avatar. */
|
|
40
|
+
declare function useAgentProfile(fallback: string): {
|
|
41
|
+
readonly name: string;
|
|
42
|
+
readonly avatarUrl: string | null;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Where the launcher remembers that it greeted a person in a mode (sync or async; the app's preferences store). A
|
|
46
|
+
* sign-out keeps it on purpose (a person is not greeted again after signing back in). Given to the runtime as
|
|
47
|
+
* `greetingStorage`, `runtime.erase()` erases it; `forgetGreeting` is the low-level helper for a storage of the app's.
|
|
48
|
+
*/
|
|
49
|
+
interface GreetingStorage {
|
|
50
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
51
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
52
|
+
/** Erasure removes the record, never leaves a key that names the person. */
|
|
53
|
+
removeItem(key: string): void | Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Removes the launcher's record of greeting this person in these modes (`runtime.erase()` does it for `greetingStorage`). */
|
|
57
|
+
declare function forgetGreeting(storage: GreetingStorage, subject: string | null, modes: readonly string[], storageKey?: (subject: string | null, mode: string) => string): Promise<void>;
|
|
58
|
+
interface UseAgentLauncher {
|
|
59
|
+
/** The status offers the launcher, and the app shows it here. */
|
|
60
|
+
readonly shows: boolean;
|
|
61
|
+
/** Its greeting bubble: once per person and mode, a moment after the launcher shows. */
|
|
62
|
+
readonly greets: boolean;
|
|
63
|
+
/** The person opens the agent (the greeting is done too). */
|
|
64
|
+
readonly open: () => void;
|
|
65
|
+
/** The person closes the greeting. */
|
|
66
|
+
readonly dismiss: () => void;
|
|
67
|
+
}
|
|
68
|
+
/** The floating launcher's state: whether it shows and greets, and its two actions. */
|
|
69
|
+
declare function useAgentLauncher(options: {
|
|
70
|
+
readonly subject: string | null;
|
|
71
|
+
readonly mode: string;
|
|
72
|
+
/** The app's own rule (the tabs it shows on, no keyboard up…); default true. */
|
|
73
|
+
readonly visible?: boolean;
|
|
74
|
+
readonly storage?: GreetingStorage;
|
|
75
|
+
/** The storage's key for a person and mode, to reuse a record the app already keeps. Default `greetedKey`. */
|
|
76
|
+
readonly storageKey?: (subject: string | null, mode: string) => string;
|
|
77
|
+
/** How long after showing the greeting appears. Default 1200 ms. */
|
|
78
|
+
readonly delayMs?: number;
|
|
79
|
+
readonly onOpen: () => void;
|
|
80
|
+
}): UseAgentLauncher;
|
|
81
|
+
|
|
82
|
+
export { AgentRuntimeProvider, type GreetingStorage, type UseAgentConversation, type UseAgentLauncher, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState };
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { AgentRuntimeProvider, forgetGreeting, greetedKey, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState } from './chunk-V4UBOVTK.js';
|
|
2
|
+
//# sourceMappingURL=agent.js.map
|
|
3
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"agent.js"}
|