Fix preset publication, volatile settings and client form binding

This commit is contained in:
oliver 2026-10-11 12:54:02 +08:00
parent fe0fe70d33
commit 0b9a3a82bc
66 changed files with 8546 additions and 3690 deletions

View file

@ -6,6 +6,8 @@
* even when Bun emits them as separate ESM files.
*/
import { DEFAULT_CAPABILITY_GROUPS } from './capability-groups.ts'
import { normalizeApiUrl } from './http.ts'
import type { NetxToolConnection } from './tools.ts'
type Listener = () => void
@ -33,8 +35,11 @@ function store(): Store {
*/
export function publishNetxConnection(next: NetxToolConnection): void {
const state = store()
if (JSON.stringify(state.connection) === JSON.stringify(next)) return
state.connection = next
for (const listener of state.listeners) listener()
for (const listener of [...state.listeners]) {
try { listener() } catch (error) { console.error('netxops: connection subscriber failed', error) }
}
}
/** @returns the last published connection, if any. */
@ -42,6 +47,33 @@ export function getNetxConnection(): NetxToolConnection | undefined {
return store().connection
}
/**
* Fallback used when the Host bridge has not published yet (credentials still
* pending, or host apply delayed). Publication stays disabled until the Host
* publishes its actual policy, so a detached preset cannot bypass revocation.
*/
export function provisionalNetxConnection(): NetxToolConnection {
const envUrl = process.env.NETX_API_URL?.trim() ?? ''
const normalized = envUrl ? normalizeApiUrl(envUrl) : normalizeApiUrl('http://127.0.0.1:8890')
return {
toolsEnabled: false,
apiUrl: normalized.ok ? normalized.url : 'http://127.0.0.1:8890',
token: process.env.NETX_API_TOKEN?.trim() ?? '',
lang: (typeof process.env.NETX_LANG === 'string' && process.env.NETX_LANG.trim()
? process.env.NETX_LANG.trim()
: 'zh'),
toolCallTimeoutMs: 120_000,
groups: DEFAULT_CAPABILITY_GROUPS,
groupKbInPreset: true,
groupKbPublic: false,
}
}
/** Live snapshot, or a disabled provisional connection until the Host is ready. */
export function requireNetxConnection(): NetxToolConnection {
return getNetxConnection() ?? provisionalNetxConnection()
}
/**
* Subscribe to connection publishes (settings / credential remounts).
* @param listener - called synchronously after each publish.