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
73 changes: 65 additions & 8 deletions apps/desktop/src/lib/trpc/routers/ai-chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { z } from "zod";
import { publicProcedure, router } from "../..";
import {
type ClaudeStreamEvent,
claudeSessionManager,
chatSessionManager,
sessionStore,
} from "./utils/session-manager";

export const createAiChatRouter = () => {
Expand All @@ -20,11 +21,28 @@ export const createAiChatRouter = () => {
.input(
z.object({
sessionId: z.string(),
workspaceId: z.string(),
cwd: z.string(),
}),
)
.mutation(async ({ input }) => {
await claudeSessionManager.startSession({
await chatSessionManager.startSession({
sessionId: input.sessionId,
workspaceId: input.workspaceId,
cwd: input.cwd,
});
return { success: true };
}),

restoreSession: publicProcedure
.input(
z.object({
sessionId: z.string(),
cwd: z.string(),
}),
)
.mutation(async ({ input }) => {
await chatSessionManager.restoreSession({
sessionId: input.sessionId,
cwd: input.cwd,
});
Expand All @@ -34,25 +52,64 @@ export const createAiChatRouter = () => {
interrupt: publicProcedure
.input(z.object({ sessionId: z.string() }))
.mutation(async ({ input }) => {
await claudeSessionManager.interrupt({ sessionId: input.sessionId });
await chatSessionManager.interrupt({
sessionId: input.sessionId,
});
return { success: true };
}),

stopSession: publicProcedure
.input(z.object({ sessionId: z.string() }))
.mutation(async ({ input }) => {
await claudeSessionManager.stopSession({ sessionId: input.sessionId });
await chatSessionManager.deactivateSession({
sessionId: input.sessionId,
});
return { success: true };
}),

deleteSession: publicProcedure
.input(z.object({ sessionId: z.string() }))
.mutation(async ({ input }) => {
await chatSessionManager.deleteSession({
sessionId: input.sessionId,
});
return { success: true };
}),

renameSession: publicProcedure
.input(
z.object({
sessionId: z.string(),
title: z.string(),
}),
)
.mutation(async ({ input }) => {
await chatSessionManager.updateSessionMeta(input.sessionId, {
title: input.title,
});
return { success: true };
}),

listSessions: publicProcedure
.input(z.object({ workspaceId: z.string() }))
.query(async ({ input }) => {
return sessionStore.listByWorkspace(input.workspaceId);
}),

getSession: publicProcedure
.input(z.object({ sessionId: z.string() }))
.query(async ({ input }) => {
return (await sessionStore.get(input.sessionId)) ?? null;
}),

isSessionActive: publicProcedure
.input(z.object({ sessionId: z.string() }))
.query(({ input }) => {
return claudeSessionManager.isSessionActive(input.sessionId);
return chatSessionManager.isSessionActive(input.sessionId);
}),

getActiveSessions: publicProcedure.query(() => {
return claudeSessionManager.getActiveSessions();
return chatSessionManager.getActiveSessions();
}),

streamEvents: publicProcedure
Expand All @@ -66,10 +123,10 @@ export const createAiChatRouter = () => {
emit.next(event);
};

claudeSessionManager.on("event", onEvent);
chatSessionManager.on("event", onEvent);

return () => {
claudeSessionManager.off("event", onEvent);
chatSessionManager.off("event", onEvent);
};
});
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { buildClaudeEnv } from "../auth";
import type {
AgentProvider,
AgentProviderSpec,
AgentRegistration,
} from "./types";

const CLAUDE_AGENT_URL =
process.env.CLAUDE_AGENT_URL || "http://localhost:9090";

export class ClaudeSdkProvider implements AgentProvider {
readonly spec: AgentProviderSpec = {
id: "claude-sdk",
name: "Claude",
};

getAgentRegistration({
sessionId,
cwd,
}: {
sessionId: string;
cwd: string;
}): AgentRegistration {
const env = buildClaudeEnv();

return {
id: "claude",
endpoint: `${CLAUDE_AGENT_URL}/`,
triggers: "user-messages",
bodyTemplate: {
sessionId,
cwd,
env,
},
};
}

async getProviderSessionId(sessionId: string): Promise<string | undefined> {
try {
const res = await fetch(`${CLAUDE_AGENT_URL}/sessions/${sessionId}`);
if (!res.ok) return undefined;

const data = (await res.json()) as {
claudeSessionId?: string;
};
return data.claudeSessionId;
} catch {
return undefined;
}
Comment on lines +47 to +49
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Silent error swallowing in getProviderSessionId.

The catch block returns undefined without logging. Per coding guidelines, errors should at minimum be logged with context. A network failure here would be invisible during debugging.

Proposed fix
-		} catch {
-			return undefined;
+		} catch (error) {
+			console.error(`[agent-provider/claude] Failed to get provider session ID for ${sessionId}:`, error);
+			return undefined;
 		}

As per coding guidelines: "Never swallow errors silently; at minimum log errors with context before rethrowing or handling them explicitly."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch {
return undefined;
}
} catch (error) {
console.error(`[agent-provider/claude] Failed to get provider session ID for ${sessionId}:`, error);
return undefined;
}
🤖 Prompt for AI Agents
In
`@apps/desktop/src/lib/trpc/routers/ai-chat/utils/agent-provider/claude-sdk-provider.ts`
around lines 55 - 57, The catch in getProviderSessionId is silently swallowing
errors; update the catch to log the error with context (e.g., include the
function name "getProviderSessionId", provider identifier or request details)
before returning undefined or rethrowing as appropriate; ensure you capture the
caught error object and pass it to your existing logger (e.g., processLogger,
logger, or console.error) so network/HTTP failures are visible during debugging.

}

async cleanup(_sessionId: string): Promise<void> {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { ClaudeSdkProvider } from "./claude-sdk-provider";
export type {
AgentProvider,
AgentProviderSpec,
AgentRegistration,
} from "./types";
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export interface AgentProviderSpec {
id: string;
name: string;
}

export interface AgentRegistration {
id: string;
endpoint: string;
triggers: string;
bodyTemplate: Record<string, unknown>;
}

export interface AgentProvider {
readonly spec: AgentProviderSpec;

getAgentRegistration(opts: {
sessionId: string;
cwd: string;
}): AgentRegistration;

getProviderSessionId(sessionId: string): Promise<string | undefined>;

cleanup(sessionId: string): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { ClaudeSdkProvider } from "../agent-provider";
import { SessionStore } from "../session-store";
import { ChatSessionManager } from "./session-manager";

export type {
ClaudeStreamEvent,
ErrorEvent,
SessionEndEvent,
SessionStartEvent,
} from "./session-manager";
export { claudeSessionManager } from "./session-manager";
export { ChatSessionManager } from "./session-manager";

const provider = new ClaudeSdkProvider();
const sessionStore = new SessionStore();

export const chatSessionManager = new ChatSessionManager(
provider,
sessionStore,
);
export { sessionStore };
Loading