The best way to control Claude Code, Codex and more from any computer or phone
| 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 PresetProps { |
| 14 | /** |
| 15 | * Name of the preset. Must be unique within the account/region and match |
| 16 | * `^[\w-]+$`. If omitted, a unique name is generated. Changing the name |
| 17 | * replaces the preset. |
| 18 | */ |
| 19 | presetName?: string; |
| 20 | /** |
| 21 | * Optional description of the preset. |
| 22 | */ |
| 23 | description?: string; |
| 24 | /** |
| 25 | * Optional category used to group presets in the MediaConvert console. |
| 26 | */ |
| 27 | category?: string; |
| 28 | /** |
| 29 | * The transcode output settings this preset applies — audio descriptions, |
| 30 | * caption descriptions, container settings, and the video description |
| 31 | * (codec, resolution, bitrate, …). Required. |
| 32 | */ |
| 33 | settings: mediaconvert.PresetSettings; |
| 34 | /** |
| 35 | * User-defined tags for the preset. |
| 36 | */ |
| 37 | tags?: Record<string, string>; |
| 38 | } |
| 39 | |
| 40 | export interface Preset extends Resource< |
| 41 | "AWS.MediaConvert.Preset", |
| 42 | PresetProps, |
| 43 | { |
| 44 | presetName: string; |
| 45 | presetArn: string; |
| 46 | type: string | undefined; |
| 47 | category: string | undefined; |
| 48 | }, |
| 49 | never, |
| 50 | Providers |
| 51 | > {} |
| 52 | |
| 53 | /** |
| 54 | * An AWS Elemental MediaConvert output preset — a reusable, named bundle of |
| 55 | * output settings (container, video codec/resolution/bitrate, audio, and |
| 56 | * captions) that job templates and jobs reference to produce one output. |
| 57 | * |
| 58 | * ### Creating a Preset |
| 59 | * **Example:** MP4 / H.264 Preset |
| 60 | * ```typescript |
| 61 | * const preset = yield* MediaConvert.Preset("Mp4", { |
| 62 | * description: "1080p H.264 MP4", |
| 63 | * settings: { |
| 64 | * ContainerSettings: { Container: "MP4" }, |
| 65 | * VideoDescription: { |
| 66 | * Width: 1920, |
| 67 | * Height: 1080, |
| 68 | * CodecSettings: { |
| 69 | * Codec: "H_264", |
| 70 | * H264Settings: { RateControlMode: "QVBR", MaxBitrate: 5000000 }, |
| 71 | * }, |
| 72 | * }, |
| 73 | * AudioDescriptions: [ |
| 74 | * { |
| 75 | * CodecSettings: { |
| 76 | * Codec: "AAC", |
| 77 | * AacSettings: { |
| 78 | * Bitrate: 96000, |
| 79 | * CodingMode: "CODING_MODE_2_0", |
| 80 | * SampleRate: 48000, |
| 81 | * }, |
| 82 | * }, |
| 83 | * }, |
| 84 | * ], |
| 85 | * }, |
| 86 | * }); |
| 87 | * ``` |
| 88 | * |
| 89 | * @resource |
| 90 | */ |
| 91 | export const Preset = Resource<Preset>("AWS.MediaConvert.Preset"); |
| 92 | |
| 93 | export const PresetProvider = () => |
| 94 | Provider.effect( |
| 95 | Preset, |
| 96 | Effect.gen(function* () { |
| 97 | const createName = Effect.fn(function* (id: string, props: PresetProps) { |
| 98 | return props.presetName ?? (yield* createPhysicalName({ id, maxLength: 128 })); |
| 99 | }); |
| 100 | |
| 101 | const toAttrs = (preset: mediaconvert.Preset & { Name: string }) => ({ |
| 102 | presetName: preset.Name, |
| 103 | presetArn: preset.Arn!, |
| 104 | type: preset.Type, |
| 105 | category: preset.Category, |
| 106 | }); |
| 107 | |
| 108 | /** Get a preset by name; typed not-found → undefined. */ |
| 109 | const getPreset = Effect.fn(function* (name: string) { |
| 110 | const response = yield* mediaconvert |
| 111 | .getPreset({ Name: name }) |
| 112 | .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined)); |
| 113 | return response?.Preset; |
| 114 | }); |
| 115 | |
| 116 | return { |
| 117 | stables: ["presetName", "presetArn"], |
| 118 | |
| 119 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 120 | if (!isResolved(news)) return undefined; |
| 121 | const oldName = yield* createName(id, olds); |
| 122 | const newName = yield* createName(id, news); |
| 123 | if (oldName !== newName) return { action: "replace" } as const; |
| 124 | }), |
| 125 | |
| 126 | read: Effect.fn(function* ({ id, olds, output }) { |
| 127 | const name = output?.presetName ?? (yield* createName(id, olds ?? {})); |
| 128 | const preset = yield* getPreset(name); |
| 129 | if (preset === undefined) return undefined; |
| 130 | const attrs = toAttrs(preset); |
| 131 | const tags = yield* readMcTags(preset.Arn!); |
| 132 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 133 | }), |
| 134 | |
| 135 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 136 | const internalTags = yield* createInternalTags(id); |
| 137 | const desiredTags = { ...internalTags, ...news.tags }; |
| 138 | const name = output?.presetName ?? (yield* createName(id, news)); |
| 139 | |
| 140 | // 1. Observe — cloud state is authoritative. |
| 141 | let preset = yield* getPreset(name); |
| 142 | |
| 143 | // 2. Ensure — create if missing. |
| 144 | if (preset === undefined) { |
| 145 | const created = yield* mediaconvert.createPreset({ |
| 146 | Name: name, |
| 147 | Description: news.description, |
| 148 | Category: news.category, |
| 149 | Settings: news.settings, |
| 150 | Tags: desiredTags, |
| 151 | }); |
| 152 | preset = created.Preset!; |
| 153 | } else { |
| 154 | // 3. Sync — UpdatePreset re-applies the mutable fields in place. |
| 155 | const updated = yield* mediaconvert.updatePreset({ |
| 156 | Name: name, |
| 157 | Description: news.description, |
| 158 | Category: news.category, |
| 159 | Settings: news.settings, |
| 160 | }); |
| 161 | preset = updated.Preset!; |
| 162 | } |
| 163 | |
| 164 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 165 | yield* syncMcTags(preset.Arn!, desiredTags); |
| 166 | |
| 167 | yield* session.note(name); |
| 168 | return toAttrs(preset as mediaconvert.Preset & { Name: string }); |
| 169 | }), |
| 170 | |
| 171 | delete: Effect.fn(function* ({ output }) { |
| 172 | yield* mediaconvert |
| 173 | .deletePreset({ Name: output.presetName }) |
| 174 | .pipe(Effect.catchTag("NotFoundException", () => Effect.void)); |
| 175 | }), |
| 176 | |
| 177 | list: () => |
| 178 | mediaconvert.listPresets.pages({}).pipe( |
| 179 | Stream.runCollect, |
| 180 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Presets ?? [])), |
| 181 | Effect.map((presets) => presets.map(toAttrs)), |
| 182 | ), |
| 183 | }; |
| 184 | }), |
| 185 | ); |