Skip to content

会话与宿主状态 ​

读对话历史、读流水线状态、查已装插件、按 id 定位插件目录 / 运行库、写日志。


会话与历史 ​

python
host.get_recent_conversation(max_turns: int = 5) -> list[dict]
host.get_history_as_text(query: str = "") -> str

get_recent_conversation 每项:

python
{
  "id": int,
  "speaker": "peer" | "self",
  "speaker_label": str,     # 展示名,已随 UI 语言本地化
  "transcript": str,        # 纯 ASR 原文
  "translation": str,
  "time": str,
}
  • 只包含有效句(原文或译文非空)。
  • max_turns 会被裁剪到 [1, 50]。
  • 兼容别名:getRecentConversation。

喂给 LLM(如 assistant 插件)时只用 transcript:机翻会引入二次失真。

get_history_as_text(query) 返回以文本形式整理的历史(query 非空时先按关键词过滤), 适合直接拼进 prompt。


管线与宿主状态 ​

python
host.get_pipeline_state() -> dict
host.get_active_provider(plugin_type: str) -> str
host.list_installed_plugins(type_filter: str = "") -> list[dict]
host.get_app_version() -> str
host.get_locale() -> str
host.log(level: str, message: str, **fields: Any) -> None

get_pipeline_state():

python
{
  "running": bool,
  "loading": bool,
  "scene": str,
  "source_lang": str,
  "target_lang": str,
  "outbound_source_lang": str,
  "outbound_target_lang": str,
  "show_source": bool,
  "show_translation": bool,
  "enable_translation": bool,
  "mic_muted": bool,
  "interpretation_mode": bool,
  "interpretation_streaming": bool,
  "providers": {
    "asr": str, "mt": str, "tts": str,
    "interpretation": str, "theme": str,
  },
}
  • get_active_provider("asr" | "mt" | "tts" | "theme" | "interpretation") → 插件 ID 或 "none"。
  • list_installed_plugins(type_filter) 每项含 id / name / type / enabled / version / source_path(与工坊列表同源)。type_filter 传 "" 或 "all" 表示全部; 传 "other" 表示未分类。
  • get_locale() 返回当前 UI 语言(如 zh-CN / en-US)。
  • log(level, message, **fields):level ∈ debug|info|warning|error, 写入宿主插件日志通道,可带任意 JSON 字段(host.log("info", "加载完成", ms=1200))。 这是你唯一的排障出口——用户发日志时能直接看到。

按 id 定位插件与运行库 ​

python
host.plugin_dir(plugin_id: str) -> str        # 安装目录;未安装返回 ""
host.has_plugin(plugin_id: str) -> bool       # 是否已安装(不要求已启用)
host.list_installed_plugins() -> list[dict]   # 每项带 source_path
host.runtime_dir(runtime_id: str) -> str      # 运行库目录;未下载返回 ""
host.has_runtime(runtime_id: str) -> bool     # 运行库是否已就绪

永远不要硬编码兄弟插件目录名——工坊安装时目录名 = 插件 id (<数据目录>/workshop/workshop.amd_llama),而你手放进 extensions/ 的可能是下划线写法。 按 id 查两边都对:

python
from pathlib import Path

# 兄弟插件(非运行库)
dep = self.host.plugin_dir("workshop.common_qwen3_asr")
python = Path(dep) / "venv" / "Scripts" / "python.exe" if dep else None

# 运行库(0.2.0 内置,无承载插件)
rt = self.host.runtime_dir("llama_runtime")
exe = Path(rt) / "llama-server.exe" if rt else None

配套的依赖声明与宿主门禁见 前置插件依赖 与 宿主运行库。


建议 ​

  • 需要知道「现在是不是在跑」而不是「读一次状态」→ 订阅 pipeline.state 事件, 而不是轮询 get_pipeline_state()(见 事件总线)。
  • check_ready() 里判断可用性时,用 plugin_dir() / runtime_dir() / inspect_model_artifact() 做文件级判断,不要启动服务(见 能力声明)。

相关 ​

TransBox - 赋能全人类无界限跨语言交流