@mevdragon/vidfarm-devcli 0.6.0 → 0.7.0
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/SKILL.director.md +28 -7
- package/SKILL.platform.md +9 -5
- package/demo/dist/app.js +59 -59
- package/dist/src/app.js +332 -47
- package/dist/src/cli.js +59 -96
- package/dist/src/config.js +7 -0
- package/dist/src/editor-chat.js +4 -4
- package/dist/src/primitive-registry.js +125 -0
- package/dist/src/services/upstream.js +248 -0
- package/package.json +1 -1
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { config } from "../config.js";
|
|
2
|
+
import { StorageService } from "./storage.js";
|
|
3
|
+
import { serverlessRecords } from "./serverless-records.js";
|
|
4
|
+
// Cloud passthrough for `vidfarm serve`. A locally-booted box keeps the
|
|
5
|
+
// editor, storage, and render fully local, but mirrors the upstream (prod)
|
|
6
|
+
// deployment for everything catalog-shaped: /discover and /library list the
|
|
7
|
+
// cloud data, templates/forks seed onto local disk on demand, and a render
|
|
8
|
+
// can be handed off to the cloud renderer. The upstream host + API key are
|
|
9
|
+
// injected by the serve command (VIDFARM_UPSTREAM_HOST / _API_KEY); deployed
|
|
10
|
+
// environments never set them, so every helper here is a no-op in the cloud.
|
|
11
|
+
const UPSTREAM_TIMEOUT_MS = 15_000;
|
|
12
|
+
export function upstreamHost() {
|
|
13
|
+
const raw = (config.VIDFARM_UPSTREAM_HOST ?? "").trim().replace(/\/+$/, "");
|
|
14
|
+
if (!raw)
|
|
15
|
+
return null;
|
|
16
|
+
if (!/^https?:\/\//i.test(raw))
|
|
17
|
+
return null;
|
|
18
|
+
return raw;
|
|
19
|
+
}
|
|
20
|
+
// Passthrough only ever activates on a fully-local box. STORAGE_DRIVER=local
|
|
21
|
+
// is the serve fingerprint (same signal the editor's liveReload uses).
|
|
22
|
+
export function isUpstreamEnabled() {
|
|
23
|
+
return config.STORAGE_DRIVER === "local" && Boolean(upstreamHost());
|
|
24
|
+
}
|
|
25
|
+
export function upstreamApiKey() {
|
|
26
|
+
const key = (config.VIDFARM_UPSTREAM_API_KEY ?? "").trim();
|
|
27
|
+
return key || null;
|
|
28
|
+
}
|
|
29
|
+
export function hasUpstreamKey() {
|
|
30
|
+
return isUpstreamEnabled() && Boolean(upstreamApiKey());
|
|
31
|
+
}
|
|
32
|
+
export function upstreamAuthHeaders(extra) {
|
|
33
|
+
const headers = { accept: "application/json", ...(extra ?? {}) };
|
|
34
|
+
const key = upstreamApiKey();
|
|
35
|
+
if (key)
|
|
36
|
+
headers["vidfarm-api-key"] = key;
|
|
37
|
+
return headers;
|
|
38
|
+
}
|
|
39
|
+
// One-shot JSON fetch against the upstream host. Never throws on HTTP or
|
|
40
|
+
// network errors — callers treat a failed upstream as "no cloud data" and
|
|
41
|
+
// fall back to local-only behavior.
|
|
42
|
+
export async function upstreamJson(pathWithQuery, init) {
|
|
43
|
+
const host = upstreamHost();
|
|
44
|
+
if (!host)
|
|
45
|
+
return { ok: false, status: 0, json: null };
|
|
46
|
+
try {
|
|
47
|
+
const response = await fetch(`${host}${pathWithQuery}`, {
|
|
48
|
+
method: init?.method ?? "GET",
|
|
49
|
+
headers: upstreamAuthHeaders(init?.body !== undefined ? { "content-type": "application/json" } : undefined),
|
|
50
|
+
body: init?.body !== undefined ? JSON.stringify(init.body) : undefined,
|
|
51
|
+
signal: AbortSignal.timeout(init?.timeoutMs ?? UPSTREAM_TIMEOUT_MS)
|
|
52
|
+
});
|
|
53
|
+
const json = await response.json().catch(() => null);
|
|
54
|
+
return { ok: response.ok, status: response.status, json };
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.warn(`[upstream] ${init?.method ?? "GET"} ${pathWithQuery} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
58
|
+
return { ok: false, status: 0, json: null };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Forward the incoming request to the same path on the upstream host with the
|
|
62
|
+
// upstream API key swapped in for the local session. Used as a fallback when
|
|
63
|
+
// a resource id (approved post, inspiration) doesn't resolve locally — the id
|
|
64
|
+
// then almost certainly belongs to the cloud account this box mirrors.
|
|
65
|
+
// requireKey defaults to true so authed reads/mutations never proxy without
|
|
66
|
+
// cloud credentials; public-data GET fallbacks pass requireKey: false.
|
|
67
|
+
export async function proxyRequestToUpstream(c, options) {
|
|
68
|
+
const host = upstreamHost();
|
|
69
|
+
if (!host || !isUpstreamEnabled())
|
|
70
|
+
return null;
|
|
71
|
+
if ((options?.requireKey ?? true) && !hasUpstreamKey())
|
|
72
|
+
return null;
|
|
73
|
+
const incoming = new URL(c.req.url);
|
|
74
|
+
const target = `${host}${incoming.pathname}${incoming.search}`;
|
|
75
|
+
const method = c.req.method.toUpperCase();
|
|
76
|
+
const headers = upstreamAuthHeaders();
|
|
77
|
+
let body;
|
|
78
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
79
|
+
body = await c.req.text().catch(() => "");
|
|
80
|
+
const contentType = c.req.header("content-type");
|
|
81
|
+
if (contentType)
|
|
82
|
+
headers["content-type"] = contentType;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const response = await fetch(target, {
|
|
86
|
+
method,
|
|
87
|
+
headers,
|
|
88
|
+
body,
|
|
89
|
+
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS)
|
|
90
|
+
});
|
|
91
|
+
const payload = Buffer.from(await response.arrayBuffer());
|
|
92
|
+
return new Response(payload, {
|
|
93
|
+
status: response.status,
|
|
94
|
+
headers: {
|
|
95
|
+
"content-type": response.headers.get("content-type") ?? "application/json",
|
|
96
|
+
"x-vidfarm-upstream": host
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
console.warn(`[upstream] proxy ${method} ${incoming.pathname} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
// On-demand seeding: pull a cloud fork's composition onto local disk and mint
|
|
107
|
+
// the matching local template + fork records so the local editor resolves it.
|
|
108
|
+
// Same behavior as `vidfarm serve <templateId>` boot seeding, but callable
|
|
109
|
+
// from the /editor route when the user clicks a proxied catalog entry.
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
function seedAuthHeaders(shareToken) {
|
|
112
|
+
const headers = { accept: "text/html,application/json" };
|
|
113
|
+
const key = upstreamApiKey();
|
|
114
|
+
if (key)
|
|
115
|
+
headers["vidfarm-api-key"] = key;
|
|
116
|
+
if (shareToken)
|
|
117
|
+
headers["vidfarm-share-token"] = shareToken;
|
|
118
|
+
return headers;
|
|
119
|
+
}
|
|
120
|
+
// /editor/<template_id> issues a 302 → /editor/<template_id>?fork=<id>
|
|
121
|
+
// whenever the caller (or the template's default fork) has one. Follow the
|
|
122
|
+
// redirect once by hand so we can read the fork id off the query string.
|
|
123
|
+
async function resolveUpstreamForkId(input) {
|
|
124
|
+
try {
|
|
125
|
+
const res = await fetch(`${input.host}/editor/${encodeURIComponent(input.templateId)}`, {
|
|
126
|
+
method: "GET",
|
|
127
|
+
redirect: "manual",
|
|
128
|
+
headers: seedAuthHeaders(input.shareToken),
|
|
129
|
+
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS)
|
|
130
|
+
});
|
|
131
|
+
if (res.status >= 300 && res.status < 400) {
|
|
132
|
+
const location = res.headers.get("location");
|
|
133
|
+
if (location) {
|
|
134
|
+
const target = new URL(location, input.host);
|
|
135
|
+
const fork = target.searchParams.get("fork");
|
|
136
|
+
if (fork)
|
|
137
|
+
return fork;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// fall through to null — caller starts empty
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
// Best-effort: returns null (and logs) instead of throwing so callers can
|
|
147
|
+
// fall back to their template-not-found behavior.
|
|
148
|
+
export async function seedFromUpstream(input) {
|
|
149
|
+
const host = upstreamHost();
|
|
150
|
+
if (!host || !isUpstreamEnabled())
|
|
151
|
+
return null;
|
|
152
|
+
const storage = new StorageService();
|
|
153
|
+
const headers = seedAuthHeaders(input.shareToken);
|
|
154
|
+
// 1. Resolve the source fork: explicit fork wins, else the template's
|
|
155
|
+
// default fork (grabs the shared decomposition even if another user owns it).
|
|
156
|
+
let cloudForkId = input.forkId ?? null;
|
|
157
|
+
if (!cloudForkId && input.templateId) {
|
|
158
|
+
cloudForkId = await resolveUpstreamForkId({ host, templateId: input.templateId, shareToken: input.shareToken });
|
|
159
|
+
}
|
|
160
|
+
if (!cloudForkId) {
|
|
161
|
+
console.warn(`[upstream] seed: no fork resolvable for template ${input.templateId ?? "<none>"}`);
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
// 2. Read the serialized fork for its template id + title.
|
|
165
|
+
const forkMeta = (await upstreamJson(`/api/v1/compositions/${encodeURIComponent(cloudForkId)}`)).json;
|
|
166
|
+
const templateId = input.templateId ?? (typeof forkMeta?.template_id === "string" ? forkMeta.template_id : null);
|
|
167
|
+
if (!templateId || !templateId.startsWith("template_")) {
|
|
168
|
+
console.warn(`[upstream] seed: could not determine a template id for fork ${cloudForkId}`);
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const title = (typeof forkMeta?.title === "string" && forkMeta.title) || `Local · ${templateId}`;
|
|
172
|
+
// 3. Materialize the local template record (the editor 404s without it).
|
|
173
|
+
const existingTemplate = await serverlessRecords.getTemplate(templateId);
|
|
174
|
+
if (!existingTemplate) {
|
|
175
|
+
await serverlessRecords.createTemplate({
|
|
176
|
+
id: templateId,
|
|
177
|
+
inspirationId: `local:${templateId}`,
|
|
178
|
+
customerId: input.customerId,
|
|
179
|
+
originalUrl: typeof forkMeta?.original_url === "string" ? forkMeta.original_url : "",
|
|
180
|
+
sourceHost: "local",
|
|
181
|
+
title,
|
|
182
|
+
visibility: "public"
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// 4. Materialize the local fork (reuse the cloud id for stable URLs — the
|
|
186
|
+
// shared id is also what lets a later "Render in Cloud" find its source).
|
|
187
|
+
const existingFork = await serverlessRecords.getCompositionFork(cloudForkId);
|
|
188
|
+
if (!existingFork) {
|
|
189
|
+
await serverlessRecords.createCompositionFork({
|
|
190
|
+
id: cloudForkId,
|
|
191
|
+
customerId: input.customerId,
|
|
192
|
+
templateId,
|
|
193
|
+
title,
|
|
194
|
+
visibility: "private"
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
// 5. Pull composition.html/json onto disk (skip if present unless refetch,
|
|
198
|
+
// so local edits aren't clobbered).
|
|
199
|
+
const htmlKey = storage.compositionForkWorkingKey(cloudForkId, "composition.html");
|
|
200
|
+
const jsonKey = storage.compositionForkWorkingKey(cloudForkId, "composition.json");
|
|
201
|
+
const hasLocalHtml = Boolean(await storage.readText(htmlKey));
|
|
202
|
+
if (!hasLocalHtml || input.refetch) {
|
|
203
|
+
try {
|
|
204
|
+
const htmlRes = await fetch(`${host}/api/v1/compositions/${encodeURIComponent(cloudForkId)}/composition.html`, {
|
|
205
|
+
headers,
|
|
206
|
+
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS)
|
|
207
|
+
});
|
|
208
|
+
if (!htmlRes.ok) {
|
|
209
|
+
console.warn(`[upstream] seed: could not fetch composition.html for ${cloudForkId} (${htmlRes.status})`);
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
const compositionHtml = await htmlRes.text();
|
|
213
|
+
await storage.putText(htmlKey, compositionHtml, "text/html; charset=utf-8");
|
|
214
|
+
// Backfill the local template's preview metadata from the composition
|
|
215
|
+
// itself (no single-template REST read exists) so the seeded copy also
|
|
216
|
+
// renders a card on the LOCAL /discover feed.
|
|
217
|
+
const sourceVideoMatch = compositionHtml.match(/data-source-video="([^"]+)"/i);
|
|
218
|
+
const durationMatch = compositionHtml.match(/data-duration="([^"]+)"/i);
|
|
219
|
+
const template = await serverlessRecords.getTemplate(templateId);
|
|
220
|
+
if (template && !template.videoUrl && sourceVideoMatch?.[1]) {
|
|
221
|
+
const durationSeconds = durationMatch ? Number.parseFloat(durationMatch[1]) : NaN;
|
|
222
|
+
await serverlessRecords.updateTemplate({
|
|
223
|
+
templateId,
|
|
224
|
+
patch: {
|
|
225
|
+
videoUrl: sourceVideoMatch[1].replace(/&/g, "&"),
|
|
226
|
+
...(Number.isFinite(durationSeconds) && durationSeconds > 0 ? { durationSeconds } : {})
|
|
227
|
+
}
|
|
228
|
+
}).catch(() => undefined);
|
|
229
|
+
}
|
|
230
|
+
const jsonRes = await fetch(`${host}/api/v1/compositions/${encodeURIComponent(cloudForkId)}/composition.json`, {
|
|
231
|
+
headers,
|
|
232
|
+
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS)
|
|
233
|
+
});
|
|
234
|
+
if (jsonRes.ok)
|
|
235
|
+
await storage.putBuffer(jsonKey, Buffer.from(await jsonRes.text(), "utf8"), "application/json");
|
|
236
|
+
console.log(`[upstream] seeded fork ${cloudForkId} from ${host}`);
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
console.warn(`[upstream] seed: composition fetch failed for ${cloudForkId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// 6. Point the template's default fork at the local copy so a bare
|
|
244
|
+
// /editor/:templateId resolves it too.
|
|
245
|
+
await serverlessRecords.stampDefaultForkIfUnset({ templateId, forkId: cloudForkId });
|
|
246
|
+
return { templateId, forkId: cloudForkId };
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=upstream.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mevdragon/vidfarm-devcli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Local bridge for the Vidfarm Trackpad Editor. `vidfarm serve <template_id>` boots the FULL editor on localhost (disk-backed records/storage, free in-process render); edit composition.html on disk (Claude Code, Codex, etc.) and the browser live-morphs it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|