Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
<!-- status: implemented -->
<!-- status: implemented -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>host-api · speech</title>
<style>
:root{
--bg:#0e0e0e; --surface:#161616; --surface-2:#1b1b1b; --line:#2a2a2a; --line-2:#353535;
--text:#ededed; --text-2:#b0b0b0; --muted:#7d7d7d; --faint:#5c5c5c;
--ok:#40c977; --ok-bg:rgba(64,201,119,.10);
--warn:#ff8549; --warn-bg:rgba(255,133,73,.10);
--error:#ff6764; --error-bg:rgba(255,103,100,.10);
--host:#4c8dff; --plugin:#c27aff; --slot:#ff8549;
--font:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Hiragino Sans GB","Microsoft YaHei",sans-serif;
--mono:ui-monospace,"SF Mono","Cascadia Code",Consolas,monospace;
}
*{box-sizing:border-box} html,body{margin:0}
body{background:var(--bg);color:var(--text);font-family:var(--font);font-size:14px;line-height:1.6}
a{color:inherit} code,kbd{font-family:var(--mono);font-size:12px;color:var(--text-2);background:var(--surface-2);padding:1px 5px;border-radius:4px}
pre{font-family:var(--mono);font-size:12.5px;background:var(--surface-2);padding:12px 14px;border-radius:10px;overflow:auto;color:var(--text-2);white-space:pre-wrap}
.plan-nav{display:flex;flex-wrap:wrap;align-items:center;gap:4px;padding:10px 28px;background:#0c0c0c;border-bottom:1px solid var(--line);font-size:13px}
.plan-nav a{color:var(--text-2);text-decoration:none;padding:3px 8px;border-radius:6px}
.plan-nav a:hover{background:var(--surface);color:var(--text)}
.plan-nav a[aria-current="page"]{background:var(--surface);color:var(--text);box-shadow:inset 0 0 0 1px var(--line-2)}
.plan-nav .sep{color:var(--faint);padding:0 2px} .plan-nav .spacer{flex:1}
.top{border-bottom:1px solid var(--line);padding:26px 28px 20px;background:linear-gradient(180deg,#141414,#0e0e0e)}
.top h1{margin:0 0 6px;font-size:21px} .top p{margin:0;color:var(--text-2);max-width:76ch}
.top .meta{margin-top:8px;color:var(--faint);font-size:12.5px}
main{max-width:1080px;margin:0 auto;padding:26px 28px 80px}
section{margin:0 0 36px}
section>h2{display:flex;align-items:baseline;gap:10px;margin:0 0 8px;font-size:16.5px}
section>h2 .n{font-family:var(--mono);font-size:11.5px;color:var(--muted);border:1px solid var(--line-2);border-radius:4px;padding:1px 6px}
section>.lead{margin:0 0 14px;color:var(--text-2);max-width:82ch}
table{width:100%;border-collapse:collapse;font-size:13.5px;margin:0 0 12px}
th,td{text-align:left;padding:8px 10px;border-bottom:1px solid var(--line);vertical-align:top}
th{color:var(--muted);font-weight:500;font-size:12px;text-transform:uppercase;letter-spacing:.03em}
td.id{font-family:var(--mono);font-size:12.5px;color:var(--text-2)}
.pill{display:inline-block;padding:1px 7px;border-radius:99px;font-size:11px;font-family:var(--mono);white-space:nowrap}
.pill.has{color:var(--ok);background:var(--ok-bg)}
.pill.warn{color:var(--warn);background:var(--warn-bg)}
.pill.perm{color:var(--host);background:rgba(76,141,255,.12)}
.callout{margin:12px 0;padding:12px 14px;border-radius:10px;background:var(--surface);border-left:3px solid var(--line-2);color:var(--text-2);font-size:13px}
.callout.warn{border-left-color:var(--warn)}
.callout b{color:var(--text)}
ul,ol{margin:0 0 12px;padding-left:1.35em;color:var(--text-2)}
li{margin:0 0 4px} li b{color:var(--text)}
article.api{margin:0 0 56px;padding-top:8px;border-top:1px solid var(--line)}
article.api>h2{display:flex;align-items:center;gap:10px;margin:18px 0 4px;font-size:19px}
article.api>.meta{margin:0 0 18px;color:var(--faint);font-size:12.5px}
section>h3{display:flex;align-items:baseline;gap:10px;margin:0 0 8px;font-size:15.5px}
section>h3 .n{font-family:var(--mono);font-size:11.5px;color:var(--muted);border:1px solid var(--line-2);border-radius:4px;padding:1px 6px}
</style>
</head>
<body>
<p data-aigc-mark="1" style="margin:0 0 8px;font-size:10px;line-height:1.3;color:#6b6560;font-weight:400;">AI生成</p>
<nav class="plan-nav">
<a href="../index.html">plugin-plan</a><span class="sep">/</span><a href="index.html">host-api</a><span class="sep">/</span><a href="speech.html" aria-current="page">speech</a>
<span class="spacer"></span>
<a href="#speech.registerAdapter">registerAdapter</a>
<a href="#speech.unregisterAdapter">unregisterAdapter</a>
</nav>
<header class="top">
<h1>host-api · speech</h1>
<p>命名空间 <code>speech</code> · 2 条。每条 API 一节:接口约定、数据怎么走、出错、不提供什么、相关条目、典型场景、已拍板。</p>
<div class="meta">status: <b>implemented</b> · 事实源 <code>packages/plugin-sdk/src/index.ts</code> · <code>PluginHostApi</code></div>
</header>
<main>
<article class="api" id="speech.registerAdapter">
<h2><code>speech.registerAdapter</code><span class="pill has">implemented</span></h2>
<p class="meta">status: <b>implemented</b> · 事实源 <code>packages/plugin-sdk/src/index.ts</code> · <code>PluginHostApi</code></p>
<section>
<h3><span class="n">1</span>接口约定</h3>
<pre class="sig">pi.speech.registerAdapter(adapter: PluginSpeechAdapter): Promise<void></pre>
<p class="lead">注册一种语音协议适配器(转写 <code>transcribe</code> 或合成 <code>synthesize</code>),协议名全局唯一。权限:<span class="pill perm">—</span>。</p>
<p><b>入参</b></p>
<table>
<thead><tr><th>字段</th><th>类型</th><th>必填</th><th>说明</th></tr></thead>
<tbody>
<tr><td class="id">protocol</td><td class="id">string</td><td>是</td><td>协议名,全局唯一,注销时用它寻址。</td></tr>
<tr><td class="id">label</td><td class="id">string</td><td>是</td><td>设置页等界面显示名。</td></tr>
<tr><td class="id">roles</td><td class="id">("transcribe" | "synthesize")[]</td><td>是</td><td>支持的角色列表。</td></tr>
<tr><td class="id">handle</td><td class="id">(input: PluginSpeechHandleInput) => Promise<PluginSpeechHandleResult> | PluginSpeechHandleResult</td><td>是</td><td>宿主回调,留在插件进程:入参含 <code>role</code>、<code>modelId</code>,转写带 <code>audio</code>(<code>{ mimeType, data }</code>),合成带 <code>text</code>;返回 <code>{ kind: "text" | "audio" | "http", … }</code>。</td></tr>
</tbody>
</table>
<p><b>出参</b></p>
<table>
<thead><tr><th>字段</th><th>类型</th><th>说明</th></tr></thead>
<tbody>
<tr><td class="id">(返回值)</td><td class="id">Promise<void></td><td>登记成功后兑现,无返回数据。</td></tr>
</tbody>
</table>
<p><b>输入样例</b></p>
<pre>await pi.speech.registerAdapter({
protocol: "my-whisper",
label: "My Whisper 私有转写",
roles: ["transcribe"],
handle: async (input) => {
// input.role === "transcribe",input.audio 为 { mimeType, data }
return { kind: "text", text: "识别出的文本" };
},
});</pre>
<p><b>输出样例</b></p>
<pre>// 无返回值(Promise<void>)</pre>
<div class="callout"><b>一句话边界。</b><code>handle</code> 永远留在插件进程执行;HTTP 型结果由宿主用绑定密钥代发且必须同源,密钥不下发插件。</div>
</section>
<section>
<h3><span class="n">2</span>数据怎么走</h3>
<ol>
<li>插件交出适配器对象(protocol、label、roles、handle 回调)。</li>
<li>校验:协议名全局唯一、角色合法;字段非法 <code>INVALID_ARGUMENT</code>,未授权 <code>PERMISSION_DENIED</code>。</li>
<li>宿主登记该协议,持续到 <code>speech.unregisterAdapter</code> 或插件卸载。</li>
<li>此后宿主按协议把听写/朗读请求回调进插件 <code>handle</code>。</li>
</ol>
<table>
<thead><tr><th>方向</th><th>内容</th></tr></thead>
<tbody>
<tr><td>插件 → 宿主</td><td>适配器对象(含 <code>handle</code> 回调)。</td></tr>
<tr><td>宿主 → 插件</td><td>语音请求回调 <code>handle</code>(转写/合成入参,插件返回结果)。</td></tr>
</tbody>
</table>
</section>
<section>
<h3><span class="n">3</span>出错怎么办</h3>
<table>
<thead><tr><th>情况</th><th>结果</th><th>码</th></tr></thead>
<tbody>
<tr><td>未获语音适配器权限</td><td>Promise 拒绝,登记不生效</td><td class="id">PERMISSION_DENIED</td></tr>
<tr><td>协议名重复或字段非法</td><td>Promise 拒绝,登记不生效</td><td class="id">INVALID_ARGUMENT</td></tr>
</tbody>
</table>
</section>
<section>
<h3><span class="n">4</span>不提供什么</h3>
<p class="lead"><code>handle</code> 留在插件进程;HTTP 计划由宿主用绑定密钥代发且必须同源。</p>
<div class="callout"><b>宿主保证 / 已接受代价:</b>宿主保证:密钥不下发插件。已接受代价:同源限制排除跨域语音服务。</div>
</section>
<section>
<h3><span class="n">5</span>相关条目</h3>
<ul>
<li><a href="speech.html#speech.unregisterAdapter"><code>host-api/speech.unregisterAdapter</code></a></li>
</ul>
</section>
<section>
<h3><span class="n">6</span>还没定的事</h3>
<p class="lead">无。</p>
</section>
<section>
<h3><span class="n">7</span>典型场景</h3>
<table><thead><tr><th>场景</th><th>插件做什么</th></tr></thead><tbody>
<tr><td><b>私有转写</b></td><td>把音频转成文字交给宿主听写。</td></tr>
<tr><td><b>私有合成</b></td><td>把文本合成音频供朗读。</td></tr>
</tbody></table>
</section>
<section>
<h3><span class="n">8</span>已拍板</h3>
<ul><li>协议名全局唯一;角色 transcribe / synthesize。</li></ul>
</section>
</article>
<article class="api" id="speech.unregisterAdapter">
<h2><code>speech.unregisterAdapter</code><span class="pill has">implemented</span></h2>
<p class="meta">status: <b>implemented</b> · 事实源 <code>packages/plugin-sdk/src/index.ts</code> · <code>PluginHostApi</code></p>
<section>
<h3><span class="n">1</span>接口约定</h3>
<pre class="sig">pi.speech.unregisterAdapter(protocol: string): Promise<void></pre>
<p class="lead">按协议名注销本插件注册的语音适配器。权限:<span class="pill perm">—</span>。</p>
<p><b>入参</b></p>
<table>
<thead><tr><th>字段</th><th>类型</th><th>必填</th><th>说明</th></tr></thead>
<tbody>
<tr><td class="id">protocol</td><td class="id">string</td><td>是</td><td>要注销的协议名,与 registerAdapter 时一致。</td></tr>
</tbody>
</table>
<p><b>出参</b></p>
<table>
<thead><tr><th>字段</th><th>类型</th><th>说明</th></tr></thead>
<tbody>
<tr><td class="id">(返回值)</td><td class="id">Promise<void></td><td>注销完成后兑现,无返回数据。</td></tr>
</tbody>
</table>
<p><b>输入样例</b></p>
<pre>await pi.speech.unregisterAdapter("my-whisper");</pre>
<p><b>输出样例</b></p>
<pre>// 无返回值(Promise<void>)</pre>
<div class="callout"><b>一句话边界。</b>只注销本插件名下的协议,不影响其他插件适配器;插件卸载时宿主强制清理。</div>
</section>
<section>
<h3><span class="n">2</span>数据怎么走</h3>
<ol>
<li>插件传入协议名。</li>
<li>宿主立即移除该协议登记,不再把语音请求回调进插件。</li>
<li>Promise 兑现为 <code>void</code>;未知名通常静默成功。</li>
</ol>
</section>
<section>
<h3><span class="n">3</span>出错怎么办</h3>
<table>
<thead><tr><th>情况</th><th>结果</th><th>码</th></tr></thead>
<tbody>
<tr><td>协议名未注册</td><td>静默成功,不报错</td><td class="id">—</td></tr>
</tbody>
</table>
</section>
<section>
<h3><span class="n">4</span>不提供什么</h3>
<p class="lead">不影响其他插件适配器。</p>
</section>
<section>
<h3><span class="n">5</span>相关条目</h3>
<ul>
<li><a href="speech.html#speech.registerAdapter"><code>host-api/speech.registerAdapter</code></a></li>
</ul>
</section>
<section>
<h3><span class="n">6</span>还没定的事</h3>
<p class="lead">无。</p>
</section>
<section>
<h3><span class="n">7</span>典型场景</h3>
<table><thead><tr><th>场景</th><th>插件做什么</th></tr></thead><tbody>
<tr><td><b>停用语音源</b></td><td>用户关掉插件语音能力。</td></tr>
</tbody></table>
</section>
<section>
<h3><span class="n">8</span>已拍板</h3>
<ul><li>与 register 成对。</li></ul>
</section>
</article>
</main>
</body>
</html>