GeezerVison is showing github.com — feat(plugin): support parent session creation (#52359) · Ichinose-Kazuki/opencode@c174a3f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/client/src/effect/api/api.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -198,6 +198,7 @@ export type SessionStatsOperation<E = never> = (input?: SessionStatsInput) => Ef | 198 | 198 |
| 199 | 199 | export type SessionCreateInput = { | 200 | 200 | readonly id?: Session.ID | undefined | | 201 | + readonly parentID?: Session.ID | undefined | 201 | 202 | readonly title?: string | undefined | 202 | 203 | readonly agent?: Agent.ID | undefined | 203 | 204 | readonly model?: Model.Ref | undefined | |
Collapse file Copy file name to clipboardExpand all lines: packages/client/src/effect/generated/client.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -393,6 +393,7 @@ const EndpointSessionCreate = (raw: RawClient["server.session"]) => (input?: Ses | 393 | 393 | raw["session.create"]({ | 394 | 394 | payload: { | 395 | 395 | id: input?.["id"], | | 396 | + parentID: input?.["parentID"], | 396 | 397 | title: input?.["title"], | 397 | 398 | agent: input?.["agent"], | 398 | 399 | model: input?.["model"], | |
Collapse file Copy file name to clipboardExpand all lines: packages/client/src/promise/generated/client.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -577,6 +577,7 @@ export function make(options: ClientOptions) { | 577 | 577 | path: `/api/session`, | 578 | 578 | body: { | 579 | 579 | id: input?.["id"], | | 580 | + parentID: input?.["parentID"], | 580 | 581 | title: input?.["title"], | 581 | 582 | agent: input?.["agent"], | 582 | 583 | model: input?.["model"], | @@ -585,7 +586,7 @@ export function make(options: ClientOptions) { | 585 | 586 | permissions: input?.["permissions"], | 586 | 587 | }, | 587 | 588 | successStatus: 200, | 588 | | - declaredStatuses: [400, 401], | | 589 | + declaredStatuses: [400, 401, 404], | 589 | 590 | empty: false, | 590 | 591 | }, | 591 | 592 | requestOptions, | |
Collapse file Copy file name to clipboardExpand all lines: packages/client/src/promise/generated/types.ts +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2914,6 +2914,7 @@ export type SessionStatsOutput = { data: SessionStatsInfo }["data"] | 2914 | 2914 | export type SessionCreateInput = { | 2915 | 2915 | readonly id?: { | 2916 | 2916 | readonly id?: string | null | | 2917 | + readonly parentID?: string | null | 2917 | 2918 | readonly title?: string | null | 2918 | 2919 | readonly agent?: string | null | 2919 | 2920 | readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | @@ -2925,8 +2926,23 @@ export type SessionCreateInput = { | 2925 | 2926 | readonly effect: "allow" | "deny" | "ask" | 2926 | 2927 | }> | null | 2927 | 2928 | }["id"] | | 2929 | + readonly parentID?: { | | 2930 | + readonly id?: string | null | | 2931 | + readonly parentID?: string | null | | 2932 | + readonly title?: string | null | | 2933 | + readonly agent?: string | null | | 2934 | + readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | | 2935 | + readonly location?: { readonly directory: string } | null | | 2936 | + readonly metadata?: { readonly [x: string]: JsonValue } | null | | 2937 | + readonly permissions?: ReadonlyArray<{ | | 2938 | + readonly action: string | | 2939 | + readonly resource: string | | 2940 | + readonly effect: "allow" | "deny" | "ask" | | 2941 | + }> | null | | 2942 | + }["parentID"] | 2928 | 2943 | readonly title?: { | 2929 | 2944 | readonly id?: string | null | | 2945 | + readonly parentID?: string | null | 2930 | 2946 | readonly title?: string | null | 2931 | 2947 | readonly agent?: string | null | 2932 | 2948 | readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | @@ -2940,6 +2956,7 @@ export type SessionCreateInput = { | 2940 | 2956 | }["title"] | 2941 | 2957 | readonly agent?: { | 2942 | 2958 | readonly id?: string | null | | 2959 | + readonly parentID?: string | null | 2943 | 2960 | readonly title?: string | null | 2944 | 2961 | readonly agent?: string | null | 2945 | 2962 | readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | @@ -2953,6 +2970,7 @@ export type SessionCreateInput = { | 2953 | 2970 | }["agent"] | 2954 | 2971 | readonly model?: { | 2955 | 2972 | readonly id?: string | null | | 2973 | + readonly parentID?: string | null | 2956 | 2974 | readonly title?: string | null | 2957 | 2975 | readonly agent?: string | null | 2958 | 2976 | readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | @@ -2966,6 +2984,7 @@ export type SessionCreateInput = { | 2966 | 2984 | }["model"] | 2967 | 2985 | readonly location?: { | 2968 | 2986 | readonly id?: string | null | | 2987 | + readonly parentID?: string | null | 2969 | 2988 | readonly title?: string | null | 2970 | 2989 | readonly agent?: string | null | 2971 | 2990 | readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | @@ -2979,6 +2998,7 @@ export type SessionCreateInput = { | 2979 | 2998 | }["location"] | 2980 | 2999 | readonly metadata?: { | 2981 | 3000 | readonly id?: string | null | | 3001 | + readonly parentID?: string | null | 2982 | 3002 | readonly title?: string | null | 2983 | 3003 | readonly agent?: string | null | 2984 | 3004 | readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | @@ -2992,6 +3012,7 @@ export type SessionCreateInput = { | 2992 | 3012 | }["metadata"] | 2993 | 3013 | readonly permissions?: { | 2994 | 3014 | readonly id?: string | null | | 3015 | + readonly parentID?: string | null | 2995 | 3016 | readonly title?: string | null | 2996 | 3017 | readonly agent?: string | null | 2997 | 3018 | readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | |
Collapse file Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { expect, test } from "bun:test" | | 2 | +import { OpenCode } from "../src/promise/index" | | 3 | + | | 4 | +test("session.create forwards parentID from the Promise client", async () => { | | 5 | + let body: unknown | | 6 | + const client = OpenCode.make({ | | 7 | + baseUrl: "http://localhost:3000", | | 8 | + fetch: async (_input, init) => { | | 9 | + body = JSON.parse(String(init?.body)) | | 10 | + return Response.json({ data: { id: "ses_child" } }) | | 11 | + }, | | 12 | + }) | | 13 | + | | 14 | + const child = await client.session.create({ parentID: "ses_parent", title: "Child" }) | | 15 | + | | 16 | + expect(child.id).toBe("ses_child") | | 17 | + expect(body).toEqual({ parentID: "ses_parent", title: "Child" }) | | 18 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/host.ts +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -534,8 +534,13 @@ export const make = Effect.fn("PluginHost.make")(function* ( | 534 | 534 | model: input?.model, | 535 | 535 | metadata: input?.metadata, | 536 | 536 | permissions: input?.permissions, | 537 | | - location: | 538 | | - input?.location ?? Location.Ref.make({ directory: location.directory, workspaceID: location.workspaceID }), | | 537 | + ...(input?.parentID === undefined | | 538 | + ? { | | 539 | + location: | | 540 | + input?.location ?? | | 541 | + Location.Ref.make({ directory: location.directory, workspaceID: location.workspaceID }), | | 542 | + } | | 543 | + : { parentID: input.parentID }), | 539 | 544 | }), | 540 | 545 | get: (input) => sessions.get(input.sessionID), | 541 | 546 | remove: (input) => sessions.remove(input.sessionID), | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/plugin.test.ts +31-2Lines changed: 31 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,9 +8,10 @@ import { Credential } from "@opencode/core/credential" | 8 | 8 | import { Integration } from "@opencode/core/integration" | 9 | 9 | import { Plugin } from "@opencode/core/plugin" | 10 | 10 | import { PluginModule } from "@opencode/core/plugin/module" | | 11 | +import { Session } from "@opencode/core/session" | | 12 | +import { SessionSchema } from "@opencode/core/session/schema" | 11 | 13 | import { Watcher } from "@opencode/core/filesystem/watcher" | 12 | 14 | import { fromPromise } from "@opencode/plugin/promise/adapter" | 13 | | -import { Session } from "@opencode/schema/session" | 14 | 15 | import { testEffect } from "./lib/effect" | 15 | 16 | import { PluginTestLayer } from "./plugin/fixture" | 16 | 17 |
| @@ -471,6 +472,34 @@ it.effect("normalizes Promise plugin API inputs through JSON", () => | 471 | 472 | }), | 472 | 473 | ) | 473 | 474 |
| | 475 | +it.effect("creates child sessions from a plugin at the parent's location", () => | | 476 | + Effect.gen(function* () { | | 477 | + const plugins = yield* Plugin.Service | | 478 | + const sessions = yield* Session.Service | | 479 | + const created = yield* Deferred.make<{ parentID: SessionSchema.ID; childID: SessionSchema.ID }>() | | 480 | + yield* plugins.activate([ | | 481 | + { | | 482 | + id: "child-session", | | 483 | + revision: "1", | | 484 | + effect: (ctx) => | | 485 | + Effect.gen(function* () { | | 486 | + const parent = yield* ctx.session | | 487 | + .create({ title: "Parent", metadata: { source: "parent" } }) | | 488 | + .pipe(Effect.orDie) | | 489 | + const child = yield* ctx.session.create({ parentID: parent.id, title: "Child" }).pipe(Effect.orDie) | | 490 | + yield* Deferred.succeed(created, { parentID: parent.id, childID: child.id }) | | 491 | + }), | | 492 | + }, | | 493 | + ]) | | 494 | + yield* plugins.awaitActivation | | 495 | + const ids = yield* Deferred.await(created) | | 496 | + const parent = yield* sessions.get(ids.parentID) | | 497 | + const child = yield* sessions.get(ids.childID) | | 498 | + | | 499 | + expect(child).toMatchObject({ parentID: parent.id, location: parent.location, metadata: parent.metadata }) | | 500 | + }), | | 501 | +) | | 502 | + | 474 | 503 | it.effect("reloading a plugin replaces its command implementation", () => | 475 | 504 | Effect.gen(function* () { | 476 | 505 | const plugins = yield* Plugin.Service | @@ -497,7 +526,7 @@ it.effect("reloading a plugin replaces its command implementation", () => | 497 | 526 | ]) | 498 | 527 | const request = { | 499 | 528 | name: "greet", | 500 | | - invocation: { sessionID: Session.ID.make("ses_plugin"), prompt: { text: "" }, delivery: "steer" as const }, | | 529 | + invocation: { sessionID: SessionSchema.ID.make("ses_plugin"), prompt: { text: "" }, delivery: "steer" as const }, | 501 | 530 | } | 502 | 531 |
| 503 | 532 | yield* load("1", "before") | |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/session.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -220,6 +220,7 @@ export const makeSessionGroup = < | 220 | 220 | HttpApiEndpoint.post("session.create", "/api/session", { | 221 | 221 | payload: Schema.Struct({ | 222 | 222 | id: Session.ID.pipe(Schema.optional), | | 223 | + parentID: Session.ID.pipe(Schema.optional), | 223 | 224 | title: Schema.String.pipe(Schema.optional), | 224 | 225 | agent: Agent.ID.pipe(Schema.optional), | 225 | 226 | model: Model.Ref.pipe(Schema.optional), | @@ -228,11 +229,13 @@ export const makeSessionGroup = < | 228 | 229 | permissions: Permission.Ruleset.pipe(Schema.optional), | 229 | 230 | }), | 230 | 231 | success: Schema.Struct({ data: PublicSessionInfo }), | | 232 | + error: SessionNotFoundError, | 231 | 233 | }).annotateMerge( | 232 | 234 | OpenApi.annotations({ | 233 | 235 | identifier: "session.create", | 234 | 236 | summary: "Create session", | 235 | | - description: "Create a session at the requested location.", | | 237 | + description: | | 238 | + "Create a session at the requested location. A parentID creates a linked child session at its parent's location.", | 236 | 239 | }), | 237 | 240 | ), | 238 | 241 | ) | |
Collapse file Copy file name to clipboardExpand all lines: packages/server/src/handlers/session.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,9 +133,11 @@ export const SessionHandler = HttpApiBuilder.group(Api, "server.session", (handl | 133 | 133 | model: ctx.payload.model, | 134 | 134 | metadata: ctx.payload.metadata, | 135 | 135 | permissions: ctx.payload.permissions, | 136 | | - location: ctx.payload.location ?? { directory: AbsolutePath.make(process.cwd()) }, | | 136 | + ...(ctx.payload.parentID === undefined | | 137 | + ? { location: ctx.payload.location ?? { directory: AbsolutePath.make(process.cwd()) } } | | 138 | + : { parentID: ctx.payload.parentID }), | 137 | 139 | }) | 138 | | - .pipe(Effect.orDie), | | 140 | + .pipe(Effect.catchTag("Session.NotFoundError", missingSession)), | 139 | 141 | } | 140 | 142 | }), | 141 | 143 | ) | |
Collapse file Copy file name to clipboard +65Lines changed: 65 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { expect } from "bun:test" | | 2 | +import { Session } from "@opencode/schema/session" | | 3 | +import { Effect, Schema } from "effect" | | 4 | +import { it } from "../../core/test/lib/effect" | | 5 | +import { ServerFetch } from "../src/fetch" | | 6 | + | | 7 | +const SessionResponse = Schema.Struct({ data: Schema.toEncoded(Session.Info) }) | | 8 | +const SessionsResponse = Schema.Struct({ data: Schema.Array(Schema.toEncoded(Session.Info)) }) | | 9 | + | | 10 | +it.live("creates a child at its parent's location and lists it under the parent", () => | | 11 | + Effect.gen(function* () { | | 12 | + const handler = yield* ServerFetch.make({ | | 13 | + app: { version: "test" }, | | 14 | + database: { path: ":memory:" }, | | 15 | + fs: { filewatcher: false }, | | 16 | + }) | | 17 | + const create = (body: unknown) => | | 18 | + Effect.promise(async () => { | | 19 | + const response = await handler( | | 20 | + new Request("http://opencode.local/api/session", { | | 21 | + method: "POST", | | 22 | + headers: { "content-type": "application/json" }, | | 23 | + body: JSON.stringify(body), | | 24 | + }), | | 25 | + ) | | 26 | + expect(response.status).toBe(200) | | 27 | + return Schema.decodeUnknownSync(SessionResponse)(await response.json()).data | | 28 | + }) | | 29 | + const parent = yield* create({ title: "Parent" }) | | 30 | + const child = yield* create({ parentID: parent.id, title: "Child", location: { directory: "/unused" } }) | | 31 | + const response = yield* Effect.promise(() => | | 32 | + handler(new Request(`http://opencode.local/api/session?parentID=${parent.id}`)), | | 33 | + ) | | 34 | + expect(response.status).toBe(200) | | 35 | + const children = Schema.decodeUnknownSync(SessionsResponse)(yield* Effect.promise(() => response.json())) | | 36 | + | | 37 | + expect(child).toMatchObject({ parentID: parent.id, location: parent.location }) | | 38 | + expect(children.data.map((session) => session.id)).toEqual([child.id]) | | 39 | + }).pipe(Effect.scoped), | | 40 | +) | | 41 | + | | 42 | +it.live("returns not found when creating a child of a missing session", () => | | 43 | + Effect.gen(function* () { | | 44 | + const handler = yield* ServerFetch.make({ | | 45 | + app: { version: "test" }, | | 46 | + database: { path: ":memory:" }, | | 47 | + fs: { filewatcher: false }, | | 48 | + }) | | 49 | + const parentID = Session.ID.create() | | 50 | + const response = yield* Effect.promise(() => | | 51 | + handler( | | 52 | + new Request("http://opencode.local/api/session", { | | 53 | + method: "POST", | | 54 | + headers: { "content-type": "application/json" }, | | 55 | + body: JSON.stringify({ parentID, title: "Child" }), | | 56 | + }), | | 57 | + ), | | 58 | + ) | | 59 | + expect(response.status).toBe(404) | | 60 | + expect(yield* Effect.promise(() => response.json())).toMatchObject({ | | 61 | + _tag: "SessionNotFoundError", | | 62 | + sessionID: parentID, | | 63 | + }) | | 64 | + }).pipe(Effect.scoped), | | 65 | +) |
|