@vinaverse/react 0.1.0-alpha.11 → 0.1.0-alpha.13
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/dist/agent.cjs +43 -165
- package/dist/agent.cjs.map +1 -1
- package/dist/agent.d.cts +9 -9
- package/dist/agent.d.ts +9 -9
- package/dist/agent.js +1 -1
- package/dist/chunk-BCBSNOGI.cjs +182 -0
- package/dist/chunk-BCBSNOGI.cjs.map +1 -0
- package/dist/{chunk-V4UBOVTK.js → chunk-XVAZ57DV.js} +16 -8
- package/dist/chunk-XVAZ57DV.js.map +1 -0
- package/dist/history.cjs +6 -16
- package/dist/history.cjs.map +1 -1
- package/dist/history.d.cts +6 -0
- package/dist/history.d.ts +6 -0
- package/dist/history.js +4 -3
- package/dist/history.js.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-V4UBOVTK.js.map +0 -1
package/dist/agent.cjs
CHANGED
|
@@ -1,174 +1,52 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var react = require('react');
|
|
5
|
-
var jsxRuntime = require('react/jsx-runtime');
|
|
3
|
+
var chunkBCBSNOGI_cjs = require('./chunk-BCBSNOGI.cjs');
|
|
6
4
|
|
|
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
5
|
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "AgentRuntimeProvider", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return chunkBCBSNOGI_cjs.AgentRuntimeProvider; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "forgetGreeting", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return chunkBCBSNOGI_cjs.forgetGreeting; }
|
|
14
|
+
});
|
|
159
15
|
Object.defineProperty(exports, "greetedKey", {
|
|
160
16
|
enumerable: true,
|
|
161
|
-
get: function () { return
|
|
17
|
+
get: function () { return chunkBCBSNOGI_cjs.greetedKey; }
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports, "useAgentConversation", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentConversation; }
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports, "useAgentFeatures", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentFeatures; }
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(exports, "useAgentLauncher", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentLauncher; }
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "useAgentName", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentName; }
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(exports, "useAgentProfile", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentProfile; }
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(exports, "useAgentRuntime", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentRuntime; }
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(exports, "useAgentStatus", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentStatus; }
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(exports, "useAgentStatusState", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function () { return chunkBCBSNOGI_cjs.useAgentStatusState; }
|
|
162
50
|
});
|
|
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
51
|
//# sourceMappingURL=agent.cjs.map
|
|
174
52
|
//# sourceMappingURL=agent.cjs.map
|
package/dist/agent.cjs.map
CHANGED
|
@@ -1 +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,O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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"]}
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"agent.cjs"}
|
package/dist/agent.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey,
|
|
1
|
+
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentOpen, AgentStatus, AgentStatusState } from '@vinaverse/client/agent';
|
|
2
2
|
export { greetedKey } from '@vinaverse/client/agent';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
|
|
@@ -18,10 +18,7 @@ interface UseAgentConversation {
|
|
|
18
18
|
* The key's conversation, shown while the component is mounted (kept across screens; released when it unmounts), told
|
|
19
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
20
|
*/
|
|
21
|
-
declare function useAgentConversation(key: AgentKey, open?:
|
|
22
|
-
readonly place?: AgentPlace | null;
|
|
23
|
-
readonly entryPoint?: string | null;
|
|
24
|
-
}): UseAgentConversation;
|
|
21
|
+
declare function useAgentConversation(key: AgentKey, open?: AgentOpen): UseAgentConversation;
|
|
25
22
|
/** The agent's status (read once, then at most every few minutes); null until the first read. */
|
|
26
23
|
declare function useAgentStatus(): AgentStatus | null;
|
|
27
24
|
/**
|
|
@@ -29,15 +26,18 @@ declare function useAgentStatus(): AgentStatus | null;
|
|
|
29
26
|
* not read" and draws each its own way. Reads the status, as `useAgentStatus` does.
|
|
30
27
|
*/
|
|
31
28
|
declare function useAgentStatusState(): AgentStatusState;
|
|
32
|
-
/**
|
|
33
|
-
|
|
29
|
+
/**
|
|
30
|
+
* The agent's name: the newest session's, else the status's, else `fallback`. With `mode`, that mode's newest session
|
|
31
|
+
* only, so one side's agent is never named after the other's.
|
|
32
|
+
*/
|
|
33
|
+
declare function useAgentName(fallback: string, mode?: string): string;
|
|
34
34
|
/** What the agent can do, from the status (all off until it is read, and when the agent is off). */
|
|
35
35
|
declare function useAgentFeatures(): AgentStatus['features'] & {
|
|
36
36
|
readonly available: boolean;
|
|
37
37
|
readonly launcher: boolean;
|
|
38
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): {
|
|
39
|
+
/** The agent as an entry shows it before any session: its name (`fallback` until known; `mode`'s) and avatar. */
|
|
40
|
+
declare function useAgentProfile(fallback: string, mode?: string): {
|
|
41
41
|
readonly name: string;
|
|
42
42
|
readonly avatarUrl: string | null;
|
|
43
43
|
};
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey,
|
|
1
|
+
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentOpen, AgentStatus, AgentStatusState } from '@vinaverse/client/agent';
|
|
2
2
|
export { greetedKey } from '@vinaverse/client/agent';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
|
|
@@ -18,10 +18,7 @@ interface UseAgentConversation {
|
|
|
18
18
|
* The key's conversation, shown while the component is mounted (kept across screens; released when it unmounts), told
|
|
19
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
20
|
*/
|
|
21
|
-
declare function useAgentConversation(key: AgentKey, open?:
|
|
22
|
-
readonly place?: AgentPlace | null;
|
|
23
|
-
readonly entryPoint?: string | null;
|
|
24
|
-
}): UseAgentConversation;
|
|
21
|
+
declare function useAgentConversation(key: AgentKey, open?: AgentOpen): UseAgentConversation;
|
|
25
22
|
/** The agent's status (read once, then at most every few minutes); null until the first read. */
|
|
26
23
|
declare function useAgentStatus(): AgentStatus | null;
|
|
27
24
|
/**
|
|
@@ -29,15 +26,18 @@ declare function useAgentStatus(): AgentStatus | null;
|
|
|
29
26
|
* not read" and draws each its own way. Reads the status, as `useAgentStatus` does.
|
|
30
27
|
*/
|
|
31
28
|
declare function useAgentStatusState(): AgentStatusState;
|
|
32
|
-
/**
|
|
33
|
-
|
|
29
|
+
/**
|
|
30
|
+
* The agent's name: the newest session's, else the status's, else `fallback`. With `mode`, that mode's newest session
|
|
31
|
+
* only, so one side's agent is never named after the other's.
|
|
32
|
+
*/
|
|
33
|
+
declare function useAgentName(fallback: string, mode?: string): string;
|
|
34
34
|
/** What the agent can do, from the status (all off until it is read, and when the agent is off). */
|
|
35
35
|
declare function useAgentFeatures(): AgentStatus['features'] & {
|
|
36
36
|
readonly available: boolean;
|
|
37
37
|
readonly launcher: boolean;
|
|
38
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): {
|
|
39
|
+
/** The agent as an entry shows it before any session: its name (`fallback` until known; `mode`'s) and avatar. */
|
|
40
|
+
declare function useAgentProfile(fallback: string, mode?: string): {
|
|
41
41
|
readonly name: string;
|
|
42
42
|
readonly avatarUrl: string | null;
|
|
43
43
|
};
|
package/dist/agent.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { AgentRuntimeProvider, forgetGreeting, greetedKey, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState } from './chunk-
|
|
1
|
+
export { AgentRuntimeProvider, forgetGreeting, greetedKey, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState } from './chunk-XVAZ57DV.js';
|
|
2
2
|
//# sourceMappingURL=agent.js.map
|
|
3
3
|
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1,182 @@
|
|
|
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, anchor, entry } = 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
|
+
const about = anchor ? `${anchor.type}\0${anchor.id}` : anchor;
|
|
35
|
+
const via = entry ? `${entry.point}\0${entry.label ?? ""}` : entry;
|
|
36
|
+
react.useLayoutEffect(() => {
|
|
37
|
+
conversation.tell(openRef.current);
|
|
38
|
+
}, [
|
|
39
|
+
conversation,
|
|
40
|
+
screen,
|
|
41
|
+
entityType,
|
|
42
|
+
entityId,
|
|
43
|
+
filters,
|
|
44
|
+
cleared,
|
|
45
|
+
entryPoint,
|
|
46
|
+
about,
|
|
47
|
+
via
|
|
48
|
+
]);
|
|
49
|
+
const state = react.useSyncExternalStore(
|
|
50
|
+
conversation.subscribe,
|
|
51
|
+
conversation.getState,
|
|
52
|
+
conversation.getState
|
|
53
|
+
);
|
|
54
|
+
const startNew = react.useCallback(
|
|
55
|
+
() => runtime.startNew(keyRef.current),
|
|
56
|
+
[runtime]
|
|
57
|
+
);
|
|
58
|
+
return { conversation, state, startNew };
|
|
59
|
+
}
|
|
60
|
+
function useAgentStatus() {
|
|
61
|
+
const runtime = useAgentRuntime();
|
|
62
|
+
react.useEffect(() => {
|
|
63
|
+
runtime.status().catch(() => void 0);
|
|
64
|
+
}, [runtime]);
|
|
65
|
+
return react.useSyncExternalStore(
|
|
66
|
+
runtime.subscribe,
|
|
67
|
+
runtime.getStatus,
|
|
68
|
+
runtime.getStatus
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
function useAgentStatusState() {
|
|
72
|
+
const runtime = useAgentRuntime();
|
|
73
|
+
react.useEffect(() => {
|
|
74
|
+
runtime.status().catch(() => void 0);
|
|
75
|
+
}, [runtime]);
|
|
76
|
+
return react.useSyncExternalStore(
|
|
77
|
+
runtime.subscribe,
|
|
78
|
+
runtime.getStatusState,
|
|
79
|
+
runtime.getStatusState
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
function useAgentName(fallback, mode) {
|
|
83
|
+
const runtime = useAgentRuntime();
|
|
84
|
+
const read = react.useCallback(() => runtime.getName(mode), [runtime, mode]);
|
|
85
|
+
return react.useSyncExternalStore(runtime.subscribe, read, read) ?? fallback;
|
|
86
|
+
}
|
|
87
|
+
function useAgentFeatures() {
|
|
88
|
+
const status = useAgentStatus();
|
|
89
|
+
return {
|
|
90
|
+
available: status?.available === true,
|
|
91
|
+
launcher: status?.launcher === true,
|
|
92
|
+
history: status?.features.history === true,
|
|
93
|
+
memory: status?.features.memory === true,
|
|
94
|
+
businessAgent: status?.features.businessAgent === true,
|
|
95
|
+
businessSetup: status?.features.businessSetup === true
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function useAgentProfile(fallback, mode) {
|
|
99
|
+
const status = useAgentStatus();
|
|
100
|
+
return {
|
|
101
|
+
name: useAgentName(fallback, mode),
|
|
102
|
+
avatarUrl: status?.avatarUrl ?? null
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async function forgetGreeting(storage, subject, modes, storageKey = agent.greetedKey) {
|
|
106
|
+
for (const mode of modes) await storage.removeItem(storageKey(subject, mode));
|
|
107
|
+
}
|
|
108
|
+
var MEMORY = /* @__PURE__ */ new Map();
|
|
109
|
+
var memory = {
|
|
110
|
+
getItem: (k) => MEMORY.get(k) ?? null,
|
|
111
|
+
setItem: (k, v) => void MEMORY.set(k, v),
|
|
112
|
+
removeItem: (k) => void MEMORY.delete(k)
|
|
113
|
+
};
|
|
114
|
+
function useAgentLauncher(options) {
|
|
115
|
+
const runtime = useAgentRuntime();
|
|
116
|
+
const status = useAgentStatus();
|
|
117
|
+
const { subject, mode, onOpen } = options;
|
|
118
|
+
const storage = options.storage ?? runtime.greetings ?? memory;
|
|
119
|
+
const shows = status?.launcher === true && options.visible !== false;
|
|
120
|
+
const key = (options.storageKey ?? agent.greetedKey)(subject, mode);
|
|
121
|
+
const [greeted, setGreeted] = react.useState(
|
|
122
|
+
null
|
|
123
|
+
);
|
|
124
|
+
const [due, setDue] = react.useState(null);
|
|
125
|
+
react.useEffect(() => {
|
|
126
|
+
let live = true;
|
|
127
|
+
Promise.resolve(storage.getItem(key)).then(
|
|
128
|
+
(value) => {
|
|
129
|
+
if (live) setGreeted({ key, done: value === "1" });
|
|
130
|
+
},
|
|
131
|
+
() => {
|
|
132
|
+
if (live) setGreeted({ key, done: false });
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
return () => {
|
|
136
|
+
live = false;
|
|
137
|
+
};
|
|
138
|
+
}, [storage, key]);
|
|
139
|
+
const pending = shows && greeted?.key === key && !greeted.done;
|
|
140
|
+
react.useEffect(() => {
|
|
141
|
+
if (!pending) return;
|
|
142
|
+
const timer = setTimeout(() => {
|
|
143
|
+
setDue(key);
|
|
144
|
+
runtime.emit({ type: "launcher_greeted" });
|
|
145
|
+
}, options.delayMs ?? 1200);
|
|
146
|
+
return () => clearTimeout(timer);
|
|
147
|
+
}, [pending, key, runtime, options.delayMs]);
|
|
148
|
+
const done = react.useCallback(() => {
|
|
149
|
+
setGreeted({ key, done: true });
|
|
150
|
+
void Promise.resolve(storage.setItem(key, "1")).catch(() => void 0);
|
|
151
|
+
}, [storage, key]);
|
|
152
|
+
const open = react.useCallback(() => {
|
|
153
|
+
done();
|
|
154
|
+
runtime.emit({ type: "launcher_opened" });
|
|
155
|
+
onOpen();
|
|
156
|
+
}, [done, runtime, onOpen]);
|
|
157
|
+
const dismiss = react.useCallback(() => {
|
|
158
|
+
done();
|
|
159
|
+
runtime.emit({ type: "greeting_closed" });
|
|
160
|
+
}, [done, runtime]);
|
|
161
|
+
react.useEffect(() => {
|
|
162
|
+
if (shows) runtime.emit({ type: "launcher_shown" });
|
|
163
|
+
}, [shows, runtime]);
|
|
164
|
+
return { shows, greets: pending && due === key, open, dismiss };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
Object.defineProperty(exports, "greetedKey", {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
get: function () { return agent.greetedKey; }
|
|
170
|
+
});
|
|
171
|
+
exports.AgentRuntimeProvider = AgentRuntimeProvider;
|
|
172
|
+
exports.forgetGreeting = forgetGreeting;
|
|
173
|
+
exports.useAgentConversation = useAgentConversation;
|
|
174
|
+
exports.useAgentFeatures = useAgentFeatures;
|
|
175
|
+
exports.useAgentLauncher = useAgentLauncher;
|
|
176
|
+
exports.useAgentName = useAgentName;
|
|
177
|
+
exports.useAgentProfile = useAgentProfile;
|
|
178
|
+
exports.useAgentRuntime = useAgentRuntime;
|
|
179
|
+
exports.useAgentStatus = useAgentStatus;
|
|
180
|
+
exports.useAgentStatusState = useAgentStatusState;
|
|
181
|
+
//# sourceMappingURL=chunk-BCBSNOGI.cjs.map
|
|
182
|
+
//# sourceMappingURL=chunk-BCBSNOGI.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,GAAkB,EAAC,EACG;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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AA,OAAO,EAAE,KAAA,EAAO,MAAA,EAAQ,WAAW,GAAA,KAAQ,GAAA,EAAK,MAAM,OAAA,EAAQ;AAChE","file":"chunk-BCBSNOGI.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 AgentOpen,\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: AgentOpen = {},\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, anchor, entry } = 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 const about = anchor ? `${anchor.type}\\u0000${anchor.id}` : anchor;\n const via = entry ? `${entry.point}\\u0000${entry.label ?? ''}` : entry;\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 about,\n via,\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/**\n * The agent's name: the newest session's, else the status's, else `fallback`. With `mode`, that mode's newest session\n * only, so one side's agent is never named after the other's.\n */\nexport function useAgentName(fallback: string, mode?: string): string {\n const runtime = useAgentRuntime();\n const read = useCallback(() => runtime.getName(mode), [runtime, mode]);\n return useSyncExternalStore(runtime.subscribe, read, read) ?? fallback;\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; `mode`'s) and avatar. */\nexport function useAgentProfile(\n fallback: string,\n mode?: string,\n): {\n readonly name: string;\n readonly avatarUrl: string | null;\n} {\n const status = useAgentStatus();\n return {\n name: useAgentName(fallback, mode),\n avatarUrl: status?.avatarUrl ?? null,\n };\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"]}
|
|
@@ -24,12 +24,14 @@ function useAgentConversation(key, open = {}) {
|
|
|
24
24
|
const openRef = useRef(open);
|
|
25
25
|
openRef.current = open;
|
|
26
26
|
useLayoutEffect(() => conversation.show(openRef.current), [conversation]);
|
|
27
|
-
const { place, entryPoint } = open;
|
|
27
|
+
const { place, entryPoint, anchor, entry } = open;
|
|
28
28
|
const screen = place?.screen;
|
|
29
29
|
const entityType = place?.entity?.type;
|
|
30
30
|
const entityId = place?.entity?.id;
|
|
31
31
|
const filters = place?.filters ? JSON.stringify(place.filters) : null;
|
|
32
32
|
const cleared = place === null;
|
|
33
|
+
const about = anchor ? `${anchor.type}\0${anchor.id}` : anchor;
|
|
34
|
+
const via = entry ? `${entry.point}\0${entry.label ?? ""}` : entry;
|
|
33
35
|
useLayoutEffect(() => {
|
|
34
36
|
conversation.tell(openRef.current);
|
|
35
37
|
}, [
|
|
@@ -39,7 +41,9 @@ function useAgentConversation(key, open = {}) {
|
|
|
39
41
|
entityId,
|
|
40
42
|
filters,
|
|
41
43
|
cleared,
|
|
42
|
-
entryPoint
|
|
44
|
+
entryPoint,
|
|
45
|
+
about,
|
|
46
|
+
via
|
|
43
47
|
]);
|
|
44
48
|
const state = useSyncExternalStore(
|
|
45
49
|
conversation.subscribe,
|
|
@@ -74,9 +78,10 @@ function useAgentStatusState() {
|
|
|
74
78
|
runtime.getStatusState
|
|
75
79
|
);
|
|
76
80
|
}
|
|
77
|
-
function useAgentName(fallback) {
|
|
81
|
+
function useAgentName(fallback, mode) {
|
|
78
82
|
const runtime = useAgentRuntime();
|
|
79
|
-
|
|
83
|
+
const read = useCallback(() => runtime.getName(mode), [runtime, mode]);
|
|
84
|
+
return useSyncExternalStore(runtime.subscribe, read, read) ?? fallback;
|
|
80
85
|
}
|
|
81
86
|
function useAgentFeatures() {
|
|
82
87
|
const status = useAgentStatus();
|
|
@@ -89,9 +94,12 @@ function useAgentFeatures() {
|
|
|
89
94
|
businessSetup: status?.features.businessSetup === true
|
|
90
95
|
};
|
|
91
96
|
}
|
|
92
|
-
function useAgentProfile(fallback) {
|
|
97
|
+
function useAgentProfile(fallback, mode) {
|
|
93
98
|
const status = useAgentStatus();
|
|
94
|
-
return {
|
|
99
|
+
return {
|
|
100
|
+
name: useAgentName(fallback, mode),
|
|
101
|
+
avatarUrl: status?.avatarUrl ?? null
|
|
102
|
+
};
|
|
95
103
|
}
|
|
96
104
|
async function forgetGreeting(storage, subject, modes, storageKey = greetedKey) {
|
|
97
105
|
for (const mode of modes) await storage.removeItem(storageKey(subject, mode));
|
|
@@ -156,5 +164,5 @@ function useAgentLauncher(options) {
|
|
|
156
164
|
}
|
|
157
165
|
|
|
158
166
|
export { AgentRuntimeProvider, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState };
|
|
159
|
-
//# sourceMappingURL=chunk-
|
|
160
|
-
//# sourceMappingURL=chunk-
|
|
167
|
+
//# sourceMappingURL=chunk-XVAZ57DV.js.map
|
|
168
|
+
//# sourceMappingURL=chunk-XVAZ57DV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/agent.tsx"],"names":[],"mappings":";;;;;;AAyBA,IAAM,cAAA,iCAAoE,IAAI,CAAA;AAGvE,SAAS,qBAAqB,KAAA,EAGvB;AACZ,EAAA,uBACE,GAAA,CAAC,eAAe,QAAA,EAAf,EAAwB,OAAO,KAAA,CAAM,OAAA,EACnC,gBAAM,QAAA,EACT,CAAA;AAEJ;AAEO,SAAS,eAAA,GAA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AAS,CAAA;AAAA,EACvE,CAAA,EAAG,CAAC,OAAA,EAAS,GAAG,CAAC,CAAA;AACjB,EAAA,MAAM,IAAA,GAAO,YAAY,MAAM;AAC7B,IAAA,IAAA,EAAK;AACL,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,GAAU,YAAY,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,EAAA,SAAA,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":"chunk-XVAZ57DV.js","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 AgentOpen,\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: AgentOpen = {},\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, anchor, entry } = 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 const about = anchor ? `${anchor.type}\\u0000${anchor.id}` : anchor;\n const via = entry ? `${entry.point}\\u0000${entry.label ?? ''}` : entry;\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 about,\n via,\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/**\n * The agent's name: the newest session's, else the status's, else `fallback`. With `mode`, that mode's newest session\n * only, so one side's agent is never named after the other's.\n */\nexport function useAgentName(fallback: string, mode?: string): string {\n const runtime = useAgentRuntime();\n const read = useCallback(() => runtime.getName(mode), [runtime, mode]);\n return useSyncExternalStore(runtime.subscribe, read, read) ?? fallback;\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; `mode`'s) and avatar. */\nexport function useAgentProfile(\n fallback: string,\n mode?: string,\n): {\n readonly name: string;\n readonly avatarUrl: string | null;\n} {\n const status = useAgentStatus();\n return {\n name: useAgentName(fallback, mode),\n avatarUrl: status?.avatarUrl ?? null,\n };\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/history.cjs
CHANGED
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var chunkBCBSNOGI_cjs = require('./chunk-BCBSNOGI.cjs');
|
|
3
4
|
var history = require('@vinaverse/client/history');
|
|
4
5
|
var react = require('react');
|
|
5
|
-
require('@vinaverse/client/agent');
|
|
6
|
-
require('react/jsx-runtime');
|
|
7
6
|
|
|
8
|
-
// src/history.tsx
|
|
9
|
-
var RuntimeContext = /* @__PURE__ */ react.createContext(null);
|
|
10
|
-
function useAgentRuntime() {
|
|
11
|
-
const runtime = react.useContext(RuntimeContext);
|
|
12
|
-
if (!runtime)
|
|
13
|
-
throw new Error("Agent hooks render inside <AgentRuntimeProvider>.");
|
|
14
|
-
return runtime;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// src/history.tsx
|
|
18
7
|
function useAgentHistory(options) {
|
|
19
|
-
const runtime = useAgentRuntime();
|
|
8
|
+
const runtime = chunkBCBSNOGI_cjs.useAgentRuntime();
|
|
20
9
|
const { subject, mode, thread, pageSize } = options;
|
|
21
10
|
const scope = options.scope ?? null;
|
|
22
11
|
const agent = options.agent ?? null;
|
|
@@ -38,9 +27,10 @@ function useAgentHistory(options) {
|
|
|
38
27
|
react.useEffect(() => {
|
|
39
28
|
if (!state.started) history$1.refresh();
|
|
40
29
|
}, [history$1, state.started]);
|
|
30
|
+
const own = options.keyOf;
|
|
41
31
|
const keyOf = react.useCallback(
|
|
42
|
-
(conversation) => history.continueKey(conversation, subject, thread),
|
|
43
|
-
[subject, thread]
|
|
32
|
+
(conversation) => own ? own(conversation) : history.continueKey(conversation, subject, thread),
|
|
33
|
+
[own, subject, thread]
|
|
44
34
|
);
|
|
45
35
|
return {
|
|
46
36
|
state,
|
|
@@ -59,7 +49,7 @@ function useAgentHistory(options) {
|
|
|
59
49
|
};
|
|
60
50
|
}
|
|
61
51
|
function useAgentTranscript(conversationId, options = {}) {
|
|
62
|
-
const runtime = useAgentRuntime();
|
|
52
|
+
const runtime = chunkBCBSNOGI_cjs.useAgentRuntime();
|
|
63
53
|
const { pageSize } = options;
|
|
64
54
|
const transcript = react.useMemo(
|
|
65
55
|
() => history.createAgentTranscript({
|
package/dist/history.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/agent.tsx","../src/history.tsx"],"names":["useContext","history","useMemo","createAgentHistory","useSyncExternalStore","useEffect","useCallback","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,OAAO,CAAC,CAAA;AAC9B,EAAA,OAAO;AAAA,IACL,KAAA;AAAA,IACA,MAAM,UAAA,CAAW,IAAA;AAAA,IACjB,aAAa,UAAA,CAAW,WAAA;AAAA,IACxB,OAAO,UAAA,CAAW;AAAA,GACpB;AACF","file":"history.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","// React bindings of a person's history (@vinaverse/client/history): the list of their past conversations and one\n// read back, each a store subscribed to while a component is mounted; delete and continue through the runtime. An\n// entry of its own, so an app that draws no history ships none of it. No react-native or DOM import.\nimport type { AgentConversation, AgentKey } from '@vinaverse/client/agent';\nimport {\n type AgentHistory,\n type AgentHistoryState,\n type AgentTranscript,\n type AgentTranscriptState,\n type ConversationSummary,\n continueKey,\n createAgentHistory,\n createAgentTranscript,\n} from '@vinaverse/client/history';\nimport { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react';\nimport { useAgentRuntime } from './agent.js';\n\nexport interface UseAgentHistoryOptions {\n /** The person signed in (null for a guest): the key a conversation continues or is deleted under. */\n readonly subject: string | null;\n /** The app's side: `customer` or `business`. */\n readonly mode: string;\n /** The business acted for, with `business`. */\n readonly scope?: string | null;\n /** The agent, for a backend that serves more than one. */\n readonly agent?: string | null;\n /** The thread a continued conversation runs in; default `chat`. */\n readonly thread?: string;\n /** Conversations per page (1–50). Default 20. */\n readonly pageSize?: number;\n}\n\nexport interface UseAgentHistory {\n readonly state: AgentHistoryState;\n /** Reads the first page anew (after a failure, or to see what changed). */\n readonly refresh: () => void;\n /** The next older page, when there is one and none is being read. */\n readonly loadMore: () => void;\n /** Reads again the page that failed: the first, or the next. */\n readonly retry: () => void;\n /** The key the conversation continues under for this person (`continueKey`). */\n readonly keyOf: (conversation: ConversationSummary) => AgentKey;\n /**\n * Deletes the conversation at the backend and drops its row; the device forgets it only where its key still\n * continues it. One already gone is dropped too; any other failure is thrown, the row kept.\n */\n readonly remove: (conversation: ConversationSummary) => Promise<void>;\n /**\n * The person goes on with a past conversation: pinned for its key and opened through the backend's resume when the\n * app shows it (`AgentChat` with `keyOf(conversation)`, or `show()` on what this returns).\n */\n readonly continue: (conversation: ConversationSummary) => AgentConversation;\n}\n\n/** The person's past conversations, read as the component mounts, newest activity first. */\nexport function useAgentHistory(\n options: UseAgentHistoryOptions,\n): UseAgentHistory {\n const runtime = useAgentRuntime();\n const { subject, mode, thread, pageSize } = options;\n const scope = options.scope ?? null;\n const agent = options.agent ?? null;\n const history = useMemo<AgentHistory>(\n () =>\n createAgentHistory({\n runtime,\n mode,\n scope,\n agent,\n ...(pageSize === undefined ? {} : { pageSize }),\n }),\n [runtime, mode, scope, agent, pageSize],\n );\n const state = useSyncExternalStore(\n history.subscribe,\n history.getSnapshot,\n history.getSnapshot,\n );\n useEffect(() => {\n if (!state.started) history.refresh();\n }, [history, state.started]);\n const keyOf = useCallback(\n (conversation: ConversationSummary) =>\n continueKey(conversation, subject, thread),\n [subject, thread],\n );\n return {\n state,\n refresh: history.refresh,\n loadMore: history.loadMore,\n retry: history.retry,\n keyOf,\n remove: useCallback(\n (conversation: ConversationSummary) =>\n history.remove(keyOf(conversation), conversation.id),\n [history, keyOf],\n ),\n continue: useCallback(\n (conversation: ConversationSummary) =>\n runtime.continue(keyOf(conversation), conversation.id),\n [runtime, keyOf],\n ),\n };\n}\n\nexport interface UseAgentTranscript {\n readonly state: AgentTranscriptState;\n /** Reads the newest page anew. */\n readonly load: () => void;\n /** The page before the earliest read, prepended. */\n readonly loadEarlier: () => void;\n /** Reads again the page that failed: the newest, or the earlier one. */\n readonly retry: () => void;\n}\n\n/** A past conversation read back, newest page first, as the component mounts. */\nexport function useAgentTranscript(\n conversationId: string,\n options: { readonly pageSize?: number } = {},\n): UseAgentTranscript {\n const runtime = useAgentRuntime();\n const { pageSize } = options;\n const transcript = useMemo<AgentTranscript>(\n () =>\n createAgentTranscript({\n runtime,\n conversationId,\n ...(pageSize === undefined ? {} : { pageSize }),\n }),\n [runtime, conversationId, pageSize],\n );\n const state = useSyncExternalStore(\n transcript.subscribe,\n transcript.getSnapshot,\n transcript.getSnapshot,\n );\n useEffect(() => {\n if (!state.started) transcript.load();\n }, [transcript, state.started]);\n return {\n state,\n load: transcript.load,\n loadEarlier: transcript.loadEarlier,\n retry: transcript.retry,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/history.tsx"],"names":["useAgentRuntime","history","useMemo","createAgentHistory","useSyncExternalStore","useEffect","useCallback","continueKey","createAgentTranscript"],"mappings":";;;;;;AA6DO,SAAS,gBACd,OAAA,EACiB;AACjB,EAAA,MAAM,UAAUA,iCAAA,EAAgB;AAChC,EAAA,MAAM,EAAE,OAAA,EAAS,IAAA,EAAM,MAAA,EAAQ,UAAS,GAAI,OAAA;AAC5C,EAAA,MAAM,KAAA,GAAQ,QAAQ,KAAA,IAAS,IAAA;AAC/B,EAAA,MAAM,KAAA,GAAQ,QAAQ,KAAA,IAAS,IAAA;AAC/B,EAAA,MAAMC,SAAA,GAAUC,aAAA;AAAA,IACd,MACEC,0BAAA,CAAmB;AAAA,MACjB,OAAA;AAAA,MACA,IAAA;AAAA,MACA,KAAA;AAAA,MACA,KAAA;AAAA,MACA,GAAI,QAAA,KAAa,MAAA,GAAY,EAAC,GAAI,EAAE,QAAA;AAAS,KAC9C,CAAA;AAAA,IACH,CAAC,OAAA,EAAS,IAAA,EAAM,KAAA,EAAO,OAAO,QAAQ;AAAA,GACxC;AACA,EAAA,MAAM,KAAA,GAAQC,0BAAA;AAAA,IACZH,SAAA,CAAQ,SAAA;AAAA,IACRA,SAAA,CAAQ,WAAA;AAAA,IACRA,SAAA,CAAQ;AAAA,GACV;AACA,EAAAI,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,CAAC,KAAA,CAAM,OAAA,EAASJ,SAAA,CAAQ,OAAA,EAAQ;AAAA,EACtC,CAAA,EAAG,CAACA,SAAA,EAAS,KAAA,CAAM,OAAO,CAAC,CAAA;AAC3B,EAAA,MAAM,MAAM,OAAA,CAAQ,KAAA;AACpB,EAAA,MAAM,KAAA,GAAQK,iBAAA;AAAA,IACZ,CAAC,iBACC,GAAA,GAAM,GAAA,CAAI,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AAA,CAAW,IAAA,EAAK;AAAA,EACtC,CAAA,EAAG,CAAC,UAAA,EAAY,KAAA,CAAM,OAAO,CAAC,CAAA;AAC9B,EAAA,OAAO;AAAA,IACL,KAAA;AAAA,IACA,MAAM,UAAA,CAAW,IAAA;AAAA,IACjB,aAAa,UAAA,CAAW,WAAA;AAAA,IACxB,OAAO,UAAA,CAAW;AAAA,GACpB;AACF","file":"history.cjs","sourcesContent":["// React bindings of a person's history (@vinaverse/client/history): the list of their past conversations and one\n// read back, each a store subscribed to while a component is mounted; delete and continue through the runtime. An\n// entry of its own, so an app that draws no history ships none of it. No react-native or DOM import.\nimport type { AgentConversation, AgentKey } from '@vinaverse/client/agent';\nimport {\n type AgentHistory,\n type AgentHistoryState,\n type AgentTranscript,\n type AgentTranscriptState,\n type ConversationSummary,\n continueKey,\n createAgentHistory,\n createAgentTranscript,\n} from '@vinaverse/client/history';\nimport { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react';\nimport { useAgentRuntime } from './agent.js';\n\nexport interface UseAgentHistoryOptions {\n /** The person signed in (null for a guest): the key a conversation continues or is deleted under. */\n readonly subject: string | null;\n /** The app's side: `customer` or `business`. */\n readonly mode: string;\n /** The business acted for, with `business`. */\n readonly scope?: string | null;\n /** The agent, for a backend that serves more than one. */\n readonly agent?: string | null;\n /** The thread a continued conversation runs in; default `chat`. */\n readonly thread?: string;\n /** Conversations per page (1–50). Default 20. */\n readonly pageSize?: number;\n /**\n * The app's key for a past conversation, when its keys are not the server's (`experience_mode` is `customer` or\n * `business`; the app may key its modes in its own words). Continue and delete go under it. Keep it stable.\n * Default: `continueKey(conversation, subject, thread)`.\n */\n readonly keyOf?: (conversation: ConversationSummary) => AgentKey;\n}\n\nexport interface UseAgentHistory {\n readonly state: AgentHistoryState;\n /** Reads the first page anew (after a failure, or to see what changed). */\n readonly refresh: () => void;\n /** The next older page, when there is one and none is being read. */\n readonly loadMore: () => void;\n /** Reads again the page that failed: the first, or the next. */\n readonly retry: () => void;\n /** The key the conversation continues under for this person (`continueKey`). */\n readonly keyOf: (conversation: ConversationSummary) => AgentKey;\n /**\n * Deletes the conversation at the backend and drops its row; the device forgets it only where its key still\n * continues it. One already gone is dropped too; any other failure is thrown, the row kept.\n */\n readonly remove: (conversation: ConversationSummary) => Promise<void>;\n /**\n * The person goes on with a past conversation: pinned for its key and opened through the backend's resume when the\n * app shows it (`AgentChat` with `keyOf(conversation)`, or `show()` on what this returns).\n */\n readonly continue: (conversation: ConversationSummary) => AgentConversation;\n}\n\n/** The person's past conversations, read as the component mounts, newest activity first. */\nexport function useAgentHistory(\n options: UseAgentHistoryOptions,\n): UseAgentHistory {\n const runtime = useAgentRuntime();\n const { subject, mode, thread, pageSize } = options;\n const scope = options.scope ?? null;\n const agent = options.agent ?? null;\n const history = useMemo<AgentHistory>(\n () =>\n createAgentHistory({\n runtime,\n mode,\n scope,\n agent,\n ...(pageSize === undefined ? {} : { pageSize }),\n }),\n [runtime, mode, scope, agent, pageSize],\n );\n const state = useSyncExternalStore(\n history.subscribe,\n history.getSnapshot,\n history.getSnapshot,\n );\n useEffect(() => {\n if (!state.started) history.refresh();\n }, [history, state.started]);\n const own = options.keyOf;\n const keyOf = useCallback(\n (conversation: ConversationSummary) =>\n own ? own(conversation) : continueKey(conversation, subject, thread),\n [own, subject, thread],\n );\n return {\n state,\n refresh: history.refresh,\n loadMore: history.loadMore,\n retry: history.retry,\n keyOf,\n remove: useCallback(\n (conversation: ConversationSummary) =>\n history.remove(keyOf(conversation), conversation.id),\n [history, keyOf],\n ),\n continue: useCallback(\n (conversation: ConversationSummary) =>\n runtime.continue(keyOf(conversation), conversation.id),\n [runtime, keyOf],\n ),\n };\n}\n\nexport interface UseAgentTranscript {\n readonly state: AgentTranscriptState;\n /** Reads the newest page anew. */\n readonly load: () => void;\n /** The page before the earliest read, prepended. */\n readonly loadEarlier: () => void;\n /** Reads again the page that failed: the newest, or the earlier one. */\n readonly retry: () => void;\n}\n\n/** A past conversation read back, newest page first, as the component mounts. */\nexport function useAgentTranscript(\n conversationId: string,\n options: { readonly pageSize?: number } = {},\n): UseAgentTranscript {\n const runtime = useAgentRuntime();\n const { pageSize } = options;\n const transcript = useMemo<AgentTranscript>(\n () =>\n createAgentTranscript({\n runtime,\n conversationId,\n ...(pageSize === undefined ? {} : { pageSize }),\n }),\n [runtime, conversationId, pageSize],\n );\n const state = useSyncExternalStore(\n transcript.subscribe,\n transcript.getSnapshot,\n transcript.getSnapshot,\n );\n useEffect(() => {\n if (!state.started) transcript.load();\n }, [transcript, state.started]);\n return {\n state,\n load: transcript.load,\n loadEarlier: transcript.loadEarlier,\n retry: transcript.retry,\n };\n}\n"]}
|
package/dist/history.d.cts
CHANGED
|
@@ -14,6 +14,12 @@ interface UseAgentHistoryOptions {
|
|
|
14
14
|
readonly thread?: string;
|
|
15
15
|
/** Conversations per page (1–50). Default 20. */
|
|
16
16
|
readonly pageSize?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The app's key for a past conversation, when its keys are not the server's (`experience_mode` is `customer` or
|
|
19
|
+
* `business`; the app may key its modes in its own words). Continue and delete go under it. Keep it stable.
|
|
20
|
+
* Default: `continueKey(conversation, subject, thread)`.
|
|
21
|
+
*/
|
|
22
|
+
readonly keyOf?: (conversation: ConversationSummary) => AgentKey;
|
|
17
23
|
}
|
|
18
24
|
interface UseAgentHistory {
|
|
19
25
|
readonly state: AgentHistoryState;
|
package/dist/history.d.ts
CHANGED
|
@@ -14,6 +14,12 @@ interface UseAgentHistoryOptions {
|
|
|
14
14
|
readonly thread?: string;
|
|
15
15
|
/** Conversations per page (1–50). Default 20. */
|
|
16
16
|
readonly pageSize?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The app's key for a past conversation, when its keys are not the server's (`experience_mode` is `customer` or
|
|
19
|
+
* `business`; the app may key its modes in its own words). Continue and delete go under it. Keep it stable.
|
|
20
|
+
* Default: `continueKey(conversation, subject, thread)`.
|
|
21
|
+
*/
|
|
22
|
+
readonly keyOf?: (conversation: ConversationSummary) => AgentKey;
|
|
17
23
|
}
|
|
18
24
|
interface UseAgentHistory {
|
|
19
25
|
readonly state: AgentHistoryState;
|
package/dist/history.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useAgentRuntime } from './chunk-
|
|
1
|
+
import { useAgentRuntime } from './chunk-XVAZ57DV.js';
|
|
2
2
|
import { createAgentHistory, continueKey, createAgentTranscript } from '@vinaverse/client/history';
|
|
3
3
|
import { useMemo, useSyncExternalStore, useEffect, useCallback } from 'react';
|
|
4
4
|
|
|
@@ -25,9 +25,10 @@ function useAgentHistory(options) {
|
|
|
25
25
|
useEffect(() => {
|
|
26
26
|
if (!state.started) history.refresh();
|
|
27
27
|
}, [history, state.started]);
|
|
28
|
+
const own = options.keyOf;
|
|
28
29
|
const keyOf = useCallback(
|
|
29
|
-
(conversation) => continueKey(conversation, subject, thread),
|
|
30
|
-
[subject, thread]
|
|
30
|
+
(conversation) => own ? own(conversation) : continueKey(conversation, subject, thread),
|
|
31
|
+
[own, subject, thread]
|
|
31
32
|
);
|
|
32
33
|
return {
|
|
33
34
|
state,
|
package/dist/history.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/history.tsx"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"sources":["../src/history.tsx"],"names":[],"mappings":";;;;AA6DO,SAAS,gBACd,OAAA,EACiB;AACjB,EAAA,MAAM,UAAU,eAAA,EAAgB;AAChC,EAAA,MAAM,EAAE,OAAA,EAAS,IAAA,EAAM,MAAA,EAAQ,UAAS,GAAI,OAAA;AAC5C,EAAA,MAAM,KAAA,GAAQ,QAAQ,KAAA,IAAS,IAAA;AAC/B,EAAA,MAAM,KAAA,GAAQ,QAAQ,KAAA,IAAS,IAAA;AAC/B,EAAA,MAAM,OAAA,GAAU,OAAA;AAAA,IACd,MACE,kBAAA,CAAmB;AAAA,MACjB,OAAA;AAAA,MACA,IAAA;AAAA,MACA,KAAA;AAAA,MACA,KAAA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S,kBAAA,CACd,cAAA,EACA,OAAA,GAA0C,EAAC,EACvB;AACpB,EAAA,MAAM,UAAU,eAAA,EAAgB;AAChC,EAAA,MAAM,EAAE,UAAS,GAAI,OAAA;AACrB,EAAA,MAAM,UAAA,GAAa,OAAA;AAAA,IACjB,MACE,qBAAA,CAAsB;AAAA,MACpB,OAAA;AAAA,MACA,cAAA;AAAA,MACA,GAAI,QAAA,KAAa,MAAA,GAAY,EAAC,GAAI,EAAE,QAAA;AAAS,KAC9C,CAAA;AAAA,IACH,CAAC,OAAA,EAAS,cAAA,EAAgB,QAAQ;AAAA,GACpC;AACA,EAAA,MAAM,KAAA,GAAQ,oBAAA;AAAA,IACZ,UAAA,CAAW,SAAA;AAAA,IACX,UAAA,CAAW,WAAA;AAAA,IACX,UAAA,CAAW;AAAA,GACb;AACA,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,IAAI,CAAC,KAAA,CAAM,OAAA,EAAS,UAAA,CAAW,IAAA,EAAK;AAAA,EACtC,CAAA,EAAG,CAAC,UAAA,EAAY,KAAA,CAAM,OAAO,CAAC,CAAA;AAC9B,EAAA,OAAO;AAAA,IACL,KAAA;AAAA,IACA,MAAM,UAAA,CAAW,IAAA;AAAA,IACjB,aAAa,UAAA,CAAW,WAAA;AAAA,IACxB,OAAO,UAAA,CAAW;AAAA,GACpB;AACF","file":"history.js","sourcesContent":["// React bindings of a person's history (@vinaverse/client/history): the list of their past conversations and one\n// read back, each a store subscribed to while a component is mounted; delete and continue through the runtime. An\n// entry of its own, so an app that draws no history ships none of it. No react-native or DOM import.\nimport type { AgentConversation, AgentKey } from '@vinaverse/client/agent';\nimport {\n type AgentHistory,\n type AgentHistoryState,\n type AgentTranscript,\n type AgentTranscriptState,\n type ConversationSummary,\n continueKey,\n createAgentHistory,\n createAgentTranscript,\n} from '@vinaverse/client/history';\nimport { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react';\nimport { useAgentRuntime } from './agent.js';\n\nexport interface UseAgentHistoryOptions {\n /** The person signed in (null for a guest): the key a conversation continues or is deleted under. */\n readonly subject: string | null;\n /** The app's side: `customer` or `business`. */\n readonly mode: string;\n /** The business acted for, with `business`. */\n readonly scope?: string | null;\n /** The agent, for a backend that serves more than one. */\n readonly agent?: string | null;\n /** The thread a continued conversation runs in; default `chat`. */\n readonly thread?: string;\n /** Conversations per page (1–50). Default 20. */\n readonly pageSize?: number;\n /**\n * The app's key for a past conversation, when its keys are not the server's (`experience_mode` is `customer` or\n * `business`; the app may key its modes in its own words). Continue and delete go under it. Keep it stable.\n * Default: `continueKey(conversation, subject, thread)`.\n */\n readonly keyOf?: (conversation: ConversationSummary) => AgentKey;\n}\n\nexport interface UseAgentHistory {\n readonly state: AgentHistoryState;\n /** Reads the first page anew (after a failure, or to see what changed). */\n readonly refresh: () => void;\n /** The next older page, when there is one and none is being read. */\n readonly loadMore: () => void;\n /** Reads again the page that failed: the first, or the next. */\n readonly retry: () => void;\n /** The key the conversation continues under for this person (`continueKey`). */\n readonly keyOf: (conversation: ConversationSummary) => AgentKey;\n /**\n * Deletes the conversation at the backend and drops its row; the device forgets it only where its key still\n * continues it. One already gone is dropped too; any other failure is thrown, the row kept.\n */\n readonly remove: (conversation: ConversationSummary) => Promise<void>;\n /**\n * The person goes on with a past conversation: pinned for its key and opened through the backend's resume when the\n * app shows it (`AgentChat` with `keyOf(conversation)`, or `show()` on what this returns).\n */\n readonly continue: (conversation: ConversationSummary) => AgentConversation;\n}\n\n/** The person's past conversations, read as the component mounts, newest activity first. */\nexport function useAgentHistory(\n options: UseAgentHistoryOptions,\n): UseAgentHistory {\n const runtime = useAgentRuntime();\n const { subject, mode, thread, pageSize } = options;\n const scope = options.scope ?? null;\n const agent = options.agent ?? null;\n const history = useMemo<AgentHistory>(\n () =>\n createAgentHistory({\n runtime,\n mode,\n scope,\n agent,\n ...(pageSize === undefined ? {} : { pageSize }),\n }),\n [runtime, mode, scope, agent, pageSize],\n );\n const state = useSyncExternalStore(\n history.subscribe,\n history.getSnapshot,\n history.getSnapshot,\n );\n useEffect(() => {\n if (!state.started) history.refresh();\n }, [history, state.started]);\n const own = options.keyOf;\n const keyOf = useCallback(\n (conversation: ConversationSummary) =>\n own ? own(conversation) : continueKey(conversation, subject, thread),\n [own, subject, thread],\n );\n return {\n state,\n refresh: history.refresh,\n loadMore: history.loadMore,\n retry: history.retry,\n keyOf,\n remove: useCallback(\n (conversation: ConversationSummary) =>\n history.remove(keyOf(conversation), conversation.id),\n [history, keyOf],\n ),\n continue: useCallback(\n (conversation: ConversationSummary) =>\n runtime.continue(keyOf(conversation), conversation.id),\n [runtime, keyOf],\n ),\n };\n}\n\nexport interface UseAgentTranscript {\n readonly state: AgentTranscriptState;\n /** Reads the newest page anew. */\n readonly load: () => void;\n /** The page before the earliest read, prepended. */\n readonly loadEarlier: () => void;\n /** Reads again the page that failed: the newest, or the earlier one. */\n readonly retry: () => void;\n}\n\n/** A past conversation read back, newest page first, as the component mounts. */\nexport function useAgentTranscript(\n conversationId: string,\n options: { readonly pageSize?: number } = {},\n): UseAgentTranscript {\n const runtime = useAgentRuntime();\n const { pageSize } = options;\n const transcript = useMemo<AgentTranscript>(\n () =>\n createAgentTranscript({\n runtime,\n conversationId,\n ...(pageSize === undefined ? {} : { pageSize }),\n }),\n [runtime, conversationId, pageSize],\n );\n const state = useSyncExternalStore(\n transcript.subscribe,\n transcript.getSnapshot,\n transcript.getSnapshot,\n );\n useEffect(() => {\n if (!state.started) transcript.load();\n }, [transcript, state.started]);\n return {\n state,\n load: transcript.load,\n loadEarlier: transcript.loadEarlier,\n retry: transcript.retry,\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vinaverse/react",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.13",
|
|
4
4
|
"description": "Vinaverse for React: hooks over the conversation store, and the copy and formatting every Vinaverse React UI shares. No react-native or DOM import.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Vinaverse, Inc.",
|
|
@@ -70,8 +70,8 @@
|
|
|
70
70
|
"dist"
|
|
71
71
|
],
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@vinaverse/client": "0.1.0-alpha.
|
|
74
|
-
"@vinaverse/tokens": "0.1.0-alpha.
|
|
73
|
+
"@vinaverse/client": "0.1.0-alpha.13",
|
|
74
|
+
"@vinaverse/tokens": "0.1.0-alpha.13"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
77
|
"react": ">=19.1"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/agent.tsx"],"names":[],"mappings":";;;;;;AAyBA,IAAM,cAAA,iCAAoE,IAAI,CAAA;AAGvE,SAAS,qBAAqB,KAAA,EAGvB;AACZ,EAAA,uBACE,GAAA,CAAC,eAAe,QAAA,EAAf,EAAwB,OAAO,KAAA,CAAM,OAAA,EACnC,gBAAM,QAAA,EACT,CAAA;AAEJ;AAEO,SAAS,eAAA,GAAgC;AAC9C,EAAA,MAAM,OAAA,GAAU,WAAW,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,GAAS,OAAO,GAAG,CAAA;AACzB,EAAA,MAAA,CAAO,OAAA,GAAU,GAAA;AACjB,EAAA,MAAM,CAAA,GAAI,SAAS,GAAG,CAAA;AAEtB,EAAA,MAAM,IAAA,GAAO,WAAA,CAAY,MAAM,OAAA,CAAQ,IAAA,CAAK,MAAA,CAAO,OAAO,CAAA,EAAG,CAAC,OAAA,EAAS,CAAC,CAAC,CAAA;AACzE,EAAA,MAAM,YAAA,GAAe,oBAAA,CAAqB,OAAA,CA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AK,IAAA,EAAM,IAAA,EAAM,CAAA;AAC9B,IAAA,KAAK,OAAA,CAAQ,OAAA,CAAQ,OAAA,CAAQ,OAAA,CAAQ,GAAA,EAAK,GAAG,CAAC,CAAA,CAAE,KAAA,CAAM,MAAM,MAAS,CAAA;AAAA,EACvE,CAAA,EAAG,CAAC,OAAA,EAAS,GAAG,CAAC,CAAA;AACjB,EAAA,MAAM,IAAA,GAAO,YAAY,MAAM;AAC7B,IAAA,IAAA,EAAK;AACL,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,GAAU,YAAY,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,EAAA,SAAA,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":"chunk-V4UBOVTK.js","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"]}
|