@gyldendalas/gyldendal-divine-api 2.0.0 → 2.3.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/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AIChatCompletion, AIChatCompletionRequest, Bookmark, BookmarkUpdate, ClientSettings, ClientSettingsResponse, Continue, CookieConsentRequest, Folder, FolderContent, HighlightAddResult, HighlightCountResponse, HighlightDeleteResult, HighlightRequest, HighlightResponse, HighlightResultType, HighlightSearchRequest, HighlightSearchResponse, HighlightUpdateRequest, HighlightUpdateResponse, MRU, NotesRequest, PdfExportResult, PrintRequest, Quiz, QuizFormResponse, QuizFormType, QuizResult, QuizSession, QuizStartResponse, SaveAnswerResponse, SharedQuizData, SharedStudentQuiz, SharedTeacherQuiz, SiteMapRequest, SynthesisRequest, SynthesisResponse, Tag, TagOutput, WritingTaskAnswer, WritingTaskRequest, WritingTaskResponseBucket } from "@gyldendalsolutions/divine-contracts";
1
+ import { AIChatCompletion, AIChatCompletionRequest, Bookmark, BookmarkUpdate, ClientSettings, ClientSettingsResponse, Continue, CookieConsentRequest, CourseGeneratorChatRequest, CourseGeneratorChatResponse, Folder, FolderContent, HighlightAddResult, HighlightCountResponse, HighlightDeleteResult, HighlightRequest, HighlightResponse, HighlightResultType, HighlightSearchRequest, HighlightSearchResponse, HighlightUpdateRequest, HighlightUpdateResponse, MRU, NotesRequest, PdfExportResult, PrintRequest, Quiz, QuizFormResponse, QuizFormType, QuizResult, QuizSession, QuizStartResponse, SaveAnswerResponse, SearchBookRequest, SearchBooksRequest, SearchResponse, SearchSuggestRequest, SearchSuggestResponse, SharedQuizData, SharedStudentQuiz, SharedTeacherQuiz, SiteMapRequest, SynthesisRequest, SynthesisResponse, Tag, TagOutput, WritingTaskAnswer, WritingTaskRequest, WritingTaskResponseBucket } from "@gyldendalsolutions/divine-contracts";
2
2
  //#region src/jwt.d.ts
3
3
  /**
4
4
  * Extracts the expiry time from a JWT token's `exp` claim.
@@ -222,12 +222,13 @@ declare class BaseService {
222
222
  body: string | null | undefined;
223
223
  timeout: number;
224
224
  }): Promise<Response>;
225
- postAsync({ url, headers, addAuthHeaders, body, timeout }: {
225
+ postAsync({ url, headers, addAuthHeaders, body, timeout, signal }: {
226
226
  url: URL | string;
227
227
  headers: Headers | undefined | null;
228
228
  addAuthHeaders?: boolean;
229
229
  body: string | null | undefined;
230
230
  timeout: number;
231
+ signal?: AbortSignal;
231
232
  }): Promise<Response>;
232
233
  deleteAsync({ url, headers, addAuthHeaders, timeout }: {
233
234
  url: URL | string;
@@ -242,13 +243,18 @@ declare class BaseService {
242
243
  body: string | null | undefined;
243
244
  timeout: number;
244
245
  }): Promise<Response>;
245
- callAsync({ url, body, method, headers, timeout, addAuthHeaders }: {
246
+ callAsync({ url, body, method, headers, timeout, addAuthHeaders, signal }: {
246
247
  url: URL | string;
247
248
  body: string | null | undefined;
248
249
  method: string;
249
250
  headers: Headers | undefined | null;
250
251
  timeout: number;
251
252
  addAuthHeaders?: boolean;
253
+ /**
254
+ * Cancels the request from the caller's side as well, e.g. a suggestion
255
+ * request made stale by the next keystroke. The timeout still applies.
256
+ */
257
+ signal?: AbortSignal;
252
258
  }): Promise<Response>;
253
259
  }
254
260
  //#endregion
@@ -626,6 +632,18 @@ export declare class AIBotService extends BaseService {
626
632
  body: AIChatCompletionRequest;
627
633
  timeout?: number;
628
634
  }): Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>;
635
+ courseGeneratorChat({ body, timeout }: {
636
+ body: CourseGeneratorChatRequest;
637
+ timeout?: number;
638
+ }): Promise<CourseGeneratorChatResponse>;
639
+ /**
640
+ * Streams the turn as NDJSON, one `CourseGeneratorStreamEvent` (from
641
+ * divine-contracts) per line.
642
+ */
643
+ streamingCourseGeneratorChat({ body, timeout }: {
644
+ body: CourseGeneratorChatRequest;
645
+ timeout?: number;
646
+ }): Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>;
629
647
  }
