mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 23:19:32 +00:00
feat(agent): replace isAi with sessionId & implement admin logs UI
This commit is contained in:
@@ -41,17 +41,61 @@ export async function aiChatActivity(params: AiChatParams) {
|
||||
export interface CreateCommentParams {
|
||||
assetId: string
|
||||
message: string
|
||||
isAi?: boolean
|
||||
sessionId?: string | null
|
||||
agentId?: string | null
|
||||
replyToId?: string | null
|
||||
}
|
||||
|
||||
export async function createCommentActivity(params: CreateCommentParams) {
|
||||
if (params.sessionId) {
|
||||
const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default'
|
||||
|
||||
// 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,
|
||||
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(),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return prisma.assetComment.create({
|
||||
data: {
|
||||
assetId: params.assetId,
|
||||
message: params.message,
|
||||
isAi: params.isAi ?? false,
|
||||
sessionId: params.sessionId ?? null,
|
||||
creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null,
|
||||
replyToId: params.replyToId,
|
||||
},
|
||||
@@ -61,11 +105,15 @@ export async function createCommentActivity(params: CreateCommentParams) {
|
||||
export interface UpdateCommentParams {
|
||||
commentId: string
|
||||
message: string
|
||||
sessionId?: string | null
|
||||
}
|
||||
|
||||
export async function updateCommentActivity(params: UpdateCommentParams) {
|
||||
return prisma.assetComment.update({
|
||||
where: { id: params.commentId },
|
||||
data: { message: params.message },
|
||||
data: {
|
||||
message: params.message,
|
||||
...(params.sessionId !== undefined ? { sessionId: params.sessionId } : {}),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ describe('AIAutofill Workflow', () => {
|
||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
message: '__AUTOFILL__',
|
||||
isAi: true,
|
||||
sessionId: 'task-1',
|
||||
agentId: 'default',
|
||||
})
|
||||
|
||||
@@ -160,7 +160,7 @@ describe('AIAutofill Workflow', () => {
|
||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
message: '__AUTOFILL__',
|
||||
isAi: true,
|
||||
sessionId: 'task-1',
|
||||
agentId: 'real-agent-id',
|
||||
})
|
||||
})
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function aiAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
|
||||
assetId: task.assetId,
|
||||
message: '__AUTOFILL__',
|
||||
isAi: true,
|
||||
sessionId: (payload?.session_id as string) || task.id,
|
||||
agentId: (payload?.agentId as string) || 'default',
|
||||
})
|
||||
placeholderCommentId = placeholder.id
|
||||
@@ -148,6 +148,7 @@ export async function aiAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||
await executeActivity(TaskQueueDb, updateCommentActivity, {
|
||||
commentId: placeholderCommentId,
|
||||
message: 'Autofill completed successfully.',
|
||||
sessionId: aiResult.sessionId,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ describe('AIChat Workflow', () => {
|
||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
message: '__CHAT__',
|
||||
isAi: true,
|
||||
sessionId: 'pending',
|
||||
agentId: 'bot-1',
|
||||
replyToId: 'comment-1',
|
||||
})
|
||||
@@ -172,7 +172,7 @@ describe('AIChat Workflow', () => {
|
||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
message: '__CHAT__',
|
||||
isAi: true,
|
||||
sessionId: 'pending',
|
||||
agentId: 'bot-1',
|
||||
replyToId: 'root-comment-id', // MUST be root, not reply-1
|
||||
})
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
|
||||
assetId: task.assetId,
|
||||
message: '__CHAT__',
|
||||
isAi: true,
|
||||
sessionId: payload?.session_id || 'pending',
|
||||
agentId: payload?.agentId,
|
||||
replyToId: userComment.replyToId ?? userComment.id,
|
||||
})
|
||||
@@ -100,6 +100,7 @@ export async function aiChat(task: WorkflowTask): Promise<void> {
|
||||
await executeActivity(TaskQueueDb, updateCommentActivity, {
|
||||
commentId: placeholderCommentId,
|
||||
message: aiResult.text,
|
||||
sessionId: aiResult.sessionId,
|
||||
})
|
||||
|
||||
// Also store sessionId in comment's metadata if we had such a field,
|
||||
|
||||
@@ -78,7 +78,7 @@ describe('AIEmbedding Workflow', () => {
|
||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
message: '__EMBEDDING__',
|
||||
isAi: true,
|
||||
sessionId: 'task-1',
|
||||
agentId: 'default',
|
||||
})
|
||||
|
||||
@@ -136,7 +136,7 @@ describe('AIEmbedding Workflow', () => {
|
||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
message: '__EMBEDDING__',
|
||||
isAi: true,
|
||||
sessionId: 'task-1',
|
||||
agentId: 'real-agent-id',
|
||||
})
|
||||
})
|
||||
|
||||
@@ -25,7 +25,7 @@ export async function aiEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
||||
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
|
||||
assetId: task.assetId,
|
||||
message: '__EMBEDDING__',
|
||||
isAi: true,
|
||||
sessionId: (payload?.session_id as string) || task.id,
|
||||
agentId: (payload?.agentId as string) || 'default',
|
||||
})
|
||||
placeholderCommentId = placeholder.id
|
||||
|
||||
@@ -35,7 +35,7 @@ export async function aiTranscriptionMedia(task: WorkflowTask): Promise<void> {
|
||||
const placeholder = await executeActivity(TaskQueueDb, createCommentActivity, {
|
||||
assetId: task.assetId,
|
||||
message: '__TRANSCRIPTION__',
|
||||
isAi: true,
|
||||
sessionId: (payload?.session_id as string) || task.id,
|
||||
agentId: (payload?.agentId as string) || 'default',
|
||||
})
|
||||
placeholderCommentId = placeholder.id
|
||||
|
||||
Reference in New Issue
Block a user