Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions .github/workflows/desktop-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Desktop release

on:
push:
tags: ['v*']
workflow_dispatch:
inputs:
tag:
description: Existing release tag (for example, v0.1.4)
required: true
type: string

permissions:
contents: read

concurrency:
group: desktop-release-${{ inputs.tag || github.ref_name }}
cancel-in-progress: false

env:
RELEASE_TAG: ${{ inputs.tag || github.ref_name }}

jobs:
build:
runs-on: macos-15
timeout-minutes: 45
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
ref: refs/tags/${{ env.RELEASE_TAG }}
persist-credentials: false
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0
with:
node-version-file: .nvmrc
- name: Check release version and architecture
run: |
test "$RELEASE_TAG" = "v$(node -p 'require("./apps/desktop/package.json").version')"
test "$(uname -m)" = arm64
- name: Prepare build tools
run: |
brew install uv
media="$RUNNER_TEMP/desktop-media"
mkdir -p "$media"
for tool in ffmpeg ffprobe; do
curl --fail --silent --show-error --location --retry 3 \
"https://github.com/descriptinc/ffmpeg-ffprobe-static/releases/download/b6.1.2-rc.1/${tool}-darwin-arm64" \
--output "$media/$tool"
chmod +x "$media/$tool"
"$media/$tool" -version
done
echo "OPENDIRECTOR_FFMPEG_DIR=$media" >> "$GITHUB_ENV"
- name: Build desktop application
env:
CSC_IDENTITY_AUTO_DISCOVERY: 'false'
run: |
npm ci --prefix apps/desktop
npm run prepare:runtime --prefix apps/desktop
npm run pack --prefix apps/desktop
- name: Build and smoke-test DMG
run: |
staging="$RUNNER_TEMP/desktop-dmg"
mount="$RUNNER_TEMP/desktop-mount"
mkdir -p "$staging" "$mount" release-assets
ditto apps/desktop/build/mac-arm64/OpenDirector.app "$staging/OpenDirector.app"
ln -s /Applications "$staging/Applications"
dmg="OpenDirector-${RELEASE_TAG}-mac-arm64-unsigned.dmg"
hdiutil create -volname OpenDirector -srcfolder "$staging" -format UDZO "release-assets/$dmg"
hdiutil attach "release-assets/$dmg" -readonly -nobrowse -mountpoint "$mount"
trap 'hdiutil detach "$mount"' EXIT
node apps/desktop/scripts/smoke.mjs "$mount/OpenDirector.app/Contents/MacOS/OpenDirector"
cd release-assets
shasum -a 256 "$dmg" > "${dmg}.sha256"
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: desktop-dmg
path: release-assets/
compression-level: 0
if-no-files-found: error

