@microvac/plugin-decisions-native 0.0.0-stage → 0.1.0-rc.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/lib/client.js +808 -0
- package/package.json +40 -4
- package/src/client.tsx +44 -0
- package/src/fold.ts +132 -0
- package/src/index.js +19 -0
- package/src/keyboard.ts +133 -0
- package/src/microvac-ui.d.ts +64 -0
- package/src/model.ts +300 -0
- package/src/page.tsx +441 -0
- package/src/preferences.ts +39 -0
- package/src/types.ts +58 -0
- package/src/useNativeDecisions.ts +62 -0
- package/README.md +0 -3
package/src/model.ts
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import type { AttentionItem, AttentionProjectRef, AttentionSourceKind } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The native Decisions page's list (`docs/microvac/decisions.md`), as pure
|
|
5
|
+
* functions: which attention cards are decisions, who they are for, how they
|
|
6
|
+
* group and sort, and the snooze times. No React, no network.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Decisions (see `GLOSSARY.md`): the cards an agent asks a person to answer,
|
|
11
|
+
* plus the native approvals (hire and other approvals, budget, join request).
|
|
12
|
+
* Blocked chains, failed runs, recovery actions and reviews are not decisions.
|
|
13
|
+
*/
|
|
14
|
+
export const DECISION_SOURCE_KINDS: readonly AttentionSourceKind[] = Object.freeze([
|
|
15
|
+
"issue_thread_interaction",
|
|
16
|
+
"decision",
|
|
17
|
+
"approval",
|
|
18
|
+
"budget_alert",
|
|
19
|
+
"join_request",
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
/** Attention feed views the page merges: addressed to the viewer, unassigned, addressed to the company. */
|
|
23
|
+
export const DECISION_FEED_VIEWS = ["mine", "unassigned", "company"] as const;
|
|
24
|
+
export type DecisionFeedView = (typeof DECISION_FEED_VIEWS)[number];
|
|
25
|
+
export type DecisionFeeds = Partial<Record<DecisionFeedView, readonly AttentionItem[] | undefined>>;
|
|
26
|
+
|
|
27
|
+
/** "for you": addressed to the viewer; "anyone": unassigned or addressed to the company. */
|
|
28
|
+
export type DecisionMarker = "for-you" | "anyone";
|
|
29
|
+
|
|
30
|
+
export interface NativeDecision {
|
|
31
|
+
/** `<sourceKind>:<subject id>`, the key of `?card=` and of decision triage. */
|
|
32
|
+
key: string;
|
|
33
|
+
item: AttentionItem;
|
|
34
|
+
marker: DecisionMarker;
|
|
35
|
+
/** End of the snooze when the card is snoozed now, else `null`. */
|
|
36
|
+
snoozedUntil: string | null;
|
|
37
|
+
/** Where the snooze is stored: decision triage, or an Inbox dismissal set elsewhere. */
|
|
38
|
+
snoozeSource: "triage" | "inbox" | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface DecisionList {
|
|
42
|
+
open: NativeDecision[];
|
|
43
|
+
snoozed: NativeDecision[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function decisionKey(item: Pick<AttentionItem, "sourceKind" | "subject">): string {
|
|
47
|
+
return `${item.sourceKind}:${item.subject.id}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Parses `?card=<kind>:<id>`; `null` when absent or malformed. */
|
|
51
|
+
export function parseCardParam(value: string | null | undefined): string | null {
|
|
52
|
+
if (!value) return null;
|
|
53
|
+
const at = value.indexOf(":");
|
|
54
|
+
return at > 0 && at < value.length - 1 ? value : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function time(value: string | null | undefined): number {
|
|
58
|
+
if (!value) return 0;
|
|
59
|
+
const parsed = Date.parse(value);
|
|
60
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function isDecisionItem(item: AttentionItem): boolean {
|
|
64
|
+
return DECISION_SOURCE_KINDS.includes(item.sourceKind);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** An automatic provider-limit retry waits on its own; the person has nothing to answer. */
|
|
68
|
+
function isAutomatic(item: AttentionItem): boolean {
|
|
69
|
+
return Boolean(item.providerLimit && item.providerLimit.state !== "needs_retry");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function toDecision(item: AttentionItem, marker: DecisionMarker, now: number): NativeDecision | null {
|
|
73
|
+
if (!isDecisionItem(item) || item.archivedAt || isAutomatic(item)) return null;
|
|
74
|
+
let snoozedUntil: string | null = null;
|
|
75
|
+
let snoozeSource: NativeDecision["snoozeSource"] = null;
|
|
76
|
+
if (item.snoozedUntil && time(item.snoozedUntil) > now) {
|
|
77
|
+
snoozedUntil = item.snoozedUntil;
|
|
78
|
+
snoozeSource = "triage";
|
|
79
|
+
}
|
|
80
|
+
const dismissal = item.dismissal;
|
|
81
|
+
if (dismissal?.isActive) {
|
|
82
|
+
// Archived for this person (Inbox or Close… menu): not a decision to show.
|
|
83
|
+
if (dismissal.kind !== "snooze") return null;
|
|
84
|
+
if (!snoozedUntil && dismissal.snoozedUntil && time(dismissal.snoozedUntil) > now) {
|
|
85
|
+
snoozedUntil = dismissal.snoozedUntil;
|
|
86
|
+
snoozeSource = "inbox";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { key: decisionKey(item), item, marker, snoozedUntil, snoozeSource };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* One list from the three audience views (fetched with `includeDismissed`, so
|
|
94
|
+
* snoozed cards come too). A card in several views shows once, marked "for
|
|
95
|
+
* you" when it is addressed to the viewer. Obligation evidence folds into its
|
|
96
|
+
* lead (`fold`, Microvac UI's `decisions.foldObligations`), as on the upstream page.
|
|
97
|
+
*/
|
|
98
|
+
export function collectDecisions(
|
|
99
|
+
feeds: DecisionFeeds,
|
|
100
|
+
now: number,
|
|
101
|
+
fold: (items: readonly AttentionItem[]) => { cards: AttentionItem[] } = (items) => ({ cards: [...items] }),
|
|
102
|
+
): DecisionList {
|
|
103
|
+
const byKey = new Map<string, NativeDecision>();
|
|
104
|
+
for (const view of DECISION_FEED_VIEWS) {
|
|
105
|
+
const items = feeds[view];
|
|
106
|
+
if (!items) continue;
|
|
107
|
+
const marker: DecisionMarker = view === "mine" ? "for-you" : "anyone";
|
|
108
|
+
for (const item of fold(items).cards) {
|
|
109
|
+
const decision = toDecision(item, marker, now);
|
|
110
|
+
if (!decision || byKey.has(decision.key)) continue;
|
|
111
|
+
byKey.set(decision.key, decision);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const open: NativeDecision[] = [];
|
|
115
|
+
const snoozed: NativeDecision[] = [];
|
|
116
|
+
for (const decision of byKey.values()) (decision.snoozedUntil ? snoozed : open).push(decision);
|
|
117
|
+
snoozed.sort((a, b) => time(a.snoozedUntil) - time(b.snoozedUntil) || a.key.localeCompare(b.key));
|
|
118
|
+
return { open, snoozed };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// --- Labels ---------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
const INTERACTION_TYPE_LABELS: Record<string, string> = {
|
|
124
|
+
ask_user_questions: "Question",
|
|
125
|
+
request_confirmation: "Confirmation",
|
|
126
|
+
request_checkbox_confirmation: "Confirmation",
|
|
127
|
+
request_item_verdicts: "Verdicts",
|
|
128
|
+
suggest_tasks: "Suggested tasks",
|
|
129
|
+
connection_intent: "Connection",
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const APPROVAL_TYPE_LABELS: Record<string, string> = {
|
|
133
|
+
hire_agent: "Hire",
|
|
134
|
+
budget_override_required: "Budget",
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/** The card's type, shown on the card and used by the "Type" grouping. */
|
|
138
|
+
export function decisionTypeLabel(item: AttentionItem): string {
|
|
139
|
+
switch (item.sourceKind) {
|
|
140
|
+
case "issue_thread_interaction": {
|
|
141
|
+
if (item.detail?.kind === "plan_approval") return "Plan review";
|
|
142
|
+
const kind = item.subject.metadata?.kind;
|
|
143
|
+
return (typeof kind === "string" && INTERACTION_TYPE_LABELS[kind]) || "Question";
|
|
144
|
+
}
|
|
145
|
+
case "decision":
|
|
146
|
+
return "Choice";
|
|
147
|
+
case "approval": {
|
|
148
|
+
const type = item.detail?.kind === "approval" ? item.detail.approvalType : item.subject.metadata?.type;
|
|
149
|
+
return (typeof type === "string" && APPROVAL_TYPE_LABELS[type]) || "Approval";
|
|
150
|
+
}
|
|
151
|
+
case "budget_alert":
|
|
152
|
+
return "Budget";
|
|
153
|
+
case "join_request":
|
|
154
|
+
return "Join request";
|
|
155
|
+
default:
|
|
156
|
+
return "Decision";
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The agent that asked, for the card and the "Agent" grouping. */
|
|
161
|
+
export function decisionAgentName(item: AttentionItem): string | null {
|
|
162
|
+
const name = item.originAgentName ?? item.requester?.name ?? null;
|
|
163
|
+
return name && name.trim() ? name.trim() : null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// --- Grouping and sort ------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
export const DECISION_GROUPINGS = ["project", "type", "agent", "none"] as const;
|
|
169
|
+
export type DecisionGrouping = (typeof DECISION_GROUPINGS)[number];
|
|
170
|
+
export const DEFAULT_GROUPING: DecisionGrouping = "project";
|
|
171
|
+
|
|
172
|
+
export const DECISION_SORTS = ["oldest", "newest"] as const;
|
|
173
|
+
export type DecisionSort = (typeof DECISION_SORTS)[number];
|
|
174
|
+
export const DEFAULT_SORT: DecisionSort = "oldest";
|
|
175
|
+
|
|
176
|
+
export const GROUPING_LABELS: Record<DecisionGrouping, string> = {
|
|
177
|
+
project: "Project",
|
|
178
|
+
type: "Type",
|
|
179
|
+
agent: "Agent",
|
|
180
|
+
none: "None",
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export interface DecisionGroup {
|
|
184
|
+
key: string;
|
|
185
|
+
label: string;
|
|
186
|
+
/** Set for project groups: the project's colour and icon. */
|
|
187
|
+
project: AttentionProjectRef | null;
|
|
188
|
+
decisions: NativeDecision[];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const NO_GROUP_KEY = "__none__";
|
|
192
|
+
const NO_PROJECT_LABEL = "No project";
|
|
193
|
+
const NO_AGENT_LABEL = "No agent";
|
|
194
|
+
|
|
195
|
+
/** When the card started waiting: its creation, else its last activity. */
|
|
196
|
+
export function decisionAskedAt(decision: NativeDecision): string {
|
|
197
|
+
return decision.item.createdAt || decision.item.activityAt;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function sortDecisions(decisions: readonly NativeDecision[], order: DecisionSort): NativeDecision[] {
|
|
201
|
+
const direction = order === "oldest" ? 1 : -1;
|
|
202
|
+
return [...decisions].sort(
|
|
203
|
+
(a, b) => direction * (time(decisionAskedAt(a)) - time(decisionAskedAt(b))) || a.key.localeCompare(b.key),
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function groupOf(decision: NativeDecision, grouping: DecisionGrouping): Omit<DecisionGroup, "decisions"> {
|
|
208
|
+
const { item } = decision;
|
|
209
|
+
switch (grouping) {
|
|
210
|
+
case "project":
|
|
211
|
+
return item.project
|
|
212
|
+
? { key: item.project.id, label: item.project.name, project: item.project }
|
|
213
|
+
: { key: NO_GROUP_KEY, label: NO_PROJECT_LABEL, project: null };
|
|
214
|
+
case "type": {
|
|
215
|
+
const label = decisionTypeLabel(item);
|
|
216
|
+
return { key: label, label, project: null };
|
|
217
|
+
}
|
|
218
|
+
case "agent": {
|
|
219
|
+
const name = decisionAgentName(item);
|
|
220
|
+
return name ? { key: name, label: name, project: null } : { key: NO_GROUP_KEY, label: NO_AGENT_LABEL, project: null };
|
|
221
|
+
}
|
|
222
|
+
default:
|
|
223
|
+
return { key: NO_GROUP_KEY, label: "", project: null };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Groups sorted cards. Groups follow their first card in the chosen order;
|
|
229
|
+
* cards without a project (or agent) go last. "None" gives one unnamed group.
|
|
230
|
+
*/
|
|
231
|
+
export function groupDecisions(
|
|
232
|
+
decisions: readonly NativeDecision[],
|
|
233
|
+
grouping: DecisionGrouping,
|
|
234
|
+
order: DecisionSort,
|
|
235
|
+
): DecisionGroup[] {
|
|
236
|
+
const groups = new Map<string, DecisionGroup>();
|
|
237
|
+
for (const decision of sortDecisions(decisions, order)) {
|
|
238
|
+
const head = groupOf(decision, grouping);
|
|
239
|
+
let group = groups.get(head.key);
|
|
240
|
+
if (!group) {
|
|
241
|
+
group = { ...head, decisions: [] };
|
|
242
|
+
groups.set(head.key, group);
|
|
243
|
+
}
|
|
244
|
+
group.decisions.push(decision);
|
|
245
|
+
}
|
|
246
|
+
const list = [...groups.values()];
|
|
247
|
+
if (grouping === "none") return list;
|
|
248
|
+
const rest = list.filter((group) => group.key !== NO_GROUP_KEY);
|
|
249
|
+
const missing = list.find((group) => group.key === NO_GROUP_KEY);
|
|
250
|
+
return missing ? [...rest, missing] : rest;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The cards in the order the page shows them, for J/K and "open the next one". */
|
|
254
|
+
export function displayOrder(groups: readonly DecisionGroup[]): NativeDecision[] {
|
|
255
|
+
return groups.flatMap((group) => group.decisions);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The card to open after `answeredKey` left the list: the one that followed
|
|
260
|
+
* it, else the one before it, else none.
|
|
261
|
+
*/
|
|
262
|
+
export function nextOpenKey(previousOrder: readonly string[], answeredKey: string, remaining: readonly string[]): string | null {
|
|
263
|
+
const left = new Set(remaining);
|
|
264
|
+
const at = previousOrder.indexOf(answeredKey);
|
|
265
|
+
if (at >= 0) {
|
|
266
|
+
for (let index = at + 1; index < previousOrder.length; index++) if (left.has(previousOrder[index]!)) return previousOrder[index]!;
|
|
267
|
+
for (let index = at - 1; index >= 0; index--) if (left.has(previousOrder[index]!)) return previousOrder[index]!;
|
|
268
|
+
}
|
|
269
|
+
return remaining[0] ?? null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// --- Snooze -------------------------------------------------------------------
|
|
273
|
+
|
|
274
|
+
export interface SnoozePreset {
|
|
275
|
+
key: "3h" | "tomorrow" | "monday" | "week";
|
|
276
|
+
label: string;
|
|
277
|
+
until: Date;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const HOUR_MS = 3_600_000;
|
|
281
|
+
|
|
282
|
+
/** The snooze choices, in the browser's local time. */
|
|
283
|
+
export function snoozePresets(now: Date): SnoozePreset[] {
|
|
284
|
+
const inThreeHours = new Date(now.getTime() + 3 * HOUR_MS);
|
|
285
|
+
const tomorrow = new Date(now);
|
|
286
|
+
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
287
|
+
tomorrow.setHours(9, 0, 0, 0);
|
|
288
|
+
const monday = new Date(now);
|
|
289
|
+
// Next Monday, a week ahead when today is Monday.
|
|
290
|
+
const daysToMonday = (8 - monday.getDay()) % 7 || 7;
|
|
291
|
+
monday.setDate(monday.getDate() + daysToMonday);
|
|
292
|
+
monday.setHours(9, 0, 0, 0);
|
|
293
|
+
const week = new Date(now.getTime() + 7 * 24 * HOUR_MS);
|
|
294
|
+
return [
|
|
295
|
+
{ key: "3h", label: "3 hours", until: inThreeHours },
|
|
296
|
+
{ key: "tomorrow", label: "Tomorrow 9:00", until: tomorrow },
|
|
297
|
+
{ key: "monday", label: "Monday 9:00", until: monday },
|
|
298
|
+
{ key: "week", label: "One week", until: week },
|
|
299
|
+
];
|
|
300
|
+
}
|