@mobcode/openclaw-plugin 0.1.23 → 0.1.25
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/package.json +1 -1
- package/src/plugin-definition.js +103 -26
package/package.json
CHANGED
package/src/plugin-definition.js
CHANGED
|
@@ -15,6 +15,10 @@ import { registerMobcodeRuntimeObservers } from "./runtime-events.js";
|
|
|
15
15
|
import { MobcodeStateStore } from "./state-store.js";
|
|
16
16
|
import { registerMobcodeTools } from "./plugin-tools.js";
|
|
17
17
|
|
|
18
|
+
function cloneJson(value) {
|
|
19
|
+
return value === undefined ? undefined : JSON.parse(JSON.stringify(value));
|
|
20
|
+
}
|
|
21
|
+
|
|
18
22
|
function createSafePluginLogger(api) {
|
|
19
23
|
const pluginLogger = api?.logger;
|
|
20
24
|
return {
|
|
@@ -29,6 +33,77 @@ function describeLoggerShape(api) {
|
|
|
29
33
|
return `info=${typeof api?.logger?.info} warn=${typeof api?.logger?.warn} error=${typeof api?.logger?.error} debug=${typeof api?.logger?.debug}`;
|
|
30
34
|
}
|
|
31
35
|
|
|
36
|
+
function resolveToolIdentity(event, ctx) {
|
|
37
|
+
const toolCallId =
|
|
38
|
+
String(event?.message?.toolCallId ?? "").trim() ||
|
|
39
|
+
String(event?.toolCallId ?? "").trim() ||
|
|
40
|
+
String(ctx?.toolCallId ?? "").trim() ||
|
|
41
|
+
undefined;
|
|
42
|
+
const toolName =
|
|
43
|
+
String(event?.message?.toolName ?? "").trim() ||
|
|
44
|
+
String(event?.toolName ?? "").trim() ||
|
|
45
|
+
String(ctx?.toolName ?? "").trim() ||
|
|
46
|
+
undefined;
|
|
47
|
+
return { toolCallId, toolName };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function buildPersistedToolResultFromAfterToolCall(event, ctx) {
|
|
51
|
+
const { toolCallId, toolName } = resolveToolIdentity(event, ctx);
|
|
52
|
+
if (!toolCallId) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const clonedResult = cloneJson(event?.result);
|
|
57
|
+
if (
|
|
58
|
+
clonedResult &&
|
|
59
|
+
typeof clonedResult === "object" &&
|
|
60
|
+
!Array.isArray(clonedResult) &&
|
|
61
|
+
String(clonedResult.role ?? "").trim() === "toolResult"
|
|
62
|
+
) {
|
|
63
|
+
return {
|
|
64
|
+
...(clonedResult ?? {}),
|
|
65
|
+
...(toolCallId ? { toolCallId } : {}),
|
|
66
|
+
...(toolName ? { toolName } : {}),
|
|
67
|
+
id:
|
|
68
|
+
typeof clonedResult.id === "string" && clonedResult.id.trim()
|
|
69
|
+
? clonedResult.id.trim()
|
|
70
|
+
: toolCallId,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const content = Array.isArray(clonedResult?.content)
|
|
75
|
+
? clonedResult.content
|
|
76
|
+
: [
|
|
77
|
+
{
|
|
78
|
+
type: "text",
|
|
79
|
+
text:
|
|
80
|
+
typeof clonedResult?.aggregated === "string" && clonedResult.aggregated.trim()
|
|
81
|
+
? clonedResult.aggregated
|
|
82
|
+
: typeof event?.error === "string" && event.error.trim()
|
|
83
|
+
? event.error
|
|
84
|
+
: typeof clonedResult === "string"
|
|
85
|
+
? clonedResult
|
|
86
|
+
: JSON.stringify(clonedResult ?? null),
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
role: "toolResult",
|
|
92
|
+
toolCallId,
|
|
93
|
+
...(toolName ? { toolName } : {}),
|
|
94
|
+
content,
|
|
95
|
+
...(clonedResult && typeof clonedResult === "object" && !Array.isArray(clonedResult)
|
|
96
|
+
? { details: clonedResult }
|
|
97
|
+
: {}),
|
|
98
|
+
isError:
|
|
99
|
+
clonedResult?.isError === true ||
|
|
100
|
+
clonedResult?.is_error === true ||
|
|
101
|
+
(typeof event?.error === "string" && event.error.trim().length > 0),
|
|
102
|
+
timestamp: Date.now(),
|
|
103
|
+
id: toolCallId,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
32
107
|
export function createMobcodePluginDefinition() {
|
|
33
108
|
return {
|
|
34
109
|
id: "mobcode",
|
|
@@ -85,47 +160,49 @@ export function createMobcodePluginDefinition() {
|
|
|
85
160
|
store,
|
|
86
161
|
});
|
|
87
162
|
api.on("after_tool_call", (event, ctx) => {
|
|
163
|
+
const persistedMessage = buildPersistedToolResultFromAfterToolCall(event, ctx);
|
|
164
|
+
const messageId =
|
|
165
|
+
typeof persistedMessage?.id === "string" && persistedMessage.id.trim()
|
|
166
|
+
? persistedMessage.id.trim()
|
|
167
|
+
: undefined;
|
|
88
168
|
log.warn(
|
|
89
|
-
`[mobcode-debug] after_tool_call sessionKey=${String(ctx?.sessionKey ?? "").trim() || "-"} toolCallId=${String(event?.toolCallId ?? "").trim() || "-"} toolName=${String(event?.toolName ?? "").trim() || "-"} durationMs=${typeof event?.durationMs === "number" ? String(event.durationMs) : "-"}`,
|
|
169
|
+
`[mobcode-debug] after_tool_call sessionKey=${String(ctx?.sessionKey ?? "").trim() || "-"} toolCallId=${String(event?.toolCallId ?? "").trim() || "-"} toolName=${String(event?.toolName ?? "").trim() || "-"} durationMs=${typeof event?.durationMs === "number" ? String(event.durationMs) : "-"} messageId=${messageId ?? "-"}`,
|
|
90
170
|
);
|
|
91
|
-
|
|
92
|
-
|
|
171
|
+
if (!persistedMessage) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
93
174
|
console.warn(
|
|
94
|
-
"[mobcode-direct-debug]
|
|
175
|
+
"[mobcode-direct-debug] after_tool_call before appendMessageSync",
|
|
95
176
|
"sessionKey=",
|
|
96
177
|
String(ctx?.sessionKey ?? "").trim() || "-",
|
|
97
178
|
"toolCallId=",
|
|
98
|
-
|
|
179
|
+
persistedMessage.toolCallId ?? "-",
|
|
99
180
|
"toolName=",
|
|
100
|
-
|
|
181
|
+
persistedMessage.toolName ?? "-",
|
|
101
182
|
"messageId=",
|
|
102
|
-
|
|
103
|
-
? event.message.id.trim()
|
|
104
|
-
: "-",
|
|
105
|
-
"role=",
|
|
106
|
-
String(event?.message?.role ?? "").trim() || "-",
|
|
107
|
-
);
|
|
108
|
-
log.warn(
|
|
109
|
-
`[mobcode-debug] tool_result_persist sessionKey=${String(ctx?.sessionKey ?? "").trim() || "-"} toolCallId=${String(event?.toolCallId ?? "").trim() || "-"} toolName=${String(event?.toolName ?? "").trim() || "-"} messageId=${typeof event?.message?.id === "string" && event.message.id.trim() ? event.message.id.trim() : "-"}`,
|
|
183
|
+
messageId ?? "-",
|
|
110
184
|
);
|
|
185
|
+
store.appendMessageSync({
|
|
186
|
+
sessionKey: ctx?.sessionKey,
|
|
187
|
+
message: persistedMessage,
|
|
188
|
+
messageId,
|
|
189
|
+
});
|
|
190
|
+
console.warn("[mobcode-direct-debug] after_tool_call after appendMessageSync");
|
|
191
|
+
});
|
|
192
|
+
api.on("tool_result_persist", (event, ctx) => {
|
|
193
|
+
const { toolCallId, toolName } = resolveToolIdentity(event, ctx);
|
|
111
194
|
console.warn(
|
|
112
|
-
"[mobcode-direct-debug]
|
|
195
|
+
"[mobcode-direct-debug] tool_result_persist skip",
|
|
113
196
|
"sessionKey=",
|
|
114
197
|
String(ctx?.sessionKey ?? "").trim() || "-",
|
|
115
198
|
"toolCallId=",
|
|
116
|
-
|
|
199
|
+
toolCallId ?? "-",
|
|
117
200
|
"toolName=",
|
|
118
|
-
|
|
201
|
+
toolName ?? "-",
|
|
202
|
+
);
|
|
203
|
+
log.warn(
|
|
204
|
+
`[mobcode-debug] tool_result_persist skipped sessionKey=${String(ctx?.sessionKey ?? "").trim() || "-"} toolCallId=${toolCallId ?? "-"} toolName=${toolName ?? "-"}`,
|
|
119
205
|
);
|
|
120
|
-
store.appendMessageSync({
|
|
121
|
-
sessionKey: ctx?.sessionKey,
|
|
122
|
-
message: event?.message,
|
|
123
|
-
messageId:
|
|
124
|
-
typeof event?.message?.id === "string" && event.message.id.trim()
|
|
125
|
-
? event.message.id.trim()
|
|
126
|
-
: undefined,
|
|
127
|
-
});
|
|
128
|
-
console.warn("[mobcode-direct-debug] after appendMessageSync");
|
|
129
206
|
});
|
|
130
207
|
},
|
|
131
208
|
};
|