Skip to content

Commit

Permalink
feat: peer多智能体组多轮对话的展示;
Browse files Browse the repository at this point in the history
  • Loading branch information
xujingli authored and BroKun committed Sep 4, 2024
1 parent 1120fdf commit 8e9c116
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 71 deletions.
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

0 comments on commit 8e9c116

Please sign in to comment.