@tansy/ai 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -2
  3. package/admin.d.ts +2 -0
  4. package/admin.js +17 -0
  5. package/ai.css +280 -0
  6. package/api.d.ts +91 -0
  7. package/api.js +79 -0
  8. package/assistant.d.ts +3 -0
  9. package/assistant.js +290 -0
  10. package/attach.d.ts +48 -0
  11. package/attach.js +141 -0
  12. package/attachments.d.ts +34 -0
  13. package/attachments.js +66 -0
  14. package/chats.d.ts +35 -0
  15. package/chats.js +78 -0
  16. package/client.d.ts +3 -0
  17. package/client.js +88 -0
  18. package/composer.d.ts +38 -0
  19. package/composer.js +155 -0
  20. package/errors.d.ts +5 -0
  21. package/errors.js +25 -0
  22. package/history.d.ts +15 -0
  23. package/history.js +47 -0
  24. package/icons.generated.d.ts +5 -0
  25. package/icons.generated.js +66 -0
  26. package/images.d.ts +21 -0
  27. package/images.js +23 -0
  28. package/index.d.ts +26 -0
  29. package/index.js +701 -0
  30. package/logic.d.ts +77 -0
  31. package/logic.js +166 -0
  32. package/markdown.d.ts +38 -0
  33. package/markdown.js +142 -0
  34. package/media.d.ts +24 -0
  35. package/media.js +64 -0
  36. package/messages.d.ts +891 -0
  37. package/messages.js +689 -0
  38. package/models.d.ts +7 -0
  39. package/models.js +47 -0
  40. package/package.json +46 -4
  41. package/parts.d.ts +59 -0
  42. package/parts.js +110 -0
  43. package/provider.d.ts +134 -0
  44. package/provider.js +117 -0
  45. package/role.d.ts +24 -0
  46. package/role.js +25 -0
  47. package/workflow/check.d.ts +46 -0
  48. package/workflow/check.js +63 -0
  49. package/workflow/client.d.ts +90 -0
  50. package/workflow/client.js +51 -0
  51. package/workflow/editor.d.ts +13 -0
  52. package/workflow/editor.js +133 -0
  53. package/workflow/engine.d.ts +232 -0
  54. package/workflow/engine.js +779 -0
  55. package/workflow/icons.d.ts +26 -0
  56. package/workflow/icons.js +67 -0
  57. package/workflow/model-picker.d.ts +22 -0
  58. package/workflow/model-picker.js +69 -0
  59. package/workflow/page.d.ts +3 -0
  60. package/workflow/page.js +118 -0
  61. package/workflow/routes.d.ts +12 -0
  62. package/workflow/routes.js +418 -0
  63. package/workflow/run.d.ts +8 -0
  64. package/workflow/run.js +162 -0
  65. package/workflow/schema.d.ts +183 -0
  66. package/workflow/schema.js +143 -0
  67. package/workflow/templates.d.ts +9 -0
  68. package/workflow/templates.js +81 -0
  69. package/workflow/workflow.css +241 -0
