forked from claude-code-best/claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoalStorage.ts
More file actions
55 lines (52 loc) · 1.8 KB
/
Copy pathgoalStorage.ts
File metadata and controls
55 lines (52 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Goal persistence bridge — connects the in-memory `goalState` map
* to the JSONL transcript that backs --resume.
*
* Splitting this off keeps goalState pure (testable without touching
* the file system) while still giving the slash command + tool a
* single call to "save the current goal".
*/
import type { UUID } from 'crypto'
import { getSessionId } from '../../bootstrap/state.js'
import type { GoalState } from '../../types/logs.js'
import {
clearGoalEntry as clearGoalEntryOnDisk,
saveGoal as saveGoalOnDisk,
} from '../../utils/sessionStorage.js'
import { _setGoalFromPersistedState, getGoal } from './goalState.js'
/**
* Snapshot the current in-memory goal for the running session to the
* JSONL transcript. Called by every mutating helper in goalState
* (set / pause / resume / complete / token update / blocked).
*
* No-op when there is no goal — used as a fire-and-forget convenience.
*/
export function persistCurrentGoal(): void {
const sessionId = getSessionId() as UUID
const goal = getGoal(sessionId)
if (!goal) return
saveGoalOnDisk(sessionId, goal)
}
/**
* Hydrate the in-memory map from a `loadTranscriptFile` result. Called
* by REPL.tsx after restoreSessionMetadata so `--resume` carries the
* goal across process restarts.
*/
export function hydrateGoalFromTranscript(
goalsMap: Map<UUID, GoalState>,
sessionId?: UUID,
): GoalState | null {
const id = (sessionId ?? (getSessionId() as UUID)) as UUID
const state = goalsMap.get(id)
if (!state) return null
_setGoalFromPersistedState(state, id)
return state
}
/**
* Persist an explicit clear — writes the `goal-cleared` tombstone so
* a future --resume cannot resurrect a stale goal entry.
*/
export function persistGoalClear(): void {
const sessionId = getSessionId() as UUID
clearGoalEntryOnDisk(sessionId)
}