mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-07 22:10:21 +08:00
feat(agents): add ensurePathsExist method to validate and create accessible paths
This commit is contained in:
parent
49f9dff9da
commit
1c2211aefb
@ -161,6 +161,50 @@ export abstract class BaseService {
|
|||||||
return deserialized
|
return deserialized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected ensurePathsExist(paths?: string[]): void {
|
||||||
|
if (!paths?.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const rawPath of paths) {
|
||||||
|
if (!rawPath) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolvedPath = path.resolve(rawPath)
|
||||||
|
|
||||||
|
let stats: fs.Stats | null = null
|
||||||
|
try {
|
||||||
|
if (fs.existsSync(resolvedPath)) {
|
||||||
|
stats = fs.statSync(resolvedPath)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn('Failed to inspect accessible path', {
|
||||||
|
path: rawPath,
|
||||||
|
error: error instanceof Error ? error.message : String(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const looksLikeFile =
|
||||||
|
(stats && stats.isFile()) ||
|
||||||
|
(!stats && path.extname(resolvedPath) !== '' && !resolvedPath.endsWith(path.sep))
|
||||||
|
|
||||||
|
const directoryToEnsure = looksLikeFile ? path.dirname(resolvedPath) : resolvedPath
|
||||||
|
|
||||||
|
if (!fs.existsSync(directoryToEnsure)) {
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(directoryToEnsure, { recursive: true })
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('Failed to create accessible path directory', {
|
||||||
|
path: directoryToEnsure,
|
||||||
|
error: error instanceof Error ? error.message : String(error)
|
||||||
|
})
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Force re-initialization (for development/testing)
|
* Force re-initialization (for development/testing)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -43,6 +43,8 @@ export class AgentService extends BaseService {
|
|||||||
req.accessible_paths = [defaultPath]
|
req.accessible_paths = [defaultPath]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.ensurePathsExist(req.accessible_paths)
|
||||||
|
|
||||||
const serializedReq = this.serializeJsonFields(req)
|
const serializedReq = this.serializeJsonFields(req)
|
||||||
|
|
||||||
const insertData: InsertAgentRow = {
|
const insertData: InsertAgentRow = {
|
||||||
@ -126,6 +128,10 @@ export class AgentService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const now = new Date().toISOString()
|
const now = new Date().toISOString()
|
||||||
|
|
||||||
|
if (updates.accessible_paths) {
|
||||||
|
this.ensurePathsExist(updates.accessible_paths)
|
||||||
|
}
|
||||||
const serializedUpdates = this.serializeJsonFields(updates)
|
const serializedUpdates = this.serializeJsonFields(updates)
|
||||||
|
|
||||||
const updateData: Partial<AgentRow> = {
|
const updateData: Partial<AgentRow> = {
|
||||||
|
|||||||
@ -50,6 +50,8 @@ export class SessionService extends BaseService {
|
|||||||
...req
|
...req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.ensurePathsExist(sessionData.accessible_paths)
|
||||||
|
|
||||||
const serializedData = this.serializeJsonFields(sessionData)
|
const serializedData = this.serializeJsonFields(sessionData)
|
||||||
|
|
||||||
const insertData: InsertSessionRow = {
|
const insertData: InsertSessionRow = {
|
||||||
@ -168,6 +170,10 @@ export class SessionService extends BaseService {
|
|||||||
// We'll skip this validation for now to avoid circular dependencies
|
// We'll skip this validation for now to avoid circular dependencies
|
||||||
|
|
||||||
const now = new Date().toISOString()
|
const now = new Date().toISOString()
|
||||||
|
|
||||||
|
if (updates.accessible_paths) {
|
||||||
|
this.ensurePathsExist(updates.accessible_paths)
|
||||||
|
}
|
||||||
const serializedUpdates = this.serializeJsonFields(updates)
|
const serializedUpdates = this.serializeJsonFields(updates)
|
||||||
|
|
||||||
const updateData: Partial<SessionRow> = {
|
const updateData: Partial<SessionRow> = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user