feat(frontend): 记忆页面迁移到 Memory V2
前端记忆模块整体迁到后端 Memory V2 接口,修复调旧端点 /api/memory
返回 404 的问题。
- core/memory/{types,api,hooks}.ts 重写为 V2 数据模型与端点
(USER.md / MEMORY.md 条目增删改、配置读写、Hindsight 检索)
- memory-settings-page.tsx 重写:用户画像 + 工作笔记两区的条目
增删改、记忆设置面板(白名单字段)、Hindsight 检索面板
- 工作笔记按专家隔离,顶部专家下拉(默认助手 + useAgents 列出的
用户专家),专家列表取自 agents 注册表
- settings-dialog 记忆分区移到技能下方
- i18n(zh-CN/en-US/types)memory 文案整体替换
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
21a76f5e7c
commit
013aafde17
8 files changed
+1083
-1487
No files matched your search
+143
-149
@@ -1,149 +1,143 @@
|
||||
import { getBackendBaseURL } from "../config";
|
||||
import { apiFetch } from "../api";
|
||||
|
||||
import type {
|
||||
MemoryFactInput,
|
||||
MemoryFactPatchInput,
|
||||
UserMemory,
|
||||
} from "./types";
|
||||
|
||||
async function readMemoryResponse(
|
||||
response: Response,
|
||||
fallbackMessage: string,
|
||||
): Promise<UserMemory> {
|
||||
function formatErrorDetail(detail: unknown): string | null {
|
||||
if (typeof detail === "string") {
|
||||
return detail;
|
||||
}
|
||||
|
||||
if (Array.isArray(detail)) {
|
||||
const parts = detail
|
||||
.map((item) => {
|
||||
if (typeof item === "string") {
|
||||
return item;
|
||||
}
|
||||
|
||||
if (item && typeof item === "object") {
|
||||
const record = item as Record<string, unknown>;
|
||||
if (typeof record.msg === "string") {
|
||||
return record.msg;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.stringify(record);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return String(item);
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
return parts.length > 0 ? parts.join("; ") : null;
|
||||
}
|
||||
|
||||
if (detail && typeof detail === "object") {
|
||||
try {
|
||||
return JSON.stringify(detail);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
typeof detail === "string" ||
|
||||
typeof detail === "number" ||
|
||||
typeof detail === "boolean" ||
|
||||
typeof detail === "bigint"
|
||||
) {
|
||||
return String(detail);
|
||||
}
|
||||
|
||||
if (typeof detail === "symbol") {
|
||||
return detail.description ?? null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = (await response.json().catch(() => ({}))) as {
|
||||
detail?: unknown;
|
||||
};
|
||||
const detailMessage = formatErrorDetail(errorData.detail);
|
||||
throw new Error(
|
||||
detailMessage ?? `${fallbackMessage}: ${response.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
return response.json() as Promise<UserMemory>;
|
||||
}
|
||||
|
||||
export async function loadMemory(): Promise<UserMemory> {
|
||||
const response = await apiFetch(`${getBackendBaseURL()}/api/memory`);
|
||||
return readMemoryResponse(response, "Failed to fetch memory");
|
||||
}
|
||||
|
||||
export async function clearMemory(): Promise<UserMemory> {
|
||||
const response = await apiFetch(`${getBackendBaseURL()}/api/memory`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
return readMemoryResponse(response, "Failed to clear memory");
|
||||
}
|
||||
|
||||
export async function deleteMemoryFact(factId: string): Promise<UserMemory> {
|
||||
const response = await apiFetch(
|
||||
`${getBackendBaseURL()}/api/memory/facts/${encodeURIComponent(factId)}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
},
|
||||
);
|
||||
return readMemoryResponse(response, "Failed to delete memory fact");
|
||||
}
|
||||
|
||||
export async function exportMemory(): Promise<UserMemory> {
|
||||
const response = await apiFetch(`${getBackendBaseURL()}/api/memory/export`);
|
||||
return readMemoryResponse(response, "Failed to export memory");
|
||||
}
|
||||
|
||||
export async function importMemory(memory: UserMemory): Promise<UserMemory> {
|
||||
const response = await apiFetch(`${getBackendBaseURL()}/api/memory/import`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(memory),
|
||||
});
|
||||
return readMemoryResponse(response, "Failed to import memory");
|
||||
}
|
||||
|
||||
export async function createMemoryFact(
|
||||
input: MemoryFactInput,
|
||||
): Promise<UserMemory> {
|
||||
const response = await apiFetch(`${getBackendBaseURL()}/api/memory/facts`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
return readMemoryResponse(response, "Failed to create memory fact");
|
||||
}
|
||||
|
||||
export async function updateMemoryFact(
|
||||
factId: string,
|
||||
input: MemoryFactPatchInput,
|
||||
): Promise<UserMemory> {
|
||||
const response = await apiFetch(
|
||||
`${getBackendBaseURL()}/api/memory/facts/${encodeURIComponent(factId)}`,
|
||||
{
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(input),
|
||||
},
|
||||
);
|
||||
return readMemoryResponse(response, "Failed to update memory fact");
|
||||
}
|
||||
// Memory V2 API 客户端。对应后端 /api/memory/v2 系列端点。
|
||||
|
||||
import { apiFetch } from "../api";
|
||||
import { getBackendBaseURL } from "../config";
|
||||
|
||||
import type {
|
||||
MemorySearchMode,
|
||||
MemorySearchResult,
|
||||
MemoryTarget,
|
||||
MemoryV2ConfigResponse,
|
||||
MemoryV2Data,
|
||||
} from "./types";
|
||||
|
||||
/** 从响应里提取后端的错误详情文本。 */
|
||||
function formatErrorDetail(detail: unknown): string | null {
|
||||
if (typeof detail === "string") {
|
||||
return detail;
|
||||
}
|
||||
if (Array.isArray(detail)) {
|
||||
const parts = detail
|
||||
.map((item) => {
|
||||
if (typeof item === "string") return item;
|
||||
if (item && typeof item === "object") {
|
||||
const record = item as Record<string, unknown>;
|
||||
if (typeof record.msg === "string") return record.msg;
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.filter(Boolean);
|
||||
return parts.length > 0 ? parts.join("; ") : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function readJson<T>(response: Response, fallbackMessage: string): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const errorData = (await response.json().catch(() => ({}))) as {
|
||||
detail?: unknown;
|
||||
};
|
||||
const detailMessage = formatErrorDetail(errorData.detail);
|
||||
throw new Error(detailMessage ?? `${fallbackMessage}: ${response.statusText}`);
|
||||
}
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
/** 拼接可选的 ?agent= 查询串。 */
|
||||
function agentQuery(agent?: string): string {
|
||||
return agent ? `?agent=${encodeURIComponent(agent)}` : "";
|
||||
}
|
||||
|
||||
// --- 本地记忆数据 (USER.md / MEMORY.md) ---
|
||||
|
||||
export async function loadMemoryV2(agent?: string): Promise<MemoryV2Data> {
|
||||
const response = await apiFetch(
|
||||
`${getBackendBaseURL()}/api/memory/v2${agentQuery(agent)}`,
|
||||
);
|
||||
return readJson<MemoryV2Data>(response, "Failed to fetch memory");
|
||||
}
|
||||
|
||||
export async function addMemoryEntry(
|
||||
target: MemoryTarget,
|
||||
content: string,
|
||||
agent?: string,
|
||||
): Promise<MemoryV2Data> {
|
||||
const response = await apiFetch(
|
||||
`${getBackendBaseURL()}/api/memory/v2/entries${agentQuery(agent)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ target, content }),
|
||||
},
|
||||
);
|
||||
return readJson<MemoryV2Data>(response, "Failed to add memory entry");
|
||||
}
|
||||
|
||||
export async function replaceMemoryEntry(
|
||||
target: MemoryTarget,
|
||||
oldText: string,
|
||||
content: string,
|
||||
agent?: string,
|
||||
): Promise<MemoryV2Data> {
|
||||
const response = await apiFetch(
|
||||
`${getBackendBaseURL()}/api/memory/v2/entries${agentQuery(agent)}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ target, old_text: oldText, content }),
|
||||
},
|
||||
);
|
||||
return readJson<MemoryV2Data>(response, "Failed to replace memory entry");
|
||||
}
|
||||
|
||||
export async function deleteMemoryEntry(
|
||||
target: MemoryTarget,
|
||||
oldText: string,
|
||||
agent?: string,
|
||||
): Promise<MemoryV2Data> {
|
||||
const params = new URLSearchParams({ target, old_text: oldText });
|
||||
if (agent) {
|
||||
params.set("agent", agent);
|
||||
}
|
||||
const response = await apiFetch(
|
||||
`${getBackendBaseURL()}/api/memory/v2/entries?${params.toString()}`,
|
||||
{ method: "DELETE" },
|
||||
);
|
||||
return readJson<MemoryV2Data>(response, "Failed to delete memory entry");
|
||||
}
|
||||
|
||||
// --- 配置 ---
|
||||
|
||||
export async function loadMemoryConfig(): Promise<MemoryV2ConfigResponse> {
|
||||
const response = await apiFetch(`${getBackendBaseURL()}/api/memory/v2/config`);
|
||||
return readJson<MemoryV2ConfigResponse>(response, "Failed to fetch memory config");
|
||||
}
|
||||
|
||||
export async function patchMemoryConfig(
|
||||
overrides: Record<string, unknown>,
|
||||
): Promise<MemoryV2ConfigResponse> {
|
||||
const response = await apiFetch(`${getBackendBaseURL()}/api/memory/v2/config`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ overrides }),
|
||||
});
|
||||
return readJson<MemoryV2ConfigResponse>(response, "Failed to update memory config");
|
||||
}
|
||||
|
||||
// --- Hindsight 检索 ---
|
||||
|
||||
export async function searchMemory(
|
||||
query: string,
|
||||
mode: MemorySearchMode = "recall",
|
||||
agent?: string,
|
||||
): Promise<MemorySearchResult> {
|
||||
const response = await apiFetch(
|
||||
`${getBackendBaseURL()}/api/memory/v2/search${agentQuery(agent)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ query, mode }),
|
||||
},
|
||||
);
|
||||
return readJson<MemorySearchResult>(response, "Failed to search memory");
|
||||
}
|
||||
Reference in new issue
Block a user