mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
chore(lint): replace ESLint with Oxlint and clear anti-slop findings
This commit is contained in:
1 parent
c6c13335e5
commit
b625acfc0c
1083 files changed
+67089
-80136
No files matched your search
+31
-38
@@ -1,92 +1,85 @@
|
||||
import { isRpcError } from '@opencode/client'
|
||||
import type { Goal } from '@opencode/schema/goal'
|
||||
import { z } from 'zod'
|
||||
import type { OpenCodeApi } from './index'
|
||||
|
||||
/** The OpenCode goal as the server reports it. */
|
||||
export type GoalState = Goal.State
|
||||
|
||||
/** The lifecycle status of an OpenCode goal. */
|
||||
export type GoalStatus = Goal.State['status']
|
||||
|
||||
/** The RPC the `/goal` plugin exposes on the OpenCode server. */
|
||||
export const GOAL_RPC_ID = 'opencode.goal'
|
||||
|
||||
/** The event the `/goal` plugin publishes in a session's location on every goal write. */
|
||||
export const GOAL_CHANGED_EVENT_TYPE = 'rpc.opencode.goal.changed'
|
||||
|
||||
/** The slash command that controls a session goal. */
|
||||
export const GOAL_COMMAND_NAME = 'goal'
|
||||
|
||||
/** Pause reasons that mean the user stopped the goal rather than the goal finishing its work. */
|
||||
export const GOAL_USER_PAUSE_REASONS = ['Paused by the user', 'Stopped by the user'] as const
|
||||
|
||||
const GOAL_USER_PAUSE_REASON_SET: ReadonlySet<string> = new Set(GOAL_USER_PAUSE_REASONS)
|
||||
|
||||
const GOAL_STATUSES = ['active', 'paused', 'complete'] as const satisfies readonly GoalStatus[]
|
||||
|
||||
/** Reports whether a value from a remote server has the shape of a reported goal. */
|
||||
function isGoalState(value: unknown): value is GoalState {
|
||||
if (typeof value !== 'object' || value === null) return false
|
||||
const goal = value as { id?: unknown; objective?: unknown; status?: unknown; rounds?: unknown; maxRounds?: unknown }
|
||||
return (
|
||||
typeof goal.id === 'string' &&
|
||||
typeof goal.objective === 'string' &&
|
||||
(GOAL_STATUSES as readonly unknown[]).includes(goal.status) &&
|
||||
typeof goal.rounds === 'number' &&
|
||||
typeof goal.maxRounds === 'number'
|
||||
)
|
||||
}
|
||||
const GoalStateSchema = z.object({
|
||||
id: z.string(),
|
||||
objective: z.string(),
|
||||
status: z.enum(GOAL_STATUSES),
|
||||
reason: z.string().optional(),
|
||||
rounds: z.number(),
|
||||
maxRounds: z.number(),
|
||||
stalls: z.number().optional(),
|
||||
boot: z.string(),
|
||||
updatedAt: z.number(),
|
||||
})
|
||||
|
||||
const GoalChangedEventSchema = z.object({
|
||||
sessionID: z.string(),
|
||||
goal: GoalStateSchema.nullable(),
|
||||
})
|
||||
|
||||
/** The result of reading a session goal, distinguishing an older server without the goal RPC. */
|
||||
export type SessionGoalRead = { supported: true; goal: GoalState | null } | { supported: false }
|
||||
|
||||
/** Reads a session goal, reporting an unsupported server instead of throwing. */
|
||||
export type SessionGoalRpc = { rpc: Pick<OpenCodeApi['rpc'], 'call'> }
|
||||
|
||||
export async function readSessionGoal(
|
||||
api: OpenCodeApi,
|
||||
api: SessionGoalRpc,
|
||||
input: { sessionID: string; directory?: string },
|
||||
): Promise<SessionGoalRead> {
|
||||
try {
|
||||
const location = input.directory ? { location: { directory: input.directory } } : undefined
|
||||
const result = await api.rpc.call({
|
||||
rpcID: GOAL_RPC_ID,
|
||||
method: 'get',
|
||||
input: { sessionID: input.sessionID },
|
||||
...(input.directory ? { location: { directory: input.directory } } : {}),
|
||||
...location,
|
||||
})
|
||||
const output: unknown = result.output ?? null
|
||||
if (output === null) return { supported: true, goal: null }
|
||||
if (!isGoalState(output)) return { supported: false }
|
||||
return { supported: true, goal: output }
|
||||
const goal = GoalStateSchema.safeParse(output)
|
||||
if (!goal.success) return { supported: false }
|
||||
return { supported: true, goal: goal.data }
|
||||
} catch (error) {
|
||||
if (isRpcError(error) && error.type === 'rpc.unavailable') return { supported: false }
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/** Extracts a session goal change from an OpenCode event, ignoring unrelated and malformed events. */
|
||||
export function goalChangeFromEvent(
|
||||
event: { type: string; data: unknown },
|
||||
): { sessionID: string; goal: GoalState | null } | null {
|
||||
if (event.type !== GOAL_CHANGED_EVENT_TYPE) return null
|
||||
|
||||
const data = event.data
|
||||
if (typeof data !== 'object' || data === null) return null
|
||||
const change = GoalChangedEventSchema.safeParse(event.data)
|
||||
if (!change.success) return null
|
||||
|
||||
const sessionID = (data as { sessionID?: unknown }).sessionID
|
||||
if (typeof sessionID !== 'string') return null
|
||||
|
||||
const goal = (data as { goal?: unknown }).goal
|
||||
if (goal === null) return { sessionID, goal: null }
|
||||
if (!isGoalState(goal)) return null
|
||||
|
||||
return { sessionID, goal }
|
||||
return { sessionID: change.data.sessionID, goal: change.data.goal }
|
||||
}
|
||||
|
||||
/** Reports whether the goal is still being worked on, whether running or paused. */
|
||||
export function isGoalOpen(goal: GoalState | null | undefined): boolean {
|
||||
return goal?.status === 'active' || goal?.status === 'paused'
|
||||
}
|
||||
|
||||
/** Reports whether the goal reached an outcome, meaning it completed or the reviewer paused it. */
|
||||
export function isGoalOutcome(goal: GoalState): boolean {
|
||||
if (goal.status === 'complete') return true
|
||||
if (goal.status !== 'paused') return false
|
||||
return !(GOAL_USER_PAUSE_REASONS as readonly string[]).includes(goal.reason ?? '')
|
||||
return !GOAL_USER_PAUSE_REASON_SET.has(goal.reason ?? '')
|
||||
}
|
||||
Reference in new issue
Block a user