@vertesia/client 0.42.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 (193) hide show
  1. package/LICENSE +13 -0
  2. package/lib/cjs/AccountApi.js +66 -0
  3. package/lib/cjs/AccountApi.js.map +1 -0
  4. package/lib/cjs/AccountsApi.js +13 -0
  5. package/lib/cjs/AccountsApi.js.map +1 -0
  6. package/lib/cjs/AnalyticsApi.js +13 -0
  7. package/lib/cjs/AnalyticsApi.js.map +1 -0
  8. package/lib/cjs/ApiKeysApi.js +63 -0
  9. package/lib/cjs/ApiKeysApi.js.map +1 -0
  10. package/lib/cjs/CommandsApi.js +19 -0
  11. package/lib/cjs/CommandsApi.js.map +1 -0
  12. package/lib/cjs/EnvironmentsApi.js +58 -0
  13. package/lib/cjs/EnvironmentsApi.js.map +1 -0
  14. package/lib/cjs/IamApi.js +51 -0
  15. package/lib/cjs/IamApi.js.map +1 -0
  16. package/lib/cjs/InteractionBase.js +44 -0
  17. package/lib/cjs/InteractionBase.js.map +1 -0
  18. package/lib/cjs/InteractionsApi.js +184 -0
  19. package/lib/cjs/InteractionsApi.js.map +1 -0
  20. package/lib/cjs/ProjectsApi.js +48 -0
  21. package/lib/cjs/ProjectsApi.js.map +1 -0
  22. package/lib/cjs/PromptsApi.js +133 -0
  23. package/lib/cjs/PromptsApi.js.map +1 -0
  24. package/lib/cjs/RefsApi.js +14 -0
  25. package/lib/cjs/RefsApi.js.map +1 -0
  26. package/lib/cjs/RunsApi.js +79 -0
  27. package/lib/cjs/RunsApi.js.map +1 -0
  28. package/lib/cjs/StreamSource.js +17 -0
  29. package/lib/cjs/StreamSource.js.map +1 -0
  30. package/lib/cjs/TrainingApi.js +54 -0
  31. package/lib/cjs/TrainingApi.js.map +1 -0
  32. package/lib/cjs/UsersApi.js +13 -0
  33. package/lib/cjs/UsersApi.js.map +1 -0
  34. package/lib/cjs/client.js +161 -0
  35. package/lib/cjs/client.js.map +1 -0
  36. package/lib/cjs/execute.js +146 -0
  37. package/lib/cjs/execute.js.map +1 -0
  38. package/lib/cjs/index.js +22 -0
  39. package/lib/cjs/index.js.map +1 -0
  40. package/lib/cjs/package.json +3 -0
  41. package/lib/cjs/store/CommandsApi.js +33 -0
  42. package/lib/cjs/store/CommandsApi.js.map +1 -0
  43. package/lib/cjs/store/FilesApi.js +122 -0
  44. package/lib/cjs/store/FilesApi.js.map +1 -0
  45. package/lib/cjs/store/ObjectsApi.js +185 -0
  46. package/lib/cjs/store/ObjectsApi.js.map +1 -0
  47. package/lib/cjs/store/TypesApi.js +52 -0
  48. package/lib/cjs/store/TypesApi.js.map +1 -0
  49. package/lib/cjs/store/WorkflowsApi.js +92 -0
  50. package/lib/cjs/store/WorkflowsApi.js.map +1 -0
  51. package/lib/cjs/store/client.js +49 -0
  52. package/lib/cjs/store/client.js.map +1 -0
  53. package/lib/cjs/store/errors.js +11 -0
  54. package/lib/cjs/store/errors.js.map +1 -0
  55. package/lib/cjs/store/index.js +22 -0
  56. package/lib/cjs/store/index.js.map +1 -0
  57. package/lib/esm/AccountApi.js +63 -0
  58. package/lib/esm/AccountApi.js.map +1 -0
  59. package/lib/esm/AccountsApi.js +10 -0
  60. package/lib/esm/AccountsApi.js.map +1 -0
  61. package/lib/esm/AnalyticsApi.js +10 -0
  62. package/lib/esm/AnalyticsApi.js.map +1 -0
  63. package/lib/esm/ApiKeysApi.js +59 -0
  64. package/lib/esm/ApiKeysApi.js.map +1 -0
  65. package/lib/esm/CommandsApi.js +16 -0
  66. package/lib/esm/CommandsApi.js.map +1 -0
  67. package/lib/esm/EnvironmentsApi.js +55 -0
  68. package/lib/esm/EnvironmentsApi.js.map +1 -0
  69. package/lib/esm/IamApi.js +45 -0
  70. package/lib/esm/IamApi.js.map +1 -0
  71. package/lib/esm/InteractionBase.js +40 -0
  72. package/lib/esm/InteractionBase.js.map +1 -0
  73. package/lib/esm/InteractionsApi.js +181 -0
  74. package/lib/esm/InteractionsApi.js.map +1 -0
  75. package/lib/esm/ProjectsApi.js +45 -0
  76. package/lib/esm/ProjectsApi.js.map +1 -0
  77. package/lib/esm/PromptsApi.js +130 -0
  78. package/lib/esm/PromptsApi.js.map +1 -0
  79. package/lib/esm/RefsApi.js +10 -0
  80. package/lib/esm/RefsApi.js.map +1 -0
  81. package/lib/esm/RunsApi.js +75 -0
  82. package/lib/esm/RunsApi.js.map +1 -0
  83. package/lib/esm/StreamSource.js +13 -0
  84. package/lib/esm/StreamSource.js.map +1 -0
  85. package/lib/esm/TrainingApi.js +51 -0
  86. package/lib/esm/TrainingApi.js.map +1 -0
  87. package/lib/esm/UsersApi.js +10 -0
  88. package/lib/esm/UsersApi.js.map +1 -0
  89. package/lib/esm/client.js +154 -0
  90. package/lib/esm/client.js.map +1 -0
  91. package/lib/esm/execute.js +108 -0
  92. package/lib/esm/execute.js.map +1 -0
  93. package/lib/esm/index.js +6 -0
  94. package/lib/esm/index.js.map +1 -0
  95. package/lib/esm/store/CommandsApi.js +28 -0
  96. package/lib/esm/store/CommandsApi.js.map +1 -0
  97. package/lib/esm/store/FilesApi.js +117 -0
  98. package/lib/esm/store/FilesApi.js.map +1 -0
  99. package/lib/esm/store/ObjectsApi.js +181 -0
  100. package/lib/esm/store/ObjectsApi.js.map +1 -0
  101. package/lib/esm/store/TypesApi.js +48 -0
  102. package/lib/esm/store/TypesApi.js.map +1 -0
  103. package/lib/esm/store/WorkflowsApi.js +86 -0
  104. package/lib/esm/store/WorkflowsApi.js.map +1 -0
  105. package/lib/esm/store/client.js +45 -0
  106. package/lib/esm/store/client.js.map +1 -0
  107. package/lib/esm/store/errors.js +7 -0
  108. package/lib/esm/store/errors.js.map +1 -0
  109. package/lib/esm/store/index.js +6 -0
  110. package/lib/esm/store/index.js.map +1 -0
  111. package/lib/tsconfig.tsbuildinfo +1 -0
  112. package/lib/types/AccountApi.d.ts +43 -0
  113. package/lib/types/AccountApi.d.ts.map +1 -0
  114. package/lib/types/AccountsApi.d.ts +6 -0
  115. package/lib/types/AccountsApi.d.ts.map +1 -0
  116. package/lib/types/AnalyticsApi.d.ts +6 -0
  117. package/lib/types/AnalyticsApi.d.ts.map +1 -0
  118. package/lib/types/ApiKeysApi.d.ts +42 -0
  119. package/lib/types/ApiKeysApi.d.ts.map +1 -0
  120. package/lib/types/CommandsApi.d.ts +10 -0
  121. package/lib/types/CommandsApi.d.ts.map +1 -0
  122. package/lib/types/EnvironmentsApi.d.ts +30 -0
  123. package/lib/types/EnvironmentsApi.d.ts.map +1 -0
  124. package/lib/types/IamApi.d.ts +39 -0
  125. package/lib/types/IamApi.d.ts.map +1 -0
  126. package/lib/types/InteractionBase.d.ts +22 -0
  127. package/lib/types/InteractionBase.d.ts.map +1 -0
  128. package/lib/types/InteractionsApi.d.ts +129 -0
  129. package/lib/types/InteractionsApi.d.ts.map +1 -0
  130. package/lib/types/ProjectsApi.d.ts +17 -0
  131. package/lib/types/ProjectsApi.d.ts.map +1 -0
  132. package/lib/types/PromptsApi.d.ts +106 -0
  133. package/lib/types/PromptsApi.d.ts.map +1 -0
  134. package/lib/types/RefsApi.d.ts +6 -0
  135. package/lib/types/RefsApi.d.ts.map +1 -0
  136. package/lib/types/RunsApi.d.ts +62 -0
  137. package/lib/types/RunsApi.d.ts.map +1 -0
  138. package/lib/types/StreamSource.d.ts +7 -0
  139. package/lib/types/StreamSource.d.ts.map +1 -0
  140. package/lib/types/TrainingApi.d.ts +27 -0
  141. package/lib/types/TrainingApi.d.ts.map +1 -0
  142. package/lib/types/UsersApi.d.ts +6 -0
  143. package/lib/types/UsersApi.d.ts.map +1 -0
  144. package/lib/types/client.d.ts +85 -0
  145. package/lib/types/client.d.ts.map +1 -0
  146. package/lib/types/execute.d.ts +32 -0
  147. package/lib/types/execute.d.ts.map +1 -0
  148. package/lib/types/index.d.ts +8 -0
  149. package/lib/types/index.d.ts.map +1 -0
  150. package/lib/types/store/CommandsApi.d.ts +13 -0
  151. package/lib/types/store/CommandsApi.d.ts.map +1 -0
  152. package/lib/types/store/FilesApi.d.ts +41 -0
  153. package/lib/types/store/FilesApi.d.ts.map +1 -0
  154. package/lib/types/store/ObjectsApi.d.ts +76 -0
  155. package/lib/types/store/ObjectsApi.d.ts.map +1 -0
  156. package/lib/types/store/TypesApi.d.ts +13 -0
  157. package/lib/types/store/TypesApi.d.ts.map +1 -0
  158. package/lib/types/store/WorkflowsApi.d.ts +39 -0
  159. package/lib/types/store/WorkflowsApi.d.ts.map +1 -0
  160. package/lib/types/store/client.d.ts +23 -0
  161. package/lib/types/store/client.d.ts.map +1 -0
  162. package/lib/types/store/errors.d.ts +4 -0
  163. package/lib/types/store/errors.d.ts.map +1 -0
  164. package/lib/types/store/index.d.ts +5 -0
  165. package/lib/types/store/index.d.ts.map +1 -0
  166. package/package.json +36 -0
  167. package/src/AccountApi.ts +75 -0
  168. package/src/AccountsApi.ts +18 -0
  169. package/src/AnalyticsApi.ts +20 -0
  170. package/src/ApiKeysApi.ts +67 -0
  171. package/src/CommandsApi.ts +23 -0
  172. package/src/EnvironmentsApi.ts +70 -0
  173. package/src/IamApi.ts +68 -0
  174. package/src/InteractionBase.ts +44 -0
  175. package/src/InteractionsApi.ts +210 -0
  176. package/src/ProjectsApi.ts +58 -0
  177. package/src/PromptsApi.ts +160 -0
  178. package/src/RefsApi.ts +18 -0
  179. package/src/RunsApi.ts +103 -0
  180. package/src/StreamSource.ts +3 -0
  181. package/src/TrainingApi.ts +71 -0
  182. package/src/UsersApi.ts +18 -0
  183. package/src/client.ts +191 -0
  184. package/src/execute.ts +114 -0
  185. package/src/index.ts +8 -0
  186. package/src/store/CommandsApi.ts +42 -0
  187. package/src/store/FilesApi.ts +139 -0
  188. package/src/store/ObjectsApi.ts +245 -0
  189. package/src/store/TypesApi.ts +61 -0
  190. package/src/store/WorkflowsApi.ts +121 -0
  191. package/src/store/client.ts +61 -0
  192. package/src/store/errors.ts +7 -0
  193. package/src/store/index.ts +6 -0
