mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
refactor(core): avoid redundant decode and re-branding in session inbox and projector (#57890)
This commit is contained in:
committed by
GitHub
Unverified
parent
863ea41364
commit
6dfc681835
@@ -15,6 +15,7 @@ import { existsSync } from "node:fs"
|
||||
import path from "node:path"
|
||||
import type { Database as SQLiteDatabase } from "bun:sqlite"
|
||||
import { Project } from "@opencode/schema/project"
|
||||
import { Agent } from "@opencode/schema/agent"
|
||||
|
||||
export type SourceMessage = {
|
||||
readonly id: string
|
||||
@@ -469,7 +470,7 @@ export function transformSession(input: TransformInput): TransformResult {
|
||||
return {
|
||||
messages: migrated,
|
||||
session: {
|
||||
agent: input.session.agent ?? (latestUser?.value.role === "user" ? latestUser.value.agent : null),
|
||||
agent: input.session.agent ?? (latestUser?.value.role === "user" ? Agent.ID.make(latestUser.value.agent) : null),
|
||||
model:
|
||||
input.session.model ??
|
||||
(latestUser?.value.role === "user"
|
||||
|
||||
@@ -131,15 +131,24 @@ const promotedFromMessage = Effect.fn("SessionInbox.promotedFromMessage")(functi
|
||||
return User.make({
|
||||
...base,
|
||||
type: "user",
|
||||
payload: decodeUser(message),
|
||||
payload: UserPayload.make({
|
||||
text: message.text,
|
||||
files: message.files,
|
||||
agents: message.agents,
|
||||
skills: message.skills,
|
||||
metadata: message.metadata,
|
||||
}),
|
||||
})
|
||||
if (message.type === "synthetic")
|
||||
return Synthetic.make({
|
||||
...base,
|
||||
type: "synthetic",
|
||||
payload: decodeSynthetic(message),
|
||||
payload: SyntheticPayload.make({
|
||||
text: message.text,
|
||||
description: message.description,
|
||||
metadata: message.metadata,
|
||||
}),
|
||||
})
|
||||
return yield* new LifecycleConflict({ id })
|
||||
})
|
||||
|
||||
export type Interface = Effect.Success<ReturnType<typeof make>>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { DateTime, Schema } from "effect"
|
||||
import { Agent } from "@opencode/schema/agent"
|
||||
import { Location } from "@opencode/schema/location"
|
||||
import { Model } from "@opencode/schema/model"
|
||||
import { Project } from "@opencode/schema/project"
|
||||
@@ -26,7 +25,7 @@ export function fromRow(row: typeof SessionTable.$inferSelect): SessionSchema.In
|
||||
boundary: row.fork_boundary,
|
||||
}
|
||||
: undefined,
|
||||
agent: row.agent ? Agent.ID.make(row.agent) : undefined,
|
||||
agent: row.agent ?? undefined,
|
||||
model: row.model
|
||||
? {
|
||||
id: Model.ID.make(row.model.id),
|
||||
|
||||
@@ -6,7 +6,6 @@ import path from "path"
|
||||
import { Database } from "../database/database.js"
|
||||
import { Bus } from "../bus.js"
|
||||
import { makeGlobalNode } from "@opencode/util/effect/app-node"
|
||||
import { Agent } from "@opencode/schema/agent"
|
||||
import { Model } from "@opencode/schema/model"
|
||||
import { SessionEvent } from "./event.js"
|
||||
import { SessionMessage } from "./message.js"
|
||||
@@ -257,13 +256,13 @@ function run(db: DatabaseService, event: MessageEvent) {
|
||||
return Effect.gen(function* () {
|
||||
const updateMessage = (message: SessionMessage.Info) => {
|
||||
const encoded = encodeMessage(message)
|
||||
const { id, type, ...data } = encoded
|
||||
const { id: _, type, ...data } = encoded
|
||||
return db
|
||||
.update(SessionMessageTable)
|
||||
.set({ type, time_created: DateTime.toEpochMillis(message.time.created), data })
|
||||
.where(
|
||||
and(
|
||||
eq(SessionMessageTable.id, SessionMessage.ID.make(id)),
|
||||
eq(SessionMessageTable.id, message.id),
|
||||
eq(SessionMessageTable.session_id, event.data.sessionID),
|
||||
),
|
||||
)
|
||||
@@ -280,7 +279,7 @@ function run(db: DatabaseService, event: MessageEvent) {
|
||||
.get()
|
||||
.pipe(
|
||||
Effect.orDie,
|
||||
Effect.map((row) => (row?.agent ? Agent.ID.make(row.agent) : undefined)),
|
||||
Effect.map((row) => row?.agent ?? undefined),
|
||||
)
|
||||
},
|
||||
getModel() {
|
||||
@@ -411,11 +410,11 @@ function run(db: DatabaseService, event: MessageEvent) {
|
||||
|
||||
function insertMessage(db: DatabaseService, event: SessionEvent.DurableEvent, message: SessionMessage.Info) {
|
||||
const encoded = encodeMessage(message)
|
||||
const { id, type, ...data } = encoded
|
||||
const { id: _, type, ...data } = encoded
|
||||
return db
|
||||
.insert(SessionMessageTable)
|
||||
.values({
|
||||
id: SessionMessage.ID.make(id),
|
||||
id: message.id,
|
||||
session_id: event.data.sessionID,
|
||||
type,
|
||||
seq: event.durable.seq,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { directoryColumn, pathColumn } from "../database/path.js"
|
||||
import { ProjectTable } from "../project/sql.js"
|
||||
import type { SessionMessage } from "./message.js"
|
||||
import type { SessionInbox } from "./inbox.js"
|
||||
import type { Agent } from "@opencode/schema/agent"
|
||||
import type { FileDiff } from "@opencode/schema/file-diff"
|
||||
import type { Permission } from "@opencode/schema/permission"
|
||||
import type { Project } from "@opencode/schema/project"
|
||||
@@ -50,7 +51,7 @@ export const SessionTable = sqliteTable(
|
||||
tokens_cache_write: integer().notNull().default(0),
|
||||
revert: text({ mode: "json" }).$type<Session.Revert | RevertV1>(),
|
||||
permission: text({ mode: "json" }).$type<Permission.Ruleset>(),
|
||||
agent: text(),
|
||||
agent: text().$type<Agent.ID>(),
|
||||
model: text({ mode: "json" }).$type<{
|
||||
id: string
|
||||
providerID: string
|
||||
|
||||
@@ -49,7 +49,7 @@ function setup(rules: Permission.Ruleset = [], sessionID = Session.ID.make("ses_
|
||||
directory: "/project",
|
||||
title: "test",
|
||||
version: "test",
|
||||
agent: "test",
|
||||
agent: Agent.ID.make("test"),
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
.run()
|
||||
|
||||
@@ -921,7 +921,7 @@ describe("Session.create", () => {
|
||||
const input = { id, location }
|
||||
const created = yield* session.create(input)
|
||||
|
||||
yield* db.update(SessionTable).set({ agent: "build" }).where(eq(SessionTable.id, id)).run().pipe(Effect.orDie)
|
||||
yield* db.update(SessionTable).set({ agent: Agent.defaultID }).where(eq(SessionTable.id, id)).run().pipe(Effect.orDie)
|
||||
|
||||
expect(yield* session.create(input)).toMatchObject({ id: created.id, agent: "build" })
|
||||
}),
|
||||
|
||||
@@ -331,7 +331,7 @@ describe("SessionProjector", () => {
|
||||
|
||||
it.effect("projects durable context messages supported by the updater", () =>
|
||||
Effect.gen(function* () {
|
||||
const db = yield* seedSession({ agent: "plan", model: previousModel })
|
||||
const db = yield* seedSession({ agent: Agent.ID.make("plan"), model: previousModel })
|
||||
const bus = yield* Bus.Service
|
||||
|
||||
yield* bus.publish(SessionEvent.AgentSelected, {
|
||||
|
||||
@@ -1843,7 +1843,7 @@ describe("SessionRunnerLLM", () => {
|
||||
)
|
||||
yield* s.db
|
||||
.update(SessionTable)
|
||||
.set({ agent: "reviewer" })
|
||||
.set({ agent: Agent.ID.make("reviewer") })
|
||||
.where(eq(SessionTable.id, sessionID))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
@@ -1863,7 +1863,7 @@ describe("SessionRunnerLLM", () => {
|
||||
scenario("fails before the model request when the selected agent is unavailable", function* (s) {
|
||||
yield* s.db
|
||||
.update(SessionTable)
|
||||
.set({ agent: "explore" })
|
||||
.set({ agent: Agent.ID.make("explore") })
|
||||
.where(eq(SessionTable.id, sessionID))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
|
||||
@@ -8,6 +8,7 @@ import { SessionMessage } from "@opencode/core/session/message"
|
||||
import { SessionSchema } from "@opencode/core/session/schema"
|
||||
import { SessionTable } from "@opencode/core/session/sql"
|
||||
import { Project } from "@opencode/core/project"
|
||||
import { Agent } from "@opencode/schema/agent"
|
||||
import { ProjectTable } from "@opencode/core/project/sql"
|
||||
import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { Global } from "@opencode/util/global"
|
||||
@@ -661,7 +662,7 @@ describe("V1Migration.transformSession", () => {
|
||||
const compact = user("msg_000000000027aaaaaaaaaaaaaa", {}, 3)
|
||||
const summary = assistant("msg_000000000028aaaaaaaaaaaaaa", compact.id, { summary: true }, 4)
|
||||
const existing = session({
|
||||
agent: "existing",
|
||||
agent: Agent.ID.make("existing"),
|
||||
model: { id: "existing-model", providerID: "existing-provider", variant: "existing" },
|
||||
})
|
||||
const result = transform(
|
||||
@@ -680,7 +681,7 @@ describe("V1Migration.transformSession", () => {
|
||||
metadata: { recent: "[User]: question\n\n[Assistant]: answer" },
|
||||
})
|
||||
expect(result.messages[2].data).not.toHaveProperty("recent")
|
||||
expect(result.session.agent).toBe("existing")
|
||||
expect(result.session.agent).toBe(Agent.ID.make("existing"))
|
||||
expect(result.session.model).toEqual(existing.model)
|
||||
})
|
||||
|
||||
@@ -722,7 +723,7 @@ describe("V1Migration.transformSession", () => {
|
||||
},
|
||||
])
|
||||
expect(result.session).toEqual({
|
||||
agent: "review",
|
||||
agent: Agent.ID.make("review"),
|
||||
model: { id: "m2", providerID: "p2", variant: "default" },
|
||||
cost: 7,
|
||||
tokens_input: 8,
|
||||
|
||||
Reference in New Issue
Block a user