mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-03-19 10:43:08 +00:00
Fix: Restore views properly, model selection for commit and pr and speed up some cli models with session resume (#801)
* Changes from fix/restoring-view * feat: Add resume query safety checks and optimize store selectors * feat: Improve session management and model normalization * refactor: Extract prompt building logic and handle file path parsing for renames
This commit is contained in:
@@ -30,6 +30,7 @@ import {
|
||||
type CopilotRuntimeModel,
|
||||
} from '@automaker/types';
|
||||
import { createLogger, isAbortError } from '@automaker/utils';
|
||||
import { resolveModelString } from '@automaker/model-resolver';
|
||||
import { CopilotClient, type PermissionRequest } from '@github/copilot-sdk';
|
||||
import {
|
||||
normalizeTodos,
|
||||
@@ -116,6 +117,12 @@ export interface CopilotError extends Error {
|
||||
suggestion?: string;
|
||||
}
|
||||
|
||||
type CopilotSession = Awaited<ReturnType<CopilotClient['createSession']>>;
|
||||
type CopilotSessionOptions = Parameters<CopilotClient['createSession']>[0];
|
||||
type ResumableCopilotClient = CopilotClient & {
|
||||
resumeSession?: (sessionId: string, options: CopilotSessionOptions) => Promise<CopilotSession>;
|
||||
};
|
||||
|
||||
// =============================================================================
|
||||
// Tool Name Normalization
|
||||
// =============================================================================
|
||||
@@ -516,7 +523,11 @@ export class CopilotProvider extends CliProvider {
|
||||
}
|
||||
|
||||
const promptText = this.extractPromptText(options);
|
||||
const bareModel = options.model || DEFAULT_BARE_MODEL;
|
||||
// resolveModelString may return dash-separated canonical names (e.g. "claude-sonnet-4-6"),
|
||||
// but the Copilot SDK expects dot-separated version suffixes (e.g. "claude-sonnet-4.6").
|
||||
// Normalize by converting the last dash-separated numeric pair to dot notation.
|
||||
const resolvedModel = resolveModelString(options.model || DEFAULT_BARE_MODEL);
|
||||
const bareModel = resolvedModel.replace(/-(\d+)-(\d+)$/, '-$1.$2');
|
||||
const workingDirectory = options.cwd || process.cwd();
|
||||
|
||||
logger.debug(
|
||||
@@ -554,12 +565,14 @@ export class CopilotProvider extends CliProvider {
|
||||
});
|
||||
};
|
||||
|
||||
// Declare session outside try so it's accessible in the catch block for cleanup.
|
||||
let session: CopilotSession | undefined;
|
||||
|
||||
try {
|
||||
await client.start();
|
||||
logger.debug(`CopilotClient started with cwd: ${workingDirectory}`);
|
||||
|
||||
// Create session with streaming enabled for real-time events
|
||||
const session = await client.createSession({
|
||||
const sessionOptions: CopilotSessionOptions = {
|
||||
model: bareModel,
|
||||
streaming: true,
|
||||
// AUTONOMOUS MODE: Auto-approve all permission requests.
|
||||
@@ -572,13 +585,33 @@ export class CopilotProvider extends CliProvider {
|
||||
logger.debug(`Permission request: ${request.kind}`);
|
||||
return { kind: 'approved' };
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const sessionId = session.sessionId;
|
||||
logger.debug(`Session created: ${sessionId}`);
|
||||
// Resume the previous Copilot session when possible; otherwise create a fresh one.
|
||||
const resumableClient = client as ResumableCopilotClient;
|
||||
let sessionResumed = false;
|
||||
if (options.sdkSessionId && typeof resumableClient.resumeSession === 'function') {
|
||||
try {
|
||||
session = await resumableClient.resumeSession(options.sdkSessionId, sessionOptions);
|
||||
sessionResumed = true;
|
||||
logger.debug(`Resumed Copilot session: ${session.sessionId}`);
|
||||
} catch (resumeError) {
|
||||
logger.warn(
|
||||
`Failed to resume Copilot session "${options.sdkSessionId}", creating a new session: ${resumeError}`
|
||||
);
|
||||
session = await client.createSession(sessionOptions);
|
||||
}
|
||||
} else {
|
||||
session = await client.createSession(sessionOptions);
|
||||
}
|
||||
|
||||
// session is always assigned by this point (both branches above assign it)
|
||||
const activeSession = session!;
|
||||
const sessionId = activeSession.sessionId;
|
||||
logger.debug(`Session ${sessionResumed ? 'resumed' : 'created'}: ${sessionId}`);
|
||||
|
||||
// Set up event handler to push events to queue
|
||||
session.on((event: SdkEvent) => {
|
||||
activeSession.on((event: SdkEvent) => {
|
||||
logger.debug(`SDK event: ${event.type}`);
|
||||
|
||||
if (event.type === 'session.idle') {
|
||||
@@ -596,7 +629,7 @@ export class CopilotProvider extends CliProvider {
|
||||
});
|
||||
|
||||
// Send the prompt (non-blocking)
|
||||
await session.send({ prompt: promptText });
|
||||
await activeSession.send({ prompt: promptText });
|
||||
|
||||
// Process events as they arrive
|
||||
while (!sessionComplete || eventQueue.length > 0) {
|
||||
@@ -604,7 +637,7 @@ export class CopilotProvider extends CliProvider {
|
||||
|
||||
// Check for errors first (before processing events to avoid race condition)
|
||||
if (sessionError) {
|
||||
await session.destroy();
|
||||
await activeSession.destroy();
|
||||
await client.stop();
|
||||
throw sessionError;
|
||||
}
|
||||
@@ -624,11 +657,19 @@ export class CopilotProvider extends CliProvider {
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
await session.destroy();
|
||||
await activeSession.destroy();
|
||||
await client.stop();
|
||||
logger.debug('CopilotClient stopped successfully');
|
||||
} catch (error) {
|
||||
// Ensure client is stopped on error
|
||||
// Ensure session is destroyed and client is stopped on error to prevent leaks.
|
||||
// The session may have been created/resumed before the error occurred.
|
||||
if (session) {
|
||||
try {
|
||||
await session.destroy();
|
||||
} catch (sessionCleanupError) {
|
||||
logger.debug(`Failed to destroy session during cleanup: ${sessionCleanupError}`);
|
||||
}
|
||||
}
|
||||
try {
|
||||
await client.stop();
|
||||
} catch (cleanupError) {
|
||||
|
||||
Reference in New Issue
Block a user