@gemies/sdk 10.2.0 → 10.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/package.json +1 -1
- package/src/components/ApprovalBar.tsx +131 -0
- package/src/components/GemieChatContent.tsx +54 -20
- package/src/components/GemieComposer.tsx +7 -0
- package/src/components/thread/ApprovalCard.tsx +73 -71
- package/src/components/thread/GemieThread.tsx +4 -3
- package/src/hooks/useApprovalShortcuts.ts +93 -0
- package/src/lib/copy-en.ts +16 -4
- package/src/lib/copy-es.ts +16 -4
- package/src/runtime/approvals.ts +150 -13
- package/src/runtime/config.tsx +16 -4
- package/src/runtime/index.ts +9 -1
- package/src/runtime/resumable-stream.ts +306 -0
- package/src/runtime/routes.ts +14 -0
- package/src/runtime/sse-frame-reader.ts +20 -1
- package/src/runtime/stream-events.ts +68 -0
- package/src/runtime/use-approval-actions.ts +162 -0
- package/src/runtime/use-conversation-stream.ts +79 -36
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
1
|
# @gemies/sdk
|
|
2
2
|
|
|
3
|
+
## 10.4.0
|
|
4
|
+
|
|
5
|
+
### Richer answers to a coding CLI's permission questions
|
|
6
|
+
|
|
7
|
+
A coding CLI (ADR 0043 §5) asks to run something on the person's computer and the turn waits. The
|
|
8
|
+
question now offers more than allow and deny, and shows in two places that answer together.
|
|
9
|
+
|
|
10
|
+
- **Decisions.** `ApprovalDecision` is `allow`, `allow_session` (this tool on this exact path or
|
|
11
|
+
URL, for the rest of the conversation's session; offered only where the server can scope it, so
|
|
12
|
+
not for shell commands today), `deny` and `cancel` (deny, and stop the turn).
|
|
13
|
+
A denial can carry `feedback`, words the agent is told. When the agent supplies its own options
|
|
14
|
+
(`ApprovalOption`), their ids and labels are shown and sent back unchanged (`optionId`); the
|
|
15
|
+
built-in ones are named in English and Spanish (`chat.approval.option.*`). Every question can be
|
|
16
|
+
declined.
|
|
17
|
+
- **In the thread and above the composer.** The timeline's card lists the answers; the composer docks
|
|
18
|
+
a compact `ApprovalBar` (who is waiting, what they would run, the answers), and on web `Y` approves,
|
|
19
|
+
`A` allows for the session and `N` declines, once per press, only on the chat on screen and except
|
|
20
|
+
while typing. Both answer through
|
|
21
|
+
one `useApprovalActions`, so an answer is sent once however many places show the question.
|
|
22
|
+
- **A reply declines.** While a question waits, text typed in the composer and sent declines it and
|
|
23
|
+
goes to the agent as the feedback. The field's placeholder and the bar say so; Stop gives way to
|
|
24
|
+
send while there are words to send, and comes back when there are none.
|
|
25
|
+
- **Nothing stays open.** When a turn ends — answered, stopped, failed, the connection lost — every
|
|
26
|
+
question still waiting is settled (`ended`, or `cancel` when the person stopped it), so no card
|
|
27
|
+
stays pressable for a request nobody waits on. A question answered by the person's earlier
|
|
28
|
+
"allow for this session" reads as such (`auto`).
|
|
29
|
+
- **Host.** `onApprovalDecision(requestId, agentId, decision, details?)` takes the wider decision
|
|
30
|
+
and `{ optionId, feedback }`; it rejects with `ApprovalExpiredError` for a question no longer open
|
|
31
|
+
(a 404, or a 409 for one answered differently elsewhere). New optional `listApprovalGrants` and
|
|
32
|
+
`revokeApprovalGrant` let the thread list what was allowed for the session and take each back;
|
|
33
|
+
without them nothing is listed. A host written against 10.3 keeps compiling and carries the wider
|
|
34
|
+
decisions to Gemies as they are; it forwards no `details` until it takes them.
|
|
35
|
+
- `chat.approval.allow` and `chat.approval.deny` are gone (use `chat.approval.option.allow` and
|
|
36
|
+
`chat.approval.option.deny`); `stillPending` is gone, since a new turn starts with none.
|
|
37
|
+
|
|
38
|
+
## 10.3.0
|
|
39
|
+
|
|
40
|
+
### Chat streams resume after a dropped connection
|
|
41
|
+
|
|
42
|
+
When the connection that carries a turn drops (a network switch, a sleeping tab, a proxy closing an
|
|
43
|
+
idle socket), `useConversationStream` no longer fails the turn. The server numbers every frame of a
|
|
44
|
+
turn (the SSE `id:`) and keeps the newest ones; the client remembers the last number it applied and,
|
|
45
|
+
on a stream that ends without `[DONE]` or fails mid-read, reconnects to
|
|
46
|
+
`GET /v1/chat/turns/:turnId/stream` with exponential backoff and jitter and carries on. A frame it
|
|
47
|
+
already has is dropped by its number, so a delta is applied once. When the server cannot replay what
|
|
48
|
+
was missed (the cursor is older than its buffer, or the turn finished meanwhile) it sends one
|
|
49
|
+
`alia.snapshot` of the turn instead, which the client applies idempotently over the reply it is
|
|
50
|
+
drawing. The turn is failed only once the resume is refused or its attempts are spent, and then as a
|
|
51
|
+
cut stream always was: `hasUsableStreamOutput` decides whether the person keeps an answer.
|
|
52
|
+
|
|
53
|
+
A server that does not number its frames is read exactly as before. Nothing is added to the public
|
|
54
|
+
API: the reconnect lives inside the hook.
|
|
55
|
+
|
|
3
56
|
## 10.2.0
|
|
4
57
|
|
|
5
58
|
### The files an agent changed, as a diff
|
package/package.json
CHANGED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Button } from '@oxy.so/bloom/button';
|
|
2
|
+
import { createTokenStyles } from '@oxy.so/bloom/theme';
|
|
3
|
+
import { Text } from '@oxy.so/bloom/typography';
|
|
4
|
+
import React, { useMemo } from 'react';
|
|
5
|
+
import { Platform, View } from 'react-native';
|
|
6
|
+
import {
|
|
7
|
+
APPROVAL_SHORTCUT_KEYS,
|
|
8
|
+
type ApprovalShortcutActions,
|
|
9
|
+
useApprovalShortcuts,
|
|
10
|
+
} from '../hooks/useApprovalShortcuts';
|
|
11
|
+
import { useLabels } from '../lib/labels';
|
|
12
|
+
import {
|
|
13
|
+
approvalOptionLabel,
|
|
14
|
+
optionOfKind,
|
|
15
|
+
orderedApprovalOptions,
|
|
16
|
+
type ApprovalDecision,
|
|
17
|
+
} from '../runtime/approvals';
|
|
18
|
+
import type { ApprovalActions } from '../runtime/use-approval-actions';
|
|
19
|
+
|
|
20
|
+
const SHORTCUT_OF: Partial<Record<ApprovalDecision, string>> = {
|
|
21
|
+
allow: APPROVAL_SHORTCUT_KEYS.approve,
|
|
22
|
+
allow_session: APPROVAL_SHORTCUT_KEYS.approveForSession,
|
|
23
|
+
deny: APPROVAL_SHORTCUT_KEYS.decline,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The CLI's question, compact, above the composer where the person is about to type (ADR 0043 §5):
|
|
28
|
+
* who asks and what it would run, the answers, and — said plainly — that writing a reply below
|
|
29
|
+
* declines it and tells the agent why. On web the keys answer too (`useApprovalShortcuts`).
|
|
30
|
+
*
|
|
31
|
+
* The thread shows the same question as a card in the timeline; both answer through the same
|
|
32
|
+
* actions, so one answered here is answered there.
|
|
33
|
+
*/
|
|
34
|
+
export const ApprovalBar = React.memo(function ApprovalBar({
|
|
35
|
+
approvals,
|
|
36
|
+
isFocused = true,
|
|
37
|
+
}: {
|
|
38
|
+
approvals: ApprovalActions;
|
|
39
|
+
/** False for a chat kept mounted under another screen: it must not hear the keyboard. */
|
|
40
|
+
isFocused?: boolean;
|
|
41
|
+
}) {
|
|
42
|
+
const { t } = useLabels();
|
|
43
|
+
const styles = useStyles();
|
|
44
|
+
const { pending: entry, decide, sending, failed, canAnswer } = approvals;
|
|
45
|
+
const busy = entry !== undefined && sending.has(entry.requestId);
|
|
46
|
+
|
|
47
|
+
const shortcuts = useMemo<ApprovalShortcutActions | undefined>(() => {
|
|
48
|
+
if (!entry || !canAnswer || busy || !isFocused) return undefined;
|
|
49
|
+
const answer = (kind: ApprovalDecision) => {
|
|
50
|
+
const option = optionOfKind(entry, kind);
|
|
51
|
+
return option ? () => void decide(entry, kind, { optionId: option.id }) : undefined;
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
approve: answer('allow'),
|
|
55
|
+
approveForSession: answer('allow_session'),
|
|
56
|
+
// The way out is a no: declining, or stopping when that is all the question offers.
|
|
57
|
+
decline: answer('deny') ?? answer('cancel'),
|
|
58
|
+
};
|
|
59
|
+
}, [entry, canAnswer, busy, decide, isFocused]);
|
|
60
|
+
useApprovalShortcuts(shortcuts);
|
|
61
|
+
|
|
62
|
+
if (!entry) return null;
|
|
63
|
+
const waiting = approvals.entries.filter((e) => e.outcome === 'pending').length;
|
|
64
|
+
const hasKeys = Platform.OS === 'web' && isFocused;
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<View
|
|
68
|
+
style={styles.bar}
|
|
69
|
+
accessibilityRole="alert"
|
|
70
|
+
accessibilityLiveRegion="polite"
|
|
71
|
+
aria-live="polite"
|
|
72
|
+
>
|
|
73
|
+
<View style={styles.text}>
|
|
74
|
+
<Text style={styles.title} numberOfLines={1}>
|
|
75
|
+
{t('chat.approval.bar.title', { agent: approvals.agentName, tool: entry.tool })}
|
|
76
|
+
{waiting > 1 ? ` · ${t('chat.approval.bar.more', { count: waiting - 1 })}` : ''}
|
|
77
|
+
</Text>
|
|
78
|
+
{entry.summary ? (
|
|
79
|
+
<Text style={styles.summary} numberOfLines={2}>
|
|
80
|
+
{entry.summary}
|
|
81
|
+
</Text>
|
|
82
|
+
) : null}
|
|
83
|
+
<Text style={styles.hint}>
|
|
84
|
+
{failed.has(entry.requestId)
|
|
85
|
+
? t('chat.approval.sendFailed')
|
|
86
|
+
: t('chat.approval.bar.replyHint', { agent: approvals.agentName })}
|
|
87
|
+
</Text>
|
|
88
|
+
</View>
|
|
89
|
+
{canAnswer && !busy ? (
|
|
90
|
+
<View style={styles.actions}>
|
|
91
|
+
{orderedApprovalOptions(entry.options).map((option) => (
|
|
92
|
+
<Button
|
|
93
|
+
key={option.id}
|
|
94
|
+
size="xs"
|
|
95
|
+
appearance={option.kind === 'allow' ? 'solid' : 'subtle'}
|
|
96
|
+
tone={option.kind === 'cancel' ? 'danger' : undefined}
|
|
97
|
+
onPress={() => void decide(entry, option.kind, { optionId: option.id })}
|
|
98
|
+
trailing={
|
|
99
|
+
hasKeys && SHORTCUT_OF[option.kind] && option.label === undefined ? (
|
|
100
|
+
<Text style={styles.key}>{SHORTCUT_OF[option.kind]}</Text>
|
|
101
|
+
) : undefined
|
|
102
|
+
}
|
|
103
|
+
>
|
|
104
|
+
{approvalOptionLabel(option, t)}
|
|
105
|
+
</Button>
|
|
106
|
+
))}
|
|
107
|
+
</View>
|
|
108
|
+
) : null}
|
|
109
|
+
</View>
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const useStyles = createTokenStyles((t) => ({
|
|
114
|
+
bar: {
|
|
115
|
+
flexDirection: 'row',
|
|
116
|
+
flexWrap: 'wrap',
|
|
117
|
+
alignItems: 'center',
|
|
118
|
+
gap: 8,
|
|
119
|
+
marginBottom: 8,
|
|
120
|
+
paddingHorizontal: 12,
|
|
121
|
+
paddingVertical: 8,
|
|
122
|
+
borderRadius: 16,
|
|
123
|
+
backgroundColor: t.popover,
|
|
124
|
+
},
|
|
125
|
+
text: { flexGrow: 1, flexShrink: 1, flexBasis: 200, gap: 2 },
|
|
126
|
+
title: { fontSize: 14, fontWeight: 600, color: t.foreground, lineHeight: 20 },
|
|
127
|
+
summary: { fontSize: 13, color: t.foreground, lineHeight: 18, fontFamily: 'monospace' },
|
|
128
|
+
hint: { fontSize: 12, color: t.mutedForeground, lineHeight: 16 },
|
|
129
|
+
actions: { flexDirection: 'row', flexWrap: 'wrap', gap: 6 },
|
|
130
|
+
key: { fontSize: 11, color: t.mutedForeground, lineHeight: 14 },
|
|
131
|
+
}));
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { useConversationActionApprovals } from '../runtime/action-approvals';
|
|
2
|
+
import { optionOfKind } from '../runtime/approvals';
|
|
3
|
+
import { useApprovalActions } from '../runtime/use-approval-actions';
|
|
4
|
+
import { ApprovalBar } from './ApprovalBar';
|
|
2
5
|
/**
|
|
3
6
|
* The conversation: header, thread and composer, with its voice call. The one screen of the
|
|
4
7
|
* bottom sheet, `GemieChatScreen` and the Alia app — they are frames around this.
|
|
@@ -422,7 +425,7 @@ function GemieConversationView({
|
|
|
422
425
|
suggestedNewConversation,
|
|
423
426
|
dismissSuggestedNewConversation,
|
|
424
427
|
agentRun,
|
|
425
|
-
approvals,
|
|
428
|
+
approvals: approvalEntries,
|
|
426
429
|
settleApproval,
|
|
427
430
|
failedTurn,
|
|
428
431
|
retryFailedTurn,
|
|
@@ -664,20 +667,43 @@ function GemieConversationView({
|
|
|
664
667
|
);
|
|
665
668
|
|
|
666
669
|
// A coding CLI's question to its own user (ADR 0043 §5): the host carries the answer to Gemies.
|
|
670
|
+
// The thread's card and the composer's bar answer through the same actions.
|
|
667
671
|
const decideApproval = runtime.host.onApprovalDecision;
|
|
668
|
-
const
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
672
|
+
const stopTurn = useCallback(() => void stop(), [stop]);
|
|
673
|
+
const approvals = useApprovalActions({
|
|
674
|
+
entries: approvalEntries,
|
|
675
|
+
agentName: lead.name,
|
|
676
|
+
onDecide:
|
|
677
|
+
decideApproval === undefined
|
|
678
|
+
? undefined
|
|
679
|
+
: (entry, decision, details) =>
|
|
680
|
+
decideApproval(entry.requestId, entry.agentId, decision, details),
|
|
681
|
+
onSettled: settleApproval,
|
|
682
|
+
onStopTurn: stopTurn,
|
|
683
|
+
grants: {
|
|
684
|
+
agentId,
|
|
685
|
+
conversationId,
|
|
686
|
+
list: runtime.host.listApprovalGrants,
|
|
687
|
+
revoke: runtime.host.revokeApprovalGrant,
|
|
688
|
+
},
|
|
689
|
+
});
|
|
690
|
+
// While a question waits, a reply typed in the composer is the answer: it declines the request
|
|
691
|
+
// and the words go to the agent. The composer says so, and sends instead of stopping.
|
|
692
|
+
const decliningOption =
|
|
693
|
+
approvals.canAnswer && approvals.pending !== undefined
|
|
694
|
+
? optionOfKind(approvals.pending, 'deny')
|
|
695
|
+
: undefined;
|
|
696
|
+
const replyDeclines = decliningOption !== undefined && composer.text.trim() !== '';
|
|
697
|
+
const submitReply = useCallback(async () => {
|
|
698
|
+
const entry = approvals.pending;
|
|
699
|
+
const words = composer.text.trim();
|
|
700
|
+
if (!entry || !decliningOption || words === '') return;
|
|
701
|
+
const sent = await approvals.decide(entry, 'deny', {
|
|
702
|
+
optionId: decliningOption.id,
|
|
703
|
+
feedback: words,
|
|
704
|
+
});
|
|
705
|
+
if (sent) composer.setText('');
|
|
706
|
+
}, [approvals, composer, decliningOption]);
|
|
681
707
|
|
|
682
708
|
// ── The chrome ──
|
|
683
709
|
const latestMessages = useRef(messages);
|
|
@@ -760,8 +786,14 @@ function GemieConversationView({
|
|
|
760
786
|
}}
|
|
761
787
|
value={composer.text}
|
|
762
788
|
onValueChange={composer.setText}
|
|
763
|
-
onSubmit={() => void composer.submit(composer.text)}
|
|
764
|
-
|
|
789
|
+
onSubmit={() => void (replyDeclines ? submitReply() : composer.submit(composer.text))}
|
|
790
|
+
// Stop gives way to send while a reply would decline the question the agent waits on.
|
|
791
|
+
busy={isLoading && !replyDeclines}
|
|
792
|
+
approval={
|
|
793
|
+
approvals.pending === undefined ? undefined : (
|
|
794
|
+
<ApprovalBar approvals={approvals} isFocused={isFocused} />
|
|
795
|
+
)
|
|
796
|
+
}
|
|
765
797
|
loaderPhase={composerPhase}
|
|
766
798
|
disabled={closed || loading}
|
|
767
799
|
onStop={stop}
|
|
@@ -778,9 +810,11 @@ function GemieConversationView({
|
|
|
778
810
|
? t('usageLimit.inputDisabledPlaceholder')
|
|
779
811
|
: editing !== undefined
|
|
780
812
|
? t('composer.editPlaceholder')
|
|
781
|
-
:
|
|
782
|
-
?
|
|
783
|
-
:
|
|
813
|
+
: approvals.pending !== undefined && decliningOption !== undefined
|
|
814
|
+
? t('chat.approval.replyPlaceholder', { agent: lead.name })
|
|
815
|
+
: group === undefined
|
|
816
|
+
? labels.placeholder(lead.name)
|
|
817
|
+
: t('chat.groupPlaceholder')
|
|
784
818
|
}
|
|
785
819
|
status={status}
|
|
786
820
|
// The mode selector is the power level: how much power this turn gets.
|
|
@@ -835,7 +869,7 @@ function GemieConversationView({
|
|
|
835
869
|
onRegenerate={composer.regenerate}
|
|
836
870
|
onStartEdit={edit.start}
|
|
837
871
|
newConversationOffer={newConversationOffer}
|
|
838
|
-
approvals={
|
|
872
|
+
approvals={approvals}
|
|
839
873
|
actionApprovals={showsPast ? undefined : actionApprovals}
|
|
840
874
|
agentActivity={agentRun === null ? null : agentActivity}
|
|
841
875
|
call={voice.isActive ? { agentState: voice.agentState } : undefined}
|
|
@@ -106,6 +106,11 @@ export interface GemieComposerProps<T extends GemieSuggestion = GemieSuggestion>
|
|
|
106
106
|
suggestions?: GemieComposerSuggestions<T>;
|
|
107
107
|
/** What Alia is waiting on, docked above the composer. */
|
|
108
108
|
questions?: GemieComposerQuestions;
|
|
109
|
+
/**
|
|
110
|
+
* A coding CLI's permission question (`ApprovalBar`), docked above the composer beside the
|
|
111
|
+
* questions: where the person is about to type the reply that declines it.
|
|
112
|
+
*/
|
|
113
|
+
approval?: ReactNode;
|
|
109
114
|
/** The tab on the card's top edge (`ComposerPanelStatusTab`). */
|
|
110
115
|
status?: ReactNode;
|
|
111
116
|
/** Drawn where send would be while there is nothing to send (voice mode). */
|
|
@@ -151,6 +156,7 @@ function GemieComposerBody<T extends GemieSuggestion>({
|
|
|
151
156
|
attachmentKinds,
|
|
152
157
|
suggestions,
|
|
153
158
|
questions,
|
|
159
|
+
approval,
|
|
154
160
|
status,
|
|
155
161
|
emptyAction,
|
|
156
162
|
accessory,
|
|
@@ -301,6 +307,7 @@ function GemieComposerBody<T extends GemieSuggestion>({
|
|
|
301
307
|
{labels.dictationRemaining(remainingSeconds)}
|
|
302
308
|
</Text>
|
|
303
309
|
) : null}
|
|
310
|
+
{approval}
|
|
304
311
|
{questions && questions.items.length > 0 ? (
|
|
305
312
|
// Beside the loader, not through the pill's `question` slot: the light traces the box it
|
|
306
313
|
// wraps, so a question inside it would be ringed too.
|
|
@@ -1,111 +1,79 @@
|
|
|
1
|
+
import { Button } from '@oxy.so/bloom/button';
|
|
1
2
|
import { Notification } from '@oxy.so/bloom/notification';
|
|
2
3
|
import { Muted } from '@oxy.so/bloom/typography';
|
|
3
|
-
import React
|
|
4
|
+
import React from 'react';
|
|
4
5
|
import { StyleSheet, View } from 'react-native';
|
|
5
6
|
import { useLabels } from '../../lib/labels';
|
|
6
7
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
approvalOptionLabel,
|
|
9
|
+
orderedApprovalOptions,
|
|
9
10
|
type ApprovalEntry,
|
|
10
|
-
type
|
|
11
|
+
type ApprovalGrant,
|
|
11
12
|
} from '../../runtime/approvals';
|
|
12
|
-
|
|
13
|
-
export interface ApprovalsProps {
|
|
14
|
-
readonly entries: readonly ApprovalEntry[];
|
|
15
|
-
/** Who is asking: the agent's name, as the thread names it. */
|
|
16
|
-
readonly agentName: string;
|
|
17
|
-
/**
|
|
18
|
-
* Carries the person's answer to Gemies. Rejects with `ApprovalExpiredError` when the question is
|
|
19
|
-
* no longer open. Omitted, a question is shown with no way to answer it.
|
|
20
|
-
*/
|
|
21
|
-
readonly onDecide?: (entry: ApprovalEntry, decision: ApprovalDecision) => Promise<void>;
|
|
22
|
-
/** The answer is known here (the person's own, or the question closed): show it now. */
|
|
23
|
-
readonly onSettled: (requestId: string, outcome: ApprovalOutcome) => void;
|
|
24
|
-
}
|
|
13
|
+
import type { ApprovalActions } from '../../runtime/use-approval-actions';
|
|
25
14
|
|
|
26
15
|
/**
|
|
27
16
|
* A coding CLI asking its own user for permission to run something on their computer, in the
|
|
28
|
-
* conversation (Gemies ADR 0043 §5): who is asking, what for, and the
|
|
29
|
-
* for the person; once answered the card shrinks to one line saying how it ended.
|
|
17
|
+
* conversation (Gemies ADR 0043 §5): who is asking, what for, and the answers it offers. The CLI
|
|
18
|
+
* waits for the person; once answered the card shrinks to one line saying how it ended.
|
|
30
19
|
*
|
|
31
|
-
* This is the CLI's prompt to the person who runs it, not a permission Gemies or Oxy asks for.
|
|
20
|
+
* This is the CLI's prompt to the person who runs it, not a permission Gemies or Oxy asks for. The
|
|
21
|
+
* composer shows the same question in a compact bar; both answer through the same actions, so a
|
|
22
|
+
* question answered in one is answered in both.
|
|
32
23
|
*/
|
|
33
24
|
export const ApprovalCards = React.memo(function ApprovalCards({
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}: ApprovalsProps) {
|
|
25
|
+
approvals,
|
|
26
|
+
}: {
|
|
27
|
+
approvals: ApprovalActions;
|
|
28
|
+
}) {
|
|
39
29
|
return (
|
|
40
30
|
<>
|
|
41
|
-
{entries.map((entry) => (
|
|
42
|
-
<ApprovalCard
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
onDecide={onDecide}
|
|
47
|
-
onSettled={onSettled}
|
|
48
|
-
/>
|
|
31
|
+
{approvals.entries.map((entry) => (
|
|
32
|
+
<ApprovalCard key={entry.requestId} entry={entry} approvals={approvals} />
|
|
33
|
+
))}
|
|
34
|
+
{approvals.grants.map((grant) => (
|
|
35
|
+
<ApprovalGrantRow key={grant.id} grant={grant} approvals={approvals} />
|
|
49
36
|
))}
|
|
50
37
|
</>
|
|
51
38
|
);
|
|
52
39
|
});
|
|
53
40
|
|
|
54
|
-
function ApprovalCard({
|
|
55
|
-
entry,
|
|
56
|
-
agentName,
|
|
57
|
-
onDecide,
|
|
58
|
-
onSettled,
|
|
59
|
-
}: {
|
|
60
|
-
entry: ApprovalEntry;
|
|
61
|
-
agentName: string;
|
|
62
|
-
onDecide: ApprovalsProps['onDecide'];
|
|
63
|
-
onSettled: ApprovalsProps['onSettled'];
|
|
64
|
-
}) {
|
|
41
|
+
function ApprovalCard({ entry, approvals }: { entry: ApprovalEntry; approvals: ApprovalActions }) {
|
|
65
42
|
const { t } = useLabels();
|
|
66
|
-
const [sending, setSending] = useState(false);
|
|
67
|
-
const [failed, setFailed] = useState(false);
|
|
68
|
-
|
|
69
|
-
const decide = useCallback(
|
|
70
|
-
(decision: ApprovalDecision) => {
|
|
71
|
-
if (sending || !onDecide) return;
|
|
72
|
-
setSending(true);
|
|
73
|
-
setFailed(false);
|
|
74
|
-
onDecide(entry, decision).then(
|
|
75
|
-
() => onSettled(entry.requestId, decision),
|
|
76
|
-
(error: unknown) => {
|
|
77
|
-
setSending(false);
|
|
78
|
-
if (error instanceof ApprovalExpiredError) onSettled(entry.requestId, 'expired');
|
|
79
|
-
else setFailed(true);
|
|
80
|
-
},
|
|
81
|
-
);
|
|
82
|
-
},
|
|
83
|
-
[sending, onDecide, entry, onSettled],
|
|
84
|
-
);
|
|
85
43
|
|
|
86
44
|
if (entry.outcome !== 'pending') {
|
|
45
|
+
const line = t(
|
|
46
|
+
entry.auto ? 'chat.approval.resolved.auto' : `chat.approval.resolved.${entry.outcome}`,
|
|
47
|
+
{ tool: entry.tool },
|
|
48
|
+
);
|
|
87
49
|
return (
|
|
88
50
|
<View style={styles.resolved} accessibilityRole="text">
|
|
89
|
-
<Muted>{
|
|
51
|
+
<Muted>{entry.feedback ? `${line} · “${entry.feedback}”` : line}</Muted>
|
|
90
52
|
</View>
|
|
91
53
|
);
|
|
92
54
|
}
|
|
93
55
|
|
|
94
|
-
const description = [
|
|
56
|
+
const description = [
|
|
57
|
+
entry.summary,
|
|
58
|
+
approvals.failed.has(entry.requestId) ? t('chat.approval.sendFailed') : undefined,
|
|
59
|
+
]
|
|
95
60
|
.filter(Boolean)
|
|
96
61
|
.join('\n');
|
|
62
|
+
const sending = approvals.sending.has(entry.requestId);
|
|
97
63
|
return (
|
|
98
64
|
<Notification
|
|
99
65
|
status="warning"
|
|
100
|
-
title={t('chat.approval.title', { agent: agentName, tool: entry.tool })}
|
|
66
|
+
title={t('chat.approval.title', { agent: approvals.agentName, tool: entry.tool })}
|
|
101
67
|
description={description === '' ? undefined : description}
|
|
102
68
|
// While an answer is on its way there is nothing to press: a second tap must not send another.
|
|
103
69
|
actions={
|
|
104
|
-
|
|
105
|
-
?
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
70
|
+
approvals.canAnswer && !sending
|
|
71
|
+
? orderedApprovalOptions(entry.options).map((option) => ({
|
|
72
|
+
label: approvalOptionLabel(option, t),
|
|
73
|
+
onPress: () => void approvals.decide(entry, option.kind, { optionId: option.id }),
|
|
74
|
+
...(option.kind === 'allow' ? {} : { appearance: 'subtle' as const }),
|
|
75
|
+
...(option.kind === 'cancel' ? { tone: 'danger' as const } : {}),
|
|
76
|
+
}))
|
|
109
77
|
: undefined
|
|
110
78
|
}
|
|
111
79
|
dismissible={false}
|
|
@@ -113,6 +81,40 @@ function ApprovalCard({
|
|
|
113
81
|
);
|
|
114
82
|
}
|
|
115
83
|
|
|
84
|
+
/** What the person allowed for the session, with the way to take it back. */
|
|
85
|
+
function ApprovalGrantRow({
|
|
86
|
+
grant,
|
|
87
|
+
approvals,
|
|
88
|
+
}: {
|
|
89
|
+
grant: ApprovalGrant;
|
|
90
|
+
approvals: ApprovalActions;
|
|
91
|
+
}) {
|
|
92
|
+
const { t } = useLabels();
|
|
93
|
+
return (
|
|
94
|
+
<View style={styles.grant}>
|
|
95
|
+
<Muted style={styles.grantText} numberOfLines={1}>
|
|
96
|
+
{`${t('chat.approval.grant', { tool: grant.tool })}: ${grant.target}`}
|
|
97
|
+
</Muted>
|
|
98
|
+
<Button
|
|
99
|
+
size="xs"
|
|
100
|
+
appearance="plain"
|
|
101
|
+
tone="neutral"
|
|
102
|
+
onPress={() => void approvals.revokeGrant(grant.id)}
|
|
103
|
+
>
|
|
104
|
+
{t('chat.approval.revoke')}
|
|
105
|
+
</Button>
|
|
106
|
+
</View>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
116
110
|
const styles = StyleSheet.create({
|
|
117
111
|
resolved: { paddingHorizontal: 16, paddingVertical: 4 },
|
|
112
|
+
grant: {
|
|
113
|
+
flexDirection: 'row',
|
|
114
|
+
alignItems: 'center',
|
|
115
|
+
gap: 8,
|
|
116
|
+
paddingHorizontal: 16,
|
|
117
|
+
paddingVertical: 2,
|
|
118
|
+
},
|
|
119
|
+
grantText: { flexShrink: 1 },
|
|
118
120
|
});
|
|
@@ -36,7 +36,8 @@ import type { ThreadHistory } from '../../runtime/thread/thread-history';
|
|
|
36
36
|
import type { AgentState } from '../../types';
|
|
37
37
|
import { AgentResultCard } from './AgentResultCard';
|
|
38
38
|
import { AgentTaskCard } from './AgentTaskCard';
|
|
39
|
-
import
|
|
39
|
+
import type { ApprovalActions } from '../../runtime/use-approval-actions';
|
|
40
|
+
import { ApprovalCards } from './ApprovalCard';
|
|
40
41
|
import { NewConversationOffer } from './NewConversationOffer';
|
|
41
42
|
import { type ThreadActions, ThreadRow } from './ThreadRow';
|
|
42
43
|
|
|
@@ -90,7 +91,7 @@ export interface GemieThreadProps {
|
|
|
90
91
|
* A coding CLI asking its own user for permission to run something on their computer, while the
|
|
91
92
|
* turn waits (ADR 0043 §5). Drawn after the turns, where the person is looking.
|
|
92
93
|
*/
|
|
93
|
-
approvals?:
|
|
94
|
+
approvals?: ApprovalActions;
|
|
94
95
|
actionApprovals?: ConversationActionApprovals;
|
|
95
96
|
/** The run an agent is on for this conversation: in progress, or the card it leaves. */
|
|
96
97
|
agentActivity?: AgentActivityState | null;
|
|
@@ -404,7 +405,7 @@ export const GemieThread = memo(function GemieThread({
|
|
|
404
405
|
) : null}
|
|
405
406
|
|
|
406
407
|
{approvals === undefined || approvals.entries.length === 0 ? null : (
|
|
407
|
-
<ApprovalCards {
|
|
408
|
+
<ApprovalCards approvals={approvals} />
|
|
408
409
|
)}
|
|
409
410
|
|
|
410
411
|
{/* The agent's offer to start the next stretch fresh. Last in the list on purpose: it
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
|
|
4
|
+
/** What a shortcut does; each is absent when the question does not offer it. */
|
|
5
|
+
export interface ApprovalShortcutActions {
|
|
6
|
+
readonly approve?: () => void;
|
|
7
|
+
readonly approveForSession?: () => void;
|
|
8
|
+
readonly decline?: () => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The key that does each thing, so the buttons can name it. */
|
|
12
|
+
export const APPROVAL_SHORTCUT_KEYS = {
|
|
13
|
+
approve: 'Y',
|
|
14
|
+
approveForSession: 'A',
|
|
15
|
+
decline: 'N',
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
/** The part of a key event the shortcuts read. */
|
|
19
|
+
export interface ShortcutKeyEvent {
|
|
20
|
+
readonly key: string;
|
|
21
|
+
readonly ctrlKey?: boolean;
|
|
22
|
+
readonly metaKey?: boolean;
|
|
23
|
+
readonly altKey?: boolean;
|
|
24
|
+
readonly shiftKey?: boolean;
|
|
25
|
+
readonly isComposing?: boolean;
|
|
26
|
+
/** The key is being held down. */
|
|
27
|
+
readonly repeat?: boolean;
|
|
28
|
+
readonly defaultPrevented?: boolean;
|
|
29
|
+
readonly target?: unknown;
|
|
30
|
+
preventDefault?: () => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Whether keys typed here are text: a field, or anything the person can edit. */
|
|
34
|
+
function isEditing(target: unknown): boolean {
|
|
35
|
+
const el = target as { tagName?: string; isContentEditable?: boolean } | null;
|
|
36
|
+
if (el === null || typeof el !== 'object') return false;
|
|
37
|
+
const tag = el.tagName?.toLowerCase();
|
|
38
|
+
return tag === 'input' || tag === 'textarea' || tag === 'select' || el.isContentEditable === true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What a key does to the question: `Y` approves it once, `A` allows it for the session, `N`
|
|
43
|
+
* declines it. Only a bare key pressed once counts — a held key must not answer the next question
|
|
44
|
+
* too — and not while the person is typing: the composer is where they write the reply that
|
|
45
|
+
* declines with feedback, and its letters are theirs. `Escape` is left alone: it closes whatever
|
|
46
|
+
* is open over the chat, and closing something must not decline a request.
|
|
47
|
+
*/
|
|
48
|
+
export function approvalShortcut(
|
|
49
|
+
event: ShortcutKeyEvent,
|
|
50
|
+
actions: ApprovalShortcutActions,
|
|
51
|
+
): (() => void) | undefined {
|
|
52
|
+
if (event.defaultPrevented || event.isComposing || event.repeat) return undefined;
|
|
53
|
+
if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return undefined;
|
|
54
|
+
if (isEditing(event.target)) return undefined;
|
|
55
|
+
switch (event.key) {
|
|
56
|
+
case 'y':
|
|
57
|
+
case 'Y':
|
|
58
|
+
return actions.approve;
|
|
59
|
+
case 'a':
|
|
60
|
+
case 'A':
|
|
61
|
+
return actions.approveForSession;
|
|
62
|
+
case 'n':
|
|
63
|
+
case 'N':
|
|
64
|
+
return actions.decline;
|
|
65
|
+
default:
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The keyboard shortcuts of the question the composer's bar shows, on web (there is no keyboard to
|
|
72
|
+
* speak of elsewhere). `target` is where keys are heard: the document, unless a test brings its own.
|
|
73
|
+
*/
|
|
74
|
+
export function useApprovalShortcuts(
|
|
75
|
+
actions: ApprovalShortcutActions | undefined,
|
|
76
|
+
target:
|
|
77
|
+
| Pick<EventTarget, 'addEventListener' | 'removeEventListener'>
|
|
78
|
+
| undefined = Platform.OS === 'web'
|
|
79
|
+
? (globalThis as { document?: Document }).document
|
|
80
|
+
: undefined,
|
|
81
|
+
): void {
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
if (actions === undefined || target === undefined) return;
|
|
84
|
+
const onKeyDown = (event: Event) => {
|
|
85
|
+
const run = approvalShortcut(event as unknown as ShortcutKeyEvent, actions);
|
|
86
|
+
if (run === undefined) return;
|
|
87
|
+
(event as unknown as ShortcutKeyEvent).preventDefault?.();
|
|
88
|
+
run();
|
|
89
|
+
};
|
|
90
|
+
target.addEventListener('keydown', onKeyDown);
|
|
91
|
+
return () => target.removeEventListener('keydown', onKeyDown);
|
|
92
|
+
}, [actions, target]);
|
|
93
|
+
}
|