6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
114 lines
3.0 KiB
TypeScript
114 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import { authenticatedFetch } from "@/lib/auth-fetch";
|
|
import { buildBackendUrl } from "@/lib/env-config";
|
|
|
|
export type MemoryScope = "user" | "team";
|
|
|
|
export interface MemoryLimits {
|
|
soft: number;
|
|
hard: number;
|
|
}
|
|
|
|
export type MemoryLimitLevel = "ok" | "warning" | "error";
|
|
|
|
export interface MemoryEditorDocument {
|
|
document_id: number;
|
|
title: string;
|
|
document_type: "USER_MEMORY" | "TEAM_MEMORY";
|
|
source_markdown: string;
|
|
}
|
|
|
|
interface MemoryReadResponse {
|
|
memory_md?: string;
|
|
limits?: MemoryLimits;
|
|
}
|
|
|
|
function getMemoryPath(scope: MemoryScope, workspaceId?: number | null) {
|
|
if (scope === "user") return "/api/v1/users/me/memory";
|
|
if (!workspaceId) throw new Error("Missing workspace context");
|
|
return `/api/v1/workspaces/${workspaceId}/memory`;
|
|
}
|
|
|
|
export function getMemoryLimitState(length: number, limits?: MemoryLimits | null) {
|
|
if (!limits) {
|
|
return {
|
|
level: "ok" as MemoryLimitLevel,
|
|
label: `${length.toLocaleString()} chars`,
|
|
isOverLimit: false,
|
|
};
|
|
}
|
|
|
|
const isOverLimit = length > limits.hard;
|
|
const isNearLimit = length > limits.soft;
|
|
const level: MemoryLimitLevel = isOverLimit ? "error" : isNearLimit ? "warning" : "ok";
|
|
const suffix = isOverLimit ? " - Exceeds limit" : isNearLimit ? " - Approaching limit" : "";
|
|
|
|
return {
|
|
level,
|
|
label: `${length.toLocaleString()}/${limits.hard.toLocaleString()} chars${suffix}`,
|
|
isOverLimit,
|
|
};
|
|
}
|
|
|
|
export async function fetchMemoryEditorDocument({
|
|
scope,
|
|
workspaceId,
|
|
title,
|
|
signal,
|
|
}: {
|
|
scope: MemoryScope;
|
|
workspaceId?: number | null;
|
|
title?: string | null;
|
|
signal?: AbortSignal;
|
|
}) {
|
|
const response = await authenticatedFetch(buildBackendUrl(getMemoryPath(scope, workspaceId)), {
|
|
method: "GET",
|
|
signal,
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({ detail: "Failed to fetch memory" }));
|
|
throw new Error(errorData.detail || "Failed to fetch memory");
|
|
}
|
|
|
|
const data = (await response.json()) as MemoryReadResponse;
|
|
const isTeamMemory = scope === "team";
|
|
|
|
return {
|
|
limits: data.limits ?? null,
|
|
document: {
|
|
document_id: isTeamMemory ? -1002 : -1001,
|
|
title: title || (isTeamMemory ? "Team Memory" : "Personal Memory"),
|
|
document_type: isTeamMemory ? "TEAM_MEMORY" : "USER_MEMORY",
|
|
source_markdown: data.memory_md ?? "",
|
|
} satisfies MemoryEditorDocument,
|
|
};
|
|
}
|
|
|
|
export async function saveMemoryMarkdown({
|
|
scope,
|
|
workspaceId,
|
|
markdown,
|
|
}: {
|
|
scope: MemoryScope;
|
|
workspaceId?: number | null;
|
|
markdown: string;
|
|
}) {
|
|
const response = await authenticatedFetch(buildBackendUrl(getMemoryPath(scope, workspaceId)), {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ memory_md: markdown }),
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({ detail: "Failed to save memory" }));
|
|
throw new Error(errorData.detail || "Failed to save memory");
|
|
}
|
|
|
|
const data = (await response.json()) as MemoryReadResponse;
|
|
|
|
return {
|
|
markdown: data.memory_md ?? markdown,
|
|
limits: data.limits,
|
|
};
|
|
}
|