feat(coding-agent): refine session_directory hook closes #1729
This commit is contained in:
@@ -115,6 +115,7 @@ export type {
|
||||
SessionBeforeTreeResult,
|
||||
SessionCompactEvent,
|
||||
SessionDirectoryEvent,
|
||||
SessionDirectoryHandler,
|
||||
SessionDirectoryResult,
|
||||
SessionEvent,
|
||||
SessionForkEvent,
|
||||
|
||||
@@ -851,40 +851,6 @@ export class ExtensionRunner {
|
||||
return { skillPaths, promptPaths, themePaths };
|
||||
}
|
||||
|
||||
/** Emit session_directory event. Returns custom session directory from extensions (last one wins). */
|
||||
async emitSessionDirectory(cwd: string, cliSessionDir: string | undefined): Promise<string | undefined> {
|
||||
const ctx = this.createContext();
|
||||
let customSessionDir: string | undefined;
|
||||
|
||||
for (const ext of this.extensions) {
|
||||
const handlers = ext.handlers.get("session_directory");
|
||||
if (!handlers || handlers.length === 0) continue;
|
||||
|
||||
for (const handler of handlers) {
|
||||
try {
|
||||
const event = { type: "session_directory" as const, cwd, cliSessionDir };
|
||||
const handlerResult = await handler(event, ctx);
|
||||
const result = handlerResult as { sessionDir?: string } | undefined;
|
||||
|
||||
if (result?.sessionDir) {
|
||||
customSessionDir = result.sessionDir;
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const stack = err instanceof Error ? err.stack : undefined;
|
||||
this.emitError({
|
||||
extensionPath: ext.path,
|
||||
event: "session_directory",
|
||||
error: message,
|
||||
stack,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return customSessionDir;
|
||||
}
|
||||
|
||||
/** Emit input event. Transforms chain, "handled" short-circuits. */
|
||||
async emitInput(text: string, images: ImageContent[] | undefined, source: InputSource): Promise<InputEventResult> {
|
||||
const ctx = this.createContext();
|
||||
|
||||
@@ -392,8 +392,6 @@ export interface ResourcesDiscoverResult {
|
||||
export interface SessionDirectoryEvent {
|
||||
type: "session_directory";
|
||||
cwd: string;
|
||||
/** CLI-provided session directory (if any) */
|
||||
cliSessionDir: string | undefined;
|
||||
}
|
||||
|
||||
/** Fired on initial session load */
|
||||
@@ -880,6 +878,11 @@ export interface SessionDirectoryResult {
|
||||
sessionDir?: string;
|
||||
}
|
||||
|
||||
/** Special startup-only handler. Unlike other events, this receives no ExtensionContext. */
|
||||
export type SessionDirectoryHandler = (
|
||||
event: SessionDirectoryEvent,
|
||||
) => Promise<SessionDirectoryResult | undefined> | SessionDirectoryResult | undefined;
|
||||
|
||||
export interface SessionBeforeSwitchResult {
|
||||
cancel?: boolean;
|
||||
}
|
||||
@@ -950,7 +953,7 @@ export interface ExtensionAPI {
|
||||
// =========================================================================
|
||||
|
||||
on(event: "resources_discover", handler: ExtensionHandler<ResourcesDiscoverEvent, ResourcesDiscoverResult>): void;
|
||||
on(event: "session_directory", handler: ExtensionHandler<SessionDirectoryEvent, SessionDirectoryResult>): void;
|
||||
on(event: "session_directory", handler: SessionDirectoryHandler): void;
|
||||
on(event: "session_start", handler: ExtensionHandler<SessionStartEvent>): void;
|
||||
on(
|
||||
event: "session_before_switch",
|
||||
|
||||
Reference in New Issue
Block a user