diff --git a/front/admin/db.ts b/front/admin/db.ts index 6cbe77c21217..9fd574e5d253 100644 --- a/front/admin/db.ts +++ b/front/admin/db.ts @@ -1,13 +1,11 @@ import { + AgentMessage, App, - AssistantAgentMessage, - AssistantConversation, - AssistantMessage, - AssistantUserMessage, ChatMessage, ChatRetrievedDocument, ChatSession, Clone, + Conversation, Dataset, DataSource, DocumentTrackerChangeSuggestion, @@ -17,10 +15,12 @@ import { Key, Membership, MembershipInvitation, + Message, Provider, Run, TrackedDocument, User, + UserMessage, UserMetadata, Workspace, XP1Run, @@ -49,10 +49,10 @@ async function main() { await ExtractedEvent.sync({ alter: true }); await DocumentTrackerChangeSuggestion.sync({ alter: true }); - await AssistantConversation.sync({ alter: true }); - await AssistantUserMessage.sync({ alter: true }); - await AssistantAgentMessage.sync({ alter: true }); - await AssistantMessage.sync({ alter: true }); + await Conversation.sync({ alter: true }); + await UserMessage.sync({ alter: true }); + await AgentMessage.sync({ alter: true }); + await Message.sync({ alter: true }); await XP1User.sync({ alter: true }); await XP1Run.sync({ alter: true }); diff --git a/front/lib/api/assistant/actions/retrieval.ts b/front/lib/api/assistant/actions/retrieval.ts index b1464e446928..922b1fc17f71 100644 --- a/front/lib/api/assistant/actions/retrieval.ts +++ b/front/lib/api/assistant/actions/retrieval.ts @@ -23,9 +23,9 @@ import { AgentConfigurationType, } from "@app/types/assistant/agent"; import { - AssistantAgentMessageType, - AssistantConversationType, - AssistantUserMessageType, + AgentMessageType, + ConversationType, + UserMessageType, } from "@app/types/assistant/conversation"; /** @@ -174,8 +174,8 @@ export async function retrievalActionSpecification( export async function generateRetrievalParams( auth: Authenticator, configuration: RetrievalConfigurationType, - conversation: AssistantConversationType, - userMessage: AssistantUserMessageType + conversation: ConversationType, + userMessage: UserMessageType ): Promise< Result< { query: string | null; relativeTimeFrame: TimeFrame | null; topK: number }, @@ -299,15 +299,15 @@ export type RetrievalSuccessEvent = { action: RetrievalActionType; }; -// This method is in charge of running the retrieval and creating an AssistantAgentRetrieval DB +// This method is in charge of running the retrieval and creating an AgentRetrieval DB // object in the database (along with the RetrievedDocument objects). It does not create any generic // model related to the conversation. export async function* runRetrieval( auth: Authenticator, configuration: AgentConfigurationType, - conversation: AssistantConversationType, - userMessage: AssistantUserMessageType, - agentMessage: AssistantAgentMessageType + conversation: ConversationType, + userMessage: UserMessageType, + agentMessage: AgentMessageType ): AsyncGenerator< | RetrievalParamsEvent | RetrievalDocumentsEvent diff --git a/front/lib/api/assistant/agent.ts b/front/lib/api/assistant/agent.ts index 45088fe6875d..2e6fbfa5e943 100644 --- a/front/lib/api/assistant/agent.ts +++ b/front/lib/api/assistant/agent.ts @@ -14,9 +14,9 @@ import { AgentMessageConfigurationType, } from "@app/types/assistant/agent"; import { - AssistantAgentActionType, - AssistantAgentMessageType, - AssistantConversationType, + AgentActionType, + AgentMessageType, + ConversationType, } from "@app/types/assistant/conversation"; import { @@ -88,7 +88,7 @@ export async function updateAgentConfiguration( export async function generateActionInputs( auth: Authenticator, specification: AgentActionSpecification, - conversation: AssistantConversationType + conversation: ConversationType ): Promise, Error>> { const model = { providerId: "openai", @@ -160,7 +160,7 @@ export type AgentMessageNewEvent = { type: "agent_message_new"; created: number; configurationId: string; - message: AssistantAgentMessageType; + message: AgentMessageType; }; // Generic event sent when an error occured (whether it's during the action or the message generation). @@ -184,7 +184,7 @@ export type AgentActionSuccessEvent = { created: number; configurationId: string; messageId: string; - action: AssistantAgentActionType; + action: AgentActionType; }; // Event sent when tokens are streamed as the the agent is generating a message. @@ -202,16 +202,16 @@ export type AgentMessageSuccessEvent = { created: number; configurationId: string; messageId: string; - message: AssistantAgentMessageType; + message: AgentMessageType; }; -// This interface is used to execute an agent. It is in charge of creating the AssistantAgentMessage +// This interface is used to execute an agent. It is in charge of creating the AgentMessage // object in database (fully completed or with error set if an error occured). It is called to run // an agent or when retrying a previous agent interaction. export async function* runAgent( auth: Authenticator, configuration: AgentConfigurationType, - conversation: AssistantConversationType + conversation: ConversationType ): AsyncGenerator< | AgentMessageNewEvent | AgentErrorEvent diff --git a/front/lib/api/assistant/conversation.ts b/front/lib/api/assistant/conversation.ts index 6f01a1ba04a3..9d5f1384871a 100644 --- a/front/lib/api/assistant/conversation.ts +++ b/front/lib/api/assistant/conversation.ts @@ -9,24 +9,20 @@ import { import { Authenticator } from "@app/lib/auth"; import { CoreAPI } from "@app/lib/core_api"; import { front_sequelize } from "@app/lib/databases"; -import { - AssistantAgentMessage, - AssistantMessage, - AssistantUserMessage, -} from "@app/lib/models"; +import { AgentMessage, Message, UserMessage } from "@app/lib/models"; import { Err, Ok, Result } from "@app/lib/result"; import { generateModelSId } from "@app/lib/utils"; import logger from "@app/logger/logger"; import { isRetrievalActionType } from "@app/types/assistant/actions/retrieval"; import { - AssistantAgentMessageType, - AssistantConversationType, - AssistantMention, - AssistantUserMessageContext, - AssistantUserMessageType, + AgentMessageType, + ConversationType, + isAgentMention, isAgentMessageType, - isAssistantAgentMention, isUserMessageType, + Mention, + UserMessageContext, + UserMessageType, } from "@app/types/assistant/conversation"; import { renderRetrievalActionForModel } from "./actions/retrieval"; @@ -52,7 +48,7 @@ export async function renderConversationForModel({ model, allowedTokenCount, }: { - conversation: AssistantConversationType; + conversation: ConversationType; model: { providerId: string; modelId: string }; allowedTokenCount: number; }): Promise> { @@ -156,7 +152,7 @@ export async function renderConversationForModel({ // Event sent when the user message is created. export type UserMessageNewEvent = { type: "user_message_new"; - message: AssistantUserMessageType; + message: UserMessageType; }; // This method is in charge of creating a new user message in database, running the necessary agents @@ -169,10 +165,10 @@ export async function* postUserMessage( mentions, context, }: { - conversation: AssistantConversationType; + conversation: ConversationType; message: string; - mentions: AssistantMention[]; - context: AssistantUserMessageContext; + mentions: Mention[]; + context: UserMessageContext; } ): AsyncGenerator< | UserMessageNewEvent @@ -185,26 +181,26 @@ export async function* postUserMessage( > { const user = auth.user(); - let userMessage: AssistantUserMessageType | null = null; - const agentMessages: AssistantAgentMessageType[] = []; + let userMessage: UserMessageType | null = null; + const agentMessages: AgentMessageType[] = []; await front_sequelize.transaction(async (t) => { let nextMessageRank = - ((await AssistantMessage.max("rank", { + ((await Message.max("rank", { where: { - assistantConversationId: conversation.id, + conversationId: conversation.id, }, transaction: t, })) ?? -1) + 1; - const userMessageRow = await AssistantMessage.create( + const userMessageRow = await Message.create( { sId: generateModelSId(), rank: nextMessageRank++, - assistantConversationId: conversation.id, + conversationId: conversation.id, parentId: null, - assistantUserMessageId: ( - await AssistantUserMessage.create( + userMessageId: ( + await UserMessage.create( { message: message, userContextUsername: context.username, @@ -237,15 +233,15 @@ export async function* postUserMessage( // for each assistant mention, create an "empty" agent message for (const m of mentions) { - if (isAssistantAgentMention(m)) { - const agentMessageRow = await AssistantMessage.create( + if (isAgentMention(m)) { + const agentMessageRow = await Message.create( { sId: generateModelSId(), rank: nextMessageRank++, - assistantConversationId: conversation.id, + conversationId: conversation.id, parentId: userMessage.id, - assistantAgentMessageId: ( - await AssistantAgentMessage.create({}, { transaction: t }) + agentMessageId: ( + await AgentMessage.create({}, { transaction: t }) ).id, }, { @@ -309,15 +305,15 @@ export async function* postUserMessage( } // This method is in charge of re-running an agent interaction (generating a new -// AssistantAgentMessage as a result) +// AgentMessage as a result) export async function* retryAgentMessage( auth: Authenticator, { conversation, message, }: { - conversation: AssistantConversationType; - message: AssistantAgentMessageType; + conversation: ConversationType; + message: AgentMessageType; } ): AsyncGenerator< | AgentMessageNewEvent @@ -348,8 +344,8 @@ export async function* editUserMessage( message, content, }: { - conversation: AssistantConversationType; - message: AssistantUserMessageType; + conversation: ConversationType; + message: UserMessageType; content: string; } ): AsyncGenerator< diff --git a/front/lib/models/assistant/conversation.ts b/front/lib/models/assistant/conversation.ts index bd8fea094e78..a30c377e3ffd 100644 --- a/front/lib/models/assistant/conversation.ts +++ b/front/lib/models/assistant/conversation.ts @@ -10,23 +10,23 @@ import { import { front_sequelize } from "@app/lib/databases"; import { User } from "@app/lib/models/user"; import { - AssistantAgentMessageStatus, - AssistantConversationVisibility, - AssistantMessageVisibility, + AgentMessageStatus, + ConversationVisibility, + MessageVisibility, } from "@app/types/assistant/conversation"; -export class AssistantConversation extends Model< - InferAttributes, - InferCreationAttributes +export class Conversation extends Model< + InferAttributes, + InferCreationAttributes > { declare id: CreationOptional; declare sId: string; declare title: string | null; declare created: Date; - declare visibility: CreationOptional; + declare visibility: CreationOptional; } -AssistantConversation.init( +Conversation.init( { id: { type: DataTypes.INTEGER, @@ -53,14 +53,14 @@ AssistantConversation.init( }, }, { - modelName: "assistant_conversation", + modelName: "conversation", sequelize: front_sequelize, } ); -export class AssistantUserMessage extends Model< - InferAttributes, - InferCreationAttributes +export class UserMessage extends Model< + InferAttributes, + InferCreationAttributes > { declare id: CreationOptional; @@ -75,7 +75,7 @@ export class AssistantUserMessage extends Model< declare userId: ForeignKey | null; } -AssistantUserMessage.init( +UserMessage.init( { id: { type: DataTypes.INTEGER, @@ -108,29 +108,29 @@ AssistantUserMessage.init( }, }, { - modelName: "assistant_user_message", + modelName: "user_message", sequelize: front_sequelize, } ); -User.hasMany(AssistantUserMessage, { +User.hasMany(UserMessage, { foreignKey: { name: "userId" }, }); -export class AssistantAgentMessage extends Model< - InferAttributes, - InferCreationAttributes +export class AgentMessage extends Model< + InferAttributes, + InferCreationAttributes > { declare id: CreationOptional; - declare status: CreationOptional; + declare status: CreationOptional; declare message: string | null; declare errorCode: string | null; declare errorMessage: string | null; } -AssistantAgentMessage.init( +AgentMessage.init( { id: { type: DataTypes.INTEGER, @@ -156,32 +156,30 @@ AssistantAgentMessage.init( }, }, { - modelName: "assistant_agent_message", + modelName: "agent_message", sequelize: front_sequelize, } ); -export class AssistantMessage extends Model< - InferAttributes, - InferCreationAttributes +export class Message extends Model< + InferAttributes, + InferCreationAttributes > { declare id: CreationOptional; declare sId: string; declare version: CreationOptional; declare rank: number; - declare visibility: CreationOptional; + declare visibility: CreationOptional; - declare assistantConversationId: ForeignKey; + declare conversationId: ForeignKey; - declare parentId: ForeignKey | null; - declare assistantUserMessageId: ForeignKey | null; - declare assistantAgentMessageId: ForeignKey< - AssistantAgentMessage["id"] - > | null; + declare parentId: ForeignKey | null; + declare userMessageId: ForeignKey | null; + declare agentMessageId: ForeignKey | null; } -AssistantMessage.init( +Message.init( { id: { type: DataTypes.INTEGER, @@ -209,37 +207,37 @@ AssistantMessage.init( }, }, { - modelName: "assistant_message", + modelName: "message", sequelize: front_sequelize, indexes: [ { unique: true, - fields: ["version", "assistantConversationId", "rank"], + fields: ["version", "conversationId", "rank"], }, ], hooks: { beforeValidate: (message) => { - if ( - !message.assistantUserMessageId === !message.assistantAgentMessageId - ) { + if (!message.userMessageId === !message.agentMessageId) { throw new Error( - "Exactly one of assistantUserMessageId, assistantAgentMessageId must be non-null" + "Exactly one of userMessageId, agentMessageId must be non-null" ); } }, }, } ); -AssistantConversation.hasMany(AssistantMessage, { - foreignKey: { name: "assistantConversationId", allowNull: false }, +Conversation.hasMany(Message, { + foreignKey: { name: "conversationId", allowNull: false }, onDelete: "CASCADE", }); -AssistantUserMessage.hasOne(AssistantMessage, { - foreignKey: "assistantUserMessageId", +UserMessage.hasOne(Message, { + foreignKey: "userMessageId", + as: "_message", }); -AssistantAgentMessage.hasOne(AssistantMessage, { - foreignKey: "assistantAgentMessageId", +AgentMessage.hasOne(Message, { + foreignKey: "agentMessageId", + as: "_message", }); -AssistantMessage.belongsTo(AssistantMessage, { +Message.belongsTo(Message, { foreignKey: "parentId", }); diff --git a/front/lib/models/index.ts b/front/lib/models/index.ts index 98b8170938a6..6d5a9ad276d7 100644 --- a/front/lib/models/index.ts +++ b/front/lib/models/index.ts @@ -1,9 +1,9 @@ import { App, Clone, Dataset, Provider, Run } from "@app/lib/models/apps"; import { - AssistantAgentMessage, - AssistantConversation, - AssistantMessage, - AssistantUserMessage, + AgentMessage, + Conversation, + Message, + UserMessage, } from "@app/lib/models/assistant/conversation"; import { ChatMessage, @@ -27,15 +27,13 @@ import { import { XP1Run, XP1User } from "@app/lib/models/xp1"; export { + AgentMessage, App, - AssistantAgentMessage, - AssistantConversation, - AssistantMessage, - AssistantUserMessage, ChatMessage, ChatRetrievedDocument, ChatSession, Clone, + Conversation, Dataset, DataSource, DocumentTrackerChangeSuggestion, @@ -45,10 +43,12 @@ export { Key, Membership, MembershipInvitation, + Message, Provider, Run, TrackedDocument, User, + UserMessage, UserMetadata, Workspace, XP1Run, diff --git a/front/types/assistant/actions/retrieval.ts b/front/types/assistant/actions/retrieval.ts index 77191f587ce6..8117c070c9ee 100644 --- a/front/types/assistant/actions/retrieval.ts +++ b/front/types/assistant/actions/retrieval.ts @@ -4,7 +4,7 @@ import { ModelId } from "@app/lib/databases"; import { AgentActionConfigurationType } from "@app/types/assistant/agent"; -import { AssistantAgentActionType } from "@app/types/assistant/conversation"; +import { AgentActionType } from "@app/types/assistant/conversation"; export type TimeFrame = { count: number; @@ -76,13 +76,13 @@ export type RetrievalDocumentType = { }; export function isRetrievalActionType( - arg: AssistantAgentActionType + arg: AgentActionType ): arg is RetrievalActionType { return arg.type === "retrieval_action"; } export type RetrievalActionType = { - id: ModelId; // AssistantAgentRetrieval. + id: ModelId; // AgentRetrieval. type: "retrieval_action"; params: { dataSources: "all" | DataSourceConfiguration[]; diff --git a/front/types/assistant/conversation.ts b/front/types/assistant/conversation.ts index 9925ac85e27c..a438d4abb3b6 100644 --- a/front/types/assistant/conversation.ts +++ b/front/types/assistant/conversation.ts @@ -8,29 +8,25 @@ import { AgentConfigurationType } from "./agent"; * Mentions */ -export type AssistantAgentMention = { +export type AgentMention = { configurationId: string; }; -export type AssistantUserMention = { +export type UserMention = { provider: string; providerId: string; }; -export type AssistantMention = AssistantAgentMention | AssistantUserMention; +export type Mention = AgentMention | UserMention; -export type AssistantMessageVisibility = "visible" | "deleted"; +export type MessageVisibility = "visible" | "deleted"; -export function isAssistantAgentMention( - arg: AssistantMention -): arg is AssistantAgentMention { - return (arg as AssistantAgentMention).configurationId !== undefined; +export function isAgentMention(arg: Mention): arg is AgentMention { + return (arg as AgentMention).configurationId !== undefined; } -export function isAssistantUserMention( - arg: AssistantMention -): arg is AssistantUserMention { - const maybeUserMention = arg as AssistantUserMention; +export function isUserMention(arg: Mention): arg is UserMention { + const maybeUserMention = arg as UserMention; return ( maybeUserMention.provider !== undefined && maybeUserMention.providerId !== undefined @@ -41,7 +37,7 @@ export function isAssistantUserMention( * User messages */ -export type AssistantUserMessageContext = { +export type UserMessageContext = { username: string; timezone: string; fullName: string | null; @@ -49,21 +45,21 @@ export type AssistantUserMessageContext = { profilePictureUrl: string | null; }; -export type AssistantUserMessageType = { +export type UserMessageType = { id: ModelId; type: "user_message"; sId: string; - visibility: AssistantMessageVisibility; + visibility: MessageVisibility; version: number; user: UserType | null; - mentions: AssistantMention[]; + mentions: Mention[]; message: string; - context: AssistantUserMessageContext; + context: UserMessageContext; }; export function isUserMessageType( - arg: AssistantUserMessageType | AssistantAgentMessageType -): arg is AssistantUserMessageType { + arg: UserMessageType | AgentMessageType +): arg is UserMessageType { return arg.type === "user_message"; } @@ -71,15 +67,15 @@ export function isUserMessageType( * Agent messages */ -export type AssistantUserFeedbackType = { +export type UserFeedbackType = { user: UserType; value: "positive" | "negative" | null; comment: string | null; }; -export type AssistantAgentActionType = RetrievalActionType; +export type AgentActionType = RetrievalActionType; -export type AssistantAgentMessageStatus = +export type AgentMessageStatus = | "created" | "action_running" | "writing" @@ -93,19 +89,19 @@ export type AssistantAgentMessageStatus = * them together in case of error of either. We store an error only here whether it's an error * coming from the action or from the message generation. */ -export type AssistantAgentMessageType = { +export type AgentMessageType = { id: ModelId; type: "agent_message"; sId: string; - visibility: AssistantMessageVisibility; + visibility: MessageVisibility; version: number; parentMessageId: string | null; configuration: AgentConfigurationType; - status: AssistantAgentMessageStatus; - action: AssistantAgentActionType | null; + status: AgentMessageStatus; + action: AgentActionType | null; message: string | null; - feedbacks: AssistantUserFeedbackType[]; + feedbacks: UserFeedbackType[]; error: { code: string; message: string; @@ -113,8 +109,8 @@ export type AssistantAgentMessageType = { }; export function isAgentMessageType( - arg: AssistantUserMessageType | AssistantAgentMessageType -): arg is AssistantAgentMessageType { + arg: UserMessageType | AgentMessageType +): arg is AgentMessageType { return arg.type === "agent_message"; } @@ -122,17 +118,17 @@ export function isAgentMessageType( * Conversations */ -export type AssistantConversationVisibility = "private" | "workspace"; +export type ConversationVisibility = "private" | "workspace"; /** * content [][] structure is intended to allow retries (of agent messages) or edits (of user * messages). */ -export type AssistantConversationType = { +export type ConversationType = { id: ModelId; created: number; sId: string; title: string | null; - content: (AssistantUserMessageType[] | AssistantAgentMessageType[])[]; - visibility: AssistantConversationVisibility; + content: (UserMessageType[] | AgentMessageType[])[]; + visibility: ConversationVisibility; };