@neuralinnovations/dataisland-sdk 0.0.1-dev1 → 0.0.1-dev11

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 (276) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +174 -1
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +3 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/package.json +98 -0
  8. package/dist/src/appBuilder.d.ts +44 -0
  9. package/dist/src/appBuilder.d.ts.map +1 -0
  10. package/dist/src/appBuilder.js +10 -0
  11. package/dist/src/appBuilder.js.map +1 -0
  12. package/dist/src/commands/startCommandHandler.d.ts +7 -0
  13. package/dist/src/commands/startCommandHandler.d.ts.map +1 -0
  14. package/dist/src/commands/startCommandHandler.js +17 -0
  15. package/dist/src/commands/startCommandHandler.js.map +1 -0
  16. package/dist/src/context.d.ts +23 -0
  17. package/dist/src/context.d.ts.map +1 -0
  18. package/dist/src/context.js +31 -0
  19. package/dist/src/context.js.map +1 -0
  20. package/dist/src/credentials.d.ts +28 -0
  21. package/dist/src/credentials.d.ts.map +1 -0
  22. package/dist/src/credentials.js +70 -0
  23. package/dist/src/credentials.js.map +1 -0
  24. package/dist/src/dataIslandApp.d.ts +50 -0
  25. package/dist/src/dataIslandApp.d.ts.map +1 -0
  26. package/dist/src/dataIslandApp.js +10 -0
  27. package/dist/src/dataIslandApp.js.map +1 -0
  28. package/dist/src/disposable.d.ts +84 -0
  29. package/dist/src/disposable.d.ts.map +1 -0
  30. package/dist/src/disposable.js +140 -0
  31. package/dist/src/disposable.js.map +1 -0
  32. package/dist/src/dto/accessGroupResponse.d.ts +27 -0
  33. package/dist/src/dto/accessGroupResponse.d.ts.map +1 -0
  34. package/dist/src/dto/accessGroupResponse.js +3 -0
  35. package/dist/src/dto/accessGroupResponse.js.map +1 -0
  36. package/dist/src/dto/chatResponse.d.ts +78 -0
  37. package/dist/src/dto/chatResponse.d.ts.map +1 -0
  38. package/dist/src/dto/chatResponse.js +45 -0
  39. package/dist/src/dto/chatResponse.js.map +1 -0
  40. package/dist/src/dto/userInfoResponse.d.ts +40 -0
  41. package/dist/src/dto/userInfoResponse.d.ts.map +1 -0
  42. package/dist/src/dto/userInfoResponse.js +3 -0
  43. package/dist/src/dto/userInfoResponse.js.map +1 -0
  44. package/dist/src/dto/workspacesResponse.d.ts +43 -0
  45. package/dist/src/dto/workspacesResponse.d.ts.map +1 -0
  46. package/dist/src/dto/workspacesResponse.js +3 -0
  47. package/dist/src/dto/workspacesResponse.js.map +1 -0
  48. package/dist/src/events.d.ts +17 -0
  49. package/dist/src/events.d.ts.map +1 -0
  50. package/dist/src/events.js +52 -0
  51. package/dist/src/events.js.map +1 -0
  52. package/dist/src/index.d.ts +56 -0
  53. package/dist/src/index.d.ts.map +1 -0
  54. package/dist/src/index.js +89 -0
  55. package/dist/src/index.js.map +1 -0
  56. package/dist/src/internal/app.impl.d.ts +28 -0
  57. package/dist/src/internal/app.impl.d.ts.map +1 -0
  58. package/dist/src/internal/app.impl.js +151 -0
  59. package/dist/src/internal/app.impl.js.map +1 -0
  60. package/dist/src/internal/appBuilder.impl.d.ts +27 -0
  61. package/dist/src/internal/appBuilder.impl.d.ts.map +1 -0
  62. package/dist/src/internal/appBuilder.impl.js +71 -0
  63. package/dist/src/internal/appBuilder.impl.js.map +1 -0
  64. package/dist/src/internal/createApp.impl.d.ts +4 -0
  65. package/dist/src/internal/createApp.impl.d.ts.map +1 -0
  66. package/dist/src/internal/createApp.impl.js +11 -0
  67. package/dist/src/internal/createApp.impl.js.map +1 -0
  68. package/dist/src/internal/registry.d.ts +22 -0
  69. package/dist/src/internal/registry.d.ts.map +1 -0
  70. package/dist/src/internal/registry.js +69 -0
  71. package/dist/src/internal/registry.js.map +1 -0
  72. package/dist/src/middleware.d.ts +5 -0
  73. package/dist/src/middleware.d.ts.map +1 -0
  74. package/dist/src/middleware.js +3 -0
  75. package/dist/src/middleware.js.map +1 -0
  76. package/dist/src/services/commandService.d.ts +18 -0
  77. package/dist/src/services/commandService.d.ts.map +1 -0
  78. package/dist/src/services/commandService.js +40 -0
  79. package/dist/src/services/commandService.js.map +1 -0
  80. package/dist/src/services/credentialService.d.ts +9 -0
  81. package/dist/src/services/credentialService.d.ts.map +1 -0
  82. package/dist/src/services/credentialService.js +26 -0
  83. package/dist/src/services/credentialService.js.map +1 -0
  84. package/dist/src/services/middlewareService.d.ts +9 -0
  85. package/dist/src/services/middlewareService.d.ts.map +1 -0
  86. package/dist/src/services/middlewareService.js +34 -0
  87. package/dist/src/services/middlewareService.js.map +1 -0
  88. package/dist/src/services/organizationService.d.ts +10 -0
  89. package/dist/src/services/organizationService.d.ts.map +1 -0
  90. package/dist/src/services/organizationService.js +19 -0
  91. package/dist/src/services/organizationService.js.map +1 -0
  92. package/dist/src/services/requestBuilder.d.ts +17 -0
  93. package/dist/src/services/requestBuilder.d.ts.map +1 -0
  94. package/dist/src/services/requestBuilder.js +104 -0
  95. package/dist/src/services/requestBuilder.js.map +1 -0
  96. package/dist/src/services/responseUtils.d.ts +6 -0
  97. package/dist/src/services/responseUtils.d.ts.map +1 -0
  98. package/dist/src/services/responseUtils.js +32 -0
  99. package/dist/src/services/responseUtils.js.map +1 -0
  100. package/dist/src/services/rpcService.d.ts +77 -0
  101. package/dist/src/services/rpcService.d.ts.map +1 -0
  102. package/dist/src/services/rpcService.js +125 -0
  103. package/dist/src/services/rpcService.js.map +1 -0
  104. package/dist/src/services/service.d.ts +21 -0
  105. package/dist/src/services/service.d.ts.map +1 -0
  106. package/dist/src/services/service.js +41 -0
  107. package/dist/src/services/service.js.map +1 -0
  108. package/dist/src/services/userProfileService.d.ts +8 -0
  109. package/dist/src/services/userProfileService.d.ts.map +1 -0
  110. package/dist/src/services/userProfileService.js +32 -0
  111. package/dist/src/services/userProfileService.js.map +1 -0
  112. package/dist/src/storages/chats/answer.d.ts +30 -0
  113. package/dist/src/storages/chats/answer.d.ts.map +1 -0
  114. package/dist/src/storages/chats/answer.impl.d.ts +23 -0
  115. package/dist/src/storages/chats/answer.impl.d.ts.map +1 -0
  116. package/dist/src/storages/chats/answer.impl.js +110 -0
  117. package/dist/src/storages/chats/answer.impl.js.map +1 -0
  118. package/dist/src/storages/chats/answer.js +7 -0
  119. package/dist/src/storages/chats/answer.js.map +1 -0
  120. package/dist/src/storages/chats/chat.d.ts +30 -0
  121. package/dist/src/storages/chats/chat.d.ts.map +1 -0
  122. package/dist/src/storages/chats/chat.impl.d.ts +22 -0
  123. package/dist/src/storages/chats/chat.impl.d.ts.map +1 -0
  124. package/dist/src/storages/chats/chat.impl.js +67 -0
  125. package/dist/src/storages/chats/chat.impl.js.map +1 -0
  126. package/dist/src/storages/chats/chat.js +12 -0
  127. package/dist/src/storages/chats/chat.js.map +1 -0
  128. package/dist/src/storages/chats/chats.d.ts +40 -0
  129. package/dist/src/storages/chats/chats.d.ts.map +1 -0
  130. package/dist/src/storages/chats/chats.impl.d.ts +18 -0
  131. package/dist/src/storages/chats/chats.impl.d.ts.map +1 -0
  132. package/dist/src/storages/chats/chats.impl.js +102 -0
  133. package/dist/src/storages/chats/chats.impl.js.map +1 -0
  134. package/dist/src/storages/chats/chats.js +16 -0
  135. package/dist/src/storages/chats/chats.js.map +1 -0
  136. package/dist/src/storages/files/file.d.ts +24 -0
  137. package/dist/src/storages/files/file.d.ts.map +1 -0
  138. package/dist/src/storages/files/file.impl.d.ts +18 -0
  139. package/dist/src/storages/files/file.impl.d.ts.map +1 -0
  140. package/dist/src/storages/files/file.impl.js +52 -0
  141. package/dist/src/storages/files/file.impl.js.map +1 -0
  142. package/dist/src/storages/files/file.js +10 -0
  143. package/dist/src/storages/files/file.js.map +1 -0
  144. package/dist/src/storages/files/files.d.ts +33 -0
  145. package/dist/src/storages/files/files.d.ts.map +1 -0
  146. package/dist/src/storages/files/files.impl.d.ts +33 -0
  147. package/dist/src/storages/files/files.impl.d.ts.map +1 -0
  148. package/dist/src/storages/files/files.impl.js +159 -0
  149. package/dist/src/storages/files/files.impl.js.map +1 -0
  150. package/dist/src/storages/files/files.js +19 -0
  151. package/dist/src/storages/files/files.js.map +1 -0
  152. package/dist/src/storages/files/filesPage.d.ts +23 -0
  153. package/dist/src/storages/files/filesPage.d.ts.map +1 -0
  154. package/dist/src/storages/files/filesPage.js +10 -0
  155. package/dist/src/storages/files/filesPage.js.map +1 -0
  156. package/dist/src/storages/groups/groups.d.ts +87 -0
  157. package/dist/src/storages/groups/groups.d.ts.map +1 -0
  158. package/dist/src/storages/groups/groups.impl.d.ts +55 -0
  159. package/dist/src/storages/groups/groups.impl.d.ts.map +1 -0
  160. package/dist/src/storages/groups/groups.impl.js +245 -0
  161. package/dist/src/storages/groups/groups.impl.js.map +1 -0
  162. package/dist/src/storages/groups/groups.js +26 -0
  163. package/dist/src/storages/groups/groups.js.map +1 -0
  164. package/dist/src/storages/organizations/organization.d.ts +38 -0
  165. package/dist/src/storages/organizations/organization.d.ts.map +1 -0
  166. package/dist/src/storages/organizations/organization.impl.d.ts +30 -0
  167. package/dist/src/storages/organizations/organization.impl.d.ts.map +1 -0
  168. package/dist/src/storages/organizations/organization.impl.js +71 -0
  169. package/dist/src/storages/organizations/organization.impl.js.map +1 -0
  170. package/dist/src/storages/organizations/organization.js +10 -0
  171. package/dist/src/storages/organizations/organization.js.map +1 -0
  172. package/dist/src/storages/organizations/organizations.d.ts +46 -0
  173. package/dist/src/storages/organizations/organizations.d.ts.map +1 -0
  174. package/dist/src/storages/organizations/organizations.impl.d.ts +38 -0
  175. package/dist/src/storages/organizations/organizations.impl.d.ts.map +1 -0
  176. package/dist/src/storages/organizations/organizations.impl.js +151 -0
  177. package/dist/src/storages/organizations/organizations.impl.js.map +1 -0
  178. package/dist/src/storages/organizations/organizations.js +20 -0
  179. package/dist/src/storages/organizations/organizations.js.map +1 -0
  180. package/dist/src/storages/user/userProfile.d.ts +32 -0
  181. package/dist/src/storages/user/userProfile.d.ts.map +1 -0
  182. package/dist/src/storages/user/userProfile.impl.d.ts +13 -0
  183. package/dist/src/storages/user/userProfile.impl.d.ts.map +1 -0
  184. package/dist/src/storages/user/userProfile.impl.js +51 -0
  185. package/dist/src/storages/user/userProfile.impl.js.map +1 -0
  186. package/dist/src/storages/user/userProfile.js +12 -0
  187. package/dist/src/storages/user/userProfile.js.map +1 -0
  188. package/dist/src/storages/workspaces/workspace.d.ts +40 -0
  189. package/dist/src/storages/workspaces/workspace.d.ts.map +1 -0
  190. package/dist/src/storages/workspaces/workspace.impl.d.ts +22 -0
  191. package/dist/src/storages/workspaces/workspace.impl.d.ts.map +1 -0
  192. package/dist/src/storages/workspaces/workspace.impl.js +87 -0
  193. package/dist/src/storages/workspaces/workspace.impl.js.map +1 -0
  194. package/dist/src/storages/workspaces/workspace.js +18 -0
  195. package/dist/src/storages/workspaces/workspace.js.map +1 -0
  196. package/dist/src/storages/workspaces/workspaces.d.ts +47 -0
  197. package/dist/src/storages/workspaces/workspaces.d.ts.map +1 -0
  198. package/dist/src/storages/workspaces/workspaces.impl.d.ts +33 -0
  199. package/dist/src/storages/workspaces/workspaces.impl.d.ts.map +1 -0
  200. package/dist/src/storages/workspaces/workspaces.impl.js +157 -0
  201. package/dist/src/storages/workspaces/workspaces.impl.js.map +1 -0
  202. package/dist/src/storages/workspaces/workspaces.js +19 -0
  203. package/dist/src/storages/workspaces/workspaces.js.map +1 -0
  204. package/dist/src/unitTest.d.ts +12 -0
  205. package/dist/src/unitTest.d.ts.map +1 -0
  206. package/dist/src/unitTest.js +44 -0
  207. package/dist/src/unitTest.js.map +1 -0
  208. package/index.d.ts +1 -0
  209. package/index.js +1 -0
  210. package/package.json +41 -3
  211. package/src/appBuilder.ts +24 -5
  212. package/src/commands/startCommandHandler.ts +14 -0
  213. package/src/context.ts +32 -0
  214. package/src/credentials.ts +31 -9
  215. package/src/dataIslandApp.ts +59 -0
  216. package/src/disposable.ts +4 -5
  217. package/src/dto/accessGroupResponse.ts +35 -0
  218. package/src/dto/chatResponse.ts +103 -0
  219. package/src/dto/userInfoResponse.ts +47 -0
  220. package/src/dto/workspacesResponse.ts +49 -0
  221. package/src/events.ts +13 -17
  222. package/src/index.ts +44 -18
  223. package/src/internal/app.impl.ts +97 -32
  224. package/src/internal/appBuilder.impl.ts +39 -12
  225. package/src/internal/createApp.impl.ts +5 -5
  226. package/src/middleware.ts +1 -1
  227. package/src/services/commandService.ts +44 -0
  228. package/src/services/credentialService.ts +3 -3
  229. package/src/services/middlewareService.ts +8 -6
  230. package/src/services/organizationService.ts +28 -0
  231. package/src/services/requestBuilder.ts +127 -0
  232. package/src/services/responseUtils.ts +32 -0
  233. package/src/services/rpcService.ts +129 -52
  234. package/src/services/service.ts +10 -8
  235. package/src/services/userProfileService.ts +38 -0
  236. package/src/storages/chats/answer.impl.ts +163 -0
  237. package/src/storages/chats/answer.ts +42 -0
  238. package/src/storages/chats/chat.impl.ts +87 -0
  239. package/src/storages/chats/chat.ts +38 -0
  240. package/src/storages/chats/chats.impl.ts +142 -0
  241. package/src/storages/chats/chats.ts +47 -0
  242. package/src/storages/files/file.impl.ts +69 -0
  243. package/src/storages/files/file.ts +28 -0
  244. package/src/storages/files/files.impl.ts +213 -0
  245. package/src/storages/files/files.ts +38 -0
  246. package/src/storages/files/filesPage.ts +27 -0
  247. package/src/storages/groups/groups.impl.ts +326 -0
  248. package/src/storages/groups/groups.ts +101 -0
  249. package/src/storages/organizations/organization.impl.ts +95 -0
  250. package/src/storages/organizations/organization.ts +44 -0
  251. package/src/storages/organizations/organizations.impl.ts +197 -0
  252. package/src/storages/organizations/organizations.ts +56 -0
  253. package/src/storages/user/userProfile.impl.ts +56 -0
  254. package/src/storages/user/userProfile.ts +42 -0
  255. package/src/storages/workspaces/workspace.impl.ts +109 -0
  256. package/src/storages/workspaces/workspace.ts +49 -0
  257. package/src/storages/workspaces/workspaces.impl.ts +212 -0
  258. package/src/storages/workspaces/workspaces.ts +53 -0
  259. package/src/unitTest.ts +53 -0
  260. package/.browserslistrc +0 -5
  261. package/.editorconfig +0 -22
  262. package/.eslintrc.json +0 -44
  263. package/.github/workflows/publish-npm.yml +0 -28
  264. package/.prettierignore +0 -1
  265. package/.prettierrc +0 -11
  266. package/.yarnrc +0 -2
  267. package/babel.config.js +0 -6
  268. package/jest.config.ts +0 -199
  269. package/src/appSdk.ts +0 -40
  270. package/src/internal/context.ts +0 -13
  271. package/src/types.ts +0 -110
  272. package/test/disposable.test.ts +0 -39
  273. package/test/events.test.ts +0 -151
  274. package/test/index.test.ts +0 -83
  275. package/test/registry.test.ts +0 -44
  276. package/tsconfig.json +0 -31
