mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 22:03:45 +00:00
Fix branch summarization abort handling and tree navigation
- Check response.stopReason instead of catching errors for abort detection - Return result object from _generateBranchSummary instead of throwing - Fix summary attachment: attach to navigation target position, not old branch - Support root-level summaries (parentId=null) in branchWithSummary - Remove setTimeout hack for re-showing tree selector on abort
This commit is contained in:
parent
9dac0a1423
commit
01dae9ebcc
7 changed files with 55 additions and 40 deletions
|
|
@ -1583,7 +1583,7 @@ export class AgentSession {
|
|||
targetId: string,
|
||||
options: { summarize?: boolean; customInstructions?: string } = {},
|
||||
): Promise<{ editorText?: string; cancelled: boolean; aborted?: boolean }> {
|
||||
const oldLeafId = this.sessionManager.getLeafUuid();
|
||||
const oldLeafId = this.sessionManager.getLeafId();
|
||||
|
||||
// No-op if already at target
|
||||
if (targetId === oldLeafId) {
|
||||
|
|
@ -1661,21 +1661,19 @@ export class AgentSession {
|
|||
// Run default summarizer if needed
|
||||
let summaryText: string | undefined;
|
||||
if (options.summarize && entriesToSummarize.length > 0 && !hookSummary) {
|
||||
try {
|
||||
summaryText = await this._generateBranchSummary(
|
||||
entriesToSummarize,
|
||||
options.customInstructions,
|
||||
this._branchSummaryAbortController.signal,
|
||||
);
|
||||
} catch (error) {
|
||||
this._branchSummaryAbortController = undefined;
|
||||
// Check if aborted
|
||||
if (error instanceof Error && (error.name === "AbortError" || error.message === "aborted")) {
|
||||
return { cancelled: true, aborted: true };
|
||||
}
|
||||
// Re-throw actual errors so UI can display them
|
||||
throw error;
|
||||
const result = await this._generateBranchSummary(
|
||||
entriesToSummarize,
|
||||
options.customInstructions,
|
||||
this._branchSummaryAbortController.signal,
|
||||
);
|
||||
this._branchSummaryAbortController = undefined;
|
||||
if (result.aborted) {
|
||||
return { cancelled: true, aborted: true };
|
||||
}
|
||||
if (result.error) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
summaryText = result.summary;
|
||||
} else if (hookSummary) {
|
||||
summaryText = hookSummary.summary;
|
||||
}
|
||||
|
|
@ -1704,14 +1702,17 @@ export class AgentSession {
|
|||
}
|
||||
|
||||
// Switch leaf (with or without summary)
|
||||
// Summary is attached at the navigation target position (newLeafId), not the old branch
|
||||
let summaryEntry: BranchSummaryEntry | undefined;
|
||||
if (newLeafId === null) {
|
||||
// Navigating to root user message - reset leaf to empty
|
||||
this.sessionManager.resetLeaf();
|
||||
} else if (summaryText) {
|
||||
if (summaryText) {
|
||||
// Create summary at target position (can be null for root)
|
||||
const summaryId = this.sessionManager.branchWithSummary(newLeafId, summaryText);
|
||||
summaryEntry = this.sessionManager.getEntry(summaryId) as BranchSummaryEntry;
|
||||
} else if (newLeafId === null) {
|
||||
// No summary, navigating to root - reset leaf
|
||||
this.sessionManager.resetLeaf();
|
||||
} else {
|
||||
// No summary, navigating to non-root
|
||||
this.sessionManager.branch(newLeafId);
|
||||
}
|
||||
|
||||
|
|
@ -1723,7 +1724,7 @@ export class AgentSession {
|
|||
if (this._hookRunner) {
|
||||
await this._hookRunner.emit({
|
||||
type: "session_tree",
|
||||
newLeafId: this.sessionManager.getLeafUuid(),
|
||||
newLeafId: this.sessionManager.getLeafId(),
|
||||
oldLeafId,
|
||||
summaryEntry,
|
||||
fromHook: summaryText ? fromHook : undefined,
|
||||
|
|
@ -1744,7 +1745,7 @@ export class AgentSession {
|
|||
entries: SessionEntry[],
|
||||
customInstructions: string | undefined,
|
||||
signal: AbortSignal,
|
||||
): Promise<string> {
|
||||
): Promise<{ summary?: string; aborted?: boolean; error?: string }> {
|
||||
// Convert entries to messages for summarization
|
||||
const messages: Array<{ role: string; content: string }> = [];
|
||||
for (const entry of entries) {
|
||||
|
|
@ -1770,7 +1771,7 @@ export class AgentSession {
|
|||
}
|
||||
|
||||
if (messages.length === 0) {
|
||||
return "No content to summarize";
|
||||
return { summary: "No content to summarize" };
|
||||
}
|
||||
|
||||
// Build prompt for summarization
|
||||
|
|
@ -1804,12 +1805,20 @@ export class AgentSession {
|
|||
{ apiKey, signal, maxTokens: 1024 },
|
||||
);
|
||||
|
||||
// Check if aborted or errored
|
||||
if (response.stopReason === "aborted") {
|
||||
return { aborted: true };
|
||||
}
|
||||
if (response.stopReason === "error") {
|
||||
return { error: response.errorMessage || "Summarization failed" };
|
||||
}
|
||||
|
||||
const summary = response.content
|
||||
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
||||
.map((c) => c.text)
|
||||
.join("\n");
|
||||
|
||||
return summary || "No summary generated";
|
||||
return { summary: summary || "No summary generated" };
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ export type ReadonlySessionManager = Pick<
|
|||
| "getSessionDir"
|
||||
| "getSessionId"
|
||||
| "getSessionFile"
|
||||
| "getLeafUuid"
|
||||
| "getLeafId"
|
||||
| "getLeafEntry"
|
||||
| "getEntry"
|
||||
| "getLabel"
|
||||
|
|
|
|||
|
|
@ -684,7 +684,7 @@ export class SessionManager {
|
|||
// Tree Traversal
|
||||
// =========================================================================
|
||||
|
||||
getLeafUuid(): string | null {
|
||||
getLeafId(): string | null {
|
||||
return this.leafId;
|
||||
}
|
||||
|
||||
|
|
@ -845,8 +845,8 @@ export class SessionManager {
|
|||
* Same as branch(), but also appends a branch_summary entry that captures
|
||||
* context from the abandoned conversation path.
|
||||
*/
|
||||
branchWithSummary(branchFromId: string, summary: string): string {
|
||||
if (!this.byId.has(branchFromId)) {
|
||||
branchWithSummary(branchFromId: string | null, summary: string): string {
|
||||
if (branchFromId !== null && !this.byId.has(branchFromId)) {
|
||||
throw new Error(`Entry ${branchFromId} not found`);
|
||||
}
|
||||
this.leafId = branchFromId;
|
||||
|
|
@ -855,7 +855,7 @@ export class SessionManager {
|
|||
id: generateId(this.byId),
|
||||
parentId: branchFromId,
|
||||
timestamp: new Date().toISOString(),
|
||||
fromId: branchFromId,
|
||||
fromId: branchFromId ?? "root",
|
||||
summary,
|
||||
};
|
||||
this._appendEntry(entry);
|
||||
|
|
|
|||
|
|
@ -609,8 +609,12 @@ class TreeList implements Component {
|
|||
return `[edit: ${path}]`;
|
||||
}
|
||||
case "bash": {
|
||||
const cmd = String(args.command || "").slice(0, 50);
|
||||
return `[bash: ${cmd}${(args.command as string)?.length > 50 ? "..." : ""}]`;
|
||||
const rawCmd = String(args.command || "");
|
||||
const cmd = rawCmd
|
||||
.replace(/[\n\t]/g, " ")
|
||||
.trim()
|
||||
.slice(0, 50);
|
||||
return `[bash: ${cmd}${rawCmd.length > 50 ? "..." : ""}]`;
|
||||
}
|
||||
case "grep": {
|
||||
const pattern = String(args.pattern || "");
|
||||
|
|
|
|||
|
|
@ -1600,7 +1600,7 @@ export class InteractiveMode {
|
|||
|
||||
private showTreeSelector(): void {
|
||||
const tree = this.sessionManager.getTree();
|
||||
const realLeafId = this.sessionManager.getLeafUuid();
|
||||
const realLeafId = this.sessionManager.getLeafId();
|
||||
|
||||
// Find the visible leaf for display (skip metadata entries like labels)
|
||||
let visibleLeafId = realLeafId;
|
||||
|
|
@ -1660,7 +1660,9 @@ export class InteractiveMode {
|
|||
const result = await this.session.navigateTree(entryId, { summarize: wantsSummary });
|
||||
|
||||
if (result.aborted) {
|
||||
// Summarization aborted - re-show tree selector
|
||||
this.showStatus("Branch summarization cancelled");
|
||||
this.showTreeSelector();
|
||||
return;
|
||||
}
|
||||
if (result.cancelled) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue