The best way to control Claude Code, Codex and more from any computer or phone

main
ts 218 lines 7.5 KB
Raw
1 import * as mediaconvert from "@distilled.cloud/aws/mediaconvert";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import type { Providers } from "../Providers.ts";
11 import { readMcTags, syncMcTags } from "./internal.ts";
12
13 export interface JobTemplateProps {
14 /**
15 * Name of the job template. Must be unique within the account/region and
16 * match `^[\w-]+$`. If omitted, a unique name is generated. Changing the
17 * name replaces the job template.
18 */
19 jobTemplateName?: string;
20 /**
21 * Optional description of the job template.
22 */
23 description?: string;
24 /**
25 * Optional category used to group job templates in the MediaConvert console.
26 */
27 category?: string;
28 /**
29 * Name of the queue jobs created from this template are submitted to. If
30 * omitted, the account's `Default` queue is used.
31 */
32 queue?: string;
33 /**
34 * Relative priority (-50 to 50) of jobs created from this template.
35 */
36 priority?: number;
37 /**
38 * The transcode settings — input template(s), output groups, timecode
39 * config, and so on — applied to jobs created from this template. Required.
40 */
41 settings: mediaconvert.JobTemplateSettings;
42 /**
43 * Accelerated transcoding configuration.
44 */
45 accelerationSettings?: mediaconvert.AccelerationSettings;
46 /**
47 * Optional destination queues jobs hop to if they wait too long.
48 */
49 hopDestinations?: mediaconvert.HopDestination[];
50 /**
51 * How often, in seconds, MediaConvert emits `STATUS_UPDATE` events for jobs
52 * created from this template.
53 */
54 statusUpdateInterval?: mediaconvert.StatusUpdateInterval;
55 /**
56 * User-defined tags for the job template.
57 */
58 tags?: Record<string, string>;
59 }
60
61 export interface JobTemplate extends Resource<
62 "AWS.MediaConvert.JobTemplate",
63 JobTemplateProps,
64 {
65 jobTemplateName: string;
66 jobTemplateArn: string;
67 type: string | undefined;
68 category: string | undefined;
69 },
70 never,
71 Providers
72 > {}
73
74 /**
75 * An AWS Elemental MediaConvert job template — a reusable, named transcode
76 * configuration (inputs, output groups, and job-level settings) that new jobs
77 * are created from so callers only supply the input/output specifics.
78 *
79 * ### Creating a Job Template
80 * **Example:** MP4 File-Group Template
81 * ```typescript
82 * const template = yield* MediaConvert.JobTemplate("Mp4", {
83 * description: "Single MP4 output",
84 * settings: {
85 * Inputs: [{ TimecodeSource: "ZEROBASED" }],
86 * OutputGroups: [
87 * {
88 * OutputGroupSettings: {
89 * Type: "FILE_GROUP_SETTINGS",
90 * FileGroupSettings: {},
91 * },
92 * Outputs: [
93 * {
94 * ContainerSettings: { Container: "MP4" },
95 * VideoDescription: {
96 * CodecSettings: {
97 * Codec: "H_264",
98 * H264Settings: {
99 * RateControlMode: "QVBR",
100 * MaxBitrate: 5000000,
101 * },
102 * },
103 * },
104 * },
105 * ],
106 * },
107 * ],
108 * },
109 * });
110 * ```
111 *
112 * @resource
113 */
114 export const JobTemplate = Resource<JobTemplate>("AWS.MediaConvert.JobTemplate");
115
116 export const JobTemplateProvider = () =>
117 Provider.effect(
118 JobTemplate,
119 Effect.gen(function* () {
120 const createName = Effect.fn(function* (id: string, props: JobTemplateProps) {
121 return props.jobTemplateName ?? (yield* createPhysicalName({ id, maxLength: 128 }));
122 });
123
124 const toAttrs = (template: mediaconvert.JobTemplate & { Name: string }) => ({
125 jobTemplateName: template.Name,
126 jobTemplateArn: template.Arn!,
127 type: template.Type,
128 category: template.Category,
129 });
130
131 /** Get a job template by name; typed not-found → undefined. */
132 const getJobTemplate = Effect.fn(function* (name: string) {
133 const response = yield* mediaconvert
134 .getJobTemplate({ Name: name })
135 .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
136 return response?.JobTemplate;
137 });
138
139 return {
140 stables: ["jobTemplateName", "jobTemplateArn"],
141
142 diff: Effect.fn(function* ({ id, olds, news }) {
143 if (!isResolved(news)) return undefined;
144 const oldName = yield* createName(id, olds);
145 const newName = yield* createName(id, news);
146 if (oldName !== newName) return { action: "replace" } as const;
147 }),
148
149 read: Effect.fn(function* ({ id, olds, output }) {
150 const name = output?.jobTemplateName ?? (yield* createName(id, olds ?? {}));
151 const template = yield* getJobTemplate(name);
152 if (template === undefined) return undefined;
153 const attrs = toAttrs(template);
154 const tags = yield* readMcTags(template.Arn!);
155 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
156 }),
157
158 reconcile: Effect.fn(function* ({ id, news, output, session }) {
159 const internalTags = yield* createInternalTags(id);
160 const desiredTags = { ...internalTags, ...news.tags };
161 const name = output?.jobTemplateName ?? (yield* createName(id, news));
162
163 // 1. Observe — cloud state is authoritative.
164 let template = yield* getJobTemplate(name);
165
166 // 2. Ensure — create if missing.
167 if (template === undefined) {
168 const created = yield* mediaconvert.createJobTemplate({
169 Name: name,
170 Description: news.description,
171 Category: news.category,
172 Queue: news.queue,
173 Priority: news.priority,
174 Settings: news.settings,
175 AccelerationSettings: news.accelerationSettings,
176 HopDestinations: news.hopDestinations,
177 StatusUpdateInterval: news.statusUpdateInterval,
178 Tags: desiredTags,
179 });
180 template = created.JobTemplate!;
181 } else {
182 // 3. Sync — UpdateJobTemplate re-applies the mutable fields.
183 const updated = yield* mediaconvert.updateJobTemplate({
184 Name: name,
185 Description: news.description,
186 Category: news.category,
187 Queue: news.queue,
188 Priority: news.priority,
189 Settings: news.settings,
190 AccelerationSettings: news.accelerationSettings,
191 HopDestinations: news.hopDestinations,
192 StatusUpdateInterval: news.statusUpdateInterval,
193 });
194 template = updated.JobTemplate!;
195 }
196
197 // 3b. Sync tags — diff against OBSERVED cloud tags.
198 yield* syncMcTags(template.Arn!, desiredTags);
199
200 yield* session.note(name);
201 return toAttrs(template as mediaconvert.JobTemplate & { Name: string });
202 }),
203
204 delete: Effect.fn(function* ({ output }) {
205 yield* mediaconvert
206 .deleteJobTemplate({ Name: output.jobTemplateName })
207 .pipe(Effect.catchTag("NotFoundException", () => Effect.void));
208 }),
209
210 list: () =>
211 mediaconvert.listJobTemplates.pages({}).pipe(
212 Stream.runCollect,
213 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.JobTemplates ?? [])),
214 Effect.map((templates) => templates.map(toAttrs)),
215 ),
216 };
217 }),
218 );