package/assistant.js ADDED
@@ -0,0 +1,290 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useChat } from "@ai-sdk/react";
4
+ import { DefaultChatTransport, generateId, lastAssistantMessageIsCompleteWithApprovalResponses } from "ai";
5
+ import { adminPath, apiPath, PageHeader, useT } from "tansy/admin";
6
+ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
7
+ import { aiStatus, deleteChat, listChats, loadChat } from "./api";
8
+ import { activeProvider, Composer, ProviderMenu } from "./composer";
9
+ import { SentFiles, useAttachments } from "./attach";
10
+ import { History } from "./history";
11
+ import { AnswerParts, ArrowDown, CopyButton, Panel, Plus, Retry, Spark, textOf } from "./parts";
12
+ import { ModelIcon } from "./workflow/icons";
13
+ import "./ai.css";
14
+ /** Only the most recent messages go up, so a long chat stays under the request size limit. */
15
+ const HISTORY = 40;
16
+ /**
17
+ * Starting points on an empty chat. A complete request is sent as it is; the start of one
18
+ * goes in the box for the person to finish, so a click never asks for something they didn't.
19
+ */
20
+ const SUGGESTIONS = [
21
+ { key: "collections", send: true },
22
+ { key: "drafts", send: true },
23
+ { key: "post", send: false },
24
+ { key: "course", send: false },
25
+ ];
26
+ /** The open chat's id, in the address, so a reload or a shared link opens it again. */
27
+ const CHAT_PARAM = "chat";
28
+ const CHAT_ID = /^[A-Za-z0-9_-]{1,64}$/;
29
+ function chatInAddress() {
30
+ const id = new URL(window.location.href).searchParams.get(CHAT_PARAM);
31
+ return id && CHAT_ID.test(id) ? id : null;
32
+ }
33
+ function showChatInAddress(id) {
34
+ const url = new URL(window.location.href);
35
+ if (id)
36
+ url.searchParams.set(CHAT_PARAM, id);
37
+ else
38
+ url.searchParams.delete(CHAT_PARAM);
39
+ if (url.href !== window.location.href)
40
+ window.history.replaceState(window.history.state, "", url);
41
+ }
42
+ /* This browser's own conveniences: the chats panel open or shut, and the last model and thinking chosen. Gone in a private window, which is fine. */
43
+ const SIDE_KEY = "tansy.ai.side";
44
+ const CHOICE_KEY = "tansy.ai.choice";
45
+ const REASONING = new Set(["provider-default", "none", "low", "medium", "high"]);
46
+ /**
47
+ * The model and thinking the next request asks for. The transport reads it when it sends,
48
+ * which includes the sends that follow an approval, so it lives outside React's render.
49
+ * There is one assistant on a page.
50
+ */
51
+ const picked = { choice: { provider: null, model: null, reasoning: "provider-default", role: null } };
52
+ function remembered(key) {
53
+ try {
54
+ const value = window.localStorage.getItem(key);
55
+ return value ? JSON.parse(value) : null;
56
+ }
57
+ catch {
58
+ return null;
59
+ }
60
+ }
61
+ function remember(key, value) {
62
+ try {
63
+ window.localStorage.setItem(key, JSON.stringify(value));
64
+ }
65
+ catch {
66
+ // Storage can be off. Nothing depends on it.
67
+ }
68
+ }
69
+ /** The chats panel sits beside the chat from this width, and over it below. */
70
+ const WIDE = "(min-width: 64rem)";
71
+ /** How close to the bottom, in pixels, still counts as at the bottom. */
72
+ const BOTTOM = 8;
73
+ /**
74
+ * Keeps the newest text in view while an answer streams, until the person scrolls up to read,
75
+ * and again once they scroll back down. Only the person's own scrolling stops it: content
76
+ * growing never does, and the chat scrolls itself rather than the page, so nothing else
77
+ * fights it for the scroll position.
78
+ */
79
+ function useFollow(mounted, messages, chatStatus) {
80
+ const scroller = useRef(null);
81
+ const content = useRef(null);
82
+ const following = useRef(true);
83
+ const lastTop = useRef(0);
84
+ const [atBottom, setAtBottom] = useState(true);
85
+ const toBottom = useCallback((smooth = false) => {
86
+ const element = scroller.current;
87
+ if (!element)
88
+ return;
89
+ following.current = true;
90
+ setAtBottom(true);
91
+ element.scrollTo({ top: element.scrollHeight, behavior: smooth ? "smooth" : "auto" });
92
+ }, []);
93
+ const onScroll = useCallback(() => {
94
+ const element = scroller.current;
95
+ if (!element)
96
+ return;
97
+ const gap = element.scrollHeight - element.scrollTop - element.clientHeight;
98
+ if (gap <= BOTTOM)
99
+ following.current = true;
100
+ else if (element.scrollTop < lastTop.current - 1)
101
+ following.current = false;
102
+ lastTop.current = element.scrollTop;
103
+ setAtBottom(gap <= BOTTOM * 4);
104
+ }, []);
105
+ // A wheel or a key up means the person wants to read, even before the scroll moves.
106
+ const onWheel = useCallback((event) => { if (event.deltaY < 0)
107
+ following.current = false; }, []);
108
+ useEffect(() => {
109
+ const element = content.current;
110
+ if (!element)
111
+ return;
112
+ const observer = new ResizeObserver(() => {
113
+ const box = scroller.current;
114
+ if (box && following.current)
115
+ box.scrollTop = box.scrollHeight;
116
+ onScroll();
117
+ });
118
+ observer.observe(element);
119
+ return () => observer.disconnect();
120
+ }, [mounted, onScroll]);
121
+ useLayoutEffect(() => {
122
+ const box = scroller.current;
123
+ if (box && following.current)
124
+ box.scrollTop = box.scrollHeight;
125
+ }, [mounted, messages, chatStatus]);
126
+ /** Follow again from the next change, as after switching chats. */
127
+ const resume = useCallback(() => { following.current = true; }, []);
128
+ return { scroller, content, toBottom, onScroll, onWheel, atBottom, resume };
129
+ }
130
+ /** The assistant page: a chat that works on the CMS as the signed-in person, asking before every change. */
131
+ export function Assistant() {
132
+ const { t } = useT();
133
+ const [status, setStatus] = useState(undefined);
134
+ const [input, setInput] = useState("");
135
+ // Each chat is saved under its id as its answers end, and listed under History. A new id makes useChat start a new chat, with these messages.
136
+ const [session, setSession] = useState(() => ({ id: generateId(), messages: [] }));
137
+ const [chats, setChats] = useState([]);
138
+ const [chatsLoaded, setChatsLoaded] = useState(false);
139
+ const [sideOpen, setSideOpen] = useState(false);
140
+ const [choice, setChoiceState] = useState(picked.choice);
141
+ const refreshChats = useCallback(() => { void listChats().then((list) => { setChats(list); setChatsLoaded(true); }); }, []);
142
+ const setChoice = (next) => {
143
+ picked.choice = next;
144
+ setChoiceState(next);
145
+ // The role isn't kept: each visit starts on the site's role, the safe one.
146
+ remember(CHOICE_KEY, { provider: next.provider, model: next.model, reasoning: next.reasoning });
147
+ };
148
+ const { messages, sendMessage, addToolApprovalResponse, status: chatStatus, stop, error, clearError, regenerate } = useChat({
149
+ id: session.id,
150
+ messages: session.messages,
151
+ transport: new DefaultChatTransport({
152
+ api: apiPath("/ext/ai/chat"),
153
+ prepareSendMessagesRequest: ({ id, messages: all }) => {
154
+ const { provider, model, reasoning, role } = picked.choice;
155
+ return { body: { chatId: id, messages: all.slice(-HISTORY), ...(provider ? { provider } : {}), ...(model ? { model } : {}), ...(role ? { role } : {}), reasoning } };
156
+ },
157
+ }),
158
+ sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
159
+ onFinish: () => refreshChats(),
160
+ });
161
+ const busy = chatStatus === "submitted" || chatStatus === "streaming";
162
+ const attachments = useAttachments(status?.chooseModel ? choice.model : null, Boolean(status?.ready), status?.chooseModel ? choice.provider : null);
163
+ const { scroller, content, toBottom, onScroll, onWheel, atBottom, resume } = useFollow(Boolean(status), messages, chatStatus);
164
+ const narrow = () => !window.matchMedia(WIDE).matches;
165
+ const showSide = (open) => {
166
+ setSideOpen(open);
167
+ if (!narrow())
168
+ remember(SIDE_KEY, open);
169
+ };
170
+ const switchTo = (id, saved) => {
171
+ void stop();
172
+ clearError();
173
+ setInput("");
174
+ attachments.clear();
175
+ if (narrow())
176
+ setSideOpen(false);
177
+ resume();
178
+ setSession({ id, messages: saved });
179
+ showChatInAddress(saved.length ? id : null);
180
+ };
181
+ const openChat = async (id) => {
182
+ if (id === session.id) {
183
+ if (narrow())
184
+ setSideOpen(false);
185
+ return;
186
+ }
187
+ const chat = await loadChat(id);
188
+ if (chat)
189
+ switchTo(id, chat.messages);
190
+ else
191
+ refreshChats();
192
+ };
193
+ const newChat = () => switchTo(generateId(), []);
194
+ const removeChat = async (id) => {
195
+ if (!(await deleteChat(id)))
196
+ return;
197
+ if (id === session.id)
198
+ newChat();
199
+ refreshChats();
200
+ };
201
+ useEffect(() => {
202
+ void aiStatus().then((value) => {
203
+ setStatus(value);
204
+ const saved = remembered(CHOICE_KEY);
205
+ // A provider no longer set up falls back to the site's, with its model.
206
+ const provider = typeof saved?.provider === "string" && value?.providers?.some((item) => item.id === saved.provider && item.id !== value.provider) ? saved.provider : null;
207
+ const sameProvider = (saved?.provider ?? null) === provider;
208
+ const next = {
209
+ provider,
210
+ model: sameProvider && typeof saved?.model === "string" && saved.model ? saved.model : null,
211
+ reasoning: saved?.reasoning && REASONING.has(saved.reasoning) ? saved.reasoning : value?.reasoning ?? "provider-default",
212
+ role: null,
213
+ };
214
+ picked.choice = next;
215
+ setChoiceState(next);
216
+ setSideOpen(window.matchMedia(WIDE).matches && remembered(SIDE_KEY) !== false);
217
+ });
218
+ refreshChats();
219
+ const id = chatInAddress();
220
+ if (id) {
221
+ void loadChat(id).then((chat) => {
222
+ if (chat)
223
+ setSession({ id, messages: chat.messages });
224
+ else
225
+ showChatInAddress(null);
226
+ });
227
+ }
228
+ }, [refreshChats]);
229
+ // A new chat from anywhere with Ctrl/Cmd + Shift + O, as in other chat apps.
230
+ useEffect(() => {
231
+ const onKey = (event) => {
232
+ if ((event.metaKey || event.ctrlKey) && event.shiftKey && event.key.toLowerCase() === "o") {
233
+ event.preventDefault();
234
+ newChat();
235
+ }
236
+ };
237
+ window.addEventListener("keydown", onKey);
238
+ return () => window.removeEventListener("keydown", onKey);
239
+ });
240
+ const send = useCallback((text) => {
241
+ if ((!text.trim() && !attachments.ready) || busy || attachments.uploading)
242
+ return;
243
+ clearError();
244
+ toBottom();
245
+ const files = attachments.take();
246
+ void sendMessage(files.length ? { text: text.trim(), files } : { text: text.trim() });
247
+ setInput("");
248
+ showChatInAddress(session.id);
249
+ }, [busy, clearError, sendMessage, session.id, toBottom, attachments]);
250
+ const inputRef = useRef(null);
251
+ /** Puts the start of a request in the box, with the cursor after it, for the person to finish. */
252
+ const startWith = (text) => {
253
+ const value = `${text.trimEnd()} `;
254
+ setInput(value);
255
+ requestAnimationFrame(() => {
256
+ const box = inputRef.current;
257
+ if (!box)
258
+ return;
259
+ box.focus();
260
+ box.setSelectionRange(value.length, value.length);
261
+ });
262
+ };
263
+ if (status === undefined)
264
+ return _jsx("div", { className: "ai-app", "aria-busy": "true" });
265
+ if (status === null)
266
+ return _jsxs("div", { className: "cms-gutter", children: [_jsx(PageHeader, { title: t("ext.ai.assistant.title") }), _jsx("p", { className: "cms-empty", children: t("ext.ai.assistant.noAccess") })] });
267
+ const composer = (_jsx(Composer, { inputRef: inputRef, value: input, onChange: setInput, onSend: () => send(input), onStop: () => stop(), busy: busy, disabled: !status.ready, autoFocus: status.ready, status: status, choice: choice, onChoice: setChoice, attachments: attachments }));
268
+ const last = messages[messages.length - 1];
269
+ const waiting = busy && (!last || last.role !== "assistant" || !last.parts.some((part) => (part.type === "text" && part.text) || part.type === "reasoning" || part.type === "dynamic-tool"));
270
+ const title = chats.find((chat) => chat.id === session.id)?.title ?? (messages[0] ? textOf(messages[0]) : "");
271
+ const empty = messages.length === 0;
272
+ const notice = !status.ready && _jsxs("p", { className: "ai-notice", children: [t("ext.ai.assistant.notSetUp"), " ", _jsx("a", { href: adminPath("/extensions/ai/settings"), children: t("ext.ai.assistant.settingsLink") }), "."] });
273
+ return (_jsxs("div", { className: sideOpen ? "ai-app ai-app-side" : "ai-app", children: [sideOpen && _jsx("button", { type: "button", className: "ai-scrim", "aria-label": t("ext.ai.history.hide"), tabIndex: -1, onClick: () => showSide(false) }), sideOpen && _jsx(History, { chats: chats, loaded: chatsLoaded, current: empty ? undefined : session.id, onOpen: (id) => void openChat(id), onDelete: (id) => void removeChat(id), onNew: newChat, onClose: () => showSide(false) }), _jsxs("div", { className: "ai-main", children: [_jsxs("header", { className: "ai-bar", children: [!sideOpen && _jsx("button", { type: "button", className: "ai-ghost ai-ghost-icon", "aria-label": t("ext.ai.history.show"), title: t("ext.ai.history.show"), onClick: () => showSide(true), children: _jsx(Panel, {}) }), _jsx("h1", { className: "ai-bar-title", title: title || undefined, children: empty ? t("ext.ai.assistant.title") : title }), _jsx(ProviderMenu, { status: status, value: choice.provider, onChange: (provider) => setChoice({ ...choice, provider, model: null }) }), !empty && _jsxs("button", { type: "button", className: "ai-ghost", title: `${t("ext.ai.assistant.newChat")} (Ctrl+Shift+O)`, onClick: newChat, children: [_jsx(Plus, {}), _jsx("span", { className: "ai-hide-sm", children: t("ext.ai.assistant.newChat") })] })] }), _jsx("div", { ref: scroller, className: "ai-scroll", onScroll: onScroll, onWheel: onWheel, children: _jsx("div", { ref: content, className: empty ? "ai-column ai-column-empty" : "ai-column", children: empty
274
+ ? (_jsxs("div", { className: "ai-start", children: [_jsxs("div", { className: "ai-hero", children: [_jsx(Spark, { className: "ai-hero-mark" }), _jsx("p", { className: "ai-hero-title", children: t("ext.ai.assistant.greeting") })] }), notice, composer, status.ready && (_jsx("div", { className: "ai-chips", role: "list", children: SUGGESTIONS.map(({ key, send: complete }) => {
275
+ const text = t(`ext.ai.assistant.suggestions.${key}`);
276
+ return (_jsx("button", { type: "button", role: "listitem", className: "ai-chip", title: complete ? text : t("ext.ai.assistant.fillHint", { text }), onClick: () => (complete ? send(text) : startWith(text)), children: t(`ext.ai.assistant.suggestionLabels.${key}`) }, key));
277
+ }) })), _jsx("p", { className: "ai-welcome", children: t("ext.ai.assistant.welcome") })] }))
278
+ : (_jsxs("div", { className: "ai-thread", "aria-live": "polite", children: [notice, messages.map((message, index) => {
279
+ if (message.role === "user") {
280
+ const text = textOf(message);
281
+ return (_jsxs("div", { className: "ai-row ai-row-user", children: [_jsx(SentFiles, { message: message }), text && _jsx("div", { className: "ai-bubble", children: text }), _jsx("div", { className: "ai-row-actions", children: _jsx(CopyButton, { text: text }) })] }, message.id));
282
+ }
283
+ const text = textOf(message);
284
+ const isLast = index === messages.length - 1;
285
+ const streaming = busy && isLast;
286
+ const { model, provider } = message.metadata ?? {};
287
+ const answeredBy = activeProvider(status, typeof provider === "string" ? provider : null);
288
+ return (_jsxs("div", { className: "ai-row ai-row-assistant", children: [_jsx(AnswerParts, { message: message, respond: (id, approved) => void addToolApprovalResponse({ id, approved }) }), streaming && _jsx("span", { className: "ai-cursor", "aria-hidden": "true", children: _jsx(Spark, {}) }), !streaming && (text || isLast) && (_jsxs("div", { className: "ai-row-actions", children: [text && _jsx(CopyButton, { text: text }), isLast && !busy && _jsx("button", { type: "button", className: "ai-ghost ai-ghost-icon", "aria-label": t("ext.ai.assistant.regenerate"), title: t("ext.ai.assistant.regenerate"), onClick: () => { clearError(); toBottom(); void regenerate(); }, children: _jsx(Retry, {}) }), typeof model === "string" && _jsxs("span", { className: "ai-model-tag", title: model, children: [_jsx(ModelIcon, { model: model, provider: answeredBy.id, icon: answeredBy.icon, size: 14 }), model.split("/").pop()] })] }))] }, message.id));
289
+ }), waiting && (_jsx("div", { className: "ai-row ai-row-assistant", role: "status", children: _jsxs("span", { className: "ai-waiting", children: [_jsx(Spark, { className: "ai-waiting-mark" }), _jsx("span", { className: "ai-shimmer", children: t("ext.ai.assistant.thinking") })] }) })), error && (_jsxs("div", { className: "ai-failure", role: "alert", children: [_jsx("span", { children: error.message || t("ext.ai.assistant.failed") }), _jsxs("button", { type: "button", className: "ai-ghost", onClick: () => { clearError(); toBottom(); void regenerate(); }, children: [_jsx(Retry, {}), _jsx("span", { children: t("ext.ai.assistant.retry") })] })] }))] })) }) }), !empty && (_jsx("div", { className: "ai-dock", children: _jsxs("div", { className: "ai-column", children: [!atBottom && (_jsx("button", { type: "button", className: "ai-jump", "aria-label": t("ext.ai.assistant.jumpToLatest"), title: t("ext.ai.assistant.jumpToLatest"), onClick: () => toBottom(true), children: _jsx(ArrowDown, {}) })), composer, _jsx("p", { className: "ai-foot", children: t("ext.ai.assistant.foot") })] }) }))] })] }));
290
+ }
package/attach.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ import type { FileUIPart, UIMessage } from "ai";
2
+ import { type ChatCapabilities } from "./api";
3
+ import { type ToolPart } from "./parts";
4
+ export declare const Paperclip: () => import("react").JSX.Element;
5
+ /** A file on its way into the next message. */
6
+ export interface Pending {
7
+ key: string;
8
+ name: string;
9
+ mediaType: string;
10
+ status: "uploading" | "ready" | "failed";
11
+ /** A picture's preview while it uploads, from the browser's own copy. */
12
+ preview?: string;
13
+ id?: string;
14
+ url?: string;
15
+ error?: string;
16
+ }
17
+ /**
18
+ * The files the person is attaching, uploaded as soon as they're chosen so sending is quick,
19
+ * and what the chat's model reads, so only files it can take are offered.
20
+ */
21
+ export declare function useAttachments(model: string | null, enabled: boolean, provider?: string | null): {
22
+ capabilities: ChatCapabilities | null;
23
+ items: Pending[];
24
+ add: (files: File[]) => void;
25
+ remove: (key: string) => void;
26
+ take: () => FileUIPart[];
27
+ clear: () => void;
28
+ uploading: boolean;
29
+ ready: boolean;
30
+ };
31
+ export type Attachments = ReturnType<typeof useAttachments>;
32
+ /** The files waiting to go with the next message. */
33
+ export declare function PendingFiles({ attachments }: {
34
+ attachments: Attachments;
35
+ }): import("react").JSX.Element | null;
36
+ /** The button that opens the file picker, offering only what the model reads. */
37
+ export declare function AttachButton({ attachments, disabled }: {
38
+ attachments: Attachments;
39
+ disabled: boolean;
40
+ }): import("react").JSX.Element | null;
41
+ /** The files on a message the person sent: pictures as thumbnails, anything else by name. */
42
+ export declare function SentFiles({ message }: {
43
+ message: UIMessage;
44
+ }): import("react").JSX.Element | null;
45
+ /** A picture the assistant drew: shown in the chat, with a download and a way into the site's files. */
46
+ export declare function PictureCard({ part }: {
47
+ part: ToolPart;
48
+ }): import("react").JSX.Element;
package/attach.js ADDED
@@ -0,0 +1,141 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import { adminPath, useT } from "tansy/admin";
4
+ import { useCallback, useEffect, useRef, useState } from "react";
5
+ import { addToFiles, chatCapabilities, deleteAttachment, uploadAttachment } from "./api";
6
+ import { attachmentType, MAX_ATTACHMENTS } from "./media";
7
+ import { Check, Cross, Icon } from "./parts";
8
+ /* Files in the chat: attaching them to a message, showing them on it, and the pictures the assistant draws. */
9
+ export const Paperclip = () => _jsx(Icon, { children: _jsx("path", { d: "m13 7.5-5.3 5.3a3.25 3.25 0 0 1-4.6-4.6l5.6-5.6a2.1 2.1 0 0 1 3 3l-5.6 5.6a1 1 0 0 1-1.4-1.4l5.2-5.2" }) });
10
+ const FileIcon = () => _jsxs(Icon, { children: [_jsx("path", { d: "M9 1.75H4.5a1.5 1.5 0 0 0-1.5 1.5v9.5a1.5 1.5 0 0 0 1.5 1.5h7a1.5 1.5 0 0 0 1.5-1.5V5.75L9 1.75Z" }), _jsx("path", { d: "M9 1.75v4h4" })] });
11
+ const Download = () => _jsx(Icon, { children: _jsx("path", { d: "M8 2.5v8M4.5 7 8 10.5 11.5 7M3 13.5h10" }) });
12
+ const megabytes = (bytes) => Math.round(bytes / 1024 / 1024);
13
+ /**
14
+ * The files the person is attaching, uploaded as soon as they're chosen so sending is quick,
15
+ * and what the chat's model reads, so only files it can take are offered.
16
+ */
17
+ export function useAttachments(model, enabled, provider = null) {
18
+ const { t } = useT();
19
+ const [capabilities, setCapabilities] = useState(null);
20
+ const [items, setItems] = useState([]);
21
+ const counter = useRef(0);
22
+ useEffect(() => {
23
+ if (!enabled)
24
+ return;
25
+ let live = true;
26
+ void chatCapabilities(model, provider).then((value) => { if (live)
27
+ setCapabilities(value); });
28
+ return () => { live = false; };
29
+ }, [model, enabled, provider]);
30
+ const patch = (key, value) => setItems((list) => list.map((item) => (item.key === key ? { ...item, ...value } : item)));
31
+ // The list as last rendered, so adding files can count it without starting uploads inside a state update.
32
+ const current = useRef(items);
33
+ useEffect(() => { current.current = items; }, [items]);
34
+ const add = useCallback((files) => {
35
+ if (!capabilities || !files.length)
36
+ return;
37
+ const room = MAX_ATTACHMENTS - current.current.filter((item) => item.status !== "failed").length;
38
+ const next = [];
39
+ files.forEach((file, index) => {
40
+ const key = `f${counter.current++}`;
41
+ const mediaType = attachmentType(file.type, file.name);
42
+ const fail = (error) => { next.push({ key, name: file.name, mediaType: file.type, status: "failed", error }); };
43
+ if (index >= room)
44
+ return fail(t("ext.ai.attach.tooMany", { count: MAX_ATTACHMENTS }));
45
+ if (!mediaType)
46
+ return fail(t("ext.ai.attach.unsupported"));
47
+ if (!capabilities.accept.includes(mediaType))
48
+ return fail(t("ext.ai.attach.notRead", { model: capabilities.model.split("/").pop() ?? capabilities.model }));
49
+ if (file.size > capabilities.maxBytes)
50
+ return fail(t("ext.ai.attach.tooLarge", { size: megabytes(capabilities.maxBytes) }));
51
+ const preview = mediaType.startsWith("image/") ? URL.createObjectURL(file) : undefined;
52
+ next.push({ key, name: file.name, mediaType, status: "uploading", preview });
53
+ uploadAttachment(file, file.name, mediaType).then((saved) => patch(key, { status: "ready", id: saved.id, url: saved.url, name: saved.name }), (error) => patch(key, { status: "failed", error: error instanceof Error ? error.message : t("ext.ai.attach.failed") }));
54
+ });
55
+ setItems((list) => [...list, ...next]);
56
+ }, [capabilities, t]);
57
+ const remove = useCallback((key) => {
58
+ const item = current.current.find((entry) => entry.key === key);
59
+ if (item?.preview)
60
+ URL.revokeObjectURL(item.preview);
61
+ if (item?.id)
62
+ void deleteAttachment(item.id);
63
+ setItems((list) => list.filter((entry) => entry.key !== key));
64
+ }, []);
65
+ /** The ready files as message parts, and an empty list for the next message. Failed ones are dropped. */
66
+ const take = useCallback(() => {
67
+ const ready = items.filter((item) => item.status === "ready" && item.url);
68
+ for (const item of items)
69
+ if (item.preview)
70
+ URL.revokeObjectURL(item.preview);
71
+ setItems([]);
72
+ return ready.map((item) => ({ type: "file", mediaType: item.mediaType, filename: item.name, url: item.url }));
73
+ }, [items]);
74
+ /** Forgets the list without deleting what was uploaded, as when switching chats. */
75
+ const clear = useCallback(() => {
76
+ for (const item of current.current)
77
+ if (item.preview)
78
+ URL.revokeObjectURL(item.preview);
79
+ setItems([]);
80
+ }, []);
81
+ return {
82
+ capabilities,
83
+ items,
84
+ add,
85
+ remove,
86
+ take,
87
+ clear,
88
+ uploading: items.some((item) => item.status === "uploading"),
89
+ ready: items.some((item) => item.status === "ready"),
90
+ };
91
+ }
92
+ /** The files waiting to go with the next message. */
93
+ export function PendingFiles({ attachments }) {
94
+ const { t } = useT();
95
+ if (!attachments.items.length)
96
+ return null;
97
+ return (_jsx("ul", { className: "ai-files ai-files-pending", "aria-label": t("ext.ai.attach.attached"), children: attachments.items.map((item) => (_jsxs("li", { className: `ai-file ai-file-${item.status}`, title: item.error ?? item.name, children: [item.preview
98
+ // eslint-disable-next-line @next/next/no-img-element -- the browser's own copy of a file being attached, which next/image can't load
99
+ ? _jsx("img", { className: "ai-file-thumb", src: item.preview, alt: "" })
100
+ : _jsx("span", { className: "ai-file-thumb ai-file-thumb-icon", children: _jsx(FileIcon, {}) }), _jsxs("span", { className: "ai-file-text", children: [_jsx("span", { className: "ai-file-name", children: item.name }), _jsx("span", { className: "ai-file-note", children: item.status === "uploading" ? t("ext.ai.attach.uploading") : item.status === "failed" ? item.error : "" })] }), item.status === "uploading" && _jsx("span", { className: "ai-spinner", "aria-hidden": "true" }), _jsx("button", { type: "button", className: "ai-file-remove", "aria-label": t("ext.ai.attach.remove", { name: item.name }), title: t("ext.ai.attach.remove", { name: item.name }), onClick: () => attachments.remove(item.key), children: _jsx(Cross, {}) })] }, item.key))) }));
101
+ }
102
+ /** The button that opens the file picker, offering only what the model reads. */
103
+ export function AttachButton({ attachments, disabled }) {
104
+ const { t } = useT();
105
+ const input = useRef(null);
106
+ const accept = attachments.capabilities?.accept ?? [];
107
+ if (!attachments.capabilities)
108
+ return null;
109
+ return (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "ai-pill ai-pill-icon", "aria-label": t("ext.ai.attach.button"), title: t("ext.ai.attach.hint"), disabled: disabled, onClick: () => input.current?.click(), children: _jsx(Paperclip, {}) }), _jsx("input", { ref: input, type: "file", hidden: true, multiple: true, accept: [...accept, ".md", ".markdown", ".csv", ".txt"].join(","), onChange: (event) => { attachments.add(Array.from(event.target.files ?? [])); event.target.value = ""; } })] }));
110
+ }
111
+ /** The files on a message the person sent: pictures as thumbnails, anything else by name. */
112
+ export function SentFiles({ message }) {
113
+ const files = message.parts.filter((part) => part.type === "file");
114
+ if (!files.length)
115
+ return null;
116
+ return (_jsx("ul", { className: "ai-files ai-files-sent", children: files.map((file, index) => (_jsx("li", { children: _jsx("a", { className: file.mediaType.startsWith("image/") ? "ai-file ai-file-image" : "ai-file", href: file.url, target: "_blank", rel: "noreferrer", title: file.filename, children: file.mediaType.startsWith("image/")
117
+ // eslint-disable-next-line @next/next/no-img-element -- the person's own attachment, served behind their session, which next/image can't fetch
118
+ ? _jsx("img", { className: "ai-file-picture", src: file.url, alt: file.filename ?? "", loading: "lazy" })
119
+ : _jsxs(_Fragment, { children: [_jsx("span", { className: "ai-file-thumb ai-file-thumb-icon", children: _jsx(FileIcon, {}) }), _jsx("span", { className: "ai-file-text", children: _jsx("span", { className: "ai-file-name", children: file.filename ?? file.mediaType }) })] }) }) }, index))) }));
120
+ }
121
+ /** A picture the assistant drew: shown in the chat, with a download and a way into the site's files. */
122
+ export function PictureCard({ part }) {
123
+ const { t } = useT();
124
+ const [added, setAdded] = useState("no");
125
+ const output = part.state === "output-available" ? part.output : null;
126
+ const prompt = part.input?.prompt;
127
+ if (part.state === "output-error" || output?.error) {
128
+ return _jsx("div", { className: "ai-tool ai-tool-failed", children: _jsxs("p", { className: "ai-tool-error", children: [t("ext.ai.picture.failed"), " ", part.state === "output-error" ? part.errorText : output?.error?.message] }) });
129
+ }
130
+ if (!output?.image) {
131
+ return _jsxs("div", { className: "ai-picture ai-picture-working", role: "status", children: [_jsx("span", { className: "ai-spinner", "aria-hidden": "true" }), _jsx("span", { className: "ai-shimmer", children: t("ext.ai.picture.drawing") })] });
132
+ }
133
+ const { url, name } = output.image;
134
+ const add = () => {
135
+ setAdded("adding");
136
+ addToFiles(url, name).then(() => setAdded("yes"), (error) => setAdded(error instanceof Error ? error.message : t("ext.ai.picture.addFailed")));
137
+ };
138
+ return (_jsxs("figure", { className: "ai-picture", children: [_jsx("a", { href: url, target: "_blank", rel: "noreferrer", children: _jsx("img", { src: url, alt: typeof prompt === "string" ? prompt : "", loading: "lazy" }) }), _jsxs("figcaption", { className: "ai-picture-bar", children: [_jsxs("a", { className: "ai-ghost", href: url, download: name, children: [_jsx(Download, {}), _jsx("span", { children: t("ext.ai.picture.download") })] }), added === "yes"
139
+ ? _jsxs("a", { className: "ai-ghost", href: adminPath("/files"), children: [_jsx(Check, {}), _jsx("span", { children: t("ext.ai.picture.added") })] })
140
+ : _jsxs("button", { type: "button", className: "ai-ghost", disabled: added === "adding", onClick: add, children: [_jsx(FileIcon, {}), _jsx("span", { children: added === "adding" ? t("ext.ai.picture.adding") : t("ext.ai.picture.addToFiles") })] }), added !== "no" && added !== "adding" && added !== "yes" && _jsx("span", { className: "ai-tool-error", role: "alert", children: added })] })] }));
141
+ }
@@ -0,0 +1,34 @@
1
+ import type { PluginStore } from "tansy/plugin";
2
+ /**
3
+ * Files each person attached to their chats, and pictures the assistant drew for them, in the
4
+ * plugin's store under a key only that person's id leads to. Kept in pieces, as a stored value
5
+ * is at most 256 KiB, and served back only to their owner.
6
+ */
7
+ export declare const ATTACHMENT_ID: RegExp;
8
+ /** Kept per person. Adding one more drops the oldest. */
9
+ export declare const MAX_KEPT = 300;
10
+ export interface AttachmentMeta {
11
+ mediaType: string;
12
+ name: string;
13
+ bytes: number;
14
+ chunks: number;
15
+ createdAt: string;
16
+ drawn?: boolean;
17
+ }
18
+ /** The attachment ids a chat's messages point at. */
19
+ export declare const attachmentIds: (value: unknown) => string[];
20
+ /** The id in an address the chat gave out, such as "/api/ext/ai/attachments/<id>", or null for any other address. */
21
+ export declare function attachmentIdOf(url: string): string | null;
22
+ export declare function attachmentStore(store: PluginStore): {
23
+ meta: (userId: string, id: string) => Promise<AttachmentMeta | null>;
24
+ get: (userId: string, id: string) => Promise<(AttachmentMeta & {
25
+ data: Buffer;
26
+ }) | null>;
27
+ put: (userId: string, file: {
28
+ data: Uint8Array;
29
+ mediaType: string;
30
+ name: string;
31
+ drawn?: boolean;
32
+ }) => Promise<string>;
33
+ remove: (userId: string, id: string) => Promise<void>;
34
+ };
package/attachments.js ADDED
@@ -0,0 +1,66 @@
1
+ import { createHash, randomBytes } from "node:crypto";
2
+ import { MAX_DRAWN_BYTES } from "./media";
3
+ /**
4
+ * Files each person attached to their chats, and pictures the assistant drew for them, in the
5
+ * plugin's store under a key only that person's id leads to. Kept in pieces, as a stored value
6
+ * is at most 256 KiB, and served back only to their owner.
7
+ */
8
+ export const ATTACHMENT_ID = /^[a-z0-9]{20}$/;
9
+ /** Kept per person. Adding one more drops the oldest. */
10
+ export const MAX_KEPT = 300;
11
+ const CHUNK = 180 * 1024;
12
+ /** The person's id as a store key: ids from sign-in providers can hold any character. */
13
+ const owner = (userId) => createHash("sha256").update(`attachment:${userId}`).digest("hex").slice(0, 32);
14
+ const metaKey = (userId, id) => `attmeta/${owner(userId)}/${id}`;
15
+ const chunkKey = (userId, id, index) => `att/${owner(userId)}/${id}/${index}`;
16
+ const newId = () => Array.from(randomBytes(20), (byte) => "abcdefghijklmnopqrstuvwxyz0123456789"[byte % 36]).join("");
17
+ /** The attachment ids a chat's messages point at. */
18
+ export const attachmentIds = (value) => [...new Set([...JSON.stringify(value ?? null).matchAll(/\/ext\/ai\/attachments\/([a-z0-9]{20})/g)].map((match) => match[1]))];
19
+ /** The id in an address the chat gave out, such as "/api/ext/ai/attachments/<id>", or null for any other address. */
20
+ export function attachmentIdOf(url) {
21
+ const match = /^\/[\w/-]*\/ext\/ai\/attachments\/([a-z0-9]{20})$/.exec(url);
22
+ return match ? match[1] : null;
23
+ }
24
+ export function attachmentStore(store) {
25
+ async function meta(userId, id) {
26
+ if (!ATTACHMENT_ID.test(id))
27
+ return null;
28
+ const value = (await store.get(metaKey(userId, id)));
29
+ return value && typeof value.chunks === "number" ? value : null;
30
+ }
31
+ async function get(userId, id) {
32
+ const found = await meta(userId, id);
33
+ if (!found)
34
+ return null;
35
+ const parts = [];
36
+ for (let index = 0; index < found.chunks; index++)
37
+ parts.push(String((await store.get(chunkKey(userId, id, index))) ?? ""));
38
+ return { ...found, data: Buffer.from(parts.join(""), "base64") };
39
+ }
40
+ async function remove(userId, id) {
41
+ const found = await meta(userId, id);
42
+ if (!found)
43
+ return;
44
+ for (let index = 0; index < found.chunks; index++)
45
+ await store.delete(chunkKey(userId, id, index));
46
+ await store.delete(metaKey(userId, id));
47
+ }
48
+ async function put(userId, file) {
49
+ if (file.data.byteLength > MAX_DRAWN_BYTES)
50
+ throw new Error("The file is too large to keep.");
51
+ const id = newId();
52
+ const text = Buffer.from(file.data).toString("base64");
53
+ const chunks = Math.ceil(text.length / CHUNK);
54
+ for (let index = 0; index < chunks; index++)
55
+ await store.set(chunkKey(userId, id, index), text.slice(index * CHUNK, (index + 1) * CHUNK));
56
+ await store.set(metaKey(userId, id), { mediaType: file.mediaType, name: file.name.slice(0, 120), bytes: file.data.byteLength, chunks, createdAt: new Date().toISOString(), ...(file.drawn ? { drawn: true } : {}) });
57
+ const all = await store.list(`attmeta/${owner(userId)}/`);
58
+ if (all.length > MAX_KEPT) {
59
+ const oldest = all.map(({ key, value }) => ({ id: key.slice(key.lastIndexOf("/") + 1), at: String(value.createdAt) })).sort((a, b) => a.at.localeCompare(b.at));
60
+ for (const old of oldest.slice(0, all.length - MAX_KEPT))
61
+ await remove(userId, old.id);
62
+ }
63
+ return id;
64
+ }
65
+ return { meta, get, put, remove };
66
+ }
package/chats.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import type { UIMessage } from "ai";
2
+ import type { PluginStore } from "tansy/plugin";
3
+ /**
4
+ * Each person's assistant conversations, in the plugin's store: a JSON value per chat,
5
+ * under a key only that person's id leads to. Nobody else's chats can be named, read
6
+ * or deleted, whatever permissions they hold.
7
+ */
8
+ /** A chat id as the browser makes it. */
9
+ export declare const CHAT_ID: RegExp;
10
+ /** Kept per person. Saving one more drops the least recently used. */
11
+ export declare const MAX_CHATS = 100;
12
+ export interface ChatSummary {
13
+ id: string;
14
+ title: string;
15
+ updatedAt: string;
16
+ }
17
+ export interface StoredChat extends ChatSummary {
18
+ messages: UIMessage[];
19
+ }
20
+ /** The first thing the person asked, on one line. */
21
+ export declare function titleOf(messages: UIMessage[]): string;
22
+ /**
23
+ * The whole conversation after a turn. The browser sends only its latest messages, so the
24
+ * stored ones before them are kept. Anything stored from where they start is replaced by
25
+ * the turn's messages, which also drops an answer the person asked to try again.
26
+ */
27
+ export declare function mergeHistory(stored: UIMessage[], finished: UIMessage[]): UIMessage[];
28
+ /** Drops the oldest messages until the chat fits in one stored value. */
29
+ export declare function fitted(messages: UIMessage[]): UIMessage[];
30
+ export declare function chatStore(store: PluginStore): {
31
+ list: (userId: string) => Promise<ChatSummary[]>;
32
+ get: (userId: string, id: string) => Promise<StoredChat | null>;
33
+ save: (userId: string, id: string, finished: UIMessage[]) => Promise<void>;
34
+ remove: (userId: string, id: string) => Promise<void>;
35
+ };