@inkeep/agents-run-api 0.0.0-dev-20251222193909 → 0.0.0-dev-20251222214618
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/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/thinking-preparation.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/tool.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-component.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-components.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact-retrieval-guidance.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact.js +5 -0
- package/dist/a2a/client.d.ts +184 -0
- package/dist/a2a/client.js +510 -0
- package/dist/a2a/handlers.d.ts +7 -0
- package/dist/a2a/handlers.js +559 -0
- package/dist/a2a/transfer.d.ts +22 -0
- package/dist/a2a/transfer.js +46 -0
- package/dist/a2a/types.d.ts +79 -0
- package/dist/a2a/types.js +22 -0
- package/dist/agents/Agent.d.ts +220 -0
- package/dist/agents/Agent.js +1938 -0
- package/dist/agents/ModelFactory.d.ts +63 -0
- package/dist/agents/ModelFactory.js +194 -0
- package/dist/agents/SystemPromptBuilder.d.ts +21 -0
- package/dist/agents/SystemPromptBuilder.js +48 -0
- package/dist/agents/ToolSessionManager.d.ts +63 -0
- package/dist/agents/ToolSessionManager.js +146 -0
- package/dist/agents/generateTaskHandler.d.ts +49 -0
- package/dist/agents/generateTaskHandler.js +512 -0
- package/dist/agents/relationTools.d.ts +57 -0
- package/dist/agents/relationTools.js +262 -0
- package/dist/agents/types.d.ts +28 -0
- package/dist/agents/types.js +1 -0
- package/dist/agents/versions/v1/Phase1Config.d.ts +26 -0
- package/dist/agents/versions/v1/Phase1Config.js +413 -0
- package/dist/agents/versions/v1/Phase2Config.d.ts +31 -0
- package/dist/agents/versions/v1/Phase2Config.js +330 -0
- package/dist/constants/execution-limits/defaults.d.ts +47 -0
- package/dist/constants/execution-limits/defaults.js +48 -0
- package/dist/constants/execution-limits/index.d.ts +6 -0
- package/dist/constants/execution-limits/index.js +16 -0
- package/dist/create-app.d.ts +9 -0
- package/dist/create-app.js +195 -0
- package/dist/data/agent.d.ts +7 -0
- package/dist/data/agent.js +72 -0
- package/dist/data/agents.d.ts +34 -0
- package/dist/data/agents.js +139 -0
- package/dist/data/conversations.d.ts +88 -0
- package/dist/{conversations2.js → data/conversations.js} +42 -7
- package/dist/data/db/dbClient.d.ts +6 -0
- package/dist/data/db/dbClient.js +17 -0
- package/dist/env.d.ts +57 -0
- package/dist/env.js +1 -2
- package/dist/handlers/executionHandler.d.ts +39 -0
- package/dist/handlers/executionHandler.js +456 -0
- package/dist/index.d.ts +8 -29
- package/dist/index.js +5 -11487
- package/dist/instrumentation.d.ts +1 -2
- package/dist/instrumentation.js +66 -3
- package/dist/{logger2.js → logger.d.ts} +1 -2
- package/dist/logger.js +1 -1
- package/dist/middleware/api-key-auth.d.ts +26 -0
- package/dist/middleware/api-key-auth.js +240 -0
- package/dist/middleware/index.d.ts +2 -0
- package/dist/middleware/index.js +3 -0
- package/dist/openapi.d.ts +4 -0
- package/dist/openapi.js +54 -0
- package/dist/routes/agents.d.ts +12 -0
- package/dist/routes/agents.js +147 -0
- package/dist/routes/chat.d.ts +13 -0
- package/dist/routes/chat.js +293 -0
- package/dist/routes/chatDataStream.d.ts +13 -0
- package/dist/routes/chatDataStream.js +352 -0
- package/dist/routes/mcp.d.ts +13 -0
- package/dist/routes/mcp.js +495 -0
- package/dist/services/AgentSession.d.ts +356 -0
- package/dist/services/AgentSession.js +1208 -0
- package/dist/services/ArtifactParser.d.ts +105 -0
- package/dist/services/ArtifactParser.js +338 -0
- package/dist/services/ArtifactService.d.ts +123 -0
- package/dist/services/ArtifactService.js +611 -0
- package/dist/services/IncrementalStreamParser.d.ts +98 -0
- package/dist/services/IncrementalStreamParser.js +327 -0
- package/dist/services/MidGenerationCompressor.d.ts +91 -0
- package/dist/services/MidGenerationCompressor.js +375 -0
- package/dist/services/PendingToolApprovalManager.d.ts +62 -0
- package/dist/services/PendingToolApprovalManager.js +133 -0
- package/dist/services/ResponseFormatter.d.ts +39 -0
- package/dist/services/ResponseFormatter.js +152 -0
- package/dist/tools/NativeSandboxExecutor.d.ts +38 -0
- package/dist/tools/NativeSandboxExecutor.js +432 -0
- package/dist/tools/SandboxExecutorFactory.d.ts +36 -0
- package/dist/tools/SandboxExecutorFactory.js +80 -0
- package/dist/tools/VercelSandboxExecutor.d.ts +71 -0
- package/dist/tools/VercelSandboxExecutor.js +340 -0
- package/dist/tools/distill-conversation-tool.d.ts +41 -0
- package/dist/tools/distill-conversation-tool.js +156 -0
- package/dist/tools/sandbox-utils.d.ts +18 -0
- package/dist/tools/sandbox-utils.js +53 -0
- package/dist/types/chat.d.ts +27 -0
- package/dist/types/chat.js +1 -0
- package/dist/types/execution-context.d.ts +46 -0
- package/dist/types/execution-context.js +27 -0
- package/dist/types/xml.d.ts +5 -0
- package/dist/utils/SchemaProcessor.d.ts +52 -0
- package/dist/utils/SchemaProcessor.js +182 -0
- package/dist/utils/agent-operations.d.ts +62 -0
- package/dist/utils/agent-operations.js +53 -0
- package/dist/utils/artifact-component-schema.d.ts +42 -0
- package/dist/utils/artifact-component-schema.js +186 -0
- package/dist/utils/cleanup.d.ts +21 -0
- package/dist/utils/cleanup.js +59 -0
- package/dist/utils/data-component-schema.d.ts +2 -0
- package/dist/utils/data-component-schema.js +3 -0
- package/dist/utils/default-status-schemas.d.ts +20 -0
- package/dist/utils/default-status-schemas.js +24 -0
- package/dist/utils/json-postprocessor.d.ts +13 -0
- package/dist/{json-postprocessor.cjs → utils/json-postprocessor.js} +1 -2
- package/dist/utils/model-context-utils.d.ts +31 -0
- package/dist/utils/model-context-utils.js +149 -0
- package/dist/utils/model-resolver.d.ts +6 -0
- package/dist/utils/model-resolver.js +34 -0
- package/dist/utils/schema-validation.d.ts +44 -0
- package/dist/utils/schema-validation.js +97 -0
- package/dist/utils/stream-helpers.d.ts +197 -0
- package/dist/utils/stream-helpers.js +518 -0
- package/dist/utils/stream-registry.d.ts +22 -0
- package/dist/utils/stream-registry.js +34 -0
- package/dist/utils/token-estimator.d.ts +69 -0
- package/dist/utils/token-estimator.js +53 -0
- package/dist/utils/tracer.d.ts +7 -0
- package/dist/utils/tracer.js +7 -0
- package/package.json +4 -20
- package/dist/SandboxExecutorFactory.cjs +0 -895
- package/dist/SandboxExecutorFactory.js +0 -893
- package/dist/SandboxExecutorFactory.js.map +0 -1
- package/dist/chunk-VBDAOXYI.cjs +0 -927
- package/dist/chunk-VBDAOXYI.js +0 -832
- package/dist/chunk-VBDAOXYI.js.map +0 -1
- package/dist/chunk.cjs +0 -34
- package/dist/conversations.cjs +0 -7
- package/dist/conversations.js +0 -7
- package/dist/conversations2.cjs +0 -209
- package/dist/conversations2.js.map +0 -1
- package/dist/dbClient.cjs +0 -9676
- package/dist/dbClient.js +0 -9670
- package/dist/dbClient.js.map +0 -1
- package/dist/dbClient2.cjs +0 -5
- package/dist/dbClient2.js +0 -5
- package/dist/env.cjs +0 -59
- package/dist/env.js.map +0 -1
- package/dist/execution-limits.cjs +0 -260
- package/dist/execution-limits.js +0 -63
- package/dist/execution-limits.js.map +0 -1
- package/dist/index.cjs +0 -11512
- package/dist/index.d.cts +0 -36
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/instrumentation.cjs +0 -12
- package/dist/instrumentation.d.cts +0 -18
- package/dist/instrumentation.d.cts.map +0 -1
- package/dist/instrumentation.d.ts.map +0 -1
- package/dist/instrumentation2.cjs +0 -116
- package/dist/instrumentation2.js +0 -69
- package/dist/instrumentation2.js.map +0 -1
- package/dist/json-postprocessor.js +0 -20
- package/dist/json-postprocessor.js.map +0 -1
- package/dist/logger.cjs +0 -5
- package/dist/logger2.cjs +0 -1
- package/dist/nodefs.cjs +0 -29
- package/dist/nodefs.js +0 -27
- package/dist/nodefs.js.map +0 -1
- package/dist/opfs-ahp.cjs +0 -367
- package/dist/opfs-ahp.js +0 -368
- package/dist/opfs-ahp.js.map +0 -1
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { getLogger } from "../logger.js";
|
|
2
|
+
import { agentSessionManager } from "./AgentSession.js";
|
|
3
|
+
import { distillConversation } from "../tools/distill-conversation-tool.js";
|
|
4
|
+
import { randomUUID } from "crypto";
|
|
5
|
+
|
|
6
|
+
//#region src/services/MidGenerationCompressor.ts
|
|
7
|
+
const logger = getLogger("MidGenerationCompressor");
|
|
8
|
+
/**
|
|
9
|
+
* Get compression config from environment variables
|
|
10
|
+
*/
|
|
11
|
+
function getCompressionConfigFromEnv() {
|
|
12
|
+
return {
|
|
13
|
+
hardLimit: parseInt(process.env.AGENTS_COMPRESSION_HARD_LIMIT || "120000"),
|
|
14
|
+
safetyBuffer: parseInt(process.env.AGENTS_COMPRESSION_SAFETY_BUFFER || "20000"),
|
|
15
|
+
enabled: process.env.AGENTS_COMPRESSION_ENABLED !== "false"
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Simple mid-generation compressor
|
|
20
|
+
* Compresses context when generate() steps get too large
|
|
21
|
+
*/
|
|
22
|
+
var MidGenerationCompressor = class {
|
|
23
|
+
shouldCompress = false;
|
|
24
|
+
processedToolCalls = /* @__PURE__ */ new Set();
|
|
25
|
+
lastProcessedMessageIndex = 0;
|
|
26
|
+
cumulativeSummary = null;
|
|
27
|
+
constructor(sessionId, conversationId, tenantId, projectId, config, summarizerModel, baseModel) {
|
|
28
|
+
this.sessionId = sessionId;
|
|
29
|
+
this.conversationId = conversationId;
|
|
30
|
+
this.tenantId = tenantId;
|
|
31
|
+
this.projectId = projectId;
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.summarizerModel = summarizerModel;
|
|
34
|
+
this.baseModel = baseModel;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get the hard limit for compression decisions
|
|
38
|
+
*/
|
|
39
|
+
getHardLimit() {
|
|
40
|
+
return this.config.hardLimit;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Estimate tokens (4 chars = 1 token)
|
|
44
|
+
*/
|
|
45
|
+
estimateTokens(content) {
|
|
46
|
+
const text = typeof content === "string" ? content : JSON.stringify(content);
|
|
47
|
+
return Math.ceil(text.length / 4);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Calculate total context size
|
|
51
|
+
*/
|
|
52
|
+
calculateContextSize(messages) {
|
|
53
|
+
return messages.reduce((total, msg) => {
|
|
54
|
+
let msgTokens = 0;
|
|
55
|
+
if (Array.isArray(msg.content)) {
|
|
56
|
+
for (const block of msg.content) if (block.type === "text") msgTokens += this.estimateTokens(block.text || "");
|
|
57
|
+
else if (block.type === "tool-call") msgTokens += this.estimateTokens(JSON.stringify({
|
|
58
|
+
toolCallId: block.toolCallId,
|
|
59
|
+
toolName: block.toolName,
|
|
60
|
+
input: block.input
|
|
61
|
+
}));
|
|
62
|
+
else if (block.type === "tool-result") msgTokens += this.estimateTokens(JSON.stringify({
|
|
63
|
+
toolCallId: block.toolCallId,
|
|
64
|
+
toolName: block.toolName,
|
|
65
|
+
output: block.output
|
|
66
|
+
}));
|
|
67
|
+
} else if (typeof msg.content === "string") msgTokens += this.estimateTokens(msg.content);
|
|
68
|
+
else if (msg.content) msgTokens += this.estimateTokens(JSON.stringify(msg.content));
|
|
69
|
+
return total + msgTokens;
|
|
70
|
+
}, 0);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Manual compression request from LLM tool
|
|
74
|
+
*/
|
|
75
|
+
requestManualCompression(reason) {
|
|
76
|
+
this.shouldCompress = true;
|
|
77
|
+
logger.info({
|
|
78
|
+
sessionId: this.sessionId,
|
|
79
|
+
reason: reason || "Manual request from LLM"
|
|
80
|
+
}, "Manual compression requested");
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Check if compression is needed (either automatic or manual)
|
|
84
|
+
*/
|
|
85
|
+
isCompressionNeeded(messages) {
|
|
86
|
+
if (this.shouldCompress) return true;
|
|
87
|
+
const contextSize = this.calculateContextSize(messages);
|
|
88
|
+
return this.config.hardLimit - contextSize <= this.config.safetyBuffer;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Perform compression: save all tool results as artifacts and create summary
|
|
92
|
+
*/
|
|
93
|
+
async compress(messages) {
|
|
94
|
+
const contextSizeBefore = this.calculateContextSize(messages);
|
|
95
|
+
logger.info({
|
|
96
|
+
sessionId: this.sessionId,
|
|
97
|
+
messageCount: messages.length,
|
|
98
|
+
contextSize: contextSizeBefore
|
|
99
|
+
}, "COMPRESSION: Starting compression");
|
|
100
|
+
const toolResultCount = messages.reduce((count, msg) => {
|
|
101
|
+
if (Array.isArray(msg.content)) return count + msg.content.filter((block) => block.type === "tool-result").length;
|
|
102
|
+
return count;
|
|
103
|
+
}, 0);
|
|
104
|
+
logger.debug({ toolResultCount }, "Tool results found for compression");
|
|
105
|
+
const toolCallToArtifactMap = await this.saveToolResultsAsArtifacts(messages);
|
|
106
|
+
const summary = await this.createConversationSummary(messages, toolCallToArtifactMap);
|
|
107
|
+
const contextSizeAfter = this.estimateTokens(JSON.stringify(summary));
|
|
108
|
+
const session = agentSessionManager.getSession(this.sessionId);
|
|
109
|
+
if (session) {
|
|
110
|
+
const wasManualRequest = this.shouldCompress;
|
|
111
|
+
session.recordEvent("compression", this.sessionId, {
|
|
112
|
+
reason: wasManualRequest ? "manual" : "automatic",
|
|
113
|
+
messageCount: messages.length,
|
|
114
|
+
artifactCount: Object.keys(toolCallToArtifactMap).length,
|
|
115
|
+
contextSizeBefore,
|
|
116
|
+
contextSizeAfter,
|
|
117
|
+
compressionType: "mid_generation"
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
this.shouldCompress = false;
|
|
121
|
+
logger.info({
|
|
122
|
+
sessionId: this.sessionId,
|
|
123
|
+
artifactsCreated: Object.keys(toolCallToArtifactMap).length,
|
|
124
|
+
messageCount: messages.length,
|
|
125
|
+
contextSizeBefore,
|
|
126
|
+
contextSizeAfter,
|
|
127
|
+
artifactIds: Object.values(toolCallToArtifactMap)
|
|
128
|
+
}, "COMPRESSION: Compression completed successfully");
|
|
129
|
+
return {
|
|
130
|
+
artifactIds: Object.values(toolCallToArtifactMap),
|
|
131
|
+
summary
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 1. Save NEW tool results as artifacts (only process messages since last compression)
|
|
136
|
+
*/
|
|
137
|
+
async saveToolResultsAsArtifacts(messages) {
|
|
138
|
+
const session = agentSessionManager.getSession(this.sessionId);
|
|
139
|
+
if (!session) throw new Error(`No session found: ${this.sessionId}`);
|
|
140
|
+
const toolCallToArtifactMap = {};
|
|
141
|
+
const newMessages = messages.slice(this.lastProcessedMessageIndex);
|
|
142
|
+
logger.debug({
|
|
143
|
+
totalMessages: messages.length,
|
|
144
|
+
newMessages: newMessages.length,
|
|
145
|
+
startIndex: this.lastProcessedMessageIndex
|
|
146
|
+
}, "Starting compression artifact processing");
|
|
147
|
+
for (const message of newMessages) if (Array.isArray(message.content)) {
|
|
148
|
+
for (const block of message.content) if (block.type === "tool-result") {
|
|
149
|
+
if (block.toolName === "get_reference_artifact" || block.toolName === "thinking_complete") {
|
|
150
|
+
logger.debug({
|
|
151
|
+
toolCallId: block.toolCallId,
|
|
152
|
+
toolName: block.toolName
|
|
153
|
+
}, "Skipping special tool - not creating artifacts");
|
|
154
|
+
this.processedToolCalls.add(block.toolCallId);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (this.processedToolCalls.has(block.toolCallId)) {
|
|
158
|
+
logger.debug({
|
|
159
|
+
toolCallId: block.toolCallId,
|
|
160
|
+
toolName: block.toolName
|
|
161
|
+
}, "Skipping already processed tool call");
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const artifactId = `compress_${block.toolName || "tool"}_${block.toolCallId || Date.now()}_${randomUUID().slice(0, 8)}`;
|
|
165
|
+
logger.debug({
|
|
166
|
+
artifactId,
|
|
167
|
+
toolName: block.toolName,
|
|
168
|
+
toolCallId: block.toolCallId
|
|
169
|
+
}, "Saving compression artifact");
|
|
170
|
+
let toolInput = null;
|
|
171
|
+
if (Array.isArray(message.content)) toolInput = message.content.find((b) => b.type === "tool-call" && b.toolCallId === block.toolCallId)?.input;
|
|
172
|
+
const cleanToolResult = this.removeStructureHints(block.output);
|
|
173
|
+
const toolResultData = {
|
|
174
|
+
toolName: block.toolName,
|
|
175
|
+
toolInput,
|
|
176
|
+
toolResult: cleanToolResult,
|
|
177
|
+
compressedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
178
|
+
};
|
|
179
|
+
if (this.isEmpty(toolResultData)) {
|
|
180
|
+
logger.debug({
|
|
181
|
+
toolName: block.toolName,
|
|
182
|
+
toolCallId: block.toolCallId
|
|
183
|
+
}, "Skipping empty tool result");
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const artifactData = {
|
|
187
|
+
artifactId,
|
|
188
|
+
taskId: `task_${this.conversationId}-${this.sessionId}`,
|
|
189
|
+
toolCallId: block.toolCallId,
|
|
190
|
+
artifactType: "tool_result",
|
|
191
|
+
pendingGeneration: true,
|
|
192
|
+
tenantId: this.tenantId,
|
|
193
|
+
projectId: this.projectId,
|
|
194
|
+
contextId: this.conversationId,
|
|
195
|
+
subAgentId: this.sessionId,
|
|
196
|
+
metadata: {
|
|
197
|
+
toolCallId: block.toolCallId,
|
|
198
|
+
toolName: block.toolName,
|
|
199
|
+
compressionReason: "mid_generation_context_limit"
|
|
200
|
+
},
|
|
201
|
+
summaryData: {
|
|
202
|
+
toolName: block.toolName,
|
|
203
|
+
note: "Compressed tool result - see full data for details"
|
|
204
|
+
},
|
|
205
|
+
data: toolResultData
|
|
206
|
+
};
|
|
207
|
+
const fullData = artifactData.data;
|
|
208
|
+
if (!(fullData && typeof fullData === "object" && Object.keys(fullData).length > 0 && fullData.toolResult && (typeof fullData.toolResult !== "object" || Object.keys(fullData.toolResult).length > 0))) {
|
|
209
|
+
logger.debug({
|
|
210
|
+
artifactId,
|
|
211
|
+
toolName: block.toolName,
|
|
212
|
+
toolCallId: block.toolCallId
|
|
213
|
+
}, "Skipping empty compression artifact");
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
session.recordEvent("artifact_saved", this.sessionId, artifactData);
|
|
217
|
+
this.processedToolCalls.add(block.toolCallId);
|
|
218
|
+
toolCallToArtifactMap[block.toolCallId] = artifactId;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
this.lastProcessedMessageIndex = messages.length;
|
|
222
|
+
logger.debug({
|
|
223
|
+
totalArtifactsCreated: Object.keys(toolCallToArtifactMap).length,
|
|
224
|
+
newMessageIndex: this.lastProcessedMessageIndex
|
|
225
|
+
}, "Compression artifact processing completed");
|
|
226
|
+
return toolCallToArtifactMap;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* 3. Create conversation summary with artifact references
|
|
230
|
+
*/
|
|
231
|
+
async createConversationSummary(messages, toolCallToArtifactMap) {
|
|
232
|
+
const textMessages = this.extractTextMessages(messages, toolCallToArtifactMap);
|
|
233
|
+
logger.debug({
|
|
234
|
+
sessionId: this.sessionId,
|
|
235
|
+
messageCount: messages.length,
|
|
236
|
+
textMessageCount: textMessages.length,
|
|
237
|
+
artifactCount: Object.keys(toolCallToArtifactMap).length,
|
|
238
|
+
sampleMessages: messages.slice(0, 2).map((m) => ({
|
|
239
|
+
role: m.role,
|
|
240
|
+
contentType: typeof m.content,
|
|
241
|
+
contentPreview: typeof m.content === "string" ? m.content.substring(0, 100) : "array/object"
|
|
242
|
+
}))
|
|
243
|
+
}, "Starting distillation with debug info");
|
|
244
|
+
const summary = await distillConversation({
|
|
245
|
+
messages,
|
|
246
|
+
conversationId: this.conversationId,
|
|
247
|
+
currentSummary: this.cumulativeSummary,
|
|
248
|
+
summarizerModel: this.summarizerModel,
|
|
249
|
+
toolCallToArtifactMap
|
|
250
|
+
});
|
|
251
|
+
this.cumulativeSummary = summary;
|
|
252
|
+
logger.debug({
|
|
253
|
+
sessionId: this.sessionId,
|
|
254
|
+
summaryGenerated: !!summary,
|
|
255
|
+
summaryHighLevel: summary?.high_level,
|
|
256
|
+
artifactsCount: summary?.related_artifacts?.length || 0
|
|
257
|
+
}, "Distillation completed");
|
|
258
|
+
return {
|
|
259
|
+
text_messages: textMessages,
|
|
260
|
+
summary
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Extract text messages and convert tool calls to descriptive text
|
|
265
|
+
* Avoids API tool-call/tool-result pairing issues while preserving context
|
|
266
|
+
*/
|
|
267
|
+
extractTextMessages(messages, toolCallToArtifactMap) {
|
|
268
|
+
const textMessages = [];
|
|
269
|
+
const toolCallPairs = /* @__PURE__ */ new Map();
|
|
270
|
+
for (const message of messages) if (Array.isArray(message.content)) {
|
|
271
|
+
for (const block of message.content) if (block.type === "tool-call") if (!toolCallPairs.has(block.toolCallId)) toolCallPairs.set(block.toolCallId, {
|
|
272
|
+
call: block,
|
|
273
|
+
result: null
|
|
274
|
+
});
|
|
275
|
+
else toolCallPairs.get(block.toolCallId).call = block;
|
|
276
|
+
else if (block.type === "tool-result") if (!toolCallPairs.has(block.toolCallId)) toolCallPairs.set(block.toolCallId, {
|
|
277
|
+
call: null,
|
|
278
|
+
result: block
|
|
279
|
+
});
|
|
280
|
+
else toolCallPairs.get(block.toolCallId).result = block;
|
|
281
|
+
}
|
|
282
|
+
for (const message of messages) if (message.role === "assistant" && typeof message.content === "string") textMessages.push({
|
|
283
|
+
role: message.role,
|
|
284
|
+
content: message.content
|
|
285
|
+
});
|
|
286
|
+
else if (message.role === "assistant" && Array.isArray(message.content)) {
|
|
287
|
+
const textParts = [];
|
|
288
|
+
const toolCallsInMessage = /* @__PURE__ */ new Set();
|
|
289
|
+
const preservedBlocks = [];
|
|
290
|
+
for (const block of message.content) if (block.type === "text") textParts.push(block.text);
|
|
291
|
+
else if (block.type === "tool-call" && block.toolName === "thinking_complete") {} else if (block.type === "tool-call") toolCallsInMessage.add(block.toolCallId);
|
|
292
|
+
for (const toolCallId of toolCallsInMessage) {
|
|
293
|
+
const pair = toolCallPairs.get(toolCallId);
|
|
294
|
+
const artifactId = toolCallToArtifactMap[toolCallId];
|
|
295
|
+
if (pair?.call) {
|
|
296
|
+
const args = JSON.stringify(pair.call.input);
|
|
297
|
+
const artifactText = artifactId ? ` Results compressed into artifact: ${artifactId}.` : " Results were compressed but not saved.";
|
|
298
|
+
textParts.push(`I called ${pair.call.toolName}(${args}).${artifactText}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (preservedBlocks.length > 0 && textParts.length > 0) {
|
|
302
|
+
const content = [...preservedBlocks];
|
|
303
|
+
if (textParts.length > 0) content.push({
|
|
304
|
+
type: "text",
|
|
305
|
+
text: textParts.join("\n\n")
|
|
306
|
+
});
|
|
307
|
+
textMessages.push({
|
|
308
|
+
role: message.role,
|
|
309
|
+
content
|
|
310
|
+
});
|
|
311
|
+
} else if (preservedBlocks.length > 0) textMessages.push({
|
|
312
|
+
role: message.role,
|
|
313
|
+
content: preservedBlocks
|
|
314
|
+
});
|
|
315
|
+
else if (textParts.length > 0) textMessages.push({
|
|
316
|
+
role: message.role,
|
|
317
|
+
content: textParts.join("\n\n")
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return textMessages;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Check if tool result data is effectively empty
|
|
324
|
+
*/
|
|
325
|
+
isEmpty(toolResultData) {
|
|
326
|
+
if (!toolResultData || typeof toolResultData !== "object") return true;
|
|
327
|
+
const { toolResult } = toolResultData;
|
|
328
|
+
if (!toolResult) return true;
|
|
329
|
+
if (typeof toolResult === "object" && !Array.isArray(toolResult)) {
|
|
330
|
+
const keys = Object.keys(toolResult);
|
|
331
|
+
if (keys.length === 0) return true;
|
|
332
|
+
return keys.every((key) => {
|
|
333
|
+
const value = toolResult[key];
|
|
334
|
+
if (value === null || value === void 0 || value === "") return true;
|
|
335
|
+
if (Array.isArray(value) && value.length === 0) return true;
|
|
336
|
+
if (typeof value === "object" && Object.keys(value).length === 0) return true;
|
|
337
|
+
return false;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
if (Array.isArray(toolResult) && toolResult.length === 0) return true;
|
|
341
|
+
if (typeof toolResult === "string" && toolResult.trim() === "") return true;
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Recursively remove _structureHints from an object
|
|
346
|
+
*/
|
|
347
|
+
removeStructureHints(obj) {
|
|
348
|
+
if (obj === null || obj === void 0) return obj;
|
|
349
|
+
if (Array.isArray(obj)) return obj.map((item) => this.removeStructureHints(item));
|
|
350
|
+
if (typeof obj === "object") {
|
|
351
|
+
const cleaned = {};
|
|
352
|
+
for (const [key, value] of Object.entries(obj)) if (key !== "_structureHints") cleaned[key] = this.removeStructureHints(value);
|
|
353
|
+
return cleaned;
|
|
354
|
+
}
|
|
355
|
+
return obj;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Get current state for debugging
|
|
359
|
+
*/
|
|
360
|
+
getState() {
|
|
361
|
+
return {
|
|
362
|
+
shouldCompress: this.shouldCompress,
|
|
363
|
+
config: this.config
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Get the current compression summary
|
|
368
|
+
*/
|
|
369
|
+
getCompressionSummary() {
|
|
370
|
+
return this.cumulativeSummary;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
//#endregion
|
|
375
|
+
export { MidGenerationCompressor, getCompressionConfigFromEnv };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//#region src/services/PendingToolApprovalManager.d.ts
|
|
2
|
+
interface PendingToolApproval {
|
|
3
|
+
toolCallId: string;
|
|
4
|
+
toolName: string;
|
|
5
|
+
args: any;
|
|
6
|
+
conversationId: string;
|
|
7
|
+
subAgentId: string;
|
|
8
|
+
createdAt: number;
|
|
9
|
+
resolve: (result: {
|
|
10
|
+
approved: boolean;
|
|
11
|
+
reason?: string;
|
|
12
|
+
}) => void;
|
|
13
|
+
reject: (error: Error) => void;
|
|
14
|
+
timeoutId: ReturnType<typeof setTimeout>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Manages pending tool approval requests during agent execution.
|
|
18
|
+
* Similar to ToolSessionManager but for approval workflows.
|
|
19
|
+
* Uses in-memory Map storage with automatic cleanup like OAuth PKCE store.
|
|
20
|
+
*/
|
|
21
|
+
declare class PendingToolApprovalManager {
|
|
22
|
+
private static instance;
|
|
23
|
+
private pendingApprovals;
|
|
24
|
+
private constructor();
|
|
25
|
+
static getInstance(): PendingToolApprovalManager;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new pending approval and return a promise that resolves with approval status
|
|
28
|
+
*/
|
|
29
|
+
waitForApproval(toolCallId: string, toolName: string, args: any, conversationId: string, subAgentId: string): Promise<{
|
|
30
|
+
approved: boolean;
|
|
31
|
+
reason?: string;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Approve a pending tool call
|
|
35
|
+
*/
|
|
36
|
+
approveToolCall(toolCallId: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Deny a pending tool call
|
|
39
|
+
*/
|
|
40
|
+
denyToolCall(toolCallId: string, reason?: string): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Clean up expired approvals (called by interval timer)
|
|
43
|
+
*/
|
|
44
|
+
private cleanupExpiredApprovals;
|
|
45
|
+
/**
|
|
46
|
+
* Get current status for monitoring
|
|
47
|
+
*/
|
|
48
|
+
getStatus(): {
|
|
49
|
+
pendingApprovals: number;
|
|
50
|
+
approvals: {
|
|
51
|
+
toolCallId: string;
|
|
52
|
+
toolName: string;
|
|
53
|
+
conversationId: string;
|
|
54
|
+
subAgentId: string;
|
|
55
|
+
createdAt: number;
|
|
56
|
+
age: number;
|
|
57
|
+
}[];
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
declare const pendingToolApprovalManager: PendingToolApprovalManager;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { PendingToolApproval, PendingToolApprovalManager, pendingToolApprovalManager };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { getLogger } from "../logger.js";
|
|
2
|
+
|
|
3
|
+
//#region src/services/PendingToolApprovalManager.ts
|
|
4
|
+
const logger = getLogger("PendingToolApprovalManager");
|
|
5
|
+
const APPROVAL_CLEANUP_INTERVAL_MS = 120 * 1e3;
|
|
6
|
+
const APPROVAL_TIMEOUT_MS = 600 * 1e3;
|
|
7
|
+
/**
|
|
8
|
+
* Manages pending tool approval requests during agent execution.
|
|
9
|
+
* Similar to ToolSessionManager but for approval workflows.
|
|
10
|
+
* Uses in-memory Map storage with automatic cleanup like OAuth PKCE store.
|
|
11
|
+
*/
|
|
12
|
+
var PendingToolApprovalManager = class PendingToolApprovalManager {
|
|
13
|
+
static instance;
|
|
14
|
+
pendingApprovals = /* @__PURE__ */ new Map();
|
|
15
|
+
constructor() {
|
|
16
|
+
setInterval(() => this.cleanupExpiredApprovals(), APPROVAL_CLEANUP_INTERVAL_MS);
|
|
17
|
+
}
|
|
18
|
+
static getInstance() {
|
|
19
|
+
if (!PendingToolApprovalManager.instance) PendingToolApprovalManager.instance = new PendingToolApprovalManager();
|
|
20
|
+
return PendingToolApprovalManager.instance;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create a new pending approval and return a promise that resolves with approval status
|
|
24
|
+
*/
|
|
25
|
+
async waitForApproval(toolCallId, toolName, args, conversationId, subAgentId) {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const timeoutId = setTimeout(() => {
|
|
28
|
+
this.pendingApprovals.delete(toolCallId);
|
|
29
|
+
resolve({
|
|
30
|
+
approved: false,
|
|
31
|
+
reason: `Tool approval timeout for ${toolName} (${toolCallId})`
|
|
32
|
+
});
|
|
33
|
+
}, APPROVAL_TIMEOUT_MS);
|
|
34
|
+
const approval = {
|
|
35
|
+
toolCallId,
|
|
36
|
+
toolName,
|
|
37
|
+
args,
|
|
38
|
+
conversationId,
|
|
39
|
+
subAgentId,
|
|
40
|
+
createdAt: Date.now(),
|
|
41
|
+
resolve,
|
|
42
|
+
reject,
|
|
43
|
+
timeoutId
|
|
44
|
+
};
|
|
45
|
+
this.pendingApprovals.set(toolCallId, approval);
|
|
46
|
+
logger.info({
|
|
47
|
+
toolCallId,
|
|
48
|
+
toolName,
|
|
49
|
+
conversationId,
|
|
50
|
+
subAgentId
|
|
51
|
+
}, "Tool approval request created, waiting for user response");
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Approve a pending tool call
|
|
56
|
+
*/
|
|
57
|
+
approveToolCall(toolCallId) {
|
|
58
|
+
const approval = this.pendingApprovals.get(toolCallId);
|
|
59
|
+
if (!approval) {
|
|
60
|
+
logger.warn({ toolCallId }, "Tool approval not found or already processed");
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
logger.info({
|
|
64
|
+
toolCallId,
|
|
65
|
+
toolName: approval.toolName,
|
|
66
|
+
conversationId: approval.conversationId
|
|
67
|
+
}, "Tool approved by user, resuming execution");
|
|
68
|
+
clearTimeout(approval.timeoutId);
|
|
69
|
+
this.pendingApprovals.delete(toolCallId);
|
|
70
|
+
approval.resolve({ approved: true });
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Deny a pending tool call
|
|
75
|
+
*/
|
|
76
|
+
denyToolCall(toolCallId, reason) {
|
|
77
|
+
const approval = this.pendingApprovals.get(toolCallId);
|
|
78
|
+
if (!approval) {
|
|
79
|
+
logger.warn({ toolCallId }, "Tool approval not found or already processed");
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
logger.info({
|
|
83
|
+
toolCallId,
|
|
84
|
+
toolName: approval.toolName,
|
|
85
|
+
conversationId: approval.conversationId,
|
|
86
|
+
reason
|
|
87
|
+
}, "Tool execution denied by user");
|
|
88
|
+
clearTimeout(approval.timeoutId);
|
|
89
|
+
this.pendingApprovals.delete(toolCallId);
|
|
90
|
+
approval.resolve({
|
|
91
|
+
approved: false,
|
|
92
|
+
reason: reason || "User denied approval"
|
|
93
|
+
});
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Clean up expired approvals (called by interval timer)
|
|
98
|
+
*/
|
|
99
|
+
cleanupExpiredApprovals() {
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
let cleanedUp = 0;
|
|
102
|
+
for (const [toolCallId, approval] of this.pendingApprovals) if (now - approval.createdAt > APPROVAL_TIMEOUT_MS) {
|
|
103
|
+
clearTimeout(approval.timeoutId);
|
|
104
|
+
this.pendingApprovals.delete(toolCallId);
|
|
105
|
+
approval.resolve({
|
|
106
|
+
approved: false,
|
|
107
|
+
reason: "Tool approval expired"
|
|
108
|
+
});
|
|
109
|
+
cleanedUp++;
|
|
110
|
+
}
|
|
111
|
+
if (cleanedUp > 0) logger.info({ cleanedUp }, "Cleaned up expired tool approvals");
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get current status for monitoring
|
|
115
|
+
*/
|
|
116
|
+
getStatus() {
|
|
117
|
+
return {
|
|
118
|
+
pendingApprovals: this.pendingApprovals.size,
|
|
119
|
+
approvals: Array.from(this.pendingApprovals.values()).map((approval) => ({
|
|
120
|
+
toolCallId: approval.toolCallId,
|
|
121
|
+
toolName: approval.toolName,
|
|
122
|
+
conversationId: approval.conversationId,
|
|
123
|
+
subAgentId: approval.subAgentId,
|
|
124
|
+
createdAt: approval.createdAt,
|
|
125
|
+
age: Date.now() - approval.createdAt
|
|
126
|
+
}))
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const pendingToolApprovalManager = PendingToolApprovalManager.getInstance();
|
|
131
|
+
|
|
132
|
+
//#endregion
|
|
133
|
+
export { PendingToolApprovalManager, pendingToolApprovalManager };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { MessageContent } from "@inkeep/agents-core";
|
|
2
|
+
|
|
3
|
+
//#region src/services/ResponseFormatter.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Response formatter that uses the unified ArtifactParser to convert artifact markers
|
|
7
|
+
* into data parts for consistent artifact handling across all agent responses
|
|
8
|
+
*/
|
|
9
|
+
declare class ResponseFormatter {
|
|
10
|
+
private artifactParser;
|
|
11
|
+
private subAgentId?;
|
|
12
|
+
constructor(tenantId: string, artifactParserOptions?: {
|
|
13
|
+
sessionId?: string;
|
|
14
|
+
taskId?: string;
|
|
15
|
+
projectId?: string;
|
|
16
|
+
contextId?: string;
|
|
17
|
+
artifactComponents?: any[];
|
|
18
|
+
streamRequestId?: string;
|
|
19
|
+
subAgentId?: string;
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* Process structured object response and replace artifact markers with actual artifacts
|
|
23
|
+
*/
|
|
24
|
+
formatObjectResponse(responseObject: any, contextId: string): Promise<MessageContent>;
|
|
25
|
+
/**
|
|
26
|
+
* Process agent response and convert artifact markers to data parts
|
|
27
|
+
*/
|
|
28
|
+
formatResponse(responseText: string, contextId: string): Promise<MessageContent>;
|
|
29
|
+
/**
|
|
30
|
+
* Count unique artifacts in parts array
|
|
31
|
+
*/
|
|
32
|
+
private countUniqueArtifacts;
|
|
33
|
+
/**
|
|
34
|
+
* Log artifacts found in parts to span
|
|
35
|
+
*/
|
|
36
|
+
private logArtifacts;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { ResponseFormatter };
|