630
648
  //#endregion
631
649
  //#region src/services/quiz_service.d.ts
@@ -761,6 +779,53 @@ export declare class QuizService extends BaseService {
761
779
  }): Promise<any>;
762
780
  }
763
781
  //#endregion
782
+ //#region src/services/solr_proxy_service.d.ts
783
+ /**
784
+ * The v2 search API: typed search endpoints, where v1 passed raw search
785
+ * queries through.
786
+ *
787
+ * Authenticate with the book's search token as `bearerToken` (a browser:
788
+ * the user's groups come from the token) or with an `apiKey` (a server; it
789
+ * may state the user's groups in the request only if its configuration
790
+ * allows it). Every call is a POST, so the query never lands in a URL, and
791
+ * with it in proxy and access logs.
792
+ */
793
+ export declare class SolrProxyService extends BaseService {
794
+ readonly serviceName: "solrProxy";
795
+ /**
796
+ * Searches one book. `hostname` is the book's primary hostname or any of
797
+ * its aliases; the hits' URLs use the one given. Rejects with a 404 for a
798
+ * book that does not exist or is outside the client's scope alike.
799
+ */
800
+ searchBook({ hostname, search, timeout, signal }: {
801
+ hostname: string;
802
+ search: SearchBookRequest;
803
+ timeout?: number;
804
+ signal?: AbortSignal;
805
+ }): Promise<SearchResponse>;
806
+ /**
807
+ * Searches many books: every book the client may search, or those in
808
+ * `search.books`. With `group_by_book`, pages over books instead of hits.
809
+ */
810
+ searchBooks({ search, timeout, signal }: {
811
+ search: SearchBooksRequest;
812
+ timeout?: number;
813
+ signal?: AbortSignal;
814
+ }): Promise<SearchResponse>;
815
+ /**
816
+ * Completions of the word being typed, from the book's headings. Pass a
817
+ * `signal` and abort it on the next keystroke, so a slow answer for an
818
+ * earlier prefix cannot overwrite a newer one.
819
+ */
820
+ suggestBook({ hostname, suggest, timeout, signal }: {
821
+ hostname: string;
822
+ suggest: SearchSuggestRequest;
823
+ timeout?: number;
824
+ signal?: AbortSignal;
825
+ }): Promise<SearchSuggestResponse>;
826
+ private post;
827
+ }
828
+ //#endregion
764
829
  //#region src/services/writing_task_service.d.ts
765
830
  /**
766
831
  * The writing task service authenticates on the bearer token but still reads
@@ -798,10 +863,5 @@ export declare class WritingTaskService extends BaseService {
798
863
  }): Promise<void>;
799
864
  }
800
865
  //#endregion
801
- //#region src/services/solr_proxy_service.d.ts
802
- export declare class SolrProxyService extends BaseService {
803
- readonly serviceName: "solrProxy";
804
- }
805
- //#endregion
806
866
  export { Response$1 as Response };
807
867
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -350,14 +350,15 @@ var BaseService = class {
350
350
  timeout
351
351
  });
352
352
  }
353
- async postAsync({ url, headers, addAuthHeaders = true, body, timeout }) {
353
+ async postAsync({ url, headers, addAuthHeaders = true, body, timeout, signal }) {
354
354
  return await this.callAsync({
355
355
  url,
356
356
  body,
357
357
  method: "POST",
358
358
  headers,
359
359
  addAuthHeaders,
360
- timeout
360
+ timeout,
361
+ signal
361
362
  });
362
363
  }
363
364
  async deleteAsync({ url, headers, addAuthHeaders = true, timeout }) {
@@ -380,14 +381,14 @@ var BaseService = class {
380
381
  timeout
381
382
  });
382
383
  }
383
- async callAsync({ url, body, method, headers, timeout, addAuthHeaders }) {
384
+ async callAsync({ url, body, method, headers, timeout, addAuthHeaders, signal }) {
384
385
  if (!headers) headers = new Headers();
385
386
  if (addAuthHeaders) headers = await this.addAuthHeaders(headers);
386
387
  const response = await fetch(url, {
387
388
  method,
388
389
  body,
389
390
  headers,
390
- signal: AbortSignal.timeout(timeout)
391
+ signal: signal ? AbortSignal.any([AbortSignal.timeout(timeout), signal]) : AbortSignal.timeout(timeout)
391
392
  });
392
393
  if (!response.ok) throw new HTTPError(`Got ${response.status} while calling ${response.url}`, response);
393
394
  return response;
@@ -632,7 +633,7 @@ var UserSettingsBookmarks = class extends UserSettingsBase {
632
633
  })).json();
633
634
  }
634
635
  async getFavourite({ timeout = 5e3 }) {
635
- let url = `${this.getUrlPrefix()}/bookmark/favourite`;
636
+ const url = `${this.getUrlPrefix()}/bookmark/favourite`;
636
637
  const headers = new Headers();
637
638
  return (await this.getAsync({
638
639
  url,
@@ -667,7 +668,7 @@ var UserSettingsContinue = class extends UserSettingsBase {
667
668
  });
668
669
  }
669
670
  async getContinue({ isbn, timeout = 5e3 }) {
670
- let url = `${this.getUrlPrefix()}/continue/isbn/${isbn}`;
671
+ const url = `${this.getUrlPrefix()}/continue/isbn/${isbn}`;
671
672
  const headers = new Headers();
672
673
  try {
673
674
  return (await this.getAsync({
@@ -706,7 +707,7 @@ var UserSettingsMRU = class extends UserSettingsBase {
706
707
  });
707
708
  }
708
709
  async getMRU({ timeout = 5e3 }) {
709
- let url = `${this.getUrlPrefix()}/mru`;
710
+ const url = `${this.getUrlPrefix()}/mru`;
710
711
  const headers = new Headers();
711
712
  return (await this.getAsync({
712
713
  url,
@@ -871,7 +872,7 @@ var UserFolders = class extends TaggingService {
871
872
  resource_type: "myaccount_user_folder",
872
873
  timeout
873
874
  });
874
- let folders = [];
875
+ const folders = [];
875
876
  for (const tag of tags) {
876
877
  const folder_payload = JSON.parse(Buffer.from(tag["tag_value"], "base64").toString());
877
878
  folders.push({
@@ -893,15 +894,14 @@ var UserFolders = class extends TaggingService {
893
894
  * @return Folder[]
894
895
  **/
