agent-bridge-aun 1.0.33 → 1.0.34

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.
@@ -12,7 +12,7 @@ const agent_1 = require("../../agent");
12
12
  const messages_1 = require("./messages");
13
13
  const attachments_1 = require("./attachments");
14
14
  const manifest = {
15
- id: "aun", version: "1.0.33", type: "channel", displayName: "AUN 通道",
15
+ id: "aun", version: "1.0.34", type: "channel", displayName: "AUN 通道",
16
16
  entry: "dist/src-ts/plugins/aun/index.js", apiVersion: "1", process: "in-process",
17
17
  capabilities: ["channel", "channel.identity.create", "channel.receive", "channel.send", "health", "cli"],
18
18
  configSchema: "config.schema.json",
@@ -86,7 +86,7 @@ class AunPlugin {
86
86
  const known = new Set([...this.clients.keys(), ...this.listStoreAids()]);
87
87
  return {
88
88
  evolcore: [...known].filter((aid) => !own.has(aid)).sort(),
89
- muAgentBridge: [...this.clients.keys()].filter((aid) => own.has(aid)).sort(),
89
+ muAgentBridge: [...known].filter((aid) => own.has(aid)).sort(),
90
90
  localMuAgentBridge: [...own].filter((aid) => !known.has(aid)).sort(),
91
91
  };
92
92
  },
@@ -125,42 +125,29 @@ class AunPlugin {
125
125
  }
126
126
  },
