2026-05-15 10:57:17 -07:00
|
|
|
"use server";
|
|
|
|
|
|
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
|
import { redirect } from "next/navigation";
|
2026-08-11 14:58:11 -07:00
|
|
|
import { and, eq, inArray } from "drizzle-orm";
|
2026-05-15 10:57:17 -07:00
|
|
|
import { auth } from "@/auth";
|
|
|
|
|
import { db } from "@/lib/db/client";
|
2026-08-11 14:58:11 -07:00
|
|
|
import { projects } from "@/lib/db/schema";
|
2026-05-17 09:54:48 -07:00
|
|
|
import { resolveProjectId, upsertProject } from "@/lib/projects";
|
2026-05-15 10:57:17 -07:00
|
|
|
import {
|
|
|
|
|
MemoryWriteInput,
|
|
|
|
|
MemoryUpdateInput,
|
2026-05-17 10:06:10 -07:00
|
|
|
MemoryDeleteInput,
|
2026-05-15 10:57:17 -07:00
|
|
|
} from "@shared-memory/schemas";
|
2026-08-11 14:58:11 -07:00
|
|
|
import { getUserGroupNames, readableProjectIds } from "@/lib/access";
|
2026-05-17 09:50:59 -07:00
|
|
|
import {
|
2026-08-11 14:58:11 -07:00
|
|
|
createMemory,
|
|
|
|
|
softDeleteMemory,
|
|
|
|
|
updateMemory,
|
|
|
|
|
type Actor,
|
|
|
|
|
type Outcome,
|
|
|
|
|
type ProjectResolver,
|
|
|
|
|
} from "@/lib/memory-mutations";
|
2026-05-15 10:57:17 -07:00
|
|
|
|
|
|
|
|
/**
|
2026-08-11 14:58:11 -07:00
|
|
|
* Server Actions for memory CRUD from the Web UI.
|
2026-05-15 10:57:17 -07:00
|
|
|
*
|
2026-08-11 14:58:11 -07:00
|
|
|
* These are thin adapters: form parsing, then `lib/memory-mutations`,
|
|
|
|
|
* then revalidate/redirect. The authorize → mutate → re-embed → CAS →
|
|
|
|
|
* audit sequence lives in that shared module so this surface and the MCP
|
|
|
|
|
* tools cannot drift apart — they previously did, and the sharing rules
|
|
|
|
|
* ended up subtly different between them.
|
2026-05-17 09:50:59 -07:00
|
|
|
*
|
2026-08-11 14:58:11 -07:00
|
|
|
* `actor` is "web" in audit_log so we can tell the two paths apart later.
|
2026-05-15 10:57:17 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async function requireUserId(): Promise<string> {
|
|
|
|
|
const session = await auth();
|
|
|
|
|
if (!session?.user?.id) throw new Error("not authenticated");
|
|
|
|
|
return session.user.id;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 14:58:11 -07:00
|
|
|
/** Server Actions signal failure by throwing; the shared layer returns Outcome. */
|
|
|
|
|
function must<T>(outcome: Outcome<T>): T {
|
|
|
|
|
if (!outcome.ok) throw new Error(outcome.error);
|
|
|
|
|
return outcome.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function webActor(): Promise<{ actor: Actor; resolveProject: ProjectResolver }> {
|
|
|
|
|
const userId = await requireUserId();
|
|
|
|
|
const groups = await getUserGroupNames(userId);
|
|
|
|
|
return {
|
|
|
|
|
actor: { userId, groups, via: "web" },
|
|
|
|
|
resolveProject: webProjectResolver(userId, groups),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Project resolution for Web UI writes. Unlike the MCP surface, an
|
|
|
|
|
* unknown key is CREATED rather than rejected — a person typing a project
|
|
|
|
|
* name into a form means to make one. Shared projects are matched only
|
|
|
|
|
* within the set the user can actually read, because `projects.key` is
|
|
|
|
|
* unique per user rather than globally: an unscoped key match could
|
|
|
|
|
* otherwise select someone else's project.
|
|
|
|
|
*
|
|
|
|
|
* Write access to whatever this returns is enforced centrally by the
|
|
|
|
|
* mutation layer, so it deliberately isn't re-checked here.
|
|
|
|
|
*/
|
|
|
|
|
function webProjectResolver(userId: string, groupNames: string[]): ProjectResolver {
|
|
|
|
|
return async (key: string) => {
|
|
|
|
|
const owned = await resolveProjectId(userId, key);
|
|
|
|
|
if (owned) return { ok: true, value: owned };
|
|
|
|
|
|
|
|
|
|
const readableIds = await readableProjectIds(userId, groupNames);
|
|
|
|
|
const shared =
|
|
|
|
|
readableIds.length > 0
|
|
|
|
|
? await db
|
|
|
|
|
.select({ id: projects.id })
|
|
|
|
|
.from(projects)
|
|
|
|
|
.where(and(eq(projects.key, key), inArray(projects.id, readableIds)))
|
|
|
|
|
.limit(1)
|
|
|
|
|
: [];
|
|
|
|
|
if (shared[0]) return { ok: true, value: shared[0].id };
|
|
|
|
|
|
|
|
|
|
return { ok: true, value: await upsertProject(userId, key) };
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 10:57:17 -07:00
|
|
|
function parseTags(raw: FormDataEntryValue | null): string[] {
|
|
|
|
|
if (typeof raw !== "string") return [];
|
|
|
|
|
return raw
|
|
|
|
|
.split(/[,\s]+/)
|
|
|
|
|
.map((t) => t.trim())
|
|
|
|
|
.filter((t) => t.length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createMemoryAction(formData: FormData) {
|
2026-08-11 14:58:11 -07:00
|
|
|
const { actor, resolveProject } = await webActor();
|
2026-05-15 10:57:17 -07:00
|
|
|
|
2026-08-11 14:58:11 -07:00
|
|
|
const parsed = MemoryWriteInput.safeParse({
|
2026-05-15 10:57:17 -07:00
|
|
|
content: String(formData.get("content") ?? "").trim(),
|
|
|
|
|
scope: (formData.get("scope") as "project" | "user") || "project",
|
|
|
|
|
project: (formData.get("project") as string | null)?.trim() || undefined,
|
|
|
|
|
tags: parseTags(formData.get("tags")),
|
2026-08-11 14:58:11 -07:00
|
|
|
});
|
2026-05-15 10:57:17 -07:00
|
|
|
if (!parsed.success) {
|
|
|
|
|
throw new Error(parsed.error.issues.map((i) => i.message).join("; "));
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 14:58:11 -07:00
|
|
|
const created = must(await createMemory(actor, parsed.data, resolveProject));
|
2026-05-15 10:57:17 -07:00
|
|
|
|
|
|
|
|
revalidatePath("/memories");
|
2026-08-11 14:58:11 -07:00
|
|
|
redirect(`/memories/${created.id}`);
|
2026-05-15 10:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateMemoryAction(formData: FormData) {
|
2026-08-11 14:58:11 -07:00
|
|
|
const { actor, resolveProject } = await webActor();
|
2026-05-15 10:57:17 -07:00
|
|
|
|
|
|
|
|
const id = String(formData.get("id") ?? "");
|
2026-05-17 06:43:19 -07:00
|
|
|
const rawScope = formData.get("scope");
|
|
|
|
|
const rawProject = (formData.get("project") as string | null)?.trim() || undefined;
|
2026-05-17 09:50:59 -07:00
|
|
|
const rawVersion = formData.get("version");
|
|
|
|
|
const versionNum =
|
|
|
|
|
typeof rawVersion === "string" && rawVersion.length > 0
|
|
|
|
|
? Number.parseInt(rawVersion, 10)
|
|
|
|
|
: undefined;
|
2026-05-15 10:57:17 -07:00
|
|
|
const payload = {
|
|
|
|
|
id,
|
|
|
|
|
content: ((formData.get("content") as string | null) ?? "").trim() || undefined,
|
|
|
|
|
tags: parseTags(formData.get("tags")),
|
2026-05-17 06:43:19 -07:00
|
|
|
scope:
|
|
|
|
|
rawScope === "project" || rawScope === "user"
|
|
|
|
|
? (rawScope as "project" | "user")
|
|
|
|
|
: undefined,
|
|
|
|
|
project: rawProject,
|
2026-05-17 09:50:59 -07:00
|
|
|
version: Number.isFinite(versionNum) ? versionNum : undefined,
|
2026-05-15 10:57:17 -07:00
|
|
|
};
|
|
|
|
|
const parsed = MemoryUpdateInput.safeParse(payload);
|
|
|
|
|
if (!parsed.success) {
|
|
|
|
|
throw new Error(parsed.error.issues.map((i) => i.message).join("; "));
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 14:58:11 -07:00
|
|
|
must(await updateMemory(actor, parsed.data, resolveProject));
|
2026-05-15 10:57:17 -07:00
|
|
|
|
|
|
|
|
revalidatePath(`/memories/${parsed.data.id}`);
|
|
|
|
|
revalidatePath("/memories");
|
|
|
|
|
redirect(`/memories/${parsed.data.id}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteMemoryAction(formData: FormData) {
|
2026-08-11 14:58:11 -07:00
|
|
|
const { actor } = await webActor();
|
2026-05-15 10:57:17 -07:00
|
|
|
const id = String(formData.get("id") ?? "");
|
2026-05-17 10:06:10 -07:00
|
|
|
const rawVersion = formData.get("version");
|
|
|
|
|
const version =
|
|
|
|
|
typeof rawVersion === "string" && rawVersion.length > 0
|
|
|
|
|
? Number.parseInt(rawVersion, 10)
|
|
|
|
|
: undefined;
|
|
|
|
|
const parsed = MemoryDeleteInput.safeParse({
|
|
|
|
|
id,
|
|
|
|
|
version: Number.isFinite(version) ? version : undefined,
|
|
|
|
|
});
|
2026-05-15 10:57:17 -07:00
|
|
|
if (!parsed.success) throw new Error(parsed.error.issues[0]!.message);
|
|
|
|
|
|
2026-08-11 14:58:11 -07:00
|
|
|
must(await softDeleteMemory(actor, parsed.data));
|
2026-05-15 10:57:17 -07:00
|
|
|
|
|
|
|
|
revalidatePath("/memories");
|
|
|
|
|
redirect("/memories");
|
|
|
|
|
}
|