@mobcode/openclaw-plugin 0.1.24 → 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 +100 -39
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,50 +160,39 @@ 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
|
-
|
|
93
|
-
|
|
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
|
-
};
|
|
171
|
+
if (!persistedMessage) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
114
174
|
console.warn(
|
|
115
|
-
"[mobcode-direct-debug]
|
|
175
|
+
"[mobcode-direct-debug] after_tool_call before appendMessageSync",
|
|
116
176
|
"sessionKey=",
|
|
117
177
|
String(ctx?.sessionKey ?? "").trim() || "-",
|
|
118
178
|
"toolCallId=",
|
|
119
|
-
toolCallId ?? "-",
|
|
179
|
+
persistedMessage.toolCallId ?? "-",
|
|
120
180
|
"toolName=",
|
|
121
|
-
toolName ?? "-",
|
|
181
|
+
persistedMessage.toolName ?? "-",
|
|
122
182
|
"messageId=",
|
|
123
183
|
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
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);
|
|
130
194
|
console.warn(
|
|
131
|
-
"[mobcode-direct-debug]
|
|
195
|
+
"[mobcode-direct-debug] tool_result_persist skip",
|
|
132
196
|
"sessionKey=",
|
|
133
197
|
String(ctx?.sessionKey ?? "").trim() || "-",
|
|
134
198
|
"toolCallId=",
|
|
@@ -136,12 +200,9 @@ export function createMobcodePluginDefinition() {
|
|
|
136
200
|
"toolName=",
|
|
137
201
|
toolName ?? "-",
|
|
138
202
|
);
|
|
139
|
-
|
|
140
|
-
sessionKey
|
|
141
|
-
|
|
142
|
-
messageId,
|
|
143
|
-
});
|
|
144
|
-
console.warn("[mobcode-direct-debug] after appendMessageSync");
|
|
203
|
+
log.warn(
|
|
204
|
+
`[mobcode-debug] tool_result_persist skipped sessionKey=${String(ctx?.sessionKey ?? "").trim() || "-"} toolCallId=${toolCallId ?? "-"} toolName=${toolName ?? "-"}`,
|
|
205
|
+
);
|
|
145
206
|
});
|
|
146
207
|
},
|
|
147
208
|
};
|