blun-king-cli 9.1.567 → 9.1.569
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/agent-spine-plugin/.claude-plugin/marketplace.json +1 -1
- package/agent-spine-plugin/.claude-plugin/plugin.json +1 -1
- package/agent-spine-plugin/.codex-plugin/plugin.json +2 -1
- package/agent-spine-plugin/CHANGELOG.md +1581 -0
- package/agent-spine-plugin/README.md +30 -4
- package/agent-spine-plugin/blun.plugin.json +3 -3
- package/agent-spine-plugin/docs/acceptance.md +61 -0
- package/agent-spine-plugin/docs/assignment-continuation.md +48 -0
- package/agent-spine-plugin/docs/host-integration.md +178 -0
- package/agent-spine-plugin/docs/preflight-recall.md +69 -0
- package/agent-spine-plugin/docs/preservation-contract.md +53 -0
- package/agent-spine-plugin/docs/quality-gates.md +50 -0
- package/agent-spine-plugin/docs/releasing.md +85 -0
- package/agent-spine-plugin/docs/session-timeline.md +251 -0
- package/agent-spine-plugin/docs/source-roots.md +113 -0
- package/agent-spine-plugin/docs/structured-completion.md +67 -0
- package/agent-spine-plugin/docs/world-model.md +94 -0
- package/agent-spine-plugin/hooks/codex.json +2 -2
- package/agent-spine-plugin/hooks/hooks.json +1 -1
- package/agent-spine-plugin/hooks/version.json +1 -1
- package/agent-spine-plugin/package.json +13 -3
- package/agent-spine-plugin/scripts/check-codex-install.js +226 -0
- package/agent-spine-plugin/scripts/check-hosts.js +14 -5
- package/agent-spine-plugin/scripts/check-install-hook.js +226 -0
- package/agent-spine-plugin/scripts/check-install-selfstarter.js +154 -0
- package/agent-spine-plugin/scripts/check-install.js +478 -0
- package/agent-spine-plugin/scripts/check-line-budget.js +58 -0
- package/agent-spine-plugin/scripts/check-syntax.js +29 -0
- package/agent-spine-plugin/scripts/github-actions.js +11 -0
- package/agent-spine-plugin/scripts/hermetic-process.js +183 -0
- package/agent-spine-plugin/scripts/release-check.js +145 -0
- package/agent-spine-plugin/scripts/run-acceptance.js +19 -0
- package/agent-spine-plugin/scripts/run-checks.js +47 -0
- package/agent-spine-plugin/scripts/run-tests-hermetic.js +89 -0
- package/agent-spine-plugin/skills/agent-spine/SKILL.md +16 -2
- package/agent-spine-plugin/spine-example/1-identity.md +12 -0
- package/agent-spine-plugin/spine-example/2-voice.md +6 -0
- package/agent-spine-plugin/spine-example/3-conduct.md +8 -0
- package/agent-spine-plugin/spine-example/4-history.md +4 -0
- package/agent-spine-plugin/src/cli-agent.js +296 -0
- package/agent-spine-plugin/src/cli-attention.js +95 -0
- package/agent-spine-plugin/src/cli-autonomy.js +36 -0
- package/agent-spine-plugin/src/cli-common.js +71 -0
- package/agent-spine-plugin/src/cli-continuity.js +116 -0
- package/agent-spine-plugin/src/cli-core.js +128 -0
- package/agent-spine-plugin/src/cli-diagnostics.js +291 -0
- package/agent-spine-plugin/src/cli-host.js +21 -0
- package/agent-spine-plugin/src/cli-learning.js +305 -0
- package/agent-spine-plugin/src/cli-premortem.js +16 -0
- package/agent-spine-plugin/src/cli-sharing.js +230 -0
- package/agent-spine-plugin/src/cli.js +40 -1350
- package/agent-spine-plugin/src/codex-reader-launcher.js +182 -0
- package/agent-spine-plugin/src/hook.js +185 -567
- package/agent-spine-plugin/src/index.js +16 -2
- package/agent-spine-plugin/src/lib/acceptance.js +113 -11
- package/agent-spine-plugin/src/lib/action-lesson-recall.js +53 -0
- package/agent-spine-plugin/src/lib/attention-context.js +167 -0
- package/agent-spine-plugin/src/lib/attention-events.js +113 -0
- package/agent-spine-plugin/src/lib/attention-privacy.js +72 -0
- package/agent-spine-plugin/src/lib/attention-schema.js +164 -0
- package/agent-spine-plugin/src/lib/attention-storage.js +93 -0
- package/agent-spine-plugin/src/lib/attention.js +9 -600
- package/agent-spine-plugin/src/lib/audit-premortem.js +223 -0
- package/agent-spine-plugin/src/lib/audit.js +56 -6
- package/agent-spine-plugin/src/lib/autonomy-policy.js +110 -0
- package/agent-spine-plugin/src/lib/autonomy-store.js +202 -0
- package/agent-spine-plugin/src/lib/autonomy.js +8 -0
- package/agent-spine-plugin/src/lib/briefing.js +67 -4
- package/agent-spine-plugin/src/lib/catalog-document-read.js +51 -0
- package/agent-spine-plugin/src/lib/catalog.js +1 -1
- package/agent-spine-plugin/src/lib/codex-installation.js +231 -0
- package/agent-spine-plugin/src/lib/codex-skill-installation.js +211 -0
- package/agent-spine-plugin/src/lib/context.js +3 -1
- package/agent-spine-plugin/src/lib/delivery-agent-usage.js +224 -0
- package/agent-spine-plugin/src/lib/delivery-assignment.js +220 -0
- package/agent-spine-plugin/src/lib/delivery-command-actions.js +453 -0
- package/agent-spine-plugin/src/lib/delivery-knowledge.js +78 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-binding.js +107 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-closure.js +171 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-codec.js +45 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-correction.js +101 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-file.js +21 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-index.js +493 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-inspection.js +37 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-recovery.js +120 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-rejection.js +65 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-results.js +28 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-session-guard.js +46 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-write-ledger.js +285 -0
- package/agent-spine-plugin/src/lib/delivery-premortem.js +499 -0
- package/agent-spine-plugin/src/lib/delivery-shell-heredoc.js +115 -0
- package/agent-spine-plugin/src/lib/delivery-shell-substitutions.js +111 -0
- package/agent-spine-plugin/src/lib/delivery-shell-wrapper.js +64 -0
- package/agent-spine-plugin/src/lib/delivery-target.js +73 -0
- package/agent-spine-plugin/src/lib/delivery-verification.js +445 -0
- package/agent-spine-plugin/src/lib/documents.js +27 -5
- package/agent-spine-plugin/src/lib/filesystem-retry.js +2 -0
- package/agent-spine-plugin/src/lib/gateway-common.js +68 -0
- package/agent-spine-plugin/src/lib/gateway-control.js +302 -0
- package/agent-spine-plugin/src/lib/gateway-delivery.js +103 -0
- package/agent-spine-plugin/src/lib/gateway-execution.js +350 -0
- package/agent-spine-plugin/src/lib/gateway-host-lifecycle.js +185 -0
- package/agent-spine-plugin/src/lib/gateway-inspection.js +85 -0
- package/agent-spine-plugin/src/lib/gateway-knowledge.js +129 -0
- package/agent-spine-plugin/src/lib/gateway-policy-provenance.js +197 -0
- package/agent-spine-plugin/src/lib/gateway-premortem-disposition.js +82 -0
- package/agent-spine-plugin/src/lib/gateway-premortem.js +363 -0
- package/agent-spine-plugin/src/lib/gateway-runs.js +300 -0
- package/agent-spine-plugin/src/lib/gateway-runtime-identity.js +31 -0
- package/agent-spine-plugin/src/lib/gateway-runtime-records.js +21 -0
- package/agent-spine-plugin/src/lib/gateway-runtime.js +18 -1623
- package/agent-spine-plugin/src/lib/gateway-state-transaction.js +356 -0
- package/agent-spine-plugin/src/lib/gateway-state.js +342 -0
- package/agent-spine-plugin/src/lib/hook-artifact-guards.js +356 -0
- package/agent-spine-plugin/src/lib/hook-audit.js +16 -2
- package/agent-spine-plugin/src/lib/hook-briefing-use.js +68 -0
- package/agent-spine-plugin/src/lib/hook-context.js +424 -0
- package/agent-spine-plugin/src/lib/hook-final-message.js +26 -0
- package/agent-spine-plugin/src/lib/hook-input.js +27 -0
- package/agent-spine-plugin/src/lib/hook-output.js +140 -0
- package/agent-spine-plugin/src/lib/hook-premortem.js +257 -0
- package/agent-spine-plugin/src/lib/hook-process-advisory.js +37 -0
- package/agent-spine-plugin/src/lib/hook-protection.js +95 -0
- package/agent-spine-plugin/src/lib/hook-stop-verification.js +84 -0
- package/agent-spine-plugin/src/lib/hook-timeline.js +91 -0
- package/agent-spine-plugin/src/lib/identifier-analysis.js +446 -0
- package/agent-spine-plugin/src/lib/indexed-memory.js +23 -6
- package/agent-spine-plugin/src/lib/knowledge-evidence.js +431 -0
- package/agent-spine-plugin/src/lib/learning-applications.js +441 -0
- package/agent-spine-plugin/src/lib/learning-candidates.js +274 -0
- package/agent-spine-plugin/src/lib/learning-context.js +130 -0
- package/agent-spine-plugin/src/lib/learning-delivery-contracts.js +310 -0
- package/agent-spine-plugin/src/lib/learning-evaluation-contracts.js +350 -0
- package/agent-spine-plugin/src/lib/learning-evaluation-registration.js +372 -0
- package/agent-spine-plugin/src/lib/learning-evaluation-revocation.js +292 -0
- package/agent-spine-plugin/src/lib/learning-evidence-contracts.js +321 -0
- package/agent-spine-plugin/src/lib/learning-findings.js +458 -0
- package/agent-spine-plugin/src/lib/learning-measurement-contracts.js +285 -0
- package/agent-spine-plugin/src/lib/learning-measurements.js +265 -0
- package/agent-spine-plugin/src/lib/learning-outcome-contracts.js +165 -0
- package/agent-spine-plugin/src/lib/learning-outcomes.js +343 -0
- package/agent-spine-plugin/src/lib/learning-reconciliation.js +290 -0
- package/agent-spine-plugin/src/lib/learning-retry-contracts.js +188 -0
- package/agent-spine-plugin/src/lib/learning-schema.js +229 -0
- package/agent-spine-plugin/src/lib/learning-scope-targets.js +424 -0
- package/agent-spine-plugin/src/lib/learning-state-upgrade.js +477 -0
- package/agent-spine-plugin/src/lib/learning-status-configuration.js +476 -0
- package/agent-spine-plugin/src/lib/learning-storage.js +203 -0
- package/agent-spine-plugin/src/lib/learning-trial-recovery.js +218 -0
- package/agent-spine-plugin/src/lib/learning-validation-contracts.js +375 -0
- package/agent-spine-plugin/src/lib/learning-validation-renewal.js +298 -0
- package/agent-spine-plugin/src/lib/learning-validation-runtime.js +234 -0
- package/agent-spine-plugin/src/lib/learning.js +36 -6923
- package/agent-spine-plugin/src/lib/lesson-recall-session.js +172 -0
- package/agent-spine-plugin/src/lib/mcp-autonomy-tools.js +37 -0
- package/agent-spine-plugin/src/lib/mcp-delivery-completion.js +124 -0
- package/agent-spine-plugin/src/lib/mcp-delivery-tools.js +45 -0
- package/agent-spine-plugin/src/lib/mcp-premortem.js +68 -0
- package/agent-spine-plugin/src/lib/mcp-runtime.js +269 -0
- package/agent-spine-plugin/src/lib/mcp-source-context.js +51 -0
- package/agent-spine-plugin/src/lib/mcp-timeline-tools.js +164 -0
- package/agent-spine-plugin/src/lib/mcp-world-tools.js +52 -0
- package/agent-spine-plugin/src/lib/owned-file-lock.js +30 -10
- package/agent-spine-plugin/src/lib/project-portfolio.js +176 -0
- package/agent-spine-plugin/src/lib/selfstarter-core.js +382 -0
- package/agent-spine-plugin/src/lib/selfstarter-jobs.js +149 -0
- package/agent-spine-plugin/src/lib/selfstarter-lease.js +204 -0
- package/agent-spine-plugin/src/lib/selfstarter-policy.js +90 -0
- package/agent-spine-plugin/src/lib/selfstarter-workspace.js +111 -0
- package/agent-spine-plugin/src/lib/selfstarter.js +11 -911
- package/agent-spine-plugin/src/lib/session-timeline-auth.js +316 -0
- package/agent-spine-plugin/src/lib/session-timeline-codex.js +58 -0
- package/agent-spine-plugin/src/lib/session-timeline-contract.js +48 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-source.js +41 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-storage.js +132 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-transport.js +17 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment.js +500 -0
- package/agent-spine-plugin/src/lib/session-timeline-event-extract.js +157 -0
- package/agent-spine-plugin/src/lib/session-timeline-host-origin.js +74 -0
- package/agent-spine-plugin/src/lib/session-timeline-host-receipt.js +117 -0
- package/agent-spine-plugin/src/lib/session-timeline-invocation.js +201 -0
- package/agent-spine-plugin/src/lib/session-timeline-king.js +79 -0
- package/agent-spine-plugin/src/lib/session-timeline-prior.js +59 -0
- package/agent-spine-plugin/src/lib/session-timeline-provider.js +34 -0
- package/agent-spine-plugin/src/lib/session-timeline-query.js +55 -0
- package/agent-spine-plugin/src/lib/session-timeline-results.js +31 -0
- package/agent-spine-plugin/src/lib/session-timeline-root.js +11 -0
- package/agent-spine-plugin/src/lib/session-timeline-search.js +82 -0
- package/agent-spine-plugin/src/lib/session-timeline-sid-acl.js +217 -0
- package/agent-spine-plugin/src/lib/session-timeline-source.js +83 -0
- package/agent-spine-plugin/src/lib/session-timeline-state.js +45 -0
- package/agent-spine-plugin/src/lib/session-timeline-transport.js +50 -0
- package/agent-spine-plugin/src/lib/session-timeline-windows-acl.js +148 -0
- package/agent-spine-plugin/src/lib/session-timeline.js +453 -0
- package/agent-spine-plugin/src/lib/source-roots.js +69 -136
- package/agent-spine-plugin/src/lib/source-tree-scan.js +178 -0
- package/agent-spine-plugin/src/lib/task-knowledge-context.js +78 -0
- package/agent-spine-plugin/src/lib/timeline-tool-guard.js +202 -0
- package/agent-spine-plugin/src/lib/world-knowledge.js +249 -0
- package/agent-spine-plugin/src/lib/world-model.js +278 -0
- package/agent-spine-plugin/src/mcp.js +20 -161
- package/agent-spine-plugin/src/version.js +1 -1
- package/agent-spine-plugin/src/worker.js +22 -5
- package/bin/agent-resume-snapshot.cjs +2 -2
- package/bin/agentspine-king-goal-inbox.mjs +111 -0
- package/bin/agentspine-king-goal-intake.mjs +106 -0
- package/bin/baseline-skill-performance-policy.cjs +1 -16
- package/bin/core-bootstrap.js +2 -0
- package/bin/curiosity-scout-policy.cjs +5 -1
- package/bin/input-draft-persistence.cjs +2 -2
- package/bin/king-tui-function-contract.json +33 -0
- package/bin/launcher-restart-policy.cjs +150 -0
- package/bin/launcher-runtime.js +56 -44
- package/bin/managed-context-startup-policy.cjs +27 -0
- package/bin/managed-plugin-selection.cjs +116 -0
- package/bin/mistake-relevance-policy.cjs +1 -1
- package/bin/observer-hooks.cjs +14 -0
- package/bin/oversized-context-offload-policy.cjs +86 -0
- package/bin/plugin-bootstrap.js +7 -40
- package/bin/proactive-compaction-policy.cjs +1 -1
- package/bin/provider-model-refresh-deadline.cjs +53 -0
- package/bin/provider-model-refresh-policy.cjs +107 -0
- package/bin/release-artifact-freeze-policy.cjs +30 -0
- package/bin/repeated-user-message-projection.cjs +3 -126
- package/bin/research-page-result.cjs +74 -0
- package/bin/runtime-exit-ledger.cjs +1 -0
- package/bin/session-compaction-policy.cjs +84 -0
- package/bin/skill-listing-performance-policy.cjs +2 -2
- package/bin/standard-tools-bootstrap.js +0 -37
- package/bin/subagent-skill-policy.cjs +3 -1
- package/bin/telegram-approval-relay.cjs +12 -7
- package/bin/telegram-private-conversation-policy.cjs +3 -2
- package/bin/telegram-queue-handoff-policy.cjs +24 -0
- package/bin/thinking-activity-status-policy.cjs +1 -1
- package/bin/thinking-only-guard.cjs +16 -12
- package/bin/tool-call-loop-policy.cjs +0 -2
- package/bin/tool-result-offload-policy.cjs +11 -2
- package/bin/tui-functional-contract.cjs +55 -0
- package/bin/update-notice.js +18 -14
- package/bin/user-message-offload-policy.cjs +1 -1
- package/bin/user-prompt-hook-origin-policy.cjs +34 -0
- package/bin/windows-node-crash-dump.cjs +110 -0
- package/blun.mjs +2517 -1048
- package/codebase-index/codebase_index.py +4 -3
- package/package.json +3 -17
- package/standard-skills/research-evidence/SKILL.md +39 -0
- package/standard-skills/research-evidence/references/evidence-format.md +82 -0
- package/standard-skills/research-evidence/scripts/evidence-collection.cjs +254 -0
- package/standard-skills/research-evidence/scripts/score-report.cjs +112 -0
- package/standard-skills/web-lesen/SKILL.md +37 -22
- package/standard-skills/web-lesen/scripts/crawl_public.py +376 -0
- package/telegram-plugin/DELIVERY.md +36 -0
- package/telegram-plugin/bin/telegram-approval-relay.cjs +13 -7
- package/telegram-plugin/bin/telegram-launcher-status-queue.cjs +122 -0
- package/telegram-plugin/bin/telegram-private-conversation-policy.cjs +3 -2
- package/telegram-plugin/bin/telegram-reply-parts.cjs +149 -0
- package/telegram-plugin/dist/bridge.mjs +7 -56
- package/telegram-plugin/dist/mcp-server.mjs +33 -4
- package/agent-spine-plugin/skill/SKILL.md +0 -76
- package/bin/mnemo-connect-heartbeat.cjs +0 -204
- package/bin/mnemo-tool-agent-policy.cjs +0 -22
- package/telegram-plugin/bin/telegram-mnemo-capture.cjs +0 -297
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('node:fs');
|
|
4
|
-
const path = require('node:path');
|
|
5
|
-
|
|
6
|
-
const DEFAULT_TIMEOUT_MS = 5_000;
|
|
7
|
-
const DEFAULT_RETRY_MS = 15_000;
|
|
8
|
-
|
|
9
|
-
function cleanString(value) {
|
|
10
|
-
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function cleanHubUrl(value) {
|
|
14
|
-
const raw = cleanString(value);
|
|
15
|
-
if (!raw) return null;
|
|
16
|
-
try {
|
|
17
|
-
const url = new URL(raw);
|
|
18
|
-
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) return null;
|
|
19
|
-
url.pathname = url.pathname.replace(/\/+$/u, '');
|
|
20
|
-
url.search = '';
|
|
21
|
-
url.hash = '';
|
|
22
|
-
return url.toString().replace(/\/$/u, '');
|
|
23
|
-
} catch {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function readProfileMnemoConfig(profileHome) {
|
|
29
|
-
const root = cleanString(profileHome);
|
|
30
|
-
if (!root) return null;
|
|
31
|
-
try {
|
|
32
|
-
const file = path.join(root, 'mcp.json');
|
|
33
|
-
const stat = fs.statSync(file);
|
|
34
|
-
if (!stat.isFile() || stat.size > 1024 * 1024) return null;
|
|
35
|
-
const config = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
36
|
-
const server = config?.mcpServers?.mnemo;
|
|
37
|
-
if (!server || server.enabled === false) return null;
|
|
38
|
-
const env = server.env && typeof server.env === 'object' ? server.env : {};
|
|
39
|
-
const agentName = cleanString(env.BLUN_MNEMO_AGENT) || cleanString(env.MNEMO_AGENT);
|
|
40
|
-
if (!agentName || /[\u0000-\u001f\u007f]/u.test(agentName) || agentName.length > 128) return null;
|
|
41
|
-
const configuredUrl = cleanString(env.BLUN_MNEMO_HUB_URL)
|
|
42
|
-
|| cleanString(env.MNEMO_HUB_URL)
|
|
43
|
-
|| cleanString(env.MNEMO_URL);
|
|
44
|
-
const baseUrl = configuredUrl ? cleanHubUrl(configuredUrl) : null;
|
|
45
|
-
return baseUrl ? { agentName, baseUrl } : null;
|
|
46
|
-
} catch {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function resolveCaptureConfig(options = {}) {
|
|
52
|
-
const env = options.env || process.env;
|
|
53
|
-
const profile = readProfileMnemoConfig(options.profileHome || env.BLUN_HOME);
|
|
54
|
-
const configuredUrl = options.baseUrl
|
|
55
|
-
|| env.BLUN_MNEMO_HUB_URL
|
|
56
|
-
|| env.MNEMO_HUB_URL
|
|
57
|
-
|| env.MNEMO_URL
|
|
58
|
-
|| profile?.baseUrl;
|
|
59
|
-
const baseUrl = cleanHubUrl(configuredUrl);
|
|
60
|
-
const agentName = cleanString(options.agentName)
|
|
61
|
-
|| cleanString(env.BLUN_MNEMO_AGENT)
|
|
62
|
-
|| cleanString(env.MNEMO_AGENT)
|
|
63
|
-
|| profile?.agentName;
|
|
64
|
-
return baseUrl && agentName ? { baseUrl, agentName } : null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function isLoopOnlyMessage(message) {
|
|
68
|
-
const text = String(message?.text || '').trim();
|
|
69
|
-
const eventKind = String(message?.meta?.event_kind || '').trim();
|
|
70
|
-
return /^\/loop(?:@[A-Za-z0-9_]+)?(?:\s|$)/iu.test(text)
|
|
71
|
-
|| /^(?:loop_tick|loop_nudge|loop_status|runtime_loop)$/iu.test(eventKind);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function booleanMeta(value) {
|
|
75
|
-
if (value === true || value === 'true') return true;
|
|
76
|
-
if (value === false || value === 'false') return false;
|
|
77
|
-
return undefined;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function buildTelegramCapturePayload(message, options = {}) {
|
|
81
|
-
if (!message || isLoopOnlyMessage(message)) return null;
|
|
82
|
-
const meta = message.meta && typeof message.meta === 'object' ? message.meta : {};
|
|
83
|
-
const chatId = cleanString(meta.chat_id);
|
|
84
|
-
const messageId = cleanString(meta.message_id);
|
|
85
|
-
if (!chatId || !messageId) return null;
|
|
86
|
-
const text = typeof message.text === 'string' ? message.text : '';
|
|
87
|
-
const actor = cleanString(meta.user) || cleanString(meta.user_id) || 'telegram-user';
|
|
88
|
-
const actorId = cleanString(meta.user_id);
|
|
89
|
-
const chatType = cleanString(meta.chat_type);
|
|
90
|
-
const topic = cleanString(meta.telegram_thread_id) || 'root';
|
|
91
|
-
const agentName = cleanString(options.agentName);
|
|
92
|
-
const privateScope = chatType === 'private' && agentName ? `${agentName}:` : '';
|
|
93
|
-
const refId = `${privateScope}${chatId}:${messageId}`;
|
|
94
|
-
const channelScope = chatType === 'private' && agentName ? `${agentName}:${chatId}` : chatId;
|
|
95
|
-
const captureMeta = {
|
|
96
|
-
chat_id: chatId,
|
|
97
|
-
message_id: messageId,
|
|
98
|
-
telegram_thread_id: topic,
|
|
99
|
-
...(chatType ? { chat_type: chatType } : {}),
|
|
100
|
-
...(cleanString(meta.chat_title) ? { chat_title: cleanString(meta.chat_title) } : {}),
|
|
101
|
-
...(actorId ? { user_id: actorId } : {}),
|
|
102
|
-
...(booleanMeta(meta.is_bot) === undefined ? {} : { is_bot: booleanMeta(meta.is_bot) }),
|
|
103
|
-
...(agentName ? { recipient_agent: agentName } : {}),
|
|
104
|
-
};
|
|
105
|
-
return {
|
|
106
|
-
source: 'telegram',
|
|
107
|
-
channel: `telegram-chat:${channelScope}`,
|
|
108
|
-
direction: 'inbound',
|
|
109
|
-
actor,
|
|
110
|
-
...(actorId ? { actor_id: actorId } : {}),
|
|
111
|
-
speaker: actor,
|
|
112
|
-
event_kind: 'telegram_message',
|
|
113
|
-
dedupe_key: `telegram:${refId}`,
|
|
114
|
-
ref_kind: 'telegram_message',
|
|
115
|
-
ref_id: refId,
|
|
116
|
-
thread_id: `${channelScope}:${topic}`,
|
|
117
|
-
...(cleanString(meta.ts) ? { occurred_at: cleanString(meta.ts) } : {}),
|
|
118
|
-
content: text,
|
|
119
|
-
promote_transcript: true,
|
|
120
|
-
promote_memory: false,
|
|
121
|
-
meta: captureMeta,
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async function postMnemoTool(baseUrl, tool, payload, options = {}) {
|
|
126
|
-
const controller = new AbortController();
|
|
127
|
-
const timer = (options.setTimeoutImpl || setTimeout)(
|
|
128
|
-
() => controller.abort(),
|
|
129
|
-
options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
130
|
-
);
|
|
131
|
-
timer.unref?.();
|
|
132
|
-
try {
|
|
133
|
-
const response = await (options.fetchImpl || fetch)(
|
|
134
|
-
`${baseUrl}/tool/${encodeURIComponent(tool)}`,
|
|
135
|
-
{
|
|
136
|
-
method: 'POST',
|
|
137
|
-
headers: { 'content-type': 'application/json' },
|
|
138
|
-
body: JSON.stringify(payload),
|
|
139
|
-
signal: controller.signal,
|
|
140
|
-
},
|
|
141
|
-
);
|
|
142
|
-
if (!response.ok) throw new Error(`Mnemo ${tool} HTTP ${response.status}`);
|
|
143
|
-
const body = await response.json();
|
|
144
|
-
if (body && body.error) throw new Error(String(body.error));
|
|
145
|
-
return body;
|
|
146
|
-
} finally {
|
|
147
|
-
(options.clearTimeoutImpl || clearTimeout)(timer);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function readCheckpoint(file) {
|
|
152
|
-
try {
|
|
153
|
-
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
154
|
-
return Number.isSafeInteger(parsed.offset) && parsed.offset >= 0 ? parsed.offset : 0;
|
|
155
|
-
} catch {
|
|
156
|
-
return 0;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function readPending(queueFile, offset) {
|
|
161
|
-
try {
|
|
162
|
-
const bytes = fs.readFileSync(queueFile);
|
|
163
|
-
if (offset < 0 || offset > bytes.length) offset = 0;
|
|
164
|
-
const pending = [];
|
|
165
|
-
let cursor = offset;
|
|
166
|
-
while (cursor < bytes.length) {
|
|
167
|
-
const newline = bytes.indexOf(10, cursor);
|
|
168
|
-
if (newline < 0) break;
|
|
169
|
-
const endOffset = newline + 1;
|
|
170
|
-
const line = bytes.subarray(cursor, newline).toString('utf8').trim();
|
|
171
|
-
cursor = endOffset;
|
|
172
|
-
if (!line) continue;
|
|
173
|
-
try {
|
|
174
|
-
const parsed = JSON.parse(line);
|
|
175
|
-
if (parsed && typeof parsed.payload === 'object') pending.push({
|
|
176
|
-
payload: parsed.payload,
|
|
177
|
-
endOffset,
|
|
178
|
-
});
|
|
179
|
-
} catch {
|
|
180
|
-
// A malformed historical row is skipped; later valid receipts still drain.
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return pending;
|
|
184
|
-
} catch {
|
|
185
|
-
return [];
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function writeCheckpoint(file, offset) {
|
|
190
|
-
const temporary = `${file}.${process.pid}.tmp`;
|
|
191
|
-
fs.writeFileSync(temporary, `${JSON.stringify({ version: 1, offset })}\n`, { mode: 0o600 });
|
|
192
|
-
fs.renameSync(temporary, file);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function createTelegramMnemoCapture(options = {}) {
|
|
196
|
-
const config = resolveCaptureConfig(options);
|
|
197
|
-
if (!config) {
|
|
198
|
-
return Object.freeze({
|
|
199
|
-
enabled: false,
|
|
200
|
-
enqueue: () => false,
|
|
201
|
-
flush: async () => true,
|
|
202
|
-
pendingCount: () => 0,
|
|
203
|
-
stop: async () => {},
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const dir = path.resolve(options.stateDir || path.join(process.cwd(), '.telegram'));
|
|
208
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
209
|
-
const queueFile = path.join(dir, 'mnemo-capture-queue.jsonl');
|
|
210
|
-
const checkpointFile = path.join(dir, 'mnemo-capture.checkpoint.json');
|
|
211
|
-
if (!fs.existsSync(queueFile)) fs.writeFileSync(queueFile, '', { mode: 0o600 });
|
|
212
|
-
let checkpointOffset = readCheckpoint(checkpointFile);
|
|
213
|
-
let pending = readPending(queueFile, checkpointOffset);
|
|
214
|
-
const pendingKeys = new Set(pending.map((item) => item.payload.dedupe_key));
|
|
215
|
-
const callTool = options.callTool
|
|
216
|
-
|| ((tool, payload) => postMnemoTool(config.baseUrl, tool, payload, options));
|
|
217
|
-
const retryMs = options.retryMs ?? DEFAULT_RETRY_MS;
|
|
218
|
-
const onError = typeof options.onError === 'function' ? options.onError : () => {};
|
|
219
|
-
let stopped = false;
|
|
220
|
-
let activeFlush = null;
|
|
221
|
-
let retryTimer = null;
|
|
222
|
-
|
|
223
|
-
const schedule = (delay = 0) => {
|
|
224
|
-
if (stopped || options.autoStart === false || retryTimer !== null || pending.length === 0) return;
|
|
225
|
-
retryTimer = setTimeout(() => {
|
|
226
|
-
retryTimer = null;
|
|
227
|
-
void flush();
|
|
228
|
-
}, delay);
|
|
229
|
-
retryTimer.unref?.();
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
const flush = async () => {
|
|
233
|
-
if (activeFlush) return activeFlush;
|
|
234
|
-
activeFlush = (async () => {
|
|
235
|
-
while (!stopped && pending.length > 0) {
|
|
236
|
-
const item = pending[0];
|
|
237
|
-
try {
|
|
238
|
-
await callTool('mem_capture_ingest', item.payload);
|
|
239
|
-
} catch (error) {
|
|
240
|
-
onError(error);
|
|
241
|
-
schedule(retryMs);
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
pending.shift();
|
|
245
|
-
pendingKeys.delete(item.payload.dedupe_key);
|
|
246
|
-
checkpointOffset = item.endOffset;
|
|
247
|
-
writeCheckpoint(checkpointFile, checkpointOffset);
|
|
248
|
-
}
|
|
249
|
-
return pending.length === 0;
|
|
250
|
-
})().finally(() => {
|
|
251
|
-
activeFlush = null;
|
|
252
|
-
});
|
|
253
|
-
return activeFlush;
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
const enqueue = (message) => {
|
|
257
|
-
if (stopped) return false;
|
|
258
|
-
const payload = buildTelegramCapturePayload(message, { agentName: config.agentName });
|
|
259
|
-
if (!payload || pendingKeys.has(payload.dedupe_key)) return false;
|
|
260
|
-
const line = `${JSON.stringify({ version: 1, payload })}\n`;
|
|
261
|
-
const startOffset = fs.statSync(queueFile).size;
|
|
262
|
-
fs.appendFileSync(queueFile, line, { encoding: 'utf8', mode: 0o600 });
|
|
263
|
-
const item = {
|
|
264
|
-
payload,
|
|
265
|
-
endOffset: startOffset + Buffer.byteLength(line),
|
|
266
|
-
};
|
|
267
|
-
pending.push(item);
|
|
268
|
-
pendingKeys.add(payload.dedupe_key);
|
|
269
|
-
schedule();
|
|
270
|
-
return true;
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
if (pending.length > 0) schedule();
|
|
274
|
-
return Object.freeze({
|
|
275
|
-
enabled: true,
|
|
276
|
-
agentName: config.agentName,
|
|
277
|
-
baseUrl: config.baseUrl,
|
|
278
|
-
enqueue,
|
|
279
|
-
flush,
|
|
280
|
-
pendingCount: () => pending.length,
|
|
281
|
-
async stop() {
|
|
282
|
-
stopped = true;
|
|
283
|
-
if (retryTimer !== null) clearTimeout(retryTimer);
|
|
284
|
-
retryTimer = null;
|
|
285
|
-
if (activeFlush) await activeFlush;
|
|
286
|
-
},
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
module.exports = {
|
|
291
|
-
buildTelegramCapturePayload,
|
|
292
|
-
createTelegramMnemoCapture,
|
|
293
|
-
isLoopOnlyMessage,
|
|
294
|
-
postMnemoTool,
|
|
295
|
-
readProfileMnemoConfig,
|
|
296
|
-
resolveCaptureConfig,
|
|
297
|
-
};
|