@bevel-software/platform-core-frontend 0.26.0 → 0.28.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 (206) hide show
  1. package/package.json +2 -2
  2. package/src/core/CoreAppShell.tsx +87 -25
  3. package/src/core/ProductName.tsx +49 -0
  4. package/src/core/__tests__/ProductName.test.tsx +46 -0
  5. package/src/core/__tests__/ShellRoutes.test.tsx +22 -0
  6. package/src/core/__tests__/core-contributions.test.ts +16 -0
  7. package/src/core/core-contributions.ts +17 -0
  8. package/src/core/maker-url.ts +30 -0
  9. package/src/core/registry.ts +78 -1
  10. package/src/index.css +23 -5
  11. package/src/index.ts +5 -0
  12. package/src/lib/__tests__/latest-request.test.ts +48 -0
  13. package/src/lib/latest-request.ts +45 -0
  14. package/src/modules/access/__tests__/ManageAccessDialog.no-sso-invite.test.tsx +222 -0
  15. package/src/modules/access/__tests__/ManageAccessDialog.proposal.test.tsx +1 -1
  16. package/src/modules/access/api.ts +19 -3
  17. package/src/modules/access/components/ManageAccessDialog.tsx +61 -8
  18. package/src/modules/admin/__tests__/AdminRolesPage.fixed-admin.test.tsx +16 -1
  19. package/src/modules/admin/__tests__/RolesCorruptedBanner.test.tsx +70 -0
  20. package/src/modules/admin/__tests__/UserAccountsPage.test.tsx +284 -39
  21. package/src/modules/admin/components/AdminRolesPage.tsx +13 -14
  22. package/src/modules/admin/components/RolesCorruptedBanner.tsx +9 -4
  23. package/src/modules/admin/components/UserAccountsPage.tsx +91 -107
  24. package/src/modules/admin/services/roles.api.ts +14 -1
  25. package/src/modules/auth/__tests__/LoginScreen.test.tsx +80 -5
  26. package/src/modules/auth/__tests__/sso-strict-probe.test.ts +66 -0
  27. package/src/modules/auth/components/LoginScreen.tsx +101 -73
  28. package/src/modules/auth/services/account.api.ts +59 -3
  29. package/src/modules/auth/services/sso.ts +30 -10
  30. package/src/modules/auth/state/use-sign-in-methods.ts +41 -0
  31. package/src/modules/change-requests/__tests__/ChangeRequestDialog.sync-banner.test.tsx +176 -0
  32. package/src/modules/change-requests/__tests__/ChangeRequestDialog.unreadable.test.tsx +68 -0
  33. package/src/modules/change-requests/__tests__/ChangeRequestLink.test.tsx +182 -0
  34. package/src/modules/change-requests/__tests__/FileChangeBoxes.test.tsx +1 -1
  35. package/src/modules/change-requests/__tests__/readFileOnBranch.test.ts +79 -0
  36. package/src/modules/change-requests/components/ChangeBox.tsx +6 -1
  37. package/src/modules/change-requests/components/ChangeRequestDialog.tsx +45 -15
  38. package/src/modules/change-requests/components/ChangeRequestLink.tsx +151 -0
  39. package/src/modules/change-requests/components/CrFileTree.tsx +2 -1
  40. package/src/modules/change-requests/components/FileChangeBoxes.tsx +4 -2
  41. package/src/modules/change-requests/hooks/useApplyChangeRequest.ts +6 -3
  42. package/src/modules/change-requests/services/change-requests.api.ts +15 -2
  43. package/src/modules/change-requests/utils/readFailure.ts +22 -0
  44. package/src/modules/embed/__tests__/EmbedLinkPage.test.tsx +74 -0
  45. package/src/modules/embed/__tests__/EmbedView.test.tsx +473 -0
  46. package/src/modules/embed/__tests__/embed-config.test.ts +128 -0
  47. package/src/modules/embed/__tests__/embed-host.test.ts +111 -0
  48. package/src/modules/embed/__tests__/mcp-app-view.test.ts +295 -0
  49. package/src/modules/embed/__tests__/mount.test.tsx +157 -0
  50. package/src/modules/embed/components/EmbedLinkPage.tsx +109 -0
  51. package/src/modules/embed/components/EmbedView.tsx +545 -0
  52. package/src/modules/embed/embed-config.ts +132 -0
  53. package/src/modules/embed/embed-host.ts +120 -0
  54. package/src/modules/embed/mount.tsx +80 -0
  55. package/src/modules/embed/services/embed-token.ts +22 -0
  56. package/src/modules/embed/services/embed.api.ts +158 -0
  57. package/src/modules/git/__tests__/BranchSwitcher.test.tsx +202 -4
  58. package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +59 -1
  59. package/src/modules/git/__tests__/PullNeededBanner.test.tsx +39 -22
  60. package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +7 -2
  61. package/src/modules/git/branch-delete-menu-item.tsx +24 -0
  62. package/src/modules/git/components/BranchSwitcher.tsx +29 -5
  63. package/src/modules/git/components/GitSyncFailedBanner.tsx +53 -15
  64. package/src/modules/git/components/HistoryVersionPreview.tsx +5 -3
  65. package/src/modules/git/components/PullNeededBanner.tsx +15 -49
  66. package/src/modules/git/hooks/useAutoPullUpdates.ts +9 -5
  67. package/src/modules/git/index.ts +15 -0
  68. package/src/modules/git/services/__tests__/error-messages.test.ts +67 -0
  69. package/src/modules/git/services/__tests__/pull-failure.test.ts +36 -0
  70. package/src/modules/git/services/error-messages.ts +58 -1
  71. package/src/modules/git/services/pull-failure.ts +68 -0
  72. package/src/modules/git/state/branch-delete-confirm.ts +46 -0
  73. package/src/modules/layout/__tests__/HeaderAlignment.test.tsx +1 -0
  74. package/src/modules/layout/__tests__/breakpoints.test.ts +7 -1
  75. package/src/modules/layout/breakpoints.ts +8 -0
  76. package/src/modules/library/__tests__/PluginExtras.test.tsx +9 -9
  77. package/src/modules/library/__tests__/PluginPage.test.tsx +8 -1
  78. package/src/modules/library/__tests__/SkillsTree.test.tsx +13 -4
  79. package/src/modules/library/components/LibraryPage.tsx +22 -0
  80. package/src/modules/library/components/PluginExtras.tsx +23 -6
  81. package/src/modules/library/components/PluginPage.tsx +6 -1
  82. package/src/modules/library/routes/LibraryRoutes.tsx +8 -7
  83. package/src/modules/library/services/plugins.api.ts +4 -1
  84. package/src/modules/library/state/plugins-revision.ts +28 -0
  85. package/src/modules/onboarding/__tests__/InviteButton.test.tsx +214 -0
  86. package/src/modules/onboarding/__tests__/agent-connection-api.test.ts +55 -0
  87. package/src/modules/onboarding/__tests__/creator-welcome.test.tsx +87 -82
  88. package/src/modules/onboarding/__tests__/first-page-prompt.test.ts +164 -0
  89. package/src/modules/onboarding/__tests__/get-started-column.test.tsx +1458 -0
  90. package/src/modules/onboarding/__tests__/invite-dialog.test.tsx +811 -0
  91. package/src/modules/onboarding/__tests__/invite-emails.test.ts +247 -0
  92. package/src/modules/onboarding/__tests__/onboarding.test.tsx +226 -193
  93. package/src/modules/onboarding/__tests__/starter-pack-card.test.tsx +136 -0
  94. package/src/modules/onboarding/components/CreatorWelcomePage.tsx +7 -11
  95. package/src/modules/onboarding/components/EmailChipsInput.tsx +138 -0
  96. package/src/modules/onboarding/components/GetStartedColumn.tsx +670 -0
  97. package/src/modules/onboarding/components/InviteButton.tsx +139 -0
  98. package/src/modules/onboarding/components/InviteDialog.tsx +741 -0
  99. package/src/modules/onboarding/components/RootLanding.tsx +8 -24
  100. package/src/modules/onboarding/components/StarterPackCard.tsx +98 -0
  101. package/src/modules/onboarding/components/WelcomePage.tsx +65 -154
  102. package/src/modules/onboarding/first-page-prompt.ts +123 -0
  103. package/src/modules/onboarding/invite-emails.ts +313 -0
  104. package/src/modules/onboarding/services/agent-connection.api.ts +53 -0
  105. package/src/modules/onboarding/services/starter-packs.api.ts +83 -0
  106. package/src/modules/onboarding/state/agent-connection.ts +167 -0
  107. package/src/modules/onboarding/state/invite-dialog.context.ts +35 -0
  108. package/src/modules/onboarding/state/invite-dialog.tsx +39 -0
  109. package/src/modules/onboarding/state/onboarding.ts +133 -59
  110. package/src/modules/onboarding/state/starter-packs.ts +169 -0
  111. package/src/modules/settings/settings-nav-items.tsx +8 -3
  112. package/src/modules/setup/__tests__/setup.first-run-storage.test.tsx +389 -0
  113. package/src/modules/setup/__tests__/setup.github.test.tsx +5 -5
  114. package/src/modules/setup/__tests__/setup.kept-draft.test.tsx +1 -1
  115. package/src/modules/setup/__tests__/setup.repository-change.test.tsx +1 -1
  116. package/src/modules/setup/__tests__/setup.repository.test.tsx +21 -21
  117. package/src/modules/setup/__tests__/setup.test.tsx +56 -15
  118. package/src/modules/setup/__tests__/useConnectionProbe.test.tsx +123 -0
  119. package/src/modules/setup/components/FirstRunStorage.tsx +560 -0
  120. package/src/modules/setup/components/SetupGate.tsx +46 -1
  121. package/src/modules/setup/components/SetupScreen.tsx +81 -84
  122. package/src/modules/setup/hooks/useConnectionProbe.ts +93 -0
  123. package/src/modules/setup/services/setup.api.ts +3 -3
  124. package/src/modules/setup/utils/suggested-branch.ts +27 -0
  125. package/src/modules/toolbar/commands/__tests__/actions.test.tsx +210 -0
  126. package/src/modules/toolbar/commands/actions.tsx +330 -0
  127. package/src/modules/toolbar/commands/command-menu.ts +44 -0
  128. package/src/modules/toolbar/commands/useCommandActions.ts +71 -0
  129. package/src/modules/toolbar/commands/useCommandShortcuts.ts +121 -0
  130. package/src/modules/toolbar/components/AppSwitcher.tsx +125 -22
  131. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +1 -1
  132. package/src/modules/toolbar/components/ProfileMenu.tsx +9 -4
  133. package/src/modules/toolbar/components/SearchPalette.tsx +540 -0
  134. package/src/modules/toolbar/components/Toolbar.tsx +10 -0
  135. package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +230 -52
  136. package/src/modules/toolbar/components/__tests__/ProfileMenu.test.tsx +64 -0
  137. package/src/modules/toolbar/components/__tests__/SearchPalette.test.tsx +864 -0
  138. package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +57 -2
  139. package/src/modules/toolbar/search/__tests__/rank.test.ts +93 -0
  140. package/src/modules/toolbar/search/__tests__/sources.test.ts +131 -0
  141. package/src/modules/toolbar/search/rank.ts +111 -0
  142. package/src/modules/toolbar/search/sources.ts +169 -0
  143. package/src/modules/workflow/hooks/useFileLock.ts +10 -7
  144. package/src/modules/workflow/services/__tests__/lock.api.test.ts +41 -0
  145. package/src/modules/workflow/services/lock.api.ts +17 -2
  146. package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +49 -1
  147. package/src/modules/workspace/components/EditorTabs.tsx +39 -15
  148. package/src/modules/workspace/components/FileExplorer.tsx +39 -6
  149. package/src/modules/workspace/components/FilePaneCard.tsx +6 -2
  150. package/src/modules/workspace/components/FileRoute.tsx +12 -7
  151. package/src/modules/workspace/components/FileViewer.tsx +271 -74
  152. package/src/modules/workspace/components/KbPageHeader.tsx +13 -6
  153. package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +88 -21
  154. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +92 -4
  155. package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +23 -3
  156. package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +330 -6
  157. package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +17 -2
  158. package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -16
  159. package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +6 -0
  160. package/src/modules/workspace/components/renderers/EmailRenderer.tsx +23 -21
  161. package/src/modules/workspace/components/renderers/ImageRenderer.tsx +13 -29
  162. package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +9 -2
  163. package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +8 -0
  164. package/src/modules/workspace/components/renderers/PdfRenderer.tsx +14 -19
  165. package/src/modules/workspace/components/renderers/PptxRenderer.tsx +11 -17
  166. package/src/modules/workspace/components/renderers/ToolRenderer.tsx +35 -7
  167. package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +11 -17
  168. package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.links.test.tsx +73 -0
  169. package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.scaffold.test.tsx +64 -0
  170. package/src/modules/workspace/components/renderers/__tests__/ToolRenderer.surface.test.tsx +82 -0
  171. package/src/modules/workspace/components/renderers/__tests__/bridgeHarness.ts +99 -0
  172. package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +129 -35
  173. package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +149 -0
  174. package/src/modules/workspace/components/renderers/__tests__/kbMarkdownLinkPolicy.test.tsx +100 -0
  175. package/src/modules/workspace/components/renderers/htmlSandbox.ts +76 -20
  176. package/src/modules/workspace/components/renderers/index.ts +26 -0
  177. package/src/modules/workspace/components/renderers/kbMarkdownPipeline.tsx +51 -1
  178. package/src/modules/workspace/components/renderers/rendererRawRead.ts +53 -0
  179. package/src/modules/workspace/components/renderers/rendererSurface.ts +109 -0
  180. package/src/modules/workspace/components/renderers/rendererWorkspace.ts +8 -2
  181. package/src/modules/workspace/hooks/__tests__/useCreatePage.test.ts +137 -0
  182. package/src/modules/workspace/hooks/__tests__/useKnowledgeWriteTarget.test.ts +302 -0
  183. package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +126 -21
  184. package/src/modules/workspace/hooks/useCreatePage.ts +206 -0
  185. package/src/modules/workspace/hooks/useKnowledgeWriteTarget.ts +238 -0
  186. package/src/modules/workspace/hooks/useWorkspaceImageResolver.ts +24 -7
  187. package/src/modules/workspace/hooks/useWorkspaceState.ts +42 -8
  188. package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +10 -0
  189. package/src/modules/workspace/routing/__tests__/md-links-parity.test.tsx +113 -0
  190. package/src/modules/workspace/routing/kb-routes.ts +66 -12
  191. package/src/modules/workspace/state/editable-page.ts +42 -0
  192. package/src/modules/workspace/state/workspace.context.ts +13 -4
  193. package/src/modules/workspace/utils/__tests__/display-file-name.test.ts +60 -0
  194. package/src/modules/workspace/utils/__tests__/fileTree.test.ts +75 -0
  195. package/src/modules/workspace/utils/__tests__/treeConfirm.test.ts +16 -0
  196. package/src/modules/workspace/utils/display-file-name.ts +102 -0
  197. package/src/modules/workspace/utils/fileTree.ts +49 -14
  198. package/src/modules/workspace/utils/treeConfirm.ts +15 -0
  199. package/src/shared/__tests__/no-builtin-confirm.test.ts +102 -0
  200. package/src/shared/components/ConfirmProvider.tsx +156 -0
  201. package/src/shared/components/Dialog.tsx +7 -0
  202. package/src/shared/components/__tests__/ConfirmProvider.test.tsx +198 -0
  203. package/src/shared/components/confirm-context.ts +94 -0
  204. package/src/shared/components/index.ts +3 -0
  205. package/src/modules/onboarding/components/WelcomeRoute.tsx +0 -57
  206. package/src/modules/onboarding/welcome-state.ts +0 -36