@@ -0,0 +1,163 @@
1
+ import { Context } from "../../context"
2
+ import {
3
+ AnswerDto,
4
+ AnswerStatus,
5
+ AnswerStepDto,
6
+ FetchAnswerResponse,
7
+ FetchTokensResponse,
8
+ SourceDto,
9
+ StepType
10
+ } from "../../dto/chatResponse"
11
+ import { ResponseUtils } from "../../services/responseUtils"
12
+ import { RpcService } from "../../services/rpcService"
13
+ import { Answer, AnswerId } from "./answer"
14
+ import { Chat } from "./chat"
15
+
16
+ export class AnswerImpl extends Answer {
17
+ private _content?: AnswerDto
18
+
19
+ private _steps?: AnswerStepDto[]
20
+ private _status?: AnswerStatus
21
+ private _id?: AnswerId
22
+
23
+ constructor(
24
+ private readonly chat: Chat,
25
+ private readonly context: Context) {
26
+ super()
27
+ }
28
+
29
+ async initFromData(answer: AnswerDto): Promise<AnswerImpl> {
30
+ this._content = answer
31
+ this._id = answer.id
32
+
33
+ // fetch answer
34
+ await this.fetch()
35
+
36
+ return this
37
+ }
38
+
39
+ async initFromId(id: AnswerId): Promise<AnswerImpl> {
40
+ this._id = id
41
+
42
+ // fetch answer
43
+ await this.fetch()
44
+
45
+ return this
46
+ }
47
+
48
+ get id(): string {
49
+ return <string>this._id
50
+ }
51
+
52
+ get status(): AnswerStatus {
53
+ return <AnswerStatus>this._status
54
+ }
55
+
56
+ private getStep(type: StepType): AnswerStepDto | undefined {
57
+ return this._steps?.find(step => step.type === type)
58
+ }
59
+
60
+ async sources(type: StepType): Promise<SourceDto[]> {
61
+ // fetch answer
62
+ await this.fetch()
63
+ // get step
64
+ const step = this.getStep(type)
65
+
66
+ // check step
67
+ if (!step) {
68
+ throw new Error(`Step with type ${type.toString()} is not found, answer: ${this.id}, organization: ${this.chat.organization.id}`)
69
+ }
70
+
71
+ // get sources
72
+ const response = await this.context
73
+ .resolve(RpcService)
74
+ ?.requestBuilder("api/v1/Chats/answer/sources")
75
+ .searchParam("chat_uid", this.chat.id)
76
+ .searchParam("uid", this.id)
77
+ .searchParam("step_id", step.id)
78
+ .sendGet()
79
+
80
+ // check response status
81
+ if (ResponseUtils.isFail(response)) {
82
+ await ResponseUtils.throwError(`Failed to get sources for ${type.toString()}, answer: ${this.id}, organization: ${this.chat.organization.id}`, response)
83
+ }
84
+
85
+ // parse sources from the server's response
86
+ const sources = (await response!.json()).sources as SourceDto[]
87
+
88
+ return sources
89
+ }
90
+
91
+ async fetch(): Promise<void> {
92
+ // fetch answer from position 0
93
+ const position = 0
94
+ // fetch answer
95
+ const response = await this.context
96
+ .resolve(RpcService)
97
+ ?.requestBuilder("api/v1/Chats/answer/fetch")
98
+ .searchParam("chatId", this.chat.id)
99
+ .searchParam("questionId", this.id)
100
+ .searchParam("position", position.toString())
101
+ .sendGet()
102
+
103
+ // check response status
104
+ if (ResponseUtils.isFail(response)) {
105
+ await ResponseUtils.throwError(`Failed to fetch answer ${this.id}`, response)
106
+ }
107
+
108
+ // parse answer from the server's response
109
+ const answer = (await response!.json()) as FetchAnswerResponse
110
+
111
+ // update answer
112
+ this._status = <AnswerStatus>answer.status
113
+ this._steps = <AnswerStepDto[]>answer.steps
114
+ }
115
+
116
+ async fetchTokens(type: StepType, token_start_at: number): Promise<FetchTokensResponse> {
117
+ // fetch answer
118
+ await this.fetch()
119
+ // get step
120
+ const step = this.getStep(type)
121
+
122
+ // check step
123
+ if (!step) {
124
+ throw new Error(`Step with type ${type.toString()} is not found`)
125
+ }
126
+
127
+ // get tokens
128
+ const response = await this.context
129
+ .resolve(RpcService)
130
+ ?.requestBuilder("api/v1/Chats/answer/fetch/tokens")
131
+ .searchParam("chat_uid", this.chat.id)
132
+ .searchParam("uid", this.id)
133
+ .searchParam("step_id", step.id)
134
+ .searchParam("token_start_at", token_start_at.toString())
135
+ .sendGet()
136
+
137
+ // check response status
138
+ if (ResponseUtils.isFail(response)) {
139
+ await ResponseUtils.throwError(`Failed to get sources for ${type.toString()}`, response)
140
+ }
141
+
142
+ // parse tokens from the server's response
143
+ const tokens = (await response!.json()) as FetchTokensResponse
144
+
145
+ return tokens
146
+ }
147
+
148
+ async cancel(): Promise<void> {
149
+ // send request to the server
150
+ const response = await this.context
151
+ .resolve(RpcService)
152
+ ?.requestBuilder("api/v1/Chats/answer/cancel")
153
+ .sendPutJson({
154
+ chat_id: this.chat.id,
155
+ uid: this.id
156
+ })
157
+
158
+ // check response status
159
+ if (ResponseUtils.isFail(response)) {
160
+ await ResponseUtils.throwError("Failed to cancel a question", response)
161
+ }
162
+ }
163
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ AnswerStatus,
3
+ FetchTokensResponse,
4
+ SourceDto,
5
+ StepType
6
+ } from "../../dto/chatResponse"
7
+
8
+ export type AnswerId = string
9
+ export type StepId = string
10
+
11
+ export abstract class Answer {
12
+
13
+ /**
14
+ * Answer id.
15
+ */
16
+ abstract get id(): AnswerId
17
+
18
+ /**
19
+ * Answer status.
20
+ */
21
+ abstract get status(): AnswerStatus
22
+
23
+ /**
24
+ * Answer sources.
25
+ */
26
+ abstract sources(type: StepType): Promise<SourceDto[]>
27
+
28
+ /**
29
+ * Fetch answer.
30
+ */
31
+ abstract fetch(): Promise<void>
32
+
33
+ /**
34
+ * Fetch answer.
35
+ */
36
+ abstract fetchTokens(type: StepType, tokenStartAt: number): Promise<FetchTokensResponse>
37
+
38
+ /**
39
+ * Cancel answer
40
+ */
41
+ abstract cancel(): Promise<void>
42
+ }
@@ -0,0 +1,87 @@
1
+ import { Chat, ChatAnswerType } from "./chat"
2
+ import { Disposable } from "../../disposable"
3
+ import { Answer } from "./answer"
4
+ import { ChatDto } from "../../dto/chatResponse"
5
+ import { Context } from "../../context"
6
+ import { AnswerImpl } from "./answer.impl"
7
+ import { RpcService } from "../../services/rpcService"
8
+ import { ResponseUtils } from "../../services/responseUtils"
9
+ import { Organization } from "../organizations/organization"
10
+
11
+ export class ChatImpl extends Chat implements Disposable {
12
+ private _isDisposed: boolean = false
13
+ private readonly _answers: Answer[] = []
14
+
15
+ private _content?: ChatDto
16
+
17
+ constructor(
18
+ private readonly context: Context,
19
+ public readonly organization: Organization
20
+ ) {
21
+ super()
22
+ }
23
+
24
+ async initFrom(chat: ChatDto): Promise<ChatImpl> {
25
+ this._content = chat
26
+
27
+ // init answers
28
+ for (const ans of chat.answers) {
29
+ // create answer implementation
30
+ const answer = await new AnswerImpl(this, this.context).initFromData(ans)
31
+
32
+ // add answer to the collection
33
+ this._answers.push(answer)
34
+ }
35
+
36
+ return this
37
+ }
38
+
39
+ get id(): string {
40
+ return <string>this._content?.id
41
+ }
42
+
43
+ get name(): string {
44
+ return <string>this._content?.name
45
+ }
46
+
47
+ get collection(): readonly Answer[] {
48
+ return this._answers
49
+ }
50
+
51
+ get isDisposed(): boolean {
52
+ return this._isDisposed
53
+ }
54
+
55
+ async ask(message: string, answerType: ChatAnswerType): Promise<Answer> {
56
+ // send request to the server
57
+ const response = await this.context
58
+ .resolve(RpcService)
59
+ ?.requestBuilder("api/v1/Chats/question")
60
+ .sendPutJson({
61
+ chatId: this.id,
62
+ questionMessage: message,
63
+ isLongAnswer: (answerType === ChatAnswerType.LONG)
64
+ })
65
+
66
+ // check response status
67
+ if (ResponseUtils.isFail(response)) {
68
+ await ResponseUtils.throwError(`Failed to ask a question, organization: ${this.organization.id}`, response)
69
+ }
70
+
71
+ // parse answer id from the server's response
72
+ const id = (await response!.json()).id
73
+
74
+ // create answer implementation
75
+ const answer = await new AnswerImpl(this, this.context).initFromId(id)
76
+
77
+ // add answer to the collection
78
+ this._answers.push(answer)
79
+
80
+ return answer
81
+ }
82
+
83
+ dispose(): void {
84
+ this._isDisposed = true
85
+ }
86
+
87
+ }
@@ -0,0 +1,38 @@
1
+ import { Answer } from "./answer"
2
+ import { Organization } from "../organizations/organization"
3
+
4
+ export type ChatId = string
5
+
6
+ export enum ChatAnswerType {
7
+ SHORT = "short",
8
+ LONG = "long"
9
+ }
10
+
11
+ export abstract class Chat {
12
+
13
+ /**
14
+ * Organization.
15
+ */
16
+ abstract get organization(): Organization
17
+
18
+ /**
19
+ * Chat id.
20
+ */
21
+ abstract get id(): ChatId
22
+
23
+ /**
24
+ * Chat name.
25
+ */
26
+ abstract get name(): string
27
+
28
+ /**
29
+ * Answers list.
30
+ */
31
+ abstract get collection(): ReadonlyArray<Answer>
32
+
33
+ /**
34
+ * Ask new question in chat.
35
+ */
36
+ abstract ask(message: string, answerType: ChatAnswerType): Promise<Answer>
37
+ }
38
+
@@ -0,0 +1,142 @@
1
+ import { Context } from "../../context"
2
+ import { ChatDto, ChatListResponse } from "../../dto/chatResponse"
3
+ import { ResponseUtils } from "../../services/responseUtils"
4
+ import { RpcService } from "../../services/rpcService"
5
+ import { OrganizationImpl } from "../organizations/organization.impl"
6
+ import { OrganizationId } from "../organizations/organizations"
7
+ import { Chat } from "./chat"
8
+ import { ChatImpl } from "./chat.impl"
9
+ import { Chats, ChatsEvent } from "./chats"
10
+
11
+ export class ChatsImpl extends Chats {
12
+ private readonly _chats: Chat[] = []
13
+
14
+ constructor(
15
+ public readonly organization: OrganizationImpl,
16
+ private readonly context: Context
17
+ ) {
18
+ super()
19
+ }
20
+
21
+ async initFrom(organizationId: OrganizationId): Promise<void> {
22
+ // init chats from the server's response
23
+ const limit = 100
24
+ const page = 0
25
+ const response = await this.context
26
+ .resolve(RpcService)
27
+ ?.requestBuilder("api/v1/Chats/list")
28
+ .searchParam("organizationId", organizationId)
29
+ .searchParam("limit", limit.toString())
30
+ .searchParam("page", page.toString())
31
+ .sendGet()
32
+
33
+ // check response status
34
+ if (ResponseUtils.isFail(response)) {
35
+ await ResponseUtils.throwError(
36
+ `Chats list org id:${organizationId}, page:${page}, limit:${limit}, failed`,
37
+ response
38
+ )
39
+ }
40
+
41
+ // parse chats from the server's response
42
+ const chats = (await response!.json()) as ChatListResponse
43
+
44
+ // init chats
45
+ for (const cht of chats.chats) {
46
+ // create chat implementation
47
+ const chat = await new ChatImpl(this.context, this.organization).initFrom(cht)
48
+
49
+ // add chat to the collection
50
+ this._chats.push(chat)
51
+
52
+ // dispatch event
53
+ this.dispatch({
54
+ type: ChatsEvent.ADDED,
55
+ data: chat
56
+ })
57
+ }
58
+
59
+ }
60
+
61
+ get collection(): readonly Chat[] {
62
+ return this._chats
63
+ }
64
+
65
+ get(id: string): Chat {
66
+ return <Chat>this.tryGet(id)
67
+ }
68
+
69
+ tryGet(id: string): Chat | undefined {
70
+ return this._chats.find(chat => chat.id === id)
71
+ }
72
+
73
+ async create(): Promise<Chat> {
74
+
75
+ // send create request to the server
76
+ const response = await this.context
77
+ .resolve(RpcService)
78
+ ?.requestBuilder("api/v1/Chats")
79
+ .sendPostJson({ organizationId: this.organization.id })
80
+
81
+ // check response status
82
+ if (ResponseUtils.isFail(response)) {
83
+ await ResponseUtils.throwError(`Failed to create chat, organization: ${this.organization.id}`, response)
84
+ }
85
+
86
+ // parse workspace from the server's response
87
+ const content = (await response!.json()).chat as ChatDto
88
+
89
+ // create workspace implementation
90
+ const chat = new ChatImpl(this.context, this.organization)
91
+ await chat.initFrom(content)
92
+
93
+ // add chat to the collection
94
+ this._chats.push(chat)
95
+
96
+ // dispatch event
97
+ this.dispatch({
98
+ type: ChatsEvent.ADDED,
99
+ data: chat
100
+ })
101
+
102
+ return chat
103
+ }
104
+
105
+ async delete(id: string): Promise<void> {
106
+ // get chat by id
107
+ const chat = <ChatImpl>this.tryGet(id)
108
+
109
+ // check if chat is found
110
+ if (!chat) {
111
+ throw new Error(`Chat ${id} is not found, organization: ${this.organization.id}`)
112
+ }
113
+
114
+ // send delete request to the server
115
+ const response = await this.context
116
+ .resolve(RpcService)
117
+ ?.requestBuilder("api/v1/Chats")
118
+ .searchParam("id", id)
119
+ .sendDelete()
120
+
121
+ // check response status
122
+ if (ResponseUtils.isFail(response)) {
123
+ await ResponseUtils.throwError(
124
+ `Failed to delete chat: ${id}, organization: ${this.organization.id}`,
125
+ response
126
+ )
127
+ }
128
+
129
+ // remove chat from the collection
130
+ const index = this._chats.indexOf(<ChatImpl>chat)
131
+ if (index < 0) {
132
+ throw new Error(`Chat ${id} is not found, organization: ${this.organization.id}`)
133
+ }
134
+ this._chats.splice(index, 1)
135
+
136
+ // dispatch event
137
+ this.dispatch({
138
+ type: ChatsEvent.REMOVED,
139
+ data: chat
140
+ })
141
+ }
142
+ }
@@ -0,0 +1,47 @@
1
+ import { EventDispatcher } from "../../events"
2
+ import { Chat, ChatId } from "./chat"
3
+ import { Organization } from "../organizations/organization"
4
+
5
+ export enum ChatsEvent {
6
+ ADDED = "added",
7
+ REMOVED = "removed"
8
+ }
9
+
10
+ /**
11
+ * Chats storage.
12
+ */
13
+ export abstract class Chats extends EventDispatcher<ChatsEvent, Chat> {
14
+
15
+ /**
16
+ * Organization.
17
+ */
18
+ abstract get organization(): Organization
19
+
20
+ /**
21
+ * Chats list.
22
+ */
23
+ abstract get collection(): ReadonlyArray<Chat>
24
+
25
+ /**
26
+ * Create new chat.
27
+ */
28
+ abstract create(): Promise<Chat>
29
+
30
+ /**
31
+ * Get chat by id.
32
+ * @param id
33
+ */
34
+ abstract get(id: ChatId): Chat
35
+
36
+ /**
37
+ * Try to get chat by id.
38
+ * @param id
39
+ */
40
+ abstract tryGet(id: ChatId): Chat | undefined
41
+
42
+ /**
43
+ * Delete chat.
44
+ * @param id
45
+ */
46
+ abstract delete(id: ChatId): Promise<void>
47
+ }
@@ -0,0 +1,69 @@
1
+ import { Context } from "../../context"
2
+ import { Disposable } from "../../disposable"
3
+ import { FileDto, FileProgressDto } from "../../dto/workspacesResponse"
4
+ import { RpcService } from "../../services/rpcService"
5
+ import { ResponseUtils } from "../../services/responseUtils"
6
+ import { File } from "./file"
7
+
8
+ export class FileImpl extends File implements Disposable {
9
+ private _isDisposed: boolean = false
10
+ private _content?: FileDto
11
+
12
+ constructor(private readonly context: Context) {
13
+ super()
14
+ }
15
+
16
+ public initFrom(file: FileDto): File {
17
+ this._content = file
18
+
19
+ return this
20
+ }
21
+
22
+ get isDisposed(): boolean {
23
+ return this._isDisposed
24
+ }
25
+
26
+ dispose(): void {
27
+ this._isDisposed = true
28
+ }
29
+
30
+ get id(): string {
31
+ return <string>this._content?.id
32
+ }
33
+
34
+ get name(): string {
35
+ return <string>this._content?.name
36
+ }
37
+
38
+ async url(): Promise<string> {
39
+ const response = await this.context
40
+ .resolve(RpcService)
41
+ ?.requestBuilder("api/v1/Files/url")
42
+ .searchParam("id", this.id)
43
+ .sendGet()
44
+
45
+ if (ResponseUtils.isFail(response)) {
46
+ await ResponseUtils.throwError(
47
+ `Failed to get file ${this.id} url`,
48
+ response
49
+ )
50
+ }
51
+
52
+ return (await response!.json()).url
53
+ }
54
+
55
+ async status(): Promise<FileProgressDto> {
56
+ const response = await this.context
57
+ .resolve(RpcService)
58
+ ?.requestBuilder("api/v1/Files/fetch")
59
+ .searchParam("id", this.id)
60
+ .sendGet()
61
+
62
+ if (ResponseUtils.isFail(response)) {
63
+ await ResponseUtils.throwError(`Failed to get file ${this.id}`, response)
64
+ }
65
+
66
+ const content = await response!.json()
67
+ return content.progress as FileProgressDto
68
+ }
69
+ }
@@ -0,0 +1,28 @@
1
+ import { FileProgressDto } from "../../dto/workspacesResponse"
2
+
3
+ export type FileId = string
4
+
5
+ /**
6
+ * File.
7
+ */
8
+ export abstract class File {
9
+ /**
10
+ * File id.
11
+ */
12
+ abstract get id(): FileId
13
+
14
+ /**
15
+ * File name.
16
+ */
17
+ abstract get name(): string
18
+
19
+ /**
20
+ * Get temporary url.
21
+ */
22
+ abstract url(): Promise<string>
23
+
24
+ /**
25
+ * Get file status.
26
+ */
27
+ abstract status(): Promise<FileProgressDto>
28
+ }