Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
import { Router } from "express";
|
2026-03-03 08:45:26 -06:00
|
|
|
import type { Db } from "@paperclipai/db";
|
2026-04-07 18:26:34 -05:00
|
|
|
import { and, eq } from "drizzle-orm";
|
|
|
|
|
import { inboxDismissals, joinRequests } from "@paperclipai/db";
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
import { sidebarBadgeService } from "../services/sidebar-badges.js";
|
2026-02-23 14:40:32 -06:00
|
|
|
import { accessService } from "../services/access.js";
|
2026-03-06 09:42:47 -06:00
|
|
|
import { dashboardService } from "../services/dashboard.js";
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
import { assertCompanyAccess } from "./authz.js";
|
|
|
|
|
|
2026-04-07 18:26:34 -05:00
|
|
|
function buildDismissedAtByKey(
|
|
|
|
|
dismissals: Array<{ itemKey: string; dismissedAt: Date | string }>,
|
|
|
|
|
): Map<string, number> {
|
|
|
|
|
return new Map(
|
|
|
|
|
dismissals.map((dismissal) => [dismissal.itemKey, new Date(dismissal.dismissedAt).getTime()]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
export function sidebarBadgeRoutes(db: Db) {
|
|
|
|
|
const router = Router();
|
|
|
|
|
const svc = sidebarBadgeService(db);
|
2026-02-23 14:40:32 -06:00
|
|
|
const access = accessService(db);
|
2026-03-06 09:42:47 -06:00
|
|
|
const dashboard = dashboardService(db);
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
|
|
|
|
|
router.get("/companies/:companyId/sidebar-badges", async (req, res) => {
|
|
|
|
|
const companyId = req.params.companyId as string;
|
|
|
|
|
assertCompanyAccess(req, companyId);
|
2026-02-23 14:40:32 -06:00
|
|
|
let canApproveJoins = false;
|
|
|
|
|
if (req.actor.type === "board") {
|
|
|
|
|
canApproveJoins =
|
|
|
|
|
req.actor.source === "local_implicit" ||
|
|
|
|
|
Boolean(req.actor.isInstanceAdmin) ||
|
|
|
|
|
(await access.canUser(companyId, req.actor.userId, "joins:approve"));
|
|
|
|
|
} else if (req.actor.type === "agent" && req.actor.agentId) {
|
|
|
|
|
canApproveJoins = await access.hasPermission(companyId, "agent", req.actor.agentId, "joins:approve");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 18:26:34 -05:00
|
|
|
const visibleJoinRequests = canApproveJoins
|
2026-02-23 14:40:32 -06:00
|
|
|
? await db
|
2026-04-07 18:26:34 -05:00
|
|
|
.select({
|
|
|
|
|
id: joinRequests.id,
|
|
|
|
|
updatedAt: joinRequests.updatedAt,
|
|
|
|
|
createdAt: joinRequests.createdAt,
|
|
|
|
|
})
|
2026-02-23 14:40:32 -06:00
|
|
|
.from(joinRequests)
|
|
|
|
|
.where(and(eq(joinRequests.companyId, companyId), eq(joinRequests.status, "pending_approval")))
|
2026-04-07 18:26:34 -05:00
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
const dismissedAtByKey =
|
|
|
|
|
req.actor.type === "board" && req.actor.userId
|
|
|
|
|
? await db
|
|
|
|
|
.select({ itemKey: inboxDismissals.itemKey, dismissedAt: inboxDismissals.dismissedAt })
|
|
|
|
|
.from(inboxDismissals)
|
|
|
|
|
.where(and(eq(inboxDismissals.companyId, companyId), eq(inboxDismissals.userId, req.actor.userId)))
|
|
|
|
|
.then(buildDismissedAtByKey)
|
|
|
|
|
: new Map<string, number>();
|
2026-02-23 14:40:32 -06:00
|
|
|
|
2026-02-26 16:33:39 -06:00
|
|
|
const badges = await svc.get(companyId, {
|
2026-04-07 18:26:34 -05:00
|
|
|
dismissals: dismissedAtByKey,
|
|
|
|
|
joinRequests: visibleJoinRequests,
|
2026-02-26 16:33:39 -06:00
|
|
|
});
|
2026-03-06 09:42:47 -06:00
|
|
|
const summary = await dashboard.summary(companyId);
|
2026-03-06 11:01:29 -06:00
|
|
|
const hasFailedRuns = badges.failedRuns > 0;
|
2026-03-06 09:42:47 -06:00
|
|
|
const alertsCount =
|
2026-03-06 11:01:29 -06:00
|
|
|
(summary.agents.error > 0 && !hasFailedRuns ? 1 : 0) +
|
2026-03-06 09:42:47 -06:00
|
|
|
(summary.costs.monthBudgetCents > 0 && summary.costs.monthUtilizationPercent >= 80 ? 1 : 0);
|
2026-04-07 18:26:34 -05:00
|
|
|
badges.inbox = badges.failedRuns + alertsCount + badges.joinRequests + badges.approvals;
|
2026-03-06 09:42:47 -06:00
|
|
|
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
res.json(badges);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return router;
|
|
|
|
|
}
|