diff --git a/packages/opencode/src/session/index.ts b/packages/opencode/src/session/index.ts index 3fcdab5238c3..baabab76de2f 100644 --- a/packages/opencode/src/session/index.ts +++ b/packages/opencode/src/session/index.ts @@ -18,6 +18,7 @@ import { SessionPrompt } from "./prompt" import { fn } from "@/util/fn" import { Command } from "../command" import { Snapshot } from "@/snapshot" +import { Plugin } from "@/plugin" import type { Provider } from "@/provider/provider" import { PermissionNext } from "@/permission/next" @@ -136,9 +137,19 @@ export namespace Session { }) .optional(), async (input) => { + const sessionID = Identifier.ascending("session") + + // Allow plugins to customize session creation (e.g., set custom directory) + const creating = await Plugin.trigger( + "session.creating", + { sessionID, parentID: input?.parentID }, + { directory: Instance.directory }, + ) + return createNext({ + id: sessionID, parentID: input?.parentID, - directory: Instance.directory, + directory: creating.directory, title: input?.title, permission: input?.permission, }) diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts index 36a4657d74c5..592c94e7c70f 100644 --- a/packages/plugin/src/index.ts +++ b/packages/plugin/src/index.ts @@ -204,6 +204,16 @@ export interface Hooks { system: string[] }, ) => Promise + /** + * Called before a session is created. Allows plugins to customize + * session creation, including setting a custom working directory. + * + * - `directory`: The working directory for the session (defaults to Instance.directory) + */ + "session.creating"?: ( + input: { sessionID: string; parentID?: string }, + output: { directory: string }, + ) => Promise /** * Called before session compaction starts. Allows plugins to customize * the compaction prompt. diff --git a/packages/web/src/content/docs/plugins.mdx b/packages/web/src/content/docs/plugins.mdx index 66a1b3cad95d..78fce29d511d 100644 --- a/packages/web/src/content/docs/plugins.mdx +++ b/packages/web/src/content/docs/plugins.mdx @@ -179,6 +179,7 @@ Plugins can subscribe to events as seen below in the Examples section. Here is a #### Session Events +- `session.creating` - `session.created` - `session.compacted` - `session.deleted`