blun-king-cli 9.1.253 → 9.1.254

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.
@@ -7,11 +7,17 @@ const DEFAULT_MAX_CHARS = 1800;
7
7
  const MIN_MAX_CHARS = 512;
8
8
  const HARD_MAX_CHARS = 2800;
9
9
  const SAFE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/u;
10
+ const TELEGRAM_SUBJECT_RE = /^\d{1,32}$/u;
11
+ const TELEGRAM_CHAT_RE = /^-?\d{1,32}$/u;
10
12
 
11
13
  function enabled(env) {
12
14
  return /^(?:1|true)$/iu.test(String(env.BLUN_IDENTITY_CONTEXT ?? '').trim());
13
15
  }
14
16
 
17
+ function autoGraphEnabled(env) {
18
+ return /^(?:1|true)$/iu.test(String(env.BLUN_IDENTITY_AUTO_GRAPH ?? '').trim());
19
+ }
20
+
15
21
  function safeId(value) {
16
22
  const normalized = String(value ?? '').trim();
17
23
  return SAFE_ID_RE.test(normalized) ? normalized : '';
@@ -55,6 +61,27 @@ function readJson(root, segments) {
55
61
  }
56
62
  }
57
63
 
64
+ function createJsonOnce(root, segments, value) {
65
+ const target = path.resolve(root, ...segments);
66
+ if (!isInside(root, target)) return false;
67
+ let handle;
68
+ try {
69
+ const parent = path.dirname(target);
70
+ fs.mkdirSync(parent, { recursive: true });
71
+ const realParent = fs.realpathSync(parent);
72
+ if (!isInside(root, realParent)) return false;
73
+ handle = fs.openSync(target, 'wx', 0o600);
74
+ fs.writeFileSync(handle, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
75
+ fs.fsyncSync(handle);
76
+ return true;
77
+ } catch (error) {
78
+ if (error?.code === 'EEXIST') return false;
79
+ return false;
80
+ } finally {
81
+ if (handle !== undefined) fs.closeSync(handle);
82
+ }
83
+ }
84
+
58
85
  function cleanText(value, maxChars = 240) {
59
86
  if (typeof value !== 'string') return '';
60
87
  return value.replace(/[\u0000-\u001f\u007f]+/gu, ' ').replace(/\s+/gu, ' ').trim().slice(0, maxChars);
@@ -123,6 +150,65 @@ function truncateBlock(value, limit) {
123
150
  return `${value.slice(0, Math.max(0, limit - marker.length)).trimEnd()}${marker}`;
124
151
  }
125
152
 
153
+ function trustedTimestamp(value) {
154
+ const text = cleanText(value, 64);
155
+ if (!text || Number.isNaN(Date.parse(text))) return undefined;
156
+ return text;
157
+ }
158
+
159
+ function recordChannelIdentity(envelope, env = process.env) {
160
+ if (!autoGraphEnabled(env) || envelope?.source !== 'telegram') return undefined;
161
+ const meta = envelope?.meta;
162
+ if (!meta || typeof meta !== 'object') return undefined;
163
+ const subjectId = String(meta.user_id ?? '').trim();
164
+ const chatId = String(meta.chat_id ?? '').trim();
165
+ if (!TELEGRAM_SUBJECT_RE.test(subjectId) || !TELEGRAM_CHAT_RE.test(chatId)) return undefined;
166
+
167
+ const root = resolveRoot(env);
168
+ if (!root) return undefined;
169
+ const manifest = readJson(root, ['manifest.json']);
170
+ const tenantId = safeId(env.BLUN_IDENTITY_TENANT_ID);
171
+ const manifestTenantId = safeId(manifest?.tenant_id);
172
+ const configuredAgentId = safeId(env.BLUN_AGENT_ID);
173
+ const agentId = configuredAgentId || safeId(manifest?.active_agent_id);
174
+ if (!tenantId || tenantId !== manifestTenantId || !agentId || manifest?.version !== 1) return undefined;
175
+
176
+ const actorId = `telegram-${subjectId}`;
177
+ const groupId = chatId.startsWith('-') ? `telegram-chat-${chatId.slice(1)}` : undefined;
178
+ const displayName = cleanText(meta.user, 120) || actorId;
179
+ const firstSeenAt = trustedTimestamp(meta.ts);
180
+ const created = [];
181
+ if (createJsonOnce(root, ['actors', `${actorId}.json`], {
182
+ version: 1,
183
+ actor_id: actorId,
184
+ provider: 'telegram',
185
+ provider_subject_id: subjectId,
186
+ display_name: displayName,
187
+ kind: meta.is_bot === true || String(meta.is_bot ?? '').toLowerCase() === 'true' ? 'agent' : 'person',
188
+ aliases: [],
189
+ ...(firstSeenAt ? { first_seen_at: firstSeenAt } : {}),
190
+ })) created.push('actor');
191
+ if (createJsonOnce(root, ['agents', agentId, 'relationships', `${actorId}.json`], {
192
+ version: 1,
193
+ actor_id: actorId,
194
+ status: 'new',
195
+ summary: '',
196
+ confirmed_preferences: [],
197
+ no_gos: [],
198
+ open_threads: [],
199
+ })) created.push('relationship');
200
+ if (groupId && createJsonOnce(root, ['groups', `${groupId}.json`], {
201
+ version: 1,
202
+ group_id: groupId,
203
+ provider: 'telegram',
204
+ provider_chat_id: chatId,
205
+ display_name: groupId,
206
+ purpose: '',
207
+ relevant_roles: [],
208
+ })) created.push('group');
209
+ return { actor_id: actorId, ...(groupId ? { group_id: groupId } : {}), created };
210
+ }
211
+
126
212
  function identitySystemBlock(env = process.env) {
127
213
  if (!enabled(env)) return '';
128
214
  const root = resolveRoot(env);
@@ -164,4 +250,5 @@ function identitySystemBlock(env = process.env) {
164
250
 
165
251
  module.exports = {
166
252
  identitySystemBlock,
253
+ recordChannelIdentity,
167
254
  };
package/blun.mjs CHANGED
@@ -21438,7 +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
+ var { identitySystemBlock, recordChannelIdentity } = createRequire(import.meta.url)("./bin/identity-context-policy.cjs");
21442
21442
  async function prepareSystemPromptContext(kaos, brandHome, options) {
21443
21443
  const additionalDirs = normalizeAdditionalDirs(options?.additionalDirs ?? []);
21444
21444
  const [cwdListing, agentsMdResult, additionalDirsInfo] = await Promise.all([
@@ -516449,6 +516449,7 @@ var BlunTUI = class {
516449
516449
  * steers one FIFO head into the active turn without interrupting it.
516450
516450
  */
516451
516451
  injectChannelMessage(envelope, acknowledge) {
516452
+ recordChannelIdentity({ source: "telegram", meta: envelope.meta });
516452
516453
  const remoteCommand = channelMessageAddressed(envelope) ? telegramRemoteCommand(envelope.text) : void 0;
516453
516454
  if (remoteCommand !== void 0) {
516454
516455
  this.injectTelegramRemoteCommand(envelope, remoteCommand, acknowledge);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blun-king-cli",
3
- "version": "9.1.253",
3
+ "version": "9.1.254",
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": {