@gugacoder/agentic-sdk 0.2.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 +2 -0
- package/dist/agent.js +463 -0
- package/dist/context/compaction.d.ts +27 -0
- package/dist/context/compaction.js +219 -0
- package/dist/context/models.d.ts +6 -0
- package/dist/context/models.js +41 -0
- package/dist/context/tokenizer.d.ts +5 -0
- package/dist/context/tokenizer.js +11 -0
- package/dist/context/usage.d.ts +11 -0
- package/dist/context/usage.js +49 -0
- package/dist/display-schemas.d.ts +1865 -0
- package/dist/display-schemas.js +219 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +28 -0
- package/dist/middleware/logging.d.ts +2 -0
- package/dist/middleware/logging.js +32 -0
- package/dist/prompts/assembly.d.ts +13 -0
- package/dist/prompts/assembly.js +229 -0
- package/dist/providers.d.ts +19 -0
- package/dist/providers.js +44 -0
- package/dist/proxy.d.ts +2 -0
- package/dist/proxy.js +103 -0
- package/dist/schemas.d.ts +228 -0
- package/dist/schemas.js +51 -0
- package/dist/session.d.ts +7 -0
- package/dist/session.js +102 -0
- package/dist/structured.d.ts +18 -0
- package/dist/structured.js +38 -0
- package/dist/tool-repair.d.ts +21 -0
- package/dist/tool-repair.js +72 -0
- package/dist/tools/api-spec.d.ts +4 -0
- package/dist/tools/api-spec.js +123 -0
- package/dist/tools/apply-patch.d.ts +484 -0
- package/dist/tools/apply-patch.js +157 -0
- package/dist/tools/ask-user.d.ts +14 -0
- package/dist/tools/ask-user.js +27 -0
- package/dist/tools/bash.d.ts +550 -0
- package/dist/tools/bash.js +43 -0
- package/dist/tools/batch.d.ts +13 -0
- package/dist/tools/batch.js +84 -0
- package/dist/tools/brave-search.d.ts +6 -0
- package/dist/tools/brave-search.js +19 -0
- package/dist/tools/code-search.d.ts +20 -0
- package/dist/tools/code-search.js +42 -0
- package/dist/tools/diagnostics.d.ts +4 -0
- package/dist/tools/diagnostics.js +69 -0
- package/dist/tools/display.d.ts +483 -0
- package/dist/tools/display.js +77 -0
- package/dist/tools/edit.d.ts +682 -0
- package/dist/tools/edit.js +47 -0
- package/dist/tools/glob.d.ts +4 -0
- package/dist/tools/glob.js +42 -0
- package/dist/tools/grep.d.ts +6 -0
- package/dist/tools/grep.js +69 -0
- package/dist/tools/http-request.d.ts +7 -0
- package/dist/tools/http-request.js +98 -0
- package/dist/tools/index.d.ts +1611 -0
- package/dist/tools/index.js +46 -0
- package/dist/tools/job-tools.d.ts +24 -0
- package/dist/tools/job-tools.js +67 -0
- package/dist/tools/list-dir.d.ts +5 -0
- package/dist/tools/list-dir.js +79 -0
- package/dist/tools/multi-edit.d.ts +814 -0
- package/dist/tools/multi-edit.js +57 -0
- package/dist/tools/read.d.ts +5 -0
- package/dist/tools/read.js +33 -0
- package/dist/tools/task.d.ts +21 -0
- package/dist/tools/task.js +51 -0
- package/dist/tools/todo.d.ts +14 -0
- package/dist/tools/todo.js +60 -0
- package/dist/tools/web-fetch.d.ts +4 -0
- package/dist/tools/web-fetch.js +126 -0
- package/dist/tools/web-search.d.ts +22 -0
- package/dist/tools/web-search.js +48 -0
- package/dist/tools/write.d.ts +550 -0
- package/dist/tools/write.js +30 -0
- package/dist/types.d.ts +201 -0
- package/dist/types.js +1 -0
- package/package.json +43 -0
- package/src/agent.ts +520 -0
- package/src/context/compaction.ts +265 -0
- package/src/context/models.ts +42 -0
- package/src/context/tokenizer.ts +12 -0
- package/src/context/usage.ts +65 -0
- package/src/display-schemas.ts +276 -0
- package/src/index.ts +43 -0
- package/src/middleware/logging.ts +37 -0
- package/src/prompts/assembly.ts +263 -0
- package/src/prompts/identity.md +10 -0
- package/src/prompts/patterns.md +7 -0
- package/src/prompts/safety.md +7 -0
- package/src/prompts/tool-guide.md +9 -0
- package/src/prompts/tools/bash.md +7 -0
- package/src/prompts/tools/edit.md +7 -0
- package/src/prompts/tools/glob.md +7 -0
- package/src/prompts/tools/grep.md +7 -0
- package/src/prompts/tools/read.md +7 -0
- package/src/prompts/tools/write.md +7 -0
- package/src/providers.ts +58 -0
- package/src/proxy.ts +101 -0
- package/src/schemas.ts +58 -0
- package/src/session.ts +110 -0
- package/src/structured.ts +65 -0
- package/src/tool-repair.ts +92 -0
- package/src/tools/api-spec.ts +158 -0
- package/src/tools/apply-patch.ts +188 -0
- package/src/tools/ask-user.ts +40 -0
- package/src/tools/bash.ts +51 -0
- package/src/tools/batch.ts +103 -0
- package/src/tools/brave-search.ts +24 -0
- package/src/tools/code-search.ts +69 -0
- package/src/tools/diagnostics.ts +93 -0
- package/src/tools/display.ts +105 -0
- package/src/tools/edit.ts +55 -0
- package/src/tools/glob.ts +46 -0
- package/src/tools/grep.ts +68 -0
- package/src/tools/http-request.ts +103 -0
- package/src/tools/index.ts +48 -0
- package/src/tools/job-tools.ts +84 -0
- package/src/tools/list-dir.ts +102 -0
- package/src/tools/multi-edit.ts +65 -0
- package/src/tools/read.ts +40 -0
- package/src/tools/task.ts +71 -0
- package/src/tools/todo.ts +82 -0
- package/src/tools/web-fetch.ts +155 -0
- package/src/tools/web-search.ts +75 -0
- package/src/tools/write.ts +34 -0
- package/src/types.ts +145 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { tool } from "ai";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { readFile, writeFile, unlink, mkdir } from "node:fs/promises";
|
|
4
|
+
import { dirname } from "node:path";
|
|
5
|
+
function parsePatch(patch) {
|
|
6
|
+
const lines = patch.split("\n");
|
|
7
|
+
const operations = [];
|
|
8
|
+
let current = null;
|
|
9
|
+
let started = false;
|
|
10
|
+
for (const line of lines) {
|
|
11
|
+
if (line.trim() === "*** Begin Patch") {
|
|
12
|
+
started = true;
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (line.trim() === "*** End Patch") {
|
|
16
|
+
if (current)
|
|
17
|
+
operations.push(current);
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
if (!started)
|
|
21
|
+
continue;
|
|
22
|
+
if (line.startsWith("*** Add File: ")) {
|
|
23
|
+
if (current)
|
|
24
|
+
operations.push(current);
|
|
25
|
+
current = { type: "add", path: line.slice("*** Add File: ".length).trim(), lines: [] };
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (line.startsWith("*** Update File: ")) {
|
|
29
|
+
if (current)
|
|
30
|
+
operations.push(current);
|
|
31
|
+
current = { type: "update", path: line.slice("*** Update File: ".length).trim(), lines: [] };
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (line.startsWith("*** Delete File: ")) {
|
|
35
|
+
if (current)
|
|
36
|
+
operations.push(current);
|
|
37
|
+
current = { type: "delete", path: line.slice("*** Delete File: ".length).trim(), lines: [] };
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (current) {
|
|
41
|
+
current.lines.push(line);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Handle case where End Patch is missing but we have a pending operation
|
|
45
|
+
if (current && !operations.includes(current)) {
|
|
46
|
+
operations.push(current);
|
|
47
|
+
}
|
|
48
|
+
return operations;
|
|
49
|
+
}
|
|
50
|
+
function applyUpdate(original, patchLines) {
|
|
51
|
+
const originalLines = original.split("\n");
|
|
52
|
+
const result = [];
|
|
53
|
+
let originalIdx = 0;
|
|
54
|
+
let i = 0;
|
|
55
|
+
while (i < patchLines.length) {
|
|
56
|
+
const line = patchLines[i];
|
|
57
|
+
if (line.startsWith("@@")) {
|
|
58
|
+
// Context marker — find the context line in the original to sync position
|
|
59
|
+
const contextText = line.slice(3); // skip "@@ "
|
|
60
|
+
// Advance in original until we find a line matching the context
|
|
61
|
+
while (originalIdx < originalLines.length) {
|
|
62
|
+
if (originalLines[originalIdx] === contextText) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
result.push(originalLines[originalIdx]);
|
|
66
|
+
originalIdx++;
|
|
67
|
+
}
|
|
68
|
+
// Push the context line itself
|
|
69
|
+
if (originalIdx < originalLines.length) {
|
|
70
|
+
result.push(originalLines[originalIdx]);
|
|
71
|
+
originalIdx++;
|
|
72
|
+
}
|
|
73
|
+
i++;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (line.startsWith("-")) {
|
|
77
|
+
// Remove line — skip it in original
|
|
78
|
+
originalIdx++;
|
|
79
|
+
i++;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (line.startsWith("+")) {
|
|
83
|
+
// Add line
|
|
84
|
+
result.push(line.slice(1));
|
|
85
|
+
i++;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
// Unrecognized line in patch — treat as context (copy from original)
|
|
89
|
+
if (line.startsWith(" ")) {
|
|
90
|
+
result.push(originalLines[originalIdx] ?? line.slice(1));
|
|
91
|
+
originalIdx++;
|
|
92
|
+
}
|
|
93
|
+
i++;
|
|
94
|
+
}
|
|
95
|
+
// Copy remaining original lines
|
|
96
|
+
while (originalIdx < originalLines.length) {
|
|
97
|
+
result.push(originalLines[originalIdx]);
|
|
98
|
+
originalIdx++;
|
|
99
|
+
}
|
|
100
|
+
return result.join("\n");
|
|
101
|
+
}
|
|
102
|
+
export function createApplyPatchTool(opts) {
|
|
103
|
+
const baseTool = tool({
|
|
104
|
+
description: "Applies a multi-file patch in envelope format (Begin Patch / End Patch). Supports Add File (create new), Update File (apply diff with @@ context and +/- lines), and Delete File operations.",
|
|
105
|
+
inputSchema: z.object({
|
|
106
|
+
patch: z
|
|
107
|
+
.string()
|
|
108
|
+
.describe('The patch text in envelope format: "*** Begin Patch" / "*** End Patch" with "*** Add File: path", "*** Update File: path", "*** Delete File: path" sections'),
|
|
109
|
+
}),
|
|
110
|
+
execute: async ({ patch }) => {
|
|
111
|
+
try {
|
|
112
|
+
const operations = parsePatch(patch);
|
|
113
|
+
if (operations.length === 0) {
|
|
114
|
+
return "Error: no valid operations found in patch. Ensure the patch uses the *** Begin Patch / *** End Patch envelope format.";
|
|
115
|
+
}
|
|
116
|
+
const results = [];
|
|
117
|
+
for (const op of operations) {
|
|
118
|
+
switch (op.type) {
|
|
119
|
+
case "add": {
|
|
120
|
+
const content = op.lines
|
|
121
|
+
.filter((l) => l.startsWith("+"))
|
|
122
|
+
.map((l) => l.slice(1))
|
|
123
|
+
.join("\n");
|
|
124
|
+
await mkdir(dirname(op.path), { recursive: true });
|
|
125
|
+
await writeFile(op.path, content, "utf-8");
|
|
126
|
+
results.push(`Added: ${op.path}`);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case "update": {
|
|
130
|
+
const original = await readFile(op.path, "utf-8");
|
|
131
|
+
const updated = applyUpdate(original, op.lines);
|
|
132
|
+
await writeFile(op.path, updated, "utf-8");
|
|
133
|
+
results.push(`Updated: ${op.path}`);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
case "delete": {
|
|
137
|
+
await unlink(op.path);
|
|
138
|
+
results.push(`Deleted: ${op.path}`);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return `Patch applied successfully:\n${results.join("\n")}`;
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
return `Error applying patch: ${err.message}`;
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
if (opts?.autoApprove === false) {
|
|
151
|
+
return Object.assign(baseTool, {
|
|
152
|
+
needsApproval: async () => true,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return baseTool;
|
|
156
|
+
}
|
|
157
|
+
export const applyPatchTool = createApplyPatchTool();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callback type for the AskUser tool.
|
|
3
|
+
* The consumer (CLI, API, UI) implements this to present the question
|
|
4
|
+
* to the user and return their answer.
|
|
5
|
+
*/
|
|
6
|
+
export type AskUserCallback = (question: string, options?: string[]) => Promise<string>;
|
|
7
|
+
/**
|
|
8
|
+
* Factory that creates the AskUser tool with an injected callback.
|
|
9
|
+
* If no callback is provided, returns a tool that explains no handler is configured.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createAskUserTool(onAskUser?: AskUserCallback): import("ai").Tool<{
|
|
12
|
+
question: string;
|
|
13
|
+
options?: string[] | undefined;
|
|
14
|
+
}, string>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { tool } from "ai";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
/**
|
|
4
|
+
* Factory that creates the AskUser tool with an injected callback.
|
|
5
|
+
* If no callback is provided, returns a tool that explains no handler is configured.
|
|
6
|
+
*/
|
|
7
|
+
export function createAskUserTool(onAskUser) {
|
|
8
|
+
return tool({
|
|
9
|
+
description: "Ask the user a question during execution. Use this to request clarifications, preferences, or decisions before proceeding. The user will see the question and can respond.",
|
|
10
|
+
inputSchema: z.object({
|
|
11
|
+
question: z
|
|
12
|
+
.string()
|
|
13
|
+
.describe("The question to ask the user"),
|
|
14
|
+
options: z
|
|
15
|
+
.array(z.string())
|
|
16
|
+
.optional()
|
|
17
|
+
.describe("Optional list of answer choices to present to the user"),
|
|
18
|
+
}),
|
|
19
|
+
execute: async ({ question, options }) => {
|
|
20
|
+
if (!onAskUser) {
|
|
21
|
+
return "AskUser handler not configured. The consuming application must provide an onAskUser callback in AiAgentOptions to enable user interaction.";
|
|
22
|
+
}
|
|
23
|
+
const answer = await onAskUser(question, options);
|
|
24
|
+
return answer;
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|