@carlonicora/nextjs-jsonapi 3.12.0 → 3.14.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.
Files changed (145) hide show
  1. package/dist/{AssistantInterface-DwdBzS9f.d.ts → AssistantInterface-RvZzlcA_.d.ts} +2 -0
  2. package/dist/{AssistantInterface-DMh-zApr.d.mts → AssistantInterface-jL56dyNc.d.mts} +2 -0
  3. package/dist/{AssistantMessageInterface-0zyVingf.d.ts → AssistantMessageInterface-CuaMDs2i.d.ts} +1 -1
  4. package/dist/{AssistantMessageInterface-ddyG114R.d.mts → AssistantMessageInterface-Du3ShThp.d.mts} +1 -1
  5. package/dist/{BlockNoteEditor-CR5KH4AI.mjs → BlockNoteEditor-QG5OWH5K.mjs} +4 -4
  6. package/dist/{BlockNoteEditor-6G27S6P3.js → BlockNoteEditor-WP4K423H.js} +19 -19
  7. package/dist/{BlockNoteEditor-6G27S6P3.js.map → BlockNoteEditor-WP4K423H.js.map} +1 -1
  8. package/dist/{BlockNoteViewer-5MITJ7AD.js → BlockNoteViewer-CWFPYYIU.js} +3 -3
  9. package/dist/{BlockNoteViewer-5MITJ7AD.js.map → BlockNoteViewer-CWFPYYIU.js.map} +1 -1
  10. package/dist/{BlockNoteViewer-NJKFUNXP.mjs → BlockNoteViewer-P2VKEFRN.mjs} +2 -2
  11. package/dist/{ai-connection.module-28f-hux0.d.mts → HandbookSectionInterface-BOxIdBdd.d.mts} +54 -1
  12. package/dist/{ai-connection.module-Bqwh_DnR.d.ts → HandbookSectionInterface-CAXxbDw0.d.ts} +54 -1
  13. package/dist/{auth.interface-B6ZQLGjS.d.ts → auth.interface-BC1jp_ni.d.ts} +1 -1
  14. package/dist/{auth.interface-jYD8Gc4P.d.mts → auth.interface-dR2-6gPM.d.mts} +1 -1
  15. package/dist/billing/index.js +401 -401
  16. package/dist/billing/index.mjs +3 -3
  17. package/dist/{chunk-JEGL7KHK.mjs → chunk-46BRB36R.mjs} +2847 -1803
  18. package/dist/chunk-46BRB36R.mjs.map +1 -0
  19. package/dist/{chunk-L7DZSTFU.js → chunk-IB7DTXWG.js} +846 -129
  20. package/dist/chunk-IB7DTXWG.js.map +1 -0
  21. package/dist/{chunk-OKGGYCO5.js → chunk-OH6GJY4B.js} +2162 -1118
  22. package/dist/chunk-OH6GJY4B.js.map +1 -0
  23. package/dist/{chunk-Q2BREXH3.mjs → chunk-OSFYCSTW.mjs} +724 -7
  24. package/dist/chunk-OSFYCSTW.mjs.map +1 -0
  25. package/dist/{chunk-HVNEWDRF.mjs → chunk-VZKJIAND.mjs} +2 -2
  26. package/dist/{chunk-4ETVCRQD.js → chunk-XH4IURUD.js} +7 -7
  27. package/dist/{chunk-4ETVCRQD.js.map → chunk-XH4IURUD.js.map} +1 -1
  28. package/dist/client/index.d.mts +2 -2
  29. package/dist/client/index.d.ts +2 -2
  30. package/dist/client/index.js +4 -4
  31. package/dist/client/index.mjs +3 -3
  32. package/dist/components/index.d.mts +270 -7
  33. package/dist/components/index.d.ts +270 -7
  34. package/dist/components/index.js +32 -4
  35. package/dist/components/index.js.map +1 -1
  36. package/dist/components/index.mjs +31 -3
  37. package/dist/contexts/index.d.mts +17 -5
  38. package/dist/contexts/index.d.ts +17 -5
  39. package/dist/contexts/index.js +4 -4
  40. package/dist/contexts/index.mjs +3 -3
  41. package/dist/core/index.d.mts +491 -11
  42. package/dist/core/index.d.ts +491 -11
  43. package/dist/core/index.js +38 -2
  44. package/dist/core/index.js.map +1 -1
  45. package/dist/core/index.mjs +37 -1
  46. package/dist/features/help/index.js +37 -37
  47. package/dist/features/help/index.mjs +3 -3
  48. package/dist/features/tokenusage/index.js +101 -101
  49. package/dist/features/tokenusage/index.mjs +3 -3
  50. package/dist/index.d.mts +7 -7
  51. package/dist/index.d.ts +7 -7
  52. package/dist/index.js +39 -3
  53. package/dist/index.js.map +1 -1
  54. package/dist/index.mjs +38 -2
  55. package/dist/{notification.interface-DYF56CtE.d.ts → notification.interface-CjzV_KII.d.ts} +1 -1
  56. package/dist/{notification.interface-BWOCvKRP.d.mts → notification.interface-MnRt1Tjm.d.mts} +1 -1
  57. package/dist/{s3.service-D_dswjgz.d.mts → s3.service-C0Yq2Sx3.d.mts} +2 -2
  58. package/dist/{s3.service-DserU9VK.d.ts → s3.service-CUGarZs3.d.ts} +2 -2
  59. package/dist/server/index.d.mts +3 -3
  60. package/dist/server/index.d.ts +3 -3
  61. package/dist/server/index.js +3 -3
  62. package/dist/server/index.mjs +1 -1
  63. package/dist/{useSocket-BZG9oX-L.d.mts → useSocket-BcH7l7B8.d.mts} +1 -1
  64. package/dist/{useSocket-CiyTtYFj.d.ts → useSocket-DCZd_eJk.d.ts} +1 -1
  65. package/package.json +2 -1
  66. package/src/components/containers/MermaidDiagram.tsx +117 -0
  67. package/src/components/containers/ReactMarkdownContainer.tsx +98 -15
  68. package/src/components/containers/index.ts +1 -0
  69. package/src/components/index.ts +1 -0
  70. package/src/core/index.ts +5 -0
  71. package/src/core/registry/ModuleRegistry.ts +4 -0
  72. package/src/features/assistant/components/parts/AssistantComposer.tsx +22 -3
  73. package/src/features/assistant/components/parts/AssistantSidebar.tsx +13 -3
  74. package/src/features/assistant/components/parts/AssistantThreadHeader.tsx +10 -3
  75. package/src/features/assistant/contexts/AssistantContext.tsx +30 -3
  76. package/src/features/assistant/contexts/__tests__/AssistantContext.spec.tsx +32 -0
  77. package/src/features/assistant/data/Assistant.ts +2 -0
  78. package/src/features/assistant/data/AssistantInterface.ts +2 -0
  79. package/src/features/assistant/data/AssistantService.ts +4 -0
  80. package/src/features/assistant-message/components/parts/MessageSourcesContainer.tsx +5 -1
  81. package/src/features/assistant-message/data/AssistantMessage.ts +27 -2
  82. package/src/features/content/data/content.interface.ts +1 -1
  83. package/src/features/handbook/HandbookPageModule.ts +41 -0
  84. package/src/features/handbook/HandbookSectionModule.ts +35 -0
  85. package/src/features/handbook/HandbookThreadMessageModule.ts +21 -0
  86. package/src/features/handbook/HandbookThreadModule.ts +29 -0
  87. package/src/features/handbook/components/containers/HandbookAdminContainer.tsx +17 -0
  88. package/src/features/handbook/components/containers/HandbookAskContainer.tsx +142 -0
  89. package/src/features/handbook/components/containers/HandbookAskSheet.tsx +149 -0
  90. package/src/features/handbook/components/containers/HandbookPageContainer.tsx +143 -0
  91. package/src/features/handbook/components/containers/__tests__/HandbookAskContainer.spec.tsx +268 -0
  92. package/src/features/handbook/components/containers/__tests__/HandbookAskSheet.spec.tsx +143 -0
  93. package/src/features/handbook/components/containers/__tests__/HandbookPageContainer.spec.tsx +288 -0
  94. package/src/features/handbook/components/details/HandbookPageDetails.tsx +52 -0
  95. package/src/features/handbook/components/details/HandbookPageNeighbours.tsx +72 -0
  96. package/src/features/handbook/components/details/HandbookPageToc.tsx +165 -0
  97. package/src/features/handbook/components/forms/HandbookSyncButton.tsx +43 -0
  98. package/src/features/handbook/components/forms/__tests__/HandbookSyncButton.spec.tsx +42 -0
  99. package/src/features/handbook/components/index.ts +12 -0
  100. package/src/features/handbook/components/lists/HandbookContents.tsx +196 -0
  101. package/src/features/handbook/components/lists/HandbookSectionGroup.tsx +153 -0
  102. package/src/features/handbook/components/lists/__tests__/HandbookContents.spec.tsx +247 -0
  103. package/src/features/handbook/components/parts/HandbookIndexStatus.tsx +62 -0
  104. package/src/features/handbook/components/parts/HandbookPageNavigator.tsx +143 -0
  105. package/src/features/handbook/contexts/HandbookContext.tsx +109 -0
  106. package/src/features/handbook/contexts/__tests__/HandbookContext.spec.tsx +131 -0
  107. package/src/features/handbook/data/HandbookPage.spec.ts +136 -0
  108. package/src/features/handbook/data/HandbookPage.ts +99 -0
  109. package/src/features/handbook/data/HandbookPageFields.ts +31 -0
  110. package/src/features/handbook/data/HandbookPageInput.ts +8 -0
  111. package/src/features/handbook/data/HandbookPageInterface.ts +29 -0
  112. package/src/features/handbook/data/HandbookPageService.ts +60 -0
  113. package/src/features/handbook/data/HandbookSection.spec.ts +58 -0
  114. package/src/features/handbook/data/HandbookSection.ts +73 -0
  115. package/src/features/handbook/data/HandbookSectionInput.ts +12 -0
  116. package/src/features/handbook/data/HandbookSectionInterface.ts +26 -0
  117. package/src/features/handbook/data/HandbookSectionService.ts +26 -0
  118. package/src/features/handbook/data/HandbookThread.ts +114 -0
  119. package/src/features/handbook/data/HandbookThreadInterface.ts +31 -0
  120. package/src/features/handbook/data/HandbookThreadMessage.ts +144 -0
  121. package/src/features/handbook/data/HandbookThreadMessageInterface.ts +35 -0
  122. package/src/features/handbook/data/HandbookThreadMessageService.ts +30 -0
  123. package/src/features/handbook/data/HandbookThreadService.ts +86 -0
  124. package/src/features/handbook/data/__tests__/HandbookThreadService.spec.ts +112 -0
  125. package/src/features/handbook/data/handbookDisplay.spec.ts +92 -0
  126. package/src/features/handbook/data/handbookDisplay.ts +55 -0
  127. package/src/features/handbook/data/index.ts +16 -0
  128. package/src/features/handbook/hooks/__tests__/useHandbookAsk.spec.ts +198 -0
  129. package/src/features/handbook/hooks/useHandbookAsk.ts +213 -0
  130. package/src/features/handbook/i18n-keys.ts +46 -0
  131. package/src/features/handbook/index.ts +7 -0
  132. package/src/features/how-to/components/forms/HowToEditor.tsx +0 -1
  133. package/src/features/how-to/components/forms/HowToReindexButton.spec.tsx +84 -0
  134. package/src/features/how-to/components/forms/HowToReindexButton.tsx +46 -0
  135. package/src/features/how-to/components/index.ts +1 -0
  136. package/src/features/how-to/components/lists/HowToList.tsx +5 -1
  137. package/src/features/how-to/data/HowTo.spec.ts +61 -1
  138. package/src/features/how-to/data/HowToService.ts +13 -0
  139. package/dist/chunk-JEGL7KHK.mjs.map +0 -1
  140. package/dist/chunk-L7DZSTFU.js.map +0 -1
  141. package/dist/chunk-OKGGYCO5.js.map +0 -1
  142. package/dist/chunk-Q2BREXH3.mjs.map +0 -1
  143. /package/dist/{BlockNoteEditor-CR5KH4AI.mjs.map → BlockNoteEditor-QG5OWH5K.mjs.map} +0 -0
  144. /package/dist/{BlockNoteViewer-NJKFUNXP.mjs.map → BlockNoteViewer-P2VKEFRN.mjs.map} +0 -0
  145. /package/dist/{chunk-HVNEWDRF.mjs.map → chunk-VZKJIAND.mjs.map} +0 -0
