Skip to content
Merged
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
12 changes: 12 additions & 0 deletions Documentation/ExtensionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fxcodex workspace list --json
fxcodex workspace paths work --json
fxcodex open work --json
fxcodex open --workspace-id 00000000-0000-0000-0000-000000000001 --json
fxcodex open-url work 'codex://auth/callback?code=…' --json
fxcodex delete work personal --yes --json
```

Expand Down Expand Up @@ -82,6 +83,7 @@ The machine interface is available for:
- `workspace rename`
- `workspace use` and its root `use` alias
- `workspace open` and its root `open` alias
- `open-url`
- `workspace delete` and its root `delete` alias
- `workspace erase` and its root `erase` alias
- `integrations attributes get`
Expand All @@ -101,6 +103,16 @@ already records the directory; this component is deprecated in favor of the
extension. Extension installation requires `--yes` when Raycast is absent, or
`--open-in-browser` to bypass Raycast installation.

## Delivering a link

`open-url <name> <url>` hands an app link to one workspace's Codex instance. A
running instance receives a `kAEGetURL` Apple Event addressed to its process
ID; a stopped one launches with the workspace's environment and the link. The
response data contains `workspace_name`, `workspace_id`, `process_id` and
`delivery` (`apple-event` or `launch`). Errors use `workspace_not_found`,
`unsupported_url` (the scheme isn't one Codex registers, or the value isn't a
URL), `application_not_found` and `url_delivery_failed`.

## Stable workspace identities

Schema 2.0 assigns every workspace a stable lowercase UUID. Workspace names
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ let package = Package(
),
.package(
url: "https://github.com/pointfreeco/swift-custom-dump.git",
.upToNextMajor(from: "1.6.1")
// Keep the IssueReporting package graph used by Dependencies 1.14.
.upToNextMinor(from: "1.6.1")
),
.package(
url: "https://github.com/onmyway133/Promptberry.git",
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ name.
- [Quick start](#quick-start)
- [Desktop and terminal usage](#desktop-and-terminal-usage)
- [Managing workspaces](#managing-workspaces)
- [Delivering links to a workspace](#delivering-links-to-a-workspace)
- [App naming](#app-naming)
- [Updates](#updates)
- [Integration attributes](#integration-attributes)
Expand Down Expand Up @@ -195,6 +196,7 @@ Arguments after `--` are forwarded unchanged to `codex` or `codex exec`.
| Create and select it | `fxcodex workspace create <name> --use` |
| Create and open it | `fxcodex workspace create <name> --open` |
| Open or focus a workspace | `fxcodex open [name]` |
| Deliver a link to one workspace | `fxcodex open-url <name> <url>` |
| Select the current workspace | `fxcodex use [name]` |
| Rename the current managed workspace | `fxcodex workspace rename <new-name>` |
| Rename a specified managed workspace | `fxcodex workspace rename <old-name> <new-name>` |
Expand Down Expand Up @@ -222,6 +224,39 @@ fxcodex workspace paths work --no-all --codex-home --user-data

Raw components are `workspace-root`, `codex-home`, and `user-data`.

## Delivering links to a workspace

A plain `open codex://…` reaches whichever Codex instance macOS picks. To hand
a link, such as a sign-in callback, to one workspace's Codex:

```sh
fxcodex open-url work 'codex://auth/callback?code=…'
```

If that workspace's Codex is running, fxcodex sends the link to its process
only, as a `kAEGetURL` Apple Event addressed by process ID, the same event
Launch Services would send. If it is not running, that workspace's Codex
launches with its own Codex home, profile and the link. The link never goes
through Launch Services' scheme routing.

The command accepts the app-link schemes the installed Codex registers in its
`CFBundleURLTypes` (`codex` today); `http` and `https` are not app links and are
rejected. It exits non-zero with a one-line message on stderr when the workspace
doesn't exist, Codex doesn't handle the link, or delivery fails. `--json`
returns the workspace, process ID and `delivery` (`apple-event` or `launch`).

The first link sent to a running instance can trigger macOS's Automation prompt
("… wants to control Codex") for the app that runs fxcodex, such as Terminal or
Arx. If it was declined, allow it in System Settings > Privacy & Security >
Automation.

In Arx, Air Traffic Control can run a script for `codex://` links, chosen per
profile, with the link as `$1`:

```sh
fxcodex open-url work "$1"
```

## App naming

