8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
161 lines
5.1 KiB
TypeScript
161 lines
5.1 KiB
TypeScript
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { Effect, Layer, Context, Schema } from "effect"
|
|
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
|
import { EventV2Bridge } from "@/event-v2-bridge"
|
|
import { Snapshot } from "@/snapshot"
|
|
import { Session } from "./session"
|
|
import { SessionID, MessageID } from "./schema"
|
|
import { Config } from "@/config/config"
|
|
|
|
function unquoteGitPath(input: string) {
|
|
if (!input.startsWith('"')) return input
|
|
if (!input.endsWith('"')) return input
|
|
const body = input.slice(1, -1)
|
|
const bytes: number[] = []
|
|
|
|
for (let i = 0; i < body.length; i++) {
|
|
const char = body[i]!
|
|
if (char !== "\\") {
|
|
bytes.push(char.charCodeAt(0))
|
|
continue
|
|
}
|
|
|
|
const next = body[i + 1]
|
|
if (!next) {
|
|
bytes.push("\\".charCodeAt(0))
|
|
continue
|
|
}
|
|
|
|
if (next >= "0" && next <= "7") {
|
|
const chunk = body.slice(i + 1, i + 4)
|
|
const match = chunk.match(/^[0-7]{1,3}/)
|
|
if (!match) {
|
|
bytes.push(next.charCodeAt(0))
|
|
i++
|
|
continue
|
|
}
|
|
bytes.push(parseInt(match[0], 8))
|
|
i += match[0].length
|
|
continue
|
|
}
|
|
|
|
const escaped =
|
|
next === "n"
|
|
? "\n"
|
|
: next === "r"
|
|
? "\r"
|
|
: next === "t"
|
|
? "\t"
|
|
: next === "b"
|
|
? "\b"
|
|
: next === "f"
|
|
? "\f"
|
|
: next === "v"
|
|
? "\v"
|
|
: next === "\\" || next === '"'
|
|
? next
|
|
: undefined
|
|
|
|
bytes.push((escaped ?? next).charCodeAt(0))
|
|
i++
|
|
}
|
|
|
|
return Buffer.from(bytes).toString()
|
|
}
|
|
|
|
export interface Interface {
|
|
readonly summarize: (input: { sessionID: SessionID; messageID: MessageID }) => Effect.Effect<void>
|
|
readonly diff: (input: { sessionID: SessionID; messageID?: MessageID }) => Effect.Effect<Snapshot.FileDiff[]>
|
|
readonly computeDiff: (input: { messages: SessionV1.WithParts[] }) => Effect.Effect<Snapshot.FileDiff[]>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionSummary") {}
|
|
|
|
const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const sessions = yield* Session.Service
|
|
const snapshot = yield* Snapshot.Service
|
|
const events = yield* EventV2Bridge.Service
|
|
const config = yield* Config.Service
|
|
|
|
const computeDiff = Effect.fn("SessionSummary.computeDiff")(function* (input: { messages: SessionV1.WithParts[] }) {
|
|
let from: string | undefined
|
|
let to: string | undefined
|
|
for (const item of input.messages) {
|
|
if (!from) {
|
|
for (const part of item.parts) {
|
|
if (part.type === "step-start" && part.snapshot) {
|
|
from = part.snapshot
|
|
break
|
|
}
|
|
}
|
|
}
|
|
for (const part of item.parts) {
|
|
if (part.type === "step-finish" && part.snapshot) to = part.snapshot
|
|
}
|
|
}
|
|
if (from && to) return yield* snapshot.diffFull(from, to)
|
|
return []
|
|
})
|
|
|
|
const summarize = Effect.fn("SessionSummary.summarize")(function* (input: {
|
|
sessionID: SessionID
|
|
messageID: MessageID
|
|
}) {
|
|
yield* sessions.setSummary({
|
|
sessionID: input.sessionID,
|
|
summary: {
|
|
additions: 0,
|
|
deletions: 0,
|
|
files: 0,
|
|
},
|
|
})
|
|
yield* events.publish(Session.Event.Diff, { sessionID: input.sessionID, diff: [] })
|
|
if ((yield* config.get()).snapshot === false) return
|
|
const all = yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)
|
|
if (!all.length) return
|
|
|
|
const messages = all.filter(
|
|
(m) => m.info.id === input.messageID || (m.info.role === "assistant" && m.info.parentID === input.messageID),
|
|
)
|
|
const target = messages.find((m) => m.info.id === input.messageID)
|
|
if (!target || target.info.role !== "user") return
|
|
const msgDiffs = yield* computeDiff({ messages })
|
|
target.info.summary = { ...target.info.summary, diffs: msgDiffs }
|
|
yield* sessions.updateMessage(target.info)
|
|
})
|
|
|
|
const diff = Effect.fn("SessionSummary.diff")(function* (input: { sessionID: SessionID; messageID?: MessageID }) {
|
|
if (!input.messageID) return []
|
|
const message = (yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)).find(
|
|
(item) => item.info.id === input.messageID,
|
|
)
|
|
if (!message || message.info.role !== "user") return []
|
|
const diffs = message.info.summary?.diffs ?? []
|
|
return diffs.map((item) => {
|
|
if (item.file === undefined) return item
|
|
const file = unquoteGitPath(item.file)
|
|
if (file === item.file) return item
|
|
return { ...item, file }
|
|
})
|
|
})
|
|
|
|
return Service.of({ summarize, diff, computeDiff })
|
|
}),
|
|
)
|
|
|
|
export const DiffInput = Schema.Struct({
|
|
sessionID: SessionID,
|
|
messageID: Schema.optional(MessageID),
|
|
})
|
|
export type DiffInput = Schema.Schema.Type<typeof DiffInput>
|
|
|
|
export const node = LayerNode.make({
|
|
service: Service,
|
|
layer: layer,
|
|
deps: [Session.node, Snapshot.node, EventV2Bridge.node, Config.node],
|
|
})
|
|
|
|
export * as SessionSummary from "./summary"
|