feat: GET /permission 响应格式对齐前端 PermissionRequest 类型
- 工具名 PascalCase 映射为小写 permission key(匹配 i18n) - 从 tool input 提取 patterns(file_path/command 等) - 补齐 sessionID/id/tool/callID/metadata/always 字段
This commit is contained in:
parent
9c3441331c
commit
0393ce927d
|
|
@ -2,11 +2,63 @@ import { Hono } from 'hono'
|
||||||
import type { SessionManager } from '../sessionManager.js'
|
import type { SessionManager } from '../sessionManager.js'
|
||||||
import { notFound } from '../errors.js'
|
import { notFound } from '../errors.js'
|
||||||
|
|
||||||
|
const TOOL_NAME_TO_PERMISSION: Record<string, string> = {
|
||||||
|
Read: 'read',
|
||||||
|
Edit: 'edit',
|
||||||
|
Write: 'edit',
|
||||||
|
Glob: 'glob',
|
||||||
|
Grep: 'grep',
|
||||||
|
LS: 'list',
|
||||||
|
Bash: 'bash',
|
||||||
|
PowerShell: 'bash',
|
||||||
|
Agent: 'task',
|
||||||
|
WebFetch: 'webfetch',
|
||||||
|
WebSearch: 'websearch',
|
||||||
|
TodoRead: 'todoread',
|
||||||
|
TodoWrite: 'todowrite',
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractPatterns(input: Record<string, unknown>): string[] {
|
||||||
|
const patterns: string[] = []
|
||||||
|
for (const key of ['file_path', 'path', 'pattern', 'glob'] as const) {
|
||||||
|
const v = typeof input[key] === 'string' ? input[key] as string : ''
|
||||||
|
if (v) patterns.push(v)
|
||||||
|
}
|
||||||
|
const cmd = typeof input.command === 'string' ? input.command as string : ''
|
||||||
|
if (cmd) patterns.push(cmd)
|
||||||
|
return patterns
|
||||||
|
}
|
||||||
|
|
||||||
|
function toPermissionKey(toolName: string): string {
|
||||||
|
return TOOL_NAME_TO_PERMISSION[toolName] ?? toolName.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatPermission(perm: {
|
||||||
|
requestId: string
|
||||||
|
sessionId: string
|
||||||
|
toolName: string
|
||||||
|
toolUseId: string
|
||||||
|
input: Record<string, unknown>
|
||||||
|
}) {
|
||||||
|
return {
|
||||||
|
id: perm.requestId,
|
||||||
|
sessionID: perm.sessionId,
|
||||||
|
permission: toPermissionKey(perm.toolName),
|
||||||
|
patterns: extractPatterns(perm.input),
|
||||||
|
metadata: { input: perm.input },
|
||||||
|
always: [] as string[],
|
||||||
|
tool: {
|
||||||
|
messageID: '',
|
||||||
|
callID: perm.toolUseId,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createPermissionRoutes(sessionManager: SessionManager): Hono {
|
export function createPermissionRoutes(sessionManager: SessionManager): Hono {
|
||||||
return new Hono()
|
return new Hono()
|
||||||
.get('/permission', c => {
|
.get('/permission', c => {
|
||||||
const permissions = sessionManager.getAllPendingPermissions()
|
const permissions = sessionManager.getAllPendingPermissions()
|
||||||
return c.json({ permissions })
|
return c.json({ permissions: permissions.map(formatPermission) })
|
||||||
})
|
})
|
||||||
.post('/permission/:requestID/reply', async c => {
|
.post('/permission/:requestID/reply', async c => {
|
||||||
const requestId = c.req.param('requestID')
|
const requestId = c.req.param('requestID')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user