Skip to content

Commit

Permalink
refactor(app): agent config
Browse files Browse the repository at this point in the history
  • Loading branch information
BroKun committed Jun 16, 2024
1 parent e7287ca commit e627b78
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 49 deletions.
39 changes: 21 additions & 18 deletions web/app/src/modules/agent-bot/agent-bot.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { inject, prop, transient } from '@difizen/mana-app';
import qs from 'query-string';

import { AsyncModel } from '../../common/async-model.js';
import { AxiosClient } from '../axios-client/index.js';
import { UserManager } from '../user/index.js';

import { AgentConfigManager } from './agent-config-manager.js';
import type { AgentConfig } from './agent-config.js';
import type { AgentConfigOption } from './protocol.js';
import { AgentBotOption, AgentBotType } from './protocol.js';

@transient()
Expand All @@ -24,9 +23,7 @@ export class AgentBot extends AsyncModel<AgentBot, AgentBotOption> {
@prop()
draft?: AgentConfig;

draftConfigId?: number;

option: any;
option: AgentBotOption;

constructor(
@inject(AgentBotOption) option: AgentBotOption,
Expand All @@ -40,32 +37,29 @@ export class AgentBot extends AsyncModel<AgentBot, AgentBotOption> {

this.id = option.id;
this.initialize(option);
this.ensureDraft();
this.ensureDraft(option);
}

shouldInitFromMeta(option: AgentBotOption): boolean {
return AgentBotType.isFullOption(option);
}

async ensureDraft(): Promise<AgentConfig> {
async ensureDraft(option: AgentBotOption): Promise<AgentConfig | undefined> {
await this.ready;
let config: AgentConfig;
if (this.draftConfigId) {
config = await this.configManager.getDraft({ id: this.draftConfigId });
} else {
config = await this.configManager.create();
this.draftConfigId = config.id;
await this.save();
let draftConfig = option.draft;
if (!this.option.draft) {
draftConfig = await this.fetchDraftInfo(option);
}
if (draftConfig) {
this.draft = this.configManager.create(draftConfig);
}
this.draft = config;
return config;
return this.draft;
}

protected override fromMeta(option: AgentBotOption) {
this.id = option.id;
this.name = option.name!;
this.avatar = option.avatar!;
this.draftConfigId = option.draft;
super.fromMeta(option);
}

Expand All @@ -78,10 +72,19 @@ export class AgentBot extends AsyncModel<AgentBot, AgentBotOption> {
}
}

async fetchDraftInfo(option: AgentBotOption) {
const res = await this.axios.get<AgentConfigOption>(
`api/v1/agent/bots/${option.id}/draft`,
);
if (res.status === 200) {
return res.data;
}
return undefined;
}

toMeta(): AgentBotOption {
return {
id: this.id,
draft: this.draftConfigId,
name: this.name,
avatar: this.avatar,
};
Expand Down
8 changes: 2 additions & 6 deletions web/app/src/modules/agent-bot/agent-config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ export class AgentConfigManager {
return this.configFactory(option);
};

create = async (): Promise<AgentConfig> => {
const res = await this.axios.post<AgentConfigOption>(`api/v1/agent/configs`);
if (res.status !== 200) {
throw new Error('failed to create agent config');
}
return this.configFactory(res.data);
create = (option: AgentConfigOption): AgentConfig => {
return this.configFactory(option);
};
}
20 changes: 9 additions & 11 deletions web/app/src/modules/agent-bot/agent-config.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { inject, prop, transient } from '@difizen/mana-app';
import qs from 'query-string';

import { AsyncModel } from '../../common/async-model.js';
import { AxiosClient } from '../axios-client/index.js';
import { UserManager } from '../user/index.js';

import type {
AgentConfigMeta,
AgentConfigModelMeta,
AgentConfigStatus,
} from './protocol.js';
import type { AgentConfigInfo, AgentConfigModelMeta } from './protocol.js';
import { AgentConfigOption, AgentConfigType } from './protocol.js';

@transient()
export class AgentConfig extends AsyncModel<AgentConfig, AgentConfigOption> {
protected axios: AxiosClient;
id: number;

status: AgentConfigStatus = 'draft';
bot_id: number;

is_draft = true;

@prop()
persona?: string;
Expand All @@ -26,7 +22,7 @@ export class AgentConfig extends AsyncModel<AgentConfig, AgentConfigOption> {
model?: AgentConfigModelMeta;

@prop()
config?: AgentConfigMeta;
config?: AgentConfigInfo;

option?: AgentConfigOption;

Expand All @@ -48,7 +44,8 @@ export class AgentConfig extends AsyncModel<AgentConfig, AgentConfigOption> {

protected override fromMeta(option: AgentConfigOption): void {
this.id = option.id;
this.status = option.status || 'draft';
this.bot_id = option.bot_id;
this.is_draft = option.is_draft || true;
this.persona = option.config?.persona;
this.model = option.config?.model;
// TODO: tools & datasets
Expand All @@ -69,7 +66,8 @@ export class AgentConfig extends AsyncModel<AgentConfig, AgentConfigOption> {
toMeta(): AgentConfigOption {
return {
id: this.id,
status: this.status,
bot_id: this.bot_id,
is_draft: this.is_draft,
config: {
...(this.config || {}),
persona: this.persona,
Expand Down
13 changes: 6 additions & 7 deletions web/app/src/modules/agent-bot/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface AgentConfigModelMeta {
key: string;
[key: string]: any;
}
export interface AgentConfigMeta {
export interface AgentConfigInfo {
persona: string;
tools: AgentConfigToolMeta[];
datasets: AgentConfigDatasetMeta[];
Expand All @@ -31,8 +31,9 @@ export const AgentConfigOption = Syringe.defineToken('AgentConfigOption', {

export interface AgentConfigOption {
id: number;
status?: AgentConfigStatus;
config?: Partial<AgentConfigMeta>;
bot_id: number;
is_draft?: boolean;
config?: Partial<AgentConfigInfo>;
}

export type AgentConfigFactory = (options: AgentConfigOption) => AgentConfig;
Expand All @@ -45,7 +46,7 @@ export const AgentConfigType = {
return !!(data && 'id' in data);
},
isFullOption(data?: Record<string, any>): boolean {
return AgentBotType.isOption(data) && 'config' in data;
return AgentBotType.isOption(data) && 'bot_id' in data && 'config' in data;
},
};

Expand All @@ -58,7 +59,7 @@ export interface AgentBotMeta {

export interface AgentBotOption extends Partial<AgentBotMeta> {
id: number;
draft?: number;
draft?: AgentConfigOption | null;
}

export const AgentBotOption = Syringe.defineToken('AgentBotOption', {
Expand All @@ -78,5 +79,3 @@ export const AgentBotType = {
return AgentBotType.isOption(data) && 'name' in data && 'avatar' in data;
},
};

export type AgentConfigStatus = 'draft' | 'publish';
10 changes: 6 additions & 4 deletions web/app/src/modules/axios-client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { Syringe } from '@difizen/mana-app';
import axios from 'axios';
import qs from 'query-string';

import { UserManager } from '../user/user-manager.js';

Expand All @@ -10,13 +11,14 @@ export const getContextClient = (ctx: Syringe.Context) => {
axios.interceptors.request.use(async function (config) {
// TODO: change to jwt token
if (config.url) {
const url = new URL(config.url);
if (!url.searchParams.has('user_id')) {
const parsed = qs.parseUrl(config.url);
const query = parsed.query;
if (!query['user_id']) {
const userManager = ctx.container.get(UserManager);
await userManager.initialized;
if (userManager.current) {
url.searchParams.append('user_id', userManager.current.id);
config.url = url.toString();
query['user_id'] = userManager.current.id;
config.url = qs.stringifyUrl(parsed);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/app/src/modules/model/built-in-model-meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ModelMeta } from './protocol';
import type { ModelMeta } from './protocol.js';

const gpt4: ModelMeta = {
key: 'gpt-4',
Expand Down
2 changes: 1 addition & 1 deletion web/app/src/modules/model/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, prop, transient } from '@difizen/mana-app';

import { ModelOption, type ModelConfigMeta, type ModelMeta } from './protocol';
import { ModelOption, type ModelConfigMeta, type ModelMeta } from './protocol.js';

@transient()
export class Model {
Expand Down
2 changes: 1 addition & 1 deletion web/app/src/modules/model/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Syringe } from '@difizen/mana-app';

import type { Model } from './model';
import type { Model } from './model.js';

export interface ModelMeta {
key: string;
Expand Down

0 comments on commit e627b78

Please sign in to comment.