openartifacts-publish.ps1 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  2. $OutputEncoding = [System.Text.Encoding]::UTF8
  3. $SOURCE = if ($args.Count -ge 1) { $args[0] } else { '' }
  4. $STAGED_HTML = if ($args.Count -ge 2) { $args[1] } else { '' }
  5. if (($args.Count -ne 1 -and $args.Count -ne 2) -or -not $SOURCE -or ($args.Count -eq 2 -and -not $STAGED_HTML)) {
  6. [Console]::Error.WriteLine('Usage: openartifacts-publish.ps1 <source-note-path> [staged-html-path]')
  7. exit 1
  8. }
  9. $OBSIDIAN_CLI = [Environment]::GetEnvironmentVariable('COPILOT_OBSIDIAN_CLI')
  10. $WORKSPACE_ROOT = [Environment]::GetEnvironmentVariable('OPENARTIFACTS_WORKSPACE_ROOT')
  11. if (-not $WORKSPACE_ROOT) {
  12. [Console]::Error.WriteLine('The owning Obsidian workspace is unavailable.')
  13. exit 1
  14. }
  15. $EXIT_CODE = 0
  16. try {
  17. if (-not $OBSIDIAN_CLI) { throw 'A compatible Obsidian CLI is unavailable.' }
  18. Set-Location -LiteralPath $WORKSPACE_ROOT
  19. $VAULT_NAME = Split-Path -Leaf (Get-Location).Path
  20. if (-not $VAULT_NAME) { throw 'The owning Obsidian vault is unavailable.' }
  21. $SOURCE_B64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($SOURCE))
  22. if ($args.Count -eq 1) {
  23. $CODE = "(()=>{const decode=(value)=>new TextDecoder().decode(Uint8Array.from(atob(value),(char)=>char.charCodeAt(0)));const bridge=app.plugins.plugins.copilot?.openArtifactsAgentBridge;if(!bridge)throw new Error('Copilot OpenArtifacts host is unavailable.');return bridge.reviewAgentManage(decode('$SOURCE_B64')).then(JSON.stringify);})()"
  24. } else {
  25. $HTML_B64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($STAGED_HTML))
  26. $CODE = "(()=>{const decode=(value)=>new TextDecoder().decode(Uint8Array.from(atob(value),(char)=>char.charCodeAt(0)));const bridge=app.plugins.plugins.copilot?.openArtifactsAgentBridge;if(!bridge)throw new Error('Copilot OpenArtifacts host is unavailable.');return bridge.reviewAgentPublish(decode('$SOURCE_B64'),decode('$HTML_B64')).then(JSON.stringify);})()"
  27. }
  28. $CLI_OUTPUT = & $OBSIDIAN_CLI "vault=$VAULT_NAME" 'eval' "code=$CODE"
  29. if ($LASTEXITCODE -ne 0) { throw 'Copilot could not complete the OpenArtifacts review.' }
  30. $CLI_RESULT = [string](@($CLI_OUTPUT | Where-Object { ([string]$_).StartsWith('=> {') })[-1])
  31. if (-not $CLI_RESULT.StartsWith('=> {')) {
  32. throw 'Copilot could not complete the OpenArtifacts review.'
  33. }
  34. $OUTCOME = $CLI_RESULT.Substring(3)
  35. [Console]::Out.Write($OUTCOME)
  36. } catch {
  37. [Console]::Error.WriteLine($_.Exception.Message)
  38. $EXIT_CODE = 1
  39. }
  40. exit $EXIT_CODE