`vendor/MetasequoiaImeEngine` submodule 改为 `engine/` 目录,导入的是 product-lock.json 原本钉住的提交 c810d201,逐文件核对过 blob,因此这是一次 搬迁而不是引擎升级。引擎自己的嵌套 submodule(googlepinyinime-rev、utfcpp、 miniaudio)按各自 gitlink 一并展开,来源与提交见 engine/UPSTREAM.md。 Windows 专门化裁掉了不属于本产品的部分:whisper.cpp 与本地推理 provider、 macOS 示例宿主、整个 dictionary/ 建库流水线、上游 CI 与 release-please 配置。 词库仍按 product-lock.json 在构建时取发布产物。 引擎不再进 product-lock.json:没有 gitlink 就没有可比对的东西,锁里写一个上游 commit 等于宣称这棵树仍等于上游,而第一个 Windows 专门化改动落地时这句话就假了。 verify-contracts / verify-checkout 随之移除,verify-published 保留。 CI 与工具链:引擎源码并入根 scripts/format.sh(删掉引擎自己的那份,避免两套排除 列表分叉);引擎 voice.yml 里的 silero_vad.onnx 摘要校验移到本仓 lock job; engine-bump-triage 工作流与其测试删除;gitsubmodule 的 dependabot 频率由 daily 改为 monthly。 README、README.en、AGENTS.md、CONTRIBUTING、docs/product-release.md 及各子目录 文档同步说明新的位置与边界。
147 lines
4.1 KiB
C++
147 lines
4.1 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <string>
|
|
#include <vector>
|
|
#include "ipc_protocol_limits.h"
|
|
|
|
// Framed voice-composition snapshots on the worker pipe.
|
|
// Shared by both endpoints; do not copy this implementation into a platform repo.
|
|
// Each worker packet is still wchar_t[200]; a snapshot longer than one packet
|
|
// is sent as ordered frames and assembled before any TSF SetText.
|
|
namespace FanyImeVoiceCompositionPipe
|
|
{
|
|
constexpr wchar_t kFlagFirst = 0x1;
|
|
constexpr wchar_t kFlagLast = 0x2;
|
|
constexpr std::size_t kPacketChars = FanyImePipeLimits::CandidateTextCapacity;
|
|
constexpr std::size_t kHeaderChars = 2;
|
|
constexpr std::size_t kMaxChunkChars = kPacketChars - kHeaderChars - 1;
|
|
constexpr std::size_t kMaxSnapshotChars = 2048;
|
|
|
|
struct Frame
|
|
{
|
|
bool valid = false;
|
|
bool first = false;
|
|
bool last = false;
|
|
wchar_t generation = 0;
|
|
std::wstring chunk;
|
|
};
|
|
|
|
inline Frame ParseFrame(const wchar_t *data, std::size_t n = kPacketChars)
|
|
{
|
|
Frame frame;
|
|
if (data == nullptr || n < kHeaderChars + 1)
|
|
{
|
|
return frame;
|
|
}
|
|
// The terminator has to sit inside the chunk: a middle frame legally carries flags 0, so a scan starting at the
|
|
// header would accept data[0] as the terminator and let the chunk read run past the packet.
|
|
std::size_t terminatorIndex = n;
|
|
for (std::size_t i = kHeaderChars; i < n; ++i)
|
|
{
|
|
if (data[i] == L'\0')
|
|
{
|
|
terminatorIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
if (terminatorIndex == n)
|
|
{
|
|
return frame;
|
|
}
|
|
const wchar_t flags = data[0];
|
|
if ((flags & static_cast<wchar_t>(~(kFlagFirst | kFlagLast))) != 0 || data[1] == 0)
|
|
{
|
|
return frame;
|
|
}
|
|
frame.valid = true;
|
|
frame.first = (flags & kFlagFirst) != 0;
|
|
frame.last = (flags & kFlagLast) != 0;
|
|
frame.generation = data[1];
|
|
frame.chunk.assign(data + kHeaderChars, terminatorIndex - kHeaderChars);
|
|
return frame;
|
|
}
|
|
|
|
inline std::vector<std::wstring> EncodeSnapshot(std::wstring text, wchar_t generation)
|
|
{
|
|
std::vector<std::wstring> frames;
|
|
if (generation == 0)
|
|
{
|
|
return frames;
|
|
}
|
|
if (text.size() > kMaxSnapshotChars)
|
|
{
|
|
text.resize(kMaxSnapshotChars);
|
|
}
|
|
if (text.empty())
|
|
{
|
|
std::wstring frame(kHeaderChars, L'\0');
|
|
frame[0] = static_cast<wchar_t>(kFlagFirst | kFlagLast);
|
|
frame[1] = generation;
|
|
frames.push_back(std::move(frame));
|
|
return frames;
|
|
}
|
|
|
|
std::size_t offset = 0;
|
|
while (offset < text.size())
|
|
{
|
|
const std::size_t chunkLen = (std::min)(kMaxChunkChars, text.size() - offset);
|
|
std::wstring frame(kHeaderChars + chunkLen, L'\0');
|
|
wchar_t flags = 0;
|
|
if (offset == 0)
|
|
{
|
|
flags |= kFlagFirst;
|
|
}
|
|
if (offset + chunkLen >= text.size())
|
|
{
|
|
flags |= kFlagLast;
|
|
}
|
|
frame[0] = flags;
|
|
frame[1] = generation;
|
|
std::copy(text.begin() + static_cast<std::ptrdiff_t>(offset),
|
|
text.begin() + static_cast<std::ptrdiff_t>(offset + chunkLen), frame.begin() + kHeaderChars);
|
|
frames.push_back(std::move(frame));
|
|
offset += chunkLen;
|
|
}
|
|
return frames;
|
|
}
|
|
|
|
inline std::wstring AssembleFrames(const std::vector<std::wstring> &payloads)
|
|
{
|
|
std::wstring assembled;
|
|
bool started = false;
|
|
wchar_t generation = 0;
|
|
for (const std::wstring &payload : payloads)
|
|
{
|
|
wchar_t data[kPacketChars] = {};
|
|
if (payload.size() >= kPacketChars)
|
|
{
|
|
return {};
|
|
}
|
|
std::copy(payload.begin(), payload.end(), data);
|
|
const Frame frame = ParseFrame(data);
|
|
if (!frame.valid)
|
|
{
|
|
return {};
|
|
}
|
|
if (frame.first)
|
|
{
|
|
assembled.clear();
|
|
generation = frame.generation;
|
|
started = true;
|
|
}
|
|
else if (!started || frame.generation != generation)
|
|
{
|
|
return {};
|
|
}
|
|
assembled += frame.chunk;
|
|
if (frame.last)
|
|
{
|
|
return assembled;
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
} // namespace FanyImeVoiceCompositionPipe
|