@zihanw/pi-forge 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/README.md +87 -2
  2. package/README.zh-CN.md +72 -2
  3. package/dist/compiler.d.ts +10 -0
  4. package/dist/compiler.d.ts.map +1 -0
  5. package/dist/compiler.js +490 -0
  6. package/dist/compiler.js.map +1 -0
  7. package/dist/extension-registry.d.ts +25 -0
  8. package/dist/extension-registry.d.ts.map +1 -0
  9. package/dist/extension-registry.js +6 -0
  10. package/dist/extension-registry.js.map +1 -0
  11. package/dist/forge-extensions.d.ts +28 -0
  12. package/dist/forge-extensions.d.ts.map +1 -0
  13. package/dist/forge-extensions.js +182 -0
  14. package/dist/forge-extensions.js.map +1 -0
  15. package/dist/index.d.ts +8 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +376 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/lifecycle.d.ts +14 -0
  20. package/dist/lifecycle.d.ts.map +1 -0
  21. package/dist/lifecycle.js +139 -0
  22. package/dist/lifecycle.js.map +1 -0
  23. package/dist/loader.d.ts +8 -0
  24. package/dist/loader.d.ts.map +1 -0
  25. package/dist/loader.js +345 -0
  26. package/dist/loader.js.map +1 -0
  27. package/dist/macro-engine.d.ts +23 -0
  28. package/dist/macro-engine.d.ts.map +1 -0
  29. package/dist/macro-engine.js +262 -0
  30. package/dist/macro-engine.js.map +1 -0
  31. package/dist/payload-capture.d.ts +15 -0
  32. package/dist/payload-capture.d.ts.map +1 -0
  33. package/dist/payload-capture.js +86 -0
  34. package/dist/payload-capture.js.map +1 -0
  35. package/dist/payload-command.d.ts +10 -0
  36. package/dist/payload-command.d.ts.map +1 -0
  37. package/dist/payload-command.js +112 -0
  38. package/dist/payload-command.js.map +1 -0
  39. package/dist/policy.d.ts +6 -0
  40. package/dist/policy.d.ts.map +1 -0
  41. package/dist/policy.js +38 -0
  42. package/dist/policy.js.map +1 -0
  43. package/dist/preset-command.d.ts +14 -0
  44. package/dist/preset-command.d.ts.map +1 -0
  45. package/dist/preset-command.js +232 -0
  46. package/dist/preset-command.js.map +1 -0
  47. package/dist/preview.d.ts +12 -0
  48. package/dist/preview.d.ts.map +1 -0
  49. package/dist/preview.js +179 -0
  50. package/dist/preview.js.map +1 -0
  51. package/dist/regex.d.ts +7 -0
  52. package/dist/regex.d.ts.map +1 -0
  53. package/dist/regex.js +432 -0
  54. package/dist/regex.js.map +1 -0
  55. package/dist/render-helpers.d.ts +44 -0
  56. package/dist/render-helpers.d.ts.map +1 -0
  57. package/dist/render-helpers.js +128 -0
  58. package/dist/render-helpers.js.map +1 -0
  59. package/dist/runtime-state.d.ts +26 -0
  60. package/dist/runtime-state.d.ts.map +1 -0
  61. package/dist/runtime-state.js +15 -0
  62. package/dist/runtime-state.js.map +1 -0
  63. package/dist/sillytavern-importer/items.d.ts +3 -0
  64. package/dist/sillytavern-importer/items.d.ts.map +1 -0
  65. package/dist/sillytavern-importer/items.js +88 -0
  66. package/dist/sillytavern-importer/items.js.map +1 -0
  67. package/dist/sillytavern-importer/macros.d.ts +15 -0
  68. package/dist/sillytavern-importer/macros.d.ts.map +1 -0
  69. package/dist/sillytavern-importer/macros.js +141 -0
  70. package/dist/sillytavern-importer/macros.js.map +1 -0
  71. package/dist/sillytavern-importer/prompt-order.d.ts +6 -0
  72. package/dist/sillytavern-importer/prompt-order.d.ts.map +1 -0
  73. package/dist/sillytavern-importer/prompt-order.js +38 -0
  74. package/dist/sillytavern-importer/prompt-order.js.map +1 -0
  75. package/dist/sillytavern-importer/regex.d.ts +3 -0
  76. package/dist/sillytavern-importer/regex.d.ts.map +1 -0
  77. package/dist/sillytavern-importer/regex.js +275 -0
  78. package/dist/sillytavern-importer/regex.js.map +1 -0
  79. package/dist/sillytavern-importer/report.d.ts +21 -0
  80. package/dist/sillytavern-importer/report.d.ts.map +1 -0
  81. package/dist/sillytavern-importer/report.js +166 -0
  82. package/dist/sillytavern-importer/report.js.map +1 -0
  83. package/dist/sillytavern-importer/types.d.ts +106 -0
  84. package/dist/sillytavern-importer/types.d.ts.map +1 -0
  85. package/dist/sillytavern-importer/types.js +2 -0
  86. package/dist/sillytavern-importer/types.js.map +1 -0
  87. package/dist/sillytavern-importer.d.ts +5 -0
  88. package/dist/sillytavern-importer.d.ts.map +1 -0
  89. package/dist/sillytavern-importer.js +117 -0
  90. package/dist/sillytavern-importer.js.map +1 -0
  91. package/dist/slot-renderers.d.ts +25 -0
  92. package/dist/slot-renderers.d.ts.map +1 -0
  93. package/dist/slot-renderers.js +334 -0
  94. package/dist/slot-renderers.js.map +1 -0
  95. package/dist/stack-migration.d.ts +29 -0
  96. package/dist/stack-migration.d.ts.map +1 -0
  97. package/dist/stack-migration.js +126 -0
  98. package/dist/stack-migration.js.map +1 -0
  99. package/dist/storage.d.ts +10 -0
  100. package/dist/storage.d.ts.map +1 -0
  101. package/dist/storage.js +36 -0
  102. package/dist/storage.js.map +1 -0
  103. package/dist/types.d.ts +173 -0
  104. package/dist/types.d.ts.map +1 -0
  105. package/dist/types.js +2 -0
  106. package/dist/types.js.map +1 -0
  107. package/dist/web-editor/index.d.ts +3 -0
  108. package/dist/web-editor/index.d.ts.map +1 -0
  109. package/dist/web-editor/index.js +2 -0
  110. package/dist/web-editor/index.js.map +1 -0
  111. package/dist/web-editor/page.d.ts +2 -0
  112. package/dist/web-editor/page.d.ts.map +1 -0
  113. package/dist/web-editor/page.js +3331 -0
  114. package/dist/web-editor/page.js.map +1 -0
  115. package/dist/web-editor/server.d.ts +4 -0
  116. package/dist/web-editor/server.d.ts.map +1 -0
  117. package/dist/web-editor/server.js +263 -0
  118. package/dist/web-editor/server.js.map +1 -0
  119. package/dist/web-editor/types.d.ts +125 -0
  120. package/dist/web-editor/types.d.ts.map +1 -0
  121. package/dist/web-editor/types.js +2 -0
  122. package/dist/web-editor/types.js.map +1 -0
  123. package/dist/web-host.d.ts +29 -0
  124. package/dist/web-host.d.ts.map +1 -0
  125. package/dist/web-host.js +174 -0
  126. package/dist/web-host.js.map +1 -0
  127. package/examples/custom-system-status-extension/README.md +85 -0
  128. package/examples/custom-system-status-extension/index.ts +136 -0
  129. package/examples/custom-system-status-extension/prompt-stack.json +39 -0
  130. package/examples/image-reader-prompt-stack.json +124 -0
  131. package/package.json +28 -2
  132. package/src/compiler.ts +2 -472
  133. package/src/extension-registry.ts +33 -0
  134. package/src/forge-extensions.ts +223 -0
  135. package/src/index.ts +208 -653
  136. package/src/lifecycle.ts +171 -0
  137. package/src/macro-engine.ts +358 -0
  138. package/src/payload-command.ts +138 -0
  139. package/src/preset-command.ts +280 -0
  140. package/src/preview.ts +226 -0
  141. package/src/render-helpers.ts +169 -0
  142. package/src/runtime-state.ts +40 -0
  143. package/src/sillytavern-importer/items.ts +98 -0
  144. package/src/sillytavern-importer/macros.ts +159 -0
  145. package/src/sillytavern-importer/prompt-order.ts +54 -0
  146. package/src/sillytavern-importer/regex.ts +270 -0
  147. package/src/sillytavern-importer/report.ts +202 -0
  148. package/src/sillytavern-importer/types.ts +120 -0
  149. package/src/sillytavern-importer.ts +55 -865
  150. package/src/slot-renderers.ts +414 -0
  151. package/src/storage.ts +17 -0
  152. package/src/types.ts +3 -14
  153. package/src/web-editor/page.ts +516 -30
  154. package/src/web-editor/server.ts +9 -4
  155. package/src/web-editor/types.ts +18 -4
  156. package/src/web-host.ts +16 -13
