Skip to content

Commit

Permalink
improving error handling functionality for services
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed May 26, 2024
1 parent ef30159 commit 69c043a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
15 changes: 10 additions & 5 deletions component/src/services/openAI/openAIAssistantIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class OpenAIAssistantIO extends DirectServiceIO {
private static readonly NEW_ASSISTANT_URL = 'https://api.openai.com/v1/assistants';
private static readonly POLLING_TIMEOUT_MS = 800;
private readonly _functionHandler?: AssistantFunctionHandler;
permittedErrorPrefixes = ['Incorrect'];
permittedErrorPrefixes = ['Incorrect', 'Please send text'];
private messages?: Messages;
private run_id?: string;
private searchedForThreadId = false;
Expand Down Expand Up @@ -108,7 +108,7 @@ export class OpenAIAssistantIO extends DirectServiceIO {
return undefined;
}

private static processFileSearchMessage(
private static processAttachmentsMessage(
processedMessage: MessageContentI,
uploadedFiles: UploadedFile[],
toolType: OpenAIAssistant['files_tool_type']
Expand All @@ -126,10 +126,10 @@ export class OpenAIAssistantIO extends DirectServiceIO {
// https://platform.openai.com/docs/api-reference/messages/createMessage
if (uploadedFiles && uploadedFiles.length > 0) {
if (this.filesToolType === 'file_search') {
return OpenAIAssistantIO.processFileSearchMessage(processedMessage, uploadedFiles, 'file_search');
return OpenAIAssistantIO.processAttachmentsMessage(processedMessage, uploadedFiles, 'file_search');
}
if (this.filesToolType === 'code_interpreter') {
return OpenAIAssistantIO.processFileSearchMessage(processedMessage, uploadedFiles, 'code_interpreter');
return OpenAIAssistantIO.processAttachmentsMessage(processedMessage, uploadedFiles, 'code_interpreter');
}
const imageMessage = OpenAIAssistantIO.processImageMessage(processedMessage, uploadedFiles);
if (imageMessage) return imageMessage;
Expand Down Expand Up @@ -200,7 +200,12 @@ export class OpenAIAssistantIO extends DirectServiceIO {
if (this.waitingForStreamResponse || (this.isSSEStream && this.sessionId)) {
return await this.handleStream(result);
}
if (result.error) throw result.error.message;
if (result.error) {
if (result.error.message.startsWith(OpenAIAssistantUtils.FILES_WITH_TEXT_ERROR)) {
throw Error('Please send text with your file(s)');
}
throw result.error.message;
}
await this.assignThreadAndRun(result);
// https://platform.openai.com/docs/api-reference/runs/getRun
const url = `${OpenAIAssistantIO.THREAD_PREFIX}/${this.sessionId}/runs/${this.run_id}`;
Expand Down
3 changes: 3 additions & 0 deletions component/src/services/openAI/utils/openAIAssistantUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type FileDetails = {fileId: string; path?: string; name?: string}[];
export type UploadedFile = {id: string; name: string};

export class OpenAIAssistantUtils {
// triggered ONLY for file_search and code_interceptor
public static readonly FILES_WITH_TEXT_ERROR = 'content with type `text` must have `text` values';

public static async storeFiles(serviceIO: ServiceIO, messages: Messages, files: File[]) {
const headers = serviceIO.connectSettings.headers;
if (!headers) return;
Expand Down
3 changes: 3 additions & 0 deletions component/src/types/errorInternal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {Response} from './response';

export type ErrorResp = string | string[] | Error | Response;
2 changes: 1 addition & 1 deletion component/src/utils/HTTP/customHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CustomHandler {
stream.finaliseStreamedMessage();
} catch (error) {
console.error(error);
messages.addNewErrorMessage('service', error as string);
messages.addNewErrorMessage('service', error as Error);
}
}

Expand Down
9 changes: 8 additions & 1 deletion component/src/utils/HTTP/requestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Messages} from '../../views/chat/messages/messages';
import {Response as ResponseI} from '../../types/response';
import {RequestDetails} from '../../types/interceptors';
import {ErrorResp} from '../../types/errorInternal';
import {ServiceIO} from '../../services/serviceIO';
import {GenericObject} from '../../types/object';
import {Connect} from '../../types/connect';
Expand Down Expand Up @@ -35,9 +36,15 @@ export class RequestUtils {
return result;
}

public static displayError(messages: Messages, err: object | string, defMessage = 'Service error, please try again.') {
public static displayError(messages: Messages, err: ErrorResp, defMessage = 'Service error, please try again.') {
console.error(err);
if (typeof err === 'object') {
if (err instanceof Error) {
return messages.addNewErrorMessage('service', err.message);
}
if (Array.isArray(err) || typeof err.error === 'string') {
return messages.addNewErrorMessage('service', err);
}
if (Object.keys(err).length === 0) {
return messages.addNewErrorMessage('service', defMessage);
}
Expand Down
38 changes: 28 additions & 10 deletions component/src/views/chat/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {FireEvents} from '../../../utils/events/fireEvents';
import {ErrorMessageOverrides} from '../../../types/error';
import {ResponseI} from '../../../types/responseInternal';
import {TextToSpeech} from './textToSpeech/textToSpeech';
import {ErrorResp} from '../../../types/errorInternal';
import {Demo, DemoResponse} from '../../../types/demo';
import {MessageStyleUtils} from './messageStyleUtils';
import {IntroMessage} from '../../../types/messages';
Expand Down Expand Up @@ -197,7 +198,7 @@ export class Messages extends MessagesBase {
}

// prettier-ignore
public addNewErrorMessage(type: keyof Omit<ErrorMessageOverrides, 'default'>, message?: string) {
public addNewErrorMessage(type: keyof Omit<ErrorMessageOverrides, 'default'>, message?: ErrorResp) {
this.removeMessageOnError();
const messageElements = Messages.createBaseElements();
const {outerContainer, bubbleElement} = messageElements;
Expand All @@ -222,16 +223,33 @@ export class Messages extends MessagesBase {
return undefined;
}

private getPermittedMessage(message?: string) {
private static extractErrorMessages(message: ErrorResp): string[] {
if (Array.isArray(message)) {
return message;
}
if (message instanceof Error) {
return [message.message];
}
if (typeof message === 'string') {
return [message];
}
if (typeof message === 'object' && message.error) {
return [message.error];
}
return [];
}

private getPermittedMessage(message?: ErrorResp): string | undefined {
if (message) {
if (this._displayServiceErrorMessages) return message;
if (typeof message === 'string' && this._permittedErrorPrefixes) {
const result = Messages.checkPermittedErrorPrefixes(this._permittedErrorPrefixes, message);
if (result) return result;
} else if (Array.isArray(message) && this._permittedErrorPrefixes) {
for (let i = 0; i < message.length; i += 1) {
const result = Messages.checkPermittedErrorPrefixes(this._permittedErrorPrefixes, message[i]);
if (result) return result;
const messages = Messages.extractErrorMessages(message); // turning all into array for convenience
for (let i = 0; i < messages.length; i += 1) {
const messageStr = messages[i];
if (typeof messageStr === 'string') {
if (this._displayServiceErrorMessages) return messageStr;
if (this._permittedErrorPrefixes) {
const result = Messages.checkPermittedErrorPrefixes(this._permittedErrorPrefixes, messageStr);
if (result) return result;
}
}
}
}
Expand Down

0 comments on commit 69c043a

Please sign in to comment.