@notis_ai/cli 0.2.0 → 0.2.1

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 (58) hide show
  1. package/README.md +39 -10
  2. package/package.json +7 -1
  3. package/src/command-specs/apps.js +852 -47
  4. package/src/command-specs/helpers.js +28 -3
  5. package/src/command-specs/index.js +1 -1
  6. package/src/command-specs/tools.js +33 -6
  7. package/src/runtime/agent-browser.js +192 -0
  8. package/src/runtime/app-boundary-validator.js +132 -0
  9. package/src/runtime/app-dev-server.js +577 -0
  10. package/src/runtime/app-dev-sessions.js +87 -0
  11. package/src/runtime/app-platform.js +646 -71
  12. package/src/runtime/cli-mode.generated.js +4 -0
  13. package/src/runtime/cli-mode.js +29 -0
  14. package/src/runtime/output.js +2 -2
  15. package/src/runtime/ports.js +15 -0
  16. package/src/runtime/profiles.js +34 -3
  17. package/src/runtime/transport.js +129 -4
  18. package/template/.harness/index.html.tmpl +260 -0
  19. package/template/app/globals.css +3 -0
  20. package/template/app/layout.tsx +6 -0
  21. package/template/app/page.tsx +55 -0
  22. package/template/components/ui/badge.tsx +28 -0
  23. package/template/components/ui/button.tsx +53 -0
  24. package/template/components/ui/card.tsx +56 -0
  25. package/template/components.json +20 -0
  26. package/template/lib/utils.ts +6 -0
  27. package/template/notis.config.ts +33 -0
  28. package/template/package.json +31 -0
  29. package/template/packages/notis-sdk/package.json +26 -0
  30. package/template/packages/notis-sdk/src/components/MultiSelectActionBar.tsx +272 -0
  31. package/template/packages/notis-sdk/src/components/MultiSelectCheckbox.tsx +91 -0
  32. package/template/packages/notis-sdk/src/components/MultiSelectDragOverlay.tsx +39 -0
  33. package/template/packages/notis-sdk/src/config.ts +71 -0
  34. package/template/packages/notis-sdk/src/helpers.ts +131 -0
  35. package/template/packages/notis-sdk/src/hooks/useAppState.ts +50 -0
  36. package/template/packages/notis-sdk/src/hooks/useBackend.ts +41 -0
  37. package/template/packages/notis-sdk/src/hooks/useCollectionItem.ts +58 -0
  38. package/template/packages/notis-sdk/src/hooks/useDatabase.ts +88 -0
  39. package/template/packages/notis-sdk/src/hooks/useDocument.ts +61 -0
  40. package/template/packages/notis-sdk/src/hooks/useMultiSelect.ts +502 -0
  41. package/template/packages/notis-sdk/src/hooks/useNotis.ts +33 -0
  42. package/template/packages/notis-sdk/src/hooks/useNotisNavigation.ts +49 -0
  43. package/template/packages/notis-sdk/src/hooks/useTool.ts +49 -0
  44. package/template/packages/notis-sdk/src/hooks/useTools.ts +56 -0
  45. package/template/packages/notis-sdk/src/hooks/useTopBarSearch.ts +73 -0
  46. package/template/packages/notis-sdk/src/hooks/useUpsertDocument.ts +58 -0
  47. package/template/packages/notis-sdk/src/index.ts +67 -0
  48. package/template/packages/notis-sdk/src/provider.tsx +43 -0
  49. package/template/packages/notis-sdk/src/runtime.ts +182 -0
  50. package/template/packages/notis-sdk/src/styles.css +38 -0
  51. package/template/packages/notis-sdk/src/ui.ts +15 -0
  52. package/template/packages/notis-sdk/src/vite.ts +56 -0
  53. package/template/packages/notis-sdk/tsconfig.json +15 -0
  54. package/template/postcss.config.mjs +8 -0
  55. package/template/tailwind.config.ts +58 -0
  56. package/template/tsconfig.json +22 -0
  57. package/template/vite.config.ts +10 -0
  58. package/src/runtime/app-preview-server.js +0 -272
