MCPcopy
hub / github.com/Dimillian/CodexMonitor / upsertItem

Function upsertItem

src/utils/threadItems.listOps.ts:28–127  ·  view source on GitHub ↗
(list: ConversationItem[], item: ConversationItem)

Source from the content-addressed store, hash-verified

26}
27
28export function upsertItem(list: ConversationItem[], item: ConversationItem) {
29 const index = list.findIndex((entry) => entry.id === item.id);
30 if (index === -1) {
31 return [...list, item];
32 }
33 const existing = list[index];
34 const next = [...list];
35
36 if (existing.kind !== item.kind) {
37 next[index] = item;
38 return next;
39 }
40
41 if (existing.kind === "message" && item.kind === "message") {
42 const existingText = existing.text ?? "";
43 const incomingText = item.text ?? "";
44 next[index] = {
45 ...existing,
46 ...item,
47 text: incomingText.length >= existingText.length ? incomingText : existingText,
48 images: item.images?.length ? item.images : existing.images,
49 };
50 return next;
51 }
52
53 if (existing.kind === "userInput" && item.kind === "userInput") {
54 next[index] = {
55 ...existing,
56 ...item,
57 questions: mergeUserInputQuestions(existing.questions, item.questions),
58 };
59 return next;
60 }
61
62 if (existing.kind === "reasoning" && item.kind === "reasoning") {
63 const existingSummary = existing.summary ?? "";
64 const incomingSummary = item.summary ?? "";
65 const existingContent = existing.content ?? "";
66 const incomingContent = item.content ?? "";
67 next[index] = {
68 ...existing,
69 ...item,
70 summary:
71 incomingSummary.length >= existingSummary.length
72 ? incomingSummary
73 : existingSummary,
74 content:
75 incomingContent.length >= existingContent.length
76 ? incomingContent
77 : existingContent,
78 };
79 return next;
80 }
81
82 if (existing.kind === "tool" && item.kind === "tool") {
83 const existingOutput = existing.output ?? "";
84 const incomingOutput = item.output ?? "";
85 const hasIncomingOutput = incomingOutput.trim().length > 0;

Callers 2

reduceThreadItemsFunction · 0.90

Calls 1

mergeUserInputQuestionsFunction · 0.85

Tested by

no test coverage detected