@gemies/sdk 10.8.0 → 10.10.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 +47 -0
- package/README.md +4 -0
- package/package.json +1 -1
- package/src/components/GemieChatContent.tsx +117 -27
- package/src/components/GemieComposer.tsx +25 -4
- package/src/components/diff/AnnotatedPatch.tsx +125 -0
- package/src/components/diff/DiffCommentCards.tsx +132 -0
- package/src/components/diff/DiffCommentChips.tsx +57 -0
- package/src/components/diff/FileChangeSection.tsx +12 -5
- package/src/components/diff/FileChangesViewer.tsx +21 -5
- package/src/components/diff/PatchLines.tsx +124 -13
- package/src/components/diff/diff-comments-context.ts +29 -0
- package/src/components/diff/diff-renderer.ts +17 -1
- 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 +52 -12
- package/src/components/thread/ThreadSearch.tsx +61 -78
- package/src/components/thread/ThreadTurns.tsx +94 -0
- package/src/components/thread/TurnDiffComments.tsx +52 -0
- package/src/hooks/useChatComposer.ts +20 -3
- 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/index.ts +8 -0
- package/src/lib/content-utils.ts +10 -0
- package/src/lib/copy-en.ts +26 -0
- package/src/lib/copy-es.ts +26 -0
- package/src/runtime/composer-draft-store.ts +43 -16
- package/src/runtime/model.ts +1 -0
- package/src/runtime/thread/diff-comments.ts +227 -0
- package/src/runtime/thread/message-processor.ts +58 -37
- package/src/runtime/thread/thread-find.ts +102 -0
- package/src/runtime/use-approval-actions.ts +7 -1
- package/src/runtime/use-conversation-thread.ts +10 -4
- package/src/runtime/use-start-conversation.ts +2 -1
- package/src/runtime/use-turn-edit.ts +5 -3
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { createRandomUuid } from '../random-uuid';
|
|
2
|
+
import { type PatchLine, patchLines } from './file-changes';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A comment a person leaves on lines of a change an agent made, and how it travels to the gemie.
|
|
6
|
+
*
|
|
7
|
+
* Comments are draft context: they live in the composer's draft until the next message is sent, and
|
|
8
|
+
* are then part of that message's own words, as one tagged block each after what the person wrote
|
|
9
|
+
* (`composeTurnText`). The model reads the file, the lines and the comment as text, so nothing on
|
|
10
|
+
* the server knows about them; the thread and the composer read the blocks back (`splitTurnText`),
|
|
11
|
+
* which is how a sent comment is drawn as a card and comes back as a chip when its turn is edited,
|
|
12
|
+
* or when its send fails.
|
|
13
|
+
*
|
|
14
|
+
* The selection, excerpt and block format follow the design of third-party code (see
|
|
15
|
+
* THIRD_PARTY_NOTICES.md).
|
|
16
|
+
*
|
|
17
|
+
* Pure.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Which side of a diff a line number counts in: the file before the change, or after it. */
|
|
21
|
+
export type DiffSide = 'old' | 'new';
|
|
22
|
+
|
|
23
|
+
/** One line of a patch, named by a side and its number there. */
|
|
24
|
+
export interface DiffLinePoint {
|
|
25
|
+
side: DiffSide;
|
|
26
|
+
line: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A run of a patch's lines, both ends included. */
|
|
30
|
+
export interface DiffLineRange {
|
|
31
|
+
from: DiffLinePoint;
|
|
32
|
+
to: DiffLinePoint;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The lines a comment is about, as a person names them: "lines 3–5", or removed ones. */
|
|
36
|
+
export interface DiffLineSpan {
|
|
37
|
+
side: DiffSide;
|
|
38
|
+
start: number;
|
|
39
|
+
end: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The lines picked in one file's patch. */
|
|
43
|
+
export interface DiffSelection {
|
|
44
|
+
/** Its ends in the patch's own order, each named by the side the line is on. */
|
|
45
|
+
range: DiffLineRange;
|
|
46
|
+
lines: DiffLineSpan;
|
|
47
|
+
/** The picked lines of the diff, each behind its `+`, `-` or space. */
|
|
48
|
+
snippet: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface DiffComment extends DiffSelection {
|
|
52
|
+
id: string;
|
|
53
|
+
path: string;
|
|
54
|
+
text: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The longest run one comment covers; a longer selection is cut to its first lines. */
|
|
58
|
+
export const DIFF_COMMENT_MAX_LINES = 200;
|
|
59
|
+
|
|
60
|
+
/** The longest comment the editor takes. */
|
|
61
|
+
export const DIFF_COMMENT_MAX_CHARS = 4000;
|
|
62
|
+
|
|
63
|
+
/** A line of the file, which a hunk header and a note are not. */
|
|
64
|
+
export type DiffCodeLine = Extract<PatchLine, { kind: 'context' | 'add' | 'delete' }>;
|
|
65
|
+
|
|
66
|
+
const isCodeLine = (line: PatchLine): line is DiffCodeLine =>
|
|
67
|
+
line.kind !== 'hunk' && line.kind !== 'note';
|
|
68
|
+
|
|
69
|
+
/** The point a line is named by: a removed line has only its old number. */
|
|
70
|
+
export function diffLinePoint(line: DiffCodeLine): DiffLinePoint {
|
|
71
|
+
return line.kind === 'delete'
|
|
72
|
+
? { side: 'old', line: line.oldLine ?? 0 }
|
|
73
|
+
: { side: 'new', line: line.newLine ?? 0 };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const isAt = (line: DiffCodeLine, point: DiffLinePoint): boolean =>
|
|
77
|
+
point.side === 'old'
|
|
78
|
+
? line.kind !== 'add' && line.oldLine === point.line
|
|
79
|
+
: line.kind !== 'delete' && line.newLine === point.line;
|
|
80
|
+
|
|
81
|
+
const SIGN = { add: '+', delete: '-', context: ' ' } as const;
|
|
82
|
+
|
|
83
|
+
/** The picked lines' numbers: the new file's where any of them is in it, else the old file's. */
|
|
84
|
+
function spanOf(lines: readonly DiffCodeLine[]): DiffLineSpan {
|
|
85
|
+
const inNew = lines.flatMap((line) => (line.newLine === undefined ? [] : [line.newLine]));
|
|
86
|
+
const side: DiffSide = inNew.length > 0 ? 'new' : 'old';
|
|
87
|
+
const numbers = side === 'new' ? inNew : lines.map((line) => line.oldLine ?? 0);
|
|
88
|
+
return { side, start: Math.min(...numbers), end: Math.max(...numbers) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The lines of `patch` between two points, in either order; `null` when a point is not a line of
|
|
93
|
+
* this patch.
|
|
94
|
+
*/
|
|
95
|
+
export function selectDiffLines(patch: string, range: DiffLineRange): DiffSelection | null {
|
|
96
|
+
const lines = patchLines(patch).filter(isCodeLine);
|
|
97
|
+
const first = lines.findIndex((line) => isAt(line, range.from));
|
|
98
|
+
const second = lines.findIndex((line) => isAt(line, range.to));
|
|
99
|
+
if (first < 0 || second < 0) return null;
|
|
100
|
+
const start = Math.min(first, second);
|
|
101
|
+
const end = Math.min(Math.max(first, second), start + DIFF_COMMENT_MAX_LINES - 1);
|
|
102
|
+
const picked = lines.slice(start, end + 1);
|
|
103
|
+
const [head] = picked;
|
|
104
|
+
const tail = picked.at(-1);
|
|
105
|
+
if (head === undefined || tail === undefined) return null;
|
|
106
|
+
return {
|
|
107
|
+
range: { from: diffLinePoint(head), to: diffLinePoint(tail) },
|
|
108
|
+
lines: spanOf(picked),
|
|
109
|
+
snippet: picked.map((line) => `${SIGN[line.kind]}${line.text}`).join('\n'),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The comments of `comments` that are about lines this patch of `path` still holds, as written. */
|
|
114
|
+
export function diffCommentsOn(
|
|
115
|
+
comments: readonly DiffComment[],
|
|
116
|
+
path: string,
|
|
117
|
+
patch: string,
|
|
118
|
+
): DiffComment[] {
|
|
119
|
+
return comments.filter(
|
|
120
|
+
(comment) =>
|
|
121
|
+
comment.path === path && selectDiffLines(patch, comment.range)?.snippet === comment.snippet,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** A comment on a selection of `path`'s diff. */
|
|
126
|
+
export function createDiffComment(
|
|
127
|
+
path: string,
|
|
128
|
+
selection: DiffSelection,
|
|
129
|
+
text: string,
|
|
130
|
+
): DiffComment {
|
|
131
|
+
return { ...selection, id: createRandomUuid(), path, text: text.trim() };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const ATTRIBUTE_ENTITIES = [
|
|
135
|
+
['&', '&'],
|
|
136
|
+
['"', '"'],
|
|
137
|
+
['<', '<'],
|
|
138
|
+
['>', '>'],
|
|
139
|
+
['\n', ' '],
|
|
140
|
+
] as const;
|
|
141
|
+
|
|
142
|
+
const escapeAttribute = (value: string): string =>
|
|
143
|
+
ATTRIBUTE_ENTITIES.reduce((out, [raw, entity]) => out.replaceAll(raw, entity), value);
|
|
144
|
+
|
|
145
|
+
// Read back in reverse, `&` last, so a path that spells an entity keeps it.
|
|
146
|
+
const unescapeAttribute = (value: string): string =>
|
|
147
|
+
ATTRIBUTE_ENTITIES.reduceRight((out, [raw, entity]) => out.replaceAll(entity, raw), value);
|
|
148
|
+
|
|
149
|
+
/** A comment that spells the closing tag keeps it as words: one backslash in, one out. */
|
|
150
|
+
const escapeText = (text: string): string =>
|
|
151
|
+
text.replace(/<(\\*)\/diff_comment/g, '<$1\\/diff_comment');
|
|
152
|
+
const unescapeText = (text: string): string =>
|
|
153
|
+
text.replace(/<\\(\\*)\/diff_comment/g, '<$1/diff_comment');
|
|
154
|
+
|
|
155
|
+
/** Longer than any run of backticks in the snippet, so nothing in it closes the fence. */
|
|
156
|
+
function fenceFor(snippet: string): string {
|
|
157
|
+
const longest = Math.max(0, ...Array.from(snippet.matchAll(/`+/g), (run) => run[0].length));
|
|
158
|
+
return '`'.repeat(Math.max(3, longest + 1));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const point = ({ side, line }: DiffLinePoint) => `${side}:${line}`;
|
|
162
|
+
|
|
163
|
+
function commentBlock(comment: DiffComment): string {
|
|
164
|
+
const { lines, range } = comment;
|
|
165
|
+
const fence = fenceFor(comment.snippet);
|
|
166
|
+
return [
|
|
167
|
+
`<diff_comment file="${escapeAttribute(comment.path)}" lines="${lines.side}:${lines.start}-${lines.end}" from="${point(range.from)}" to="${point(range.to)}">`,
|
|
168
|
+
`${fence}diff`,
|
|
169
|
+
comment.snippet,
|
|
170
|
+
fence,
|
|
171
|
+
escapeText(comment.text),
|
|
172
|
+
'</diff_comment>',
|
|
173
|
+
].join('\n');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The words of a message that carries `comments`: what the person wrote, then a block for each. */
|
|
177
|
+
export function composeTurnText(words: string, comments: readonly DiffComment[]): string {
|
|
178
|
+
if (comments.length === 0) return words;
|
|
179
|
+
return [...(words === '' ? [] : [words]), ...comments.map(commentBlock)].join('\n\n');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const BLOCK =
|
|
183
|
+
/<diff_comment file="([^"\n]*)" lines="(old|new):(\d+)-(\d+)" from="(old|new):(\d+)" to="(old|new):(\d+)">\n(`{3,})diff\n([\s\S]*?)\n\9\n([\s\S]*?)\n<\/diff_comment>(?:\n\n|\s*$)/y;
|
|
184
|
+
|
|
185
|
+
/** Every block from `start` to the end of `content`, or `null` when something else is there too. */
|
|
186
|
+
function blocksFrom(content: string, start: number): DiffComment[] | null {
|
|
187
|
+
const block = new RegExp(BLOCK);
|
|
188
|
+
block.lastIndex = start;
|
|
189
|
+
const comments: DiffComment[] = [];
|
|
190
|
+
while (block.lastIndex < content.length) {
|
|
191
|
+
const match = block.exec(content);
|
|
192
|
+
if (match === null) return null;
|
|
193
|
+
const [, path, side, first, last, fromSide, from, toSide, to, , snippet, text] =
|
|
194
|
+
match as unknown as string[];
|
|
195
|
+
comments.push({
|
|
196
|
+
id: createRandomUuid(),
|
|
197
|
+
path: unescapeAttribute(path as string),
|
|
198
|
+
lines: { side: side as DiffSide, start: Number(first), end: Number(last) },
|
|
199
|
+
range: {
|
|
200
|
+
from: { side: fromSide as DiffSide, line: Number(from) },
|
|
201
|
+
to: { side: toSide as DiffSide, line: Number(to) },
|
|
202
|
+
},
|
|
203
|
+
snippet: snippet as string,
|
|
204
|
+
text: unescapeText(text as string),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return comments;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* A message's words and the comments it carries: the inverse of `composeTurnText`, named as a
|
|
212
|
+
* composer's draft names them. Only blocks at the end of the message count; a tag anywhere else is
|
|
213
|
+
* something the person wrote.
|
|
214
|
+
*/
|
|
215
|
+
export function splitTurnText(content: string): { text: string; diffComments: DiffComment[] } {
|
|
216
|
+
let start = content.indexOf('<diff_comment ');
|
|
217
|
+
while (start >= 0) {
|
|
218
|
+
if (start === 0 || content.slice(start - 2, start) === '\n\n') {
|
|
219
|
+
const diffComments = blocksFrom(content, start);
|
|
220
|
+
if (diffComments !== null && diffComments.length > 0) {
|
|
221
|
+
return { text: content.slice(0, Math.max(0, start - 2)), diffComments };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
start = content.indexOf('<diff_comment ', start + 1);
|
|
225
|
+
}
|
|
226
|
+
return { text: content, diffComments: [] };
|
|
227
|
+
}
|
|
@@ -16,25 +16,71 @@ export interface ProcessedMessage {
|
|
|
16
16
|
metadata?: Record<string, any>;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
/*
|
|
20
|
+
* A message is text anyone can write, and it is processed as the thread draws it, so every pattern
|
|
21
|
+
* here reads it once: a tag's name and attributes never hold another `[`, which makes many unclosed
|
|
22
|
+
* tags cost no more than one.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** A tag that stands alone: `[NAME …]`, with or without the `ALIA_` prefix. */
|
|
26
|
+
const tag = (rest: string) => new RegExp(String.raw`\[(?:ALIA_)?${rest}`, 'g');
|
|
27
|
+
|
|
28
|
+
const REACT = tag(String.raw`REACT:[^[\]]+\]\s*`);
|
|
29
|
+
const TGIMAGE = tag(String.raw`TGIMAGE[^[\]]*\]\s*`);
|
|
30
|
+
const TGDOC = tag(String.raw`TGDOC[^[\]]*\]\s*`);
|
|
31
|
+
const IMAGE = tag(String.raw`IMAGE[^[\]]*\]`);
|
|
32
|
+
const CREDIBILITY = tag(String.raw`CREDIBILITY[^[\]]*\]`);
|
|
33
|
+
|
|
34
|
+
/** The tags that open and close a `[NAME …]…[/NAME]` block. */
|
|
35
|
+
const markers = (name: string) =>
|
|
36
|
+
new RegExp(String.raw`\[(?:ALIA_)?${name}[^[\]]*\]|\[\/(?:ALIA_)?${name}\]`, 'g');
|
|
37
|
+
|
|
38
|
+
const TGLINKS = markers('TGLINKS');
|
|
39
|
+
const VISUAL_BLOCKS = ['COMPACTLIST', 'BANNER', 'COMPARISON', 'TIMELINE'].map(markers);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* `content` without its blocks of one kind and the space after each: from an opening tag to the
|
|
43
|
+
* first closing one. A tag with no partner stays as written.
|
|
44
|
+
*/
|
|
45
|
+
function withoutBlocks(content: string, blockMarkers: RegExp): string {
|
|
46
|
+
let kept = '';
|
|
47
|
+
let from = 0;
|
|
48
|
+
let opened = -1;
|
|
49
|
+
for (const marker of content.matchAll(blockMarkers)) {
|
|
50
|
+
if (!marker[0].startsWith('[/')) {
|
|
51
|
+
if (opened < 0) opened = marker.index;
|
|
52
|
+
} else if (opened >= 0) {
|
|
53
|
+
kept += content.slice(from, opened);
|
|
54
|
+
const space = /\s*/y;
|
|
55
|
+
space.lastIndex = marker.index + marker[0].length;
|
|
56
|
+
space.exec(content);
|
|
57
|
+
from = space.lastIndex;
|
|
58
|
+
opened = -1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return kept + content.slice(from);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Without the tags only Telegram acts on. */
|
|
65
|
+
const withoutTelegramTags = (content: string) =>
|
|
66
|
+
withoutBlocks(content.replace(REACT, '').replace(TGIMAGE, ''), TGLINKS).replace(TGDOC, '');
|
|
67
|
+
|
|
68
|
+
/** Without the visual components only the app draws. */
|
|
69
|
+
const withoutAppComponents = (content: string) =>
|
|
70
|
+
VISUAL_BLOCKS.reduce(withoutBlocks, content).replace(IMAGE, '').replace(CREDIBILITY, '');
|
|
71
|
+
|
|
72
|
+
const withoutTitle = (content: string) => content.replace(TITLE_STRIP_RE, '').trim();
|
|
73
|
+
|
|
19
74
|
/**
|
|
20
75
|
* Process a message for the Web/Mobile App
|
|
21
76
|
* - Removes Telegram-specific tags ([REACT], [TGIMAGE], [TGLINKS], [TGDOC])
|
|
22
77
|
* - Preserves app-specific components ([COMPACTLIST], [BANNER], [COMPARISON], etc.)
|
|
23
78
|
*/
|
|
24
79
|
function processForApp(content: string): ProcessedMessage {
|
|
25
|
-
// Remove Telegram-specific tags and title tags
|
|
26
|
-
const cleanedText = content
|
|
27
|
-
.replace(/\[(?:ALIA_)?REACT:[^\]]+\]\s*/g, '')
|
|
28
|
-
.replace(/\[(?:ALIA_)?TGIMAGE[^\]]*\]\s*/g, '')
|
|
29
|
-
.replace(/\[(?:ALIA_)?TGLINKS[^\]]*\][\s\S]*?\[\/(?:ALIA_)?TGLINKS\]\s*/g, '')
|
|
30
|
-
.replace(/\[(?:ALIA_)?TGDOC[^\]]*\]\s*/g, '')
|
|
31
|
-
.replace(TITLE_STRIP_RE, '')
|
|
32
|
-
.trim();
|
|
33
|
-
|
|
34
80
|
// App-specific components like [COMPACTLIST], [BANNER], etc. are kept
|
|
35
81
|
// They will be rendered by the Markdown component
|
|
36
82
|
return {
|
|
37
|
-
text:
|
|
83
|
+
text: withoutTitle(withoutTelegramTags(content)),
|
|
38
84
|
components: [], // Could extract components here if needed
|
|
39
85
|
};
|
|
40
86
|
}
|
|
@@ -45,21 +91,10 @@ function processForApp(content: string): ProcessedMessage {
|
|
|
45
91
|
* - Preserves Telegram-specific tags for bot processing
|
|
46
92
|
*/
|
|
47
93
|
function processForTelegram(content: string): ProcessedMessage {
|
|
48
|
-
// Remove app-specific visual components (not supported in Telegram)
|
|
49
|
-
const cleanedText = content
|
|
50
|
-
.replace(/\[(?:ALIA_)?COMPACTLIST[^\]]*\][\s\S]*?\[\/(?:ALIA_)?COMPACTLIST\]\s*/g, '')
|
|
51
|
-
.replace(/\[(?:ALIA_)?BANNER[^\]]*\][\s\S]*?\[\/(?:ALIA_)?BANNER\]\s*/g, '')
|
|
52
|
-
.replace(/\[(?:ALIA_)?COMPARISON[^\]]*\][\s\S]*?\[\/(?:ALIA_)?COMPARISON\]\s*/g, '')
|
|
53
|
-
.replace(/\[(?:ALIA_)?TIMELINE[^\]]*\][\s\S]*?\[\/(?:ALIA_)?TIMELINE\]\s*/g, '')
|
|
54
|
-
.replace(/\[(?:ALIA_)?IMAGE[^\]]*\]/g, '')
|
|
55
|
-
.replace(/\[(?:ALIA_)?CREDIBILITY[^\]]*\]/g, '')
|
|
56
|
-
.replace(TITLE_STRIP_RE, '')
|
|
57
|
-
.trim();
|
|
58
|
-
|
|
59
94
|
// Telegram tags like [REACT], [TGIMAGE], etc. are kept
|
|
60
95
|
// They will be processed by the Telegram bot
|
|
61
96
|
return {
|
|
62
|
-
text:
|
|
97
|
+
text: withoutTitle(withoutAppComponents(content)),
|
|
63
98
|
};
|
|
64
99
|
}
|
|
65
100
|
|
|
@@ -74,20 +109,6 @@ export function processMessage(content: string, platform: Platform): ProcessedMe
|
|
|
74
109
|
return processForTelegram(content);
|
|
75
110
|
default:
|
|
76
111
|
// Fallback: remove all special tags
|
|
77
|
-
return {
|
|
78
|
-
text: content
|
|
79
|
-
.replace(/\[(?:ALIA_)?REACT:[^\]]+\]\s*/g, '')
|
|
80
|
-
.replace(/\[(?:ALIA_)?TGIMAGE[^\]]*\]\s*/g, '')
|
|
81
|
-
.replace(/\[(?:ALIA_)?TGLINKS[^\]]*\][\s\S]*?\[\/(?:ALIA_)?TGLINKS\]\s*/g, '')
|
|
82
|
-
.replace(/\[(?:ALIA_)?TGDOC[^\]]*\]\s*/g, '')
|
|
83
|
-
.replace(/\[(?:ALIA_)?COMPACTLIST[^\]]*\][\s\S]*?\[\/(?:ALIA_)?COMPACTLIST\]\s*/g, '')
|
|
84
|
-
.replace(/\[(?:ALIA_)?BANNER[^\]]*\][\s\S]*?\[\/(?:ALIA_)?BANNER\]\s*/g, '')
|
|
85
|
-
.replace(/\[(?:ALIA_)?COMPARISON[^\]]*\][\s\S]*?\[\/(?:ALIA_)?COMPARISON\]\s*/g, '')
|
|
86
|
-
.replace(/\[(?:ALIA_)?TIMELINE[^\]]*\][\s\S]*?\[\/(?:ALIA_)?TIMELINE\]\s*/g, '')
|
|
87
|
-
.replace(/\[(?:ALIA_)?IMAGE[^\]]*\]/g, '')
|
|
88
|
-
.replace(/\[(?:ALIA_)?CREDIBILITY[^\]]*\]/g, '')
|
|
89
|
-
.replace(TITLE_STRIP_RE, '')
|
|
90
|
-
.trim(),
|
|
91
|
-
};
|
|
112
|
+
return { text: withoutTitle(withoutAppComponents(withoutTelegramTags(content))) };
|
|
92
113
|
}
|
|
93
114
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { messageText, questionParts } from '../../lib/content-utils';
|
|
2
|
+
import type { Message } from '../types';
|
|
3
|
+
import { fileChangeName } from './file-changes';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Finding words in the open thread, and the turns it can be jumped between, as data.
|
|
7
|
+
*
|
|
8
|
+
* Both read only the rows already loaded, which includes those above the mounted window
|
|
9
|
+
* (`timeline.ts`) but not history the thread has not paged in yet.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** A turn of the thread, as the jump list names it: the person's message that opened it. */
|
|
13
|
+
export interface ThreadTurn {
|
|
14
|
+
/** The id of the person's message, which is the row a jump lands on. */
|
|
15
|
+
id: string;
|
|
16
|
+
/** Its first words, on one line. Empty for a turn that was only attachments. */
|
|
17
|
+
preview: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** How much of a question the jump list shows. */
|
|
21
|
+
const PREVIEW_LENGTH = 120;
|
|
22
|
+
|
|
23
|
+
const FENCE = /^[ \t]*(```|~~~).*$/;
|
|
24
|
+
const BLOCK_TAG =
|
|
25
|
+
/\[\/?(?:ALIA_)?(?:COMPACTLIST|BANNER|COMPARISON|TIMELINE|IMAGE|CREDIBILITY)\b[^\]]*\]/g;
|
|
26
|
+
|
|
27
|
+
/** One line of Markdown without the marks a reader never sees. */
|
|
28
|
+
function stripInline(line: string): string {
|
|
29
|
+
return (
|
|
30
|
+
line
|
|
31
|
+
.replace(/^[ \t]*(?:#{1,6}[ \t]+|>[ \t]?|[-*+][ \t]+|\d+\.[ \t]+)/, '')
|
|
32
|
+
// An image or a link reads as its text; the address is not on screen.
|
|
33
|
+
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
34
|
+
.replace(/(\*\*|__|~~)(?=\S)(.+?)\1/g, '$2')
|
|
35
|
+
// Single marks only around a whole run, so `snake_case` and `2 * 3` stay as written.
|
|
36
|
+
.replace(/(?<![\w*_])([*_])(?!\s)([^*_\n]+?)\1(?![\w*_])/g, '$2')
|
|
37
|
+
.replace(/`([^`]*)`/g, '$1')
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A reply's Markdown as the words it draws. Code inside a fence is kept exactly. */
|
|
42
|
+
function withoutMarkdown(source: string): string {
|
|
43
|
+
const out: string[] = [];
|
|
44
|
+
let fenced = false;
|
|
45
|
+
for (const line of source.replace(BLOCK_TAG, ' ').split('\n')) {
|
|
46
|
+
if (FENCE.test(line)) fenced = !fenced;
|
|
47
|
+
else out.push(fenced ? line : stripInline(line));
|
|
48
|
+
}
|
|
49
|
+
return out.join('\n');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const visible = new WeakMap<Message, string>();
|
|
53
|
+
|
|
54
|
+
/** A question's words, then what the cards of its diff comments say: each file and comment. */
|
|
55
|
+
function questionWords(message: Message): string {
|
|
56
|
+
const { text, diffComments } = questionParts(message);
|
|
57
|
+
return [text, ...diffComments.flatMap((c) => [fileChangeName(c.path), c.text])].join(' ');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The words a row shows: a question as typed, with its diff comments, a reply without its Markdown.
|
|
62
|
+
* Remembered per message object, which a streamed token replaces, so an open find re-reads only the
|
|
63
|
+
* reply being written.
|
|
64
|
+
*/
|
|
65
|
+
export function visibleText(message: Message): string {
|
|
66
|
+
let text = visible.get(message);
|
|
67
|
+
if (text === undefined) {
|
|
68
|
+
const words =
|
|
69
|
+
message.role === 'assistant' ? withoutMarkdown(messageText(message)) : questionWords(message);
|
|
70
|
+
text = words.replace(/\s+/g, ' ').trim();
|
|
71
|
+
visible.set(message, text);
|
|
72
|
+
}
|
|
73
|
+
return text;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Case and accents are not what a person is looking for: "informacion" finds "Información". */
|
|
77
|
+
function fold(text: string): string {
|
|
78
|
+
return text
|
|
79
|
+
.normalize('NFD')
|
|
80
|
+
.replace(/\p{Mark}/gu, '')
|
|
81
|
+
.toLowerCase();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The ids of the messages whose visible words contain `query`, in reading order. */
|
|
85
|
+
export function findMatches(rows: readonly Message[], query: string): string[] {
|
|
86
|
+
const wanted = fold(query.replace(/\s+/g, ' ').trim());
|
|
87
|
+
if (wanted === '') return [];
|
|
88
|
+
return rows.filter((row) => fold(visibleText(row)).includes(wanted)).map((row) => row.id);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The index `delta` steps from `index` among `count` matches, wrapping at both ends. */
|
|
92
|
+
export function stepMatch(index: number, delta: 1 | -1, count: number): number {
|
|
93
|
+
if (count === 0) return -1;
|
|
94
|
+
return (index + delta + count) % count;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The thread's turns, one per message of the person's, in reading order. */
|
|
98
|
+
export function threadTurns(rows: readonly Message[]): ThreadTurn[] {
|
|
99
|
+
return rows
|
|
100
|
+
.filter((row) => row.role === 'user')
|
|
101
|
+
.map((row) => ({ id: row.id, preview: visibleText(row).slice(0, PREVIEW_LENGTH) }));
|
|
102
|
+
}
|
|
@@ -26,6 +26,8 @@ export interface ApprovalActions {
|
|
|
26
26
|
readonly canAnswer: boolean;
|
|
27
27
|
/** Questions whose answer is on its way: nothing is offered for them until it arrives. */
|
|
28
28
|
readonly sending: ReadonlySet<string>;
|
|
29
|
+
/** Those of `sending` whose answer carries words for the agent: a reply steering the turn. */
|
|
30
|
+
readonly steering: ReadonlySet<string>;
|
|
29
31
|
/** Questions whose answer could not be sent: offered again, with a line saying so. */
|
|
30
32
|
readonly failed: ReadonlySet<string>;
|
|
31
33
|
/** The oldest question still waiting: the one the bar and the shortcuts are about. */
|
|
@@ -81,6 +83,7 @@ export function useApprovalActions({
|
|
|
81
83
|
grants: source,
|
|
82
84
|
}: UseApprovalActionsInput): ApprovalActions {
|
|
83
85
|
const [sending, setSending] = useState<ReadonlySet<string>>(() => new Set());
|
|
86
|
+
const [steering, setSteering] = useState<ReadonlySet<string>>(() => new Set());
|
|
84
87
|
const [failed, setFailed] = useState<ReadonlySet<string>>(() => new Set());
|
|
85
88
|
// The guard is a ref: two presses in one frame both see the same render's state.
|
|
86
89
|
const inFlight = useRef(new Set<string>());
|
|
@@ -91,6 +94,7 @@ export function useApprovalActions({
|
|
|
91
94
|
if (!onDecide || entry.outcome !== 'pending' || inFlight.current.has(id)) return false;
|
|
92
95
|
inFlight.current.add(id);
|
|
93
96
|
setSending((s) => mark(s, id, true));
|
|
97
|
+
if (details?.feedback) setSteering((s) => mark(s, id, true));
|
|
94
98
|
setFailed((f) => mark(f, id, false));
|
|
95
99
|
try {
|
|
96
100
|
await onDecide(entry, decision, details);
|
|
@@ -104,6 +108,7 @@ export function useApprovalActions({
|
|
|
104
108
|
} finally {
|
|
105
109
|
inFlight.current.delete(id);
|
|
106
110
|
setSending((s) => mark(s, id, false));
|
|
111
|
+
setSteering((s) => mark(s, id, false));
|
|
107
112
|
}
|
|
108
113
|
},
|
|
109
114
|
[onDecide, onSettled, onStopTurn],
|
|
@@ -151,12 +156,13 @@ export function useApprovalActions({
|
|
|
151
156
|
agentName,
|
|
152
157
|
canAnswer: onDecide !== undefined,
|
|
153
158
|
sending,
|
|
159
|
+
steering,
|
|
154
160
|
failed,
|
|
155
161
|
pending,
|
|
156
162
|
grants,
|
|
157
163
|
revokeGrant,
|
|
158
164
|
decide,
|
|
159
165
|
}),
|
|
160
|
-
[entries, agentName, onDecide, sending, failed, pending, grants, revokeGrant, decide],
|
|
166
|
+
[entries, agentName, onDecide, sending, steering, failed, pending, grants, revokeGrant, decide],
|
|
161
167
|
);
|
|
162
168
|
}
|
|
@@ -13,6 +13,7 @@ import { useDocumentUpload } from './oxy-files';
|
|
|
13
13
|
import { useGemieRuntime } from './config';
|
|
14
14
|
import { releaseAttachments, useComposerDraftStore } from './composer-draft-store';
|
|
15
15
|
import { useConversationStore } from './conversation-store';
|
|
16
|
+
import { splitTurnText } from './thread/diff-comments';
|
|
16
17
|
import {
|
|
17
18
|
useClearConversation,
|
|
18
19
|
useConversation,
|
|
@@ -219,7 +220,10 @@ export function useConversationThread({
|
|
|
219
220
|
const lastUserText = useMemo(() => {
|
|
220
221
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
221
222
|
const message = messages[i];
|
|
222
|
-
|
|
223
|
+
// The sidebar row reads the person's words, not the blocks of their diff comments.
|
|
224
|
+
if (message?.role === 'user') {
|
|
225
|
+
return splitTurnText(getTextFromContent(message.content)).text;
|
|
226
|
+
}
|
|
223
227
|
}
|
|
224
228
|
return '';
|
|
225
229
|
}, [messages]);
|
|
@@ -315,7 +319,7 @@ export function useConversationThread({
|
|
|
315
319
|
// A failed delete still leaves the user whole — it only leaves the row.
|
|
316
320
|
await deleteConversation(conversationId).catch(() => {});
|
|
317
321
|
useComposerDraftStore.getState().restore(newChatDraft, {
|
|
318
|
-
|
|
322
|
+
...splitTurnText(pending.text),
|
|
319
323
|
attachments: pending.attachments,
|
|
320
324
|
mcpServerId: pending.mcpServerId,
|
|
321
325
|
skillNames: pending.skillNames,
|
|
@@ -361,7 +365,7 @@ export function useConversationThread({
|
|
|
361
365
|
// and the composer stays clear because the text is already on screen.
|
|
362
366
|
if (outcome === 'failed') {
|
|
363
367
|
useComposerDraftStore.getState().restore(draft, {
|
|
364
|
-
|
|
368
|
+
...splitTurnText(content),
|
|
365
369
|
attachments: attachments ?? [],
|
|
366
370
|
mcpServerId: options?.mcpServerId ?? null,
|
|
367
371
|
skillNames: options?.skillNames ?? [],
|
|
@@ -465,7 +469,9 @@ export function useConversationThread({
|
|
|
465
469
|
if (outcome === 'failed') {
|
|
466
470
|
setMessages(beforeEdit);
|
|
467
471
|
useComposerDraftStore.getState().restore(draft, {
|
|
468
|
-
|
|
472
|
+
...splitTurnText(
|
|
473
|
+
typeof newContent === 'string' ? newContent : getTextFromContent(content),
|
|
474
|
+
),
|
|
469
475
|
attachments: attachments ?? [],
|
|
470
476
|
mcpServerId: options?.mcpServerId ?? null,
|
|
471
477
|
skillNames: options?.skillNames ?? [],
|
|
@@ -8,6 +8,7 @@ import { useComposerDraftStore, type DraftTarget } from './composer-draft-store'
|
|
|
8
8
|
import { useGemieRuntime } from './config';
|
|
9
9
|
import { useConversationStore } from './conversation-store';
|
|
10
10
|
import { useCreateConversation } from './conversations';
|
|
11
|
+
import { splitTurnText } from './thread/diff-comments';
|
|
11
12
|
import type { SendOptions } from './types';
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -83,7 +84,7 @@ export function useStartConversation({ agentId }: { agentId?: string } = {}) {
|
|
|
83
84
|
const conversation = await createConversation({ agentId }).catch(() => undefined);
|
|
84
85
|
if (conversation === undefined) {
|
|
85
86
|
useComposerDraftStore.getState().restore(draft, {
|
|
86
|
-
text,
|
|
87
|
+
...splitTurnText(text),
|
|
87
88
|
attachments,
|
|
88
89
|
mcpServerId: options?.mcpServerId ?? null,
|
|
89
90
|
skillNames: options?.skillNames ?? [],
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useCallback, useRef } from 'react';
|
|
2
2
|
import { getTextFromContent } from '../lib/content-utils';
|
|
3
|
+
import { splitTurnText } from './thread/diff-comments';
|
|
3
4
|
import type { Message, SendOptions } from './types';
|
|
4
5
|
import type { GemieAttachment } from '../types';
|
|
5
6
|
import { useComposerDraft, useComposerDraftStore, type DraftTarget } from './composer-draft-store';
|
|
@@ -28,8 +29,9 @@ export interface TurnEditOptions {
|
|
|
28
29
|
* chat's draft now (`composer-draft-store.ts`), like the text it is made of:
|
|
29
30
|
* per account and per conversation, and gone when the draft is.
|
|
30
31
|
*
|
|
31
|
-
* - `start` loads the question's words
|
|
32
|
-
* with into the composer, keeping aside what
|
|
32
|
+
* - `start` loads the question's words, the diff comments it carried and the
|
|
33
|
+
* connector and skills it was sent with into the composer, keeping aside what
|
|
34
|
+
* the composer held. A question
|
|
33
35
|
* this screen did not send has no record of its options, so the composer's
|
|
34
36
|
* own choice stays.
|
|
35
37
|
* - `cancel` gives the composer back what it held.
|
|
@@ -61,7 +63,7 @@ export function useTurnEdit({ target, messages, turnOptionsOf, onEditMessage }:
|
|
|
61
63
|
const sentWith = turnOptionsOf?.(messageId);
|
|
62
64
|
store.startEdit(store.address(target), {
|
|
63
65
|
messageId,
|
|
64
|
-
|
|
66
|
+
...splitTurnText(getTextFromContent(message.content)),
|
|
65
67
|
mcpServerId: sentWith?.mcpServerId ?? current?.mcpServerId ?? null,
|
|
66
68
|
skillNames: sentWith?.skillNames ?? current?.skillNames ?? [],
|
|
67
69
|
});
|