@tensorpass/aiconnect-react 0.1.7 → 0.1.8

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.
@@ -96,6 +96,6 @@ export function Composer(props) {
96
96
  }
97
97
  }, [dictating, fieldRef]);
98
98
  const note = attachments?.note ?? voice.error;
99
- return (_jsxs("form", { className: "aic-composer", onSubmit: question.onSubmit, children: [note ? _jsx("p", { className: "aic-composer-note", role: "alert", children: note }) : null, _jsxs("div", { className: "aic-field", "data-dictating": dictating || undefined, children: [attachments && reads ? _jsx(ComposerAttachments, { attachments: attachments, reads: reads }) : null, dictating ? _jsx(DictationBar, { dictation: voice }) : (_jsxs(_Fragment, { children: [attachments?.policy ? _jsx(AttachButton, { policy: attachments.policy, onFiles: attachments.add }) : null, _jsx("textarea", { ref: fieldRef, className: "aic-field-input", rows: 1, dir: "auto", value: question.text, placeholder: placeholder, "aria-label": placeholder, onKeyDown: question.onKeyDown, onChange: question.onChange, onPaste: pasteHandler(attachments) }), voiceOn && !busy ? _jsx(MicButton, { onClick: () => void voice.start() }) : null, busy ? _jsx(StopButton, { onStop: onStop }) : (_jsx("button", { type: "submit", className: "aic-send", disabled: !question.canSend, "aria-label": moduleMessages.send, ...tracking, children: _jsx(SendIcon, {}) }))] }))] })] }));
99
+ return (_jsxs("form", { className: "aic-composer", onSubmit: question.onSubmit, children: [note ? _jsx("p", { className: "aic-composer-note", role: "alert", children: note }) : null, _jsxs("div", { className: "aic-field", "data-dictating": dictating || undefined, children: [attachments && reads ? _jsx(ComposerAttachments, { attachments: attachments, reads: reads }) : null, dictating ? _jsx(DictationBar, { dictation: voice }) : (_jsxs(_Fragment, { children: [attachments?.policy ? _jsx(AttachButton, { policy: attachments.policy, onFiles: attachments.add }) : null, _jsx("textarea", { ref: fieldRef, className: "aic-field-input", name: "question", rows: 1, dir: "auto", value: question.text, placeholder: placeholder, "aria-label": placeholder, onKeyDown: question.onKeyDown, onChange: question.onChange, onPaste: pasteHandler(attachments) }), voiceOn && !busy ? _jsx(MicButton, { onClick: () => void voice.start() }) : null, busy ? _jsx(StopButton, { onStop: onStop }) : (_jsx("button", { type: "submit", className: "aic-send", disabled: !question.canSend, "aria-label": moduleMessages.send, ...tracking, children: _jsx(SendIcon, {}) }))] }))] })] }));
100
100
  }
101
101
  //# sourceMappingURL=Composer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Composer.js","sourceRoot":"","sources":["../../../src/modules/parts/Composer.tsx"],"names":[],"mappings":";AAAA,wGAAwG;AACxG,0GAA0G;AAC1G,2GAA2G;AAC3G,+GAA+G;AAE/G,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAI5C,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA6B,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,+BAAgC,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAkC,CAAC;AAEvE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA8B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,0BAA2B,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAa,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAgB1D,0DAA0D;AAC1D,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B,sGAAsG;AACtG,gEAAgE;AAChE,MAAM,UAAU,SAAS,CAAC,KAAiC;IACzD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO;IACT,CAAC;IACD,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;IAC5B,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAC7C,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,GAAG,I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G,IACD,CACR,CAAC;AACJ,CAAC","sourcesContent":["// The visitor's question box: it grows with the text, sends with Enter (Shift and Enter add a line) and\n// turns its send button into Stop while the answer streams. When the site takes attachments, it shows the\n// question's files above the text, takes files pasted into it, and offers the attach button; when the site\n// takes voice, its microphone button turns the box into the dictation bar, whose transcript lands in the text.\n\nimport { useEffect, useState } from 'react';\nimport type { ChangeEvent, ClipboardEvent, FormEvent, KeyboardEvent, RefObject } from 'react';\nimport type { Dictation, ReadableParts } from '@tensorpass/aiconnect';\nimport type { PreparedAttachment } from '@tensorpass/aiconnect/attachments';\nimport { useAiConnect } from '../../AiConnectProvider.tsx';\nimport { AttachButton } from './attachments/AttachButton.tsx';\nimport { ComposerAttachments } from './attachments/AttachmentRows.tsx';\nimport type { Attachments } from './attachments/useAttachments.ts';\nimport { DictationBar } from './dictation/DictationBar.tsx';\nimport { MicButton, useVoiceAvailable } from './dictation/MicButton.tsx';\nimport { useDictation } from './dictation/useDictation.ts';\nimport { SendIcon } from './icons.tsx';\nimport { useActionAttribute } from './moduleAnalytics.ts';\n\nexport interface ComposerProps {\n busy: boolean;\n placeholder: string;\n onSend: (question: string, attachments: PreparedAttachment[]) => void;\n onStop: () => void;\n fieldRef: RefObject<HTMLTextAreaElement | null>;\n // The question's files, when the site takes attachments.\n attachments?: Attachments;\n reads?: ReadableParts;\n // The account's dictation, and whether the site takes voice.\n dictation?: Dictation | null;\n voice?: boolean;\n}\n\n// The tallest the box grows before it scrolls, in pixels.\nconst MAX_FIELD_HEIGHT = 168;\n\n// Fits the box to its text, rounded up to a whole pixel so a single line never shows a scrollbar, and\n// lets it scroll once the text is taller than the box may grow.\nexport function fitHeight(field: HTMLTextAreaElement | null): void {\n if (!field) {\n return;\n }\n field.style.height = 'auto';\n const needed = Math.ceil(field.scrollHeight);\n field.style.height = `${Math.min(needed, MAX_FIELD_HEIGHT)}px`;\n field.style.overflowY = needed > MAX_FIELD_HEIGHT ? 'auto' : 'hidden';\n}\n\nexport function StopButton({ onStop }: { onStop: () => void }) {\n const { moduleMessages } = useAiConnect();\n return (\n <button type=\"button\" className=\"aic-stop\" onClick={onStop}>\n <i aria-hidden=\"true\" />{moduleMessages.stop}\n </button>\n );\n}\n\ntype QuestionTextOptions = Pick<ComposerProps, 'busy' | 'onSend' | 'fieldRef' | 'attachments'>;\n\n// The text of the question box and its keys: Enter sends unless an answer is streaming or a file is still\n// read, and the box shrinks back once the question is sent. A dictation's transcript joins the text.\nfunction useQuestionText({ busy, onSend, fieldRef, attachments }: QuestionTextOptions) {\n const [text, setText] = useState('');\n const ready = attachments?.ready ?? [];\n const canSend = !busy && !attachments?.reading && (Boolean(text.trim()) || ready.length > 0);\n const submit = () => {\n if (!canSend) {\n return;\n }\n onSend(text, ready);\n setText('');\n attachments?.clear();\n if (fieldRef.current) {\n fieldRef.current.style.height = '';\n fieldRef.current.style.overflowY = '';\n }\n };\n return {\n text,\n canSend,\n insert: (words: string) => setText((current) => [current.trimEnd(), words].filter(Boolean).join(' ')),\n onChange: (event: ChangeEvent<HTMLTextAreaElement>) => {\n setText(event.target.value);\n fitHeight(event.target);\n },\n onSubmit: (event: FormEvent) => {\n event.preventDefault();\n submit();\n },\n onKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => {\n if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) {\n event.preventDefault();\n submit();\n }\n },\n };\n}\n\n// Files pasted into the box join the question; the pasted text, if any, goes in the box as usual.\nfunction pasteHandler(attachments: Attachments | undefined) {\n return (event: ClipboardEvent<HTMLTextAreaElement>) => {\n const files = [...event.clipboardData.files];\n if (!attachments || files.length === 0) {\n return;\n }\n attachments.add(files);\n if (!event.clipboardData.types.includes('text/plain')) {\n event.preventDefault();\n }\n };\n}\n\nexport function Composer(props: ComposerProps) {\n const { busy, placeholder, onStop, fieldRef, attachments, reads, dictation } = props;\n const { moduleMessages } = useAiConnect();\n const question = useQuestionText(props);\n const voice = useDictation(dictation ?? null, question.insert);\n const voiceOn = useVoiceAvailable(dictation, props.voice);\n const tracking = useActionAttribute('send');\n const dictating = voice.status !== 'idle';\n // Back from the dictation bar, the box takes the focus and the height of its text.\n useEffect(() => {\n if (!dictating && fieldRef.current && fieldRef.current.value) {\n fitHeight(fieldRef.current);\n fieldRef.current.focus();\n }\n }, [dictating, fieldRef]);\n const note = attachments?.note ?? voice.error;\n return (\n <form className=\"aic-composer\" onSubmit={question.onSubmit}>\n {note ? <p className=\"aic-composer-note\" role=\"alert\">{note}</p> : null}\n <div className=\"aic-field\" data-dictating={dictating || undefined}>\n {attachments && reads ? <ComposerAttachments attachments={attachments} reads={reads} /> : null}\n {dictating ? <DictationBar dictation={voice} /> : (\n <>\n {attachments?.policy ? <AttachButton policy={attachments.policy} onFiles={attachments.add} /> : null}\n <textarea ref={fieldRef} className=\"aic-field-input\" rows={1} dir=\"auto\" value={question.text}\n placeholder={placeholder} aria-label={placeholder} onKeyDown={question.onKeyDown}\n onChange={question.onChange} onPaste={pasteHandler(attachments)} />\n {voiceOn && !busy ? <MicButton onClick={() => void voice.start()} /> : null}\n {busy ? <StopButton onStop={onStop} /> : (\n <button type=\"submit\" className=\"aic-send\" disabled={!question.canSend} aria-label={moduleMessages.send}\n {...tracking}>\n <SendIcon />\n </button>\n )}\n </>\n )}\n </div>\n </form>\n );\n}\n"]}
