@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 203 lines 6.34 KB
Raw
1 import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9 import { expandDataStore, parseResourceName } from "./internal.ts";
10
11 export type DataStoresConversationProps = {
12 /**
13 * Parent Data Store resource name
14 * `projects/{project}/locations/{location}/dataStores/{dataStore}`.
15 * Immutable — changing it replaces the conversation.
16 */
17 dataStore: string;
18 /**
19 * Unique identifier for tracking users.
20 */
21 userPseudoId?: string;
22 /**
23 * Conversation state (`IN_PROGRESS`, `COMPLETED`).
24 */
25 state?: string;
26 };
27
28 export type DataStoresConversation = Resource<
29 "GCP.DiscoveryEngine.DataStoresConversation",
30 DataStoresConversationProps,
31 {
32 /** Full resource name `.../dataStores/{dataStore}/conversations/{id}`. */
33 name: string;
34 /** Conversation id (last path segment). Server-assigned on create. */
35 conversationId: string;
36 /** Parent data store resource name. */
37 dataStore: string;
38 /** Project id. */
39 project: string;
40 /** Location id. */
41 location: string;
42 /** Pseudo id. */
43 userPseudoId: string | undefined;
44 /** Conversation state. */
45 state: string | undefined;
46 /** RFC3339 start timestamp. */
47 startTime: string | undefined;
48 /** RFC3339 end timestamp. */
49 endTime: string | undefined;
50 },
51 never,
52 Providers
53 >;
54
55 /**
56 * A Vertex AI Search Conversation attached to a Data Store.
57 *
58 * Its id is assigned by the API, so only a recorded resource can be read
59 * back. The conversation id is server-assigned. Parent is immutable; user id
60 * and state update in place.
61 *
62 * ### Creating a Conversation
63 * **Example:** Start a conversation
64 * ```typescript
65 * const conversation = yield* GCP.DiscoveryEngine.DataStoresConversation(
66 * "Support",
67 * { dataStore: dataStore.name },
68 * );
69 * ```
70 *
71 * ### Updating a Conversation
72 * **Example:** Mark completed
73 * ```typescript
74 * // Same logical id as before; only the changed props differ.
75 * const conversation = yield* GCP.DiscoveryEngine.DataStoresConversation(
76 * "Support",
77 * {
78 * dataStore: dataStore.name,
79 * state: "COMPLETED",
80 * },
81 * );
82 * ```
83 *
84 * @resource
85 * @category DiscoveryEngine
86 */
87 export const DataStoresConversation = Resource<DataStoresConversation>(
88 "GCP.DiscoveryEngine.DataStoresConversation",
89 );
90
91 export class DataStoresConversationNotResolved extends Data.TaggedError(
92 "GCP.DiscoveryEngine.DataStoresConversationNotResolved",
93 )<{
94 resourceName: string;
95 }> {}
96
97 const toAttrs = (
98 conversation: discoveryengine.GoogleCloudDiscoveryengineV1Conversation,
99 project: string,
100 ) => {
101 const name = conversation.name ?? "";
102 const parsed = parseResourceName(name, "conversations");
103 return {
104 name,
105 conversationId: parsed.id,
106 dataStore: parsed.dataStore,
107 project: parsed.project || project,
108 location: parsed.location,
109 userPseudoId: conversation.userPseudoId,
110 state: conversation.state,
111 startTime: conversation.startTime,
112 endTime: conversation.endTime,
113 };
114 };
115
116 const getByName = (name: string) =>
117 name.length === 0
118 ? Effect.undefined
119 : discoveryengine
120 .getProjectsLocationsDataStoresConversations({ name })
121 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
122
123 export const DataStoresConversationProvider = () =>
124 Provider.succeed(DataStoresConversation, {
125 stables: ["name", "conversationId", "dataStore", "project", "location", "startTime"],
126
127 diff: Effect.fn(function* ({ news, olds, output }) {
128 if (!isResolved(news)) return undefined;
129 const previousParent = olds?.dataStore ?? output?.dataStore;
130 if (previousParent !== undefined && news.dataStore !== previousParent) {
131 return { action: "replace" as const, deleteFirst: false };
132 }
133 return undefined;
134 }),
135
136 read: Effect.fn(function* ({ output }) {
137 const env = yield* GcpEnvironment.current;
138 // Server-assigned id: only a recorded name can be observed.
139 if (output?.name === undefined) return undefined;
140 const existing = yield* getByName(output.name);
141 if (existing === undefined) return undefined;
142 return toAttrs(existing, env.project);
143 }),
144
145 reconcile: Effect.fn(function* ({ news, output }) {
146 const env = yield* GcpEnvironment.current;
147 const parent = expandDataStore(news.dataStore, env.project, output?.location ?? "global");
148 const userPseudoId = news.userPseudoId;
149
150 let current = yield* getByName(output?.name ?? "");
151
152 if (current === undefined) {
153 const created = yield* discoveryengine
154 .createProjectsLocationsDataStoresConversations({
155 parent,
156 body: {
157 userPseudoId,
158 state: news.state,
159 },
160 })
161 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
162 current = created ?? undefined;
163 }
164
165 if (current === undefined) {
166 return yield* new DataStoresConversationNotResolved({
167 resourceName: output?.name ?? `${parent}/conversations`,
168 });
169 }
170
171 const userChanged = userPseudoId !== undefined && current.userPseudoId !== userPseudoId;
172 const stateChanged = news.state !== undefined && (current.state ?? "") !== news.state;
173
174 if (userChanged || stateChanged) {
175 current = yield* discoveryengine.patchProjectsLocationsDataStoresConversations({
176 name: current.name ?? "",
177 updateMask: [
178 userChanged ? "user_pseudo_id" : undefined,
179 stateChanged ? "state" : undefined,
180 ]
181 .filter((field): field is string => field !== undefined)
182 .join(","),
183 body: {
184 name: current.name,
185 userPseudoId,
186 state: news.state,
187 },
188 });
189 }
190
191 return toAttrs(current, env.project);
192 }),
193
194 delete: Effect.fn(function* ({ output }) {
195 const existing = yield* getByName(output.name);
196 if (existing === undefined) return;
197 yield* discoveryengine
198 .deleteProjectsLocationsDataStoresConversations({
199 name: output.name,
200 })
201 .pipe(Effect.catchTag("NotFound", () => Effect.void));
202 }),
203 });