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

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 (128) hide show
  1. package/jest.config.js +4 -4
  2. package/lib/index.cjs +5999 -6044
  3. package/lib/index.js +5977 -6038
  4. package/lib/src/Ad4mClient.d.ts +16 -9
  5. package/lib/src/Ad4mClient.js +25 -46
  6. package/lib/src/Ad4mClient.test.d.ts +1 -0
  7. package/lib/src/Ad4mClient.test.js +1204 -0
  8. package/lib/src/Literal.d.ts +2 -1
  9. package/lib/src/Literal.js +35 -38
  10. package/lib/src/Literal.test.d.ts +1 -0
  11. package/lib/src/Literal.test.js +41 -0
  12. package/lib/src/SmartLiteral.d.ts +2 -1
  13. package/lib/src/SmartLiteral.js +15 -30
  14. package/lib/src/agent/AgentClient.d.ts +4 -1
  15. package/lib/src/agent/AgentClient.js +99 -79
  16. package/lib/src/ai/AIClient.d.ts +2 -1
  17. package/lib/src/ai/AIClient.js +220 -224
  18. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  19. package/lib/src/expression/ExpressionClient.js +86 -73
  20. package/lib/src/index.d.ts +1 -1
  21. package/lib/src/index.js +1 -1
  22. package/lib/src/language/LanguageClient.d.ts +1 -1
  23. package/lib/src/language/LanguageClient.js +52 -52
  24. package/lib/src/model/Ad4mModel.d.ts +157 -937
  25. package/lib/src/model/Ad4mModel.js +212 -2797
  26. package/lib/src/model/Ad4mModel.test.js +1002 -561
  27. package/lib/src/model/decorators.d.ts +94 -426
  28. package/lib/src/model/decorators.js +333 -741
  29. package/lib/src/model/mutation.d.ts +81 -0
  30. package/lib/src/model/mutation.js +317 -0
  31. package/lib/src/model/parentUtils.d.ts +32 -0
  32. package/lib/src/model/parentUtils.js +64 -0
  33. package/lib/src/model/prolog/generatePrologFacts.d.ts +36 -0
  34. package/lib/src/model/prolog/generatePrologFacts.js +139 -0
  35. package/lib/src/model/prolog/index.d.ts +1 -0
  36. package/lib/src/model/prolog/index.js +1 -0
  37. package/lib/src/model/query/ModelQueryBuilder.d.ts +82 -0
  38. package/lib/src/model/query/ModelQueryBuilder.js +115 -0
  39. package/lib/src/model/query/QueryBuilder.d.ts +101 -0
  40. package/lib/src/model/query/QueryBuilder.js +193 -0
  41. package/lib/src/model/query/SurrealQueryBuilder.d.ts +48 -0
  42. package/lib/src/model/query/SurrealQueryBuilder.js +371 -0
  43. package/lib/src/model/query/fetchInstance.d.ts +24 -0
  44. package/lib/src/model/query/fetchInstance.js +144 -0
  45. package/lib/src/model/query/hydration.d.ts +54 -0
  46. package/lib/src/model/query/hydration.js +214 -0
  47. package/lib/src/model/query/operations.d.ts +59 -0
  48. package/lib/src/model/query/operations.js +384 -0
  49. package/lib/src/model/query/snapshot.d.ts +59 -0
  50. package/lib/src/model/query/snapshot.js +106 -0
  51. package/lib/src/model/query/surrealCompiler.d.ts +42 -0
  52. package/lib/src/model/query/surrealCompiler.js +304 -0
  53. package/lib/src/model/schema/fromJSONSchema.d.ts +67 -0
  54. package/lib/src/model/schema/fromJSONSchema.js +264 -0
  55. package/lib/src/model/schema/metadata.d.ts +15 -0
  56. package/lib/src/model/schema/metadata.js +112 -0
  57. package/lib/src/model/subscription.d.ts +47 -0
  58. package/lib/src/model/subscription.js +231 -0
  59. package/lib/src/model/transaction.d.ts +27 -0
  60. package/lib/src/model/transaction.js +32 -0
  61. package/lib/src/model/types.d.ts +195 -0
  62. package/lib/src/model/types.js +8 -0
  63. package/lib/src/model/util.js +7 -7
  64. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +5 -1
  65. package/lib/src/neighbourhood/NeighbourhoodClient.js +263 -181
  66. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -1
  67. package/lib/src/neighbourhood/NeighbourhoodProxy.js +13 -28
  68. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +1 -5
  69. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  70. package/lib/src/perspectives/PerspectiveClient.js +316 -240
  71. package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -92
  72. package/lib/src/perspectives/PerspectiveProxy.js +803 -444
  73. package/lib/src/runtime/RuntimeClient.d.ts +5 -1
  74. package/lib/src/runtime/RuntimeClient.js +412 -238
  75. package/lib/src/shacl/SHACLFlow.test.js +164 -105
  76. package/lib/src/shacl/SHACLShape.d.ts +17 -3
  77. package/lib/src/shacl/SHACLShape.js +127 -90
  78. package/lib/src/shacl/SHACLShape.test.js +244 -176
  79. package/lib/src/utils.d.ts +8 -0
  80. package/lib/src/utils.js +21 -7
  81. package/package.json +8 -13
  82. package/src/Ad4mClient.test.ts +1564 -1315
  83. package/src/Ad4mClient.ts +55 -54
  84. package/src/Literal.test.ts +45 -26
  85. package/src/Literal.ts +82 -66
  86. package/src/SmartLiteral.ts +74 -58
  87. package/src/agent/AgentClient.ts +144 -99
  88. package/src/ai/AIClient.ts +409 -359
  89. package/src/expression/ExpressionClient.ts +142 -96
  90. package/src/index.ts +1 -1
  91. package/src/language/LanguageClient.ts +137 -113
  92. package/src/model/Ad4mModel.test.ts +1128 -647
  93. package/src/model/Ad4mModel.ts +485 -3247
  94. package/src/model/IMPROVEMENTS.md +482 -0
  95. package/src/model/PREDICATE_COLLISION_FIX.md +651 -0
  96. package/src/model/README.md +317 -0
  97. package/src/model/SHACL_MUTATION_DECOUPLING.md +356 -0
  98. package/src/model/SUBSCRIPTION_STRATEGY.md +317 -0
  99. package/src/model/TC39_STAGE_3_DECORATORS.md +609 -0
  100. package/src/model/decorators.ts +689 -1046
  101. package/src/model/mutation.ts +461 -0
  102. package/src/model/parentUtils.ts +91 -0
  103. package/src/model/prolog/generatePrologFacts.ts +176 -0
  104. package/src/model/prolog/index.ts +1 -0
  105. package/src/model/query/ModelQueryBuilder.ts +173 -0
  106. package/src/model/query/fetchInstance.ts +181 -0
  107. package/src/model/query/hydration.ts +273 -0
  108. package/src/model/query/operations.ts +510 -0
  109. package/src/model/query/snapshot.ts +126 -0
  110. package/src/model/query/surrealCompiler.ts +376 -0
  111. package/src/model/schema/fromJSONSchema.ts +411 -0
  112. package/src/model/schema/metadata.ts +148 -0
  113. package/src/model/subscription.ts +307 -0
  114. package/src/model/transaction.ts +60 -0
  115. package/src/model/types.ts +207 -0
  116. package/src/model/util.ts +47 -48
  117. package/src/neighbourhood/NeighbourhoodClient.ts +393 -251
  118. package/src/neighbourhood/NeighbourhoodProxy.test.ts +3 -7
  119. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -40
  120. package/src/perspectives/PerspectiveClient.ts +779 -540
  121. package/src/perspectives/PerspectiveProxy.ts +2860 -2053
  122. package/src/runtime/RuntimeClient.ts +775 -476
  123. package/src/shacl/SHACLFlow.test.ts +196 -129
  124. package/src/shacl/SHACLShape.test.ts +284 -190
  125. package/src/shacl/SHACLShape.ts +225 -172
  126. package/src/utils.ts +31 -17
  127. package/tsconfig.json +9 -10
  128. package/src/model/Subject.ts +0 -159
