🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(verify): bill workspace verify model calls to the workspace (#22766)
Rdmclin28004b155b99f
diff --git a/apps/server/src/services/verify/__tests__/executor.test.ts b/apps/server/src/services/verify/__tests__/executor.test.tsindex 3a17ae3bb58..5890cf3efaa 100644--- a/apps/server/src/services/verify/__tests__/executor.test.ts+++ b/apps/server/src/services/verify/__tests__/executor.test.ts@@ -2,6 +2,8 @@import { terminalRecord } from '@lobechat/utils/verify/terminalEvidence.fixture';import { beforeEach, describe, expect, it, vi } from 'vitest';+import { AiGenerationService } from '@/server/services/aiGeneration';+import { VerifyExecutorService } from '../executor';const mocks = vi.hoisted(() => ({@@ -474,6 +476,44 @@ describe('VerifyExecutorService', () => {);});+ /**+ * Regression: judge calls in a workspace billed the caller's personal budget because the+ * model call was built without the workspace id.+ */+ it('bills batch and single-item judging to the workspace it runs in', async () => {+ mocks.runEnsureForOperation.mockResolvedValue({+ id: 'run-1',+ plan: [+ { id: 'a', index: 0, required: true, title: 'A', verifierType: 'llm' },+ { id: 'b', index: 1, required: true, title: 'B', verifierType: 'llm' },+ ],+ planConfirmedAt: new Date(),+ });+ mocks.resultListByRun.mockReset().mockResolvedValue([]);+ mocks.aiGenerateObject+ .mockResolvedValueOnce({+ verdicts: [+ { checkItemId: 'a', confidence: 1, evidence: 'ok', reasoning: 'ok', verdict: 'passed' },+ ],+ })+ .mockResolvedValueOnce({ confidence: 1, evidence: 'ok', reasoning: 'ok', verdict: 'passed' });+ const db = {} as never;++ await new VerifyExecutorService(db, 'user-1', 'ws-1').execute({+ deliverable: 'done',+ goal: 'ship',+ modelConfig: { model: 'model', provider: 'provider' },+ operationId: 'op-1',+ });++ expect(mocks.aiGenerateObject).toHaveBeenCalledTimes(2);+ expect(AiGenerationService).toHaveBeenCalledTimes(2);+ expect(vi.mocked(AiGenerationService).mock.calls).toEqual([+ [db, 'user-1', 'ws-1'],+ [db, 'user-1', 'ws-1'],+ ]);+ });+it('loads screenshot content into a vision-model message', async () => {mocks.aiModelFind.mockResolvedValue({ abilities: { vision: true } });mocks.runEnsureForOperation.mockResolvedValue({diff --git a/apps/server/src/services/verify/__tests__/holisticPlan.test.ts b/apps/server/src/services/verify/__tests__/holisticPlan.test.tsindex 3ff1144fe08..959159f853b 100644--- a/apps/server/src/services/verify/__tests__/holisticPlan.test.ts+++ b/apps/server/src/services/verify/__tests__/holisticPlan.test.ts@@ -1,6 +1,8 @@import type { VerifyCheckItem } from '@lobechat/types';import { beforeEach, describe, expect, it, vi } from 'vitest';+import { AiGenerationService } from '@/server/services/aiGeneration';+import { VERIFY_PLAN_MODEL_CONFIG } from '../modelConfig';import { VerifyPlanGeneratorService } from '../planGenerator';@@ -252,3 +254,38 @@ describe('verify plan generation model', () => {});});});++/**+ * Regression: plan generation in a workspace billed the caller's personal budget because the+ * model call was built without the workspace id, so the cost never reached workspace spend.+ */+describe('verify plan generation billing scope', () => {+ beforeEach(() => {+ generateObjectMock.mockReset();+ vi.mocked(AiGenerationService).mockClear();+ });++ it('bills criteria generation to the workspace it runs in', async () => {+ generateObjectMock.mockResolvedValue({ criteria: [] });++ await new VerifyPlanGeneratorService(db, 'user-1', 'ws-1').generateCriteria({+ goal: 'Ship the feature',+ });++ expect(AiGenerationService).toHaveBeenCalledWith(db, 'user-1', 'ws-1');+ });++ it('bills the requirement split to the workspace it runs in', async () => {+ generateObjectMock.mockResolvedValue({ criteria: [] });++ await new VerifyPlanGeneratorService(db, 'user-1', 'ws-1').generateDraftPlan({+ enableAiGeneration: true,+ goal: 'Ship the feature',+ holisticFallback: true,+ operationId: 'op-1',+ });++ expect(generateObjectMock).toHaveBeenCalledTimes(1);+ expect(AiGenerationService).toHaveBeenCalledWith(db, 'user-1', 'ws-1');+ });+});diff --git a/apps/server/src/services/verify/__tests__/reporterBilling.test.ts b/apps/server/src/services/verify/__tests__/reporterBilling.test.tsnew file mode 100644index 00000000000..a8b25559c03--- /dev/null+++ b/apps/server/src/services/verify/__tests__/reporterBilling.test.ts@@ -0,0 +1,81 @@+// @vitest-environment node+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { AiGenerationService } from '@/server/services/aiGeneration';++import { VerifyReporterService } from '../reporter';++const mocks = vi.hoisted(() => ({+ aiGenerateObject: vi.fn(),+ documentFindByIds: vi.fn(),+ evidenceListByRun: vi.fn(),+ reportUpsertByRun: vi.fn(),+ resultListByRun: vi.fn(),+}));++vi.mock('@/database/models/document', () => ({+ DocumentModel: vi.fn(function () {+ return { findByIds: mocks.documentFindByIds };+ }),+}));+vi.mock('@/database/models/verifyCheckResult', () => ({+ VerifyCheckResultModel: vi.fn(function () {+ return { listByRun: mocks.resultListByRun };+ }),+}));+vi.mock('@/database/models/verifyEvidence', () => ({+ VerifyEvidenceModel: vi.fn(function () {+ return { listByRun: mocks.evidenceListByRun };+ }),+}));+vi.mock('@/database/models/verifyReport', () => ({+ VerifyReportModel: vi.fn(function () {+ return { upsertByRun: mocks.reportUpsertByRun };+ }),+}));+vi.mock('@/server/services/aiGeneration', () => ({+ AiGenerationService: vi.fn(function () {+ return { generateObject: mocks.aiGenerateObject };+ }),+}));+vi.mock('../responseLanguage', () => ({+ resolveVerifyResponseLanguage: vi.fn(async () => undefined),+}));++/**+ * Regression: the report narrative in a workspace billed the caller's personal budget because+ * the model call was built without the workspace id.+ */+describe('VerifyReporterService billing scope', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ mocks.resultListByRun.mockResolvedValue([+ {+ checkItemId: 'a',+ checkItemTitle: 'A',+ confidence: 1,+ required: true,+ status: 'passed',+ verdict: 'passed',+ },+ ]);+ mocks.evidenceListByRun.mockResolvedValue([]);+ mocks.documentFindByIds.mockResolvedValue([]);+ mocks.aiGenerateObject.mockResolvedValue({ content: 'All good', summary: 'Passed' });+ mocks.reportUpsertByRun.mockImplementation(async (report) => report);+ });++ it('bills the report narrative to the workspace it runs in', async () => {+ const db = {} as never;++ await new VerifyReporterService(db, 'user-1', 'ws-1').generateReport({+ deliverable: 'done',+ goal: 'ship',+ modelConfig: { model: 'model', provider: 'provider' },+ verifyRunId: 'run-1',+ });++ expect(mocks.aiGenerateObject).toHaveBeenCalledTimes(1);+ expect(AiGenerationService).toHaveBeenCalledWith(db, 'user-1', 'ws-1');+ });+});diff --git a/apps/server/src/services/verify/executor.ts b/apps/server/src/services/verify/executor.tsindex d42d6e83641..c07dec2c45f 100644--- a/apps/server/src/services/verify/executor.ts+++ b/apps/server/src/services/verify/executor.ts@@ -92,6 +92,7 @@ const toToulmin = (v: SingleVerdict): ToulminVerdict => ({export class VerifyExecutorService {private readonly db: LobeChatDatabase;private readonly userId: string;+ private readonly workspaceId?: string;private readonly runModel: VerifyRunModel;private readonly resultModel: VerifyCheckResultModel;private readonly statusService: VerifyStatusService;@@ -104,6 +105,7 @@ export class VerifyExecutorService {constructor(db: LobeChatDatabase, userId: string, workspaceId?: string) {this.db = db;this.userId = userId;+ this.workspaceId = workspaceId;this.runModel = new VerifyRunModel(db, userId, workspaceId);this.resultModel = new VerifyCheckResultModel(db, userId, workspaceId);this.statusService = new VerifyStatusService(db, userId, workspaceId);@@ -475,7 +477,7 @@ export class VerifyExecutorService {mode: 'batch',});- const ai = new AiGenerationService(this.db, this.userId);+ const ai = new AiGenerationService(this.db, this.userId, this.workspaceId);const raw = await ai.generateObject({...chain,@@ -553,7 +555,7 @@ export class VerifyExecutorService {mode: 'single',});- const ai = new AiGenerationService(this.db, this.userId);+ const ai = new AiGenerationService(this.db, this.userId, this.workspaceId);const raw = await ai.generateObject({...chain,diff --git a/apps/server/src/services/verify/planGenerator.ts b/apps/server/src/services/verify/planGenerator.tsindex 543e43628c9..3b15a2cfc8e 100644--- a/apps/server/src/services/verify/planGenerator.ts+++ b/apps/server/src/services/verify/planGenerator.ts@@ -134,6 +134,7 @@ const criterionToCheckItem = (export class VerifyPlanGeneratorService {private readonly db: LobeChatDatabase;private readonly userId: string;+ private readonly workspaceId?: string;private readonly criterionModel: VerifyCriterionModel;private readonly rubricModel: VerifyRubricModel;private readonly runModel: VerifyRunModel;@@ -154,6 +155,7 @@ export class VerifyPlanGeneratorService {) {this.db = db;this.userId = userId;+ this.workspaceId = workspaceId;this.callerAgentVisibility = callerAgentVisibility;this.criterionModel = new VerifyCriterionModel(db, userId, workspaceId);this.rubricModel = new VerifyRubricModel(db, userId, workspaceId);@@ -174,7 +176,11 @@ export class VerifyPlanGeneratorService {maxCriteria?: number;}): Promise<CriterionDraft[]> {const maxCriteria = params.maxCriteria ?? DEFAULT_MAX_AI_CRITERIA;- const raw = await new AiGenerationService(this.db, this.userId).generateObject(+ const raw = await new AiGenerationService(+ this.db,+ this.userId,+ this.workspaceId,+ ).generateObject({...chainVerifyPlan({context: params.context,@@ -333,7 +339,7 @@ export class VerifyPlanGeneratorService {maxCriteria: params.maxCriteria,});- const ai = new AiGenerationService(this.db, this.userId);+ const ai = new AiGenerationService(this.db, this.userId, this.workspaceId);const raw = await ai.generateObject({...chain,diff --git a/apps/server/src/services/verify/reporter.ts b/apps/server/src/services/verify/reporter.tsindex 4a6b36b1e37..12ab43efda2 100644--- a/apps/server/src/services/verify/reporter.ts+++ b/apps/server/src/services/verify/reporter.ts@@ -43,6 +43,7 @@ export interface GenerateReportParams {export class VerifyReporterService {private readonly db: LobeChatDatabase;private readonly userId: string;+ private readonly workspaceId?: string;private readonly resultModel: VerifyCheckResultModel;private readonly evidenceModel: VerifyEvidenceModel;private readonly reportModel: VerifyReportModel;@@ -51,6 +52,7 @@ export class VerifyReporterService {constructor(db: LobeChatDatabase, userId: string, workspaceId?: string) {this.db = db;this.userId = userId;+ this.workspaceId = workspaceId;this.resultModel = new VerifyCheckResultModel(db, userId, workspaceId);this.evidenceModel = new VerifyEvidenceModel(db, userId, workspaceId);this.reportModel = new VerifyReportModel(db, userId, workspaceId);@@ -127,7 +129,7 @@ export class VerifyReporterService {modelConfig: { model: string; provider: string },) {try {- const ai = new AiGenerationService(this.db, this.userId);+ const ai = new AiGenerationService(this.db, this.userId, this.workspaceId);const raw = await ai.generateObject({...chain,