Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
338 lines
12 KiB
TypeScript
338 lines
12 KiB
TypeScript
// Voice Call API module exposes the plugin public contract.
|
|
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import type { OpenClawConfig } from "openclaw/plugin-sdk/plugin-entry";
|
|
import { normalizeAgentId } from "openclaw/plugin-sdk/routing";
|
|
import {
|
|
archiveLegacyStateSource,
|
|
type PluginDoctorStateMigration,
|
|
type PluginStateKeyedStore,
|
|
} from "openclaw/plugin-sdk/runtime-doctor";
|
|
import {
|
|
buildVoiceCallLegacyJsonlEventKey,
|
|
CALL_RECORD_CHUNK_MAX_ENTRIES,
|
|
CALL_RECORD_EVENT_CHUNKS_NAMESPACE,
|
|
CALL_RECORD_EVENT_META_MAX_ENTRIES,
|
|
CALL_RECORD_EVENTS_NAMESPACE,
|
|
MAX_CALL_RECORD_EVENTS,
|
|
MAX_CHUNKS_PER_CALL_RECORD_EVENT,
|
|
prepareVoiceCallRecordForStorage,
|
|
parseVoiceCallRecordLine,
|
|
RAW_CALL_RECORD_CHUNK_BYTES,
|
|
resolveVoiceCallLegacyCallLogPath,
|
|
} from "./src/manager/store.js";
|
|
import type { CallRecord } from "./src/types.js";
|
|
|
|
// Doctor state migration for Voice Call legacy JSONL call logs.
|
|
|
|
/** Plugin state metadata row for one migrated call record event. */
|
|
type CallRecordEventMeta = {
|
|
chunkCount: number;
|
|
byteLength: number;
|
|
persistedAt?: number;
|
|
sequence?: number;
|
|
};
|
|
|
|
/** Plugin state chunk row for one migrated call record event. */
|
|
type CallRecordEventChunk = {
|
|
index: number;
|
|
dataBase64: string;
|
|
};
|
|
|
|
/** Prepared legacy JSONL call record ready for plugin state import. */
|
|
type PreparedLegacyCallRecord = {
|
|
eventKey: string;
|
|
lineNumber: number;
|
|
chunks: CallRecordEventChunk[];
|
|
meta: CallRecordEventMeta;
|
|
};
|
|
|
|
/** Resolve home from doctor env with OS fallback. */
|
|
function resolveHome(env: NodeJS.ProcessEnv): string {
|
|
return env.HOME?.trim() || os.homedir();
|
|
}
|
|
|
|
/** Resolve config paths, including "~", against the doctor env home. */
|
|
function resolveUserPath(input: string, env: NodeJS.ProcessEnv): string {
|
|
const trimmed = input.trim();
|
|
if (!trimmed) {
|
|
return trimmed;
|
|
}
|
|
if (trimmed.startsWith("~")) {
|
|
return path.resolve(trimmed.replace(/^~(?=$|[\\/])/, resolveHome(env)));
|
|
}
|
|
return path.resolve(trimmed);
|
|
}
|
|
|
|
/** Read the configured voice-call store path from either package id. */
|
|
function getVoiceCallConfigStore(config: PluginDoctorStateMigrationParams["config"]): string {
|
|
for (const pluginId of ["voice-call", "@openclaw/voice-call"]) {
|
|
const rawConfig = config.plugins?.entries?.[pluginId]?.config;
|
|
if (!rawConfig || typeof rawConfig !== "object" || Array.isArray(rawConfig)) {
|
|
continue;
|
|
}
|
|
const store = (rawConfig as { store?: unknown }).store;
|
|
if (typeof store === "string" && store.trim()) {
|
|
return store.trim();
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
type PluginDoctorStateMigrationParams = Parameters<
|
|
PluginDoctorStateMigration["detectLegacyState"]
|
|
>[0];
|
|
|
|
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
return value && typeof value === "object" && !Array.isArray(value)
|
|
? (value as Record<string, unknown>)
|
|
: undefined;
|
|
}
|
|
|
|
/** Return Voice Call agents whose templated core session stores need migration. */
|
|
export function resolveSessionStoreAgentIds(params: { cfg: OpenClawConfig }): string[] {
|
|
const agentIds = new Set<string>();
|
|
for (const pluginId of ["voice-call", "@openclaw/voice-call"]) {
|
|
const entry = params.cfg.plugins?.entries?.[pluginId];
|
|
if (!entry) {
|
|
continue;
|
|
}
|
|
const config = entry.config === undefined ? {} : asRecord(entry.config);
|
|
if (!config) {
|
|
continue;
|
|
}
|
|
agentIds.add(normalizeAgentId(typeof config.agentId === "string" ? config.agentId : undefined));
|
|
const numbers = asRecord(config.numbers);
|
|
for (const route of Object.values(numbers ?? {})) {
|
|
const agentId = asRecord(route)?.agentId;
|
|
if (typeof agentId === "string") {
|
|
agentIds.add(normalizeAgentId(agentId));
|
|
}
|
|
}
|
|
}
|
|
return [...agentIds].toSorted();
|
|
}
|
|
|
|
/** Resolve the voice-call store path used by legacy and plugin-state call records. */
|
|
function resolveVoiceCallStorePath(params: {
|
|
config: PluginDoctorStateMigrationParams["config"];
|
|
env: NodeJS.ProcessEnv;
|
|
}): string {
|
|
const configuredStore = getVoiceCallConfigStore(params.config);
|
|
if (configuredStore) {
|
|
return resolveUserPath(configuredStore, params.env);
|
|
}
|
|
return path.join(resolveHome(params.env), ".openclaw", "voice-calls");
|
|
}
|
|
|
|
/** Return true when a path exists and is a file. */
|
|
|
|
/** Build the plugin state key for one migrated event chunk. */
|
|
function buildChunkKey(eventKey: string, index: number): string {
|
|
return `${eventKey}:chunk:${String(index).padStart(4, "0")}`;
|
|
}
|
|
|
|
/** Chunk a prepared call record into bounded plugin state rows. */
|
|
function prepareChunks(call: CallRecord): {
|
|
chunks: CallRecordEventChunk[];
|
|
meta: CallRecordEventMeta;
|
|
} {
|
|
const serialized = JSON.stringify(prepareVoiceCallRecordForStorage(call));
|
|
const buffer = Buffer.from(serialized, "utf8");
|
|
const chunkCount = Math.max(1, Math.ceil(buffer.byteLength / RAW_CALL_RECORD_CHUNK_BYTES));
|
|
if (chunkCount > MAX_CHUNKS_PER_CALL_RECORD_EVENT) {
|
|
throw new Error(
|
|
`voice-call record exceeds SQLite chunk limit (${chunkCount}/${MAX_CHUNKS_PER_CALL_RECORD_EVENT})`,
|
|
);
|
|
}
|
|
const chunks: CallRecordEventChunk[] = [];
|
|
for (let index = 0; index < chunkCount; index += 1) {
|
|
const chunk = buffer.subarray(
|
|
index * RAW_CALL_RECORD_CHUNK_BYTES,
|
|
(index + 1) * RAW_CALL_RECORD_CHUNK_BYTES,
|
|
);
|
|
chunks.push({ index, dataBase64: chunk.toString("base64") });
|
|
}
|
|
return {
|
|
chunks,
|
|
meta: {
|
|
chunkCount,
|
|
byteLength: buffer.byteLength,
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Read and prepare legacy JSONL call records, collecting line-level warnings. */
|
|
async function readLegacyCallRecords(filePath: string): Promise<{
|
|
entries: PreparedLegacyCallRecord[];
|
|
warnings: string[];
|
|
}> {
|
|
let content;
|
|
try {
|
|
content = await fs.readFile(filePath, "utf8");
|
|
} catch {
|
|
return { entries: [], warnings: [] };
|
|
}
|
|
const entries: PreparedLegacyCallRecord[] = [];
|
|
const warnings: string[] = [];
|
|
let index = 0;
|
|
for (const line of content.split("\n")) {
|
|
const parsed = parseVoiceCallRecordLine(line, index);
|
|
if (!parsed) {
|
|
if (line.trim()) {
|
|
warnings.push(`Skipped malformed Voice Call call-log line ${index + 1}`);
|
|
}
|
|
index += 1;
|
|
continue;
|
|
}
|
|
try {
|
|
const prepared = prepareChunks(parsed.call);
|
|
entries.push({
|
|
eventKey: buildVoiceCallLegacyJsonlEventKey(line, index),
|
|
lineNumber: index + 1,
|
|
chunks: prepared.chunks,
|
|
meta: {
|
|
...prepared.meta,
|
|
persistedAt: parsed.persistedAt,
|
|
sequence: parsed.sequence,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
warnings.push(`Skipped Voice Call call-log line ${index + 1}: ${String(err)}`);
|
|
}
|
|
index += 1;
|
|
}
|
|
return { entries, warnings };
|
|
}
|
|
|
|
/** Archive the legacy JSONL source after a complete migration. */
|
|
|
|
/** Select newest missing records that fit remaining plugin state capacity. */
|
|
async function selectEntriesForImport(params: {
|
|
entries: PreparedLegacyCallRecord[];
|
|
eventStore: PluginStateKeyedStore<CallRecordEventMeta>;
|
|
chunkStore: PluginStateKeyedStore<CallRecordEventChunk>;
|
|
warnings: string[];
|
|
}): Promise<{ existingEventKeys: Set<string>; entries: PreparedLegacyCallRecord[] }> {
|
|
const existingEventKeys = new Set((await params.eventStore.entries()).map((entry) => entry.key));
|
|
const missingEntries = params.entries.filter((entry) => !existingEventKeys.has(entry.eventKey));
|
|
const existingChunks = await params.chunkStore.entries();
|
|
let eventRoom = Math.max(0, MAX_CALL_RECORD_EVENTS - existingEventKeys.size);
|
|
let chunkRoom = Math.max(0, CALL_RECORD_CHUNK_MAX_ENTRIES - existingChunks.length);
|
|
const selected: PreparedLegacyCallRecord[] = [];
|
|
let pruned = 0;
|
|
for (const entry of missingEntries.toReversed()) {
|
|
if (eventRoom <= 0 || entry.chunks.length > chunkRoom) {
|
|
pruned++;
|
|
continue;
|
|
}
|
|
selected.push(entry);
|
|
eventRoom--;
|
|
chunkRoom -= entry.chunks.length;
|
|
}
|
|
if (pruned > 0) {
|
|
params.warnings.push(
|
|
`Pruned ${pruned} older Voice Call call-log ${pruned === 1 ? "record" : "records"} during migration because plugin state keeps the newest ${MAX_CALL_RECORD_EVENTS} records`,
|
|
);
|
|
}
|
|
return { existingEventKeys, entries: selected.toReversed() };
|
|
}
|
|
|
|
/** Import prepared legacy call records into plugin state. */
|
|
async function importLegacyCallRecords(params: {
|
|
entries: PreparedLegacyCallRecord[];
|
|
eventStore: PluginStateKeyedStore<CallRecordEventMeta>;
|
|
chunkStore: PluginStateKeyedStore<CallRecordEventChunk>;
|
|
warnings: string[];
|
|
}): Promise<number> {
|
|
const selected = await selectEntriesForImport(params);
|
|
let imported = 0;
|
|
for (const entry of selected.entries) {
|
|
if (selected.existingEventKeys.has(entry.eventKey)) {
|
|
continue;
|
|
}
|
|
try {
|
|
for (const chunk of entry.chunks) {
|
|
await params.chunkStore.register(buildChunkKey(entry.eventKey, chunk.index), chunk);
|
|
}
|
|
await params.eventStore.register(entry.eventKey, entry.meta);
|
|
selected.existingEventKeys.add(entry.eventKey);
|
|
imported++;
|
|
} catch (err) {
|
|
params.warnings.push(
|
|
`Failed migrating Voice Call call-log line ${entry.lineNumber}: ${String(err)}`,
|
|
);
|
|
}
|
|
}
|
|
return imported;
|
|
}
|
|
|
|
/** Doctor migrations owned by the voice-call plugin. */
|
|
export const stateMigrations: PluginDoctorStateMigration[] = [
|
|
{
|
|
id: "voice-call-calls-jsonl-to-plugin-state",
|
|
label: "Voice Call call log",
|
|
async detectLegacyState(params) {
|
|
const storePath = resolveVoiceCallStorePath(params);
|
|
const filePath = resolveVoiceCallLegacyCallLogPath(storePath);
|
|
const { entries } = await readLegacyCallRecords(filePath);
|
|
if (entries.length === 0) {
|
|
return null;
|
|
}
|
|
return {
|
|
preview: [
|
|
`- Voice Call call log: ${entries.length} ${entries.length === 1 ? "record" : "records"} -> plugin state (${CALL_RECORD_EVENTS_NAMESPACE})`,
|
|
],
|
|
};
|
|
},
|
|
async migrateLegacyState(params) {
|
|
const changes: string[] = [];
|
|
const warnings: string[] = [];
|
|
const storePath = resolveVoiceCallStorePath(params);
|
|
const filePath = resolveVoiceCallLegacyCallLogPath(storePath);
|
|
const { entries, warnings: readWarnings } = await readLegacyCallRecords(filePath);
|
|
warnings.push(...readWarnings);
|
|
if (entries.length === 0) {
|
|
return { changes, warnings };
|
|
}
|
|
const env = { ...params.env, OPENCLAW_STATE_DIR: storePath };
|
|
const eventStore = params.context.openPluginStateKeyedStore<CallRecordEventMeta>({
|
|
namespace: CALL_RECORD_EVENTS_NAMESPACE,
|
|
maxEntries: CALL_RECORD_EVENT_META_MAX_ENTRIES,
|
|
env,
|
|
});
|
|
const chunkStore = params.context.openPluginStateKeyedStore<CallRecordEventChunk>({
|
|
namespace: CALL_RECORD_EVENT_CHUNKS_NAMESPACE,
|
|
maxEntries: CALL_RECORD_CHUNK_MAX_ENTRIES,
|
|
env,
|
|
});
|
|
const imported = await importLegacyCallRecords({
|
|
entries,
|
|
eventStore,
|
|
chunkStore,
|
|
warnings,
|
|
});
|
|
if (imported > 0) {
|
|
changes.push(
|
|
`Migrated ${imported} Voice Call call-log ${imported === 1 ? "record" : "records"} -> plugin state`,
|
|
);
|
|
}
|
|
if (
|
|
warnings.some(
|
|
(warning) =>
|
|
warning.startsWith("Failed migrating Voice Call") ||
|
|
warning.startsWith("Skipped malformed Voice Call call-log line") ||
|
|
warning.startsWith("Skipped Voice Call call-log line") ||
|
|
warning.startsWith("Skipped Voice Call call-log migration"),
|
|
)
|
|
) {
|
|
warnings.push("Left Voice Call call-log source in place because migration was incomplete");
|
|
return { changes, warnings };
|
|
}
|
|
await archiveLegacyStateSource({ filePath, label: "Voice Call call-log", changes, warnings });
|
|
return { changes, warnings };
|
|
},
|
|
},
|
|
];
|