ruleloom 0.0.0-stage → 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.
- package/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/dist/adapters/claude.d.ts +3 -0
- package/dist/adapters/claude.d.ts.map +1 -0
- package/dist/adapters/claude.js +59 -0
- package/dist/adapters/claude.js.map +1 -0
- package/dist/adapters/codex.d.ts +3 -0
- package/dist/adapters/codex.d.ts.map +1 -0
- package/dist/adapters/codex.js +84 -0
- package/dist/adapters/codex.js.map +1 -0
- package/dist/adapters/copilot.d.ts +3 -0
- package/dist/adapters/copilot.d.ts.map +1 -0
- package/dist/adapters/copilot.js +53 -0
- package/dist/adapters/copilot.js.map +1 -0
- package/dist/adapters/format.d.ts +27 -0
- package/dist/adapters/format.d.ts.map +1 -0
- package/dist/adapters/format.js +55 -0
- package/dist/adapters/format.js.map +1 -0
- package/dist/adapters/index.d.ts +10 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +11 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/types.d.ts +19 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +2 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/assemble.d.ts +61 -0
- package/dist/assemble.d.ts.map +1 -0
- package/dist/assemble.js +133 -0
- package/dist/assemble.js.map +1 -0
- package/dist/blocks.d.ts +32 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +59 -0
- package/dist/blocks.js.map +1 -0
- package/dist/bundle.d.ts +121 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +157 -0
- package/dist/bundle.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +141 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +360 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +207 -0
- package/dist/config.js.map +1 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +16 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/project.d.ts +149 -0
- package/dist/project.d.ts.map +1 -0
- package/dist/project.js +102 -0
- package/dist/project.js.map +1 -0
- package/dist/prompt.d.ts +62 -0
- package/dist/prompt.d.ts.map +1 -0
- package/dist/prompt.js +91 -0
- package/dist/prompt.js.map +1 -0
- package/dist/resources.d.ts +39 -0
- package/dist/resources.d.ts.map +1 -0
- package/dist/resources.js +118 -0
- package/dist/resources.js.map +1 -0
- package/dist/select.d.ts +14 -0
- package/dist/select.d.ts.map +1 -0
- package/dist/select.js +25 -0
- package/dist/select.js.map +1 -0
- package/dist/slots.d.ts +35 -0
- package/dist/slots.d.ts.map +1 -0
- package/dist/slots.js +86 -0
- package/dist/slots.js.map +1 -0
- package/docs/agent-definition.md +86 -0
- package/docs/api.md +65 -0
- package/docs/architecture.md +47 -0
- package/docs/cli.md +28 -0
- package/docs/errors.md +76 -0
- package/docs/targets.md +49 -0
- package/package.json +51 -4
package/dist/prompt.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { readAgentConfig } from './config.js';
|
|
4
|
+
import { loadBlockTemplates, renderBlock } from './blocks.js';
|
|
5
|
+
import { RuleLoomError } from './errors.js';
|
|
6
|
+
import { ModuleReader } from './resources.js';
|
|
7
|
+
import { selectModules } from './select.js';
|
|
8
|
+
import { bindSlots, resolveAgentSlots, usedSlots } from './slots.js';
|
|
9
|
+
const sha256 = (value) => createHash('sha256').update(value, 'utf8').digest('hex');
|
|
10
|
+
export function compilePrompt(options) {
|
|
11
|
+
const { project } = options;
|
|
12
|
+
const agentDir = join(project.agentsRoot, options.agent);
|
|
13
|
+
const config = readAgentConfig(agentDir);
|
|
14
|
+
const reader = new ModuleReader(project);
|
|
15
|
+
const blocks = loadBlockTemplates(project.blocksRoot, {
|
|
16
|
+
floor: project.blockNumberFloor,
|
|
17
|
+
...(options.expectedBlocks ? { expected: options.expectedBlocks } : {}),
|
|
18
|
+
});
|
|
19
|
+
const blockNames = blocks.map((block) => block.name);
|
|
20
|
+
// Inherited rules open the prompt: their absence changes what the model
|
|
21
|
+
// produces, not how well.
|
|
22
|
+
const inherited = config.sharedRules.map((name) => ({
|
|
23
|
+
path: `shared/rules/${name}`, tags: [], always: true, trailing: false, modes: [],
|
|
24
|
+
}));
|
|
25
|
+
const selected = [
|
|
26
|
+
...inherited,
|
|
27
|
+
...selectModules(config, {
|
|
28
|
+
...(options.mode ? { mode: options.mode } : {}),
|
|
29
|
+
...(options.failedCodes ? { failedCodes: options.failedCodes } : {}),
|
|
30
|
+
}),
|
|
31
|
+
];
|
|
32
|
+
if (selected.length === 0)
|
|
33
|
+
throw new RuleLoomError('PROMPT_NO_MODULES', `nothing selected for ${config.name}`);
|
|
34
|
+
const slots = resolveAgentSlots(config, {
|
|
35
|
+
...(options.random ? { random: options.random } : {}),
|
|
36
|
+
...(options.data ? { data: options.data } : {}),
|
|
37
|
+
});
|
|
38
|
+
const used = new Set();
|
|
39
|
+
const modules = [];
|
|
40
|
+
const parts = [];
|
|
41
|
+
if (config.preamble.length > 0) {
|
|
42
|
+
parts.push(bindSlots(config.preamble.join('\n').trim(), slots, { where: 'the preamble', used, blocks: blockNames }));
|
|
43
|
+
}
|
|
44
|
+
const read = (module) => {
|
|
45
|
+
// Hashed after binding: the record describes what was compiled.
|
|
46
|
+
const body = bindSlots(reader.read(agentDir, module.path), slots, { where: module.path, used, blocks: blockNames });
|
|
47
|
+
modules.push({ path: module.path, sha256: sha256(body), bytes: Buffer.byteLength(body, 'utf8') });
|
|
48
|
+
return body;
|
|
49
|
+
};
|
|
50
|
+
for (const module of selected.filter((m) => !m.trailing))
|
|
51
|
+
parts.push('---', read(module));
|
|
52
|
+
const staticText = `${parts.join('\n\n')}\n`;
|
|
53
|
+
const trailingText = selected
|
|
54
|
+
.filter((module) => module.trailing)
|
|
55
|
+
.map((module) => `---\n\n${read(module)}`)
|
|
56
|
+
.join('\n\n');
|
|
57
|
+
const values = options.values ?? {};
|
|
58
|
+
const cited = blockNames.filter((name) => staticText.includes(`{{${name}}}`) || trailingText.includes(`{{${name}}}`));
|
|
59
|
+
const unresolved = cited.filter((name) => values[name] === undefined);
|
|
60
|
+
const unknownValues = Object.keys(values).filter((name) => !blockNames.includes(name));
|
|
61
|
+
if (unknownValues.length > 0) {
|
|
62
|
+
throw new RuleLoomError('PROMPT_VALUE_UNKNOWN', `${unknownValues.join(', ')} — blocks: ${blockNames.join(', ') || '(none)'}`);
|
|
63
|
+
}
|
|
64
|
+
const dynamicParts = [];
|
|
65
|
+
for (const block of blocks) {
|
|
66
|
+
const value = values[block.name];
|
|
67
|
+
if (value === undefined || value === '')
|
|
68
|
+
continue;
|
|
69
|
+
dynamicParts.push(`---\n\n${renderBlock(block, value)}`);
|
|
70
|
+
}
|
|
71
|
+
const runText = dynamicParts.length > 0 ? `${dynamicParts.join('\n\n')}\n` : '';
|
|
72
|
+
if (trailingText)
|
|
73
|
+
dynamicParts.push(trailingText);
|
|
74
|
+
const dynamicText = dynamicParts.length > 0 ? `${dynamicParts.join('\n\n')}\n` : '';
|
|
75
|
+
return {
|
|
76
|
+
agent: config.name,
|
|
77
|
+
version: config.version,
|
|
78
|
+
static: staticText,
|
|
79
|
+
dynamic: dynamicText,
|
|
80
|
+
runBlocks: runText,
|
|
81
|
+
trailing: trailingText ? `${trailingText}\n` : '',
|
|
82
|
+
text: dynamicText ? `${staticText}\n${dynamicText}` : staticText,
|
|
83
|
+
staticSha256: sha256(staticText),
|
|
84
|
+
modules,
|
|
85
|
+
unresolvedBlocks: unresolved,
|
|
86
|
+
cited: usedSlots(slots, used),
|
|
87
|
+
slots,
|
|
88
|
+
selection: options.failedCodes && options.failedCodes.length > 0 ? 'retry' : 'full',
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../src/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,eAAe,EAAoB,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAiErE,MAAM,MAAM,GAAG,CAAC,KAAa,EAAU,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAEnG,MAAM,UAAU,aAAa,CAAC,OAA6B;IACzD,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAC5B,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IACzD,MAAM,MAAM,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IACzC,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC;IACzC,MAAM,MAAM,GAAG,kBAAkB,CAAC,OAAO,CAAC,UAAU,EAAE;QACpD,KAAK,EAAE,OAAO,CAAC,gBAAgB;QAC/B,GAAG,CAAC,OAAO,C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
|
+
* Where a module path points, and what it resolved to.
|
|
4
|
+
*
|
|
5
|
+
* Three roots, stated by the path itself so a reader can tell where a rule
|
|
6
|
+
* comes from without opening a manifest:
|
|
7
|
+
*
|
|
8
|
+
* source/rules/10_X.md the agent's own directory
|
|
9
|
+
* shared/rules/00_X.md the project's shared directory
|
|
10
|
+
* @common/rules/00_X.md a declared resource — a local referential or a
|
|
11
|
+
* Git repository pinned to a ref
|
|
12
|
+
*/
|
|
13
|
+
export interface ResolvedResource {
|
|
14
|
+
alias: string;
|
|
15
|
+
root: string;
|
|
16
|
+
/** Present for a Git resource: the commit actually read. */
|
|
17
|
+
git?: {
|
|
18
|
+
url: string;
|
|
19
|
+
ref: string;
|
|
20
|
+
commit: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** One declared resource, resolved to a directory. */
|
|
24
|
+
export declare function resolveResource(project: Project, alias: string): ResolvedResource;
|
|
25
|
+
/** Resolves module paths against the agent, the shared directory and the resources, caching each resource. */
|
|
26
|
+
export declare class ModuleReader {
|
|
27
|
+
private readonly project;
|
|
28
|
+
private readonly resolved;
|
|
29
|
+
constructor(project: Project);
|
|
30
|
+
/** The resources this reader touched, for the build manifest. */
|
|
31
|
+
used(): ResolvedResource[];
|
|
32
|
+
/** Absolute file a module path names, refused if it escapes its root. */
|
|
33
|
+
locate(agentDir: string, path: string): string;
|
|
34
|
+
/** A module's text, trimmed. */
|
|
35
|
+
read(agentDir: string, path: string): string;
|
|
36
|
+
/** A shared rule by file name, with the names known when it is missing. */
|
|
37
|
+
readSharedRule(name: string): string;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=resources.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.d.ts","sourceRoot":"","sources":["../src/resources.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAY,MAAM,cAAc,CAAC;AAEtD;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,4DAA4D;IAC5D,GAAG,CAAC,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;CACpD;AAsDD,sDAAsD;AACtD,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,gBAAgB,CAYjF;AAED,8GAA8G;AAC9G,qBAAa,YAAY;IAGX,OAAO,CAAC,QAAQ,CAAC,OAAO;IAFpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAuC;gBAEnC,OAAO,EAAE,OAAO;IAE7C,iEAAiE;IACjE,IAAI,IAAI,gBAAgB,EAAE;IAI1B,yEAAyE;IACzE,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;IAyB9C,gCAAgC;IAChC,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;IAM5C,2EAA2E;IAC3E,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;CASrC"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from 'node:fs';
|
|
4
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
5
|
+
import { RuleLoomError } from './errors.js';
|
|
6
|
+
const SHARED_PREFIX = 'shared/';
|
|
7
|
+
const RESOURCE_PREFIX = /^@([a-z][a-z0-9-]*)\/(.+)$/;
|
|
8
|
+
/** True when `child` is `parent` or inside it. */
|
|
9
|
+
function inside(parent, child) {
|
|
10
|
+
const rel = relative(parent, child);
|
|
11
|
+
return rel === '' || (!rel.startsWith(`..${sep}`) && rel !== '..' && !resolve(rel).startsWith(sep + '..'));
|
|
12
|
+
}
|
|
13
|
+
const git = (args, cwd) => execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
|
|
14
|
+
/**
|
|
15
|
+
* A Git resource, fetched once into the cache at the pinned ref.
|
|
16
|
+
*
|
|
17
|
+
* Fetched by ref rather than cloned by branch so a tag and a commit work the
|
|
18
|
+
* same way. The commit read is returned and recorded in every bundle
|
|
19
|
+
* manifest: a ref is a name, the commit is what was compiled.
|
|
20
|
+
*/
|
|
21
|
+
function fetchGit(project, alias, resource) {
|
|
22
|
+
const key = createHash('sha256').update(`${resource.git}#${resource.ref}`).digest('hex').slice(0, 16);
|
|
23
|
+
const dir = join(project.cacheRoot, `${alias}-${key}`);
|
|
24
|
+
if (!existsSync(join(dir, '.git'))) {
|
|
25
|
+
rmSync(dir, { recursive: true, force: true });
|
|
26
|
+
mkdirSync(dir, { recursive: true });
|
|
27
|
+
try {
|
|
28
|
+
git(['init', '--quiet'], dir);
|
|
29
|
+
git(['remote', 'add', 'origin', resource.git], dir);
|
|
30
|
+
git(['fetch', '--quiet', '--depth', '1', 'origin', resource.ref], dir);
|
|
31
|
+
git(['checkout', '--quiet', '--detach', 'FETCH_HEAD'], dir);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
rmSync(dir, { recursive: true, force: true });
|
|
35
|
+
throw new RuleLoomError('RESOURCE_GIT_FETCH_FAILED', `@${alias} — ${resource.git}#${resource.ref}: ${error.message.split('\n')[0]}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const commit = git(['rev-parse', 'HEAD'], dir);
|
|
39
|
+
if (resource.commit && !commit.startsWith(resource.commit)) {
|
|
40
|
+
throw new RuleLoomError('RESOURCE_COMMIT_MISMATCH', `@${alias} — ${resource.ref} resolves to ${commit}, the project pins ${resource.commit}`);
|
|
41
|
+
}
|
|
42
|
+
const root = resolve(dir, resource.path);
|
|
43
|
+
if (!inside(dir, root)) {
|
|
44
|
+
throw new RuleLoomError('RESOURCE_PATH_OUTSIDE', `@${alias} — path ${resource.path} leaves the repository`);
|
|
45
|
+
}
|
|
46
|
+
return { alias, root, git: { url: resource.git, ref: resource.ref, commit } };
|
|
47
|
+
}
|
|
48
|
+
/** One declared resource, resolved to a directory. */
|
|
49
|
+
export function resolveResource(project, alias) {
|
|
50
|
+
const resource = project.resources[alias];
|
|
51
|
+
if (!resource) {
|
|
52
|
+
throw new RuleLoomError('RESOURCE_UNKNOWN', `@${alias} — the project declares ${Object.keys(project.resources).join(', ') || '(no resource)'}`);
|
|
53
|
+
}
|
|
54
|
+
if ('git' in resource)
|
|
55
|
+
return fetchGit(project, alias, resource);
|
|
56
|
+
const root = resolve(project.root, resource.path);
|
|
57
|
+
if (!existsSync(root))
|
|
58
|
+
throw new RuleLoomError('RESOURCE_MISSING', `@${alias} — ${root} does not exist`);
|
|
59
|
+
return { alias, root };
|
|
60
|
+
}
|
|
61
|
+
/** Resolves module paths against the agent, the shared directory and the resources, caching each resource. */
|
|
62
|
+
export class ModuleReader {
|
|
63
|
+
project;
|
|
64
|
+
resolved = new Map();
|
|
65
|
+
constructor(project) {
|
|
66
|
+
this.project = project;
|
|
67
|
+
}
|
|
68
|
+
/** The resources this reader touched, for the build manifest. */
|
|
69
|
+
used() {
|
|
70
|
+
return [...this.resolved.values()].sort((a, b) => a.alias.localeCompare(b.alias));
|
|
71
|
+
}
|
|
72
|
+
/** Absolute file a module path names, refused if it escapes its root. */
|
|
73
|
+
locate(agentDir, path) {
|
|
74
|
+
let root;
|
|
75
|
+
let rest;
|
|
76
|
+
const resource = RESOURCE_PREFIX.exec(path);
|
|
77
|
+
if (resource) {
|
|
78
|
+
const alias = resource[1];
|
|
79
|
+
let entry = this.resolved.get(alias);
|
|
80
|
+
if (!entry) {
|
|
81
|
+
entry = resolveResource(this.project, alias);
|
|
82
|
+
this.resolved.set(alias, entry);
|
|
83
|
+
}
|
|
84
|
+
root = entry.root;
|
|
85
|
+
rest = resource[2];
|
|
86
|
+
}
|
|
87
|
+
else if (path.startsWith(SHARED_PREFIX)) {
|
|
88
|
+
root = this.project.sharedRoot;
|
|
89
|
+
rest = path.slice(SHARED_PREFIX.length);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
root = agentDir;
|
|
93
|
+
rest = path;
|
|
94
|
+
}
|
|
95
|
+
const file = resolve(root, rest);
|
|
96
|
+
if (!inside(root, file))
|
|
97
|
+
throw new RuleLoomError('MODULE_PATH_OUTSIDE', `${path} leaves its root ${root}`);
|
|
98
|
+
return file;
|
|
99
|
+
}
|
|
100
|
+
/** A module's text, trimmed. */
|
|
101
|
+
read(agentDir, path) {
|
|
102
|
+
const file = this.locate(agentDir, path);
|
|
103
|
+
if (!existsSync(file))
|
|
104
|
+
throw new RuleLoomError('AGENT_MODULE_MISSING', `${path} (agent ${agentDir})`);
|
|
105
|
+
return readFileSync(file, 'utf8').trim();
|
|
106
|
+
}
|
|
107
|
+
/** A shared rule by file name, with the names known when it is missing. */
|
|
108
|
+
readSharedRule(name) {
|
|
109
|
+
const dir = join(this.project.sharedRoot, 'rules');
|
|
110
|
+
const file = join(dir, name);
|
|
111
|
+
if (!inside(dir, resolve(file)) || !existsSync(file)) {
|
|
112
|
+
const known = existsSync(dir) ? readdirSync(dir).sort().join(', ') : '(none)';
|
|
113
|
+
throw new RuleLoomError('SHARED_RULE_MISSING', `${name} — known: ${known}`);
|
|
114
|
+
}
|
|
115
|
+
return readFileSync(file, 'utf8').trim();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=resources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.js","sourceRoot":"","sources":["../src/resources.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY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AC,CAAC,QAAQ,CAAC;YAC9E,MAAM,IAAI,aAAa,CAAC,qBAAqB,EAAE,GAAG,IAAI,aAAa,KAAK,EAAE,CAAC,CAAC;QAC9E,CAAC;QACD,OAAO,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;IAC3C,CAAC;CACF"}
|
package/dist/select.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AgentConfig, AgentModule } from './config.js';
|
|
2
|
+
/** The mode a compilation is for: the one asked, or the agent's first. */
|
|
3
|
+
export declare function resolveMode(config: AgentConfig, requested?: string): string | undefined;
|
|
4
|
+
/** Modules of a mode: those bound to no mode, and that mode's. */
|
|
5
|
+
export declare function modulesForMode(config: AgentConfig, mode?: string): AgentModule[];
|
|
6
|
+
export interface SelectOptions {
|
|
7
|
+
/** One of the agent's `modes`; absent is its default. */
|
|
8
|
+
mode?: string;
|
|
9
|
+
/** Retry: keep only the modules tagged with one of these codes, and every `always` module. */
|
|
10
|
+
failedCodes?: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
/** Modules kept for a mode and, on a retry, for the codes that failed. */
|
|
13
|
+
export declare function selectModules(config: AgentConfig, options?: SelectOptions): AgentModule[];
|
|
14
|
+
//# sourceMappingURL=select.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../src/select.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE5D,0EAA0E;AAC1E,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CASvF;AAED,kEAAkE;AAClE,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,WAAW,EAAE,CAIhF;AAED,MAAM,WAAW,aAAa;IAC5B,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8FAA8F;IAC9F,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACjC;AAED,0EAA0E;AAC1E,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,GAAE,aAAkB,GAAG,WAAW,EAAE,CAK7F"}
|
package/dist/select.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RuleLoomError } from './errors.js';
|
|
2
|
+
/** The mode a compilation is for: the one asked, or the agent's first. */
|
|
3
|
+
export function resolveMode(config, requested) {
|
|
4
|
+
if (requested === undefined)
|
|
5
|
+
return config.modes[0];
|
|
6
|
+
if (!config.modes.includes(requested)) {
|
|
7
|
+
throw new RuleLoomError('AGENT_MODE_UNKNOWN', `${config.name} does not declare mode ${requested} — declared: ${config.modes.join(', ') || '(none)'}`);
|
|
8
|
+
}
|
|
9
|
+
return requested;
|
|
10
|
+
}
|
|
11
|
+
/** Modules of a mode: those bound to no mode, and that mode's. */
|
|
12
|
+
export function modulesForMode(config, mode) {
|
|
13
|
+
const resolved = resolveMode(config, mode);
|
|
14
|
+
return config.modules.filter((module) => module.modes.length === 0
|
|
15
|
+
|| (resolved !== undefined && module.modes.includes(resolved)));
|
|
16
|
+
}
|
|
17
|
+
/** Modules kept for a mode and, on a retry, for the codes that failed. */
|
|
18
|
+
export function selectModules(config, options = {}) {
|
|
19
|
+
const eligible = modulesForMode(config, options.mode);
|
|
20
|
+
if (!options.failedCodes || options.failedCodes.length === 0)
|
|
21
|
+
return eligible;
|
|
22
|
+
const wanted = new Set(options.failedCodes);
|
|
23
|
+
return eligible.filter((module) => module.always || module.tags.some((tag) => wanted.has(tag)));
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=select.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select.js","sourceRoot":"","sources":["../src/select.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EA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,EAAE,CAAC,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AAClG,CAAC"}
|
package/dist/slots.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AgentConfig } from './config.js';
|
|
2
|
+
/** One `NAME=value` override, trimmed and validated. */
|
|
3
|
+
export declare function resolveDataOverride(name: string, value: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* The value each `${{NAME}}` an agent declares takes for one compilation.
|
|
6
|
+
*
|
|
7
|
+
* `data` is copied as written, then overridden by `given.data`. `random` takes
|
|
8
|
+
* the drawn value from `given.random`, or its first value when nothing was
|
|
9
|
+
* drawn — deterministic on purpose, so two compilations compare byte for byte.
|
|
10
|
+
* Both `given` maps are pools: a run carries every value it drew for every
|
|
11
|
+
* agent, so a name this agent does not declare is ignored. A drawn *value*
|
|
12
|
+
* outside the declared list is refused: a typo or a stale pin.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveAgentSlots(config: AgentConfig, given?: {
|
|
15
|
+
random?: Record<string, string>;
|
|
16
|
+
data?: Record<string, string>;
|
|
17
|
+
}): Record<string, string>;
|
|
18
|
+
export interface BindOptions {
|
|
19
|
+
/** Where the text comes from, for the error message. */
|
|
20
|
+
where?: string;
|
|
21
|
+
/** Collects the agent slots the text cited. */
|
|
22
|
+
used?: Set<string>;
|
|
23
|
+
/** Run block names left in place as `{{NAME}}`. */
|
|
24
|
+
blocks?: Iterable<string>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Binds the agent's slots in a text and refuses any slot nobody knows.
|
|
28
|
+
*
|
|
29
|
+
* One pass with a function replacer: a value is inserted once, never re-read
|
|
30
|
+
* as a template, and `$` sequences in it stay text.
|
|
31
|
+
*/
|
|
32
|
+
export declare function bindSlots(text: string, slots: Record<string, string>, options?: BindOptions): string;
|
|
33
|
+
/** The slots a text actually cited, with their values, sorted by name. */
|
|
34
|
+
export declare function usedSlots(slots: Record<string, string>, used: Set<string>): Record<string, string>;
|
|
35
|
+
//# sourceMappingURL=slots.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slots.d.ts","sourceRoot":"","sources":["../src/slots.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA0B/C,wDAAwD;AACxD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CASvE;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,WAAW,EACnB,KAAK,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAO,GAC7E,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAaxB;AAED,MAAM,WAAW,WAAW;IAC1B,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,+CAA+C;IAC/C,IAAI,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACnB,mDAAmD;IACnD,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;CAC3B;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,GAAE,WAAgB,GAAG,MAAM,CAuBxG;AAED,0EAA0E;AAC1E,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAElG"}
|
package/dist/slots.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { RuleLoomError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Two sigils, two owners.
|
|
4
|
+
*
|
|
5
|
+
* `${{NAME}}` belongs to the agent: a `random` or `data` slot from its own
|
|
6
|
+
* `agent.json`, bound in place — in the preamble, a rule, a skill — so it is
|
|
7
|
+
* part of the agent's text for that compilation.
|
|
8
|
+
*
|
|
9
|
+
* `{{NAME}}` belongs to the run: a block appended after the static text from
|
|
10
|
+
* the project's block templates (`<shared>/blocks/`). It is left exactly as it
|
|
11
|
+
* is.
|
|
12
|
+
*
|
|
13
|
+
* Anything else, either sigil, is a compilation error naming where. Without
|
|
14
|
+
* it a typo such as `{{THEM}}` reaches the model as a literal and nobody
|
|
15
|
+
* notices.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* A value an operator may pass for a `data` slot at compile time: a name, not
|
|
19
|
+
* a document. No `{` or `}`, so it cannot smuggle a slot into the text it is
|
|
20
|
+
* written into; short, because a model asked to copy a long free-form value
|
|
21
|
+
* rewrites it.
|
|
22
|
+
*/
|
|
23
|
+
const DATA_OVERRIDE = /^[\p{L}\p{N}](?:[\p{L}\p{N} _.-]{0,62}[\p{L}\p{N}])?$/u;
|
|
24
|
+
/** One `NAME=value` override, trimmed and validated. */
|
|
25
|
+
export function resolveDataOverride(name, value) {
|
|
26
|
+
const trimmed = value.trim();
|
|
27
|
+
if (!DATA_OVERRIDE.test(trimmed)) {
|
|
28
|
+
throw new RuleLoomError('DATA_OVERRIDE_INVALID', `${name} = "${value}" — 1 to 64 characters: letters, digits, dash, dot, underscore or inner space; never a brace`);
|
|
29
|
+
}
|
|
30
|
+
return trimmed;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The value each `${{NAME}}` an agent declares takes for one compilation.
|
|
34
|
+
*
|
|
35
|
+
* `data` is copied as written, then overridden by `given.data`. `random` takes
|
|
36
|
+
* the drawn value from `given.random`, or its first value when nothing was
|
|
37
|
+
* drawn — deterministic on purpose, so two compilations compare byte for byte.
|
|
38
|
+
* Both `given` maps are pools: a run carries every value it drew for every
|
|
39
|
+
* agent, so a name this agent does not declare is ignored. A drawn *value*
|
|
40
|
+
* outside the declared list is refused: a typo or a stale pin.
|
|
41
|
+
*/
|
|
42
|
+
export function resolveAgentSlots(config, given = {}) {
|
|
43
|
+
const slots = { ...config.data };
|
|
44
|
+
for (const [name, value] of Object.entries(given.data ?? {})) {
|
|
45
|
+
if (Object.hasOwn(slots, name))
|
|
46
|
+
slots[name] = resolveDataOverride(name, value);
|
|
47
|
+
}
|
|
48
|
+
for (const [name, values] of Object.entries(config.random)) {
|
|
49
|
+
const wanted = given.random?.[name];
|
|
50
|
+
if (wanted !== undefined && !values.includes(wanted)) {
|
|
51
|
+
throw new RuleLoomError('RANDOM_VALUE_UNKNOWN', `${name}=${wanted} — ${config.name} declares ${values.join(', ')}`);
|
|
52
|
+
}
|
|
53
|
+
slots[name] = wanted ?? values[0];
|
|
54
|
+
}
|
|
55
|
+
return slots;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Binds the agent's slots in a text and refuses any slot nobody knows.
|
|
59
|
+
*
|
|
60
|
+
* One pass with a function replacer: a value is inserted once, never re-read
|
|
61
|
+
* as a template, and `$` sequences in it stay text.
|
|
62
|
+
*/
|
|
63
|
+
export function bindSlots(text, slots, options = {}) {
|
|
64
|
+
const where = options.where ?? 'text';
|
|
65
|
+
const blocks = new Set(options.blocks ?? []);
|
|
66
|
+
return text.replace(/(\$?)\{\{([A-Z][A-Z0-9_]*)\}\}/g, (whole, sigil, name) => {
|
|
67
|
+
if (sigil === '$') {
|
|
68
|
+
if (Object.hasOwn(slots, name)) {
|
|
69
|
+
options.used?.add(name);
|
|
70
|
+
return slots[name];
|
|
71
|
+
}
|
|
72
|
+
const declared = Object.keys(slots);
|
|
73
|
+
throw new RuleLoomError('AGENT_SLOT_UNKNOWN', `${where} cites \${{${name}}}, which the agent declares neither in random nor in data `
|
|
74
|
+
+ `(${declared.length > 0 ? declared.join(', ') : 'no slot declared'})`);
|
|
75
|
+
}
|
|
76
|
+
if (blocks.has(name))
|
|
77
|
+
return whole;
|
|
78
|
+
const hint = Object.hasOwn(slots, name) ? ` — the agent declares this name: write \${{${name}}}` : '';
|
|
79
|
+
throw new RuleLoomError('PROMPT_SLOT_UNKNOWN', `${where} cites {{${name}}}, which is not a run block (${[...blocks].join(', ') || 'none declared'})${hint}`);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/** The slots a text actually cited, with their values, sorted by name. */
|
|
83
|
+
export function usedSlots(slots, used) {
|
|
84
|
+
return Object.fromEntries([...used].sort().map((name) => [name, slots[name]]));
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=slots.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slots.js","sourceRoot":"","sources":["../src/slots.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5C;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AACH,MAAM,aAAa,GAAG,wDAAwD,CAAC;AAE/E,wDAAwD;AACxD,MAAM,UAAU,mBAAmB,CAAC,IAAY,EAAE,KAAa;IAC7D,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,aAAa,CACrB,uBAAuB,EACvB,GAAG,IAAI,OAAO,KAAK,8FAA8F,CAClH,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAAmB,EACnB,QAA4E,EAAE;IAE9E,MAAM,KAAK,GAA2B,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;IACzD,KAAK,MAAM,CAAC,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|
|
4
|
+
|
|
5
|
+
Optional; at the project root. Every path is relative to the file, except `sharedDir` (relative to `agentsDir`) and `constraints` / `blocksDir` (relative to `sharedDir`).
|
|
6
|
+
|
|
7
|
+
| Field | Default | Meaning |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| `agentsDir` | `"."` | One sub-directory per agent, each with an `agent.json`. |
|
|
10
|
+
| `sharedDir` | `"shared"` | Shared rules (`rules/`), run blocks, constraints. Never an agent. |
|
|
11
|
+
| `exclude` | `[]` | Sub-directories of `agentsDir` that are never agents. |
|
|
12
|
+
| `namePrefix` | `""` | Prefix of every generated agent name: `acme-` makes `writer` into `acme-writer`. Lowercase, digits, `-`. |
|
|
13
|
+
| `constraints` | — | A paragraph appended to every agent file. Say out loud that rules are loaded for the agent, so it does not conclude they are optional. |
|
|
14
|
+
| `blocksDir` | `"blocks"` | Run block templates. |
|
|
15
|
+
| `blockNumberFloor` | `100` | Lowest number a run block may carry; hand-written modules stay below. |
|
|
16
|
+
| `targets` | `["claude"]` | Targets compiled when none is named. |
|
|
17
|
+
| `resources` | `{}` | Shared referentials by alias (see below). |
|
|
18
|
+
| `cacheDir` | `".ruleloom/cache"` | Where Git resources are fetched. |
|
|
19
|
+
|
|
20
|
+
### Resources
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
{
|
|
24
|
+
"resources": {
|
|
25
|
+
"common": { "path": "../company-rules" },
|
|
26
|
+
"security": { "git": "https://github.com/acme/agent-rules.git", "ref": "v2.3.0", "commit": "9f1c2ab", "path": "rules" }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
A module cites them as `@common/rules/00_TONE.md`. `ref` is a tag or a commit — a branch moves, and a moving source is not reproducible. `commit`, when given, makes compilation fail if the ref resolves elsewhere (`RESOURCE_COMMIT_MISMATCH`). The commit read is recorded in `build-manifest.json` either way.
|
|
32
|
+
|
|
33
|
+
## `agent.json` — one agent
|
|
34
|
+
|
|
35
|
+
| Field | Required | Meaning |
|
|
36
|
+
|---|---|---|
|
|
37
|
+
| `name` | yes | The agent's own name. |
|
|
38
|
+
| `version` | yes | Recorded in prompts and manifests. |
|
|
39
|
+
| `summary` | yes | One line: what it turns into what. Becomes the platform's `description`. |
|
|
40
|
+
| `inputs`, `outputs` | — | Artefact names, for the orchestrator. RuleLoom does not read them. |
|
|
41
|
+
| `preamble` | — | Lines opening every prompt and agent file. Slots allowed. |
|
|
42
|
+
| `sharedRules` | — | File names in `<shared>/rules/`, inherited first. |
|
|
43
|
+
| `data` | — | Constant slots: `{ "TONE": "plain" }`, cited as `${{TONE}}`. Free text, no braces, ≤ 2000 characters. |
|
|
44
|
+
| `random` | — | Drawn slots: `{ "SHAPE": ["list", "table"] }`, at least two bare names. The first is taken when nothing is drawn. |
|
|
45
|
+
| `modes` | — | Variants of the agent; the first is the default. |
|
|
46
|
+
| `runtime.skills` | — | Skill slugs to preload (Claude Code). A slug the agent does not have is refused. |
|
|
47
|
+
| `mcp` | — | MCP servers by name: `{ "command", "args", "env" }` (stdio) or `{ "url" }` (HTTP). `env` lists variable names only. |
|
|
48
|
+
| `targets` | — | Per-target settings written into the agent's frontmatter or config: `claude`, `codex`, `copilot`. See [targets](targets.md). |
|
|
49
|
+
| `modules` | yes | The files, in reading order. |
|
|
50
|
+
|
|
51
|
+
### Modules
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{ "path": "source/rules/10_SHAPE.md", "tags": ["SHAPE_WRONG"], "always": false, "trailing": false, "modes": [] }
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
| Field | Meaning |
|
|
58
|
+
|---|---|
|
|
59
|
+
| `path` | Relative to the agent; `shared/…` for the shared directory; `@alias/…` for a resource. |
|
|
60
|
+
| `kind` | `rule`, `skill` or `mission`. Inferred when absent: a path under `skills/` is a skill, anything else a rule. At most one mission. |
|
|
61
|
+
| `tags` | Verification codes the module helps fix. A retry (`failedCodes`) keeps only matching modules and the `always` ones. |
|
|
62
|
+
| `always` | Kept on every retry — the output contract, above all. |
|
|
63
|
+
| `trailing` | In a prompt, placed after the run blocks: the last thing the model reads. Ignored by bundles. |
|
|
64
|
+
| `modes` | Compiled only for these modes. Empty: every mode. |
|
|
65
|
+
|
|
66
|
+
A module's slug is its file name, lowercase, `_` → `-`: `20_CHANGELOG_STYLE.md` → `20-changelog-style`. A skill's description is its first heading without the `[nn]` prefix.
|
|
67
|
+
|
|
68
|
+
## Slots and run blocks
|
|
69
|
+
|
|
70
|
+
Two sigils, two owners:
|
|
71
|
+
|
|
72
|
+
- `${{NAME}}` — the agent's: bound at compile time from `data` or `random`.
|
|
73
|
+
- `{{NAME}}` — the run's: a block template `<shared>/blocks/<number>_<NAME>.md` whose first line is `# [<number>] …` and which cites `{{NAME}}` once. `compilePrompt` appends the blocks the caller supplies, in number order, after the static text. Bundles leave `{{NAME}}` as written.
|
|
74
|
+
|
|
75
|
+
Any other `{{…}}` or `${{…}}` is a compile error naming the file.
|
|
76
|
+
|
|
77
|
+
```md
|
|
78
|
+
<!-- shared/blocks/100_BRIEF.md -->
|
|
79
|
+
# [100] BRIEF
|
|
80
|
+
|
|
81
|
+
{{BRIEF}}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Determinism
|
|
85
|
+
|
|
86
|
+
Compilation reads `agent.json` order, never a directory listing, writes no timestamp, and takes the first value of every `random` slot unless the caller passes a drawn one. Two compilations of the same inputs produce the same bytes; `build-manifest.json` carries one checksum over every emitted file. Randomness belongs to whoever runs the agent: draw once, pass the values, record them.
|
package/docs/api.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# API
|
|
2
|
+
|
|
3
|
+
```ts
|
|
4
|
+
import {
|
|
5
|
+
loadProject, defineProject,
|
|
6
|
+
compilePrompt, compileBundle, compileAllBundles, writeBundles, materialiseAgent,
|
|
7
|
+
readAgentConfig, selectModules, resolveAgentSlots, bindSlots, loadBlockTemplates,
|
|
8
|
+
RuleLoomError,
|
|
9
|
+
} from 'ruleloom';
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Projects
|
|
13
|
+
|
|
14
|
+
- `loadProject(root)` — reads `root/ruleloom.json`, or the defaults when there is none.
|
|
15
|
+
- `defineProject(root, options)` — the same from an object, for a caller that keeps its conventions in code.
|
|
16
|
+
|
|
17
|
+
## Prompts
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const prompt = compilePrompt({
|
|
21
|
+
project, agent: 'writer',
|
|
22
|
+
values: { BRIEF: '…', FEEDBACK: '…' }, // run blocks
|
|
23
|
+
random: { SHAPE: 'table' }, // drawn slots; first value otherwise
|
|
24
|
+
data: { TONE: 'dry' }, // data overrides
|
|
25
|
+
failedCodes: ['SHAPE_WRONG'], // retry: tagged + always modules only
|
|
26
|
+
mode: 'digest',
|
|
27
|
+
expectedBlocks: ['BRIEF', 'FEEDBACK'], // refuse a missing or extra block template
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Returns `static`, `dynamic` (run blocks then trailing modules), `runBlocks`, `trailing`, `text`, `staticSha256`, `modules` (path, sha256, bytes), `unresolvedBlocks`, `cited`, `slots`, `selection` (`full` | `retry`).
|
|
32
|
+
|
|
33
|
+
## Bundles
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const bundle = compileBundle({ project, agent: 'writer', target: 'codex', scope: 'isolated' });
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Returns `{ agent, agentName, target, files, manifest }`. `files` is a `Map<path, content>` including `build-manifest.json`:
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"agent": "writer", "version": "1.0.0", "target": "codex", "scope": "isolated",
|
|
44
|
+
"sources": [{ "path": "source/rules/10_SHAPE.md", "sha256": "…", "bytes": 61 }],
|
|
45
|
+
"resources": [{ "alias": "common", "url": "…", "ref": "v1", "commit": "…" }],
|
|
46
|
+
"files": [{ "path": "AGENTS.md", "sha256": "…", "bytes": 812 }],
|
|
47
|
+
"checksum": "…",
|
|
48
|
+
"cited": { "SHAPE": "list" },
|
|
49
|
+
"slots": { "SHAPE": "list", "TONE": "plain" },
|
|
50
|
+
"notes": ["…"]
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- `compileAllBundles(project, { target })` — every agent.
|
|
55
|
+
- `writeBundles({ project, target, outRoot, agent?, check? })` — writes `outRoot/<agent>/` and returns `{ written, drifted, orphaned, bundles }`.
|
|
56
|
+
- `materialiseAgent({ project, agent, target, runtimeRoot })` — a dedicated workspace; `RUNTIME_ROOT_NOT_ISOLATED` when configuration above it would leak in.
|
|
57
|
+
- `inheritedConfig(dir, target)` — what an agent launched in `dir` would inherit from its parents.
|
|
58
|
+
|
|
59
|
+
## Lower level
|
|
60
|
+
|
|
61
|
+
`assembleAgent` (the platform-neutral agent every adapter receives), `ADAPTERS` / `claudeAdapter` / `codexAdapter` / `copilotAdapter`, `selectModules`, `resolveAgentSlots`, `bindSlots`, `loadBlockTemplates`, `renderBlock`, `moduleSlug`, `moduleTitle`, `yamlScalar`.
|
|
62
|
+
|
|
63
|
+
## Errors
|
|
64
|
+
|
|
65
|
+
Every failure is a `RuleLoomError` with a stable `code` and a message starting with it. Match on `error.code`, never on the sentence. See [errors](errors.md).
|