agent-tower 0.1.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/dist/app.d.ts +2 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +62 -0
- package/dist/app.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +131 -0
- package/dist/cli.js.map +1 -0
- package/dist/core/container.d.ts +11 -0
- package/dist/core/container.d.ts.map +1 -0
- package/dist/core/container.js +66 -0
- package/dist/core/container.js.map +1 -0
- package/dist/core/event-bus.d.ts +58 -0
- package/dist/core/event-bus.d.ts.map +1 -0
- package/dist/core/event-bus.js +29 -0
- package/dist/core/event-bus.js.map +1 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +36 -0
- package/dist/errors.js.map +1 -0
- package/dist/executors/base.executor.d.ts +160 -0
- package/dist/executors/base.executor.d.ts.map +1 -0
- package/dist/executors/base.executor.js +122 -0
- package/dist/executors/base.executor.js.map +1 -0
- package/dist/executors/claude-code.executor.d.ts +78 -0
- package/dist/executors/claude-code.executor.d.ts.map +1 -0
- package/dist/executors/claude-code.executor.js +160 -0
- package/dist/executors/claude-code.executor.js.map +1 -0
- package/dist/executors/command-builder.d.ts +64 -0
- package/dist/executors/command-builder.d.ts.map +1 -0
- package/dist/executors/command-builder.js +144 -0
- package/dist/executors/command-builder.js.map +1 -0
- package/dist/executors/cursor-agent.executor.d.ts +57 -0
- package/dist/executors/cursor-agent.executor.d.ts.map +1 -0
- package/dist/executors/cursor-agent.executor.js +109 -0
- package/dist/executors/cursor-agent.executor.js.map +1 -0
- package/dist/executors/default-profiles.json +29 -0
- package/dist/executors/execution-env.d.ts +64 -0
- package/dist/executors/execution-env.d.ts.map +1 -0
- package/dist/executors/execution-env.js +97 -0
- package/dist/executors/execution-env.js.map +1 -0
- package/dist/executors/gemini-cli.executor.d.ts +55 -0
- package/dist/executors/gemini-cli.executor.d.ts.map +1 -0
- package/dist/executors/gemini-cli.executor.js +117 -0
- package/dist/executors/gemini-cli.executor.js.map +1 -0
- package/dist/executors/index.d.ts +49 -0
- package/dist/executors/index.d.ts.map +1 -0
- package/dist/executors/index.js +95 -0
- package/dist/executors/index.js.map +1 -0
- package/dist/executors/profiles.d.ts +61 -0
- package/dist/executors/profiles.d.ts.map +1 -0
- package/dist/executors/profiles.js +192 -0
- package/dist/executors/profiles.js.map +1 -0
- package/dist/git/git-cli.d.ts +59 -0
- package/dist/git/git-cli.d.ts.map +1 -0
- package/dist/git/git-cli.js +156 -0
- package/dist/git/git-cli.js.map +1 -0
- package/dist/git/worktree.manager.d.ts +166 -0
- package/dist/git/worktree.manager.d.ts.map +1 -0
- package/dist/git/worktree.manager.js +481 -0
- package/dist/git/worktree.manager.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/context.d.ts +15 -0
- package/dist/mcp/context.d.ts.map +1 -0
- package/dist/mcp/context.js +19 -0
- package/dist/mcp/context.js.map +1 -0
- package/dist/mcp/http-client.d.ts +46 -0
- package/dist/mcp/http-client.d.ts.map +1 -0
- package/dist/mcp/http-client.js +100 -0
- package/dist/mcp/http-client.js.map +1 -0
- package/dist/mcp/index.d.ts +3 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +36 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/server.d.ts +6 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +32 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tools/projects.d.ts +7 -0
- package/dist/mcp/tools/projects.d.ts.map +1 -0
- package/dist/mcp/tools/projects.js +12 -0
- package/dist/mcp/tools/projects.js.map +1 -0
- package/dist/mcp/tools/sessions.d.ts +7 -0
- package/dist/mcp/tools/sessions.d.ts.map +1 -0
- package/dist/mcp/tools/sessions.js +22 -0
- package/dist/mcp/tools/sessions.js.map +1 -0
- package/dist/mcp/tools/tasks.d.ts +7 -0
- package/dist/mcp/tools/tasks.d.ts.map +1 -0
- package/dist/mcp/tools/tasks.js +68 -0
- package/dist/mcp/tools/tasks.js.map +1 -0
- package/dist/mcp/tools/workspaces.d.ts +7 -0
- package/dist/mcp/tools/workspaces.d.ts.map +1 -0
- package/dist/mcp/tools/workspaces.js +45 -0
- package/dist/mcp/tools/workspaces.js.map +1 -0
- package/dist/mcp/types.d.ts +109 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +55 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/middleware/tunnel-auth.d.ts +13 -0
- package/dist/middleware/tunnel-auth.d.ts.map +1 -0
- package/dist/middleware/tunnel-auth.js +39 -0
- package/dist/middleware/tunnel-auth.js.map +1 -0
- package/dist/output/__tests__/claude-code-parser-token.test.d.ts +2 -0
- package/dist/output/__tests__/claude-code-parser-token.test.d.ts.map +1 -0
- package/dist/output/__tests__/claude-code-parser-token.test.js +233 -0
- package/dist/output/__tests__/claude-code-parser-token.test.js.map +1 -0
- package/dist/output/__tests__/cursor-agent-parser-token.test.d.ts +2 -0
- package/dist/output/__tests__/cursor-agent-parser-token.test.d.ts.map +1 -0
- package/dist/output/__tests__/cursor-agent-parser-token.test.js +95 -0
- package/dist/output/__tests__/cursor-agent-parser-token.test.js.map +1 -0
- package/dist/output/__tests__/msg-store-token.test.d.ts +2 -0
- package/dist/output/__tests__/msg-store-token.test.d.ts.map +1 -0
- package/dist/output/__tests__/msg-store-token.test.js +38 -0
- package/dist/output/__tests__/msg-store-token.test.js.map +1 -0
- package/dist/output/claude-code-parser.d.ts +96 -0
- package/dist/output/claude-code-parser.d.ts.map +1 -0
- package/dist/output/claude-code-parser.js +498 -0
- package/dist/output/claude-code-parser.js.map +1 -0
- package/dist/output/cursor-agent-parser.d.ts +87 -0
- package/dist/output/cursor-agent-parser.d.ts.map +1 -0
- package/dist/output/cursor-agent-parser.js +476 -0
- package/dist/output/cursor-agent-parser.js.map +1 -0
- package/dist/output/index.d.ts +9 -0
- package/dist/output/index.d.ts.map +1 -0
- package/dist/output/index.js +13 -0
- package/dist/output/index.js.map +1 -0
- package/dist/output/msg-store.d.ts +110 -0
- package/dist/output/msg-store.d.ts.map +1 -0
- package/dist/output/msg-store.js +274 -0
- package/dist/output/msg-store.js.map +1 -0
- package/dist/output/types.d.ts +90 -0
- package/dist/output/types.d.ts.map +1 -0
- package/dist/output/types.js +93 -0
- package/dist/output/types.js.map +1 -0
- package/dist/output/utils/ansi.d.ts +8 -0
- package/dist/output/utils/ansi.d.ts.map +1 -0
- package/dist/output/utils/ansi.js +17 -0
- package/dist/output/utils/ansi.js.map +1 -0
- package/dist/output/utils/patch.d.ts +57 -0
- package/dist/output/utils/patch.d.ts.map +1 -0
- package/dist/output/utils/patch.js +113 -0
- package/dist/output/utils/patch.js.map +1 -0
- package/dist/pipeline/agent-pipeline.d.ts +28 -0
- package/dist/pipeline/agent-pipeline.d.ts.map +1 -0
- package/dist/pipeline/agent-pipeline.js +76 -0
- package/dist/pipeline/agent-pipeline.js.map +1 -0
- package/dist/routes/attachments.d.ts +3 -0
- package/dist/routes/attachments.d.ts.map +1 -0
- package/dist/routes/attachments.js +141 -0
- package/dist/routes/attachments.js.map +1 -0
- package/dist/routes/demo.d.ts +3 -0
- package/dist/routes/demo.d.ts.map +1 -0
- package/dist/routes/demo.js +152 -0
- package/dist/routes/demo.js.map +1 -0
- package/dist/routes/files.d.ts +3 -0
- package/dist/routes/files.d.ts.map +1 -0
- package/dist/routes/files.js +313 -0
- package/dist/routes/files.js.map +1 -0
- package/dist/routes/filesystem.d.ts +3 -0
- package/dist/routes/filesystem.d.ts.map +1 -0
- package/dist/routes/filesystem.js +126 -0
- package/dist/routes/filesystem.js.map +1 -0
- package/dist/routes/git.d.ts +3 -0
- package/dist/routes/git.d.ts.map +1 -0
- package/dist/routes/git.js +170 -0
- package/dist/routes/git.js.map +1 -0
- package/dist/routes/index.d.ts +3 -0
- package/dist/routes/index.d.ts.map +1 -0
- package/dist/routes/index.js +45 -0
- package/dist/routes/index.js.map +1 -0
- package/dist/routes/notifications.d.ts +10 -0
- package/dist/routes/notifications.d.ts.map +1 -0
- package/dist/routes/notifications.js +54 -0
- package/dist/routes/notifications.js.map +1 -0
- package/dist/routes/profiles.d.ts +12 -0
- package/dist/routes/profiles.d.ts.map +1 -0
- package/dist/routes/profiles.js +76 -0
- package/dist/routes/profiles.js.map +1 -0
- package/dist/routes/projects.d.ts +3 -0
- package/dist/routes/projects.d.ts.map +1 -0
- package/dist/routes/projects.js +94 -0
- package/dist/routes/projects.js.map +1 -0
- package/dist/routes/sessions.d.ts +12 -0
- package/dist/routes/sessions.d.ts.map +1 -0
- package/dist/routes/sessions.js +109 -0
- package/dist/routes/sessions.js.map +1 -0
- package/dist/routes/system.d.ts +3 -0
- package/dist/routes/system.d.ts.map +1 -0
- package/dist/routes/system.js +42 -0
- package/dist/routes/system.js.map +1 -0
- package/dist/routes/tasks.d.ts +3 -0
- package/dist/routes/tasks.d.ts.map +1 -0
- package/dist/routes/tasks.js +136 -0
- package/dist/routes/tasks.js.map +1 -0
- package/dist/routes/terminals.d.ts +3 -0
- package/dist/routes/terminals.d.ts.map +1 -0
- package/dist/routes/terminals.js +54 -0
- package/dist/routes/terminals.js.map +1 -0
- package/dist/routes/tunnel.d.ts +3 -0
- package/dist/routes/tunnel.d.ts.map +1 -0
- package/dist/routes/tunnel.js +36 -0
- package/dist/routes/tunnel.js.map +1 -0
- package/dist/routes/workspaces.d.ts +3 -0
- package/dist/routes/workspaces.d.ts.map +1 -0
- package/dist/routes/workspaces.js +171 -0
- package/dist/routes/workspaces.js.map +1 -0
- package/dist/services/commit-message.service.d.ts +32 -0
- package/dist/services/commit-message.service.d.ts.map +1 -0
- package/dist/services/commit-message.service.js +202 -0
- package/dist/services/commit-message.service.js.map +1 -0
- package/dist/services/notifications/feishu-channel.d.ts +9 -0
- package/dist/services/notifications/feishu-channel.d.ts.map +1 -0
- package/dist/services/notifications/feishu-channel.js +51 -0
- package/dist/services/notifications/feishu-channel.js.map +1 -0
- package/dist/services/notifications/index.d.ts +3 -0
- package/dist/services/notifications/index.d.ts.map +1 -0
- package/dist/services/notifications/index.js +2 -0
- package/dist/services/notifications/index.js.map +1 -0
- package/dist/services/notifications/notification.service.d.ts +10 -0
- package/dist/services/notifications/notification.service.d.ts.map +1 -0
- package/dist/services/notifications/notification.service.js +85 -0
- package/dist/services/notifications/notification.service.js.map +1 -0
- package/dist/services/notifications/os-channel.d.ts +8 -0
- package/dist/services/notifications/os-channel.d.ts.map +1 -0
- package/dist/services/notifications/os-channel.js +38 -0
- package/dist/services/notifications/os-channel.js.map +1 -0
- package/dist/services/notifications/types.d.ts +11 -0
- package/dist/services/notifications/types.d.ts.map +1 -0
- package/dist/services/notifications/types.js +2 -0
- package/dist/services/notifications/types.js.map +1 -0
- package/dist/services/project.service.d.ts +91 -0
- package/dist/services/project.service.d.ts.map +1 -0
- package/dist/services/project.service.js +138 -0
- package/dist/services/project.service.js.map +1 -0
- package/dist/services/session-manager.d.ts +164 -0
- package/dist/services/session-manager.d.ts.map +1 -0
- package/dist/services/session-manager.js +569 -0
- package/dist/services/session-manager.js.map +1 -0
- package/dist/services/task.service.d.ts +179 -0
- package/dist/services/task.service.d.ts.map +1 -0
- package/dist/services/task.service.js +281 -0
- package/dist/services/task.service.js.map +1 -0
- package/dist/services/terminal-manager.d.ts +64 -0
- package/dist/services/terminal-manager.d.ts.map +1 -0
- package/dist/services/terminal-manager.js +217 -0
- package/dist/services/terminal-manager.js.map +1 -0
- package/dist/services/tunnel.service.d.ts +16 -0
- package/dist/services/tunnel.service.d.ts.map +1 -0
- package/dist/services/tunnel.service.js +77 -0
- package/dist/services/tunnel.service.js.map +1 -0
- package/dist/services/workspace.service.d.ts +219 -0
- package/dist/services/workspace.service.d.ts.map +1 -0
- package/dist/services/workspace.service.js +351 -0
- package/dist/services/workspace.service.js.map +1 -0
- package/dist/socket/events.d.ts +2 -0
- package/dist/socket/events.d.ts.map +1 -0
- package/dist/socket/events.js +3 -0
- package/dist/socket/events.js.map +1 -0
- package/dist/socket/index.d.ts +20 -0
- package/dist/socket/index.d.ts.map +1 -0
- package/dist/socket/index.js +77 -0
- package/dist/socket/index.js.map +1 -0
- package/dist/socket/middleware/auth.d.ts +13 -0
- package/dist/socket/middleware/auth.d.ts.map +1 -0
- package/dist/socket/middleware/auth.js +29 -0
- package/dist/socket/middleware/auth.js.map +1 -0
- package/dist/socket/middleware/error-handler.d.ts +11 -0
- package/dist/socket/middleware/error-handler.d.ts.map +1 -0
- package/dist/socket/middleware/error-handler.js +39 -0
- package/dist/socket/middleware/error-handler.js.map +1 -0
- package/dist/socket/middleware/index.d.ts +3 -0
- package/dist/socket/middleware/index.d.ts.map +1 -0
- package/dist/socket/middleware/index.js +3 -0
- package/dist/socket/middleware/index.js.map +1 -0
- package/dist/socket/rooms.d.ts +30 -0
- package/dist/socket/rooms.d.ts.map +1 -0
- package/dist/socket/rooms.js +39 -0
- package/dist/socket/rooms.js.map +1 -0
- package/dist/socket/socket-gateway.d.ts +21 -0
- package/dist/socket/socket-gateway.d.ts.map +1 -0
- package/dist/socket/socket-gateway.js +173 -0
- package/dist/socket/socket-gateway.js.map +1 -0
- package/dist/types/index.d.ts +41 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +41 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.d.ts +12 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +25 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/web/assets/AgentDemoPage-WdWEQtyU.js +1 -0
- package/dist/web/assets/DemoPage-C4Z_ftHI.js +4 -0
- package/dist/web/assets/NotificationSettingsPage-FrLfgt9y.js +1 -0
- package/dist/web/assets/ProfileSettingsPage-Cs93rTRr.js +3 -0
- package/dist/web/assets/ProjectKanbanPage-BmlRVfzo.js +75 -0
- package/dist/web/assets/ProjectKanbanPage-G5OQeJvU.css +1 -0
- package/dist/web/assets/api-client-CEBkg5lt.js +1 -0
- package/dist/web/assets/circle-alert-BEozlsMd.js +1 -0
- package/dist/web/assets/code-block-OCS4YCEC-CGgYYS4A.js +2 -0
- package/dist/web/assets/folder-picker-BP3x3phB.js +1 -0
- package/dist/web/assets/index-BuAijQvi.js +13 -0
- package/dist/web/assets/index-CxV866Yg.css +1 -0
- package/dist/web/assets/loader-circle-yEIqsdJ1.js +1 -0
- package/dist/web/assets/mermaid-NOHMQCX5-CIqtI2jF.js +123 -0
- package/dist/web/assets/mermaid-NOHMQCX5-lI9o3AWT.css +1 -0
- package/dist/web/assets/modal-BoX2RXaV.js +1 -0
- package/dist/web/assets/query-keys-BD_s_Etj.js +1 -0
- package/dist/web/assets/use-profiles-Rh8rBsAt.js +1 -0
- package/dist/web/index.html +14 -0
- package/dist/web/vite.svg +1 -0
- package/node_modules/@agent-tower/shared/dist/dev-port.d.ts +12 -0
- package/node_modules/@agent-tower/shared/dist/dev-port.d.ts.map +1 -0
- package/node_modules/@agent-tower/shared/dist/dev-port.js +31 -0
- package/node_modules/@agent-tower/shared/dist/dev-port.js.map +1 -0
- package/node_modules/@agent-tower/shared/dist/index.d.ts +5 -0
- package/node_modules/@agent-tower/shared/dist/index.d.ts.map +1 -0
- package/node_modules/@agent-tower/shared/dist/index.js +5 -0
- package/node_modules/@agent-tower/shared/dist/index.js.map +1 -0
- package/node_modules/@agent-tower/shared/dist/log-adapter.d.ts +72 -0
- package/node_modules/@agent-tower/shared/dist/log-adapter.d.ts.map +1 -0
- package/node_modules/@agent-tower/shared/dist/log-adapter.js +144 -0
- package/node_modules/@agent-tower/shared/dist/log-adapter.js.map +1 -0
- package/node_modules/@agent-tower/shared/dist/socket/events.d.ts +133 -0
- package/node_modules/@agent-tower/shared/dist/socket/events.d.ts.map +1 -0
- package/node_modules/@agent-tower/shared/dist/socket/events.js +32 -0
- package/node_modules/@agent-tower/shared/dist/socket/events.js.map +1 -0
- package/node_modules/@agent-tower/shared/dist/socket/index.d.ts +2 -0
- package/node_modules/@agent-tower/shared/dist/socket/index.d.ts.map +1 -0
- package/node_modules/@agent-tower/shared/dist/socket/index.js +2 -0
- package/node_modules/@agent-tower/shared/dist/socket/index.js.map +1 -0
- package/node_modules/@agent-tower/shared/dist/types.d.ts +168 -0
- package/node_modules/@agent-tower/shared/dist/types.d.ts.map +1 -0
- package/node_modules/@agent-tower/shared/dist/types.js +53 -0
- package/node_modules/@agent-tower/shared/dist/types.js.map +1 -0
- package/node_modules/@agent-tower/shared/package.json +38 -0
- package/package.json +46 -0
- package/prisma/schema.prisma +130 -0
- package/scripts/postinstall.js +120 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod';
|
|
2
|
+
import * as fs from 'node:fs/promises';
|
|
3
|
+
import * as fssync from 'node:fs';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
const IMAGE_EXTENSIONS = new Set([
|
|
6
|
+
'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg', '.ico', '.avif',
|
|
7
|
+
]);
|
|
8
|
+
const MIME_MAP = {
|
|
9
|
+
'.png': 'image/png',
|
|
10
|
+
'.jpg': 'image/jpeg',
|
|
11
|
+
'.jpeg': 'image/jpeg',
|
|
12
|
+
'.gif': 'image/gif',
|
|
13
|
+
'.bmp': 'image/bmp',
|
|
14
|
+
'.webp': 'image/webp',
|
|
15
|
+
'.svg': 'image/svg+xml',
|
|
16
|
+
'.ico': 'image/x-icon',
|
|
17
|
+
'.avif': 'image/avif',
|
|
18
|
+
};
|
|
19
|
+
const IGNORED_DIRS = new Set([
|
|
20
|
+
'node_modules',
|
|
21
|
+
'.git',
|
|
22
|
+
'dist',
|
|
23
|
+
'build',
|
|
24
|
+
'.next',
|
|
25
|
+
'out',
|
|
26
|
+
'coverage',
|
|
27
|
+
'.turbo',
|
|
28
|
+
'.cache',
|
|
29
|
+
'.pnpm',
|
|
30
|
+
'.yarn',
|
|
31
|
+
]);
|
|
32
|
+
function handleError(error, reply) {
|
|
33
|
+
if (error instanceof ZodError) {
|
|
34
|
+
reply.code(400);
|
|
35
|
+
return { error: 'Validation failed', code: 'VALIDATION_ERROR', details: error.errors };
|
|
36
|
+
}
|
|
37
|
+
if (error && typeof error === 'object' && 'code' in error) {
|
|
38
|
+
const code = error.code;
|
|
39
|
+
if (code === 'ENOENT') {
|
|
40
|
+
reply.code(404);
|
|
41
|
+
return { error: 'Not found', code: 'NOT_FOUND' };
|
|
42
|
+
}
|
|
43
|
+
if (code === 'EACCES' || code === 'EPERM') {
|
|
44
|
+
reply.code(403);
|
|
45
|
+
return { error: 'Permission denied', code: 'PERMISSION_DENIED' };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
reply.code(500);
|
|
49
|
+
return { error: 'Internal server error', code: 'INTERNAL_ERROR' };
|
|
50
|
+
}
|
|
51
|
+
function normalizeUserPath(input) {
|
|
52
|
+
const raw = (input ?? '').trim();
|
|
53
|
+
if (!raw || raw === '/')
|
|
54
|
+
return '';
|
|
55
|
+
const noBackslash = raw.replace(/\\/g, '/');
|
|
56
|
+
const stripped = noBackslash.startsWith('/') ? noBackslash.slice(1) : noBackslash;
|
|
57
|
+
return stripped;
|
|
58
|
+
}
|
|
59
|
+
function assertNoTraversalSegments(relPath) {
|
|
60
|
+
const segments = relPath.split('/').filter(Boolean);
|
|
61
|
+
if (segments.some((s) => s === '..')) {
|
|
62
|
+
throw new ZodError([
|
|
63
|
+
{
|
|
64
|
+
code: 'custom',
|
|
65
|
+
message: 'Path traversal (..) is not allowed',
|
|
66
|
+
path: ['path'],
|
|
67
|
+
},
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function resolveInWorkingDir(workingDir, userPath) {
|
|
72
|
+
const baseReal = await fs.realpath(workingDir);
|
|
73
|
+
const rel = normalizeUserPath(userPath);
|
|
74
|
+
assertNoTraversalSegments(rel);
|
|
75
|
+
const abs = path.resolve(baseReal, rel);
|
|
76
|
+
const relative = path.relative(baseReal, abs);
|
|
77
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
78
|
+
const err = new Error('Resolved path is outside workingDir');
|
|
79
|
+
err.code = 'OUTSIDE_WORKING_DIR';
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
return { baseReal, rel, abs };
|
|
83
|
+
}
|
|
84
|
+
function inferLanguage(filePath) {
|
|
85
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
86
|
+
switch (ext) {
|
|
87
|
+
case '.ts':
|
|
88
|
+
case '.tsx':
|
|
89
|
+
return 'typescript';
|
|
90
|
+
case '.js':
|
|
91
|
+
case '.jsx':
|
|
92
|
+
case '.mjs':
|
|
93
|
+
case '.cjs':
|
|
94
|
+
return 'javascript';
|
|
95
|
+
case '.json':
|
|
96
|
+
return 'json';
|
|
97
|
+
case '.md':
|
|
98
|
+
case '.mdx':
|
|
99
|
+
return 'markdown';
|
|
100
|
+
case '.css':
|
|
101
|
+
return 'css';
|
|
102
|
+
case '.scss':
|
|
103
|
+
return 'scss';
|
|
104
|
+
case '.html':
|
|
105
|
+
return 'html';
|
|
106
|
+
case '.yml':
|
|
107
|
+
case '.yaml':
|
|
108
|
+
return 'yaml';
|
|
109
|
+
case '.sh':
|
|
110
|
+
return 'shell';
|
|
111
|
+
case '.py':
|
|
112
|
+
return 'python';
|
|
113
|
+
case '.go':
|
|
114
|
+
return 'go';
|
|
115
|
+
case '.rs':
|
|
116
|
+
return 'rust';
|
|
117
|
+
default:
|
|
118
|
+
return 'plaintext';
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const treeQuerySchema = z.object({
|
|
122
|
+
path: z.string().optional(),
|
|
123
|
+
workingDir: z
|
|
124
|
+
.string()
|
|
125
|
+
.min(1, 'workingDir is required')
|
|
126
|
+
.refine((v) => path.isAbsolute(v), { message: 'workingDir must be absolute' })
|
|
127
|
+
.refine((v) => !v.split(path.sep).includes('..'), {
|
|
128
|
+
message: 'Path traversal (..) is not allowed',
|
|
129
|
+
}),
|
|
130
|
+
});
|
|
131
|
+
const readQuerySchema = z.object({
|
|
132
|
+
path: z.string().min(1, 'path is required'),
|
|
133
|
+
workingDir: z
|
|
134
|
+
.string()
|
|
135
|
+
.min(1, 'workingDir is required')
|
|
136
|
+
.refine((v) => path.isAbsolute(v), { message: 'workingDir must be absolute' })
|
|
137
|
+
.refine((v) => !v.split(path.sep).includes('..'), {
|
|
138
|
+
message: 'Path traversal (..) is not allowed',
|
|
139
|
+
}),
|
|
140
|
+
});
|
|
141
|
+
const writeBodySchema = z.object({
|
|
142
|
+
path: z.string().min(1, 'path is required'),
|
|
143
|
+
workingDir: z
|
|
144
|
+
.string()
|
|
145
|
+
.min(1, 'workingDir is required')
|
|
146
|
+
.refine((v) => path.isAbsolute(v), { message: 'workingDir must be absolute' })
|
|
147
|
+
.refine((v) => !v.split(path.sep).includes('..'), {
|
|
148
|
+
message: 'Path traversal (..) is not allowed',
|
|
149
|
+
}),
|
|
150
|
+
content: z.string(),
|
|
151
|
+
});
|
|
152
|
+
export async function filesRoutes(app) {
|
|
153
|
+
/**
|
|
154
|
+
* GET /tree?path=/&workingDir=/abs/path
|
|
155
|
+
* 列出指定目录下一层文件/文件夹(不递归)
|
|
156
|
+
*/
|
|
157
|
+
app.get('/tree', async (request, reply) => {
|
|
158
|
+
try {
|
|
159
|
+
const { path: userPath, workingDir } = treeQuerySchema.parse(request.query);
|
|
160
|
+
if (!fssync.existsSync(workingDir)) {
|
|
161
|
+
reply.code(400);
|
|
162
|
+
return { error: `workingDir does not exist: ${workingDir}`, code: 'WORKING_DIR_NOT_FOUND' };
|
|
163
|
+
}
|
|
164
|
+
const { baseReal, abs } = await resolveInWorkingDir(workingDir, userPath);
|
|
165
|
+
const dirReal = await fs.realpath(abs);
|
|
166
|
+
const relative = path.relative(baseReal, dirReal);
|
|
167
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
168
|
+
reply.code(400);
|
|
169
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
170
|
+
}
|
|
171
|
+
const stat = await fs.stat(dirReal);
|
|
172
|
+
if (!stat.isDirectory()) {
|
|
173
|
+
reply.code(400);
|
|
174
|
+
return { error: 'path is not a directory', code: 'NOT_A_DIRECTORY' };
|
|
175
|
+
}
|
|
176
|
+
const entries = await fs.readdir(dirReal, { withFileTypes: true });
|
|
177
|
+
const items = entries
|
|
178
|
+
.filter((e) => {
|
|
179
|
+
if (e.isDirectory())
|
|
180
|
+
return !IGNORED_DIRS.has(e.name);
|
|
181
|
+
// ignore some noisy files
|
|
182
|
+
if (e.name === '.DS_Store')
|
|
183
|
+
return false;
|
|
184
|
+
return true;
|
|
185
|
+
})
|
|
186
|
+
.map((e) => {
|
|
187
|
+
const type = e.isDirectory() ? 'directory' : 'file';
|
|
188
|
+
return { name: e.name, type };
|
|
189
|
+
})
|
|
190
|
+
.sort((a, b) => {
|
|
191
|
+
if (a.type !== b.type)
|
|
192
|
+
return a.type === 'directory' ? -1 : 1;
|
|
193
|
+
return a.name.localeCompare(b.name);
|
|
194
|
+
});
|
|
195
|
+
return { items };
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (error && typeof error === 'object' && 'code' in error && error.code === 'OUTSIDE_WORKING_DIR') {
|
|
199
|
+
reply.code(400);
|
|
200
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
201
|
+
}
|
|
202
|
+
return handleError(error, reply);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
/**
|
|
206
|
+
* GET /read?path=src/auth/Login.tsx&workingDir=/abs/path
|
|
207
|
+
* 读取文件内容
|
|
208
|
+
*/
|
|
209
|
+
app.get('/read', async (request, reply) => {
|
|
210
|
+
try {
|
|
211
|
+
const { path: userFilePath, workingDir } = readQuerySchema.parse(request.query);
|
|
212
|
+
if (!fssync.existsSync(workingDir)) {
|
|
213
|
+
reply.code(400);
|
|
214
|
+
return { error: `workingDir does not exist: ${workingDir}`, code: 'WORKING_DIR_NOT_FOUND' };
|
|
215
|
+
}
|
|
216
|
+
const { baseReal, abs } = await resolveInWorkingDir(workingDir, userFilePath);
|
|
217
|
+
const fileReal = await fs.realpath(abs);
|
|
218
|
+
const relative = path.relative(baseReal, fileReal);
|
|
219
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
220
|
+
reply.code(400);
|
|
221
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
222
|
+
}
|
|
223
|
+
const stat = await fs.stat(fileReal);
|
|
224
|
+
if (!stat.isFile()) {
|
|
225
|
+
reply.code(400);
|
|
226
|
+
return { error: 'path is not a file', code: 'NOT_A_FILE' };
|
|
227
|
+
}
|
|
228
|
+
const content = await fs.readFile(fileReal, 'utf8');
|
|
229
|
+
return { content, language: inferLanguage(userFilePath) };
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
if (error && typeof error === 'object' && 'code' in error && error.code === 'OUTSIDE_WORKING_DIR') {
|
|
233
|
+
reply.code(400);
|
|
234
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
235
|
+
}
|
|
236
|
+
return handleError(error, reply);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
/**
|
|
240
|
+
* POST /write { path, workingDir, content }
|
|
241
|
+
* 保存文件内容
|
|
242
|
+
*/
|
|
243
|
+
app.post('/write', async (request, reply) => {
|
|
244
|
+
try {
|
|
245
|
+
const { path: userFilePath, workingDir, content } = writeBodySchema.parse(request.body || {});
|
|
246
|
+
if (!fssync.existsSync(workingDir)) {
|
|
247
|
+
reply.code(400);
|
|
248
|
+
return { error: `workingDir does not exist: ${workingDir}`, code: 'WORKING_DIR_NOT_FOUND' };
|
|
249
|
+
}
|
|
250
|
+
const { baseReal, abs } = await resolveInWorkingDir(workingDir, userFilePath);
|
|
251
|
+
// Ensure parent dir exists and is within workingDir (mitigate symlink escapes)
|
|
252
|
+
const parent = path.dirname(abs);
|
|
253
|
+
const parentReal = await fs.realpath(parent);
|
|
254
|
+
const parentRel = path.relative(baseReal, parentReal);
|
|
255
|
+
if (parentRel.startsWith('..') || path.isAbsolute(parentRel)) {
|
|
256
|
+
reply.code(400);
|
|
257
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
258
|
+
}
|
|
259
|
+
await fs.writeFile(abs, content, 'utf8');
|
|
260
|
+
return { success: true };
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
if (error && typeof error === 'object' && 'code' in error && error.code === 'OUTSIDE_WORKING_DIR') {
|
|
264
|
+
reply.code(400);
|
|
265
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
266
|
+
}
|
|
267
|
+
return handleError(error, reply);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
/**
|
|
271
|
+
* GET /image?path=assets/logo.png&workingDir=/abs/path
|
|
272
|
+
* 返回图片文件的原始二进制数据
|
|
273
|
+
*/
|
|
274
|
+
app.get('/image', async (request, reply) => {
|
|
275
|
+
try {
|
|
276
|
+
const { path: userFilePath, workingDir } = readQuerySchema.parse(request.query);
|
|
277
|
+
if (!fssync.existsSync(workingDir)) {
|
|
278
|
+
reply.code(400);
|
|
279
|
+
return { error: `workingDir does not exist: ${workingDir}`, code: 'WORKING_DIR_NOT_FOUND' };
|
|
280
|
+
}
|
|
281
|
+
const { baseReal, abs } = await resolveInWorkingDir(workingDir, userFilePath);
|
|
282
|
+
const fileReal = await fs.realpath(abs);
|
|
283
|
+
const relative = path.relative(baseReal, fileReal);
|
|
284
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
285
|
+
reply.code(400);
|
|
286
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
287
|
+
}
|
|
288
|
+
const stat = await fs.stat(fileReal);
|
|
289
|
+
if (!stat.isFile()) {
|
|
290
|
+
reply.code(400);
|
|
291
|
+
return { error: 'path is not a file', code: 'NOT_A_FILE' };
|
|
292
|
+
}
|
|
293
|
+
const ext = path.extname(fileReal).toLowerCase();
|
|
294
|
+
if (!IMAGE_EXTENSIONS.has(ext)) {
|
|
295
|
+
reply.code(400);
|
|
296
|
+
return { error: 'Not an image file', code: 'NOT_IMAGE' };
|
|
297
|
+
}
|
|
298
|
+
const mime = MIME_MAP[ext] || 'application/octet-stream';
|
|
299
|
+
const buffer = await fs.readFile(fileReal);
|
|
300
|
+
reply.header('Content-Type', mime);
|
|
301
|
+
reply.header('Cache-Control', 'no-cache');
|
|
302
|
+
return reply.send(buffer);
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
if (error && typeof error === 'object' && 'code' in error && error.code === 'OUTSIDE_WORKING_DIR') {
|
|
306
|
+
reply.code(400);
|
|
307
|
+
return { error: 'Path is outside workingDir', code: 'OUTSIDE_WORKING_DIR' };
|
|
308
|
+
}
|
|
309
|
+
return handleError(error, reply);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../../src/routes/files.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAClC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAIlC,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC;IAC/B,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO;CAC1E,CAAC,CAAC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CAAC;YAC9E,CAAC;YACD,OAAO,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACnC,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesystem.d.ts","sourceRoot":"","sources":["../../src/routes/filesystem.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAgD/C,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,eAAe,iBAuF1D"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import * as os from 'node:os';
|
|
5
|
+
/**
|
|
6
|
+
* 查询参数校验:path 必须是绝对路径且不含 ".." 片段
|
|
7
|
+
*/
|
|
8
|
+
const browseQuerySchema = z.object({
|
|
9
|
+
path: z
|
|
10
|
+
.string()
|
|
11
|
+
.optional()
|
|
12
|
+
.default('')
|
|
13
|
+
.transform((v) => v || os.homedir())
|
|
14
|
+
.refine((v) => path.isAbsolute(v), { message: 'path must be absolute' })
|
|
15
|
+
.refine((v) => !v.split(path.sep).includes('..'), {
|
|
16
|
+
message: 'Path traversal (..) is not allowed',
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
19
|
+
const validateQuerySchema = z.object({
|
|
20
|
+
path: z
|
|
21
|
+
.string()
|
|
22
|
+
.min(1, 'path is required')
|
|
23
|
+
.refine((v) => path.isAbsolute(v), { message: 'path must be absolute' })
|
|
24
|
+
.refine((v) => !v.split(path.sep).includes('..'), {
|
|
25
|
+
message: 'Path traversal (..) is not allowed',
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
/**
|
|
29
|
+
* 统一错误处理
|
|
30
|
+
*/
|
|
31
|
+
function handleError(error, reply) {
|
|
32
|
+
if (error instanceof ZodError) {
|
|
33
|
+
const fieldErrors = error.errors.map((e) => ({
|
|
34
|
+
field: e.path.join('.'),
|
|
35
|
+
message: e.message,
|
|
36
|
+
}));
|
|
37
|
+
reply.code(400);
|
|
38
|
+
return { error: 'Validation failed', code: 'VALIDATION_ERROR', details: fieldErrors };
|
|
39
|
+
}
|
|
40
|
+
reply.code(500);
|
|
41
|
+
return { error: 'Internal server error', code: 'INTERNAL_ERROR' };
|
|
42
|
+
}
|
|
43
|
+
export async function filesystemRoutes(app) {
|
|
44
|
+
/**
|
|
45
|
+
* GET /browse?path=<dir>
|
|
46
|
+
* 列出指定目录下的子目录(不列文件),返回每个目录的 isGitRepo 标记
|
|
47
|
+
*/
|
|
48
|
+
app.get('/browse', async (request, reply) => {
|
|
49
|
+
try {
|
|
50
|
+
const { path: dirPath } = browseQuerySchema.parse(request.query);
|
|
51
|
+
// 检查目录是否存在
|
|
52
|
+
if (!fs.existsSync(dirPath)) {
|
|
53
|
+
reply.code(400);
|
|
54
|
+
return { error: `Directory does not exist: ${dirPath}`, code: 'DIR_NOT_FOUND' };
|
|
55
|
+
}
|
|
56
|
+
const stat = fs.statSync(dirPath);
|
|
57
|
+
if (!stat.isDirectory()) {
|
|
58
|
+
reply.code(400);
|
|
59
|
+
return { error: `Path is not a directory: ${dirPath}`, code: 'NOT_A_DIRECTORY' };
|
|
60
|
+
}
|
|
61
|
+
// 读取子条目,仅保留目录
|
|
62
|
+
let entries;
|
|
63
|
+
try {
|
|
64
|
+
entries = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
reply.code(403);
|
|
68
|
+
return { error: `Permission denied: ${dirPath}`, code: 'PERMISSION_DENIED' };
|
|
69
|
+
}
|
|
70
|
+
const dirs = entries
|
|
71
|
+
.filter((entry) => {
|
|
72
|
+
try {
|
|
73
|
+
return entry.isDirectory() && !entry.name.startsWith('.');
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
.map((entry) => {
|
|
80
|
+
const fullPath = path.join(dirPath, entry.name);
|
|
81
|
+
const gitPath = path.join(fullPath, '.git');
|
|
82
|
+
let isGitRepo = false;
|
|
83
|
+
try {
|
|
84
|
+
isGitRepo = fs.existsSync(gitPath);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// ignore permission errors
|
|
88
|
+
}
|
|
89
|
+
return { name: entry.name, path: fullPath, isGitRepo };
|
|
90
|
+
})
|
|
91
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
92
|
+
return { current: dirPath, parent: path.dirname(dirPath), dirs };
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
return handleError(error, reply);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
/**
|
|
99
|
+
* GET /validate?path=<dir>
|
|
100
|
+
* 验证路径是否为有效 Git 仓库
|
|
101
|
+
*/
|
|
102
|
+
app.get('/validate', async (request, reply) => {
|
|
103
|
+
try {
|
|
104
|
+
const { path: dirPath } = validateQuerySchema.parse(request.query);
|
|
105
|
+
// 检查路径是否存在
|
|
106
|
+
if (!fs.existsSync(dirPath)) {
|
|
107
|
+
return { valid: false, path: dirPath, error: 'Path does not exist' };
|
|
108
|
+
}
|
|
109
|
+
// 检查是否是目录
|
|
110
|
+
const stat = fs.statSync(dirPath);
|
|
111
|
+
if (!stat.isDirectory()) {
|
|
112
|
+
return { valid: false, path: dirPath, error: 'Path is not a directory' };
|
|
113
|
+
}
|
|
114
|
+
// 检查是否是 Git 仓库
|
|
115
|
+
const gitPath = path.join(dirPath, '.git');
|
|
116
|
+
if (!fs.existsSync(gitPath)) {
|
|
117
|
+
return { valid: false, path: dirPath, error: 'Not a Git repository (no .git found)' };
|
|
118
|
+
}
|
|
119
|
+
return { valid: true, path: dirPath };
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
return handleError(error, reply);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
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,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YAC3C,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC5B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,sCAAsC,EAAE,CAAC;YACxF,CAAC;YAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;QACxC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACnC,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git.d.ts","sourceRoot":"","sources":["../../src/routes/git.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAgF/C,wBAAsB,SAAS,CAAC,GAAG,EAAE,eAAe,iBAmGnD"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod';
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
function handleError(error, reply) {
|
|
5
|
+
if (error instanceof ZodError) {
|
|
6
|
+
reply.code(400);
|
|
7
|
+
return { error: 'Validation failed', code: 'VALIDATION_ERROR', details: error.errors };
|
|
8
|
+
}
|
|
9
|
+
reply.code(500);
|
|
10
|
+
return { error: 'Internal server error', code: 'INTERNAL_ERROR' };
|
|
11
|
+
}
|
|
12
|
+
/** execFile promisified with timeout */
|
|
13
|
+
function execGit(cwd, args) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
execFile('git', args, { cwd, timeout: 10_000, maxBuffer: 10 * 1024 * 1024 }, (err, stdout) => {
|
|
16
|
+
if (err) {
|
|
17
|
+
// git diff returns exit code 1 when there are differences — that's fine
|
|
18
|
+
if (stdout !== undefined && stdout !== '') {
|
|
19
|
+
resolve(stdout);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
reject(err);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
resolve(stdout);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function parseNameStatus(output) {
|
|
30
|
+
if (!output.trim())
|
|
31
|
+
return [];
|
|
32
|
+
return output
|
|
33
|
+
.trim()
|
|
34
|
+
.split('\n')
|
|
35
|
+
.map((line) => {
|
|
36
|
+
// Format: "M\tpath" or "R100\told\tnew"
|
|
37
|
+
const parts = line.split('\t');
|
|
38
|
+
if (parts.length < 2)
|
|
39
|
+
return null;
|
|
40
|
+
const rawStatus = parts[0];
|
|
41
|
+
// Normalize rename status (R100 -> R)
|
|
42
|
+
const status = rawStatus.startsWith('R') ? 'R' : rawStatus;
|
|
43
|
+
const filePath = parts.length >= 3 ? parts[2] : parts[1];
|
|
44
|
+
return { status, path: filePath };
|
|
45
|
+
})
|
|
46
|
+
.filter((entry) => entry !== null);
|
|
47
|
+
}
|
|
48
|
+
const changesQuerySchema = z.object({
|
|
49
|
+
workingDir: z
|
|
50
|
+
.string()
|
|
51
|
+
.min(1, 'workingDir is required')
|
|
52
|
+
.refine((v) => path.isAbsolute(v), { message: 'workingDir must be absolute' })
|
|
53
|
+
.refine((v) => !v.split(path.sep).includes('..'), {
|
|
54
|
+
message: 'Path traversal (..) is not allowed',
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
const diffQuerySchema = z.object({
|
|
58
|
+
workingDir: z
|
|
59
|
+
.string()
|
|
60
|
+
.min(1, 'workingDir is required')
|
|
61
|
+
.refine((v) => path.isAbsolute(v), { message: 'workingDir must be absolute' })
|
|
62
|
+
.refine((v) => !v.split(path.sep).includes('..'), {
|
|
63
|
+
message: 'Path traversal (..) is not allowed',
|
|
64
|
+
}),
|
|
65
|
+
path: z
|
|
66
|
+
.string()
|
|
67
|
+
.min(1, 'path is required')
|
|
68
|
+
.refine((v) => !v.split('/').includes('..'), {
|
|
69
|
+
message: 'Path traversal (..) is not allowed',
|
|
70
|
+
}),
|
|
71
|
+
type: z.enum(['uncommitted', 'committed']),
|
|
72
|
+
});
|
|
73
|
+
export async function gitRoutes(app) {
|
|
74
|
+
app.setErrorHandler((error, _request, reply) => {
|
|
75
|
+
return handleError(error, reply);
|
|
76
|
+
});
|
|
77
|
+
/**
|
|
78
|
+
* GET /changes?workingDir=/path/to/worktree
|
|
79
|
+
* Returns uncommitted and committed changes relative to main.
|
|
80
|
+
*/
|
|
81
|
+
app.get('/changes', async (request, reply) => {
|
|
82
|
+
try {
|
|
83
|
+
const { workingDir } = changesQuerySchema.parse(request.query);
|
|
84
|
+
// Uncommitted changes (staged + unstaged vs HEAD)
|
|
85
|
+
let uncommitted = [];
|
|
86
|
+
try {
|
|
87
|
+
const output = await execGit(workingDir, ['diff', '--name-status', 'HEAD']);
|
|
88
|
+
uncommitted = parseNameStatus(output);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// If HEAD doesn't exist (initial commit), try against empty tree
|
|
92
|
+
try {
|
|
93
|
+
const output = await execGit(workingDir, ['diff', '--name-status']);
|
|
94
|
+
uncommitted = parseNameStatus(output);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// ignore
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Also include untracked files
|
|
101
|
+
try {
|
|
102
|
+
const untrackedOutput = await execGit(workingDir, [
|
|
103
|
+
'ls-files', '--others', '--exclude-standard',
|
|
104
|
+
]);
|
|
105
|
+
if (untrackedOutput.trim()) {
|
|
106
|
+
const untrackedFiles = untrackedOutput.trim().split('\n');
|
|
107
|
+
for (const f of untrackedFiles) {
|
|
108
|
+
uncommitted.push({ status: 'A', path: f });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// ignore
|
|
114
|
+
}
|
|
115
|
+
// Committed changes (main...HEAD)
|
|
116
|
+
let committed = [];
|
|
117
|
+
try {
|
|
118
|
+
const output = await execGit(workingDir, ['diff', '--name-status', 'main...HEAD']);
|
|
119
|
+
committed = parseNameStatus(output);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// main branch might not exist — try origin/main
|
|
123
|
+
try {
|
|
124
|
+
const output = await execGit(workingDir, ['diff', '--name-status', 'origin/main...HEAD']);
|
|
125
|
+
committed = parseNameStatus(output);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// ignore — no main branch to compare against
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return { uncommitted, committed };
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
return handleError(error, reply);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
/**
|
|
138
|
+
* GET /diff?workingDir=/path&path=src/index.ts&type=uncommitted|committed
|
|
139
|
+
* Returns the diff content for a single file.
|
|
140
|
+
*/
|
|
141
|
+
app.get('/diff', async (request, reply) => {
|
|
142
|
+
try {
|
|
143
|
+
const { workingDir, path: filePath, type } = diffQuerySchema.parse(request.query);
|
|
144
|
+
let diff = '';
|
|
145
|
+
try {
|
|
146
|
+
if (type === 'uncommitted') {
|
|
147
|
+
diff = await execGit(workingDir, ['diff', '--', filePath]);
|
|
148
|
+
// If no staged/unstaged diff, the file might be untracked — show full content as addition
|
|
149
|
+
if (!diff.trim()) {
|
|
150
|
+
diff = await execGit(workingDir, ['diff', '--no-index', '/dev/null', filePath]).catch(() => '');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
diff = await execGit(workingDir, ['diff', 'main...HEAD', '--', filePath]);
|
|
155
|
+
if (!diff.trim()) {
|
|
156
|
+
diff = await execGit(workingDir, ['diff', 'origin/main...HEAD', '--', filePath]).catch(() => '');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// ignore — return empty diff
|
|
162
|
+
}
|
|
163
|
+
return { diff };
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
return handleError(error, reply);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=git.js.map
|