@widgetic/chat 0.1.39 → 0.1.40
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/README.md +71 -0
- package/dist/components/Chat.svelte +35 -1
- package/dist/components/Chat.svelte.d.ts +29 -0
- package/dist/components/ChatComposer.svelte +5 -1
- package/dist/components/ChatMessages.svelte +545 -210
- package/dist/components/ChatMessages.svelte.d.ts +18 -0
- package/dist/components/agent/AgentAvatar.svelte +153 -0
- package/dist/components/agent/AgentAvatar.svelte.d.ts +27 -0
- package/dist/components/agent/AgentTurn.svelte +64 -0
- package/dist/components/agent/AgentTurn.svelte.d.ts +32 -0
- package/dist/components/agent/AvatarBoundary.svelte +29 -0
- package/dist/components/agent/AvatarBoundary.svelte.d.ts +20 -0
- package/dist/components/agent/CodeCard.svelte +288 -0
- package/dist/components/agent/CodeCard.svelte.d.ts +12 -0
- package/dist/components/agent/GenerationModule.svelte +99 -0
- package/dist/components/agent/GenerationModule.svelte.d.ts +16 -0
- package/dist/components/agent/MemberAvatar.svelte +87 -0
- package/dist/components/agent/MemberAvatar.svelte.d.ts +8 -0
- package/dist/components/index.d.ts +5 -0
- package/dist/components/index.js +9 -0
- package/dist/i18n/messages.d.ts +31 -9
- package/dist/i18n/messages.js +31 -9
- package/dist/types/agent.d.ts +74 -0
- package/dist/types/agent.js +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/dist/utils/agentTurn.d.ts +219 -0
- package/dist/utils/agentTurn.js +301 -0
- package/dist/utils/bubbleFit.d.ts +75 -0
- package/dist/utils/bubbleFit.js +116 -0
- package/dist/utils/bubbleShrinkWrap.d.ts +32 -0
- package/dist/utils/bubbleShrinkWrap.js +360 -0
- package/dist/utils/codeHighlight.d.ts +13 -0
- package/dist/utils/codeHighlight.js +61 -0
- package/dist/utils/codePrettier.d.ts +6 -0
- package/dist/utils/codePrettier.js +34 -0
- package/dist/utils/codeStreamFormat.d.ts +90 -0
- package/dist/utils/codeStreamFormat.js +429 -0
- package/dist/utils/generationArchiveStore.d.ts +5 -0
- package/dist/utils/generationArchiveStore.js +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +3 -0
- package/package.json +3 -2
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import type { ChatMessageKey } from '../i18n/messages.js';
|
|
2
|
+
import type { AgentAvatarState, ChatMember } from '../types/agent.js';
|
|
3
|
+
import type { GenerationActivityPhase } from './generationActivity.js';
|
|
4
|
+
/**
|
|
5
|
+
* The rules of the assistant's turn: what its avatar looks like and does at each moment, how a finished turn is judged,
|
|
6
|
+
* who wrote a message of a person, and what is kept of the work once it is done.
|
|
7
|
+
*
|
|
8
|
+
* Plain TypeScript with no Svelte in it, so the rules can be tested in Node as they are (`tests/agent-turn.test.mjs`)
|
|
9
|
+
* and a page that is not the chat (the workflows assistant) can follow the same choreography.
|
|
10
|
+
*/
|
|
11
|
+
/** `large` is the avatar of the moments that matter (waiting, a card to answer, the conclusion); `small` sits by the text. */
|
|
12
|
+
export type AgentAvatarSize = 'large' | 'small';
|
|
13
|
+
/** What the avatar of an assistant turn looks like and does. */
|
|
14
|
+
export interface AgentLook {
|
|
15
|
+
state: AgentAvatarState;
|
|
16
|
+
size: AgentAvatarSize;
|
|
17
|
+
/** True for the avatar that moves (the newest turn); false for one that only sits there as a still picture. */
|
|
18
|
+
live: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The look while a generation runs.
|
|
22
|
+
*
|
|
23
|
+
* - Waiting for the first word of the model: the avatar is large and thinks. A plan card or a question, when there is
|
|
24
|
+
* one, sits to its right.
|
|
25
|
+
* - Its reasoning streams: the avatar shrinks to the left and KEEPS THINKING for as long as the reasoning is being written.
|
|
26
|
+
* The words beside it say "Thinking…" (the thinking module's own title): the avatar carries no name or label.
|
|
27
|
+
* - What it writes next streams (the code, or the reply the model writes: the chat has no other stream than the reasoning and
|
|
28
|
+
* this one): still small, and now it WRITES; the code module's title says "Writing code…".
|
|
29
|
+
*
|
|
30
|
+
* (The mascot package maps the phases the same way: `thinking-stream` is its `thinking` state.)
|
|
31
|
+
*/
|
|
32
|
+
export declare function liveAgentLook(phase: GenerationActivityPhase): AgentLook;
|
|
33
|
+
/** How a turn ended, as far as the avatar cares. */
|
|
34
|
+
export type TurnOutcome =
|
|
35
|
+
/** The assistant built something (a commit, "Code generation completed") or a generation ran for this turn. */
|
|
36
|
+
'success'
|
|
37
|
+
/** The last thing it said is a failure. */
|
|
38
|
+
| 'error'
|
|
39
|
+
/** The person stopped it: their own command, not a failure. */
|
|
40
|
+
| 'stopped'
|
|
41
|
+
/** The last thing it said is a card waiting for an answer. */
|
|
42
|
+
| 'question'
|
|
43
|
+
/** Anything else it said (a plain answer). */
|
|
44
|
+
| 'answer';
|
|
45
|
+
/** What the chat found out about one visible reply of a turn (the chat's own tests of the text, kept out of this file). */
|
|
46
|
+
export interface TurnReplyFacts {
|
|
47
|
+
isFailure: boolean;
|
|
48
|
+
isStopped: boolean;
|
|
49
|
+
/** The reply carries clarification suggestions: a question card waits under it. */
|
|
50
|
+
hasQuestion: boolean;
|
|
51
|
+
/** The reply announces a widget commit. */
|
|
52
|
+
hasCommit: boolean;
|
|
53
|
+
/** The reply is the "Code generation completed" line. */
|
|
54
|
+
isCompletion: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Judges a turn from its visible replies. The LAST reply decides how it ended; a commit or a completion line anywhere in
|
|
58
|
+
* the turn (or a generation that ran for it) makes an ordinary last reply a success.
|
|
59
|
+
*/
|
|
60
|
+
export declare function turnOutcome(replies: readonly TurnReplyFacts[], generationRanForTurn: boolean): TurnOutcome | null;
|
|
61
|
+
/**
|
|
62
|
+
* The look of a turn that is not running. Only the newest turn moves, and only its conclusion is large (the success or
|
|
63
|
+
* the error avatar, or a question to answer): older turns are small still pictures, so a long chat keeps one mascot
|
|
64
|
+
* alive instead of one per message.
|
|
65
|
+
*/
|
|
66
|
+
export declare function settledAgentLook(outcome: TurnOutcome, isNewestTurn: boolean): AgentLook;
|
|
67
|
+
/** Where the avatar that takes over from the live one starts. */
|
|
68
|
+
export interface AvatarStart {
|
|
69
|
+
/** The size it glides from to its own. */
|
|
70
|
+
startSize: AgentAvatarSize;
|
|
71
|
+
/** The state it is drawn in first, before it moves on to its own (a mascot blends between the two). Undefined: its own from the start. */
|
|
72
|
+
startState: AgentAvatarState | undefined;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The live avatar of a run and the one of its ending are two avatars, one after the other, in the same place. The second one starts
|
|
76
|
+
* from how the first one looked when the run ended, so what the person watched carries on instead of being swapped: a Stop pressed
|
|
77
|
+
* while the avatar is still large shrinks it down to its small size, and the mascot calms from what it was doing; a conclusion
|
|
78
|
+
* grows out of the avatar of the work.
|
|
79
|
+
*
|
|
80
|
+
* Without a live avatar to take over from (the conversation was opened or reloaded after the run) it starts from the small one,
|
|
81
|
+
* which is where a conclusion has always grown out of.
|
|
82
|
+
*/
|
|
83
|
+
export declare function avatarStartAfter(lastLiveLook: AgentLook | null): AvatarStart;
|
|
84
|
+
/** What `soundStateOf` looks at. */
|
|
85
|
+
export interface SoundStateInput {
|
|
86
|
+
/** A run is working and its live avatar is on screen. */
|
|
87
|
+
runIsLive: boolean;
|
|
88
|
+
/** How the live avatar looks for the phase the run is in. */
|
|
89
|
+
liveLook: AgentLook;
|
|
90
|
+
/** A run worked while this conversation was open here (the person saw it happen). */
|
|
91
|
+
runWasWatched: boolean;
|
|
92
|
+
/** How the avatar of the newest turn looks, or null when that turn has no assistant to show (yet). */
|
|
93
|
+
newestTurnLook: AgentLook | null;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The state of the avatar the person is looking at, for the page's sound to follow, or null when there is nothing to hear.
|
|
97
|
+
*
|
|
98
|
+
* - While a run works, the live avatar's own state: thinking when it starts, writing when code or the answer is written.
|
|
99
|
+
* - When it ends, the state of the newest turn's avatar: success or error say how it went, and idle (a question, a stop) says it
|
|
100
|
+
* is waiting.
|
|
101
|
+
* - Never for a conversation that was only opened: its newest turn may be left in `error` or `success`, but nothing happened in
|
|
102
|
+
* front of the person, so nothing is heard. Opening a chat must not play the sound of a failure.
|
|
103
|
+
* - Null as well while the newest turn has nothing to show yet (a prompt just sent, or a run that has ended a moment before its
|
|
104
|
+
* conclusion arrives): the sound keeps what it last heard.
|
|
105
|
+
*
|
|
106
|
+
* Only a change of this value is worth telling the sound about, and the caller tells it once for each.
|
|
107
|
+
*/
|
|
108
|
+
export declare function soundStateOf(input: SoundStateInput): AgentAvatarState | null;
|
|
109
|
+
/** The side of the avatar in pixels. Without a page's own avatar the large one is only a little larger: a plain circle that big is noise. */
|
|
110
|
+
export declare const AGENT_AVATAR_PIXELS: {
|
|
111
|
+
/** The mascot's full drawing (64 px and up). */
|
|
112
|
+
readonly largeWithPageAvatar: 80;
|
|
113
|
+
readonly largeWithoutPageAvatar: 32;
|
|
114
|
+
/** The mascot's compact drawing, which still moves (28 px and up). */
|
|
115
|
+
readonly small: 28;
|
|
116
|
+
};
|
|
117
|
+
export declare function agentAvatarPixels(size: AgentAvatarSize, hasPageAvatar: boolean): number;
|
|
118
|
+
/**
|
|
119
|
+
* The catalog key of what a screen reader says about the avatar in a state, or null when it says nothing: an avatar at rest
|
|
120
|
+
* sits beside the words of the turn, which are what is read (the others say what is going on, which no words beside it do).
|
|
121
|
+
*/
|
|
122
|
+
export declare function agentAvatarLabelKey(state: AgentAvatarState): ChatMessageKey | null;
|
|
123
|
+
/** A stable text for a look, so the chat can say a change once and not on every redraw. */
|
|
124
|
+
export declare function agentLookSignature(look: AgentLook): string;
|
|
125
|
+
/** Which parts of the work are on screen, and which of them start open. */
|
|
126
|
+
export interface GenerationModulesView {
|
|
127
|
+
/** The typed "Assistant is thinking" line: nothing has streamed yet. */
|
|
128
|
+
showShimmer: boolean;
|
|
129
|
+
showThinking: boolean;
|
|
130
|
+
/** Reasoning is still arriving (and nothing else has started). */
|
|
131
|
+
thinkingStreaming: boolean;
|
|
132
|
+
/** A module is open while it is the live one, and closed once the next has taken over. The person can still open it. */
|
|
133
|
+
thinkingOpenByDefault: boolean;
|
|
134
|
+
showCode: boolean;
|
|
135
|
+
codeStreaming: boolean;
|
|
136
|
+
codeOpenByDefault: boolean;
|
|
137
|
+
}
|
|
138
|
+
/** The modules while a generation runs. The thoughts fold away once the code starts, the code stays open while it streams. */
|
|
139
|
+
export declare function liveModulesView(phase: GenerationActivityPhase): GenerationModulesView;
|
|
140
|
+
/** The modules under a conclusion: whatever was kept, closed, for the person to open. */
|
|
141
|
+
export declare function finishedModulesView(finished: Pick<FinishedGeneration, 'thinking' | 'code'> | null): GenerationModulesView;
|
|
142
|
+
/**
|
|
143
|
+
* The thinking and the code of the generation that just ended: what the modules under the conclusion show.
|
|
144
|
+
* Kept in memory for the newest turn only (the host saves neither); it is gone after a reload, and the conclusion
|
|
145
|
+
* simply shows without them.
|
|
146
|
+
*/
|
|
147
|
+
export interface FinishedGeneration {
|
|
148
|
+
/** The prompt of the turn it belongs to, normalized with `normalizePromptText`. */
|
|
149
|
+
prompt: string;
|
|
150
|
+
thinking: string;
|
|
151
|
+
code: string;
|
|
152
|
+
thoughtSeconds: number | null;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The most text kept of either part. A generation streams tails; the chat stitches them, and a runaway stream
|
|
156
|
+
* must not grow memory without end.
|
|
157
|
+
*/
|
|
158
|
+
export declare const MAX_KEPT_GENERATION_CHARS = 120000;
|
|
159
|
+
/**
|
|
160
|
+
* A prompt as a way to recognise its turn. A message id is no use: the host swaps a made-up id for the saved one, and
|
|
161
|
+
* may reload the whole conversation after a generation. The words the person wrote survive both.
|
|
162
|
+
*/
|
|
163
|
+
export declare function normalizePromptText(content: string | null | undefined): string;
|
|
164
|
+
/** What to keep of a generation that ended, or null when it left nothing to show. */
|
|
165
|
+
export declare function keepFinishedGeneration(input: {
|
|
166
|
+
promptText: string | null | undefined;
|
|
167
|
+
thinking: string;
|
|
168
|
+
code: string;
|
|
169
|
+
thoughtSeconds: number | null;
|
|
170
|
+
}): FinishedGeneration | null;
|
|
171
|
+
/** Whether the kept work belongs under the turn of this prompt: the newest turn, found again by the words of its prompt. */
|
|
172
|
+
export declare function finishedGenerationBelongsTo(finished: FinishedGeneration | null, newestTurnPromptText: string | null | undefined): finished is FinishedGeneration;
|
|
173
|
+
/**
|
|
174
|
+
* Whether the work kept for a run is out of date because the person sent another prompt after the one that run answered.
|
|
175
|
+
* The words of a prompt sent again (a retry) are the same, so `finishedGenerationBelongsTo` cannot tell the new turn from the
|
|
176
|
+
* old one, and for as long as the host takes to save the prompt and start the run (the chat adds the message first) the new turn
|
|
177
|
+
* would show the last run's conclusion. The message the kept work answered tells them apart: a user message after it is a new turn.
|
|
178
|
+
*
|
|
179
|
+
* That message is found by its id, not by the object: the chat builds new message objects when a prompt is sent (it clears the
|
|
180
|
+
* marks of the last version first), while the id of a prompt is the saved one by the time its run ends, because the host
|
|
181
|
+
* saves a prompt before it starts the run.
|
|
182
|
+
*
|
|
183
|
+
* When there is no id, or no message with it in the list any more (the host loaded the conversation again, or the work came back
|
|
184
|
+
* from the archive, which knows only the words) there is nothing to tell by, and the words decide, as they always did.
|
|
185
|
+
*/
|
|
186
|
+
export declare function keptWorkIsOutdated(answeredMessageId: string | null, messagesInOrder: ReadonlyArray<{
|
|
187
|
+
id: string;
|
|
188
|
+
messageType: string;
|
|
189
|
+
}>): boolean;
|
|
190
|
+
/** The `userId` the chat gives a message the signed-in person adds with `addUserMessage`, before the host has saved it and told the real one. */
|
|
191
|
+
export declare const CURRENT_USER_PLACEHOLDER_ID = "current_user";
|
|
192
|
+
/**
|
|
193
|
+
* The `userId` the chat's own local service gives every message the person types: `Chat.sendMessage` shows the message at
|
|
194
|
+
* once, made by that service, and `onSaveMessage` swaps the id of the message but never this. A made-up id too: until the page
|
|
195
|
+
* loads the history again (and the messages carry the real ids), it stands for the signed-in person.
|
|
196
|
+
*/
|
|
197
|
+
export declare const LOCAL_SERVICE_USER_ID = "test_user";
|
|
198
|
+
/**
|
|
199
|
+
* Finds the person behind a message. The team the page gave wins (it is the list the page keeps up to date); the
|
|
200
|
+
* signed-in person comes next, which is also who the made-up ids of a message that is not saved yet stand for.
|
|
201
|
+
* Null when nobody can be named: the message then has no face and no tooltip, instead of a wrong one.
|
|
202
|
+
*/
|
|
203
|
+
export declare function resolveChatMember(userId: string | null | undefined, members: readonly ChatMember[], currentUser: ChatMember | null | undefined): ChatMember | null;
|
|
204
|
+
/** The text the initials of an avatar come from: the name, or the part of the e-mail before the @. Empty when there is neither. */
|
|
205
|
+
export declare function memberDisplayName(member: ChatMember | null | undefined): string;
|
|
206
|
+
/**
|
|
207
|
+
* The e-mail line of the tooltip over an avatar: the e-mail only, never a placeholder word. The same rule as the
|
|
208
|
+
* project header of the Widgetic site (`projectMemberTooltipEmail`), so a person sees the same thing in both places.
|
|
209
|
+
*/
|
|
210
|
+
export declare function memberTooltipEmail(email: string | null | undefined): string | null;
|
|
211
|
+
/**
|
|
212
|
+
* Where a click on an avatar goes: a `mailto:` link to the e-mail of the person, as the avatars of the project header of the
|
|
213
|
+
* Widgetic site do (`personMailtoHref`). Null when there is no e-mail to write to (the same rule as the tooltip's: never a
|
|
214
|
+
* placeholder word), and the avatar is then only a picture. The part before the @ is encoded (a `+` in it would otherwise be
|
|
215
|
+
* read as a space); the domain is left as it is.
|
|
216
|
+
*/
|
|
217
|
+
export declare function memberMailtoHref(email: string | null | undefined): string | null;
|
|
218
|
+
/** Whether the page told the chat anything about who is who: without it the chat draws no faces next to what people wrote. */
|
|
219
|
+
export declare function pageNamesPeople(members: readonly ChatMember[], currentUser: ChatMember | null | undefined): boolean;
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/** A turn that is not the newest, or has nothing to say about itself: small, still, calm. */
|
|
2
|
+
const RESTING_STILL_LOOK = { state: 'idle', size: 'small', live: false };
|
|
3
|
+
/**
|
|
4
|
+
* The look while a generation runs.
|
|
5
|
+
*
|
|
6
|
+
* - Waiting for the first word of the model: the avatar is large and thinks. A plan card or a question, when there is
|
|
7
|
+
* one, sits to its right.
|
|
8
|
+
* - Its reasoning streams: the avatar shrinks to the left and KEEPS THINKING for as long as the reasoning is being written.
|
|
9
|
+
* The words beside it say "Thinking…" (the thinking module's own title): the avatar carries no name or label.
|
|
10
|
+
* - What it writes next streams (the code, or the reply the model writes: the chat has no other stream than the reasoning and
|
|
11
|
+
* this one): still small, and now it WRITES; the code module's title says "Writing code…".
|
|
12
|
+
*
|
|
13
|
+
* (The mascot package maps the phases the same way: `thinking-stream` is its `thinking` state.)
|
|
14
|
+
*/
|
|
15
|
+
export function liveAgentLook(phase) {
|
|
16
|
+
switch (phase) {
|
|
17
|
+
case 'shimmer-only':
|
|
18
|
+
return { state: 'thinking', size: 'large', live: true };
|
|
19
|
+
case 'thinking-stream':
|
|
20
|
+
return { state: 'thinking', size: 'small', live: true };
|
|
21
|
+
case 'thinking-and-code':
|
|
22
|
+
case 'code-only':
|
|
23
|
+
return { state: 'writing', size: 'small', live: true };
|
|
24
|
+
case 'idle':
|
|
25
|
+
default:
|
|
26
|
+
return { ...RESTING_STILL_LOOK, live: true };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Judges a turn from its visible replies. The LAST reply decides how it ended; a commit or a completion line anywhere in
|
|
31
|
+
* the turn (or a generation that ran for it) makes an ordinary last reply a success.
|
|
32
|
+
*/
|
|
33
|
+
export function turnOutcome(replies, generationRanForTurn) {
|
|
34
|
+
const lastReply = replies[replies.length - 1];
|
|
35
|
+
if (!lastReply)
|
|
36
|
+
return generationRanForTurn ? 'success' : null;
|
|
37
|
+
if (lastReply.isStopped)
|
|
38
|
+
return 'stopped';
|
|
39
|
+
if (lastReply.isFailure)
|
|
40
|
+
return 'error';
|
|
41
|
+
if (lastReply.hasQuestion)
|
|
42
|
+
return 'question';
|
|
43
|
+
if (generationRanForTurn || replies.some((reply) => reply.hasCommit || reply.isCompletion))
|
|
44
|
+
return 'success';
|
|
45
|
+
return 'answer';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The look of a turn that is not running. Only the newest turn moves, and only its conclusion is large (the success or
|
|
49
|
+
* the error avatar, or a question to answer): older turns are small still pictures, so a long chat keeps one mascot
|
|
50
|
+
* alive instead of one per message.
|
|
51
|
+
*/
|
|
52
|
+
export function settledAgentLook(outcome, isNewestTurn) {
|
|
53
|
+
if (!isNewestTurn)
|
|
54
|
+
return RESTING_STILL_LOOK;
|
|
55
|
+
switch (outcome) {
|
|
56
|
+
case 'success':
|
|
57
|
+
return { state: 'success', size: 'large', live: true };
|
|
58
|
+
case 'error':
|
|
59
|
+
return { state: 'error', size: 'large', live: true };
|
|
60
|
+
case 'question':
|
|
61
|
+
return { state: 'idle', size: 'large', live: true };
|
|
62
|
+
case 'stopped':
|
|
63
|
+
case 'answer':
|
|
64
|
+
default:
|
|
65
|
+
return { state: 'idle', size: 'small', live: true };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The live avatar of a run and the one of its ending are two avatars, one after the other, in the same place. The second one starts
|
|
70
|
+
* from how the first one looked when the run ended, so what the person watched carries on instead of being swapped: a Stop pressed
|
|
71
|
+
* while the avatar is still large shrinks it down to its small size, and the mascot calms from what it was doing; a conclusion
|
|
72
|
+
* grows out of the avatar of the work.
|
|
73
|
+
*
|
|
74
|
+
* Without a live avatar to take over from (the conversation was opened or reloaded after the run) it starts from the small one,
|
|
75
|
+
* which is where a conclusion has always grown out of.
|
|
76
|
+
*/
|
|
77
|
+
export function avatarStartAfter(lastLiveLook) {
|
|
78
|
+
if (!lastLiveLook)
|
|
79
|
+
return { startSize: 'small', startState: undefined };
|
|
80
|
+
return { startSize: lastLiveLook.size, startState: lastLiveLook.state };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The state of the avatar the person is looking at, for the page's sound to follow, or null when there is nothing to hear.
|
|
84
|
+
*
|
|
85
|
+
* - While a run works, the live avatar's own state: thinking when it starts, writing when code or the answer is written.
|
|
86
|
+
* - When it ends, the state of the newest turn's avatar: success or error say how it went, and idle (a question, a stop) says it
|
|
87
|
+
* is waiting.
|
|
88
|
+
* - Never for a conversation that was only opened: its newest turn may be left in `error` or `success`, but nothing happened in
|
|
89
|
+
* front of the person, so nothing is heard. Opening a chat must not play the sound of a failure.
|
|
90
|
+
* - Null as well while the newest turn has nothing to show yet (a prompt just sent, or a run that has ended a moment before its
|
|
91
|
+
* conclusion arrives): the sound keeps what it last heard.
|
|
92
|
+
*
|
|
93
|
+
* Only a change of this value is worth telling the sound about, and the caller tells it once for each.
|
|
94
|
+
*/
|
|
95
|
+
export function soundStateOf(input) {
|
|
96
|
+
if (input.runIsLive)
|
|
97
|
+
return input.liveLook.state;
|
|
98
|
+
if (!input.runWasWatched || input.newestTurnLook === null)
|
|
99
|
+
return null;
|
|
100
|
+
return input.newestTurnLook.state;
|
|
101
|
+
}
|
|
102
|
+
/** The side of the avatar in pixels. Without a page's own avatar the large one is only a little larger: a plain circle that big is noise. */
|
|
103
|
+
export const AGENT_AVATAR_PIXELS = {
|
|
104
|
+
/** The mascot's full drawing (64 px and up). */
|
|
105
|
+
largeWithPageAvatar: 80,
|
|
106
|
+
largeWithoutPageAvatar: 32,
|
|
107
|
+
/** The mascot's compact drawing, which still moves (28 px and up). */
|
|
108
|
+
small: 28
|
|
109
|
+
};
|
|
110
|
+
export function agentAvatarPixels(size, hasPageAvatar) {
|
|
111
|
+
if (size === 'small')
|
|
112
|
+
return AGENT_AVATAR_PIXELS.small;
|
|
113
|
+
return hasPageAvatar ? AGENT_AVATAR_PIXELS.largeWithPageAvatar : AGENT_AVATAR_PIXELS.largeWithoutPageAvatar;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The catalog key of what a screen reader says about the avatar in a state, or null when it says nothing: an avatar at rest
|
|
117
|
+
* sits beside the words of the turn, which are what is read (the others say what is going on, which no words beside it do).
|
|
118
|
+
*/
|
|
119
|
+
export function agentAvatarLabelKey(state) {
|
|
120
|
+
switch (state) {
|
|
121
|
+
case 'thinking':
|
|
122
|
+
return 'chat.generation.statusThinking';
|
|
123
|
+
case 'writing':
|
|
124
|
+
return 'chat.generation.statusWritingCode';
|
|
125
|
+
case 'success':
|
|
126
|
+
return 'chat.agent.avatar.success';
|
|
127
|
+
case 'error':
|
|
128
|
+
return 'chat.agent.avatar.error';
|
|
129
|
+
case 'idle':
|
|
130
|
+
default:
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** A stable text for a look, so the chat can say a change once and not on every redraw. */
|
|
135
|
+
export function agentLookSignature(look) {
|
|
136
|
+
return `${look.state}:${look.size}:${look.live ? 'live' : 'still'}`;
|
|
137
|
+
}
|
|
138
|
+
const NOTHING_SHOWN = {
|
|
139
|
+
showShimmer: false,
|
|
140
|
+
showThinking: false,
|
|
141
|
+
thinkingStreaming: false,
|
|
142
|
+
thinkingOpenByDefault: false,
|
|
143
|
+
showCode: false,
|
|
144
|
+
codeStreaming: false,
|
|
145
|
+
codeOpenByDefault: false
|
|
146
|
+
};
|
|
147
|
+
/** The modules while a generation runs. The thoughts fold away once the code starts, the code stays open while it streams. */
|
|
148
|
+
export function liveModulesView(phase) {
|
|
149
|
+
switch (phase) {
|
|
150
|
+
case 'shimmer-only':
|
|
151
|
+
return { ...NOTHING_SHOWN, showShimmer: true };
|
|
152
|
+
case 'thinking-stream':
|
|
153
|
+
return { ...NOTHING_SHOWN, showThinking: true, thinkingStreaming: true, thinkingOpenByDefault: true };
|
|
154
|
+
case 'thinking-and-code':
|
|
155
|
+
return { ...NOTHING_SHOWN, showThinking: true, showCode: true, codeStreaming: true, codeOpenByDefault: true };
|
|
156
|
+
case 'code-only':
|
|
157
|
+
return { ...NOTHING_SHOWN, showCode: true, codeStreaming: true, codeOpenByDefault: true };
|
|
158
|
+
case 'idle':
|
|
159
|
+
default:
|
|
160
|
+
return NOTHING_SHOWN;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** The modules under a conclusion: whatever was kept, closed, for the person to open. */
|
|
164
|
+
export function finishedModulesView(finished) {
|
|
165
|
+
if (!finished)
|
|
166
|
+
return NOTHING_SHOWN;
|
|
167
|
+
return {
|
|
168
|
+
...NOTHING_SHOWN,
|
|
169
|
+
showThinking: finished.thinking.trim().length > 0,
|
|
170
|
+
showCode: finished.code.trim().length > 0
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The most text kept of either part. A generation streams tails; the chat stitches them, and a runaway stream
|
|
175
|
+
* must not grow memory without end.
|
|
176
|
+
*/
|
|
177
|
+
export const MAX_KEPT_GENERATION_CHARS = 120_000;
|
|
178
|
+
/**
|
|
179
|
+
* A prompt as a way to recognise its turn. A message id is no use: the host swaps a made-up id for the saved one, and
|
|
180
|
+
* may reload the whole conversation after a generation. The words the person wrote survive both.
|
|
181
|
+
*/
|
|
182
|
+
export function normalizePromptText(content) {
|
|
183
|
+
return (content ?? '').replace(/\s+/g, ' ').trim();
|
|
184
|
+
}
|
|
185
|
+
/** What to keep of a generation that ended, or null when it left nothing to show. */
|
|
186
|
+
export function keepFinishedGeneration(input) {
|
|
187
|
+
const thinking = input.thinking.slice(-MAX_KEPT_GENERATION_CHARS);
|
|
188
|
+
const code = input.code.slice(-MAX_KEPT_GENERATION_CHARS);
|
|
189
|
+
if (thinking.trim().length === 0 && code.trim().length === 0)
|
|
190
|
+
return null;
|
|
191
|
+
return {
|
|
192
|
+
prompt: normalizePromptText(input.promptText),
|
|
193
|
+
thinking,
|
|
194
|
+
code,
|
|
195
|
+
thoughtSeconds: input.thoughtSeconds
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/** Whether the kept work belongs under the turn of this prompt: the newest turn, found again by the words of its prompt. */
|
|
199
|
+
export function finishedGenerationBelongsTo(finished, newestTurnPromptText) {
|
|
200
|
+
if (!finished)
|
|
201
|
+
return false;
|
|
202
|
+
return finished.prompt === normalizePromptText(newestTurnPromptText);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Whether the work kept for a run is out of date because the person sent another prompt after the one that run answered.
|
|
206
|
+
* The words of a prompt sent again (a retry) are the same, so `finishedGenerationBelongsTo` cannot tell the new turn from the
|
|
207
|
+
* old one, and for as long as the host takes to save the prompt and start the run (the chat adds the message first) the new turn
|
|
208
|
+
* would show the last run's conclusion. The message the kept work answered tells them apart: a user message after it is a new turn.
|
|
209
|
+
*
|
|
210
|
+
* That message is found by its id, not by the object: the chat builds new message objects when a prompt is sent (it clears the
|
|
211
|
+
* marks of the last version first), while the id of a prompt is the saved one by the time its run ends, because the host
|
|
212
|
+
* saves a prompt before it starts the run.
|
|
213
|
+
*
|
|
214
|
+
* When there is no id, or no message with it in the list any more (the host loaded the conversation again, or the work came back
|
|
215
|
+
* from the archive, which knows only the words) there is nothing to tell by, and the words decide, as they always did.
|
|
216
|
+
*/
|
|
217
|
+
export function keptWorkIsOutdated(answeredMessageId, messagesInOrder) {
|
|
218
|
+
if (!answeredMessageId)
|
|
219
|
+
return false;
|
|
220
|
+
let answeredIndex = -1;
|
|
221
|
+
for (let index = messagesInOrder.length - 1; index >= 0; index--) {
|
|
222
|
+
if (messagesInOrder[index].id === answeredMessageId) {
|
|
223
|
+
answeredIndex = index;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (answeredIndex === -1)
|
|
228
|
+
return false;
|
|
229
|
+
for (let index = answeredIndex + 1; index < messagesInOrder.length; index++) {
|
|
230
|
+
if (messagesInOrder[index].messageType === 'user')
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
// --- who wrote it -------------------------------------------------------------------------------------------
|
|
236
|
+
/** The `userId` the chat gives a message the signed-in person adds with `addUserMessage`, before the host has saved it and told the real one. */
|
|
237
|
+
export const CURRENT_USER_PLACEHOLDER_ID = 'current_user';
|
|
238
|
+
/**
|
|
239
|
+
* The `userId` the chat's own local service gives every message the person types: `Chat.sendMessage` shows the message at
|
|
240
|
+
* once, made by that service, and `onSaveMessage` swaps the id of the message but never this. A made-up id too: until the page
|
|
241
|
+
* loads the history again (and the messages carry the real ids), it stands for the signed-in person.
|
|
242
|
+
*/
|
|
243
|
+
export const LOCAL_SERVICE_USER_ID = 'test_user';
|
|
244
|
+
/** Every id the chat makes up for a message of the signed-in person that the page has not told the real id of. */
|
|
245
|
+
const MADE_UP_USER_IDS = new Set([CURRENT_USER_PLACEHOLDER_ID, LOCAL_SERVICE_USER_ID]);
|
|
246
|
+
/**
|
|
247
|
+
* Finds the person behind a message. The team the page gave wins (it is the list the page keeps up to date); the
|
|
248
|
+
* signed-in person comes next, which is also who the made-up ids of a message that is not saved yet stand for.
|
|
249
|
+
* Null when nobody can be named: the message then has no face and no tooltip, instead of a wrong one.
|
|
250
|
+
*/
|
|
251
|
+
export function resolveChatMember(userId, members, currentUser) {
|
|
252
|
+
const standsForCurrentUser = userId != null && MADE_UP_USER_IDS.has(userId);
|
|
253
|
+
const wantedId = standsForCurrentUser ? currentUser?.id : userId;
|
|
254
|
+
if (!wantedId)
|
|
255
|
+
return null;
|
|
256
|
+
const teamMember = members.find((member) => member.id === wantedId);
|
|
257
|
+
if (teamMember)
|
|
258
|
+
return teamMember;
|
|
259
|
+
if (currentUser && currentUser.id === wantedId)
|
|
260
|
+
return currentUser;
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
/** The text the initials of an avatar come from: the name, or the part of the e-mail before the @. Empty when there is neither. */
|
|
264
|
+
export function memberDisplayName(member) {
|
|
265
|
+
const name = (member?.name ?? '').trim();
|
|
266
|
+
if (name)
|
|
267
|
+
return name;
|
|
268
|
+
const email = (member?.email ?? '').trim();
|
|
269
|
+
return email ? (email.split('@')[0] ?? '') : '';
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* The e-mail line of the tooltip over an avatar: the e-mail only, never a placeholder word. The same rule as the
|
|
273
|
+
* project header of the Widgetic site (`projectMemberTooltipEmail`), so a person sees the same thing in both places.
|
|
274
|
+
*/
|
|
275
|
+
export function memberTooltipEmail(email) {
|
|
276
|
+
const trimmed = (email ?? '').trim();
|
|
277
|
+
if (!trimmed)
|
|
278
|
+
return null;
|
|
279
|
+
if (trimmed.toLowerCase() === 'undefined')
|
|
280
|
+
return null;
|
|
281
|
+
return trimmed;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Where a click on an avatar goes: a `mailto:` link to the e-mail of the person, as the avatars of the project header of the
|
|
285
|
+
* Widgetic site do (`personMailtoHref`). Null when there is no e-mail to write to (the same rule as the tooltip's: never a
|
|
286
|
+
* placeholder word), and the avatar is then only a picture. The part before the @ is encoded (a `+` in it would otherwise be
|
|
287
|
+
* read as a space); the domain is left as it is.
|
|
288
|
+
*/
|
|
289
|
+
export function memberMailtoHref(email) {
|
|
290
|
+
const address = memberTooltipEmail(email);
|
|
291
|
+
if (!address)
|
|
292
|
+
return null;
|
|
293
|
+
const atIndex = address.indexOf('@');
|
|
294
|
+
if (atIndex < 0)
|
|
295
|
+
return `mailto:${encodeURIComponent(address)}`;
|
|
296
|
+
return `mailto:${encodeURIComponent(address.slice(0, atIndex))}@${address.slice(atIndex + 1)}`;
|
|
297
|
+
}
|
|
298
|
+
/** Whether the page told the chat anything about who is who: without it the chat draws no faces next to what people wrote. */
|
|
299
|
+
export function pageNamesPeople(members, currentUser) {
|
|
300
|
+
return members.length > 0 || Boolean(currentUser?.id);
|
|
301
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic of tight chat bubbles, apart from the page.
|
|
3
|
+
*
|
|
4
|
+
* CSS gives a bubble whose text wraps the whole `max-width`, however short its lines (it picks the width of a box before it
|
|
5
|
+
* breaks the lines), which leaves dead space beside them. The narrowest width that still wraps to the same number of lines has
|
|
6
|
+
* to be searched for, and that needs the text measured at several widths: what Pretext does with plain arithmetic, without
|
|
7
|
+
* touching the DOM. This file holds the search and a few small helpers, with no import of Pretext and nothing from the browser,
|
|
8
|
+
* so they run in Node and are tested there with a fake text engine. The browser side is `bubbleShrinkWrap.ts`.
|
|
9
|
+
*/
|
|
10
|
+
/** How a text wraps in a box of one width. */
|
|
11
|
+
export interface WrapStats {
|
|
12
|
+
/** How many lines the text makes. */
|
|
13
|
+
lineCount: number;
|
|
14
|
+
/** How wide the widest of those lines is, in px. */
|
|
15
|
+
maxLineWidth: number;
|
|
16
|
+
}
|
|
17
|
+
/** What the search needs to know about one text: Pretext's prepared text answers it, a fake one does in the tests. */
|
|
18
|
+
export interface BubbleTextEngine {
|
|
19
|
+
/** The width of the widest line when only the explicit line breaks apply (CSS `max-content`), in px. */
|
|
20
|
+
naturalWidth: number;
|
|
21
|
+
/** How the text wraps in a box this wide. */
|
|
22
|
+
measureWrap(width: number): WrapStats;
|
|
23
|
+
}
|
|
24
|
+
/** The width a bubble's text is given, and the number of lines it makes there. */
|
|
25
|
+
export interface TightTextWidth {
|
|
26
|
+
/** Whole px: the widest line of the narrowest wrapping that keeps the line count. */
|
|
27
|
+
width: number;
|
|
28
|
+
/** How many lines the text makes at that width. */
|
|
29
|
+
lineCount: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The narrowest width, in whole px, at which a text still wraps to the same number of lines it makes in `availableWidth`.
|
|
33
|
+
*
|
|
34
|
+
* Returns `null` when CSS cannot do better, so the bubble is left alone: there is no room to measure, the text does not wrap
|
|
35
|
+
* (CSS `fit-content` is already as wide as its widest line, and that is the case of most messages), or the measurements are not
|
|
36
|
+
* usable. A narrower width than the answer would add a line; a wider one would only add dead space.
|
|
37
|
+
*
|
|
38
|
+
* It is the algorithm of the Pretext "bubbles" demo: a binary search over the line count (more lines the narrower the box, never
|
|
39
|
+
* fewer), then the widest line at the width found.
|
|
40
|
+
*
|
|
41
|
+
* @param engine How the text wraps at a given width.
|
|
42
|
+
* @param availableWidth The room the text has when the bubble is as wide as it may be, in px (the content box CSS gives it).
|
|
43
|
+
*/
|
|
44
|
+
export declare function findTightTextWidth(engine: BubbleTextEngine, availableWidth: number): TightTextWidth | null;
|
|
45
|
+
/**
|
|
46
|
+
* The widths to try, in order, when the browser wraps a text in more lines than Pretext counted at `width`: the width itself,
|
|
47
|
+
* then a little wider (a pixel or two of difference in how the browser and the canvas measure a line), never past the room.
|
|
48
|
+
*/
|
|
49
|
+
export declare function widerWidthsToTry(width: number, maxWidth: number): number[];
|
|
50
|
+
/**
|
|
51
|
+
* How many lines a set of text boxes makes, from the top edge of each (the boxes of one line share a top, give or take the
|
|
52
|
+
* odd pixel of a link or a bold word in it).
|
|
53
|
+
*
|
|
54
|
+
* @param tops The `top` of each client rectangle of the text.
|
|
55
|
+
* @param tolerance How far two tops may be apart and still be the same line, in px.
|
|
56
|
+
*/
|
|
57
|
+
export declare function countLinesByTops(tops: readonly number[], tolerance?: number): number;
|
|
58
|
+
/** The parts of an element's computed style the canvas font string is made of. */
|
|
59
|
+
export interface FontParts {
|
|
60
|
+
fontStyle: string;
|
|
61
|
+
fontWeight: string;
|
|
62
|
+
fontSize: string;
|
|
63
|
+
fontFamily: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The canvas `font` shorthand Pretext measures with, from computed style: `italic 500 14px Inter, sans-serif`.
|
|
67
|
+
* (`getComputedStyle(...).font` is empty in some browsers, so the shorthand is put together from its parts.) An empty answer
|
|
68
|
+
* means the style cannot say, and the text is left to CSS.
|
|
69
|
+
*/
|
|
70
|
+
export declare function buildCanvasFont(parts: FontParts): string;
|
|
71
|
+
/**
|
|
72
|
+
* The letter spacing, in px, of a computed `letter-spacing` value: `normal` is none, `0.5px` is 0.5. Anything that is not a
|
|
73
|
+
* length in px (the browser always answers in px) counts as none.
|
|
74
|
+
*/
|
|
75
|
+
export declare function letterSpacingInPixels(computedLetterSpacing: string): number;
|