mu-harness 0.17.1 → 0.17.3

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.
@@ -2,3 +2,4 @@ export type { Agent, GrantValue, ToolDecision, ToolGrants } from './types.js';
2
2
  export { type AgentRegistry, createAgentRegistry, grantArg, toolDecision, toolNames } from './registry.js';
3
3
  export { parseAgent } from './parser.js';
4
4
  export { loadAgents } from './loader.js';
5
+ export { createAgentWriterTool } from './writer.js';
@@ -1,3 +1,4 @@
1
1
  export { createAgentRegistry, grantArg, toolDecision, toolNames } from './registry.js';
2
2
  export { parseAgent } from './parser.js';
3
3
  export { loadAgents } from './loader.js';
4
+ export { createAgentWriterTool } from './writer.js';
@@ -2,6 +2,13 @@ import type { Agent, ToolDecision } from './types.js';
2
2
  export interface AgentRegistry {
3
3
  list(): Agent[];
4
4
  get(name: string): Agent | undefined;
5
+ /**
6
+ * Register (or replace) an agent at runtime — mirrors {@link SkillRegistry.add}.
7
+ * Lets tools like `create_agent` make a freshly-authored agent immediately
8
+ * delegatable without a restart. Agents that `extends` the added one are
9
+ * re-resolved so they pick up the change.
10
+ */
11
+ add(agent: Agent): void;
5
12
  }
6
13
  export declare const grantArg: (tool: string, input: unknown) => string | undefined;
7
14
  export declare const toolDecision: (agent: Agent, tool: string, arg?: string) => ToolDecision;
@@ -84,5 +84,12 @@ export const createAgentRegistry = (agents = []) => {
84
84
  return {
85
85
  list: () => [...byName.values()],
86
86
  get: (name) => byName.get(name),
87
+ add: (agent) => {
88
+ raw.set(agent.name, agent);
89
+ byName.set(agent.name, resolve(agent.name, new Set()));
90
+ for (const [name, a] of raw)
91
+ if (a.extends === agent.name)
92
+ byName.set(name, resolve(name, new Set()));
93
+ },
87
94
  };
88
95
  };
@@ -0,0 +1,14 @@
1
+ import type { Tool } from 'mu-core';
2
+ import type { Scope } from '../common/index.js';
3
+ import type { AgentRegistry } from './registry.js';
4
+ /**
5
+ * `create_agent` — authors a reusable agent definition (`.md` with frontmatter)
6
+ * and registers it live via {@link AgentRegistry.add}, so it can be delegated to
7
+ * through `subagent` without a restart. Mirrors {@link createSkillWriterTool}:
8
+ * the `scope` selects the save location, or a configured `forceScope` pins it.
9
+ */
10
+ export declare const createAgentWriterTool: (deps: {
11
+ dirs: Record<Scope, string>;
12
+ registry: AgentRegistry;
13
+ forceScope?: Scope;
14
+ }) => Tool;
@@ -0,0 +1,81 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { mkdir, writeFile } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { stringify as stringifyYaml } from '../deps/jsr.io/@std/yaml/1.1.0/mod.js';
5
+ import { parseAgent } from './parser.js';
6
+ const slug = (name) => name.trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
7
+ /**
8
+ * `create_agent` — authors a reusable agent definition (`.md` with frontmatter)
9
+ * and registers it live via {@link AgentRegistry.add}, so it can be delegated to
10
+ * through `subagent` without a restart. Mirrors {@link createSkillWriterTool}:
11
+ * the `scope` selects the save location, or a configured `forceScope` pins it.
12
+ */
13
+ export const createAgentWriterTool = (deps) => {
14
+ const { forceScope } = deps;
15
+ const scopeProp = forceScope ? {} : {
16
+ scope: {
17
+ type: 'string',
18
+ enum: ['local', 'config'],
19
+ description: 'Where to save it: "local" = this project (repo-first), "config" = global config dir. Defaults to "local".',
20
+ },
21
+ };
22
+ return {
23
+ name: 'create_agent',
24
+ description: forceScope
25
+ ? `Define a reusable agent (name, description, system prompt, optional per-tool grants) that can be delegated to via \`subagent\`. Always saved to the "${forceScope}" agents directory.`
26
+ : 'Define a reusable agent (name, description, system prompt, optional per-tool grants) that can be delegated to via `subagent`. `scope: "local"` saves it to this project, `scope: "config"` makes it available across all projects.',
27
+ parameters: {
28
+ type: 'object',
29
+ properties: {
30
+ name: { type: 'string', description: 'Short agent name (kebab-case); also the filename.' },
31
+ description: { type: 'string', description: 'One line describing what this agent is for.' },
32
+ prompt: { type: 'string', description: 'The system prompt that defines the agent.' },
33
+ tools: {
34
+ type: 'object',
35
+ description: 'Optional per-tool grants: map a tool name to "allow" | "ask" | "deny" (or a nested {glob: decision} map). Omitted tools are denied — be explicit about what it may use.',
36
+ additionalProperties: true,
37
+ },
38
+ model: { type: 'string', description: 'Optional model ref override.' },
39
+ color: { type: 'string', description: 'Optional hex color for the UI.' },
40
+ ...scopeProp,
41
+ },
42
+ required: ['name', 'description', 'prompt'],
43
+ additionalProperties: false,
44
+ },
45
+ run: async (input) => {
46
+ const { name, description, prompt, tools, model, color, scope } = (input ?? {});
47
+ if (!name || !description || !prompt) {
48
+ return [{ type: 'text', text: 'Error: create_agent requires `name`, `description`, and `prompt`.' }];
49
+ }
50
+ // A configured forceScope wins over whatever the model passed.
51
+ const resolved = forceScope ?? scope ?? 'local';
52
+ const base = deps.dirs[resolved];
53
+ if (!base)
54
+ return [{ type: 'text', text: `Error: unknown scope "${scope}".` }];
55
+ const id = slug(name);
56
+ if (!id)
57
+ return [{ type: 'text', text: `Error: invalid agent name "${name}".` }];
58
+ if (deps.registry.get(id)) {
59
+ return [{ type: 'text', text: `Error: an agent named "${id}" already exists.` }];
60
+ }
61
+ const file = join(base, `${id}.md`);
62
+ if (existsSync(file))
63
+ return [{ type: 'text', text: `Error: ${file} already exists.` }];
64
+ const frontmatter = { name: id, description };
65
+ if (model)
66
+ frontmatter.model = model;
67
+ if (color)
68
+ frontmatter.color = color;
69
+ if (tools && typeof tools === 'object')
70
+ frontmatter.tools = tools;
71
+ const source = `---\n${stringifyYaml(frontmatter).trimEnd()}\n---\n\n${prompt.trim()}\n`;
72
+ await mkdir(base, { recursive: true });
73
+ await writeFile(file, source, 'utf-8');
74
+ deps.registry.add(parseAgent(source, id));
75
+ return [{
76
+ type: 'text',
77
+ text: `Created agent "${id}" at ${file}. It can now be delegated to via the \`subagent\` tool.`,
78
+ }];
79
+ },
80
+ };
81
+ };
@@ -1,2 +1,3 @@
1
1
  export { createEmitter, type Emitter, strList } from './utils.js';
