@cognipeer/agent-sdk 0.2.3 → 0.2.5

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.cts CHANGED
@@ -165,9 +165,15 @@ type TraceSinkHttpConfig = {
165
165
  headers?: Record<string, string>;
166
166
  };
167
167
  type TraceSinkConfig = TraceSinkFileConfig | TraceSinkCustomConfig | TraceSinkCognipeerConfig | TraceSinkHttpConfig;
168
+ type TracingMode = "batched" | "streaming";
168
169
  type TracingConfig = {
169
170
  enabled: boolean;
170
171
  logData?: boolean;
172
+ /** Tracing mode:
173
+ * - "batched" (default): Events are collected and sent together when session ends
174
+ * - "streaming": Session starts immediately, events are sent in real-time as they occur
175
+ */
176
+ mode?: TracingMode;
171
177
  sink?: TraceSinkConfig;
172
178
  };
173
179
  type SmartAgentTracingConfig = TracingConfig;
@@ -372,10 +378,30 @@ type ResolvedTraceSink = {
372
378
  };
373
379
  type ResolvedTraceConfig = {
374
380
  enabled: boolean;
375
- mode: "batched";
381
+ mode: TracingMode;
376
382
  logData: boolean;
377
383
  sink: ResolvedTraceSink;
378
384
  };
