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 = @() $messageSources = $null $messageSourceSummaries = @() $messageList = $null $messageListSources = @() $messageListContentTextNonEmptyCount = 0 $messageListDownloadRefNonEmptyCount = 0 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" } $messageSources = Invoke-SidecarJson @{ protocol = "isphere.msglib.v1" request_id = "verify-message-sources" op = "message_sources" args = @{ sqlite_dll_path = $SQLiteDllPath db_path = $DbPath password = "123" } } if (-not $messageSources.ok -or -not $messageSources.data.metadata_only -or -not $messageSources.data.read_only -or $messageSources.data.message_body_values_returned -or $messageSources.data.raw_rows_returned -or $messageSources.data.file_paths_returned) { throw "message_sources failed" } foreach ($source in @($messageSources.data.message_sources)) { $messageSourceSummaries += [ordered]@{ source = $source.source table = $source.table role = $source.role available = $source.available row_count = $source.row_count } } $messageList = Invoke-SidecarJson @{ protocol = "isphere.msglib.v1" request_id = "verify-list-messages" op = "list_messages" args = @{ sqlite_dll_path = $SQLiteDllPath db_path = $DbPath password = "123" conversation_type = "auto" limit = 5 include_body = $false include_attachment_metadata = $true } } if (-not $messageList.ok -or -not $messageList.data.read_only -or $messageList.data.message_body_values_returned -or $messageList.data.raw_rows_returned -or $messageList.data.file_paths_returned) { throw "list_messages failed" } $messageListRows = @($messageList.data.messages) $messageListSources = @($messageList.data.source_tables) $messageListContentTextNonEmptyCount = @($messageListRows | Where-Object { $_.content_text -or $_.text }).Count $messageListDownloadRefNonEmptyCount = @($messageListRows | ForEach-Object { @($_.attachments) } | Where-Object { $_.download_ref }).Count if ($messageListContentTextNonEmptyCount -ne 0 -or $messageListDownloadRefNonEmptyCount -ne 0) { throw "list_messages returned body text or download refs while include_body=false" } 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 message_source_count = if ($messageSources) { $messageSources.data.message_sources.Count } else { 0 } message_source_summaries = $messageSourceSummaries message_list_count = if ($messageList) { @($messageList.data.messages).Count } else { 0 } message_list_sources = $messageListSources message_list_body_values_returned = if ($messageList) { [bool]$messageList.data.message_body_values_returned } else { $false } message_list_raw_rows_returned = if ($messageList) { [bool]$messageList.data.raw_rows_returned } else { $false } message_list_file_paths_returned = if ($messageList) { [bool]$messageList.data.file_paths_returned } else { $false } message_list_content_values_omitted = ($messageListContentTextNonEmptyCount -eq 0) message_list_download_refs_omitted = ($messageListDownloadRefNonEmptyCount -eq 0) message_body_values_returned = $false raw_rows_returned = $false file_paths_returned = $false } | ConvertTo-Json -Depth 8 -Compress