@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
|
@@ -1,9 +1,33 @@
|
|
|
1
|
+
import type { AuthCommands } from '../aiBackend';
|
|
1
2
|
import { ChatRouter } from '../chatRouter';
|
|
2
3
|
import type { INotebookSnapshot } from '../notebookContext';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/** Settings holding `values`; a key not given has no value. */
|
|
6
|
+
const settingsWith = (values: Record<string, unknown>) =>
|
|
7
|
+
({
|
|
8
|
+
get: (key: string) => ({ composite: values[key] })
|
|
9
|
+
}) as unknown as ConstructorParameters<typeof ChatRouter>[0];
|
|
10
|
+
|
|
11
|
+
const settings = settingsWith({ datameshToken: 'a-token' });
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A host with these commands, each answering with what its function returns.
|
|
15
|
+
* `enabled` and `visible` are what the registry's queries answer, which is how
|
|
16
|
+
* the sign-in state is read without running anything.
|
|
17
|
+
*/
|
|
18
|
+
const hostCommands = (
|
|
19
|
+
commands: Record<string, () => unknown>,
|
|
20
|
+
{ enabled = [] as string[], visible = [] as string[] } = {}
|
|
21
|
+
) =>
|
|
22
|
+
({
|
|
23
|
+
hasCommand: jest.fn((id: string) => id in commands),
|
|
24
|
+
execute: jest.fn(async (id: string) => commands[id]()),
|
|
25
|
+
isEnabled: jest.fn((id: string) => enabled.includes(id)),
|
|
26
|
+
isVisible: jest.fn((id: string) => visible.includes(id))
|
|
27
|
+
}) as AuthCommands & { execute: jest.Mock };
|
|
28
|
+
|
|
29
|
+
/** Plain JupyterLab: no Oceanum.io sign-in to lend. */
|
|
30
|
+
const noSignIn = hostCommands({});
|
|
7
31
|
|
|
8
32
|
const snapshot = (
|
|
9
33
|
cells: INotebookSnapshot['cells'],
|
|
@@ -12,6 +36,7 @@ const snapshot = (
|
|
|
12
36
|
|
|
13
37
|
let sent: Record<string, unknown> | undefined;
|
|
14
38
|
let sentHeaders: Record<string, string> | undefined;
|
|
39
|
+
let sentUrl: string | undefined;
|
|
15
40
|
|
|
16
41
|
interface IRequestInit {
|
|
17
42
|
body: string;
|
|
@@ -22,9 +47,10 @@ interface IRequestInit {
|
|
|
22
47
|
/** Answer every request with what `response` builds from it. */
|
|
23
48
|
function answering(response: (init: IRequestInit) => unknown): void {
|
|
24
49
|
(globalThis as any).fetch = jest.fn(
|
|
25
|
-
async (
|
|
50
|
+
async (url: string, init: IRequestInit) => {
|
|
26
51
|
sent = JSON.parse(init.body);
|
|
27
52
|
sentHeaders = init.headers;
|
|
53
|
+
sentUrl = url;
|
|
28
54
|
return response(init);
|
|
29
55
|
}
|
|
30
56
|
);
|
|
@@ -33,6 +59,7 @@ function answering(response: (init: IRequestInit) => unknown): void {
|
|
|
33
59
|
beforeEach(() => {
|
|
34
60
|
sent = undefined;
|
|
35
61
|
sentHeaders = undefined;
|
|
62
|
+
sentUrl = undefined;
|
|
36
63
|
answering(() => ({
|
|
37
64
|
ok: true,
|
|
38
65
|
status: 200,
|
|
@@ -82,7 +109,7 @@ function streamed(frames: string[], signal?: AbortSignal, hang = false) {
|
|
|
82
109
|
|
|
83
110
|
describe('ChatRouter: the notebook sent with a request', () => {
|
|
84
111
|
it('is the conversation notebook, code and markdown', async () => {
|
|
85
|
-
const router = new ChatRouter(settings, async () =>
|
|
112
|
+
const router = new ChatRouter(settings, noSignIn, async () =>
|
|
86
113
|
snapshot([
|
|
87
114
|
{ kind: 'code', source: 'x = 1' },
|
|
88
115
|
{ kind: 'markdown', source: '## Notes' },
|
|
@@ -100,7 +127,7 @@ describe('ChatRouter: the notebook sent with a request', () => {
|
|
|
100
127
|
});
|
|
101
128
|
|
|
102
129
|
it('carries the selected cell of that notebook, which a code answer may replace', async () => {
|
|
103
|
-
const router = new ChatRouter(settings, async () =>
|
|
130
|
+
const router = new ChatRouter(settings, noSignIn, async () =>
|
|
104
131
|
snapshot([{ kind: 'code', source: 'df.head()' }], {
|
|
105
132
|
source: 'df.head()',
|
|
106
133
|
isCode: true
|
|
@@ -115,7 +142,7 @@ describe('ChatRouter: the notebook sent with a request', () => {
|
|
|
115
142
|
|
|
116
143
|
it('asks for the notebook on every request, so it follows the conversation', async () => {
|
|
117
144
|
let current = snapshot([{ kind: 'code', source: 'a = 1' }]);
|
|
118
|
-
const router = new ChatRouter(settings, async () => current);
|
|
145
|
+
const router = new ChatRouter(settings, noSignIn, async () => current);
|
|
119
146
|
|
|
120
147
|
await router.route('one');
|
|
121
148
|
expect(sent?.notebookCells).toEqual(['a = 1']);
|
|
@@ -126,7 +153,7 @@ describe('ChatRouter: the notebook sent with a request', () => {
|
|
|
126
153
|
});
|
|
127
154
|
|
|
128
155
|
it('sends no notebook when there is none', async () => {
|
|
129
|
-
const router = new ChatRouter(settings, async () => null);
|
|
156
|
+
const router = new ChatRouter(settings, noSignIn, async () => null);
|
|
130
157
|
|
|
131
158
|
await router.route('hello');
|
|
132
159
|
|
|
@@ -138,7 +165,7 @@ describe('ChatRouter: the notebook sent with a request', () => {
|
|
|
138
165
|
it('still sends the question when the notebook cannot be read', async () => {
|
|
139
166
|
// Context is best-effort; a notebook that could not be read must not
|
|
140
167
|
// cost the user the answer.
|
|
141
|
-
const router = new ChatRouter(settings, async () => {
|
|
168
|
+
const router = new ChatRouter(settings, noSignIn, async () => {
|
|
142
169
|
throw new Error('could not read');
|
|
143
170
|
});
|
|
144
171
|
|
|
@@ -154,7 +181,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
154
181
|
|
|
155
182
|
it('asks for the stream only when someone is listening', async () => {
|
|
156
183
|
// Without a listener the request is byte-identical to what it always was.
|
|
157
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
184
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
158
185
|
|
|
159
186
|
await router.route('hello');
|
|
160
187
|
expect(sentHeaders?.Accept).toBeUndefined();
|
|
@@ -179,7 +206,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
179
206
|
])
|
|
180
207
|
);
|
|
181
208
|
const progress: unknown[] = [];
|
|
182
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
209
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
183
210
|
|
|
184
211
|
const result = await router.route('find waves', [], undefined, p =>
|
|
185
212
|
progress.push(p)
|
|
@@ -197,7 +224,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
197
224
|
|
|
198
225
|
it('reads JSON when the server answers with JSON anyway', async () => {
|
|
199
226
|
// An older backend, or a proxy that buffered the stream away.
|
|
200
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
227
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
201
228
|
|
|
202
229
|
const result = await router.route('hello', [], undefined, () => undefined);
|
|
203
230
|
|
|
@@ -216,7 +243,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
216
243
|
})
|
|
217
244
|
])
|
|
218
245
|
);
|
|
219
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
246
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
220
247
|
|
|
221
248
|
await expect(
|
|
222
249
|
router.route('hi', [], undefined, () => undefined)
|
|
@@ -230,7 +257,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
230
257
|
it('fails a stream that ends without its answer', async () => {
|
|
231
258
|
// A dropped connection: a failure, not a shorter answer.
|
|
232
259
|
answering(() => streamed([frame('status', { phase: 'generating' })]));
|
|
233
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
260
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
234
261
|
|
|
235
262
|
await expect(
|
|
236
263
|
router.route('hi', [], undefined, () => undefined)
|
|
@@ -261,7 +288,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
261
288
|
})
|
|
262
289
|
}
|
|
263
290
|
}));
|
|
264
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
291
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
265
292
|
|
|
266
293
|
await expect(
|
|
267
294
|
router.route('hi', [], undefined, () => undefined)
|
|
@@ -280,7 +307,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
280
307
|
])
|
|
281
308
|
);
|
|
282
309
|
const progress: unknown[] = [];
|
|
283
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
310
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
284
311
|
|
|
285
312
|
const response = await router.observe(
|
|
286
313
|
'hi',
|
|
@@ -301,7 +328,7 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
301
328
|
streamed([frame('status', { phase: 'generating' })], init.signal, true)
|
|
302
329
|
);
|
|
303
330
|
const progress: unknown[] = [];
|
|
304
|
-
const router = new ChatRouter(settings, noNotebook);
|
|
331
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
305
332
|
|
|
306
333
|
const pending = router.route('hi', [], controller.signal, p =>
|
|
307
334
|
progress.push(p)
|
|
@@ -313,3 +340,218 @@ describe('ChatRouter: the streamed answer', () => {
|
|
|
313
340
|
expect(progress).toEqual([{ phase: 'generating' }]);
|
|
314
341
|
});
|
|
315
342
|
});
|
|
343
|
+
|
|
344
|
+
describe('ChatRouter: where a request goes, and what it is signed with', () => {
|
|
345
|
+
const noNotebook = async (): Promise<null> => null;
|
|
346
|
+
/** Oceanum Notebook with sign-in configured, and someone signed in. */
|
|
347
|
+
const signedIn = (token: () => unknown) =>
|
|
348
|
+
hostCommands(
|
|
349
|
+
{
|
|
350
|
+
'oceanum-auth:access-token': token,
|
|
351
|
+
'oceanum-auth:sign-in': () => undefined,
|
|
352
|
+
'oceanum-auth:sign-out': () => undefined
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
enabled: ['oceanum-auth:sign-in'],
|
|
356
|
+
visible: ['oceanum-auth:sign-out']
|
|
357
|
+
}
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
it('sends a pasted token as X-Datamesh-Token to the default backend', async () => {
|
|
361
|
+
const router = new ChatRouter(settings, noSignIn, noNotebook);
|
|
362
|
+
|
|
363
|
+
await router.route('hello');
|
|
364
|
+
|
|
365
|
+
expect(sentUrl).toBe('https://ai.oceanum.io/api/chat');
|
|
366
|
+
expect(sentHeaders?.['X-Datamesh-Token']).toBe('a-token');
|
|
367
|
+
expect(sentHeaders?.Authorization).toBeUndefined();
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it('prefers a pasted token over the sign-in, without asking for it', async () => {
|
|
371
|
+
// An explicit token is the user's choice, as it is in the kernel.
|
|
372
|
+
const host = signedIn(() => 'a-jwt');
|
|
373
|
+
const router = new ChatRouter(settings, host, noNotebook);
|
|
374
|
+
|
|
375
|
+
await router.route('hello');
|
|
376
|
+
|
|
377
|
+
expect(sentHeaders?.['X-Datamesh-Token']).toBe('a-token');
|
|
378
|
+
expect(sentHeaders?.Authorization).toBeUndefined();
|
|
379
|
+
expect(host.execute).not.toHaveBeenCalled();
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it.each([
|
|
383
|
+
['no', undefined],
|
|
384
|
+
['an empty', ''],
|
|
385
|
+
['a blank', ' ']
|
|
386
|
+
])(
|
|
387
|
+
'uses the sign-in as a bearer token with %s pasted token',
|
|
388
|
+
async (_, token) => {
|
|
389
|
+
const router = new ChatRouter(
|
|
390
|
+
settingsWith({ datameshToken: token }),
|
|
391
|
+
signedIn(() => 'a-jwt'),
|
|
392
|
+
noNotebook
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
await router.route('hello');
|
|
396
|
+
|
|
397
|
+
expect(sentHeaders?.Authorization).toBe('Bearer a-jwt');
|
|
398
|
+
expect(sentHeaders?.['X-Datamesh-Token']).toBeUndefined();
|
|
399
|
+
}
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
it('asks for the sign-in on every request, so a refreshed token is used', async () => {
|
|
403
|
+
let token = 'first-jwt';
|
|
404
|
+
const router = new ChatRouter(
|
|
405
|
+
settingsWith({}),
|
|
406
|
+
signedIn(() => token),
|
|
407
|
+
noNotebook
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
await router.route('one');
|
|
411
|
+
expect(sentHeaders?.Authorization).toBe('Bearer first-jwt');
|
|
412
|
+
|
|
413
|
+
token = 'second-jwt';
|
|
414
|
+
await router.observe('two', [], []);
|
|
415
|
+
expect(sentHeaders?.Authorization).toBe('Bearer second-jwt');
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('sends to the configured address, without a double slash', async () => {
|
|
419
|
+
const router = new ChatRouter(
|
|
420
|
+
settingsWith({
|
|
421
|
+
datameshToken: 'a-token',
|
|
422
|
+
aiBackendUrl: ' https://ai.oceanum.tech/ '
|
|
423
|
+
}),
|
|
424
|
+
noSignIn,
|
|
425
|
+
noNotebook
|
|
426
|
+
);
|
|
427
|
+
|
|
428
|
+
await router.route('hello');
|
|
429
|
+
expect(sentUrl).toBe('https://ai.oceanum.tech/api/chat');
|
|
430
|
+
|
|
431
|
+
await router.observe('hello', [], []);
|
|
432
|
+
expect(sentUrl).toBe('https://ai.oceanum.tech/api/chat/observe');
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('sends to the default for an invalid address', async () => {
|
|
436
|
+
const router = new ChatRouter(
|
|
437
|
+
settingsWith({
|
|
438
|
+
datameshToken: 'a-token',
|
|
439
|
+
aiBackendUrl: 'javascript:alert(1)'
|
|
440
|
+
}),
|
|
441
|
+
noSignIn,
|
|
442
|
+
noNotebook
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
await router.route('hello');
|
|
446
|
+
|
|
447
|
+
expect(sentUrl).toBe('https://ai.oceanum.io/api/chat');
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it('asks the user to sign in when the host can sign in and nobody has', async () => {
|
|
451
|
+
const router = new ChatRouter(
|
|
452
|
+
settingsWith({}),
|
|
453
|
+
signedIn(() => null),
|
|
454
|
+
noNotebook
|
|
455
|
+
);
|
|
456
|
+
|
|
457
|
+
await expect(router.route('hello')).rejects.toMatchObject({
|
|
458
|
+
name: 'ChatRouterError',
|
|
459
|
+
message: expect.stringContaining('Sign in to Oceanum.io')
|
|
460
|
+
});
|
|
461
|
+
expect((globalThis as any).fetch).not.toHaveBeenCalled();
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('asks for a Datamesh token when the host has no sign-in', async () => {
|
|
465
|
+
const router = new ChatRouter(settingsWith({}), noSignIn, noNotebook);
|
|
466
|
+
|
|
467
|
+
await expect(router.route('hello')).rejects.toThrow(
|
|
468
|
+
'Datamesh token not configured. Set your token in Settings → Oceanum.io.'
|
|
469
|
+
);
|
|
470
|
+
expect((globalThis as any).fetch).not.toHaveBeenCalled();
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
it('asks for a Datamesh token where the site has sign-in disabled', async () => {
|
|
474
|
+
// The command is there, but the site has no Oceanum.io environment, so
|
|
475
|
+
// telling the user to sign in would be telling them to do the impossible.
|
|
476
|
+
const router = new ChatRouter(
|
|
477
|
+
settingsWith({}),
|
|
478
|
+
hostCommands({
|
|
479
|
+
'oceanum-auth:access-token': () => null,
|
|
480
|
+
'oceanum-auth:sign-in': () => undefined
|
|
481
|
+
}),
|
|
482
|
+
noNotebook
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
await expect(router.route('hello')).rejects.toThrow(
|
|
486
|
+
'Datamesh token not configured. Set your token in Settings → Oceanum.io.'
|
|
487
|
+
);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
it.each([
|
|
491
|
+
['rejects', () => Promise.reject(new Error('no'))],
|
|
492
|
+
['answers with something other than a string', () => ({ token: 'x' })]
|
|
493
|
+
])('counts a sign-in command that %s as signed out', async (_, answer) => {
|
|
494
|
+
const router = new ChatRouter(
|
|
495
|
+
settingsWith({}),
|
|
496
|
+
signedIn(answer),
|
|
497
|
+
noNotebook
|
|
498
|
+
);
|
|
499
|
+
|
|
500
|
+
await expect(router.route('hello')).rejects.toThrow(
|
|
501
|
+
'Sign in to Oceanum.io'
|
|
502
|
+
);
|
|
503
|
+
expect((globalThis as any).fetch).not.toHaveBeenCalled();
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it('says the sign-in was refused on a 401 with the sign-in', async () => {
|
|
507
|
+
answering(() => ({ ok: false, status: 401 }));
|
|
508
|
+
const router = new ChatRouter(
|
|
509
|
+
settingsWith({}),
|
|
510
|
+
signedIn(() => 'a-jwt'),
|
|
511
|
+
noNotebook
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
await expect(router.route('hello')).rejects.toMatchObject({
|
|
515
|
+
message:
|
|
516
|
+
'Your Oceanum.io sign-in was not accepted. Sign in to Oceanum.io again.',
|
|
517
|
+
statusCode: 401
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it('says the token was refused on a 401 with a pasted token', async () => {
|
|
522
|
+
answering(() => ({ ok: false, status: 401 }));
|
|
523
|
+
|
|
524
|
+
await expect(
|
|
525
|
+
new ChatRouter(settings, noSignIn, noNotebook).route('hello')
|
|
526
|
+
).rejects.toMatchObject({
|
|
527
|
+
message: 'Invalid or expired Datamesh token.',
|
|
528
|
+
statusCode: 401
|
|
529
|
+
});
|
|
530
|
+
// Where the host can sign in, the pasted token is what stands in the way.
|
|
531
|
+
await expect(
|
|
532
|
+
new ChatRouter(
|
|
533
|
+
settings,
|
|
534
|
+
signedIn(() => 'a-jwt'),
|
|
535
|
+
noNotebook
|
|
536
|
+
).route('hello')
|
|
537
|
+
).rejects.toThrow('clear it to use your Oceanum.io sign-in');
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it('names the configured address when it cannot be reached', async () => {
|
|
541
|
+
(globalThis as any).fetch = jest.fn(async () => {
|
|
542
|
+
throw new TypeError('Failed to fetch');
|
|
543
|
+
});
|
|
544
|
+
const router = new ChatRouter(
|
|
545
|
+
settingsWith({
|
|
546
|
+
datameshToken: 'a-token',
|
|
547
|
+
aiBackendUrl: 'http://localhost:8000/'
|
|
548
|
+
}),
|
|
549
|
+
noSignIn,
|
|
550
|
+
noNotebook
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
await expect(router.route('hello')).rejects.toThrow(
|
|
554
|
+
'Could not reach Oceanum AI backend at http://localhost:8000. Is it running?'
|
|
555
|
+
);
|
|
556
|
+
});
|
|
557
|
+
});
|
package/src/aiBackend.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
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
|
+
import { OCEANUM_AI_BACKEND_URL } from './constants';
|
|
17
|
+
import { validHttpUrl } from './httpUrl';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The host's command answering with the current Oceanum.io access token (a
|
|
21
|
+
* JWT), or null when signed out. The token is cached and refreshed by the
|
|
22
|
+
* host, so it is asked for on every use rather than kept.
|
|
23
|
+
*/
|
|
24
|
+
export const ACCESS_TOKEN_COMMAND = 'oceanum-auth:access-token';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The host's command that starts signing in to Oceanum.io. Enabled only where
|
|
28
|
+
* sign-in is configured for the site.
|
|
29
|
+
*/
|
|
30
|
+
export const SIGN_IN_COMMAND = 'oceanum-auth:sign-in';
|
|
31
|
+
|
|
32
|
+
/** The host's command that signs out. Visible only while signed in. */
|
|
33
|
+
export const SIGN_OUT_COMMAND = 'oceanum-auth:sign-out';
|
|
34
|
+
|
|
35
|
+
/** The part of the command registry the sign-in is reached through. */
|
|
36
|
+
export type AuthCommands = Pick<
|
|
37
|
+
CommandRegistry,
|
|
38
|
+
'hasCommand' | 'execute' | 'isEnabled' | 'isVisible'
|
|
39
|
+
>;
|
|
40
|
+
|
|
41
|
+
export type AiCredentialSource = 'datamesh-token' | 'sign-in';
|
|
42
|
+
|
|
43
|
+
export interface IAiCredential {
|
|
44
|
+
/** Which credential it is: the 401 message differs. */
|
|
45
|
+
source: AiCredentialSource;
|
|
46
|
+
headers: Record<string, string>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The backend address for an `aiBackendUrl` setting value, without a trailing
|
|
51
|
+
* slash so a path can be appended. An invalid value falls back to the default.
|
|
52
|
+
*/
|
|
53
|
+
export function aiBackendUrl(value: string | null | undefined): string {
|
|
54
|
+
const url = validHttpUrl(value, OCEANUM_AI_BACKEND_URL);
|
|
55
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A `datameshToken` setting value, trimmed; '' when there is none. */
|
|
59
|
+
export function pastedToken(datameshToken: unknown): string {
|
|
60
|
+
return typeof datameshToken === 'string' ? datameshToken.trim() : '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Asks a query of the host's command, treating a missing command, or a query
|
|
65
|
+
* that throws, as false. Queries never emit `commandExecuted`.
|
|
66
|
+
*/
|
|
67
|
+
function query(
|
|
68
|
+
commands: AuthCommands,
|
|
69
|
+
id: string,
|
|
70
|
+
which: 'isEnabled' | 'isVisible'
|
|
71
|
+
): boolean {
|
|
72
|
+
try {
|
|
73
|
+
return commands.hasCommand(id) && commands[which](id);
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Whether the host can sign the user in to Oceanum.io on this site. */
|
|
80
|
+
export function canSignIn(commands: AuthCommands): boolean {
|
|
81
|
+
return query(commands, SIGN_IN_COMMAND, 'isEnabled');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Whether the host says the user is signed in to Oceanum.io. */
|
|
85
|
+
export function isSignedIn(commands: AuthCommands): boolean {
|
|
86
|
+
return query(commands, SIGN_OUT_COMMAND, 'isVisible');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Which credential a request would carry, without fetching it: runs no
|
|
91
|
+
* command, so it is safe to ask whenever something changes.
|
|
92
|
+
*/
|
|
93
|
+
export function aiCredentialSource(
|
|
94
|
+
datameshToken: unknown,
|
|
95
|
+
commands: AuthCommands
|
|
96
|
+
): AiCredentialSource | null {
|
|
97
|
+
if (pastedToken(datameshToken)) {
|
|
98
|
+
return 'datamesh-token';
|
|
99
|
+
}
|
|
100
|
+
if (commands.hasCommand(ACCESS_TOKEN_COMMAND) && isSignedIn(commands)) {
|
|
101
|
+
return 'sign-in';
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Asks the host for its Oceanum.io access token; null without the command.
|
|
108
|
+
* This runs a command: call it only when a token is needed for a request.
|
|
109
|
+
*/
|
|
110
|
+
export function signInToken(commands: AuthCommands): () => Promise<unknown> {
|
|
111
|
+
return async () =>
|
|
112
|
+
commands.hasCommand(ACCESS_TOKEN_COMMAND)
|
|
113
|
+
? commands.execute(ACCESS_TOKEN_COMMAND)
|
|
114
|
+
: null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The credential for a request: the pasted Datamesh token if there is one,
|
|
119
|
+
* otherwise the host's sign-in, otherwise null.
|
|
120
|
+
*
|
|
121
|
+
* @param datameshToken The `datameshToken` setting value.
|
|
122
|
+
* @param getSignInToken Asked only without a pasted token. Anything but a
|
|
123
|
+
* non-empty string -- including a rejection -- counts as signed out.
|
|
124
|
+
*/
|
|
125
|
+
export async function resolveAiCredential(
|
|
126
|
+
datameshToken: unknown,
|
|
127
|
+
getSignInToken: () => Promise<unknown>
|
|
128
|
+
): Promise<IAiCredential | null> {
|
|
129
|
+
const pasted = pastedToken(datameshToken);
|
|
130
|
+
if (pasted) {
|
|
131
|
+
return {
|
|
132
|
+
source: 'datamesh-token',
|
|
133
|
+
headers: { 'X-Datamesh-Token': pasted }
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
let token: unknown;
|
|
137
|
+
try {
|
|
138
|
+
token = await getSignInToken();
|
|
139
|
+
} catch {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const signedIn = typeof token === 'string' ? token.trim() : '';
|
|
143
|
+
if (!signedIn) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
source: 'sign-in',
|
|
148
|
+
headers: { Authorization: `Bearer ${signedIn}` }
|
|
149
|
+
};
|
|
150
|
+
}
|
package/src/chatRouter.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { ISettingRegistry } from '@jupyterlab/settingregistry';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
aiBackendUrl,
|
|
4
|
+
canSignIn,
|
|
5
|
+
resolveAiCredential,
|
|
6
|
+
signInToken,
|
|
7
|
+
type AuthCommands
|
|
8
|
+
} from './aiBackend';
|
|
3
9
|
import type { Progress } from './progress';
|
|
4
10
|
import { readSse } from './sse';
|
|
5
11
|
import type { ObservedRun } from './notebookRun';
|
|
@@ -76,6 +82,8 @@ function asOceanumResponse(data: unknown): OceanumResponse {
|
|
|
76
82
|
|
|
77
83
|
export class ChatRouter {
|
|
78
84
|
/**
|
|
85
|
+
* @param _commands Where the host's Oceanum.io sign-in is asked for, when
|
|
86
|
+
* there is no pasted Datamesh token.
|
|
79
87
|
* @param _notebook What the conversation's notebook holds -- the one its
|
|
80
88
|
* answers are placed in -- or null if there is nothing to send. Asked on
|
|
81
89
|
* every request, so it follows the conversation rather than whichever tab
|
|
@@ -83,6 +91,7 @@ export class ChatRouter {
|
|
|
83
91
|
*/
|
|
84
92
|
constructor(
|
|
85
93
|
private _settings: ISettingRegistry.ISettings,
|
|
94
|
+
private _commands: AuthCommands,
|
|
86
95
|
private _notebook: () => Promise<INotebookSnapshot | null>
|
|
87
96
|
) {}
|
|
88
97
|
|
|
@@ -178,20 +187,30 @@ export class ChatRouter {
|
|
|
178
187
|
signal?: AbortSignal,
|
|
179
188
|
onProgress?: (progress: Progress) => void
|
|
180
189
|
): Promise<unknown> {
|
|
181
|
-
|
|
182
|
-
|
|
190
|
+
// Resolved per request, not once: the sign-in token refreshes, and the
|
|
191
|
+
// user can sign in, sign out or paste a token between two requests.
|
|
192
|
+
const credential = await resolveAiCredential(
|
|
193
|
+
this._settings.get('datameshToken').composite,
|
|
194
|
+
signInToken(this._commands)
|
|
195
|
+
);
|
|
196
|
+
if (!credential) {
|
|
183
197
|
throw new ChatRouterError(
|
|
184
|
-
|
|
198
|
+
canSignIn(this._commands)
|
|
199
|
+
? 'Not signed in. Sign in to Oceanum.io to use Oceanum AI, or set a Datamesh token in Settings → Oceanum.io.'
|
|
200
|
+
: 'Datamesh token not configured. Set your token in Settings → Oceanum.io.'
|
|
185
201
|
);
|
|
186
202
|
}
|
|
203
|
+
const backend = aiBackendUrl(
|
|
204
|
+
this._settings.get('aiBackendUrl').composite as string
|
|
205
|
+
);
|
|
187
206
|
|
|
188
207
|
let response: Response;
|
|
189
208
|
try {
|
|
190
|
-
response = await fetch(`${
|
|
209
|
+
response = await fetch(`${backend}${path}`, {
|
|
191
210
|
method: 'POST',
|
|
192
211
|
headers: {
|
|
193
212
|
'Content-Type': 'application/json',
|
|
194
|
-
|
|
213
|
+
...credential.headers,
|
|
195
214
|
// Ask for the streamed form only when someone is listening. The
|
|
196
215
|
// server requires the media type to be NAMED -- a wildcard never
|
|
197
216
|
// selects it -- so omitting this is what keeps the plain JSON path
|
|
@@ -208,12 +227,21 @@ export class ChatRouter {
|
|
|
208
227
|
throw err;
|
|
209
228
|
}
|
|
210
229
|
throw new ChatRouterError(
|
|
211
|
-
`Could not reach Oceanum AI backend at ${
|
|
230
|
+
`Could not reach Oceanum AI backend at ${backend}. Is it running?`
|
|
212
231
|
);
|
|
213
232
|
}
|
|
214
233
|
|
|
215
234
|
if (response.status === 401) {
|
|
216
|
-
|
|
235
|
+
let message = 'Invalid or expired Datamesh token.';
|
|
236
|
+
if (credential.source === 'sign-in') {
|
|
237
|
+
message =
|
|
238
|
+
'Your Oceanum.io sign-in was not accepted. Sign in to Oceanum.io again.';
|
|
239
|
+
} else if (canSignIn(this._commands)) {
|
|
240
|
+
// A pasted token wins over the sign-in, so say how to fall back to it.
|
|
241
|
+
message +=
|
|
242
|
+
' Update it, or clear it to use your Oceanum.io sign-in, in Settings → Oceanum.io.';
|
|
243
|
+
}
|
|
244
|
+
throw new ChatRouterError(message, 401);
|
|
217
245
|
}
|
|
218
246
|
|
|
219
247
|
if (!response.ok) {
|
package/src/constants.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* These values are compiled into the extension.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
/** URL of the Oceanum AI backend service */
|
|
6
|
+
/** Default URL of the Oceanum AI backend service (the `aiBackendUrl` setting) */
|
|
7
7
|
export const OCEANUM_AI_BACKEND_URL = 'https://ai.oceanum.io';
|
|
8
8
|
|
|
9
9
|
/** URL of the Datamesh UI embedded in the Datamesh panel */
|
package/src/datameshUiUrl.ts
CHANGED
|
@@ -7,18 +7,11 @@
|
|
|
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
|
|
|
11
12
|
/** The Datamesh UI address to use for a `datameshUiUrl` setting value. */
|
|
12
13
|
export function validDatameshUiUrl(url: string | null | undefined): URL {
|
|
13
|
-
|
|
14
|
-
const parsed = new URL((url ?? '').trim());
|
|
15
|
-
if (parsed.protocol === 'https:' || parsed.protocol === 'http:') {
|
|
16
|
-
return parsed;
|
|
17
|
-
}
|
|
18
|
-
} catch {
|
|
19
|
-
// Empty or not a URL: the default below.
|
|
20
|
-
}
|
|
21
|
-
return new URL(DATAMESH_UI_SERVICE);
|
|
14
|
+
return validHttpUrl(url, DATAMESH_UI_SERVICE);
|
|
22
15
|
}
|
|
23
16
|
|
|
24
17
|
/**
|
package/src/httpUrl.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
9
|
+
/** `value` as an http(s) URL, or `fallback` when it is empty or not one. */
|
|
10
|
+
export function validHttpUrl(
|
|
11
|
+
value: string | null | undefined,
|
|
12
|
+
fallback: string
|
|
13
|
+
): URL {
|
|
14
|
+
try {
|
|
15
|
+
const parsed = new URL((value ?? '').trim());
|
|
16
|
+
if (parsed.protocol === 'https:' || parsed.protocol === 'http:') {
|
|
17
|
+
return parsed;
|
|
18
|
+
}
|
|
19
|
+
} catch {
|
|
20
|
+
// Empty or not a URL: the default below.
|
|
21
|
+
}
|
|
22
|
+
return new URL(fallback);
|
|
23
|
+
}
|