@metad/contracts 3.6.2 → 3.6.7

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 (287) hide show
  1. package/.babelrc +5 -0
  2. package/.eslintrc.json +18 -0
  3. package/babel.config.json +1 -0
  4. package/index.cjs.js +5 -4233
  5. package/jest.config.ts +16 -0
  6. package/package.json +2 -2
  7. package/project.json +53 -0
  8. package/src/agent/{ai-model.d.ts → ai-model.ts} +2 -2
  9. package/src/agent/graph.ts +275 -0
  10. package/src/agent/index.ts +5 -0
  11. package/src/agent/token.ts +23 -0
  12. package/src/agent/utils.spec.ts +283 -0
  13. package/src/agent/utils.ts +122 -0
  14. package/src/agent/variables.ts +66 -0
  15. package/src/ai/ai-model.model.ts +232 -0
  16. package/src/ai/ai-provider.model.ts +53 -0
  17. package/src/ai/ai.model.ts +87 -0
  18. package/src/ai/chat-message-feedback.model.ts +31 -0
  19. package/src/ai/chat-message.model.ts +277 -0
  20. package/src/ai/chat.model.ts +207 -0
  21. package/src/ai/copilot-checkpoint.model.ts +25 -0
  22. package/src/ai/copilot-example.model.ts +23 -0
  23. package/src/ai/copilot-model.model.ts +31 -0
  24. package/src/ai/copilot-organization.model.ts +21 -0
  25. package/src/ai/copilot-provider.model.ts +46 -0
  26. package/src/ai/copilot-role.model.ts +48 -0
  27. package/src/ai/copilot-store.model.ts +34 -0
  28. package/src/ai/copilot-user.model.ts +24 -0
  29. package/src/ai/copilot.model.ts +47 -0
  30. package/src/ai/{environment.model.d.ts → environment.model.ts} +12 -10
  31. package/src/ai/feature.model.ts +6 -0
  32. package/src/ai/index.ts +40 -0
  33. package/src/ai/knowledge-doc-chunk.model.ts +123 -0
  34. package/src/ai/knowledge-doc-chunk.spec.ts +47 -0
  35. package/src/ai/knowledge-doc-page.model.ts +20 -0
  36. package/src/ai/knowledge-doc.model.ts +364 -0
  37. package/src/ai/knowledge-pipeline.ts +138 -0
  38. package/src/ai/knowledge-retrieval-log.model.ts +19 -0
  39. package/src/ai/knowledgebase-task.model.ts +46 -0
  40. package/src/ai/knowledgebase.model.ts +205 -0
  41. package/src/ai/rag-web.ts +54 -0
  42. package/src/ai/rag.ts +24 -0
  43. package/src/ai/role-permissions.ts +13 -0
  44. package/src/ai/types.ts +83 -0
  45. package/src/ai/xpert-agent-execution.model.ts +115 -0
  46. package/src/ai/xpert-agent.model.ts +264 -0
  47. package/src/ai/xpert-project.model.ts +103 -0
  48. package/src/ai/xpert-table.model.ts +100 -0
  49. package/src/ai/xpert-task.model.ts +32 -0
  50. package/src/ai/xpert-template.model.ts +53 -0
  51. package/src/ai/xpert-tool-mcp.model.ts +77 -0
  52. package/src/ai/xpert-tool.model.ts +216 -0
  53. package/src/ai/xpert-toolset.model.ts +220 -0
  54. package/src/ai/xpert-workflow-task.prompt.ts +71 -0
  55. package/src/ai/xpert-workflow.model.ts +502 -0
  56. package/src/ai/xpert-workspace.model.ts +42 -0
  57. package/src/ai/xpert.model.ts +637 -0
  58. package/src/ai/xpert.utils.ts +214 -0
  59. package/src/analytics/ai.ts +79 -0
  60. package/src/analytics/approval-policy.model.ts +40 -0
  61. package/src/analytics/business-area-user.model.ts +43 -0
  62. package/src/analytics/business-area.ts +20 -0
  63. package/src/analytics/certification.model.ts +12 -0
  64. package/src/analytics/chatbi-conversation.model.ts +16 -0
  65. package/src/analytics/chatbi-model.model.ts +22 -0
  66. package/src/analytics/collection.model.ts +8 -0
  67. package/src/analytics/comment.model.ts +14 -0
  68. package/src/analytics/data-source-type.ts +19 -0
  69. package/src/analytics/data-source.ts +72 -0
  70. package/src/analytics/favorite.ts +18 -0
  71. package/src/analytics/features.ts +19 -0
  72. package/src/analytics/feed-model.ts +15 -0
  73. package/src/analytics/gateway.ts +31 -0
  74. package/src/analytics/index.ts +39 -0
  75. package/src/analytics/indicator-app.ts +49 -0
  76. package/src/analytics/{indicator-market.d.ts → indicator-market.ts} +6 -2
  77. package/src/analytics/indicator.ts +143 -0
  78. package/src/analytics/model-query-log.model.ts +45 -0
  79. package/src/analytics/model-query.ts +22 -0
  80. package/src/analytics/{notification-destination.d.ts → notification-destination.ts} +9 -5
  81. package/src/analytics/{permission-approval-user.model.d.ts → permission-approval-user.model.ts} +10 -8
  82. package/src/analytics/permission-approval.model.ts +58 -0
  83. package/src/analytics/project.model.ts +56 -0
  84. package/src/analytics/role-permissions.ts +36 -0
  85. package/src/analytics/schema.ts +300 -0
  86. package/src/analytics/screenshot.model.ts +28 -0
  87. package/src/analytics/semantic-model-entity.ts +50 -0
  88. package/src/analytics/semantic-model-member.ts +49 -0
  89. package/src/analytics/semantic-model.ts +237 -0
  90. package/src/analytics/{story-point.d.ts → story-point.ts} +11 -6
  91. package/src/analytics/story-template.model.ts +34 -0
  92. package/src/analytics/story-widget.ts +15 -0
  93. package/src/analytics/story.ts +109 -0
  94. package/src/analytics/subscription.ts +43 -0
  95. package/src/analytics/visit.model.ts +39 -0
  96. package/src/analytics/webSocket.ts +53 -0
  97. package/src/api-key.model.ts +11 -0
  98. package/src/base-entity.model.ts +34 -0
  99. package/src/contact.model.ts +45 -0
  100. package/src/core.model.ts +103 -0
  101. package/src/{country.model.d.ts → country.model.ts} +3 -2
  102. package/src/currency.model.ts +582 -0
  103. package/src/{custom-smtp.model.d.ts → custom-smtp.model.ts} +21 -17
  104. package/src/date-picker.model.ts +5 -0
  105. package/src/email-template.model.ts +50 -0
  106. package/src/email.model.ts +46 -0
  107. package/src/employee.model.ts +199 -0
  108. package/src/entity-with-members.model.ts +16 -0
  109. package/src/feature.model.ts +108 -0
  110. package/src/file-provider.ts +37 -0
  111. package/src/help-center-article.model.ts +35 -0
  112. package/src/help-center.model.ts +33 -0
  113. package/src/http-status.enum.ts +58 -0
  114. package/src/import-export.model.ts +42 -0
  115. package/src/index.ts +60 -0
  116. package/src/integration/dify.ts +31 -0
  117. package/src/integration/dingtalk.ts +16 -0
  118. package/src/integration/fastgpt.ts +32 -0
  119. package/src/integration/firecrawl.ts +46 -0
  120. package/src/integration/github.ts +149 -0
  121. package/src/integration/index.ts +15 -0
  122. package/src/integration/lark.ts +79 -0
  123. package/src/integration/wecom.ts +16 -0
  124. package/src/integration.model.ts +119 -0
  125. package/src/invite.model.ts +144 -0
  126. package/src/language.model.ts +22 -0
  127. package/src/organization-contact.model.ts +84 -0
  128. package/src/{organization-department.model.d.ts → organization-department.model.ts} +16 -10
  129. package/src/organization-language.model.ts +24 -0
  130. package/src/organization-projects.model.ts +99 -0
  131. package/src/organization-team-employee-model.ts +14 -0
  132. package/src/organization-team-model.ts +25 -0
  133. package/src/organization.model.ts +266 -0
  134. package/src/{password-reset.model.d.ts → password-reset.model.ts} +14 -10
  135. package/src/{plain-object.model.d.ts → plain-object.model.ts} +1 -1
  136. package/src/plugin.ts +15 -0
  137. package/src/role-permission.model.ts +123 -0
  138. package/src/role.model.ts +31 -0
  139. package/src/schedule.ts +71 -0
  140. package/src/secret-token.model.ts +15 -0
  141. package/src/seed.model.ts +50 -0
  142. package/src/storage-file.model.ts +46 -0
  143. package/src/tag-entity.model.ts +23 -0
  144. package/src/tenant.model.ts +52 -0
  145. package/src/tools/index.ts +1 -0
  146. package/src/tools/sandbox.ts +5 -0
  147. package/src/translation.model.ts +37 -0
  148. package/src/types.ts +277 -0
  149. package/src/user-organization.model.ts +31 -0
  150. package/src/user.model.ts +151 -0
  151. package/src/visibility.model.ts +6 -0
  152. package/tsconfig.json +22 -0
  153. package/tsconfig.lib.json +10 -0
  154. package/tsconfig.spec.json +9 -0
  155. package/index.cjs.d.ts +0 -1
  156. package/index.esm.d.ts +0 -1
  157. package/index.esm.js +0 -4129
  158. package/src/agent/graph.d.ts +0 -148
  159. package/src/agent/index.d.ts +0 -5
  160. package/src/agent/token.d.ts +0 -21
  161. package/src/agent/utils.d.ts +0 -19
  162. package/src/agent/variables.d.ts +0 -14
  163. package/src/ai/ai-model.model.d.ts +0 -198
  164. package/src/ai/ai-provider.model.d.ts +0 -43
  165. package/src/ai/ai.model.d.ts +0 -82
  166. package/src/ai/chat-message-feedback.model.d.ts +0 -24
  167. package/src/ai/chat-message.model.d.ts +0 -222
  168. package/src/ai/chat.model.d.ts +0 -189
  169. package/src/ai/copilot-checkpoint.model.d.ts +0 -23
  170. package/src/ai/copilot-example.model.d.ts +0 -19
  171. package/src/ai/copilot-model.model.d.ts +0 -26
  172. package/src/ai/copilot-organization.model.d.ts +0 -17
  173. package/src/ai/copilot-provider.model.d.ts +0 -31
  174. package/src/ai/copilot-role.model.d.ts +0 -43
  175. package/src/ai/copilot-store.model.d.ts +0 -29
  176. package/src/ai/copilot-user.model.d.ts +0 -20
  177. package/src/ai/copilot.model.d.ts +0 -36
  178. package/src/ai/feature.model.d.ts +0 -6
  179. package/src/ai/index.d.ts +0 -39
  180. package/src/ai/knowledge-doc-chunk.model.d.ts +0 -60
  181. package/src/ai/knowledge-doc-page.model.d.ts +0 -15
  182. package/src/ai/knowledge-doc.model.d.ts +0 -192
  183. package/src/ai/knowledge-pipeline.d.ts +0 -107
  184. package/src/ai/knowledge-retrieval-log.model.d.ts +0 -10
  185. package/src/ai/knowledgebase-task.model.d.ts +0 -39
  186. package/src/ai/knowledgebase.model.d.ts +0 -162
  187. package/src/ai/rag-web.d.ts +0 -26
  188. package/src/ai/rag.d.ts +0 -24
  189. package/src/ai/role-permissions.d.ts +0 -13
  190. package/src/ai/types.d.ts +0 -39
  191. package/src/ai/xpert-agent-execution.model.d.ts +0 -96
  192. package/src/ai/xpert-agent.model.d.ts +0 -192
  193. package/src/ai/xpert-project.model.d.ts +0 -86
  194. package/src/ai/xpert-task.model.d.ts +0 -26
  195. package/src/ai/xpert-template.model.d.ts +0 -47
  196. package/src/ai/xpert-tool-mcp.model.d.ts +0 -67
  197. package/src/ai/xpert-tool.model.d.ts +0 -147
  198. package/src/ai/xpert-toolset.model.d.ts +0 -200
  199. package/src/ai/xpert-workflow-task.prompt.d.ts +0 -2
  200. package/src/ai/xpert-workflow.model.d.ts +0 -366
  201. package/src/ai/xpert-workspace.model.d.ts +0 -32
  202. package/src/ai/xpert.model.d.ts +0 -529
  203. package/src/ai/xpert.utils.d.ts +0 -80
  204. package/src/analytics/ai.d.ts +0 -73
  205. package/src/analytics/approval-policy.model.d.ts +0 -30
  206. package/src/analytics/business-area-user.model.d.ts +0 -35
  207. package/src/analytics/business-area.d.ts +0 -17
  208. package/src/analytics/certification.model.d.ts +0 -11
  209. package/src/analytics/chatbi-conversation.model.d.ts +0 -15
  210. package/src/analytics/chatbi-model.model.d.ts +0 -18
  211. package/src/analytics/collection.model.d.ts +0 -6
  212. package/src/analytics/comment.model.d.ts +0 -10
  213. package/src/analytics/data-source-type.d.ts +0 -16
  214. package/src/analytics/data-source.d.ts +0 -62
  215. package/src/analytics/favorite.d.ts +0 -14
  216. package/src/analytics/features.d.ts +0 -19
  217. package/src/analytics/feed-model.d.ts +0 -13
  218. package/src/analytics/gateway.d.ts +0 -27
  219. package/src/analytics/index.d.ts +0 -38
  220. package/src/analytics/indicator-app.d.ts +0 -44
  221. package/src/analytics/indicator.d.ts +0 -106
  222. package/src/analytics/model-query-log.model.d.ts +0 -41
  223. package/src/analytics/model-query.d.ts +0 -21
  224. package/src/analytics/permission-approval.model.d.ts +0 -47
  225. package/src/analytics/project.model.d.ts +0 -50
  226. package/src/analytics/role-permissions.d.ts +0 -25
  227. package/src/analytics/schema.d.ts +0 -262
  228. package/src/analytics/screenshot.model.d.ts +0 -25
  229. package/src/analytics/semantic-model-entity.d.ts +0 -37
  230. package/src/analytics/semantic-model-member.d.ts +0 -24
  231. package/src/analytics/semantic-model.d.ts +0 -175
  232. package/src/analytics/story-template.model.d.ts +0 -28
  233. package/src/analytics/story-widget.d.ts +0 -12
  234. package/src/analytics/story.d.ts +0 -93
  235. package/src/analytics/subscription.d.ts +0 -38
  236. package/src/analytics/visit.model.d.ts +0 -34
  237. package/src/analytics/webSocket.d.ts +0 -48
  238. package/src/api-key.model.d.ts +0 -10
  239. package/src/base-entity.model.d.ts +0 -27
  240. package/src/contact.model.d.ts +0 -41
  241. package/src/core.model.d.ts +0 -97
  242. package/src/currency.model.d.ts +0 -579
  243. package/src/date-picker.model.d.ts +0 -5
  244. package/src/email-template.model.d.ts +0 -39
  245. package/src/email.model.d.ts +0 -36
  246. package/src/employee.model.d.ts +0 -128
  247. package/src/entity-with-members.model.d.ts +0 -13
  248. package/src/feature.model.d.ts +0 -93
  249. package/src/file-provider.d.ts +0 -33
  250. package/src/help-center-article.model.d.ts +0 -0
  251. package/src/help-center.model.d.ts +0 -0
  252. package/src/http-status.enum.d.ts +0 -57
  253. package/src/import-export.model.d.ts +0 -36
  254. package/src/index.d.ts +0 -48
  255. package/src/integration/dify.d.ts +0 -0
  256. package/src/integration/dingtalk.d.ts +0 -2
  257. package/src/integration/fastgpt.d.ts +0 -0
  258. package/src/integration/firecrawl.d.ts +0 -2
  259. package/src/integration/github.d.ts +0 -69
  260. package/src/integration/index.d.ts +0 -4
  261. package/src/integration/lark.d.ts +0 -11
  262. package/src/integration/wecom.d.ts +0 -2
  263. package/src/integration.model.d.ts +0 -90
  264. package/src/invite.model.d.ts +0 -123
  265. package/src/language.model.d.ts +0 -19
  266. package/src/organization-contact.model.d.ts +0 -73
  267. package/src/organization-language.model.d.ts +0 -18
  268. package/src/organization-projects.model.d.ts +0 -80
  269. package/src/organization-team-employee-model.d.ts +0 -12
  270. package/src/organization-team-model.d.ts +0 -19
  271. package/src/organization.model.d.ts +0 -239
  272. package/src/plugin.d.ts +0 -13
  273. package/src/role-permission.model.d.ts +0 -61
  274. package/src/role.model.d.ts +0 -26
  275. package/src/schedule.d.ts +0 -27
  276. package/src/secret-token.model.d.ts +0 -11
  277. package/src/seed.model.d.ts +0 -37
  278. package/src/storage-file.model.d.ts +0 -40
  279. package/src/tag-entity.model.d.ts +0 -20
  280. package/src/tenant.model.d.ts +0 -40
  281. package/src/tools/index.d.ts +0 -1
  282. package/src/tools/sandbox.d.ts +0 -5
  283. package/src/translation.model.d.ts +0 -26
  284. package/src/types.d.ts +0 -241
  285. package/src/user-organization.model.d.ts +0 -24
  286. package/src/user.model.d.ts +0 -131
  287. package/src/visibility.model.d.ts +0 -6
