@bevel-software/platform-core-frontend 0.1.0

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.
Files changed (203) hide show
  1. package/LICENSE +202 -0
  2. package/package.json +70 -0
  3. package/src/core/CoreAppShell.tsx +444 -0
  4. package/src/core/__tests__/ShellRoutes.test.tsx +95 -0
  5. package/src/core/events.ts +29 -0
  6. package/src/core/registry.ts +336 -0
  7. package/src/css-side-effect-imports.d.ts +7 -0
  8. package/src/html-extensions.ts +12 -0
  9. package/src/index.css +49 -0
  10. package/src/index.ts +23 -0
  11. package/src/lib/api.ts +57 -0
  12. package/src/lib/utils.ts +138 -0
  13. package/src/modules/access/__tests__/ManageAccessDialog.test.tsx +130 -0
  14. package/src/modules/access/__tests__/useFileAccess.test.tsx +253 -0
  15. package/src/modules/access/api.ts +205 -0
  16. package/src/modules/access/components/AccessRestrictedBanner.tsx +31 -0
  17. package/src/modules/access/components/ManageAccessDialog.tsx +1232 -0
  18. package/src/modules/access/components/NodeOwnersBanner.tsx +30 -0
  19. package/src/modules/access/hooks/useFileAccess.ts +151 -0
  20. package/src/modules/admin/components/AdminRolesPage.tsx +943 -0
  21. package/src/modules/admin/components/RolesCorruptedBanner.tsx +74 -0
  22. package/src/modules/admin/services/admin.api.ts +92 -0
  23. package/src/modules/admin/services/roles.api.ts +136 -0
  24. package/src/modules/admin/state/admin.context.tsx +223 -0
  25. package/src/modules/auth/components/LoginScreen.tsx +135 -0
  26. package/src/modules/auth/hooks/useAuthState.ts +59 -0
  27. package/src/modules/auth/services/auth.api.ts +24 -0
  28. package/src/modules/auth/services/microsoft-oauth.ts +57 -0
  29. package/src/modules/auth/state/auth.context.ts +18 -0
  30. package/src/modules/git/__tests__/BranchSwitcher.test.tsx +347 -0
  31. package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +201 -0
  32. package/src/modules/git/__tests__/ProtectedBranchBanner.test.tsx +82 -0
  33. package/src/modules/git/__tests__/PullNeededBanner.test.tsx +441 -0
  34. package/src/modules/git/__tests__/git.api.test.ts +113 -0
  35. package/src/modules/git/__tests__/pr.api.test.ts +44 -0
  36. package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +212 -0
  37. package/src/modules/git/components/BranchSwitcher.tsx +527 -0
  38. package/src/modules/git/components/DiffView.tsx +30 -0
  39. package/src/modules/git/components/FileComparisonPanel.tsx +377 -0
  40. package/src/modules/git/components/FileHistoryPanel.tsx +320 -0
  41. package/src/modules/git/components/ProtectedBranchBanner.tsx +42 -0
  42. package/src/modules/git/components/PullNeededBanner.tsx +204 -0
  43. package/src/modules/git/components/PullRequestsForMe.tsx +244 -0
  44. package/src/modules/git/components/UnifiedDiffView.tsx +37 -0
  45. package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +99 -0
  46. package/src/modules/git/hooks/useAutoPullUpdates.ts +121 -0
  47. package/src/modules/git/hooks/useGitState.ts +335 -0
  48. package/src/modules/git/services/__tests__/error-messages.test.ts +134 -0
  49. package/src/modules/git/services/error-messages.ts +257 -0
  50. package/src/modules/git/services/git.api.ts +163 -0
  51. package/src/modules/git/services/pr-prompts.ts +159 -0
  52. package/src/modules/git/services/pr.api.ts +39 -0
  53. package/src/modules/git/state/auto-update.context.ts +21 -0
  54. package/src/modules/git/state/git.context.ts +82 -0
  55. package/src/modules/layout/components/AppLayout.tsx +40 -0
  56. package/src/modules/layout/components/DemoBanner.tsx +57 -0
  57. package/src/modules/layout/components/MaintenanceOverlay.tsx +104 -0
  58. package/src/modules/layout/components/MobileChatLayout.tsx +153 -0
  59. package/src/modules/layout/components/ResizableThreePaneLayout.tsx +203 -0
  60. package/src/modules/layout/components/SlideOverlay.tsx +224 -0
  61. package/src/modules/layout/components/__tests__/MobileChatLayout.test.tsx +77 -0
  62. package/src/modules/layout/components/__tests__/ResizableThreePaneLayout.test.tsx +57 -0
  63. package/src/modules/layout/hooks/useMediaQuery.ts +23 -0
  64. package/src/modules/layout/state/__tests__/layout.context.test.tsx +39 -0
  65. package/src/modules/layout/state/layout.context.ts +46 -0
  66. package/src/modules/library/__tests__/DetailDialog.test.tsx +200 -0
  67. package/src/modules/library/__tests__/diff.test.ts +62 -0
  68. package/src/modules/library/__tests__/loadout.test.tsx +72 -0
  69. package/src/modules/library/__tests__/status.test.ts +117 -0
  70. package/src/modules/library/components/ChangeRequestDock.tsx +47 -0
  71. package/src/modules/library/components/CompareView.tsx +357 -0
  72. package/src/modules/library/components/DetailDialog.tsx +566 -0
  73. package/src/modules/library/components/GearIcon.tsx +12 -0
  74. package/src/modules/library/components/LibraryCard.tsx +132 -0
  75. package/src/modules/library/components/LibraryPage.tsx +254 -0
  76. package/src/modules/library/components/LoadoutSidebar.tsx +156 -0
  77. package/src/modules/library/components/StatusGem.tsx +21 -0
  78. package/src/modules/library/components/SuggestChange.tsx +171 -0
  79. package/src/modules/library/components/particles.ts +50 -0
  80. package/src/modules/library/hooks/useLibraryData.ts +119 -0
  81. package/src/modules/library/hooks/useSkillDetail.ts +74 -0
  82. package/src/modules/library/library.css +320 -0
  83. package/src/modules/library/services/library.api.ts +164 -0
  84. package/src/modules/library/state/loadout.tsx +129 -0
  85. package/src/modules/library/state/toast.tsx +42 -0
  86. package/src/modules/library/utils/diff.ts +102 -0
  87. package/src/modules/library/utils/status.ts +116 -0
  88. package/src/modules/pr/__tests__/PrApprovalBadge.test.tsx +93 -0
  89. package/src/modules/pr/__tests__/PrCancelButton.test.tsx +158 -0
  90. package/src/modules/pr/__tests__/PrCancelConfirmDialog.test.tsx +44 -0
  91. package/src/modules/pr/__tests__/PrMergeButton.test.tsx +360 -0
  92. package/src/modules/pr/__tests__/bucketize-comments.test.ts +85 -0
  93. package/src/modules/pr/__tests__/parsePatch.test.ts +104 -0
  94. package/src/modules/pr/__tests__/usePrViewerState.test.ts +72 -0
  95. package/src/modules/pr/components/OpenChangeRequestDialog.tsx +193 -0
  96. package/src/modules/pr/components/PrApprovalBadge.tsx +80 -0
  97. package/src/modules/pr/components/PrCancelButton.tsx +99 -0
  98. package/src/modules/pr/components/PrCancelConfirmDialog.tsx +119 -0
  99. package/src/modules/pr/components/PrCommentComposer.tsx +92 -0
  100. package/src/modules/pr/components/PrCommentThread.tsx +168 -0
  101. package/src/modules/pr/components/PrCommentsPanel.tsx +143 -0
  102. package/src/modules/pr/components/PrDiffPane.tsx +425 -0
  103. package/src/modules/pr/components/PrFileRow.tsx +164 -0
  104. package/src/modules/pr/components/PrFilesList.tsx +53 -0
  105. package/src/modules/pr/components/PrHeader.tsx +45 -0
  106. package/src/modules/pr/components/PrHeaderOverflowMenu.tsx +58 -0
  107. package/src/modules/pr/components/PrMergeButton.tsx +268 -0
  108. package/src/modules/pr/components/PrMergeConfirmDialog.tsx +112 -0
  109. package/src/modules/pr/components/PrRefreshFromTargetButton.tsx +110 -0
  110. package/src/modules/pr/components/PrViewer.tsx +364 -0
  111. package/src/modules/pr/hooks/usePrViewerState.ts +150 -0
  112. package/src/modules/pr/services/pr-approvals.api.ts +31 -0
  113. package/src/modules/pr/services/pr-cancel.api.ts +19 -0
  114. package/src/modules/pr/services/pr-comments.api.ts +45 -0
  115. package/src/modules/pr/services/pr-detail.api.ts +17 -0
  116. package/src/modules/pr/services/pr-merge.api.ts +53 -0
  117. package/src/modules/pr/services/pr-open.api.ts +30 -0
  118. package/src/modules/pr/state/pr-viewer.context.ts +34 -0
  119. package/src/modules/pr/utils/bucketize-comments.ts +55 -0
  120. package/src/modules/pr/utils/parsePatch.ts +57 -0
  121. package/src/modules/review/__tests__/review.api.test.ts +121 -0
  122. package/src/modules/review/components/BinaryChangePlaceholder.tsx +15 -0
  123. package/src/modules/review/components/DiffViewer.tsx +78 -0
  124. package/src/modules/review/components/MarkdownDiffViewer.tsx +263 -0
  125. package/src/modules/review/components/ReviewFileRow.tsx +114 -0
  126. package/src/modules/review/components/ReviewPanel.tsx +260 -0
  127. package/src/modules/review/components/ReviewPanelSurface.tsx +52 -0
  128. package/src/modules/review/components/__tests__/ReviewPanel.test.tsx +106 -0
  129. package/src/modules/review/hooks/__tests__/useReviewState.test.ts +88 -0
  130. package/src/modules/review/hooks/useReviewState.ts +256 -0
  131. package/src/modules/review/services/review.api.ts +72 -0
  132. package/src/modules/review/state/ReviewProvider.tsx +28 -0
  133. package/src/modules/review/state/review.context.ts +29 -0
  134. package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +488 -0
  135. package/src/modules/secrets-vault/components/SecretsPage.tsx +276 -0
  136. package/src/modules/secrets-vault/components/ToolSecretsPanel.tsx +362 -0
  137. package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +51 -0
  138. package/src/modules/secrets-vault/services/connect.api.ts +113 -0
  139. package/src/modules/secrets-vault/services/secrets.api.ts +82 -0
  140. package/src/modules/secrets-vault/services/tool-secrets.api.ts +116 -0
  141. package/src/modules/toolbar/components/AdminMenu.tsx +250 -0
  142. package/src/modules/toolbar/components/AppSwitcher.tsx +108 -0
  143. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +548 -0
  144. package/src/modules/toolbar/components/Toolbar.tsx +123 -0
  145. package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +122 -0
  146. package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +353 -0
  147. package/src/modules/toolbar/services/external-api-keys.api.ts +65 -0
  148. package/src/modules/tools/ToolsExplorerPage.tsx +380 -0
  149. package/src/modules/workflow/hooks/useEventBusSubscription.ts +24 -0
  150. package/src/modules/workflow/hooks/useFileLock.ts +430 -0
  151. package/src/modules/workflow/services/lock.api.ts +139 -0
  152. package/src/modules/workflow/state/EventBusFocusBinder.tsx +25 -0
  153. package/src/modules/workflow/state/EventBusProvider.tsx +232 -0
  154. package/src/modules/workflow/state/event-bus.context.ts +75 -0
  155. package/src/modules/workspace/__tests__/testFixtures.ts +54 -0
  156. package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +91 -0
  157. package/src/modules/workspace/components/EditorTabs.tsx +384 -0
  158. package/src/modules/workspace/components/FileExplorer.tsx +1068 -0
  159. package/src/modules/workspace/components/FileRoute.tsx +392 -0
  160. package/src/modules/workspace/components/FileViewer.tsx +906 -0
  161. package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +241 -0
  162. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +666 -0
  163. package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +330 -0
  164. package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +492 -0
  165. package/src/modules/workspace/components/renderers/CsvRenderer.tsx +174 -0
  166. package/src/modules/workspace/components/renderers/DocxRenderer.tsx +87 -0
  167. package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +231 -0
  168. package/src/modules/workspace/components/renderers/ImageRenderer.tsx +50 -0
  169. package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +287 -0
  170. package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +252 -0
  171. package/src/modules/workspace/components/renderers/MermaidDiagram.tsx +268 -0
  172. package/src/modules/workspace/components/renderers/PdfRenderer.tsx +81 -0
  173. package/src/modules/workspace/components/renderers/TextRenderer.tsx +86 -0
  174. package/src/modules/workspace/components/renderers/ToolForm.tsx +404 -0
  175. package/src/modules/workspace/components/renderers/ToolRenderer.tsx +417 -0
  176. package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +157 -0
  177. package/src/modules/workspace/components/renderers/__tests__/CsvRenderer.test.ts +57 -0
  178. package/src/modules/workspace/components/renderers/__tests__/KbMarkdownView.test.tsx +95 -0
  179. package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +328 -0
  180. package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +167 -0
  181. package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +318 -0
  182. package/src/modules/workspace/components/renderers/csvUtils.ts +76 -0
  183. package/src/modules/workspace/components/renderers/htmlSandbox.ts +332 -0
  184. package/src/modules/workspace/components/renderers/index.ts +39 -0
  185. package/src/modules/workspace/components/renderers/types.ts +27 -0
  186. package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +298 -0
  187. package/src/modules/workspace/hooks/useWorkspaceState.ts +1315 -0
  188. package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +99 -0
  189. package/src/modules/workspace/routing/kb-routes.ts +266 -0
  190. package/src/modules/workspace/services/__tests__/workspace.api.test.ts +72 -0
  191. package/src/modules/workspace/services/workspace.api.ts +185 -0
  192. package/src/modules/workspace/state/workspace.context.ts +235 -0
  193. package/src/modules/workspace/utils/__tests__/frontmatter.test.ts +107 -0
  194. package/src/modules/workspace/utils/diff.ts +53 -0
  195. package/src/modules/workspace/utils/fileTree.ts +179 -0
  196. package/src/modules/workspace/utils/frontmatter.ts +80 -0
  197. package/src/modules/workspace/utils/readDroppedEntries.ts +119 -0
  198. package/src/modules/workspace/utils/tab-persistence.ts +23 -0
  199. package/src/shared/components/Dialog.tsx +198 -0
  200. package/src/shared/components/PageShell.tsx +51 -0
  201. package/src/shared/components/useModalLayer.ts +35 -0
  202. package/src/shared/markdown/Markdown.tsx +62 -0
  203. package/src/test-setup.ts +59 -0
