@neuralinnovations/dataisland-sdk 0.0.1-dev2 → 0.0.1-dev20
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/LICENSE +201 -0
- package/README.md +306 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/package.json +100 -0
- package/dist/src/appBuilder.d.ts +44 -0
- package/dist/src/appBuilder.d.ts.map +1 -0
- package/dist/src/appBuilder.js +10 -0
- package/dist/src/appBuilder.js.map +1 -0
- package/dist/src/commands/startCommandHandler.d.ts +7 -0
- package/dist/src/commands/startCommandHandler.d.ts.map +1 -0
- package/dist/src/commands/startCommandHandler.js +17 -0
- package/dist/src/commands/startCommandHandler.js.map +1 -0
- package/dist/src/context.d.ts +23 -0
- package/dist/src/context.d.ts.map +1 -0
- package/dist/src/context.js +31 -0
- package/dist/src/context.js.map +1 -0
- package/dist/src/credentials.d.ts +28 -0
- package/dist/src/credentials.d.ts.map +1 -0
- package/dist/src/credentials.js +70 -0
- package/dist/src/credentials.js.map +1 -0
- package/dist/src/dataIslandApp.d.ts +50 -0
- package/dist/src/dataIslandApp.d.ts.map +1 -0
- package/dist/src/dataIslandApp.js +10 -0
- package/dist/src/dataIslandApp.js.map +1 -0
- package/dist/src/disposable.d.ts +84 -0
- package/dist/src/disposable.d.ts.map +1 -0
- package/dist/src/disposable.js +147 -0
- package/dist/src/disposable.js.map +1 -0
- package/dist/src/dto/accessGroupResponse.d.ts +27 -0
- package/dist/src/dto/accessGroupResponse.d.ts.map +1 -0
- package/dist/src/dto/accessGroupResponse.js +3 -0
- package/dist/src/dto/accessGroupResponse.js.map +1 -0
- package/dist/src/dto/chatResponse.d.ts +78 -0
- package/dist/src/dto/chatResponse.d.ts.map +1 -0
- package/dist/src/dto/chatResponse.js +45 -0
- package/dist/src/dto/chatResponse.js.map +1 -0
- package/dist/src/dto/userInfoResponse.d.ts +42 -0
- package/dist/src/dto/userInfoResponse.d.ts.map +1 -0
- package/dist/src/dto/userInfoResponse.js +3 -0
- package/dist/src/dto/userInfoResponse.js.map +1 -0
- package/dist/src/dto/workspacesResponse.d.ts +43 -0
- package/dist/src/dto/workspacesResponse.d.ts.map +1 -0
- package/dist/src/dto/workspacesResponse.js +3 -0
- package/dist/src/dto/workspacesResponse.js.map +1 -0
- package/dist/src/events.d.ts +17 -0
- package/dist/src/events.d.ts.map +1 -0
- package/dist/src/events.js +52 -0
- package/dist/src/events.js.map +1 -0
- package/dist/src/index.d.ts +58 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +91 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/internal/app.impl.d.ts +28 -0
- package/dist/src/internal/app.impl.d.ts.map +1 -0
- package/dist/src/internal/app.impl.js +151 -0
- package/dist/src/internal/app.impl.js.map +1 -0
- package/dist/src/internal/appBuilder.impl.d.ts +27 -0
- package/dist/src/internal/appBuilder.impl.d.ts.map +1 -0
- package/dist/src/internal/appBuilder.impl.js +71 -0
- package/dist/src/internal/appBuilder.impl.js.map +1 -0
- package/dist/src/internal/createApp.impl.d.ts +4 -0
- package/dist/src/internal/createApp.impl.d.ts.map +1 -0
- package/dist/src/internal/createApp.impl.js +11 -0
- package/dist/src/internal/createApp.impl.js.map +1 -0
- package/dist/src/internal/registry.d.ts +66 -0
- package/dist/src/internal/registry.d.ts.map +1 -0
- package/dist/src/internal/registry.js +114 -0
- package/dist/src/internal/registry.js.map +1 -0
- package/dist/src/middleware.d.ts +5 -0
- package/dist/src/middleware.d.ts.map +1 -0
- package/dist/src/middleware.js +3 -0
- package/dist/src/middleware.js.map +1 -0
- package/dist/src/services/commandService.d.ts +18 -0
- package/dist/src/services/commandService.d.ts.map +1 -0
- package/dist/src/services/commandService.js +40 -0
- package/dist/src/services/commandService.js.map +1 -0
- package/dist/src/services/credentialService.d.ts +9 -0
- package/dist/src/services/credentialService.d.ts.map +1 -0
- package/dist/src/services/credentialService.js +26 -0
- package/dist/src/services/credentialService.js.map +1 -0
- package/dist/src/services/middlewareService.d.ts +9 -0
- package/dist/src/services/middlewareService.d.ts.map +1 -0
- package/dist/src/services/middlewareService.js +34 -0
- package/dist/src/services/middlewareService.js.map +1 -0
- package/dist/src/services/organizationService.d.ts +10 -0
- package/dist/src/services/organizationService.d.ts.map +1 -0
- package/dist/src/services/organizationService.js +19 -0
- package/dist/src/services/organizationService.js.map +1 -0
- package/dist/src/services/requestBuilder.d.ts +17 -0
- package/dist/src/services/requestBuilder.d.ts.map +1 -0
- package/dist/src/services/requestBuilder.js +104 -0
- package/dist/src/services/requestBuilder.js.map +1 -0
- package/dist/src/services/responseUtils.d.ts +6 -0
- package/dist/src/services/responseUtils.d.ts.map +1 -0
- package/dist/src/services/responseUtils.js +32 -0
- package/dist/src/services/responseUtils.js.map +1 -0
- package/dist/src/services/rpcService.d.ts +77 -0
- package/dist/src/services/rpcService.d.ts.map +1 -0
- package/dist/src/services/rpcService.js +125 -0
- package/dist/src/services/rpcService.js.map +1 -0
- package/dist/src/services/service.d.ts +21 -0
- package/dist/src/services/service.d.ts.map +1 -0
- package/dist/src/services/service.js +41 -0
- package/dist/src/services/service.js.map +1 -0
- package/dist/src/services/userProfileService.d.ts +8 -0
- package/dist/src/services/userProfileService.d.ts.map +1 -0
- package/dist/src/services/userProfileService.js +32 -0
- package/dist/src/services/userProfileService.js.map +1 -0
- package/dist/src/storages/chats/answer.d.ts +40 -0
- package/dist/src/storages/chats/answer.d.ts.map +1 -0
- package/dist/src/storages/chats/answer.impl.d.ts +24 -0
- package/dist/src/storages/chats/answer.impl.d.ts.map +1 -0
- package/dist/src/storages/chats/answer.impl.js +130 -0
- package/dist/src/storages/chats/answer.impl.js.map +1 -0
- package/dist/src/storages/chats/answer.js +14 -0
- package/dist/src/storages/chats/answer.js.map +1 -0
- package/dist/src/storages/chats/chat.d.ts +39 -0
- package/dist/src/storages/chats/chat.d.ts.map +1 -0
- package/dist/src/storages/chats/chat.impl.d.ts +24 -0
- package/dist/src/storages/chats/chat.impl.d.ts.map +1 -0
- package/dist/src/storages/chats/chat.impl.js +98 -0
- package/dist/src/storages/chats/chat.impl.js.map +1 -0
- package/dist/src/storages/chats/chat.js +12 -0
- package/dist/src/storages/chats/chat.js.map +1 -0
- package/dist/src/storages/chats/chats.d.ts +40 -0
- package/dist/src/storages/chats/chats.d.ts.map +1 -0
- package/dist/src/storages/chats/chats.impl.d.ts +18 -0
- package/dist/src/storages/chats/chats.impl.d.ts.map +1 -0
- package/dist/src/storages/chats/chats.impl.js +102 -0
- package/dist/src/storages/chats/chats.impl.js.map +1 -0
- package/dist/src/storages/chats/chats.js +16 -0
- package/dist/src/storages/chats/chats.js.map +1 -0
- package/dist/src/storages/files/file.d.ts +31 -0
- package/dist/src/storages/files/file.d.ts.map +1 -0
- package/dist/src/storages/files/file.impl.d.ts +21 -0
- package/dist/src/storages/files/file.impl.d.ts.map +1 -0
- package/dist/src/storages/files/file.impl.js +64 -0
- package/dist/src/storages/files/file.impl.js.map +1 -0
- package/dist/src/storages/files/file.js +11 -0
- package/dist/src/storages/files/file.js.map +1 -0
- package/dist/src/storages/files/files.d.ts +34 -0
- package/dist/src/storages/files/files.d.ts.map +1 -0
- package/dist/src/storages/files/files.impl.d.ts +22 -0
- package/dist/src/storages/files/files.impl.d.ts.map +1 -0
- package/dist/src/storages/files/files.impl.js +140 -0
- package/dist/src/storages/files/files.impl.js.map +1 -0
- package/dist/src/storages/files/files.js +20 -0
- package/dist/src/storages/files/files.js.map +1 -0
- package/dist/src/storages/files/filesPage.d.ts +28 -0
- package/dist/src/storages/files/filesPage.d.ts.map +1 -0
- package/dist/src/storages/files/filesPage.impl.d.ts +15 -0
- package/dist/src/storages/files/filesPage.impl.d.ts.map +1 -0
- package/dist/src/storages/files/filesPage.impl.js +38 -0
- package/dist/src/storages/files/filesPage.impl.js.map +1 -0
- package/dist/src/storages/files/filesPage.js +10 -0
- package/dist/src/storages/files/filesPage.js.map +1 -0
- package/dist/src/storages/groups/groups.d.ts +91 -0
- package/dist/src/storages/groups/groups.d.ts.map +1 -0
- package/dist/src/storages/groups/groups.impl.d.ts +58 -0
- package/dist/src/storages/groups/groups.impl.d.ts.map +1 -0
- package/dist/src/storages/groups/groups.impl.js +275 -0
- package/dist/src/storages/groups/groups.impl.js.map +1 -0
- package/dist/src/storages/groups/groups.js +26 -0
- package/dist/src/storages/groups/groups.js.map +1 -0
- package/dist/src/storages/organizations/organization.d.ts +54 -0
- package/dist/src/storages/organizations/organization.d.ts.map +1 -0
- package/dist/src/storages/organizations/organization.impl.d.ts +32 -0
- package/dist/src/storages/organizations/organization.impl.d.ts.map +1 -0
- package/dist/src/storages/organizations/organization.impl.js +120 -0
- package/dist/src/storages/organizations/organization.impl.js.map +1 -0
- package/dist/src/storages/organizations/organization.js +18 -0
- package/dist/src/storages/organizations/organization.js.map +1 -0
- package/dist/src/storages/organizations/organizations.d.ts +46 -0
- package/dist/src/storages/organizations/organizations.d.ts.map +1 -0
- package/dist/src/storages/organizations/organizations.impl.d.ts +38 -0
- package/dist/src/storages/organizations/organizations.impl.d.ts.map +1 -0
- package/dist/src/storages/organizations/organizations.impl.js +151 -0
- package/dist/src/storages/organizations/organizations.impl.js.map +1 -0
- package/dist/src/storages/organizations/organizations.js +20 -0
- package/dist/src/storages/organizations/organizations.js.map +1 -0
- package/dist/src/storages/user/userProfile.d.ts +32 -0
- package/dist/src/storages/user/userProfile.d.ts.map +1 -0
- package/dist/src/storages/user/userProfile.impl.d.ts +13 -0
- package/dist/src/storages/user/userProfile.impl.d.ts.map +1 -0
- package/dist/src/storages/user/userProfile.impl.js +51 -0
- package/dist/src/storages/user/userProfile.impl.js.map +1 -0
- package/dist/src/storages/user/userProfile.js +12 -0
- package/dist/src/storages/user/userProfile.js.map +1 -0
- package/dist/src/storages/workspaces/workspace.d.ts +44 -0
- package/dist/src/storages/workspaces/workspace.d.ts.map +1 -0
- package/dist/src/storages/workspaces/workspace.impl.d.ts +23 -0
- package/dist/src/storages/workspaces/workspace.impl.d.ts.map +1 -0
- package/dist/src/storages/workspaces/workspace.impl.js +98 -0
- package/dist/src/storages/workspaces/workspace.impl.js.map +1 -0
- package/dist/src/storages/workspaces/workspace.js +18 -0
- package/dist/src/storages/workspaces/workspace.js.map +1 -0
- package/dist/src/storages/workspaces/workspaces.d.ts +47 -0
- package/dist/src/storages/workspaces/workspaces.d.ts.map +1 -0
- package/dist/src/storages/workspaces/workspaces.impl.d.ts +33 -0
- package/dist/src/storages/workspaces/workspaces.impl.d.ts.map +1 -0
- package/dist/src/storages/workspaces/workspaces.impl.js +157 -0
- package/dist/src/storages/workspaces/workspaces.impl.js.map +1 -0
- package/dist/src/storages/workspaces/workspaces.js +19 -0
- package/dist/src/storages/workspaces/workspaces.js.map +1 -0
- package/dist/src/unitTest.d.ts +12 -0
- package/dist/src/unitTest.d.ts.map +1 -0
- package/dist/src/unitTest.js +44 -0
- package/dist/src/unitTest.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +43 -4
- package/src/appBuilder.ts +6 -6
- package/src/commands/startCommandHandler.ts +2 -2
- package/src/context.ts +5 -4
- package/src/credentials.ts +29 -7
- package/src/{appSdk.ts → dataIslandApp.ts} +7 -7
- package/src/disposable.ts +20 -8
- package/src/dto/accessGroupResponse.ts +35 -0
- package/src/dto/chatResponse.ts +103 -0
- package/src/dto/userInfoResponse.ts +15 -3
- package/src/dto/workspacesResponse.ts +49 -0
- package/src/events.ts +13 -13
- package/src/index.ts +41 -20
- package/src/internal/app.impl.ts +28 -31
- package/src/internal/appBuilder.impl.ts +16 -16
- package/src/internal/createApp.impl.ts +5 -5
- package/src/internal/registry.ts +54 -6
- package/src/services/commandService.ts +3 -3
- package/src/services/credentialService.ts +3 -3
- package/src/services/middlewareService.ts +4 -4
- package/src/services/organizationService.ts +18 -116
- package/src/services/requestBuilder.ts +40 -15
- package/src/services/responseUtils.ts +32 -0
- package/src/services/rpcService.ts +28 -11
- package/src/services/service.ts +10 -8
- package/src/services/userProfileService.ts +18 -66
- package/src/storages/chats/answer.impl.ts +186 -0
- package/src/storages/chats/answer.ts +55 -0
- package/src/storages/chats/chat.impl.ts +126 -0
- package/src/storages/chats/chat.ts +49 -0
- package/src/storages/chats/chats.impl.ts +142 -0
- package/src/storages/chats/chats.ts +47 -0
- package/src/storages/files/file.impl.ts +87 -0
- package/src/storages/files/file.ts +40 -0
- package/src/storages/files/files.impl.ts +191 -0
- package/src/storages/files/files.ts +39 -0
- package/src/storages/files/filesPage.impl.ts +37 -0
- package/src/storages/files/filesPage.ts +33 -0
- package/src/storages/groups/groups.impl.ts +363 -0
- package/src/storages/groups/groups.ts +106 -0
- package/src/storages/organizations/organization.impl.ts +163 -0
- package/src/storages/organizations/organization.ts +67 -0
- package/src/storages/organizations/organizations.impl.ts +197 -0
- package/src/storages/{organizations.ts → organizations/organizations.ts} +8 -28
- package/src/storages/user/userProfile.impl.ts +56 -0
- package/src/storages/{userProfile.ts → user/userProfile.ts} +2 -2
- package/src/storages/workspaces/workspace.impl.ts +129 -0
- package/src/storages/workspaces/workspace.ts +54 -0
- package/src/storages/workspaces/workspaces.impl.ts +212 -0
- package/src/storages/workspaces/workspaces.ts +53 -0
- package/src/unitTest.ts +12 -1
- package/.browserslistrc +0 -5
- package/.editorconfig +0 -22
- package/.eslintrc.json +0 -44
- package/.github/workflows/publish-npm.yml +0 -28
- package/.prettierignore +0 -1
- package/.prettierrc +0 -11
- package/.yarnrc +0 -2
- package/babel.config.js +0 -6
- package/jest.config.ts +0 -199
- package/jest.setup.ts +0 -2
- package/src/services/organizationImpl.ts +0 -51
- package/src/services/organizationsImpl.ts +0 -55
- package/src/types.ts +0 -86
- package/test/commands.test.ts +0 -24
- package/test/disposable.test.ts +0 -39
- package/test/events.test.ts +0 -151
- package/test/index.test.ts +0 -163
- package/test/registry.test.ts +0 -44
- package/test/services.test.ts +0 -56
- package/test/setup.ts +0 -2
- package/test/unitTest.test.ts +0 -21
- package/tsconfig.json +0 -31
@@ -0,0 +1,186 @@
|
|
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, AnswerEvent, 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
|
+
initFromData(answer: AnswerDto): AnswerImpl {
|
30
|
+
this._content = answer
|
31
|
+
this._id = answer.id
|
32
|
+
|
33
|
+
return this
|
34
|
+
}
|
35
|
+
|
36
|
+
async initFromId(id: AnswerId): Promise<AnswerImpl> {
|
37
|
+
this._id = id
|
38
|
+
|
39
|
+
// fetch answer
|
40
|
+
await this.fetch()
|
41
|
+
|
42
|
+
this.dispatch({
|
43
|
+
type: AnswerEvent.ADDED,
|
44
|
+
data: this
|
45
|
+
})
|
46
|
+
|
47
|
+
return this
|
48
|
+
}
|
49
|
+
|
50
|
+
get id(): string {
|
51
|
+
return <string>this._id
|
52
|
+
}
|
53
|
+
|
54
|
+
get status(): AnswerStatus {
|
55
|
+
return <AnswerStatus>this._status
|
56
|
+
}
|
57
|
+
|
58
|
+
get content(): AnswerDto {
|
59
|
+
if (this.status != AnswerStatus.RUNNING){
|
60
|
+
return <AnswerDto>this._content
|
61
|
+
}
|
62
|
+
throw new Error("Answer status is running, please use fetch() or fetch_tokens()")
|
63
|
+
}
|
64
|
+
|
65
|
+
private getStep(type: StepType): AnswerStepDto | undefined {
|
66
|
+
return this._steps?.find(step => step.type === type)
|
67
|
+
}
|
68
|
+
|
69
|
+
async sources(type: StepType): Promise<SourceDto[]> {
|
70
|
+
// fetch answer
|
71
|
+
await this.fetch()
|
72
|
+
// get step
|
73
|
+
const step = this.getStep(type)
|
74
|
+
|
75
|
+
// check step
|
76
|
+
if (!step) {
|
77
|
+
throw new Error(`Step with type ${type.toString()} is not found, answer: ${this.id}, organization: ${this.chat.organization.id}`)
|
78
|
+
}
|
79
|
+
|
80
|
+
// get sources
|
81
|
+
const response = await this.context
|
82
|
+
.resolve(RpcService)
|
83
|
+
?.requestBuilder("api/v1/Chats/answer/sources")
|
84
|
+
.searchParam("chat_uid", this.chat.id)
|
85
|
+
.searchParam("uid", this.id)
|
86
|
+
.searchParam("step_id", step.id)
|
87
|
+
.sendGet()
|
88
|
+
|
89
|
+
// check response status
|
90
|
+
if (ResponseUtils.isFail(response)) {
|
91
|
+
await ResponseUtils.throwError(`Failed to get sources for ${type.toString()}, answer: ${this.id}, organization: ${this.chat.organization.id}`, response)
|
92
|
+
}
|
93
|
+
|
94
|
+
// parse sources from the server's response
|
95
|
+
const sources = (await response!.json()).sources as SourceDto[]
|
96
|
+
|
97
|
+
return sources
|
98
|
+
}
|
99
|
+
|
100
|
+
async fetch(): Promise<void> {
|
101
|
+
// fetch answer from position 0
|
102
|
+
const position = 0
|
103
|
+
// fetch answer
|
104
|
+
const response = await this.context
|
105
|
+
.resolve(RpcService)
|
106
|
+
?.requestBuilder("api/v1/Chats/answer/fetch")
|
107
|
+
.searchParam("chatId", this.chat.id)
|
108
|
+
.searchParam("questionId", this.id)
|
109
|
+
.searchParam("position", position.toString())
|
110
|
+
.sendGet()
|
111
|
+
|
112
|
+
// check response status
|
113
|
+
if (ResponseUtils.isFail(response)) {
|
114
|
+
await ResponseUtils.throwError(`Failed to fetch answer ${this.id}`, response)
|
115
|
+
}
|
116
|
+
|
117
|
+
// parse answer from the server's response
|
118
|
+
const answer = (await response!.json()) as FetchAnswerResponse
|
119
|
+
|
120
|
+
// update answer
|
121
|
+
this._status = <AnswerStatus>answer.status
|
122
|
+
this._steps = <AnswerStepDto[]>answer.steps
|
123
|
+
|
124
|
+
this.dispatch({
|
125
|
+
type: AnswerEvent.UPDATED,
|
126
|
+
data: this
|
127
|
+
})
|
128
|
+
|
129
|
+
if (this._status != AnswerStatus.RUNNING){
|
130
|
+
await this.chat.update()
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
async fetchTokens(type: StepType, token_start_at: number): Promise<FetchTokensResponse> {
|
135
|
+
// fetch answer
|
136
|
+
await this.fetch()
|
137
|
+
// get step
|
138
|
+
const step = this.getStep(type)
|
139
|
+
|
140
|
+
// check step
|
141
|
+
if (!step) {
|
142
|
+
throw new Error(`Step with type ${type.toString()} is not found`)
|
143
|
+
}
|
144
|
+
|
145
|
+
// get tokens
|
146
|
+
const response = await this.context
|
147
|
+
.resolve(RpcService)
|
148
|
+
?.requestBuilder("api/v1/Chats/answer/fetch/tokens")
|
149
|
+
.searchParam("chat_uid", this.chat.id)
|
150
|
+
.searchParam("uid", this.id)
|
151
|
+
.searchParam("step_id", step.id)
|
152
|
+
.searchParam("token_start_at", token_start_at.toString())
|
153
|
+
.sendGet()
|
154
|
+
|
155
|
+
// check response status
|
156
|
+
if (ResponseUtils.isFail(response)) {
|
157
|
+
await ResponseUtils.throwError(`Failed to get sources for ${type.toString()}`, response)
|
158
|
+
}
|
159
|
+
|
160
|
+
// parse tokens from the server's response
|
161
|
+
const tokens = (await response!.json()) as FetchTokensResponse
|
162
|
+
|
163
|
+
return tokens
|
164
|
+
}
|
165
|
+
|
166
|
+
async cancel(): Promise<void> {
|
167
|
+
// send request to the server
|
168
|
+
const response = await this.context
|
169
|
+
.resolve(RpcService)
|
170
|
+
?.requestBuilder("api/v1/Chats/answer/cancel")
|
171
|
+
.sendPutJson({
|
172
|
+
chat_id: this.chat.id,
|
173
|
+
uid: this.id
|
174
|
+
})
|
175
|
+
|
176
|
+
// check response status
|
177
|
+
if (ResponseUtils.isFail(response)) {
|
178
|
+
await ResponseUtils.throwError("Failed to cancel a question", response)
|
179
|
+
}
|
180
|
+
|
181
|
+
this.dispatch({
|
182
|
+
type: AnswerEvent.CANCALLED,
|
183
|
+
data: this
|
184
|
+
})
|
185
|
+
}
|
186
|
+
}
|
@@ -0,0 +1,55 @@
|
|
1
|
+
import {
|
2
|
+
AnswerDto,
|
3
|
+
AnswerStatus,
|
4
|
+
FetchTokensResponse,
|
5
|
+
SourceDto,
|
6
|
+
StepType
|
7
|
+
} from "../../dto/chatResponse"
|
8
|
+
import { EventDispatcher } from "../../events"
|
9
|
+
|
10
|
+
export type AnswerId = string
|
11
|
+
export type StepId = string
|
12
|
+
|
13
|
+
export enum AnswerEvent {
|
14
|
+
ADDED = "added",
|
15
|
+
CANCALLED = "cancelled",
|
16
|
+
UPDATED = "updated"
|
17
|
+
}
|
18
|
+
|
19
|
+
export abstract class Answer extends EventDispatcher<AnswerEvent, Answer> {
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Answer id.
|
23
|
+
*/
|
24
|
+
abstract get id(): AnswerId
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Answer data object
|
28
|
+
*/
|
29
|
+
abstract get content(): AnswerDto
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Answer status.
|
33
|
+
*/
|
34
|
+
abstract get status(): AnswerStatus
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Answer sources.
|
38
|
+
*/
|
39
|
+
abstract sources(type: StepType): Promise<SourceDto[]>
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Fetch answer.
|
43
|
+
*/
|
44
|
+
abstract fetch(): Promise<void>
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Fetch answer.
|
48
|
+
*/
|
49
|
+
abstract fetchTokens(type: StepType, tokenStartAt: number): Promise<FetchTokensResponse>
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Cancel answer
|
53
|
+
*/
|
54
|
+
abstract cancel(): Promise<void>
|
55
|
+
}
|
@@ -0,0 +1,126 @@
|
|
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: AnswerImpl[] = []
|
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 <Answer[]>this._answers
|
49
|
+
}
|
50
|
+
|
51
|
+
get isDisposed(): boolean {
|
52
|
+
return this._isDisposed
|
53
|
+
}
|
54
|
+
|
55
|
+
public getAnswer(id: string): Answer {
|
56
|
+
const answer = this._answers.find(answer => answer.id === id)
|
57
|
+
if (answer){
|
58
|
+
return answer
|
59
|
+
}
|
60
|
+
throw new Error(`Answer with id ${id} is not found`)
|
61
|
+
}
|
62
|
+
|
63
|
+
async ask(message: string, answerType: ChatAnswerType): Promise<Answer> {
|
64
|
+
// send request to the server
|
65
|
+
const response = await this.context
|
66
|
+
.resolve(RpcService)
|
67
|
+
?.requestBuilder("api/v1/Chats/question")
|
68
|
+
.sendPutJson({
|
69
|
+
chatId: this.id,
|
70
|
+
questionMessage: message,
|
71
|
+
isLongAnswer: (answerType === ChatAnswerType.LONG)
|
72
|
+
})
|
73
|
+
|
74
|
+
// check response status
|
75
|
+
if (ResponseUtils.isFail(response)) {
|
76
|
+
await ResponseUtils.throwError(`Failed to ask a question, organization: ${this.organization.id}`, response)
|
77
|
+
}
|
78
|
+
|
79
|
+
// parse answer id from the server's response
|
80
|
+
const id = (await response!.json()).id
|
81
|
+
|
82
|
+
// create answer implementation
|
83
|
+
const answer = await new AnswerImpl(this, this.context).initFromId(id)
|
84
|
+
|
85
|
+
// add answer to the collection
|
86
|
+
this._answers.push(answer)
|
87
|
+
|
88
|
+
return answer
|
89
|
+
}
|
90
|
+
|
91
|
+
async update(): Promise<void>{
|
92
|
+
const response = await this.context
|
93
|
+
.resolve(RpcService)
|
94
|
+
?.requestBuilder("api/v1/Chats")
|
95
|
+
.searchParam("id", this.id)
|
96
|
+
.sendGet()
|
97
|
+
|
98
|
+
// check response status
|
99
|
+
if (ResponseUtils.isFail(response)) {
|
100
|
+
await ResponseUtils.throwError(`Failed to update chat ${this.id}`, response)
|
101
|
+
}
|
102
|
+
|
103
|
+
const chat = (await response!.json()).chat as ChatDto
|
104
|
+
|
105
|
+
this._content = chat
|
106
|
+
|
107
|
+
for (const ans of chat.answers) {
|
108
|
+
let answer = this._answers.find(answer => answer.id === ans.id)
|
109
|
+
if (!answer){
|
110
|
+
// create answer implementation
|
111
|
+
answer = new AnswerImpl(this, this.context).initFromData(ans)
|
112
|
+
}else{
|
113
|
+
this._answers.splice(this._answers.indexOf(answer), 1)
|
114
|
+
|
115
|
+
answer.initFromData(ans)
|
116
|
+
}
|
117
|
+
// add answer to the collection
|
118
|
+
this._answers.push(answer)
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
dispose(): void {
|
123
|
+
this._isDisposed = true
|
124
|
+
}
|
125
|
+
|
126
|
+
}
|
@@ -0,0 +1,49 @@
|
|
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
|
+
* Get answer by id
|
35
|
+
* @param id answer id
|
36
|
+
*/
|
37
|
+
abstract getAnswer(id: string): Answer
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Ask new question in chat.
|
41
|
+
*/
|
42
|
+
abstract ask(message: string, answerType: ChatAnswerType): Promise<Answer>
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Update chat
|
46
|
+
*/
|
47
|
+
abstract update(): Promise<void>
|
48
|
+
}
|
49
|
+
|
@@ -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,87 @@
|
|
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
|
+
import { FilesEvent } from "./files"
|
8
|
+
|
9
|
+
export class FileImpl extends File implements Disposable {
|
10
|
+
private _isDisposed: boolean = false
|
11
|
+
private _content?: FileDto
|
12
|
+
private _status?: FileProgressDto
|
13
|
+
|
14
|
+
|
15
|
+
constructor(private readonly context: Context) {
|
16
|
+
super()
|
17
|
+
}
|
18
|
+
|
19
|
+
public initFrom(file: FileDto): File {
|
20
|
+
this._content = file
|
21
|
+
|
22
|
+
return this
|
23
|
+
}
|
24
|
+
|
25
|
+
get isDisposed(): boolean {
|
26
|
+
return this._isDisposed
|
27
|
+
}
|
28
|
+
|
29
|
+
dispose(): void {
|
30
|
+
this._isDisposed = true
|
31
|
+
}
|
32
|
+
|
33
|
+
get id(): string {
|
34
|
+
return <string>this._content?.id
|
35
|
+
}
|
36
|
+
|
37
|
+
get name(): string {
|
38
|
+
return <string>this._content?.name
|
39
|
+
}
|
40
|
+
|
41
|
+
get createdAt(): number {
|
42
|
+
return <number>this._content?.createdAt
|
43
|
+
}
|
44
|
+
|
45
|
+
get status(): FileProgressDto {
|
46
|
+
return <FileProgressDto>this._status
|
47
|
+
}
|
48
|
+
|
49
|
+
async url(): Promise<string> {
|
50
|
+
const response = await this.context
|
51
|
+
.resolve(RpcService)
|
52
|
+
?.requestBuilder("api/v1/Files/url")
|
53
|
+
.searchParam("id", this.id)
|
54
|
+
.sendGet()
|
55
|
+
|
56
|
+
if (ResponseUtils.isFail(response)) {
|
57
|
+
await ResponseUtils.throwError(
|
58
|
+
`Failed to get file ${this.id} url`,
|
59
|
+
response
|
60
|
+
)
|
61
|
+
}
|
62
|
+
|
63
|
+
return (await response!.json()).url
|
64
|
+
}
|
65
|
+
|
66
|
+
async updateStatus(): Promise<void> {
|
67
|
+
const response = await this.context
|
68
|
+
.resolve(RpcService)
|
69
|
+
?.requestBuilder("api/v1/Files/fetch")
|
70
|
+
.searchParam("id", this.id)
|
71
|
+
.sendGet()
|
72
|
+
|
73
|
+
if (ResponseUtils.isFail(response)) {
|
74
|
+
await ResponseUtils.throwError(`Failed to get file ${this.id}`, response)
|
75
|
+
}
|
76
|
+
|
77
|
+
this._status = (await response!.json()).progress as FileProgressDto
|
78
|
+
|
79
|
+
|
80
|
+
// dispatch event, file updated
|
81
|
+
this.dispatch({
|
82
|
+
type: FilesEvent.UPDATED,
|
83
|
+
data: this
|
84
|
+
})
|
85
|
+
|
86
|
+
}
|
87
|
+
}
|