@coasys/ad4m 0.12.0-rc1-dev.2 → 0.12.0-rc1-dev.4

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 (153) hide show
  1. package/jest.config.js +10 -3
  2. package/lib/index.cjs +6893 -5884
  3. package/lib/index.js +6889 -5872
  4. package/lib/src/Ad4mClient.d.ts +9 -16
  5. package/lib/src/Ad4mClient.js +46 -25
  6. package/lib/src/Literal.d.ts +1 -2
  7. package/lib/src/Literal.js +38 -35
  8. package/lib/src/SmartLiteral.d.ts +1 -2
  9. package/lib/src/SmartLiteral.js +30 -15
  10. package/lib/src/agent/AgentClient.d.ts +1 -4
  11. package/lib/src/agent/AgentClient.js +79 -99
  12. package/lib/src/ai/AIClient.d.ts +1 -2
  13. package/lib/src/ai/AIClient.js +224 -220
  14. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  15. package/lib/src/expression/ExpressionClient.js +73 -86
  16. package/lib/src/index.d.ts +1 -1
  17. package/lib/src/index.js +1 -1
  18. package/lib/src/language/LanguageClient.d.ts +1 -1
  19. package/lib/src/language/LanguageClient.js +52 -52
  20. package/lib/src/model/Ad4mModel.d.ts +1351 -144
  21. package/lib/src/model/Ad4mModel.js +3760 -228
  22. package/lib/src/model/Ad4mModel.test.js +522 -979
  23. package/lib/src/model/Subject.d.ts +3 -3
  24. package/lib/src/model/Subject.js +25 -25
  25. package/lib/src/model/decorators.d.ts +444 -100
  26. package/lib/src/model/decorators.js +686 -321
  27. package/lib/src/model/util.d.ts +6 -6
  28. package/lib/src/model/util.js +24 -24
  29. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +1 -5
  30. package/lib/src/neighbourhood/NeighbourhoodClient.js +179 -242
  31. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +1 -2
  32. package/lib/src/neighbourhood/NeighbourhoodProxy.js +28 -13
  33. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +5 -1
  34. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  35. package/lib/src/perspectives/PerspectiveClient.js +240 -316
  36. package/lib/src/perspectives/PerspectiveProxy.d.ts +99 -61
  37. package/lib/src/perspectives/PerspectiveProxy.js +485 -832
  38. package/lib/src/runtime/RuntimeClient.d.ts +1 -5
  39. package/lib/src/runtime/RuntimeClient.js +238 -412
  40. package/lib/src/shacl/SHACLFlow.test.js +105 -164
  41. package/lib/src/shacl/SHACLShape.d.ts +12 -17
  42. package/lib/src/shacl/SHACLShape.js +115 -128
  43. package/lib/src/shacl/SHACLShape.test.js +176 -244
  44. package/lib/src/shims/reflect-metadata.d.ts +0 -0
  45. package/lib/src/shims/type-graphql.d.ts +32 -0
  46. package/lib/src/src/Ad4mClient.d.ts +28 -0
  47. package/lib/src/src/Address.d.ts +1 -0
  48. package/lib/src/src/DID.d.ts +1 -0
  49. package/lib/src/src/Exception.d.ts +7 -0
  50. package/lib/src/src/Literal.d.ts +7 -0
  51. package/lib/src/src/PubSub.d.ts +17 -0
  52. package/lib/src/src/SmartLiteral.d.ts +12 -0
  53. package/lib/src/src/agent/Agent.d.ts +110 -0
  54. package/lib/src/src/agent/AgentClient.d.ts +62 -0
  55. package/lib/src/src/agent/AgentResolver.d.ts +30 -0
  56. package/lib/src/src/agent/AgentStatus.d.ts +8 -0
  57. package/lib/src/src/ai/AIClient.d.ts +32 -0
  58. package/lib/src/src/ai/AIResolver.d.ts +75 -0
  59. package/lib/src/src/ai/Tasks.d.ts +37 -0
  60. package/lib/src/src/buildSchema.d.ts +1 -0
  61. package/lib/src/src/expression/Expression.d.ts +28 -0
  62. package/lib/src/src/expression/ExpressionClient.d.ts +13 -0
  63. package/lib/src/src/expression/ExpressionRef.d.ts +9 -0
  64. package/lib/src/src/expression/ExpressionResolver.d.ts +10 -0
  65. package/lib/src/src/index.d.ts +35 -0
  66. package/lib/src/src/jestSetup.d.ts +1 -0
  67. package/lib/src/src/language/Icon.d.ts +4 -0
  68. package/lib/src/src/language/Language.d.ts +206 -0
  69. package/lib/src/src/language/LanguageClient.d.ts +17 -0
  70. package/lib/src/src/language/LanguageContext.d.ts +37 -0
  71. package/lib/src/src/language/LanguageHandle.d.ts +9 -0
  72. package/lib/src/src/language/LanguageMeta.d.ts +35 -0
  73. package/lib/src/src/language/LanguageRef.d.ts +6 -0
  74. package/lib/src/src/language/LanguageResolver.d.ts +19 -0
  75. package/lib/src/src/links/Links.d.ts +39 -0
  76. package/lib/src/src/links/Links.test.d.ts +1 -0
  77. package/lib/src/src/model/Ad4mModel.d.ts +1438 -0
  78. package/lib/src/src/model/Ad4mModel.test.d.ts +1 -0
  79. package/lib/src/src/model/Subject.d.ts +25 -0
  80. package/lib/src/src/model/decorators.d.ts +545 -0
  81. package/lib/src/src/model/util.d.ts +12 -0
  82. package/lib/src/src/neighbourhood/Neighbourhood.d.ts +10 -0
  83. package/lib/src/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
  84. package/lib/src/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
  85. package/lib/src/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
  86. package/lib/src/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
  87. package/lib/src/src/perspectives/LinkQuery.d.ts +11 -0
  88. package/lib/src/src/perspectives/Perspective.d.ts +38 -0
  89. package/lib/src/src/perspectives/Perspective.test.d.ts +1 -0
  90. package/lib/src/src/perspectives/PerspectiveClient.d.ts +97 -0
  91. package/lib/src/src/perspectives/PerspectiveDiff.d.ts +9 -0
  92. package/lib/src/src/perspectives/PerspectiveHandle.d.ts +18 -0
  93. package/lib/src/src/perspectives/PerspectiveProxy.d.ts +865 -0
  94. package/lib/src/src/perspectives/PerspectiveResolver.d.ts +57 -0
  95. package/lib/src/src/runtime/RuntimeClient.d.ts +56 -0
  96. package/lib/src/src/runtime/RuntimeResolver.d.ts +132 -0
  97. package/lib/src/src/shacl/SHACLFlow.d.ts +171 -0
  98. package/lib/src/src/shacl/SHACLFlow.test.d.ts +1 -0
  99. package/lib/src/src/shacl/SHACLShape.d.ts +117 -0
  100. package/lib/src/src/shacl/SHACLShape.test.d.ts +1 -0
  101. package/lib/src/src/typeDefs.d.ts +1 -0
  102. package/lib/src/src/unwrapApolloResult.d.ts +2 -0
  103. package/lib/src/src/utils.d.ts +25 -0
  104. package/lib/src/utils.d.ts +0 -8
  105. package/lib/src/utils.js +7 -21
  106. package/package.json +15 -14
  107. package/src/Ad4mClient.test.ts +1315 -1564
  108. package/src/Ad4mClient.ts +54 -55
  109. package/src/Literal.test.ts +26 -45
  110. package/src/Literal.ts +66 -82
  111. package/src/SmartLiteral.ts +58 -74
  112. package/src/agent/AgentClient.ts +99 -144
  113. package/src/ai/AIClient.ts +359 -409
  114. package/src/expression/ExpressionClient.ts +96 -142
  115. package/src/index.ts +1 -1
  116. package/src/language/LanguageClient.ts +113 -137
  117. package/src/model/Ad4mModel.test.ts +616 -1114
  118. package/src/model/Ad4mModel.ts +4386 -451
  119. package/src/model/Subject.ts +159 -0
  120. package/src/model/decorators.ts +1168 -667
  121. package/src/model/util.ts +54 -53
  122. package/src/neighbourhood/NeighbourhoodClient.ts +266 -392
  123. package/src/neighbourhood/NeighbourhoodProxy.test.ts +7 -3
  124. package/src/neighbourhood/NeighbourhoodProxy.ts +40 -58
  125. package/src/perspectives/PerspectiveClient.ts +540 -779
  126. package/src/perspectives/PerspectiveProxy.ts +2067 -2860
  127. package/src/runtime/RuntimeClient.ts +476 -775
  128. package/src/shacl/SHACLFlow.test.ts +129 -196
  129. package/src/shacl/SHACLShape.test.ts +190 -284
  130. package/src/shacl/SHACLShape.ts +200 -224
  131. package/src/utils.ts +17 -31
  132. package/tsconfig.json +10 -9
  133. package/src/model/IMPROVEMENTS.md +0 -482
  134. package/src/model/PREDICATE_COLLISION_FIX.md +0 -651
  135. package/src/model/README.md +0 -317
  136. package/src/model/SHACL_MUTATION_DECOUPLING.md +0 -356
  137. package/src/model/SUBSCRIPTION_STRATEGY.md +0 -317
  138. package/src/model/TC39_STAGE_3_DECORATORS.md +0 -609
  139. package/src/model/mutation.ts +0 -461
  140. package/src/model/parentUtils.ts +0 -91
  141. package/src/model/prolog/generatePrologFacts.ts +0 -176
  142. package/src/model/prolog/index.ts +0 -1
  143. package/src/model/query/ModelQueryBuilder.ts +0 -173
  144. package/src/model/query/fetchInstance.ts +0 -181
  145. package/src/model/query/hydration.ts +0 -273
  146. package/src/model/query/operations.ts +0 -510
  147. package/src/model/query/snapshot.ts +0 -126
  148. package/src/model/query/surrealCompiler.ts +0 -376
  149. package/src/model/schema/fromJSONSchema.ts +0 -411
  150. package/src/model/schema/metadata.ts +0 -148
  151. package/src/model/subscription.ts +0 -307
  152. package/src/model/transaction.ts +0 -60
  153. package/src/model/types.ts +0 -207
