update task-loop type

This commit is contained in:
锦恢 2025-05-11 23:24:47 +08:00
parent 2db3ab8888
commit 942755b3e6
2 changed files with 68 additions and 5 deletions

View File

@ -39,7 +39,8 @@ export class TaskLoop {
private onError: (error: IErrorMssage) => void = (msg) => {};
private onChunk: (chunk: ChatCompletionChunk) => void = (chunk) => {};
private onDone: () => void = () => {};
private onToolCalled: (toolCallResult: ToolCallResult) => void = (toolCall) => {};
private onToolCall: (toolCall: ToolCall) => ToolCall = toolCall => toolCall;
private onToolCalled: (toolCallResult: ToolCallResult) => ToolCallResult = toolCallResult => toolCallResult;
private onEpoch: () => void = () => {};
private completionUsage: ChatCompletionChunk['usage'] | undefined;
private llmConfig: any;
@ -220,6 +221,10 @@ export class TaskLoop {
this.streamingToolCalls.value = [];
}
/**
* @description error
* @param handler
*/
public registerOnError(handler: (msg: IErrorMssage) => void) {
this.onError = handler;
}
@ -228,15 +233,35 @@ export class TaskLoop {
this.onChunk = handler;
}
/**
* @description chat.completion
* @param handler
*/
public registerOnDone(handler: () => void) {
this.onDone = handler;
}
/**
* @description epoch
* @param handler
*/
public registerOnEpoch(handler: () => void) {
this.onEpoch = handler;
}
public registerOnToolCalled(handler: (toolCallResult: ToolCallResult) => void) {
/**
* @description toolcall
* @param handler
*/
public registerOnToolCall(handler: (toolCall: ToolCall) => ToolCall) {
this.onToolCall = handler;
}
/**
* @description toolcall
* @param handler
*/
public registerOnToolCalled(handler: (toolCallResult: ToolCallResult) => ToolCallResult) {
this.onToolCalled = handler;
}
@ -335,6 +360,8 @@ export class TaskLoop {
pinkLog('调用工具数量:' + this.streamingToolCalls.value.length);
for (const toolCall of this.streamingToolCalls.value || []) {
const toolCallResult = await handleToolCalls(toolCall);
this.onToolCalled(toolCallResult);

View File

@ -80,12 +80,43 @@ export class TaskLoop {
private doConversation;
makeChatData(tabStorage: any): ChatCompletionCreateParamsBase | undefined;
abort(): void;
/**
* @description error
* @param handler
*/
registerOnError(handler: (msg: IErrorMssage) => void): void;
registerOnChunk(handler: (chunk: ChatCompletionChunk) => void): void;
/**
* @description chat.completion
* @param handler
*/
registerOnDone(handler: () => void): void;
/**
* @description epoch
* @param handler
*/
registerOnEpoch(handler: () => void): void;
/**
* @description toolcall
* @param handler
*/
registerOnToolCalled(handler: (toolCallResult: ToolCallResult) => void): void;
setMaxEpochs(maxEpochs: number): void;
/**
* @description toolcall
* @param handler
*/
registerOnToolCall(handler: (toolCall: ToolCall) => ToolCall): void;
/**
* @description LLM
*/
getLlmConfig(): any;
/**
* @description LLM nodejs
* @param config
@ -98,7 +129,12 @@ export class TaskLoop {
* })
*/
setLlmConfig(config: any): void;
getLlmConfig(): any;
/**
* @description epoch
* @param maxEpochs
*/
setMaxEpochs(maxEpochs: number): void;
bindStreaming(content: Ref<string>, toolCalls: Ref<ToolCall[]>): void;
connectToService(): Promise<void>;
/**