rol-websocket-channel 1.8.3 → 1.8.4
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 +34 -0
- package/index.ts +36 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -899,6 +899,22 @@ function publishToolEvent(event) {
|
|
|
899
899
|
console.error("[mqtt] Error publishing tool event:", err);
|
|
900
900
|
}
|
|
901
901
|
}
|
|
902
|
+
function truncateDeep(obj, maxLen = 1000) {
|
|
903
|
+
if (typeof obj === "string") {
|
|
904
|
+
return obj.length > maxLen ? obj.slice(0, maxLen) + "...[truncated]" : obj;
|
|
905
|
+
}
|
|
906
|
+
if (Array.isArray(obj)) {
|
|
907
|
+
return obj.map(v => truncateDeep(v, maxLen));
|
|
908
|
+
}
|
|
909
|
+
if (obj !== null && typeof obj === "object") {
|
|
910
|
+
const res = {};
|
|
911
|
+
for (const k of Object.keys(obj)) {
|
|
912
|
+
res[k] = truncateDeep(obj[k], maxLen);
|
|
913
|
+
}
|
|
914
|
+
return res;
|
|
915
|
+
}
|
|
916
|
+
return obj;
|
|
917
|
+
}
|
|
902
918
|
function registerToolEventHooks(api) {
|
|
903
919
|
// before_tool_call:AI 即将调用某个工具时触发
|
|
904
920
|
if (typeof api.on === 'function') {
|
|
@@ -937,6 +953,24 @@ function registerToolEventHooks(api) {
|
|
|
937
953
|
},
|
|
938
954
|
});
|
|
939
955
|
});
|
|
956
|
+
// llm_output:大模型生成回复后触发,可获取 thinking 内容
|
|
957
|
+
api.on("llm_output", (event, ctx) => {
|
|
958
|
+
publishToolEvent({
|
|
959
|
+
type: "receiver",
|
|
960
|
+
source: "thinking",
|
|
961
|
+
event: "llm_output",
|
|
962
|
+
timestamp: Date.now(),
|
|
963
|
+
data: {
|
|
964
|
+
runId: event.runId ?? ctx.runId,
|
|
965
|
+
sessionKey: ctx.sessionKey ?? event.sessionId,
|
|
966
|
+
provider: event.provider,
|
|
967
|
+
model: event.model,
|
|
968
|
+
assistantTexts: truncateDeep(event.assistantTexts, 1000),
|
|
969
|
+
lastAssistant: truncateDeep(event.lastAssistant, 1000),
|
|
970
|
+
usage: event.usage,
|
|
971
|
+
},
|
|
972
|
+
});
|
|
973
|
+
});
|
|
940
974
|
}
|
|
941
975
|
else {
|
|
942
976
|
console.error("[mqtt] api.on is not available. Cannot register tool event hooks.");
|
package/index.ts
CHANGED
|
@@ -1151,6 +1151,23 @@ function publishToolEvent(event: object): void {
|
|
|
1151
1151
|
}
|
|
1152
1152
|
}
|
|
1153
1153
|
|
|
1154
|
+
function truncateDeep(obj: any, maxLen = 1000): any {
|
|
1155
|
+
if (typeof obj === "string") {
|
|
1156
|
+
return obj.length > maxLen ? obj.slice(0, maxLen) + "...[truncated]" : obj;
|
|
1157
|
+
}
|
|
1158
|
+
if (Array.isArray(obj)) {
|
|
1159
|
+
return obj.map(v => truncateDeep(v, maxLen));
|
|
1160
|
+
}
|
|
1161
|
+
if (obj !== null && typeof obj === "object") {
|
|
1162
|
+
const res: any = {};
|
|
1163
|
+
for (const k of Object.keys(obj)) {
|
|
1164
|
+
res[k] = truncateDeep(obj[k], maxLen);
|
|
1165
|
+
}
|
|
1166
|
+
return res;
|
|
1167
|
+
}
|
|
1168
|
+
return obj;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1154
1171
|
function registerToolEventHooks(api: any): void {
|
|
1155
1172
|
// before_tool_call:AI 即将调用某个工具时触发
|
|
1156
1173
|
if (typeof api.on === 'function') {
|
|
@@ -1190,6 +1207,25 @@ function registerToolEventHooks(api: any): void {
|
|
|
1190
1207
|
},
|
|
1191
1208
|
});
|
|
1192
1209
|
});
|
|
1210
|
+
|
|
1211
|
+
// llm_output:大模型生成回复后触发,可获取 thinking 内容
|
|
1212
|
+
api.on("llm_output", (event: any, ctx: any) => {
|
|
1213
|
+
publishToolEvent({
|
|
1214
|
+
type: "receiver",
|
|
1215
|
+
source: "thinking",
|
|
1216
|
+
event: "llm_output",
|
|
1217
|
+
timestamp: Date.now(),
|
|
1218
|
+
data: {
|
|
1219
|
+
runId: event.runId ?? ctx.runId,
|
|
1220
|
+
sessionKey: ctx.sessionKey ?? event.sessionId,
|
|
1221
|
+
provider: event.provider,
|
|
1222
|
+
model: event.model,
|
|
1223
|
+
assistantTexts: truncateDeep(event.assistantTexts, 1000),
|
|
1224
|
+
lastAssistant: truncateDeep(event.lastAssistant, 1000),
|
|
1225
|
+
usage: event.usage,
|
|
1226
|
+
},
|
|
1227
|
+
});
|
|
1228
|
+
});
|
|
1193
1229
|
} else {
|
|
1194
1230
|
console.error("[mqtt] api.on is not available. Cannot register tool event hooks.");
|
|
1195
1231
|
}
|