@vercel/geistdocs 2.7.10 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/dist/assets/icons/icon-fullscreen-close.d.ts +26 -0
  2. package/dist/assets/icons/icon-fullscreen-close.d.ts.map +1 -0
  3. package/dist/assets/icons/icon-fullscreen-close.js +25 -0
  4. package/dist/assets/icons/icon-fullscreen.d.ts +26 -0
  5. package/dist/assets/icons/icon-fullscreen.d.ts.map +1 -0
  6. package/dist/assets/icons/icon-fullscreen.js +25 -0
  7. package/dist/assets/icons/icon-thumb-down.d.ts +26 -0
  8. package/dist/assets/icons/icon-thumb-down.d.ts.map +1 -0
  9. package/dist/assets/icons/icon-thumb-down.js +25 -0
  10. package/dist/assets/icons/index.d.ts +3 -0
  11. package/dist/assets/icons/index.d.ts.map +1 -1
  12. package/dist/assets/icons/index.js +3 -0
  13. package/dist/cli/index.js +1 -1
  14. package/dist/config.d.ts +67 -3
  15. package/dist/config.d.ts.map +1 -1
  16. package/dist/controls.d.ts +23 -1
  17. package/dist/controls.d.ts.map +1 -1
  18. package/dist/controls.js +44 -3
  19. package/dist/footer.js +1 -1
  20. package/dist/internal/chat/activity.d.ts +5 -1
  21. package/dist/internal/chat/activity.d.ts.map +1 -1
  22. package/dist/internal/chat/activity.js +11 -4
  23. package/dist/internal/chat/chat-user.d.ts +12 -0
  24. package/dist/internal/chat/chat-user.d.ts.map +1 -0
  25. package/dist/internal/chat/chat-user.js +27 -0
  26. package/dist/internal/chat/commands.d.ts +91 -0
  27. package/dist/internal/chat/commands.d.ts.map +1 -0
  28. package/dist/internal/chat/commands.js +165 -0
  29. package/dist/internal/chat/context.d.ts +49 -2
  30. package/dist/internal/chat/context.d.ts.map +1 -1
  31. package/dist/internal/chat/context.js +57 -4
  32. package/dist/internal/chat/message-actions.d.ts +15 -0
  33. package/dist/internal/chat/message-actions.d.ts.map +1 -0
  34. package/dist/internal/chat/message-actions.js +113 -0
  35. package/dist/internal/chat/message-feedback.d.ts +68 -0
  36. package/dist/internal/chat/message-feedback.d.ts.map +1 -0
  37. package/dist/internal/chat/message-feedback.js +188 -0
  38. package/dist/internal/chat/message-metadata.d.ts.map +1 -1
  39. package/dist/internal/chat/message-metadata.js +7 -1
  40. package/dist/internal/chat/message-text.d.ts +9 -0
  41. package/dist/internal/chat/message-text.d.ts.map +1 -0
  42. package/dist/internal/chat/message-text.js +15 -0
  43. package/dist/internal/chat/message.d.ts +4 -2
  44. package/dist/internal/chat/message.d.ts.map +1 -1
  45. package/dist/internal/chat/message.js +8 -1
  46. package/dist/internal/chat/options.d.ts +94 -0
  47. package/dist/internal/chat/options.d.ts.map +1 -0
  48. package/dist/internal/chat/options.js +171 -0
  49. package/dist/internal/chat/page-attachment.d.ts +12 -0
  50. package/dist/internal/chat/page-attachment.d.ts.map +1 -0
  51. package/dist/internal/chat/page-attachment.js +18 -0
  52. package/dist/internal/chat/session-provider.d.ts +33 -0
  53. package/dist/internal/chat/session-provider.d.ts.map +1 -0
  54. package/dist/internal/chat/session-provider.js +138 -0
  55. package/dist/internal/chat/session.d.ts +60 -0
  56. package/dist/internal/chat/session.d.ts.map +1 -0
  57. package/dist/internal/chat/session.js +85 -0
  58. package/dist/internal/chat/shell.d.ts.map +1 -1
  59. package/dist/internal/chat/shell.js +86 -157
  60. package/dist/internal/chat/use-chat-commands.d.ts +28 -0
  61. package/dist/internal/chat/use-chat-commands.d.ts.map +1 -0
  62. package/dist/internal/chat/use-chat-commands.js +62 -0
  63. package/dist/internal/chat/user-avatar.d.ts +7 -0
  64. package/dist/internal/chat/user-avatar.d.ts.map +1 -0
  65. package/dist/internal/chat/user-avatar.js +27 -0
  66. package/dist/internal/eve-agent-session-cookie.d.ts +68 -0
  67. package/dist/internal/eve-agent-session-cookie.d.ts.map +1 -0
  68. package/dist/internal/eve-agent-session-cookie.js +164 -0
  69. package/dist/internal/eve-agent-session.d.ts +9 -7
  70. package/dist/internal/eve-agent-session.d.ts.map +1 -1
  71. package/dist/internal/eve-agent-session.js +10 -13
  72. package/dist/internal/eve-agent.d.ts +96 -24
  73. package/dist/internal/eve-agent.d.ts.map +1 -1
  74. package/dist/internal/eve-agent.js +74 -51
  75. package/dist/internal/feedback-action.d.ts +14 -0
  76. package/dist/internal/feedback-action.d.ts.map +1 -1
  77. package/dist/internal/feedback-action.js +37 -5
  78. package/dist/internal/mdx-to-markdown.d.ts.map +1 -1
  79. package/dist/internal/mdx-to-markdown.js +226 -19
  80. package/dist/internal/page-actions-client.d.ts +9 -0
  81. package/dist/internal/page-actions-client.d.ts.map +1 -1
  82. package/dist/internal/page-actions-client.js +8 -4
  83. package/dist/internal/provider-client.d.ts.map +1 -1
  84. package/dist/internal/provider-client.js +10 -4
  85. package/dist/internal/use-mobile.d.ts +7 -0
  86. package/dist/internal/use-mobile.d.ts.map +1 -1
  87. package/dist/internal/use-mobile.js +17 -1
  88. package/dist/internal/webmcp-client.d.ts.map +1 -1
  89. package/dist/internal/webmcp-client.js +13 -3
  90. package/dist/internal/webmcp.d.ts +5 -1
  91. package/dist/internal/webmcp.d.ts.map +1 -1
  92. package/dist/internal/webmcp.js +31 -1
  93. package/dist/pages/changelog.d.ts.map +1 -1
  94. package/dist/pages/changelog.js +4 -0
  95. package/dist/pages/docs.d.ts.map +1 -1
  96. package/dist/pages/docs.js +2 -0
  97. package/dist/routes/chat.d.ts +83 -5
  98. package/dist/routes/chat.d.ts.map +1 -1
  99. package/dist/routes/chat.js +517 -80
  100. package/dist/routes/discovery.d.ts +1 -1
  101. package/dist/routes/discovery.d.ts.map +1 -1
  102. package/dist/routes/discovery.js +7 -11
  103. package/dist/routes/openapi.d.ts.map +1 -1
  104. package/dist/routes/openapi.js +47 -25
  105. package/docs/agents.md +3 -3
  106. package/docs/llms.txt +634 -37
  107. package/docs/manifest.json +3 -3
  108. package/docs/pages/agent-readiness.md +1 -1
  109. package/docs/pages/ask-ai.md +570 -26
  110. package/docs/pages/configuration.md +21 -1
  111. package/docs/pages/env.md +17 -3
  112. package/docs/pages/feedback.md +4 -0
  113. package/docs/pages/webmcp.md +18 -3
  114. package/docs/sitemap.md +1 -1
  115. package/package.json +1 -2
  116. package/template/.env.example +5 -0
  117. package/template/AGENTS.md +2 -0
  118. package/template/content/docs/agent-readiness.mdx +1 -1
  119. package/template/content/docs/ask-ai.cn.mdx +466 -15
  120. package/template/content/docs/ask-ai.mdx +570 -26
  121. package/template/content/docs/configuration.cn.mdx +39 -0
  122. package/template/content/docs/configuration.mdx +21 -1
  123. package/template/content/docs/env.cn.mdx +15 -1
  124. package/template/content/docs/env.mdx +18 -4
  125. package/template/content/docs/feedback.cn.mdx +4 -0
  126. package/template/content/docs/feedback.mdx +4 -0
  127. package/template/content/docs/webmcp.mdx +18 -3
  128. package/template/package.json +1 -2