@@ -0,0 +1,404 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import { Plus, X } from 'lucide-react';
3
+ import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
4
+ import { extractFrontmatter } from '@bevel-software/platform-shared';
5
+
6
+ /**
7
+ * A friendly form over a `.tool` manual. THE TOOL IS THE FRONTMATTER: everything
8
+ * — `id`/`name`, access verbs (`read`/`write`/`owner`/`download`), and the config
9
+ * (`type`/`url`/`variables`/…) — lives in ONE `---` YAML block. The form edits
10
+ * that one object and re-serializes it to `---\n<yaml>\n---\n`. Inline `tools[]`
11
+ * are edited as raw JSON. `readOnly` disables every control so the same layout
12
+ * renders for view-only files. The Code tab shows the raw file.
13
+ */
14
+
15
+ type Obj = Record<string, unknown>;
16
+ type ToolType = 'inline' | 'http' | 'mcp';
17
+
18
+ const ACCESS_VERBS = ['read', 'write', 'owner', 'download'] as const;
19
+ const inputCls =
20
+ 'w-full rounded border border-slate-300 px-2 py-1 text-xs focus:border-slate-500 focus:outline-none disabled:bg-slate-50 disabled:text-slate-500';
21
+
22
+ /**
23
+ * Parse the tool object — the `---` fence's contents, or the whole file when
24
+ * unfenced — plus the free-form notes after the closing fence (the parser
25
+ * ignores them, but the form must CARRY them so an edit doesn't delete them).
26
+ * `model` is null if unparseable.
27
+ */
28
+ function parseFile(text: string): { model: Obj | null; notes: string } {
29
+ const fm = extractFrontmatter(text);
30
+ const notes = fm ? fm.body : '';
31
+ const source = (fm ? fm.frontmatter : text).trim();
32
+ if (!source) return { model: {}, notes };
33
+ try {
34
+ const p = parseYaml(source);
35
+ return { model: p && typeof p === 'object' && !Array.isArray(p) ? (p as Obj) : null, notes };
36
+ } catch {
37
+ return { model: null, notes };
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Serialize the tool object into one `---` fenced YAML block (dropping empty
43
+ * lists / blank strings), re-appending the file's free-form notes verbatim.
44
+ */
45
+ function serializeFile(model: Obj, notes: string): string {
46
+ const out: Obj = {};
47
+ for (const [k, v] of Object.entries(model)) {
48
+ if (v == null || (typeof v === 'string' && !v.trim()) || (Array.isArray(v) && v.length === 0)) continue;
49
+ out[k] = v;
50
+ }
51
+ return `---\n${stringifyYaml(out)}---\n${notes}`;
52
+ }
53
+
54
+ function normType(raw: unknown): ToolType {
55
+ const t = typeof raw === 'string' ? raw.toLowerCase().trim() : '';
56
+ return t === 'http' || t === 'mcp' ? t : 'inline';
57
+ }
58
+
59
+ export function ToolForm({
60
+ value,
61
+ onChange,
62
+ readOnly = false,
63
+ }: {
64
+ value: string;
65
+ onChange: (next: string) => void;
66
+ readOnly?: boolean;
67
+ }) {
68
+ const lastEmitted = useRef<string | null>(null);
69
+ const [parsed, setParsed] = useState<{ model: Obj | null; notes: string }>(() => parseFile(value));
70
+
71
+ useEffect(() => {
72
+ if (value === lastEmitted.current) return;
73
+ setParsed(parseFile(value));
74
+ }, [value]);
75
+
76
+ const { model, notes } = parsed;
77
+ if (!model) {
78
+ return (
79
+ <p className="text-[11px] text-amber-600">
80
+ This file isn’t valid JSON/YAML — switch to Code to {readOnly ? 'view' : 'fix'} it.
81
+ </p>
82
+ );
83
+ }
84
+
85
+ // The whole tool is one object; identity/access and config share it. Keep the
86
+ // two setters as aliases so the field JSX below reads either concern uniformly.
87
+ const set = (patch: Obj) => {
88
+ if (readOnly) return;
89
+ const next = { ...model, ...patch };
90
+ setParsed({ model: next, notes });
91
+ const text = serializeFile(next, notes);
92
+ lastEmitted.current = text;
93
+ onChange(text);
94
+ };
95
+ const frontmatter = model;
96
+ const body = model;
97
+ const setFm = set;
98
+ const setBody = set;
99
+
100
+ const type = normType(body.type);
101
+ const remote = body.remote !== false;
102
+ const headers = body.headers && typeof body.headers === 'object' ? (body.headers as Record<string, string>) : {};
103
+ const variables = Array.isArray(body.variables) ? (body.variables as Obj[]) : [];
104
+
105
+ return (
106
+ <div className="space-y-3">
107
+ <Field label="Id (the tool's stable name / secret namespace — lowercase snake_case)">
108
+ <input
109
+ className={inputCls}
110
+ disabled={readOnly}
111
+ value={typeof frontmatter.id === 'string' ? frontmatter.id : ''}
112
+ onChange={(e) => setFm({ id: e.target.value })}
113
+ placeholder="my_tool"
114
+ />
115
+ </Field>
116
+
117
+ <Field label="Type">
118
+ <select className={inputCls} disabled={readOnly} value={type} onChange={(e) => setBody({ type: e.target.value })}>
119
+ <option value="inline">inline — tools embedded in this file</option>
120
+ <option value="http">http — URL returning a UTCP manual</option>
121
+ <option value="mcp">mcp — remote MCP server</option>
122
+ </select>
123
+ </Field>
124
+
125
+ <label className="flex items-center gap-2 text-[11px] text-slate-600">
126
+ <input type="checkbox" disabled={readOnly} checked={remote} onChange={(e) => setBody({ remote: e.target.checked })} />
127
+ Available to remote agents
128
+ <span className="text-slate-400">(uncheck for local-only tools, e.g. a localhost MCP server)</span>
129
+ </label>
130
+
131
+ {(type === 'http' || type === 'mcp') && (
132
+ <Field label="URL">
133
+ <input
134
+ className={inputCls}
135
+ disabled={readOnly}
136
+ value={typeof body.url === 'string' ? body.url : ''}
137
+ onChange={(e) => setBody({ url: e.target.value })}
138
+ placeholder={type === 'mcp' ? 'https://mcp.example.com' : 'https://api.example.com/utcp'}
139
+ />
140
+ </Field>
141
+ )}
142
+
143
+ {type === 'http' && (
144
+ <Field label="Method">
145
+ <select
146
+ className={inputCls}
147
+ disabled={readOnly}
148
+ value={body.httpMethod === 'POST' ? 'POST' : 'GET'}
149
+ onChange={(e) => setBody({ httpMethod: e.target.value })}
150
+ >
151
+ <option value="GET">GET</option>
152
+ <option value="POST">POST</option>
153
+ </select>
154
+ </Field>
155
+ )}
156
+
157
+ {(type === 'http' || type === 'mcp') && (
158
+ <KeyValueRows
159
+ label="Headers"
160
+ readOnly={readOnly}
161
+ entries={Object.entries(headers)}
162
+ placeholderKey="Authorization"
163
+ placeholderVal="Bearer ${API_KEY}"
164
+ onChange={(entries) => setBody({ headers: Object.fromEntries(entries.filter(([k]) => k)) })}
165
+ />
166
+ )}
167
+
168
+ <VariableRows variables={variables} readOnly={readOnly} onChange={(v) => setBody({ variables: v })} />
169
+
170
+ {type === 'inline' && (
171
+ <InlineToolsEditor tools={body.tools} readOnly={readOnly} onChange={(tools) => setBody({ tools })} />
172
+ )}
173
+
174
+ <AccessSection frontmatter={frontmatter} readOnly={readOnly} onChange={setFm} />
175
+ </div>
176
+ );
177
+ }
178
+
179
+ /** Per-verb access lists (read/write/owner/download) — one principal per line. */
180
+ function AccessSection({ frontmatter, readOnly, onChange }: { frontmatter: Obj; readOnly: boolean; onChange: (patch: Obj) => void }) {
181
+ const asLines = (v: unknown): string => (Array.isArray(v) ? v.map(String).join('\n') : typeof v === 'string' ? String(v) : '');
182
+ // Per-verb draft while a textarea is focused. A fully-controlled value of
183
+ // `asLines(cleaned lines)` would swallow Enter: the newline is trimmed/
184
+ // filtered out of the emitted lines, so the re-render erases it and a second
185
+ // principal can never be started. The draft keeps what the user typed; the
186
+ // CLEANED lines are still emitted on every keystroke, and blur re-syncs the
187
+ // display to canonical form.
188
+ const [drafts, setDrafts] = useState<Partial<Record<string, string>>>({});
189
+ return (
190
+ <div>
191
+ <span className="mb-0.5 block text-[11px] font-medium text-slate-500">
192
+ Access <span className="font-normal text-slate-400">(one role or `Name &lt;email&gt;` per line; prefix `deny ` to remove)</span>
193
+ </span>
194
+ <div className="grid grid-cols-2 gap-2">
195
+ {ACCESS_VERBS.map((verb) => (
196
+ <label key={verb} className="block">
197
+ <span className="mb-0.5 block text-[10px] uppercase tracking-wide text-slate-400">{verb}</span>
198
+ <textarea
199
+ disabled={readOnly}
200
+ rows={2}
201
+ value={drafts[verb] ?? asLines(frontmatter[verb])}
202
+ onChange={(e) => {
203
+ const raw = e.target.value;
204
+ setDrafts((d) => ({ ...d, [verb]: raw }));
205
+ onChange({ [verb]: raw.split(/\r?\n/).map((s) => s.trim()).filter(Boolean) });
206
+ }}
207
+ onBlur={() =>
208
+ setDrafts((d) => {
209
+ const { [verb]: _done, ...rest } = d;
210
+ return rest;
211
+ })
212
+ }
213
+ className="w-full resize-y rounded border border-slate-300 px-1.5 py-1 text-[11px] focus:border-slate-500 focus:outline-none disabled:bg-slate-50 disabled:text-slate-500"
214
+ placeholder={verb === 'read' ? 'everyone' : 'Role Name'}
215
+ />
216
+ </label>
217
+ ))}
218
+ </div>
219
+ </div>
220
+ );
221
+ }
222
+
223
+ function Field({ label, children }: { label: string; children: React.ReactNode }) {
224
+ return (
225
+ <label className="block">
226
+ <span className="mb-0.5 block text-[11px] font-medium text-slate-500">{label}</span>
227
+ {children}
228
+ </label>
229
+ );
230
+ }
231
+
232
+ function KeyValueRows({
233
+ label,
234
+ entries,
235
+ placeholderKey,
236
+ placeholderVal,
237
+ readOnly,
238
+ onChange,
239
+ }: {
240
+ label: string;
241
+ entries: [string, string][];
242
+ placeholderKey: string;
243
+ placeholderVal: string;
244
+ readOnly: boolean;
245
+ onChange: (entries: [string, string][]) => void;
246
+ }) {
247
+ const editAt = (i: number, next: [string, string]) => onChange(entries.map((e, j) => (j === i ? next : e)));
248
+ return (
249
+ <div>
250
+ <span className="mb-0.5 block text-[11px] font-medium text-slate-500">{label}</span>
251
+ <div className="space-y-1">
252
+ {entries.length === 0 && readOnly && <p className="text-[11px] text-slate-400">None.</p>}
253
+ {entries.map(([k, v], i) => (
254
+ <div key={i} className="flex gap-1">
255
+ <input
256
+ className={inputCls}
257
+ disabled={readOnly}
258
+ value={k}
259
+ placeholder={placeholderKey}
260
+ onChange={(e) => editAt(i, [e.target.value, v])}
261
+ />
262
+ <input
263
+ className={inputCls}
264
+ disabled={readOnly}
265
+ value={typeof v === 'string' ? v : ''}
266
+ placeholder={placeholderVal}
267
+ onChange={(e) => editAt(i, [k, e.target.value])}
268
+ />
269
+ {!readOnly && <RemoveButton onClick={() => onChange(entries.filter((_, j) => j !== i))} />}
270
+ </div>
271
+ ))}
272
+ {!readOnly && <AddButton label="Add header" onClick={() => onChange([...entries, ['', '']])} />}
273
+ </div>
274
+ </div>
275
+ );
276
+ }
277
+
278
+ function VariableRows({
279
+ variables,
280
+ readOnly,
281
+ onChange,
282
+ }: {
283
+ variables: Obj[];
284
+ readOnly: boolean;
285
+ onChange: (v: Obj[]) => void;
286
+ }) {
287
+ const editAt = (i: number, patch: Obj) => onChange(variables.map((v, j) => (j === i ? { ...v, ...patch } : v)));
288
+ return (
289
+ <div>
290
+ <span className="mb-0.5 block text-[11px] font-medium text-slate-500">
291
+ Variables <span className="font-normal text-slate-400">(who provisions each secret)</span>
292
+ </span>
293
+ <div className="space-y-1">
294
+ {variables.length === 0 && readOnly && <p className="text-[11px] text-slate-400">None.</p>}
295
+ {variables.map((v, i) => (
296
+ <div key={i} className="flex gap-1">
297
+ <input
298
+ className={inputCls}
299
+ disabled={readOnly}
300
+ value={typeof v.name === 'string' ? v.name : ''}
301
+ placeholder="API_KEY"
302
+ onChange={(e) => editAt(i, { name: e.target.value })}
303
+ />
304
+ <select
305
+ className={inputCls}
306
+ disabled={readOnly}
307
+ value={v.scope === 'user' ? 'user' : 'admin'}
308
+ onChange={(e) => editAt(i, { scope: e.target.value })}
309
+ >
310
+ <option value="admin">admin (shared)</option>
311
+ <option value="user">user (per-user)</option>
312
+ </select>
313
+ <input
314
+ className={inputCls}
315
+ disabled={readOnly}
316
+ value={typeof v.label === 'string' ? v.label : ''}
317
+ placeholder="label (optional)"
318
+ onChange={(e) => editAt(i, { label: e.target.value })}
319
+ />
320
+ {!readOnly && <RemoveButton onClick={() => onChange(variables.filter((_, j) => j !== i))} />}
321
+ </div>
322
+ ))}
323
+ {!readOnly && (
324
+ <AddButton label="Add variable" onClick={() => onChange([...variables, { name: '', scope: 'admin' }])} />
325
+ )}
326
+ </div>
327
+ </div>
328
+ );
329
+ }
330
+
331
+ /** Raw-JSON editor for inline `tools[]`; only propagates a valid parse (keeps local text otherwise). */
332
+ function InlineToolsEditor({
333
+ tools,
334
+ readOnly,
335
+ onChange,
336
+ }: {
337
+ tools: unknown;
338
+ readOnly: boolean;
339
+ onChange: (tools: unknown) => void;
340
+ }) {
341
+ const external = JSON.stringify(Array.isArray(tools) ? tools : [], null, 2);
342
+ const [text, setText] = useState(external);
343
+ const [error, setError] = useState<string | null>(null);
344
+ const lastEmitted = useRef<string | null>(null);
345
+
346
+ useEffect(() => {
347
+ if (external === lastEmitted.current) return;
348
+ setText(external);
349
+ setError(null);
350
+ }, [external]);
351
+
352
+ const onEdit = (next: string) => {
353
+ setText(next);
354
+ try {
355
+ const parsed = JSON.parse(next);
356
+ if (!Array.isArray(parsed)) throw new Error('must be a JSON array');
357
+ setError(null);
358
+ lastEmitted.current = JSON.stringify(parsed, null, 2);
359
+ onChange(parsed);
360
+ } catch (err) {
361
+ setError(err instanceof Error ? err.message : 'invalid JSON');
362
+ }
363
+ };
364
+
365
+ return (
366
+ <div>
367
+ <span className="mb-0.5 block text-[11px] font-medium text-slate-500">Inline tools (advanced, raw JSON)</span>
368
+ <textarea
369
+ value={readOnly ? external : text}
370
+ readOnly={readOnly}
371
+ onChange={(e) => onEdit(e.target.value)}
372
+ spellCheck={false}
373
+ rows={8}
374
+ className="w-full resize-y rounded border border-slate-300 bg-slate-50 p-2 font-mono text-[11px] focus:border-slate-500 focus:outline-none"
375
+ />
376
+ {!readOnly && error && <p className="mt-0.5 text-[10px] text-red-600">Inline tools: {error}</p>}
377
+ </div>
378
+ );
379
+ }
380
+
381
+ function AddButton({ label, onClick }: { label: string; onClick: () => void }) {
382
+ return (
383
+ <button
384
+ type="button"
385
+ onClick={onClick}
386
+ className="flex items-center gap-1 rounded px-1.5 py-0.5 text-[11px] text-slate-500 hover:bg-slate-100"
387
+ >
388
+ <Plus size={11} /> {label}
389
+ </button>
390
+ );
391
+ }
392
+
393
+ function RemoveButton({ onClick }: { onClick: () => void }) {
394
+ return (
395
+ <button
396
+ type="button"
397
+ onClick={onClick}
398
+ className="rounded p-1 text-slate-400 hover:bg-red-50 hover:text-red-600"
399
+ aria-label="Remove"
400
+ >
401
+ <X size={12} />
402
+ </button>
403
+ );
404
+ }