The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as speech from "@distilled.cloud/gcp/speech_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | type ClassItem, |
| 12 | DEFAULT_LOCATION, |
| 13 | deleteCustomClass, |
| 14 | getCustomClass, |
| 15 | listOwnedCustomClasses, |
| 16 | locationParent, |
| 17 | markerFromItems, |
| 18 | normalizeLocation, |
| 19 | parseResourceName, |
| 20 | replaceOnIdentity, |
| 21 | resourceNameOf, |
| 22 | sameItems, |
| 23 | stripOwnershipItems, |
| 24 | toPhysicalId, |
| 25 | updateMaskOf, |
| 26 | waitUntilGone, |
| 27 | } from "./internal.ts"; |
| 28 | |
| 29 | export type { ClassItem }; |
| 30 | |
| 31 | export type CustomClassProps = { |
| 32 | /** |
| 33 | * Speech-to-Text Adaptation location. The global Speech endpoint |
| 34 | * accepts `global`; `us` and `eu` need matching regional endpoints. |
| 35 | * Immutable — changing it replaces the custom class. |
| 36 | * @default "global" |
| 37 | */ |
| 38 | location?: string; |
| 39 | /** |
| 40 | * Custom class id (the `{custom_class}` segment of |
| 41 | * `projects/{project}/locations/{location}/customClasses/{custom_class}`). |
| 42 | * If omitted, a unique id is generated. Letters, numbers, and hyphens; |
| 43 | * 4-63 characters; must start with a letter. Immutable — changing it |
| 44 | * replaces the custom class. |
| 45 | */ |
| 46 | customClassId?: string; |
| 47 | /** |
| 48 | * Class items (words or phrases that represent one concept). |
| 49 | */ |
| 50 | items?: ClassItem[]; |
| 51 | }; |
| 52 | |
| 53 | export type CustomClass = Resource< |
| 54 | "GCP.Speech.CustomClass", |
| 55 | CustomClassProps, |
| 56 | { |
| 57 | /** Full resource name `projects/{project}/locations/{location}/customClasses/{custom_class}`. */ |
| 58 | name: string; |
| 59 | /** Custom class id (last path segment). */ |
| 60 | customClassId: string; |
| 61 | /** Project id. */ |
| 62 | project: string; |
| 63 | /** Adaptation location. */ |
| 64 | location: string; |
| 65 | /** Class items. */ |
| 66 | items: ClassItem[]; |
| 67 | /** Server-assigned uid, if present. */ |
| 68 | uid: string | undefined; |
| 69 | /** Lifecycle state, if present. */ |
| 70 | state: string | undefined; |
| 71 | /** KMS key encrypting class items, if any. */ |
| 72 | kmsKeyName: string | undefined; |
| 73 | }, |
| 74 | never, |
| 75 | Providers |
| 76 | >; |
| 77 | |
| 78 | /** |
| 79 | * A Speech-to-Text Adaptation custom class. Custom classes group words |
| 80 | * that represent one concept (for example passenger ship names) and are |
| 81 | * referenced from PhraseSet hints as `${custom_class_id}`. |
| 82 | * |
| 83 | * Speech-to-Text v1 classes have no labels field and Alchemy does not |
| 84 | * write ownership into the items (they steer recognition). A generated id |
| 85 | * identifies the class; one with an explicit id that Alchemy has no state |
| 86 | * for is reported as unowned. Location and custom class id are identity — changing either replaces the class. |
| 87 | * `items` update in place. |
| 88 | * |
| 89 | * ### Creating a Custom Class |
| 90 | * **Example:** Generated id |
| 91 | * ```typescript |
| 92 | * const ships = yield* GCP.Speech.CustomClass("Ships", { |
| 93 | * items: [{ value: "sloop" }, { value: "ketch" }], |
| 94 | * }); |
| 95 | * ``` |
| 96 | * |
| 97 | * **Example:** Explicit id and location |
| 98 | * ```typescript |
| 99 | * const ships = yield* GCP.Speech.CustomClass("Ships", { |
| 100 | * location: "global", |
| 101 | * customClassId: "passenger-ships", |
| 102 | * items: [{ value: "sloop" }], |
| 103 | * }); |
| 104 | * ``` |
| 105 | * |
| 106 | * ### Updating a Custom Class |
| 107 | * **Example:** Replace the class items |
| 108 | * ```typescript |
| 109 | * const ships = yield* GCP.Speech.CustomClass("Ships", { |
| 110 | * items: [{ value: "brig" }, { value: "barque" }], |
| 111 | * }); |
| 112 | * ``` |
| 113 | * |
| 114 | * @resource |
| 115 | * @category Speech |
| 116 | */ |
| 117 | export const CustomClass = Resource<CustomClass>("GCP.Speech.CustomClass"); |
| 118 | |
| 119 | export class CustomClassNotResolved extends Data.TaggedError("GCP.Speech.CustomClassNotResolved")<{ |
| 120 | resourceName: string; |
| 121 | }> {} |
| 122 | |
| 123 | const customClassNameOf = (project: string, location: string, customClassId: string) => |
| 124 | resourceNameOf(project, location, "customClasses", customClassId); |
| 125 | |
| 126 | const toAttrs = (customClass: speech.CustomClass, project: string) => { |
| 127 | const name = customClass.name ?? ""; |
| 128 | const parsed = parseResourceName(name, "customClasses"); |
| 129 | return { |
| 130 | name, |
| 131 | customClassId: parsed.id || customClass.customClassId || "", |
| 132 | // Speech echoes names with the project number; report the project id. |
| 133 | project, |
| 134 | location: parsed.location, |
| 135 | items: stripOwnershipItems(customClass.items), |
| 136 | uid: customClass.uid, |
| 137 | state: customClass.state, |
| 138 | kmsKeyName: customClass.kmsKeyName, |
| 139 | }; |
| 140 | }; |
| 141 | |
| 142 | export const CustomClassProvider = () => |
| 143 | Provider.succeed(CustomClass, { |
| 144 | stables: ["name", "customClassId", "project", "location", "uid"], |
| 145 | |
| 146 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 147 | if (!isResolved(news)) return undefined; |
| 148 | return replaceOnIdentity({ |
| 149 | previousId: olds?.customClassId ?? output?.customClassId, |
| 150 | nextId: news.customClassId, |
| 151 | previousLocation: olds?.location ?? output?.location, |
| 152 | nextLocation: news.location ?? olds?.location ?? output?.location, |
| 153 | }); |
| 154 | }), |
| 155 | |
| 156 | read: Effect.fn(function* ({ id, olds, output }) { |
| 157 | const env = yield* GcpEnvironment.current; |
| 158 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 159 | const customClassId = yield* toPhysicalId(id, olds?.customClassId, output?.customClassId); |
| 160 | const existing = yield* getCustomClass( |
| 161 | output?.name ?? customClassNameOf(env.project, location, customClassId), |
| 162 | ); |
| 163 | if (existing === undefined) return undefined; |
| 164 | const attrs = toAttrs(existing, env.project); |
| 165 | // No labels: a generated id derives from this stack, stage, logical |
| 166 | // id and instance; an explicit id is only ours when state has it. |
| 167 | return output !== undefined || olds?.customClassId === undefined ? attrs : Unowned(attrs); |
| 168 | }), |
| 169 | |
| 170 | list: () => |
| 171 | Effect.gen(function* () { |
| 172 | const env = yield* GcpEnvironment.current; |
| 173 | const classes = yield* listOwnedCustomClasses(env.project); |
| 174 | return classes.map((customClass) => toAttrs(customClass, env.project)); |
| 175 | }), |
| 176 | |
| 177 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 178 | const env = yield* GcpEnvironment.current; |
| 179 | const location = normalizeLocation(news.location ?? output?.location ?? DEFAULT_LOCATION); |
| 180 | const customClassId = yield* toPhysicalId(id, news.customClassId, output?.customClassId); |
| 181 | const items = news.items ?? []; |
| 182 | const name = output?.name ?? customClassNameOf(env.project, location, customClassId); |
| 183 | |
| 184 | let current = yield* getCustomClass(name); |
| 185 | |
| 186 | if (current === undefined) { |
| 187 | const created = yield* speech |
| 188 | .createProjectsLocationsCustomClasses({ |
| 189 | parent: locationParent(env.project, location), |
| 190 | body: { |
| 191 | customClassId, |
| 192 | customClass: { items }, |
| 193 | }, |
| 194 | }) |
| 195 | .pipe( |
| 196 | Effect.catchTag("Conflict", () => |
| 197 | getCustomClass(customClassNameOf(env.project, location, customClassId)), |
| 198 | ), |
| 199 | ); |
| 200 | current = created ?? undefined; |
| 201 | if (current?.name) { |
| 202 | current = (yield* getCustomClass(current.name)) ?? current; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (current === undefined) { |
| 207 | return yield* new CustomClassNotResolved({ |
| 208 | resourceName: name || customClassNameOf(env.project, location, customClassId), |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | const currentName = current.name ?? name; |
| 213 | const updateMask = updateMaskOf( |
| 214 | markerFromItems(current.items) === undefined && sameItems(current.items, news.items) |
| 215 | ? undefined |
| 216 | : "items", |
| 217 | ); |
| 218 | if (updateMask.length > 0) { |
| 219 | current = yield* speech.patchProjectsLocationsCustomClasses({ |
| 220 | name: currentName, |
| 221 | updateMask, |
| 222 | body: { |
| 223 | name: currentName, |
| 224 | items, |
| 225 | }, |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | return toAttrs(current, env.project); |
| 230 | }), |
| 231 | |
| 232 | delete: Effect.fn(function* ({ output }) { |
| 233 | if (!output.name) return; |
| 234 | yield* deleteCustomClass(output.name); |
| 235 | yield* waitUntilGone(getCustomClass(output.name)); |
| 236 | }), |
| 237 | }); |