Skip to content

LokvisRuntimeImpl

Defined in: packages/runtime/src/runtime-impl.ts:40

Runtime 实现类,持有 5 个 managers 并委托方法

new LokvisRuntimeImpl(config?): LokvisRuntimeImpl

Defined in: packages/runtime/src/runtime-impl.ts:81

RuntimeConfig & InternalRuntimeInit = {}

LokvisRuntimeImpl

readonly version: "0.1.0" = RUNTIME_VERSION

Defined in: packages/runtime/src/runtime-impl.ts:41

Runtime 版本

LokvisRuntime.version


readonly eventBus: EventBus

Defined in: packages/runtime/src/runtime-impl.ts:42

事件总线

LokvisRuntime.eventBus

get status(): RuntimeStatus

Defined in: packages/runtime/src/runtime-impl.ts:140

当前状态

RuntimeStatus

当前状态

LokvisRuntime.status


get isPro(): boolean

Defined in: packages/runtime/src/runtime-impl.ts:141

是否为 Pro 模式(影响批量上限/并发槽位/workflow 数,W6.2)

boolean

是否为 Pro 模式(影响批量上限/并发槽位/workflow 数,W6.2)

LokvisRuntime.isPro


get plan(): Plan

Defined in: packages/runtime/src/runtime-impl.ts:142

用户订阅计划(G1)。比 isPro 更细粒度,用于 AI 调用计费判定。 isPro === (plan !== 'free'),二者保持一致。

Plan

用户订阅计划(G1)。比 isPro 更细粒度,用于 AI 调用计费判定。 isPro === (plan !== 'free'),二者保持一致。

LokvisRuntime.plan


get batch(): BatchProcessor

Defined in: packages/runtime/src/runtime-impl.ts:143

批量处理器(W6.1:并发控制 + 进度 + 失败重试)

BatchProcessor

批量处理器(W6.1:并发控制 + 进度 + 失败重试)

LokvisRuntime.batch

_registerMetadataReader(name, reader): void

Defined in: packages/runtime/src/runtime-impl.ts:67

注册元数据读取器(由 PluginContext.registerMetadataReader 转发,内部 API)

string

MetadataReader

void


_registerPanel(panel): void

Defined in: packages/runtime/src/runtime-impl.ts:76

注册 UI Panel 定义(由 PluginContext.registerPanel 转发,内部 API)。 同 id 重复注册覆盖前者(与 registerMetadataReader 语义一致,支持热更新), 并同步发射 panel:registered 事件,UI 层订阅后刷新面板列表。

PanelDefinition

void


run(workflow, inputs, options?): Promise<WorkflowResult>

Defined in: packages/runtime/src/runtime-impl.ts:146

运行工作流

Workflow

string[] | Asset[]

RunOptions

Promise<WorkflowResult>

LokvisRuntime.run


cancel(workflowId): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:150

取消运行

string

Promise<void>

LokvisRuntime.cancel


pause(workflowId): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:154

暂停运行

string

Promise<void>

LokvisRuntime.pause


resume(workflowId): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:158

恢复运行

string

Promise<void>

LokvisRuntime.resume


disposeWorkflow(workflowId): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:162

销毁工作流的运行时状态(取消运行 + 清空历史栈 + 回收历史 outputs 资产)。

修复 review 报告:原接口无清理入口,长会话累积导致 historyStacks Map 与 AssetStore 中孤儿资产泄漏。ui-react 应在 Workspace 卸载时调用。

string

Promise<void>

LokvisRuntime.disposeWorkflow


dispose(): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:188

销毁整个 Runtime(W21.6)。

顺序:

  1. 标记 disposed(阻止后续 run/cancel,防止清理期间新请求进入)
  2. executor.cancelAll() —— 取消所有运行中 workflow 的 AbortController
  3. batchProcessor.dispose() —— 标记所有非终态 job 为 cancelled + 清理订阅
  4. historyManager.disposeAll() —— 清空所有历史栈(reset 触发 onEvict → assetStore.remove 回收 outputs 资产);TD-2.1 改为 await 等待 persistHistory 删除 IDB 记录落地
  5. 清理 metadataReaders 与 panels(释放插件注册的引用)
  6. 若 ownsAssetStore(工厂创建而非注入):调用 assetStore.dispose?.() 关闭 Dexie 连接 / 清空内存 Map。注入路径由消费方自行管理。

不清理:

  • historyStore(由消费方注入或工厂创建,Dexie 连接由浏览器 GC 处理)
  • eventBus listeners(允许外部已订阅的 listener 仍收到最后一批 workflow:cancelled 等事件;若需要清空可单独调 eventBus.clear)

幂等:重复调用为 no-op。

Promise<void>

LokvisRuntime.dispose


history(workflowId): Promise<HistoryEntry[]>

Defined in: packages/runtime/src/runtime-impl.ts:209

获取工作流的执行历史

string

Promise<HistoryEntry[]>

LokvisRuntime.history


