mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
feat(plugin): expose session removal (#52387)
This commit is contained in:
committed by
GitHub
Unverified
parent
91b9bc52a4
commit
a923596ee7
@@ -538,6 +538,7 @@ export const make = Effect.fn("PluginHost.make")(function* (
|
||||
input?.location ?? Location.Ref.make({ directory: location.directory, workspaceID: location.workspaceID }),
|
||||
}),
|
||||
get: (input) => sessions.get(input.sessionID),
|
||||
remove: (input) => sessions.remove(input.sessionID),
|
||||
switchAgent: sessions.switchAgent,
|
||||
switchModel: sessions.switchModel,
|
||||
prompt: sessions.prompt,
|
||||
|
||||
@@ -166,6 +166,7 @@ export function host(overrides: Overrides = {}): Plugin.Context {
|
||||
hook: overrides.session?.hook ?? (() => Effect.die("unused session.hook")),
|
||||
create: overrides.session?.create ?? (() => Effect.die("unused session.create")),
|
||||
get: overrides.session?.get ?? (() => Effect.die("unused session.get")),
|
||||
remove: overrides.session?.remove ?? (() => Effect.die("unused session.remove")),
|
||||
switchAgent: overrides.session?.switchAgent ?? (() => Effect.die("unused session.switchAgent")),
|
||||
switchModel: overrides.session?.switchModel ?? (() => Effect.die("unused session.switchModel")),
|
||||
prompt: overrides.session?.prompt ?? (() => Effect.die("unused session.prompt")),
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { expect } from "bun:test"
|
||||
import { Plugin } from "@opencode/core/plugin"
|
||||
import { PluginHost } from "@opencode/core/plugin/host"
|
||||
import { PluginPromise } from "@opencode/core/plugin/promise"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { Effect } from "effect"
|
||||
import { testEffect } from "../lib/effect"
|
||||
import { PluginTestLayer } from "./fixture"
|
||||
|
||||
const it = testEffect(PluginTestLayer)
|
||||
|
||||
it.live("Effect plugins can remove sessions and their children", () =>
|
||||
Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
const sessions = yield* Session.Service
|
||||
const context = yield* PluginHost.make(plugins)
|
||||
const parent = yield* context.session.create({ title: "Temporary worker" })
|
||||
const child = yield* sessions.create({ parentID: parent.id })
|
||||
const unrelated = yield* context.session.create({ title: "Unrelated session" })
|
||||
|
||||
expect(yield* context.session.remove({ sessionID: parent.id })).toBeUndefined()
|
||||
|
||||
expect(yield* sessions.get(parent.id).pipe(Effect.flip)).toMatchObject({
|
||||
_tag: "Session.NotFoundError",
|
||||
sessionID: parent.id,
|
||||
})
|
||||
expect(yield* sessions.get(child.id).pipe(Effect.flip)).toMatchObject({
|
||||
_tag: "Session.NotFoundError",
|
||||
sessionID: child.id,
|
||||
})
|
||||
expect(yield* context.session.get({ sessionID: unrelated.id })).toEqual(unrelated)
|
||||
expect(yield* context.session.remove({ sessionID: parent.id }).pipe(Effect.flip)).toMatchObject({
|
||||
_tag: "Session.NotFoundError",
|
||||
sessionID: parent.id,
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.live("Promise plugins can remove sessions and their children", () =>
|
||||
Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
const sessions = yield* Session.Service
|
||||
const context = yield* PluginHost.make(plugins)
|
||||
const parent = yield* context.session.create({ title: "Temporary worker" })
|
||||
const child = yield* sessions.create({ parentID: parent.id })
|
||||
const unrelated = yield* context.session.create({ title: "Unrelated session" })
|
||||
|
||||
yield* PluginPromise.fromPromise({
|
||||
id: "test.session-remove",
|
||||
async setup(ctx) {
|
||||
expect(await ctx.session.remove({ sessionID: parent.id })).toBeUndefined()
|
||||
await expect(ctx.session.get({ sessionID: parent.id })).rejects.toThrow()
|
||||
await expect(ctx.session.get({ sessionID: child.id })).rejects.toThrow()
|
||||
expect(await ctx.session.get({ sessionID: unrelated.id })).toMatchObject({ id: unrelated.id })
|
||||
await expect(ctx.session.remove({ sessionID: parent.id })).rejects.toThrow()
|
||||
},
|
||||
}).effect(context)
|
||||
|
||||
expect((yield* sessions.list()).data.map((session) => session.id)).toEqual([unrelated.id])
|
||||
}),
|
||||
)
|
||||
@@ -154,6 +154,7 @@ export type SessionDomain = Pick<
|
||||
SessionApi<unknown>,
|
||||
| "create"
|
||||
| "get"
|
||||
| "remove"
|
||||
| "switchAgent"
|
||||
| "switchModel"
|
||||
| "prompt"
|
||||
|
||||
@@ -581,6 +581,7 @@ export function fromPromise(plugin: Plugin) {
|
||||
),
|
||||
create: adaptApiMethod(SessionEndpoints["session.create"], host.session.create),
|
||||
get: adaptApiMethod(SessionEndpoints["session.get"], host.session.get),
|
||||
remove: adaptApiMethod(SessionEndpoints["session.remove"], host.session.remove),
|
||||
switchAgent: adaptApiMethod(SessionEndpoints["session.switchAgent"], host.session.switchAgent),
|
||||
switchModel: adaptApiMethod(SessionEndpoints["session.switchModel"], host.session.switchModel),
|
||||
prompt: adaptApiMethod(SessionEndpoints["session.prompt"], host.session.prompt),
|
||||
|
||||
@@ -154,6 +154,7 @@ export type SessionDomain = Pick<
|
||||
SessionApi,
|
||||
| "create"
|
||||
| "get"
|
||||
| "remove"
|
||||
| "switchAgent"
|
||||
| "switchModel"
|
||||
| "prompt"
|
||||
|
||||
@@ -821,6 +821,16 @@ effect: (ctx) =>
|
||||
}),
|
||||
```
|
||||
|
||||
Remove a temporary session with `remove`. Removal stops active execution and recursively deletes child sessions.
|
||||
|
||||
```ts
|
||||
effect: (ctx) =>
|
||||
Effect.gen(function* () {
|
||||
const created = yield* ctx.session.create({ title: "Temporary worker" }).pipe(Effect.orDie)
|
||||
yield* ctx.session.remove({ sessionID: created.id }).pipe(Effect.orDie)
|
||||
}),
|
||||
```
|
||||
|
||||
Schemas: [`Session.Info`](/api#schema-Session.Info), [`Model.Ref`](/api#schema-Model.Ref),
|
||||
[`Session.Inbox.User`](/api#schema-Session.Inbox.User),
|
||||
[`Session.Inbox.Synthetic`](/api#schema-Session.Inbox.Synthetic).
|
||||
@@ -830,6 +840,7 @@ type SessionDomain = Pick<
|
||||
SessionApi<unknown>,
|
||||
| "create"
|
||||
| "get"
|
||||
| "remove"
|
||||
| "switchAgent"
|
||||
| "switchModel"
|
||||
| "prompt"
|
||||
|
||||
@@ -774,6 +774,12 @@ await ctx.session.interrupt({ sessionID, continue: false })
|
||||
await ctx.session.wait({ sessionID })
|
||||
```
|
||||
|
||||
Remove a temporary session with `remove`. Removal stops active execution and recursively deletes child sessions.
|
||||
|
||||
```ts
|
||||
await ctx.session.remove({ sessionID })
|
||||
```
|
||||
|
||||
#### Reference
|
||||
|
||||
Schemas: [`Session.Info`](/api#schema-Session.Info), [`Model.Ref`](/api#schema-Model.Ref),
|
||||
@@ -783,6 +789,7 @@ Schemas: [`Session.Info`](/api#schema-Session.Info), [`Model.Ref`](/api#schema-M
|
||||
interface SessionContext {
|
||||
create(input?: SessionCreateInput, requestOptions?: RequestOptions): Promise<SessionInfo>
|
||||
get(input: SessionGetInput, requestOptions?: RequestOptions): Promise<SessionInfo>
|
||||
remove(input: SessionRemoveInput, requestOptions?: RequestOptions): Promise<void>
|
||||
context(input: SessionContextInput, requestOptions?: RequestOptions): Promise<readonly SessionMessageInfo[]>
|
||||
switchAgent(input: SessionSwitchAgentInput, requestOptions?: RequestOptions): Promise<void>
|
||||
switchModel(input: SessionSwitchModelInput, requestOptions?: RequestOptions): Promise<void>
|
||||
|
||||
Reference in New Issue
Block a user