@nextclaw/ncp-toolkit 0.1.0 → 0.1.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.d.ts +7 -6
- package/dist/index.js +46 -20
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { NcpAgentConversationStateManager,
|
|
1
|
+
import { NcpAgentConversationStateManager, NcpAgentConversationSnapshot, NcpEndpointEvent, NcpRequestEnvelope, NcpMessageSentPayload, NcpMessageAcceptedPayload, NcpResponseEnvelope, NcpCompletedEnvelope, NcpFailedEnvelope, NcpMessageAbortPayload, NcpTextStartPayload, NcpTextDeltaPayload, NcpTextEndPayload, NcpReasoningStartPayload, NcpReasoningDeltaPayload, NcpReasoningEndPayload, NcpToolCallStartPayload, NcpToolCallArgsPayload, NcpToolCallArgsDeltaPayload, NcpToolCallEndPayload, NcpToolCallResultPayload, NcpRunStartedPayload, NcpRunFinishedPayload, NcpRunErrorPayload, NcpRunMetadataPayload, NcpError } from '@nextclaw/ncp';
|
|
2
2
|
|
|
3
3
|
declare class DefaultNcpAgentConversationStateManager implements NcpAgentConversationStateManager {
|
|
4
4
|
private messages;
|
|
5
5
|
private streamingMessage;
|
|
6
6
|
private error;
|
|
7
|
+
private activeRun;
|
|
7
8
|
private readonly listeners;
|
|
8
9
|
private readonly toolCallMessageIdByCallId;
|
|
9
10
|
private readonly toolCallArgsRawByCallId;
|
|
10
11
|
private stateVersion;
|
|
11
|
-
getSnapshot():
|
|
12
|
-
subscribe(listener: (snapshot:
|
|
13
|
-
dispatch(event: NcpEndpointEvent): void
|
|
12
|
+
getSnapshot(): NcpAgentConversationSnapshot;
|
|
13
|
+
subscribe(listener: (snapshot: NcpAgentConversationSnapshot) => void): () => boolean;
|
|
14
|
+
dispatch(event: NcpEndpointEvent): Promise<void>;
|
|
14
15
|
handleMessageRequest(payload: NcpRequestEnvelope): void;
|
|
15
16
|
handleMessageSent(payload: NcpMessageSentPayload): void;
|
|
16
17
|
handleMessageAccepted(_payload: NcpMessageAcceptedPayload): void;
|
|
@@ -29,10 +30,10 @@ declare class DefaultNcpAgentConversationStateManager implements NcpAgentConvers
|
|
|
29
30
|
handleMessageToolCallArgsDelta(payload: NcpToolCallArgsDeltaPayload): void;
|
|
30
31
|
handleMessageToolCallEnd(payload: NcpToolCallEndPayload): void;
|
|
31
32
|
handleMessageToolCallResult(payload: NcpToolCallResultPayload): void;
|
|
32
|
-
handleRunStarted(
|
|
33
|
+
handleRunStarted(payload: NcpRunStartedPayload): void;
|
|
33
34
|
handleRunFinished(_payload: NcpRunFinishedPayload): void;
|
|
34
35
|
handleRunError(payload: NcpRunErrorPayload): void;
|
|
35
|
-
handleRunMetadata(
|
|
36
|
+
handleRunMetadata(payload: NcpRunMetadataPayload): void;
|
|
36
37
|
handleEndpointError(payload: NcpError): void;
|
|
37
38
|
private applyToolCallArgs;
|
|
38
39
|
private ensureStreamingMessage;
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,7 @@ var buildRuntimeError = (payload) => {
|
|
|
13
13
|
code: "runtime-error",
|
|
14
14
|
message: message && message.length > 0 ? message : "Agent run failed.",
|
|
15
15
|
details: {
|
|
16
|
-
|
|
16
|
+
sessionId: payload.sessionId,
|
|
17
17
|
messageId: payload.messageId,
|
|
18
18
|
threadId: payload.threadId,
|
|
19
19
|
runId: payload.runId
|
|
@@ -24,6 +24,7 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
24
24
|
messages = [];
|
|
25
25
|
streamingMessage = null;
|
|
26
26
|
error = null;
|
|
27
|
+
activeRun = null;
|
|
27
28
|
listeners = /* @__PURE__ */ new Set();
|
|
28
29
|
toolCallMessageIdByCallId = /* @__PURE__ */ new Map();
|
|
29
30
|
toolCallArgsRawByCallId = /* @__PURE__ */ new Map();
|
|
@@ -32,19 +33,22 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
32
33
|
return {
|
|
33
34
|
messages: this.messages.map((message) => cloneMessage(message)),
|
|
34
35
|
streamingMessage: this.streamingMessage ? cloneMessage(this.streamingMessage) : null,
|
|
35
|
-
error: this.error ? { ...this.error, details: this.error.details ? { ...this.error.details } : void 0 } : null
|
|
36
|
+
error: this.error ? { ...this.error, details: this.error.details ? { ...this.error.details } : void 0 } : null,
|
|
37
|
+
activeRun: this.activeRun ? { ...this.activeRun } : null
|
|
36
38
|
};
|
|
37
39
|
}
|
|
38
40
|
subscribe(listener) {
|
|
39
41
|
this.listeners.add(listener);
|
|
40
42
|
return () => this.listeners.delete(listener);
|
|
41
43
|
}
|
|
42
|
-
dispatch(event) {
|
|
44
|
+
async dispatch(event) {
|
|
43
45
|
const versionBeforeDispatch = this.stateVersion;
|
|
44
46
|
switch (event.type) {
|
|
45
47
|
case "message.request":
|
|
46
48
|
this.handleMessageRequest(event.payload);
|
|
47
49
|
break;
|
|
50
|
+
case "message.resume-request":
|
|
51
|
+
break;
|
|
48
52
|
case "message.sent":
|
|
49
53
|
this.handleMessageSent(event.payload);
|
|
50
54
|
break;
|
|
@@ -215,14 +219,14 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
215
219
|
}
|
|
216
220
|
}
|
|
217
221
|
handleMessageTextStart(payload) {
|
|
218
|
-
this.ensureStreamingMessage(payload.
|
|
222
|
+
this.ensureStreamingMessage(payload.sessionId, payload.messageId, "streaming");
|
|
219
223
|
this.setError(null);
|
|
220
224
|
}
|
|
221
225
|
handleMessageTextDelta(payload) {
|
|
222
226
|
if (!payload.delta) {
|
|
223
227
|
return;
|
|
224
228
|
}
|
|
225
|
-
const targetMessage = this.ensureStreamingMessage(payload.
|
|
229
|
+
const targetMessage = this.ensureStreamingMessage(payload.sessionId, payload.messageId, "streaming");
|
|
226
230
|
const nextParts = [...targetMessage.parts];
|
|
227
231
|
const lastPart = nextParts[nextParts.length - 1];
|
|
228
232
|
if (lastPart?.type === "text") {
|
|
@@ -252,13 +256,13 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
252
256
|
});
|
|
253
257
|
}
|
|
254
258
|
handleMessageReasoningStart(payload) {
|
|
255
|
-
this.ensureStreamingMessage(payload.
|
|
259
|
+
this.ensureStreamingMessage(payload.sessionId, payload.messageId, "streaming");
|
|
256
260
|
}
|
|
257
261
|
handleMessageReasoningDelta(payload) {
|
|
258
262
|
if (!payload.delta) {
|
|
259
263
|
return;
|
|
260
264
|
}
|
|
261
|
-
const targetMessage = this.ensureStreamingMessage(payload.
|
|
265
|
+
const targetMessage = this.ensureStreamingMessage(payload.sessionId, payload.messageId, "streaming");
|
|
262
266
|
const nextParts = [...targetMessage.parts];
|
|
263
267
|
const reasoningPartIndex = this.findLastReasoningPartIndex(nextParts);
|
|
264
268
|
if (reasoningPartIndex >= 0) {
|
|
@@ -285,7 +289,7 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
285
289
|
}
|
|
286
290
|
handleMessageToolCallStart(payload) {
|
|
287
291
|
const targetMessage = this.resolveToolCallTargetMessage(
|
|
288
|
-
payload.
|
|
292
|
+
payload.sessionId,
|
|
289
293
|
payload.toolCallId,
|
|
290
294
|
payload.messageId
|
|
291
295
|
);
|
|
@@ -306,16 +310,16 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
306
310
|
}
|
|
307
311
|
handleMessageToolCallArgs(payload) {
|
|
308
312
|
this.toolCallArgsRawByCallId.set(payload.toolCallId, payload.args);
|
|
309
|
-
this.applyToolCallArgs(payload.
|
|
313
|
+
this.applyToolCallArgs(payload.sessionId, payload.toolCallId, payload.args);
|
|
310
314
|
}
|
|
311
315
|
handleMessageToolCallArgsDelta(payload) {
|
|
312
316
|
const currentArgs = this.toolCallArgsRawByCallId.get(payload.toolCallId) ?? "";
|
|
313
317
|
const nextArgs = `${currentArgs}${payload.delta}`;
|
|
314
318
|
this.toolCallArgsRawByCallId.set(payload.toolCallId, nextArgs);
|
|
315
|
-
this.applyToolCallArgs(payload.
|
|
319
|
+
this.applyToolCallArgs(payload.sessionId, payload.toolCallId, nextArgs, payload.messageId);
|
|
316
320
|
}
|
|
317
321
|
handleMessageToolCallEnd(payload) {
|
|
318
|
-
const targetMessage = this.resolveToolCallTargetMessage(payload.
|
|
322
|
+
const targetMessage = this.resolveToolCallTargetMessage(payload.sessionId, payload.toolCallId);
|
|
319
323
|
const args = this.toolCallArgsRawByCallId.get(payload.toolCallId) ?? "";
|
|
320
324
|
const nextParts = this.upsertToolInvocationPart(targetMessage.parts, {
|
|
321
325
|
type: "tool-invocation",
|
|
@@ -343,7 +347,7 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
343
347
|
return this.upsertToolInvocationPart(targetMessage.parts, mergedPart);
|
|
344
348
|
});
|
|
345
349
|
if (!updated) {
|
|
346
|
-
const fallbackMessage = this.resolveToolCallTargetMessage(payload.
|
|
350
|
+
const fallbackMessage = this.resolveToolCallTargetMessage(payload.sessionId, payload.toolCallId);
|
|
347
351
|
const nextParts = this.upsertToolInvocationPart(fallbackMessage.parts, {
|
|
348
352
|
type: "tool-invocation",
|
|
349
353
|
toolCallId: payload.toolCallId,
|
|
@@ -358,21 +362,43 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
358
362
|
});
|
|
359
363
|
}
|
|
360
364
|
}
|
|
361
|
-
handleRunStarted(
|
|
365
|
+
handleRunStarted(payload) {
|
|
362
366
|
this.setError(null);
|
|
367
|
+
this.activeRun = {
|
|
368
|
+
runId: payload.runId ?? null,
|
|
369
|
+
sessionId: payload.sessionId
|
|
370
|
+
};
|
|
371
|
+
this.stateVersion += 1;
|
|
363
372
|
}
|
|
364
373
|
handleRunFinished(_payload) {
|
|
374
|
+
this.activeRun = null;
|
|
375
|
+
this.stateVersion += 1;
|
|
365
376
|
}
|
|
366
377
|
handleRunError(payload) {
|
|
367
378
|
this.setError(buildRuntimeError(payload));
|
|
379
|
+
this.activeRun = null;
|
|
380
|
+
this.stateVersion += 1;
|
|
368
381
|
}
|
|
369
|
-
handleRunMetadata(
|
|
382
|
+
handleRunMetadata(payload) {
|
|
383
|
+
const m = payload.metadata;
|
|
384
|
+
if (m?.kind === "ready") {
|
|
385
|
+
const ready = m;
|
|
386
|
+
this.activeRun = {
|
|
387
|
+
runId: ready.runId ?? this.activeRun?.runId ?? null,
|
|
388
|
+
sessionId: ready.sessionId ?? this.activeRun?.sessionId,
|
|
389
|
+
abortDisabledReason: ready.supportsAbort === false ? ready.abortDisabledReason ?? "Unsupported" : null
|
|
390
|
+
};
|
|
391
|
+
this.stateVersion += 1;
|
|
392
|
+
} else if (m?.kind === "final") {
|
|
393
|
+
this.activeRun = null;
|
|
394
|
+
this.stateVersion += 1;
|
|
395
|
+
}
|
|
370
396
|
}
|
|
371
397
|
handleEndpointError(payload) {
|
|
372
398
|
this.setError(payload);
|
|
373
399
|
}
|
|
374
|
-
applyToolCallArgs(
|
|
375
|
-
const targetMessage = this.resolveToolCallTargetMessage(
|
|
400
|
+
applyToolCallArgs(sessionId, toolCallId, args, messageId) {
|
|
401
|
+
const targetMessage = this.resolveToolCallTargetMessage(sessionId, toolCallId, messageId);
|
|
376
402
|
const toolName = this.findToolNameByCallId(targetMessage.parts, toolCallId) ?? "unknown";
|
|
377
403
|
const nextParts = this.upsertToolInvocationPart(targetMessage.parts, {
|
|
378
404
|
type: "tool-invocation",
|
|
@@ -387,7 +413,7 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
387
413
|
status: "streaming"
|
|
388
414
|
});
|
|
389
415
|
}
|
|
390
|
-
ensureStreamingMessage(
|
|
416
|
+
ensureStreamingMessage(sessionId, messageId, status) {
|
|
391
417
|
if (this.streamingMessage?.id === messageId) {
|
|
392
418
|
if (this.streamingMessage.status === status) {
|
|
393
419
|
return this.streamingMessage;
|
|
@@ -401,7 +427,7 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
401
427
|
}
|
|
402
428
|
const nextStreamingMessage = {
|
|
403
429
|
id: messageId,
|
|
404
|
-
|
|
430
|
+
sessionId,
|
|
405
431
|
role: DEFAULT_ASSISTANT_ROLE,
|
|
406
432
|
status,
|
|
407
433
|
parts: [],
|
|
@@ -410,10 +436,10 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
410
436
|
this.replaceStreamingMessage(nextStreamingMessage);
|
|
411
437
|
return nextStreamingMessage;
|
|
412
438
|
}
|
|
413
|
-
resolveToolCallTargetMessage(
|
|
439
|
+
resolveToolCallTargetMessage(sessionId, toolCallId, messageId) {
|
|
414
440
|
const preferredMessageId = messageId?.trim() || this.toolCallMessageIdByCallId.get(toolCallId) || this.streamingMessage?.id || `tool-${toolCallId}`;
|
|
415
441
|
this.toolCallMessageIdByCallId.set(toolCallId, preferredMessageId);
|
|
416
|
-
return this.ensureStreamingMessage(
|
|
442
|
+
return this.ensureStreamingMessage(sessionId, preferredMessageId, "streaming");
|
|
417
443
|
}
|
|
418
444
|
updateMessageContainingToolCall(toolCallId, updater) {
|
|
419
445
|
if (this.streamingMessage) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextclaw/ncp-toolkit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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.1.
|
|
18
|
+
"@nextclaw/ncp": "0.1.1"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/node": "^20.17.6",
|