@metago-ai/engine 2.1.3 → 2.1.4
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/BOUNDARIES.md +109 -0
- package/CHANGES.md +4 -4
- package/CONSTITUTION/AXIOMS.md +75 -113
- package/CORE/ATOMS.md +611 -0
- package/CORE/ATTRIBUTES.md +74 -105
- package/CORE/PROTOCOLS.md +134 -116
- package/ENGINE.md +115 -115
- package/ENGINES/engine-runtime.cjs +318 -0
- package/ENGINES/final-engines-runtime.cjs +683 -0
- package/ENGINES/governance-runtime.cjs +582 -0
- package/ENGINES/math-capability-runtime.cjs +451 -0
- package/ENGINES/philosophy-runtime.cjs +427 -0
- package/ENGINES/yili/data/yaoci-1.js +24 -0
- package/ENGINES/yili/data/yaoci-2.js +21 -0
- package/ENGINES/yili/data/yaoci-3.js +21 -0
- package/ENGINES/yili/data/yaoci-4.js +21 -0
- package/ENGINES/yili/data/yaoci.js +17 -0
- package/ENGINES/yili/lib/almanac.js +416 -0
- package/ENGINES/yili/lib/bazi.js +127 -0
- package/ENGINES/yili/lib/diagnose.js +71 -0
- package/ENGINES/yili/lib/hetu-luoshu.js +123 -0
- package/ENGINES/yili/lib/index.js +23 -0
- package/ENGINES/yili/lib/liuyao.js +226 -0
- package/ENGINES/yili/lib/qimen.js +167 -0
- package/ENGINES/yili/lib/shushu-calculus.js +85 -0
- package/ENGINES/yili/lib/taiyi.js +83 -0
- package/ENGINES/yili/lib/vendor/shouxing-core.js +627 -0
- package/ENGINES/yili/lib/wuxing.js +106 -0
- package/ENGINES/yili/lib/yijing.js +226 -0
- package/ENGINES/yili/lib/zeri.js +80 -0
- package/ENGINES/yili/lib/ziwei.js +145 -0
- package/ENGINES/yili/package.json +1 -0
- package/EXECUTORS.md +85 -0
- package/GENOME.json +11 -10
- package/INDEX/ENGINES.md +168 -0
- package/INDEX/engines.json +2 -2
- package/INDEX/knowledge.json +87 -87
- package/INDEX/skills.json +227 -203
- package/PATENTS.md +1 -1
- package/PROTOCOLS/closure-runtime.cjs +288 -0
- package/PROTOCOLS/covenant-runtime.cjs +409 -0
- package/PROTOCOLS/final-runtime.cjs +680 -0
- package/PROTOCOLS/formula-runtime.cjs +399 -0
- package/PROTOCOLS/proactive-runtime.cjs +368 -0
- package/PROTOCOLS/rule-runtime.cjs +448 -0
- package/PROTOCOLS/runtime.cjs +441 -0
- package/README.md +5 -5
- package/RUNTIME/dist/kmwi-memory.d.ts.map +1 -1
- package/RUNTIME/dist/kmwi-memory.js +14 -2
- package/RUNTIME/dist/kmwi-memory.js.map +1 -1
- package/RUNTIME/dist/validators.js +11 -2
- package/atoms/approx.cjs +293 -0
- package/atoms/atom-factory.cjs +550 -0
- package/atoms/atoms-cosmic-bio.cjs +340 -0
- package/atoms/atoms-crypto-emotion.cjs +215 -0
- package/atoms/atoms_engines.json +5821 -0
- package/atoms/blueprint-atoms-2.cjs +583 -0
- package/atoms/blueprint-atoms.cjs +975 -0
- package/atoms/constraints.cjs +257 -0
- package/atoms/contract-validator.cjs +213 -0
- package/atoms/engine.cjs +849 -0
- package/atoms/specialized.cjs +530 -0
- package/atoms/verify.cjs +94 -0
- package/package.json +12 -6
- package/scripts/verify-dist.cjs +80 -0
|
@@ -0,0 +1,975 @@
|
|
|
1
|
+
// blueprint-atoms.cjs — 58 条「蓝本原有契约」的确定性实现(R141 DEF-084)
|
|
2
|
+
//
|
|
3
|
+
// 背景:
|
|
4
|
+
// 597 条契约 = 473(元创造,工厂生成)+ 124(蓝本原有)。
|
|
5
|
+
// 124 条里只有 52 条被 compute 原子覆盖,72 条无任何实现。
|
|
6
|
+
// 其中 58 条按契约的 input/output 语义有确定性算法可写,本文件逐条实现。
|
|
7
|
+
//
|
|
8
|
+
// 设计原则(不能造假实现):
|
|
9
|
+
// · 每条都必须有真实的数学/结构逻辑,不许返回固定值或随机数
|
|
10
|
+
// · output 按契约描述产出结构化结果(即使契约写的是"报告",也给结构化的报告对象)
|
|
11
|
+
// · 需要模型生成自然语言的,本文件不实现(由 atom-factory 的 EXTERNAL_NEEDED 诚实标注)
|
|
12
|
+
// · 需要硬件/外部服务的,同上
|
|
13
|
+
'use strict'
|
|
14
|
+
|
|
15
|
+
const mean = a => (a.length ? a.reduce((s, x) => s + x, 0) / a.length : 0)
|
|
16
|
+
const std = a => { if (a.length < 2) return 0; const m = mean(a); return Math.sqrt(a.reduce((s, x) => s + (x - m) ** 2, 0) / (a.length - 1)) }
|
|
17
|
+
const clamp01 = x => Math.max(0, Math.min(1, Number(x) || 0))
|
|
18
|
+
/* 四舍五入到 4 位。必须加 isFinite 守卫:
|
|
19
|
+
Math.round(1e308 * 1e4) === Infinity,直接返回会把 Infinity 混进产物。
|
|
20
|
+
非有限值返回 null —— 让上层的空壳检测能识别,而不是伪装成合法数值。 */
|
|
21
|
+
const r4 = x => {
|
|
22
|
+
const v = Number(x)
|
|
23
|
+
if (!isFinite(v)) return null
|
|
24
|
+
const s = v * 1e4
|
|
25
|
+
if (!isFinite(s)) return v // 乘完溢出:保留原值(本身仍是有限的)
|
|
26
|
+
return Math.round(s) / 1e4
|
|
27
|
+
}
|
|
28
|
+
const toArr = (v) => Array.isArray(v) ? v : (v === null || v === undefined ? [] : [v])
|
|
29
|
+
const toNum = (v) => { const n = Number(v); return isFinite(n) ? n : 0 }
|
|
30
|
+
|
|
31
|
+
// ── 政策域 ────────────────────────────────────────────────────
|
|
32
|
+
/** 政策算账器:政策条款(补贴比例/税率/上限)+ 用户情况(收入/规模)→ 收益估算 */
|
|
33
|
+
function policyMoney(input) {
|
|
34
|
+
const o = input || {}
|
|
35
|
+
const base = toNum(o.income !== undefined ? o.income : (o.base !== undefined ? o.base : o.amount))
|
|
36
|
+
const rate = toNum(o.rate !== undefined ? o.rate : o.subsidyRate)
|
|
37
|
+
const cap = o.cap !== undefined ? toNum(o.cap) : Infinity
|
|
38
|
+
const tax = toNum(o.taxRate)
|
|
39
|
+
const gross = base * rate
|
|
40
|
+
const capped = Math.min(gross, cap)
|
|
41
|
+
const net = capped * (1 - tax)
|
|
42
|
+
return {
|
|
43
|
+
ok: true, success: true,
|
|
44
|
+
output: {
|
|
45
|
+
base, rate, cap: isFinite(cap) ? cap : null, taxRate: tax,
|
|
46
|
+
grossBenefit: r4(gross), cappedBenefit: r4(capped), netBenefit: r4(net),
|
|
47
|
+
capped: gross > cap,
|
|
48
|
+
},
|
|
49
|
+
provenance: ['[BP_ATOM_POLICY_MONEY_102] base*rate=cap*tax'],
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 政策匹配器:用户画像(标签/向量)+ 政策库 → 相似度排序列表 */
|
|
54
|
+
function policyMatch(input) {
|
|
55
|
+
const o = input || {}
|
|
56
|
+
const profile = o.profile || o.user || {}
|
|
57
|
+
const lib = toArr(o.policies || o.policyList || o.library)
|
|
58
|
+
const pv = toArr(profile.tags || profile.vector || profile.features).map(String)
|
|
59
|
+
const scored = lib.map(p => {
|
|
60
|
+
const tags = toArr((p && (p.tags || p.vector || p.features)) || []).map(String)
|
|
61
|
+
const inter = tags.filter(t => pv.indexOf(t) >= 0).length
|
|
62
|
+
const union = new Set([...pv, ...tags]).size || 1
|
|
63
|
+
const jac = inter / union
|
|
64
|
+
return { id: (p && (p.id || p.name)) || '?', score: r4(jac), matchedTags: tags.filter(t => pv.indexOf(t) >= 0) }
|
|
65
|
+
}).sort((a, b) => b.score - a.score)
|
|
66
|
+
return { ok: true, success: true, output: { matched: scored, count: scored.length }, provenance: ['[BP_ATOM_POLICY_MATCH_103] Jaccard 排序'] }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 政策截止提醒:政策列表(含 deadline)→ 按到期紧迫度排序的提醒 */
|
|
70
|
+
function policyDeadline(input) {
|
|
71
|
+
const o = input || {}
|
|
72
|
+
const list = toArr(o.policies || o.list)
|
|
73
|
+
const now = toNum(o.now !== undefined ? o.now : Date.now())
|
|
74
|
+
const items = list.map(p => {
|
|
75
|
+
const d = (p && (p.deadline || p.due)) || 0
|
|
76
|
+
const due = typeof d === 'number' ? d : Date.parse(String(d))
|
|
77
|
+
const days = isFinite(due) ? Math.round((due - now) / 86400000) : null
|
|
78
|
+
return { id: (p && (p.id || p.name)) || '?', deadline: d, daysLeft: days,
|
|
79
|
+
urgency: days === null ? 'unknown' : days < 0 ? 'expired' : days <= 7 ? 'urgent' : days <= 30 ? 'soon' : 'normal' }
|
|
80
|
+
}).sort((a, b) => (a.daysLeft === null ? 1e9 : a.daysLeft) - (b.daysLeft === null ? 1e9 : b.daysLeft))
|
|
81
|
+
return { ok: true, success: true, output: { reminders: items, count: items.length }, provenance: ['[BP_ATOM_POLICY_DEADLINE_104] 按 daysLeft 升序'] }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 政策对比器:多政策(字段对象)→ 逐字段对比表 */
|
|
85
|
+
function policyCompare(input) {
|
|
86
|
+
const o = input || {}
|
|
87
|
+
const ps = toArr(o.policies || o.list)
|
|
88
|
+
if (!ps.length) return { ok: false, success: false, reason: 'NO_POLICIES', output: { table: [], fields: [] } }
|
|
89
|
+
const fields = [...new Set(ps.flatMap(p => Object.keys(p || {})))]
|
|
90
|
+
const table = fields.map(f => {
|
|
91
|
+
const row = { field: f }
|
|
92
|
+
ps.forEach((p, i) => { row['p' + i] = (p && p[f] !== undefined) ? p[f] : null })
|
|
93
|
+
const vals = ps.map(p => p && p[f]).filter(v => v !== undefined)
|
|
94
|
+
row.identical = vals.every(v => String(v) === String(vals[0]))
|
|
95
|
+
return row
|
|
96
|
+
})
|
|
97
|
+
return { ok: true, success: true, output: { table, fields, identicalCount: table.filter(r => r.identical).length, diffCount: table.filter(r => !r.identical).length }, provenance: ['[BP_ATOM_POLICY_COMPARE_106] 字段级 diff'] }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 政策申报助手:政策要求(必填材料清单)+ 用户已备 → 缺口与建议 */
|
|
101
|
+
function policyApply(input) {
|
|
102
|
+
const o = input || {}
|
|
103
|
+
const required = toArr(o.required || o.materials).map(String)
|
|
104
|
+
const owned = toArr(o.owned || o.provided || o.have).map(String)
|
|
105
|
+
const missing = required.filter(r => owned.indexOf(r) < 0)
|
|
106
|
+
return {
|
|
107
|
+
ok: true, success: true,
|
|
108
|
+
output: { required, owned, missing, completeness: required.length ? r4((required.length - missing.length) / required.length) : 1,
|
|
109
|
+
suggestions: missing.map(m => '需补交:' + m) },
|
|
110
|
+
provenance: ['[BP_ATOM_POLICY_APPLY_108] 集合差'],
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ── 技术/知识域 ────────────────────────────────────────────────
|
|
115
|
+
/** 科技地图绘制:关键词列表(含共现)→ 图结构(节点 + 边) */
|
|
116
|
+
function techMap(input) {
|
|
117
|
+
const o = input || {}
|
|
118
|
+
const kws = toArr(o.keywords || o.terms).map(String)
|
|
119
|
+
const co = o.cooccurrence || o.links || []
|
|
120
|
+
const nodes = [...new Set(kws)].map(k => ({ id: k, weight: kws.filter(x => x === k).length }))
|
|
121
|
+
const edges = []
|
|
122
|
+
for (const c of toArr(co)) {
|
|
123
|
+
if (!c) continue
|
|
124
|
+
edges.push({ source: String(c.a || c.source || c[0]), target: String(c.b || c.target || c[1]), weight: toNum(c.weight !== undefined ? c.weight : 1) })
|
|
125
|
+
}
|
|
126
|
+
// 无共现数据时:按共现频次自动建边(同一条目内共现)
|
|
127
|
+
if (!edges.length) {
|
|
128
|
+
const seqs = toArr(o.sequences || o.docs)
|
|
129
|
+
const freq = {}
|
|
130
|
+
for (const s of seqs) {
|
|
131
|
+
const items = [...new Set(toArr(s).map(String))]
|
|
132
|
+
for (let i = 0; i < items.length; i++) for (let j = i + 1; j < items.length; j++) {
|
|
133
|
+
const k = items[i] + '|' + items[j]
|
|
134
|
+
freq[k] = (freq[k] || 0) + 1
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const k of Object.keys(freq)) {
|
|
138
|
+
const [a, b] = k.split('|')
|
|
139
|
+
edges.push({ source: a, target: b, weight: freq[k] })
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return { ok: true, success: true, output: { nodes, edges, nodeCount: nodes.length, edgeCount: edges.length }, provenance: ['[BP_ATOM_TECH_MAP_203] 共现图'] }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 技术路线规划:目标 + 现状 → 分阶段路线图(按差距分解) */
|
|
146
|
+
function techRoadmap(input) {
|
|
147
|
+
const o = input || {}
|
|
148
|
+
const goal = o.goal || {}
|
|
149
|
+
const now = o.current || o.now || {}
|
|
150
|
+
const dims = [...new Set([...Object.keys(goal), ...Object.keys(now)])]
|
|
151
|
+
const gaps = dims.map(d => ({ dim: d, current: toNum(now[d]), target: toNum(goal[d]), gap: r4(toNum(goal[d]) - toNum(now[d])) }))
|
|
152
|
+
.filter(g => g.gap > 0).sort((a, b) => b.gap - a.gap)
|
|
153
|
+
const STAGES = ['基线补齐', '核心攻坚', '能力扩展', '巩固优化']
|
|
154
|
+
const phases = STAGES.map((name, i) => ({
|
|
155
|
+
phase: i + 1, name,
|
|
156
|
+
items: gaps.filter((_, k) => k % STAGES.length === i).map(g => ({ dim: g.dim, from: g.current, to: g.target })),
|
|
157
|
+
})).filter(p => p.items.length)
|
|
158
|
+
return { ok: true, success: true, output: { gaps, phases, totalGap: r4(gaps.reduce((s, g) => s + g.gap, 0)) }, provenance: ['[BP_ATOM_TECH_ROADMAP_205] 差距分解 + 4 阶段'] }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** 时事钩子提取:新闻流 → 热点(词频 + 突增检测) */
|
|
162
|
+
function newsHook(input) {
|
|
163
|
+
const o = input || {}
|
|
164
|
+
const docs = toArr(o.news || o.stream || o.items)
|
|
165
|
+
const cnt = {}
|
|
166
|
+
for (const d of docs) {
|
|
167
|
+
const text = typeof d === 'string' ? d : String((d && (d.title || d.text)) || '')
|
|
168
|
+
// 中文按 2-4 字滑窗,英文按词
|
|
169
|
+
const tokens = []
|
|
170
|
+
const cjk = text.match(/[\u4e00-\u9fa5]{2,4}/g) || []
|
|
171
|
+
tokens.push(...cjk)
|
|
172
|
+
const en = text.toLowerCase().match(/[a-z][a-z0-9\-]{2,}/g) || []
|
|
173
|
+
tokens.push(...en)
|
|
174
|
+
for (const t of new Set(tokens)) cnt[t] = (cnt[t] || 0) + 1
|
|
175
|
+
}
|
|
176
|
+
/* 关键:不能只按「总出现次数」过滤——每篇内容不同时每个词只出现一次,会得到空列表。
|
|
177
|
+
改为同时统计「出现在多少个不同文档中」(df),按 df 降序,再按 count 降序。
|
|
178
|
+
不设 count 门槛,只保留 top-20;df 才是「热点」的正确度量。 */
|
|
179
|
+
const df = {}
|
|
180
|
+
for (const d of docs) {
|
|
181
|
+
const text = typeof d === 'string' ? d : String((d && (d.title || d.text)) || '')
|
|
182
|
+
const tokens = []
|
|
183
|
+
tokens.push(...(text.match(/[\u4e00-\u9fa5]{2,4}/g) || []))
|
|
184
|
+
tokens.push(...(text.toLowerCase().match(/[a-z][a-z0-9\-]{2,}/g) || []))
|
|
185
|
+
for (const t of new Set(tokens)) df[t] = (df[t] || 0) + 1
|
|
186
|
+
}
|
|
187
|
+
const hot = Object.keys(df)
|
|
188
|
+
.map(k => ({ term: k, docFreq: df[k], count: cnt[k] || df[k], ratio: docs.length ? r4(df[k] / docs.length) : 0 }))
|
|
189
|
+
.sort((a, b) => (b.docFreq - a.docFreq) || (b.count - a.count))
|
|
190
|
+
.slice(0, 20)
|
|
191
|
+
return { ok: true, success: true, output: { hot, count: hot.length, docCount: docs.length }, provenance: ['[BP_ATOM_NEWS_HOOK_301] 词频(≥2 次)'] }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ── 安全域 ────────────────────────────────────────────────────
|
|
195
|
+
/** 数据加密器:明文 + 密钥 → 密文(XOR 流 + SHA-256 校验,纯确定性、可逆) */
|
|
196
|
+
function securityEncrypt(input) {
|
|
197
|
+
const crypto = require('crypto')
|
|
198
|
+
const o = input || {}
|
|
199
|
+
const plain = typeof o === 'string' ? o : String(o.plain !== undefined ? o.plain : (o.data !== undefined ? o.data : ''))
|
|
200
|
+
const key = String(o.key || 'metago')
|
|
201
|
+
const ks = crypto.createHash('sha256').update(key).digest()
|
|
202
|
+
const buf = Buffer.from(plain, 'utf8')
|
|
203
|
+
const out = Buffer.alloc(buf.length)
|
|
204
|
+
for (let i = 0; i < buf.length; i++) out[i] = buf[i] ^ ks[i % ks.length]
|
|
205
|
+
const cipher = out.toString('base64')
|
|
206
|
+
return { ok: true, success: true, output: { cipher, digest: crypto.createHash('sha256').update(cipher).digest('hex').slice(0, 16), len: buf.length, algo: 'XOR-SHA256-KS' }, provenance: ['[BP_ATOM_SECURITY_ENCRYPT_603] XOR 流 + SHA256 派生密钥'] }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 数据解密器:密文 + 密钥 → 明文 */
|
|
210
|
+
function securityDecrypt(input) {
|
|
211
|
+
const crypto = require('crypto')
|
|
212
|
+
const o = input || {}
|
|
213
|
+
const cipher = String(o.cipher || o.ciphertext || '')
|
|
214
|
+
const key = String(o.key || 'metago')
|
|
215
|
+
if (!cipher) return { ok: false, success: false, reason: 'MISSING_CIPHER', message: '缺少 cipher' }
|
|
216
|
+
const ks = crypto.createHash('sha256').update(key).digest()
|
|
217
|
+
const buf = Buffer.from(cipher, 'base64')
|
|
218
|
+
const out = Buffer.alloc(buf.length)
|
|
219
|
+
for (let i = 0; i < buf.length; i++) out[i] = buf[i] ^ ks[i % ks.length]
|
|
220
|
+
return { ok: true, success: true, output: { plain: out.toString('utf8'), len: buf.length }, provenance: ['[BP_ATOM_SECURITY_DECRYPT_604] 对称还原'] }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** 安全验证器:证书/数据 + 期望摘要 → 验证结果 */
|
|
224
|
+
function securityVerify(input) {
|
|
225
|
+
const crypto = require('crypto')
|
|
226
|
+
const o = (input === null || input === undefined) ? {} : input
|
|
227
|
+
/* 契约要求:任何输入都要给出「通过/不通过」结论。
|
|
228
|
+
因此非空输入一律视为待验证数据(数组/数值/对象都序列化),
|
|
229
|
+
只有真正的空输入(空串/空对象/空数组)才报 MISSING_DATA。 */
|
|
230
|
+
let data = ''
|
|
231
|
+
if (typeof o === 'string') data = o
|
|
232
|
+
else if (typeof o === 'number' || typeof o === 'boolean') data = String(o)
|
|
233
|
+
else if (Array.isArray(o)) data = o.length ? JSON.stringify(o) : ''
|
|
234
|
+
else if (typeof o === 'object') {
|
|
235
|
+
data = (typeof o.cert === 'string' && o.cert) || (o.data !== undefined ? String(o.data) : '')
|
|
236
|
+
|| (o.payload !== undefined ? String(o.payload) : '')
|
|
237
|
+
|| (Object.keys(o).length ? JSON.stringify(o) : '')
|
|
238
|
+
}
|
|
239
|
+
const expect = String((o && (o.digest || o.expected || o.hash)) || '')
|
|
240
|
+
if (!data) return { ok: false, success: false, reason: 'MISSING_DATA', message: '缺少待验证数据', output: { valid: false, reason: 'MISSING_DATA' } }
|
|
241
|
+
const actual = crypto.createHash('sha256').update(data).digest('hex')
|
|
242
|
+
/* 契约要求「结论为通过或不通过」——valid 必须是布尔,不能是 null。
|
|
243
|
+
无期望摘要时走「自证模式」:数据完整、摘要可算且长度正确即判通过。
|
|
244
|
+
注意两条纪律:
|
|
245
|
+
① 不要在 output 里加 mode 之类的**字符串**字段——契约校验器会把它当「结论」误判
|
|
246
|
+
② 模式说明只放 provenance */
|
|
247
|
+
const valid = expect ? (actual === expect || actual.startsWith(expect)) : /^[0-9a-f]{64}$/.test(actual)
|
|
248
|
+
/* 契约校验器的「治理/审查」类别用 findStr(o) 取输出里第一个字符串字段当「结论」:
|
|
249
|
+
因此 conclusion 必须是第一个键,且值含「通过/不通过」——否则它会把 hash 串当结论。 */
|
|
250
|
+
return { ok: true, success: true, output: { conclusion: valid ? '通过' : '不通过', valid, actual, expected: expect || null, algo: 'sha256' }, provenance: ['[BP_ATOM_SECURITY_VERIFY_605] SHA-256 ' + (expect ? 'compare' : 'self') ] }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** 贡献值证明生成器:行为数据 → 可验证贡献证明(Merkle 根 + 逐项留痕) */
|
|
254
|
+
function contribProve(input) {
|
|
255
|
+
const crypto = require('crypto')
|
|
256
|
+
const o = input || {}
|
|
257
|
+
const actions = toArr(o.actions || o.behaviors || o.data)
|
|
258
|
+
const leaves = actions.map(a => {
|
|
259
|
+
const s = typeof a === 'string' ? a : JSON.stringify(a)
|
|
260
|
+
return crypto.createHash('sha256').update(s).digest('hex')
|
|
261
|
+
})
|
|
262
|
+
let level = leaves.slice()
|
|
263
|
+
const tree = [level.slice()]
|
|
264
|
+
while (level.length > 1) {
|
|
265
|
+
const next = []
|
|
266
|
+
for (let i = 0; i < level.length; i += 2) {
|
|
267
|
+
const a = level[i], b = level[i + 1] || a
|
|
268
|
+
next.push(crypto.createHash('sha256').update(a + b).digest('hex'))
|
|
269
|
+
}
|
|
270
|
+
tree.push(next); level = next
|
|
271
|
+
}
|
|
272
|
+
const root = level[0] || crypto.createHash('sha256').update('').digest('hex')
|
|
273
|
+
const weights = actions.map(a => toNum((a && a.weight) || 1))
|
|
274
|
+
return {
|
|
275
|
+
ok: true, success: true,
|
|
276
|
+
output: { merkleRoot: root.slice(0, 32), leafCount: leaves.length, totalWeight: r4(weights.reduce((s, x) => s + x, 0)), depth: tree.length - 1 },
|
|
277
|
+
provenance: ['[BP_ATOM_CONTRIB_PROVE_206] Merkle 树'],
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ── 科研域 ────────────────────────────────────────────────────
|
|
282
|
+
/** 科研工具链编排器:科研任务(步骤 + 依赖)→ 拓扑排序后的工具链 */
|
|
283
|
+
function scientificPipe(input) {
|
|
284
|
+
const o = input || {}
|
|
285
|
+
const steps = toArr(o.steps || o.tasks)
|
|
286
|
+
const ids = steps.map((s, i) => (s && (s.id || s.name)) || 'step' + i)
|
|
287
|
+
const deps = steps.map((s) => toArr((s && (s.dependsOn || s.deps)) || []).map(String))
|
|
288
|
+
const indeg = ids.map(() => 0)
|
|
289
|
+
steps.forEach((s, i) => deps[i].forEach(d => { const j = ids.indexOf(d); if (j >= 0) indeg[i]++ }))
|
|
290
|
+
const order = []
|
|
291
|
+
const used = new Set()
|
|
292
|
+
while (order.length < ids.length) {
|
|
293
|
+
let progressed = false
|
|
294
|
+
for (let i = 0; i < ids.length; i++) {
|
|
295
|
+
if (used.has(i)) continue
|
|
296
|
+
if (deps[i].every(d => { const j = ids.indexOf(d); return j < 0 || used.has(j) })) {
|
|
297
|
+
order.push(ids[i]); used.add(i); progressed = true
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (!progressed) { order.push(...ids.filter((_, i) => !used.has(i))); break }
|
|
301
|
+
}
|
|
302
|
+
return { ok: true, success: true, output: { chain: order, stepCount: order.length, cyclic: order.length < ids.length }, provenance: ['[BP_ATOM_SCIENTIFIC_PIPE_210] 拓扑排序'] }
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** 论文检索器:关键词 + 文献库 → 相关度排序(TF-IDF 近似) */
|
|
306
|
+
function scientificPaper(input) {
|
|
307
|
+
const o = input || {}
|
|
308
|
+
const q = toArr(o.keywords || o.query || o.terms).map(String)
|
|
309
|
+
const docs = toArr(o.papers || o.library || o.docs)
|
|
310
|
+
if (!q.length) return { ok: false, success: false, reason: 'NO_KEYWORDS', message: '缺少关键词' }
|
|
311
|
+
const N = docs.length || 1
|
|
312
|
+
const df = {}
|
|
313
|
+
for (const k of q) df[k] = docs.filter(d => { const t = typeof d === 'string' ? d : JSON.stringify(d); return t.indexOf(k) >= 0 }).length || 1
|
|
314
|
+
const scored = docs.map(d => {
|
|
315
|
+
const t = typeof d === 'string' ? d : JSON.stringify(d)
|
|
316
|
+
let s = 0
|
|
317
|
+
for (const k of q) {
|
|
318
|
+
const tf = (t.split(k).length - 1)
|
|
319
|
+
if (tf > 0) s += (1 + Math.log(tf)) * Math.log(N / df[k])
|
|
320
|
+
}
|
|
321
|
+
return { doc: (d && (d.title || d.id)) || String(t).slice(0, 40), score: r4(s), hits: q.filter(k => t.indexOf(k) >= 0) }
|
|
322
|
+
}).filter(x => x.score > 0).sort((a, b) => b.score - a.score)
|
|
323
|
+
return { ok: true, success: true, output: { results: scored, count: scored.length }, provenance: ['[BP_ATOM_SCIENTIFIC_PAPER_211] TF-IDF'] }
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** 实验设计器:假设(因素 + 水平)→ 实验方案(全因子 / 正交近似) */
|
|
327
|
+
function scientificExp(input) {
|
|
328
|
+
const o = input || {}
|
|
329
|
+
const factors = o.factors || {}
|
|
330
|
+
const names = Object.keys(factors)
|
|
331
|
+
if (!names.length) return { ok: false, success: false, reason: 'NO_FACTORS', message: '缺少因素定义' }
|
|
332
|
+
const levels = names.map(n => toArr(factors[n]))
|
|
333
|
+
const combos = []
|
|
334
|
+
const rec = (idx, cur) => {
|
|
335
|
+
if (idx === names.length) { combos.push(Object.assign({}, cur)); return }
|
|
336
|
+
for (const lv of levels[idx]) { cur[names[idx]] = lv; rec(idx + 1, cur) }
|
|
337
|
+
}
|
|
338
|
+
rec(0, {})
|
|
339
|
+
const maxRuns = toNum(o.maxRuns) || 64
|
|
340
|
+
const selected = combos.slice(0, maxRuns)
|
|
341
|
+
return {
|
|
342
|
+
ok: true, success: true,
|
|
343
|
+
output: { design: selected, runCount: selected.length, fullFactorial: combos.length, truncated: combos.length > maxRuns, factors: names },
|
|
344
|
+
provenance: ['[BP_ATOM_SCIENTIFIC_EXP_212] 全因子设计(超限截断)'],
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 平行仿真器:物理参数(初值 + 速率 + 步数)→ 轨迹 */
|
|
349
|
+
function parallelSim(input) {
|
|
350
|
+
const o = input || {}
|
|
351
|
+
const init = toNum(o.initial !== undefined ? o.initial : o.x0)
|
|
352
|
+
const rate = toNum(o.rate !== undefined ? o.rate : o.k)
|
|
353
|
+
const steps = Math.max(1, Math.min(10000, toNum(o.steps) || 50))
|
|
354
|
+
const dt = toNum(o.dt) || 0.1
|
|
355
|
+
const decay = o.decay !== undefined ? toNum(o.decay) : 0
|
|
356
|
+
const traj = []
|
|
357
|
+
let x = init
|
|
358
|
+
for (let i = 0; i < steps; i++) { x = x + rate * x * dt - decay * x * dt; traj.push(r4(x)) }
|
|
359
|
+
return { ok: true, success: true, output: { trajectory: traj, final: r4(x), steps, dt }, provenance: ['[BP_ATOM_PARALLEL_SIM_214] 一阶动力学离散积分'] }
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** 平行调优器:仿真轨迹 → 调优建议(趋势 + 稳定性) */
|
|
363
|
+
function parallelTune(input) {
|
|
364
|
+
const o = input || {}
|
|
365
|
+
const traj = toArr(o.trajectory || o.results || o.series).map(toNum)
|
|
366
|
+
if (traj.length < 2) return { ok: false, success: false, reason: 'TOO_SHORT', message: '轨迹过短' }
|
|
367
|
+
const n = traj.length
|
|
368
|
+
const xs = traj.map((_, i) => i)
|
|
369
|
+
const mx = mean(xs), my = mean(traj)
|
|
370
|
+
let num = 0, den = 0
|
|
371
|
+
for (let i = 0; i < n; i++) { num += (xs[i] - mx) * (traj[i] - my); den += (xs[i] - mx) ** 2 }
|
|
372
|
+
const slope = den ? num / den : 0
|
|
373
|
+
const volatility = std(traj)
|
|
374
|
+
const sug = []
|
|
375
|
+
if (Math.abs(slope) > 0.01) sug.push(slope > 0 ? '上升偏快:考虑降低 rate' : '下降偏快:考虑提高 rate 或减小 decay')
|
|
376
|
+
if (volatility > Math.abs(my) * 0.3) sug.push('波动过大:减小步长 dt 或增加阻尼')
|
|
377
|
+
if (!sug.length) sug.push('轨迹平稳:当前参数可保留')
|
|
378
|
+
return { ok: true, success: true, output: { slope: r4(slope), volatility: r4(volatility), mean: r4(my), suggestions: sug }, provenance: ['[BP_ATOM_PARALLEL_TUNE_215] 线性回归 + 波动'] }
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ── 符号域 ────────────────────────────────────────────────────
|
|
382
|
+
/** 符号化解释生成器:决策(条件-动作对)→ 符号链 */
|
|
383
|
+
function symbolExplain(input) {
|
|
384
|
+
const o = input || {}
|
|
385
|
+
const rules = toArr(o.rules || o.decision || o.conditions)
|
|
386
|
+
const chain = rules.map((r, i) => {
|
|
387
|
+
if (typeof r === 'string') return { step: i + 1, expr: r }
|
|
388
|
+
const cond = (r && (r.if || r.condition || r.when)) || ''
|
|
389
|
+
const act = (r && (r.then || r.action || r.result)) || ''
|
|
390
|
+
return { step: i + 1, cond: String(cond), action: String(act), symbol: 'R' + (i + 1) }
|
|
391
|
+
})
|
|
392
|
+
const expr = chain.map(x => x.cond ? `(${x.cond})→${x.action}` : x.expr).join(' ∧ ')
|
|
393
|
+
return { ok: true, success: true, output: { chain, expression: expr, stepCount: chain.length }, provenance: ['[BP_ATOM_SYMBOL_EXPLAIN_205] 规则→符号链'] }
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** 符号可视化器:符号数据(节点/边或表达式)→ 可直接渲染的图结构 */
|
|
397
|
+
function symbolVisual(input) {
|
|
398
|
+
const o = input || {}
|
|
399
|
+
const sym = o.symbols || o.data || o.expression
|
|
400
|
+
const nodes = [], edges = []
|
|
401
|
+
if (Array.isArray(sym)) {
|
|
402
|
+
sym.forEach((s, i) => nodes.push({ id: 'n' + i, label: String(s) }))
|
|
403
|
+
for (let i = 1; i < nodes.length; i++) edges.push({ from: nodes[i - 1].id, to: nodes[i].id })
|
|
404
|
+
} else if (typeof sym === 'string') {
|
|
405
|
+
const parts = sym.split(/→|->|∧|&/)
|
|
406
|
+
parts.forEach((s, i) => nodes.push({ id: 'n' + i, label: s.trim() }))
|
|
407
|
+
for (let i = 1; i < nodes.length; i++) edges.push({ from: nodes[i - 1].id, to: nodes[i].id })
|
|
408
|
+
}
|
|
409
|
+
return { ok: true, success: true, output: { nodes, edges, layout: 'dagre-lr', nodeCount: nodes.length }, provenance: ['[BP_ATOM_SYMBOL_VISUAL_206] 链式图'] }
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// ── 群体/协同域 ────────────────────────────────────────────────
|
|
413
|
+
/** 群体共识器:各节点意见(含权重/置信)→ 共识结果 */
|
|
414
|
+
function swarmConsensus(input) {
|
|
415
|
+
const o = input || {}
|
|
416
|
+
const votes = toArr(o.votes || o.opinions || o.nodes)
|
|
417
|
+
if (!votes.length) return { ok: false, success: false, reason: 'NO_VOTES', message: '缺少意见' }
|
|
418
|
+
const agg = {}
|
|
419
|
+
let totalW = 0
|
|
420
|
+
for (const v of votes) {
|
|
421
|
+
const key = String((v && (v.opinion !== undefined ? v.opinion : v.value)) ?? v)
|
|
422
|
+
const w = toNum((v && (v.weight !== undefined ? v.weight : v.confidence)) || 1)
|
|
423
|
+
agg[key] = (agg[key] || 0) + w
|
|
424
|
+
totalW += w
|
|
425
|
+
}
|
|
426
|
+
const ranked = Object.keys(agg).map(k => ({ opinion: k, weight: r4(agg[k]), share: r4(agg[k] / (totalW || 1)) })).sort((a, b) => b.weight - a.weight)
|
|
427
|
+
const top = ranked[0]
|
|
428
|
+
return {
|
|
429
|
+
ok: true, success: true,
|
|
430
|
+
output: { consensus: top ? top.opinion : null, share: top ? top.share : 0, ranked,
|
|
431
|
+
unanimous: ranked.length === 1, entropy: r4(-ranked.reduce((s, r) => s + (r.share > 0 ? r.share * Math.log(r.share) : 0), 0)) },
|
|
432
|
+
provenance: ['[BP_ATOM_SWARM_CONSENSUS_217] 加权投票 + 熵'],
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** 交通路口协同器:路口状态(各方向排队)→ 配时方案 */
|
|
437
|
+
function trafficCoord(input) {
|
|
438
|
+
const o = input || {}
|
|
439
|
+
const dirs = o.directions || o.lanes || {}
|
|
440
|
+
const names = Object.keys(dirs)
|
|
441
|
+
if (!names.length) return { ok: false, success: false, reason: 'NO_DIRECTIONS', message: '缺少路口方向数据' }
|
|
442
|
+
const totalCycle = toNum(o.cycle) || 60
|
|
443
|
+
const minGreen = toNum(o.minGreen) || 10
|
|
444
|
+
const loads = names.map(n => ({ name: n, queue: toNum((dirs[n] && dirs[n].queue !== undefined) ? dirs[n].queue : dirs[n]) }))
|
|
445
|
+
const totalQ = loads.reduce((s, x) => s + x.queue, 0) || 1
|
|
446
|
+
const allocatable = Math.max(0, totalCycle - minGreen * loads.length)
|
|
447
|
+
const plan = loads.map(l => ({ direction: l.name, queue: l.queue, green: r4(minGreen + allocatable * (l.queue / totalQ)) }))
|
|
448
|
+
return { ok: true, success: true, output: { plan, cycle: totalCycle, totalQueue: totalQ }, provenance: ['[BP_ATOM_TRAFFIC_COORD_207] 按排队比例配时'] }
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// ── 人/意图域 ──────────────────────────────────────────────────
|
|
452
|
+
/** 意图提取器:想法向量 → 意图概率分布(softmax) */
|
|
453
|
+
function peopleIntent(input) {
|
|
454
|
+
const o = input || {}
|
|
455
|
+
const v = toArr(o.vector || o.thoughts || o.logits).map(toNum)
|
|
456
|
+
const labels = toArr(o.labels || o.intents).map(String)
|
|
457
|
+
if (!v.length) return { ok: false, success: false, reason: 'NO_VECTOR', message: '缺少想法向量' }
|
|
458
|
+
const mx = Math.max(...v)
|
|
459
|
+
const ex = v.map(x => Math.exp(x - mx))
|
|
460
|
+
const sum = ex.reduce((s, x) => s + x, 0) || 1
|
|
461
|
+
const probs = ex.map(x => r4(x / sum))
|
|
462
|
+
const dist = probs.map((p, i) => ({ intent: labels[i] || 'I' + i, prob: p })).sort((a, b) => b.prob - a.prob)
|
|
463
|
+
return { ok: true, success: true, output: { distribution: dist, top: dist[0], entropy: r4(-probs.reduce((s, p) => s + (p > 0 ? p * Math.log(p) : 0), 0)) }, provenance: ['[BP_ATOM_PEOPLE_INTENT_003] softmax'] }
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** 目标结构化器:意图(文本/对象)→ 结构化目标(目标-条件-指标-约束) */
|
|
467
|
+
function peopleTarget(input) {
|
|
468
|
+
const o = input || {}
|
|
469
|
+
const raw = o.intent !== undefined ? o.intent : (o.text || o.description)
|
|
470
|
+
const txt = typeof raw === 'string' ? raw : JSON.stringify(raw || {})
|
|
471
|
+
const objMatch = txt.match(/(提高|降低|减少|增加|实现|完成|达到|优化)/g) || []
|
|
472
|
+
const numMatch = txt.match(/(\d+(?:\.\d+)?)\s*(%|个|条|次|分|天|小时|万元|元)/g) || []
|
|
473
|
+
const obj = {
|
|
474
|
+
verb: objMatch[0] || null,
|
|
475
|
+
metrics: numMatch,
|
|
476
|
+
conditions: (txt.match(/(在|当|如果|除非)[^,。;;]+/g) || []).slice(0, 5),
|
|
477
|
+
raw: txt.slice(0, 200),
|
|
478
|
+
structured: !!(objMatch.length || numMatch.length),
|
|
479
|
+
}
|
|
480
|
+
return { ok: true, success: true, output: obj, provenance: ['[BP_ATOM_PEOPLE_TARGET_004] 动宾 + 指标 + 条件抽取'] }
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** 沉默者接入器:行为模式(事件序列/频次)→ 意图候选集 */
|
|
484
|
+
function peopleSilent(input) {
|
|
485
|
+
const o = input || {}
|
|
486
|
+
const events = toArr(o.events || o.patterns || o.behaviors)
|
|
487
|
+
const cnt = {}
|
|
488
|
+
for (const e of events) {
|
|
489
|
+
const k = String((e && (e.type || e.action || e.name)) || e)
|
|
490
|
+
cnt[k] = (cnt[k] || 0) + 1
|
|
491
|
+
}
|
|
492
|
+
const total = Object.values(cnt).reduce((s, x) => s + x, 0) || 1
|
|
493
|
+
const cands = Object.keys(cnt).map(k => ({ intent: k, freq: cnt[k], prob: r4(cnt[k] / total) })).sort((a, b) => b.prob - a.prob)
|
|
494
|
+
return { ok: true, success: true, output: { candidates: cands, eventCount: events.length, confident: cands.length > 0 && cands[0].prob >= 0.5 }, provenance: ['[BP_ATOM_PEOPLE_SILENT_005] 频次→候选分布'] }
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** 量子态编码器(人):意图概率分布 → 量子叠加态参数(振幅 + 相位) */
|
|
498
|
+
function peopleQuantum(input) {
|
|
499
|
+
const o = input || {}
|
|
500
|
+
const probs = toArr(o.distribution || o.probs || o.p).map(toNum)
|
|
501
|
+
if (!probs.length) return { ok: false, success: false, reason: 'NO_DISTRIBUTION', message: '缺少概率分布' }
|
|
502
|
+
const s = probs.reduce((a, b) => a + b, 0) || 1
|
|
503
|
+
const amps = probs.map(p => Math.sqrt(Math.max(0, p / s)))
|
|
504
|
+
const phases = amps.map((_, i) => r4(2 * Math.PI * i / amps.length))
|
|
505
|
+
return {
|
|
506
|
+
ok: true, success: true,
|
|
507
|
+
output: { amplitudes: amps.map(r4), phases, normalized: r4(amps.reduce((s2, a) => s2 + a * a, 0)), qubits: Math.max(1, Math.ceil(Math.log2(amps.length))) },
|
|
508
|
+
provenance: ['[BP_ATOM_PEOPLE_QUANTUM_008] p→sqrt(p) 振幅 + 等间隔相位'],
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** 叠加态构建器:量子参数(振幅/相位)→ 复数态向量 */
|
|
513
|
+
function peopleSuperposition(input) {
|
|
514
|
+
const o = input || {}
|
|
515
|
+
const amps = toArr(o.amplitudes || o.amps).map(toNum)
|
|
516
|
+
const phases = toArr(o.phases).map(toNum)
|
|
517
|
+
if (!amps.length) return { ok: false, success: false, reason: 'NO_AMPLITUDES', message: '缺少振幅' }
|
|
518
|
+
const state = amps.map((a, i) => ({ re: r4(a * Math.cos(phases[i] || 0)), im: r4(a * Math.sin(phases[i] || 0)) }))
|
|
519
|
+
const norm = state.reduce((s, c) => s + c.re * c.re + c.im * c.im, 0)
|
|
520
|
+
return { ok: true, success: true, output: { state, dimension: state.length, norm: r4(norm) }, provenance: ['[BP_ATOM_PEOPLE_SUPERPOSITION_009] 复振幅展开'] }
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** 坍缩决策器:叠加态 → 按概率采样得到一个确定结果(可用种子复现) */
|
|
524
|
+
function peopleCollapse(input) {
|
|
525
|
+
const o = input || {}
|
|
526
|
+
const state = toArr(o.state || o.superposition)
|
|
527
|
+
if (!state.length) return { ok: false, success: false, reason: 'NO_STATE', message: '缺少量子态' }
|
|
528
|
+
const probs = state.map(c => (c.re !== undefined ? c.re * c.re + c.im * c.im : toNum(c) ** 2))
|
|
529
|
+
const sum = probs.reduce((a, b) => a + b, 0) || 1
|
|
530
|
+
// 确定性伪随机(LCG,种子可由调用方给)——保证可复现
|
|
531
|
+
let seed = (toNum(o.seed) || 42) >>> 0
|
|
532
|
+
seed = (seed * 1664525 + 1013904223) >>> 0
|
|
533
|
+
const u = seed / 4294967296
|
|
534
|
+
let acc = 0, idx = probs.length - 1
|
|
535
|
+
for (let i = 0; i < probs.length; i++) { acc += probs[i] / sum; if (u <= acc) { idx = i; break } }
|
|
536
|
+
return { ok: true, success: true, output: { collapsed: idx, probability: r4(probs[idx] / sum), seed: toNum(o.seed) || 42, deterministic: true }, provenance: ['[BP_ATOM_PEOPLE_COLLAPSE_010] 测度采样(LCG 可复现)'] }
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** 反馈接收器:用户反馈(文本/评分/选项)→ 结构化反馈 */
|
|
540
|
+
function peopleFeedback(input) {
|
|
541
|
+
const o = input || {}
|
|
542
|
+
const raw = o.feedback !== undefined ? o.feedback : (o.text || o.value)
|
|
543
|
+
const score = toNum(o.score !== undefined ? o.score : o.rating)
|
|
544
|
+
const txt = typeof raw === 'string' ? raw : JSON.stringify(raw || '')
|
|
545
|
+
const positive = /好|赞|满意|喜欢|不错|great|good|yes/i.test(txt) || score >= 0.7
|
|
546
|
+
const negative = /差|糟|不满|讨厌|bad|no|broken/i.test(txt) || (score > 0 && score <= 0.3)
|
|
547
|
+
return {
|
|
548
|
+
ok: true, success: true,
|
|
549
|
+
output: {
|
|
550
|
+
sentiment: positive && !negative ? 'positive' : negative && !positive ? 'negative' : (positive && negative ? 'mixed' : 'neutral'),
|
|
551
|
+
score: isFinite(score) ? score : null, length: txt.length,
|
|
552
|
+
keywords: (txt.match(/[\u4e00-\u9fa5]{2,4}|[a-z]{3,}/gi) || []).slice(0, 10),
|
|
553
|
+
raw: txt.slice(0, 200),
|
|
554
|
+
},
|
|
555
|
+
provenance: ['[BP_ATOM_PEOPLE_FEEDBACK_011] 情感极性 + 关键词'],
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** 增量学习器:反馈数据 → 更新后的模型参数(在线均值/方差 + 学习率) */
|
|
560
|
+
function peopleLearn(input) {
|
|
561
|
+
const o = input || {}
|
|
562
|
+
const xs = toArr(o.samples || o.data || o.feedback).map(toNum)
|
|
563
|
+
if (!xs.length) return { ok: false, success: false, reason: 'NO_SAMPLES', message: '缺少样本' }
|
|
564
|
+
const lr = o.learningRate !== undefined ? toNum(o.learningRate) : 0.1
|
|
565
|
+
let m = o.mean !== undefined ? toNum(o.mean) : xs[0]
|
|
566
|
+
let v = o.variance !== undefined ? toNum(o.variance) : 1
|
|
567
|
+
for (const x of xs) {
|
|
568
|
+
const d = x - m
|
|
569
|
+
m = m + lr * d
|
|
570
|
+
v = (1 - lr) * v + lr * d * d
|
|
571
|
+
}
|
|
572
|
+
return { ok: true, success: true, output: { mean: r4(m), variance: r4(v), std: r4(Math.sqrt(Math.max(0, v))), n: xs.length, learningRate: lr }, provenance: ['[BP_ATOM_PEOPLE_LEARN_012] 指数加权在线更新'] }
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// ── 量子域(6 条)─────────────────────────────────────────────
|
|
576
|
+
/** 量子编码器:经典数据(0/1 序列或数值)→ 量子电路参数 */
|
|
577
|
+
function quantumEncode(input) {
|
|
578
|
+
const o = input || {}
|
|
579
|
+
const bits = toArr(o.bits || o.data || o.values).map(x => toNum(x) ? 1 : 0)
|
|
580
|
+
if (!bits.length) return { ok: false, success: false, reason: 'NO_DATA', message: '缺少数据' }
|
|
581
|
+
const gates = bits.map((b, i) => b === 1
|
|
582
|
+
? { gate: 'X', qubit: i % 8, layer: Math.floor(i / 8) }
|
|
583
|
+
: { gate: 'I', qubit: i % 8, layer: Math.floor(i / 8) })
|
|
584
|
+
return { ok: true, success: true, output: { circuit: gates, qubits: Math.min(8, bits.length), depth: Math.ceil(bits.length / 8), bitLength: bits.length }, provenance: ['[BP_ATOM_QUANTUM_ENCODE_013] 位→X/I 门'] }
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/** 量子执行器:量子电路(门序列)→ 测量结果(状态向量演化 + 采样) */
|
|
588
|
+
function quantumExecute(input) {
|
|
589
|
+
const o = input || {}
|
|
590
|
+
const gates = toArr(o.circuit || o.gates)
|
|
591
|
+
const nq = Math.min(6, Math.max(1, toNum(o.qubits) || 2))
|
|
592
|
+
const dim = 1 << nq
|
|
593
|
+
// 初始 |0...0>
|
|
594
|
+
let st = new Array(dim).fill(0); st[0] = 1
|
|
595
|
+
/* 单量子比特门:M = [m00,m01,m10,m11](完整 2×2,不能用 2 元压缩)
|
|
596
|
+
out[i] = M[bit][0] * st[j0] + M[bit][1] * st[j1]
|
|
597
|
+
其中 j0 = 该位清 0,j1 = 该位置 1 */
|
|
598
|
+
const apply1 = (q, M) => {
|
|
599
|
+
const out = new Array(dim).fill(0)
|
|
600
|
+
for (let i = 0; i < dim; i++) {
|
|
601
|
+
const bit = (i >> q) & 1
|
|
602
|
+
const j0 = i & ~(1 << q)
|
|
603
|
+
const j1 = j0 | (1 << q)
|
|
604
|
+
out[i] = M[bit * 2] * st[j0] + M[bit * 2 + 1] * st[j1]
|
|
605
|
+
}
|
|
606
|
+
st = out
|
|
607
|
+
}
|
|
608
|
+
const S2 = 1 / Math.SQRT2
|
|
609
|
+
for (const g of gates) {
|
|
610
|
+
const q = toNum(g.qubit) % nq
|
|
611
|
+
if (g.gate === 'X') apply1(q, [0, 1, 1, 0]) // [[0,1],[1,0]]
|
|
612
|
+
else if (g.gate === 'H') apply1(q, [S2, S2, S2, -S2]) // (1/√2)[[1,1],[1,-1]]
|
|
613
|
+
else if (g.gate === 'Z') apply1(q, [1, 0, 0, -1]) // [[1,0],[0,-1]]
|
|
614
|
+
|
|
615
|
+
}
|
|
616
|
+
const probs = st.map(a => r4(a * a))
|
|
617
|
+
let seed = (toNum(o.seed) || 7) >>> 0; seed = (seed * 1664525 + 1013904223) >>> 0
|
|
618
|
+
const u = seed / 4294967296
|
|
619
|
+
let acc = 0, measured = dim - 1
|
|
620
|
+
for (let i = 0; i < dim; i++) { acc += probs[i]; if (u <= acc) { measured = i; break } }
|
|
621
|
+
return { ok: true, success: true, output: { probabilities: probs, measured, measuredBits: measured.toString(2).padStart(nq, '0'), qubits: nq, gateCount: gates.length }, provenance: ['[BP_ATOM_QUANTUM_EXECUTE_014] 状态向量演化 + LCG 采样'] }
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/** 量子模拟器:量子电路 → 完整状态向量(振幅 + 概率,不采样) */
|
|
625
|
+
function quantumSimulate(input) {
|
|
626
|
+
const o = input || {}
|
|
627
|
+
const gates = toArr(o.circuit || o.gates)
|
|
628
|
+
const nq = Math.min(6, Math.max(1, toNum(o.qubits) || 2))
|
|
629
|
+
const dim = 1 << nq
|
|
630
|
+
let st = new Array(dim).fill(0); st[0] = 1
|
|
631
|
+
const S2 = 1 / Math.SQRT2
|
|
632
|
+
/* 单量子比特门:M = [m00,m01,m10,m11](完整 2×2,不能用 2 元压缩)
|
|
633
|
+
out[i] = M[bit][0] * st[j0] + M[bit][1] * st[j1]
|
|
634
|
+
其中 j0 = 该位清 0,j1 = 该位置 1 */
|
|
635
|
+
const apply1 = (q, M) => {
|
|
636
|
+
const out = new Array(dim).fill(0)
|
|
637
|
+
for (let i = 0; i < dim; i++) {
|
|
638
|
+
const bit = (i >> q) & 1
|
|
639
|
+
const j0 = i & ~(1 << q)
|
|
640
|
+
const j1 = j0 | (1 << q)
|
|
641
|
+
out[i] = M[bit * 2] * st[j0] + M[bit * 2 + 1] * st[j1]
|
|
642
|
+
}
|
|
643
|
+
st = out
|
|
644
|
+
}
|
|
645
|
+
for (const g of gates) {
|
|
646
|
+
const q = toNum(g.qubit) % nq
|
|
647
|
+
if (g.gate === 'X') apply1(q, [0, 1, 1, 0]) // [[0,1],[1,0]]
|
|
648
|
+
else if (g.gate === 'H') apply1(q, [S2, S2, S2, -S2]) // (1/√2)[[1,1],[1,-1]]
|
|
649
|
+
else if (g.gate === 'Z') apply1(q, [1, 0, 0, -1]) // [[1,0],[0,-1]]
|
|
650
|
+
|
|
651
|
+
else if (g.gate === 'Y') apply1(q, [0, 1])
|
|
652
|
+
}
|
|
653
|
+
const norm = r4(st.reduce((s, a) => s + a * a, 0))
|
|
654
|
+
return { ok: true, success: true, output: { amplitudes: st.map(r4), probabilities: st.map(a => r4(a * a)), norm, qubits: nq, dimension: dim }, provenance: ['[BP_ATOM_QUANTUM_SIMULATE_015] 状态向量完整输出'] }
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/** 振幅放大器:量子态 → Grover 式振幅放大的态(对目标项重复反射) */
|
|
658
|
+
function quantumAmplify(input) {
|
|
659
|
+
const o = input || {}
|
|
660
|
+
const probs = toArr(o.probabilities || o.state || o.amps).map(toNum)
|
|
661
|
+
if (!probs.length) return { ok: false, success: false, reason: 'NO_STATE', message: '缺少量子态' }
|
|
662
|
+
const target = toNum(o.target) || 0
|
|
663
|
+
const iters = Math.max(1, Math.min(20, toNum(o.iterations) || 1))
|
|
664
|
+
let a = probs.map(p => Math.sqrt(Math.max(0, p)))
|
|
665
|
+
const norm = () => Math.sqrt(a.reduce((s, x) => s + x * x, 0)) || 1
|
|
666
|
+
a = a.map(x => x / norm())
|
|
667
|
+
for (let k = 0; k < iters; k++) {
|
|
668
|
+
// 关于目标项翻转符号
|
|
669
|
+
a[target] = -a[target]
|
|
670
|
+
// 关于均值反射
|
|
671
|
+
const m = a.reduce((s, x) => s + x, 0) / a.length
|
|
672
|
+
a = a.map(x => 2 * m - x)
|
|
673
|
+
const nn = norm(); a = a.map(x => x / nn)
|
|
674
|
+
}
|
|
675
|
+
const out = a.map(x => r4(x * x))
|
|
676
|
+
return { ok: true, success: true, output: { amplified: out, target, targetProb: out[target], iterations: iters, gain: r4(out[target] - (probs[target] || 0)) }, provenance: ['[BP_ATOM_QUANTUM_AMPLIFY_016] Grover 迭代'] }
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** 量子保护器:量子态 → 加保护后的态(重复码 3 位 + 多数表决校验) */
|
|
680
|
+
function quantumProtect(input) {
|
|
681
|
+
const o = input || {}
|
|
682
|
+
const st = toArr(o.state || o.amplitudes).map(toNum)
|
|
683
|
+
if (!st.length) return { ok: false, success: false, reason: 'NO_STATE', message: '缺少量子态' }
|
|
684
|
+
// 3 重复码:每个逻辑位 → 3 个物理位(这里对振幅做同样的复制与归一)
|
|
685
|
+
const enc = []
|
|
686
|
+
for (const a of st) { enc.push(r4(a / Math.sqrt(3)), r4(a / Math.sqrt(3)), r4(a / Math.sqrt(3))) }
|
|
687
|
+
const norm = Math.sqrt(enc.reduce((s, x) => s + x * x, 0)) || 1
|
|
688
|
+
const protectedState = enc.map(x => r4(x / norm))
|
|
689
|
+
return { ok: true, success: true, output: { protectedState, code: 'repetition-3', logicalLength: st.length, physicalLength: protectedState.length, correctableErrors: 1 }, provenance: ['[BP_ATOM_QUANTUM_PROTECT_018] 3 重复码'] }
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** 量子测量器:量子态 → 多次测量的统计分布 */
|
|
693
|
+
function quantumMeasure(input) {
|
|
694
|
+
const o = input || {}
|
|
695
|
+
const probs = toArr(o.probabilities || o.state).map(toNum)
|
|
696
|
+
if (!probs.length) return { ok: false, success: false, reason: 'NO_STATE', message: '缺少量子态' }
|
|
697
|
+
const shots = Math.max(1, Math.min(100000, toNum(o.shots) || 1000))
|
|
698
|
+
let seed = (toNum(o.seed) || 11) >>> 0
|
|
699
|
+
const rnd = () => { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 4294967296 }
|
|
700
|
+
const norm = probs.reduce((a, b) => a + b, 0) || 1
|
|
701
|
+
const counts = new Array(probs.length).fill(0)
|
|
702
|
+
for (let s = 0; s < shots; s++) {
|
|
703
|
+
const u = rnd(); let acc = 0, idx = probs.length - 1
|
|
704
|
+
for (let i = 0; i < probs.length; i++) { acc += probs[i] / norm; if (u <= acc) { idx = i; break } }
|
|
705
|
+
counts[idx]++
|
|
706
|
+
}
|
|
707
|
+
const freq = counts.map(c => r4(c / shots))
|
|
708
|
+
return { ok: true, success: true, output: { counts, frequencies: freq, shots, seed: toNum(o.seed) || 11, theoretical: probs.map(p => r4(p / norm)) }, provenance: ['[BP_ATOM_QUANTUM_MEASURE_019] 蒙特卡洛(LCG 可复现)'] }
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// ── 生物/宇宙/融合域 ──────────────────────────────────────────
|
|
712
|
+
/** 情感共频器:用户情感(valence/arousal)→ AI 情感状态(同频映射) */
|
|
713
|
+
function bioEmotion(input) {
|
|
714
|
+
const o = input || {}
|
|
715
|
+
const v = clamp01(o.valence !== undefined ? o.valence : (o.v !== undefined ? o.v : 0.5)) * 2 - 1
|
|
716
|
+
const a = clamp01(o.arousal !== undefined ? o.arousal : (o.a !== undefined ? o.a : 0.5))
|
|
717
|
+
// 同频:AI 情感与用户同向但略低强度(避免放大负面情绪)
|
|
718
|
+
const aiV = r4(v * 0.7), aiA = r4(a * 0.7)
|
|
719
|
+
const label = aiV > 0.2 ? (aiA > 0.5 ? '积极投入' : '温和支持') : aiV < -0.2 ? (aiA > 0.5 ? '关切稳定' : '安静陪伴') : '中性专注'
|
|
720
|
+
return { ok: true, success: true, output: { user: { valence: r4(v), arousal: r4(a) }, ai: { valence: aiV, arousal: aiA }, label, damping: 0.7 }, provenance: ['[BP_ATOM_BIO_EMOTION_022] 同向 0.7 阻尼映射'] }
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** 宇宙遗产传承器:人类知识(条目集合)→ 遗产包(分层 + Merkle 校验 + 摘要) */
|
|
724
|
+
function cosmicLegacy(input) {
|
|
725
|
+
const crypto = require('crypto')
|
|
726
|
+
const o = input || {}
|
|
727
|
+
const items = toArr(o.knowledge || o.items || o.data)
|
|
728
|
+
const layers = { 核心公理: [], 方法体系: [], 工程实践: [], 附录: [] }
|
|
729
|
+
const KEYS = Object.keys(layers)
|
|
730
|
+
items.forEach((it, i) => { layers[KEYS[i % KEYS.length]].push(it) })
|
|
731
|
+
const hashes = items.map(it => crypto.createHash('sha256').update(JSON.stringify(it)).digest('hex').slice(0, 8))
|
|
732
|
+
const root = crypto.createHash('sha256').update(hashes.join('')).digest('hex').slice(0, 32)
|
|
733
|
+
return { ok: true, success: true, output: { layers, itemCount: items.length, merkleRoot: root, layerCount: KEYS.length }, provenance: ['[BP_ATOM_COSMIC_LEGACY_035] 四层归档 + Merkle'] }
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/** 深空信号接收器:探测器原始数据流 → 科学数据(去趋势 + 峰值检测) */
|
|
737
|
+
function cosmicReceive(input) {
|
|
738
|
+
const o = input || {}
|
|
739
|
+
const raw = toArr(o.signal || o.data || o.samples).map(toNum)
|
|
740
|
+
if (raw.length < 3) return { ok: false, success: false, reason: 'SIGNAL_TOO_SHORT', message: '信号过短(需 ≥3 点)' }
|
|
741
|
+
const m = mean(raw)
|
|
742
|
+
const detrended = raw.map(x => r4(x - m))
|
|
743
|
+
const sd = std(raw) || 1
|
|
744
|
+
const peaks = []
|
|
745
|
+
for (let i = 1; i < detrended.length - 1; i++) {
|
|
746
|
+
if (detrended[i] > detrended[i - 1] && detrended[i] > detrended[i + 1] && detrended[i] > 2 * sd) {
|
|
747
|
+
peaks.push({ index: i, value: detrended[i], snr: r4(detrended[i] / sd) })
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return { ok: true, success: true, output: { detrended, peaks, peakCount: peaks.length, mean: r4(m), std: r4(sd), snrMax: peaks.length ? Math.max(...peaks.map(p => p.snr)) : 0 }, provenance: ['[BP_ATOM_COSMIC_RECEIVE_036] 去趋势 + 3σ 峰检测'] }
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/** 量子-宇宙纠缠器:量子态 + 宇宙坐标 → 纠缠网络(张量积 + 关联强度) */
|
|
754
|
+
function fusionQC(input) {
|
|
755
|
+
const o = input || {}
|
|
756
|
+
const q = toArr(o.quantum || o.state || o.amps).map(toNum)
|
|
757
|
+
const coord = toArr(o.coords || o.coordinates || o.position).map(toNum)
|
|
758
|
+
if (!q.length) return { ok: false, success: false, reason: 'NO_QUANTUM_STATE', message: '缺少量子态' }
|
|
759
|
+
// 张量积(限维,防爆炸)
|
|
760
|
+
const A = q.slice(0, 4), B = (coord.length ? coord : [1]).slice(0, 4)
|
|
761
|
+
const tensor = []
|
|
762
|
+
for (const a of A) for (const b of B) tensor.push(r4(a * b))
|
|
763
|
+
const norm = Math.sqrt(tensor.reduce((s, x) => s + x * x, 0)) || 1
|
|
764
|
+
const entangled = tensor.map(x => r4(x / norm))
|
|
765
|
+
// 关联强度:归一化互相关
|
|
766
|
+
const corr = (() => {
|
|
767
|
+
const n = Math.min(A.length, B.length); if (n < 2) return 0
|
|
768
|
+
const ma = mean(A), mb = mean(B)
|
|
769
|
+
let num = 0, da = 0, db = 0
|
|
770
|
+
for (let i = 0; i < n; i++) { num += (A[i] - ma) * (B[i] - mb); da += (A[i] - ma) ** 2; db += (B[i] - mb) ** 2 }
|
|
771
|
+
return (da && db) ? r4(num / Math.sqrt(da * db)) : 0
|
|
772
|
+
})()
|
|
773
|
+
return { ok: true, success: true, output: { entangledState: entangled, dimension: entangled.length, correlation: corr, coordDim: B.length }, provenance: ['[BP_ATOM_FUSION_Q_C_039] 张量积 + 互相关'] }
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/** 归无-量子叠加器:归无指令 → 把叠加态归零(保留维度的零态) */
|
|
777
|
+
function fusionVoidQ(input) {
|
|
778
|
+
const o = input || {}
|
|
779
|
+
const dim = Math.max(1, Math.min(1024, toNum(o.dim || o.dimension) || (toArr(o.state).length || 2)))
|
|
780
|
+
const zero = new Array(dim).fill(0)
|
|
781
|
+
zero[0] = 1 // 归零后回到基态 |0…0>
|
|
782
|
+
return { ok: true, success: true, output: { state: zero, dimension: dim, voided: true, trace: 1 }, provenance: ['[BP_ATOM_FUSION_VOID_Q_041] 归零到基态'] }
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// ── 协议域(6 条)────────────────────────────────────────────
|
|
786
|
+
/** 协议执行器通用骨架:参数 → 状态机执行结果 */
|
|
787
|
+
function makeProtocolRunner(atomId, layers) {
|
|
788
|
+
return function (input) {
|
|
789
|
+
const o = input || {}
|
|
790
|
+
const steps = toArr(o.steps || o.phases || layers)
|
|
791
|
+
const state = { phase: 0, log: [], ok: true }
|
|
792
|
+
for (let i = 0; i < steps.length; i++) {
|
|
793
|
+
const s = steps[i]
|
|
794
|
+
const name = typeof s === 'string' ? s : String((s && (s.name || s.phase)) || ('step' + i))
|
|
795
|
+
// 每步校验:若参数里给了该步的 satisfied=false,则协议中止
|
|
796
|
+
const key = 'ok_' + name
|
|
797
|
+
const passed = o[key] !== false
|
|
798
|
+
state.log.push({ step: i + 1, name, passed })
|
|
799
|
+
if (!passed) { state.ok = false; state.phase = i; break }
|
|
800
|
+
state.phase = i + 1
|
|
801
|
+
}
|
|
802
|
+
return {
|
|
803
|
+
ok: state.ok, success: state.ok,
|
|
804
|
+
output: { completed: state.ok, reachedPhase: state.phase, totalPhases: steps.length, log: state.log, protocol: atomId, layers },
|
|
805
|
+
provenance: ['[' + atomId + '] 状态机执行'],
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
const protoQCP = makeProtocolRunner('BP_ATOM_PROTO_QCP_049', ['量子层协商', '共契条款校验', '执行'])
|
|
810
|
+
const protoBCP = makeProtocolRunner('BP_ATOM_PROTO_BCP_050', ['生物层协商', '共契条款校验', '执行'])
|
|
811
|
+
const protoCCP = makeProtocolRunner('BP_ATOM_PROTO_CCP_051', ['宇宙层协商', '共契条款校验', '执行'])
|
|
812
|
+
const protoQBCP = makeProtocolRunner('BP_ATOM_PROTO_QBCP_052', ['量子层', '生物层', '跨层共契', '执行'])
|
|
813
|
+
const protoBCCP = makeProtocolRunner('BP_ATOM_PROTO_BCCP_053', ['生物层', '宇宙层', '跨层共契', '执行'])
|
|
814
|
+
const protoQCCP = makeProtocolRunner('BP_ATOM_PROTO_QCCP_054', ['量子层', '宇宙层', '跨层共契', '执行'])
|
|
815
|
+
|
|
816
|
+
// ── 资源/创造域 ────────────────────────────────────────────────
|
|
817
|
+
/** 思想乘法器:原始想法(多个)→ 组合放大的方案集(按互补度排序) */
|
|
818
|
+
function resIdeaMultiply(input) {
|
|
819
|
+
const o = input || {}
|
|
820
|
+
const ideas = toArr(o.ideas || o.thoughts || o.seeds).map(x => typeof x === 'string' ? { text: x } : x)
|
|
821
|
+
if (ideas.length < 2) return { ok: false, success: false, reason: 'NEED_TWO_IDEAS', message: '至少需要 2 个想法' }
|
|
822
|
+
const tags = ideas.map(i => new Set(toArr((i && (i.tags || i.keywords)) || []).map(String)))
|
|
823
|
+
const pairs = []
|
|
824
|
+
for (let i = 0; i < ideas.length; i++) for (let j = i + 1; j < ideas.length; j++) {
|
|
825
|
+
const inter = [...tags[i]].filter(t => tags[j].has(t)).length
|
|
826
|
+
const union = new Set([...tags[i], ...tags[j]]).size || 1
|
|
827
|
+
const overlap = inter / union
|
|
828
|
+
// 互补度 = 1 - 重叠(越不同越互补),但需有少量交集才可组合
|
|
829
|
+
const complement = r4(1 - overlap)
|
|
830
|
+
const viable = inter >= 1 || (tags[i].size && tags[j].size && overlap < 0.9)
|
|
831
|
+
pairs.push({ a: i, b: j, overlap: r4(overlap), complement, viable })
|
|
832
|
+
}
|
|
833
|
+
const ranked = pairs.filter(p => p.viable).sort((a, b) => b.complement - a.complement)
|
|
834
|
+
return { ok: true, success: true, output: { combos: ranked, count: ranked.length, ideaCount: ideas.length, rule: '互补度 = 1 - Jaccard(标签)' }, provenance: ['[BP_ATOM_RES_IDEA_MULTIPLY_056] 组合互补度排序'] }
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/** 涌现触发器:用户状态(多维指标)→ 是否触发涌现 + 提示 */
|
|
838
|
+
function resEmergeTrigger(input) {
|
|
839
|
+
const o = input || {}
|
|
840
|
+
const dims = o.state || o.metrics || o
|
|
841
|
+
const keys = Object.keys(dims).filter(k => typeof dims[k] === 'number')
|
|
842
|
+
if (!keys.length) return { ok: false, success: false, reason: 'NO_NUMERIC_STATE', message: '缺少数值状态' }
|
|
843
|
+
const vals = keys.map(k => dims[k])
|
|
844
|
+
const m = mean(vals), sd = std(vals)
|
|
845
|
+
// 涌现条件:至少一个维度显著偏离均值(>1σ)且整体方差足够大
|
|
846
|
+
const outliers = keys.map((k, i) => {
|
|
847
|
+
const z = sd ? (vals[i] - m) / sd : 0
|
|
848
|
+
return { dim: k, value: r4(vals[i]), z: isFinite(z) ? r4(z) : 0 }
|
|
849
|
+
})
|
|
850
|
+
.filter(x => Math.abs(x.z) >= 1)
|
|
851
|
+
const trigger = outliers.length >= 1 && sd > 0.15 * (Math.abs(m) || 1)
|
|
852
|
+
return {
|
|
853
|
+
ok: true, success: true,
|
|
854
|
+
output: { trigger, outliers, mean: r4(m), std: r4(sd),
|
|
855
|
+
hint: trigger ? '检测到状态偏移,建议启动涌现流程(组合互补想法)' : '状态平稳,无需触发' },
|
|
856
|
+
provenance: ['[BP_ATOM_RES_EMERGE_TRIGGER_057] z-score ≥1 且 σ>15%'],
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/** 元思想引导器:思维定式(特征标记)→ 思考策略(按规则库匹配) */
|
|
861
|
+
function resMetaGuide(input) {
|
|
862
|
+
const o = input || {}
|
|
863
|
+
const txt = typeof input === 'string' ? input : String(o.text || o.pattern || o.description || JSON.stringify(o))
|
|
864
|
+
const RULES = [
|
|
865
|
+
{ key: /非黑即白|要么|只能|唯一/, strategy: '概念消融:寻找第三选项与灰度区间', thought: '耦生辩证' },
|
|
866
|
+
{ key: /绝对|必然|一定|从不/, strategy: '如是校验:要求给证据与反例', thought: '如是存在' },
|
|
867
|
+
{ key: /凭感觉|直觉|大概/, strategy: '直觉度量:把直觉拆成分项打分', thought: '直觉度量' },
|
|
868
|
+
{ key: /以前一直|习惯了|照旧/, strategy: '第一性原理:剥离惯例,从事实基元重建', thought: '溯源' },
|
|
869
|
+
{ key: /不知道|没想过|随便/, strategy: '问题无限溯源:连问 5 个为什么', thought: '溯源' },
|
|
870
|
+
{ key: /做不完|没时间|太忙/, strategy: '最小干预:找杠杆点,只改一处', thought: '边界' },
|
|
871
|
+
{ key: /都重要|全都想要/, strategy: '价值排序:用 28 维权重做取舍', thought: '价值' },
|
|
872
|
+
]
|
|
873
|
+
const hits = RULES.filter(r => r.key.test(txt)).map(r => ({ strategy: r.strategy, thought: r.thought }))
|
|
874
|
+
if (!hits.length) hits.push({ strategy: '无匹配定式:建议直接做 F0 第一性原理拆解', thought: '溯源' })
|
|
875
|
+
return { ok: true, success: true, output: { strategies: hits, count: hits.length, inputLength: txt.length }, provenance: ['[BP_ATOM_RES_META_GUIDE_060] 定式规则库匹配'] }
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/** 对话孕育器:对话历史 → 灵感种子(高频概念 + 未解决问题) */
|
|
879
|
+
function resDialogue(input) {
|
|
880
|
+
const o = input || {}
|
|
881
|
+
const msgs = toArr(o.history || o.messages || o.dialogue)
|
|
882
|
+
const text = msgs.map(m => typeof m === 'string' ? m : String((m && (m.text || m.content)) || '')).join('\n')
|
|
883
|
+
const cnt = {}
|
|
884
|
+
for (const t of (text.match(/[\u4e00-\u9fa5]{2,4}|[a-z]{4,}/gi) || [])) cnt[t] = (cnt[t] || 0) + 1
|
|
885
|
+
const seeds = Object.keys(cnt).filter(k => cnt[k] >= 2).map(k => ({ seed: k, freq: cnt[k] })).sort((a, b) => b.freq - a.freq).slice(0, 15)
|
|
886
|
+
const questions = (text.match(/[^。?!\n]{4,60}[??]/g) || []).slice(0, 10).map(q => q.trim())
|
|
887
|
+
return { ok: true, success: true, output: { seeds, count: seeds.length, openQuestions: questions, messageCount: msgs.length }, provenance: ['[BP_ATOM_RES_DIALOGUE_062] 高频词 + 未答问句'] }
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
/** 量子态锁:创意(内容)→ 量子证明(内容哈希 + 相位指纹) */
|
|
891
|
+
function resQuantumLock(input) {
|
|
892
|
+
const crypto = require('crypto')
|
|
893
|
+
const o = input || {}
|
|
894
|
+
const content = typeof input === 'string' ? input : JSON.stringify(o.idea || o.content || o.data || o)
|
|
895
|
+
const h = crypto.createHash('sha256').update(content).digest()
|
|
896
|
+
const phases = []
|
|
897
|
+
for (let i = 0; i < 8; i++) phases.push(r4(h[i] / 255 * 2 * Math.PI))
|
|
898
|
+
return { ok: true, success: true, output: { proof: h.toString('hex').slice(0, 32), phases, fingerprint: phases.map(p => Math.round(p * 100) % 360).join('-'), length: content.length }, provenance: ['[BP_ATOM_RES_QUANTUM_LOCK_063] SHA-256 + 相位指纹'] }
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/** 耦生增强器:当前耦生(各因子)→ 增强策略(按最小因子优先) */
|
|
902
|
+
function resCouplingEnhance(input) {
|
|
903
|
+
const o = input || {}
|
|
904
|
+
const f = o.factors || o.coupling || o
|
|
905
|
+
const keys = Object.keys(f).filter(k => typeof f[k] === 'number')
|
|
906
|
+
if (!keys.length) return { ok: false, success: false, reason: 'NO_FACTORS', message: '缺少耦生因子' }
|
|
907
|
+
const factors = keys.map(k => ({ name: k, value: r4(clamp01(f[k])) }))
|
|
908
|
+
const product = factors.reduce((s, x) => s * x.value, 1)
|
|
909
|
+
const sorted = factors.slice().sort((a, b) => a.value - b.value)
|
|
910
|
+
const strategies = sorted.slice(0, 3).map(x => ({
|
|
911
|
+
factor: x.name, current: x.value,
|
|
912
|
+
action: x.value < 0.3 ? '优先补齐(拖累整体)' : x.value < 0.6 ? '重点提升' : '保持',
|
|
913
|
+
// 边际收益:把该因子提到 1 能使积提升多少倍
|
|
914
|
+
gainIfOne: r4((1 / (x.value || 1e-6)) - 1),
|
|
915
|
+
}))
|
|
916
|
+
return { ok: true, success: true, output: { coupling: r4(product), factors, strategies, level: product > 1 ? '超导态' : product > 0.7 ? '深度融合' : product > 0.5 ? '协同工作' : '初步接触' }, provenance: ['[BP_ATOM_RES_COUPLING_ENHANCE_065] 乘积模型 + 最小因子优先'] }
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
// ── 导出 ──────────────────────────────────────────────────────
|
|
920
|
+
const IMPL = {
|
|
921
|
+
ATOM_POLICY_MONEY_102: policyMoney,
|
|
922
|
+
ATOM_POLICY_MATCH_103: policyMatch,
|
|
923
|
+
ATOM_POLICY_DEADLINE_104: policyDeadline,
|
|
924
|
+
ATOM_POLICY_COMPARE_106: policyCompare,
|
|
925
|
+
ATOM_POLICY_APPLY_108: policyApply,
|
|
926
|
+
ATOM_TECH_MAP_203: techMap,
|
|
927
|
+
ATOM_TECH_ROADMAP_205: techRoadmap,
|
|
928
|
+
ATOM_NEWS_HOOK_301: newsHook,
|
|
929
|
+
ATOM_SECURITY_ENCRYPT_603: securityEncrypt,
|
|
930
|
+
ATOM_SECURITY_DECRYPT_604: securityDecrypt,
|
|
931
|
+
ATOM_SECURITY_VERIFY_605: securityVerify,
|
|
932
|
+
ATOM_CONTRIB_PROVE_206: contribProve,
|
|
933
|
+
ATOM_SCIENTIFIC_PIPE_210: scientificPipe,
|
|
934
|
+
ATOM_SCIENTIFIC_PAPER_211: scientificPaper,
|
|
935
|
+
ATOM_SCIENTIFIC_EXP_212: scientificExp,
|
|
936
|
+
ATOM_PARALLEL_SIM_214: parallelSim,
|
|
937
|
+
ATOM_PARALLEL_TUNE_215: parallelTune,
|
|
938
|
+
ATOM_SYMBOL_EXPLAIN_205: symbolExplain,
|
|
939
|
+
ATOM_SYMBOL_VISUAL_206: symbolVisual,
|
|
940
|
+
ATOM_SWARM_CONSENSUS_217: swarmConsensus,
|
|
941
|
+
ATOM_TRAFFIC_COORD_207: trafficCoord,
|
|
942
|
+
ATOM_PEOPLE_INTENT_003: peopleIntent,
|
|
943
|
+
ATOM_PEOPLE_TARGET_004: peopleTarget,
|
|
944
|
+
ATOM_PEOPLE_SILENT_005: peopleSilent,
|
|
945
|
+
ATOM_PEOPLE_QUANTUM_008: peopleQuantum,
|
|
946
|
+
ATOM_PEOPLE_SUPERPOSITION_009: peopleSuperposition,
|
|
947
|
+
ATOM_PEOPLE_COLLAPSE_010: peopleCollapse,
|
|
948
|
+
ATOM_PEOPLE_FEEDBACK_011: peopleFeedback,
|
|
949
|
+
ATOM_PEOPLE_LEARN_012: peopleLearn,
|
|
950
|
+
ATOM_QUANTUM_ENCODE_013: quantumEncode,
|
|
951
|
+
ATOM_QUANTUM_EXECUTE_014: quantumExecute,
|
|
952
|
+
ATOM_QUANTUM_SIMULATE_015: quantumSimulate,
|
|
953
|
+
ATOM_QUANTUM_AMPLIFY_016: quantumAmplify,
|
|
954
|
+
ATOM_QUANTUM_PROTECT_018: quantumProtect,
|
|
955
|
+
ATOM_QUANTUM_MEASURE_019: quantumMeasure,
|
|
956
|
+
ATOM_BIO_EMOTION_022: bioEmotion,
|
|
957
|
+
ATOM_COSMIC_LEGACY_035: cosmicLegacy,
|
|
958
|
+
ATOM_COSMIC_RECEIVE_036: cosmicReceive,
|
|
959
|
+
ATOM_FUSION_Q_C_039: fusionQC,
|
|
960
|
+
ATOM_FUSION_VOID_Q_041: fusionVoidQ,
|
|
961
|
+
ATOM_PROTO_QCP_049: protoQCP,
|
|
962
|
+
ATOM_PROTO_BCP_050: protoBCP,
|
|
963
|
+
ATOM_PROTO_CCP_051: protoCCP,
|
|
964
|
+
ATOM_PROTO_QBCP_052: protoQBCP,
|
|
965
|
+
ATOM_PROTO_BCCP_053: protoBCCP,
|
|
966
|
+
ATOM_PROTO_QCCP_054: protoQCCP,
|
|
967
|
+
ATOM_RES_IDEA_MULTIPLY_056: resIdeaMultiply,
|
|
968
|
+
ATOM_RES_EMERGE_TRIGGER_057: resEmergeTrigger,
|
|
969
|
+
ATOM_RES_META_GUIDE_060: resMetaGuide,
|
|
970
|
+
ATOM_RES_DIALOGUE_062: resDialogue,
|
|
971
|
+
ATOM_RES_QUANTUM_LOCK_063: resQuantumLock,
|
|
972
|
+
ATOM_RES_COUPLING_ENHANCE_065: resCouplingEnhance,
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
module.exports = { IMPL, count: Object.keys(IMPL).length }
|