@cognipeer/agent-sdk 0.2.6 → 0.2.7
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 +214 -67
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.mjs +214 -68
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -758,6 +758,17 @@ declare function applyTokenLimits({ state, limits, }: {
|
|
|
758
758
|
* For production accuracy, consider using a proper tokenizer like tiktoken.
|
|
759
759
|
*/
|
|
760
760
|
declare function countApproxTokens(text: string): number;
|
|
761
|
+
/**
|
|
762
|
+
* Estimates the total token count for an array of messages.
|
|
763
|
+
*
|
|
764
|
+
* Unlike counting only `.content`, this includes:
|
|
765
|
+
* - `tool_calls[].function.arguments` (can be massive JSON)
|
|
766
|
+
* - `tool_calls[].function.name`
|
|
767
|
+
* - `additional_kwargs.tool_calls` (LangChain/Bedrock)
|
|
768
|
+
* - Per-message overhead (role, name, formatting)
|
|
769
|
+
* - Anthropic tool_use content blocks
|
|
770
|
+
*/
|
|
771
|
+
declare function countMessagesTokens(messages: any[]): number;
|
|
761
772
|
|
|
762
773
|
declare function createContextTools(stateRef: {
|
|
763
774
|
toolHistory?: any[];
|
|
@@ -914,4 +925,4 @@ declare function httpSink(url: string, headers?: Record<string, string>): TraceS
|
|
|
914
925
|
*/
|
|
915
926
|
declare function startStreamingSession(session: TraceSessionRuntime | undefined, agentRuntime?: AgentRuntimeConfig): Promise<void>;
|
|
916
927
|
|
|
917
|
-
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 };
|
|
928
|
+
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, countMessagesTokens, 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
|
@@ -758,6 +758,17 @@ declare function applyTokenLimits({ state, limits, }: {
|
|
|
758
758
|
* For production accuracy, consider using a proper tokenizer like tiktoken.
|
|
759
759
|
*/
|
|
760
760
|
declare function countApproxTokens(text: string): number;
|
|
761
|
+
/**
|
|
762
|
+
* Estimates the total token count for an array of messages.
|
|
763
|
+
*
|
|
764
|
+
* Unlike counting only `.content`, this includes:
|
|
765
|
+
* - `tool_calls[].function.arguments` (can be massive JSON)
|
|
766
|
+
* - `tool_calls[].function.name`
|
|
767
|
+
* - `additional_kwargs.tool_calls` (LangChain/Bedrock)
|
|
768
|
+
* - Per-message overhead (role, name, formatting)
|
|
769
|
+
* - Anthropic tool_use content blocks
|
|
770
|
+
*/
|
|
771
|
+
declare function countMessagesTokens(messages: any[]): number;
|
|
761
772
|
|
|
762
773
|
declare function createContextTools(stateRef: {
|
|
763
774
|
toolHistory?: any[];
|
|
@@ -914,4 +925,4 @@ declare function httpSink(url: string, headers?: Record<string, string>): TraceS
|
|
|
914
925
|
*/
|
|
915
926
|
declare function startStreamingSession(session: TraceSessionRuntime | undefined, agentRuntime?: AgentRuntimeConfig): Promise<void>;
|
|
916
927
|
|
|
917
|
-
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 };
|
|
928
|
+
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, countMessagesTokens, 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
|
@@ -183,7 +183,9 @@ function fromLangchainModel(lcModel) {
|
|
|
183
183
|
stream: async function* (messages, options) {
|
|
184
184
|
if (typeof lcModel.stream !== "function") return;
|
|
185
185
|
const lcMessages = messages.map(toLC);
|
|
186
|
-
|
|
186
|
+
const streamResult = lcModel.stream(lcMessages, options);
|
|
187
|
+
const stream = typeof streamResult?.then === "function" ? await streamResult : streamResult;
|
|
188
|
+
for await (const chunk of stream) {
|
|
187
189
|
yield chunk;
|
|
188
190
|
}
|
|
189
191
|
},
|
|
@@ -305,17 +307,17 @@ function normalizeUsage(raw) {
|
|
|
305
307
|
raw.totalTokens,
|
|
306
308
|
(typeof prompt === "number" ? prompt : 0) + (typeof completion === "number" ? completion : 0)
|
|
307
309
|
);
|
|
308
|
-
const promptDetailsSrc = raw.prompt_tokens_details || raw.promptTokensDetails || {};
|
|
309
|
-
const completionDetailsSrc = raw.completion_tokens_details || raw.completionTokensDetails || {};
|
|
310
|
+
const promptDetailsSrc = raw.prompt_tokens_details || raw.promptTokensDetails || raw.input_token_details || raw.inputTokenDetails || {};
|
|
311
|
+
const completionDetailsSrc = raw.completion_tokens_details || raw.completionTokensDetails || raw.output_token_details || raw.outputTokenDetails || {};
|
|
310
312
|
const normalized = {
|
|
311
313
|
prompt_tokens: safe(prompt),
|
|
312
314
|
completion_tokens: safe(completion),
|
|
313
315
|
total_tokens: safe(total || (prompt || 0) + (completion || 0)),
|
|
314
316
|
prompt_tokens_details: {
|
|
315
317
|
cached_tokens: safe(
|
|
316
|
-
promptDetailsSrc.cached_tokens || promptDetailsSrc.cached || raw.cached_input_tokens || raw.cached_prompt_tokens
|
|
318
|
+
promptDetailsSrc.cached_tokens || promptDetailsSrc.cached || promptDetailsSrc.cache_read || raw.cached_input_tokens || raw.cached_prompt_tokens || raw.cache_read_input_tokens
|
|
317
319
|
),
|
|
318
|
-
audio_tokens: safe(promptDetailsSrc.audio_tokens)
|
|
320
|
+
audio_tokens: safe(promptDetailsSrc.audio_tokens || promptDetailsSrc.audio)
|
|
319
321
|
},
|
|
320
322
|
completion_tokens_details: {
|
|
321
323
|
reasoning_tokens: safe(completionDetailsSrc.reasoning_tokens),
|
|
@@ -1001,8 +1003,12 @@ function recordTraceEvent(session, event) {
|
|
|
1001
1003
|
session.summary.eventCounts[event.type] = (session.summary.eventCounts[event.type] || 0) + 1;
|
|
1002
1004
|
let sections;
|
|
1003
1005
|
if (session.resolvedConfig.logData) {
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
+
if (event.sections && event.sections.length > 0) {
|
|
1007
|
+
sections = ensureUniqueSections(id, event.sections);
|
|
1008
|
+
} else {
|
|
1009
|
+
const converted = convertMessageListToSections(event.messageList);
|
|
1010
|
+
sections = ensureUniqueSections(id, converted);
|
|
1011
|
+
}
|
|
1006
1012
|
}
|
|
1007
1013
|
const resolvedModel = event.model ?? inferModelFromMessages(event.messageList);
|
|
1008
1014
|
const resolvedProvider = event.provider ?? inferProviderFromMessages(event.messageList);
|
|
@@ -1468,7 +1474,9 @@ function createAgentCoreNode(opts) {
|
|
|
1468
1474
|
if (streamingEnabled && typeof modelWithTools.stream === "function") {
|
|
1469
1475
|
let streamedText = "";
|
|
1470
1476
|
let streamedMessage;
|
|
1471
|
-
|
|
1477
|
+
const streamResult = modelWithTools.stream(normalizedMessages, { signal: abortSignal, cancellationToken });
|
|
1478
|
+
const stream = typeof streamResult?.then === "function" ? await streamResult : streamResult;
|
|
1479
|
+
for await (const chunk of stream) {
|
|
1472
1480
|
if (cancellationToken && cancellationToken.isCancellationRequested || abortSignal?.aborted) {
|
|
1473
1481
|
break;
|
|
1474
1482
|
}
|
|
@@ -1513,7 +1521,7 @@ function createAgentCoreNode(opts) {
|
|
|
1513
1521
|
...state.messages,
|
|
1514
1522
|
response
|
|
1515
1523
|
];
|
|
1516
|
-
const rawUsage = response?.usage || response?.response_metadata?.token_usage || response?.response_metadata?.tokenUsage || response?.response_metadata?.usage;
|
|
1524
|
+
const rawUsage = response?.usage_metadata || response?.usage || response?.response_metadata?.token_usage || response?.response_metadata?.tokenUsage || response?.response_metadata?.usage;
|
|
1517
1525
|
const normalized = normalizeUsage(rawUsage);
|
|
1518
1526
|
const modelName = getModelName(runtime.model || opts.model) || "unknown_model";
|
|
1519
1527
|
const durationMs = Date.now() - start;
|
|
@@ -1604,41 +1612,6 @@ function createToolsNode(initialTools, opts) {
|
|
|
1604
1612
|
const remaining = Math.max(0, limits.maxToolCalls - toolCount);
|
|
1605
1613
|
const planned = toolCalls.slice(0, remaining);
|
|
1606
1614
|
const skipped = toolCalls.slice(remaining);
|
|
1607
|
-
for (const tc of skipped) {
|
|
1608
|
-
onEvent?.({ type: "tool_call", phase: "skipped", name: tc.name, id: tc.id, args: tc.args });
|
|
1609
|
-
const sanitizedArgs = sanitizeTracePayload(tc.args);
|
|
1610
|
-
const messageList = [
|
|
1611
|
-
{
|
|
1612
|
-
role: "assistant",
|
|
1613
|
-
name: tc.name,
|
|
1614
|
-
content: `Skipped tool due to tool-call limit: ${tc.name}`,
|
|
1615
|
-
tool_calls: [
|
|
1616
|
-
{
|
|
1617
|
-
id: tc.id,
|
|
1618
|
-
type: "function",
|
|
1619
|
-
function: {
|
|
1620
|
-
name: tc.name,
|
|
1621
|
-
arguments: sanitizedArgs
|
|
1622
|
-
}
|
|
1623
|
-
}
|
|
1624
|
-
]
|
|
1625
|
-
}
|
|
1626
|
-
];
|
|
1627
|
-
recordTraceEvent(traceSession, {
|
|
1628
|
-
type: "tool_call",
|
|
1629
|
-
label: `Tool Skipped - ${tc.name}`,
|
|
1630
|
-
actor: { scope: "tool", name: tc.name, role: "tool" },
|
|
1631
|
-
status: "skipped",
|
|
1632
|
-
toolExecutionId: tc.id,
|
|
1633
|
-
messageList
|
|
1634
|
-
});
|
|
1635
|
-
appended.push({
|
|
1636
|
-
role: "tool",
|
|
1637
|
-
content: `Skipped tool due to tool-call limit: ${tc.name}`,
|
|
1638
|
-
tool_call_id: tc.id || `${tc.name}_${appended.length}`,
|
|
1639
|
-
name: tc.name
|
|
1640
|
-
});
|
|
1641
|
-
}
|
|
1642
1615
|
const isCancelled = () => {
|
|
1643
1616
|
if (abortSignal?.aborted) return { cancelled: true, reason: "aborted" };
|
|
1644
1617
|
if (cancellationToken && cancellationToken.isCancellationRequested) return { cancelled: true, reason: "cancelled" };
|
|
@@ -1879,6 +1852,42 @@ function createToolsNode(initialTools, opts) {
|
|
|
1879
1852
|
break;
|
|
1880
1853
|
}
|
|
1881
1854
|
}
|
|
1855
|
+
for (const tc of skipped) {
|
|
1856
|
+
onEvent?.({ type: "tool_call", phase: "skipped", name: tc.name, id: tc.id, args: tc.args });
|
|
1857
|
+
const sanitizedArgs = sanitizeTracePayload(tc.args);
|
|
1858
|
+
const messageList = [
|
|
1859
|
+
{
|
|
1860
|
+
role: "assistant",
|
|
1861
|
+
name: tc.name,
|
|
1862
|
+
content: `Skipped tool due to tool-call limit: ${tc.name}`,
|
|
1863
|
+
tool_calls: [
|
|
1864
|
+
{
|
|
1865
|
+
id: tc.id,
|
|
1866
|
+
type: "function",
|
|
1867
|
+
function: {
|
|
1868
|
+
name: tc.name,
|
|
1869
|
+
arguments: sanitizedArgs
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
]
|
|
1873
|
+
}
|
|
1874
|
+
];
|
|
1875
|
+
recordTraceEvent(traceSession, {
|
|
1876
|
+
type: "tool_call",
|
|
1877
|
+
label: `Tool Skipped - ${tc.name}`,
|
|
1878
|
+
actor: { scope: "tool", name: tc.name, role: "tool" },
|
|
1879
|
+
status: "skipped",
|
|
1880
|
+
toolExecutionId: tc.id,
|
|
1881
|
+
messageList
|
|
1882
|
+
});
|
|
1883
|
+
appended.push({
|
|
1884
|
+
role: "tool",
|
|
1885
|
+
content: `Skipped tool due to tool-call limit: ${tc.name}`,
|
|
1886
|
+
tool_call_id: tc.id || `${tc.name}_${appended.length}`,
|
|
1887
|
+
name: tc.name
|
|
1888
|
+
});
|
|
1889
|
+
toolCount += 1;
|
|
1890
|
+
}
|
|
1882
1891
|
if (!awaitingApproval && state.ctx?.__awaitingApproval) {
|
|
1883
1892
|
const ctx = { ...state.ctx };
|
|
1884
1893
|
delete ctx.__awaitingApproval;
|
|
@@ -2114,6 +2123,59 @@ function countApproxTokens(text) {
|
|
|
2114
2123
|
const asciiCount = text.length - nonAsciiCount;
|
|
2115
2124
|
return Math.ceil(asciiCount / 4 + nonAsciiCount / 1.5);
|
|
2116
2125
|
}
|
|
2126
|
+
function extractMessageText(m) {
|
|
2127
|
+
if (!m) return "";
|
|
2128
|
+
const parts = [];
|
|
2129
|
+
parts.push(m.role || "");
|
|
2130
|
+
if (m.name) parts.push(m.name);
|
|
2131
|
+
if (typeof m.content === "string") {
|
|
2132
|
+
parts.push(m.content);
|
|
2133
|
+
} else if (Array.isArray(m.content)) {
|
|
2134
|
+
for (const c of m.content) {
|
|
2135
|
+
if (typeof c === "string") parts.push(c);
|
|
2136
|
+
else if (c?.text) parts.push(c.text);
|
|
2137
|
+
else if (c?.content) parts.push(c.content);
|
|
2138
|
+
else if (c?.type === "tool_use" && c?.input) {
|
|
2139
|
+
parts.push(c.name || "");
|
|
2140
|
+
parts.push(typeof c.input === "string" ? c.input : JSON.stringify(c.input));
|
|
2141
|
+
} else if (c != null && typeof c === "object") {
|
|
2142
|
+
parts.push(JSON.stringify(c));
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
} else if (m.content != null && typeof m.content === "object") {
|
|
2146
|
+
parts.push(JSON.stringify(m.content));
|
|
2147
|
+
}
|
|
2148
|
+
if (Array.isArray(m.tool_calls)) {
|
|
2149
|
+
for (const tc of m.tool_calls) {
|
|
2150
|
+
if (tc?.function?.name) parts.push(tc.function.name);
|
|
2151
|
+
if (tc?.function?.arguments) {
|
|
2152
|
+
parts.push(
|
|
2153
|
+
typeof tc.function.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function.arguments)
|
|
2154
|
+
);
|
|
2155
|
+
}
|
|
2156
|
+
if (tc?.name && !tc?.function?.name) parts.push(tc.name);
|
|
2157
|
+
if (tc?.args && !tc?.function?.arguments) {
|
|
2158
|
+
parts.push(typeof tc.args === "string" ? tc.args : JSON.stringify(tc.args));
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
if (Array.isArray(m.additional_kwargs?.tool_calls)) {
|
|
2163
|
+
for (const tc of m.additional_kwargs.tool_calls) {
|
|
2164
|
+
if (tc?.function?.name) parts.push(tc.function.name);
|
|
2165
|
+
if (tc?.function?.arguments) {
|
|
2166
|
+
parts.push(
|
|
2167
|
+
typeof tc.function.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function.arguments)
|
|
2168
|
+
);
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
return parts.join("\n");
|
|
2173
|
+
}
|
|
2174
|
+
function countMessagesTokens(messages) {
|
|
2175
|
+
if (!Array.isArray(messages) || messages.length === 0) return 0;
|
|
2176
|
+
const allText = messages.map(extractMessageText).join("\n");
|
|
2177
|
+
return countApproxTokens(allText) + messages.length * 4;
|
|
2178
|
+
}
|
|
2117
2179
|
|
|
2118
2180
|
// src/agent.ts
|
|
2119
2181
|
function createAgent(opts) {
|
|
@@ -2283,8 +2345,7 @@ function createAgent(opts) {
|
|
|
2283
2345
|
maxTok = 5e4;
|
|
2284
2346
|
}
|
|
2285
2347
|
if (maxTok) {
|
|
2286
|
-
const
|
|
2287
|
-
const tokenCount = countApproxTokens(allText);
|
|
2348
|
+
const tokenCount = countMessagesTokens(state.messages || []);
|
|
2288
2349
|
if (tokenCount > maxTok) {
|
|
2289
2350
|
const ctx = { ...state.ctx || {}, __needsSummarization: true };
|
|
2290
2351
|
state = { ...state, ctx };
|
|
@@ -2665,6 +2726,83 @@ function createAgentNode(opts) {
|
|
|
2665
2726
|
};
|
|
2666
2727
|
}
|
|
2667
2728
|
|
|
2729
|
+
// src/utils/traceSections.ts
|
|
2730
|
+
function formatMessageContent(content) {
|
|
2731
|
+
if (content == null) return "";
|
|
2732
|
+
if (typeof content === "string") return content;
|
|
2733
|
+
if (Array.isArray(content)) {
|
|
2734
|
+
return content.map((item) => {
|
|
2735
|
+
if (typeof item === "string") return item;
|
|
2736
|
+
if (item && typeof item === "object") {
|
|
2737
|
+
if (typeof item.text === "string") return item.text;
|
|
2738
|
+
if (typeof item.content === "string") return item.content;
|
|
2739
|
+
try {
|
|
2740
|
+
return JSON.stringify(item);
|
|
2741
|
+
} catch {
|
|
2742
|
+
return String(item);
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
try {
|
|
2746
|
+
return JSON.stringify(item);
|
|
2747
|
+
} catch {
|
|
2748
|
+
return String(item);
|
|
2749
|
+
}
|
|
2750
|
+
}).filter(Boolean).join("\n");
|
|
2751
|
+
}
|
|
2752
|
+
if (typeof content === "object") {
|
|
2753
|
+
if (typeof content.text === "string") return content.text;
|
|
2754
|
+
if (typeof content.content === "string") return content.content;
|
|
2755
|
+
try {
|
|
2756
|
+
return JSON.stringify(content, null, 2);
|
|
2757
|
+
} catch {
|
|
2758
|
+
return String(content);
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
return String(content);
|
|
2762
|
+
}
|
|
2763
|
+
function buildSummarizationSections(opts) {
|
|
2764
|
+
const sections = [];
|
|
2765
|
+
sections.push({
|
|
2766
|
+
kind: "summary",
|
|
2767
|
+
label: "Context Summary",
|
|
2768
|
+
content: opts.summaryText
|
|
2769
|
+
});
|
|
2770
|
+
const tokensSaved = opts.tokenCountBefore - opts.tokenCountAfter;
|
|
2771
|
+
const reductionPct = opts.tokenCountBefore > 0 ? Math.round(tokensSaved / opts.tokenCountBefore * 100) : 0;
|
|
2772
|
+
const metaData = {
|
|
2773
|
+
messagesCompressed: opts.messagesCompressed,
|
|
2774
|
+
tokenCountBefore: opts.tokenCountBefore,
|
|
2775
|
+
tokenCountAfter: opts.tokenCountAfter,
|
|
2776
|
+
tokensSaved,
|
|
2777
|
+
reductionPercent: reductionPct
|
|
2778
|
+
};
|
|
2779
|
+
if (opts.inputTokens !== void 0) metaData.inputTokens = opts.inputTokens;
|
|
2780
|
+
if (opts.outputTokens !== void 0) metaData.outputTokens = opts.outputTokens;
|
|
2781
|
+
if (opts.cachedInputTokens !== void 0) metaData.cachedInputTokens = opts.cachedInputTokens;
|
|
2782
|
+
if (opts.durationMs !== void 0) metaData.durationMs = opts.durationMs;
|
|
2783
|
+
if (opts.previousSummary) metaData.previousSummaryLength = opts.previousSummary.length;
|
|
2784
|
+
sections.push({
|
|
2785
|
+
kind: "metadata",
|
|
2786
|
+
label: "Summarization Stats",
|
|
2787
|
+
data: metaData
|
|
2788
|
+
});
|
|
2789
|
+
if (Array.isArray(opts.promptMessages) && opts.promptMessages.length > 0) {
|
|
2790
|
+
for (const msg of opts.promptMessages) {
|
|
2791
|
+
const role = typeof msg?.role === "string" ? msg.role : "system";
|
|
2792
|
+
const content = formatMessageContent(msg?.content ?? "");
|
|
2793
|
+
if (content) {
|
|
2794
|
+
sections.push({
|
|
2795
|
+
kind: "message",
|
|
2796
|
+
label: `Summarization Prompt (${role})`,
|
|
2797
|
+
role,
|
|
2798
|
+
content
|
|
2799
|
+
});
|
|
2800
|
+
}
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
return sections;
|
|
2804
|
+
}
|
|
2805
|
+
|
|
2668
2806
|
// src/nodes/contextSummarize.ts
|
|
2669
2807
|
var systemMessage = (content) => ({ role: "system", content });
|
|
2670
2808
|
var humanMessage = (content) => ({ role: "user", content });
|
|
@@ -2723,8 +2861,7 @@ function createContextSummarizeNode(opts) {
|
|
|
2723
2861
|
if (compressableMessages.length === 0) {
|
|
2724
2862
|
return {};
|
|
2725
2863
|
}
|
|
2726
|
-
const
|
|
2727
|
-
const tokenCountBefore = countApproxTokens(allTextBefore);
|
|
2864
|
+
const tokenCountBefore = countMessagesTokens(messages);
|
|
2728
2865
|
const summaryPromptMaxTokens = config.summaryPromptMaxTokens;
|
|
2729
2866
|
const availableTokensForConversation = summaryPromptMaxTokens - 500;
|
|
2730
2867
|
const messageTexts = [];
|
|
@@ -2787,7 +2924,7 @@ Summary:`;
|
|
|
2787
2924
|
durationMs = Date.now() - startTime;
|
|
2788
2925
|
summaryText = typeof response.content === "string" ? response.content : Array.isArray(response.content) ? response.content.map((c) => c.text || "").join("") : JSON.stringify(response.content);
|
|
2789
2926
|
outputTokensEstimate = countApproxTokens(summaryText);
|
|
2790
|
-
const rawUsage = response?.usage || response?.response_metadata?.token_usage || response?.response_metadata?.tokenUsage || response?.response_metadata?.usage;
|
|
2927
|
+
const rawUsage = response?.usage_metadata || response?.usage || response?.response_metadata?.token_usage || response?.response_metadata?.tokenUsage || response?.response_metadata?.usage;
|
|
2791
2928
|
const normalized = normalizeUsage(rawUsage);
|
|
2792
2929
|
if (normalized) {
|
|
2793
2930
|
inputTokensActual = normalized.prompt_tokens;
|
|
@@ -2819,7 +2956,16 @@ Summary:`;
|
|
|
2819
2956
|
inputTokens: inputTokensEstimate,
|
|
2820
2957
|
outputTokens: 0,
|
|
2821
2958
|
error: { message: err?.message || String(err), stack: err?.stack },
|
|
2822
|
-
|
|
2959
|
+
sections: buildSummarizationSections({
|
|
2960
|
+
summaryText: "",
|
|
2961
|
+
messagesCompressed: 0,
|
|
2962
|
+
tokenCountBefore,
|
|
2963
|
+
tokenCountAfter: tokenCountBefore,
|
|
2964
|
+
inputTokens: inputTokensEstimate,
|
|
2965
|
+
outputTokens: 0,
|
|
2966
|
+
durationMs,
|
|
2967
|
+
promptMessages: summaryPrompt
|
|
2968
|
+
})
|
|
2823
2969
|
});
|
|
2824
2970
|
return {};
|
|
2825
2971
|
}
|
|
@@ -2873,8 +3019,7 @@ Summary:`;
|
|
|
2873
3019
|
};
|
|
2874
3020
|
const summaries = Array.isArray(state.summaries) ? [...state.summaries, summaryText] : [summaryText];
|
|
2875
3021
|
const finalMessages = [...validatedMessages, assistantSummaryCall, toolSummaryResponse];
|
|
2876
|
-
const
|
|
2877
|
-
const tokenCountAfter = countApproxTokens(allTextAfter);
|
|
3022
|
+
const tokenCountAfter = countMessagesTokens(finalMessages);
|
|
2878
3023
|
const summarizationEvent = {
|
|
2879
3024
|
type: "summarization",
|
|
2880
3025
|
summary: summaryText,
|
|
@@ -2901,10 +3046,21 @@ Summary:`;
|
|
|
2901
3046
|
outputTokens: outputTokensActual ?? outputTokensEstimate,
|
|
2902
3047
|
cachedInputTokens,
|
|
2903
3048
|
totalTokens: totalTokensActual,
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
3049
|
+
sections: buildSummarizationSections({
|
|
3050
|
+
summaryText,
|
|
3051
|
+
previousSummary: previousSummary || void 0,
|
|
3052
|
+
messagesCompressed: compressableMessages.length,
|
|
3053
|
+
tokenCountBefore,
|
|
3054
|
+
tokenCountAfter,
|
|
3055
|
+
inputTokens: inputTokensActual ?? inputTokensEstimate,
|
|
3056
|
+
outputTokens: outputTokensActual ?? outputTokensEstimate,
|
|
3057
|
+
cachedInputTokens,
|
|
3058
|
+
durationMs,
|
|
3059
|
+
promptMessages: sanitizeTracePayload([
|
|
3060
|
+
...summaryPrompt,
|
|
3061
|
+
{ role: "assistant", content: summaryText, name: "summarization_result" }
|
|
3062
|
+
])
|
|
3063
|
+
}),
|
|
2908
3064
|
debug: {
|
|
2909
3065
|
messagesCompressed: compressableMessages.length,
|
|
2910
3066
|
tokenCountBefore,
|
|
@@ -3028,17 +3184,7 @@ function needsSummarization(state, opts, summarizationEnabled) {
|
|
|
3028
3184
|
maxTok = 5e4;
|
|
3029
3185
|
}
|
|
3030
3186
|
try {
|
|
3031
|
-
const
|
|
3032
|
-
if (typeof m.content === "string") return m.content;
|
|
3033
|
-
if (Array.isArray(m.content)) {
|
|
3034
|
-
return m.content.map((c) => typeof c === "string" ? c : c?.text ?? c?.content ?? JSON.stringify(c)).join("");
|
|
3035
|
-
}
|
|
3036
|
-
if (m.content && typeof m.content === "object") {
|
|
3037
|
-
return JSON.stringify(m.content);
|
|
3038
|
-
}
|
|
3039
|
-
return "";
|
|
3040
|
-
}).join("\n");
|
|
3041
|
-
const tokenCount = countApproxTokens(allText);
|
|
3187
|
+
const tokenCount = countMessagesTokens(state.messages || []);
|
|
3042
3188
|
return tokenCount > maxTok;
|
|
3043
3189
|
} catch (e) {
|
|
3044
3190
|
return false;
|
|
@@ -3437,6 +3583,6 @@ function createCodeGuardrail(options) {
|
|
|
3437
3583
|
});
|
|
3438
3584
|
}
|
|
3439
3585
|
|
|
3440
|
-
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 };
|
|
3586
|
+
export { GuardrailPhase, agentVerdictRule, applyTokenLimits, buildSystemPrompt, captureSnapshot, codePresenceRule, cognipeerSink, countApproxTokens, countMessagesTokens, 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 };
|
|
3441
3587
|
//# sourceMappingURL=index.mjs.map
|
|
3442
3588
|
//# sourceMappingURL=index.mjs.map
|