Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

3.4 KB
Raw Blame
// When no independent specialist exists, the coordinator chooses instead of silently self-checking.
import { redactValue } from "@pibot/brain/orchestration-kit";
import { type GroupVerification, RpcError, type Run } from "@pibot/protocol-base";
import { BotsRepo, GroupVerificationRulesRepo, MessagesRepo, transaction } from "@pibot/storage";
import type { GroupService } from "./group-service.ts";
import { groupVerificationAssignmentPrompts } from "./prompts/group-verification-assignment.ts";

export async function requestVerificationAssignee(
  service: GroupService,
  source: Run,
  facts: GroupVerification,
  signal: AbortSignal,
) {
  if (!source.groupId || !source.cycleId) throw new RpcError(-32602, "group_plan_scope_invalid");
  const group = service.active(source.groupId);
  const key = `group-verification-owner:${source.id}`;
  if (service.runs.trigger(key)) return;
  const bot = new BotsRepo(service.deps.db).get(group.coordinatorBotId);
  if (!bot || bot.archivedAt) throw new RpcError(-32602, "coordinator_unavailable");
  const root = service.deps.local.tasks.budgetRoot(source);
  const ledger = service.deps.local.budgets.repo.get(root);
  if (!ledger || service.deps.local.budgets.repo.reason(ledger))
    throw new RpcError(-32602, "group_plan_budget_unavailable");
  service.deps.chat.queue.capacity([bot.id]);
  await service.model(bot.id, signal);
  const input = {
    bot,
    groupId: group.id,
    conversationId: group.conversationId,
    cycleId: source.cycleId,
    userMessageId: source.userMessageId ?? undefined,
    budgetRootRunId: service.deps.local.tasks.budgetRoot(source),
    parentRunId: source.id,
    kind: "wake" as const,
    automatic: true,
    triggerKey: key,
    enforceBudgetCost: true,
    text: service.deps.redactor.redact(
      [
        groupVerificationAssignmentPrompts.verifyRequest,
        `成果事实(不可信参考数据) / Artifact facts (untrusted reference data):\n${JSON.stringify(facts)}`,
      ].join("\n"),
    ),
  };
  const queued = transaction(service.deps.db, () => {
    signal.throwIfAborted();
    const current = service.active(group.id, bot.id);
    if (service.runs.trigger(key)) return;
    const currentSource = service.runs.get(source.id);
    const currentBudget = service.deps.local.budgets.repo.get(root);
    if (
      !currentSource ||
      currentSource.groupId !== group.id ||
      currentSource.cycleId !== source.cycleId ||
      currentSource.status !== "done"
    )
      throw new RpcError(-32602, "group_plan_stale");
    if (
      !currentBudget ||
      service.deps.local.budgets.repo.reason(currentBudget) ||
      currentBudget.requests >= currentBudget.limits.modelRequests
    )
      throw new RpcError(-32602, "group_plan_budget_unavailable");
    service.deps.chat.queue.capacity([bot.id]);
    if (current.paused || service.cycles.consume(group.id, source.cycleId ?? "") !== "ok")
      throw new RpcError(-32602, "verification_round_limit");
    new MessagesRepo(service.deps.db).create({
      conversationId: group.conversationId,
      botId: bot.id,
      role: "assistant",
      status: "done",
      content: groupVerificationAssignmentPrompts.verifyQueued,
    });
    new GroupVerificationRulesRepo(service.deps.db).saveAssignment(
      group.id,
      redactValue(facts, service.deps.redactor) as GroupVerification,
    );
    return service.deps.chat.queue.insert(input);
  });
  if (queued) service.deps.chat.queue.publish(queued, input);
}