The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Input } from "alchemy/Input"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import { sha256 } from "alchemy/Util/sha256"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | |
| 6 | /** |
| 7 | * Lift a script-name input (a literal, an Output, or a Config/Effect-valued |
| 8 | * name) into an Output so it can feed plan-time derivations. |
| 9 | * |
| 10 | * @internal |
| 11 | */ |
| 12 | export const asScriptNameOutput = (scriptName: Input<string>): Output.Output<string> => |
| 13 | Output.asOutput( |
| 14 | (Effect.isEffect(scriptName) ? scriptName.pipe(Effect.orDie) : scriptName) as |
| 15 | | string |
| 16 | | Output.Output<string> |
| 17 | | Effect.Effect<string>, |
| 18 | ); |
| 19 | |
| 20 | /** |
| 21 | * Derive an account-global Workflow name from a resolved host Worker name and |
| 22 | * exported class. |
| 23 | * |
| 24 | * @internal |
| 25 | */ |
| 26 | export const generateWorkflowName = Effect.fn(function* (scriptName: string, className: string) { |
| 27 | const base = `${scriptName}-${className}`.toLowerCase().replaceAll(/[^a-z0-9-]/g, "-"); |
| 28 | const hash = yield* sha256(base); |
| 29 | const suffix = `-${hash.slice(0, 8)}`; |
| 30 | // Trim trailing dashes left by mid-name truncation so the result never |
| 31 | // contains a `--` seam. |
| 32 | const head = base.slice(0, 64 - suffix.length).replace(/-+$/, ""); |
| 33 | return `${head}${suffix}`; |
| 34 | }); |
| 35 | |
| 36 | /** |
| 37 | * Derive an account-global Workflow name when the host Worker name may still |
| 38 | * be an unresolved input. |
| 39 | * |
| 40 | * @internal |
| 41 | */ |
| 42 | export const makeWorkflowName = ( |
| 43 | scriptName: Input<string>, |
| 44 | className: string, |
| 45 | ): Output.Output<string> => |
| 46 | asScriptNameOutput(scriptName).pipe( |
| 47 | Output.mapEffect((scriptName) => generateWorkflowName(scriptName, className)), |
| 48 | ); |