@dsh-plugin/dsh-synapse 0.1.0-dev.41044228975 → 0.1.0-dev.41051739681

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +12 -24
  2. package/lib/host/agent.d.ts +8 -4
  3. package/lib/host/agent.js +16 -99
  4. package/lib/host/agent.js.map +1 -1
  5. package/lib/host/bot.js +9 -7
  6. package/lib/host/bot.js.map +1 -1
  7. package/lib/host/config-migration.d.ts +3 -0
  8. package/lib/host/config-migration.js +103 -0
  9. package/lib/host/config-migration.js.map +1 -0
  10. package/lib/host/config.d.ts +4 -18
  11. package/lib/host/config.js +77 -130
  12. package/lib/host/config.js.map +1 -1
  13. package/lib/host/feature-config.d.ts +20 -0
  14. package/lib/host/feature-config.js +264 -0
  15. package/lib/host/feature-config.js.map +1 -0
  16. package/lib/host/gateway.d.ts +4 -6
  17. package/lib/host/gateway.js +42 -104
  18. package/lib/host/gateway.js.map +1 -1
  19. package/lib/host/github.d.ts +2 -2
  20. package/lib/host/harness.d.ts +23 -0
  21. package/lib/host/harness.js +209 -0
  22. package/lib/host/harness.js.map +1 -0
  23. package/lib/host/index.js +22 -12
  24. package/lib/host/index.js.map +1 -1
  25. package/lib/host/mcp.d.ts +1 -1
  26. package/lib/host/mcp.js +15 -11
  27. package/lib/host/mcp.js.map +1 -1
  28. package/lib/host/panel.html +2 -1
  29. package/lib/host/panel.js +40 -24
  30. package/lib/host/panel.js.map +1 -1
  31. package/lib/host/types.d.ts +104 -16
  32. package/package.json +1 -1
  33. package/scripts/dump-npm-latest.mjs +29 -10
  34. package/scripts/host-smoke.mjs +139 -0
  35. package/src/agent.ts +34 -114
  36. package/src/bot.ts +9 -7
  37. package/src/config-migration.ts +148 -0
  38. package/src/config.ts +116 -152
  39. package/src/feature-config.ts +351 -0
  40. package/src/gateway.ts +48 -115
  41. package/src/harness.ts +253 -0
  42. package/src/index.ts +25 -18
  43. package/src/mcp.ts +23 -11
  44. package/src/panel.html +2 -1
  45. package/src/panel.ts +53 -24
  46. package/src/types.ts +75 -13
  47. package/test/config-migration.test.mjs +137 -0
  48. package/test/core.test.mjs +82 -16
  49. package/test/fixtures/host.mjs +133 -0
  50. package/test/harness.test.mjs +90 -0
  51. package/test/integration.test.mjs +10 -12
  52. package/test/panel.test.mjs +71 -3