App renaming is optional and independent of workspace isolation. Rename a valid
Expand Down Expand Up @@ -380,6 +415,7 @@ integrations:
fxcodex --json status --all
FXCODEX_JSON=1 fxcodex workspace list
fxcodex workspace paths work --json --no-user-data
fxcodex open-url work 'codex://…' --json
fxcodex integrations attributes get raycast --path script_commands --json
fxcodex integrations raycast install script-command \
--directory "$HOME/.config/raycast/script-commands" \
Expand Down
9 changes: 9 additions & 0 deletions Scripts/fxcodex.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- open-url sends kAEGetURL to a workspace's Codex; the hardened runtime blocks Apple Events without this. -->
<key>com.apple.security.automation.apple-events</key>
<true/>
</dict>
</plist>
11 changes: 10 additions & 1 deletion Scripts/sign-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ CODESIGN="${CODESIGN:-codesign}"
SHASUM="${SHASUM:-shasum}"
SIGNING_IDENTITY="${SIGNING_IDENTITY:-}"
SIGNING_KEYCHAIN="${SIGNING_KEYCHAIN:-}"
ENTITLEMENTS="${ENTITLEMENTS:-$(dirname -- "$0")/fxcodex.entitlements}"

usage() {
printf '%s\n' \
"Usage: SIGNING_IDENTITY=<identity> ./Scripts/sign-release.sh <executable>..." \
"" \
"Signs each executable with hardened runtime and a secure timestamp," \
"Signs each executable with hardened runtime, the entitlements in" \
"Scripts/fxcodex.entitlements (override with ENTITLEMENTS) and a secure timestamp," \
"verifies the signature, and replaces its adjacent SHA-256 checksum." \
"Set SIGNING_KEYCHAIN when the identity is stored in a non-default keychain."
}
Expand All @@ -21,6 +23,11 @@ if [ -z "$SIGNING_IDENTITY" ] || [ "$#" -eq 0 ]; then
exit 64
fi

if [ ! -f "$ENTITLEMENTS" ]; then
printf 'Missing entitlements: %s\n' "$ENTITLEMENTS" >&2
exit 1
fi

