@nextclaw/ncp-toolkit 0.5.9 → 0.5.11-beta.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/dist/index.d.ts +12 -1
- package/dist/index.js +41 -9
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NcpAgentClientEndpoint, NcpAgentConversationHydrationParams, NcpAgentConversationSnapshot, NcpAgentConversationStateManager, NcpAgentRunApi, NcpAgentRunInput, NcpAgentRunSendOptions, NcpAgentRunStreamOptions, NcpAgentRuntime, NcpAgentServerEndpoint, NcpAgentStreamProvider, NcpEndpointEvent, NcpEndpointManifest, NcpEndpointSubscriber, NcpError, NcpErrorCode, NcpMessage, NcpMessageAbortPayload, NcpMessagePart, NcpMessageSentPayload, NcpReasoningDeltaPayload, NcpReasoningEndPayload, NcpReasoningStartPayload, NcpRequestEnvelope, NcpRunErrorPayload, NcpRunFinishedPayload, NcpRunMetadataPayload, NcpRunStartedPayload, NcpSessionApi, NcpSessionPatch, NcpSessionSummary, NcpStreamRequestPayload, NcpTextDeltaPayload, NcpTextEndPayload, NcpTextStartPayload, NcpToolCallArgsDeltaPayload, NcpToolCallArgsPayload, NcpToolCallEndPayload, NcpToolCallResultPayload, NcpToolCallStartPayload, OpenAITool } from "@nextclaw/ncp";
|
|
1
|
+
import { NcpAgentClientEndpoint, NcpAgentConversationHydrationParams, NcpAgentConversationSnapshot, NcpAgentConversationStateManager, NcpAgentRunApi, NcpAgentRunInput, NcpAgentRunSendOptions, NcpAgentRunStreamOptions, NcpAgentRuntime, NcpAgentServerEndpoint, NcpAgentStreamProvider, NcpContextWindowUpdatedPayload, NcpEndpointEvent, NcpEndpointManifest, NcpEndpointSubscriber, NcpError, NcpErrorCode, NcpMessage, NcpMessageAbortPayload, NcpMessagePart, NcpMessageSentPayload, NcpReasoningDeltaPayload, NcpReasoningEndPayload, NcpReasoningStartPayload, NcpRequestEnvelope, NcpRunErrorPayload, NcpRunFinishedPayload, NcpRunMetadataPayload, NcpRunStartedPayload, NcpSessionApi, NcpSessionPatch, NcpSessionSummary, NcpStreamRequestPayload, NcpTextDeltaPayload, NcpTextEndPayload, NcpTextStartPayload, NcpToolCallArgsDeltaPayload, NcpToolCallArgsPayload, NcpToolCallEndPayload, NcpToolCallResultPayload, NcpToolCallStartPayload, OpenAITool } from "@nextclaw/ncp";
|
|
2
2
|
|
|
3
3
|
//#region src/agent/agent-conversation-state-manager.d.ts
|
|
4
4
|
declare class DefaultNcpAgentConversationStateManager implements NcpAgentConversationStateManager {
|
|
@@ -6,6 +6,7 @@ declare class DefaultNcpAgentConversationStateManager implements NcpAgentConvers
|
|
|
6
6
|
private streamingMessage;
|
|
7
7
|
private error;
|
|
8
8
|
private activeRun;
|
|
9
|
+
private contextWindow;
|
|
9
10
|
private readonly listeners;
|
|
10
11
|
private readonly toolCallMessageIdByCallId;
|
|
11
12
|
private readonly toolCallArgsRawByCallId;
|
|
@@ -37,6 +38,7 @@ declare class DefaultNcpAgentConversationStateManager implements NcpAgentConvers
|
|
|
37
38
|
handleRunFinished: (payload: NcpRunFinishedPayload) => void;
|
|
38
39
|
handleRunError: (payload: NcpRunErrorPayload) => void;
|
|
39
40
|
handleRunMetadata: (payload: NcpRunMetadataPayload) => void;
|
|
41
|
+
handleContextWindowUpdated: (payload: NcpContextWindowUpdatedPayload) => void;
|
|
40
42
|
handleEndpointError: (payload: NcpError) => void;
|
|
41
43
|
private applyToolCallArgs;
|
|
42
44
|
private ensureStreamingMessage;
|
|
@@ -111,10 +113,18 @@ interface AgentSessionStore {
|
|
|
111
113
|
deleteSession(sessionId: string): Promise<AgentSessionRecord | null>;
|
|
112
114
|
}
|
|
113
115
|
//#endregion
|
|
116
|
+
//#region src/agent/agent-backend/agent-backend-session-utils.d.ts
|
|
117
|
+
type SessionContextWindowResolver = (params: {
|
|
118
|
+
messages: readonly NcpMessage[];
|
|
119
|
+
metadata: Record<string, unknown>;
|
|
120
|
+
sessionId: string;
|
|
121
|
+
}) => Record<string, unknown> | null;
|
|
122
|
+
//#endregion
|
|
114
123
|
//#region src/agent/agent-backend/agent-backend.d.ts
|
|
115
124
|
type DefaultNcpAgentBackendConfig = {
|
|
116
125
|
createRuntime: CreateRuntimeFn;
|
|
117
126
|
sessionStore: AgentSessionStore;
|
|
127
|
+
resolveSessionContextWindow?: SessionContextWindowResolver;
|
|
118
128
|
onSessionRunStatusChanged?: (payload: {
|
|
119
129
|
sessionKey: string;
|
|
120
130
|
status: "running" | "idle";
|
|
@@ -135,6 +145,7 @@ declare class DefaultNcpAgentBackend implements NcpAgentServerEndpoint, NcpSessi
|
|
|
135
145
|
private readonly executor;
|
|
136
146
|
private readonly publisher;
|
|
137
147
|
private readonly sessionRealtime;
|
|
148
|
+
private readonly resolveSessionContextWindow;
|
|
138
149
|
private started;
|
|
139
150
|
constructor(config: DefaultNcpAgentBackendConfig);
|
|
140
151
|
start: () => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -91,6 +91,7 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
91
91
|
streamingMessage = null;
|
|
92
92
|
error = null;
|
|
93
93
|
activeRun = null;
|
|
94
|
+
contextWindow = null;
|
|
94
95
|
listeners = /* @__PURE__ */ new Set();
|
|
95
96
|
toolCallMessageIdByCallId = /* @__PURE__ */ new Map();
|
|
96
97
|
toolCallArgsRawByCallId = /* @__PURE__ */ new Map();
|
|
@@ -107,7 +108,8 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
107
108
|
...this.error,
|
|
108
109
|
details: this.error.details ? { ...this.error.details } : void 0
|
|
109
110
|
} : null,
|
|
110
|
-
activeRun: this.activeRun ? { ...this.activeRun } : null
|
|
111
|
+
activeRun: this.activeRun ? { ...this.activeRun } : null,
|
|
112
|
+
contextWindow: this.contextWindow ? { ...this.contextWindow } : null
|
|
111
113
|
};
|
|
112
114
|
this.snapshotCache = snapshot;
|
|
113
115
|
this.snapshotVersion = this.stateVersion;
|
|
@@ -118,11 +120,12 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
118
120
|
return () => this.listeners.delete(listener);
|
|
119
121
|
};
|
|
120
122
|
reset = () => {
|
|
121
|
-
if (this.messages.length === 0 && !this.streamingMessage && !this.error && !this.activeRun && this.toolCallMessageIdByCallId.size === 0 && this.toolCallArgsRawByCallId.size === 0) return;
|
|
123
|
+
if (this.messages.length === 0 && !this.streamingMessage && !this.error && !this.activeRun && !this.contextWindow && this.toolCallMessageIdByCallId.size === 0 && this.toolCallArgsRawByCallId.size === 0) return;
|
|
122
124
|
this.messages = [];
|
|
123
125
|
this.streamingMessage = null;
|
|
124
126
|
this.error = null;
|
|
125
127
|
this.activeRun = null;
|
|
128
|
+
this.contextWindow = null;
|
|
126
129
|
this.toolCallMessageIdByCallId.clear();
|
|
127
130
|
this.toolCallArgsRawByCallId.clear();
|
|
128
131
|
this.lastSettledRunId = null;
|
|
@@ -133,6 +136,7 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
133
136
|
this.messages = payload.messages.map((message) => normalizeConversationMessage(message));
|
|
134
137
|
this.streamingMessage = null;
|
|
135
138
|
this.error = null;
|
|
139
|
+
this.contextWindow = payload.contextWindow ? { ...payload.contextWindow } : null;
|
|
136
140
|
this.activeRun = payload.activeRun ? {
|
|
137
141
|
...payload.activeRun,
|
|
138
142
|
sessionId: payload.activeRun.sessionId ?? payload.sessionId,
|
|
@@ -204,6 +208,9 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
204
208
|
case NcpEventType.RunMetadata:
|
|
205
209
|
this.handleRunMetadata(event.payload);
|
|
206
210
|
break;
|
|
211
|
+
case NcpEventType.ContextWindowUpdated:
|
|
212
|
+
this.handleContextWindowUpdated(event.payload);
|
|
213
|
+
break;
|
|
207
214
|
case NcpEventType.EndpointError:
|
|
208
215
|
this.handleEndpointError(event.payload);
|
|
209
216
|
break;
|
|
@@ -343,7 +350,8 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
343
350
|
toolName: existingPart.toolName,
|
|
344
351
|
state: "result",
|
|
345
352
|
args: existingPart.args,
|
|
346
|
-
result: payload.content
|
|
353
|
+
result: payload.content,
|
|
354
|
+
resultContentItems: payload.contentItems
|
|
347
355
|
};
|
|
348
356
|
return upsertToolInvocationPart(targetMessage.parts, mergedPart);
|
|
349
357
|
})) {
|
|
@@ -353,7 +361,8 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
353
361
|
toolCallId: payload.toolCallId,
|
|
354
362
|
toolName: "unknown",
|
|
355
363
|
state: "result",
|
|
356
|
-
result: payload.content
|
|
364
|
+
result: payload.content,
|
|
365
|
+
resultContentItems: payload.contentItems
|
|
357
366
|
});
|
|
358
367
|
this.replaceStreamingMessage({
|
|
359
368
|
...fallbackMessage,
|
|
@@ -399,6 +408,10 @@ var DefaultNcpAgentConversationStateManager = class {
|
|
|
399
408
|
this.clearActiveRun();
|
|
400
409
|
}
|
|
401
410
|
};
|
|
411
|
+
handleContextWindowUpdated = (payload) => {
|
|
412
|
+
this.contextWindow = { ...payload.contextWindow };
|
|
413
|
+
this.stateVersion += 1;
|
|
414
|
+
};
|
|
402
415
|
handleEndpointError = (payload) => {
|
|
403
416
|
if (payload.code === "abort-error") {
|
|
404
417
|
this.handleMessageAbort({
|
|
@@ -992,6 +1005,21 @@ function toLiveSessionSummary(session) {
|
|
|
992
1005
|
...Object.keys(metadata).length > 0 ? { metadata } : {}
|
|
993
1006
|
};
|
|
994
1007
|
}
|
|
1008
|
+
function withSessionContextWindow(summary, messages, resolver) {
|
|
1009
|
+
const contextWindow = resolver?.({
|
|
1010
|
+
messages,
|
|
1011
|
+
metadata: summary.metadata ?? {},
|
|
1012
|
+
sessionId: summary.sessionId
|
|
1013
|
+
});
|
|
1014
|
+
return contextWindow ? {
|
|
1015
|
+
...summary,
|
|
1016
|
+
contextWindow
|
|
1017
|
+
} : summary;
|
|
1018
|
+
}
|
|
1019
|
+
function toLiveSessionSummaryWithContextWindow(session, resolver) {
|
|
1020
|
+
const messages = readMessages(session.stateManager.getSnapshot());
|
|
1021
|
+
return withSessionContextWindow(toLiveSessionSummary(session), messages, resolver);
|
|
1022
|
+
}
|
|
995
1023
|
function now() {
|
|
996
1024
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
997
1025
|
}
|
|
@@ -1016,11 +1044,12 @@ function resolveAutoSessionLabelFromMessages(messages) {
|
|
|
1016
1044
|
return null;
|
|
1017
1045
|
}
|
|
1018
1046
|
function withAutoSessionLabel(params) {
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1047
|
+
const { metadata, messages } = params;
|
|
1048
|
+
if (readOptionalString$1(metadata.label)) return metadata;
|
|
1049
|
+
const nextLabel = resolveAutoSessionLabelFromMessages(messages);
|
|
1050
|
+
if (!nextLabel) return metadata;
|
|
1022
1051
|
return {
|
|
1023
|
-
...
|
|
1052
|
+
...metadata,
|
|
1024
1053
|
label: nextLabel
|
|
1025
1054
|
};
|
|
1026
1055
|
}
|
|
@@ -1140,9 +1169,11 @@ var DefaultNcpAgentBackend = class {
|
|
|
1140
1169
|
executor;
|
|
1141
1170
|
publisher;
|
|
1142
1171
|
sessionRealtime;
|
|
1172
|
+
resolveSessionContextWindow;
|
|
1143
1173
|
started = false;
|
|
1144
1174
|
constructor(config) {
|
|
1145
1175
|
this.sessionStore = config.sessionStore;
|
|
1176
|
+
this.resolveSessionContextWindow = config.resolveSessionContextWindow;
|
|
1146
1177
|
this.onSessionRunStatusChanged = config.onSessionRunStatusChanged;
|
|
1147
1178
|
this.sessionRegistry = new AgentLiveSessionRegistry(this.sessionStore, config.createRuntime);
|
|
1148
1179
|
this.executor = new AgentRunExecutor();
|
|
@@ -1258,7 +1289,8 @@ var DefaultNcpAgentBackend = class {
|
|
|
1258
1289
|
getSession = async (sessionId) => {
|
|
1259
1290
|
const liveSession = this.sessionRegistry.getSession(sessionId);
|
|
1260
1291
|
const storedSession = await this.sessionStore.getSession(sessionId);
|
|
1261
|
-
|
|
1292
|
+
const liveMessages = liveSession ? readMessages(liveSession.stateManager.getSnapshot()) : null;
|
|
1293
|
+
return storedSession ? withSessionContextWindow(toSessionSummary(storedSession, liveSession), liveMessages ?? storedSession.messages, this.resolveSessionContextWindow) : liveSession ? toLiveSessionSummaryWithContextWindow(liveSession, this.resolveSessionContextWindow) : null;
|
|
1262
1294
|
};
|
|
1263
1295
|
appendMessage = async (sessionId, message) => {
|
|
1264
1296
|
await this.ensureStarted();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextclaw/ncp-toolkit",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.11-beta.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Toolkit implementations built on top of the NextClaw Communication Protocol.",
|
|
6
6
|
"type": "module",
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@nextclaw/ncp": "0.5.
|
|
18
|
+
"@nextclaw/ncp": "0.5.6-beta.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/node": "^20.17.6",
|
|
22
22
|
"prettier": "^3.3.3",
|
|
23
23
|
"typescript": "^5.6.3",
|
|
24
24
|
"vitest": "^4.1.2",
|
|
25
|
-
"@nextclaw/ncp-agent-runtime": "0.3.
|
|
25
|
+
"@nextclaw/ncp-agent-runtime": "0.3.16-beta.0"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
28
|
"build": "tsdown src/index.ts --dts --clean --target es2022 --no-fixedExtension",
|