@gemies/sdk 0.0.0-stage → 1.0.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 +1662 -0
- package/LICENSE +202 -0
- package/NOTICE +15 -0
- package/README.md +516 -2
- package/package.json +102 -4
- package/src/animations.ts +5 -0
- package/src/ask-eager.ts +8 -0
- package/src/ask-text.ts +11 -0
- package/src/ask.ts +12 -0
- package/src/avatar.ts +13 -0
- package/src/character.ts +1 -0
- package/src/components/AmbientField.tsx +580 -0
- package/src/components/ComposerDropZone.tsx +235 -0
- package/src/components/ComposerSuggestions.tsx +45 -0
- package/src/components/GemieAskCore.tsx +184 -0
- package/src/components/GemieAskProvider.tsx +18 -0
- package/src/components/GemieAskProviderLazy.tsx +27 -0
- package/src/components/GemieAskProviderText.tsx +25 -0
- package/src/components/GemieAskSheet.tsx +51 -0
- package/src/components/GemieAskSheetWithVoice.tsx +10 -0
- package/src/components/GemieAvatar.tsx +57 -0
- package/src/components/GemieAvatarIcon.tsx +35 -0
- package/src/components/GemieChatContent.tsx +900 -0
- package/src/components/GemieChatHeader.tsx +123 -0
- package/src/components/GemieChatScreen.tsx +170 -0
- package/src/components/GemieChatSheet.tsx +368 -0
- package/src/components/GemieChatWithVoice.tsx +13 -0
- package/src/components/GemieComposer.tsx +388 -0
- package/src/components/GemieGroupWelcome.tsx +37 -0
- package/src/components/GemieParticipants.tsx +133 -0
- package/src/components/GemieParticipantsHeader.tsx +27 -0
- package/src/components/GemieRecentConversations.tsx +65 -0
- package/src/components/GemieSharedConversation.tsx +87 -0
- package/src/components/GemieWelcomeMessage.tsx +58 -0
- package/src/components/IdentityMark.tsx +225 -0
- package/src/components/Image.tsx +12 -0
- package/src/components/InertWhen.tsx +48 -0
- package/src/components/PlanPreviewCard.tsx +53 -0
- package/src/components/QuestionCard.tsx +64 -0
- package/src/components/ReplyMarkdown.tsx +373 -0
- package/src/components/RichBlocks.tsx +195 -0
- package/src/components/VoiceModeIcon.tsx +18 -0
- package/src/components/canvas/CanvasComponent.tsx +65 -0
- package/src/components/canvas/ChartRenderer.tsx +73 -0
- package/src/components/canvas/CodeRenderer.tsx +20 -0
- package/src/components/canvas/DashboardRenderer.tsx +70 -0
- package/src/components/canvas/DeckRenderer.tsx +43 -0
- package/src/components/canvas/FormRenderer.tsx +101 -0
- package/src/components/canvas/MarkdownRenderer.tsx +14 -0
- package/src/components/canvas/TableRenderer.tsx +47 -0
- package/src/components/icons/RiEmojiPicker.tsx +39 -0
- package/src/components/icons/RiMoreDots.tsx +32 -0
- package/src/components/icons/RiReplyLine.tsx +25 -0
- package/src/components/thought/FilesAndSources.tsx +120 -0
- package/src/components/thought/ThoughtPanel.tsx +426 -0
- package/src/components/thought/ToolStep.tsx +134 -0
- package/src/components/thread/AgentExchangeView.tsx +112 -0
- package/src/components/thread/AgentResultCard.tsx +125 -0
- package/src/components/thread/AgentTaskCard.tsx +127 -0
- package/src/components/thread/ChatBubble.tsx +256 -0
- package/src/components/thread/GemieThread.tsx +455 -0
- package/src/components/thread/MessageBlockBoundary.tsx +78 -0
- package/src/components/thread/NewConversationOffer.tsx +54 -0
- package/src/components/thread/ThreadRow.tsx +598 -0
- package/src/components/thread/ThreadSearch.tsx +130 -0
- package/src/components/thread/ThreadSkeleton.tsx +30 -0
- package/src/components/thread/ToolResultCard.tsx +254 -0
- package/src/components/thread/TurnMenu.tsx +54 -0
- package/src/components/thread/TurnStatusLine.tsx +34 -0
- package/src/components/voice/GemieVoiceCall.tsx +21 -0
- package/src/components/voice/GemieVoiceCall.web.tsx +17 -0
- package/src/components/voice/VoiceCallContext.ts +13 -0
- package/src/components/voice/VoiceControls.tsx +106 -0
- package/src/components/voice/VoiceSession.tsx +120 -0
- package/src/components/voice/withVoiceCall.tsx +25 -0
- package/src/csp.ts +50 -0
- package/src/dictation.ts +40 -0
- package/src/hooks/useAmbientWave.ts +184 -0
- package/src/hooks/useAtBottom.ts +51 -0
- package/src/hooks/useAttachMenu.ts +93 -0
- package/src/hooks/useAttachmentIntake.ts +401 -0
- package/src/hooks/useAudioLevelMonitor.ts +46 -0
- package/src/hooks/useAudioLevels.ts +51 -0
- package/src/hooks/useChatComposer.ts +309 -0
- package/src/hooks/useChatVoice.ts +129 -0
- package/src/hooks/useComposerSuggestions.ts +84 -0
- package/src/hooks/useDictation.ts +292 -0
- package/src/hooks/useDocumentPicker.ts +11 -0
- package/src/hooks/useFailedTurnToast.ts +43 -0
- package/src/hooks/useFocusWhenPresented.ts +41 -0
- package/src/hooks/useGemieActivity.ts +31 -0
- package/src/hooks/useImagePicker.ts +38 -0
- package/src/hooks/useMessageReactions.ts +119 -0
- package/src/hooks/useMessageVote.ts +50 -0
- package/src/hooks/useOpenThought.ts +79 -0
- package/src/hooks/usePendingQuestions.ts +19 -0
- package/src/hooks/useReadAloud.ts +92 -0
- package/src/hooks/useShareHandler.ts +24 -0
- package/src/hooks/useSoundEffects.ts +137 -0
- package/src/hooks/useTTS.ts +303 -0
- package/src/hooks/useThoughtScope.ts +59 -0
- package/src/hooks/useThreadRows.ts +157 -0
- package/src/hooks/useThreadView.ts +67 -0
- package/src/hooks/useTimelineWindow.ts +256 -0
- package/src/hooks/useVoiceLauncher.ts +180 -0
- package/src/hooks/useVoiceRoom.ts +212 -0
- package/src/index.ts +140 -0
- package/src/lib/activity-socket.ts +102 -0
- package/src/lib/activity-store.ts +53 -0
- package/src/lib/add-menu.ts +7 -0
- package/src/lib/agent-character.ts +28 -0
- package/src/lib/alia-character.ts +14 -0
- package/src/lib/attachment-intake.ts +154 -0
- package/src/lib/attachment-tiles.ts +74 -0
- package/src/lib/audio-level.ts +130 -0
- package/src/lib/audio-request-auth.ts +4 -0
- package/src/lib/audio-session-binding.ts +27 -0
- package/src/lib/bubbles.tsx +16 -0
- package/src/lib/call-thread.ts +33 -0
- package/src/lib/canvas-result.ts +21 -0
- package/src/lib/catalogue-tool.ts +22 -0
- package/src/lib/citations.ts +192 -0
- package/src/lib/clipboard.ts +23 -0
- package/src/lib/content-utils.ts +46 -0
- package/src/lib/copy-en.ts +291 -0
- package/src/lib/copy-es.ts +287 -0
- package/src/lib/copy.ts +38 -0
- package/src/lib/deferred/document-picker.ts +2 -0
- package/src/lib/deferred/document-picker.web.ts +12 -0
- package/src/lib/deferred/expo-audio.ts +2 -0
- package/src/lib/deferred/expo-audio.web.ts +12 -0
- package/src/lib/deferred/image-data-url.ts +5 -0
- package/src/lib/deferred/image-data-url.web.ts +12 -0
- package/src/lib/dictation-protocol.ts +142 -0
- package/src/lib/dictation-recorder-types.ts +37 -0
- package/src/lib/dictation-recorder.native.ts +90 -0
- package/src/lib/dictation-recorder.ts +131 -0
- package/src/lib/dictation-stream.ts +238 -0
- package/src/lib/dictation.ts +216 -0
- package/src/lib/document-picker.ts +31 -0
- package/src/lib/domain.ts +12 -0
- package/src/lib/flights.ts +33 -0
- package/src/lib/focus-return.ts +66 -0
- package/src/lib/greeting.ts +15 -0
- package/src/lib/image-data-url.ts +35 -0
- package/src/lib/keyboard.native.tsx +25 -0
- package/src/lib/keyboard.tsx +55 -0
- package/src/lib/labels-es.ts +113 -0
- package/src/lib/labels.tsx +207 -0
- package/src/lib/origins.ts +28 -0
- package/src/lib/participants.ts +97 -0
- package/src/lib/pcm.ts +57 -0
- package/src/lib/power-level-options.ts +66 -0
- package/src/lib/questions.ts +92 -0
- package/src/lib/speech-language.ts +20 -0
- package/src/lib/speech-messages.ts +54 -0
- package/src/lib/speech-synthesis.ts +156 -0
- package/src/lib/suggestions.ts +53 -0
- package/src/lib/task-labels.ts +35 -0
- package/src/lib/text-input.ts +10 -0
- package/src/lib/tool-call-status.ts +28 -0
- package/src/lib/tool-icons.ts +36 -0
- package/src/lib/tool-registry.ts +71 -0
- package/src/lib/tool-renderers.tsx +32 -0
- package/src/lib/turn-state.ts +15 -0
- package/src/lib/utils.ts +35 -0
- package/src/lib/voice-audio-engine-types.ts +59 -0
- package/src/lib/voice-audio-engine.native.ts +232 -0
- package/src/lib/voice-audio-engine.ts +206 -0
- package/src/lib/voice-call-request.ts +49 -0
- package/src/lib/voice-call.ts +627 -0
- package/src/lib/voice-controls-labels.ts +31 -0
- package/src/lib/voice-levels.ts +64 -0
- package/src/lib/voice-protocol.ts +328 -0
- package/src/lib/voice-worklets.ts +19 -0
- package/src/lib/work-log.ts +370 -0
- package/src/metro/chart-importers.json +18 -0
- package/src/metro/granular-charts.js +3 -0
- package/src/metro/index.cjs +227 -0
- package/src/metro/index.d.cts +5 -0
- package/src/runtime/agents.ts +151 -0
- package/src/runtime/api.ts +144 -0
- package/src/runtime/app-assertion.ts +51 -0
- package/src/runtime/attachment-utils.ts +97 -0
- package/src/runtime/chat-turn-control.ts +74 -0
- package/src/runtime/composer-draft-store.ts +270 -0
- package/src/runtime/config.tsx +222 -0
- package/src/runtime/conversation-dates.ts +31 -0
- package/src/runtime/conversation-store.ts +49 -0
- package/src/runtime/conversations.ts +633 -0
- package/src/runtime/error-utils.ts +144 -0
- package/src/runtime/host-tools.ts +290 -0
- package/src/runtime/index.ts +137 -0
- package/src/runtime/message-history.ts +124 -0
- package/src/runtime/message-reactions.ts +214 -0
- package/src/runtime/model.ts +39 -0
- package/src/runtime/oxy-files.ts +37 -0
- package/src/runtime/power-levels.ts +66 -0
- package/src/runtime/query-keys.ts +49 -0
- package/src/runtime/random-uuid.ts +25 -0
- package/src/runtime/reaction-cache.ts +45 -0
- package/src/runtime/room-events.ts +333 -0
- package/src/runtime/routes.ts +81 -0
- package/src/runtime/sharing.ts +217 -0
- package/src/runtime/sse-frame-reader.ts +88 -0
- package/src/runtime/stream-events.ts +154 -0
- package/src/runtime/stream-outcome.ts +21 -0
- package/src/runtime/thought-store.ts +103 -0
- package/src/runtime/thread/agent-activity.ts +261 -0
- package/src/runtime/thread/agent-exchange.ts +94 -0
- package/src/runtime/thread/automation-labels.ts +79 -0
- package/src/runtime/thread/conversation-export.ts +135 -0
- package/src/runtime/thread/message-days.ts +120 -0
- package/src/runtime/thread/message-processor.ts +93 -0
- package/src/runtime/thread/speaker-changes.ts +66 -0
- package/src/runtime/thread/task-utils.ts +11 -0
- package/src/runtime/thread/thought-utils.ts +750 -0
- package/src/runtime/thread/thread-history.ts +139 -0
- package/src/runtime/thread/timeline.ts +202 -0
- package/src/runtime/thread/title-tags.ts +8 -0
- package/src/runtime/thread/tool-cards.ts +387 -0
- package/src/runtime/turn-failure.ts +108 -0
- package/src/runtime/turn-preparation.ts +21 -0
- package/src/runtime/types.ts +172 -0
- package/src/runtime/usage-limit-error.ts +44 -0
- package/src/runtime/use-agent-activity.ts +80 -0
- package/src/runtime/use-conversation-stream.ts +1669 -0
- package/src/runtime/use-conversation-thread.ts +590 -0
- package/src/runtime/use-gemie-thread.ts +54 -0
- package/src/runtime/use-room-events.ts +75 -0
- package/src/runtime/use-start-conversation.ts +107 -0
- package/src/runtime/use-thread-history.ts +73 -0
- package/src/runtime/use-thread-search.ts +100 -0
- package/src/runtime/use-turn-edit.ts +99 -0
- package/src/text.ts +19 -0
- package/src/theme.ts +16 -0
- package/src/tools.ts +27 -0
- package/src/types.ts +161 -0
- package/src/voice-messages.ts +2 -0
- package/src/voice.ts +67 -0
- package/tsconfig.json +17 -0
- package/worklets/voice-processors.js +152 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { useEffect, useMemo } from 'react';
|
|
3
|
+
import { gemieQueryKeys } from '../runtime/query-keys';
|
|
4
|
+
import { type ThoughtScope, useThoughtStore } from '../runtime/thought-store';
|
|
5
|
+
import type { FailedTurn } from '../runtime/turn-failure';
|
|
6
|
+
import type { Message } from '../runtime/types';
|
|
7
|
+
|
|
8
|
+
export interface UseThoughtScopeOptions {
|
|
9
|
+
/** The conversation being streamed into, or `undefined` for a new chat. */
|
|
10
|
+
conversationId: string | undefined;
|
|
11
|
+
messages: Message[];
|
|
12
|
+
conversationLoading: boolean;
|
|
13
|
+
isLoading: boolean;
|
|
14
|
+
failedTurn: FailedTurn | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* This conversation as the thought panel reads it: its messages, whether they are all here yet,
|
|
19
|
+
* and the turn in flight — kept in the thought store.
|
|
20
|
+
*
|
|
21
|
+
* Keyed on the conversation and its messages, NOT on the panel being open. Syncing only while
|
|
22
|
+
* the panel was open meant the first press on a tool row rendered against whatever the store held
|
|
23
|
+
* before — another conversation's messages, or none — and found nothing by id. The store accepts
|
|
24
|
+
* this only while the open selection belongs to the same conversation, so a new-chat screen
|
|
25
|
+
* mounted underneath cannot blank a panel opened here.
|
|
26
|
+
*
|
|
27
|
+
* A load that failed is read off the query cache at the moment the memo recomputes: the load's
|
|
28
|
+
* end is what flips `conversationLoading`, so the read is fresh exactly when it matters.
|
|
29
|
+
*/
|
|
30
|
+
export function useThoughtScope({
|
|
31
|
+
conversationId,
|
|
32
|
+
messages,
|
|
33
|
+
conversationLoading,
|
|
34
|
+
isLoading,
|
|
35
|
+
failedTurn,
|
|
36
|
+
}: UseThoughtScopeOptions): ThoughtScope {
|
|
37
|
+
const queryClient = useQueryClient();
|
|
38
|
+
const sync = useThoughtStore((s) => s.sync);
|
|
39
|
+
|
|
40
|
+
const scope = useMemo<ThoughtScope>(() => {
|
|
41
|
+
const loadFailed =
|
|
42
|
+
conversationId !== undefined &&
|
|
43
|
+
queryClient.getQueryState(gemieQueryKeys.conversations.detail(conversationId))?.status ===
|
|
44
|
+
'error';
|
|
45
|
+
return {
|
|
46
|
+
conversationId: conversationId ?? null,
|
|
47
|
+
messages,
|
|
48
|
+
status: conversationLoading ? 'loading' : loadFailed ? 'failed' : 'ready',
|
|
49
|
+
isLoading,
|
|
50
|
+
failedTurn,
|
|
51
|
+
};
|
|
52
|
+
}, [conversationId, messages, conversationLoading, isLoading, failedTurn, queryClient]);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
sync(scope);
|
|
56
|
+
}, [scope, sync]);
|
|
57
|
+
|
|
58
|
+
return scope;
|
|
59
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { useLabels } from '../lib/labels';
|
|
3
|
+
import {
|
|
4
|
+
type Message,
|
|
5
|
+
type ThreadMessage,
|
|
6
|
+
arrivedIds,
|
|
7
|
+
daySeparators,
|
|
8
|
+
threadSeamIds,
|
|
9
|
+
turnTimings,
|
|
10
|
+
} from '../runtime/model';
|
|
11
|
+
import { consultedReplies, speakerChanges } from '../runtime/thread/speaker-changes';
|
|
12
|
+
import { useTimelineShape } from './useTimelineWindow';
|
|
13
|
+
|
|
14
|
+
/** The history of a conversation that has none, as ONE array: a fresh `[]` per render would break every memo. */
|
|
15
|
+
const NO_HISTORY: ThreadMessage[] = [];
|
|
16
|
+
|
|
17
|
+
/** For a row the timings map has never heard of: a shared frozen object keeps memoised rows still. */
|
|
18
|
+
const EMPTY_TIMING = Object.freeze({ startedAt: null, endedAt: null });
|
|
19
|
+
|
|
20
|
+
/** True for Alia's own replies (a delegated agent's are not). */
|
|
21
|
+
function isGemieReply(message: Message): boolean {
|
|
22
|
+
return message.role === 'assistant' && !message.agentInfo;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface UseThreadRowsOptions {
|
|
26
|
+
/** The conversation being streamed into. */
|
|
27
|
+
messages: readonly Message[];
|
|
28
|
+
/** Everything said in the thread before it, oldest first. Empty where nothing is behind it. */
|
|
29
|
+
history: readonly ThreadMessage[] | undefined;
|
|
30
|
+
/** The conversation being streamed into: where the seam between the history and the live rows falls. */
|
|
31
|
+
conversationId: string | undefined;
|
|
32
|
+
/** The conversation's messages are still on their way. */
|
|
33
|
+
conversationLoading: boolean;
|
|
34
|
+
/** The agents that are part of this conversation (the direct agent or a group's members): not announced as newcomers. */
|
|
35
|
+
memberIds?: ReadonlySet<string>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What the thread draws, derived once per change of its STRUCTURE.
|
|
40
|
+
*
|
|
41
|
+
* The live list as ids, roles, stamps and speakers is what a streamed token never changes, so
|
|
42
|
+
* everything derived from the whole thread (day lines, seams, turn timings, the last answer of
|
|
43
|
+
* Alia's, the window's ids) is keyed on that rather than on the messages: a token re-derives
|
|
44
|
+
* nothing, it re-renders the row it was written into and that is all.
|
|
45
|
+
*/
|
|
46
|
+
export function useThreadRows({
|
|
47
|
+
messages,
|
|
48
|
+
history: historyMessages,
|
|
49
|
+
conversationId,
|
|
50
|
+
conversationLoading,
|
|
51
|
+
memberIds,
|
|
52
|
+
}: UseThreadRowsOptions) {
|
|
53
|
+
const { t, locale } = useLabels();
|
|
54
|
+
const history = historyMessages ?? NO_HISTORY;
|
|
55
|
+
|
|
56
|
+
const live = useMemo(
|
|
57
|
+
() => messages.filter((m) => m.role === 'user' || m.role === 'assistant'),
|
|
58
|
+
[messages],
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Everything on screen, in reading order: the history first, the conversation being streamed
|
|
63
|
+
* into after it. Every position below — which row is last, which is the last of Alia's — is an
|
|
64
|
+
* index into THIS, not into the live messages.
|
|
65
|
+
*/
|
|
66
|
+
const rows = useMemo<Message[]>(
|
|
67
|
+
() => (history.length === 0 ? live : [...history, ...live]),
|
|
68
|
+
[history, live],
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const liveShape = useTimelineShape(live);
|
|
72
|
+
const shape = useMemo<readonly Message[]>(
|
|
73
|
+
() => (history.length === 0 ? liveShape : [...history, ...liveShape]),
|
|
74
|
+
[history, liveShape],
|
|
75
|
+
);
|
|
76
|
+
const ids = useMemo(() => shape.map((m) => m.id), [shape]);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The live rows that arrived since the last change to the list, which are the only ones that
|
|
80
|
+
* animate in. A history row never does, and neither does a conversation that was loaded or
|
|
81
|
+
* switched to (`arrivedIds`).
|
|
82
|
+
*/
|
|
83
|
+
const previousLive = useRef<{ ids: readonly string[]; loading: boolean } | null>(null);
|
|
84
|
+
const arrived = useMemo(
|
|
85
|
+
() =>
|
|
86
|
+
arrivedIds(
|
|
87
|
+
previousLive.current?.ids ?? null,
|
|
88
|
+
previousLive.current?.loading ?? false,
|
|
89
|
+
liveShape,
|
|
90
|
+
),
|
|
91
|
+
[liveShape],
|
|
92
|
+
);
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
previousLive.current = { ids: liveShape.map((m) => m.id), loading: conversationLoading };
|
|
95
|
+
}, [liveShape, conversationLoading]);
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Every turn's start and end, computed once for the whole thread: asked per row it is O(n²), and
|
|
99
|
+
* this list re-renders roughly twenty times a second while an answer streams.
|
|
100
|
+
*/
|
|
101
|
+
const timings = useMemo(() => turnTimings(shape), [shape]);
|
|
102
|
+
|
|
103
|
+
/** The day each message starts, when it starts a new one: Bloom's inline date line above it. */
|
|
104
|
+
const dayLabels = useMemo(() => {
|
|
105
|
+
const labels = new Map<string, string>();
|
|
106
|
+
for (const { messageId, label } of daySeparators(shape, new Date(), locale)) {
|
|
107
|
+
labels.set(
|
|
108
|
+
messageId,
|
|
109
|
+
label.kind === 'today'
|
|
110
|
+
? t('chat.today')
|
|
111
|
+
: label.kind === 'yesterday'
|
|
112
|
+
? t('chat.yesterday')
|
|
113
|
+
: label.text,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return labels;
|
|
117
|
+
}, [shape, locale, t]);
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Where one conversation of a thread ends and the next begins: the model was given a fresh
|
|
121
|
+
* context there, and everything above is out of its sight (`threadSeamIds`).
|
|
122
|
+
*/
|
|
123
|
+
const seamIds = useMemo(
|
|
124
|
+
() => threadSeamIds(history, liveShape, conversationId ?? ''),
|
|
125
|
+
[history, liveShape, conversationId],
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
/** The turns where a consulted agent comes into the conversation: a line says whom the person is chatting with. */
|
|
129
|
+
const speakers = useMemo(() => speakerChanges(shape, memberIds), [shape, memberIds]);
|
|
130
|
+
|
|
131
|
+
/** What those agents said: under the line, nothing; the line opens it. */
|
|
132
|
+
const consulted = useMemo(() => consultedReplies(shape, memberIds), [shape, memberIds]);
|
|
133
|
+
|
|
134
|
+
const timingOf = useCallback(
|
|
135
|
+
(messageId: string) => timings.get(messageId) ?? EMPTY_TIMING,
|
|
136
|
+
[timings],
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const lastReplyIndex = useMemo(
|
|
140
|
+
() => shape.reduce((acc, m, i) => (isGemieReply(m) ? i : acc), -1),
|
|
141
|
+
[shape],
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
rows,
|
|
146
|
+
live,
|
|
147
|
+
history,
|
|
148
|
+
ids,
|
|
149
|
+
arrived,
|
|
150
|
+
dayLabels,
|
|
151
|
+
seamIds,
|
|
152
|
+
speakers,
|
|
153
|
+
consulted,
|
|
154
|
+
lastReplyIndex,
|
|
155
|
+
timingOf,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
2
|
+
import type { GemieThreadAddress } from '../runtime/routes';
|
|
3
|
+
import type { ThreadHistory, ThreadSearchHit } from '../runtime/thread/thread-history';
|
|
4
|
+
import { useThreadHistory } from '../runtime/use-thread-history';
|
|
5
|
+
import { useThreadWindow } from '../runtime/use-thread-search';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What the thread looks at: the present, or a moment in its past.
|
|
9
|
+
*
|
|
10
|
+
* An agent's thread has two states and they are not variations of one. At the present, the
|
|
11
|
+
* history runs into the conversation being streamed into. At a moment — a hit of the thread's
|
|
12
|
+
* search — the screen shows the stretch around one old message and there is nothing live beneath
|
|
13
|
+
* it. The way back is leaving, not scrolling: the endpoint pages BACKWARDS only, so walking
|
|
14
|
+
* forward from a jump would be a road that ends.
|
|
15
|
+
*
|
|
16
|
+
* `thread` is the gemie's thread, by handle or by id; without it the chat is one conversation with
|
|
17
|
+
* nothing behind it and there is nothing to page into, search or jump to.
|
|
18
|
+
*/
|
|
19
|
+
export function useThreadView(
|
|
20
|
+
thread: GemieThreadAddress | undefined,
|
|
21
|
+
conversationId: string | undefined,
|
|
22
|
+
) {
|
|
23
|
+
const present = useThreadHistory(thread, conversationId);
|
|
24
|
+
const [searchOpen, setSearchOpen] = useState(false);
|
|
25
|
+
/** The message the reader jumped to, by cursor, or `null` for the present. */
|
|
26
|
+
const [jumpedTo, setJumpedTo] = useState<string | null>(null);
|
|
27
|
+
const past = useThreadWindow(thread, jumpedTo);
|
|
28
|
+
const jumped = jumpedTo !== null;
|
|
29
|
+
|
|
30
|
+
// Ask for the page above, unless one is already coming. Bloom fires `onStartReached` once per
|
|
31
|
+
// approach and re-arms when the reader leaves the zone, so the guards left are about the DATA.
|
|
32
|
+
const source = jumped
|
|
33
|
+
? { ...past, loading: past.isLoadingMore || past.isLoading }
|
|
34
|
+
: { ...present, loading: present.isLoadingMore };
|
|
35
|
+
const { messages, hasMore, loading, loadMore: fetchMore } = source;
|
|
36
|
+
const loadMore = useCallback(() => {
|
|
37
|
+
if (!loading) void fetchMore();
|
|
38
|
+
}, [loading, fetchMore]);
|
|
39
|
+
|
|
40
|
+
const history = useMemo<ThreadHistory | undefined>(
|
|
41
|
+
() =>
|
|
42
|
+
thread === undefined ? undefined : { messages, loading, ...(hasMore ? { loadMore } : {}) },
|
|
43
|
+
[thread, messages, loading, hasMore, loadMore],
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Open the thread around a hit, and close the search over it. The cursor rather than the
|
|
48
|
+
* message id: an id addresses a message, and only a cursor addresses a POSITION, which is what
|
|
49
|
+
* a window is asked for.
|
|
50
|
+
*/
|
|
51
|
+
const jump = useCallback((hit: ThreadSearchHit) => {
|
|
52
|
+
setJumpedTo(hit.cursor);
|
|
53
|
+
setSearchOpen(false);
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
history,
|
|
58
|
+
/** The cursor jumped to, or `null` at the present. */
|
|
59
|
+
focusCursor: jumpedTo,
|
|
60
|
+
jumped,
|
|
61
|
+
jump,
|
|
62
|
+
backToLatest: useCallback(() => setJumpedTo(null), []),
|
|
63
|
+
searchOpen,
|
|
64
|
+
openSearch: useCallback(() => setSearchOpen(true), []),
|
|
65
|
+
closeSearch: useCallback(() => setSearchOpen(false), []),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Message,
|
|
3
|
+
TAIL,
|
|
4
|
+
TOP,
|
|
5
|
+
type TimelineAnchor,
|
|
6
|
+
type TimelinePosition,
|
|
7
|
+
collapseToTail,
|
|
8
|
+
pinAnchor,
|
|
9
|
+
resolveStart,
|
|
10
|
+
revealAbove,
|
|
11
|
+
sameTimelineShape,
|
|
12
|
+
timelinePositions,
|
|
13
|
+
} from '../runtime/model';
|
|
14
|
+
import type { AiChatThreadHandle } from '@oxy.so/bloom/ai-chat';
|
|
15
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useState, type RefObject } from 'react';
|
|
16
|
+
import type { LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent } from 'react-native';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The list, handed back as the SAME array for as long as only what its
|
|
20
|
+
* messages say has changed (`sameTimelineShape`).
|
|
21
|
+
*
|
|
22
|
+
* The array it returns may hold an older copy of the message being streamed
|
|
23
|
+
* into, so it is for deriving structure — ids, roles, stamps, speakers — and
|
|
24
|
+
* never for drawing text.
|
|
25
|
+
*/
|
|
26
|
+
export function useTimelineShape<
|
|
27
|
+
T extends Pick<Message, 'id' | 'role' | 'createdAt' | 'agentInfo'>,
|
|
28
|
+
>(list: readonly T[]): readonly T[] {
|
|
29
|
+
const shape = useRef(list);
|
|
30
|
+
if (!sameTimelineShape(shape.current, list)) shape.current = list;
|
|
31
|
+
return shape.current;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Options {
|
|
35
|
+
/** Every row's id, in reading order. Keep its identity stable across tokens. */
|
|
36
|
+
ids: readonly string[];
|
|
37
|
+
/**
|
|
38
|
+
* What a saved position belongs to — the conversation — or `null` for a view
|
|
39
|
+
* that is not remembered (a cursor jump's window of the past).
|
|
40
|
+
*/
|
|
41
|
+
scrollKey: string | null;
|
|
42
|
+
/** The conversation's messages have loaded; a restore waits for it. */
|
|
43
|
+
ready: boolean;
|
|
44
|
+
/** Index of a cursor jump's target row, or -1. It is always mounted. */
|
|
45
|
+
focusIndex: number;
|
|
46
|
+
/** A turn is streaming; the window is never folded under it. */
|
|
47
|
+
turnInFlight: boolean;
|
|
48
|
+
atBottomThreshold: number;
|
|
49
|
+
threadRef: RefObject<AiChatThreadHandle | null>;
|
|
50
|
+
/** Ask for the page of history above the first loaded row. */
|
|
51
|
+
onLoadHistory?: () => void;
|
|
52
|
+
onScroll?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What is still to be done to the scroll once the rows it needs are laid out.
|
|
57
|
+
* A restore remembers the list it was set against: that list may still be the
|
|
58
|
+
* previous conversation's, and only a list that changed after it can say the
|
|
59
|
+
* saved row is really gone.
|
|
60
|
+
*/
|
|
61
|
+
type PendingScroll =
|
|
62
|
+
| { kind: 'end' }
|
|
63
|
+
| { kind: 'offset'; position: TimelinePosition; setAgainst: readonly string[] };
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A restore is waiting for its conversation's rows: the row it was saved
|
|
67
|
+
* against is not on screen, and the list has not changed since it was set. A
|
|
68
|
+
* list that did change without bringing that row back means the row is gone
|
|
69
|
+
* (edited away, regenerated), and the restore falls back to the newest turn.
|
|
70
|
+
*/
|
|
71
|
+
function awaitingRows(pending: { current: PendingScroll | null }, ids: readonly string[]): boolean {
|
|
72
|
+
const todo = pending.current;
|
|
73
|
+
if (todo?.kind !== 'offset' || ids.includes(todo.position.probeId)) return false;
|
|
74
|
+
if (ids === todo.setAgainst) return true;
|
|
75
|
+
pending.current = { kind: 'end' };
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The state before the first key: nothing mounted yet, so nothing to remember. */
|
|
80
|
+
const MOUNTING = Symbol('mounting');
|
|
81
|
+
|
|
82
|
+
const nextFrame = (run: () => void): void => {
|
|
83
|
+
if (typeof requestAnimationFrame === 'function') requestAnimationFrame(run);
|
|
84
|
+
else setTimeout(run, 0);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Which rows the thread mounts, and the scroll that goes with them: the window
|
|
89
|
+
* over a long history, and the position a conversation is returned to.
|
|
90
|
+
*
|
|
91
|
+
* ## The window
|
|
92
|
+
*
|
|
93
|
+
* The newest `INITIAL_ROWS` rows mount first. Near the top, Bloom's
|
|
94
|
+
* `onStartReached` reveals `PAGE_ROWS` more — or, with none left, asks for the
|
|
95
|
+
* thread's history — and `maintainStartPosition` holds the reader on the row
|
|
96
|
+
* they were reading while either lands above. The window folds back to its
|
|
97
|
+
* opening size only when the reader is at the bottom and nothing is streaming.
|
|
98
|
+
* See `runtime/thread/timeline.ts` for why it is this and not virtualization.
|
|
99
|
+
*
|
|
100
|
+
* ## The position
|
|
101
|
+
*
|
|
102
|
+
* Leaving a conversation — by navigating away or by switching the screen to
|
|
103
|
+
* another — saves the offset, whether the reader was at the bottom, and the
|
|
104
|
+
* window's anchor. Coming back restores the anchor first, so the same rows are
|
|
105
|
+
* mounted above the reader, and the offset once they have laid out. Coming
|
|
106
|
+
* back from a cursor jump does not restore: "back to latest" means the latest.
|
|
107
|
+
*/
|
|
108
|
+
export function useTimelineWindow({
|
|
109
|
+
ids,
|
|
110
|
+
scrollKey,
|
|
111
|
+
ready,
|
|
112
|
+
focusIndex,
|
|
113
|
+
turnInFlight,
|
|
114
|
+
atBottomThreshold,
|
|
115
|
+
threadRef,
|
|
116
|
+
onLoadHistory,
|
|
117
|
+
onScroll,
|
|
118
|
+
}: Options) {
|
|
119
|
+
const [state, setState] = useState<{ key: string | null; anchor: TimelineAnchor }>({
|
|
120
|
+
key: scrollKey,
|
|
121
|
+
anchor: TAIL,
|
|
122
|
+
});
|
|
123
|
+
// The render after a key change and before its effect: the new conversation
|
|
124
|
+
// starts from the newest rows until the effect says otherwise.
|
|
125
|
+
const anchor = state.key === scrollKey ? state.anchor : TAIL;
|
|
126
|
+
const start = useMemo(() => resolveStart(anchor, ids, focusIndex), [anchor, ids, focusIndex]);
|
|
127
|
+
|
|
128
|
+
const setAnchor = useCallback(
|
|
129
|
+
(next: TimelineAnchor) => setState((current) => ({ key: current.key, anchor: next })),
|
|
130
|
+
[],
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
/** This render's facts, for callbacks and cleanups that outlive it. */
|
|
134
|
+
const latest = useRef({ ids, start, anchor, ready, turnInFlight, onLoadHistory });
|
|
135
|
+
useLayoutEffect(() => {
|
|
136
|
+
latest.current = { ids, start, anchor, ready, turnInFlight, onLoadHistory };
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
/** The last position the thread reported for the current key. */
|
|
140
|
+
const lastOffset = useRef<number | null>(null);
|
|
141
|
+
const atBottom = useRef(true);
|
|
142
|
+
const pending = useRef<PendingScroll | null>(null);
|
|
143
|
+
const previousKey = useRef<string | null | typeof MOUNTING>(MOUNTING);
|
|
144
|
+
|
|
145
|
+
useLayoutEffect(() => {
|
|
146
|
+
const cameFrom = previousKey.current;
|
|
147
|
+
previousKey.current = scrollKey;
|
|
148
|
+
lastOffset.current = null;
|
|
149
|
+
atBottom.current = true;
|
|
150
|
+
// Returning from a jump is a request for the present, not for the past.
|
|
151
|
+
// A reader who left from the bottom comes back to the newest page, which
|
|
152
|
+
// is where they were; only a position mid-thread brings its rows back.
|
|
153
|
+
const read =
|
|
154
|
+
scrollKey !== null && cameFrom !== null ? timelinePositions.read(scrollKey) : undefined;
|
|
155
|
+
const saved = read?.atBottom === false ? read : undefined;
|
|
156
|
+
pending.current =
|
|
157
|
+
scrollKey === null
|
|
158
|
+
? null
|
|
159
|
+
: saved === undefined
|
|
160
|
+
? { kind: 'end' }
|
|
161
|
+
: { kind: 'offset', position: saved, setAgainst: ids };
|
|
162
|
+
setState({ key: scrollKey, anchor: saved?.anchor ?? TAIL });
|
|
163
|
+
|
|
164
|
+
return () => {
|
|
165
|
+
if (scrollKey === null) return;
|
|
166
|
+
const { ids: rows, start: first, anchor: kept } = latest.current;
|
|
167
|
+
if (rows.length === 0) return;
|
|
168
|
+
timelinePositions.save(scrollKey, {
|
|
169
|
+
anchor: kept,
|
|
170
|
+
offset: lastOffset.current ?? 0,
|
|
171
|
+
atBottom: atBottom.current,
|
|
172
|
+
probeId: rows[first] as string,
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
}, [scrollKey]);
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Pin the opening window to its first row once the rows exist, so a turn
|
|
179
|
+
* appended at the bottom grows the window rather than sliding it — a slide
|
|
180
|
+
* would unmount the top row under a reader who is mid-thread. Re-pinned the
|
|
181
|
+
* same way when the pinned row has gone (the list was replaced) — except
|
|
182
|
+
* while a restore waits for its conversation's rows: the rows on screen are
|
|
183
|
+
* still the last conversation's, and the anchor being restored is not in
|
|
184
|
+
* them yet.
|
|
185
|
+
*/
|
|
186
|
+
useLayoutEffect(() => {
|
|
187
|
+
if (ids.length === 0 || awaitingRows(pending, ids)) return;
|
|
188
|
+
const stale = anchor.kind === 'tail' || (anchor.kind === 'row' && !ids.includes(anchor.id));
|
|
189
|
+
if (!stale) return;
|
|
190
|
+
setState((current) =>
|
|
191
|
+
current.key === scrollKey && current.anchor === anchor
|
|
192
|
+
? { key: current.key, anchor: pinAnchor(ids, start) }
|
|
193
|
+
: current,
|
|
194
|
+
);
|
|
195
|
+
}, [ids, anchor, start, scrollKey]);
|
|
196
|
+
|
|
197
|
+
const handleStartReached = useCallback(() => {
|
|
198
|
+
const { ids: rows, start: first, onLoadHistory: loadHistory } = latest.current;
|
|
199
|
+
const next = revealAbove(rows, first);
|
|
200
|
+
if (next !== null) {
|
|
201
|
+
setAnchor(next);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
setAnchor(TOP);
|
|
205
|
+
loadHistory?.();
|
|
206
|
+
}, [setAnchor]);
|
|
207
|
+
|
|
208
|
+
const handleScroll = useCallback(
|
|
209
|
+
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
210
|
+
const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
|
|
211
|
+
lastOffset.current = contentOffset.y;
|
|
212
|
+
const wasAtBottom = atBottom.current;
|
|
213
|
+
atBottom.current =
|
|
214
|
+
contentSize.height - contentOffset.y - layoutMeasurement.height <= atBottomThreshold;
|
|
215
|
+
if (atBottom.current && !wasAtBottom && !latest.current.turnInFlight) {
|
|
216
|
+
const { ids: rows, start: first } = latest.current;
|
|
217
|
+
const folded = collapseToTail(rows, first);
|
|
218
|
+
if (folded !== null) {
|
|
219
|
+
pending.current = { kind: 'end' };
|
|
220
|
+
setAnchor(folded);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
onScroll?.(event);
|
|
224
|
+
},
|
|
225
|
+
[atBottomThreshold, onScroll, setAnchor],
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The rows have laid out: carry out whatever scroll was waiting for them.
|
|
230
|
+
* A frame later, so it lands after the thread's own reaction to the same
|
|
231
|
+
* layout (on web it follows to the end on its first one).
|
|
232
|
+
*/
|
|
233
|
+
const handleListLayout = useCallback(
|
|
234
|
+
(_event: LayoutChangeEvent) => {
|
|
235
|
+
const { ids: rows, ready: loaded } = latest.current;
|
|
236
|
+
if (pending.current === null || !loaded || rows.length === 0) return;
|
|
237
|
+
if (awaitingRows(pending, rows)) return;
|
|
238
|
+
const todo = pending.current;
|
|
239
|
+
pending.current = null;
|
|
240
|
+
nextFrame(() => {
|
|
241
|
+
if (todo.kind === 'end') threadRef.current?.scrollToEnd({ animated: false });
|
|
242
|
+
else threadRef.current?.scrollToOffset({ offset: todo.position.offset });
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
[threadRef],
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
/** Index of the first mounted row. */
|
|
250
|
+
start,
|
|
251
|
+
/** For `AiChatThread`: absent when there is nothing above to reveal or load. */
|
|
252
|
+
onStartReached: start > 0 || onLoadHistory !== undefined ? handleStartReached : undefined,
|
|
253
|
+
onScroll: handleScroll,
|
|
254
|
+
onListLayout: handleListLayout,
|
|
255
|
+
};
|
|
256
|
+
}
|