合并记忆盖头:本人钥匙 whoami 定身份,网关请求盖 team/agent 头+钉死本人鉴权(2026-11-11 拆 key 收官件)
This commit is contained in:
1 parent
c987d68d05
commit
c685d7b104
1 file changed
+92
-2
+92
-2
@@ -57,6 +57,57 @@ function toolToken() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 模型通道钥匙(本人 sk-mem key):DSH 运行时已把凭据注入环境变量
|
||||
* (apiKeyEnv: MARKMAG_TEAM_KEY 同源,setup-account 写入的凭据即在此)。 */
|
||||
function teamKey() {
|
||||
return process.env.MARKMAG_TEAM_KEY || ''
|
||||
}
|
||||
|
||||
/** 记忆盖头只盖打到 TDB 网关的请求(同源不同路径的一律不碰,比如弹匣 API 本身)。 */
|
||||
const GATEWAY_PREFIXES = ['https://brain.sidaocai.cn/tdb-api', 'http://127.0.0.1:8096']
|
||||
|
||||
function matchesGateway(input) {
|
||||
let target
|
||||
try {
|
||||
const raw = typeof input === 'string' ? input : input instanceof URL ? input.href : input?.url
|
||||
if (typeof raw !== 'string') return false
|
||||
target = new URL(raw)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
return GATEWAY_PREFIXES.some((prefix) => {
|
||||
let parsed
|
||||
try {
|
||||
parsed = new URL(prefix)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
if (target.origin !== parsed.origin) return false
|
||||
const path = parsed.pathname
|
||||
if (path === '/') return true
|
||||
const pathname = target.pathname
|
||||
return pathname === path || (pathname.startsWith(path) && (path.endsWith('/') || pathname[path.length] === '/'))
|
||||
})
|
||||
}
|
||||
|
||||
/** 凭本人钥匙问弹匣要身份(部门组 + 本人记忆 agent)。拿不到返回 null。 */
|
||||
async function fetchIdentity() {
|
||||
const key = teamKey()
|
||||
if (!key) return null
|
||||
try {
|
||||
const res = await fetch(MARKMAG_BASE + '/api/agent/whoami', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ key }),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
})
|
||||
const data = await res.json().catch(() => null)
|
||||
return data && data.ok && data.team_id && data.agent_id ? data : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** 调弹匣工具面(同一个端点,agent 和面板共用;规则判定全在弹匣侧)。 */
|
||||
async function callMarkmag(tool, args) {
|
||||
const token = toolToken()
|
||||
@@ -209,11 +260,50 @@ export function apply(ctx) {
|
||||
)
|
||||
step('panel-api-registered')
|
||||
|
||||
// ── 3. 启动提示 ─────────────────────────────────────────
|
||||
// ── 3. 记忆盖头(2026-11-11 并入)──────────────────────
|
||||
// TDB 的记忆按 (团队, 本人 agent) 归账:本人钥匙 → 弹匣 whoami 拿身份 →
|
||||
// 凡打到 TDB 网关的请求盖 x-team-id / x-agent-id,Authorization 一并钉死为
|
||||
// 本人钥匙。拿不到身份(key 失效/网络不通)就原样放行,不拦使用,稍后重试。
|
||||
const memory = { identity: null, at: 0 }
|
||||
const refreshIdentity = async () => {
|
||||
if (memory.identity && Date.now() - memory.at < 30 * 60_000) return
|
||||
const id = await fetchIdentity()
|
||||
if (id) {
|
||||
memory.identity = id
|
||||
memory.at = Date.now()
|
||||
step('memory-identity-ready', { name: id.name, team: id.team_id, agent: id.agent_id })
|
||||
} else if (!memory.identity) {
|
||||
step('memory-identity-pending', { hint: 'key 未配置/未换成本人 key/弹匣不可达' })
|
||||
}
|
||||
}
|
||||
const originalFetch = globalThis.fetch
|
||||
const patchedFetch = (input, init) => {
|
||||
if (!memory.identity || !matchesGateway(input)) return originalFetch(input, init)
|
||||
const headers = new Headers(input instanceof Request ? input.headers : init?.headers)
|
||||
headers.set('x-team-id', memory.identity.team_id)
|
||||
headers.set('x-agent-id', memory.identity.agent_id)
|
||||
const key = teamKey()
|
||||
if (key) headers.set('Authorization', 'Bearer ' + key)
|
||||
if (input instanceof Request && init === undefined) return originalFetch(new Request(input, { headers }))
|
||||
return originalFetch(input, { ...init, headers })
|
||||
}
|
||||
globalThis.fetch = patchedFetch
|
||||
ctx.effect(() => {
|
||||
refreshIdentity()
|
||||
const timer = setInterval(refreshIdentity, 10 * 60_000)
|
||||
timer.unref?.()
|
||||
return () => {
|
||||
clearInterval(timer)
|
||||
if (globalThis.fetch === patchedFetch) globalThis.fetch = originalFetch
|
||||
}
|
||||
}, 'dsh-markmag: memory stamp')
|
||||
step('memory-stamp-mounted')
|
||||
|
||||
// ── 4. 启动提示 ─────────────────────────────────────────
|
||||
ctx.effect(() => {
|
||||
try {
|
||||
ctx.get('logger')?.info?.(
|
||||
'[dsh-markmag] 已挂载:四道菜模型通道(GLM-5.3 / GLM-5.3-Flash,凭据 ' + CREDENTIAL + ')+ 弹匣工具面 + 侧边栏面板'
|
||||
'[dsh-markmag] 已挂载:四道菜模型通道(GLM-5.3 / GLM-5.3-Flash,凭据 ' + CREDENTIAL + ')+ 弹匣工具面 + 侧边栏面板 + 记忆盖头(本人钥匙自动记部门组)'
|
||||
)
|
||||
} catch {}
|
||||
}, 'dsh-markmag: startup note')
|
||||
|
||||
Reference in new issue
Block a user