mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 15:14:10 +00:00
refactor(activities): improve type-safety and primary key generation in aiChatActivity
This commit is contained in:
@@ -3,6 +3,7 @@ import { setupTestDbHooks } from '@/db-test-hooks'
|
|||||||
import { prisma } from '@/db'
|
import { prisma } from '@/db'
|
||||||
import { aiChatActivity } from './agent'
|
import { aiChatActivity } from './agent'
|
||||||
import { agentService } from '@/services/agent/agent'
|
import { agentService } from '@/services/agent/agent'
|
||||||
|
import type { SessionTreeEntry } from '@earendil-works/pi-agent-core'
|
||||||
|
|
||||||
vi.mock('@/services/agent/agent', () => ({
|
vi.mock('@/services/agent/agent', () => ({
|
||||||
agentService: {
|
agentService: {
|
||||||
@@ -111,11 +112,16 @@ describe('Agent Activities', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
expect(entries.length).toBe(1)
|
expect(entries.length).toBe(1)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
const entryData = entries[0].entry as unknown as SessionTreeEntry
|
||||||
const entryData = entries[0].entry as any
|
expect(entryData.id).toBeDefined()
|
||||||
expect(entryData.id).toBe(comment1.id)
|
expect(entryData.id).not.toBe(comment1.id)
|
||||||
expect(entryData.message.content[0].text).toBe('[User One]: First comment message')
|
if (entryData.type === 'message') {
|
||||||
expect(entryData.message.role).toBe('user')
|
const msg = entryData.message as { role: 'user'; content: { type: 'text'; text: string }[] }
|
||||||
|
expect(msg.content[0].text).toBe('[User One]: First comment message')
|
||||||
|
expect(msg.role).toBe('user')
|
||||||
|
} else {
|
||||||
|
throw new Error('Expected entry to be a message')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support deleteCommentActivity', async () => {
|
it('should support deleteCommentActivity', async () => {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { agentService } from '@/services/agent/agent'
|
import { agentService } from '@/services/agent/agent'
|
||||||
import type { AutofillField } from '@/agent'
|
import type { AutofillField } from '@/agent'
|
||||||
import { prisma } from '@/db'
|
import { prisma } from '@/db'
|
||||||
|
import { ulid } from 'ulid'
|
||||||
|
import type { AgentMessage, SessionTreeEntry } from '@earendil-works/pi-agent-core'
|
||||||
|
|
||||||
export interface AutofillAiParams {
|
export interface AutofillAiParams {
|
||||||
teamId: string
|
teamId: string
|
||||||
@@ -125,7 +127,7 @@ export async function aiChatActivity(params: AiChatParams) {
|
|||||||
let prevId: string | null = null
|
let prevId: string | null = null
|
||||||
for (const c of existingComments) {
|
for (const c of existingComments) {
|
||||||
const isAgent = c.creator?.type === 'agent' || c.sessionId !== null
|
const isAgent = c.creator?.type === 'agent' || c.sessionId !== null
|
||||||
const role = isAgent ? 'assistant' : 'user'
|
const role = isAgent ? ('assistant' as const) : ('user' as const)
|
||||||
let messageContent = c.message || ''
|
let messageContent = c.message || ''
|
||||||
|
|
||||||
// Prepend user name to user messages to ensure agent knows who sent what
|
// Prepend user name to user messages to ensure agent knows who sent what
|
||||||
@@ -133,25 +135,53 @@ export async function aiChatActivity(params: AiChatParams) {
|
|||||||
messageContent = `[${c.creator.name}]: ${messageContent}`
|
messageContent = `[${c.creator.name}]: ${messageContent}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const entryId = c.id
|
const entryId = ulid()
|
||||||
const entryJson = {
|
let message: AgentMessage
|
||||||
|
if (role === 'user') {
|
||||||
|
message = {
|
||||||
|
role: 'user',
|
||||||
|
content: [{ type: 'text', text: messageContent }],
|
||||||
|
timestamp: c.createdAt.getTime(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
message = {
|
||||||
|
role: 'assistant',
|
||||||
|
content: [{ type: 'text', text: messageContent }],
|
||||||
|
api: 'openai-completions',
|
||||||
|
provider: 'openai',
|
||||||
|
model: 'gpt-4',
|
||||||
|
usage: {
|
||||||
|
input: 0,
|
||||||
|
output: 0,
|
||||||
|
cacheRead: 0,
|
||||||
|
cacheWrite: 0,
|
||||||
|
totalTokens: 0,
|
||||||
|
cost: {
|
||||||
|
input: 0,
|
||||||
|
output: 0,
|
||||||
|
cacheRead: 0,
|
||||||
|
cacheWrite: 0,
|
||||||
|
total: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
stopReason: 'stop',
|
||||||
|
timestamp: c.createdAt.getTime(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const entryJson: SessionTreeEntry = {
|
||||||
type: 'message',
|
type: 'message',
|
||||||
id: entryId,
|
id: entryId,
|
||||||
parentId: prevId,
|
parentId: prevId,
|
||||||
timestamp: c.createdAt.toISOString(),
|
timestamp: c.createdAt.toISOString(),
|
||||||
message: {
|
message,
|
||||||
role,
|
|
||||||
content: [{ type: 'text', text: messageContent }],
|
|
||||||
timestamp: c.createdAt.getTime(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.agentSessionEntry.create({
|
await prisma.agentSessionEntry.create({
|
||||||
data: {
|
data: {
|
||||||
id: entryId,
|
id: entryId,
|
||||||
sessionId,
|
sessionId,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- entryJson conforms to the internal PiSessionEntry/SessionTreeEntry JSON schema which uses generic json types
|
entry: entryJson,
|
||||||
entry: entryJson as any,
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
prevId = entryId
|
prevId = entryId
|
||||||
|
|||||||
Reference in New Issue
Block a user