@oceanum/oceanumlab 4.6.2 → 4.7.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.
- package/lib/DatameshWidget.d.ts +7 -1
- package/lib/DatameshWidget.js +110 -43
- package/lib/aiBackend.d.ts +66 -0
- package/lib/aiBackend.js +101 -0
- package/lib/chatRouter.d.ts +5 -1
- package/lib/chatRouter.js +27 -9
- package/lib/constants.d.ts +1 -1
- package/lib/constants.js +1 -1
- package/lib/datameshUiUrl.js +2 -10
- package/lib/httpUrl.d.ts +9 -0
- package/lib/httpUrl.js +20 -0
- package/lib/index.js +15 -2
- package/package.json +1 -1
- package/schema/datamesh-connect.json +6 -0
- package/src/DatameshWidget.tsx +184 -54
- package/src/__tests__/DatameshWidget.spec.tsx +380 -0
- package/src/__tests__/aiBackend.spec.ts +272 -0
- package/src/__tests__/chatRouter.spec.ts +259 -17
- package/src/aiBackend.ts +150 -0
- package/src/chatRouter.ts +36 -8
- package/src/constants.ts +1 -1
- package/src/datameshUiUrl.ts +2 -9
- package/src/httpUrl.ts +23 -0
- package/src/index.ts +19 -2
package/lib/DatameshWidget.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { JupyterFrontEnd } from '@jupyterlab/application';
|
|
|
2
2
|
import { ReactWidget } from '@jupyterlab/apputils';
|
|
3
3
|
import { LabIcon } from '@jupyterlab/ui-components';
|
|
4
4
|
import { CommandRegistry } from '@lumino/commands';
|
|
5
|
-
import { Signal } from '@lumino/signaling';
|
|
5
|
+
import { ISignal, Signal } from '@lumino/signaling';
|
|
6
6
|
import { Widget } from '@lumino/widgets';
|
|
7
7
|
import React from 'react';
|
|
8
8
|
export interface IDatasource {
|
|
@@ -44,6 +44,12 @@ export interface IDatameshWidgetProps {
|
|
|
44
44
|
datameshUiUrl: () => string;
|
|
45
45
|
/** The Datamesh UI panel's iframe window; `null` when the panel is closed. */
|
|
46
46
|
datameshUiFrame: () => Window | null;
|
|
47
|
+
/** The `datameshToken` setting. */
|
|
48
|
+
datameshToken: () => string;
|
|
49
|
+
/** The `aiBackendUrl` setting. */
|
|
50
|
+
aiBackendUrl: () => string;
|
|
51
|
+
/** Emitted when the settings change. */
|
|
52
|
+
settingsChanged: ISignal<unknown, void>;
|
|
47
53
|
commands: CommandRegistry;
|
|
48
54
|
getCurrentWidget: () => Widget;
|
|
49
55
|
}
|
package/lib/DatameshWidget.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Signal } from '@lumino/signaling';
|
|
|
9
9
|
import React from 'react';
|
|
10
10
|
import { marked } from 'marked';
|
|
11
11
|
import { DatasourceItem } from './DatasourceItem';
|
|
12
|
-
import {
|
|
12
|
+
import { SIGN_IN_COMMAND, aiBackendUrl, aiCredentialSource, canSignIn, pastedToken, resolveAiCredential, signInToken } from './aiBackend';
|
|
13
13
|
import { isDatameshUiMessage } from './datameshUiUrl';
|
|
14
14
|
import { describeProgress, onProgress } from './progress';
|
|
15
15
|
// Configure marked for safe rendering
|
|
@@ -284,30 +284,71 @@ class DatameshWorkspaceDisplay extends React.Component {
|
|
|
284
284
|
});
|
|
285
285
|
}
|
|
286
286
|
}
|
|
287
|
+
function readAiAccess(commands, settings) {
|
|
288
|
+
const datameshToken = settings.datameshToken();
|
|
289
|
+
return {
|
|
290
|
+
source: aiCredentialSource(datameshToken, commands),
|
|
291
|
+
pasted: pastedToken(datameshToken),
|
|
292
|
+
canSignIn: canSignIn(commands),
|
|
293
|
+
backend: aiBackendUrl(settings.aiBackendUrl())
|
|
294
|
+
};
|
|
295
|
+
}
|
|
287
296
|
/**
|
|
288
|
-
*
|
|
289
|
-
*
|
|
297
|
+
* How the sidebar can reach Oceanum AI, read again whenever the settings or the
|
|
298
|
+
* commands change: the host calls `notifyCommandChanged` when the user signs
|
|
299
|
+
* in or out.
|
|
300
|
+
*
|
|
301
|
+
* Only the registry's queries are used, never `execute`, and nothing runs on a
|
|
302
|
+
* timer: every executed command closes JupyterLab's command palette.
|
|
290
303
|
*/
|
|
291
|
-
function
|
|
292
|
-
const [
|
|
304
|
+
function useAiAccess(commands, settings) {
|
|
305
|
+
const [access, setAccess] = React.useState(() => readAiAccess(commands, settings));
|
|
293
306
|
React.useEffect(() => {
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
307
|
+
const update = () => {
|
|
308
|
+
const next = readAiAccess(commands, settings);
|
|
309
|
+
// Same content keeps the same object, so nothing downstream re-runs.
|
|
310
|
+
setAccess(prev => JSON.stringify(prev) === JSON.stringify(next) ? prev : next);
|
|
311
|
+
};
|
|
312
|
+
// Anything that changed between the first render and now.
|
|
313
|
+
update();
|
|
314
|
+
commands.commandChanged.connect(update);
|
|
315
|
+
settings.settingsChanged.connect(update);
|
|
316
|
+
return () => {
|
|
317
|
+
commands.commandChanged.disconnect(update);
|
|
318
|
+
settings.settingsChanged.disconnect(update);
|
|
319
|
+
};
|
|
320
|
+
}, [commands, settings]);
|
|
321
|
+
return access;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Shows a message prompting the user to sign in to Oceanum.io, or to configure
|
|
325
|
+
* their Datamesh token where the site has no sign-in. Only renders when there
|
|
326
|
+
* is no credential.
|
|
327
|
+
*/
|
|
328
|
+
function TokenConfigMessage({ commands, settings }) {
|
|
329
|
+
const access = useAiAccess(commands, settings);
|
|
330
|
+
if (access.source) {
|
|
299
331
|
return null;
|
|
300
332
|
}
|
|
333
|
+
const openSettings = () => commands.execute('settingeditor:open', { query: 'Oceanum' });
|
|
334
|
+
if (access.canSignIn) {
|
|
335
|
+
const signIn = () => commands.execute(SIGN_IN_COMMAND).catch(() => {
|
|
336
|
+
console.warn('Oceanum AI: could not start signing in to Oceanum.io.');
|
|
337
|
+
});
|
|
338
|
+
return (React.createElement("div", { className: "oceanum-token-config" },
|
|
339
|
+
React.createElement("a", { onClick: () => void signIn() }, "Sign in to Oceanum.io"),
|
|
340
|
+
" to use Oceanum AI, or set a ",
|
|
341
|
+
React.createElement("a", { onClick: openSettings }, "Datamesh token"),
|
|
342
|
+
"."));
|
|
343
|
+
}
|
|
301
344
|
return (React.createElement("div", { className: "oceanum-token-config" },
|
|
302
|
-
"Set your",
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
' ',
|
|
306
|
-
"to enable Oceanum.oi services",
|
|
345
|
+
"Set your ",
|
|
346
|
+
React.createElement("a", { onClick: openSettings }, "Datamesh token"),
|
|
347
|
+
" to enable Oceanum.io services",
|
|
307
348
|
' ',
|
|
308
349
|
React.createElement("a", { href: "https://home.oceanum.io/account", target: "_blank", rel: "noopener noreferrer" }, "Get token here")));
|
|
309
350
|
}
|
|
310
|
-
function AIChatPanel({ commands }) {
|
|
351
|
+
function AIChatPanel({ commands, settings }) {
|
|
311
352
|
const [messages, setMessages] = React.useState([]);
|
|
312
353
|
const [input, setInput] = React.useState('');
|
|
313
354
|
const [loading, setLoading] = React.useState(false);
|
|
@@ -322,49 +363,75 @@ function AIChatPanel({ commands }) {
|
|
|
322
363
|
const [history, setHistory] = React.useState([]);
|
|
323
364
|
const [historyIndex, setHistoryIndex] = React.useState(-1);
|
|
324
365
|
const [tempInput, setTempInput] = React.useState('');
|
|
325
|
-
//
|
|
326
|
-
|
|
327
|
-
const
|
|
366
|
+
// Which credential and address the chat has, as the settings and the host
|
|
367
|
+
// report them.
|
|
368
|
+
const access = useAiAccess(commands, settings);
|
|
369
|
+
// What the capabilities depend on: which credential, the pasted token and
|
|
370
|
+
// the address. Not the sign-in token itself: it refreshes, and finding it out
|
|
371
|
+
// runs the host's command.
|
|
372
|
+
const capabilitiesKey = access.source
|
|
373
|
+
? JSON.stringify([access.source, access.pasted, access.backend])
|
|
374
|
+
: '';
|
|
328
375
|
// Check if code capability is enabled (null = loading/unknown)
|
|
329
376
|
const [codeEnabled, setCodeEnabled] = React.useState(null);
|
|
330
|
-
//
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
if (currentToken !== token) {
|
|
335
|
-
setToken(currentToken);
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
const interval = setInterval(checkToken, 1000);
|
|
339
|
-
return () => clearInterval(interval);
|
|
340
|
-
}, [token]);
|
|
341
|
-
// Fetch capabilities when token changes
|
|
377
|
+
// The sidebar's one pending access-token request, shared rather than
|
|
378
|
+
// repeated if the capabilities are asked for again before it answers.
|
|
379
|
+
const tokenRequest = React.useRef(null);
|
|
380
|
+
// Fetch capabilities when the credential or the address changes
|
|
342
381
|
React.useEffect(() => {
|
|
343
|
-
if (!
|
|
382
|
+
if (!access.source) {
|
|
344
383
|
setCodeEnabled(null);
|
|
345
384
|
return;
|
|
346
385
|
}
|
|
386
|
+
// An answer for a credential that has since changed is dropped.
|
|
387
|
+
let current = true;
|
|
388
|
+
const signInTokenOnce = () => {
|
|
389
|
+
if (!tokenRequest.current) {
|
|
390
|
+
tokenRequest.current = signInToken(commands)().finally(() => {
|
|
391
|
+
tokenRequest.current = null;
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
return tokenRequest.current;
|
|
395
|
+
};
|
|
347
396
|
const fetchCapabilities = async () => {
|
|
397
|
+
const credential = await resolveAiCredential(access.pasted, signInTokenOnce);
|
|
398
|
+
if (!current) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (!credential) {
|
|
402
|
+
// The host says signed in but has no token to give: leave it to a
|
|
403
|
+
// request to report, rather than hide the chat.
|
|
404
|
+
setCodeEnabled(null);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
348
407
|
try {
|
|
349
|
-
const response = await fetch(`${
|
|
350
|
-
headers:
|
|
351
|
-
'X-Datamesh-Token': token
|
|
352
|
-
}
|
|
408
|
+
const response = await fetch(`${access.backend}/api/capabilities`, {
|
|
409
|
+
headers: credential.headers
|
|
353
410
|
});
|
|
411
|
+
if (!current) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
354
414
|
if (response.ok) {
|
|
355
415
|
const data = await response.json();
|
|
356
|
-
|
|
416
|
+
if (current) {
|
|
417
|
+
setCodeEnabled(data.code === true);
|
|
418
|
+
}
|
|
357
419
|
}
|
|
358
420
|
else {
|
|
359
421
|
setCodeEnabled(false);
|
|
360
422
|
}
|
|
361
423
|
}
|
|
362
424
|
catch (_a) {
|
|
363
|
-
|
|
425
|
+
if (current) {
|
|
426
|
+
setCodeEnabled(false);
|
|
427
|
+
}
|
|
364
428
|
}
|
|
365
429
|
};
|
|
366
430
|
void fetchCapabilities();
|
|
367
|
-
|
|
431
|
+
return () => {
|
|
432
|
+
current = false;
|
|
433
|
+
};
|
|
434
|
+
}, [capabilitiesKey]);
|
|
368
435
|
// Auto-scroll to bottom when messages change
|
|
369
436
|
React.useEffect(() => {
|
|
370
437
|
var _a;
|
|
@@ -501,8 +568,8 @@ function AIChatPanel({ commands }) {
|
|
|
501
568
|
return;
|
|
502
569
|
}
|
|
503
570
|
};
|
|
504
|
-
// Hide AI panel if no
|
|
505
|
-
if (!
|
|
571
|
+
// Hide AI panel if no credential or code capability is not enabled
|
|
572
|
+
if (!access.source || codeEnabled === false) {
|
|
506
573
|
return null;
|
|
507
574
|
}
|
|
508
575
|
return (React.createElement("div", { className: "oceanum-ai-chat" },
|
|
@@ -571,8 +638,8 @@ export class DatameshConnectWidget extends ReactWidget {
|
|
|
571
638
|
React.createElement("div", { className: "open-datamesh-ui", onClick: this.props.openDatameshUI, title: "Open Datamesh UI" }, React.createElement(addIcon.react, { height: "20px", verticalAlign: "middle" }))),
|
|
572
639
|
React.createElement("div", { className: "datamesh-connect-workspace" },
|
|
573
640
|
React.createElement(UseSignal, { signal: this.renderSignal, initialArgs: null }, (_, datameshWorkspace) => this.renderDisplay(datameshWorkspace)),
|
|
574
|
-
React.createElement(TokenConfigMessage, { commands: this.props.commands })),
|
|
641
|
+
React.createElement(TokenConfigMessage, { commands: this.props.commands, settings: this.props })),
|
|
575
642
|
React.createElement("div", { className: "datamesh-connect-divider" }),
|
|
576
|
-
React.createElement(AIChatPanel, { commands: this.props.commands })));
|
|
643
|
+
React.createElement(AIChatPanel, { commands: this.props.commands, settings: this.props })));
|
|
577
644
|
}
|
|
578
645
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the Oceanum AI backend is, and what a request to it is signed with.
|
|
3
|
+
*
|
|
4
|
+
* The backend accepts either a Datamesh token (`X-Datamesh-Token`) or an
|
|
5
|
+
* Oceanum.io access token (`Authorization: Bearer`). A token pasted into the
|
|
6
|
+
* settings is explicit, so it wins; otherwise a host that signs users in to
|
|
7
|
+
* Oceanum.io -- Oceanum Notebook -- lends its sign-in through its commands.
|
|
8
|
+
* Plain JupyterLab has no such commands and needs the pasted token.
|
|
9
|
+
*
|
|
10
|
+
* Whether the user is signed in, or can sign in, is read with the registry's
|
|
11
|
+
* queries (`isVisible`, `isEnabled`), never by running a command: every
|
|
12
|
+
* `execute` emits `commandExecuted`, which closes JupyterLab's command palette.
|
|
13
|
+
* The access-token command is run only when a token is actually needed.
|
|
14
|
+
*/
|
|
15
|
+
import type { CommandRegistry } from '@lumino/commands';
|
|
16
|
+
/**
|
|
17
|
+
* The host's command answering with the current Oceanum.io access token (a
|
|
18
|
+
* JWT), or null when signed out. The token is cached and refreshed by the
|
|
19
|
+
* host, so it is asked for on every use rather than kept.
|
|
20
|
+
*/
|
|
21
|
+
export declare const ACCESS_TOKEN_COMMAND = "oceanum-auth:access-token";
|
|
22
|
+
/**
|
|
23
|
+
* The host's command that starts signing in to Oceanum.io. Enabled only where
|
|
24
|
+
* sign-in is configured for the site.
|
|
25
|
+
*/
|
|
26
|
+
export declare const SIGN_IN_COMMAND = "oceanum-auth:sign-in";
|
|
27
|
+
/** The host's command that signs out. Visible only while signed in. */
|
|
28
|
+
export declare const SIGN_OUT_COMMAND = "oceanum-auth:sign-out";
|
|
29
|
+
/** The part of the command registry the sign-in is reached through. */
|
|
30
|
+
export type AuthCommands = Pick<CommandRegistry, 'hasCommand' | 'execute' | 'isEnabled' | 'isVisible'>;
|
|
31
|
+
export type AiCredentialSource = 'datamesh-token' | 'sign-in';
|
|
32
|
+
export interface IAiCredential {
|
|
33
|
+
/** Which credential it is: the 401 message differs. */
|
|
34
|
+
source: AiCredentialSource;
|
|
35
|
+
headers: Record<string, string>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The backend address for an `aiBackendUrl` setting value, without a trailing
|
|
39
|
+
* slash so a path can be appended. An invalid value falls back to the default.
|
|
40
|
+
*/
|
|
41
|
+
export declare function aiBackendUrl(value: string | null | undefined): string;
|
|
42
|
+
/** A `datameshToken` setting value, trimmed; '' when there is none. */
|
|
43
|
+
export declare function pastedToken(datameshToken: unknown): string;
|
|
44
|
+
/** Whether the host can sign the user in to Oceanum.io on this site. */
|
|
45
|
+
export declare function canSignIn(commands: AuthCommands): boolean;
|
|
46
|
+
/** Whether the host says the user is signed in to Oceanum.io. */
|
|
47
|
+
export declare function isSignedIn(commands: AuthCommands): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Which credential a request would carry, without fetching it: runs no
|
|
50
|
+
* command, so it is safe to ask whenever something changes.
|
|
51
|
+
*/
|
|
52
|
+
export declare function aiCredentialSource(datameshToken: unknown, commands: AuthCommands): AiCredentialSource | null;
|
|
53
|
+
/**
|
|
54
|
+
* Asks the host for its Oceanum.io access token; null without the command.
|
|
55
|
+
* This runs a command: call it only when a token is needed for a request.
|
|
56
|
+
*/
|
|
57
|
+
export declare function signInToken(commands: AuthCommands): () => Promise<unknown>;
|
|
58
|
+
/**
|
|
59
|
+
* The credential for a request: the pasted Datamesh token if there is one,
|
|
60
|
+
* otherwise the host's sign-in, otherwise null.
|
|
61
|
+
*
|
|
62
|
+
* @param datameshToken The `datameshToken` setting value.
|
|
63
|
+
* @param getSignInToken Asked only without a pasted token. Anything but a
|
|
64
|
+
* non-empty string -- including a rejection -- counts as signed out.
|
|
65
|
+
*/
|
|
66
|
+
export declare function resolveAiCredential(datameshToken: unknown, getSignInToken: () => Promise<unknown>): Promise<IAiCredential | null>;
|
package/lib/aiBackend.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { OCEANUM_AI_BACKEND_URL } from './constants';
|
|
2
|
+
import { validHttpUrl } from './httpUrl';
|
|
3
|
+
/**
|
|
4
|
+
* The host's command answering with the current Oceanum.io access token (a
|
|
5
|
+
* JWT), or null when signed out. The token is cached and refreshed by the
|
|
6
|
+
* host, so it is asked for on every use rather than kept.
|
|
7
|
+
*/
|
|
8
|
+
export const ACCESS_TOKEN_COMMAND = 'oceanum-auth:access-token';
|
|
9
|
+
/**
|
|
10
|
+
* The host's command that starts signing in to Oceanum.io. Enabled only where
|
|
11
|
+
* sign-in is configured for the site.
|
|
12
|
+
*/
|
|
13
|
+
export const SIGN_IN_COMMAND = 'oceanum-auth:sign-in';
|
|
14
|
+
/** The host's command that signs out. Visible only while signed in. */
|
|
15
|
+
export const SIGN_OUT_COMMAND = 'oceanum-auth:sign-out';
|
|
16
|
+
/**
|
|
17
|
+
* The backend address for an `aiBackendUrl` setting value, without a trailing
|
|
18
|
+
* slash so a path can be appended. An invalid value falls back to the default.
|
|
19
|
+
*/
|
|
20
|
+
export function aiBackendUrl(value) {
|
|
21
|
+
const url = validHttpUrl(value, OCEANUM_AI_BACKEND_URL);
|
|
22
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
|
|
23
|
+
}
|
|
24
|
+
/** A `datameshToken` setting value, trimmed; '' when there is none. */
|
|
25
|
+
export function pastedToken(datameshToken) {
|
|
26
|
+
return typeof datameshToken === 'string' ? datameshToken.trim() : '';
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Asks a query of the host's command, treating a missing command, or a query
|
|
30
|
+
* that throws, as false. Queries never emit `commandExecuted`.
|
|
31
|
+
*/
|
|
32
|
+
function query(commands, id, which) {
|
|
33
|
+
try {
|
|
34
|
+
return commands.hasCommand(id) && commands[which](id);
|
|
35
|
+
}
|
|
36
|
+
catch (_a) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Whether the host can sign the user in to Oceanum.io on this site. */
|
|
41
|
+
export function canSignIn(commands) {
|
|
42
|
+
return query(commands, SIGN_IN_COMMAND, 'isEnabled');
|
|
43
|
+
}
|
|
44
|
+
/** Whether the host says the user is signed in to Oceanum.io. */
|
|
45
|
+
export function isSignedIn(commands) {
|
|
46
|
+
return query(commands, SIGN_OUT_COMMAND, 'isVisible');
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Which credential a request would carry, without fetching it: runs no
|
|
50
|
+
* command, so it is safe to ask whenever something changes.
|
|
51
|
+
*/
|
|
52
|
+
export function aiCredentialSource(datameshToken, commands) {
|
|
53
|
+
if (pastedToken(datameshToken)) {
|
|
54
|
+
return 'datamesh-token';
|
|
55
|
+
}
|
|
56
|
+
if (commands.hasCommand(ACCESS_TOKEN_COMMAND) && isSignedIn(commands)) {
|
|
57
|
+
return 'sign-in';
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Asks the host for its Oceanum.io access token; null without the command.
|
|
63
|
+
* This runs a command: call it only when a token is needed for a request.
|
|
64
|
+
*/
|
|
65
|
+
export function signInToken(commands) {
|
|
66
|
+
return async () => commands.hasCommand(ACCESS_TOKEN_COMMAND)
|
|
67
|
+
? commands.execute(ACCESS_TOKEN_COMMAND)
|
|
68
|
+
: null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The credential for a request: the pasted Datamesh token if there is one,
|
|
72
|
+
* otherwise the host's sign-in, otherwise null.
|
|
73
|
+
*
|
|
74
|
+
* @param datameshToken The `datameshToken` setting value.
|
|
75
|
+
* @param getSignInToken Asked only without a pasted token. Anything but a
|
|
76
|
+
* non-empty string -- including a rejection -- counts as signed out.
|
|
77
|
+
*/
|
|
78
|
+
export async function resolveAiCredential(datameshToken, getSignInToken) {
|
|
79
|
+
const pasted = pastedToken(datameshToken);
|
|
80
|
+
if (pasted) {
|
|
81
|
+
return {
|
|
82
|
+
source: 'datamesh-token',
|
|
83
|
+
headers: { 'X-Datamesh-Token': pasted }
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
let token;
|
|
87
|
+
try {
|
|
88
|
+
token = await getSignInToken();
|
|
89
|
+
}
|
|
90
|
+
catch (_a) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const signedIn = typeof token === 'string' ? token.trim() : '';
|
|
94
|
+
if (!signedIn) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
source: 'sign-in',
|
|
99
|
+
headers: { Authorization: `Bearer ${signedIn}` }
|
|
100
|
+
};
|
|
101
|
+
}
|
package/lib/chatRouter.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ISettingRegistry } from '@jupyterlab/settingregistry';
|
|
2
|
+
import { type AuthCommands } from './aiBackend';
|
|
2
3
|
import type { Progress } from './progress';
|
|
3
4
|
import type { ObservedRun } from './notebookRun';
|
|
4
5
|
import type { INotebookSnapshot } from './notebookContext';
|
|
@@ -36,14 +37,17 @@ export declare class ChatRouterError extends Error {
|
|
|
36
37
|
}
|
|
37
38
|
export declare class ChatRouter {
|
|
38
39
|
private _settings;
|
|
40
|
+
private _commands;
|
|
39
41
|
private _notebook;
|
|
40
42
|
/**
|
|
43
|
+
* @param _commands Where the host's Oceanum.io sign-in is asked for, when
|
|
44
|
+
* there is no pasted Datamesh token.
|
|
41
45
|
* @param _notebook What the conversation's notebook holds -- the one its
|
|
42
46
|
* answers are placed in -- or null if there is nothing to send. Asked on
|
|
43
47
|
* every request, so it follows the conversation rather than whichever tab
|
|
44
48
|
* happens to be active.
|
|
45
49
|
*/
|
|
46
|
-
constructor(_settings: ISettingRegistry.ISettings, _notebook: () => Promise<INotebookSnapshot | null>);
|
|
50
|
+
constructor(_settings: ISettingRegistry.ISettings, _commands: AuthCommands, _notebook: () => Promise<INotebookSnapshot | null>);
|
|
47
51
|
route(prompt: string, chatHistory?: ChatMessage[], signal?: AbortSignal, onProgress?: (progress: Progress) => void): Promise<RouteResult>;
|
|
48
52
|
/**
|
|
49
53
|
* The notebook half of the execute loop: the code ran in the user's own
|
package/lib/chatRouter.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { aiBackendUrl, canSignIn, resolveAiCredential, signInToken } from './aiBackend';
|
|
2
2
|
import { readSse } from './sse';
|
|
3
3
|
import { formatNotebookCells } from './notebookContext';
|
|
4
4
|
export class ChatRouterError extends Error {
|
|
@@ -31,13 +31,16 @@ function asOceanumResponse(data) {
|
|
|
31
31
|
}
|
|
32
32
|
export class ChatRouter {
|
|
33
33
|
/**
|
|
34
|
+
* @param _commands Where the host's Oceanum.io sign-in is asked for, when
|
|
35
|
+
* there is no pasted Datamesh token.
|
|
34
36
|
* @param _notebook What the conversation's notebook holds -- the one its
|
|
35
37
|
* answers are placed in -- or null if there is nothing to send. Asked on
|
|
36
38
|
* every request, so it follows the conversation rather than whichever tab
|
|
37
39
|
* happens to be active.
|
|
38
40
|
*/
|
|
39
|
-
constructor(_settings, _notebook) {
|
|
41
|
+
constructor(_settings, _commands, _notebook) {
|
|
40
42
|
this._settings = _settings;
|
|
43
|
+
this._commands = _commands;
|
|
41
44
|
this._notebook = _notebook;
|
|
42
45
|
}
|
|
43
46
|
async route(prompt, chatHistory = [], signal, onProgress) {
|
|
@@ -99,17 +102,22 @@ export class ChatRouter {
|
|
|
99
102
|
}
|
|
100
103
|
async _send(path, payload, signal, onProgress) {
|
|
101
104
|
var _a;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
// Resolved per request, not once: the sign-in token refreshes, and the
|
|
106
|
+
// user can sign in, sign out or paste a token between two requests.
|
|
107
|
+
const credential = await resolveAiCredential(this._settings.get('datameshToken').composite, signInToken(this._commands));
|
|
108
|
+
if (!credential) {
|
|
109
|
+
throw new ChatRouterError(canSignIn(this._commands)
|
|
110
|
+
? 'Not signed in. Sign in to Oceanum.io to use Oceanum AI, or set a Datamesh token in Settings → Oceanum.io.'
|
|
111
|
+
: 'Datamesh token not configured. Set your token in Settings → Oceanum.io.');
|
|
105
112
|
}
|
|
113
|
+
const backend = aiBackendUrl(this._settings.get('aiBackendUrl').composite);
|
|
106
114
|
let response;
|
|
107
115
|
try {
|
|
108
|
-
response = await fetch(`${
|
|
116
|
+
response = await fetch(`${backend}${path}`, {
|
|
109
117
|
method: 'POST',
|
|
110
118
|
headers: {
|
|
111
119
|
'Content-Type': 'application/json',
|
|
112
|
-
|
|
120
|
+
...credential.headers,
|
|
113
121
|
// Ask for the streamed form only when someone is listening. The
|
|
114
122
|
// server requires the media type to be NAMED -- a wildcard never
|
|
115
123
|
// selects it -- so omitting this is what keeps the plain JSON path
|
|
@@ -126,10 +134,20 @@ export class ChatRouter {
|
|
|
126
134
|
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
127
135
|
throw err;
|
|
128
136
|
}
|
|
129
|
-
throw new ChatRouterError(`Could not reach Oceanum AI backend at ${
|
|
137
|
+
throw new ChatRouterError(`Could not reach Oceanum AI backend at ${backend}. Is it running?`);
|
|
130
138
|
}
|
|
131
139
|
if (response.status === 401) {
|
|
132
|
-
|
|
140
|
+
let message = 'Invalid or expired Datamesh token.';
|
|
141
|
+
if (credential.source === 'sign-in') {
|
|
142
|
+
message =
|
|
143
|
+
'Your Oceanum.io sign-in was not accepted. Sign in to Oceanum.io again.';
|
|
144
|
+
}
|
|
145
|
+
else if (canSignIn(this._commands)) {
|
|
146
|
+
// A pasted token wins over the sign-in, so say how to fall back to it.
|
|
147
|
+
message +=
|
|
148
|
+
' Update it, or clear it to use your Oceanum.io sign-in, in Settings → Oceanum.io.';
|
|
149
|
+
}
|
|
150
|
+
throw new ChatRouterError(message, 401);
|
|
133
151
|
}
|
|
134
152
|
if (!response.ok) {
|
|
135
153
|
let detail = '';
|
package/lib/constants.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Build-time constants for Oceanum AI integration.
|
|
3
3
|
* These values are compiled into the extension.
|
|
4
4
|
*/
|
|
5
|
-
/** URL of the Oceanum AI backend service */
|
|
5
|
+
/** Default URL of the Oceanum AI backend service (the `aiBackendUrl` setting) */
|
|
6
6
|
export declare const OCEANUM_AI_BACKEND_URL = "https://ai.oceanum.io";
|
|
7
7
|
/** URL of the Datamesh UI embedded in the Datamesh panel */
|
|
8
8
|
export declare const DATAMESH_UI_SERVICE = "https://ui.datamesh.oceanum.io";
|
package/lib/constants.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Build-time constants for Oceanum AI integration.
|
|
3
3
|
* These values are compiled into the extension.
|
|
4
4
|
*/
|
|
5
|
-
/** URL of the Oceanum AI backend service */
|
|
5
|
+
/** Default URL of the Oceanum AI backend service (the `aiBackendUrl` setting) */
|
|
6
6
|
export const OCEANUM_AI_BACKEND_URL = 'https://ai.oceanum.io';
|
|
7
7
|
/** URL of the Datamesh UI embedded in the Datamesh panel */
|
|
8
8
|
export const DATAMESH_UI_SERVICE = 'https://ui.datamesh.oceanum.io';
|
package/lib/datameshUiUrl.js
CHANGED
|
@@ -7,18 +7,10 @@
|
|
|
7
7
|
* rather than ending up in an iframe's `src`.
|
|
8
8
|
*/
|
|
9
9
|
import { DATAMESH_UI_SERVICE } from './constants';
|
|
10
|
+
import { validHttpUrl } from './httpUrl';
|
|
10
11
|
/** The Datamesh UI address to use for a `datameshUiUrl` setting value. */
|
|
11
12
|
export function validDatameshUiUrl(url) {
|
|
12
|
-
|
|
13
|
-
const parsed = new URL((url !== null && url !== void 0 ? url : '').trim());
|
|
14
|
-
if (parsed.protocol === 'https:' || parsed.protocol === 'http:') {
|
|
15
|
-
return parsed;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
catch (_a) {
|
|
19
|
-
// Empty or not a URL: the default below.
|
|
20
|
-
}
|
|
21
|
-
return new URL(DATAMESH_UI_SERVICE);
|
|
13
|
+
return validHttpUrl(url, DATAMESH_UI_SERVICE);
|
|
22
14
|
}
|
|
23
15
|
/**
|
|
24
16
|
* The panel's iframe `src`: the Datamesh UI in embed mode, which hides the
|
package/lib/httpUrl.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An address typed into a setting, checked before anything uses it.
|
|
3
|
+
*
|
|
4
|
+
* A setting is typed in by hand, so anything that is not an http(s) URL falls
|
|
5
|
+
* back to the default rather than ending up in an iframe's `src` or a
|
|
6
|
+
* request's address.
|
|
7
|
+
*/
|
|
8
|
+
/** `value` as an http(s) URL, or `fallback` when it is empty or not one. */
|
|
9
|
+
export declare function validHttpUrl(value: string | null | undefined, fallback: string): URL;
|
package/lib/httpUrl.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An address typed into a setting, checked before anything uses it.
|
|
3
|
+
*
|
|
4
|
+
* A setting is typed in by hand, so anything that is not an http(s) URL falls
|
|
5
|
+
* back to the default rather than ending up in an iframe's `src` or a
|
|
6
|
+
* request's address.
|
|
7
|
+
*/
|
|
8
|
+
/** `value` as an http(s) URL, or `fallback` when it is empty or not one. */
|
|
9
|
+
export function validHttpUrl(value, fallback) {
|
|
10
|
+
try {
|
|
11
|
+
const parsed = new URL((value !== null && value !== void 0 ? value : '').trim());
|
|
12
|
+
if (parsed.protocol === 'https:' || parsed.protocol === 'http:') {
|
|
13
|
+
return parsed;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
catch (_a) {
|
|
17
|
+
// Empty or not a URL: the default below.
|
|
18
|
+
}
|
|
19
|
+
return new URL(fallback);
|
|
20
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { ILayoutRestorer, ILabStatus } from '@jupyterlab/application';
|
|
|
2
2
|
import { ICommandPalette, Notification } from '@jupyterlab/apputils';
|
|
3
3
|
import { LabIcon } from '@jupyterlab/ui-components';
|
|
4
4
|
import { find } from '@lumino/algorithm';
|
|
5
|
+
import { Signal } from '@lumino/signaling';
|
|
5
6
|
import { ISettingRegistry } from '@jupyterlab/settingregistry';
|
|
6
7
|
import { IStateDB } from '@jupyterlab/statedb';
|
|
7
8
|
import { INotebookTracker } from '@jupyterlab/notebook';
|
|
@@ -15,7 +16,7 @@ import { snapshotFromIpynb, snapshotOf } from './notebookContext';
|
|
|
15
16
|
import { notebookHost } from './notebookHost';
|
|
16
17
|
import { KernelHandoff } from './kernelHandoff';
|
|
17
18
|
import { runChatLoop, STOPPED } from './aiLoop';
|
|
18
|
-
import { DATAMESH_UI_SERVICE, MAX_OBSERVE_ROUNDS } from './constants';
|
|
19
|
+
import { DATAMESH_UI_SERVICE, MAX_OBSERVE_ROUNDS, OCEANUM_AI_BACKEND_URL } from './constants';
|
|
19
20
|
import '../style/index.css';
|
|
20
21
|
import oceanumSvg from '../style/icons/oceanum.svg';
|
|
21
22
|
// Theme-adaptive icon using currentColor (auto-adapts to light/dark themes)
|
|
@@ -41,6 +42,12 @@ export const datamesh_connect_extension = {
|
|
|
41
42
|
console.log('Oceanum datamesh connect extension is loaded');
|
|
42
43
|
// The `datameshUiUrl` setting; the default until the settings load.
|
|
43
44
|
let datameshUiUrl = DATAMESH_UI_SERVICE;
|
|
45
|
+
// The `aiBackendUrl` and `datameshToken` settings for the AI chat; the
|
|
46
|
+
// defaults until the settings load.
|
|
47
|
+
let aiBackendUrl = OCEANUM_AI_BACKEND_URL;
|
|
48
|
+
let datameshTokenSetting = '';
|
|
49
|
+
// Tells the sidebar the settings above have changed.
|
|
50
|
+
const settingsChanged = new Signal(app);
|
|
44
51
|
const findDatameshUI = () => find(app.shell.widgets('main'), (widget) => widget.id === 'datamesh-ui');
|
|
45
52
|
//Try to get the datamesh token from the settings
|
|
46
53
|
const updateSettings = (set) => {
|
|
@@ -53,12 +60,15 @@ export const datamesh_connect_extension = {
|
|
|
53
60
|
}).then(res => console.log(res));
|
|
54
61
|
}
|
|
55
62
|
window.injectToken = set.get('injectToken').user;
|
|
63
|
+
aiBackendUrl = set.get('aiBackendUrl').composite;
|
|
64
|
+
datameshTokenSetting = set.get('datameshToken').composite;
|
|
56
65
|
// An open Datamesh UI panel follows a change of address.
|
|
57
66
|
datameshUiUrl = set.get('datameshUiUrl').composite;
|
|
58
67
|
const datameshUI = findDatameshUI();
|
|
59
68
|
if (datameshUI) {
|
|
60
69
|
datameshUI.url = datameshUiUrl;
|
|
61
70
|
}
|
|
71
|
+
settingsChanged.emit();
|
|
62
72
|
};
|
|
63
73
|
//Try to get the datamesh token from the envars
|
|
64
74
|
Promise.all([app.restored, settingRegistry.load(PLUGIN_ID)])
|
|
@@ -95,6 +105,9 @@ export const datamesh_connect_extension = {
|
|
|
95
105
|
openDatameshUI: openDatameshUI,
|
|
96
106
|
datameshUiUrl: () => datameshUiUrl,
|
|
97
107
|
datameshUiFrame: () => { var _a, _b; return (_b = (_a = findDatameshUI()) === null || _a === void 0 ? void 0 : _a.frame) !== null && _b !== void 0 ? _b : null; },
|
|
108
|
+
datameshToken: () => datameshTokenSetting,
|
|
109
|
+
aiBackendUrl: () => aiBackendUrl,
|
|
110
|
+
settingsChanged,
|
|
98
111
|
commands: app.commands,
|
|
99
112
|
getCurrentWidget
|
|
100
113
|
});
|
|
@@ -137,7 +150,7 @@ export const oceanum_ai_extension = {
|
|
|
137
150
|
// The panel the current request read its selected cell from, if the
|
|
138
151
|
// notebook was open: a code answer may replace that cell only there.
|
|
139
152
|
let readFrom = null;
|
|
140
|
-
const router = new ChatRouter(settings, async () => {
|
|
153
|
+
const router = new ChatRouter(settings, app.commands, async () => {
|
|
141
154
|
const open = await pin.current();
|
|
142
155
|
readFrom = open;
|
|
143
156
|
if (open) {
|