andy-stack_vaultkeeper-ai/Conversations/Conversation.ts
Andrew Beal 18372ce01b refactor: improve error handling with structured API errors and retry logic
Replace console.error calls with Exception.log, implement typed API errors with automatic retry for transient failures (rate limits, server errors), add error state tracking to conversation content, and enhance system prompt with action-first operating principle, update unit tests
2025-11-19 20:30:53 +00:00

59 lines
No EOL
2.1 KiB
TypeScript

import { StringTools } from "Helpers/StringTools";
import { ConversationContent } from "./ConversationContent";
import { ApiErrorType } from "Types/ApiError";
export class Conversation {
title: string;
created: Date;
updated: Date;
path: string;
contents: ConversationContent[] = [];
constructor() {
this.created = new Date();
this.updated = new Date();
this.title = `${StringTools.dateToString(this.created)}`;
}
public static isConversationData(data: unknown): data is { title: string; created: string; updated: string; contents: ConversationContent[] } {
return (
typeof data === "object" &&
data !== null &&
"title" in data &&
"created" in data &&
"updated" in data &&
"contents" in data &&
typeof data.title === "string" &&
typeof data.created === "string" &&
typeof data.updated === "string" &&
Array.isArray(data.contents) &&
data.contents.every(ConversationContent.isConversationContentData)
);
}
public setMostRecentContent(content: string) {
const conversationContent: ConversationContent | undefined = this.contents[this.contents.length - 1];
if (conversationContent) {
conversationContent.content = content;
conversationContent.errorType = undefined;
}
}
public setMostRecentError(content: string, errorType: ApiErrorType) {
const conversationContent: ConversationContent | undefined = this.contents[this.contents.length - 1];
if (conversationContent) {
conversationContent.content = content;
conversationContent.errorType = errorType;
}
}
public setMostRecentFunctionCall(functionCall: string) {
const conversationContent: ConversationContent | undefined = this.contents[this.contents.length - 1];
if (conversationContent) {
conversationContent.functionCall = functionCall;
conversationContent.isFunctionCall = true;
}
}
}