@@ -0,0 +1,113 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { renderToStaticMarkup } from 'react-dom/server';
3
+ import Markdown from 'react-markdown';
4
+ import remarkGfm from 'remark-gfm';
5
+ import remarkFrontmatter from 'remark-frontmatter';
6
+ import { MD_ID_LINK_RE, resolveMdLink, scanMarkdownLinks } from '@bevel-software/platform-shared';
7
+ import { NODE_ID_LINK_RE, resolveKbHref, safeDecode } from '../kb-routes';
8
+
9
+ /**
10
+ * The link grammar `move_file` rewrites with (platform-shared `md-links`) and
11
+ * the app that renders and follows those links must agree on two things, over
12
+ * one fixture set:
13
+ * - WHICH destinations a page holds: what the scanner finds in a page's body
14
+ * is exactly what react-markdown renders as an `href` or `src`;
15
+ * - WHERE each one points: `resolveMdLink` and `resolveKbHref` give the same
16
+ * path, branch and anchor, and the same id-link verdict.
17
+ *
18
+ * One deliberate difference is pinned at the end: link syntax inside inline
19
+ * code. Neither the renderer nor the rewriter treats it as a link — but the
20
+ * graph parser and `validate_graph` do, so after a move such a code example
21
+ * reads as dangling there.
22
+ */
23
+
24
+ const KB = 'knowledge-base';
25
+ const BASE = `${KB}/Projects/A/One.md`;
26
+
27
+ /** The shared fixture set: pages whose every link destination both sides must agree on. */
28
+ const PAGES: string[] = [
29
+ '[rel](../B/Two.md) [dot](./Three.md#goal) [up](../../Index.md "Index")',
30
+ `[root](/${KB}/Projects/B/Two.md) [app](/workspace/main/${KB}/Projects/My%20Doc.md#x)`,
31
+ '[angle](<../B/My Doc.md>) [enc](../B/My%20Doc.md) ![img](../assets/shot.png "Shot")',
32
+ '[id](project-hexis) [id-anchor](project-hexis#scope) [same](#overview) [web](https://example.com/a.md)',
33
+ '[ref][r1] and [collapsed][]\n\n[r1]: ../B/Ref.md "Ref"\n[collapsed]: <../B/Col lapsed.md>',
34
+ '[parens](../B/Plan(v2).md) [escaped](../B/a\\_b.md) [![inner](i.png)](../B/Outer.md)',
35
+ '- item [in list](../B/List.md)\n\n> quoted [in quote](../B/Quote.md)\n\n| a | b |\n|---|---|\n| [cell](../B/Cell.md) | x |',
36
+ 'Text with `code [not](../B/Code.md)` and\n\n```\n[fenced](../B/Fenced.md)\n```\n\n~~~md\n![fenced](x.png)\n~~~\n\nafter [after](../B/After.md)',
37
+ '---\nnodeType: "[Task](../../NodeTypes/Task.md)"\nid: one\n---\n\n# One\n[body](../B/Body.md)',
38
+ '[folder](../B/) [mangled](junk/knowledge-base/Projects/C.md)',
39
+ // Indented code blocks: four columns in, where a paragraph cannot continue.
40
+ '[before](../B/Before.md)\n\n [indented](../B/Indented.md)\n ![x](y.png)\n\n still code [c](../B/C2.md)\n\n[after](../B/After2.md)',
41
+ '# Heading\n [code after heading](../B/H.md)\n\npara\n [lazy continuation](../B/Lazy.md)',
42
+ '\t[tab code](../B/Tab.md)\n\ntext [t](../B/T.md)',
43
+ '- item\n\n [list paragraph](../B/ListPara.md)\n\n [list code](../B/ListCode.md)\n\nback [out](../B/Out.md)\n\n [code again](../B/Code2.md)',
44
+ '1. one\n2. two\n\n [ordered para](../B/Ord.md)\n\n [ordered code](../B/OrdCode.md)',
45
+ '> quote\n\n [code after quote](../B/Q.md)\n\n [two spaces](../B/Two2.md)',
46
+ // A fence line four columns in opens or closes nothing (a paragraph's continuation, or still code).
47
+ 'para\n ~~~\n [lazy fence](../B/LazyFence.md)\n ~~~\n\npara\n ```\n [tick span](../B/Span.md)\n ```',
48
+ '~~~\n ~~~\n[in fence](../B/InFence.md)\n~~~\n[out of fence](../B/OutFence.md)',
49
+ '- item\n\n ~~~\n [list fence](../B/ListFence.md)\n ~~~\n\n[after list](../B/AfterList.md)',
50
+ // Reference definitions inside blockquotes.
51
+ '> [q1]: ../B/Q1.md "t"\n> > [q2]: <../B/Q 2.md>\n\n[one][q1] [two][q2]',
52
+ ];
53
+
54
+ /** The destinations react-markdown renders for a page, in document order, as the attribute values. */
55
+ function rendered(page: string): string[] {
56
+ const html = renderToStaticMarkup(<Markdown remarkPlugins={[remarkGfm, remarkFrontmatter]}>{page}</Markdown>);
57
+ const doc = new DOMParser().parseFromString(html, 'text/html');
58
+ return [...doc.querySelectorAll('a[href], img[src]')].map((el) => el.getAttribute(el.tagName === 'A' ? 'href' : 'src')!);
59
+ }
60
+
61
+ /** Destinations compared as what they name: decoded, markdown escapes undone. */
62
+ const canonical = (d: string) => safeDecode(d.replace(/\\([!-/:-@[-`{-~])/g, '$1'));
63
+
64
+ describe('md-links agrees with the app on a shared fixture set', () => {
65
+ it.each(PAGES.map((p, i) => [i, p]))('page %i: the scanner finds exactly the destinations the app renders', (_i, page) => {
66
+ const scanned = scanMarkdownLinks(page).filter((s) => !s.inFrontmatter);
67
+ // The renderer emits a definition where a reference USES it; order by first use.
68
+ const ours = scanned.map((s) => canonical(s.destination)).sort();
69
+ const theirs = rendered(page).map(canonical).sort();
70
+ expect(theirs.length).toBeGreaterThan(0);
71
+ expect(ours).toEqual(theirs);
72
+ });
73
+
74
+ it.each(PAGES.map((p, i) => [i, p]))('page %i: every destination resolves to the same place', (_i, page) => {
75
+ for (const span of scanMarkdownLinks(page)) {
76
+ const image = span.kind === 'image';
77
+ const ours = resolveMdLink(span.destination, { basePath: BASE, kbDirName: KB, image });
78
+ const href = canonical(span.destination);
79
+ // The app follows an id-link by id before it resolves anything as a path.
80
+ expect(MD_ID_LINK_RE.test(href), href).toBe(NODE_ID_LINK_RE.test(href));
81
+ if (NODE_ID_LINK_RE.test(href)) {
82
+ expect(ours, href).toBeNull();
83
+ continue;
84
+ }
85
+ const theirs = resolveKbHref(span.destination.replace(/\\([!-/:-@[-`{-~])/g, '$1'), {
86
+ basePath: BASE,
87
+ kbDirName: KB,
88
+ repairMangledPath: !image,
89
+ });
90
+ if (theirs === null || theirs.kind === 'external') {
91
+ expect(ours, href).toBeNull();
92
+ continue;
93
+ }
94
+ // A same-page anchor names the page itself: nothing a move rewrites.
95
+ if (href.startsWith('#')) {
96
+ expect(ours, href).toBeNull();
97
+ continue;
98
+ }
99
+ expect(ours, href).toEqual({
100
+ form: href.startsWith('/workspace/') ? 'workspace' : href.startsWith('/') ? 'root' : 'relative',
101
+ path: theirs.path,
102
+ branch: theirs.branch,
103
+ hash: theirs.hash,
104
+ });
105
+ }
106
+ });
107
+
108
+ it('link syntax inside inline code: a link to neither the app nor the rewriter', () => {
109
+ const page = 'Example: `[x](../B/Two.md)`';
110
+ expect(rendered(page)).toEqual([]);
111
+ expect(scanMarkdownLinks(page)).toEqual([]);
112
+ });
113
+ });
@@ -1,7 +1,8 @@
1
- import { useCallback, useEffect, useState } from 'react';
1
+ import { useCallback, useContext, useEffect, useState } from 'react';
2
2
  import { useLocation, useNavigate } from 'react-router-dom';
