@nextclaw/ncp-toolkit 0.1.0
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/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +520 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NextClaw contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @nextclaw/ncp-toolkit
|
|
2
|
+
|
|
3
|
+
Toolkit implementations built on top of `@nextclaw/ncp` protocol contracts.
|
|
4
|
+
|
|
5
|
+
## Build
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm -C packages/nextclaw-ncp-toolkit build
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Scope
|
|
12
|
+
|
|
13
|
+
- Reference conversation-state manager implementations
|
|
14
|
+
- Protocol-level helper logic that depends on `@nextclaw/ncp` contracts
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { NcpAgentConversationStateManager, NcpConversationSnapshot, 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
|
+
|
|
3
|
+
declare class DefaultNcpAgentConversationStateManager implements NcpAgentConversationStateManager {
|
|
4
|
+
private messages;
|
|
5
|
+
private streamingMessage;
|
|
6
|
+
private error;
|
|
7
|
+
private readonly listeners;
|
|
8
|
+
private readonly toolCallMessageIdByCallId;
|
|
9
|
+
private readonly toolCallArgsRawByCallId;
|
|
10
|
+
private stateVersion;
|
|
11
|
+
getSnapshot(): NcpConversationSnapshot;
|
|
12
|
+
subscribe(listener: (snapshot: NcpConversationSnapshot) => void): () => boolean;
|
|
13
|
+
dispatch(event: NcpEndpointEvent): void;
|
|
14
|
+
handleMessageRequest(payload: NcpRequestEnvelope): void;
|
|
15
|
+
handleMessageSent(payload: NcpMessageSentPayload): void;
|
|
16
|
+
handleMessageAccepted(_payload: NcpMessageAcceptedPayload): void;
|
|
17
|
+
handleMessageIncoming(payload: NcpResponseEnvelope): void;
|
|
18
|
+
handleMessageCompleted(payload: NcpCompletedEnvelope): void;
|
|
19
|
+
handleMessageFailed(payload: NcpFailedEnvelope): void;
|
|
20
|
+
handleMessageAbort(payload: NcpMessageAbortPayload): void;
|
|
21
|
+
handleMessageTextStart(payload: NcpTextStartPayload): void;
|
|
22
|
+
handleMessageTextDelta(payload: NcpTextDeltaPayload): void;
|
|
23
|
+
handleMessageTextEnd(payload: NcpTextEndPayload): void;
|
|
24
|
+
handleMessageReasoningStart(payload: NcpReasoningStartPayload): void;
|
|
25
|
+
handleMessageReasoningDelta(payload: NcpReasoningDeltaPayload): void;
|
|
26
|
+
handleMessageReasoningEnd(payload: NcpReasoningEndPayload): void;
|
|
27
|
+
handleMessageToolCallStart(payload: NcpToolCallStartPayload): void;
|
|
28
|
+
handleMessageToolCallArgs(payload: NcpToolCallArgsPayload): void;
|
|
29
|
+
handleMessageToolCallArgsDelta(payload: NcpToolCallArgsDeltaPayload): void;
|
|
30
|
+
handleMessageToolCallEnd(payload: NcpToolCallEndPayload): void;
|
|
31
|
+
handleMessageToolCallResult(payload: NcpToolCallResultPayload): void;
|
|
32
|
+
handleRunStarted(_payload: NcpRunStartedPayload): void;
|
|
33
|
+
handleRunFinished(_payload: NcpRunFinishedPayload): void;
|
|
34
|
+
handleRunError(payload: NcpRunErrorPayload): void;
|
|
35
|
+
handleRunMetadata(_payload: NcpRunMetadataPayload): void;
|
|
36
|
+
handleEndpointError(payload: NcpError): void;
|
|
37
|
+
private applyToolCallArgs;
|
|
38
|
+
private ensureStreamingMessage;
|
|
39
|
+
private resolveToolCallTargetMessage;
|
|
40
|
+
private updateMessageContainingToolCall;
|
|
41
|
+
private findToolInvocationPart;
|
|
42
|
+
private findToolNameByCallId;
|
|
43
|
+
private upsertToolInvocationPart;
|
|
44
|
+
private findLastReasoningPartIndex;
|
|
45
|
+
private upsertMessage;
|
|
46
|
+
private replaceStreamingMessage;
|
|
47
|
+
private setError;
|
|
48
|
+
private clearToolCallTrackingByMessageId;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { DefaultNcpAgentConversationStateManager };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
// src/agent/agent-conversation-state-manager.ts
|
|
2
|
+
var DEFAULT_ASSISTANT_ROLE = "assistant";
|
|
3
|
+
var cloneMessage = (message) => {
|
|
4
|
+
return {
|
|
5
|
+
...message,
|
|
6
|
+
parts: [...message.parts],
|
|
7
|
+
metadata: message.metadata ? { ...message.metadata } : void 0
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
var buildRuntimeError = (payload) => {
|
|
11
|
+
const message = payload.error?.trim();
|
|
12
|
+
return {
|
|
13
|
+
code: "runtime-error",
|
|
14
|
+
message: message && message.length > 0 ? message : "Agent run failed.",
|
|
15
|
+
details: {
|
|
16
|
+
sessionKey: payload.sessionKey,
|
|
17
|
+
messageId: payload.messageId,
|
|
18
|
+
threadId: payload.threadId,
|
|
19
|
+
runId: payload.runId
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
var DefaultNcpAgentConversationStateManager = class {
|
|
24
|
+
messages = [];
|
|
25
|
+
streamingMessage = null;
|
|
26
|
+
error = null;
|
|
27
|
+
listeners = /* @__PURE__ */ new Set();
|
|
28
|
+
toolCallMessageIdByCallId = /* @__PURE__ */ new Map();
|
|
29
|
+
toolCallArgsRawByCallId = /* @__PURE__ */ new Map();
|
|
30
|
+
stateVersion = 0;
|
|
31
|
+
getSnapshot() {
|
|
32
|
+
return {
|
|
33
|
+
messages: this.messages.map((message) => cloneMessage(message)),
|
|
34
|
+
streamingMessage: this.streamingMessage ? cloneMessage(this.streamingMessage) : null,
|
|
35
|
+
error: this.error ? { ...this.error, details: this.error.details ? { ...this.error.details } : void 0 } : null
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
subscribe(listener) {
|
|
39
|
+
this.listeners.add(listener);
|
|
40
|
+
return () => this.listeners.delete(listener);
|
|
41
|
+
}
|
|
42
|
+
dispatch(event) {
|
|
43
|
+
const versionBeforeDispatch = this.stateVersion;
|
|
44
|
+
switch (event.type) {
|
|
45
|
+
case "message.request":
|
|
46
|
+
this.handleMessageRequest(event.payload);
|
|
47
|
+
break;
|
|
48
|
+
case "message.sent":
|
|
49
|
+
this.handleMessageSent(event.payload);
|
|
50
|
+
break;
|
|
51
|
+
case "message.accepted":
|
|
52
|
+
this.handleMessageAccepted(event.payload);
|
|
53
|
+
break;
|
|
54
|
+
case "message.incoming":
|
|
55
|
+
this.handleMessageIncoming(event.payload);
|
|
56
|
+
break;
|
|
57
|
+
case "message.completed":
|
|
58
|
+
this.handleMessageCompleted(event.payload);
|
|
59
|
+
break;
|
|
60
|
+
case "message.failed":
|
|
61
|
+
this.handleMessageFailed(event.payload);
|
|
62
|
+
break;
|
|
63
|
+
case "message.abort":
|
|
64
|
+
this.handleMessageAbort(event.payload);
|
|
65
|
+
break;
|
|
66
|
+
case "message.text-start":
|
|
67
|
+
this.handleMessageTextStart(event.payload);
|
|
68
|
+
break;
|
|
69
|
+
case "message.text-delta":
|
|
70
|
+
this.handleMessageTextDelta(event.payload);
|
|
71
|
+
break;
|
|
72
|
+
case "message.text-end":
|
|
73
|
+
this.handleMessageTextEnd(event.payload);
|
|
74
|
+
break;
|
|
75
|
+
case "message.reasoning-start":
|
|
76
|
+
this.handleMessageReasoningStart(event.payload);
|
|
77
|
+
break;
|
|
78
|
+
case "message.reasoning-delta":
|
|
79
|
+
this.handleMessageReasoningDelta(event.payload);
|
|
80
|
+
break;
|
|
81
|
+
case "message.reasoning-end":
|
|
82
|
+
this.handleMessageReasoningEnd(event.payload);
|
|
83
|
+
break;
|
|
84
|
+
case "message.tool-call-start":
|
|
85
|
+
this.handleMessageToolCallStart(event.payload);
|
|
86
|
+
break;
|
|
87
|
+
case "message.tool-call-args":
|
|
88
|
+
this.handleMessageToolCallArgs(event.payload);
|
|
89
|
+
break;
|
|
90
|
+
case "message.tool-call-args-delta":
|
|
91
|
+
this.handleMessageToolCallArgsDelta(event.payload);
|
|
92
|
+
break;
|
|
93
|
+
case "message.tool-call-end":
|
|
94
|
+
this.handleMessageToolCallEnd(event.payload);
|
|
95
|
+
break;
|
|
96
|
+
case "message.tool-call-result":
|
|
97
|
+
this.handleMessageToolCallResult(event.payload);
|
|
98
|
+
break;
|
|
99
|
+
case "run.started":
|
|
100
|
+
this.handleRunStarted(event.payload);
|
|
101
|
+
break;
|
|
102
|
+
case "run.finished":
|
|
103
|
+
this.handleRunFinished(event.payload);
|
|
104
|
+
break;
|
|
105
|
+
case "run.error":
|
|
106
|
+
this.handleRunError(event.payload);
|
|
107
|
+
break;
|
|
108
|
+
case "run.metadata":
|
|
109
|
+
this.handleRunMetadata(event.payload);
|
|
110
|
+
break;
|
|
111
|
+
case "endpoint.error":
|
|
112
|
+
this.handleEndpointError(event.payload);
|
|
113
|
+
break;
|
|
114
|
+
case "endpoint.ready":
|
|
115
|
+
case "typing.start":
|
|
116
|
+
case "typing.end":
|
|
117
|
+
case "presence.updated":
|
|
118
|
+
case "message.read":
|
|
119
|
+
case "message.delivered":
|
|
120
|
+
case "message.recalled":
|
|
121
|
+
case "message.reaction":
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (this.stateVersion !== versionBeforeDispatch) {
|
|
127
|
+
const snapshot = this.getSnapshot();
|
|
128
|
+
for (const listener of this.listeners) {
|
|
129
|
+
listener(snapshot);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
handleMessageRequest(payload) {
|
|
134
|
+
this.upsertMessage(payload.message);
|
|
135
|
+
this.setError(null);
|
|
136
|
+
}
|
|
137
|
+
handleMessageSent(payload) {
|
|
138
|
+
this.upsertMessage(payload.message);
|
|
139
|
+
this.setError(null);
|
|
140
|
+
}
|
|
141
|
+
handleMessageAccepted(_payload) {
|
|
142
|
+
}
|
|
143
|
+
handleMessageIncoming(payload) {
|
|
144
|
+
const incomingMessage = cloneMessage(payload.message);
|
|
145
|
+
if (incomingMessage.status === "streaming" || incomingMessage.status === "pending") {
|
|
146
|
+
this.replaceStreamingMessage(incomingMessage);
|
|
147
|
+
this.setError(null);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
this.upsertMessage(incomingMessage);
|
|
151
|
+
if (this.streamingMessage?.id === incomingMessage.id) {
|
|
152
|
+
this.replaceStreamingMessage(null);
|
|
153
|
+
this.clearToolCallTrackingByMessageId(incomingMessage.id);
|
|
154
|
+
}
|
|
155
|
+
this.setError(null);
|
|
156
|
+
}
|
|
157
|
+
handleMessageCompleted(payload) {
|
|
158
|
+
const completedMessage = {
|
|
159
|
+
...cloneMessage(payload.message),
|
|
160
|
+
status: "final"
|
|
161
|
+
};
|
|
162
|
+
this.upsertMessage(completedMessage);
|
|
163
|
+
if (this.streamingMessage?.id === completedMessage.id) {
|
|
164
|
+
this.replaceStreamingMessage(null);
|
|
165
|
+
}
|
|
166
|
+
this.clearToolCallTrackingByMessageId(completedMessage.id);
|
|
167
|
+
this.setError(null);
|
|
168
|
+
}
|
|
169
|
+
handleMessageFailed(payload) {
|
|
170
|
+
this.setError(payload.error);
|
|
171
|
+
const targetMessageId = payload.messageId?.trim();
|
|
172
|
+
if (!targetMessageId) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (this.streamingMessage?.id === targetMessageId) {
|
|
176
|
+
this.upsertMessage({
|
|
177
|
+
...this.streamingMessage,
|
|
178
|
+
status: "error"
|
|
179
|
+
});
|
|
180
|
+
this.replaceStreamingMessage(null);
|
|
181
|
+
this.clearToolCallTrackingByMessageId(targetMessageId);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const messageIndex = this.messages.findIndex((message) => message.id === targetMessageId);
|
|
185
|
+
if (messageIndex < 0) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const nextMessages = [...this.messages];
|
|
189
|
+
nextMessages[messageIndex] = {
|
|
190
|
+
...nextMessages[messageIndex],
|
|
191
|
+
status: "error"
|
|
192
|
+
};
|
|
193
|
+
this.messages = nextMessages;
|
|
194
|
+
this.stateVersion += 1;
|
|
195
|
+
}
|
|
196
|
+
handleMessageAbort(payload) {
|
|
197
|
+
const targetMessageId = payload.messageId?.trim();
|
|
198
|
+
this.setError({
|
|
199
|
+
code: "abort-error",
|
|
200
|
+
message: "Message aborted.",
|
|
201
|
+
details: {
|
|
202
|
+
messageId: targetMessageId,
|
|
203
|
+
correlationId: payload.correlationId
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
if (this.streamingMessage && (!targetMessageId || this.streamingMessage.id === targetMessageId)) {
|
|
207
|
+
this.upsertMessage({
|
|
208
|
+
...this.streamingMessage,
|
|
209
|
+
status: "error"
|
|
210
|
+
});
|
|
211
|
+
this.replaceStreamingMessage(null);
|
|
212
|
+
if (targetMessageId) {
|
|
213
|
+
this.clearToolCallTrackingByMessageId(targetMessageId);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
handleMessageTextStart(payload) {
|
|
218
|
+
this.ensureStreamingMessage(payload.sessionKey, payload.messageId, "streaming");
|
|
219
|
+
this.setError(null);
|
|
220
|
+
}
|
|
221
|
+
handleMessageTextDelta(payload) {
|
|
222
|
+
if (!payload.delta) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const targetMessage = this.ensureStreamingMessage(payload.sessionKey, payload.messageId, "streaming");
|
|
226
|
+
const nextParts = [...targetMessage.parts];
|
|
227
|
+
const lastPart = nextParts[nextParts.length - 1];
|
|
228
|
+
if (lastPart?.type === "text") {
|
|
229
|
+
nextParts[nextParts.length - 1] = {
|
|
230
|
+
type: "text",
|
|
231
|
+
text: `${lastPart.text}${payload.delta}`
|
|
232
|
+
};
|
|
233
|
+
} else {
|
|
234
|
+
nextParts.push({ type: "text", text: payload.delta });
|
|
235
|
+
}
|
|
236
|
+
this.replaceStreamingMessage({
|
|
237
|
+
...targetMessage,
|
|
238
|
+
parts: nextParts,
|
|
239
|
+
status: "streaming"
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
handleMessageTextEnd(payload) {
|
|
243
|
+
if (this.streamingMessage?.id !== payload.messageId) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (this.streamingMessage.status !== "streaming") {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
this.replaceStreamingMessage({
|
|
250
|
+
...this.streamingMessage,
|
|
251
|
+
status: "pending"
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
handleMessageReasoningStart(payload) {
|
|
255
|
+
this.ensureStreamingMessage(payload.sessionKey, payload.messageId, "streaming");
|
|
256
|
+
}
|
|
257
|
+
handleMessageReasoningDelta(payload) {
|
|
258
|
+
if (!payload.delta) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const targetMessage = this.ensureStreamingMessage(payload.sessionKey, payload.messageId, "streaming");
|
|
262
|
+
const nextParts = [...targetMessage.parts];
|
|
263
|
+
const reasoningPartIndex = this.findLastReasoningPartIndex(nextParts);
|
|
264
|
+
if (reasoningPartIndex >= 0) {
|
|
265
|
+
const existingPart = nextParts[reasoningPartIndex];
|
|
266
|
+
if (existingPart?.type === "reasoning") {
|
|
267
|
+
nextParts[reasoningPartIndex] = {
|
|
268
|
+
type: "reasoning",
|
|
269
|
+
text: `${existingPart.text}${payload.delta}`
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
nextParts.push({ type: "reasoning", text: payload.delta });
|
|
274
|
+
}
|
|
275
|
+
this.replaceStreamingMessage({
|
|
276
|
+
...targetMessage,
|
|
277
|
+
parts: nextParts,
|
|
278
|
+
status: "streaming"
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
handleMessageReasoningEnd(payload) {
|
|
282
|
+
if (this.streamingMessage?.id !== payload.messageId) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
handleMessageToolCallStart(payload) {
|
|
287
|
+
const targetMessage = this.resolveToolCallTargetMessage(
|
|
288
|
+
payload.sessionKey,
|
|
289
|
+
payload.toolCallId,
|
|
290
|
+
payload.messageId
|
|
291
|
+
);
|
|
292
|
+
this.toolCallArgsRawByCallId.set(payload.toolCallId, "");
|
|
293
|
+
const nextParts = this.upsertToolInvocationPart(targetMessage.parts, {
|
|
294
|
+
type: "tool-invocation",
|
|
295
|
+
toolCallId: payload.toolCallId,
|
|
296
|
+
toolName: payload.toolName,
|
|
297
|
+
state: "partial-call",
|
|
298
|
+
args: ""
|
|
299
|
+
});
|
|
300
|
+
this.replaceStreamingMessage({
|
|
301
|
+
...targetMessage,
|
|
302
|
+
parts: nextParts,
|
|
303
|
+
status: "streaming"
|
|
304
|
+
});
|
|
305
|
+
this.setError(null);
|
|
306
|
+
}
|
|
307
|
+
handleMessageToolCallArgs(payload) {
|
|
308
|
+
this.toolCallArgsRawByCallId.set(payload.toolCallId, payload.args);
|
|
309
|
+
this.applyToolCallArgs(payload.sessionKey, payload.toolCallId, payload.args);
|
|
310
|
+
}
|
|
311
|
+
handleMessageToolCallArgsDelta(payload) {
|
|
312
|
+
const currentArgs = this.toolCallArgsRawByCallId.get(payload.toolCallId) ?? "";
|
|
313
|
+
const nextArgs = `${currentArgs}${payload.delta}`;
|
|
314
|
+
this.toolCallArgsRawByCallId.set(payload.toolCallId, nextArgs);
|
|
315
|
+
this.applyToolCallArgs(payload.sessionKey, payload.toolCallId, nextArgs, payload.messageId);
|
|
316
|
+
}
|
|
317
|
+
handleMessageToolCallEnd(payload) {
|
|
318
|
+
const targetMessage = this.resolveToolCallTargetMessage(payload.sessionKey, payload.toolCallId);
|
|
319
|
+
const args = this.toolCallArgsRawByCallId.get(payload.toolCallId) ?? "";
|
|
320
|
+
const nextParts = this.upsertToolInvocationPart(targetMessage.parts, {
|
|
321
|
+
type: "tool-invocation",
|
|
322
|
+
toolCallId: payload.toolCallId,
|
|
323
|
+
toolName: this.findToolNameByCallId(targetMessage.parts, payload.toolCallId) ?? "unknown",
|
|
324
|
+
state: "call",
|
|
325
|
+
args
|
|
326
|
+
});
|
|
327
|
+
this.replaceStreamingMessage({
|
|
328
|
+
...targetMessage,
|
|
329
|
+
parts: nextParts,
|
|
330
|
+
status: "streaming"
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
handleMessageToolCallResult(payload) {
|
|
334
|
+
const updated = this.updateMessageContainingToolCall(payload.toolCallId, (targetMessage, existingPart) => {
|
|
335
|
+
const mergedPart = {
|
|
336
|
+
type: "tool-invocation",
|
|
337
|
+
toolCallId: payload.toolCallId,
|
|
338
|
+
toolName: existingPart.toolName,
|
|
339
|
+
state: "result",
|
|
340
|
+
args: existingPart.args,
|
|
341
|
+
result: payload.content
|
|
342
|
+
};
|
|
343
|
+
return this.upsertToolInvocationPart(targetMessage.parts, mergedPart);
|
|
344
|
+
});
|
|
345
|
+
if (!updated) {
|
|
346
|
+
const fallbackMessage = this.resolveToolCallTargetMessage(payload.sessionKey, payload.toolCallId);
|
|
347
|
+
const nextParts = this.upsertToolInvocationPart(fallbackMessage.parts, {
|
|
348
|
+
type: "tool-invocation",
|
|
349
|
+
toolCallId: payload.toolCallId,
|
|
350
|
+
toolName: "unknown",
|
|
351
|
+
state: "result",
|
|
352
|
+
result: payload.content
|
|
353
|
+
});
|
|
354
|
+
this.replaceStreamingMessage({
|
|
355
|
+
...fallbackMessage,
|
|
356
|
+
parts: nextParts,
|
|
357
|
+
status: "streaming"
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
handleRunStarted(_payload) {
|
|
362
|
+
this.setError(null);
|
|
363
|
+
}
|
|
364
|
+
handleRunFinished(_payload) {
|
|
365
|
+
}
|
|
366
|
+
handleRunError(payload) {
|
|
367
|
+
this.setError(buildRuntimeError(payload));
|
|
368
|
+
}
|
|
369
|
+
handleRunMetadata(_payload) {
|
|
370
|
+
}
|
|
371
|
+
handleEndpointError(payload) {
|
|
372
|
+
this.setError(payload);
|
|
373
|
+
}
|
|
374
|
+
applyToolCallArgs(sessionKey, toolCallId, args, messageId) {
|
|
375
|
+
const targetMessage = this.resolveToolCallTargetMessage(sessionKey, toolCallId, messageId);
|
|
376
|
+
const toolName = this.findToolNameByCallId(targetMessage.parts, toolCallId) ?? "unknown";
|
|
377
|
+
const nextParts = this.upsertToolInvocationPart(targetMessage.parts, {
|
|
378
|
+
type: "tool-invocation",
|
|
379
|
+
toolCallId,
|
|
380
|
+
toolName,
|
|
381
|
+
state: "partial-call",
|
|
382
|
+
args
|
|
383
|
+
});
|
|
384
|
+
this.replaceStreamingMessage({
|
|
385
|
+
...targetMessage,
|
|
386
|
+
parts: nextParts,
|
|
387
|
+
status: "streaming"
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
ensureStreamingMessage(sessionKey, messageId, status) {
|
|
391
|
+
if (this.streamingMessage?.id === messageId) {
|
|
392
|
+
if (this.streamingMessage.status === status) {
|
|
393
|
+
return this.streamingMessage;
|
|
394
|
+
}
|
|
395
|
+
const nextStreamingMessage2 = {
|
|
396
|
+
...this.streamingMessage,
|
|
397
|
+
status
|
|
398
|
+
};
|
|
399
|
+
this.replaceStreamingMessage(nextStreamingMessage2);
|
|
400
|
+
return nextStreamingMessage2;
|
|
401
|
+
}
|
|
402
|
+
const nextStreamingMessage = {
|
|
403
|
+
id: messageId,
|
|
404
|
+
sessionKey,
|
|
405
|
+
role: DEFAULT_ASSISTANT_ROLE,
|
|
406
|
+
status,
|
|
407
|
+
parts: [],
|
|
408
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
409
|
+
};
|
|
410
|
+
this.replaceStreamingMessage(nextStreamingMessage);
|
|
411
|
+
return nextStreamingMessage;
|
|
412
|
+
}
|
|
413
|
+
resolveToolCallTargetMessage(sessionKey, toolCallId, messageId) {
|
|
414
|
+
const preferredMessageId = messageId?.trim() || this.toolCallMessageIdByCallId.get(toolCallId) || this.streamingMessage?.id || `tool-${toolCallId}`;
|
|
415
|
+
this.toolCallMessageIdByCallId.set(toolCallId, preferredMessageId);
|
|
416
|
+
return this.ensureStreamingMessage(sessionKey, preferredMessageId, "streaming");
|
|
417
|
+
}
|
|
418
|
+
updateMessageContainingToolCall(toolCallId, updater) {
|
|
419
|
+
if (this.streamingMessage) {
|
|
420
|
+
const part = this.findToolInvocationPart(this.streamingMessage.parts, toolCallId);
|
|
421
|
+
if (part) {
|
|
422
|
+
const nextParts = updater(this.streamingMessage, part);
|
|
423
|
+
this.replaceStreamingMessage({ ...this.streamingMessage, parts: nextParts });
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
for (let index = this.messages.length - 1; index >= 0; index -= 1) {
|
|
428
|
+
const candidateMessage = this.messages[index];
|
|
429
|
+
const part = this.findToolInvocationPart(candidateMessage.parts, toolCallId);
|
|
430
|
+
if (!part) {
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
const nextMessages = [...this.messages];
|
|
434
|
+
nextMessages[index] = {
|
|
435
|
+
...candidateMessage,
|
|
436
|
+
parts: updater(candidateMessage, part)
|
|
437
|
+
};
|
|
438
|
+
this.messages = nextMessages;
|
|
439
|
+
this.stateVersion += 1;
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
findToolInvocationPart(parts, toolCallId) {
|
|
445
|
+
for (let index = parts.length - 1; index >= 0; index -= 1) {
|
|
446
|
+
const part = parts[index];
|
|
447
|
+
if (part.type === "tool-invocation" && part.toolCallId === toolCallId) {
|
|
448
|
+
return part;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
findToolNameByCallId(parts, toolCallId) {
|
|
454
|
+
const part = this.findToolInvocationPart(parts, toolCallId);
|
|
455
|
+
return part?.toolName ?? null;
|
|
456
|
+
}
|
|
457
|
+
upsertToolInvocationPart(parts, toolPart) {
|
|
458
|
+
const nextParts = [...parts];
|
|
459
|
+
for (let index = nextParts.length - 1; index >= 0; index -= 1) {
|
|
460
|
+
const part = nextParts[index];
|
|
461
|
+
if (part.type === "tool-invocation" && part.toolCallId === toolPart.toolCallId) {
|
|
462
|
+
nextParts[index] = {
|
|
463
|
+
...part,
|
|
464
|
+
...toolPart
|
|
465
|
+
};
|
|
466
|
+
return nextParts;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
nextParts.push(toolPart);
|
|
470
|
+
return nextParts;
|
|
471
|
+
}
|
|
472
|
+
findLastReasoningPartIndex(parts) {
|
|
473
|
+
for (let index = parts.length - 1; index >= 0; index -= 1) {
|
|
474
|
+
if (parts[index]?.type === "reasoning") {
|
|
475
|
+
return index;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return -1;
|
|
479
|
+
}
|
|
480
|
+
upsertMessage(message) {
|
|
481
|
+
const normalizedMessage = cloneMessage(message);
|
|
482
|
+
const messageIndex = this.messages.findIndex((item) => item.id === normalizedMessage.id);
|
|
483
|
+
if (messageIndex < 0) {
|
|
484
|
+
this.messages = [...this.messages, normalizedMessage];
|
|
485
|
+
this.stateVersion += 1;
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const nextMessages = [...this.messages];
|
|
489
|
+
nextMessages[messageIndex] = normalizedMessage;
|
|
490
|
+
this.messages = nextMessages;
|
|
491
|
+
this.stateVersion += 1;
|
|
492
|
+
}
|
|
493
|
+
replaceStreamingMessage(nextStreamingMessage) {
|
|
494
|
+
if (!nextStreamingMessage && !this.streamingMessage) {
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
this.streamingMessage = nextStreamingMessage ? cloneMessage(nextStreamingMessage) : null;
|
|
498
|
+
this.stateVersion += 1;
|
|
499
|
+
}
|
|
500
|
+
setError(nextError) {
|
|
501
|
+
const hasSameError = this.error?.code === nextError?.code && this.error?.message === nextError?.message && this.error?.details === nextError?.details && this.error?.cause === nextError?.cause;
|
|
502
|
+
if (hasSameError) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
this.error = nextError ? { ...nextError, details: nextError.details ? { ...nextError.details } : void 0 } : null;
|
|
506
|
+
this.stateVersion += 1;
|
|
507
|
+
}
|
|
508
|
+
clearToolCallTrackingByMessageId(messageId) {
|
|
509
|
+
for (const [toolCallId, trackedMessageId] of this.toolCallMessageIdByCallId) {
|
|
510
|
+
if (trackedMessageId !== messageId) {
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
this.toolCallMessageIdByCallId.delete(toolCallId);
|
|
514
|
+
this.toolCallArgsRawByCallId.delete(toolCallId);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
export {
|
|
519
|
+
DefaultNcpAgentConversationStateManager
|
|
520
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nextclaw/ncp-toolkit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Toolkit implementations built on top of the NextClaw Communication Protocol.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"development": "./src/index.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@nextclaw/ncp": "0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^20.17.6",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
23
|
+
"@typescript-eslint/parser": "^7.18.0",
|
|
24
|
+
"eslint": "^8.57.1",
|
|
25
|
+
"eslint-config-prettier": "^9.1.0",
|
|
26
|
+
"prettier": "^3.3.3",
|
|
27
|
+
"tsup": "^8.3.5",
|
|
28
|
+
"typescript": "^5.6.3",
|
|
29
|
+
"vitest": "^2.1.2"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup src/index.ts --format esm --dts --out-dir dist",
|
|
33
|
+
"lint": "eslint .",
|
|
34
|
+
"tsc": "tsc -p tsconfig.json",
|
|
35
|
+
"test": "vitest run"
|
|
36
|
+
}
|
|
37
|
+
}
|