Add extension mode context
This commit is contained in:
@@ -56,6 +56,7 @@ import {
|
||||
type ContextUsage,
|
||||
type ExtensionCommandContextActions,
|
||||
type ExtensionErrorListener,
|
||||
type ExtensionMode,
|
||||
ExtensionRunner,
|
||||
type ExtensionUIContext,
|
||||
type InputSource,
|
||||
@@ -187,6 +188,7 @@ export interface AgentSessionConfig {
|
||||
|
||||
export interface ExtensionBindings {
|
||||
uiContext?: ExtensionUIContext;
|
||||
mode?: ExtensionMode;
|
||||
commandContextActions?: ExtensionCommandContextActions;
|
||||
abortHandler?: () => void;
|
||||
shutdownHandler?: ShutdownHandler;
|
||||
@@ -300,6 +302,7 @@ export class AgentSession {
|
||||
private _baseToolsOverride?: Record<string, AgentTool>;
|
||||
private _sessionStartEvent: SessionStartEvent;
|
||||
private _extensionUIContext?: ExtensionUIContext;
|
||||
private _extensionMode: ExtensionMode = "print";
|
||||
private _extensionCommandContextActions?: ExtensionCommandContextActions;
|
||||
private _extensionAbortHandler?: () => void;
|
||||
private _extensionShutdownHandler?: ShutdownHandler;
|
||||
@@ -2064,6 +2067,9 @@ export class AgentSession {
|
||||
if (bindings.uiContext !== undefined) {
|
||||
this._extensionUIContext = bindings.uiContext;
|
||||
}
|
||||
if (bindings.mode !== undefined) {
|
||||
this._extensionMode = bindings.mode;
|
||||
}
|
||||
if (bindings.commandContextActions !== undefined) {
|
||||
this._extensionCommandContextActions = bindings.commandContextActions;
|
||||
}
|
||||
@@ -2136,7 +2142,7 @@ export class AgentSession {
|
||||
}
|
||||
|
||||
private _applyExtensionBindings(runner: ExtensionRunner): void {
|
||||
runner.setUIContext(this._extensionUIContext);
|
||||
runner.setUIContext(this._extensionUIContext, this._extensionMode);
|
||||
runner.bindCommandContext(this._extensionCommandContextActions);
|
||||
|
||||
this._extensionErrorUnsubscriber?.();
|
||||
|
||||
@@ -66,6 +66,7 @@ export type {
|
||||
ExtensionFactory,
|
||||
ExtensionFlag,
|
||||
ExtensionHandler,
|
||||
ExtensionMode,
|
||||
// Runtime
|
||||
ExtensionRuntime,
|
||||
ExtensionShortcut,
|
||||
|
||||
@@ -28,6 +28,7 @@ import type {
|
||||
ExtensionError,
|
||||
ExtensionEvent,
|
||||
ExtensionFlag,
|
||||
ExtensionMode,
|
||||
ExtensionRuntime,
|
||||
ExtensionShortcut,
|
||||
ExtensionUIContext,
|
||||
@@ -225,6 +226,7 @@ export class ExtensionRunner {
|
||||
private extensions: Extension[];
|
||||
private runtime: ExtensionRuntime;
|
||||
private uiContext: ExtensionUIContext;
|
||||
private mode: ExtensionMode = "print";
|
||||
private cwd: string;
|
||||
private sessionManager: SessionManager;
|
||||
private modelRegistry: ModelRegistry;
|
||||
@@ -354,8 +356,9 @@ export class ExtensionRunner {
|
||||
this.reloadHandler = async () => {};
|
||||
}
|
||||
|
||||
setUIContext(uiContext?: ExtensionUIContext): void {
|
||||
setUIContext(uiContext?: ExtensionUIContext, mode: ExtensionMode = "print"): void {
|
||||
this.uiContext = uiContext ?? noOpUIContext;
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
getUIContext(): ExtensionUIContext {
|
||||
@@ -578,6 +581,10 @@ export class ExtensionRunner {
|
||||
runner.assertActive();
|
||||
return runner.uiContext;
|
||||
},
|
||||
get mode() {
|
||||
runner.assertActive();
|
||||
return runner.mode;
|
||||
},
|
||||
get hasUI() {
|
||||
runner.assertActive();
|
||||
return runner.hasUI();
|
||||
|
||||
@@ -295,10 +295,14 @@ export interface CompactOptions {
|
||||
/**
|
||||
* Context passed to extension event handlers.
|
||||
*/
|
||||
export type ExtensionMode = "tui" | "rpc" | "json" | "print";
|
||||
|
||||
export interface ExtensionContext {
|
||||
/** UI methods for user interaction */
|
||||
ui: ExtensionUIContext;
|
||||
/** Whether UI is available (false in print/RPC mode) */
|
||||
/** Current run mode. Use "tui" to guard terminal-only UI such as custom components. */
|
||||
mode: ExtensionMode;
|
||||
/** Whether dialog-capable UI is available (true in TUI and RPC modes) */
|
||||
hasUI: boolean;
|
||||
/** Current working directory */
|
||||
cwd: string;
|
||||
|
||||
@@ -1508,6 +1508,7 @@ export class InteractiveMode {
|
||||
const uiContext = this.createExtensionUIContext();
|
||||
await this.session.bindExtensions({
|
||||
uiContext,
|
||||
mode: "tui",
|
||||
abortHandler: () => {
|
||||
this.restoreQueuedMessagesToEditor({ abort: true });
|
||||
},
|
||||
@@ -1654,6 +1655,7 @@ export class InteractiveMode {
|
||||
// Create a context for shortcut handlers
|
||||
const createContext = (): ExtensionContext => ({
|
||||
ui: this.createExtensionUIContext(),
|
||||
mode: "tui",
|
||||
hasUI: true,
|
||||
cwd: this.sessionManager.getCwd(),
|
||||
sessionManager: this.sessionManager,
|
||||
|
||||
@@ -71,6 +71,7 @@ export async function runPrintMode(runtimeHost: AgentSessionRuntime, options: Pr
|
||||
const rebindSession = async (): Promise<void> => {
|
||||
session = runtimeHost.session;
|
||||
await session.bindExtensions({
|
||||
mode: mode === "json" ? "json" : "print",
|
||||
commandContextActions: {
|
||||
waitForIdle: () => session.agent.waitForIdle(),
|
||||
newSession: async (newSessionOptions) => runtimeHost.newSession(newSessionOptions),
|
||||
|
||||
@@ -317,6 +317,7 @@ export async function runRpcMode(runtimeHost: AgentSessionRuntime): Promise<neve
|
||||
session = runtimeHost.session;
|
||||
await session.bindExtensions({
|
||||
uiContext: createExtensionUIContext(),
|
||||
mode: "rpc",
|
||||
commandContextActions: {
|
||||
waitForIdle: () => session.agent.waitForIdle(),
|
||||
newSession: async (options) => runtimeHost.newSession(options),
|
||||
|
||||
Reference in New Issue
Block a user