@robota-sdk/agent-sdk 3.0.0-beta.1
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/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/node/index.cjs +576 -0
- package/dist/node/index.d.cts +294 -0
- package/dist/node/index.d.ts +294 -0
- package/dist/node/index.js +521 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Robota Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @robota-sdk/agent-sdk
|
|
2
|
+
|
|
3
|
+
Programmatic SDK for building AI agents with Robota. Provides a single `query()` entry point along with Session management, built-in tools, permissions, hooks, streaming, and context loading.
|
|
4
|
+
|
|
5
|
+
This is the **assembly layer** of the Robota ecosystem -- it composes lower-level packages (`agent-core`, `agent-tools`, `agent-sessions`) into a cohesive SDK.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @robota-sdk/agent-sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @robota-sdk/agent-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { query } from '@robota-sdk/agent-sdk';
|
|
19
|
+
|
|
20
|
+
// Simple one-shot query
|
|
21
|
+
const response = await query('Show me the file list');
|
|
22
|
+
|
|
23
|
+
// With options
|
|
24
|
+
const response = await query('Analyze the code', {
|
|
25
|
+
cwd: '/path/to/project',
|
|
26
|
+
permissionMode: 'acceptEdits',
|
|
27
|
+
maxTurns: 10,
|
|
28
|
+
onTextDelta: (delta) => process.stdout.write(delta),
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
- **query()** -- Single entry point for AI agent interactions with streaming support
|
|
35
|
+
- **Session** -- Wraps the Robota engine with permission checks, tool wiring, history, and streaming
|
|
36
|
+
- **Built-in Tools** -- Bash, Read, Write, Edit, Glob, Grep (from `@robota-sdk/agent-tools`)
|
|
37
|
+
- **Agent Tool** -- Sub-agent session creation for multi-agent workflows
|
|
38
|
+
- **Permissions** -- 3-step evaluation (deny list, allow list, mode policy) with four modes: `plan`, `default`, `acceptEdits`, `bypassPermissions`
|
|
39
|
+
- **Hooks** -- `PreToolUse`, `PostToolUse`, `SessionStart`, `Stop` events with shell command execution
|
|
40
|
+
- **Streaming** -- Real-time text delta callbacks via `onTextDelta`
|
|
41
|
+
- **Context Loading** -- AGENTS.md / CLAUDE.md walk-up discovery and system prompt assembly
|
|
42
|
+
- **Config Loading** -- 3-layer merge (user global, project, local) with `$ENV:VAR` substitution
|
|
43
|
+
- **Context Window Management** -- Token tracking, auto-compaction at ~83.5%, manual `session.compact()`
|
|
44
|
+
|
|
45
|
+
## Architecture
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
agent-sdk (assembly layer)
|
|
49
|
+
-> agent-sessions (Session, SessionStore)
|
|
50
|
+
-> agent-tools (tool infrastructure + 6 built-in tools)
|
|
51
|
+
-> agent-core (Robota engine, providers, permissions, hooks)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`agent-sdk` assembles existing packages -- it does not re-implement functionality that belongs in lower layers.
|
|
55
|
+
|
|
56
|
+
## Session Usage
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { Session } from '@robota-sdk/agent-sessions';
|
|
60
|
+
|
|
61
|
+
const session = new Session({ config, context, terminal, permissionMode });
|
|
62
|
+
const response = await session.run('Hello');
|
|
63
|
+
session.getHistory();
|
|
64
|
+
session.clearHistory();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Documentation
|
|
68
|
+
|
|
69
|
+
See [docs/SPEC.md](./docs/SPEC.md) for the full specification, architecture details, and design decisions.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
MIT
|
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Session: () => import_agent_sessions.Session,
|
|
34
|
+
SessionStore: () => import_agent_sessions2.SessionStore,
|
|
35
|
+
TRUST_TO_MODE: () => import_agent_core.TRUST_TO_MODE,
|
|
36
|
+
agentTool: () => agentTool,
|
|
37
|
+
bashTool: () => import_agent_tools.bashTool,
|
|
38
|
+
buildSystemPrompt: () => buildSystemPrompt,
|
|
39
|
+
detectProject: () => detectProject,
|
|
40
|
+
editTool: () => import_agent_tools4.editTool,
|
|
41
|
+
evaluatePermission: () => import_agent_core2.evaluatePermission,
|
|
42
|
+
globTool: () => import_agent_tools5.globTool,
|
|
43
|
+
grepTool: () => import_agent_tools6.grepTool,
|
|
44
|
+
loadConfig: () => loadConfig,
|
|
45
|
+
loadContext: () => loadContext,
|
|
46
|
+
promptForApproval: () => promptForApproval,
|
|
47
|
+
query: () => query,
|
|
48
|
+
readTool: () => import_agent_tools2.readTool,
|
|
49
|
+
runHooks: () => import_agent_core3.runHooks,
|
|
50
|
+
setAgentToolDeps: () => setAgentToolDeps,
|
|
51
|
+
writeTool: () => import_agent_tools3.writeTool
|
|
52
|
+
});
|
|
53
|
+
module.exports = __toCommonJS(index_exports);
|
|
54
|
+
|
|
55
|
+
// src/types.ts
|
|
56
|
+
var import_agent_core = require("@robota-sdk/agent-core");
|
|
57
|
+
|
|
58
|
+
// src/session.ts
|
|
59
|
+
var import_agent_sessions = require("@robota-sdk/agent-sessions");
|
|
60
|
+
|
|
61
|
+
// src/session-store.ts
|
|
62
|
+
var import_agent_sessions2 = require("@robota-sdk/agent-sessions");
|
|
63
|
+
|
|
64
|
+
// src/config/config-loader.ts
|
|
65
|
+
var import_fs = require("fs");
|
|
66
|
+
var import_path = require("path");
|
|
67
|
+
|
|
68
|
+
// src/config/config-types.ts
|
|
69
|
+
var import_zod = require("zod");
|
|
70
|
+
var ProviderSchema = import_zod.z.object({
|
|
71
|
+
name: import_zod.z.string().optional(),
|
|
72
|
+
model: import_zod.z.string().optional(),
|
|
73
|
+
apiKey: import_zod.z.string().optional()
|
|
74
|
+
});
|
|
75
|
+
var PermissionsSchema = import_zod.z.object({
|
|
76
|
+
/** Patterns that are always approved without prompting */
|
|
77
|
+
allow: import_zod.z.array(import_zod.z.string()).optional(),
|
|
78
|
+
/** Patterns that are always denied */
|
|
79
|
+
deny: import_zod.z.array(import_zod.z.string()).optional()
|
|
80
|
+
});
|
|
81
|
+
var EnvSchema = import_zod.z.record(import_zod.z.string()).optional();
|
|
82
|
+
var HookDefinitionSchema = import_zod.z.object({
|
|
83
|
+
type: import_zod.z.literal("command"),
|
|
84
|
+
command: import_zod.z.string()
|
|
85
|
+
});
|
|
86
|
+
var HookGroupSchema = import_zod.z.object({
|
|
87
|
+
matcher: import_zod.z.string(),
|
|
88
|
+
hooks: import_zod.z.array(HookDefinitionSchema)
|
|
89
|
+
});
|
|
90
|
+
var HooksSchema = import_zod.z.object({
|
|
91
|
+
PreToolUse: import_zod.z.array(HookGroupSchema).optional(),
|
|
92
|
+
PostToolUse: import_zod.z.array(HookGroupSchema).optional(),
|
|
93
|
+
SessionStart: import_zod.z.array(HookGroupSchema).optional(),
|
|
94
|
+
Stop: import_zod.z.array(HookGroupSchema).optional()
|
|
95
|
+
}).optional();
|
|
96
|
+
var SettingsSchema = import_zod.z.object({
|
|
97
|
+
/** Trust level used when no --permission-mode flag is given */
|
|
98
|
+
defaultTrustLevel: import_zod.z.enum(["safe", "moderate", "full"]).optional(),
|
|
99
|
+
provider: ProviderSchema.optional(),
|
|
100
|
+
permissions: PermissionsSchema.optional(),
|
|
101
|
+
env: EnvSchema,
|
|
102
|
+
hooks: HooksSchema
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// src/config/config-loader.ts
|
|
106
|
+
function getHomeDir() {
|
|
107
|
+
return process.env.HOME ?? process.env.USERPROFILE ?? "/";
|
|
108
|
+
}
|
|
109
|
+
var DEFAULTS = {
|
|
110
|
+
defaultTrustLevel: "moderate",
|
|
111
|
+
provider: {
|
|
112
|
+
name: "anthropic",
|
|
113
|
+
model: "claude-opus-4-5",
|
|
114
|
+
apiKey: void 0
|
|
115
|
+
},
|
|
116
|
+
permissions: {
|
|
117
|
+
allow: [],
|
|
118
|
+
deny: []
|
|
119
|
+
},
|
|
120
|
+
env: {}
|
|
121
|
+
};
|
|
122
|
+
function readJsonFile(filePath) {
|
|
123
|
+
if (!(0, import_fs.existsSync)(filePath)) {
|
|
124
|
+
return void 0;
|
|
125
|
+
}
|
|
126
|
+
const raw = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
127
|
+
return JSON.parse(raw);
|
|
128
|
+
}
|
|
129
|
+
function resolveEnvRef(value) {
|
|
130
|
+
const ENV_PREFIX = "$ENV:";
|
|
131
|
+
if (value.startsWith(ENV_PREFIX)) {
|
|
132
|
+
const varName = value.slice(ENV_PREFIX.length);
|
|
133
|
+
return process.env[varName] ?? value;
|
|
134
|
+
}
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
function resolveEnvRefs(settings) {
|
|
138
|
+
if (settings.provider?.apiKey !== void 0) {
|
|
139
|
+
return {
|
|
140
|
+
...settings,
|
|
141
|
+
provider: {
|
|
142
|
+
...settings.provider,
|
|
143
|
+
apiKey: resolveEnvRef(settings.provider.apiKey)
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return settings;
|
|
148
|
+
}
|
|
149
|
+
function mergeSettings(layers) {
|
|
150
|
+
return layers.reduce((merged, layer) => {
|
|
151
|
+
return {
|
|
152
|
+
...merged,
|
|
153
|
+
...layer,
|
|
154
|
+
provider: merged.provider !== void 0 || layer.provider !== void 0 ? { ...merged.provider, ...layer.provider } : void 0,
|
|
155
|
+
permissions: merged.permissions !== void 0 || layer.permissions !== void 0 ? {
|
|
156
|
+
allow: layer.permissions?.allow ?? merged.permissions?.allow,
|
|
157
|
+
deny: layer.permissions?.deny ?? merged.permissions?.deny
|
|
158
|
+
} : void 0,
|
|
159
|
+
env: {
|
|
160
|
+
...merged.env ?? {},
|
|
161
|
+
...layer.env ?? {}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}, {});
|
|
165
|
+
}
|
|
166
|
+
function toResolvedConfig(merged) {
|
|
167
|
+
return {
|
|
168
|
+
defaultTrustLevel: merged.defaultTrustLevel ?? DEFAULTS.defaultTrustLevel,
|
|
169
|
+
provider: {
|
|
170
|
+
name: merged.provider?.name ?? DEFAULTS.provider.name,
|
|
171
|
+
model: merged.provider?.model ?? DEFAULTS.provider.model,
|
|
172
|
+
apiKey: merged.provider?.apiKey ?? DEFAULTS.provider.apiKey
|
|
173
|
+
},
|
|
174
|
+
permissions: {
|
|
175
|
+
allow: merged.permissions?.allow ?? DEFAULTS.permissions.allow,
|
|
176
|
+
deny: merged.permissions?.deny ?? DEFAULTS.permissions.deny
|
|
177
|
+
},
|
|
178
|
+
env: merged.env ?? DEFAULTS.env,
|
|
179
|
+
hooks: merged.hooks ?? void 0
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async function loadConfig(cwd) {
|
|
183
|
+
const userSettingsPath = (0, import_path.join)(getHomeDir(), ".robota", "settings.json");
|
|
184
|
+
const projectSettingsPath = (0, import_path.join)(cwd, ".robota", "settings.json");
|
|
185
|
+
const localSettingsPath = (0, import_path.join)(cwd, ".robota", "settings.local.json");
|
|
186
|
+
const rawLayers = [
|
|
187
|
+
readJsonFile(userSettingsPath),
|
|
188
|
+
readJsonFile(projectSettingsPath),
|
|
189
|
+
readJsonFile(localSettingsPath)
|
|
190
|
+
].filter((v) => v !== void 0);
|
|
191
|
+
const parsedLayers = rawLayers.map((raw, index) => {
|
|
192
|
+
const result = SettingsSchema.safeParse(raw);
|
|
193
|
+
if (!result.success) {
|
|
194
|
+
const paths = [userSettingsPath, projectSettingsPath, localSettingsPath].filter(
|
|
195
|
+
(_, i) => rawLayers[i] !== void 0
|
|
196
|
+
);
|
|
197
|
+
throw new Error(`Invalid settings in ${paths[index] ?? "unknown"}: ${result.error.message}`);
|
|
198
|
+
}
|
|
199
|
+
return resolveEnvRefs(result.data);
|
|
200
|
+
});
|
|
201
|
+
const merged = mergeSettings(parsedLayers);
|
|
202
|
+
return toResolvedConfig(merged);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/context/context-loader.ts
|
|
206
|
+
var import_fs2 = require("fs");
|
|
207
|
+
var import_path2 = require("path");
|
|
208
|
+
var AGENTS_FILENAME = "AGENTS.md";
|
|
209
|
+
var CLAUDE_FILENAME = "CLAUDE.md";
|
|
210
|
+
function collectFilesWalkingUp(startDir, filename) {
|
|
211
|
+
const found = [];
|
|
212
|
+
let current = (0, import_path2.resolve)(startDir);
|
|
213
|
+
let atRoot = false;
|
|
214
|
+
while (!atRoot) {
|
|
215
|
+
const candidate = (0, import_path2.join)(current, filename);
|
|
216
|
+
if ((0, import_fs2.existsSync)(candidate)) {
|
|
217
|
+
found.push(candidate);
|
|
218
|
+
}
|
|
219
|
+
const parent = (0, import_path2.dirname)(current);
|
|
220
|
+
atRoot = parent === current;
|
|
221
|
+
if (!atRoot) {
|
|
222
|
+
current = parent;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return found.reverse();
|
|
226
|
+
}
|
|
227
|
+
function extractCompactInstructions(content) {
|
|
228
|
+
const lines = content.split("\n");
|
|
229
|
+
let capturing = false;
|
|
230
|
+
let headingLevel = 0;
|
|
231
|
+
const captured = [];
|
|
232
|
+
for (const line of lines) {
|
|
233
|
+
const headingMatch = /^(#{1,6})\s+/.exec(line);
|
|
234
|
+
if (headingMatch) {
|
|
235
|
+
if (capturing) {
|
|
236
|
+
if (headingMatch[1].length <= headingLevel) break;
|
|
237
|
+
}
|
|
238
|
+
if (/compact\s+instructions/i.test(line)) {
|
|
239
|
+
capturing = true;
|
|
240
|
+
headingLevel = headingMatch[1].length;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (capturing) {
|
|
245
|
+
captured.push(line);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const result = captured.join("\n").trim();
|
|
249
|
+
return result || void 0;
|
|
250
|
+
}
|
|
251
|
+
async function loadContext(cwd) {
|
|
252
|
+
const agentsPaths = collectFilesWalkingUp(cwd, AGENTS_FILENAME);
|
|
253
|
+
const claudePaths = collectFilesWalkingUp(cwd, CLAUDE_FILENAME);
|
|
254
|
+
const agentsMd = agentsPaths.map((p) => (0, import_fs2.readFileSync)(p, "utf-8")).join("\n\n");
|
|
255
|
+
const claudeMd = claudePaths.map((p) => (0, import_fs2.readFileSync)(p, "utf-8")).join("\n\n");
|
|
256
|
+
const compactInstructions = extractCompactInstructions(claudeMd);
|
|
257
|
+
return { agentsMd, claudeMd, compactInstructions };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// src/context/project-detector.ts
|
|
261
|
+
var import_fs3 = require("fs");
|
|
262
|
+
var import_path3 = require("path");
|
|
263
|
+
function tryReadJson(filePath) {
|
|
264
|
+
if (!(0, import_fs3.existsSync)(filePath)) return void 0;
|
|
265
|
+
try {
|
|
266
|
+
return JSON.parse((0, import_fs3.readFileSync)(filePath, "utf-8"));
|
|
267
|
+
} catch {
|
|
268
|
+
return void 0;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function detectPackageManager(cwd) {
|
|
272
|
+
if ((0, import_fs3.existsSync)((0, import_path3.join)(cwd, "pnpm-workspace.yaml")) || (0, import_fs3.existsSync)((0, import_path3.join)(cwd, "pnpm-lock.yaml"))) {
|
|
273
|
+
return "pnpm";
|
|
274
|
+
}
|
|
275
|
+
if ((0, import_fs3.existsSync)((0, import_path3.join)(cwd, "yarn.lock"))) {
|
|
276
|
+
return "yarn";
|
|
277
|
+
}
|
|
278
|
+
if ((0, import_fs3.existsSync)((0, import_path3.join)(cwd, "bun.lockb"))) {
|
|
279
|
+
return "bun";
|
|
280
|
+
}
|
|
281
|
+
if ((0, import_fs3.existsSync)((0, import_path3.join)(cwd, "package-lock.json"))) {
|
|
282
|
+
return "npm";
|
|
283
|
+
}
|
|
284
|
+
return void 0;
|
|
285
|
+
}
|
|
286
|
+
async function detectProject(cwd) {
|
|
287
|
+
const pkgJsonPath = (0, import_path3.join)(cwd, "package.json");
|
|
288
|
+
const tsconfigPath = (0, import_path3.join)(cwd, "tsconfig.json");
|
|
289
|
+
const pyprojectPath = (0, import_path3.join)(cwd, "pyproject.toml");
|
|
290
|
+
const cargoPath = (0, import_path3.join)(cwd, "Cargo.toml");
|
|
291
|
+
const goModPath = (0, import_path3.join)(cwd, "go.mod");
|
|
292
|
+
if ((0, import_fs3.existsSync)(pkgJsonPath)) {
|
|
293
|
+
const pkgJson = tryReadJson(pkgJsonPath);
|
|
294
|
+
const language = (0, import_fs3.existsSync)(tsconfigPath) ? "typescript" : "javascript";
|
|
295
|
+
const packageManager = detectPackageManager(cwd);
|
|
296
|
+
return {
|
|
297
|
+
type: "node",
|
|
298
|
+
name: pkgJson?.name,
|
|
299
|
+
packageManager,
|
|
300
|
+
language
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
if ((0, import_fs3.existsSync)(pyprojectPath) || (0, import_fs3.existsSync)((0, import_path3.join)(cwd, "setup.py"))) {
|
|
304
|
+
return {
|
|
305
|
+
type: "python",
|
|
306
|
+
language: "python"
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
if ((0, import_fs3.existsSync)(cargoPath)) {
|
|
310
|
+
return {
|
|
311
|
+
type: "rust",
|
|
312
|
+
language: "rust"
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
if ((0, import_fs3.existsSync)(goModPath)) {
|
|
316
|
+
return {
|
|
317
|
+
type: "go",
|
|
318
|
+
language: "go"
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
type: "unknown",
|
|
323
|
+
language: "unknown"
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/context/system-prompt-builder.ts
|
|
328
|
+
var TRUST_LEVEL_DESCRIPTIONS = {
|
|
329
|
+
safe: "safe (read-only / plan mode \u2014 only read-access tools are available)",
|
|
330
|
+
moderate: "moderate (default mode \u2014 write and bash tools require approval)",
|
|
331
|
+
full: "full (acceptEdits mode \u2014 file writes are auto-approved; bash requires approval)"
|
|
332
|
+
};
|
|
333
|
+
function buildProjectSection(info) {
|
|
334
|
+
const lines = ["## Current Project"];
|
|
335
|
+
if (info.name !== void 0) {
|
|
336
|
+
lines.push(`- **Name:** ${info.name}`);
|
|
337
|
+
}
|
|
338
|
+
if (info.type !== "unknown") {
|
|
339
|
+
lines.push(`- **Type:** ${info.type}`);
|
|
340
|
+
}
|
|
341
|
+
if (info.language !== "unknown") {
|
|
342
|
+
lines.push(`- **Language:** ${info.language}`);
|
|
343
|
+
}
|
|
344
|
+
if (info.packageManager !== void 0) {
|
|
345
|
+
lines.push(`- **Package manager:** ${info.packageManager}`);
|
|
346
|
+
}
|
|
347
|
+
return lines.join("\n");
|
|
348
|
+
}
|
|
349
|
+
function buildToolsSection(descriptions) {
|
|
350
|
+
if (descriptions.length === 0) {
|
|
351
|
+
return "";
|
|
352
|
+
}
|
|
353
|
+
const lines = ["## Available Tools", ...descriptions.map((d) => `- ${d}`)];
|
|
354
|
+
return lines.join("\n");
|
|
355
|
+
}
|
|
356
|
+
function buildSystemPrompt(params) {
|
|
357
|
+
const { agentsMd, claudeMd, toolDescriptions, trustLevel, projectInfo } = params;
|
|
358
|
+
const sections = [];
|
|
359
|
+
sections.push(
|
|
360
|
+
[
|
|
361
|
+
"## Role",
|
|
362
|
+
"You are an AI coding assistant with access to tools that let you read and modify code.",
|
|
363
|
+
"You help developers understand, write, and improve their codebase.",
|
|
364
|
+
"Always be precise, follow existing code conventions, and prefer minimal changes."
|
|
365
|
+
].join("\n")
|
|
366
|
+
);
|
|
367
|
+
sections.push(buildProjectSection(projectInfo));
|
|
368
|
+
sections.push(
|
|
369
|
+
[
|
|
370
|
+
"## Permission Mode",
|
|
371
|
+
`Your current trust level is **${TRUST_LEVEL_DESCRIPTIONS[trustLevel]}**.`
|
|
372
|
+
].join("\n")
|
|
373
|
+
);
|
|
374
|
+
if (agentsMd.trim().length > 0) {
|
|
375
|
+
sections.push(["## Agent Instructions", agentsMd].join("\n"));
|
|
376
|
+
}
|
|
377
|
+
if (claudeMd.trim().length > 0) {
|
|
378
|
+
sections.push(["## Project Notes", claudeMd].join("\n"));
|
|
379
|
+
}
|
|
380
|
+
sections.push(
|
|
381
|
+
[
|
|
382
|
+
"## Web Search",
|
|
383
|
+
"You have access to web search. When the user asks to search, look up, or find current/latest information,",
|
|
384
|
+
"you MUST use the web_search tool. Do NOT answer from training data when the user explicitly asks to search.",
|
|
385
|
+
"Always prefer web search for: news, latest versions, current events, live documentation."
|
|
386
|
+
].join("\n")
|
|
387
|
+
);
|
|
388
|
+
const toolsSection = buildToolsSection(toolDescriptions);
|
|
389
|
+
if (toolsSection.length > 0) {
|
|
390
|
+
sections.push(toolsSection);
|
|
391
|
+
}
|
|
392
|
+
return sections.join("\n\n");
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// src/query.ts
|
|
396
|
+
var import_agent_sessions3 = require("@robota-sdk/agent-sessions");
|
|
397
|
+
|
|
398
|
+
// src/permissions/permission-prompt.ts
|
|
399
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
400
|
+
var PERMISSION_OPTIONS = ["Allow", "Deny"];
|
|
401
|
+
var ALLOW_INDEX = 0;
|
|
402
|
+
function formatArgs(toolArgs) {
|
|
403
|
+
const entries = Object.entries(toolArgs);
|
|
404
|
+
if (entries.length === 0) {
|
|
405
|
+
return "(no arguments)";
|
|
406
|
+
}
|
|
407
|
+
return entries.map(([k, v]) => `${k}: ${typeof v === "string" ? v : JSON.stringify(v)}`).join(", ");
|
|
408
|
+
}
|
|
409
|
+
async function promptForApproval(terminal, toolName, toolArgs) {
|
|
410
|
+
terminal.writeLine("");
|
|
411
|
+
terminal.writeLine(import_chalk.default.yellow(`[Permission Required] Tool: ${toolName}`));
|
|
412
|
+
terminal.writeLine(import_chalk.default.dim(` ${formatArgs(toolArgs)}`));
|
|
413
|
+
terminal.writeLine("");
|
|
414
|
+
const selected = await terminal.select(PERMISSION_OPTIONS, ALLOW_INDEX);
|
|
415
|
+
return selected === ALLOW_INDEX;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// src/query.ts
|
|
419
|
+
async function query(prompt, options) {
|
|
420
|
+
const cwd = options?.cwd ?? process.cwd();
|
|
421
|
+
const [config, context, projectInfo] = await Promise.all([
|
|
422
|
+
loadConfig(cwd),
|
|
423
|
+
loadContext(cwd),
|
|
424
|
+
detectProject(cwd)
|
|
425
|
+
]);
|
|
426
|
+
const noopTerminal = {
|
|
427
|
+
write: () => {
|
|
428
|
+
},
|
|
429
|
+
writeLine: () => {
|
|
430
|
+
},
|
|
431
|
+
writeMarkdown: () => {
|
|
432
|
+
},
|
|
433
|
+
writeError: () => {
|
|
434
|
+
},
|
|
435
|
+
prompt: () => Promise.resolve(""),
|
|
436
|
+
select: () => Promise.resolve(0),
|
|
437
|
+
spinner: () => ({ stop: () => {
|
|
438
|
+
}, update: () => {
|
|
439
|
+
} })
|
|
440
|
+
};
|
|
441
|
+
const session = new import_agent_sessions3.Session({
|
|
442
|
+
config,
|
|
443
|
+
context,
|
|
444
|
+
terminal: noopTerminal,
|
|
445
|
+
projectInfo,
|
|
446
|
+
permissionMode: options?.permissionMode ?? "bypassPermissions",
|
|
447
|
+
maxTurns: options?.maxTurns,
|
|
448
|
+
provider: options?.provider,
|
|
449
|
+
permissionHandler: options?.permissionHandler,
|
|
450
|
+
onTextDelta: options?.onTextDelta,
|
|
451
|
+
onCompact: options?.onCompact,
|
|
452
|
+
compactInstructions: context.compactInstructions,
|
|
453
|
+
systemPromptBuilder: buildSystemPrompt,
|
|
454
|
+
promptForApproval
|
|
455
|
+
});
|
|
456
|
+
return session.run(prompt);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/permissions/permission-gate.ts
|
|
460
|
+
var import_agent_core2 = require("@robota-sdk/agent-core");
|
|
461
|
+
|
|
462
|
+
// src/hooks/hook-runner.ts
|
|
463
|
+
var import_agent_core3 = require("@robota-sdk/agent-core");
|
|
464
|
+
|
|
465
|
+
// src/tools/bash-tool.ts
|
|
466
|
+
var import_agent_tools = require("@robota-sdk/agent-tools");
|
|
467
|
+
|
|
468
|
+
// src/tools/read-tool.ts
|
|
469
|
+
var import_agent_tools2 = require("@robota-sdk/agent-tools");
|
|
470
|
+
|
|
471
|
+
// src/tools/write-tool.ts
|
|
472
|
+
var import_agent_tools3 = require("@robota-sdk/agent-tools");
|
|
473
|
+
|
|
474
|
+
// src/tools/edit-tool.ts
|
|
475
|
+
var import_agent_tools4 = require("@robota-sdk/agent-tools");
|
|
476
|
+
|
|
477
|
+
// src/tools/glob-tool.ts
|
|
478
|
+
var import_agent_tools5 = require("@robota-sdk/agent-tools");
|
|
479
|
+
|
|
480
|
+
// src/tools/grep-tool.ts
|
|
481
|
+
var import_agent_tools6 = require("@robota-sdk/agent-tools");
|
|
482
|
+
|
|
483
|
+
// src/tools/agent-tool.ts
|
|
484
|
+
var import_zod2 = require("zod");
|
|
485
|
+
var import_agent_tools7 = require("@robota-sdk/agent-tools");
|
|
486
|
+
var import_agent_sessions4 = require("@robota-sdk/agent-sessions");
|
|
487
|
+
function asZodSchema(schema) {
|
|
488
|
+
return schema;
|
|
489
|
+
}
|
|
490
|
+
var AgentSchema = import_zod2.z.object({
|
|
491
|
+
prompt: import_zod2.z.string().describe("Task description for the sub-agent"),
|
|
492
|
+
description: import_zod2.z.string().optional().describe("Short description of what the sub-agent will do (3-5 words)")
|
|
493
|
+
});
|
|
494
|
+
var agentToolDeps;
|
|
495
|
+
function setAgentToolDeps(deps) {
|
|
496
|
+
agentToolDeps = deps;
|
|
497
|
+
}
|
|
498
|
+
async function runAgent(args) {
|
|
499
|
+
if (!agentToolDeps) {
|
|
500
|
+
const result = {
|
|
501
|
+
success: false,
|
|
502
|
+
output: "",
|
|
503
|
+
error: "Agent tool not initialized \u2014 missing dependencies"
|
|
504
|
+
};
|
|
505
|
+
return JSON.stringify(result);
|
|
506
|
+
}
|
|
507
|
+
const subSession = new import_agent_sessions4.Session({
|
|
508
|
+
config: agentToolDeps.config,
|
|
509
|
+
context: agentToolDeps.context,
|
|
510
|
+
projectInfo: agentToolDeps.projectInfo,
|
|
511
|
+
// No terminal needed — sub-agents don't prompt for permissions
|
|
512
|
+
terminal: {
|
|
513
|
+
write: () => {
|
|
514
|
+
},
|
|
515
|
+
writeLine: () => {
|
|
516
|
+
},
|
|
517
|
+
writeMarkdown: () => {
|
|
518
|
+
},
|
|
519
|
+
writeError: () => {
|
|
520
|
+
},
|
|
521
|
+
prompt: () => Promise.resolve(""),
|
|
522
|
+
select: () => Promise.resolve(0),
|
|
523
|
+
spinner: () => ({ stop: () => {
|
|
524
|
+
}, update: () => {
|
|
525
|
+
} })
|
|
526
|
+
},
|
|
527
|
+
// Sub-agents bypass permissions — they inherit parent's trust
|
|
528
|
+
permissionMode: "bypassPermissions"
|
|
529
|
+
});
|
|
530
|
+
try {
|
|
531
|
+
const response = await subSession.run(args.prompt);
|
|
532
|
+
const result = {
|
|
533
|
+
success: true,
|
|
534
|
+
output: response
|
|
535
|
+
};
|
|
536
|
+
return JSON.stringify(result);
|
|
537
|
+
} catch (err) {
|
|
538
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
539
|
+
const result = {
|
|
540
|
+
success: false,
|
|
541
|
+
output: "",
|
|
542
|
+
error: `Sub-agent error: ${message}`
|
|
543
|
+
};
|
|
544
|
+
return JSON.stringify(result);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
var agentTool = (0, import_agent_tools7.createZodFunctionTool)(
|
|
548
|
+
"Agent",
|
|
549
|
+
"Spawn a sub-agent with isolated context to handle a task. The sub-agent has its own conversation history and can use all tools.",
|
|
550
|
+
asZodSchema(AgentSchema),
|
|
551
|
+
async (params) => {
|
|
552
|
+
return runAgent(params);
|
|
553
|
+
}
|
|
554
|
+
);
|
|
555
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
556
|
+
0 && (module.exports = {
|
|
557
|
+
Session,
|
|
558
|
+
SessionStore,
|
|
559
|
+
TRUST_TO_MODE,
|
|
560
|
+
agentTool,
|
|
561
|
+
bashTool,
|
|
562
|
+
buildSystemPrompt,
|
|
563
|
+
detectProject,
|
|
564
|
+
editTool,
|
|
565
|
+
evaluatePermission,
|
|
566
|
+
globTool,
|
|
567
|
+
grepTool,
|
|
568
|
+
loadConfig,
|
|
569
|
+
loadContext,
|
|
570
|
+
promptForApproval,
|
|
571
|
+
query,
|
|
572
|
+
readTool,
|
|
573
|
+
runHooks,
|
|
574
|
+
setAgentToolDeps,
|
|
575
|
+
writeTool
|
|
576
|
+
});
|