@nextclaw/kernel 0.5.1-beta.1 → 0.5.1
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 +30 -6
- package/dist/index.js.map +1 -1
- package/package.json +14 -14
package/dist/index.js
CHANGED
|
@@ -3433,29 +3433,53 @@ function upsertNcpAgentSessionSummaryEvent(params) {
|
|
|
3433
3433
|
async function replayNcpAgentSessionEvents(events) {
|
|
3434
3434
|
const stateManager = new DefaultNcpAgentConversationStateManager();
|
|
3435
3435
|
const knownMessageIds = /* @__PURE__ */ new Set();
|
|
3436
|
+
const toolResultsByCallId = /* @__PURE__ */ new Map();
|
|
3436
3437
|
for (const event of events) {
|
|
3437
3438
|
if (isJournalOnlyEvent(event)) continue;
|
|
3438
|
-
const replayEvent = createReplayEvent(event);
|
|
3439
|
+
const replayEvent = createReplayEvent(event, toolResultsByCallId);
|
|
3439
3440
|
const bootstrapEvent = createReplayStreamingBootstrapEvent(replayEvent, knownMessageIds);
|
|
3440
3441
|
if (bootstrapEvent) await stateManager.dispatch(bootstrapEvent);
|
|
3441
3442
|
rememberReplayMessageId(replayEvent, knownMessageIds);
|
|
3442
3443
|
await stateManager.dispatch(replayEvent);
|
|
3444
|
+
if (replayEvent.type === NcpEventType.MessageToolCallResult) toolResultsByCallId.set(replayEvent.payload.toolCallId, replayEvent.payload);
|
|
3443
3445
|
}
|
|
3444
3446
|
const snapshot = stateManager.getSnapshot();
|
|
3445
3447
|
return [...snapshot.messages.map((message) => structuredClone(message)), ...snapshot.streamingMessage ? [structuredClone(snapshot.streamingMessage)] : []];
|
|
3446
3448
|
}
|
|
3447
|
-
function createReplayEvent(event) {
|
|
3449
|
+
function createReplayEvent(event, toolResultsByCallId) {
|
|
3448
3450
|
const replayEvent = structuredClone(event);
|
|
3449
3451
|
const replayMessage = readMessageFromSummaryEvent(replayEvent);
|
|
3450
3452
|
const legacyCompactionMessageId = readLegacyContextCompactionMessageId(replayMessage);
|
|
3451
3453
|
if (replayMessage && legacyCompactionMessageId) replayMessage.id = legacyCompactionMessageId;
|
|
3452
3454
|
if (replayMessage?.role === "assistant" && (replayMessage.status === "pending" || replayMessage.status === "streaming")) replayMessage.status = "final";
|
|
3453
|
-
if (replayEvent.type === "session.snapshot.message" || replayEvent.type === NcpEventType.MessageCompleted)
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3455
|
+
if (replayEvent.type === "session.snapshot.message" || replayEvent.type === NcpEventType.MessageCompleted) {
|
|
3456
|
+
replayEvent.payload.message = mergeReplayCompletedToolResults(replayEvent.payload.message, toolResultsByCallId);
|
|
3457
|
+
return {
|
|
3458
|
+
type: NcpEventType.MessageSent,
|
|
3459
|
+
payload: replayEvent.payload
|
|
3460
|
+
};
|
|
3461
|
+
}
|
|
3457
3462
|
return replayEvent;
|
|
3458
3463
|
}
|
|
3464
|
+
function mergeReplayCompletedToolResults(message, toolResultsByCallId) {
|
|
3465
|
+
let changed = false;
|
|
3466
|
+
const parts = message.parts.map((part) => {
|
|
3467
|
+
if (part.type !== "tool-invocation" || part.state === "result" || !part.toolCallId) return part;
|
|
3468
|
+
const result = toolResultsByCallId.get(part.toolCallId);
|
|
3469
|
+
if (!result) return part;
|
|
3470
|
+
changed = true;
|
|
3471
|
+
return {
|
|
3472
|
+
...part,
|
|
3473
|
+
state: "result",
|
|
3474
|
+
result: result.content,
|
|
3475
|
+
resultContentItems: result.contentItems
|
|
3476
|
+
};
|
|
3477
|
+
});
|
|
3478
|
+
return changed ? {
|
|
3479
|
+
...message,
|
|
3480
|
+
parts
|
|
3481
|
+
} : message;
|
|
3482
|
+
}
|
|
3459
3483
|
function readLegacyContextCompactionMessageId(message) {
|
|
3460
3484
|
const checkpoint = isRecord$11(message?.metadata?.checkpoint) ? message.metadata.checkpoint : null;
|
|
3461
3485
|
const checkpointId = typeof checkpoint?.id === "string" ? checkpoint.id : "";
|