@nextclaw/ncp-toolkit 0.4.3 → 0.4.5
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 +92 -31
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -103,9 +103,12 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
103
103
|
return this.snapshotCache;
|
|
104
104
|
}
|
|
105
105
|
const snapshot = {
|
|
106
|
-
messages: this.messages
|
|
107
|
-
streamingMessage: this.streamingMessage
|
|
108
|
-
error: this.error ? {
|
|
106
|
+
messages: this.messages,
|
|
107
|
+
streamingMessage: this.streamingMessage,
|
|
108
|
+
error: this.error ? {
|
|
109
|
+
...this.error,
|
|
110
|
+
details: this.error.details ? { ...this.error.details } : void 0
|
|
111
|
+
} : null,
|
|
109
112
|
activeRun: this.activeRun ? { ...this.activeRun } : null
|
|
110
113
|
};
|
|
111
114
|
this.snapshotCache = snapshot;
|
|
@@ -130,7 +133,9 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
130
133
|
this.notifyListeners();
|
|
131
134
|
}
|
|
132
135
|
hydrate(payload) {
|
|
133
|
-
this.messages = payload.messages.map(
|
|
136
|
+
this.messages = payload.messages.map(
|
|
137
|
+
(message) => normalizeConversationMessage(message)
|
|
138
|
+
);
|
|
134
139
|
this.streamingMessage = null;
|
|
135
140
|
this.error = null;
|
|
136
141
|
this.activeRun = payload.activeRun ? {
|
|
@@ -238,14 +243,22 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
238
243
|
}
|
|
239
244
|
}
|
|
240
245
|
handleMessageTextStart(payload) {
|
|
241
|
-
this.ensureStreamingMessage(
|
|
246
|
+
this.ensureStreamingMessage(
|
|
247
|
+
payload.sessionId,
|
|
248
|
+
payload.messageId,
|
|
249
|
+
"streaming"
|
|
250
|
+
);
|
|
242
251
|
this.setError(null);
|
|
243
252
|
}
|
|
244
253
|
handleMessageTextDelta(payload) {
|
|
245
254
|
if (!payload.delta) {
|
|
246
255
|
return;
|
|
247
256
|
}
|
|
248
|
-
const targetMessage = this.ensureStreamingMessage(
|
|
257
|
+
const targetMessage = this.ensureStreamingMessage(
|
|
258
|
+
payload.sessionId,
|
|
259
|
+
payload.messageId,
|
|
260
|
+
"streaming"
|
|
261
|
+
);
|
|
249
262
|
const nextParts = [...targetMessage.parts];
|
|
250
263
|
const lastPart = nextParts[nextParts.length - 1];
|
|
251
264
|
if (lastPart?.type === "text") {
|
|
@@ -275,13 +288,21 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
275
288
|
});
|
|
276
289
|
}
|
|
277
290
|
handleMessageReasoningStart(payload) {
|
|
278
|
-
this.ensureStreamingMessage(
|
|
291
|
+
this.ensureStreamingMessage(
|
|
292
|
+
payload.sessionId,
|
|
293
|
+
payload.messageId,
|
|
294
|
+
"streaming"
|
|
295
|
+
);
|
|
279
296
|
}
|
|
280
297
|
handleMessageReasoningDelta(payload) {
|
|
281
298
|
if (!payload.delta) {
|
|
282
299
|
return;
|
|
283
300
|
}
|
|
284
|
-
const targetMessage = this.ensureStreamingMessage(
|
|
301
|
+
const targetMessage = this.ensureStreamingMessage(
|
|
302
|
+
payload.sessionId,
|
|
303
|
+
payload.messageId,
|
|
304
|
+
"streaming"
|
|
305
|
+
);
|
|
285
306
|
const nextParts = [...targetMessage.parts];
|
|
286
307
|
const lastPart = nextParts[nextParts.length - 1];
|
|
287
308
|
if (lastPart?.type === "reasoning") {
|
|
@@ -332,10 +353,18 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
332
353
|
const currentArgs = this.toolCallArgsRawByCallId.get(payload.toolCallId) ?? "";
|
|
333
354
|
const nextArgs = `${currentArgs}${payload.delta}`;
|
|
334
355
|
this.toolCallArgsRawByCallId.set(payload.toolCallId, nextArgs);
|
|
335
|
-
this.applyToolCallArgs(
|
|
356
|
+
this.applyToolCallArgs(
|
|
357
|
+
payload.sessionId,
|
|
358
|
+
payload.toolCallId,
|
|
359
|
+
nextArgs,
|
|
360
|
+
payload.messageId
|
|
361
|
+
);
|
|
336
362
|
}
|
|
337
363
|
handleMessageToolCallEnd(payload) {
|
|
338
|
-
const targetMessage = this.resolveToolCallTargetMessage(
|
|
364
|
+
const targetMessage = this.resolveToolCallTargetMessage(
|
|
365
|
+
payload.sessionId,
|
|
366
|
+
payload.toolCallId
|
|
367
|
+
);
|
|
339
368
|
const args = this.toolCallArgsRawByCallId.get(payload.toolCallId) ?? "";
|
|
340
369
|
const nextParts = upsertToolInvocationPart(targetMessage.parts, {
|
|
341
370
|
type: "tool-invocation",
|
|
@@ -351,19 +380,25 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
351
380
|
});
|
|
352
381
|
}
|
|
353
382
|
handleMessageToolCallResult(payload) {
|
|
354
|
-
const updated = this.updateMessageContainingToolCall(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
383
|
+
const updated = this.updateMessageContainingToolCall(
|
|
384
|
+
payload.toolCallId,
|
|
385
|
+
(targetMessage, existingPart) => {
|
|
386
|
+
const mergedPart = {
|
|
387
|
+
type: "tool-invocation",
|
|
388
|
+
toolCallId: payload.toolCallId,
|
|
389
|
+
toolName: existingPart.toolName,
|
|
390
|
+
state: "result",
|
|
391
|
+
args: existingPart.args,
|
|
392
|
+
result: payload.content
|
|
393
|
+
};
|
|
394
|
+
return upsertToolInvocationPart(targetMessage.parts, mergedPart);
|
|
395
|
+
}
|
|
396
|
+
);
|
|
365
397
|
if (!updated) {
|
|
366
|
-
const fallbackMessage = this.resolveToolCallTargetMessage(
|
|
398
|
+
const fallbackMessage = this.resolveToolCallTargetMessage(
|
|
399
|
+
payload.sessionId,
|
|
400
|
+
payload.toolCallId
|
|
401
|
+
);
|
|
367
402
|
const nextParts = upsertToolInvocationPart(fallbackMessage.parts, {
|
|
368
403
|
type: "tool-invocation",
|
|
369
404
|
toolCallId: payload.toolCallId,
|
|
@@ -380,7 +415,10 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
380
415
|
}
|
|
381
416
|
handleRunStarted(payload) {
|
|
382
417
|
this.setError(null);
|
|
383
|
-
this.activeRun = {
|
|
418
|
+
this.activeRun = {
|
|
419
|
+
runId: payload.runId ?? null,
|
|
420
|
+
sessionId: payload.sessionId
|
|
421
|
+
};
|
|
384
422
|
this.stateVersion += 1;
|
|
385
423
|
}
|
|
386
424
|
handleRunFinished(_payload) {
|
|
@@ -420,7 +458,11 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
420
458
|
this.setError(payload);
|
|
421
459
|
}
|
|
422
460
|
applyToolCallArgs(sessionId, toolCallId, args, messageId) {
|
|
423
|
-
const targetMessage = this.resolveToolCallTargetMessage(
|
|
461
|
+
const targetMessage = this.resolveToolCallTargetMessage(
|
|
462
|
+
sessionId,
|
|
463
|
+
toolCallId,
|
|
464
|
+
messageId
|
|
465
|
+
);
|
|
424
466
|
const toolName = findToolNameByCallId(targetMessage.parts, toolCallId) ?? "unknown";
|
|
425
467
|
const nextParts = upsertToolInvocationPart(targetMessage.parts, {
|
|
426
468
|
type: "tool-invocation",
|
|
@@ -447,9 +489,13 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
447
489
|
this.replaceStreamingMessage(nextStreamingMessage2);
|
|
448
490
|
return nextStreamingMessage2;
|
|
449
491
|
}
|
|
450
|
-
const messageIndex = this.messages.findIndex(
|
|
492
|
+
const messageIndex = this.messages.findIndex(
|
|
493
|
+
(message) => message.id === messageId
|
|
494
|
+
);
|
|
451
495
|
if (messageIndex >= 0) {
|
|
452
|
-
const existingMessage = cloneConversationMessage(
|
|
496
|
+
const existingMessage = cloneConversationMessage(
|
|
497
|
+
this.messages[messageIndex]
|
|
498
|
+
);
|
|
453
499
|
const nextMessages = [...this.messages];
|
|
454
500
|
nextMessages.splice(messageIndex, 1);
|
|
455
501
|
this.messages = nextMessages;
|
|
@@ -492,14 +538,24 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
492
538
|
resolveToolCallTargetMessage(sessionId, toolCallId, messageId) {
|
|
493
539
|
const preferredMessageId = messageId?.trim() || this.toolCallMessageIdByCallId.get(toolCallId) || this.streamingMessage?.id || `tool-${toolCallId}`;
|
|
494
540
|
this.toolCallMessageIdByCallId.set(toolCallId, preferredMessageId);
|
|
495
|
-
return this.ensureStreamingMessage(
|
|
541
|
+
return this.ensureStreamingMessage(
|
|
542
|
+
sessionId,
|
|
543
|
+
preferredMessageId,
|
|
544
|
+
"streaming"
|
|
545
|
+
);
|
|
496
546
|
}
|
|
497
547
|
updateMessageContainingToolCall(toolCallId, updater) {
|
|
498
548
|
if (this.streamingMessage) {
|
|
499
|
-
const part = findToolInvocationPart(
|
|
549
|
+
const part = findToolInvocationPart(
|
|
550
|
+
this.streamingMessage.parts,
|
|
551
|
+
toolCallId
|
|
552
|
+
);
|
|
500
553
|
if (part) {
|
|
501
554
|
const nextParts = updater(this.streamingMessage, part);
|
|
502
|
-
this.replaceStreamingMessage({
|
|
555
|
+
this.replaceStreamingMessage({
|
|
556
|
+
...this.streamingMessage,
|
|
557
|
+
parts: nextParts
|
|
558
|
+
});
|
|
503
559
|
return true;
|
|
504
560
|
}
|
|
505
561
|
}
|
|
@@ -522,7 +578,9 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
522
578
|
}
|
|
523
579
|
upsertMessage(message) {
|
|
524
580
|
const normalizedMessage = normalizeConversationMessage(message);
|
|
525
|
-
const messageIndex = this.messages.findIndex(
|
|
581
|
+
const messageIndex = this.messages.findIndex(
|
|
582
|
+
(item) => item.id === normalizedMessage.id
|
|
583
|
+
);
|
|
526
584
|
if (messageIndex < 0) {
|
|
527
585
|
this.messages = [...this.messages, normalizedMessage];
|
|
528
586
|
this.stateVersion += 1;
|
|
@@ -545,7 +603,10 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
545
603
|
if (hasSameError) {
|
|
546
604
|
return;
|
|
547
605
|
}
|
|
548
|
-
this.error = nextError ? {
|
|
606
|
+
this.error = nextError ? {
|
|
607
|
+
...nextError,
|
|
608
|
+
details: nextError.details ? { ...nextError.details } : void 0
|
|
609
|
+
} : null;
|
|
549
610
|
this.stateVersion += 1;
|
|
550
611
|
}
|
|
551
612
|
clearActiveRun() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextclaw/ncp-toolkit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Toolkit implementations built on top of the NextClaw Communication Protocol.",
|
|
6
6
|
"type": "module",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@nextclaw/ncp": "0.
|
|
18
|
+
"@nextclaw/ncp": "0.4.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/node": "^20.17.6",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"tsup": "^8.3.5",
|
|
24
24
|
"typescript": "^5.6.3",
|
|
25
25
|
"vitest": "^2.1.2",
|
|
26
|
-
"@nextclaw/ncp-agent-runtime": "0.
|
|
26
|
+
"@nextclaw/ncp-agent-runtime": "0.3.0"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "tsup src/index.ts --format esm --dts --out-dir dist",
|