@mobcode/openclaw-plugin 0.1.24 → 0.1.26
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 +118 -40
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",
|
|
@@ -84,51 +159,57 @@ export function createMobcodePluginDefinition() {
|
|
|
84
159
|
api,
|
|
85
160
|
store,
|
|
86
161
|
});
|
|
87
|
-
api.on("after_tool_call", (event, ctx) => {
|
|
162
|
+
api.on("after_tool_call", async (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 ?? "-"}`,
|
|
170
|
+
);
|
|
171
|
+
if (!persistedMessage) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
console.warn(
|
|
175
|
+
"[mobcode-direct-debug] after_tool_call store target",
|
|
176
|
+
"rootDir=",
|
|
177
|
+
store.rootDir,
|
|
178
|
+
"databasePath=",
|
|
179
|
+
store.databasePath,
|
|
90
180
|
);
|
|
91
|
-
});
|
|
92
|
-
api.on("tool_result_persist", (event, ctx) => {
|
|
93
|
-
const toolCallId =
|
|
94
|
-
String(event?.message?.toolCallId ?? "").trim() ||
|
|
95
|
-
String(event?.toolCallId ?? "").trim() ||
|
|
96
|
-
String(ctx?.toolCallId ?? "").trim() ||
|
|
97
|
-
undefined;
|
|
98
|
-
const toolName =
|
|
99
|
-
String(event?.message?.toolName ?? "").trim() ||
|
|
100
|
-
String(event?.toolName ?? "").trim() ||
|
|
101
|
-
String(ctx?.toolName ?? "").trim() ||
|
|
102
|
-
undefined;
|
|
103
|
-
const messageId =
|
|
104
|
-
typeof event?.message?.id === "string" && event.message.id.trim()
|
|
105
|
-
? event.message.id.trim()
|
|
106
|
-
: toolCallId;
|
|
107
|
-
const persistedMessage = {
|
|
108
|
-
...(event?.message ?? {}),
|
|
109
|
-
role: event?.message?.role ?? "toolResult",
|
|
110
|
-
...(toolCallId ? { toolCallId } : {}),
|
|
111
|
-
...(toolName ? { toolName } : {}),
|
|
112
|
-
...(messageId ? { id: messageId } : {}),
|
|
113
|
-
};
|
|
114
181
|
console.warn(
|
|
115
|
-
"[mobcode-direct-debug]
|
|
182
|
+
"[mobcode-direct-debug] after_tool_call before appendMessage",
|
|
116
183
|
"sessionKey=",
|
|
117
184
|
String(ctx?.sessionKey ?? "").trim() || "-",
|
|
118
185
|
"toolCallId=",
|
|
119
|
-
toolCallId ?? "-",
|
|
186
|
+
persistedMessage.toolCallId ?? "-",
|
|
120
187
|
"toolName=",
|
|
121
|
-
toolName ?? "-",
|
|
188
|
+
persistedMessage.toolName ?? "-",
|
|
122
189
|
"messageId=",
|
|
123
190
|
messageId ?? "-",
|
|
124
|
-
"role=",
|
|
125
|
-
String(persistedMessage.role ?? "").trim() || "-",
|
|
126
|
-
);
|
|
127
|
-
log.warn(
|
|
128
|
-
`[mobcode-debug] tool_result_persist sessionKey=${String(ctx?.sessionKey ?? "").trim() || "-"} toolCallId=${toolCallId ?? "-"} toolName=${toolName ?? "-"} messageId=${messageId ?? "-"}`,
|
|
129
191
|
);
|
|
192
|
+
try {
|
|
193
|
+
const appendResult = await store.appendMessage({
|
|
194
|
+
sessionKey: ctx?.sessionKey,
|
|
195
|
+
message: persistedMessage,
|
|
196
|
+
messageId,
|
|
197
|
+
});
|
|
198
|
+
console.warn(
|
|
199
|
+
"[mobcode-direct-debug] after_tool_call appendMessage result",
|
|
200
|
+
JSON.stringify(appendResult ?? null),
|
|
201
|
+
);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
console.warn(
|
|
204
|
+
"[mobcode-direct-debug] after_tool_call appendMessage failed",
|
|
205
|
+
String(error),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
api.on("tool_result_persist", (event, ctx) => {
|
|
210
|
+
const { toolCallId, toolName } = resolveToolIdentity(event, ctx);
|
|
130
211
|
console.warn(
|
|
131
|
-
"[mobcode-direct-debug]
|
|
212
|
+
"[mobcode-direct-debug] tool_result_persist skip",
|
|
132
213
|
"sessionKey=",
|
|
133
214
|
String(ctx?.sessionKey ?? "").trim() || "-",
|
|
134
215
|
"toolCallId=",
|
|
@@ -136,12 +217,9 @@ export function createMobcodePluginDefinition() {
|
|
|
136
217
|
"toolName=",
|
|
137
218
|
toolName ?? "-",
|
|
138
219
|
);
|
|
139
|
-
|
|
140
|
-
sessionKey
|
|
141
|
-
|
|
142
|
-
messageId,
|
|
143
|
-
});
|
|
144
|
-
console.warn("[mobcode-direct-debug] after appendMessageSync");
|
|
220
|
+
log.warn(
|
|
221
|
+
`[mobcode-debug] tool_result_persist skipped sessionKey=${String(ctx?.sessionKey ?? "").trim() || "-"} toolCallId=${toolCallId ?? "-"} toolName=${toolName ?? "-"}`,
|
|
222
|
+
);
|
|
145
223
|
});
|
|
146
224
|
},
|
|
147
225
|
};
|