TypeScript
fix(chat): open thread links across connected environments (#21505)
Julius Marminge05d32e55d327
diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsxindex 4f8ad6f586a..ff5bf3147e0 100644--- a/apps/mobile/src/features/threads/ThreadFeed.tsx+++ b/apps/mobile/src/features/threads/ThreadFeed.tsx@@ -34,7 +34,7 @@ import {} from "@t3tools/contracts";import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";-import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";+import { parseThreadLinkHref, resolveThreadLinkRef } from "@t3tools/shared/threadLinks";import { PROMPT_LINK_MAX_PROMPT_LENGTH } from "@t3tools/shared/promptLinks";import {parseComposerContextHref,@@ -1788,9 +1788,13 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {const onMarkdownLinkPress = (href: string, imageEmbed = false) => {const linkedThreadId = parseThreadLinkHref(href);if (linkedThreadId) {+ const source = scopeThreadRef(props.environmentId, linkedThreadId);+ const target =+ resolveThreadLinkRef(source, appAtomRegistry.get(environmentThreadShells.threadRefsAtom)) ??+ source;navigation.navigate("Thread", {- environmentId: String(props.environmentId),- threadId: String(linkedThreadId),+ environmentId: String(target.environmentId),+ threadId: String(target.threadId),});return;}diff --git a/apps/web/src/components/chat/MarkdownThreadLink.tsx b/apps/web/src/components/chat/MarkdownThreadLink.tsxindex 6979e868db1..4996be3884a 100644--- a/apps/web/src/components/chat/MarkdownThreadLink.tsx+++ b/apps/web/src/components/chat/MarkdownThreadLink.tsx@@ -1,10 +1,14 @@import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";import type { EnvironmentId, ThreadId } from "@t3tools/contracts";-import { formatThreadLink, percentDecodedThreadLinkId } from "@t3tools/shared/threadLinks";+import {+ formatThreadLink,+ percentDecodedThreadLinkId,+ resolveThreadLinkRef,+} from "@t3tools/shared/threadLinks";import { Link } from "@tanstack/react-router";import { MessageSquareTextIcon } from "lucide-react";-import { useProject, useThreadShell } from "../../state/entities";+import { useProject, useThreadRefs, useThreadShell } from "../../state/entities";import { ProjectFavicon } from "../ProjectFavicon";export function MarkdownThreadLink(props: {@@ -17,16 +21,24 @@ export function MarkdownThreadLink(props: {const decoded = useThreadShell(written === null && decodedId !== null ? scopeThreadRef(props.environmentId, decodedId) : null,);- const thread = written ?? decoded;+ const local = written ?? decoded;+ const refs = useThreadRefs(local === null);+ const remote = useThreadShell(+ local === null+ ? resolveThreadLinkRef(scopeThreadRef(props.environmentId, props.threadId), refs)+ : null,+ );+ const thread = local ?? remote;+ const environmentId = thread?.environmentId ?? props.environmentId;const threadId = thread?.id ?? props.threadId;const project = useProject(- thread === null ? null : scopeProjectRef(props.environmentId, thread.projectId),+ thread === null ? null : scopeProjectRef(environmentId, thread.projectId),);const title = thread?.title.trim() || props.label;return (<Linkto="/$environmentId/$threadId"- params={{ environmentId: props.environmentId, threadId }}+ params={{ environmentId, threadId }}title={thread === null ? "Thread no longer available" : project?.title}data-markdown-copy={formatThreadLink(threadId, title)}>diff --git a/apps/web/src/state/entities.ts b/apps/web/src/state/entities.tsindex b456752cab0..91fb46503b9 100644--- a/apps/web/src/state/entities.ts+++ b/apps/web/src/state/entities.ts@@ -79,8 +79,8 @@ export function setActiveEnvironmentId(environmentId: EnvironmentId | null): voiappAtomRegistry.set(activeEnvironmentIdAtom, environmentId);}-export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {- return useAtomValue(environmentThreadShells.threadRefsAtom);+export function useThreadRefs(enabled = true): ReadonlyArray<ScopedThreadRef> {+ return useAtomValue(enabled ? environmentThreadShells.threadRefsAtom : EMPTY_THREAD_REFS_ATOM);}export function useEnvironmentThreadRefs(diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.mdindex 9b9412af654..93f2f81d6bd 100644--- a/docs/orchestration-v2/orchestrator-mcp-server.md+++ b/docs/orchestration-v2/orchestrator-mcp-server.md@@ -375,9 +375,10 @@ Actor and ingress are separate so agent-authored user-role messages remaindistinguishable from human-authored messages.Agents mention another thread as `[title](t3-thread://v1/<threadId>)`. The-link carries only the id, which resolves in the environment of the message that-holds it. Clients show the thread's current title rather than the label, so a-rename never leaves a stale link.+link carries only the id. Clients first resolve it in the message's environment,+then use a unique match in another enabled environment. Ambiguous matches stay+unresolved. Web and desktop show the resolved thread’s current title. Mobile+relabels local matches; cross-environment links can retain the written label.List and read results report `snoozed` and `snoozedUntil`, and`t3_thread_list` filters on `snoozed`. The server's `isSnoozed` follows thediff --git a/packages/shared/src/threadLinks.test.ts b/packages/shared/src/threadLinks.test.tsindex e6855ea9cf2..f65d1c0599b 100644--- a/packages/shared/src/threadLinks.test.ts+++ b/packages/shared/src/threadLinks.test.ts@@ -1,6 +1,55 @@import { describe, expect, it } from "vite-plus/test";+import { EnvironmentId, ThreadId } from "@t3tools/contracts";-import { formatThreadLink, parseThreadLinkHref, relabelThreadLinks } from "./threadLinks.ts";+import {+ formatThreadLink,+ parseThreadLinkHref,+ relabelThreadLinks,+ resolveThreadLinkRef,+} from "./threadLinks.ts";++const ref = (environmentId: string, threadId = "chat-1") => ({+ environmentId: EnvironmentId.make(environmentId),+ threadId: ThreadId.make(threadId),+});++describe("thread link destinations", () => {+ it("prefers the local literal id, then the local decoded id, over remote matches", () => {+ const source = ref("local", "provider%3A1");+ const decoded = ref("local", "provider:1");+ const remote = ref("remote", "provider%3A1");+ expect(resolveThreadLinkRef(source, [remote, decoded, source])).toEqual(source);+ expect(resolveThreadLinkRef(source, [remote, decoded])).toEqual(decoded);+ });++ it("opens the unique matching chat in another environment", () => {+ const destination = ref("remote");+ expect(resolveThreadLinkRef(ref("local"), [ref("local", "other-chat"), destination])).toEqual(+ destination,+ );+ });++ it("preserves a remote literal id and decodes only when that id is absent", () => {+ const source = ref("local", "provider%3A1");+ const literal = ref("remote", "provider%3A1");+ const decoded = ref("other-remote", "provider:1");+ expect(resolveThreadLinkRef(source, [decoded, literal])).toEqual(literal);+ expect(resolveThreadLinkRef(source, [decoded])).toEqual(decoded);+ });++ it("leaves ambiguous or missing destinations unresolved", () => {+ const source = ref("local", "provider%3A1");+ const literal = ref("remote", "provider%3A1");+ const decoded = ref("remote", "provider:1");+ expect(+ resolveThreadLinkRef(source, [literal, ref("other-remote", literal.threadId), decoded]),+ ).toBeNull();+ expect(+ resolveThreadLinkRef(source, [decoded, ref("other-remote", decoded.threadId)]),+ ).toBeNull();+ expect(resolveThreadLinkRef(source, [ref("remote", "other-chat")])).toBeNull();+ });+});describe("thread links", () => {it("takes the thread id verbatim, percent escapes included", () => {diff --git a/packages/shared/src/threadLinks.ts b/packages/shared/src/threadLinks.tsindex 0b17846db14..20c64e1ec1a 100644--- a/packages/shared/src/threadLinks.ts+++ b/packages/shared/src/threadLinks.ts@@ -1,4 +1,4 @@-import { ThreadId } from "@t3tools/contracts";+import { ThreadId, type ScopedThreadRef } from "@t3tools/contracts";import * as Option from "effect/Option";import * as Schema from "effect/Schema";@@ -25,6 +25,30 @@ export function percentDecodedThreadLinkId(threadId: ThreadId): ThreadId | null}}+export function resolveThreadLinkRef(+ source: ScopedThreadRef,+ refs: ReadonlyArray<ScopedThreadRef>,+): ScopedThreadRef | null {+ const decodedId = percentDecodedThreadLinkId(source.threadId);+ const ids = decodedId === null ? [source.threadId] : [source.threadId, decodedId];+ for (const threadId of ids) {+ const local = refs.find(+ (ref) => ref.environmentId === source.environmentId && ref.threadId === threadId,+ );+ if (local) return local;+ }+ for (const threadId of ids) {+ let match: ScopedThreadRef | null = null;+ for (const ref of refs) {+ if (ref.threadId !== threadId) continue;+ if (match !== null) return null;+ match = ref;+ }+ if (match !== null) return match;+ }+ return null;+}+export function formatThreadLink(threadId: string, label: string): string {const cleaned = label.replace(/[[\]\\\r\n]/g, " ")