The best way to control Claude Code, Codex and more from any computer or phone

main
ts 286 lines 8.81 KB
Raw
1 #!/usr/bin/env node
2
3 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import * as NodeOS from "node:os";
6 import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
7 import * as Console from "effect/Console";
8 import * as DateTime from "effect/DateTime";
9 import * as Effect from "effect/Effect";
10 import * as FileSystem from "effect/FileSystem";
11 import * as Option from "effect/Option";
12 import * as Path from "effect/Path";
13 import * as Schema from "effect/Schema";
14 import * as SqlClient from "effect/sql/SqlClient";
15 import { Argument, Command, Flag } from "effect/cli";
16
17 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
18
19 export const SqliteStateOperation = Schema.Literals(["query", "exec"]);
20 export type SqliteStateOperation = typeof SqliteStateOperation.Type;
21
22 export class SqliteStateMultipleSqlSourcesError extends Schema.TaggedError<SqliteStateMultipleSqlSourcesError>()(
23 "SqliteStateMultipleSqlSourcesError",
24 {},
25 ) {
26 override get message(): string {
27 return "Provide only one of --sql or --file.";
28 }
29 }
30
31 export class SqliteStateMissingSqlSourceError extends Schema.TaggedError<SqliteStateMissingSqlSourceError>()(
32 "SqliteStateMissingSqlSourceError",
33 {},
34 ) {
35 override get message(): string {
36 return "Provide one of --sql or --file.";
37 }
38 }
39
40 export class SqliteStateEmptySqlError extends Schema.TaggedError<SqliteStateEmptySqlError>()(
41 "SqliteStateEmptySqlError",
42 {},
43 ) {
44 override get message(): string {
45 return "SQL input is empty.";
46 }
47 }
48
49 export class SqliteStateDatabaseMissingError extends Schema.TaggedError<SqliteStateDatabaseMissingError>()(
50 "SqliteStateDatabaseMissingError",
51 {
52 databasePath: Schema.String,
53 },
54 ) {
55 override get message(): string {
56 return `Database does not exist at '${this.databasePath}'. Start T3 once to run migrations.`;
57 }
58 }
59
60 export class SqliteStateSharedHomeMutationError extends Schema.TaggedError<SqliteStateSharedHomeMutationError>()(
61 "SqliteStateSharedHomeMutationError",
62 {},
63 ) {
64 override get message(): string {
65 return "Refusing to mutate the shared ~/.t3 database. Use an isolated --base-dir.";
66 }
67 }
68
69 export class SqliteStateSqlFileError extends Schema.TaggedError<SqliteStateSqlFileError>()(
70 "SqliteStateSqlFileError",
71 {
72 filePath: Schema.String,
73 cause: Schema.Defect(),
74 },
75 ) {
76 override get message(): string {
77 return `Failed to read SQL from '${this.filePath}'.`;
78 }
79 }
80
81 export class SqliteStateDatabaseError extends Schema.TaggedError<SqliteStateDatabaseError>()(
82 "SqliteStateDatabaseError",
83 {
84 operation: SqliteStateOperation,
85 databasePath: Schema.String,
86 cause: Schema.Defect(),
87 },
88 ) {
89 override get message(): string {
90 return `Failed to ${this.operation} SQLite database at '${this.databasePath}'.`;
91 }
92 }
93
94 const SqliteStateValue = Schema.Union([
95 Schema.Null,
96 Schema.String,
97 Schema.Number,
98 Schema.Array(Schema.Number),
99 ]);
100 const SqliteStateRow = Schema.Record(Schema.String, SqliteStateValue);
101 const SqliteStateQueryResult = Schema.Struct({
102 operation: Schema.Literal("query"),
103 database: Schema.String,
104 rows: Schema.Array(SqliteStateRow),
105 });
106 const SqliteStateExecResult = Schema.Struct({
107 operation: Schema.Literal("exec"),
108 database: Schema.String,
109 backup: Schema.String,
110 });
111 const SqliteStateResult = Schema.Union([SqliteStateQueryResult, SqliteStateExecResult]);
112 const encodeSqliteStateResult = Schema.encodeEffect(fromJsonStringPretty(SqliteStateResult));
113
114 export type SqliteStateResult = typeof SqliteStateResult.Type;
115
116 type RawSqliteValue = null | string | number | bigint | Uint8Array;
117 type RawSqliteRow = Readonly<Record<string, RawSqliteValue>>;
118
119 export interface RunSqliteStateInput {
120 readonly operation: SqliteStateOperation;
121 readonly baseDir: string;
122 readonly sql?: string | undefined;
123 readonly file?: string | undefined;
124 }
125
126 export interface RunSqliteStateOptions {
127 readonly sharedHome?: string | undefined;
128 }
129
130 const resolveSqlSource = Effect.fn("resolveSqliteStateSqlSource")(function* (
131 sql: string | undefined,
132 file: string | undefined,
133 ) {
134 if (sql !== undefined && file !== undefined) {
135 return yield* new SqliteStateMultipleSqlSourcesError();
136 }
137 if (sql === undefined && file === undefined) {
138 return yield* new SqliteStateMissingSqlSourceError();
139 }
140
141 const fs = yield* FileSystem.FileSystem;
142 const path = yield* Path.Path;
143 let source: string;
144 if (sql !== undefined) {
145 source = sql;
146 } else {
147 const filePath = path.resolve(file as string);
148 source = yield* fs
149 .readFileString(filePath)
150 .pipe(Effect.mapError((cause) => new SqliteStateSqlFileError({ filePath, cause })));
151 }
152
153 const trimmed = source.trim();
154 if (trimmed.length === 0) {
155 return yield* new SqliteStateEmptySqlError();
156 }
157 return trimmed;
158 });
159
160 function normalizeSqliteValue(value: RawSqliteValue): typeof SqliteStateValue.Type {
161 if (typeof value === "bigint") {
162 const numericValue = Number(value);
163 return Number.isSafeInteger(numericValue) ? numericValue : value.toString();
164 }
165 if (value instanceof Uint8Array) {
166 return Array.from(value);
167 }
168 return value;
169 }
170
171 function normalizeSqliteRow(row: RawSqliteRow): typeof SqliteStateRow.Type {
172 return Object.fromEntries(
173 Object.entries(row).map(([key, value]) => [key, normalizeSqliteValue(value)]),
174 );
175 }
176
177 export const runSqliteState = Effect.fn("runSqliteState")(function* (
178 input: RunSqliteStateInput,
179 options: RunSqliteStateOptions = {},
180 ) {
181 const fs = yield* FileSystem.FileSystem;
182 const path = yield* Path.Path;
183 const baseDir = path.resolve(input.baseDir);
184 const sharedHome = path.resolve(options.sharedHome ?? path.join(NodeOS.homedir(), ".t3"));
185 const databasePath = path.join(baseDir, "userdata", "statev2.sqlite");
186 const source = yield* resolveSqlSource(input.sql, input.file);
187
188 if (!(yield* fs.exists(databasePath))) {
189 return yield* new SqliteStateDatabaseMissingError({ databasePath });
190 }
191 if (input.operation === "exec") {
192 const [canonicalBaseDir, canonicalSharedHome] = yield* Effect.all([
193 fs.realPath(baseDir),
194 fs.realPath(sharedHome).pipe(Effect.orElseSucceed(() => sharedHome)),
195 ]);
196 if (canonicalBaseDir === canonicalSharedHome) {
197 return yield* new SqliteStateSharedHomeMutationError();
198 }
199 }
200
201 const program = Effect.gen(function* () {
202 const sql = yield* SqlClient.SqlClient;
203 yield* sql.unsafe("PRAGMA busy_timeout = 5000").unprepared;
204 // This connection can checkpoint statev2.sqlite, so it needs Sqlite.ts's F_FULLFSYNC barrier.
205 yield* sql.unsafe("PRAGMA checkpoint_fullfsync = ON").unprepared;
206
207 if (input.operation === "query") {
208 const rows = yield* sql.unsafe<RawSqliteRow>(source).unprepared.pipe(
209 Effect.provideService(SqlClient.SafeIntegers, true),
210 Effect.map((rows) => rows.map(normalizeSqliteRow)),
211 );
212 return {
213 operation: "query",
214 database: databasePath,
215 rows,
216 } as const;
217 }
218
219 const timestamp = DateTime.formatIso(yield* DateTime.now).replaceAll(":", "-");
220 const backupPath = `${databasePath}.backup-${timestamp}`;
221 yield* sql`VACUUM INTO ${backupPath}`;
222 yield* fs.chmod(backupPath, 0o600);
223 yield* sql.withTransaction(sql.unsafe(source).unprepared);
224
225 return {
226 operation: "exec",
227 database: databasePath,
228 backup: backupPath,
229 } as const;
230 });
231
232 return yield* program.pipe(
233 Effect.provide(
234 NodeSqliteClient.layer({
235 filename: databasePath,
236 readonly: input.operation === "query",
237 }),
238 ),
239 Effect.mapError(
240 (cause) =>
241 new SqliteStateDatabaseError({
242 operation: input.operation,
243 databasePath,
244 cause,
245 }),
246 ),
247 );
248 });
249
250 const t3SqliteStateCommand = Command.make(
251 "t3-sqlite-state",
252 {
253 operation: Argument.Literals("operation", SqliteStateOperation.literals).pipe(
254 Argument.withDescription("Run a read-only query or a backed-up fixture mutation."),
255 ),
256 baseDir: Flag.String("base-dir").pipe(
257 Flag.withDescription("Explicit T3 base directory containing userdata/statev2.sqlite."),
258 ),
259 sql: Flag.String("sql").pipe(
260 Flag.optional,
261 Flag.withDescription("SQL source supplied directly on the command line."),
262 ),
263 file: Flag.String("file").pipe(
264 Flag.optional,
265 Flag.withDescription("Path to a SQL source file."),
266 ),
267 },
268 ({ operation, baseDir, sql, file }) =>
269 runSqliteState({
270 operation,
271 baseDir,
272 sql: Option.getOrUndefined(sql),
273 file: Option.getOrUndefined(file),
274 }).pipe(Effect.flatMap(encodeSqliteStateResult), Effect.flatMap(Console.log)),
275 ).pipe(
276 Command.withDescription(
277 "Inspect or seed an isolated T3 SQLite database with automatic backups for writes.",
278 ),
279 );
280
281 if (import.meta.main) {
282 Command.run(t3SqliteStateCommand, { version: "0.0.0" }).pipe(
283 Effect.provide(NodeServices.layer),
284 NodeRuntime.runMain,
285 );
286 }