@vertesia/client 1.6.0-dev.20261019.172735Z → 1.6.0-dev.20261120.123957Z

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 (209) hide show
  1. package/lib/AccountApi.d.ts +2 -0
  2. package/lib/AccountApi.d.ts.map +1 -1
  3. package/lib/AccountApi.js +3 -0
  4. package/lib/AccountApi.js.map +1 -1
  5. package/lib/AccountRolesApi.d.ts +11 -0
  6. package/lib/AccountRolesApi.d.ts.map +1 -0
  7. package/lib/AccountRolesApi.js +22 -0
  8. package/lib/AccountRolesApi.js.map +1 -0
  9. package/lib/ApiKeysApi.d.ts +4 -7
  10. package/lib/ApiKeysApi.d.ts.map +1 -1
  11. package/lib/ApiKeysApi.js +5 -4
  12. package/lib/ApiKeysApi.js.map +1 -1
  13. package/lib/AppsApi.d.ts +18 -3
  14. package/lib/AppsApi.d.ts.map +1 -1
  15. package/lib/AppsApi.js +34 -4
  16. package/lib/AppsApi.js.map +1 -1
  17. package/lib/AuditTrailApi.d.ts +6 -1
  18. package/lib/AuditTrailApi.d.ts.map +1 -1
  19. package/lib/AuditTrailApi.js +8 -0
  20. package/lib/AuditTrailApi.js.map +1 -1
  21. package/lib/IamApi.d.ts +1 -1
  22. package/lib/IamApi.js +1 -1
  23. package/lib/InteractionCatalogApi.d.ts +19 -1
  24. package/lib/InteractionCatalogApi.d.ts.map +1 -1
  25. package/lib/InteractionCatalogApi.js +30 -0
  26. package/lib/InteractionCatalogApi.js.map +1 -1
  27. package/lib/InteractionsApi.d.ts +6 -13
  28. package/lib/InteractionsApi.d.ts.map +1 -1
  29. package/lib/InteractionsApi.js +4 -24
  30. package/lib/InteractionsApi.js.map +1 -1
  31. package/lib/OAuthClientsApi.d.ts +3 -1
  32. package/lib/OAuthClientsApi.d.ts.map +1 -1
  33. package/lib/OAuthClientsApi.js +4 -0
  34. package/lib/OAuthClientsApi.js.map +1 -1
  35. package/lib/OAuthProvidersApi.d.ts +6 -2
  36. package/lib/OAuthProvidersApi.d.ts.map +1 -1
  37. package/lib/OAuthProvidersApi.js +6 -2
  38. package/lib/OAuthProvidersApi.js.map +1 -1
  39. package/lib/RunsApi.d.ts +3 -2
  40. package/lib/RunsApi.d.ts.map +1 -1
  41. package/lib/RunsApi.js +3 -2
  42. package/lib/RunsApi.js.map +1 -1
  43. package/lib/StreamSource.d.ts +19 -1
  44. package/lib/StreamSource.d.ts.map +1 -1
  45. package/lib/StreamSource.js +31 -1
  46. package/lib/StreamSource.js.map +1 -1
  47. package/lib/UsersApi.d.ts +7 -1
  48. package/lib/UsersApi.d.ts.map +1 -1
  49. package/lib/UsersApi.js +8 -0
  50. package/lib/UsersApi.js.map +1 -1
  51. package/lib/client.d.ts +14 -2
  52. package/lib/client.d.ts.map +1 -1
  53. package/lib/client.js +55 -8
  54. package/lib/client.js.map +1 -1
  55. package/lib/execute.d.ts +16 -2
  56. package/lib/execute.d.ts.map +1 -1
  57. package/lib/execute.js +16 -3
  58. package/lib/execute.js.map +1 -1
  59. package/lib/idempotency-key.d.ts +19 -0
  60. package/lib/idempotency-key.d.ts.map +1 -0
  61. package/lib/idempotency-key.js +34 -0
  62. package/lib/idempotency-key.js.map +1 -0
  63. package/lib/index.d.ts +1 -0
  64. package/lib/index.d.ts.map +1 -1
  65. package/lib/index.js.map +1 -1
  66. package/lib/nodejs/NodeStreamSource.d.ts +11 -1
  67. package/lib/nodejs/NodeStreamSource.d.ts.map +1 -1
  68. package/lib/nodejs/NodeStreamSource.js +56 -16
  69. package/lib/nodejs/NodeStreamSource.js.map +1 -1
  70. package/lib/store/AgentsApi.d.ts +3 -0
  71. package/lib/store/AgentsApi.d.ts.map +1 -1
  72. package/lib/store/AgentsApi.js +10 -1
  73. package/lib/store/AgentsApi.js.map +1 -1
  74. package/lib/store/ContentSourcesApi.d.ts +52 -0
  75. package/lib/store/ContentSourcesApi.d.ts.map +1 -0
  76. package/lib/store/ContentSourcesApi.js +85 -0
  77. package/lib/store/ContentSourcesApi.js.map +1 -0
  78. package/lib/store/CostApi.d.ts +6 -1
  79. package/lib/store/CostApi.d.ts.map +1 -1
  80. package/lib/store/CostApi.js +7 -0
  81. package/lib/store/CostApi.js.map +1 -1
  82. package/lib/store/DashboardApi.d.ts +61 -19
  83. package/lib/store/DashboardApi.d.ts.map +1 -1
  84. package/lib/store/DashboardApi.js +75 -19
  85. package/lib/store/DashboardApi.js.map +1 -1
  86. package/lib/store/DataApi.d.ts +9 -9
  87. package/lib/store/DataApi.d.ts.map +1 -1
  88. package/lib/store/DataApi.js +14 -10
  89. package/lib/store/DataApi.js.map +1 -1
  90. package/lib/store/FilesApi.d.ts +15 -1
  91. package/lib/store/FilesApi.d.ts.map +1 -1
  92. package/lib/store/FilesApi.js +54 -69
  93. package/lib/store/FilesApi.js.map +1 -1
  94. package/lib/store/IndexingApi.d.ts +0 -6
  95. package/lib/store/IndexingApi.d.ts.map +1 -1
  96. package/lib/store/IndexingApi.js +3 -17
  97. package/lib/store/IndexingApi.js.map +1 -1
  98. package/lib/store/NotificationsApi.d.ts +39 -0
  99. package/lib/store/NotificationsApi.d.ts.map +1 -0
  100. package/lib/store/NotificationsApi.js +68 -0
  101. package/lib/store/NotificationsApi.js.map +1 -0
  102. package/lib/store/ObjectsApi.d.ts +6 -7
  103. package/lib/store/ObjectsApi.d.ts.map +1 -1
  104. package/lib/store/ObjectsApi.js +25 -46
  105. package/lib/store/ObjectsApi.js.map +1 -1
  106. package/lib/store/RenderingApi.d.ts.map +1 -1
  107. package/lib/store/RenderingApi.js +1 -0
  108. package/lib/store/RenderingApi.js.map +1 -1
  109. package/lib/store/WorkflowsApi.d.ts +19 -1
  110. package/lib/store/WorkflowsApi.d.ts.map +1 -1
  111. package/lib/store/WorkflowsApi.js +21 -0
  112. package/lib/store/WorkflowsApi.js.map +1 -1
  113. package/lib/store/chunked-upload.d.ts +71 -0
  114. package/lib/store/chunked-upload.d.ts.map +1 -0
  115. package/lib/store/chunked-upload.js +206 -0
  116. package/lib/store/chunked-upload.js.map +1 -0
  117. package/lib/store/client.d.ts +9 -1
  118. package/lib/store/client.d.ts.map +1 -1
  119. package/lib/store/client.js +15 -1
  120. package/lib/store/client.js.map +1 -1
  121. package/lib/store/index.d.ts +3 -0
  122. package/lib/store/index.d.ts.map +1 -1
  123. package/lib/store/index.js +3 -0
  124. package/lib/store/index.js.map +1 -1
  125. package/lib/store/node-request.browser.d.ts +14 -0
  126. package/lib/store/node-request.browser.d.ts.map +1 -0
  127. package/lib/store/node-request.browser.js +20 -0
  128. package/lib/store/node-request.browser.js.map +1 -0
  129. package/lib/store/node-request.d.ts +20 -0
  130. package/lib/store/node-request.d.ts.map +1 -0
  131. package/lib/store/node-request.js +116 -0
  132. package/lib/store/node-request.js.map +1 -0
  133. package/lib/store/node-sessions.browser.d.ts +3 -0
  134. package/lib/store/node-sessions.browser.d.ts.map +1 -0
  135. package/lib/store/node-sessions.browser.js +15 -0
  136. package/lib/store/node-sessions.browser.js.map +1 -0
  137. package/lib/store/node-sessions.d.ts +14 -0
  138. package/lib/store/node-sessions.d.ts.map +1 -0
  139. package/lib/store/node-sessions.js +88 -0
  140. package/lib/store/node-sessions.js.map +1 -0
  141. package/lib/store/signed-url.d.ts +12 -16
  142. package/lib/store/signed-url.d.ts.map +1 -1
  143. package/lib/store/signed-url.js +111 -71
  144. package/lib/store/signed-url.js.map +1 -1
  145. package/lib/store/stream-bytes.d.ts +16 -0
  146. package/lib/store/stream-bytes.d.ts.map +1 -0
  147. package/lib/store/stream-bytes.js +70 -0
  148. package/lib/store/stream-bytes.js.map +1 -0
  149. package/lib/store/upload-body.d.ts +21 -0
  150. package/lib/store/upload-body.d.ts.map +1 -0
  151. package/lib/store/upload-body.js +32 -0
  152. package/lib/store/upload-body.js.map +1 -0
  153. package/lib/store/upload-content.d.ts +21 -0
  154. package/lib/store/upload-content.d.ts.map +1 -0
  155. package/lib/store/upload-content.js +113 -0
  156. package/lib/store/upload-content.js.map +1 -0
  157. package/lib/store/upload-runtime.d.ts +21 -0
  158. package/lib/store/upload-runtime.d.ts.map +1 -0
  159. package/lib/store/upload-runtime.js +40 -0
  160. package/lib/store/upload-runtime.js.map +1 -0
  161. package/lib/store/zeno-bulk.d.ts +9 -0
  162. package/lib/store/zeno-bulk.d.ts.map +1 -0
  163. package/lib/store/zeno-bulk.js +26 -0
  164. package/lib/store/zeno-bulk.js.map +1 -0
  165. package/lib/vertesia-client.js +10 -9
  166. package/lib/vertesia-client.js.map +1 -1
  167. package/package.json +38 -9
  168. package/src/AccountApi.ts +3 -0
  169. package/src/AccountRolesApi.ts +29 -0
  170. package/src/ApiKeysApi.ts +4 -12
  171. package/src/AppsApi.ts +58 -4
  172. package/src/AuditTrailApi.ts +11 -0
  173. package/src/IamApi.ts +1 -1
  174. package/src/InteractionCatalogApi.ts +44 -1
  175. package/src/InteractionsApi.ts +11 -33
  176. package/src/OAuthClientsApi.ts +6 -0
  177. package/src/OAuthProvidersApi.ts +7 -2
  178. package/src/RunsApi.ts +7 -2
  179. package/src/StreamSource.ts +29 -0
  180. package/src/UsersApi.ts +10 -1
  181. package/src/client.ts +72 -8
  182. package/src/execute.ts +33 -4
  183. package/src/idempotency-key.ts +34 -0
  184. package/src/index.ts +1 -0
  185. package/src/nodejs/NodeStreamSource.ts +63 -24
  186. package/src/store/AgentsApi.ts +13 -1
  187. package/src/store/ContentSourcesApi.ts +126 -0
  188. package/src/store/CostApi.ts +10 -0
  189. package/src/store/DashboardApi.ts +93 -18
  190. package/src/store/DataApi.ts +14 -10
  191. package/src/store/FilesApi.ts +84 -72
  192. package/src/store/IndexingApi.ts +3 -18
  193. package/src/store/NotificationsApi.ts +103 -0
  194. package/src/store/ObjectsApi.ts +32 -51
  195. package/src/store/RenderingApi.ts +1 -0
  196. package/src/store/WorkflowsApi.ts +25 -0
  197. package/src/store/chunked-upload.ts +272 -0
  198. package/src/store/client.ts +21 -1
  199. package/src/store/index.ts +3 -0
  200. package/src/store/node-request.browser.ts +22 -0
  201. package/src/store/node-request.ts +120 -0
  202. package/src/store/node-sessions.browser.ts +16 -0
  203. package/src/store/node-sessions.ts +109 -0
  204. package/src/store/signed-url.ts +126 -72
  205. package/src/store/stream-bytes.ts +79 -0
  206. package/src/store/upload-body.ts +37 -0
  207. package/src/store/upload-content.ts +129 -0
  208. package/src/store/upload-runtime.ts +42 -0
  209. package/src/store/zeno-bulk.ts +30 -0
