add workspaceId to indirect entities (#19522)
Required for `workspace:export` command --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
+10
-1
@@ -12,12 +12,21 @@ import {
|
||||
|
||||
import { FileEntity } from 'src/engine/core-modules/file/entities/file.entity';
|
||||
import { AgentMessageEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-message.entity';
|
||||
import type { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
|
||||
@Entity('agentMessagePart')
|
||||
@Entity({ name: 'agentMessagePart', schema: 'core' })
|
||||
export class AgentMessagePartEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
@Index()
|
||||
workspaceId: string;
|
||||
|
||||
@ManyToOne('WorkspaceEntity', { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'workspaceId' })
|
||||
workspace: Relation<WorkspaceEntity>;
|
||||
|
||||
@Column('uuid')
|
||||
@Index()
|
||||
messageId: string;
|
||||
|
||||
+10
-1
@@ -13,6 +13,7 @@ import {
|
||||
import { AgentMessagePartEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-message-part.entity';
|
||||
import { AgentTurnEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-turn.entity';
|
||||
import { AgentChatThreadEntity } from 'src/engine/metadata-modules/ai/ai-chat/entities/agent-chat-thread.entity';
|
||||
import type { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
|
||||
export enum AgentMessageRole {
|
||||
SYSTEM = 'system',
|
||||
@@ -25,11 +26,19 @@ export enum AgentMessageStatus {
|
||||
SENT = 'sent',
|
||||
}
|
||||
|
||||
@Entity('agentMessage')
|
||||
@Entity({ name: 'agentMessage', schema: 'core' })
|
||||
export class AgentMessageEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
@Index()
|
||||
workspaceId: string;
|
||||
|
||||
@ManyToOne('WorkspaceEntity', { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'workspaceId' })
|
||||
workspace: Relation<WorkspaceEntity>;
|
||||
|
||||
@Column('uuid')
|
||||
@Index()
|
||||
threadId: string;
|
||||
|
||||
+10
-1
@@ -13,12 +13,21 @@ import {
|
||||
import { AgentMessageEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-message.entity';
|
||||
import { AgentTurnEvaluationEntity } from 'src/engine/metadata-modules/ai/ai-agent-monitor/entities/agent-turn-evaluation.entity';
|
||||
import { AgentChatThreadEntity } from 'src/engine/metadata-modules/ai/ai-chat/entities/agent-chat-thread.entity';
|
||||
import type { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
|
||||
@Entity('agentTurn')
|
||||
@Entity({ name: 'agentTurn', schema: 'core' })
|
||||
export class AgentTurnEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
@Index()
|
||||
workspaceId: string;
|
||||
|
||||
@ManyToOne('WorkspaceEntity', { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'workspaceId' })
|
||||
workspace: Relation<WorkspaceEntity>;
|
||||
|
||||
@Column('uuid')
|
||||
@Index()
|
||||
threadId: string;
|
||||
|
||||
+2
@@ -13,6 +13,7 @@ const isToolPart = (part: ExtendedUIMessagePart): part is ToolUIPart => {
|
||||
export const mapUIMessagePartsToDBParts = (
|
||||
uiMessageParts: ExtendedUIMessagePart[],
|
||||
messageId: string,
|
||||
workspaceId: string,
|
||||
): Partial<AgentMessagePartEntity>[] => {
|
||||
return uiMessageParts
|
||||
.map((part, index) => {
|
||||
@@ -20,6 +21,7 @@ export const mapUIMessagePartsToDBParts = (
|
||||
messageId,
|
||||
orderIndex: index,
|
||||
type: part.type,
|
||||
workspaceId,
|
||||
};
|
||||
|
||||
switch (part.type) {
|
||||
|
||||
+10
-1
@@ -10,12 +10,21 @@ import {
|
||||
} from 'typeorm';
|
||||
|
||||
import { AgentTurnEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-turn.entity';
|
||||
import type { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
|
||||
@Entity('agentTurnEvaluation')
|
||||
@Entity({ name: 'agentTurnEvaluation', schema: 'core' })
|
||||
export class AgentTurnEvaluationEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
@Index()
|
||||
workspaceId: string;
|
||||
|
||||
@ManyToOne('WorkspaceEntity', { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'workspaceId' })
|
||||
workspace: Relation<WorkspaceEntity>;
|
||||
|
||||
@Column('uuid')
|
||||
@Index()
|
||||
turnId: string;
|
||||
|
||||
+2
@@ -44,6 +44,7 @@ export class RunEvaluationInputJob {
|
||||
role: 'user',
|
||||
parts: [{ type: 'text', text: data.input }],
|
||||
},
|
||||
workspaceId: data.workspaceId,
|
||||
});
|
||||
|
||||
const agent = await this.agentRepository.findOne({
|
||||
@@ -72,6 +73,7 @@ export class RunEvaluationInputJob {
|
||||
},
|
||||
],
|
||||
},
|
||||
workspaceId: data.workspaceId,
|
||||
});
|
||||
|
||||
await this.messageQueueService.add<{
|
||||
|
||||
+2
@@ -68,6 +68,7 @@ export class AgentTurnResolver {
|
||||
): Promise<AgentTurnEntity> {
|
||||
const thread = this.threadRepository.create({
|
||||
userWorkspaceId,
|
||||
workspaceId: workspace.id,
|
||||
title: `Eval: ${input.substring(0, 50)}...`,
|
||||
});
|
||||
const savedThread = await this.threadRepository.save(thread);
|
||||
@@ -75,6 +76,7 @@ export class AgentTurnResolver {
|
||||
const turn = this.turnRepository.create({
|
||||
threadId: savedThread.id,
|
||||
agentId,
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
const savedTurn = await this.turnRepository.save(turn);
|
||||
|
||||
|
||||
+1
@@ -36,6 +36,7 @@ export class AgentTurnGraderService {
|
||||
|
||||
const evaluation = this.evaluationRepository.create({
|
||||
turnId,
|
||||
workspaceId: turn.workspaceId,
|
||||
score,
|
||||
comment,
|
||||
});
|
||||
|
||||
+10
-1
@@ -13,13 +13,22 @@ import {
|
||||
import { UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
|
||||
import { AgentMessageEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-message.entity';
|
||||
import { AgentTurnEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-turn.entity';
|
||||
import type { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { EntityRelation } from 'src/engine/workspace-manager/workspace-migration/types/entity-relation.interface';
|
||||
|
||||
@Entity('agentChatThread')
|
||||
@Entity({ name: 'agentChatThread', schema: 'core' })
|
||||
export class AgentChatThreadEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
@Index()
|
||||
workspaceId: string;
|
||||
|
||||
@ManyToOne('WorkspaceEntity', { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'workspaceId' })
|
||||
workspace: EntityRelation<WorkspaceEntity>;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
@Index()
|
||||
userWorkspaceId: string;
|
||||
|
||||
+5
@@ -144,6 +144,7 @@ export class StreamAgentChatJob {
|
||||
part.type === 'text' || part.type === 'file',
|
||||
),
|
||||
},
|
||||
workspaceId: data.workspaceId,
|
||||
});
|
||||
|
||||
userMessagePromise.catch(() => {});
|
||||
@@ -271,6 +272,7 @@ export class StreamAgentChatJob {
|
||||
await this.handleStreamFinish({
|
||||
responseMessage,
|
||||
threadId: data.threadId,
|
||||
workspaceId: data.workspaceId,
|
||||
streamUsage,
|
||||
lastStepConversationSize,
|
||||
modelConfig,
|
||||
@@ -410,6 +412,7 @@ export class StreamAgentChatJob {
|
||||
private async handleStreamFinish({
|
||||
responseMessage,
|
||||
threadId,
|
||||
workspaceId,
|
||||
streamUsage,
|
||||
lastStepConversationSize,
|
||||
modelConfig,
|
||||
@@ -417,6 +420,7 @@ export class StreamAgentChatJob {
|
||||
}: {
|
||||
responseMessage: Omit<ExtendedUIMessage, 'id'>;
|
||||
threadId: string;
|
||||
workspaceId: string;
|
||||
streamUsage: {
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
@@ -437,6 +441,7 @@ export class StreamAgentChatJob {
|
||||
threadId,
|
||||
uiMessage: responseMessage,
|
||||
turnId: userMessage.turnId ?? undefined,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
await this.threadRepository.update(threadId, {
|
||||
|
||||
+1
@@ -174,6 +174,7 @@ export class AgentChatResolver {
|
||||
threadId,
|
||||
text,
|
||||
id: messageId,
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
|
||||
await this.eventPublisherService.publish({
|
||||
|
||||
+2
@@ -77,6 +77,7 @@ export class AgentChatStreamingService {
|
||||
role: AgentMessageRole.USER,
|
||||
parts: [{ type: 'text' as const, text }],
|
||||
},
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
|
||||
const previousMessages = await this.loadMessagesFromDB(
|
||||
@@ -138,6 +139,7 @@ export class AgentChatStreamingService {
|
||||
const turnId = await this.agentChatService.promoteQueuedMessage(
|
||||
nextQueued.id,
|
||||
threadId,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
if (turnId === null) {
|
||||
|
||||
+12
@@ -60,6 +60,7 @@ export class AgentChatService {
|
||||
}) {
|
||||
const thread = this.threadRepository.create({
|
||||
userWorkspaceId,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
const savedThread = await this.threadRepository.save(thread);
|
||||
@@ -105,6 +106,7 @@ export class AgentChatService {
|
||||
agentId,
|
||||
turnId,
|
||||
id,
|
||||
workspaceId,
|
||||
}: {
|
||||
threadId: string;
|
||||
uiMessage: Omit<ExtendedUIMessage, 'id'>;
|
||||
@@ -112,6 +114,7 @@ export class AgentChatService {
|
||||
agentId?: string;
|
||||
turnId?: string;
|
||||
id?: string;
|
||||
workspaceId: string;
|
||||
}) {
|
||||
let actualTurnId = turnId;
|
||||
|
||||
@@ -119,6 +122,7 @@ export class AgentChatService {
|
||||
const turn = this.turnRepository.create({
|
||||
threadId,
|
||||
agentId: agentId ?? null,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
const savedTurn = await this.turnRepository.save(turn);
|
||||
@@ -133,6 +137,7 @@ export class AgentChatService {
|
||||
role: uiMessage.role as AgentMessageRole,
|
||||
agentId: agentId ?? null,
|
||||
processedAt: new Date(),
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
const savedMessage = await this.messageRepository.save(message);
|
||||
@@ -141,6 +146,7 @@ export class AgentChatService {
|
||||
const dbParts = mapUIMessagePartsToDBParts(
|
||||
uiMessage.parts,
|
||||
savedMessage.id,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
await this.messagePartRepository.save(dbParts);
|
||||
@@ -175,10 +181,12 @@ export class AgentChatService {
|
||||
threadId,
|
||||
text,
|
||||
id,
|
||||
workspaceId,
|
||||
}: {
|
||||
threadId: string;
|
||||
text: string;
|
||||
id?: string;
|
||||
workspaceId: string;
|
||||
}): Promise<AgentMessageEntity> {
|
||||
const message = this.messageRepository.create({
|
||||
...(id ? { id } : {}),
|
||||
@@ -187,6 +195,7 @@ export class AgentChatService {
|
||||
role: AgentMessageRole.USER,
|
||||
agentId: null,
|
||||
status: AgentMessageStatus.QUEUED,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
const savedMessage = await this.messageRepository.save(message);
|
||||
@@ -196,6 +205,7 @@ export class AgentChatService {
|
||||
orderIndex: 0,
|
||||
type: 'text',
|
||||
textContent: text,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
await this.messagePartRepository.save(part);
|
||||
@@ -234,10 +244,12 @@ export class AgentChatService {
|
||||
async promoteQueuedMessage(
|
||||
messageId: string,
|
||||
threadId: string,
|
||||
workspaceId: string,
|
||||
): Promise<string | null> {
|
||||
const turn = this.turnRepository.create({
|
||||
threadId,
|
||||
agentId: null,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
const savedTurn = await this.turnRepository.save(turn);
|
||||
|
||||
Reference in New Issue
Block a user