Open History to see the last commit
1import { type ContextMenuItem, type NativeApi } from "@t3tools/contracts";
2
3import { resetGitStatusStateForTests } from "./lib/gitStatusState";
4import { showContextMenuFallback } from "./contextMenuFallback";
5import { __resetWsRpcAtomClientForTests } from "./rpc/client";
6import { resetRequestLatencyStateForTests } from "./rpc/requestLatencyState";
7import { resetServerStateForTests } from "./rpc/serverState";
8import { resetWsConnectionStateForTests } from "./rpc/wsConnectionState";
9import { __resetWsRpcClientForTests, getWsRpcClient } from "./wsRpcClient";
10
11let instance: { api: NativeApi } | null = null;
12
13export async function __resetWsNativeApiForTests() {
14 instance = null;
15 await __resetWsRpcAtomClientForTests();
16 await __resetWsRpcClientForTests();
17 resetGitStatusStateForTests();
18 resetRequestLatencyStateForTests();
19 resetServerStateForTests();
20 resetWsConnectionStateForTests();
21}
22
23export function createWsNativeApi(): NativeApi {
24 if (instance) {
25 return instance.api;
26 }
27
28 const rpcClient = getWsRpcClient();
29
30 const api: NativeApi = {
31 dialogs: {
32 pickFolder: async () => {
33 if (!window.desktopBridge) return null;
34 return window.desktopBridge.pickFolder();
35 },
36 confirm: async (message) => {
37 if (window.desktopBridge) {
38 return window.desktopBridge.confirm(message);
39 }
40 return window.confirm(message);
41 },
42 },
43 terminal: {
44 open: (input) => rpcClient.terminal.open(input as never),
45 write: (input) => rpcClient.terminal.write(input as never),
46 resize: (input) => rpcClient.terminal.resize(input as never),
47 clear: (input) => rpcClient.terminal.clear(input as never),
48 restart: (input) => rpcClient.terminal.restart(input as never),
49 close: (input) => rpcClient.terminal.close(input as never),
50 onEvent: (callback) => rpcClient.terminal.onEvent(callback),
51 },
52 projects: {
53 searchEntries: rpcClient.projects.searchEntries,
54 writeFile: rpcClient.projects.writeFile,
55 },
56 shell: {
57 openInEditor: (cwd, editor) => rpcClient.shell.openInEditor({ cwd, editor }),
58 openExternal: async (url) => {
59 if (window.desktopBridge) {
60 const opened = await window.desktopBridge.openExternal(url);
61 if (!opened) {
62 throw new Error("Unable to open link.");
63 }
64 return;
65 }
66
67 window.open(url, "_blank", "noopener,noreferrer");
68 },
69 },
70 git: {
71 pull: rpcClient.git.pull,
72 refreshStatus: rpcClient.git.refreshStatus,
73 onStatus: (input, callback, options) => rpcClient.git.onStatus(input, callback, options),
74 listBranches: rpcClient.git.listBranches,
75 createWorktree: rpcClient.git.createWorktree,
76 removeWorktree: rpcClient.git.removeWorktree,
77 createBranch: rpcClient.git.createBranch,
78 checkout: rpcClient.git.checkout,
79 init: rpcClient.git.init,
80 resolvePullRequest: rpcClient.git.resolvePullRequest,
81 preparePullRequestThread: rpcClient.git.preparePullRequestThread,
82 },
83 contextMenu: {
84 show: async <T extends string>(
85 items: readonly ContextMenuItem<T>[],
86 position?: { x: number; y: number },
87 ): Promise<T | null> => {
88 if (window.desktopBridge) {
89 return window.desktopBridge.showContextMenu(items, position) as Promise<T | null>;
90 }
91 return showContextMenuFallback(items, position);
92 },
93 },
94 server: {
95 getConfig: rpcClient.server.getConfig,
96 refreshProviders: rpcClient.server.refreshProviders,
97 upsertKeybinding: rpcClient.server.upsertKeybinding,
98 getSettings: rpcClient.server.getSettings,
99 updateSettings: rpcClient.server.updateSettings,
100 },
101 orchestration: {
102 getSnapshot: rpcClient.orchestration.getSnapshot,
103 dispatchCommand: rpcClient.orchestration.dispatchCommand,
104 getTurnDiff: rpcClient.orchestration.getTurnDiff,
105 getFullThreadDiff: rpcClient.orchestration.getFullThreadDiff,
106 replayEvents: (fromSequenceExclusive) =>
107 rpcClient.orchestration
108 .replayEvents({ fromSequenceExclusive })
109 .then((events) => [...events]),
110 onDomainEvent: (callback, options) =>
111 rpcClient.orchestration.onDomainEvent(callback, options),
112 },
113 };
114
115 instance = { api };
116 return api;
117}