nextclaw-core 0.4.2 → 0.4.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.d.ts +1 -1
- package/dist/index.js +63 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -148,7 +148,7 @@ declare class SessionManager {
|
|
|
148
148
|
getOrCreate(key: string): Session;
|
|
149
149
|
getIfExists(key: string): Session | null;
|
|
150
150
|
addMessage(session: Session, role: string, content: string, extra?: Record<string, unknown>): void;
|
|
151
|
-
getHistory(session: Session, maxMessages?: number): Array<Record<string,
|
|
151
|
+
getHistory(session: Session, maxMessages?: number): Array<Record<string, unknown>>;
|
|
152
152
|
clear(session: Session): void;
|
|
153
153
|
private load;
|
|
154
154
|
save(session: Session): void;
|
package/dist/index.js
CHANGED
|
@@ -982,16 +982,17 @@ var ExecTool = class extends Tool {
|
|
|
982
982
|
"\\brm\\s+-[rf]{1,2}\\b",
|
|
983
983
|
"\\bdel\\s+/[fq]\\b",
|
|
984
984
|
"\\brmdir\\s+/s\\b",
|
|
985
|
-
"\\b(format|mkfs|diskpart)\\b",
|
|
986
985
|
"\\bdd\\s+if=",
|
|
987
986
|
">\\s*/dev/sd",
|
|
988
987
|
"\\b(shutdown|reboot|poweroff)\\b",
|
|
989
988
|
":\\(\\)\\s*\\{.*\\};\\s*:"
|
|
990
989
|
]).map((pattern) => new RegExp(pattern, "i"));
|
|
991
990
|
this.allowPatterns = (options.allowPatterns ?? []).map((pattern) => new RegExp(pattern, "i"));
|
|
991
|
+
this.dangerousCommands = ["format", "diskpart", "mkfs"];
|
|
992
992
|
}
|
|
993
993
|
denyPatterns;
|
|
994
994
|
allowPatterns;
|
|
995
|
+
dangerousCommands;
|
|
995
996
|
get name() {
|
|
996
997
|
return "exec";
|
|
997
998
|
}
|
|
@@ -1037,6 +1038,9 @@ ${stderr}`);
|
|
|
1037
1038
|
}
|
|
1038
1039
|
guardCommand(command, cwd) {
|
|
1039
1040
|
const normalized = command.trim().toLowerCase();
|
|
1041
|
+
if (this.isDangerousCommand(normalized)) {
|
|
1042
|
+
return "Error: Command blocked by safety guard (dangerous pattern detected)";
|
|
1043
|
+
}
|
|
1040
1044
|
for (const pattern of this.denyPatterns) {
|
|
1041
1045
|
if (pattern.test(normalized)) {
|
|
1042
1046
|
return "Error: Command blocked by safety guard (dangerous pattern detected)";
|
|
@@ -1062,6 +1066,26 @@ ${stderr}`);
|
|
|
1062
1066
|
}
|
|
1063
1067
|
return null;
|
|
1064
1068
|
}
|
|
1069
|
+
isDangerousCommand(command) {
|
|
1070
|
+
const segments = command.split(/\s*(?:\|\||&&|;|\|)\s*/);
|
|
1071
|
+
for (const segment2 of segments) {
|
|
1072
|
+
const match = segment2.trim().match(/^(?:sudo\s+)?([^\s]+)/i);
|
|
1073
|
+
if (!match) {
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
const token = match[1]?.toLowerCase() ?? "";
|
|
1077
|
+
if (!token) {
|
|
1078
|
+
continue;
|
|
1079
|
+
}
|
|
1080
|
+
if (this.dangerousCommands.includes(token)) {
|
|
1081
|
+
return true;
|
|
1082
|
+
}
|
|
1083
|
+
if (token.startsWith("mkfs")) {
|
|
1084
|
+
return true;
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
return false;
|
|
1088
|
+
}
|
|
1065
1089
|
};
|
|
1066
1090
|
function truncateOutput(result, maxLen = 1e4) {
|
|
1067
1091
|
if (result.length <= maxLen) {
|
|
@@ -2296,7 +2320,25 @@ var SessionManager = class {
|
|
|
2296
2320
|
}
|
|
2297
2321
|
getHistory(session, maxMessages = 50) {
|
|
2298
2322
|
const recent = session.messages.length > maxMessages ? session.messages.slice(-maxMessages) : session.messages;
|
|
2299
|
-
return recent.map((msg) =>
|
|
2323
|
+
return recent.map((msg) => {
|
|
2324
|
+
const entry = {
|
|
2325
|
+
role: msg.role,
|
|
2326
|
+
content: msg.content
|
|
2327
|
+
};
|
|
2328
|
+
if (typeof msg.name === "string") {
|
|
2329
|
+
entry.name = msg.name;
|
|
2330
|
+
}
|
|
2331
|
+
if (typeof msg.tool_call_id === "string") {
|
|
2332
|
+
entry.tool_call_id = msg.tool_call_id;
|
|
2333
|
+
}
|
|
2334
|
+
if (Array.isArray(msg.tool_calls)) {
|
|
2335
|
+
entry.tool_calls = msg.tool_calls;
|
|
2336
|
+
}
|
|
2337
|
+
if (typeof msg.reasoning_content === "string" && msg.reasoning_content) {
|
|
2338
|
+
entry.reasoning_content = msg.reasoning_content;
|
|
2339
|
+
}
|
|
2340
|
+
return entry;
|
|
2341
|
+
});
|
|
2300
2342
|
}
|
|
2301
2343
|
clear(session) {
|
|
2302
2344
|
session.messages = [];
|
|
@@ -2536,6 +2578,7 @@ var AgentLoop = class {
|
|
|
2536
2578
|
chatId: msg.chatId,
|
|
2537
2579
|
sessionKey
|
|
2538
2580
|
});
|
|
2581
|
+
this.sessions.addMessage(session, "user", msg.content);
|
|
2539
2582
|
let iteration = 0;
|
|
2540
2583
|
let finalContent = null;
|
|
2541
2584
|
const maxIterations = this.options.maxIterations ?? 20;
|
|
@@ -2556,9 +2599,17 @@ var AgentLoop = class {
|
|
|
2556
2599
|
}
|
|
2557
2600
|
}));
|
|
2558
2601
|
this.context.addAssistantMessage(messages, response.content, toolCallDicts, response.reasoningContent ?? null);
|
|
2602
|
+
this.sessions.addMessage(session, "assistant", response.content ?? "", {
|
|
2603
|
+
tool_calls: toolCallDicts,
|
|
2604
|
+
reasoning_content: response.reasoningContent ?? null
|
|
2605
|
+
});
|
|
2559
2606
|
for (const call of response.toolCalls) {
|
|
2560
2607
|
const result = await this.tools.execute(call.name, call.arguments);
|
|
2561
2608
|
this.context.addToolResult(messages, call.id, call.name, result);
|
|
2609
|
+
this.sessions.addMessage(session, "tool", result, {
|
|
2610
|
+
tool_call_id: call.id,
|
|
2611
|
+
name: call.name
|
|
2612
|
+
});
|
|
2562
2613
|
}
|
|
2563
2614
|
} else {
|
|
2564
2615
|
finalContent = response.content;
|
|
@@ -2571,11 +2622,9 @@ var AgentLoop = class {
|
|
|
2571
2622
|
const { content: cleanedContent, replyTo } = parseReplyTags(finalContent, messageId);
|
|
2572
2623
|
finalContent = cleanedContent;
|
|
2573
2624
|
if (isSilentReplyText(finalContent, SILENT_REPLY_TOKEN)) {
|
|
2574
|
-
this.sessions.addMessage(session, "user", msg.content);
|
|
2575
2625
|
this.sessions.save(session);
|
|
2576
2626
|
return null;
|
|
2577
2627
|
}
|
|
2578
|
-
this.sessions.addMessage(session, "user", msg.content);
|
|
2579
2628
|
this.sessions.addMessage(session, "assistant", finalContent);
|
|
2580
2629
|
this.sessions.save(session);
|
|
2581
2630
|
return {
|
|
@@ -2610,6 +2659,7 @@ var AgentLoop = class {
|
|
|
2610
2659
|
chatId: originChatId,
|
|
2611
2660
|
sessionKey
|
|
2612
2661
|
});
|
|
2662
|
+
this.sessions.addMessage(session, "user", `[System: ${msg.senderId}] ${msg.content}`);
|
|
2613
2663
|
let iteration = 0;
|
|
2614
2664
|
let finalContent = null;
|
|
2615
2665
|
const maxIterations = this.options.maxIterations ?? 20;
|
|
@@ -2630,9 +2680,17 @@ var AgentLoop = class {
|
|
|
2630
2680
|
}
|
|
2631
2681
|
}));
|
|
2632
2682
|
this.context.addAssistantMessage(messages, response.content, toolCallDicts, response.reasoningContent ?? null);
|
|
2683
|
+
this.sessions.addMessage(session, "assistant", response.content ?? "", {
|
|
2684
|
+
tool_calls: toolCallDicts,
|
|
2685
|
+
reasoning_content: response.reasoningContent ?? null
|
|
2686
|
+
});
|
|
2633
2687
|
for (const call of response.toolCalls) {
|
|
2634
2688
|
const result = await this.tools.execute(call.name, call.arguments);
|
|
2635
2689
|
this.context.addToolResult(messages, call.id, call.name, result);
|
|
2690
|
+
this.sessions.addMessage(session, "tool", result, {
|
|
2691
|
+
tool_call_id: call.id,
|
|
2692
|
+
name: call.name
|
|
2693
|
+
});
|
|
2636
2694
|
}
|
|
2637
2695
|
} else {
|
|
2638
2696
|
finalContent = response.content;
|
|
@@ -2645,9 +2703,9 @@ var AgentLoop = class {
|
|
|
2645
2703
|
const { content: cleanedContent, replyTo } = parseReplyTags(finalContent, void 0);
|
|
2646
2704
|
finalContent = cleanedContent;
|
|
2647
2705
|
if (isSilentReplyText(finalContent, SILENT_REPLY_TOKEN)) {
|
|
2706
|
+
this.sessions.save(session);
|
|
2648
2707
|
return null;
|
|
2649
2708
|
}
|
|
2650
|
-
this.sessions.addMessage(session, "user", `[System: ${msg.senderId}] ${msg.content}`);
|
|
2651
2709
|
this.sessions.addMessage(session, "assistant", finalContent);
|
|
2652
2710
|
this.sessions.save(session);
|
|
2653
2711
|
return {
|