@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
91 lines (90 loc) • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeChatCompletionChunks = exports.mergeToolCalls = exports.inputMessageToChatCompletionMessage = exports.normalizeContentParts = exports.RateLimitError = exports.isRateLimitError = void 0;
const logger_1 = require("../../../util/logger");
const HTTP_RATE_LIMIT = 429;
function isRateLimitError(e) {
if (!e || typeof e !== 'object') {
return false;
}
return ('status' in e && e.status === HTTP_RATE_LIMIT) || ('code' in e && e.code === HTTP_RATE_LIMIT);
}
exports.isRateLimitError = isRateLimitError;
class RateLimitError extends logger_1.AppModuleAggregateError {
constructor({ error, message } = {}) {
const newMessage = message || 'Rate limit reached';
super([error || new Error(newMessage)], newMessage);
this.status = HTTP_RATE_LIMIT;
this.name = 'RateLimitError';
}
}
exports.RateLimitError = RateLimitError;
function normalizeContentParts(content) {
return Array.isArray(content)
? content.map((part) => part.type === 'image'
? {
type: 'image_url',
image_url: {
url: part.url,
},
}
: part)
: content;
}
exports.normalizeContentParts = normalizeContentParts;
function inputMessageToChatCompletionMessage(message) {
switch (message.role) {
case undefined:
case 'user':
return {
role: 'user',
content: normalizeContentParts(message.content),
};
default:
return message;
}
}
exports.inputMessageToChatCompletionMessage = inputMessageToChatCompletionMessage;
function mergeToolCalls(chunks) {
const toolCalls = [];
chunks.forEach((chunk) => {
if (!chunk.tool_calls) {
return;
}
chunk.tool_calls.forEach((toolCallChunk) => {
const delta = toolCalls.find((toolCall) => toolCall.index === toolCallChunk.index);
if (!delta) {
toolCalls.push(toolCallChunk);
}
else if (!!toolCallChunk.function && 'arguments' in toolCallChunk.function) {
if (!delta.function) {
delta.function = toolCallChunk.function;
}
else if (!delta.function.arguments) {
delta.function.arguments = toolCallChunk.function.arguments;
}
else {
delta.function.arguments += toolCallChunk.function.arguments;
}
}
});
});
toolCalls.map((tool) => {
if ('index' in tool) {
delete tool.index;
}
});
return !toolCalls.length ? null : toolCalls;
}
exports.mergeToolCalls = mergeToolCalls;
function mergeChatCompletionChunks(chunks) {
const tool_calls = mergeToolCalls(chunks);
return [
{
role: chunks[0].role || 'assistant',
content: tool_calls ? null : chunks.map((chunk) => chunk.content).join(''),
tool_calls,
},
];
}
exports.mergeChatCompletionChunks = mergeChatCompletionChunks;