Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add isolated workflow Effect runtime

Richie McIlroyb3242e266585
diff --git a/apps/web/__tests__/unit/workflow-runtime-boundary.test.ts b/apps/web/__tests__/unit/workflow-runtime-boundary.test.ts
new file mode 100644
index 000000000..cc9afdcdb
--- /dev/null
+++ b/apps/web/__tests__/unit/workflow-runtime-boundary.test.ts
@@ -0,0 +1,97 @@
+import { readFile } from "node:fs/promises";
+import { dirname, join, relative, resolve } from "node:path";
+import { describe, expect, it } from "vitest";
+
+const workflowEntries = [
+ "workflows/process-video.ts",
+ "workflows/finalize-desktop-recording.ts",
+ "workflows/transcribe.ts",
+ "workflows/generate-ai.ts",
+ "workflows/edit-video.ts",
+ "workflows/admin-reprocess-video.ts",
+ "workflows/import-loom-video.ts",
+ "workflows/agent-cap-operation.ts",
+];
+
+const forbiddenModules = new Set([
+ "@cap/utils",
+ "@cap/web-backend",
+ "@/lib/server",
+]);
+
+const importPattern = /(?:from\s+|import\s*)["']([^"']+)["']/g;
+
+async function resolveSourceFile(path: string) {
+ for (const candidate of [
+ path,
+ `${path}.ts`,
+ `${path}.tsx`,
+ join(path, "index.ts"),
+ ]) {
+ try {
+ await readFile(candidate, "utf8");
+ return candidate;
+ } catch {}
+ }
+
+ return null;
+}
+
+async function resolveImport(from: string, specifier: string) {
+ if (specifier.startsWith("@/")) {
+ return resolveSourceFile(join(process.cwd(), specifier.slice(2)));
+ }
+
+ if (specifier.startsWith("@cap/web-backend/src/")) {
+ return resolveSourceFile(
+ join(
+ process.cwd(),
+ "../../packages/web-backend/src",
+ specifier.slice("@cap/web-backend/src/".length),
+ ),
+ );
+ }
+
+ if (specifier.startsWith(".")) {
+ return resolveSourceFile(resolve(dirname(from), specifier));
+ }
+
+ return null;
+}
+
+async function findForbiddenImports(entry: string) {
+ const pending = [join(process.cwd(), entry)];
+ const visited = new Set<string>();
+ const violations: string[] = [];
+
+ while (pending.length > 0) {
+ const file = pending.pop();
+ if (!file || visited.has(file)) continue;
+ visited.add(file);
+
+ const source = await readFile(file, "utf8");
+ for (const match of source.matchAll(importPattern)) {
+ const specifier = match[1];
+ if (!specifier) continue;
+
+ if (forbiddenModules.has(specifier)) {
+ violations.push(`${relative(process.cwd(), file)} -> ${specifier}`);
+ continue;
+ }
+
+ const dependency = await resolveImport(file, specifier);
+ if (dependency) pending.push(dependency);
+ }
+ }
+
+ return violations;
+}
+
+describe("customer video workflow runtime boundary", () => {
+ it.each(workflowEntries)(
+ "keeps %s out of the full web runtime",
+ async (entry) => {
+ expect(await findForbiddenImports(entry)).toEqual([]);
+ },
+ );
+});
diff --git a/apps/web/lib/workflow-runtime.ts b/apps/web/lib/workflow-runtime.ts
new file mode 100644
index 000000000..61a10c47f
--- /dev/null
+++ b/apps/web/lib/workflow-runtime.ts
@@ -0,0 +1,24 @@
+import { AwsCredentials } from "@cap/web-backend/src/Aws";
+import { Organisations } from "@cap/web-backend/src/Organisations/index";
+import { Storage } from "@cap/web-backend/src/Storage/index";
+import { Cause, type Effect, Exit, Layer, ManagedRuntime } from "effect";
+
+const WorkflowDependencies = Layer.mergeAll(
+ Storage.Default,
+ AwsCredentials.Default,
+ Organisations.Default,
+);
+
+const WorkflowRuntime = ManagedRuntime.make(WorkflowDependencies);
+
+export const runWorkflowPromise = <A, E>(
+ effect: Effect.Effect<A, E, Layer.Layer.Success<typeof WorkflowDependencies>>,
+) =>
+ WorkflowRuntime.runPromiseExit(effect).then((result) => {
+ if (Exit.isFailure(result)) {
+ if (Cause.isDieType(result.cause)) throw result.cause.defect;
+ throw result;
+ }
+
+ return result.value;
+ });