File size: 9,220 Bytes
fdc8b59
 
 
 
 
 
 
 
 
 
 
 
 
2c4e38d
fdc8b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c4e38d
 
 
 
 
 
 
 
 
fdc8b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import * as React from 'react'
import { randomUUID } from 'crypto'
import { getTotalCost as getTotalCostUSD } from '../../cost-tracker.js'
import { getTotalTokensUsed } from '../../bootstrap/state.js'
import { Box, Text, useInput } from '../../ink.js'
import type { Goal, GoalStatus } from '../../state/AppStateStore.js'
import type { LocalJSXCommandContext, LocalJSXCommandOnDone } from '../../types/command.js'
import {
  buildObjectiveUpdatedPrompt,
  formatElapsed,
  formatGoalStatus,
  isGoalContinuationPrompt,
} from '../../utils/goal.js'
import { clearGoal, saveGoal } from '../../utils/sessionStorage.js'
import { enqueue, removeByFilter } from '../../utils/messageQueueManager.js'
import { renderToString } from '../../utils/staticRender.js'

function statusColor(status: GoalStatus): string {
  switch (status) {
    case 'pursuing':
      return 'green'
    case 'paused':
      return 'yellow'
    case 'achieved':
      return 'cyan'
    case 'blocked':
      return 'red'
    case 'usage-limited':
      return 'gray'
    case 'budget-limited':
      return 'magenta'
  }
}

function GoalDisplay({
  goal,
  now,
}: {
  goal: Goal
  now: number
}): React.ReactNode {
  const elapsed = formatElapsed(now - goal.startedAt)

  return (
    <Box flexDirection="column">
      <Text bold>Goal</Text>
      <Text>{goal.objective}</Text>
      <Box marginTop={1}>
        <Text>Status: </Text>
        <Text color={statusColor(goal.status)}>
          {formatGoalStatus(goal.status)}
        </Text>
      </Box>
      <Text dimColor>
        Elapsed: {elapsed} · Continuations:{' '}
        {goal.continuationCount}
      </Text>
      {goal.lastReason ? (
        <Text dimColor>Last update: {goal.lastReason}</Text>
      ) : null}
    </Box>
  )
}

function GoalOverwriteConfirm({
  existing,
  objective,
  context,
  onDone,
  inPlanMode,
}: {
  existing: Goal
  objective: string
  context: LocalJSXCommandContext
  onDone: LocalJSXCommandOnDone
  inPlanMode: boolean
}): React.ReactNode {
  const [choice, setChoice] = React.useState<'replace' | 'cancel' | null>(null)

  useInput((input, key) => {
    if (choice !== null) return
    if (input === 'y' || input === 'Y' || key.return) {
      setChoice('replace')
    } else if (input === 'n' || input === 'N' || key.escape) {
      setChoice('cancel')
    }
  })

  React.useEffect(() => {
    if (choice === null) return
    const { setAppState } = context

    if (choice === 'cancel') {
      onDone('Goal not changed.')
      return
    }

    const now = Date.now()
    const newGoal: Goal = {
      id: randomUUID(),
      objective,
      status: 'pursuing',
      startedAt: now,
      startCostUSD: getTotalCostUSD(),
      startTokensUsed: getTotalTokensUsed(),
      continuationCount: 0,
      lastUpdatedAt: now,
    }
    setAppState((prev: AppState) => ({ ...prev, goal: newGoal } satisfies AppState))
    clearQueuedGoalContinuations()

    const message = inPlanMode
      ? `Goal set: ${objective}\nAuto-continuation is disabled while in Plan mode. Exit plan mode (Shift+Tab) to begin pursuit.`
      : `Goal set: ${objective}\nThe agent will auto-continue toward this objective until it is achieved, blocked, or paused. Use /goal pause, /goal resume, /goal edit, or /goal clear to manage it.`
    onDone(message, {
      metaMessages: [
        `[goal] Active goal id: ${newGoal.id}. If calling update_goal for this goal, include goal_id='${newGoal.id}'.`,
      ],
    })
  }, [choice, objective, context, onDone, inPlanMode])

  return (
    <Box flexDirection="column">
      <Text bold>Replace existing goal?</Text>
      <Box marginTop={1}>
        <Text dimColor>
          Current goal ({formatGoalStatus(existing.status)}):
        </Text>
      </Box>
      <Text>{existing.objective}</Text>
      <Box marginTop={1}>
        <Text dimColor>New objective:</Text>
      </Box>
      <Text>{objective}</Text>
      <Box marginTop={1}>
        <Text dimColor>
          Press <Text bold>Enter</Text> to replace, <Text bold>Esc</Text> to
          cancel.
        </Text>
      </Box>
    </Box>
  )
}

type AppState = ReturnType<typeof context.getAppState>

function setGoal(
  setAppState: LocalJSXCommandContext['setAppState'],
  updater: (prev: Goal | undefined) => Goal | undefined,
): void {
  setAppState((prev: AppState) => {
    const newGoal = updater(prev.goal)
    if (newGoal) {
      saveGoal({ type: 'goal', ...newGoal })
    } else {
      clearGoal()
    }
    return { ...prev, goal: newGoal } satisfies AppState
  })
}