@@ -0,0 +1,18 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { IconCross, IconFileText } from "#geistdocs-assets/icons";
4
+ import { cn } from "#geistdocs-ui/utils";
5
+ import { getChatPageLabel, getMessagePageAttachment, } from "./commands";
6
+ /** Removable card above the prompt input naming the page the next message carries. */
7
+ export const ChatPageAttachmentCard = ({ onRemove, page, }) => {
8
+ const label = getChatPageLabel(page);
9
+ return (_jsxs("div", { className: "flex min-w-0 items-center gap-2 rounded-md border border-gray-alpha-400 bg-background-200 py-1.5 pr-1 pl-2.5 text-gray-1000 text-label-13", "data-geistdocs-chat-page-attachment": true, children: [_jsx(IconFileText, { className: "shrink-0 text-gray-900", size: 14 }), _jsxs("span", { className: "min-w-0 flex-1 truncate", title: page.url, children: [_jsx("span", { className: "sr-only", children: "Asking about " }), label] }), _jsx("button", { "aria-label": `Remove ${label}`, className: "flex size-6 shrink-0 cursor-pointer items-center justify-center rounded text-gray-900 hover:bg-gray-alpha-100 hover:text-gray-1000 focus-visible:shadow-focus-ring focus-visible:outline-none", onClick: onRemove, type: "button", children: _jsx(IconCross, { size: 12 }) })] }));
10
+ };
11
+ /** Chip on a sent user message naming the page it was asked about. */
12
+ export const ChatMessagePageChip = ({ className, metadata, }) => {
13
+ const page = getMessagePageAttachment(metadata);
14
+ if (!page) {
15
+ return null;
16
+ }
17
+ return (_jsxs("span", { className: cn("inline-flex max-w-[80%] items-center gap-1 rounded-full border border-gray-alpha-400 px-2 py-0.5 text-gray-900 text-label-12", className), "data-geistdocs-chat-message-page": true, title: page.url, children: [_jsx(IconFileText, { className: "shrink-0", size: 12 }), _jsxs("span", { className: "truncate", children: [_jsx("span", { className: "sr-only", children: "Asked about " }), getChatPageLabel(page)] })] }));
18
+ };
@@ -0,0 +1,33 @@
1
+ import { type ReactNode } from "react";
2
+ import { type GeistdocsChatSession } from "./session";
3
+ type ChatSessionState = {
4
+ status: "loading";
5
+ } | {
6
+ error: Error;
7
+ /**
8
+ * Try again after a transient failure, such as an unreadable history
9
+ * store. Absent for configuration errors, which retrying can't fix.
10
+ */
11
+ retry?: () => void;
12
+ status: "error";
13
+ } | {
14
+ /** Clear the stored and displayed conversation. */
15
+ clearConversation: () => Promise<void>;
16
+ session: GeistdocsChatSession;
17
+ status: "ready";
18
+ };
19
+ /**
20
+ * The shared chat session. `"error"` must be thrown by the consumer so it
21
+ * fails inside that shell's chat error boundary instead of the page.
22
+ */
23
+ export declare const useChatSession: () => ChatSessionState;
24
+ /**
25
+ * Provides the single chat shared by the desktop panel and the mobile
26
+ * drawer. An invalid `ai.endpoint` is caught here and handed to the shells
27
+ * as an `"error"` state, so it fails inside their error boundaries.
28
+ */
29
+ export declare const GeistdocsChatSessionProvider: ({ children, }: {
30
+ children: ReactNode;
31
+ }) => import("react/jsx-runtime").JSX.Element;
32
+ export {};
33
+ //# sourceMappingURL=session-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-provider.d.ts","sourceRoot":"","sources":["../../../src/internal/chat/session-provider.tsx"],"names":[],"mappings":"AAIA,OAAO,EAEL,KAAK,SAAS,EAQf,MAAM,OAAO,CAAC;AAMf,OAAO,EAEL,KAAK,oBAAoB,EAE1B,MAAM,WAAW,CAAC;AAInB,KAAK,gBAAgB,GACjB;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,GACrB;IACE,KAAK,EAAE,KAAK,CAAC;IACb;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,IAAI,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;CACjB,GACD;IACE,mDAAmD;IACnD,iBAAiB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,OAAO,EAAE,oBAAoB,CAAC;IAC9B,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAIN;;;GAGG;AACH,eAAO,MAAM,cAAc,wBAU1B,CAAC;AA8HF;;;;GAIG;AACH,eAAO,MAAM,4BAA4B,GAAI,eAE1C;IACD,QAAQ,EAAE,SAAS,CAAC;CACrB,4CAmCA,CAAC"}
@@ -0,0 +1,138 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { useChat } from "@ai-sdk/react";
4
+ import { usePathname } from "next/navigation";
5
+ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore, } from "react";
6
+ import { useToasts } from "#geistdocs-ui/components/toasts";
7
+ import { getPublicPath } from "../../config";
8
+ import { useGeistdocsChatContext } from "./context";
9
+ import { getChatDatabase } from "./db";
10
+ import { resolveChatEndpoint, stampChatMessages } from "./options";
11
+ import { createChatSession, toError, } from "./session";
12
+ const SAVE_DELAY_MS = 300;
13
+ const ChatSessionContext = createContext(null);
14
+ /**
15
+ * The shared chat session. `"error"` must be thrown by the consumer so it
16
+ * fails inside that shell's chat error boundary instead of the page.
17
+ */
18
+ export const useChatSession = () => {
19
+ const state = useContext(ChatSessionContext);
20
+ if (!state) {
21
+ throw new Error("useChatSession must be used within GeistdocsChatSessionProvider.");
22
+ }
23
+ return state;
24
+ };
25
+ /**
26
+ * Owns the one chat both shells render: restores the stored conversation,
27
+ * saves it as it changes, clears it, and prewarms eve sessions. Runs once per
28
+ * page however many shells are mounted.
29
+ */
30
+ const ChatSessionRoot = ({ api, children, }) => {
31
+ const { historyRetentionMs, prewarmEveAgent, title } = useGeistdocsChatContext();
32
+ const pathname = usePathname();
33
+ const toast = useToasts();
34
+ const pathnameRef = useRef(pathname);
35
+ const toastRef = useRef(toast);
36
+ const saveTimeoutRef = useRef(undefined);
37
+ const db = useMemo(() => getChatDatabase(title), [title]);
38
+ const [session] = useState(() => createChatSession({
39
+ api,
40
+ getCurrentRoute: () => pathnameRef.current,
41
+ history: {
42
+ clear: () => db.messages.clear(),
43
+ read: () => db.messages.orderBy("sequence").toArray(),
44
+ },
45
+ historyRetentionMs,
46
+ onError: (error) => {
47
+ toastRef.current.error({ text: error.message });
48
+ },
49
+ prewarmEveAgent,
50
+ }));
51
+ const { messages } = useChat({ chat: session.chat });
52
+ const history = useSyncExternalStore(session.history.subscribe, session.history.getState, session.history.getState);
53
+ const initialized = history.status === "ready";
54
+ const persist = historyRetentionMs > 0;
55
+ useEffect(() => {
56
+ pathnameRef.current = pathname;
57
+ toastRef.current = toast;
58
+ });
59
+ useEffect(() => {
60
+ session.history.restore();
61
+ }, [session]);
62
+ useEffect(() => {
63
+ if (!(initialized && persist) || messages.length === 0) {
64
+ return;
65
+ }
66
+ clearTimeout(saveTimeoutRef.current);
67
+ saveTimeoutRef.current = setTimeout(async () => {
68
+ try {
69
+ await db.transaction("rw", db.messages, async () => {
70
+ // Keep each stored message's original timestamp so the retention
71
+ // window measures the last new message, not the last save.
72
+ const messagesToStore = stampChatMessages(messages, await db.messages.toArray(), Date.now());
73
+ await db.messages.clear();
74
+ await db.messages.bulkAdd(messagesToStore);
75
+ });
76
+ }
77
+ catch (error) {
78
+ console.error("Failed to save messages:", error);
79
+ }
80
+ }, SAVE_DELAY_MS);
81
+ }, [db, initialized, messages, persist]);
82
+ useEffect(() => () => clearTimeout(saveTimeoutRef.current), []);
83
+ useEffect(() => {
84
+ if (initialized && messages.length === 0) {
85
+ session.prewarm();
86
+ }
87
+ }, [initialized, messages.length, session]);
88
+ const clearConversation = useCallback(async () => {
89
+ clearTimeout(saveTimeoutRef.current);
90
+ saveTimeoutRef.current = undefined;
91
+ try {
92
+ await db.messages.clear();
93
+ }
94
+ catch (error) {
95
+ console.error("Failed to clear messages:", error);
96
+ }
97
+ session.chat.messages = [];
98
+ }, [db, session]);
99
+ const value = useMemo(() => {
100
+ if (history.status === "ready") {
101
+ return { clearConversation, session, status: "ready" };
102
+ }
103
+ if (history.status === "error") {
104
+ return {
105
+ error: history.error,
106
+ retry: session.history.restore,
107
+ status: "error",
108
+ };
109
+ }
110
+ return history;
111
+ }, [clearConversation, history, session]);
112
+ return (_jsx(ChatSessionContext.Provider, { value: value, children: children }));
113
+ };
114
+ /**
115
+ * Provides the single chat shared by the desktop panel and the mobile
116
+ * drawer. An invalid `ai.endpoint` is caught here and handed to the shells
117
+ * as an `"error"` state, so it fails inside their error boundaries.
118
+ */
119
+ export const GeistdocsChatSessionProvider = ({ children, }) => {
120
+ const { basePath, chatEndpoint, historyRetentionMs, prewarmEveAgent, title } = useGeistdocsChatContext();
121
+ const resolved = useMemo(() => {
122
+ try {
123
+ return {
124
+ api: getPublicPath(resolveChatEndpoint(chatEndpoint), basePath),
125
+ };
126
+ }
127
+ catch (error) {
128
+ return { failed: { error: toError(error), status: "error" } };
129
+ }
130
+ }, [basePath, chatEndpoint]);
131
+ if ("failed" in resolved) {
132
+ return (_jsx(ChatSessionContext.Provider, { value: resolved.failed, children: children }));
133
+ }
134
+ return (
135
+ // The session captures these options when it is created, so a change
136
+ // starts a new one.
137
+ _jsx(ChatSessionRoot, { api: resolved.api, children: children }, [resolved.api, title, historyRetentionMs, prewarmEveAgent].join("\n")));
138
+ };
@@ -0,0 +1,60 @@
1
+ import { Chat } from "@ai-sdk/react";
2
+ import { type UIMessage } from "ai";
3
+ export type ChatHistoryState = {
4
+ status: "loading";
5
+ } | {
6
+ status: "ready";
7
+ } | {
8
+ error: Error;
9
+ status: "error";
10
+ };
11
+ /** A stored message row: the message plus its storage fields. */
12
+ export type StoredChatRow = UIMessage & {
13
+ sequence?: unknown;
14
+ timestamp?: unknown;
15
+ };
16
+ /** Browser storage holding the conversation (IndexedDB in the chat shell). */
17
+ export interface ChatHistoryStore {
18
+ clear: () => Promise<void>;
19
+ /** Stored rows in `sequence` order. */
20
+ read: () => Promise<StoredChatRow[]>;
21
+ }
22
+ export declare const toError: (error: unknown) => Error;
23
+ /**
24
+ * The one Ask AI conversation a page owns. The desktop panel and the mobile
25
+ * drawer both render this chat, so whichever is active sends with the full
26
+ * history, sees the same status, and persists the same messages.
27
+ */
28
+ export interface GeistdocsChatSession {
29
+ chat: Chat<UIMessage>;
30
+ history: {
31
+ getState: () => ChatHistoryState;
32
+ /**
33
+ * Load the stored conversation into the chat, clearing it instead when it
34
+ * has expired. After a failed load, calling it again retries. Does
35
+ * nothing while a load is running or once one has succeeded, so it never
36
+ * replaces a conversation in progress.
37
+ */
38
+ restore: () => void;
39
+ subscribe: (listener: () => void) => () => void;
40
+ };
41
+ /**
42
+ * Prewarm an eve session for the next conversation. Does nothing when
43
+ * prewarming is off, a prewarm is already pending, or the conversation has
44
+ * messages.
45
+ */
46
+ prewarm: () => void;
47
+ }
48
+ export interface CreateChatSessionOptions {
49
+ /** Resolved, app-local chat route URL. */
50
+ api: string;
51
+ /** Route the reader is on when a message is sent. */
52
+ getCurrentRoute: () => string | null;
53
+ history: ChatHistoryStore;
54
+ /** Milliseconds a stored conversation is kept after its last message. */
55
+ historyRetentionMs: number;
56
+ onError: (error: Error) => void;
57
+ prewarmEveAgent: boolean;
58
+ }
59
+ export declare const createChatSession: ({ api, getCurrentRoute, history, historyRetentionMs, onError, prewarmEveAgent, }: CreateChatSessionOptions) => GeistdocsChatSession;
60
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/internal/chat/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAwB,KAAK,SAAS,EAAE,MAAM,IAAI,CAAC;AAI1D,MAAM,MAAM,gBAAgB,GACxB;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,GACrB;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,GACnB;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC;AAEtC,iEAAiE;AACjE,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG;IACtC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,8EAA8E;AAC9E,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,uCAAuC;IACvC,IAAI,EAAE,MAAM,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;CACtC;AAED,eAAO,MAAM,OAAO,GAAI,OAAO,OAAO,UACqB,CAAC;AAE5D;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACtB,OAAO,EAAE;QACP,QAAQ,EAAE,MAAM,gBAAgB,CAAC;QACjC;;;;;WAKG;QACH,OAAO,EAAE,MAAM,IAAI,CAAC;QACpB,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;KACjD,CAAC;IACF;;;;OAIG;IACH,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,wBAAwB;IACvC,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,qDAAqD;IACrD,eAAe,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IACrC,OAAO,EAAE,gBAAgB,CAAC;IAC1B,yEAAyE;IACzE,kBAAkB,EAAE,MAAM,CAAC;IAC3B,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAChC,eAAe,EAAE,OAAO,CAAC;CAC1B;AAED,eAAO,MAAM,iBAAiB,GAAI,kFAO/B,wBAAwB,KAAG,oBA6F7B,CAAC"}
@@ -0,0 +1,85 @@
1
+ import { Chat } from "@ai-sdk/react";
2
+ import { DefaultChatTransport } from "ai";
3
+ import { requestEveAgentPrewarm } from "../eve-agent-session.js";
4
+ import { restoreStoredChat } from "./options.js";
5
+ export const toError = (error) => error instanceof Error ? error : new Error(String(error));
6
+ export const createChatSession = ({ api, getCurrentRoute, history, historyRetentionMs, onError, prewarmEveAgent, }) => {
7
+ let pendingPrewarm = null;
8
+ let historyState = { status: "loading" };
9
+ let restoring = false;
10
+ const historyListeners = new Set();
11
+ const setHistoryState = (next) => {
12
+ historyState = next;
13
+ for (const listener of historyListeners) {
14
+ listener();
15
+ }
16
+ };
17
+ const readHistory = async () => {
18
+ const { expired, messages } = restoreStoredChat({
19
+ now: Date.now(),
20
+ retentionMs: historyRetentionMs,
21
+ rows: await history.read(),
22
+ });
23
+ if (expired) {
24
+ await history.clear().catch((error) => {
25
+ console.error("Failed to clear expired messages:", error);
26
+ });
27
+ }
28
+ return messages;
29
+ };
30
+ const chat = new Chat({
31
+ transport: new DefaultChatTransport({
32
+ api,
33
+ prepareSendMessagesRequest: async ({ body, messages }) => {
34
+ // The prewarm response sets the session cookie; wait for it so the
35
+ // first message reuses the prewarmed session.
36
+ await pendingPrewarm;
37
+ pendingPrewarm = null;
38
+ return {
39
+ body: {
40
+ ...body,
41
+ messages,
42
+ currentRoute: getCurrentRoute(),
43
+ },
44
+ };
45
+ },
46
+ }),
47
+ onError,
48
+ });
49
+ return {
50
+ chat,
51
+ history: {
52
+ getState: () => historyState,
53
+ restore: () => {
54
+ if (restoring || historyState.status === "ready") {
55
+ return;
56
+ }
57
+ restoring = true;
58
+ if (historyState.status === "error") {
59
+ setHistoryState({ status: "loading" });
60
+ }
61
+ readHistory().then((messages) => {
62
+ restoring = false;
63
+ chat.messages = messages;
64
+ setHistoryState({ status: "ready" });
65
+ }, (error) => {
66
+ restoring = false;
67
+ setHistoryState({ error: toError(error), status: "error" });
68
+ });
69
+ },
70
+ subscribe: (listener) => {
71
+ historyListeners.add(listener);
72
+ return () => {
73
+ historyListeners.delete(listener);
74
+ };
75
+ },
76
+ },
77
+ prewarm: () => {
78
+ if (prewarmEveAgent &&
79
+ pendingPrewarm === null &&
80
+ chat.messages.length === 0) {
81
+ pendingPrewarm = requestEveAgentPrewarm({ apiUrl: api });
82
+ }
83
+ },
84
+ };
85
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["../../../src/internal/chat/shell.tsx"],"names":[],"mappings":"AA8aA,eAAO,MAAM,kBAAkB,sDAsD9B,CAAC"}
1
+ {"version":3,"file":"shell.d.ts","sourceRoot":"","sources":["../../../src/internal/chat/shell.tsx"],"names":[],"mappings":"AA6ZA,eAAO,MAAM,kBAAkB,sDAgE9B,CAAC"}