memory.get no longer returns the embedding and content_tsv ---------------------------------------------------------- It used a bare select() and returned the raw DB row, while memory.list and memory.search already projected an explicit 9-field shape. On a ~13k-char memory those two internal columns were 55% of the response and pushed it past the MCP tool-output cap, so large memories could not be fetched inline at all. memory.get now returns the same 9 fields as its siblings; user_id is still selected for the authorization check and stripped before responding. memory.patch ------------ memory.update only accepts full replacement, so adding one line to a large document meant resending the whole document — expensive enough that edits were being skipped rather than risk silently truncating shared team documents. memory.patch replaces one exact occurrence of old_string. An absent or ambiguous match is an error, never a silent no-op and never an arbitrary pick; that refusal is what makes the operation safe to hand to an agent. The semantics live in lib/memory-patch.ts as a pure function, free of DB and auth, so both surfaces share them. Shared mutation layer --------------------- The MCP tools and the Web UI Server Actions each reimplemented authorize -> mutate -> re-embed -> CAS -> audit, and had drifted. Both now route through lib/memory-mutations.ts. BEHAVIOUR CHANGE: memory.delete over MCP skipped the project ACL whenever the caller authored the row, so a memory written while a share was rw stayed deletable by its author after an owner downgraded that share to ro. memory.update and the whole Web UI always checked. Authoring a row now grants no standing write privilege on any path. The one deliberate difference between the surfaces is injected as a ProjectResolver: MCP refuses an unknown project key so an agent cannot spawn near-miss projects off a typo, while the Web UI creates one because a person typing a name into a form means to. Tests and lint -------------- Adds vitest. The integration tests run against a real Postgres rather than a mocked DB. The embedder sidecar is the only stub and it is deterministic per-text, so re-embedding is verified by asserting the stored vector actually changed rather than that a mock was called. One test pins that content_tsv is a generated column and therefore cannot rot after a patch — only the embedding needs an explicit recompute. pnpm lint previously dropped into an interactive `next lint` setup prompt and exited 1; ESLint had never been configured here. Replaced with the ESLint CLI and a flat config bridging eslint-config-next through FlatCompat. Clean at --max-warnings=0. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
169 lines
5.6 KiB
TypeScript
169 lines
5.6 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { and, eq, inArray } from "drizzle-orm";
|
|
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db/client";
|
|
import { projects } from "@/lib/db/schema";
|
|
import { resolveProjectId, upsertProject } from "@/lib/projects";
|
|
import {
|
|
MemoryWriteInput,
|
|
MemoryUpdateInput,
|
|
MemoryDeleteInput,
|
|
} from "@shared-memory/schemas";
|
|
import { getUserGroupNames, readableProjectIds } from "@/lib/access";
|
|
import {
|
|
createMemory,
|
|
softDeleteMemory,
|
|
updateMemory,
|
|
type Actor,
|
|
type Outcome,
|
|
type ProjectResolver,
|
|
} from "@/lib/memory-mutations";
|
|
|
|
/**
|
|
* Server Actions for memory CRUD from the Web UI.
|
|
*
|
|
* 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.
|
|
*
|
|
* `actor` is "web" in audit_log so we can tell the two paths apart later.
|
|
*/
|
|
|
|
async function requireUserId(): Promise<string> {
|
|
const session = await auth();
|
|
if (!session?.user?.id) throw new Error("not authenticated");
|
|
return session.user.id;
|
|
}
|
|
|
|
/** 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) };
|
|
};
|
|
}
|
|
|
|
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) {
|
|
const { actor, resolveProject } = await webActor();
|
|
|
|
const parsed = MemoryWriteInput.safeParse({
|
|
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")),
|
|
});
|
|
if (!parsed.success) {
|
|
throw new Error(parsed.error.issues.map((i) => i.message).join("; "));
|
|
}
|
|
|
|
const created = must(await createMemory(actor, parsed.data, resolveProject));
|
|
|
|
revalidatePath("/memories");
|
|
redirect(`/memories/${created.id}`);
|
|
}
|
|
|
|
export async function updateMemoryAction(formData: FormData) {
|
|
const { actor, resolveProject } = await webActor();
|
|
|
|
const id = String(formData.get("id") ?? "");
|
|
const rawScope = formData.get("scope");
|
|
const rawProject = (formData.get("project") as string | null)?.trim() || undefined;
|
|
const rawVersion = formData.get("version");
|
|
const versionNum =
|
|
typeof rawVersion === "string" && rawVersion.length > 0
|
|
? Number.parseInt(rawVersion, 10)
|
|
: undefined;
|
|
const payload = {
|
|
id,
|
|
content: ((formData.get("content") as string | null) ?? "").trim() || undefined,
|
|
tags: parseTags(formData.get("tags")),
|
|
scope:
|
|
rawScope === "project" || rawScope === "user"
|
|
? (rawScope as "project" | "user")
|
|
: undefined,
|
|
project: rawProject,
|
|
version: Number.isFinite(versionNum) ? versionNum : undefined,
|
|
};
|
|
const parsed = MemoryUpdateInput.safeParse(payload);
|
|
if (!parsed.success) {
|
|
throw new Error(parsed.error.issues.map((i) => i.message).join("; "));
|
|
}
|
|
|
|
must(await updateMemory(actor, parsed.data, resolveProject));
|
|
|
|
revalidatePath(`/memories/${parsed.data.id}`);
|
|
revalidatePath("/memories");
|
|
redirect(`/memories/${parsed.data.id}`);
|
|
}
|
|
|
|
export async function deleteMemoryAction(formData: FormData) {
|
|
const { actor } = await webActor();
|
|
const id = String(formData.get("id") ?? "");
|
|
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,
|
|
});
|
|
if (!parsed.success) throw new Error(parsed.error.issues[0]!.message);
|
|
|
|
must(await softDeleteMemory(actor, parsed.data));
|
|
|
|
revalidatePath("/memories");
|
|
redirect("/memories");
|
|
}
|