@terreno/ui 57.9.0 → 57.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/Button.d.ts.map +1 -1
- package/dist/Button.js +19 -4
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +68 -1
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/DateTimeField.d.ts.map +1 -1
- package/dist/DateTimeField.js +90 -17
- package/dist/DateTimeField.js.map +1 -1
- package/dist/FilePickerButton.d.ts +15 -1
- package/dist/FilePickerButton.d.ts.map +1 -1
- package/dist/FilePickerButton.js +26 -9
- package/dist/FilePickerButton.js.map +1 -1
- package/dist/GPTChat.d.ts +57 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +199 -7
- package/dist/GPTChat.js.map +1 -1
- package/dist/HtmlFrame.d.ts +11 -0
- package/dist/HtmlFrame.d.ts.map +1 -0
- package/dist/HtmlFrame.js +29 -0
- package/dist/HtmlFrame.js.map +1 -0
- package/dist/Text.d.ts +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +2 -2
- package/dist/Text.js.map +1 -1
- package/dist/asks/AskCard.d.ts +34 -0
- package/dist/asks/AskCard.d.ts.map +1 -0
- package/dist/asks/AskCard.js +91 -0
- package/dist/asks/AskCard.js.map +1 -0
- package/dist/asks/AskChoice.d.ts +11 -0
- package/dist/asks/AskChoice.d.ts.map +1 -0
- package/dist/asks/AskChoice.js +146 -0
- package/dist/asks/AskChoice.js.map +1 -0
- package/dist/asks/AskConfirm.d.ts +15 -0
- package/dist/asks/AskConfirm.d.ts.map +1 -0
- package/dist/asks/AskConfirm.js +17 -0
- package/dist/asks/AskConfirm.js.map +1 -0
- package/dist/asks/AskFiles.d.ts +18 -0
- package/dist/asks/AskFiles.d.ts.map +1 -0
- package/dist/asks/AskFiles.js +149 -0
- package/dist/asks/AskFiles.js.map +1 -0
- package/dist/asks/AskForm.d.ts +27 -0
- package/dist/asks/AskForm.d.ts.map +1 -0
- package/dist/asks/AskForm.js +146 -0
- package/dist/asks/AskForm.js.map +1 -0
- package/dist/asks/AskMarkdown.d.ts +25 -0
- package/dist/asks/AskMarkdown.d.ts.map +1 -0
- package/dist/asks/AskMarkdown.js +73 -0
- package/dist/asks/AskMarkdown.js.map +1 -0
- package/dist/asks/SimpleAskCard.d.ts +23 -0
- package/dist/asks/SimpleAskCard.d.ts.map +1 -0
- package/dist/asks/SimpleAskCard.js +32 -0
- package/dist/asks/SimpleAskCard.js.map +1 -0
- package/dist/asks/askControls.d.ts +33 -0
- package/dist/asks/askControls.d.ts.map +1 -0
- package/dist/asks/askControls.js +30 -0
- package/dist/asks/askControls.js.map +1 -0
- package/dist/asks/askFileRefs.d.ts +26 -0
- package/dist/asks/askFileRefs.d.ts.map +1 -0
- package/dist/asks/askFileRefs.js +75 -0
- package/dist/asks/askFileRefs.js.map +1 -0
- package/dist/asks/askFormDraft.d.ts +60 -0
- package/dist/asks/askFormDraft.d.ts.map +1 -0
- package/dist/asks/askFormDraft.js +224 -0
- package/dist/asks/askFormDraft.js.map +1 -0
- package/dist/asks/askSummary.d.ts +10 -0
- package/dist/asks/askSummary.d.ts.map +1 -0
- package/dist/asks/askSummary.js +138 -0
- package/dist/asks/askSummary.js.map +1 -0
- package/dist/asks/askTypes.d.ts +21 -0
- package/dist/asks/askTypes.d.ts.map +1 -0
- package/dist/asks/askTypes.js +2 -0
- package/dist/asks/askTypes.js.map +1 -0
- package/dist/asks/simpleCardButtonVariants.d.ts +5 -0
- package/dist/asks/simpleCardButtonVariants.d.ts.map +1 -0
- package/dist/asks/simpleCardButtonVariants.js +8 -0
- package/dist/asks/simpleCardButtonVariants.js.map +1 -0
- package/dist/blocks/BlocksError.d.ts +8 -0
- package/dist/blocks/BlocksError.d.ts.map +1 -0
- package/dist/blocks/BlocksError.js +14 -0
- package/dist/blocks/BlocksError.js.map +1 -0
- package/dist/blocks/BlocksView.d.ts +10 -0
- package/dist/blocks/BlocksView.d.ts.map +1 -0
- package/dist/blocks/BlocksView.js +90 -0
- package/dist/blocks/BlocksView.js.map +1 -0
- package/dist/blocks/blockRenderers.d.ts +25 -0
- package/dist/blocks/blockRenderers.d.ts.map +1 -0
- package/dist/blocks/blockRenderers.js +136 -0
- package/dist/blocks/blockRenderers.js.map +1 -0
- package/dist/blocks/datasetToPoints.d.ts +22 -0
- package/dist/blocks/datasetToPoints.d.ts.map +1 -0
- package/dist/blocks/datasetToPoints.js +28 -0
- package/dist/blocks/datasetToPoints.js.map +1 -0
- package/dist/blocks/useBlockSelections.d.ts +6 -0
- package/dist/blocks/useBlockSelections.d.ts.map +1 -0
- package/dist/blocks/useBlockSelections.js +10 -0
- package/dist/blocks/useBlockSelections.js.map +1 -0
- package/dist/blocks/useResolvedDatasets.d.ts +18 -0
- package/dist/blocks/useResolvedDatasets.d.ts.map +1 -0
- package/dist/blocks/useResolvedDatasets.js +59 -0
- package/dist/blocks/useResolvedDatasets.js.map +1 -0
- package/dist/blocks/useResolvedImages.d.ts +15 -0
- package/dist/blocks/useResolvedImages.d.ts.map +1 -0
- package/dist/blocks/useResolvedImages.js +66 -0
- package/dist/blocks/useResolvedImages.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +6 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +4 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/package.json +2 -1
- package/src/Button.test.tsx +72 -0
- package/src/Button.tsx +34 -4
- package/src/Common.ts +67 -0
- package/src/DateTimeField.test.tsx +185 -2
- package/src/DateTimeField.tsx +106 -16
- package/src/DonutChart.test.tsx +5 -10
- package/src/FilePickerButton.test.tsx +56 -3
- package/src/FilePickerButton.tsx +83 -17
- package/src/GPTChat.test.tsx +758 -2
- package/src/GPTChat.tsx +517 -7
- package/src/HtmlFrame.tsx +60 -0
- package/src/LineChart.test.tsx +5 -10
- package/src/Text.test.tsx +22 -0
- package/src/Text.tsx +10 -1
- package/src/asks/AskCard.test.tsx +1428 -0
- package/src/asks/AskCard.tsx +240 -0
- package/src/asks/AskChoice.tsx +368 -0
- package/src/asks/AskConfirm.tsx +42 -0
- package/src/asks/AskFiles.test.tsx +347 -0
- package/src/asks/AskFiles.tsx +246 -0
- package/src/asks/AskForm.tsx +334 -0
- package/src/asks/AskMarkdown.tsx +176 -0
- package/src/asks/SimpleAskCard.test.tsx +253 -0
- package/src/asks/SimpleAskCard.tsx +82 -0
- package/src/asks/askControls.tsx +98 -0
- package/src/asks/askFileRefs.test.ts +155 -0
- package/src/asks/askFileRefs.ts +98 -0
- package/src/asks/askFormDraft.test.ts +347 -0
- package/src/asks/askFormDraft.ts +309 -0
- package/src/asks/askSummary.test.ts +255 -0
- package/src/asks/askSummary.ts +160 -0
- package/src/asks/askTypes.ts +25 -0
- package/src/asks/asksUseTerrenoComponents.test.ts +55 -0
- package/src/asks/simpleCardButtonVariants.ts +14 -0
- package/src/blocks/BlocksError.tsx +28 -0
- package/src/blocks/BlocksView.test.tsx +607 -0
- package/src/blocks/BlocksView.tsx +125 -0
- package/src/blocks/blockRenderers.tsx +288 -0
- package/src/blocks/datasetToPoints.test.ts +31 -0
- package/src/blocks/datasetToPoints.ts +48 -0
- package/src/blocks/useBlockSelections.ts +13 -0
- package/src/blocks/useResolvedDatasets.ts +78 -0
- package/src/blocks/useResolvedImages.ts +81 -0
- package/src/index.tsx +7 -0
- package/src/isolated/HtmlFrame.isolated.tsx +54 -0
- package/src/lazyBoundaries/heavyOptionalExports.test.tsx +2 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +14 -0
package/src/GPTChat.tsx
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AskValidationError,
|
|
3
|
+
askResponseSchema,
|
|
4
|
+
type Block,
|
|
5
|
+
type BlockAction,
|
|
6
|
+
type BlocksDocument,
|
|
7
|
+
parseBlocksPartial,
|
|
8
|
+
} from "@terreno/blocks";
|
|
1
9
|
import {DateTime} from "luxon";
|
|
2
10
|
import React, {useCallback, useEffect, useRef, useState} from "react";
|
|
3
11
|
import {
|
|
12
|
+
AccessibilityInfo,
|
|
13
|
+
findNodeHandle,
|
|
14
|
+
type Text as NativeText,
|
|
4
15
|
Platform,
|
|
5
16
|
Image as RNImage,
|
|
6
17
|
type ScrollView as RNScrollView,
|
|
7
18
|
type TextInput as RNTextInput,
|
|
19
|
+
View,
|
|
8
20
|
} from "react-native";
|
|
9
21
|
|
|
10
22
|
import {AttachmentPreview} from "./AttachmentPreview";
|
|
23
|
+
import {AskCard} from "./asks/AskCard";
|
|
24
|
+
import type {AskFilesResolver} from "./asks/askFileRefs";
|
|
25
|
+
import type {AskSubmitHandler, ChatAsk} from "./asks/askTypes";
|
|
11
26
|
import {Box} from "./Box";
|
|
12
27
|
import {Button} from "./Button";
|
|
28
|
+
import {BlocksView} from "./blocks/BlocksView";
|
|
29
|
+
import type {BlocksViewProps} from "./Common";
|
|
13
30
|
import {DropdownMenuItem} from "./DropdownMenuItem";
|
|
14
31
|
import {DropdownPanel} from "./DropdownPanel";
|
|
15
32
|
import type {SelectedFile} from "./FilePickerButton";
|
|
@@ -69,8 +86,31 @@ export interface ToolResultInfo {
|
|
|
69
86
|
// Message Types
|
|
70
87
|
// ============================================================
|
|
71
88
|
|
|
89
|
+
export interface BlockChatEvent {
|
|
90
|
+
action: BlockAction;
|
|
91
|
+
blockId: string;
|
|
92
|
+
elementId: string;
|
|
93
|
+
messageId: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** What `onBlockCallback` returns. `replace: "block"` swaps that block. `text` appends a message. */
|
|
97
|
+
export interface BlockCallbackResult {
|
|
98
|
+
blocks?: Block | BlocksDocument;
|
|
99
|
+
replace?: "block";
|
|
100
|
+
text?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
72
103
|
export interface GPTChatMessage {
|
|
104
|
+
/**
|
|
105
|
+
* Set on a `tool-call` message when the tool call is an agent ask. The chat renders an `AskCard`
|
|
106
|
+
* instead of the tool call, and hides the ask's `tool-result` message.
|
|
107
|
+
*/
|
|
108
|
+
ask?: ChatAsk;
|
|
109
|
+
/** Subtle caption under a block reply, such as "3 components". */
|
|
110
|
+
blockNote?: string;
|
|
73
111
|
content: string;
|
|
112
|
+
/** Stable id for block actions. Falls back to the message index. */
|
|
113
|
+
id?: string;
|
|
74
114
|
contentParts?: MessageContentPart[];
|
|
75
115
|
rating?: "up" | "down";
|
|
76
116
|
role: "user" | "assistant" | "system" | "tool-call" | "tool-result";
|
|
@@ -96,16 +136,31 @@ export interface MCPToolDetail {
|
|
|
96
136
|
}
|
|
97
137
|
|
|
98
138
|
export interface GPTChatProps {
|
|
139
|
+
/** Errors for the last answer to each ask, keyed by tool call id, such as a 400's `fields`. */
|
|
140
|
+
askErrors?: Record<string, AskValidationError[]>;
|
|
99
141
|
attachments?: SelectedFile[];
|
|
100
142
|
availableModels?: Array<{label: string; value: string}>;
|
|
101
143
|
currentHistoryId?: string;
|
|
102
144
|
currentMessages: GPTChatMessage[];
|
|
103
145
|
geminiApiKey?: string;
|
|
104
146
|
histories: GPTChatHistory[];
|
|
147
|
+
/** Callback names the host will run. A callback outside this list is disabled. */
|
|
148
|
+
hostActions?: readonly string[];
|
|
105
149
|
isStreaming?: boolean;
|
|
106
150
|
/** Available MCP tools to display in the tools panel. */
|
|
107
151
|
mcpTools?: MCPToolDetail[];
|
|
108
152
|
mcpServers?: MCPServerStatus[];
|
|
153
|
+
/**
|
|
154
|
+
* Called when the user answers a pending ask. The pressed control shows a loading state until
|
|
155
|
+
* the returned promise settles. Without it, asks are shown but cannot be answered.
|
|
156
|
+
*/
|
|
157
|
+
onAskSubmit?: AskSubmitHandler;
|
|
158
|
+
/** `open` and `select`. `reply` calls `onSubmit`. `callback` calls `onBlockCallback`. */
|
|
159
|
+
onBlockAction?: (event: BlockChatEvent) => void;
|
|
160
|
+
/** Runs a callback button. The button stays loading until the promise settles. */
|
|
161
|
+
onBlockCallback?: (
|
|
162
|
+
event: BlockChatEvent
|
|
163
|
+
) => BlockCallbackResult | Promise<BlockCallbackResult | undefined> | undefined;
|
|
109
164
|
onAttachFiles?: (files: SelectedFile[]) => void;
|
|
110
165
|
onCreateHistory: () => void;
|
|
111
166
|
onDeleteHistory: (id: string) => void;
|
|
@@ -117,6 +172,15 @@ export interface GPTChatProps {
|
|
|
117
172
|
onSelectHistory: (id: string) => void;
|
|
118
173
|
onSubmit: (prompt: string) => void;
|
|
119
174
|
onUpdateTitle?: (id: string, title: string) => void;
|
|
175
|
+
/**
|
|
176
|
+
* Turns the files picked for a `files` ask into the answer's refs: uploads (`{fileId}`) or data
|
|
177
|
+
* URLs (`{url}`). Defaults to data URLs. Throw to keep the ask open.
|
|
178
|
+
*/
|
|
179
|
+
resolveAskFiles?: AskFilesResolver;
|
|
180
|
+
/** Loads a `ref` dataset while `uiBlocks` is on. */
|
|
181
|
+
resolveDataset?: BlocksViewProps["resolveDataset"];
|
|
182
|
+
/** Turns a `file:` image id into a URL while `uiBlocks` is on. */
|
|
183
|
+
resolveImage?: BlocksViewProps["resolveImage"];
|
|
120
184
|
selectedModel?: string;
|
|
121
185
|
/**
|
|
122
186
|
* Optional consumer-owned character for an empty chat. Terreno does not ship a
|
|
@@ -127,6 +191,15 @@ export interface GPTChatProps {
|
|
|
127
191
|
suggestedPrompts?: string[];
|
|
128
192
|
systemMemory?: string;
|
|
129
193
|
testID?: string;
|
|
194
|
+
/**
|
|
195
|
+
* Assistant messages are whole-reply documents. A streaming message renders each finished
|
|
196
|
+
* top-level block and a spinner for the block still arriving.
|
|
197
|
+
*/
|
|
198
|
+
uiBlocks?: boolean;
|
|
199
|
+
/** Renders `html` blocks in a sandboxed frame. Off until the host turns it on. */
|
|
200
|
+
allowHtml?: boolean;
|
|
201
|
+
/** Hostnames allowed on https image sources. Empty rejects every https image. */
|
|
202
|
+
imageHosts?: readonly string[];
|
|
130
203
|
}
|
|
131
204
|
|
|
132
205
|
// ============================================================
|
|
@@ -660,7 +733,149 @@ const ContentPartsPreview = ({
|
|
|
660
733
|
);
|
|
661
734
|
};
|
|
662
735
|
|
|
663
|
-
const
|
|
736
|
+
const replacementBlock = (blocks: Block | BlocksDocument): Block | undefined => {
|
|
737
|
+
if ("type" in blocks) {
|
|
738
|
+
return blocks;
|
|
739
|
+
}
|
|
740
|
+
return blocks.blocks[0];
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
const documentFromPartial = (partial: ReturnType<typeof parseBlocksPartial>): BlocksDocument => {
|
|
744
|
+
const doc = {v: 1} as BlocksDocument;
|
|
745
|
+
if (partial.datasets !== undefined) {
|
|
746
|
+
doc.datasets = partial.datasets as BlocksDocument["datasets"];
|
|
747
|
+
}
|
|
748
|
+
doc.blocks = partial.blocks as Block[];
|
|
749
|
+
return doc;
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
const AssistantBlocks = ({
|
|
753
|
+
allowHtml,
|
|
754
|
+
content,
|
|
755
|
+
hostActions,
|
|
756
|
+
imageHosts,
|
|
757
|
+
isPartial,
|
|
758
|
+
messageId,
|
|
759
|
+
onBlockEvent,
|
|
760
|
+
overrides,
|
|
761
|
+
pendingElementIds,
|
|
762
|
+
resolveDataset,
|
|
763
|
+
resolveImage,
|
|
764
|
+
}: {
|
|
765
|
+
allowHtml?: boolean;
|
|
766
|
+
content: string;
|
|
767
|
+
hostActions?: readonly string[];
|
|
768
|
+
imageHosts?: readonly string[];
|
|
769
|
+
isPartial: boolean;
|
|
770
|
+
messageId: string;
|
|
771
|
+
onBlockEvent: (event: BlockChatEvent) => void;
|
|
772
|
+
overrides?: Record<string, Block>;
|
|
773
|
+
pendingElementIds?: readonly string[];
|
|
774
|
+
resolveDataset?: BlocksViewProps["resolveDataset"];
|
|
775
|
+
resolveImage?: BlocksViewProps["resolveImage"];
|
|
776
|
+
}): React.ReactElement => {
|
|
777
|
+
if (isPartial) {
|
|
778
|
+
const partial = parseBlocksPartial(content);
|
|
779
|
+
return (
|
|
780
|
+
<Box gap={2} testID={`gpt-blocks-${messageId}`}>
|
|
781
|
+
{partial.blocks.length > 0 ? (
|
|
782
|
+
<BlocksView
|
|
783
|
+
allowHtml={allowHtml}
|
|
784
|
+
document={documentFromPartial(partial)}
|
|
785
|
+
hostActions={hostActions}
|
|
786
|
+
imageHosts={imageHosts}
|
|
787
|
+
onAction={(event) =>
|
|
788
|
+
onBlockEvent({
|
|
789
|
+
action: event.action,
|
|
790
|
+
blockId: event.blockId,
|
|
791
|
+
elementId: event.elementId,
|
|
792
|
+
messageId,
|
|
793
|
+
})
|
|
794
|
+
}
|
|
795
|
+
overrides={overrides}
|
|
796
|
+
pendingElementIds={pendingElementIds}
|
|
797
|
+
resolveDataset={resolveDataset}
|
|
798
|
+
resolveImage={resolveImage}
|
|
799
|
+
streaming
|
|
800
|
+
/>
|
|
801
|
+
) : null}
|
|
802
|
+
{partial.pending ? (
|
|
803
|
+
<Box testID="gpt-blocks-pending">
|
|
804
|
+
<Spinner size="sm" />
|
|
805
|
+
</Box>
|
|
806
|
+
) : null}
|
|
807
|
+
</Box>
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
return (
|
|
811
|
+
<BlocksView
|
|
812
|
+
allowHtml={allowHtml}
|
|
813
|
+
document={content}
|
|
814
|
+
hostActions={hostActions}
|
|
815
|
+
imageHosts={imageHosts}
|
|
816
|
+
onAction={(event) =>
|
|
817
|
+
onBlockEvent({
|
|
818
|
+
action: event.action,
|
|
819
|
+
blockId: event.blockId,
|
|
820
|
+
elementId: event.elementId,
|
|
821
|
+
messageId,
|
|
822
|
+
})
|
|
823
|
+
}
|
|
824
|
+
overrides={overrides}
|
|
825
|
+
pendingElementIds={pendingElementIds}
|
|
826
|
+
resolveDataset={resolveDataset}
|
|
827
|
+
resolveImage={resolveImage}
|
|
828
|
+
testID={`gpt-blocks-${messageId}`}
|
|
829
|
+
/>
|
|
830
|
+
);
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
const MessageText = ({
|
|
834
|
+
allowHtml,
|
|
835
|
+
content,
|
|
836
|
+
hostActions,
|
|
837
|
+
imageHosts,
|
|
838
|
+
isPartial,
|
|
839
|
+
messageId,
|
|
840
|
+
onBlockEvent,
|
|
841
|
+
overrides,
|
|
842
|
+
pendingElementIds,
|
|
843
|
+
resolveDataset,
|
|
844
|
+
resolveImage,
|
|
845
|
+
role,
|
|
846
|
+
uiBlocks,
|
|
847
|
+
}: {
|
|
848
|
+
allowHtml?: boolean;
|
|
849
|
+
content: string;
|
|
850
|
+
hostActions?: readonly string[];
|
|
851
|
+
imageHosts?: readonly string[];
|
|
852
|
+
isPartial: boolean;
|
|
853
|
+
messageId: string;
|
|
854
|
+
onBlockEvent: (event: BlockChatEvent) => void;
|
|
855
|
+
overrides?: Record<string, Block>;
|
|
856
|
+
pendingElementIds?: readonly string[];
|
|
857
|
+
resolveDataset?: BlocksViewProps["resolveDataset"];
|
|
858
|
+
resolveImage?: BlocksViewProps["resolveImage"];
|
|
859
|
+
role: string;
|
|
860
|
+
uiBlocks: boolean;
|
|
861
|
+
}): React.ReactElement => {
|
|
862
|
+
if (role === "assistant" && uiBlocks) {
|
|
863
|
+
return (
|
|
864
|
+
<AssistantBlocks
|
|
865
|
+
allowHtml={allowHtml}
|
|
866
|
+
content={content}
|
|
867
|
+
hostActions={hostActions}
|
|
868
|
+
imageHosts={imageHosts}
|
|
869
|
+
isPartial={isPartial}
|
|
870
|
+
messageId={messageId}
|
|
871
|
+
onBlockEvent={onBlockEvent}
|
|
872
|
+
overrides={overrides}
|
|
873
|
+
pendingElementIds={pendingElementIds}
|
|
874
|
+
resolveDataset={resolveDataset}
|
|
875
|
+
resolveImage={resolveImage}
|
|
876
|
+
/>
|
|
877
|
+
);
|
|
878
|
+
}
|
|
664
879
|
if (role === "assistant") {
|
|
665
880
|
return <MarkdownView>{content}</MarkdownView>;
|
|
666
881
|
}
|
|
@@ -787,18 +1002,183 @@ const EmptyChatHero = ({
|
|
|
787
1002
|
);
|
|
788
1003
|
};
|
|
789
1004
|
|
|
1005
|
+
/**
|
|
1006
|
+
* Moves focus to a pending ask when it appears, so keyboard and screen reader users land on it.
|
|
1007
|
+
* On web the ask's group takes keyboard focus. On native, screen reader focus goes to the ask's
|
|
1008
|
+
* question, because it only lands on an accessible element and the group is not one. A raw
|
|
1009
|
+
* `View` because `Box` does not expose its native view to a ref.
|
|
1010
|
+
*/
|
|
1011
|
+
const AskFocusTarget = ({
|
|
1012
|
+
label,
|
|
1013
|
+
renderCard,
|
|
1014
|
+
}: {
|
|
1015
|
+
label: string;
|
|
1016
|
+
renderCard: (promptRef: React.RefObject<NativeText | null>) => React.ReactElement;
|
|
1017
|
+
}): React.ReactElement => {
|
|
1018
|
+
const viewRef = useRef<View>(null);
|
|
1019
|
+
const promptRef = useRef<NativeText>(null);
|
|
1020
|
+
|
|
1021
|
+
// Focus the ask once, when it mounts; later renders of the same ask leave focus alone.
|
|
1022
|
+
useEffect(() => {
|
|
1023
|
+
if (Platform.OS === "web") {
|
|
1024
|
+
(viewRef.current as unknown as HTMLElement | null)?.focus?.({preventScroll: true});
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
const node = promptRef.current ? findNodeHandle(promptRef.current) : null;
|
|
1028
|
+
if (node) {
|
|
1029
|
+
AccessibilityInfo.setAccessibilityFocus(node);
|
|
1030
|
+
}
|
|
1031
|
+
}, []);
|
|
1032
|
+
|
|
1033
|
+
return (
|
|
1034
|
+
<View aria-label={label} ref={viewRef} role="group" tabIndex={-1}>
|
|
1035
|
+
{renderCard(promptRef)}
|
|
1036
|
+
</View>
|
|
1037
|
+
);
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
/** The ask's answer, taken from its `tool-result` message when the host did not set `response`. */
|
|
1041
|
+
const withStoredResponse = (ask: ChatAsk, results: Map<string, unknown>): ChatAsk => {
|
|
1042
|
+
if (ask.response || !results.has(ask.toolCallId)) {
|
|
1043
|
+
return ask;
|
|
1044
|
+
}
|
|
1045
|
+
const stored = askResponseSchema.safeParse(results.get(ask.toolCallId));
|
|
1046
|
+
return stored.success ? {...ask, response: stored.data} : ask;
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
const AskTranscriptItem = ({
|
|
1050
|
+
ask,
|
|
1051
|
+
errors,
|
|
1052
|
+
onAskSubmit,
|
|
1053
|
+
resolveAskFiles,
|
|
1054
|
+
}: {
|
|
1055
|
+
ask: ChatAsk;
|
|
1056
|
+
errors?: AskValidationError[];
|
|
1057
|
+
onAskSubmit?: AskSubmitHandler;
|
|
1058
|
+
resolveAskFiles?: AskFilesResolver;
|
|
1059
|
+
}): React.ReactElement => {
|
|
1060
|
+
const renderCard = (promptRef?: React.Ref<NativeText>): React.ReactElement => (
|
|
1061
|
+
<AskCard
|
|
1062
|
+
ask={ask}
|
|
1063
|
+
errors={errors}
|
|
1064
|
+
onSubmit={onAskSubmit}
|
|
1065
|
+
promptRef={promptRef}
|
|
1066
|
+
resolveAskFiles={resolveAskFiles}
|
|
1067
|
+
testID={`gpt-ask-${ask.toolCallId}`}
|
|
1068
|
+
/>
|
|
1069
|
+
);
|
|
1070
|
+
if (ask.status !== "pending") {
|
|
1071
|
+
return <Box alignItems="start">{renderCard()}</Box>;
|
|
1072
|
+
}
|
|
1073
|
+
return (
|
|
1074
|
+
<Box maxWidth="80%" width="100%">
|
|
1075
|
+
<AskFocusTarget
|
|
1076
|
+
label={ask.input?.title ?? "Question from the assistant"}
|
|
1077
|
+
renderCard={renderCard}
|
|
1078
|
+
/>
|
|
1079
|
+
</Box>
|
|
1080
|
+
);
|
|
1081
|
+
};
|
|
1082
|
+
|
|
790
1083
|
const MessageList = ({
|
|
1084
|
+
allowHtml,
|
|
1085
|
+
appendedByMessage,
|
|
1086
|
+
askErrors,
|
|
1087
|
+
blockOverrides,
|
|
791
1088
|
currentMessages,
|
|
792
1089
|
handleCopyMessage,
|
|
1090
|
+
hostActions,
|
|
1091
|
+
imageHosts,
|
|
1092
|
+
isStreaming,
|
|
1093
|
+
onAskSubmit,
|
|
1094
|
+
onBlockEvent,
|
|
793
1095
|
onRateFeedback,
|
|
1096
|
+
pendingElements,
|
|
1097
|
+
resolveAskFiles,
|
|
1098
|
+
resolveDataset,
|
|
1099
|
+
resolveImage,
|
|
1100
|
+
uiBlocks,
|
|
794
1101
|
}: {
|
|
1102
|
+
allowHtml?: boolean;
|
|
1103
|
+
appendedByMessage: Record<string, GPTChatMessage[]>;
|
|
1104
|
+
askErrors?: Record<string, AskValidationError[]>;
|
|
1105
|
+
blockOverrides: Record<string, Record<string, Block>>;
|
|
795
1106
|
currentMessages: GPTChatMessage[];
|
|
796
1107
|
handleCopyMessage: (message: GPTChatMessage) => void;
|
|
1108
|
+
hostActions?: readonly string[];
|
|
1109
|
+
imageHosts?: readonly string[];
|
|
1110
|
+
isStreaming: boolean;
|
|
1111
|
+
onAskSubmit?: AskSubmitHandler;
|
|
1112
|
+
onBlockEvent: (event: BlockChatEvent) => void;
|
|
797
1113
|
onRateFeedback?: (promptIndex: number, rating: "up" | "down" | null) => void;
|
|
1114
|
+
pendingElements: Record<string, readonly string[]>;
|
|
1115
|
+
resolveAskFiles?: AskFilesResolver;
|
|
1116
|
+
resolveDataset?: BlocksViewProps["resolveDataset"];
|
|
1117
|
+
resolveImage?: BlocksViewProps["resolveImage"];
|
|
1118
|
+
uiBlocks: boolean;
|
|
798
1119
|
}): React.ReactElement => {
|
|
1120
|
+
const askToolCallIds = new Set<string>();
|
|
1121
|
+
const toolResults = new Map<string, unknown>();
|
|
1122
|
+
for (const message of currentMessages) {
|
|
1123
|
+
if (message.role === "tool-call" && message.ask) {
|
|
1124
|
+
askToolCallIds.add(message.ask.toolCallId);
|
|
1125
|
+
}
|
|
1126
|
+
if (message.role === "tool-result" && message.toolResult) {
|
|
1127
|
+
toolResults.set(message.toolResult.toolCallId, message.toolResult.result);
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
// The live reply stays partial after an ask is appended, so a draft document does not
|
|
1132
|
+
// validate as a finished message while the turn is still streaming.
|
|
1133
|
+
const liveAssistantIndex = ((): number => {
|
|
1134
|
+
if (!uiBlocks || !isStreaming) {
|
|
1135
|
+
return -1;
|
|
1136
|
+
}
|
|
1137
|
+
for (let index = currentMessages.length - 1; index >= 0; index -= 1) {
|
|
1138
|
+
const role = currentMessages[index]?.role;
|
|
1139
|
+
if (role === "assistant") {
|
|
1140
|
+
return index;
|
|
1141
|
+
}
|
|
1142
|
+
if (role === "user") {
|
|
1143
|
+
return -1;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
return -1;
|
|
1147
|
+
})();
|
|
1148
|
+
const rows = currentMessages.flatMap((message, sourceIndex) => {
|
|
1149
|
+
const messageKey = message.id ?? `msg-${sourceIndex}`;
|
|
1150
|
+
const extras = appendedByMessage[messageKey] ?? [];
|
|
1151
|
+
return [
|
|
1152
|
+
{message, messageKey, sourceIndex},
|
|
1153
|
+
...extras.map((extra) => ({
|
|
1154
|
+
message: extra,
|
|
1155
|
+
messageKey: extra.id ?? `${messageKey}-extra`,
|
|
1156
|
+
sourceIndex: -1 as const,
|
|
1157
|
+
})),
|
|
1158
|
+
];
|
|
1159
|
+
});
|
|
1160
|
+
|
|
799
1161
|
return (
|
|
800
1162
|
<>
|
|
801
|
-
{
|
|
1163
|
+
{rows.map(({message, messageKey, sourceIndex}, index) => {
|
|
1164
|
+
if (message.role === "tool-call" && message.ask) {
|
|
1165
|
+
return (
|
|
1166
|
+
<AskTranscriptItem
|
|
1167
|
+
ask={withStoredResponse(message.ask, toolResults)}
|
|
1168
|
+
errors={askErrors?.[message.ask.toolCallId]}
|
|
1169
|
+
key={`ask-${message.ask.toolCallId}`}
|
|
1170
|
+
onAskSubmit={onAskSubmit}
|
|
1171
|
+
resolveAskFiles={resolveAskFiles}
|
|
1172
|
+
/>
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
if (
|
|
1176
|
+
message.role === "tool-result" &&
|
|
1177
|
+
message.toolResult &&
|
|
1178
|
+
askToolCallIds.has(message.toolResult.toolCallId)
|
|
1179
|
+
) {
|
|
1180
|
+
return null;
|
|
1181
|
+
}
|
|
802
1182
|
if (message.role === "tool-call" && message.toolCall) {
|
|
803
1183
|
return (
|
|
804
1184
|
<Box alignItems="start" key={`msg-${index}`} maxWidth="80%">
|
|
@@ -815,8 +1195,9 @@ const MessageList = ({
|
|
|
815
1195
|
}
|
|
816
1196
|
|
|
817
1197
|
const hasImages = message.contentParts?.some((p) => p.type === "image");
|
|
1198
|
+
const messageId = messageKey;
|
|
818
1199
|
return (
|
|
819
|
-
<Box alignItems={message.role === "user" ? "end" : "start"} key={
|
|
1200
|
+
<Box alignItems={message.role === "user" ? "end" : "start"} key={messageKey}>
|
|
820
1201
|
<Box
|
|
821
1202
|
color={message.role === "user" ? "primary" : "neutralLight"}
|
|
822
1203
|
maxWidth={hasImages ? "90%" : "80%"}
|
|
@@ -827,12 +1208,31 @@ const MessageList = ({
|
|
|
827
1208
|
hasContent={Boolean(message.content)}
|
|
828
1209
|
parts={message.contentParts}
|
|
829
1210
|
/>
|
|
830
|
-
<MessageText
|
|
1211
|
+
<MessageText
|
|
1212
|
+
allowHtml={allowHtml}
|
|
1213
|
+
content={message.content}
|
|
1214
|
+
hostActions={hostActions}
|
|
1215
|
+
imageHosts={imageHosts}
|
|
1216
|
+
isPartial={liveAssistantIndex >= 0 && sourceIndex === liveAssistantIndex}
|
|
1217
|
+
messageId={messageId}
|
|
1218
|
+
onBlockEvent={onBlockEvent}
|
|
1219
|
+
overrides={blockOverrides[messageId]}
|
|
1220
|
+
pendingElementIds={pendingElements[messageId]}
|
|
1221
|
+
resolveDataset={resolveDataset}
|
|
1222
|
+
resolveImage={resolveImage}
|
|
1223
|
+
role={message.role}
|
|
1224
|
+
uiBlocks={uiBlocks}
|
|
1225
|
+
/>
|
|
1226
|
+
{message.blockNote ? (
|
|
1227
|
+
<Text color="secondaryLight" size="sm" testID={`gpt-block-note-${messageId}`}>
|
|
1228
|
+
{message.blockNote}
|
|
1229
|
+
</Text>
|
|
1230
|
+
) : null}
|
|
831
1231
|
<AssistantActions
|
|
832
1232
|
handleCopyMessage={handleCopyMessage}
|
|
833
|
-
index={
|
|
1233
|
+
index={sourceIndex}
|
|
834
1234
|
message={message}
|
|
835
|
-
onRateFeedback={onRateFeedback}
|
|
1235
|
+
onRateFeedback={sourceIndex >= 0 ? onRateFeedback : undefined}
|
|
836
1236
|
/>
|
|
837
1237
|
</Box>
|
|
838
1238
|
</Box>
|
|
@@ -1000,15 +1400,20 @@ const SCROLL_TO_BOTTOM_THRESHOLD = 100;
|
|
|
1000
1400
|
const COMPOSER_MAX_HEIGHT = 200;
|
|
1001
1401
|
|
|
1002
1402
|
export const GPTChat = ({
|
|
1403
|
+
askErrors,
|
|
1003
1404
|
attachments = [],
|
|
1004
1405
|
availableModels,
|
|
1005
1406
|
currentHistoryId,
|
|
1006
1407
|
currentMessages,
|
|
1007
1408
|
geminiApiKey,
|
|
1008
1409
|
histories,
|
|
1410
|
+
hostActions,
|
|
1009
1411
|
isStreaming = false,
|
|
1010
1412
|
mcpTools,
|
|
1011
1413
|
mcpServers,
|
|
1414
|
+
onAskSubmit,
|
|
1415
|
+
onBlockAction,
|
|
1416
|
+
onBlockCallback,
|
|
1012
1417
|
onAttachFiles,
|
|
1013
1418
|
onCreateHistory,
|
|
1014
1419
|
onDeleteHistory,
|
|
@@ -1020,11 +1425,17 @@ export const GPTChat = ({
|
|
|
1020
1425
|
onSelectHistory,
|
|
1021
1426
|
onSubmit,
|
|
1022
1427
|
onUpdateTitle,
|
|
1428
|
+
resolveAskFiles,
|
|
1429
|
+
resolveDataset,
|
|
1430
|
+
resolveImage,
|
|
1023
1431
|
selectedModel,
|
|
1024
1432
|
mascot,
|
|
1025
1433
|
suggestedPrompts,
|
|
1026
1434
|
systemMemory,
|
|
1027
1435
|
testID,
|
|
1436
|
+
uiBlocks = false,
|
|
1437
|
+
allowHtml = false,
|
|
1438
|
+
imageHosts,
|
|
1028
1439
|
}: GPTChatProps): React.ReactElement => {
|
|
1029
1440
|
const [inputValue, setInputValue] = useState("");
|
|
1030
1441
|
const [editingHistoryId, setEditingHistoryId] = useState<string | null>(null);
|
|
@@ -1038,6 +1449,83 @@ export const GPTChat = ({
|
|
|
1038
1449
|
const [isApiKeyModalVisible, setIsApiKeyModalVisible] = useState(false);
|
|
1039
1450
|
const [isToolsModalVisible, setIsToolsModalVisible] = useState(false);
|
|
1040
1451
|
const [apiKeyDraft, setApiKeyDraft] = useState(geminiApiKey ?? "");
|
|
1452
|
+
const [blockOverrides, setBlockOverrides] = useState<Record<string, Record<string, Block>>>({});
|
|
1453
|
+
const [pendingElements, setPendingElements] = useState<Record<string, string[]>>({});
|
|
1454
|
+
const [appendedByMessage, setAppendedByMessage] = useState<Record<string, GPTChatMessage[]>>({});
|
|
1455
|
+
const [blockHistoryId, setBlockHistoryId] = useState(currentHistoryId);
|
|
1456
|
+
const historyIdRef = useRef(currentHistoryId);
|
|
1457
|
+
historyIdRef.current = currentHistoryId;
|
|
1458
|
+
if (blockHistoryId !== currentHistoryId) {
|
|
1459
|
+
setBlockHistoryId(currentHistoryId);
|
|
1460
|
+
setBlockOverrides({});
|
|
1461
|
+
setPendingElements({});
|
|
1462
|
+
setAppendedByMessage({});
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
const runBlockCallback = useCallback(
|
|
1466
|
+
async (event: BlockChatEvent): Promise<void> => {
|
|
1467
|
+
setPendingElements((current) => ({
|
|
1468
|
+
...current,
|
|
1469
|
+
[event.messageId]: [...(current[event.messageId] ?? []), event.elementId],
|
|
1470
|
+
}));
|
|
1471
|
+
const historyAtStart = historyIdRef.current;
|
|
1472
|
+
try {
|
|
1473
|
+
const result = await onBlockCallback?.(event);
|
|
1474
|
+
if (historyIdRef.current !== historyAtStart) {
|
|
1475
|
+
return;
|
|
1476
|
+
}
|
|
1477
|
+
if (result?.text) {
|
|
1478
|
+
const text = result.text;
|
|
1479
|
+
setAppendedByMessage((current) => {
|
|
1480
|
+
const existing = current[event.messageId] ?? [];
|
|
1481
|
+
return {
|
|
1482
|
+
...current,
|
|
1483
|
+
[event.messageId]: [
|
|
1484
|
+
...existing,
|
|
1485
|
+
{
|
|
1486
|
+
content: text,
|
|
1487
|
+
id: `block-text-${event.messageId}-${event.elementId}-${existing.length}`,
|
|
1488
|
+
role: "assistant",
|
|
1489
|
+
},
|
|
1490
|
+
],
|
|
1491
|
+
};
|
|
1492
|
+
});
|
|
1493
|
+
}
|
|
1494
|
+
if (result?.replace === "block" && result.blocks !== undefined) {
|
|
1495
|
+
const block = replacementBlock(result.blocks);
|
|
1496
|
+
if (block !== undefined) {
|
|
1497
|
+
setBlockOverrides((current) => ({
|
|
1498
|
+
...current,
|
|
1499
|
+
[event.messageId]: {...(current[event.messageId] ?? {}), [event.blockId]: block},
|
|
1500
|
+
}));
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
} finally {
|
|
1504
|
+
setPendingElements((current) => ({
|
|
1505
|
+
...current,
|
|
1506
|
+
[event.messageId]: (current[event.messageId] ?? []).filter(
|
|
1507
|
+
(id) => id !== event.elementId
|
|
1508
|
+
),
|
|
1509
|
+
}));
|
|
1510
|
+
}
|
|
1511
|
+
},
|
|
1512
|
+
[onBlockCallback]
|
|
1513
|
+
);
|
|
1514
|
+
|
|
1515
|
+
const handleBlockEvent = useCallback(
|
|
1516
|
+
(event: BlockChatEvent): void => {
|
|
1517
|
+
if (event.action.kind === "reply") {
|
|
1518
|
+
onSubmit(event.action.text);
|
|
1519
|
+
return;
|
|
1520
|
+
}
|
|
1521
|
+
if (event.action.kind === "callback") {
|
|
1522
|
+
void runBlockCallback(event);
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
onBlockAction?.(event);
|
|
1526
|
+
},
|
|
1527
|
+
[onBlockAction, onSubmit, runBlockCallback]
|
|
1528
|
+
);
|
|
1041
1529
|
|
|
1042
1530
|
const handleSubmit = useCallback(() => {
|
|
1043
1531
|
const trimmed = inputValue.trim();
|
|
@@ -1285,11 +1773,33 @@ export const GPTChat = ({
|
|
|
1285
1773
|
) : (
|
|
1286
1774
|
<>
|
|
1287
1775
|
<MessageList
|
|
1776
|
+
allowHtml={allowHtml}
|
|
1777
|
+
appendedByMessage={appendedByMessage}
|
|
1778
|
+
askErrors={askErrors}
|
|
1779
|
+
blockOverrides={blockOverrides}
|
|
1288
1780
|
currentMessages={currentMessages}
|
|
1289
1781
|
handleCopyMessage={handleCopyMessage}
|
|
1782
|
+
hostActions={hostActions}
|
|
1783
|
+
imageHosts={imageHosts}
|
|
1784
|
+
isStreaming={isStreaming}
|
|
1785
|
+
onAskSubmit={onAskSubmit}
|
|
1786
|
+
onBlockEvent={handleBlockEvent}
|
|
1290
1787
|
onRateFeedback={onRateFeedback}
|
|
1788
|
+
pendingElements={pendingElements}
|
|
1789
|
+
resolveAskFiles={resolveAskFiles}
|
|
1790
|
+
resolveDataset={resolveDataset}
|
|
1791
|
+
resolveImage={resolveImage}
|
|
1792
|
+
uiBlocks={uiBlocks}
|
|
1793
|
+
/>
|
|
1794
|
+
<StreamingIndicator
|
|
1795
|
+
isStreaming={
|
|
1796
|
+
isStreaming &&
|
|
1797
|
+
!(
|
|
1798
|
+
uiBlocks &&
|
|
1799
|
+
currentMessages[currentMessages.length - 1]?.role === "assistant"
|
|
1800
|
+
)
|
|
1801
|
+
}
|
|
1291
1802
|
/>
|
|
1292
|
-
<StreamingIndicator isStreaming={isStreaming} />
|
|
1293
1803
|
</>
|
|
1294
1804
|
)}
|
|
1295
1805
|
</Box>
|