feat: abort agent requests

This commit is contained in:
Danny Avila 2024-09-02 14:06:39 -04:00
parent 757f544a9b
commit 7d54f7147f
No known key found for this signature in database
GPG key ID: 2DD9CC89B9B50364
4 changed files with 26 additions and 5 deletions

View file

@ -46,6 +46,13 @@ class AgentClient extends BaseClient {
this.options = Object.assign({ endpoint: options.endpoint }, clientOptions);
}
/**
* Returns the aggregated content parts for the current run.
* @returns {MessageContentComplex[]} */
getContentParts() {
return this.contentParts;
}
setOptions(options) {
logger.info('[api/server/controllers/agents/client.js] setOptions', options);
}
@ -437,12 +444,19 @@ class AgentClient extends BaseClient {
});
logger.info(this.contentParts, { depth: null });
} catch (err) {
if (!abortController.signal.aborted) {
logger.error(
'[api/server/controllers/agents/client.js #chatCompletion] Unhandled error type',
'[api/server/controllers/agents/client.js #sendCompletion] Unhandled error type',
err,
);
throw err;
}
logger.warn(
'[api/server/controllers/agents/client.js #sendCompletion] Operation aborted',
err,
);
}
}
getEncoding() {

View file

@ -46,6 +46,7 @@ const AgentController = async (req, res, next, initializeClient, addTitle) => {
};
try {
/** @type {{ client: TAgentClient }} */
const { client } = await initializeClient({ req, res, endpointOption });
const getAbortData = () => ({
@ -54,8 +55,8 @@ const AgentController = async (req, res, next, initializeClient, addTitle) => {
promptTokens,
conversationId,
userMessagePromise,
// text: getPartialText(),
messageId: responseMessageId,
content: client.getContentParts(),
parentMessageId: overrideParentMessageId ?? userMessageId,
});

View file

@ -107,7 +107,7 @@ const createAbortController = (req, res, getAbortData, getReqData) => {
finish_reason: 'incomplete',
endpoint: endpointOption.endpoint,
iconURL: endpointOption.iconURL,
model: endpointOption.modelOptions.model,
model: endpointOption.modelOptions?.model ?? endpointOption.model_parameters?.model,
unfinished: false,
error: false,
isCreatedByUser: false,

View file

@ -899,6 +899,12 @@
* @memberof typedefs
*/
/**
* @exports TAgentClient
* @typedef {import('./server/controllers/agents/client')} TAgentClient
* @memberof typedefs
*/
/**
* @exports ImportBatchBuilder
* @typedef {import('./server/utils/import/importBatchBuilder.js').ImportBatchBuilder} ImportBatchBuilder