1
+ {"version":3,"file":"Composer.js","sourceRoot":"","sources":["../../../src/modules/parts/Composer.tsx"],"names":[],"mappings":";AAAA,wGAAwG;AACxG,0GAA0G;AAC1G,2GAA2G;AAC3G,+GAA+G;AAE/G,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAI5C,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA6B,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,+BAAgC,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAkC,CAAC;AAEvE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA8B,CAAC;AAC5D,OAAO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E,WAAW,EAAE,WAAW,gBAAc,WAAW,EAAE,SAAS,EAAE,QAAQ,CAAC,SAAS,EACtG,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,OAAO,EAAE,YAAY,CAAC,WAAW,CAAC,GAAI,EACpE,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,KAAC,SAAS,IAAC,OAAO,EAAE,GAAG,EAAE,CAAC,KAAK,KAAK,CAAC,KAAK,EAAE,GAAI,CAAC,CAAC,CAAC,IAAI,EAC1E,IAAI,CAAC,CAAC,CAAC,KAAC,UAAU,IAAC,MAAM,EAAE,MAAM,GAAI,CAAC,CAAC,CAAC,CACvC,iBAAQ,IAAI,EAAC,QAAQ,EAAC,SAAS,EAAC,UAAU,EAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,OAAO,gBAAc,cAAc,CAAC,IAAI,KACjG,QAAQ,YACZ,KAAC,QAAQ,KAAG,GACL,CACV,IACA,CACJ,IACG,IACD,CACR,CAAC;AACJ,CAAC","sourcesContent":["// The visitor's question box: it grows with the text, sends with Enter (Shift and Enter add a line) and\n// turns its send button into Stop while the answer streams. When the site takes attachments, it shows the\n// question's files above the text, takes files pasted into it, and offers the attach button; when the site\n// takes voice, its microphone button turns the box into the dictation bar, whose transcript lands in the text.\n\nimport { useEffect, useState } from 'react';\nimport type { ChangeEvent, ClipboardEvent, FormEvent, KeyboardEvent, RefObject } from 'react';\nimport type { Dictation, ReadableParts } from '@tensorpass/aiconnect';\nimport type { PreparedAttachment } from '@tensorpass/aiconnect/attachments';\nimport { useAiConnect } from '../../AiConnectProvider.tsx';\nimport { AttachButton } from './attachments/AttachButton.tsx';\nimport { ComposerAttachments } from './attachments/AttachmentRows.tsx';\nimport type { Attachments } from './attachments/useAttachments.ts';\nimport { DictationBar } from './dictation/DictationBar.tsx';\nimport { MicButton, useVoiceAvailable } from './dictation/MicButton.tsx';\nimport { useDictation } from './dictation/useDictation.ts';\nimport { SendIcon } from './icons.tsx';\nimport { useActionAttribute } from './moduleAnalytics.ts';\n\nexport interface ComposerProps {\n busy: boolean;\n placeholder: string;\n onSend: (question: string, attachments: PreparedAttachment[]) => void;\n onStop: () => void;\n fieldRef: RefObject<HTMLTextAreaElement | null>;\n // The question's files, when the site takes attachments.\n attachments?: Attachments;\n reads?: ReadableParts;\n // The account's dictation, and whether the site takes voice.\n dictation?: Dictation | null;\n voice?: boolean;\n}\n\n// The tallest the box grows before it scrolls, in pixels.\nconst MAX_FIELD_HEIGHT = 168;\n\n// Fits the box to its text, rounded up to a whole pixel so a single line never shows a scrollbar, and\n// lets it scroll once the text is taller than the box may grow.\nexport function fitHeight(field: HTMLTextAreaElement | null): void {\n if (!field) {\n return;\n }\n field.style.height = 'auto';\n const needed = Math.ceil(field.scrollHeight);\n field.style.height = `${Math.min(needed, MAX_FIELD_HEIGHT)}px`;\n field.style.overflowY = needed > MAX_FIELD_HEIGHT ? 'auto' : 'hidden';\n}\n\nexport function StopButton({ onStop }: { onStop: () => void }) {\n const { moduleMessages } = useAiConnect();\n return (\n <button type=\"button\" className=\"aic-stop\" onClick={onStop}>\n <i aria-hidden=\"true\" />{moduleMessages.stop}\n </button>\n );\n}\n\ntype QuestionTextOptions = Pick<ComposerProps, 'busy' | 'onSend' | 'fieldRef' | 'attachments'>;\n\n// The text of the question box and its keys: Enter sends unless an answer is streaming or a file is still\n// read, and the box shrinks back once the question is sent. A dictation's transcript joins the text.\nfunction useQuestionText({ busy, onSend, fieldRef, attachments }: QuestionTextOptions) {\n const [text, setText] = useState('');\n const ready = attachments?.ready ?? [];\n const canSend = !busy && !attachments?.reading && (Boolean(text.trim()) || ready.length > 0);\n const submit = () => {\n if (!canSend) {\n return;\n }\n onSend(text, ready);\n setText('');\n attachments?.clear();\n if (fieldRef.current) {\n fieldRef.current.style.height = '';\n fieldRef.current.style.overflowY = '';\n }\n };\n return {\n text,\n canSend,\n insert: (words: string) => setText((current) => [current.trimEnd(), words].filter(Boolean).join(' ')),\n onChange: (event: ChangeEvent<HTMLTextAreaElement>) => {\n setText(event.target.value);\n fitHeight(event.target);\n },\n onSubmit: (event: FormEvent) => {\n event.preventDefault();\n submit();\n },\n onKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => {\n if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) {\n event.preventDefault();\n submit();\n }\n },\n };\n}\n\n// Files pasted into the box join the question; the pasted text, if any, goes in the box as usual.\nfunction pasteHandler(attachments: Attachments | undefined) {\n return (event: ClipboardEvent<HTMLTextAreaElement>) => {\n const files = [...event.clipboardData.files];\n if (!attachments || files.length === 0) {\n return;\n }\n attachments.add(files);\n if (!event.clipboardData.types.includes('text/plain')) {\n event.preventDefault();\n }\n };\n}\n\nexport function Composer(props: ComposerProps) {\n const { busy, placeholder, onStop, fieldRef, attachments, reads, dictation } = props;\n const { moduleMessages } = useAiConnect();\n const question = useQuestionText(props);\n const voice = useDictation(dictation ?? null, question.insert);\n const voiceOn = useVoiceAvailable(dictation, props.voice);\n const tracking = useActionAttribute('send');\n const dictating = voice.status !== 'idle';\n // Back from the dictation bar, the box takes the focus and the height of its text.\n useEffect(() => {\n if (!dictating && fieldRef.current && fieldRef.current.value) {\n fitHeight(fieldRef.current);\n fieldRef.current.focus();\n }\n }, [dictating, fieldRef]);\n const note = attachments?.note ?? voice.error;\n return (\n <form className=\"aic-composer\" onSubmit={question.onSubmit}>\n {note ? <p className=\"aic-composer-note\" role=\"alert\">{note}</p> : null}\n <div className=\"aic-field\" data-dictating={dictating || undefined}>\n {attachments && reads ? <ComposerAttachments attachments={attachments} reads={reads} /> : null}\n {dictating ? <DictationBar dictation={voice} /> : (\n <>\n {attachments?.policy ? <AttachButton policy={attachments.policy} onFiles={attachments.add} /> : null}\n <textarea ref={fieldRef} className=\"aic-field-input\" name=\"question\" rows={1} dir=\"auto\"\n value={question.text} placeholder={placeholder} aria-label={placeholder} onKeyDown={question.onKeyDown}\n onChange={question.onChange} onPaste={pasteHandler(attachments)} />\n {voiceOn && !busy ? <MicButton onClick={() => void voice.start()} /> : null}\n {busy ? <StopButton onStop={onStop} /> : (\n <button type=\"submit\" className=\"aic-send\" disabled={!question.canSend} aria-label={moduleMessages.send}\n {...tracking}>\n <SendIcon />\n </button>\n )}\n </>\n )}\n </div>\n </form>\n );\n}\n"]}
@@ -1,10 +1,11 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- // The conversation of a chat module. Empty, it shows its heading, the connection step and the
3
- // suggested questions, under the wallet's ring in the full page mode. Then each turn shows the
4
- // visitor's question with its files, the site's tools as they run, the answer as it streams and the site's own
5
- // result cards, and the last answer's error comes with a way to ask again, or, for a TensorPass account
6
- // without a plan, the offer to subscribe. The view follows the answer down while the visitor stays at the
7
- // bottom.
2
+ // The conversation of a chat module. Empty, it shows its heading, the suggested questions, then the
3
+ // connection step, under the wallet's ring in the full page mode. The step appears once the first wallet
4
+ // check ends, so it comes last and the heading and the questions the page rendered stay in place. Then
5
+ // each turn shows the visitor's question with its files, the site's tools as they run, the answer as it
6
+ // streams and the site's own result cards, and the last answer's error comes with a way to ask again,
7
+ // or, for a TensorPass account without a plan, the offer to subscribe. The view follows the answer down
8
+ // while the visitor stays at the bottom.
8
9
  import { useEffect, useRef } from 'react';