getHistoryState(workflowId): Promise<{ entries: HistoryEntry[]; cursor: number; }>

Defined in: packages/runtime/src/runtime-impl.ts:210

获取工作流历史状态(条目 + 当前游标)。 游标 -1 表示无已应用条目(初始状态);i 表示第 i 条已应用。 比 history() 多返回 cursor,UI 据此高亮当前步骤。

string

Promise<{ entries: HistoryEntry[]; cursor: number; }>

LokvisRuntime.getHistoryState


undo(workflowId): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:213

撤销一步

string

Promise<void>

LokvisRuntime.undo


redo(workflowId): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:214

重做一步

string

Promise<void>

LokvisRuntime.redo


jumpTo(workflowId, index): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:215

跳转到指定历史条目(按时间顺序的索引,-1 表示回到初始)。 用于 HistoryPanel 点击条目直接跳转,等价于连续 undo/redo 到目标位置。 越界或游标未变时为 no-op。

string

number

Promise<void>

LokvisRuntime.jumpTo


importAsset(source): Promise<string>

Defined in: packages/runtime/src/runtime-impl.ts:218

导入资产

AssetSource

Promise<string>

LokvisRuntime.importAsset


getAsset(id): Promise<Asset>

Defined in: packages/runtime/src/runtime-impl.ts:219

获取资产

string

Promise<Asset>

LokvisRuntime.getAsset


exportAsset(id, format?): Promise<Blob>

Defined in: packages/runtime/src/runtime-impl.ts:220

导出资产为 Blob

string

string

Promise<Blob>

LokvisRuntime.exportAsset


readAssetExif(id): Promise<ExifData | null>

Defined in: packages/runtime/src/runtime-impl.ts:221

读取 image 资产的 EXIF 元数据(W7.3/7.4)。

长期方案(MetadataReader 依赖反转):Runtime 持有 plugin-image 通过 ctx.registerMetadataReader('image.read-exif', fn) 注册的读取器引用, 按名调用。Plugin 未安装时优雅降级返回 null。 readExif 实现位于 plugin-image(Capability 层),不进 engine-image (不符合 Engine 层 Blob↔Blob 纯函数约束)。 UI 通过此方法访问 EXIF,不直接依赖 Engine/Plugin 包(五层架构单向依赖)。

string

资产 ID(须为 image 类型)

Promise<ExifData | null>

ExifData;非 image / 无 EXIF / 解析失败 / reader 未注册返回 null

LokvisRuntime.readAssetExif


readAssetImageMetadata(id): Promise<ImageMetadata | null>

Defined in: packages/runtime/src/runtime-impl.ts:222

读取 image 资产的 dimensions/format 元数据。

走 MetadataReader 机制(与 readAssetExif 同一设计):Runtime 持有 plugin-image 通过 ctx.registerMetadataReader('image.read-metadata', fn) 注册的 reader 引用,按名调用。reader 内部调 engine-image/node 的 getMetadata(sharp .metadata())。

用途:mcp-server 在 image tool 处理完成后,读取输出 Blob 的精确尺寸 用于结果文本报告。Plugin 未安装时优雅降级返回 null。

架构意义:使 mcp-server 不再直接 import @lokvis/engine-image(违反 五层架构单向依赖),改为通过 Runtime 间接调用(见 A1 修复)。

string

资产 ID(须为 image 类型)

Promise<ImageMetadata | null>

ImageMetadata;非 image / reader 未注册 / 解析失败返回 null

LokvisRuntime.readAssetImageMetadata


readAssetPdfInfo(id): Promise<PdfInfo | null>

Defined in: packages/runtime/src/runtime-impl.ts:223

读取 pdf 资产的页数。

走 MetadataReader 机制:Runtime 持有 plugin-pdf 通过 ctx.registerMetadataReader('pdf.read-info', fn) 注册的 reader 引用, 按名调用。reader 内部调 engine-pdf 的 getPdfInfo(pdf-lib getPageCount)。

用途:mcp-server 在 pdf tool 处理完成后,读取输出 Blob 的页数用于结果 文本报告。Plugin 未安装时优雅降级返回 null。

架构意义:使 mcp-server 不再直接 import @lokvis/engine-pdf(违反 五层架构单向依赖),改为通过 Runtime 间接调用(见 A1 修复)。

string

资产 ID(须为 pdf 类型)

Promise<PdfInfo | null>

PdfInfo;非 pdf / reader 未注册 / 解析失败返回 null

LokvisRuntime.readAssetPdfInfo


removeAsset(id): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:224

删除资产

string

Promise<void>

LokvisRuntime.removeAsset


listAssets(): Promise<Asset[]>

Defined in: packages/runtime/src/runtime-impl.ts:225

列出所有资产

Promise<Asset[]>

LokvisRuntime.listAssets


getStorageUsage(): Promise<{ usage: number; quota: number; }>

Defined in: packages/runtime/src/runtime-impl.ts:226

查询存储配额使用情况(W6.7)。

