@nextclaw/ncp-toolkit 0.5.10 → 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 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;
@@ -401,6 +408,10 @@ var DefaultNcpAgentConversationStateManager = class {
401
408
  this.clearActiveRun();
402
409
  }
403
410
  };
411
+ handleContextWindowUpdated = (payload) => {
412
+ this.contextWindow = { ...payload.contextWindow };
413
+ this.stateVersion += 1;
414
+ };
404
415
  handleEndpointError = (payload) => {
405
416
  if (payload.code === "abort-error") {
406
417
  this.handleMessageAbort({
@@ -994,6 +1005,21 @@ function toLiveSessionSummary(session) {
994
1005
  ...Object.keys(metadata).length > 0 ? { metadata } : {}
995
1006
  };
996
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
+ }
997
1023
  function now() {
998
1024
  return (/* @__PURE__ */ new Date()).toISOString();
999
1025
  }
@@ -1018,11 +1044,12 @@ function resolveAutoSessionLabelFromMessages(messages) {
1018
1044
  return null;
1019
1045
  }
1020
1046
  function withAutoSessionLabel(params) {
1021
- if (readOptionalString$1(params.metadata.label)) return params.metadata;
1022
- const nextLabel = resolveAutoSessionLabelFromMessages(params.messages);
1023
- if (!nextLabel) return params.metadata;
1047
+ const { metadata, messages } = params;
1048
+ if (readOptionalString$1(metadata.label)) return metadata;
1049
+ const nextLabel = resolveAutoSessionLabelFromMessages(messages);
1050
+ if (!nextLabel) return metadata;
1024
1051
  return {
1025
- ...params.metadata,
1052
+ ...metadata,
1026
1053
  label: nextLabel
1027
1054
  };
1028
1055
  }
@@ -1142,9 +1169,11 @@ var DefaultNcpAgentBackend = class {
1142
1169
  executor;
1143
1170
  publisher;
1144
1171
  sessionRealtime;
1172
+ resolveSessionContextWindow;
1145
1173
  started = false;
1146
1174
  constructor(config) {
1147
1175
  this.sessionStore = config.sessionStore;
1176
+ this.resolveSessionContextWindow = config.resolveSessionContextWindow;
1148
1177
  this.onSessionRunStatusChanged = config.onSessionRunStatusChanged;
1149
1178
  this.sessionRegistry = new AgentLiveSessionRegistry(this.sessionStore, config.createRuntime);
1150
1179
  this.executor = new AgentRunExecutor();
@@ -1260,7 +1289,8 @@ var DefaultNcpAgentBackend = class {
1260
1289
  getSession = async (sessionId) => {
1261
1290
  const liveSession = this.sessionRegistry.getSession(sessionId);
1262
1291
  const storedSession = await this.sessionStore.getSession(sessionId);
1263
- return storedSession ? toSessionSummary(storedSession, liveSession) : liveSession ? toLiveSessionSummary(liveSession) : null;
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;
1264
1294
  };
1265
1295
  appendMessage = async (sessionId, message) => {
1266
1296
  await this.ensureStarted();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextclaw/ncp-toolkit",
3
- "version": "0.5.10",
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.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.15"
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",