Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: peer多智能体组多轮对话的展示 & umi版本较低时流失输出无效 #58

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions web-apps/platform/src/modules/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ export class Chat extends AsyncModel<Chat, ChatOption> {
const newMessageModel: ChatMessageModel = JSON.parse(e.data);
const message = this.getOrCreateMessage(newMessageModel);
this.messages = [...this.messages, message];
setImmediate(() => this.scrollToBottom(true, false));
setImmediate(() => {
if (!this.showToBottomBtn) {
this.scrollToBottom(true, false);
}
});
}

if (e.event === 'chunk') {
Expand All @@ -241,7 +245,11 @@ export class Chat extends AsyncModel<Chat, ChatOption> {
if (msg) {
this.processingChunk = msg;
msg.appendChunk(chunk);
setImmediate(() => this.scrollToBottom(true, false));
setImmediate(() => {
if (!this.showToBottomBtn) {
this.scrollToBottom(true, false);
}
});
}
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion web-apps/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"remark-breaks": "^4.0.0",
"remark-gfm": "^4.0.0",
"timeago.js": "^4.0.2",
"umi": "^4.1.1"
"umi": "^4.3.18"
},
"devDependencies": {
"@babel/plugin-proposal-decorators": "^7.23.7",
Expand Down
137 changes: 108 additions & 29 deletions web-apps/ui/src/modules/chat-message/peer-message-item-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
ChatEventResult,
ChatEventStep,
ChatEventStepQA,
StepContent,
} from './protocol.js';
import { ChatMessageItemOption } from './protocol.js';

Expand All @@ -28,8 +29,8 @@ export class PeerChatMessageItem extends AIChatMessageItem {
@prop()
reviewingPlanner: string;

@prop()
currentStep = 0;
// @prop()
// currentStep = 0;

@prop()
steps: ChatEventStep[] = [];
Expand All @@ -39,17 +40,27 @@ export class PeerChatMessageItem extends AIChatMessageItem {
@prop()
received = false;

@prop()
planningContent = '';
// @prop()
// planningContent = '';

// @prop()
// executingContent: ChatEventStepQA[] = [];

// @prop()
// expressingContent = '';

// @prop()
// reviewingContent = '';

@prop()
executingContent: ChatEventStepQA[] = [];
currRound: number;

@prop()
expressingContent = '';
roundsContent: StepContent[];

@prop()
reviewingContent = '';
protected currRoundContent: StepContent;
protected _isConstructorInitialized = false;

constructor(
@inject(ChatMessageItemOption) option: ChatMessageItemOption,
Expand All @@ -58,9 +69,35 @@ export class PeerChatMessageItem extends AIChatMessageItem {
) {
super(option, axios, agentManager);
this.agentReady = this.agentDeferred.promise;
this.addEmptyRoundContent(0);
this.initialize();
this._isConstructorInitialized = true;
}

/**
* @param roundStartsAt 当前轮从第几步开始
*/
protected addEmptyRoundContent = (roundStartsAt: number) => {
if (this.currRound === undefined) {
this.currRound = -1;
}

this.currRound += 1;
this.currRoundContent = {
currentStep: 0,
roundStartsAt, // 第一轮肯定从planner开始
planningContent: '',
executingContent: [],
expressingContent: '',
reviewingContent: '',
};

if (!this.roundsContent) {
this.roundsContent = [];
}
this.roundsContent.push(this.currRoundContent);
};

override initialize = async () => {
await this.getAgent();
if (!this.agent) {
Expand All @@ -81,17 +118,17 @@ export class PeerChatMessageItem extends AIChatMessageItem {
const expressing = members[2];
this.expressingPlanner = expressing.id;
const reviewing = members[3];
this.reviewingContent = this.contentMap[this.reviewingPlanner];

this.planningContent = this.contentMap[this.planningPlanner];
this.expressingContent = this.contentMap[this.expressingPlanner];
this.currRoundContent.reviewingContent = this.contentMap[this.reviewingPlanner];
this.currRoundContent.planningContent = this.contentMap[this.planningPlanner];
this.currRoundContent.expressingContent = this.contentMap[this.expressingPlanner];
this.contentMap[this.expressingPlanner] = this._content;
this.reviewingPlanner = reviewing.id;
};

override get content(): string {
if (this.expressingContent) {
return this.expressingContent;
if (this.currRoundContent.expressingContent) {
return this.currRoundContent.expressingContent;
}
if (this.expressingPlanner) {
return this.contentMap[this.expressingPlanner] || '';
Expand All @@ -100,8 +137,12 @@ export class PeerChatMessageItem extends AIChatMessageItem {
}

override set content(v) {
if (this.expressingContent) {
this.expressingContent = v;
if (!this._isConstructorInitialized) {
this._content = v;
return;
}
if (this.currRoundContent.expressingContent) {
this.currRoundContent.expressingContent = v;
}
if (this.expressingPlanner) {
this.contentMap[this.expressingPlanner] = v;
Expand All @@ -110,28 +151,62 @@ export class PeerChatMessageItem extends AIChatMessageItem {
}
}

/**
*
* @param agent_id 当前chunk的agent_id
* 判断当前是不是需要开启新的一轮对话执行。
*/
protected judgeAndAddEmptyRound = (agent_id: string) => {
switch (agent_id) {
case this.planningPlanner:
case this.executingPlanner:
// expressingContent有可能是undefined
if (
this.currRound >= 0 &&
this.roundsContent[this.currRound].expressingContent &&
this.roundsContent[this.currRound].expressingContent !== ''
) {
this.addEmptyRoundContent(agent_id === this.planningPlanner ? 0 : 1);
}
break;
case this.expressingPlanner:
if (
this.currRound >= 0 &&
this.roundsContent[this.currRound].reviewingContent && // 有可能undefined
this.roundsContent[this.currRound].reviewingContent !== ''
) {
this.addEmptyRoundContent(2);
}
break;
case this.reviewingPlanner:
// 一般不会有某一轮对话一上来就是rwviewing
break;
}
};

override appendChunk(e: ChatEventChunk) {
if (this.planningPlanner) {
this.judgeAndAddEmptyRound(e.agent_id);
switch (e.agent_id) {
case this.planningPlanner:
this.planningContent = `${this.planningContent || ''}${e.output || ''}`;
this.currRoundContent.planningContent = `${this.currRoundContent.planningContent || ''}${e.output || ''}`;
try {
const data = JSON.parse(this.planningContent);
this.planningContent = data.thought;
this.planningContent += '\n\n';
this.planningContent += this.toContentStr(
const data = JSON.parse(this.currRoundContent.planningContent);
this.currRoundContent.planningContent = data.thought;
this.currRoundContent.planningContent += '\n\n';
this.currRoundContent.planningContent += this.toContentStr(
data.framework as string | string[],
);
this.currentStep = 1;
this.currRoundContent.currentStep = 1;
} catch (e) {
// console.error(e);
}
break;
case this.expressingPlanner:
this.expressingContent = `${this.expressingContent || ''}${e.output || ''}`;
this.currRoundContent.expressingContent = `${this.currRoundContent.expressingContent || ''}${e.output || ''}`;
break;
case this.reviewingPlanner:
this.reviewingContent = `${this.reviewingContent || ''}${e.output || ''}`;
this.currRoundContent.reviewingContent = `${this.currRoundContent.reviewingContent || ''}${e.output || ''}`;
break;
default:
break;
Expand Down Expand Up @@ -177,8 +252,8 @@ export class PeerChatMessageItem extends AIChatMessageItem {
if (data.agent_id === this.reviewingPlanner) {
eventStep = 3;
}
if (eventStep > this.currentStep) {
this.currentStep = eventStep;
if (eventStep > this.currRoundContent.currentStep) {
this.currRoundContent.currentStep = eventStep;
}

if (e.event === 'result') {
Expand All @@ -196,28 +271,32 @@ export class PeerChatMessageItem extends AIChatMessageItem {

override handleSteps(e: ChatEventStep): void {
let eventStep = 0;

if (e.agent_id === this.planningPlanner) {
eventStep = 1;
}
if (e.agent_id === this.executingPlanner) {
eventStep = 2;
this.executingContent = e.output as ChatEventStepQA[];
this.currRoundContent.executingContent = e.output as ChatEventStepQA[];
}
if (e.agent_id === this.expressingPlanner) {
eventStep = 3;
}
if (e.agent_id === this.reviewingPlanner) {
eventStep = 4;
this.reviewingContent = this.toContentStr(e.output as string | string[]);
this.currRoundContent.reviewingContent = this.toContentStr(
e.output as string | string[],
);
}
if (eventStep > this.currentStep) {
this.currentStep = eventStep;
if (eventStep > this.currRoundContent.currentStep) {
this.currRoundContent.currentStep = eventStep;
}
this.steps[eventStep] = e;
}

override handleResult(e: ChatEventResult): void {
super.handleResult(e);
this.currentStep = 4;
const currRoundContent = this.roundsContent[this.currRound]; // TODO: 具体第几轮
currRoundContent.currentStep = 4;
}
}
9 changes: 9 additions & 0 deletions web-apps/ui/src/modules/chat-message/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export interface MessageCreate {
stream?: boolean;
}

export interface StepContent {
currentStep: number; // 0-4
roundStartsAt: number; // 0-4
planningContent: string;
executingContent: ChatEventStepQA[];
expressingContent: string;
reviewingContent: string;
}

export interface MessageItem {
senderType?: MessageSender;
content: string;
Expand Down
Loading
Loading