The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ces from "@distilled.cloud/gcp/ces_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 { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | collectPages, |
| 13 | encodeOwnership, |
| 14 | expandApp, |
| 15 | forEachApp, |
| 16 | hasOwnershipMarker, |
| 17 | normalizeLocation, |
| 18 | ownedByAlchemy, |
| 19 | parseOwnership, |
| 20 | parseResourceName, |
| 21 | replaceOnIdentity, |
| 22 | retryTransient, |
| 23 | sameJson, |
| 24 | sameText, |
| 25 | toPhysicalId, |
| 26 | updateMaskOf, |
| 27 | waitUntilGone, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | export type AppsExampleProps = { |
| 31 | /** |
| 32 | * Parent CES app. Full name |
| 33 | * `projects/{project}/locations/{location}/apps/{app}` or the app id |
| 34 | * (combined with `location`). Immutable — changing it replaces the |
| 35 | * example. |
| 36 | */ |
| 37 | app: string; |
| 38 | /** |
| 39 | * Region used when `app` is a bare id. |
| 40 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 41 | */ |
| 42 | location?: string; |
| 43 | /** |
| 44 | * Example id. If omitted, a unique name is generated. Immutable — |
| 45 | * changing it replaces the example. |
| 46 | */ |
| 47 | exampleId?: string; |
| 48 | /** |
| 49 | * Human-readable name. Required by the API; Alchemy falls back to the |
| 50 | * generated example id. |
| 51 | */ |
| 52 | displayName?: string; |
| 53 | /** |
| 54 | * Human-readable description. Examples have no labels field, so |
| 55 | * Alchemy stamps ownership into this field. |
| 56 | */ |
| 57 | description?: string; |
| 58 | /** |
| 59 | * Entry agent resource name. When omitted, the root agent handles the |
| 60 | * conversation. |
| 61 | */ |
| 62 | entryAgent?: string; |
| 63 | /** |
| 64 | * Conversation messages that make up the example. |
| 65 | */ |
| 66 | messages?: ces.MessageList; |
| 67 | }; |
| 68 | |
| 69 | export type AppsExample = Resource< |
| 70 | "GCP.CES.AppsExample", |
| 71 | AppsExampleProps, |
| 72 | { |
| 73 | /** Full resource name `.../apps/{app}/examples/{example}`. */ |
| 74 | name: string; |
| 75 | /** Example id (last path segment). */ |
| 76 | exampleId: string; |
| 77 | /** Parent app resource name. */ |
| 78 | app: string; |
| 79 | /** Location id. */ |
| 80 | location: string; |
| 81 | /** Project id. */ |
| 82 | project: string; |
| 83 | /** Display name. */ |
| 84 | displayName: string | undefined; |
| 85 | /** User description with the Alchemy ownership prefix stripped. */ |
| 86 | description: string | undefined; |
| 87 | /** Entry agent resource name. */ |
| 88 | entryAgent: string | undefined; |
| 89 | /** Conversation messages. */ |
| 90 | messages: ces.MessageList | undefined; |
| 91 | /** Whether the example is invalid. */ |
| 92 | invalid: boolean | undefined; |
| 93 | /** RFC3339 creation timestamp. */ |
| 94 | createTime: string | undefined; |
| 95 | /** RFC3339 last-update timestamp. */ |
| 96 | updateTime: string | undefined; |
| 97 | /** Server-assigned etag. */ |
| 98 | etag: string | undefined; |
| 99 | }, |
| 100 | never, |
| 101 | Providers |
| 102 | >; |
| 103 | |
| 104 | /** |
| 105 | * A Customer Engagement Suite few-shot example conversation inside an |
| 106 | * app. |
| 107 | * |
| 108 | * Examples have no labels field — Alchemy stamps ownership into |
| 109 | * `description` so `list` / nuke can find them. Parent app, location, |
| 110 | * and example id are immutable. |
| 111 | * |
| 112 | * ### Creating an Example |
| 113 | * **Example:** Greeting transcript |
| 114 | * ```typescript |
| 115 | * const example = yield* GCP.CES.AppsExample("Hello", { |
| 116 | * app: app.name, |
| 117 | * displayName: "hello", |
| 118 | * messages: [ |
| 119 | * { role: "user", chunks: [{ text: "Hi" }] }, |
| 120 | * { role: "agent", chunks: [{ text: "Hello! How can I help?" }] }, |
| 121 | * ], |
| 122 | * }); |
| 123 | * ``` |
| 124 | * |
| 125 | * @resource |
| 126 | * @category CES |
| 127 | */ |
| 128 | export const AppsExample = Resource<AppsExample>("GCP.CES.AppsExample"); |
| 129 | |
| 130 | export class AppsExampleNotResolved extends Data.TaggedError("GCP.CES.AppsExampleNotResolved")<{ |
| 131 | resourceName: string; |
| 132 | }> {} |
| 133 | |
| 134 | const resourceName = (app: string, exampleId: string) => `${app}/examples/${exampleId}`; |
| 135 | |
| 136 | const toAttrs = (example: ces.Example, project: string, region: string, appHint?: string) => { |
| 137 | const name = example.name ?? ""; |
| 138 | const parsed = parseResourceName(name, "examples", region); |
| 139 | return { |
| 140 | name, |
| 141 | exampleId: parsed.id, |
| 142 | app: name.includes("/examples/") ? parsed.app : (appHint ?? parsed.parent), |
| 143 | location: parsed.location, |
| 144 | project: parsed.project || project, |
| 145 | displayName: example.displayName, |
| 146 | description: parseOwnership(example.description).text, |
| 147 | entryAgent: example.entryAgent, |
| 148 | messages: example.messages, |
| 149 | invalid: example.invalid, |
| 150 | createTime: example.createTime, |
| 151 | updateTime: example.updateTime, |
| 152 | etag: example.etag, |
| 153 | }; |
| 154 | }; |
| 155 | |
| 156 | const getByName = (name: string) => |
| 157 | name.length === 0 |
| 158 | ? Effect.undefined |
| 159 | : ces |
| 160 | .getProjectsLocationsAppsExamples({ name }) |
| 161 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 162 | |
| 163 | const listAt = (parent: string, project: string, region: string) => |
| 164 | collectPages( |
| 165 | ces.listProjectsLocationsAppsExamples.pages({ parent, pageSize: 100 }), |
| 166 | (page) => page.examples, |
| 167 | ).pipe( |
| 168 | Effect.map((examples) => |
| 169 | examples |
| 170 | .filter((example) => hasOwnershipMarker(example.description)) |
| 171 | .map((example) => toAttrs(example, project, region, parent)), |
| 172 | ), |
| 173 | ); |
| 174 | |
| 175 | export const AppsExampleProvider = () => |
| 176 | Provider.succeed(AppsExample, { |
| 177 | stables: ["name", "exampleId", "app", "location", "project", "createTime"], |
| 178 | |
| 179 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 180 | if (!isResolved(news)) return undefined; |
| 181 | return replaceOnIdentity({ |
| 182 | previousId: olds?.exampleId ?? output?.exampleId, |
| 183 | nextId: news.exampleId, |
| 184 | previousParent: olds?.app ?? output?.app, |
| 185 | nextParent: news.app, |
| 186 | }); |
| 187 | }), |
| 188 | |
| 189 | read: Effect.fn(function* ({ id, olds, output }) { |
| 190 | const env = yield* GcpEnvironment.current; |
| 191 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 192 | const app = olds?.app ? expandApp(olds.app, env.project, location) : output?.app; |
| 193 | const exampleId = yield* toPhysicalId(id, olds?.exampleId, output?.exampleId); |
| 194 | const name = output?.name ?? (app !== undefined ? resourceName(app, exampleId) : ""); |
| 195 | const existing = yield* getByName(name); |
| 196 | if (existing === undefined) return undefined; |
| 197 | const attrs = toAttrs(existing, env.project, env.region, app); |
| 198 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 199 | }), |
| 200 | |
| 201 | list: () => |
| 202 | Effect.gen(function* () { |
| 203 | const env = yield* GcpEnvironment.current; |
| 204 | return yield* forEachApp( |
| 205 | env.project, |
| 206 | (parent) => listAt(parent, env.project, env.region), |
| 207 | env.region, |
| 208 | ); |
| 209 | }), |
| 210 | |
| 211 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 212 | const env = yield* GcpEnvironment.current; |
| 213 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 214 | const app = expandApp(news.app, env.project, location); |
| 215 | const exampleId = yield* toPhysicalId(id, news.exampleId, output?.exampleId); |
| 216 | const name = output?.name ?? resourceName(app, exampleId); |
| 217 | const ownership = yield* createInternalLabels(id); |
| 218 | const description = encodeOwnership(ownership, news.description); |
| 219 | const displayName = news.displayName ?? exampleId; |
| 220 | |
| 221 | let current = yield* getByName(name); |
| 222 | |
| 223 | if (current === undefined) { |
| 224 | const created = yield* retryTransient( |
| 225 | ces.createProjectsLocationsAppsExamples({ |
| 226 | parent: app, |
| 227 | exampleId, |
| 228 | body: { |
| 229 | displayName, |
| 230 | description, |
| 231 | entryAgent: news.entryAgent, |
| 232 | messages: news.messages, |
| 233 | }, |
| 234 | }), |
| 235 | ).pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 236 | current = created ?? undefined; |
| 237 | } |
| 238 | |
| 239 | if (current === undefined) { |
| 240 | return yield* new AppsExampleNotResolved({ resourceName: name }); |
| 241 | } |
| 242 | |
| 243 | const currentName = current.name ?? name; |
| 244 | const displayChanged = !sameText(current.displayName, displayName); |
| 245 | const descriptionChanged = !sameText(current.description, description); |
| 246 | const entryChanged = !sameText(current.entryAgent, news.entryAgent); |
| 247 | const messagesChanged = !sameJson(current.messages, news.messages); |
| 248 | |
| 249 | if (displayChanged || descriptionChanged || entryChanged || messagesChanged) { |
| 250 | current = yield* retryTransient( |
| 251 | ces.patchProjectsLocationsAppsExamples({ |
| 252 | name: currentName, |
| 253 | updateMask: updateMaskOf( |
| 254 | displayChanged ? "display_name" : undefined, |
| 255 | descriptionChanged ? "description" : undefined, |
| 256 | entryChanged ? "entry_agent" : undefined, |
| 257 | messagesChanged ? "messages" : undefined, |
| 258 | ), |
| 259 | body: { |
| 260 | displayName, |
| 261 | description, |
| 262 | entryAgent: news.entryAgent, |
| 263 | messages: news.messages, |
| 264 | }, |
| 265 | }), |
| 266 | ); |
| 267 | } |
| 268 | |
| 269 | return toAttrs(current, env.project, env.region, app); |
| 270 | }), |
| 271 | |
| 272 | delete: Effect.fn(function* ({ output }) { |
| 273 | if (!output.name) return; |
| 274 | yield* retryTransient(ces.deleteProjectsLocationsAppsExamples({ name: output.name })).pipe( |
| 275 | Effect.catchTag("NotFound", () => Effect.void), |
| 276 | ); |
| 277 | yield* waitUntilGone(getByName(output.name)); |
| 278 | }), |
| 279 | }); |