Skip to content

Commit cd2e518

Browse files
authored
Merge branch 'dev' into feat/agent-order-field
2 parents bfb22d5 + 59c0fc2 commit cd2e518

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

packages/opencode/src/session/session.sql.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Timestamps } from "../storage/schema.sql"
1111

1212
type PartData = Omit<MessageV2.Part, "id" | "sessionID" | "messageID">
1313
type InfoData = Omit<MessageV2.Info, "id" | "sessionID">
14-
type EntryData = Omit<SessionEntry.Entry, "id" | "type">
1514

1615
export const SessionTable = sqliteTable(
1716
"session",
@@ -104,7 +103,7 @@ export const SessionEntryTable = sqliteTable(
104103
.$type<SessionID>()
105104
.notNull()
106105
.references(() => SessionTable.id, { onDelete: "cascade" }),
107-
type: text().notNull(),
106+
type: text().$type<SessionEntry.Type>().notNull(),
108107
...Timestamps,
109108
data: text({ mode: "json" }).notNull().$type<Omit<SessionEntry.Entry, "type" | "id">>(),
110109
},

packages/opencode/src/v2/session-entry.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Identifier } from "@/id/id"
2+
import { Database } from "@/node"
3+
import type { SessionID } from "@/session/schema"
4+
import { SessionEntryTable } from "@/session/session.sql"
25
import { withStatics } from "@/util/schema"
3-
import { DateTime, Effect, Schema } from "effect"
6+
import { Context, DateTime, Effect, Layer, Schema } from "effect"
7+
import { eq } from "../storage/db"
48

59
export namespace SessionEntry {
610
export const ID = Schema.String.pipe(Schema.brand("Session.Entry.ID")).pipe(
@@ -181,6 +185,43 @@ export namespace SessionEntry {
181185
overflow: Schema.Boolean.pipe(Schema.optional),
182186
}) {}
183187

184-
export const Entry = Schema.Union([User, Synthetic, Request, Tool, Text, Reasoning, Complete, Retry, Compaction])
188+
export const Entry = Schema.Union([User, Synthetic, Request, Tool, Text, Reasoning, Complete, Retry, Compaction], {
189+
mode: "oneOf",
190+
})
185191
export type Entry = Schema.Schema.Type<typeof Entry>
192+
193+
export type Type = Entry["type"]
194+
195+
export interface Interface {
196+
readonly decode: (row: typeof SessionEntryTable.$inferSelect) => Entry
197+
readonly fromSession: (sessionID: SessionID) => Effect.Effect<Entry[], never>
198+
}
199+
200+
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionEntry") {}
201+
202+
export const layer: Layer.Layer<Service, never, never> = Layer.effect(
203+
Service,
204+
Effect.gen(function* () {
205+
const decodeEntry = Schema.decodeUnknownSync(Entry)
206+
207+
const decode: (typeof Service.Service)["decode"] = (row) => decodeEntry({ ...row, id: row.id, type: row.type })
208+
209+
const fromSession = Effect.fn("SessionEntry.fromSession")(function* (sessionID: SessionID) {
210+
return Database.use((db) =>
211+
db
212+
.select()
213+
.from(SessionEntryTable)
214+
.where(eq(SessionEntryTable.session_id, sessionID))
215+
.orderBy(SessionEntryTable.id)
216+
.all()
217+
.map((row) => decode(row)),
218+
)
219+
})
220+
221+
return Service.of({
222+
decode,
223+
fromSession,
224+
})
225+
}),
226+
)
186227
}

0 commit comments

Comments
 (0)