refactor: unwrap SessionEntry namespace + self-reexport (#22977)
This commit is contained in:
@@ -2,19 +2,18 @@ import { Schema } from "effect"
|
|||||||
import { SessionEvent } from "./session-event"
|
import { SessionEvent } from "./session-event"
|
||||||
import { produce } from "immer"
|
import { produce } from "immer"
|
||||||
|
|
||||||
export namespace SessionEntry {
|
export const ID = SessionEvent.ID
|
||||||
export const ID = SessionEvent.ID
|
export type ID = Schema.Schema.Type<typeof ID>
|
||||||
export type ID = Schema.Schema.Type<typeof ID>
|
|
||||||
|
|
||||||
const Base = {
|
const Base = {
|
||||||
id: SessionEvent.ID,
|
id: SessionEvent.ID,
|
||||||
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
|
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
|
||||||
time: Schema.Struct({
|
time: Schema.Struct({
|
||||||
created: Schema.DateTimeUtc,
|
created: Schema.DateTimeUtc,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
export class User extends Schema.Class<User>("Session.Entry.User")({
|
export class User extends Schema.Class<User>("Session.Entry.User")({
|
||||||
...Base,
|
...Base,
|
||||||
text: SessionEvent.Prompt.fields.text,
|
text: SessionEvent.Prompt.fields.text,
|
||||||
files: SessionEvent.Prompt.fields.files,
|
files: SessionEvent.Prompt.fields.files,
|
||||||
@@ -23,7 +22,7 @@ export namespace SessionEntry {
|
|||||||
time: Schema.Struct({
|
time: Schema.Struct({
|
||||||
created: Schema.DateTimeUtc,
|
created: Schema.DateTimeUtc,
|
||||||
}),
|
}),
|
||||||
}) {
|
}) {
|
||||||
static fromEvent(event: SessionEvent.Prompt) {
|
static fromEvent(event: SessionEvent.Prompt) {
|
||||||
return new User({
|
return new User({
|
||||||
id: event.id,
|
id: event.id,
|
||||||
@@ -35,46 +34,46 @@ export namespace SessionEntry {
|
|||||||
time: { created: event.timestamp },
|
time: { created: event.timestamp },
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Synthetic extends Schema.Class<Synthetic>("Session.Entry.Synthetic")({
|
export class Synthetic extends Schema.Class<Synthetic>("Session.Entry.Synthetic")({
|
||||||
...SessionEvent.Synthetic.fields,
|
...SessionEvent.Synthetic.fields,
|
||||||
...Base,
|
...Base,
|
||||||
type: Schema.Literal("synthetic"),
|
type: Schema.Literal("synthetic"),
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export class ToolStatePending extends Schema.Class<ToolStatePending>("Session.Entry.ToolState.Pending")({
|
export class ToolStatePending extends Schema.Class<ToolStatePending>("Session.Entry.ToolState.Pending")({
|
||||||
status: Schema.Literal("pending"),
|
status: Schema.Literal("pending"),
|
||||||
input: Schema.String,
|
input: Schema.String,
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export class ToolStateRunning extends Schema.Class<ToolStateRunning>("Session.Entry.ToolState.Running")({
|
export class ToolStateRunning extends Schema.Class<ToolStateRunning>("Session.Entry.ToolState.Running")({
|
||||||
status: Schema.Literal("running"),
|
status: Schema.Literal("running"),
|
||||||
input: Schema.Record(Schema.String, Schema.Unknown),
|
input: Schema.Record(Schema.String, Schema.Unknown),
|
||||||
title: Schema.String.pipe(Schema.optional),
|
title: Schema.String.pipe(Schema.optional),
|
||||||
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
|
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export class ToolStateCompleted extends Schema.Class<ToolStateCompleted>("Session.Entry.ToolState.Completed")({
|
export class ToolStateCompleted extends Schema.Class<ToolStateCompleted>("Session.Entry.ToolState.Completed")({
|
||||||
status: Schema.Literal("completed"),
|
status: Schema.Literal("completed"),
|
||||||
input: Schema.Record(Schema.String, Schema.Unknown),
|
input: Schema.Record(Schema.String, Schema.Unknown),
|
||||||
output: Schema.String,
|
output: Schema.String,
|
||||||
title: Schema.String,
|
title: Schema.String,
|
||||||
metadata: Schema.Record(Schema.String, Schema.Unknown),
|
metadata: Schema.Record(Schema.String, Schema.Unknown),
|
||||||
attachments: SessionEvent.FileAttachment.pipe(Schema.Array, Schema.optional),
|
attachments: SessionEvent.FileAttachment.pipe(Schema.Array, Schema.optional),
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export class ToolStateError extends Schema.Class<ToolStateError>("Session.Entry.ToolState.Error")({
|
export class ToolStateError extends Schema.Class<ToolStateError>("Session.Entry.ToolState.Error")({
|
||||||
status: Schema.Literal("error"),
|
status: Schema.Literal("error"),
|
||||||
input: Schema.Record(Schema.String, Schema.Unknown),
|
input: Schema.Record(Schema.String, Schema.Unknown),
|
||||||
error: Schema.String,
|
error: Schema.String,
|
||||||
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
|
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export const ToolState = Schema.Union([ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
|
export const ToolState = Schema.Union([ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
|
||||||
export type ToolState = Schema.Schema.Type<typeof ToolState>
|
export type ToolState = Schema.Schema.Type<typeof ToolState>
|
||||||
|
|
||||||
export class AssistantTool extends Schema.Class<AssistantTool>("Session.Entry.Assistant.Tool")({
|
export class AssistantTool extends Schema.Class<AssistantTool>("Session.Entry.Assistant.Tool")({
|
||||||
type: Schema.Literal("tool"),
|
type: Schema.Literal("tool"),
|
||||||
callID: Schema.String,
|
callID: Schema.String,
|
||||||
name: Schema.String,
|
name: Schema.String,
|
||||||
@@ -85,22 +84,22 @@ export namespace SessionEntry {
|
|||||||
completed: Schema.DateTimeUtc.pipe(Schema.optional),
|
completed: Schema.DateTimeUtc.pipe(Schema.optional),
|
||||||
pruned: Schema.DateTimeUtc.pipe(Schema.optional),
|
pruned: Schema.DateTimeUtc.pipe(Schema.optional),
|
||||||
}),
|
}),
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export class AssistantText extends Schema.Class<AssistantText>("Session.Entry.Assistant.Text")({
|
export class AssistantText extends Schema.Class<AssistantText>("Session.Entry.Assistant.Text")({
|
||||||
type: Schema.Literal("text"),
|
type: Schema.Literal("text"),
|
||||||
text: Schema.String,
|
text: Schema.String,
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export class AssistantReasoning extends Schema.Class<AssistantReasoning>("Session.Entry.Assistant.Reasoning")({
|
export class AssistantReasoning extends Schema.Class<AssistantReasoning>("Session.Entry.Assistant.Reasoning")({
|
||||||
type: Schema.Literal("reasoning"),
|
type: Schema.Literal("reasoning"),
|
||||||
text: Schema.String,
|
text: Schema.String,
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export const AssistantContent = Schema.Union([AssistantText, AssistantReasoning, AssistantTool])
|
export const AssistantContent = Schema.Union([AssistantText, AssistantReasoning, AssistantTool])
|
||||||
export type AssistantContent = Schema.Schema.Type<typeof AssistantContent>
|
export type AssistantContent = Schema.Schema.Type<typeof AssistantContent>
|
||||||
|
|
||||||
export class Assistant extends Schema.Class<Assistant>("Session.Entry.Assistant")({
|
export class Assistant extends Schema.Class<Assistant>("Session.Entry.Assistant")({
|
||||||
...Base,
|
...Base,
|
||||||
type: Schema.Literal("assistant"),
|
type: Schema.Literal("assistant"),
|
||||||
content: AssistantContent.pipe(Schema.Array),
|
content: AssistantContent.pipe(Schema.Array),
|
||||||
@@ -119,26 +118,26 @@ export namespace SessionEntry {
|
|||||||
created: Schema.DateTimeUtc,
|
created: Schema.DateTimeUtc,
|
||||||
completed: Schema.DateTimeUtc.pipe(Schema.optional),
|
completed: Schema.DateTimeUtc.pipe(Schema.optional),
|
||||||
}),
|
}),
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export class Compaction extends Schema.Class<Compaction>("Session.Entry.Compaction")({
|
export class Compaction extends Schema.Class<Compaction>("Session.Entry.Compaction")({
|
||||||
...SessionEvent.Compacted.fields,
|
...SessionEvent.Compacted.fields,
|
||||||
type: Schema.Literal("compaction"),
|
type: Schema.Literal("compaction"),
|
||||||
...Base,
|
...Base,
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
export const Entry = Schema.Union([User, Synthetic, Assistant, Compaction])
|
export const Entry = Schema.Union([User, Synthetic, Assistant, Compaction])
|
||||||
|
|
||||||
export type Entry = Schema.Schema.Type<typeof Entry>
|
export type Entry = Schema.Schema.Type<typeof Entry>
|
||||||
|
|
||||||
export type Type = Entry["type"]
|
export type Type = Entry["type"]
|
||||||
|
|
||||||
export type History = {
|
export type History = {
|
||||||
entries: Entry[]
|
entries: Entry[]
|
||||||
pending: Entry[]
|
pending: Entry[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function step(old: History, event: SessionEvent.Event): History {
|
export function step(old: History, event: SessionEvent.Event): History {
|
||||||
return produce(old, (draft) => {
|
return produce(old, (draft) => {
|
||||||
const lastAssistant = draft.entries.findLast((x) => x.type === "assistant")
|
const lastAssistant = draft.entries.findLast((x) => x.type === "assistant")
|
||||||
const pendingAssistant = lastAssistant && !lastAssistant.time.completed ? lastAssistant : undefined
|
const pendingAssistant = lastAssistant && !lastAssistant.time.completed ? lastAssistant : undefined
|
||||||
@@ -279,17 +278,17 @@ export namespace SessionEntry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly decode: (row: typeof SessionEntryTable.$inferSelect) => Entry
|
readonly decode: (row: typeof SessionEntryTable.$inferSelect) => Entry
|
||||||
readonly fromSession: (sessionID: SessionID) => Effect.Effect<Entry[], never>
|
readonly fromSession: (sessionID: SessionID) => Effect.Effect<Entry[], never>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionEntry") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionEntry") {}
|
||||||
|
|
||||||
export const layer: Layer.Layer<Service, never, never> = Layer.effect(
|
export const layer: Layer.Layer<Service, never, never> = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const decodeEntry = Schema.decodeUnknownSync(Entry)
|
const decodeEntry = Schema.decodeUnknownSync(Entry)
|
||||||
@@ -313,6 +312,7 @@ export namespace SessionEntry {
|
|||||||
fromSession,
|
fromSession,
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
*/
|
*/
|
||||||
}
|
|
||||||
|
export * as SessionEntry from "./session-entry"
|
||||||
|
|||||||
Reference in New Issue
Block a user