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
56 changes: 55 additions & 1 deletion apps/server/src/relay/AgentAwarenessRelay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,46 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
).toHaveLength(160);
});

it("resolves standalone chats without requiring a project snapshot", () => {
const environmentId = "env-1" as EnvironmentId;
const threadId = "chat-1" as ThreadId;
const thread = {
id: threadId,
projectId: null,
title: "Standalone chat",
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
session: null,
latestTurn: {
turnId: "turn-chat" as TurnId,
state: "completed",
requestedAt: "2026-05-25T00:00:00.000Z",
startedAt: "2026-05-25T00:00:00.000Z",
completedAt: "2026-05-25T00:01:00.000Z",
assistantMessageId: null,
},
updatedAt: "2026-05-25T00:01:00.000Z",
hasPendingApprovals: false,
hasPendingUserInput: false,
} as OrchestrationThreadShell;

expect(
AgentAwarenessRelay.resolveAgentAwarenessRelayPublishSnapshot({
environmentId,
threadId,
thread: Option.some(thread),
project: Option.none(),
}),
).toMatchObject({
projectId: null,
reason: "snapshot",
state: {
projectTitle: "Chats",
threadTitle: "Standalone chat",
phase: "completed",
},
});
});

it("resolves a null publish state when a thread or project snapshot disappeared", () => {
const environmentId = "env-1" as EnvironmentId;
const threadId = "thread-1" as ThreadId;
Expand Down Expand Up @@ -291,6 +331,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
const environmentId = "env-1" as EnvironmentId;
const projectId = "project-1" as ProjectId;
const activeThreadId = "thread-active" as ThreadId;
const activeChatId = "chat-active" as ThreadId;
const idleThreadId = "thread-idle" as ThreadId;

const baseThread = {
Expand Down Expand Up @@ -338,6 +379,19 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
...baseThread,
id: idleThreadId,
},
{
...baseThread,
id: activeChatId,
projectId: null,
latestTurn: {
turnId: "turn-chat" as TurnId,
state: "running",
requestedAt: now,
startedAt: now,
completedAt: null,
assistantMessageId: null,
},
},
{
...baseThread,
id: "thread-missing-project" as ThreadId,
Expand All @@ -353,7 +407,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
},
],
}),
).toEqual([activeThreadId]);
).toEqual([activeThreadId, activeChatId]);
});

