Skip to content

Commit b43479a

Browse files
committed
refactor: util to prevent duplicated messages
1 parent ddd8f14 commit b43479a

File tree

2 files changed

+39
-65
lines changed

2 files changed

+39
-65
lines changed

cli/src/commands/tasks.ts

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* /tasks command - View and manage task history
33
*/
44

5+
import { generateMessage } from "../ui/utils/messages.js"
56
import type { Command, ArgumentProviderContext } from "./core/types.js"
67
import type { HistoryItem } from "@roo-code/types"
78

@@ -68,29 +69,26 @@ function truncate(text: string, maxLength: number): string {
6869
*/
6970
async function showTaskHistory(context: any, dataOverride?: any): Promise<void> {
7071
const { taskHistoryData, taskHistoryLoading, taskHistoryError, fetchTaskHistory, addMessage } = context
71-
const now = Date.now()
7272

7373
// Use override data if provided, otherwise use context data
7474
const data = dataOverride || taskHistoryData
7575

7676
// If loading, show loading message
7777
if (taskHistoryLoading && !dataOverride) {
7878
addMessage({
79-
id: `system-${now}`,
79+
...generateMessage(),
8080
type: "system",
8181
content: "Loading task history...",
82-
ts: now,
8382
})
8483
return
8584
}
8685

8786
// If error, show error message
8887
if (taskHistoryError && !dataOverride) {
8988
addMessage({
90-
id: `error-fetch-${now}`,
89+
...generateMessage(),
9190
type: "error",
9291
content: `Failed to load task history: ${taskHistoryError}`,
93-
ts: now,
9492
})
9593
return
9694
}
@@ -99,10 +97,9 @@ async function showTaskHistory(context: any, dataOverride?: any): Promise<void>
9997
if (!data) {
10098
await fetchTaskHistory()
10199
addMessage({
102-
id: `system-no-data-${now}`,
100+
...generateMessage(),
103101
type: "system",
104102
content: "Loading task history...",
105-
ts: now,
106103
})
107104
return
108105
}
@@ -111,10 +108,9 @@ async function showTaskHistory(context: any, dataOverride?: any): Promise<void>
111108

112109
if (historyItems.length === 0) {
113110
addMessage({
114-
id: `system-no-tasks-${now}`,
111+
...generateMessage(),
115112
type: "system",
116113
content: "No tasks found in history.",
117-
ts: now,
118114
})
119115
return
120116
}
@@ -136,10 +132,9 @@ async function showTaskHistory(context: any, dataOverride?: any): Promise<void>
136132
})
137133

138134
addMessage({
139-
id: `system-${now}`,
135+
...generateMessage(),
140136
type: "system",
141137
content,
142-
ts: now,
143138
})
144139
}
145140

