Skip to content

Commit f107cf0

Browse files
committed
fix(clients): keep uncached thread links navigable
1 parent 567a105 commit f107cf0

7 files changed

Lines changed: 87 additions & 35 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
2+
import { AtomRegistry } from "effect/reactivity";
3+
import { expect, it, vi } from "vite-plus/test";
4+
5+
vi.mock("./threads", async () => {
6+
const { Atom } = await import("effect/reactivity");
7+
return {
8+
environmentThreadDetails: {},
9+
environmentThreadShells: {
10+
threadShellAtom: () => Atom.make(null),
11+
threadRefsAtom: Atom.make([]),
12+
},
13+
};
14+
});
15+
vi.mock("./projects", () => ({ environmentProjects: {} }));
16+
vi.mock("./server", () => ({ environmentServerConfigsAtom: {}, serverEnvironment: {} }));
17+
vi.mock("./assets", () => ({ projectFaviconUrlAtom: () => null }));
18+
vi.mock("../native/ProjectChipIconImages", () => ({
19+
projectGlyphImageAtom: () => null,
20+
projectGlyphKey: () => "",
21+
}));
22+
23+
import { resolveThreadLink } from "./entities";
24+
25+
it("opens an explicit thread destination even when the shell omits it", () => {
26+
const registry = AtomRegistry.make();
27+
const destination: ScopedThreadRef = {
28+
environmentId: EnvironmentId.make("archbook"),
29+
threadId: ThreadId.make("thread:delegated-task:mcp%3A1"),
30+
};
31+
try {
32+
expect(
33+
resolveThreadLink(
34+
registry.get.bind(registry),
35+
destination,
36+
EnvironmentId.make("another-environment"),
37+
),
38+
).toEqual(destination);
39+
} finally {
40+
registry.dispose();
41+
}
42+
});

‎apps/mobile/src/state/entities.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export function resolveThreadLink(
228228
if (known) return known;
229229
return link.environmentId === null
230230
? guessThreadLinkRef(link, read(environmentThreadShells.threadRefsAtom))
231-
: null;
231+
: scopeThreadRef(link.environmentId, link.threadId);
232232
}
233233

