π€― 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
import { verifyEvidenceFileIds } from '@lobechat/const/verify';
import type { VerifyEvidence } from '@lobechat/types';
import { FileSource } from '@lobechat/types';
import { and, asc, eq, inArray, isNull } from 'drizzle-orm';
import { files } from '../schemas/file';
import { verifyCheckResults, verifyEvidence } from '../schemas/verify';
import type { LobeChatDatabase } from '../type';
import { buildWorkspacePayload, buildWorkspaceWhere } from '../utils/workspace';
/** Caller-supplied fields when recording one evidence artifact (ownership is injected). */
type CreateVerifyEvidence = Omit<VerifyEvidence, 'id' | 'createdAt'>;
/** An evidence row annotated with the run-stable `checkItemId` of its result. */
export type VerifyEvidenceForRun = VerifyEvidence & { checkItemId: string };
/** An evidence row annotated with both its check item and the round it came from. */
export type VerifyEvidenceForRuns = VerifyEvidenceForRun & { verifyRunId: string };
export class VerifyEvidenceModel {
private readonly db: LobeChatDatabase;
private readonly userId: string;
private readonly workspaceId?: string;
constructor(db: LobeChatDatabase, userId: string, workspaceId?: string) {
this.db = db;
this.userId = userId;
this.workspaceId = workspaceId;
}
private ownership = () =>
buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, verifyEvidence);
/**
* Attribute the backing file rows to the acceptance surface so the resource
* library stops listing them.
*
* Stamped here rather than at upload time on purpose: artifacts reach the
* `files` table through the generic `file.createFile` procedure, which every
* CLI version and every capturer shares. Tagging at the moment the artifact
* becomes evidence is the one funnel all of them pass through, so an outdated
* `lh` binary can't keep seeding untagged files into the library.
*
* Scoped like any other read: workspace mode covers all member files, personal
* mode only the caller's. `source IS NULL` keeps a generation-sourced file
* (attached as evidence) from losing its original attribution.
*/
private markFilesAsEvidence = async (rows: CreateVerifyEvidence[]) => {
// A terminal evidence's stdout / stderr / recording files are evidence too.
const fileIds = [...new Set(rows.flatMap(verifyEvidenceFileIds))];
if (fileIds.length === 0) return;
await this.db
.update(files)
.set({ source: FileSource.Acceptance })
.where(
and(
inArray(files.id, fileIds),
isNull(files.source),
buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, files),
),
);
};
create = async (params: CreateVerifyEvidence) => {
const [result] = await this.db
.insert(verifyEvidence)
.values(buildWorkspacePayload({ userId: this.userId, workspaceId: this.workspaceId }, params))
.returning();
await this.markFilesAsEvidence([params]);
return result;
};
/** Batch-insert the artifacts captured by a single probe / verifier run. */
createMany = async (rows: CreateVerifyEvidence[]) => {
if (rows.length === 0) return [];
const inserted = await this.db
.insert(verifyEvidence)
.values(
rows.map((r) =>
buildWorkspacePayload({ userId: this.userId, workspaceId: this.workspaceId }, r),
),
)
.returning();
await this.markFilesAsEvidence(rows);
return inserted;
};
findById = async (id: string) => {
return this.db.query.verifyEvidence.findFirst({
where: and(eq(verifyEvidence.id, id), this.ownership()),
});
};
/**
* All evidence for one check result, oldest first. The table is flat (no
* `parent_evidence_id`) β a recursive evidence chain is modelled as a new check,
* so this single query is the whole "tree" for a result.
*/
listByCheckResult = async (checkResultId: string) => {
return this.db
.select()
.from(verifyEvidence)
.where(and(eq(verifyEvidence.checkResultId, checkResultId), this.ownership()))
.orderBy(asc(verifyEvidence.createdAt));
};
/**
* All evidence for a whole verification session, each row carrying the
* `checkItemId` of the result it backs (joined through `verify_check_results`).
* Lets the judge and reporter group a run's artifacts by plan item in one
* query, oldest first.
*/
listByRun = async (verifyRunId: string): Promise<VerifyEvidenceForRun[]> => {
const rows = await this.db
.select({
checkItemId: verifyCheckResults.checkItemId,
evidence: verifyEvidence,
})
.from(verifyEvidence)
.innerJoin(verifyCheckResults, eq(verifyEvidence.checkResultId, verifyCheckResults.id))
.where(and(eq(verifyCheckResults.verifyRunId, verifyRunId), this.ownership()))
.orderBy(asc(verifyEvidence.createdAt));
return rows.map((r) => ({ ...r.evidence, checkItemId: r.checkItemId }));
};
/**
* All evidence across several verification rounds in one query, each row
* carrying its result's `checkItemId` and the round (`verifyRunId`) it was
* captured in β the acceptance union joins a whole round chain at once.
*/
listByRuns = async (verifyRunIds: string[]): Promise<VerifyEvidenceForRuns[]> => {
if (verifyRunIds.length === 0) return [];
const rows = await this.db
.select({
checkItemId: verifyCheckResults.checkItemId,
evidence: verifyEvidence,
verifyRunId: verifyCheckResults.verifyRunId,
})
.from(verifyEvidence)
.innerJoin(verifyCheckResults, eq(verifyEvidence.checkResultId, verifyCheckResults.id))
.where(and(inArray(verifyCheckResults.verifyRunId, verifyRunIds), this.ownership()))
.orderBy(asc(verifyEvidence.createdAt));
return rows.map((r) => ({
...r.evidence,
checkItemId: r.checkItemId,
// The join filter guarantees a run id; the column is only nullable for legacy rows.
verifyRunId: r.verifyRunId!,
}));
};
delete = async (id: string) => {
return this.db.delete(verifyEvidence).where(and(eq(verifyEvidence.id, id), this.ownership()));
};
}