Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { WorkspaceStore } from "@superset/panes";
import type { ChatLaunchConfig } from "shared/tabs-types";
import type { StoreApi } from "zustand/vanilla";
import type { PaneViewerData } from "../../../../types";
import { ChatPaneInterface as WorkspaceChatInterface } from "./components/WorkspaceChatInterface";
import { useWorkspaceChatController } from "./hooks/useWorkspaceChatController";

Expand All @@ -8,12 +11,18 @@ export function ChatPane({
workspaceId,
initialLaunchConfig,
onConsumeLaunchConfig,
paneId = null,
tabId = null,
store = null,
}: {
onSessionIdChange: (sessionId: string | null) => void;
sessionId: string | null;
workspaceId: string;
initialLaunchConfig?: ChatLaunchConfig | null;
onConsumeLaunchConfig?: () => void;
paneId?: string | null;
tabId?: string | null;
store?: StoreApi<WorkspaceStore<PaneViewerData>> | null;
}) {
const { organizationId, workspacePath, handleNewChat, getOrCreateSession } =
useWorkspaceChatController({
Expand All @@ -33,6 +42,9 @@ export function ChatPane({
workspaceId={workspaceId}
organizationId={organizationId}
cwd={workspacePath}
paneId={paneId}
tabId={tabId}
store={store}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "renderer/lib/dev-chat";
import { posthog } from "renderer/lib/posthog";
import { useChatPreferencesStore } from "renderer/stores/chat-preferences";
import { useAutoOpenSubagentPanes } from "../../hooks/useAutoOpenSubagentPanes";
import {
type UseChatDisplayReturn,
useChatDisplay,
Expand Down Expand Up @@ -198,6 +199,9 @@ export function ChatPaneInterface({
getOrCreateSession,
onResetSession,
onUserMessageSubmitted,
paneId = null,
tabId = null,
store = null,
}: ChatPaneInterfaceProps) {
const { models: availableModels, defaultModel } = useAvailableModels();
const selectedModelId = useChatPreferencesStore(
Expand Down Expand Up @@ -309,6 +313,13 @@ export function ChatPaneInterface({
pendingPlanApproval = null,
pendingQuestion = null,
} = chat;
useAutoOpenSubagentPanes({
store,
tabId,
parentPaneId: paneId,
sessionId,
activeSubagents,
});
const isAwaitingAssistant =
isRunning || submitStatus === "submitted" || submitStatus === "streaming";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { WorkspaceStore } from "@superset/panes";
import type { ChatLaunchConfig } from "shared/tabs-types";
import type { StoreApi } from "zustand/vanilla";
import type { PaneViewerData } from "../../../../../../types";

export interface ChatPaneInterfaceProps {
sessionId: string | null;
Expand All @@ -16,4 +19,7 @@ export interface ChatPaneInterfaceProps {
getOrCreateSession: () => Promise<string>;
onResetSession: () => Promise<void>;
onUserMessageSubmitted?: (message: string) => void;
paneId?: string | null;
tabId?: string | null;
store?: StoreApi<WorkspaceStore<PaneViewerData>> | null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useAutoOpenSubagentPanes } from "./useAutoOpenSubagentPanes";
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type { WorkspaceStore } from "@superset/panes";
import { useEffect, useRef } from "react";
import type { StoreApi } from "zustand/vanilla";
import type { PaneViewerData } from "../../../../../../types";
import {
findSubagentPaneLocation,
openSubagentPane,
} from "../../../../../../utils/openSubagentPane";
import type { UseChatDisplayReturn } from "../useWorkspaceChatDisplay";

type ChatActiveSubagents = NonNullable<UseChatDisplayReturn["activeSubagents"]>;

function asRecord(value: unknown): Record<string, unknown> | null {
if (typeof value !== "object" || value === null || Array.isArray(value)) {
return null;
}
return value as Record<string, unknown>;
}

function asString(value: unknown): string | undefined {
if (typeof value !== "string") return undefined;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}

function listSubagentEntries(
activeSubagents: ChatActiveSubagents | undefined | null,
): Array<{
toolCallId: string;
task?: string;
agentType?: string;
}> {
if (!activeSubagents || typeof activeSubagents.entries !== "function") {
return [];
}

const entries: Array<{
toolCallId: string;
task?: string;
agentType?: string;
}> = [];
for (const [toolCallId, subagent] of activeSubagents.entries()) {
if (typeof toolCallId !== "string" || toolCallId.length === 0) continue;
const record = asRecord(subagent);
entries.push({
toolCallId,
task: asString(record?.task),
agentType: asString(record?.agentType) ?? asString(record?.displayName),
});
}
return entries;
}

export function useAutoOpenSubagentPanes({
store,
tabId,
parentPaneId,
sessionId,
activeSubagents,
}: {
store: StoreApi<WorkspaceStore<PaneViewerData>> | null;
tabId: string | null;
parentPaneId: string | null;
sessionId: string | null;
activeSubagents: ChatActiveSubagents | undefined | null;
}) {
const openedToolCallIdsRef = useRef(new Set<string>());

useEffect(() => {
openedToolCallIdsRef.current.clear();
}, [sessionId, parentPaneId]);

useEffect(() => {
if (!store || !tabId || !parentPaneId || !sessionId) return;

const state = store.getState();
for (const entry of listSubagentEntries(activeSubagents)) {
if (openedToolCallIdsRef.current.has(entry.toolCallId)) continue;
openedToolCallIdsRef.current.add(entry.toolCallId);
if (findSubagentPaneLocation(state, entry.toolCallId)) continue;

openSubagentPane(store, {
tabId,
parentPaneId,
parentSessionId: sessionId,
toolCallId: entry.toolCallId,
task: entry.task,
agentType: entry.agentType,
});
}
}, [activeSubagents, parentPaneId, sessionId, store, tabId]);
Comment thread
hackice20 marked this conversation as resolved.
Resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { RendererContext } from "@superset/panes";
import { useMemo } from "react";
import type { PaneViewerData, SubagentPaneData } from "../../../../types";
import { SubagentExecutionMessage } from "../ChatPane/components/WorkspaceChatInterface/components/ChatMessageList/components/SubagentExecutionMessage";
import { useChatDisplay } from "../ChatPane/hooks/useWorkspaceChatDisplay";
import { resolveSubagentEntries } from "./utils/resolveSubagentEntries";

interface SubagentPaneProps {
context: RendererContext<PaneViewerData>;
workspaceId: string;
}

export function SubagentPane({ context, workspaceId }: SubagentPaneProps) {
const data = context.pane.data as SubagentPaneData;
const chat = useChatDisplay({
sessionId: data.parentSessionId,
workspaceId,
enabled: Boolean(data.parentSessionId),
});

const entries = useMemo(
() =>
resolveSubagentEntries({
toolCallId: data.toolCallId,
activeSubagents: chat.activeSubagents,
messages: chat.messages ?? [],
fallback: {
task: data.task,
agentType: data.agentType,
},
}),
[
chat.activeSubagents,
chat.messages,
data.agentType,
data.task,
data.toolCallId,
],
);

return (
<div className="flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden bg-background">
<div className="min-h-0 flex-1 overflow-y-auto p-3 select-text">
{entries.length > 0 ? (
<SubagentExecutionMessage subagents={entries} inline />
) : (
<div className="flex h-full items-center justify-center px-4 text-center text-sm text-muted-foreground">
Waiting for subagent activity…
</div>
)}
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { RendererContext } from "@superset/panes";
import { cn } from "@superset/ui/utils";
import { useMemo } from "react";
import type { PaneViewerData, SubagentPaneData } from "../../../../../../types";
import { useChatDisplay } from "../../../ChatPane/hooks/useWorkspaceChatDisplay";
import { resolveSubagentTitle } from "../../utils/resolveSubagentEntries";

interface SubagentPaneTitleProps {
context: RendererContext<PaneViewerData>;
workspaceId: string;
}

export function SubagentPaneTitle({
context,
workspaceId,
}: SubagentPaneTitleProps) {
const data = context.pane.data as SubagentPaneData;
const chat = useChatDisplay({
sessionId: data.parentSessionId,
workspaceId,
enabled: Boolean(data.parentSessionId),
});

const title = useMemo(
() =>
resolveSubagentTitle({
toolCallId: data.toolCallId,
activeSubagents: chat.activeSubagents,
messages: chat.messages ?? [],
fallback: {
task: data.task,
agentType: data.agentType,
},
}),
[
chat.activeSubagents,
chat.messages,
data.agentType,
data.task,
data.toolCallId,
],
);

return (
<div
className={cn(
"flex min-w-0 items-center gap-1.5 text-xs transition-colors duration-150",
context.isActive ? "text-foreground" : "text-muted-foreground",
)}
title={data.task ?? title}
>
<span className="min-w-0 truncate">{title}</span>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SubagentPaneTitle } from "./SubagentPaneTitle";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SubagentPane } from "./SubagentPane";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
resolveSubagentEntries,
resolveSubagentTitle,
} from "./resolveSubagentEntries";
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it } from "bun:test";
import { resolveSubagentEntries } from "./resolveSubagentEntries";

describe("resolveSubagentEntries", () => {
it("prefers live activeSubagents state", () => {
const activeSubagents = new Map([
[
"tool-1",
{
agentType: "explore",
task: "Find files",
textDelta: "Searching…",
toolCalls: [{ name: "bash", isError: false }],
status: "running" as const,
},
],
]);

const entries = resolveSubagentEntries({
toolCallId: "tool-1",
activeSubagents,
messages: [],
fallback: { task: "fallback" },
});

expect(entries).toHaveLength(1);
expect(entries[0]?.[0]).toBe("tool-1");
expect((entries[0]?.[1] as { task: string }).task).toBe("Find files");
});

it("falls back to message history when subagent left the live map", () => {
const entries = resolveSubagentEntries({
toolCallId: "tool-2",
activeSubagents: new Map(),
messages: [
{
id: "msg-1",
role: "assistant",
content: [
{
type: "tool_call",
id: "tool-2",
name: "subagent",
args: { task: "Summarize", agentType: "explore" },
},
{
type: "tool_result",
id: "tool-2",
result: "All done",
isError: false,
},
],
createdAt: new Date(),
},
] as never,
fallback: {},
});

expect(entries).toHaveLength(1);
expect((entries[0]?.[1] as { status: string }).status).toBe("completed");
expect((entries[0]?.[1] as { result?: string }).result).toBe("All done");
});

it("uses fallback when nothing is known yet", () => {
const entries = resolveSubagentEntries({
toolCallId: "tool-3",
activeSubagents: undefined,
messages: [],
fallback: { task: "Bootstrapping", agentType: "execute" },
});

expect(entries).toHaveLength(1);
expect((entries[0]?.[1] as { agentType: string }).agentType).toBe(
"execute",
);
});
});
Loading