Files
deepseek-harness/packages/client/file-upload/src/http-route.ts
T
Yichen Jiang 9be99a0e28 feat(web): upload composer images when they are added
An ordinary Session's composer now uploads each image draft through the
background upload queue as soon as it is added. The raw upload route stages
the body as a prompt image when the request names its media type: the Host
reads at most the per-image byte limit, then validates, normalizes, and
stores the image as prompt admission would. The prompt cites the receipt in
a file part; prompt admission resolves it to the stored image, applies the
image-input check, and counts stored images toward maxImagesPerMessage.

Send waits for every image receipt, and a failed image upload turns its
thumbnail into a retry control. Commands and subagent continuations keep
encoding images inline.
2026-10-21 13:50:19 +08:00

87 lines
3.0 KiB
TypeScript

/** Authenticated raw-byte upload route registered on the Connection fetch registry. */
import { brandString } from '@deepseek-ai/dsh-brand'
import type { SessionId } from '@deepseek-ai/dsh-session'
import { remoteErrorOf } from '@deepseek-ai/dsh-typert-protocol'
import type { FileUploads } from './index.ts'
import type { FileUploadValue, ImageUploadValue } from './types.ts'
type FileUploadHttpResult =
| { readonly ok: true; readonly value: FileUploadValue | ImageUploadValue }
| {
readonly ok: false
readonly error: { readonly code: string; readonly message: string; readonly details: object }
}
/**
* Handle one authenticated raw-byte upload. A `mediaType` query parameter
* stages the body as a prompt image; without it the body is stored verbatim.
* @param service - Host upload service receiving streamed bytes.
* @param request - authenticated HTTP request from Connection.
* @returns JSON result using HTTP status 200 after request validation.
*/
export async function handleFileUploadHttp(service: FileUploads, request: Request): Promise<Response> {
if (request.method !== 'POST') {
return new Response(null, { status: 405, headers: { allow: 'POST' } })
}
const mediaType = request.headers.get('content-type')?.split(';', 1)[0]?.trim().toLowerCase()
if (mediaType !== 'application/octet-stream') {
return new Response('content type must be application/octet-stream', { status: 415 })
}
const url = new URL(request.url)
const sessionId = url.searchParams.get('sessionId')
if (sessionId === null || sessionId === '') {
return new Response('sessionId is required', { status: 400 })
}
const name = url.searchParams.get('name') ?? undefined
const imageMediaType = url.searchParams.get('mediaType') ?? undefined
const upload = {
sessionId: brandString<SessionId>(sessionId),
data: requestBodyChunks(request.body),
signal: request.signal,
...(name === undefined ? {} : { name }),
}
let result: FileUploadHttpResult
try {
result = {
ok: true,
value: imageMediaType === undefined
? await service.uploadStream(upload)
: await service.uploadImageStream({ ...upload, mediaType: imageMediaType }),
}
} catch (error) {
const failure = remoteErrorOf(error)
result = {
ok: false,
error: failure !== undefined
? { code: failure.code, message: failure.message, details: failure.details }
: {
code: 'gateway/internal',
message: error instanceof Error ? error.message : String(error),
details: {},
},
}
}
return new Response(JSON.stringify(result), {
status: 200,
headers: {
'content-type': 'application/json; charset=utf-8',
'cache-control': 'no-store',
},
})
}
async function* requestBodyChunks(body: ReadableStream<Uint8Array> | null): AsyncIterable<Uint8Array> {
if (body === null) return
const reader = body.getReader()
try {
while (true) {
const chunk = await reader.read()
if (chunk.done) return
yield chunk.value
}
} finally {
reader.releaseLock()
}
}