The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import {
workspaceAuthContextStorage,
withWorkspaceAuthContext,
} from 'src/engine/core-modules/auth/storage/workspace-auth-context.storage';
import { isUserAuthContext } from 'src/engine/core-modules/auth/guards/is-user-auth-context.guard';
import { Injectable } from '@nestjs/common';
import { PermissionFlagType } from 'twenty-shared/constants';
import { isDefined } from 'twenty-shared/utils';
import { AgentMessageRole } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-message-role.enum';
import { AgentMessageStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-message-status.enum';
import { AgentMessageWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message.workspace-entity';
import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';
import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';
import { AgentChatThreadWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-chat-thread.workspace-entity';
import { AgentChatThreadService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread.service';
import { UserWorkspaceAuthContextService } from 'src/engine/core-modules/user-workspace/services/user-workspace-auth-context.service';
import { PermissionsService } from 'src/engine/permissions/permissions.service';
import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';
import { type AgentChatSender } from 'src/engine/metadata-modules/ai/ai-chat/types/agent-chat-sender.type';
import {
AiException,
AiExceptionCode,
} from 'src/engine/metadata-modules/ai/ai.exception';
@Injectable()
export class AgentChatActorService {
constructor(
@InjectAgentHistoryRepository('agentMessage')
private readonly messages: AgentHistoryRepository<AgentMessageWorkspaceEntity>,
@InjectAgentHistoryRepository('agentChatThread')
private readonly threads: AgentHistoryRepository<AgentChatThreadWorkspaceEntity>,
private readonly threadService: AgentChatThreadService,
private readonly userAuthContextService: UserWorkspaceAuthContextService,
private readonly permissionsService: PermissionsService,
) {}
async resolveMessage({
workspaceId,
threadId,
messageId,
turnId,
}: {
workspaceId: string;
threadId: string;
messageId?: string;
turnId?: string;
}) {
if (!isDefined(messageId) && !isDefined(turnId)) {
throw new AiException(
'Message identity required for execution',
AiExceptionCode.MESSAGE_NOT_FOUND,
);
}
const message = await this.messages.findOne(workspaceId, {
where: {
threadId,
role: AgentMessageRole.USER,
...(isDefined(messageId) ? { id: messageId } : { turnId }),
},
order: { createdAt: 'ASC', id: 'ASC' },
});
if (
isDefined(messageId) &&
(!isDefined(message) || (isDefined(turnId) && message.turnId !== turnId))
) {
throw new AiException(
'Message not found',
AiExceptionCode.MESSAGE_NOT_FOUND,
);
}
// Every member message has its sender since 2.43, so one without a sender
// nor an application was written by a member who has left since
if (
isDefined(message) &&
!isDefined(message.senderWorkspaceMemberId) &&
!isDefined(message.senderApplicationId)
) {
throw new AiException(
'Message sender is no longer a workspace member',
AiExceptionCode.RUN_AS_WORKSPACE_MEMBER_NOT_FOUND,
);
}
// a turn the agent opens has no user message, and an application's message has no member: both run as the
// thread's member, never as the worker's caller or the participant whose turn drained the queue
let workspaceMemberId = message?.senderWorkspaceMemberId;
if (!isDefined(workspaceMemberId)) {
const thread = await this.threads.findOneOrFail(workspaceId, {
where: { id: threadId },
});
if (!isDefined(thread.workspaceMemberId)) {
throw new AiException(
'Message has no sender or workspace member owner',
AiExceptionCode.RUN_AS_WORKSPACE_MEMBER_NOT_FOUND,
);
}
workspaceMemberId = thread.workspaceMemberId;
}
const { userWorkspaceId } =
await this.userAuthContextService.resolveWorkspaceMember({
workspaceId,
workspaceMemberId,
});
const sender: AgentChatSender = {
userWorkspaceId,
applicationId: message?.senderApplicationId ?? null,
};
return { message, sender };
}
async authorize({
workspaceId,
threadId,
sender,
}: {
workspaceId: string;
threadId: string;
sender: AgentChatSender;
}) {
const authContext = await this.userAuthContextService.resolve({
workspaceId,
...sender,
});
const thread = await withWorkspaceAuthContext(authContext, () =>
this.threadService.getWritableThread({
workspaceId,
threadId,
workspaceMemberId: authContext.workspaceMemberId,
}),
);
if (isDefined(thread.deletedAt)) {
throw new AiException(
'Thread is deleted',
AiExceptionCode.THREAD_NOT_FOUND,
);
}
if (
!(await this.permissionsService.userHasWorkspaceSettingPermission({
workspaceId,
userWorkspaceId: sender.userWorkspaceId,
applicationId: sender.applicationId ?? undefined,
agentId: authContext.agentId,
setting: PermissionFlagType.AI,
}))
) {
throw new AiException(
'Chat execution is not permitted',
AiExceptionCode.THREAD_NOT_FOUND,
);
}
if (!(await this.hasRole(authContext))) {
throw new AiException(
'Chat execution is not permitted',
AiExceptionCode.THREAD_NOT_FOUND,
);
}
return { authContext };
}
// a member with no role left cannot act in the chat nor run the calls they approve
async hasRole(authContext: WorkspaceAuthContext): Promise<boolean> {
const roleIds = await this.permissionsService.getRoleIds(authContext);
return roleIds.length > 0;
}
// resolved from the application context of the turn that made the call
async authorizeToolCallResolution({
workspaceId,
threadId,
messageId,
}: {
workspaceId: string;
threadId: string;
messageId: string;
}): Promise<void> {
const toolCallMessage = await this.messages.findOne(workspaceId, {
where: { id: messageId, threadId, role: AgentMessageRole.ASSISTANT },
select: ['turnId'],
});
if (!isDefined(toolCallMessage?.turnId)) {
throw new AiException(
'Tool call turn not found',
AiExceptionCode.MESSAGE_NOT_FOUND,
);
}
const { sender } = await this.resolveMessage({
workspaceId,
threadId,
turnId: toolCallMessage.turnId,
});
const request = workspaceAuthContextStorage.getStore();
if (
!isDefined(request) ||
!isUserAuthContext(request) ||
request.workspace.id !== workspaceId ||
(request.application?.id ?? null) !== sender.applicationId
) {
throw new AiException(
'Resolving this tool call requires the original application context',
AiExceptionCode.TOOL_CALL_RESOLUTION_FORBIDDEN,
);
}
}
async authorizeRetry(args: {
workspaceId: string;
threadId: string;
turnId: string;
userWorkspaceId: string;
}) {
const execution = await this.authorizeJob(args);
const request = workspaceAuthContextStorage.getStore();
if (
isDefined(request) &&
isUserAuthContext(request) &&
(request.workspace.id !== args.workspaceId ||
request.userWorkspaceId !== execution.sender.userWorkspaceId ||
(request.application?.id ?? null) !== execution.sender.applicationId)
) {
throw new AiException(
'Retry requires the original sender context',
AiExceptionCode.MESSAGE_NOT_FOUND,
);
}
return execution;
}
async authorizeJob({
workspaceId,
threadId,
messageId,
turnId,
userWorkspaceId,
}: {
workspaceId: string;
threadId: string;
messageId?: string;
turnId?: string;
userWorkspaceId: string;
}) {
const { message, sender } = await this.resolveMessage({
workspaceId,
threadId,
messageId,
turnId,
});
if (
sender.userWorkspaceId !== userWorkspaceId ||
(isDefined(message) && message.status !== AgentMessageStatus.SENT)
) {
throw new AiException(
'Message sender does not match execution',
AiExceptionCode.MESSAGE_NOT_FOUND,
);
}
const authorization = await this.authorize({
workspaceId,
threadId,
sender,
});
return { message, sender, authorization };
}
}