@neuralinnovations/dataisland-sdk 0.0.1-dev7 → 0.0.1-dev9

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 (79) hide show
  1. package/.github/workflows/docs.yml +32 -0
  2. package/.github/workflows/format.yml +42 -0
  3. package/.github/workflows/publish-npm.yml +19 -16
  4. package/.github/workflows/tests.yml +38 -0
  5. package/.github/workflows/version.yml +44 -0
  6. package/LICENSE +201 -0
  7. package/README.md +37 -2
  8. package/docs/classes/BasicCredential.md +1 -1
  9. package/docs/classes/BearerCredential.md +1 -1
  10. package/docs/classes/Chat.md +34 -6
  11. package/docs/classes/Chats.md +87 -1
  12. package/docs/classes/CredentialBase.md +1 -1
  13. package/docs/classes/DataIslandApp.md +1 -1
  14. package/docs/classes/DebugCredential.md +1 -1
  15. package/docs/classes/DefaultCredential.md +1 -1
  16. package/docs/classes/DisposableContainer.md +1 -1
  17. package/docs/classes/EventDispatcher.md +1 -1
  18. package/docs/classes/File.md +1 -1
  19. package/docs/classes/Files.md +2 -2
  20. package/docs/classes/FilesPage.md +1 -1
  21. package/docs/classes/Group.md +19 -1
  22. package/docs/classes/Groups.md +27 -3
  23. package/docs/classes/Lifetime.md +1 -1
  24. package/docs/classes/Organization.md +37 -1
  25. package/docs/classes/Organizations.md +1 -1
  26. package/docs/classes/UserProfile.md +1 -1
  27. package/docs/classes/Workspace.md +1 -1
  28. package/docs/classes/Workspaces.md +6 -2
  29. package/docs/enums/ChatAnswerType.md +22 -0
  30. package/docs/enums/ChatsEvent.md +1 -1
  31. package/docs/enums/FilesEvent.md +1 -1
  32. package/docs/enums/GroupEvent.md +3 -1
  33. package/docs/enums/OrganizationsEvent.md +1 -1
  34. package/docs/enums/UserEvent.md +1 -1
  35. package/docs/enums/WorkspaceEvent.md +1 -1
  36. package/docs/enums/WorkspacesEvent.md +1 -1
  37. package/docs/interfaces/Disposable.md +1 -1
  38. package/docs/interfaces/Event.md +1 -1
  39. package/docs/interfaces/EventSubscriber.md +1 -1
  40. package/docs/interfaces/Input.md +1 -1
  41. package/docs/modules.md +5 -3
  42. package/package.json +6 -2
  43. package/src/dataIslandApp.ts +2 -2
  44. package/src/dto/chatResponse.ts +54 -55
  45. package/src/dto/workspacesResponse.ts +2 -2
  46. package/src/index.ts +13 -13
  47. package/src/internal/app.impl.ts +2 -2
  48. package/src/services/organizationService.ts +2 -2
  49. package/src/services/userProfileService.ts +2 -2
  50. package/src/storages/chats/answer.impl.ts +163 -0
  51. package/src/storages/chats/answer.ts +42 -0
  52. package/src/storages/chats/chat.impl.ts +87 -0
  53. package/src/storages/chats/chat.ts +38 -0
  54. package/src/storages/chats/chats.impl.ts +142 -0
  55. package/src/storages/chats/chats.ts +47 -0
  56. package/src/storages/{file.impl.ts → files/file.impl.ts} +5 -5
  57. package/src/storages/{file.ts → files/file.ts} +1 -1
  58. package/src/storages/{files.impl.ts → files/files.impl.ts} +6 -6
  59. package/src/storages/{files.ts → files/files.ts} +2 -2
  60. package/src/storages/{groups.impl.ts → groups/groups.impl.ts} +86 -97
  61. package/src/storages/groups/groups.ts +101 -0
  62. package/src/storages/{organization.impl.ts → organizations/organization.impl.ts} +34 -7
  63. package/src/storages/{organization.ts → organizations/organization.ts} +13 -2
  64. package/src/storages/{organizations.impl.ts → organizations/organizations.impl.ts} +10 -4
  65. package/src/storages/{organizations.ts → organizations/organizations.ts} +1 -1
  66. package/src/storages/{userProfile.impl.ts → user/userProfile.impl.ts} +1 -1
  67. package/src/storages/{userProfile.ts → user/userProfile.ts} +1 -1
  68. package/src/storages/{workspace.impl.ts → workspaces/workspace.impl.ts} +7 -7
  69. package/src/storages/{workspace.ts → workspaces/workspace.ts} +3 -3
  70. package/src/storages/{workspaces.impl.ts → workspaces/workspaces.impl.ts} +11 -11
  71. package/src/storages/{workspaces.ts → workspaces/workspaces.ts} +2 -2
  72. package/test/chats.test.ts +48 -0
  73. package/test/organization.test.ts +13 -1
  74. package/test/setup.ts +7 -0
  75. package/docs/enums/ChatAnswer.md +0 -22
  76. package/src/storages/chat.ts +0 -21
  77. package/src/storages/chats.ts +0 -17
  78. package/src/storages/groups.ts +0 -43
  79. /package/src/storages/{filesPage.ts → files/filesPage.ts} +0 -0
