@notis_ai/cli 0.2.0-beta.16.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 (52) hide show
  1. package/README.md +335 -0
  2. package/bin/notis.js +2 -0
  3. package/package.json +38 -0
  4. package/src/cli.js +147 -0
  5. package/src/command-specs/apps.js +496 -0
  6. package/src/command-specs/auth.js +178 -0
  7. package/src/command-specs/db.js +163 -0
  8. package/src/command-specs/helpers.js +193 -0
  9. package/src/command-specs/index.js +20 -0
  10. package/src/command-specs/meta.js +154 -0
  11. package/src/command-specs/tools.js +391 -0
  12. package/src/runtime/app-platform.js +624 -0
  13. package/src/runtime/app-preview-server.js +312 -0
  14. package/src/runtime/errors.js +55 -0
  15. package/src/runtime/help.js +60 -0
  16. package/src/runtime/output.js +180 -0
  17. package/src/runtime/profiles.js +202 -0
  18. package/src/runtime/transport.js +198 -0
  19. package/template/app/globals.css +3 -0
  20. package/template/app/layout.tsx +7 -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 +18 -0
  28. package/template/package.json +32 -0
  29. package/template/packages/notis-sdk/package.json +26 -0
  30. package/template/packages/notis-sdk/src/config.ts +48 -0
  31. package/template/packages/notis-sdk/src/helpers.ts +131 -0
  32. package/template/packages/notis-sdk/src/hooks/useAppState.ts +50 -0
  33. package/template/packages/notis-sdk/src/hooks/useBackend.ts +41 -0
  34. package/template/packages/notis-sdk/src/hooks/useCollectionItem.ts +58 -0
  35. package/template/packages/notis-sdk/src/hooks/useDatabase.ts +87 -0
  36. package/template/packages/notis-sdk/src/hooks/useDocument.ts +61 -0
  37. package/template/packages/notis-sdk/src/hooks/useNotis.ts +31 -0
  38. package/template/packages/notis-sdk/src/hooks/useNotisNavigation.ts +49 -0
  39. package/template/packages/notis-sdk/src/hooks/useTool.ts +49 -0
  40. package/template/packages/notis-sdk/src/hooks/useTools.ts +56 -0
  41. package/template/packages/notis-sdk/src/hooks/useUpsertDocument.ts +57 -0
  42. package/template/packages/notis-sdk/src/index.ts +47 -0
  43. package/template/packages/notis-sdk/src/provider.tsx +44 -0
  44. package/template/packages/notis-sdk/src/runtime.ts +159 -0
  45. package/template/packages/notis-sdk/src/styles.css +123 -0
  46. package/template/packages/notis-sdk/src/ui.ts +15 -0
  47. package/template/packages/notis-sdk/src/vite.ts +54 -0
  48. package/template/packages/notis-sdk/tsconfig.json +15 -0
  49. package/template/postcss.config.mjs +8 -0
  50. package/template/tailwind.config.ts +58 -0
  51. package/template/tsconfig.json +22 -0
  52. package/template/vite.config.ts +10 -0
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@notis/sdk",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./src/index.ts",
6
+ "exports": {
7
+ ".": "./src/index.ts",
8
+ "./config": "./src/config.ts",
9
+ "./vite": "./src/vite.ts",
10
+ "./ui": "./src/ui.ts",
11
+ "./styles.css": "./src/styles.css"
12
+ },
13
+ "files": [
14
+ "src",
15
+ "template"
16
+ ],
17
+ "peerDependencies": {
18
+ "react": ">=18.0.0",
19
+ "react-dom": ">=18.0.0",
20
+ "vite": ">=5.0.0",
21
+ "@vitejs/plugin-react": ">=4.0.0"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.0.0"
25
+ }
26
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Configuration utilities for notis.config.ts.
3
+ *
4
+ * Usage:
5
+ * ```ts
6
+ * // notis.config.ts
7
+ * import { defineNotisApp } from '@notis/sdk/config';
8
+ *
9
+ * export default defineNotisApp({
10
+ * name: 'My App',
11
+ * description: 'Does things',
12
+ * icon: 'lucide:layout-dashboard',
13
+ * databases: ['tasks'],
14
+ * routes: [...],
15
+ * tools: [...],
16
+ * });
17
+ * ```
18
+ */
19
+
20
+ export interface NotisRouteConfig {
21
+ path: string;
22
+ name: string;
23
+ icon?: string;
24
+ default?: boolean;
25
+ exportName?: string;
26
+ collection?: {
27
+ database: string;
28
+ titleProperty: string;
29
+ };
30
+ }
31
+
32
+ export interface NotisAppConfig {
33
+ name: string;
34
+ description?: string;
35
+ icon?: string;
36
+ databases?: string[];
37
+ routes?: NotisRouteConfig[];
38
+ tools?: string[];
39
+ }
40
+
41
+ /**
42
+ * Identity function that provides type checking and autocomplete for the
43
+ * Notis app configuration. The returned object is read at build time by
44
+ * `notis apps build` to generate the manifest.
45
+ */
46
+ export function defineNotisApp(config: NotisAppConfig): NotisAppConfig {
47
+ return config;
48
+ }
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Property extraction helpers for Notis DocumentRecord properties.
3
+ *
4
+ * Document properties come back in Notion-style nested shapes. These helpers
5
+ * normalize them into simple JS values regardless of the shape.
6
+ */
7
+
8
+ import type { DocumentRecord } from './runtime';
9
+
10
+ type Obj = Record<string, unknown>;
11
+
12
+ /** Extract a string from a title, rich_text, or plain text property. */
13
+ export function extractText(val: unknown): string {
14
+ if (typeof val === 'string') return val;
15
+ if (val && typeof val === 'object') {
16
+ const obj = val as Obj;
17
+ if (typeof obj.content === 'string') return obj.content;
18
+ if (typeof obj.plain_text === 'string') return obj.plain_text;
19
+ if (Array.isArray(obj.title)) {
20
+ return obj.title.map((t: Obj) => extractText(t?.text || t)).join('');
21
+ }
22
+ if (Array.isArray(obj.rich_text)) {
23
+ return obj.rich_text.map((t: Obj) => extractText(t?.text || t)).join('');
24
+ }
25
+ if (obj.text) return extractText(obj.text);
26
+ }
27
+ return '';
28
+ }
29
+
30
+ /** Extract a string from a select or status property. */
31
+ export function extractSelect(val: unknown): string {
32
+ if (typeof val === 'string') return val;
33
+ if (val && typeof val === 'object') {
34
+ const obj = val as Obj;
35
+ if (obj.select && typeof obj.select === 'object') {
36
+ return (obj.select as Obj).name as string || '';
37
+ }
38
+ if (obj.status && typeof obj.status === 'object') {
39
+ return (obj.status as Obj).name as string || '';
40
+ }
41
+ if (typeof obj.name === 'string') return obj.name;
42
+ }
43
+ return '';
44
+ }
45
+
46
+ /** Extract a date string from a date property. */
47
+ export function extractDate(val: unknown): string {
48
+ if (typeof val === 'string') return val;
49
+ if (val && typeof val === 'object') {
50
+ const obj = val as Obj;
51
+ if (obj.date && typeof obj.date === 'object') {
52
+ return (obj.date as Obj).start as string || '';
53
+ }
54
+ if (typeof obj.start === 'string') return obj.start;
55
+ }
56
+ return '';
57
+ }
58
+
59
+ /** Extract a number from a number property. */
60
+ export function extractNumber(val: unknown): number | null {
61
+ if (typeof val === 'number') return val;
62
+ if (val && typeof val === 'object') {
63
+ const obj = val as Obj;
64
+ if (typeof obj.number === 'number') return obj.number;
65
+ }
66
+ return null;
67
+ }
68
+
69
+ /** Extract a boolean from a checkbox property. */
70
+ export function extractCheckbox(val: unknown): boolean {
71
+ if (typeof val === 'boolean') return val;
72
+ if (val && typeof val === 'object') {
73
+ const obj = val as Obj;
74
+ if (typeof obj.checkbox === 'boolean') return obj.checkbox;
75
+ }
76
+ return false;
77
+ }
78
+
79
+ /** Extract multi-select values as a string array. */
80
+ export function extractMultiSelect(val: unknown): string[] {
81
+ if (Array.isArray(val)) {
82
+ return val
83
+ .map((v) => {
84
+ if (typeof v === 'string') return v;
85
+ if (v && typeof v === 'object' && typeof (v as Obj).name === 'string') return (v as Obj).name as string;
86
+ return '';
87
+ })
88
+ .filter(Boolean);
89
+ }
90
+ if (val && typeof val === 'object') {
91
+ const obj = val as Obj;
92
+ if (Array.isArray(obj.multi_select)) return extractMultiSelect(obj.multi_select);
93
+ }
94
+ if (typeof val === 'string' && val) return [val];
95
+ return [];
96
+ }
97
+
98
+ /** Extract relation IDs from a relation property. */
99
+ export function extractRelation(val: unknown): string[] {
100
+ if (Array.isArray(val)) {
101
+ return val
102
+ .map((v) => {
103
+ if (typeof v === 'string') return v;
104
+ if (v && typeof v === 'object' && typeof (v as Obj).id === 'string') return (v as Obj).id as string;
105
+ return '';
106
+ })
107
+ .filter(Boolean);
108
+ }
109
+ if (val && typeof val === 'object') {
110
+ const obj = val as Obj;
111
+ if (Array.isArray(obj.relation)) return extractRelation(obj.relation);
112
+ }
113
+ if (typeof val === 'string' && val) return [val];
114
+ return [];
115
+ }
116
+
117
+ /**
118
+ * Extract display title from a document. Falls back through several property
119
+ * names and shapes to find a human-readable title.
120
+ */
121
+ export function docTitle(doc: DocumentRecord): string {
122
+ const t = doc.title;
123
+ if (t && !/^[0-9a-f]{8}-[0-9a-f]{4}/.test(t)) return t;
124
+ const p = doc.properties || {};
125
+ const name = p.Name ?? p.name ?? p.Title ?? p.title;
126
+ if (name) {
127
+ const extracted = extractText(name);
128
+ if (extracted) return extracted;
129
+ }
130
+ return t || 'Untitled';
131
+ }
@@ -0,0 +1,50 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useState } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+
6
+ /**
7
+ * Persist app-level state per user per app using localStorage.
8
+ *
9
+ * ```tsx
10
+ * const [order, setOrder] = useAppState<string[]>('task-order', []);
11
+ * ```
12
+ *
13
+ * State is scoped by `app.id` so different apps don't collide.
14
+ * Falls back to defaultValue when localStorage is unavailable.
15
+ */
16
+ export function useAppState<T>(key: string, defaultValue: T): [T, (value: T | ((prev: T) => T)) => void] {
17
+ const runtime = useNotisRuntime();
18
+ const appId = runtime?.app?.id || '_unknown';
19
+ const storageKey = `notis-app-state:${appId}:${key}`;
20
+
21
+ const [value, setValueInternal] = useState<T>(() => {
22
+ if (typeof window === 'undefined') return defaultValue;
23
+ try {
24
+ const stored = localStorage.getItem(storageKey);
25
+ if (stored !== null) return JSON.parse(stored) as T;
26
+ } catch {
27
+ // Ignore parse errors or storage unavailable
28
+ }
29
+ return defaultValue;
30
+ });
31
+
32
+ const setValue = useCallback(
33
+ (newValue: T | ((prev: T) => T)) => {
34
+ setValueInternal((prev) => {
35
+ const resolved = typeof newValue === 'function'
36
+ ? (newValue as (prev: T) => T)(prev)
37
+ : newValue;
38
+ try {
39
+ localStorage.setItem(storageKey, JSON.stringify(resolved));
40
+ } catch {
41
+ // Ignore quota errors
42
+ }
43
+ return resolved;
44
+ });
45
+ },
46
+ [storageKey],
47
+ );
48
+
49
+ return [value, setValue];
50
+ }
@@ -0,0 +1,41 @@
1
+ 'use client';
2
+
3
+ import { useCallback } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+
6
+ interface UseBackendResult {
7
+ /**
8
+ * Make an authenticated request to the Notis backend.
9
+ * The path should be an absolute backend path (e.g. `/portal_composio/actions`).
10
+ */
11
+ request: (path: string, options?: {
12
+ method?: string;
13
+ headers?: Record<string, string>;
14
+ body?: unknown;
15
+ }) => Promise<unknown>;
16
+ }
17
+
18
+ /**
19
+ * Raw backend request proxy. Use this for custom server endpoints or
20
+ * integration APIs not covered by the typed hooks.
21
+ *
22
+ * ```tsx
23
+ * const { request } = useBackend();
24
+ * const data = await request('/portal_composio/actions', { method: 'POST', body: { ... } });
25
+ * ```
26
+ */
27
+ export function useBackend(): UseBackendResult {
28
+ const runtime = useNotisRuntime();
29
+
30
+ const request = useCallback(
31
+ async (path: string, options?: { method?: string; headers?: Record<string, string>; body?: unknown }) => {
32
+ if (!runtime) {
33
+ throw new Error('Notis runtime not available. Ensure NotisProvider is mounted.');
34
+ }
35
+ return runtime.request(path, options);
36
+ },
37
+ [runtime],
38
+ );
39
+
40
+ return { request };
41
+ }
@@ -0,0 +1,58 @@
1
+ 'use client';
2
+
3
+ import { useEffect, useState } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+ import type { CollectionItem } from '../runtime';
6
+
7
+ interface UseCollectionResult {
8
+ /** Items in the collection (from the bound database). */
9
+ items: CollectionItem[];
10
+ loading: boolean;
11
+ error: Error | null;
12
+ }
13
+
14
+ /**
15
+ * List collection items for the current route. Only meaningful on routes
16
+ * that have a `collection` binding in `notis.config.ts`.
17
+ *
18
+ * ```tsx
19
+ * const { items, loading } = useCollectionItems();
20
+ * ```
21
+ */
22
+ export function useCollectionItems(): UseCollectionResult {
23
+ const runtime = useNotisRuntime();
24
+ const [items, setItems] = useState<CollectionItem[]>([]);
25
+ const [loading, setLoading] = useState(true);
26
+ const [error, setError] = useState<Error | null>(null);
27
+
28
+ useEffect(() => {
29
+ if (!runtime) {
30
+ setLoading(false);
31
+ return;
32
+ }
33
+
34
+ let cancelled = false;
35
+ setLoading(true);
36
+
37
+ runtime
38
+ .listCollectionItems()
39
+ .then((result) => {
40
+ if (!cancelled) {
41
+ setItems(result.items);
42
+ setLoading(false);
43
+ }
44
+ })
45
+ .catch((err) => {
46
+ if (!cancelled) {
47
+ setError(err instanceof Error ? err : new Error(String(err)));
48
+ setLoading(false);
49
+ }
50
+ });
51
+
52
+ return () => {
53
+ cancelled = true;
54
+ };
55
+ }, [runtime]);
56
+
57
+ return { items, loading, error };
58
+ }
@@ -0,0 +1,87 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useEffect, useState } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+ import type { DocumentRecord, QueryFilter } from '../runtime';
6
+
7
+ interface UseDatabaseOptions {
8
+ filter?: QueryFilter;
9
+ pageSize?: number;
10
+ offset?: number;
11
+ /** Set to false to skip the initial fetch. */
12
+ enabled?: boolean;
13
+ }
14
+
15
+ interface UseDatabaseResult {
16
+ documents: DocumentRecord[];
17
+ loading: boolean;
18
+ error: Error | null;
19
+ refetch: () => void;
20
+ }
21
+
22
+ /**
23
+ * Query documents from a Notis database owned by this app.
24
+ *
25
+ * ```tsx
26
+ * const { documents, loading } = useDatabase('transactions', {
27
+ * filter: { sorts: [{ property: 'date', direction: 'desc' }] },
28
+ * pageSize: 20,
29
+ * });
30
+ * ```
31
+ */
32
+ export function useDatabase(
33
+ databaseSlug: string,
34
+ options: UseDatabaseOptions = {},
35
+ ): UseDatabaseResult {
36
+ const runtime = useNotisRuntime();
37
+ const [documents, setDocuments] = useState<DocumentRecord[]>([]);
38
+ const [loading, setLoading] = useState(true);
39
+ const [error, setError] = useState<Error | null>(null);
40
+ const [fetchKey, setFetchKey] = useState(0);
41
+
42
+ const enabled = options.enabled !== false;
43
+
44
+ const refetch = useCallback(() => {
45
+ setFetchKey((k) => k + 1);
46
+ }, []);
47
+
48
+ useEffect(() => {
49
+ if (!runtime || !enabled) {
50
+ setLoading(false);
51
+ return;
52
+ }
53
+
54
+ let cancelled = false;
55
+ setLoading(true);
56
+ setError(null);
57
+
58
+ runtime
59
+ .queryDatabase({
60
+ databaseSlug,
61
+ query: {
62
+ ...options.filter,
63
+ page_size: options.pageSize,
64
+ },
65
+ offset: options.offset,
66
+ })
67
+ .then((result) => {
68
+ if (!cancelled) {
69
+ setDocuments(result.documents);
70
+ setLoading(false);
71
+ }
72
+ })
73
+ .catch((err) => {
74
+ if (!cancelled) {
75
+ setError(err instanceof Error ? err : new Error(String(err)));
76
+ setLoading(false);
77
+ }
78
+ });
79
+
80
+ return () => {
81
+ cancelled = true;
82
+ };
83
+ // eslint-disable-next-line react-hooks/exhaustive-deps
84
+ }, [runtime, databaseSlug, fetchKey, enabled]);
85
+
86
+ return { documents, loading, error, refetch };
87
+ }
@@ -0,0 +1,61 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useEffect, useState } from 'react';
4
+ import { useNotisRuntime } from '../provider';
5
+ import type { DocumentRecord } from '../runtime';
6
+
7
+ interface UseDocumentResult {
8
+ document: DocumentRecord | null;
9
+ loading: boolean;
10
+ error: Error | null;
11
+ refetch: () => void;
12
+ }
13
+
14
+ /**
15
+ * Fetch a single document by ID.
16
+ *
17
+ * ```tsx
18
+ * const { document, loading } = useDocument(documentId);
19
+ * ```
20
+ */
21
+ export function useDocument(documentId: string | null | undefined): UseDocumentResult {
22
+ const runtime = useNotisRuntime();
23
+ const [document, setDocument] = useState<DocumentRecord | null>(null);
24
+ const [loading, setLoading] = useState(true);
25
+ const [error, setError] = useState<Error | null>(null);
26
+ const [fetchKey, setFetchKey] = useState(0);
27
+
28
+ const refetch = useCallback(() => setFetchKey((k) => k + 1), []);
29
+
30
+ useEffect(() => {
31
+ if (!runtime || !documentId) {
32
+ setLoading(false);
33
+ return;
34
+ }
35
+
36
+ let cancelled = false;
37
+ setLoading(true);
38
+ setError(null);
39
+
40
+ runtime
41
+ .getDocument({ documentId })
42
+ .then((result) => {
43
+ if (!cancelled) {
44
+ setDocument(result);
45
+ setLoading(false);
46
+ }
47
+ })
48
+ .catch((err) => {
49
+ if (!cancelled) {
50
+ setError(err instanceof Error ? err : new Error(String(err)));
51
+ setLoading(false);
52
+ }
53
+ });
54
+
55
+ return () => {
56
+ cancelled = true;
57
+ };
58
+ }, [runtime, documentId, fetchKey]);
59
+
60
+ return { document, loading, error, refetch };
61
+ }
@@ -0,0 +1,31 @@
1
+ 'use client';
2
+
3
+ import { useNotisRuntime } from '../provider';
4
+ import type { AppDescriptor, RouteDescriptor, DatabaseDescriptor } 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
+ /** Whether the runtime is loaded and available. */
14
+ ready: boolean;
15
+ }
16
+
17
+ /**
18
+ * Access app-level metadata: the app descriptor, current route, and declared
19
+ * databases. Returns safe defaults when the runtime isn't available (SSR or
20
+ * during `next dev` without preview).
21
+ */
22
+ export function useNotis(): NotisContext {
23
+ const runtime = useNotisRuntime();
24
+
25
+ return {
26
+ app: runtime?.app ?? null,
27
+ route: runtime?.route ?? null,
28
+ databases: runtime?.databases ?? [],
29
+ ready: runtime !== null,
30
+ };
31
+ }
@@ -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
+ * In local preview, falls back to window.location.
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
+ }