2026-04-07 18:26:34 -05:00
|
|
|
import { and, desc, eq, inArray, not } from "drizzle-orm";
|
2026-03-03 08:45:26 -06:00
|
|
|
import type { Db } from "@paperclipai/db";
|
|
|
|
|
import { agents, approvals, heartbeatRuns } from "@paperclipai/db";
|
|
|
|
|
import type { SidebarBadges } from "@paperclipai/shared";
|
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
|
|
|
|
|
|
|
|
const ACTIONABLE_APPROVAL_STATUSES = ["pending", "revision_requested"];
|
2026-02-20 10:32:17 -06:00
|
|
|
const FAILED_HEARTBEAT_STATUSES = ["failed", "timed_out"];
|
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
|
|
|
|
2026-04-07 18:26:34 -05:00
|
|
|
function normalizeTimestamp(value: Date | string | null | undefined): number {
|
|
|
|
|
if (!value) return 0;
|
|
|
|
|
const timestamp = new Date(value).getTime();
|
|
|
|
|
return Number.isFinite(timestamp) ? timestamp : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isDismissed(
|
|
|
|
|
dismissedAtByKey: ReadonlyMap<string, number>,
|
|
|
|
|
itemKey: string,
|
|
|
|
|
activityAt: Date | string | null | undefined,
|
|
|
|
|
) {
|
|
|
|
|
const dismissedAt = dismissedAtByKey.get(itemKey);
|
|
|
|
|
if (dismissedAt == null) return false;
|
|
|
|
|
return dismissedAt >= normalizeTimestamp(activityAt);
|
|
|
|
|
}
|
|
|
|
|
|
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 sidebarBadgeService(db: Db) {
|
|
|
|
|
return {
|
2026-02-26 16:33:39 -06:00
|
|
|
get: async (
|
|
|
|
|
companyId: string,
|
2026-04-07 18:26:34 -05:00
|
|
|
extra?: {
|
|
|
|
|
dismissals?: ReadonlyMap<string, number>;
|
|
|
|
|
joinRequests?: Array<{ id: string; updatedAt: Date | string | null; createdAt: Date | string }>;
|
|
|
|
|
unreadTouchedIssues?: number;
|
|
|
|
|
},
|
2026-02-26 16:33:39 -06:00
|
|
|
): Promise<SidebarBadges> => {
|
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
|
|
|
const actionableApprovals = await db
|
2026-04-07 18:26:34 -05:00
|
|
|
.select({ id: approvals.id, updatedAt: approvals.updatedAt })
|
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
|
|
|
.from(approvals)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(approvals.companyId, companyId),
|
|
|
|
|
inArray(approvals.status, ACTIONABLE_APPROVAL_STATUSES),
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-04-07 18:26:34 -05:00
|
|
|
.then((rows) =>
|
|
|
|
|
rows.filter((row) => !isDismissed(extra?.dismissals ?? new Map(), `approval:${row.id}`, row.updatedAt)).length
|
|
|
|
|
);
|
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
|
|
|
|
2026-02-20 10:32:17 -06:00
|
|
|
const latestRunByAgent = await db
|
|
|
|
|
.selectDistinctOn([heartbeatRuns.agentId], {
|
2026-04-07 18:26:34 -05:00
|
|
|
id: heartbeatRuns.id,
|
2026-02-20 10:32:17 -06:00
|
|
|
runStatus: heartbeatRuns.status,
|
2026-04-07 18:26:34 -05:00
|
|
|
createdAt: heartbeatRuns.createdAt,
|
2026-02-20 10:32:17 -06:00
|
|
|
})
|
|
|
|
|
.from(heartbeatRuns)
|
|
|
|
|
.innerJoin(agents, eq(heartbeatRuns.agentId, agents.id))
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(heartbeatRuns.companyId, companyId),
|
|
|
|
|
eq(agents.companyId, companyId),
|
|
|
|
|
not(eq(agents.status, "terminated")),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.orderBy(heartbeatRuns.agentId, desc(heartbeatRuns.createdAt));
|
|
|
|
|
|
|
|
|
|
const failedRuns = latestRunByAgent.filter((row) =>
|
2026-04-07 18:26:34 -05:00
|
|
|
FAILED_HEARTBEAT_STATUSES.includes(row.runStatus)
|
|
|
|
|
&& !isDismissed(extra?.dismissals ?? new Map(), `run:${row.id}`, row.createdAt),
|
2026-02-20 10:32:17 -06:00
|
|
|
).length;
|
|
|
|
|
|
2026-04-07 18:26:34 -05:00
|
|
|
const joinRequests = (extra?.joinRequests ?? []).filter((row) =>
|
|
|
|
|
!isDismissed(
|
|
|
|
|
extra?.dismissals ?? new Map(),
|
|
|
|
|
`join:${row.id}`,
|
|
|
|
|
row.updatedAt ?? row.createdAt,
|
|
|
|
|
)
|
|
|
|
|
).length;
|
2026-03-06 08:21:03 -06:00
|
|
|
const unreadTouchedIssues = extra?.unreadTouchedIssues ?? 0;
|
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
|
|
|
return {
|
2026-03-06 08:21:03 -06:00
|
|
|
inbox: actionableApprovals + failedRuns + joinRequests + unreadTouchedIssues,
|
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
|
|
|
approvals: actionableApprovals,
|
2026-02-20 10:32:17 -06:00
|
|
|
failedRuns,
|
2026-02-23 14:40:32 -06:00
|
|
|
joinRequests,
|
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
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|