Skip to content

Commit 8b55a25

Browse files
authored
Merge branch 'main' into feat/claude-prompt-ghost-text
2 parents 8731aea + c77a7b7 commit 8b55a25

357 files changed

Lines changed: 12582 additions & 1649 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ Install the latest version of the desktop app from [GitHub Releases](https://git
4848
winget install T3Tools.T3Code
4949
```
5050

51+
#### Windows (`scoop`)
52+
53+
```bash
54+
scoop install extras/t3code
55+
```
56+
5157
#### macOS (Homebrew)
5258

5359
```bash

‎apps/desktop/src/app/DesktopClerk.test.ts‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ import * as ElectronApp from "../electron/ElectronApp.ts";
3535
import * as ElectronShell from "../electron/ElectronShell.ts";
3636
import * as ElectronWindow from "../electron/ElectronWindow.ts";
3737
import * as DesktopClerk from "./DesktopClerk.ts";
38+
import * as DesktopWebLinks from "./DesktopWebLinks.ts";
3839
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
3940
import * as DesktopPreReadyFileSystem from "./DesktopPreReadyFileSystem.ts";
4041

42+
/** Clerk forwards web links; tests that are not about links ignore them. */
43+
const ignoreWebLinks = DesktopWebLinks.DesktopWebLinks.of({
44+
receive: () => Effect.void,
45+
setRendererReady: () => Effect.void,
46+
});
47+
4148
const layerDesktopClerk = (
4249
isDevelopment = true,
4350
events: string[] = [],
@@ -220,11 +227,12 @@ describe("DesktopClerk", () => {
220227

221228
assert.isTrue(Exit.isSuccess(exit));
222229
assert.equal(quit.mock.calls.length, 0);
223-
assert.deepEqual(registeredEvents, ["open-url", "second-instance"]);
230+
assert.deepEqual(registeredEvents, ["open-url", "open-file", "second-instance"]);
224231
}).pipe(
225232
Effect.provide(layerDesktopClerk()),
226233
Effect.provideService(ElectronApp.ElectronApp, electronApp),
227234
Effect.provideService(ElectronWindow.ElectronWindow, electronWindow),
235+
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
228236
);
229237
});
230238

@@ -253,6 +261,7 @@ describe("DesktopClerk", () => {
253261
Effect.provide(layerDesktopClerk()),
254262
Effect.provideService(ElectronApp.ElectronApp, electronApp),
255263
Effect.provideService(ElectronWindow.ElectronWindow, electronWindow),
264+
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
256265
);
257266
});
258267
});
@@ -299,6 +308,7 @@ it.effect(
299308
Effect.provide(layerDesktopClerk()),
300309
Effect.provideService(ElectronApp.ElectronApp, electronApp),
301310
Effect.provideService(ElectronWindow.ElectronWindow, electronWindow),
311+
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
302312
);
303313
},
304314
);
@@ -381,6 +391,54 @@ it.effect.each(["startup", "open-url"] as const)(
381391
ElectronWindow.ElectronWindow,
382392
{} as ElectronWindow.ElectronWindow["Service"],
383393
),
394+
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
384395
);
385396
}).pipe(Effect.scoped),
386397
);
398+
399+
it.effect("hands a web link to the renderer and leaves other links alone", () =>
400+
Effect.gen(function* () {
401+
storageMock.mockReturnValue(storageAdapter);
402+
createClerkBridgeMock.mockReturnValue({ cleanup: vi.fn(), isPrimaryInstance: true });
403+
const listeners = new Map<string, (...args: unknown[]) => void>();
404+
const electronApp = {
405+
whenReady: Effect.void,
406+
on: (name: string, listener: (...args: unknown[]) => void) =>
407+
Effect.sync(() => {
408+
listeners.set(name, listener);
409+
}),
410+
} as unknown as ElectronApp.ElectronApp["Service"];
411+
const received: Array<string> = [];
412+
yield* Effect.gen(function* () {
413+
const clerk = yield* DesktopClerk.DesktopClerk;
414+
yield* clerk.configure;
415+
const open = (url: string) => {
416+
const event = { preventDefault: vi.fn() };
417+
listeners.get("open-url")!(event, url);
418+
return event.preventDefault.mock.calls.length;
419+
};
420+
// macOS hands the default browser every web link.
421+
assert.strictEqual(open("https://example.com/page"), 1);
422+
assert.strictEqual(open("http://localhost:3000/"), 1);
423+
// Anything else is not a web page; Electron keeps its own handling.
424+
assert.strictEqual(open("mailto:[email protected]"), 0);
425+
yield* Effect.yieldNow;
426+
assert.deepStrictEqual(received, ["https://example.com/page", "http://localhost:3000/"]);
427+
}).pipe(
428+
Effect.provide(layerDesktopClerk(true, [], "darwin")),
429+
Effect.provideService(HostProcess.Arguments, ["t3"]),
430+
Effect.provideService(ElectronApp.ElectronApp, electronApp),
431+
Effect.provideService(
432+
ElectronWindow.ElectronWindow,
433+
{} as ElectronWindow.ElectronWindow["Service"],
434+
),
435+
Effect.provideService(
436+
DesktopWebLinks.DesktopWebLinks,
437+
DesktopWebLinks.DesktopWebLinks.of({
438+
receive: (url) => Effect.sync(() => void received.push(url)),
439+
setRendererReady: () => Effect.void,
440+
}),
441+
),
442+
);
443+
}).pipe(Effect.scoped),
444+
);