2
2
  export { type Frontmatter, parseFrontmatter, str } from './frontmatter.js';
3
+ export type { Scope } from './scope.js';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Where a host saves agent-authored definitions (skills, sub-agents, …):
3
+ * - `local` → the current project (repo-first, e.g. `<cwd>/skills`).
4
+ * - `config` → the global config dir, shared across every project.
5
+ *
6
+ * Generic across definition kinds so every writer tool speaks the same vocabulary.
7
+ */
8
+ export type Scope = 'local' | 'config';
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,7 @@
1
+ import os from 'node:os';
1
2
  import { join } from 'node:path';
2
3
  import process from 'node:process';
3
- import { createAgentRegistry, grantArg, loadAgents, toolDecision, toolNames } from '../agents/index.js';
4
+ import { createAgentRegistry, createAgentWriterTool, grantArg, loadAgents, toolDecision, toolNames } from '../agents/index.js';
4
5
  import { createAgentsCommand, createCommandRegistry, createHelpCommand, createSessionsCommand, createSkillsCommand, } from '../commands/index.js';
5
6
  import { createHarnessConfig } from '../config/index.js';
6
7
  import { mergeHooks } from '../hooks/index.js';
@@ -10,6 +11,7 @@ import { createScheduler, createScheduleTaskTool, createTasksCommand, createTask
10
11
  import { createAgentSession, createSessionCatalog, createSessionManager, createSessionStore, persistTo, runTitler, } from '../session/index.js';
11
12
  import { createRunSkillTool, createSkillRegistry, createSkillTool, createSkillWriterTool, loadSkills, runSkill, } from '../skills/index.js';
12
13
  import { createSubAgentRegistry, createSubAgentTool, runSubAgent } from '../subAgents/index.js';
14
+ import { environmentBlock } from './environment.js';
13
15
  import { createModelRegistry } from './models.js';
14
16
  const TITLE_AGENT = {
15
17
  name: 'title',
@@ -18,17 +20,33 @@ const TITLE_AGENT = {
18
20
  tools: [],
19
21
  };
20
22
  export const createHarness = async (options) => {
21
- const { hostName, xdg, providers, model, agents: hostAgents = [], skills: hostSkills = [], skillScope, title, titleModel, scheduler: enableScheduler = false, approvals, ...sessionDefaults } = options;
23
+ const { hostName, xdg, providers, model, agents: hostAgents = [], skills: hostSkills = [], skillScope, agentScope, agentDirs, title, titleModel, scheduler: enableScheduler = false, approvals, ...sessionDefaults } = options;
22
24
  const cwd = options.cwd ?? process.cwd();
23
25
  const config = createHarnessConfig({ hostName, xdg });
24
26
  const models = createModelRegistry({ providers, default: model });
25
- const plugins = createPluginStore({ dir: join(config.configDir, 'plugins') });
27
+ const pluginsDir = join(config.configDir, 'plugins');
28
+ const agentsDir = agentDirs?.config ?? join(config.configDir, 'agents');
29
+ const localAgentsDir = agentDirs?.local ?? join(cwd, 'agents');
30
+ const plugins = createPluginStore({ dir: pluginsDir });
26
31
  const newId = () => crypto.randomUUID();
27
32
  const pluginAgents = (sessionDefaults.plugins ?? []).flatMap((plugin) => plugin.agents ?? []);
28
- const diskAgents = await loadAgents(join(config.configDir, 'agents'));
29
- const agents = createAgentRegistry([...hostAgents, ...pluginAgents, ...diskAgents]);
33
+ const localAgents = await loadAgents(localAgentsDir);
34
+ const diskAgents = await loadAgents(agentsDir);
35
+ const agents = createAgentRegistry([...hostAgents, ...pluginAgents, ...localAgents, ...diskAgents]);
30
36
  const skillsDir = join(config.configDir, 'skills');
31
37
  const cwdSkillsDir = join(cwd, 'skills');
38
+ const envBlock = environmentBlock({
39
+ os: `${os.platform()} ${os.release()} (${os.arch()})`,
40
+ configDir: config.configDir,
41
+ pluginsDir,
42
+ skillsDir,
43
+ agentsDir,
44
+ hostName,
45
+ hostSourceUrl: options.sourceUrl,
46
+ });
47
+ const envHook = {
48
+ prepareRequest: ({ system }) => ({ system: system ? `${system}\n\n${envBlock}` : envBlock }),
49
+ };
32
50
  const pluginSkills = (sessionDefaults.plugins ?? []).flatMap((plugin) => plugin.skills ?? []);
33
51
  const cwdSkills = await loadSkills(cwdSkillsDir);
34
52
  const diskSkills = await loadSkills(skillsDir);
@@ -38,6 +56,11 @@ export const createHarness = async (options) => {
38
56
  registry: skills,
39
57
  forceScope: skillScope,
40
58
  });
59
+ const agentWriterTool = createAgentWriterTool({
60
+ dirs: { local: localAgentsDir, config: agentsDir },
61
+ registry: agents,
62
+ forceScope: agentScope,
63
+ });
41
64
  const scopeSkills = (agent) => {
42
65
  if (!agent)
43
66
  return skills;
@@ -52,6 +75,7 @@ export const createHarness = async (options) => {
52
75
  ...extra,
53
76
  createSkillTool(scopeSkills(agent)),
54
77
  skillWriterTool,
78
+ agentWriterTool,
55
79
  ...schedulerTools,
56
80
  ];
57
81
  const approvalHook = (getAgent) => approvals
@@ -77,7 +101,7 @@ export const createHarness = async (options) => {
77
101
  });
78
102
  const spawn = (agent) => persistTo(store, persona(agent, {
79
103
  tools: sessionTools(agent),
80
- hooks: mergeHooks([sessionDefaults.hooks, allowList(toolNames(agent)), approvalHook(() => agent)]),
104
+ hooks: mergeHooks([sessionDefaults.hooks, allowList(toolNames(agent)), approvalHook(() => agent), envHook]),
81
105
  }));
82
106
  const scheduler = enableScheduler && tasks
83
107
  ? createScheduler({
@@ -125,7 +149,7 @@ export const createHarness = async (options) => {
125
149
  },
126
150
  revive: ({ id, model: ref, messages }) => createAgentSession({
127
151
  ...sessionDefaults,
128
- hooks: mergeHooks([sessionDefaults.hooks, approvalHook(() => approvals?.activeAgent())]),
152
+ hooks: mergeHooks([sessionDefaults.hooks, approvalHook(() => approvals?.activeAgent()), envHook]),
129
153
  tools: sessionTools(undefined, [createSubAgentTool({ registry: agents, spawn, runs, parentId: id })]),
130
154
  ...models.resolve(ref),
131
155
  id,
@@ -0,0 +1,10 @@
1
+ export interface EnvironmentInfo {
2
+ os: string;
3
+ configDir: string;
4
+ pluginsDir: string;
5
+ skillsDir: string;
6
+ agentsDir: string;
7
+ hostName: string;
8
+ hostSourceUrl?: string;
9
+ }
10
+ export declare function environmentBlock(info: EnvironmentInfo): string;
@@ -0,0 +1,14 @@
1
+ const HARNESS_SOURCE_URL = 'https://github.com/gaetan-puleo/mu-ai';
2
+ export function environmentBlock(info) {
3
+ const lines = [
4
+ `Operating system: ${info.os}`,
5
+ `Config directory: ${info.configDir}`,
6
+ `Plugins directory: ${info.pluginsDir}`,
7
+ `Skills directory: ${info.skillsDir}`,
8
+ `Sub-agents directory: ${info.agentsDir}`,
9
+ `Harness (mu) source code: ${HARNESS_SOURCE_URL}`,
10
+ ];
11
+ if (info.hostSourceUrl)
12
+ lines.push(`${info.hostName} source code: ${info.hostSourceUrl}`);
13
+ return `<env>\n${lines.join('\n')}\n</env>`;
14
+ }
@@ -1,12 +1,13 @@
1
1
  import type { Provider } from 'mu-core';
2
2
  import type { Agent, AgentRegistry, ToolDecision } from '../agents/index.js';
3
3
  import type { CommandRegistry } from '../commands/index.js';
4
+ import type { Scope } from '../common/index.js';
4
5
  import type { HarnessConfig, HarnessConfigOptions } from '../config/index.js';
5
6
  import type { ApprovalManager } from '../permissions/index.js';
6
7
  import type { PluginStore } from '../plugin/index.js';
7
8
  import type { Scheduler, TaskStore } from '../scheduler/index.js';
8
9
  import type { AgentSessionConfig, SessionManager } from '../session/index.js';
9
- import type { Skill, SkillRegistry, SkillScope } from '../skills/index.js';
10
+ import type { Skill, SkillRegistry } from '../skills/index.js';
10
11
  import type { SubAgentRegistry, SubAgentResult } from '../subAgents/index.js';
11
12
  import type { ModelRegistry } from './models.js';
12
13
  export type HarnessOptions = HarnessConfigOptions & Omit<AgentSessionConfig, 'provider' | 'model' | 'id' | 'messages'> & {
@@ -19,10 +20,24 @@ export type HarnessOptions = HarnessConfigOptions & Omit<AgentSessionConfig, 'pr
19
20
  * argument is overridden (and dropped from the tool schema). Unset → the
20
21
  * model chooses, defaulting to 'local'.
21
22
  */
22
- skillScope?: SkillScope;
23
+ skillScope?: Scope;
24
+ /**
25
+ * Forces the save location of `create_agent`. Same semantics as
26
+ * {@link skillScope}: set to pin the scope, leave unset to let the model choose.
27
+ */
28
+ agentScope?: Scope;
29
+ /**
30
+ * Overrides where `create_agent` writes (and which dirs are loaded at boot).
31
+ * Defaults to `{ local: <cwd>/agents, config: <configDir>/agents }`.
32
+ */
33
+ agentDirs?: {
34
+ local?: string;
35
+ config?: string;
36
+ };
23
37
  title?: boolean;
24
38
  titleModel?: string;
25
39
  cwd?: string;
40
+ sourceUrl?: string;
26
41
  scheduler?: boolean;
27
42
  approvals?: {
28
43
  manager: ApprovalManager;
@@ -4,5 +4,5 @@ export { parseSkill } from './parser.js';
4
4
  export { loadSkills } from './loader.js';
5
5
  export { skillMatchesPlatform } from './platform.js';
6
6
  export { createSkillTool } from './tool.js';
7
- export { createSkillWriterTool, type SkillScope } from './writer.js';
7
+ export { createSkillWriterTool } from './writer.js';
8
8
  export { createRunSkillTool, runSkill, type RunSkillDeps } from './run.js';
@@ -1,8 +1,8 @@
1
1
  import type { Tool } from 'mu-core';
2
+ import type { Scope } from '../common/index.js';
2
3
  import type { SkillRegistry } from './registry.js';
3
- export type SkillScope = 'local' | 'config';
4
4
  export declare const createSkillWriterTool: (deps: {
5
- dirs: Record<SkillScope, string>;
5
+ dirs: Record<Scope, string>;
6
6
  registry: SkillRegistry;
7
- forceScope?: SkillScope;
7
+ forceScope?: Scope;
8
8
  }) => Tool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mu-harness",
3
- "version": "0.17.1",
3
+ "version": "0.17.3",
4
4
  "description": "Agent harness: createHarness wires mu-core into a host — XDG paths, model registry, plugins, disk-loaded agents & skills, sub-agents, sessions (JSONL + SQLite catalog), slash commands, permission/approval hooks, an optional scheduler, and a composable TUI chat app",
5
5
  "license": "MIT",
6
6
  "main": "./script/index.js",
@@ -23,8 +23,8 @@
23
23
  "@swc/wasm-typescript": "^1.15.0",
24
24
  "cli-highlight": "^2.1.11",
25
25
  "croner": "^9.0.0",
26
- "mu-core": "^0.17.1",
27
- "mu-tui": "^0.17.1"
26
+ "mu-core": "^0.17.3",
27
+ "mu-tui": "^0.17.3"
28
28
  },
29
29
  "_generatedBy": "dnt@dev",
30
30
  "types": "./esm/index.d.ts"
@@ -2,3 +2,4 @@ export type { Agent, GrantValue, ToolDecision, ToolGrants } from './types.js';
2
2
  export { type AgentRegistry, createAgentRegistry, grantArg, toolDecision, toolNames } from './registry.js';
3
3
  export { parseAgent } from './parser.js';
4
4
  export { loadAgents } from './loader.js';
5
+ export { createAgentWriterTool } from './writer.js';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadAgents = exports.parseAgent = exports.toolNames = exports.toolDecision = exports.grantArg = exports.createAgentRegistry = void 0;
3
+ exports.createAgentWriterTool = exports.loadAgents = exports.parseAgent = exports.toolNames = exports.toolDecision = exports.grantArg = exports.createAgentRegistry = void 0;
4
4
  var registry_js_1 = require("./registry.js");
5
5
  Object.defineProperty(exports, "createAgentRegistry", { enumerable: true, get: function () { return registry_js_1.createAgentRegistry; } });
6
6
  Object.defineProperty(exports, "grantArg", { enumerable: true, get: function () { return registry_js_1.grantArg; } });
@@ -10,3 +10,5 @@ var parser_js_1 = require("./parser.js");
10
10
  Object.defineProperty(exports, "parseAgent", { enumerable: true, get: function () { return parser_js_1.parseAgent; } });
11
11
  var loader_js_1 = require("./loader.js");
12
12
  Object.defineProperty(exports, "loadAgents", { enumerable: true, get: function () { return loader_js_1.loadAgents; } });
13
+ var writer_js_1 = require("./writer.js");
14
+ Object.defineProperty(exports, "createAgentWriterTool", { enumerable: true, get: function () { return writer_js_1.createAgentWriterTool; } });
@@ -2,6 +2,13 @@ import type { Agent, ToolDecision } from './types.js';
2
2
  export interface AgentRegistry {
3
3
  list(): Agent[];
4
4
  get(name: string): Agent | undefined;
5
+ /**
6
+ * Register (or replace) an agent at runtime — mirrors {@link SkillRegistry.add}.
7
+ * Lets tools like `create_agent` make a freshly-authored agent immediately
8
+ * delegatable without a restart. Agents that `extends` the added one are
9
+ * re-resolved so they pick up the change.
10
+ */
11
+ add(agent: Agent): void;
5
12
  }
6
13
  export declare const grantArg: (tool: string, input: unknown) => string | undefined;
7
14
  export declare const toolDecision: (agent: Agent, tool: string, arg?: string) => ToolDecision;
@@ -90,6 +90,13 @@ const createAgentRegistry = (agents = []) => {
90
90
  return {
91
91
  list: () => [...byName.values()],
92
92
  get: (name) => byName.get(name),
93
+ add: (agent) => {
94
+ raw.set(agent.name, agent);
95
+ byName.set(agent.name, resolve(agent.name, new Set()));
96
+ for (const [name, a] of raw)
97
+ if (a.extends === agent.name)
98
+ byName.set(name, resolve(name, new Set()));
99
+ },
93
100
  };
94
101
  };
95
102
  exports.createAgentRegistry = createAgentRegistry;
@@ -0,0 +1,14 @@
1
+ import type { Tool } from 'mu-core';
2
+ import type { Scope } from '../common/index.js';
3
+ import type { AgentRegistry } from './registry.js';
4
+ /**
5
+ * `create_agent` — authors a reusable agent definition (`.md` with frontmatter)
6
+ * and registers it live via {@link AgentRegistry.add}, so it can be delegated to
7
+ * through `subagent` without a restart. Mirrors {@link createSkillWriterTool}:
8
+ * the `scope` selects the save location, or a configured `forceScope` pins it.
9
+ */
10
+ export declare const createAgentWriterTool: (deps: {
11
+ dirs: Record<Scope, string>;
12
+ registry: AgentRegistry;
13
+ forceScope?: Scope;
14
+ }) => Tool;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAgentWriterTool = void 0;
4
+ const node_fs_1 = require("node:fs");
5
+ const promises_1 = require("node:fs/promises");
6
+ const node_path_1 = require("node:path");
7
+ const mod_js_1 = require("../deps/jsr.io/@std/yaml/1.1.0/mod.js");
8
+ const parser_js_1 = require("./parser.js");
9
+ const slug = (name) => name.trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
10
+ /**
11
+ * `create_agent` — authors a reusable agent definition (`.md` with frontmatter)
12
+ * and registers it live via {@link AgentRegistry.add}, so it can be delegated to
13
+ * through `subagent` without a restart. Mirrors {@link createSkillWriterTool}:
14
+ * the `scope` selects the save location, or a configured `forceScope` pins it.
15
+ */
16
+ const createAgentWriterTool = (deps) => {
17
+ const { forceScope } = deps;
18
+ const scopeProp = forceScope ? {} : {
19
+ scope: {
20
+ type: 'string',
21
+ enum: ['local', 'config'],
22
+ description: 'Where to save it: "local" = this project (repo-first), "config" = global config dir. Defaults to "local".',
23
+ },
24
+ };
25
+ return {
26
+ name: 'create_agent',
27
+ description: forceScope
28
+ ? `Define a reusable agent (name, description, system prompt, optional per-tool grants) that can be delegated to via \`subagent\`. Always saved to the "${forceScope}" agents directory.`
29
+ : 'Define a reusable agent (name, description, system prompt, optional per-tool grants) that can be delegated to via `subagent`. `scope: "local"` saves it to this project, `scope: "config"` makes it available across all projects.',
30
+ parameters: {
31
+ type: 'object',
32
+ properties: {
33
+ name: { type: 'string', description: 'Short agent name (kebab-case); also the filename.' },
34
+ description: { type: 'string', description: 'One line describing what this agent is for.' },
35
+ prompt: { type: 'string', description: 'The system prompt that defines the agent.' },
36
+ tools: {
37
+ type: 'object',
38
+ description: 'Optional per-tool grants: map a tool name to "allow" | "ask" | "deny" (or a nested {glob: decision} map). Omitted tools are denied — be explicit about what it may use.',
39
+ additionalProperties: true,
40
+ },
41
+ model: { type: 'string', description: 'Optional model ref override.' },
42
+ color: { type: 'string', description: 'Optional hex color for the UI.' },
43
+ ...scopeProp,
44
+ },
45
+ required: ['name', 'description', 'prompt'],
46
+ additionalProperties: false,
47
+ },
48
+ run: async (input) => {
49
+ const { name, description, prompt, tools, model, color, scope } = (input ?? {});
50
+ if (!name || !description || !prompt) {
51
+ return [{ type: 'text', text: 'Error: create_agent requires `name`, `description`, and `prompt`.' }];
52
+ }
53
+ // A configured forceScope wins over whatever the model passed.
54
+ const resolved = forceScope ?? scope ?? 'local';
55
+ const base = deps.dirs[resolved];
56
+ if (!base)
57
+ return [{ type: 'text', text: `Error: unknown scope "${scope}".` }];
58
+ const id = slug(name);
59
+ if (!id)
60
+ return [{ type: 'text', text: `Error: invalid agent name "${name}".` }];
61
+ if (deps.registry.get(id)) {
62
+ return [{ type: 'text', text: `Error: an agent named "${id}" already exists.` }];
63
+ }
64
+ const file = (0, node_path_1.join)(base, `${id}.md`);
65
+ if ((0, node_fs_1.existsSync)(file))
66
+ return [{ type: 'text', text: `Error: ${file} already exists.` }];
67
+ const frontmatter = { name: id, description };
68
+ if (model)
69
+ frontmatter.model = model;
70
+ if (color)
71
+ frontmatter.color = color;
72
+ if (tools && typeof tools === 'object')
73
+ frontmatter.tools = tools;
74
+ const source = `---\n${(0, mod_js_1.stringify)(frontmatter).trimEnd()}\n---\n\n${prompt.trim()}\n`;
75
+ await (0, promises_1.mkdir)(base, { recursive: true });
76
+ await (0, promises_1.writeFile)(file, source, 'utf-8');
77
+ deps.registry.add((0, parser_js_1.parseAgent)(source, id));
78
+ return [{
79
+ type: 'text',
80
+ text: `Created agent "${id}" at ${file}. It can now be delegated to via the \`subagent\` tool.`,
81
+ }];
82
+ },
83
+ };
84
+ };
85
+ exports.createAgentWriterTool = createAgentWriterTool;
@@ -1,2 +1,3 @@
1
1
  export { createEmitter, type Emitter, strList } from './utils.js';
2
2
  export { type Frontmatter, parseFrontmatter, str } from './frontmatter.js';
3
+ export type { Scope } from './scope.js';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Where a host saves agent-authored definitions (skills, sub-agents, …):
3
+ * - `local` → the current project (repo-first, e.g. `<cwd>/skills`).
4
+ * - `config` → the global config dir, shared across every project.
5
+ *
6
+ * Generic across definition kinds so every writer tool speaks the same vocabulary.
7
+ */
8
+ export type Scope = 'local' | 'config';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createHarness = void 0;
7
+ const node_os_1 = __importDefault(require("node:os"));
7
8
  const node_path_1 = require("node:path");
8
9
  const node_process_1 = __importDefault(require("node:process"));
9
10
  const index_js_1 = require("../agents/index.js");
@@ -16,6 +17,7 @@ const index_js_7 = require("../scheduler/index.js");
16
17
  const index_js_8 = require("../session/index.js");
17
18
  const index_js_9 = require("../skills/index.js");
18
19
  const index_js_10 = require("../subAgents/index.js");
20
+ const environment_js_1 = require("./environment.js");
19
21
  const models_js_1 = require("./models.js");
20
22
  const TITLE_AGENT = {
21
23
  name: 'title',
@@ -24,17 +26,33 @@ const TITLE_AGENT = {
24
26
  tools: [],
25
27
  };
26
28
  const createHarness = async (options) => {
27
- const { hostName, xdg, providers, model, agents: hostAgents = [], skills: hostSkills = [], skillScope, title, titleModel, scheduler: enableScheduler = false, approvals, ...sessionDefaults } = options;
29
+ const { hostName, xdg, providers, model, agents: hostAgents = [], skills: hostSkills = [], skillScope, agentScope, agentDirs, title, titleModel, scheduler: enableScheduler = false, approvals, ...sessionDefaults } = options;
28
30
  const cwd = options.cwd ?? node_process_1.default.cwd();
29
31
  const config = (0, index_js_3.createHarnessConfig)({ hostName, xdg });
30
32
  const models = (0, models_js_1.createModelRegistry)({ providers, default: model });
31
- const plugins = (0, index_js_6.createPluginStore)({ dir: (0, node_path_1.join)(config.configDir, 'plugins') });
33
+ const pluginsDir = (0, node_path_1.join)(config.configDir, 'plugins');
34
+ const agentsDir = agentDirs?.config ?? (0, node_path_1.join)(config.configDir, 'agents');
35
+ const localAgentsDir = agentDirs?.local ?? (0, node_path_1.join)(cwd, 'agents');
36
+ const plugins = (0, index_js_6.createPluginStore)({ dir: pluginsDir });
32
37
  const newId = () => crypto.randomUUID();
33
38
  const pluginAgents = (sessionDefaults.plugins ?? []).flatMap((plugin) => plugin.agents ?? []);
34
- const diskAgents = await (0, index_js_1.loadAgents)((0, node_path_1.join)(config.configDir, 'agents'));
35
- const agents = (0, index_js_1.createAgentRegistry)([...hostAgents, ...pluginAgents, ...diskAgents]);
39
+ const localAgents = await (0, index_js_1.loadAgents)(localAgentsDir);
40
+ const diskAgents = await (0, index_js_1.loadAgents)(agentsDir);
41
+ const agents = (0, index_js_1.createAgentRegistry)([...hostAgents, ...pluginAgents, ...localAgents, ...diskAgents]);
36
42
  const skillsDir = (0, node_path_1.join)(config.configDir, 'skills');
37
43
  const cwdSkillsDir = (0, node_path_1.join)(cwd, 'skills');
44
+ const envBlock = (0, environment_js_1.environmentBlock)({
45
+ os: `${node_os_1.default.platform()} ${node_os_1.default.release()} (${node_os_1.default.arch()})`,
46
+ configDir: config.configDir,
47
+ pluginsDir,
48
+ skillsDir,
49
+ agentsDir,
50
+ hostName,
51
+ hostSourceUrl: options.sourceUrl,
52
+ });
53
+ const envHook = {
54
+ prepareRequest: ({ system }) => ({ system: system ? `${system}\n\n${envBlock}` : envBlock }),
55
+ };
38
56
  const pluginSkills = (sessionDefaults.plugins ?? []).flatMap((plugin) => plugin.skills ?? []);
39
57
  const cwdSkills = await (0, index_js_9.loadSkills)(cwdSkillsDir);
40
58
  const diskSkills = await (0, index_js_9.loadSkills)(skillsDir);
@@ -44,6 +62,11 @@ const createHarness = async (options) => {
44
62
  registry: skills,
45
63
  forceScope: skillScope,
46
64
  });
65
+ const agentWriterTool = (0, index_js_1.createAgentWriterTool)({
66
+ dirs: { local: localAgentsDir, config: agentsDir },
67
+ registry: agents,
68
+ forceScope: agentScope,
69
+ });
47
70
  const scopeSkills = (agent) => {
48
71
  if (!agent)
49
72
  return skills;
@@ -58,6 +81,7 @@ const createHarness = async (options) => {
58
81
  ...extra,
59
82
  (0, index_js_9.createSkillTool)(scopeSkills(agent)),
60
83
  skillWriterTool,
84
+ agentWriterTool,
61
85
  ...schedulerTools,
62
86
  ];
63
87
  const approvalHook = (getAgent) => approvals
@@ -83,7 +107,7 @@ const createHarness = async (options) => {
83
107
  });
84
108
  const spawn = (agent) => (0, index_js_8.persistTo)(store, persona(agent, {
85
109
  tools: sessionTools(agent),
86
- hooks: (0, index_js_4.mergeHooks)([sessionDefaults.hooks, (0, index_js_5.allowList)((0, index_js_1.toolNames)(agent)), approvalHook(() => agent)]),
110
+ hooks: (0, index_js_4.mergeHooks)([sessionDefaults.hooks, (0, index_js_5.allowList)((0, index_js_1.toolNames)(agent)), approvalHook(() => agent), envHook]),
87
111
  }));
88
112
  const scheduler = enableScheduler && tasks
89
113
  ? (0, index_js_7.createScheduler)({
@@ -131,7 +155,7 @@ const createHarness = async (options) => {
131
155
  },
132
156
  revive: ({ id, model: ref, messages }) => (0, index_js_8.createAgentSession)({
133
157
  ...sessionDefaults,
134
- hooks: (0, index_js_4.mergeHooks)([sessionDefaults.hooks, approvalHook(() => approvals?.activeAgent())]),
158
+ hooks: (0, index_js_4.mergeHooks)([sessionDefaults.hooks, approvalHook(() => approvals?.activeAgent()), envHook]),
135
159
  tools: sessionTools(undefined, [(0, index_js_10.createSubAgentTool)({ registry: agents, spawn, runs, parentId: id })]),
136
160
  ...models.resolve(ref),
137
161
  id,
@@ -0,0 +1,10 @@
1
+ export interface EnvironmentInfo {
2
+ os: string;
3
+ configDir: string;
4
+ pluginsDir: string;
5
+ skillsDir: string;
6
+ agentsDir: string;
7
+ hostName: string;
8
+ hostSourceUrl?: string;
9
+ }
10
+ export declare function environmentBlock(info: EnvironmentInfo): string;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.environmentBlock = environmentBlock;
4
+ const HARNESS_SOURCE_URL = 'https://github.com/gaetan-puleo/mu-ai';
5
+ function environmentBlock(info) {
6
+ const lines = [
7
+ `Operating system: ${info.os}`,
8
+ `Config directory: ${info.configDir}`,
9
+ `Plugins directory: ${info.pluginsDir}`,
10
+ `Skills directory: ${info.skillsDir}`,
11
+ `Sub-agents directory: ${info.agentsDir}`,
12
+ `Harness (mu) source code: ${HARNESS_SOURCE_URL}`,
13
+ ];
14
+ if (info.hostSourceUrl)
15
+ lines.push(`${info.hostName} source code: ${info.hostSourceUrl}`);
16
+ return `<env>\n${lines.join('\n')}\n</env>`;
17
+ }
@@ -1,12 +1,13 @@
1
1
  import type { Provider } from 'mu-core';
2
2
  import type { Agent, AgentRegistry, ToolDecision } from '../agents/index.js';
3
3
  import type { CommandRegistry } from '../commands/index.js';
4
+ import type { Scope } from '../common/index.js';
4
5
  import type { HarnessConfig, HarnessConfigOptions } from '../config/index.js';
5
6
  import type { ApprovalManager } from '../permissions/index.js';
6
7
  import type { PluginStore } from '../plugin/index.js';
7
8
  import type { Scheduler, TaskStore } from '../scheduler/index.js';
8
9
  import type { AgentSessionConfig, SessionManager } from '../session/index.js';
9
- import type { Skill, SkillRegistry, SkillScope } from '../skills/index.js';
10
+ import type { Skill, SkillRegistry } from '../skills/index.js';
10
11
  import type { SubAgentRegistry, SubAgentResult } from '../subAgents/index.js';
11
12
  import type { ModelRegistry } from './models.js';
12
13
  export type HarnessOptions = HarnessConfigOptions & Omit<AgentSessionConfig, 'provider' | 'model' | 'id' | 'messages'> & {
@@ -19,10 +20,24 @@ export type HarnessOptions = HarnessConfigOptions & Omit<AgentSessionConfig, 'pr
19
20
  * argument is overridden (and dropped from the tool schema). Unset → the
20
21
  * model chooses, defaulting to 'local'.
21
22
  */
22
- skillScope?: SkillScope;
23
+ skillScope?: Scope;
24
+ /**
25
+ * Forces the save location of `create_agent`. Same semantics as
26
+ * {@link skillScope}: set to pin the scope, leave unset to let the model choose.
27
+ */
28
+ agentScope?: Scope;
29
+ /**
30
+ * Overrides where `create_agent` writes (and which dirs are loaded at boot).
31
+ * Defaults to `{ local: <cwd>/agents, config: <configDir>/agents }`.
32
+ */
33
+ agentDirs?: {
34
+ local?: string;
35
+ config?: string;
36
+ };
23
37
  title?: boolean;
24
38
  titleModel?: string;
25
39
  cwd?: string;
40
+ sourceUrl?: string;
26
41
  scheduler?: boolean;
27
42
  approvals?: {
28
43
  manager: ApprovalManager;
@@ -4,5 +4,5 @@ export { parseSkill } from './parser.js';
4
4
  export { loadSkills } from './loader.js';
5
5
  export { skillMatchesPlatform } from './platform.js';
6
6
  export { createSkillTool } from './tool.js';
7
- export { createSkillWriterTool, type SkillScope } from './writer.js';
7
+ export { createSkillWriterTool } from './writer.js';
8
8
  export { createRunSkillTool, runSkill, type RunSkillDeps } from './run.js';
@@ -1,8 +1,8 @@
1
1
  import type { Tool } from 'mu-core';
2
+ import type { Scope } from '../common/index.js';
2
3
  import type { SkillRegistry } from './registry.js';
3
- export type SkillScope = 'local' | 'config';
4
4
  export declare const createSkillWriterTool: (deps: {
5
- dirs: Record<SkillScope, string>;
5
+ dirs: Record<Scope, string>;
6
6
  registry: SkillRegistry;
7
- forceScope?: SkillScope;
7
+ forceScope?: Scope;
8
8
  }) => Tool;