@@ -0,0 +1,24 @@
1
+ import { IBasePerTenantAndOrganizationEntityModel } from '../base-entity.model'
2
+ import { IOrganization } from '../organization.model'
3
+ import { IUser } from '../user.model'
4
+ import { AiProvider } from './ai.model'
5
+ import { ICopilot, TCopilotTokenUsage } from './copilot.model'
6
+
7
+ /**
8
+ *
9
+ */
10
+ export interface ICopilotUser extends IBasePerTenantAndOrganizationEntityModel, TCopilotTokenUsage {
11
+ orgId?: string
12
+ org?: IOrganization
13
+ copilotId?: string
14
+ copilot?: ICopilot
15
+ userId?: string
16
+ user?: IUser
17
+ // Associated AI Model provider
18
+ provider?: AiProvider | string
19
+ model?: string
20
+
21
+ // Total tokens used historically
22
+ tokenTotalUsed?: number
23
+ priceTotalUsed?: number
24
+ }
@@ -0,0 +1,47 @@
1
+ import { IBasePerTenantAndOrganizationEntityModel } from '../base-entity.model'
2
+ import { ICopilotModel } from './copilot-model.model'
3
+ import { ICopilotProvider } from './copilot-provider.model'
4
+
5
+ export interface ICopilot extends IBasePerTenantAndOrganizationEntityModel {
6
+ name?: string
7
+ role: AiProviderRole
8
+ enabled?: boolean
9
+
10
+ showTokenizer?: boolean
11
+ /**
12
+ * Balance of Token
13
+ */
14
+ tokenBalance?: number
15
+
16
+ /**
17
+ * Details config for openai api
18
+ */
19
+ options?: any
20
+
21
+ modelProvider?: ICopilotProvider
22
+ copilotModel?: ICopilotModel
23
+
24
+ // Temporary properties
25
+ usage?: TCopilotTokenUsage
26
+ }
27
+
28
+ /**
29
+ * The order of priority is: `Embedding`, `Secondary`, `Primary`
30
+ */
31
+ export enum AiProviderRole {
32
+ Primary = 'primary',
33
+ Secondary = 'secondary',
34
+ Embedding = 'embedding',
35
+ Reasoning = 'reasoning'
36
+ }
37
+
38
+ export type TCopilotTokenUsage = {
39
+ // Token limit for the current period
40
+ tokenLimit?: number
41
+ priceLimit?: number
42
+ // Tokens used in the current period
43
+ tokenUsed?: number
44
+ priceUsed?: number
45
+
46
+ currency?: string
47
+ }
@@ -1,16 +1,18 @@
1
- import { IBasePerWorkspaceEntityModel } from "./xpert-workspace.model";
1
+ import { IBasePerWorkspaceEntityModel } from "./xpert-workspace.model"
2
+
2
3
  export type TEnvironmentVariable = {
3
- name: string;
4
- value: string;
5
- type: 'default' | 'secret';
4
+ name: string
5
+ value: string
6
+ type: 'default' | 'secret'
6
7
  /**
7
8
  * Has owner, private variable
8
9
  */
9
- owner?: string;
10
- };
10
+ owner?: string
11
+ }
12
+
11
13
  export interface IEnvironment extends IBasePerWorkspaceEntityModel {
12
- name: string;
13
- isDefault?: boolean;
14
- isArchived?: boolean;
15
- variables: TEnvironmentVariable[];
14
+ name: string
15
+ isDefault?: boolean
16
+ isArchived?: boolean
17
+ variables: TEnvironmentVariable[]
16
18
  }