127
127
  bind: async (pluginId, aid, _port) => {
128
+ if (!aid || !aidPattern.test(aid))
129
+ throw new Error("AID 格式无效,必须指定要关联的 AID");
128
130
  if (pluginId === "aun" || pluginId === "abweb")
129
131
  throw new Error("只能关联 Agent 插件");
132
+ if (this.context?.pluginStatus(pluginId) !== "ready")
133
+ throw new Error("目标 Agent 插件未启动");
130
134
  const previous = this.bindings;
131
135
  const previousTimes = this.bindingTimes;
132
- this.bindings = { ...previous };
133
- this.bindingTimes = { ...previousTimes };
136
+ const connection = this.clients.get(aid);
137
+ const hadIdentity = this.identities.has(aid);
138
+ const hadSubscription = Boolean(connection?.subscription);
134
139
  try {
135
- if (aid) {
136
- if (!aidPattern.test(aid))
137
- throw new Error("AID 格式无效");
138
- if (this.context?.pluginStatus(pluginId) !== "ready")
139
- throw new Error("目标 Agent 插件未启动");
140
- if (this.identities.has(aid) || this.legacyCreatedAids.has(aid)) {
141
- if (!this.clients.has(aid))
142
- await this.connectAid(aid);
143
- if (!this.clients.has(aid))
144
- throw new Error("AID 当前未连接");
145
- this.bindings[aid] = pluginId;
146
- }
147
- else {
148
- // 本地身份由插件直接接管,不再通过 ec 配置路由。
149
- const loaded = this.store.load(aid);
150
- if (!loaded.ok || !loaded.data)
151
- throw new Error("AID 不在本地密钥库中,无法关联");
152
- this.identities.add(aid);
153
- if (!this.clients.has(aid))
154
- await this.connectAid(aid);
155
- if (!this.clients.has(aid))
156
- throw new Error("AID 当前未连接");
157
- this.bindings[aid] = pluginId;
158
- }
159
- }
160
- else
161
- throw new Error("必须指定要关联的 AID");
162
- if (aid)
163
- this.bindingTimes[aid] = new Date().toISOString();
140
+ const loaded = this.store.load(aid);
141
+ if (!loaded.ok || !loaded.data)
142
+ throw new Error("AID 不在本地密钥库中,无法关联");
143
+ this.identities.add(aid);
144
+ if (!connection)
145
+ await this.connectAid(aid);
146
+ if (!this.clients.has(aid))
147
+ throw new Error("AID 当前未连接");
148
+ this.bindings = { ...previous, [aid]: pluginId };
149
+ this.bindingTimes = { ...previousTimes, [aid]: new Date().toISOString() };
150
+ this.subscribeAid(aid);
164
151
  await this.persist();
165
152
  }
166
153
  catch (error) {
@@ -168,6 +155,29 @@ class AunPlugin {
168
155
  this.bindingTimes = previousTimes;
169
156
  this.config.bindings = previous;
170
157
  this.config.binding_times = previousTimes;
158
+ if (!hadIdentity)
159
+ this.identities.delete(aid);
160
+ this.config.identities = [...this.identities].sort();
161
+ if (!hadSubscription) {
162
+ this.clients.get(aid)?.subscription?.unsubscribe();
163
+ if (connection)
164
+ connection.subscription = undefined;
165
+ }
166
+ if (!connection) {
167
+ try {
168
+ await this.clients.get(aid)?.client.close();
169
+ }
170
+ catch (closeError) {
171
+ this.context?.logger.warn("AUN 关联回滚关闭连接失败", { aid, error: safeError(closeError) });
172
+ }
173
+ finally {
174
+ this.clients.delete(aid);
175
+ if (!this.clients.size) {
176
+ this.status = "degraded";
177
+ this.healthMessage = "暂无已连接 AID";
178
+ }
179
+ }
180
+ }
171
181
  this.context?.logger.error("AUN 身份关联失败", { aid, pluginId, error: safeError(error) });
172
182
  throw error;
173
183
  }
@@ -178,6 +188,8 @@ class AunPlugin {
178
188
  throw new Error("AID 格式无效");
179
189
  if (this.bindings[aid] !== pluginId)
180
190
  throw new Error("指定 AID 当前未关联到该 Agent");
191
+ if ([...this.pendingMessages.keys()].some(key => JSON.parse(key)[0] === aid))
192
+ throw new Error("该 AID 正在处理消息,请等待任务完成后再解绑");
181
193
  const previous = this.bindings;
182
194
  const previousTimes = this.bindingTimes;
183
195
  this.bindings = { ...previous };
@@ -194,6 +206,21 @@ class AunPlugin {
194
206
  this.config.binding_times = previousTimes;
195
207
  throw error;
196
208
  }
209
+ const connection = this.clients.get(aid);
210
+ connection?.subscription?.unsubscribe();
211
+ try {
212
+ await connection?.client.close();
213
+ }
214
+ catch (error) {
215
+ this.context?.logger.warn("AUN 解绑已完成,关闭连接失败", { aid, pluginId, error: safeError(error) });
216
+ }
217
+ finally {
218
+ this.clients.delete(aid);
219
+ if (!this.clients.size) {
220
+ this.status = "degraded";
221
+ this.healthMessage = "暂无已连接 AID";
222
+ }
223
+ }
197
224
  this.context?.logger.info("AUN 单个身份关联已解除", { aid, pluginId });
198
225
  },
199
226
  };
@@ -207,6 +234,7 @@ class AunPlugin {
207
234
  async activate(context) {
208
235
  this.context = context;
209
236
  this.config = { ...context.config };
237
+ this.identities.clear();
210
238
  this.legacyCreatedAids.clear();
211
239
  this.createdAids.clear();
212
240
  try {
@@ -266,15 +294,25 @@ class AunPlugin {
266
294
  if (this.aid)
267
295
  this.identities.add(this.aid);
268
296
  this.acceptingMessages = true;
269
- for (const aid of this.identities)
270
- await this.connectAid(aid);
271
- // 密钥库中的其余 AID 也自动连接:不再依赖 ec 或 EvolCore 进程
272
- for (const aid of this.listStoreAids()) {
273
- this.identities.add(aid);
274
- if (!this.clients.has(aid))
297
+ // 只订阅明确关联 Agent 的身份,避免接管 EvolCore 或其它 AID 的消息。
298
+ for (const aid of Object.keys(this.bindings)) {
299
+ const target = this.bindings[aid];
300
+ if (!target || !context.pluginStatus(target)) {
301
+ context.logger.warn("AUN 跳过目标插件不存在的旧绑定,未连接身份", { aid, agentPlugin: target });
302
+ continue;
303
+ }
304
+ if (context.pluginStatus(target) !== "ready")
305
+ context.logger.warn("AUN 恢复已有显式绑定,目标插件尚未就绪;关联保持不变", { aid, agentPlugin: target, status: context.pluginStatus(target) });
306
+ if (aidPattern.test(aid))
275
307
  await this.connectAid(aid);
276
308
  }
277
309
  }
310
+ subscribeAid(aid) {
311
+ const connection = this.clients.get(aid);
312
+ if (!connection || connection.subscription || !this.bindings[aid])
313
+ return;
314
+ connection.subscription = connection.client.on("message.received", (payload) => this.onMessage(payload, aid));
315
+ }
278
316
  async connectAid(aid) {
279
317
  try {
280
318
  await this.prepareTrustRoots(aid);
@@ -282,7 +320,7 @@ class AunPlugin {
282
320
  if (!loaded.ok || !loaded.data)
283
321
  throw new Error(loaded.error?.message ?? "本地密钥库中不存在该 AID");
284
322
  const client = new this.sdk.AUNClient(loaded.data.aid);
285
- const subscription = this.identities.has(aid) || this.legacyCreatedAids.has(aid)
323
+ const subscription = this.bindings[aid]
286
324
  ? client.on("message.received", (payload) => this.onMessage(payload, aid))
287
325
  : undefined;
288
326
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-bridge-aun",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "mu-agent-bridge 的 AUN 通道插件。",
5
5
  "keywords": [
6
6
  "mu-agent-bridge-plugin",
package/plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "aun",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "type": "channel",
5
5
  "displayName": "AUN 通道",
6
6
  "entry": "dist/src-ts/plugins/aun/index.js",