@@ -1,425 +1,375 @@
1
1
  import { ApolloClient, gql } from "@apollo/client";
2
2
  import unwrapApolloResult from "../unwrapApolloResult";
3
- import { isSocketCloseError } from "../utils";
4
- import base64js from "base64-js";
5
- import pako from "pako";
3
+ import base64js from 'base64-js';
4
+ import pako from 'pako'
6
5
  import { AIModelLoadingStatus, AITask, AITaskInput } from "./Tasks";
7
- import { ModelInput, Model, ModelType } from "./AIResolver";
6
+ import { ModelInput, Model, ModelType } from "./AIResolver"
8
7
 
9
8
  export class AIClient {
10
- private _apolloClient: ApolloClient<any>;
11
- private _transcriptionSubscriptions: Map<string, any> = new Map();
12
-
13
- constructor(apolloClient: ApolloClient<any>, subscribe: boolean = true) {
14
- this._apolloClient = apolloClient;
15
- }
16
-
17
- async getModels(): Promise<Model[]> {
18
- const result = await this._apolloClient.query({
19
- query: gql`
20
- query {
21
- aiGetModels {
22
- id
23
- name
24
- api {
25
- baseUrl
26
- apiKey
27
- model
28
- apiType
9
+ #apolloClient: ApolloClient<any>;
10
+ #transcriptionSubscriptions: Map<string, any> = new Map();
11
+
12
+ constructor(apolloClient: ApolloClient<any>, subscribe: boolean = true) {
13
+ this.#apolloClient = apolloClient;
14
+ }
15
+
16
+ async getModels(): Promise<Model[]> {
17
+ const result = await this.#apolloClient.query({
18
+ query: gql`
19
+ query {
20
+ aiGetModels {
21
+ id
22
+ name
23
+ api {
24
+ baseUrl
25
+ apiKey
26
+ model
27
+ apiType
28
+ }
29
+ local {
30
+ fileName
31
+ tokenizerSource {
32
+ repo
33
+ revision
34
+ fileName
35
+ }
36
+ huggingfaceRepo
37
+ revision
38
+ }
39
+ modelType
40
+ }
41
+ }
42
+ `
43
+ });
44
+ return unwrapApolloResult(result).aiGetModels;
45
+ }
46
+
47
+ async addModel(model: ModelInput): Promise<string> {
48
+ const result = await this.#apolloClient.mutate({
49
+ mutation: gql`
50
+ mutation($model: ModelInput!) {
51
+ aiAddModel(model: $model)
52
+ }
53
+ `,
54
+ variables: { model }
55
+ });
56
+ return unwrapApolloResult(result).aiAddModel;
57
+ }
58
+
59
+ async updateModel(modelId: string, model: ModelInput): Promise<boolean> {
60
+ const result = await this.#apolloClient.mutate({
61
+ mutation: gql`
62
+ mutation($modelId: String!, $model: ModelInput!) {
63
+ aiUpdateModel(modelId: $modelId, model: $model)
64
+ }
65
+ `,
66
+ variables: { modelId, model }
67
+ });
68
+ return unwrapApolloResult(result).aiUpdateModel;
69
+ }
70
+
71
+ async removeModel(modelId: string): Promise<boolean> {
72
+ const result = await this.#apolloClient.mutate({
73
+ mutation: gql`
74
+ mutation($modelId: String!) {
75
+ aiRemoveModel(modelId: $modelId)
76
+ }
77
+ `,
78
+ variables: { modelId }
79
+ });
80
+ return unwrapApolloResult(result).aiRemoveModel;
81
+ }
82
+
83
+ async setDefaultModel(modelType: ModelType, modelId: string): Promise<boolean> {
84
+ const result = await this.#apolloClient.mutate({
85
+ mutation: gql`
86
+ mutation($modelType: ModelType!, $modelId: String!) {
87
+ aiSetDefaultModel(modelType: $modelType modelId: $modelId)
88
+ }
89
+ `,
90
+ variables: { modelId, modelType }
91
+ });
92
+ return unwrapApolloResult(result).aiSetDefaultModel;
93
+ }
94
+
95
+ async getDefaultModel(modelType: ModelType): Promise<Model> {
96
+ const result = await this.#apolloClient.query({
97
+ query: gql`
98
+ query($modelType: ModelType!) {
99
+ aiGetDefaultModel(modelType: $modelType) {
100
+ id
101
+ name
102
+ api {
103
+ baseUrl
104
+ apiKey
105
+ model
106
+ apiType
107
+ }
108
+ local {
109
+ fileName
110
+ tokenizerSource {
111
+ repo
112
+ revision
113
+ fileName
114
+ }
115
+ huggingfaceRepo
116
+ revision
117
+ }
118
+ modelType
119
+ }
120
+ }
121
+ `,
122
+ variables: { modelType }
123
+ });
124
+ return unwrapApolloResult(result).aiGetDefaultModel;
125
+ }
126
+
127
+ async tasks(): Promise<AITask[]> {
128
+ const { aiTasks } = unwrapApolloResult(await this.#apolloClient.query({
129
+ query: gql`
130
+ query {
131
+ aiTasks {
132
+ name
133
+ modelId
134
+ taskId
135
+ systemPrompt
136
+ promptExamples {
137
+ input
138
+ output
139
+ }
140
+ metaData
141
+ createdAt
142
+ updatedAt
143
+ }
144
+ }
145
+ `
146
+ }));
147
+
148
+ return aiTasks;
149
+ }
150
+
151
+ async addTask(name: string, modelId: string, systemPrompt: string, promptExamples: { input: string, output: string }[], metaData?: string): Promise<AITask> {
152
+ const task = new AITaskInput(name, modelId, systemPrompt, promptExamples, metaData);
153
+ const { aiAddTask } = unwrapApolloResult(await this.#apolloClient.mutate({
154
+ mutation: gql`
155
+ mutation AiAddTask($task: AITaskInput!) {
156
+ aiAddTask(task: $task) {
157
+ name
158
+ modelId
159
+ taskId
160
+ systemPrompt
161
+ promptExamples {
162
+ input
163
+ output
164
+ }
165
+ metaData
166
+ createdAt
167
+ updatedAt
168
+ }
169
+ }
170
+ `,
171
+ variables: {
172
+ task
29
173
  }
30
- local {
31
- fileName
32
- tokenizerSource {
33
- repo
34
- revision
35
- fileName
36
- }
37
- huggingfaceRepo
38
- revision
174
+ }));
175
+
176
+ return aiAddTask;
177
+ }
178
+
179
+ async removeTask(taskId: string): Promise<AITask> {
180
+ const { aiRemoveTask } = unwrapApolloResult(await this.#apolloClient.mutate({
181
+ mutation: gql`
182
+ mutation AiRemoveTask($taskId: String!) {
183
+ aiRemoveTask(taskId: $taskId) {
184
+ name
185
+ modelId
186
+ taskId
187
+ systemPrompt
188
+ promptExamples {
189
+ input
190
+ output
191
+ }
192
+ metaData
193
+ createdAt
194
+ updatedAt
195
+ }
196
+ }
197
+ `,
198
+ variables: {
199
+ taskId
39
200
  }
40
- modelType
41
- }
42
- }
43
- `,
44
- });
45
- return unwrapApolloResult(result).aiGetModels;
46
- }
47
-
48
- async addModel(model: ModelInput): Promise<string> {
49
- const result = await this._apolloClient.mutate({
50
- mutation: gql`
51
- mutation ($model: ModelInput!) {
52
- aiAddModel(model: $model)
53
- }
54
- `,
55
- variables: { model },
56
- });
57
- return unwrapApolloResult(result).aiAddModel;
58
- }
59
-
60
- async updateModel(modelId: string, model: ModelInput): Promise<boolean> {
61
- const result = await this._apolloClient.mutate({
62
- mutation: gql`
63
- mutation ($modelId: String!, $model: ModelInput!) {
64
- aiUpdateModel(modelId: $modelId, model: $model)
65
- }
66
- `,
67
- variables: { modelId, model },
68
- });
69
- return unwrapApolloResult(result).aiUpdateModel;
70
- }
71
-
72
- async removeModel(modelId: string): Promise<boolean> {
73
- const result = await this._apolloClient.mutate({
74
- mutation: gql`
75
- mutation ($modelId: String!) {
76
- aiRemoveModel(modelId: $modelId)
77
- }
78
- `,
79
- variables: { modelId },
80
- });
81
- return unwrapApolloResult(result).aiRemoveModel;
82
- }
83
-
84
- async setDefaultModel(
85
- modelType: ModelType,
86
- modelId: string,
87
- ): Promise<boolean> {
88
- const result = await this._apolloClient.mutate({
89
- mutation: gql`
90
- mutation ($modelType: ModelType!, $modelId: String!) {
91
- aiSetDefaultModel(modelType: $modelType, modelId: $modelId)
92
- }
93
- `,
94
- variables: { modelId, modelType },
95
- });
96
- return unwrapApolloResult(result).aiSetDefaultModel;
97
- }
98
-
99
- async getDefaultModel(modelType: ModelType): Promise<Model> {
100
- const result = await this._apolloClient.query({
101
- query: gql`
102
- query ($modelType: ModelType!) {
103
- aiGetDefaultModel(modelType: $modelType) {
104
- id
105
- name
106
- api {
107
- baseUrl
108
- apiKey
109
- model
110
- apiType
201
+ }));
202
+
203
+ return aiRemoveTask;
204
+ }
205
+
206
+ async updateTask(taskId: string, task: AITask): Promise<AITask> {
207
+ const { aiUpdateTask } = unwrapApolloResult(await this.#apolloClient.mutate({
208
+ mutation: gql`
209
+ mutation AiUpdateTask($taskId: String!, $task: AITaskInput!) {
210
+ aiUpdateTask(taskId: $taskId, task: $task) {
211
+ name
212
+ modelId
213
+ taskId
214
+ systemPrompt
215
+ promptExamples {
216
+ input
217
+ output
218
+ }
219
+ metaData
220
+ createdAt
221
+ updatedAt
222
+ }
223
+ }
224
+ `,
225
+ variables: {
226
+ taskId,
227
+ task: {
228
+ name: task.name,
229
+ modelId: task.modelId,
230
+ systemPrompt: task.systemPrompt,
231
+ promptExamples: task.promptExamples
232
+ }
111
233
  }
112
- local {
113
- fileName
114
- tokenizerSource {
115
- repo
116
- revision
117
- fileName
118
- }
119
- huggingfaceRepo
120
- revision
234
+ }));
235
+
236
+ return aiUpdateTask;
237
+ }
238
+
239
+ async modelLoadingStatus(model: string): Promise<AIModelLoadingStatus> {
240
+ const { aiModelLoadingStatus } = unwrapApolloResult(await this.#apolloClient.query({
241
+ query: gql`
242
+ query AiModelLoadingStatus($model: String!) {
243
+ aiModelLoadingStatus(model: $model) {
244
+ model
245
+ status
246
+ progress
247
+ loaded
248
+ downloaded
249
+ }
250
+ }
251
+ `,
252
+ variables: {
253
+ model
121
254
  }
122
- modelType
123
- }
124
- }
125
- `,
126
- variables: { modelType },
127
- });
128
- return unwrapApolloResult(result).aiGetDefaultModel;
129
- }
130
-
131
- async tasks(): Promise<AITask[]> {
132
- const { aiTasks } = unwrapApolloResult(
133
- await this._apolloClient.query({
134
- query: gql`
135
- query {
136
- aiTasks {
137
- name
138
- modelId
139
- taskId
140
- systemPrompt
141
- promptExamples {
142
- input
143
- output
144
- }
145
- metaData
146
- createdAt
147
- updatedAt
255
+ }));
256
+
257
+ return aiModelLoadingStatus
258
+ }
259
+
260
+ async prompt(taskId: string, prompt: string): Promise<string> {
261
+ const { aiPrompt } = unwrapApolloResult(await this.#apolloClient.mutate({
262
+ mutation: gql`
263
+ mutation AiPrompt($taskId: String!, $prompt: String!) {
264
+ aiPrompt(taskId: $taskId, prompt: $prompt)
265
+ }
266
+ `,
267
+ variables: {
268
+ taskId,
269
+ prompt
148
270
  }
149
- }
150
- `,
151
- }),
152
- );
153
-
154
- return aiTasks;
155
- }
156
-
157
- async addTask(
158
- name: string,
159
- modelId: string,
160
- systemPrompt: string,
161
- promptExamples: { input: string; output: string }[],
162
- metaData?: string,
163
- ): Promise<AITask> {
164
- const task = new AITaskInput(
165
- name,
166
- modelId,
167
- systemPrompt,
168
- promptExamples,
169
- metaData,
170
- );
171
- const { aiAddTask } = unwrapApolloResult(
172
- await this._apolloClient.mutate({
173
- mutation: gql`
174
- mutation AiAddTask($task: AITaskInput!) {
175
- aiAddTask(task: $task) {
176
- name
177
- modelId
178
- taskId
179
- systemPrompt
180
- promptExamples {
181
- input
182
- output
183
- }
184
- metaData
185
- createdAt
186
- updatedAt
271
+ }));
272
+
273
+ return aiPrompt;
274
+ }
275
+
276
+ async embed(modelId: string, text: string): Promise<Array<number>> {
277
+ const { aiEmbed } = unwrapApolloResult(await this.#apolloClient.mutate({
278
+ mutation: gql`
279
+ mutation aiEmbed($modelId: String!, $text: String!) {
280
+ aiEmbed(modelId: $modelId, text: $text)
281
+ }
282
+ `,
283
+ variables: {
284
+ modelId,
285
+ text
187
286
  }
188
- }
189
- `,
190
- variables: {
191
- task,
192
- },
193
- }),
194
- );
195
-
196
- return aiAddTask;
197
- }
198
-
199
- async removeTask(taskId: string): Promise<AITask> {
200
- const { aiRemoveTask } = unwrapApolloResult(
201
- await this._apolloClient.mutate({
202
- mutation: gql`
203
- mutation AiRemoveTask($taskId: String!) {
204
- aiRemoveTask(taskId: $taskId) {
205
- name
206
- modelId
207
- taskId
208
- systemPrompt
209
- promptExamples {
210
- input
211
- output
212
- }
213
- metaData
214
- createdAt
215
- updatedAt
287
+ }));
288
+
289
+ const compressed = base64js.toByteArray(aiEmbed);
290
+
291
+ const decompressed = JSON.parse(pako.inflate(compressed, { to: 'string' }));
292
+
293
+ return decompressed;
294
+ }
295
+
296
+ async openTranscriptionStream(
297
+ modelId: string,
298
+ streamCallback: (text: string) => void,
299
+ params?: {
300
+ startThreshold?: number;
301
+ startWindow?: number;
302
+ endThreshold?: number;
303
+ endWindow?: number;
304
+ timeBeforeSpeech?: number;
305
+ }
306
+ ): Promise<string> {
307
+ const { aiOpenTranscriptionStream } = unwrapApolloResult(await this.#apolloClient.mutate({
308
+ mutation: gql`
309
+ mutation AiOpenTranscriptionStream($modelId: String!, $params: VoiceActivityParamsInput) {
310
+ aiOpenTranscriptionStream(modelId: $modelId, params: $params)
311
+ }
312
+ `,
313
+ variables: {
314
+ modelId,
315
+ params
216
316
  }
217
- }
218
- `,
219
- variables: {
220
- taskId,
221
- },
222
- }),
223
- );
224
-
225
- return aiRemoveTask;
226
- }
227
-
228
- async updateTask(taskId: string, task: AITask): Promise<AITask> {
229
- const { aiUpdateTask } = unwrapApolloResult(
230
- await this._apolloClient.mutate({
231
- mutation: gql`
232
- mutation AiUpdateTask($taskId: String!, $task: AITaskInput!) {
233
- aiUpdateTask(taskId: $taskId, task: $task) {
234
- name
235
- modelId
236
- taskId
237
- systemPrompt
238
- promptExamples {
239
- input
240
- output
241
- }
242
- metaData
243
- createdAt
244
- updatedAt
317
+ }));
318
+
319
+ const subscription = this.#apolloClient.subscribe({
320
+ query: gql` subscription {
321
+ aiTranscriptionText(streamId: "${aiOpenTranscriptionStream}")
322
+ }`
323
+ }).subscribe({
324
+ next(data) {
325
+ streamCallback(data.data.aiTranscriptionText);
326
+
327
+ return data.data.aiTranscriptionText;
328
+ },
329
+ error(err) {
330
+ console.error(err);
245
331
  }
246
- }
247
- `,
248
- variables: {
249
- taskId,
250
- task: {
251
- name: task.name,
252
- modelId: task.modelId,
253
- systemPrompt: task.systemPrompt,
254
- promptExamples: task.promptExamples,
255
- },
256
- },
257
- }),
258
- );
259
-
260
- return aiUpdateTask;
261
- }
262
-
263
- async modelLoadingStatus(model: string): Promise<AIModelLoadingStatus> {
264
- const { aiModelLoadingStatus } = unwrapApolloResult(
265
- await this._apolloClient.query({
266
- query: gql`
267
- query AiModelLoadingStatus($model: String!) {
268
- aiModelLoadingStatus(model: $model) {
269
- model
270
- status
271
- progress
272
- loaded
273
- downloaded
332
+ });
333
+
334
+ this.#transcriptionSubscriptions.set(aiOpenTranscriptionStream, subscription);
335
+
336
+ return aiOpenTranscriptionStream;
337
+ }
338
+
339
+ async closeTranscriptionStream(streamId: string): Promise<void> {
340
+ const { aiCloseTranscriptionStream } = unwrapApolloResult(await this.#apolloClient.mutate({
341
+ mutation: gql`
342
+ mutation aiCloseTranscriptionStream($streamId: String!) {
343
+ aiCloseTranscriptionStream(streamId: $streamId)
344
+ }
345
+ `,
346
+ variables: {
347
+ streamId
274
348
  }
275
- }
276
- `,
277
- variables: {
278
- model,
279
- },
280
- }),
281
- );
282
-
283
- return aiModelLoadingStatus;
284
- }
285
-
286
- async prompt(taskId: string, prompt: string): Promise<string> {
287
- const { aiPrompt } = unwrapApolloResult(
288
- await this._apolloClient.mutate({
289
- mutation: gql`
290
- mutation AiPrompt($taskId: String!, $prompt: String!) {
291
- aiPrompt(taskId: $taskId, prompt: $prompt)
292
- }
293
- `,
294
- variables: {
295
- taskId,
296
- prompt,
297
- },
298
- }),
299
- );
300
-
301
- return aiPrompt;
302
- }
303
-
304
- async embed(modelId: string, text: string): Promise<Array<number>> {
305
- const { aiEmbed } = unwrapApolloResult(
306
- await this._apolloClient.mutate({
307
- mutation: gql`
308
- mutation aiEmbed($modelId: String!, $text: String!) {
309
- aiEmbed(modelId: $modelId, text: $text)
310
- }
311
- `,
312
- variables: {
313
- modelId,
314
- text,
315
- },
316
- }),
317
- );
318
-
319
- const compressed = base64js.toByteArray(aiEmbed);
320
-
321
- const decompressed = JSON.parse(pako.inflate(compressed, { to: "string" }));
322
-
323
- return decompressed;
324
- }
325
-
326
- async openTranscriptionStream(
327
- modelId: string,
328
- streamCallback: (text: string) => void,
329
- params?: {
330
- startThreshold?: number;
331
- startWindow?: number;
332
- endThreshold?: number;
333
- endWindow?: number;
334
- timeBeforeSpeech?: number;
335
- },
336
- ): Promise<string> {
337
- const { aiOpenTranscriptionStream } = unwrapApolloResult(
338
- await this._apolloClient.mutate({
339
- mutation: gql`
340
- mutation AiOpenTranscriptionStream(
341
- $modelId: String!
342
- $params: VoiceActivityParamsInput
343
- ) {
344
- aiOpenTranscriptionStream(modelId: $modelId, params: $params)
345
- }
346
- `,
347
- variables: {
348
- modelId,
349
- params,
350
- },
351
- }),
352
- );
353
-
354
- const subscription = this._apolloClient
355
- .subscribe({
356
- query: gql` subscription {
357
- aiTranscriptionText(streamId: "${aiOpenTranscriptionStream}")
358
- }`,
359
- })
360
- .subscribe({
361
- next(data) {
362
- streamCallback(data.data.aiTranscriptionText);
363
-
364
- return data.data.aiTranscriptionText;
365
- },
366
- error(err) {
367
- if (!isSocketCloseError(err)) console.error(err);
368
- },
369
- });
370
-
371
- this._transcriptionSubscriptions.set(
372
- aiOpenTranscriptionStream,
373
- subscription,
374
- );
375
-
376
- return aiOpenTranscriptionStream;
377
- }
378
-
379
- async closeTranscriptionStream(streamId: string): Promise<void> {
380
- const { aiCloseTranscriptionStream } = unwrapApolloResult(
381
- await this._apolloClient.mutate({
382
- mutation: gql`
383
- mutation aiCloseTranscriptionStream($streamId: String!) {
384
- aiCloseTranscriptionStream(streamId: $streamId)
385
- }
386
- `,
387
- variables: {
388
- streamId,
389
- },
390
- }),
391
- );
392
-
393
- const subscription = this._transcriptionSubscriptions.get(streamId);
394
-
395
- if (!subscription.closed) {
396
- subscription.unsubscribe();
349
+ }));
350
+
351
+ const subscription = this.#transcriptionSubscriptions.get(streamId);
352
+
353
+ if (!subscription.closed) {
354
+ subscription.unsubscribe();
355
+ }
356
+
357
+ return aiCloseTranscriptionStream;
397
358
  }
