@unson/brainbase-mcp 0.8.0 → 0.10.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.
@@ -0,0 +1,290 @@
1
+ /*
2
+ * Browser-only Sigma entry point.
3
+ *
4
+ * Keep all DOM/WebGL work behind this module. The sibling contract module is
5
+ * intentionally safe to import from Node tests and from hosts that render an
6
+ * accessible list instead of a graph.
7
+ */
8
+
9
+ import { MultiDirectedGraph, Sigma } from './project-graph-vendor.js';
10
+ import {
11
+ PROJECT_GRAPH_TYPE_COLORS,
12
+ createProjectGraphModel,
13
+ projectGraphNeighborIds,
14
+ } from './project-graph.js';
15
+
16
+ const DEFAULT_EDGE_COLOR = '#cbd5e1';
17
+ const SELECTED_COLOR = '#312e81';
18
+ const UNRELATED_COLOR = '#cbd5e1';
19
+ const UNRELATED_EDGE_COLOR = '#e2e8f0';
20
+
21
+ function isRecord(value) {
22
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
23
+ }
24
+
25
+ function asDocument(container) {
26
+ return container?.ownerDocument || (typeof document !== 'undefined' ? document : null);
27
+ }
28
+
29
+ function abortError() {
30
+ const error = new Error('project_graph_mount_aborted');
31
+ error.name = 'AbortError';
32
+ return error;
33
+ }
34
+
35
+ function assertMountTarget(container) {
36
+ if (!container || typeof container.appendChild !== 'function') {
37
+ throw new TypeError('mountProjectGraph requires a DOM container');
38
+ }
39
+ const doc = asDocument(container);
40
+ if (!doc || typeof doc.createElement !== 'function') {
41
+ throw new TypeError('mountProjectGraph requires a DOM document');
42
+ }
43
+ return doc;
44
+ }
45
+
46
+ function setText(element, text) {
47
+ if (element) element.textContent = text;
48
+ }
49
+
50
+ function makeButton(doc, label, ariaLabel) {
51
+ const button = doc.createElement('button');
52
+ button.type = 'button';
53
+ button.className = 'bb-project-graph-control';
54
+ button.setAttribute('aria-label', ariaLabel);
55
+ button.textContent = label;
56
+ return button;
57
+ }
58
+
59
+ function makeGraph(model) {
60
+ const graph = new MultiDirectedGraph();
61
+ for (const node of model.nodes) {
62
+ graph.addNode(node.id, {
63
+ x: Number.isFinite(node.x) ? node.x : 0,
64
+ y: Number.isFinite(node.y) ? node.y : 0,
65
+ size: node.size,
66
+ label: node.label,
67
+ color: node.color || PROJECT_GRAPH_TYPE_COLORS.unknown,
68
+ baseColor: node.color || PROJECT_GRAPH_TYPE_COLORS.unknown,
69
+ semanticType: node.type,
70
+ // Sigma's renderer program type is deliberately separate from the
71
+ // ontology type. Unknown ontology types must still draw as circles.
72
+ type: 'circle',
73
+ highlighted: false,
74
+ });
75
+ }
76
+ for (const edge of model.edges) {
77
+ graph.addDirectedEdgeWithKey(edge.id, edge.source, edge.target, {
78
+ label: edge.label || '',
79
+ color: edge.color || DEFAULT_EDGE_COLOR,
80
+ baseColor: edge.color || DEFAULT_EDGE_COLOR,
81
+ size: 1,
82
+ type: 'arrow',
83
+ });
84
+ }
85
+ return graph;
86
+ }
87
+
88
+ function edgeTouches(edge, nodeId, neighborIds) {
89
+ if (!nodeId) return false;
90
+ return edge.source === nodeId || edge.target === nodeId ||
91
+ (neighborIds.has(edge.source) && neighborIds.has(edge.target));
92
+ }
93
+
94
+ /**
95
+ * Mounts one project graph and returns an idempotent resource handle.
96
+ *
97
+ * The graph is built only from the supplied projection. Invalid endpoints have
98
+ * already been removed by createProjectGraphModel, so this renderer never
99
+ * invents a node merely to make an edge drawable.
100
+ */
101
+ export function mountProjectGraph(container, options = {}) {
102
+ const doc = assertMountTarget(container);
103
+ const signal = options?.signal;
104
+ if (signal?.aborted) throw abortError();
105
+
106
+ const model = createProjectGraphModel(
107
+ { nodes: options?.nodes, edges: options?.edges },
108
+ { selectedId: options?.selectedId || null },
109
+ );
110
+ const mountRoot = doc.createElement('div');
111
+ mountRoot.className = 'bb-project-graph';
112
+ mountRoot.setAttribute('data-graph-contract', 'brainbase.project-graph.v1');
113
+
114
+ const toolbar = doc.createElement('div');
115
+ toolbar.className = 'bb-project-graph-controls';
116
+ toolbar.setAttribute('role', 'toolbar');
117
+ toolbar.setAttribute('aria-label', 'グラフ表示の操作');
118
+ const zoomIn = makeButton(doc, '+', 'グラフを拡大');
119
+ const zoomOut = makeButton(doc, '−', 'グラフを縮小');
120
+ const reset = makeButton(doc, '全体', 'グラフ全体を表示');
121
+ toolbar.append(zoomIn, zoomOut, reset);
122
+
123
+ const status = doc.createElement('p');
124
+ status.className = 'bb-project-graph-status';
125
+ status.setAttribute('aria-live', 'polite');
126
+ status.setAttribute('aria-atomic', 'true');
127
+
128
+ const surface = doc.createElement('div');
129
+ surface.className = 'bb-project-graph-surface';
130
+ surface.setAttribute('role', 'img');
131
+ surface.setAttribute('aria-label', 'プロジェクトの関係グラフ');
132
+ surface.style.position = 'relative';
133
+ surface.style.width = '100%';
134
+ surface.style.height = '100%';
135
+
136
+ mountRoot.append(toolbar, status, surface);
137
+ container.appendChild(mountRoot);
138
+
139
+ let renderer;
140
+ let selectedId = null;
141
+ let neighborIds = new Set();
142
+ let destroyed = false;
143
+ let abortListener = null;
144
+ const nodeById = new Map(model.nodes.map((node) => [node.id, node]));
145
+ const edgeById = new Map(model.edges.map((edge) => [edge.id, edge]));
146
+ let onZoomIn;
147
+ let onZoomOut;
148
+ let onReset;
149
+
150
+ const getNeighbors = () => neighborIds;
151
+
152
+ const nodeReducer = (nodeId, data) => {
153
+ const base = { ...data };
154
+ const neighbors = getNeighbors();
155
+ if (!selectedId) return base;
156
+ if (nodeId === selectedId) {
157
+ return {
158
+ ...base,
159
+ color: SELECTED_COLOR,
160
+ size: Math.max(11, (base.size || 9) + 4),
161
+ highlighted: true,
162
+ forceLabel: true,
163
+ };
164
+ }
165
+ if (neighbors.has(nodeId)) {
166
+ return {
167
+ ...base,
168
+ color: base.baseColor || PROJECT_GRAPH_TYPE_COLORS.unknown,
169
+ size: Math.max(9, (base.size || 9) + 1),
170
+ highlighted: true,
171
+ forceLabel: true,
172
+ };
173
+ }
174
+ return {
175
+ ...base,
176
+ color: UNRELATED_COLOR,
177
+ size: Math.max(5, (base.size || 9) * 0.72),
178
+ label: null,
179
+ forceLabel: false,
180
+ highlighted: false,
181
+ };
182
+ };
183
+
184
+ const edgeReducer = (edgeId, data) => {
185
+ const edge = edgeById.get(edgeId);
186
+ const base = { ...data };
187
+ if (!selectedId || !edge) return { ...base, label: null, color: base.baseColor || DEFAULT_EDGE_COLOR };
188
+ const neighbors = getNeighbors();
189
+ if (!edgeTouches(edge, selectedId, neighbors)) {
190
+ return { ...base, label: null, color: UNRELATED_EDGE_COLOR, size: 0.5 };
191
+ }
192
+ return {
193
+ ...base,
194
+ label: edge.label || null,
195
+ forceLabel: Boolean(edge.label),
196
+ color: edge.color || DEFAULT_EDGE_COLOR,
197
+ size: 1.5,
198
+ };
199
+ };
200
+
201
+ const updateStatus = () => {
202
+ if (!selectedId) {
203
+ setText(status, `${model.nodes.length}件の対象、${model.edges.length}件の関係。点を選択すると周辺を強調します。`);
204
+ return;
205
+ }
206
+ const selected = nodeById.get(selectedId);
207
+ const neighbors = getNeighbors();
208
+ setText(status, `${selected?.label || selectedId}を選択中。関連する対象${neighbors.size}件を強調しています。`);
209
+ };
210
+
211
+ const notifySelection = () => {
212
+ if (typeof options?.onSelect === 'function') options.onSelect(selectedId);
213
+ };
214
+
215
+ const setSelected = (nextId, { notify = true } = {}) => {
216
+ if (destroyed) return;
217
+ selectedId = nextId && nodeById.has(nextId) ? nextId : null;
218
+ neighborIds = projectGraphNeighborIds(model.nodes, model.edges, selectedId);
219
+ updateStatus();
220
+ if (renderer) renderer.refresh();
221
+ if (notify) notifySelection();
222
+ };
223
+
224
+ try {
225
+ renderer = new Sigma(makeGraph(model), surface, {
226
+ allowInvalidContainer: true,
227
+ renderLabels: true,
228
+ renderEdgeLabels: true,
229
+ labelDensity: 0.8,
230
+ labelRenderedSizeThreshold: 8,
231
+ labelGridCellSize: 120,
232
+ defaultNodeType: 'circle',
233
+ defaultEdgeType: 'arrow',
234
+ defaultNodeColor: PROJECT_GRAPH_TYPE_COLORS.unknown,
235
+ defaultEdgeColor: DEFAULT_EDGE_COLOR,
236
+ nodeReducer,
237
+ edgeReducer,
238
+ });
239
+ } catch (error) {
240
+ // A missing WebGL context is a renderer capability failure. Let the host
241
+ // choose its accessible list fallback while retaining the original cause.
242
+ mountRoot.remove();
243
+ if (isRecord(error)) error.code = 'project_graph_renderer_unavailable';
244
+ throw error;
245
+ }
246
+
247
+ if (signal?.aborted) {
248
+ renderer.kill();
249
+ mountRoot.remove();
250
+ throw abortError();
251
+ }
252
+
253
+ const onNodeClick = ({ node }) => setSelected(node);
254
+ const onStageClick = () => setSelected(null);
255
+ renderer.on('clickNode', onNodeClick);
256
+ renderer.on('clickStage', onStageClick);
257
+ onZoomIn = () => renderer?.getCamera().animatedZoom({ factor: 1.5 });
258
+ onZoomOut = () => renderer?.getCamera().animatedUnzoom({ factor: 1.5 });
259
+ onReset = () => renderer?.getCamera().animatedReset();
260
+ zoomIn.addEventListener('click', onZoomIn);
261
+ zoomOut.addEventListener('click', onZoomOut);
262
+ reset.addEventListener('click', onReset);
263
+ setSelected(options?.selectedId || null, { notify: false });
264
+
265
+ const destroy = () => {
266
+ if (destroyed) return;
267
+ destroyed = true;
268
+ if (signal && abortListener) signal.removeEventListener('abort', abortListener);
269
+ renderer?.off('clickNode', onNodeClick);
270
+ renderer?.off('clickStage', onStageClick);
271
+ zoomIn.removeEventListener('click', onZoomIn);
272
+ zoomOut.removeEventListener('click', onZoomOut);
273
+ reset.removeEventListener('click', onReset);
274
+ renderer?.kill();
275
+ renderer = null;
276
+ mountRoot.remove();
277
+ };
278
+ abortListener = () => destroy();
279
+ signal?.addEventListener('abort', abortListener, { once: true });
280
+ updateStatus();
281
+
282
+ return {
283
+ destroy,
284
+ select: (nodeId) => setSelected(nodeId),
285
+ getSelectedId: () => selectedId,
286
+ getModel: () => model,
287
+ };
288
+ }
289
+
290
+ export default mountProjectGraph;
@@ -0,0 +1,109 @@
1
+ Third-party licenses bundled in ui/project-graph-vendor.js
2
+
3
+ sigma 3.0.3 (MIT)
4
+ Copyright (C) 2013-2025, Alexis Jacomy, Guillaume Plique, Benoît Simard https://www.sigmajs.org
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
7
+ to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
+ and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
15
+ IN THE SOFTWARE.
16
+
17
+ graphology 0.26.0 (MIT)
18
+ The MIT License (MIT)
19
+
20
+ Copyright (c) 2016-2021 Guillaume Plique (Yomguithereal)
21
+
22
+ Permission is hereby granted, free of charge, to any person obtaining a copy
23
+ of this software and associated documentation files (the "Software"), to deal
24
+ in the Software without restriction, including without limitation the rights
25
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26
+ copies of the Software, and to permit persons to whom the Software is
27
+ furnished to do so, subject to the following conditions:
28
+
29
+ The above copyright notice and this permission notice shall be included in
30
+ all copies or substantial portions of the Software.
31
+
32
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38
+ THE SOFTWARE.
39
+
40
+ graphology-utils 2.5.2 (MIT)
41
+ The MIT License (MIT)
42
+
43
+ Copyright (c) 2017-2021 Guillaume Plique (Yomguithereal)
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining a copy
46
+ of this software and associated documentation files (the "Software"), to deal
47
+ in the Software without restriction, including without limitation the rights
48
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49
+ copies of the Software, and to permit persons to whom the Software is
50
+ furnished to do so, subject to the following conditions:
51
+
52
+ The above copyright notice and this permission notice shall be included in
53
+ all copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
61
+ THE SOFTWARE.
62
+
63
+ graphology-types 0.24.8 (MIT)
64
+ The MIT License (MIT)
65
+
66
+ Copyright (c) 2020-2021 Guillaume Plique (Yomguithereal)
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining a copy
69
+ of this software and associated documentation files (the "Software"), to deal
70
+ in the Software without restriction, including without limitation the rights
71
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
72
+ copies of the Software, and to permit persons to whom the Software is
73
+ furnished to do so, subject to the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be included in
76
+ all copies or substantial portions of the Software.
77
+
78
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
81
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
83
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
84
+ THE SOFTWARE.
85
+
86
+ events 3.3.0 (MIT)
87
+ MIT
88
+
89
+ Copyright Joyent, Inc. and other Node contributors.
90
+
91
+ Permission is hereby granted, free of charge, to any person obtaining a
92
+ copy of this software and associated documentation files (the
93
+ "Software"), to deal in the Software without restriction, including
94
+ without limitation the rights to use, copy, modify, merge, publish,
95
+ distribute, sublicense, and/or sell copies of the Software, and to permit
96
+ persons to whom the Software is furnished to do so, subject to the
97
+ following conditions:
98
+
99
+ The above copyright notice and this permission notice shall be included
100
+ in all copies or substantial portions of the Software.
101
+
102
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
103
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
104
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
105
+ NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
106
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
107
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
108
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
109
+