9
10
  import { useAiConnect } from "../../AiConnectProvider.js";
10
11
  import { ConnectRing } from "../../ConnectRing.js";
@@ -66,6 +67,6 @@ export function Conversation({ chat, intro, suggestions, ...view }) {
66
67
  const logRef = useRef(null);
67
68
  const onScroll = useFollowAnswer(logRef, chat);
68
69
  const empty = chat.turns.length === 0 && !chat.pendingQuestion;
69
- return (_jsxs("div", { ref: logRef, className: "aic-log", role: "log", "aria-busy": chat.busy, onScroll: onScroll, children: [empty ? (_jsxs("div", { className: "aic-empty", children: [_jsx("div", { className: "aic-empty-ring", children: _jsx(ConnectRing, { live: false }) }), _jsx("h3", { className: "aic-intro", children: intro }), _jsx(ConnectStep, { pending: false }), _jsx(Suggestions, { items: suggestions, onPick: chat.ask })] })) : null, _jsxs("ol", { className: "aic-turns", children: [chat.turns.map((turn, index) => (_jsxs("li", { className: "aic-turn", children: [_jsx(Question, { text: turn.question, attachments: turn.attachments, unread: turn.unread }), _jsx(TurnBody, { turn: turn, view: view, stopped: chat.stopped.includes(index), linkAllowed: chat.linkAllowed, live: chat.busy && index === chat.turns.length - 1 })] }, index))), chat.pendingQuestion ? (_jsxs("li", { className: "aic-turn", children: [_jsx(Question, { text: chat.pendingQuestion.text, attachments: chat.pendingQuestion.attachments }), _jsx(ConnectStep, { pending: true })] })) : null] }), chat.refusal === PLAN_INACTIVE ? _jsx(PlanOffer, { onActive: chat.retry }) : null, chat.error && chat.refusal !== PLAN_INACTIVE ? (_jsxs("div", { className: "aic-error", role: "alert", children: [_jsx("p", { children: chat.error }), _jsx("button", { type: "button", className: "aic-text-button", onClick: chat.retry, children: moduleMessages.retry })] })) : null] }));
70
+ return (_jsxs("div", { ref: logRef, className: "aic-log", role: "log", "aria-busy": chat.busy, onScroll: onScroll, children: [empty ? (_jsxs("div", { className: "aic-empty", children: [_jsx("div", { className: "aic-empty-ring", children: _jsx(ConnectRing, { live: false }) }), _jsx("h3", { className: "aic-intro", children: intro }), _jsx(Suggestions, { items: suggestions, onPick: chat.ask }), _jsx(ConnectStep, { pending: false })] })) : null, _jsxs("ol", { className: "aic-turns", children: [chat.turns.map((turn, index) => (_jsxs("li", { className: "aic-turn", children: [_jsx(Question, { text: turn.question, attachments: turn.attachments, unread: turn.unread }), _jsx(TurnBody, { turn: turn, view: view, stopped: chat.stopped.includes(index), linkAllowed: chat.linkAllowed, live: chat.busy && index === chat.turns.length - 1 })] }, index))), chat.pendingQuestion ? (_jsxs("li", { className: "aic-turn", children: [_jsx(Question, { text: chat.pendingQuestion.text, attachments: chat.pendingQuestion.attachments }), _jsx(ConnectStep, { pending: true })] })) : null] }), chat.refusal === PLAN_INACTIVE ? _jsx(PlanOffer, { onActive: chat.retry }) : null, chat.error && chat.refusal !== PLAN_INACTIVE ? (_jsxs("div", { className: "aic-error", role: "alert", children: [_jsx("p", { children: chat.error }), _jsx("button", { type: "button", className: "aic-text-button", onClick: chat.retry, children: moduleMessages.retry })] })) : null] }));
70
71
  }
