The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as vault from "@distilled.cloud/gcp/vault_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 { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | encodeOwnershipLine, |
| 12 | findOwnedSavedQuery, |
| 13 | findSavedQueryByName, |
| 14 | getSavedQuery, |
| 15 | hasOwnershipMarker, |
| 16 | ignoreMissing, |
| 17 | jsonEqual, |
| 18 | listActiveMatters, |
| 19 | listSavedQueries, |
| 20 | MAX_SAVED_QUERY_NAME_LENGTH, |
| 21 | ownedByAlchemy, |
| 22 | ownershipLabels, |
| 23 | parseOwnership, |
| 24 | toGeneratedName, |
| 25 | } from "./internal.ts"; |
| 26 | |
| 27 | export type MattersSavedQueryProps = { |
| 28 | /** |
| 29 | * Parent matter id. Immutable — changing it replaces the saved query. |
| 30 | */ |
| 31 | matterId: string; |
| 32 | /** |
| 33 | * Server-assigned saved query id. Leave blank on create. Immutable — |
| 34 | * changing it replaces the saved query. |
| 35 | */ |
| 36 | savedQueryId?: string; |
| 37 | /** |
| 38 | * Display name (max 100 characters including Alchemy's ownership |
| 39 | * marker). Vault saved queries have no labels field, so ownership is |
| 40 | * stored in a `[alchemy …]` prefix and stripped from attributes. Vault |
| 41 | * has no saved-query update API — changing the name replaces the |
| 42 | * query. |
| 43 | */ |
| 44 | displayName?: string; |
| 45 | /** |
| 46 | * Search parameters. Required on create. Vault has no saved-query |
| 47 | * update API — changing the query replaces the saved query. |
| 48 | */ |
| 49 | query: vault.Query; |
| 50 | }; |
| 51 | |
| 52 | export type MattersSavedQuery = Resource< |
| 53 | "GCP.Vault.MattersSavedQuery", |
| 54 | MattersSavedQueryProps, |
| 55 | { |
| 56 | /** Server-assigned saved query id. */ |
| 57 | savedQueryId: string; |
| 58 | /** Parent matter id. */ |
| 59 | matterId: string; |
| 60 | /** Project id used when the saved query was reconciled. */ |
| 61 | project: string; |
| 62 | /** User-facing name with the Alchemy ownership prefix stripped. */ |
| 63 | displayName: string | undefined; |
| 64 | /** Search parameters. */ |
| 65 | query: vault.Query | undefined; |
| 66 | /** RFC3339 create timestamp. */ |
| 67 | createTime: string | undefined; |
| 68 | }, |
| 69 | never, |
| 70 | Providers |
| 71 | >; |
| 72 | |
| 73 | /** |
| 74 | * A Google Vault saved query on a matter. |
| 75 | * |
| 76 | * Vault saved queries have no labels field, so Alchemy stamps ownership |
| 77 | * into `displayName` for `list` / nuke. There is no update API — changing |
| 78 | * the query or display name replaces the saved query (delete-first). |
| 79 | * |
| 80 | * ### Creating a Saved Query |
| 81 | * **Example:** Mail search |
| 82 | * ```typescript |
| 83 | * const saved = yield* GCP.Vault.MattersSavedQuery("Contracts", { |
| 84 | * matterId: matter.matterId, |
| 85 | * query: { |
| 86 | * corpus: "MAIL", |
| 87 | * dataScope: "ALL_DATA", |
| 88 | * searchMethod: "ACCOUNT", |
| 89 | * accountInfo: { emails: ["user@example.com"] }, |
| 90 | * terms: "subject:contract", |
| 91 | * }, |
| 92 | * }); |
| 93 | * ``` |
| 94 | * |
| 95 | * **Example:** Named Drive search |
| 96 | * ```typescript |
| 97 | * const saved = yield* GCP.Vault.MattersSavedQuery("Drive", { |
| 98 | * matterId: matter.matterId, |
| 99 | * displayName: "shared-drives", |
| 100 | * query: { |
| 101 | * corpus: "DRIVE", |
| 102 | * dataScope: "ALL_DATA", |
| 103 | * searchMethod: "ENTIRE_ORG", |
| 104 | * }, |
| 105 | * }); |
| 106 | * ``` |
| 107 | * |
| 108 | * @resource |
| 109 | * @category Vault |
| 110 | */ |
| 111 | export const MattersSavedQuery = Resource<MattersSavedQuery>("GCP.Vault.MattersSavedQuery"); |
| 112 | |
| 113 | export class MattersSavedQueryNotResolved extends Data.TaggedError( |
| 114 | "GCP.Vault.MattersSavedQueryNotResolved", |
| 115 | )<{ |
| 116 | matterId: string; |
| 117 | savedQueryId: string; |
| 118 | }> {} |
| 119 | |
| 120 | const toAttrs = (item: vault.SavedQuery, project: string) => ({ |
| 121 | savedQueryId: item.savedQueryId ?? "", |
| 122 | matterId: item.matterId ?? "", |
| 123 | project, |
| 124 | displayName: parseOwnership(item.displayName).text, |
| 125 | query: item.query, |
| 126 | createTime: item.createTime, |
| 127 | }); |
| 128 | |
| 129 | export const MattersSavedQueryProvider = () => |
| 130 | Provider.succeed(MattersSavedQuery, { |
| 131 | stables: ["savedQueryId", "matterId", "project", "createTime"], |
| 132 | |
| 133 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 134 | if (!isResolved(news)) return undefined; |
| 135 | const previousMatter = olds?.matterId ?? output?.matterId; |
| 136 | if (previousMatter !== undefined && news.matterId !== previousMatter) { |
| 137 | return { action: "replace" as const, deleteFirst: true }; |
| 138 | } |
| 139 | const previousId = olds?.savedQueryId ?? output?.savedQueryId; |
| 140 | if ( |
| 141 | previousId !== undefined && |
| 142 | news.savedQueryId !== undefined && |
| 143 | news.savedQueryId !== previousId |
| 144 | ) { |
| 145 | return { action: "replace" as const, deleteFirst: true }; |
| 146 | } |
| 147 | const previousName = olds?.displayName ?? output?.displayName; |
| 148 | if ( |
| 149 | news.displayName !== undefined && |
| 150 | previousName !== undefined && |
| 151 | news.displayName !== previousName |
| 152 | ) { |
| 153 | return { action: "replace" as const, deleteFirst: true }; |
| 154 | } |
| 155 | const previousQuery = olds?.query ?? output?.query; |
| 156 | if (previousQuery !== undefined && !jsonEqual(previousQuery, news.query)) { |
| 157 | return { action: "replace" as const, deleteFirst: true }; |
| 158 | } |
| 159 | return undefined; |
| 160 | }), |
| 161 | |
| 162 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 163 | const env = yield* GcpEnvironment.current; |
| 164 | const matterId = olds?.matterId ?? output?.matterId ?? ""; |
| 165 | const savedQueryId = olds?.savedQueryId ?? output?.savedQueryId ?? ""; |
| 166 | let existing = yield* getSavedQuery(matterId, savedQueryId); |
| 167 | if (existing === undefined) { |
| 168 | const ownership = yield* ownershipLabels(id); |
| 169 | const displayName = encodeOwnershipLine( |
| 170 | ownership, |
| 171 | olds?.displayName ?? output?.displayName, |
| 172 | MAX_SAVED_QUERY_NAME_LENGTH, |
| 173 | ); |
| 174 | existing = yield* findSavedQueryByName(matterId, displayName); |
| 175 | } |
| 176 | if (existing === undefined) { |
| 177 | existing = yield* findOwnedSavedQuery(id, matterId); |
| 178 | } |
| 179 | if (existing === undefined) return undefined; |
| 180 | const attrs = toAttrs(existing, env.project); |
| 181 | return (yield* ownedByAlchemy(id, existing.displayName)) |
| 182 | ? attrs |
| 183 | : unownedUnlessPrevious(attrs, previous, "savedQueryId"); |
| 184 | }), |
| 185 | |
| 186 | list: () => |
| 187 | Effect.gen(function* () { |
| 188 | const env = yield* GcpEnvironment.current; |
| 189 | const matters = yield* listActiveMatters(); |
| 190 | const pages = yield* Effect.forEach( |
| 191 | matters, |
| 192 | (matter) => |
| 193 | listSavedQueries(matter.matterId ?? "").pipe( |
| 194 | Effect.map((items) => |
| 195 | items |
| 196 | .filter((item) => hasOwnershipMarker(item.displayName)) |
| 197 | .map((item) => toAttrs(item, env.project)), |
| 198 | ), |
| 199 | ), |
| 200 | { concurrency: 4 }, |
| 201 | ); |
| 202 | return pages.flat(); |
| 203 | }), |
| 204 | |
| 205 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 206 | const env = yield* GcpEnvironment.current; |
| 207 | const matterId = news.matterId; |
| 208 | const ownership = yield* ownershipLabels(id); |
| 209 | const generated = yield* toGeneratedName(id, news.displayName, output?.displayName, 40); |
| 210 | const displayName = encodeOwnershipLine(ownership, generated, MAX_SAVED_QUERY_NAME_LENGTH); |
| 211 | const desired: vault.SavedQuery = { |
| 212 | displayName, |
| 213 | query: news.query, |
| 214 | }; |
| 215 | |
| 216 | let current = yield* getSavedQuery(matterId, news.savedQueryId ?? output?.savedQueryId ?? ""); |
| 217 | if (current === undefined) { |
| 218 | current = yield* findSavedQueryByName(matterId, displayName); |
| 219 | } |
| 220 | if (current === undefined) { |
| 221 | current = yield* findOwnedSavedQuery(id, matterId); |
| 222 | } |
| 223 | |
| 224 | if (current === undefined) { |
| 225 | const created = yield* vault |
| 226 | .createMattersSavedQueries({ |
| 227 | matterId, |
| 228 | body: desired, |
| 229 | }) |
| 230 | .pipe(Effect.catchTag("Conflict", () => findSavedQueryByName(matterId, displayName))); |
| 231 | current = created ?? undefined; |
| 232 | } |
| 233 | |
| 234 | if (current === undefined) { |
| 235 | return yield* new MattersSavedQueryNotResolved({ |
| 236 | matterId, |
| 237 | savedQueryId: news.savedQueryId ?? output?.savedQueryId ?? displayName, |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | return toAttrs(current, env.project); |
| 242 | }), |
| 243 | |
| 244 | delete: Effect.fn(function* ({ output }) { |
| 245 | if (output.matterId.length === 0 || output.savedQueryId.length === 0) { |
| 246 | return; |
| 247 | } |
| 248 | yield* ignoreMissing( |
| 249 | vault.deleteMattersSavedQueries({ |
| 250 | matterId: output.matterId, |
| 251 | savedQueryId: output.savedQueryId, |
| 252 | }), |
| 253 | ); |
| 254 | }), |
| 255 | }); |