The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as cci from "@distilled.cloud/gcp/contactcenterinsights_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 * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { resourceNameFromOperation, waitForOperation } from "./operations.ts"; |
| 13 | import { |
| 14 | encodeOwnership, |
| 15 | encodeOwnershipLine, |
| 16 | hasOwnershipMarker, |
| 17 | lastSegment, |
| 18 | locationOf, |
| 19 | locationParent, |
| 20 | ownedByAlchemy, |
| 21 | parentOf, |
| 22 | parseOwnership, |
| 23 | } from "./ownership.ts"; |
| 24 | |
| 25 | export type IssueModelsIssueProps = { |
| 26 | /** |
| 27 | * Parent IssueModel resource name |
| 28 | * (`projects/{project}/locations/{location}/issueModels/{issue_model}`). |
| 29 | * Immutable — changing it replaces the issue. |
| 30 | */ |
| 31 | parent: string; |
| 32 | /** |
| 33 | * Representative name. |
| 34 | */ |
| 35 | displayName?: string; |
| 36 | /** |
| 37 | * Representative description. Issues have no labels field, so Alchemy |
| 38 | * ownership is stored in a `[alchemy …]` prefix and stripped from |
| 39 | * attributes. |
| 40 | */ |
| 41 | displayDescription?: string; |
| 42 | }; |
| 43 | |
| 44 | export type IssueModelsIssue = Resource< |
| 45 | "GCP.ContactCenterInsights.IssueModelsIssue", |
| 46 | IssueModelsIssueProps, |
| 47 | { |
| 48 | /** Full resource name. */ |
| 49 | name: string; |
| 50 | /** Issue id (last path segment). */ |
| 51 | issueId: string; |
| 52 | /** Parent issue model resource name. */ |
| 53 | parent: string; |
| 54 | /** Region id. */ |
| 55 | location: string; |
| 56 | /** Project id. */ |
| 57 | project: string; |
| 58 | /** User-facing display name. */ |
| 59 | displayName: string | undefined; |
| 60 | /** User description with the Alchemy ownership prefix stripped. */ |
| 61 | displayDescription: string | undefined; |
| 62 | /** Sample representative utterances. */ |
| 63 | sampleUtterances: string[]; |
| 64 | /** RFC3339 creation timestamp. */ |
| 65 | createTime: string | undefined; |
| 66 | /** RFC3339 last-update timestamp. */ |
| 67 | updateTime: string | undefined; |
| 68 | }, |
| 69 | never, |
| 70 | Providers |
| 71 | >; |
| 72 | |
| 73 | /** |
| 74 | * An issue under a Contact Center Insights issue model. |
| 75 | * |
| 76 | * Parent issue model is immutable. Issues have no labels field — Alchemy |
| 77 | * stamps ownership into `displayDescription`. Display name and description |
| 78 | * update in place. Create is a long-running operation; the parent model |
| 79 | * must be undeployed. |
| 80 | * |
| 81 | * ### Creating an Issue |
| 82 | * **Example:** Billing issue under a model |
| 83 | * ```typescript |
| 84 | * const issue = yield* GCP.ContactCenterInsights.IssueModelsIssue("Billing", { |
| 85 | * parent: model.name, |
| 86 | * displayName: "billing", |
| 87 | * displayDescription: "questions about invoices", |
| 88 | * }); |
| 89 | * ``` |
| 90 | * |
| 91 | * @resource |
| 92 | * @category ContactCenterInsights |
| 93 | */ |
| 94 | export const IssueModelsIssue = Resource<IssueModelsIssue>( |
| 95 | "GCP.ContactCenterInsights.IssueModelsIssue", |
| 96 | ); |
| 97 | |
| 98 | export class IssueModelsIssueNotResolved extends Data.TaggedError( |
| 99 | "GCP.ContactCenterInsights.IssueModelsIssueNotResolved", |
| 100 | )<{ |
| 101 | resourceName: string; |
| 102 | }> {} |
| 103 | |
| 104 | const toAttrs = (issue: cci.GoogleCloudContactcenterinsightsV1Issue, project: string) => { |
| 105 | const name = issue.name ?? ""; |
| 106 | const parsed = parseOwnership(issue.displayDescription); |
| 107 | return { |
| 108 | name, |
| 109 | issueId: lastSegment(name), |
| 110 | parent: parentOf(name), |
| 111 | location: locationOf(name), |
| 112 | project, |
| 113 | displayName: issue.displayName, |
| 114 | displayDescription: parsed.text, |
| 115 | sampleUtterances: issue.sampleUtterances ?? [], |
| 116 | createTime: issue.createTime, |
| 117 | updateTime: issue.updateTime, |
| 118 | }; |
| 119 | }; |
| 120 | |
| 121 | const getByName = (name: string) => |
| 122 | name.length === 0 |
| 123 | ? Effect.undefined |
| 124 | : cci |
| 125 | .getProjectsLocationsIssueModelsIssues({ name }) |
| 126 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 127 | |
| 128 | const waitUntilExists = (name: string) => |
| 129 | getByName(name).pipe( |
| 130 | Effect.flatMap((issue) => |
| 131 | issue |
| 132 | ? Effect.succeed(issue) |
| 133 | : Effect.fail(new IssueModelsIssueNotResolved({ resourceName: name })), |
| 134 | ), |
| 135 | Effect.retry({ |
| 136 | while: (error) => error._tag === "GCP.ContactCenterInsights.IssueModelsIssueNotResolved", |
| 137 | times: 8, |
| 138 | schedule: Schedule.spaced("2 seconds"), |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | const listModels = (parent: string) => |
| 143 | cci.listProjectsLocationsIssueModels({ parent }).pipe( |
| 144 | Effect.map((page) => |
| 145 | (page.issueModels ?? []).map((model) => model.name ?? "").filter((name) => name.length > 0), |
| 146 | ), |
| 147 | Effect.catchTag("NotFound", () => Effect.succeed([] as string[])), |
| 148 | ); |
| 149 | |
| 150 | const listAtParent = (parent: string, project: string) => |
| 151 | cci.listProjectsLocationsIssueModelsIssues({ parent }).pipe( |
| 152 | Effect.map((page) => |
| 153 | (page.issues ?? []) |
| 154 | .filter((issue) => hasOwnershipMarker(issue.displayDescription)) |
| 155 | .map((issue) => toAttrs(issue, project)), |
| 156 | ), |
| 157 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 158 | ); |
| 159 | |
| 160 | const findOwned = (parent: string, displayDescription: string, displayName: string | undefined) => |
| 161 | cci.listProjectsLocationsIssueModelsIssues({ parent }).pipe( |
| 162 | Effect.map((page) => |
| 163 | (page.issues ?? []).find( |
| 164 | (issue) => |
| 165 | issue.displayDescription === displayDescription || |
| 166 | (displayName !== undefined && issue.displayName === displayName), |
| 167 | ), |
| 168 | ), |
| 169 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 170 | ); |
| 171 | |
| 172 | export const IssueModelsIssueProvider = () => |
| 173 | Provider.succeed(IssueModelsIssue, { |
| 174 | stables: ["name", "issueId", "parent", "location", "project", "createTime"], |
| 175 | |
| 176 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 177 | if (!isResolved(news)) return undefined; |
| 178 | const previousParent = olds?.parent ?? output?.parent; |
| 179 | if (previousParent !== undefined && news.parent !== previousParent) { |
| 180 | return { action: "replace" as const, deleteFirst: false }; |
| 181 | } |
| 182 | return undefined; |
| 183 | }), |
| 184 | |
| 185 | read: Effect.fn(function* ({ id, olds, output }) { |
| 186 | const env = yield* GcpEnvironment.current; |
| 187 | const existing = yield* getByName(output?.name ?? ""); |
| 188 | if (existing !== undefined) { |
| 189 | const attrs = toAttrs(existing, env.project); |
| 190 | return (yield* ownedByAlchemy(id, existing.displayDescription)) ? attrs : Unowned(attrs); |
| 191 | } |
| 192 | if (olds?.parent === undefined) return undefined; |
| 193 | const ownership = yield* createInternalLabels(id); |
| 194 | const displayDescription = encodeOwnership(ownership, olds.displayDescription); |
| 195 | const found = yield* findOwned(olds.parent, displayDescription, olds.displayName); |
| 196 | if (found === undefined) return undefined; |
| 197 | const attrs = toAttrs(found, env.project); |
| 198 | return (yield* ownedByAlchemy(id, found.displayDescription)) ? attrs : Unowned(attrs); |
| 199 | }), |
| 200 | |
| 201 | list: () => |
| 202 | Effect.gen(function* () { |
| 203 | const env = yield* GcpEnvironment.current; |
| 204 | const models = yield* listModels(locationParent(env.project, env.region)); |
| 205 | const pages = yield* Effect.forEach(models, (parent) => listAtParent(parent, env.project), { |
| 206 | concurrency: 4, |
| 207 | }); |
| 208 | return pages.flat(); |
| 209 | }), |
| 210 | |
| 211 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 212 | const env = yield* GcpEnvironment.current; |
| 213 | const ownership = yield* createInternalLabels(id); |
| 214 | const displayDescription = encodeOwnership(ownership, news.displayDescription); |
| 215 | const displayName = |
| 216 | news.displayName ?? encodeOwnershipLine(ownership, news.displayDescription); |
| 217 | |
| 218 | let current = yield* getByName(output?.name ?? ""); |
| 219 | if (current === undefined) { |
| 220 | current = yield* findOwned(news.parent, displayDescription, displayName); |
| 221 | } |
| 222 | |
| 223 | if (current === undefined) { |
| 224 | const created = yield* cci |
| 225 | .createProjectsLocationsIssueModelsIssues({ |
| 226 | parent: news.parent, |
| 227 | body: { |
| 228 | displayName, |
| 229 | displayDescription, |
| 230 | }, |
| 231 | }) |
| 232 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 233 | if (created !== undefined) { |
| 234 | const done = yield* waitForOperation(created); |
| 235 | const createdName = resourceNameFromOperation(done) ?? ""; |
| 236 | if (createdName.length > 0) { |
| 237 | current = yield* waitUntilExists(createdName); |
| 238 | } |
| 239 | } |
| 240 | if (current === undefined) { |
| 241 | current = yield* findOwned(news.parent, displayDescription, displayName); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (current === undefined) { |
| 246 | return yield* new IssueModelsIssueNotResolved({ |
| 247 | resourceName: output?.name ?? `${news.parent}/issues/-`, |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | const name = current.name ?? ""; |
| 252 | const displayChanged = (current.displayName ?? "") !== displayName; |
| 253 | const descriptionChanged = (current.displayDescription ?? "") !== displayDescription; |
| 254 | |
| 255 | if (displayChanged || descriptionChanged) { |
| 256 | current = yield* cci.patchProjectsLocationsIssueModelsIssues({ |
| 257 | name, |
| 258 | updateMask: [ |
| 259 | displayChanged ? "display_name" : undefined, |
| 260 | descriptionChanged ? "display_description" : undefined, |
| 261 | ] |
| 262 | .filter((field): field is string => field !== undefined) |
| 263 | .join(","), |
| 264 | body: { |
| 265 | name, |
| 266 | displayName, |
| 267 | displayDescription, |
| 268 | }, |
| 269 | }); |
| 270 | } |
| 271 | |
| 272 | return toAttrs(current, env.project); |
| 273 | }), |
| 274 | |
| 275 | delete: Effect.fn(function* ({ output }) { |
| 276 | yield* cci |
| 277 | .deleteProjectsLocationsIssueModelsIssues({ name: output.name }) |
| 278 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 279 | }), |
| 280 | }); |