Files
PaperClipAI/ui/src/lib/responsibility-automation.ts
T
43b2d8be22 feat(dashboard): add an experimental areas and responsibilities dashboard (#22069)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The dashboard is the first page a person sees in a company
> - The classic dashboard shows agents, runs, costs and recent tasks
> - It does not show what the person is responsible for, or if that work
goes well
> - People think in areas, such as Fundraising or Customers, and in the
things they own in each area
> - This pull request adds a dashboard built on areas and
responsibilities, behind an experimental setting that is off by default
> - The benefit is that a person sees what they own, how it is going,
and what agents do for it, on one page

## Linked Issues or Issue Description

No public issue exists for this work. The description follows the
feature request template.

**Subsystem affected**

Dashboard, navigation, the task conversation, routines, and the instance
experimental settings.

**Problem or motivation**

The classic dashboard counts agents and tasks. It does not say what a
person owns or if it goes well. People who run a company with agents
want to see their areas, the responsibilities in each area, the bar each
one must meet, and the agents and routines that serve it.

**Proposed solution**

A person owns responsibilities. Each responsibility belongs to an area
and has a bar, a number to reach. Projects, tasks and routines link to a
responsibility, and the page shows its status from those links. An agent
can interview the person and draft the dashboard. The person accepts or
declines each change, and any change can be undone from a saved version.

**Alternatives considered**

Add the same data to the classic dashboard. That page is a fixed set of
cards for company activity, so a second, person-centred page is clearer.
The setting lets a person pick one of the two.

**Roadmap alignment**

This is an experimental feature behind `enableResponsibilities`. It is
off by default and changes nothing for a person who leaves it off.

## What Changed

- Add the setting "Areas & Responsibilities dashboard"
(`enableResponsibilities`, off by default). On, Dashboard in the sidebar
opens the new page, and the classic dashboard link is not shown in the
sidebars or the command palette. Off, nothing changes.
- Add tables and migrations 0492 to 0497 for areas, responsibilities,
links to work, metrics, status reports, saved dashboard versions, and
model-call records for summaries.
- Add the API: areas, responsibilities, links, bars, metrics, status
reports, summaries, runs, automation, and the dashboard conversation
with versions and undo. Each route checks company access and writes to
the activity log.
- Add four task conversations with an agent: about a bar, about an
automation, about a routine, and about the whole dashboard. The agent
proposes a change. Nothing applies until a person accepts it.
- Add standing checks: routines that an agent carries out on its
heartbeat and reports.
- Add the Dashboard page: search, counts, areas that open and close, a
primary New area button, and a message box at the bottom of the page.
Add the responsibility page with its bar, metrics, work and routines.
- Add the Paperclip skill sections and runner API entries that let an
agent use these routes. The runner skill inventory is regenerated (166
to 174 rows).
- Keep the new runner API tests in their own file so
`runner-api.test.ts` stays at 985 lines.
- Onboarding is not changed. The onboarding changes that build on this
work are a separate pull request.

## Verification

- Type checks pass for `packages/db`, `packages/shared`, `server` (all
layers) and `ui`.
- `pnpm --filter @paperclipai/db check:migrations` passes. A new
`drizzle-kit generate` run produces no further change.
- Tests for every changed file pass: 26 server test files (3,009 tests),
116 UI test files (1,991 tests) and 28 shared test files (432 tests).
- The runner capability inventory checks pass.
- I started the app on a local database that already held earlier data.
Master's new migrations and these migrations applied without error. With
the setting on, the Dashboard and a responsibility page loaded with no
console or server errors. The More menu and the command palette show one
Dashboard.
- I did not run `pnpm verify`, the full local suite, or the browser
suites. Required CI covers them.
- The agent conversations are tested with simulated agents. No real
model has run them.

## Risks

- Medium. The change is large, and the setting is off by default. With
it off, the new routes refuse requests and the UI is unchanged.
- Migrations only add tables and columns. Two replace a CHECK
constraint, and one builds an index `CONCURRENTLY`. These two have the
`online_compatible` override with a reason in the file, and the second
one is listed in `MIGRATIONS_WITH_TOP_LEVEL_STATEMENTS`.
- Master can add migrations before this merges. The migrations then need
new numbers.
- `responsibilities.ts` (1,663 lines), `responsibility-summaries.ts`
(1,239 lines) and one Storybook fixture file (1,305 lines) are new files
over 1,000 lines. They each keep one cohesive unit together: the service
for one table family, the model-call path for summaries, and the
fixtures that the stories share. No existing file crosses 1,000 lines.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Anthropic Claude: Claude Fable 5.2 (`claude-fable-5-2`) wrote most of
the change, with Claude Sonnet 5.5 (`claude-sonnet-5-5`) for the final
steps. Both used tool use, code execution and browser checks.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Scott Tong <scott@scottsmbpm5max.lan>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 20:45:44 -08:00

312 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
AUTOMATION_TRIGGER_LABEL,
RESPONSIBILITY_AUTOMATION_LEVELS,
responsibilityAutomationPercent,
type AutomationCandidate,
type AutomationStart,
type ResponsibilityArea,
type ResponsibilityAutomation,
type ResponsibilityAutomationAreaTotals,
type ResponsibilityAutomationItem,
type ResponsibilityAutomationLevel,
type ResponsibilityAutomationOverview,
type ResponsibilityAutomationStalled,
type ResponsibilityWithRollup,
} from "@paperclipai/shared";
import { describeReadingAge } from "./responsibilities";
import { outlineAreaGroups, type OutlineAreaGroup } from "./responsibility-views";
/**
* Derivations for the Dashboard's Automation view and the automation line on
* a responsibility page (experimental, `enableResponsibilities`). The rule
* itself (levels, share, ranking, the trigger hint) is in `@paperclipai/shared`;
* this joins the server's automation read with the overview the page already
* has and puts it in words. Pure, so the view stays a plain renderer.
*/
function count(n: number, singular: string, plural: string): string {
return `${n} ${n === 1 ? singular : plural}`;
}
/** "40% automated", or null with no share. */
export function automationShareLabel(share: number | null): string | null {
const percent = responsibilityAutomationPercent(share);
return percent === null ? null : `${percent}% automated`;
}
/** "2 automations · 5 manual tasks": the counts in words. */
export function automationSplitWords(automations: number, manualTasks: number): string {
return `${count(automations, "automation", "automations")} · ${count(manualTasks, "manual task", "manual tasks")}`;
}
/**
* The counts under the meter, or why there are none ("Nothing is filed under
* this yet.").
*/
export function automationCountsLine(automation: Pick<ResponsibilityAutomation, "automationCount" | "manualTaskCount" | "nothingFiled">): string {
if (automation.nothingFiled) return "Nothing is filed under this yet.";
if (automation.automationCount + automation.manualTaskCount === 0) return "No automations and no recent manual tasks.";
return automationSplitWords(automation.automationCount, automation.manualTaskCount);
}
/** Each responsibility's automation, by id. */
export function automationById(read: Pick<ResponsibilityAutomationOverview, "responsibilities">): Map<string, ResponsibilityAutomation> {
return new Map(read.responsibilities.map((item) => [item.responsibilityId, item]));
}
export interface AutomationAreaGroup extends OutlineAreaGroup {
/** "40% automated" for the area, counting each automation and task once. */
automationShare: string | null;
}
/**
* The Automation view's groups: the Board's areas, and inside each area the
* most manual responsibilities first (lowest share, with no share counting
* as none; then the most manual tasks; then the area's own order). A
* responsibility the read does not cover yet (just added) counts as manual.
* `levels`, when given, keeps only responsibilities at those levels and drops
* areas left empty.
*/
export function automationAreaGroups(
areas: readonly Pick<ResponsibilityArea, "id" | "title" | "status">[],
responsibilities: readonly ResponsibilityWithRollup[],
read: Pick<ResponsibilityAutomationOverview, "responsibilities" | "areas">,
levels?: ReadonlySet<ResponsibilityAutomationLevel> | null,
): AutomationAreaGroup[] {
const byId = automationById(read);
const areaTotals = new Map<string | null, ResponsibilityAutomationAreaTotals>(read.areas.map((area) => [area.areaId, area]));
const known = new Set(areas.map((area) => area.id));
const order = (item: ResponsibilityWithRollup, index: number) => {
const automation = byId.get(item.id);
return { item, index, share: automation?.share ?? 0, manual: automation?.manualTaskCount ?? 0 };
};
return outlineAreaGroups(areas, responsibilities).flatMap((group) => {
const items = group.items
.map(order)
.sort((a, b) => a.share - b.share || b.manual - a.manual || a.index - b.index)
.map((entry) => entry.item)
.filter((item) => !levels || levels.size === 0 || levels.has(byId.get(item.id)?.level ?? "manual"));
if (items.length === 0) return [];
// Responsibilities in an area the page does not list are grouped as "Not in an area", like the Board.
const areaKey = group.area && known.has(group.area.id) ? group.area.id : null;
const totals = areaTotals.get(areaKey) ?? null;
return [{ ...group, items, automationShare: totals ? automationShareLabel(totals.share) : null }];
});
}
/** How many responsibilities are at each level, among those listed. */
export function automationLevelCounts(
responsibilities: readonly Pick<ResponsibilityWithRollup, "id">[],
read: Pick<ResponsibilityAutomationOverview, "responsibilities">,
): Record<ResponsibilityAutomationLevel, number> {
const byId = automationById(read);
const counts = Object.fromEntries(RESPONSIBILITY_AUTOMATION_LEVELS.map((level) => [level, 0])) as Record<ResponsibilityAutomationLevel, number>;
for (const item of responsibilities) counts[byId.get(item.id)?.level ?? "manual"] += 1;
return counts;
}
/** Distinct manual tasks not marked "doesn't repeat": what could still be automated. */
export function remainingCandidateCount(
read: Pick<ResponsibilityAutomationOverview, "responsibilities">,
dismissed: ReadonlySet<string>,
): number {
const ids = new Set<string>();
for (const item of read.responsibilities) for (const id of item.manualTaskIds) if (!dismissed.has(id)) ids.add(id);
return ids.size;
}
/**
* The overall sentence: "9 of your 20 responsibilities have nothing automated
* yet; 14 manual tasks could be automated." Reads sensibly at the ends.
*/
export function automationSentence(input: { total: number; manual: number; candidates: number }): string {
const { total, manual, candidates } = input;
if (total === 0) return "You do not own any responsibilities yet.";
const tail = candidates > 0
? `${count(candidates, "manual task", "manual tasks")} could be automated.`
: null;
if (manual === 0) {
return tail ? `Every responsibility you own has something automated; ${tail}` : "Everything you own is automated.";
}
const lead = manual === total
? total === 1 ? "Your responsibility has nothing automated yet" : `None of your ${total} responsibilities has anything automated yet`
: `${manual} of your ${total} responsibilities ${manual === 1 ? "has" : "have"} nothing automated yet`;
return tail ? `${lead}; ${tail}` : `${lead}.`;
}
type AgentName = (id: string) => string | undefined;
function agentLabel(id: string | null | undefined, agentName: AgentName, fallback = "an agent"): string {
return (id ? agentName(id) : undefined) ?? fallback;
}
/** "3 minutes ago", "2 hours ago", else the day words ("yesterday", "4 days ago"). */
export function describeCheckAge(at: Date | string, now: Date): string {
const minutes = Math.floor((now.getTime() - new Date(at).getTime()) / 60_000);
if (!Number.isFinite(minutes) || minutes < 1) return "just now";
if (minutes < 60) return `${minutes} ${minutes === 1 ? "minute" : "minutes"} ago`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours} ${hours === 1 ? "hour" : "hours"} ago`;
return describeReadingAge(at, now);
}
/** How an automation starts, in words: the kind and its detail. */
export interface AutomationStartWords {
kind: AutomationStart["kind"];
/** "On a schedule", "From a webhook", "Standing check", "On request". */
label: string;
/** "Every Monday at 9 AM", "on each wake-up", "when someone runs it". */
detail: string | null;
/** A second quiet line: the last check, the last call, the next run. Null when there is nothing to say. */
status: string | null;
}
/** "On a schedule · Every Monday at 9 AM", "Standing check · on each wake-up", with the last thing that happened. */
export function automationStartWords(item: Pick<ResponsibilityAutomationItem, "start">, now: Date): AutomationStartWords {
const { start } = item;
switch (start.kind) {
case "schedule":
return {
kind: start.kind,
label: AUTOMATION_TRIGGER_LABEL.schedule,
detail: start.cadenceLabel,
status: null,
};
case "webhook":
return {
kind: start.kind,
label: AUTOMATION_TRIGGER_LABEL.webhook,
detail: "when another system calls it",
status: start.lastDeliveryAt ? `Last called ${describeCheckAge(start.lastDeliveryAt, now)}` : "Not called yet",
};
case "standing_check": {
const last = start.lastResult;
return {
kind: start.kind,
label: AUTOMATION_TRIGGER_LABEL.standing_check,
detail: "on each wake-up",
status: last
? `Last checked ${describeCheckAge(last.checkedAt, now)}: ${last.outcome === "acted" ? `acted${last.note ? ` (${last.note})` : ""}` : "nothing needed"}`
: "Not checked yet",
};
}
case "on_request":
return { kind: start.kind, label: "On request", detail: "when someone runs it", status: null };
}
}
/** Why an automation filed here is not running, in a few words. */
export function stalledWords(stalled: Pick<ResponsibilityAutomationStalled, "reason" | "assigneeAgentId">, agentName: AgentName): string {
switch (stalled.reason) {
case "paused":
return "Paused";
case "no_agent":
return "No agent to run it";
case "no_wakeups":
return `Not running: ${agentLabel(stalled.assigneeAgentId, agentName, "its agent")} doesn’t wake up on its own (heartbeat off)`;
}
}
/**
* Why a task is listed when the hint has nothing to say, in a few words, or
* null: "Analyst offered: Every Monday at 9 AM", "Done by Analyst 3 days ago",
* "Done by hand yesterday", "With Analyst now".
*/
export function candidateReasonWords(candidate: AutomationCandidate, agentName: AgentName, now: Date): string | null {
switch (candidate.reason) {
case "pending_offer": {
const offer = candidate.pendingOffer;
if (!offer) return "An agent offered to repeat it";
return `${agentName(offer.agentId ?? "") ?? "Your agent"} offered: ${offer.cadenceLabel}`;
}
case "hint":
return candidate.hint?.why ?? null;
case "done_by_agent":
return `Done by ${agentLabel(candidate.assigneeAgentId, agentName)} ${describeReadingAge(candidate.completedAt ?? candidate.updatedAt, now)}`;
case "done":
return `Done by hand ${describeReadingAge(candidate.completedAt ?? candidate.updatedAt, now)}`;
case "with_agent":
return `With ${agentLabel(candidate.assigneeAgentId, agentName)} now`;
case "open":
return null;
}
}
/** The primary action's label: start, continue, or review the agent's proposal. */
export function conversationButtonLabel(candidate: Pick<AutomationCandidate, "conversation" | "responsibleAgentId">, agentName: AgentName): string {
const who = agentLabel(candidate.responsibleAgentId, agentName, "your agent");
if (candidate.conversation?.state === "proposal_waiting") return `Review ${who}’s proposal`;
if (candidate.conversation?.state === "discussing") return `Continue with ${who}`;
return `Automate with ${who}`;
}
/**
* When nothing is left to suggest, something true and short to say instead,
* or null to say nothing (nothing filed is said above).
*/
export function noCandidatesWords(automation: Pick<ResponsibilityAutomation, "automationCount" | "manualTaskCount" | "nothingFiled">): string | null {
if (automation.nothingFiled) return null;
if (automation.manualTaskCount > 0) return "Nothing else here looks worth automating.";
if (automation.automationCount > 0) return "The automations are covering the work.";
return "No recent manual tasks to automate.";
}
// ---------------------------------------------------------------------------
// "Doesn't repeat", remembered in this browser
// ---------------------------------------------------------------------------
/**
* Prototype only: which suggested tasks the person said do not repeat is kept
* in this browser's local storage, per company, so the list stops suggesting
* them here. It is not shared across browsers or people, and the page works
* the same (suggesting them again) when storage is blocked.
*/
export function automationDismissedStorageKey(companyId: string): string {
return `paperclip:responsibilities:automation-dismissed:${companyId}`;
}
export function readDismissedCandidates(companyId: string): Set<string> {
try {
const raw = globalThis.localStorage?.getItem(automationDismissedStorageKey(companyId));
const parsed: unknown = raw ? JSON.parse(raw) : [];
return new Set(Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === "string") : []);
} catch {
return new Set();
}
}
export function writeDismissedCandidates(companyId: string, ids: ReadonlySet<string>): void {
try {
globalThis.localStorage?.setItem(automationDismissedStorageKey(companyId), JSON.stringify([...ids]));
} catch {
// Blocked storage: the choice lasts for this visit only.
}
}
/** A candidate worth showing up front: the hint has a suggestion, an agent offered, or a conversation is open. */
function isRelevant(candidate: AutomationCandidate): boolean {
return candidate.hint !== null || candidate.pendingOffer !== null || candidate.conversation !== null;
}
/**
* The manual tasks a row shows: up to `limit` relevant ones up front (or, with
* none relevant, the first one, offered to work out with the agent); the rest
* of those the read listed behind "and N more"; and how many more the read
* only counted (reachable on the responsibility's page). Dismissed tasks are
* left out everywhere.
*/
export function visibleCandidates(
automation: Pick<ResponsibilityAutomation, "candidates" | "candidateTotal" | "manualTaskIds">,
dismissed: ReadonlySet<string>,
limit = 3,
): { shown: AutomationCandidate[]; others: AutomationCandidate[]; unlisted: number; more: number } {
const open = automation.candidates.filter((candidate) => !dismissed.has(candidate.issueId));
const relevant = open.filter(isRelevant);
const shown = (relevant.length > 0 ? relevant : open).slice(0, relevant.length > 0 ? limit : 1);
const shownIds = new Set(shown.map((candidate) => candidate.issueId));
const others = open.filter((candidate) => !shownIds.has(candidate.issueId));
const dismissedHere = automation.manualTaskIds.filter((id) => dismissed.has(id)).length;
const remaining = Math.max(0, automation.candidateTotal - dismissedHere - shown.length);
return { shown, others, unlisted: Math.max(0, remaining - others.length), more: remaining };
}