Files
isphere-ai-bridge/scripts/verify-msglib-sidecar.ps1
2026-07-10 03:02:16 +08:00

108 lines
3.7 KiB
PowerShell

param(
[string]$SidecarExe = "runs/msglib-sidecar/MsgLibReadSidecar.exe",
[string]$SQLiteDllPath = $env:ISPHERE_MSGLIB_SQLITE_DLL,
[string]$DbPath = $env:ISPHERE_MSGLIB_DB,
[switch]$SkipBuild
)
$ErrorActionPreference = "Stop"
$repo = Resolve-Path -LiteralPath (Join-Path $PSScriptRoot "..")
$sidecarPath = Join-Path $repo $SidecarExe
function Invoke-SidecarJson([hashtable]$Request) {
$json = $Request | ConvertTo-Json -Depth 10 -Compress
$output = $json | & $sidecarPath
if ($LASTEXITCODE -ne 0) {
throw "sidecar exited with code $LASTEXITCODE"
}
try {
return $output | ConvertFrom-Json
}
catch {
throw "sidecar output was not JSON: $output"
}
}
if (-not $SkipBuild) {
& powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $repo "scripts\build-msglib-sidecar.ps1") | Out-Host
if ($LASTEXITCODE -ne 0) {
throw "build-msglib-sidecar.ps1 failed with exit code $LASTEXITCODE"
}
}
if (-not (Test-Path -LiteralPath $sidecarPath)) {
throw "sidecar not found: $sidecarPath"
}
$self = Invoke-SidecarJson @{
protocol = "isphere.msglib.v1"
request_id = "verify-self-check"
op = "self_check"
args = @{}
}
if (-not $self.ok -or $self.data.sidecar_name -ne "MsgLibReadSidecar" -or $self.data.process_bits -ne 32) {
throw "self_check failed: $($self | ConvertTo-Json -Depth 8 -Compress)"
}
$providerChecked = $false
$displaySources = $null
$displayEntitySummaries = @()
if ($SQLiteDllPath -and $DbPath) {
if (-not (Test-Path -LiteralPath $SQLiteDllPath)) {
throw "SQLite DLL not found: $SQLiteDllPath"
}
if (-not (Test-Path -LiteralPath $DbPath)) {
throw "MsgLib DB not found: $DbPath"
}
$displaySources = Invoke-SidecarJson @{
protocol = "isphere.msglib.v1"
request_id = "verify-display-sources"
op = "display_sources"
args = @{
sqlite_dll_path = $SQLiteDllPath
db_path = $DbPath
password = "123"
include_counts = $true
max_tables = 64
}
}
if (-not $displaySources.ok -or -not $displaySources.data.metadata_only -or $displaySources.data.message_body_values_returned) {
throw "display_sources failed"
}
foreach ($entityType in @("contacts", "groups")) {
$displayEntities = Invoke-SidecarJson @{
protocol = "isphere.msglib.v1"
request_id = "verify-display-entities-$entityType"
op = "display_entities"
args = @{
sqlite_dll_path = $SQLiteDllPath
db_path = $DbPath
password = "123"
entity_type = $entityType
limit = 5
}
}
if (-not $displayEntities.ok -or -not $displayEntities.data.metadata_only -or -not $displayEntities.data.read_only -or $displayEntities.data.message_body_values_returned -or $displayEntities.data.raw_rows_returned) {
throw "display_entities failed for $entityType"
}
$sourceTables = @($displayEntities.data.entities | ForEach-Object { $_.source_table } | Sort-Object -Unique)
$displayEntitySummaries += [ordered]@{
entity_type = $entityType
entity_count = @($displayEntities.data.entities).Count
source_tables = $sourceTables
}
}
$providerChecked = $true
}
[ordered]@{
ok = $true
sidecar = (Resolve-Path -LiteralPath $sidecarPath).Path
version = $self.data.sidecar_version
process_bits = $self.data.process_bits
provider_checked = $providerChecked
display_source_count = if ($displaySources) { $displaySources.data.display_sources.Count } else { 0 }
display_entity_summaries = $displayEntitySummaries
} | ConvertTo-Json -Depth 8 -Compress