Handle exception with default return value for devchat client methods
This commit is contained in:
parent
b663fdcc3f
commit
f707bef5fc
@ -32,6 +32,27 @@ function timeThis(
|
|||||||
return descriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function catchAndReturn(defaultReturn: any) {
|
||||||
|
return function (
|
||||||
|
target: Object,
|
||||||
|
propertyKey: string,
|
||||||
|
descriptor: TypedPropertyDescriptor<any>
|
||||||
|
) {
|
||||||
|
const originalMethod = descriptor.value;
|
||||||
|
|
||||||
|
descriptor.value = async function (...args: any[]) {
|
||||||
|
try {
|
||||||
|
return await originalMethod.apply(this, args);
|
||||||
|
} catch (error) {
|
||||||
|
logger.channel()?.error(`Error in [${propertyKey}]: ${error}`);
|
||||||
|
return defaultReturn;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return descriptor;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export interface ChatRequest {
|
export interface ChatRequest {
|
||||||
content: string;
|
content: string;
|
||||||
model_name: string;
|
model_name: string;
|
||||||
@ -145,7 +166,8 @@ export class DevChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@timeThis
|
@timeThis
|
||||||
async getWorkflowList(): Promise<any> {
|
@catchAndReturn([])
|
||||||
|
async getWorkflowList(): Promise<any[]> {
|
||||||
const response = await this._get("/workflows/list");
|
const response = await this._get("/workflows/list");
|
||||||
logger
|
logger
|
||||||
.channel()
|
.channel()
|
||||||
@ -158,6 +180,7 @@ export class DevChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@timeThis
|
@timeThis
|
||||||
|
@catchAndReturn({})
|
||||||
async getWorkflowConfig(): Promise<any> {
|
async getWorkflowConfig(): Promise<any> {
|
||||||
const response = await this._get("/workflows/config");
|
const response = await this._get("/workflows/config");
|
||||||
logger
|
logger
|
||||||
@ -171,6 +194,7 @@ export class DevChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@timeThis
|
@timeThis
|
||||||
|
@catchAndReturn(undefined)
|
||||||
async updateWorkflows(): Promise<void> {
|
async updateWorkflows(): Promise<void> {
|
||||||
const response = await this._post("/workflows/update");
|
const response = await this._post("/workflows/update");
|
||||||
logger
|
logger
|
||||||
@ -227,9 +251,7 @@ export class DevChatClient {
|
|||||||
chatRes.finish_reason = chunkData["finish_reason"];
|
chatRes.finish_reason = chunkData["finish_reason"];
|
||||||
if (chatRes.finish_reason === "should_run_workflow") {
|
if (chatRes.finish_reason === "should_run_workflow") {
|
||||||
chatRes.extra = chunkData["extra"];
|
chatRes.extra = chunkData["extra"];
|
||||||
logger
|
logger.channel()?.debug("should run workflow via cli.");
|
||||||
.channel()
|
|
||||||
?.debug("should run workflow via cli.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,12 +267,20 @@ export class DevChatClient {
|
|||||||
|
|
||||||
response.data.on("error", (error) => {
|
response.data.on("error", (error) => {
|
||||||
logger.channel()?.error("Streaming error:", error);
|
logger.channel()?.error("Streaming error:", error);
|
||||||
// TODO: handle error?
|
chatRes.isError = true;
|
||||||
reject(error); // Reject the promise on error
|
chatRes.response += `\n${error}`;
|
||||||
|
resolve(chatRes); // handle error by resolving the promise
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// TODO: handle error?
|
const errorRes: ChatResponse = {
|
||||||
reject(error); // Reject the promise if the request fails
|
"prompt-hash": "",
|
||||||
|
user: "",
|
||||||
|
date: "",
|
||||||
|
response: `${error}`,
|
||||||
|
finish_reason: "",
|
||||||
|
isError: true,
|
||||||
|
};
|
||||||
|
resolve(errorRes); // handle error by resolving the promise using an errorRes
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -271,6 +301,7 @@ export class DevChatClient {
|
|||||||
* @returns A tuple of inserted hash and error message.
|
* @returns A tuple of inserted hash and error message.
|
||||||
*/
|
*/
|
||||||
@timeThis
|
@timeThis
|
||||||
|
@catchAndReturn({ hash: "" })
|
||||||
async insertLog(logData: LogData): Promise<LogInsertRes> {
|
async insertLog(logData: LogData): Promise<LogInsertRes> {
|
||||||
let body = {
|
let body = {
|
||||||
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
|
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
|
||||||
@ -282,7 +313,6 @@ export class DevChatClient {
|
|||||||
if (jsondata.length <= DevChatClient.logRawDataSizeLimit) {
|
if (jsondata.length <= DevChatClient.logRawDataSizeLimit) {
|
||||||
// Use json data directly
|
// Use json data directly
|
||||||
body["jsondata"] = jsondata;
|
body["jsondata"] = jsondata;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Write json data to a temp file
|
// Write json data to a temp file
|
||||||
const tempDir = os.tmpdir();
|
const tempDir = os.tmpdir();
|
||||||
@ -300,13 +330,15 @@ export class DevChatClient {
|
|||||||
response.data
|
response.data
|
||||||
)}, ${typeof response.data}}`
|
)}, ${typeof response.data}}`
|
||||||
);
|
);
|
||||||
|
|
||||||
// Clean up temp file
|
// Clean up temp file
|
||||||
if (filepath) {
|
if (filepath) {
|
||||||
try {
|
try {
|
||||||
await fs.promises.unlink(filepath);
|
await fs.promises.unlink(filepath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.channel()?.error(`Failed to delete temp file ${filepath}: ${error}`);
|
logger
|
||||||
|
.channel()
|
||||||
|
?.error(`Failed to delete temp file ${filepath}: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,6 +349,7 @@ export class DevChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@timeThis
|
@timeThis
|
||||||
|
@catchAndReturn({ success: false })
|
||||||
async deleteLog(logHash: string): Promise<LogDeleteRes> {
|
async deleteLog(logHash: string): Promise<LogDeleteRes> {
|
||||||
const data = {
|
const data = {
|
||||||
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
|
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
|
||||||
@ -330,14 +363,15 @@ export class DevChatClient {
|
|||||||
response.data
|
response.data
|
||||||
)}, ${typeof response.data}}`
|
)}, ${typeof response.data}}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const res: LogDeleteRes = {
|
const res: LogDeleteRes = {
|
||||||
success: response.data["success"],
|
success: response.data["success"],
|
||||||
};
|
};
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@timeThis
|
@timeThis
|
||||||
|
@catchAndReturn([])
|
||||||
async getTopicLogs(
|
async getTopicLogs(
|
||||||
topicRootHash: string,
|
topicRootHash: string,
|
||||||
limit: number,
|
limit: number,
|
||||||
@ -348,12 +382,9 @@ export class DevChatClient {
|
|||||||
offset: offset,
|
offset: offset,
|
||||||
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
|
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
|
||||||
};
|
};
|
||||||
const response = await this._get(
|
const response = await this._get(`/topics/${topicRootHash}/logs`, {
|
||||||
`/topics/${topicRootHash}/logs`,
|
params: data,
|
||||||
{
|
});
|
||||||
params: data,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const logs: ShortLog[] = response.data;
|
const logs: ShortLog[] = response.data;
|
||||||
logs.reverse();
|
logs.reverse();
|
||||||
@ -366,6 +397,7 @@ export class DevChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@timeThis
|
@timeThis
|
||||||
|
@catchAndReturn([])
|
||||||
async getTopics(limit: number, offset: number): Promise<any[]> {
|
async getTopics(limit: number, offset: number): Promise<any[]> {
|
||||||
const data = {
|
const data = {
|
||||||
limit: limit,
|
limit: limit,
|
||||||
@ -387,6 +419,7 @@ export class DevChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@timeThis
|
@timeThis
|
||||||
|
@catchAndReturn(undefined)
|
||||||
async deleteTopic(topicRootHash: string): Promise<void> {
|
async deleteTopic(topicRootHash: string): Promise<void> {
|
||||||
const data = {
|
const data = {
|
||||||
topic_hash: topicRootHash,
|
topic_hash: topicRootHash,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user