mirror of
https://github.com/andy-stack/vaultkeeper-ai.git
synced 2026-07-22 16:30:27 +00:00
Introduce a new AbortService to centralize cancellation logic across all async operations, replacing scattered AbortSignal parameters with a unified singleton service. This improves maintainability and provides consistent cancellation behavior throughout the application. Key changes: - Add AbortService for centralized abort signal management with automatic cleanup - Refactor all AI providers (Claude, Gemini, OpenAI) to use AbortService instead of passing AbortSignal parameters - Update streaming operations to use centralized abort handling - Add CancellationIndicator component to show visual feedback during operation cancellation - Rename ChatAreaThought to ThoughtIndicator for better semantic clarity - Add Environment enum for consistent environment detection - Enhance ChatService lifecycle with proper cancellation state management - Remove scattered abort-related UI selectors and error messages in favor of dedicated indicator - Add safeContinue() factory method to ConversationContent for internal continuations - Update all tests to reflect new abort handling architecture This change simplifies the API surface by removing AbortSignal parameters from method signatures while improving the user experience with clearer cancellation feedback.
89 lines
3.6 KiB
TypeScript
89 lines
3.6 KiB
TypeScript
import { Resolve } from "./DependencyService";
|
|
import { Services } from "./Services";
|
|
import type { IConversationNamingService } from "AIClasses/IConversationNamingService";
|
|
import type { ConversationFileSystemService } from "./ConversationFileSystemService";
|
|
import type { Conversation } from "Conversations/Conversation";
|
|
import type { VaultService } from "./VaultService";
|
|
import { Path } from "Enums/Path";
|
|
import { Exception } from "Helpers/Exception";
|
|
import { Notice } from "obsidian";
|
|
import { AbortService } from "./AbortService";
|
|
|
|
export class ConversationNamingService {
|
|
private readonly stackLimit: number = 1000;
|
|
|
|
private namingProvider: IConversationNamingService | undefined;
|
|
private conversationService: ConversationFileSystemService;
|
|
private vaultService: VaultService;
|
|
private abortService: AbortService;
|
|
|
|
constructor() {
|
|
this.conversationService = Resolve<ConversationFileSystemService>(Services.ConversationFileSystemService);
|
|
this.vaultService = Resolve<VaultService>(Services.VaultService);
|
|
this.abortService = Resolve<AbortService>(Services.AbortService);
|
|
}
|
|
|
|
public resolveNamingProvider() {
|
|
this.namingProvider = Resolve<IConversationNamingService>(Services.IConversationNamingService);
|
|
}
|
|
|
|
public async requestName(conversation: Conversation, userPrompt: string, onNameChanged: ((name: string) => void) | undefined) {
|
|
await this.abortService.abortableOperation(async () => {
|
|
if (!this.namingProvider) {
|
|
return;
|
|
}
|
|
|
|
const conversationPath = this.conversationService.getCurrentConversationPath();
|
|
|
|
if (!conversationPath) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const generatedName: string = await this.namingProvider.generateName(userPrompt);
|
|
const validatedName: string = await this.validateName(generatedName);
|
|
|
|
const stillExists = this.conversationService.getCurrentConversationPath() === conversationPath;
|
|
if (!stillExists) {
|
|
return;
|
|
}
|
|
|
|
const updateResult = await this.conversationService.updateConversationTitle(conversationPath, validatedName);
|
|
|
|
if (updateResult instanceof Error) {
|
|
Exception.throw(updateResult);
|
|
}
|
|
|
|
conversation.title = validatedName;
|
|
const saveResult = await this.conversationService.saveConversation(conversation);
|
|
|
|
if (saveResult instanceof Error) {
|
|
Exception.throw(saveResult);
|
|
}
|
|
|
|
onNameChanged?.(conversation.title);
|
|
} catch (error) {
|
|
if (!AbortService.isAbortError(error)) {
|
|
Exception.log(error);
|
|
new Notice(`Failed to name conversation '${conversation.title}'`);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private async validateName(generatedName: string): Promise<string> {
|
|
const cleanedTitle = generatedName.trim().replace(/^["']|["']$/g, "").split(/\s+/).slice(0, 6).join(" ");
|
|
|
|
let index = 1;
|
|
let availableTitle = cleanedTitle;
|
|
while (await this.vaultService.exists(`${Path.Conversations}/${availableTitle}.json`, true)) {
|
|
availableTitle = `${cleanedTitle}(${index})`;
|
|
index++;
|
|
|
|
if (index > this.stackLimit) {
|
|
Exception.throw(`Stack limit reached when trying to generate conversation name for "${cleanedTitle}"`);
|
|
}
|
|
}
|
|
return availableTitle;
|
|
}
|
|
}
|