398
359
 
399
- return aiCloseTranscriptionStream;
400
- }
401
-
402
- async feedTranscriptionStream(
403
- streamIds: string | string[],
404
- audio: Float32Array,
405
- ): Promise<void> {
406
- const { aiFeedTranscriptionStream } = unwrapApolloResult(
407
- await this._apolloClient.mutate({
408
- mutation: gql`
409
- mutation AiFeedTranscriptionStream(
410
- $streamIds: [String!]!
411
- $audio: [Float!]!
412
- ) {
413
- aiFeedTranscriptionStream(streamIds: $streamIds, audio: $audio)
414
- }
415
- `,
416
- variables: {
417
- streamIds: Array.isArray(streamIds) ? streamIds : [streamIds],
418
- audio: audio,
419
- },
420
- }),
421
- );
422
-
423
- return aiFeedTranscriptionStream;
424
- }
425
- }
360
+ async feedTranscriptionStream(streamIds: string | string[], audio: Float32Array): Promise<void> {
361
+ const { aiFeedTranscriptionStream } = unwrapApolloResult(await this.#apolloClient.mutate({
362
+ mutation: gql`
363
+ mutation AiFeedTranscriptionStream($streamIds: [String!]!, $audio: [Float!]!) {
364
+ aiFeedTranscriptionStream(streamIds: $streamIds, audio: $audio)
365
+ }
366
+ `,
367
+ variables: {
368
+ streamIds: Array.isArray(streamIds) ? streamIds : [streamIds],
369
+ audio: audio
370
+ }
371
+ }));
372
+
373
+ return aiFeedTranscriptionStream;
374
+ }
375
+ }