@@ -1,375 +1,425 @@
1
1
  import { ApolloClient, gql } from "@apollo/client";
2
2
  import unwrapApolloResult from "../unwrapApolloResult";
3
- import base64js from 'base64-js';
4
- import pako from 'pako'
3
+ import { isSocketCloseError } from "../utils";
4
+ import base64js from "base64-js";
5
+ import pako from "pako";
5
6
  import { AIModelLoadingStatus, AITask, AITaskInput } from "./Tasks";
6
- import { ModelInput, Model, ModelType } from "./AIResolver"
7
+ import { ModelInput, Model, ModelType } from "./AIResolver";
7
8
 
8
9
  export class AIClient {
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
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
173
29
  }
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
30
+ local {
31
+ fileName
32
+ tokenizerSource {
33
+ repo
34
+ revision
35
+ fileName
36
+ }
37
+ huggingfaceRepo
38
+ revision
200
39
  }
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
- }
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
233
111
  }
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
112
+ local {
113
+ fileName
114
+ tokenizerSource {
115
+ repo
116
+ revision
117
+ fileName
118
+ }
119
+ huggingfaceRepo
120
+ revision
254
121
  }
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
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
270
148
  }
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
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
286
187
  }
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
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
316
216
  }
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);
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
331
245
  }
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
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
348
274
  }
349
- }));
350
-
351
- const subscription = this.#transcriptionSubscriptions.get(streamId);
352
-
353
- if (!subscription.closed) {
354
- subscription.unsubscribe();
355
- }
356
-
357
- return aiCloseTranscriptionStream;
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();
358
397
  }
359
398
 
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
- }
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
+ }