@zihanw/pi-forge 0.1.0 → 0.3.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/README.md +354 -185
- package/README.zh-CN.md +453 -0
- package/examples/default-prompt-stack.json +54 -51
- package/examples/reviewer-prompt-stack.json +115 -0
- package/examples/sillytavern-dm-writer-prompt-stack.json +190 -0
- package/package.json +4 -8
- package/src/compiler.ts +588 -87
- package/src/index.ts +509 -140
- package/src/loader.ts +179 -12
- package/src/payload-capture.ts +85 -0
- package/src/policy.ts +42 -0
- package/src/regex.ts +500 -0
- package/src/sillytavern-importer.ts +484 -50
- package/src/stack-migration.ts +159 -0
- package/src/storage.ts +28 -0
- package/src/types.ts +83 -2
- package/src/web-editor/index.ts +2 -0
- package/src/web-editor/page.ts +2844 -0
- package/src/web-editor/server.ts +289 -0
- package/src/web-editor/types.ts +84 -0
- package/src/web-host.ts +229 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readdirSync, unlinkSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { legacyPromptStacksDir, promptStacksDir } from "./storage.ts";
|
|
4
|
+
|
|
5
|
+
export interface PromptStackMigrationOptions {
|
|
6
|
+
dryRun?: boolean;
|
|
7
|
+
overwrite?: boolean;
|
|
8
|
+
deleteLegacy?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface PromptStackMigrationFile {
|
|
12
|
+
name: string;
|
|
13
|
+
sourcePath: string;
|
|
14
|
+
targetPath: string;
|
|
15
|
+
action: "copy" | "overwrite" | "skip" | "error";
|
|
16
|
+
reason?: string;
|
|
17
|
+
deleteLegacy: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PromptStackMigrationReport {
|
|
21
|
+
sourceDir: string;
|
|
22
|
+
targetDir: string;
|
|
23
|
+
dryRun: boolean;
|
|
24
|
+
overwrite: boolean;
|
|
25
|
+
deleteLegacy: boolean;
|
|
26
|
+
files: PromptStackMigrationFile[];
|
|
27
|
+
copied: number;
|
|
28
|
+
overwritten: number;
|
|
29
|
+
skipped: number;
|
|
30
|
+
errors: number;
|
|
31
|
+
deletedLegacy: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function migrateLegacyPromptStacks(cwd: string, options: PromptStackMigrationOptions = {}): PromptStackMigrationReport {
|
|
35
|
+
const sourceDir = legacyPromptStacksDir(cwd);
|
|
36
|
+
const targetDir = promptStacksDir(cwd);
|
|
37
|
+
const report: PromptStackMigrationReport = {
|
|
38
|
+
sourceDir,
|
|
39
|
+
targetDir,
|
|
40
|
+
dryRun: options.dryRun === true,
|
|
41
|
+
overwrite: options.overwrite === true,
|
|
42
|
+
deleteLegacy: options.deleteLegacy === true,
|
|
43
|
+
files: [],
|
|
44
|
+
copied: 0,
|
|
45
|
+
overwritten: 0,
|
|
46
|
+
skipped: 0,
|
|
47
|
+
errors: 0,
|
|
48
|
+
deletedLegacy: 0,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
if (!existsSync(sourceDir)) return report;
|
|
52
|
+
|
|
53
|
+
let entries: string[];
|
|
54
|
+
try {
|
|
55
|
+
entries = readdirSync(sourceDir).filter((name) => name.endsWith(".json")).sort();
|
|
56
|
+
} catch (error) {
|
|
57
|
+
report.files.push({
|
|
58
|
+
name: "(legacy directory)",
|
|
59
|
+
sourcePath: sourceDir,
|
|
60
|
+
targetPath: targetDir,
|
|
61
|
+
action: "error",
|
|
62
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
63
|
+
deleteLegacy: false,
|
|
64
|
+
});
|
|
65
|
+
report.errors++;
|
|
66
|
+
return report;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const name of entries) {
|
|
70
|
+
const sourcePath = join(sourceDir, name);
|
|
71
|
+
const targetPath = join(targetDir, name);
|
|
72
|
+
const targetExists = existsSync(targetPath);
|
|
73
|
+
if (targetExists && !report.overwrite) {
|
|
74
|
+
report.files.push({
|
|
75
|
+
name,
|
|
76
|
+
sourcePath,
|
|
77
|
+
targetPath,
|
|
78
|
+
action: "skip",
|
|
79
|
+
reason: "target already exists",
|
|
80
|
+
deleteLegacy: false,
|
|
81
|
+
});
|
|
82
|
+
report.skipped++;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const action = targetExists ? "overwrite" : "copy";
|
|
87
|
+
try {
|
|
88
|
+
if (!report.dryRun) {
|
|
89
|
+
mkdirSync(dirname(targetPath), { recursive: true });
|
|
90
|
+
copyFileSync(sourcePath, targetPath);
|
|
91
|
+
if (report.deleteLegacy) {
|
|
92
|
+
unlinkSync(sourcePath);
|
|
93
|
+
report.deletedLegacy++;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (action === "overwrite") report.overwritten++;
|
|
97
|
+
else report.copied++;
|
|
98
|
+
report.files.push({
|
|
99
|
+
name,
|
|
100
|
+
sourcePath,
|
|
101
|
+
targetPath,
|
|
102
|
+
action,
|
|
103
|
+
deleteLegacy: report.deleteLegacy,
|
|
104
|
+
});
|
|
105
|
+
} catch (error) {
|
|
106
|
+
report.files.push({
|
|
107
|
+
name,
|
|
108
|
+
sourcePath,
|
|
109
|
+
targetPath,
|
|
110
|
+
action: "error",
|
|
111
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
112
|
+
deleteLegacy: false,
|
|
113
|
+
});
|
|
114
|
+
report.errors++;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return report;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function renderMigrationReport(report: PromptStackMigrationReport): string {
|
|
122
|
+
const lines = [
|
|
123
|
+
"# Prompt Stack Migration",
|
|
124
|
+
"",
|
|
125
|
+
`Source: ${report.sourceDir}`,
|
|
126
|
+
`Target: ${report.targetDir}`,
|
|
127
|
+
`Mode: ${report.dryRun ? "dry run" : "write"}`,
|
|
128
|
+
`Overwrite existing target files: ${report.overwrite ? "yes" : "no"}`,
|
|
129
|
+
`Delete legacy files after copy: ${report.deleteLegacy ? "yes" : "no"}`,
|
|
130
|
+
"",
|
|
131
|
+
`Copied: ${report.copied}`,
|
|
132
|
+
`Overwritten: ${report.overwritten}`,
|
|
133
|
+
`Skipped: ${report.skipped}`,
|
|
134
|
+
`Errors: ${report.errors}`,
|
|
135
|
+
`Deleted legacy files: ${report.deletedLegacy}`,
|
|
136
|
+
"",
|
|
137
|
+
"## Files",
|
|
138
|
+
"",
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
if (report.files.length === 0) {
|
|
142
|
+
lines.push("No legacy prompt-stack JSON files found.");
|
|
143
|
+
return lines.join("\n");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
for (const file of report.files) {
|
|
147
|
+
const suffix = file.reason ? ` (${file.reason})` : "";
|
|
148
|
+
lines.push(`- ${file.action}: ${file.name}${suffix}`);
|
|
149
|
+
lines.push(` from: ${file.sourcePath}`);
|
|
150
|
+
lines.push(` to: ${file.targetPath}`);
|
|
151
|
+
if (file.deleteLegacy) lines.push(" legacy delete: yes");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!report.deleteLegacy && !report.dryRun && (report.copied || report.overwritten)) {
|
|
155
|
+
lines.push("", "Legacy files were left in place. Re-run with `--delete-legacy` after checking the migrated stacks if you want to remove them.");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return lines.join("\n");
|
|
159
|
+
}
|
package/src/storage.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
2
|
+
|
|
3
|
+
export function promptStacksDir(cwd: string): string {
|
|
4
|
+
return join(cwd, ".pi", "forge", "prompt-stacks");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function legacyPromptStacksDir(cwd: string): string {
|
|
8
|
+
return join(cwd, ".pi", "prompt-stacks");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function promptStackReadDirs(cwd: string): string[] {
|
|
12
|
+
return [promptStacksDir(cwd), legacyPromptStacksDir(cwd)];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function promptStackPath(cwd: string, id: string): string {
|
|
16
|
+
return join(promptStacksDir(cwd), `${id}.json`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isInsidePromptStackStorage(cwd: string, filePath: string): boolean {
|
|
20
|
+
return promptStackReadDirs(cwd).some((dir) => isInsideDir(dir, filePath));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isInsideDir(rootDir: string, filePath: string): boolean {
|
|
24
|
+
const root = resolve(rootDir);
|
|
25
|
+
const target = resolve(filePath);
|
|
26
|
+
const rel = relative(root, target);
|
|
27
|
+
return !!rel && !rel.startsWith("..") && !isAbsolute(rel);
|
|
28
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -19,6 +19,42 @@ export type PromptStackSlot =
|
|
|
19
19
|
| "pi-docs"
|
|
20
20
|
| "variables";
|
|
21
21
|
|
|
22
|
+
export type PromptStackSlotFormat = "xml" | "json" | "plain";
|
|
23
|
+
|
|
24
|
+
export type PromptRegexStage = "history" | "compiled";
|
|
25
|
+
|
|
26
|
+
export type PromptRegexEffect = "outgoing" | "display" | "both" | "finalize";
|
|
27
|
+
|
|
28
|
+
export type PromptRegexTarget = "system" | "messages";
|
|
29
|
+
|
|
30
|
+
export interface PromptRegexRule {
|
|
31
|
+
id: string;
|
|
32
|
+
name?: string;
|
|
33
|
+
enabled?: boolean;
|
|
34
|
+
stage: PromptRegexStage;
|
|
35
|
+
effect?: PromptRegexEffect;
|
|
36
|
+
pattern: string;
|
|
37
|
+
flags?: string;
|
|
38
|
+
replace?: string;
|
|
39
|
+
trimStrings?: string[];
|
|
40
|
+
roles?: string[];
|
|
41
|
+
targets?: PromptRegexTarget[];
|
|
42
|
+
maxMessages?: number;
|
|
43
|
+
maxChars?: number;
|
|
44
|
+
minDepth?: number;
|
|
45
|
+
maxDepth?: number;
|
|
46
|
+
source?: Record<string, unknown>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface PromptRegexConfig {
|
|
50
|
+
schemaVersion?: 1;
|
|
51
|
+
rules?: PromptRegexRule[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type PromptResourcePolicy =
|
|
55
|
+
| { allow?: string[]; deny?: never }
|
|
56
|
+
| { allow?: never; deny?: string[] };
|
|
57
|
+
|
|
22
58
|
export interface PromptStackDefaults {
|
|
23
59
|
syntheticMessagesVisible?: boolean;
|
|
24
60
|
unresolvedMacroPolicy?: "warn" | "keep" | "error";
|
|
@@ -50,15 +86,40 @@ export interface VariablesSlotOptions {
|
|
|
50
86
|
includeSession?: boolean;
|
|
51
87
|
/** Include turn variables. Default: true. */
|
|
52
88
|
includeTurn?: boolean;
|
|
89
|
+
/** Render format. Default: xml. */
|
|
90
|
+
format?: Exclude<PromptStackSlotFormat, "json">;
|
|
53
91
|
}
|
|
54
92
|
|
|
55
93
|
export interface PromptStackSlotOptions {
|
|
56
94
|
/** For chat-history: include the latest user message in the expanded history. Default: true. */
|
|
57
95
|
includeLastUserMessage?: boolean;
|
|
96
|
+
/** For chat-history: remove assistant thinking content blocks from inserted history. Default: false. */
|
|
97
|
+
stripAssistantThinking?: boolean;
|
|
98
|
+
/** For chat-history: keep only messages with these roles. Default: all roles. */
|
|
99
|
+
roles?: string[];
|
|
100
|
+
/** For chat-history: include Pi branch/compaction summaries. Default: true. */
|
|
101
|
+
includeSummaries?: boolean;
|
|
102
|
+
/** For chat-history: keep or drop tool-call/tool-result history. Default: keep. */
|
|
103
|
+
toolMode?: "keep" | "drop";
|
|
104
|
+
/** For chat-history: keep only the most recent N messages after filtering. */
|
|
105
|
+
maxMessages?: number;
|
|
106
|
+
/** For chat-history: keep only the most recent messages within an approximate character budget. */
|
|
107
|
+
maxChars?: number;
|
|
58
108
|
/** For variables: control which variable scopes are included. */
|
|
59
109
|
includeStatic?: boolean;
|
|
60
110
|
includeSession?: boolean;
|
|
61
111
|
includeTurn?: boolean;
|
|
112
|
+
format?: PromptStackSlotFormat;
|
|
113
|
+
/** For tools: only render tools that provide prompt snippets, matching Pi's default prompt builder. */
|
|
114
|
+
onlyWithSnippets?: boolean;
|
|
115
|
+
/** For tool-guidelines: override the plain-format section heading. */
|
|
116
|
+
heading?: string;
|
|
117
|
+
/** For tool-guidelines: include Pi's default concise/file-path guideline bullets. */
|
|
118
|
+
includePiDefaultGuidelines?: boolean;
|
|
119
|
+
/** For tool-guidelines: match Pi's default wording where it differs from pi-forge's generic wording. */
|
|
120
|
+
piStyle?: boolean;
|
|
121
|
+
/** For skills: omit the skills section unless the read tool is active, matching Pi's default prompt builder. */
|
|
122
|
+
requireReadTool?: boolean;
|
|
62
123
|
[key: string]: unknown;
|
|
63
124
|
}
|
|
64
125
|
|
|
@@ -70,6 +131,10 @@ export interface PromptStackSlotItem extends PromptStackBaseItem {
|
|
|
70
131
|
|
|
71
132
|
export type PromptStackItem = PromptStackBlockItem | PromptStackSlotItem;
|
|
72
133
|
|
|
134
|
+
export type PromptVariablePrimitive = string | number | boolean | null;
|
|
135
|
+
|
|
136
|
+
export type PromptVariableValue = PromptVariablePrimitive | PromptVariableValue[] | { [key: string]: PromptVariableValue };
|
|
137
|
+
|
|
73
138
|
export interface PromptStack {
|
|
74
139
|
schemaVersion: 1;
|
|
75
140
|
type?: "pi-forge.prompt-stack";
|
|
@@ -80,7 +145,10 @@ export interface PromptStack {
|
|
|
80
145
|
mode?: PromptStackMode;
|
|
81
146
|
defaults?: PromptStackDefaults;
|
|
82
147
|
context?: PromptStackContextOptions;
|
|
148
|
+
tools?: PromptResourcePolicy;
|
|
149
|
+
skills?: PromptResourcePolicy;
|
|
83
150
|
variables?: Record<string, string>;
|
|
151
|
+
regex?: PromptRegexConfig;
|
|
84
152
|
items: PromptStackItem[];
|
|
85
153
|
import?: Record<string, unknown>;
|
|
86
154
|
}
|
|
@@ -100,8 +168,8 @@ export interface PromptStackDiagnostic {
|
|
|
100
168
|
}
|
|
101
169
|
|
|
102
170
|
export interface PromptVariableStore {
|
|
103
|
-
turn: Record<string,
|
|
104
|
-
session: Record<string,
|
|
171
|
+
turn: Record<string, PromptVariableValue>;
|
|
172
|
+
session: Record<string, PromptVariableValue>;
|
|
105
173
|
sessionDirty?: boolean;
|
|
106
174
|
}
|
|
107
175
|
|
|
@@ -118,8 +186,21 @@ export interface CompileSystemPromptResult {
|
|
|
118
186
|
diagnostics: PromptStackDiagnostic[];
|
|
119
187
|
}
|
|
120
188
|
|
|
189
|
+
export type CompileMessageSourceKind = "stack-item" | "chat-history" | "implicit-history";
|
|
190
|
+
|
|
191
|
+
export interface CompileMessageSource {
|
|
192
|
+
kind: CompileMessageSourceKind;
|
|
193
|
+
itemId?: string;
|
|
194
|
+
itemName?: string;
|
|
195
|
+
slot?: string;
|
|
196
|
+
historyIndex?: number;
|
|
197
|
+
historyCount?: number;
|
|
198
|
+
role?: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
121
201
|
export interface CompileMessagesResult {
|
|
122
202
|
messages: AgentMessage[];
|
|
203
|
+
messageSources: CompileMessageSource[];
|
|
123
204
|
diagnostics: PromptStackDiagnostic[];
|
|
124
205
|
}
|
|
125
206
|
|