refactor(api): reuse attachment limits for file responses

This commit is contained in:
_Kerman
2026-09-08 12:55:57 +08:00
parent 88ab8e9133
commit 40c5ef4fc3
12 changed files with 25 additions and 61 deletions
+2 -8
View File
@@ -70,10 +70,6 @@ declare module '@deepseek-ai/cordis' {
export interface Config {
/** Override platform desktop-opener detection. */
readonly nativeOpen?: boolean
/** Inclusive byte limit for images served by `/api/file`. Defaults to `ctx.attachments.imageLimits.maxImageBytes`. */
readonly maxImageBytes?: number
/** Inclusive byte limit for other files served by `/api/file`. Defaults to `ctx.attachments.imageLimits.maxImageBytes`. */
readonly maxFileBytes?: number
}
/** Host integrations replaceable by direct unit tests. */
@@ -101,8 +97,6 @@ export class SessionController extends TypertRemoteService {
static Config: z<Config> = z.object({
nativeOpen: z.boolean(),
maxImageBytes: z.number().step(1).min(1).max(Number.MAX_SAFE_INTEGER),
maxFileBytes: z.number().step(1).min(1).max(Number.MAX_SAFE_INTEGER),
})
private readonly agents: ApiSessionAgentController
@@ -116,7 +110,7 @@ export class SessionController extends TypertRemoteService {
/**
* @param ctx - Host context containing the Session capability assembly.
* @param config - native-opener and media-read deployment policy.
* @param config - native-opener deployment policy.
* @param internals - host integrations replaceable by direct unit tests.
*/
constructor(ctx: Context, config: Config, internals: SessionControllerInternals = {}) {
@@ -141,7 +135,7 @@ export class SessionController extends TypertRemoteService {
this.canOpenPath = internals.canOpenPath
?? (() => config.nativeOpen ?? (internals.openPath !== undefined || canOpenNativePath()))
ctx.plugin(SessionFileReferences)
ctx.plugin(SessionMediaReferences, config)
ctx.plugin(SessionMediaReferences)
ctx.plugin(SessionSkillCatalog)
ctx.on('session/created', (session) => {
@@ -19,7 +19,7 @@ const BASE_HEADERS = {
'Content-Security-Policy': "sandbox; default-src 'none'",
}
async function serveFile(request: Request, fs: FileSystem, limits: { image: number; file: number }): Promise<Response> {
async function serveFile(request: Request, fs: FileSystem, maxBytes: number): Promise<Response> {
const fail = (status: number, text: string): Response =>
new Response(request.method === 'HEAD' ? null : text, { status, headers: BASE_HEADERS })
const path = new URL(request.url).searchParams.get('path')
@@ -28,7 +28,6 @@ async function serveFile(request: Request, fs: FileSystem, limits: { image: numb
try {
const target = await fs.resolve(path, { signal: request.signal })
const mediaType = mime.lookup(target.displayPath) || 'application/octet-stream'
const maxBytes = mediaType.startsWith('image/') ? limits.image : limits.file
const headers: Record<string, string> = {
...BASE_HEADERS,
'Content-Type': mediaType,
@@ -66,14 +65,13 @@ async function serveFile(request: Request, fs: FileSystem, limits: { image: numb
*/
export const SessionMediaReferences = {
inject: ['connection', 'fs', 'attachments'],
apply(ctx: Context, config: { maxImageBytes?: number; maxFileBytes?: number }): void {
const defaults = ctx.attachments.imageLimits.maxImageBytes
const limits = { image: config.maxImageBytes ?? defaults, file: config.maxFileBytes ?? defaults }
apply(ctx: Context): void {
const maxBytes = ctx.attachments.imageLimits.maxImageBytes
ctx.effect(() => ctx.connection.fetch.register({
path: '/api/file',
methods: ['GET', 'HEAD'],
requestBody: 'buffered',
fetch: request => serveFile(request, ctx.fs, limits),
fetch: request => serveFile(request, ctx.fs, maxBytes),
}), 'session-controller: /api/file')
},
}