@gemies/sdk 10.5.0 → 10.8.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 +43 -0
- package/package.json +1 -1
- package/src/components/GemieChatContent.tsx +1 -0
- package/src/components/GemieComposer.tsx +27 -2
- package/src/components/PlanPreviewCard.tsx +71 -18
- package/src/components/QuestionCard.tsx +47 -18
- package/src/hooks/useApprovalShortcuts.ts +1 -1
- package/src/hooks/useQuestionKeyboard.ts +193 -0
- package/src/index.ts +1 -0
- package/src/lib/copy-en.ts +5 -0
- package/src/lib/copy-es.ts +5 -0
- package/src/lib/deferred/markdown-file.ts +5 -0
- package/src/lib/deferred/markdown-file.web.ts +8 -0
- package/src/lib/markdown-file.ts +39 -0
- package/src/lib/plan-markdown.ts +19 -0
- package/src/runtime/index.ts +1 -0
- package/src/runtime/markdown-file.ts +14 -0
- package/src/runtime/power-levels.ts +9 -2
- package/src/runtime/resumable-stream.ts +19 -12
- package/src/runtime/thread/conversation-export.ts +1 -1
- package/src/runtime/use-conversation-stream.ts +21 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
# @gemies/sdk
|
|
2
2
|
|
|
3
|
+
## 10.8.0
|
|
4
|
+
|
|
5
|
+
### A plan to keep, and a question answered by keyboard
|
|
6
|
+
|
|
7
|
+
The plan card (`PlanPreviewCard`) folds away and back (remembered while the card is mounted), copies
|
|
8
|
+
its plan as Markdown, and downloads it as a `.md` file (the share sheet on a phone). New words:
|
|
9
|
+
`planCard.*` (en, es). `deliverMarkdownFile` is now exported from `@gemies/sdk/runtime`, for a host
|
|
10
|
+
that hands a Markdown document of its own to the person.
|
|
11
|
+
|
|
12
|
+
On web the question card (`QuestionCard`) is a full keyboard flow:
|
|
13
|
+
|
|
14
|
+
- It takes the focus when it appears and nothing else holds it. From an empty composer, ↑ moves into
|
|
15
|
+
it.
|
|
16
|
+
- ↑ and ↓ (and Home, End) move between the answers. Enter, Space and the digits pick, as before.
|
|
17
|
+
- ⌘/Ctrl+Enter is Next, or Done on the last question.
|
|
18
|
+
- Escape dismisses it, as its corner button does. In a free-text field with something typed, the
|
|
19
|
+
first Escape only leaves the field.
|
|
20
|
+
- A held key counts once. After a keyboard answer or dismissal the focus returns to the composer;
|
|
21
|
+
after a tap or a click it is left alone.
|
|
22
|
+
|
|
23
|
+
Touch and pointer behave as before. `QuestionCard` takes `autoFocus`, `onReturnFocus` and a `ref`
|
|
24
|
+
(`QuestionCardHandle`), and `GemieComposerQuestions` takes `autoFocus`; `GemieChatContent` sets it
|
|
25
|
+
while its chat is on screen.
|
|
26
|
+
|
|
27
|
+
## 10.7.0
|
|
28
|
+
|
|
29
|
+
### A turn is sent once
|
|
30
|
+
|
|
31
|
+
Every chat turn request now carries an `Idempotency-Key` equal to the turn's own id, the id the
|
|
32
|
+
server already names the turn by. A request that reaches the API twice (a replay after a token
|
|
33
|
+
refresh, a dropped response, a retry through another instance) starts the turn once. When the server
|
|
34
|
+
answers the second one from its receipt (`Idempotency-Replayed: true`), the SDK resumes the first
|
|
35
|
+
request's stream (`GET /v1/chat/turns/:turnId/stream`, 10.3.0) from its beginning. If that stream
|
|
36
|
+
cannot be followed to its end, the person's message is kept and no Retry is offered, since it would
|
|
37
|
+
send the message again. Requires the API of ADR 0049, which lists the header in CORS and answers
|
|
38
|
+
the replay.
|
|
39
|
+
|
|
40
|
+
## 10.6.0
|
|
41
|
+
|
|
42
|
+
`isDeviceModelId` also accepts `computer/<model>`, the ChatGPT on the person's own computer that
|
|
43
|
+
Gemies lists with their device models when it is switched on and its daemon is connected: it is
|
|
44
|
+
kept as the choice while connected and replaced by `auto` when it is not, like a `local/` model.
|
|
45
|
+
|
|
3
46
|
## 10.5.0
|
|
4
47
|
|
|
5
48
|
### A coding agent's work, as one row, with a command's output
|
package/package.json
CHANGED
|
@@ -783,6 +783,7 @@ function GemieConversationView({
|
|
|
783
783
|
items: asked.questions,
|
|
784
784
|
onSubmit: (message) => void composer.sendTurn(message, undefined, composer.turnOptions),
|
|
785
785
|
onDismiss: asked.dismiss,
|
|
786
|
+
autoFocus: isFocused,
|
|
786
787
|
}}
|
|
787
788
|
value={composer.text}
|
|
788
789
|
onValueChange={composer.setText}
|
|
@@ -36,6 +36,8 @@ import {
|
|
|
36
36
|
} from './ComposerDropZone';
|
|
37
37
|
import { ComposerSuggestions } from './ComposerSuggestions';
|
|
38
38
|
import { QuestionCard } from './QuestionCard';
|
|
39
|
+
import type { ShortcutKeyEvent } from '../hooks/useApprovalShortcuts';
|
|
40
|
+
import type { QuestionCardHandle } from '../hooks/useQuestionKeyboard';
|
|
39
41
|
import { createTokenStyles } from '@oxy.so/bloom/theme';
|
|
40
42
|
|
|
41
43
|
/**
|
|
@@ -72,6 +74,11 @@ export interface GemieComposerQuestions {
|
|
|
72
74
|
/** Sends the answers as the next message. */
|
|
73
75
|
onSubmit: (message: string) => void;
|
|
74
76
|
onDismiss: () => void;
|
|
77
|
+
/**
|
|
78
|
+
* The composer's chat is on screen, so the card may take the focus when nothing else holds it.
|
|
79
|
+
* Default `false`: on web, ↑ in an empty composer moves into the card either way.
|
|
80
|
+
*/
|
|
81
|
+
autoFocus?: boolean;
|
|
75
82
|
}
|
|
76
83
|
|
|
77
84
|
export interface GemieComposerProps<T extends GemieSuggestion = GemieSuggestion> {
|
|
@@ -173,6 +180,21 @@ function GemieComposerBody<T extends GemieSuggestion>({
|
|
|
173
180
|
(message: string) => (onError ? onError(message) : toast.error(message)),
|
|
174
181
|
[onError],
|
|
175
182
|
);
|
|
183
|
+
const ownFieldRef = useRef<TextInputHandle | null>(null);
|
|
184
|
+
const fieldRef = inputRef ?? ownFieldRef;
|
|
185
|
+
const questionRef = useRef<QuestionCardHandle>(null);
|
|
186
|
+
const returnToField = useCallback(() => fieldRef.current?.focus(), [fieldRef]);
|
|
187
|
+
// ↑ in an empty field moves into the question above it, once the host has had the key.
|
|
188
|
+
const handleKeyPress = useCallback(
|
|
189
|
+
(event: TextInputKeyPressEvent) => {
|
|
190
|
+
onKeyPress?.(event);
|
|
191
|
+
if (event.defaultPrevented || value !== '') return;
|
|
192
|
+
const { key, shiftKey, altKey, ctrlKey, metaKey } = event.nativeEvent as ShortcutKeyEvent;
|
|
193
|
+
if (key !== 'ArrowUp' || shiftKey || altKey || ctrlKey || metaKey) return;
|
|
194
|
+
if (questionRef.current?.focus()) event.preventDefault();
|
|
195
|
+
},
|
|
196
|
+
[onKeyPress, value],
|
|
197
|
+
);
|
|
176
198
|
// Paste is a DOM gesture on a DOM node; the panel publishes no ref to one.
|
|
177
199
|
const hostId = `composer-${useId().replace(/[^a-zA-Z0-9]/g, '')}`;
|
|
178
200
|
|
|
@@ -312,9 +334,12 @@ function GemieComposerBody<T extends GemieSuggestion>({
|
|
|
312
334
|
// Beside the loader, not through the pill's `question` slot: the light traces the box it
|
|
313
335
|
// wraps, so a question inside it would be ringed too.
|
|
314
336
|
<QuestionCard
|
|
337
|
+
ref={questionRef}
|
|
315
338
|
questions={questions.items}
|
|
316
339
|
onSubmit={questions.onSubmit}
|
|
317
340
|
onDismiss={questions.onDismiss}
|
|
341
|
+
autoFocus={questions.autoFocus}
|
|
342
|
+
onReturnFocus={returnToField}
|
|
318
343
|
/>
|
|
319
344
|
) : null}
|
|
320
345
|
<ComposerLoader phase={loaderPhase ?? (busy ? 'streaming' : 'idle')}>
|
|
@@ -322,7 +347,7 @@ function GemieComposerBody<T extends GemieSuggestion>({
|
|
|
322
347
|
value={value}
|
|
323
348
|
onValueChange={onValueChange}
|
|
324
349
|
onSubmit={() => onSubmit()}
|
|
325
|
-
inputRef={
|
|
350
|
+
inputRef={fieldRef}
|
|
326
351
|
busy={busy}
|
|
327
352
|
onStop={onStop}
|
|
328
353
|
disabled={disabled || intake.isBusy || !hasContent}
|
|
@@ -347,7 +372,7 @@ function GemieComposerBody<T extends GemieSuggestion>({
|
|
|
347
372
|
onAttachmentRetry={intake.retry}
|
|
348
373
|
status={status}
|
|
349
374
|
emptyAction={emptyAction}
|
|
350
|
-
onKeyPress={
|
|
375
|
+
onKeyPress={handleKeyPress}
|
|
351
376
|
labels={panelLabels}
|
|
352
377
|
// The loader paints the pill's surface behind its light; the controls go frosted over it.
|
|
353
378
|
surface={false}
|
|
@@ -1,14 +1,25 @@
|
|
|
1
|
+
import { Button } from '@oxy.so/bloom/button';
|
|
1
2
|
import { Card, CardBody, CardHeader, CardTitle } from '@oxy.so/bloom/card';
|
|
3
|
+
import { Collapsible } from '@oxy.so/bloom/collapsible';
|
|
4
|
+
import { RiArrowDownSLine } from '@oxy.so/bloom/icons/RiArrowDownSLine';
|
|
5
|
+
import { RiArrowUpSLine } from '@oxy.so/bloom/icons/RiArrowUpSLine';
|
|
6
|
+
import { RiDownload2Line } from '@oxy.so/bloom/icons/RiDownload2Line';
|
|
7
|
+
import { RiFileCopyLine } from '@oxy.so/bloom/icons/RiFileCopyLine';
|
|
2
8
|
import { RiListCheck3 } from '@oxy.so/bloom/icons/RiListCheck3';
|
|
3
9
|
import { Item } from '@oxy.so/bloom/item';
|
|
4
|
-
import { useTheme } from '@oxy.so/bloom/theme';
|
|
10
|
+
import { createTokenStyles, useTheme } from '@oxy.so/bloom/theme';
|
|
11
|
+
import { toast } from '@oxy.so/bloom/toast';
|
|
5
12
|
import { Text } from '@oxy.so/bloom/typography';
|
|
13
|
+
import { useState } from 'react';
|
|
6
14
|
import { View } from 'react-native';
|
|
7
15
|
import Animated from 'react-native-reanimated';
|
|
8
16
|
import { planPreviewEnter } from '../animations';
|
|
17
|
+
import { copyText } from '../lib/clipboard';
|
|
9
18
|
import { useLabels } from '../lib/labels';
|
|
19
|
+
import { planMarkdown } from '../lib/plan-markdown';
|
|
20
|
+
import { deliverMarkdownFile } from '../runtime/markdown-file';
|
|
21
|
+
import { exportFilename } from '../runtime/thread/conversation-export';
|
|
10
22
|
import type { PlanStep } from '../types';
|
|
11
|
-
import { createTokenStyles } from '@oxy.so/bloom/theme';
|
|
12
23
|
|
|
13
24
|
interface PlanPreviewCardProps {
|
|
14
25
|
steps: PlanStep[];
|
|
@@ -16,12 +27,23 @@ interface PlanPreviewCardProps {
|
|
|
16
27
|
|
|
17
28
|
/**
|
|
18
29
|
* The plan the agent shows before it works. It is only shown: the agent does not wait on it, and
|
|
19
|
-
* nobody is asked to approve it.
|
|
30
|
+
* nobody is asked to approve it. The person can fold it away (remembered for as long as this card
|
|
31
|
+
* is mounted), copy it, or take it as a Markdown file.
|
|
20
32
|
*/
|
|
21
33
|
export function PlanPreviewCard({ steps }: PlanPreviewCardProps) {
|
|
22
34
|
const styles = useStyles();
|
|
23
|
-
const
|
|
35
|
+
const { planTitle, t } = useLabels();
|
|
24
36
|
const { colors } = useTheme();
|
|
37
|
+
const [open, setOpen] = useState(true);
|
|
38
|
+
|
|
39
|
+
const copy = () => void copyText(planMarkdown(planTitle, steps), t);
|
|
40
|
+
const download = () => {
|
|
41
|
+
deliverMarkdownFile(
|
|
42
|
+
exportFilename('plan', new Date()),
|
|
43
|
+
planMarkdown(planTitle, steps),
|
|
44
|
+
planTitle,
|
|
45
|
+
).catch(() => toast.error(t('planCard.downloadFailed')));
|
|
46
|
+
};
|
|
25
47
|
|
|
26
48
|
return (
|
|
27
49
|
<Animated.View entering={planPreviewEnter} style={styles.card}>
|
|
@@ -29,22 +51,52 @@ export function PlanPreviewCard({ steps }: PlanPreviewCardProps) {
|
|
|
29
51
|
<CardHeader>
|
|
30
52
|
<View style={styles.titleRow}>
|
|
31
53
|
<RiListCheck3 width={16} height={16} fill={colors.icon} />
|
|
32
|
-
<CardTitle>{
|
|
54
|
+
<CardTitle style={styles.title}>{planTitle}</CardTitle>
|
|
55
|
+
<Button
|
|
56
|
+
size="xs"
|
|
57
|
+
appearance="plain"
|
|
58
|
+
tone="neutral"
|
|
59
|
+
iconOnly
|
|
60
|
+
icon={RiFileCopyLine}
|
|
61
|
+
accessibilityLabel={t('planCard.copy')}
|
|
62
|
+
onPress={copy}
|
|
63
|
+
/>
|
|
64
|
+
<Button
|
|
65
|
+
size="xs"
|
|
66
|
+
appearance="plain"
|
|
67
|
+
tone="neutral"
|
|
68
|
+
iconOnly
|
|
69
|
+
icon={RiDownload2Line}
|
|
70
|
+
accessibilityLabel={t('planCard.download')}
|
|
71
|
+
onPress={download}
|
|
72
|
+
/>
|
|
73
|
+
<Button
|
|
74
|
+
size="xs"
|
|
75
|
+
appearance="plain"
|
|
76
|
+
tone="neutral"
|
|
77
|
+
iconOnly
|
|
78
|
+
icon={open ? RiArrowUpSLine : RiArrowDownSLine}
|
|
79
|
+
accessibilityLabel={t(open ? 'planCard.collapse' : 'planCard.expand')}
|
|
80
|
+
aria-expanded={open}
|
|
81
|
+
onPress={() => setOpen((current) => !current)}
|
|
82
|
+
/>
|
|
33
83
|
</View>
|
|
34
84
|
</CardHeader>
|
|
35
|
-
<
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
85
|
+
<Collapsible open={open}>
|
|
86
|
+
<CardBody>
|
|
87
|
+
{steps.map((step, index) => (
|
|
88
|
+
<Item
|
|
89
|
+
// A plan has no ids and its steps never reorder: the position is the identity.
|
|
90
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: see above
|
|
91
|
+
key={index}
|
|
92
|
+
density="compact"
|
|
93
|
+
leading={<Text style={styles.stepNumber}>{index + 1}</Text>}
|
|
94
|
+
title={step.action}
|
|
95
|
+
subtitle={step.description || undefined}
|
|
96
|
+
/>
|
|
97
|
+
))}
|
|
98
|
+
</CardBody>
|
|
99
|
+
</Collapsible>
|
|
48
100
|
</Card>
|
|
49
101
|
</Animated.View>
|
|
50
102
|
);
|
|
@@ -53,6 +105,7 @@ export function PlanPreviewCard({ steps }: PlanPreviewCardProps) {
|
|
|
53
105
|
const useStyles = createTokenStyles((t) => ({
|
|
54
106
|
card: { marginVertical: 8 },
|
|
55
107
|
titleRow: { flexDirection: 'row', alignItems: 'center', gap: 8 },
|
|
108
|
+
title: { flex: 1 },
|
|
56
109
|
stepNumber: {
|
|
57
110
|
width: 20,
|
|
58
111
|
textAlign: 'center',
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Questionnaire, type QuestionnaireAnswers } from '@oxy.so/bloom/questionnaire';
|
|
2
2
|
import { Muted } from '@oxy.so/bloom/typography';
|
|
3
|
+
import { type Ref, useId } from 'react';
|
|
3
4
|
import { View, StyleSheet } from 'react-native';
|
|
5
|
+
import { type QuestionCardHandle, useQuestionKeyboard } from '../hooks/useQuestionKeyboard';
|
|
4
6
|
import { type AskedQuestion, formatAnswers } from '../lib/questions';
|
|
5
7
|
|
|
6
8
|
interface QuestionCardProps {
|
|
@@ -9,6 +11,12 @@ interface QuestionCardProps {
|
|
|
9
11
|
onSubmit: (message: string) => void;
|
|
10
12
|
/** Shows the corner dismiss; typing a reply in the composer answers just as well. */
|
|
11
13
|
onDismiss?: () => void;
|
|
14
|
+
/** Take the focus when the card appears and nothing else holds it. Default `false`. */
|
|
15
|
+
autoFocus?: boolean;
|
|
16
|
+
/** The keyboard answered or dismissed the card: move the focus on, to the composer. */
|
|
17
|
+
onReturnFocus?: () => void;
|
|
18
|
+
/** Lets a host move the keyboard into the card. */
|
|
19
|
+
ref?: Ref<QuestionCardHandle>;
|
|
12
20
|
}
|
|
13
21
|
|
|
14
22
|
/** Ids are positions: the questions are the model's, and carry none. */
|
|
@@ -17,10 +25,27 @@ const idOf = (index: number) => `q${index}`;
|
|
|
17
25
|
/**
|
|
18
26
|
* The questions Alia asked, as Bloom's `Questionnaire`. Give it to the composer's `question`
|
|
19
27
|
* slot while the person can still answer: it docks above the composer, where they are about
|
|
20
|
-
* to type, rather than sitting in the thread.
|
|
28
|
+
* to type, rather than sitting in the thread. On web it is answered by keyboard too
|
|
29
|
+
* (`useQuestionKeyboard`).
|
|
21
30
|
*/
|
|
22
|
-
export function QuestionCard({
|
|
23
|
-
|
|
31
|
+
export function QuestionCard({
|
|
32
|
+
questions,
|
|
33
|
+
onSubmit,
|
|
34
|
+
onDismiss,
|
|
35
|
+
autoFocus = false,
|
|
36
|
+
onReturnFocus,
|
|
37
|
+
ref,
|
|
38
|
+
}: QuestionCardProps) {
|
|
39
|
+
const testID = `question-card-${useId().replace(/[^a-zA-Z0-9]/g, '')}`;
|
|
40
|
+
const { rootRef, leave, dismiss } = useQuestionKeyboard({
|
|
41
|
+
testID,
|
|
42
|
+
autoFocus,
|
|
43
|
+
onDismiss,
|
|
44
|
+
onReturnFocus,
|
|
45
|
+
ref,
|
|
46
|
+
});
|
|
47
|
+
const complete = (answers: QuestionnaireAnswers) => {
|
|
48
|
+
leave();
|
|
24
49
|
onSubmit(
|
|
25
50
|
formatAnswers(
|
|
26
51
|
questions,
|
|
@@ -30,23 +55,27 @@ export function QuestionCard({ questions, onSubmit, onDismiss }: QuestionCardPro
|
|
|
30
55
|
})),
|
|
31
56
|
),
|
|
32
57
|
);
|
|
58
|
+
};
|
|
33
59
|
|
|
34
60
|
return (
|
|
35
|
-
<
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
61
|
+
<View ref={rootRef}>
|
|
62
|
+
<Questionnaire
|
|
63
|
+
testID={testID}
|
|
64
|
+
questions={questions.map((q, i) => ({
|
|
65
|
+
id: idOf(i),
|
|
66
|
+
question: q.question,
|
|
67
|
+
select: q.multiSelect ? 'multiple' : 'single',
|
|
68
|
+
other: true,
|
|
69
|
+
options: q.options.map((option, index) => ({
|
|
70
|
+
value: String(index),
|
|
71
|
+
label: option.label,
|
|
72
|
+
description: option.description,
|
|
73
|
+
})),
|
|
74
|
+
}))}
|
|
75
|
+
onComplete={complete}
|
|
76
|
+
onDismiss={onDismiss ? dismiss : undefined}
|
|
77
|
+
/>
|
|
78
|
+
</View>
|
|
50
79
|
);
|
|
51
80
|
}
|
|
52
81
|
|
|
@@ -31,7 +31,7 @@ export interface ShortcutKeyEvent {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/** Whether keys typed here are text: a field, or anything the person can edit. */
|
|
34
|
-
function isEditing(target: unknown): boolean {
|
|
34
|
+
export function isEditing(target: unknown): boolean {
|
|
35
35
|
const el = target as { tagName?: string; isContentEditable?: boolean } | null;
|
|
36
36
|
if (el === null || typeof el !== 'object') return false;
|
|
37
37
|
const tag = el.tagName?.toLowerCase();
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ComponentRef,
|
|
3
|
+
type Ref,
|
|
4
|
+
type RefObject,
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useImperativeHandle,
|
|
8
|
+
useRef,
|
|
9
|
+
} from 'react';
|
|
10
|
+
import { Platform, type View } from 'react-native';
|
|
11
|
+
import { isEditing, type ShortcutKeyEvent } from './useApprovalShortcuts';
|
|
12
|
+
|
|
13
|
+
/** What a key pressed inside the question card asks of it. */
|
|
14
|
+
export type QuestionKeyCommand =
|
|
15
|
+
| 'previous'
|
|
16
|
+
| 'next'
|
|
17
|
+
| 'first'
|
|
18
|
+
| 'last'
|
|
19
|
+
/** Next, or Done on the last question. */
|
|
20
|
+
| 'confirm'
|
|
21
|
+
| 'dismiss'
|
|
22
|
+
/** Out of the free-text field, back to the options, keeping what was typed. */
|
|
23
|
+
| 'leaveField'
|
|
24
|
+
/** A held key: it must not answer the next question too. */
|
|
25
|
+
| 'swallow';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What a key does while the focus is in the question card. Enter, Space and the digits that pick an
|
|
29
|
+
* answer are the card's own (Bloom's `Questionnaire`); this adds what it lacks. As with the
|
|
30
|
+
* approval keys, a held key counts once and an input method's keys are left to it.
|
|
31
|
+
*/
|
|
32
|
+
export function questionKey(event: ShortcutKeyEvent): QuestionKeyCommand | undefined {
|
|
33
|
+
if (event.defaultPrevented || event.isComposing || event.altKey || event.shiftKey) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const { key } = event;
|
|
37
|
+
const typing = isEditing(event.target);
|
|
38
|
+
if (event.repeat && (key === 'Enter' || (!typing && /^[ 1-9]$/.test(key)))) return 'swallow';
|
|
39
|
+
if (event.ctrlKey || event.metaKey) return key === 'Enter' ? 'confirm' : undefined;
|
|
40
|
+
switch (key) {
|
|
41
|
+
case 'ArrowUp':
|
|
42
|
+
return 'previous';
|
|
43
|
+
case 'ArrowDown':
|
|
44
|
+
return 'next';
|
|
45
|
+
case 'Home':
|
|
46
|
+
return typing ? undefined : 'first';
|
|
47
|
+
case 'End':
|
|
48
|
+
return typing ? undefined : 'last';
|
|
49
|
+
case 'Escape': {
|
|
50
|
+
if (event.repeat) return 'swallow';
|
|
51
|
+
const draft = typing ? (event.target as { value?: string }).value : undefined;
|
|
52
|
+
return draft ? 'leaveField' : 'dismiss';
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** How a host moves the keyboard into the card. */
|
|
60
|
+
export interface QuestionCardHandle {
|
|
61
|
+
/** Focus the picked answer, or the first. `false` where there is no keyboard focus to move. */
|
|
62
|
+
focus: () => boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface QuestionKeyboardOptions {
|
|
66
|
+
/** The `testID` given to the `Questionnaire`, whose rows and buttons are found by it. */
|
|
67
|
+
testID: string;
|
|
68
|
+
/** Take the focus when the card appears and nothing else holds it. */
|
|
69
|
+
autoFocus: boolean;
|
|
70
|
+
onDismiss?: () => void;
|
|
71
|
+
/** The card is about to go and it was the keyboard that sent it away. */
|
|
72
|
+
onReturnFocus?: () => void;
|
|
73
|
+
ref?: Ref<QuestionCardHandle>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
interface QuestionKeyboard {
|
|
77
|
+
/** For the view around the card: where its keys are heard. */
|
|
78
|
+
rootRef: RefObject<ComponentRef<typeof View> | null>;
|
|
79
|
+
/** Call before the card goes: gives the focus back if the keyboard is what is leaving it. */
|
|
80
|
+
leave: () => void;
|
|
81
|
+
/** Hand the focus back as `leave` does, then dismiss. */
|
|
82
|
+
dismiss: () => void;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const isWeb = () => Platform.OS === 'web';
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The question card's keyboard, on web (elsewhere the platform's own focus handling stands): the
|
|
89
|
+
* arrows, Home and End move between the answers, ⌘/Ctrl+Enter goes on, Escape puts the card away as
|
|
90
|
+
* its dismiss button does, and a keyboard answer hands the focus back to the composer.
|
|
91
|
+
*/
|
|
92
|
+
export function useQuestionKeyboard({
|
|
93
|
+
testID,
|
|
94
|
+
autoFocus,
|
|
95
|
+
onDismiss,
|
|
96
|
+
onReturnFocus,
|
|
97
|
+
ref,
|
|
98
|
+
}: QuestionKeyboardOptions): QuestionKeyboard {
|
|
99
|
+
const rootRef = useRef<ComponentRef<typeof View>>(null);
|
|
100
|
+
/** Whether the last thing done in the card was a key: a tap must not raise a phone's keyboard. */
|
|
101
|
+
const byKeyboard = useRef(false);
|
|
102
|
+
const latest = useRef({ onDismiss, onReturnFocus });
|
|
103
|
+
latest.current = { onDismiss, onReturnFocus };
|
|
104
|
+
|
|
105
|
+
const root = useCallback(
|
|
106
|
+
() => (isWeb() ? (rootRef.current as unknown as HTMLElement | null) : null),
|
|
107
|
+
[],
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
/** The answers of the question on screen, in order, the free-text field last. */
|
|
111
|
+
const answers = useCallback(
|
|
112
|
+
() =>
|
|
113
|
+
Array.from(
|
|
114
|
+
root()?.querySelectorAll<HTMLElement>(
|
|
115
|
+
`[data-testid^="${testID}-option-"], [data-testid="${testID}-other-input"]`,
|
|
116
|
+
) ?? [],
|
|
117
|
+
),
|
|
118
|
+
[root, testID],
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const focus = useCallback(() => {
|
|
122
|
+
const all = answers();
|
|
123
|
+
const picked = all.find(
|
|
124
|
+
(el) =>
|
|
125
|
+
el.getAttribute('aria-pressed') === 'true' || el.getAttribute('aria-checked') === 'true',
|
|
126
|
+
);
|
|
127
|
+
const target = picked ?? all[0];
|
|
128
|
+
// The card is docked in view; moving into it must not scroll the page under it.
|
|
129
|
+
target?.focus({ preventScroll: true });
|
|
130
|
+
return target !== undefined;
|
|
131
|
+
}, [answers]);
|
|
132
|
+
|
|
133
|
+
const leave = useCallback(() => {
|
|
134
|
+
const node = root();
|
|
135
|
+
if (!byKeyboard.current || !node?.contains(document.activeElement)) return;
|
|
136
|
+
latest.current.onReturnFocus?.();
|
|
137
|
+
}, [root]);
|
|
138
|
+
|
|
139
|
+
const dismiss = useCallback(() => {
|
|
140
|
+
leave();
|
|
141
|
+
latest.current.onDismiss?.();
|
|
142
|
+
}, [leave]);
|
|
143
|
+
|
|
144
|
+
useImperativeHandle(ref, () => ({ focus }), [focus]);
|
|
145
|
+
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
if (!autoFocus || !root()) return;
|
|
148
|
+
const active = document.activeElement;
|
|
149
|
+
if (active === null || active === document.body) focus();
|
|
150
|
+
}, [autoFocus, root, focus]);
|
|
151
|
+
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
const node = root();
|
|
154
|
+
if (!node) return;
|
|
155
|
+
const move = (to: (index: number, count: number) => number) => {
|
|
156
|
+
const all = answers();
|
|
157
|
+
if (all.length === 0) return;
|
|
158
|
+
const index = all.indexOf(document.activeElement as HTMLElement);
|
|
159
|
+
all[(to(index, all.length) + all.length) % all.length]?.focus();
|
|
160
|
+
};
|
|
161
|
+
const run: Record<QuestionKeyCommand, () => void> = {
|
|
162
|
+
previous: () => move((index, count) => (index < 0 ? count - 1 : index - 1)),
|
|
163
|
+
next: () => move((index) => index + 1),
|
|
164
|
+
first: () => move(() => 0),
|
|
165
|
+
last: () => move((_, count) => count - 1),
|
|
166
|
+
confirm: () => node.querySelector<HTMLElement>(`[data-testid="${testID}-next"]`)?.click(),
|
|
167
|
+
dismiss,
|
|
168
|
+
leaveField: () => void focus(),
|
|
169
|
+
swallow: () => {},
|
|
170
|
+
};
|
|
171
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
172
|
+
byKeyboard.current = true;
|
|
173
|
+
const command = questionKey(event);
|
|
174
|
+
if (command === undefined || (command === 'dismiss' && !latest.current.onDismiss)) return;
|
|
175
|
+
// Heard on the way down and stopped here: the focused row would take ⌘Enter as a press, and
|
|
176
|
+
// whatever is open under the card would take its Escape.
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
event.stopPropagation();
|
|
179
|
+
run[command]();
|
|
180
|
+
};
|
|
181
|
+
const onPointerDown = () => {
|
|
182
|
+
byKeyboard.current = false;
|
|
183
|
+
};
|
|
184
|
+
node.addEventListener('keydown', onKeyDown, true);
|
|
185
|
+
node.addEventListener('pointerdown', onPointerDown, true);
|
|
186
|
+
return () => {
|
|
187
|
+
node.removeEventListener('keydown', onKeyDown, true);
|
|
188
|
+
node.removeEventListener('pointerdown', onPointerDown, true);
|
|
189
|
+
};
|
|
190
|
+
}, [root, answers, focus, dismiss, testID]);
|
|
191
|
+
|
|
192
|
+
return { rootRef, leave, dismiss };
|
|
193
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -82,6 +82,7 @@ export type { DeviceModel } from './lib/power-level-options';
|
|
|
82
82
|
export { GemieWelcomeMessage } from './components/GemieWelcomeMessage';
|
|
83
83
|
export { PlanPreviewCard } from './components/PlanPreviewCard';
|
|
84
84
|
export { AskedQuestions, QuestionCard } from './components/QuestionCard';
|
|
85
|
+
export type { QuestionCardHandle } from './hooks/useQuestionKeyboard';
|
|
85
86
|
export { GemieComposer } from './components/GemieComposer';
|
|
86
87
|
export type {
|
|
87
88
|
GemieComposerProps,
|
package/src/lib/copy-en.ts
CHANGED
|
@@ -417,6 +417,11 @@ export const COPY_EN: Readonly<Record<string, CopyEntry>> = {
|
|
|
417
417
|
'panels.form.enter': 'Enter {{field}}',
|
|
418
418
|
'panels.form.select': 'Select…',
|
|
419
419
|
'panels.form.submit': 'Submit',
|
|
420
|
+
'planCard.collapse': 'Collapse plan',
|
|
421
|
+
'planCard.copy': 'Copy plan',
|
|
422
|
+
'planCard.download': 'Download plan as Markdown',
|
|
423
|
+
'planCard.downloadFailed': "Couldn't download the plan",
|
|
424
|
+
'planCard.expand': 'Expand plan',
|
|
420
425
|
'powerLevels.auto.description': 'Picks the right level for each message',
|
|
421
426
|
'powerLevels.auto.label': 'Auto',
|
|
422
427
|
'powerLevels.high.description': 'Thinks harder on tougher questions',
|
package/src/lib/copy-es.ts
CHANGED
|
@@ -422,6 +422,11 @@ export const COPY_ES: Readonly<Record<string, CopyEntry>> = {
|
|
|
422
422
|
'panels.form.enter': 'Escribe {{field}}',
|
|
423
423
|
'panels.form.select': 'Selecciona…',
|
|
424
424
|
'panels.form.submit': 'Enviar',
|
|
425
|
+
'planCard.collapse': 'Contraer el plan',
|
|
426
|
+
'planCard.copy': 'Copiar el plan',
|
|
427
|
+
'planCard.download': 'Descargar el plan en Markdown',
|
|
428
|
+
'planCard.downloadFailed': 'No se pudo descargar el plan',
|
|
429
|
+
'planCard.expand': 'Desplegar el plan',
|
|
425
430
|
'powerLevels.auto.description': 'Elige el nivel adecuado para cada mensaje',
|
|
426
431
|
'powerLevels.auto.label': 'Auto',
|
|
427
432
|
'powerLevels.high.description': 'Piensa más en las preguntas difíciles',
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Markdown file writer on web, imported statically: an `import()` here would be an async chunk
|
|
3
|
+
* boundary inside the SDK (see `./image-data-url.web.ts`). Native keeps the lazy load.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as MarkdownFile from '../markdown-file';
|
|
7
|
+
|
|
8
|
+
export const loadMarkdownFile = async () => MarkdownFile;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { File, Paths } from 'expo-file-system';
|
|
2
|
+
import { Platform, Share } from 'react-native';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hand a Markdown document to the person, however this platform does that.
|
|
6
|
+
*
|
|
7
|
+
* On web that is a download: a `Blob` behind a temporary object URL, clicked
|
|
8
|
+
* through an anchor that names the file. The URL is revoked once the click
|
|
9
|
+
* has been dispatched — synchronously would race the browser reading it.
|
|
10
|
+
*
|
|
11
|
+
* On a phone the document is written to the CACHE directory (the system may
|
|
12
|
+
* reclaim it, which is right for a file whose only purpose is to be handed
|
|
13
|
+
* on) and offered through React Native's own share sheet: iOS's sheet takes a
|
|
14
|
+
* file `url` and shares the `.md` itself, while Android's `Share.share` accepts
|
|
15
|
+
* only `message`, so there the sheet carries the document's text.
|
|
16
|
+
*/
|
|
17
|
+
export async function deliverMarkdownFile(
|
|
18
|
+
filename: string,
|
|
19
|
+
content: string,
|
|
20
|
+
title: string,
|
|
21
|
+
): Promise<void> {
|
|
22
|
+
if (Platform.OS === 'web') {
|
|
23
|
+
const blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
|
|
24
|
+
const url = URL.createObjectURL(blob);
|
|
25
|
+
const anchor = document.createElement('a');
|
|
26
|
+
anchor.href = url;
|
|
27
|
+
anchor.download = filename;
|
|
28
|
+
anchor.rel = 'noopener';
|
|
29
|
+
document.body.appendChild(anchor);
|
|
30
|
+
anchor.click();
|
|
31
|
+
anchor.remove();
|
|
32
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const file = new File(Paths.cache, filename);
|
|
37
|
+
await file.write(content);
|
|
38
|
+
await Share.share(Platform.OS === 'ios' ? { url: file.uri, title } : { message: content, title });
|
|
39
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { PlanStep } from '../types';
|
|
2
|
+
|
|
3
|
+
/** Continuation lines belong to the list item they follow, so they are indented under it. */
|
|
4
|
+
const indented = (text: string) =>
|
|
5
|
+
text
|
|
6
|
+
.split('\n')
|
|
7
|
+
.map((line) => line.trim())
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
.join('\n ');
|
|
10
|
+
|
|
11
|
+
/** A plan as a Markdown document: its title, then the steps as a numbered list. */
|
|
12
|
+
export function planMarkdown(title: string, steps: readonly PlanStep[]): string {
|
|
13
|
+
const items = steps.map((step, index) => {
|
|
14
|
+
const action = `${index + 1}. ${indented(step.action)}`;
|
|
15
|
+
const description = indented(step.description ?? '');
|
|
16
|
+
return description ? `${action}\n ${description}` : action;
|
|
17
|
+
});
|
|
18
|
+
return `# ${title}\n\n${items.join('\n')}\n`;
|
|
19
|
+
}
|
package/src/runtime/index.ts
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { loadMarkdownFile } from '../lib/deferred/markdown-file';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hand a Markdown document to the person as a file: a download on web, the share sheet on a phone
|
|
5
|
+
* (`lib/markdown-file.ts`). A conversation's export and a plan's download both go through here.
|
|
6
|
+
*/
|
|
7
|
+
export async function deliverMarkdownFile(
|
|
8
|
+
filename: string,
|
|
9
|
+
content: string,
|
|
10
|
+
title: string,
|
|
11
|
+
): Promise<void> {
|
|
12
|
+
const { deliverMarkdownFile: deliver } = await loadMarkdownFile();
|
|
13
|
+
await deliver(filename, content, title);
|
|
14
|
+
}
|
|
@@ -22,9 +22,16 @@ export function isPowerLevel(value: unknown): value is PowerLevel {
|
|
|
22
22
|
return typeof value === 'string' && (POWER_LEVELS as readonly string[]).includes(value);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* A model served by one of the person's own devices: `local/<runtime>/<model>`, or the ChatGPT on
|
|
27
|
+
* their computer that Gemies offers when its daemon is connected (`computer/chatgpt`, ADR 0047).
|
|
28
|
+
*/
|
|
26
29
|
export function isDeviceModelId(value: unknown): value is string {
|
|
27
|
-
return
|
|
30
|
+
return (
|
|
31
|
+
typeof value === 'string' &&
|
|
32
|
+
((value.startsWith('local/') && value.length > 'local/'.length) ||
|
|
33
|
+
(value.startsWith('computer/') && value.length > 'computer/'.length))
|
|
34
|
+
);
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
export interface ModeSelection {
|
|
@@ -72,8 +72,12 @@ function sleepUnlessAborted(ms: number, signal: AbortSignal): Promise<void> {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
export interface ResumableFrameSourceOptions {
|
|
75
|
-
/**
|
|
76
|
-
|
|
75
|
+
/**
|
|
76
|
+
* The turn's first response, already known to be `ok` with a body; or null when the server said it
|
|
77
|
+
* already has this turn (ADR 0049) and there is no first stream: reading starts by resuming it
|
|
78
|
+
* from its first frame.
|
|
79
|
+
*/
|
|
80
|
+
response: Response | null;
|
|
77
81
|
/** Open the turn's stream again, after the frame numbered `cursor`. */
|
|
78
82
|
reconnect(cursor: number, signal: AbortSignal): Promise<Response>;
|
|
79
83
|
/** The turn's own signal: a Stop or a detach ends the stream, never a resume. */
|
|
@@ -176,7 +180,7 @@ export function createResumableFrameSource(
|
|
|
176
180
|
else options.signal.addEventListener('abort', followTurn, { once: true });
|
|
177
181
|
const { signal } = lifetime;
|
|
178
182
|
|
|
179
|
-
let reader = options.response
|
|
183
|
+
let reader = options.response?.body?.getReader();
|
|
180
184
|
let decoder = new TextDecoder();
|
|
181
185
|
let frames = createSseFrameReader();
|
|
182
186
|
/** The number of the newest frame passed on. */
|
|
@@ -277,17 +281,20 @@ export function createResumableFrameSource(
|
|
|
277
281
|
while (true) {
|
|
278
282
|
failure = undefined;
|
|
279
283
|
try {
|
|
280
|
-
if (
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
284
|
+
if (reader) {
|
|
285
|
+
const { done, value } = await reader.read();
|
|
286
|
+
if (!done) {
|
|
287
|
+
const accepted = accept(frames.push(decoder.decode(value, { stream: true })));
|
|
288
|
+
if (accepted.length > 0) {
|
|
289
|
+
attempts = 0;
|
|
290
|
+
return { done: false, frames: accepted };
|
|
291
|
+
}
|
|
292
|
+
continue;
|
|
287
293
|
}
|
|
288
|
-
|
|
294
|
+
if (sawTerminal) return { done: true };
|
|
295
|
+
} else if (options.response !== null) {
|
|
296
|
+
return { done: true };
|
|
289
297
|
}
|
|
290
|
-
if (sawTerminal) return { done: true };
|
|
291
298
|
} catch (error) {
|
|
292
299
|
if (signal.aborted) throw error;
|
|
293
300
|
failure = error;
|
|
@@ -7,7 +7,7 @@ import { extractSources } from './thought-utils';
|
|
|
7
7
|
* Pure, and deliberately so: the header's menu item is the only thing that
|
|
8
8
|
* asks for this, and what it hands over is the same `messages` array the
|
|
9
9
|
* screen is streaming into. Everything about DELIVERY — a download on web, the
|
|
10
|
-
* share sheet on a phone — lives in `
|
|
10
|
+
* share sheet on a phone — lives in `lib/markdown-file.ts`, so this module can
|
|
11
11
|
* be tested against a string and nothing else.
|
|
12
12
|
*
|
|
13
13
|
* ## What is kept and what is left out
|
|
@@ -817,6 +817,9 @@ export function useConversationStream({
|
|
|
817
817
|
...appAssertion,
|
|
818
818
|
Accept: 'text/event-stream',
|
|
819
819
|
'Content-Type': 'application/json',
|
|
820
|
+
// This action's id, the one the turn is named by: a request that reaches the server
|
|
821
|
+
// twice (a replay after a token refresh, a network retry) starts the turn once.
|
|
822
|
+
'Idempotency-Key': turnControl.id,
|
|
820
823
|
...(deviceInfo === undefined ? {} : { 'X-Device-Info': JSON.stringify(deviceInfo) }),
|
|
821
824
|
},
|
|
822
825
|
body: JSON.stringify({
|
|
@@ -942,7 +945,13 @@ export function useConversationStream({
|
|
|
942
945
|
throw new Error(errorMessage);
|
|
943
946
|
}
|
|
944
947
|
|
|
945
|
-
|
|
948
|
+
// The server already has this very turn: this request reached it twice, and it ran nothing
|
|
949
|
+
// again (ADR 0049). The answer is the first request's, so it is read by resuming that turn
|
|
950
|
+
// rather than from this response, which carries only its receipt.
|
|
951
|
+
const replayed = response.headers.get('Idempotency-Replayed') === 'true';
|
|
952
|
+
if (replayed) {
|
|
953
|
+
await response.body?.cancel();
|
|
954
|
+
} else if (!response.body) {
|
|
946
955
|
throw new Error('No response received from server');
|
|
947
956
|
}
|
|
948
957
|
setComposerPhase('streaming');
|
|
@@ -967,7 +976,7 @@ export function useConversationStream({
|
|
|
967
976
|
const turnId = turnControl.id;
|
|
968
977
|
const client = linked.client;
|
|
969
978
|
const source = createResumableFrameSource({
|
|
970
|
-
response,
|
|
979
|
+
response: replayed ? null : response,
|
|
971
980
|
signal: controller.signal,
|
|
972
981
|
reconnect: async (cursor, signal) =>
|
|
973
982
|
client.requestAuthenticatedResponse({
|
|
@@ -993,6 +1002,16 @@ export function useConversationStream({
|
|
|
993
1002
|
// Flush any remaining batched content before checking
|
|
994
1003
|
flushPendingUpdates();
|
|
995
1004
|
|
|
1005
|
+
// A turn the server already had, whose stream cannot be followed to its end (a group
|
|
1006
|
+
// room has none to resume). The message did reach the server, so it is kept and offered
|
|
1007
|
+
// no Retry, which would send it a second time.
|
|
1008
|
+
if (replayed && !sawTerminal) {
|
|
1009
|
+
setError(
|
|
1010
|
+
new Error('This message was already received; its answer will appear here.'),
|
|
1011
|
+
);
|
|
1012
|
+
return keepFailedTurn(false);
|
|
1013
|
+
}
|
|
1014
|
+
|
|
996
1015
|
// The server answered with a stand-in, or with nothing usable at
|
|
997
1016
|
// all: the turn stays, with the error under it. (With real output
|
|
998
1017
|
// AND a synthetic tail, this keeps the output and marks the tail.)
|