@@ -0,0 +1,58 @@
1
+ import { ApiTopic, ClientBase } from "@vertesia/api-fetch-client";
2
+ import { AwsConfiguration, GithubConfiguration, GladiaConfiguration, ICreateProjectPayload, Project, ProjectIntegrationListEntry, ProjectRef, SupportedIntegrations } from "@vertesia/common";
3
+
4
+ export default class ProjectsApi extends ApiTopic {
5
+ constructor(parent: ClientBase) {
6
+ super(parent, "/api/v1/projects");
7
+ }
8
+
9
+ list(account?: string[]): Promise<ProjectRef[]> {
10
+ return this.get('/', { query: { account } });
11
+ }
12
+
13
+ retrieve(projectId: string): Promise<Project> {
14
+ return this.get(`/${projectId}`);
15
+ }
16
+
17
+ create(payload: ICreateProjectPayload): Promise<Project> {
18
+ return this.post('/', {
19
+ payload
20
+ });
21
+ }
22
+
23
+ update(projectId: string, payload: Partial<Project>): Promise<Project> {
24
+ return this.put(`/${projectId}`, {
25
+ payload
26
+ });
27
+ }
28
+
29
+ integrations: IntegrationsConfigurationApi = new IntegrationsConfigurationApi(this);
30
+
31
+ }
32
+
33
+ class IntegrationsConfigurationApi extends ApiTopic {
34
+
35
+ constructor(parent: ClientBase) {
36
+ super(parent, "/");
37
+ }
38
+
39
+ list(projectId: string): Promise<ProjectIntegrationListEntry[]> {
40
+ return this.get(`/${projectId}/integrations`).then(res => res.integrations);
41
+ }
42
+
43
+ retrieve(projectId: string, integrationId: SupportedIntegrations): Promise<GladiaConfiguration | GithubConfiguration | AwsConfiguration | undefined> {
44
+ return this.get(`/${projectId}/integrations/${integrationId}`).catch(err => {
45
+ if (err.status === 404) {
46
+ return undefined;
47
+ }
48
+ throw err;
49
+ });
50
+ }
51
+
52
+ update(projectId: string, integrationId: string, payload: any): Promise<GladiaConfiguration | GithubConfiguration> {
53
+ return this.put(`/${projectId}/integrations/${integrationId}`, {
54
+ payload
55
+ });
56
+ }
57
+
58
+ }
@@ -0,0 +1,160 @@
1
+ import { ComputePromptFacetPayload, PromptSearchPayload, PromptSearchQuery, PromptTemplate, PromptTemplateForkPayload, PromptTemplateCreatePayload, PromptTemplateRef, PromptTemplateUpdatePayload } from "@vertesia/common";
2
+ import { ApiTopic, ClientBase } from "@vertesia/api-fetch-client";
3
+
4
+ export interface ComputePromptFacetsResponse {
5
+ role?: { _id: string, count: number }[];
6
+ total?: { count: number }[];
7
+ }
8
+
9
+ export default class PromptsApi extends ApiTopic {
10
+ constructor(parent: ClientBase) {
11
+ super(parent, "/api/v1/prompts")
12
+ }
13
+
14
+ /**
15
+ * Get the list of all prompt templates
16
+ * @param payload query payload to filter search
17
+ * @returns PromptTemplateRef[]
18
+ **/
19
+ list(payload: PromptSearchPayload = {}): Promise<PromptTemplateRef[]> {
20
+ const query = payload.query || {} as PromptSearchQuery;
21
+
22
+ return this.get("/", {
23
+ query: {
24
+ ...query
25
+ }
26
+ });
27
+ }
28
+
29
+ /**
30
+ * Get the list of all prompt facets
31
+ * @param payload query payload to filter facet search
32
+ * @returns ComputePromptFacetsResponse[]
33
+ **/
34
+ computeFacets(query: ComputePromptFacetPayload): Promise<ComputePromptFacetsResponse> {
35
+ return this.post("/facets", {
36
+ payload: query
37
+ });
38
+ }
39
+
40
+ /**
41
+ * Create a new prompt template
42
+ * @param payload PromptTemplateCreatePayload
43
+ * @returns PromptTemplate
44
+ * @throws ApiError
45
+ * @throws 400 if payload is invalid
46
+ * @throws 500 if creation fails
47
+ **/
48
+ create(payload: PromptTemplateCreatePayload): Promise<PromptTemplate> {
49
+ return this.post('/', {
50
+ payload
51
+ });
52
+ }
53
+
54
+ /**
55
+ * Retrieve an existing prompt template
56
+ * @param id of the prompt template to retrieve
57
+ * @returns PromptTemplate
58
+ **/
59
+ retrieve(id: string): Promise<PromptTemplate> {
60
+ return this.get(`/${id}`);
61
+ }
62
+
63
+ /**
64
+ * Update an existing prompt template
65
+ * @param id of the prompt template to update
66
+ * @param payload PromptTemplateCreatePayload
67
+ * @returns PromptTemplate
68
+ * @throws ApiError
69
+ * @throws 400 if payload is invalid
70
+ * @throws 500 if update fails
71
+ * @throws 404 if not found
72
+ **/
73
+ update(id: string, payload: PromptTemplateUpdatePayload): Promise<PromptTemplate> {
74
+ return this.put(`/${id}`, {
75
+ payload
76
+ });
77
+ }
78
+
79
+ /**
80
+ * Delete an existing prompt template
81
+ * @param id of the prompt template to delete
82
+ * @returns void
83
+ */
84
+ delete(id: string): Promise<void> {
85
+ return this.del(`/${id}`);
86
+ }
87
+
88
+ /**
89
+ * Fork an existing prompt template
90
+ * @param id of the prompt template to fork
91
+ * @param payload PromptTemplateForkPayload
92
+ * @returns Forked PromptTemplate
93
+ */
94
+ fork(id: string, payload: PromptTemplateForkPayload): Promise<PromptTemplate> {
95
+ return this.post(`/${id}/fork`, {
96
+ payload
97
+ });
98
+ }
99
+
100
+ /**
101
+ * Render a prompt template
102
+ * @param id of the prompt template to render
103
+ * @param payload that will be passed to the prompt template to generate the prompts
104
+ * @returns PromptTemplate
105
+ * @throws ApiError
106
+ * @throws 404 if not found
107
+ * @throws 400 if payload is invalid
108
+ * @throws 500 if render fails
109
+ **/
110
+ render(id: string, payload: {}): Promise<PromptTemplate> {
111
+ return this.post(`/${id}/render`, {
112
+ payload
113
+ });
114
+ }
115
+
116
+ /**
117
+ * Get options for a field
118
+ * @param field name to get options for
119
+ * @returns string[]
120
+ */
121
+ options(field: string): Promise<string[]> {
122
+ return this.get(`/options/${field}`);
123
+ }
124
+
125
+ /**
126
+ * List the versions of the prompt template. Returens an empty array if no versions are found
127
+ * @param id
128
+ * @returns the versions list or an empty array if no versions are found
129
+ */
130
+ listVersions(id: string): Promise<PromptTemplateRef[]> {
131
+ return this.get(`/${id}/versions`);
132
+ }
133
+
134
+
135
+ /**
136
+ * Retrieve list of interactions that use the prompt template
137
+ */
138
+ listInteractions(id: string): Promise<ListInteractionsResponse> {
139
+ return this.get(`/${id}/interactions`);
140
+ }
141
+
142
+ /**
143
+ * List the forks of the prompt. Returns an empty array if no forks are found
144
+ * @param id of the prompt to search forks
145
+ * @returns the versions list or an empty array if no forks are found
146
+ */
147
+ listForks(id: string): Promise<PromptTemplateRef[]> {
148
+ return this.get(`/${id}/forks`);
149
+ }
150
+
151
+ }
152
+
153
+ export interface ListInteractionsResponse {
154
+ prompt: string,
155
+ interactions: {
156
+ id: string,
157
+ name: string,
158
+ versions: string[],
159
+ }[]
160
+ }
package/src/RefsApi.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { ResolvableRef, ResourceRef } from "@vertesia/common"
2
+ import { ApiTopic, ClientBase } from "@vertesia/api-fetch-client"
3
+
4
+
5
+ export class RefsApi extends ApiTopic {
6
+
7
+ constructor(parent: ClientBase) {
8
+ super(parent, "/api/v1/refs")
9
+ }
10
+
11
+
12
+ resolve(refs: ResolvableRef[]): Promise<ResourceRef[]> {
13
+
14
+ return this.post('/resolve', { payload: { refs } })
15
+
16
+ }
17
+
18
+ }
package/src/RunsApi.ts ADDED
@@ -0,0 +1,103 @@
1
+ import { ComputeRunFacetPayload, ExecutionRun, ExecutionRunRef, FindPayload, RunCreatePayload, RunListingFilters, RunListingQueryOptions, RunSearchPayload } from "@vertesia/common";
2
+ import { ApiTopic, ClientBase } from "@vertesia/api-fetch-client";
3
+ import { ComposableClient } from "./client.js";
4
+
5
+ export interface FilterOption {
6
+ id: string,
7
+ name: string,
8
+ count: number
9
+ }
10
+
11
+ export interface ComputeRunFacetsResponse {
12
+ environments?: { _id: string, count: number }[];
13
+ interactions?: { _id: string, count: number }[];
14
+ models?: { _id: string, count: number }[];
15
+ tags?: { _id: string, count: number }[];
16
+ status?: { _id: string, count: number }[];
17
+ total?: { count: number }[];
18
+ }
19
+
20
+ export class RunsApi extends ApiTopic {
21
+
22
+ constructor(parent: ClientBase) {
23
+ super(parent, "/api/v1/runs")
24
+ }
25
+
26
+ /**
27
+ * Get the list of all runs
28
+ * @param project optional project id to filter by
29
+ * @param interaction optional interaction id to filter by
30
+ * @returns InteractionResult[]
31
+ **/
32
+ list({ limit, offset, filters }: RunListingQueryOptions): Promise<ExecutionRunRef[]> {
33
+
34
+ const query = {
35
+ limit,
36
+ offset,
37
+ ...filters
38
+ }
39
+
40
+ return this.get('/', { query: query });
41
+ }
42
+
43
+ find(payload: FindPayload): Promise<ExecutionRun[]> {
44
+ return this.post("/find", {
45
+ payload
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Get a run by id
51
+ * @param id
52
+ * @returns InteractionResult
53
+ **/
54
+ retrieve(id: string): Promise<ExecutionRun> {
55
+ return this.get('/' + id);
56
+ }
57
+
58
+ /**
59
+ * Get filter options for a field
60
+ * return FilterOption[]
61
+ */
62
+ filterOptions(field: string, filters: RunListingFilters): Promise<FilterOption[]> {
63
+ const query = {
64
+ ...filters
65
+ }
66
+ return this.get(`/filter-options/${field}`, { query });
67
+
68
+ }
69
+
70
+ create(payload: RunCreatePayload): Promise<ExecutionRun> {
71
+ const sessionTags = (this.client as ComposableClient).sessionTags;
72
+ if (sessionTags) {
73
+ let tags = Array.isArray(sessionTags) ? sessionTags : [sessionTags];
74
+ if (Array.isArray(payload.tags)) {
75
+ tags = tags.concat(payload.tags)
76
+ } else if (payload.tags) {
77
+ tags = tags.concat([payload.tags])
78
+ }
79
+ payload = { ...payload, tags }
80
+ }
81
+ return this.post("/", {
82
+ payload
83
+ });
84
+ }
85
+
86
+ /**
87
+ * Get the list of all runs facets
88
+ * @param payload query payload to filter facet search
89
+ * @returns ComputeRunFacetsResponse[]
90
+ **/
91
+ computeFacets(query: ComputeRunFacetPayload): Promise<ComputeRunFacetsResponse> {
92
+ return this.post("/facets", {
93
+ payload: query
94
+ });
95
+ }
96
+
97
+ search(payload: RunSearchPayload): Promise<ExecutionRunRef[]> {
98
+ return this.post("/search", {
99
+ payload
100
+ });
101
+ }
102
+
103
+ }
@@ -0,0 +1,3 @@
1
+ export class StreamSource {
2
+ constructor(public stream: ReadableStream, public name: string, public type?: string, public id?: string) { }
3
+ }
@@ -0,0 +1,71 @@
1
+ import { TrainingJob } from "@llumiverse/core";
2
+ import { ExecutionRunRef, ListTrainingSessionsQuery, TrainingSession, TrainingSessionCreatePayload, TrainingSessionRef } from "@vertesia/common";
3
+ import { ApiTopic, ClientBase } from "@vertesia/api-fetch-client";
4
+
5
+
6
+ export default class TrainingApi extends ApiTopic {
7
+
8
+ constructor(parent: ClientBase) {
9
+ super(parent, "/api/v1/training")
10
+ }
11
+
12
+ listSessions(query: ListTrainingSessionsQuery = {}): Promise<TrainingSessionRef[]> {
13
+ return this.get('/', { query: query as any });
14
+ }
15
+
16
+ listSessionNames(query: ListTrainingSessionsQuery = {}): Promise<{ id: string, name: string }[]> {
17
+ return this.get('/names', { query: query as any });
18
+ }
19
+
20
+ createSession(payload: TrainingSessionCreatePayload): Promise<TrainingSession> {
21
+ return this.post('/', { payload })
22
+ }
23
+
24
+ getSession(sessionId: string): Promise<TrainingSession> {
25
+ return this.get('/' + sessionId)
26
+ }
27
+
28
+ addToSession(sessionId: string, runs: string[]): Promise<any> {
29
+ return this.post('/' + sessionId + '/add', { payload: { runs } })
30
+ }
31
+
32
+ listSessionRuns(sessionId: string, limit = 100, offset = 0): Promise<ExecutionRunRef[]> {
33
+ return this.get('/' + sessionId + '/runs', { query: { limit, offset } });
34
+ }
35
+
36
+ buildSession(sessionId: string) {
37
+ return this.post('/' + sessionId + '/build');
38
+ }
39
+
40
+ getDataUrl(sessionId: string): Promise<{ url: string }> {
41
+ return this.get('/' + sessionId + '/url')
42
+ }
43
+
44
+ getDataUploadUrl(sessionId: string, isResumable = false): Promise<{ url: string }> {
45
+ return this.get('/' + sessionId + '/upload-url',
46
+ isResumable ? { query: { resumable: 'true' } } : {})
47
+ }
48
+
49
+ startTraining(sessionId: string): Promise<TrainingJob> {
50
+ return this.post('/' + sessionId + '/start');
51
+ }
52
+
53
+ cancelTraining(sessionId: string): Promise<TrainingJob> {
54
+ return this.post('/' + sessionId + '/cancel');
55
+ }
56
+
57
+ getTrainingJob(jobId: string): Promise<TrainingJob> {
58
+ return this.get(`/job/${jobId}`);
59
+ }
60
+
61
+ getAndSyncTrainingJob(jobId: string): Promise<TrainingJob> {
62
+ return this.get(`/job/${jobId}`, {
63
+ query: { sync: 'true' }
64
+ });
65
+ }
66
+
67
+ setDataset(sessionId: string, name: string = "default"): Promise<any> {
68
+ return this.post('/' + sessionId + '/dataset', { payload: { dataset: name } })
69
+ }
70
+
71
+ }
@@ -0,0 +1,18 @@
1
+ import { User } from "@vertesia/common";
2
+ import { ApiTopic, ClientBase } from "@vertesia/api-fetch-client";
3
+
4
+
5
+
6
+
7
+ export default class UsersApi extends ApiTopic {
8
+
9
+ constructor(parent: ClientBase) {
10
+ super(parent, "/api/v1/users")
11
+ }
12
+
13
+ retrieve(userId: string): Promise<User> {
14
+ return this.get('/' + userId);
15
+ }
16
+
17
+
18
+ }
package/src/client.ts ADDED
@@ -0,0 +1,191 @@
1
+ import { AbstractFetchClient } from "@vertesia/api-fetch-client";
2
+ import { AuthTokenResponse } from "@vertesia/common";
3
+ import AccountApi from "./AccountApi.js";
4
+ import AccountsApi from "./AccountsApi.js";
5
+ import AnalyticsApi from "./AnalyticsApi.js";
6
+ import { ApiKeysApi } from "./ApiKeysApi.js";
7
+ import CommandsApi from "./CommandsApi.js";
8
+ import EnvironmentsApi from "./EnvironmentsApi.js";
9
+ import { IamApi } from "./IamApi.js";
10
+ import InteractionsApi from "./InteractionsApi.js";
11
+ import ProjectsApi from "./ProjectsApi.js";
12
+ import PromptsApi from "./PromptsApi.js";
13
+ import { RefsApi } from "./RefsApi.js";
14
+ import { RunsApi } from "./RunsApi.js";
15
+ import { ZenoClient } from "./store/client.js";
16
+ import TrainingApi from "./TrainingApi.js";
17
+ import UsersApi from "./UsersApi.js";
18
+
19
+ /**
20
+ * 1 min threshold constant in ms
21
+ */
22
+ const EXPIRATION_THRESHOLD = 60000;
23
+
24
+ export interface ComposableClientProps {
25
+ serverUrl: string;
26
+ storeUrl: string;
27
+ apikey?: string;
28
+ projectId?: string;
29
+ sessionTags?: string | string[];
30
+ onRequest?: (request: Request) => void;
31
+ onResponse?: (response: Response) => void;
32
+ }
33
+
34
+ export class ComposableClient extends AbstractFetchClient<ComposableClient> {
35
+
36
+ /**
37
+ * The JWT token linked to the API KEY (sk or pk)
38
+ */
39
+ _jwt: string | null = null;
40
+
41
+ /**
42
+ * The store client
43
+ */
44
+ store: ZenoClient;
45
+
46
+ /**
47
+ * The session name will be sent when executing an interaction as a tag
48
+ */
49
+ sessionTags?: string | string[];
50
+
51
+ constructor(
52
+ opts: ComposableClientProps = {} as any
53
+ ) {
54
+ super(opts.serverUrl);
55
+ if (!opts.serverUrl) {
56
+ throw new Error("storeUrl is required for ComposableClient");
57
+ }
58
+ if (!opts.storeUrl) {
59
+ throw new Error("storeUrl is required for ComposableClient");
60
+ }
61
+ this.store = new ZenoClient({
62
+ serverUrl: opts.storeUrl,
63
+ apikey: opts.apikey,
64
+ onRequest: opts.onRequest,
65
+ onResponse: opts.onResponse
66
+ });
67
+
68
+ if (opts.apikey) {
69
+ this.withApiKey(opts.apikey);
70
+ }
71
+ if (opts.projectId) {
72
+ this.headers["x-project-id"] = opts.projectId;
73
+ }
74
+ this.onRequest = opts.onRequest;
75
+ this.onResponse = opts.onResponse;
76
+ this.sessionTags = opts.sessionTags;
77
+ }
78
+
79
+
80
+ /**
81
+ * Overwrite to keep store and composable clients synchronized on the auth callback
82
+ * @param authCb
83
+ * @returns
84
+ */
85
+ withAuthCallback(authCb?: (() => Promise<string>) | null) {
86
+ this.store.withAuthCallback(authCb);
87
+ return super.withAuthCallback(authCb);
88
+ }
89
+
90
+ async withApiKey(apiKey: string | null) {
91
+ return this.withAuthCallback(
92
+ apiKey ? async () => {
93
+ if (!isApiKey(apiKey)) {
94
+ return `Bearer ${apiKey}`
95
+ }
96
+
97
+ if (isTokenExpired(this._jwt)) {
98
+ const jwt = await this.getAuthToken(apiKey);
99
+ this._jwt = jwt.token;
100
+ }
101
+ return `Bearer ${this._jwt}`
102
+ } : undefined
103
+ );
104
+ }
105
+
106
+ /**
107
+ * Alias for store.workflows
108
+ */
109
+ get workflows() {
110
+ return this.store.workflows;
111
+ }
112
+
113
+ /**
114
+ * Alias for store.objects
115
+ */
116
+ get objects() {
117
+ return this.store.objects;
118
+ }
119
+
120
+ get files() {
121
+ return this.store.files;
122
+ }
123
+
124
+ /**
125
+ * Alias for store.types
126
+ */
127
+ get types() {
128
+ return this.store.types;
129
+ }
130
+
131
+
132
+ set project(projectId: string | null) {
133
+ if (projectId) {
134
+ this.headers["x-project-id"] = projectId;
135
+ } else {
136
+ delete this.headers["x-project-id"];
137
+ }
138
+ }
139
+
140
+ get project() {
141
+ return this.headers["x-project-id"] || null;
142
+ }
143
+
144
+ /**
145
+ *
146
+ * Generate a token for use with other Composable's services
147
+ *
148
+ * @param accountId: selected account to generate the token for
149
+ * @returns AuthTokenResponse
150
+ */
151
+ async getAuthToken(token?: string, accountId?: string): Promise<AuthTokenResponse> {
152
+ const query = {
153
+ accountId,
154
+ token
155
+ };
156
+
157
+ return this.get('/auth/token', { query: query, headers: { "authorization": undefined } as any });
158
+ }
159
+
160
+ projects = new ProjectsApi(this);
161
+ environments = new EnvironmentsApi(this);
162
+ interactions = new InteractionsApi(this);
163
+ prompts = new PromptsApi(this);
164
+ runs = new RunsApi(this);
165
+ account = new AccountApi(this);
166
+ accounts = new AccountsApi(this);
167
+ apikeys = new ApiKeysApi(this);
168
+ analytics = new AnalyticsApi(this);
169
+ training = new TrainingApi(this);
170
+ users = new UsersApi(this);
171
+ iam = new IamApi(this);
172
+ refs = new RefsApi(this);
173
+ commands = new CommandsApi(this);
174
+ }
175
+
176
+ function isApiKey(apiKey: string) {
177
+ return (apiKey.startsWith('pk-') || apiKey.startsWith('sk-'));
178
+ }
179
+
180
+ function isTokenExpired(token: string | null) {
181
+ if (!token) {
182
+ return true;
183
+ }
184
+
185
+ const payloadBase64 = token.split('.')[1];
186
+ const decodedJson = Buffer.from(payloadBase64, 'base64').toString();
187
+ const decoded = JSON.parse(decodedJson)
188
+ const exp = decoded.exp;
189
+ const currentTime = Date.now();
190
+ return (currentTime <= exp * 1000 - EXPIRATION_THRESHOLD);
191
+ }