The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as dialogflow from "@distilled.cloud/gcp/dialogflow_v3"; |
| 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 Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | DEFAULT_LOCATION, |
| 13 | encodeOwnershipLine, |
| 14 | fingerprint, |
| 15 | hasOwnershipMarker, |
| 16 | internalLabels, |
| 17 | lastSegment, |
| 18 | listLocations, |
| 19 | locationOf, |
| 20 | locationParent, |
| 21 | normalizeLocation, |
| 22 | ownedByAlchemy, |
| 23 | parseOwnership, |
| 24 | projectOf, |
| 25 | sameText, |
| 26 | toResourceId, |
| 27 | updateMaskOf, |
| 28 | retryQuota, |
| 29 | waitUntilReadable, |
| 30 | } from "./internal.ts"; |
| 31 | |
| 32 | export type RedactionStrategy = "REDACTION_STRATEGY_UNSPECIFIED" | "REDACT_WITH_SERVICE"; |
| 33 | |
| 34 | export type RedactionScope = "REDACTION_SCOPE_UNSPECIFIED" | "REDACT_DISK_STORAGE"; |
| 35 | |
| 36 | export type RetentionStrategy = "RETENTION_STRATEGY_UNSPECIFIED" | "REMOVE_AFTER_CONVERSATION"; |
| 37 | |
| 38 | export type PurgeDataType = "PURGE_DATA_TYPE_UNSPECIFIED" | "DIALOGFLOW_HISTORY"; |
| 39 | |
| 40 | export type AudioFormat = "AUDIO_FORMAT_UNSPECIFIED" | "MULAW" | "MP3" | "OGG"; |
| 41 | |
| 42 | export type AudioExportSettings = { |
| 43 | /** GCS bucket that stores exported audio. */ |
| 44 | gcsBucket?: string; |
| 45 | /** Filename pattern for exported audio. */ |
| 46 | audioExportPattern?: string; |
| 47 | /** Whether to redact exported audio. */ |
| 48 | enableAudioRedaction?: boolean; |
| 49 | /** Exported audio format. */ |
| 50 | audioFormat?: AudioFormat | (string & {}); |
| 51 | /** Whether to also export synthesized TTS audio. */ |
| 52 | storeTtsAudio?: boolean; |
| 53 | }; |
| 54 | |
| 55 | export type InsightsExportSettings = { |
| 56 | /** Whether to export conversations to Contact Center Insights. */ |
| 57 | enableInsightsExport?: boolean; |
| 58 | }; |
| 59 | |
| 60 | export type SecuritySettingProps = { |
| 61 | /** |
| 62 | * Security settings id (the `{securitySettings}` segment of |
| 63 | * `projects/{project}/locations/{location}/securitySettings/{id}`). |
| 64 | * Server-assigned on create. Immutable — changing it replaces the |
| 65 | * settings. |
| 66 | */ |
| 67 | securitySettingsId?: string; |
| 68 | /** |
| 69 | * Location (`global`, `us-central1`, …). Immutable — changing it |
| 70 | * replaces the settings. `US-CENTRAL1` is accepted and normalized to |
| 71 | * `us-central1`. |
| 72 | * @default "global" |
| 73 | */ |
| 74 | location?: string; |
| 75 | /** |
| 76 | * Human-readable name. Security settings have no labels field, so |
| 77 | * Alchemy stamps ownership into this field for `list` / nuke. |
| 78 | */ |
| 79 | displayName?: string; |
| 80 | /** How sensitive data is redacted. */ |
| 81 | redactionStrategy?: RedactionStrategy | (string & {}); |
| 82 | /** Which data is subject to redaction. */ |
| 83 | redactionScope?: RedactionScope | (string & {}); |
| 84 | /** |
| 85 | * DLP inspect template resource name |
| 86 | * `projects/{project}/locations/{location}/inspectTemplates/{template}`. |
| 87 | */ |
| 88 | inspectTemplate?: string; |
| 89 | /** |
| 90 | * DLP de-identify template resource name |
| 91 | * `projects/{project}/locations/{location}/deidentifyTemplates/{template}`. |
| 92 | */ |
| 93 | deidentifyTemplate?: string; |
| 94 | /** Data types purged when retention triggers. */ |
| 95 | purgeDataTypes?: Array<PurgeDataType | (string & {})>; |
| 96 | /** Audio export configuration. */ |
| 97 | audioExportSettings?: AudioExportSettings; |
| 98 | /** Contact Center Insights export configuration. */ |
| 99 | insightsExportSettings?: InsightsExportSettings; |
| 100 | /** |
| 101 | * Retention window in days. Mutually exclusive with |
| 102 | * `retentionStrategy`. |
| 103 | */ |
| 104 | retentionWindowDays?: number; |
| 105 | /** |
| 106 | * Retention strategy. Mutually exclusive with `retentionWindowDays`. |
| 107 | */ |
| 108 | retentionStrategy?: RetentionStrategy | (string & {}); |
| 109 | }; |
| 110 | |
| 111 | export type SecuritySetting = Resource< |
| 112 | "GCP.Dialogflow.SecuritySetting", |
| 113 | SecuritySettingProps, |
| 114 | { |
| 115 | /** Full resource name `projects/{project}/locations/{location}/securitySettings/{id}`. */ |
| 116 | name: string; |
| 117 | /** Security settings id (last path segment). */ |
| 118 | securitySettingsId: string; |
| 119 | /** Location id. */ |
| 120 | location: string; |
| 121 | /** Project id. */ |
| 122 | project: string; |
| 123 | /** User display name with the Alchemy ownership prefix stripped. */ |
| 124 | displayName: string | undefined; |
| 125 | /** Redaction strategy. */ |
| 126 | redactionStrategy: string | undefined; |
| 127 | /** Redaction scope. */ |
| 128 | redactionScope: string | undefined; |
| 129 | /** DLP inspect template. */ |
| 130 | inspectTemplate: string | undefined; |
| 131 | /** DLP de-identify template. */ |
| 132 | deidentifyTemplate: string | undefined; |
| 133 | /** Data types purged on retention. */ |
| 134 | purgeDataTypes: string[]; |
| 135 | /** Audio export configuration. */ |
| 136 | audioExportSettings: AudioExportSettings | undefined; |
| 137 | /** Insights export configuration. */ |
| 138 | insightsExportSettings: InsightsExportSettings | undefined; |
| 139 | /** Retention window in days. */ |
| 140 | retentionWindowDays: number | undefined; |
| 141 | /** Retention strategy. */ |
| 142 | retentionStrategy: string | undefined; |
| 143 | }, |
| 144 | never, |
| 145 | Providers |
| 146 | >; |
| 147 | |
| 148 | /** |
| 149 | * Dialogflow CX security settings for data redaction and retention. |
| 150 | * |
| 151 | * Security settings live at a location (not under an agent) and have no |
| 152 | * labels field — Alchemy stamps ownership into `displayName` so `list` / |
| 153 | * nuke can find them. Location and id are immutable. Display name, |
| 154 | * redaction, DLP templates, purge types, export settings, and retention |
| 155 | * update in place. |
| 156 | * |
| 157 | * ### Creating Security Settings |
| 158 | * **Example:** Remove data after each conversation |
| 159 | * ```typescript |
| 160 | * const settings = yield* GCP.Dialogflow.SecuritySetting("Retention", { |
| 161 | * location: "us-central1", |
| 162 | * displayName: "session-only", |
| 163 | * retentionStrategy: "REMOVE_AFTER_CONVERSATION", |
| 164 | * }); |
| 165 | * ``` |
| 166 | * |
| 167 | * ### Updating Security Settings |
| 168 | * Change props on the same logical id; the engine keeps the physical id. |
| 169 | * |
| 170 | * **Example:** Switch to a 30-day window |
| 171 | * ```typescript |
| 172 | * const settings = yield* GCP.Dialogflow.SecuritySetting("Retention", { |
| 173 | * location: "us-central1", |
| 174 | * displayName: "thirty-days", |
| 175 | * retentionWindowDays: 30, |
| 176 | * }); |
| 177 | * ``` |
| 178 | * |
| 179 | * @resource |
| 180 | * @category Dialogflow |
| 181 | */ |
| 182 | export const SecuritySetting = Resource<SecuritySetting>("GCP.Dialogflow.SecuritySetting"); |
| 183 | |
| 184 | export class SecuritySettingNotResolved extends Data.TaggedError( |
| 185 | "GCP.Dialogflow.SecuritySettingNotResolved", |
| 186 | )<{ |
| 187 | resourceName: string; |
| 188 | }> {} |
| 189 | |
| 190 | const resourceName = (project: string, location: string, securitySettingsId: string) => |
| 191 | `${locationParent(project, location)}/securitySettings/${securitySettingsId}`; |
| 192 | |
| 193 | const audioOf = ( |
| 194 | settings: dialogflow.GoogleCloudDialogflowCxV3SecuritySettingsAudioExportSettings | undefined, |
| 195 | ): AudioExportSettings | undefined => { |
| 196 | if (settings === undefined) return undefined; |
| 197 | return { |
| 198 | gcsBucket: settings.gcsBucket, |
| 199 | audioExportPattern: settings.audioExportPattern, |
| 200 | enableAudioRedaction: settings.enableAudioRedaction, |
| 201 | audioFormat: settings.audioFormat, |
| 202 | storeTtsAudio: settings.storeTtsAudio, |
| 203 | }; |
| 204 | }; |
| 205 | |
| 206 | const insightsOf = ( |
| 207 | settings: dialogflow.GoogleCloudDialogflowCxV3SecuritySettingsInsightsExportSettings | undefined, |
| 208 | ): InsightsExportSettings | undefined => { |
| 209 | if (settings === undefined) return undefined; |
| 210 | return { enableInsightsExport: settings.enableInsightsExport }; |
| 211 | }; |
| 212 | |
| 213 | const toAttrs = ( |
| 214 | settings: dialogflow.GoogleCloudDialogflowCxV3SecuritySettings, |
| 215 | project: string, |
| 216 | ) => { |
| 217 | const name = settings.name ?? ""; |
| 218 | return { |
| 219 | name, |
| 220 | securitySettingsId: lastSegment(name), |
| 221 | location: locationOf(name), |
| 222 | project: projectOf(name) || project, |
| 223 | displayName: parseOwnership(settings.displayName).text, |
| 224 | redactionStrategy: settings.redactionStrategy, |
| 225 | redactionScope: settings.redactionScope, |
| 226 | inspectTemplate: settings.inspectTemplate, |
| 227 | deidentifyTemplate: settings.deidentifyTemplate, |
| 228 | purgeDataTypes: [...(settings.purgeDataTypes ?? [])], |
| 229 | audioExportSettings: audioOf(settings.audioExportSettings), |
| 230 | insightsExportSettings: insightsOf(settings.insightsExportSettings), |
| 231 | retentionWindowDays: settings.retentionWindowDays, |
| 232 | retentionStrategy: settings.retentionStrategy, |
| 233 | }; |
| 234 | }; |
| 235 | |
| 236 | const getByName = (name: string) => |
| 237 | name.length === 0 |
| 238 | ? Effect.undefined |
| 239 | : dialogflow |
| 240 | .getProjectsLocationsSecuritySettings({ name }) |
| 241 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 242 | |
| 243 | const listAt = (parent: string, project: string) => |
| 244 | dialogflow.listProjectsLocationsSecuritySettings.pages({ parent, pageSize: 100 }).pipe( |
| 245 | Stream.flatMap((page) => Stream.fromIterable(page.securitySettings ?? [])), |
| 246 | Stream.filter((settings) => hasOwnershipMarker(settings.displayName)), |
| 247 | Stream.map((settings) => toAttrs(settings, project)), |
| 248 | Stream.runCollect, |
| 249 | Effect.map((chunk) => Array.from(chunk)), |
| 250 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 251 | ); |
| 252 | |
| 253 | const findByDisplayName = (parent: string, displayName: string) => |
| 254 | dialogflow.listProjectsLocationsSecuritySettings.pages({ parent, pageSize: 100 }).pipe( |
| 255 | Stream.flatMap((page) => Stream.fromIterable(page.securitySettings ?? [])), |
| 256 | Stream.filter((settings) => settings.displayName === displayName), |
| 257 | Stream.runHead, |
| 258 | Effect.map((option) => (option._tag === "Some" ? option.value : undefined)), |
| 259 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 260 | ); |
| 261 | |
| 262 | export const SecuritySettingProvider = () => |
| 263 | Provider.succeed(SecuritySetting, { |
| 264 | stables: ["name", "securitySettingsId", "location", "project"], |
| 265 | |
| 266 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 267 | if (!isResolved(news)) return undefined; |
| 268 | const previousLocation = olds?.location ?? output?.location; |
| 269 | if ( |
| 270 | previousLocation !== undefined && |
| 271 | news.location !== undefined && |
| 272 | normalizeLocation(previousLocation) !== normalizeLocation(news.location) |
| 273 | ) { |
| 274 | return { action: "replace" as const, deleteFirst: false }; |
| 275 | } |
| 276 | const previousId = olds?.securitySettingsId ?? output?.securitySettingsId; |
| 277 | if ( |
| 278 | previousId !== undefined && |
| 279 | news.securitySettingsId !== undefined && |
| 280 | news.securitySettingsId !== previousId |
| 281 | ) { |
| 282 | return { action: "replace" as const, deleteFirst: false }; |
| 283 | } |
| 284 | return undefined; |
| 285 | }), |
| 286 | |
| 287 | read: Effect.fn(function* ({ id, olds, output }) { |
| 288 | const env = yield* GcpEnvironment.current; |
| 289 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 290 | const securitySettingsId = yield* toResourceId( |
| 291 | id, |
| 292 | olds?.securitySettingsId, |
| 293 | output?.securitySettingsId, |
| 294 | ); |
| 295 | const name = output?.name ?? resourceName(env.project, location, securitySettingsId); |
| 296 | let existing = yield* getByName(name); |
| 297 | if (existing === undefined && output?.name === undefined) { |
| 298 | const ownership = yield* internalLabels(id); |
| 299 | existing = yield* findByDisplayName( |
| 300 | locationParent(env.project, location), |
| 301 | encodeOwnershipLine(ownership, olds?.displayName), |
| 302 | ); |
| 303 | } |
| 304 | if (existing === undefined) return undefined; |
| 305 | const attrs = toAttrs(existing, env.project); |
| 306 | return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs); |
| 307 | }), |
| 308 | |
| 309 | list: () => |
| 310 | Effect.gen(function* () { |
| 311 | const env = yield* GcpEnvironment.current; |
| 312 | const pages = yield* Effect.forEach( |
| 313 | listLocations(env.region), |
| 314 | (location) => listAt(locationParent(env.project, location), env.project), |
| 315 | { concurrency: 2 }, |
| 316 | ); |
| 317 | return pages.flat(); |
| 318 | }), |
| 319 | |
| 320 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 321 | const env = yield* GcpEnvironment.current; |
| 322 | const location = normalizeLocation(news.location ?? output?.location ?? DEFAULT_LOCATION); |
| 323 | const parent = locationParent(env.project, location); |
| 324 | const securitySettingsId = yield* toResourceId( |
| 325 | id, |
| 326 | news.securitySettingsId, |
| 327 | output?.securitySettingsId, |
| 328 | ); |
| 329 | const name = output?.name ?? resourceName(env.project, location, securitySettingsId); |
| 330 | const ownership = yield* internalLabels(id); |
| 331 | const displayName = encodeOwnershipLine(ownership, news.displayName); |
| 332 | const body: dialogflow.GoogleCloudDialogflowCxV3SecuritySettings = { |
| 333 | displayName, |
| 334 | redactionStrategy: news.redactionStrategy, |
| 335 | redactionScope: news.redactionScope, |
| 336 | inspectTemplate: news.inspectTemplate, |
| 337 | deidentifyTemplate: news.deidentifyTemplate, |
| 338 | purgeDataTypes: news.purgeDataTypes, |
| 339 | audioExportSettings: news.audioExportSettings, |
| 340 | insightsExportSettings: news.insightsExportSettings, |
| 341 | retentionWindowDays: news.retentionWindowDays, |
| 342 | retentionStrategy: |
| 343 | news.retentionWindowDays === undefined ? news.retentionStrategy : undefined, |
| 344 | }; |
| 345 | |
| 346 | let current = yield* getByName(name); |
| 347 | if (current === undefined) { |
| 348 | current = yield* findByDisplayName(parent, displayName); |
| 349 | } |
| 350 | |
| 351 | if (current === undefined) { |
| 352 | const created = yield* dialogflow |
| 353 | .createProjectsLocationsSecuritySettings({ |
| 354 | parent, |
| 355 | body, |
| 356 | }) |
| 357 | .pipe(Effect.catchTag("Conflict", () => findByDisplayName(parent, displayName))); |
| 358 | current = |
| 359 | created?.name !== undefined ? yield* waitUntilReadable(created.name, getByName) : created; |
| 360 | } |
| 361 | |
| 362 | if (current === undefined) { |
| 363 | return yield* new SecuritySettingNotResolved({ resourceName: name }); |
| 364 | } |
| 365 | |
| 366 | const currentName = current.name ?? name; |
| 367 | const displayChanged = !sameText(current.displayName, displayName); |
| 368 | const strategyChanged = !sameText(current.redactionStrategy, news.redactionStrategy); |
| 369 | const scopeChanged = !sameText(current.redactionScope, news.redactionScope); |
| 370 | const inspectChanged = !sameText(current.inspectTemplate, news.inspectTemplate); |
| 371 | const deidentifyChanged = !sameText(current.deidentifyTemplate, news.deidentifyTemplate); |
| 372 | const purgeChanged = fingerprint(current.purgeDataTypes) !== fingerprint(news.purgeDataTypes); |
| 373 | const audioChanged = |
| 374 | fingerprint(audioOf(current.audioExportSettings)) !== fingerprint(news.audioExportSettings); |
| 375 | const insightsChanged = |
| 376 | fingerprint(insightsOf(current.insightsExportSettings)) !== |
| 377 | fingerprint(news.insightsExportSettings); |
| 378 | const windowChanged = (current.retentionWindowDays ?? 0) !== (news.retentionWindowDays ?? 0); |
| 379 | const retentionStrategyChanged = !sameText(current.retentionStrategy, body.retentionStrategy); |
| 380 | |
| 381 | if ( |
| 382 | displayChanged || |
| 383 | strategyChanged || |
| 384 | scopeChanged || |
| 385 | inspectChanged || |
| 386 | deidentifyChanged || |
| 387 | purgeChanged || |
| 388 | audioChanged || |
| 389 | insightsChanged || |
| 390 | windowChanged || |
| 391 | retentionStrategyChanged |
| 392 | ) { |
| 393 | current = yield* dialogflow.patchProjectsLocationsSecuritySettings({ |
| 394 | name: currentName, |
| 395 | updateMask: updateMaskOf( |
| 396 | displayChanged ? "display_name" : undefined, |
| 397 | strategyChanged ? "redaction_strategy" : undefined, |
| 398 | scopeChanged ? "redaction_scope" : undefined, |
| 399 | inspectChanged ? "inspect_template" : undefined, |
| 400 | deidentifyChanged ? "deidentify_template" : undefined, |
| 401 | purgeChanged ? "purge_data_types" : undefined, |
| 402 | audioChanged ? "audio_export_settings" : undefined, |
| 403 | insightsChanged ? "insights_export_settings" : undefined, |
| 404 | windowChanged ? "retention_window_days" : undefined, |
| 405 | retentionStrategyChanged ? "retention_strategy" : undefined, |
| 406 | ), |
| 407 | body: { ...body, name: currentName }, |
| 408 | }); |
| 409 | } |
| 410 | |
| 411 | return toAttrs(current, env.project); |
| 412 | }, retryQuota), |
| 413 | |
| 414 | delete: Effect.fn(function* ({ output }) { |
| 415 | yield* dialogflow |
| 416 | .deleteProjectsLocationsSecuritySettings({ name: output.name }) |
| 417 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 418 | }, retryQuota), |
| 419 | }); |