@terreno/ui 57.8.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/AreaChart.d.ts +1 -1
- package/dist/AreaChart.d.ts.map +1 -1
- package/dist/AreaChart.js +70 -22
- package/dist/AreaChart.js.map +1 -1
- package/dist/BarChart.d.ts.map +1 -1
- package/dist/BarChart.js +55 -13
- package/dist/BarChart.js.map +1 -1
- package/dist/Button.d.ts.map +1 -1
- package/dist/Button.js +19 -4
- package/dist/Button.js.map +1 -1
- package/dist/ChartCard.d.ts +5 -0
- package/dist/ChartCard.d.ts.map +1 -0
- package/dist/ChartCard.js +18 -0
- package/dist/ChartCard.js.map +1 -0
- package/dist/Common.d.ts +147 -6
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/DashboardGrid.d.ts +6 -2
- package/dist/DashboardGrid.d.ts.map +1 -1
- package/dist/DashboardGrid.js +43 -10
- package/dist/DashboardGrid.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/DonutChart.d.ts.map +1 -1
- package/dist/DonutChart.js +20 -10
- package/dist/DonutChart.js.map +1 -1
- package/dist/DropdownMenuItem.d.ts +16 -0
- package/dist/DropdownMenuItem.d.ts.map +1 -0
- package/dist/DropdownMenuItem.js +11 -0
- package/dist/DropdownMenuItem.js.map +1 -0
- package/dist/FilePickerButton.d.ts +15 -1
- package/dist/FilePickerButton.d.ts.map +1 -1
- package/dist/FilePickerButton.js +90 -13
- 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 +301 -20
- 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/LineChart.d.ts +1 -1
- package/dist/LineChart.d.ts.map +1 -1
- package/dist/LineChart.js +62 -21
- package/dist/LineChart.js.map +1 -1
- package/dist/Scorecard.d.ts +5 -0
- package/dist/Scorecard.d.ts.map +1 -0
- package/dist/Scorecard.js +11 -0
- package/dist/Scorecard.js.map +1 -0
- package/dist/SparklineChart.d.ts +5 -0
- package/dist/SparklineChart.d.ts.map +1 -0
- package/dist/SparklineChart.js +35 -0
- package/dist/SparklineChart.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/TextField.d.ts.map +1 -1
- package/dist/TextField.js +12 -4
- package/dist/TextField.js.map +1 -1
- package/dist/ToastNotifications.d.ts.map +1 -1
- package/dist/ToastNotifications.js +29 -6
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +2 -1
- package/dist/WebDropdownMenu.d.ts.map +1 -1
- package/dist/WebDropdownMenu.js +2 -6
- package/dist/WebDropdownMenu.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/charts/ChartFacadeContainer.d.ts +10 -0
- package/dist/charts/ChartFacadeContainer.d.ts.map +1 -0
- package/dist/charts/ChartFacadeContainer.js +10 -0
- package/dist/charts/ChartFacadeContainer.js.map +1 -0
- package/dist/charts/ChartFrame.d.ts +4 -0
- package/dist/charts/ChartFrame.d.ts.map +1 -1
- package/dist/charts/ChartFrame.js +4 -2
- package/dist/charts/ChartFrame.js.map +1 -1
- package/dist/charts/layout.d.ts +9 -2
- package/dist/charts/layout.d.ts.map +1 -1
- package/dist/charts/layout.js +28 -6
- package/dist/charts/layout.js.map +1 -1
- package/dist/charts/scales.d.ts +3 -1
- package/dist/charts/scales.d.ts.map +1 -1
- package/dist/charts/scales.js +7 -7
- package/dist/charts/scales.js.map +1 -1
- package/dist/charts/series.d.ts +15 -0
- package/dist/charts/series.d.ts.map +1 -0
- package/dist/charts/series.js +19 -0
- package/dist/charts/series.js.map +1 -0
- package/dist/charts/types/chartTypes.d.ts +7 -0
- package/dist/charts/types/chartTypes.d.ts.map +1 -1
- package/dist/dashboardGridLayout.d.ts +6 -0
- package/dist/dashboardGridLayout.d.ts.map +1 -1
- package/dist/dashboardGridLayout.js +19 -3
- package/dist/dashboardGridLayout.js.map +1 -1
- package/dist/index.d.ts +9 -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 +15 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +10 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/resolveDocumentBodyPortalTarget.d.ts +7 -0
- package/dist/resolveDocumentBodyPortalTarget.d.ts.map +1 -0
- package/dist/resolveDocumentBodyPortalTarget.js +12 -0
- package/dist/resolveDocumentBodyPortalTarget.js.map +1 -0
- package/package.json +2 -1
- package/src/AIRequestExplorer.test.tsx +2 -2
- package/src/AreaChart.test.tsx +81 -0
- package/src/AreaChart.tsx +171 -62
- package/src/BarChart.test.tsx +119 -0
- package/src/BarChart.tsx +94 -20
- package/src/Button.test.tsx +72 -0
- package/src/Button.tsx +34 -4
- package/src/ChartCard.test.tsx +79 -0
- package/src/ChartCard.tsx +80 -0
- package/src/Common.ts +136 -2
- package/src/DashboardGrid.test.tsx +98 -1
- package/src/DashboardGrid.tsx +71 -20
- package/src/DateTimeField.test.tsx +185 -2
- package/src/DateTimeField.tsx +106 -16
- package/src/DonutChart.test.tsx +78 -10
- package/src/DonutChart.tsx +87 -14
- package/src/DropdownMenuItem.tsx +48 -0
- package/src/FilePickerButton.test.tsx +116 -9
- package/src/FilePickerButton.tsx +177 -23
- package/src/GPTChat.test.tsx +891 -8
- package/src/GPTChat.tsx +713 -45
- package/src/HtmlFrame.tsx +60 -0
- package/src/LineChart.test.tsx +109 -10
- package/src/LineChart.tsx +154 -58
- package/src/Scorecard.test.tsx +65 -0
- package/src/Scorecard.tsx +45 -0
- package/src/SparklineChart.test.tsx +69 -0
- package/src/SparklineChart.tsx +85 -0
- package/src/Text.test.tsx +22 -0
- package/src/Text.tsx +10 -1
- package/src/TextField.test.tsx +16 -0
- package/src/TextField.tsx +13 -3
- package/src/ToastNotifications.tsx +33 -6
- package/src/ToastNotifications.webModal.test.tsx +285 -0
- package/src/WebDropdownMenu.tsx +3 -7
- 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/bunSetup.ts +1 -0
- package/src/charts/ChartFacadeContainer.tsx +35 -0
- package/src/charts/ChartFrame.tsx +39 -1
- package/src/charts/layout.test.ts +22 -1
- package/src/charts/layout.ts +37 -4
- package/src/charts/scales.test.ts +33 -0
- package/src/charts/scales.ts +10 -6
- package/src/charts/series.ts +43 -0
- package/src/charts/types/chartTypes.ts +8 -0
- package/src/dashboardGridLayout.test.ts +59 -1
- package/src/dashboardGridLayout.ts +30 -3
- package/src/index.tsx +13 -0
- package/src/isolated/HtmlFrame.isolated.tsx +54 -0
- package/src/lazyBoundaries/heavyOptionalExports.test.tsx +2 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +35 -0
- package/src/resolveDocumentBodyPortalTarget.ts +11 -0
package/src/GPTChat.tsx
CHANGED
|
@@ -1,14 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AskValidationError,
|
|
3
|
+
askResponseSchema,
|
|
4
|
+
type Block,
|
|
5
|
+
type BlockAction,
|
|
6
|
+
type BlocksDocument,
|
|
7
|
+
parseBlocksPartial,
|
|
8
|
+
} from "@terreno/blocks";
|
|
9
|
+
import {DateTime} from "luxon";
|
|
1
10
|
import React, {useCallback, useEffect, useRef, useState} from "react";
|
|
2
11
|
import {
|
|
12
|
+
AccessibilityInfo,
|
|
13
|
+
findNodeHandle,
|
|
14
|
+
type Text as NativeText,
|
|
3
15
|
Platform,
|
|
4
16
|
Image as RNImage,
|
|
5
17
|
type ScrollView as RNScrollView,
|
|
6
18
|
type TextInput as RNTextInput,
|
|
19
|
+
View,
|
|
7
20
|
} from "react-native";
|
|
8
21
|
|
|
9
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";
|
|
10
26
|
import {Box} from "./Box";
|
|
11
27
|
import {Button} from "./Button";
|
|
28
|
+
import {BlocksView} from "./blocks/BlocksView";
|
|
29
|
+
import type {BlocksViewProps} from "./Common";
|
|
30
|
+
import {DropdownMenuItem} from "./DropdownMenuItem";
|
|
31
|
+
import {DropdownPanel} from "./DropdownPanel";
|
|
12
32
|
import type {SelectedFile} from "./FilePickerButton";
|
|
13
33
|
import {FilePickerButton} from "./FilePickerButton";
|
|
14
34
|
import {Heading} from "./Heading";
|
|
@@ -66,8 +86,31 @@ export interface ToolResultInfo {
|
|
|
66
86
|
// Message Types
|
|
67
87
|
// ============================================================
|
|
68
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
|
+
|
|
69
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;
|
|
70
111
|
content: string;
|
|
112
|
+
/** Stable id for block actions. Falls back to the message index. */
|
|
113
|
+
id?: string;
|
|
71
114
|
contentParts?: MessageContentPart[];
|
|
72
115
|
rating?: "up" | "down";
|
|
73
116
|
role: "user" | "assistant" | "system" | "tool-call" | "tool-result";
|
|
@@ -93,16 +136,31 @@ export interface MCPToolDetail {
|
|
|
93
136
|
}
|
|
94
137
|
|
|
95
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[]>;
|
|
96
141
|
attachments?: SelectedFile[];
|
|
97
142
|
availableModels?: Array<{label: string; value: string}>;
|
|
98
143
|
currentHistoryId?: string;
|
|
99
144
|
currentMessages: GPTChatMessage[];
|
|
100
145
|
geminiApiKey?: string;
|
|
101
146
|
histories: GPTChatHistory[];
|
|
147
|
+
/** Callback names the host will run. A callback outside this list is disabled. */
|
|
148
|
+
hostActions?: readonly string[];
|
|
102
149
|
isStreaming?: boolean;
|
|
103
150
|
/** Available MCP tools to display in the tools panel. */
|
|
104
151
|
mcpTools?: MCPToolDetail[];
|
|
105
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;
|
|
106
164
|
onAttachFiles?: (files: SelectedFile[]) => void;
|
|
107
165
|
onCreateHistory: () => void;
|
|
108
166
|
onDeleteHistory: (id: string) => void;
|
|
@@ -114,6 +172,15 @@ export interface GPTChatProps {
|
|
|
114
172
|
onSelectHistory: (id: string) => void;
|
|
115
173
|
onSubmit: (prompt: string) => void;
|
|
116
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"];
|
|
117
184
|
selectedModel?: string;
|
|
118
185
|
/**
|
|
119
186
|
* Optional consumer-owned character for an empty chat. Terreno does not ship a
|
|
@@ -124,6 +191,15 @@ export interface GPTChatProps {
|
|
|
124
191
|
suggestedPrompts?: string[];
|
|
125
192
|
systemMemory?: string;
|
|
126
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[];
|
|
127
203
|
}
|
|
128
204
|
|
|
129
205
|
// ============================================================
|
|
@@ -228,18 +304,103 @@ const handleDownloadFile = (url: string, filename: string): void => {
|
|
|
228
304
|
document.body.removeChild(link);
|
|
229
305
|
};
|
|
230
306
|
|
|
307
|
+
const IMAGE_EXTENSIONS: Record<string, string> = {
|
|
308
|
+
"image/gif": "gif",
|
|
309
|
+
"image/jpeg": "jpg",
|
|
310
|
+
"image/png": "png",
|
|
311
|
+
"image/webp": "webp",
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const getImageFilename = (part: ImageContentPart): string => {
|
|
315
|
+
const extension = IMAGE_EXTENSIONS[part.mimeType ?? ""] ?? "png";
|
|
316
|
+
return `image-${DateTime.now().toMillis()}.${extension}`;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
// Browsers reliably accept only PNG on the clipboard, so other formats are redrawn as PNG.
|
|
320
|
+
const convertBlobToPng = async (blob: Blob): Promise<Blob> => {
|
|
321
|
+
if (blob.type === "image/png") {
|
|
322
|
+
return blob;
|
|
323
|
+
}
|
|
324
|
+
const bitmap = await createImageBitmap(blob);
|
|
325
|
+
const canvas = document.createElement("canvas");
|
|
326
|
+
canvas.width = bitmap.width;
|
|
327
|
+
canvas.height = bitmap.height;
|
|
328
|
+
canvas.getContext("2d")?.drawImage(bitmap, 0, 0);
|
|
329
|
+
return new Promise((resolve, reject) => {
|
|
330
|
+
canvas.toBlob((png) => (png ? resolve(png) : reject(new Error("PNG conversion failed"))));
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/** Copies the image itself (not a text placeholder) to the clipboard. */
|
|
335
|
+
const copyImageToClipboard = async (part: ImageContentPart): Promise<void> => {
|
|
336
|
+
if (Platform.OS === "web") {
|
|
337
|
+
if (typeof ClipboardItem === "undefined" || !navigator.clipboard?.write) {
|
|
338
|
+
console.warn("Copying images is not supported in this browser");
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const response = await fetch(part.url);
|
|
342
|
+
const png = await convertBlobToPng(await response.blob());
|
|
343
|
+
await navigator.clipboard.write([new ClipboardItem({"image/png": png})]);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const Clipboard = await import("expo-clipboard");
|
|
347
|
+
const base64 = part.url.startsWith("data:") ? part.url.split(",")[1] : undefined;
|
|
348
|
+
if (base64) {
|
|
349
|
+
await Clipboard.setImageAsync(base64);
|
|
350
|
+
} else {
|
|
351
|
+
await Clipboard.setStringAsync(part.url);
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const ImageActions = ({part}: {part: ImageContentPart}): React.ReactElement => {
|
|
356
|
+
const handleCopy = useCallback(async (): Promise<void> => {
|
|
357
|
+
try {
|
|
358
|
+
await copyImageToClipboard(part);
|
|
359
|
+
} catch (error) {
|
|
360
|
+
console.warn("Failed to copy image", error);
|
|
361
|
+
}
|
|
362
|
+
}, [part]);
|
|
363
|
+
|
|
364
|
+
const handleDownload = useCallback((): void => {
|
|
365
|
+
handleDownloadFile(part.url, getImageFilename(part));
|
|
366
|
+
}, [part]);
|
|
367
|
+
|
|
368
|
+
return (
|
|
369
|
+
<Box direction="row" gap={1} testID="gpt-image-actions">
|
|
370
|
+
<IconButton
|
|
371
|
+
accessibilityLabel="Copy image"
|
|
372
|
+
iconName="copy"
|
|
373
|
+
onClick={handleCopy}
|
|
374
|
+
testID="gpt-copy-image"
|
|
375
|
+
variant="ghost"
|
|
376
|
+
/>
|
|
377
|
+
{Platform.OS === "web" ? (
|
|
378
|
+
<IconButton
|
|
379
|
+
accessibilityLabel="Download image"
|
|
380
|
+
iconName="download"
|
|
381
|
+
onClick={handleDownload}
|
|
382
|
+
testID="gpt-download-image"
|
|
383
|
+
variant="ghost"
|
|
384
|
+
/>
|
|
385
|
+
) : null}
|
|
386
|
+
</Box>
|
|
387
|
+
);
|
|
388
|
+
};
|
|
389
|
+
|
|
231
390
|
const MessageContentParts = ({parts}: {parts: MessageContentPart[]}): React.ReactElement => {
|
|
232
391
|
return (
|
|
233
392
|
<Box gap={2}>
|
|
234
393
|
{parts.map((part, index) => {
|
|
235
394
|
if (part.type === "image") {
|
|
236
395
|
return (
|
|
237
|
-
<
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
396
|
+
<Box gap={1} key={`content-${index}`}>
|
|
397
|
+
<RNImage
|
|
398
|
+
resizeMode="contain"
|
|
399
|
+
source={{uri: part.url}}
|
|
400
|
+
style={{borderRadius: 8, height: 400, maxWidth: 800, minWidth: 400, width: "100%"}}
|
|
401
|
+
/>
|
|
402
|
+
<ImageActions part={part} />
|
|
403
|
+
</Box>
|
|
243
404
|
);
|
|
244
405
|
}
|
|
245
406
|
if (part.type === "file") {
|
|
@@ -464,25 +625,44 @@ const HistoryItemTitle = ({
|
|
|
464
625
|
);
|
|
465
626
|
}
|
|
466
627
|
return (
|
|
467
|
-
<
|
|
468
|
-
{history.
|
|
469
|
-
|
|
628
|
+
<Box flex="grow" minWidth={0}>
|
|
629
|
+
<Text color={history.id === currentHistoryId ? "inverted" : "primary"} size="sm" truncate>
|
|
630
|
+
{history.title ?? "New Chat"}
|
|
631
|
+
</Text>
|
|
632
|
+
</Box>
|
|
470
633
|
);
|
|
471
634
|
};
|
|
472
635
|
|
|
473
|
-
const
|
|
636
|
+
const HistoryItemActions = ({
|
|
474
637
|
editingHistoryId,
|
|
475
638
|
handleFinishRename,
|
|
476
639
|
handleStartRename,
|
|
477
640
|
history,
|
|
641
|
+
isSelected,
|
|
642
|
+
onDeleteHistory,
|
|
478
643
|
onUpdateTitle,
|
|
479
644
|
}: {
|
|
480
645
|
editingHistoryId: string | null;
|
|
481
646
|
handleFinishRename: () => void;
|
|
482
647
|
handleStartRename: (id: string, title: string) => void;
|
|
483
648
|
history: GPTChatHistory;
|
|
649
|
+
isSelected: boolean;
|
|
650
|
+
onDeleteHistory: (id: string) => void;
|
|
484
651
|
onUpdateTitle?: (id: string, title: string) => void;
|
|
485
|
-
}): React.ReactElement
|
|
652
|
+
}): React.ReactElement => {
|
|
653
|
+
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
|
654
|
+
const title = history.title ?? "New Chat";
|
|
655
|
+
|
|
656
|
+
const handleRename = useCallback((): void => {
|
|
657
|
+
setIsMenuOpen(false);
|
|
658
|
+
handleStartRename(history.id, history.title ?? "");
|
|
659
|
+
}, [handleStartRename, history.id, history.title]);
|
|
660
|
+
|
|
661
|
+
const handleDelete = useCallback((): void => {
|
|
662
|
+
setIsMenuOpen(false);
|
|
663
|
+
onDeleteHistory(history.id);
|
|
664
|
+
}, [history.id, onDeleteHistory]);
|
|
665
|
+
|
|
486
666
|
if (editingHistoryId === history.id) {
|
|
487
667
|
return (
|
|
488
668
|
<IconButton
|
|
@@ -493,16 +673,45 @@ const HistoryItemActionButton = ({
|
|
|
493
673
|
/>
|
|
494
674
|
);
|
|
495
675
|
}
|
|
496
|
-
|
|
497
|
-
return null;
|
|
498
|
-
}
|
|
676
|
+
|
|
499
677
|
return (
|
|
500
|
-
<
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
678
|
+
<DropdownPanel
|
|
679
|
+
align="auto"
|
|
680
|
+
isOpen={isMenuOpen}
|
|
681
|
+
onOpenChange={setIsMenuOpen}
|
|
682
|
+
renderTrigger={({toggle}) => (
|
|
683
|
+
<IconButton
|
|
684
|
+
accessibilityLabel={`Chat actions: ${title}`}
|
|
685
|
+
iconName="ellipsis-vertical"
|
|
686
|
+
onClick={toggle}
|
|
687
|
+
testID={`gpt-history-menu-${history.id}`}
|
|
688
|
+
variant={isSelected ? "primary" : "ghost"}
|
|
689
|
+
/>
|
|
690
|
+
)}
|
|
691
|
+
showActionButtons={false}
|
|
692
|
+
testID={`gpt-history-menu-panel-${history.id}`}
|
|
693
|
+
width={180}
|
|
694
|
+
>
|
|
695
|
+
<Box gap={1}>
|
|
696
|
+
{onUpdateTitle ? (
|
|
697
|
+
<DropdownMenuItem
|
|
698
|
+
accessibilityLabel={`Rename chat: ${title}`}
|
|
699
|
+
iconName="pen-to-square"
|
|
700
|
+
label="Rename"
|
|
701
|
+
onClick={handleRename}
|
|
702
|
+
testID={`gpt-rename-history-${history.id}`}
|
|
703
|
+
/>
|
|
704
|
+
) : null}
|
|
705
|
+
<DropdownMenuItem
|
|
706
|
+
accessibilityLabel={`Delete chat: ${title}`}
|
|
707
|
+
color="error"
|
|
708
|
+
iconName="trash-can"
|
|
709
|
+
label="Delete"
|
|
710
|
+
onClick={handleDelete}
|
|
711
|
+
testID={`gpt-delete-history-${history.id}`}
|
|
712
|
+
/>
|
|
713
|
+
</Box>
|
|
714
|
+
</DropdownPanel>
|
|
506
715
|
);
|
|
507
716
|
};
|
|
508
717
|
|
|
@@ -524,7 +733,149 @@ const ContentPartsPreview = ({
|
|
|
524
733
|
);
|
|
525
734
|
};
|
|
526
735
|
|
|
527
|
-
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
|
+
}
|
|
528
879
|
if (role === "assistant") {
|
|
529
880
|
return <MarkdownView>{content}</MarkdownView>;
|
|
530
881
|
}
|
|
@@ -569,7 +920,7 @@ const AssistantActions = ({
|
|
|
569
920
|
message,
|
|
570
921
|
onRateFeedback,
|
|
571
922
|
}: {
|
|
572
|
-
handleCopyMessage: (
|
|
923
|
+
handleCopyMessage: (message: GPTChatMessage) => void;
|
|
573
924
|
index: number;
|
|
574
925
|
message: GPTChatMessage;
|
|
575
926
|
onRateFeedback?: (promptIndex: number, rating: "up" | "down" | null) => void;
|
|
@@ -583,7 +934,7 @@ const AssistantActions = ({
|
|
|
583
934
|
<IconButton
|
|
584
935
|
accessibilityLabel="Copy message"
|
|
585
936
|
iconName="copy"
|
|
586
|
-
onClick={() => handleCopyMessage(message
|
|
937
|
+
onClick={() => handleCopyMessage(message)}
|
|
587
938
|
testID={`gpt-copy-msg-${index}`}
|
|
588
939
|
/>
|
|
589
940
|
</Box>
|
|
@@ -651,18 +1002,183 @@ const EmptyChatHero = ({
|
|
|
651
1002
|
);
|
|
652
1003
|
};
|
|
653
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
|
+
|
|
654
1083
|
const MessageList = ({
|
|
1084
|
+
allowHtml,
|
|
1085
|
+
appendedByMessage,
|
|
1086
|
+
askErrors,
|
|
1087
|
+
blockOverrides,
|
|
655
1088
|
currentMessages,
|
|
656
1089
|
handleCopyMessage,
|
|
1090
|
+
hostActions,
|
|
1091
|
+
imageHosts,
|
|
1092
|
+
isStreaming,
|
|
1093
|
+
onAskSubmit,
|
|
1094
|
+
onBlockEvent,
|
|
657
1095
|
onRateFeedback,
|
|
1096
|
+
pendingElements,
|
|
1097
|
+
resolveAskFiles,
|
|
1098
|
+
resolveDataset,
|
|
1099
|
+
resolveImage,
|
|
1100
|
+
uiBlocks,
|
|
658
1101
|
}: {
|
|
1102
|
+
allowHtml?: boolean;
|
|
1103
|
+
appendedByMessage: Record<string, GPTChatMessage[]>;
|
|
1104
|
+
askErrors?: Record<string, AskValidationError[]>;
|
|
1105
|
+
blockOverrides: Record<string, Record<string, Block>>;
|
|
659
1106
|
currentMessages: GPTChatMessage[];
|
|
660
|
-
handleCopyMessage: (
|
|
1107
|
+
handleCopyMessage: (message: GPTChatMessage) => void;
|
|
1108
|
+
hostActions?: readonly string[];
|
|
1109
|
+
imageHosts?: readonly string[];
|
|
1110
|
+
isStreaming: boolean;
|
|
1111
|
+
onAskSubmit?: AskSubmitHandler;
|
|
1112
|
+
onBlockEvent: (event: BlockChatEvent) => void;
|
|
661
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;
|
|
662
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
|
+
|
|
663
1161
|
return (
|
|
664
1162
|
<>
|
|
665
|
-
{
|
|
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
|
+
}
|
|
666
1182
|
if (message.role === "tool-call" && message.toolCall) {
|
|
667
1183
|
return (
|
|
668
1184
|
<Box alignItems="start" key={`msg-${index}`} maxWidth="80%">
|
|
@@ -679,8 +1195,9 @@ const MessageList = ({
|
|
|
679
1195
|
}
|
|
680
1196
|
|
|
681
1197
|
const hasImages = message.contentParts?.some((p) => p.type === "image");
|
|
1198
|
+
const messageId = messageKey;
|
|
682
1199
|
return (
|
|
683
|
-
<Box alignItems={message.role === "user" ? "end" : "start"} key={
|
|
1200
|
+
<Box alignItems={message.role === "user" ? "end" : "start"} key={messageKey}>
|
|
684
1201
|
<Box
|
|
685
1202
|
color={message.role === "user" ? "primary" : "neutralLight"}
|
|
686
1203
|
maxWidth={hasImages ? "90%" : "80%"}
|
|
@@ -691,12 +1208,31 @@ const MessageList = ({
|
|
|
691
1208
|
hasContent={Boolean(message.content)}
|
|
692
1209
|
parts={message.contentParts}
|
|
693
1210
|
/>
|
|
694
|
-
<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}
|
|
695
1231
|
<AssistantActions
|
|
696
1232
|
handleCopyMessage={handleCopyMessage}
|
|
697
|
-
index={
|
|
1233
|
+
index={sourceIndex}
|
|
698
1234
|
message={message}
|
|
699
|
-
onRateFeedback={onRateFeedback}
|
|
1235
|
+
onRateFeedback={sourceIndex >= 0 ? onRateFeedback : undefined}
|
|
700
1236
|
/>
|
|
701
1237
|
</Box>
|
|
702
1238
|
</Box>
|
|
@@ -858,16 +1394,26 @@ const ApiKeyModal = ({
|
|
|
858
1394
|
// Main Component
|
|
859
1395
|
// ============================================================
|
|
860
1396
|
|
|
1397
|
+
/** Pixels of content below the viewport before "Scroll to bottom" appears. */
|
|
1398
|
+
const SCROLL_TO_BOTTOM_THRESHOLD = 100;
|
|
1399
|
+
/** Height the composer grows to before its text scrolls. */
|
|
1400
|
+
const COMPOSER_MAX_HEIGHT = 200;
|
|
1401
|
+
|
|
861
1402
|
export const GPTChat = ({
|
|
1403
|
+
askErrors,
|
|
862
1404
|
attachments = [],
|
|
863
1405
|
availableModels,
|
|
864
1406
|
currentHistoryId,
|
|
865
1407
|
currentMessages,
|
|
866
1408
|
geminiApiKey,
|
|
867
1409
|
histories,
|
|
1410
|
+
hostActions,
|
|
868
1411
|
isStreaming = false,
|
|
869
1412
|
mcpTools,
|
|
870
1413
|
mcpServers,
|
|
1414
|
+
onAskSubmit,
|
|
1415
|
+
onBlockAction,
|
|
1416
|
+
onBlockCallback,
|
|
871
1417
|
onAttachFiles,
|
|
872
1418
|
onCreateHistory,
|
|
873
1419
|
onDeleteHistory,
|
|
@@ -879,11 +1425,17 @@ export const GPTChat = ({
|
|
|
879
1425
|
onSelectHistory,
|
|
880
1426
|
onSubmit,
|
|
881
1427
|
onUpdateTitle,
|
|
1428
|
+
resolveAskFiles,
|
|
1429
|
+
resolveDataset,
|
|
1430
|
+
resolveImage,
|
|
882
1431
|
selectedModel,
|
|
883
1432
|
mascot,
|
|
884
1433
|
suggestedPrompts,
|
|
885
1434
|
systemMemory,
|
|
886
1435
|
testID,
|
|
1436
|
+
uiBlocks = false,
|
|
1437
|
+
allowHtml = false,
|
|
1438
|
+
imageHosts,
|
|
887
1439
|
}: GPTChatProps): React.ReactElement => {
|
|
888
1440
|
const [inputValue, setInputValue] = useState("");
|
|
889
1441
|
const [editingHistoryId, setEditingHistoryId] = useState<string | null>(null);
|
|
@@ -897,6 +1449,83 @@ export const GPTChat = ({
|
|
|
897
1449
|
const [isApiKeyModalVisible, setIsApiKeyModalVisible] = useState(false);
|
|
898
1450
|
const [isToolsModalVisible, setIsToolsModalVisible] = useState(false);
|
|
899
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
|
+
);
|
|
900
1529
|
|
|
901
1530
|
const handleSubmit = useCallback(() => {
|
|
902
1531
|
const trimmed = inputValue.trim();
|
|
@@ -943,9 +1572,21 @@ export const GPTChat = ({
|
|
|
943
1572
|
return () => el.removeEventListener("keydown", handler);
|
|
944
1573
|
}, [inputElement]);
|
|
945
1574
|
|
|
946
|
-
|
|
1575
|
+
// Image-only replies have no meaningful text, so copy the image instead of a placeholder.
|
|
1576
|
+
const handleCopyMessage = useCallback(async (message: GPTChatMessage) => {
|
|
1577
|
+
const firstImage = message.contentParts?.find(
|
|
1578
|
+
(part): part is ImageContentPart => part.type === "image"
|
|
1579
|
+
);
|
|
1580
|
+
if (firstImage && !message.content.trim()) {
|
|
1581
|
+
try {
|
|
1582
|
+
await copyImageToClipboard(firstImage);
|
|
1583
|
+
} catch (error) {
|
|
1584
|
+
console.warn("Failed to copy image", error);
|
|
1585
|
+
}
|
|
1586
|
+
return;
|
|
1587
|
+
}
|
|
947
1588
|
const Clipboard = await import("expo-clipboard");
|
|
948
|
-
await Clipboard.setStringAsync(
|
|
1589
|
+
await Clipboard.setStringAsync(message.content);
|
|
949
1590
|
}, []);
|
|
950
1591
|
|
|
951
1592
|
const scrollToBottom = useCallback(() => {
|
|
@@ -963,12 +1604,17 @@ export const GPTChat = ({
|
|
|
963
1604
|
const handleScroll = useCallback((offsetY: number) => {
|
|
964
1605
|
scrollOffsetRef.current = offsetY;
|
|
965
1606
|
const distanceFromBottom = contentHeightRef.current - offsetY - viewportHeightRef.current;
|
|
966
|
-
setIsScrolledUp(distanceFromBottom >
|
|
1607
|
+
setIsScrolledUp(distanceFromBottom > SCROLL_TO_BOTTOM_THRESHOLD);
|
|
967
1608
|
}, []);
|
|
968
1609
|
|
|
1610
|
+
// Growing content (a streaming reply) must not count as the user scrolling up, but content
|
|
1611
|
+
// that shrinks to fit the viewport (switching to a short or empty chat) clears the flag.
|
|
969
1612
|
const handleContentLayout = useCallback(
|
|
970
|
-
(
|
|
971
|
-
contentHeightRef.current =
|
|
1613
|
+
(event: {nativeEvent: {layout: {height: number; width: number; x: number; y: number}}}) => {
|
|
1614
|
+
contentHeightRef.current = event.nativeEvent.layout.height;
|
|
1615
|
+
if (contentHeightRef.current <= viewportHeightRef.current + SCROLL_TO_BOTTOM_THRESHOLD) {
|
|
1616
|
+
setIsScrolledUp(false);
|
|
1617
|
+
}
|
|
972
1618
|
},
|
|
973
1619
|
[]
|
|
974
1620
|
);
|
|
@@ -1095,21 +1741,16 @@ export const GPTChat = ({
|
|
|
1095
1741
|
history={history}
|
|
1096
1742
|
setEditingTitle={setEditingTitle}
|
|
1097
1743
|
/>
|
|
1098
|
-
<Box
|
|
1099
|
-
<
|
|
1744
|
+
<Box marginLeft={1}>
|
|
1745
|
+
<HistoryItemActions
|
|
1100
1746
|
editingHistoryId={editingHistoryId}
|
|
1101
1747
|
handleFinishRename={handleFinishRename}
|
|
1102
1748
|
handleStartRename={handleStartRename}
|
|
1103
1749
|
history={history}
|
|
1750
|
+
isSelected={history.id === currentHistoryId}
|
|
1751
|
+
onDeleteHistory={onDeleteHistory}
|
|
1104
1752
|
onUpdateTitle={onUpdateTitle}
|
|
1105
1753
|
/>
|
|
1106
|
-
<IconButton
|
|
1107
|
-
accessibilityLabel={`Delete chat: ${history.title ?? "New Chat"}`}
|
|
1108
|
-
iconName="trash"
|
|
1109
|
-
onClick={() => onDeleteHistory(history.id)}
|
|
1110
|
-
testID={`gpt-delete-history-${history.id}`}
|
|
1111
|
-
variant="destructive"
|
|
1112
|
-
/>
|
|
1113
1754
|
</Box>
|
|
1114
1755
|
</Box>
|
|
1115
1756
|
))}
|
|
@@ -1132,18 +1773,43 @@ export const GPTChat = ({
|
|
|
1132
1773
|
) : (
|
|
1133
1774
|
<>
|
|
1134
1775
|
<MessageList
|
|
1776
|
+
allowHtml={allowHtml}
|
|
1777
|
+
appendedByMessage={appendedByMessage}
|
|
1778
|
+
askErrors={askErrors}
|
|
1779
|
+
blockOverrides={blockOverrides}
|
|
1135
1780
|
currentMessages={currentMessages}
|
|
1136
1781
|
handleCopyMessage={handleCopyMessage}
|
|
1782
|
+
hostActions={hostActions}
|
|
1783
|
+
imageHosts={imageHosts}
|
|
1784
|
+
isStreaming={isStreaming}
|
|
1785
|
+
onAskSubmit={onAskSubmit}
|
|
1786
|
+
onBlockEvent={handleBlockEvent}
|
|
1137
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
|
+
}
|
|
1138
1802
|
/>
|
|
1139
|
-
<StreamingIndicator isStreaming={isStreaming} />
|
|
1140
1803
|
</>
|
|
1141
1804
|
)}
|
|
1142
1805
|
</Box>
|
|
1143
1806
|
</Box>
|
|
1144
1807
|
</Box>
|
|
1145
1808
|
|
|
1146
|
-
<ScrollToBottomButton
|
|
1809
|
+
<ScrollToBottomButton
|
|
1810
|
+
isScrolledUp={isScrolledUp && !isEmptyChat}
|
|
1811
|
+
scrollToBottom={scrollToBottom}
|
|
1812
|
+
/>
|
|
1147
1813
|
<AttachmentSection attachments={attachments} onRemoveAttachment={onRemoveAttachment} />
|
|
1148
1814
|
|
|
1149
1815
|
{/* Input */}
|
|
@@ -1171,7 +1837,9 @@ export const GPTChat = ({
|
|
|
1171
1837
|
<TextArea
|
|
1172
1838
|
blurOnSubmit={false}
|
|
1173
1839
|
disabled={isStreaming}
|
|
1840
|
+
grow
|
|
1174
1841
|
inputRef={handleInputRef}
|
|
1842
|
+
maxHeight={COMPOSER_MAX_HEIGHT}
|
|
1175
1843
|
onChange={setInputValue}
|
|
1176
1844
|
placeholder="Type a message..."
|
|
1177
1845
|
testID="gpt-input"
|