@@ -0,0 +1,6 @@
1
+ export enum AiFeatureEnum {
2
+ FEATURE_COPILOT = 'FEATURE_COPILOT',
3
+ FEATURE_COPILOT_KNOWLEDGEBASE = 'FEATURE_COPILOT_KNOWLEDGEBASE',
4
+ FEATURE_COPILOT_CHAT = 'FEATURE_COPILOT_CHAT',
5
+ FEATURE_XPERT = 'FEATURE_XPERT',
6
+ }
@@ -0,0 +1,40 @@
1
+ export * from './ai-model.model'
2
+ export * from './ai.model'
3
+ export * from './chat.model'
4
+ export * from './chat-message.model'
5
+ export * from './chat-message-feedback.model'
6
+ export * from './copilot-checkpoint.model'
7
+ export * from './copilot-example.model'
8
+ export * from './copilot-model.model'
9
+ export * from './copilot-organization.model'
10
+ export * from './copilot-provider.model'
11
+ export * from './copilot-store.model'
12
+ export * from './copilot-user.model'
13
+ export * from './copilot.model'
14
+ export * from './feature.model'
15
+ export * from './rag'
16
+ export * from './rag-web'
17
+ export * from './knowledgebase.model'
18
+ export * from './knowledge-doc.model'
19
+ export * from './knowledge-doc-page.model'
20
+ export * from './role-permissions'
21
+ export * from './xpert-agent-execution.model'
22
+ export * from './xpert-agent.model'
23
+ export * from './xpert-tool.model'
24
+ export * from './xpert-toolset.model'
25
+ export * from './xpert-workspace.model'
26
+ export * from './xpert-table.model'
27
+ export * from './xpert.model'
28
+ export * from './xpert.utils'
29
+ export * from './types'
30
+ export * from './xpert-template.model'
31
+ export * from './xpert-task.model'
32
+ export * from './xpert-workflow.model'
33
+ export * from './xpert-workflow-task.prompt'
34
+ export * from './xpert-tool-mcp.model'
35
+ export * from './xpert-project.model'
36
+ export * from './environment.model'
37
+ export * from './knowledgebase-task.model'
38
+ export * from './knowledge-pipeline'
39
+ export * from './knowledge-retrieval-log.model'
40
+ export * from './knowledge-doc-chunk.model'
@@ -0,0 +1,123 @@
1
+ import { DocumentInterface } from '@langchain/core/documents'
2
+ import { IBasePerTenantAndOrganizationEntityModel } from '../base-entity.model'
3
+ import { IKnowledgeDocument } from './knowledge-doc.model'
4
+ import { IKnowledgebase } from './knowledgebase.model'
5
+
6
+ export type TDocumentAsset = {
7
+ type: 'image' | 'video' | 'audio' | 'file'
8
+ url: string // remote url
9
+ filePath: string // local path
10
+ }
11
+
12
+ export interface IDocChunkMetadata {
13
+ chunkId: string
14
+ parentId?: string | null
15
+ children?: DocumentInterface<IDocChunkMetadata>[]
16
+
17
+ knowledgeId?: string
18
+ chunkIndex?: number // Index within the document or parent chunk
19
+ enabled?: boolean
20
+
21
+ /**
22
+ * Default to 'text'. Indicates the original media type of the chunk.
23
+ * @default text
24
+ */
25
+ mediaType?: 'text' | 'image' | 'video' | 'audio' // Media type of the chunk
26
+ /**
27
+ * Associated assets like images, videos, etc.
28
+ */
29
+ assets?: TDocumentAsset[]
30
+ /**
31
+ * Whether the chunk is represented as a vector in the vector store
32
+ */
33
+ isVector?: boolean
34
+ score?: number
35
+ relevanceScore?: number
36
+
37
+ // tokens
38
+ tokens?: number
39
+
40
+ [key: string]: any
41
+ }
42
+
43
+
44
+ /**
45
+ * Segmented chunk of a knowledge document
46
+ */
47
+ export interface IKnowledgeDocumentChunk<Metadata extends IDocChunkMetadata = any>
48
+ extends DocumentInterface<Metadata>,
49
+ IBasePerTenantAndOrganizationEntityModel {
50
+ documentId?: string
51
+ document?: IKnowledgeDocument
52
+ knowledgebaseId?: string
53
+ knowledgebase?: IKnowledgebase
54
+
55
+ parent?: IKnowledgeDocumentChunk<Metadata>
56
+ children?: IKnowledgeDocumentChunk<Metadata>[]
57
+ }
58
+
59
+
60
+ /**
61
+ * Build a hierarchical tree structure from a flat list of DocumentInterface objects,
62
+ * and
63
+ *
64
+ * @template Metadata - Type of metadata, defaults to IDocChunkMetadata
65
+ * @param documents - A flat array of DocumentInterface objects
66
+ * @returns the hierarchical tree (root-level DocumentInterface[])
67
+ */
68
+ export function buildChunkTree<Metadata extends IDocChunkMetadata = IDocChunkMetadata>(
69
+ documents: DocumentInterface<Metadata>[]
70
+ ): DocumentInterface<Metadata>[] {
71
+ if (!documents || documents.length === 0) return []
72
+
73
+ // Step 1. Build a lookup map for quick access by chunkId
74
+ const map = new Map<string, DocumentInterface<Metadata>>()
75
+ for (const doc of documents) {
76
+ if (doc.metadata?.chunkId) {
77
+ map.set(doc.metadata.chunkId, { ...doc, metadata: { ...doc.metadata, children: [] } })
78
+ }
79
+ }
80
+
81
+ // Step 2. Organize nodes into tree
82
+ const roots: DocumentInterface<Metadata>[] = []
83
+
84
+ for (const doc of map.values()) {
85
+ const parentId = doc.metadata.parentId
86
+ if (parentId && map.has(parentId)) {
87
+ const parent = map.get(parentId)
88
+ parent.metadata.children = parent.metadata.children || []
89
+ parent.metadata.children.push(doc)
90
+ } else {
91
+ roots.push(doc)
92
+ }
93
+ }
94
+
95
+ return roots
96
+ }
97
+
98
+ /**
99
+ * Find all leaf nodes (nodes without children).
100
+ *
101
+ * @param roots
102
+ * @returns
103
+ */
104
+ export function collectTreeLeaves(roots: IKnowledgeDocumentChunk[]) {
105
+ const leaves: IKnowledgeDocumentChunk[] = []
106
+
107
+ const collectLeaves = (node: IKnowledgeDocumentChunk) => {
108
+ const children = node.metadata.children
109
+ if (!children || children.length === 0) {
110
+ leaves.push(node)
111
+ } else {
112
+ for (const child of children) {
113
+ collectLeaves(child as IKnowledgeDocumentChunk)
114
+ }
115
+ }
116
+ }
117
+
118
+ for (const root of roots) {
119
+ collectLeaves(root)
120
+ }
121
+
122
+ return leaves
123
+ }
@@ -0,0 +1,47 @@
1
+ import { DocumentInterface } from "@langchain/core/documents"
2
+ import { buildChunkTree, IDocChunkMetadata } from "./knowledge-doc-chunk.model"
3
+
4
+ describe("buildChunkTree", () => {
5
+ function makeDoc(
6
+ chunkId: string,
7
+ parentId?: string,
8
+ extra?: Partial<IDocChunkMetadata>
9
+ ): DocumentInterface<IDocChunkMetadata> {
10
+ return {
11
+ pageContent: `Content ${chunkId}`,
12
+ metadata: {
13
+ chunkId,
14
+ parentId,
15
+ ...extra,
16
+ } as IDocChunkMetadata,
17
+ }
18
+ }
19
+
20
+ it("does not mutate input documents", () => {
21
+ const chunks: DocumentInterface<IDocChunkMetadata>[] = [
22
+ {
23
+ pageContent: 'Parent chunk',
24
+ metadata: { chunkId: '1' },
25
+ },
26
+ {
27
+ pageContent: 'Parent chunk',
28
+ metadata: { chunkId: '2' },
29
+ },
30
+ {
31
+ pageContent: 'Child 1',
32
+ metadata: { chunkId: '1-1', parentId: '1', type: 'child' },
33
+ },
34
+ {
35
+ pageContent: 'Child 2',
36
+ metadata: { chunkId: '1-2', parentId: '1', type: 'child' },
37
+ },
38
+ {
39
+ pageContent: 'Grandchild of 1-2',
40
+ metadata: { chunkId: '1-2-1', parentId: '1-2', type: 'child' },
41
+ },
42
+ ]
43
+
44
+ const tree = buildChunkTree(chunks)
45
+ console.log('Tree:', JSON.stringify(tree, null, 2))
46
+ })
47
+ })
@@ -0,0 +1,20 @@
1
+ import { Document } from '@langchain/core/documents'
2
+ import { IBasePerTenantAndOrganizationEntityModel } from '../base-entity.model'
3
+ import { IKnowledgeDocument } from './knowledge-doc.model'
4
+ import { DocumentMetadata, IKnowledgebase } from './knowledgebase.model'
5
+
6
+
7
+ /**
8
+ * Segmented page of a knowledge document
9
+ */
10
+ export interface IKnowledgeDocumentPage<Metadata extends DocumentMetadata = DocumentMetadata>
11
+ extends Document,
12
+ IBasePerTenantAndOrganizationEntityModel {
13
+ documentId?: string
14
+ document?: IKnowledgeDocument
15
+ knowledgebaseId?: string
16
+ knowledgebase?: IKnowledgebase
17
+
18
+ metadata: Metadata
19
+ pageContent: string
20
+ }