@mevdragon/vidfarm-devcli 0.1.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.
@@ -0,0 +1,198 @@
1
+ import { config } from "./config.js";
2
+ import { database } from "./db.js";
3
+ import { createId } from "./lib/ids.js";
4
+ export function createTemplateJobContext(input) {
5
+ const templateConfig = database.getTemplateConfig(input.customer.id, input.template.id);
6
+ const prefix = `templates/${input.template.id}/users/${input.customer.id}/jobs/${input.job.id}`;
7
+ return {
8
+ env: config.isProduction ? "production" : "development",
9
+ customer: input.customer,
10
+ templateConfig,
11
+ logger: {
12
+ debug(message, metadata = {}) {
13
+ database.addJobEvent({
14
+ id: createId("evt"),
15
+ jobId: input.job.id,
16
+ level: "debug",
17
+ message,
18
+ metadata,
19
+ progress: null,
20
+ artifactKey: null
21
+ });
22
+ },
23
+ info(message, metadata = {}) {
24
+ database.addJobEvent({
25
+ id: createId("evt"),
26
+ jobId: input.job.id,
27
+ level: "info",
28
+ message,
29
+ metadata,
30
+ progress: null,
31
+ artifactKey: null
32
+ });
33
+ },
34
+ warn(message, metadata = {}) {
35
+ database.addJobEvent({
36
+ id: createId("evt"),
37
+ jobId: input.job.id,
38
+ level: "warn",
39
+ message,
40
+ metadata,
41
+ progress: null,
42
+ artifactKey: null
43
+ });
44
+ },
45
+ error(message, metadata = {}) {
46
+ database.addJobEvent({
47
+ id: createId("evt"),
48
+ jobId: input.job.id,
49
+ level: "error",
50
+ message,
51
+ metadata,
52
+ progress: null,
53
+ artifactKey: null
54
+ });
55
+ },
56
+ progress(progress, message, metadata = {}) {
57
+ database.addJobEvent({
58
+ id: createId("evt"),
59
+ jobId: input.job.id,
60
+ level: "info",
61
+ message,
62
+ metadata,
63
+ progress,
64
+ artifactKey: typeof metadata.artifactKey === "string" ? metadata.artifactKey : null
65
+ });
66
+ database.updateJobStatus({
67
+ id: input.job.id,
68
+ status: "running",
69
+ progress
70
+ });
71
+ }
72
+ },
73
+ jobs: {
74
+ async enqueueChild({ operationName, workflowName, payload, providerHint }) {
75
+ const child = await input.jobs.enqueueChildJob({
76
+ templateId: input.template.id,
77
+ operationName,
78
+ workflowName,
79
+ tracer: input.job.tracer,
80
+ payload,
81
+ customerId: input.customer.id,
82
+ parentJobId: input.job.id,
83
+ providerHint
84
+ });
85
+ return { jobId: child.id };
86
+ }
87
+ },
88
+ storage: {
89
+ async putJson(key, value) {
90
+ const stored = await input.storage.putJson(`${prefix}/${key}`, value);
91
+ insertArtifact("json", stored.key, stored.url, {});
92
+ await input.billing.record({
93
+ customerId: input.customer.id,
94
+ jobId: input.job.id,
95
+ templateId: input.template.id,
96
+ type: "storage_write",
97
+ costUsd: 0.005,
98
+ chargeUsd: 0.01,
99
+ metadata: { key: stored.key }
100
+ });
101
+ return stored;
102
+ },
103
+ async putText(key, value, contentType = "text/plain; charset=utf-8") {
104
+ const stored = await input.storage.putText(`${prefix}/${key}`, value, contentType);
105
+ insertArtifact("text", stored.key, stored.url, { contentType });
106
+ await input.billing.record({
107
+ customerId: input.customer.id,
108
+ jobId: input.job.id,
109
+ templateId: input.template.id,
110
+ type: "storage_write",
111
+ costUsd: 0.005,
112
+ chargeUsd: 0.01,
113
+ metadata: { key: stored.key }
114
+ });
115
+ return stored;
116
+ },
117
+ async putBuffer(key, value, options = {}) {
118
+ const stored = await input.storage.putBuffer(`${prefix}/${key}`, value, options.contentType);
119
+ insertArtifact(options.kind ?? "binary", stored.key, stored.url, {
120
+ ...(options.metadata ?? {}),
121
+ contentType: options.contentType ?? null
122
+ });
123
+ await input.billing.record({
124
+ customerId: input.customer.id,
125
+ jobId: input.job.id,
126
+ templateId: input.template.id,
127
+ type: "storage_write",
128
+ costUsd: 0.005,
129
+ chargeUsd: 0.01,
130
+ metadata: { key: stored.key }
131
+ });
132
+ return stored;
133
+ },
134
+ getPublicUrl(key) {
135
+ return input.storage.getPublicUrl(`${prefix}/${key}`);
136
+ }
137
+ },
138
+ billing: {
139
+ async record(record) {
140
+ await input.billing.record({
141
+ customerId: input.customer.id,
142
+ jobId: input.job.id,
143
+ templateId: input.template.id,
144
+ type: record.type,
145
+ costUsd: record.costUsd,
146
+ chargeUsd: record.chargeUsd ?? Number((record.costUsd * 2).toFixed(4)),
147
+ metadata: record.metadata
148
+ });
149
+ }
150
+ },
151
+ providers: {
152
+ async generateText(request) {
153
+ return input.providers.generateText({
154
+ customerId: input.customer.id,
155
+ jobId: input.job.id,
156
+ workerId: input.workerId,
157
+ ...request
158
+ });
159
+ },
160
+ async generateImage(request) {
161
+ return input.providers.generateImage({
162
+ customerId: input.customer.id,
163
+ jobId: input.job.id,
164
+ workerId: input.workerId,
165
+ ...request
166
+ });
167
+ },
168
+ async analyzeImageLayout(request) {
169
+ return input.providers.analyzeImageLayout({
170
+ customerId: input.customer.id,
171
+ jobId: input.job.id,
172
+ workerId: input.workerId,
173
+ ...request
174
+ });
175
+ }
176
+ },
177
+ remotion: {
178
+ async render(request) {
179
+ return input.remotion.render({
180
+ ...request,
181
+ outputKey: request.outputKey ? `${prefix}/${request.outputKey}` : undefined
182
+ });
183
+ }
184
+ }
185
+ };
186
+ function insertArtifact(kind, storageKey, publicUrl, metadata) {
187
+ database.insertArtifact({
188
+ id: createId("art"),
189
+ jobId: input.job.id,
190
+ customerId: input.customer.id,
191
+ templateId: input.template.id,
192
+ kind,
193
+ storageKey,
194
+ publicUrl,
195
+ metadata
196
+ });
197
+ }
198
+ }