@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/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@microvac/plugin-decisions-native",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-rc.17",
4
+ "license": "MIT",
5
+ "description": "Microvac plugin: the answer-first Decisions page and interaction card.",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./src/index.js",
9
+ "./client": "./lib/client.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "src",
14
+ "lib"
15
+ ],
16
+ "microvac": {
17
+ "ui": "^1.1.0"
18
+ },
19
+ "dsh": {
20
+ "client": {
21
+ "platform": "web",
22
+ "inject": [
23
+ "pages"
24
+ ],
25
+ "external": [
26
+ "@microvac/ui"
27
+ ],
28
+ "immediately": true
29
+ }
30
+ },
31
+ "dependencies": {
32
+ "@deepseek-ai/schemastery": "3.20.0-alpha.2"
33
+ },
34
+ "scripts": {
35
+ "build:client": "node ../../scripts/microvac/build-client-plugin.mjs .",
36
+ "typecheck": "tsc --noEmit"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/microvac-ai/microvac"
41
+ }
42
+ }
package/src/client.tsx ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Client face of the `decisions-native` plugin (`docs/microvac/decisions.md`):
3
+ * the Microvac Decisions page and the answer-first interaction card, given to
4
+ * the page slots, plus the page's navigation count. Answer forms are
5
+ * upstream's, reached through Microvac UI (`decisions.AnswerForm`).
6
+ */
7
+ import { useRef } from "react";
8
+ import { upstream } from "@microvac/ui";
9
+ import { useQuestionFold } from "./fold.js";
10
+ import { DecisionsPage } from "./page.js";
11
+ import type { InteractionCardProps } from "./types.js";
12
+ import { useNativeDecisionCount } from "./useNativeDecisions.js";
13
+
14
+ export const name = "@microvac/plugin-decisions-native";
15
+ export const inject = ["pages"];
16
+
17
+ interface PagesContext {
18
+ pages: {
19
+ provide(
20
+ slot: string,
21
+ occupant: { id: string; component: unknown; useCount?: (context: { companyId: string | null }) => number | undefined },
22
+ ): () => void;
23
+ };
24
+ }
25
+
26
+ /**
27
+ * The issue thread card: a pending decision shows answer-first, as on the
28
+ * Decisions page, its long question folded. The wrapper has no box of its own.
29
+ */
30
+ export function AnswerFirstInteractionCard(props: InteractionCardProps) {
31
+ const pending = props.interaction?.status === "pending";
32
+ const ref = useRef<HTMLDivElement>(null);
33
+ useQuestionFold(ref, pending);
34
+ return (
35
+ <div ref={ref} className="contents">
36
+ <upstream.InteractionCard {...props} presentationVariant={pending ? "coordinator-decision" : props.presentationVariant} />
37
+ </div>
38
+ );
39
+ }
40
+
41
+ export function apply(ctx: PagesContext) {
42
+ ctx.pages.provide("decisions", { id: name, component: DecisionsPage, useCount: useNativeDecisionCount });
43
+ ctx.pages.provide("interaction-card", { id: name, component: AnswerFirstInteractionCard });
44
+ }
package/src/fold.ts ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Folding of a long question on the answer-first card (`docs/microvac/decisions.md`,
3
+ * "answer controls visible without scrolling on a 400 px phone"). The answer
4
+ * forms are upstream's: their compact layout marks the ask
5
+ * (`data-decision-prompt`), the agent's prompt under it (`data-decision-note`)
6
+ * and the context panel (`data-needs-you-panel`). Each one longer than four
7
+ * lines is clamped, with a "Show more" toggle right under it, so the answer
8
+ * controls follow at once. Only attributes and toggles of our own are written,
9
+ * and only when they differ, so a caller observing mutations does not loop.
10
+ */
11
+ import { useEffect, type RefObject } from "react";
12
+
13
+ /** `folded` (clamped, fits), `overflow` (clamped, longer: faded, with a toggle) or `expanded`. */
14
+ export const FOLD_ATTRIBUTE = "data-microvac-fold";
15
+ /** Marks the "Show more" / "Show less" button the fold adds under a question. */
16
+ export const FOLD_TOGGLE_ATTRIBUTE = "data-microvac-fold-toggle";
17
+
18
+ const QUESTION_SELECTOR = "[data-decision-prompt], [data-decision-note], [data-needs-you-panel]";
19
+ /** A panel holding answer controls is the form itself: never folded. */
20
+ const CONTROL_SELECTOR = "input, textarea, select, [role='radio'], [role='checkbox'], [role='radiogroup'], [role='textbox']";
21
+ const TOGGLE_CLASS = "block text-xs text-muted-foreground underline-offset-2 hover:underline";
22
+ const STYLE_ID = "microvac-decisions-fold";
23
+
24
+ export const FOLD_CSS = `
25
+ [${FOLD_ATTRIBUTE}="folded"], [${FOLD_ATTRIBUTE}="overflow"] { max-height: 4lh; overflow: hidden; }
26
+ [data-needs-you-panel][${FOLD_ATTRIBUTE}="folded"], [data-needs-you-panel][${FOLD_ATTRIBUTE}="overflow"] { max-height: calc(4lh + 1.5rem); }
27
+ [${FOLD_ATTRIBUTE}="overflow"] { mask-image: linear-gradient(to bottom, #000 55%, transparent); }
28
+ `;
29
+
30
+ type FoldState = "folded" | "overflow" | "expanded";
31
+
32
+ function questions(container: ParentNode): HTMLElement[] {
33
+ const found = [...container.querySelectorAll<HTMLElement>(QUESTION_SELECTOR)];
34
+ return found.filter((element) => {
35
+ if (element.matches("[data-needs-you-panel]") && element.querySelector(CONTROL_SELECTOR)) return false;
36
+ // A question inside another one folds with it.
37
+ return !found.some((other) => other !== element && other.contains(element));
38
+ });
39
+ }
40
+
41
+ function setAttribute(element: Element, name: string, value: string) {
42
+ if (element.getAttribute(name) !== value) element.setAttribute(name, value);
43
+ }
44
+
45
+ function setText(element: Element, text: string) {
46
+ if (element.textContent !== text) element.textContent = text;
47
+ }
48
+
49
+ function isToggle(element: Element | null): element is HTMLButtonElement {
50
+ return Boolean(element?.hasAttribute(FOLD_TOGGLE_ATTRIBUTE));
51
+ }
52
+
53
+ function onToggle(event: Event) {
54
+ const toggle = event.currentTarget as HTMLButtonElement;
55
+ const question = toggle.previousElementSibling;
56
+ if (!question?.hasAttribute(FOLD_ATTRIBUTE)) return;
57
+ const expanded = question.getAttribute(FOLD_ATTRIBUTE) === "expanded";
58
+ question.setAttribute(FOLD_ATTRIBUTE, expanded ? "overflow" : "expanded");
59
+ syncToggle(toggle, !expanded);
60
+ }
61
+
62
+ function syncToggle(toggle: HTMLButtonElement, expanded: boolean) {
63
+ setText(toggle, expanded ? "Show less" : "Show more");
64
+ setAttribute(toggle, "aria-expanded", String(expanded));
65
+ }
66
+
67
+ function createToggle(document: Document): HTMLButtonElement {
68
+ const toggle = document.createElement("button");
69
+ toggle.type = "button";
70
+ toggle.className = TOGGLE_CLASS;
71
+ toggle.setAttribute(FOLD_TOGGLE_ATTRIBUTE, "");
72
+ toggle.addEventListener("click", onToggle);
73
+ return toggle;
74
+ }
75
+
76
+ /** Folds every long question in `container` and keeps its toggle right under it; idempotent. */
77
+ export function foldLongQuestions(container: ParentNode): void {
78
+ const kept = new Set<Element>();
79
+ for (const question of questions(container)) {
80
+ let state = question.getAttribute(FOLD_ATTRIBUTE) as FoldState | null;
81
+ if (state !== "folded" && state !== "overflow" && state !== "expanded") {
82
+ state = "folded";
83
+ question.setAttribute(FOLD_ATTRIBUTE, state);
84
+ }
85
+ // Measured while clamped: an expanded question was long when it was folded.
86
+ if (state !== "expanded") {
87
+ state = question.scrollHeight > question.clientHeight + 1 ? "overflow" : "folded";
88
+ setAttribute(question, FOLD_ATTRIBUTE, state);
89
+ }
90
+ if (state === "folded") continue;
91
+ const next = question.nextElementSibling;
92
+ let toggle: HTMLButtonElement;
93
+ if (isToggle(next)) {
94
+ toggle = next;
95
+ } else {
96
+ toggle = createToggle(question.ownerDocument);
97
+ question.after(toggle);
98
+ }
99
+ syncToggle(toggle, state === "expanded");
100
+ kept.add(toggle);
101
+ }
102
+ for (const toggle of container.querySelectorAll(`[${FOLD_TOGGLE_ATTRIBUTE}]`)) {
103
+ if (!kept.has(toggle)) toggle.remove();
104
+ }
105
+ }
106
+
107
+ function ensureFoldStyle(document: Document) {
108
+ if (document.getElementById(STYLE_ID)) return;
109
+ const style = document.createElement("style");
110
+ style.id = STYLE_ID;
111
+ style.textContent = FOLD_CSS;
112
+ document.head.appendChild(style);
113
+ }
114
+
115
+ /** Keeps the long questions under `ref` folded while `active`, as the upstream form renders and resizes. */
116
+ export function useQuestionFold(ref: RefObject<HTMLElement | null>, active = true): void {
117
+ useEffect(() => {
118
+ const root = ref.current;
119
+ if (!root || !active) return;
120
+ ensureFoldStyle(root.ownerDocument);
121
+ const fold = () => foldLongQuestions(root);
122
+ fold();
123
+ const mutations = typeof MutationObserver === "undefined" ? null : new MutationObserver(fold);
124
+ mutations?.observe(root, { childList: true, subtree: true, characterData: true });
125
+ const resizes = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(fold);
126
+ resizes?.observe(root);
127
+ return () => {
128
+ mutations?.disconnect();
129
+ resizes?.disconnect();
130
+ };
131
+ }, [ref, active]);
132
+ }
package/src/index.js ADDED
@@ -0,0 +1,19 @@
1
+ // @ts-check
2
+ /**
3
+ * Host face of the `decisions-native` plugin: nothing to configure or start.
4
+ * Its client face (`src/client.tsx`, built into `lib/client.js`) provides the
5
+ * Microvac Decisions page, its navigation count and the answer-first
6
+ * interaction card through the page slots (`docs/microvac/decisions.md`).
7
+ * Enable `decisions-classic` instead to get the upstream page back.
8
+ *
9
+ * # microvac.yml
10
+ * - id: decisions-native
11
+ * name: "@microvac/plugin-decisions-native"
12
+ */
13
+ import z from "@deepseek-ai/schemastery";
14
+
15
+ export const name = "@microvac/plugin-decisions-native";
16
+
17
+ export const Config = z.object({});
18
+
19
+ export function apply() {}
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Keyboard of the native Decisions page (desktop): 1–9 pick an option, Enter
3
+ * sends, J/K move between cards, S opens Snooze, ? shows the help. The
4
+ * answer forms are upstream's; the keys drive them through their DOM (the
5
+ * options are `role="radio"`/`role="checkbox"` buttons, the send button is
6
+ * the primary `data-slot="button"`), so no upstream component changes.
7
+ */
8
+
9
+ export type DecisionShortcut =
10
+ | { kind: "option"; index: number }
11
+ | { kind: "send" }
12
+ | { kind: "next" }
13
+ | { kind: "previous" }
14
+ | { kind: "snooze" }
15
+ | { kind: "help" }
16
+ | { kind: "close-help" };
17
+
18
+ export interface ShortcutHelpEntry {
19
+ keys: string[];
20
+ label: string;
21
+ }
22
+
23
+ export const SHORTCUT_HELP: readonly ShortcutHelpEntry[] = Object.freeze([
24
+ { keys: ["1", "…", "9"], label: "Pick an option" },
25
+ { keys: ["Enter"], label: "Send the answer" },
26
+ { keys: ["J"], label: "Next card" },
27
+ { keys: ["K"], label: "Previous card" },
28
+ { keys: ["S"], label: "Snooze the card" },
29
+ { keys: ["?"], label: "Show or hide this help" },
30
+ ]);
31
+
32
+ interface KeyLike {
33
+ key: string;
34
+ altKey?: boolean;
35
+ ctrlKey?: boolean;
36
+ metaKey?: boolean;
37
+ isComposing?: boolean;
38
+ target?: EventTarget | null;
39
+ }
40
+
41
+ const TEXT_INPUT_TYPES = new Set(["text", "search", "email", "url", "tel", "password", "number", "date", "datetime-local", "time", "month", "week"]);
42
+
43
+ /** Whether typing goes into this element: shortcuts stay off there. */
44
+ export function isTextEntryTarget(target: EventTarget | null | undefined): boolean {
45
+ if (!target || typeof (target as Element).closest !== "function") return false;
46
+ const element = target as HTMLElement;
47
+ if (element.isContentEditable || element.closest("[contenteditable=''], [contenteditable='true']")) return true;
48
+ const tag = element.tagName;
49
+ if (tag === "TEXTAREA" || tag === "SELECT") return true;
50
+ if (tag === "INPUT") return TEXT_INPUT_TYPES.has(((element as HTMLInputElement).type || "text").toLowerCase());
51
+ return element.getAttribute("role") === "textbox" || element.getAttribute("role") === "combobox";
52
+ }
53
+
54
+ /** The page action a key press asks for, or `null` when the key is not a shortcut here. */
55
+ export function decisionShortcut(event: KeyLike): DecisionShortcut | null {
56
+ if (event.altKey || event.ctrlKey || event.metaKey || event.isComposing) return null;
57
+ if (isTextEntryTarget(event.target)) return null;
58
+ const key = event.key;
59
+ if (key.length === 1 && key >= "1" && key <= "9") return { kind: "option", index: Number(key) - 1 };
60
+ switch (key) {
61
+ case "Enter": {
62
+ // A focused button or link already acts on Enter by itself.
63
+ const tag = (event.target as Element | null)?.tagName;
64
+ if (tag === "BUTTON" || tag === "A") return null;
65
+ return { kind: "send" };
66
+ }
67
+ case "j":
68
+ case "J":
69
+ return { kind: "next" };
70
+ case "k":
71
+ case "K":
72
+ return { kind: "previous" };
73
+ case "s":
74
+ case "S":
75
+ return { kind: "snooze" };
76
+ case "?":
77
+ return { kind: "help" };
78
+ case "Escape":
79
+ return { kind: "close-help" };
80
+ default:
81
+ return null;
82
+ }
83
+ }
84
+
85
+ /** True on a device whose main pointer is touch: no physical keyboard to hint at. */
86
+ export function isTouchDevice(): boolean {
87
+ if (typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
88
+ return window.matchMedia("(pointer: coarse)").matches && !window.matchMedia("(any-pointer: fine)").matches;
89
+ }
90
+
91
+ const OPTION_SELECTOR = "[role='radio'], [role='checkbox'], input[type='radio'], input[type='checkbox']";
92
+ const BUTTON_SELECTOR = "button[data-slot='button']";
93
+ /** Attribute the page sets on upstream controls to show their key hint. */
94
+ export const KBD_HINT_ATTRIBUTE = "data-microvac-kbd";
95
+
96
+ function enabled(element: Element): boolean {
97
+ return !(element as HTMLButtonElement).disabled && element.getAttribute("aria-disabled") !== "true";
98
+ }
99
+
100
+ /** The pickable options of an answer form, in reading order. */
101
+ export function answerOptions(container: ParentNode): HTMLElement[] {
102
+ return [...container.querySelectorAll<HTMLElement>(OPTION_SELECTOR)].filter(enabled);
103
+ }
104
+
105
+ /**
106
+ * The button that sends the answer: the last primary (`default` variant)
107
+ * button of the form. Confirmations without options send with their accept
108
+ * button, which is that same primary button.
109
+ */
110
+ export function sendButton(container: ParentNode): HTMLElement | null {
111
+ const primaries = [...container.querySelectorAll<HTMLElement>(`${BUTTON_SELECTOR}[data-variant='default']`)];
112
+ return primaries.length > 0 ? primaries[primaries.length - 1]! : null;
113
+ }
114
+
115
+ /**
116
+ * Marks the options (1–9) and the send button (↵) with their key, so the
117
+ * page's CSS shows the hint on the upstream controls. Only attributes that
118
+ * differ are written, so a caller observing mutations does not loop.
119
+ */
120
+ export function markKeyHints(container: ParentNode, show: boolean): void {
121
+ const wanted = new Map<HTMLElement, string>();
122
+ if (show) {
123
+ answerOptions(container).slice(0, 9).forEach((option, index) => wanted.set(option, String(index + 1)));
124
+ const send = sendButton(container);
125
+ if (send && !wanted.has(send)) wanted.set(send, "↵");
126
+ }
127
+ for (const element of container.querySelectorAll<HTMLElement>(`[${KBD_HINT_ATTRIBUTE}]`)) {
128
+ if (!wanted.has(element)) element.removeAttribute(KBD_HINT_ATTRIBUTE);
129
+ }
130
+ for (const [element, hint] of wanted) {
131
+ if (element.getAttribute(KBD_HINT_ATTRIBUTE) !== hint) element.setAttribute(KBD_HINT_ATTRIBUTE, hint);
132
+ }
133
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The part of Microvac UI (`ui/src/microvac/ui-module`) this plugin uses; the
3
+ * page provides the module at run time. `tsconfig.json` maps `@microvac/ui`
4
+ * here for type checking; the build keeps it external.
5
+ */
6
+ import type { ComponentType, ReactNode } from "react";
7
+ import type { AttentionFeed, AttentionItem } from "./types.js";
8
+
9
+ type AnyComponent = ComponentType<any>;
10
+
11
+ export declare const version: string;
12
+ export declare function requireVersion(range: string, plugin?: string): void;
13
+
14
+ export declare const queryKeys: { attention(companyId: string): readonly unknown[] };
15
+ export declare function useQueries(options: {
16
+ queries: readonly {
17
+ queryKey: readonly unknown[];
18
+ queryFn: () => Promise<AttentionFeed>;
19
+ enabled?: boolean;
20
+ refetchInterval?: number;
21
+ refetchOnWindowFocus?: boolean;
22
+ }[];
23
+ }): {
24
+ data?: AttentionFeed;
25
+ error: unknown;
26
+ isPending: boolean;
27
+ dataUpdatedAt: number;
28
+ refetch(): Promise<unknown>;
29
+ }[];
30
+ export declare function useQueryClient(): { invalidateQueries(filters: { queryKey: readonly unknown[] }): Promise<void> };
31
+ export declare function useSelectedCompany(): { companyId: string | null; company: { issuePrefix?: string | null } | null };
32
+ export declare function useViewerId(): string | null;
33
+ export declare function useToast(): (input: { title: string; body?: string; tone?: "info" | "success" | "warn" | "error" }) => void;
34
+
35
+ export declare const Link: AnyComponent;
36
+ export declare function useSearchParams(): [URLSearchParams, (next: URLSearchParams, options?: { replace?: boolean }) => void];
37
+
38
+ export declare const Button: AnyComponent;
39
+ export declare const Badge: AnyComponent;
40
+ export declare const MarkdownBody: ComponentType<{ children: string; className?: string }>;
41
+ export declare const DropdownMenu: AnyComponent;
42
+ export declare const DropdownMenuTrigger: AnyComponent;
43
+ export declare const DropdownMenuContent: AnyComponent;
44
+ export declare const DropdownMenuItem: AnyComponent;
45
+ export declare const DropdownMenuLabel: AnyComponent;
46
+ export declare const DropdownMenuSeparator: AnyComponent;
47
+ export declare function cn(...classes: unknown[]): string;
48
+ export declare function relativeTime(value: string | Date): string;
49
+
50
+ export declare const decisions: {
51
+ listAttention(
52
+ companyId: string,
53
+ query: { view?: string; all?: boolean; includeDismissed?: boolean },
54
+ ): Promise<AttentionFeed>;
55
+ foldObligations(items: readonly AttentionItem[]): { cards: AttentionItem[] };
56
+ snooze(companyId: string, item: AttentionItem, until: string): Promise<void>;
57
+ wake(companyId: string, item: AttentionItem): Promise<void>;
58
+ AnswerForm: ComponentType<{ item: AttentionItem; companyId: string; currentUserId?: string | null; leading?: ReactNode }>;
59
+ History: ComponentType<{ companyId: string; open: boolean; onOpenChange: (open: boolean) => void }>;
60
+ };
61
+
62
+ export declare const upstream: {
63
+ readonly InteractionCard: AnyComponent;
64
+ };