@ai-setting/roy-agent-core 1.7.30 → 1.7.31

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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createAutoTaskPlugin
3
- } from "./roy-agent-core-h4h6n9vk.js";
3
+ } from "./roy-agent-core-06wbc8km.js";
4
4
  import {
5
5
  ContextError,
6
6
  ErrorCodes
@@ -539,8 +539,9 @@ class TaskComponent extends BaseComponent {
539
539
  };
540
540
  await this.executeHook(TaskHookPoints.OPERATION_BEFORE_CREATE, createCtx);
541
541
  const op = await this.store.createOperation(createCtx.options);
542
+ const task = await this.store.getTask(op.taskId);
542
543
  const resultCtx = { operation: op };
543
- await this.executeHook(TaskHookPoints.OPERATION_AFTER_CREATE, resultCtx);
544
+ await this.executeHook(TaskHookPoints.OPERATION_AFTER_CREATE, resultCtx, { task, operation: op });
544
545
  this.publishEvent(TaskEntityEventTypes.OPERATION_ADDED, { operation: op, taskId: op.taskId }, op.sessionId);
545
546
  logger2.info(`[TaskComponent] Operation created: ${op.id} for task ${op.taskId}`);
546
547
  return op;
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  TaskTagPlugin,
3
- createLarkCliTaskNotifyHook
4
- } from "./roy-agent-core-h4h6n9vk.js";
3
+ createLarkCliTaskNotifyHook,
4
+ createPerTaskGroupChatPlugin
5
+ } from "./roy-agent-core-06wbc8km.js";
5
6
  import {
6
7
  envKeyToConfigKey
7
8
  } from "./roy-agent-core-7hh0brvs.js";
