@marcoscale98/pi-extensible-workflows 5.14.1-fork.1 → 5.17.0-fork.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -0
- package/dist/src/agent-execution.js +36 -51
- package/dist/src/background-widget.d.ts +56 -0
- package/dist/src/background-widget.js +22 -15
- package/dist/src/decoders.js +2 -1
- package/dist/src/host-navigator.d.ts +18 -0
- package/dist/src/host-navigator.js +71 -13
- package/dist/src/host-view.d.ts +16 -1
- package/dist/src/host-view.js +109 -42
- package/dist/src/host.js +57 -24
- package/dist/src/index.d.ts +3 -1
- package/dist/src/index.js +867 -545
- package/dist/src/index.js.map +3 -3
- package/dist/src/registry.js +2 -1
- package/dist/src/roles.d.ts +70 -0
- package/dist/src/roles.js +325 -0
- package/dist/src/settings.d.ts +13 -0
- package/dist/src/settings.js +253 -0
- package/dist/src/trajectory.d.ts +29 -4
- package/dist/src/trajectory.js +58 -3
- package/dist/src/types.d.ts +0 -1
- package/dist/src/utils.d.ts +2 -1
- package/dist/src/utils.js +10 -0
- package/dist/src/validation.d.ts +5 -17
- package/dist/src/validation.js +9 -458
- package/dist/subagents/index.js +1132 -694
- package/dist/subagents/index.js.map +3 -3
- package/dist/subagents/src/contracts.d.ts +4 -0
- package/dist/subagents/src/index.d.ts +1 -1
- package/dist/subagents/src/index.js +2 -1
- package/dist/subagents/src/manager.js +29 -10
- package/dist/subagents/src/navigator.js +396 -247
- package/dist/subagents/src/view.d.ts +23 -2
- package/dist/subagents/src/view.js +96 -30
- package/dist/trajectory/assets/index.html +347 -231
- package/dist/trajectory/assets/prism.min.js +13 -0
- package/dist/trajectory/index.js +377 -257
- package/dist/trajectory/index.js.map +3 -3
- package/dist/trajectory/src/assets/index.html +347 -231
- package/dist/trajectory/src/assets/prism.min.js +13 -0
- package/dist/trajectory/src/export.js +4 -2
- package/dist/trajectory/src/index.d.ts +2 -0
- package/dist/trajectory/src/index.js +143 -42
- package/dist/trajectory/src/server.js +134 -9
- package/dist/trajectory/src/server.js.map +1 -1
- package/package.json +2 -1
- package/src/agent-execution.ts +36 -42
- package/src/background-widget.ts +25 -18
- package/src/decoders.ts +2 -1
- package/src/host-navigator.ts +58 -14
- package/src/host-view.ts +93 -37
- package/src/host.ts +51 -21
- package/src/index.ts +3 -1
- package/src/registry.ts +2 -1
- package/src/roles.ts +326 -0
- package/src/settings.ts +206 -0
- package/src/trajectory.ts +76 -7
- package/src/types.ts +1 -1
- package/src/utils.ts +8 -1
- package/src/validation.ts +12 -356
- package/subagents/README.md +1 -1
- package/subagents/src/contracts.ts +4 -0
- package/subagents/src/index.ts +3 -2
- package/subagents/src/manager.ts +28 -10
- package/subagents/src/navigator.ts +336 -180
- package/subagents/src/view.ts +97 -28
- package/trajectory/src/assets/index.html +347 -231
- package/trajectory/src/assets/prism.min.js +13 -0
- package/trajectory/src/export.ts +4 -2
- package/trajectory/src/index.ts +118 -38
- package/trajectory/src/server.ts +112 -12
package/dist/src/validation.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
6
3
|
import * as acorn from "acorn";
|
|
7
4
|
import { Script } from "node:vm";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export
|
|
5
|
+
import { assertModelThinking, validateSchema, deepFreeze, errorText, fail, jsonObject, jsonValue, mergeWorkflowExtensionSettings, modelAliasName, modelCapability, object, positiveInteger, resolveModelReference, resourcePatternHasMagic, unknownModel } from "./utils.js";
|
|
6
|
+
import { WORKFLOW_CALL_KINDS } from "./types.js";
|
|
7
|
+
export { validateSchema } from "./utils.js";
|
|
8
|
+
import { loadAgentDefinitions, loadProjectAgentDefinitions } from "./roles.js";
|
|
9
|
+
export { loadAgentDefinitions, loadProjectAgentDefinitions, parseRoleMarkdown, workflowRoleDirectories } from "./roles.js";
|
|
10
|
+
import { validateContextFileScopes, validateSelectorList } from "./settings.js";
|
|
11
|
+
export { DEFAULT_SETTINGS, loadSettings, loadSettingsOverrides, resolveAgentResourcePolicy, resolveWorkflowSettings, saveModelAliases, validateContextFileScopes, validateModelAliasAvailability, validateSelectorList, validateWorkflowExtensionSettings, workflowProjectSettingsPath, workflowSettingsPath } from "./settings.js";
|
|
15
12
|
export function validateCheckpoint(value) {
|
|
16
13
|
if (!object(value) || Object.keys(value).some((key) => !["name", "prompt", "context"].includes(key)) || typeof value.name !== "string" || value.name.trim() === "" || typeof value.prompt !== "string" || !jsonValue(value.context))
|
|
17
14
|
fail("INVALID_METADATA", "checkpoint requires only name, prompt, and JSON context");
|
|
@@ -21,442 +18,6 @@ export function validateCheckpoint(value) {
|
|
|
21
18
|
fail("INVALID_METADATA", "checkpoint context exceeds 4096 UTF-8 bytes");
|
|
22
19
|
return { name: value.name, prompt: value.prompt, context: value.context };
|
|
23
20
|
}
|
|
24
|
-
export function workflowSettingsPath(agentDir = getAgentDir()) { return join(agentDir, ROLE_DIRECTORY, "settings.json"); }
|
|
25
|
-
function normalizedResourcePath(value, settingsPath) {
|
|
26
|
-
if (value === "*")
|
|
27
|
-
return value;
|
|
28
|
-
let expanded = value === "~" ? homedir() : value.startsWith("~/") || value.startsWith("~\\") ? join(homedir(), value.slice(2)) : value;
|
|
29
|
-
if (expanded.startsWith("file://"))
|
|
30
|
-
expanded = fileURLToPath(expanded);
|
|
31
|
-
const resolved = resolve(dirname(settingsPath), expanded);
|
|
32
|
-
if (expanded === "**" || expanded.startsWith("**/") || expanded.startsWith("**\\"))
|
|
33
|
-
return expanded;
|
|
34
|
-
if (resourcePatternHasMagic(expanded)) {
|
|
35
|
-
const magicIndex = resolved.search(/[*?\x5b\x5d{}()]/);
|
|
36
|
-
const separatorIndex = Math.max(resolved.lastIndexOf("/", magicIndex), resolved.lastIndexOf("\\", magicIndex));
|
|
37
|
-
const rootBoundary = separatorIndex === 0 || (separatorIndex === 2 && /^[A-Za-z]:[\\/]/.test(resolved));
|
|
38
|
-
const prefix = rootBoundary ? resolved.slice(0, separatorIndex + 1) : separatorIndex >= 0 ? resolved.slice(0, separatorIndex) : resolved;
|
|
39
|
-
const suffix = rootBoundary ? resolved.slice(separatorIndex + 1) : separatorIndex >= 0 ? resolved.slice(separatorIndex) : "";
|
|
40
|
-
return `${canonicalPath(prefix)}${suffix}`;
|
|
41
|
-
}
|
|
42
|
-
return canonicalPath(resolved);
|
|
43
|
-
}
|
|
44
|
-
function validateSelectorList(value, path, kind, errorCode = "INVALID_SETTINGS", normalizeExtensions = true) {
|
|
45
|
-
if (value === undefined)
|
|
46
|
-
return undefined;
|
|
47
|
-
if (!Array.isArray(value))
|
|
48
|
-
fail(errorCode, `${path}.${kind} must be an array`);
|
|
49
|
-
const normalized = [];
|
|
50
|
-
for (const [index, entry] of value.entries()) {
|
|
51
|
-
if (typeof entry !== "string" || !entry.trim())
|
|
52
|
-
fail(errorCode, `${path}.${kind}[${String(index)}] must be a non-empty string`);
|
|
53
|
-
let selector = entry.trim();
|
|
54
|
-
if (kind === "extensions" && normalizeExtensions) {
|
|
55
|
-
const negated = selector.startsWith("!");
|
|
56
|
-
const body = negated ? selector.slice(1) : selector;
|
|
57
|
-
if (!body)
|
|
58
|
-
fail(errorCode, `${path}.${kind}[${String(index)}] must be a valid minimatch pattern: Empty minimatch pattern ${JSON.stringify(selector)}`);
|
|
59
|
-
try {
|
|
60
|
-
selector = `${negated ? "!" : ""}${normalizedResourcePath(body, path)}`;
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
fail(errorCode, `${path}.${kind}[${String(index)}] must be a valid path: ${errorText(error)}`);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
try {
|
|
67
|
-
validateResourcePattern(selector);
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
fail(errorCode, `${path}.${kind}[${String(index)}] must be a valid minimatch pattern: ${errorText(error)}`);
|
|
71
|
-
}
|
|
72
|
-
normalized.push(selector);
|
|
73
|
-
}
|
|
74
|
-
return Object.freeze(normalized);
|
|
75
|
-
}
|
|
76
|
-
function validateWorktreePostCreateCommand(value, settingsPath) {
|
|
77
|
-
if (value === undefined)
|
|
78
|
-
return undefined;
|
|
79
|
-
if (!Array.isArray(value) || value.length === 0)
|
|
80
|
-
fail("INVALID_SETTINGS", `${settingsPath}.worktreePostCreateCommand must be a non-empty array`);
|
|
81
|
-
return Object.freeze(value.map((entry, index) => {
|
|
82
|
-
if (typeof entry !== "string" || !entry.trim())
|
|
83
|
-
fail("INVALID_SETTINGS", `${settingsPath}.worktreePostCreateCommand[${String(index)}] must be a non-empty string`);
|
|
84
|
-
return entry;
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
87
|
-
function selectorsFromSettings(settings) {
|
|
88
|
-
return {
|
|
89
|
-
...(settings.skills === undefined ? {} : { skills: settings.skills }),
|
|
90
|
-
...(settings.extensions === undefined ? {} : { extensions: settings.extensions }),
|
|
91
|
-
...(settings.tools === undefined ? {} : { tools: settings.tools }),
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
function selectorSet(value) { return { skills: [...(value?.skills ?? [])], extensions: [...(value?.extensions ?? [])], ...(value?.tools === undefined ? {} : { tools: [...value.tools] }) }; }
|
|
95
|
-
function validateContextFileScopes(value, rolePath) {
|
|
96
|
-
if (value === undefined)
|
|
97
|
-
return undefined;
|
|
98
|
-
if (!Array.isArray(value) || !value.every(isContextFileScope))
|
|
99
|
-
fail("INVALID_METADATA", `${rolePath}.contextFiles must be an array containing only global, project, or cwd`);
|
|
100
|
-
return [...value];
|
|
101
|
-
}
|
|
102
|
-
export function validateWorkflowExtensionSettings(value, settingsPath, errorCode = "INVALID_SETTINGS", applyDefaults = true) {
|
|
103
|
-
if (value === undefined)
|
|
104
|
-
return undefined;
|
|
105
|
-
const base = `${settingsPath}.extensionSettings`;
|
|
106
|
-
if (!object(value))
|
|
107
|
-
fail(errorCode, `${base} must be an object`);
|
|
108
|
-
const normalized = {};
|
|
109
|
-
for (const [namespace, raw] of Object.entries(value)) {
|
|
110
|
-
if (!validWorkflowExtensionNamespace(namespace))
|
|
111
|
-
fail(errorCode, `${base} contains an invalid namespace: ${namespace}`);
|
|
112
|
-
if (namespace === "herdr") {
|
|
113
|
-
if (!object(raw))
|
|
114
|
-
fail(errorCode, `${base}.herdr must be an object`);
|
|
115
|
-
if (Object.keys(raw).some((key) => key !== "enableFullyInspectableMode"))
|
|
116
|
-
fail(errorCode, `${base}.herdr contains an unsupported setting`);
|
|
117
|
-
if (raw.enableFullyInspectableMode !== undefined && typeof raw.enableFullyInspectableMode !== "boolean")
|
|
118
|
-
fail(errorCode, `${base}.herdr.enableFullyInspectableMode must be a boolean`);
|
|
119
|
-
normalized.herdr = Object.freeze({ ...(raw.enableFullyInspectableMode === undefined ? {} : { enableFullyInspectableMode: raw.enableFullyInspectableMode }) });
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
if (namespace === "trajectory") {
|
|
123
|
-
if (!object(raw))
|
|
124
|
-
fail(errorCode, `${base}.trajectory must be an object`);
|
|
125
|
-
if (Object.keys(raw).some((key) => key !== "port" && key !== "themes"))
|
|
126
|
-
fail(errorCode, `${base}.trajectory contains an unsupported setting`);
|
|
127
|
-
if (raw.port !== undefined && (!positiveInteger(raw.port) || raw.port > 65535))
|
|
128
|
-
fail(errorCode, `${base}.trajectory.port must be an integer from 1 to 65535`);
|
|
129
|
-
if (raw.themes !== undefined && typeof raw.themes !== "boolean")
|
|
130
|
-
fail(errorCode, `${base}.trajectory.themes must be a boolean`);
|
|
131
|
-
normalized.trajectory = Object.freeze({ ...(raw.port === undefined ? {} : { port: raw.port }), ...(raw.themes === undefined ? (applyDefaults ? { themes: false } : {}) : { themes: raw.themes }) });
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
if (!jsonValue(raw))
|
|
135
|
-
fail(errorCode, `${base}.${namespace} must be JSON-compatible`);
|
|
136
|
-
normalized[namespace] = structuredClone(raw);
|
|
137
|
-
}
|
|
138
|
-
return deepFreeze(normalized);
|
|
139
|
-
}
|
|
140
|
-
function positiveRetentionInteger(value) { return positiveInteger(value) && Number.isSafeInteger(value); }
|
|
141
|
-
function validateRetention(value, settingsPath) {
|
|
142
|
-
if (value === undefined)
|
|
143
|
-
return undefined;
|
|
144
|
-
const base = `${settingsPath}.retention`;
|
|
145
|
-
if (!object(value))
|
|
146
|
-
fail("INVALID_SETTINGS", `${base} must be an object`);
|
|
147
|
-
const unknown = Object.keys(value).find((key) => key !== "olderThanDays" && key !== "maxTerminalRuns");
|
|
148
|
-
if (unknown)
|
|
149
|
-
fail("INVALID_SETTINGS", `Unknown retention setting at ${base}: ${unknown}`);
|
|
150
|
-
if (value.olderThanDays !== undefined && !positiveRetentionInteger(value.olderThanDays))
|
|
151
|
-
fail("INVALID_SETTINGS", `${base}.olderThanDays must be a positive integer`);
|
|
152
|
-
if (value.maxTerminalRuns !== undefined && !positiveRetentionInteger(value.maxTerminalRuns))
|
|
153
|
-
fail("INVALID_SETTINGS", `${base}.maxTerminalRuns must be a positive integer`);
|
|
154
|
-
return Object.freeze({ ...(value.olderThanDays === undefined ? {} : { olderThanDays: value.olderThanDays }), ...(value.maxTerminalRuns === undefined ? {} : { maxTerminalRuns: value.maxTerminalRuns }) });
|
|
155
|
-
}
|
|
156
|
-
function parseSettings(path, partial) {
|
|
157
|
-
let parsed;
|
|
158
|
-
try {
|
|
159
|
-
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
160
|
-
}
|
|
161
|
-
catch (error) {
|
|
162
|
-
if (isNodeError(error, "ENOENT"))
|
|
163
|
-
return partial ? Object.freeze({}) : DEFAULT_SETTINGS;
|
|
164
|
-
fail("CONFIG_ERROR", `Invalid workflow settings JSON at ${path}: ${errorText(error)}`);
|
|
165
|
-
}
|
|
166
|
-
if (!object(parsed))
|
|
167
|
-
fail("INVALID_SETTINGS", `Workflow settings at ${path} must be an object`);
|
|
168
|
-
const allowed = new Set(["concurrency", "modelAliases", "skills", "extensions", "extensionSettings", "tools", "retention", "worktreePostCreateCommand", ...(partial ? [] : ["backgroundWidget"])]);
|
|
169
|
-
const unknown = Object.keys(parsed).find((key) => !allowed.has(key));
|
|
170
|
-
if (Object.prototype.hasOwnProperty.call(parsed, "disabledAgentResources"))
|
|
171
|
-
fail("INVALID_SETTINGS", `disabledAgentResources is no longer supported; use skills, extensions, and tools selectors (settings: ${path})`);
|
|
172
|
-
if (unknown)
|
|
173
|
-
fail("INVALID_SETTINGS", `Unknown workflow setting at ${path}: ${unknown}`);
|
|
174
|
-
const concurrency = parsed.concurrency === undefined ? (partial ? undefined : DEFAULT_SETTINGS.concurrency) : parsed.concurrency;
|
|
175
|
-
if (concurrency !== undefined && (!positiveInteger(concurrency) || concurrency > 16))
|
|
176
|
-
fail("INVALID_SETTINGS", `${path}.concurrency must be an integer from 1 to 16`);
|
|
177
|
-
const backgroundWidget = parsed.backgroundWidget === undefined ? (partial ? undefined : DEFAULT_SETTINGS.backgroundWidget) : parsed.backgroundWidget;
|
|
178
|
-
if (backgroundWidget !== undefined && typeof backgroundWidget !== "boolean")
|
|
179
|
-
fail("INVALID_SETTINGS", `${path}.backgroundWidget must be a boolean`);
|
|
180
|
-
const modelAliases = parsed.modelAliases === undefined ? undefined : validateModelAliases(parsed.modelAliases, path);
|
|
181
|
-
const skills = validateSelectorList(parsed.skills, path, "skills");
|
|
182
|
-
const tools = validateSelectorList(parsed.tools, path, "tools");
|
|
183
|
-
const extensions = validateSelectorList(parsed.extensions, path, "extensions");
|
|
184
|
-
const extensionSettings = parsed.extensionSettings === undefined ? undefined : validateWorkflowExtensionSettings(parsed.extensionSettings, path, "INVALID_SETTINGS", !partial);
|
|
185
|
-
const retention = validateRetention(parsed.retention, path);
|
|
186
|
-
const worktreePostCreateCommand = validateWorktreePostCreateCommand(parsed.worktreePostCreateCommand, path);
|
|
187
|
-
return Object.freeze({
|
|
188
|
-
...(concurrency === undefined ? {} : { concurrency }), ...(backgroundWidget === undefined ? {} : { backgroundWidget }), ...(modelAliases === undefined ? {} : { modelAliases }),
|
|
189
|
-
...(skills === undefined ? {} : { skills }), ...(extensions === undefined ? {} : { extensions }),
|
|
190
|
-
...(extensionSettings === undefined ? {} : { extensionSettings }), ...(tools === undefined ? {} : { tools }), ...(retention === undefined ? {} : { retention }),
|
|
191
|
-
...(worktreePostCreateCommand === undefined ? {} : { worktreePostCreateCommand }),
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
export function loadSettings(path = workflowSettingsPath()) { return parseSettings(path, false); }
|
|
195
|
-
export function loadSettingsOverrides(path) { return parseSettings(path, true); }
|
|
196
|
-
export function resolveWorkflowSettings(cwd, projectTrusted, globalSettingsPath = workflowSettingsPath()) {
|
|
197
|
-
const projectSettingsPath = workflowProjectSettingsPath(cwd);
|
|
198
|
-
const global = loadSettings(globalSettingsPath);
|
|
199
|
-
const project = projectTrusted ? loadSettingsOverrides(projectSettingsPath) : Object.freeze({});
|
|
200
|
-
const projectHas = (key) => Object.prototype.hasOwnProperty.call(project, key);
|
|
201
|
-
const sourceFor = (key) => projectHas(key) ? projectSettingsPath : globalSettingsPath;
|
|
202
|
-
const globalSelectors = selectorsFromSettings(global);
|
|
203
|
-
const projectSelectors = selectorsFromSettings(project);
|
|
204
|
-
const effectiveSelectors = selectorSet({
|
|
205
|
-
skills: [...(globalSelectors.skills ?? []), ...(projectSelectors.skills ?? [])],
|
|
206
|
-
extensions: [...(globalSelectors.extensions ?? []), ...(projectSelectors.extensions ?? [])],
|
|
207
|
-
...(globalSelectors.tools === undefined && projectSelectors.tools === undefined ? {} : { tools: [...(globalSelectors.tools ?? []), ...(projectSelectors.tools ?? [])] }),
|
|
208
|
-
});
|
|
209
|
-
const hasExtensionSelectors = global.extensions !== undefined || project.extensions !== undefined;
|
|
210
|
-
const extensionSettings = projectHas("extensionSettings") ? project.extensionSettings : global.extensionSettings;
|
|
211
|
-
const sources = {
|
|
212
|
-
concurrency: projectHas("concurrency") ? projectSettingsPath : globalSettingsPath,
|
|
213
|
-
modelAliases: projectHas("modelAliases") ? projectSettingsPath : globalSettingsPath,
|
|
214
|
-
skills: sourceFor("skills"), extensions: sourceFor("extensions"), tools: sourceFor("tools"),
|
|
215
|
-
...(extensionSettings === undefined ? {} : { extensionSettings: projectHas("extensionSettings") ? projectSettingsPath : globalSettingsPath }),
|
|
216
|
-
...(project.retention === undefined && global.retention === undefined ? {} : { retention: project.retention === undefined ? globalSettingsPath : projectSettingsPath }),
|
|
217
|
-
...(projectHas("worktreePostCreateCommand") || global.worktreePostCreateCommand !== undefined ? { worktreePostCreateCommand: projectHas("worktreePostCreateCommand") ? projectSettingsPath : globalSettingsPath } : {}),
|
|
218
|
-
};
|
|
219
|
-
const effective = Object.freeze({
|
|
220
|
-
concurrency: project.concurrency ?? global.concurrency,
|
|
221
|
-
backgroundWidget: global.backgroundWidget ?? true,
|
|
222
|
-
...(projectHas("modelAliases") ? { modelAliases: project.modelAliases } : global.modelAliases === undefined ? {} : { modelAliases: global.modelAliases }),
|
|
223
|
-
...(effectiveSelectors.skills.length ? { skills: effectiveSelectors.skills } : global.skills !== undefined || project.skills !== undefined ? { skills: effectiveSelectors.skills } : {}),
|
|
224
|
-
...(hasExtensionSelectors ? { extensions: effectiveSelectors.extensions } : {}),
|
|
225
|
-
...(extensionSettings === undefined ? {} : { extensionSettings }),
|
|
226
|
-
...(effectiveSelectors.tools?.length ? { tools: effectiveSelectors.tools } : global.tools !== undefined || project.tools !== undefined ? { tools: effectiveSelectors.tools } : {}),
|
|
227
|
-
...((project.retention ?? global.retention) === undefined ? {} : { retention: project.retention ?? global.retention }),
|
|
228
|
-
...(projectHas("worktreePostCreateCommand") ? { worktreePostCreateCommand: project.worktreePostCreateCommand } : global.worktreePostCreateCommand === undefined ? {} : { worktreePostCreateCommand: global.worktreePostCreateCommand }),
|
|
229
|
-
});
|
|
230
|
-
return { globalSettingsPath, projectSettingsPath, projectTrusted, global, project, effective, sources };
|
|
231
|
-
}
|
|
232
|
-
export function validateModelAliasAvailability(aliases, names, availableModels, knownModels, settingsPath) {
|
|
233
|
-
for (const name of names) {
|
|
234
|
-
try {
|
|
235
|
-
const target = modelCapability(name, aliases, knownModels, settingsPath);
|
|
236
|
-
if (!availableModels.has(target))
|
|
237
|
-
unknownModel(name, target, settingsPath);
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
throw annotateModelAliasError(error, name);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
export function resolveAgentResourcePolicy(cwd, projectTrusted, globalSettingsPath = workflowSettingsPath()) {
|
|
245
|
-
const resolved = resolveWorkflowSettings(cwd, projectTrusted, globalSettingsPath);
|
|
246
|
-
const global = selectorSet(selectorsFromSettings(resolved.global));
|
|
247
|
-
const project = selectorSet(selectorsFromSettings(resolved.project));
|
|
248
|
-
const effective = selectorSet(selectorsFromSettings(resolved.effective));
|
|
249
|
-
return { globalSettingsPath: resolved.globalSettingsPath, projectSettingsPath: resolved.projectSettingsPath, projectTrusted, global, project, effective, unmatchedSkills: [], unmatchedExtensions: [], unmatchedTools: [], selectorSources: { global: selectorsFromSettings(resolved.global), project: selectorsFromSettings(resolved.project) } };
|
|
250
|
-
}
|
|
251
|
-
export function saveModelAliases(path = workflowSettingsPath(), aliases = {}) {
|
|
252
|
-
const normalized = validateModelAliases(aliases, path);
|
|
253
|
-
let parsed = {};
|
|
254
|
-
try {
|
|
255
|
-
loadSettings(path);
|
|
256
|
-
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
257
|
-
}
|
|
258
|
-
catch (error) {
|
|
259
|
-
if (!isNodeError(error, "ENOENT"))
|
|
260
|
-
throw error;
|
|
261
|
-
}
|
|
262
|
-
if (!object(parsed))
|
|
263
|
-
fail("INVALID_SETTINGS", `Workflow settings at ${path} must be an object`);
|
|
264
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
265
|
-
atomicWriteFile(path, `${JSON.stringify({ ...parsed, modelAliases: normalized }, null, 2)}\n`, true);
|
|
266
|
-
}
|
|
267
|
-
export function parseRoleMarkdown(content, strict = false, rolePath) {
|
|
268
|
-
if (!strict) {
|
|
269
|
-
if (!content.startsWith("---\n"))
|
|
270
|
-
return { prompt: content };
|
|
271
|
-
const end = content.indexOf("\n---", 4);
|
|
272
|
-
if (end < 0)
|
|
273
|
-
return { prompt: content };
|
|
274
|
-
const meta = {};
|
|
275
|
-
for (const line of content.slice(4, end).split("\n")) {
|
|
276
|
-
const match = /^(model|tools|skills|extensions|description|overrideSystemPrompt|override_system_prompt|is_system_prompt|contextFiles|extensionSettings|disabledAgentResources|thinking)\s*:\s*(.+)$/.exec(line.trim());
|
|
277
|
-
if (match?.[1] === "disabledAgentResources")
|
|
278
|
-
fail("INVALID_METADATA", "disabledAgentResources is no longer supported; use skills, extensions, and tools selectors");
|
|
279
|
-
if (match?.[1] === "thinking")
|
|
280
|
-
fail("INVALID_METADATA", "Role thinking is not supported; put it on model as provider/model:thinking");
|
|
281
|
-
if (match?.[1] && match[2])
|
|
282
|
-
meta[match[1]] = match[2].trim();
|
|
283
|
-
}
|
|
284
|
-
const unquote = (v) => v.replace(/^['"]|['"]$/g, "");
|
|
285
|
-
const parseList = (value) => value === undefined ? undefined : value.replace(/^\[|\]$/g, "").split(",").map((entry) => unquote(entry.trim())).filter(Boolean);
|
|
286
|
-
const tools = parseList(meta.tools);
|
|
287
|
-
const skills = parseList(meta.skills);
|
|
288
|
-
const extensions = parseList(meta.extensions);
|
|
289
|
-
let extensionSettings;
|
|
290
|
-
if (meta.extensionSettings !== undefined) {
|
|
291
|
-
try {
|
|
292
|
-
extensionSettings = JSON.parse(meta.extensionSettings);
|
|
293
|
-
}
|
|
294
|
-
catch (error) {
|
|
295
|
-
fail("INVALID_METADATA", `Invalid role extensionSettings: ${errorText(error)}`);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
const rolePathValue = rolePath ?? "<role>";
|
|
299
|
-
const normalizedExtensionSettings = validateWorkflowExtensionSettings(extensionSettings, rolePathValue, "INVALID_METADATA", false);
|
|
300
|
-
const definition = { prompt: content.slice(end + 4).replace(/^\n/, "") };
|
|
301
|
-
if (meta.model) {
|
|
302
|
-
const model = unquote(meta.model);
|
|
303
|
-
assertModelThinking(model, "Role model");
|
|
304
|
-
definition.model = model;
|
|
305
|
-
}
|
|
306
|
-
if (meta.description)
|
|
307
|
-
definition.description = unquote(meta.description);
|
|
308
|
-
if (tools)
|
|
309
|
-
definition.tools = tools;
|
|
310
|
-
if (skills)
|
|
311
|
-
definition.skills = skills;
|
|
312
|
-
if (extensions)
|
|
313
|
-
definition.extensions = extensions;
|
|
314
|
-
if (normalizedExtensionSettings !== undefined)
|
|
315
|
-
definition.extensionSettings = normalizedExtensionSettings;
|
|
316
|
-
const overrideSystemPrompt = meta.overrideSystemPrompt ?? meta.override_system_prompt ?? meta.is_system_prompt;
|
|
317
|
-
const contextFiles = meta.contextFiles ? meta.contextFiles.replace(/^\[|\]$/g, "").split(",").map((scope) => unquote(scope.trim())).filter(Boolean) : undefined;
|
|
318
|
-
const normalizedContextFiles = validateContextFileScopes(contextFiles, "role");
|
|
319
|
-
if (overrideSystemPrompt)
|
|
320
|
-
definition.overrideSystemPrompt = overrideSystemPrompt === "true";
|
|
321
|
-
if (normalizedContextFiles)
|
|
322
|
-
definition.contextFiles = normalizedContextFiles;
|
|
323
|
-
return definition;
|
|
324
|
-
}
|
|
325
|
-
const normalized = content.replace(/\r\n?/g, "\n");
|
|
326
|
-
if (normalized.startsWith("---\n") && normalized.indexOf("\n---", 3) < 0)
|
|
327
|
-
fail("INVALID_METADATA", "Role frontmatter is missing its closing delimiter");
|
|
328
|
-
let parsed;
|
|
329
|
-
try {
|
|
330
|
-
parsed = parseFrontmatter(content);
|
|
331
|
-
}
|
|
332
|
-
catch (error) {
|
|
333
|
-
fail("INVALID_METADATA", `Invalid role frontmatter: ${errorText(error)}`);
|
|
334
|
-
}
|
|
335
|
-
if (!object(parsed.frontmatter))
|
|
336
|
-
fail("INVALID_METADATA", "Role frontmatter must be an object");
|
|
337
|
-
const { model, tools, skills, extensions, extensionSettings, description, contextFiles } = parsed.frontmatter;
|
|
338
|
-
if (Object.prototype.hasOwnProperty.call(parsed.frontmatter, "disabledAgentResources"))
|
|
339
|
-
fail("INVALID_METADATA", "disabledAgentResources is no longer supported; use skills, extensions, and tools selectors");
|
|
340
|
-
if (Object.prototype.hasOwnProperty.call(parsed.frontmatter, "thinking"))
|
|
341
|
-
fail("INVALID_METADATA", "Role thinking is not supported; put it on model as provider/model:thinking");
|
|
342
|
-
const overrideSystemPrompt = parsed.frontmatter.overrideSystemPrompt ?? parsed.frontmatter.override_system_prompt ?? parsed.frontmatter.is_system_prompt;
|
|
343
|
-
if (model !== undefined && (typeof model !== "string" || model.trim() === ""))
|
|
344
|
-
fail("INVALID_METADATA", "Role model must be a non-empty string");
|
|
345
|
-
if (description !== undefined && (typeof description !== "string" || description.trim() === "" || description.length > 1024 || /[\r\n]/.test(description)))
|
|
346
|
-
fail("INVALID_METADATA", "Role description must be a non-empty single-line string of at most 1024 characters");
|
|
347
|
-
if (overrideSystemPrompt !== undefined && typeof overrideSystemPrompt !== "boolean")
|
|
348
|
-
fail("INVALID_METADATA", "Role overrideSystemPrompt must be a boolean");
|
|
349
|
-
const normalizedContextFiles = validateContextFileScopes(contextFiles, rolePath ?? "<role>");
|
|
350
|
-
const rolePathValue = rolePath ?? "<role>";
|
|
351
|
-
const normalizedTools = validateSelectorList(tools, rolePathValue, "tools", "INVALID_METADATA");
|
|
352
|
-
const normalizedSkills = validateSelectorList(skills, rolePathValue, "skills", "INVALID_METADATA");
|
|
353
|
-
const normalizedExtensions = validateSelectorList(extensions, rolePathValue, "extensions", "INVALID_METADATA");
|
|
354
|
-
const normalizedExtensionSettings = validateWorkflowExtensionSettings(extensionSettings, rolePathValue, "INVALID_METADATA", false);
|
|
355
|
-
const normalizedDescription = typeof description === "string" ? description.trim() : undefined;
|
|
356
|
-
const normalizedModel = typeof model === "string" ? model.trim() : undefined;
|
|
357
|
-
if (normalizedModel !== undefined)
|
|
358
|
-
assertModelThinking(normalizedModel, "Role model");
|
|
359
|
-
const definition = { prompt: parsed.body };
|
|
360
|
-
if (normalizedDescription !== undefined)
|
|
361
|
-
definition.description = normalizedDescription;
|
|
362
|
-
if (normalizedModel !== undefined)
|
|
363
|
-
definition.model = normalizedModel;
|
|
364
|
-
if (normalizedTools !== undefined)
|
|
365
|
-
definition.tools = normalizedTools;
|
|
366
|
-
if (normalizedSkills !== undefined)
|
|
367
|
-
definition.skills = normalizedSkills;
|
|
368
|
-
if (normalizedExtensions !== undefined)
|
|
369
|
-
definition.extensions = normalizedExtensions;
|
|
370
|
-
if (normalizedExtensionSettings !== undefined)
|
|
371
|
-
definition.extensionSettings = normalizedExtensionSettings;
|
|
372
|
-
if (typeof overrideSystemPrompt === "boolean")
|
|
373
|
-
definition.overrideSystemPrompt = overrideSystemPrompt;
|
|
374
|
-
if (normalizedContextFiles !== undefined)
|
|
375
|
-
definition.contextFiles = normalizedContextFiles;
|
|
376
|
-
return definition;
|
|
377
|
-
}
|
|
378
|
-
const ROLE_DIRECTORY = "pi-extensible-workflows";
|
|
379
|
-
export function workflowRoleDirectories(agentDir = getAgentDir()) {
|
|
380
|
-
return [join(agentDir, ROLE_DIRECTORY, "roles")];
|
|
381
|
-
}
|
|
382
|
-
function projectRoleDirectories(root) {
|
|
383
|
-
return [join(root, ROLE_DIRECTORY, "roles")];
|
|
384
|
-
}
|
|
385
|
-
function roleDirectorySources(dirs) {
|
|
386
|
-
const seen = new Set();
|
|
387
|
-
return dirs.flatMap((value) => {
|
|
388
|
-
const source = typeof value === "string" ? { path: value } : { path: value.path, extension: value.extension, ...(value.builtin === true ? { builtin: true } : {}) };
|
|
389
|
-
if (seen.has(source.path))
|
|
390
|
-
return [];
|
|
391
|
-
seen.add(source.path);
|
|
392
|
-
return [source];
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
function roleDirectoryLabel(source, extension = true) {
|
|
396
|
-
return source.extension ? `Extension "${source.extension.headline}" (${source.extension.version})` : extension ? "Registered workflow extension" : "Standard workflow";
|
|
397
|
-
}
|
|
398
|
-
function isRoleFile(dir, entry) {
|
|
399
|
-
if (extname(entry.name) !== ".md")
|
|
400
|
-
return false;
|
|
401
|
-
if (entry.isFile())
|
|
402
|
-
return true;
|
|
403
|
-
if (!entry.isSymbolicLink())
|
|
404
|
-
return false;
|
|
405
|
-
try {
|
|
406
|
-
return statSync(join(dir, entry.name)).isFile();
|
|
407
|
-
}
|
|
408
|
-
catch (error) {
|
|
409
|
-
if (isNodeError(error, "ENOENT"))
|
|
410
|
-
return false;
|
|
411
|
-
throw error;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
function scanRoleFiles(dirs, extension) {
|
|
415
|
-
const files = [];
|
|
416
|
-
for (const source of roleDirectorySources(dirs)) {
|
|
417
|
-
let entries;
|
|
418
|
-
try {
|
|
419
|
-
entries = readdirSync(source.path, { withFileTypes: true });
|
|
420
|
-
}
|
|
421
|
-
catch (error) {
|
|
422
|
-
if (!extension && isNodeError(error, "ENOENT"))
|
|
423
|
-
continue;
|
|
424
|
-
fail("INVALID_METADATA", `${roleDirectoryLabel(source, extension)} role directory "${source.path}" could not be scanned: ${errorText(error)}`);
|
|
425
|
-
}
|
|
426
|
-
for (const entry of entries)
|
|
427
|
-
if (isRoleFile(source.path, entry))
|
|
428
|
-
files.push({ name: basename(entry.name, ".md"), path: join(source.path, entry.name), source });
|
|
429
|
-
}
|
|
430
|
-
return files.sort((left, right) => left.name.localeCompare(right.name) || left.path.localeCompare(right.path));
|
|
431
|
-
}
|
|
432
|
-
function readRoleDefinitions(dirs, extension = false) {
|
|
433
|
-
const files = scanRoleFiles(dirs, extension);
|
|
434
|
-
const starterFiles = files.filter(({ source }) => source.builtin === true);
|
|
435
|
-
const regularFiles = files.filter(({ source }) => source.builtin !== true);
|
|
436
|
-
if (extension) {
|
|
437
|
-
const byName = new Map();
|
|
438
|
-
for (const file of regularFiles)
|
|
439
|
-
byName.set(file.name, [...(byName.get(file.name) ?? []), file]);
|
|
440
|
-
for (const [name, matches] of byName)
|
|
441
|
-
if (matches.length > 1)
|
|
442
|
-
fail("INVALID_METADATA", `Duplicate extension role "${name}": ${matches.map(({ path, source }) => `${roleDirectoryLabel(source)} role directory "${source.path}" (${path})`).join("; ")}`);
|
|
443
|
-
}
|
|
444
|
-
const read = (roleFiles) => Object.fromEntries(roleFiles.map(({ name, path, source }) => {
|
|
445
|
-
try {
|
|
446
|
-
return [name, parseRoleMarkdown(readFileSync(path, "utf8"), true, path)];
|
|
447
|
-
}
|
|
448
|
-
catch (error) {
|
|
449
|
-
if (extension)
|
|
450
|
-
fail("INVALID_METADATA", `${roleDirectoryLabel(source)} role directory "${source.path}" contains invalid role "${name}" at "${path}": ${errorText(error)}`);
|
|
451
|
-
throw error;
|
|
452
|
-
}
|
|
453
|
-
}));
|
|
454
|
-
return extension ? { ...read(starterFiles), ...read(regularFiles) } : read(files);
|
|
455
|
-
}
|
|
456
|
-
export function loadProjectAgentDefinitions(cwd) { return readRoleDefinitions(projectRoleDirectories(join(cwd, ".pi"))); }
|
|
457
|
-
export function loadAgentDefinitions(cwd, agentDir = getAgentDir(), projectTrusted = true, extensionRoleDirectories = registeredWorkflowRoleDirectoryRegistrations()) {
|
|
458
|
-
return deepFreeze({ ...readRoleDefinitions(extensionRoleDirectories, true), ...readRoleDefinitions(workflowRoleDirectories(agentDir)), ...(projectTrusted ? readRoleDefinitions(projectRoleDirectories(join(cwd, ".pi"))) : {}) });
|
|
459
|
-
}
|
|
460
21
|
function validateRolePolicies(definitions, roles, availableModels, aliases = {}, knownModels = availableModels, settingsPath) {
|
|
461
22
|
for (const role of roles) {
|
|
462
23
|
const definition = definitions[role];
|
|
@@ -767,16 +328,6 @@ export function workflowPrompt(template, values) {
|
|
|
767
328
|
return typeof value === "string" ? value : JSON.stringify(value, null, 2);
|
|
768
329
|
});
|
|
769
330
|
}
|
|
770
|
-
export function validateSchema(schema, at = "schema") {
|
|
771
|
-
if (!object(schema) || Object.getPrototypeOf(schema) !== Object.prototype || !jsonValue(schema))
|
|
772
|
-
fail("INVALID_SCHEMA", `${at} must be a plain JSON-compatible Schema object`);
|
|
773
|
-
if (typeof schema.type !== "string" && !Array.isArray(schema.type) && schema.$ref === undefined && schema.anyOf === undefined && schema.oneOf === undefined && schema.allOf === undefined && schema.const === undefined && schema.enum === undefined)
|
|
774
|
-
fail("INVALID_SCHEMA", `${at} has no JSON Schema shape`);
|
|
775
|
-
if (schema.required !== undefined && (!Array.isArray(schema.required) || schema.required.some((key) => typeof key !== "string")))
|
|
776
|
-
fail("INVALID_SCHEMA", `${at}.required must be an array of strings`);
|
|
777
|
-
if (schema.properties !== undefined && !object(schema.properties))
|
|
778
|
-
fail("INVALID_SCHEMA", `${at}.properties must be an object`);
|
|
779
|
-
}
|
|
780
331
|
const AGENT_OPTION_KEYS = new Set(["label", "model", "tools", "skills", "extensions", "contextFiles", "role", "outputSchema", "retries", "timeoutMs"]);
|
|
781
332
|
function validateAgentOption(key, value, aliases, knownModels, settingsPath) {
|
|
782
333
|
switch (key) {
|