@@ -1,88 +1,87 @@
1
-
2
1
  export interface SourceDto {
3
- id: string;
4
- name: string;
5
- url: string;
6
- content: string;
7
- page: number;
2
+ id: string;
3
+ name: string;
4
+ url: string;
5
+ content: string;
6
+ page: number;
8
7
  }
9
8
 
10
9
  export interface AnswerDto {
11
- id: string;
12
- chatId: string;
13
- question: string;
14
- context: string;
15
- sources: SourceDto[];
16
- timestamp: number;
10
+ id: string;
11
+ chatId: string;
12
+ question: string;
13
+ context: string;
14
+ sources: SourceDto[];
15
+ timestamp: number;
17
16
  }
18
17
 
19
18
  export interface ChatDto {
20
- id: string;
21
- name: string;
22
- createdAt: number;
23
- modifiedAt: number;
24
- userId: string;
25
- organizationId: string;
26
- workspaceId: string;
27
- answers: AnswerDto[];
19
+ id: string;
20
+ name: string;
21
+ createdAt: number;
22
+ modifiedAt: number;
23
+ userId: string;
24
+ organizationId: string;
25
+ workspaceId: string;
26
+ answers: AnswerDto[];
28
27
  }
29
28
 
30
29
  export interface ChatListResponse {
31
- chats: ChatDto[]
30
+ chats: ChatDto[]
32
31
  }
33
32
 
34
33
  export enum AnswerStatus {
35
- RUNNING = 0,
36
- SUCCESS = 1,
37
- CANCELED = 2,
38
- FAIL = 3,
34
+ RUNNING = 0,
35
+ SUCCESS = 1,
36
+ CANCELED = 2,
37
+ FAIL = 3,
39
38
  }
40
39
 
