* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import type {
HostExitedNotification,
HostStartResult,
HostPublishSessionResult,
HostLocalServerOptions,
HostGitHubEnvironmentOptions,
} from "./generated/rpc.js";
import type { CopilotSession } from "./session.js";
import type { SessionConfig, ResumeSessionConfig } from "./types.js";
export interface AhpSessionCreateRequest {
config: Omit<SessionConfig, "onPermissionRequest">;
signal: AbortSignal;
}
export interface AhpSessionResumeRequest {
sessionId: string;
config: Omit<ResumeSessionConfig, "onPermissionRequest">;
signal: AbortSignal;
}
* A listener-task exit report, or an owner disconnect that cannot acknowledge cleanup.
* `reason: "exited"` means the hosting task failed, not that the runtime process died.
* In-process hosts have no process exit code (`exitCode` is absent).
* @experimental
*/
export type AhpHostExit = HostExitedNotification;
* Options for runtime-supervised AHP hosting. Select at least one transport.
* Callbacks are captured at startup;
* later changes to this options object do not reconfigure an existing host.
* @experimental
*/
export interface AhpHostOptions {
localServer?: HostLocalServerOptions;
githubEnvironment?: HostGitHubEnvironmentOptions;
onExit?: (exit: AhpHostExit) => void;
* Materialize a fresh session on this client. Preserve the supplied config
* (including its sessionId and workingDirectory); add your own prompt and tools.
* The callback and all tool/hook functions stay in the application.
*/
createSession?: (request: AhpSessionCreateRequest) => Promise<CopilotSession>;
* Resume a durable session previously created by an application factory.
* Return the original object from this client's resumeSession, or an existing
* attached original without reconfiguring it. Published resident sessions
* attach directly and never invoke this callback.
*/
resumeSession?: (request: AhpSessionResumeRequest) => Promise<CopilotSession>;
* Called once per handoff with the original returned object after AHP detaches
* or the handoff fails. The SDK never disconnects or destroys this object.
*/
onSessionReleased?: (session: CopilotSession) => void | Promise<void>;
}
* A connection-owned AHP listener supervised by the runtime.
*
* The runtime hosts the listener in-process, without a companion executable. Disconnecting
* the owning client also stops the host; reconnecting does not reclaim it.
* Stopping a host does not delete its underlying sessions.
* Its durable AHP catalog is shared by successive hosts in the runtime's
* effective Copilot home; a concurrent host for that catalog is rejected.
*
* @experimental
*/
export class AhpHost {
readonly hostId: string;
readonly url: string | undefined;
readonly environmentId: string | undefined;
readonly token: string | undefined;
readonly pid: number | undefined;
constructor(
info: HostStartResult,
private readonly disposeHost: () => Promise<void>,
private readonly publishHostSession: (
sessionId: string
) => Promise<HostPublishSessionResult>
) {
this.hostId = info.hostId;
this.url = info.url;
this.environmentId = info.environmentId;
this.token = info.token;
this.pid = info.pid;
}
dispose(): Promise<void> {
return this.disposeHost();
}
publishSession(sessionId: string): Promise<HostPublishSessionResult> {
return this.publishHostSession(sessionId);
}
async [Symbol.asyncDispose](): Promise<void> {
await this.dispose();
}
}