@iinm/plain-agent 1.8.4 → 1.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/plain +1 -1
- package/package.json +7 -5
- package/sandbox/bin/plain-sandbox +13 -0
- package/src/agent.d.ts +52 -0
- package/src/agent.mjs +204 -0
- package/src/agentLoop.mjs +419 -0
- package/src/agentState.mjs +41 -0
- package/src/claudeCodePlugin.mjs +164 -0
- package/src/cliArgs.mjs +175 -0
- package/src/cliBatch.mjs +147 -0
- package/src/cliCommands.mjs +283 -0
- package/src/cliCompleter.mjs +227 -0
- package/src/cliCost.mjs +309 -0
- package/src/cliFormatter.mjs +413 -0
- package/src/cliInteractive.mjs +529 -0
- package/src/cliInterruptTransform.mjs +51 -0
- package/src/cliMuteTransform.mjs +26 -0
- package/src/cliPasteTransform.mjs +183 -0
- package/src/config.d.ts +36 -0
- package/src/config.mjs +197 -0
- package/src/context/loadAgentRoles.mjs +294 -0
- package/src/context/loadPrompts.mjs +337 -0
- package/src/context/loadUserMessageContext.mjs +147 -0
- package/src/costTracker.mjs +210 -0
- package/src/env.mjs +44 -0
- package/src/main.mjs +281 -0
- package/src/mcpClient.mjs +351 -0
- package/src/mcpIntegration.mjs +160 -0
- package/src/model.d.ts +109 -0
- package/src/modelCaller.mjs +32 -0
- package/src/modelDefinition.d.ts +92 -0
- package/src/prompt.mjs +138 -0
- package/src/providers/anthropic.d.ts +248 -0
- package/src/providers/anthropic.mjs +587 -0
- package/src/providers/bedrock.d.ts +249 -0
- package/src/providers/bedrock.mjs +700 -0
- package/src/providers/gemini.d.ts +208 -0
- package/src/providers/gemini.mjs +754 -0
- package/src/providers/openai.d.ts +281 -0
- package/src/providers/openai.mjs +544 -0
- package/src/providers/openaiCompatible.d.ts +147 -0
- package/src/providers/openaiCompatible.mjs +652 -0
- package/src/providers/platform/awsSigV4.mjs +184 -0
- package/src/providers/platform/azure.mjs +42 -0
- package/src/providers/platform/bedrock.mjs +78 -0
- package/src/providers/platform/googleCloud.mjs +34 -0
- package/src/subagent.mjs +265 -0
- package/src/tmpfile.mjs +27 -0
- package/src/tool.d.ts +74 -0
- package/src/toolExecutor.mjs +236 -0
- package/src/toolInputValidator.mjs +183 -0
- package/src/toolUseApprover.mjs +99 -0
- package/src/tools/askURL.mjs +209 -0
- package/src/tools/askWeb.mjs +208 -0
- package/src/tools/compactContext.d.ts +4 -0
- package/src/tools/compactContext.mjs +87 -0
- package/src/tools/execCommand.d.ts +22 -0
- package/src/tools/execCommand.mjs +200 -0
- package/src/tools/patchFile.d.ts +4 -0
- package/src/tools/patchFile.mjs +133 -0
- package/src/tools/switchToMainAgent.d.ts +3 -0
- package/src/tools/switchToMainAgent.mjs +43 -0
- package/src/tools/switchToSubagent.d.ts +4 -0
- package/src/tools/switchToSubagent.mjs +59 -0
- package/src/tools/tmuxCommand.d.ts +14 -0
- package/src/tools/tmuxCommand.mjs +194 -0
- package/src/tools/writeFile.d.ts +4 -0
- package/src/tools/writeFile.mjs +56 -0
- package/src/usageStore.mjs +167 -0
- package/src/utils/evalJSONConfig.mjs +72 -0
- package/src/utils/matchValue.d.ts +6 -0
- package/src/utils/matchValue.mjs +40 -0
- package/src/utils/noThrow.mjs +31 -0
- package/src/utils/notify.mjs +29 -0
- package/src/utils/parseFileRange.mjs +18 -0
- package/src/utils/readFileRange.mjs +33 -0
- package/src/utils/retryOnError.mjs +41 -0
- package/src/voiceInput.mjs +61 -0
- package/src/voiceInputGemini.mjs +105 -0
- package/src/voiceInputOpenAI.mjs +104 -0
- package/src/voiceInputSession.mjs +543 -0
- package/src/voiceToggleKey.mjs +62 -0
- package/dist/main.mjs +0 -473
- package/dist/main.mjs.map +0 -7
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Tool } from '../tool'
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { styleText } from "node:util";
|
|
6
|
+
import { getGoogleCloudAccessToken } from "../providers/platform/googleCloud.mjs";
|
|
7
|
+
import { noThrow } from "../utils/noThrow.mjs";
|
|
8
|
+
|
|
9
|
+
/** @typedef {AskURLToolGeminiOptions | AskURLToolGeminiVertexAIOptions} AskURLToolOptions */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {Object} AskURLToolGeminiOptions
|
|
13
|
+
* @property {"gemini"} provider
|
|
14
|
+
* @property {string=} baseURL
|
|
15
|
+
* @property {string} apiKey
|
|
16
|
+
* @property {string} model
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {Object} AskURLToolGeminiVertexAIOptions
|
|
21
|
+
* @property {"gemini-vertex-ai"} provider
|
|
22
|
+
* @property {string} baseURL
|
|
23
|
+
* @property {string=} account
|
|
24
|
+
* @property {string} model
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {Object} AskURLInput
|
|
29
|
+
* @property {string} question
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {AskURLToolOptions} config
|
|
34
|
+
* @returns {Tool}
|
|
35
|
+
*/
|
|
36
|
+
export function createAskURLTool(config) {
|
|
37
|
+
/**
|
|
38
|
+
* @param {AskURLInput} input
|
|
39
|
+
* @param {number} retryCount
|
|
40
|
+
* @returns {Promise<string | Error>}
|
|
41
|
+
*/
|
|
42
|
+
async function askURL(input, retryCount = 0) {
|
|
43
|
+
const model = config.model ?? "gemini-3-flash-preview";
|
|
44
|
+
const url =
|
|
45
|
+
config.provider === "gemini-vertex-ai"
|
|
46
|
+
? `${config.baseURL}/publishers/google/models/${config.model}:generateContent`
|
|
47
|
+
: config.baseURL
|
|
48
|
+
? `${config.baseURL}/models/${model}:generateContent`
|
|
49
|
+
: `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
|
|
50
|
+
|
|
51
|
+
/** @type {Record<string,string>} */
|
|
52
|
+
const authHeader =
|
|
53
|
+
config.provider === "gemini-vertex-ai"
|
|
54
|
+
? {
|
|
55
|
+
Authorization: `Bearer ${await getGoogleCloudAccessToken(config.account)}`,
|
|
56
|
+
}
|
|
57
|
+
: {
|
|
58
|
+
"x-goog-api-key": config.apiKey ?? "",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const data = {
|
|
62
|
+
contents: [
|
|
63
|
+
{
|
|
64
|
+
role: "user",
|
|
65
|
+
parts: [
|
|
66
|
+
{
|
|
67
|
+
text: `I need a comprehensive answer to this question. Please note that I don't have access to external URLs, so include all relevant facts, data, or explanations directly in your response. Avoid referencing links I can't open.
|
|
68
|
+
|
|
69
|
+
Question: ${input.question}`,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
tools: [
|
|
75
|
+
{
|
|
76
|
+
url_context: {},
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const response = await fetch(url, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
...authHeader,
|
|
85
|
+
"Content-Type": "application/json",
|
|
86
|
+
},
|
|
87
|
+
body: JSON.stringify(data),
|
|
88
|
+
signal: AbortSignal.timeout(120 * 1000),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
if (response.status === 429 || response.status >= 500) {
|
|
92
|
+
const interval = Math.min(2 * 2 ** retryCount, 16);
|
|
93
|
+
console.error(
|
|
94
|
+
styleText(
|
|
95
|
+
"yellow",
|
|
96
|
+
`Google API returned ${response.status}. Retrying in ${interval} seconds...`,
|
|
97
|
+
),
|
|
98
|
+
);
|
|
99
|
+
await new Promise((resolve) => setTimeout(resolve, interval * 1000));
|
|
100
|
+
return askURL(input, retryCount + 1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!response.ok) {
|
|
104
|
+
return new Error(
|
|
105
|
+
`Failed to ask Web: status=${response.status}, body=${await response.text()}`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const body = await response.json();
|
|
110
|
+
|
|
111
|
+
const candidate = body.candidates?.[0];
|
|
112
|
+
const text = candidate?.content?.parts?.[0]?.text;
|
|
113
|
+
/** @type {{segment?:{startIndex:number,endIndex:number,text:string},groundingChunkIndices?:number[]}[] | undefined} */
|
|
114
|
+
const supports = candidate?.groundingMetadata?.groundingSupports;
|
|
115
|
+
/** @type {{web?:{uri:string,title:string}}[] | undefined} */
|
|
116
|
+
const chunks = candidate?.groundingMetadata?.groundingChunks;
|
|
117
|
+
|
|
118
|
+
if (typeof text !== "string") {
|
|
119
|
+
return new Error(
|
|
120
|
+
`Unexpected response format from Google: ${JSON.stringify(body)}`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {string} source
|
|
126
|
+
* @param {number} byteIndex
|
|
127
|
+
* @param {string} insertText
|
|
128
|
+
*/
|
|
129
|
+
const insertTextAtUtf8ByteIndex = (source, byteIndex, insertText) => {
|
|
130
|
+
const sourceBuffer = Buffer.from(source, "utf8");
|
|
131
|
+
const normalizedByteIndex = Math.max(
|
|
132
|
+
0,
|
|
133
|
+
Math.min(byteIndex, sourceBuffer.length),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
return Buffer.concat([
|
|
137
|
+
sourceBuffer.subarray(0, normalizedByteIndex),
|
|
138
|
+
Buffer.from(insertText, "utf8"),
|
|
139
|
+
sourceBuffer.subarray(normalizedByteIndex),
|
|
140
|
+
]).toString("utf8");
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Sort by end_index desc because Gemini grounding indexes are byte offsets
|
|
144
|
+
// into the original UTF-8 text.
|
|
145
|
+
const sortedSupports = supports?.toSorted(
|
|
146
|
+
(a, b) => (b.segment?.endIndex ?? 0) - (a.segment?.endIndex ?? 0),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// Insert citations using UTF-8 byte offsets.
|
|
150
|
+
let textWithCitations = text;
|
|
151
|
+
for (const support of sortedSupports ?? []) {
|
|
152
|
+
const endIndex = support.segment?.endIndex;
|
|
153
|
+
if (
|
|
154
|
+
typeof endIndex !== "number" ||
|
|
155
|
+
!support.groundingChunkIndices?.length
|
|
156
|
+
) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
textWithCitations = insertTextAtUtf8ByteIndex(
|
|
161
|
+
textWithCitations,
|
|
162
|
+
endIndex,
|
|
163
|
+
` [${support.groundingChunkIndices.map((i) => i + 1).join(", ")}] `,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const chunkString = (chunks ?? [])
|
|
168
|
+
.map(
|
|
169
|
+
(chunk, index) =>
|
|
170
|
+
`- [${index + 1} - ${chunk.web?.title}](${chunk.web?.uri})`,
|
|
171
|
+
)
|
|
172
|
+
.join("\n");
|
|
173
|
+
|
|
174
|
+
return [textWithCitations, chunkString].join("\n\n");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
def: {
|
|
179
|
+
name: "ask_url",
|
|
180
|
+
description:
|
|
181
|
+
"Use one or more provided URLs to answer a question. Include the URLs in your question.",
|
|
182
|
+
inputSchema: {
|
|
183
|
+
type: "object",
|
|
184
|
+
properties: {
|
|
185
|
+
question: {
|
|
186
|
+
type: "string",
|
|
187
|
+
description:
|
|
188
|
+
"The question to ask, including one or more URLs to use as context.",
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
required: ["question"],
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @param {AskURLInput} input
|
|
197
|
+
* @returns {Promise<string | Error>}
|
|
198
|
+
*/
|
|
199
|
+
impl: async (input) => await noThrow(async () => askURL(input, 0)),
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @param {Record<string, unknown>} _input
|
|
203
|
+
* @returns {Record<string, unknown>}
|
|
204
|
+
*/
|
|
205
|
+
maskApprovalInput: (_input) => {
|
|
206
|
+
return {};
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Tool } from '../tool'
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { styleText } from "node:util";
|
|
6
|
+
import { getGoogleCloudAccessToken } from "../providers/platform/googleCloud.mjs";
|
|
7
|
+
import { noThrow } from "../utils/noThrow.mjs";
|
|
8
|
+
|
|
9
|
+
/** @typedef {AskWebToolGeminiOptions | AskWebToolGeminiVertexAIOptions} AskWebToolOptions */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {Object} AskWebToolGeminiOptions
|
|
13
|
+
* @property {"gemini"} provider
|
|
14
|
+
* @property {string=} baseURL
|
|
15
|
+
* @property {string} apiKey
|
|
16
|
+
* @property {string} model
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {Object} AskWebToolGeminiVertexAIOptions
|
|
21
|
+
* @property {"gemini-vertex-ai"} provider
|
|
22
|
+
* @property {string} baseURL
|
|
23
|
+
* @property {string=} account
|
|
24
|
+
* @property {string} model
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {Object} AskWebInput
|
|
29
|
+
* @property {string} question
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {AskWebToolOptions} config
|
|
34
|
+
* @returns {Tool}
|
|
35
|
+
*/
|
|
36
|
+
export function createAskWebTool(config) {
|
|
37
|
+
/**
|
|
38
|
+
* @param {AskWebInput} input
|
|
39
|
+
* @param {number} retryCount
|
|
40
|
+
* @returns {Promise<string | Error>}
|
|
41
|
+
*/
|
|
42
|
+
async function askWeb(input, retryCount = 0) {
|
|
43
|
+
const model = config.model ?? "gemini-3-flash-preview";
|
|
44
|
+
const url =
|
|
45
|
+
config.provider === "gemini-vertex-ai"
|
|
46
|
+
? `${config.baseURL}/publishers/google/models/${config.model}:generateContent`
|
|
47
|
+
: config.baseURL
|
|
48
|
+
? `${config.baseURL}/models/${model}:generateContent`
|
|
49
|
+
: `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
|
|
50
|
+
|
|
51
|
+
/** @type {Record<string,string>} */
|
|
52
|
+
const authHeader =
|
|
53
|
+
config.provider === "gemini-vertex-ai"
|
|
54
|
+
? {
|
|
55
|
+
Authorization: `Bearer ${await getGoogleCloudAccessToken(config.account)}`,
|
|
56
|
+
}
|
|
57
|
+
: {
|
|
58
|
+
"x-goog-api-key": config.apiKey ?? "",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const data = {
|
|
62
|
+
contents: [
|
|
63
|
+
{
|
|
64
|
+
role: "user",
|
|
65
|
+
parts: [
|
|
66
|
+
{
|
|
67
|
+
text: `I need a comprehensive answer to this question. Please note that I don't have access to external URLs, so include all relevant facts, data, or explanations directly in your response. Avoid referencing links I can't open.
|
|
68
|
+
|
|
69
|
+
Question: ${input.question}`,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
tools: [
|
|
75
|
+
{
|
|
76
|
+
google_search: {},
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const response = await fetch(url, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
...authHeader,
|
|
85
|
+
"Content-Type": "application/json",
|
|
86
|
+
},
|
|
87
|
+
body: JSON.stringify(data),
|
|
88
|
+
signal: AbortSignal.timeout(120 * 1000),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
if (response.status === 429 || response.status >= 500) {
|
|
92
|
+
const interval = Math.min(2 * 2 ** retryCount, 16);
|
|
93
|
+
console.error(
|
|
94
|
+
styleText(
|
|
95
|
+
"yellow",
|
|
96
|
+
`Google API returned ${response.status}. Retrying in ${interval} seconds...`,
|
|
97
|
+
),
|
|
98
|
+
);
|
|
99
|
+
await new Promise((resolve) => setTimeout(resolve, interval * 1000));
|
|
100
|
+
return askWeb(input, retryCount + 1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!response.ok) {
|
|
104
|
+
return new Error(
|
|
105
|
+
`Failed to ask Web: status=${response.status}, body=${await response.text()}`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const body = await response.json();
|
|
110
|
+
|
|
111
|
+
const candidate = body.candidates?.[0];
|
|
112
|
+
const text = candidate?.content?.parts?.[0]?.text;
|
|
113
|
+
/** @type {{segment?:{startIndex:number,endIndex:number,text:string},groundingChunkIndices?:number[]}[] | undefined} */
|
|
114
|
+
const supports = candidate?.groundingMetadata?.groundingSupports;
|
|
115
|
+
/** @type {{web?:{uri:string,title:string}}[] | undefined} */
|
|
116
|
+
const chunks = candidate?.groundingMetadata?.groundingChunks;
|
|
117
|
+
|
|
118
|
+
if (typeof text !== "string") {
|
|
119
|
+
return new Error(
|
|
120
|
+
`Unexpected response format from Google: ${JSON.stringify(body)}`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {string} source
|
|
126
|
+
* @param {number} byteIndex
|
|
127
|
+
* @param {string} insertText
|
|
128
|
+
*/
|
|
129
|
+
const insertTextAtUtf8ByteIndex = (source, byteIndex, insertText) => {
|
|
130
|
+
const sourceBuffer = Buffer.from(source, "utf8");
|
|
131
|
+
const normalizedByteIndex = Math.max(
|
|
132
|
+
0,
|
|
133
|
+
Math.min(byteIndex, sourceBuffer.length),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
return Buffer.concat([
|
|
137
|
+
sourceBuffer.subarray(0, normalizedByteIndex),
|
|
138
|
+
Buffer.from(insertText, "utf8"),
|
|
139
|
+
sourceBuffer.subarray(normalizedByteIndex),
|
|
140
|
+
]).toString("utf8");
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Sort by end_index desc because Gemini grounding indexes are byte offsets
|
|
144
|
+
// into the original UTF-8 text.
|
|
145
|
+
const sortedSupports = supports?.toSorted(
|
|
146
|
+
(a, b) => (b.segment?.endIndex ?? 0) - (a.segment?.endIndex ?? 0),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// Insert citations using UTF-8 byte offsets.
|
|
150
|
+
let textWithCitations = text;
|
|
151
|
+
for (const support of sortedSupports ?? []) {
|
|
152
|
+
const endIndex = support.segment?.endIndex;
|
|
153
|
+
if (
|
|
154
|
+
typeof endIndex !== "number" ||
|
|
155
|
+
!support.groundingChunkIndices?.length
|
|
156
|
+
) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
textWithCitations = insertTextAtUtf8ByteIndex(
|
|
161
|
+
textWithCitations,
|
|
162
|
+
endIndex,
|
|
163
|
+
` [${support.groundingChunkIndices.map((i) => i + 1).join(", ")}] `,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const chunkString = (chunks ?? [])
|
|
168
|
+
.map(
|
|
169
|
+
(chunk, index) =>
|
|
170
|
+
`- [${index + 1} - ${chunk.web?.title}](${chunk.web?.uri})`,
|
|
171
|
+
)
|
|
172
|
+
.join("\n");
|
|
173
|
+
|
|
174
|
+
return [textWithCitations, chunkString].join("\n\n");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
def: {
|
|
179
|
+
name: "ask_web",
|
|
180
|
+
description:
|
|
181
|
+
"Use the web search to answer questions that need up-to-date information or supporting sources.",
|
|
182
|
+
inputSchema: {
|
|
183
|
+
type: "object",
|
|
184
|
+
properties: {
|
|
185
|
+
question: {
|
|
186
|
+
type: "string",
|
|
187
|
+
description: "The question to ask",
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
required: ["question"],
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {AskWebInput} input
|
|
196
|
+
* @returns {Promise<string | Error>}
|
|
197
|
+
*/
|
|
198
|
+
impl: async (input) => await noThrow(async () => askWeb(input, 0)),
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @param {Record<string, unknown>} _input
|
|
202
|
+
* @returns {Record<string, unknown>}
|
|
203
|
+
*/
|
|
204
|
+
maskApprovalInput: (_input) => {
|
|
205
|
+
return {};
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Tool, ToolImplementation } from '../tool'
|
|
3
|
+
* @import { CompactContextInput } from './compactContext'
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { AGENT_MEMORY_DIR } from "../env.mjs";
|
|
9
|
+
import { noThrow } from "../utils/noThrow.mjs";
|
|
10
|
+
|
|
11
|
+
export const compactContextToolName = "compact_context";
|
|
12
|
+
|
|
13
|
+
/** @returns {Tool} */
|
|
14
|
+
export function createCompactContextTool() {
|
|
15
|
+
/** @type {ToolImplementation} */
|
|
16
|
+
let impl = async () => {
|
|
17
|
+
throw new Error("Not implemented");
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** @type {Tool} */
|
|
21
|
+
const tool = {
|
|
22
|
+
def: {
|
|
23
|
+
name: compactContextToolName,
|
|
24
|
+
description:
|
|
25
|
+
"Discard prior messages and reload task state from a memory file.",
|
|
26
|
+
inputSchema: {
|
|
27
|
+
type: "object",
|
|
28
|
+
properties: {
|
|
29
|
+
memoryPath: {
|
|
30
|
+
type: "string",
|
|
31
|
+
description: `Path to the memory file under ${AGENT_MEMORY_DIR}/.`,
|
|
32
|
+
},
|
|
33
|
+
reason: {
|
|
34
|
+
type: "string",
|
|
35
|
+
description: "The reason for compacting the context.",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
required: ["memoryPath", "reason"],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
// Implementation is injected by the agent so it can access subagent
|
|
43
|
+
// state (compact_context is not allowed during subagent execution).
|
|
44
|
+
get impl() {
|
|
45
|
+
return impl;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
injectImpl(fn) {
|
|
49
|
+
impl = fn;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return tool;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Read a memory file and return the compact_context tool result string.
|
|
58
|
+
* Validates that the memoryPath is within the project memory directory.
|
|
59
|
+
* @param {CompactContextInput} input
|
|
60
|
+
* @returns {Promise<string | Error>}
|
|
61
|
+
*/
|
|
62
|
+
export async function readMemoryForCompaction(input) {
|
|
63
|
+
return await noThrow(async () => {
|
|
64
|
+
const absolutePath = path.resolve(input.memoryPath);
|
|
65
|
+
const memoryDir = path.resolve(AGENT_MEMORY_DIR);
|
|
66
|
+
const relativePath = path.relative(memoryDir, absolutePath);
|
|
67
|
+
if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) {
|
|
68
|
+
return new Error(
|
|
69
|
+
`Access denied: memoryPath must be within ${AGENT_MEMORY_DIR}`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const memoryContent = await fs.readFile(absolutePath, {
|
|
74
|
+
encoding: "utf-8",
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return [
|
|
78
|
+
"Context compacted. Prior conversation has been discarded.",
|
|
79
|
+
`Reason: ${input.reason}`,
|
|
80
|
+
`Memory file: ${input.memoryPath}`,
|
|
81
|
+
"",
|
|
82
|
+
"Resume the task using the memory file contents below.",
|
|
83
|
+
"",
|
|
84
|
+
memoryContent,
|
|
85
|
+
].join("\n");
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type ExecCommandInput = {
|
|
2
|
+
command: string;
|
|
3
|
+
args?: string[];
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type ExecCommandConfig = {
|
|
7
|
+
sandbox?: ExecCommandSanboxConfig;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type ExecCommandSanboxConfig = {
|
|
11
|
+
command: string;
|
|
12
|
+
args?: string[];
|
|
13
|
+
separator?: string;
|
|
14
|
+
rules?: {
|
|
15
|
+
pattern: {
|
|
16
|
+
command: string;
|
|
17
|
+
args?: string[];
|
|
18
|
+
};
|
|
19
|
+
mode: "sandbox" | "unsandboxed";
|
|
20
|
+
additionalArgs?: string[];
|
|
21
|
+
}[];
|
|
22
|
+
};
|