Files
pipelines/app/lib/features/chat/domain/chat_find.dart
T
alan 9a6363b80e Chat: draw agent follow-ups and automatic continues apart from the operator
The server now names a user turn's origin (followup, auto_continue,
auto_switch, scheduled) on chat messages, user frames and queued prompts,
from the prompt's operation id while it holds the prompt and from its own
text prefixes in history read later. Origin is a new field, so older
clients keep treating these as ordinary user turns.

The app draws an agent follow-up, auto-continue and account-switch prompt
as a quiet outlined card on the agent's side with an icon, label and the
schedule facts from the preamble; the operator's own scheduled message
keeps its bubble with a small Scheduled mark. Find searches the drawn text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 15:40:34 +00:00

151 lines
4.7 KiB
Dart

import '../../../core/api_gateway.dart';
import 'chat_prompt_origin.dart';
/// One piece of transcript text find searches, as the transcript shows it:
/// a user turn's text (its text parts joined by line breaks, as its bubble
/// draws them) or one text part of an answer. [rowKey] is the transcript
/// row's stable key; [text] is exactly the string the row renders.
class ChatFindUnit {
const ChatFindUnit(this.rowIndex, this.rowKey, this.text, this.messageIds);
final int rowIndex;
final String rowKey;
final String text;
/// The stored messages the row holds.
final List<String> messageIds;
}
/// A match's character range in its unit's text.
typedef ChatFindRange = ({int start, int end});
/// One match in the loaded transcript.
class ChatFindMatch {
const ChatFindMatch(this.unit, this.start, this.end, this.ordinal);
final ChatFindUnit unit;
final int start;
final int end;
/// Which occurrence in [unit] this is, from its start.
final int ordinal;
String get rowKey => unit.rowKey;
/// Whether [other] is the same occurrence in the same text.
bool sameAs(ChatFindMatch other) =>
other.rowKey == rowKey &&
other.unit.text == unit.text &&
other.ordinal == ordinal;
}
/// A match the server found in a message the transcript has not loaded yet.
class ChatFindRemoteHit {
const ChatFindRemoteHit(this.messageId, this.part, this.offset);
final String messageId;
final int part;
final int offset;
/// The hits of a scoped chat search reply, newest message first.
static List<ChatFindRemoteHit> fromReply(JsonMap reply) {
final results = reply['search_results'];
final list = results is Map ? results['results'] : null;
if (list is! List) return const [];
return [
for (final result in list)
if (result is Map && result['chat'] is Map)
ChatFindRemoteHit(
'${(result['chat'] as Map)['message_id'] ?? ''}',
_int((result['chat'] as Map)['part']),
_int((result['chat'] as Map)['offset']),
),
];
}
static int _int(Object? value) => value is num ? value.toInt() : 0;
}
/// The text units of transcript [rows] (keyed by [keys]), in display order.
/// Only what reads as conversation is searched: user turns and the text of
/// answers, not activity, cards or notices.
List<ChatFindUnit> chatFindUnits(List<JsonMap> rows, List<String> keys) {
final units = <ChatFindUnit>[];
for (var index = 0; index < rows.length && index < keys.length; index++) {
final row = rows[index];
final kind = row['kind'];
if (kind == 'user') {
final message = row['message'] is Map ? row['message'] as Map : row;
final text = chatPromptDisplayText(
message,
_textParts(message).join('\n'),
);
if (text.trim().isEmpty) continue;
final id = '${row['key'] ?? message['id'] ?? ''}';
units.add(ChatFindUnit(index, keys[index], text, [id]));
} else if (kind == 'assistant') {
final ids = [
for (final id in row['messageIds'] as List? ?? const []) '$id',
];
for (final text in _textParts(row)) {
if (text.trim().isEmpty) continue;
units.add(ChatFindUnit(index, keys[index], text, ids));
}
}
}
return units;
}
List<String> _textParts(Map<dynamic, dynamic> source) {
final parts = source['parts'];
if (parts is! List) return const [];
return [
for (final part in parts)
if (part is Map &&
(part['type'] == null || part['type'] == 'text') &&
part['text'] is String)
part['text'] as String,
];
}
/// The non-overlapping occurrences of [query] in [text].
List<ChatFindRange> chatFindRanges(
String text,
String query, {
required bool caseSensitive,
}) {
if (query.isEmpty || text.length < query.length) return const [];
final pattern = RegExp(RegExp.escape(query), caseSensitive: caseSensitive);
return [
for (final match in pattern.allMatches(text))
(start: match.start, end: match.end),
];
}
/// Every match of [query] in [units], in display order (oldest first).
List<ChatFindMatch> chatFindMatches(
List<ChatFindUnit> units,
String query, {
required bool caseSensitive,
int cap = chatFindLocalCap,
}) {
final matches = <ChatFindMatch>[];
for (final unit in units) {
var ordinal = 0;
for (final range in chatFindRanges(
unit.text,
query,
caseSensitive: caseSensitive,
)) {
matches.add(ChatFindMatch(unit, range.start, range.end, ordinal++));
if (matches.length >= cap) return matches;
}
}
return matches;
}
/// How many matches find counts in the loaded transcript, and in history
/// it has not loaded, before it reports "N+".
const chatFindLocalCap = 2000;
const chatFindRemoteCap = 1000;