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
18 changes: 18 additions & 0 deletions src/auto.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const c_time = @cImport({
});
const cli = @import("cli.zig");
const io_util = @import("io_util.zig");
const opencode_sync = @import("opencode_sync.zig");
const registry = @import("registry.zig");
const sessions = @import("sessions.zig");
const usage_api = @import("usage_api.zig");
Expand Down Expand Up @@ -1309,6 +1310,20 @@ pub fn maybeAutoSwitchWithUsageFetcher(
return maybeAutoSwitchWithUsageFetcherAndRefreshState(allocator, codex_home, reg, null, usage_fetcher);
}

fn tryRefreshRunningOpencodeAfterSwitch(
allocator: std.mem.Allocator,
codex_home: []const u8,
reg: *const registry.Registry,
) void {
opencode_sync.sync(allocator, codex_home, reg) catch |err| {
std.log.warn("opencode file sync skipped after auto-switch: {s}", .{@errorName(err)});
return;
};
opencode_sync.refreshRunningServers(allocator, codex_home, reg) catch |err| {
std.log.warn("opencode runtime refresh skipped after auto-switch: {s}", .{@errorName(err)});
};
}

pub fn maybeAutoSwitchForDaemonWithUsageFetcher(
allocator: std.mem.Allocator,
codex_home: []const u8,
Expand Down Expand Up @@ -1395,6 +1410,7 @@ pub fn maybeAutoSwitchForDaemonWithUsageFetcher(
const previous_active_key = reg.accounts.items[active_idx].account_key;
const next_active_key = reg.accounts.items[candidate_idx].account_key;
try registry.activateAccountByKey(allocator, codex_home, reg, next_active_key);
tryRefreshRunningOpencodeAfterSwitch(allocator, codex_home, reg);
try refresh_state.candidate_index.handleActiveSwitch(
allocator,
reg,
Expand Down Expand Up @@ -1433,6 +1449,7 @@ pub fn maybeAutoSwitchForDaemonWithUsageFetcher(
const previous_active_key = reg.accounts.items[active_idx].account_key;
const next_active_key = reg.accounts.items[candidate_idx].account_key;
try registry.activateAccountByKey(allocator, codex_home, reg, next_active_key);
tryRefreshRunningOpencodeAfterSwitch(allocator, codex_home, reg);
try refresh_state.candidate_index.handleActiveSwitch(
allocator,
reg,
Expand Down Expand Up @@ -1487,6 +1504,7 @@ fn maybeAutoSwitchWithUsageFetcherAndRefreshState(
}

try registry.activateAccountByKey(allocator, codex_home, reg, reg.accounts.items[candidate_idx].account_key);
tryRefreshRunningOpencodeAfterSwitch(allocator, codex_home, reg);
return .{ .refreshed_candidates = refreshed_candidates, .state_changed = true, .switched = true };
}

Expand Down
17 changes: 17 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,23 @@ pub fn printRemoveSummary(labels: []const []const u8) !void {
try out.flush();
}

pub fn printSwitchSuccess(email: []const u8, alias: []const u8, account_name: ?[]const u8) !void {
var stdout: io_util.Stdout = undefined;
stdout.init();
const out = stdout.out();
try out.print("Switched active account to {s}", .{email});
if (alias.len != 0) {
try out.print(" (alias: {s})", .{alias});
}
if (account_name) |name| {
if (name.len != 0) {
try out.print(" [{s}]", .{name});
}
}
try out.writeAll(".\n");
try out.flush();
}

fn writeCodexLoginLaunchFailureHint(err_name: []const u8, use_color: bool) !void {
var buffer: [512]u8 = undefined;
var writer = std.fs.File.stderr().writer(&buffer);
Expand Down
31 changes: 31 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const registry = @import("registry.zig");
const auth = @import("auth.zig");
const auto = @import("auto.zig");
const format = @import("format.zig");
const opencode_sync = @import("opencode_sync.zig");

const skip_service_reconcile_env = "CODEX_AUTH_SKIP_SERVICE_RECONCILE";
const account_name_refresh_only_env = "CODEX_AUTH_REFRESH_ACCOUNT_NAMES_ONLY";
Expand Down Expand Up @@ -82,6 +83,19 @@ fn runMain() !void {
.clean => |_| try handleClean(allocator, codex_home.?),
}

if (shouldSyncOpencode(cmd)) {
var reg = try registry.loadRegistry(allocator, codex_home.?);
defer reg.deinit(allocator);
opencode_sync.sync(allocator, codex_home.?, &reg) catch |err| {
std.log.warn("opencode file sync skipped: {s}", .{@errorName(err)});
};
if (shouldRefreshRunningOpencode(cmd)) {
opencode_sync.refreshRunningServers(allocator, codex_home.?, &reg) catch |err| {
std.log.warn("opencode runtime refresh skipped: {s}", .{@errorName(err)});
};
}
}

if (shouldReconcileManagedService(cmd)) {
try auto.reconcileManagedService(allocator, codex_home.?);
}
Expand All @@ -103,6 +117,20 @@ pub fn shouldReconcileManagedService(cmd: cli.Command) bool {
};
}

fn shouldSyncOpencode(cmd: cli.Command) bool {
return switch (cmd) {
.list, .login, .import_auth, .switch_account, .remove_account => true,
else => false,
};
}

fn shouldRefreshRunningOpencode(cmd: cli.Command) bool {
return switch (cmd) {
.login, .import_auth, .switch_account, .remove_account => true,
else => false,
};
}

pub const ForegroundUsageRefreshTarget = enum {
list,
switch_account,
Expand Down Expand Up @@ -580,6 +608,9 @@ fn handleSwitch(allocator: std.mem.Allocator, codex_home: []const u8, opts: cli.

try registry.activateAccountByKey(allocator, codex_home, &reg, account_key);
try registry.saveRegistry(allocator, codex_home, &reg);
const selected_idx = registry.findAccountIndexByAccountKey(&reg, account_key) orelse unreachable;
const selected_rec = &reg.accounts.items[selected_idx];
try cli.printSwitchSuccess(selected_rec.email, selected_rec.alias, selected_rec.account_name);
maybeSpawnBackgroundAccountNameRefresh(allocator, &reg);
}

Expand Down
Loading