-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_bundle.mjs
More file actions
269 lines (239 loc) · 8.69 KB
/
Copy pathprompt_bundle.mjs
File metadata and controls
269 lines (239 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import crypto from "crypto";
import fs from "fs";
import path from "path";
import { parseFrontmatter } from "./markdown_frontmatter.mjs";
function env(name, fallback = "") {
const v = process.env[name];
return (v === undefined || v === null || String(v).trim() === "") ? fallback : String(v);
}
function normalizeList(s) {
return String(s || "")
.split(",")
.map((x) => String(x).trim())
.filter(Boolean);
}
function readFileText(filePath) {
try {
if (!fs.existsSync(filePath)) return "";
return fs.readFileSync(filePath, "utf8");
} catch {
return "";
}
}
function sha256Hex(text) {
return crypto.createHash("sha256").update(String(text || ""), "utf8").digest("hex");
}
function truncateText(text, maxChars) {
const s = String(text || "");
if (!maxChars || s.length <= maxChars) return { text: s, truncated: false };
return { text: `${s.slice(0, maxChars)}\n\n...[truncated]`, truncated: true };
}
const CONSTITUTIONAL_KERNEL_FILE = "CONSTITUTIONAL_KERNEL.md";
const CONSTITUTIONAL_FILES = [
CONSTITUTIONAL_KERNEL_FILE,
"CONSTITUTION.md",
"IDENTITY.md",
"identity/personas/SOUL.md",
"TOOLS.md",
"identity/personas/USER.md",
"PROMPT_CORE.md",
"PROMPT_MODES.md",
];
const SUPPLEMENTAL_FILES = [];
const CLIENT_SAFE_PROMPT_FILES = [
CONSTITUTIONAL_KERNEL_FILE,
"CONSTITUTION.md",
"IDENTITY.md",
"PROMPT_CORE.md",
"PROMPT_MODES.md",
];
function normalizeTrustZone(value) {
const raw = String(value || "").trim().toLowerCase();
return ["private_self", "trusted_collaborator", "outside_contact", "paid_public"].includes(raw) ? raw : "";
}
function resolvePromptFiles(options = {}) {
const trustZone = normalizeTrustZone(options.trustZone ?? options.trust_zone);
if (trustZone === "paid_public") {
return CLIENT_SAFE_PROMPT_FILES.map((p) => path.resolve(process.cwd(), p));
}
const defaultList = [...CONSTITUTIONAL_FILES];
const packs = {
core: defaultList,
creative: [
...CONSTITUTIONAL_FILES,
...SUPPLEMENTAL_FILES,
"PROTOCOL.md",
"overlays/LEVERAGE.md",
"identity/personas/PENGUIN.md",
"identity/personas/COPPER-INU.md",
],
ops: [
...CONSTITUTIONAL_FILES,
...SUPPLEMENTAL_FILES,
"OPERATIONS.md",
"COMMUNICATION.md",
"MARKETPLACE_PROTOCOL.md",
"LEGAL-GUARDRAILS.md",
"CLIENTS.md",
"CLIENT_TEMPLATE.md",
"MEMORY.md",
],
// Full is intentionally "batteries included" but still bounded; if you truly want everything,
// set DIZZY_PROMPT_FILES manually.
full: [
...CONSTITUTIONAL_FILES,
...SUPPLEMENTAL_FILES,
"DESIGN.md",
"INTERACTION_NORMS.md",
"NEXT.md",
"CAPABILITIES.md",
"ECONOMICS.md",
"OPERATIONS.md",
"COMMUNICATION.md",
"MARKETPLACE_PROTOCOL.md",
"LEGAL-GUARDRAILS.md",
"PROTOCOL.md",
"overlays/LEVERAGE.md",
"identity/personas/PENGUIN.md",
"identity/personas/COPPER-INU.md",
"identity/personas/TROLL.md",
"MEMORY.md",
],
};
const pack = String(env("DIZZY_PROMPT_PACK", "")).trim().toLowerCase();
let list = defaultList;
if (pack && packs[pack]) {
list = packs[pack];
} else {
const configured = normalizeList(env("DIZZY_PROMPT_FILES", ""));
if (configured.length) list = configured;
}
const overlaysConfig = normalizeList(env("DIZZY_PROMPT_OVERLAYS", ""));
const combinedList = [...list, ...overlaysConfig];
return combinedList.map((p) => path.resolve(process.cwd(), p));
}
const ATTENTION_SINK_FILES = [
"PROMPT_CORE.md",
"AGENTS.md",
"CONSTITUTION.md",
"IDENTITY.md",
];
let cached = null;
export function getPromptSources(options = {}) {
const maxCharsPerFile = Math.max(500, Number(env("DIZZY_PROMPT_FILE_MAX_CHARS", "8000")) || 8000);
const files = resolvePromptFiles(options);
// Block-Sparse Prompt Attention: Ensure sink files are always processed first
const sortedFiles = [...files].sort((a, b) => {
const aIsSink = ATTENTION_SINK_FILES.some(s => a.endsWith(s));
const bIsSink = ATTENTION_SINK_FILES.some(s => b.endsWith(s));
if (aIsSink && !bIsSink) return -1;
if (!aIsSink && bIsSink) return 1;
return 0;
});
let sources = [];
let totalChars = 0;
let compressionRatio = 1.0; // Placeholder for future retrofitting
for (const absPath of sortedFiles) {
const rel = path.relative(process.cwd(), absPath).replace(/\\/g, "/");
const isSink = ATTENTION_SINK_FILES.includes(rel);
const raw = readFileText(absPath);
if (!raw) {
sources.push({
path: rel,
role: CONSTITUTIONAL_FILES.includes(rel) ? "constitutional" : "supplemental",
exists: false,
bytes: 0,
sha256: "",
truncated: false,
text: "",
is_sink: isSink,
});
continue;
}
const { data, body } = parseFrontmatter(raw);
if (data && data.expires_at) {
const expiry = Date.parse(data.expires_at.trim());
if (!Number.isNaN(expiry) && Date.now() > expiry) {
if (CONSTITUTIONAL_FILES.includes(rel)) {
throw new Error(`Constitutional prompt source expired: ${rel} (${data.expires_at.trim()})`);
}
continue;
}
}
const hash = sha256Hex(raw);
// Sinks are never truncated. Other files respect the maxChars limit.
const charsLimit = isSink ? 0 : maxCharsPerFile;
const { text, truncated } = truncateText(body.trim(), charsLimit);
const role = CONSTITUTIONAL_FILES.includes(rel) ? "constitutional" : "supplemental";
sources.push({
path: rel,
role,
exists: true,
bytes: Buffer.byteLength(raw, "utf8"),
sha256: hash,
truncated,
text,
is_sink: isSink,
});
totalChars += text.length;
}
// Placeholder for Dynamic Prompt Retrofitting. In the future, this block
// would check hardware state and prune/summarize non-sink sources if needed.
// For now, we just calculate the potential compression.
const hardwareThresholdChars = 32000; // Example threshold
if (totalChars > hardwareThresholdChars) {
// This is where summarization logic would go.
// For now, we simulate a 50% reduction for demonstration.
const targetChars = hardwareThresholdChars;
compressionRatio = targetChars / totalChars;
}
return { sources, compression_ratio: compressionRatio };
}
export function buildChatSystemPrompt(options = {}) {
const trustZone = normalizeTrustZone(options.trustZone ?? options.trust_zone);
const { sources, compression_ratio } = getPromptSources({ trustZone });
const backend = String(env("DIZZY_CHAT_BACKEND", "")).trim().toLowerCase();
const model = String(env("GEMINI_MODEL", "")).trim();
const pack = String(env("DIZZY_PROMPT_PACK", "")).trim().toLowerCase();
const brevityMode = String(env("DIZZY_BREVITY_MODE", "lite")).trim().toLowerCase();
const affectMode = String(env("DIZZY_AFFECT_MODE", "attuned")).trim().toLowerCase();
const reinforcementMode = String(env("DIZZY_REINFORCEMENT_MODE", "gold_star")).trim().toLowerCase();
const header = [
"You are Dizzy.",
"Treat the following files as the authoritative runtime constitution. Follow them in order.",
"If they conflict, prefer earlier documents over later ones.",
"Keep the constitutional layer small and legible. Supplemental guidance should refine judgment, not flood it.",
"",
`chat_backend=${backend || "(unset)"}`,
`gemini_model=${model || "(unset)"}`,
`prompt_pack=${trustZone === "paid_public" ? "client_safe_forced" : (pack || "(unset)")}`,
`trust_zone=${trustZone || "(unset)"}`,
`brevity_mode=${brevityMode || "(unset)"}`,
`affect_mode=${affectMode || "(unset)"}`,
`reinforcement_mode=${reinforcementMode || "(unset)"}`,
`context_compression_ratio=${compression_ratio.toFixed(2)}`,
].join("\n");
const blocks = sources.map((s) => {
const titleBase = `${s.path} [${s.role}]`;
const title = s.exists ? `${titleBase}${s.truncated ? " (truncated)" : ""}` : `${titleBase} (missing)`;
return [
"",
`=== ${title} ===`,
s.exists ? s.text : "(missing file)",
`=== END ${s.path} ===`,
].join("\n");
});
const systemPrompt = [header, ...blocks].join("\n");
return { systemPrompt, sources, compression_ratio };
}
export function getCachedChatSystemPrompt(options = {}) {
const now = Date.now();
const ttlMs = Math.max(250, Number(env("DIZZY_PROMPT_CACHE_MS", "2000")) || 2000);
const trustZone = normalizeTrustZone(options.trustZone ?? options.trust_zone);
const pack = String(env("DIZZY_PROMPT_PACK", "")).trim().toLowerCase();
const key = `${trustZone || "default"}:${pack}`;
if (cached && cached.key === key && now - cached.at < ttlMs) return cached.value;
const value = buildChatSystemPrompt({ trustZone });
cached = { at: now, key, value };
return value;
}