895
896
  async listNextFolderLevel({ parent_folder_uuid, timeout = 3e3 }) {
896
- let tags;
897
897
  if (!parent_folder_uuid) parent_folder_uuid = this.hasNoParent;
898
- tags = await this.getTagsByName({
898
+ const tags = await this.getTagsByName({
899
899
  identity: await this.getIdentity(),
900
900
  resource_type: "myaccount_user_folder",
901
901
  tag_name: parent_folder_uuid,
902
902
  timeout
903
903
  });
904
- let folders = [];
904
+ const folders = [];
905
905
  for (const tag of tags) {
906
906
  const folder_payload = JSON.parse(Buffer.from(tag["tag_value"], "base64").toString());
907
907
  folders.push({
@@ -923,12 +923,12 @@ var UserFolders = class extends TaggingService {
923
923
  * @return (Folder | FolderContent)[]
924
924
  **/
925
925
  async getFolderContent({ folder_uuid, timeout = 3e3 }) {
926
- let tags = await this.getTagsByName({
926
+ const tags = await this.getTagsByName({
927
927
  identity: await this.getIdentity(),
928
928
  tag_name: folder_uuid,
929
929
  timeout
930
930
  });
931
- let content = [];
931
+ const content = [];
932
932
  for (const tag of tags) if (tag["resource_type"] === "myaccount_user_folder") {
933
933
  const folder_payload = JSON.parse(Buffer.from(tag["tag_value"], "base64").toString());
934
934
  content.push({
@@ -1059,6 +1059,12 @@ var UserFolders = class extends TaggingService {
1059
1059
  };
1060
1060
  //#endregion
1061
1061
  //#region src/services/ai_bot_service.ts
1062
+ /**
1063
+ * A turn runs an agent through many searches and tool calls, so it takes
1064
+ * minutes where a plain chat takes seconds. The timeout covers reading the
1065
+ * whole stream, not just its first byte.
1066
+ */
1067
+ const COURSE_GENERATOR_TIMEOUT = 3e5;
1062
1068
  var AIBotService = class extends BaseService {
1063
1069
  serviceName = "aiBot";
1064
1070
  async constructChatUrl({ interactivityId, isbn, stream }) {
@@ -1102,6 +1108,32 @@ var AIBotService = class extends BaseService {
1102
1108
  if (!responseBody) throw new Error("Response body is null");
1103
1109
  return responseBody;
1104
1110
  }
1111
+ async courseGeneratorChat({ body, timeout = COURSE_GENERATOR_TIMEOUT }) {
1112
+ const headers = new Headers();
1113
+ headers.append("Content-Type", "application/json");
1114
+ return (await this.postAsync({
1115
+ url: `${this.getUrlPrefix()}/v1/course_generator/chat`,
1116
+ headers,
1117
+ body: JSON.stringify(body),
1118
+ timeout
1119
+ })).json();
1120
+ }
1121
+ /**
1122
+ * Streams the turn as NDJSON, one `CourseGeneratorStreamEvent` (from
1123
+ * divine-contracts) per line.
1124
+ */
1125
+ async streamingCourseGeneratorChat({ body, timeout = COURSE_GENERATOR_TIMEOUT }) {
1126
+ const headers = new Headers();
1127
+ headers.append("Content-Type", "application/json");
1128
+ const responseBody = (await this.postAsync({
1129
+ url: `${this.getUrlPrefix()}/v1/course_generator/chat/stream`,
1130
+ headers,
1131
+ body: JSON.stringify(body),
1132
+ timeout
1133
+ })).body;
1134
+ if (!responseBody) throw new Error("Response body is null");
1135
+ return responseBody;
1136
+ }
1105
1137
  };
1106
1138
  //#endregion
1107
1139
  //#region src/services/quiz_service.ts
@@ -1330,6 +1362,58 @@ var QuizService = class extends BaseService {
1330
1362
  }
1331
1363
  };
1332
1364
  //#endregion
1365
+ //#region src/services/solr_proxy_service.ts
1366
+ /**
1367
+ * The v2 search API: typed search endpoints, where v1 passed raw search
1368
+ * queries through.
1369
+ *
1370
+ * Authenticate with the book's search token as `bearerToken` (a browser:
1371
+ * the user's groups come from the token) or with an `apiKey` (a server; it
1372
+ * may state the user's groups in the request only if its configuration
1373
+ * allows it). Every call is a POST, so the query never lands in a URL, and
1374
+ * with it in proxy and access logs.
1375
+ */
1376
+ var SolrProxyService = class extends BaseService {
1377
+ serviceName = "solrProxy";
1378
+ /**
1379
+ * Searches one book. `hostname` is the book's primary hostname or any of
1380
+ * its aliases; the hits' URLs use the one given. Rejects with a 404 for a
1381
+ * book that does not exist or is outside the client's scope alike.
1382
+ */
1383
+ async searchBook({ hostname, search, timeout = 1e4, signal }) {
1384
+ const url = `${this.getUrlPrefix()}/api/v2/books/${encodeURIComponent(hostname)}/search`;
1385
+ return this.post(url, search, timeout, signal);
1386
+ }
1387
+ /**
1388
+ * Searches many books: every book the client may search, or those in
1389
+ * `search.books`. With `group_by_book`, pages over books instead of hits.
1390
+ */
1391
+ async searchBooks({ search, timeout = 1e4, signal }) {
1392
+ const url = `${this.getUrlPrefix()}/api/v2/search`;
1393
+ return this.post(url, search, timeout, signal);
1394
+ }
1395
+ /**
1396
+ * Completions of the word being typed, from the book's headings. Pass a
1397
+ * `signal` and abort it on the next keystroke, so a slow answer for an
1398
+ * earlier prefix cannot overwrite a newer one.
1399
+ */
1400
+ async suggestBook({ hostname, suggest, timeout = 3e3, signal }) {
1401
+ const url = `${this.getUrlPrefix()}/api/v2/books/${encodeURIComponent(hostname)}/suggest`;
1402
+ return this.post(url, suggest, timeout, signal);
1403
+ }
1404
+ async post(url, body, timeout, signal) {
1405
+ const headers = new Headers();
1406
+ headers.set("Content-Type", "application/json");
1407
+ return (await this.postAsync({
1408
+ url,
1409
+ headers,
1410
+ body: JSON.stringify(body),
1411
+ timeout,
1412
+ signal
1413
+ })).json();
1414
+ }
1415
+ };
1416
+ //#endregion
1333
1417
  //#region src/services/writing_task_service.ts
1334
1418
  /**
1335
1419
  * The writing task service authenticates on the bearer token but still reads
@@ -1397,11 +1481,6 @@ var WritingTaskService = class extends BaseService {
1397
1481
  }
1398
1482
  };
1399
1483
  //#endregion
1400
- //#region src/services/solr_proxy_service.ts
1401
- var SolrProxyService = class extends BaseService {
1402
- serviceName = "solrProxy";
1403
- };
1404
- //#endregion
1405
1484
  export { AIBotService, CookieConsentLog, ENVIRONMENTS, GALE_QA_URL, HighlightService, PdfGeneratorService, PollyService, QuizService, SERVICE_URLS, SolrProxyService, TaggingService, UserFolders, UserSettingsBookmarks, UserSettingsClientSettings, UserSettingsContinue, UserSettingsMRU, WritingTaskService, ambientOverride, getJwtExpiry, isServiceEnvironment, overrideEnvVarName, overrideToUrl, reportOverride, resetOverrideReports, resolveServiceUrl };
1406
1485
 
1407
1486
  //# sourceMappingURL=index.mjs.map