@jcjeon/integration-cli 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +23 -0
- package/.npmignore +21 -0
- package/.prettierignore +6 -0
- package/.prettierrc +26 -0
- package/AGENTS.md +10 -0
- package/CLAUDE.md +10 -0
- package/README.md +384 -0
- package/apps/server/README.md +294 -0
- package/apps/server/eslint.config.mjs +20 -0
- package/apps/server/nest-cli.json +8 -0
- package/apps/server/package.json +89 -0
- package/apps/server/scripts/postinstall.js +53 -0
- package/apps/server/src/__mocks__/glob.js +6 -0
- package/apps/server/src/__mocks__/uuid.js +5 -0
- package/apps/server/src/app.controller.spec.ts +24 -0
- package/apps/server/src/app.controller.ts +13 -0
- package/apps/server/src/app.module.ts +18 -0
- package/apps/server/src/app.service.ts +8 -0
- package/apps/server/src/common/ji-paths.ts +41 -0
- package/apps/server/src/database/database.module.ts +27 -0
- package/apps/server/src/database/entities/agent-changelog.entity.ts +39 -0
- package/apps/server/src/database/entities/agent-session.entity.ts +29 -0
- package/apps/server/src/database/entities/conversation.entity.ts +41 -0
- package/apps/server/src/database/entities/session.entity.ts +16 -0
- package/apps/server/src/database/entities/task-agent-run.entity.ts +40 -0
- package/apps/server/src/database/entities/task-agent.entity.ts +42 -0
- package/apps/server/src/database/entities/task-requirement.entity.ts +27 -0
- package/apps/server/src/database/entities/task-run.entity.ts +41 -0
- package/apps/server/src/database/entities/task.entity.ts +44 -0
- package/apps/server/src/main.ts +65 -0
- package/apps/server/src/modules/agents/agent-model-settings.spec.ts +80 -0
- package/apps/server/src/modules/agents/agents.module.ts +11 -0
- package/apps/server/src/modules/agents/claude/claude-auth.manager.ts +83 -0
- package/apps/server/src/modules/agents/claude/claude-pty.manager.ts +380 -0
- package/apps/server/src/modules/agents/claude/claude.controller.ts +85 -0
- package/apps/server/src/modules/agents/claude/claude.gateway.ts +158 -0
- package/apps/server/src/modules/agents/claude/claude.module.ts +18 -0
- package/apps/server/src/modules/agents/claude/claude.service.ts +67 -0
- package/apps/server/src/modules/agents/claude/dto/create-session.dto.ts +24 -0
- package/apps/server/src/modules/agents/claude/dto/resize-session.dto.ts +13 -0
- package/apps/server/src/modules/agents/claude/dto/send-input.dto.ts +9 -0
- package/apps/server/src/modules/agents/claude/interfaces/claude-session.interface.ts +26 -0
- package/apps/server/src/modules/agents/claude/interfaces/pty-event.interface.ts +10 -0
- package/apps/server/src/modules/agents/claude/interfaces/stream-event.interface.ts +61 -0
- package/apps/server/src/modules/agents/codex/codex-auth.manager.ts +107 -0
- package/apps/server/src/modules/agents/codex/codex-session.manager.ts +357 -0
- package/apps/server/src/modules/agents/codex/codex.controller.ts +64 -0
- package/apps/server/src/modules/agents/codex/codex.gateway.ts +97 -0
- package/apps/server/src/modules/agents/codex/codex.module.ts +17 -0
- package/apps/server/src/modules/agents/codex/dto/configure-auth.dto.ts +7 -0
- package/apps/server/src/modules/agents/gemini/dto/configure-auth.dto.ts +15 -0
- package/apps/server/src/modules/agents/gemini/dto/create-session.dto.ts +9 -0
- package/apps/server/src/modules/agents/gemini/dto/send-input.dto.ts +9 -0
- package/apps/server/src/modules/agents/gemini/gemini-auth.manager.ts +157 -0
- package/apps/server/src/modules/agents/gemini/gemini-session.manager.ts +287 -0
- package/apps/server/src/modules/agents/gemini/gemini.controller.ts +93 -0
- package/apps/server/src/modules/agents/gemini/gemini.gateway.ts +149 -0
- package/apps/server/src/modules/agents/gemini/gemini.module.ts +17 -0
- package/apps/server/src/modules/agents/gemini/interfaces/gemini-session.interface.ts +18 -0
- package/apps/server/src/modules/agents/gemini/interfaces/stream-event.interface.ts +14 -0
- package/apps/server/src/modules/agents/session-termination.spec.ts +103 -0
- package/apps/server/src/modules/changelog/changelog.controller.ts +20 -0
- package/apps/server/src/modules/changelog/changelog.module.ts +14 -0
- package/apps/server/src/modules/changelog/changelog.service.spec.ts +531 -0
- package/apps/server/src/modules/changelog/changelog.service.ts +690 -0
- package/apps/server/src/modules/conversations/conversation.controller.spec.ts +106 -0
- package/apps/server/src/modules/conversations/conversation.controller.ts +60 -0
- package/apps/server/src/modules/conversations/conversation.module.ts +14 -0
- package/apps/server/src/modules/conversations/conversation.service.spec.ts +176 -0
- package/apps/server/src/modules/conversations/conversation.service.ts +54 -0
- package/apps/server/src/modules/conversations/dto/create-conversation.dto.ts +37 -0
- package/apps/server/src/modules/conversations/enums/conversation.enum.ts +13 -0
- package/apps/server/src/modules/fs/fs.controller.ts +29 -0
- package/apps/server/src/modules/fs/fs.module.ts +8 -0
- package/apps/server/src/modules/harness/dto/save-harness.dto.ts +9 -0
- package/apps/server/src/modules/harness/harness.controller.spec.ts +95 -0
- package/apps/server/src/modules/harness/harness.controller.ts +35 -0
- package/apps/server/src/modules/harness/harness.module.ts +11 -0
- package/apps/server/src/modules/harness/harness.service.spec.ts +217 -0
- package/apps/server/src/modules/harness/harness.service.ts +112 -0
- package/apps/server/src/modules/sessions/session.controller.spec.ts +68 -0
- package/apps/server/src/modules/sessions/session.controller.ts +43 -0
- package/apps/server/src/modules/sessions/session.module.ts +14 -0
- package/apps/server/src/modules/sessions/session.service.spec.ts +106 -0
- package/apps/server/src/modules/sessions/session.service.ts +35 -0
- package/apps/server/src/modules/tasks/dto/create-task.dto.ts +54 -0
- package/apps/server/src/modules/tasks/dto/execute-task.dto.ts +22 -0
- package/apps/server/src/modules/tasks/dto/merge-file.dto.ts +7 -0
- package/apps/server/src/modules/tasks/dto/rerun-task.dto.ts +14 -0
- package/apps/server/src/modules/tasks/dto/update-task.dto.ts +55 -0
- package/apps/server/src/modules/tasks/task-execution.service.ts +978 -0
- package/apps/server/src/modules/tasks/task.gateway.ts +140 -0
- package/apps/server/src/modules/tasks/tasks.controller.spec.ts +210 -0
- package/apps/server/src/modules/tasks/tasks.controller.ts +139 -0
- package/apps/server/src/modules/tasks/tasks.module.ts +30 -0
- package/apps/server/src/modules/tasks/tasks.service.spec.ts +552 -0
- package/apps/server/src/modules/tasks/tasks.service.ts +333 -0
- package/apps/server/test/app.e2e-spec.ts +28 -0
- package/apps/server/test/jest-e2e.json +9 -0
- package/apps/server/tsconfig.build.json +4 -0
- package/apps/server/tsconfig.json +13 -0
- package/apps/web/AGENTS.md +7 -0
- package/apps/web/CLAUDE.md +1 -0
- package/apps/web/README.md +36 -0
- package/apps/web/eslint.config.mjs +21 -0
- package/apps/web/next-env.d.ts +6 -0
- package/apps/web/next.config.ts +7 -0
- package/apps/web/package.json +49 -0
- package/apps/web/postcss.config.mjs +7 -0
- package/apps/web/public/file.svg +1 -0
- package/apps/web/public/globe.svg +1 -0
- package/apps/web/public/next.svg +1 -0
- package/apps/web/public/vercel.svg +1 -0
- package/apps/web/public/window.svg +1 -0
- package/apps/web/src/app/claude/page.tsx +5 -0
- package/apps/web/src/app/codex/page.tsx +126 -0
- package/apps/web/src/app/favicon.ico +0 -0
- package/apps/web/src/app/gemini/page.tsx +130 -0
- package/apps/web/src/app/globals.css +149 -0
- package/apps/web/src/app/layout.tsx +40 -0
- package/apps/web/src/app/login/page.tsx +67 -0
- package/apps/web/src/app/page.tsx +497 -0
- package/apps/web/src/app/task/[id]/page.tsx +11 -0
- package/apps/web/src/app/test/page.tsx +298 -0
- package/apps/web/src/components/ui/Modal.tsx +78 -0
- package/apps/web/src/components/ui/WorkingDirPicker.tsx +195 -0
- package/apps/web/src/components/ui/__tests__/Modal.test.tsx +68 -0
- package/apps/web/src/features/auth/api/__tests__/auth.api.test.ts +83 -0
- package/apps/web/src/features/auth/api/auth.api.ts +81 -0
- package/apps/web/src/features/auth/hooks/__tests__/useClaudeAuth.test.ts +166 -0
- package/apps/web/src/features/auth/hooks/__tests__/useCodexAuth.test.ts +127 -0
- package/apps/web/src/features/auth/hooks/__tests__/useGeminiAuth.test.ts +120 -0
- package/apps/web/src/features/auth/hooks/useClaudeAuth.ts +88 -0
- package/apps/web/src/features/auth/hooks/useCodexAuth.ts +149 -0
- package/apps/web/src/features/auth/hooks/useGeminiAuth.ts +125 -0
- package/apps/web/src/features/auth/ui/CodexLoginPanel.tsx +302 -0
- package/apps/web/src/features/auth/ui/GeminiLoginPanel.tsx +316 -0
- package/apps/web/src/features/auth/ui/LoginForm.tsx +190 -0
- package/apps/web/src/features/auth/ui/LoginPanel.tsx +114 -0
- package/apps/web/src/features/auth/ui/__tests__/LoginPanel.test.tsx +105 -0
- package/apps/web/src/features/chat/api/__tests__/sessions.api.test.ts +187 -0
- package/apps/web/src/features/chat/api/sessions.api.ts +161 -0
- package/apps/web/src/features/chat/container/ClaudePageContainer.tsx +152 -0
- package/apps/web/src/features/chat/hooks/__tests__/useCodexSessions.test.ts +131 -0
- package/apps/web/src/features/chat/hooks/__tests__/useGeminiSessions.test.ts +130 -0
- package/apps/web/src/features/chat/hooks/useAgentModelSettings.ts +54 -0
- package/apps/web/src/features/chat/hooks/useClaudeSessions.ts +323 -0
- package/apps/web/src/features/chat/hooks/useCodexSessions.ts +275 -0
- package/apps/web/src/features/chat/hooks/useGeminiSessions.ts +255 -0
- package/apps/web/src/features/chat/hooks/useSessionCommand.ts +66 -0
- package/apps/web/src/features/chat/hooks/useSessionRename.ts +61 -0
- package/apps/web/src/features/chat/hooks/useSessionWorkingDirectories.ts +34 -0
- package/apps/web/src/features/chat/hooks/useUnifiedSessions.ts +156 -0
- package/apps/web/src/features/chat/lib/agentModelOptions.ts +72 -0
- package/apps/web/src/features/chat/ui/AgentModelPicker.tsx +134 -0
- package/apps/web/src/features/chat/ui/AgentSelectModal.tsx +236 -0
- package/apps/web/src/features/chat/ui/ChatInput.tsx +162 -0
- package/apps/web/src/features/chat/ui/ChatMessage.tsx +204 -0
- package/apps/web/src/features/chat/ui/ChatWorkspace.tsx +207 -0
- package/apps/web/src/features/chat/ui/CheckingSkeleton.tsx +44 -0
- package/apps/web/src/features/chat/ui/ClaudeLoginView.tsx +44 -0
- package/apps/web/src/features/chat/ui/PermissionCard.tsx +37 -0
- package/apps/web/src/features/chat/ui/SessionSidebar.tsx +280 -0
- package/apps/web/src/features/chat/ui/__tests__/AgentSelectModal.test.tsx +58 -0
- package/apps/web/src/features/chat/ui/__tests__/ChatInput.test.tsx +134 -0
- package/apps/web/src/features/chat/ui/__tests__/ChatMessage.test.tsx +106 -0
- package/apps/web/src/features/chat/ui/__tests__/ChatWorkspace.test.tsx +66 -0
- package/apps/web/src/features/diff/ui/DiffFileRow.tsx +73 -0
- package/apps/web/src/features/diff/ui/DiffHunk.tsx +61 -0
- package/apps/web/src/features/diff/ui/FileChangeBadge.tsx +23 -0
- package/apps/web/src/features/diff/ui/__tests__/DiffFileRow.test.tsx +40 -0
- package/apps/web/src/features/diff/ui/__tests__/DiffHunk.test.tsx +24 -0
- package/apps/web/src/features/diff/ui/__tests__/FileChangeBadge.test.tsx +16 -0
- package/apps/web/src/features/fs/api/fs.api.ts +14 -0
- package/apps/web/src/features/fs/hooks/useDirBrowser.ts +50 -0
- package/apps/web/src/features/harness/api/__tests__/harness.api.test.ts +73 -0
- package/apps/web/src/features/harness/api/harness.api.ts +46 -0
- package/apps/web/src/features/harness/hooks/__tests__/useHarness.test.ts +65 -0
- package/apps/web/src/features/harness/hooks/useHarness.ts +66 -0
- package/apps/web/src/features/harness/ui/HarnessModal.tsx +171 -0
- package/apps/web/src/features/harness/ui/__tests__/HarnessModal.test.tsx +46 -0
- package/apps/web/src/features/status/ui/AgentStatusModal.tsx +267 -0
- package/apps/web/src/features/status/ui/__tests__/AgentStatusModal.test.tsx +71 -0
- package/apps/web/src/features/tasks/api/__tests__/changelog.api.test.ts +89 -0
- package/apps/web/src/features/tasks/api/__tests__/tasks.api.test.ts +282 -0
- package/apps/web/src/features/tasks/api/changelog.api.ts +52 -0
- package/apps/web/src/features/tasks/api/tasks.api.ts +175 -0
- package/apps/web/src/features/tasks/container/TaskDetailPageContainer.tsx +69 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useChangelogCodeCopy.test.ts +48 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useTaskChangelog.test.ts +48 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useTaskCreate.test.ts +217 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useTaskEdit.test.ts +152 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useTaskExecution.test.ts +143 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useTaskList.test.ts +168 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useTaskNotification.test.ts +125 -0
- package/apps/web/src/features/tasks/hooks/__tests__/useTaskRuns.test.ts +51 -0
- package/apps/web/src/features/tasks/hooks/useChangelogCodeCopy.ts +52 -0
- package/apps/web/src/features/tasks/hooks/useCopyToClipboard.ts +47 -0
- package/apps/web/src/features/tasks/hooks/useTaskChangelog.ts +32 -0
- package/apps/web/src/features/tasks/hooks/useTaskCreate.ts +137 -0
- package/apps/web/src/features/tasks/hooks/useTaskDetail.ts +217 -0
- package/apps/web/src/features/tasks/hooks/useTaskEdit.ts +130 -0
- package/apps/web/src/features/tasks/hooks/useTaskExecution.ts +137 -0
- package/apps/web/src/features/tasks/hooks/useTaskList.ts +159 -0
- package/apps/web/src/features/tasks/hooks/useTaskNotification.ts +80 -0
- package/apps/web/src/features/tasks/hooks/useTaskRuns.ts +32 -0
- package/apps/web/src/features/tasks/ui/AgentOutputPanel.tsx +203 -0
- package/apps/web/src/features/tasks/ui/AgentRoleSelect.tsx +97 -0
- package/apps/web/src/features/tasks/ui/ChangelogPanel.tsx +321 -0
- package/apps/web/src/features/tasks/ui/RunHistoryPanel.tsx +193 -0
- package/apps/web/src/features/tasks/ui/TaskCreateModal.tsx +205 -0
- package/apps/web/src/features/tasks/ui/TaskDetailView.tsx +413 -0
- package/apps/web/src/features/tasks/ui/TaskEditModal.tsx +165 -0
- package/apps/web/src/features/tasks/ui/TaskListModal.tsx +591 -0
- package/apps/web/src/features/tasks/ui/__tests__/AgentRoleSelect.test.tsx +91 -0
- package/apps/web/src/features/tasks/ui/__tests__/ChangelogPanel.test.tsx +94 -0
- package/apps/web/src/features/tasks/ui/__tests__/RunHistoryPanel.test.tsx +71 -0
- package/apps/web/src/features/tasks/ui/__tests__/TaskCreateModal.test.tsx +153 -0
- package/apps/web/src/features/tasks/ui/__tests__/TaskEditModal.test.tsx +75 -0
- package/apps/web/src/features/tasks/ui/__tests__/TaskListModal.test.tsx +243 -0
- package/apps/web/src/hooks/useWorkingDir.ts +28 -0
- package/apps/web/src/lib/__tests__/ansi.test.ts +88 -0
- package/apps/web/src/lib/ansi.ts +105 -0
- package/apps/web/src/lib/constants.ts +4 -0
- package/apps/web/src/lib/quota.ts +22 -0
- package/apps/web/src/lib/theme.tsx +78 -0
- package/apps/web/src/lib/toast.tsx +175 -0
- package/apps/web/src/store/agentStatusStore.ts +38 -0
- package/apps/web/tsconfig.json +18 -0
- package/apps/web/vitest.config.ts +25 -0
- package/apps/web/vitest.setup.ts +10 -0
- package/package.json +85 -0
- package/packages/cli/dist/commands/check.d.ts +1 -0
- package/packages/cli/dist/commands/check.js +89 -0
- package/packages/cli/dist/commands/init.d.ts +5 -0
- package/packages/cli/dist/commands/init.js +183 -0
- package/packages/cli/dist/commands/start.d.ts +4 -0
- package/packages/cli/dist/commands/start.js +188 -0
- package/packages/cli/dist/index.d.ts +2 -0
- package/packages/cli/dist/index.js +71 -0
- package/packages/cli/dist/utils/agent-tools.d.ts +28 -0
- package/packages/cli/dist/utils/agent-tools.js +193 -0
- package/packages/cli/dist/utils/project-init.d.ts +12 -0
- package/packages/cli/dist/utils/project-init.js +258 -0
- package/packages/cli/dist/utils/proxy.d.ts +8 -0
- package/packages/cli/dist/utils/proxy.js +138 -0
- package/packages/cli/package.json +30 -0
- package/packages/cli/src/commands/check.ts +77 -0
- package/packages/cli/src/commands/init.ts +209 -0
- package/packages/cli/src/commands/start.ts +183 -0
- package/packages/cli/src/index.ts +91 -0
- package/packages/cli/src/utils/agent-tools.ts +201 -0
- package/packages/cli/src/utils/project-init.ts +252 -0
- package/packages/cli/src/utils/proxy.ts +123 -0
- package/packages/cli/tsconfig.json +14 -0
- package/packages/eslint-config/base.mjs +31 -0
- package/packages/eslint-config/nest.mjs +55 -0
- package/packages/eslint-config/next.mjs +23 -0
- package/packages/eslint-config/package.json +20 -0
- package/packages/typescript-config/base.json +16 -0
- package/packages/typescript-config/nestjs.json +17 -0
- package/packages/typescript-config/nextjs.json +15 -0
- package/packages/typescript-config/package.json +11 -0
- package/turbo.json +28 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { io, type Socket } from "socket.io-client";
|
|
5
|
+
|
|
6
|
+
import { CODEX_WS_NAMESPACE, SERVER_URL } from "@/lib/constants";
|
|
7
|
+
import {
|
|
8
|
+
createCodexSession as apiCreateSession,
|
|
9
|
+
deleteCodexSession,
|
|
10
|
+
fetchConversations,
|
|
11
|
+
fetchDBSessions,
|
|
12
|
+
saveCodexConversation,
|
|
13
|
+
updateSessionTitle,
|
|
14
|
+
} from "../api/sessions.api";
|
|
15
|
+
import type { DBConversation, SessionInfo } from "../api/sessions.api";
|
|
16
|
+
import type { AgentModelSettings } from "../lib/agentModelOptions";
|
|
17
|
+
import type { ChatMessage, ResultMeta, SessionState } from "./useClaudeSessions";
|
|
18
|
+
|
|
19
|
+
export type { SessionInfo };
|
|
20
|
+
|
|
21
|
+
let msgId = 0;
|
|
22
|
+
const nextId = () => String(++msgId);
|
|
23
|
+
|
|
24
|
+
function toMessages(convos: DBConversation[]): ChatMessage[] {
|
|
25
|
+
return convos.map((c) => ({
|
|
26
|
+
id: c.id,
|
|
27
|
+
role: (c.type === "user_message" ? "user" : "assistant") as ChatMessage["role"],
|
|
28
|
+
content: c.content,
|
|
29
|
+
createdAt: new Date(c.createdAt),
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function useCodexSessions() {
|
|
34
|
+
const socketRef = useRef<Socket | null>(null);
|
|
35
|
+
const [connectionStatus, setConnectionStatus] = useState<"disconnected" | "connecting" | "connected">("disconnected");
|
|
36
|
+
const [sessions, setSessions] = useState<SessionState[]>([]);
|
|
37
|
+
const [selectedSessionId, setSelectedSessionId] = useState<string | null>(null);
|
|
38
|
+
const [error, setError] = useState<string | null>(null);
|
|
39
|
+
|
|
40
|
+
const streamingRef = useRef<Record<string, string>>({});
|
|
41
|
+
const pendingPromptIdRef = useRef<Record<string, string>>({});
|
|
42
|
+
const pendingUserMsgRef = useRef<Record<string, { promptId: string; content: string }>>({});
|
|
43
|
+
const loadingSessionsRef = useRef<Set<string>>(new Set());
|
|
44
|
+
|
|
45
|
+
const loadSessionsFromDB = useCallback(async () => {
|
|
46
|
+
try {
|
|
47
|
+
const dbSessions = await fetchDBSessions("codex");
|
|
48
|
+
setSessions((prev) => {
|
|
49
|
+
const existingIds = new Set(prev.map((s) => s.info.id));
|
|
50
|
+
const newStates: SessionState[] = dbSessions
|
|
51
|
+
.filter((s) => !existingIds.has(s.sessionId))
|
|
52
|
+
.map((s) => ({
|
|
53
|
+
info: { id: s.sessionId, title: !s.title || s.title === "server" ? "Codex" : s.title, createdAt: s.createdAt },
|
|
54
|
+
messages: [],
|
|
55
|
+
streaming: "",
|
|
56
|
+
isWaiting: false,
|
|
57
|
+
messagesLoaded: false,
|
|
58
|
+
agentId: "codex" as const,
|
|
59
|
+
}));
|
|
60
|
+
return [...prev, ...newStates].sort(
|
|
61
|
+
(a, b) => new Date(b.info.createdAt).getTime() - new Date(a.info.createdAt).getTime(),
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
} catch {}
|
|
65
|
+
}, []);
|
|
66
|
+
|
|
67
|
+
const loadConversations = useCallback(async (sessionId: string) => {
|
|
68
|
+
if (loadingSessionsRef.current.has(sessionId)) return;
|
|
69
|
+
loadingSessionsRef.current.add(sessionId);
|
|
70
|
+
try {
|
|
71
|
+
const convos = await fetchConversations(sessionId);
|
|
72
|
+
const codexConvos = convos.filter((c) => c.agentModel === "codex");
|
|
73
|
+
setSessions((prev) =>
|
|
74
|
+
prev.map((s) =>
|
|
75
|
+
s.info.id === sessionId
|
|
76
|
+
? { ...s, messages: toMessages(codexConvos), messagesLoaded: true }
|
|
77
|
+
: s,
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
} catch {
|
|
81
|
+
setSessions((prev) =>
|
|
82
|
+
prev.map((s) => (s.info.id === sessionId ? { ...s, messagesLoaded: true } : s)),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}, []);
|
|
86
|
+
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
const socket = io(`${SERVER_URL}${CODEX_WS_NAMESPACE}`, { transports: ["websocket"] });
|
|
89
|
+
socketRef.current = socket;
|
|
90
|
+
setConnectionStatus("connecting");
|
|
91
|
+
|
|
92
|
+
socket.on("connect", () => {
|
|
93
|
+
setConnectionStatus("connected");
|
|
94
|
+
void loadSessionsFromDB();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
socket.on("disconnect", () => setConnectionStatus("disconnected"));
|
|
98
|
+
|
|
99
|
+
socket.on("session:text", ({ sessionId, text }: { sessionId: string; text: string }) => {
|
|
100
|
+
streamingRef.current[sessionId] = (streamingRef.current[sessionId] ?? "") + text;
|
|
101
|
+
const accumulated = streamingRef.current[sessionId];
|
|
102
|
+
setSessions((prev) =>
|
|
103
|
+
prev.map((s) =>
|
|
104
|
+
s.info.id === sessionId ? { ...s, streaming: accumulated, isWaiting: true } : s,
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
socket.on("session:result", ({ sessionId, isError }: { sessionId: string; isError: boolean }) => {
|
|
110
|
+
const content = (streamingRef.current[sessionId] ?? "").trim();
|
|
111
|
+
streamingRef.current[sessionId] = "";
|
|
112
|
+
|
|
113
|
+
const promptId = pendingPromptIdRef.current[sessionId];
|
|
114
|
+
const userMsg = pendingUserMsgRef.current[sessionId];
|
|
115
|
+
if (promptId) {
|
|
116
|
+
if (userMsg) {
|
|
117
|
+
saveCodexConversation(sessionId, userMsg.promptId, userMsg.content, "user_message");
|
|
118
|
+
delete pendingUserMsgRef.current[sessionId];
|
|
119
|
+
}
|
|
120
|
+
if (content) {
|
|
121
|
+
saveCodexConversation(sessionId, promptId, content, "agent_message");
|
|
122
|
+
}
|
|
123
|
+
delete pendingPromptIdRef.current[sessionId];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const meta: ResultMeta = { result: "", isError, durationMs: 0, costUsd: 0 };
|
|
127
|
+
|
|
128
|
+
setSessions((prev) =>
|
|
129
|
+
prev.map((s) => {
|
|
130
|
+
if (s.info.id !== sessionId) return s;
|
|
131
|
+
const newMessages = [...s.messages];
|
|
132
|
+
if (content) {
|
|
133
|
+
newMessages.push({
|
|
134
|
+
id: nextId(),
|
|
135
|
+
role: "assistant",
|
|
136
|
+
content,
|
|
137
|
+
meta,
|
|
138
|
+
createdAt: new Date(),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return { ...s, messages: newMessages, streaming: "", isWaiting: false };
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
socket.on("session:exit", ({ sessionId }: { sessionId: string }) => {
|
|
147
|
+
streamingRef.current[sessionId] = "";
|
|
148
|
+
setSessions((prev) =>
|
|
149
|
+
prev.map((s) => (s.info.id === sessionId ? { ...s, streaming: "", isWaiting: false } : s)),
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
socket.on("session:replaced", ({ oldSessionId, newSessionId }: { oldSessionId: string; newSessionId: string }) => {
|
|
154
|
+
if (streamingRef.current[oldSessionId] !== undefined) {
|
|
155
|
+
streamingRef.current[newSessionId] = streamingRef.current[oldSessionId];
|
|
156
|
+
delete streamingRef.current[oldSessionId];
|
|
157
|
+
}
|
|
158
|
+
if (pendingPromptIdRef.current[oldSessionId] !== undefined) {
|
|
159
|
+
pendingPromptIdRef.current[newSessionId] = pendingPromptIdRef.current[oldSessionId];
|
|
160
|
+
delete pendingPromptIdRef.current[oldSessionId];
|
|
161
|
+
}
|
|
162
|
+
if (pendingUserMsgRef.current[oldSessionId] !== undefined) {
|
|
163
|
+
pendingUserMsgRef.current[newSessionId] = pendingUserMsgRef.current[oldSessionId];
|
|
164
|
+
delete pendingUserMsgRef.current[oldSessionId];
|
|
165
|
+
}
|
|
166
|
+
setSessions((prev) =>
|
|
167
|
+
prev.map((s) =>
|
|
168
|
+
s.info.id === oldSessionId ? { ...s, info: { ...s.info, id: newSessionId } } : s,
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
setSelectedSessionId((prev) => (prev === oldSessionId ? newSessionId : prev));
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
socket.on("error", ({ message }: { message: string }) => setError(message));
|
|
175
|
+
|
|
176
|
+
return () => { socket.disconnect(); };
|
|
177
|
+
}, [loadSessionsFromDB]);
|
|
178
|
+
|
|
179
|
+
useEffect(() => {
|
|
180
|
+
if (!selectedSessionId) return;
|
|
181
|
+
const session = sessions.find((s) => s.info.id === selectedSessionId);
|
|
182
|
+
if (!session || session.messagesLoaded) return;
|
|
183
|
+
void loadConversations(selectedSessionId);
|
|
184
|
+
}, [selectedSessionId, sessions, loadConversations]);
|
|
185
|
+
|
|
186
|
+
const renameSession = useCallback((sessionId: string, newTitle: string) => {
|
|
187
|
+
setSessions((prev) =>
|
|
188
|
+
prev.map((s) =>
|
|
189
|
+
s.info.id === sessionId ? { ...s, info: { ...s.info, title: newTitle } } : s,
|
|
190
|
+
),
|
|
191
|
+
);
|
|
192
|
+
void updateSessionTitle(sessionId, newTitle).catch(() => undefined);
|
|
193
|
+
}, []);
|
|
194
|
+
|
|
195
|
+
const createSession = useCallback(async (
|
|
196
|
+
workingDirectory?: string,
|
|
197
|
+
modelSettings?: AgentModelSettings,
|
|
198
|
+
): Promise<string | null> => {
|
|
199
|
+
setError(null);
|
|
200
|
+
try {
|
|
201
|
+
const raw = await apiCreateSession({ workingDirectory, ...modelSettings });
|
|
202
|
+
const title = raw.workingDirectory
|
|
203
|
+
? raw.workingDirectory.replace(/[/\\]+$/, "").split(/[/\\]/).filter(Boolean).at(-1) ?? "Codex"
|
|
204
|
+
: "Codex";
|
|
205
|
+
|
|
206
|
+
const newState: SessionState = {
|
|
207
|
+
info: { ...raw, title },
|
|
208
|
+
messages: [],
|
|
209
|
+
streaming: "",
|
|
210
|
+
isWaiting: false,
|
|
211
|
+
messagesLoaded: true,
|
|
212
|
+
agentId: "codex",
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
setSessions((prev) => [newState, ...prev]);
|
|
216
|
+
setSelectedSessionId(raw.id);
|
|
217
|
+
return raw.id;
|
|
218
|
+
} catch (e) {
|
|
219
|
+
setError(e instanceof Error ? e.message : "Codex 세션 생성 실패");
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
}, []);
|
|
223
|
+
|
|
224
|
+
const sendMessage = useCallback((sessionId: string, text: string, modelSettings?: AgentModelSettings) => {
|
|
225
|
+
const promptId = crypto.randomUUID();
|
|
226
|
+
pendingPromptIdRef.current[sessionId] = promptId;
|
|
227
|
+
pendingUserMsgRef.current[sessionId] = { promptId, content: text };
|
|
228
|
+
|
|
229
|
+
setSessions((prev) =>
|
|
230
|
+
prev.map((s) =>
|
|
231
|
+
s.info.id === sessionId
|
|
232
|
+
? {
|
|
233
|
+
...s,
|
|
234
|
+
messages: [
|
|
235
|
+
...s.messages,
|
|
236
|
+
{ id: nextId(), role: "user" as const, content: text, createdAt: new Date() },
|
|
237
|
+
],
|
|
238
|
+
isWaiting: true,
|
|
239
|
+
}
|
|
240
|
+
: s,
|
|
241
|
+
),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
socketRef.current?.emit("session:message", {
|
|
245
|
+
sessionId,
|
|
246
|
+
input: text,
|
|
247
|
+
...modelSettings,
|
|
248
|
+
});
|
|
249
|
+
}, []);
|
|
250
|
+
|
|
251
|
+
const terminateSession = useCallback(async (sessionId: string) => {
|
|
252
|
+
try {
|
|
253
|
+
await deleteCodexSession(sessionId);
|
|
254
|
+
setSessions((prev) => prev.filter((s) => s.info.id !== sessionId));
|
|
255
|
+
setSelectedSessionId((prev) => (prev === sessionId ? null : prev));
|
|
256
|
+
} catch (err) {
|
|
257
|
+
setError(err instanceof Error ? err.message : "세션 삭제 실패");
|
|
258
|
+
}
|
|
259
|
+
}, []);
|
|
260
|
+
|
|
261
|
+
const selectedSession = sessions.find((s) => s.info.id === selectedSessionId) ?? null;
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
connectionStatus,
|
|
265
|
+
sessions,
|
|
266
|
+
selectedSession,
|
|
267
|
+
selectedSessionId,
|
|
268
|
+
error,
|
|
269
|
+
createSession,
|
|
270
|
+
selectSession: setSelectedSessionId,
|
|
271
|
+
sendMessage,
|
|
272
|
+
terminateSession,
|
|
273
|
+
renameSession,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { io, type Socket } from "socket.io-client";
|
|
5
|
+
|
|
6
|
+
import { GEMINI_WS_NAMESPACE, SERVER_URL } from "@/lib/constants";
|
|
7
|
+
import {
|
|
8
|
+
createGeminiSession as apiCreateSession,
|
|
9
|
+
deleteGeminiSession,
|
|
10
|
+
fetchConversations,
|
|
11
|
+
fetchDBSessions,
|
|
12
|
+
saveGeminiConversation,
|
|
13
|
+
updateSessionTitle,
|
|
14
|
+
} from "../api/sessions.api";
|
|
15
|
+
import type { DBConversation, SessionInfo } from "../api/sessions.api";
|
|
16
|
+
import type { ChatMessage, ResultMeta, SessionState, ToolUseBlock } from "./useClaudeSessions";
|
|
17
|
+
|
|
18
|
+
export type { SessionInfo };
|
|
19
|
+
|
|
20
|
+
// ─── 헬퍼 ────────────────────────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
let msgId = 0;
|
|
23
|
+
const nextId = () => String(++msgId);
|
|
24
|
+
|
|
25
|
+
function toMessages(convos: DBConversation[]): ChatMessage[] {
|
|
26
|
+
return convos.map((c) => ({
|
|
27
|
+
id: c.id,
|
|
28
|
+
role: (c.type === "user_message" ? "user" : "assistant") as ChatMessage["role"],
|
|
29
|
+
content: c.content,
|
|
30
|
+
createdAt: new Date(c.createdAt),
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ─── Hook ────────────────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
export function useGeminiSessions() {
|
|
37
|
+
const socketRef = useRef<Socket | null>(null);
|
|
38
|
+
const [connectionStatus, setConnectionStatus] = useState<"disconnected" | "connecting" | "connected">("disconnected");
|
|
39
|
+
const [sessions, setSessions] = useState<SessionState[]>([]);
|
|
40
|
+
const [selectedSessionId, setSelectedSessionId] = useState<string | null>(null);
|
|
41
|
+
const [error, setError] = useState<string | null>(null);
|
|
42
|
+
|
|
43
|
+
const streamingRef = useRef<Record<string, string>>({});
|
|
44
|
+
const pendingPromptIdRef = useRef<Record<string, string>>({});
|
|
45
|
+
const loadingSessionsRef = useRef<Set<string>>(new Set());
|
|
46
|
+
|
|
47
|
+
// ─── DB 세션 로드 ──────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
const loadSessionsFromDB = useCallback(async () => {
|
|
50
|
+
try {
|
|
51
|
+
const dbSessions = await fetchDBSessions('gemini');
|
|
52
|
+
setSessions((prev) => {
|
|
53
|
+
const existingIds = new Set(prev.map((s) => s.info.id));
|
|
54
|
+
// Gemini 세션만 필터링 (agentModel='gemini' 인 conversation이 있는 sessionId)
|
|
55
|
+
// 단순화: DB sessions 전체를 불러오되 이미 있는 건 스킵
|
|
56
|
+
const newStates: SessionState[] = dbSessions
|
|
57
|
+
.filter((s) => !existingIds.has(s.sessionId))
|
|
58
|
+
.map((s) => ({
|
|
59
|
+
info: { id: s.sessionId, title: !s.title || s.title === "server" ? "Gemini" : s.title, createdAt: s.createdAt },
|
|
60
|
+
messages: [],
|
|
61
|
+
streaming: "",
|
|
62
|
+
isWaiting: false,
|
|
63
|
+
messagesLoaded: false,
|
|
64
|
+
agentId: "gemini" as const,
|
|
65
|
+
}));
|
|
66
|
+
return [...prev, ...newStates].sort(
|
|
67
|
+
(a, b) => new Date(b.info.createdAt).getTime() - new Date(a.info.createdAt).getTime(),
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
} catch {}
|
|
71
|
+
}, []);
|
|
72
|
+
|
|
73
|
+
// ─── 대화 기록 로드 ────────────────────────────────────────────────────────
|
|
74
|
+
|
|
75
|
+
const loadConversations = useCallback(async (sessionId: string) => {
|
|
76
|
+
if (loadingSessionsRef.current.has(sessionId)) return;
|
|
77
|
+
loadingSessionsRef.current.add(sessionId);
|
|
78
|
+
try {
|
|
79
|
+
const convos = await fetchConversations(sessionId);
|
|
80
|
+
// gemini 대화만 필터
|
|
81
|
+
const geminiConvos = convos.filter((c) => c.agentModel === "gemini");
|
|
82
|
+
setSessions((prev) =>
|
|
83
|
+
prev.map((s) =>
|
|
84
|
+
s.info.id === sessionId
|
|
85
|
+
? { ...s, messages: toMessages(geminiConvos), messagesLoaded: true }
|
|
86
|
+
: s,
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
} catch {
|
|
90
|
+
setSessions((prev) =>
|
|
91
|
+
prev.map((s) => (s.info.id === sessionId ? { ...s, messagesLoaded: true } : s)),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
96
|
+
// ─── WebSocket ─────────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
const socket = io(`${SERVER_URL}${GEMINI_WS_NAMESPACE}`, { transports: ["websocket"] });
|
|
100
|
+
socketRef.current = socket;
|
|
101
|
+
setConnectionStatus("connecting");
|
|
102
|
+
|
|
103
|
+
socket.on("connect", () => {
|
|
104
|
+
setConnectionStatus("connected");
|
|
105
|
+
void loadSessionsFromDB();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
socket.on("disconnect", () => setConnectionStatus("disconnected"));
|
|
109
|
+
|
|
110
|
+
socket.on("session:text", ({ sessionId, text }: { sessionId: string; text: string }) => {
|
|
111
|
+
streamingRef.current[sessionId] = (streamingRef.current[sessionId] ?? "") + text;
|
|
112
|
+
const accumulated = streamingRef.current[sessionId];
|
|
113
|
+
setSessions((prev) =>
|
|
114
|
+
prev.map((s) =>
|
|
115
|
+
s.info.id === sessionId ? { ...s, streaming: accumulated, isWaiting: true } : s,
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
socket.on("session:result", ({ sessionId, isError }: { sessionId: string; isError: boolean }) => {
|
|
121
|
+
const content = (streamingRef.current[sessionId] ?? "").trim();
|
|
122
|
+
streamingRef.current[sessionId] = "";
|
|
123
|
+
|
|
124
|
+
const promptId = pendingPromptIdRef.current[sessionId];
|
|
125
|
+
if (promptId && content) {
|
|
126
|
+
saveGeminiConversation(sessionId, promptId, content, "agent_message");
|
|
127
|
+
delete pendingPromptIdRef.current[sessionId];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const meta: ResultMeta = { result: "", isError, durationMs: 0, costUsd: 0 };
|
|
131
|
+
|
|
132
|
+
setSessions((prev) =>
|
|
133
|
+
prev.map((s) => {
|
|
134
|
+
if (s.info.id !== sessionId) return s;
|
|
135
|
+
const newMessages = [...s.messages];
|
|
136
|
+
if (content) {
|
|
137
|
+
newMessages.push({
|
|
138
|
+
id: nextId(),
|
|
139
|
+
role: "assistant",
|
|
140
|
+
content,
|
|
141
|
+
meta,
|
|
142
|
+
createdAt: new Date(),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return { ...s, messages: newMessages, streaming: "", isWaiting: false };
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
socket.on("session:exit", ({ sessionId }: { sessionId: string }) => {
|
|
151
|
+
streamingRef.current[sessionId] = "";
|
|
152
|
+
setSessions((prev) =>
|
|
153
|
+
prev.map((s) => (s.info.id === sessionId ? { ...s, streaming: "", isWaiting: false } : s)),
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
socket.on("error", ({ message }: { message: string }) => setError(message));
|
|
158
|
+
|
|
159
|
+
return () => { socket.disconnect(); };
|
|
160
|
+
}, [loadSessionsFromDB]);
|
|
161
|
+
|
|
162
|
+
// ─── 세션 선택 시 대화 기록 로드 ─────────────────────────────────────────
|
|
163
|
+
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (!selectedSessionId) return;
|
|
166
|
+
const session = sessions.find((s) => s.info.id === selectedSessionId);
|
|
167
|
+
if (!session || session.messagesLoaded) return;
|
|
168
|
+
void loadConversations(selectedSessionId);
|
|
169
|
+
}, [selectedSessionId, sessions, loadConversations]);
|
|
170
|
+
|
|
171
|
+
// ─── 공개 API ─────────────────────────────────────────────────────────────
|
|
172
|
+
|
|
173
|
+
const renameSession = useCallback((sessionId: string, newTitle: string) => {
|
|
174
|
+
setSessions((prev) =>
|
|
175
|
+
prev.map((s) =>
|
|
176
|
+
s.info.id === sessionId ? { ...s, info: { ...s.info, title: newTitle } } : s,
|
|
177
|
+
),
|
|
178
|
+
);
|
|
179
|
+
void updateSessionTitle(sessionId, newTitle).catch(() => undefined);
|
|
180
|
+
}, []);
|
|
181
|
+
|
|
182
|
+
const createSession = useCallback(async (workingDirectory?: string): Promise<string | null> => {
|
|
183
|
+
setError(null);
|
|
184
|
+
try {
|
|
185
|
+
const raw = await apiCreateSession(workingDirectory);
|
|
186
|
+
const title = raw.workingDirectory
|
|
187
|
+
? raw.workingDirectory.replace(/[/\\]+$/, "").split(/[/\\]/).filter(Boolean).at(-1) ?? "Gemini"
|
|
188
|
+
: "Gemini";
|
|
189
|
+
|
|
190
|
+
const newState: SessionState = {
|
|
191
|
+
info: { ...raw, title },
|
|
192
|
+
messages: [],
|
|
193
|
+
streaming: "",
|
|
194
|
+
isWaiting: false,
|
|
195
|
+
messagesLoaded: true,
|
|
196
|
+
agentId: "gemini",
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
setSessions((prev) => [newState, ...prev]);
|
|
200
|
+
setSelectedSessionId(raw.id);
|
|
201
|
+
return raw.id;
|
|
202
|
+
} catch (e) {
|
|
203
|
+
setError(e instanceof Error ? e.message : "Gemini 세션 생성 실패");
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
}, []);
|
|
207
|
+
|
|
208
|
+
const sendMessage = useCallback((sessionId: string, text: string) => {
|
|
209
|
+
const promptId = crypto.randomUUID();
|
|
210
|
+
pendingPromptIdRef.current[sessionId] = promptId;
|
|
211
|
+
|
|
212
|
+
setSessions((prev) =>
|
|
213
|
+
prev.map((s) =>
|
|
214
|
+
s.info.id === sessionId
|
|
215
|
+
? {
|
|
216
|
+
...s,
|
|
217
|
+
messages: [
|
|
218
|
+
...s.messages,
|
|
219
|
+
{ id: nextId(), role: "user" as const, content: text, createdAt: new Date() },
|
|
220
|
+
],
|
|
221
|
+
isWaiting: true,
|
|
222
|
+
}
|
|
223
|
+
: s,
|
|
224
|
+
),
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
saveGeminiConversation(sessionId, promptId, text, "user_message");
|
|
228
|
+
socketRef.current?.emit("session:message", { sessionId, input: text });
|
|
229
|
+
}, []);
|
|
230
|
+
|
|
231
|
+
const terminateSession = useCallback(async (sessionId: string) => {
|
|
232
|
+
try {
|
|
233
|
+
await deleteGeminiSession(sessionId);
|
|
234
|
+
setSessions((prev) => prev.filter((s) => s.info.id !== sessionId));
|
|
235
|
+
setSelectedSessionId((prev) => (prev === sessionId ? null : prev));
|
|
236
|
+
} catch (err) {
|
|
237
|
+
setError(err instanceof Error ? err.message : "세션 삭제 실패");
|
|
238
|
+
}
|
|
239
|
+
}, []);
|
|
240
|
+
|
|
241
|
+
const selectedSession = sessions.find((s) => s.info.id === selectedSessionId) ?? null;
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
connectionStatus,
|
|
245
|
+
sessions,
|
|
246
|
+
selectedSession,
|
|
247
|
+
selectedSessionId,
|
|
248
|
+
error,
|
|
249
|
+
createSession,
|
|
250
|
+
selectSession: setSelectedSessionId,
|
|
251
|
+
sendMessage,
|
|
252
|
+
terminateSession,
|
|
253
|
+
renameSession,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback } from "react";
|
|
4
|
+
|
|
5
|
+
import { getClaudeStatus } from "@/features/auth/api/auth.api";
|
|
6
|
+
import type { AgentModelSettingsByAgent } from "../lib/agentModelOptions";
|
|
7
|
+
import type { ChatMessage, SessionState } from "./useClaudeSessions";
|
|
8
|
+
|
|
9
|
+
interface UseSessionCommandParams {
|
|
10
|
+
selectedSession: SessionState | null;
|
|
11
|
+
selectedSessionId: string | null;
|
|
12
|
+
sendMessage: (sessionId: string, text: string, modelSettingsByAgent?: AgentModelSettingsByAgent) => void;
|
|
13
|
+
modelSettingsByAgent?: AgentModelSettingsByAgent;
|
|
14
|
+
injectClaudeMessage: (sessionId: string, message: Omit<ChatMessage, "id" | "createdAt">) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function useSessionCommand({
|
|
18
|
+
selectedSession,
|
|
19
|
+
selectedSessionId,
|
|
20
|
+
sendMessage,
|
|
21
|
+
modelSettingsByAgent,
|
|
22
|
+
injectClaudeMessage,
|
|
23
|
+
}: UseSessionCommandParams) {
|
|
24
|
+
return useCallback(
|
|
25
|
+
async (text: string) => {
|
|
26
|
+
const trimmed = text.trim();
|
|
27
|
+
if (!selectedSessionId || !trimmed) return;
|
|
28
|
+
|
|
29
|
+
if (trimmed === "/status" && selectedSession?.agentId === "claude") {
|
|
30
|
+
injectClaudeMessage(selectedSessionId, { role: "user", content: "/status" });
|
|
31
|
+
try {
|
|
32
|
+
const data = await getClaudeStatus();
|
|
33
|
+
const auth = data.auth;
|
|
34
|
+
const authLines: string[] = [];
|
|
35
|
+
|
|
36
|
+
if (auth.loggedIn) {
|
|
37
|
+
authLines.push(`인증 ✅ 로그인됨 (${auth.authMethod})`);
|
|
38
|
+
if (auth.email) authLines.push(`계정 ${auth.email}`);
|
|
39
|
+
if (auth.orgName) authLines.push(`조직 ${auth.orgName}`);
|
|
40
|
+
if (auth.subscriptionType) authLines.push(`구독 ${auth.subscriptionType}`);
|
|
41
|
+
} else {
|
|
42
|
+
authLines.push("인증 ❌ 로그아웃 상태");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const lines = [
|
|
46
|
+
`Claude Code ${data.version}`,
|
|
47
|
+
`플랫폼 ${data.platform}`,
|
|
48
|
+
...authLines,
|
|
49
|
+
`활성 세션 ${data.activeSessions}개`,
|
|
50
|
+
].join("\n");
|
|
51
|
+
|
|
52
|
+
injectClaudeMessage(selectedSessionId, { role: "system", content: lines });
|
|
53
|
+
} catch {
|
|
54
|
+
injectClaudeMessage(selectedSessionId, {
|
|
55
|
+
role: "system",
|
|
56
|
+
content: "❌ 상태 조회 실패 — 서버 연결을 확인하세요.",
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
sendMessage(selectedSessionId, trimmed, modelSettingsByAgent);
|
|
63
|
+
},
|
|
64
|
+
[injectClaudeMessage, modelSettingsByAgent, selectedSession, selectedSessionId, sendMessage],
|
|
65
|
+
);
|
|
66
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
|
|
5
|
+
export function useSessionRename(onRename: (sessionId: string, newTitle: string) => void) {
|
|
6
|
+
const [menuOpenId, setMenuOpenId] = useState<string | null>(null);
|
|
7
|
+
const [renamingId, setRenamingId] = useState<string | null>(null);
|
|
8
|
+
const [renameValue, setRenameValue] = useState("");
|
|
9
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
10
|
+
const skipNextBlurRef = useRef(false);
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (!menuOpenId) return;
|
|
14
|
+
|
|
15
|
+
const handler = (event: MouseEvent) => {
|
|
16
|
+
if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
|
|
17
|
+
setMenuOpenId(null);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
document.addEventListener("mousedown", handler);
|
|
22
|
+
return () => document.removeEventListener("mousedown", handler);
|
|
23
|
+
}, [menuOpenId]);
|
|
24
|
+
|
|
25
|
+
const startRename = useCallback((sessionId: string, currentTitle: string) => {
|
|
26
|
+
setMenuOpenId(null);
|
|
27
|
+
setRenamingId(sessionId);
|
|
28
|
+
setRenameValue(currentTitle);
|
|
29
|
+
}, []);
|
|
30
|
+
|
|
31
|
+
const confirmRename = useCallback(() => {
|
|
32
|
+
if (skipNextBlurRef.current) {
|
|
33
|
+
skipNextBlurRef.current = false;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (renamingId && renameValue.trim()) {
|
|
38
|
+
onRename(renamingId, renameValue.trim());
|
|
39
|
+
}
|
|
40
|
+
setRenamingId(null);
|
|
41
|
+
setRenameValue("");
|
|
42
|
+
}, [onRename, renameValue, renamingId]);
|
|
43
|
+
|
|
44
|
+
const cancelRename = useCallback(() => {
|
|
45
|
+
skipNextBlurRef.current = true;
|
|
46
|
+
setRenamingId(null);
|
|
47
|
+
setRenameValue("");
|
|
48
|
+
}, []);
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
menuOpenId,
|
|
52
|
+
setMenuOpenId,
|
|
53
|
+
renamingId,
|
|
54
|
+
renameValue,
|
|
55
|
+
setRenameValue,
|
|
56
|
+
menuRef,
|
|
57
|
+
startRename,
|
|
58
|
+
confirmRename,
|
|
59
|
+
cancelRename,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
|
|
5
|
+
export function useSessionWorkingDirectories(selectedSessionId: string | null) {
|
|
6
|
+
const [sessionDirs, setSessionDirs] = useState<Record<string, string>>({});
|
|
7
|
+
const [currentDir, setCurrentDir] = useState("");
|
|
8
|
+
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
setCurrentDir(selectedSessionId ? (sessionDirs[selectedSessionId] ?? "") : "");
|
|
11
|
+
}, [selectedSessionId, sessionDirs]);
|
|
12
|
+
|
|
13
|
+
const handleDirChange = useCallback(
|
|
14
|
+
(path: string) => {
|
|
15
|
+
setCurrentDir(path);
|
|
16
|
+
if (selectedSessionId) {
|
|
17
|
+
setSessionDirs((prev) => ({ ...prev, [selectedSessionId]: path }));
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
[selectedSessionId],
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const assignDirectoryToSession = useCallback((sessionId: string, path: string) => {
|
|
24
|
+
setSessionDirs((prev) => ({ ...prev, [sessionId]: path }));
|
|
25
|
+
setCurrentDir(path);
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
sessionDirs,
|
|
30
|
+
currentDir,
|
|
31
|
+
handleDirChange,
|
|
32
|
+
assignDirectoryToSession,
|
|
33
|
+
};
|
|
34
|
+
}
|