mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
refactor(goals): replace the Manager session-goal API with OpenCode built-in /goal
This commit is contained in:
1 parent
c8647c1eec
commit
cbe92c92ad
84 files changed
+2099
-6321
No files matched your search
+2
-1
@@ -21,7 +21,8 @@
|
||||
"./themes": "./src/themes/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@opencode/client": "2.1.2",
|
||||
"@opencode/client": "2.2.1",
|
||||
"@opencode/schema": "2.2.1",
|
||||
"jsonc-parser": "^3.3.1",
|
||||
"zod": "^4.1.12"
|
||||
},
|
||||
|
||||
@@ -1,37 +1,6 @@
|
||||
import type { SessionGoal, SessionGoalStatus, SessionGoalStopReason } from '../schemas/session-goals'
|
||||
import type { GoalState, GoalStatus } from '../opencode/goal'
|
||||
import type { MultiRunEntryStatus } from '../schemas/multi-runs'
|
||||
|
||||
const GOAL_OUTCOME_TITLES: Record<SessionGoalStatus, string> = {
|
||||
active: 'Goal active',
|
||||
paused: 'Goal paused',
|
||||
completed: 'Goal completed',
|
||||
blocked: 'Goal blocked',
|
||||
stopped: 'Goal stopped',
|
||||
}
|
||||
|
||||
const GOAL_STOP_REASON_LABELS: Record<SessionGoalStopReason, string> = {
|
||||
cancelled: 'Cancelled',
|
||||
user_paused: 'Paused by user',
|
||||
continuation_limit: 'Continuation limit reached',
|
||||
token_budget: 'Token budget reached',
|
||||
turn_error: 'Turn failed',
|
||||
interrupted: 'Interrupted',
|
||||
audit_failed: 'Audit failed',
|
||||
session_deleted: 'Session deleted',
|
||||
}
|
||||
|
||||
export function getGoalOutcomeTitle(status: SessionGoalStatus): string {
|
||||
return GOAL_OUTCOME_TITLES[status]
|
||||
}
|
||||
|
||||
export function getGoalStopReasonLabel(stopReason: SessionGoalStopReason): string {
|
||||
return GOAL_STOP_REASON_LABELS[stopReason]
|
||||
}
|
||||
|
||||
export function getGoalOutcomeReason(goal: Pick<SessionGoal, 'stopReason' | 'lastReason'>): string | null {
|
||||
return goal.stopReason ? getGoalStopReasonLabel(goal.stopReason) : goal.lastReason
|
||||
}
|
||||
|
||||
const MULTI_RUN_ENTRY_STATUS_LABELS: Record<MultiRunEntryStatus, string> = {
|
||||
starting: 'Starting',
|
||||
started: 'Started',
|
||||
@@ -43,13 +12,20 @@ export function getMultiRunEntryStatusLabel(status: MultiRunEntryStatus): string
|
||||
return MULTI_RUN_ENTRY_STATUS_LABELS[status]
|
||||
}
|
||||
|
||||
export function getGoalTurnLabel(goal: Pick<SessionGoal, 'continuationCount' | 'maxContinuations'>): string {
|
||||
return `Turn ${goal.continuationCount}/${goal.maxContinuations}`
|
||||
const GOAL_STATUS_TITLES: Record<GoalStatus, string> = {
|
||||
active: 'Goal active',
|
||||
paused: 'Goal paused',
|
||||
complete: 'Goal completed',
|
||||
}
|
||||
|
||||
export function getGoalTokenLabel(goal: Pick<SessionGoal, 'tokensUsed' | 'tokenBudget'>): string | null {
|
||||
if (goal.tokenBudget === null) return null
|
||||
return `${goal.tokensUsed.toLocaleString()}/${goal.tokenBudget.toLocaleString()} tokens`
|
||||
/** Titles a notification for the current state of an OpenCode goal. */
|
||||
export function getGoalStatusTitle(status: GoalStatus): string {
|
||||
return GOAL_STATUS_TITLES[status]
|
||||
}
|
||||
|
||||
/** Labels the continuation progress of an OpenCode goal. */
|
||||
export function getGoalRoundsLabel(goal: Pick<GoalState, 'rounds' | 'maxRounds'>): string {
|
||||
return `Round ${goal.rounds}/${goal.maxRounds}`
|
||||
}
|
||||
|
||||
const PERMISSION_LABELS: Record<string, string> = {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { isRpcError } from '@opencode/client'
|
||||
import type { Goal } from '@opencode/schema/goal'
|
||||
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_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'
|
||||
)
|
||||
}
|
||||
|
||||
/** 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 async function readSessionGoal(
|
||||
api: OpenCodeApi,
|
||||
input: { sessionID: string; directory?: string },
|
||||
): Promise<SessionGoalRead> {
|
||||
try {
|
||||
const result = await api.rpc.call({
|
||||
rpcID: GOAL_RPC_ID,
|
||||
method: 'get',
|
||||
input: { sessionID: input.sessionID },
|
||||
...(input.directory ? { location: { directory: input.directory } } : {}),
|
||||
})
|
||||
return { supported: true, goal: (result.output ?? null) as GoalState | null }
|
||||
} 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 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 }
|
||||
}
|
||||
|
||||
/** 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 ?? '')
|
||||
}
|
||||
@@ -51,6 +51,19 @@ export { ClientError, isIntegrationNotFoundError, isMcpServerNotFoundError, isSe
|
||||
|
||||
export { assistantText, sessionIDFromEvent, toolContentText } from './content'
|
||||
|
||||
export {
|
||||
GOAL_CHANGED_EVENT_TYPE,
|
||||
GOAL_COMMAND_NAME,
|
||||
GOAL_RPC_ID,
|
||||
GOAL_USER_PAUSE_REASONS,
|
||||
goalChangeFromEvent,
|
||||
isGoalOpen,
|
||||
isGoalOutcome,
|
||||
readSessionGoal,
|
||||
} from './goal'
|
||||
|
||||
export type { GoalState, GoalStatus, SessionGoalRead } from './goal'
|
||||
|
||||
export { configModelRef, formatOpenCodeModelRef, parseOpenCodeModelRef, selectConfiguredModelRef } from './modelRef'
|
||||
|
||||
export {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { compareParsedSemver, compareSemver, normalizeSemver, parseSemver, type Semver } from '../utils/semver'
|
||||
|
||||
export const OPENCODE_PINNED_VERSION = '2.1.2'
|
||||
export const OPENCODE_PINNED_VERSION = '2.2.1'
|
||||
|
||||
const OPENCODE_RELEASE_BASE_URL = 'https://opencode.ai/files/bin'
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ export * from './internal-assistant'
|
||||
export * from './session-pins'
|
||||
export * from './session-permissions'
|
||||
export * from './session-sandbox'
|
||||
export * from './session-goals'
|
||||
export * from './internal-sessions'
|
||||
export * from './multi-runs'
|
||||
export * from './logs'
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
export const SESSION_PROMPT_MAX_LENGTH = 20000;
|
||||
export const FUSION_PROMPT_MAX_LENGTH = 60000;
|
||||
export const FUSION_INSTRUCTIONS_MAX_LENGTH = 4000;
|
||||
export const GOAL_MAX_CONTINUATIONS_MIN = 1;
|
||||
export const GOAL_MAX_CONTINUATIONS_MAX = 200;
|
||||
@@ -1,77 +0,0 @@
|
||||
import { z } from "zod";
|
||||
import { SESSION_PROMPT_MAX_LENGTH, GOAL_MAX_CONTINUATIONS_MIN, GOAL_MAX_CONTINUATIONS_MAX } from "./limits";
|
||||
|
||||
export const SessionGoalStatusSchema = z.enum(["active", "paused", "completed", "blocked", "stopped"]);
|
||||
|
||||
export type SessionGoalStatus = z.infer<typeof SessionGoalStatusSchema>;
|
||||
|
||||
export const SessionGoalStopReasonSchema = z.enum([
|
||||
"cancelled",
|
||||
"user_paused",
|
||||
"continuation_limit",
|
||||
"token_budget",
|
||||
"turn_error",
|
||||
"interrupted",
|
||||
"audit_failed",
|
||||
"session_deleted",
|
||||
]);
|
||||
|
||||
export type SessionGoalStopReason = z.infer<typeof SessionGoalStopReasonSchema>;
|
||||
|
||||
export const SessionGoalVerdictSchema = z.enum(["continue", "done", "blocked"]);
|
||||
|
||||
export type SessionGoalVerdict = z.infer<typeof SessionGoalVerdictSchema>;
|
||||
|
||||
export const SessionGoalTurnStateSchema = z.enum(["waiting", "running"]);
|
||||
|
||||
export type SessionGoalTurnState = z.infer<typeof SessionGoalTurnStateSchema>;
|
||||
|
||||
export const SessionGoalSchema = z.object({
|
||||
id: z.number().int(),
|
||||
sessionId: z.string(),
|
||||
serverId: z.string(),
|
||||
directory: z.string(),
|
||||
objective: z.string(),
|
||||
status: SessionGoalStatusSchema,
|
||||
stopReason: SessionGoalStopReasonSchema.nullable(),
|
||||
turnState: SessionGoalTurnStateSchema,
|
||||
continuationCount: z.number().int(),
|
||||
maxContinuations: z.number().int(),
|
||||
tokenBudget: z.number().int().nullable(),
|
||||
tokensUsed: z.number().int(),
|
||||
consecutiveBlocked: z.number().int(),
|
||||
lastVerdict: SessionGoalVerdictSchema.nullable(),
|
||||
lastReason: z.string().nullable(),
|
||||
createdAt: z.number(),
|
||||
updatedAt: z.number(),
|
||||
finishedAt: z.number().nullable(),
|
||||
});
|
||||
|
||||
export type SessionGoal = z.infer<typeof SessionGoalSchema>;
|
||||
|
||||
export const SESSION_GOAL_POLL_INTERVAL_MS = 3000;
|
||||
|
||||
export type OpenSessionGoal = SessionGoal & { status: "active" | "paused" };
|
||||
|
||||
export type TerminalSessionGoal = SessionGoal & { status: "completed" | "blocked" | "stopped" };
|
||||
|
||||
/** True while the goal still drives its session: active or paused. */
|
||||
export function isOpenSessionGoal(goal: SessionGoal | null | undefined): goal is OpenSessionGoal {
|
||||
return goal?.status === "active" || goal?.status === "paused";
|
||||
}
|
||||
|
||||
/** True once the goal has finished: completed, blocked or stopped. */
|
||||
export function isTerminalSessionGoal(goal: SessionGoal | null | undefined): goal is TerminalSessionGoal {
|
||||
return goal?.status === "completed" || goal?.status === "blocked" || goal?.status === "stopped";
|
||||
}
|
||||
|
||||
export const StartSessionGoalRequestSchema = z.object({
|
||||
sessionId: z.string().min(1),
|
||||
serverId: z.string().min(1).optional(),
|
||||
directory: z.string().min(1),
|
||||
objective: z.string().trim().min(1).max(SESSION_PROMPT_MAX_LENGTH),
|
||||
maxContinuations: z.number().int().min(GOAL_MAX_CONTINUATIONS_MIN).max(GOAL_MAX_CONTINUATIONS_MAX).optional(),
|
||||
tokenBudget: z.number().int().positive().optional(),
|
||||
});
|
||||
|
||||
export type StartSessionGoalRequest = z.infer<typeof StartSessionGoalRequestSchema>;
|
||||
@@ -3,7 +3,6 @@ import { NotificationPreferencesSchema, DEFAULT_NOTIFICATION_PREFERENCES } from
|
||||
import { OPENCODE_CONFIG_SOURCE_NAMES } from "../config/defaults";
|
||||
import { ColorThemeIdSchema, MANAGER_COLOR_THEME_ID } from "../themes";
|
||||
import { SessionPermissionModeSchema } from "./session-permissions";
|
||||
import { GOAL_MAX_CONTINUATIONS_MIN, GOAL_MAX_CONTINUATIONS_MAX } from "./limits";
|
||||
import { SessionSandboxResourcesSchema } from "./session-sandbox";
|
||||
|
||||
export const CustomCommandSchema = z.object({
|
||||
@@ -193,16 +192,12 @@ export const DEFAULT_SANDBOX_PREFERENCES: SandboxPreferences = {
|
||||
|
||||
export const SessionDefaultsSchema = z.object({
|
||||
permissionMode: SessionPermissionModeSchema,
|
||||
goalMaxContinuations: z.number().int().min(GOAL_MAX_CONTINUATIONS_MIN).max(GOAL_MAX_CONTINUATIONS_MAX).optional(),
|
||||
goalTokenBudget: z.number().int().positive().optional(),
|
||||
goalAuditorModel: z.string().optional(),
|
||||
});
|
||||
|
||||
export type SessionDefaults = z.infer<typeof SessionDefaultsSchema>;
|
||||
|
||||
export const DEFAULT_SESSION_DEFAULTS = {
|
||||
permissionMode: 'ask',
|
||||
goalMaxContinuations: 20,
|
||||
} satisfies SessionDefaults;
|
||||
|
||||
export const DevicePairingPreferencesSchema = z.object({
|
||||
|
||||
Reference in new issue
Block a user