@@ -148,23 +143,20 @@ async function showTaskHistory(context: any, dataOverride?: any): Promise<void>
148143
*/
149144
async function searchTasks(context: any, query: string): Promise<void> {
150145
const { updateTaskHistoryFilters, addMessage } = context
151-
const now = Date.now()
152146

153147
if (!query) {
154148
addMessage({
155-
id: `error-validation-${now}`,
149+
...generateMessage(),
156150
type: "error",
157151
content: "Usage: /tasks search <query>",
158-
ts: now,
159152
})
160153
return
161154
}
162155

163156
addMessage({
164-
id: `system-${now}`,
157+
...generateMessage(),
165158
type: "system",
166159
content: `Searching for "${query}"...`,
167-
ts: now,
168160
})
169161

170162
try {
@@ -174,10 +166,9 @@ async function searchTasks(context: any, query: string): Promise<void> {
174166
await showTaskHistory(context, newData)
175167
} catch (error) {
176168
addMessage({
177-
id: `error-fetch-${now}`,
169+
...generateMessage(),
178170
type: "error",
179171
content: `Failed to search tasks: ${error instanceof Error ? error.message : String(error)}`,
180-
ts: now,
181172
})
182173
}
183174
}
@@ -187,19 +178,18 @@ async function searchTasks(context: any, query: string): Promise<void> {
187178
*/
188179
async function selectTask(context: any, taskId: string): Promise<void> {
189180
const { sendWebviewMessage, addMessage, replaceMessages, refreshTerminal } = context
190-
const now = Date.now()
191181

192182
if (!taskId) {
193183
addMessage({
194-
id: `error-validation-${now}`,
184+
...generateMessage(),
195185
type: "error",
196186
content: "Usage: /tasks select <task-id>",
197-
ts: now,
198187
})
199188
return
200189
}
201190

202191
try {
192+
const now = Date.now()
203193
replaceMessages([
204194
{
205195
id: `empty-${now}`,
@@ -223,10 +213,9 @@ async function selectTask(context: any, taskId: string): Promise<void> {
223213
})
224214
} catch (error) {
225215
addMessage({
226-
id: `error-fetch-${now}`,
216+
...generateMessage(),
227217
type: "error",
228218
content: `Failed to switch to task: ${error instanceof Error ? error.message : String(error)}`,
229-
ts: now,
230219
})
231220
}
232221
}
@@ -236,14 +225,12 @@ async function selectTask(context: any, taskId: string): Promise<void> {
236225
*/
237226
async function changePage(context: any, pageNum: string): Promise<void> {
238227
const { taskHistoryData, changeTaskHistoryPage, addMessage } = context
239-
const now = Date.now()
240228

241229
if (!taskHistoryData) {
242230
addMessage({
243-
id: `error-validation-${now}`,
231+
...generateMessage(),
244232
type: "error",
245233
content: "No task history loaded. Use /tasks to load history first.",
246-
ts: now,
247234
})
248235
return
249236
}
@@ -252,19 +239,17 @@ async function changePage(context: any, pageNum: string): Promise<void> {
252239

253240
if (isNaN(pageIndex) || pageIndex < 0 || pageIndex >= taskHistoryData.pageCount) {
254241
addMessage({
255-
id: `error-validation-${now}`,
242+
...generateMessage(),
256243
type: "error",
257244
content: `Invalid page number. Must be between 1 and ${taskHistoryData.pageCount}.`,
258-
ts: now,
259245
})
260246
return
261247
}
262248

263249
addMessage({
264-
id: `system-${now}`,
250+
...generateMessage(),
265251
type: "system",
266252
content: `Loading page ${pageIndex + 1}...`,
267-
ts: now,
268253
})
269254

270255
try {
@@ -274,10 +259,9 @@ async function changePage(context: any, pageNum: string): Promise<void> {
274259
await showTaskHistory(context, newData)
275260
} catch (error) {
276261
addMessage({
277-
id: `error-fetch-${now}`,
262+
...generateMessage(),
278263
type: "error",
279264
content: `Failed to load page: ${error instanceof Error ? error.message : String(error)}`,
280-
ts: now,
281265
})
282266
}
283267
}
@@ -291,29 +275,26 @@ async function nextPage(context: any): Promise<void> {
291275

292276
if (!taskHistoryData) {
293277
addMessage({
294-
id: `error-validation-${now}`,
278+
...generateMessage(),
295279
type: "error",
296280
content: "No task history loaded. Use /tasks to load history first.",
297-
ts: Date.now(),
298281
})
299282
return
300283
}
301284

302285
if (taskHistoryData.pageIndex >= taskHistoryData.pageCount - 1) {
303286
addMessage({
304-
id: `system-${now}`,
287+
...generateMessage(),
305288
type: "system",
306289
content: "Already on the last page.",
307-
ts: Date.now(),
308290
})
309291
return
310292
}
311293

312294
addMessage({
313-
id: `system-${now}`,
295+
...generateMessage(),
314296
type: "system",
315297
content: "Loading next page...",
316-
ts: now,
317298
})
318299

319300
try {
@@ -323,10 +304,9 @@ async function nextPage(context: any): Promise<void> {
323304
await showTaskHistory(context, newData)
324305
} catch (error) {
325306
addMessage({
326-
id: `error-fetch-${now}`,
307+
...generateMessage(),
327308
type: "error",
328309
content: `Failed to load next page: ${error instanceof Error ? error.message : String(error)}`,
329-
ts: now,
330310
})
331311
}
332312
}
@@ -336,33 +316,29 @@ async function nextPage(context: any): Promise<void> {
336316
*/
337317
async function previousPage(context: any): Promise<void> {
338318
const { taskHistoryData, previousTaskHistoryPage, addMessage } = context
339-
const now = Date.now()
340319

341320
if (!taskHistoryData) {
342321
addMessage({
343-
id: `error-validation-${now}`,
322+
...generateMessage(),
344323
type: "error",
345324
content: "No task history loaded. Use /tasks to load history first.",
346-
ts: now,
347325
})
348326
return
349327
}
350328

351329
if (taskHistoryData.pageIndex <= 0) {
352330
addMessage({
353-
id: `system-${now}`,
331+
...generateMessage(),
354332
type: "system",
355333
content: "Already on the first page.",
356-
ts: now,
357334
})
358335
return
359336
}
360337

361338
addMessage({
362-
id: `system-${now}`,
339+
...generateMessage(),
363340
type: "system",
364341
content: "Loading previous page...",
365-
ts: now,
366342
})
367343

368344
try {
@@ -372,10 +348,9 @@ async function previousPage(context: any): Promise<void> {
372348
await showTaskHistory(context, newData)
373349
} catch (error) {
374350
addMessage({
375-
id: `error-fetch-${now}`,
351+
...generateMessage(),
376352
type: "error",
377353
content: `Failed to load previous page: ${error instanceof Error ? error.message : String(error)}`,
378-
ts: now,
379354
})
380355
}
381356
}
@@ -385,26 +360,23 @@ async function previousPage(context: any): Promise<void> {
385360
*/
386361
async function changeSortOrder(context: any, sortOption: string): Promise<void> {
387362
const { updateTaskHistoryFilters, addMessage } = context
388-
const now = Date.now()
389363

390364
const validSorts = Object.keys(SORT_OPTION_MAP)
391365
const mappedSort = SORT_OPTION_MAP[sortOption]
392366

393367
if (!mappedSort) {
394368
addMessage({
395-
id: `error-validation-${now}`,
369+
...generateMessage(),
396370
type: "error",
397371
content: `Invalid sort option. Valid options: ${validSorts.join(", ")}`,
398-
ts: now,
399372
})
400373
return
401374
}
402375

403376
addMessage({
404-
id: `system-${now}`,
377+
...generateMessage(),
405378
type: "system",
406379
content: `Sorting by ${sortOption}...`,
407-
ts: Date.now(),
408380
})
409381

410382
try {
@@ -414,10 +386,9 @@ async function changeSortOrder(context: any, sortOption: string): Promise<void>
414386
await showTaskHistory(context, newData)
415387
} catch (error) {
416388
addMessage({
417-
id: `error-fetch-${now}`,
389+
...generateMessage(),
418390
type: "error",
419391
content: `Failed to change sort order: ${error instanceof Error ? error.message : String(error)}`,
420-
ts: now,
421392
})
422393
}
423394
}
@@ -427,7 +398,6 @@ async function changeSortOrder(context: any, sortOption: string): Promise<void>
427398
*/
428399
async function changeFilter(context: any, filterOption: string): Promise<void> {
429400
const { updateTaskHistoryFilters, addMessage } = context
430-
const now = Date.now()
431401

432402
let filterUpdate: any
433403
let loadingMessage: string
@@ -455,19 +425,17 @@ async function changeFilter(context: any, filterOption: string): Promise<void> {
455425

456426
default:
457427
addMessage({
458-
id: `error-validation-${now}`,
428+
...generateMessage(),
459429
type: "error",
460430
content: "Invalid filter option. Valid options: current, all, favorites, all-tasks",
461-
ts: now,
462431
})
463432
return
464433
}
465434

466435
addMessage({
467-
id: `system-${now}`,
436+
...generateMessage(),
468437
type: "system",
469438
content: loadingMessage,
470-
ts: Date.now(),
471439
})
472440

473441
try {
@@ -477,10 +445,9 @@ async function changeFilter(context: any, filterOption: string): Promise<void> {
477445
await showTaskHistory(context, newData)
478446
} catch (error) {
479447
addMessage({
480-
id: `error-fetch-${now}`,
448+
...generateMessage(),
481449
type: "error",
482450
content: `Failed to change filter: ${error instanceof Error ? error.message : String(error)}`,
483-
ts: now,
484451
})
485452
}
486453
}
@@ -632,10 +599,9 @@ export const tasksCommand: Command = {
632599

633600
default:
634601
context.addMessage({
635-
id: Date.now().toString(),
602+
...generateMessage(),
636603
type: "error",
637604
content: `Unknown subcommand "${subcommand}". Available: search, select, page, next, prev, sort, filter`,
638-
ts: Date.now(),
639605
})
640606
}
641607
},

cli/src/ui/utils/messages.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const generateMessage = () => {
2+
const now = Date.now()
3+
const uniqueSuffix = Math.floor(Math.random() * 10000)
4+
return {
5+
id: `msg-${now}-${uniqueSuffix}`,
6+
ts: now,
7+
}
8+
}

0 commit comments

Comments
 (0)