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.

2.0 KB
Raw Blame
import type { VerifyCheckDecisionDetail, VerifyUserDecision, VerifyVerdict } from '@lobechat/types';

import { VerifyCheckResultModel } from '@/database/models/verifyCheckResult';
import type { LobeChatDatabase } from '@/database/type';

/**
 * Ground-truth derived from comparing the user's decision against the verifier's
 * verdict. Drives the data flywheel (nightly FP/FN aggregation by verifier_type).
 *
 * - false positive: verifier said `failed` but the user disagreed (rejected) or
 *   shipped anyway (overridden) — the verifier flagged a non-issue.
 * - false negative: verifier said `passed` but the user rejected it — the
 *   verifier missed a real problem.
 */
export const computeFalseFlags = (
  verdict: VerifyVerdict | null,
  decision: VerifyUserDecision,
): { isFalseNegative: boolean; isFalsePositive: boolean } => ({
  isFalseNegative: verdict === 'passed' && decision === 'rejected',
  isFalsePositive: verdict === 'failed' && (decision === 'rejected' || decision === 'overridden'),
});

export class VerifyFeedbackService {
  private readonly resultModel: VerifyCheckResultModel;

  constructor(db: LobeChatDatabase, userId: string, workspaceId?: string) {
    this.resultModel = new VerifyCheckResultModel(db, userId, workspaceId);
  }

  /**
   * Record a user's decision on a result and precompute its FP/FN flags. The
   * optional detail (note, circled evidence regions, who/when) rides along on
   * `user_decision_detail`; omitting it leaves any existing detail untouched.
   */
  async submitDecision(
    resultId: string,
    decision: VerifyUserDecision,
    detail?: VerifyCheckDecisionDetail,
  ): Promise<boolean> {
    const result = await this.resultModel.findById(resultId);
    if (!result) return false;

    const { isFalsePositive, isFalseNegative } = computeFalseFlags(result.verdict, decision);

    await this.resultModel.update(resultId, {
      isFalseNegative,
      isFalsePositive,
      userDecision: decision,
      ...(detail ? { userDecisionDetail: detail } : {}),
    });
    return true;
  }
}