2026-05-20 22:43:31 +08:00
|
|
|
import { agentService } from '@/services/agent/agent'
|
|
|
|
|
import type { AutofillField } from '@/agent'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { prisma } from '@/db'
|
|
|
|
|
|
|
|
|
|
export interface AutofillAiParams {
|
|
|
|
|
teamId: string
|
|
|
|
|
images: string[]
|
|
|
|
|
fields: AutofillField[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function autofillAiActivity(params: AutofillAiParams) {
|
|
|
|
|
const prompt = 'Analyze the provided images and extract metadata.'
|
2026-05-20 22:43:31 +08:00
|
|
|
return agentService.autofill(params.teamId, prompt, params.images, params.fields)
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AiChatParams {
|
|
|
|
|
teamId: string
|
|
|
|
|
agentId: string
|
|
|
|
|
message: string
|
|
|
|
|
imageUrls: string[]
|
|
|
|
|
projectId: string
|
|
|
|
|
folderId: string
|
|
|
|
|
agentsInstruction?: string
|
|
|
|
|
sessionId?: string
|
|
|
|
|
userId?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function aiChatActivity(params: AiChatParams) {
|
|
|
|
|
const cleanMessage = params.message.replace(/<@[A-Z0-9]+>/g, '').trim()
|
2026-05-20 22:43:31 +08:00
|
|
|
return agentService.chatWithAgent(
|
2026-05-19 16:05:09 +08:00
|
|
|
params.teamId,
|
|
|
|
|
params.agentId,
|
|
|
|
|
cleanMessage,
|
|
|
|
|
params.imageUrls,
|
|
|
|
|
params.agentsInstruction || '',
|
|
|
|
|
params.sessionId,
|
|
|
|
|
params.userId,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CreateCommentParams {
|
|
|
|
|
assetId: string
|
|
|
|
|
message: string
|
2026-05-21 14:58:31 +08:00
|
|
|
sessionId?: string | null
|
2026-05-19 16:05:09 +08:00
|
|
|
agentId?: string | null
|
|
|
|
|
replyToId?: string | null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createCommentActivity(params: CreateCommentParams) {
|
2026-05-21 14:58:31 +08:00
|
|
|
if (params.sessionId) {
|
|
|
|
|
const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default'
|
|
|
|
|
|
2026-05-21 20:41:37 +08:00
|
|
|
// Fetch teamId from asset
|
|
|
|
|
const asset = await prisma.asset.findUnique({
|
|
|
|
|
where: { id: params.assetId },
|
|
|
|
|
include: { project: true },
|
|
|
|
|
})
|
|
|
|
|
if (!asset || !asset.project) throw new Error('asset or project not found')
|
|
|
|
|
const teamId = asset.project.teamId
|
|
|
|
|
|
2026-05-21 14:58:31 +08:00
|
|
|
// Ensure user exists for agent
|
|
|
|
|
const userExists = await prisma.user.findUnique({ where: { id: agentId } })
|
|
|
|
|
if (!userExists) {
|
|
|
|
|
await prisma.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
id: agentId,
|
|
|
|
|
name: 'Ai Agent',
|
|
|
|
|
email: `${agentId}@shumai.ai`,
|
|
|
|
|
type: 'agent',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure agent exists
|
|
|
|
|
const agentExists = await prisma.agent.findUnique({ where: { id: agentId } })
|
|
|
|
|
if (!agentExists) {
|
|
|
|
|
await prisma.agent.create({
|
|
|
|
|
data: {
|
|
|
|
|
id: agentId,
|
2026-05-21 20:41:37 +08:00
|
|
|
teamId,
|
2026-05-21 14:58:31 +08:00
|
|
|
type: 'chat',
|
|
|
|
|
config: {
|
|
|
|
|
provider: 'openai',
|
|
|
|
|
model: 'gpt-4',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure agent session exists
|
|
|
|
|
const sessionExists = await prisma.agentSession.findUnique({ where: { id: params.sessionId } })
|
|
|
|
|
if (!sessionExists) {
|
|
|
|
|
await prisma.agentSession.create({
|
|
|
|
|
data: {
|
|
|
|
|
id: params.sessionId,
|
|
|
|
|
agentId: agentId,
|
|
|
|
|
cwd: process.cwd(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
return prisma.assetComment.create({
|
|
|
|
|
data: {
|
|
|
|
|
assetId: params.assetId,
|
|
|
|
|
message: params.message,
|
2026-05-21 14:58:31 +08:00
|
|
|
sessionId: params.sessionId ?? null,
|
2026-05-19 16:05:09 +08:00
|
|
|
creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null,
|
|
|
|
|
replyToId: params.replyToId,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UpdateCommentParams {
|
|
|
|
|
commentId: string
|
|
|
|
|
message: string
|
2026-05-21 14:58:31 +08:00
|
|
|
sessionId?: string | null
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateCommentActivity(params: UpdateCommentParams) {
|
|
|
|
|
return prisma.assetComment.update({
|
|
|
|
|
where: { id: params.commentId },
|
2026-05-21 14:58:31 +08:00
|
|
|
data: {
|
|
|
|
|
message: params.message,
|
|
|
|
|
...(params.sessionId !== undefined ? { sessionId: params.sessionId } : {}),
|
|
|
|
|
},
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
}
|