Skip to content

Commit 596348f

Browse files
authored
feat(api): add standalone profile and rules routes (#185)
1 parent c95019a commit 596348f

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎dsh-mneme/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ dsh web
488488
|------|------|------|
489489
| `GET` | `/health` | 健康检查(免鉴权),返回 `{ok:true}` |
490490
| `GET` | `/status` | 版本、记忆统计、实体数、运行时长 |
491+
| `GET` | `/profile` | 用户画像,返回 `{profile:"..."}`;未设置时为空字符串 |
492+
| `GET` | `/rules` | 行为规则列表,返回 `{rules:[...]}`;未设置时为空数组 |
491493
| `GET` | `/memories?limit&offset&type&minImportance&source&order=chrono` | 分页列出记忆 |
492494
| `GET` | `/memories/:id` | 单条记忆 |
493495
| `POST` | `/memories` | 新增记忆 `{type,title,content,importance?,tags?,source?}` |

‎dsh-mneme/lib/api-standalone.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ export function createStandaloneApi({ service, store, config = {}, logger, setti
152152
return;
153153
}
154154

155+
// --- GET /profile: user self-description -------------------------------
156+
if (req.method === "GET" && pathname === "/profile") {
157+
sendJson(res, 200, { profile: settings?.getProfile?.() ?? "" });
158+
return;
159+
}
160+
161+
// --- GET /rules: agent behavior rules -----------------------------------
162+
if (req.method === "GET" && pathname === "/rules") {
163+
sendJson(res, 200, { rules: settings?.getRules?.() ?? [] });
164+
return;
165+
}
166+
155167
// --- GET /memories: paged + filtered list (same semantics as the
156168
// internal /api/dsh-mneme/list) ------------------------------------
157169
if (req.method === "GET" && pathname === "/memories") {

‎dsh-mneme/src/api-standalone.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ export function createStandaloneApi({ service, store, config = {}, logger, setti
152152
return;
153153
}
154154

155+
// --- GET /profile: user self-description -------------------------------
156+
if (req.method === "GET" && pathname === "/profile") {
157+
sendJson(res, 200, { profile: settings?.getProfile?.() ?? "" });
158+
return;
159+
}
160+
161+
// --- GET /rules: agent behavior rules -----------------------------------
162+
if (req.method === "GET" && pathname === "/rules") {
163+
sendJson(res, 200, { rules: settings?.getRules?.() ?? [] });
164+
return;
165+
}
166+
155167
// --- GET /memories: paged + filtered list (same semantics as the
156168
// internal /api/dsh-mneme/list) ------------------------------------
157169
if (req.method === "GET" && pathname === "/memories") {

‎dsh-mneme/test/standalone-api.test.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test("GET /health is open without a token; every other route 401s", async () =>
4040
assert.equal(health.status, 200);
4141
assert.deepEqual(await health.json(), { ok: true });
4242

43-
for (const path of ["/status", "/memories", "/memories/x", "/search?q=x"]) {
43+
for (const path of ["/status", "/profile", "/rules", "/memories", "/memories/x", "/search?q=x"]) {
4444
const res = await fetch(`${base}${path}`);
4545
assert.equal(res.status, 401, `${path} requires a token`);
4646
assert.deepEqual(await res.json(), { error: "unauthorized" });
@@ -53,6 +53,39 @@ test("GET /health is open without a token; every other route 401s", async () =>
5353
}
5454
});
5555

56+
test("GET /profile and /rules return saved settings", async () => {
57+
const { base, auth, settings, close } = await setup();
58+
try {
59+
settings.setProfile("我是后端工程师");
60+
settings.setRules(["先验证再修改", "默认使用中文回复"]);
61+
62+
const profile = await fetch(`${base}/profile`, { headers: auth });
63+
assert.equal(profile.status, 200);
64+
assert.deepEqual(await profile.json(), { profile: "我是后端工程师" });
65+
66+
const rules = await fetch(`${base}/rules`, { headers: auth });
67+
assert.equal(rules.status, 200);
68+
assert.deepEqual(await rules.json(), { rules: ["先验证再修改", "默认使用中文回复"] });
69+
} finally {
70+
close();
71+
}
72+
});
73+
74+
test("GET /profile and /rules return empty values when unset", async () => {
75+
const { base, auth, close } = await setup();
76+
try {
77+
const profile = await fetch(`${base}/profile`, { headers: auth });
78+
assert.equal(profile.status, 200);
79+
assert.deepEqual(await profile.json(), { profile: "" });
80+
81+
const rules = await fetch(`${base}/rules`, { headers: auth });
82+
assert.equal(rules.status, 200);
83+
assert.deepEqual(await rules.json(), { rules: [] });
84+
} finally {
85+
close();
86+
}
87+
});
88+
5689
test("token is persisted into settings kv and reused across restarts", async () => {
5790
const store = createStore(":memory:");
5891
const service = createService({ store, mirror: null, config: {} });

0 commit comments

Comments
 (0)