3
- import { useGit } from '../../git/state/git.context';
4
- import { useWorkspace } from '../state/workspace.context';
3
+ import { GitContext } from '../../git/state/git.context';
4
+ import { WorkspaceContext } from '../state/workspace.context';
5
+ import { useRendererSurface } from '../components/renderers/rendererSurface';
5
6
  import { authFetch } from '../../../lib/api';
6
7
  import {
7
8
  isExternalHref,
@@ -136,8 +137,12 @@ export function fetchNodeId(branch: string, workspacePath: string): Promise<stri
136
137
  * links id-based on their own, independent of the `FileRoute` path→id redirect.
137
138
  */
138
139
  export function useCanonicalFileUrl(workspacePath: string | null): string | null {
139
- const git = useGit();
140
- const branch = git.status?.branch ?? null;
140
+ // A surface outside the app knows the address of the file it is showing —
141
+ // it was told it by whatever opened it — and has no git status to resolve
142
+ // one from. Asked FIRST, and before `useGit`, which has no provider there.
143
+ const surface = useRendererSurface();
144
+ const git = useContext(GitContext);
145
+ const branch = surface ? null : (git?.status?.branch ?? null);
141
146
  // Tie the resolved id to the exact (branch, path) it was fetched for. Inputs
142
147
  // change a render before the effect re-resolves, so without this key the URL
143
148
  // would briefly pair the new path with the *previous* node's id.
@@ -156,6 +161,7 @@ export function useCanonicalFileUrl(workspacePath: string | null): string | null
156
161
  };
157
162
  }, [key, branch, workspacePath]);
