fix: default to user home dir when no workDir selected on new session

Previously, creating a new session without selecting a project directory
defaulted to process.cwd(), which in the desktop app resolves to the app
installation path. Now it defaults to os.homedir().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-04-08 21:40:05 +08:00
parent 6aedd6eb53
commit 90b5772fb4
5 changed files with 11 additions and 11 deletions

View File

@ -30,8 +30,8 @@ export const sessionsApi = {
return api.get<MessagesResponse>(`/api/sessions/${sessionId}/messages`)
},
create(workDir: string) {
return api.post<CreateSessionResponse>('/api/sessions', { workDir })
create(workDir?: string) {
return api.post<CreateSessionResponse>('/api/sessions', workDir ? { workDir } : {})
},
delete(sessionId: string) {

View File

@ -48,7 +48,7 @@ export const useSessionStore = create<SessionStore>((set, get) => ({
},
createSession: async (workDir?: string) => {
const { sessionId: id } = await sessionsApi.create(workDir ?? '.')
const { sessionId: id } = await sessionsApi.create(workDir || undefined)
// Refresh session list
await get().fetchSessions()
set({ activeSessionId: id })

View File

@ -154,8 +154,8 @@ async function createSession(req: Request): Promise<Response> {
throw ApiError.badRequest('Invalid JSON body')
}
if (!body.workDir || typeof body.workDir !== 'string') {
throw ApiError.badRequest('workDir (string) is required in request body')
if (body.workDir && typeof body.workDir !== 'string') {
throw ApiError.badRequest('workDir must be a string')
}
const result = await sessionService.createSession(body.workDir)

View File

@ -470,13 +470,12 @@ export class SessionService {
/**
* Create a new session file for the given working directory.
*/
async createSession(workDir: string): Promise<{ sessionId: string }> {
if (!workDir) {
throw ApiError.badRequest('workDir is required')
}
async createSession(workDir?: string): Promise<{ sessionId: string }> {
// Default to user home directory when no workDir specified
const resolvedWorkDir = workDir || os.homedir()
// Resolve to absolute path
const absWorkDir = path.resolve(workDir)
const absWorkDir = path.resolve(resolvedWorkDir)
let stat
try {
stat = await fs.stat(absWorkDir)

View File

@ -8,6 +8,7 @@
import type { ServerWebSocket } from 'bun'
import type { ClientMessage, ServerMessage } from './events.js'
import * as os from 'node:os'
import {
ConversationStartupError,
conversationService,
@ -152,7 +153,7 @@ async function handleUserMessage(
message: Extract<ClientMessage, { type: 'user_message' }>
) {
const { sessionId } = ws.data
let workDir = process.cwd()
let workDir = os.homedir()
// Send thinking status
sendMessage(ws, { type: 'status', state: 'thinking', verb: 'Thinking' })