diff --git a/packages/client/src/promise/generated/client.ts b/packages/client/src/promise/generated/client.ts index 95a0a90d077..3cea8f8e48b 100644 --- a/packages/client/src/promise/generated/client.ts +++ b/packages/client/src/promise/generated/client.ts @@ -526,7 +526,7 @@ export function make(options: ClientOptions) { path: `/api/session/import`, body: { info: input["info"], messages: input["messages"], location: input["location"] }, successStatus: 200, - declaredStatuses: [409, 401, 400], + declaredStatuses: [409, 404, 401, 400], empty: false, }, requestOptions, diff --git a/packages/core/src/session/transfer.ts b/packages/core/src/session/transfer.ts index 279e8e46d17..6a88e83077a 100644 --- a/packages/core/src/session/transfer.ts +++ b/packages/core/src/session/transfer.ts @@ -34,7 +34,10 @@ export interface Interface { sessionID: Session.ID sanitize?: boolean }) => Effect.Effect - readonly import: (input: { data: Data; location: Location.Ref }) => Effect.Effect + readonly import: (input: { + data: Data + location: Location.Ref + }) => Effect.Effect } export class Service extends Context.Service()("@opencode/SessionTransfer") {} @@ -66,6 +69,7 @@ const layer = Layer.effect( .get() .pipe(Effect.orDie) if (recorded) return yield* new ImportConflictError({ sessionID }) + if (input.data.info.parentID) yield* sessions.get(input.data.info.parentID) const project = yield* projects.resolve(input.location.directory) yield* upsertProject(db, project).pipe(Effect.orDie) const messages = input.data.messages.filter(isSettled).map((message, index) => { @@ -85,6 +89,7 @@ const layer = Layer.effect( SessionEvent.Created, { sessionID, + parentID: input.data.info.parentID, slug: Slug.create(), version: app.version, projectID: project.id, diff --git a/packages/protocol/src/groups/session.ts b/packages/protocol/src/groups/session.ts index 4613b05faa6..37610ba34eb 100644 --- a/packages/protocol/src/groups/session.ts +++ b/packages/protocol/src/groups/session.ts @@ -191,12 +191,13 @@ export const makeSessionGroup = (sessionLo location: Location.Ref.pipe(Schema.optional), }), success: Schema.Struct({ data: Session.Info }), - error: ConflictError, + error: [ConflictError, SessionNotFoundError], }).annotateMerge( OpenApi.annotations({ identifier: "v2.session.import", summary: "Import session", - description: "Import a projected session transcript at the requested location.", + description: + "Import a projected session transcript at the requested location. If parentID is supplied, the parent session must already exist; import parents before children.", }), ), ) diff --git a/packages/server/src/handlers/session.ts b/packages/server/src/handlers/session.ts index 282eeeb4264..3f0c24dab21 100644 --- a/packages/server/src/handlers/session.ts +++ b/packages/server/src/handlers/session.ts @@ -135,6 +135,7 @@ export const SessionHandler = HttpApiBuilder.group(Api, "server.session", (handl location: ctx.payload.location ?? { directory: AbsolutePath.make(process.cwd()) }, }) .pipe( + Effect.catchTag("Session.NotFoundError", missingSession), Effect.catchTag( "SessionTransfer.ImportConflictError", (error) => diff --git a/packages/server/test/session-import.test.ts b/packages/server/test/session-import.test.ts new file mode 100644 index 00000000000..67b12cf4896 --- /dev/null +++ b/packages/server/test/session-import.test.ts @@ -0,0 +1,72 @@ +import { expect } from "bun:test" +import { Session } from "@opencode-ai/schema/session" +import { Effect, Schema } from "effect" +import { it } from "../../core/test/lib/effect" +import { ServerFetch } from "../src/fetch" + +const SessionResponse = Schema.Struct({ data: Schema.toEncoded(Session.Info) }) +const SessionsResponse = Schema.Struct({ data: Schema.Array(Schema.toEncoded(Session.Info)) }) + +const setup = Effect.gen(function* () { + const handler = yield* ServerFetch.make({ + app: { version: "test" }, + database: { path: ":memory:" }, + fs: { filewatcher: false }, + }) + return (path: string, body?: unknown, status = 200) => + Effect.promise(async () => { + const response = await handler( + new Request(`http://opencode.local${path}`, { + method: body === undefined ? "GET" : "POST", + headers: { "content-type": "application/json" }, + body: body === undefined ? undefined : JSON.stringify(body), + }), + ) + const json: unknown = await response.json() + expect(response.status).toBe(status) + return json + }) +}) + +it.live("preserves imported parentID through HTTP import, read, and parent filter", () => + Effect.gen(function* () { + const request = yield* setup + const parent = Schema.decodeUnknownSync(SessionResponse)(yield* request("/api/session", { title: "Parent" })) + const id = Session.ID.create() + const imported = Schema.decodeUnknownSync(SessionResponse)( + yield* request("/api/session/import", { + info: { ...parent.data, id, parentID: parent.data.id, title: "Imported child" }, + messages: [], + }), + ) + const read = Schema.decodeUnknownSync(SessionResponse)(yield* request(`/api/session/${id}`)) + const children = Schema.decodeUnknownSync(SessionsResponse)( + yield* request(`/api/session?parentID=${parent.data.id}`), + ) + expect({ + imported: imported.data.parentID, + read: read.data.parentID, + children: children.data.map((child) => child.id), + }).toEqual({ imported: parent.data.id, read: parent.data.id, children: [id] }) + }).pipe(Effect.scoped), +) +;["missing", "self"].forEach((parent) => { + it.live(`rejects a ${parent} parent without creating the imported session`, () => + Effect.gen(function* () { + const request = yield* setup + const template = Schema.decodeUnknownSync(SessionResponse)(yield* request("/api/session", {})) + const id = Session.ID.create() + const parentID = parent === "self" ? id : Session.ID.create() + const error = yield* request( + "/api/session/import", + { info: { ...template.data, id, parentID }, messages: [] }, + 404, + ) + expect(error).toMatchObject({ _tag: "SessionNotFoundError", sessionID: parentID }) + expect(yield* request(`/api/session/${id}`, undefined, 404)).toMatchObject({ + _tag: "SessionNotFoundError", + sessionID: id, + }) + }).pipe(Effect.scoped), + ) +})