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
@@ -1,11 +1,41 @@
|
||||
import type { Migration } from '../migration-runner'
|
||||
import { ensureSessionGoalsTable } from '../session-goals'
|
||||
|
||||
const migration: Migration = {
|
||||
id: '026-session-goals',
|
||||
legacy: { version: 26, name: 'session-goals' },
|
||||
up(db) {
|
||||
ensureSessionGoalsTable(db)
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS session_goals (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
server_id TEXT NOT NULL DEFAULT 'local',
|
||||
directory TEXT NOT NULL,
|
||||
objective TEXT NOT NULL,
|
||||
status TEXT NOT NULL CHECK(status IN ('active','paused','completed','blocked','stopped')),
|
||||
stop_reason TEXT,
|
||||
turn_state TEXT NOT NULL DEFAULT 'waiting',
|
||||
continuation_count INTEGER NOT NULL DEFAULT 0,
|
||||
max_continuations INTEGER NOT NULL,
|
||||
token_budget INTEGER,
|
||||
tokens_at_start INTEGER NOT NULL DEFAULT 0,
|
||||
tokens_used INTEGER NOT NULL DEFAULT 0,
|
||||
consecutive_blocked INTEGER NOT NULL DEFAULT 0,
|
||||
last_verdict TEXT,
|
||||
last_reason TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
finished_at INTEGER
|
||||
)
|
||||
`)
|
||||
db.run(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_goals_open
|
||||
ON session_goals(server_id, session_id)
|
||||
WHERE status IN ('active','paused')
|
||||
`)
|
||||
db.run(`
|
||||
CREATE INDEX IF NOT EXISTS idx_session_goals_session
|
||||
ON session_goals(session_id, created_at DESC)
|
||||
`)
|
||||
},
|
||||
down(db) {
|
||||
db.run('DROP TABLE IF EXISTS session_goals')
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { Migration } from '../migration-runner'
|
||||
|
||||
function createSessionGoalsTable(db: Database): void {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS session_goals (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
server_id TEXT NOT NULL DEFAULT 'local',
|
||||
directory TEXT NOT NULL,
|
||||
objective TEXT NOT NULL,
|
||||
status TEXT NOT NULL CHECK(status IN ('active','paused','completed','blocked','stopped')),
|
||||
stop_reason TEXT,
|
||||
turn_state TEXT NOT NULL DEFAULT 'waiting',
|
||||
continuation_count INTEGER NOT NULL DEFAULT 0,
|
||||
max_continuations INTEGER NOT NULL,
|
||||
token_budget INTEGER,
|
||||
tokens_at_start INTEGER NOT NULL DEFAULT 0,
|
||||
tokens_used INTEGER NOT NULL DEFAULT 0,
|
||||
consecutive_blocked INTEGER NOT NULL DEFAULT 0,
|
||||
last_verdict TEXT,
|
||||
last_reason TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
finished_at INTEGER
|
||||
)
|
||||
`)
|
||||
db.run(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_goals_open
|
||||
ON session_goals(server_id, session_id)
|
||||
WHERE status IN ('active','paused')
|
||||
`)
|
||||
db.run(`
|
||||
CREATE INDEX IF NOT EXISTS idx_session_goals_session
|
||||
ON session_goals(session_id, created_at DESC)
|
||||
`)
|
||||
}
|
||||
|
||||
const migration: Migration = {
|
||||
id: '202610291200-drop-session-goals',
|
||||
|
||||
up(db) {
|
||||
db.run('DROP INDEX IF EXISTS idx_session_goals_open')
|
||||
db.run('DROP INDEX IF EXISTS idx_session_goals_session')
|
||||
db.run('DROP TABLE IF EXISTS session_goals')
|
||||
},
|
||||
|
||||
down(db) {
|
||||
createSessionGoalsTable(db)
|
||||
},
|
||||
}
|
||||
|
||||
export default migration
|
||||
@@ -42,6 +42,7 @@ import { secretsGlobalScopeIndexMigration } from './202610181410-secrets-global-
|
||||
import migration202610221800 from './202610221800-session-device-name'
|
||||
import migration202610231200 from './202610231200-push-subscription-session-id'
|
||||
import migration202610251900 from './202610251900-schedule-sandbox-config'
|
||||
import migration202610291200 from './202610291200-drop-session-goals'
|
||||
|
||||
export const allMigrations: Migration[] = [
|
||||
migration001,
|
||||
@@ -87,4 +88,5 @@ export const allMigrations: Migration[] = [
|
||||
migration202610221800,
|
||||
migration202610231200,
|
||||
migration202610251900,
|
||||
migration202610291200,
|
||||
]
|
||||
@@ -1,268 +0,0 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import {
|
||||
LOCAL_OPENCODE_SERVER_ID,
|
||||
type SessionGoalStatus,
|
||||
type SessionGoalStopReason,
|
||||
type SessionGoalTurnState,
|
||||
type SessionGoalVerdict,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
|
||||
export interface SessionGoalRecord {
|
||||
id: number
|
||||
sessionId: string
|
||||
serverId: string
|
||||
directory: string
|
||||
objective: string
|
||||
status: SessionGoalStatus
|
||||
stopReason: SessionGoalStopReason | null
|
||||
turnState: SessionGoalTurnState
|
||||
continuationCount: number
|
||||
maxContinuations: number
|
||||
tokenBudget: number | null
|
||||
tokensAtStart: number
|
||||
tokensUsed: number
|
||||
consecutiveBlocked: number
|
||||
lastVerdict: SessionGoalVerdict | null
|
||||
lastReason: string | null
|
||||
createdAt: number
|
||||
updatedAt: number
|
||||
finishedAt: number | null
|
||||
}
|
||||
|
||||
export interface InsertSessionGoalInput {
|
||||
sessionId: string
|
||||
serverId?: string
|
||||
directory: string
|
||||
objective: string
|
||||
maxContinuations: number
|
||||
tokenBudget: number | null
|
||||
tokensAtStart: number
|
||||
}
|
||||
|
||||
export interface SessionGoalPatch {
|
||||
status?: SessionGoalStatus
|
||||
stopReason?: SessionGoalStopReason | null
|
||||
turnState?: SessionGoalTurnState
|
||||
continuationCount?: number
|
||||
tokensUsed?: number
|
||||
consecutiveBlocked?: number
|
||||
lastVerdict?: SessionGoalVerdict | null
|
||||
lastReason?: string | null
|
||||
finishedAt?: number | null
|
||||
}
|
||||
|
||||
interface SessionGoalRow {
|
||||
id: number
|
||||
session_id: string
|
||||
server_id: string
|
||||
directory: string
|
||||
objective: string
|
||||
status: SessionGoalStatus
|
||||
stop_reason: SessionGoalStopReason | null
|
||||
turn_state: SessionGoalTurnState
|
||||
continuation_count: number
|
||||
max_continuations: number
|
||||
token_budget: number | null
|
||||
tokens_at_start: number
|
||||
tokens_used: number
|
||||
consecutive_blocked: number
|
||||
last_verdict: SessionGoalVerdict | null
|
||||
last_reason: string | null
|
||||
created_at: number
|
||||
updated_at: number
|
||||
finished_at: number | null
|
||||
}
|
||||
|
||||
const SESSION_GOAL_COLUMNS = `
|
||||
id, session_id, server_id, directory, objective, status, stop_reason, turn_state,
|
||||
continuation_count, max_continuations, token_budget, tokens_at_start, tokens_used,
|
||||
consecutive_blocked, last_verdict, last_reason, created_at, updated_at, finished_at
|
||||
`
|
||||
|
||||
export function ensureSessionGoalsTable(db: Database): void {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS session_goals (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
server_id TEXT NOT NULL DEFAULT 'local',
|
||||
directory TEXT NOT NULL,
|
||||
objective TEXT NOT NULL,
|
||||
status TEXT NOT NULL CHECK(status IN ('active','paused','completed','blocked','stopped')),
|
||||
stop_reason TEXT,
|
||||
turn_state TEXT NOT NULL DEFAULT 'waiting',
|
||||
continuation_count INTEGER NOT NULL DEFAULT 0,
|
||||
max_continuations INTEGER NOT NULL,
|
||||
token_budget INTEGER,
|
||||
tokens_at_start INTEGER NOT NULL DEFAULT 0,
|
||||
tokens_used INTEGER NOT NULL DEFAULT 0,
|
||||
consecutive_blocked INTEGER NOT NULL DEFAULT 0,
|
||||
last_verdict TEXT,
|
||||
last_reason TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
finished_at INTEGER
|
||||
)
|
||||
`)
|
||||
db.run(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_session_goals_open
|
||||
ON session_goals(server_id, session_id)
|
||||
WHERE status IN ('active','paused')
|
||||
`)
|
||||
db.run(`
|
||||
CREATE INDEX IF NOT EXISTS idx_session_goals_session
|
||||
ON session_goals(session_id, created_at DESC)
|
||||
`)
|
||||
}
|
||||
|
||||
export function insertSessionGoal(db: Database, input: InsertSessionGoalInput): SessionGoalRecord | null {
|
||||
const now = Date.now()
|
||||
try {
|
||||
const result = db.prepare(`
|
||||
INSERT INTO session_goals(
|
||||
session_id, server_id, directory, objective, status, stop_reason, turn_state,
|
||||
continuation_count, max_continuations, token_budget, tokens_at_start, tokens_used,
|
||||
consecutive_blocked, last_verdict, last_reason, created_at, updated_at, finished_at
|
||||
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
`).run(
|
||||
input.sessionId,
|
||||
input.serverId ?? LOCAL_OPENCODE_SERVER_ID,
|
||||
input.directory,
|
||||
input.objective,
|
||||
'active',
|
||||
null,
|
||||
'waiting',
|
||||
0,
|
||||
input.maxContinuations,
|
||||
input.tokenBudget,
|
||||
input.tokensAtStart,
|
||||
0,
|
||||
0,
|
||||
null,
|
||||
null,
|
||||
now,
|
||||
now,
|
||||
null,
|
||||
)
|
||||
return getSessionGoalById(db, Number(result.lastInsertRowid))
|
||||
} catch (error) {
|
||||
if (isUniqueConstraintError(error)) {
|
||||
return null
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export function getSessionGoalById(db: Database, id: number): SessionGoalRecord | null {
|
||||
const row = db
|
||||
.prepare(`SELECT ${SESSION_GOAL_COLUMNS} FROM session_goals WHERE id = ?`)
|
||||
.get(id) as SessionGoalRow | undefined
|
||||
return row ? mapSessionGoalRow(row) : null
|
||||
}
|
||||
|
||||
export function getLatestSessionGoal(
|
||||
db: Database,
|
||||
sessionId: string,
|
||||
serverId: string = LOCAL_OPENCODE_SERVER_ID,
|
||||
): SessionGoalRecord | null {
|
||||
const row = db
|
||||
.prepare(`
|
||||
SELECT ${SESSION_GOAL_COLUMNS} FROM session_goals
|
||||
WHERE server_id = ? AND session_id = ?
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 1
|
||||
`)
|
||||
.get(serverId, sessionId) as SessionGoalRow | undefined
|
||||
return row ? mapSessionGoalRow(row) : null
|
||||
}
|
||||
|
||||
export function listOpenSessionGoals(db: Database): SessionGoalRecord[] {
|
||||
const rows = db
|
||||
.prepare(`SELECT ${SESSION_GOAL_COLUMNS} FROM session_goals WHERE status IN ('active','paused') ORDER BY created_at ASC`)
|
||||
.all() as SessionGoalRow[]
|
||||
return rows.map(mapSessionGoalRow)
|
||||
}
|
||||
|
||||
export function transitionSessionGoal(
|
||||
db: Database,
|
||||
id: number,
|
||||
fromStatuses: SessionGoalStatus[],
|
||||
patch: SessionGoalPatch,
|
||||
): SessionGoalRecord | null {
|
||||
const assignments: string[] = ['updated_at = ?']
|
||||
const values: (string | number | null)[] = [Date.now()]
|
||||
|
||||
if (patch.status !== undefined) {
|
||||
assignments.push('status = ?')
|
||||
values.push(patch.status)
|
||||
}
|
||||
if (patch.stopReason !== undefined) {
|
||||
assignments.push('stop_reason = ?')
|
||||
values.push(patch.stopReason)
|
||||
}
|
||||
if (patch.turnState !== undefined) {
|
||||
assignments.push('turn_state = ?')
|
||||
values.push(patch.turnState)
|
||||
}
|
||||
if (patch.continuationCount !== undefined) {
|
||||
assignments.push('continuation_count = ?')
|
||||
values.push(patch.continuationCount)
|
||||
}
|
||||
if (patch.tokensUsed !== undefined) {
|
||||
assignments.push('tokens_used = ?')
|
||||
values.push(patch.tokensUsed)
|
||||
}
|
||||
if (patch.consecutiveBlocked !== undefined) {
|
||||
assignments.push('consecutive_blocked = ?')
|
||||
values.push(patch.consecutiveBlocked)
|
||||
}
|
||||
if (patch.lastVerdict !== undefined) {
|
||||
assignments.push('last_verdict = ?')
|
||||
values.push(patch.lastVerdict)
|
||||
}
|
||||
if (patch.lastReason !== undefined) {
|
||||
assignments.push('last_reason = ?')
|
||||
values.push(patch.lastReason)
|
||||
}
|
||||
if (patch.finishedAt !== undefined) {
|
||||
assignments.push('finished_at = ?')
|
||||
values.push(patch.finishedAt)
|
||||
}
|
||||
|
||||
const placeholders = fromStatuses.map(() => '?').join(', ')
|
||||
const result = db
|
||||
.prepare(`UPDATE session_goals SET ${assignments.join(', ')} WHERE id = ? AND status IN (${placeholders})`)
|
||||
.run(...values, id, ...fromStatuses)
|
||||
|
||||
if (result.changes === 0) {
|
||||
return null
|
||||
}
|
||||
return getSessionGoalById(db, id)
|
||||
}
|
||||
|
||||
function mapSessionGoalRow(row: SessionGoalRow): SessionGoalRecord {
|
||||
return {
|
||||
id: row.id,
|
||||
sessionId: row.session_id,
|
||||
serverId: row.server_id,
|
||||
directory: row.directory,
|
||||
objective: row.objective,
|
||||
status: row.status,
|
||||
stopReason: row.stop_reason,
|
||||
turnState: row.turn_state,
|
||||
continuationCount: row.continuation_count,
|
||||
maxContinuations: row.max_continuations,
|
||||
tokenBudget: row.token_budget,
|
||||
tokensAtStart: row.tokens_at_start,
|
||||
tokensUsed: row.tokens_used,
|
||||
consecutiveBlocked: row.consecutive_blocked,
|
||||
lastVerdict: row.last_verdict,
|
||||
lastReason: row.last_reason,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
finishedAt: row.finished_at,
|
||||
}
|
||||
}
|
||||
|
||||
function isUniqueConstraintError(error: unknown): boolean {
|
||||
return error instanceof Error && /UNIQUE constraint failed/i.test(error.message)
|
||||
}
|
||||
Reference in new issue
Block a user