158
163
 
164
+ if (surface) return workspacePath ? surface.canonicalUrlFor(workspacePath) : null;
159
165
  if (!branch || !workspacePath) return null;
160
166
  const nodeId = resolved && resolved.key === key ? resolved.id : null;
161
167
  const relative = nodeId ? kbNodeUrl(branch, nodeId) : kbFileUrl(branch, workspacePath);
@@ -317,10 +323,19 @@ export function branchFromPathname(pathname: string): string | null {
317
323
  }
318
324
 
319
325
  export function useFileNav() {
326
+ // Outside the app every opener is the surface's: the embed opens a page in
327
+ // a NEW TAB through its host and never navigates itself. Read before the
328
+ // two app contexts, which have no provider there — so they are read
329
+ // non-throwing and asserted only on the app path below.
330
+ const surface = useRendererSurface();
320
331
  const navigate = useNavigate();
321
332
  const location = useLocation();
322
- const git = useGit();
323
- const { kbDirName } = useWorkspace();
333
+ const git = useContext(GitContext);
334
+ const workspace = useContext(WorkspaceContext);
335
+ if (!surface && (!git || !workspace)) {
336
+ throw new Error('useFileNav must be used within GitContext/WorkspaceContext or a RendererSurface');
337
+ }
338
+ const kbDirName = surface?.kbDirName ?? workspace?.kbDirName ?? '';
324
339
  // The URL's branch first, the git status second. During a branch switch the
325
340
  // status still reports the branch being LEFT — it only catches up once the
326
341
  // destination workspace has bootstrapped and answered — so building a click
@@ -329,7 +344,7 @@ export function useFileNav() {
329
344
  // which branch is on screen (`FileRoute` bootstraps to match it), so a click
330
345
  // lands on the branch being switched TO. Off a workspace route there is no
331
346
  // branch in the URL and the status is the only answer there is.
332
- const branch = branchFromPathname(location.pathname) ?? git.status?.branch ?? null;
347
+ const branch = branchFromPathname(location.pathname) ?? git?.status?.branch ?? null;
333
348
 
334
349
  const openFile = useCallback(
335
350
  (pathOrUrl: string) => {
@@ -423,11 +438,28 @@ export function useFileNav() {
423
438
  * later segment happens to equal `kbDirName` — which a real folder inside
424
439
  * the tree is allowed to be. A path that came from the tree needs no
425
440
  * repair, so applying one could only ever open the wrong file.
441
+ *
442
+ * `edit` asks the viewer to open the file straight into edit mode — for a
443
+ * page the caller has just created, which is there to be written, not read.
444
+ * It travels as router state (`startEditing`, the key the skill page
445
+ * already answers to), not in the URL, so a shared or bookmarked link never
446
+ * drops anyone into the editor; `FileViewer` honours it once and clears it.
447
+ * The path rides along (`startEditingPath`) because the URL does not keep
448
+ * naming it: `FileRoute` swaps a node's path URL for its id URL.
449
+ *
450
+ * `replace` swaps the current history entry instead of adding one — for
451
+ * asking to edit the page already on screen, where a second entry for the
452
+ * same URL would make Back look like it did nothing.
426
453
  */
427
454
  const openWorkspacePath = useCallback(
428
- (path: string) => {
455
+ (path: string, options?: { edit?: boolean; replace?: boolean }) => {
429
456
  if (!branch) return;
430
- navigate(kbFileUrl(branch, path));
457
+ const url = kbFileUrl(branch, path);
458
+ const replace = options?.replace === true;
459
+ const state = { startEditing: true, startEditingPath: path };
460
+ if (options?.edit) navigate(url, replace ? { state, replace } : { state });
461
+ else if (replace) navigate(url, { replace });
462
+ else navigate(url);
431
463
  },
432
464
  [branch, navigate],
433
465
  );
@@ -437,6 +469,19 @@ export function useFileNav() {
437
469
  navigate(kbFileUrl(branch));
438
470
  }, [branch, navigate]);
439
471
 
472
+ // One return for the app, one for a surface. The surface's verbs are
473
+ // substituted WHOLESALE rather than branched inside each opener: "open it
474
+ // in a new tab, never navigate" is a different behaviour, not a parameter
475
+ // of this one, and a half-overridden navigator is how an embed ends up on
476
+ // a page no token was minted for.
477
+ if (surface) {
478
+ return {
479
+ openFile: (pathOrUrl: string) => surface.openLink(pathOrUrl, ''),
480
+ openLink: surface.openLink,
481
+ openWorkspacePath: surface.openWorkspacePath,
482
+ closeFile: () => undefined,
483
+ };
484
+ }
440
485
  return { openFile, openLink, openWorkspacePath, closeFile };
441
486
  }
442
487
 
@@ -449,9 +494,14 @@ export function useFileNav() {
449
494
  * the chat citation renderer so both resolve id-links identically.
450
495
  */
451
496
  export function useNodeIdNav() {
497
+ const surface = useRendererSurface();
452
498
  const { openFile } = useFileNav();
453
- const git = useGit();
454
- const branch = git.status?.branch ?? null;
499
+ const git = useContext(GitContext);
500
+ const location = useLocation();
501
+ // The URL's branch first, the git status second — the same order as
502
+ // `useFileNav`: during a branch switch the status still names the branch
503
+ // being left, and an id resolved against it would open the wrong tree.
504
+ const branch = branchFromPathname(location.pathname) ?? git?.status?.branch ?? null;
455
505
 
456
506
  const openNodeId = useCallback(
457
507
  async (idOrLink: string) => {
@@ -476,5 +526,9 @@ export function useNodeIdNav() {
476
526
  [branch, openFile],
477
527
  );
478
528
 
529
+ // The surface resolves an id its own way — the embed hands it to its host
530
+ // as an app address, which the app then resolves with the reader's own
531
+ // session. Resolving it HERE would need a session the embed does not have.
532
+ if (surface) return { openNodeId: surface.openNodeId };
479
533
  return { openNodeId };
480
534
  }
@@ -0,0 +1,42 @@
1
+ import { useSyncExternalStore } from 'react';
2
+
3
+ /**
4
+ * The page on screen that an Edit click would open for editing right now, or
5
+ * null: no page open, already editing (or entering it, or proposing), a file
6
+ * with no editing surface, someone else holding the lock, or a reader who may
7
+ * only propose.
8
+ *
9
+ * Edit mode is the viewer's own state, and should stay so — but the command
10
+ * menu offers "Edit this page", and an entry that does nothing when chosen
11
+ * is worse than none. So the viewer PUBLISHES its verdict here, the same
12
+ * conditions its Edit button is drawn and enabled by, and the menu reads it.
13
+ * One string rather than a context, because the menu lives in the toolbar,
14
+ * above and beside the viewer, not under it.
15
+ */
16
+ let editablePage: string | null = null;
17
+ const listeners = new Set<() => void>();
18
+
19
+ function subscribe(listener: () => void): () => void {
20
+ listeners.add(listener);
21
+ return () => listeners.delete(listener);
22
+ }
23
+
24
+ /** Called by the viewer whenever its answer changes. */
25
+ export function publishEditablePage(path: string | null): void {
26
+ if (editablePage === path) return;
27
+ editablePage = path;
28
+ listeners.forEach((l) => l());
29
+ }
30
+
31
+ /** Withdraw `path` — but only if it is still the published one (another viewer may have taken over). */
32
+ export function withdrawEditablePage(path: string): void {
33
+ if (editablePage === path) publishEditablePage(null);
34
+ }
35
+
36
+ export function useEditablePage(): string | null {
37
+ return useSyncExternalStore(
38
+ subscribe,
39
+ () => editablePage,
40
+ () => null,
41
+ );
42
+ }
@@ -248,16 +248,19 @@ export interface WorkspaceContextValue {
248
248
  */
249
249
  addTab: (relativePath: string) => Promise<boolean>;
250
250
  /**
251
- * Close a tab. Prompts via `window.confirm` if the tab has unsaved edits;
251
+ * Close a tab. Asks in the app's own dialog if the tab has unsaved edits;
252
252
  * pass `{ skipConfirm: true }` from bulk-close paths that have already
253
253
  * collected one consolidated confirm. If the closed tab was active,
254
254
  * activates the tab to its left (or right) and returns its path so the
255
255
  * caller can update the URL. Returns `{ closed: false }` when the user
256
- * cancelled the dirty-confirm.
256
+ * cancelled the dirty-confirm, or when the workspace moved to another
257
+ * branch while it was asked. A bulk close that asked its own question
258
+ * passes the `workspaceId` it asked in, and closes nothing if that has
259
+ * moved on since: the same path on the new branch is a different tab.
257
260
  */
258
261
  closeTab: (
259
262
  tab: OpenTab,
260
- options?: { skipConfirm?: boolean },
263
+ options?: { skipConfirm?: boolean; workspaceId?: string | null },
261
264
  ) => Promise<{ closed: boolean; newActivePath: string | null }>;
262
265
  /** Activate a tab without prompting. Triggers a refetch if the tab's cache was invalidated. */
263
266
  activateTab: (tab: OpenTab) => void;
@@ -283,7 +286,13 @@ export interface WorkspaceContextValue {
283
286
  activePath: string | null,
284
287
  ) => Promise<HydrateResult>;
285
288
 
286
- createFile: (relativePath: string, content?: string) => Promise<void>;
289
+ /**
290
+ * Write a new file and refresh the tree. Unconditional by default, as the
291
+ * explorer's New file has always been; `ifAbsent` makes it an exclusive
292
+ * create, refused with a 409 `WorkspaceApiError` when the path already
293
+ * exists, for a caller that picked the name from a tree that may be stale.
294
+ */
295
+ createFile: (relativePath: string, content?: string, options?: { ifAbsent?: boolean }) => Promise<void>;
287
296
  createDirectory: (relativePath: string) => Promise<void>;
288
297
  /**
289
298
  * Extract a `.zip` file already in the workspace. Defaults to "unzip here":
@@ -0,0 +1,60 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { displayFileName, fileNameTooltip, isAccessRulesFile } from '../display-file-name';
3
+
4
+ const KB = 'knowledge-base';
5
+
6
+ describe('displayFileName', () => {
7
+ it("calls a plugin's plugin.json Plugin settings, from a workspace path or a repository path", () => {
8
+ expect(displayFileName('knowledge-base/Plugins/GTM/plugin.json', KB)).toBe('Plugin settings');
9
+ expect(displayFileName('Plugins/GTM/plugin.json')).toBe('Plugin settings');
10
+ expect(fileNameTooltip('knowledge-base/Plugins/GTM/plugin.json', KB)).toBe('plugin.json');
11
+ });
12
+
13
+ it('names a plugin under grouping folders the same way: a plugin has no fixed depth', () => {
14
+ expect(displayFileName('knowledge-base/Plugins/departments/engineering/shared/ado/plugin.json', KB)).toBe(
15
+ 'Plugin settings',
16
+ );
17
+ expect(displayFileName('Plugins/departments/engineering/shared/ado/plugin.json')).toBe('Plugin settings');
18
+ });
19
+
20
+ it("leaves a skill's bundled plugin.json alone: it is an example, not a plugin's settings", () => {
21
+ expect(displayFileName('knowledge-base/Plugins/GTM/skills/demo/plugin.json', KB)).toBe('plugin.json');
22
+ expect(displayFileName('Plugins/departments/ado/skills/demo/assets/plugin.json')).toBe('plugin.json');
23
+ // A plugin that is itself called skills is still a plugin.
24
+ expect(displayFileName('Plugins/skills/plugin.json')).toBe('Plugin settings');
25
+ expect(displayFileName('knowledge-base/Plugins/departments/skills/plugin.json', KB)).toBe('Plugin settings');
26
+ });
27
+
28
+ it('knows the plugins root only at the top of the repository', () => {
29
+ // A folder somebody called Plugins inside the knowledge tree holds no plugins.
30
+ expect(displayFileName('knowledge-base/KnowledgeBase/Plugins/x/plugin.json', KB)).toBe('plugin.json');
31
+ expect(displayFileName('KnowledgeBase/Plugins/x/plugin.json')).toBe('plugin.json');
32
+ // A workspace path is read from the clone folder; without it the root cannot be placed.
33
+ expect(displayFileName('knowledge-base/Plugins/GTM/plugin.json')).toBe('plugin.json');
34
+ expect(displayFileName('elsewhere/Plugins/GTM/plugin.json', KB)).toBe('plugin.json');
35
+ expect(displayFileName('knowledge-base/KnowledgeBase/plugin.json', KB)).toBe('plugin.json');
36
+ expect(displayFileName('Plugins/plugin.json')).toBe('plugin.json');
37
+ expect(displayFileName('plugin.json')).toBe('plugin.json');
38
+ expect(fileNameTooltip('knowledge-base/KnowledgeBase/plugin.json', KB)).toBeUndefined();
39
+ });
40
+
41
+ it("calls a folder's access.md Who has access, at any depth and in any case", () => {
42
+ expect(displayFileName('knowledge-base/access.md', KB)).toBe('Who has access');
43
+ expect(displayFileName('knowledge-base/Plugins/GTM/access.md', KB)).toBe('Who has access');
44
+ expect(displayFileName('KnowledgeBase/Legal/access.md')).toBe('Who has access');
45
+ expect(displayFileName('KnowledgeBase/Legal/Access.MD')).toBe('Who has access');
46
+ expect(fileNameTooltip('knowledge-base/KnowledgeBase/Legal/access.md', KB)).toBe('access.md');
47
+ expect(isAccessRulesFile('knowledge-base/KnowledgeBase/Legal/access.md')).toBe(true);
48
+ expect(isAccessRulesFile('KnowledgeBase/Legal/ACCESS.md')).toBe(true);
49
+ // A note that only mentions access in its name is a note.
50
+ expect(displayFileName('knowledge-base/KnowledgeBase/access-policy.md', KB)).toBe('access-policy.md');
51
+ expect(isAccessRulesFile('knowledge-base/KnowledgeBase/access-policy.md')).toBe(false);
52
+ });
53
+
54
+ it('shows every other file by its own name, with no tooltip of its own', () => {
55
+ expect(displayFileName('knowledge-base/KnowledgeBase/Handbook.md', KB)).toBe('Handbook.md');
56
+ expect(displayFileName('Plugins/GTM/mcp.json')).toBe('mcp.json');
57
+ expect(displayFileName('README.md')).toBe('README.md');
58
+ expect(fileNameTooltip('knowledge-base/KnowledgeBase/Handbook.md', KB)).toBeUndefined();
59
+ });
60
+ });
@@ -2,6 +2,8 @@ import { describe, it, expect } from 'vitest';
2
2
  import type { FileTreeEntry } from '@bevel-software/platform-shared';
3
3
  import {
4
4
  checkoutRoot,
5
+ findEntryByPath,
6
+ knowledgeFiles,
5
7
  mergePendingIntoTree,
6
8
  omitPathFromTree,
7
9
  pathExistsInTree,
@@ -61,6 +63,54 @@ describe('subtreeHasVisibleEntries', () => {
61
63
  });
62
64
  });
63
65
 
66
+ /** The one exact-path lookup the workspace and the Get set up column share. */
67
+ describe('findEntryByPath', () => {
68
+ const page: FileTreeEntry = { name: 'note.md', relativePath: 'knowledge-base/KnowledgeBase/note.md', type: 'file' };
69
+ const folder: FileTreeEntry = {
70
+ name: 'KnowledgeBase',
71
+ relativePath: 'knowledge-base/KnowledgeBase',
72
+ type: 'directory',
73
+ children: [page],
74
+ };
75
+ const tree: FileTreeEntry = {
76
+ name: '.',
77
+ relativePath: '.',
78
+ type: 'directory',
79
+ children: [{ name: 'knowledge-base', relativePath: 'knowledge-base', type: 'directory', children: [folder] }],
80
+ };
81
+
82
+ it('finds a folder and a file at their exact paths', () => {
83
+ expect(findEntryByPath(tree, 'knowledge-base/KnowledgeBase')).toBe(folder);
84
+ expect(findEntryByPath(tree, 'knowledge-base/KnowledgeBase/note.md')).toBe(page);
85
+ });
86
+
87
+ it('answers null for a path the tree does not have, a prefix of a name included, and for no tree', () => {
88
+ expect(findEntryByPath(tree, 'knowledge-base/Knowledge')).toBeNull();
89
+ expect(findEntryByPath(tree, 'knowledge-base/KnowledgeBase/other.md')).toBeNull();
90
+ expect(findEntryByPath(null, 'knowledge-base')).toBeNull();
91
+ });
92
+
93
+ it('never opens a folder off the path', () => {
94
+ let opened = 0;
95
+ const skills: FileTreeEntry = { name: 'Skills', relativePath: 'knowledge-base/Skills', type: 'directory' };
96
+ Object.defineProperty(skills, 'children', {
97
+ get: () => {
98
+ opened++;
99
+ return [{ name: 'a.md', relativePath: 'knowledge-base/Skills/a.md', type: 'file' }];
100
+ },
101
+ });
102
+ const wide: FileTreeEntry = {
103
+ name: '.',
104
+ relativePath: '.',
105
+ type: 'directory',
106
+ children: [{ name: 'knowledge-base', relativePath: 'knowledge-base', type: 'directory', children: [skills, folder] }],
107
+ };
108
+ expect(findEntryByPath(wide, 'knowledge-base/KnowledgeBase/note.md')).toBe(page);
109
+ expect(findEntryByPath(wide, 'knowledge-base/Missing.md')).toBeNull();
110
+ expect(opened).toBe(0);
111
+ });
112
+ });
113
+
64
114
  /**
65
115
  * What was kept out of ONE root is that root's own count, set by the server
66
116
  * per folder: a Skills tree emptied by the read rules says "nothing shared",
@@ -225,6 +275,31 @@ describe('suggestedPages', () => {
225
275
  });
226
276
  });
227
277
 
278
+ describe('knowledgeFiles', () => {
279
+ it('lists every file the Knowledge explorer browses, not only documents — but never access rules or Plugins/', () => {
280
+ expect(knowledgeFiles(TREE, KB).map((e) => e.relativePath)).toEqual([
281
+ 'knowledge-base/KnowledgeBase/Onboarding.md',
282
+ 'knowledge-base/KnowledgeBase/GTM/Pricing.md',
283
+ 'knowledge-base/KnowledgeBase/GTM/deals.csv',
284
+ ]);
285
+ });
286
+
287
+ it('skips dot-prefixed bookkeeping and is empty without a checkout', () => {
288
+ const tree = dir('', [
289
+ dir(KB, [
290
+ dir(`${KB}/KnowledgeBase`, [
291
+ file(`${KB}/KnowledgeBase/.bevelignore`),
292
+ dir(`${KB}/KnowledgeBase/.git`, [file(`${KB}/KnowledgeBase/.git/HEAD`)]),
293
+ file(`${KB}/KnowledgeBase/Team.md`),
294
+ ]),
295
+ ]),
296
+ ]);
297
+ expect(knowledgeFiles(tree, KB).map((e) => e.name)).toEqual(['Team.md']);
298
+ expect(knowledgeFiles(null, KB)).toEqual([]);
299
+ expect(knowledgeFiles(TREE, null)).toEqual([]);
300
+ });
301
+ });
302
+
228
303
  describe('omitPathFromTree', () => {
229
304
  it('removes only the exact root control file and preserves a nested namesake', () => {
230
305
  const tree = dir('', [
@@ -8,6 +8,7 @@ import {
8
8
  moveQuestion,
9
9
  moveSentence,
10
10
  moveWarnings,
11
+ platformFileDeleteRefusal,
11
12
  platformFileDragRefusal,
12
13
  platformFileMoveRefusal,
13
14
  principalLabel,
@@ -224,6 +225,21 @@ const KB = 'knowledge-base';
224
225
  * The refusal is the server's sentence, decided by the shared predicate, so
225
226
  * the sidebar cannot drift from the endpoint it would have called.
226
227
  */
228
+ describe('platformFileDeleteRefusal', () => {
229
+ it('refuses only the repository\'s own files: the root access.md and roles.yaml', () => {
230
+ expect(platformFileDeleteRefusal(`${KB}/access.md`, KB)).toBe("access.md is the repository's own file and cannot be deleted.");
231
+ expect(platformFileDeleteRefusal(`${KB}/roles.yaml`, KB)).toBe("roles.yaml is the repository's own file and cannot be deleted.");
232
+ });
233
+
234
+ it('leaves a nested access.md, any .bevelignore and content to the server\'s write gate', () => {
235
+ expect(platformFileDeleteRefusal(`${KB}/Team/access.md`, KB)).toBeNull();
236
+ expect(platformFileDeleteRefusal(`${KB}/Team/roles.yaml`, KB)).toBeNull();
237
+ expect(platformFileDeleteRefusal(`${KB}/.bevelignore`, KB)).toBeNull();
238
+ expect(platformFileDeleteRefusal(`${KB}/Access.md`, KB)).toBeNull();
239
+ expect(platformFileDeleteRefusal('access.md', KB)).toBeNull();
240
+ });
241
+ });
242
+
227
243
  describe('platformFileMoveRefusal', () => {
228
244
  it('refuses the three platform files where the platform reads them', () => {
229
245
  expect(platformFileMoveRefusal(`${KB}/access.md`, KB))
@@ -0,0 +1,102 @@
1
+ /**
2
+ * The workspace's own: what a knowledge-base file is CALLED on screen is a
3
+ * fact about the workspace's layout (its plugins root, its access rules),
4
+ * so it lives here with the tree it names, not among the shared components
5
+ * that know nothing of either. Other modules — the change-request views,
6
+ * the history preview, the search — read it from here.
7
+ */
8
+ import { PLUGIN_MANIFEST_FILE, PLUGIN_SKILLS_DIR, currentKbLayout } from '@bevel-software/platform-shared';
9
+
10
+ /**
11
+ * What the app CALLS a file, wherever it names one: a row in the tree, a tab,
12
+ * a page's title, the bar above a document, a search result.
13
+ *
14
+ * Almost always that is the file's own name. The exceptions are the files the
15
+ * platform reads as configuration, whose names only make sense to someone who
16
+ * knows how Hexis is built: a plugin's `plugin.json` is its settings, and a
17
+ * folder's `access.md` is who has access to it. Those read in plain words
18
+ * here, and only here, so no surface can call the same file two different
19
+ * things.
20
+ *
21
+ * DISPLAY ONLY. The file on disk, its URL, every API and everything an agent
22
+ * reads keep the real name; {@link fileNameTooltip} hands it to whoever
23
+ * hovers, so it is never hidden from someone who needs it.
24
+ *
25
+ * Paths, not bare names: a `plugin.json` is the plugin's settings only in a
26
+ * plugin's own folder under the plugins root, and the plugins root is the one
27
+ * at the top of the repository — a folder a person happened to call `Plugins`
28
+ * inside the knowledge tree holds no plugins. So a caller says what its path
29
+ * is relative to: `kbDirName` for a workspace path (`<kbDirName>/Plugins/…`),
30
+ * null for one that is already repository-relative (a change request's file
31
+ * list). A plugin may sit under grouping folders, so its depth is not fixed;
32
+ * what is fixed is that a `plugin.json` inside a plugin's `skills/` is a
33
+ * skill's bundled example and nothing more. An `access.md` governs its folder
34
+ * at any depth, so its name alone is enough.
35
+ */
36
+
37
+ /** The access rules file's name — a platform file at any depth, in any case. */
38
+ const ACCESS_RULES_FILE = 'access.md';
39
+
40
+ function baseName(path: string): string {
41
+ return path.slice(path.lastIndexOf('/') + 1);
42
+ }
43
+
44
+ /**
45
+ * The path's segments from the repository root, or null for a workspace path
46
+ * that does not start with the clone folder: that file is outside the
47
+ * repository and under no root at all.
48
+ */
49
+ function repoSegments(path: string, kbDirName: string | null): string[] | null {
50
+ const segments = path.split('/').filter(Boolean);
51
+ if (kbDirName === null) return segments;
52
+ return segments[0] === kbDirName ? segments.slice(1) : null;
53
+ }
54
+
55
+ /**
56
+ * Whether repository-relative `segments` name a plugin's own manifest: a
57
+ * `plugin.json` under the plugins root, in a plugin's folder at any depth
58
+ * (plugins sit under grouping folders too), and not inside a plugin's
59
+ * `skills/`, where one is a skill's bundled example. A skill's example sits
60
+ * at least one folder below a `skills` segment (`<plugin>/skills/<skill>/…`),
61
+ * so only a `skills` segment ABOVE the file's own folder excludes it: a
62
+ * plugin that is itself called `skills` keeps its settings.
63
+ */
64
+ function isPluginManifest(segments: string[]): boolean {
65
+ if (segments.length < 3) return false;
66
+ if (segments[0] !== currentKbLayout().pluginsDir) return false;
67
+ if (segments[segments.length - 1] !== PLUGIN_MANIFEST_FILE) return false;
68
+ return !segments.slice(1, -2).includes(PLUGIN_SKILLS_DIR);
69
+ }
70
+
71
+ /** The plain name of a platform file, or null for every other file. */
72
+ function plainName(path: string, kbDirName: string | null): string | null {
73
+ if (isAccessRulesFile(path)) return 'Who has access';
74
+ const segments = repoSegments(path, kbDirName);
75
+ return segments && isPluginManifest(segments) ? 'Plugin settings' : null;
76
+ }
77
+
78
+ /**
79
+ * Whether `path` is a folder's access rules — the file "Who has access" names.
80
+ * The one rule for it, wherever the app asks: the tree's suggestions and the
81
+ * file page both read it from here.
82
+ */
83
+ export function isAccessRulesFile(path: string): boolean {
84
+ return baseName(path).toLowerCase() === ACCESS_RULES_FILE;
85
+ }
86
+
87
+ /**
88
+ * The name to show for the file at `path`. `kbDirName` is the clone folder a
89
+ * workspace path starts with; null (the default) says the path is already
90
+ * repository-relative.
91
+ */
92
+ export function displayFileName(path: string, kbDirName: string | null = null): string {
93
+ return plainName(path, kbDirName) ?? baseName(path);
94
+ }
95
+
96
+ /**
97
+ * The file's real name, for a `title` — set only when {@link displayFileName}
98
+ * shows something else, so an ordinary file keeps whatever tooltip it had.
99
+ */
100
+ export function fileNameTooltip(path: string, kbDirName: string | null = null): string | undefined {
101
+ return plainName(path, kbDirName) === null ? undefined : baseName(path);
102
+ }