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
24 changes: 4 additions & 20 deletions apps/desktop/src/acp/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { mockGit, mockQrRows } from "./mockGit";

import { BUILTIN_THEMES } from "../lib/themes";
import { arr, type Json, type JsonObject, num, obj, str } from "./jsonrpc";
import { MockConnections } from "./mockConnections";
import { mockBrowse, mockCreate } from "./mockFolders";
import { ACP_CAPS, HARNESS_MODELS, initialHarnesses, MOCK_CUSTOM_MODELS, withLast } from "./mockHarnesses";
import { MockLoops } from "./mockLoops";
Expand Down Expand Up @@ -93,6 +94,7 @@ export class MockEngine {
private waiting = new Map<number, (result: JsonObject) => void>();
private counter = 0;
private loops = new MockLoops();
private connections = new MockConnections();
private agents = new Map<string, JsonObject[]>();
/** The inbox seed runs once, after the list the app first reads. */
private needsYouSeeded = false;
Expand Down Expand Up @@ -858,6 +860,8 @@ export class MockEngine {

private async handle(method: string, params: JsonObject): Promise<Json> {
if (method.startsWith("_rustcode/loops/")) return this.loops.answer(method, obj(params) ?? {});
if (method === "_rustcode/connections" || method.startsWith("_rustcode/connections/"))
return this.connections.answer(method, obj(params) ?? {});
if (isMockTransferMethod(method)) {
return handleMockTransfer(method, params, {
getThread: (id) => this.thread(id),
Expand Down Expand Up @@ -2467,26 +2471,6 @@ export class MockEngine {
this.mcpRestarted.add(name);
return this.handle("_rustcode/capabilities", {});
}
case "_rustcode/connections":
return {
github: {
cliInstalled: true,
accounts: [{ host: "github.com", login: "octocat", active: true, valid: true, scopes: ["repo"] }],
pending: null,
error: null,
},
accounts: [
{
provider: "openai",
account: "a1",
display: "you@example.com",
method: "chat_gpt",
active: true,
expiresAt: null,
needsSignIn: false,
},
],
};
default:
throw Object.assign(new Error("Method not found"), { code: -32601 });
}
Expand Down
114 changes: 114 additions & 0 deletions apps/desktop/src/acp/mockConnections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { type JsonObject, str } from "./jsonrpc";

const APPROVE_MS = 4000;

interface GitHubRow {
host: string;
login: string;
active: boolean;
valid: boolean;
scopes: string[];
}

interface AccountRow {
provider: string;
account: string;
display: string;
method: string;
active: boolean;
expiresAt: null;
needsSignIn: boolean;
}

/**
* The mock engine's Settings › Connections (`_rustcode/connections/*`): one GitHub sign-in, a
* device-code sign-in that GitHub approves after a few seconds, and three model accounts.
*/
export class MockConnections {
private github: GitHubRow[] = [
{ host: "github.com", login: "octocat", active: true, valid: true, scopes: ["repo", "read:org"] },
];
private accounts: AccountRow[] = [
{
provider: "openai",
account: "a1",
display: "you@example.com",
method: "chat_gpt",
active: true,
expiresAt: null,
needsSignIn: false,
},
{
provider: "anthropic",
account: "work",
display: "Work API key",
method: "api_key",
active: false,
expiresAt: null,
needsSignIn: false,
},
{
provider: "github_copilot",
account: "c1",
display: "octocat",
method: "github_copilot",
active: false,
expiresAt: null,
needsSignIn: true,
},
];
private pending: { code: string; url: string } | null = null;
private approval: ReturnType<typeof setTimeout> | null = null;

snapshot(): JsonObject {
return {
github: {
cliInstalled: true,
installHint: "brew install gh",
accounts: this.github.map((a) => ({ ...a })),
pending: this.pending,
error: null,
},
accounts: this.accounts.map((a) => ({ ...a })),
};
}

async answer(method: string, params: JsonObject): Promise<JsonObject> {
switch (method) {
case "_rustcode/connections/github/login":
this.pending = { code: "4F2A-9C7D", url: "https://github.com/login/device" };
return this.pending;
case "_rustcode/connections/github/wait":
await new Promise<void>((resolve, reject) => {
this.approval = setTimeout(resolve, APPROVE_MS);
this.rejectWait = reject;
});
this.pending = null;
this.github = this.github.filter((a) => a.login !== "hubot").map((a) => ({ ...a, active: false }));
this.github.push({ host: "github.com", login: "hubot", active: true, valid: true, scopes: ["repo"] });
return this.snapshot();
case "_rustcode/connections/github/cancel":
if (this.approval) clearTimeout(this.approval);
this.pending = null;
this.rejectWait?.(new Error("GitHub sign-in was cancelled."));
return {};
case "_rustcode/connections/github/logout":
this.github = this.github.filter(
(a) => !(a.host === str(params.host) && a.login === str(params.login)),
);
return this.snapshot();
case "_rustcode/connections/account/logout":
this.accounts = this.accounts.filter(
(a) => !(a.provider === str(params.provider) && a.account === str(params.account)),
);
return this.snapshot();
case "_rustcode/connections/account/refresh":
await new Promise((r) => setTimeout(r, 600));
return this.snapshot();
default:
return this.snapshot();
}
}

private rejectWait: ((e: Error) => void) | null = null;
}
1 change: 1 addition & 0 deletions apps/desktop/src/acp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const Method = {
githubCancel: "_rustcode/connections/github/cancel",
githubLogout: "_rustcode/connections/github/logout",
accountLogout: "_rustcode/connections/account/logout",
accountRefresh: "_rustcode/connections/account/refresh",
pulls: "_rustcode/pulls",
/** The timed approvals still in force ("Allow for 1 hour"), and revoking one. */
permissions: "_rustcode/permissions",
Expand Down
125 changes: 125 additions & 0 deletions apps/desktop/src/state/connections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { arr, bool, type Json, obj, str } from "../acp/jsonrpc";

/** A GitHub CLI sign-in on this computer. */
export interface GitHubAccount {
host: string;
login: string;
active: boolean;
valid: boolean;
scopes: string[];
}

/** A saved model account (`credentials.json`), signed in with `/login`. */
export interface ModelAccount {
provider: string;
account: string;
display: string;
method: string;
active: boolean;
needsSignIn: boolean;
}

/** The one-time code `gh auth login --web` printed, and where to enter it. */
export interface DeviceCode {
code: string;
url: string;
}

/** Settings › Connections: the engine's `_rustcode/connections` snapshot. */
export interface Connections {
github?: {
cliInstalled: boolean;
installHint?: string;
accounts: GitHubAccount[];
pending?: DeviceCode;
error?: string;
};
accounts: ModelAccount[];
}

export function parseDeviceCode(v: Json): DeviceCode | undefined {
const o = obj(v);
const code = str(o?.code);
const url = str(o?.url);
return code && url ? { code, url } : undefined;
}

export function parseConnections(v: Json): Connections {
const root = obj(v) ?? {};
const gh = obj(root.github);
return {
github: gh
? {
cliInstalled: bool(gh.cliInstalled) ?? false,
installHint: str(gh.installHint),
accounts: arr(gh.accounts).map((a) => {
const o = obj(a) ?? {};
return {
host: str(o.host) ?? "",
login: str(o.login) ?? "",
active: bool(o.active) ?? false,
valid: bool(o.valid) ?? false,
scopes: arr(o.scopes).flatMap((s) => (typeof s === "string" ? [s] : [])),
};
}),
pending: parseDeviceCode(gh.pending ?? null),
error: str(gh.error),
}
: undefined,
accounts: arr(root.accounts).map((a) => {
const o = obj(a) ?? {};
return {
provider: str(o.provider) ?? "",
account: str(o.account) ?? "",
display: str(o.display) ?? "",
method: str(o.method) ?? "",
active: bool(o.active) ?? false,
needsSignIn: bool(o.needsSignIn) ?? false,
};
}),
};
}

const PROVIDERS: Record<string, { label: string; mark?: string }> = {
openai: { label: "OpenAI", mark: "codex" },
anthropic: { label: "Anthropic", mark: "claude" },
google: { label: "Google", mark: "gemini" },
gemini: { label: "Google", mark: "gemini" },
github_copilot: { label: "GitHub Copilot" },
openrouter: { label: "OpenRouter" },
deepseek: { label: "DeepSeek" },
mistral: { label: "Mistral" },
xai: { label: "xAI" },
};

const METHODS: Record<string, string> = {
api_key: "API key",
chat_gpt: "ChatGPT sign-in",
github_copilot: "Copilot sign-in",
claude_cli: "Claude Code sign-in",
};

/** A provider id as people write it: `openai` is OpenAI. An unknown id keeps its own spelling. */
export function providerLabel(provider: string): string {
return PROVIDERS[provider]?.label ?? provider;
}

/** The harness whose mark stands for a provider (OpenAI is Codex's knot), if one does. */
export function providerMark(provider: string): string | undefined {
return PROVIDERS[provider]?.mark;
}

/** How an account signed in, in words: `chat_gpt` is "ChatGPT sign-in". */
export function methodLabel(method: string): string {
return METHODS[method] ?? method.replace(/_/g, " ");
}

/** The model account's second line: provider and how it signed in. */
export function accountDetail(account: ModelAccount): string {
return [providerLabel(account.provider), methodLabel(account.method)].filter(Boolean).join(" · ");
}

/** A GitHub account's second line: host, and the scopes its token carries when it lists any. */
export function githubDetail(account: GitHubAccount): string {
return account.scopes.length ? `${account.host} · ${account.scopes.join(", ")}` : account.host;
}
Loading
Loading