@@ -0,0 +1,90 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdtemp } from "node:fs/promises";
4
+ import path from "node:path";
5
+ import os from "node:os";
6
+ import { defaultsFor } from "@dsh-plugin/dsh-loader/config";
7
+ import { template } from "../lib/host/config.js";
8
+ import { Harness } from "../lib/host/harness.js";
9
+ import { hostFixture } from "./fixtures/host.mjs";
10
+
11
+ test("native runs inherit current host selection, import attachments and dispose on cancellation", async () => {
12
+ const c = defaultsFor(template.fields);
13
+ const host = hostFixture(async ({ message, signal }) => {
14
+ if (message.content[0].text === "cancel") {
15
+ await new Promise((resolve) =>
16
+ signal.addEventListener("abort", resolve, { once: true }),
17
+ );
18
+ signal.throwIfAborted();
19
+ }
20
+ return "done";
21
+ });
22
+ const harness = new Harness(
23
+ host.loader,
24
+ () => c,
25
+ { data: await mkdtemp(path.join(os.tmpdir(), "synapse-harness-")) },
26
+ {},
27
+ );
28
+ try {
29
+ await harness.run("SOUL", [
30
+ { type: "image_url", image_url: { url: "data:image/png;base64,YWJj" } },
31
+ ]);
32
+ assert.equal(host.attachments[0].data.toString(), "abc");
33
+ host.select({
34
+ provider: "changed",
35
+ model: "new-model",
36
+ reasoningEffort: "high",
37
+ });
38
+ await harness.run("SOUL", "hello");
39
+ assert.deepEqual(host.created[1].options.agentOptions, {
40
+ provider: "changed",
41
+ model: "new-model",
42
+ reasoningEffort: "high",
43
+ });
44
+ const controller = new AbortController();
45
+ const task = harness.run("SOUL", "cancel", [], [], controller.signal);
46
+ setTimeout(() => controller.abort(), 30);
47
+ await assert.rejects(task, /abort/i);
48
+ assert.equal(host.created.length, host.disposed.length);
49
+ } finally {
50
+ await harness.close();
51
+ }
52
+ });
53
+
54
+ test("native host errors propagate and approval answers stay in the requesting agent", async () => {
55
+ const host = hostFixture(async ({ approve }) => {
56
+ assert.equal(
57
+ await approve({ toolName: "fixture/read", reason: "policy" }),
58
+ "rejected",
59
+ );
60
+ throw new Error("host failure");
61
+ });
62
+ const harness = new Harness(
63
+ host.loader,
64
+ () => defaultsFor(template.fields),
65
+ { data: await mkdtemp(path.join(os.tmpdir(), "synapse-harness-")) },
66
+ {},
67
+ );
68
+ try {
69
+ const seen = [];
70
+ await assert.rejects(
71
+ harness.run(
72
+ "SOUL",
73
+ "hello",
74
+ [],
75
+ [],
76
+ undefined,
77
+ undefined,
78
+ async (req) => {
79
+ seen.push(req);
80
+ return "rejected";
81
+ },
82
+ ),
83
+ /host failure/,
84
+ );
85
+ assert.equal(seen[0].toolName, "fixture/read");
86
+ assert.equal(host.disposed.length, 1);
87
+ } finally {
88
+ await harness.close();
89
+ }
90
+ });
@@ -23,18 +23,19 @@ const waitFor = async (fn, ms = 10000) => {
23
23
  };
