@superblocksteam/library-shared 2.0.160-next.0 → 2.0.161-next.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.
@@ -0,0 +1,752 @@
1
+ import { normalizeLegacyChatRole, TEXT_ATTACHMENT_MIME_TYPES, } from "./ai.js";
2
+ const SUPPORTED_CHAT_ROLES = ["user", "assistant", "system"];
3
+ function isNonNullObject(value) {
4
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5
+ }
6
+ function parsePersistedKnowledgeFacts(facts) {
7
+ if (!isNonNullObject(facts)) {
8
+ return null;
9
+ }
10
+ if (!Array.isArray(facts.org) || !Array.isArray(facts.user)) {
11
+ return null;
12
+ }
13
+ const integrations = {};
14
+ if (facts.integrations != null) {
15
+ if (!isNonNullObject(facts.integrations)) {
16
+ return null;
17
+ }
18
+ for (const [key, value] of Object.entries(facts.integrations)) {
19
+ if (!Array.isArray(value)) {
20
+ return null;
21
+ }
22
+ integrations[key] = value;
23
+ }
24
+ }
25
+ return {
26
+ org: facts.org,
27
+ user: facts.user,
28
+ integrations,
29
+ };
30
+ }
31
+ /**
32
+ * Required-field contract for a persisted row. Callers own their own logging;
33
+ * `reason` says which field failed.
34
+ */
35
+ export function parsePersistedChatMessageFields(payload) {
36
+ if (!payload || typeof payload !== "object") {
37
+ return { valid: false, reason: "payload" };
38
+ }
39
+ const message = payload;
40
+ if (typeof message.role !== "string") {
41
+ return { valid: false, reason: "role" };
42
+ }
43
+ const role = normalizeLegacyChatRole(message.role);
44
+ if (!SUPPORTED_CHAT_ROLES.includes(role)) {
45
+ return { valid: false, reason: "normalizedRole" };
46
+ }
47
+ if (typeof message.content !== "string") {
48
+ return { valid: false, reason: "content" };
49
+ }
50
+ if (typeof message.type !== "string") {
51
+ return { valid: false, reason: "type" };
52
+ }
53
+ return {
54
+ valid: true,
55
+ fields: { role, content: message.content, type: message.type },
56
+ };
57
+ }
58
+ const TEXT_LIKE_EXTENSIONS = [
59
+ ".txt",
60
+ ".text",
61
+ ".log",
62
+ ".md",
63
+ ".markdown",
64
+ ".csv",
65
+ ".tsv",
66
+ ".json",
67
+ ".jsonl",
68
+ ".ndjson",
69
+ ".xml",
70
+ ".yaml",
71
+ ".yml",
72
+ ".toml",
73
+ ".ini",
74
+ ".cfg",
75
+ ".conf",
76
+ ".env",
77
+ ".sh",
78
+ ".bash",
79
+ ".zsh",
80
+ ".js",
81
+ ".jsx",
82
+ ".ts",
83
+ ".tsx",
84
+ ".mts",
85
+ ".cts",
86
+ ".py",
87
+ ".go",
88
+ ".java",
89
+ ".rb",
90
+ ".php",
91
+ ".sql",
92
+ ".css",
93
+ ".scss",
94
+ ".html",
95
+ ".htm",
96
+ ];
97
+ export function inferTextAttachmentType(params) {
98
+ const attachmentType = typeof params.attachmentType === "string"
99
+ ? params.attachmentType.toLowerCase()
100
+ : "";
101
+ const mimeType = typeof params.mimeType === "string" ? params.mimeType.toLowerCase() : "";
102
+ const lowerFileName = (params.fileName ?? "").toLowerCase();
103
+ if (attachmentType === "csv" || mimeType === "text/csv") {
104
+ return "csv";
105
+ }
106
+ if (attachmentType === "json" || mimeType === "application/json") {
107
+ return "json";
108
+ }
109
+ if (attachmentType === "yaml" ||
110
+ mimeType === "text/yaml" ||
111
+ mimeType === "application/yaml" ||
112
+ mimeType === "application/x-yaml") {
113
+ return "yaml";
114
+ }
115
+ if (attachmentType === "css" || mimeType === "text/css") {
116
+ return "css";
117
+ }
118
+ if (attachmentType === "txt") {
119
+ return "txt";
120
+ }
121
+ if (lowerFileName.endsWith(".csv")) {
122
+ return "csv";
123
+ }
124
+ if (lowerFileName.endsWith(".json")) {
125
+ return "json";
126
+ }
127
+ if (lowerFileName.endsWith(".yaml") || lowerFileName.endsWith(".yml")) {
128
+ return "yaml";
129
+ }
130
+ if (lowerFileName.endsWith(".css")) {
131
+ return "css";
132
+ }
133
+ if (mimeType.startsWith("text/") ||
134
+ mimeType.includes("json") ||
135
+ mimeType.includes("xml") ||
136
+ mimeType.includes("yaml") ||
137
+ mimeType.includes("toml") ||
138
+ mimeType.includes("javascript") ||
139
+ mimeType.includes("typescript") ||
140
+ mimeType.includes("ecmascript") ||
141
+ mimeType.includes("markdown") ||
142
+ mimeType.includes("shellscript") ||
143
+ mimeType.includes("python") ||
144
+ mimeType.includes("ruby") ||
145
+ mimeType.includes("php") ||
146
+ mimeType.includes("sql") ||
147
+ TEXT_LIKE_EXTENSIONS.some((extension) => lowerFileName.endsWith(extension))) {
148
+ return "txt";
149
+ }
150
+ return null;
151
+ }
152
+ /***
153
+ * Validates and sanitizes the attachments array
154
+ * @param attachments - The attachments array to validate
155
+ * @returns A tuple containing:
156
+ * - a boolean indicating if the attachments are valid
157
+ * - the sanitized attachments array
158
+ */
159
+ function validateAttachments(attachments) {
160
+ if (!attachments) {
161
+ return [true, undefined];
162
+ }
163
+ if (typeof attachments !== "object" || !Array.isArray(attachments)) {
164
+ return [false, undefined];
165
+ }
166
+ const validAttachments = attachments
167
+ .map((attachment) => {
168
+ if (typeof attachment !== "object" || !("type" in attachment)) {
169
+ return undefined;
170
+ }
171
+ // Validate image attachment
172
+ if (attachment.type === "image") {
173
+ if (typeof attachment.image !== "string") {
174
+ return undefined;
175
+ }
176
+ return {
177
+ type: attachment.type,
178
+ image: attachment.image,
179
+ fileName: typeof attachment.fileName === "string"
180
+ ? attachment.fileName
181
+ : undefined,
182
+ };
183
+ }
184
+ // Validate PDF attachment
185
+ if (attachment.type === "pdf") {
186
+ if (typeof attachment.data !== "string") {
187
+ return undefined;
188
+ }
189
+ return {
190
+ type: attachment.type,
191
+ data: attachment.data,
192
+ fileName: typeof attachment.fileName === "string"
193
+ ? attachment.fileName
194
+ : undefined,
195
+ size: attachment.size,
196
+ };
197
+ }
198
+ // Validate archive attachment (zip, tgz, gz)
199
+ if (attachment.type === "archive") {
200
+ if (typeof attachment.data !== "string") {
201
+ return undefined;
202
+ }
203
+ return {
204
+ type: attachment.type,
205
+ data: attachment.data,
206
+ fileName: typeof attachment.fileName === "string"
207
+ ? attachment.fileName
208
+ : undefined,
209
+ size: attachment.size,
210
+ };
211
+ }
212
+ const normalizedTextAttachmentType = typeof attachment.content === "string"
213
+ ? inferTextAttachmentType({
214
+ attachmentType: attachment.type,
215
+ mimeType: attachment.mimeType,
216
+ fileName: typeof attachment.fileName === "string"
217
+ ? attachment.fileName
218
+ : "",
219
+ })
220
+ : null;
221
+ // Validate text attachments and normalize any text-like subtype to the
222
+ // stable server-side text attachment shape.
223
+ if (normalizedTextAttachmentType) {
224
+ if (typeof attachment.content !== "string") {
225
+ return undefined;
226
+ }
227
+ return {
228
+ type: normalizedTextAttachmentType,
229
+ content: attachment.content,
230
+ fileName: typeof attachment.fileName === "string"
231
+ ? attachment.fileName
232
+ : undefined,
233
+ mimeType: typeof attachment.mimeType === "string"
234
+ ? attachment.mimeType
235
+ : TEXT_ATTACHMENT_MIME_TYPES[normalizedTextAttachmentType],
236
+ size: attachment.size,
237
+ };
238
+ }
239
+ if (attachment.type === "uploaded") {
240
+ if (typeof attachment.url !== "string" ||
241
+ typeof attachment.mediaType !== "string") {
242
+ return undefined;
243
+ }
244
+ return {
245
+ type: "uploaded",
246
+ url: attachment.url,
247
+ signedUrl: typeof attachment.signedUrl === "string"
248
+ ? attachment.signedUrl
249
+ : undefined,
250
+ mediaType: attachment.mediaType,
251
+ fileName: typeof attachment.fileName === "string"
252
+ ? attachment.fileName
253
+ : undefined,
254
+ label: typeof attachment.label === "string" ? attachment.label : undefined,
255
+ insight: typeof attachment.insight === "string"
256
+ ? attachment.insight
257
+ : undefined,
258
+ storageKey: typeof attachment.storageKey === "string"
259
+ ? attachment.storageKey
260
+ : undefined,
261
+ signedUrlExpiresAt: typeof attachment.signedUrlExpiresAt === "string"
262
+ ? attachment.signedUrlExpiresAt
263
+ : undefined,
264
+ scopeType: attachment.scopeType === "app" || attachment.scopeType === "org"
265
+ ? attachment.scopeType
266
+ : undefined,
267
+ applicationId: typeof attachment.applicationId === "string"
268
+ ? attachment.applicationId
269
+ : undefined,
270
+ };
271
+ }
272
+ return undefined;
273
+ })
274
+ .filter(Boolean);
275
+ return [validAttachments.length === attachments.length, validAttachments];
276
+ }
277
+ export function parsePersistedChatMessage(payload, options) {
278
+ const { fallbackTimestamp, onInvalid, onError } = options;
279
+ try {
280
+ if (!payload || typeof payload !== "object") {
281
+ return null;
282
+ }
283
+ const msg = payload;
284
+ // Required fields come from the shared contract, so the editor's durable
285
+ // reader of the same rows cannot drift into rendering what this drops.
286
+ // Legacy roles "server" and "ai" (written by older server paths such as
287
+ // setApplicationHash for `draftCommitted` messages, and recognized as
288
+ // assistant-like in commitMessage.ts) are coerced to "assistant" there,
289
+ // so existing chat history keeps rendering and a client that dedupes
290
+ // id-less rows on a composite key keys them the same way we do.
291
+ const requiredFields = parsePersistedChatMessageFields(payload);
292
+ if (!requiredFields.valid) {
293
+ switch (requiredFields.reason) {
294
+ case "role":
295
+ onInvalid?.(`Invalid message role: ${msg.role}`);
296
+ break;
297
+ case "normalizedRole":
298
+ onInvalid?.(`Invalid message role after normalize: ${msg.role}`);
299
+ break;
300
+ case "content":
301
+ onInvalid?.(`Invalid message content (type=${typeof msg.content})`);
302
+ break;
303
+ case "type":
304
+ onInvalid?.(`Invalid message type: ${msg.type}`);
305
+ break;
306
+ case "payload":
307
+ break;
308
+ }
309
+ return null;
310
+ }
311
+ const { role, content } = requiredFields.fields;
312
+ // Validate optional fields with defaults
313
+ const timestamp = Number.isFinite(msg.timestamp)
314
+ ? msg.timestamp
315
+ : fallbackTimestamp;
316
+ const group = typeof msg.group === "string" ? msg.group : undefined;
317
+ const status = typeof msg.status === "string" ? msg.status : undefined;
318
+ const action = typeof msg.action === "string" ? msg.action : undefined;
319
+ const title = typeof msg.title === "string" ? msg.title : undefined;
320
+ const streaming = typeof msg.streaming === "boolean" ? msg.streaming : undefined;
321
+ const integrations = Array.isArray(msg.integrations)
322
+ ? msg.integrations
323
+ : undefined;
324
+ const entities = Array.isArray(msg.entities)
325
+ ? msg.entities
326
+ : undefined;
327
+ const [isAttachmentsValid, attachments] = validateAttachments(msg.attachments);
328
+ if (!isAttachmentsValid) {
329
+ onInvalid?.(`Invalid attachments`);
330
+ return null;
331
+ }
332
+ if (msg.type === "tool") {
333
+ return {
334
+ role,
335
+ content,
336
+ type: "tool",
337
+ id: typeof msg.id === "string" ? msg.id : undefined,
338
+ timestamp,
339
+ status: status,
340
+ action: action,
341
+ args: msg.args,
342
+ tool: msg.tool,
343
+ toolCallId: typeof msg.toolCallId === "string" ? msg.toolCallId : undefined,
344
+ title,
345
+ };
346
+ }
347
+ else if (msg.type === "tool-result") {
348
+ if (typeof msg.toolCallId !== "string" ||
349
+ typeof msg.toolName !== "string") {
350
+ onInvalid?.(`Invalid tool-result message: missing required fields`);
351
+ return null;
352
+ }
353
+ return {
354
+ role,
355
+ content: content || "",
356
+ type: "tool-result",
357
+ id: typeof msg.id === "string" ? msg.id : undefined,
358
+ timestamp,
359
+ status: status,
360
+ toolCallId: msg.toolCallId,
361
+ toolName: msg.toolName,
362
+ output: msg.output,
363
+ title,
364
+ };
365
+ }
366
+ else if (msg.type === "tool-error") {
367
+ if (typeof msg.toolCallId !== "string" ||
368
+ typeof msg.toolName !== "string") {
369
+ onInvalid?.(`Invalid tool-error message: missing required fields`);
370
+ return null;
371
+ }
372
+ return {
373
+ role,
374
+ content: content || "",
375
+ type: "tool-error",
376
+ id: typeof msg.id === "string" ? msg.id : undefined,
377
+ timestamp,
378
+ status: status,
379
+ toolCallId: msg.toolCallId,
380
+ toolName: msg.toolName,
381
+ error: msg.error instanceof Error
382
+ ? msg.error.message
383
+ : typeof msg.error === "string"
384
+ ? msg.error
385
+ : (JSON.stringify(msg.error, null, 2) ?? "Unknown error"),
386
+ title,
387
+ errorCode: msg.errorCode,
388
+ errorSummary: typeof msg.errorSummary === "string"
389
+ ? msg.errorSummary
390
+ : undefined,
391
+ };
392
+ }
393
+ else if (msg.group === "checklist") {
394
+ // typeof null === "object" and arrays are objects; processChecklistMessage
395
+ // reads checklistData.type and payload unguarded, so reject those here.
396
+ if (!isNonNullObject(msg.checklistData) ||
397
+ typeof msg.checklistData.type !== "string" ||
398
+ !isNonNullObject(msg.checklistData.payload)) {
399
+ onInvalid?.(`Invalid checklist data`);
400
+ return null;
401
+ }
402
+ return {
403
+ role,
404
+ content,
405
+ type: "tool",
406
+ id: typeof msg.id === "string" ? msg.id : undefined,
407
+ timestamp,
408
+ group,
409
+ status,
410
+ action,
411
+ checklistData: msg.checklistData,
412
+ title,
413
+ };
414
+ }
415
+ else if (msg.type === "multi_choice") {
416
+ if (!Array.isArray(msg.choices) || msg.choices.length === 0) {
417
+ onInvalid?.(`Invalid choices for multi_choice message`);
418
+ return null;
419
+ }
420
+ return {
421
+ role,
422
+ content,
423
+ type: "multi_choice",
424
+ id: typeof msg.id === "string" ? msg.id : undefined,
425
+ timestamp,
426
+ choices: msg.choices,
427
+ selectionType: typeof msg.selectionType === "string"
428
+ ? msg.selectionType
429
+ : "single",
430
+ title,
431
+ questionType: typeof msg.questionType === "string"
432
+ ? msg.questionType
433
+ : undefined,
434
+ subtitle: typeof msg.subtitle === "string" ? msg.subtitle : undefined,
435
+ details: typeof msg.details === "string" ? msg.details : undefined,
436
+ knowledgeCandidates: Array.isArray(msg.knowledgeCandidates)
437
+ ? msg.knowledgeCandidates
438
+ : undefined,
439
+ };
440
+ }
441
+ else if (msg.type === "remember_knowledge_draft") {
442
+ if (!Array.isArray(msg.candidates) || msg.candidates.length === 0) {
443
+ onInvalid?.(`Invalid candidates for remember_knowledge_draft message`);
444
+ return null;
445
+ }
446
+ return {
447
+ role,
448
+ content,
449
+ type: "remember_knowledge_draft",
450
+ id: typeof msg.id === "string" ? msg.id : undefined,
451
+ timestamp,
452
+ candidates: msg.candidates,
453
+ title,
454
+ };
455
+ }
456
+ else if (msg.type === "multi_choice_response") {
457
+ if (typeof msg.responseToMessageId !== "string") {
458
+ onInvalid?.(`Invalid responseToMessageId for multi_choice_response`);
459
+ return null;
460
+ }
461
+ if (!Array.isArray(msg.selectedChoiceIndices)) {
462
+ onInvalid?.(`Invalid selectedChoiceIndices for multi_choice_response`);
463
+ return null;
464
+ }
465
+ const response = {
466
+ role,
467
+ content,
468
+ type: "multi_choice_response",
469
+ id: typeof msg.id === "string" ? msg.id : undefined,
470
+ timestamp,
471
+ responseToMessageId: msg.responseToMessageId,
472
+ selectedChoiceIndices: msg.selectedChoiceIndices,
473
+ selectedScopes: Array.isArray(msg.selectedScopes)
474
+ ? msg.selectedScopes
475
+ : undefined,
476
+ savedFacts: Array.isArray(msg.savedFacts)
477
+ ? msg.savedFacts
478
+ : undefined,
479
+ title,
480
+ };
481
+ return response;
482
+ }
483
+ else if (msg.type === "remember_knowledge_draft_response") {
484
+ if (typeof msg.responseToMessageId !== "string") {
485
+ onInvalid?.(`Invalid responseToMessageId for remember_knowledge_draft_response`);
486
+ return null;
487
+ }
488
+ if (!Array.isArray(msg.selectedCandidateIndices)) {
489
+ onInvalid?.(`Invalid selectedCandidateIndices for remember_knowledge_draft_response`);
490
+ return null;
491
+ }
492
+ return {
493
+ role,
494
+ content,
495
+ type: "remember_knowledge_draft_response",
496
+ id: typeof msg.id === "string" ? msg.id : undefined,
497
+ timestamp,
498
+ responseToMessageId: msg.responseToMessageId,
499
+ selectedCandidateIndices: msg.selectedCandidateIndices,
500
+ selectedScopes: Array.isArray(msg.selectedScopes)
501
+ ? msg.selectedScopes
502
+ : undefined,
503
+ savedFacts: Array.isArray(msg.savedFacts)
504
+ ? msg.savedFacts
505
+ : undefined,
506
+ title,
507
+ };
508
+ }
509
+ else if (msg.type === "confirm") {
510
+ return {
511
+ role,
512
+ content,
513
+ type: "confirm",
514
+ id: typeof msg.id === "string" ? msg.id : undefined,
515
+ timestamp,
516
+ knowledgeEntry: typeof msg.knowledgeEntry === "object" && msg.knowledgeEntry !== null
517
+ ? msg.knowledgeEntry
518
+ : undefined,
519
+ title,
520
+ };
521
+ }
522
+ else if (msg.type === "searchable_dropdown") {
523
+ if (!Array.isArray(msg.options) || msg.options.length === 0) {
524
+ onInvalid?.(`Invalid options for searchable_dropdown message`);
525
+ return null;
526
+ }
527
+ return {
528
+ role,
529
+ content,
530
+ type: "searchable_dropdown",
531
+ id: typeof msg.id === "string" ? msg.id : undefined,
532
+ timestamp,
533
+ options: msg.options,
534
+ placeholder: typeof msg.placeholder === "string" ? msg.placeholder : undefined,
535
+ title,
536
+ };
537
+ }
538
+ else if (msg.type === "searchable_dropdown_response") {
539
+ if (typeof msg.responseToMessageId !== "string") {
540
+ onInvalid?.(`Invalid responseToMessageId for searchable_dropdown_response`);
541
+ return null;
542
+ }
543
+ if (typeof msg.selectedValue !== "string") {
544
+ onInvalid?.(`Invalid selectedValue for searchable_dropdown_response`);
545
+ return null;
546
+ }
547
+ return {
548
+ role,
549
+ content,
550
+ type: "searchable_dropdown_response",
551
+ id: typeof msg.id === "string" ? msg.id : undefined,
552
+ timestamp,
553
+ responseToMessageId: msg.responseToMessageId,
554
+ selectedValue: msg.selectedValue,
555
+ selectedLabel: typeof msg.selectedLabel === "string"
556
+ ? msg.selectedLabel
557
+ : msg.selectedValue,
558
+ title,
559
+ };
560
+ }
561
+ else if (msg.type === "confirm_response") {
562
+ if (typeof msg.responseToMessageId !== "string") {
563
+ onInvalid?.(`Invalid responseToMessageId for confirm_response`);
564
+ return null;
565
+ }
566
+ return {
567
+ role,
568
+ content,
569
+ type: "confirm_response",
570
+ id: typeof msg.id === "string" ? msg.id : undefined,
571
+ timestamp,
572
+ responseToMessageId: msg.responseToMessageId,
573
+ approved: Boolean(msg.approved),
574
+ selectedScopes: Array.isArray(msg.selectedScopes)
575
+ ? msg.selectedScopes
576
+ : undefined,
577
+ savedFacts: Array.isArray(msg.savedFacts)
578
+ ? msg.savedFacts
579
+ : undefined,
580
+ title,
581
+ };
582
+ }
583
+ else if (msg.type === "plan_response") {
584
+ if (typeof msg.responseToMessageId !== "string") {
585
+ onInvalid?.(`Invalid responseToMessageId for plan_response`);
586
+ return null;
587
+ }
588
+ return {
589
+ role,
590
+ content,
591
+ type: "plan_response",
592
+ id: typeof msg.id === "string" ? msg.id : undefined,
593
+ timestamp,
594
+ responseToMessageId: msg.responseToMessageId,
595
+ title,
596
+ approved: typeof msg.approved === "boolean" ? msg.approved : undefined,
597
+ enableTesting: typeof msg.enableTesting === "boolean"
598
+ ? msg.enableTesting
599
+ : undefined,
600
+ };
601
+ }
602
+ else if (msg.type === "test_run_start") {
603
+ if (typeof msg.testRunId !== "string") {
604
+ onInvalid?.(`Invalid test_run_start message: missing testRunId`);
605
+ return null;
606
+ }
607
+ return {
608
+ role,
609
+ content,
610
+ type: "test_run_start",
611
+ id: typeof msg.id === "string" ? msg.id : undefined,
612
+ timestamp,
613
+ testRunId: msg.testRunId,
614
+ header: typeof msg.header === "string" ? msg.header : "End-to-end Testing",
615
+ testCases: Array.isArray(msg.testCases) ? msg.testCases : undefined,
616
+ };
617
+ }
618
+ else if (msg.type === "test_case_update") {
619
+ if (typeof msg.testRunId !== "string") {
620
+ onInvalid?.(`Invalid test_case_update message: missing testRunId`);
621
+ return null;
622
+ }
623
+ if (typeof msg.testCaseId !== "string") {
624
+ onInvalid?.(`Invalid test_case_update message: missing testCaseId`);
625
+ return null;
626
+ }
627
+ return {
628
+ role,
629
+ content,
630
+ type: "test_case_update",
631
+ id: typeof msg.id === "string" ? msg.id : undefined,
632
+ timestamp,
633
+ testRunId: msg.testRunId,
634
+ testCaseId: msg.testCaseId,
635
+ testCaseStatus: msg.testCaseStatus,
636
+ };
637
+ }
638
+ else if (msg.type === "test_run_end") {
639
+ if (typeof msg.testRunId !== "string") {
640
+ onInvalid?.(`Invalid test_run_end message: missing testRunId`);
641
+ return null;
642
+ }
643
+ return {
644
+ role,
645
+ content,
646
+ type: "test_run_end",
647
+ id: typeof msg.id === "string" ? msg.id : undefined,
648
+ timestamp,
649
+ testRunId: msg.testRunId,
650
+ testStatus: typeof msg.testStatus === "string"
651
+ ? msg.testStatus
652
+ : "completed",
653
+ summary: typeof msg.summary === "string" ? msg.summary : undefined,
654
+ };
655
+ }
656
+ else if (msg.type === "retry_notification") {
657
+ return {
658
+ role,
659
+ content,
660
+ type: "retry_notification",
661
+ id: typeof msg.id === "string" ? msg.id : undefined,
662
+ timestamp,
663
+ retryType: msg.retryType,
664
+ attempt: typeof msg.attempt === "number" ? msg.attempt : undefined,
665
+ delaySeconds: typeof msg.delaySeconds === "number" ? msg.delaySeconds : undefined,
666
+ fromProvider: typeof msg.fromProvider === "string" ? msg.fromProvider : undefined,
667
+ toProvider: typeof msg.toProvider === "string" ? msg.toProvider : undefined,
668
+ reason: typeof msg.reason === "string" ? msg.reason : undefined,
669
+ };
670
+ }
671
+ else if (msg.type === "knowledge") {
672
+ const facts = parsePersistedKnowledgeFacts(msg.facts);
673
+ if (!facts) {
674
+ onInvalid?.(`Invalid knowledge facts`);
675
+ return null;
676
+ }
677
+ return {
678
+ role,
679
+ content,
680
+ type: "knowledge",
681
+ id: typeof msg.id === "string" ? msg.id : undefined,
682
+ timestamp,
683
+ status,
684
+ facts,
685
+ };
686
+ }
687
+ else if (msg.type === "app_knowledge_saved") {
688
+ return {
689
+ role,
690
+ content,
691
+ type: "app_knowledge_saved",
692
+ id: typeof msg.id === "string" ? msg.id : undefined,
693
+ timestamp,
694
+ knowledgeCount: typeof msg.knowledgeCount === "number" ? msg.knowledgeCount : 0,
695
+ responseToMessageId: typeof msg.responseToMessageId === "string"
696
+ ? msg.responseToMessageId
697
+ : undefined,
698
+ savedFacts: Array.isArray(msg.savedFacts) ? msg.savedFacts : undefined,
699
+ };
700
+ }
701
+ else if (msg.type === "next_steps") {
702
+ return {
703
+ role,
704
+ content,
705
+ type: "next_steps",
706
+ id: typeof msg.id === "string" ? msg.id : undefined,
707
+ timestamp,
708
+ nextSteps: Array.isArray(msg.nextSteps) ? msg.nextSteps : [],
709
+ };
710
+ }
711
+ const baseMessage = {
712
+ role: role,
713
+ content,
714
+ type: msg.type,
715
+ id: typeof msg.id === "string" ? msg.id : undefined,
716
+ timestamp,
717
+ group,
718
+ status: status,
719
+ action: action,
720
+ attachments,
721
+ title,
722
+ };
723
+ if (msg.type === "plan") {
724
+ if (streaming !== undefined) {
725
+ baseMessage.streaming = streaming;
726
+ }
727
+ if (integrations) {
728
+ baseMessage.integrations = integrations;
729
+ }
730
+ if (entities) {
731
+ baseMessage.entities = entities;
732
+ }
733
+ if (msg.includesFreshDatabase === true) {
734
+ baseMessage.includesFreshDatabase = true;
735
+ }
736
+ }
737
+ // Preserve commitId for commitRestored, commitRestoring, and draftCommitted messages
738
+ if ((msg.type === "commitRestored" ||
739
+ msg.type === "commitRestoring" ||
740
+ msg.type === "draftCommitted") &&
741
+ typeof msg.commitId === "string") {
742
+ baseMessage.commitId =
743
+ msg.commitId;
744
+ }
745
+ return baseMessage;
746
+ }
747
+ catch (error) {
748
+ onError?.(error);
749
+ return null;
750
+ }
751
+ }
752
+ //# sourceMappingURL=persisted-chat-message.js.map