wave-agent-sdk 0.13.5 → 0.14.0
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/agent.d.ts +6 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +16 -2
- package/dist/managers/aiManager.d.ts +3 -0
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +93 -8
- package/dist/managers/messageManager.d.ts +15 -0
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +52 -2
- package/dist/managers/messageQueue.d.ts +1 -0
- package/dist/managers/messageQueue.d.ts.map +1 -1
- package/dist/managers/messageQueue.js +8 -0
- package/dist/managers/permissionManager.d.ts +4 -0
- package/dist/managers/permissionManager.d.ts.map +1 -1
- package/dist/managers/permissionManager.js +6 -0
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +23 -17
- package/dist/prompts/index.d.ts +2 -1
- package/dist/prompts/index.d.ts.map +1 -1
- package/dist/prompts/index.js +50 -25
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +11 -1
- package/dist/tools/agentTool.d.ts.map +1 -1
- package/dist/tools/agentTool.js +14 -2
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +27 -5
- package/dist/tools/types.d.ts +1 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/webFetchTool.d.ts.map +1 -1
- package/dist/tools/webFetchTool.js +202 -78
- package/dist/types/messaging.d.ts +1 -0
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/utils/convertMessagesForAPI.js +1 -1
- package/dist/utils/groupMessagesByApiRound.d.ts +24 -0
- package/dist/utils/groupMessagesByApiRound.d.ts.map +1 -0
- package/dist/utils/groupMessagesByApiRound.js +97 -0
- package/dist/utils/messageOperations.d.ts +1 -0
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/microcompact.d.ts +7 -0
- package/dist/utils/microcompact.d.ts.map +1 -0
- package/dist/utils/microcompact.js +78 -0
- package/package.json +2 -1
- package/src/agent.ts +17 -2
- package/src/managers/aiManager.ts +117 -15
- package/src/managers/messageManager.ts +64 -2
- package/src/managers/messageQueue.ts +9 -0
- package/src/managers/permissionManager.ts +7 -0
- package/src/managers/subagentManager.ts +28 -24
- package/src/prompts/index.ts +51 -25
- package/src/services/aiService.ts +14 -1
- package/src/tools/agentTool.ts +14 -2
- package/src/tools/bashTool.ts +27 -5
- package/src/tools/types.ts +1 -0
- package/src/tools/webFetchTool.ts +276 -86
- package/src/types/messaging.ts +1 -0
- package/src/utils/convertMessagesForAPI.ts +1 -1
- package/src/utils/groupMessagesByApiRound.ts +120 -0
- package/src/utils/messageOperations.ts +1 -0
- package/src/utils/microcompact.ts +101 -0
|
@@ -1,30 +1,80 @@
|
|
|
1
1
|
import TurndownService from "turndown";
|
|
2
|
+
import { LRUCache } from "lru-cache";
|
|
2
3
|
import { WEB_FETCH_TOOL_NAME } from "../constants/tools.js";
|
|
3
4
|
import { logger } from "../utils/globalLogger.js";
|
|
5
|
+
// --- Security Limits ---
|
|
6
|
+
const MAX_HTTP_CONTENT_LENGTH = 10 * 1024 * 1024; // 10MB
|
|
7
|
+
const FETCH_TIMEOUT_MS = 60000; // 60s
|
|
8
|
+
const MAX_REDIRECTS = 10;
|
|
9
|
+
const MAX_MARKDOWN_LENGTH = 100000;
|
|
10
|
+
const USER_AGENT = "Wave-User (+https://github.com/netease-lcap/wave-agent)";
|
|
11
|
+
// --- Cache (LRU with 15min TTL, 50MB max) ---
|
|
4
12
|
const CACHE_TTL = 15 * 60 * 1000; // 15 minutes
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
const CACHE_MAX_BYTES = 50 * 1024 * 1024; // 50MB
|
|
14
|
+
const cache = new LRUCache({
|
|
15
|
+
ttl: CACHE_TTL,
|
|
16
|
+
maxSize: CACHE_MAX_BYTES,
|
|
17
|
+
sizeCalculation: (entry) => entry.bytes,
|
|
18
|
+
});
|
|
19
|
+
// --- Helpers ---
|
|
20
|
+
function formatSize(bytes) {
|
|
21
|
+
if (bytes < 1024)
|
|
22
|
+
return `${bytes}B`;
|
|
23
|
+
if (bytes < 1024 * 1024)
|
|
24
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
25
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
26
|
+
}
|
|
27
|
+
function validateURL(url) {
|
|
28
|
+
if (url.length > 2000) {
|
|
29
|
+
return {
|
|
30
|
+
valid: false,
|
|
31
|
+
error: "URL exceeds maximum length of 2000 characters",
|
|
32
|
+
};
|
|
10
33
|
}
|
|
11
|
-
|
|
12
|
-
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = new URL(url);
|
|
13
37
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
38
|
+
catch (error) {
|
|
39
|
+
return {
|
|
40
|
+
valid: false,
|
|
41
|
+
error: `Invalid URL: ${error instanceof Error ? error.message : String(error)}`,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (parsed.username || parsed.password) {
|
|
45
|
+
return { valid: false, error: "URL must not contain username or password" };
|
|
46
|
+
}
|
|
47
|
+
const hostParts = parsed.hostname.split(".");
|
|
48
|
+
if (hostParts.length < 2) {
|
|
49
|
+
return {
|
|
50
|
+
valid: false,
|
|
51
|
+
error: "URL hostname must have at least two parts (e.g., example.com)",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return { valid: true };
|
|
18
55
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
56
|
+
function isPermittedRedirect(originalUrl, redirectUrl) {
|
|
57
|
+
try {
|
|
58
|
+
const original = new URL(originalUrl);
|
|
59
|
+
const redirect = new URL(redirectUrl);
|
|
60
|
+
const origHost = original.host;
|
|
61
|
+
const redirHost = redirect.host;
|
|
62
|
+
// Same host
|
|
63
|
+
if (origHost === redirHost)
|
|
64
|
+
return true;
|
|
65
|
+
// www. variation (e.g., example.com <-> www.example.com)
|
|
66
|
+
const bareOrig = origHost.replace(/^www\./, "");
|
|
67
|
+
const bareRedir = redirHost.replace(/^www\./, "");
|
|
68
|
+
if (bareOrig === bareRedir)
|
|
69
|
+
return true;
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return false;
|
|
26
74
|
}
|
|
27
|
-
}
|
|
75
|
+
}
|
|
76
|
+
const GITHUB_URL_ERROR = "For GitHub URLs, please use the 'gh' CLI via the Bash tool instead (e.g., 'gh pr view', 'gh issue view', 'gh api').";
|
|
77
|
+
// --- Tool ---
|
|
28
78
|
export const webFetchTool = {
|
|
29
79
|
name: WEB_FETCH_TOOL_NAME,
|
|
30
80
|
config: {
|
|
@@ -44,10 +94,11 @@ Usage notes:
|
|
|
44
94
|
- IMPORTANT: If an MCP-provided web fetch tool is available, prefer using that tool instead of this one, as it may have fewer restrictions.
|
|
45
95
|
- The URL must be a fully-formed valid URL
|
|
46
96
|
- HTTP URLs will be automatically upgraded to HTTPS
|
|
97
|
+
- Content exceeding ${formatSize(MAX_MARKDOWN_LENGTH)} will be truncated
|
|
47
98
|
- The prompt should describe what information you want to extract from the page
|
|
48
99
|
- This tool is read-only and does not modify any files
|
|
49
100
|
- Results may be summarized if the content is very large
|
|
50
|
-
- Includes a
|
|
101
|
+
- Includes an LRU cache with a 15-minute TTL for faster responses when repeatedly accessing the same URL
|
|
51
102
|
- When a URL redirects to a different host, the tool will inform you and provide the redirect URL in a special format. You should then make a new WebFetch request with the redirect URL to fetch the content.
|
|
52
103
|
- For GitHub URLs, prefer using the gh CLI via Bash instead (e.g., gh pr view, gh issue view, gh api).`,
|
|
53
104
|
parameters: {
|
|
@@ -82,79 +133,82 @@ Usage notes:
|
|
|
82
133
|
if (url.startsWith("http://")) {
|
|
83
134
|
url = "https://" + url.substring(7);
|
|
84
135
|
}
|
|
136
|
+
// Validate URL
|
|
137
|
+
const validation = validateURL(url);
|
|
138
|
+
if (!validation.valid) {
|
|
139
|
+
return {
|
|
140
|
+
success: false,
|
|
141
|
+
content: "",
|
|
142
|
+
error: validation.error,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
85
145
|
// Check for GitHub URLs
|
|
86
146
|
if (url.includes("github.com")) {
|
|
87
147
|
return {
|
|
88
148
|
success: false,
|
|
89
149
|
content: "",
|
|
90
|
-
error:
|
|
150
|
+
error: GITHUB_URL_ERROR,
|
|
91
151
|
};
|
|
92
152
|
}
|
|
93
153
|
try {
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if
|
|
105
|
-
|
|
106
|
-
const originalHost = new URL(url).host;
|
|
107
|
-
const redirectHost = new URL(redirectUrl).host;
|
|
108
|
-
if (originalHost !== redirectHost) {
|
|
109
|
-
return {
|
|
110
|
-
success: true,
|
|
111
|
-
content: `REDIRECT_TO: ${redirectUrl}\nThe URL redirected to a different host. Please make a new WebFetch request with this redirect URL if you wish to continue.`,
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
// If same host, we could follow it, but the requirement says "When a URL redirects to a different host, the tool will inform you".
|
|
115
|
-
// For simplicity and following the requirement strictly, let's just return the redirect for different hosts.
|
|
116
|
-
// If it's the same host, we can try to fetch again or just return it too.
|
|
117
|
-
return {
|
|
118
|
-
success: true,
|
|
119
|
-
content: `REDIRECT_TO: ${redirectUrl}\nThe URL redirected. Please make a new WebFetch request with this redirect URL.`,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (!response.ok) {
|
|
124
|
-
return {
|
|
125
|
-
success: false,
|
|
126
|
-
content: "",
|
|
127
|
-
error: `Failed to fetch URL: ${response.status} ${response.statusText}`,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
const html = await response.text();
|
|
131
|
-
const turndownService = new TurndownService();
|
|
132
|
-
markdown = turndownService.turndown(html);
|
|
133
|
-
setToCache(url, markdown);
|
|
154
|
+
const cached = cache.get(url);
|
|
155
|
+
if (cached) {
|
|
156
|
+
const markdown = cached.content;
|
|
157
|
+
return processWithAI(url, prompt, markdown, cached.code, cached.codeText, context);
|
|
158
|
+
}
|
|
159
|
+
// Fetch with redirect following
|
|
160
|
+
const result = await fetchWithRedirects(url, context.abortSignal);
|
|
161
|
+
if (result.kind === "redirect") {
|
|
162
|
+
return {
|
|
163
|
+
success: true,
|
|
164
|
+
content: `REDIRECT_TO: ${result.redirectUrl}\nThe URL redirected to a different host. Please make a new WebFetch request with this redirect URL if you wish to continue.`,
|
|
165
|
+
};
|
|
134
166
|
}
|
|
135
|
-
|
|
136
|
-
if (!context.aiManager || !context.aiService) {
|
|
167
|
+
if (result.kind === "error") {
|
|
137
168
|
return {
|
|
138
169
|
success: false,
|
|
139
|
-
content:
|
|
140
|
-
error:
|
|
170
|
+
content: "",
|
|
171
|
+
error: result.error,
|
|
141
172
|
};
|
|
142
173
|
}
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
174
|
+
const { response, finalUrl } = result;
|
|
175
|
+
if (!response.ok) {
|
|
176
|
+
return {
|
|
177
|
+
success: false,
|
|
178
|
+
content: "",
|
|
179
|
+
error: `Failed to fetch URL: ${response.status} ${response.statusText}`,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const contentLengthHeader = response.headers.get("content-length");
|
|
183
|
+
const contentLength = contentLengthHeader
|
|
184
|
+
? parseInt(contentLengthHeader, 10)
|
|
185
|
+
: null;
|
|
186
|
+
if (contentLength !== null && contentLength > MAX_HTTP_CONTENT_LENGTH) {
|
|
187
|
+
return {
|
|
188
|
+
success: false,
|
|
189
|
+
content: "",
|
|
190
|
+
error: `Content too large: ${formatSize(contentLength)} exceeds limit of ${formatSize(MAX_HTTP_CONTENT_LENGTH)}`,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
const html = await response.text();
|
|
194
|
+
const turndownService = new TurndownService();
|
|
195
|
+
let markdown = turndownService.turndown(html);
|
|
196
|
+
const markdownBytes = new TextEncoder().encode(markdown).length;
|
|
197
|
+
// Truncate if too large
|
|
198
|
+
if (markdown.length > MAX_MARKDOWN_LENGTH) {
|
|
199
|
+
markdown =
|
|
200
|
+
markdown.substring(0, MAX_MARKDOWN_LENGTH) +
|
|
201
|
+
`[Content truncated at ${MAX_MARKDOWN_LENGTH} characters due to length limit.]`;
|
|
202
|
+
}
|
|
203
|
+
// Store in LRU cache
|
|
204
|
+
cache.set(finalUrl, {
|
|
205
|
+
bytes: markdownBytes,
|
|
206
|
+
code: response.status,
|
|
207
|
+
codeText: response.statusText,
|
|
148
208
|
content: markdown,
|
|
149
|
-
|
|
150
|
-
model: fastModel,
|
|
151
|
-
abortSignal: context.abortSignal,
|
|
209
|
+
contentType: response.headers.get("content-type") || "",
|
|
152
210
|
});
|
|
153
|
-
return
|
|
154
|
-
success: true,
|
|
155
|
-
content: aiResponse.content || "",
|
|
156
|
-
shortResult: `Processed content from ${url}`,
|
|
157
|
-
};
|
|
211
|
+
return processWithAI(finalUrl, prompt, markdown, response.status, response.statusText, context, markdownBytes);
|
|
158
212
|
}
|
|
159
213
|
catch (error) {
|
|
160
214
|
logger.error(`WebFetch failed for ${url}:`, error);
|
|
@@ -169,3 +223,73 @@ Usage notes:
|
|
|
169
223
|
return `Fetch ${params.url}`;
|
|
170
224
|
},
|
|
171
225
|
};
|
|
226
|
+
// --- Fetch with redirect following ---
|
|
227
|
+
async function fetchWithRedirects(initialUrl, abortSignal, redirectCount = 0) {
|
|
228
|
+
if (redirectCount >= MAX_REDIRECTS) {
|
|
229
|
+
return {
|
|
230
|
+
kind: "error",
|
|
231
|
+
error: `Too many redirects (max ${MAX_REDIRECTS})`,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const controller = new AbortController();
|
|
235
|
+
const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
236
|
+
// Forward the context's abort signal if provided
|
|
237
|
+
if (abortSignal) {
|
|
238
|
+
abortSignal.addEventListener("abort", () => controller.abort(), {
|
|
239
|
+
once: true,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
let response;
|
|
243
|
+
try {
|
|
244
|
+
response = await fetch(initialUrl, {
|
|
245
|
+
redirect: "manual",
|
|
246
|
+
signal: controller.signal,
|
|
247
|
+
headers: {
|
|
248
|
+
"User-Agent": USER_AGENT,
|
|
249
|
+
Accept: "text/markdown, text/html, */*",
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
finally {
|
|
254
|
+
clearTimeout(timeoutId);
|
|
255
|
+
}
|
|
256
|
+
if (response.status >= 300 && response.status < 400) {
|
|
257
|
+
const location = response.headers.get("location");
|
|
258
|
+
if (location) {
|
|
259
|
+
const redirectUrl = new URL(location, initialUrl).toString();
|
|
260
|
+
if (!isPermittedRedirect(initialUrl, redirectUrl)) {
|
|
261
|
+
return { kind: "redirect", redirectUrl };
|
|
262
|
+
}
|
|
263
|
+
// Follow permitted redirect recursively
|
|
264
|
+
return fetchWithRedirects(redirectUrl, abortSignal, redirectCount + 1);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return { kind: "response", response, finalUrl: initialUrl };
|
|
268
|
+
}
|
|
269
|
+
// --- AI Processing ---
|
|
270
|
+
async function processWithAI(url, prompt, markdown, statusCode, statusText, context, contentSize) {
|
|
271
|
+
if (!context.aiManager || !context.aiService) {
|
|
272
|
+
return {
|
|
273
|
+
success: false,
|
|
274
|
+
content: markdown,
|
|
275
|
+
error: "AI Manager or AI Service not available for processing content",
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
const modelConfig = context.aiManager.getModelConfig();
|
|
279
|
+
const fastModel = modelConfig.fastModel;
|
|
280
|
+
const aiResponse = await context.aiService.processWebContent({
|
|
281
|
+
gatewayConfig: context.aiManager.getGatewayConfig(),
|
|
282
|
+
modelConfig: modelConfig,
|
|
283
|
+
content: markdown,
|
|
284
|
+
prompt: prompt,
|
|
285
|
+
model: fastModel,
|
|
286
|
+
abortSignal: context.abortSignal,
|
|
287
|
+
});
|
|
288
|
+
const sizeStr = contentSize !== undefined ? formatSize(contentSize) : "unknown size";
|
|
289
|
+
const statusStr = `${statusCode} ${statusText}`.trim();
|
|
290
|
+
return {
|
|
291
|
+
success: true,
|
|
292
|
+
content: aiResponse.content || "",
|
|
293
|
+
shortResult: `Received ${sizeStr} (${statusStr}) from ${url}`,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messaging.d.ts","sourceRoot":"","sources":["../../src/types/messaging.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAEvC,oBAAY,aAAa;IACvB,IAAI,SAAS;IACb,IAAI,SAAS;CACd;AAED,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC3C,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,MAAM,YAAY,GACpB,SAAS,GACT,UAAU,GACV,SAAS,GACT,UAAU,GACV,SAAS,GACT,aAAa,GACb,cAAc,GACd,gBAAgB,GAChB,qBAAqB,CAAC;AAE1B,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,oB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jC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,OAAO,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,SAAS,GAAG,KAAK,CAAC;IACzB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;CAC7B;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,cAAc,CAAC;IACrB,SAAS,EAAE,OAAO,gBAAgB,EAAE,YAAY,EAAE,CAAC;CACpD;AAED,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,mBAAmB,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC;IAC5B,MAAM,EAAE,WAAW,GAAG,QAAQ,GAAG,QAAQ,CAAC;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB"}
|
|
@@ -42,7 +42,7 @@ export function convertMessagesForAPI(messages) {
|
|
|
42
42
|
const compressBlock = message.blocks.find((block) => block.type === "compress");
|
|
43
43
|
if (compressBlock && compressBlock.type === "compress") {
|
|
44
44
|
recentMessages.unshift({
|
|
45
|
-
role: "
|
|
45
|
+
role: "user",
|
|
46
46
|
content: compressBlock.content,
|
|
47
47
|
});
|
|
48
48
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Message } from "../types/index.js";
|
|
2
|
+
export interface ApiRound {
|
|
3
|
+
messages: Message[];
|
|
4
|
+
estimatedTokens: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Groups messages into "API rounds" — each round corresponds to one API
|
|
8
|
+
* call-response cycle. This is critical because in agentic sessions with a
|
|
9
|
+
* single user prompt, Wave creates a new Message per API round (each recursive
|
|
10
|
+
* sendAIMessage call creates a new assistant message).
|
|
11
|
+
*
|
|
12
|
+
* Boundaries:
|
|
13
|
+
* - A new `role: "user"` message starts a new round.
|
|
14
|
+
* - A new `role: "assistant"` message with a different `id` starts a new round.
|
|
15
|
+
* - A message with a `compress` block is pushed as its own round and starts a
|
|
16
|
+
* new round after it.
|
|
17
|
+
*/
|
|
18
|
+
export declare function groupMessagesByApiRound(messages: Message[]): ApiRound[];
|
|
19
|
+
/**
|
|
20
|
+
* Returns the last `roundCount` complete API rounds as a flat message array.
|
|
21
|
+
* Never splits a tool_use/tool_result pair. If fewer rounds exist, returns all.
|
|
22
|
+
*/
|
|
23
|
+
export declare function getLastApiRounds(messages: Message[], roundCount: number): Message[];
|
|
24
|
+
//# sourceMappingURL=groupMessagesByApiRound.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"groupMessagesByApiRound.d.ts","sourceRoot":"","sources":["../../src/utils/groupMessagesByApiRound.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,WAAW,QAAQ;IACvB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,QAAQ,EAAE,CA8DvE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,OAAO,EAAE,EACnB,UAAU,EAAE,MAAM,GACjB,OAAO,EAAE,CAIX"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups messages into "API rounds" — each round corresponds to one API
|
|
3
|
+
* call-response cycle. This is critical because in agentic sessions with a
|
|
4
|
+
* single user prompt, Wave creates a new Message per API round (each recursive
|
|
5
|
+
* sendAIMessage call creates a new assistant message).
|
|
6
|
+
*
|
|
7
|
+
* Boundaries:
|
|
8
|
+
* - A new `role: "user"` message starts a new round.
|
|
9
|
+
* - A new `role: "assistant"` message with a different `id` starts a new round.
|
|
10
|
+
* - A message with a `compress` block is pushed as its own round and starts a
|
|
11
|
+
* new round after it.
|
|
12
|
+
*/
|
|
13
|
+
export function groupMessagesByApiRound(messages) {
|
|
14
|
+
const rounds = [];
|
|
15
|
+
let currentRound = [];
|
|
16
|
+
let lastAssistantId;
|
|
17
|
+
for (const msg of messages) {
|
|
18
|
+
let startNewRound = false;
|
|
19
|
+
if (msg.role === "user") {
|
|
20
|
+
startNewRound = true;
|
|
21
|
+
}
|
|
22
|
+
else if (msg.role === "assistant") {
|
|
23
|
+
// Compress block is always its own round
|
|
24
|
+
const hasCompress = msg.blocks.some((b) => b.type === "compress");
|
|
25
|
+
if (hasCompress) {
|
|
26
|
+
startNewRound = true;
|
|
27
|
+
}
|
|
28
|
+
else if (msg.id !== lastAssistantId) {
|
|
29
|
+
// New assistant id starts a new round.
|
|
30
|
+
// Exception: if the current round is [user] (first assistant after a
|
|
31
|
+
// user prompt in a normal conversation), keep them together as one
|
|
32
|
+
// round. But if we already have assistant(s) in this round (agentic
|
|
33
|
+
// tool loop), the new id starts a new round.
|
|
34
|
+
const roundHasOtherAssistant = currentRound.some((m) => m.role === "assistant" && m.id !== msg.id);
|
|
35
|
+
if (roundHasOtherAssistant) {
|
|
36
|
+
startNewRound = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
lastAssistantId = msg.id;
|
|
40
|
+
}
|
|
41
|
+
if (startNewRound && currentRound.length > 0) {
|
|
42
|
+
rounds.push({
|
|
43
|
+
messages: currentRound,
|
|
44
|
+
estimatedTokens: estimateTokens(currentRound),
|
|
45
|
+
});
|
|
46
|
+
currentRound = [];
|
|
47
|
+
}
|
|
48
|
+
currentRound.push(msg);
|
|
49
|
+
// After pushing a compress message as its own round, flush immediately
|
|
50
|
+
if (msg.role === "assistant" &&
|
|
51
|
+
msg.blocks.some((b) => b.type === "compress")) {
|
|
52
|
+
rounds.push({
|
|
53
|
+
messages: currentRound,
|
|
54
|
+
estimatedTokens: estimateTokens(currentRound),
|
|
55
|
+
});
|
|
56
|
+
currentRound = [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (currentRound.length > 0) {
|
|
60
|
+
rounds.push({
|
|
61
|
+
messages: currentRound,
|
|
62
|
+
estimatedTokens: estimateTokens(currentRound),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return rounds;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Returns the last `roundCount` complete API rounds as a flat message array.
|
|
69
|
+
* Never splits a tool_use/tool_result pair. If fewer rounds exist, returns all.
|
|
70
|
+
*/
|
|
71
|
+
export function getLastApiRounds(messages, roundCount) {
|
|
72
|
+
const rounds = groupMessagesByApiRound(messages);
|
|
73
|
+
const lastRounds = rounds.slice(-roundCount);
|
|
74
|
+
return lastRounds.flatMap((r) => r.messages);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Roughly estimate token count from character count (~4 chars per token).
|
|
78
|
+
*/
|
|
79
|
+
function estimateTokens(messages) {
|
|
80
|
+
let chars = 0;
|
|
81
|
+
for (const msg of messages) {
|
|
82
|
+
for (const block of msg.blocks) {
|
|
83
|
+
if ("content" in block && typeof block.content === "string") {
|
|
84
|
+
chars += block.content.length;
|
|
85
|
+
}
|
|
86
|
+
if (block.type === "tool" &&
|
|
87
|
+
block.parameters &&
|
|
88
|
+
typeof block.parameters === "string") {
|
|
89
|
+
chars += block.parameters.length;
|
|
90
|
+
}
|
|
91
|
+
if (block.type === "tool" && block.result) {
|
|
92
|
+
chars += block.result.length;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return Math.ceil(chars / 4);
|
|
97
|
+
}
|
|
@@ -41,6 +41,7 @@ export interface UpdateToolBlockParams {
|
|
|
41
41
|
compactParams?: string;
|
|
42
42
|
parametersChunk?: string;
|
|
43
43
|
isManuallyBackgrounded?: boolean;
|
|
44
|
+
timestamp?: number;
|
|
44
45
|
}
|
|
45
46
|
export type AgentToolBlockUpdateParams = Omit<UpdateToolBlockParams, "messages">;
|
|
46
47
|
export interface AddErrorBlockParams {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messageOperations.d.ts","sourceRoot":"","sources":["../../src/utils/messageOperations.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,OAAO,EACP,KAAK,EAGN,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGlD,OAAO,EAAE,qCAAqC,EAAE,MAAM,qBAAqB,CAAC;AAI5E,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,MAAM,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACnD,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAGD,MAAM,WAAW,oBAAqB,SAAQ,iBAAiB;IAC7D,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,CAAC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CAAC;AAEF;;GAEG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,MAAM,CAU3D;AAED;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAAI,UAAU,OAAO,EAAE,KAAG,OAAO,EASlE,CAAC;AAEF;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAoBtD;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,MAAM,CAUR;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAoB1D;AAED,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC;IAC5B,MAAM,EAAE,WAAW,GAAG,QAAQ,GAAG,QAAQ,CAAC;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,gCAAgC,GAAI,8DAO9C,4BAA4B,KAAG,OAAO,EAiBxC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Message } from "../types/messaging.js";
|
|
2
|
+
export interface MicrocompactOptions {
|
|
3
|
+
timeThresholdMS: number;
|
|
4
|
+
recentResultsToKeep: number;
|
|
5
|
+
}
|
|
6
|
+
export declare function microcompactMessages(messages: Message[], options: MicrocompactOptions): Message[];
|
|
7
|
+
//# sourceMappingURL=microcompact.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"microcompact.d.ts","sourceRoot":"","sources":["../../src/utils/microcompact.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAa,MAAM,uBAAuB,CAAC;AAEhE,MAAM,WAAW,mBAAmB;IAClC,eAAe,EAAE,MAAM,CAAC;IACxB,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAID,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,OAAO,EAAE,EACnB,OAAO,EAAE,mBAAmB,GAC3B,OAAO,EAAE,CAwFX"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const CLEARED_RESULT = "[Old tool result content cleared]";
|
|
2
|
+
export function microcompactMessages(messages, options) {
|
|
3
|
+
const { timeThresholdMS, recentResultsToKeep } = options;
|
|
4
|
+
// 1. Find the latest tool block timestamp across all assistant messages
|
|
5
|
+
let lastAssistantTime = 0;
|
|
6
|
+
for (const msg of messages) {
|
|
7
|
+
if (msg.role === "assistant") {
|
|
8
|
+
for (const block of msg.blocks) {
|
|
9
|
+
if (block.type === "tool" && block.stage === "end" && block.timestamp) {
|
|
10
|
+
if (block.timestamp > lastAssistantTime) {
|
|
11
|
+
lastAssistantTime = block.timestamp;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// 2. If no prior assistant messages with completed tools, return unchanged
|
|
18
|
+
if (lastAssistantTime === 0) {
|
|
19
|
+
return messages;
|
|
20
|
+
}
|
|
21
|
+
// 3. If within threshold, return unchanged
|
|
22
|
+
if (Date.now() - lastAssistantTime < timeThresholdMS) {
|
|
23
|
+
return messages;
|
|
24
|
+
}
|
|
25
|
+
const toolRefs = [];
|
|
26
|
+
for (let mi = 0; mi < messages.length; mi++) {
|
|
27
|
+
const msg = messages[mi];
|
|
28
|
+
if (msg.role === "assistant") {
|
|
29
|
+
for (let bi = 0; bi < msg.blocks.length; bi++) {
|
|
30
|
+
const block = msg.blocks[bi];
|
|
31
|
+
if (block.type === "tool" && block.stage === "end" && block.timestamp) {
|
|
32
|
+
toolRefs.push({
|
|
33
|
+
msgIndex: mi,
|
|
34
|
+
blockIndex: bi,
|
|
35
|
+
timestamp: block.timestamp,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
toolRefs.sort((a, b) => b.timestamp - a.timestamp);
|
|
42
|
+
// 5. Mark the top N as "keep"
|
|
43
|
+
const keepSet = new Set();
|
|
44
|
+
for (let i = 0; i < Math.min(recentResultsToKeep, toolRefs.length); i++) {
|
|
45
|
+
const ref = toolRefs[i];
|
|
46
|
+
keepSet.add(`${ref.msgIndex}:${ref.blockIndex}`);
|
|
47
|
+
}
|
|
48
|
+
// 6. Deep-copy messages and clear result + shortResult on non-kept blocks
|
|
49
|
+
const result = messages.map((msg) => ({
|
|
50
|
+
...msg,
|
|
51
|
+
blocks: msg.blocks.map((block) => {
|
|
52
|
+
if (block.type === "tool" && block.stage === "end" && block.timestamp) {
|
|
53
|
+
return { ...block };
|
|
54
|
+
}
|
|
55
|
+
return block;
|
|
56
|
+
}),
|
|
57
|
+
}));
|
|
58
|
+
// Clear non-kept tool blocks
|
|
59
|
+
for (const ref of toolRefs) {
|
|
60
|
+
const key = `${ref.msgIndex}:${ref.blockIndex}`;
|
|
61
|
+
if (!keepSet.has(key)) {
|
|
62
|
+
result[ref.msgIndex] = {
|
|
63
|
+
...result[ref.msgIndex],
|
|
64
|
+
blocks: result[ref.msgIndex].blocks.map((b, idx) => {
|
|
65
|
+
if (idx === ref.blockIndex && b.type === "tool") {
|
|
66
|
+
return {
|
|
67
|
+
...b,
|
|
68
|
+
result: CLEARED_RESULT,
|
|
69
|
+
shortResult: undefined,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return b;
|
|
73
|
+
}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wave-agent-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "SDK for building AI-powered development tools and agents",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"cron-parser": "^5.5.0",
|
|
34
34
|
"fuzzysort": "^3.1.0",
|
|
35
35
|
"glob": "^13.0.0",
|
|
36
|
+
"lru-cache": "^11.3.5",
|
|
36
37
|
"minimatch": "^10.0.3",
|
|
37
38
|
"openai": "^5.12.2",
|
|
38
39
|
"turndown": "^7.2.2"
|
package/src/agent.ts
CHANGED
|
@@ -332,6 +332,19 @@ export class Agent {
|
|
|
332
332
|
return this.messageQueue.getQueue();
|
|
333
333
|
}
|
|
334
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Remove a queued message by index
|
|
337
|
+
* @param index - The index of the message to remove
|
|
338
|
+
* @returns true if the message was removed, false if the index was out of bounds
|
|
339
|
+
*/
|
|
340
|
+
public removeQueuedMessage(index: number): boolean {
|
|
341
|
+
const removed = this.messageQueue.removeAt(index);
|
|
342
|
+
if (removed) {
|
|
343
|
+
this.options.callbacks?.onQueuedMessagesChange?.(this.queuedMessages);
|
|
344
|
+
}
|
|
345
|
+
return removed;
|
|
346
|
+
}
|
|
347
|
+
|
|
335
348
|
/**
|
|
336
349
|
* Process the next queued message when the agent becomes idle.
|
|
337
350
|
* Dequeues the next message and handles it based on type.
|
|
@@ -566,11 +579,13 @@ export class Agent {
|
|
|
566
579
|
|
|
567
580
|
/** Unified interrupt method, interrupts both AI messages and command execution */
|
|
568
581
|
public abortMessage(): void {
|
|
582
|
+
// Clear queue first to prevent processQueuedMessage from dequeuing
|
|
583
|
+
// when abortAIMessage triggers onLoadingChange(false)
|
|
584
|
+
this.messageQueue.clear();
|
|
585
|
+
this.options.callbacks?.onQueuedMessagesChange?.(this.queuedMessages);
|
|
569
586
|
this.abortAIMessage(); // This will abort tools including Agent tool (subagents)
|
|
570
587
|
this.abortBashCommand();
|
|
571
588
|
this.abortSlashCommand();
|
|
572
|
-
this.messageQueue.clear();
|
|
573
|
-
this.options.callbacks?.onQueuedMessagesChange?.(this.queuedMessages);
|
|
574
589
|
}
|
|
575
590
|
|
|
576
591
|
/** Interrupt bash command execution */
|