rol-websocket-channel 1.7.9 → 1.8.0
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.
- package/dist/index.js +58 -0
- package/index.ts +61 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -747,6 +747,7 @@ export default defineChannelPluginEntry({
|
|
|
747
747
|
},
|
|
748
748
|
registerFull(api) {
|
|
749
749
|
registerArtifactsTools(api);
|
|
750
|
+
registerToolEventHooks(api);
|
|
750
751
|
},
|
|
751
752
|
});
|
|
752
753
|
let legacyIsPluginRegistered = false;
|
|
@@ -879,3 +880,60 @@ function registerAdminBridgeCli(api) {
|
|
|
879
880
|
],
|
|
880
881
|
});
|
|
881
882
|
}
|
|
883
|
+
// ============================================
|
|
884
|
+
// 8. Tool 事件钩子:实时通过 MQTT 推送 AI 工具调用过程
|
|
885
|
+
// ============================================
|
|
886
|
+
function publishToolEvent(event) {
|
|
887
|
+
const conn = ConnectionManager.getGlobalConnection();
|
|
888
|
+
if (!conn || !conn.ws || !conn.ws.connected)
|
|
889
|
+
return;
|
|
890
|
+
let targetTopic = conn.topic;
|
|
891
|
+
if (targetTopic.endsWith("#")) {
|
|
892
|
+
targetTopic = targetTopic.slice(0, -1) + "app";
|
|
893
|
+
}
|
|
894
|
+
targetTopic = targetTopic.replace("device", "app").replace("bot", "app");
|
|
895
|
+
try {
|
|
896
|
+
conn.ws.publish(targetTopic, JSON.stringify(event));
|
|
897
|
+
}
|
|
898
|
+
catch {
|
|
899
|
+
// 忽略发布错误,避免影响主流程
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
function registerToolEventHooks(api) {
|
|
903
|
+
// before_tool_call:AI 即将调用某个工具时触发
|
|
904
|
+
api.registerHook("before_tool_call", (event, ctx) => {
|
|
905
|
+
publishToolEvent({
|
|
906
|
+
type: "receiver",
|
|
907
|
+
source: "tools",
|
|
908
|
+
event: "before_tool_call",
|
|
909
|
+
timestamp: Date.now(),
|
|
910
|
+
data: {
|
|
911
|
+
toolName: event.toolName,
|
|
912
|
+
toolCallId: event.toolCallId,
|
|
913
|
+
runId: event.runId ?? ctx.runId,
|
|
914
|
+
sessionKey: ctx.sessionKey,
|
|
915
|
+
agentId: ctx.agentId,
|
|
916
|
+
params: event.params,
|
|
917
|
+
},
|
|
918
|
+
});
|
|
919
|
+
});
|
|
920
|
+
// after_tool_call:工具执行完毕(含结果或错误)时触发
|
|
921
|
+
api.registerHook("after_tool_call", (event, ctx) => {
|
|
922
|
+
publishToolEvent({
|
|
923
|
+
type: "tool_event",
|
|
924
|
+
source: "system",
|
|
925
|
+
event: "after_tool_call",
|
|
926
|
+
timestamp: Date.now(),
|
|
927
|
+
data: {
|
|
928
|
+
toolName: event.toolName,
|
|
929
|
+
toolCallId: event.toolCallId,
|
|
930
|
+
runId: event.runId ?? ctx.runId,
|
|
931
|
+
sessionKey: ctx.sessionKey,
|
|
932
|
+
agentId: ctx.agentId,
|
|
933
|
+
durationMs: event.durationMs,
|
|
934
|
+
error: event.error,
|
|
935
|
+
result: event.result,
|
|
936
|
+
},
|
|
937
|
+
});
|
|
938
|
+
});
|
|
939
|
+
}
|
package/index.ts
CHANGED
|
@@ -936,6 +936,7 @@ export default defineChannelPluginEntry({
|
|
|
936
936
|
},
|
|
937
937
|
registerFull(api) {
|
|
938
938
|
registerArtifactsTools(api);
|
|
939
|
+
registerToolEventHooks(api);
|
|
939
940
|
},
|
|
940
941
|
});
|
|
941
942
|
|
|
@@ -1129,3 +1130,63 @@ function registerAdminBridgeCli(api: OpenClawPluginApi) {
|
|
|
1129
1130
|
},
|
|
1130
1131
|
);
|
|
1131
1132
|
}
|
|
1133
|
+
|
|
1134
|
+
// ============================================
|
|
1135
|
+
// 8. Tool 事件钩子:实时通过 MQTT 推送 AI 工具调用过程
|
|
1136
|
+
// ============================================
|
|
1137
|
+
function publishToolEvent(event: object): void {
|
|
1138
|
+
const conn = ConnectionManager.getGlobalConnection();
|
|
1139
|
+
if (!conn || !conn.ws || !conn.ws.connected) return;
|
|
1140
|
+
|
|
1141
|
+
let targetTopic = conn.topic;
|
|
1142
|
+
if (targetTopic.endsWith("#")) {
|
|
1143
|
+
targetTopic = targetTopic.slice(0, -1) + "app";
|
|
1144
|
+
}
|
|
1145
|
+
targetTopic = targetTopic.replace("device", "app").replace("bot", "app");
|
|
1146
|
+
|
|
1147
|
+
try {
|
|
1148
|
+
conn.ws.publish(targetTopic, JSON.stringify(event));
|
|
1149
|
+
} catch {
|
|
1150
|
+
// 忽略发布错误,避免影响主流程
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
function registerToolEventHooks(api: any): void {
|
|
1155
|
+
// before_tool_call:AI 即将调用某个工具时触发
|
|
1156
|
+
api.registerHook("before_tool_call", (event: any, ctx: any) => {
|
|
1157
|
+
publishToolEvent({
|
|
1158
|
+
type: "receiver",
|
|
1159
|
+
source: "tools",
|
|
1160
|
+
event: "before_tool_call",
|
|
1161
|
+
timestamp: Date.now(),
|
|
1162
|
+
data: {
|
|
1163
|
+
toolName: event.toolName,
|
|
1164
|
+
toolCallId: event.toolCallId,
|
|
1165
|
+
runId: event.runId ?? ctx.runId,
|
|
1166
|
+
sessionKey: ctx.sessionKey,
|
|
1167
|
+
agentId: ctx.agentId,
|
|
1168
|
+
params: event.params,
|
|
1169
|
+
},
|
|
1170
|
+
});
|
|
1171
|
+
});
|
|
1172
|
+
|
|
1173
|
+
// after_tool_call:工具执行完毕(含结果或错误)时触发
|
|
1174
|
+
api.registerHook("after_tool_call", (event: any, ctx: any) => {
|
|
1175
|
+
publishToolEvent({
|
|
1176
|
+
type: "tool_event",
|
|
1177
|
+
source: "system",
|
|
1178
|
+
event: "after_tool_call",
|
|
1179
|
+
timestamp: Date.now(),
|
|
1180
|
+
data: {
|
|
1181
|
+
toolName: event.toolName,
|
|
1182
|
+
toolCallId: event.toolCallId,
|
|
1183
|
+
runId: event.runId ?? ctx.runId,
|
|
1184
|
+
sessionKey: ctx.sessionKey,
|
|
1185
|
+
agentId: ctx.agentId,
|
|
1186
|
+
durationMs: event.durationMs,
|
|
1187
|
+
error: event.error,
|
|
1188
|
+
result: event.result,
|
|
1189
|
+
},
|
|
1190
|
+
});
|
|
1191
|
+
});
|
|
1192
|
+
}
|