@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,67 @@
|
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
import * as os from 'os';
|
|
3
|
+
|
|
4
|
+
import { Injectable } from '@nestjs/common';
|
|
5
|
+
|
|
6
|
+
import { ClaudeAuthManager } from './claude-auth.manager';
|
|
7
|
+
import { ClaudePtyManager } from './claude-pty.manager';
|
|
8
|
+
import type { ClaudeCreateSessionDto as CreateSessionDto } from './dto/create-session.dto';
|
|
9
|
+
import type { SessionInfo } from './interfaces/claude-session.interface';
|
|
10
|
+
|
|
11
|
+
export interface ClaudeStatus {
|
|
12
|
+
version: string;
|
|
13
|
+
auth: {
|
|
14
|
+
loggedIn: boolean;
|
|
15
|
+
authMethod: string;
|
|
16
|
+
apiProvider: string;
|
|
17
|
+
email?: string;
|
|
18
|
+
orgName?: string;
|
|
19
|
+
subscriptionType?: string;
|
|
20
|
+
};
|
|
21
|
+
activeSessions: number;
|
|
22
|
+
platform: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@Injectable()
|
|
26
|
+
export class ClaudeService {
|
|
27
|
+
constructor(
|
|
28
|
+
private readonly ptyManager: ClaudePtyManager,
|
|
29
|
+
private readonly authManager: ClaudeAuthManager,
|
|
30
|
+
) {}
|
|
31
|
+
|
|
32
|
+
createSession(dto: CreateSessionDto): SessionInfo {
|
|
33
|
+
return this.ptyManager.createSession(dto.workingDirectory, dto.model, dto.reasoning);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
terminateSession(sessionId: string): void {
|
|
37
|
+
this.ptyManager.terminateSession(sessionId);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
sendMessage(sessionId: string, message: string): void {
|
|
41
|
+
this.ptyManager.sendMessage(sessionId, message);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getSession(sessionId: string): SessionInfo {
|
|
45
|
+
return this.ptyManager.getSessionInfo(sessionId);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
listSessions(): SessionInfo[] {
|
|
49
|
+
return this.ptyManager.listSessions();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getStatus(): Promise<ClaudeStatus> {
|
|
53
|
+
let version = 'unknown';
|
|
54
|
+
try {
|
|
55
|
+
version = execSync('claude --version', { encoding: 'utf8', timeout: 3000 }).trim();
|
|
56
|
+
} catch {}
|
|
57
|
+
|
|
58
|
+
const auth = await this.authManager.getAuthStatus();
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
version,
|
|
62
|
+
auth,
|
|
63
|
+
activeSessions: this.ptyManager.listSessions().length,
|
|
64
|
+
platform: `${os.platform()} ${os.arch()}`,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ApiPropertyOptional } from '@nestjs/swagger';
|
|
2
|
+
import { IsObject, IsOptional, IsString } from 'class-validator';
|
|
3
|
+
|
|
4
|
+
export class ClaudeCreateSessionDto {
|
|
5
|
+
@ApiPropertyOptional({ example: '/Users/me/my-project', description: '세션 작업 디렉토리' })
|
|
6
|
+
@IsOptional()
|
|
7
|
+
@IsString()
|
|
8
|
+
workingDirectory?: string;
|
|
9
|
+
|
|
10
|
+
@ApiPropertyOptional({ example: 'sonnet', description: 'Claude CLI 모델명 또는 별칭' })
|
|
11
|
+
@IsOptional()
|
|
12
|
+
@IsString()
|
|
13
|
+
model?: string;
|
|
14
|
+
|
|
15
|
+
@ApiPropertyOptional({ example: 'high', description: 'Claude reasoning effort' })
|
|
16
|
+
@IsOptional()
|
|
17
|
+
@IsString()
|
|
18
|
+
reasoning?: string;
|
|
19
|
+
|
|
20
|
+
@ApiPropertyOptional({ example: { NODE_ENV: 'development' }, description: '추가 환경 변수' })
|
|
21
|
+
@IsOptional()
|
|
22
|
+
@IsObject()
|
|
23
|
+
env?: Record<string, string>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type SessionStatus = 'idle' | 'processing' | 'terminated';
|
|
2
|
+
|
|
3
|
+
export interface ClaudeSession {
|
|
4
|
+
id: string;
|
|
5
|
+
/** Claude CLI가 발급한 session_id — --resume에 사용 */
|
|
6
|
+
claudeSessionId: string | null;
|
|
7
|
+
status: SessionStatus;
|
|
8
|
+
workingDirectory: string;
|
|
9
|
+
model: string | null;
|
|
10
|
+
reasoning: string | null;
|
|
11
|
+
createdAt: Date;
|
|
12
|
+
lastActivity: Date;
|
|
13
|
+
/** 첫 메시지 전송 전까지 false — DB에 아직 적재되지 않은 상태 */
|
|
14
|
+
persisted: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SessionInfo {
|
|
18
|
+
id: string;
|
|
19
|
+
claudeSessionId: string | null;
|
|
20
|
+
status: SessionStatus;
|
|
21
|
+
workingDirectory: string;
|
|
22
|
+
model: string | null;
|
|
23
|
+
reasoning: string | null;
|
|
24
|
+
createdAt: Date;
|
|
25
|
+
lastActivity: Date;
|
|
26
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Claude CLI --output-format stream-json 이벤트 타입 */
|
|
2
|
+
|
|
3
|
+
export interface TextDeltaEvent {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
text: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ToolUseEvent {
|
|
9
|
+
sessionId: string;
|
|
10
|
+
tool: string;
|
|
11
|
+
input: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ResultEvent {
|
|
15
|
+
sessionId: string;
|
|
16
|
+
result: string;
|
|
17
|
+
isError: boolean;
|
|
18
|
+
durationMs: number;
|
|
19
|
+
costUsd: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SessionExitEvent {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
exitCode: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ─── Claude CLI NDJSON raw 타입 ──────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
export interface ClaudeInitEvent {
|
|
30
|
+
type: 'system';
|
|
31
|
+
subtype: 'init';
|
|
32
|
+
session_id: string;
|
|
33
|
+
model: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ClaudeAssistantEvent {
|
|
37
|
+
type: 'assistant';
|
|
38
|
+
session_id: string;
|
|
39
|
+
message: {
|
|
40
|
+
content: Array<
|
|
41
|
+
| { type: 'text'; text: string }
|
|
42
|
+
| { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> }
|
|
43
|
+
>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ClaudeResultEvent {
|
|
48
|
+
type: 'result';
|
|
49
|
+
subtype: 'success' | 'error';
|
|
50
|
+
session_id: string;
|
|
51
|
+
result: string;
|
|
52
|
+
is_error: boolean;
|
|
53
|
+
duration_ms: number;
|
|
54
|
+
total_cost_usd: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type ClaudeStreamEvent =
|
|
58
|
+
| ClaudeInitEvent
|
|
59
|
+
| ClaudeAssistantEvent
|
|
60
|
+
| ClaudeResultEvent
|
|
61
|
+
| { type: string };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { execFileSync } from 'child_process';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
|
|
6
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
7
|
+
import * as pty from 'node-pty';
|
|
8
|
+
|
|
9
|
+
const IS_WIN = process.platform === 'win32';
|
|
10
|
+
|
|
11
|
+
export interface CodexAuthStatus {
|
|
12
|
+
installed: boolean;
|
|
13
|
+
loggedIn: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const KEY_PATH = path.join(os.homedir(), '.ji', 'codex-key');
|
|
17
|
+
const ANSI_STRIP = /\x1b\[[0-9;]*[a-zA-Z]/g;
|
|
18
|
+
|
|
19
|
+
@Injectable()
|
|
20
|
+
export class CodexAuthManager {
|
|
21
|
+
private readonly logger = new Logger(CodexAuthManager.name);
|
|
22
|
+
private readonly loginProcesses = new Map<string, pty.IPty>();
|
|
23
|
+
|
|
24
|
+
getAuthStatus(): CodexAuthStatus {
|
|
25
|
+
const installed = this.isInstalled();
|
|
26
|
+
if (!installed) return { installed: false, loggedIn: false };
|
|
27
|
+
|
|
28
|
+
const loggedIn = this.isLoggedInViaDevice() || !!(this.readStoredApiKey() || process.env.OPENAI_API_KEY);
|
|
29
|
+
return { installed, loggedIn };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
saveApiKey(apiKey: string): void {
|
|
33
|
+
const dir = path.dirname(KEY_PATH);
|
|
34
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
35
|
+
fs.writeFileSync(KEY_PATH, apiKey, { mode: 0o600 });
|
|
36
|
+
this.logger.log('Saved Codex API key');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
startLogin(
|
|
40
|
+
clientId: string,
|
|
41
|
+
onOutput: (text: string) => void,
|
|
42
|
+
onDone: (success: boolean) => void,
|
|
43
|
+
): void {
|
|
44
|
+
this.cancelLogin(clientId);
|
|
45
|
+
|
|
46
|
+
const proc = pty.spawn(IS_WIN ? 'codex.cmd' : 'codex', ['login', '--device-auth'], {
|
|
47
|
+
name: 'xterm-256color',
|
|
48
|
+
cols: 120,
|
|
49
|
+
rows: 30,
|
|
50
|
+
env: process.env as Record<string, string>,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
this.loginProcesses.set(clientId, proc);
|
|
54
|
+
this.logger.log(`Started Codex device login for client ${clientId} (pid: ${proc.pid})`);
|
|
55
|
+
|
|
56
|
+
proc.onData((data) => onOutput(data.replace(ANSI_STRIP, '')));
|
|
57
|
+
|
|
58
|
+
proc.onExit(({ exitCode }) => {
|
|
59
|
+
this.loginProcesses.delete(clientId);
|
|
60
|
+
this.logger.log(`Codex login for ${clientId} exited with code ${exitCode}`);
|
|
61
|
+
onDone(exitCode === 0);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
cancelLogin(clientId: string): void {
|
|
66
|
+
const proc = this.loginProcesses.get(clientId);
|
|
67
|
+
if (proc) {
|
|
68
|
+
try { proc.kill(); } catch { /* ignore */ }
|
|
69
|
+
this.loginProcesses.delete(clientId);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
getEnvForCodex(): NodeJS.ProcessEnv {
|
|
74
|
+
const apiKey = this.readStoredApiKey() ?? process.env.OPENAI_API_KEY;
|
|
75
|
+
return apiKey ? { ...process.env, OPENAI_API_KEY: apiKey } : process.env;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private isInstalled(): boolean {
|
|
79
|
+
try {
|
|
80
|
+
execFileSync('codex', ['--version'], { stdio: 'ignore', shell: true });
|
|
81
|
+
return true;
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private isLoggedInViaDevice(): boolean {
|
|
88
|
+
try {
|
|
89
|
+
execFileSync('codex', ['login', 'status'], {
|
|
90
|
+
timeout: 5000,
|
|
91
|
+
stdio: 'ignore',
|
|
92
|
+
shell: true,
|
|
93
|
+
});
|
|
94
|
+
return true;
|
|
95
|
+
} catch {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private readStoredApiKey(): string | null {
|
|
101
|
+
try {
|
|
102
|
+
return fs.readFileSync(KEY_PATH, 'utf8').trim() || null;
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { spawn, type ChildProcess } from 'child_process';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
|
|
4
|
+
const IS_WIN = process.platform === 'win32';
|
|
5
|
+
|
|
6
|
+
import { Injectable, Logger, NotFoundException, OnModuleDestroy } from '@nestjs/common';
|
|
7
|
+
import { InjectRepository } from '@nestjs/typeorm';
|
|
8
|
+
import { Repository } from 'typeorm';
|
|
9
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
10
|
+
|
|
11
|
+
import { AgentSessionEntity } from '../../../database/entities/agent-session.entity';
|
|
12
|
+
import { SessionEntity } from '../../../database/entities/session.entity';
|
|
13
|
+
import { CodexAuthManager } from './codex-auth.manager';
|
|
14
|
+
|
|
15
|
+
export interface CodexSessionInfo {
|
|
16
|
+
id: string;
|
|
17
|
+
status: 'idle' | 'processing' | 'terminated';
|
|
18
|
+
workingDirectory: string;
|
|
19
|
+
model: string | null;
|
|
20
|
+
reasoning: string | null;
|
|
21
|
+
createdAt: Date;
|
|
22
|
+
lastActivity: Date;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface CodexSession extends CodexSessionInfo {
|
|
26
|
+
persisted: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ANSI_STRIP = /\x1b\[[0-9;?]*[a-zA-Z]|\x1b\][^\x07]*\x07/g;
|
|
30
|
+
const CODEX_REASONING_LEVELS = new Set(['low', 'medium', 'high', 'xhigh']);
|
|
31
|
+
|
|
32
|
+
function normalizeModel(value?: string | null): string | null {
|
|
33
|
+
const trimmed = value?.trim();
|
|
34
|
+
if (!trimmed || trimmed === 'default') return null;
|
|
35
|
+
return trimmed;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeReasoning(value?: string | null): string | null {
|
|
39
|
+
const trimmed = value?.trim();
|
|
40
|
+
if (!trimmed || trimmed === 'default') return null;
|
|
41
|
+
return CODEX_REASONING_LEVELS.has(trimmed) ? trimmed : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* codex exec 출력 형식:
|
|
46
|
+
* Reading additional input from stdin...
|
|
47
|
+
* OpenAI Codex vX.Y.Z
|
|
48
|
+
* --------
|
|
49
|
+
* workdir: ... / model: ... / ...
|
|
50
|
+
* --------
|
|
51
|
+
* user
|
|
52
|
+
* <user prompt echo>
|
|
53
|
+
* codex
|
|
54
|
+
* <actual response lines>
|
|
55
|
+
* tokens used
|
|
56
|
+
* <token count>
|
|
57
|
+
* <response duplicate>
|
|
58
|
+
*
|
|
59
|
+
* "codex" 섹션 내용만 emit하고, 나머지는 모두 버린다.
|
|
60
|
+
*/
|
|
61
|
+
type ParseState = 'header' | 'user_echo' | 'response' | 'done';
|
|
62
|
+
|
|
63
|
+
class CodexOutputParser {
|
|
64
|
+
private state: ParseState = 'header';
|
|
65
|
+
private lineBuffer = '';
|
|
66
|
+
|
|
67
|
+
/** 청크를 받아 emit할 텍스트만 반환한다 (없으면 빈 문자열). */
|
|
68
|
+
feed(raw: string): string {
|
|
69
|
+
const text = raw.replace(ANSI_STRIP, '').replace(/\r/g, '');
|
|
70
|
+
this.lineBuffer += text;
|
|
71
|
+
|
|
72
|
+
const lines = this.lineBuffer.split('\n');
|
|
73
|
+
this.lineBuffer = lines.pop() ?? '';
|
|
74
|
+
|
|
75
|
+
let out = '';
|
|
76
|
+
for (const line of lines) {
|
|
77
|
+
out += this.handleLine(line);
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 프로세스 종료 시 버퍼 플러시 */
|
|
83
|
+
flush(): string {
|
|
84
|
+
if (!this.lineBuffer) return '';
|
|
85
|
+
const out = this.handleLine(this.lineBuffer);
|
|
86
|
+
this.lineBuffer = '';
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private handleLine(line: string): string {
|
|
91
|
+
if (this.state === 'done') return '';
|
|
92
|
+
|
|
93
|
+
// 헤더 구간: 두 번째 '--------' 까지 모두 버림
|
|
94
|
+
if (this.state === 'header') {
|
|
95
|
+
if (line === '--------') this.state = 'user_echo';
|
|
96
|
+
return '';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 섹션 헤더 감지
|
|
100
|
+
if (line === 'user') { this.state = 'user_echo'; return ''; }
|
|
101
|
+
if (line === 'codex') { this.state = 'response'; return ''; }
|
|
102
|
+
if (line === 'tokens used') { this.state = 'done'; return ''; }
|
|
103
|
+
|
|
104
|
+
// user echo 줄 → 스킵하고 다음 섹션 헤더 대기
|
|
105
|
+
if (this.state === 'user_echo') return '';
|
|
106
|
+
|
|
107
|
+
// response 구간 → 그대로 출력
|
|
108
|
+
if (this.state === 'response') return line + '\n';
|
|
109
|
+
|
|
110
|
+
return '';
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@Injectable()
|
|
115
|
+
export class CodexSessionManager extends EventEmitter implements OnModuleDestroy {
|
|
116
|
+
private readonly logger = new Logger(CodexSessionManager.name);
|
|
117
|
+
private readonly sessions = new Map<string, CodexSession>();
|
|
118
|
+
private readonly processes = new Map<string, ChildProcess>();
|
|
119
|
+
|
|
120
|
+
constructor(
|
|
121
|
+
@InjectRepository(AgentSessionEntity)
|
|
122
|
+
private readonly agentSessionRepo: Repository<AgentSessionEntity>,
|
|
123
|
+
@InjectRepository(SessionEntity)
|
|
124
|
+
private readonly sessionRepo: Repository<SessionEntity>,
|
|
125
|
+
private readonly authManager: CodexAuthManager,
|
|
126
|
+
) {
|
|
127
|
+
super();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
createSession(workingDirectory = process.cwd(), model?: string, reasoning?: string): CodexSessionInfo {
|
|
131
|
+
const id = uuidv4();
|
|
132
|
+
const now = new Date();
|
|
133
|
+
const session: CodexSession = {
|
|
134
|
+
id,
|
|
135
|
+
status: 'idle',
|
|
136
|
+
workingDirectory,
|
|
137
|
+
model: normalizeModel(model),
|
|
138
|
+
reasoning: normalizeReasoning(reasoning),
|
|
139
|
+
createdAt: now,
|
|
140
|
+
lastActivity: now,
|
|
141
|
+
persisted: false,
|
|
142
|
+
};
|
|
143
|
+
this.sessions.set(id, session);
|
|
144
|
+
this.logger.log(`Created Codex session ${id} (cwd: ${workingDirectory})`);
|
|
145
|
+
return this.toInfo(session);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
getSession(id: string): CodexSessionInfo {
|
|
149
|
+
const session = this.sessions.get(id);
|
|
150
|
+
if (!session) throw new NotFoundException(`Session ${id} not found`);
|
|
151
|
+
return this.toInfo(session);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
listSessions(): CodexSessionInfo[] {
|
|
155
|
+
return [...this.sessions.values()].map((s) => this.toInfo(s));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
terminateSession(id: string): void {
|
|
159
|
+
const session = this.sessions.get(id);
|
|
160
|
+
if (!session) throw new NotFoundException(`Session ${id} not found`);
|
|
161
|
+
session.status = 'terminated';
|
|
162
|
+
this.killProcess(id);
|
|
163
|
+
this.sessions.delete(id);
|
|
164
|
+
if (session.persisted) {
|
|
165
|
+
void this.agentSessionRepo.update(id, { status: 'terminated' });
|
|
166
|
+
}
|
|
167
|
+
this.logger.log(`Terminated Codex session ${id}`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
sendMessage(sessionId: string, message: string): void {
|
|
171
|
+
const existing = this.sessions.get(sessionId);
|
|
172
|
+
if (!existing) {
|
|
173
|
+
void this.restoreAndSend(sessionId, message);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (existing.status === 'processing') throw new Error(`Session ${sessionId} is already processing`);
|
|
177
|
+
|
|
178
|
+
existing.status = 'processing';
|
|
179
|
+
existing.lastActivity = new Date();
|
|
180
|
+
|
|
181
|
+
if (!existing.persisted) {
|
|
182
|
+
void this.persistSession(existing).then(() => {
|
|
183
|
+
if (existing.status === 'terminated' || !this.sessions.has(sessionId)) {
|
|
184
|
+
void this.agentSessionRepo.update(sessionId, { status: 'terminated' });
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.spawnCodex(existing, message);
|
|
188
|
+
});
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
void this.agentSessionRepo.update(sessionId, { status: 'processing' });
|
|
193
|
+
this.spawnCodex(existing, message);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private async restoreAndSend(
|
|
197
|
+
sessionId: string,
|
|
198
|
+
message: string,
|
|
199
|
+
model?: string,
|
|
200
|
+
reasoning?: string,
|
|
201
|
+
): Promise<void> {
|
|
202
|
+
const record = await this.agentSessionRepo.findOne({ where: { id: sessionId } });
|
|
203
|
+
if (!record) {
|
|
204
|
+
const newSession = this.createSession(undefined, model, reasoning);
|
|
205
|
+
this.logger.log(`Session ${sessionId} not found — spawned new session ${newSession.id}`);
|
|
206
|
+
this.emit('session:replaced', { oldSessionId: sessionId, newSessionId: newSession.id });
|
|
207
|
+
this.sendMessage(newSession.id, message);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const now = new Date();
|
|
211
|
+
const session: CodexSession = {
|
|
212
|
+
id: sessionId,
|
|
213
|
+
status: 'processing',
|
|
214
|
+
workingDirectory: record.workingDirectory,
|
|
215
|
+
model: normalizeModel(record.model),
|
|
216
|
+
reasoning: normalizeReasoning(record.reasoning),
|
|
217
|
+
createdAt: record.createdAt,
|
|
218
|
+
lastActivity: now,
|
|
219
|
+
persisted: true,
|
|
220
|
+
};
|
|
221
|
+
this.updateModelSettings(session, model, reasoning);
|
|
222
|
+
this.sessions.set(sessionId, session);
|
|
223
|
+
this.logger.log(`Restored Codex session ${sessionId} (cwd: ${record.workingDirectory})`);
|
|
224
|
+
void this.agentSessionRepo.update(sessionId, { status: 'processing' });
|
|
225
|
+
this.spawnCodex(session, message);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private async persistSession(session: CodexSession): Promise<void> {
|
|
229
|
+
const title = session.workingDirectory.replace(/[/\\]+$/, '').split(/[/\\]/).filter(Boolean).at(-1) ?? 'Codex';
|
|
230
|
+
await Promise.all([
|
|
231
|
+
this.agentSessionRepo.save({
|
|
232
|
+
id: session.id,
|
|
233
|
+
claudeSessionId: null,
|
|
234
|
+
status: 'processing',
|
|
235
|
+
workingDirectory: session.workingDirectory,
|
|
236
|
+
model: session.model,
|
|
237
|
+
reasoning: session.reasoning,
|
|
238
|
+
}),
|
|
239
|
+
this.sessionRepo.save({ sessionId: session.id, title, agentType: 'codex' }),
|
|
240
|
+
]);
|
|
241
|
+
session.persisted = true;
|
|
242
|
+
this.logger.log(`Persisted Codex session ${session.id} (${title})`);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private updateModelSettings(session: CodexSession, model?: string, reasoning?: string): void {
|
|
246
|
+
if (model !== undefined) session.model = normalizeModel(model);
|
|
247
|
+
if (reasoning !== undefined) session.reasoning = normalizeReasoning(reasoning);
|
|
248
|
+
if (session.persisted && (model !== undefined || reasoning !== undefined)) {
|
|
249
|
+
void this.agentSessionRepo.update(session.id, {
|
|
250
|
+
model: session.model,
|
|
251
|
+
reasoning: session.reasoning,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
sendMessageWithSettings(sessionId: string, message: string, model?: string, reasoning?: string): void {
|
|
257
|
+
const existing = this.sessions.get(sessionId);
|
|
258
|
+
if (existing) {
|
|
259
|
+
this.updateModelSettings(existing, model, reasoning);
|
|
260
|
+
this.sendMessage(sessionId, message);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
void this.restoreAndSend(sessionId, message, model, reasoning);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private spawnCodex(session: CodexSession, message: string): void {
|
|
267
|
+
const sessionId = session.id;
|
|
268
|
+
const parser = new CodexOutputParser();
|
|
269
|
+
|
|
270
|
+
const codexArgs = ['exec', '-c', 'approval_policy=never', '-c', 'sandbox_mode=danger-full-access'];
|
|
271
|
+
if (session.model) {
|
|
272
|
+
codexArgs.push('-m', session.model);
|
|
273
|
+
}
|
|
274
|
+
if (session.reasoning) {
|
|
275
|
+
codexArgs.push('-c', `model_reasoning_effort="${session.reasoning}"`);
|
|
276
|
+
}
|
|
277
|
+
codexArgs.push(message);
|
|
278
|
+
const [cmd, spawnArgs] = IS_WIN
|
|
279
|
+
? ['cmd.exe', ['/c', 'codex', ...codexArgs]]
|
|
280
|
+
: ['codex', codexArgs];
|
|
281
|
+
|
|
282
|
+
const proc = spawn(cmd, spawnArgs, {
|
|
283
|
+
cwd: session.workingDirectory,
|
|
284
|
+
env: this.authManager.getEnvForCodex(),
|
|
285
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
286
|
+
});
|
|
287
|
+
this.processes.set(sessionId, proc);
|
|
288
|
+
|
|
289
|
+
let output = '';
|
|
290
|
+
|
|
291
|
+
const handleChunk = (chunk: Buffer) => {
|
|
292
|
+
const filtered = parser.feed(chunk.toString());
|
|
293
|
+
if (!filtered) return;
|
|
294
|
+
output += filtered;
|
|
295
|
+
this.emit('session:text', { sessionId, text: filtered });
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
proc.stdout.on('data', handleChunk);
|
|
299
|
+
proc.stderr.on('data', handleChunk);
|
|
300
|
+
|
|
301
|
+
proc.on('close', (exitCode) => {
|
|
302
|
+
this.processes.delete(sessionId);
|
|
303
|
+
const exitPayload = { sessionId, exitCode: exitCode ?? -1 };
|
|
304
|
+
|
|
305
|
+
if (session.status === 'terminated' || !this.sessions.has(sessionId)) {
|
|
306
|
+
this.emit('session:exit', exitPayload);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const tail = parser.flush();
|
|
311
|
+
if (tail) { output += tail; this.emit('session:text', { sessionId, text: tail }); }
|
|
312
|
+
|
|
313
|
+
session.status = 'idle';
|
|
314
|
+
void this.agentSessionRepo.update(sessionId, { status: 'idle' });
|
|
315
|
+
const isError = (exitCode ?? 0) !== 0;
|
|
316
|
+
this.emit('session:result', { sessionId, result: output.trim(), isError, durationMs: 0, costUsd: 0 });
|
|
317
|
+
this.emit('session:exit', exitPayload);
|
|
318
|
+
this.logger.log(`Codex session ${sessionId} finished (exit: ${exitCode})`);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
proc.on('error', (err) => {
|
|
322
|
+
this.processes.delete(sessionId);
|
|
323
|
+
if (session.status === 'terminated' || !this.sessions.has(sessionId)) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
session.status = 'idle';
|
|
328
|
+
void this.agentSessionRepo.update(sessionId, { status: 'idle' });
|
|
329
|
+
this.logger.error(`Codex session ${sessionId} spawn error: ${err.message}`);
|
|
330
|
+
this.emit('error', { sessionId, message: err.message });
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
onModuleDestroy(): void {
|
|
335
|
+
for (const sessionId of this.processes.keys()) {
|
|
336
|
+
this.killProcess(sessionId);
|
|
337
|
+
}
|
|
338
|
+
this.sessions.clear();
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private killProcess(sessionId: string): void {
|
|
342
|
+
const proc = this.processes.get(sessionId);
|
|
343
|
+
if (!proc) return;
|
|
344
|
+
|
|
345
|
+
this.processes.delete(sessionId);
|
|
346
|
+
try {
|
|
347
|
+
if (!proc.killed) proc.kill('SIGTERM');
|
|
348
|
+
} catch (err) {
|
|
349
|
+
this.logger.warn(`Failed to kill Codex session ${sessionId}: ${err instanceof Error ? err.message : String(err)}`);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private toInfo(session: CodexSession): CodexSessionInfo {
|
|
354
|
+
const { persisted: _p, ...info } = session;
|
|
355
|
+
return info;
|
|
356
|
+
}
|
|
357
|
+
}
|