385
+ type TraceSessionRuntime = {
386
+ sessionId: string;
387
+ startedAt: number;
388
+ resolvedConfig: ResolvedTraceConfig;
389
+ events: TraceEventRecord[];
390
+ summary: TraceSessionSummary;
391
+ status: TraceSessionStatus;
392
+ errors: TraceErrorRecord[];
393
+ fileBaseDir?: string;
394
+ fileSessionDir?: string;
395
+ /** For streaming mode: whether session start has been sent to sink */
396
+ sessionStarted?: boolean;
397
+ /** Agent info for streaming mode session start */
398
+ agentInfo?: {
399
+ name?: string;
400
+ version?: string;
401
+ model?: string;
402
+ provider?: string;
403
+ };
404
+ };
379
405
  type HandoffDescriptor<TParsed = any> = {
380
406
  type: "handoff";
381
407
  toolName: string;
@@ -478,6 +504,25 @@ type PlanEvent = {
478
504
  type SummarizationEvent = {
479
505
  type: "summarization";
480
506
  summary: string;
507
+ /** Number of messages that were compressed/summarized */
508
+ messagesCompressed?: number;
509
+ /** Input tokens used for summarization prompt (from model response if available, otherwise estimated) */
510
+ inputTokens?: number;
511
+ /** Output tokens from summarization response (from model response if available, otherwise estimated) */
512
+ outputTokens?: number;
513
+ /** Cached input tokens from model response (prompt cache hit) */
514
+ cachedInputTokens?: number;
515
+ /** Total tokens (input + output) */
516
+ totalTokens?: number;
517
+ /** Duration of the summarization call in milliseconds */
518
+ durationMs?: number;
519
+ /** Previous summary content (if incremental summarization) */
520
+ previousSummary?: string;
521
+ /** Total token count before summarization */
522
+ tokenCountBefore?: number;
523
+ /** Total token count after summarization */
524
+ tokenCountAfter?: number;
525
+ /** @deprecated Use messagesCompressed instead */
481
526
  archivedCount?: number;
482
527
  };
483
528
  type FinalAnswerEvent = {
@@ -853,5 +898,10 @@ declare function customSink(handler: CustomSinkArg): TraceSinkConfig;
853
898
  declare function cognipeerSink(apiKey: string): TraceSinkConfig;
854
899
  declare function cognipeerSink(url: string | undefined, apiKey: string): TraceSinkConfig;
855
900
  declare function httpSink(url: string, headers?: Record<string, string>): TraceSinkConfig;
901
+ /**
902
+ * Start a streaming trace session. Should be called before recording events in streaming mode.
903
+ * This sends the session start to the remote endpoint immediately.
904
+ */
905
+ declare function startStreamingSession(session: TraceSessionRuntime | undefined, agentRuntime?: AgentRuntimeConfig): Promise<void>;
856
906
 
857
- export { type AIMessage, type AgentInstance, type AgentInvokeResult, type AgentLimits, type AgentOptions, type AgentRuleOptions, type AgentRuntimeConfig, type AgentSnapshot, type AgentSnapshotMetadata, type AgentState, type BaseChatMessage, type BaseChatMessagePart, type BaseChatModel, type BaseMessage, type CodeDetectionRuleOptions, type CodeGuardrailOptions, type ConversationGuardrail, type CustomRuleCallback, type CustomRuleOptions, type GuardrailContext, type GuardrailDisposition, type GuardrailEvent, type GuardrailIncident, type GuardrailOutcome, GuardrailPhase, type GuardrailRule, type HandoffDescriptor, type InvokeConfig, type JsonGuardrailOptions, type JsonSchemaRuleOptions, type Message, type PendingToolApproval, type RegexGuardrailOptions, type RegexRuleOptions, type ResolvedTraceConfig, type ResolvedTraceSink, type RestoreSnapshotOptions, type SerializableSmartState, type SmartAgentEvent, type SmartAgentInstance, type SmartAgentLimits, type SmartAgentOptions, type SmartAgentTracingConfig, type SmartModel, type SmartState, type SmartToolFn, type SnapshotOptions, type SnapshotRuntimeHint, type TokenLimits, type ToolApprovalEvent, type ToolApprovalResolution, type ToolInterface, type TraceDataSection, type TraceErrorRecord, type TraceEventRecord, type TraceMessageSection, type TraceMetadataSection, type TraceSessionConfigSnapshot, type TraceSessionFile, type TraceSessionStatus, type TraceSessionSummary, type TraceSinkCognipeerConfig, type TraceSinkConfig, type TraceSinkCustomConfig, type TraceSinkFileConfig, type TraceSinkHttpConfig, type TraceSinkSnapshot, type TraceSummarySection, type TraceToolCallSection, type TraceToolResultSection, type TracingConfig, agentVerdictRule, applyTokenLimits, buildSystemPrompt, captureSnapshot, codePresenceRule, cognipeerSink, countApproxTokens, createAgent, createAgentNode, createCodeGuardrail, createContextSummarizeNode, createContextTools, createGuardrail, createJsonGuardrail, createRegexGuardrail, createResolverNode, createSmartAgent, createTool, createToolLimitFinalizeNode, createToolsNode, customCallbackRule, customSink, evaluateGuardrails, fileSink, fromLangchainModel, fromLangchainTools, httpSink, isSmartModel, jsonSchemaRule, regexRule, resolveToolApprovalState, restoreSnapshot, withTools };
907
+ export { type AIMessage, type AgentInstance, type AgentInvokeResult, type AgentLimits, type AgentOptions, type AgentRuleOptions, type AgentRuntimeConfig, type AgentSnapshot, type AgentSnapshotMetadata, type AgentState, type BaseChatMessage, type BaseChatMessagePart, type BaseChatModel, type BaseMessage, type CodeDetectionRuleOptions, type CodeGuardrailOptions, type ConversationGuardrail, type CustomRuleCallback, type CustomRuleOptions, type GuardrailContext, type GuardrailDisposition, type GuardrailEvent, type GuardrailIncident, type GuardrailOutcome, GuardrailPhase, type GuardrailRule, type HandoffDescriptor, type InvokeConfig, type JsonGuardrailOptions, type JsonSchemaRuleOptions, type Message, type PendingToolApproval, type RegexGuardrailOptions, type RegexRuleOptions, type ResolvedTraceConfig, type ResolvedTraceSink, type RestoreSnapshotOptions, type SerializableSmartState, type SmartAgentEvent, type SmartAgentInstance, type SmartAgentLimits, type SmartAgentOptions, type SmartAgentTracingConfig, type SmartModel, type SmartState, type SmartToolFn, type SnapshotOptions, type SnapshotRuntimeHint, type TokenLimits, type ToolApprovalEvent, type ToolApprovalResolution, type ToolInterface, type TraceDataSection, type TraceErrorRecord, type TraceEventRecord, type TraceMessageSection, type TraceMetadataSection, type TraceSessionConfigSnapshot, type TraceSessionFile, type TraceSessionStatus, type TraceSessionSummary, type TraceSinkCognipeerConfig, type TraceSinkConfig, type TraceSinkCustomConfig, type TraceSinkFileConfig, type TraceSinkHttpConfig, type TraceSinkSnapshot, type TraceSummarySection, type TraceToolCallSection, type TraceToolResultSection, type TracingConfig, type TracingMode, agentVerdictRule, applyTokenLimits, buildSystemPrompt, captureSnapshot, codePresenceRule, cognipeerSink, countApproxTokens, createAgent, createAgentNode, createCodeGuardrail, createContextSummarizeNode, createContextTools, createGuardrail, createJsonGuardrail, createRegexGuardrail, createResolverNode, createSmartAgent, createTool, createToolLimitFinalizeNode, createToolsNode, customCallbackRule, customSink, evaluateGuardrails, fileSink, fromLangchainModel, fromLangchainTools, httpSink, isSmartModel, jsonSchemaRule, regexRule, resolveToolApprovalState, restoreSnapshot, startStreamingSession, withTools };
package/dist/index.d.ts CHANGED
@@ -165,9 +165,15 @@ type TraceSinkHttpConfig = {
165
165
  headers?: Record<string, string>;
166
166
  };
167
167
  type TraceSinkConfig = TraceSinkFileConfig | TraceSinkCustomConfig | TraceSinkCognipeerConfig | TraceSinkHttpConfig;
168
+ type TracingMode = "batched" | "streaming";
168
169
  type TracingConfig = {
169
170
  enabled: boolean;
170
171
  logData?: boolean;
172
+ /** Tracing mode:
173
+ * - "batched" (default): Events are collected and sent together when session ends
174
+ * - "streaming": Session starts immediately, events are sent in real-time as they occur
175
+ */
176
+ mode?: TracingMode;
171
177
  sink?: TraceSinkConfig;
172
178
  };
173
179
  type SmartAgentTracingConfig = TracingConfig;
@@ -372,10 +378,30 @@ type ResolvedTraceSink = {
372
378
  };
373
379
  type ResolvedTraceConfig = {
374
380
  enabled: boolean;
375
- mode: "batched";
381
+ mode: TracingMode;
376
382
  logData: boolean;
377
383
  sink: ResolvedTraceSink;
378
384
  };
385
+ type TraceSessionRuntime = {
386
+ sessionId: string;
387
+ startedAt: number;
388
+ resolvedConfig: ResolvedTraceConfig;
389
+ events: TraceEventRecord[];
390
+ summary: TraceSessionSummary;
391
+ status: TraceSessionStatus;
392
+ errors: TraceErrorRecord[];
393
+ fileBaseDir?: string;
394
+ fileSessionDir?: string;
395
+ /** For streaming mode: whether session start has been sent to sink */
396
+ sessionStarted?: boolean;
397
+ /** Agent info for streaming mode session start */
398
+ agentInfo?: {
399
+ name?: string;
400
+ version?: string;
401
+ model?: string;
402
+ provider?: string;
403
+ };
404
+ };
379
405
  type HandoffDescriptor<TParsed = any> = {
380
406
  type: "handoff";
381
407
  toolName: string;
@@ -478,6 +504,25 @@ type PlanEvent = {
478
504
  type SummarizationEvent = {
479
505
  type: "summarization";
480
506
  summary: string;
507
+ /** Number of messages that were compressed/summarized */
508
+ messagesCompressed?: number;
509
+ /** Input tokens used for summarization prompt (from model response if available, otherwise estimated) */
510
+ inputTokens?: number;
511
+ /** Output tokens from summarization response (from model response if available, otherwise estimated) */
512
+ outputTokens?: number;
513
+ /** Cached input tokens from model response (prompt cache hit) */
514
+ cachedInputTokens?: number;
515
+ /** Total tokens (input + output) */
516
+ totalTokens?: number;
517
+ /** Duration of the summarization call in milliseconds */
518
+ durationMs?: number;
519
+ /** Previous summary content (if incremental summarization) */
520
+ previousSummary?: string;
521
+ /** Total token count before summarization */
522
+ tokenCountBefore?: number;
523
+ /** Total token count after summarization */
524
+ tokenCountAfter?: number;
525
+ /** @deprecated Use messagesCompressed instead */
481
526
  archivedCount?: number;
482
527
  };
483
528
  type FinalAnswerEvent = {
@@ -853,5 +898,10 @@ declare function customSink(handler: CustomSinkArg): TraceSinkConfig;
853
898
  declare function cognipeerSink(apiKey: string): TraceSinkConfig;
854
899
  declare function cognipeerSink(url: string | undefined, apiKey: string): TraceSinkConfig;
855
900
  declare function httpSink(url: string, headers?: Record<string, string>): TraceSinkConfig;
901
+ /**
902
+ * Start a streaming trace session. Should be called before recording events in streaming mode.
903
+ * This sends the session start to the remote endpoint immediately.
904
+ */
905
+ declare function startStreamingSession(session: TraceSessionRuntime | undefined, agentRuntime?: AgentRuntimeConfig): Promise<void>;
856
906
 
857
- export { type AIMessage, type AgentInstance, type AgentInvokeResult, type AgentLimits, type AgentOptions, type AgentRuleOptions, type AgentRuntimeConfig, type AgentSnapshot, type AgentSnapshotMetadata, type AgentState, type BaseChatMessage, type BaseChatMessagePart, type BaseChatModel, type BaseMessage, type CodeDetectionRuleOptions, type CodeGuardrailOptions, type ConversationGuardrail, type CustomRuleCallback, type CustomRuleOptions, type GuardrailContext, type GuardrailDisposition, type GuardrailEvent, type GuardrailIncident, type GuardrailOutcome, GuardrailPhase, type GuardrailRule, type HandoffDescriptor, type InvokeConfig, type JsonGuardrailOptions, type JsonSchemaRuleOptions, type Message, type PendingToolApproval, type RegexGuardrailOptions, type RegexRuleOptions, type ResolvedTraceConfig, type ResolvedTraceSink, type RestoreSnapshotOptions, type SerializableSmartState, type SmartAgentEvent, type SmartAgentInstance, type SmartAgentLimits, type SmartAgentOptions, type SmartAgentTracingConfig, type SmartModel, type SmartState, type SmartToolFn, type SnapshotOptions, type SnapshotRuntimeHint, type TokenLimits, type ToolApprovalEvent, type ToolApprovalResolution, type ToolInterface, type TraceDataSection, type TraceErrorRecord, type TraceEventRecord, type TraceMessageSection, type TraceMetadataSection, type TraceSessionConfigSnapshot, type TraceSessionFile, type TraceSessionStatus, type TraceSessionSummary, type TraceSinkCognipeerConfig, type TraceSinkConfig, type TraceSinkCustomConfig, type TraceSinkFileConfig, type TraceSinkHttpConfig, type TraceSinkSnapshot, type TraceSummarySection, type TraceToolCallSection, type TraceToolResultSection, type TracingConfig, agentVerdictRule, applyTokenLimits, buildSystemPrompt, captureSnapshot, codePresenceRule, cognipeerSink, countApproxTokens, createAgent, createAgentNode, createCodeGuardrail, createContextSummarizeNode, createContextTools, createGuardrail, createJsonGuardrail, createRegexGuardrail, createResolverNode, createSmartAgent, createTool, createToolLimitFinalizeNode, createToolsNode, customCallbackRule, customSink, evaluateGuardrails, fileSink, fromLangchainModel, fromLangchainTools, httpSink, isSmartModel, jsonSchemaRule, regexRule, resolveToolApprovalState, restoreSnapshot, withTools };
907
+ export { type AIMessage, type AgentInstance, type AgentInvokeResult, type AgentLimits, type AgentOptions, type AgentRuleOptions, type AgentRuntimeConfig, type AgentSnapshot, type AgentSnapshotMetadata, type AgentState, type BaseChatMessage, type BaseChatMessagePart, type BaseChatModel, type BaseMessage, type CodeDetectionRuleOptions, type CodeGuardrailOptions, type ConversationGuardrail, type CustomRuleCallback, type CustomRuleOptions, type GuardrailContext, type GuardrailDisposition, type GuardrailEvent, type GuardrailIncident, type GuardrailOutcome, GuardrailPhase, type GuardrailRule, type HandoffDescriptor, type InvokeConfig, type JsonGuardrailOptions, type JsonSchemaRuleOptions, type Message, type PendingToolApproval, type RegexGuardrailOptions, type RegexRuleOptions, type ResolvedTraceConfig, type ResolvedTraceSink, type RestoreSnapshotOptions, type SerializableSmartState, type SmartAgentEvent, type SmartAgentInstance, type SmartAgentLimits, type SmartAgentOptions, type SmartAgentTracingConfig, type SmartModel, type SmartState, type SmartToolFn, type SnapshotOptions, type SnapshotRuntimeHint, type TokenLimits, type ToolApprovalEvent, type ToolApprovalResolution, type ToolInterface, type TraceDataSection, type TraceErrorRecord, type TraceEventRecord, type TraceMessageSection, type TraceMetadataSection, type TraceSessionConfigSnapshot, type TraceSessionFile, type TraceSessionStatus, type TraceSessionSummary, type TraceSinkCognipeerConfig, type TraceSinkConfig, type TraceSinkCustomConfig, type TraceSinkFileConfig, type TraceSinkHttpConfig, type TraceSinkSnapshot, type TraceSummarySection, type TraceToolCallSection, type TraceToolResultSection, type TracingConfig, type TracingMode, agentVerdictRule, applyTokenLimits, buildSystemPrompt, captureSnapshot, codePresenceRule, cognipeerSink, countApproxTokens, createAgent, createAgentNode, createCodeGuardrail, createContextSummarizeNode, createContextTools, createGuardrail, createJsonGuardrail, createRegexGuardrail, createResolverNode, createSmartAgent, createTool, createToolLimitFinalizeNode, createToolsNode, customCallbackRule, customSink, evaluateGuardrails, fileSink, fromLangchainModel, fromLangchainTools, httpSink, isSmartModel, jsonSchemaRule, regexRule, resolveToolApprovalState, restoreSnapshot, startStreamingSession, withTools };
package/dist/index.mjs CHANGED
@@ -550,6 +550,7 @@ function ensureDir(p) {
550
550
  function withDefaults(config) {
551
551
  const enabled = Boolean(config?.enabled);
552
552
  const logData = config?.logData ?? DEFAULT_TRACE_CONFIG.logData;
553
+ const mode = config?.mode === "streaming" ? "streaming" : "batched";
553
554
  let sink;
554
555
  try {
555
556
  sink = resolveSink(config?.sink);
@@ -562,7 +563,7 @@ function withDefaults(config) {
562
563
  return {
563
564
  enabled,
564
565
  logData,
565
- mode: "batched",
566
+ mode,
566
567
  sink
567
568
  };
568
569
  }
@@ -661,6 +662,108 @@ async function postTraceSession(url, headers, payload) {
661
662
  throw err;
662
663
  }
663
664
  }
665
+ async function postStreamingSessionStart(baseUrl, headers, payload) {
666
+ const fetchFn = typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : void 0;
667
+ if (!fetchFn) {
668
+ throw new Error("HTTP sink requires fetch to be available in this runtime.");
669
+ }
670
+ const url = `${baseUrl.replace(/\/$/, "")}/${payload.sessionId}/start`;
671
+ const finalHeaders = { ...headers || {} };
672
+ if (!Object.keys(finalHeaders).some((key) => key.toLowerCase() === "content-type")) {
673
+ finalHeaders["content-type"] = "application/json";
674
+ }
675
+ try {
676
+ const response = await fetchFn(url, {
677
+ method: "POST",
678
+ headers: finalHeaders,
679
+ body: JSON.stringify(payload)
680
+ });
681
+ if (!response.ok) {
682
+ let responseText = "";
683
+ try {
684
+ responseText = await response.text();
685
+ } catch {
686
+ }
687
+ const statusLine = `HTTP ${response.status}${response.statusText ? ` ${response.statusText}` : ""}`;
688
+ const bodyPreview = responseText ? ` - ${responseText.slice(0, 200)}` : "";
689
+ throw new Error(`${statusLine}${bodyPreview}`);
690
+ }
691
+ } catch (err) {
692
+ const errMsg = err instanceof Error ? err.message : String(err);
693
+ if (typeof console !== "undefined" && typeof console.error === "function") {
694
+ console.error("[Tracing] Failed to post session start:", errMsg);
695
+ }
696
+ throw err;
697
+ }
698
+ }
699
+ async function postStreamingEvent(baseUrl, headers, sessionId, event) {
700
+ const fetchFn = typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : void 0;
701
+ if (!fetchFn) {
702
+ throw new Error("HTTP sink requires fetch to be available in this runtime.");
703
+ }
704
+ const url = `${baseUrl.replace(/\/$/, "")}/${sessionId}/events`;
705
+ const finalHeaders = { ...headers || {} };
706
+ if (!Object.keys(finalHeaders).some((key) => key.toLowerCase() === "content-type")) {
707
+ finalHeaders["content-type"] = "application/json";
708
+ }
709
+ try {
710
+ const response = await fetchFn(url, {
711
+ method: "POST",
712
+ headers: finalHeaders,
713
+ body: JSON.stringify({ event })
714
+ });
715
+ if (!response.ok) {
716
+ let responseText = "";
717
+ try {
718
+ responseText = await response.text();
719
+ } catch {
720
+ }
721
+ const statusLine = `HTTP ${response.status}${response.statusText ? ` ${response.statusText}` : ""}`;
722
+ const bodyPreview = responseText ? ` - ${responseText.slice(0, 200)}` : "";
723
+ throw new Error(`${statusLine}${bodyPreview}`);
724
+ }
725
+ } catch (err) {
726
+ const errMsg = err instanceof Error ? err.message : String(err);
727
+ if (typeof console !== "undefined" && typeof console.error === "function") {
728
+ console.error("[Tracing] Failed to post streaming event:", errMsg);
729
+ }
730
+ throw err;
731
+ }
732
+ }
733
+ async function postStreamingSessionEnd(baseUrl, headers, payload) {
734
+ const fetchFn = typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : void 0;
735
+ if (!fetchFn) {
736
+ throw new Error("HTTP sink requires fetch to be available in this runtime.");
737
+ }
738
+ const url = `${baseUrl.replace(/\/$/, "")}/${payload.sessionId}/end`;
739
+ const finalHeaders = { ...headers || {} };
740
+ if (!Object.keys(finalHeaders).some((key) => key.toLowerCase() === "content-type")) {
741
+ finalHeaders["content-type"] = "application/json";
742
+ }
743
+ try {
744
+ const response = await fetchFn(url, {
745
+ method: "POST",
746
+ headers: finalHeaders,
747
+ body: JSON.stringify(payload)
748
+ });
749
+ if (!response.ok) {
750
+ let responseText = "";
751
+ try {
752
+ responseText = await response.text();
753
+ } catch {
754
+ }
755
+ const statusLine = `HTTP ${response.status}${response.statusText ? ` ${response.statusText}` : ""}`;
756
+ const bodyPreview = responseText ? ` - ${responseText.slice(0, 200)}` : "";
757
+ throw new Error(`${statusLine}${bodyPreview}`);
758
+ }
759
+ } catch (err) {
760
+ const errMsg = err instanceof Error ? err.message : String(err);
761
+ if (typeof console !== "undefined" && typeof console.error === "function") {
762
+ console.error("[Tracing] Failed to post session end:", errMsg);
763
+ }
764
+ throw err;
765
+ }
766
+ }
664
767
  function createEmptySummary() {
665
768
  return {
666
769
  totalDurationMs: 0,
@@ -837,7 +940,8 @@ function createTraceSession(opts) {
837
940
  events: [],
838
941
  summary: createEmptySummary(),
839
942
  status: "in_progress",
840
- errors: []
943
+ errors: [],
944
+ sessionStarted: false
841
945
  };
842
946
  if (cfg.sink.type === "file") {
843
947
  const baseDir = cfg.sink.baseDir;
@@ -849,6 +953,49 @@ function createTraceSession(opts) {
849
953
  }
850
954
  return runtime;
851
955
  }
956
+ async function startStreamingSession(session, agentRuntime) {
957
+ if (!session) return;
958
+ if (session.resolvedConfig.mode !== "streaming") return;
959
+ if (session.sessionStarted) return;
960
+ const sink = session.resolvedConfig.sink;
961
+ const agentInfo = agentRuntime ? {
962
+ name: agentRuntime.name || "unknown-agent",
963
+ version: agentRuntime.version,
964
+ model: getModelName(agentRuntime.model) || "unknown-model",
965
+ provider: getProviderName(agentRuntime.model)
966
+ } : void 0;
967
+ session.agentInfo = agentInfo;
968
+ const configSnapshot = buildConfigSnapshot(session);
969
+ const startedAtIso = new Date(session.startedAt).toISOString();
970
+ const payload = {
971
+ sessionId: session.sessionId,
972
+ startedAt: startedAtIso,
973
+ agent: agentInfo,
974
+ config: configSnapshot
975
+ };
976
+ if (sink.type === "cognipeer" || sink.type === "http") {
977
+ try {
978
+ const headers = sink.type === "cognipeer" ? { Authorization: `Bearer ${sink.apiKey}` } : sink.headers;
979
+ await postStreamingSessionStart(sink.url, headers, payload);
980
+ session.sessionStarted = true;
981
+ } catch (err) {
982
+ const message = err instanceof Error ? err.message : String(err);
983
+ session.errors.push({
984
+ eventId: "session_start",
985
+ message,
986
+ type: "sink",
987
+ timestamp: startedAtIso
988
+ });
989
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
990
+ console.warn("[Tracing] Failed to start streaming session, falling back to batched:", message);
991
+ }
992
+ }
993
+ } else if (sink.type === "custom" && typeof sink.onEvent === "function") {
994
+ session.sessionStarted = true;
995
+ } else if (sink.type === "file") {
996
+ session.sessionStarted = true;
997
+ }
998
+ }
852
999
  function recordTraceEvent(session, event) {
853
1000
  if (!session) return void 0;
854
1001
  const sequence = session.events.length + 1;
@@ -951,6 +1098,23 @@ function recordTraceEvent(session, event) {
951
1098
  }
952
1099
  }
953
1100
  }
1101
+ if (session.resolvedConfig.mode === "streaming" && session.sessionStarted) {
1102
+ if (sink.type === "cognipeer" || sink.type === "http") {
1103
+ const headers = sink.type === "cognipeer" ? { Authorization: `Bearer ${sink.apiKey}` } : sink.headers;
1104
+ postStreamingEvent(sink.url, headers, session.sessionId, record).catch((err) => {
1105
+ const errMsg = err instanceof Error ? err.message : String(err);
1106
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
1107
+ console.warn("[Tracing] Failed to send streaming event:", errMsg);
1108
+ }
1109
+ session.errors.push({
1110
+ eventId: record.id,
1111
+ message: errMsg,
1112
+ type: "sink",
1113
+ timestamp: timestampIso
1114
+ });
1115
+ });
1116
+ }
1117
+ }
954
1118
  return record;
955
1119
  }
956
1120
  async function finalizeTraceSession(session, params = {}) {
@@ -985,7 +1149,7 @@ async function finalizeTraceSession(session, params = {}) {
985
1149
  startedAt: startedAtIso,
986
1150
  endedAt: endedAtIso,
987
1151
  durationMs,
988
- agent: agentInfo,
1152
+ agent: agentInfo || session.agentInfo,
989
1153
  config: configSnapshot,
990
1154
  summary: session.summary,
991
1155
  events: session.events,
@@ -995,10 +1159,22 @@ async function finalizeTraceSession(session, params = {}) {
995
1159
  const payloadForSink = buildPayload(initialStatus);
996
1160
  let sinkFailed = false;
997
1161
  const sink = session.resolvedConfig.sink;
1162
+ const isStreamingMode = session.resolvedConfig.mode === "streaming";
998
1163
  if (sink.type === "cognipeer" || sink.type === "http") {
999
1164
  try {
1000
1165
  const headers = sink.type === "cognipeer" ? { Authorization: `Bearer ${sink.apiKey}` } : sink.headers;
1001
- await postTraceSession(sink.url, headers, payloadForSink);
1166
+ if (isStreamingMode && session.sessionStarted) {
1167
+ await postStreamingSessionEnd(sink.url, headers, {
1168
+ sessionId: session.sessionId,
1169
+ endedAt: endedAtIso,
1170
+ durationMs,
1171
+ status: initialStatus,
1172
+ summary: session.summary,
1173
+ errors: session.errors
1174
+ });
1175
+ } else {
1176
+ await postTraceSession(sink.url, headers, payloadForSink);
1177
+ }
1002
1178
  } catch (err) {
1003
1179
  sinkFailed = true;
1004
1180
  const message = err instanceof Error ? err.message : String(err);
@@ -1211,7 +1387,95 @@ function createAgentCoreNode(opts) {
1211
1387
  };
1212
1388
  let response;
1213
1389
  try {
1214
- const normalizedMessages = normalizeBedrockToolPairing([...state.messages]);
1390
+ const ensureUniqueToolCallIds = (input) => {
1391
+ const msgs = Array.isArray(input) ? input.map((m) => ({ ...m })) : [];
1392
+ const usedIds = /* @__PURE__ */ new Set();
1393
+ let counter = 0;
1394
+ for (let i = 0; i < msgs.length; i++) {
1395
+ const m = msgs[i];
1396
+ if (m?.role !== "assistant") continue;
1397
+ const hasToolCalls = Array.isArray(m.tool_calls) && m.tool_calls.length > 0;
1398
+ const contentArr = Array.isArray(m.content) ? m.content : [];
1399
+ const hasContentToolUse = contentArr.some((c) => c?.type === "tool_use" && c?.id);
1400
+ const hasAdditionalToolCalls = Array.isArray(m.additional_kwargs?.tool_calls) && m.additional_kwargs.tool_calls.length > 0;
1401
+ if (!hasToolCalls && !hasContentToolUse && !hasAdditionalToolCalls) continue;
1402
+ if (hasToolCalls) {
1403
+ m.tool_calls = m.tool_calls.map((tc) => ({ ...tc }));
1404
+ }
1405
+ if (hasContentToolUse) {
1406
+ m.content = contentArr.map(
1407
+ (c) => c && typeof c === "object" ? { ...c } : c
1408
+ );
1409
+ }
1410
+ if (hasAdditionalToolCalls) {
1411
+ m.additional_kwargs = { ...m.additional_kwargs };
1412
+ m.additional_kwargs.tool_calls = m.additional_kwargs.tool_calls.map((tc) => ({ ...tc }));
1413
+ }
1414
+ const idsInMessage = /* @__PURE__ */ new Set();
1415
+ if (hasToolCalls) {
1416
+ for (const tc of m.tool_calls) {
1417
+ if (tc?.id) idsInMessage.add(tc.id);
1418
+ }
1419
+ }
1420
+ if (hasContentToolUse) {
1421
+ for (const c of m.content) {
1422
+ if (c?.type === "tool_use" && c?.id) idsInMessage.add(c.id);
1423
+ }
1424
+ }
1425
+ if (hasAdditionalToolCalls) {
1426
+ for (const tc of m.additional_kwargs.tool_calls) {
1427
+ if (tc?.id) idsInMessage.add(tc.id);
1428
+ }
1429
+ }
1430
+ for (const id of idsInMessage) {
1431
+ if (!usedIds.has(id)) {
1432
+ usedIds.add(id);
1433
+ continue;
1434
+ }
1435
+ const newId = `${id}_${Date.now()}_${counter++}`;
1436
+ if (hasToolCalls) {
1437
+ for (const tc of m.tool_calls) {
1438
+ if (tc.id === id) tc.id = newId;
1439
+ }
1440
+ }
1441
+ if (hasContentToolUse && Array.isArray(m.content)) {
1442
+ for (const c of m.content) {
1443
+ if (c?.type === "tool_use" && c.id === id) c.id = newId;
1444
+ }
1445
+ }
1446
+ if (hasAdditionalToolCalls) {
1447
+ for (const tc of m.additional_kwargs.tool_calls) {
1448
+ if (tc.id === id) tc.id = newId;
1449
+ }
1450
+ }
1451
+ for (let j = i + 1; j < msgs.length; j++) {
1452
+ const next = msgs[j];
1453
+ if (next?.role === "assistant") break;
1454
+ if (next?.role === "tool" && next.tool_call_id === id) {
1455
+ next.tool_call_id = newId;
1456
+ }
1457
+ if (Array.isArray(next?.content)) {
1458
+ let contentCloned = false;
1459
+ for (let k = 0; k < next.content.length; k++) {
1460
+ const block = next.content[k];
1461
+ if (block?.type === "tool_result" && block.tool_use_id === id) {
1462
+ if (!contentCloned) {
1463
+ next.content = next.content.map((x) => x && typeof x === "object" ? { ...x } : x);
1464
+ contentCloned = true;
1465
+ }
1466
+ next.content[k].tool_use_id = newId;
1467
+ }
1468
+ }
1469
+ }
1470
+ }
1471
+ usedIds.add(newId);
1472
+ }
1473
+ }
1474
+ return msgs;
1475
+ };
1476
+ const normalizedMessages = ensureUniqueToolCallIds(
1477
+ normalizeBedrockToolPairing([...state.messages])
1478
+ );
1215
1479
  debugToolPairing(normalizedMessages);
1216
1480
  if (streamingEnabled && typeof modelWithTools.stream === "function") {
1217
1481
  let streamedText = "";
@@ -2458,10 +2722,14 @@ function createContextSummarizeNode(opts) {
2458
2722
  if (!config.enabled) return {};
2459
2723
  const model = opts.model;
2460
2724
  const messages = state.messages || [];
2725
+ const onEvent = state.ctx?.__onEvent;
2726
+ const traceSession = state.ctx?.__traceSession;
2461
2727
  const compressableMessages = messages.filter((m) => m.role === "tool" && m.content !== "SUMMARIZED");
2462
2728
  if (compressableMessages.length === 0) {
2463
2729
  return {};
2464
2730
  }
2731
+ const allTextBefore = messages.map((m) => typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.map((c) => typeof c === "string" ? c : c?.text ?? c?.content ?? "").join("") : "").join("\n");
2732
+ const tokenCountBefore = countApproxTokens(allTextBefore);
2465
2733
  const summaryPromptMaxTokens = config.summaryPromptMaxTokens;
2466
2734
  const availableTokensForConversation = summaryPromptMaxTokens - 500;
2467
2735
  const messageTexts = [];
@@ -2510,12 +2778,54 @@ Summary:`;
2510
2778
  systemMessage("You are a helpful assistant that summarizes conversation history efficiently."),
2511
2779
  humanMessage(promptBody)
2512
2780
  ];
2781
+ const inputTokensEstimate = countApproxTokens(summaryPrompt.map((m) => m.content).join("\n"));
2513
2782
  let summaryText = "Summary unavailable.";
2783
+ let outputTokensEstimate = 0;
2784
+ let durationMs = 0;
2785
+ let inputTokensActual;
2786
+ let outputTokensActual;
2787
+ let cachedInputTokens;
2788
+ let totalTokensActual;
2789
+ const startTime = Date.now();
2514
2790
  try {
2515
2791
  const response = await model.invoke(summaryPrompt);
2792
+ durationMs = Date.now() - startTime;
2516
2793
  summaryText = typeof response.content === "string" ? response.content : Array.isArray(response.content) ? response.content.map((c) => c.text || "").join("") : JSON.stringify(response.content);
2794
+ outputTokensEstimate = countApproxTokens(summaryText);
2795
+ const rawUsage = response?.usage || response?.response_metadata?.token_usage || response?.response_metadata?.tokenUsage || response?.response_metadata?.usage;
2796
+ const normalized = normalizeUsage(rawUsage);
2797
+ if (normalized) {
2798
+ inputTokensActual = normalized.prompt_tokens;
2799
+ outputTokensActual = normalized.completion_tokens;
2800
+ totalTokensActual = normalized.total_tokens;
2801
+ cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
2802
+ }
2517
2803
  } catch (err) {
2804
+ durationMs = Date.now() - startTime;
2518
2805
  console.error("[ContextSummarize] Failed to generate summary:", err);
2806
+ const errorEvent = {
2807
+ type: "summarization",
2808
+ summary: "",
2809
+ messagesCompressed: 0,
2810
+ inputTokens: inputTokensEstimate,
2811
+ outputTokens: 0,
2812
+ durationMs,
2813
+ previousSummary: previousSummary || void 0,
2814
+ tokenCountBefore,
2815
+ tokenCountAfter: tokenCountBefore
2816
+ };
2817
+ onEvent?.(errorEvent);
2818
+ recordTraceEvent(traceSession, {
2819
+ type: "summarization",
2820
+ label: "Context Summarization Failed",
2821
+ actor: { scope: "system", name: "summarizer", role: "summarization" },
2822
+ status: "error",
2823
+ durationMs,
2824
+ inputTokens: inputTokensEstimate,
2825
+ outputTokens: 0,
2826
+ error: { message: err?.message || String(err), stack: err?.stack },
2827
+ messageList: sanitizeTracePayload(summaryPrompt)
2828
+ });
2519
2829
  return {};
2520
2830
  }
2521
2831
  const toolCallIdToAssistantIdx = /* @__PURE__ */ new Map();
@@ -2567,8 +2877,51 @@ Summary:`;
2567
2877
  content: summaryText
2568
2878
  };
2569
2879
  const summaries = Array.isArray(state.summaries) ? [...state.summaries, summaryText] : [summaryText];
2880
+ const finalMessages = [...validatedMessages, assistantSummaryCall, toolSummaryResponse];
2881
+ const allTextAfter = finalMessages.map((m) => typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.map((c) => typeof c === "string" ? c : c?.text ?? c?.content ?? "").join("") : "").join("\n");
2882
+ const tokenCountAfter = countApproxTokens(allTextAfter);
2883
+ const summarizationEvent = {
2884
+ type: "summarization",
2885
+ summary: summaryText,
2886
+ messagesCompressed: compressableMessages.length,
2887
+ inputTokens: inputTokensActual ?? inputTokensEstimate,
2888
+ outputTokens: outputTokensActual ?? outputTokensEstimate,
2889
+ cachedInputTokens,
2890
+ totalTokens: totalTokensActual ?? (inputTokensActual ?? inputTokensEstimate) + (outputTokensActual ?? outputTokensEstimate),
2891
+ durationMs,
2892
+ previousSummary: previousSummary || void 0,
2893
+ tokenCountBefore,
2894
+ tokenCountAfter,
2895
+ archivedCount: compressableMessages.length
2896
+ // deprecated field for backward compatibility
2897
+ };
2898
+ onEvent?.(summarizationEvent);
2899
+ recordTraceEvent(traceSession, {
2900
+ type: "summarization",
2901
+ label: "Context Summarization",
2902
+ actor: { scope: "system", name: "summarizer", role: "summarization" },
2903
+ status: "success",
2904
+ durationMs,
2905
+ inputTokens: inputTokensActual ?? inputTokensEstimate,
2906
+ outputTokens: outputTokensActual ?? outputTokensEstimate,
2907
+ cachedInputTokens,
2908
+ totalTokens: totalTokensActual,
2909
+ messageList: sanitizeTracePayload([
2910
+ ...summaryPrompt,
2911
+ { role: "assistant", content: summaryText, name: "summarization_result" }
2912
+ ]),
2913
+ debug: {
2914
+ messagesCompressed: compressableMessages.length,
2915
+ tokenCountBefore,
2916
+ tokenCountAfter,
2917
+ tokensSaved: tokenCountBefore - tokenCountAfter,
2918
+ previousSummaryLength: previousSummary?.length || 0,
2919
+ usageFromModel: inputTokensActual !== void 0,
2920
+ cachedInputTokens
2921
+ }
2922
+ });
2570
2923
  return {
2571
- messages: [...validatedMessages, assistantSummaryCall, toolSummaryResponse],
2924
+ messages: finalMessages,
2572
2925
  summaries
2573
2926
  };
2574
2927
  };
@@ -3095,6 +3448,6 @@ function createCodeGuardrail(options) {
3095
3448
  });
3096
3449
  }
3097
3450
 
3098
- export { GuardrailPhase, agentVerdictRule, applyTokenLimits, buildSystemPrompt, captureSnapshot, codePresenceRule, cognipeerSink, countApproxTokens, createAgent, createAgentNode, createCodeGuardrail, createContextSummarizeNode, createContextTools, createGuardrail, createJsonGuardrail, createRegexGuardrail, createResolverNode, createSmartAgent, createTool, createToolLimitFinalizeNode, createToolsNode, customCallbackRule, customSink, evaluateGuardrails, fileSink, fromLangchainModel, fromLangchainTools, httpSink, isSmartModel, jsonSchemaRule, regexRule, resolveToolApprovalState, restoreSnapshot, withTools };
3451
+ export { GuardrailPhase, agentVerdictRule, applyTokenLimits, buildSystemPrompt, captureSnapshot, codePresenceRule, cognipeerSink, countApproxTokens, createAgent, createAgentNode, createCodeGuardrail, createContextSummarizeNode, createContextTools, createGuardrail, createJsonGuardrail, createRegexGuardrail, createResolverNode, createSmartAgent, createTool, createToolLimitFinalizeNode, createToolsNode, customCallbackRule, customSink, evaluateGuardrails, fileSink, fromLangchainModel, fromLangchainTools, httpSink, isSmartModel, jsonSchemaRule, regexRule, resolveToolApprovalState, restoreSnapshot, startStreamingSession, withTools };
3099
3452
  //# sourceMappingURL=index.mjs.map
3100
3453
  //# sourceMappingURL=index.mjs.map