@@ -78,6 +78,11 @@ async function handleRequest(host: WebEditorHost, token: string, req: IncomingMe
78
78
  return;
79
79
  }
80
80
 
81
+ if (req.method === "GET" && parts[1] === "resources" && parts.length === 2) {
82
+ sendJson(res, 200, host.listResources());
83
+ return;
84
+ }
85
+
81
86
  if (req.method === "POST" && parts[1] === "stacks" && parts.length === 2) {
82
87
  const body = await readJsonBody(req);
83
88
  const parsed = readStackPayload(body);
@@ -88,7 +93,7 @@ async function handleRequest(host: WebEditorHost, token: string, req: IncomingMe
88
93
  const options = isPlainObject(body)
89
94
  ? { activate: body.activate === true, overwrite: body.overwrite === true }
90
95
  : {};
91
- const result = host.createStack(parsed.stack, options);
96
+ const result = await host.createStack(parsed.stack, options);
92
97
  if (result.ok && parsed.importFormat) {
93
98
  sendJson(res, 200, { ...result, importFormat: parsed.importFormat, importReport: parsed.importReport });
94
99
  return;
@@ -114,12 +119,12 @@ async function handleRequest(host: WebEditorHost, token: string, req: IncomingMe
114
119
  sendJson(res, 400, { error: parsed.error });
115
120
  return;
116
121
  }
117
- sendOperation(res, host.saveStack(parts[2]!, parsed.stack));
122
+ sendOperation(res, await host.saveStack(parts[2]!, parsed.stack));
118
123
  return;
119
124
  }
120
125
 
121
126
  if (req.method === "DELETE" && parts[1] === "stacks" && parts.length === 3) {
122
- sendOperation(res, host.deleteStack(parts[2]!));
127
+ sendOperation(res, await host.deleteStack(parts[2]!));
123
128
  return;
124
129
  }
125
130
 
@@ -173,7 +178,7 @@ async function handleRequest(host: WebEditorHost, token: string, req: IncomingMe
173
178
  }
174
179
 
175
180
  if (req.method === "POST" && parts[1] === "reload" && parts.length === 2) {
176
- sendOperation(res, host.reloadStacks());
181
+ sendOperation(res, await host.reloadStacks());
177
182
  return;
178
183
  }
179
184
 
@@ -16,10 +16,11 @@ export interface WebEditorStackSummary {
16
16
  export interface WebEditorHost {
17
17
  cwd: string;
18
18
  listStacks(): WebEditorStackSummary[];
19
+ listResources(): WebEditorPolicyResources;
19
20
  getStack(id: string): { stack: PromptStack; filePath: string; diagnostics: PromptStackDiagnostic[] } | undefined;
20
- createStack(stack: PromptStack, options: WebEditorCreateStackOptions): WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }>;
21
- saveStack(id: string, stack: PromptStack): WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }>;
22
- deleteStack(id: string): WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }>;
21
+ createStack(stack: PromptStack, options: WebEditorCreateStackOptions): Promise<WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }>>;
22
+ saveStack(id: string, stack: PromptStack): Promise<WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }>>;
23
+ deleteStack(id: string): Promise<WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }>>;
23
24
  validateStack(stack: PromptStack): PromptStackDiagnostic[];