function clearQueuedGoalContinuations(): void {
  removeByFilter(cmd => isGoalContinuationPrompt(cmd.value))
}

export async function call(
  onDone: LocalJSXCommandOnDone,
  context: LocalJSXCommandContext,
  args: string,
): Promise<React.ReactNode> {
  const { getAppState, setAppState } = context
  const trimmed = args.trim()
  const firstTokenMatch = trimmed.match(/^\S+/)
  const firstToken = firstTokenMatch?.[0] ?? ''
  const sub = firstToken.toLowerCase()
  const rest = firstTokenMatch
    ? trimmed.slice(firstTokenMatch[0].length).trimStart()
    : ''

  const appState = getAppState()
  const existing = appState.goal
  const inPlanMode = appState.toolPermissionContext.mode === 'plan'

  if (sub === 'pause') {
    if (!existing) {
      onDone('No active goal.')
      return null
    }
    if (existing.status !== 'pursuing') {
      onDone(`Goal is already ${formatGoalStatus(existing.status)}.`)
      return null
    }
    setGoal(setAppState, g =>
      g ? { ...g, status: 'paused', lastUpdatedAt: Date.now() } : g,
    )
    clearQueuedGoalContinuations()
    onDone('Goal paused. Auto-continuation suspended.')
    return null
  }

  if (sub === 'resume') {
    if (!existing) {
      onDone('No active goal.')
      return null
    }
    if (existing.status === 'pursuing') {
      onDone('Goal already pursuing.')
      return null
    }
    if (existing.status === 'achieved') {
      onDone(
        `Goal already ${formatGoalStatus(existing.status)}. Use /goal <objective> to start a new one.`,
      )
      return null
    }
    setGoal(setAppState, g =>
      g
        ? {
            ...g,
            status: 'pursuing',
            continuationCount: 0,
            startedAt: Date.now(),
            startCostUSD: getTotalCostUSD(),
            startTokensUsed: getTotalTokensUsed(),
            lastUpdatedAt: Date.now(),
            lastReason: undefined,
          }
        : g,
    )
    clearQueuedGoalContinuations()
    onDone(
      'Goal resumed. Continuation count and budget window reset; auto-continuation will start on the next idle tick.',
    )
    return null
  }

  if (sub === 'clear') {
    if (!existing) {
      onDone('No active goal.')
      return null
    }
    setGoal(setAppState, () => undefined)
    clearQueuedGoalContinuations()
    onDone('Goal cleared.')
    return null
  }

  if (sub === 'edit') {
    if (!existing) {
      onDone('No active goal. Set one with: /goal <objective>')
      return null
    }
    if (!rest) {
      onDone('Usage: /goal edit <new objective>')
      return null
    }
    setGoal(setAppState, g =>
      g
        ? {
            ...g,
            objective: rest,
            lastUpdatedAt: Date.now(),
          }
        : g,
    )
    clearQueuedGoalContinuations()
    onDone(`Goal objective updated to: ${rest}`, {
      metaMessages: [
        buildObjectiveUpdatedPrompt({ ...existing, objective: rest }),
      ],
    })
    return null
  }

  if (trimmed === '') {
    if (!existing) {
      onDone(
        'No active goal. Set one with: /goal <objective>\nThen the agent will auto-continue toward it across turns.',
      )
      return null
    }
    const display = (
      <GoalDisplay
        goal={existing as Goal}
        now={Date.now()}
      />
    )
    const output = await renderToString(display)
    onDone(output)
    return null
  }

  const objective = (sub === 'set' ? rest : trimmed).trim()
  if (!objective) {
    onDone('Missing objective.\nUsage: /goal <objective>')
    return null
  }

  if (existing && (existing.status === 'pursuing' || existing.status === 'paused')) {
    return (
      <GoalOverwriteConfirm
        existing={existing}
        objective={objective}
        context={context}
        onDone={onDone}
        inPlanMode={inPlanMode}
      />
    )
  }

  const now = Date.now()
  const newGoal: Goal = {
    id: randomUUID(),
    objective,
    status: 'pursuing',
    startedAt: now,
    startCostUSD: getTotalCostUSD(),
    startTokensUsed: getTotalTokensUsed(),
    continuationCount: 0,
    lastUpdatedAt: now,
  }
  setGoal(setAppState, () => newGoal)
  clearQueuedGoalContinuations()

  const message = inPlanMode
    ? `Goal set: ${objective}\nAuto-continuation is disabled while in Plan mode. Exit plan mode (Shift+Tab) to begin pursuit.`
    : `Goal set: ${objective}\nThe agent will auto-continue toward this objective until it is achieved, blocked, or paused. Use /goal pause, /goal resume, /goal edit, or /goal clear to manage it.`
  onDone(message, {
    metaMessages: [
      `[goal] Active goal id: ${newGoal.id}. If calling update_goal for this goal, include goal_id='${newGoal.id}'.`,
    ],
  })
  return null
}