@nodetool-ai/runtime-core 0.8.19 → 0.8.21
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/dist/.nodetool-build-stamp +1 -1
- package/dist/context-host.d.ts +222 -0
- package/dist/context-host.d.ts.map +1 -0
- package/dist/context-host.js +1629 -0
- package/dist/context-host.js.map +1 -0
- package/dist/fake-host.d.ts +48 -0
- package/dist/fake-host.d.ts.map +1 -0
- package/dist/fake-host.js +1903 -0
- package/dist/fake-host.js.map +1 -0
- package/dist/host.d.ts +221 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +556 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/model-interfaces.d.ts +226 -3
- package/dist/model-interfaces.d.ts.map +1 -1
- package/dist/node-context.d.ts +6 -1
- package/dist/node-context.d.ts.map +1 -1
- package/dist/safe-url.d.ts +11 -6
- package/dist/safe-url.d.ts.map +1 -1
- package/dist/safe-url.js +85 -42
- package/dist/safe-url.js.map +1 -1
- package/package.json +14 -4
|
@@ -0,0 +1,1903 @@
|
|
|
1
|
+
import { isString } from "@nodetool-ai/protocol";
|
|
2
|
+
import { InMemoryStorageAdapter } from "@nodetool-ai/storage";
|
|
3
|
+
import { StorageWorkspace } from "./storage-workspace.js";
|
|
4
|
+
import { HostError, assetPut, assetQuery, assetUpdateMeta, assertHostNotCancelled, combineHostSignals, createHostChangeEmitter, emptyHostImageDocumentData, emptyHostScriptDocument, emptyHostStoryboardDocument, emptyHostTimelineDocument, entityCreate, entityPatch, entityQuery, imageDocumentCreate, imageDocumentPatch, imageDocumentQuery, isHostError, jobQuery, messageCreate, messageQuery, normalizeHostAssetRow, normalizeHostEntityRow, normalizeHostImageDocumentRow, normalizeHostJobRow, normalizeHostMessageRow, normalizeHostProjectRow, normalizeHostScriptRow, normalizeHostSettingRow, normalizeHostStoryboardRow, normalizeHostThreadRow, normalizeHostTimelineSequenceRow, normalizeHostWorkflowRow, notGrantedError, paginateHostRows, parseHostFetchAuth, parseHostFetchUrl, parseHostId, parseHostProgramName, parseHostSchema, projectCreate, projectPatch, projectQuery, runHostOp, scriptCreate, scriptPatch, scriptQuery, settingQuery, storyboardCreate, storyboardPatch, storyboardQuery, threadCreate, threadPatch, threadQuery, timelineClipsDurationMs, timelineSequenceCreate, timelineSequencePatch, timelineSequenceQuery, toPlainJson, unservedWatch, validateHostExecOptions, workflowCreate, workflowPatch, workflowQuery } from "./host.js";
|
|
5
|
+
const normEntityName = (value) => value.trim().toLowerCase();
|
|
6
|
+
/**
|
|
7
|
+
* In-memory {@link Host} implementation for unit and contract tests.
|
|
8
|
+
* Enforces the same schema validation, ownership scoping, cancellation, and
|
|
9
|
+
* optimistic-concurrency rules as the production backends.
|
|
10
|
+
*/
|
|
11
|
+
export function createFakeHost(options = {}) {
|
|
12
|
+
const backend = "fake";
|
|
13
|
+
const userId = options.userId ?? "fake-user";
|
|
14
|
+
const projectId = options.projectId ?? "default";
|
|
15
|
+
const workflowId = options.workflowId ?? null;
|
|
16
|
+
const jobId = options.jobId ?? null;
|
|
17
|
+
const signal = options.signal;
|
|
18
|
+
const deniedNamespaces = new Set(options.deniedNamespaces ?? []);
|
|
19
|
+
const deniedRoots = new Set(options.deniedRoots ?? []);
|
|
20
|
+
let seq = 0;
|
|
21
|
+
let lastTsMs = 0;
|
|
22
|
+
const nextId = () => {
|
|
23
|
+
seq += 1;
|
|
24
|
+
return seq.toString(16).padStart(32, "0");
|
|
25
|
+
};
|
|
26
|
+
const nextTimestamp = (after) => {
|
|
27
|
+
const afterMs = after ? Date.parse(after) : NaN;
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
const next = Math.max(now, Number.isFinite(afterMs) ? afterMs + 1 : now, lastTsMs + 1);
|
|
30
|
+
lastTsMs = next;
|
|
31
|
+
return new Date(next).toISOString();
|
|
32
|
+
};
|
|
33
|
+
const assetsMap = new Map();
|
|
34
|
+
const assetBytesMap = new Map();
|
|
35
|
+
for (const seed of options.initialAssets ?? []) {
|
|
36
|
+
const row = normalizeHostAssetRow(seed.row, backend);
|
|
37
|
+
assetsMap.set(row.id, { owner: row.user_id, row });
|
|
38
|
+
if (seed.bytes) {
|
|
39
|
+
assetBytesMap.set(row.id, new Uint8Array(seed.bytes));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const boardsMap = new Map();
|
|
43
|
+
for (const seed of options.initialStoryboards ?? []) {
|
|
44
|
+
const row = normalizeHostStoryboardRow(seed, backend);
|
|
45
|
+
boardsMap.set(row.id, { owner: userId, row });
|
|
46
|
+
}
|
|
47
|
+
const scriptsMap = new Map();
|
|
48
|
+
for (const seed of options.initialScripts ?? []) {
|
|
49
|
+
const row = normalizeHostScriptRow(seed, backend);
|
|
50
|
+
scriptsMap.set(row.id, { owner: userId, row });
|
|
51
|
+
}
|
|
52
|
+
const timelinesMap = new Map();
|
|
53
|
+
for (const seed of options.initialTimelineSequences ?? []) {
|
|
54
|
+
const row = normalizeHostTimelineSequenceRow(seed, backend);
|
|
55
|
+
timelinesMap.set(row.id, { owner: userId, row });
|
|
56
|
+
}
|
|
57
|
+
const imageDocsMap = new Map();
|
|
58
|
+
for (const seed of options.initialImageDocuments ?? []) {
|
|
59
|
+
const row = normalizeHostImageDocumentRow(seed, backend);
|
|
60
|
+
imageDocsMap.set(row.id, { owner: userId, row });
|
|
61
|
+
}
|
|
62
|
+
const entitiesMap = new Map();
|
|
63
|
+
for (const seed of options.initialEntities ?? []) {
|
|
64
|
+
const row = normalizeHostEntityRow(seed, backend);
|
|
65
|
+
entitiesMap.set(row.id, { owner: userId, row });
|
|
66
|
+
}
|
|
67
|
+
const workflowsMap = new Map();
|
|
68
|
+
for (const seed of options.initialWorkflows ?? []) {
|
|
69
|
+
const row = normalizeHostWorkflowRow(seed, backend);
|
|
70
|
+
workflowsMap.set(row.id, { owner: userId, row });
|
|
71
|
+
}
|
|
72
|
+
const jobsMap = new Map();
|
|
73
|
+
for (const seed of options.initialJobs ?? []) {
|
|
74
|
+
const row = normalizeHostJobRow(seed, backend);
|
|
75
|
+
jobsMap.set(row.id, { owner: row.user_id, row });
|
|
76
|
+
}
|
|
77
|
+
const threadsMap = new Map();
|
|
78
|
+
for (const seed of options.initialThreads ?? []) {
|
|
79
|
+
const row = normalizeHostThreadRow(seed, backend);
|
|
80
|
+
threadsMap.set(row.id, { owner: row.user_id, row });
|
|
81
|
+
}
|
|
82
|
+
const messagesMap = new Map();
|
|
83
|
+
for (const seed of options.initialMessages ?? []) {
|
|
84
|
+
const row = normalizeHostMessageRow(seed, backend);
|
|
85
|
+
messagesMap.set(row.id, { owner: row.user_id, row });
|
|
86
|
+
}
|
|
87
|
+
const projectsMap = new Map();
|
|
88
|
+
for (const seed of options.initialProjects ?? []) {
|
|
89
|
+
const row = normalizeHostProjectRow(seed, backend);
|
|
90
|
+
projectsMap.set(row.id, { owner: userId, row });
|
|
91
|
+
}
|
|
92
|
+
const tempMap = new Map();
|
|
93
|
+
const cacheMap = new Map();
|
|
94
|
+
const allowedPrograms = options.allowedPrograms
|
|
95
|
+
? new Set(options.allowedPrograms)
|
|
96
|
+
: null;
|
|
97
|
+
const assetsEmitter = createHostChangeEmitter();
|
|
98
|
+
const workflowsEmitter = createHostChangeEmitter();
|
|
99
|
+
const jobsEmitter = createHostChangeEmitter();
|
|
100
|
+
const threadsEmitter = createHostChangeEmitter();
|
|
101
|
+
const messagesEmitter = createHostChangeEmitter();
|
|
102
|
+
const storyboardsEmitter = createHostChangeEmitter();
|
|
103
|
+
const scriptsEmitter = createHostChangeEmitter();
|
|
104
|
+
const timelinesEmitter = createHostChangeEmitter();
|
|
105
|
+
const imageDocsEmitter = createHostChangeEmitter();
|
|
106
|
+
const entitiesEmitter = createHostChangeEmitter();
|
|
107
|
+
const projectsEmitter = createHostChangeEmitter();
|
|
108
|
+
const settingsEmitter = createHostChangeEmitter();
|
|
109
|
+
const normalizeTempKey = (keyOrUri) => keyOrUri.startsWith("memory://")
|
|
110
|
+
? keyOrUri.slice("memory://".length)
|
|
111
|
+
: keyOrUri;
|
|
112
|
+
const readSettingRow = (key) => {
|
|
113
|
+
const settingsObj = options.settings;
|
|
114
|
+
if (!settingsObj || !Object.hasOwn(settingsObj, key))
|
|
115
|
+
return null;
|
|
116
|
+
const raw = settingsObj[key];
|
|
117
|
+
if (raw === undefined)
|
|
118
|
+
return null;
|
|
119
|
+
const entry = isString(raw) ? { value: raw } : raw;
|
|
120
|
+
return normalizeHostSettingRow({
|
|
121
|
+
key,
|
|
122
|
+
value: entry.value,
|
|
123
|
+
description: entry.description ?? "",
|
|
124
|
+
updated_at: entry.updated_at ?? null
|
|
125
|
+
}, backend);
|
|
126
|
+
};
|
|
127
|
+
const listAllSettingRows = () => {
|
|
128
|
+
const settingsObj = options.settings;
|
|
129
|
+
if (!settingsObj)
|
|
130
|
+
return [];
|
|
131
|
+
const out = [];
|
|
132
|
+
for (const key of Object.keys(settingsObj)) {
|
|
133
|
+
const row = readSettingRow(key);
|
|
134
|
+
if (row)
|
|
135
|
+
out.push(row);
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
};
|
|
139
|
+
const secretsMap = new Map(Object.entries(options.secrets ?? {}));
|
|
140
|
+
const projectWs = options.projectWorkspace === undefined
|
|
141
|
+
? new StorageWorkspace(new InMemoryStorageAdapter())
|
|
142
|
+
: options.projectWorkspace;
|
|
143
|
+
const scratchWs = options.scratchWorkspace === undefined
|
|
144
|
+
? new StorageWorkspace(new InMemoryStorageAdapter())
|
|
145
|
+
: options.scratchWorkspace;
|
|
146
|
+
const requireNamespace = (ns) => {
|
|
147
|
+
if (deniedNamespaces.has(ns)) {
|
|
148
|
+
throw notGrantedError(ns, backend);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
backend,
|
|
153
|
+
io: {
|
|
154
|
+
files(root) {
|
|
155
|
+
assertHostNotCancelled(signal, backend);
|
|
156
|
+
requireNamespace("io");
|
|
157
|
+
if (deniedRoots.has(root)) {
|
|
158
|
+
throw notGrantedError(`io.files("${root}")`, backend);
|
|
159
|
+
}
|
|
160
|
+
const ws = root === "project" ? projectWs : scratchWs;
|
|
161
|
+
if (!ws) {
|
|
162
|
+
throw notGrantedError(`io.files("${root}")`, backend);
|
|
163
|
+
}
|
|
164
|
+
return ws;
|
|
165
|
+
},
|
|
166
|
+
async fetch(input, init) {
|
|
167
|
+
const callSignal = init?.signal;
|
|
168
|
+
assertHostNotCancelled(signal, backend);
|
|
169
|
+
assertHostNotCancelled(callSignal, backend);
|
|
170
|
+
requireNamespace("io");
|
|
171
|
+
if (!options.fetchHandler) {
|
|
172
|
+
throw notGrantedError("io.fetch", backend);
|
|
173
|
+
}
|
|
174
|
+
const url = parseHostFetchUrl(input, backend);
|
|
175
|
+
parseHostFetchAuth(init?.auth, backend);
|
|
176
|
+
const combinedSignal = combineHostSignals(signal, callSignal);
|
|
177
|
+
const nextInit = {
|
|
178
|
+
...init,
|
|
179
|
+
signal: combinedSignal
|
|
180
|
+
};
|
|
181
|
+
try {
|
|
182
|
+
const response = await options.fetchHandler(url, nextInit);
|
|
183
|
+
assertHostNotCancelled(signal, backend);
|
|
184
|
+
assertHostNotCancelled(callSignal, backend);
|
|
185
|
+
return response;
|
|
186
|
+
}
|
|
187
|
+
catch (err) {
|
|
188
|
+
if (isHostError(err))
|
|
189
|
+
throw err;
|
|
190
|
+
if (signal?.aborted ||
|
|
191
|
+
callSignal?.aborted ||
|
|
192
|
+
(err instanceof Error && err.name === "AbortError")) {
|
|
193
|
+
throw new HostError("cancelled", `Host fetch cancelled (${backend})`, backend);
|
|
194
|
+
}
|
|
195
|
+
throw err;
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
async exec(program, args, execOptions) {
|
|
199
|
+
const callSignal = execOptions?.signal;
|
|
200
|
+
assertHostNotCancelled(signal, backend);
|
|
201
|
+
assertHostNotCancelled(callSignal, backend);
|
|
202
|
+
requireNamespace("io");
|
|
203
|
+
const validProgram = parseHostProgramName(program, backend);
|
|
204
|
+
validateHostExecOptions(execOptions, backend);
|
|
205
|
+
if (!options.execHandler ||
|
|
206
|
+
(allowedPrograms !== null && !allowedPrograms.has(validProgram))) {
|
|
207
|
+
throw notGrantedError(`io.exec("${validProgram}")`, backend);
|
|
208
|
+
}
|
|
209
|
+
const combinedSignal = combineHostSignals(signal, callSignal);
|
|
210
|
+
const nextOptions = {
|
|
211
|
+
...execOptions,
|
|
212
|
+
signal: combinedSignal
|
|
213
|
+
};
|
|
214
|
+
try {
|
|
215
|
+
const result = await options.execHandler(validProgram, [...args], nextOptions);
|
|
216
|
+
assertHostNotCancelled(signal, backend);
|
|
217
|
+
assertHostNotCancelled(callSignal, backend);
|
|
218
|
+
const outputs = {};
|
|
219
|
+
for (const [relPath, bytes] of Object.entries(result.outputs)) {
|
|
220
|
+
outputs[relPath] = new Uint8Array(bytes);
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
exitCode: result.exitCode,
|
|
224
|
+
stdout: result.stdout,
|
|
225
|
+
stderr: result.stderr,
|
|
226
|
+
outputs
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
if (isHostError(err))
|
|
231
|
+
throw err;
|
|
232
|
+
if (signal?.aborted ||
|
|
233
|
+
callSignal?.aborted ||
|
|
234
|
+
(err instanceof Error && err.name === "AbortError")) {
|
|
235
|
+
throw new HostError("cancelled", `Host exec cancelled (${backend})`, backend);
|
|
236
|
+
}
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
db: {
|
|
242
|
+
assets: {
|
|
243
|
+
async get(id) {
|
|
244
|
+
return runHostOp(signal, backend, async () => {
|
|
245
|
+
requireNamespace("db");
|
|
246
|
+
const validId = parseHostId(id, backend);
|
|
247
|
+
const entry = assetsMap.get(validId);
|
|
248
|
+
if (!entry || entry.owner !== userId)
|
|
249
|
+
return null;
|
|
250
|
+
return normalizeHostAssetRow(entry.row, backend);
|
|
251
|
+
});
|
|
252
|
+
},
|
|
253
|
+
async list(query, cursor) {
|
|
254
|
+
return runHostOp(signal, backend, async () => {
|
|
255
|
+
requireNamespace("db");
|
|
256
|
+
const parsed = query !== undefined
|
|
257
|
+
? parseHostSchema(assetQuery, query, backend)
|
|
258
|
+
: {};
|
|
259
|
+
const filtered = [...assetsMap.values()]
|
|
260
|
+
.filter((entry) => entry.owner === userId)
|
|
261
|
+
.map((entry) => entry.row)
|
|
262
|
+
.filter((row) => {
|
|
263
|
+
if (parsed.parent_id !== undefined &&
|
|
264
|
+
row.parent_id !== parsed.parent_id) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
if (parsed.content_type !== undefined &&
|
|
268
|
+
!row.content_type.startsWith(parsed.content_type)) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
if (parsed.workflow_id !== undefined &&
|
|
272
|
+
row.workflow_id !== parsed.workflow_id) {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
if (parsed.job_id !== undefined &&
|
|
276
|
+
row.job_id !== parsed.job_id) {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
return true;
|
|
280
|
+
})
|
|
281
|
+
.sort((a, b) => {
|
|
282
|
+
const byTime = b.created_at.localeCompare(a.created_at);
|
|
283
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
284
|
+
})
|
|
285
|
+
.map((row) => normalizeHostAssetRow(row, backend));
|
|
286
|
+
const limit = parsed.page_size ?? 10000;
|
|
287
|
+
return paginateHostRows(filtered, (row) => row.id, limit, cursor, backend);
|
|
288
|
+
});
|
|
289
|
+
},
|
|
290
|
+
watch(query, watchOptions) {
|
|
291
|
+
if (deniedNamespaces.has("db")) {
|
|
292
|
+
return unservedWatch("db", backend);
|
|
293
|
+
}
|
|
294
|
+
const parsed = query !== undefined
|
|
295
|
+
? parseHostSchema(assetQuery, query, backend)
|
|
296
|
+
: {};
|
|
297
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
298
|
+
return assetsEmitter.watch((change) => {
|
|
299
|
+
if (!change.row)
|
|
300
|
+
return true;
|
|
301
|
+
if (parsed.parent_id !== undefined &&
|
|
302
|
+
change.row.parent_id !== parsed.parent_id) {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
if (parsed.content_type !== undefined &&
|
|
306
|
+
!change.row.content_type.startsWith(parsed.content_type)) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
if (parsed.workflow_id !== undefined &&
|
|
310
|
+
change.row.workflow_id !== parsed.workflow_id) {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
if (parsed.job_id !== undefined &&
|
|
314
|
+
change.row.job_id !== parsed.job_id) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
return true;
|
|
318
|
+
}, watchSignal);
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
workflows: {
|
|
322
|
+
async get(id) {
|
|
323
|
+
return runHostOp(signal, backend, async () => {
|
|
324
|
+
requireNamespace("db");
|
|
325
|
+
const validId = parseHostId(id, backend);
|
|
326
|
+
const entry = workflowsMap.get(validId);
|
|
327
|
+
if (!entry || entry.owner !== userId)
|
|
328
|
+
return null;
|
|
329
|
+
return normalizeHostWorkflowRow(entry.row, backend);
|
|
330
|
+
});
|
|
331
|
+
},
|
|
332
|
+
async list(query, cursor) {
|
|
333
|
+
return runHostOp(signal, backend, async () => {
|
|
334
|
+
requireNamespace("db");
|
|
335
|
+
const parsed = query !== undefined
|
|
336
|
+
? parseHostSchema(workflowQuery, query, backend)
|
|
337
|
+
: {};
|
|
338
|
+
const rows = [...workflowsMap.values()]
|
|
339
|
+
.filter((entry) => entry.owner === userId)
|
|
340
|
+
.map((entry) => entry.row)
|
|
341
|
+
.filter((row) => {
|
|
342
|
+
if (parsed.run_mode !== undefined &&
|
|
343
|
+
row.run_mode !== parsed.run_mode) {
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
if (parsed.tag !== undefined &&
|
|
347
|
+
!(row.tags ?? []).includes(parsed.tag)) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
return true;
|
|
351
|
+
})
|
|
352
|
+
.sort((a, b) => {
|
|
353
|
+
const byTime = (b.updated_at ?? "").localeCompare(a.updated_at ?? "");
|
|
354
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
355
|
+
})
|
|
356
|
+
.map((row) => normalizeHostWorkflowRow(row, backend));
|
|
357
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
358
|
+
});
|
|
359
|
+
},
|
|
360
|
+
async create(input) {
|
|
361
|
+
return runHostOp(signal, backend, async () => {
|
|
362
|
+
requireNamespace("db");
|
|
363
|
+
const parsed = parseHostSchema(workflowCreate, input, backend);
|
|
364
|
+
const id = parsed.id ?? nextId();
|
|
365
|
+
const existing = workflowsMap.get(id);
|
|
366
|
+
if (existing) {
|
|
367
|
+
if (existing.owner !== userId) {
|
|
368
|
+
throw new HostError("not_found", `Workflow not found: ${id}`, backend);
|
|
369
|
+
}
|
|
370
|
+
return normalizeHostWorkflowRow(existing.row, backend);
|
|
371
|
+
}
|
|
372
|
+
const now = nextTimestamp();
|
|
373
|
+
const row = normalizeHostWorkflowRow({
|
|
374
|
+
id,
|
|
375
|
+
project_id: projectId,
|
|
376
|
+
access: parsed.access,
|
|
377
|
+
created_at: now,
|
|
378
|
+
updated_at: now,
|
|
379
|
+
name: parsed.name,
|
|
380
|
+
tool_name: parsed.tool_name ?? null,
|
|
381
|
+
description: parsed.description ?? null,
|
|
382
|
+
tags: parsed.tags ?? [],
|
|
383
|
+
thumbnail: parsed.thumbnail ?? null,
|
|
384
|
+
thumbnail_url: parsed.thumbnail_url ?? null,
|
|
385
|
+
graph: parsed.graph ?? { nodes: [], edges: [] },
|
|
386
|
+
input_schema: null,
|
|
387
|
+
output_schema: null,
|
|
388
|
+
settings: parsed.settings ?? null,
|
|
389
|
+
package_name: parsed.package_name ?? null,
|
|
390
|
+
path: parsed.path ?? null,
|
|
391
|
+
run_mode: parsed.run_mode ?? "workflow",
|
|
392
|
+
workspace_id: parsed.workspace_id ?? null,
|
|
393
|
+
required_providers: null,
|
|
394
|
+
required_models: null,
|
|
395
|
+
html_app: parsed.html_app ?? null,
|
|
396
|
+
app_doc: parsed.app_doc ?? null,
|
|
397
|
+
etag: null,
|
|
398
|
+
schema_version: 1
|
|
399
|
+
}, backend);
|
|
400
|
+
workflowsMap.set(id, { owner: userId, row });
|
|
401
|
+
workflowsEmitter.emit({
|
|
402
|
+
kind: "created",
|
|
403
|
+
id: row.id,
|
|
404
|
+
row,
|
|
405
|
+
updatedAt: row.updated_at ?? undefined
|
|
406
|
+
});
|
|
407
|
+
return normalizeHostWorkflowRow(row, backend);
|
|
408
|
+
});
|
|
409
|
+
},
|
|
410
|
+
async update(id, patch) {
|
|
411
|
+
return runHostOp(signal, backend, async () => {
|
|
412
|
+
requireNamespace("db");
|
|
413
|
+
const validId = parseHostId(id, backend);
|
|
414
|
+
const parsed = parseHostSchema(workflowPatch, patch, backend);
|
|
415
|
+
const entry = workflowsMap.get(validId);
|
|
416
|
+
if (!entry || entry.owner !== userId) {
|
|
417
|
+
throw new HostError("not_found", `Workflow not found: ${validId}`, backend);
|
|
418
|
+
}
|
|
419
|
+
if (parsed.baseUpdatedAt !== undefined &&
|
|
420
|
+
entry.row.updated_at !== parsed.baseUpdatedAt) {
|
|
421
|
+
throw new HostError("conflict", `Workflow ${validId} was modified concurrently`, backend);
|
|
422
|
+
}
|
|
423
|
+
const nextRow = {
|
|
424
|
+
...entry.row,
|
|
425
|
+
name: parsed.name ?? entry.row.name,
|
|
426
|
+
tool_name: parsed.tool_name !== undefined
|
|
427
|
+
? parsed.tool_name
|
|
428
|
+
: entry.row.tool_name,
|
|
429
|
+
package_name: parsed.package_name !== undefined
|
|
430
|
+
? parsed.package_name
|
|
431
|
+
: entry.row.package_name,
|
|
432
|
+
path: parsed.path !== undefined ? parsed.path : entry.row.path,
|
|
433
|
+
tags: parsed.tags !== undefined ? parsed.tags : entry.row.tags,
|
|
434
|
+
description: parsed.description !== undefined
|
|
435
|
+
? parsed.description
|
|
436
|
+
: entry.row.description,
|
|
437
|
+
thumbnail: parsed.thumbnail !== undefined
|
|
438
|
+
? parsed.thumbnail
|
|
439
|
+
: entry.row.thumbnail,
|
|
440
|
+
thumbnail_url: parsed.thumbnail_url !== undefined
|
|
441
|
+
? parsed.thumbnail_url
|
|
442
|
+
: entry.row.thumbnail_url,
|
|
443
|
+
access: parsed.access ?? entry.row.access,
|
|
444
|
+
graph: parsed.graph !== undefined ? parsed.graph : entry.row.graph,
|
|
445
|
+
settings: parsed.settings !== undefined
|
|
446
|
+
? parsed.settings
|
|
447
|
+
: entry.row.settings,
|
|
448
|
+
run_mode: parsed.run_mode !== undefined
|
|
449
|
+
? parsed.run_mode
|
|
450
|
+
: entry.row.run_mode,
|
|
451
|
+
workspace_id: parsed.workspace_id !== undefined
|
|
452
|
+
? parsed.workspace_id
|
|
453
|
+
: entry.row.workspace_id,
|
|
454
|
+
html_app: parsed.html_app !== undefined
|
|
455
|
+
? parsed.html_app
|
|
456
|
+
: entry.row.html_app,
|
|
457
|
+
app_doc: parsed.app_doc !== undefined
|
|
458
|
+
? parsed.app_doc
|
|
459
|
+
: entry.row.app_doc,
|
|
460
|
+
updated_at: nextTimestamp(entry.row.updated_at ?? undefined)
|
|
461
|
+
};
|
|
462
|
+
const normalized = normalizeHostWorkflowRow(nextRow, backend);
|
|
463
|
+
workflowsMap.set(validId, { owner: userId, row: normalized });
|
|
464
|
+
workflowsEmitter.emit({
|
|
465
|
+
kind: "updated",
|
|
466
|
+
id: normalized.id,
|
|
467
|
+
row: normalized,
|
|
468
|
+
updatedAt: normalized.updated_at ?? undefined
|
|
469
|
+
});
|
|
470
|
+
return normalizeHostWorkflowRow(normalized, backend);
|
|
471
|
+
});
|
|
472
|
+
},
|
|
473
|
+
async delete(id) {
|
|
474
|
+
return runHostOp(signal, backend, async () => {
|
|
475
|
+
requireNamespace("db");
|
|
476
|
+
const validId = parseHostId(id, backend);
|
|
477
|
+
const entry = workflowsMap.get(validId);
|
|
478
|
+
if (!entry || entry.owner !== userId) {
|
|
479
|
+
throw new HostError("not_found", `Workflow not found: ${validId}`, backend);
|
|
480
|
+
}
|
|
481
|
+
workflowsMap.delete(validId);
|
|
482
|
+
workflowsEmitter.emit({ kind: "deleted", id: validId });
|
|
483
|
+
});
|
|
484
|
+
},
|
|
485
|
+
watch(query, watchOptions) {
|
|
486
|
+
if (deniedNamespaces.has("db")) {
|
|
487
|
+
return unservedWatch("db", backend);
|
|
488
|
+
}
|
|
489
|
+
const parsed = query !== undefined
|
|
490
|
+
? parseHostSchema(workflowQuery, query, backend)
|
|
491
|
+
: {};
|
|
492
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
493
|
+
return workflowsEmitter.watch((change) => {
|
|
494
|
+
if (!change.row)
|
|
495
|
+
return true;
|
|
496
|
+
if (parsed.run_mode !== undefined &&
|
|
497
|
+
change.row.run_mode !== parsed.run_mode) {
|
|
498
|
+
return false;
|
|
499
|
+
}
|
|
500
|
+
if (parsed.tag !== undefined &&
|
|
501
|
+
!(change.row.tags ?? []).includes(parsed.tag)) {
|
|
502
|
+
return false;
|
|
503
|
+
}
|
|
504
|
+
return true;
|
|
505
|
+
}, watchSignal);
|
|
506
|
+
}
|
|
507
|
+
},
|
|
508
|
+
jobs: {
|
|
509
|
+
async get(id) {
|
|
510
|
+
return runHostOp(signal, backend, async () => {
|
|
511
|
+
requireNamespace("db");
|
|
512
|
+
const validId = parseHostId(id, backend);
|
|
513
|
+
const entry = jobsMap.get(validId);
|
|
514
|
+
if (!entry || entry.owner !== userId)
|
|
515
|
+
return null;
|
|
516
|
+
return normalizeHostJobRow(entry.row, backend);
|
|
517
|
+
});
|
|
518
|
+
},
|
|
519
|
+
async list(query, cursor) {
|
|
520
|
+
return runHostOp(signal, backend, async () => {
|
|
521
|
+
requireNamespace("db");
|
|
522
|
+
const parsed = query !== undefined
|
|
523
|
+
? parseHostSchema(jobQuery, query, backend)
|
|
524
|
+
: {};
|
|
525
|
+
const rows = [...jobsMap.values()]
|
|
526
|
+
.filter((entry) => entry.owner === userId)
|
|
527
|
+
.map((entry) => entry.row)
|
|
528
|
+
.filter((row) => {
|
|
529
|
+
if (parsed.workflow_id !== undefined &&
|
|
530
|
+
row.workflow_id !== parsed.workflow_id) {
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
if (parsed.status !== undefined &&
|
|
534
|
+
row.status !== parsed.status) {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
return true;
|
|
538
|
+
})
|
|
539
|
+
.sort((a, b) => {
|
|
540
|
+
const byTime = (b.started_at ?? "").localeCompare(a.started_at ?? "");
|
|
541
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
542
|
+
})
|
|
543
|
+
.map((row) => normalizeHostJobRow(parsed.include_outputs ? row : { ...row, outputs: null }, backend));
|
|
544
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
545
|
+
});
|
|
546
|
+
},
|
|
547
|
+
watch(query, watchOptions) {
|
|
548
|
+
if (deniedNamespaces.has("db")) {
|
|
549
|
+
return unservedWatch("db", backend);
|
|
550
|
+
}
|
|
551
|
+
const parsed = query !== undefined
|
|
552
|
+
? parseHostSchema(jobQuery, query, backend)
|
|
553
|
+
: {};
|
|
554
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
555
|
+
return jobsEmitter.watch((change) => {
|
|
556
|
+
if (!change.row)
|
|
557
|
+
return true;
|
|
558
|
+
if (parsed.workflow_id !== undefined &&
|
|
559
|
+
change.row.workflow_id !== parsed.workflow_id) {
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
if (parsed.status !== undefined &&
|
|
563
|
+
change.row.status !== parsed.status) {
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
return true;
|
|
567
|
+
}, watchSignal);
|
|
568
|
+
}
|
|
569
|
+
},
|
|
570
|
+
threads: {
|
|
571
|
+
async get(id) {
|
|
572
|
+
return runHostOp(signal, backend, async () => {
|
|
573
|
+
requireNamespace("db");
|
|
574
|
+
const validId = parseHostId(id, backend);
|
|
575
|
+
const entry = threadsMap.get(validId);
|
|
576
|
+
if (!entry || entry.owner !== userId)
|
|
577
|
+
return null;
|
|
578
|
+
return normalizeHostThreadRow(entry.row, backend);
|
|
579
|
+
});
|
|
580
|
+
},
|
|
581
|
+
async list(query, cursor) {
|
|
582
|
+
return runHostOp(signal, backend, async () => {
|
|
583
|
+
requireNamespace("db");
|
|
584
|
+
const parsed = query !== undefined
|
|
585
|
+
? parseHostSchema(threadQuery, query, backend)
|
|
586
|
+
: {};
|
|
587
|
+
const reverse = parsed.reverse ?? true;
|
|
588
|
+
const rows = [...threadsMap.values()]
|
|
589
|
+
.filter((entry) => entry.owner === userId)
|
|
590
|
+
.map((entry) => entry.row)
|
|
591
|
+
.filter((row) => parsed.workflow_id === undefined ||
|
|
592
|
+
row.workflow_id === parsed.workflow_id)
|
|
593
|
+
.sort((a, b) => {
|
|
594
|
+
const byTime = reverse
|
|
595
|
+
? b.updated_at.localeCompare(a.updated_at)
|
|
596
|
+
: a.updated_at.localeCompare(b.updated_at);
|
|
597
|
+
return byTime !== 0
|
|
598
|
+
? byTime
|
|
599
|
+
: reverse
|
|
600
|
+
? b.id.localeCompare(a.id)
|
|
601
|
+
: a.id.localeCompare(b.id);
|
|
602
|
+
})
|
|
603
|
+
.map((row) => normalizeHostThreadRow(row, backend));
|
|
604
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
605
|
+
});
|
|
606
|
+
},
|
|
607
|
+
async create(input) {
|
|
608
|
+
return runHostOp(signal, backend, async () => {
|
|
609
|
+
requireNamespace("db");
|
|
610
|
+
const parsed = parseHostSchema(threadCreate, input, backend);
|
|
611
|
+
const id = parsed.id ?? nextId();
|
|
612
|
+
const existing = threadsMap.get(id);
|
|
613
|
+
if (existing) {
|
|
614
|
+
if (existing.owner !== userId) {
|
|
615
|
+
throw new HostError("not_found", `Thread not found: ${id}`, backend);
|
|
616
|
+
}
|
|
617
|
+
return normalizeHostThreadRow(existing.row, backend);
|
|
618
|
+
}
|
|
619
|
+
const now = nextTimestamp();
|
|
620
|
+
const row = normalizeHostThreadRow({
|
|
621
|
+
id,
|
|
622
|
+
user_id: userId,
|
|
623
|
+
project_id: projectId,
|
|
624
|
+
workflow_id: parsed.workflow_id ?? null,
|
|
625
|
+
title: parsed.title ?? "New Thread",
|
|
626
|
+
kind: parsed.kind,
|
|
627
|
+
created_at: now,
|
|
628
|
+
updated_at: now,
|
|
629
|
+
etag: null
|
|
630
|
+
}, backend);
|
|
631
|
+
threadsMap.set(id, { owner: userId, row });
|
|
632
|
+
threadsEmitter.emit({
|
|
633
|
+
kind: "created",
|
|
634
|
+
id: row.id,
|
|
635
|
+
row,
|
|
636
|
+
updatedAt: row.updated_at
|
|
637
|
+
});
|
|
638
|
+
return normalizeHostThreadRow(row, backend);
|
|
639
|
+
});
|
|
640
|
+
},
|
|
641
|
+
async update(id, patch) {
|
|
642
|
+
return runHostOp(signal, backend, async () => {
|
|
643
|
+
requireNamespace("db");
|
|
644
|
+
const validId = parseHostId(id, backend);
|
|
645
|
+
const parsed = parseHostSchema(threadPatch, patch, backend);
|
|
646
|
+
const entry = threadsMap.get(validId);
|
|
647
|
+
if (!entry || entry.owner !== userId) {
|
|
648
|
+
throw new HostError("not_found", `Thread not found: ${validId}`, backend);
|
|
649
|
+
}
|
|
650
|
+
const nextRow = normalizeHostThreadRow({
|
|
651
|
+
...entry.row,
|
|
652
|
+
title: parsed.title ?? entry.row.title,
|
|
653
|
+
updated_at: nextTimestamp(entry.row.updated_at)
|
|
654
|
+
}, backend);
|
|
655
|
+
threadsMap.set(validId, { owner: userId, row: nextRow });
|
|
656
|
+
threadsEmitter.emit({
|
|
657
|
+
kind: "updated",
|
|
658
|
+
id: nextRow.id,
|
|
659
|
+
row: nextRow,
|
|
660
|
+
updatedAt: nextRow.updated_at
|
|
661
|
+
});
|
|
662
|
+
return normalizeHostThreadRow(nextRow, backend);
|
|
663
|
+
});
|
|
664
|
+
},
|
|
665
|
+
async delete(id) {
|
|
666
|
+
return runHostOp(signal, backend, async () => {
|
|
667
|
+
requireNamespace("db");
|
|
668
|
+
const validId = parseHostId(id, backend);
|
|
669
|
+
const entry = threadsMap.get(validId);
|
|
670
|
+
if (!entry || entry.owner !== userId) {
|
|
671
|
+
throw new HostError("not_found", `Thread not found: ${validId}`, backend);
|
|
672
|
+
}
|
|
673
|
+
threadsMap.delete(validId);
|
|
674
|
+
for (const [msgId, msgEntry] of messagesMap.entries()) {
|
|
675
|
+
if (msgEntry.row.thread_id === validId) {
|
|
676
|
+
messagesMap.delete(msgId);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
threadsEmitter.emit({ kind: "deleted", id: validId });
|
|
680
|
+
});
|
|
681
|
+
},
|
|
682
|
+
watch(query, watchOptions) {
|
|
683
|
+
if (deniedNamespaces.has("db")) {
|
|
684
|
+
return unservedWatch("db", backend);
|
|
685
|
+
}
|
|
686
|
+
const parsed = query !== undefined
|
|
687
|
+
? parseHostSchema(threadQuery, query, backend)
|
|
688
|
+
: {};
|
|
689
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
690
|
+
return threadsEmitter.watch((change) => {
|
|
691
|
+
if (!change.row)
|
|
692
|
+
return true;
|
|
693
|
+
if (parsed.workflow_id !== undefined &&
|
|
694
|
+
change.row.workflow_id !== parsed.workflow_id) {
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
return true;
|
|
698
|
+
}, watchSignal);
|
|
699
|
+
}
|
|
700
|
+
},
|
|
701
|
+
messages: {
|
|
702
|
+
async get(id) {
|
|
703
|
+
return runHostOp(signal, backend, async () => {
|
|
704
|
+
requireNamespace("db");
|
|
705
|
+
const validId = parseHostId(id, backend);
|
|
706
|
+
const entry = messagesMap.get(validId);
|
|
707
|
+
if (!entry || entry.owner !== userId)
|
|
708
|
+
return null;
|
|
709
|
+
return normalizeHostMessageRow(entry.row, backend);
|
|
710
|
+
});
|
|
711
|
+
},
|
|
712
|
+
async list(query, cursor) {
|
|
713
|
+
return runHostOp(signal, backend, async () => {
|
|
714
|
+
requireNamespace("db");
|
|
715
|
+
if (!query) {
|
|
716
|
+
throw new HostError("invalid", "thread_id is required to list messages", backend);
|
|
717
|
+
}
|
|
718
|
+
const parsed = parseHostSchema(messageQuery, query, backend);
|
|
719
|
+
const thread = threadsMap.get(parsed.thread_id);
|
|
720
|
+
if (!thread || thread.owner !== userId) {
|
|
721
|
+
return { rows: [], next: null };
|
|
722
|
+
}
|
|
723
|
+
const reverse = parsed.reverse ?? false;
|
|
724
|
+
const rows = [...messagesMap.values()]
|
|
725
|
+
.filter((entry) => entry.owner === userId &&
|
|
726
|
+
entry.row.thread_id === parsed.thread_id)
|
|
727
|
+
.map((entry) => entry.row)
|
|
728
|
+
.sort((a, b) => {
|
|
729
|
+
const byTime = reverse
|
|
730
|
+
? b.created_at.localeCompare(a.created_at)
|
|
731
|
+
: a.created_at.localeCompare(b.created_at);
|
|
732
|
+
return byTime !== 0
|
|
733
|
+
? byTime
|
|
734
|
+
: reverse
|
|
735
|
+
? b.id.localeCompare(a.id)
|
|
736
|
+
: a.id.localeCompare(b.id);
|
|
737
|
+
})
|
|
738
|
+
.map((row) => normalizeHostMessageRow(row, backend));
|
|
739
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 100, cursor, backend);
|
|
740
|
+
});
|
|
741
|
+
},
|
|
742
|
+
async create(input) {
|
|
743
|
+
return runHostOp(signal, backend, async () => {
|
|
744
|
+
requireNamespace("db");
|
|
745
|
+
const parsed = parseHostSchema(messageCreate, input, backend);
|
|
746
|
+
const thread = threadsMap.get(parsed.thread_id);
|
|
747
|
+
if (!thread || thread.owner !== userId) {
|
|
748
|
+
throw new HostError("not_found", `Thread not found: ${parsed.thread_id}`, backend);
|
|
749
|
+
}
|
|
750
|
+
const id = parsed.id ?? nextId();
|
|
751
|
+
const existing = messagesMap.get(id);
|
|
752
|
+
if (existing) {
|
|
753
|
+
if (existing.owner !== userId) {
|
|
754
|
+
throw new HostError("not_found", `Message not found: ${id}`, backend);
|
|
755
|
+
}
|
|
756
|
+
return normalizeHostMessageRow(existing.row, backend);
|
|
757
|
+
}
|
|
758
|
+
const now = nextTimestamp();
|
|
759
|
+
const row = normalizeHostMessageRow({
|
|
760
|
+
type: "message",
|
|
761
|
+
id,
|
|
762
|
+
user_id: userId,
|
|
763
|
+
thread_id: parsed.thread_id,
|
|
764
|
+
role: parsed.role,
|
|
765
|
+
name: parsed.name ?? null,
|
|
766
|
+
content: parsed.content ?? null,
|
|
767
|
+
tool_calls: parsed.tool_calls ?? null,
|
|
768
|
+
tool_call_id: parsed.tool_call_id ?? null,
|
|
769
|
+
provider: null,
|
|
770
|
+
model: null,
|
|
771
|
+
cost: null,
|
|
772
|
+
workflow_id: parsed.workflow_id ?? null,
|
|
773
|
+
agent_execution_id: null,
|
|
774
|
+
execution_event_type: null,
|
|
775
|
+
workflow_target: null,
|
|
776
|
+
media_generation: null,
|
|
777
|
+
created_at: now,
|
|
778
|
+
updated_at: now
|
|
779
|
+
}, backend);
|
|
780
|
+
messagesMap.set(id, { owner: userId, row });
|
|
781
|
+
messagesEmitter.emit({
|
|
782
|
+
kind: "created",
|
|
783
|
+
id: row.id,
|
|
784
|
+
row,
|
|
785
|
+
updatedAt: row.updated_at
|
|
786
|
+
});
|
|
787
|
+
return normalizeHostMessageRow(row, backend);
|
|
788
|
+
});
|
|
789
|
+
},
|
|
790
|
+
watch(query, watchOptions) {
|
|
791
|
+
if (deniedNamespaces.has("db")) {
|
|
792
|
+
return unservedWatch("db", backend);
|
|
793
|
+
}
|
|
794
|
+
const parsed = query !== undefined
|
|
795
|
+
? parseHostSchema(messageQuery, query, backend)
|
|
796
|
+
: undefined;
|
|
797
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
798
|
+
return messagesEmitter.watch((change) => {
|
|
799
|
+
if (!change.row || !parsed)
|
|
800
|
+
return true;
|
|
801
|
+
return change.row.thread_id === parsed.thread_id;
|
|
802
|
+
}, watchSignal);
|
|
803
|
+
}
|
|
804
|
+
},
|
|
805
|
+
storyboards: {
|
|
806
|
+
async get(id) {
|
|
807
|
+
return runHostOp(signal, backend, async () => {
|
|
808
|
+
requireNamespace("db");
|
|
809
|
+
const validId = parseHostId(id, backend);
|
|
810
|
+
const entry = boardsMap.get(validId);
|
|
811
|
+
if (!entry || entry.owner !== userId)
|
|
812
|
+
return null;
|
|
813
|
+
return normalizeHostStoryboardRow(entry.row, backend);
|
|
814
|
+
});
|
|
815
|
+
},
|
|
816
|
+
async list(query, cursor) {
|
|
817
|
+
return runHostOp(signal, backend, async () => {
|
|
818
|
+
requireNamespace("db");
|
|
819
|
+
const parsed = query !== undefined
|
|
820
|
+
? parseHostSchema(storyboardQuery, query, backend)
|
|
821
|
+
: {};
|
|
822
|
+
let rows = [...boardsMap.values()]
|
|
823
|
+
.filter((entry) => entry.owner === userId)
|
|
824
|
+
.map((entry) => entry.row)
|
|
825
|
+
.sort((a, b) => {
|
|
826
|
+
const byTime = b.updatedAt.localeCompare(a.updatedAt);
|
|
827
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
828
|
+
});
|
|
829
|
+
if (parsed.recastOf) {
|
|
830
|
+
const { templateId, recastKey } = parsed.recastOf;
|
|
831
|
+
const match = rows.find((row) => row.document.templateId === templateId &&
|
|
832
|
+
row.document.recastKey === recastKey);
|
|
833
|
+
rows = match ? [match] : [];
|
|
834
|
+
}
|
|
835
|
+
const normalized = rows.map((row) => normalizeHostStoryboardRow(row, backend));
|
|
836
|
+
const limit = parsed.limit ?? 50;
|
|
837
|
+
return paginateHostRows(normalized, (row) => row.id, limit, cursor, backend);
|
|
838
|
+
});
|
|
839
|
+
},
|
|
840
|
+
async create(input) {
|
|
841
|
+
return runHostOp(signal, backend, async () => {
|
|
842
|
+
requireNamespace("db");
|
|
843
|
+
const parsed = parseHostSchema(storyboardCreate, input, backend);
|
|
844
|
+
const id = parsed.id ?? nextId();
|
|
845
|
+
const existing = boardsMap.get(id);
|
|
846
|
+
if (existing) {
|
|
847
|
+
if (existing.owner !== userId) {
|
|
848
|
+
throw new HostError("not_found", `Storyboard not found: ${id}`, backend);
|
|
849
|
+
}
|
|
850
|
+
return normalizeHostStoryboardRow(existing.row, backend);
|
|
851
|
+
}
|
|
852
|
+
const now = nextTimestamp();
|
|
853
|
+
const row = normalizeHostStoryboardRow({
|
|
854
|
+
id,
|
|
855
|
+
projectId,
|
|
856
|
+
name: parsed.name,
|
|
857
|
+
document: parsed.document ?? emptyHostStoryboardDocument(),
|
|
858
|
+
createdAt: now,
|
|
859
|
+
updatedAt: now
|
|
860
|
+
}, backend);
|
|
861
|
+
boardsMap.set(id, { owner: userId, row });
|
|
862
|
+
storyboardsEmitter.emit({
|
|
863
|
+
kind: "created",
|
|
864
|
+
id: row.id,
|
|
865
|
+
row,
|
|
866
|
+
updatedAt: row.updatedAt
|
|
867
|
+
});
|
|
868
|
+
return normalizeHostStoryboardRow(row, backend);
|
|
869
|
+
});
|
|
870
|
+
},
|
|
871
|
+
async update(id, patch) {
|
|
872
|
+
return runHostOp(signal, backend, async () => {
|
|
873
|
+
requireNamespace("db");
|
|
874
|
+
const validId = parseHostId(id, backend);
|
|
875
|
+
const parsed = parseHostSchema(storyboardPatch, patch, backend);
|
|
876
|
+
const entry = boardsMap.get(validId);
|
|
877
|
+
if (!entry || entry.owner !== userId) {
|
|
878
|
+
throw new HostError("not_found", `Storyboard not found: ${validId}`, backend);
|
|
879
|
+
}
|
|
880
|
+
if (parsed.baseUpdatedAt !== undefined &&
|
|
881
|
+
entry.row.updatedAt !== parsed.baseUpdatedAt) {
|
|
882
|
+
throw new HostError("conflict", `Storyboard ${validId} was modified concurrently`, backend);
|
|
883
|
+
}
|
|
884
|
+
const nextRow = {
|
|
885
|
+
id: entry.row.id,
|
|
886
|
+
projectId: entry.row.projectId,
|
|
887
|
+
name: parsed.name ?? entry.row.name,
|
|
888
|
+
document: parsed.document ?? entry.row.document,
|
|
889
|
+
createdAt: entry.row.createdAt,
|
|
890
|
+
updatedAt: nextTimestamp(entry.row.updatedAt)
|
|
891
|
+
};
|
|
892
|
+
const timelineId = parsed.timelineId !== undefined
|
|
893
|
+
? parsed.timelineId ?? undefined
|
|
894
|
+
: entry.row.timelineId;
|
|
895
|
+
if (timelineId !== undefined) {
|
|
896
|
+
nextRow.timelineId = timelineId;
|
|
897
|
+
}
|
|
898
|
+
const normalized = normalizeHostStoryboardRow(nextRow, backend);
|
|
899
|
+
boardsMap.set(validId, { owner: userId, row: normalized });
|
|
900
|
+
storyboardsEmitter.emit({
|
|
901
|
+
kind: "updated",
|
|
902
|
+
id: normalized.id,
|
|
903
|
+
row: normalized,
|
|
904
|
+
updatedAt: normalized.updatedAt
|
|
905
|
+
});
|
|
906
|
+
return normalizeHostStoryboardRow(normalized, backend);
|
|
907
|
+
});
|
|
908
|
+
},
|
|
909
|
+
async delete(id) {
|
|
910
|
+
return runHostOp(signal, backend, async () => {
|
|
911
|
+
requireNamespace("db");
|
|
912
|
+
const validId = parseHostId(id, backend);
|
|
913
|
+
const entry = boardsMap.get(validId);
|
|
914
|
+
if (!entry || entry.owner !== userId) {
|
|
915
|
+
throw new HostError("not_found", `Storyboard not found: ${validId}`, backend);
|
|
916
|
+
}
|
|
917
|
+
boardsMap.delete(validId);
|
|
918
|
+
storyboardsEmitter.emit({ kind: "deleted", id: validId });
|
|
919
|
+
});
|
|
920
|
+
},
|
|
921
|
+
watch(query, watchOptions) {
|
|
922
|
+
if (deniedNamespaces.has("db")) {
|
|
923
|
+
return unservedWatch("db", backend);
|
|
924
|
+
}
|
|
925
|
+
const parsed = query !== undefined
|
|
926
|
+
? parseHostSchema(storyboardQuery, query, backend)
|
|
927
|
+
: {};
|
|
928
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
929
|
+
return storyboardsEmitter.watch((change) => {
|
|
930
|
+
if (!change.row || !parsed.recastOf)
|
|
931
|
+
return true;
|
|
932
|
+
return (change.row.document.templateId === parsed.recastOf.templateId &&
|
|
933
|
+
change.row.document.recastKey === parsed.recastOf.recastKey);
|
|
934
|
+
}, watchSignal);
|
|
935
|
+
}
|
|
936
|
+
},
|
|
937
|
+
scripts: {
|
|
938
|
+
async get(id) {
|
|
939
|
+
return runHostOp(signal, backend, async () => {
|
|
940
|
+
requireNamespace("db");
|
|
941
|
+
const validId = parseHostId(id, backend);
|
|
942
|
+
const entry = scriptsMap.get(validId);
|
|
943
|
+
if (!entry || entry.owner !== userId)
|
|
944
|
+
return null;
|
|
945
|
+
return normalizeHostScriptRow(entry.row, backend);
|
|
946
|
+
});
|
|
947
|
+
},
|
|
948
|
+
async list(query, cursor) {
|
|
949
|
+
return runHostOp(signal, backend, async () => {
|
|
950
|
+
requireNamespace("db");
|
|
951
|
+
const parsed = query !== undefined
|
|
952
|
+
? parseHostSchema(scriptQuery, query, backend)
|
|
953
|
+
: {};
|
|
954
|
+
const rows = [...scriptsMap.values()]
|
|
955
|
+
.filter((entry) => entry.owner === userId)
|
|
956
|
+
.map((entry) => entry.row)
|
|
957
|
+
.filter((row) => parsed.timelineId === undefined ||
|
|
958
|
+
row.timelineId === parsed.timelineId)
|
|
959
|
+
.sort((a, b) => {
|
|
960
|
+
const byTime = b.updatedAt.localeCompare(a.updatedAt);
|
|
961
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
962
|
+
})
|
|
963
|
+
.map((row) => normalizeHostScriptRow(row, backend));
|
|
964
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
965
|
+
});
|
|
966
|
+
},
|
|
967
|
+
async create(input) {
|
|
968
|
+
return runHostOp(signal, backend, async () => {
|
|
969
|
+
requireNamespace("db");
|
|
970
|
+
const parsed = parseHostSchema(scriptCreate, input, backend);
|
|
971
|
+
const id = parsed.id ?? nextId();
|
|
972
|
+
const existing = scriptsMap.get(id);
|
|
973
|
+
if (existing) {
|
|
974
|
+
if (existing.owner !== userId) {
|
|
975
|
+
throw new HostError("not_found", `Script not found: ${id}`, backend);
|
|
976
|
+
}
|
|
977
|
+
return normalizeHostScriptRow(existing.row, backend);
|
|
978
|
+
}
|
|
979
|
+
const now = nextTimestamp();
|
|
980
|
+
const row = normalizeHostScriptRow({
|
|
981
|
+
id,
|
|
982
|
+
projectId,
|
|
983
|
+
name: parsed.name,
|
|
984
|
+
document: parsed.document ?? emptyHostScriptDocument(),
|
|
985
|
+
createdAt: now,
|
|
986
|
+
updatedAt: now
|
|
987
|
+
}, backend);
|
|
988
|
+
scriptsMap.set(id, { owner: userId, row });
|
|
989
|
+
scriptsEmitter.emit({
|
|
990
|
+
kind: "created",
|
|
991
|
+
id: row.id,
|
|
992
|
+
row,
|
|
993
|
+
updatedAt: row.updatedAt
|
|
994
|
+
});
|
|
995
|
+
return normalizeHostScriptRow(row, backend);
|
|
996
|
+
});
|
|
997
|
+
},
|
|
998
|
+
async update(id, patch) {
|
|
999
|
+
return runHostOp(signal, backend, async () => {
|
|
1000
|
+
requireNamespace("db");
|
|
1001
|
+
const validId = parseHostId(id, backend);
|
|
1002
|
+
const parsed = parseHostSchema(scriptPatch, patch, backend);
|
|
1003
|
+
const entry = scriptsMap.get(validId);
|
|
1004
|
+
if (!entry || entry.owner !== userId) {
|
|
1005
|
+
throw new HostError("not_found", `Script not found: ${validId}`, backend);
|
|
1006
|
+
}
|
|
1007
|
+
if (parsed.baseUpdatedAt !== undefined &&
|
|
1008
|
+
entry.row.updatedAt !== parsed.baseUpdatedAt) {
|
|
1009
|
+
throw new HostError("conflict", `Script ${validId} was modified concurrently`, backend);
|
|
1010
|
+
}
|
|
1011
|
+
const nextRow = {
|
|
1012
|
+
id: entry.row.id,
|
|
1013
|
+
projectId: entry.row.projectId,
|
|
1014
|
+
name: parsed.name ?? entry.row.name,
|
|
1015
|
+
document: parsed.document ?? entry.row.document,
|
|
1016
|
+
createdAt: entry.row.createdAt,
|
|
1017
|
+
updatedAt: nextTimestamp(entry.row.updatedAt)
|
|
1018
|
+
};
|
|
1019
|
+
const timelineId = parsed.timelineId !== undefined
|
|
1020
|
+
? (parsed.timelineId ?? undefined)
|
|
1021
|
+
: entry.row.timelineId;
|
|
1022
|
+
if (timelineId !== undefined) {
|
|
1023
|
+
nextRow.timelineId = timelineId;
|
|
1024
|
+
}
|
|
1025
|
+
const storyboardId = parsed.storyboardId !== undefined
|
|
1026
|
+
? (parsed.storyboardId ?? undefined)
|
|
1027
|
+
: entry.row.storyboardId;
|
|
1028
|
+
if (storyboardId !== undefined) {
|
|
1029
|
+
nextRow.storyboardId = storyboardId;
|
|
1030
|
+
}
|
|
1031
|
+
const normalized = normalizeHostScriptRow(nextRow, backend);
|
|
1032
|
+
scriptsMap.set(validId, { owner: userId, row: normalized });
|
|
1033
|
+
scriptsEmitter.emit({
|
|
1034
|
+
kind: "updated",
|
|
1035
|
+
id: normalized.id,
|
|
1036
|
+
row: normalized,
|
|
1037
|
+
updatedAt: normalized.updatedAt
|
|
1038
|
+
});
|
|
1039
|
+
return normalizeHostScriptRow(normalized, backend);
|
|
1040
|
+
});
|
|
1041
|
+
},
|
|
1042
|
+
async delete(id) {
|
|
1043
|
+
return runHostOp(signal, backend, async () => {
|
|
1044
|
+
requireNamespace("db");
|
|
1045
|
+
const validId = parseHostId(id, backend);
|
|
1046
|
+
const entry = scriptsMap.get(validId);
|
|
1047
|
+
if (!entry || entry.owner !== userId) {
|
|
1048
|
+
throw new HostError("not_found", `Script not found: ${validId}`, backend);
|
|
1049
|
+
}
|
|
1050
|
+
scriptsMap.delete(validId);
|
|
1051
|
+
scriptsEmitter.emit({ kind: "deleted", id: validId });
|
|
1052
|
+
});
|
|
1053
|
+
},
|
|
1054
|
+
watch(query, watchOptions) {
|
|
1055
|
+
if (deniedNamespaces.has("db")) {
|
|
1056
|
+
return unservedWatch("db", backend);
|
|
1057
|
+
}
|
|
1058
|
+
const parsed = query !== undefined
|
|
1059
|
+
? parseHostSchema(scriptQuery, query, backend)
|
|
1060
|
+
: {};
|
|
1061
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
1062
|
+
return scriptsEmitter.watch((change) => {
|
|
1063
|
+
if (!change.row || parsed.timelineId === undefined)
|
|
1064
|
+
return true;
|
|
1065
|
+
return change.row.timelineId === parsed.timelineId;
|
|
1066
|
+
}, watchSignal);
|
|
1067
|
+
}
|
|
1068
|
+
},
|
|
1069
|
+
timelineSequences: {
|
|
1070
|
+
async get(id) {
|
|
1071
|
+
return runHostOp(signal, backend, async () => {
|
|
1072
|
+
requireNamespace("db");
|
|
1073
|
+
const validId = parseHostId(id, backend);
|
|
1074
|
+
const entry = timelinesMap.get(validId);
|
|
1075
|
+
if (!entry || entry.owner !== userId)
|
|
1076
|
+
return null;
|
|
1077
|
+
return normalizeHostTimelineSequenceRow(entry.row, backend);
|
|
1078
|
+
});
|
|
1079
|
+
},
|
|
1080
|
+
async list(query, cursor) {
|
|
1081
|
+
return runHostOp(signal, backend, async () => {
|
|
1082
|
+
requireNamespace("db");
|
|
1083
|
+
const parsed = query !== undefined
|
|
1084
|
+
? parseHostSchema(timelineSequenceQuery, query, backend)
|
|
1085
|
+
: {};
|
|
1086
|
+
const rows = [...timelinesMap.values()]
|
|
1087
|
+
.filter((entry) => entry.owner === userId)
|
|
1088
|
+
.map((entry) => entry.row)
|
|
1089
|
+
.sort((a, b) => {
|
|
1090
|
+
const byTime = b.updatedAt.localeCompare(a.updatedAt);
|
|
1091
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
1092
|
+
})
|
|
1093
|
+
.map((row) => normalizeHostTimelineSequenceRow(row, backend));
|
|
1094
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
1095
|
+
});
|
|
1096
|
+
},
|
|
1097
|
+
async create(input) {
|
|
1098
|
+
return runHostOp(signal, backend, async () => {
|
|
1099
|
+
requireNamespace("db");
|
|
1100
|
+
const parsed = parseHostSchema(timelineSequenceCreate, input, backend);
|
|
1101
|
+
const id = parsed.id ?? nextId();
|
|
1102
|
+
const existing = timelinesMap.get(id);
|
|
1103
|
+
if (existing) {
|
|
1104
|
+
if (existing.owner !== userId) {
|
|
1105
|
+
throw new HostError("not_found", `Timeline sequence not found: ${id}`, backend);
|
|
1106
|
+
}
|
|
1107
|
+
return normalizeHostTimelineSequenceRow(existing.row, backend);
|
|
1108
|
+
}
|
|
1109
|
+
const now = nextTimestamp();
|
|
1110
|
+
const doc = parsed.document ?? emptyHostTimelineDocument();
|
|
1111
|
+
const candidate = {
|
|
1112
|
+
...doc,
|
|
1113
|
+
id,
|
|
1114
|
+
projectId,
|
|
1115
|
+
name: parsed.name,
|
|
1116
|
+
fps: parsed.fps,
|
|
1117
|
+
width: parsed.width,
|
|
1118
|
+
height: parsed.height,
|
|
1119
|
+
durationMs: timelineClipsDurationMs(doc.clips),
|
|
1120
|
+
createdAt: now,
|
|
1121
|
+
updatedAt: now
|
|
1122
|
+
};
|
|
1123
|
+
if (parsed.templateId !== undefined) {
|
|
1124
|
+
candidate.templateId = parsed.templateId;
|
|
1125
|
+
}
|
|
1126
|
+
const row = normalizeHostTimelineSequenceRow(candidate, backend);
|
|
1127
|
+
timelinesMap.set(id, { owner: userId, row });
|
|
1128
|
+
timelinesEmitter.emit({
|
|
1129
|
+
kind: "created",
|
|
1130
|
+
id: row.id,
|
|
1131
|
+
row,
|
|
1132
|
+
updatedAt: row.updatedAt
|
|
1133
|
+
});
|
|
1134
|
+
return normalizeHostTimelineSequenceRow(row, backend);
|
|
1135
|
+
});
|
|
1136
|
+
},
|
|
1137
|
+
async update(id, patch) {
|
|
1138
|
+
return runHostOp(signal, backend, async () => {
|
|
1139
|
+
requireNamespace("db");
|
|
1140
|
+
const validId = parseHostId(id, backend);
|
|
1141
|
+
const parsed = parseHostSchema(timelineSequencePatch, patch, backend);
|
|
1142
|
+
const entry = timelinesMap.get(validId);
|
|
1143
|
+
if (!entry || entry.owner !== userId) {
|
|
1144
|
+
throw new HostError("not_found", `Timeline sequence not found: ${validId}`, backend);
|
|
1145
|
+
}
|
|
1146
|
+
if (parsed.baseUpdatedAt !== undefined &&
|
|
1147
|
+
entry.row.updatedAt !== parsed.baseUpdatedAt) {
|
|
1148
|
+
throw new HostError("conflict", `Timeline sequence ${validId} was modified concurrently`, backend);
|
|
1149
|
+
}
|
|
1150
|
+
const nextRow = {
|
|
1151
|
+
...entry.row,
|
|
1152
|
+
...(parsed.document ?? {}),
|
|
1153
|
+
id: entry.row.id,
|
|
1154
|
+
projectId: entry.row.projectId,
|
|
1155
|
+
name: parsed.name ?? entry.row.name,
|
|
1156
|
+
fps: parsed.fps ?? entry.row.fps,
|
|
1157
|
+
width: parsed.width ?? entry.row.width,
|
|
1158
|
+
height: parsed.height ?? entry.row.height,
|
|
1159
|
+
durationMs: parsed.durationMs !== undefined
|
|
1160
|
+
? parsed.durationMs
|
|
1161
|
+
: parsed.document !== undefined
|
|
1162
|
+
? timelineClipsDurationMs(parsed.document.clips)
|
|
1163
|
+
: entry.row.durationMs,
|
|
1164
|
+
createdAt: entry.row.createdAt,
|
|
1165
|
+
updatedAt: nextTimestamp(entry.row.updatedAt)
|
|
1166
|
+
};
|
|
1167
|
+
const normalized = normalizeHostTimelineSequenceRow(nextRow, backend);
|
|
1168
|
+
timelinesMap.set(validId, { owner: userId, row: normalized });
|
|
1169
|
+
timelinesEmitter.emit({
|
|
1170
|
+
kind: "updated",
|
|
1171
|
+
id: normalized.id,
|
|
1172
|
+
row: normalized,
|
|
1173
|
+
updatedAt: normalized.updatedAt
|
|
1174
|
+
});
|
|
1175
|
+
return normalizeHostTimelineSequenceRow(normalized, backend);
|
|
1176
|
+
});
|
|
1177
|
+
},
|
|
1178
|
+
async delete(id) {
|
|
1179
|
+
return runHostOp(signal, backend, async () => {
|
|
1180
|
+
requireNamespace("db");
|
|
1181
|
+
const validId = parseHostId(id, backend);
|
|
1182
|
+
const entry = timelinesMap.get(validId);
|
|
1183
|
+
if (!entry || entry.owner !== userId) {
|
|
1184
|
+
throw new HostError("not_found", `Timeline sequence not found: ${validId}`, backend);
|
|
1185
|
+
}
|
|
1186
|
+
timelinesMap.delete(validId);
|
|
1187
|
+
timelinesEmitter.emit({ kind: "deleted", id: validId });
|
|
1188
|
+
});
|
|
1189
|
+
},
|
|
1190
|
+
watch(query, watchOptions) {
|
|
1191
|
+
if (deniedNamespaces.has("db")) {
|
|
1192
|
+
return unservedWatch("db", backend);
|
|
1193
|
+
}
|
|
1194
|
+
if (query !== undefined) {
|
|
1195
|
+
parseHostSchema(timelineSequenceQuery, query, backend);
|
|
1196
|
+
}
|
|
1197
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
1198
|
+
return timelinesEmitter.watch(undefined, watchSignal);
|
|
1199
|
+
}
|
|
1200
|
+
},
|
|
1201
|
+
imageDocuments: {
|
|
1202
|
+
async get(id) {
|
|
1203
|
+
return runHostOp(signal, backend, async () => {
|
|
1204
|
+
requireNamespace("db");
|
|
1205
|
+
const validId = parseHostId(id, backend);
|
|
1206
|
+
const entry = imageDocsMap.get(validId);
|
|
1207
|
+
if (!entry || entry.owner !== userId)
|
|
1208
|
+
return null;
|
|
1209
|
+
return normalizeHostImageDocumentRow(entry.row, backend);
|
|
1210
|
+
});
|
|
1211
|
+
},
|
|
1212
|
+
async list(query, cursor) {
|
|
1213
|
+
return runHostOp(signal, backend, async () => {
|
|
1214
|
+
requireNamespace("db");
|
|
1215
|
+
const parsed = query !== undefined
|
|
1216
|
+
? parseHostSchema(imageDocumentQuery, query, backend)
|
|
1217
|
+
: {};
|
|
1218
|
+
const rows = [...imageDocsMap.values()]
|
|
1219
|
+
.filter((entry) => entry.owner === userId)
|
|
1220
|
+
.map((entry) => entry.row)
|
|
1221
|
+
.sort((a, b) => {
|
|
1222
|
+
const byTime = b.updatedAt.localeCompare(a.updatedAt);
|
|
1223
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
1224
|
+
})
|
|
1225
|
+
.map((row) => normalizeHostImageDocumentRow(row, backend));
|
|
1226
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
1227
|
+
});
|
|
1228
|
+
},
|
|
1229
|
+
async create(input) {
|
|
1230
|
+
return runHostOp(signal, backend, async () => {
|
|
1231
|
+
requireNamespace("db");
|
|
1232
|
+
const parsed = parseHostSchema(imageDocumentCreate, input, backend);
|
|
1233
|
+
const id = parsed.id ?? nextId();
|
|
1234
|
+
const existing = imageDocsMap.get(id);
|
|
1235
|
+
if (existing) {
|
|
1236
|
+
if (existing.owner !== userId) {
|
|
1237
|
+
throw new HostError("not_found", `Image document not found: ${id}`, backend);
|
|
1238
|
+
}
|
|
1239
|
+
return normalizeHostImageDocumentRow(existing.row, backend);
|
|
1240
|
+
}
|
|
1241
|
+
const now = nextTimestamp();
|
|
1242
|
+
const row = normalizeHostImageDocumentRow({
|
|
1243
|
+
id,
|
|
1244
|
+
projectId,
|
|
1245
|
+
name: parsed.name,
|
|
1246
|
+
width: parsed.width,
|
|
1247
|
+
height: parsed.height,
|
|
1248
|
+
backgroundColor: parsed.backgroundColor,
|
|
1249
|
+
document: parsed.document ??
|
|
1250
|
+
emptyHostImageDocumentData(parsed.width, parsed.height, parsed.backgroundColor),
|
|
1251
|
+
createdAt: now,
|
|
1252
|
+
updatedAt: now
|
|
1253
|
+
}, backend);
|
|
1254
|
+
imageDocsMap.set(id, { owner: userId, row });
|
|
1255
|
+
imageDocsEmitter.emit({
|
|
1256
|
+
kind: "created",
|
|
1257
|
+
id: row.id,
|
|
1258
|
+
row,
|
|
1259
|
+
updatedAt: row.updatedAt
|
|
1260
|
+
});
|
|
1261
|
+
return normalizeHostImageDocumentRow(row, backend);
|
|
1262
|
+
});
|
|
1263
|
+
},
|
|
1264
|
+
async update(id, patch) {
|
|
1265
|
+
return runHostOp(signal, backend, async () => {
|
|
1266
|
+
requireNamespace("db");
|
|
1267
|
+
const validId = parseHostId(id, backend);
|
|
1268
|
+
const parsed = parseHostSchema(imageDocumentPatch, patch, backend);
|
|
1269
|
+
const entry = imageDocsMap.get(validId);
|
|
1270
|
+
if (!entry || entry.owner !== userId) {
|
|
1271
|
+
throw new HostError("not_found", `Image document not found: ${validId}`, backend);
|
|
1272
|
+
}
|
|
1273
|
+
if (parsed.baseUpdatedAt !== undefined &&
|
|
1274
|
+
entry.row.updatedAt !== parsed.baseUpdatedAt) {
|
|
1275
|
+
throw new HostError("conflict", `Image document ${validId} was modified concurrently`, backend);
|
|
1276
|
+
}
|
|
1277
|
+
const nextRow = {
|
|
1278
|
+
id: entry.row.id,
|
|
1279
|
+
projectId: entry.row.projectId,
|
|
1280
|
+
name: parsed.name ?? entry.row.name,
|
|
1281
|
+
width: parsed.width ?? entry.row.width,
|
|
1282
|
+
height: parsed.height ?? entry.row.height,
|
|
1283
|
+
backgroundColor: parsed.backgroundColor ?? entry.row.backgroundColor,
|
|
1284
|
+
document: parsed.document ?? entry.row.document,
|
|
1285
|
+
createdAt: entry.row.createdAt,
|
|
1286
|
+
updatedAt: nextTimestamp(entry.row.updatedAt)
|
|
1287
|
+
};
|
|
1288
|
+
if (entry.row.workflowId !== undefined) {
|
|
1289
|
+
nextRow.workflowId = entry.row.workflowId;
|
|
1290
|
+
}
|
|
1291
|
+
const thumbnailAssetId = parsed.thumbnailAssetId !== undefined
|
|
1292
|
+
? (parsed.thumbnailAssetId ?? undefined)
|
|
1293
|
+
: entry.row.thumbnailAssetId;
|
|
1294
|
+
if (thumbnailAssetId !== undefined) {
|
|
1295
|
+
nextRow.thumbnailAssetId = thumbnailAssetId;
|
|
1296
|
+
}
|
|
1297
|
+
const normalized = normalizeHostImageDocumentRow(nextRow, backend);
|
|
1298
|
+
imageDocsMap.set(validId, { owner: userId, row: normalized });
|
|
1299
|
+
imageDocsEmitter.emit({
|
|
1300
|
+
kind: "updated",
|
|
1301
|
+
id: normalized.id,
|
|
1302
|
+
row: normalized,
|
|
1303
|
+
updatedAt: normalized.updatedAt
|
|
1304
|
+
});
|
|
1305
|
+
return normalizeHostImageDocumentRow(normalized, backend);
|
|
1306
|
+
});
|
|
1307
|
+
},
|
|
1308
|
+
async delete(id) {
|
|
1309
|
+
return runHostOp(signal, backend, async () => {
|
|
1310
|
+
requireNamespace("db");
|
|
1311
|
+
const validId = parseHostId(id, backend);
|
|
1312
|
+
const entry = imageDocsMap.get(validId);
|
|
1313
|
+
if (!entry || entry.owner !== userId) {
|
|
1314
|
+
throw new HostError("not_found", `Image document not found: ${validId}`, backend);
|
|
1315
|
+
}
|
|
1316
|
+
imageDocsMap.delete(validId);
|
|
1317
|
+
imageDocsEmitter.emit({ kind: "deleted", id: validId });
|
|
1318
|
+
});
|
|
1319
|
+
},
|
|
1320
|
+
watch(query, watchOptions) {
|
|
1321
|
+
if (deniedNamespaces.has("db")) {
|
|
1322
|
+
return unservedWatch("db", backend);
|
|
1323
|
+
}
|
|
1324
|
+
if (query !== undefined) {
|
|
1325
|
+
parseHostSchema(imageDocumentQuery, query, backend);
|
|
1326
|
+
}
|
|
1327
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
1328
|
+
return imageDocsEmitter.watch(undefined, watchSignal);
|
|
1329
|
+
}
|
|
1330
|
+
},
|
|
1331
|
+
entities: {
|
|
1332
|
+
async get(id) {
|
|
1333
|
+
return runHostOp(signal, backend, async () => {
|
|
1334
|
+
requireNamespace("db");
|
|
1335
|
+
const validId = parseHostId(id, backend);
|
|
1336
|
+
const entry = entitiesMap.get(validId);
|
|
1337
|
+
if (!entry || entry.owner !== userId)
|
|
1338
|
+
return null;
|
|
1339
|
+
return normalizeHostEntityRow(entry.row, backend);
|
|
1340
|
+
});
|
|
1341
|
+
},
|
|
1342
|
+
async list(query, cursor) {
|
|
1343
|
+
return runHostOp(signal, backend, async () => {
|
|
1344
|
+
requireNamespace("db");
|
|
1345
|
+
const parsed = query !== undefined
|
|
1346
|
+
? parseHostSchema(entityQuery, query, backend)
|
|
1347
|
+
: {};
|
|
1348
|
+
const wanted = parsed.nameContains
|
|
1349
|
+
? normEntityName(parsed.nameContains)
|
|
1350
|
+
: "";
|
|
1351
|
+
const rows = [...entitiesMap.values()]
|
|
1352
|
+
.filter((entry) => entry.owner === userId)
|
|
1353
|
+
.map((entry) => entry.row)
|
|
1354
|
+
.filter((row) => {
|
|
1355
|
+
if (parsed.kind && row.kind !== parsed.kind)
|
|
1356
|
+
return false;
|
|
1357
|
+
if (wanted && !normEntityName(row.name).includes(wanted)) {
|
|
1358
|
+
return false;
|
|
1359
|
+
}
|
|
1360
|
+
if (parsed.tags && parsed.tags.length > 0) {
|
|
1361
|
+
const own = row.tags ?? [];
|
|
1362
|
+
if (!parsed.tags.every((tag) => own.includes(tag))) {
|
|
1363
|
+
return false;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
return true;
|
|
1367
|
+
})
|
|
1368
|
+
.sort((a, b) => {
|
|
1369
|
+
const byTime = (b.created_at ?? "").localeCompare(a.created_at ?? "");
|
|
1370
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
1371
|
+
})
|
|
1372
|
+
.map((row) => normalizeHostEntityRow(row, backend));
|
|
1373
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
1374
|
+
});
|
|
1375
|
+
},
|
|
1376
|
+
async create(input) {
|
|
1377
|
+
return runHostOp(signal, backend, async () => {
|
|
1378
|
+
requireNamespace("db");
|
|
1379
|
+
const parsed = parseHostSchema(entityCreate, input, backend);
|
|
1380
|
+
const assetEntry = assetsMap.get(parsed.imageAssetId);
|
|
1381
|
+
if (!assetEntry || assetEntry.owner !== userId) {
|
|
1382
|
+
throw new HostError("not_found", `Asset ${parsed.imageAssetId} was not found`, backend);
|
|
1383
|
+
}
|
|
1384
|
+
if (!assetEntry.row.content_type.startsWith("image/")) {
|
|
1385
|
+
throw new HostError("invalid", `${assetEntry.row.name || assetEntry.row.id} is a ${assetEntry.row.content_type} asset; entities are image assets`, backend);
|
|
1386
|
+
}
|
|
1387
|
+
const sourceKey = parsed.source?.key?.trim();
|
|
1388
|
+
const owned = [...entitiesMap.values()].filter((entry) => entry.owner === userId &&
|
|
1389
|
+
(entry.row.project_id ?? "default") === projectId);
|
|
1390
|
+
const existingByKey = sourceKey
|
|
1391
|
+
? owned.find((entry) => entry.row.source?.key === sourceKey)
|
|
1392
|
+
: undefined;
|
|
1393
|
+
const existingByName = !existingByKey
|
|
1394
|
+
? owned.find((entry) => entry.row.kind === parsed.kind &&
|
|
1395
|
+
normEntityName(entry.row.name) ===
|
|
1396
|
+
normEntityName(parsed.name) &&
|
|
1397
|
+
(!sourceKey || !entry.row.source?.key?.trim()))
|
|
1398
|
+
: undefined;
|
|
1399
|
+
const existing = existingByKey ?? existingByName;
|
|
1400
|
+
const id = existing ? existing.row.id : assetEntry.row.id;
|
|
1401
|
+
const refUri = id === assetEntry.row.id
|
|
1402
|
+
? `asset://${assetEntry.row.id}`
|
|
1403
|
+
: `asset://${assetEntry.row.id}`;
|
|
1404
|
+
const candidate = {
|
|
1405
|
+
...(existing?.row ?? {}),
|
|
1406
|
+
type: "entity",
|
|
1407
|
+
id,
|
|
1408
|
+
kind: parsed.kind,
|
|
1409
|
+
name: parsed.name,
|
|
1410
|
+
descriptor: parsed.descriptor,
|
|
1411
|
+
project_id: projectId,
|
|
1412
|
+
reference_images: [
|
|
1413
|
+
{
|
|
1414
|
+
type: "image",
|
|
1415
|
+
asset_id: assetEntry.row.id,
|
|
1416
|
+
uri: refUri
|
|
1417
|
+
}
|
|
1418
|
+
],
|
|
1419
|
+
created_at: existing?.row.created_at ??
|
|
1420
|
+
assetEntry.row.created_at ??
|
|
1421
|
+
nextTimestamp()
|
|
1422
|
+
};
|
|
1423
|
+
if (parsed.description !== undefined) {
|
|
1424
|
+
candidate.description = parsed.description;
|
|
1425
|
+
}
|
|
1426
|
+
if (parsed.tags !== undefined) {
|
|
1427
|
+
candidate.tags = [...parsed.tags];
|
|
1428
|
+
}
|
|
1429
|
+
if (parsed.voiceId !== undefined) {
|
|
1430
|
+
candidate.voice_id = parsed.voiceId;
|
|
1431
|
+
}
|
|
1432
|
+
if (parsed.source !== undefined) {
|
|
1433
|
+
candidate.source = { ...parsed.source };
|
|
1434
|
+
}
|
|
1435
|
+
const normalized = normalizeHostEntityRow(candidate, backend);
|
|
1436
|
+
entitiesMap.set(id, { owner: userId, row: normalized });
|
|
1437
|
+
entitiesEmitter.emit({
|
|
1438
|
+
kind: existing ? "updated" : "created",
|
|
1439
|
+
id: normalized.id,
|
|
1440
|
+
row: normalized,
|
|
1441
|
+
updatedAt: normalized.updated_at
|
|
1442
|
+
});
|
|
1443
|
+
return normalizeHostEntityRow(normalized, backend);
|
|
1444
|
+
});
|
|
1445
|
+
},
|
|
1446
|
+
async update(id, patch) {
|
|
1447
|
+
return runHostOp(signal, backend, async () => {
|
|
1448
|
+
requireNamespace("db");
|
|
1449
|
+
const validId = parseHostId(id, backend);
|
|
1450
|
+
const parsed = parseHostSchema(entityPatch, patch, backend);
|
|
1451
|
+
const entry = entitiesMap.get(validId);
|
|
1452
|
+
if (!entry || entry.owner !== userId) {
|
|
1453
|
+
throw new HostError("not_found", `Entity not found: ${validId}`, backend);
|
|
1454
|
+
}
|
|
1455
|
+
let referenceImages = entry.row.reference_images;
|
|
1456
|
+
if (parsed.imageAssetId !== undefined) {
|
|
1457
|
+
const assetEntry = assetsMap.get(parsed.imageAssetId);
|
|
1458
|
+
if (!assetEntry || assetEntry.owner !== userId) {
|
|
1459
|
+
throw new HostError("not_found", `Asset ${parsed.imageAssetId} was not found`, backend);
|
|
1460
|
+
}
|
|
1461
|
+
if (!assetEntry.row.content_type.startsWith("image/")) {
|
|
1462
|
+
throw new HostError("invalid", `${assetEntry.row.name || assetEntry.row.id} is a ${assetEntry.row.content_type} asset; entities are image assets`, backend);
|
|
1463
|
+
}
|
|
1464
|
+
referenceImages = [
|
|
1465
|
+
{
|
|
1466
|
+
type: "image",
|
|
1467
|
+
asset_id: assetEntry.row.id,
|
|
1468
|
+
uri: `asset://${assetEntry.row.id}`
|
|
1469
|
+
}
|
|
1470
|
+
];
|
|
1471
|
+
}
|
|
1472
|
+
const candidate = {
|
|
1473
|
+
...entry.row,
|
|
1474
|
+
type: "entity",
|
|
1475
|
+
id: entry.row.id,
|
|
1476
|
+
kind: parsed.kind ?? entry.row.kind,
|
|
1477
|
+
name: parsed.name ?? entry.row.name,
|
|
1478
|
+
descriptor: parsed.descriptor ?? entry.row.descriptor,
|
|
1479
|
+
reference_images: referenceImages,
|
|
1480
|
+
updated_at: nextTimestamp(entry.row.updated_at)
|
|
1481
|
+
};
|
|
1482
|
+
if (parsed.description !== undefined) {
|
|
1483
|
+
candidate.description = parsed.description;
|
|
1484
|
+
}
|
|
1485
|
+
if (parsed.tags !== undefined) {
|
|
1486
|
+
candidate.tags = [...parsed.tags];
|
|
1487
|
+
}
|
|
1488
|
+
if (parsed.voiceId !== undefined) {
|
|
1489
|
+
candidate.voice_id = parsed.voiceId;
|
|
1490
|
+
}
|
|
1491
|
+
if (parsed.source !== undefined) {
|
|
1492
|
+
candidate.source = { ...parsed.source };
|
|
1493
|
+
}
|
|
1494
|
+
const normalized = normalizeHostEntityRow(candidate, backend);
|
|
1495
|
+
entitiesMap.set(validId, { owner: userId, row: normalized });
|
|
1496
|
+
entitiesEmitter.emit({
|
|
1497
|
+
kind: "updated",
|
|
1498
|
+
id: normalized.id,
|
|
1499
|
+
row: normalized,
|
|
1500
|
+
updatedAt: normalized.updated_at
|
|
1501
|
+
});
|
|
1502
|
+
return normalizeHostEntityRow(normalized, backend);
|
|
1503
|
+
});
|
|
1504
|
+
},
|
|
1505
|
+
async delete(id) {
|
|
1506
|
+
return runHostOp(signal, backend, async () => {
|
|
1507
|
+
requireNamespace("db");
|
|
1508
|
+
const validId = parseHostId(id, backend);
|
|
1509
|
+
const entry = entitiesMap.get(validId);
|
|
1510
|
+
if (!entry || entry.owner !== userId) {
|
|
1511
|
+
throw new HostError("not_found", `Entity not found: ${validId}`, backend);
|
|
1512
|
+
}
|
|
1513
|
+
entitiesMap.delete(validId);
|
|
1514
|
+
entitiesEmitter.emit({ kind: "deleted", id: validId });
|
|
1515
|
+
});
|
|
1516
|
+
},
|
|
1517
|
+
watch(query, watchOptions) {
|
|
1518
|
+
if (deniedNamespaces.has("db")) {
|
|
1519
|
+
return unservedWatch("db", backend);
|
|
1520
|
+
}
|
|
1521
|
+
const parsed = query !== undefined
|
|
1522
|
+
? parseHostSchema(entityQuery, query, backend)
|
|
1523
|
+
: {};
|
|
1524
|
+
const wanted = parsed.nameContains
|
|
1525
|
+
? normEntityName(parsed.nameContains)
|
|
1526
|
+
: "";
|
|
1527
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
1528
|
+
return entitiesEmitter.watch((change) => {
|
|
1529
|
+
if (!change.row)
|
|
1530
|
+
return true;
|
|
1531
|
+
if (parsed.kind && change.row.kind !== parsed.kind)
|
|
1532
|
+
return false;
|
|
1533
|
+
if (wanted && !normEntityName(change.row.name).includes(wanted)) {
|
|
1534
|
+
return false;
|
|
1535
|
+
}
|
|
1536
|
+
if (parsed.tags && parsed.tags.length > 0) {
|
|
1537
|
+
const own = change.row.tags ?? [];
|
|
1538
|
+
if (!parsed.tags.every((tag) => own.includes(tag)))
|
|
1539
|
+
return false;
|
|
1540
|
+
}
|
|
1541
|
+
return true;
|
|
1542
|
+
}, watchSignal);
|
|
1543
|
+
}
|
|
1544
|
+
},
|
|
1545
|
+
projects: {
|
|
1546
|
+
async get(id) {
|
|
1547
|
+
return runHostOp(signal, backend, async () => {
|
|
1548
|
+
requireNamespace("db");
|
|
1549
|
+
const validId = parseHostId(id, backend);
|
|
1550
|
+
const entry = projectsMap.get(validId);
|
|
1551
|
+
if (!entry || entry.owner !== userId)
|
|
1552
|
+
return null;
|
|
1553
|
+
return normalizeHostProjectRow(entry.row, backend);
|
|
1554
|
+
});
|
|
1555
|
+
},
|
|
1556
|
+
async list(query, cursor) {
|
|
1557
|
+
return runHostOp(signal, backend, async () => {
|
|
1558
|
+
requireNamespace("db");
|
|
1559
|
+
const parsed = query !== undefined
|
|
1560
|
+
? parseHostSchema(projectQuery, query, backend)
|
|
1561
|
+
: {};
|
|
1562
|
+
const wantArchived = parsed.archived ?? false;
|
|
1563
|
+
const rows = [...projectsMap.values()]
|
|
1564
|
+
.filter((entry) => entry.owner === userId)
|
|
1565
|
+
.map((entry) => entry.row)
|
|
1566
|
+
.filter((row) => wantArchived
|
|
1567
|
+
? row.archivedAt !== null
|
|
1568
|
+
: row.archivedAt === null)
|
|
1569
|
+
.sort((a, b) => {
|
|
1570
|
+
const byTime = b.updatedAt.localeCompare(a.updatedAt);
|
|
1571
|
+
return byTime !== 0 ? byTime : b.id.localeCompare(a.id);
|
|
1572
|
+
})
|
|
1573
|
+
.map((row) => normalizeHostProjectRow(row, backend));
|
|
1574
|
+
return paginateHostRows(rows, (row) => row.id, parsed.limit ?? 50, cursor, backend);
|
|
1575
|
+
});
|
|
1576
|
+
},
|
|
1577
|
+
async create(input) {
|
|
1578
|
+
return runHostOp(signal, backend, async () => {
|
|
1579
|
+
requireNamespace("db");
|
|
1580
|
+
const parsed = parseHostSchema(projectCreate, input, backend);
|
|
1581
|
+
const id = parsed.id ?? nextId();
|
|
1582
|
+
const existing = projectsMap.get(id);
|
|
1583
|
+
if (existing) {
|
|
1584
|
+
if (existing.owner !== userId) {
|
|
1585
|
+
throw new HostError("not_found", `Project not found: ${id}`, backend);
|
|
1586
|
+
}
|
|
1587
|
+
return normalizeHostProjectRow(existing.row, backend);
|
|
1588
|
+
}
|
|
1589
|
+
const now = nextTimestamp();
|
|
1590
|
+
const row = normalizeHostProjectRow({
|
|
1591
|
+
id,
|
|
1592
|
+
name: parsed.name,
|
|
1593
|
+
kind: parsed.kind,
|
|
1594
|
+
isPersonal: parsed.kind === "personal",
|
|
1595
|
+
archivedAt: null,
|
|
1596
|
+
threadId: null,
|
|
1597
|
+
createdAt: now,
|
|
1598
|
+
updatedAt: now
|
|
1599
|
+
}, backend);
|
|
1600
|
+
projectsMap.set(id, { owner: userId, row });
|
|
1601
|
+
projectsEmitter.emit({
|
|
1602
|
+
kind: "created",
|
|
1603
|
+
id: row.id,
|
|
1604
|
+
row,
|
|
1605
|
+
updatedAt: row.updatedAt
|
|
1606
|
+
});
|
|
1607
|
+
return normalizeHostProjectRow(row, backend);
|
|
1608
|
+
});
|
|
1609
|
+
},
|
|
1610
|
+
async update(id, patch) {
|
|
1611
|
+
return runHostOp(signal, backend, async () => {
|
|
1612
|
+
requireNamespace("db");
|
|
1613
|
+
const validId = parseHostId(id, backend);
|
|
1614
|
+
const parsed = parseHostSchema(projectPatch, patch, backend);
|
|
1615
|
+
const entry = projectsMap.get(validId);
|
|
1616
|
+
if (!entry || entry.owner !== userId) {
|
|
1617
|
+
throw new HostError("not_found", `Project not found: ${validId}`, backend);
|
|
1618
|
+
}
|
|
1619
|
+
if (parsed.kind !== undefined &&
|
|
1620
|
+
parsed.kind !== entry.row.kind &&
|
|
1621
|
+
(entry.row.kind === "personal" || parsed.kind === "personal")) {
|
|
1622
|
+
throw new HostError("not_found", `Project not found: ${validId}`, backend);
|
|
1623
|
+
}
|
|
1624
|
+
const nextKind = parsed.kind ?? entry.row.kind;
|
|
1625
|
+
const nextRow = normalizeHostProjectRow({
|
|
1626
|
+
...entry.row,
|
|
1627
|
+
name: parsed.name ?? entry.row.name,
|
|
1628
|
+
kind: nextKind,
|
|
1629
|
+
isPersonal: nextKind === "personal",
|
|
1630
|
+
updatedAt: nextTimestamp(entry.row.updatedAt)
|
|
1631
|
+
}, backend);
|
|
1632
|
+
projectsMap.set(validId, { owner: userId, row: nextRow });
|
|
1633
|
+
projectsEmitter.emit({
|
|
1634
|
+
kind: "updated",
|
|
1635
|
+
id: nextRow.id,
|
|
1636
|
+
row: nextRow,
|
|
1637
|
+
updatedAt: nextRow.updatedAt
|
|
1638
|
+
});
|
|
1639
|
+
return normalizeHostProjectRow(nextRow, backend);
|
|
1640
|
+
});
|
|
1641
|
+
},
|
|
1642
|
+
async delete(id) {
|
|
1643
|
+
return runHostOp(signal, backend, async () => {
|
|
1644
|
+
requireNamespace("db");
|
|
1645
|
+
const validId = parseHostId(id, backend);
|
|
1646
|
+
const entry = projectsMap.get(validId);
|
|
1647
|
+
if (!entry || entry.owner !== userId || entry.row.isPersonal) {
|
|
1648
|
+
throw new HostError("not_found", `Project not found: ${validId}`, backend);
|
|
1649
|
+
}
|
|
1650
|
+
projectsMap.delete(validId);
|
|
1651
|
+
projectsEmitter.emit({ kind: "deleted", id: validId });
|
|
1652
|
+
});
|
|
1653
|
+
},
|
|
1654
|
+
watch(query, watchOptions) {
|
|
1655
|
+
if (deniedNamespaces.has("db")) {
|
|
1656
|
+
return unservedWatch("db", backend);
|
|
1657
|
+
}
|
|
1658
|
+
const parsed = query !== undefined
|
|
1659
|
+
? parseHostSchema(projectQuery, query, backend)
|
|
1660
|
+
: {};
|
|
1661
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
1662
|
+
return projectsEmitter.watch((change) => {
|
|
1663
|
+
if (!change.row || parsed.archived === undefined)
|
|
1664
|
+
return true;
|
|
1665
|
+
return (change.row.archivedAt !== null) === parsed.archived;
|
|
1666
|
+
}, watchSignal);
|
|
1667
|
+
}
|
|
1668
|
+
},
|
|
1669
|
+
settings: {
|
|
1670
|
+
async get(id) {
|
|
1671
|
+
return runHostOp(signal, backend, async () => {
|
|
1672
|
+
requireNamespace("db");
|
|
1673
|
+
const key = parseHostId(id, backend);
|
|
1674
|
+
return readSettingRow(key);
|
|
1675
|
+
});
|
|
1676
|
+
},
|
|
1677
|
+
async list(query, cursor) {
|
|
1678
|
+
return runHostOp(signal, backend, async () => {
|
|
1679
|
+
requireNamespace("db");
|
|
1680
|
+
const parsed = query !== undefined
|
|
1681
|
+
? parseHostSchema(settingQuery, query, backend)
|
|
1682
|
+
: {};
|
|
1683
|
+
const wanted = parsed.keys ? new Set(parsed.keys) : null;
|
|
1684
|
+
const rows = listAllSettingRows()
|
|
1685
|
+
.filter((row) => !wanted || wanted.has(row.key))
|
|
1686
|
+
.sort((a, b) => a.key.localeCompare(b.key));
|
|
1687
|
+
return paginateHostRows(rows, (row) => row.key, rows.length || 1, cursor, backend);
|
|
1688
|
+
});
|
|
1689
|
+
},
|
|
1690
|
+
watch(query, watchOptions) {
|
|
1691
|
+
if (deniedNamespaces.has("db")) {
|
|
1692
|
+
return unservedWatch("db", backend);
|
|
1693
|
+
}
|
|
1694
|
+
const parsed = query !== undefined
|
|
1695
|
+
? parseHostSchema(settingQuery, query, backend)
|
|
1696
|
+
: {};
|
|
1697
|
+
const wanted = parsed.keys ? new Set(parsed.keys) : null;
|
|
1698
|
+
const watchSignal = combineHostSignals(signal, watchOptions?.signal);
|
|
1699
|
+
return settingsEmitter.watch((change) => {
|
|
1700
|
+
if (!change.row || !wanted)
|
|
1701
|
+
return true;
|
|
1702
|
+
return wanted.has(change.row.key);
|
|
1703
|
+
}, watchSignal);
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
},
|
|
1707
|
+
storage: {
|
|
1708
|
+
assets: {
|
|
1709
|
+
async put(bytes, meta) {
|
|
1710
|
+
return runHostOp(signal, backend, async () => {
|
|
1711
|
+
requireNamespace("storage");
|
|
1712
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
1713
|
+
throw new HostError("invalid", "Asset bytes must be a Uint8Array", backend);
|
|
1714
|
+
}
|
|
1715
|
+
const parsed = parseHostSchema(assetPut, meta, backend);
|
|
1716
|
+
const id = nextId();
|
|
1717
|
+
const now = nextTimestamp();
|
|
1718
|
+
const row = {
|
|
1719
|
+
id,
|
|
1720
|
+
user_id: userId,
|
|
1721
|
+
workflow_id: workflowId,
|
|
1722
|
+
parent_id: parsed.parentId ?? userId,
|
|
1723
|
+
name: parsed.name,
|
|
1724
|
+
content_type: parsed.contentType,
|
|
1725
|
+
size: bytes.byteLength,
|
|
1726
|
+
metadata: parsed.metadata ?? null,
|
|
1727
|
+
sketch_document_id: null,
|
|
1728
|
+
created_at: now,
|
|
1729
|
+
updated_at: now,
|
|
1730
|
+
duration: null,
|
|
1731
|
+
node_id: parsed.nodeId ?? null,
|
|
1732
|
+
job_id: jobId,
|
|
1733
|
+
timeline_id: null,
|
|
1734
|
+
project_id: projectId
|
|
1735
|
+
};
|
|
1736
|
+
if (parsed.path) {
|
|
1737
|
+
row.workspace_path = parsed.path;
|
|
1738
|
+
}
|
|
1739
|
+
const normalized = normalizeHostAssetRow(row, backend);
|
|
1740
|
+
assetsMap.set(id, { owner: userId, row: normalized });
|
|
1741
|
+
assetBytesMap.set(id, new Uint8Array(bytes));
|
|
1742
|
+
assetsEmitter.emit({
|
|
1743
|
+
kind: "created",
|
|
1744
|
+
id: normalized.id,
|
|
1745
|
+
row: normalized,
|
|
1746
|
+
updatedAt: normalized.updated_at
|
|
1747
|
+
});
|
|
1748
|
+
return normalizeHostAssetRow(normalized, backend);
|
|
1749
|
+
});
|
|
1750
|
+
},
|
|
1751
|
+
async read(id) {
|
|
1752
|
+
return runHostOp(signal, backend, async () => {
|
|
1753
|
+
requireNamespace("storage");
|
|
1754
|
+
const validId = parseHostId(id, backend);
|
|
1755
|
+
const entry = assetsMap.get(validId);
|
|
1756
|
+
if (!entry || entry.owner !== userId)
|
|
1757
|
+
return null;
|
|
1758
|
+
const bytes = assetBytesMap.get(validId);
|
|
1759
|
+
return bytes ? new Uint8Array(bytes) : null;
|
|
1760
|
+
});
|
|
1761
|
+
},
|
|
1762
|
+
async update(id, bytes, meta) {
|
|
1763
|
+
return runHostOp(signal, backend, async () => {
|
|
1764
|
+
requireNamespace("storage");
|
|
1765
|
+
const validId = parseHostId(id, backend);
|
|
1766
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
1767
|
+
throw new HostError("invalid", "Asset bytes must be a Uint8Array", backend);
|
|
1768
|
+
}
|
|
1769
|
+
const parsedMeta = meta !== undefined
|
|
1770
|
+
? parseHostSchema(assetUpdateMeta, meta, backend)
|
|
1771
|
+
: undefined;
|
|
1772
|
+
const entry = assetsMap.get(validId);
|
|
1773
|
+
if (!entry || entry.owner !== userId)
|
|
1774
|
+
return null;
|
|
1775
|
+
const nextRow = normalizeHostAssetRow({
|
|
1776
|
+
...entry.row,
|
|
1777
|
+
name: parsedMeta?.name ?? entry.row.name,
|
|
1778
|
+
content_type: parsedMeta?.contentType ?? entry.row.content_type,
|
|
1779
|
+
size: bytes.byteLength,
|
|
1780
|
+
updated_at: nextTimestamp(entry.row.updated_at)
|
|
1781
|
+
}, backend);
|
|
1782
|
+
assetsMap.set(validId, { owner: userId, row: nextRow });
|
|
1783
|
+
assetBytesMap.set(validId, new Uint8Array(bytes));
|
|
1784
|
+
assetsEmitter.emit({
|
|
1785
|
+
kind: "updated",
|
|
1786
|
+
id: nextRow.id,
|
|
1787
|
+
row: nextRow,
|
|
1788
|
+
updatedAt: nextRow.updated_at
|
|
1789
|
+
});
|
|
1790
|
+
return normalizeHostAssetRow(nextRow, backend);
|
|
1791
|
+
});
|
|
1792
|
+
},
|
|
1793
|
+
async url(id) {
|
|
1794
|
+
return runHostOp(signal, backend, async () => {
|
|
1795
|
+
requireNamespace("storage");
|
|
1796
|
+
const validId = parseHostId(id, backend);
|
|
1797
|
+
const entry = assetsMap.get(validId);
|
|
1798
|
+
if (!entry || entry.owner !== userId)
|
|
1799
|
+
return null;
|
|
1800
|
+
if (options.assetUrlResolver) {
|
|
1801
|
+
return options.assetUrlResolver(normalizeHostAssetRow(entry.row, backend));
|
|
1802
|
+
}
|
|
1803
|
+
return `asset://${entry.row.id}`;
|
|
1804
|
+
});
|
|
1805
|
+
}
|
|
1806
|
+
},
|
|
1807
|
+
temp: {
|
|
1808
|
+
async put(key, bytes, _contentType) {
|
|
1809
|
+
return runHostOp(signal, backend, async () => {
|
|
1810
|
+
requireNamespace("storage");
|
|
1811
|
+
const validKey = parseHostId(key, backend);
|
|
1812
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
1813
|
+
throw new HostError("invalid", "Temp bytes must be a Uint8Array", backend);
|
|
1814
|
+
}
|
|
1815
|
+
const normalizedKey = normalizeTempKey(validKey);
|
|
1816
|
+
tempMap.set(normalizedKey, new Uint8Array(bytes));
|
|
1817
|
+
return `memory://${normalizedKey}`;
|
|
1818
|
+
});
|
|
1819
|
+
},
|
|
1820
|
+
async read(keyOrUri) {
|
|
1821
|
+
return runHostOp(signal, backend, async () => {
|
|
1822
|
+
requireNamespace("storage");
|
|
1823
|
+
const validKey = parseHostId(keyOrUri, backend);
|
|
1824
|
+
const bytes = tempMap.get(normalizeTempKey(validKey));
|
|
1825
|
+
return bytes ? new Uint8Array(bytes) : null;
|
|
1826
|
+
});
|
|
1827
|
+
},
|
|
1828
|
+
async delete(keyOrUri) {
|
|
1829
|
+
return runHostOp(signal, backend, async () => {
|
|
1830
|
+
requireNamespace("storage");
|
|
1831
|
+
const validKey = parseHostId(keyOrUri, backend);
|
|
1832
|
+
return tempMap.delete(normalizeTempKey(validKey));
|
|
1833
|
+
});
|
|
1834
|
+
}
|
|
1835
|
+
},
|
|
1836
|
+
cache: {
|
|
1837
|
+
async get(key) {
|
|
1838
|
+
return runHostOp(signal, backend, async () => {
|
|
1839
|
+
requireNamespace("storage");
|
|
1840
|
+
const validKey = parseHostId(key, backend);
|
|
1841
|
+
const entry = cacheMap.get(validKey);
|
|
1842
|
+
if (!entry)
|
|
1843
|
+
return null;
|
|
1844
|
+
if (entry.expiresAt !== null && Date.now() > entry.expiresAt) {
|
|
1845
|
+
cacheMap.delete(validKey);
|
|
1846
|
+
return null;
|
|
1847
|
+
}
|
|
1848
|
+
// SAFETY: Cache values are stored as JSON strings and returned to
|
|
1849
|
+
// the caller under the key's expected type `T`.
|
|
1850
|
+
return JSON.parse(entry.json);
|
|
1851
|
+
});
|
|
1852
|
+
},
|
|
1853
|
+
async set(key, value, ttlMs) {
|
|
1854
|
+
return runHostOp(signal, backend, async () => {
|
|
1855
|
+
requireNamespace("storage");
|
|
1856
|
+
const validKey = parseHostId(key, backend);
|
|
1857
|
+
if (ttlMs !== undefined &&
|
|
1858
|
+
(!Number.isFinite(ttlMs) || ttlMs <= 0)) {
|
|
1859
|
+
throw new HostError("invalid", "Cache ttlMs must be a positive finite number", backend);
|
|
1860
|
+
}
|
|
1861
|
+
if (value === undefined) {
|
|
1862
|
+
throw new HostError("invalid", "Cache value must be JSON-serializable", backend);
|
|
1863
|
+
}
|
|
1864
|
+
const json = JSON.stringify(toPlainJson(value));
|
|
1865
|
+
const expiresAt = ttlMs !== undefined ? Date.now() + ttlMs : null;
|
|
1866
|
+
cacheMap.set(validKey, { json, expiresAt });
|
|
1867
|
+
});
|
|
1868
|
+
},
|
|
1869
|
+
async delete(key) {
|
|
1870
|
+
return runHostOp(signal, backend, async () => {
|
|
1871
|
+
requireNamespace("storage");
|
|
1872
|
+
const validKey = parseHostId(key, backend);
|
|
1873
|
+
const entry = cacheMap.get(validKey);
|
|
1874
|
+
if (!entry)
|
|
1875
|
+
return false;
|
|
1876
|
+
cacheMap.delete(validKey);
|
|
1877
|
+
if (entry.expiresAt !== null && Date.now() > entry.expiresAt) {
|
|
1878
|
+
return false;
|
|
1879
|
+
}
|
|
1880
|
+
return true;
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
},
|
|
1885
|
+
secrets: {
|
|
1886
|
+
async get(key) {
|
|
1887
|
+
return runHostOp(signal, backend, async () => {
|
|
1888
|
+
requireNamespace("secrets");
|
|
1889
|
+
const validKey = parseHostId(key, backend);
|
|
1890
|
+
return secretsMap.get(validKey) ?? null;
|
|
1891
|
+
});
|
|
1892
|
+
},
|
|
1893
|
+
async setting(key) {
|
|
1894
|
+
return runHostOp(signal, backend, async () => {
|
|
1895
|
+
requireNamespace("secrets");
|
|
1896
|
+
const validKey = parseHostId(key, backend);
|
|
1897
|
+
return readSettingRow(validKey)?.value ?? null;
|
|
1898
|
+
});
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
};
|
|
1902
|
+
}
|
|
1903
|
+
//# sourceMappingURL=fake-host.js.map
|