Skip to content

Commit 15255df

Browse files
committed
perf: switch firstUserMessage extraction to a more robust line-based JSON parsing
1 parent 7ec33d4 commit 15255df

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

packages/core/src/services/chatRecordingService.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,25 @@ export async function loadConversationRecord(
178178

179179
let firstUserMessage: string | undefined;
180180
// FAST PREVIEW: Find the first line that is a 'user' message and extract its content
181-
const userMsgMatch =
182-
headStr.match(
183-
/\{[^{}]*"type"\s*:\s*"user"[^{}]*"content"\s*:\s*(\[[^\]]*\]|"(?:[^"\\\\]|\\\\.)*")[^{}]*\}/,
184-
) ||
185-
headStr.match(
186-
/\{[^{}]*"content"\s*:\s*(\[[^\]]*\]|"(?:[^"\\\\]|\\\\.)*")[^{}]*"type"\s*:\s*"user"[^{}]*\}/,
181+
const userLine = headStr
182+
.split('\n')
183+
.find(
184+
(line) =>
185+
line.includes('"type":"user"') || line.includes('"type": "user"'),
187186
);
188187

189-
if (userMsgMatch) {
188+
if (userLine) {
190189
try {
191-
const content = JSON.parse(userMsgMatch[1]) as unknown;
192-
if (Array.isArray(content)) {
193-
firstUserMessage = content
194-
.map((p: unknown) => (isTextPart(p) ? p.text : ''))
195-
.join('');
196-
} else if (typeof content === 'string') {
197-
firstUserMessage = content;
190+
const record = JSON.parse(userLine) as unknown;
191+
if (hasProperty(record, 'content')) {
192+
const content = record.content;
193+
if (Array.isArray(content)) {
194+
firstUserMessage = content
195+
.map((p: unknown) => (isTextPart(p) ? p.text : ''))
196+
.join('');
197+
} else if (typeof content === 'string') {
198+
firstUserMessage = content;
199+
}
198200
}
199201
} catch {
200202
/* ignore */

0 commit comments

Comments
 (0)