‎apps/desktop/src/app/DesktopClerk.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createClerkBridge } from "@clerk/electron";
2+
import * as NodeURL from "node:url";
23
import { storage } from "@clerk/electron/storage";
34
import * as Context from "effect/Context";
45
import * as Effect from "effect/Effect";
@@ -18,6 +19,7 @@ import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
1819
import * as ElectronWindow from "../electron/ElectronWindow.ts";
1920
import * as DesktopUserData from "./DesktopUserData.ts";
2021
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
22+
import * as DesktopWebLinks from "./DesktopWebLinks.ts";
2123

2224
declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
2325

@@ -53,7 +55,10 @@ export class DesktopClerk extends Context.Service<
5355
readonly configure: Effect.Effect<
5456
void,
5557
never,
56-
ElectronApp.ElectronApp | ElectronWindow.ElectronWindow | Scope.Scope
58+
| ElectronApp.ElectronApp
59+
| ElectronWindow.ElectronWindow
60+
| DesktopWebLinks.DesktopWebLinks
61+
| Scope.Scope
5762
>;
5863
}
5964
>()("@t3tools/desktop/app/DesktopClerk") {}
@@ -125,6 +130,7 @@ export const make = Effect.gen(function* () {
125130
configure: Effect.gen(function* () {
126131
const electronApp = yield* ElectronApp.ElectronApp;
127132
const electronWindow = yield* ElectronWindow.ElectronWindow;
133+
const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
128134
const context = yield* Effect.context<ElectronWindow.ElectronWindow>();
129135
const runPromise = Effect.runPromiseWith(context);
130136

@@ -184,8 +190,21 @@ export const make = Effect.gen(function* () {
184190
};
185191
const args = yield* HostProcess.Arguments;
186192
args.some((value) => startProviderAuthHandoff(value));
193+
// As the default browser, macOS hands T3 Code every web link through the same event.
194+
const openWebLink = (url: string) => {
195+
if (!DesktopWebLinks.isWebLink(url)) return false;
196+
void runPromise(webLinks.receive(url));
197+
return true;
198+
};
187199
yield* electronApp.on("open-url", (event: { preventDefault: () => void }, url: string) => {
188-
if (startProviderAuthHandoff(url) || resumeProviderAuth(url)) event.preventDefault();
200+
if (startProviderAuthHandoff(url) || resumeProviderAuth(url) || openWebLink(url))
201+
event.preventDefault();
202+
});
203+
// A browser opens HTML files too, which macOS hands over by path.
204+
yield* electronApp.on("open-file", (event: { preventDefault: () => void }, path: string) => {
205+
if (!DesktopWebLinks.isWebPageFile(path)) return;
206+
event.preventDefault();
207+
void runPromise(webLinks.receive(NodeURL.pathToFileURL(path).href));
189208
});
190209
yield* electronApp.on("second-instance", (_event: unknown, argv: readonly string[]) => {
191210
if (argv?.some((value) => startProviderAuthHandoff(value) || resumeProviderAuth(value)))
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Stands in for an Electron webContents.
2+
import { assert, describe, it } from "@effect/vitest";
3+
import * as NodeEvents from "node:events";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
6+
import * as Option from "effect/Option";
7+
8+
import * as ElectronWindow from "../electron/ElectronWindow.ts";
9+
import { WEB_LINK_OPEN_CHANNEL } from "../ipc/channels.ts";
10+
import * as DesktopWebLinks from "./DesktopWebLinks.ts";
11+
12+
const makeWindow = () => {
13+
const sent: Array<string> = [];
14+
const revealed: Array<unknown> = [];
15+
const webContents = Object.assign(new NodeEvents.EventEmitter(), {
16+
isDestroyed: () => false,
17+
send: (channel: string, url: string) => {
18+
if (channel === WEB_LINK_OPEN_CHANNEL) sent.push(url);
19+
},
20+
});
21+
const window = { webContents } as unknown as Electron.BrowserWindow;
22+
/** The web app starts loading a new page, as a reload does. */
23+
const reload = () =>
24+
webContents.emit("did-start-navigation", { isMainFrame: true, isSameDocument: false });
25+
const layer = DesktopWebLinks.layer.pipe(
26+
Layer.provide(
27+
Layer.succeed(
28+
ElectronWindow.ElectronWindow,
29+
ElectronWindow.ElectronWindow.of({
30+
currentMainOrFirst: Effect.succeed(Option.some(window)),
31+
reveal: (target: Electron.BrowserWindow) => Effect.sync(() => void revealed.push(target)),
32+
} as unknown as ElectronWindow.ElectronWindow["Service"]),
33+
),
34+
),
35+
);
36+
return { sent, revealed, layer, reload };
37+
};
38+
39+
describe("DesktopWebLinks", () => {
40+
it("treats only http and https as web links", () => {
41+
assert.isTrue(DesktopWebLinks.isWebLink("https://example.com/"));
42+
assert.isTrue(DesktopWebLinks.isWebLink("http://localhost:3000/a?b=1"));
43+
assert.isFalse(DesktopWebLinks.isWebLink("t3code://app/welcome"));
44+
assert.isFalse(DesktopWebLinks.isWebLink("mailto:[email protected]"));
45+
assert.isFalse(DesktopWebLinks.isWebLink("not a url"));
46+
});
47+
48+
it("opens HTML files the way a browser does, and nothing else by path", () => {
49+
assert.isTrue(DesktopWebLinks.isWebPageFile("/Users/me/report.html"));
50+
assert.isTrue(DesktopWebLinks.isWebPageFile("/tmp/page.HTM"));
51+
assert.isTrue(DesktopWebLinks.isWebPageFile("/tmp/page.xhtml"));
52+
assert.isFalse(DesktopWebLinks.isWebPageFile("/tmp/notes.md"));
53+
assert.isFalse(DesktopWebLinks.isWebPageFile("/tmp/html"));
54+
});
55+
56+
it.effect("holds links until the renderer listens, then delivers them in order", () => {
57+
const { sent, revealed, layer } = makeWindow();
58+
return Effect.gen(function* () {
59+
const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
60+
// The link that launched the app arrives before the web app has loaded.
61+
yield* webLinks.receive("https://example.com/first");
62+
yield* webLinks.receive("https://example.com/second");
63+
assert.deepStrictEqual(sent, []);
64+
65+
yield* webLinks.setRendererReady(true);
66+
assert.deepStrictEqual(sent, ["https://example.com/first", "https://example.com/second"]);
67+
assert.strictEqual(revealed.length, 1);
68+
69+
yield* webLinks.receive("https://example.com/third");
70+
assert.deepStrictEqual(sent.at(-1), "https://example.com/third");
71+
72+
// A reloading web app stops listening; links wait for it again.
73+
yield* webLinks.setRendererReady(false);
74+
yield* webLinks.receive("https://example.com/fourth");
75+
assert.strictEqual(sent.length, 3);
76+
}).pipe(Effect.provide(layer));
77+
});
78+
79+
it.effect("holds links that arrive while the web app reloads until it listens again", () => {
80+
const { sent, layer, reload } = makeWindow();
81+
return Effect.gen(function* () {
82+
const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
83+
yield* webLinks.setRendererReady(true);
84+
// The old page never says it stopped; the reload itself ends its listening.
85+
reload();
86+
yield* webLinks.receive("https://example.com/during-reload");
87+
assert.deepStrictEqual(sent, []);
88+
89+
yield* webLinks.setRendererReady(true);
90+
assert.deepStrictEqual(sent, ["https://example.com/during-reload"]);
91+
}).pipe(Effect.provide(layer));
92+
});
93+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import * as Context from "effect/Context";
2+
import * as Effect from "effect/Effect";
3+
import * as Layer from "effect/Layer";
4+
import * as Option from "effect/Option";
5+
6+
import * as ElectronWindow from "../electron/ElectronWindow.ts";
7+
import { WEB_LINK_OPEN_CHANNEL } from "../ipc/channels.ts";
8+
9+
/** An http(s) link the operating system handed to T3 Code as the default browser. */
10+
export const isWebLink = (value: string) => {
11+
if (!URL.canParse(value)) return false;
12+
const { protocol } = new URL(value);
13+
return protocol === "http:" || protocol === "https:";
14+
};
15+
16+
/** An HTML file the operating system asked T3 Code to open, as browsers are asked to. */
17+
export const isWebPageFile = (path: string) => /\.x?html?$/i.test(path);
18+
19+
/**
20+
* Web links and HTML files macOS opens with T3 Code once it is the default
21+
* browser. Files arrive as file:// URLs. They can
22+
* arrive before the app is ready (the link that launched it) or while the web
23+
* app reloads, so they wait until the renderer says it is listening.
24+
*/
25+
export class DesktopWebLinks extends Context.Service<
26+
DesktopWebLinks,
27+
{
28+
/** Queues a link and delivers it as soon as the renderer is listening. */
29+
readonly receive: (url: string) => Effect.Effect<void>;
30+
/** The renderer started or stopped listening; listening flushes queued links. */
31+
readonly setRendererReady: (ready: boolean) => Effect.Effect<void>;
32+
}
33+
>()("@t3tools/desktop/app/DesktopWebLinks") {}
34+
35+
const make = Effect.gen(function* () {
36+
const electronWindow = yield* ElectronWindow.ElectronWindow;
37+
const pending: Array<string> = [];
38+
/** The page that said it is listening; a reload or crash replaces it, and it must say so again. */
39+
let listening: Electron.WebContents | null = null;
40+
let detachListening: (() => void) | null = null;
41+
42+
const stopListening = () => {
43+
detachListening?.();
44+
detachListening = null;
45+
listening = null;
46+
};
47+
48+
const flush = Effect.gen(function* () {
49+
if (listening === null || pending.length === 0) return;
50+
const window = yield* electronWindow.currentMainOrFirst;
51+
if (Option.isNone(window) || window.value.webContents !== listening) return;
52+
if (listening.isDestroyed()) {
53+
stopListening();
54+
return;
55+
}
56+
for (const url of pending.splice(0)) listening.send(WEB_LINK_OPEN_CHANNEL, url);
57+
yield* electronWindow.reveal(window.value);
58+
});
59+
60+
const startListening = Effect.gen(function* () {
61+
const window = yield* electronWindow.currentMainOrFirst;
62+
if (Option.isNone(window) || window.value.webContents.isDestroyed()) return;
63+
const webContents = window.value.webContents;
64+
if (listening === webContents) return;
65+
stopListening();
66+
listening = webContents;
67+
// Links that arrive while a new page loads wait for it to listen again.
68+
const onNavigation = (
69+
event: Electron.Event<Electron.WebContentsDidStartNavigationEventParams>,
70+
) => {
71+
if (event.isMainFrame && !event.isSameDocument) stopListening();
72+
};
73+
webContents.on("did-start-navigation", onNavigation);
74+
webContents.on("render-process-gone", stopListening);
75+
webContents.once("destroyed", stopListening);
76+
detachListening = () => {
77+
webContents.removeListener("did-start-navigation", onNavigation);
78+
webContents.removeListener("render-process-gone", stopListening);
79+
webContents.removeListener("destroyed", stopListening);
80+
};
81+
});
82+
83+
return DesktopWebLinks.of({
84+
receive: (url) =>
85+
Effect.suspend(() => {
86+
pending.push(url);
87+
return flush;
88+
}).pipe(Effect.withSpan("DesktopWebLinks.receive")),
89+
setRendererReady: (ready) =>
90+
(ready ? startListening : Effect.sync(stopListening)).pipe(
91+
Effect.andThen(flush),
92+
Effect.withSpan("DesktopWebLinks.setRendererReady"),
93+
),
94+
});
95+
});
96+
97+
export const layer = Layer.effect(DesktopWebLinks, make);

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,21 @@ describe("DesktopBackendConfiguration", () => {
325325
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
326326
);
327327

328+
it.effect("only the local primary bootstrap claims the prepared shell environment", () =>
329+
withHarness(
330+
Effect.gen(function* () {
331+
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
332+
333+
const primary = yield* configuration.resolvePrimary;
334+
const wsl = yield* configuration.resolveWsl({ port: 5000, distro: null });
335+
336+
assert.equal(primary.bootstrap.shellEnvironmentPrepared, true);
337+
// A WSL server runs on its own Linux user environment and must hydrate it.
338+
assert.isUndefined(wsl.bootstrap.shellEnvironmentPrepared);
339+
}),
340+
),
341+
);
342+
328343
it.effect("resolveWsl reuses the primary's bootstrap token", () =>
329344
withHarness(
330345
Effect.gen(function* () {

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ const resolvePrimaryStartConfig = Effect.fn("desktop.backendConfiguration.resolv
567567
host: backendExposure.bindHost,
568568
desktopBootstrapToken: input.bootstrapToken,
569569
desktopBootstrapSecret: input.bootstrapSecret,
570+
// DesktopApp installs the shell environment into process.env before any
571+
// backend starts, and this child extends it (extendEnv below). WSL gets
572+
// a different environment, so its bootstrap never sets this.
573+
shellEnvironmentPrepared: true,
570574
tailscaleServeEnabled: backendExposure.tailscaleServeEnabled,
571575
tailscaleServePort: backendExposure.tailscaleServePort,
572576
desktopTelemetryFd: 4,

0 commit comments

Comments
 (0)