it("signs the activity publish JWT and rejects tampering", async () => {
Expand Down
39 changes: 36 additions & 3 deletions apps/server/src/relay/AgentAwarenessRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ export class AgentAwarenessRelay extends Context.Service<
}
>()("t3/relay/AgentAwarenessRelay") {}

const STANDALONE_CHATS_ACTIVITY_GROUP = { title: "Chats" } as const;

function threadAwareness(input: {
readonly environmentId: EnvironmentId;
readonly thread: OrchestrationThreadShell;
readonly project: Pick<OrchestrationProjectShell, "title"> | null;
}) {
return projectThreadAwareness({
environmentId: input.environmentId,
project: input.project ?? STANDALONE_CHATS_ACTIVITY_GROUP,
thread: input.thread,
});
}

export function eventThreadId(event: OrchestrationEvent): ThreadId | null {
const payload = event.payload as { readonly threadId?: unknown };
if (typeof payload.threadId === "string") {
Expand Down Expand Up @@ -246,6 +260,19 @@ export function resolveAgentAwarenessRelayPublishSnapshot(input: {
reason: "thread-not-found",
};
}
if (input.thread.value.projectId === null) {
return {
projectId: null,
state: sanitizeRelayAgentActivityState(
threadAwareness({
environmentId: input.environmentId,
project: null,
thread: input.thread.value,
}),
),
reason: "snapshot",
};
}
if (Option.isNone(input.project)) {
return {
projectId: input.thread.value.projectId,
Expand All @@ -256,7 +283,7 @@ export function resolveAgentAwarenessRelayPublishSnapshot(input: {
return {
projectId: input.thread.value.projectId,
state: sanitizeRelayAgentActivityState(
projectThreadAwareness({
threadAwareness({
environmentId: input.environmentId,
project: input.project.value,
thread: input.thread.value,
Expand All @@ -275,14 +302,20 @@ export function resolveAgentAwarenessRelayActiveThreadIds(input: {
return input.threads
.filter((thread) => {
if (thread.projectId === null) {
return false;
return (
threadAwareness({
environmentId: input.environmentId,
project: null,
thread,
}) !== null
);
}
const project = projectById.get(thread.projectId);
if (!project) {
return false;
}
return (
projectThreadAwareness({
threadAwareness({
environmentId: input.environmentId,
project,
thread,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/ChatView.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function buildLocalDraftThread(
id: threadId,
environmentId: draftThread.environmentId,
projectId: draftThread.projectId,
title: "New thread",
title: draftThread.projectId === null ? "New chat" : "New thread",
modelSelection: fallbackModelSelection,
runtimeMode: draftThread.runtimeMode,
interactionMode: draftThread.interactionMode,
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ function ChatViewContent(props: ChatViewProps) {
[mountedTerminalThreadKeys],
);

const fallbackDraftProjectRef = draftThread
const fallbackDraftProjectRef = draftThread?.projectId
? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
: null;
const fallbackDraftProject = useProject(fallbackDraftProjectRef);
Expand Down Expand Up @@ -4177,12 +4177,13 @@ function ChatViewContent(props: ChatViewProps) {
let turnStartSucceeded = false;
if (failure === null && turnAttachmentsResult._tag === "Success") {
const bootstrap =
(isLocalDraftThread && activeProject) || baseBranchForWorktree
isLocalDraftThread || baseBranchForWorktree
? {
...(isLocalDraftThread && activeProject
...(isLocalDraftThread
? {
createThread: {
projectId: activeProject.id,
projectId: activeProject?.id ?? null,
...(activeProject ? {} : { context: { kind: "standalone" as const } }),
title,
modelSelection: threadCreateModelSelection,
runtimeMode,
Expand Down
57 changes: 18 additions & 39 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import type {
SidebarThreadGroupingMode,
ThreadId,
} from "@t3tools/contracts";
import { DEFAULT_MODEL, ProviderInstanceId } from "@t3tools/contracts";
import {
MAX_SIDEBAR_THREAD_PREVIEW_COUNT,
MIN_SIDEBAR_THREAD_PREVIEW_COUNT,
Expand Down Expand Up @@ -95,7 +94,7 @@ import { useOpenPrLink } from "../lib/openPullRequestLink";
import { startNewThreadInProjectFromContext } from "../lib/chatThreadActions";
import { isTerminalFocused } from "../lib/terminalFocus";
import { sortThreads } from "../lib/threadSort";
import { isMacPlatform, newThreadId } from "../lib/utils";
import { isMacPlatform, newDraftId, newThreadId } from "../lib/utils";
import { readLocalApi } from "../localApi";
import {
derivePhysicalProjectKey,
Expand All @@ -116,7 +115,6 @@ import { useDesktopUpdateState } from "../state/desktopUpdate";
import {
useProject,
useProjects,
useServerConfigs,
useThreadShells,
useThreadShellsForProjectRefs,
} from "../state/entities";
Expand Down Expand Up @@ -3939,7 +3937,6 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(

export default function Sidebar() {
const projects = useProjects();
const serverConfigs = useServerConfigs();
const sidebarThreads = useThreadShells();
const projectSidebarThreads = useMemo(
() => sidebarThreads.filter((thread) => thread.projectId !== null),
Expand Down Expand Up @@ -4024,7 +4021,6 @@ export default function Sidebar() {
const shortcutModifiers = useShortcutModifierState();
const { environments } = useEnvironments();
const primaryEnvironmentId = usePrimaryEnvironmentId();
const createThread = useAtomCommand(threadEnvironment.create, { reportFailure: false });
const environmentLabelById = useMemo(
() =>
new Map(
Expand Down Expand Up @@ -4186,42 +4182,25 @@ export default function Sidebar() {
});
return;
}
const providers = serverConfigs.get(primaryEnvironmentId)?.providers ?? [];
const provider =
providers.find(
(candidate) => candidate.enabled && candidate.installed && candidate.models.length > 0,
) ?? providers.find((candidate) => candidate.enabled && candidate.installed);
const draftId = newDraftId();
const threadId = newThreadId();
const modelSelection = {
instanceId: provider?.instanceId ?? ProviderInstanceId.make("codex"),
model: provider?.models[0]?.slug ?? DEFAULT_MODEL,
};
void createThread({
environmentId: primaryEnvironmentId,
input: {
threadId,
projectId: null,
context: { kind: "standalone" },
title: "New chat",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
},
}).then((result) => {
if (result._tag === "Failure") {
const error = squashAtomCommandFailure(result);
toastManager.add({
type: "error",
title: "Could not start chat",
description: error instanceof Error ? error.message : "An unexpected error occurred.",
});
return;
}
navigateToThread(scopeThreadRef(primaryEnvironmentId, threadId));
const draftStore = useComposerDraftStore.getState();
draftStore.setStandaloneDraftThreadId(primaryEnvironmentId, draftId, {
threadId,
createdAt: new Date().toISOString(),
});
draftStore.applyStickyState(draftId);
if (useThreadSelectionStore.getState().selectedThreadKeys.size > 0) {
clearSelection();
}
if (isMobile) {
setOpenMobile(false);
}
void navigate({
to: "/draft/$draftId",
params: { draftId },
});
}, [createThread, navigateToThread, primaryEnvironmentId, serverConfigs]);
}, [clearSelection, isMobile, navigate, primaryEnvironmentId, setOpenMobile]);

const projectDnDSensors = useSensors(
useSensor(PointerSensor, {
Expand Down
22 changes: 22 additions & 0 deletions apps/web/src/composerDraftStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,28 @@ describe("composerDraftStore project draft thread mapping", () => {
});
});

it("stores standalone chats as projectless drafts until first send", () => {
const store = useComposerDraftStore.getState();

store.setStandaloneDraftThreadId(TEST_ENVIRONMENT_ID, draftId, {
threadId,
createdAt: "2026-01-01T00:00:00.000Z",
});

expect(useComposerDraftStore.getState().getDraftSession(draftId)).toMatchObject({
threadId,
environmentId: TEST_ENVIRONMENT_ID,
projectId: null,
branch: null,
worktreePath: null,
envMode: "local",
runtimeMode: "full-access",
interactionMode: "default",
createdAt: "2026-01-01T00:00:00.000Z",
});
expect(useComposerDraftStore.getState().getDraftThreadByProjectRef(projectRef)).toBeNull();
});

it("clears only matching project draft mapping entries", () => {
const store = useComposerDraftStore.getState();
store.setProjectDraftThreadId(projectRef, draftId, { threadId });
Expand Down
Loading
Loading