Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance file writing and logging functionality in desktop and web applications

konstantin-paulus65d80a5b179c
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 90d1de41..0988aaf9 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -337,8 +337,16 @@ if (app.requestSingleInstanceLock()) {
);
mainBridge.handle(MAIN_CHANNELS.FILE_WRITE_OPEN, async ({ path, exclusive }) => {
- await mkdir(dirname(path), { recursive: true });
+ // The folders the write has to make are changes to the project like any
+ // other, and the watcher must not hear about ours: a generated asset
+ // landing in a project that had no `assets/` yet would otherwise come
+ // back as an outside edit and reload the library mid-write.
+ const created = await mkdir(dirname(path), { recursive: true });
markSelfWriteAbsolute(path);
+ for (let folder = dirname(path); created !== undefined; folder = dirname(folder)) {
+ markSelfWriteAbsolute(folder);
+ if (folder === created || folder === dirname(folder)) break;
+ }
const handle = await open(path, exclusive ? "wx" : "w");
const id = randomUUID();
openWrites.set(id, { handle, path });
diff --git a/apps/web/src/lib/trpc.ts b/apps/web/src/lib/trpc.ts
index efb662a9..19721d17 100644
--- a/apps/web/src/lib/trpc.ts
+++ b/apps/web/src/lib/trpc.ts
@@ -9,6 +9,68 @@ import { showUpgradeDialog } from "@/components/upgrade-dialog";
import type { AppRouter } from "@diffusionstudio/api-contract";
+/**
+ * The procedures that reach a model. What each was called with is the first
+ * thing wanted when a generation comes back wrong, so every call writes one
+ * line before it goes out and one when it lands — in the app console, which
+ * the desktop app mirrors into its log buffer (`dapi logs`).
+ */
+const MODEL_PROCEDURES = new Set([
+ "generateImage",
+ "generateVideo",
+ "generateSound",
+ "textToSpeech",
+ "removeBackground",
+ "upscaleImage",
+ "upscaleVideo",
+ "addAudioToVideo",
+ "transcribe",
+ "analyze",
+]);
+
+/** How much of a single argument a log line carries: a prompt is worth reading whole, a data url is not. */
+const ARGUMENT_MAX = 500;
+/** How much of the whole argument object a log line carries, before the log buffer cuts it anyway. */
+const ARGUMENTS_MAX = 2000;
+
+/**
+ * The arguments of a call, as one line. Strings are cut at `ARGUMENT_MAX`,
+ * and a url keeps its path alone — the query carries the upload signature,
+ * which is long and not worth writing down.
+ */
+function describeArguments(input: unknown): string {
+ const json = JSON.stringify(input, (_key, value) => {
+ if (typeof value !== "string") return value;
+ const text = /^https?:\/\//.test(value) ? value.split("?")[0]! : value;
+ return text.length > ARGUMENT_MAX ? `${text.slice(0, ARGUMENT_MAX)}…` : text;
+ });
+ if (json === undefined) return "(no arguments)";
+ return json.length > ARGUMENTS_MAX ? `${json.slice(0, ARGUMENTS_MAX)}…` : json;
+}
+
+/** Logs what every model call was asked for, and how long it took to answer. */
+const modelCallLink: TRPCLink<AppRouter> = () => ({ next, op }) =>
+ observable((observer) => {
+ const logged = MODEL_PROCEDURES.has(op.path);
+ const startedAt = performance.now();
+ const elapsed = () => `${Math.round(performance.now() - startedAt)}ms`;
+
+ if (logged) console.log(`[model] ${op.path} ${describeArguments(op.input)}`);
+
+ const sub = next(op).subscribe({
+ next: (value) => observer.next(value),
+ error: (err) => {
+ if (logged) console.error(`[model] ${op.path} failed after ${elapsed()}: ${err.message}`);
+ observer.error(err);
+ },
+ complete: () => {
+ if (logged) console.log(`[model] ${op.path} done in ${elapsed()}`);
+ observer.complete();
+ },
+ });
+ return () => sub.unsubscribe();
+ });
+
const paymentRequiredLink: TRPCLink<AppRouter> = () => ({ next, op }) =>
observable((observer) => {
const sub = next(op).subscribe({
@@ -26,6 +88,7 @@ const paymentRequiredLink: TRPCLink<AppRouter> = () => ({ next, op }) =>
export const trpc = createTRPCClient<AppRouter>({
links: [
+ modelCallLink,
paymentRequiredLink,
httpBatchLink({
url: `${import.meta.env.VITE_API_URL ?? ""}/api/trpc`,
diff --git a/apps/web/src/utils/gen-ai.ts b/apps/web/src/utils/gen-ai.ts
index 523f5a21..2828db97 100644
--- a/apps/web/src/utils/gen-ai.ts
+++ b/apps/web/src/utils/gen-ai.ts
@@ -286,7 +286,7 @@ export class EditorGenAi extends GenAi {
});
try {
- console.log(`[gen-ai] generating ${spec.type} with ${spec.model}:`, spec);
+ console.log(`[gen-ai] generating ${spec.type} with ${spec.model}`);
const { name, results, generationId } = await this.requestGeneration(spec);
assert(results.length > 0, "No results returned from the model");
diff --git a/packages/assets/src/library.ts b/packages/assets/src/library.ts
index 4a03cdc0..da60ac36 100644
--- a/packages/assets/src/library.ts
+++ b/packages/assets/src/library.ts
@@ -226,6 +226,7 @@ export class AssetLibrary {
* again. Safe to call again: it reconciles rather than replaces.
*/
public async load(): Promise<void> {
+ await this.flush();
const manifest = normalizeManifest(await this.fs.readManifest());
this.declared = new Set(manifest.folders);
@@ -450,11 +451,12 @@ export class AssetLibrary {
* another key, as a re-take of unchanged speech does.
*/
public async store(blob: Blob, options: ImportOptions & { name: string }): Promise<Asset> {
- const path = this.uniquePath(joinPath(options.folder ?? '', options.name));
+ const key = options.generation?.key;
+ const reserved = key === undefined ? undefined : this.partialFor(key);
+ const path = this.uniquePath(joinPath(options.folder ?? '', options.name), reserved);
const source = joinPath(ASSETS_DIR, path);
await this.fs.write(source, blob);
const asset = await this.describeFile(source, { path, generation: options.generation });
- const key = options.generation?.key;
if (key !== undefined) this.dropPartial(key);
const stored = this.add(asset);
if (options.generation && stored.generation?.key !== key) {
@@ -508,6 +510,11 @@ export class AssetLibrary {
this.changed();
}
+ /** The partial document standing for `key`, if one is in the library. */
+ private partialFor(key: string): PartialAsset | undefined {
+ return this.partialsNow().find((partial) => partial.generation.key === key);
+ }
+
/** Takes the partial standing for `key` out, its bytes having landed. */
private dropPartial(key: string): void {
for (const partial of this.partialsNow()) {