@cognipeer/agent-sdk 0.2.3 → 0.2.4
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.cjs +271 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +52 -2
- package/dist/index.d.ts +52 -2
- package/dist/index.mjs +271 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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:
|
|
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:
|
|
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
|
|
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
|
-
|
|
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);
|
|
@@ -2458,10 +2634,14 @@ function createContextSummarizeNode(opts) {
|
|
|
2458
2634
|
if (!config.enabled) return {};
|
|
2459
2635
|
const model = opts.model;
|
|
2460
2636
|
const messages = state.messages || [];
|
|
2637
|
+
const onEvent = state.ctx?.__onEvent;
|
|
2638
|
+
const traceSession = state.ctx?.__traceSession;
|
|
2461
2639
|
const compressableMessages = messages.filter((m) => m.role === "tool" && m.content !== "SUMMARIZED");
|
|
2462
2640
|
if (compressableMessages.length === 0) {
|
|
2463
2641
|
return {};
|
|
2464
2642
|
}
|
|
2643
|
+
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");
|
|
2644
|
+
const tokenCountBefore = countApproxTokens(allTextBefore);
|
|
2465
2645
|
const summaryPromptMaxTokens = config.summaryPromptMaxTokens;
|
|
2466
2646
|
const availableTokensForConversation = summaryPromptMaxTokens - 500;
|
|
2467
2647
|
const messageTexts = [];
|
|
@@ -2510,12 +2690,54 @@ Summary:`;
|
|
|
2510
2690
|
systemMessage("You are a helpful assistant that summarizes conversation history efficiently."),
|
|
2511
2691
|
humanMessage(promptBody)
|
|
2512
2692
|
];
|
|
2693
|
+
const inputTokensEstimate = countApproxTokens(summaryPrompt.map((m) => m.content).join("\n"));
|
|
2513
2694
|
let summaryText = "Summary unavailable.";
|
|
2695
|
+
let outputTokensEstimate = 0;
|
|
2696
|
+
let durationMs = 0;
|
|
2697
|
+
let inputTokensActual;
|
|
2698
|
+
let outputTokensActual;
|
|
2699
|
+
let cachedInputTokens;
|
|
2700
|
+
let totalTokensActual;
|
|
2701
|
+
const startTime = Date.now();
|
|
2514
2702
|
try {
|
|
2515
2703
|
const response = await model.invoke(summaryPrompt);
|
|
2704
|
+
durationMs = Date.now() - startTime;
|
|
2516
2705
|
summaryText = typeof response.content === "string" ? response.content : Array.isArray(response.content) ? response.content.map((c) => c.text || "").join("") : JSON.stringify(response.content);
|
|
2706
|
+
outputTokensEstimate = countApproxTokens(summaryText);
|
|
2707
|
+
const rawUsage = response?.usage || response?.response_metadata?.token_usage || response?.response_metadata?.tokenUsage || response?.response_metadata?.usage;
|
|
2708
|
+
const normalized = normalizeUsage(rawUsage);
|
|
2709
|
+
if (normalized) {
|
|
2710
|
+
inputTokensActual = normalized.prompt_tokens;
|
|
2711
|
+
outputTokensActual = normalized.completion_tokens;
|
|
2712
|
+
totalTokensActual = normalized.total_tokens;
|
|
2713
|
+
cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
|
|
2714
|
+
}
|
|
2517
2715
|
} catch (err) {
|
|
2716
|
+
durationMs = Date.now() - startTime;
|
|
2518
2717
|
console.error("[ContextSummarize] Failed to generate summary:", err);
|
|
2718
|
+
const errorEvent = {
|
|
2719
|
+
type: "summarization",
|
|
2720
|
+
summary: "",
|
|
2721
|
+
messagesCompressed: 0,
|
|
2722
|
+
inputTokens: inputTokensEstimate,
|
|
2723
|
+
outputTokens: 0,
|
|
2724
|
+
durationMs,
|
|
2725
|
+
previousSummary: previousSummary || void 0,
|
|
2726
|
+
tokenCountBefore,
|
|
2727
|
+
tokenCountAfter: tokenCountBefore
|
|
2728
|
+
};
|
|
2729
|
+
onEvent?.(errorEvent);
|
|
2730
|
+
recordTraceEvent(traceSession, {
|
|
2731
|
+
type: "summarization",
|
|
2732
|
+
label: "Context Summarization Failed",
|
|
2733
|
+
actor: { scope: "system", name: "summarizer", role: "summarization" },
|
|
2734
|
+
status: "error",
|
|
2735
|
+
durationMs,
|
|
2736
|
+
inputTokens: inputTokensEstimate,
|
|
2737
|
+
outputTokens: 0,
|
|
2738
|
+
error: { message: err?.message || String(err), stack: err?.stack },
|
|
2739
|
+
messageList: sanitizeTracePayload(summaryPrompt)
|
|
2740
|
+
});
|
|
2519
2741
|
return {};
|
|
2520
2742
|
}
|
|
2521
2743
|
const toolCallIdToAssistantIdx = /* @__PURE__ */ new Map();
|
|
@@ -2567,8 +2789,51 @@ Summary:`;
|
|
|
2567
2789
|
content: summaryText
|
|
2568
2790
|
};
|
|
2569
2791
|
const summaries = Array.isArray(state.summaries) ? [...state.summaries, summaryText] : [summaryText];
|
|
2792
|
+
const finalMessages = [...validatedMessages, assistantSummaryCall, toolSummaryResponse];
|
|
2793
|
+
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");
|
|
2794
|
+
const tokenCountAfter = countApproxTokens(allTextAfter);
|
|
2795
|
+
const summarizationEvent = {
|
|
2796
|
+
type: "summarization",
|
|
2797
|
+
summary: summaryText,
|
|
2798
|
+
messagesCompressed: compressableMessages.length,
|
|
2799
|
+
inputTokens: inputTokensActual ?? inputTokensEstimate,
|
|
2800
|
+
outputTokens: outputTokensActual ?? outputTokensEstimate,
|
|
2801
|
+
cachedInputTokens,
|
|
2802
|
+
totalTokens: totalTokensActual ?? (inputTokensActual ?? inputTokensEstimate) + (outputTokensActual ?? outputTokensEstimate),
|
|
2803
|
+
durationMs,
|
|
2804
|
+
previousSummary: previousSummary || void 0,
|
|
2805
|
+
tokenCountBefore,
|
|
2806
|
+
tokenCountAfter,
|
|
2807
|
+
archivedCount: compressableMessages.length
|
|
2808
|
+
// deprecated field for backward compatibility
|
|
2809
|
+
};
|
|
2810
|
+
onEvent?.(summarizationEvent);
|
|
2811
|
+
recordTraceEvent(traceSession, {
|
|
2812
|
+
type: "summarization",
|
|
2813
|
+
label: "Context Summarization",
|
|
2814
|
+
actor: { scope: "system", name: "summarizer", role: "summarization" },
|
|
2815
|
+
status: "success",
|
|
2816
|
+
durationMs,
|
|
2817
|
+
inputTokens: inputTokensActual ?? inputTokensEstimate,
|
|
2818
|
+
outputTokens: outputTokensActual ?? outputTokensEstimate,
|
|
2819
|
+
cachedInputTokens,
|
|
2820
|
+
totalTokens: totalTokensActual,
|
|
2821
|
+
messageList: sanitizeTracePayload([
|
|
2822
|
+
...summaryPrompt,
|
|
2823
|
+
{ role: "assistant", content: summaryText, name: "summarization_result" }
|
|
2824
|
+
]),
|
|
2825
|
+
debug: {
|
|
2826
|
+
messagesCompressed: compressableMessages.length,
|
|
2827
|
+
tokenCountBefore,
|
|
2828
|
+
tokenCountAfter,
|
|
2829
|
+
tokensSaved: tokenCountBefore - tokenCountAfter,
|
|
2830
|
+
previousSummaryLength: previousSummary?.length || 0,
|
|
2831
|
+
usageFromModel: inputTokensActual !== void 0,
|
|
2832
|
+
cachedInputTokens
|
|
2833
|
+
}
|
|
2834
|
+
});
|
|
2570
2835
|
return {
|
|
2571
|
-
messages:
|
|
2836
|
+
messages: finalMessages,
|
|
2572
2837
|
summaries
|
|
2573
2838
|
};
|
|
2574
2839
|
};
|
|
@@ -3095,6 +3360,6 @@ function createCodeGuardrail(options) {
|
|
|
3095
3360
|
});
|
|
3096
3361
|
}
|
|
3097
3362
|
|
|
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 };
|
|
3363
|
+
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
3364
|
//# sourceMappingURL=index.mjs.map
|
|
3100
3365
|
//# sourceMappingURL=index.mjs.map
|