24
24
  test("real MCP stdio discovery, allowlist and invocation", async () => {
25
25
  const c = defaultsFor(template.fields);
26
- c.mcp = [
26
+ c.mcpServers = [
27
27
  {
28
28
  name: "fixture",
29
29
  enabled: true,
30
30
  transport: "stdio",
31
31
  command: process.execPath,
32
- argsJson: JSON.stringify([
32
+ args: [
33
33
  fileURLToPath(new URL("./fixtures/mcp-server.mjs", import.meta.url)),
34
- ]),
35
- envJson: "{}",
36
- headersJson: "{}",
37
- allowedTools: "read",
34
+ ],
35
+ env: [],
36
+ headers: [],
37
+ allowedTools: ["read"],
38
+ url: "",
38
39
  },
39
40
  ];
40
41
  const mcp = new McpManager(() => c);
@@ -66,12 +67,8 @@ test("full upstream bot starts, connects OneBot and executes /help", async () =>
66
67
  profileDir: profile,
67
68
  homeDir: home,
68
69
  };
69
- c.features.find((x) => x.id === "command").json = JSON.stringify({
70
- version: 1,
71
- groups: [123],
72
- });
73
- c.features.find((x) => x.id === "hermes").json = JSON.stringify({
74
- version: 1,
70
+ c.modules.command.groups = [123];
71
+ Object.assign(c.modules.chat, {
75
72
  groups: [123],
76
73
  reactionEnabled: false,
77
74
  progressAsImage: false,
@@ -209,6 +206,7 @@ test("loader 2.0 lifecycle registers, authenticates routes and disposes services
209
206
  registered = t;
210
207
  return {
211
208
  get: () => ({ value: c }),
209
+ dispose() {},
212
210
  watch(fn) {
213
211
  watched = fn;
214
212
  return () => {
@@ -19,17 +19,20 @@ test("panel renders persisted configuration, skills, MCP and Hermes import witho
19
19
  });
20
20
  const w = dom.window,
21
21
  calls = [];
22
+ let saved = defaultsFor(template.fields);
23
+ saved.modules.discord.token = { $secret: "opaque-token", set: true };
22
24
  w.structuredClone = structuredClone;
23
25
  w.confirm = () => true;
24
26
  w.fetch = async (url, options) => {
25
27
  calls.push([url, options]);
26
28
  let data;
27
- if (url.startsWith("/api/dshloader/config"))
29
+ if (url.startsWith("/api/dshloader/config")) {
30
+ if (options?.method === "POST") saved = JSON.parse(options.body).value;
28
31
  data = {
29
32
  ok: true,
30
- value: { value: defaultsFor(template.fields), template, revision: 1 },
33
+ value: { value: saved, template, revision: 1 },
31
34
  };
32
- else if (url.endsWith("/status")) data = { bot: "stopped" };
35
+ } else if (url.endsWith("/status")) data = { bot: "stopped" };
33
36
  else if (url.endsWith("/skills"))
34
37
  data = [
35
38
  {
@@ -57,12 +60,77 @@ test("panel renders persisted configuration, skills, MCP and Hermes import witho
57
60
  await settle();
58
61
  };
59
62
  assert.equal(w.document.querySelectorAll("nav button").length, 5);
63
+ assert(!w.document.body.textContent.includes("AI 对话 / 审查"));
64
+ assert(!w.document.querySelector('[name="botJson"]'));
65
+ assert(!w.document.querySelector('[name="llmUrl"]'));
66
+ const input = (name, value) => {
67
+ const control = w.document.querySelector(`[name="${name}"]`);
68
+ assert(control, name);
69
+ if (control.type === "checkbox") {
70
+ control.checked = value;
71
+ control.dispatchEvent(new w.Event("change"));
72
+ } else {
73
+ control.value = value;
74
+ control.dispatchEvent(
75
+ new w.Event(control.tagName === "SELECT" ? "change" : "input"),
76
+ );
77
+ }
78
+ };
79
+ const add = async (name) => {
80
+ const box = w.document.querySelector(`fieldset[data-path="${name}"]`);
81
+ assert(box, name);
82
+ [...box.children]
83
+ .find((b) => b.tagName === "BUTTON" && b.textContent === "添加")
84
+ .click();
85
+ await settle();
86
+ };
87
+ await add("modules.chat.groups");
88
+ input("modules.chat.groups.0", "123456");
89
+ await add("modules.discord.bridges");
90
+ input("modules.discord.bridges.0.guildId", "123");
91
+ input("modules.discord.bridges.0.channelName", "general");
92
+ input("modules.discord.bridges.0.group", "123456");
93
+ input("modules.discord.bridges.0.needReply", true);
94
+ const days = w.document.querySelector(
95
+ '[name="modules.peakValley.time.0.days"]',
96
+ );
97
+ for (const option of days.options)
98
+ option.selected = ["Sat", "Sun"].includes(option.value);
99
+ days.dispatchEvent(new w.Event("change"));
100
+ await click("保存配置");
101
+ assert.deepEqual(saved.modules.chat.groups, [123456]);
102
+ assert.equal(saved.modules.discord.bridges[0].needReply, true);
103
+ assert.deepEqual(saved.modules.peakValley.time[0].days, ["Sat", "Sun"]);
104
+ assert.deepEqual(saved.modules.discord.token, {
105
+ $secret: "opaque-token",
106
+ set: true,
107
+ });
108
+ input("modules.chat.groups.0", "654321");
109
+ await click("保存配置");
110
+ assert.deepEqual(saved.modules.chat.groups, [654321]);
60
111
  await click("Skill");
61
112
  await click("demo");
62
113
  assert.match(w.document.querySelector("pre").textContent, /<script>/);
63
114
  assert.equal(w.pwned, undefined);
64
115
  await click("MCP");
65
116
  assert.match(w.document.body.textContent, /允许列表/);
117
+ await add("mcpServers");
118
+ input("mcpServers.0.name", "docs");
119
+ input("mcpServers.0.command", "node");
120
+ await add("mcpServers.0.args");
121
+ input("mcpServers.0.args.0", "server.mjs");
122
+ await add("mcpServers.0.env");
123
+ input("mcpServers.0.env.0.name", "TOKEN");
124
+ input("mcpServers.0.env.0.value", "new-secret");
125
+ assert.equal(
126
+ w.document.querySelector('[name="mcpServers.0.env.0.value"]').type,
127
+ "password",
128
+ );
129
+ await click("保存配置");
130
+ assert.deepEqual(saved.mcpServers[0].args, ["server.mjs"]);
131
+ assert.deepEqual(saved.mcpServers[0].env, [
132
+ { name: "TOKEN", value: "new-secret" },
133
+ ]);
66
134
  await click("Hermes 记忆");
67
135
  w.document.querySelector("textarea").value = "# User\n喜欢猫";
68
136
  await click("预览分块");