@capyclaw/window-kit 0.1.0 → 0.1.2
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/client/client.js +160 -0
- package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +3 -1
- package/node_modules/@capyclaw/foundation/abi/index.mjs +15 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +8 -1
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +5 -2
- package/node_modules/@capyclaw/foundation/abi/version.mjs +63 -5
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +21 -4
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +11 -10
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +8 -1
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +5 -1
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +5 -2
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +4 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +29 -2
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +31 -3
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +12 -2
- package/node_modules/@capyclaw/foundation/package.json +1 -1
- package/node_modules/@capyclaw/plugin-kit/package.json +4 -1
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +5 -2
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +3 -82
- package/node_modules/@capyclaw/plugin-kit/src/ledger.mjs +151 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +13 -5
- package/package.json +1 -1
package/client/client.js
CHANGED
|
@@ -525,6 +525,166 @@ window.__ModuleLoader__.load({
|
|
|
525
525
|
WINDOW_KIT.inject = []
|
|
526
526
|
WINDOW_KIT.apply = function () {}
|
|
527
527
|
|
|
528
|
+
/* ── 可见性驱动器:表面生命周期标准的宿主侧真源 ────────────────────────
|
|
529
|
+
* 依据 docs/SURFACE-LIFECYCLE-STANDARD.md §3/§4/§5:宿主是表面可见性的唯一真源,
|
|
530
|
+
* 聚合三路探针 —— (a) 表面是否当前主内容 shown、(b) 顶层 tab 是否隐藏 tabHidden、
|
|
531
|
+
* (c) 窗口是否焦点 focused —— 算出四态经统一信号通道下发。iframe 内 document.hidden
|
|
532
|
+
* 只反映顶层 tab,探不到「被宿主藏起来」,所以表面可见性必须宿主显式告知。
|
|
533
|
+
*
|
|
534
|
+
* 协议常量与 C/packages/capyclaw-foundation/abi/visibility.mjs 逐字段一致:
|
|
535
|
+
* 本文件是 CJS 自包含叶子,无法 import 那份 ESM,只能内联同名常量,
|
|
536
|
+
* 由 test/visibility.test.mjs 的 parity 用例钉死漂移。改协议先改 abi 再同步这里。
|
|
537
|
+
*
|
|
538
|
+
* 边界:驱动器只管「算状态 + 发信号 + 预算计时」;display/卸载 iframe 的动作、
|
|
539
|
+
* postMessage 的具体投递由 portal-hud 拿 send/onDetach 回调去做。权威/桥接面
|
|
540
|
+
* (租约心跳、apply/ack)不受这里任何状态影响 —— 只有渲染面响应可见性。
|
|
541
|
+
*/
|
|
542
|
+
WINDOW_KIT.visibility = {
|
|
543
|
+
/* 宿主 → iframe:{ type, state, sid },targetOrigin 同源。 */
|
|
544
|
+
MESSAGE: 'capyclaw:workbench:visibility',
|
|
545
|
+
/* iframe → 宿主 的回执;detached 由宿主直接卸载,无需回执。 */
|
|
546
|
+
ACK_MESSAGE: 'capyclaw:workbench:visibility-ack',
|
|
547
|
+
STATES: ['foreground', 'background', 'hidden', 'detached'],
|
|
548
|
+
DEFAULTS: { detachAfterMs: 120000, visibilityAckTimeoutMs: 1500, hiddenGraceMs: 250 },
|
|
549
|
+
|
|
550
|
+
/* 纯函数:三路探针 → 目标态。!shown||tabHidden→hidden;可见非焦点→background;否则 foreground。 */
|
|
551
|
+
computeState: function (input) {
|
|
552
|
+
var p = input || {}
|
|
553
|
+
if (!p.shown || p.tabHidden) return 'hidden'
|
|
554
|
+
if (!p.focused) return 'background'
|
|
555
|
+
return 'foreground'
|
|
556
|
+
},
|
|
557
|
+
|
|
558
|
+
/*
|
|
559
|
+
* 状态机驱动器。opts = { probe, send, onDetach, onAckTimeout, now, timers, defaults, pollMs }:
|
|
560
|
+
* probe() 宿主探针,返回 { shown, tabHidden, focused }
|
|
561
|
+
* send(state) 状态变化时调用(含 start 的初始状态);投递 postMessage 是宿主的事
|
|
562
|
+
* onDetach() hidden 持续 detachAfterMs 后卸载回调,一次
|
|
563
|
+
* onAckTimeout(s) send(s) 后 visibilityAckTimeoutMs 内没 notifyAck(s),一次
|
|
564
|
+
* now / timers 可注入的时钟与定时器面(确定性测试用);默认 Date.now 与全局四件套
|
|
565
|
+
* defaults 覆盖 DEFAULTS 的个别预算;pollMs 轮询节奏,默认 200
|
|
566
|
+
*
|
|
567
|
+
* 行为契约:
|
|
568
|
+
* - 可见→hidden 去抖 hiddenGraceMs(宽限内回可见即取消);回可见态立即;
|
|
569
|
+
* - hidden 起算 detachAfterMs(now 计时,随 tick 检查)→ detached + onDetach 一次;
|
|
570
|
+
* - 每次 send(除 detached)挂一个 ack 等待,新 send 取代旧的等待;
|
|
571
|
+
* - start/stop 幂等;stop 清掉 interval/去抖/ack 全部定时器,无泄漏。
|
|
572
|
+
*/
|
|
573
|
+
createDriver: function (opts) {
|
|
574
|
+
var o = opts || {}
|
|
575
|
+
var base = WINDOW_KIT.visibility.DEFAULTS
|
|
576
|
+
var d = o.defaults || {}
|
|
577
|
+
var defaults = {
|
|
578
|
+
detachAfterMs: typeof d.detachAfterMs === 'number' ? d.detachAfterMs : base.detachAfterMs,
|
|
579
|
+
visibilityAckTimeoutMs: typeof d.visibilityAckTimeoutMs === 'number' ? d.visibilityAckTimeoutMs : base.visibilityAckTimeoutMs,
|
|
580
|
+
hiddenGraceMs: typeof d.hiddenGraceMs === 'number' ? d.hiddenGraceMs : base.hiddenGraceMs
|
|
581
|
+
}
|
|
582
|
+
var probe = typeof o.probe === 'function' ? o.probe : function () { return { shown: true, tabHidden: false, focused: true } }
|
|
583
|
+
var send = typeof o.send === 'function' ? o.send : function () {}
|
|
584
|
+
var onDetach = typeof o.onDetach === 'function' ? o.onDetach : function () {}
|
|
585
|
+
var onAckTimeout = typeof o.onAckTimeout === 'function' ? o.onAckTimeout : function () {}
|
|
586
|
+
var now = typeof o.now === 'function' ? o.now : Date.now
|
|
587
|
+
var timers = o.timers || { setTimeout: setTimeout, clearTimeout: clearTimeout, setInterval: setInterval, clearInterval: clearInterval }
|
|
588
|
+
var pollMs = typeof o.pollMs === 'number' ? o.pollMs : 200
|
|
589
|
+
|
|
590
|
+
var running = false
|
|
591
|
+
var current = null
|
|
592
|
+
/* hidden 起算时刻(now 计时);离开 hidden 即清。detached 预算随 tick 检查,
|
|
593
|
+
* 不另挂定时器 —— 少一个要对称清理的定时器,账目更干净。 */
|
|
594
|
+
var hiddenSince = null
|
|
595
|
+
var pollId = null
|
|
596
|
+
var graceId = null
|
|
597
|
+
var ackId = null
|
|
598
|
+
var ackState = null
|
|
599
|
+
|
|
600
|
+
function clearGrace() {
|
|
601
|
+
if (graceId !== null) { timers.clearTimeout(graceId); graceId = null }
|
|
602
|
+
}
|
|
603
|
+
function clearAck() {
|
|
604
|
+
if (ackId !== null) { timers.clearTimeout(ackId); ackId = null }
|
|
605
|
+
ackState = null
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/* 提交一个新状态:发信号、按状态挂/清预算。同态重复提交是空操作。 */
|
|
609
|
+
function commit(state) {
|
|
610
|
+
if (state === current) return
|
|
611
|
+
clearGrace()
|
|
612
|
+
current = state
|
|
613
|
+
hiddenSince = state === 'hidden' ? now() : null
|
|
614
|
+
send(state)
|
|
615
|
+
/* 新信号取代旧的回执等待;detached 由宿主直接卸载,无需回执。 */
|
|
616
|
+
clearAck()
|
|
617
|
+
if (state === 'detached') {
|
|
618
|
+
onDetach()
|
|
619
|
+
return
|
|
620
|
+
}
|
|
621
|
+
ackState = state
|
|
622
|
+
ackId = timers.setTimeout(function () {
|
|
623
|
+
ackId = null
|
|
624
|
+
var s = ackState
|
|
625
|
+
ackState = null
|
|
626
|
+
if (s !== null) onAckTimeout(s)
|
|
627
|
+
}, defaults.visibilityAckTimeoutMs)
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/* 一次探针 + 状态迁移。start 后的 interval 与手动 tick 都走这里。 */
|
|
631
|
+
function tick() {
|
|
632
|
+
if (!running) return
|
|
633
|
+
var target = WINDOW_KIT.visibility.computeState(probe())
|
|
634
|
+
if (target === 'hidden') {
|
|
635
|
+
if (current === 'hidden') {
|
|
636
|
+
/* 久藏卸载:hidden 持续 detachAfterMs 才进 detached(标准 §3 预算)。 */
|
|
637
|
+
if (hiddenSince !== null && now() - hiddenSince >= defaults.detachAfterMs) commit('detached')
|
|
638
|
+
return
|
|
639
|
+
}
|
|
640
|
+
/* 已 detached 就停在 detached,等回可见再重建,不重复卸载。 */
|
|
641
|
+
if (current === 'detached') return
|
|
642
|
+
/* 初始即 hidden:没有「可见→hidden」的抖动可言,直接提交。 */
|
|
643
|
+
if (current === null) { commit('hidden'); return }
|
|
644
|
+
/* 可见→hidden 去抖:宽限内回可见即取消;已在宽限中不重置截止。 */
|
|
645
|
+
if (graceId === null) {
|
|
646
|
+
graceId = timers.setTimeout(function () {
|
|
647
|
+
graceId = null
|
|
648
|
+
commit('hidden')
|
|
649
|
+
}, defaults.hiddenGraceMs)
|
|
650
|
+
}
|
|
651
|
+
return
|
|
652
|
+
}
|
|
653
|
+
/* 目标是可见态:取消去抖,回 foreground/background 立即(回来即重建,P4)。 */
|
|
654
|
+
clearGrace()
|
|
655
|
+
if (target !== current) commit(target)
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function start() {
|
|
659
|
+
if (running) return
|
|
660
|
+
running = true
|
|
661
|
+
current = null
|
|
662
|
+
hiddenSince = null
|
|
663
|
+
/* 初始状态当场发一次(契约:send 含 start 时的初始状态)。 */
|
|
664
|
+
tick()
|
|
665
|
+
pollId = timers.setInterval(tick, pollMs)
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function stop() {
|
|
669
|
+
if (!running) return
|
|
670
|
+
running = false
|
|
671
|
+
if (pollId !== null) { timers.clearInterval(pollId); pollId = null }
|
|
672
|
+
clearGrace()
|
|
673
|
+
clearAck()
|
|
674
|
+
hiddenSince = null
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/* iframe 回执:取消对应状态的回执等待(别的状态的等待不动)。 */
|
|
678
|
+
function notifyAck(state) {
|
|
679
|
+
if (ackId !== null && ackState === state) clearAck()
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
var driver = { notifyAck: notifyAck, tick: tick, start: start, stop: stop }
|
|
683
|
+
Object.defineProperty(driver, 'state', { enumerable: true, get: function () { return current } })
|
|
684
|
+
return driver
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
528
688
|
module.exports = WINDOW_KIT
|
|
529
689
|
return module.exports
|
|
530
690
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 资产引用契约的单一真源(公共 ABI 常量)。
|
|
3
|
+
*
|
|
4
|
+
* 互操作标准里资产有两副面孔,前缀在这里定义一次,内核门面与资产服务都引用它,
|
|
5
|
+
* 避免「路由实现」与「URL 门面」各写一份 '/assets/' 而漂移:
|
|
6
|
+
* - 字节仓 HTTP 路由前缀 ASSET_URL_PREFIX = '/assets/' (asset-service 挂 GET/HEAD)
|
|
7
|
+
* - 结构化正文里的引用前缀 ASSET_REF_PREFIX = '@asset/' (work-store 信封/正文只存引用)
|
|
8
|
+
*
|
|
9
|
+
* 这是契约常量,不是宿主服务名——放 ABI 层,应用与驱动都可 import。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** 资产字节仓的 HTTP 路由前缀(asset-service 实现,内核 os.assets.url 复用)。 */
|
|
13
|
+
export const ASSET_URL_PREFIX = '/assets/'
|
|
14
|
+
|
|
15
|
+
/** 结构化正文/产物信封里的内容寻址引用前缀(@asset/<sha256>)。 */
|
|
16
|
+
export const ASSET_REF_PREFIX = '@asset/'
|
|
17
|
+
|
|
18
|
+
/** 由资产 id 生成可加载 URL:/assets/<id>(id 做 URI 编码)。 */
|
|
19
|
+
export function assetUrl(id) {
|
|
20
|
+
return `${ASSET_URL_PREFIX}${encodeURIComponent(String(id))}`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 由资产 id 生成规范引用:@asset/<id>。 */
|
|
24
|
+
export function assetRef(id) {
|
|
25
|
+
return `${ASSET_REF_PREFIX}${String(id)}`
|
|
26
|
+
}
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* doc.model —— 插件提供文档模型(稳定 ID / 清单 / 定位 / 编辑计划 / 校验)
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
import { AbiError, ERROR_CODES } from './errors.mjs'
|
|
18
|
+
|
|
17
19
|
/** 桥协议版本:端点集合与状态机语义的主版本。不兼容变更才升 major。 */
|
|
18
20
|
export const BRIDGE_PROTOCOL_VERSION = '1.0.0'
|
|
19
21
|
|
|
@@ -40,7 +42,7 @@ export function declareBridge(input = {}) {
|
|
|
40
42
|
if (!VERSION_RE.test(protocol)) errors.push(`protocol 版本非法:${protocol}`)
|
|
41
43
|
if (model !== null && !VERSION_RE.test(model)) errors.push(`model 版本非法:${model}`)
|
|
42
44
|
if (editor !== 'required' && editor !== 'optional') errors.push(`editor 只能是 required/optional:${editor}`)
|
|
43
|
-
if (errors.length) throw new
|
|
45
|
+
if (errors.length) throw new AbiError(ERROR_CODES.E_SCHEMA, `桥声明非法:${errors.join(';')}`, { errors })
|
|
44
46
|
return Object.freeze({ id, protocol, model, editor })
|
|
45
47
|
}
|
|
46
48
|
|
|
@@ -17,6 +17,8 @@ export {
|
|
|
17
17
|
defineDriver,
|
|
18
18
|
rawPlugin,
|
|
19
19
|
} from './spec.mjs'
|
|
20
|
+
/* createLoader 是宿主/测试侧装配 API(内核 createKernel 与一致性测试用),虽从 abi 导出,
|
|
21
|
+
* 应用插件不应直接调用——应用只用 createPlugin + 接口 builders(见 plugin-abi.md §二)。 */
|
|
20
22
|
export { createLoader } from './loader.mjs'
|
|
21
23
|
export {
|
|
22
24
|
BRIDGE_CAPABILITY,
|
|
@@ -29,6 +31,19 @@ export {
|
|
|
29
31
|
bridgeConsumerRequirements,
|
|
30
32
|
declareBridge,
|
|
31
33
|
} from './bridge.mjs'
|
|
34
|
+
export {
|
|
35
|
+
SURFACE_LIFECYCLE_VERSION,
|
|
36
|
+
SURFACE_LIFECYCLE_CAPABILITY_SUFFIX,
|
|
37
|
+
VISIBILITY_ACK_MESSAGE,
|
|
38
|
+
VISIBILITY_DEFAULTS,
|
|
39
|
+
VISIBILITY_MESSAGE,
|
|
40
|
+
VISIBILITY_STATES,
|
|
41
|
+
assertSurfaceLifecycle,
|
|
42
|
+
isVisibilityState,
|
|
43
|
+
normalizeVisibilityState,
|
|
44
|
+
surfaceLifecycleCapability,
|
|
45
|
+
} from './visibility.mjs'
|
|
46
|
+
export { ASSET_URL_PREFIX, ASSET_REF_PREFIX, assetUrl, assetRef } from './assets.mjs'
|
|
32
47
|
export { validateInterface } from './validate.mjs'
|
|
33
48
|
export { AbiError, ERROR_CODES, isAbiError, toAbiError } from './errors.mjs'
|
|
34
49
|
export { validateSchema } from './schema.mjs'
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { ABI_MAJOR, getInterfaceKind } from './kinds.mjs'
|
|
3
3
|
import { validateInterface } from './validate.mjs'
|
|
4
4
|
import { AbiError, ERROR_CODES, toAbiError } from './errors.mjs'
|
|
5
|
-
import { satisfies } from './version.mjs'
|
|
5
|
+
import { satisfies, isValidRange } from './version.mjs'
|
|
6
6
|
|
|
7
7
|
const thenable = (value) => value && typeof value.then === 'function'
|
|
8
8
|
|
|
@@ -164,6 +164,13 @@ export function createLoader({ adapters = {}, capabilities = {}, log = null, abi
|
|
|
164
164
|
plugins.delete(spec.name)
|
|
165
165
|
void previous.handle?.dispose?.()
|
|
166
166
|
}
|
|
167
|
+
/* 非法范围单独报 E_SCHEMA:旧实现把「range 写错」和「能力真缺」都归成
|
|
168
|
+
* E_CAP_MISSING「能力不可用」,误导排障。先区分两者。 */
|
|
169
|
+
const badRanges = spec.requires.filter((need) => need.name.startsWith('os.') && !isValidRange(need.range))
|
|
170
|
+
if (badRanges.length) {
|
|
171
|
+
throw error(ERROR_CODES.E_SCHEMA, `插件 ${spec.name} 能力范围非法:${badRanges.map((r) => r.raw).join(', ')}`,
|
|
172
|
+
{ plugin: spec.name, invalid: badRanges })
|
|
173
|
+
}
|
|
167
174
|
const missing = spec.requires.filter((need) => need.name.startsWith('os.')
|
|
168
175
|
&& !satisfies(capabilityMap()[need.name] || '', need.range))
|
|
169
176
|
if (missing.length) {
|
|
@@ -191,9 +191,12 @@ export function definePlugin(options = {}) {
|
|
|
191
191
|
const requiresEntries = requires.map((entry) => parseCapability(entry, 'requires'))
|
|
192
192
|
const providesEntries = provides.map((entry) => parseCapability(entry, 'provides'))
|
|
193
193
|
for (const entry of providesEntries) {
|
|
194
|
-
|
|
194
|
+
/* 白名单而非黑名单:provides 必须是完整 x.y.z(可带 prerelease)。旧实现只拒
|
|
195
|
+
* ^~<>= 前缀,'1.x' / '1.*' / 裸 '1' 漏网被当成版本发布,消费方 parseVersion
|
|
196
|
+
* 解析不了 → satisfies 恒 false,能力永远「不可用」。 */
|
|
197
|
+
if (!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(entry.range)) {
|
|
195
198
|
throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
|
|
196
|
-
`provides
|
|
199
|
+
`provides 必须写精确版本(x.y.z),不能写范围:${entry.raw}`, { entry: entry.raw })
|
|
197
200
|
}
|
|
198
201
|
}
|
|
199
202
|
|
|
@@ -2,25 +2,57 @@
|
|
|
2
2
|
* 版本与范围:能力声明用 `^1` / `^1.2.0` / `>=1.2` / `1.x` / 精确版本。
|
|
3
3
|
*
|
|
4
4
|
* 只实现这份 ABI 需要的语义,不引入 semver 依赖:
|
|
5
|
-
* - `^1.2.3` → >=1.2.3 且同主版本;`^0.2.3` → >=0.2.3
|
|
5
|
+
* - `^1.2.3` → >=1.2.3 且同主版本;`^0.2.3` → >=0.2.3 且同次版本;
|
|
6
|
+
* `^0.0.3` → >=0.0.3 且同补丁(0.0.x 约定,semver 标准)
|
|
6
7
|
* - `~1.2.3` → >=1.2.3 且同次版本
|
|
7
8
|
* - `1.x` / `1.*` → 同主版本
|
|
8
9
|
* - `>=` `>` `<=` `<` `=` 前缀
|
|
9
10
|
* - 裸版本 → 精确匹配(比较时忽略末位 0)
|
|
11
|
+
* - prerelease 保留并参与比较:1.0.0-beta < 1.0.0;带 prerelease 的版本只满足
|
|
12
|
+
* 同 [major,minor,patch] 且同样带 prerelease 的范围(semver 标准,避免预发布
|
|
13
|
+
* 版本被当成正式版本放行)。
|
|
10
14
|
*/
|
|
11
15
|
|
|
12
16
|
export function parseVersion(text) {
|
|
13
17
|
if (typeof text !== 'string') return null
|
|
14
|
-
const match = /^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-[0-9A-Za-z.-]+)?$/.exec(text.trim())
|
|
18
|
+
const match = /^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z.-]+))?$/.exec(text.trim())
|
|
15
19
|
if (!match) return null
|
|
16
|
-
|
|
20
|
+
const parts = [Number(match[1]), Number(match[2] || 0), Number(match[3] || 0)]
|
|
21
|
+
parts.prerelease = match[4] || null
|
|
22
|
+
return parts
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function comparePrerelease(a, b) {
|
|
26
|
+
/* 无 prerelease > 有 prerelease(1.0.0 > 1.0.0-beta)。 */
|
|
27
|
+
if (!a && !b) return 0
|
|
28
|
+
if (!a) return 1
|
|
29
|
+
if (!b) return -1
|
|
30
|
+
const left = a.split('.')
|
|
31
|
+
const right = b.split('.')
|
|
32
|
+
for (let i = 0; i < Math.max(left.length, right.length); i++) {
|
|
33
|
+
const x = left[i]
|
|
34
|
+
const y = right[i]
|
|
35
|
+
if (x === undefined) return -1
|
|
36
|
+
if (y === undefined) return 1
|
|
37
|
+
const xn = /^\d+$/.test(x)
|
|
38
|
+
const yn = /^\d+$/.test(y)
|
|
39
|
+
if (xn && yn) {
|
|
40
|
+
const d = Number(x) - Number(y)
|
|
41
|
+
if (d !== 0) return d < 0 ? -1 : 1
|
|
42
|
+
} else if (xn !== yn) {
|
|
43
|
+
return xn ? -1 : 1 // 数字标识符 < 字母数字标识符
|
|
44
|
+
} else if (x !== y) {
|
|
45
|
+
return x < y ? -1 : 1
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return 0
|
|
17
49
|
}
|
|
18
50
|
|
|
19
51
|
function compareParts(a, b) {
|
|
20
52
|
for (let i = 0; i < 3; i++) {
|
|
21
53
|
if (a[i] !== b[i]) return a[i] < b[i] ? -1 : 1
|
|
22
54
|
}
|
|
23
|
-
return
|
|
55
|
+
return comparePrerelease(a.prerelease || null, b.prerelease || null)
|
|
24
56
|
}
|
|
25
57
|
|
|
26
58
|
export function compareVersions(a, b) {
|
|
@@ -44,12 +76,23 @@ export function satisfies(version, range) {
|
|
|
44
76
|
if (raw.startsWith('^')) {
|
|
45
77
|
const base = parseVersion(raw.slice(1))
|
|
46
78
|
if (!base) return false
|
|
79
|
+
/* prerelease 只满足同 [major,minor,patch] 且带 prerelease 的范围(semver 标准),
|
|
80
|
+
* 避免 1.1.0-beta 被 ^1.0.0 当正式版本放行。 */
|
|
81
|
+
if (current.prerelease && (!base.prerelease
|
|
82
|
+
|| current[0] !== base[0] || current[1] !== base[1] || current[2] !== base[2])) return false
|
|
47
83
|
if (compareParts(current, base) < 0) return false
|
|
48
|
-
|
|
84
|
+
if (base[0] === 0) {
|
|
85
|
+
/* 0.0.x caret 收紧到补丁位:^0.0.3 只容 0.0.3,不容 0.0.9。 */
|
|
86
|
+
if (base[1] === 0) return current[0] === 0 && current[1] === 0 && current[2] === base[2]
|
|
87
|
+
return current[0] === 0 && current[1] === base[1]
|
|
88
|
+
}
|
|
89
|
+
return current[0] === base[0]
|
|
49
90
|
}
|
|
50
91
|
if (raw.startsWith('~')) {
|
|
51
92
|
const base = parseVersion(raw.slice(1))
|
|
52
93
|
if (!base) return false
|
|
94
|
+
if (current.prerelease && (!base.prerelease
|
|
95
|
+
|| current[0] !== base[0] || current[1] !== base[1] || current[2] !== base[2])) return false
|
|
53
96
|
return compareParts(current, base) >= 0 && current[0] === base[0] && current[1] === base[1]
|
|
54
97
|
}
|
|
55
98
|
if (raw.startsWith('>=')) {
|
|
@@ -80,3 +123,18 @@ export function satisfies(version, range) {
|
|
|
80
123
|
const exact = parseVersion(raw)
|
|
81
124
|
return Boolean(exact) && compareParts(current, exact) === 0
|
|
82
125
|
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 范围是否「语义合法」(区别于「合法但不满足」)。供加载器把非法范围单独报
|
|
129
|
+
* E_SCHEMA,而不是误归因成「能力不可用」误导排障。空范围视为「任意」,合法。
|
|
130
|
+
*/
|
|
131
|
+
export function isValidRange(range) {
|
|
132
|
+
const raw = String(range ?? '').trim()
|
|
133
|
+
if (!raw || raw === '*' || raw === 'x') return true
|
|
134
|
+
if (/^[\^~]/.test(raw)) return Boolean(parseVersion(raw.slice(1)))
|
|
135
|
+
if (/^(>=|<=)/.test(raw)) return Boolean(parseVersion(raw.slice(2)))
|
|
136
|
+
if (/^[><]/.test(raw)) return Boolean(parseVersion(raw.slice(1)))
|
|
137
|
+
if (raw.startsWith('=')) return isValidRange(raw.slice(1))
|
|
138
|
+
if (/^(\d+)(?:\.(\d+))?\.[x*]$/.test(raw)) return true
|
|
139
|
+
return Boolean(parseVersion(raw))
|
|
140
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 表面生命周期(surface lifecycle)的标准声明。
|
|
3
|
+
*
|
|
4
|
+
* 与 bridge.mjs 同层:只放「协议常量 + 声明校验」,不放运行时。
|
|
5
|
+
* 依据 docs/SURFACE-LIFECYCLE-STANDARD.md:
|
|
6
|
+
* - 宿主(portal-hud + window-kit)是表面可见性的唯一真源,经统一 postMessage
|
|
7
|
+
* 信号通道下发给 iframe;iframe 内 document.hidden 只反映顶层 tab,不能用来
|
|
8
|
+
* 推断「被宿主藏起来」,所以表面可见性必须宿主显式告知。
|
|
9
|
+
* - 只有渲染面响应可见性;权威/数据面与桥接/仲裁面(租约心跳、apply/ack)
|
|
10
|
+
* 永远在线,与可见性无关。
|
|
11
|
+
*
|
|
12
|
+
* 消费方:
|
|
13
|
+
* - 宿主侧:@capyclaw/window-kit 的 visibility 驱动器(CJS 自包含叶子,无法
|
|
14
|
+
* import 本 ESM 模块,内联同名常量,由 parity 测试钉死一致)。
|
|
15
|
+
* - iframe 侧:@capyclaw/document-workbench/client 的 visibility 订阅器,
|
|
16
|
+
* 以及各编辑器前端(ppt/canvas/browser)。
|
|
17
|
+
* - 注册面:suite 清单 provides.capabilities 的 <ns>.surfaceLifecycle。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { AbiError, ERROR_CODES } from './errors.mjs'
|
|
21
|
+
|
|
22
|
+
/** 表面生命周期契约版本:状态机语义与信号形状的主版本。不兼容变更才升 major。 */
|
|
23
|
+
export const SURFACE_LIFECYCLE_VERSION = '1.0.0'
|
|
24
|
+
|
|
25
|
+
/** 宿主 → iframe 的可见性信号:{ type, state, sid },targetOrigin 同源。 */
|
|
26
|
+
export const VISIBILITY_MESSAGE = 'capyclaw:workbench:visibility'
|
|
27
|
+
|
|
28
|
+
/** iframe → 宿主 的回执:{ type, state, sid }。detached 由宿主直接卸载,无需回执。 */
|
|
29
|
+
export const VISIBILITY_ACK_MESSAGE = 'capyclaw:workbench:visibility-ack'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 可见性状态(标准 §3):
|
|
33
|
+
* foreground 前台可见全速;background 可见非焦点(降速,SHOULD 档);
|
|
34
|
+
* hidden 不再绘制(渲染面必须挂起);detached 久藏卸载(影子编辑器接管)。
|
|
35
|
+
*/
|
|
36
|
+
export const VISIBILITY_STATES = Object.freeze(['foreground', 'background', 'hidden', 'detached'])
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 节奏/预算默认值(标准 §3/§4/§5)。运行时经 tuning 单一真源可调;
|
|
40
|
+
* 这里是协议默认,不是各插件私设的第二份。
|
|
41
|
+
* detachAfterMs hidden 持续多久才卸载 iframe(编辑器权威型可更长)
|
|
42
|
+
* visibilityAckTimeoutMs 信号发出后多久没回执算半挂起(宿主可强制重载)
|
|
43
|
+
* hiddenGraceMs shown→hidden 的去抖宽限,避免快速切换抖动
|
|
44
|
+
*/
|
|
45
|
+
export const VISIBILITY_DEFAULTS = Object.freeze({
|
|
46
|
+
detachAfterMs: 120000,
|
|
47
|
+
visibilityAckTimeoutMs: 1500,
|
|
48
|
+
hiddenGraceMs: 250,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
/** 能力名后缀:与 suite 清单 provides.capabilities 的 <ns>.surfaceLifecycle 对应。 */
|
|
52
|
+
export const SURFACE_LIFECYCLE_CAPABILITY_SUFFIX = 'surfaceLifecycle'
|
|
53
|
+
|
|
54
|
+
const STATE_SET = new Set(VISIBILITY_STATES)
|
|
55
|
+
|
|
56
|
+
/** 是否合法状态。 */
|
|
57
|
+
export function isVisibilityState(value) {
|
|
58
|
+
return typeof value === 'string' && STATE_SET.has(value)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 归一化:非法/缺省一律退回 foreground(宁可多渲染,不可假挂起)。 */
|
|
62
|
+
export function normalizeVisibilityState(value) {
|
|
63
|
+
return isVisibilityState(value) ? value : 'foreground'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 注册声明 → suite 清单 provides.capabilities(标准 §6)。 */
|
|
67
|
+
export function surfaceLifecycleCapability(ns, version = SURFACE_LIFECYCLE_VERSION) {
|
|
68
|
+
if (typeof ns !== 'string' || !/^[a-z0-9][a-z0-9-]*$/.test(ns)) {
|
|
69
|
+
throw new AbiError(ERROR_CODES.E_SCHEMA, `surfaceLifecycle 命名空间必须是 kebab-case:${ns}`)
|
|
70
|
+
}
|
|
71
|
+
return Object.freeze({ [`${ns}.${SURFACE_LIFECYCLE_CAPABILITY_SUFFIX}`]: version })
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 校验一份表面生命周期声明(注册契约,标准 §6)。
|
|
76
|
+
* 渲染面四钩子必须齐;editor-gated 观察必须声明无头兜底。
|
|
77
|
+
* @param {{ hooks?: string[], headlessObservation?: boolean }} input
|
|
78
|
+
*/
|
|
79
|
+
export function assertSurfaceLifecycle(input = {}) {
|
|
80
|
+
const required = ['onVisibility', 'suspendRender', 'resumeRender', 'rehydrate']
|
|
81
|
+
const hooks = Array.isArray(input.hooks) ? input.hooks : []
|
|
82
|
+
const issues = []
|
|
83
|
+
for (const hook of required) {
|
|
84
|
+
if (!hooks.includes(hook)) issues.push(`缺少渲染面钩子 ${hook}`)
|
|
85
|
+
}
|
|
86
|
+
if (input.headlessObservation !== true) {
|
|
87
|
+
issues.push('editor-gated 观察必须声明无头兜底(headlessObservation:true)')
|
|
88
|
+
}
|
|
89
|
+
return { ok: issues.length === 0, issues }
|
|
90
|
+
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* lifecycle-hook@1 的适配器。
|
|
3
3
|
*
|
|
4
|
-
* 运行时只执行 onStart / onStop
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* 运行时只执行 onStart / onStop。onInstall / onUpgrade 属于安装与升级流程:
|
|
5
|
+
* 这里把它们登记进 hook 注册表(os.hooks),保证「声明了就找得到」。
|
|
6
|
+
* 截至本版本仓内尚无自动 fire 它们的安装/升级消费者(见 plugin-abi.md §4.6)——
|
|
7
|
+
* 这是已声明的集成点,不是已落地的自动钩子。安装器可经 os.hooks.run('install'|'upgrade', args)
|
|
8
|
+
* 主动触发;run() 只提供调用 API,是否/何时调用由安装器决定。
|
|
7
9
|
*/
|
|
10
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
11
|
+
|
|
8
12
|
export const kind = 'lifecycle-hook@1'
|
|
9
13
|
|
|
10
14
|
export function createHookRegistry() {
|
|
@@ -12,7 +16,7 @@ export function createHookRegistry() {
|
|
|
12
16
|
install: [],
|
|
13
17
|
upgrade: [],
|
|
14
18
|
register(phase, entry) {
|
|
15
|
-
if (!this[phase]) throw new
|
|
19
|
+
if (!this[phase]) throw new AbiError(ERROR_CODES.E_SCHEMA, `未知生命周期阶段 ${phase}`)
|
|
16
20
|
this[phase].push(entry)
|
|
17
21
|
return () => {
|
|
18
22
|
const index = this[phase].indexOf(entry)
|
|
@@ -25,6 +29,19 @@ export function createHookRegistry() {
|
|
|
25
29
|
upgrade: this.upgrade.map((entry) => entry.plugin),
|
|
26
30
|
}
|
|
27
31
|
},
|
|
32
|
+
/**
|
|
33
|
+
* 安装/升级集成点:按登记顺序触发某阶段全部钩子,逐个 await。
|
|
34
|
+
* 仓内暂无自动调用方(见头注);这是给安装器(plugin-sync / plugin-drop / publisher)
|
|
35
|
+
* 预留的调用 API。未知阶段抛稳定错误码 E_SCHEMA。
|
|
36
|
+
*/
|
|
37
|
+
async run(phase, args = {}) {
|
|
38
|
+
if (!this[phase]) throw new AbiError(ERROR_CODES.E_SCHEMA, `未知生命周期阶段 ${phase}`)
|
|
39
|
+
const results = []
|
|
40
|
+
for (const entry of [...this[phase]]) {
|
|
41
|
+
results.push({ plugin: entry.plugin, result: await entry.run(args) })
|
|
42
|
+
}
|
|
43
|
+
return results
|
|
44
|
+
},
|
|
28
45
|
}
|
|
29
46
|
}
|
|
30
47
|
|
|
@@ -44,18 +44,19 @@ export function createRawAdapter({ log }) {
|
|
|
44
44
|
|
|
45
45
|
if (thenable(result)) {
|
|
46
46
|
let instance = {}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
})
|
|
47
|
+
/* apply 结算只做归一化,绝不在此 eagerly 跑 instance.start()——旧实现
|
|
48
|
+
* 在 compile 返回的 promise resolve 时就启动,绕过 loader 的 start 阶段与
|
|
49
|
+
* stopRequested:loader 正在回滚/已请求卸载时 raw 仍会起进程/监听。
|
|
50
|
+
* start 交给 loader 在 start 阶段显式触发(此时才尊重 stopRequested)。 */
|
|
51
|
+
const ready = Promise.resolve(result).then((value) => { instance = normalize(value) })
|
|
53
52
|
return {
|
|
54
|
-
start: () =>
|
|
53
|
+
start: async ({ signal } = {}) => {
|
|
54
|
+
await ready
|
|
55
|
+
/* 转发 loader 的卸载 signal,让 raw 实例的 start 也能感知取消。 */
|
|
56
|
+
return instance.start ? instance.start({ signal }) : undefined
|
|
57
|
+
},
|
|
55
58
|
dispose: async () => {
|
|
56
|
-
|
|
57
|
-
try { await started } catch { /* start 失败由 loader 记录 */ }
|
|
58
|
-
}
|
|
59
|
+
try { await ready } catch { /* apply 失败由 loader 记录 */ }
|
|
59
60
|
if (typeof instance.dispose === 'function') return instance.dispose()
|
|
60
61
|
},
|
|
61
62
|
}
|
|
@@ -62,14 +62,21 @@ export function createRuntimeDriverAdapter({ host, config, log }) {
|
|
|
62
62
|
...(impl.args || []),
|
|
63
63
|
]
|
|
64
64
|
|
|
65
|
+
/* keepAlive 由声明推导:restart.max===0 表示「不要自动重启」,对应 keepAlive:false。
|
|
66
|
+
* 旧实现恒 true,应用声明 restart:{max:0} 仍被 broker 拉起,切换/回收行为与声明不符。 */
|
|
67
|
+
const keepAlive = !(impl.restart && impl.restart.max === 0)
|
|
65
68
|
const serviceSpec = {
|
|
66
69
|
id: impl.id,
|
|
67
70
|
cmd,
|
|
68
71
|
ports: { main: port },
|
|
69
72
|
url: 'http://127.0.0.1:${PORT}/',
|
|
70
|
-
keepAlive
|
|
73
|
+
keepAlive,
|
|
71
74
|
}
|
|
72
75
|
if (impl.health?.path) serviceSpec.healthPath = impl.health.path
|
|
76
|
+
/* 声明的健康期望/超时与重启策略必须透传给 broker,不能被静默丢弃。 */
|
|
77
|
+
if (impl.health?.expect && typeof impl.health.expect === 'object') serviceSpec.expect = impl.health.expect
|
|
78
|
+
if (impl.health?.timeoutMs) serviceSpec.waitMs = impl.health.timeoutMs
|
|
79
|
+
if (impl.restart) serviceSpec.restart = impl.restart
|
|
73
80
|
if (impl.dir) {
|
|
74
81
|
serviceSpec.cwd = fileURLToPath(new URL(impl.dir, mountContext.context.baseUrl))
|
|
75
82
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* editorHost)的地方——它们属于 dsh 适配层,不属于 ABI。
|
|
7
7
|
*/
|
|
8
8
|
import { getService } from '../ctx-service.mjs'
|
|
9
|
+
import { HOST_SERVICE_CONTRACT } from './contract.mjs'
|
|
9
10
|
|
|
10
11
|
export const BASE_CAPABILITIES = Object.freeze({
|
|
11
12
|
'os.events': '1.0.0',
|
|
@@ -26,9 +27,12 @@ export function detectDshCapabilities(ctx) {
|
|
|
26
27
|
&& hasMethods(getService(ctx, 'tools'), 'register')) capabilities['os.tools'] = '1.0.0'
|
|
27
28
|
if (hasMethods(getService(ctx, 'serviceBroker'), 'start', 'stop')) capabilities['os.proc'] = '1.0.0'
|
|
28
29
|
if (capabilities['os.net'] && hasMethods(getService(ctx, 'editorHost'), 'createEditorHost')) capabilities['os.ui'] = '1.0.0'
|
|
29
|
-
if (hasMethods(getService(ctx, 'pluginManager'), '
|
|
30
|
+
if (hasMethods(getService(ctx, 'pluginManager'), ...HOST_SERVICE_CONTRACT['os.plugins'][1])) {
|
|
30
31
|
capabilities['os.plugins'] = '1.0.0'
|
|
31
32
|
}
|
|
32
33
|
if (hasMethods(getService(ctx, 'assets'), 'put', 'get')) capabilities['os.assets'] = '1.0.0'
|
|
34
|
+
// work-store 是可选持久化层(不在 core 六件内):探测到才 advertise os.works,
|
|
35
|
+
// 应用用 os.caps.has('os.works') 判断降级,不再 safeGet(ctx, 'workStore') 裸摸。
|
|
36
|
+
if (hasMethods(getService(ctx, 'workStore'), 'backendFor')) capabilities['os.works'] = '1.0.0'
|
|
33
37
|
return capabilities
|
|
34
38
|
}
|
|
@@ -3,7 +3,9 @@ import { parseVersion } from '../../abi/version.mjs'
|
|
|
3
3
|
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
4
4
|
|
|
5
5
|
const ACCESSORS = ['capabilities', 'log', 'skills', 'tools', 'http', 'proc', 'ui']
|
|
6
|
-
|
|
6
|
+
/** Host v1 能力→[访问器, 必需方法] 契约。导出为单一真源:内核门面与能力探测都引用它,
|
|
7
|
+
* 不再各自硬编码 os.plugins 的 dsh 方法名(listBundles/setPluginEnabled…)。 */
|
|
8
|
+
export const HOST_SERVICE_CONTRACT = {
|
|
7
9
|
'os.skills': ['skills', ['registerProvider']],
|
|
8
10
|
'os.tools': ['tools', ['register']],
|
|
9
11
|
'os.net': ['http', ['createApp', 'createSharedHost']],
|
|
@@ -12,6 +14,7 @@ const SERVICES = {
|
|
|
12
14
|
'os.ui': ['ui', ['createEditorHost']],
|
|
13
15
|
'os.plugins': ['plugins', ['listBundles', 'listPlugins', 'setBundleEnabled', 'setPluginEnabled']],
|
|
14
16
|
'os.assets': ['assets', ['put', 'get']],
|
|
17
|
+
'os.works': ['works', ['backendFor']],
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
function invalid(path, expectation) {
|
|
@@ -68,7 +71,7 @@ export function assertHostContract(host) {
|
|
|
68
71
|
if (typeof logger[method] !== 'function') invalid(`host.log().${method}`, 'must be a function')
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
|
-
for (const [capability, [accessor, methods]] of Object.entries(
|
|
74
|
+
for (const [capability, [accessor, methods]] of Object.entries(HOST_SERVICE_CONTRACT)) {
|
|
72
75
|
if (!Object.hasOwn(capabilities, capability)) continue
|
|
73
76
|
if (typeof host[accessor] !== 'function') {
|
|
74
77
|
invalid(`host.${accessor}`, `must be a function when advertising ${capability}`)
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
* web() { register({kind, path, handler}) => dispose, port } | undefined
|
|
16
16
|
* proc() { start(serviceSpec) => Promise<{errors?}>, stop(id) }
|
|
17
17
|
* ui() { createEditorHost({ id, dir, basePath, index }) }
|
|
18
|
+
* assets() { put, get, ... } | undefined —— 资产字节仓(asset-service)
|
|
19
|
+
* works() { backendFor, claimSessionMode, ... } | undefined —— 作品目录(work-store,
|
|
20
|
+
* 可选持久化层:缺席时内核 os.works 返回 null,应用自行降级内存态)
|
|
18
21
|
*
|
|
19
22
|
* `web()` 是宿主网页端口本身(dsh 即 ctx.webServer)。拿到它时,内核一律把 HTTP
|
|
20
23
|
* 挂到该端口的路径前缀下,不再另起端口;拿不到才退回 os.net 的独立监听。
|
|
@@ -46,5 +49,6 @@ export function createDshHost(ctx) {
|
|
|
46
49
|
ui: () => getService(ctx, 'editorHost'),
|
|
47
50
|
plugins: () => getService(ctx, 'pluginManager'),
|
|
48
51
|
assets: () => getService(ctx, 'assets'),
|
|
52
|
+
works: () => getService(ctx, 'workStore'),
|
|
49
53
|
}
|
|
50
54
|
}
|