for executable in "$@"; do
if [ ! -f "$executable" ]; then
printf 'Missing executable: %s\n' "$executable" >&2
Expand All @@ -32,13 +39,15 @@ for executable in "$@"; do
--force \
--keychain "$SIGNING_KEYCHAIN" \
--options runtime \
--entitlements "$ENTITLEMENTS" \
--sign "$SIGNING_IDENTITY" \
--timestamp \
"$executable"
else
"$CODESIGN" \
--force \
--options runtime \
--entitlements "$ENTITLEMENTS" \
--sign "$SIGNING_IDENTITY" \
--timestamp \
"$executable"
Expand Down
1 change: 1 addition & 0 deletions Sources/fxcodex-cli/AppCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal struct AppCommand: AsyncParsableCommand {
WorkspaceCommand.Erase.self,
RenameApplicationCommand.self,
OpenCommand.self,
OpenURLCommand.self,
CLICommand.self,
ExecCommand.self,
StatusCommand.self,
Expand Down
59 changes: 59 additions & 0 deletions Sources/fxcodex-cli/Commands/OpenURLCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import ArgumentParser
import Dependencies
import FXCodexClient

extension AppCommand {
internal struct OpenURLCommand: AsyncParsableCommand {
internal static let configuration: CommandConfiguration = .init(
commandName: "open-url",
abstract: "Deliver an app link to one workspace's Codex instance.",
discussion: """
Sends a link such as codex://… to the named workspace's Codex only. A running
instance receives it as an Apple Event addressed to its process ID; otherwise
that workspace's Codex launches with the link. The link never goes through
Launch Services' scheme routing, so other workspaces never see it.

The first delivery to a running instance can trigger macOS's Automation prompt
for the app that runs fxcodex.

Air Traffic Control in Arx can run it as a profile's script:
fxcodex open-url work "$1"
"""
)

@Argument(help: "Workspace name, such as primary or work.")
internal var workspaceName: String

@Argument(help: "The link to deliver, such as codex://….")
internal var url: String

@Flag(
inversion: .prefixedNo,
exclusivity: .chooseLast,
help: "Print a versioned machine-readable JSON response. Defaults from FXCODEX_JSON."
)
internal var json: Bool?

internal init() {}

internal func run() async throws {
@Dependency(\.fxCodexClient)
var client: FXCodexClient

let delivery: URLDelivery = try await client.openURL(self.url, self.workspaceName)

if machineOutputRequested(self.json) {
try printMachineResponse(OpenURLOutput(delivery))
return
}

let reporter: TerminalReporter = await .init()
switch delivery.method {
case .appleEvent:
await reporter.success("Sent the link to Codex · \(delivery.workspace.name) · pid \(delivery.processID).")
case .launch:
await reporter.success("Launched Codex · \(delivery.workspace.name) · pid \(delivery.processID) with the link.")
}
}
}
}
18 changes: 18 additions & 0 deletions Sources/fxcodex-cli/Models/OpenURLOutput.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation
import FXCodexClient

internal struct OpenURLOutput: Encodable {
internal let workspaceName: String
internal let workspaceID: WorkspaceID
internal let processID: Int32
internal let delivery: URLDelivery.Method

internal init(
_ delivery: URLDelivery
) {
self.workspaceName = delivery.workspace.name
self.workspaceID = delivery.workspace.id
self.processID = delivery.processID
self.delivery = delivery.method
}
}
10 changes: 10 additions & 0 deletions Sources/fxcodex-client/Common/FXCodexError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum FXCodexError: Error, Equatable, LocalizedError, Sendable {
case updateExecutableInvalid(URL)
case updateRequestFailed(Int)
case unsupportedSchemaVersion(SchemaVersion)
case unsupportedURL(String, supportedSchemes: [String])
case urlDeliveryFailed(String)
case workspaceAlreadyExists(String)
case workspaceIsRunning(String)
case workspaceNotFound(String)
Expand Down Expand Up @@ -49,6 +51,8 @@ public enum FXCodexError: Error, Equatable, LocalizedError, Sendable {
case .updateExecutableInvalid: "update_executable_invalid"
case .updateRequestFailed: "update_request_failed"
case .unsupportedSchemaVersion: "unsupported_schema_version"
case .unsupportedURL: "unsupported_url"
case .urlDeliveryFailed: "url_delivery_failed"
case .workspaceAlreadyExists: "workspace_already_exists"
case .workspaceIsRunning: "workspace_is_running"
case .workspaceNotFound: "workspace_not_found"
Expand Down Expand Up @@ -120,6 +124,12 @@ public enum FXCodexError: Error, Equatable, LocalizedError, Sendable {
case let .unsupportedSchemaVersion(version):
"Storage schema \(version) is unsupported. This fxcodex build supports schema 2.0."

case let .unsupportedURL(url, supportedSchemes):
"Codex does not handle '\(url)'. Supported schemes: \(supportedSchemes.map { "\($0)://" }.joined(separator: ", "))."

case let .urlDeliveryFailed(message):
"Could not deliver the link to Codex: \(message)"

case let .workspaceAlreadyExists(name):
"Workspace '\(name)' already exists."

Expand Down
2 changes: 2 additions & 0 deletions Sources/fxcodex-client/FXCodexClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public struct FXCodexClient: Sendable {
public var useWorkspaceByID: @Sendable (WorkspaceID) async throws -> Void
public var openWorkspace: @Sendable (String?) async throws -> Int32
public var openWorkspaceByID: @Sendable (WorkspaceID) async throws -> Int32
public var openURL: @Sendable (_ url: String, _ workspaceName: String) async throws -> URLDelivery
public var codexInvocation: @Sendable (String?, [String]) async throws -> CommandInvocation
public var status: @Sendable () async throws -> FXCodexStatus
}
Expand Down Expand Up @@ -63,6 +64,7 @@ extension DependencyValues {
useWorkspaceByID: { try await manager.useWorkspace(id: $0) },
openWorkspace: manager.openWorkspace,
openWorkspaceByID: { try await manager.openWorkspace(id: $0) },
openURL: { try await manager.openURL($0, inWorkspaceNamed: $1) },
codexInvocation: { try await manager.codexInvocation(workspaceName: $0, arguments: $1) },
status: manager.status
)
Expand Down
15 changes: 15 additions & 0 deletions Sources/fxcodex-client/Manager/CodexApplicationClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ struct CodexApplicationClient: Sendable {
var rename: @Sendable (CodexApplicationName) async throws -> CodexApplicationRenameResult
var open: @Sendable (Workspace) async throws -> Int32
var runningProcessID: @Sendable (Workspace) async throws -> Int32?
var urlSchemes: @Sendable () async throws -> [String]
var sendURL: @Sendable (_ url: URL, _ processID: Int32) async throws -> Void
var launchWithURL: @Sendable (_ workspace: Workspace, _ url: URL) async throws -> Int32
var removeRecord: @Sendable (_ workspace: Workspace) async throws -> Void
}

Expand All @@ -31,6 +34,18 @@ extension DependencyValues {
let controller: CodexApplicationController = await .init()
return try await controller.runningProcessID(for: $0)
},
urlSchemes: {
let controller: CodexApplicationController = await .init()
return try await controller.urlSchemes()
},
sendURL: { url, processID in
let controller: CodexApplicationController = await .init()
try await controller.send(url: url, toProcessID: processID)
},
launchWithURL: { workspace, url in
let controller: CodexApplicationController = await .init()
return try await controller.launch(workspace: workspace, with: url)
},
removeRecord: {
let controller: CodexApplicationController = await .init()
try await controller.removeRecord(forWorkspaceID: $0.id)
Expand Down
Loading
Loading