@uru-intelligence/app 2.1.1 → 2.1.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.
package/README.md CHANGED
@@ -298,3 +298,29 @@ Building Gems is documented at
298
298
 
299
299
  Proprietary. See [LICENSE](./LICENSE). You may use and redistribute this package
300
300
  as part of an application that runs on the Uru platform.
301
+
302
+
303
+ ### Conditional Dataset saves
304
+
305
+ Native row queries return `revision`. Pass it as `expectedDatasetRevision` with
306
+ an `idempotencyKey` to reject changes made since that read:
307
+
308
+ ```ts
309
+ const page = await uru.datasets.queryRows('contacts', { limit: 1 });
310
+ if (page.revision === undefined) throw new Error('Native revision required');
311
+ await uru.datasets.upsertRows('contacts', [{ id: rowId, name: 'Updated' }],
312
+ undefined, { expectedDatasetRevision: page.revision, idempotencyKey: saveId });
313
+ ```
314
+
315
+ Keep the key for that save across retries. Hosted Gems scope it to the verified
316
+ Gem and viewer. A repeated request can report a conflict if preparing the row
317
+ would now produce different values. Read the saved record before retrying;
318
+ confirm the intended fields were saved or ask the user to resolve the conflict.
319
+ Never assign a new key merely to bypass a conflict. Browser conditional saves
320
+ require a built Gem runtime, including GitHub previews. External datasets do
321
+ not supply a native revision and remain read-only.
322
+
323
+ The fourth argument is the mutation options object. The optional third argument
324
+ remains the result decoder. Native query responses also include `runtimeContext`
325
+ with the server-verified `artifactId` and `viewerUserId` for local recovery state.
326
+ These returned identifiers do not grant access to other resources.
@@ -101,6 +101,21 @@ const decodedRows = (value, decodeRow, label) => {
101
101
  };
