@nextclaw/ncp-toolkit 0.4.10 → 0.4.11

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.
Files changed (2) hide show
  1. package/dist/index.js +70 -13
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -845,6 +845,7 @@ var AgentRunExecutor = class {
845
845
 
846
846
  // src/agent/agent-backend/agent-backend-session-utils.ts
847
847
  import { NcpEventType as NcpEventType4 } from "@nextclaw/ncp";
848
+ var AUTO_SESSION_LABEL_MAX_LENGTH = 64;
848
849
  function readMessages(snapshot) {
849
850
  const messages = snapshot.messages.map((message) => structuredClone(message));
850
851
  if (snapshot.streamingMessage) {
@@ -853,32 +854,83 @@ function readMessages(snapshot) {
853
854
  return messages;
854
855
  }
855
856
  function toSessionSummary(session, liveSession) {
857
+ const metadata = withAutoSessionLabel({
858
+ metadata: session.metadata ? structuredClone({
859
+ ...session.metadata,
860
+ ...liveSession?.metadata ? liveSession.metadata : {}
861
+ }) : liveSession?.metadata ? structuredClone(liveSession.metadata) : {},
862
+ messages: session.messages
863
+ });
856
864
  return {
857
865
  sessionId: session.sessionId,
858
866
  messageCount: session.messages.length,
859
867
  updatedAt: session.updatedAt,
860
868
  status: liveSession?.activeExecution ? "running" : "idle",
861
- ...session.metadata ? {
862
- metadata: structuredClone({
863
- ...session.metadata,
864
- ...liveSession?.metadata ? liveSession.metadata : {}
865
- })
866
- } : liveSession?.metadata ? { metadata: structuredClone(liveSession.metadata) } : {}
869
+ ...Object.keys(metadata).length > 0 ? { metadata } : {}
867
870
  };
868
871
  }
869
872
  function toLiveSessionSummary(session) {
870
873
  const snapshot = session.stateManager.getSnapshot();
874
+ const messages = readMessages(snapshot);
875
+ const metadata = withAutoSessionLabel({
876
+ metadata: Object.keys(session.metadata).length > 0 ? structuredClone(session.metadata) : session.activeExecution?.requestEnvelope.metadata ? structuredClone(session.activeExecution.requestEnvelope.metadata) : {},
877
+ messages
878
+ });
871
879
  return {
872
880
  sessionId: session.sessionId,
873
- messageCount: readMessages(snapshot).length,
881
+ messageCount: messages.length,
874
882
  updatedAt: now(),
875
883
  status: session.activeExecution ? "running" : "idle",
876
- ...Object.keys(session.metadata).length > 0 ? { metadata: structuredClone(session.metadata) } : session.activeExecution?.requestEnvelope.metadata ? { metadata: structuredClone(session.activeExecution.requestEnvelope.metadata) } : {}
884
+ ...Object.keys(metadata).length > 0 ? { metadata } : {}
877
885
  };
878
886
  }
879
887
  function now() {
880
888
  return (/* @__PURE__ */ new Date()).toISOString();
881
889
  }
890
+ function readOptionalString(value) {
891
+ if (typeof value !== "string") {
892
+ return null;
893
+ }
894
+ const trimmed = value.trim();
895
+ return trimmed.length > 0 ? trimmed : null;
896
+ }
897
+ function truncateLabel(value) {
898
+ const characters = Array.from(value);
899
+ if (characters.length <= AUTO_SESSION_LABEL_MAX_LENGTH) {
900
+ return value;
901
+ }
902
+ return `${characters.slice(0, AUTO_SESSION_LABEL_MAX_LENGTH).join("")}\u2026`;
903
+ }
904
+ function resolveAutoSessionLabelFromMessages(messages) {
905
+ for (const message of messages) {
906
+ if (message.role !== "user") {
907
+ continue;
908
+ }
909
+ for (const part of message.parts) {
910
+ if (part.type === "text" || part.type === "rich-text") {
911
+ const text = readOptionalString(part.text);
912
+ if (text) {
913
+ return truncateLabel(text);
914
+ }
915
+ }
916
+ }
917
+ }
918
+ return null;
919
+ }
920
+ function withAutoSessionLabel(params) {
921
+ const existingLabel = readOptionalString(params.metadata.label);
922
+ if (existingLabel) {
923
+ return params.metadata;
924
+ }
925
+ const nextLabel = resolveAutoSessionLabelFromMessages(params.messages);
926
+ if (!nextLabel) {
927
+ return params.metadata;
928
+ }
929
+ return {
930
+ ...params.metadata,
931
+ label: nextLabel
932
+ };
933
+ }
882
934
  function isTerminalEvent(event) {
883
935
  switch (event.type) {
884
936
  case NcpEventType4.MessageAbort:
@@ -904,14 +956,19 @@ function buildUpdatedSessionRecord(params) {
904
956
  };
905
957
  }
906
958
  function buildPersistedLiveSessionRecord(params) {
907
- return {
908
- sessionId: params.sessionId,
909
- messages: readMessages(params.session.stateManager.getSnapshot()),
910
- updatedAt: params.updatedAt,
959
+ const messages = readMessages(params.session.stateManager.getSnapshot());
960
+ const metadata = withAutoSessionLabel({
911
961
  metadata: {
912
962
  ...params.session.metadata ? structuredClone(params.session.metadata) : {},
913
963
  ...params.session.activeExecution?.requestEnvelope.metadata ? structuredClone(params.session.activeExecution.requestEnvelope.metadata) : {}
914
- }
964
+ },
965
+ messages
966
+ });
967
+ return {
968
+ sessionId: params.sessionId,
969
+ messages,
970
+ updatedAt: params.updatedAt,
971
+ metadata
915
972
  };
916
973
  }
917
974
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextclaw/ncp-toolkit",
3
- "version": "0.4.10",
3
+ "version": "0.4.11",
4
4
  "private": false,
5
5
  "description": "Toolkit implementations built on top of the NextClaw Communication Protocol.",
6
6
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "prettier": "^3.3.3",
23
23
  "tsup": "^8.3.5",
24
24
  "typescript": "^5.6.3",
25
- "vitest": "^2.1.2",
25
+ "vitest": "^4.1.2",
26
26
  "@nextclaw/ncp-agent-runtime": "0.3.2"
27
27
  },
28
28
  "scripts": {