@nextclaw/ncp-toolkit 0.4.17 → 0.5.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
@@ -69,6 +69,7 @@ declare class EventPublisher {
69
69
 
70
70
  type RuntimeFactoryParams = {
71
71
  sessionId: string;
72
+ agentId?: string;
72
73
  stateManager: NcpAgentConversationStateManager;
73
74
  sessionMetadata: Record<string, unknown>;
74
75
  setSessionMetadata: (nextMetadata: Record<string, unknown>) => void;
@@ -76,6 +77,7 @@ type RuntimeFactoryParams = {
76
77
  type CreateRuntimeFn = (params: RuntimeFactoryParams) => NcpAgentRuntime;
77
78
  type AgentSessionRecord = {
78
79
  sessionId: string;
80
+ agentId?: string;
79
81
  messages: NcpMessage[];
80
82
  updatedAt: string;
81
83
  metadata?: Record<string, unknown>;
@@ -88,6 +90,7 @@ type LiveSessionExecution = {
88
90
  };
89
91
  type LiveSessionState = {
90
92
  sessionId: string;
93
+ agentId?: string;
91
94
  runtime: NcpAgentRuntime;
92
95
  stateManager: NcpAgentConversationStateManager;
93
96
  metadata: Record<string, unknown>;
package/dist/index.js CHANGED
@@ -751,15 +751,28 @@ var EventPublisher = class {
751
751
  };
752
752
 
753
753
  // src/agent/agent-backend/agent-live-session-registry.ts
754
+ function readOptionalAgentId(value) {
755
+ if (typeof value !== "string") {
756
+ return void 0;
757
+ }
758
+ const trimmed = value.trim().toLowerCase();
759
+ return trimmed.length > 0 ? trimmed : void 0;
760
+ }
761
+ function readAgentIdFromMetadata(metadata) {
762
+ return readOptionalAgentId(metadata?.agent_id) ?? readOptionalAgentId(metadata?.agentId);
763
+ }
754
764
  var AgentLiveSessionRegistry = class {
755
765
  constructor(sessionStore, createRuntime) {
756
766
  this.sessionStore = sessionStore;
757
767
  this.createRuntime = createRuntime;
758
768
  }
759
769
  sessions = /* @__PURE__ */ new Map();
760
- async ensureSession(sessionId, initialMetadata) {
770
+ ensureSession = async (sessionId, initialMetadata) => {
761
771
  const existing = this.sessions.get(sessionId);
762
772
  if (existing) {
773
+ if (!existing.agentId) {
774
+ existing.agentId = readAgentIdFromMetadata(initialMetadata) ?? existing.agentId;
775
+ }
763
776
  if (initialMetadata && Object.keys(initialMetadata).length > 0) {
764
777
  existing.metadata = {
765
778
  ...existing.metadata,
@@ -778,8 +791,10 @@ var AgentLiveSessionRegistry = class {
778
791
  ...storedSession?.metadata ? structuredClone(storedSession.metadata) : {},
779
792
  ...initialMetadata ? structuredClone(initialMetadata) : {}
780
793
  };
794
+ const sessionAgentId = readOptionalAgentId(storedSession?.agentId) ?? readAgentIdFromMetadata(initialMetadata);
781
795
  const session = {
782
796
  sessionId,
797
+ ...sessionAgentId ? { agentId: sessionAgentId } : {},
783
798
  stateManager,
784
799
  metadata: sessionMetadata,
785
800
  runtime: null,
@@ -788,6 +803,7 @@ var AgentLiveSessionRegistry = class {
788
803
  };
789
804
  session.runtime = this.createRuntime({
790
805
  sessionId,
806
+ ...sessionAgentId ? { agentId: sessionAgentId } : {},
791
807
  stateManager,
792
808
  sessionMetadata,
793
809
  setSessionMetadata: (nextMetadata) => {
@@ -798,23 +814,23 @@ var AgentLiveSessionRegistry = class {
798
814
  });
799
815
  this.sessions.set(sessionId, session);
800
816
  return session;
801
- }
802
- getSession(sessionId) {
817
+ };
818
+ getSession = (sessionId) => {
803
819
  return this.sessions.get(sessionId) ?? null;
804
- }
805
- deleteSession(sessionId) {
820
+ };
821
+ deleteSession = (sessionId) => {
806
822
  const session = this.sessions.get(sessionId) ?? null;
807
823
  if (session) {
808
824
  this.sessions.delete(sessionId);
809
825
  }
810
826
  return session;
811
- }
812
- clear() {
827
+ };
828
+ clear = () => {
813
829
  this.sessions.clear();
814
- }
815
- listSessions() {
830
+ };
831
+ listSessions = () => {
816
832
  return [...this.sessions.values()];
817
- }
833
+ };
818
834
  };
819
835
  function cloneMessages(messages) {
820
836
  return messages.map((message) => structuredClone(message));
@@ -1099,6 +1115,13 @@ var AgentBackendSessionRealtime = class {
1099
1115
  // src/agent/agent-backend/agent-backend-session-utils.ts
1100
1116
  import { NcpEventType as NcpEventType5 } from "@nextclaw/ncp";
1101
1117
  var AUTO_SESSION_LABEL_MAX_LENGTH = 64;
1118
+ function readOptionalAgentId2(value) {
1119
+ if (typeof value !== "string") {
1120
+ return void 0;
1121
+ }
1122
+ const trimmed = value.trim().toLowerCase();
1123
+ return trimmed.length > 0 ? trimmed : void 0;
1124
+ }
1102
1125
  function readMessages(snapshot) {
1103
1126
  const messages = snapshot.messages.map((message) => structuredClone(message));
1104
1127
  if (snapshot.streamingMessage) {
@@ -1116,6 +1139,7 @@ function toSessionSummary(session, liveSession) {
1116
1139
  });
1117
1140
  return {
1118
1141
  sessionId: session.sessionId,
1142
+ ...readOptionalAgentId2(session.agentId) ? { agentId: readOptionalAgentId2(session.agentId) } : {},
1119
1143
  messageCount: session.messages.length,
1120
1144
  updatedAt: session.updatedAt,
1121
1145
  status: liveSession?.activeExecution ? "running" : "idle",
@@ -1131,6 +1155,7 @@ function toLiveSessionSummary(session) {
1131
1155
  });
1132
1156
  return {
1133
1157
  sessionId: session.sessionId,
1158
+ ...readOptionalAgentId2(session.agentId) ? { agentId: readOptionalAgentId2(session.agentId) } : {},
1134
1159
  messageCount: messages.length,
1135
1160
  updatedAt: now(),
1136
1161
  status: session.activeExecution ? "running" : "idle",
@@ -1186,6 +1211,19 @@ function withAutoSessionLabel(params) {
1186
1211
  }
1187
1212
 
1188
1213
  // src/agent/agent-backend/agent-backend-session-persistence.ts
1214
+ function readOptionalAgentId3(value) {
1215
+ if (typeof value !== "string") {
1216
+ return void 0;
1217
+ }
1218
+ const trimmed = value.trim().toLowerCase();
1219
+ return trimmed.length > 0 ? trimmed : void 0;
1220
+ }
1221
+ function readAgentIdFromMetadata2(metadata) {
1222
+ return readOptionalAgentId3(metadata?.agent_id) ?? readOptionalAgentId3(metadata?.agentId);
1223
+ }
1224
+ function resolvePersistedAgentId(params) {
1225
+ return readOptionalAgentId3(params.liveSession?.agentId) ?? readOptionalAgentId3(params.storedSession?.agentId) ?? readAgentIdFromMetadata2(params.liveSession?.metadata) ?? readAgentIdFromMetadata2(params.storedSession?.metadata);
1226
+ }
1189
1227
  function buildUpdatedSessionRecord(params) {
1190
1228
  const nextMetadata = params.patch.metadata === null ? {} : params.patch.metadata ? structuredClone(params.patch.metadata) : structuredClone(params.liveSession?.metadata ?? params.storedSession?.metadata ?? {});
1191
1229
  if (params.liveSession) {
@@ -1193,6 +1231,15 @@ function buildUpdatedSessionRecord(params) {
1193
1231
  }
1194
1232
  return {
1195
1233
  sessionId: params.sessionId,
1234
+ ...resolvePersistedAgentId({
1235
+ liveSession: params.liveSession,
1236
+ storedSession: params.storedSession
1237
+ }) ? {
1238
+ agentId: resolvePersistedAgentId({
1239
+ liveSession: params.liveSession,
1240
+ storedSession: params.storedSession
1241
+ })
1242
+ } : {},
1196
1243
  messages: params.liveSession ? readMessages(params.liveSession.stateManager.getSnapshot()) : params.storedSession?.messages.map((message) => structuredClone(message)) ?? [],
1197
1244
  updatedAt: params.updatedAt,
1198
1245
  metadata: nextMetadata
@@ -1209,6 +1256,9 @@ function buildPersistedLiveSessionRecord(params) {
1209
1256
  });
1210
1257
  return {
1211
1258
  sessionId: params.sessionId,
1259
+ ...readOptionalAgentId3(params.session.agentId) ?? readAgentIdFromMetadata2(params.session.metadata) ?? readAgentIdFromMetadata2(params.session.activeExecution?.requestEnvelope.metadata) ? {
1260
+ agentId: readOptionalAgentId3(params.session.agentId) ?? readAgentIdFromMetadata2(params.session.metadata) ?? readAgentIdFromMetadata2(params.session.activeExecution?.requestEnvelope.metadata)
1261
+ } : {},
1212
1262
  messages,
1213
1263
  updatedAt: params.updatedAt,
1214
1264
  metadata
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextclaw/ncp-toolkit",
3
- "version": "0.4.17",
3
+ "version": "0.5.0",
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.4.6"
18
+ "@nextclaw/ncp": "0.5.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": "^4.1.2",
26
- "@nextclaw/ncp-agent-runtime": "0.3.6"
26
+ "@nextclaw/ncp-agent-runtime": "0.3.7"
27
27
  },
28
28
  "scripts": {
29
29
  "build": "tsup src/index.ts --format esm --dts --out-dir dist",