release:
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
with:
name: desktop-dmg
path: release-assets
- name: Attach verified DMG to draft release
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: |
set -euo pipefail
draft=$(gh api --paginate "repos/$GH_REPO/releases" \
--jq ".[] | select(.tag_name == \"$RELEASE_TAG\") | .draft")
case "$draft" in
true) ;;
'') gh release create "$RELEASE_TAG" --verify-tag --draft --generate-notes ;;
*) echo "::error::Release $RELEASE_TAG is already published"; exit 1 ;;
esac
gh release upload "$RELEASE_TAG" release-assets/* --clobber
21 changes: 21 additions & 0 deletions .github/workflows/desktop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Desktop checks
on:
pull_request:
paths:
- 'apps/desktop/**'
- 'apps/api/src/opendirector_api/desktop.py'
- 'tests/test_desktop_host.py'
- '.github/workflows/desktop.yml'
workflow_dispatch:
permissions:
contents: read
jobs:
shell:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0
with:
node-version-file: .nvmrc
- run: npm ci --ignore-scripts --prefix apps/desktop
- run: npm test --prefix apps/desktop
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ __pycache__/

/build/
/packages/core/src/opendirector/_bundled/
/apps/desktop/runtime/
/apps/desktop/build/
5 changes: 3 additions & 2 deletions apps/api/src/opendirector_api/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ async def follow(self):
await self.changed.wait()


def agent_routes(projects, generation, configs, provider=None) -> APIRouter:
def agent_routes(projects, generation, configs, provider=None, *, active=None) -> APIRouter:
router = APIRouter(prefix='/api/creation/projects/{project_id}/assistant')
conversations = Conversations(projects)
runtimes = AgentRuntimes(projects, generation, configs, provider)
Expand All @@ -257,7 +257,8 @@ def trace(project_id: UUID, turn_id: UUID,
raise ProjectError('轨迹文件不存在。', 404)
return FileResponse(file, media_type='text/plain', headers={'Cache-Control': 'no-store'})

active: dict[UUID, tuple[UUID, asyncio.Task, RunEvents]] = {}
if active is None:
active = {}
admission: dict[UUID, asyncio.Lock] = {}

@router.get('')
Expand Down
176 changes: 176 additions & 0 deletions apps/api/src/opendirector_api/desktop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
"""Private parent-controlled API host for the desktop application."""
import asyncio
from contextlib import redirect_stdout
import json
import logging
import os
import secrets
import shutil
import socket
import subprocess
import sys
import threading
from typing import Annotated, Literal
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field, TypeAdapter
import uvicorn
from starlette.responses import JSONResponse
from starlette.types import ASGIApp, Scope, Receive, Send
from starlette.requests import Request

from opendirector_api.localization import localized_error

from opendirector.infrastructure.local_projects import LocalProjects
from opendirector.installation import data_root
from opendirector_api.main import create_app
from opendirector_api.server import logging_config


class DesktopAuthentication:
"""Protect every HTTP resource, including media and the application document."""

def __init__(self, app: ASGIApp, *, token: str) -> None:
self.app = app
self.token = token

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
headers = dict(scope.get('headers', []))
supplied = headers.get(b'x-opendirector-desktop', b'')
if not secrets.compare_digest(supplied, self.token.encode('ascii')):
if scope['type'] == 'websocket':
await send({'type': 'websocket.close', 'code': 1008})
elif scope['type'] == 'http':
await JSONResponse({'detail': localized_error(Request(scope), chinese='桌面连接已失效,请重新打开应用。', status=403, code='desktop_auth_required')}, status_code=403)(scope, receive, send)
else:
await self.app(scope, receive, send)
return
await self.app(scope, receive, send)


class Initialize(BaseModel):
model_config = ConfigDict(extra='forbid')
type: Literal['initialize']
version: Literal[1]
token: str = Field(min_length=64, max_length=64, pattern=r'^[a-f0-9]+$')


class Control(BaseModel):
model_config = ConfigDict(extra='forbid')
type: Literal['inspect', 'shutdown']
requestId: int = Field(ge=1)


class SessionRegistration(BaseModel):
model_config = ConfigDict(extra='forbid')
type: Literal['session']
sessionId: UUID


CONTROL = TypeAdapter(Annotated[Control | SessionRegistration, Field(discriminator='type')])


def runtime_check() -> dict[str, str]:
from opendirector.agent.native import executable
from opendirector.installation import web_root
if not (web_root() / 'index.html').is_file():
raise RuntimeError('Web resources are missing.')
commands = {'agent': executable('default'), 'ffmpeg': shutil.which('ffmpeg'), 'ffprobe': shutil.which('ffprobe')}
versions = {'python': sys.version.split()[0]}
for name, path in commands.items():
if not path:
raise RuntimeError(f'{name} is missing from the bundled runtime.')
result = subprocess.run([path, '--version' if name == 'agent' else '-version'],
capture_output=True, text=True, timeout=20, check=True)
versions[name] = result.stdout.splitlines()[0]
return versions


async def serve(token: str) -> None:
output = sys.stdout

def emit(event: dict[str, object]) -> None:
output.write(json.dumps(event) + '\n')
output.flush()

emit({'type': 'starting', 'version': 1})
with redirect_stdout(sys.stderr):
versions = await asyncio.to_thread(runtime_check)
store = LocalProjects(data_root())
app = create_app(store)
app.add_middleware(DesktopAuthentication, token=token)
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.bind(('127.0.0.1', 0))
listener.listen(128)
port = listener.getsockname()[1]
os.environ['OPENDIRECTOR_API_URL'] = f'http://127.0.0.1:{port}'
sessions: set[str] = set()
commands: asyncio.Queue[str] = asyncio.Queue()
loop = asyncio.get_running_loop()

def read_controls() -> None:
while line := sys.stdin.readline(65537):
if len(line) > 65536:
loop.call_soon_threadsafe(commands.put_nowait, 'invalid\n')
return
loop.call_soon_threadsafe(commands.put_nowait, line)
loop.call_soon_threadsafe(commands.put_nowait, '')

class DesktopServer(uvicorn.Server):
async def startup(self, sockets: list[socket.socket] | None = None) -> None:
await super().startup(sockets)
if not self.should_exit:
emit({'type': 'ready', 'version': 1, 'url': f'http://127.0.0.1:{port}', 'runtime': versions})

server = DesktopServer(uvicorn.Config(app, log_config=logging_config(), access_log=False,
timeout_graceful_shutdown=15))

async def control() -> None:
while True:
line = await commands.get()
if not line:
server.should_exit = True
return
message = CONTROL.validate_json(line)
if message.type == 'session':
sessions.add(str(message.sessionId))
continue
if message.type == 'shutdown':
server.should_exit = True
return
emit({'type': 'inspection', 'requestId': message.requestId, **app.state.desktop_activity()})

threading.Thread(target=read_controls, name='desktop-parent', daemon=True).start()
reader = asyncio.create_task(control())
try:
serving = asyncio.create_task(server.serve(sockets=[listener]))
done, _ = await asyncio.wait([reader, serving], return_when=asyncio.FIRST_COMPLETED)
if reader in done and not reader.cancelled() and reader.exception():
server.should_exit = True
await serving
raise reader.exception()
await serving
finally:
reader.cancel()
await asyncio.gather(reader, return_exceptions=True)
listener.close()
for session_id in sessions:
await asyncio.to_thread(store.release, session_id)
emit({'type': 'shutdown-complete'})


def main() -> None:
try:
message = Initialize.model_validate_json(sys.stdin.readline(65537))
asyncio.run(serve(message.token))
except ValueError:
print(json.dumps({'type': 'fatal', 'version': 1, 'message': 'Invalid desktop control message.'}), flush=True)
raise SystemExit(1) from None
except (OSError, RuntimeError, subprocess.SubprocessError) as error:
logging.getLogger(__name__).exception('Desktop Host failed')
print(json.dumps({'type': 'fatal', 'version': 1, 'message': str(error)}), flush=True)
raise SystemExit(1) from error


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions apps/api/src/opendirector_api/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from opendirector.domain.creation.commands import CommandReceipt

ENGLISH_BY_CODE = {
"desktop_auth_required": "The desktop connection has expired. Reopen the application.",
'image_quality_unsupported': 'This model does not support the selected image quality. Choose Auto or select another model.',
'bigmodel_video_model': 'The BigModel video protocol supports CogVideoX-3 only.',
'bigmodel_video_output': 'Choose a supported CogVideoX-3 pixel size and a 5- or 10-second duration. The size determines the aspect ratio.',
Expand Down
12 changes: 11 additions & 1 deletion apps/api/src/opendirector_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,33 @@ def create_app(store: LocalProjects | None = None, chat_provider=None, generatio
creation_tools = ToolTasks(store, model_configs, CreationToolAssets(creation_projects), projects=creation_projects)
catalog = ModelCatalog(model_configs, chat_provider or ChatCompletions())

active_agents = {}

@asynccontextmanager
async def lifespan(app):
await creation_generation.start()
try:
await creation_tools.start()
yield
finally:
agents = [entry[1] for entry in list(active_agents.values())]
for agent in agents:
agent.cancel()
await asyncio.gather(*agents, return_exceptions=True)
try:
await creation_tools.close()
finally:
await creation_generation.close()

app = FastAPI(title="OpenDirector Local API", version="0.1.4", lifespan=lifespan)
app.include_router(creation_routes(creation_projects, creation_generation))
app.include_router(agent_routes(creation_projects, creation_generation, model_configs, chat_provider))
app.include_router(agent_routes(creation_projects, creation_generation, model_configs, chat_provider, active=active_agents))
app.include_router(library_routes(creation_projects))
app.include_router(voice_routes(creation_generation.voices))
app.state.desktop_activity = lambda: dict(
agents=len(active_agents), generations=sum(not job.future.done() for job in creation_generation.jobs.values()),
tools=sum(not job.done() for job in creation_tools.jobs.values()))


@app.exception_handler(RequestValidationError)
async def invalid_request(request, error):
Expand Down
Loading
Loading