@puredesktop/puredesktop-ui-bridge 2.1.8 → 2.1.9
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/package.json +35 -5
- package/src/agents/mcpServerPresets.test.ts +105 -0
- package/src/agents/mcpServerPresets.ts +380 -0
- package/src/agents/runtime/index.ts +7 -0
- package/src/agents/runtime/mapAgentSnapshot.ts +11 -8
- package/src/agents/runtime/pendingAsks.test.ts +117 -0
- package/src/agents/runtime/pendingAsks.ts +110 -0
- package/src/bridge/agentScopeDefaults.ts +65 -0
- package/src/bridge/agentTypes.ts +36 -2
- package/src/bridge/agents.d.mts +6 -0
- package/src/bridge/agents.mjs +10 -0
- package/src/bridge/agents.ts +4 -0
- package/src/bridge/credentials.d.mts +62 -0
- package/src/bridge/credentials.mjs +53 -0
- package/src/bridge/events.d.mts +1 -0
- package/src/bridge/events.mjs +1 -0
- package/src/bridge/googleAuth.ts +7 -152
- package/src/bridge/mcpClient.d.mts +74 -0
- package/src/bridge/mcpClient.mjs +79 -0
- package/src/bridge/methods.d.mts +22 -0
- package/src/bridge/methods.mjs +23 -0
- package/src/bridge/notifications.d.mts +12 -0
- package/src/bridge/notifications.mjs +36 -0
- package/src/bridge/operations.d.mts +44 -0
- package/src/bridge/operations.mjs +40 -0
- package/src/bridge/react/usePlatformAgentSessionDrive.test.tsx +29 -10
- package/src/bridge/react/usePlatformAgentSessionDrive.tsx +10 -81
- package/src/bridge/react/usePlatformAppSettings.test.tsx +105 -105
- package/src/bridge/react/usePlatformAppSettings.tsx +104 -104
- package/src/bridge/react/usePlatformJsonStore.test.tsx +152 -152
- package/src/bridge/react/usePlatformJsonStore.tsx +149 -149
- package/src/bridge/react/usePlatformOperations.tsx +75 -0
- package/src/bridge/storage.d.mts +17 -4
- package/src/bridge/storage.mjs +6 -3
- package/src/components/agents/AgentComposer.tsx +10 -6
- package/src/components/agents/AgentDrawerPanel.test.tsx +2 -205
- package/src/components/agents/AgentDrawerPanel.tsx +23 -64
- package/src/components/agents/AgentMessageBubble.tsx +59 -33
- package/src/components/agents/AgentMessageList.tsx +4 -9
- package/src/components/agents/AgentQuestionPromptList.tsx +34 -212
- package/src/components/agents/AgentToolApprovalActions.tsx +13 -11
- package/src/components/agents/AgentToolPendingList.tsx +6 -6
- package/src/components/agents/AgentToolPendingReview.tsx +6 -6
- package/src/components/agents/QuestionRequestList.tsx +278 -0
- package/src/components/agents/agentPanelStyles.ts +9 -33
- package/src/components/agents/agentTypes.ts +25 -43
- package/src/components/agents/index.ts +4 -0
- package/src/components/assets/asset-library/sidebar/AssetLibraryList.tsx +10 -4
- package/src/components/chrome/WorkspaceTabStrip.tsx +4 -3
- package/src/components/common/chat/ChatBox.tsx +9 -9
- package/src/components/common/chat/ChatModelMenu.tsx +34 -4
- package/src/components/common/chat/ChatThread.tsx +23 -7
- package/src/components/common/connections/ProviderConnection.test.tsx +208 -0
- package/src/components/common/connections/ProviderConnection.tsx +277 -0
- package/src/components/common/connections/index.ts +5 -0
- package/src/components/common/containers/AppFrame.tsx +26 -19
- package/src/components/common/containers/AppHeader.tsx +20 -20
- package/src/components/common/documents/DocumentSwitcher.tsx +63 -58
- package/src/components/common/dropdown/MenuDropdownItem.tsx +14 -2
- package/src/components/common/dropdown/MenuPortal.tsx +3 -1
- package/src/components/common/dropdown/menuViewportPosition.test.ts +20 -10
- package/src/components/common/dropdown/menuViewportPosition.ts +35 -27
- package/src/components/common/overlays/Modal.tsx +13 -2
- package/src/components/editor/EditorLinkPromptDialog.tsx +1 -1
- package/src/ics/generateIcs.test.ts +153 -0
- package/src/ics/generateIcs.ts +197 -0
- package/src/index.ts +3 -0
- package/src/net/httpRetry.test.ts +117 -0
- package/src/net/httpRetry.ts +111 -0
- package/src/theme/appAccents.ts +6 -0
- package/src/theme/appIdentityCss.mjs +11 -0
- package/src/theme/appIdentityCss.ts +11 -0
- package/src/theme/themes/dark.ts +3 -1
- package/src/theme/themes/light.ts +3 -1
- package/src/bridge/googleProviderConfig.ts +0 -73
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { useEffect, useMemo, useState } from 'react'
|
|
2
|
-
import { styled } from 'styled-components'
|
|
3
|
-
import { Button } from '../common/buttons/Button.js'
|
|
4
|
-
import { FlexCol, FlexRow } from '../common/containers/flex.js'
|
|
5
|
-
import { Text } from '../common/typography/Text.js'
|
|
6
1
|
import type { AgentUiToolCall } from './agentTypes.js'
|
|
2
|
+
import {
|
|
3
|
+
QuestionRequestList,
|
|
4
|
+
type QuestionRequestView,
|
|
5
|
+
} from './QuestionRequestList.js'
|
|
7
6
|
|
|
8
7
|
type QuestionMode = 'single_choice' | 'multiple_choice'
|
|
9
8
|
|
|
@@ -31,85 +30,6 @@ export interface AgentQuestionPromptListProps {
|
|
|
31
30
|
) => void | Promise<void>
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
const StyledList = styled(FlexCol)`
|
|
35
|
-
gap: 6px;
|
|
36
|
-
`
|
|
37
|
-
|
|
38
|
-
const StyledPrompt = styled(FlexCol)`
|
|
39
|
-
gap: var(--platform-spacing-sm);
|
|
40
|
-
padding: 10px;
|
|
41
|
-
border: 1px solid var(--platform-colors-border);
|
|
42
|
-
border-radius: var(--platform-radius-sm);
|
|
43
|
-
background: var(--platform-colors-bg);
|
|
44
|
-
`
|
|
45
|
-
|
|
46
|
-
const StyledQuestion = styled(Text)`
|
|
47
|
-
line-height: 1.35;
|
|
48
|
-
`
|
|
49
|
-
|
|
50
|
-
const StyledOptionGrid = styled.div`
|
|
51
|
-
display: grid;
|
|
52
|
-
gap: 6px;
|
|
53
|
-
`
|
|
54
|
-
|
|
55
|
-
const StyledOptionButton = styled.button<{ $selected: boolean }>`
|
|
56
|
-
display: grid;
|
|
57
|
-
gap: 2px;
|
|
58
|
-
width: 100%;
|
|
59
|
-
padding: 8px 10px;
|
|
60
|
-
border: 1px solid
|
|
61
|
-
${({ $selected }) =>
|
|
62
|
-
$selected
|
|
63
|
-
? 'var(--platform-colors-accent)'
|
|
64
|
-
: 'var(--platform-colors-border)'};
|
|
65
|
-
border-radius: var(--platform-radius-sm);
|
|
66
|
-
background: ${({ $selected }) =>
|
|
67
|
-
$selected
|
|
68
|
-
? 'color-mix(in srgb, var(--platform-colors-accent) 10%, var(--platform-colors-bg))'
|
|
69
|
-
: 'var(--platform-colors-surface)'};
|
|
70
|
-
color: var(--platform-colors-text);
|
|
71
|
-
text-align: left;
|
|
72
|
-
cursor: pointer;
|
|
73
|
-
transition: background var(--platform-transition-fast),
|
|
74
|
-
border-color var(--platform-transition-fast);
|
|
75
|
-
|
|
76
|
-
&:hover:not(:disabled) {
|
|
77
|
-
background: var(--platform-colors-surface-hover);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
&:focus {
|
|
81
|
-
outline: none;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
&:focus-visible {
|
|
85
|
-
outline: 1px solid var(--platform-colors-border-strong);
|
|
86
|
-
outline-offset: 2px;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
&:disabled {
|
|
90
|
-
opacity: 0.55;
|
|
91
|
-
cursor: not-allowed;
|
|
92
|
-
}
|
|
93
|
-
`
|
|
94
|
-
|
|
95
|
-
const StyledDeclineInput = styled.input`
|
|
96
|
-
min-width: 0;
|
|
97
|
-
flex: 1 1 auto;
|
|
98
|
-
height: 28px;
|
|
99
|
-
padding: 0 9px;
|
|
100
|
-
border: 1px solid var(--platform-colors-border);
|
|
101
|
-
border-radius: var(--platform-radius-sm);
|
|
102
|
-
background: var(--platform-colors-surface);
|
|
103
|
-
color: var(--platform-colors-text);
|
|
104
|
-
font: inherit;
|
|
105
|
-
font-size: var(--platform-typography-font-size-sm);
|
|
106
|
-
|
|
107
|
-
&:focus {
|
|
108
|
-
outline: none;
|
|
109
|
-
border-color: var(--platform-colors-accent);
|
|
110
|
-
}
|
|
111
|
-
`
|
|
112
|
-
|
|
113
33
|
function stringValue(value: unknown): string | null {
|
|
114
34
|
return typeof value === 'string' && value.trim() ? value.trim() : null
|
|
115
35
|
}
|
|
@@ -140,126 +60,28 @@ function parseQuestionPrompt(
|
|
|
140
60
|
return { question, mode, options }
|
|
141
61
|
}
|
|
142
62
|
|
|
143
|
-
function
|
|
144
|
-
toolCall,
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}: {
|
|
148
|
-
toolCall: AgentUiToolCall
|
|
149
|
-
onAnswer?: (
|
|
150
|
-
toolCallId: string,
|
|
151
|
-
selectedOptionIds: readonly string[],
|
|
152
|
-
) => void | Promise<void>
|
|
153
|
-
onDecline?: (
|
|
154
|
-
toolCallId: string,
|
|
155
|
-
declineReason: string,
|
|
156
|
-
) => void | Promise<void>
|
|
157
|
-
}): React.ReactElement | null {
|
|
158
|
-
const prompt = useMemo(() => parseQuestionPrompt(toolCall), [toolCall])
|
|
159
|
-
const [selectedIds, setSelectedIds] = useState<string[]>([])
|
|
160
|
-
const [declineReason, setDeclineReason] = useState('')
|
|
161
|
-
const [submitting, setSubmitting] = useState(false)
|
|
162
|
-
|
|
163
|
-
useEffect(() => {
|
|
164
|
-
setSelectedIds([])
|
|
165
|
-
setDeclineReason('')
|
|
166
|
-
setSubmitting(false)
|
|
167
|
-
}, [toolCall.id])
|
|
168
|
-
|
|
63
|
+
function requestFromToolCall(
|
|
64
|
+
toolCall: AgentUiToolCall,
|
|
65
|
+
): QuestionRequestView | null {
|
|
66
|
+
const prompt = parseQuestionPrompt(toolCall)
|
|
169
67
|
if (!prompt) return null
|
|
170
68
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
:
|
|
177
|
-
|
|
69
|
+
return {
|
|
70
|
+
id: toolCall.id,
|
|
71
|
+
questions: [
|
|
72
|
+
{
|
|
73
|
+
id: toolCall.id,
|
|
74
|
+
multiple: prompt.mode === 'multiple_choice',
|
|
75
|
+
options: prompt.options.map(option => ({
|
|
76
|
+
value: option.id,
|
|
77
|
+
label: option.label,
|
|
78
|
+
...(option.description ? { description: option.description } : {}),
|
|
79
|
+
})),
|
|
80
|
+
question: prompt.question,
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
rejectRequiresReason: true,
|
|
178
84
|
}
|
|
179
|
-
|
|
180
|
-
const submitAnswer = async (): Promise<void> => {
|
|
181
|
-
if (!onAnswer || selectedIds.length === 0 || submitting) return
|
|
182
|
-
setSubmitting(true)
|
|
183
|
-
try {
|
|
184
|
-
await onAnswer(toolCall.id, selectedIds)
|
|
185
|
-
} finally {
|
|
186
|
-
setSubmitting(false)
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const submitDecline = async (): Promise<void> => {
|
|
191
|
-
const trimmedReason = declineReason.trim()
|
|
192
|
-
if (!onDecline || !trimmedReason || submitting) return
|
|
193
|
-
setSubmitting(true)
|
|
194
|
-
try {
|
|
195
|
-
await onDecline(toolCall.id, trimmedReason)
|
|
196
|
-
} finally {
|
|
197
|
-
setSubmitting(false)
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const canAnswer = selectedIds.length > 0 && Boolean(onAnswer) && !submitting
|
|
202
|
-
const canDecline =
|
|
203
|
-
declineReason.trim().length > 0 && Boolean(onDecline) && !submitting
|
|
204
|
-
|
|
205
|
-
return (
|
|
206
|
-
<StyledPrompt>
|
|
207
|
-
<StyledQuestion size="sm" weight="medium">
|
|
208
|
-
{prompt.question}
|
|
209
|
-
</StyledQuestion>
|
|
210
|
-
<StyledOptionGrid>
|
|
211
|
-
{prompt.options.map(option => {
|
|
212
|
-
const selected = selectedIds.includes(option.id)
|
|
213
|
-
return (
|
|
214
|
-
<StyledOptionButton
|
|
215
|
-
key={option.id}
|
|
216
|
-
type="button"
|
|
217
|
-
disabled={submitting}
|
|
218
|
-
$selected={selected}
|
|
219
|
-
aria-pressed={selected}
|
|
220
|
-
onClick={() => toggleOption(option.id)}
|
|
221
|
-
>
|
|
222
|
-
<Text size="sm" weight="medium">
|
|
223
|
-
{option.label}
|
|
224
|
-
</Text>
|
|
225
|
-
{option.description ? (
|
|
226
|
-
<Text size="sm" tone="secondary">
|
|
227
|
-
{option.description}
|
|
228
|
-
</Text>
|
|
229
|
-
) : null}
|
|
230
|
-
</StyledOptionButton>
|
|
231
|
-
)
|
|
232
|
-
})}
|
|
233
|
-
</StyledOptionGrid>
|
|
234
|
-
<FlexRow $align="center" $justify="between" $gap="sm">
|
|
235
|
-
<StyledDeclineInput
|
|
236
|
-
value={declineReason}
|
|
237
|
-
disabled={submitting}
|
|
238
|
-
placeholder="Decline reason"
|
|
239
|
-
aria-label="Decline reason"
|
|
240
|
-
onChange={event => setDeclineReason(event.target.value)}
|
|
241
|
-
/>
|
|
242
|
-
<Button
|
|
243
|
-
size="sm"
|
|
244
|
-
variant="subtle"
|
|
245
|
-
disabled={!canDecline}
|
|
246
|
-
loading={submitting}
|
|
247
|
-
onClick={() => void submitDecline()}
|
|
248
|
-
>
|
|
249
|
-
Decline
|
|
250
|
-
</Button>
|
|
251
|
-
<Button
|
|
252
|
-
size="sm"
|
|
253
|
-
variant="primary"
|
|
254
|
-
disabled={!canAnswer}
|
|
255
|
-
loading={submitting}
|
|
256
|
-
onClick={() => void submitAnswer()}
|
|
257
|
-
>
|
|
258
|
-
Answer
|
|
259
|
-
</Button>
|
|
260
|
-
</FlexRow>
|
|
261
|
-
</StyledPrompt>
|
|
262
|
-
)
|
|
263
85
|
}
|
|
264
86
|
|
|
265
87
|
export function AgentQuestionPromptList({
|
|
@@ -267,18 +89,18 @@ export function AgentQuestionPromptList({
|
|
|
267
89
|
onAnswer,
|
|
268
90
|
onDecline,
|
|
269
91
|
}: AgentQuestionPromptListProps): React.ReactElement | null {
|
|
270
|
-
|
|
92
|
+
const requests = toolCalls.flatMap((toolCall): QuestionRequestView[] => {
|
|
93
|
+
const request = requestFromToolCall(toolCall)
|
|
94
|
+
return request ? [request] : []
|
|
95
|
+
})
|
|
271
96
|
|
|
272
97
|
return (
|
|
273
|
-
<
|
|
274
|
-
{
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
/>
|
|
281
|
-
))}
|
|
282
|
-
</StyledList>
|
|
98
|
+
<QuestionRequestList
|
|
99
|
+
requests={requests}
|
|
100
|
+
onAnswer={(requestId, answers) => onAnswer?.(requestId, answers[0] ?? [])}
|
|
101
|
+
onReject={(requestId, reason) =>
|
|
102
|
+
reason ? onDecline?.(requestId, reason) : undefined
|
|
103
|
+
}
|
|
104
|
+
/>
|
|
283
105
|
)
|
|
284
106
|
}
|
|
@@ -3,17 +3,15 @@ import { ChatModelMenu } from '../common/chat/ChatModelMenu.js'
|
|
|
3
3
|
import type { AgentUiToolCall } from './agentTypes.js'
|
|
4
4
|
import { StyledToolStageActions } from './agentPanelStyles.js'
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const APPROVE_ALWAYS_SCOPE = 'approve-always-scope'
|
|
7
7
|
|
|
8
8
|
export interface AgentToolApprovalActionsProps {
|
|
9
9
|
toolCall: AgentUiToolCall
|
|
10
10
|
busy?: boolean
|
|
11
11
|
onApproveThisTime: (toolCallId: string) => void
|
|
12
12
|
onRejectThisTime: (toolCallId: string) => void
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
toolCallId: string,
|
|
16
|
-
) => void
|
|
13
|
+
approveAlwaysScopeLabel?: string
|
|
14
|
+
onApproveAlwaysForScope?: (qualifiedName: string, toolCallId: string) => void
|
|
17
15
|
}
|
|
18
16
|
|
|
19
17
|
export function AgentToolApprovalActions({
|
|
@@ -21,25 +19,29 @@ export function AgentToolApprovalActions({
|
|
|
21
19
|
busy = false,
|
|
22
20
|
onApproveThisTime,
|
|
23
21
|
onRejectThisTime,
|
|
24
|
-
|
|
22
|
+
approveAlwaysScopeLabel,
|
|
23
|
+
onApproveAlwaysForScope,
|
|
25
24
|
}: AgentToolApprovalActionsProps): React.ReactElement {
|
|
25
|
+
const showScopeOption = Boolean(
|
|
26
|
+
onApproveAlwaysForScope && approveAlwaysScopeLabel,
|
|
27
|
+
)
|
|
26
28
|
return (
|
|
27
29
|
<StyledToolStageActions>
|
|
28
|
-
{
|
|
30
|
+
{showScopeOption ? (
|
|
29
31
|
<ChatModelMenu
|
|
30
32
|
primaryActionLabel="Approve"
|
|
31
33
|
onPrimaryAction={() => onApproveThisTime(toolCall.id)}
|
|
32
|
-
value={
|
|
34
|
+
value={APPROVE_ALWAYS_SCOPE}
|
|
33
35
|
options={[
|
|
34
36
|
{
|
|
35
|
-
value:
|
|
36
|
-
label:
|
|
37
|
+
value: APPROVE_ALWAYS_SCOPE,
|
|
38
|
+
label: approveAlwaysScopeLabel!,
|
|
37
39
|
},
|
|
38
40
|
]}
|
|
39
41
|
disabled={busy}
|
|
40
42
|
menuPlacement="above"
|
|
41
43
|
aria-label="Approve"
|
|
42
|
-
onSelect={() =>
|
|
44
|
+
onSelect={() => onApproveAlwaysForScope!(toolCall.name, toolCall.id)}
|
|
43
45
|
/>
|
|
44
46
|
) : (
|
|
45
47
|
<Button
|
|
@@ -8,10 +8,8 @@ export interface AgentToolPendingListProps {
|
|
|
8
8
|
onReject: () => void
|
|
9
9
|
onApproveOne?: (toolCallId: string) => void
|
|
10
10
|
onRejectOne?: (toolCallId: string) => void
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
toolCallId: string,
|
|
14
|
-
) => void
|
|
11
|
+
approveAlwaysScopeLabel?: string
|
|
12
|
+
onApproveAlwaysForScope?: (qualifiedName: string, toolCallId: string) => void
|
|
15
13
|
}
|
|
16
14
|
|
|
17
15
|
export function AgentToolPendingList({
|
|
@@ -21,7 +19,8 @@ export function AgentToolPendingList({
|
|
|
21
19
|
onReject,
|
|
22
20
|
onApproveOne,
|
|
23
21
|
onRejectOne,
|
|
24
|
-
|
|
22
|
+
approveAlwaysScopeLabel,
|
|
23
|
+
onApproveAlwaysForScope,
|
|
25
24
|
}: AgentToolPendingListProps): React.ReactElement | null {
|
|
26
25
|
if (toolCalls.length === 0) return null
|
|
27
26
|
|
|
@@ -33,7 +32,8 @@ export function AgentToolPendingList({
|
|
|
33
32
|
onRejectAll={onReject}
|
|
34
33
|
onApproveOne={onApproveOne ?? onApprove}
|
|
35
34
|
onRejectOne={onRejectOne ?? onReject}
|
|
36
|
-
|
|
35
|
+
approveAlwaysScopeLabel={approveAlwaysScopeLabel}
|
|
36
|
+
onApproveAlwaysForScope={onApproveAlwaysForScope}
|
|
37
37
|
/>
|
|
38
38
|
)
|
|
39
39
|
}
|
|
@@ -16,10 +16,8 @@ export interface AgentToolPendingReviewProps {
|
|
|
16
16
|
onRejectAll: () => void
|
|
17
17
|
onApproveOne: (toolCallId: string) => void
|
|
18
18
|
onRejectOne: (toolCallId: string) => void
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
toolCallId: string,
|
|
22
|
-
) => void
|
|
19
|
+
approveAlwaysScopeLabel?: string
|
|
20
|
+
onApproveAlwaysForScope?: (qualifiedName: string, toolCallId: string) => void
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
export function AgentToolPendingReview({
|
|
@@ -29,7 +27,8 @@ export function AgentToolPendingReview({
|
|
|
29
27
|
onRejectAll,
|
|
30
28
|
onApproveOne,
|
|
31
29
|
onRejectOne,
|
|
32
|
-
|
|
30
|
+
approveAlwaysScopeLabel,
|
|
31
|
+
onApproveAlwaysForScope,
|
|
33
32
|
}: AgentToolPendingReviewProps): React.ReactElement | null {
|
|
34
33
|
const [index, setIndex] = useState(0)
|
|
35
34
|
const count = toolCalls.length
|
|
@@ -88,7 +87,8 @@ export function AgentToolPendingReview({
|
|
|
88
87
|
busy={busy}
|
|
89
88
|
onApproveThisTime={onApproveOne}
|
|
90
89
|
onRejectThisTime={onRejectOne}
|
|
91
|
-
|
|
90
|
+
approveAlwaysScopeLabel={approveAlwaysScopeLabel}
|
|
91
|
+
onApproveAlwaysForScope={onApproveAlwaysForScope}
|
|
92
92
|
/>
|
|
93
93
|
}
|
|
94
94
|
/>
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import { styled } from 'styled-components'
|
|
3
|
+
import { Button } from '../common/buttons/Button.js'
|
|
4
|
+
import { FlexCol, FlexRow } from '../common/containers/flex.js'
|
|
5
|
+
import { Text } from '../common/typography/Text.js'
|
|
6
|
+
|
|
7
|
+
export interface QuestionOptionView {
|
|
8
|
+
value: string
|
|
9
|
+
label: string
|
|
10
|
+
description?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface QuestionView {
|
|
14
|
+
id: string
|
|
15
|
+
question: string
|
|
16
|
+
multiple?: boolean
|
|
17
|
+
custom?: boolean
|
|
18
|
+
options: QuestionOptionView[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface QuestionRequestView {
|
|
22
|
+
id: string
|
|
23
|
+
questions: QuestionView[]
|
|
24
|
+
rejectRequiresReason?: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface QuestionRequestListProps {
|
|
28
|
+
requests: QuestionRequestView[]
|
|
29
|
+
onAnswer?: (requestId: string, answers: string[][]) => void | Promise<void>
|
|
30
|
+
onReject?: (requestId: string, reason?: string) => void | Promise<void>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const StyledList = styled(FlexCol)`
|
|
34
|
+
gap: 6px;
|
|
35
|
+
`
|
|
36
|
+
|
|
37
|
+
const StyledPrompt = styled(FlexCol)`
|
|
38
|
+
gap: var(--platform-spacing-sm);
|
|
39
|
+
padding: 10px;
|
|
40
|
+
border: 1px solid var(--platform-colors-border);
|
|
41
|
+
border-radius: var(--platform-radius-sm);
|
|
42
|
+
background: var(--platform-colors-bg);
|
|
43
|
+
`
|
|
44
|
+
|
|
45
|
+
const StyledQuestion = styled(Text)`
|
|
46
|
+
line-height: 1.35;
|
|
47
|
+
`
|
|
48
|
+
|
|
49
|
+
const StyledOptionGrid = styled.div`
|
|
50
|
+
display: grid;
|
|
51
|
+
gap: 6px;
|
|
52
|
+
`
|
|
53
|
+
|
|
54
|
+
const StyledOptionButton = styled.button<{ $selected: boolean }>`
|
|
55
|
+
display: grid;
|
|
56
|
+
gap: 2px;
|
|
57
|
+
width: 100%;
|
|
58
|
+
padding: 8px 10px;
|
|
59
|
+
border: 1px solid
|
|
60
|
+
${({ $selected }) =>
|
|
61
|
+
$selected
|
|
62
|
+
? 'var(--platform-colors-accent)'
|
|
63
|
+
: 'var(--platform-colors-border)'};
|
|
64
|
+
border-radius: var(--platform-radius-sm);
|
|
65
|
+
background: ${({ $selected }) =>
|
|
66
|
+
$selected
|
|
67
|
+
? 'color-mix(in srgb, var(--platform-colors-accent) 10%, var(--platform-colors-bg))'
|
|
68
|
+
: 'var(--platform-colors-surface)'};
|
|
69
|
+
color: var(--platform-colors-text);
|
|
70
|
+
text-align: left;
|
|
71
|
+
cursor: pointer;
|
|
72
|
+
transition: background var(--platform-transition-fast),
|
|
73
|
+
border-color var(--platform-transition-fast);
|
|
74
|
+
|
|
75
|
+
&:hover:not(:disabled) {
|
|
76
|
+
background: var(--platform-colors-surface-hover);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
&:focus {
|
|
80
|
+
outline: none;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
&:focus-visible {
|
|
84
|
+
outline: 1px solid var(--platform-colors-border-strong);
|
|
85
|
+
outline-offset: 2px;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
&:disabled {
|
|
89
|
+
opacity: 0.55;
|
|
90
|
+
cursor: not-allowed;
|
|
91
|
+
}
|
|
92
|
+
`
|
|
93
|
+
|
|
94
|
+
const StyledInput = styled.input`
|
|
95
|
+
min-width: 0;
|
|
96
|
+
flex: 1 1 auto;
|
|
97
|
+
height: 28px;
|
|
98
|
+
padding: 0 9px;
|
|
99
|
+
border: 1px solid var(--platform-colors-border);
|
|
100
|
+
border-radius: var(--platform-radius-sm);
|
|
101
|
+
background: var(--platform-colors-surface);
|
|
102
|
+
color: var(--platform-colors-text);
|
|
103
|
+
font: inherit;
|
|
104
|
+
font-size: var(--platform-typography-font-size-sm);
|
|
105
|
+
|
|
106
|
+
&:focus {
|
|
107
|
+
outline: none;
|
|
108
|
+
border-color: var(--platform-colors-accent);
|
|
109
|
+
}
|
|
110
|
+
`
|
|
111
|
+
|
|
112
|
+
function QuestionRequest({
|
|
113
|
+
request,
|
|
114
|
+
onAnswer,
|
|
115
|
+
onReject,
|
|
116
|
+
}: {
|
|
117
|
+
request: QuestionRequestView
|
|
118
|
+
onAnswer?: QuestionRequestListProps['onAnswer']
|
|
119
|
+
onReject?: QuestionRequestListProps['onReject']
|
|
120
|
+
}): React.ReactElement {
|
|
121
|
+
const [answers, setAnswers] = useState<string[][]>([])
|
|
122
|
+
const [rejectReason, setRejectReason] = useState('')
|
|
123
|
+
const [submitting, setSubmitting] = useState(false)
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
setAnswers(request.questions.map(() => []))
|
|
127
|
+
setRejectReason('')
|
|
128
|
+
setSubmitting(false)
|
|
129
|
+
}, [request.id, request.questions.length])
|
|
130
|
+
|
|
131
|
+
const setQuestionAnswer = (index: number, value: string): void => {
|
|
132
|
+
const question = request.questions[index]
|
|
133
|
+
setAnswers(current => {
|
|
134
|
+
const next = [...current]
|
|
135
|
+
const selected = next[index] ?? []
|
|
136
|
+
next[index] = question.multiple
|
|
137
|
+
? selected.includes(value)
|
|
138
|
+
? selected.filter(item => item !== value)
|
|
139
|
+
: [...selected, value]
|
|
140
|
+
: [value]
|
|
141
|
+
return next
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const submitAnswer = async (): Promise<void> => {
|
|
146
|
+
if (!onAnswer || submitting || !answers.every(answer => answer.length)) {
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
setSubmitting(true)
|
|
150
|
+
try {
|
|
151
|
+
await onAnswer(request.id, answers)
|
|
152
|
+
} finally {
|
|
153
|
+
setSubmitting(false)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const submitReject = async (): Promise<void> => {
|
|
158
|
+
if (!onReject || submitting) return
|
|
159
|
+
const reason = rejectReason.trim()
|
|
160
|
+
if (request.rejectRequiresReason && !reason) return
|
|
161
|
+
setSubmitting(true)
|
|
162
|
+
try {
|
|
163
|
+
await onReject(request.id, reason || undefined)
|
|
164
|
+
} finally {
|
|
165
|
+
setSubmitting(false)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const canAnswer =
|
|
170
|
+
Boolean(onAnswer) && answers.length > 0 && answers.every(answer => answer.length)
|
|
171
|
+
const canReject =
|
|
172
|
+
Boolean(onReject) &&
|
|
173
|
+
(!request.rejectRequiresReason || rejectReason.trim().length > 0)
|
|
174
|
+
|
|
175
|
+
return (
|
|
176
|
+
<StyledPrompt>
|
|
177
|
+
{request.questions.map((question, index) => (
|
|
178
|
+
<FlexCol key={question.id} $gap="sm">
|
|
179
|
+
<StyledQuestion size="sm" weight="medium">
|
|
180
|
+
{question.question}
|
|
181
|
+
</StyledQuestion>
|
|
182
|
+
<StyledOptionGrid>
|
|
183
|
+
{question.options.map(option => {
|
|
184
|
+
const selected = answers[index]?.includes(option.value) ?? false
|
|
185
|
+
return (
|
|
186
|
+
<StyledOptionButton
|
|
187
|
+
key={option.value}
|
|
188
|
+
type="button"
|
|
189
|
+
disabled={submitting}
|
|
190
|
+
$selected={selected}
|
|
191
|
+
aria-pressed={selected}
|
|
192
|
+
onClick={() => setQuestionAnswer(index, option.value)}
|
|
193
|
+
>
|
|
194
|
+
<Text size="sm" weight="medium">
|
|
195
|
+
{option.label}
|
|
196
|
+
</Text>
|
|
197
|
+
{option.description ? (
|
|
198
|
+
<Text size="sm" tone="secondary">
|
|
199
|
+
{option.description}
|
|
200
|
+
</Text>
|
|
201
|
+
) : null}
|
|
202
|
+
</StyledOptionButton>
|
|
203
|
+
)
|
|
204
|
+
})}
|
|
205
|
+
</StyledOptionGrid>
|
|
206
|
+
{question.custom ? (
|
|
207
|
+
<StyledInput
|
|
208
|
+
value={answers[index]?.[0] ?? ''}
|
|
209
|
+
disabled={submitting}
|
|
210
|
+
placeholder="Custom answer"
|
|
211
|
+
aria-label="Custom answer"
|
|
212
|
+
onChange={event => {
|
|
213
|
+
const value = event.target.value
|
|
214
|
+
setAnswers(current => {
|
|
215
|
+
const next = [...current]
|
|
216
|
+
next[index] = value.trim() ? [value] : []
|
|
217
|
+
return next
|
|
218
|
+
})
|
|
219
|
+
}}
|
|
220
|
+
/>
|
|
221
|
+
) : null}
|
|
222
|
+
</FlexCol>
|
|
223
|
+
))}
|
|
224
|
+
<FlexRow $align="center" $justify="between" $gap="sm">
|
|
225
|
+
{request.rejectRequiresReason ? (
|
|
226
|
+
<StyledInput
|
|
227
|
+
value={rejectReason}
|
|
228
|
+
disabled={submitting}
|
|
229
|
+
placeholder="Decline reason"
|
|
230
|
+
aria-label="Decline reason"
|
|
231
|
+
onChange={event => setRejectReason(event.target.value)}
|
|
232
|
+
/>
|
|
233
|
+
) : null}
|
|
234
|
+
{onReject ? (
|
|
235
|
+
<Button
|
|
236
|
+
size="sm"
|
|
237
|
+
variant="subtle"
|
|
238
|
+
disabled={!canReject || submitting}
|
|
239
|
+
loading={submitting}
|
|
240
|
+
onClick={() => void submitReject()}
|
|
241
|
+
>
|
|
242
|
+
Decline
|
|
243
|
+
</Button>
|
|
244
|
+
) : null}
|
|
245
|
+
<Button
|
|
246
|
+
size="sm"
|
|
247
|
+
variant="primary"
|
|
248
|
+
disabled={!canAnswer || submitting}
|
|
249
|
+
loading={submitting}
|
|
250
|
+
onClick={() => void submitAnswer()}
|
|
251
|
+
>
|
|
252
|
+
Answer
|
|
253
|
+
</Button>
|
|
254
|
+
</FlexRow>
|
|
255
|
+
</StyledPrompt>
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function QuestionRequestList({
|
|
260
|
+
requests,
|
|
261
|
+
onAnswer,
|
|
262
|
+
onReject,
|
|
263
|
+
}: QuestionRequestListProps): React.ReactElement | null {
|
|
264
|
+
if (requests.length === 0) return null
|
|
265
|
+
|
|
266
|
+
return (
|
|
267
|
+
<StyledList aria-label="Questions from agent">
|
|
268
|
+
{requests.map(request => (
|
|
269
|
+
<QuestionRequest
|
|
270
|
+
key={request.id}
|
|
271
|
+
request={request}
|
|
272
|
+
onAnswer={onAnswer}
|
|
273
|
+
onReject={onReject}
|
|
274
|
+
/>
|
|
275
|
+
))}
|
|
276
|
+
</StyledList>
|
|
277
|
+
)
|
|
278
|
+
}
|