Skip to content

Commit d9348a5

Browse files
committed
feat(cli): implement todo toggle visibility with ctrl+t (#97)
- Todos hidden by default, show '(N tasks - ctrl+t to show)' indicator - Press ctrl+t to toggle full todo list with icons and priorities - Centralized state in liveblock to prevent desync after reset - Added toggleTasksVisible() and getTasksVisible() methods
1 parent 3b00ad6 commit d9348a5

9 files changed

Lines changed: 310 additions & 15 deletions

File tree

‎packages/opencode/src/cli/lite/index.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function padLines(text: string): string {
3939
}
4040

4141
// UI State
42-
let tasksVisible = false
4342
let isOperationInProgress = false
4443
let currentSessionID: string | null = null
4544
let currentModel: string | null = null
@@ -208,7 +207,7 @@ async function main() {
208207
}
209208

210209
if (key.ctrl && key.name === "t") {
211-
tasksVisible = !tasksVisible
210+
block.toggleTasksVisible()
212211
editor.render(renderPrompt(currentAgent))
213212
}
214213

‎packages/opencode/src/cli/lite/liveblock.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function createLiveBlock() {
3636
let active = false
3737
let frame = 0
3838
let interval: ReturnType<typeof setInterval> | null = null
39+
let tasksVisible = false
3940

4041
function render() {
4142
const cols = process.stdout.columns || 80
@@ -89,7 +90,11 @@ export function createLiveBlock() {
8990
}
9091

9192
// Render todos if any
92-
if (todos.length > 0) {
93+
if (todos.length > 0 && !tasksVisible) {
94+
lines.push(` ${fg.gray}(${todos.length} tasks - ctrl+t to show)${style.reset}`)
95+
}
96+
97+
if (todos.length > 0 && tasksVisible) {
9398
lines.push(` ${style.dim}${"─".repeat(Math.min(cols, 60))}${style.reset}`)
9499
for (const todo of todos) {
95100
const icon =
@@ -236,5 +241,20 @@ export function createLiveBlock() {
236241
active = false
237242
}
238243
},
244+
245+
setTasksVisible(visible: boolean) {
246+
tasksVisible = visible
247+
if (active) render()
248+
},
249+
250+
toggleTasksVisible() {
251+
tasksVisible = !tasksVisible
252+
if (active) render()
253+
return tasksVisible
254+
},
255+
256+
getTasksVisible() {
257+
return tasksVisible
258+
},
239259
}
240260
}

‎packages/opencode/src/config/config.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,11 +1096,6 @@ export namespace Config {
10961096
.optional()
10971097
.default(true)
10981098
.describe("Persist conversation context to memory for future recall"),
1099-
remory_persist_thinking: z
1100-
.boolean()
1101-
.optional()
1102-
.default(true)
1103-
.describe("Persist AI thinking/reasoning to memory"),
11041099
remory_inject_context: z
11051100
.boolean()
11061101
.optional()

‎packages/opencode/src/memory/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
export * from "./socket-client"
44
export * from "./remory"
5+
6+
export { checkRemoryAvailable, safeRemorySearch, safeRemoryAdd, getUserId, resetAvailabilityCache } from "./remory"

‎packages/opencode/src/memory/remory.test.ts‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
close,
99
invalidate,
1010
isEnabled,
11+
checkRemoryAvailable,
12+
safeRemorySearch,
13+
safeRemoryAdd,
14+
getUserId,
15+
resetAvailabilityCache,
1116
type MemoryAddParams,
1217
type MemorySearchParams,
1318
type MemoryListParams,
@@ -34,6 +39,7 @@ describe("Remory Integration", () => {
3439
searchCallCount = 0
3540
listCallCount = 0
3641
deleteCallCount = 0
42+
resetAvailabilityCache()
3743
})
3844

3945
afterEach(async () => {
@@ -193,6 +199,48 @@ describe("Remory Integration", () => {
193199
expect(searchCallCount).toBe(1)
194200
})
195201

202+
it("should search memories with proper score (for safeRemorySearch filter tests)", async () => {
203+
server = createSocketServer((request) => {
204+
if (request.method === "search") {
205+
searchCallCount++
206+
return {
207+
result: {
208+
results: [
209+
{
210+
memory_id: "mem-1",
211+
text: "Match for: test with low score",
212+
user_id: request.params.user_id,
213+
score: 0.3,
214+
},
215+
{
216+
memory_id: "mem-2",
217+
text: "Match for: test with high score",
218+
user_id: request.params.user_id,
219+
score: 0.5,
220+
},
221+
],
222+
},
223+
}
224+
}
225+
return { error: { code: -32601, message: "Method not found" } }
226+
})
227+
228+
await initialize(testSocketPath)
229+
230+
const params: MemorySearchParams = {
231+
query: "test",
232+
userId: "alice",
233+
limit: 5,
234+
}
235+
236+
const results = await search(params)
237+
238+
expect(results).toHaveLength(2)
239+
expect(results[0].score).toBe(0.3)
240+
expect(results[1].score).toBe(0.5)
241+
expect(searchCallCount).toBe(1)
242+
})
243+
196244
it("should return empty array on search when not enabled", async () => {
197245
const nonExistentPath = join(testDir, "nonexistent.sock")
198246
await initialize(nonExistentPath)
@@ -506,4 +554,108 @@ describe("Remory Integration", () => {
506554
expect(results).toEqual([])
507555
expect(responded).toBe(true) // Server still responded
508556
})
557+
558+
it("should check remory availability correctly", async () => {
559+
setupMockServer()
560+
await initialize(testSocketPath)
561+
562+
const available = await checkRemoryAvailable()
563+
expect(available).toBe(true)
564+
})
565+
566+
it("should return false when remory is not available", async () => {
567+
await close()
568+
resetAvailabilityCache()
569+
570+
const available = await checkRemoryAvailable()
571+
expect(available).toBe(false)
572+
})
573+
574+
it("should perform safe remory search with relevant results", async () => {
575+
server = createSocketServer((request) => {
576+
if (request.method === "search") {
577+
return {
578+
result: {
579+
results: [
580+
{
581+
memory_id: "mem-1",
582+
text: "Match for: where does alice work?",
583+
user_id: request.params.user_id,
584+
score: 0.3,
585+
},
586+
{ memory_id: "mem-2", text: "Less relevant match", user_id: request.params.user_id, score: 0.5 },
587+
],
588+
},
589+
}
590+
}
591+
return { error: { code: -32601, message: "Method not found" } }
592+
})
593+
594+
await initialize(testSocketPath)
595+
596+
const results = await safeRemorySearch("where does alice work?", "alice")
597+
598+
expect(results).toHaveLength(1)
599+
expect(results[0].text).toContain("where does alice work?")
600+
expect(results[0].distance).toBe(0.3)
601+
})
602+
603+
it("should return empty array when remory not available for search", async () => {
604+
await close()
605+
606+
const results = await safeRemorySearch("test", "test")
607+
608+
expect(results).toEqual([])
609+
})
610+
611+
it("should add memory safely", async () => {
612+
setupMockServer()
613+
await initialize(testSocketPath)
614+
615+
await safeRemoryAdd("Alice works at Google", "alice", { type: "test" })
616+
617+
expect(addCallCount).toBe(1)
618+
})
619+
620+
it("should silently fail when remory not available for add", async () => {
621+
await close()
622+
623+
// Should not throw
624+
await safeRemoryAdd("Test", "test")
625+
626+
expect(addCallCount).toBe(0)
627+
})
628+
629+
it("should filter results by distance threshold", async () => {
630+
server = createSocketServer((request) => {
631+
if (request.method === "search") {
632+
return {
633+
result: {
634+
results: [
635+
{ memory_id: "mem-1", text: "Very relevant", score: 0.3 },
636+
{ memory_id: "mem-2", text: "Somewhat relevant", score: 0.4 },
637+
{ memory_id: "mem-3", text: "Not relevant", score: 0.6 },
638+
],
639+
},
640+
}
641+
}
642+
return { error: { code: -32601, message: "Method not found" } }
643+
})
644+
645+
await initialize(testSocketPath)
646+
647+
const results = await safeRemorySearch("test", "alice")
648+
649+
// Only memories with score < 0.4 should be returned
650+
expect(results).toHaveLength(1)
651+
expect(results[0].text).toBe("Very relevant")
652+
expect(results[0].distance).toBe(0.3)
653+
})
654+
655+
it("should get user id from git", async () => {
656+
const userId = await getUserId()
657+
658+
expect(userId).toBeDefined()
659+
expect(typeof userId).toBe("string")
660+
})
509661
})

‎packages/opencode/src/memory/remory.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ export interface MemoryListResponse {
6969
memories: MemoryResult[]
7070
}
7171

72+
export interface Memory {
73+
text: string
74+
metadata?: Record<string, unknown>
75+
created_at?: string
76+
distance?: number
77+
}
78+
7279
export async function initialize(socketPath?: string): Promise<boolean> {
7380
state.socketPath = socketPath || DEFAULT_SOCKET_PATH
7481

@@ -246,6 +253,75 @@ function generateId(): string {
246253
return `req-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
247254
}
248255

256+
let remoryAvailable: boolean | null = null
257+
258+
export async function checkRemoryAvailable(): Promise<boolean> {
259+
if (remoryAvailable !== null) return remoryAvailable
260+
try {
261+
if (!isEnabled()) {
262+
remoryAvailable = false
263+
} else {
264+
await list({ userId: "check", limit: 1 })
265+
remoryAvailable = true
266+
}
267+
} catch {
268+
remoryAvailable = false
269+
log.warn("remory unavailable, continuing without memory features")
270+
}
271+
return remoryAvailable
272+
}
273+
274+
export function resetAvailabilityCache(): void {
275+
remoryAvailable = null
276+
}
277+
278+
export async function safeRemorySearch(query: string, userId: string, limit?: number): Promise<Memory[]> {
279+
if (!(await checkRemoryAvailable())) return []
280+
281+
try {
282+
const results = await search({
283+
query,
284+
userId,
285+
limit: limit ?? 5,
286+
})
287+
288+
const memories = results.map((r) => ({
289+
text: r.text,
290+
metadata: r.metadata,
291+
created_at: r.created_at,
292+
distance: r.score,
293+
}))
294+
295+
const relevant = memories.filter((m) => m.distance && m.distance < 0.4)
296+
return relevant
297+
} catch {
298+
return []
299+
}
300+
}
301+
302+
export async function safeRemoryAdd(text: string, userId: string, metadata?: object): Promise<void> {
303+
if (!(await checkRemoryAvailable())) return
304+
305+
try {
306+
await add({
307+
text,
308+
userId,
309+
infer: false,
310+
})
311+
} catch {
312+
// Silent fail - don't block on memory errors
313+
}
314+
}
315+
316+
export async function getUserId(): Promise<string> {
317+
try {
318+
const { execSync } = await import("child_process")
319+
return execSync("git rev-list --max-parents=0 HEAD").toString().trim()
320+
} catch {
321+
return "default-user"
322+
}
323+
}
324+
249325
export const Remory = {
250326
initialize,
251327
add,
@@ -255,4 +331,9 @@ export const Remory = {
255331
close,
256332
invalidate,
257333
isEnabled,
334+
checkRemoryAvailable,
335+
safeRemorySearch,
336+
safeRemoryAdd,
337+
getUserId,
338+
resetAvailabilityCache,
258339
}

‎packages/opencode/src/session/index.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { SessionStatus } from "./status"
2626
import crypto from "crypto"
2727
import { Agent } from "../agent/agent"
2828
import { BackgroundTasks } from "../util/tasks"
29+
import { safeRemoryAdd, getUserId } from "../memory/remory"
2930

3031
export interface TaskMetadata {
3132
agent_type: string
@@ -238,6 +239,20 @@ export namespace Session {
238239
const firstKey = backgroundTaskResults.keys().next().value
239240
if (firstKey) backgroundTaskResults.delete(firstKey)
240241
}
242+
243+
// Post-task: Store task outcome to memory
244+
if (metadata && taskResult.status === "completed") {
245+
safeRemoryAdd(
246+
`Task: ${metadata.description}\nOutcome: SUCCESS\nAgent: ${metadata.agent_type}`,
247+
await getUserId(),
248+
{
249+
type: "task_outcome",
250+
agent: metadata.agent_type,
251+
date: new Date().toISOString(),
252+
},
253+
).catch(() => {})
254+
}
255+
241256
if (sessionID) {
242257
if (!closingSessions.has(sessionID)) {
243258
const parentSessionID = metadata?.session_id

0 commit comments

Comments
 (0)