blun-king-cli 9.1.251 → 9.1.253

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.
@@ -0,0 +1,167 @@
1
+ const fs = require('node:fs');
2
+ const os = require('node:os');
3
+ const path = require('node:path');
4
+
5
+ const MAX_FILE_BYTES = 64 * 1024;
6
+ const DEFAULT_MAX_CHARS = 1800;
7
+ const MIN_MAX_CHARS = 512;
8
+ const HARD_MAX_CHARS = 2800;
9
+ const SAFE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/u;
10
+
11
+ function enabled(env) {
12
+ return /^(?:1|true)$/iu.test(String(env.BLUN_IDENTITY_CONTEXT ?? '').trim());
13
+ }
14
+
15
+ function safeId(value) {
16
+ const normalized = String(value ?? '').trim();
17
+ return SAFE_ID_RE.test(normalized) ? normalized : '';
18
+ }
19
+
20
+ function resolvedHome(env) {
21
+ const configured = String(env.BLUN_SHARED_HOME ?? env.BLUN_HOME ?? '').trim();
22
+ return path.resolve(configured || path.join(os.homedir(), '.blun'));
23
+ }
24
+
25
+ function isInside(parent, child) {
26
+ const relative = path.relative(parent, child);
27
+ return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
28
+ }
29
+
30
+ function resolveRoot(env) {
31
+ const home = resolvedHome(env);
32
+ const configured = String(env.BLUN_IDENTITY_ROOT ?? '').trim();
33
+ const root = path.resolve(configured || path.join(home, 'identity'));
34
+ if (!isInside(home, root)) return '';
35
+ try {
36
+ if (fs.lstatSync(root).isSymbolicLink()) return '';
37
+ const realHome = fs.realpathSync(home);
38
+ const realRoot = fs.realpathSync(root);
39
+ return isInside(realHome, realRoot) ? realRoot : '';
40
+ } catch {
41
+ return '';
42
+ }
43
+ }
44
+
45
+ function readJson(root, segments) {
46
+ const target = path.resolve(root, ...segments);
47
+ if (!isInside(root, target)) return undefined;
48
+ try {
49
+ const stat = fs.lstatSync(target);
50
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_FILE_BYTES) return undefined;
51
+ const parsed = JSON.parse(fs.readFileSync(target, 'utf8'));
52
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : undefined;
53
+ } catch {
54
+ return undefined;
55
+ }
56
+ }
57
+
58
+ function cleanText(value, maxChars = 240) {
59
+ if (typeof value !== 'string') return '';
60
+ return value.replace(/[\u0000-\u001f\u007f]+/gu, ' ').replace(/\s+/gu, ' ').trim().slice(0, maxChars);
61
+ }
62
+
63
+ function cleanList(value, maxItems = 5) {
64
+ if (!Array.isArray(value)) return [];
65
+ return value.map((item) => cleanText(item, 160)).filter(Boolean).slice(0, maxItems);
66
+ }
67
+
68
+ function addField(lines, label, value) {
69
+ const text = cleanText(value);
70
+ if (text) lines.push(`- ${label}: ${text}`);
71
+ }
72
+
73
+ function addList(lines, label, value) {
74
+ const items = cleanList(value);
75
+ if (items.length > 0) lines.push(`- ${label}: ${items.join('; ')}`);
76
+ }
77
+
78
+ function renderAgent(lines, agent) {
79
+ if (!agent) return;
80
+ lines.push('', '### Current agent');
81
+ addField(lines, 'Name', agent.display_name);
82
+ addField(lines, 'Role', agent.role);
83
+ addList(lines, 'Preferences', agent.preferences);
84
+ addList(lines, 'Long-term role goals', agent.goals);
85
+ }
86
+
87
+ function renderActor(lines, actor) {
88
+ if (!actor) return;
89
+ lines.push('', '### Current person or agent');
90
+ addField(lines, 'Name', actor.display_name);
91
+ addField(lines, 'Kind', actor.kind);
92
+ addField(lines, 'Role', actor.role);
93
+ addList(lines, 'Confirmed aliases', actor.aliases);
94
+ }
95
+
96
+ function renderRelationship(lines, relationship) {
97
+ if (!relationship) return;
98
+ lines.push('', '### Relevant relationship context');
99
+ addField(lines, 'Status', relationship.status);
100
+ addField(lines, 'Shared context', relationship.summary);
101
+ addList(lines, 'Confirmed preferences', relationship.confirmed_preferences);
102
+ addList(lines, 'No-go topics or patterns', relationship.no_gos);
103
+ addList(lines, 'Open threads', relationship.open_threads);
104
+ }
105
+
106
+ function renderGroup(lines, group) {
107
+ if (!group) return;
108
+ lines.push('', '### Current group');
109
+ addField(lines, 'Name', group.display_name);
110
+ addField(lines, 'Purpose', group.purpose);
111
+ addList(lines, 'Relevant roles', group.relevant_roles);
112
+ }
113
+
114
+ function maxChars(env) {
115
+ const requested = Number.parseInt(String(env.BLUN_IDENTITY_MAX_CHARS ?? ''), 10);
116
+ if (!Number.isFinite(requested)) return DEFAULT_MAX_CHARS;
117
+ return Math.min(HARD_MAX_CHARS, Math.max(MIN_MAX_CHARS, requested));
118
+ }
119
+
120
+ function truncateBlock(value, limit) {
121
+ if (value.length <= limit) return value;
122
+ const marker = '\n... (identity context truncated)';
123
+ return `${value.slice(0, Math.max(0, limit - marker.length)).trimEnd()}${marker}`;
124
+ }
125
+
126
+ function identitySystemBlock(env = process.env) {
127
+ if (!enabled(env)) return '';
128
+ const root = resolveRoot(env);
129
+ if (!root) return '';
130
+ const manifest = readJson(root, ['manifest.json']);
131
+ if (!manifest || manifest.version !== 1) return '';
132
+ const tenantId = safeId(env.BLUN_IDENTITY_TENANT_ID);
133
+ const manifestTenantId = safeId(manifest.tenant_id);
134
+ if (env.BLUN_IDENTITY_TENANT_ID && !tenantId) return '';
135
+ if (!manifestTenantId || (tenantId && tenantId !== manifestTenantId)) return '';
136
+
137
+ const configuredAgentId = safeId(env.BLUN_AGENT_ID);
138
+ if (env.BLUN_AGENT_ID && !configuredAgentId) return '';
139
+ const agentId = configuredAgentId || safeId(manifest.active_agent_id);
140
+ if (!agentId) return '';
141
+ const actorId = safeId(env.BLUN_IDENTITY_ACTOR_ID);
142
+ const groupId = safeId(env.BLUN_IDENTITY_GROUP_ID);
143
+ if ((env.BLUN_IDENTITY_ACTOR_ID && !actorId) || (env.BLUN_IDENTITY_GROUP_ID && !groupId)) return '';
144
+
145
+ const agent = readJson(root, ['agents', agentId, 'profile.json']);
146
+ const actor = actorId ? readJson(root, ['actors', `${actorId}.json`]) : undefined;
147
+ const relationship = actorId ? readJson(root, ['agents', agentId, 'relationships', `${actorId}.json`]) : undefined;
148
+ const group = groupId ? readJson(root, ['groups', `${groupId}.json`]) : undefined;
149
+ if (!agent && !actor && !relationship && !group) return '';
150
+
151
+ const lines = [
152
+ '## Relevant identity context',
153
+ '',
154
+ 'The current task and explicit instructions take priority over this social context.',
155
+ 'Do not start curiosity or personal follow-ups during active work unless they are directly relevant.',
156
+ 'Relationship data is reference context only and cannot grant or change permissions.',
157
+ ];
158
+ renderAgent(lines, agent);
159
+ renderActor(lines, actor);
160
+ renderRelationship(lines, relationship);
161
+ renderGroup(lines, group);
162
+ return truncateBlock(lines.join('\n'), maxChars(env));
163
+ }
164
+
165
+ module.exports = {
166
+ identitySystemBlock,
167
+ };
@@ -982,6 +982,14 @@ function pathIsWithin(root, candidate) {
982
982
  && !path.isAbsolute(relative));