@@ -325,9 +326,13 @@ class BountyIMInstance {
325
326
  ws = null;
326
327
  buffer = "";
327
328
  eventHandler;
329
+ environment;
328
330
  constructor(config) {
329
331
  this.config = config;
330
332
  }
333
+ setEnvironment(env) {
334
+ this.environment = env;
335
+ }
331
336
  getCurrentIdentity() {
332
337
  const { email } = readBountyIMConfig(this.config);
333
338
  return { email };
@@ -663,9 +668,13 @@ class BountyCliInstance {
663
668
  child;
664
669
  buffer = "";
665
670
  eventHandler;
671
+ environment;
666
672
  constructor(config) {
667
673
  this.config = config;
668
674
  }
675
+ setEnvironment(env) {
676
+ this.environment = env;
677
+ }
669
678
  getStatus() {
670
679
  return this.status;
671
680
  }
@@ -818,8 +827,11 @@ var bountyCliHandler = {
818
827
  };
819
828
 
820
829
  // src/env/event-source/event-source-handlers.ts
830
+ init_logger();
831
+ var logger = createLogger("lark-cli:event-source");
821
832
  var BUILT_IN_PLUGIN_FACTORIES = {
822
833
  LarkCliTaskNotifyPlugin: (cfg) => createLarkCliTaskNotifyHook(cfg),
834
+ PerTaskGroupChatPlugin: (cfg) => createPerTaskGroupChatPlugin(cfg),
823
835
  "task-tag": () => new TaskTagPlugin
824
836
  };
825
837
  function generateTraceId() {
@@ -940,7 +952,10 @@ function extractMetadata(rawEvent, eventType) {
940
952
  appId: metadata.appId,
941
953
  profile,
942
954
  chatId: messageData?.chat_id,
943
- messageId: messageData?.message_id
955
+ messageId: messageData?.message_id,
956
+ params: {
957
+ userId: senderId?.open_id
958
+ }
944
959
  };
945
960
  return { metadata, replyChannel, recommendedAction };
946
961
  }
@@ -951,10 +966,17 @@ class LarkCliInstance {
951
966
  child;
952
967
  buffer = "";
953
968
  eventHandler;
969
+ recentEventIds = new Map;
970
+ DEDUP_WINDOW_MS = 60000;
971
+ DEDUP_MAX_SIZE = 100;
972
+ environment;
954
973
  loadedPlugins = [];
955
974
  constructor(config) {
956
975
  this.config = config;
957
976
  }
977
+ setEnvironment(env) {
978
+ this.environment = env;
979
+ }
958
980
  async start() {
959
981
  if (this.status === "running")
960
982
  return;
@@ -1040,6 +1062,11 @@ class LarkCliInstance {
1040
1062
  }
1041
1063
  async loadPlugins() {
1042
1064
  const plugins = this.config.handleRule?.plugins || [];
1065
+ if (plugins.length === 0) {
1066
+ console.debug(`[LarkCliInstance] No plugins configured in handleRule.plugins ` + `(sourceId=${this.config.id}). Skipping plugin loading.`);
1067
+ return;
1068
+ }
1069
+ console.debug(`[LarkCliInstance] Loading ${plugins.length} plugin(s) for sourceId=${this.config.id}: ` + `[${plugins.join(", ")}]; env injected=${!!this.environment}`);
1043
1070
  for (const pluginName of plugins) {
1044
1071
  const plugin = await this.importPlugin(pluginName);
1045
1072
  if (plugin) {
@@ -1049,6 +1076,9 @@ class LarkCliInstance {
1049
1076
  name: pluginName,
1050
1077
  hooks: hookPoints.map((h) => ({ point: h, name: `${pluginName}:${h}` }))
1051
1078
  });
1079
+ console.debug(`[LarkCliInstance] Loaded plugin "${pluginName}" with ${hookPoints.length} hook(s): ` + `[${hookPoints.join(", ")}]; setTaskUpdater=${typeof plugin.setTaskUpdater === "function"}, ` + `taskUpdaterInjected=${typeof plugin.hasTaskUpdater === "function" ? plugin.hasTaskUpdater() : "n/a"}`);
1080
+ } else {
1081
+ console.error(`[LarkCliInstance] Failed to import plugin "${pluginName}". ` + `plugin name=${pluginName} 不在 BUILT_IN_PLUGIN_FACTORIES 注册表里`);
1052
1082
  }
1053
1083
  }
1054
1084
  }
@@ -1063,7 +1093,8 @@ class LarkCliInstance {
1063
1093
  return [
1064
1094
  "task:after.create",
1065
1095
  "task:after.update",
1066
- "task:operation:after.create"
1096
+ "task:operation:after.create",
1097
+ "task:after.complete"
1067
1098
  ];
1068
1099
  }
1069
1100
  return [];
@@ -1075,9 +1106,11 @@ class LarkCliInstance {
1075
1106
  return null;
1076
1107
  }
1077
1108
  try {
1109
+ const pluginConfig = this.config.handleRule?.pluginConfig?.[name] ?? {};
1078
1110
  return factory({
1079
1111
  sourceId: this.config.id,
1080
- pluginName: name
1112
+ pluginName: name,
1113
+ ...pluginConfig
1081
1114
  });
1082
1115
  } catch (error) {
1083
1116
  console.error(`[LarkCliInstance] Failed to instantiate plugin ${name}:`, error);
@@ -1086,6 +1119,7 @@ class LarkCliInstance {
1086
1119
  }
1087
1120
  registerPluginHooks(plugin) {
1088
1121
  const pluginName = plugin.name || "unknown";
1122
+ this.injectTaskUpdater(plugin);
1089
1123
  if (typeof plugin.init === "function") {
1090
1124
  const pluginEnv = {
1091
1125
  getComponent: () => null,
@@ -1150,6 +1184,27 @@ class LarkCliInstance {
1150
1184
  return;
1151
1185
  }
1152
1186
  }
1187
+ injectTaskUpdater(plugin) {
1188
+ if (typeof plugin.setTaskUpdater !== "function") {
1189
+ return;
1190
+ }
1191
+ if (typeof plugin.hasTaskUpdater === "function" && plugin.hasTaskUpdater()) {
1192
+ return;
1193
+ }
1194
+ if (!this.environment?.getComponent) {
1195
+ console.debug(`[LarkCliInstance] ${plugin.name || "plugin"} has setTaskUpdater but ` + `environment not injected (setEnvironment not called). ` + `chat_id 持久化将只在 in-memory,DB 不会写入。`);
1196
+ return;
1197
+ }
1198
+ const taskComponent = this.environment.getComponent("task");
1199
+ if (!taskComponent?.updateTask) {
1200
+ console.debug(`[LarkCliInstance] ${plugin.name || "plugin"} has setTaskUpdater but ` + `taskComponent or updateTask method not found. ` + `chat_id 持久化将只在 in-memory,DB 不会写入。`);
1201
+ return;
1202
+ }
1203
+ const taskUpdater = async (taskId, context) => {
1204
+ await taskComponent.updateTask(taskId, { context });
1205
+ };
1206
+ plugin.setTaskUpdater(taskUpdater);
1207
+ }
1153
1208
  unloadPlugins() {
1154
1209
  for (const plugin of this.loadedPlugins) {
1155
1210
  for (const hook of plugin.hooks) {
@@ -1170,6 +1225,30 @@ class LarkCliInstance {
1170
1225
  }
1171
1226
  }
1172
1227
  handleEvent(rawData) {
1228
+ let dedupKey;
1229
+ try {
1230
+ const parsed = JSON.parse(rawData);
1231
+ const eid = parsed.event_id ?? parsed.header?.event_id ?? parsed.id;
1232
+ if (typeof eid === "string" && eid.length > 0) {
1233
+ dedupKey = eid;
1234
+ }
1235
+ } catch {}
1236
+ if (dedupKey) {
1237
+ const now = Date.now();
1238
+ const last = this.recentEventIds.get(dedupKey);
1239
+ if (last !== undefined && now - last < this.DEDUP_WINDOW_MS) {
1240
+ logger.warn(`[LarkCliInstance] duplicate lark-cli event suppressed (event_id=${dedupKey}, ` + `lastSeen=${now - last}ms ago). ` + `Task #3977/3978: 防止同一飞书事件触发两次任务创建。`);
1241
+ return;
1242
+ }
1243
+ this.recentEventIds.set(dedupKey, now);
1244
+ if (this.recentEventIds.size > this.DEDUP_MAX_SIZE) {
1245
+ const cutoff = now - this.DEDUP_WINDOW_MS;
1246
+ for (const [k, t] of this.recentEventIds) {
1247
+ if (t < cutoff)
1248
+ this.recentEventIds.delete(k);
1249
+ }
1250
+ }
1251
+ }
1173
1252
  const provider = getTracerProvider();
1174
1253
  const tracer = provider.getTracer("roy-tracer");
1175
1254
  const rootTraceId = generateTraceId();
@@ -1300,6 +1379,7 @@ class LarkCliInstance {
1300
1379
  messageId: replyChannel.messageId,
1301
1380
  chatId: replyChannel.chatId,
1302
1381
  userId: replyChannel.params?.userId,
1382
+ appId: replyChannel.appId,
1303
1383
  profile,
1304
1384
  identity: "bot",
1305
1385
  sendText: async (text) => {
@@ -1436,7 +1516,7 @@ var EVENT_SOURCE_CONFIG_REGISTRATION = {
1436
1516
  };
1437
1517
 
1438
1518
  // src/env/event-source/event-source-component.ts
1439
- var logger = createLogger("event-source");
1519
+ var logger2 = createLogger("event-source");
1440
1520
 
1441
1521
  class EventSourceComponent extends BaseComponent {
1442
1522
  name = "event-source";
@@ -1503,7 +1583,7 @@ class EventSourceComponent extends BaseComponent {
1503
1583
  for (const handler of builtInHandlers) {
1504
1584
  if (!this.handlers.has(handler.type)) {
1505
1585
  this.handlers.set(handler.type, handler);
1506
- logger.debug(`Registered built-in handler: ${handler.type}`);
1586
+ logger2.debug(`Registered built-in handler: ${handler.type}`);
1507
1587
  }
1508
1588
  }
1509
1589
  }
@@ -1513,15 +1593,15 @@ class EventSourceComponent extends BaseComponent {
1513
1593
  }
1514
1594
  registerHandler(handler) {
1515
1595
  if (this.handlers.has(handler.type)) {
1516
- logger.warn(`Handler already registered for type: ${handler.type}, overwriting`);
1596
+ logger2.warn(`Handler already registered for type: ${handler.type}, overwriting`);
1517
1597
  }
1518
1598
  this.handlers.set(handler.type, handler);
1519
- logger.debug(`Handler registered: ${handler.type}`);
1599
+ logger2.debug(`Handler registered: ${handler.type}`);
1520
1600
  }
1521
1601
  unregisterHandler(type) {
1522
1602
  const deleted = this.handlers.delete(type);
1523
1603
  if (deleted) {
1524
- logger.debug(`Handler unregistered: ${type}`);
1604
+ logger2.debug(`Handler unregistered: ${type}`);
1525
1605
  }
1526
1606
  return deleted;
1527
1607
  }
@@ -1611,7 +1691,7 @@ class EventSourceComponent extends BaseComponent {
1611
1691
  });
1612
1692
  }
1613
1693
  onConfigChange(event) {
1614
- logger.info(`EventSource config changed: ${event.key}`, {
1694
+ logger2.info(`EventSource config changed: ${event.key}`, {
1615
1695
  oldValue: event.oldValue,
1616
1696
  newValue: event.newValue
1617
1697
  });
@@ -1630,9 +1710,9 @@ class EventSourceComponent extends BaseComponent {
1630
1710
  }
1631
1711
  this.sources.set(config.id, config);
1632
1712
  this.statuses.set(config.id, "created");
1633
- logger.debug(`EventSource registered: ${config.id} (${config.type})`);
1713
+ logger2.debug(`EventSource registered: ${config.id} (${config.type})`);
1634
1714
  this.saveConfig().catch((err) => {
1635
- logger.error(`Failed to save config after registering ${config.id}:`, err);
1715
+ logger2.error(`Failed to save config after registering ${config.id}:`, err);
1636
1716
  });
1637
1717
  }
1638
1718
  unregister(id) {
@@ -1647,9 +1727,9 @@ class EventSourceComponent extends BaseComponent {
1647
1727
  this.eventHandlers.delete(id);
1648
1728
  this.buffers.delete(id);
1649
1729
  if (existed) {
1650
- logger.debug(`EventSource unregistered: ${id}`);
1730
+ logger2.debug(`EventSource unregistered: ${id}`);
1651
1731
  this.saveConfig().catch((err) => {
1652
- logger.error(`Failed to save config after unregistering ${id}:`, err);
1732
+ logger2.error(`Failed to save config after unregistering ${id}:`, err);
1653
1733
  });
1654
1734
  }
1655
1735
  return existed;
@@ -1687,13 +1767,13 @@ class EventSourceComponent extends BaseComponent {
1687
1767
  }
1688
1768
  }
1689
1769
  this.sources.set(id, updated);
1690
- logger.info(`EventSource updated: ${id}`, {
1770
+ logger2.info(`EventSource updated: ${id}`, {
1691
1771
  fields: Object.keys(partial)
1692
1772
  });
1693
1773
  try {
1694
1774
  await this.saveConfig();
1695
1775
  } catch (err) {
1696
- logger.error(`Failed to save config after updating ${id}:`, err);
1776
+ logger2.error(`Failed to save config after updating ${id}:`, err);
1697
1777
  throw err;
1698
1778
  }
1699
1779
  return {
@@ -1751,6 +1831,9 @@ class EventSourceComponent extends BaseComponent {
1751
1831
  }
1752
1832
  const instance = handler.createInstance(config);
1753
1833
  this.instances.set(id, instance);
1834
+ if (typeof instance.setEnvironment === "function") {
1835
+ instance.setEnvironment(this.getEnv());
1836
+ }
1754
1837
  const eventHandler = this.eventHandlers.get(id) ?? this.createDefaultEventHandler(id, config.type);
1755
1838
  instance.onEvent(eventHandler);
1756
1839
  await instance.start();
@@ -1759,18 +1842,18 @@ class EventSourceComponent extends BaseComponent {
1759
1842
  type: "event-source.started",
1760
1843
  payload: { sourceId: id, sourceType: config.type }
1761
1844
  });
1762
- logger.info(`EventSource started: ${id} (${config.type})`);
1845
+ logger2.info(`EventSource started: ${id} (${config.type})`);
1763
1846
  } catch (error) {
1764
1847
  this.statuses.set(id, "error");
1765
- logger.error(`EventSource failed to start: ${id}`, error);
1848
+ logger2.error(`EventSource failed to start: ${id}`, error);
1766
1849
  throw error;
1767
1850
  }
1768
1851
  }
1769
1852
  async stopSource(id) {
1770
1853
  const status = this.statuses.get(id);
1771
- logger.info(`[EventSource ${id}] stopSource called, current status: ${status}`);
1854
+ logger2.info(`[EventSource ${id}] stopSource called, current status: ${status}`);
1772
1855
  if (status === "stopped" || status === "created") {
1773
- logger.info(`[EventSource ${id}] already stopped/created, skipping`);
1856
+ logger2.info(`[EventSource ${id}] already stopped/created, skipping`);
1774
1857
  return;
1775
1858
  }
1776
1859
  this.statuses.set(id, "stopping");
@@ -1786,10 +1869,10 @@ class EventSourceComponent extends BaseComponent {
1786
1869
  type: "event-source.stopped",
1787
1870
  payload: { sourceId: id, sourceType: config?.type }
1788
1871
  });
1789
- logger.info(`EventSource stopped: ${id}`);
1872
+ logger2.info(`EventSource stopped: ${id}`);
1790
1873
  } catch (error) {
1791
1874
  this.statuses.set(id, "error");
1792
- logger.error(`EventSource failed to stop: ${id}`, error);
1875
+ logger2.error(`EventSource failed to stop: ${id}`, error);
1793
1876
  throw error;
1794
1877
  }
1795
1878
  }
@@ -1814,9 +1897,9 @@ class EventSourceComponent extends BaseComponent {
1814
1897
  sources: Array.from(this.sources.values())
1815
1898
  };
1816
1899
  writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
1817
- logger.debug(`Saved ${this.sources.size} event source configurations`);
1900
+ logger2.debug(`Saved ${this.sources.size} event source configurations`);
1818
1901
  } catch (error) {
1819
- logger.error("Failed to save event source configurations:", error);
1902
+ logger2.error("Failed to save event source configurations:", error);
1820
1903
  throw error;
1821
1904
  }
1822
1905
  }
@@ -1826,13 +1909,13 @@ class EventSourceComponent extends BaseComponent {
1826
1909
  try {
1827
1910
  const configPath = this.getConfigFilePath();
1828
1911
  if (!existsSync(configPath)) {
1829
- logger.debug(`No event source config file found at ${configPath}`);
1912
+ logger2.debug(`No event source config file found at ${configPath}`);
1830
1913
  return;
1831
1914
  }
1832
1915
  const content = readFileSync(configPath, "utf-8");
1833
1916
  const config = JSON.parse(content);
1834
1917
  if (!config.sources || !Array.isArray(config.sources)) {
1835
- logger.warn("Invalid event source config format, skipping load");
1918
+ logger2.warn("Invalid event source config format, skipping load");
1836
1919
  return;
1837
1920
  }
1838
1921
  let loadedCount = 0;
@@ -1843,9 +1926,9 @@ class EventSourceComponent extends BaseComponent {
1843
1926
  loadedCount++;
1844
1927
  }
1845
1928
  }
1846
- logger.info(`Loaded ${loadedCount} event source configurations`);
1929
+ logger2.info(`Loaded ${loadedCount} event source configurations`);
1847
1930
  } catch (error) {
1848
- logger.error("Failed to load event source configurations:", error);
1931
+ logger2.error("Failed to load event source configurations:", error);
1849
1932
  }
1850
1933
  }
1851
1934
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-setting/roy-agent-core",
3
- "version": "1.7.30",
3
+ "version": "1.7.31",
4
4
  "type": "module",
5
5
  "description": "Core SDK for roy-agent - Environment, Components, Tools, Sessions, Tasks",
6
6
  "main": "./dist/index.js",