From e4f56e3877fa25a5556e0f803b02a7c745d40240 Mon Sep 17 00:00:00 2001 From: Andrew Beal Date: Fri, 19 Dec 2025 12:36:28 +0000 Subject: [PATCH] refactor: improve type safety in file service and error handling - Change listFiles() to synchronous method returning cached IDs - Add type predicate to ApiError.isApiError() for better type narrowing - Remove redundant ApiError union type in retry error handling --- AIClasses/BaseAIFileService.ts | 4 ++-- AIClasses/IAIFileService.ts | 2 +- Types/ApiError.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/AIClasses/BaseAIFileService.ts b/AIClasses/BaseAIFileService.ts index 92df8be..ef2ec72 100644 --- a/AIClasses/BaseAIFileService.ts +++ b/AIClasses/BaseAIFileService.ts @@ -35,7 +35,7 @@ export abstract class BaseAIFileService implements IAIFileService { this.fileIDs = await this.listFilesFromAPI(); } - public async listFiles(): Promise { + public listFiles(): string[] { return [...this.fileIDs]; } @@ -78,7 +78,7 @@ export abstract class BaseAIFileService implements IAIFileService { // Retries operation on retryable errors (500, 502, 503, 504) with exponential backoff protected async withRetry(operationName: string, operation: () => Promise): Promise { - let lastError: ApiError | unknown; + let lastError: unknown; for (let attempt = 1; attempt <= BaseAIFileService.MAX_RETRIES; attempt++) { try { diff --git a/AIClasses/IAIFileService.ts b/AIClasses/IAIFileService.ts index edacdab..fc59c10 100644 --- a/AIClasses/IAIFileService.ts +++ b/AIClasses/IAIFileService.ts @@ -2,7 +2,7 @@ import type { Attachment } from "Conversations/Attachment"; export interface IAIFileService { refreshCache(): Promise; - listFiles(): Promise; + listFiles(): string[]; uploadFile(attachment: Attachment): Promise; deleteFile(attachment: Attachment): Promise; } \ No newline at end of file diff --git a/Types/ApiError.ts b/Types/ApiError.ts index ed2d39a..4044d5c 100644 --- a/Types/ApiError.ts +++ b/Types/ApiError.ts @@ -23,7 +23,7 @@ export class ApiError extends Error { this.name = "ApiError"; } - static isApiError(error: unknown): boolean { + static isApiError(error: unknown): error is ApiError { return error instanceof ApiError; }