/
githubmirror
/
ai-legion
Обзор
Документация
Войти
/
githubmirror
/
ai-legion
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/message.ts
75 строк
2 KB
eumemic
refactor message handling to remove system role and add support for new GPT_O1_PREVIEW model
01 фев 2025, 02:04
01 фев 2025, 02:04
7c72e43
Код
Авторство
О чём код?
export interface Message { type: MessageType; source: MessageSource; targetAgentIds: string[]; content: string; } type TypelessMessage = Omit<Message, "type">; export type MessageType = keyof typeof messageBuilder; export type MessageSource = SystemMessageSource | AgentMessageSource; interface MessageSourceBase { id?: string; } interface SystemMessageSource extends MessageSourceBase { type: "user"; id?: undefined; } interface AgentMessageSource extends MessageSourceBase { type: "agent"; id: string; } export const systemSource: SystemMessageSource = { type: "user" }; export const agentSource = (id: string): AgentMessageSource => ({ type: "agent", id, }); export const CODE_BLOCK_DELIMITER = "```"; export const messageBuilder = addMessageTypes({ spontaneous: singleTargetSystemMessage, ok: singleTargetSystemMessage, error: singleTargetSystemMessage, agentToAgent: ( sourceAgentId: string, targetAgentIds: string[], content: string ) => ({ source: agentSource(sourceAgentId), targetAgentIds, content, }), }); function addMessageTypes< T extends Record<string, (...args: any) => TypelessMessage> >(record: T): { [K in keyof T]: (...args: Parameters<T[K]>) => Message } { for (const [type, builder] of Object.entries(record)) { (record as any)[type] = (...args: any) => ({ type, ...(builder as any)(...args), }); } return record as any; } function singleTargetSystemMessage( agentId: string, content: string ): TypelessMessage { return { source: systemSource, targetAgentIds: [agentId], content, }; }