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, 04:45 UTC
| 1 | import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_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 { parentOf, parseResourceName, sessionIdOf, toPhysical } from "./internal.ts"; |
| 11 | |
| 12 | export type CollectionsDataStoresSessionProps = { |
| 13 | /** |
| 14 | * Parent data store resource name. Immutable — changing it replaces |
| 15 | * the session. |
| 16 | */ |
| 17 | dataStore: string; |
| 18 | /** |
| 19 | * Session id (`[a-z0-9]`, 1-63 characters). If omitted, a unique id is |
| 20 | * generated. Immutable — changing it replaces the session. |
| 21 | */ |
| 22 | sessionId?: string; |
| 23 | /** |
| 24 | * Display name used in the UI (max 128 characters). |
| 25 | */ |
| 26 | displayName?: string; |
| 27 | /** |
| 28 | * Session state. |
| 29 | * @default "IN_PROGRESS" |
| 30 | */ |
| 31 | state?: "STATE_UNSPECIFIED" | "IN_PROGRESS" | (string & {}); |
| 32 | /** |
| 33 | * End-user id. |
| 34 | */ |
| 35 | userPseudoId?: string; |
| 36 | /** |
| 37 | * Session labels. |
| 38 | */ |
| 39 | labels?: string[]; |
| 40 | /** |
| 41 | * Pin the session to the top of the session list. |
| 42 | * @default false |
| 43 | */ |
| 44 | isPinned?: boolean; |
| 45 | }; |
| 46 | |
| 47 | export type CollectionsDataStoresSession = Resource< |
| 48 | "GCP.DiscoveryEngine.CollectionsDataStoresSession", |
| 49 | CollectionsDataStoresSessionProps, |
| 50 | { |
| 51 | /** Full resource name. */ |
| 52 | name: string; |
| 53 | /** Session id. */ |
| 54 | sessionId: string; |
| 55 | /** Parent data store resource name. */ |
| 56 | dataStore: string; |
| 57 | /** Project id. */ |
| 58 | project: string; |
| 59 | /** Location id. */ |
| 60 | location: string; |
| 61 | /** Display name. */ |
| 62 | displayName: string | undefined; |
| 63 | /** Session state. */ |
| 64 | state: string | undefined; |
| 65 | /** End-user id. */ |
| 66 | userPseudoId: string | undefined; |
| 67 | /** Labels. */ |
| 68 | labels: string[]; |
| 69 | /** Whether the session is pinned. */ |
| 70 | isPinned: boolean; |
| 71 | /** RFC3339 start time. */ |
| 72 | startTime: string | undefined; |
| 73 | /** RFC3339 end time. */ |
| 74 | endTime: string | undefined; |
| 75 | }, |
| 76 | never, |
| 77 | Providers |
| 78 | >; |
| 79 | |
| 80 | /** |
| 81 | * A Discovery Engine session on a collection data store. |
| 82 | * |
| 83 | * Without labels, ownership rests on the deterministic id: `read` reports a |
| 84 | * resource it finds without prior state as unowned (adopt it with `--adopt`). |
| 85 | * Parent and session id are immutable. |
| 86 | * |
| 87 | * ### Creating a Session |
| 88 | * **Example:** Pinned session |
| 89 | * ```typescript |
| 90 | * const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Docs", {}); |
| 91 | * const session = yield* GCP.DiscoveryEngine.CollectionsDataStoresSession( |
| 92 | * "Chat", |
| 93 | * { |
| 94 | * dataStore: store.name, |
| 95 | * displayName: "support chat", |
| 96 | * isPinned: true, |
| 97 | * }, |
| 98 | * ); |
| 99 | * ``` |
| 100 | * |
| 101 | * @resource |
| 102 | * @category DiscoveryEngine |
| 103 | */ |
| 104 | export const CollectionsDataStoresSession = Resource<CollectionsDataStoresSession>( |
| 105 | "GCP.DiscoveryEngine.CollectionsDataStoresSession", |
| 106 | ); |
| 107 | |
| 108 | export class CollectionsDataStoresSessionNotResolved extends Data.TaggedError( |
| 109 | "GCP.DiscoveryEngine.CollectionsDataStoresSessionNotResolved", |
| 110 | )<{ |
| 111 | resourceName: string; |
| 112 | }> {} |
| 113 | |
| 114 | const toAttrs = (session: discoveryengine.GoogleCloudDiscoveryengineV1Session, project: string) => { |
| 115 | const name = session.name ?? ""; |
| 116 | const parsed = parseResourceName(name, "sessions"); |
| 117 | return { |
| 118 | name, |
| 119 | sessionId: parsed.id, |
| 120 | dataStore: parentOf(name, "sessions"), |
| 121 | project: parsed.project || project, |
| 122 | location: parsed.location, |
| 123 | displayName: session.displayName, |
| 124 | state: session.state, |
| 125 | userPseudoId: session.userPseudoId, |
| 126 | labels: [...(session.labels ?? [])], |
| 127 | isPinned: session.isPinned === true, |
| 128 | startTime: session.startTime, |
| 129 | endTime: session.endTime, |
| 130 | }; |
| 131 | }; |
| 132 | |
| 133 | const resourceName = (dataStore: string, sessionId: string) => `${dataStore}/sessions/${sessionId}`; |
| 134 | |
| 135 | const getByName = (name: string) => |
| 136 | name.length === 0 |
| 137 | ? Effect.undefined |
| 138 | : discoveryengine |
| 139 | .getProjectsLocationsCollectionsDataStoresSessions({ name }) |
| 140 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 141 | |
| 142 | export const CollectionsDataStoresSessionProvider = () => |
| 143 | Provider.succeed(CollectionsDataStoresSession, { |
| 144 | stables: ["name", "sessionId", "dataStore", "project", "location", "startTime"], |
| 145 | |
| 146 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 147 | if (!isResolved(news)) return undefined; |
| 148 | const previousParent = olds?.dataStore ?? output?.dataStore; |
| 149 | const previousId = olds?.sessionId ?? output?.sessionId; |
| 150 | if ( |
| 151 | (previousParent !== undefined && news.dataStore !== previousParent) || |
| 152 | (previousId !== undefined && news.sessionId !== undefined && news.sessionId !== previousId) |
| 153 | ) { |
| 154 | return { |
| 155 | action: "replace" as const, |
| 156 | deleteFirst: |
| 157 | previousParent === news.dataStore && |
| 158 | previousId !== undefined && |
| 159 | news.sessionId === previousId, |
| 160 | }; |
| 161 | } |
| 162 | return undefined; |
| 163 | }), |
| 164 | |
| 165 | read: Effect.fn(function* ({ id, olds, output }) { |
| 166 | const env = yield* GcpEnvironment.current; |
| 167 | const parent = olds?.dataStore ?? output?.dataStore; |
| 168 | const childId = yield* toPhysical(id, olds?.sessionId, output?.sessionId, sessionIdOf); |
| 169 | const name = |
| 170 | output?.name ?? (parent !== undefined ? resourceName(parent, childId) : undefined); |
| 171 | if (name === undefined) return undefined; |
| 172 | const existing = yield* getByName(name); |
| 173 | if (existing === undefined) return undefined; |
| 174 | const attrs = toAttrs(existing, env.project); |
| 175 | // No labels field: without prior state it may not be ours. |
| 176 | return output !== undefined ? attrs : Unowned(attrs); |
| 177 | }), |
| 178 | |
| 179 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 180 | const env = yield* GcpEnvironment.current; |
| 181 | const sessionId = yield* toPhysical(id, news.sessionId, output?.sessionId, sessionIdOf); |
| 182 | const name = resourceName(news.dataStore, sessionId); |
| 183 | const displayName = news.displayName ?? sessionId; |
| 184 | const labels = news.labels ?? []; |
| 185 | const state = news.state ?? "IN_PROGRESS"; |
| 186 | const desiredPinned = news.isPinned === true; |
| 187 | |
| 188 | let current = yield* getByName(output?.name ?? name); |
| 189 | |
| 190 | if (current === undefined) { |
| 191 | const created = yield* discoveryengine |
| 192 | .createProjectsLocationsCollectionsDataStoresSessions({ |
| 193 | parent: news.dataStore, |
| 194 | sessionId, |
| 195 | body: { |
| 196 | displayName, |
| 197 | state, |
| 198 | userPseudoId: news.userPseudoId, |
| 199 | labels, |
| 200 | isPinned: desiredPinned ? true : undefined, |
| 201 | }, |
| 202 | }) |
| 203 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 204 | current = created ?? undefined; |
| 205 | } |
| 206 | |
| 207 | if (current === undefined) { |
| 208 | return yield* new CollectionsDataStoresSessionNotResolved({ resourceName: name }); |
| 209 | } |
| 210 | |
| 211 | const resource = current.name ?? name; |
| 212 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 213 | const stateChanged = (current.state ?? "") !== state; |
| 214 | const userChanged = (current.userPseudoId ?? "") !== (news.userPseudoId ?? ""); |
| 215 | const observedLabels = [...(current.labels ?? [])].sort().join("\0"); |
| 216 | const desiredLabels = [...labels].sort().join("\0"); |
| 217 | const labelsChanged = observedLabels !== desiredLabels; |
| 218 | const pinnedChanged = (current.isPinned === true) !== desiredPinned; |
| 219 | |
| 220 | if (displayNameChanged || stateChanged || userChanged || labelsChanged || pinnedChanged) { |
| 221 | current = yield* discoveryengine.patchProjectsLocationsCollectionsDataStoresSessions({ |
| 222 | name: resource, |
| 223 | updateMask: [ |
| 224 | displayNameChanged ? "display_name" : undefined, |
| 225 | stateChanged ? "state" : undefined, |
| 226 | userChanged ? "user_pseudo_id" : undefined, |
| 227 | labelsChanged ? "labels" : undefined, |
| 228 | pinnedChanged ? "is_pinned" : undefined, |
| 229 | ] |
| 230 | .filter((field): field is string => field !== undefined) |
| 231 | .join(","), |
| 232 | body: { |
| 233 | name: resource, |
| 234 | displayName, |
| 235 | state, |
| 236 | userPseudoId: news.userPseudoId, |
| 237 | labels, |
| 238 | isPinned: desiredPinned, |
| 239 | }, |
| 240 | }); |
| 241 | } |
| 242 | |
| 243 | return toAttrs(current, env.project); |
| 244 | }), |
| 245 | |
| 246 | delete: Effect.fn(function* ({ output }) { |
| 247 | yield* discoveryengine |
| 248 | .deleteProjectsLocationsCollectionsDataStoresSessions({ |
| 249 | name: output.name, |
| 250 | }) |
| 251 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 252 | }), |
| 253 | }); |