Skip to content

Commit 6a86ca8

Browse files
Align sidecar bundle parity for openwrk (anomalyco#383)
* feat(headless): align sidecar bundle parity Build headless sidecars from the same sources as desktop and record manifest checks so version integrity stays consistent across runtimes. * feat(owpenbot): hot-set telegram token Expose a health REST endpoint to update the Telegram token at runtime, wire the app to call it, and keep status reporting in sync without restarts.
1 parent aadec3a commit 6a86ca8

12 files changed

Lines changed: 352 additions & 186 deletions

File tree

‎packages/app/src/app/lib/tauri.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ export type OwpenbotOpencodeStatus = {
530530
export type OwpenbotStatus = {
531531
running: boolean;
532532
config: string;
533+
healthPort?: number | null;
533534
whatsapp: OwpenbotWhatsAppStatus;
534535
telegram: OwpenbotTelegramStatus;
535536
opencode: OwpenbotOpencodeStatus;
@@ -622,7 +623,17 @@ export async function setOwpenbotAllowlist(allowlist: string[]): Promise<ExecRes
622623

623624
export async function setOwpenbotTelegramToken(token: string): Promise<ExecResult> {
624625
try {
625-
await invoke("owpenbot_config_set", { key: "channels.telegram.token", value: token });
626+
const status = await getOwpenbotStatus();
627+
const healthPort = status?.healthPort ?? 3005;
628+
const response = await fetch(`http://127.0.0.1:${healthPort}/config/telegram-token`, {
629+
method: "POST",
630+
headers: { "Content-Type": "application/json" },
631+
body: JSON.stringify({ token }),
632+
});
633+
if (!response.ok) {
634+
const message = await response.text();
635+
return { ok: false, status: response.status, stdout: "", stderr: message };
636+
}
626637
return { ok: true, status: 0, stdout: "", stderr: "" };
627638
} catch (e) {
628639
return { ok: false, status: 1, stdout: "", stderr: String(e) };

‎packages/desktop/scripts/prepare-sidecar.mjs‎

Lines changed: 100 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ import {
1212
readdirSync,
1313
statSync,
1414
unlinkSync,
15+
writeFileSync,
1516
} from "fs";
1617
import { dirname, join, resolve } from "path";
1718
import { tmpdir } from "os";
1819
import { fileURLToPath } from "url";
1920

2021
const __dirname = dirname(fileURLToPath(import.meta.url));
21-
const sidecarDir = join(__dirname, "..", "src-tauri", "sidecars");
22+
const readArg = (name) => {
23+
const raw = process.argv.slice(2);
24+
const direct = raw.find((arg) => arg.startsWith(`${name}=`));
25+
if (direct) return direct.split("=")[1];
26+
const index = raw.indexOf(name);
27+
if (index >= 0 && raw[index + 1]) return raw[index + 1];
28+
return null;
29+
};
30+
const sidecarOverride = process.env.OPENWORK_SIDECAR_DIR?.trim() || readArg("--outdir");
31+
const sidecarDir = sidecarOverride ? resolve(sidecarOverride) : join(__dirname, "..", "src-tauri", "sidecars");
2232
const packageJsonPath = resolve(__dirname, "..", "package.json");
2333
const opencodeVersion = (() => {
2434
if (process.env.OPENCODE_VERSION?.trim()) return process.env.OPENCODE_VERSION.trim();
@@ -43,7 +53,6 @@ const owpenbotVersion = (() => {
4353
}
4454
return null;
4555
})();
46-
const owpenbotAssetOverride = process.env.OWPENBOT_ASSET?.trim() || null;
4756

4857
// Target triple for native platform binaries
4958
const resolvedTargetTriple = (() => {
@@ -116,11 +125,16 @@ const resolveBuildScript = (dir) => {
116125
const owpenbotBaseName = "owpenbot";
117126
const owpenbotName = process.platform === "win32" ? `${owpenbotBaseName}.exe` : owpenbotBaseName;
118127
const owpenbotPath = join(sidecarDir, owpenbotName);
128+
const owpenbotBuildName = bunTarget
129+
? `${owpenbotBaseName}-${bunTarget}${bunTarget.includes("windows") ? ".exe" : ""}`
130+
: owpenbotName;
131+
const owpenbotBuildPath = join(sidecarDir, owpenbotBuildName);
119132
const owpenbotTargetTriple = resolvedTargetTriple;
120133
const owpenbotTargetName = owpenbotTargetTriple
121134
? `${owpenbotBaseName}-${owpenbotTargetTriple}${owpenbotTargetTriple.includes("windows") ? ".exe" : ""}`
122135
: null;
123136
const owpenbotTargetPath = owpenbotTargetName ? join(sidecarDir, owpenbotTargetName) : null;
137+
const owpenbotDir = resolve(__dirname, "..", "..", "owpenbot");
124138
const readHeader = (filePath, length = 256) => {
125139
const fd = openSync(filePath, "r");
126140
try {
@@ -404,173 +418,112 @@ if (shouldDownloadOpencode) {
404418
console.log(`OpenCode sidecar updated to ${normalizedOpencodeVersion}.`);
405419
}
406420

421+
const owpenbotPkgRaw = readFileSync(resolve(owpenbotDir, "package.json"), "utf8");
422+
const owpenbotPkg = JSON.parse(owpenbotPkgRaw);
423+
const owpenbotPkgVersion = String(owpenbotPkg.version ?? "").trim();
407424
const normalizedOwpenbotVersion = owpenbotVersion?.startsWith("v")
408425
? owpenbotVersion.slice(1)
409426
: owpenbotVersion;
427+
const expectedOwpenbotVersion = normalizedOwpenbotVersion || owpenbotPkgVersion;
410428

411-
if (!normalizedOwpenbotVersion) {
412-
console.error(
413-
"Owpenbot version is not configured. Set OWPENBOT_VERSION or add owpenbotVersion to packages/desktop/package.json."
414-
);
429+
if (!expectedOwpenbotVersion) {
430+
console.error("Owpenbot version missing. Set owpenbotVersion or ensure package.json has version.");
415431
process.exit(1);
416432
}
417433

418-
const owpenbotAssetByTarget = {
419-
"aarch64-apple-darwin": "owpenbot-darwin-arm64.zip",
420-
"x86_64-apple-darwin": "owpenbot-darwin-x64.zip",
421-
"x86_64-unknown-linux-gnu": "owpenbot-linux-x64.tar.gz",
422-
"aarch64-unknown-linux-gnu": "owpenbot-linux-arm64.tar.gz",
423-
"x86_64-pc-windows-msvc": "owpenbot-windows-x64.zip",
424-
"aarch64-pc-windows-msvc": "owpenbot-windows-x64.zip",
425-
};
426-
427-
const owpenbotAsset =
428-
owpenbotAssetOverride ?? (resolvedTargetTriple ? owpenbotAssetByTarget[resolvedTargetTriple] : null);
429-
430-
const owpenbotUrl = owpenbotAsset
431-
? `https://github.com/different-ai/owpenbot/releases/download/v${normalizedOwpenbotVersion}/${owpenbotAsset}`
432-
: null;
433-
434-
const owpenbotCandidatePath = owpenbotTargetPath ?? owpenbotPath;
435-
const existingOwpenbotVersion =
436-
owpenbotCandidatePath && existsSync(owpenbotCandidatePath)
437-
? readBinaryVersion(owpenbotCandidatePath)
438-
: null;
439-
440-
const shouldDownloadOwpenbot =
441-
!owpenbotCandidatePath ||
442-
!existsSync(owpenbotCandidatePath) ||
443-
isStubBinary(owpenbotCandidatePath) ||
444-
!existingOwpenbotVersion ||
445-
existingOwpenbotVersion !== normalizedOwpenbotVersion;
446-
447-
if (!shouldDownloadOwpenbot) {
448-
console.log(`Owpenbot sidecar already present (${existingOwpenbotVersion}).`);
434+
if (normalizedOwpenbotVersion && owpenbotPkgVersion && normalizedOwpenbotVersion !== owpenbotPkgVersion) {
435+
console.error(`Owpenbot version mismatch: desktop=${normalizedOwpenbotVersion}, package=${owpenbotPkgVersion}`);
436+
process.exit(1);
449437
}
450438

451-
if (shouldDownloadOwpenbot) {
452-
if (!owpenbotAsset || !owpenbotUrl) {
453-
console.error(
454-
`No owpenbot asset configured for target ${resolvedTargetTriple ?? "unknown"}. Set OWPENBOT_ASSET to override.`
455-
);
456-
process.exit(1);
457-
}
458-
439+
const shouldBuildOwpenbot = !existsSync(owpenbotBuildPath) || isStubBinary(owpenbotBuildPath);
440+
if (shouldBuildOwpenbot) {
459441
mkdirSync(sidecarDir, { recursive: true });
460-
461-
const stamp = Date.now();
462-
const archivePath = join(tmpdir(), `owpenbot-${stamp}-${owpenbotAsset}`);
463-
const extractDir = join(tmpdir(), `owpenbot-${stamp}`);
464-
const checksumUrl = `https://github.com/different-ai/owpenbot/releases/download/v${normalizedOwpenbotVersion}/SHA256SUMS`;
465-
const checksumPath = join(tmpdir(), `owpenbot-${stamp}-SHA256SUMS`);
466-
467-
mkdirSync(extractDir, { recursive: true });
468-
469-
if (process.platform === "win32") {
470-
const psQuote = (value) => `'${value.replace(/'/g, "''")}'`;
471-
const downloadScript = [
472-
"$ErrorActionPreference = 'Stop'",
473-
`Invoke-WebRequest -Uri ${psQuote(owpenbotUrl)} -OutFile ${psQuote(archivePath)}`,
474-
].join("; ");
475-
const checksumScript = [
476-
"$ErrorActionPreference = 'Stop'",
477-
`Invoke-WebRequest -Uri ${psQuote(checksumUrl)} -OutFile ${psQuote(checksumPath)}`,
478-
].join("; ");
479-
480-
const downloadResult = spawnSync("powershell", ["-NoProfile", "-Command", downloadScript], {
481-
stdio: "inherit",
482-
});
483-
if (downloadResult.status !== 0) {
484-
process.exit(downloadResult.status ?? 1);
485-
}
486-
487-
const checksumResult = spawnSync("powershell", ["-NoProfile", "-Command", checksumScript], {
488-
stdio: "inherit",
489-
});
490-
if (checksumResult.status !== 0) {
491-
process.exit(checksumResult.status ?? 1);
492-
}
493-
} else {
494-
const downloadResult = spawnSync("curl", ["-fsSL", "-o", archivePath, owpenbotUrl], {
495-
stdio: "inherit",
496-
});
497-
if (downloadResult.status !== 0) {
498-
process.exit(downloadResult.status ?? 1);
499-
}
500-
501-
const checksumResult = spawnSync("curl", ["-fsSL", "-o", checksumPath, checksumUrl], {
502-
stdio: "inherit",
503-
});
504-
if (checksumResult.status !== 0) {
505-
process.exit(checksumResult.status ?? 1);
442+
if (existsSync(owpenbotBuildPath)) {
443+
try {
444+
unlinkSync(owpenbotBuildPath);
445+
} catch {
446+
// ignore
506447
}
507448
}
508-
509-
const checksumContent = readFileSync(checksumPath, "utf8");
510-
const expectedHash = parseChecksum(checksumContent, owpenbotAsset);
511-
if (!expectedHash) {
512-
console.error(`Owpenbot checksum missing for ${owpenbotAsset}.`);
513-
process.exit(1);
514-
}
515-
const actualHash = sha256File(archivePath);
516-
if (actualHash !== expectedHash) {
517-
console.error(`Owpenbot checksum mismatch for ${owpenbotAsset}.`);
449+
const owpenbotScript = resolveBuildScript(owpenbotDir);
450+
if (!existsSync(owpenbotScript)) {
451+
console.error(`Owpenbot build script not found at ${owpenbotScript}`);
518452
process.exit(1);
519453
}
520-
521-
if (process.platform === "win32") {
522-
const psQuote = (value) => `'${value.replace(/'/g, "''")}'`;
523-
const extractScript = [
524-
"$ErrorActionPreference = 'Stop'",
525-
`Expand-Archive -Path ${psQuote(archivePath)} -DestinationPath ${psQuote(extractDir)} -Force`,
526-
].join("; ");
527-
const extractResult = spawnSync("powershell", ["-NoProfile", "-Command", extractScript], {
528-
stdio: "inherit",
529-
});
530-
if (extractResult.status !== 0) {
531-
process.exit(extractResult.status ?? 1);
532-
}
533-
} else if (owpenbotAsset.endsWith(".zip")) {
534-
const unzipResult = spawnSync("unzip", ["-q", archivePath, "-d", extractDir], {
535-
stdio: "inherit",
536-
});
537-
if (unzipResult.status !== 0) {
538-
process.exit(unzipResult.status ?? 1);
539-
}
540-
} else if (owpenbotAsset.endsWith(".tar.gz")) {
541-
const tarResult = spawnSync("tar", ["-xzf", archivePath, "-C", extractDir], {
542-
stdio: "inherit",
543-
});
544-
if (tarResult.status !== 0) {
545-
process.exit(tarResult.status ?? 1);
546-
}
547-
} else {
548-
console.error(`Unknown owpenbot archive type: ${owpenbotAsset}`);
549-
process.exit(1);
454+
const owpenbotArgs = [owpenbotScript, "--outdir", sidecarDir, "--filename", "owpenbot"];
455+
if (bunTarget) {
456+
owpenbotArgs.push("--target", bunTarget);
550457
}
551-
552-
const extractedBinary = findOwpenbotBinary(extractDir);
553-
if (!extractedBinary) {
554-
console.error("Owpenbot binary not found after extraction.");
555-
process.exit(1);
458+
const result = spawnSync("bun", owpenbotArgs, { cwd: owpenbotDir, stdio: "inherit" });
459+
if (result.status !== 0) {
460+
process.exit(result.status ?? 1);
556461
}
462+
}
557463

558-
const owpenbotTargets = [owpenbotTargetPath, owpenbotPath].filter(Boolean);
559-
for (const target of owpenbotTargets) {
464+
if (existsSync(owpenbotBuildPath)) {
465+
const shouldCopyCanonical = !existsSync(owpenbotPath) || isStubBinary(owpenbotPath);
466+
if (shouldCopyCanonical && owpenbotBuildPath !== owpenbotPath) {
560467
try {
561-
if (existsSync(target)) {
562-
unlinkSync(target);
563-
}
468+
if (existsSync(owpenbotPath)) unlinkSync(owpenbotPath);
564469
} catch {
565470
// ignore
566471
}
567-
copyFileSync(extractedBinary, target);
568-
try {
569-
chmodSync(target, 0o755);
570-
} catch {
571-
// ignore
472+
copyFileSync(owpenbotBuildPath, owpenbotPath);
473+
}
474+
475+
if (owpenbotTargetPath) {
476+
const shouldCopyTarget = !existsSync(owpenbotTargetPath) || isStubBinary(owpenbotTargetPath);
477+
if (shouldCopyTarget && owpenbotBuildPath !== owpenbotTargetPath) {
478+
try {
479+
if (existsSync(owpenbotTargetPath)) unlinkSync(owpenbotTargetPath);
480+
} catch {
481+
// ignore
482+
}
483+
copyFileSync(owpenbotBuildPath, owpenbotTargetPath);
572484
}
573485
}
486+
}
487+
488+
const openworkServerVersion = (() => {
489+
try {
490+
const raw = readFileSync(resolve(openworkServerDir, "package.json"), "utf8");
491+
return String(JSON.parse(raw).version ?? "").trim();
492+
} catch {
493+
return null;
494+
}
495+
})();
496+
497+
const versions = {
498+
opencode: {
499+
version: normalizedOpencodeVersion,
500+
sha256: opencodeCandidatePath && existsSync(opencodeCandidatePath) ? sha256File(opencodeCandidatePath) : null,
501+
},
502+
"openwork-server": {
503+
version: openworkServerVersion,
504+
sha256: existsSync(openworkServerPath) ? sha256File(openworkServerPath) : null,
505+
},
506+
owpenbot: {
507+
version: expectedOwpenbotVersion,
508+
sha256: existsSync(owpenbotPath) ? sha256File(owpenbotPath) : null,
509+
},
510+
};
574511

575-
console.log(`Owpenbot sidecar updated to ${normalizedOwpenbotVersion}.`);
512+
const missing = Object.entries(versions)
513+
.filter(([, info]) => !info.version || !info.sha256)
514+
.map(([name]) => name);
515+
516+
if (missing.length) {
517+
console.error(`Sidecar version metadata incomplete for: ${missing.join(", ")}`);
518+
process.exit(1);
519+
}
520+
521+
const versionsPath = join(sidecarDir, "versions.json");
522+
try {
523+
mkdirSync(sidecarDir, { recursive: true });
524+
const content = JSON.stringify(versions, null, 2) + "\n";
525+
writeFileSync(versionsPath, content, "utf8");
526+
} catch (error) {
527+
console.error(`Failed to write versions.json: ${error}`);
528+
process.exit(1);
576529
}

‎packages/desktop/src-tauri/src/commands/owpenbot.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ pub async fn owpenbot_status(
241241
.and_then(|value| value.get("url"))
242242
.and_then(|value| value.as_str())
243243
.unwrap_or_default();
244+
let health_port = status
245+
.get("healthPort")
246+
.and_then(|value| value.as_u64());
244247

245248
let whatsapp_linked = whatsapp
246249
.get("linked")
@@ -268,6 +271,7 @@ pub async fn owpenbot_status(
268271
Ok(serde_json::json!({
269272
"running": running,
270273
"config": config_path,
274+
"healthPort": health_port,
271275
"whatsapp": {
272276
"linked": whatsapp_linked,
273277
"dmPolicy": whatsapp_dm_policy,

‎packages/headless/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"dev": "bun src/cli.ts",
1111
"build": "tsc -p tsconfig.json",
12-
"build:bin": "pnpm --filter openwork-server build:bin && node scripts/build-owpenbot.mjs && bun build --compile src/cli.ts --outfile dist/openwrk && bun scripts/build-bin.ts",
12+
"build:bin": "node ../desktop/scripts/prepare-sidecar.mjs --outdir dist && bun build --compile src/cli.ts --outfile dist/openwrk",
1313
"typecheck": "tsc -p tsconfig.json --noEmit",
1414
"test:router": "pnpm build && node scripts/router.mjs",
1515
"prepublishOnly": "pnpm build:bin"

0 commit comments

Comments
 (0)