Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 89a3261

Browse files
chore: show error from remote engine, lint (#949)
1 parent a997721 commit 89a3261

File tree

5 files changed

+90
-47
lines changed

5 files changed

+90
-47
lines changed

cortex-js/src/domain/abstracts/oai.abstract.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export abstract class OAIEngineExtension extends EngineExtension {
4040
}),
4141
);
4242

43-
4443
if (!response) {
4544
throw new Error('No response');
4645
}

cortex-js/src/extensions/cohere.engine.ts

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import { EngineStatus } from '@/domain/abstracts/engine.abstract';
88
import { ChatCompletionMessage } from '@/infrastructure/dtos/chat/chat-completion-message.dto';
99

1010
enum RoleType {
11-
user = 'USER',
12-
chatbot = 'CHATBOT',
13-
system = 'SYSTEM',
14-
}
15-
16-
type CoherePayloadType = {
17-
chat_history?: Array<{ role: RoleType; message: string }>
18-
message?: string
19-
preamble?: string
20-
}
11+
user = 'USER',
12+
chatbot = 'CHATBOT',
13+
system = 'SYSTEM',
14+
}
15+
16+
type CoherePayloadType = {
17+
chat_history?: Array<{ role: RoleType; message: string }>;
18+
message?: string;
19+
preamble?: string;
20+
};
2121

2222
/**
2323
* A class that implements the InferenceExtension interface from the @janhq/core package.
@@ -62,9 +62,9 @@ export default class CoHereEngineExtension extends OAIEngineExtension {
6262
}
6363

6464
transformPayload = (payload: any): CoherePayloadType => {
65-
console.log('payload', payload)
65+
console.log('payload', payload);
6666
if (payload.messages.length === 0) {
67-
return {}
67+
return {};
6868
}
6969

7070
const { messages, ...params } = payload;
@@ -73,31 +73,34 @@ export default class CoHereEngineExtension extends OAIEngineExtension {
7373
chat_history: [],
7474
message: '',
7575
};
76-
(messages as ChatCompletionMessage[]).forEach((item: ChatCompletionMessage, index: number) => {
77-
// Assign the message of the last item to the `message` property
78-
if (index === messages.length - 1) {
79-
convertedData.message = item.content as string
80-
return
81-
}
82-
if (item.role === 'user') {
83-
convertedData.chat_history!!.push({
84-
role: 'USER' as RoleType,
85-
message: item.content as string,
86-
})
87-
} else if (item.role === 'assistant') {
88-
convertedData.chat_history!!.push({
89-
role: 'CHATBOT' as RoleType,
90-
message: item.content as string,
91-
})
92-
} else if (item.role === 'system') {
93-
convertedData.preamble = item.content as string
94-
}
95-
})
96-
return convertedData
97-
}
76+
(messages as ChatCompletionMessage[]).forEach(
77+
(item: ChatCompletionMessage, index: number) => {
78+
// Assign the message of the last item to the `message` property
79+
if (index === messages.length - 1) {
80+
convertedData.message = item.content as string;
81+
return;
82+
}
83+
if (item.role === 'user') {
84+
convertedData.chat_history!.push({
85+
role: 'USER' as RoleType,
86+
message: item.content as string,
87+
});
88+
} else if (item.role === 'assistant') {
89+
convertedData.chat_history!.push({
90+
role: 'CHATBOT' as RoleType,
91+
message: item.content as string,
92+
});
93+
} else if (item.role === 'system') {
94+
convertedData.preamble = item.content as string;
95+
}
96+
},
97+
);
98+
return convertedData;
99+
};
98100

99101
transformResponse = (data: any) => {
100-
const text = typeof data === 'object' ? data.text : JSON.parse(data).text ?? ''
102+
const text =
103+
typeof data === 'object' ? data.text : (JSON.parse(data).text ?? '');
101104
return JSON.stringify({
102105
choices: [
103106
{
@@ -107,5 +110,5 @@ export default class CoHereEngineExtension extends OAIEngineExtension {
107110
},
108111
],
109112
});
110-
}
113+
};
111114
}

cortex-js/src/extensions/openrouter.engine.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ export default class OpenRouterEngineExtension extends OAIEngineExtension {
5050

5151
transformPayload = (data: any): any => {
5252
return {
53-
...data,
54-
model:"openrouter/auto",
55-
}
53+
...data,
54+
model: 'openrouter/auto',
55+
};
5656
};
57-
5857
}

cortex-js/src/infrastructure/controllers/chat.controller.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ export class ChatController {
3333
@Body() createChatDto: CreateChatCompletionDto,
3434
@Res() res: Response,
3535
) {
36-
const { stream } = createChatDto;
37-
36+
let { stream } = createChatDto;
37+
stream = false;
38+
createChatDto.stream = stream;
3839
this.chatService
3940
.inference(createChatDto, extractCommonHeaders(headers))
4041
.then((response) => {
@@ -46,9 +47,50 @@ export class ChatController {
4647
res.json(response);
4748
}
4849
})
49-
.catch((error) =>
50-
res.status(error.statusCode ?? 400).send(error.message),
51-
);
50+
.catch((error) => {
51+
const statusCode = error.response?.status ?? 400;
52+
let errorMessage;
53+
if (!stream) {
54+
const data = error.response?.data;
55+
return res
56+
.status(statusCode)
57+
.send(
58+
data.error?.message ||
59+
data.message ||
60+
error.message ||
61+
'An error occurred',
62+
);
63+
}
64+
const streamResponse = error.response?.data;
65+
let data = '';
66+
67+
streamResponse.on('data', (chunk: any) => {
68+
data += chunk;
69+
});
70+
71+
streamResponse.on('end', () => {
72+
try {
73+
const jsonResponse = JSON.parse(data);
74+
errorMessage =
75+
jsonResponse.error?.message ||
76+
jsonResponse.message ||
77+
error.message ||
78+
'An error occurred';
79+
} catch (err) {
80+
errorMessage = 'An error occurred while processing the response';
81+
}
82+
return res
83+
.status(error.statusCode ?? 400)
84+
.send(errorMessage || error.message || 'An error occurred');
85+
});
86+
87+
streamResponse.on('error', (streamError: any) => {
88+
errorMessage = streamError.message ?? 'An error occurred';
89+
return res
90+
.status(error.statusCode ?? 400)
91+
.send(errorMessage || error.message || 'An error occurred');
92+
});
93+
});
5294

5395
this.telemetryUsecases.addEventToQueue({
5496
name: EventName.CHAT,

cortex-js/src/infrastructure/controllers/engines.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class EnginesController {
112112
})
113113
@Patch(':name(*)')
114114
update(@Param('name') name: string, @Body() configs?: any | undefined) {
115-
console.log('configs', configs)
115+
console.log('configs', configs);
116116
return this.enginesUsecases.updateConfigs(
117117
configs.config,
118118
configs.value,

0 commit comments

Comments
 (0)