Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP INDEX IF EXISTS `aiv_event_session_idx`;
--> statement-breakpoint
DROP INDEX IF EXISTS `aiv_event_type_idx`;
--> statement-breakpoint
CREATE INDEX `aiv_state_time_updated_idx` ON `aiv_state` (`time_updated`);
32 changes: 17 additions & 15 deletions packages/opencode/src/aiv/aiv.sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ export const AivEventTable = sqliteTable(
...Timestamps,
},
(table) => [
index("aiv_event_session_idx").on(table.session_id),
index("aiv_event_session_time_idx").on(table.session_id, table.time_created),
index("aiv_event_type_idx").on(table.type),
],
)

/** Materialized current state per session (upserted from in-memory state) */
export const AivStateTable = sqliteTable("aiv_state", {
session_id: text()
.$type<SessionID>()
.primaryKey()
.references(() => SessionTable.id, { onDelete: "cascade" }),
summary: text(),
work_type: text().notNull().default("unknown"),
location: text().notNull().default("unknown"),
scope_files: integer().notNull().default(0),
scope_modules: integer().notNull().default(0),
strategy_changes: text({ mode: "json" }).$type<{ from: string; to: string; timestamp: number }[]>(),
...Timestamps,
})
export const AivStateTable = sqliteTable(
"aiv_state",
{
session_id: text()
.$type<SessionID>()
.primaryKey()
.references(() => SessionTable.id, { onDelete: "cascade" }),
summary: text(),
work_type: text().notNull().default("unknown"),
location: text().notNull().default("unknown"),
scope_files: integer().notNull().default(0),
scope_modules: integer().notNull().default(0),
strategy_changes: text({ mode: "json" }).$type<{ from: string; to: string; timestamp: number }[]>(),
...Timestamps,
},
(table) => [index("aiv_state_time_updated_idx").on(table.time_updated)],
)
46 changes: 19 additions & 27 deletions packages/opencode/src/aiv/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,30 @@ export namespace AivPersistence {
strategyChanges: AivSchema.StrategyChange[]
}) {
try {
Database.transaction((tx) => {
const existing = tx
.select()
.from(AivStateTable)
.where(eq(AivStateTable.session_id, input.sessionID as SessionID))
.get()

if (existing) {
tx.update(AivStateTable)
.set({
summary: input.summary ?? existing.summary,
work_type: input.workType,
location: input.location,
scope_files: input.scopeFiles,
scope_modules: input.scopeModules,
strategy_changes: input.strategyChanges,
})
.where(eq(AivStateTable.session_id, input.sessionID as SessionID))
.run()
} else {
tx.insert(AivStateTable)
.values({
session_id: input.sessionID as SessionID,
summary: input.summary ?? null,
Database.use((db) => {
db.insert(AivStateTable)
.values({
session_id: input.sessionID as SessionID,
summary: input.summary ?? null,
work_type: input.workType,
location: input.location,
scope_files: input.scopeFiles,
scope_modules: input.scopeModules,
strategy_changes: input.strategyChanges,
})
.onConflictDoUpdate({
target: AivStateTable.session_id,
set: {
summary: input.summary ?? undefined,
work_type: input.workType,
location: input.location,
scope_files: input.scopeFiles,
scope_modules: input.scopeModules,
strategy_changes: input.strategyChanges,
})
.run()
}
time_updated: Date.now(),
},
})
.run()
})
} catch (e) {
log.error("failed to upsert state", { error: e })
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/server/routes/aiv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ export const AIVRoutes = lazy(() =>
},
},
}),
validator("param", z.object({ sessionID: SessionID.zod })),
validator(
"query",
z.object({
limit: z.coerce.number().int().min(1).max(1000).default(100),
}),
),
async (c) => {
const sessionID = c.req.param("sessionID")
const { sessionID } = c.req.valid("param")
const { limit } = c.req.valid("query")
const events = AivPersistence.timeline(sessionID, limit)
return c.json(events)
Expand Down