返回 { usage, quota }:

  • usage:当前已用字节数(所有资产 metadata.size 之和)
  • quota:配置的存储配额上限(RuntimeConfig.storageQuota,默认 1GB)

UI 据此展示“已用/总额”进度条,接近上限(>=80%)时警告。 注意:usage 基于 listAssets 实时计算,反映 runtime 实际占用, 与浏览器 navigator.storage.estimate()(origin 整体 OPFS)不同。

Promise<{ usage: number; quota: number; }>

LokvisRuntime.getStorageUsage


capabilities(): Promise<Capability[]>

Defined in: packages/runtime/src/runtime-impl.ts:229

列出所有已注册能力

Promise<Capability[]>

LokvisRuntime.capabilities


hasCapability(name): Promise<boolean>

Defined in: packages/runtime/src/runtime-impl.ts:230

检查能力是否可用

string

Promise<boolean>

LokvisRuntime.hasCapability


isStubOnly(name): Promise<boolean>

Defined in: packages/runtime/src/runtime-impl.ts:231

检查能力是否仅有 stub 实现(无可用引擎)。

UI 层据此为 stub-only 能力显示 “Coming Soon” 标记(A7), 避免用户选择后在工作流执行阶段才收到 stub error。 返回 true 表示该能力已声明但仅有占位实现,resolve() 会跳过。

string

Promise<boolean>

LokvisRuntime.isStubOnly


listPanels(): PanelDefinition[]

Defined in: packages/runtime/src/runtime-impl.ts:234

列出插件通过 ctx.registerPanel() 注册的全部 UI Panel。

Panel 注册时同步发射 panel:registered 事件;UI 层(ui-react) 订阅该事件并调用本方法刷新面板列表,按 PanelDefinition.location 挂载到 Workspace 对应区域。component 字段为渲染器标识,由 UI 层 的 panel renderer 注册表解析(依赖反转:runtime 只持有定义, 不持有 React 组件)。

同步方法:panel 列表是纯内存状态,无 I/O。

PanelDefinition[]

LokvisRuntime.listPanels


toMcpManifest(options?): McpManifest

Defined in: packages/runtime/src/runtime-impl.ts:237

生成 MCP server manifest(不启动 server,仅描述当前可被 MCP 暴露的能力)。 用于:

  1. @lokvis/mcp-server 注册 tools 前的能力探测
  2. Dashboard 展示“可被 AI 调用的能力”
  3. 文档站自动生成 MCP tools 列表

options.batchMode 控制是否暴露 mcpExposure='batch-only' 的能力:

  • 默认 false(单文件模式):不暴露 batch-only 能力
  • true(batch 模式):暴露 batch-only 能力 mcpExposure='private' 的能力在任何模式下都不暴露。

注:本方法同步返回 —— manifest 是对 capabilityRegistry.list() (同步)的纯计算,无 I/O,故无需 async。capabilities() 仍为 async 仅为接口对称性(未来可能涉及异步加载)。

ToMcpManifestOptions = {}

McpManifest

LokvisRuntime.toMcpManifest


installPlugin(plugin): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:252

安装插件:① 注册能力声明 → ② 构造权限沙箱 → ③ 构造受限 PluginContext → ④ 应用 network guard(声明 network:none 时 monkey-patch 全局 fetch/XHR/WebSocket/EventSource)→ ⑤ 调用 plugin.install(ctx) → ⑥ restore 全局 API → ⑦ 发射 plugin:loaded 事件。不吞错。

network guard 仅在 plugin.install() 期间生效;install 后插件若异步调用 网络 API(如 setTimeout 回调)无法覆盖 —— plugin 作者应据 ctx.sandbox 主动断言(best-effort 守卫,见 docs/PROJECT_PLAN.md W18.6)。

PluginInstallEntry

Promise<void>

LokvisRuntime.installPlugin


_getAssetStore(): AssetStore

Defined in: packages/runtime/src/runtime-impl.ts:278

AssetStore


_getCapabilityRegistry(): CapabilityRegistry

Defined in: packages/runtime/src/runtime-impl.ts:279

CapabilityRegistry


_getMemoryGuard(): MemoryGuard

Defined in: packages/runtime/src/runtime-impl.ts:280

MemoryGuard


_getCurrentOutputs(workflowId): string[]

Defined in: packages/runtime/src/runtime-impl.ts:281

string

string[]


getCurrentOutputs(workflowId): Promise<string[]>

Defined in: packages/runtime/src/runtime-impl.ts:282

获取工作流当前输出 AssetId(undo/redo 后的“当前”状态)。

用途:

  • UI 实时展示工作流中间结果
  • MCP server 查询当前工作流产物
  • 暂停时检查中间输出

string

Promise<string[]>

LokvisRuntime.getCurrentOutputs


loadPersistedHistory(): Promise<void>

Defined in: packages/runtime/src/runtime-impl.ts:285

从 historyStore 预加载持久化历史快照(由 createRuntime 工厂在构造后调用一次,impl 初始化钩子)。

Promise<void>