@@ -0,0 +1,60 @@
1
+ import { AbstractService, EndpointCreator, HttpMethod, Modules, NextRef, PreviousRef } from "../../../core";
2
+ import { HandbookPageInterface } from "./HandbookPageInterface";
3
+
4
+ export class HandbookPageService extends AbstractService {
5
+ /**
6
+ * `next` / `prev` are not optional extras: `useDataListRetriever` drives
7
+ * pagination by handing the cursor back here, so a findMany that drops them
8
+ * renders exactly one page and no navigation, however many rows exist.
9
+ * Mirrors HowToService.findMany.
10
+ */
11
+ static async findMany(
12
+ params: {
13
+ search?: string;
14
+ fetchAll?: boolean;
15
+ next?: NextRef;
16
+ prev?: PreviousRef;
17
+ } = {},
18
+ ): Promise<HandbookPageInterface[]> {
19
+ const endpoint = new EndpointCreator({ endpoint: Modules.HandbookPage });
20
+
21
+ if (params.fetchAll) endpoint.addAdditionalParam("fetchAll", "true");
22
+ if (params.search) endpoint.addAdditionalParam("search", params.search);
23
+ if (Modules.HandbookPage.inclusions?.lists?.fields)
24
+ endpoint.limitToFields(Modules.HandbookPage.inclusions.lists.fields);
25
+ if (Modules.HandbookPage.inclusions?.lists?.types)
26
+ endpoint.limitToType(Modules.HandbookPage.inclusions.lists.types);
27
+
28
+ return this.callApi({
29
+ type: Modules.HandbookPage,
30
+ method: HttpMethod.GET,
31
+ endpoint: endpoint.generate(),
32
+ next: params.next,
33
+ });
34
+ }
35
+
36
+ static async findOne(params: { id: string }): Promise<HandbookPageInterface> {
37
+ return this.callApi<HandbookPageInterface>({
38
+ type: Modules.HandbookPage,
39
+ method: HttpMethod.GET,
40
+ endpoint: new EndpointCreator({ endpoint: Modules.HandbookPage, id: params.id }).generate(),
41
+ });
42
+ }
43
+
44
+ /** POST /handbookpages/sync — replies 204; the caller refetches the list. */
45
+ static async sync(): Promise<void> {
46
+ await this.callApi({
47
+ type: Modules.HandbookPage,
48
+ method: HttpMethod.POST,
49
+ endpoint: new EndpointCreator({ endpoint: Modules.HandbookPage, id: "sync" }).generate(),
50
+ });
51
+ }
52
+
53
+ static async delete(params: { id: string }): Promise<void> {
54
+ await this.callApi({
55
+ type: Modules.HandbookPage,
56
+ method: HttpMethod.DELETE,
57
+ endpoint: new EndpointCreator({ endpoint: Modules.HandbookPage, id: params.id }).generate(),
58
+ });
59
+ }
60
+ }
@@ -0,0 +1,58 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { HandbookSection } from "./HandbookSection";
3
+
4
+ describe("HandbookSection", () => {
5
+ it("rehydrates every attribute", () => {
6
+ const section = new HandbookSection().rehydrate({
7
+ jsonApi: {
8
+ type: "handbooksections",
9
+ id: "s1",
10
+ attributes: { key: "03-backend", title: "Backend", summary: "The API.", order: "03-backend" },
11
+ },
12
+ included: [],
13
+ } as any);
14
+
15
+ expect(section.key).toBe("03-backend");
16
+ expect(section.title).toBe("Backend");
17
+ expect(section.summary).toBe("The API.");
18
+ expect(section.order).toBe("03-backend");
19
+ });
20
+
21
+ it("defaults missing strings and leaves summary undefined", () => {
22
+ const section = new HandbookSection().rehydrate({
23
+ jsonApi: { type: "handbooksections", id: "s1", attributes: {} },
24
+ included: [],
25
+ } as any);
26
+
27
+ expect(section.key).toBe("");
28
+ expect(section.title).toBe("");
29
+ expect(section.summary).toBeUndefined();
30
+ expect(section.displayTitle).toBeUndefined();
31
+ expect(section.displaySummary).toBeUndefined();
32
+ });
33
+
34
+ it("rehydrates the translation beside the English original, leaving the key alone", () => {
35
+ const section = new HandbookSection().rehydrate({
36
+ jsonApi: {
37
+ type: "handbooksections",
38
+ id: "s1",
39
+ attributes: {
40
+ key: "03-backend",
41
+ title: "Backend",
42
+ summary: "The API.",
43
+ order: "03-backend",
44
+ displayTitle: "Backend (API)",
45
+ displaySummary: "Le API.",
46
+ },
47
+ },
48
+ included: [],
49
+ } as any);
50
+
51
+ expect(section.displayTitle).toBe("Backend (API)");
52
+ expect(section.displaySummary).toBe("Le API.");
53
+ // `key` and `order` are what pages are matched and sorted by; they stay
54
+ // English whatever the translation says.
55
+ expect(section.key).toBe("03-backend");
56
+ expect(section.order).toBe("03-backend");
57
+ });
58
+ });
@@ -0,0 +1,73 @@
1
+ import { AbstractApiData, JsonApiHydratedDataInterface, Modules } from "../../../core";
2
+ import { HandbookSectionInput } from "./HandbookSectionInput";
3
+ import { HandbookSectionInterface } from "./HandbookSectionInterface";
4
+
5
+ export class HandbookSection extends AbstractApiData implements HandbookSectionInterface {
6
+ private _key?: string;
7
+ private _title?: string;
8
+ private _summary?: string;
9
+ private _order?: string;
10
+ private _displayTitle?: string;
11
+ private _displaySummary?: string;
12
+
13
+ get key(): string {
14
+ return this._key ?? "";
15
+ }
16
+
17
+ get title(): string {
18
+ return this._title ?? "";
19
+ }
20
+
21
+ get summary(): string | undefined {
22
+ return this._summary;
23
+ }
24
+
25
+ get order(): string {
26
+ return this._order ?? "";
27
+ }
28
+
29
+ get displayTitle(): string | undefined {
30
+ return this._displayTitle;
31
+ }
32
+
33
+ get displaySummary(): string | undefined {
34
+ return this._displaySummary;
35
+ }
36
+
37
+ rehydrate(data: JsonApiHydratedDataInterface): this {
38
+ super.rehydrate(data);
39
+
40
+ this._key = data.jsonApi.attributes.key;
41
+ this._title = data.jsonApi.attributes.title;
42
+ this._summary = data.jsonApi.attributes.summary;
43
+ this._order = data.jsonApi.attributes.order;
44
+ this._displayTitle = data.jsonApi.attributes.displayTitle;
45
+ this._displaySummary = data.jsonApi.attributes.displaySummary;
46
+
47
+ return this;
48
+ }
49
+
50
+ /**
51
+ * Nothing calls this: sections are read-only over HTTP and written only by
52
+ * the ingest. It exists because every model implements `createJsonApi()`, and
53
+ * a model that throws on serialisation is a trap for the next writer.
54
+ */
55
+ createJsonApi(data: HandbookSectionInput) {
56
+ const response: any = {
57
+ data: {
58
+ type: Modules.HandbookSection.name,
59
+ id: data.id,
60
+ attributes: {},
61
+ relationships: {},
62
+ },
63
+ included: [],
64
+ };
65
+
66
+ if (data.key !== undefined) response.data.attributes.key = data.key;
67
+ if (data.title !== undefined) response.data.attributes.title = data.title;
68
+ if (data.summary !== undefined) response.data.attributes.summary = data.summary;
69
+ if (data.order !== undefined) response.data.attributes.order = data.order;
70
+
71
+ return response;
72
+ }
73
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Handbook sections are written only by the backend ingest, which replaces
3
+ * every one of them on each sync. No client ever posts one; this type exists so
4
+ * the model can satisfy AbstractApiData's `createJsonApi()` contract.
5
+ */
6
+ export type HandbookSectionInput = {
7
+ id: string;
8
+ key: string;
9
+ title: string;
10
+ summary?: string;
11
+ order: string;
12
+ };
@@ -0,0 +1,26 @@
1
+ import { ApiDataInterface } from "../../../core";
2
+
3
+ /**
4
+ * One top-level directory of the configured documentation tree.
5
+ *
6
+ * A page names its section by `key`, not by edge, so this interface carries no
7
+ * relationships: the contents page reads sections and pages in two calls and
8
+ * groups them client side.
9
+ */
10
+ export interface HandbookSectionInterface extends ApiDataInterface {
11
+ /** The directory name, e.g. `03-backend`. The natural key of the ingest. */
12
+ get key(): string;
13
+ /** The prettified `key`, or the README heading when the index names one. */
14
+ get title(): string;
15
+ /** The README's blurb for the section. Absent when the tree has no index. */
16
+ get summary(): string | undefined;
17
+ /** The `key`; sorting on it reproduces the directories' numeric order. */
18
+ get order(): string;
19
+ /**
20
+ * The translated rendering of `title`, when the index carries one. Every
21
+ * reader-facing surface renders `displayTitle ?? title`.
22
+ */
23
+ get displayTitle(): string | undefined;
24
+ /** The translated `summary`. Absent when the section has no translation. */
25
+ get displaySummary(): string | undefined;
26
+ }
@@ -0,0 +1,26 @@
1
+ import { AbstractService, EndpointCreator, HttpMethod, Modules } from "../../../core";
2
+ import { HandbookSectionInterface } from "./HandbookSectionInterface";
3
+
4
+ export class HandbookSectionService extends AbstractService {
5
+ /**
6
+ * Every section in one read, always.
7
+ *
8
+ * There are as many sections as an application has documentation directories
9
+ * — ten or so — so this takes no `fetchAll` argument and no cursor: unlike
10
+ * the page list, nothing here is ever a second page, and a caller that could
11
+ * ask for one page of sections would only be able to get it wrong.
12
+ */
13
+ static async findMany(): Promise<HandbookSectionInterface[]> {
14
+ const endpoint = new EndpointCreator({ endpoint: Modules.HandbookSection });
15
+
16
+ endpoint.addAdditionalParam("fetchAll", "true");
17
+ if (Modules.HandbookSection.inclusions?.lists?.fields)
18
+ endpoint.limitToFields(Modules.HandbookSection.inclusions.lists.fields);
19
+
20
+ return this.callApi({
21
+ type: Modules.HandbookSection,
22
+ method: HttpMethod.GET,
23
+ endpoint: endpoint.generate(),
24
+ });
25
+ }
26
+ }
@@ -0,0 +1,114 @@
1
+ import { AbstractApiData, JsonApiHydratedDataInterface, Modules } from "../../../core";
2
+ import { HandbookThreadInput, HandbookThreadInterface } from "./HandbookThreadInterface";
3
+ import { HandbookThreadMessageInterface } from "./HandbookThreadMessageInterface";
4
+
5
+ export class HandbookThread extends AbstractApiData implements HandbookThreadInterface {
6
+ private _title?: string;
7
+ private _messageCount?: number;
8
+ private _messages?: HandbookThreadMessageInterface[];
9
+
10
+ get title(): string {
11
+ return this._title ?? "";
12
+ }
13
+
14
+ get messageCount(): number {
15
+ return this._messageCount ?? 0;
16
+ }
17
+
18
+ /**
19
+ * The handbook runs one agent and has no engine choice, so nothing routes on
20
+ * this. It exists because `AssistantInterface` declares it, which is what
21
+ * lets the shared chat chrome take a handbook thread unchanged.
22
+ */
23
+ get engine(): string | undefined {
24
+ return undefined;
25
+ }
26
+
27
+ /** A handbook thread is never bound to a product resource. */
28
+ get boundContentType(): string | undefined {
29
+ return undefined;
30
+ }
31
+
32
+ get boundContentId(): string | undefined {
33
+ return undefined;
34
+ }
35
+
36
+ get messages(): HandbookThreadMessageInterface[] {
37
+ return this._messages ?? [];
38
+ }
39
+
40
+ rehydrate(data: JsonApiHydratedDataInterface): this {
41
+ super.rehydrate(data);
42
+
43
+ const attributes = data.jsonApi.attributes ?? {};
44
+ this._title = attributes.title;
45
+
46
+ const fromMeta = data.jsonApi.meta?.messageCount;
47
+ this._messageCount =
48
+ typeof fromMeta === "number"
49
+ ? fromMeta
50
+ : typeof attributes.messageCount === "number"
51
+ ? attributes.messageCount
52
+ : 0;
53
+
54
+ const messages = this._readIncluded<HandbookThreadMessageInterface>(
55
+ data,
56
+ "messages",
57
+ Modules.HandbookThreadMessage,
58
+ );
59
+ this._messages = Array.isArray(messages) ? messages : messages ? [messages] : [];
60
+
61
+ // `groupThreadsByBucket` buckets the sidebar on `updatedAt` as a Date, and
62
+ // `AbstractApiData` only reads it from `meta`. A thread that carries the
63
+ // timestamp as an attribute instead would otherwise fall back to "now" and
64
+ // land every thread in "Today".
65
+ if (this._updatedAt === undefined && attributes.updatedAt !== undefined)
66
+ this._updatedAt = new Date(attributes.updatedAt);
67
+ if (this._createdAt === undefined && attributes.createdAt !== undefined)
68
+ this._createdAt = new Date(attributes.createdAt);
69
+
70
+ return this;
71
+ }
72
+
73
+ /**
74
+ * The envelope `POST /handbookthreads` accepts: the opening question. The
75
+ * server derives the title from it, so no title is sent and no client id —
76
+ * the thread's identity is the server's to mint.
77
+ *
78
+ * `handbookPageId` rides the same way `question` does, through this method
79
+ * and never through `overridesJsonApiCreation`. It is written only when the
80
+ * caller supplied it: the DTO takes it as optional, and sending an explicit
81
+ * `null` would fail validation rather than mean "the whole manual".
82
+ */
83
+ createJsonApi(data: HandbookThreadInput) {
84
+ return {
85
+ data: {
86
+ type: Modules.HandbookThread.name,
87
+ attributes: {
88
+ question: data.question,
89
+ ...(data.handbookPageId !== undefined ? { handbookPageId: data.handbookPageId } : {}),
90
+ },
91
+ },
92
+ included: [],
93
+ };
94
+ }
95
+
96
+ /**
97
+ * The envelope `PATCH /handbookthreads/:id` accepts. A dedicated method
98
+ * rather than a branch inside `createJsonApi`: the rename sends the title and
99
+ * nothing else, while a create sends the question and nothing else, and the
100
+ * service must never hand-build either.
101
+ */
102
+ createRenameJsonApi(params: { id: string; title: string }) {
103
+ return {
104
+ data: {
105
+ type: Modules.HandbookThread.name,
106
+ id: params.id,
107
+ attributes: {
108
+ title: params.title,
109
+ },
110
+ },
111
+ included: [],
112
+ };
113
+ }
114
+ }
@@ -0,0 +1,31 @@
1
+ import { AssistantInterface } from "../../assistant/data/AssistantInterface";
2
+ import { HandbookThreadMessageInterface } from "./HandbookThreadMessageInterface";
3
+
4
+ /**
5
+ * A persisted handbook conversation.
6
+ *
7
+ * It extends `AssistantInterface` so the shared chat chrome —
8
+ * `AssistantSidebar`, `AssistantThreadHeader`, `groupThreadsByBucket` — accepts
9
+ * it verbatim. It is NOT an Assistant: an Assistant is company scoped and a
10
+ * platform administrator has no company, so the handbook owns its own threads
11
+ * and answers `engine` / `boundContentType` / `boundContentId` with `undefined`.
12
+ */
13
+ export interface HandbookThreadInterface extends AssistantInterface {
14
+ /** Side-loaded by `GET /handbookthreads/:id`; empty on the list endpoint. */
15
+ get messages(): HandbookThreadMessageInterface[];
16
+ }
17
+
18
+ /**
19
+ * The payload that opens a thread: the first question. The server derives the
20
+ * title from it, so nothing else is sent.
21
+ */
22
+ export type HandbookThreadInput = {
23
+ id: string;
24
+ question: string;
25
+ /**
26
+ * Scopes retrieval to one handbook page. Set only by the reader's ask sheet,
27
+ * when the reader turns the "this page only" chip on; absent everywhere else,
28
+ * and the agent then searches the whole manual.
29
+ */
30
+ handbookPageId?: string;
31
+ };
@@ -0,0 +1,144 @@
1
+ import { v4 as uuidv4 } from "uuid";
2
+ import { AbstractApiData, ApiDataInterface, JsonApiHydratedDataInterface, Modules } from "../../../core";
3
+ import type {
4
+ AssistantMessageRole,
5
+ AssistantMessageType,
6
+ } from "../../assistant-message/data/AssistantMessageInterface";
7
+ import { ChunkInterface, ChunkRelationshipMeta } from "../../chunk/data/ChunkInterface";
8
+ import { HandbookThreadMessageInput, HandbookThreadMessageInterface } from "./HandbookThreadMessageInterface";
9
+
10
+ /**
11
+ * One persisted handbook turn.
12
+ *
13
+ * The getters below are the structural contract `AssistantMessageInterface`
14
+ * asks for, which is what lets `AssistantThread` render handbook messages with
15
+ * no adapter. The handbook has no token accounting, no entity references, no
16
+ * chunk citations and no approval actions, so those answer `undefined` / `[]`
17
+ * rather than being faked.
18
+ */
19
+ export class HandbookThreadMessage extends AbstractApiData implements HandbookThreadMessageInterface {
20
+ private _role?: AssistantMessageRole;
21
+ private _content?: string;
22
+ private _position?: number;
23
+ private _sources?: string[];
24
+ private _isOptimistic = false;
25
+
26
+ get role(): AssistantMessageRole {
27
+ return this._role ?? "assistant";
28
+ }
29
+
30
+ get content(): string {
31
+ return this._content ?? "";
32
+ }
33
+
34
+ get position(): number {
35
+ return this._position ?? 0;
36
+ }
37
+
38
+ get sources(): string[] {
39
+ return this._sources ?? [];
40
+ }
41
+
42
+ /** The handbook agent returns no follow-ups. */
43
+ get suggestedQuestions(): string[] {
44
+ return [];
45
+ }
46
+
47
+ /** Token accounting is not billed against a handbook thread. */
48
+ get inputTokens(): number | undefined {
49
+ return undefined;
50
+ }
51
+
52
+ get outputTokens(): number | undefined {
53
+ return undefined;
54
+ }
55
+
56
+ /** The handbook answers from pages, never from product entities. */
57
+ get references(): ApiDataInterface[] {
58
+ return [];
59
+ }
60
+
61
+ /** Cited pages travel in `sources`, not as Chunk resources. */
62
+ get citations(): (ChunkInterface & ChunkRelationshipMeta)[] {
63
+ return [];
64
+ }
65
+
66
+ /** Set only by `buildLocal`; a rehydrated message is always real. */
67
+ get isOptimistic(): boolean {
68
+ return this._isOptimistic;
69
+ }
70
+
71
+ /** There is no operator engine behind the handbook, so never an approval request. */
72
+ get messageType(): AssistantMessageType {
73
+ return "text";
74
+ }
75
+
76
+ get actionId(): string | undefined {
77
+ return undefined;
78
+ }
79
+
80
+ rehydrate(data: JsonApiHydratedDataInterface): this {
81
+ super.rehydrate(data);
82
+
83
+ const attributes = data.jsonApi.attributes ?? {};
84
+ this._role = attributes.role as AssistantMessageRole | undefined;
85
+ this._content = attributes.content;
86
+ this._position = typeof attributes.position === "number" ? attributes.position : Number(attributes.position ?? 0);
87
+ this._sources = Array.isArray(attributes.sources) ? attributes.sources : [];
88
+
89
+ return this;
90
+ }
91
+
92
+ /**
93
+ * The envelope `POST /handbookthreads/:id/handbookthreadmessages` accepts:
94
+ * only the question. Role and position are derived server-side from the
95
+ * thread, and there is no client-chosen id for the pair of messages the turn
96
+ * creates.
97
+ *
98
+ * `handbookPageId` rides the same way `question` does, through this method
99
+ * and never through `overridesJsonApiCreation`. It is written only when the
100
+ * caller supplied it: the DTO takes it as optional, and sending an explicit
101
+ * `null` would fail validation rather than mean "the whole manual".
102
+ */
103
+ createJsonApi(data: HandbookThreadMessageInput) {
104
+ return {
105
+ data: {
106
+ type: Modules.HandbookThreadMessage.name,
107
+ attributes: {
108
+ question: data.question,
109
+ ...(data.handbookPageId !== undefined ? { handbookPageId: data.handbookPageId } : {}),
110
+ },
111
+ },
112
+ included: [],
113
+ };
114
+ }
115
+
116
+ /**
117
+ * A message that exists only on the client: the optimistic user turn while
118
+ * the agent runs, and the failure notice when it does not answer. Mirrors
119
+ * `AssistantMessage.buildLocal`, which the stateless handbook surface used
120
+ * before threads existed.
121
+ */
122
+ static buildLocal(params: {
123
+ role: AssistantMessageRole;
124
+ content: string;
125
+ position: number;
126
+ isOptimistic?: boolean;
127
+ }): HandbookThreadMessage {
128
+ const message = new HandbookThreadMessage();
129
+ message.rehydrate({
130
+ jsonApi: {
131
+ id: uuidv4(),
132
+ type: Modules.HandbookThreadMessage.name,
133
+ attributes: {
134
+ role: params.role,
135
+ content: params.content,
136
+ position: params.position,
137
+ },
138
+ } as any,
139
+ included: [],
140
+ });
141
+ message._isOptimistic = params.isOptimistic ?? false;
142
+ return message;
143
+ }
144
+ }
@@ -0,0 +1,35 @@
1
+ import { AssistantMessageInterface } from "../../assistant-message/data/AssistantMessageInterface";
2
+
3
+ /**
4
+ * One turn of a persisted handbook conversation.
5
+ *
6
+ * It extends `AssistantMessageInterface` rather than restating it: the shared
7
+ * chat components (`AssistantThread` → `MessageList` → `MessageItem`) are typed
8
+ * against that interface and take their data as plain props, so a handbook
9
+ * message that satisfies the same structural contract renders through them with
10
+ * no adapter. Everything the handbook has no concept of — token counts,
11
+ * references, citations, approval actions — is answered with the empty value.
12
+ *
13
+ * `sources` is the handbook's own addition: repo-relative page paths, in the
14
+ * order retrieval returned them.
15
+ */
16
+ export interface HandbookThreadMessageInterface extends AssistantMessageInterface {
17
+ get sources(): string[];
18
+ }
19
+
20
+ /**
21
+ * The payload of one question appended to an existing thread. `id` is the
22
+ * client-generated identity every model input in this framework carries; the
23
+ * append endpoint derives role and position server-side, so the envelope the
24
+ * model builds sends only `question`.
25
+ */
26
+ export type HandbookThreadMessageInput = {
27
+ id: string;
28
+ question: string;
29
+ /**
30
+ * Scopes retrieval to one handbook page. Set only by the reader's ask sheet,
31
+ * when the reader turns the "this page only" chip on; absent everywhere else,
32
+ * and the agent then searches the whole manual.
33
+ */
34
+ handbookPageId?: string;
35
+ };
@@ -0,0 +1,30 @@
1
+ import { AbstractService, EndpointCreator, HttpMethod, Modules } from "../../../core";
2
+ import { HandbookThreadMessageInput } from "./HandbookThreadMessageInterface";
3
+
4
+ export class HandbookThreadMessageService extends AbstractService {
5
+ /**
6
+ * Appends one question to an existing thread and runs the agent turn.
7
+ *
8
+ * It returns nothing on purpose: the caller reads the turn back with
9
+ * `HandbookThreadService.findOne`, which is the one endpoint the contract
10
+ * guarantees side-loads the full message list. That keeps a single code path
11
+ * for "load the thread after a mutation" — the same one the sidebar's select
12
+ * uses — instead of two ways of arriving at the transcript.
13
+ *
14
+ * The API answers 400 when the installation has no usable AI configuration
15
+ * and 403 for a non-administrator; both reach the caller as a thrown error.
16
+ */
17
+ static async ask(params: HandbookThreadMessageInput & { threadId: string }): Promise<void> {
18
+ await this.callApi({
19
+ type: Modules.HandbookThreadMessage,
20
+ method: HttpMethod.POST,
21
+ endpoint: new EndpointCreator({
22
+ endpoint: Modules.HandbookThread,
23
+ id: params.threadId,
24
+ childEndpoint: Modules.HandbookThreadMessage,
25
+ }).generate(),
26
+ input: { id: params.id, question: params.question, handbookPageId: params.handbookPageId },
27
+ suppressGlobalError: true,
28
+ });
29
+ }
30
+ }