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 { unownedUnlessPrevious } 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 Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | encodeOwnership, |
| 14 | hasOwnershipMarker, |
| 15 | lastSegment, |
| 16 | locationOf, |
| 17 | locationParent, |
| 18 | ownedByAlchemy, |
| 19 | parseOwnership, |
| 20 | sameJson, |
| 21 | toResourceId, |
| 22 | } from "./ownership.ts"; |
| 23 | |
| 24 | export type DateRangeConfig = { |
| 25 | /** Rolling window measured in `unit`. */ |
| 26 | relativeDateRange?: { |
| 27 | /** Number of units in the past. */ |
| 28 | quantity?: string; |
| 29 | /** Calendar unit (`DAY`, `WEEK`, `MONTH`, `QUARTER`, `YEAR`). */ |
| 30 | unit?: "TIME_UNIT_UNSPECIFIED" | "DAY" | "WEEK" | "MONTH" | "QUARTER" | "YEAR" | (string & {}); |
| 31 | }; |
| 32 | /** Inclusive start/end timestamps. */ |
| 33 | absoluteDateRange?: { |
| 34 | startTime?: string; |
| 35 | endTime?: string; |
| 36 | }; |
| 37 | }; |
| 38 | |
| 39 | export type DashboardProps = { |
| 40 | /** |
| 41 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 42 | * dashboard. |
| 43 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 44 | */ |
| 45 | location?: string; |
| 46 | /** |
| 47 | * Dashboard id (the `{dashboard}` segment). If omitted, a unique id is |
| 48 | * generated from the stack, stage, and logical id. Immutable — changing |
| 49 | * it replaces the dashboard. 4-64 characters matching |
| 50 | * `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$`. |
| 51 | */ |
| 52 | dashboardId?: string; |
| 53 | /** |
| 54 | * User-facing display name. |
| 55 | */ |
| 56 | displayName?: string; |
| 57 | /** |
| 58 | * Human-readable description. Dashboards have no labels field, so |
| 59 | * Alchemy ownership is stored in a `[alchemy …]` prefix and stripped |
| 60 | * from attributes. |
| 61 | */ |
| 62 | description?: string; |
| 63 | /** |
| 64 | * Filter applied to every chart on the dashboard. |
| 65 | */ |
| 66 | filter?: string; |
| 67 | /** |
| 68 | * Date range applied to every chart on the dashboard. |
| 69 | */ |
| 70 | dateRangeConfig?: DateRangeConfig; |
| 71 | /** |
| 72 | * Root widget container describing the dashboard layout. |
| 73 | */ |
| 74 | rootContainer?: cci.GoogleCloudContactcenterinsightsV1Container; |
| 75 | }; |
| 76 | |
| 77 | export type Dashboard = Resource< |
| 78 | "GCP.ContactCenterInsights.Dashboard", |
| 79 | DashboardProps, |
| 80 | { |
| 81 | /** Full resource name. */ |
| 82 | name: string; |
| 83 | /** Dashboard id (last path segment). */ |
| 84 | dashboardId: string; |
| 85 | /** Region id. */ |
| 86 | location: string; |
| 87 | /** Project id. */ |
| 88 | project: string; |
| 89 | /** User-facing display name. */ |
| 90 | displayName: string | undefined; |
| 91 | /** User description with the Alchemy ownership prefix stripped. */ |
| 92 | description: string | undefined; |
| 93 | /** Dashboard-wide filter. */ |
| 94 | filter: string | undefined; |
| 95 | /** Dashboard-wide date range. */ |
| 96 | dateRangeConfig: DateRangeConfig | undefined; |
| 97 | /** Root layout container. */ |
| 98 | rootContainer: cci.GoogleCloudContactcenterinsightsV1Container | undefined; |
| 99 | /** Whether the dashboard is a predefined read-only dashboard. */ |
| 100 | readOnly: boolean; |
| 101 | /** RFC3339 creation timestamp. */ |
| 102 | createTime: string | undefined; |
| 103 | /** RFC3339 last-update timestamp. */ |
| 104 | updateTime: string | undefined; |
| 105 | }, |
| 106 | never, |
| 107 | Providers |
| 108 | >; |
| 109 | |
| 110 | /** |
| 111 | * A Contact Center Insights dashboard of conversation charts. |
| 112 | * |
| 113 | * Dashboards have no labels field — Alchemy stamps ownership into the |
| 114 | * description. Location and dashboard id are immutable. Display name, |
| 115 | * description, filter, date range, and root container update in place. |
| 116 | * |
| 117 | * ### Creating a Dashboard |
| 118 | * **Example:** Generated id |
| 119 | * ```typescript |
| 120 | * const dashboard = yield* GCP.ContactCenterInsights.Dashboard("Overview", { |
| 121 | * displayName: "overview", |
| 122 | * description: "call quality", |
| 123 | * }); |
| 124 | * ``` |
| 125 | * |
| 126 | * **Example:** Named dashboard |
| 127 | * ```typescript |
| 128 | * const dashboard = yield* GCP.ContactCenterInsights.Dashboard("Overview", { |
| 129 | * dashboardId: "quality-overview", |
| 130 | * displayName: "quality", |
| 131 | * }); |
| 132 | * ``` |
| 133 | * |
| 134 | * @resource |
| 135 | * @category ContactCenterInsights |
| 136 | */ |
| 137 | export const Dashboard = Resource<Dashboard>("GCP.ContactCenterInsights.Dashboard"); |
| 138 | |
| 139 | export class DashboardNotResolved extends Data.TaggedError( |
| 140 | "GCP.ContactCenterInsights.DashboardNotResolved", |
| 141 | )<{ |
| 142 | resourceName: string; |
| 143 | }> {} |
| 144 | |
| 145 | const resourceName = (parent: string, dashboardId: string) => `${parent}/dashboards/${dashboardId}`; |
| 146 | |
| 147 | const toDateRangeConfig = ( |
| 148 | config: cci.GoogleCloudContactcenterinsightsV1DateRangeConfig | undefined, |
| 149 | ): DateRangeConfig | undefined => { |
| 150 | if (config === undefined) return undefined; |
| 151 | return { |
| 152 | relativeDateRange: |
| 153 | config.relativeDateRange === undefined |
| 154 | ? undefined |
| 155 | : { |
| 156 | quantity: config.relativeDateRange.quantity, |
| 157 | unit: config.relativeDateRange.unit, |
| 158 | }, |
| 159 | absoluteDateRange: |
| 160 | config.absoluteDateRange === undefined |
| 161 | ? undefined |
| 162 | : { |
| 163 | startTime: config.absoluteDateRange.startTime, |
| 164 | endTime: config.absoluteDateRange.endTime, |
| 165 | }, |
| 166 | }; |
| 167 | }; |
| 168 | |
| 169 | const toAttrs = (dashboard: cci.GoogleCloudContactcenterinsightsV1Dashboard, project: string) => { |
| 170 | const name = dashboard.name ?? ""; |
| 171 | const parsed = parseOwnership(dashboard.description); |
| 172 | return { |
| 173 | name, |
| 174 | dashboardId: lastSegment(name), |
| 175 | location: locationOf(name), |
| 176 | project, |
| 177 | displayName: dashboard.displayName, |
| 178 | description: parsed.text, |
| 179 | filter: dashboard.filter, |
| 180 | dateRangeConfig: toDateRangeConfig(dashboard.dateRangeConfig), |
| 181 | rootContainer: dashboard.rootContainer, |
| 182 | readOnly: dashboard.readOnly === true, |
| 183 | createTime: dashboard.createTime, |
| 184 | updateTime: dashboard.updateTime, |
| 185 | }; |
| 186 | }; |
| 187 | |
| 188 | const getByName = (name: string) => |
| 189 | name.length === 0 |
| 190 | ? Effect.undefined |
| 191 | : cci |
| 192 | .getProjectsLocationsDashboards({ name }) |
| 193 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 194 | |
| 195 | const listAt = (parent: string, project: string) => |
| 196 | cci.listProjectsLocationsDashboards.pages({ parent, pageSize: 100 }).pipe( |
| 197 | Stream.flatMap((page) => Stream.fromIterable(page.dashboards ?? [])), |
| 198 | Stream.filter((dashboard) => hasOwnershipMarker(dashboard.description)), |
| 199 | Stream.map((dashboard) => toAttrs(dashboard, project)), |
| 200 | Stream.runCollect, |
| 201 | Effect.map((chunk) => Array.from(chunk)), |
| 202 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 203 | ); |
| 204 | |
| 205 | export const DashboardProvider = () => |
| 206 | Provider.succeed(Dashboard, { |
| 207 | stables: ["name", "dashboardId", "location", "project", "createTime"], |
| 208 | |
| 209 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 210 | if (!isResolved(news)) return undefined; |
| 211 | const previousLocation = olds?.location ?? output?.location; |
| 212 | const nextLocation = news.location; |
| 213 | if ( |
| 214 | previousLocation !== undefined && |
| 215 | nextLocation !== undefined && |
| 216 | previousLocation !== nextLocation |
| 217 | ) { |
| 218 | return { action: "replace" as const, deleteFirst: false }; |
| 219 | } |
| 220 | const previousId = olds?.dashboardId ?? output?.dashboardId; |
| 221 | if ( |
| 222 | previousId !== undefined && |
| 223 | news.dashboardId !== undefined && |
| 224 | news.dashboardId !== previousId |
| 225 | ) { |
| 226 | return { action: "replace" as const, deleteFirst: true }; |
| 227 | } |
| 228 | return undefined; |
| 229 | }), |
| 230 | |
| 231 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 232 | const env = yield* GcpEnvironment.current; |
| 233 | const dashboardId = yield* toResourceId(id, olds?.dashboardId, output?.dashboardId); |
| 234 | const location = olds?.location ?? output?.location ?? env.region; |
| 235 | const name = output?.name ?? resourceName(locationParent(env.project, location), dashboardId); |
| 236 | const existing = yield* getByName(name); |
| 237 | if (existing === undefined) return undefined; |
| 238 | const attrs = toAttrs(existing, env.project); |
| 239 | return (yield* ownedByAlchemy(id, existing.description)) |
| 240 | ? attrs |
| 241 | : unownedUnlessPrevious(attrs, previous, "name"); |
| 242 | }), |
| 243 | |
| 244 | list: () => |
| 245 | Effect.gen(function* () { |
| 246 | const env = yield* GcpEnvironment.current; |
| 247 | return yield* listAt(locationParent(env.project, env.region), env.project); |
| 248 | }), |
| 249 | |
| 250 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 251 | const env = yield* GcpEnvironment.current; |
| 252 | const location = news.location ?? output?.location ?? env.region; |
| 253 | const parent = locationParent(env.project, location); |
| 254 | const dashboardId = yield* toResourceId(id, news.dashboardId, output?.dashboardId); |
| 255 | const name = resourceName(parent, dashboardId); |
| 256 | const ownership = yield* createInternalLabels(id); |
| 257 | const description = encodeOwnership(ownership, news.description); |
| 258 | const displayName = news.displayName ?? dashboardId; |
| 259 | |
| 260 | let current = yield* getByName(output?.name ?? name); |
| 261 | |
| 262 | if (current === undefined) { |
| 263 | const created = yield* cci |
| 264 | .createProjectsLocationsDashboards({ |
| 265 | parent, |
| 266 | dashboardId, |
| 267 | body: { |
| 268 | displayName, |
| 269 | description, |
| 270 | filter: news.filter, |
| 271 | dateRangeConfig: news.dateRangeConfig, |
| 272 | rootContainer: news.rootContainer, |
| 273 | }, |
| 274 | }) |
| 275 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 276 | current = created ?? undefined; |
| 277 | } |
| 278 | |
| 279 | if (current === undefined) { |
| 280 | return yield* new DashboardNotResolved({ resourceName: name }); |
| 281 | } |
| 282 | |
| 283 | const displayChanged = (current.displayName ?? "") !== displayName; |
| 284 | const descriptionChanged = (current.description ?? "") !== description; |
| 285 | const filterChanged = (current.filter ?? "") !== (news.filter ?? ""); |
| 286 | const dateRangeChanged = !sameJson(current.dateRangeConfig, news.dateRangeConfig); |
| 287 | const rootChanged = !sameJson(current.rootContainer, news.rootContainer); |
| 288 | |
| 289 | if ( |
| 290 | displayChanged || |
| 291 | descriptionChanged || |
| 292 | filterChanged || |
| 293 | dateRangeChanged || |
| 294 | rootChanged |
| 295 | ) { |
| 296 | current = yield* cci.patchProjectsLocationsDashboards({ |
| 297 | name: current.name ?? name, |
| 298 | updateMask: [ |
| 299 | displayChanged ? "display_name" : undefined, |
| 300 | descriptionChanged ? "description" : undefined, |
| 301 | filterChanged ? "filter" : undefined, |
| 302 | dateRangeChanged ? "date_range_config" : undefined, |
| 303 | rootChanged ? "root_container" : undefined, |
| 304 | ] |
| 305 | .filter((field): field is string => field !== undefined) |
| 306 | .join(","), |
| 307 | body: { |
| 308 | name: current.name ?? name, |
| 309 | displayName, |
| 310 | description, |
| 311 | filter: news.filter, |
| 312 | dateRangeConfig: news.dateRangeConfig, |
| 313 | rootContainer: news.rootContainer, |
| 314 | }, |
| 315 | }); |
| 316 | } |
| 317 | |
| 318 | return toAttrs(current, env.project); |
| 319 | }), |
| 320 | |
| 321 | delete: Effect.fn(function* ({ output }) { |
| 322 | yield* cci |
| 323 | .deleteProjectsLocationsDashboards({ name: output.name }) |
| 324 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 325 | }), |
| 326 | }); |