71
72
  //# sourceMappingURL=Conversation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Conversation.js","sourceRoot":"","sources":["../../../src/modules/parts/Conversation.tsx"],"names":[],"mappings":";AAAA,8FAA8F;AAC9F,+FAA+F;AAC/F,+GAA+G;AAC/G,wGAAwG;AACxG,0GAA0G;AAC1G,UAAU;AAEV,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA6B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAuB,CAAC;AAIpD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAkB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAkC,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAiB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAa,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAoB,CAAC;AAatD,6EAA6E;AAC7E,MAAM,aAAa,GAAG,eAAe,CAAC;AAEtC,kFAAkF;AAClF,MAAM,eAAe,GAAG,EAAE,CAAC;AAS3B,SAAS,QAAQ,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAiB;IAC5D,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,GAAG,YAAY,EAAE,CAAC;IACjD,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC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AC,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,eAAe,CAAC,WAAW,GAAI,EAC5F,KAAC,WAAW,IAAC,OAAO,SAAG,IACpB,CACN,CAAC,CAAC,CAAC,IAAI,IACL,EACJ,IAAI,CAAC,OAAO,KAAK,aAAa,CAAC,CAAC,CAAC,KAAC,SAAS,IAAC,QAAQ,EAAE,IAAI,CAAC,KAAK,GAAI,CAAC,CAAC,CAAC,IAAI,EAC3E,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,OAAO,KAAK,aAAa,CAAC,CAAC,CAAC,CAC9C,eAAK,SAAS,EAAC,WAAW,EAAC,IAAI,EAAC,OAAO,aACrC,sBAAI,IAAI,CAAC,KAAK,GAAK,EACnB,iBAAQ,IAAI,EAAC,QAAQ,EAAC,SAAS,EAAC,iBAAiB,EAAC,OAAO,EAAE,IAAI,CAAC,KAAK,YAAG,cAAc,CAAC,KAAK,GAAU,IAClG,CACP,CAAC,CAAC,CAAC,IAAI,IACJ,CACP,CAAC;AACJ,CAAC","sourcesContent":["// The conversation of a chat module. Empty, it shows its heading, the connection step and the\n// suggested questions, under the wallet's ring in the full page mode. Then each turn shows the\n// visitor's question with its files, the site's tools as they run, the answer as it streams and the site's own\n// result cards, and the last answer's error comes with a way to ask again, or, for a TensorPass account\n// without a plan, the offer to subscribe. The view follows the answer down while the visitor stays at the\n// bottom.\n\nimport { useEffect, useRef } from 'react';\nimport type { RefObject } from 'react';\nimport { useAiConnect } from '../../AiConnectProvider.tsx';\nimport { ConnectRing } from '../../ConnectRing.tsx';\nimport type { PreparedAttachment } from '@tensorpass/aiconnect/attachments';\nimport type { ChatTurn } from '../../useAiChat.ts';\nimport type { AiModuleProps } from '../types.ts';\nimport { AnswerText } from './AnswerText.tsx';\nimport { QuestionAttachments } from './attachments/AttachmentRows.tsx';\nimport { ConnectStep } from './ConnectStep.tsx';\nimport { PlanOffer } from './PlanOffer.tsx';\nimport { useActionAttribute } from './moduleAnalytics.ts';\nimport { ArrowIcon, WaveIcon } from './icons.tsx';\nimport { ToolActivityList } from './ToolActivity.tsx';\nimport type { ModuleChat } from './useModuleChat.ts';\n\nexport type TurnView = Pick<AiModuleProps, 'toolLabels' | 'renderToolResults'>;\n\nexport interface TurnBodyProps {\n turn: ChatTurn;\n live: boolean;\n stopped: boolean;\n view: TurnView;\n linkAllowed: (href: string) => boolean;\n}\n\n// The plan gateway's refusal of a TensorPass account without a subscription.\nconst PLAN_INACTIVE = 'plan_inactive';\n\n// How close to the bottom, in pixels, the visitor counts as following the answer.\nconst FOLLOW_DISTANCE = 56;\n\ninterface QuestionProps {\n text: string;\n attachments?: PreparedAttachment[];\n // The images and pages the answering model did not read.\n unread?: number;\n}\n\nfunction Question({ text, attachments, unread }: QuestionProps) {\n const { moduleMessages, state } = useAiConnect();\n const account = state.status === 'connected' ? state.providerName : '';\n return (\n <>\n {attachments?.length ? <QuestionAttachments attachments={attachments} /> : null}\n {text ? (\n <p className=\"aic-question\" dir=\"auto\">\n <span className=\"aic-visually-hidden\">{moduleMessages.question}: </span>{text}\n </p>\n ) : null}\n {unread ? (\n <p className=\"aic-unread\">\n {moduleMessages.unreadPages.replaceAll('{count}', String(unread)).replaceAll('{account}', account)}\n </p>\n ) : null}\n </>\n );\n}\n\n// What a turn shows under its question: the tools as they run, a thinking line until the first\n// words arrive, the answer, whether the visitor stopped it, and the site's own result cards.\nexport function TurnBody({ turn, live, stopped, view, linkAllowed }: TurnBodyProps) {\n const { moduleMessages } = useAiConnect();\n const toolRunning = turn.tools.some((activity) => activity.result === null);\n const results = view.renderToolResults?.(turn);\n return (\n <>\n <ToolActivityList tools={turn.tools} labels={view.toolLabels} />\n {live && !turn.answer && !toolRunning ? (\n <p className=\"aic-thinking\"><WaveIcon size={16} className=\"aic-live-arcs\" />{moduleMessages.thinking}</p>\n ) : null}\n <AnswerText text={turn.answer} streaming={live && Boolean(turn.answer)} linkAllowed={linkAllowed}\n blocked={Boolean(turn.blocked)} />\n {stopped ? <p className=\"aic-stopped\"><i aria-hidden=\"true\" />{moduleMessages.stopped}</p> : null}\n {results ? <div className=\"aic-results\">{results}</div> : null}\n </>\n );\n}\n\nexport function Suggestions({ items, onPick }: { items?: string[]; onPick: (question: string) => void }) {\n const { moduleMessages } = useAiConnect();\n const tracking = useActionAttribute('suggestion');\n if (!items?.length) {\n return null;\n }\n return (\n <ul className=\"aic-suggestions\" aria-label={moduleMessages.suggestionsLabel}>\n {items.map((question) => (\n <li key={question}>\n <button type=\"button\" className=\"aic-suggestion\" onClick={() => onPick(question)} {...tracking}>\n <span>{question}</span><ArrowIcon />\n </button>\n </li>\n ))}\n </ul>\n );\n}\n\n// Keeps the log scrolled to its end while the visitor reads at the bottom, and lets them scroll up.\n// An empty conversation stays at its top, where its heading is.\nfunction useFollowAnswer(logRef: RefObject<HTMLDivElement | null>, chat: ModuleChat) {\n const following = useRef(true);\n const asked = chat.turns.length + (chat.pendingQuestion ? 1 : 0);\n useEffect(() => {\n following.current = true;\n }, [asked]);\n useEffect(() => {\n const log = logRef.current;\n if (log && asked > 0 && following.current) {\n log.scrollTop = log.scrollHeight;\n }\n });\n return () => {\n const log = logRef.current;\n if (log) {\n following.current = log.scrollHeight - log.scrollTop - log.clientHeight < FOLLOW_DISTANCE;\n }\n };\n}\n\nexport interface ConversationProps extends TurnView {\n chat: ModuleChat;\n intro: string;\n suggestions?: string[];\n}\n\nexport function Conversation({ chat, intro, suggestions, ...view }: ConversationProps) {\n const { moduleMessages } = useAiConnect();\n const logRef = useRef<HTMLDivElement>(null);\n const onScroll = useFollowAnswer(logRef, chat);\n const empty = chat.turns.length === 0 && !chat.pendingQuestion;\n return (\n <div ref={logRef} className=\"aic-log\" role=\"log\" aria-busy={chat.busy} onScroll={onScroll}>\n {empty ? (\n <div className=\"aic-empty\">\n <div className=\"aic-empty-ring\"><ConnectRing live={false} /></div>\n <h3 className=\"aic-intro\">{intro}</h3>\n <ConnectStep pending={false} />\n <Suggestions items={suggestions} onPick={chat.ask} />\n </div>\n ) : null}\n <ol className=\"aic-turns\">\n {chat.turns.map((turn, index) => (\n <li key={index} className=\"aic-turn\">\n <Question text={turn.question} attachments={turn.attachments} unread={turn.unread} />\n <TurnBody turn={turn} view={view} stopped={chat.stopped.includes(index)} linkAllowed={chat.linkAllowed}\n live={chat.busy && index === chat.turns.length - 1} />\n </li>\n ))}\n {chat.pendingQuestion ? (\n <li className=\"aic-turn\">\n <Question text={chat.pendingQuestion.text} attachments={chat.pendingQuestion.attachments} />\n <ConnectStep pending />\n </li>\n ) : null}\n </ol>\n {chat.refusal === PLAN_INACTIVE ? <PlanOffer onActive={chat.retry} /> : null}\n {chat.error && chat.refusal !== PLAN_INACTIVE ? (\n <div className=\"aic-error\" role=\"alert\">\n <p>{chat.error}</p>\n <button type=\"button\" className=\"aic-text-button\" onClick={chat.retry}>{moduleMessages.retry}</button>\n </div>\n ) : null}\n </div>\n );\n}\n"]}