24
25
  previewStack(id: string, stack: PromptStack): WebEditorOperationResult<{ text: string; preview?: WebEditorPreview; diagnostics: PromptStackDiagnostic[] }>;
25
26
  getPayload(): WebEditorOperationResult<WebEditorPayloadSnapshot>;
@@ -27,7 +28,7 @@ export interface WebEditorHost {
27
28
  clearPayload(): WebEditorOperationResult<WebEditorPayloadSnapshot>;
28
29
  activateStack(id: string): WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }>;
29
30
  disableStacks(): WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }>;
30
- reloadStacks(): WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }>;
31
+ reloadStacks(): Promise<WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }>>;
31
32
  }
32
33
 
33
34
  export interface WebEditorPreviewSection {
@@ -70,6 +71,19 @@ export interface WebEditorCreateStackOptions {
70
71
  overwrite?: boolean;
71
72
  }
72
73
 
74
+ export interface WebEditorPolicyResource {
75
+ name: string;
76
+ description?: string;
77
+ source?: string;
78
+ active?: boolean;
79
+ hidden?: boolean;
80
+ }
81
+
82
+ export interface WebEditorPolicyResources {
83
+ tools: WebEditorPolicyResource[];
84
+ skills: WebEditorPolicyResource[];
85
+ }
86
+
73
87
  export type WebEditorOperationResult<T> = ({ ok: true } & T) | { ok: false; status?: number; error: string };
74
88
 
75
89
  export interface WebEditorServer {
package/src/web-host.ts CHANGED
@@ -12,6 +12,7 @@ import type {
12
12
  WebEditorHost,
13
13
  WebEditorOperationResult,
14
14
  WebEditorPayloadSnapshot,
15
+ WebEditorPolicyResources,
15
16
  WebEditorPreview,
16
17
  WebEditorStackSummary,
17
18
  } from "./web-editor/index.ts";
@@ -22,12 +23,13 @@ export interface WebHostRuntime {
22
23
  getActiveId(): string | undefined;
23
24
  getSelectedActiveId(): string | undefined;
24
25
  setActive(id: string | undefined): boolean;
25
- reloadStacks(preferredId?: string): void;
26
+ reloadStacks(preferredId?: string): Promise<void>;
26
27
  buildPreview(target: LoadedPromptStack): {
27
28
  text: string;
28
29
  preview: WebEditorPreview;
29
30
  diagnostics: PromptStackDiagnostic[];
30
31
  };
32
+ getPolicyResources(): WebEditorPolicyResources;
31
33
  getPayload(): WebEditorOperationResult<WebEditorPayloadSnapshot>;
32
34
  armPayload(savePath?: string): WebEditorOperationResult<WebEditorPayloadSnapshot>;
33
35
  clearPayload(): WebEditorOperationResult<WebEditorPayloadSnapshot>;
@@ -37,6 +39,7 @@ export function createWebEditorHost(ctx: ExtensionCommandContext, runtime: WebHo
37
39
  return {
38
40
  cwd: ctx.cwd,
39
41
  listStacks: () => stackSummaries(runtime.getStacks(), runtime.getActive()),
42
+ listResources: () => runtime.getPolicyResources(),
40
43
  getStack: (id) => {
41
44
  const loaded = runtime.getStacks().find((candidate) => candidate.stack.id === id);
42
45
  return loaded ? { stack: loaded.stack, filePath: loaded.filePath, diagnostics: loaded.diagnostics } : undefined;
@@ -63,8 +66,8 @@ export function createWebEditorHost(ctx: ExtensionCommandContext, runtime: WebHo
63
66
  runtime.setActive("none");
64
67
  return { ok: true, activeId: runtime.getActiveId(), stacks: stackSummaries(runtime.getStacks(), runtime.getActive()) };
65
68
  },
66
- reloadStacks: () => {
67
- runtime.reloadStacks(runtime.getSelectedActiveId());
69
+ reloadStacks: async () => {
70
+ await runtime.reloadStacks(runtime.getSelectedActiveId());
68
71
  return { ok: true, activeId: runtime.getActiveId(), stacks: stackSummaries(runtime.getStacks(), runtime.getActive()) };
69
72
  },
70
73
  };
@@ -128,12 +131,12 @@ export function loadWebEditorSettings(ctx: ExtensionCommandContext): { preferred
128
131
  };
129
132
  }
130
133
 
131
- function saveStackFile(
134
+ async function saveStackFile(
132
135
  ctx: ExtensionCommandContext,
133
136
  runtime: WebHostRuntime,
134
137
  id: string,
135
138
  stack: PromptStack,
136
- ): WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }> {
139
+ ): Promise<WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }>> {
137
140
  if (!ctx.isProjectTrusted()) {
138
141
  return { ok: false, status: 403, error: "Project is not trusted; refusing to save prompt stacks." };
139
142
  }
@@ -146,18 +149,18 @@ function saveStackFile(
146
149
 
147
150
  writeFileSync(target.filePath, `${JSON.stringify(stack, null, 2)}\n`, "utf8");
148
151
  const preferredId = runtime.getActive()?.stack.id === id ? stack.id : runtime.getSelectedActiveId();
149
- runtime.reloadStacks(preferredId);
152
+ await runtime.reloadStacks(preferredId);
150
153
  const saved = runtime.getStacks().find((candidate) => candidate.stack.id === stack.id) ?? runtime.getStacks().find((candidate) => candidate.filePath === target.filePath);
151
154
  if (!saved) return { ok: false, status: 500, error: "Saved stack could not be reloaded." };
152
155
  return { ok: true, stack: stackSummary(saved, runtime.getActive()), stacks: stackSummaries(runtime.getStacks(), runtime.getActive()) };
153
156
  }
154
157
 
155
- function createStackFile(
158
+ async function createStackFile(
156
159
  ctx: ExtensionCommandContext,
157
160
  runtime: WebHostRuntime,
158
161
  stack: PromptStack,
159
162
  options: WebEditorCreateStackOptions,
160
- ): WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }> {
163
+ ): Promise<WebEditorOperationResult<{ stack: WebEditorStackSummary; stacks: WebEditorStackSummary[] }>> {
161
164
  if (!ctx.isProjectTrusted()) {
162
165
  return { ok: false, status: 403, error: "Project is not trusted; refusing to create prompt stacks." };
163
166
  }
@@ -182,7 +185,7 @@ function createStackFile(
182
185
  const previousSelection = runtime.getSelectedActiveId();
183
186
  mkdirSync(dirname(targetPath), { recursive: true });
184
187
  writeFileSync(targetPath, `${JSON.stringify(stack, null, 2)}\n`, "utf8");
185
- runtime.reloadStacks(options.activate ? stack.id : (previousSelection ?? "none"));
188
+ await runtime.reloadStacks(options.activate ? stack.id : (previousSelection ?? "none"));
186
189
  if (options.activate) runtime.setActive(stack.id);
187
190
 
188
191
  const created = runtime.getStacks().find((candidate) => candidate.stack.id === stack.id);
@@ -190,11 +193,11 @@ function createStackFile(
190
193
  return { ok: true, stack: stackSummary(created, runtime.getActive()), stacks: stackSummaries(runtime.getStacks(), runtime.getActive()) };
191
194
  }
192
195
 
193
- function deleteStackFile(
196
+ async function deleteStackFile(
194
197
  ctx: ExtensionCommandContext,
195
198
  runtime: WebHostRuntime,
196
199
  id: string,
197
- ): WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }> {
200
+ ): Promise<WebEditorOperationResult<{ activeId?: string; stacks: WebEditorStackSummary[] }>> {
198
201
  if (!ctx.isProjectTrusted()) {
199
202
  return { ok: false, status: 403, error: "Project is not trusted; refusing to delete prompt stacks." };
200
203
  }
@@ -209,9 +212,9 @@ function deleteStackFile(
209
212
  unlinkSync(target.filePath);
210
213
  if (wasActive) {
211
214
  runtime.setActive("none");
212
- runtime.reloadStacks("none");
215
+ await runtime.reloadStacks("none");
213
216
  } else {
214
- runtime.reloadStacks(runtime.getSelectedActiveId());
217
+ await runtime.reloadStacks(runtime.getSelectedActiveId());
215
218
  }
216
219
  return { ok: true, activeId: runtime.getActiveId(), stacks: stackSummaries(runtime.getStacks(), runtime.getActive()) };
217
220
  }