mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 08:04:37 +00:00
Configure embedding servers and add a Transcript Agent (#561)
* feat: configure embedding servers and transcript agents * fix(agents): guide Transcript Agent setup * fix(agents): require a configured embedding server URL * docs: consolidate embedding and transcript changelog entry * fix(webui): stabilize transcript menu interactions * test(e2e): verify transcript scroll interruption by position * test(webui): check transcript following without frame timing races
This commit is contained in:
@@ -1040,6 +1040,65 @@ describe('AgentService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('Transcript Agent', () => {
|
||||
test('allows one Transcript Agent per team, including when it is disabled', async () => {
|
||||
const svc = new AgentService()
|
||||
const team = await prisma.team.create({ data: { name: 'Transcript agent team' } })
|
||||
const params = {
|
||||
teamId: team.id,
|
||||
type: 'transcription' as const,
|
||||
name: 'Transcript Agent',
|
||||
avatar: 'custom-transcript-avatar',
|
||||
enabled: false,
|
||||
thinkingLevel: 'off' as const,
|
||||
}
|
||||
|
||||
await expect(svc.createAgent(params)).resolves.toMatchObject({
|
||||
enabled: false,
|
||||
providerId: null,
|
||||
})
|
||||
await expect(svc.createAgent(params)).rejects.toThrow('already exists for this team')
|
||||
expect(await prisma.agent.count({ where: { teamId: team.id, type: 'transcription' } })).toBe(
|
||||
1,
|
||||
)
|
||||
})
|
||||
|
||||
test('provisions and toggles the same read-only agent from the environment', async () => {
|
||||
const svc = new AgentService()
|
||||
const team = await prisma.team.create({ data: { name: 'Managed transcript agent team' } })
|
||||
try {
|
||||
vi.stubEnv('ENABLE_TRANSCRIPTION_AGENT', 'false')
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
expect(
|
||||
await prisma.agent.count({ where: { teamId: team.id, type: 'transcription' } }),
|
||||
).toBe(0)
|
||||
|
||||
vi.stubEnv('ENABLE_TRANSCRIPTION_AGENT', 'true')
|
||||
await svc.ensureSystemAgents()
|
||||
const [agent] = await svc.listAgents({ teamId: team.id })
|
||||
expect(await svc.toAgentInfo(agent)).toMatchObject({
|
||||
enabled: true,
|
||||
readOnly: true,
|
||||
avatarPreset: 'avatar-2',
|
||||
})
|
||||
|
||||
vi.stubEnv('ENABLE_TRANSCRIPTION_AGENT', 'false')
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
const [disabled] = await svc.listAgents({ teamId: team.id })
|
||||
expect(await svc.toAgentInfo(disabled)).toMatchObject({
|
||||
id: agent.id,
|
||||
enabled: false,
|
||||
readOnly: true,
|
||||
})
|
||||
|
||||
vi.stubEnv('ENABLE_TRANSCRIPTION_AGENT', undefined)
|
||||
expect(await svc.toAgentInfo(disabled)).toMatchObject({ enabled: false, readOnly: false })
|
||||
} finally {
|
||||
vi.unstubAllEnvs()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('ensureEmbeddingAgent', () => {
|
||||
test('creates embedding agent when ENABLE_EMBEDDING_AGENT is true', async () => {
|
||||
const db = prisma
|
||||
@@ -1048,7 +1107,7 @@ describe('AgentService', () => {
|
||||
|
||||
process.env.ENABLE_EMBEDDING_AGENT = 'true'
|
||||
try {
|
||||
await svc.ensureEmbeddingAgentForTeam(team.id)
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
const agent = await db.agent.findFirst({
|
||||
where: { teamId: team.id, type: 'embedding' },
|
||||
include: { user: true, skills: { include: { skill: true } }, mcpServers: true },
|
||||
@@ -1063,7 +1122,7 @@ describe('AgentService', () => {
|
||||
expect(info.readOnly).toBe(true)
|
||||
|
||||
// Ensure idempotency
|
||||
await svc.ensureEmbeddingAgentForTeam(team.id)
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
const count = await db.agent.count({
|
||||
where: { teamId: team.id, type: 'embedding' },
|
||||
})
|
||||
@@ -1093,7 +1152,7 @@ describe('AgentService', () => {
|
||||
|
||||
process.env.ENABLE_EMBEDDING_AGENT = 'true'
|
||||
try {
|
||||
await svc.ensureEmbeddingAgentForTeam(team.id)
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
const agent = await db.agent.findFirst({
|
||||
where: { teamId: team.id, type: 'embedding' },
|
||||
include: { user: true, skills: { include: { skill: true } }, mcpServers: true },
|
||||
@@ -1113,7 +1172,7 @@ describe('AgentService', () => {
|
||||
const team = await db.team.create({ data: { name: 'Team No Agent' } })
|
||||
|
||||
delete process.env.ENABLE_EMBEDDING_AGENT
|
||||
await svc.ensureEmbeddingAgentForTeam(team.id)
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
const agent = await db.agent.findFirst({
|
||||
where: { teamId: team.id, type: 'embedding' },
|
||||
})
|
||||
@@ -1127,7 +1186,7 @@ describe('AgentService', () => {
|
||||
|
||||
process.env.ENABLE_EMBEDDING_AGENT = 'false'
|
||||
try {
|
||||
await svc.ensureEmbeddingAgentForTeam(team.id)
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
const agent = await db.agent.findFirst({
|
||||
where: { teamId: team.id, type: 'embedding' },
|
||||
})
|
||||
@@ -1157,7 +1216,7 @@ describe('AgentService', () => {
|
||||
|
||||
process.env.ENABLE_EMBEDDING_AGENT = 'false'
|
||||
try {
|
||||
await svc.ensureEmbeddingAgentForTeam(team.id)
|
||||
await svc.ensureSystemAgentsForTeam(team.id)
|
||||
const agent = await db.agent.findFirst({
|
||||
where: { teamId: team.id, type: 'embedding' },
|
||||
include: { user: true, skills: { include: { skill: true } }, mcpServers: true },
|
||||
@@ -1172,7 +1231,7 @@ describe('AgentService', () => {
|
||||
}
|
||||
})
|
||||
|
||||
test('ensureSystemEmbeddingAgents updates all teams when ENABLE_EMBEDDING_AGENT is false', async () => {
|
||||
test('ensureSystemAgents updates all teams when ENABLE_EMBEDDING_AGENT is false', async () => {
|
||||
const db = prisma
|
||||
const svc = new AgentService()
|
||||
const { team: team1 } = await setupTestData(db)
|
||||
@@ -1194,7 +1253,7 @@ describe('AgentService', () => {
|
||||
|
||||
process.env.ENABLE_EMBEDDING_AGENT = 'false'
|
||||
try {
|
||||
await svc.ensureSystemEmbeddingAgents()
|
||||
await svc.ensureSystemAgents()
|
||||
|
||||
const a1 = await db.agent.findFirst({ where: { teamId: team1.id, type: 'embedding' } })
|
||||
const a2 = await db.agent.findFirst({ where: { teamId: team2.id, type: 'embedding' } })
|
||||
|
||||
@@ -22,6 +22,7 @@ import AdmZip from 'adm-zip'
|
||||
import { getAllowedAgentRoles } from './permissions'
|
||||
import { getPresetAvatarBuffer, getPresetAvatarStorageKey, isPresetAvatarId } from './presets'
|
||||
import { agentSessionService } from './session-service'
|
||||
import { getSystemAgentSetting } from './system-agents'
|
||||
|
||||
const agentInclude = {
|
||||
user: true,
|
||||
@@ -58,7 +59,27 @@ function extractS3Key(urlOrKey?: string | null): string | null | undefined {
|
||||
export class AgentService {
|
||||
private skillContentCache = new Map<string, string>()
|
||||
|
||||
constructor(private readonly prismaClient: typeof prisma = prisma) {}
|
||||
constructor(private readonly prismaClient: typeof prisma | Prisma.TransactionClient = prisma) {}
|
||||
|
||||
private inTransaction<T>(work: (tx: Prisma.TransactionClient) => Promise<T>): Promise<T> {
|
||||
const db = this.prismaClient
|
||||
return '$transaction' in db ? db.$transaction(work) : work(db)
|
||||
}
|
||||
|
||||
private async requireUniqueAgentType(
|
||||
tx: Prisma.TransactionClient,
|
||||
teamId: string,
|
||||
type: AgentType,
|
||||
agentId?: string,
|
||||
): Promise<void> {
|
||||
if (type === 'chat') return
|
||||
|
||||
await tx.$queryRaw`SELECT id FROM teams WHERE id = ${teamId} FOR UPDATE`
|
||||
const existing = await tx.agent.findFirst({
|
||||
where: { type, teamId, id: agentId ? { not: agentId } : undefined },
|
||||
})
|
||||
if (existing) throw new Error(`Agent of type ${type} already exists for this team`)
|
||||
}
|
||||
|
||||
async getSkillContent(skillId: string): Promise<string> {
|
||||
const skill = await this.prismaClient.skill.findUnique({
|
||||
@@ -150,18 +171,6 @@ export class AgentService {
|
||||
deniedTools,
|
||||
} = params
|
||||
|
||||
if (type === 'embedding' || type === 'autofill') {
|
||||
const existing = await this.prismaClient.agent.findFirst({
|
||||
where: {
|
||||
type,
|
||||
teamId,
|
||||
},
|
||||
})
|
||||
if (existing) {
|
||||
throw new Error(`Agent of type ${type} already exists for this team`)
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'chat' || type === 'autofill') {
|
||||
if (!providerId || !modelId) {
|
||||
throw new Error('providerId and modelId are required for chat/autofill agents')
|
||||
@@ -192,11 +201,12 @@ export class AgentService {
|
||||
imageKey = extractS3Key(avatar)
|
||||
}
|
||||
|
||||
return this.prismaClient.$transaction(async (tx) => {
|
||||
return this.inTransaction(async (tx) => {
|
||||
const team = await tx.team.findUnique({
|
||||
where: { id: teamId },
|
||||
})
|
||||
if (!team) throw new Error('team not found')
|
||||
await this.requireUniqueAgentType(tx, teamId, type)
|
||||
|
||||
if (runnerId) {
|
||||
const runner = await tx.runner.findFirst({
|
||||
@@ -298,19 +308,6 @@ export class AgentService {
|
||||
|
||||
if (!agent) throw new Error('agent not found')
|
||||
|
||||
if (type === 'embedding' || type === 'autofill') {
|
||||
const existing = await this.prismaClient.agent.findFirst({
|
||||
where: {
|
||||
type,
|
||||
id: { not: agentId },
|
||||
teamId: agent.teamId,
|
||||
},
|
||||
})
|
||||
if (existing) {
|
||||
throw new Error(`Agent of type ${type} already exists for this team`)
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'chat' || type === 'autofill') {
|
||||
if (!providerId || !modelId) {
|
||||
throw new Error('providerId and modelId are required for chat/autofill agents')
|
||||
@@ -364,7 +361,9 @@ export class AgentService {
|
||||
avatarPreset: newAvatarPreset,
|
||||
}
|
||||
|
||||
return this.prismaClient.$transaction(async (tx) => {
|
||||
return this.inTransaction(async (tx) => {
|
||||
await this.requireUniqueAgentType(tx, agent.teamId, type, agentId)
|
||||
|
||||
await tx.user.update({
|
||||
where: { id: agentId },
|
||||
data: {
|
||||
@@ -476,10 +475,7 @@ export class AgentService {
|
||||
*/
|
||||
async toAgentInfo(agent: AgentInfoSource): Promise<AgentInfo> {
|
||||
const config = agent.config as unknown as PrismaJson.AgentConfig
|
||||
const isManagedByEnv =
|
||||
process.env.ENABLE_EMBEDDING_AGENT === 'true' ||
|
||||
process.env.ENABLE_EMBEDDING_AGENT === 'false'
|
||||
const isReadOnly = agent.type === 'embedding' && isManagedByEnv
|
||||
const isReadOnly = getSystemAgentSetting(agent.type) !== undefined
|
||||
|
||||
return {
|
||||
id: agent.id,
|
||||
@@ -652,57 +648,43 @@ export class AgentService {
|
||||
)
|
||||
}
|
||||
|
||||
async ensureSystemEmbeddingAgents(): Promise<void> {
|
||||
const envVal = process.env.ENABLE_EMBEDDING_AGENT
|
||||
if (envVal !== 'true' && envVal !== 'false') {
|
||||
return
|
||||
}
|
||||
async ensureSystemAgents(): Promise<void> {
|
||||
if (!getSystemAgentSetting('embedding') && !getSystemAgentSetting('transcription')) return
|
||||
|
||||
const teams = await this.prismaClient.team.findMany({
|
||||
orderBy: { id: 'desc' },
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
for (const team of teams) {
|
||||
await this.ensureEmbeddingAgentForTeam(team.id)
|
||||
await this.ensureSystemAgentsForTeam(team.id)
|
||||
}
|
||||
}
|
||||
|
||||
async ensureEmbeddingAgentForTeam(teamId: string): Promise<void> {
|
||||
const envVal = process.env.ENABLE_EMBEDDING_AGENT
|
||||
if (envVal !== 'true' && envVal !== 'false') {
|
||||
return
|
||||
}
|
||||
async ensureSystemAgentsForTeam(teamId: string): Promise<void> {
|
||||
for (const type of ['embedding', 'transcription'] as const) {
|
||||
const setting = getSystemAgentSetting(type)
|
||||
if (!setting) continue
|
||||
|
||||
const existing = await this.prismaClient.agent.findFirst({
|
||||
where: {
|
||||
teamId,
|
||||
type: 'embedding',
|
||||
},
|
||||
})
|
||||
await this.inTransaction(async (tx) => {
|
||||
await tx.$queryRaw`SELECT id FROM teams WHERE id = ${teamId} FOR UPDATE`
|
||||
const existing = await tx.agent.findFirst({
|
||||
where: { teamId, type },
|
||||
})
|
||||
|
||||
if (envVal === 'true') {
|
||||
if (!existing) {
|
||||
await this.createAgent({
|
||||
teamId,
|
||||
name: 'embedding-agent',
|
||||
type: 'embedding',
|
||||
avatar: 'avatar-1',
|
||||
thinkingLevel: 'off',
|
||||
enabled: true,
|
||||
})
|
||||
} else if (!existing.enabled) {
|
||||
await this.prismaClient.agent.update({
|
||||
where: { id: existing.id },
|
||||
data: { enabled: true },
|
||||
})
|
||||
}
|
||||
} else if (envVal === 'false') {
|
||||
if (existing && existing.enabled) {
|
||||
await this.prismaClient.agent.update({
|
||||
where: { id: existing.id },
|
||||
data: { enabled: false },
|
||||
})
|
||||
}
|
||||
if (existing && existing.enabled !== setting.enabled) {
|
||||
await tx.agent.update({ where: { id: existing.id }, data: { enabled: setting.enabled } })
|
||||
} else if (!existing && setting.enabled) {
|
||||
await new AgentService(tx).createAgent({
|
||||
teamId,
|
||||
name: type === 'embedding' ? 'embedding-agent' : setting.name,
|
||||
type,
|
||||
avatar: setting.avatar,
|
||||
thinkingLevel: 'off',
|
||||
enabled: true,
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { AgentType } from '@shumai/dtos'
|
||||
|
||||
const systemAgentSettings = {
|
||||
embedding: { envKey: 'ENABLE_EMBEDDING_AGENT', name: 'Embedding agent', avatar: 'avatar-1' },
|
||||
transcription: {
|
||||
envKey: 'ENABLE_TRANSCRIPTION_AGENT',
|
||||
name: 'Transcript Agent',
|
||||
avatar: 'avatar-2',
|
||||
},
|
||||
} as const
|
||||
|
||||
export function getSystemAgentSetting(type: AgentType) {
|
||||
if (type !== 'embedding' && type !== 'transcription') return undefined
|
||||
|
||||
const { envKey, name, avatar } = systemAgentSettings[type]
|
||||
const value = process.env[envKey]
|
||||
if (value !== 'true' && value !== 'false') return undefined
|
||||
|
||||
return { name, avatar, enabled: value === 'true' }
|
||||
}
|
||||
Reference in New Issue
Block a user