Skip to content
Open
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,9 @@ jobs:
# Bots panels saying Working for as long as its Bot works, through a read of every query and
# while a task it handed out works on. And Return in the message box starting a new line on a
# phone's touch keyboard while a computer's Enter still sends, never on the Enter that confirms
# a composed character. And a Bot's instructions at 5000 characters, held whole by its Setup
# a composed character. And every key typed in the message box landing where it was typed while a
# Bot's answer streams in, as it starts and ends, typed into the middle of the words, and in Home's
# box locked to a Bot, the box never drawn again under the keys. And a Bot's instructions at 5000 characters, held whole by its Setup
# page and the New Bot sheet, and a 5001st refused in view. And push notifications in this
# browser, turned on and off in Settings and saved through the API, a push shown by the service
# worker and a click on it opening its conversation, and on an iPhone in Safari the Home Screen
Expand Down Expand Up @@ -808,6 +810,7 @@ jobs:
app/tests/composer-keeps-draft-browser.test.tsx
app/tests/composer-send-while-dictating.test.tsx
app/tests/composer-touch-return.test.tsx
app/tests/composer-typing-while-streaming.test.tsx
app/tests/computer-panel-phone-layout.test.tsx
app/tests/computer-starting-browser.test.tsx
app/tests/connected-bot-mistakes-layout.test.tsx
Expand Down
42 changes: 40 additions & 2 deletions app/src/components/channels/composer/composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ import { useDictation } from "./use-dictation";

/** One shared empty list, so a box with no commands does not rebuild its triggers per render. */
const NO_COMMANDS: readonly CommandOption[] = [];
/** The same for a box with no Bots to mention. */
const NO_AGENTS: readonly AgentOption[] = [];