@@ -0,0 +1,502 @@
1
+ 'use client';
2
+
3
+ import {
4
+ useCallback,
5
+ useEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ type MouseEvent as ReactMouseEvent,
10
+ } from 'react';
11
+
12
+ const ROW_ATTR = 'data-notis-row-id';
13
+ const INTERACTIVE_SELECTOR = 'button, a, input, textarea, select, [role="button"]';
14
+
15
+ export interface DragRect {
16
+ left: number;
17
+ top: number;
18
+ width: number;
19
+ height: number;
20
+ }
21
+
22
+ export interface UseMultiSelectOptions<T> {
23
+ items: T[];
24
+ getId: (item: T) => string;
25
+ /** When false, suppresses the global Esc / Cmd+A / X / Shift+Arrow listeners. Default true. */
26
+ bindKeyboardShortcuts?: boolean;
27
+ /** When false, disables the rubber-band drag-select rectangle. Default true. */
28
+ enableDragSelect?: boolean;
29
+ /** Distance (px) the cursor must move past mousedown before a drag begins. Default 5. */
30
+ dragThreshold?: number;
31
+ /** Initial head id (the row that Shift+Arrow extends from). */
32
+ initialHeadId?: string | null;
33
+ /** Fires when the head moves via Shift+Arrow so the consumer can scroll the row into view. */
34
+ onHeadChange?: (id: string | null) => void;
35
+ }
36
+
37
+ export interface MultiSelectController<T> {
38
+ selectedIds: ReadonlySet<string>;
39
+ selectedCount: number;
40
+ lastClickedId: string | null;
41
+ headId: string | null;
42
+ /** Viewport-fixed drag rectangle, or null when no drag is active. */
43
+ dragRect: DragRect | null;
44
+
45
+ isSelected: (id: string) => boolean;
46
+ getSelectedItems: () => T[];
47
+
48
+ toggle: (id: string) => void;
49
+ select: (ids: string[]) => void;
50
+ clear: () => void;
51
+ selectAll: () => void;
52
+ selectRange: (anchorId: string, targetId: string) => void;
53
+ setHead: (id: string | null) => void;
54
+
55
+ /** Click handler for a row's hover checkbox. Toggles, with shift-click range from lastClickedId. */
56
+ onCheckboxClick: (id: string) => (event: ReactMouseEvent) => void;
57
+ /** mousedown on a row body — handles Cmd/Ctrl+click toggle without opening detail. */
58
+ onRowMouseDown: (id: string) => (event: ReactMouseEvent) => void;
59
+
60
+ /** Spread on each selectable row so drag-select can find them. */
61
+ getRowProps: (id: string) => { [ROW_ATTR]: string };
62
+ /** Spread on the list container — owns the drag-select mousedown/move/up + click-empty-to-clear. */
63
+ getContainerProps: () => {
64
+ ref: (node: HTMLElement | null) => void;
65
+ onMouseDown: (event: ReactMouseEvent) => void;
66
+ onClickCapture: (event: ReactMouseEvent) => void;
67
+ style: { userSelect: 'none' };
68
+ };
69
+ }
70
+
71
+ function isEditableTarget(target: EventTarget | null): boolean {
72
+ if (!(target instanceof HTMLElement)) return false;
73
+ if (target.isContentEditable) return true;
74
+ const tag = target.tagName;
75
+ return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT';
76
+ }
77
+
78
+ function rectsIntersect(
79
+ a: { left: number; top: number; right: number; bottom: number },
80
+ b: { left: number; top: number; right: number; bottom: number },
81
+ ): boolean {
82
+ return a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top;
83
+ }
84
+
85
+ export function useMultiSelect<T>(opts: UseMultiSelectOptions<T>): MultiSelectController<T> {
86
+ const {
87
+ items,
88
+ getId,
89
+ bindKeyboardShortcuts = true,
90
+ enableDragSelect = true,
91
+ dragThreshold = 5,
92
+ initialHeadId = null,
93
+ onHeadChange,
94
+ } = opts;
95
+
96
+ const [selectedIds, setSelectedIds] = useState<Set<string>>(() => new Set());
97
+ const [lastClickedId, setLastClickedId] = useState<string | null>(null);
98
+ const [headId, setHeadIdState] = useState<string | null>(initialHeadId);
99
+ const [dragRect, setDragRect] = useState<DragRect | null>(null);
100
+
101
+ // Latest-state refs so document listeners and stable callbacks read fresh
102
+ // values without retriggering the effect.
103
+ const itemsRef = useRef(items);
104
+ const getIdRef = useRef(getId);
105
+ const selectedIdsRef = useRef(selectedIds);
106
+ const lastClickedIdRef = useRef(lastClickedId);
107
+ const headIdRef = useRef(headId);
108
+ const onHeadChangeRef = useRef(onHeadChange);
109
+ const containerRef = useRef<HTMLElement | null>(null);
110
+
111
+ itemsRef.current = items;
112
+ getIdRef.current = getId;
113
+ selectedIdsRef.current = selectedIds;
114
+ lastClickedIdRef.current = lastClickedId;
115
+ headIdRef.current = headId;
116
+ onHeadChangeRef.current = onHeadChange;
117
+
118
+ const orderedIds = useMemo(() => items.map((item) => getId(item)), [items, getId]);
119
+ const orderedIdsRef = useRef(orderedIds);
120
+ orderedIdsRef.current = orderedIds;
121
+
122
+ const isSelected = useCallback((id: string) => selectedIds.has(id), [selectedIds]);
123
+
124
+ const getSelectedItems = useCallback(() => {
125
+ const set = selectedIdsRef.current;
126
+ return itemsRef.current.filter((item) => set.has(getIdRef.current(item)));
127
+ }, []);
128
+
129
+ const setHead = useCallback((id: string | null) => {
130
+ setHeadIdState((prev) => {
131
+ if (prev === id) return prev;
132
+ onHeadChangeRef.current?.(id);
133
+ return id;
134
+ });
135
+ }, []);
136
+
137
+ const toggle = useCallback((id: string) => {
138
+ setSelectedIds((prev) => {
139
+ const next = new Set(prev);
140
+ if (next.has(id)) {
141
+ next.delete(id);
142
+ } else {
143
+ next.add(id);
144
+ }
145
+ return next;
146
+ });
147
+ setLastClickedId(id);
148
+ }, []);
149
+
150
+ const select = useCallback((ids: string[]) => {
151
+ setSelectedIds(new Set(ids));
152
+ setLastClickedId(ids.length > 0 ? ids[ids.length - 1] : null);
153
+ }, []);
154
+
155
+ const clear = useCallback(() => {
156
+ setSelectedIds((prev) => (prev.size === 0 ? prev : new Set()));
157
+ }, []);
158
+
159
+ const selectAll = useCallback(() => {
160
+ const all = orderedIdsRef.current;
161
+ setSelectedIds(new Set(all));
162
+ setLastClickedId(all.length > 0 ? all[all.length - 1] : null);
163
+ }, []);
164
+
165
+ const selectRange = useCallback((anchorId: string, targetId: string) => {
166
+ const all = orderedIdsRef.current;
167
+ const anchorIdx = all.indexOf(anchorId);
168
+ const targetIdx = all.indexOf(targetId);
169
+ if (anchorIdx === -1 || targetIdx === -1) {
170
+ setSelectedIds(new Set([targetId]));
171
+ setLastClickedId(targetId);
172
+ return;
173
+ }
174
+ const start = Math.min(anchorIdx, targetIdx);
175
+ const end = Math.max(anchorIdx, targetIdx);
176
+ const range = all.slice(start, end + 1);
177
+ setSelectedIds((prev) => {
178
+ const next = new Set(prev);
179
+ for (const id of range) next.add(id);
180
+ return next;
181
+ });
182
+ setLastClickedId(targetId);
183
+ }, []);
184
+
185
+ const onCheckboxClick = useCallback(
186
+ (id: string) =>
187
+ (event: ReactMouseEvent) => {
188
+ event.stopPropagation();
189
+ if (event.shiftKey && lastClickedIdRef.current && lastClickedIdRef.current !== id) {
190
+ selectRange(lastClickedIdRef.current, id);
191
+ return;
192
+ }
193
+ toggle(id);
194
+ },
195
+ [selectRange, toggle],
196
+ );
197
+
198
+ const onRowMouseDown = useCallback(
199
+ (id: string) =>
200
+ (event: ReactMouseEvent) => {
201
+ if (event.button !== 0) return;
202
+ if (!(event.metaKey || event.ctrlKey)) return;
203
+ const target = event.target as HTMLElement | null;
204
+ if (target && target.closest(INTERACTIVE_SELECTOR)) return;
205
+ event.preventDefault();
206
+ event.stopPropagation();
207
+ toggle(id);
208
+ // The trailing click on the row should NOT open it.
209
+ armSuppressNextClick();
210
+ },
211
+ [toggle],
212
+ );
213
+
214
+ const getRowProps = useCallback(
215
+ (id: string) => ({
216
+ [ROW_ATTR]: id,
217
+ }),
218
+ [],
219
+ );
220
+
221
+ // ---------------------------------------------------------------------------
222
+ // Drag-select
223
+ // ---------------------------------------------------------------------------
224
+
225
+ const dragPendingRef = useRef(false);
226
+ const dragActiveRef = useRef(false);
227
+ const dragStartRef = useRef<{ x: number; y: number }>({ x: 0, y: 0 });
228
+ const dragCurrentRef = useRef<{ x: number; y: number }>({ x: 0, y: 0 });
229
+ // Tracks whether the most recent mousedown originated outside any row, which
230
+ // means a bare click should clear the selection.
231
+ const clickShouldClearRef = useRef(false);
232
+ // Set true after a gesture that should NOT trigger the row's onClick: an
233
+ // active drag-select, or a cmd/ctrl-click toggle. The container's
234
+ // onClickCapture consumes the next click before it reaches the row.
235
+ const suppressNextClickRef = useRef(false);
236
+
237
+ const armSuppressNextClick = () => {
238
+ suppressNextClickRef.current = true;
239
+ setTimeout(() => {
240
+ suppressNextClickRef.current = false;
241
+ }, 0);
242
+ };
243
+
244
+ const computeDragRect = useCallback((): DragRect => {
245
+ const start = dragStartRef.current;
246
+ const current = dragCurrentRef.current;
247
+ return {
248
+ left: Math.min(start.x, current.x),
249
+ top: Math.min(start.y, current.y),
250
+ width: Math.abs(current.x - start.x),
251
+ height: Math.abs(current.y - start.y),
252
+ };
253
+ }, []);
254
+
255
+ const computeIntersectingIds = useCallback(
256
+ (rect: { left: number; top: number; right: number; bottom: number }): string[] => {
257
+ const container = containerRef.current;
258
+ if (!container) return [];
259
+ const nodes = container.querySelectorAll<HTMLElement>(`[${ROW_ATTR}]`);
260
+ const ids: string[] = [];
261
+ nodes.forEach((node) => {
262
+ const rowRect = node.getBoundingClientRect();
263
+ if (rectsIntersect(rect, rowRect)) {
264
+ const id = node.getAttribute(ROW_ATTR);
265
+ if (id) ids.push(id);
266
+ }
267
+ });
268
+ return ids;
269
+ },
270
+ [],
271
+ );
272
+
273
+ const setContainerRef = useCallback(
274
+ (node: HTMLElement | null) => {
275
+ containerRef.current = node;
276
+ },
277
+ [],
278
+ );
279
+
280
+ const handleContainerMouseDown = useCallback(
281
+ (event: ReactMouseEvent) => {
282
+ if (event.button !== 0) return;
283
+ const target = event.target as HTMLElement | null;
284
+ if (!target) return;
285
+ // Cmd/Ctrl-click on a row is handled by `onRowMouseDown` (which calls
286
+ // stopPropagation), so we never reach here for that case. Skip if the
287
+ // mousedown lands on an interactive element so its native focus/text
288
+ // behavior is preserved.
289
+ if (target.closest(INTERACTIVE_SELECTOR)) {
290
+ clickShouldClearRef.current = false;
291
+ return;
292
+ }
293
+ const isOnRow = Boolean(target.closest(`[${ROW_ATTR}]`));
294
+ // A click on empty space (not on a row) should clear the selection.
295
+ clickShouldClearRef.current = !isOnRow;
296
+ if (!enableDragSelect) return;
297
+ // Arm a possible drag from anywhere in the container — including row
298
+ // bodies. If the user moves past the threshold we activate a rubber-band
299
+ // selection AND suppress the trailing click on the row.
300
+ dragPendingRef.current = true;
301
+ dragStartRef.current = { x: event.clientX, y: event.clientY };
302
+ dragCurrentRef.current = { x: event.clientX, y: event.clientY };
303
+ },
304
+ [enableDragSelect],
305
+ );
306
+
307
+ // Capture-phase click handler intercepts the trailing click after a drag so
308
+ // the row's bubble-phase onClick (which would open the row) never fires.
309
+ const handleContainerClickCapture = useCallback(
310
+ (event: ReactMouseEvent) => {
311
+ if (suppressNextClickRef.current) {
312
+ suppressNextClickRef.current = false;
313
+ event.stopPropagation();
314
+ event.preventDefault();
315
+ }
316
+ },
317
+ [],
318
+ );
319
+
320
+ // Attach document mousemove/mouseup while a drag is pending or active.
321
+ useEffect(() => {
322
+ if (!enableDragSelect) return;
323
+
324
+ const handleMouseMove = (event: MouseEvent) => {
325
+ if (dragPendingRef.current) {
326
+ const dx = event.clientX - dragStartRef.current.x;
327
+ const dy = event.clientY - dragStartRef.current.y;
328
+ if (Math.abs(dx) > dragThreshold || Math.abs(dy) > dragThreshold) {
329
+ dragPendingRef.current = false;
330
+ dragActiveRef.current = true;
331
+ window.getSelection()?.removeAllRanges();
332
+ dragCurrentRef.current = { x: event.clientX, y: event.clientY };
333
+ setDragRect(computeDragRect());
334
+ }
335
+ return;
336
+ }
337
+ if (!dragActiveRef.current) return;
338
+ event.preventDefault();
339
+ dragCurrentRef.current = { x: event.clientX, y: event.clientY };
340
+ const rect = computeDragRect();
341
+ setDragRect(rect);
342
+ const ids = computeIntersectingIds({
343
+ left: rect.left,
344
+ top: rect.top,
345
+ right: rect.left + rect.width,
346
+ bottom: rect.top + rect.height,
347
+ });
348
+ setSelectedIds(new Set(ids));
349
+ setLastClickedId(ids.length > 0 ? ids[ids.length - 1] : null);
350
+ };
351
+
352
+ const handleMouseUp = () => {
353
+ const wasPending = dragPendingRef.current;
354
+ const wasActive = dragActiveRef.current;
355
+ dragPendingRef.current = false;
356
+ dragActiveRef.current = false;
357
+ if (wasActive) {
358
+ setDragRect(null);
359
+ clickShouldClearRef.current = false;
360
+ // The trailing click on the row should not open it.
361
+ armSuppressNextClick();
362
+ return;
363
+ }
364
+ if (wasPending && clickShouldClearRef.current) {
365
+ // Bare click on empty space — clear the selection.
366
+ clickShouldClearRef.current = false;
367
+ if (selectedIdsRef.current.size > 0) {
368
+ setSelectedIds(new Set());
369
+ }
370
+ }
371
+ };
372
+
373
+ document.addEventListener('mousemove', handleMouseMove);
374
+ document.addEventListener('mouseup', handleMouseUp);
375
+ return () => {
376
+ document.removeEventListener('mousemove', handleMouseMove);
377
+ document.removeEventListener('mouseup', handleMouseUp);
378
+ };
379
+ }, [computeDragRect, computeIntersectingIds, dragThreshold, enableDragSelect]);
380
+
381
+ // ---------------------------------------------------------------------------
382
+ // Keyboard shortcuts
383
+ // ---------------------------------------------------------------------------
384
+
385
+ useEffect(() => {
386
+ if (!bindKeyboardShortcuts) return;
387
+
388
+ const handleKeyDown = (event: KeyboardEvent) => {
389
+ if (isEditableTarget(event.target)) return;
390
+
391
+ // Esc — clear selection
392
+ if (event.key === 'Escape') {
393
+ if (selectedIdsRef.current.size > 0) {
394
+ event.preventDefault();
395
+ setSelectedIds(new Set());
396
+ }
397
+ return;
398
+ }
399
+
400
+ // Cmd/Ctrl + A — select all
401
+ if ((event.metaKey || event.ctrlKey) && !event.shiftKey && !event.altKey) {
402
+ const key = event.key.toLowerCase();
403
+ if (key === 'a') {
404
+ if (orderedIdsRef.current.length === 0) return;
405
+ event.preventDefault();
406
+ const all = orderedIdsRef.current;
407
+ setSelectedIds(new Set(all));
408
+ setLastClickedId(all[all.length - 1] ?? null);
409
+ return;
410
+ }
411
+ }
412
+
413
+ // X — toggle the focused/head row
414
+ if (
415
+ event.key === 'x' &&
416
+ !event.shiftKey &&
417
+ !event.metaKey &&
418
+ !event.ctrlKey &&
419
+ !event.altKey
420
+ ) {
421
+ const targetId = headIdRef.current ?? lastClickedIdRef.current;
422
+ if (!targetId) return;
423
+ event.preventDefault();
424
+ toggle(targetId);
425
+ return;
426
+ }
427
+
428
+ // Shift + ArrowUp / ArrowDown — extend range from anchor
429
+ if (
430
+ (event.key === 'ArrowDown' || event.key === 'ArrowUp') &&
431
+ event.shiftKey &&
432
+ !event.metaKey &&
433
+ !event.ctrlKey &&
434
+ !event.altKey
435
+ ) {
436
+ const all = orderedIdsRef.current;
437
+ if (all.length === 0) return;
438
+ event.preventDefault();
439
+ const direction = event.key === 'ArrowDown' ? 1 : -1;
440
+ const currentHead = headIdRef.current;
441
+ const currentIdx = currentHead ? all.indexOf(currentHead) : -1;
442
+ const newIdx =
443
+ currentIdx === -1
444
+ ? direction === 1
445
+ ? 0
446
+ : all.length - 1
447
+ : Math.min(all.length - 1, Math.max(0, currentIdx + direction));
448
+ if (newIdx === currentIdx) return;
449
+ const newHead = all[newIdx];
450
+ const anchorId = lastClickedIdRef.current ?? currentHead ?? newHead;
451
+ const anchorIdx = all.indexOf(anchorId);
452
+ if (anchorIdx === -1) {
453
+ setSelectedIds(new Set([newHead]));
454
+ } else {
455
+ const start = Math.min(anchorIdx, newIdx);
456
+ const end = Math.max(anchorIdx, newIdx);
457
+ setSelectedIds(new Set(all.slice(start, end + 1)));
458
+ }
459
+ if (lastClickedIdRef.current === null) {
460
+ setLastClickedId(anchorId);
461
+ }
462
+ setHead(newHead);
463
+ return;
464
+ }
465
+ };
466
+
467
+ document.addEventListener('keydown', handleKeyDown);
468
+ return () => {
469
+ document.removeEventListener('keydown', handleKeyDown);
470
+ };
471
+ }, [bindKeyboardShortcuts, setHead, toggle]);
472
+
473
+ const getContainerProps = useCallback(
474
+ () => ({
475
+ ref: setContainerRef,
476
+ onMouseDown: handleContainerMouseDown,
477
+ onClickCapture: handleContainerClickCapture,
478
+ style: { userSelect: 'none' as const },
479
+ }),
480
+ [handleContainerClickCapture, handleContainerMouseDown, setContainerRef],
481
+ );
482
+
483
+ return {
484
+ selectedIds,
485
+ selectedCount: selectedIds.size,
486
+ lastClickedId,
487
+ headId,
488
+ dragRect,
489
+ isSelected,
490
+ getSelectedItems,
491
+ toggle,
492
+ select,
493
+ clear,
494
+ selectAll,
495
+ selectRange,
496
+ setHead,
497
+ onCheckboxClick,
498
+ onRowMouseDown,
499
+ getRowProps,
500
+ getContainerProps,
501
+ };
502
+ }
@@ -0,0 +1,33 @@
1
+ 'use client';
2
+
3
+ import { useNotisRuntime } from '../provider';
4
+ import type { AppDescriptor, RouteDescriptor, DatabaseDescriptor, CollectionItemDetail } from '../runtime';
5
+
6
+ interface NotisContext {
7
+ /** App metadata (id, name, icon, description). Null before runtime loads. */
8
+ app: AppDescriptor | null;
9
+ /** Current route descriptor. Null before runtime loads. */
10
+ route: RouteDescriptor | null;
11
+ /** Databases declared by this app. Empty before runtime loads. */
12
+ databases: DatabaseDescriptor[];
13
+ /** Selected collection item for the current route, when applicable. */
14
+ collectionItem: CollectionItemDetail | null;
15
+ /** Whether the runtime is loaded and available. */
16
+ ready: boolean;
17
+ }
18
+
19
+ /**
20
+ * Access app-level metadata: the app descriptor, current route, and declared
21
+ * databases. Returns safe defaults when the portal runtime is not available.
22
+ */
23
+ export function useNotis(): NotisContext {
24
+ const runtime = useNotisRuntime();
25
+
26
+ return {
27
+ app: runtime?.app ?? null,
28
+ route: runtime?.route ?? null,
29
+ databases: runtime?.databases ?? [],
30
+ collectionItem: runtime?.context?.collectionItem ?? null,
31
+ ready: runtime !== null,
32
+ };
33
+ }
@@ -0,0 +1,49 @@
1
+ 'use client';
2
+
3
+ import { useCallback } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+
6
+ interface NavigationActions {
7
+ /** Navigate to a route within the app by its path. */
8
+ toRoute: (path: string) => void;
9
+ /** Navigate to a document detail view. */
10
+ toDocument: (documentId: string) => void;
11
+ /** Navigate to the app's default route. */
12
+ toApp: () => void;
13
+ }
14
+
15
+ /**
16
+ * Navigation helpers for moving between routes and documents within the app.
17
+ * When rendered inside the portal, uses the runtime.navigate() bridge.
18
+ * Without a portal runtime, falls back to window.location for route navigation.
19
+ *
20
+ * ```tsx
21
+ * const nav = useNotisNavigation();
22
+ * nav.toRoute('/settings');
23
+ * ```
24
+ */
25
+ export function useNotisNavigation(): NavigationActions {
26
+ const runtime = useNotisRuntime();
27
+
28
+ const toRoute = useCallback((path: string) => {
29
+ if (runtime?.navigate) {
30
+ runtime.navigate({ kind: 'route', path });
31
+ } else if (typeof window !== 'undefined') {
32
+ window.location.href = path;
33
+ }
34
+ }, [runtime]);
35
+
36
+ const toDocument = useCallback((documentId: string) => {
37
+ if (runtime?.navigate) {
38
+ runtime.navigate({ kind: 'document', documentId });
39
+ }
40
+ }, [runtime]);
41
+
42
+ const toApp = useCallback(() => {
43
+ if (runtime?.navigate) {
44
+ runtime.navigate({ kind: 'app' });
45
+ }
46
+ }, [runtime]);
47
+
48
+ return { toRoute, toDocument, toApp };
49
+ }
@@ -0,0 +1,49 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useState } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+
6
+ interface UseToolResult {
7
+ call: (args?: Record<string, unknown>) => Promise<unknown>;
8
+ loading: boolean;
9
+ error: Error | null;
10
+ }
11
+
12
+ /**
13
+ * Call a specific Notis tool by name.
14
+ *
15
+ * ```tsx
16
+ * const { call, loading } = useTool('notis_web_search');
17
+ * const result = await call({ query: 'latest news' });
18
+ * ```
19
+ */
20
+ export function useTool(toolName: string): UseToolResult {
21
+ const runtime = useNotisRuntime();
22
+ const [loading, setLoading] = useState(false);
23
+ const [error, setError] = useState<Error | null>(null);
24
+
25
+ const call = useCallback(
26
+ async (args?: Record<string, unknown>): Promise<unknown> => {
27
+ if (!runtime) {
28
+ throw new Error('Notis runtime not available. Ensure NotisProvider is mounted.');
29
+ }
30
+
31
+ setLoading(true);
32
+ setError(null);
33
+
34
+ try {
35
+ const result = await runtime.callTool(toolName, args);
36
+ return result;
37
+ } catch (err) {
38
+ const e = err instanceof Error ? err : new Error(String(err));
39
+ setError(e);
40
+ throw e;
41
+ } finally {
42
+ setLoading(false);
43
+ }
44
+ },
45
+ [runtime, toolName],
46
+ );
47
+
48
+ return { call, loading, error };
49
+ }
@@ -0,0 +1,56 @@
1
+ 'use client';
2
+
3
+ import { useEffect, useState } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+ import type { ToolDescriptor } from '../runtime';
6
+
7
+ interface UseToolsResult {
8
+ tools: ToolDescriptor[];
9
+ loading: boolean;
10
+ error: Error | null;
11
+ }
12
+
13
+ /**
14
+ * List all tools available to this app.
15
+ *
16
+ * ```tsx
17
+ * const { tools, loading } = useTools();
18
+ * ```
19
+ */
20
+ export function useTools(): UseToolsResult {
21
+ const runtime = useNotisRuntime();
22
+ const [tools, setTools] = useState<ToolDescriptor[]>([]);
23
+ const [loading, setLoading] = useState(true);
24
+ const [error, setError] = useState<Error | null>(null);
25
+
26
+ useEffect(() => {
27
+ if (!runtime) {
28
+ setLoading(false);
29
+ return;
30
+ }
31
+
32
+ let cancelled = false;
33
+ setLoading(true);
34
+
35
+ runtime
36
+ .listTools()
37
+ .then((result) => {
38
+ if (!cancelled) {
39
+ setTools(result);
40
+ setLoading(false);
41
+ }
42
+ })
43
+ .catch((err) => {
44
+ if (!cancelled) {
45
+ setError(err instanceof Error ? err : new Error(String(err)));
46
+ setLoading(false);
47
+ }
48
+ });
49
+
50
+ return () => {
51
+ cancelled = true;
52
+ };
53
+ }, [runtime]);
54
+
55
+ return { tools, loading, error };
56
+ }