package/src/client.ts CHANGED
@@ -1,6 +1,18 @@
1
- import { AbstractFetchClient, type FETCH_FN, type IRequestRetryPolicy } from '@vertesia/api-fetch-client';
1
+ import {
2
+ AbstractFetchClient,
3
+ type FETCH_FN,
4
+ type IPacingRetryPolicy,
5
+ type IRequestRetryPolicy,
6
+ type RequestIdentityProvider,
7
+ } from '@vertesia/api-fetch-client';
2
8
  import type { AppSessionTokenRequest, AppSessionTokenResponse } from '@vertesia/common';
3
- import { APP_VERSION_HEADER, type AuthTokenPayload, type AuthTokenResponse } from '@vertesia/common';
9
+ import {
10
+ APP_VERSION_HEADER,
11
+ type AuthTokenPayload,
12
+ type AuthTokenResponse,
13
+ type ContentSecurity,
14
+ } from '@vertesia/common';
15
+ import { resolveAbacClaim, TokenClaimReferenceError } from '@vertesia/studio-utils/token-claims';
4
16
  import AccountApi from './AccountApi.js';
5
17
  import AccountsApi from './AccountsApi.js';
6
18
  import AnalyticsApi from './AnalyticsApi.js';
@@ -68,10 +80,14 @@ export type VertesiaClientProps = {
68
80
  storeUrl?: string;
69
81
  tokenServerUrl?: string;
70
82
  apikey?: string;
83
+ /** Supplies the complete Authorization header; takes precedence over apikey. */
84
+ authCallback?: () => Promise<string>;
71
85
  sessionTags?: string | string[];
86
+ requestIdentity?: RequestIdentityProvider;
72
87
  onRequest?: (request: Request) => void;
73
88
  onResponse?: (response: Response) => void;
74
89
  retryPolicy?: IRequestRetryPolicy;
90
+ pacingRetry?: IPacingRetryPolicy | false;
75
91
  /**
76
92
  * Default request timeout in ms applied to every request (studio + store) unless overridden
77
93
  * per-request via `timeoutMs`. `false`/`null`/`0` disables it. Aborts the whole request
@@ -92,10 +108,13 @@ const KNOWN_CLIENT_OPTIONS: Record<keyof Required<VertesiaClientProps>, true> =
92
108
  storeUrl: true,
93
109
  tokenServerUrl: true,
94
110
  apikey: true,
111
+ authCallback: true,
95
112
  sessionTags: true,
113
+ requestIdentity: true,
96
114
  onRequest: true,
97
115
  onResponse: true,
98
116
  retryPolicy: true,
117
+ pacingRetry: true,
99
118
  timeout: true,
100
119
  fetch: true,
101
120
  };
@@ -105,6 +124,7 @@ export class VertesiaClient extends AbstractFetchClient<VertesiaClient> {
105
124
  * The JWT token linked to the API KEY (sk or pk)
106
125
  */
107
126
  _jwt: string | null = null;
127
+ private abacResolution?: { jwt: string; value: Promise<ContentSecurity | undefined> };
108
128
 
109
129
  /**
110
130
  * The store client
@@ -230,9 +250,12 @@ export class VertesiaClient extends AbstractFetchClient<VertesiaClient> {
230
250
  serverUrl: zenoServerUrl,
231
251
  tokenServerUrl: this.tokenServerUrl,
232
252
  apikey: opts.apikey,
253
+ authCallback: opts.authCallback,
254
+ requestIdentity: opts.requestIdentity,
233
255
  onRequest: opts.onRequest,
234
256
  onResponse: opts.onResponse,
235
257
  retryPolicy: opts.retryPolicy,
258
+ pacingRetry: opts.pacingRetry,
236
259
  timeout: opts.timeout,
237
260
  fetch: opts.fetch,
238
261
  });
@@ -242,14 +265,21 @@ export class VertesiaClient extends AbstractFetchClient<VertesiaClient> {
242
265
  this.withRetryPolicy(opts.retryPolicy);
243
266
  }
244
267
 
268
+ if (opts.pacingRetry !== undefined) {
269
+ this.withPacingRetry(opts.pacingRetry);
270
+ }
271
+
245
272
  if (opts.timeout !== undefined) {
246
273
  this.withTimeout(opts.timeout);
247
274
  }
248
275
 
249
- if (opts.apikey) {
276
+ if (opts.authCallback) {
277
+ this.withAuthCallback(opts.authCallback);
278
+ } else if (opts.apikey) {
250
279
  void this.withApiKey(opts.apikey);
251
280
  }
252
281
 
282
+ this.requestIdentity = opts.requestIdentity;
253
283
  this.onRequest = opts.onRequest;
254
284
  this.onResponse = opts.onResponse;
255
285
  this.sessionTags = opts.sessionTags;
@@ -325,12 +355,9 @@ export class VertesiaClient extends AbstractFetchClient<VertesiaClient> {
325
355
  }
326
356
 
327
357
  async getRawJWT() {
328
- if (!this._jwt && this._auth) {
358
+ if (this._auth) {
329
359
  const auth = await this._auth();
330
- if (!this._jwt) {
331
- // the _jwt may be set by the auth callback
332
- this._jwt = auth.trim().split(' ')[1]; // remove Bearer prefix
333
- }
360
+ this._jwt = auth.trim().split(' ')[1]; // remove Bearer prefix
334
361
  }
335
362
  return this._jwt || null;
336
363
  }
@@ -340,6 +367,38 @@ export class VertesiaClient extends AbstractFetchClient<VertesiaClient> {
340
367
  return jwt ? decodeJWT(jwt) : null;
341
368
  }
342
369
 
370
+ /**
371
+ * Resolve ABAC only when it is needed. JWT decoding alone deliberately keeps opaque claim
372
+ * references intact. The existing STS validation endpoint verifies the complete bearer before
373
+ * loading Redis; knowing a reference alone never authorizes reading its policy.
374
+ */
375
+ async getAbac(): Promise<ContentSecurity | undefined> {
376
+ const jwt = await this.getRawJWT();
377
+ if (!jwt) return undefined;
378
+ if (this.abacResolution?.jwt === jwt) return this.abacResolution.value;
379
+
380
+ const payload = decodeJWT(jwt);
381
+ const value = resolveAbacClaim(payload.abac ?? payload.content_security, async () => {
382
+ const response = await this.post<{ valid: boolean; payload?: AuthTokenPayload }>(
383
+ new URL('/validate', this.tokenServerUrl).toString(),
384
+ { payload: { token: jwt } },
385
+ );
386
+ const abac = response.payload?.abac;
387
+ if (!response.valid || !abac || typeof abac !== 'object' || Array.isArray(abac)) {
388
+ throw new TokenClaimReferenceError('STS could not resolve token authorization context');
389
+ }
390
+ return { abac };
391
+ });
392
+ const entry = { jwt, value };
393
+ this.abacResolution = entry;
394
+ try {
395
+ return await value;
396
+ } catch (error: unknown) {
397
+ if (this.abacResolution === entry) this.abacResolution = undefined;
398
+ throw error;
399
+ }
400
+ }
401
+
343
402
  async getProject() {
344
403
  const jwt = await this.getDecodedJWT();
345
404
  return jwt?.project || null;
@@ -375,6 +434,10 @@ export class VertesiaClient extends AbstractFetchClient<VertesiaClient> {
375
434
  return this.store.files;
376
435
  }
377
436
 
437
+ get contentSources() {
438
+ return this.store.contentSources;
439
+ }
440
+
378
441
  get processes() {
379
442
  return this.store.processes;
380
443
  }
@@ -507,6 +570,7 @@ export function decodeJWT(jwt: string): AuthTokenPayload {
507
570
  throw new Error(`Invalid auth token: payload segment is not base64url (${(err as Error).message})`);
508
571
  }
509
572
 
573
+ // Decoding is not authentication; servers verify the signature and permission contract.
510
574
  try {
511
575
  return JSON.parse(decodedJson);
512
576
  } catch {
package/src/execute.ts CHANGED
@@ -2,6 +2,7 @@ import type { IRequestParams } from '@vertesia/api-fetch-client';
2
2
  import {
3
3
  type AsyncExecutionPayload,
4
4
  type AsyncExecutionResult,
5
+ type ExecuteNamedInteractionQuery,
5
6
  ExecutionRunStatus,
6
7
  type InteractionExecutionPayload,
7
8
  type InteractionExecutionResult,
@@ -10,6 +11,7 @@ import {
10
11
  type RateLimitRequestResponse,
11
12
  } from '@vertesia/common';
12
13
  import type { VertesiaClient } from './client.js';
14
+ import { withIdempotencyKey } from './idempotency-key.js';
13
15
 
14
16
  /**
15
17
  * Client-side timeout for a synchronous (non-streaming) interaction execution. The request blocks on
@@ -23,6 +25,20 @@ const INTERACTION_EXECUTION_TIMEOUT_MS = (() => {
23
25
  return Number.isFinite(parsed) && parsed > 0 ? parsed : 30 * 60 * 1000; // 30 minutes
24
26
  })();
25
27
 
28
+ /**
29
+ * Per-request options for a synchronous named-interaction execution: the request options this method
30
+ * can honour, plus the published execute query.
31
+ *
32
+ * `reader` and `jsonPayload` are left out: the method parses the JSON execution result and serializes
33
+ * the payload it builds, so either would break the call. `query` is the typed execute query instead of
34
+ * a free-form record, so a misspelt parameter fails to compile rather than being ignored by the server.
35
+ * With `onChunk`, the options apply to the initial request only; the stream that follows is a separate
36
+ * connection.
37
+ */
38
+ export type ExecuteInteractionRequestOptions = Omit<IRequestParams, 'reader' | 'jsonPayload' | 'query'> & {
39
+ query?: ExecuteNamedInteractionQuery;
40
+ };
41
+
26
42
  export async function EventSourceProvider(): Promise<typeof EventSource> {
27
43
  if (typeof globalThis.EventSource === 'function') {
28
44
  return globalThis.EventSource;
@@ -81,6 +97,7 @@ export async function executeInteraction<P = unknown>(
81
97
  * @param interaction
82
98
  * @param payload
83
99
  * @param onChunk
100
+ * @param requestOptions request options, e.g. `{ query: { include_prompt: false } }` to omit the rendered prompt
84
101
  * @returns
85
102
  */
86
103
  export async function executeInteractionByName<P = unknown>(
@@ -88,7 +105,7 @@ export async function executeInteractionByName<P = unknown>(
88
105
  interaction: string,
89
106
  payload: InteractionExecutionPayload = {},
90
107
  onChunk?: (chunk: string) => void,
91
- requestOptions?: IRequestParams,
108
+ requestOptions?: ExecuteInteractionRequestOptions,
92
109
  ): Promise<InteractionExecutionResult<P>> {
93
110
  const stream = !!onChunk;
94
111
  const response = await client.post<InteractionExecutionResult<P>>('/api/v1/execute', {
@@ -98,8 +115,14 @@ export async function executeInteractionByName<P = unknown>(
98
115
  interaction,
99
116
  stream,
100
117
  } as NamedInteractionExecutionPayload,
101
- // Synchronous interaction execution blocks on the model; keep a long client timeout.
102
- timeoutMs: requestOptions?.timeoutMs ?? (stream ? undefined : INTERACTION_EXECUTION_TIMEOUT_MS),
118
+ // Synchronous interaction execution blocks on the model; keep a long client timeout. An explicit
119
+ // timeout wins, including `null`, `false` or `0`, which disable it.
120
+ timeoutMs:
121
+ requestOptions?.timeoutMs !== undefined
122
+ ? requestOptions.timeoutMs
123
+ : stream
124
+ ? undefined
125
+ : INTERACTION_EXECUTION_TIMEOUT_MS,
103
126
  });
104
127
  if (stream) {
105
128
  if (response.status === ExecutionRunStatus.failed) {
@@ -167,8 +190,14 @@ export async function executeInteractionAsync(
167
190
  client: VertesiaClient,
168
191
  payload: AsyncExecutionPayload,
169
192
  ): Promise<AsyncExecutionResult> {
193
+ // A conversation starts an agent run: without `idempotency_key` a replay could start a second one,
194
+ // so opt out of any client-level retry policy (see `AgentsApi.start`). With a key, a replay
195
+ // returns the run the first request started.
196
+ const keyedPayload = payload.type === 'conversation' ? withIdempotencyKey(client, payload) : payload;
197
+ const unkeyedConversation = keyedPayload.type === 'conversation' && !keyedPayload.idempotency_key;
170
198
  return await client.post('/api/v1/execute/async', {
171
- payload,
199
+ payload: keyedPayload,
200
+ retryPolicy: unkeyedConversation ? false : undefined,
172
201
  });
173
202
  }
174
203
 
@@ -0,0 +1,34 @@
1
+ import { type ClientBase, generateIdempotencyKey } from '@vertesia/api-fetch-client';
2
+
3
+ /**
4
+ * Give a create request its own `idempotency_key` when the client's retry policy asks for idempotency
5
+ * keys and the caller set none, so a retried attempt returns what the first one created instead of
6
+ * creating it again. The server stores the key with the resource it creates. A caller's key, or a
7
+ * client without the opt-in, leaves the payload unchanged.
8
+ */
9
+ export function withIdempotencyKey<T extends { idempotency_key?: string }>(client: ClientBase, payload: T): T {
10
+ if (payload.idempotency_key || !client.getRetryPolicy()?.idempotencyKeys) {
11
+ return payload;
12
+ }
13
+ return {
14
+ ...payload,
15
+ idempotency_key: client.getRetryPolicy()?.idempotencyKey?.(payload) ?? generateIdempotencyKey(),
16
+ };
17
+ }
18
+
19
+ /**
20
+ * Give a data-store import its own `import_id` under the same opt-in as {@link withIdempotencyKey}. The server
21
+ * takes the ID as the import job's ObjectId and refuses an unknown one older than 24 hours, so it is a fresh
22
+ * ObjectId: the current time in seconds, then random bytes.
23
+ */
24
+ export function withImportId<T extends { import_id?: string }>(client: ClientBase, payload: T): T {
25
+ if (payload.import_id || !client.getRetryPolicy()?.idempotencyKeys) {
26
+ return payload;
27
+ }
28
+ const durableId = client.getRetryPolicy()?.importId?.(payload);
29
+ if (durableId) return { ...payload, import_id: durableId };
30
+ const seconds = Math.floor(Date.now() / 1000)
31
+ .toString(16)
32
+ .padStart(8, '0');
33
+ return { ...payload, import_id: seconds + generateIdempotencyKey().replaceAll('-', '').slice(0, 16) };
34
+ }
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ export type { EnableEnvironmentModelPayload, OrphanedAppInstallation } from '@ve
4
4
  export { getOAuthPermissionScopes, Permission } from '@vertesia/common';
5
5
  export * from './client.js';
6
6
  export { default as DelegationGrantsApi } from './DelegationGrantsApi.js';
7
+ export type { ExecuteInteractionRequestOptions } from './execute.js';
7
8
  export type { GroupsQueryOptions } from './GroupsApi.js';
8
9
  export * from './InteractionBase.js';
9
10
  export * from './InteractionOutput.js';
@@ -1,42 +1,81 @@
1
+ import * as fs from 'node:fs';
1
2
  import type { Readable } from 'node:stream';
2
- import { ReadableStream as NodeReadableStream } from 'node:stream/web';
3
+ import { finished } from 'node:stream/promises';
3
4
  import { StreamSource } from '../StreamSource.js';
4
5
 
5
6
  /**
6
7
  * A stream source that wraps a Node.js Readable stream.
7
8
  * This class is only works in Node.js environments.
9
+ * Cancelling the web stream destroys the Node.js stream, releasing its underlying resource (e.g. a file descriptor).
10
+ *
11
+ * When the stream reads a whole local file (`fs.createReadStream(path)`), uploads send a file-backed `Blob`
12
+ * instead of the stream (see {@link replayableBody}): it can be retried without holding the file in memory.
8
13
  */
9
14
  export class NodeStreamSource extends StreamSource {
10
- constructor(stream: Readable, name: string, type?: string, id?: string) {
11
- super(readableToWebStream(stream) as ReadableStream, name, type, id);
15
+ private readonly filePath: string | undefined;
16
+
17
+ constructor(stream: Readable, name: string, type?: string, id?: string, size?: number) {
18
+ // read before wrapping: the web stream may start reading the Node.js stream
19
+ const filePath = wholeFilePath(stream);
20
+ super(readableToWebStream(stream) as ReadableStream, name, type, id, size);
21
+ this.filePath = filePath;
12
22
  }
13
- }
14
23
 
15
- function readableToWebStream<T = string | Buffer>(stream: Readable): ReadableStream<T> {
16
- if (NodeReadableStream.from) {
17
- return NodeReadableStream.from(stream) as ReadableStream<T>;
18
- } else {
19
- return readableToWebStream_BUN(stream);
24
+ /**
25
+ * A `Blob` backed by the file the stream reads, from Node's `fs.openAsBlob`. Reading it reads the file from
26
+ * disk on demand, so each upload attempt streams the file again instead of buffering it.
27
+ */
28
+ override async replayableBody(): Promise<Blob | undefined> {
29
+ if (!this.filePath || typeof fs.openAsBlob !== 'function') return undefined;
30
+ return (await fs.openAsBlob(this.filePath, { type: this.type })) as Blob;
20
31
  }
21
32
  }
22
33
 
23
34
  /**
24
- * Bun implementation since bun is not yet supporting NodeReadableStream.from
25
- * @returns
35
+ * The path of the file a stream reads, when it reads all of it from the start: an `fs.ReadStream` opened by
36
+ * path with no range or encoding that nothing has read yet. Any other stream (a range, a descriptor, one that
37
+ * decodes the file to text, a stream already consumed, or one that is not a file) has no path, and is uploaded as
38
+ * a stream.
26
39
  */
27
- function readableToWebStream_BUN<T = string | Buffer>(stream: Readable): ReadableStream<T> {
40
+ function wholeFilePath(stream: Readable): string | undefined {
41
+ if (!(stream instanceof fs.ReadStream)) return undefined;
42
+ const { path, start, end } = stream as fs.ReadStream & { start?: number; end?: number };
43
+ if (typeof path !== 'string') return undefined;
44
+ if ((start ?? 0) !== 0 || end !== Infinity) return undefined;
45
+ // an encoding uploads the decoded text (e.g. base64), not the file's bytes
46
+ if (stream.readableEncoding) return undefined;
47
+ if (stream.bytesRead > 0 || stream.readableDidRead) return undefined;
48
+ return path;
49
+ }
50
+
51
+ /**
52
+ * Not ReadableStream.from: it cancels by calling return() on the Node.js async iterator, which is a no-op
53
+ * when nothing was read yet, so the Node.js stream would stay open. Also covers Bun, which lacks ReadableStream.from.
54
+ */
55
+ function readableToWebStream<T = string | Buffer>(stream: Readable): ReadableStream<T> {
56
+ // Nothing listens until the first read, so an error before it (e.g. a failed lazy file open) would otherwise
57
+ // crash the process. The iterator still reports the error on the next read.
58
+ stream.on('error', () => undefined);
28
59
  const it = stream[Symbol.asyncIterator]();
29
- return new ReadableStream<T>({
30
- async pull(controller) {
31
- const { value, done } = await it.next();
32
- if (done) {
33
- controller.close();
34
- } else {
35
- controller.enqueue(value);
36
- }
37
- },
38
- cancel() {
39
- it.return?.();
60
+ return new ReadableStream<T>(
61
+ {
62
+ async pull(controller) {
63
+ const { value, done } = await it.next();
64
+ if (done) {
65
+ controller.close();
66
+ } else {
67
+ controller.enqueue(value);
68
+ }
69
+ },
70
+ async cancel() {
71
+ // no error argument: it would emit 'error', which the caller may not listen for
72
+ stream.destroy();
73
+ // resolve once the underlying resource is released, so callers can remove the file afterwards;
74
+ // bounded because some streams never report their close
75
+ await finished(stream, { signal: AbortSignal.timeout(5_000) }).catch(() => undefined);
76
+ },
40
77
  },
41
- });
78
+ // read on demand, like ReadableStream.from
79
+ { highWaterMark: 0 },
80
+ );
42
81
  }
@@ -57,6 +57,7 @@ import {
57
57
  } from '@vertesia/common';
58
58
  import type { VertesiaClient } from '../client.js';
59
59
  import { EventSourceProvider } from '../execute.js';
60
+ import { withIdempotencyKey } from '../idempotency-key.js';
60
61
  import { fetchSignedUrl } from './signed-url.js';
61
62
  import { shouldCloseAgentRunStream, shouldCloseCompactRunStream } from './stream-termination.js';
62
63
 
@@ -87,13 +88,24 @@ export class AgentsApi extends ApiTopic {
87
88
  /**
88
89
  * Create and start a new agent run.
89
90
  * Returns the created AgentRun with its stable id.
91
+ *
92
+ * Set `idempotency_key` on an agent run to make the call safe to retry: the client's retry policy
93
+ * then applies, and a replayed request returns the run the first one started.
90
94
  */
91
95
  start<TData = Record<string, unknown>>(payload: CreateAgentRunPayload<TData>): Promise<AgentRun<TData>>;
92
96
  start<TData = Record<string, unknown>>(payload: CreateProcessRunPayload<TData>): Promise<ProcessRun>;
93
97
  start<TData = Record<string, unknown>>(
94
98
  payload: CreateAgentRunPayload<TData> | CreateProcessRunPayload<TData>,
95
99
  ): Promise<AgentRun<TData> | ProcessRun> {
96
- return this.post('/', { payload });
100
+ // Without a key, starting an agent run is not idempotent: a request abandoned client-side can
101
+ // still complete server-side, so a replay would start a second run. Opt out of any client-level
102
+ // retry policy explicitly rather than relying on POST being absent from its method list.
103
+ // Rate-limiter pacing retries are unaffected: those happen before the request reaches the
104
+ // handler. Process starts take no key and keep the client's policy.
105
+ // A client whose policy sends idempotency keys gives the run a generated key instead.
106
+ const keyedPayload = 'interaction' in payload ? withIdempotencyKey(this.client, payload) : payload;
107
+ const unkeyedAgentStart = 'interaction' in keyedPayload && !keyedPayload.idempotency_key;
108
+ return this.post('/', { payload: keyedPayload, retryPolicy: unkeyedAgentStart ? false : undefined });
97
109
  }
98
110
 
99
111
  /**
@@ -0,0 +1,126 @@
1
+ import { ApiTopic, type ClientBase } from '@vertesia/api-fetch-client';
2
+ import type {
3
+ ContentSourceFoldersDiscovery,
4
+ ContentSourceReport,
5
+ ContentSourceReportQuery,
6
+ ContentSourceRootsDiscovery,
7
+ ContentSourceSampleDiscovery,
8
+ ContentSourceSchemaDiscovery,
9
+ ContentSourceSiteDiscovery,
10
+ ContentSourceStatus,
11
+ CreateContentSourcePayload,
12
+ DeleteCountResult,
13
+ DiscoverContentSourceFoldersPayload,
14
+ DiscoverContentSourceSamplePayload,
15
+ DiscoverContentSourceSchemaPayload,
16
+ DiscoverContentSourceSitePayload,
17
+ ExternalContentSource,
18
+ InspectContentSourceFoldersPayload,
19
+ InspectContentSourceSamplePayload,
20
+ InspectContentSourceSchemaPayload,
21
+ SyncContentSourcePayload,
22
+ UpdateContentSourcePayload,
23
+ } from '@vertesia/common';
24
+
25
+ /**
26
+ * Folders of external content systems (SharePoint, Box) synced into the project without copies. Each source has
27
+ * a sync that imports the folder, then reads its changes on a schedule or on request.
28
+ */
29
+ export class ContentSourcesApi extends ApiTopic {
30
+ constructor(parent: ClientBase) {
31
+ super(parent, '/api/v1/content-sources');
32
+ }
33
+
34
+ list(): Promise<ExternalContentSource[]> {
35
+ return this.get('/');
36
+ }
37
+
38
+ retrieve(id: string): Promise<ExternalContentSource> {
39
+ return this.get(`/${id}`);
40
+ }
41
+
42
+ /** Checks the folder with the system and registers the source. Nothing is imported until `sync` is called. */
43
+ create(payload: CreateContentSourcePayload): Promise<ExternalContentSource> {
44
+ return this.post('/', { payload });
45
+ }
46
+
47
+ /** Changes the name, description or sync settings, and, before the first sync, the root and folder. */
48
+ update(id: string, payload: UpdateContentSourcePayload): Promise<ExternalContentSource> {
49
+ return this.put(`/${id}`, { payload });
50
+ }
51
+
52
+ /** Stops the sync and removes the source. Synced documents are kept. */
53
+ delete(id: string): Promise<DeleteCountResult> {
54
+ return this.del(`/${id}`);
55
+ }
56
+
57
+ /** Syncs now: reads the changes, or, with `full`, lists the whole folder and removes deleted files. */
58
+ sync(id: string, payload: SyncContentSourcePayload = {}): Promise<ContentSourceStatus> {
59
+ return this.post(`/${id}/sync`, { payload });
60
+ }
61
+
62
+ /** The sync's state: the current and last rounds, and when the next one starts. */
63
+ status(id: string): Promise<ContentSourceStatus> {
64
+ return this.get(`/${id}/status`);
65
+ }
66
+
67
+ /**
68
+ * A page of what a sync round did with each file: created, updated, deleted, skipped or failed. Round IDs are in
69
+ * the status's `recent_rounds`. Pass the `next_cursor` of a page to read the next one.
70
+ */
71
+ report(id: string, roundId: string, query: ContentSourceReportQuery = {}): Promise<ContentSourceReport> {
72
+ return this.get(`/${id}/rounds/${encodeURIComponent(roundId)}/report`, { query: { ...query } });
73
+ }
74
+
75
+ /**
76
+ * Resolves a site through an OAuth provider and lists its document libraries. Works before a source exists,
77
+ * so it also tells whether the connection is set up correctly.
78
+ */
79
+ discoverSite(payload: DiscoverContentSourceSitePayload): Promise<ContentSourceSiteDiscovery> {
80
+ return this.post('/discover/site', { payload });
81
+ }
82
+
83
+ /** The direct subfolders of a folder of a document library, to browse to the folder to sync. */
84
+ discoverFolders(payload: DiscoverContentSourceFoldersPayload): Promise<ContentSourceFoldersDiscovery> {
85
+ return this.post('/discover/folders', { payload });
86
+ }
87
+
88
+ /** The content types of a document library with their columns, and the library's own columns. */
89
+ discoverSchema(payload: DiscoverContentSourceSchemaPayload): Promise<ContentSourceSchemaDiscovery> {
90
+ return this.post('/discover/schema', { payload });
91
+ }
92
+
93
+ /**
94
+ * A bounded sample of the files under a folder, with each file's content type and column values. The sample is
95
+ * spread over subfolders; `truncated` says whether the folder holds more than was read.
96
+ */
97
+ discoverSample(payload: DiscoverContentSourceSamplePayload): Promise<ContentSourceSampleDiscovery> {
98
+ return this.post('/discover/sample', { payload });
99
+ }
100
+
101
+ /**
102
+ * What a source's connection reaches: the document libraries of its SharePoint site, or the Box folders shared
103
+ * with its app. The caller names no OAuth provider or address: the source holds them.
104
+ */
105
+ inspectRoots(id: string): Promise<ContentSourceRootsDiscovery> {
106
+ return this.post(`/${id}/discover/roots`);
107
+ }
108
+
109
+ /** The direct subfolders of a folder the source reaches, at the top of its root by default. */
110
+ inspectFolders(
111
+ id: string,
112
+ payload: InspectContentSourceFoldersPayload = {},
113
+ ): Promise<ContentSourceFoldersDiscovery> {
114
+ return this.post(`/${id}/discover/folders`, { payload });
115
+ }
116
+
117
+ /** The content types (SharePoint) or metadata templates (Box) and their columns. */
118
+ inspectSchema(id: string, payload: InspectContentSourceSchemaPayload = {}): Promise<ContentSourceSchemaDiscovery> {
119
+ return this.post(`/${id}/discover/schema`, { payload });
120
+ }
121
+
122
+ /** A bounded sample of the files under a folder the source reaches, its own folder by default. */
123
+ inspectSample(id: string, payload: InspectContentSourceSamplePayload = {}): Promise<ContentSourceSampleDiscovery> {
124
+ return this.post(`/${id}/discover/sample`, { payload });
125
+ }
126
+ }
@@ -6,6 +6,8 @@ import type {
6
6
  CostModelPricesQuery,
7
7
  CostRunPriceQuery,
8
8
  CostRunPriceResponse,
9
+ IntakeCostSummaryQuery,
10
+ IntakeCostSummaryResponse,
9
11
  ModelPriceComparisonResponse,
10
12
  } from '@vertesia/common';
11
13
 
@@ -30,6 +32,14 @@ export class CostApi extends ApiTopic {
30
32
  return this.get('/model-prices', { query: { ...query } });
31
33
  }
32
34
 
35
+ /**
36
+ * The LLM and embedding cost of the current project's intake runs over a window: totals, a time series, and
37
+ * breakdowns by content type, outcome and document. Requires a billing read permission.
38
+ */
39
+ getIntakeCostSummary(query: IntakeCostSummaryQuery = {}): Promise<IntakeCostSummaryResponse> {
40
+ return this.get('/intake', { query: { ...query } });
41
+ }
42
+
33
43
  /**
34
44
  * Price a single interaction run or agent run.
35
45
  */