41
- export interface AnswerStepDto{
42
- id: string;
43
- type: StepType;
44
- status: StepStatus;
45
- start_at: string;
46
- end_at: string;
47
- tokens: string[];
48
- sources: SourceDto[];
40
+ export interface AnswerStepDto {
41
+ id: string;
42
+ type: StepType;
43
+ status: StepStatus;
44
+ start_at: string;
45
+ end_at: string;
46
+ tokens: string[];
47
+ sources: SourceDto[];
49
48
  }
50
49
 
51
50
  export interface FetchAnswerResponse {
52
- id: string;
53
- status: AnswerStatus;
54
- steps: AnswerStepDto[];
51
+ id: string;
52
+ status: AnswerStatus;
53
+ steps: AnswerStepDto[];
55
54
  }
56
55
 
57
56
  export interface FetchTokensResponse {
58
- id: string;
59
- step_id: string;
60
- step_status: number;
61
- step_tokens: string[];
57
+ id: string;
58
+ step_id: string;
59
+ step_status: number;
60
+ step_tokens: string[];
62
61
  }
63
62
 
64
63
  export interface AnswerSourcesResponse {
65
- chat_uid: string;
66
- uid: string;
67
- step_id: string;
68
- sources: SourceDto[];
64
+ chat_uid: string;
65
+ uid: string;
66
+ step_id: string;
67
+ sources: SourceDto[];
69
68
  }
70
69
 
71
70
  export enum StepStatus {
72
- RUNNING = 0,
73
- SUCCESS = 1,
74
- FAIL = 2,
75
- CANCELED = 3,
71
+ RUNNING = 0,
72
+ SUCCESS = 1,
73
+ FAIL = 2,
74
+ CANCELED = 3,
76
75
  }
77
76
 
78
77
  export enum StepType {
79
- PREPARE = 0,
80
- SOURCES = 1,
81
- GENERATE_ANSWER = 6,
82
- FINALIZE_RESULT = 9,
83
- DONE = 10,
78
+ PREPARE = 0,
79
+ SOURCES = 1,
80
+ GENERATE_ANSWER = 6,
81
+ FINALIZE_RESULT = 9,
82
+ DONE = 10,
84
83
  }
85
-
84
+
86
85
  export class StepTypeInfo {
87
86
  public static hasTokens(type: StepType): boolean {
88
87
  switch (type) {
@@ -101,4 +100,4 @@ export class StepTypeInfo {
101
100
  }
102
101
  return false
103
102
  }
104
- }
103
+ }
@@ -1,5 +1,5 @@
1
- import { WorkspaceId } from "../storages/workspaces"
2
- import { FileId } from "../storages/file"
1
+ import { WorkspaceId } from "../storages/workspaces/workspaces"
2
+ import { FileId } from "../storages/files/file"
3
3
 
4
4
  export interface WorkspaceProfileDto {
5
5
  name: string
package/src/index.ts CHANGED
@@ -7,17 +7,17 @@ export * from "./events"
7
7
  export * from "./disposable"
8
8
  export * from "./credentials"
9
9
  export * from "./dataIslandApp"
10
- export * from "./storages/organizations"
11
- export * from "./storages/organization"
12
- export * from "./storages/workspaces"
13
- export * from "./storages/workspace"
14
- export * from "./storages/groups"
15
- export * from "./storages/userProfile"
16
- export * from "./storages/files"
17
- export * from "./storages/file"
18
- export * from "./storages/filesPage"
19
- export * from "./storages/chats"
20
- export * from "./storages/chat"
10
+ export * from "./storages/organizations/organizations"
11
+ export * from "./storages/organizations/organization"
12
+ export * from "./storages/workspaces/workspaces"
13
+ export * from "./storages/workspaces/workspace"
14
+ export * from "./storages/groups/groups"
15
+ export * from "./storages/user/userProfile"
16
+ export * from "./storages/files/files"
17
+ export * from "./storages/files/file"
18
+ export * from "./storages/files/filesPage"
19
+ export * from "./storages/chats/chats"
20
+ export * from "./storages/chats/chat"
21
21
 
22
22
  const _appsNotReady = new Map<string, Promise<DataIslandApp>>()
23
23
  const _appsReady = new Map<string, DataIslandApp>()
@@ -88,5 +88,5 @@ export async function dataIslandApp(
88
88
  return await appPromise
89
89
  }
90
90
 
91
- export { File } from "./storages/file"
92
- export { FilesPage } from "./storages/filesPage"
91
+ export { File } from "./storages/files/file"
92
+ export { FilesPage } from "./storages/files/filesPage"
@@ -17,8 +17,8 @@ import {
17
17
  } from "../commands/startCommandHandler"
18
18
  import { UserProfileService } from "../services/userProfileService"
19
19
  import { OrganizationService } from "../services/organizationService"
20
- import { Organizations } from "../storages/organizations"
21
- import { UserProfile } from "../storages/userProfile"
20
+ import { Organizations } from "../storages/organizations/organizations"
21
+ import { UserProfile } from "../storages/user/userProfile"
22
22
  import { isUnitTest, UnitTest } from "../unitTest"
23
23
 
24
24
  export class DataIslandAppImpl extends DataIslandApp {
@@ -1,7 +1,7 @@
1
1
  import { Service } from "./service"
2
- import { Organizations } from "../storages/organizations"
2
+ import { Organizations } from "../storages/organizations/organizations"
3
3
  import { OrganizationDto, UserSettings } from "../dto/userInfoResponse"
4
- import { OrganizationsImpl } from "../storages/organizations.impl"
4
+ import { OrganizationsImpl } from "../storages/organizations/organizations.impl"
5
5
 
6
6
  export class OrganizationService extends Service {
7
7
  private _impl?: OrganizationsImpl
@@ -1,9 +1,9 @@
1
1
  import { Service } from "./service"
2
2
  import { RpcService } from "./rpcService"
3
- import { UserProfile } from "../storages/userProfile"
3
+ import { UserProfile } from "../storages/user/userProfile"
4
4
  import { UserInfoResponse } from "../dto/userInfoResponse"
5
5
  import { OrganizationService } from "./organizationService"
6
- import { UserProfileImpl } from "../storages/userProfile.impl"
6
+ import { UserProfileImpl } from "../storages/user/userProfile.impl"
7
7
  import { ResponseUtils } from "./responseUtils"
8
8
 
9
9
  export class UserProfileService extends Service {
@@ -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
+