GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(plugin): support parent session creation (#52359) · Ichinose-Kazuki/opencode@c174a3f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • client
      • src
        • effect
          • api
            • api.ts
          • generated
            • client.ts
        • promise/generated
          • client.ts
          • types.ts
      • test
        • session-parent-create.test.ts
    • core
      • src/plugin
        • host.ts
      • test
        • plugin.test.ts
    • protocol/src/groups
      • session.ts
    • server
      • src/handlers
        • session.ts
      • test
        • session-create.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/client/src/effect/api/api.ts‎

Copy file name to clipboardExpand all lines: packages/client/src/effect/api/api.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export type SessionStatsOperation<E = never> = (input?: SessionStatsInput) => Ef
198198

199199
export type SessionCreateInput = {
200200
readonly id?: Session.ID | undefined
201+
readonly parentID?: Session.ID | undefined
201202
readonly title?: string | undefined
202203
readonly agent?: Agent.ID | undefined
203204
readonly model?: Model.Ref | undefined
Collapse file

‎packages/client/src/effect/generated/client.ts‎

Copy file name to clipboardExpand all lines: packages/client/src/effect/generated/client.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ const EndpointSessionCreate = (raw: RawClient["server.session"]) => (input?: Ses
393393
raw["session.create"]({
394394
payload: {
395395
id: input?.["id"],
396+
parentID: input?.["parentID"],
396397
title: input?.["title"],
397398
agent: input?.["agent"],
398399
model: input?.["model"],
Collapse file

‎packages/client/src/promise/generated/client.ts‎

Copy file name to clipboardExpand all lines: packages/client/src/promise/generated/client.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ export function make(options: ClientOptions) {
577577
path: `/api/session`,
578578
body: {
579579
id: input?.["id"],
580+
parentID: input?.["parentID"],
580581
title: input?.["title"],
581582
agent: input?.["agent"],
582583
model: input?.["model"],
@@ -585,7 +586,7 @@ export function make(options: ClientOptions) {
585586
permissions: input?.["permissions"],
586587
},
587588
successStatus: 200,
588-
declaredStatuses: [400, 401],
589+
declaredStatuses: [400, 401, 404],
589590
empty: false,
590591
},
591592
requestOptions,
Collapse file

‎packages/client/src/promise/generated/types.ts‎

Copy file name to clipboardExpand all lines: packages/client/src/promise/generated/types.ts
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,7 @@ export type SessionStatsOutput = { data: SessionStatsInfo }["data"]
29142914
export type SessionCreateInput = {
29152915
readonly id?: {
29162916
readonly id?: string | null
2917+
readonly parentID?: string | null
29172918
readonly title?: string | null
29182919
readonly agent?: string | null
29192920
readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null
@@ -2925,8 +2926,23 @@ export type SessionCreateInput = {
29252926
readonly effect: "allow" | "deny" | "ask"
29262927
}> | null
29272928
}["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"]
29282943
readonly title?: {
29292944
readonly id?: string | null
2945+
readonly parentID?: string | null
29302946
readonly title?: string | null
29312947
readonly agent?: string | null
29322948
readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null
@@ -2940,6 +2956,7 @@ export type SessionCreateInput = {
29402956
}["title"]
29412957
readonly agent?: {
29422958
readonly id?: string | null
2959+
readonly parentID?: string | null
29432960
readonly title?: string | null
29442961
readonly agent?: string | null
29452962
readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null
@@ -2953,6 +2970,7 @@ export type SessionCreateInput = {
29532970
}["agent"]
29542971
readonly model?: {
29552972
readonly id?: string | null
2973+
readonly parentID?: string | null
29562974
readonly title?: string | null
29572975
readonly agent?: string | null
29582976
readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null
@@ -2966,6 +2984,7 @@ export type SessionCreateInput = {
29662984
}["model"]
29672985
readonly location?: {
29682986
readonly id?: string | null
2987+
readonly parentID?: string | null
29692988
readonly title?: string | null
29702989
readonly agent?: string | null
29712990
readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null
@@ -2979,6 +2998,7 @@ export type SessionCreateInput = {
29792998
}["location"]
29802999
readonly metadata?: {
29813000
readonly id?: string | null
3001+
readonly parentID?: string | null
29823002
readonly title?: string | null
29833003
readonly agent?: string | null
29843004
readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null
@@ -2992,6 +3012,7 @@ export type SessionCreateInput = {
29923012
}["metadata"]
29933013
readonly permissions?: {
29943014
readonly id?: string | null
3015+
readonly parentID?: string | null
29953016
readonly title?: string | null
29963017
readonly agent?: string | null
29973018
readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null
Collapse file

‎packages/client/test/session-parent-create.test.ts‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

‎packages/core/src/plugin/host.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/plugin/host.ts
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,13 @@ export const make = Effect.fn("PluginHost.make")(function* (
534534
model: input?.model,
535535
metadata: input?.metadata,
536536
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 }),
539544
}),
540545
get: (input) => sessions.get(input.sessionID),
541546
remove: (input) => sessions.remove(input.sessionID),
Collapse file

‎packages/core/test/plugin.test.ts‎

Copy file name to clipboardExpand all lines: packages/core/test/plugin.test.ts
+31-2Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { Credential } from "@opencode/core/credential"
88
import { Integration } from "@opencode/core/integration"
99
import { Plugin } from "@opencode/core/plugin"
1010
import { PluginModule } from "@opencode/core/plugin/module"
11+
import { Session } from "@opencode/core/session"
12+
import { SessionSchema } from "@opencode/core/session/schema"
1113
import { Watcher } from "@opencode/core/filesystem/watcher"
1214
import { fromPromise } from "@opencode/plugin/promise/adapter"
13-
import { Session } from "@opencode/schema/session"
1415
import { testEffect } from "./lib/effect"
1516
import { PluginTestLayer } from "./plugin/fixture"
1617

@@ -471,6 +472,34 @@ it.effect("normalizes Promise plugin API inputs through JSON", () =>
471472
}),
472473
)
473474

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+
474503
it.effect("reloading a plugin replaces its command implementation", () =>
475504
Effect.gen(function* () {
476505
const plugins = yield* Plugin.Service
@@ -497,7 +526,7 @@ it.effect("reloading a plugin replaces its command implementation", () =>
497526
])
498527
const request = {
499528
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 },
501530
}
502531

503532
yield* load("1", "before")
Collapse file

‎packages/protocol/src/groups/session.ts‎

Copy file name to clipboardExpand all lines: packages/protocol/src/groups/session.ts
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export const makeSessionGroup = <
220220
HttpApiEndpoint.post("session.create", "/api/session", {
221221
payload: Schema.Struct({
222222
id: Session.ID.pipe(Schema.optional),
223+
parentID: Session.ID.pipe(Schema.optional),
223224
title: Schema.String.pipe(Schema.optional),
224225
agent: Agent.ID.pipe(Schema.optional),
225226
model: Model.Ref.pipe(Schema.optional),
@@ -228,11 +229,13 @@ export const makeSessionGroup = <
228229
permissions: Permission.Ruleset.pipe(Schema.optional),
229230
}),
230231
success: Schema.Struct({ data: PublicSessionInfo }),
232+
error: SessionNotFoundError,
231233
}).annotateMerge(
232234
OpenApi.annotations({
233235
identifier: "session.create",
234236
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.",
236239
}),
237240
),
238241
)
Collapse file

‎packages/server/src/handlers/session.ts‎

Copy file name to clipboardExpand all lines: packages/server/src/handlers/session.ts
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ export const SessionHandler = HttpApiBuilder.group(Api, "server.session", (handl
133133
model: ctx.payload.model,
134134
metadata: ctx.payload.metadata,
135135
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 }),
137139
})
138-
.pipe(Effect.orDie),
140+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession)),
139141
}
140142
}),
141143
)
Collapse file

‎packages/server/test/session-create.test.ts‎

Copy file name to clipboard
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.