Open History to see the last commit
1import type { ProjectId, TaskFolderError, WorktreeLayoutCommandKind } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Digest from "effect/crypto/Digest";
4import * as DateTime from "effect/DateTime";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as SqlClient from "effect/sql/SqlClient";
8
9import { commandHash, failed } from "./LayoutCommandApprovals.ts";
10
11/**
12 * Records and withdraws approvals of a project's `worktreeLayout` commands. An approval lets the
13 * server run that text, so only a person may give one: nothing an agent can call imports this.
14 */
15export class LayoutCommandApprovalWrites extends Context.Service<
16 LayoutCommandApprovalWrites,
17 {
18 /** `command` is the text the person was shown; null withdraws the approval. */
19 readonly set: (input: {
20 readonly projectId: ProjectId;
21 readonly kind: WorktreeLayoutCommandKind;
22 readonly command: string | null;
23 }) => Effect.Effect<void, TaskFolderError>;
24 }
25>()("t3/workspace/LayoutCommandApprovalWrites") {}
26
27const make = Effect.gen(function* () {
28 const sql = yield* SqlClient.SqlClient;
29 const digest = yield* Digest.Digest;
30
31 return LayoutCommandApprovalWrites.of({
32 set: Effect.fn("LayoutCommandApprovalWrites.set")(function* ({ projectId, kind, command }) {
33 if (command === null) {
34 yield* sql`
35 DELETE FROM layout_command_approvals WHERE project_id = ${projectId} AND kind = ${kind}
36 `.pipe(Effect.mapError(failed));
37 return;
38 }
39 const hash = yield* commandHash(digest, command);
40 const approvedAt = DateTime.formatIso(yield* DateTime.now);
41 yield* sql`
42 INSERT INTO layout_command_approvals (project_id, kind, command_hash, approved_at)
43 VALUES (${projectId}, ${kind}, ${hash}, ${approvedAt})
44 ON CONFLICT (project_id, kind) DO UPDATE SET
45 command_hash = excluded.command_hash,
46 approved_at = excluded.approved_at
47 `.pipe(Effect.mapError(failed));
48 }),
49 });
50});
51
52export const layer = Layer.effect(LayoutCommandApprovalWrites, make);