1
+ {"version":3,"file":"Conversation.js","sourceRoot":"","sources":["../../../src/modules/parts/Conversation.tsx"],"names":[],"mappings":";AAAA,oGAAoG;AACpG,yGAAyG;AACzG,uGAAuG;AACvG,wGAAwG;AACxG,sGAAsG;AACtG,wGAAwG;AACxG,yCAAyC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA6B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAuB,CAAC;AAIpD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAkB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAkC,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAiB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAa,CAA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,IAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EACpG,IAAI,EAAE,IAAI,CAAC,IAAI,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,GAAI,KAHjD,KAAK,CAIT,CACN,CAAC,EACD,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,CACtB,cAAI,SAAS,EAAC,UAAU,aACtB,KAAC,QAAQ,IAAC,IAAI,EAAE,IAAI,CAAC,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,eAAe,CAAC,WAAW,GAAI,EAC5F,KAAC,WAAW,IAAC,OAAO,SAAG,IACpB,CACN,CAAC,CAAC,CAAC,IAAI,IACL,EACJ,IAAI,CAAC,OAAO,KAAK,aAAa,CAAC,CAAC,CAAC,KAAC,SAAS,IAAC,QAAQ,EAAE,IAAI,CAAC,KAAK,GAAI,CAAC,CAAC,CAAC,IAAI,EAC3E,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,OAAO,KAAK,aAAa,CAAC,CAAC,CAAC,CAC9C,eAAK,SAAS,EAAC,WAAW,EAAC,IAAI,EAAC,OAAO,aACrC,sBAAI,IAAI,CAAC,KAAK,GAAK,EACnB,iBAAQ,IAAI,EAAC,QAAQ,EAAC,SAAS,EAAC,iBAAiB,EAAC,OAAO,EAAE,IAAI,CAAC,KAAK,YAAG,cAAc,CAAC,KAAK,GAAU,IAClG,CACP,CAAC,CAAC,CAAC,IAAI,IACJ,CACP,CAAC;AACJ,CAAC","sourcesContent":["// The conversation of a chat module. Empty, it shows its heading, the suggested questions, then the\n// connection step, under the wallet's ring in the full page mode. The step appears once the first wallet\n// check ends, so it comes last and the heading and the questions the page rendered stay in place. Then\n// each turn shows the visitor's question with its files, the site's tools as they run, the answer as it\n// streams and the site's own result cards, and the last answer's error comes with a way to ask again,\n// or, for a TensorPass account without a plan, the offer to subscribe. The view follows the answer down\n// while the visitor stays at the bottom.\n\nimport { useEffect, useRef } from 'react';\nimport type { RefObject } from 'react';\nimport { useAiConnect } from '../../AiConnectProvider.tsx';\nimport { ConnectRing } from '../../ConnectRing.tsx';\nimport type { PreparedAttachment } from '@tensorpass/aiconnect/attachments';\nimport type { ChatTurn } from '../../useAiChat.ts';\nimport type { AiModuleProps } from '../types.ts';\nimport { AnswerText } from './AnswerText.tsx';\nimport { QuestionAttachments } from './attachments/AttachmentRows.tsx';\nimport { ConnectStep } from './ConnectStep.tsx';\nimport { PlanOffer } from './PlanOffer.tsx';\nimport { useActionAttribute } from './moduleAnalytics.ts';\nimport { ArrowIcon, WaveIcon } from './icons.tsx';\nimport { ToolActivityList } from './ToolActivity.tsx';\nimport type { ModuleChat } from './useModuleChat.ts';\n\nexport type TurnView = Pick<AiModuleProps, 'toolLabels' | 'renderToolResults'>;\n\nexport interface TurnBodyProps {\n turn: ChatTurn;\n live: boolean;\n stopped: boolean;\n view: TurnView;\n linkAllowed: (href: string) => boolean;\n}\n\n// The plan gateway's refusal of a TensorPass account without a subscription.\nconst PLAN_INACTIVE = 'plan_inactive';\n\n// How close to the bottom, in pixels, the visitor counts as following the answer.\nconst FOLLOW_DISTANCE = 56;\n\ninterface QuestionProps {\n text: string;\n attachments?: PreparedAttachment[];\n // The images and pages the answering model did not read.\n unread?: number;\n}\n\nfunction Question({ text, attachments, unread }: QuestionProps) {\n const { moduleMessages, state } = useAiConnect();\n const account = state.status === 'connected' ? state.providerName : '';\n return (\n <>\n {attachments?.length ? <QuestionAttachments attachments={attachments} /> : null}\n {text ? (\n <p className=\"aic-question\" dir=\"auto\">\n <span className=\"aic-visually-hidden\">{moduleMessages.question}: </span>{text}\n </p>\n ) : null}\n {unread ? (\n <p className=\"aic-unread\">\n {moduleMessages.unreadPages.replaceAll('{count}', String(unread)).replaceAll('{account}', account)}\n </p>\n ) : null}\n </>\n );\n}\n\n// What a turn shows under its question: the tools as they run, a thinking line until the first\n// words arrive, the answer, whether the visitor stopped it, and the site's own result cards.\nexport function TurnBody({ turn, live, stopped, view, linkAllowed }: TurnBodyProps) {\n const { moduleMessages } = useAiConnect();\n const toolRunning = turn.tools.some((activity) => activity.result === null);\n const results = view.renderToolResults?.(turn);\n return (\n <>\n <ToolActivityList tools={turn.tools} labels={view.toolLabels} />\n {live && !turn.answer && !toolRunning ? (\n <p className=\"aic-thinking\"><WaveIcon size={16} className=\"aic-live-arcs\" />{moduleMessages.thinking}</p>\n ) : null}\n <AnswerText text={turn.answer} streaming={live && Boolean(turn.answer)} linkAllowed={linkAllowed}\n blocked={Boolean(turn.blocked)} />\n {stopped ? <p className=\"aic-stopped\"><i aria-hidden=\"true\" />{moduleMessages.stopped}</p> : null}\n {results ? <div className=\"aic-results\">{results}</div> : null}\n </>\n );\n}\n\nexport function Suggestions({ items, onPick }: { items?: string[]; onPick: (question: string) => void }) {\n const { moduleMessages } = useAiConnect();\n const tracking = useActionAttribute('suggestion');\n if (!items?.length) {\n return null;\n }\n return (\n <ul className=\"aic-suggestions\" aria-label={moduleMessages.suggestionsLabel}>\n {items.map((question) => (\n <li key={question}>\n <button type=\"button\" className=\"aic-suggestion\" onClick={() => onPick(question)} {...tracking}>\n <span>{question}</span><ArrowIcon />\n </button>\n </li>\n ))}\n </ul>\n );\n}\n\n// Keeps the log scrolled to its end while the visitor reads at the bottom, and lets them scroll up.\n// An empty conversation stays at its top, where its heading is.\nfunction useFollowAnswer(logRef: RefObject<HTMLDivElement | null>, chat: ModuleChat) {\n const following = useRef(true);\n const asked = chat.turns.length + (chat.pendingQuestion ? 1 : 0);\n useEffect(() => {\n following.current = true;\n }, [asked]);\n useEffect(() => {\n const log = logRef.current;\n if (log && asked > 0 && following.current) {\n log.scrollTop = log.scrollHeight;\n }\n });\n return () => {\n const log = logRef.current;\n if (log) {\n following.current = log.scrollHeight - log.scrollTop - log.clientHeight < FOLLOW_DISTANCE;\n }\n };\n}\n\nexport interface ConversationProps extends TurnView {\n chat: ModuleChat;\n intro: string;\n suggestions?: string[];\n}\n\nexport function Conversation({ chat, intro, suggestions, ...view }: ConversationProps) {\n const { moduleMessages } = useAiConnect();\n const logRef = useRef<HTMLDivElement>(null);\n const onScroll = useFollowAnswer(logRef, chat);\n const empty = chat.turns.length === 0 && !chat.pendingQuestion;\n return (\n <div ref={logRef} className=\"aic-log\" role=\"log\" aria-busy={chat.busy} onScroll={onScroll}>\n {empty ? (\n <div className=\"aic-empty\">\n <div className=\"aic-empty-ring\"><ConnectRing live={false} /></div>\n <h3 className=\"aic-intro\">{intro}</h3>\n <Suggestions items={suggestions} onPick={chat.ask} />\n <ConnectStep pending={false} />\n </div>\n ) : null}\n <ol className=\"aic-turns\">\n {chat.turns.map((turn, index) => (\n <li key={index} className=\"aic-turn\">\n <Question text={turn.question} attachments={turn.attachments} unread={turn.unread} />\n <TurnBody turn={turn} view={view} stopped={chat.stopped.includes(index)} linkAllowed={chat.linkAllowed}\n live={chat.busy && index === chat.turns.length - 1} />\n </li>\n ))}\n {chat.pendingQuestion ? (\n <li className=\"aic-turn\">\n <Question text={chat.pendingQuestion.text} attachments={chat.pendingQuestion.attachments} />\n <ConnectStep pending />\n </li>\n ) : null}\n </ol>\n {chat.refusal === PLAN_INACTIVE ? <PlanOffer onActive={chat.retry} /> : null}\n {chat.error && chat.refusal !== PLAN_INACTIVE ? (\n <div className=\"aic-error\" role=\"alert\">\n <p>{chat.error}</p>\n <button type=\"button\" className=\"aic-text-button\" onClick={chat.retry}>{moduleMessages.retry}</button>\n </div>\n ) : null}\n </div>\n );\n}\n"]}
@@ -4,22 +4,26 @@
4
4
  // visitor signs in on the page, and stays with the connection step while a sign in waits. Signing out of a
