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

main
ts 107 lines 3.53 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as GCPBindings from "../GCPBindings.ts";
3 import { grantFor, type BindingIam, type GcpHttpOp } from "../HttpBinding.ts";
4 import type { AdaptiveMtDataset } from "./AdaptiveMtDataset.ts";
5 import type { GlossariesGlossaryEntry } from "./GlossariesGlossaryEntry.ts";
6 import { locationParentOf } from "./internal.ts";
7 import type { Model } from "./Model.ts";
8
9 const closeOver = <I, A, E>(operation: GcpHttpOp<I, A, E>) => operation;
10
11 const makeNamedHttpBinding = <
12 Resource extends AdaptiveMtDataset | GlossariesGlossaryEntry | Model,
13 I extends { name: string },
14 A,
15 E,
16 >(options: {
17 tag: string;
18 iam: BindingIam;
19 operation: GcpHttpOp<I, A, E>;
20 }) =>
21 Effect.gen(function* () {
22 const bind = yield* GCPBindings.Bind;
23 const run = yield* closeOver(options.operation);
24 return Effect.fn(function* (resource: Resource) {
25 if (!globalThis.__ALCHEMY_RUNTIME__) {
26 yield* bind`${options.tag}(${resource})`({
27 iam: [grantFor(options.iam, resource.name)],
28 });
29 }
30 const name = yield* resource.name;
31 return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (
32 request?: Omit<I, "name">,
33 ) {
34 return yield* run({
35 ...request,
36 name: yield* name,
37 } as I);
38 });
39 });
40 });
41
42 const makeLocationParentHttpBinding = <
43 Resource extends AdaptiveMtDataset | Model,
44 I extends { parent: string },
45 A,
46 E,
47 >(options: {
48 tag: string;
49 iam: BindingIam;
50 operation: GcpHttpOp<I, A, E>;
51 withBody: (name: string, request: Omit<I, "parent"> | undefined) => I;
52 }) =>
53 Effect.gen(function* () {
54 const bind = yield* GCPBindings.Bind;
55 const run = yield* closeOver(options.operation);
56 return Effect.fn(function* (resource: Resource) {
57 if (!globalThis.__ALCHEMY_RUNTIME__) {
58 yield* bind`${options.tag}(${resource})`({
59 iam: [grantFor(options.iam, resource.name)],
60 });
61 }
62 const name = yield* resource.name;
63 return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (
64 request?: Omit<I, "parent">,
65 ) {
66 return yield* run(options.withBody(yield* name, request));
67 });
68 });
69 });
70
71 /**
72 * Shared HTTP scaffolding for Cloud Translation bindings.
73 * NOT exported from index.ts.
74 */
75 export const makeAdaptiveMtDatasetHttpBinding = <I extends { name: string }, A, E>(options: {
76 tag: string;
77 iam: BindingIam;
78 operation: GcpHttpOp<I, A, E>;
79 }) => makeNamedHttpBinding<AdaptiveMtDataset, I, A, E>(options);
80
81 export const makeGlossaryEntryHttpBinding = <I extends { name: string }, A, E>(options: {
82 tag: string;
83 iam: BindingIam;
84 operation: GcpHttpOp<I, A, E>;
85 }) => makeNamedHttpBinding<GlossariesGlossaryEntry, I, A, E>(options);
86
87 export const makeModelHttpBinding = <I extends { name: string }, A, E>(options: {
88 tag: string;
89 iam: BindingIam;
90 operation: GcpHttpOp<I, A, E>;
91 }) => makeNamedHttpBinding<Model, I, A, E>(options);
92
93 export const makeAdaptiveMtTranslateBinding = <I extends { parent: string }, A, E>(options: {
94 tag: string;
95 iam: BindingIam;
96 operation: GcpHttpOp<I, A, E>;
97 withBody: (name: string, request: Omit<I, "parent"> | undefined) => I;
98 }) => makeLocationParentHttpBinding<AdaptiveMtDataset, I, A, E>(options);
99
100 export const makeTranslateTextBinding = <I extends { parent: string }, A, E>(options: {
101 tag: string;
102 iam: BindingIam;
103 operation: GcpHttpOp<I, A, E>;
104 withBody: (name: string, request: Omit<I, "parent"> | undefined) => I;
105 }) => makeLocationParentHttpBinding<Model, I, A, E>(options);
106
107 export { locationParentOf };