|
| 1 | +export * as SessionGoal from "./goal" |
| 2 | + |
| 3 | +import { eq } from "drizzle-orm" |
| 4 | +import { Context, Effect, Layer, Schema } from "effect" |
| 5 | +import { Database } from "../database/database" |
| 6 | +import { EventV2 } from "../event" |
| 7 | +import { SessionSchema } from "./schema" |
| 8 | +import { GoalTable } from "./sql" |
| 9 | + |
| 10 | +export const Status = Schema.Literals(["active", "paused", "completed"]) |
| 11 | +export type Status = typeof Status.Type |
| 12 | + |
| 13 | +export const Info = Schema.Struct({ |
| 14 | + text: Schema.String.annotate({ description: "The active goal for this session" }), |
| 15 | + status: Status, |
| 16 | + budgetTokens: Schema.optional(Schema.Number).annotate({ description: "Optional token budget for the goal" }), |
| 17 | + tokensUsed: Schema.Number, |
| 18 | + timeMs: Schema.Number, |
| 19 | + startedAt: Schema.Number, |
| 20 | + pausedAt: Schema.optional(Schema.Number), |
| 21 | + completedAt: Schema.optional(Schema.Number), |
| 22 | + verification: Schema.optional(Schema.String), |
| 23 | +}).annotate({ identifier: "SessionGoal.Info" }) |
| 24 | +export type Info = typeof Info.Type |
| 25 | + |
| 26 | +export const Event = { |
| 27 | + Updated: EventV2.define({ |
| 28 | + type: "goal.updated", |
| 29 | + schema: { |
| 30 | + sessionID: SessionSchema.ID, |
| 31 | + goal: Schema.NullOr(Info), |
| 32 | + }, |
| 33 | + }), |
| 34 | +} |
| 35 | + |
| 36 | +export interface Interface { |
| 37 | + readonly set: (input: { |
| 38 | + readonly sessionID: SessionSchema.ID |
| 39 | + readonly text: string |
| 40 | + readonly budgetTokens?: number |
| 41 | + }) => Effect.Effect<Info> |
| 42 | + readonly update: (input: { |
| 43 | + readonly sessionID: SessionSchema.ID |
| 44 | + readonly text?: string |
| 45 | + readonly status?: Status |
| 46 | + readonly budgetTokens?: number | null |
| 47 | + readonly verification?: string |
| 48 | + }) => Effect.Effect<Info | undefined> |
| 49 | + readonly pause: (sessionID: SessionSchema.ID) => Effect.Effect<Info | undefined> |
| 50 | + readonly resume: (sessionID: SessionSchema.ID) => Effect.Effect<Info | undefined> |
| 51 | + readonly clear: (sessionID: SessionSchema.ID) => Effect.Effect<void> |
| 52 | + readonly get: (sessionID: SessionSchema.ID) => Effect.Effect<Info | undefined> |
| 53 | + readonly recordUsage: (input: { |
| 54 | + readonly sessionID: SessionSchema.ID |
| 55 | + readonly tokens: number |
| 56 | + readonly durationMs: number |
| 57 | + }) => Effect.Effect<void> |
| 58 | +} |
| 59 | + |
| 60 | +export class Service extends Context.Service<Service, Interface>()("@opencode/v2/SessionGoal") {} |
| 61 | + |
| 62 | +export const layer = Layer.effect( |
| 63 | + Service, |
| 64 | + Effect.gen(function* () { |
| 65 | + const { db } = yield* Database.Service |
| 66 | + const events = yield* EventV2.Service |
| 67 | + |
| 68 | + const now = () => Date.now() |
| 69 | + |
| 70 | + const read = (sessionID: SessionSchema.ID) => |
| 71 | + db |
| 72 | + .select() |
| 73 | + .from(GoalTable) |
| 74 | + .where(eq(GoalTable.session_id, sessionID)) |
| 75 | + .get() |
| 76 | + .pipe(Effect.orDie) |
| 77 | + |
| 78 | + const publish = (sessionID: SessionSchema.ID, goal: Info | undefined) => |
| 79 | + events.publish(Event.Updated, { sessionID, goal: goal ?? null }) |
| 80 | + |
| 81 | + const toInfo = (row: { |
| 82 | + text: string |
| 83 | + status: string |
| 84 | + budget_tokens: number | null |
| 85 | + tokens_used: number |
| 86 | + time_ms: number |
| 87 | + started_at: number |
| 88 | + paused_at: number | null |
| 89 | + completed_at: number | null |
| 90 | + verification: string | null |
| 91 | + }): Info => ({ |
| 92 | + text: row.text, |
| 93 | + status: row.status as Status, |
| 94 | + budgetTokens: row.budget_tokens ?? undefined, |
| 95 | + tokensUsed: row.tokens_used, |
| 96 | + timeMs: row.time_ms, |
| 97 | + startedAt: row.started_at, |
| 98 | + pausedAt: row.paused_at ?? undefined, |
| 99 | + completedAt: row.completed_at ?? undefined, |
| 100 | + verification: row.verification ?? undefined, |
| 101 | + }) |
| 102 | + |
| 103 | + const set = Effect.fn("SessionGoal.set")(function* (input: { |
| 104 | + readonly sessionID: SessionSchema.ID |
| 105 | + readonly text: string |
| 106 | + readonly budgetTokens?: number |
| 107 | + }) { |
| 108 | + const ts = now() |
| 109 | + yield* db |
| 110 | + .insert(GoalTable) |
| 111 | + .values({ |
| 112 | + session_id: input.sessionID, |
| 113 | + text: input.text, |
| 114 | + status: "active", |
| 115 | + budget_tokens: input.budgetTokens ?? null, |
| 116 | + tokens_used: 0, |
| 117 | + time_ms: 0, |
| 118 | + started_at: ts, |
| 119 | + paused_at: null, |
| 120 | + completed_at: null, |
| 121 | + verification: null, |
| 122 | + time_created: ts, |
| 123 | + time_updated: ts, |
| 124 | + }) |
| 125 | + .onConflictDoUpdate({ |
| 126 | + target: GoalTable.session_id, |
| 127 | + set: { |
| 128 | + text: input.text, |
| 129 | + status: "active", |
| 130 | + budget_tokens: input.budgetTokens ?? null, |
| 131 | + paused_at: null, |
| 132 | + completed_at: null, |
| 133 | + verification: null, |
| 134 | + time_updated: ts, |
| 135 | + }, |
| 136 | + }) |
| 137 | + .run() |
| 138 | + .pipe(Effect.orDie) |
| 139 | + const row = yield* read(input.sessionID) |
| 140 | + const info = row ? toInfo(row) : ({ text: input.text, status: "active", tokensUsed: 0, timeMs: 0, startedAt: ts } as Info) |
| 141 | + yield* publish(input.sessionID, info) |
| 142 | + return info |
| 143 | + }) |
| 144 | + |
| 145 | + const update = Effect.fn("SessionGoal.update")(function* (input: { |
| 146 | + readonly sessionID: SessionSchema.ID |
| 147 | + readonly text?: string |
| 148 | + readonly status?: Status |
| 149 | + readonly budgetTokens?: number | null |
| 150 | + readonly verification?: string |
| 151 | + }) { |
| 152 | + const current = yield* read(input.sessionID) |
| 153 | + if (!current) return undefined |
| 154 | + const ts = now() |
| 155 | + const status = input.status |
| 156 | + const next = { |
| 157 | + text: input.text ?? current.text, |
| 158 | + status: status ?? (current.status as Status), |
| 159 | + budget_tokens: input.budgetTokens === undefined ? current.budget_tokens : input.budgetTokens, |
| 160 | + tokens_used: current.tokens_used, |
| 161 | + time_ms: current.time_ms, |
| 162 | + started_at: current.started_at, |
| 163 | + paused_at: current.paused_at, |
| 164 | + completed_at: current.completed_at, |
| 165 | + verification: input.verification ?? current.verification, |
| 166 | + time_updated: ts, |
| 167 | + } |
| 168 | + if (status === "paused" && !next.paused_at) next.paused_at = ts |
| 169 | + if (status === "active" && next.paused_at) next.paused_at = null |
| 170 | + if (status === "completed" && !next.completed_at) next.completed_at = ts |
| 171 | + yield* db |
| 172 | + .update(GoalTable) |
| 173 | + .set(next) |
| 174 | + .where(eq(GoalTable.session_id, input.sessionID)) |
| 175 | + .run() |
| 176 | + .pipe(Effect.orDie) |
| 177 | + const row = yield* read(input.sessionID) |
| 178 | + const info = row ? toInfo(row) : undefined |
| 179 | + yield* publish(input.sessionID, info) |
| 180 | + return info |
| 181 | + }) |
| 182 | + |
| 183 | + const pause = Effect.fn("SessionGoal.pause")(function* (sessionID: SessionSchema.ID) { |
| 184 | + return yield* update({ sessionID, status: "paused" }) |
| 185 | + }) |
| 186 | + |
| 187 | + const resume = Effect.fn("SessionGoal.resume")(function* (sessionID: SessionSchema.ID) { |
| 188 | + return yield* update({ sessionID, status: "active" }) |
| 189 | + }) |
| 190 | + |
| 191 | + const clear = Effect.fn("SessionGoal.clear")(function* (sessionID: SessionSchema.ID) { |
| 192 | + yield* db.delete(GoalTable).where(eq(GoalTable.session_id, sessionID)).run().pipe(Effect.orDie) |
| 193 | + yield* publish(sessionID, undefined) |
| 194 | + }) |
| 195 | + |
| 196 | + const get = Effect.fn("SessionGoal.get")(function* (sessionID: SessionSchema.ID) { |
| 197 | + const row = yield* read(sessionID) |
| 198 | + return row ? toInfo(row) : undefined |
| 199 | + }) |
| 200 | + |
| 201 | + const recordUsage = Effect.fn("SessionGoal.recordUsage")(function* (input: { |
| 202 | + readonly sessionID: SessionSchema.ID |
| 203 | + readonly tokens: number |
| 204 | + readonly durationMs: number |
| 205 | + }) { |
| 206 | + const current = yield* read(input.sessionID) |
| 207 | + if (!current || (current.status as Status) !== "active") return |
| 208 | + const ts = now() |
| 209 | + yield* db |
| 210 | + .update(GoalTable) |
| 211 | + .set({ |
| 212 | + tokens_used: current.tokens_used + Math.max(0, Math.floor(input.tokens)), |
| 213 | + time_ms: current.time_ms + Math.max(0, Math.floor(input.durationMs)), |
| 214 | + time_updated: ts, |
| 215 | + }) |
| 216 | + .where(eq(GoalTable.session_id, input.sessionID)) |
| 217 | + .run() |
| 218 | + .pipe(Effect.orDie) |
| 219 | + const row = yield* read(input.sessionID) |
| 220 | + if (row) yield* publish(input.sessionID, toInfo(row)) |
| 221 | + }) |
| 222 | + |
| 223 | + return Service.of({ set, update, pause, resume, clear, get, recordUsage }) |
| 224 | + }), |
| 225 | +) |
| 226 | + |
| 227 | +export const defaultLayer = layer.pipe(Layer.provide(EventV2.defaultLayer), Layer.provide(Database.defaultLayer)) |
0 commit comments