@gemies/sdk 10.8.0 → 10.9.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 +20 -0
- package/README.md +4 -0
- package/package.json +1 -1
- package/src/components/GemieChatContent.tsx +82 -3
- package/src/components/GemieComposer.tsx +4 -0
- package/src/components/thread/FollowUpStatus.tsx +35 -0
- package/src/components/thread/GemieThread.tsx +25 -1
- package/src/components/thread/ThreadFindBar.tsx +121 -0
- package/src/components/thread/ThreadPanel.tsx +34 -0
- package/src/components/thread/ThreadRow.tsx +44 -9
- package/src/components/thread/ThreadSearch.tsx +61 -78
- package/src/components/thread/ThreadTurns.tsx +94 -0
- package/src/hooks/useFindShortcut.ts +57 -0
- package/src/hooks/useRowJump.ts +79 -0
- package/src/hooks/useThreadNavigation.ts +134 -0
- package/src/hooks/useTimelineWindow.ts +18 -1
- package/src/lib/copy-en.ts +11 -0
- package/src/lib/copy-es.ts +11 -0
- package/src/runtime/thread/thread-find.ts +95 -0
- package/src/runtime/use-approval-actions.ts +7 -1
|
@@ -3,13 +3,13 @@ import type { GemieThreadAddress } from '../../runtime/routes';
|
|
|
3
3
|
import { useThreadSearch } from '../../runtime/use-thread-search';
|
|
4
4
|
import { useLabels } from '../../lib/labels';
|
|
5
5
|
import { Button } from '@oxy.so/bloom/button';
|
|
6
|
-
import { Card, CardBody } from '@oxy.so/bloom/card';
|
|
7
6
|
import { RiCloseLine } from '@oxy.so/bloom/icons/RiCloseLine';
|
|
8
7
|
import { ListItem } from '@oxy.so/bloom/list';
|
|
9
8
|
import { Search } from '@oxy.so/bloom/search';
|
|
10
9
|
import { Muted } from '@oxy.so/bloom/typography';
|
|
11
10
|
import { useState } from 'react';
|
|
12
11
|
import { ScrollView, View, StyleSheet } from 'react-native';
|
|
12
|
+
import { ThreadPanel } from './ThreadPanel';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Searching what was said in this thread, across every conversation in it.
|
|
@@ -24,9 +24,7 @@ import { ScrollView, View, StyleSheet } from 'react-native';
|
|
|
24
24
|
* can read and not reach is a wall, so `onJump` is not an extra: it is what the
|
|
25
25
|
* hit is for.
|
|
26
26
|
*
|
|
27
|
-
* Bloom's `Search` for the field and a `ListItem` per hit, on a `
|
|
28
|
-
* over the top of the thread in its 768 column: the card is the surface, so
|
|
29
|
-
* nothing here paints one of its own.
|
|
27
|
+
* Bloom's `Search` for the field and a `ListItem` per hit, on a `ThreadPanel`.
|
|
30
28
|
*/
|
|
31
29
|
|
|
32
30
|
interface ThreadSearchProps {
|
|
@@ -61,85 +59,70 @@ export const ThreadSearch = ({ thread, onJump, onClose }: ThreadSearchProps) =>
|
|
|
61
59
|
const found = hits ?? [];
|
|
62
60
|
|
|
63
61
|
return (
|
|
64
|
-
<
|
|
65
|
-
<View style={styles.
|
|
66
|
-
<
|
|
67
|
-
<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
accessibilityLabel={t('common.close')}
|
|
86
|
-
onPress={onClose}
|
|
87
|
-
/>
|
|
88
|
-
</View>
|
|
89
|
-
|
|
90
|
-
{!asked ? (
|
|
91
|
-
<View style={styles.hint}>
|
|
92
|
-
<Muted>{t('chat.searchThreadHint')}</Muted>
|
|
93
|
-
</View>
|
|
94
|
-
) : isError ? (
|
|
95
|
-
<View style={styles.hint}>
|
|
96
|
-
<Muted>{t('chat.searchThreadFailed')}</Muted>
|
|
97
|
-
</View>
|
|
98
|
-
) : found.length === 0 ? (
|
|
99
|
-
<View style={styles.hint}>
|
|
100
|
-
<Muted>
|
|
101
|
-
{isFetching ? t('chat.searchThreadSearching') : t('chat.searchThreadEmpty')}
|
|
102
|
-
</Muted>
|
|
103
|
-
</View>
|
|
104
|
-
) : (
|
|
105
|
-
<ScrollView
|
|
106
|
-
// A height in px, not a percentage of a parent that has none:
|
|
107
|
-
// measured in Chromium, the list otherwise sized itself to its
|
|
108
|
-
// content and spilled the results over the thread behind it.
|
|
109
|
-
style={styles.results}
|
|
110
|
-
keyboardShouldPersistTaps="handled"
|
|
111
|
-
>
|
|
112
|
-
{found.map((hit) => (
|
|
113
|
-
<ListItem
|
|
114
|
-
// The cursor, never `messageId`: a message the server wrote
|
|
115
|
-
// has no client id, and `null` is not a key.
|
|
116
|
-
key={hit.cursor}
|
|
117
|
-
title={hit.snippet}
|
|
118
|
-
supporting={`${t(
|
|
119
|
-
hit.role === 'user' ? 'chat.searchThreadYou' : 'chat.searchThreadAgent',
|
|
120
|
-
)} · ${hitDay(hit.createdAt, locale)}`}
|
|
121
|
-
onPress={() => onJump(hit)}
|
|
122
|
-
/>
|
|
123
|
-
))}
|
|
124
|
-
</ScrollView>
|
|
125
|
-
)}
|
|
126
|
-
</CardBody>
|
|
127
|
-
</Card>
|
|
62
|
+
<ThreadPanel>
|
|
63
|
+
<View style={styles.searchRow}>
|
|
64
|
+
<View style={styles.search}>
|
|
65
|
+
<Search
|
|
66
|
+
label={t('chat.searchThreadPlaceholder')}
|
|
67
|
+
value={query}
|
|
68
|
+
onChangeText={setQuery}
|
|
69
|
+
onClearText={() => setQuery('')}
|
|
70
|
+
autoFocus
|
|
71
|
+
returnKeyType="search"
|
|
72
|
+
/>
|
|
73
|
+
</View>
|
|
74
|
+
<Button
|
|
75
|
+
size="md"
|
|
76
|
+
appearance="plain"
|
|
77
|
+
tone="neutral"
|
|
78
|
+
iconOnly
|
|
79
|
+
leadingIcon={RiCloseLine}
|
|
80
|
+
accessibilityLabel={t('common.close')}
|
|
81
|
+
onPress={onClose}
|
|
82
|
+
/>
|
|
128
83
|
</View>
|
|
129
|
-
|
|
84
|
+
|
|
85
|
+
{!asked ? (
|
|
86
|
+
<View style={styles.hint}>
|
|
87
|
+
<Muted>{t('chat.searchThreadHint')}</Muted>
|
|
88
|
+
</View>
|
|
89
|
+
) : isError ? (
|
|
90
|
+
<View style={styles.hint}>
|
|
91
|
+
<Muted>{t('chat.searchThreadFailed')}</Muted>
|
|
92
|
+
</View>
|
|
93
|
+
) : found.length === 0 ? (
|
|
94
|
+
<View style={styles.hint}>
|
|
95
|
+
<Muted>
|
|
96
|
+
{isFetching ? t('chat.searchThreadSearching') : t('chat.searchThreadEmpty')}
|
|
97
|
+
</Muted>
|
|
98
|
+
</View>
|
|
99
|
+
) : (
|
|
100
|
+
<ScrollView
|
|
101
|
+
// A height in px, not a percentage of a parent that has none:
|
|
102
|
+
// measured in Chromium, the list otherwise sized itself to its
|
|
103
|
+
// content and spilled the results over the thread behind it.
|
|
104
|
+
style={styles.results}
|
|
105
|
+
keyboardShouldPersistTaps="handled"
|
|
106
|
+
>
|
|
107
|
+
{found.map((hit) => (
|
|
108
|
+
<ListItem
|
|
109
|
+
// The cursor, never `messageId`: a message the server wrote
|
|
110
|
+
// has no client id, and `null` is not a key.
|
|
111
|
+
key={hit.cursor}
|
|
112
|
+
title={hit.snippet}
|
|
113
|
+
supporting={`${t(
|
|
114
|
+
hit.role === 'user' ? 'chat.searchThreadYou' : 'chat.searchThreadAgent',
|
|
115
|
+
)} · ${hitDay(hit.createdAt, locale)}`}
|
|
116
|
+
onPress={() => onJump(hit)}
|
|
117
|
+
/>
|
|
118
|
+
))}
|
|
119
|
+
</ScrollView>
|
|
120
|
+
)}
|
|
121
|
+
</ThreadPanel>
|
|
130
122
|
);
|
|
131
123
|
};
|
|
132
124
|
|
|
133
125
|
const styles = StyleSheet.create({
|
|
134
|
-
overlay: {
|
|
135
|
-
position: 'absolute',
|
|
136
|
-
insetInline: 0,
|
|
137
|
-
top: 0,
|
|
138
|
-
zIndex: 20,
|
|
139
|
-
paddingHorizontal: 16,
|
|
140
|
-
paddingTop: 16,
|
|
141
|
-
},
|
|
142
|
-
column: { width: '100%', maxWidth: 768, alignSelf: 'center' },
|
|
143
126
|
searchRow: { flexDirection: 'row', alignItems: 'center', gap: 8 },
|
|
144
127
|
search: { flexGrow: 1, flexShrink: 1, flexBasis: '0%' },
|
|
145
128
|
hint: { paddingHorizontal: 4, paddingVertical: 16 },
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useAiChatChromeInsets } from '@oxy.so/bloom/ai-chat';
|
|
2
|
+
import { Button } from '@oxy.so/bloom/button';
|
|
3
|
+
import { RiCloseLine } from '@oxy.so/bloom/icons/RiCloseLine';
|
|
4
|
+
import { RiListUnordered } from '@oxy.so/bloom/icons/RiListUnordered';
|
|
5
|
+
import { ListItem } from '@oxy.so/bloom/list';
|
|
6
|
+
import { Muted } from '@oxy.so/bloom/typography';
|
|
7
|
+
import { ScrollView, StyleSheet, View } from 'react-native';
|
|
8
|
+
import { useLabels } from '../../lib/labels';
|
|
9
|
+
import type { ThreadTurn } from '../../runtime/thread/thread-find';
|
|
10
|
+
import { ThreadPanel } from './ThreadPanel';
|
|
11
|
+
|
|
12
|
+
/** From how many turns a thread offers its list of them without being asked for it. */
|
|
13
|
+
export const LONG_THREAD_TURNS = 6;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The thread's turns, newest first, each named by the question that opened it: a press lands there.
|
|
17
|
+
*
|
|
18
|
+
* A list rather than a drawn minimap: the thread mounts only a window of its rows, so there are no
|
|
19
|
+
* heights to draw the rest from, and a map built on guesses would land in the wrong place.
|
|
20
|
+
*/
|
|
21
|
+
export function ThreadTurnList({
|
|
22
|
+
turns,
|
|
23
|
+
onJump,
|
|
24
|
+
onLoadEarlier,
|
|
25
|
+
onClose,
|
|
26
|
+
}: {
|
|
27
|
+
turns: readonly ThreadTurn[];
|
|
28
|
+
onJump: (messageId: string) => void;
|
|
29
|
+
/** Page in the history above, whose turns then join the list. Absent where there is none. */
|
|
30
|
+
onLoadEarlier?: () => void;
|
|
31
|
+
onClose: () => void;
|
|
32
|
+
}) {
|
|
33
|
+
const { t } = useLabels();
|
|
34
|
+
return (
|
|
35
|
+
<ThreadPanel>
|
|
36
|
+
<View style={styles.header}>
|
|
37
|
+
<View style={styles.title}>
|
|
38
|
+
<Muted>{t('chat.turns.title')}</Muted>
|
|
39
|
+
</View>
|
|
40
|
+
<Button
|
|
41
|
+
size="sm"
|
|
42
|
+
appearance="plain"
|
|
43
|
+
tone="neutral"
|
|
44
|
+
iconOnly
|
|
45
|
+
leadingIcon={RiCloseLine}
|
|
46
|
+
accessibilityLabel={t('common.close')}
|
|
47
|
+
onPress={onClose}
|
|
48
|
+
/>
|
|
49
|
+
</View>
|
|
50
|
+
<ScrollView style={styles.turns} keyboardShouldPersistTaps="handled">
|
|
51
|
+
{[...turns].reverse().map((turn) => (
|
|
52
|
+
<ListItem
|
|
53
|
+
key={turn.id}
|
|
54
|
+
title={turn.preview || t('chat.turns.attachment')}
|
|
55
|
+
onPress={() => onJump(turn.id)}
|
|
56
|
+
/>
|
|
57
|
+
))}
|
|
58
|
+
{onLoadEarlier === undefined ? null : (
|
|
59
|
+
<Button appearance="plain" tone="neutral" size="sm" onPress={onLoadEarlier}>
|
|
60
|
+
{t('chat.turns.loadEarlier')}
|
|
61
|
+
</Button>
|
|
62
|
+
)}
|
|
63
|
+
</ScrollView>
|
|
64
|
+
</ThreadPanel>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The way to the list from a long thread: a button at its edge, under the header. */
|
|
69
|
+
export function ThreadTurnsButton({ onPress }: { onPress: () => void }) {
|
|
70
|
+
const { t } = useLabels();
|
|
71
|
+
// The header floats over the transcript, so the edge starts under it.
|
|
72
|
+
const insets = useAiChatChromeInsets();
|
|
73
|
+
return (
|
|
74
|
+
<View style={[styles.edge, { top: (insets?.top ?? 0) + 8 }]} pointerEvents="box-none">
|
|
75
|
+
<Button
|
|
76
|
+
size="sm"
|
|
77
|
+
appearance="outline"
|
|
78
|
+
tone="neutral"
|
|
79
|
+
iconOnly
|
|
80
|
+
leadingIcon={RiListUnordered}
|
|
81
|
+
accessibilityLabel={t('chat.turns.title')}
|
|
82
|
+
onPress={onPress}
|
|
83
|
+
/>
|
|
84
|
+
</View>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const styles = StyleSheet.create({
|
|
89
|
+
header: { flexDirection: 'row', alignItems: 'center', gap: 8 },
|
|
90
|
+
title: { flexGrow: 1, flexShrink: 1, flexBasis: '0%', paddingHorizontal: 4 },
|
|
91
|
+
// A height in px, not a percentage of a parent that has none (see `ThreadSearch`).
|
|
92
|
+
turns: { marginTop: 8, maxHeight: 384 },
|
|
93
|
+
edge: { position: 'absolute', insetInlineEnd: 12, zIndex: 10 },
|
|
94
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
import type { ShortcutKeyEvent } from './useApprovalShortcuts';
|
|
4
|
+
|
|
5
|
+
/** Where keys are heard. A test brings its own. */
|
|
6
|
+
export type ShortcutTarget = Pick<EventTarget, 'addEventListener' | 'removeEventListener'>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Every chat on screen that offers find, the newest last: a sheet over a screen has two, and only
|
|
10
|
+
* the one on top answers.
|
|
11
|
+
*/
|
|
12
|
+
const owners: object[] = [];
|
|
13
|
+
|
|
14
|
+
/** ⌘F on a Mac, Ctrl+F elsewhere. Ctrl+F on a Mac moves the caret and stays the field's. */
|
|
15
|
+
export function isFindShortcut(event: ShortcutKeyEvent, mac: boolean): boolean {
|
|
16
|
+
if (event.defaultPrevented || event.isComposing || event.altKey || event.shiftKey) return false;
|
|
17
|
+
const modifier = mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
|
|
18
|
+
return modifier === true && event.key.toLowerCase() === 'f';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isMac(): boolean {
|
|
22
|
+
const platform = (globalThis as { navigator?: { platform?: string } }).navigator?.platform;
|
|
23
|
+
return /mac|iphone|ipad/i.test(platform ?? '');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The web shortcut for find in the thread. The browser's own find cannot see the rows above the
|
|
28
|
+
* thread's mounted window, so while a chat offers find it takes the key; `undefined` leaves the
|
|
29
|
+
* key to the browser. Heard in the capture phase, because a text field stops its keys bubbling.
|
|
30
|
+
*/
|
|
31
|
+
export function useFindShortcut(
|
|
32
|
+
open: (() => void) | undefined,
|
|
33
|
+
target: ShortcutTarget | undefined = Platform.OS === 'web'
|
|
34
|
+
? (globalThis as { window?: Window }).window
|
|
35
|
+
: undefined,
|
|
36
|
+
): void {
|
|
37
|
+
const latest = useRef(open);
|
|
38
|
+
latest.current = open;
|
|
39
|
+
const enabled = open !== undefined;
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!enabled || target === undefined) return;
|
|
43
|
+
const owner = {};
|
|
44
|
+
owners.push(owner);
|
|
45
|
+
const onKeyDown = (event: Event) => {
|
|
46
|
+
const key = event as unknown as ShortcutKeyEvent;
|
|
47
|
+
if (owners.at(-1) !== owner || !isFindShortcut(key, isMac())) return;
|
|
48
|
+
key.preventDefault?.();
|
|
49
|
+
latest.current?.();
|
|
50
|
+
};
|
|
51
|
+
target.addEventListener('keydown', onKeyDown, true);
|
|
52
|
+
return () => {
|
|
53
|
+
owners.splice(owners.indexOf(owner), 1);
|
|
54
|
+
target.removeEventListener('keydown', onKeyDown, true);
|
|
55
|
+
};
|
|
56
|
+
}, [enabled, target]);
|
|
57
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { AiChatThreadHandle } from '@oxy.so/bloom/ai-chat';
|
|
2
|
+
import { type ComponentRef, type RefObject, useCallback, useLayoutEffect, useRef } from 'react';
|
|
3
|
+
import type { View } from 'react-native';
|
|
4
|
+
import type { ThreadJumpTarget } from './useThreadNavigation';
|
|
5
|
+
import { nextFrame } from './useTimelineWindow';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A mounted view: a row of the thread, or the list that holds them. An interface rather than an
|
|
9
|
+
* alias, so a declaration that returns one can name it (see `TextInputHandle`).
|
|
10
|
+
*/
|
|
11
|
+
export interface RowView extends ComponentRef<typeof View> {}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The room the find bar takes at the top of the thread, px: its card at its tallest, with the
|
|
15
|
+
* line about unloaded history. The thread keeps that much clear while it marks a match.
|
|
16
|
+
*/
|
|
17
|
+
export const FIND_BAR_CLEARANCE = 104;
|
|
18
|
+
|
|
19
|
+
interface Options {
|
|
20
|
+
/** The row asked for, if any. A new `request` asks again. */
|
|
21
|
+
target: ThreadJumpTarget | null | undefined;
|
|
22
|
+
/** Every row's id, in reading order. */
|
|
23
|
+
ids: readonly string[];
|
|
24
|
+
/** Mount the row at this index (`useTimelineWindow.reveal`). */
|
|
25
|
+
reveal: (index: number) => void;
|
|
26
|
+
threadRef: RefObject<AiChatThreadHandle | null>;
|
|
27
|
+
/** Room to leave above the row, px: what floats over the top of the thread. */
|
|
28
|
+
clearance: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Bring a row into view.
|
|
33
|
+
*
|
|
34
|
+
* Bloom's thread scrolls to an offset, not to an index, so the row is first mounted (it may be
|
|
35
|
+
* above the window) and then measured against the list. It is measured when the jump is made
|
|
36
|
+
* rather than remembered from its layout: a row's position changes whenever anything above it
|
|
37
|
+
* does, and on web no layout event says so.
|
|
38
|
+
*/
|
|
39
|
+
export function useRowJump({ target, ids, reveal, threadRef, clearance }: Options) {
|
|
40
|
+
const rows = useRef(new Map<string, RowView>());
|
|
41
|
+
const listRef = useRef<RowView | null>(null);
|
|
42
|
+
const waiting = useRef<string | null>(null);
|
|
43
|
+
const latest = useRef({ ids, reveal });
|
|
44
|
+
useLayoutEffect(() => {
|
|
45
|
+
latest.current = { ids, reveal };
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const registerRow = useCallback((id: string, node: RowView | null) => {
|
|
49
|
+
if (node === null) rows.current.delete(id);
|
|
50
|
+
else rows.current.set(id, node);
|
|
51
|
+
}, []);
|
|
52
|
+
|
|
53
|
+
const request = target?.request;
|
|
54
|
+
const targetId = target?.id;
|
|
55
|
+
useLayoutEffect(() => {
|
|
56
|
+
if (request === undefined || targetId === undefined) return;
|
|
57
|
+
const index = latest.current.ids.indexOf(targetId);
|
|
58
|
+
if (index === -1) return;
|
|
59
|
+
waiting.current = targetId;
|
|
60
|
+
latest.current.reveal(index);
|
|
61
|
+
}, [request, targetId]);
|
|
62
|
+
|
|
63
|
+
// After every commit, because the row asked for may mount a render after it was asked for.
|
|
64
|
+
useLayoutEffect(() => {
|
|
65
|
+
const id = waiting.current;
|
|
66
|
+
const row = id === null ? undefined : rows.current.get(id);
|
|
67
|
+
const list = listRef.current;
|
|
68
|
+
if (row === undefined || list === null) return;
|
|
69
|
+
waiting.current = null;
|
|
70
|
+
// A frame later, once the rows revealed with it have laid out.
|
|
71
|
+
nextFrame(() => {
|
|
72
|
+
row.measureLayout(list, (_x, y) => {
|
|
73
|
+
threadRef.current?.scrollToOffset({ offset: Math.max(0, y - clearance) });
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return { listRef, registerRow };
|
|
79
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { findMatches, stepMatch, threadTurns } from '../runtime/thread/thread-find';
|
|
3
|
+
import type { Message } from '../runtime/types';
|
|
4
|
+
|
|
5
|
+
/** Ask the thread to bring a row into view. `request` tells two asks for the same row apart. */
|
|
6
|
+
export interface ThreadJumpTarget {
|
|
7
|
+
id: string;
|
|
8
|
+
request: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface NavigationState {
|
|
12
|
+
panel: 'find' | 'turns' | null;
|
|
13
|
+
query: string;
|
|
14
|
+
/** The match the reader is on, by message id: ids survive the rows changing around it. */
|
|
15
|
+
activeId: string | null;
|
|
16
|
+
jump: ThreadJumpTarget | null;
|
|
17
|
+
/** Bumped each time find is asked for, so an open bar takes the caret back. */
|
|
18
|
+
focusRequest: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const CLOSED: NavigationState = {
|
|
22
|
+
panel: null,
|
|
23
|
+
query: '',
|
|
24
|
+
activeId: null,
|
|
25
|
+
jump: null,
|
|
26
|
+
focusRequest: 0,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const NOTHING: readonly string[] = [];
|
|
30
|
+
|
|
31
|
+
const jumpTo = (current: NavigationState, id: string): ThreadJumpTarget => ({
|
|
32
|
+
id,
|
|
33
|
+
request: (current.jump?.request ?? 0) + 1,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Moving around a long thread: the find bar's query and current match, the list of turns, and the
|
|
38
|
+
* row either of them asks the thread to show.
|
|
39
|
+
*
|
|
40
|
+
* Matches are numbered in reading order and a search starts on the newest, which is where the
|
|
41
|
+
* reader already is. Only something the person does moves the thread: a reply streaming in can
|
|
42
|
+
* add a match, and never scrolls to it.
|
|
43
|
+
*/
|
|
44
|
+
export function useThreadNavigation(rows: readonly Message[], conversationId: string | undefined) {
|
|
45
|
+
const [state, setState] = useState<NavigationState & { owner: string | undefined }>({
|
|
46
|
+
...CLOSED,
|
|
47
|
+
owner: conversationId,
|
|
48
|
+
});
|
|
49
|
+
// Another conversation has other words: nothing found or listed carries over.
|
|
50
|
+
if (state.owner !== conversationId) setState({ ...CLOSED, owner: conversationId });
|
|
51
|
+
|
|
52
|
+
// Read through a ref by the actions, which must not change identity per streamed token.
|
|
53
|
+
const latestRows = useRef(rows);
|
|
54
|
+
latestRows.current = rows;
|
|
55
|
+
|
|
56
|
+
const finding = state.panel === 'find';
|
|
57
|
+
const matches = useMemo(
|
|
58
|
+
() => (finding ? findMatches(rows, state.query) : NOTHING),
|
|
59
|
+
[finding, rows, state.query],
|
|
60
|
+
);
|
|
61
|
+
const found = state.activeId === null ? -1 : matches.indexOf(state.activeId);
|
|
62
|
+
// A match that is gone (the reply was edited or regenerated) hands over to the newest.
|
|
63
|
+
const current = found === -1 ? matches.length - 1 : found;
|
|
64
|
+
|
|
65
|
+
const turns = useMemo(
|
|
66
|
+
() => (state.panel === 'turns' ? threadTurns(rows) : []),
|
|
67
|
+
[state.panel, rows],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const openFind = useCallback(() => {
|
|
71
|
+
setState((s) => ({
|
|
72
|
+
...(s.panel === 'find' ? s : { ...CLOSED, owner: s.owner, jump: s.jump }),
|
|
73
|
+
panel: 'find',
|
|
74
|
+
focusRequest: s.focusRequest + 1,
|
|
75
|
+
}));
|
|
76
|
+
}, []);
|
|
77
|
+
const openTurns = useCallback(() => {
|
|
78
|
+
setState((s) => ({ ...CLOSED, owner: s.owner, jump: s.jump, panel: 'turns' }));
|
|
79
|
+
}, []);
|
|
80
|
+
const close = useCallback(() => {
|
|
81
|
+
setState((s) =>
|
|
82
|
+
s.panel === null ? s : { ...CLOSED, owner: s.owner, jump: s.jump, panel: null },
|
|
83
|
+
);
|
|
84
|
+
}, []);
|
|
85
|
+
|
|
86
|
+
const setQuery = useCallback((query: string) => {
|
|
87
|
+
const newest = findMatches(latestRows.current, query).at(-1) ?? null;
|
|
88
|
+
setState((s) => ({
|
|
89
|
+
...s,
|
|
90
|
+
query,
|
|
91
|
+
activeId: newest,
|
|
92
|
+
jump: newest === null ? s.jump : jumpTo(s, newest),
|
|
93
|
+
}));
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
96
|
+
const step = useCallback((delta: 1 | -1) => {
|
|
97
|
+
setState((s) => {
|
|
98
|
+
const ids = findMatches(latestRows.current, s.query);
|
|
99
|
+
const at = s.activeId === null ? -1 : ids.indexOf(s.activeId);
|
|
100
|
+
const next = ids[stepMatch(at === -1 ? ids.length - 1 : at, delta, ids.length)];
|
|
101
|
+
return next === undefined ? s : { ...s, activeId: next, jump: jumpTo(s, next) };
|
|
102
|
+
});
|
|
103
|
+
}, []);
|
|
104
|
+
const older = useCallback(() => step(-1), [step]);
|
|
105
|
+
const newer = useCallback(() => step(1), [step]);
|
|
106
|
+
|
|
107
|
+
/** Land on a turn from the list, and close the list over it. */
|
|
108
|
+
const jumpToTurn = useCallback((id: string) => {
|
|
109
|
+
setState((s) => ({ ...CLOSED, owner: s.owner, panel: null, jump: jumpTo(s, id) }));
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
panel: state.panel,
|
|
114
|
+
query: state.query,
|
|
115
|
+
/** How many loaded messages hold the query. */
|
|
116
|
+
total: matches.length,
|
|
117
|
+
/** The current match's position, counted from 0 in reading order; -1 with none. */
|
|
118
|
+
current,
|
|
119
|
+
/** The message the thread marks: the current match, while find is open. */
|
|
120
|
+
highlightedId: matches[current] ?? null,
|
|
121
|
+
jump: state.jump,
|
|
122
|
+
focusRequest: state.focusRequest,
|
|
123
|
+
turns,
|
|
124
|
+
openFind,
|
|
125
|
+
openTurns,
|
|
126
|
+
close,
|
|
127
|
+
setQuery,
|
|
128
|
+
older,
|
|
129
|
+
newer,
|
|
130
|
+
jumpToTurn,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export type ThreadNavigation = ReturnType<typeof useThreadNavigation>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type Message,
|
|
3
3
|
TAIL,
|
|
4
|
+
FOCUS_MARGIN,
|
|
4
5
|
TOP,
|
|
5
6
|
type TimelineAnchor,
|
|
6
7
|
type TimelinePosition,
|
|
@@ -79,7 +80,7 @@ function awaitingRows(pending: { current: PendingScroll | null }, ids: readonly
|
|
|
79
80
|
/** The state before the first key: nothing mounted yet, so nothing to remember. */
|
|
80
81
|
const MOUNTING = Symbol('mounting');
|
|
81
82
|
|
|
82
|
-
const nextFrame = (run: () => void): void => {
|
|
83
|
+
export const nextFrame = (run: () => void): void => {
|
|
83
84
|
if (typeof requestAnimationFrame === 'function') requestAnimationFrame(run);
|
|
84
85
|
else setTimeout(run, 0);
|
|
85
86
|
};
|
|
@@ -205,6 +206,21 @@ export function useTimelineWindow({
|
|
|
205
206
|
loadHistory?.();
|
|
206
207
|
}, [setAnchor]);
|
|
207
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Mount the row at `index` for a jump to it, with a few rows above, and keep it mounted. A
|
|
211
|
+
* scroll still waiting (a restore) gives way to it.
|
|
212
|
+
*/
|
|
213
|
+
const reveal = useCallback(
|
|
214
|
+
(index: number) => {
|
|
215
|
+
pending.current = null;
|
|
216
|
+
atBottom.current = false;
|
|
217
|
+
const { ids: rows, start: first } = latest.current;
|
|
218
|
+
const wanted = Math.max(0, index - FOCUS_MARGIN);
|
|
219
|
+
if (wanted < first) setAnchor(pinAnchor(rows, wanted));
|
|
220
|
+
},
|
|
221
|
+
[setAnchor],
|
|
222
|
+
);
|
|
223
|
+
|
|
208
224
|
const handleScroll = useCallback(
|
|
209
225
|
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
210
226
|
const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
|
|
@@ -248,6 +264,7 @@ export function useTimelineWindow({
|
|
|
248
264
|
return {
|
|
249
265
|
/** Index of the first mounted row. */
|
|
250
266
|
start,
|
|
267
|
+
reveal,
|
|
251
268
|
/** For `AiChatThread`: absent when there is nothing above to reveal or load. */
|
|
252
269
|
onStartReached: start > 0 || onLoadHistory !== undefined ? handleStartReached : undefined,
|
|
253
270
|
onScroll: handleScroll,
|
package/src/lib/copy-en.ts
CHANGED
|
@@ -112,6 +112,14 @@ export const COPY_EN: Readonly<Record<string, CopyEntry>> = {
|
|
|
112
112
|
'chat.fileChanges.unified': 'Unified',
|
|
113
113
|
'chat.fileChanges.unrecorded': 'The agent did not record what changed in this file',
|
|
114
114
|
'chat.fileChanges.wrap': 'Wrap lines',
|
|
115
|
+
'chat.find.count': '{{current}} of {{total}}',
|
|
116
|
+
'chat.find.loadedOnly': 'Only the messages loaded here are searched.',
|
|
117
|
+
'chat.find.newer': 'Next match',
|
|
118
|
+
'chat.find.none': 'No matches',
|
|
119
|
+
'chat.find.older': 'Previous match',
|
|
120
|
+
'chat.find.placeholder': 'Find in conversation',
|
|
121
|
+
'chat.followUp.queued': 'Waiting to send your message…',
|
|
122
|
+
'chat.followUp.steering': 'Sending your reply to {{agent}}…',
|
|
115
123
|
'chat.like': 'Like',
|
|
116
124
|
'chat.actionApproval.send': 'Send',
|
|
117
125
|
'chat.actionApproval.approve': 'Approve',
|
|
@@ -190,6 +198,9 @@ export const COPY_EN: Readonly<Record<string, CopyEntry>> = {
|
|
|
190
198
|
'chat.turnFailed': "Alia couldn't answer right now.",
|
|
191
199
|
'chat.turnFailedRetryHint': 'Try again.',
|
|
192
200
|
'chat.turnInterrupted': 'Alia was interrupted before finishing.',
|
|
201
|
+
'chat.turns.attachment': 'Attachment',
|
|
202
|
+
'chat.turns.loadEarlier': 'Load earlier messages',
|
|
203
|
+
'chat.turns.title': 'Jump to a turn',
|
|
193
204
|
'chat.working': 'Working',
|
|
194
205
|
'chat.yesterday': 'Yesterday',
|
|
195
206
|
'common.close': 'Close',
|
package/src/lib/copy-es.ts
CHANGED
|
@@ -111,6 +111,14 @@ export const COPY_ES: Readonly<Record<string, CopyEntry>> = {
|
|
|
111
111
|
'chat.fileChanges.unified': 'Unificada',
|
|
112
112
|
'chat.fileChanges.unrecorded': 'El agente no registró qué cambió en este archivo',
|
|
113
113
|
'chat.fileChanges.wrap': 'Ajustar líneas',
|
|
114
|
+
'chat.find.count': '{{current}} de {{total}}',
|
|
115
|
+
'chat.find.loadedOnly': 'Solo se busca en los mensajes cargados aquí.',
|
|
116
|
+
'chat.find.newer': 'Coincidencia siguiente',
|
|
117
|
+
'chat.find.none': 'Sin coincidencias',
|
|
118
|
+
'chat.find.older': 'Coincidencia anterior',
|
|
119
|
+
'chat.find.placeholder': 'Buscar en la conversación',
|
|
120
|
+
'chat.followUp.queued': 'Esperando para enviar tu mensaje…',
|
|
121
|
+
'chat.followUp.steering': 'Enviando tu respuesta a {{agent}}…',
|
|
114
122
|
'chat.like': 'Me gusta',
|
|
115
123
|
'chat.actionApproval.send': 'Enviar',
|
|
116
124
|
'chat.actionApproval.approve': 'Aprobar',
|
|
@@ -189,6 +197,9 @@ export const COPY_ES: Readonly<Record<string, CopyEntry>> = {
|
|
|
189
197
|
'chat.turnFailed': 'Alia no ha podido responder ahora mismo.',
|
|
190
198
|
'chat.turnFailedRetryHint': 'Inténtalo de nuevo.',
|
|
191
199
|
'chat.turnInterrupted': 'Alia se interrumpió antes de terminar.',
|
|
200
|
+
'chat.turns.attachment': 'Adjunto',
|
|
201
|
+
'chat.turns.loadEarlier': 'Cargar mensajes anteriores',
|
|
202
|
+
'chat.turns.title': 'Ir a un turno',
|
|
192
203
|
'chat.working': 'Trabajando',
|
|
193
204
|
'chat.yesterday': 'Ayer',
|
|
194
205
|
'common.close': 'Cerrar',
|