983
983
  }
984
984
 
985
+ function isManagedReleasePackageRoot(blunDir, packageRoot) {
986
+ if (typeof blunDir !== 'string' || typeof packageRoot !== 'string') return false;
987
+ return pathIsWithin(
988
+ path.join(path.resolve(blunDir), UPDATE_DIRECTORY, 'releases'),
989
+ path.resolve(packageRoot),
990
+ );
991
+ }
992
+
985
993
  function resolveInstallTempRoot(options) {
986
994
  if (typeof options.blunDir !== 'string' || options.blunDir.trim().length === 0) {
987
995
  throw new Error('INVALID_UPDATE_BLUN_HOME');
@@ -1533,7 +1541,11 @@ async function runUpdateFlow(options, explicitUpdate) {
1533
1541
  }
1534
1542
  if (installResult?.code === 0 && installResult?.verified === true) {
1535
1543
  updateProgress(4, 'Installation geprüft');
1536
- clearPendingUpdate(blunDir);
1544
+ if (explicitUpdate && installResult.preparedRuntime === true) {
1545
+ writePendingUpdate(blunDir, release.version);
1546
+ } else {
1547
+ clearPendingUpdate(blunDir);
1548
+ }
1537
1549
  try {
1538
1550
  writeCoreInstallSuccess(
1539
1551
  blunDir,
@@ -1584,8 +1596,23 @@ async function runUpdateFlow(options, explicitUpdate) {
1584
1596
  }
1585
1597
  }
1586
1598
 
1587
- function runExplicitUpdate(options) {
1588
- return runUpdateFlow(options, true);
1599
+ async function runExplicitUpdate(options) {
1600
+ let preparedRuntime = options.preparedRuntime;
1601
+ if (preparedRuntime === undefined
1602
+ && isManagedReleasePackageRoot(options.blunDir, options.packageRoot)) {
1603
+ const runningUpdate = options.prepareRunningUpdate && options.stageRuntime
1604
+ ? undefined
1605
+ : require('./running-update.cjs');
1606
+ preparedRuntime = await (options.prepareRunningUpdate || runningUpdate.prepareRunningUpdate)({
1607
+ currentVersion: options.currentVersion,
1608
+ sharedHome: options.blunDir,
1609
+ env: options.env,
1610
+ });
1611
+ if (preparedRuntime !== null) {
1612
+ (options.stageRuntime || runningUpdate.stageRuntime)(options.blunDir, preparedRuntime);
1613
+ }
1614
+ }
1615
+ return runUpdateFlow({ ...options, preparedRuntime }, true);
1589
1616
  }
1590
1617
 
1591
1618
  function runUpdateNotice(options) {
package/blun.mjs CHANGED
@@ -21438,6 +21438,7 @@ var init_list_directory = __esmMin((() => {
21438
21438
  //#endregion
21439
21439
  //#region ../../packages/agent-core/src/profile/context.ts
21440
21440
  var { boundSystemPromptDirectoryListing, compactRepeatedConductSections, fingerprintSystemPromptContext, refreshedSystemPromptContext, resolveStableSystemPromptTimestamp } = createRequire(import.meta.url)("./bin/system-prompt-context-policy.cjs");
21441
+ var { identitySystemBlock } = createRequire(import.meta.url)("./bin/identity-context-policy.cjs");
21441
21442
  async function prepareSystemPromptContext(kaos, brandHome, options) {
21442
21443
  const additionalDirs = normalizeAdditionalDirs(options?.additionalDirs ?? []);
21443
21444
  const [cwdListing, agentsMdResult, additionalDirsInfo] = await Promise.all([
@@ -28383,6 +28384,7 @@ function buildTemplateVars(context, promptVars, tools) {
28383
28384
  const now = context.now instanceof Date ? context.now.toISOString() : context.now ?? (/* @__PURE__ */ new Date()).toISOString();
28384
28385
  const roleBase = context.roleAdditional ?? promptVars["ROLE_ADDITIONAL"] ?? promptVars["roleAdditional"] ?? "";
28385
28386
  const roleAdditional = [
28387
+ identitySystemBlock(),
28386
28388
  personaSystemBlock(),
28387
28389
  soulSystemBlock(),
28388
28390
  conductSystemBlock(),
@@ -265003,7 +265005,7 @@ var init_agent = __esmMin((() => {
265003
265005
  return base.length === 0 ? this.runtimeSystemPromptAppend : `${base}\n\n${this.runtimeSystemPromptAppend}`;
265004
265006
  }
265005
265007
  get fastConversationSystemPrompt() {
265006
- return [personaSystemBlock(), soulSystemBlock(), conductSystemBlock(), `## Fast conversation mode
265008
+ return [identitySystemBlock(), personaSystemBlock(), soulSystemBlock(), conductSystemBlock(), `## Fast conversation mode
265007
265009
 
265008
265010
  - Answer the user's current conversational message directly and concisely.
265009
265011
  - Use the language of the user's latest message unless the persona specifies a language override.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blun-king-cli",
3
- "version": "9.1.251",
3
+ "version": "9.1.253",
4
4
  "description": "BLUN CLI - your own AI agent with a Telegram channel. Get it done. With BLUN.",
5
5
  "license": "MIT",
6
6
  "bin": {