Skip to content

🤯 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.

5.9 KB
Raw Blame
import { AcceptanceEvidenceIdentifier } from '@lobechat/builtin-tool-acceptance-evidence';
import { RequestTrigger, type VerifyCheckItem } from '@lobechat/types';

import { VerifyCheckResultModel } from '@/database/models/verifyCheckResult';
import { VerifyEvidenceModel } from '@/database/models/verifyEvidence';
import { VerifyRunModel } from '@/database/models/verifyRun';
import type { AgentOperationItem } from '@/database/schemas/agentOperations';
import type { LobeChatDatabase } from '@/database/type';
import type { AgentHook } from '@/server/services/agentRuntime/hooks/types';
import { AiAgentService } from '@/server/services/aiAgent';

import { readRequiredEvidence } from './evidenceCoverage';

// A criterion that declares evidence types fails its structural gate until
// each one is present, so the prompt has to name them: without the types, the
// evidence turn sent whatever it reached for first (markdown for a `text`
// requirement, a recording as `video` for `gif`) and failed every repair round.
const describeRequiredEvidence = (item: VerifyCheckItem): string => {
  const types = [
    ...new Set(
      (readRequiredEvidence(item.verifierConfig) ?? [])
        .filter((spec) => !spec.scope || spec.scope === 'run_evidence')
        .map((spec) => spec.type),
    ),
  ];
  return types.length > 0 ? ` [requires evidence type: ${types.join(', ')}]` : '';
};

const buildEvidencePrompt = (
  items: VerifyCheckItem[],
): string => `The task execution is complete. Submit the evidence you produced for every Acceptance criterion below.

${items.map((item) => `- ${item.id}: ${item.title}${item.description ? ` — ${item.description}` : ''}${describeRequiredEvidence(item)}`).join('\n')}

Call submitEvidence once for each criterion. This is evidence collection only: do not assign verdicts and do not redo the implementation.`;

/**
 * External CLI agents cannot call server builtin tools. Preserve their final
 * handoff as inline evidence instead of starting an evidence-only hetero turn
 * that can never reach `submitEvidence`.
 *
 * Only criteria the builder left without any evidence get the handoff. A
 * criterion it already evidenced through `lh acceptance run result submit`
 * keeps exactly what it submitted, as an Acceptance round does: the final
 * report is not evidence for a specific check and is already handed to the
 * verifier as the deliverable.
 */
export const recordHeterogeneousDeliverableEvidence = async (params: {
  db: LobeChatDatabase;
  deliverable: string;
  operation: AgentOperationItem;
  plan: VerifyCheckItem[];
  userId: string;
  workspaceId?: string;
}) => {
  const { db, deliverable, operation, plan, userId, workspaceId } = params;
  const run = await new VerifyRunModel(db, userId, workspaceId).findByOperation(operation.id);
  if (!run) throw new Error('Verification run is missing for heterogeneous evidence');

  const evidenced = new Set(
    (await new VerifyEvidenceModel(db, userId, workspaceId).listByRun(run.id)).map(
      (row) => row.checkItemId,
    ),
  );

  for (const item of plan) {
    if (evidenced.has(item.id)) continue;

    const result = await new VerifyCheckResultModel(db, userId, workspaceId).upsertByCheckItem({
      checkItemId: item.id,
      checkItemIndex: item.index,
      checkItemTitle: item.title,
      operationId: operation.id,
      required: item.required,
      verifierType: item.verifierType,
      verifyRunId: run.id,
    });
    await new VerifyEvidenceModel(db, userId, workspaceId).createMany([
      {
        capturedAt: new Date(),
        capturedBy: 'agent',
        checkResultId: result.id,
        content: deliverable,
        description: 'Final deliverable reported by the heterogeneous builder.',
        documentId: null,
        fileId: null,
        type: 'text',
      },
    ]);
  }
};

export const startEvidenceSubmission = async (params: {
  db: LobeChatDatabase;
  deliverable: string;
  goal: string;
  operation: AgentOperationItem;
  plan: VerifyCheckItem[];
  userId: string;
  workspaceId?: string;
}): Promise<string> => {
  const { db, deliverable, goal, operation, plan, userId, workspaceId } = params;
  if (!operation.agentId || !operation.topicId) {
    throw new Error('Task operation has no builder agent or topic for evidence submission');
  }

  const parentOperationId = operation.id;
  const evidencePrompt = buildEvidencePrompt(plan);
  const hooks: AgentHook[] = [
    {
      handler: async () => {
        const { runVerifyAfterEvidenceSubmission } = await import('./lifecycle');
        await runVerifyAfterEvidenceSubmission(
          db,
          userId,
          {
            deliverable,
            goal,
            operationId: parentOperationId,
          },
          workspaceId,
        );
      },
      id: 'acceptance-evidence-on-complete',
      type: 'onComplete',
      webhook: {
        body: {
          deliverable,
          goal,
          parentOperationId,
          userId,
          ...(workspaceId ? { workspaceId } : {}),
        },
        delivery: 'qstash',
        fallback: 'none',
        url: '/api/workflows/verify/on-evidence-complete',
      },
    },
  ];

  const result = await new AiAgentService(db, userId, { workspaceId }).execAgent({
    agentId: operation.agentId,
    appContext: { taskId: operation.taskId, topicId: operation.topicId },
    autoStart: true,
    ephemeralUserMessage: evidencePrompt,
    exclusivePluginIds: [AcceptanceEvidenceIdentifier],
    hooks,
    parentOperationId,
    // Heterogeneous CLI adapters execute `prompt` directly, while the native
    // runtime also renders the ephemeral user message in the existing topic.
    // Keep both populated so this evidence-only continuation has an
    // instruction on every execution target.
    prompt: evidencePrompt,
    suppressUserMessage: true,
    // This turn only submits acceptance evidence, so its spend belongs to verify.
    trigger: RequestTrigger.Verify,
    userInterventionConfig: { approvalMode: 'headless' },
  });

  return result.operationId;
};