234234
export function useThreadChipIcons(

‎apps/web/src/components/ThreadContextChip.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ export function ThreadContextChip(props: {
4141
<TooltipPopup side="top">
4242
{props.unreadable
4343
? "On another machine, so this machine's agent can't read it. Remove it or switch back to send."
44-
: shell
45-
? "Open thread"
46-
: "Thread no longer available"}
44+
: "Open thread"}
4745
</TooltipPopup>
4846
</Tooltip>
4947
);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { expect, it, vi } from "vite-plus/test";
3+
4+
import { render } from "../../../test/render";
5+
6+
vi.mock("../../state/entities", () => ({
7+
useThreadShell: () => null,
8+
useThreadRefs: () => [],
9+
useProject: () => null,
10+
}));
11+
vi.mock("../ProjectFavicon", () => ({ ProjectFavicon: () => null }));
12+
13+
import { MarkdownThreadLink } from "./MarkdownThreadLink";
14+
15+
it("opens a scoped thread missing from the shell instead of disabling its link", async () => {
16+
const environmentId = EnvironmentId.make("archbook");
17+
const threadId = ThreadId.make("omitted-child");
18+
await using screen = await render(
19+
<MarkdownThreadLink
20+
environmentId={EnvironmentId.make("another-environment")}
21+
link={{ environmentId, threadId }}
22+
label={null}
23+
/>,
24+
);
25+
const link = screen.getByRole("link", { name: "Thread, Thread", exact: true });
26+
await link.click();
27+
await vi.waitFor(() => {
28+
expect(screen.router?.latestLocation.pathname).toBe(`/${environmentId}/${threadId}`);
29+
});
30+
});

‎apps/web/src/components/chat/MarkdownThreadLink.tsx‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import {
55
guessThreadLinkRef,
66
percentDecodedThreadLinkId,
77
type ThreadLink,
8-
unknownThreadLinkLabel,
98
} from "@t3tools/shared/threadLinks";
109

1110
import { configuredHostedAppUrl } from "../../hostedPairing";
1211
import { useThreadRefs, useThreadShell } from "../../state/entities";
13-
import { UnresolvedChip } from "../contextChipParts";
1412
import { ThreadContextChip } from "../ThreadContextChip";
1513

1614
/** `label` is the link's written text; an autolink has none. */
@@ -30,12 +28,11 @@ export function MarkdownThreadLink(props: {
3028
const refs = useThreadRefs(guess);
3129
const remote = useThreadShell(guess ? guessThreadLinkRef(props.link, refs) : null);
3230
const thread = local ?? remote;
33-
if (thread === null) {
34-
const unknown = unknownThreadLinkLabel(props.link, props.environmentId);
35-
return <UnresolvedChip label={props.label ?? unknown} tooltip={unknown} />;
36-
}
37-
const ref = { environmentId: thread.environmentId, threadId: thread.id };
38-
const title = thread.title.trim() || props.label || "Untitled thread";
31+
const ref = scopeThreadRef(
32+
thread?.environmentId ?? environmentId,
33+
thread?.id ?? props.link.threadId,
34+
);
35+
const title = thread?.title.trim() || props.label?.trim() || "Thread";
3936
return (
4037
<ThreadContextChip
4138
record={{ ...ref, title }}

‎packages/shared/src/threadLinks.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ describe("thread links", () => {
104104
).toBe("[There](t3-thread://v2/there/t1) and [Here](t3-thread://v2/local/t1)");
105105
});
106106

107-
it("turns a v2 link it cannot resolve into plain text, never the raw link", () => {
107+
it("keeps links navigable when their thread is missing from the shell", () => {
108108
expect(
109109
relabelThreadLinks(
110-
"<t3-thread://v2/there/t1>, <t3-thread://v2/local/t1>, [Old title](t3-thread://v2/there/t1)",
110+
"<t3-thread://v2/there/t1>, <t3-thread://v2/local/t1>, [Old title](t3-thread://v2/there/t1), <t3-thread://v1/t1>",
111111
local,
112112
() => undefined,
113113
),
114-
).toBe("Thread on another machine, Thread no longer available, Old title");
114+
).toBe(
115+
"[Thread](t3-thread://v2/there/t1), [Thread](t3-thread://v2/local/t1), [Old title](t3-thread://v2/there/t1), [Thread](t3-thread://v1/t1)",
116+
);
115117
expect(relabelThreadLinks("[Old](t3-thread://v1/t1)", local, () => undefined)).toBe(
116118
"[Old](t3-thread://v1/t1)",
117119
);
@@ -231,7 +233,7 @@ describe("hosted thread links", () => {
231233
);
232234
expect(
233235
relabelThreadLinks("https://app.t3.codes/threads/there/t1", local, () => undefined),
234-
).toBe("Thread on another machine");
236+
).toBe("[Thread](t3-thread://v2/there/t1)");
235237
expect(threadLinkRefs("Open https://app.t3.codes/threads/there/t%3A1", local)).toEqual([
236238
ref("there", "t:1"),
237239
]);

‎packages/shared/src/threadLinks.ts‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,6 @@ export function threadLinkRefs(
162162
return [...refs.values()];
163163
}
164164

165-
export function unknownThreadLinkLabel(
166-
link: ThreadLink,
167-
messageEnvironmentId: EnvironmentId,
168-
): string {
169-
return link.environmentId === null || link.environmentId === messageEnvironmentId
170-
? "Thread no longer available"
171-
: "Thread on another machine";
172-
}
173-
174165
/** Rewrites each link outside code `replace` answers for. `label` is a Markdown link's own text. */
175166
export function replaceThreadLinks(
176167
markdown: string,
@@ -185,11 +176,7 @@ export function replaceThreadLinks(
185176
});
186177
}
187178

188-
/**
189-
* Writes each link with its thread's current title, a hosted link as a v2 link. A link `title`
190-
* cannot resolve becomes plain text, its written label or a neutral one, except a labeled v1
191-
* link, which may open elsewhere.
192-
*/
179+
/** Shells can omit threads, so unknown titles keep their destinations and use a neutral label. */
193180
export function relabelThreadLinks(
194181
markdown: string,
195182
messageEnvironmentId: EnvironmentId,
@@ -205,13 +192,9 @@ export function relabelThreadLinks(
205192
? decoded
206193
: written.threadId;
207194
const current = title({ environmentId, threadId });
208-
if (current === undefined) {
209-
if (label !== undefined && written.environmentId === null) return undefined;
210-
return label?.trim() || unknownThreadLinkLabel(written, messageEnvironmentId);
211-
}
212195
return formatThreadLink(
213196
{ environmentId: written.environmentId, threadId },
214-
current.trim() || label || threadId,
197+
current?.trim() || label?.trim() || "Thread",
215198
);
216199
});
217200
}

0 commit comments

Comments
 (0)