@useavalon/caelence-agent 0.1.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.
Files changed (161) hide show
  1. package/README.md +201 -0
  2. package/bin/harness.ts +10 -0
  3. package/package.json +75 -0
  4. package/skills/copy-editor/SKILL.md +74 -0
  5. package/skills/copy-editor/references/anti-slop.md +62 -0
  6. package/skills/copy-editor/references/ui-states.md +46 -0
  7. package/skills/copy-editor/scripts/lint-copy.py +78 -0
  8. package/skills/copy-harry-dry/SKILL.md +120 -0
  9. package/skills/copy-rmbc/SKILL.md +97 -0
  10. package/skills/copywriting/SKILL.md +73 -0
  11. package/skills/impeccable/SKILL.md +133 -0
  12. package/skills/impeccable/references/visual-slop.md +36 -0
  13. package/src/cli/main.test.ts +36 -0
  14. package/src/cli/main.ts +384 -0
  15. package/src/cli/media-models.ts +82 -0
  16. package/src/cli/models.ts +43 -0
  17. package/src/cli/openrouter-live.test.ts +35 -0
  18. package/src/cli/openrouter-live.ts +72 -0
  19. package/src/cli/openrouter-modality.test.ts +25 -0
  20. package/src/cli/openrouter-modality.ts +83 -0
  21. package/src/cli/picker.test.ts +53 -0
  22. package/src/cli/picker.ts +48 -0
  23. package/src/cli/plain-text.ts +15 -0
  24. package/src/cli/slash-dispatch.test.ts +213 -0
  25. package/src/cli/slash-dispatch.ts +298 -0
  26. package/src/cli/slash.test.ts +113 -0
  27. package/src/cli/slash.ts +119 -0
  28. package/src/cli/theme.test.ts +10 -0
  29. package/src/cli/theme.ts +44 -0
  30. package/src/cli/tui-layout.test.ts +33 -0
  31. package/src/cli/tui-layout.ts +33 -0
  32. package/src/cli/tui-screen.test.ts +33 -0
  33. package/src/cli/tui-screen.ts +13 -0
  34. package/src/cli/tui.tsx +735 -0
  35. package/src/config.ts +86 -0
  36. package/src/core/approval.test.ts +60 -0
  37. package/src/core/approval.ts +83 -0
  38. package/src/core/auto-router.test.ts +31 -0
  39. package/src/core/auto-router.ts +66 -0
  40. package/src/core/errors.ts +67 -0
  41. package/src/core/events.ts +85 -0
  42. package/src/core/hooks.test.ts +66 -0
  43. package/src/core/hooks.ts +94 -0
  44. package/src/core/ids.ts +6 -0
  45. package/src/core/instructions.test.ts +49 -0
  46. package/src/core/instructions.ts +44 -0
  47. package/src/core/logger.ts +45 -0
  48. package/src/core/mcp.ts +191 -0
  49. package/src/core/mode.test.ts +63 -0
  50. package/src/core/mode.ts +60 -0
  51. package/src/core/run-turn.test.ts +339 -0
  52. package/src/core/run-turn.ts +487 -0
  53. package/src/core/session.test.ts +203 -0
  54. package/src/core/session.ts +349 -0
  55. package/src/core/stdio-mcp.ts +143 -0
  56. package/src/core/strands.ts +262 -0
  57. package/src/core/usage.test.ts +69 -0
  58. package/src/core/usage.ts +171 -0
  59. package/src/desktop/bridge.test.ts +331 -0
  60. package/src/desktop/bridge.ts +729 -0
  61. package/src/desktop/open-local.test.ts +40 -0
  62. package/src/desktop/open-local.ts +52 -0
  63. package/src/desktop/preview-cache.test.ts +36 -0
  64. package/src/desktop/preview-cache.ts +63 -0
  65. package/src/desktop/secrets.test.ts +44 -0
  66. package/src/desktop/secrets.ts +60 -0
  67. package/src/evals/runner.test.ts +43 -0
  68. package/src/evals/runner.ts +225 -0
  69. package/src/index.ts +132 -0
  70. package/src/init.test.ts +54 -0
  71. package/src/init.ts +117 -0
  72. package/src/integrations/catalog-data.json +424 -0
  73. package/src/integrations/catalog.ts +81 -0
  74. package/src/integrations/connect.ts +231 -0
  75. package/src/integrations/integrations.test.ts +329 -0
  76. package/src/integrations/marks/airtable.svg +1 -0
  77. package/src/integrations/marks/algolia-mcp.svg +1 -0
  78. package/src/integrations/marks/canva.jpg +0 -0
  79. package/src/integrations/marks/cloudflare-mcp.svg +1 -0
  80. package/src/integrations/marks/contentful-mcp.svg +1 -0
  81. package/src/integrations/marks/craft.png +0 -0
  82. package/src/integrations/marks/dinero.png +0 -0
  83. package/src/integrations/marks/figma.svg +1 -0
  84. package/src/integrations/marks/gitlab.svg +1 -0
  85. package/src/integrations/marks/google-calendar.svg +1 -0
  86. package/src/integrations/marks/google-docs.svg +1 -0
  87. package/src/integrations/marks/google-drive.svg +1 -0
  88. package/src/integrations/marks/google-gmail.svg +1 -0
  89. package/src/integrations/marks/google-sheets.svg +1 -0
  90. package/src/integrations/marks/google-slides.svg +1 -0
  91. package/src/integrations/marks/granola.png +0 -0
  92. package/src/integrations/marks/jira.svg +1 -0
  93. package/src/integrations/marks/linear.svg +1 -0
  94. package/src/integrations/marks/microsoft-365.svg +7 -0
  95. package/src/integrations/marks/miro.svg +1 -0
  96. package/src/integrations/marks/mixpanel-mcp.svg +1 -0
  97. package/src/integrations/marks/monday.png +0 -0
  98. package/src/integrations/marks/mux-mcp.png +0 -0
  99. package/src/integrations/marks/neon-mcp.svg +1 -0
  100. package/src/integrations/marks/notion.svg +1 -0
  101. package/src/integrations/marks/paddle-mcp.svg +1 -0
  102. package/src/integrations/marks/posthog-mcp.svg +1 -0
  103. package/src/integrations/marks/prismic-mcp.svg +1 -0
  104. package/src/integrations/marks/replicate-mcp.svg +1 -0
  105. package/src/integrations/marks/resend-mcp.svg +1 -0
  106. package/src/integrations/marks/sanity-mcp.svg +1 -0
  107. package/src/integrations/marks/sceneplane.png +0 -0
  108. package/src/integrations/marks/sentry-mcp.svg +1 -0
  109. package/src/integrations/marks/stripe-mcp.svg +1 -0
  110. package/src/integrations/marks/supabase-mcp.svg +1 -0
  111. package/src/integrations/marks/upstash-mcp.svg +1 -0
  112. package/src/integrations/marks/vercel.svg +1 -0
  113. package/src/integrations/marks/webflow.svg +1 -0
  114. package/src/integrations/mcp-oauth.test.ts +39 -0
  115. package/src/integrations/mcp-oauth.ts +330 -0
  116. package/src/integrations/oauth-clients.test.ts +77 -0
  117. package/src/integrations/oauth-clients.ts +127 -0
  118. package/src/integrations/oauth.ts +145 -0
  119. package/src/integrations/remote-mcp.ts +307 -0
  120. package/src/integrations/store.ts +149 -0
  121. package/src/media/openrouter-generate.test.ts +80 -0
  122. package/src/media/openrouter-generate.ts +178 -0
  123. package/src/media/prefs.ts +54 -0
  124. package/src/observability/categories.ts +41 -0
  125. package/src/observability/create.test.ts +56 -0
  126. package/src/observability/create.ts +45 -0
  127. package/src/observability/experiments.ts +113 -0
  128. package/src/observability/fake.ts +191 -0
  129. package/src/observability/index.ts +57 -0
  130. package/src/observability/judges.ts +91 -0
  131. package/src/observability/langfuse.ts +275 -0
  132. package/src/observability/noop.ts +33 -0
  133. package/src/observability/observability.test.ts +113 -0
  134. package/src/observability/prompts.ts +98 -0
  135. package/src/observability/regression-items.ts +158 -0
  136. package/src/observability/sanitize.test.ts +26 -0
  137. package/src/observability/sanitize.ts +57 -0
  138. package/src/observability/types.ts +122 -0
  139. package/src/observability/version.ts +41 -0
  140. package/src/product.ts +2 -0
  141. package/src/runtime.test.ts +98 -0
  142. package/src/runtime.ts +438 -0
  143. package/src/skills/author-skill.ts +49 -0
  144. package/src/skills/create-skill.test.ts +108 -0
  145. package/src/skills/create-skill.ts +70 -0
  146. package/src/skills/loader.test.ts +122 -0
  147. package/src/skills/loader.ts +162 -0
  148. package/src/skills/registry.test.ts +190 -0
  149. package/src/skills/registry.ts +402 -0
  150. package/src/skills/skill-command.ts +53 -0
  151. package/src/skills/user-dir.ts +8 -0
  152. package/src/tools/git.test.ts +71 -0
  153. package/src/tools/git.ts +126 -0
  154. package/src/tools/local.ts +258 -0
  155. package/src/tools/paths.test.ts +24 -0
  156. package/src/tools/paths.ts +39 -0
  157. package/src/tools/read-skill.ts +43 -0
  158. package/src/tools/subagent.test.ts +77 -0
  159. package/src/tools/subagent.ts +74 -0
  160. package/src/tools/todo.test.ts +25 -0
  161. package/src/tools/todo.ts +70 -0
