@vinaverse/react 0.1.0-alpha.7 → 0.1.0-alpha.9
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 +12 -0
- package/dist/agent.cjs.map +1 -1
- package/dist/agent.d.cts +7 -2
- package/dist/agent.d.ts +7 -2
- package/dist/agent.js +12 -1
- package/dist/agent.js.map +1 -1
- package/dist/index.cjs +4 -0
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +11 -3
package/dist/agent.cjs
CHANGED
|
@@ -64,6 +64,17 @@ function useAgentStatus() {
|
|
|
64
64
|
runtime.getStatus
|
|
65
65
|
);
|
|
66
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
|
+
}
|
|
67
78
|
function useAgentName(fallback) {
|
|
68
79
|
const runtime = useAgentRuntime();
|
|
69
80
|
return react.useSyncExternalStore(runtime.subscribe, runtime.getName, runtime.getName) ?? fallback;
|
|
@@ -158,5 +169,6 @@ exports.useAgentName = useAgentName;
|
|
|
158
169
|
exports.useAgentProfile = useAgentProfile;
|
|
159
170
|
exports.useAgentRuntime = useAgentRuntime;
|
|
160
171
|
exports.useAgentStatus = useAgentStatus;
|
|
172
|
+
exports.useAgentStatusState = useAgentStatusState;
|
|
161
173
|
//# sourceMappingURL=agent.cjs.map
|
|
162
174
|
//# sourceMappingURL=agent.cjs.map
|
package/dist/agent.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/agent.tsx"],"names":["jsx","useContext","useRef","agentKey","use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,EAAAG,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,OAAO,OAAA,CAAQ,IAAA,CAAK,EAAE,IAAA,EAAM,kBAAkB,CAAA;AAAA,EACpD,CAAA,EAAG,CAAC,KAAA,EAAO,OAAO,CAAC,CAAA;AACnB,EAAA,OAAO,EAAE,KAAA,EAAO,MAAA,EAAQ,WAAW,GAAA,KAAQ,GAAA,EAAK,MAAM,OAAA,EAAQ;AAChE","file":"agent.cjs","sourcesContent":["// React bindings of the agent runtime (@vinaverse/client/agent): a provider and hooks, thin by design. The runtime holds\n// every rule; these subscribe, show a conversation while a component is mounted, and read the launcher's state. No\n// react-native or DOM import.\nimport type {\n AgentConversation,\n AgentConversationState,\n AgentKey,\n AgentPlace,\n AgentRuntime,\n AgentStatus,\n} 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/** 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":["../src/agent.tsx"],"names":["jsx","useContext","useRef","agentKey","useCallback","useSyncExternalStore","useLayoutEffect","useEffect","greetedKey","useState"],"mappings":";;;;;;;AAyBA,IAAM,cAAA,uCAAoE,IAAI,CAAA;AAGvE,SAAS,qBAAqB,KAAA,EAGvB;AACZ,EAAA,uBACEA,cAAA,CAAC,eAAe,QAAA,EAAf,EAAwB,OAAO,KAAA,CAAM,OAAA,EACnC,gBAAM,QAAA,EACT,CAAA;AAEJ;AAEO,SAAS,eAAA,GAAgC;AAC9C,EAAA,MAAM,OAAA,GAAUC,iBAAW,cAAc,CAAA;AACzC,EAAA,IAAI,CAAC,OAAA;AACH,IAAA,MAAM,IAAI,MAAM,mDAAmD,CAAA;AACrE,EAAA,OAAO,OAAA;AACT;AAaO,SAAS,oBAAA,CACd,GAAA,EACA,IAAA,GAGI,EAAC,EACiB;AACtB,EAAA,MAAM,UAAU,eAAA,EAAgB;AAChC,EAAA,MAAM,MAAA,GAASC,aAAO,GAAG,CAAA;AACzB,EAAA,MAAA,CAAO,OAAA,GAAU,GAAA;AACjB,EAAA,MAAM,CAAA,GAAIC,eAAS,GAAG,CAAA;AAEtB,EAAA,MAAM,IAAA,GAAOC,iBAAA,CAAY,MAAM,OAAA,CAAQ,IAAA,CAAK,MAAA,CAAO,OAAO,CAAA,EAAG,CAAC,OAAA,EAAS,CAAC,CAAC,CAAA;AACzE,EAAA,MAAM,YAAA,GAAeC,0BAAA,CAAqB,OAAA,CAAQ,SAAA,EAAW,MAAM,IAAI,CAAA;AACvE,EAAA,MAAM,OAAA,GAAUH,aAAO,IAAI,CAAA;AAC3B,EAAA,OAAA,CAAQ,OAAA,GAAU,IAAA;AAGlB,EAAAI,qBAAA,CAAgB,MAAM,aAAa,IAAA,CAAK,OAAA,CAAQ,OAAO,CAAA,EAAG,CAAC,YAAY,CAAC,CAAA;AACxE,EAAA,MAAM,EAAE,KAAA,EAAO,UAAA,EAAW,GAAI,IAAA;AAC9B,EAAA,MAAM,SAAS,KAAA,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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,GAAUA,kBAAY,MAAM;AAChC,IAAA,IAAA,EAAK;AACL,IAAA,OAAA,CAAQ,IAAA,CAAK,EAAE,IAAA,EAAM,iBAAA,EAAmB,CAAA;AAAA,EAC1C,CAAA,EAAG,CAAC,IAAA,EAAM,OAAO,CAAC,CAAA;AAClB,EAAAG,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,OAAO,OAAA,CAAQ,IAAA,CAAK,EAAE,IAAA,EAAM,kBAAkB,CAAA;AAAA,EACpD,CAAA,EAAG,CAAC,KAAA,EAAO,OAAO,CAAC,CAAA;AACnB,EAAA,OAAO,EAAE,KAAA,EAAO,MAAA,EAAQ,WAAW,GAAA,KAAQ,GAAA,EAAK,MAAM,OAAA,EAAQ;AAChE","file":"agent.cjs","sourcesContent":["// React bindings of the agent runtime (@vinaverse/client/agent): a provider and hooks, thin by design. The runtime holds\n// every rule; these subscribe, show a conversation while a component is mounted, and read the launcher's state. No\n// react-native or DOM import.\nimport type {\n AgentConversation,\n AgentConversationState,\n AgentKey,\n AgentPlace,\n AgentRuntime,\n AgentStatus,\n AgentStatusState,\n} from '@vinaverse/client/agent';\nimport { agentKey, greetedKey } from '@vinaverse/client/agent';\nimport {\n createContext,\n type ReactNode,\n useCallback,\n useContext,\n useEffect,\n useLayoutEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\n\nconst RuntimeContext = /* @__PURE__ */ createContext<AgentRuntime | null>(null);\n\n/** The app's agent runtime, made once (module level, or memoized) and given to everything below. */\nexport function AgentRuntimeProvider(props: {\n readonly runtime: AgentRuntime;\n readonly children?: ReactNode;\n}): ReactNode {\n return (\n <RuntimeContext.Provider value={props.runtime}>\n {props.children}\n </RuntimeContext.Provider>\n );\n}\n\nexport function useAgentRuntime(): AgentRuntime {\n const runtime = useContext(RuntimeContext);\n if (!runtime)\n throw new Error('Agent hooks render inside <AgentRuntimeProvider>.');\n return runtime;\n}\n\nexport interface UseAgentConversation {\n readonly conversation: AgentConversation;\n readonly state: AgentConversationState;\n /** A new conversation for this key (the old one let go and forgotten). */\n readonly startNew: () => void;\n}\n\n/**\n * The key's conversation, shown while the component is mounted (kept across screens; released when it unmounts), told\n * the place and the way in as they change. A start over or its end hands the component the key's next conversation.\n */\nexport function useAgentConversation(\n key: AgentKey,\n open: {\n readonly place?: AgentPlace | null;\n readonly entryPoint?: string | null;\n } = {},\n): UseAgentConversation {\n const runtime = useAgentRuntime();\n const keyRef = useRef(key);\n keyRef.current = key;\n const k = agentKey(key);\n // biome-ignore lint/correctness/useExhaustiveDependencies: the key's string reads it again when the key changes\n const read = useCallback(() => runtime.peek(keyRef.current), [runtime, k]);\n const conversation = useSyncExternalStore(runtime.subscribe, read, read);\n const openRef = useRef(open);\n openRef.current = open;\n // Layout effects: they run before any passive effect, a child's included, so a chat below that starts the\n // conversation early (`VinaverseChat`) finds it shown and told the place.\n useLayoutEffect(() => conversation.show(openRef.current), [conversation]);\n const { place, entryPoint } = open;\n const screen = place?.screen;\n const entityType = place?.entity?.type;\n const entityId = place?.entity?.id;\n const filters = place?.filters ? JSON.stringify(place.filters) : null;\n const cleared = place === null;\n // biome-ignore lint/correctness/useExhaustiveDependencies: told by value, so a place made again each render is the same\n useLayoutEffect(() => {\n conversation.tell(openRef.current);\n }, [\n conversation,\n screen,\n entityType,\n entityId,\n filters,\n cleared,\n entryPoint,\n ]);\n const state = useSyncExternalStore(\n conversation.subscribe,\n conversation.getState,\n conversation.getState,\n );\n const startNew = useCallback(\n () => runtime.startNew(keyRef.current),\n [runtime],\n );\n return { conversation, state, startNew };\n}\n\n/** The agent's status (read once, then at most every few minutes); null until the first read. */\nexport function useAgentStatus(): AgentStatus | null {\n const runtime = useAgentRuntime();\n useEffect(() => {\n runtime.status().catch(() => undefined); // an unread status is no agent: the launcher stays hidden\n }, [runtime]);\n return useSyncExternalStore(\n runtime.subscribe,\n runtime.getStatus,\n runtime.getStatus,\n );\n}\n\n/**\n * The status with where its read stands (`{ status, reading, failed }`): a screen tells \"still reading\" from \"could\n * not read\" and draws each its own way. Reads the status, as `useAgentStatus` does.\n */\nexport function useAgentStatusState(): AgentStatusState {\n const runtime = useAgentRuntime();\n useEffect(() => {\n runtime.status().catch(() => undefined); // said through `failed`\n }, [runtime]);\n return useSyncExternalStore(\n runtime.subscribe,\n runtime.getStatusState,\n runtime.getStatusState,\n );\n}\n\n/** The agent's name: the newest session's, else the status's, else `fallback`. */\nexport function useAgentName(fallback: string): string {\n const runtime = useAgentRuntime();\n return (\n useSyncExternalStore(runtime.subscribe, runtime.getName, runtime.getName) ??\n fallback\n );\n}\n\n/** What the agent can do, from the status (all off until it is read, and when the agent is off). */\nexport function useAgentFeatures(): AgentStatus['features'] & {\n readonly available: boolean;\n readonly launcher: boolean;\n} {\n const status = useAgentStatus();\n return {\n available: status?.available === true,\n launcher: status?.launcher === true,\n history: status?.features.history === true,\n memory: status?.features.memory === true,\n businessAgent: status?.features.businessAgent === true,\n businessSetup: status?.features.businessSetup === true,\n };\n}\n\n/** The agent as an entry shows it before any session: its name (`fallback` until known) and avatar. */\nexport function useAgentProfile(fallback: string): {\n readonly name: string;\n readonly avatarUrl: string | null;\n} {\n const status = useAgentStatus();\n return { name: useAgentName(fallback), avatarUrl: status?.avatarUrl ?? null };\n}\n\n/**\n * Where the launcher remembers that it greeted a person in a mode (sync or async; the app's preferences store). A\n * sign-out keeps it on purpose (a person is not greeted again after signing back in). Given to the runtime as\n * `greetingStorage`, `runtime.erase()` erases it; `forgetGreeting` is the low-level helper for a storage of the app's.\n */\nexport interface GreetingStorage {\n getItem(key: string): string | null | Promise<string | null>;\n setItem(key: string, value: string): void | Promise<void>;\n /** Erasure removes the record, never leaves a key that names the person. */\n removeItem(key: string): void | Promise<void>;\n}\n\n/** Where the greeting is remembered, per person and mode: the default the storage sees, unless the app maps its own. */\nexport { greetedKey };\n\n/** Removes the launcher's record of greeting this person in these modes (`runtime.erase()` does it for `greetingStorage`). */\nexport async function forgetGreeting(\n storage: GreetingStorage,\n subject: string | null,\n modes: readonly string[],\n storageKey: (subject: string | null, mode: string) => string = greetedKey,\n): Promise<void> {\n for (const mode of modes) await storage.removeItem(storageKey(subject, mode));\n}\n\nconst MEMORY = new Map<string, string>();\nconst memory: GreetingStorage = {\n getItem: (k) => MEMORY.get(k) ?? null,\n setItem: (k, v) => void MEMORY.set(k, v),\n removeItem: (k) => void MEMORY.delete(k),\n};\n\nexport interface UseAgentLauncher {\n /** The status offers the launcher, and the app shows it here. */\n readonly shows: boolean;\n /** Its greeting bubble: once per person and mode, a moment after the launcher shows. */\n readonly greets: boolean;\n /** The person opens the agent (the greeting is done too). */\n readonly open: () => void;\n /** The person closes the greeting. */\n readonly dismiss: () => void;\n}\n\n/** The floating launcher's state: whether it shows and greets, and its two actions. */\nexport function useAgentLauncher(options: {\n readonly subject: string | null;\n readonly mode: string;\n /** The app's own rule (the tabs it shows on, no keyboard up…); default true. */\n readonly visible?: boolean;\n readonly storage?: GreetingStorage;\n /** The storage's key for a person and mode, to reuse a record the app already keeps. Default `greetedKey`. */\n readonly storageKey?: (subject: string | null, mode: string) => string;\n /** How long after showing the greeting appears. Default 1200 ms. */\n readonly delayMs?: number;\n readonly onOpen: () => void;\n}): UseAgentLauncher {\n const runtime = useAgentRuntime();\n const status = useAgentStatus();\n const { subject, mode, onOpen } = options;\n // The app's own store, else the runtime's (`greetingStorage`: fenced by a sign-out, reached by `erase()`).\n const storage = options.storage ?? runtime.greetings ?? memory;\n const shows = status?.launcher === true && options.visible !== false;\n const key = (options.storageKey ?? greetedKey)(subject, mode);\n const [greeted, setGreeted] = useState<{ key: string; done: boolean } | null>(\n null,\n );\n const [due, setDue] = useState<string | null>(null);\n useEffect(() => {\n let live = true;\n Promise.resolve(storage.getItem(key)).then(\n (value) => {\n if (live) setGreeted({ key, done: value === '1' });\n },\n () => {\n if (live) setGreeted({ key, done: false });\n },\n );\n return () => {\n live = false;\n };\n }, [storage, key]);\n const pending = shows && greeted?.key === key && !greeted.done;\n useEffect(() => {\n if (!pending) return;\n const timer = setTimeout(() => {\n setDue(key);\n runtime.emit({ type: 'launcher_greeted' });\n }, options.delayMs ?? 1200);\n return () => clearTimeout(timer);\n }, [pending, key, runtime, options.delayMs]);\n const done = useCallback(() => {\n setGreeted({ key, done: true });\n void Promise.resolve(storage.setItem(key, '1')).catch(() => undefined);\n }, [storage, key]);\n const open = useCallback(() => {\n done();\n runtime.emit({ type: 'launcher_opened' });\n onOpen();\n }, [done, runtime, onOpen]);\n const dismiss = useCallback(() => {\n done();\n runtime.emit({ type: 'greeting_closed' });\n }, [done, runtime]);\n useEffect(() => {\n if (shows) runtime.emit({ type: 'launcher_shown' });\n }, [shows, runtime]);\n return { shows, greets: pending && due === key, open, dismiss };\n}\n"]}
|
package/dist/agent.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentPlace, AgentStatus } from '@vinaverse/client/agent';
|
|
1
|
+
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentPlace, AgentStatus, AgentStatusState } from '@vinaverse/client/agent';
|
|
2
2
|
export { greetedKey } from '@vinaverse/client/agent';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
|
|
@@ -24,6 +24,11 @@ declare function useAgentConversation(key: AgentKey, open?: {
|
|
|
24
24
|
}): UseAgentConversation;
|
|
25
25
|
/** The agent's status (read once, then at most every few minutes); null until the first read. */
|
|
26
26
|
declare function useAgentStatus(): AgentStatus | null;
|
|
27
|
+
/**
|
|
28
|
+
* The status with where its read stands (`{ status, reading, failed }`): a screen tells "still reading" from "could
|
|
29
|
+
* not read" and draws each its own way. Reads the status, as `useAgentStatus` does.
|
|
30
|
+
*/
|
|
31
|
+
declare function useAgentStatusState(): AgentStatusState;
|
|
27
32
|
/** The agent's name: the newest session's, else the status's, else `fallback`. */
|
|
28
33
|
declare function useAgentName(fallback: string): string;
|
|
29
34
|
/** What the agent can do, from the status (all off until it is read, and when the agent is off). */
|
|
@@ -74,4 +79,4 @@ declare function useAgentLauncher(options: {
|
|
|
74
79
|
readonly onOpen: () => void;
|
|
75
80
|
}): UseAgentLauncher;
|
|
76
81
|
|
|
77
|
-
export { AgentRuntimeProvider, type GreetingStorage, type UseAgentConversation, type UseAgentLauncher, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus };
|
|
82
|
+
export { AgentRuntimeProvider, type GreetingStorage, type UseAgentConversation, type UseAgentLauncher, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState };
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentPlace, AgentStatus } from '@vinaverse/client/agent';
|
|
1
|
+
import { AgentRuntime, AgentConversation, AgentConversationState, AgentKey, AgentPlace, AgentStatus, AgentStatusState } from '@vinaverse/client/agent';
|
|
2
2
|
export { greetedKey } from '@vinaverse/client/agent';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
|
|
@@ -24,6 +24,11 @@ declare function useAgentConversation(key: AgentKey, open?: {
|
|
|
24
24
|
}): UseAgentConversation;
|
|
25
25
|
/** The agent's status (read once, then at most every few minutes); null until the first read. */
|
|
26
26
|
declare function useAgentStatus(): AgentStatus | null;
|
|
27
|
+
/**
|
|
28
|
+
* The status with where its read stands (`{ status, reading, failed }`): a screen tells "still reading" from "could
|
|
29
|
+
* not read" and draws each its own way. Reads the status, as `useAgentStatus` does.
|
|
30
|
+
*/
|
|
31
|
+
declare function useAgentStatusState(): AgentStatusState;
|
|
27
32
|
/** The agent's name: the newest session's, else the status's, else `fallback`. */
|
|
28
33
|
declare function useAgentName(fallback: string): string;
|
|
29
34
|
/** What the agent can do, from the status (all off until it is read, and when the agent is off). */
|
|
@@ -74,4 +79,4 @@ declare function useAgentLauncher(options: {
|
|
|
74
79
|
readonly onOpen: () => void;
|
|
75
80
|
}): UseAgentLauncher;
|
|
76
81
|
|
|
77
|
-
export { AgentRuntimeProvider, type GreetingStorage, type UseAgentConversation, type UseAgentLauncher, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus };
|
|
82
|
+
export { AgentRuntimeProvider, type GreetingStorage, type UseAgentConversation, type UseAgentLauncher, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState };
|
package/dist/agent.js
CHANGED
|
@@ -63,6 +63,17 @@ function useAgentStatus() {
|
|
|
63
63
|
runtime.getStatus
|
|
64
64
|
);
|
|
65
65
|
}
|
|
66
|
+
function useAgentStatusState() {
|
|
67
|
+
const runtime = useAgentRuntime();
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
runtime.status().catch(() => void 0);
|
|
70
|
+
}, [runtime]);
|
|
71
|
+
return useSyncExternalStore(
|
|
72
|
+
runtime.subscribe,
|
|
73
|
+
runtime.getStatusState,
|
|
74
|
+
runtime.getStatusState
|
|
75
|
+
);
|
|
76
|
+
}
|
|
66
77
|
function useAgentName(fallback) {
|
|
67
78
|
const runtime = useAgentRuntime();
|
|
68
79
|
return useSyncExternalStore(runtime.subscribe, runtime.getName, runtime.getName) ?? fallback;
|
|
@@ -144,6 +155,6 @@ function useAgentLauncher(options) {
|
|
|
144
155
|
return { shows, greets: pending && due === key, open, dismiss };
|
|
145
156
|
}
|
|
146
157
|
|
|
147
|
-
export { AgentRuntimeProvider, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus };
|
|
158
|
+
export { AgentRuntimeProvider, forgetGreeting, useAgentConversation, useAgentFeatures, useAgentLauncher, useAgentName, useAgentProfile, useAgentRuntime, useAgentStatus, useAgentStatusState };
|
|
148
159
|
//# sourceMappingURL=agent.js.map
|
|
149
160
|
//# sourceMappingURL=agent.js.map
|
package/dist/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/agent.tsx"],"names":[],"mappings":";;;;;;AAwBA,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,CAAQ,SAAA,EAAW,MAAM,IAAI,CAAA;AACvE,EAAA,MAAM,OAAA,GAAU,OAAO,IAAI,CAAA;AAC3B,EAAA,OAAA,CAAQ,OAAA,GAAU,I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,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":"agent.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} 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/** 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":["../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,CAAQ,SAAA,EAAW,MAAM,IAAI,CAAA;AACvE,EAAA,MAAM,OAAA,GAAU,OAAO,IAAI,CAAA;AAC3B,EAAA,OAAA,CAAQ,OAAA,GAAU,IAAA;AAGlB,EAAA,eAAA,CAAgB,MAAM,aAAa,IAAA,CAAK,OAAA,CAAQ,O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AG,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":"agent.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"]}
|
package/dist/index.cjs
CHANGED
|
@@ -88,6 +88,10 @@ Object.defineProperty(exports, "formatDate", {
|
|
|
88
88
|
enumerable: true,
|
|
89
89
|
get: function () { return copy.formatDate; }
|
|
90
90
|
});
|
|
91
|
+
Object.defineProperty(exports, "formatDayTime", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
get: function () { return copy.formatDayTime; }
|
|
94
|
+
});
|
|
91
95
|
Object.defineProperty(exports, "formatMoney", {
|
|
92
96
|
enumerable: true,
|
|
93
97
|
get: function () { return copy.formatMoney; }
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Translate, CopyOverrides } from '@vinaverse/tokens/copy';
|
|
2
|
-
export { AI_MARK, BANNED_WORDS, COPY, Copy, CopyKey, CopyOverrides, Translate, bannedWordsIn, builtInCopy, createTranslate, formatDate, formatMoney, formatTimeRange, textDirection } from '@vinaverse/tokens/copy';
|
|
2
|
+
export { AI_MARK, BANNED_WORDS, COPY, Copy, CopyKey, CopyOverrides, Translate, bannedWordsIn, builtInCopy, createTranslate, formatDate, formatDayTime, formatMoney, formatTimeRange, textDirection } from '@vinaverse/tokens/copy';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
import { ConversationView, StreamingText } from '@vinaverse/client/conversation';
|
|
5
5
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Translate, CopyOverrides } from '@vinaverse/tokens/copy';
|
|
2
|
-
export { AI_MARK, BANNED_WORDS, COPY, Copy, CopyKey, CopyOverrides, Translate, bannedWordsIn, builtInCopy, createTranslate, formatDate, formatMoney, formatTimeRange, textDirection } from '@vinaverse/tokens/copy';
|
|
2
|
+
export { AI_MARK, BANNED_WORDS, COPY, Copy, CopyKey, CopyOverrides, Translate, bannedWordsIn, builtInCopy, createTranslate, formatDate, formatDayTime, formatMoney, formatTimeRange, textDirection } from '@vinaverse/tokens/copy';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
import { ConversationView, StreamingText } from '@vinaverse/client/conversation';
|
|
5
5
|
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createTranslate, textDirection } from '@vinaverse/tokens/copy';
|
|
2
|
-
export { AI_MARK, BANNED_WORDS, COPY, bannedWordsIn, builtInCopy, createTranslate, formatDate, formatMoney, formatTimeRange, textDirection } from '@vinaverse/tokens/copy';
|
|
2
|
+
export { AI_MARK, BANNED_WORDS, COPY, bannedWordsIn, builtInCopy, createTranslate, formatDate, formatDayTime, formatMoney, formatTimeRange, textDirection } from '@vinaverse/tokens/copy';
|
|
3
3
|
import { useMemo, useContext, useSyncExternalStore, useRef, useCallback, createContext } from 'react';
|
|
4
4
|
import { jsx } from 'react/jsx-runtime';
|
|
5
5
|
|
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.9",
|
|
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.",
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
"sideEffects": false,
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
|
+
"react-native": {
|
|
24
|
+
"types": "./dist/index.d.cts",
|
|
25
|
+
"default": "./dist/index.cjs"
|
|
26
|
+
},
|
|
23
27
|
"import": {
|
|
24
28
|
"types": "./dist/index.d.ts",
|
|
25
29
|
"default": "./dist/index.js"
|
|
@@ -30,6 +34,10 @@
|
|
|
30
34
|
}
|
|
31
35
|
},
|
|
32
36
|
"./agent": {
|
|
37
|
+
"react-native": {
|
|
38
|
+
"types": "./dist/agent.d.cts",
|
|
39
|
+
"default": "./dist/agent.cjs"
|
|
40
|
+
},
|
|
33
41
|
"import": {
|
|
34
42
|
"types": "./dist/agent.d.ts",
|
|
35
43
|
"default": "./dist/agent.js"
|
|
@@ -48,8 +56,8 @@
|
|
|
48
56
|
"dist"
|
|
49
57
|
],
|
|
50
58
|
"dependencies": {
|
|
51
|
-
"@vinaverse/client": "0.1.0-alpha.
|
|
52
|
-
"@vinaverse/tokens": "0.1.0-alpha.
|
|
59
|
+
"@vinaverse/client": "0.1.0-alpha.9",
|
|
60
|
+
"@vinaverse/tokens": "0.1.0-alpha.9"
|
|
53
61
|
},
|
|
54
62
|
"peerDependencies": {
|
|
55
63
|
"react": ">=19.1"
|