/** Send was pressed while live dictation listened, it heard nothing, and the box had nothing else. */
const NOTHING_HEARD = "No words were heard, so nothing was sent. Try again.";
Expand Down Expand Up @@ -461,8 +463,8 @@ export function Composer({
compact = false,
voiceCall,
modelPicker,
agents = [],
lockedAgent,
agents: givenAgents = NO_AGENTS,
lockedAgent: givenLockedAgent,
commands = NO_COMMANDS,
onSubmit,
onQueue,
Expand All @@ -482,6 +484,27 @@ export function Composer({
queuedAttachmentCount = 0,
draftKey,
}: ComposerProps) {
/*
* THE BOTS, AND THE BOT THE BOX IS LOCKED TO, KEPT THE SAME OBJECTS WHILE THEY SAY THE SAME THING.
*
* The screens build them afresh on every render (`toAgentOptions(...)` in `channel-chat.tsx` and
* on Home), and a conversation renders again for every word of an answer as it streams in. Each
* new list was a new `triggers` for prompt-area, and a new locked Bot a new `value`, and either
* makes prompt-area draw the box again from `value`, after the render that brought them. A key
* pressed in that moment is not in that `value` yet: the box was drawn without the letter just
* typed, then with it, and the caret was left where the shorter words ended. So the next letters
* went in front of the last one ("teh"), and in an empty box the caret went to the start, with
* everything typed after it landing ahead of the first letter. Kept the same, they change only
* when a Bot does, so an answer streaming in never draws the box again under somebody typing.
*/
const agents = useKeptWhileSame(
givenAgents,
JSON.stringify(givenAgents.map(agentKey)),
);
const lockedAgent = useKeptWhileSame(
givenLockedAgent,
givenLockedAgent ? JSON.stringify(agentKey(givenLockedAgent)) : "",
);
/*
* Where this composer keeps its unsent message, and what it had kept, read once as it mounts.
* Words handed in by the caller (`initialValue`, `initialFiles`) are a message being put back on
Expand Down Expand Up @@ -2511,6 +2534,21 @@ export function Composer({
);
}

/** What a Bot shows in the box: its chip and its line in the `@` menu. */
function agentKey(agent: AgentOption) {
return [agent.id, agent.name, agent.description ?? null, agent.avatarSeed];
}

/**
* `value`, or the one kept before it while `key` is the same: a new object only once it says
* something new. Kept as `chat-transcript.tsx` keeps the names its steps found.
*/
function useKeptWhileSame<T>(value: T, key: string): T {
const kept = useRef({ key, value });
if (kept.current.key !== key) kept.current = { key, value };
return kept.current.value;
}

/** The locked Bot's chip, as `@` would have made it. */
function lockedChip(agent: AgentOption): Segment {
return {
Expand Down
139 changes: 139 additions & 0 deletions app/tests/composer-typing-while-streaming.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { CopilotKitProvider } from "@copilotkit/react-core/v2";
import { QueryClientProvider } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { Composer } from "@/components/channels/composer/composer";
import { toAgentOptions } from "@/components/channels/composer/triggers";
import { authKeys } from "@/lib/auth/queries";
import { deploymentKeys } from "@/lib/deployment/queries";
import { queryClient } from "@/query-client";

/**
* The message box under a Bot's answer as it streams in, for composer-typing-while-streaming.test.tsx
* to bundle and load in a browser.
*
* The answer grows by a word every `TOKEN_MS`, from a timer, as a socket's messages arrive: each word
* renders the page again at the default priority, as `useAgent`'s update does in `channel-chat.tsx`.
* Drawing it takes `RENDER_MS`, standing in for the transcript laying out a long answer again. And
* every render hands the box its Bots as new objects, built by `toAgentOptions`, as the screens do.
*
* `?layout=thread` (the default) is a conversation's compact box, with the props
* `conversation-view.tsx` gives it: a turn in flight while the answer streams, and a queue to park
* a message in. `?layout=home` is Home's box locked to a Bot, as `index.tsx` draws it when a Bot is
* pressed in the Bots panel, with the page rendering again as a Bot works elsewhere.
*
* `window.stream(true)` starts the answer and `window.stream(false)` ends it. `?token=<ms>` streams
* slower than a word every 16 ms, for watching it.
*/
declare global {
interface Window {
stream: (on: boolean) => void;
}
}

const params = new URLSearchParams(location.search);
const RENDER_MS = 8;
const TOKEN_MS = Number(params.get("token") ?? 16);

queryClient.setQueryData(deploymentKeys.capabilities(), {
generativeUi: true,
transcription: false,
});
queryClient.setQueryData(authKeys.currentUser(), {
id: "u-1",
name: "Dana Reyes",
email: "dana@example.com",
});

const PROFILES = [{ id: "design-bot", name: "Design Bot", title: "Design" }];

/** What the Bot writes, a word at a time, from the start again once it runs out. */
const ANSWER = (
"Here is a first pass at the landing page. The opening section leads with the promise in one " +
"line and a single button, so the page says what it is before it asks for anything. The second " +
"section walks through the three things a team does in its first week, each with a short " +
"example drawn from a real conversation, and a picture of the screen it happens on. The third " +
"section answers the questions people ask on a first call: where their data lives, what a Bot " +
"may touch, and how a person stays in charge of every change. The page closes on the same " +
"button it opened with, and a line about pricing for anyone still deciding. For the visuals, " +
"the screens are the product's own, cropped close enough to read, with nothing drawn on top of " +
"them. Each example names the team it came from and what they were trying to get done, in their " +
"words rather than ours. The tone stays plain: short sentences, no claims a reader cannot check, " +
"and every number on the page linked to where it came from. I kept the copy for the second " +
"section longest, because that is where a reader decides whether this is for them, but it can " +
"come down by a third without losing an example if you would rather the page scrolled less. " +
"Next I can draft the three examples in full, or mock up the opening section at a phone's width."
).split(" ");

/** The Bot's answer so far, laid out for as long as a long answer takes. */
function Answer({ words }: { words: number }) {
const until = performance.now() + RENDER_MS;
while (performance.now() < until) {
// Standing in for the transcript's markdown, parsed and laid out again for every token.
}
return (
<p className="text-sm">
{Array.from({ length: words }, (_, i) => ANSWER[i % ANSWER.length]).join(
" ",
)}
</p>
);
}

function Page() {
const [streaming, setStreaming] = useState(false);
const [words, setWords] = useState(0);
useEffect(() => {
window.stream = setStreaming;
}, []);
useEffect(() => {
if (!streaming) return;
const id = setInterval(() => setWords((count) => count + 1), TOKEN_MS);
return () => clearInterval(id);
}, [streaming]);

if (params.get("layout") === "home") {
return (
<div className="flex min-h-screen flex-col items-center justify-center gap-4 bg-background p-4">
<Composer
agents={toAgentOptions(PROFILES)}
autoFocus
className="w-full max-w-2xl"
lockedAgent={toAgentOptions(PROFILES)[0]}
onSubmit={() => undefined}
/>
<div className="h-24 w-full max-w-2xl overflow-hidden">
<Answer words={words} />
</div>
</div>
);
}
return (
<div className="flex h-svh flex-col bg-background">
<div className="flex min-h-0 flex-1 flex-col justify-end overflow-hidden p-4">
<Answer words={words} />
</div>
<div className="mx-auto w-full max-w-[calc(var(--container-3xl)+2rem)] shrink-0 px-4 pb-4">
<Composer
agents={toAgentOptions(PROFILES)}
className="relative w-full mt-auto"
compact
onQueue={() => undefined}
onSubmit={() => undefined}
pending={streaming}
/>
</div>
</div>
);
}

const root = document.getElementById("root");
if (root) {
createRoot(root).render(
<QueryClientProvider client={queryClient}>
<CopilotKitProvider runtimeUrl="/api/copilotkit">
<Page />
</CopilotKitProvider>
</QueryClientProvider>,
);
}
Loading
Loading