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
2 changes: 1 addition & 1 deletion FORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ migrations after upstream migrations.

98. Mobile reads artifacts ([#833](https://github.com/Jacksondr5/j5code/issues/833)), landing two reserved mobile seams. Route registration: `apps/mobile/src/Stack.tsx` imports J5-owned `j5ThreadArtifactRoutes` (`apps/mobile/src/j5/artifacts/routes.ts`) and spreads it after `ThreadFile`, adding the thread-scoped `ThreadArtifacts` list and `ThreadArtifact` viewer (`apps/mobile/src/j5/artifacts/ThreadArtifactsScreen.tsx`), read-only, with no delete. Link resolver: `onMarkdownLinkPress` in `apps/mobile/src/features/threads/ThreadFeed.tsx` makes one call to J5-owned `openArtifactLink` before its thread, media and file branches, so an `artifacts/…` link opens the viewer instead of a missing workspace file; its `fileContextMenu` and `onFileContextMenuAction` each make one call first (`artifactLinkContextMenu`, `onArtifactLinkMenuAction`), so an artifact link's long press offers Open artifact and Copy path instead of workspace-file actions. Links inside a Markdown artifact go through the same helpers in the J5 viewer. Recognition, preview kind and the HTTP read are shared with web in `packages/client-runtime/src/j5/artifacts.ts`. No upstream entry point reaches the list yet; it is one tap from any opened artifact.

99. Mobile's shared conversations show who wrote each message and who answered each prompt, the mobile half of case 80 ([#832](https://github.com/Jacksondr5/j5code/issues/832), register D33, Shared conversations AC9 to AC11, AC14 and AC16). The record and its change feed are `threadAuthors` and `threadAuthorsChanges` in `packages/client-runtime/src/j5/sharing.ts`, and the reading of it (`sharedMessageView`, the typed text, the answers without their mark, the search label) is `packages/client-runtime/src/j5/sharedConversation.ts`, both shared with web. `apps/mobile/src/features/threads/ThreadRouteScreen.tsx` imports `useSharedConversationFeed` from J5-owned `apps/mobile/src/j5/sharing/sharedConversation.tsx` and passes its answer as `ThreadDetailScreen`'s `selectedThreadFeed` in place of `composer.selectedThreadFeed`: an authored message shows as typed, after its note to the agent as a muted line that opens (drawn by case 97's `renderJ5TimelineNotice`), a message whose author is not read yet is held back or shown as this client typed it, and a question's answers lose the platform's `[Answer from …]` mark. Case 97's `useJ5Timeline` also reads the record, so `resolveJ5FeedMessage` puts another person's message on the left and `J5MessageAuthorLine` names them, or says who edited the reader's own. `apps/mobile/src/features/threads/thread-work-log.tsx` mounts `PromptAnsweredBy` after a work row's label (" · answered by Priya" on a settled question or approval). `apps/mobile/src/features/threads/ThreadQueueControl.tsx` reads its rows through `useSharedQueuedRuns`, so a queued message's row and the text an edit loads are the words typed. `apps/mobile/src/features/threads/thread-search-match.tsx` labels a hit with `threadSearchMatchLabel` instead of "You:" for every user-role hit. `apps/mobile/src/lib/filePreview.test.ts` mocks `sharedConversation.tsx` as it mocks case 97's `timelineState.ts`. Proofs: `apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts`, `apps/mobile/src/j5/a2a/timelinePresentation.test.ts`, `packages/client-runtime/src/j5/sharedConversation.test.ts`, `packages/client-runtime/src/j5/threadSearchMatchLabel.test.ts`, and the four files' rows in `apps/mobile/src/j5/upstreamSeams.test.ts`. On every rebase, keep the calls that test lists.
99. Mobile's shared conversations show who wrote each message and who answered each prompt, the mobile half of case 80 ([#832](https://github.com/Jacksondr5/j5code/issues/832), register D33, Shared conversations AC9 to AC11, AC14 and AC16). The record and its change feed are `threadAuthors` and `threadAuthorsChanges` in `packages/client-runtime/src/j5/sharing.ts`, beside who reaches the thread (`scopePeople` and `scopePeopleChanges`), which says the thread is shared before any author is recorded (`mayBeSharedConversation`, [#871](https://github.com/Jacksondr5/j5code/issues/871)); and the reading of it (`sharedMessageView`, the typed text, the answers without their mark, the search label) is `packages/client-runtime/src/j5/sharedConversation.ts`, both shared with web. `apps/mobile/src/features/threads/ThreadRouteScreen.tsx` imports `useSharedConversationFeed` from J5-owned `apps/mobile/src/j5/sharing/sharedConversation.tsx` and passes its answer as `ThreadDetailScreen`'s `selectedThreadFeed` in place of `composer.selectedThreadFeed`: an authored message shows as typed, after its note to the agent as a muted line that opens (drawn by case 97's `renderJ5TimelineNotice`), a message whose author is not read yet is held back or shown as this client typed it, and a question's answers lose the platform's `[Answer from …]` mark. Case 97's `useJ5Timeline` also reads the record, so `resolveJ5FeedMessage` puts another person's message on the left and `J5MessageAuthorLine` names them, or says who edited the reader's own. `apps/mobile/src/features/threads/thread-work-log.tsx` mounts `PromptAnsweredBy` after a work row's label (" · answered by Priya" on a settled question or approval). `apps/mobile/src/features/threads/ThreadQueueControl.tsx` reads its rows through `useSharedQueuedRuns`, so a queued message's row and the text an edit loads are the words typed. `apps/mobile/src/features/threads/thread-search-match.tsx` labels a hit with `threadSearchMatchLabel` instead of "You:" for every user-role hit. `apps/mobile/src/lib/filePreview.test.ts` mocks `sharedConversation.tsx` as it mocks case 97's `timelineState.ts`. Proofs: `apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts`, `apps/mobile/src/j5/a2a/timelinePresentation.test.ts`, `packages/client-runtime/src/j5/sharedConversation.test.ts`, `packages/client-runtime/src/j5/threadSearchMatchLabel.test.ts`, and the four files' rows in `apps/mobile/src/j5/upstreamSeams.test.ts`. On every rebase, keep the calls that test lists.

## Pin and upstream advance runbook

Expand Down
31 changes: 30 additions & 1 deletion apps/mobile/src/j5/sharing/sharedConversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ const NO_RECORD = Atom.make(AsyncResult.initial(false)) as ReturnType<
const NO_CHANGES = Atom.make(AsyncResult.initial(false)) as ReturnType<
typeof j5Sharing.threadAuthorsChanges
>;
const NO_PEOPLE = Atom.make(AsyncResult.initial(false)) as ReturnType<typeof j5Sharing.scopePeople>;
const NO_PEOPLE_CHANGES = Atom.make(AsyncResult.initial(false)) as ReturnType<
typeof j5Sharing.scopePeopleChanges
>;

/**
* Who reaches a thread, read again whenever the server says it changed, so a thread shared while
* it is open is known to be shared before anyone else's first message arrives.
*/
function useThreadPeople(environmentId: EnvironmentId | null, threadId: ThreadId | null) {
const target =
environmentId === null || threadId === null
? null
: { environmentId, input: { scope: { kind: "thread" as const, threadId } } };
const query = target === null ? NO_PEOPLE : j5Sharing.scopePeople(target);
const people = useAtomValue(query);
const changes = useAtomValue(
target === null ? NO_PEOPLE_CHANGES : j5Sharing.scopePeopleChanges(target),
);
const revision = Option.getOrNull(AsyncResult.value(changes))?.revision;
// oxlint-disable-next-line t3code/react-effect-budget -- rereads who reaches the thread when the server's change counter moves.
useEffect(() => {
if (revision !== undefined && revision > 0) appAtomRegistry.refresh(query);
}, [query, revision]);
return Option.getOrNull(AsyncResult.value(people))?.people ?? null;
}

function useThreadAuthorsResult(environmentId: EnvironmentId | null, threadId: ThreadId | null) {
const result = useAtomValue(
Expand Down Expand Up @@ -60,7 +86,8 @@ export function useSharedQueuedRuns<

/**
* Shared conversations AC16 for the native timeline (`presentSharedConversationFeed`), from the
* record of who sent each message, read again whenever the server says it changed.
* record of who sent each message and who reaches the thread, each read again whenever the server
* says it changed.
*/
export function useSharedConversationFeed(
environmentId: EnvironmentId | null,
Expand All @@ -71,6 +98,7 @@ export function useSharedConversationFeed(
environmentId === null || threadId === null ? null : { environmentId, input: { threadId } };
const query = target === null ? NO_RECORD : j5Sharing.threadAuthors(target);
const { record, failed, readAt } = useThreadAuthorsResult(environmentId, threadId);
const people = useThreadPeople(environmentId, threadId);
const changes = useAtomValue(
target === null ? NO_CHANGES : j5Sharing.threadAuthorsChanges(target),
);
Expand All @@ -94,6 +122,7 @@ export function useSharedConversationFeed(
failed,
readAt,
seen,
people,
});
// oxlint-disable-next-line t3code/react-effect-budget -- asks once more about a message waiting on its author, in case the change feed is slow.
useEffect(() => {
Expand Down
30 changes: 30 additions & 0 deletions apps/mobile/src/j5/sharing/sharedConversationFeed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe("presentSharedConversationFeed", () => {
failed: false,
readAt: 2,
seen: seenAt(["m-1"], 1),
people: null,
});
expect(texts(feed)).toEqual([
["m-1:shared-note", "[Note for the agent]"],
Expand All @@ -75,11 +76,38 @@ describe("presentSharedConversationFeed", () => {
failed: false,
readAt: null,
seen: seenAt(["m-2"], 1),
people: null,
});
expect(feed).toEqual([]);
expect(pendingKey).toBe("m-2");
});

it("holds a new writer's first message in a thread shared after its record was read", () => {
// The record was read before the thread was shared, so it still says not shared.
const input = {
feed: [message("m-4", `${prefix}First words from Priya.`)],
record: record({ shared: false, authors: [] }),
failed: false,
readAt: 1,
seen: seenAt(["m-4"], 2),
};
const owner = { level: "owner" as const, you: true, firstOwner: true };
const collaborator = { level: "collaborator" as const, you: false, firstOwner: false };
const reaching = (people: ReadonlyArray<typeof owner | typeof collaborator>) =>
people.map((person, index) => ({
...person,
personId: `human:${index}`,
name: null,
via: [],
}));
expect(
presentSharedConversationFeed({ ...input, people: reaching([owner, collaborator]) }),
).toEqual({ feed: [], pendingKey: "m-4" });
expect(presentSharedConversationFeed({ ...input, people: reaching([owner]) }).feed).toEqual(
input.feed,
);
});

it("leaves a thread only one person writes to as stored", () => {
const stored = message("m-3", "Just me.");
const { feed } = presentSharedConversationFeed({
Expand All @@ -88,6 +116,7 @@ describe("presentSharedConversationFeed", () => {
failed: false,
readAt: 2,
seen: seenAt(["m-3"], 1),
people: null,
});
expect(feed).toEqual([stored]);
});
Expand Down Expand Up @@ -125,6 +154,7 @@ describe("presentSharedConversationFeed", () => {
failed: false,
readAt: 2,
seen: NO_SHARED_MESSAGES_SEEN,
people: null,
});
const shown = feed[0];
expect(
Expand Down
8 changes: 5 additions & 3 deletions apps/mobile/src/j5/sharing/sharedConversationFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {
answersWithoutPrefix,
authorIndex,
markSharedNote,
mayBeSharedConversation,
readAnswersFor,
type SeenSharedMessages,
sharedMessageView,
} from "@t3tools/client-runtime/j5/shared-conversation";
import { MessageId } from "@t3tools/contracts";
import type { ThreadAuthors } from "@t3tools/contracts/j5";
import type { ScopePerson, ThreadAuthors } from "@t3tools/contracts/j5";

import type { ThreadFeedEntry } from "../../lib/threadActivity";

Expand All @@ -22,10 +23,11 @@ export function presentSharedConversationFeed(input: {
/** When the latest read came back; null before one has. */
readonly readAt: number | null;
readonly seen: SeenSharedMessages;
/** Who reaches the thread; null until that read answers, or when it fails. */
readonly people: ReadonlyArray<ScopePerson> | null;
}): { readonly feed: ReadonlyArray<ThreadFeedEntry>; readonly pendingKey: string } {
const { record, failed, readAt, seen } = input;
// Until the first read answers or fails, any thread may be shared.
const mayBeShared = record?.shared === true || (record === null && !failed);
const mayBeShared = mayBeSharedConversation({ record, failed, people: input.people });
const { authors, answers } = authorIndex(record);
const pending: string[] = [];
const shown = input.feed.flatMap((entry): ReadonlyArray<ThreadFeedEntry> => {
Expand Down
50 changes: 50 additions & 0 deletions apps/web/src/j5/sharing/sharedTimeline.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { describe, expect, it, vi } from "vite-plus/test";

import { render } from "../../../test/render";
import { useSharedConversationEntries } from "./sharedTimeline";

// The record and the people read are atom values, the same objects until the server answers again.
const authors = {
record: { shared: false, authors: [], answers: [] },
readAt: 1,
failed: false,
refresh: () => {},
};
const people = { data: { people: [] }, error: null };
vi.mock(import("./sharedConversation"), async (original) => ({
...(await original()),
useThreadAuthors: (() => authors) as never,
}));
vi.mock(import("./scopeAccess"), async (original) => ({
...(await original()),
useScopePeople: (() => people) as never,
}));

const threadRef = {
environmentId: EnvironmentId.make("environment-1"),
threadId: ThreadId.make("thread-1"),
};
const entries = [
{
id: "entry-1",
kind: "message",
message: { id: "message-1", role: "user", text: "Hello", createdBy: "user" },
},
];

describe("useSharedConversationEntries", () => {
it("hands the timeline the same entries while nothing it reads has changed", async () => {
const seen: Array<ReadonlyArray<unknown>> = [];
function Probe(props: { readonly tick: number }) {
seen.push(useSharedConversationEntries(threadRef, entries));
return <span>{props.tick}</span>;
}
// The hook reads no route.
await using screen = await render(<Probe tick={1} />, { router: false });
const settled = seen.length;
await screen.rerender(<Probe tick={2} />);
expect(seen.length).toBeGreaterThan(settled);
expect(seen.at(-1)).toBe(seen[settled - 1]);
});
});
19 changes: 9 additions & 10 deletions apps/web/src/j5/sharing/sharedTimeline.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
import type { MessageAuthor, PromptAnswer } from "@t3tools/contracts/j5";
import { levelReaches } from "@t3tools/contracts/j5";
import { useEffect, useState } from "react";

import {
authorIndex,
mayBeSharedConversation,
answersWithoutPrefix,
NO_SHARED_MESSAGES_SEEN,
observeSharedMessages,
Expand Down Expand Up @@ -134,15 +134,6 @@ export function useSharedConversationEntries<Entry extends MessageEntry>(
threadRef === null ? null : { kind: "thread" as const, threadId: threadRef.threadId };
// The header's pill already reads who reaches the thread; this is the same read.
const people = useScopePeople(threadRef?.environmentId ?? null, threadScope);
const reaching = people.data?.people ?? [];
const writers = reaching.filter((person) => levelReaches(person.level, "collaborator")).length;
// To anyone but the first owner, a message no one is recorded for may be the first owner's,
// which the next read says; their own view draws it as theirs.
const readsOthers = reaching.length > 1 && !reaching.some((p) => p.you && p.firstOwner);
// Until the first read answers or fails, any thread may be shared, so nothing is drawn plain
// that may carry lines for the agent.
const mayBeShared =
record?.shared === true || writers > 1 || readsOthers || (record === null && !failed);

const [stored, setStored] = useState(NO_SHARED_MESSAGES_SEEN);
const seen = observeSharedMessages(
Expand All @@ -152,6 +143,14 @@ export function useSharedConversationEntries<Entry extends MessageEntry>(
);
if (seen !== stored) setStored(seen);

// After the state hook: computed before it, the call stops the compiler memoizing the entries,
// and ConversationTimeline re-renders forever on entries that change each render.
const mayBeShared = mayBeSharedConversation({
record,
failed,
people: people.data?.people ?? null,
});

const shown = (() => {
const { authors, answers } = authorIndex(record);
const pending: string[] = [];
Expand Down
12 changes: 2 additions & 10 deletions apps/web/src/j5/sharing/sharingManage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
createEnvironmentRpcCommand,
createEnvironmentRpcQueryAtomFamily,
createEnvironmentRpcSubscriptionAtomFamily,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import type { EnvironmentId } from "@t3tools/contracts";
Expand Down Expand Up @@ -38,15 +37,8 @@ export const sharingManage = {
refreshTrigger: j5Environment.rereadOn("sharing"),
}),
/** Who reaches one thread or project, for anyone who reaches it (AC26). */
scopePeople: createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, {
label: "environment-data:j5-people:scope",
tag: J5_PEOPLE_WS_METHODS.listScopePeople,
staleTimeMs: 0,
}),
scopePeopleChanges: createEnvironmentRpcSubscriptionAtomFamily(connectionAtomRuntime, {
label: "environment-data:j5-people:scope-changes",
tag: J5_PEOPLE_WS_METHODS.subscribeScopePeopleChanges,
}),
scopePeople: j5Sharing.scopePeople,
scopePeopleChanges: j5Sharing.scopePeopleChanges,
/** For a device the server cannot place: everyone it could be (AC5). */
claimablePeople: createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, {
label: "environment-data:j5-access:claimable",
Expand Down
45 changes: 45 additions & 0 deletions packages/client-runtime/src/j5/sharedConversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
recallableMessages,
sharedMessageView,
typedText,
mayBeSharedConversation,
} from "./sharedConversation.ts";

const author = (overrides: Partial<MessageAuthor> = {}): MessageAuthor => ({
Expand Down Expand Up @@ -149,3 +150,47 @@ describe("up-arrow recall in a shared conversation", () => {
).toEqual(["Shipped."]);
});
});

describe("mayBeSharedConversation", () => {
const person = (
level: "viewer" | "collaborator" | "owner",
you: boolean,
firstOwner = false,
) => ({
level,
you,
firstOwner,
});
const unshared = { shared: false, authors: [], answers: [] };

it("holds every message until the first read answers or fails", () => {
expect(mayBeSharedConversation({ record: null, failed: false, people: null })).toBe(true);
expect(mayBeSharedConversation({ record: null, failed: true, people: null })).toBe(false);
});

it("knows a thread is shared from who reaches it, before any author is recorded", () => {
const firstOwner = person("owner", true, true);
expect(
mayBeSharedConversation({
record: unshared,
failed: false,
people: [firstOwner, person("collaborator", false)],
}),
).toBe(true);
expect(
mayBeSharedConversation({
record: unshared,
failed: false,
people: [firstOwner, person("viewer", false)],
}),
).toBe(false);
// To a Viewer, a message nobody is recorded for may be the first owner's.
expect(
mayBeSharedConversation({
record: unshared,
failed: false,
people: [person("owner", false, true), person("viewer", true)],
}),
).toBe(true);
});
});
Loading
Loading