5
5
  // web account starts the conversation again from the connection step.
6
6
  import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
7
- import { McpToolSource } from '@tensorpass/aiconnect';
8
7
  import { useAiConnect } from "../../AiConnectProvider.js";
9
8
  import { useAiChat } from "../../useAiChat.js";
10
9
  // The states in which the connection is still settling on its own, without the visitor.
11
10
  const SETTLING = new Set(['checking', 'connecting']);
12
- // The site's MCP tools, connected on the first question. The module also renders on the server,
13
- // where a relative address has no page to resolve against.
11
+ // The site's MCP tools, loaded and connected on the first question: a page whose visitor asks
12
+ // nothing never downloads the MCP client and its schema library. The module also renders on the
13
+ // server, where a relative address has no page to resolve against. A failed load is forgotten, so the
14
+ // next question loads the client again.
14
15
  function mcpTools(url) {
15
16
  let source = null;
16
17
  const connected = () => {
17
- source ??= new McpToolSource(url);
18
+ source ??= import('@tensorpass/aiconnect/mcp').then(({ McpToolSource }) => new McpToolSource(url), (error) => {
19
+ source = null;
20
+ throw error;
21
+ });
18
22
  return source;
19
23
  };
20
24
  return {
21
- listTools: () => connected().listTools(),
22
- callTool: (name, args) => connected().callTool(name, args),
25
+ listTools: async () => (await connected()).listTools(),
26
+ callTool: async (name, args) => (await connected()).callTool(name, args),
23
27
  };
24
28
  }