@@ -0,0 +1,49 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { loadInstructionLayers, userInstructionsPath } from "./instructions.ts";
6
+
7
+ describe("instruction layers", () => {
8
+ test("user path honors HARNESS_USER_INSTRUCTIONS", () => {
9
+ expect(userInstructionsPath({ HARNESS_USER_INSTRUCTIONS: "/tmp/user-agents.md" })).toBe(
10
+ "/tmp/user-agents.md",
11
+ );
12
+ });
13
+
14
+ test("stacks user then project without replacing either", async () => {
15
+ const cwd = await mkdtemp(join(tmpdir(), "harness-instr-"));
16
+ const userDir = await mkdtemp(join(tmpdir(), "harness-user-"));
17
+ try {
18
+ await writeFile(join(cwd, "AGENTS.md"), "project rule\n", "utf8");
19
+ const userFile = join(userDir, "AGENTS.md");
20
+ await writeFile(userFile, "user rule\n", "utf8");
21
+ const layers = loadInstructionLayers({
22
+ cwd,
23
+ projectFile: "AGENTS.md",
24
+ env: { HARNESS_USER_INSTRUCTIONS: userFile },
25
+ });
26
+ expect(layers.user).toBe("user rule");
27
+ expect(layers.project).toBe("project rule");
28
+ } finally {
29
+ await rm(cwd, { recursive: true, force: true });
30
+ await rm(userDir, { recursive: true, force: true });
31
+ }
32
+ });
33
+
34
+ test("missing files are empty strings", async () => {
35
+ const cwd = await mkdtemp(join(tmpdir(), "harness-instr-empty-"));
36
+ try {
37
+ await mkdir(join(cwd, "empty"), { recursive: true });
38
+ const layers = loadInstructionLayers({
39
+ cwd,
40
+ projectFile: "missing.md",
41
+ env: { HARNESS_USER_INSTRUCTIONS: join(cwd, "nope.md") },
42
+ });
43
+ expect(layers.user).toBe("");
44
+ expect(layers.project).toBe("");
45
+ } finally {
46
+ await rm(cwd, { recursive: true, force: true });
47
+ }
48
+ });
49
+ });
@@ -0,0 +1,44 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { resolve } from "node:path";
4
+
5
+ export interface InstructionLayers {
6
+ user: string;
7
+ project: string;
8
+ }
9
+
10
+ function readIfPresent(path: string): string {
11
+ if (!existsSync(path)) return "";
12
+ try {
13
+ return readFileSync(path, "utf8").trim();
14
+ } catch {
15
+ return "";
16
+ }
17
+ }
18
+
19
+ export function userInstructionsPath(
20
+ env: Record<string, string | undefined> = process.env,
21
+ ): string {
22
+ const override = env.HARNESS_USER_INSTRUCTIONS?.trim();
23
+ if (override) return resolve(override);
24
+ return resolve(homedir(), ".harness", "AGENTS.md");
25
+ }
26
+
27
+ /** User file (~/.harness/AGENTS.md) then the project file. Project does not replace the user layer. */
28
+ export function loadInstructionLayers(input: {
29
+ cwd: string;
30
+ projectFile: string;
31
+ env?: Record<string, string | undefined>;
32
+ }): InstructionLayers {
33
+ return {
34
+ user: readIfPresent(userInstructionsPath(input.env)),
35
+ project: readIfPresent(resolve(input.cwd, input.projectFile)),
36
+ };
37
+ }
38
+
39
+ export function formatInstructionLayers(layers: InstructionLayers): string {
40
+ const parts: string[] = [];
41
+ if (layers.user) parts.push("## User instructions", layers.user);
42
+ if (layers.project) parts.push("## Project instructions", layers.project);
43
+ return parts.join("\n\n");
44
+ }
@@ -0,0 +1,45 @@
1
+ type Level = "debug" | "info" | "warn" | "error";
2
+
3
+ export interface Logger {
4
+ debug(msg: string, extra?: Record<string, unknown>): void;
5
+ info(msg: string, extra?: Record<string, unknown>): void;
6
+ warn(msg: string, extra?: Record<string, unknown>): void;
7
+ error(msg: string, extra?: Record<string, unknown>): void;
8
+ child(scope: string): Logger;
9
+ }
10
+
11
+ function emit(level: Level, scope: string, msg: string, extra?: Record<string, unknown>): void {
12
+ const line = JSON.stringify({
13
+ t: new Date().toISOString(),
14
+ level,
15
+ scope,
16
+ msg,
17
+ ...(extra ? { data: extra } : {}),
18
+ });
19
+ // stderr so a TUI on stdout is not corrupted
20
+ if (level === "error") process.stderr.write(`${line}\n`);
21
+ else if (level === "warn") process.stderr.write(`${line}\n`);
22
+ else if (process.env.HARNESS_LOG === "1") process.stderr.write(`${line}\n`);
23
+ }
24
+
25
+ export function createLogger(scope: string): Logger {
26
+ return {
27
+ debug: (msg, extra) => emit("debug", scope, msg, extra),
28
+ info: (msg, extra) => emit("info", scope, msg, extra),
29
+ warn: (msg, extra) => emit("warn", scope, msg, extra),
30
+ error: (msg, extra) => emit("error", scope, msg, extra),
31
+ child: (sub) => createLogger(`${scope}:${sub}`),
32
+ };
33
+ }
34
+
35
+ export function silentLogger(): Logger {
36
+ const noop = () => undefined;
37
+ const logger: Logger = {
38
+ debug: noop,
39
+ info: noop,
40
+ warn: noop,
41
+ error: noop,
42
+ child: () => logger,
43
+ };
44
+ return logger;
45
+ }
@@ -0,0 +1,191 @@
1
+ /**
2
+ * Minimal MCP tool registry + newline-delimited JSON-RPC stdio loop.
3
+ */
4
+
5
+ export interface ToolResult {
6
+ content: Array<{ type: "text"; text: string }>;
7
+ isError?: boolean;
8
+ structuredContent?: Record<string, unknown>;
9
+ }
10
+
11
+ export interface McpToolDefinition {
12
+ name: string;
13
+ description: string;
14
+ inputSchema: Record<string, unknown>;
15
+ handler(input: Record<string, unknown>): Promise<ToolResult>;
16
+ }
17
+
18
+ export interface McpToolSet {
19
+ readonly tools: readonly McpToolDefinition[];
20
+ }
21
+
22
+ export interface McpServer extends McpToolSet {
23
+ listTools(): McpToolDefinition[];
24
+ callTool(name: string, input: Record<string, unknown>): Promise<ToolResult>;
25
+ }
26
+
27
+ export function textResult(text: string, isError = false): ToolResult {
28
+ return { content: [{ type: "text", text }], isError };
29
+ }
30
+
31
+ export function jsonResult(value: unknown, isError = false): ToolResult {
32
+ const text = JSON.stringify(value, null, 2);
33
+ const structured =
34
+ value && typeof value === "object" && !Array.isArray(value)
35
+ ? (value as Record<string, unknown>)
36
+ : undefined;
37
+ return {
38
+ content: [{ type: "text", text }],
39
+ isError,
40
+ ...(structured ? { structuredContent: structured } : {}),
41
+ };
42
+ }
43
+
44
+ export function errorResult(message: string): ToolResult {
45
+ return {
46
+ content: [{ type: "text", text: message }],
47
+ isError: true,
48
+ structuredContent: { error: message },
49
+ };
50
+ }
51
+
52
+ export function createMcpServer(tools: McpToolDefinition[]): McpServer {
53
+ const byName = new Map(tools.map((t) => [t.name, t]));
54
+ return {
55
+ tools,
56
+ listTools() {
57
+ return [...tools];
58
+ },
59
+ async callTool(name, input) {
60
+ const tool = byName.get(name);
61
+ if (tool === undefined) {
62
+ return errorResult(`Unknown tool: ${JSON.stringify(name)}`);
63
+ }
64
+ return tool.handler(input ?? {});
65
+ },
66
+ };
67
+ }
68
+
69
+ export function toolResultText(result: ToolResult): string {
70
+ return result.content.map((block) => block.text).join("\n");
71
+ }
72
+
73
+ type JsonRpcId = string | number | null;
74
+
75
+ interface JsonRpcRequest {
76
+ jsonrpc: "2.0";
77
+ id?: JsonRpcId;
78
+ method: string;
79
+ params?: Record<string, unknown>;
80
+ }
81
+
82
+ const PROTOCOL_VERSION = "2024-11-05";
83
+
84
+ export interface StdioServerOptions {
85
+ name: string;
86
+ version?: string;
87
+ onError?: (message: string) => void;
88
+ }
89
+
90
+ export function runStdioMcpServer(server: McpServer, options: StdioServerOptions): Promise<void> {
91
+ const onError = options.onError ?? ((m: string) => process.stderr.write(`${m}\n`));
92
+
93
+ const write = (message: Record<string, unknown>): void => {
94
+ process.stdout.write(`${JSON.stringify(message)}\n`);
95
+ };
96
+
97
+ const handle = async (line: string): Promise<void> => {
98
+ const trimmed = line.trim();
99
+ if (trimmed === "") return;
100
+ let req: JsonRpcRequest;
101
+ try {
102
+ req = JSON.parse(trimmed) as JsonRpcRequest;
103
+ } catch {
104
+ write({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
105
+ return;
106
+ }
107
+ const isNotification = req.id === undefined;
108
+ try {
109
+ const result = await dispatch(server, req, options);
110
+ if (!isNotification) write({ jsonrpc: "2.0", id: req.id ?? null, result });
111
+ } catch (err) {
112
+ if (!isNotification) {
113
+ const code = err instanceof JsonRpcMethodError ? -32601 : -32603;
114
+ write({
115
+ jsonrpc: "2.0",
116
+ id: req.id ?? null,
117
+ error: { code, message: err instanceof Error ? err.message : String(err) },
118
+ });
119
+ }
120
+ }
121
+ };
122
+
123
+ return new Promise<void>((resolve) => {
124
+ let buffer = "";
125
+ let chain: Promise<void> = Promise.resolve();
126
+ const stdin = process.stdin;
127
+ stdin.setEncoding("utf8");
128
+ stdin.on("data", (chunk: string) => {
129
+ buffer += chunk;
130
+ let idx = buffer.indexOf("\n");
131
+ while (idx >= 0) {
132
+ const line = buffer.slice(0, idx);
133
+ buffer = buffer.slice(idx + 1);
134
+ chain = chain.then(() => handle(line)).catch((err) => onError(String(err)));
135
+ idx = buffer.indexOf("\n");
136
+ }
137
+ });
138
+ stdin.on("end", () => {
139
+ chain = chain
140
+ .then(() => (buffer.trim() === "" ? undefined : handle(buffer)))
141
+ .then(() => resolve())
142
+ .catch((err) => {
143
+ onError(String(err));
144
+ resolve();
145
+ });
146
+ });
147
+ stdin.on("error", (err: unknown) => {
148
+ onError(String(err));
149
+ resolve();
150
+ });
151
+ });
152
+ }
153
+
154
+ async function dispatch(
155
+ server: McpServer,
156
+ req: JsonRpcRequest,
157
+ options: StdioServerOptions,
158
+ ): Promise<Record<string, unknown>> {
159
+ switch (req.method) {
160
+ case "initialize":
161
+ return {
162
+ protocolVersion: PROTOCOL_VERSION,
163
+ capabilities: { tools: {} },
164
+ serverInfo: { name: options.name, version: options.version ?? "0.0.0" },
165
+ };
166
+ case "notifications/initialized":
167
+ return {};
168
+ case "tools/list":
169
+ return {
170
+ tools: server.listTools().map((t) => ({
171
+ name: t.name,
172
+ description: t.description,
173
+ inputSchema: t.inputSchema,
174
+ })),
175
+ };
176
+ case "tools/call": {
177
+ const params = req.params ?? {};
178
+ const name = typeof params.name === "string" ? params.name : "";
179
+ const args =
180
+ params.arguments && typeof params.arguments === "object"
181
+ ? (params.arguments as Record<string, unknown>)
182
+ : {};
183
+ const result = await server.callTool(name, args);
184
+ return result as unknown as Record<string, unknown>;
185
+ }
186
+ default:
187
+ throw new JsonRpcMethodError(`Method not found: ${req.method}`);
188
+ }
189
+ }
190
+
191
+ class JsonRpcMethodError extends Error {}
@@ -0,0 +1,63 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { McpToolDefinition } from "./mcp.ts";
3
+ import {
4
+ filterToolsForMode,
5
+ parseAgentMode,
6
+ toolsAllowedInMode,
7
+ tryParseAgentMode,
8
+ } from "./mode.ts";
9
+
10
+ function stub(name: string): McpToolDefinition {
11
+ return {
12
+ name,
13
+ description: name,
14
+ inputSchema: { type: "object", properties: {} },
15
+ async handler() {
16
+ return { content: [{ type: "text", text: name }] };
17
+ },
18
+ };
19
+ }
20
+
21
+ const tools = [
22
+ stub("read_file"),
23
+ stub("write_file"),
24
+ stub("todo_write"),
25
+ stub("exec"),
26
+ stub("git_status"),
27
+ stub("git_commit"),
28
+ stub("task"),
29
+ ];
30
+
31
+ describe("agent mode", () => {
32
+ test("parses known modes and rejects the rest", () => {
33
+ expect(tryParseAgentMode("ASK")).toBe("ask");
34
+ expect(tryParseAgentMode("plan")).toBe("plan");
35
+ expect(tryParseAgentMode("agent")).toBe("agent");
36
+ expect(tryParseAgentMode("debug")).toBeUndefined();
37
+ expect(parseAgentMode("nope")).toBe("agent");
38
+ expect(parseAgentMode(undefined)).toBe("agent");
39
+ });
40
+
41
+ test("ask is read-only", () => {
42
+ const names = filterToolsForMode("ask", tools).map((tool) => tool.name);
43
+ expect(names).toEqual(["read_file", "git_status"]);
44
+ expect(toolsAllowedInMode("ask", "write_file")).toBe(false);
45
+ expect(toolsAllowedInMode("ask", "todo_write")).toBe(false);
46
+ expect(toolsAllowedInMode("ask", "exec")).toBe(false);
47
+ expect(toolsAllowedInMode("ask", "git_commit")).toBe(false);
48
+ expect(toolsAllowedInMode("ask", "task")).toBe(false);
49
+ });
50
+
51
+ test("plan adds todos only", () => {
52
+ const names = filterToolsForMode("plan", tools).map((tool) => tool.name);
53
+ expect(names).toEqual(["read_file", "todo_write", "git_status"]);
54
+ expect(toolsAllowedInMode("plan", "write_file")).toBe(false);
55
+ expect(toolsAllowedInMode("plan", "git_commit")).toBe(false);
56
+ });
57
+
58
+ test("agent keeps every tool", () => {
59
+ expect(filterToolsForMode("agent", tools).map((tool) => tool.name)).toEqual(
60
+ tools.map((tool) => tool.name),
61
+ );
62
+ });
63
+ });
@@ -0,0 +1,60 @@
1
+ import type { McpToolDefinition } from "./mcp.ts";
2
+
3
+ export const AGENT_MODES = ["ask", "plan", "agent"] as const;
4
+ export type AgentMode = (typeof AGENT_MODES)[number];
5
+
6
+ const READ_TOOLS = new Set([
7
+ "read_file",
8
+ "glob",
9
+ "grep",
10
+ "read_skill",
11
+ "git_status",
12
+ "git_diff",
13
+ "git_log",
14
+ ]);
15
+
16
+ const PLAN_TOOLS = new Set([...READ_TOOLS, "todo_write"]);
17
+
18
+ export function tryParseAgentMode(raw: string | undefined): AgentMode | undefined {
19
+ const value = raw?.trim().toLowerCase();
20
+ return value === "ask" || value === "plan" || value === "agent" ? value : undefined;
21
+ }
22
+
23
+ export function parseAgentMode(raw: string | undefined): AgentMode {
24
+ return tryParseAgentMode(raw) ?? "agent";
25
+ }
26
+
27
+ export function toolsAllowedInMode(mode: AgentMode, name: string): boolean {
28
+ if (mode === "agent") return true;
29
+ if (mode === "plan") return PLAN_TOOLS.has(name);
30
+ return READ_TOOLS.has(name);
31
+ }
32
+
33
+ export function filterToolsForMode(
34
+ mode: AgentMode,
35
+ tools: McpToolDefinition[],
36
+ ): McpToolDefinition[] {
37
+ return tools.filter((tool) => toolsAllowedInMode(mode, tool.name));
38
+ }
39
+
40
+ export function modePrompt(mode: AgentMode): string {
41
+ if (mode === "ask") {
42
+ return [
43
+ "## Mode: ask",
44
+ "Answer questions only. You may read the repo. You cannot edit files, run exec, commit, or spawn task.",
45
+ "If a change is needed, describe it. Do not pretend you applied it.",
46
+ ].join("\n");
47
+ }
48
+ if (mode === "plan") {
49
+ return [
50
+ "## Mode: plan",
51
+ "Inspect the repo and write a plan. Use todo_write for the steps.",
52
+ "Do not edit files, run exec, or commit. Wait for agent mode to implement.",
53
+ ].join("\n");
54
+ }
55
+ return [
56
+ "## Mode: agent",
57
+ "You may edit files, run approved exec, and commit when the user asked for the change.",
58
+ "Use todo_write for multi-step work. Prefer git_status / git_diff / git_log over exec for git.",
59
+ ].join("\n");
60
+ }