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

main
ts 90 lines 3.55 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Task } from "./Task.ts";
5
6 /**
7 * Shared scaffolding for AWS DataSync HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation and the IAM action is
12 * boilerplate.
13 *
14 * DataSync authorizes task-addressed actions against the *task* ARN
15 * (`arn:…:task/task-id`) and execution-addressed actions against the
16 * *task execution* ARN (`arn:…:task/task-id/execution/exec-id`), so every
17 * builder grants on both the bound task's ARN and its execution pattern.
18 */
19 const taskPolicyStatement = (task: Task, actions: readonly string[]) => ({
20 Effect: "Allow" as const,
21 Action: [...actions],
22 Resource: [
23 Output.interpolate`${task.taskArn}`,
24 Output.map(task.taskArn, (arn) => `${arn}/execution/*`),
25 ],
26 });
27
28 /**
29 * Build the impl Effect for an operation whose input carries a `TaskArn`
30 * field: the runtime callable injects the bound {@link Task}'s ARN and the
31 * deploy-time half grants `actions` on the task ARN (and its execution
32 * pattern).
33 */
34 export const makeDataSyncTaskHttpBinding = <I extends { TaskArn?: string }, A, E, R>(options: {
35 /** Fully-qualified binding tag, e.g. `AWS.DataSync.StartTaskExecution`. */
36 tag: string;
37 /** The distilled operation; `TaskArn` is injected from the task. */
38 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
39 /** IAM actions granted on the task ARN + execution pattern. */
40 actions: readonly string[];
41 }) =>
42 Effect.gen(function* () {
43 const bind = yield* AWSBindings.Bind;
44 const op = yield* AWSBindings.withCredentials(options.operation);
45
46 return Effect.fn(function* (task: Task) {
47 // Outputs yield a DEFERRED effect — resolve again per invocation below.
48 const TaskArn = yield* task.taskArn;
49 if (!globalThis.__ALCHEMY_RUNTIME__) {
50 yield* bind`${options.tag}(${task})`({
51 policyStatements: [taskPolicyStatement(task, options.actions)],
52 });
53 }
54 return Effect.fn(`${options.tag}(${task.LogicalId})`)(function* (
55 request?: Omit<I, "TaskArn">,
56 ) {
57 return yield* op({ ...request, TaskArn: yield* TaskArn } as I);
58 });
59 });
60 });
61
62 /**
63 * Build the impl Effect for a task-anchored operation whose input addresses
64 * a task *execution* by ARN (returned by `StartTaskExecution`): the request
65 * passes through as-is and the deploy-time half grants `actions` on the
66 * bound task's ARN + execution pattern.
67 */
68 export const makeDataSyncTaskExecutionHttpBinding = <I, A, E, R>(options: {
69 /** Fully-qualified binding tag, e.g. `AWS.DataSync.CancelTaskExecution`. */
70 tag: string;
71 /** The distilled operation, invoked with the caller's request as-is. */
72 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
73 /** IAM actions granted on the task ARN + execution pattern. */
74 actions: readonly string[];
75 }) =>
76 Effect.gen(function* () {
77 const bind = yield* AWSBindings.Bind;
78 const op = yield* AWSBindings.withCredentials(options.operation);
79
80 return Effect.fn(function* (task: Task) {
81 if (!globalThis.__ALCHEMY_RUNTIME__) {
82 yield* bind`${options.tag}(${task})`({
83 policyStatements: [taskPolicyStatement(task, options.actions)],
84 });
85 }
86 return Effect.fn(`${options.tag}(${task.LogicalId})`)(function* (request: I) {
87 return yield* op(request);
88 });
89 });
90 });