25
29
  // The states in which asking a question asks the wallet to connect.
@@ -1 +1 @@
1
- {"version":3,"file":"useModuleChat.js","sourceRoot":"","sources":["../../../src/modules/parts/useModuleChat.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,sGAAsG;AACtG,sGAAsG;AACtG,2GAA2G;AAC3G,sEAAsE;AAEtE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA6B,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAU/C,wFAAwF;AACxF,MAAM,QAAQ,GAAG,IAAI,GAAG,CAA4B,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC;AAEhF,gGAAgG;AAChG,2DAA2D;AAC3D,SAAS,QAAQ,CAAC,GAAW;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AE,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,CAAC;AACxE,CAAC","sourcesContent":["// The conversation a module runs: the chat on the visitor's AI with the site's tools, plus the\n// question a visitor asks before their AI is connected, with its attachments. That question opens the\n// connect window and stays on screen; it goes out on its own once the wallet connects the site or the\n// visitor signs in on the page, and stays with the connection step while a sign in waits. Signing out of a\n// web account starts the conversation again from the connection step.\n\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport { McpToolSource } from '@tensorpass/aiconnect';\nimport type { ConnectionState, GuardPolicy, ToolSource } from '@tensorpass/aiconnect';\nimport { useAiConnect } from '../../AiConnectProvider.tsx';\nimport { useAiChat } from '../../useAiChat.ts';\nimport type { AskedQuestion } from '../../useAiChat.ts';\n\nexport interface ModuleChatOptions {\n system: string;\n tools?: ToolSource;\n mcp?: string;\n guards?: GuardPolicy;\n}\n\n// The states in which the connection is still settling on its own, without the visitor.\nconst SETTLING = new Set<ConnectionState['status']>(['checking', 'connecting']);\n\n// The site's MCP tools, connected on the first question. The module also renders on the server,\n// where a relative address has no page to resolve against.\nfunction mcpTools(url: string): ToolSource {\n let source: McpToolSource | null = null;\n const connected = () => {\n source ??= new McpToolSource(url);\n return source;\n };\n return {\n listTools: () => connected().listTools(),\n callTool: (name, args) => connected().callTool(name, args),\n };\n}\n\n// The states in which asking a question asks the wallet to connect.\nconst READY_TO_CONNECT = new Set<ConnectionState['status']>(['disconnected', 'error']);\n\n// Keeps the question asked before the connection and sends it once the site is connected. Asking is\n// the visitor's one action: it asks the wallet to connect at once, or as soon as the first wallet\n// check ends, and without the wallet the module's own install step shows with the question kept.\nfunction usePendingQuestion(send: (question: AskedQuestion) => Promise<void>) {\n const { client } = useAiConnect();\n const pending = useRef<AskedQuestion | null>(null);\n const connectAfterCheck = useRef(false);\n const [pendingQuestion, setPendingQuestion] = useState<AskedQuestion | null>(null);\n\n const keep = useCallback((question: AskedQuestion | null) => {\n pending.current = question;\n setPendingQuestion(question);\n }, []);\n\n useEffect(() => client.subscribe((next) => {\n const question = pending.current;\n if (question === null || SETTLING.has(next.status)) {\n return;\n }\n const afterCheck = connectAfterCheck.current;\n connectAfterCheck.current = false;\n if (next.status === 'connected') {\n keep(null);\n void send(question);\n } else if (afterCheck && next.status === 'disconnected') {\n void client.connect();\n }\n }), [client, keep, send]);\n\n const ask = useCallback((question: string, attachments: AskedQuestion['attachments'] = []) => {\n const asked = { text: question.trim(), attachments };\n if (!asked.text && attachments.length === 0) {\n return;\n }\n const { status } = client.state;\n if (status === 'connected') {\n void send(asked);\n return;\n }\n keep(asked);\n if (READY_TO_CONNECT.has(status)) {\n void client.connect();\n } else if (status === 'checking') {\n connectAfterCheck.current = true;\n }\n }, [client, keep, send]);\n\n const forget = useCallback(() => keep(null), [keep]);\n return { pendingQuestion, ask, forget };\n}\n\n// Starts the conversation again when the web account that answered it signs out, by the visitor or because\n// its credential stopped opening the account: its answers and errors belong to that account.\nfunction useResetOnSignOut(reset: () => void) {\n const { client } = useAiConnect();\n const previous = useRef<ConnectionState>(client.state);\n useEffect(() => client.subscribe((next) => {\n const before = previous.current;\n previous.current = next;\n if (before.status === 'connected' && before.account && next.status !== 'connected') {\n reset();\n }\n }), [client, reset]);\n}\n\nexport function useModuleChat({ system, tools, mcp, guards }: ModuleChatOptions) {\n const toolSource = useMemo(() => tools ?? (mcp ? mcpTools(mcp) : undefined), [tools, mcp]);\n const chat = useAiChat({ system, tools: toolSource, guards });\n const { pendingQuestion, ask, forget } = usePendingQuestion(chat.send);\n // The turns the visitor stopped, by their place in the conversation.\n const [stopped, setStopped] = useState<number[]>([]);\n const { turns, stop: stopChat, reset: resetChat, busy, dropFailedTurn } = chat;\n\n const stop = useCallback(() => {\n if (busy) {\n setStopped((current) => [...current, turns.length - 1]);\n }\n stopChat();\n }, [busy, turns.length, stopChat]);\n\n const reset = useCallback(() => {\n resetChat();\n forget();\n setStopped([]);\n }, [resetChat, forget]);\n useResetOnSignOut(reset);\n\n // Asks the last question again in place of its failed turn.\n const retry = useCallback(() => {\n const last = turns.at(-1);\n if (last) {\n dropFailedTurn();\n ask(last.question, last.attachments);\n }\n }, [turns, ask, dropFailedTurn]);\n\n return { ...chat, ask, stop, reset, retry, pendingQuestion, stopped };\n}\n\nexport type ModuleChat = ReturnType<typeof useModuleChat>;\n"]}
1
+ {"version":3,"file":"useModuleChat.js","sourceRoot":"","sources":["../../../src/modules/parts/useModuleChat.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,sGAAsG;AACtG,sGAAsG;AACtG,2GAA2G;AAC3G,sEAAsE;AAEtE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE1E,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA6B,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAU/C,wFAAwF;AACxF,MAAM,QAAQ,GAAG,IAAI,GAAG,CAA4B,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC;AAEhF,8FAA8F;AAC9F,gGAAgG;AAChG,sGAAsG;AACtG,wCAAwC;AACxC,SAAS,QAAQ,CAAC,GAAW;IAC3B,IAAI,MAAM,GAA+B,IAAI,CAAC;IAC9C,MAAM,SAAS,GAAG,GAAG,EAAE;QACrB,MAAM,KAAK,MAAM,CAAC,2BAA2B,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE;YAC3G,MAAM,GAAG,IAAI,CAAC;YACd,MAAM,KAAK,CAAC;QACd,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IACF,OAAO;QACL,SAAS,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,SAAS,EAAE,CAAC,CAAC,SAAS,EAAE;QACtD,QAAQ,EAAE,KAAK,EAAE,IAA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;IAE/E,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,EAAE;QAC5B,IAAI,IAAI,EAAE,CAAC;YACT,UAAU,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;QAC1D,CAAC;QACD,QAAQ,EAAE,CAAC;IACb,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;IAEnC,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;QAC7B,SAAS,EAAE,CAAC;QACZ,MAAM,EAAE,CAAC;QACT,UAAU,CAAC,EAAE,CAAC,CAAC;IACjB,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;IACxB,iBAAiB,CAAC,KAAK,CAAC,CAAC;IAEzB,4DAA4D;IAC5D,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;QAC7B,MAAM,IAAI,GAAG,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1B,IAAI,IAAI,EAAE,CAAC;YACT,cAAc,EAAE,CAAC;YACjB,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QACvC,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC,CAAC;IAEjC,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,CAAC;AACxE,CAAC","sourcesContent":["// The conversation a module runs: the chat on the visitor's AI with the site's tools, plus the\n// question a visitor asks before their AI is connected, with its attachments. That question opens the\n// connect window and stays on screen; it goes out on its own once the wallet connects the site or the\n// visitor signs in on the page, and stays with the connection step while a sign in waits. Signing out of a\n// web account starts the conversation again from the connection step.\n\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport type { ConnectionState, GuardPolicy, ToolSource } from '@tensorpass/aiconnect';\nimport { useAiConnect } from '../../AiConnectProvider.tsx';\nimport { useAiChat } from '../../useAiChat.ts';\nimport type { AskedQuestion } from '../../useAiChat.ts';\n\nexport interface ModuleChatOptions {\n system: string;\n tools?: ToolSource;\n mcp?: string;\n guards?: GuardPolicy;\n}\n\n// The states in which the connection is still settling on its own, without the visitor.\nconst SETTLING = new Set<ConnectionState['status']>(['checking', 'connecting']);\n\n// The site's MCP tools, loaded and connected on the first question: a page whose visitor asks\n// nothing never downloads the MCP client and its schema library. The module also renders on the\n// server, where a relative address has no page to resolve against. A failed load is forgotten, so the\n// next question loads the client again.\nfunction mcpTools(url: string): ToolSource {\n let source: Promise<ToolSource> | null = null;\n const connected = () => {\n source ??= import('@tensorpass/aiconnect/mcp').then(({ McpToolSource }) => new McpToolSource(url), (error) => {\n source = null;\n throw error;\n });\n return source;\n };\n return {\n listTools: async () => (await connected()).listTools(),\n callTool: async (name, args) => (await connected()).callTool(name, args),\n };\n}\n\n// The states in which asking a question asks the wallet to connect.\nconst READY_TO_CONNECT = new Set<ConnectionState['status']>(['disconnected', 'error']);\n\n// Keeps the question asked before the connection and sends it once the site is connected. Asking is\n// the visitor's one action: it asks the wallet to connect at once, or as soon as the first wallet\n// check ends, and without the wallet the module's own install step shows with the question kept.\nfunction usePendingQuestion(send: (question: AskedQuestion) => Promise<void>) {\n const { client } = useAiConnect();\n const pending = useRef<AskedQuestion | null>(null);\n const connectAfterCheck = useRef(false);\n const [pendingQuestion, setPendingQuestion] = useState<AskedQuestion | null>(null);\n\n const keep = useCallback((question: AskedQuestion | null) => {\n pending.current = question;\n setPendingQuestion(question);\n }, []);\n\n useEffect(() => client.subscribe((next) => {\n const question = pending.current;\n if (question === null || SETTLING.has(next.status)) {\n return;\n }\n const afterCheck = connectAfterCheck.current;\n connectAfterCheck.current = false;\n if (next.status === 'connected') {\n keep(null);\n void send(question);\n } else if (afterCheck && next.status === 'disconnected') {\n void client.connect();\n }\n }), [client, keep, send]);\n\n const ask = useCallback((question: string, attachments: AskedQuestion['attachments'] = []) => {\n const asked = { text: question.trim(), attachments };\n if (!asked.text && attachments.length === 0) {\n return;\n }\n const { status } = client.state;\n if (status === 'connected') {\n void send(asked);\n return;\n }\n keep(asked);\n if (READY_TO_CONNECT.has(status)) {\n void client.connect();\n } else if (status === 'checking') {\n connectAfterCheck.current = true;\n }\n }, [client, keep, send]);\n\n const forget = useCallback(() => keep(null), [keep]);\n return { pendingQuestion, ask, forget };\n}\n\n// Starts the conversation again when the web account that answered it signs out, by the visitor or because\n// its credential stopped opening the account: its answers and errors belong to that account.\nfunction useResetOnSignOut(reset: () => void) {\n const { client } = useAiConnect();\n const previous = useRef<ConnectionState>(client.state);\n useEffect(() => client.subscribe((next) => {\n const before = previous.current;\n previous.current = next;\n if (before.status === 'connected' && before.account && next.status !== 'connected') {\n reset();\n }\n }), [client, reset]);\n}\n\nexport function useModuleChat({ system, tools, mcp, guards }: ModuleChatOptions) {\n const toolSource = useMemo(() => tools ?? (mcp ? mcpTools(mcp) : undefined), [tools, mcp]);\n const chat = useAiChat({ system, tools: toolSource, guards });\n const { pendingQuestion, ask, forget } = usePendingQuestion(chat.send);\n // The turns the visitor stopped, by their place in the conversation.\n const [stopped, setStopped] = useState<number[]>([]);\n const { turns, stop: stopChat, reset: resetChat, busy, dropFailedTurn } = chat;\n\n const stop = useCallback(() => {\n if (busy) {\n setStopped((current) => [...current, turns.length - 1]);\n }\n stopChat();\n }, [busy, turns.length, stopChat]);\n\n const reset = useCallback(() => {\n resetChat();\n forget();\n setStopped([]);\n }, [resetChat, forget]);\n useResetOnSignOut(reset);\n\n // Asks the last question again in place of its failed turn.\n const retry = useCallback(() => {\n const last = turns.at(-1);\n if (last) {\n dropFailedTurn();\n ask(last.question, last.attachments);\n }\n }, [turns, ask, dropFailedTurn]);\n\n return { ...chat, ask, stop, reset, retry, pendingQuestion, stopped };\n}\n\nexport type ModuleChat = ReturnType<typeof useModuleChat>;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tensorpass/aiconnect-react",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "React provider picker and chat hook for AI Connect.",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -34,8 +34,8 @@
34
34
  "build": "rm -rf dist tsconfig.tsbuildinfo && tsc -b && esbuild src/styles.css --bundle --outfile=dist/styles.css --log-level=warning && cp src/modules/styles/launchers.css dist/launchers.css"
35
35
  },
36
36
  "dependencies": {
37
- "@tensorpass/aiconnect": "0.1.7",
38
- "@tensorpass/aiconnect-design": "0.1.7"
37
+ "@tensorpass/aiconnect": "0.1.8",
38
+ "@tensorpass/aiconnect-design": "0.1.8"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": ">=19"