102
102
  const rowsResultFields = (body, rows, label) => {
103
103
  const output = { rows };
104
+ const revision = body.revision;
105
+ if (revision !== undefined) {
106
+ if (typeof revision !== "string" || !/^(0|[1-9][0-9]{0,19})$/.test(revision))
107
+ return invalidResponse(label, body, "revision");
108
+ output.revision = revision;
109
+ }
110
+ const runtimeContext = body.runtimeContext;
111
+ if (runtimeContext !== undefined) {
112
+ if (runtimeContext === null || typeof runtimeContext !== "object" || Array.isArray(runtimeContext) ||
113
+ typeof runtimeContext.artifactId !== "string" ||
114
+ (runtimeContext.viewerUserId !== null && typeof runtimeContext.viewerUserId !== "string")) {
115
+ return invalidResponse(label, body, "runtimeContext");
116
+ }
117
+ output.runtimeContext = { artifactId: runtimeContext.artifactId, viewerUserId: runtimeContext.viewerUserId };
118
+ }
104
119
  const total = optionalTotal(body, label);
105
120
  if (total !== undefined) {
106
121
  output.total = total;
package/dist/datasets.js CHANGED
@@ -102,8 +102,8 @@ const createQueryRowsInvoker = (app) => {
102
102
  return queryRows;
103
103
  };
104
104
  const createRowMutationInvoker = (options) => {
105
- async function mutate(datasetRef, rows, decodeResult) {
106
- const body = upsertBody(rows);
105
+ async function mutate(datasetRef, rows, decodeResult, mutationOptions) {
106
+ const body = { ...upsertBody(rows), ...mutationOptions };
107
107
  const path = datasetMutationPath(datasetRef);
108
108
  if (decodeResult === undefined) {
109
109
  return await postProviderDatasetWrite(options, path, body);
@@ -113,8 +113,8 @@ const createRowMutationInvoker = (options) => {
113
113
  return mutate;
114
114
  };
115
115
  const createIdMutationInvoker = (options, operation) => {
116
- async function mutate(datasetRef, rowIds, decodeResult) {
117
- const body = idMutationBody(operation, rowIds);
116
+ async function mutate(datasetRef, rowIds, decodeResult, mutationOptions) {
117
+ const body = { ...idMutationBody(operation, rowIds), ...mutationOptions };
118
118
  const path = datasetMutationPath(datasetRef);
119
119
  if (decodeResult === undefined) {
120
120
  return await postProviderDatasetWrite(options, path, body);
package/dist/types.d.ts CHANGED
@@ -151,6 +151,11 @@ export interface UruDatasetQuery {
151
151
  /** One page of Dataset rows, plus the total when `includeTotal` asked for it. */
152
152
  export interface UruDatasetRowsResult<TRow = UruDatasetRow> {
153
153
  rows: TRow[];
154
+ revision?: string;
155
+ runtimeContext?: {
156
+ artifactId: string;
157
+ viewerUserId: string | null;
158
+ };
154
159
  total?: number | null;
155
160
  limit?: number;
156
161
  offset?: number;
@@ -186,13 +191,18 @@ export interface UruDatasetQueryRows {
186
191
  <TRow>(datasetRef: string, query: UruDatasetQuery | undefined, decodeRow: UruResultDecoder<TRow>): Promise<UruDatasetRowsResult<TRow>>;
187
192
  (datasetRef: string, query?: UruDatasetQuery): Promise<UruDatasetRowsResult>;
188
193
  }
194
+ /** Public wire contract: kept local because this package has no private workspace dependencies. */
195
+ export interface UruDatasetMutationOptions {
196
+ expectedDatasetRevision?: string;
197
+ idempotencyKey?: string;
198
+ }
189
199
  export interface UruDatasetRowMutation {
190
- <TResult>(datasetRef: string, rows: readonly unknown[], decodeResult: UruResultDecoder<TResult>): Promise<UruDatasetMutationResult<TResult>>;
191
- (datasetRef: string, rows: readonly unknown[]): Promise<UruDatasetMutationResult>;
200
+ <TResult>(datasetRef: string, rows: readonly unknown[], decodeResult: UruResultDecoder<TResult>, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult<TResult>>;
201
+ (datasetRef: string, rows: readonly unknown[], decodeResult?: undefined, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult>;
192
202
  }
193
203
  export interface UruDatasetIdMutation {
194
- <TResult>(datasetRef: string, rowIds: readonly string[], decodeResult: UruResultDecoder<TResult>): Promise<UruDatasetMutationResult<TResult>>;
195
- (datasetRef: string, rowIds: readonly string[]): Promise<UruDatasetMutationResult>;
204
+ <TResult>(datasetRef: string, rowIds: readonly string[], decodeResult: UruResultDecoder<TResult>, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult<TResult>>;
205
+ (datasetRef: string, rowIds: readonly string[], decodeResult?: undefined, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult>;
196
206
  }
197
207
  export interface UruDatasetSqlWrite {
198
208
  <TResult>(sql: string, options: UruDatasetSqlWriteOptions, decodeResult: UruResultDecoder<TResult>): Promise<UruDatasetMutationResult<TResult>>;
@@ -295,6 +305,20 @@ export interface UruUploadsContext {
295
305
  upload(file: File, options?: {
296
306
  signal?: AbortSignal;
297
307
  }): Promise<UruUploadResult>;
308
+ /**
309
+ * Read one VERIFIED upload back, as a `Blob`.
310
+ *
311
+ * A Blob rather than a URL because the read is AUTHENTICATED: the platform
312
+ * serves the bytes under the same authority that minted the grant, and an
313
+ * `<img src>` or an `<a href>` carries none of the runtime's headers. Hand
314
+ * the Blob to `URL.createObjectURL` for a preview or a download link.
315
+ *
316
+ * Rejects when the upload is not the caller's, was never verified, or no
317
+ * longer exists.
318
+ */
319
+ read(uploadId: string, options?: {
320
+ signal?: AbortSignal;
321
+ }): Promise<Blob>;
298
322
  }
299
323
  export interface UruJobProgress {
300
324
  readonly completed: number;
package/dist/uploads.d.ts CHANGED
@@ -17,6 +17,14 @@
17
17
  *
18
18
  * `upload(file)` performs all three, which is what almost every caller wants.
19
19
  *
20
+ * ## And one hop back
21
+ *
22
+ * `read(uploadId)` returns the stored file as a `Blob`. The bytes come back
23
+ * THROUGH the platform, not from a storage link: the read is authorized per
24
+ * request under the same authority that minted the grant, so there is no
25
+ * shareable URL to leak. Keep the `uploadId` a verified upload gave you --
26
+ * `uru.storage` is the durable place for it -- and pass it here.
27
+ *
20
28
  * ## The refusals are values, not exceptions
21
29
  *
22
30
  * A grant that is refused (`size_mismatch`, `type_mismatch`, `unverifiable`) or
package/dist/uploads.js CHANGED
@@ -17,6 +17,14 @@
17
17
  *
18
18
  * `upload(file)` performs all three, which is what almost every caller wants.
19
19
  *
20
+ * ## And one hop back
21
+ *
22
+ * `read(uploadId)` returns the stored file as a `Blob`. The bytes come back
23
+ * THROUGH the platform, not from a storage link: the read is authorized per
24
+ * request under the same authority that minted the grant, so there is no
25
+ * shareable URL to leak. Keep the `uploadId` a verified upload gave you --
26
+ * `uru.storage` is the durable place for it -- and pass it here.
27
+ *
20
28
  * ## The refusals are values, not exceptions
21
29
  *
22
30
  * A grant that is refused (`size_mismatch`, `type_mismatch`, `unverifiable`) or
@@ -140,6 +148,48 @@ function resultFrom(value) {
140
148
  cleanupFailed: row['cleanupFailed'] === true,
141
149
  };
142
150
  }
151
+ /**
152
+ * Read the response body into a `Blob`, refusing anything above the platform's
153
+ * own per-file ceiling.
154
+ *
155
+ * Bounded for the same reason every other response in this SDK is: a runtime
156
+ * that answered with more bytes than a grant can ever hold would otherwise
157
+ * exhaust the Gem's memory through a call the Gem made on purpose.
158
+ */
159
+ async function readBlob(response) {
160
+ const declared = Number(response.headers.get('content-length') ?? '');
161
+ if (Number.isFinite(declared) && declared > URU_UPLOAD_MAX_BYTES) {
162
+ await response.body?.cancel();
163
+ invalid();
164
+ }
165
+ const reader = response.body?.getReader();
166
+ if (reader === undefined)
167
+ invalid();
168
+ const parts = [];
169
+ let size = 0;
170
+ try {
171
+ for (;;) {
172
+ const chunk = await reader.read();
173
+ if (chunk.done)
174
+ break;
175
+ size += chunk.value.byteLength;
176
+ if (size > URU_UPLOAD_MAX_BYTES) {
177
+ await reader.cancel();
178
+ invalid();
179
+ }
180
+ parts.push(chunk.value);
181
+ }
182
+ }
183
+ finally {
184
+ reader.releaseLock();
185
+ }
186
+ // The platform's stored media type, which is the one it verified the object
187
+ // against. A Blob that carried no type would render as a download of unknown
188
+ // kind wherever the Gem put it.
189
+ return new Blob(parts, {
190
+ type: response.headers.get('content-type') ?? 'application/octet-stream',
191
+ });
192
+ }
143
193
  export function createUploadsContext(options) {
144
194
  const base = `${trimTrailingSlash(options.baseUrl ?? '')}${runtimeBasePath(options)}/apps/${encodeURIComponent(options.deploymentId)}/uploads`;
145
195
  // Resolved per call rather than once here, so building the context still
@@ -190,9 +240,32 @@ export function createUploadsContext(options) {
190
240
  invalid();
191
241
  return resultFrom(await post(`/${uploadId}/complete`, undefined, settings?.signal));
192
242
  };
243
+ const read = async (uploadId, settings) => {
244
+ if (typeof uploadId !== 'string' || !UUID.test(uploadId))
245
+ invalid();
246
+ const response = await doFetch(`${base}/${uploadId}/content`, {
247
+ method: 'GET',
248
+ headers: new Headers(await resolveHeaders(options.headers)),
249
+ // Five minutes, not the thirty seconds the JSON hops use: a grant admits
250
+ // 104,857,600 bytes, and thirty seconds would need 3.5 MB/s to finish a
251
+ // file the platform was happy to store.
252
+ signal: settings?.signal === undefined
253
+ ? AbortSignal.timeout(300_000)
254
+ : AbortSignal.any([settings.signal, AbortSignal.timeout(300_000)]),
255
+ });
256
+ if (!response.ok) {
257
+ await response.body?.cancel();
258
+ throw new UruAppRuntimeError('Upload could not be read.', {
259
+ status: response.status,
260
+ body: null,
261
+ });
262
+ }
263
+ return readBlob(response);
264
+ };
193
265
  return {
194
266
  request,
195
267
  complete,
268
+ read,
196
269
  upload: async (file, settings) => {
197
270
  // The media type is the browser's, lower-cased by it already. A file the
198
271
  // OS could not type at all gets the generic one rather than an empty
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uru-intelligence/app",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "private": false,
5
5
  "description": "Runtime client for Uru Gems: capability calls, Datasets, jobs and secrets",
6
6
  "keywords": [