feat(agent): allow create_file tool to create files directly from content

Add an optional 'data' field ({name, content}) to the create_file agent tool
so agents can create files without a local file (e.g. when bash is not
available). The existing 'path' upload mode is preserved; exactly one of
'path' or 'data' must be provided.

Extract mime detection helpers (getFileMimeType, readFileMimeType) into a
new file-mime module, keeping utils/mime.ts free of fs/path imports so it
stays compatible with the Temporal workflow bundle (agent-autofill imports
getProxyType from it). Refactor create_version to reuse the extracted
helpers.

Content uploads reuse s3Service.putObject; mime type is derived from the
filename extension with a text/plain fallback, and the name is sanitized
for the S3 key.
This commit is contained in:
yiling
2026-08-04 21:16:33 +08:00
parent 5ef763fbae
commit 07ad0d2f65
9 changed files with 368 additions and 92 deletions
+61
View File
@@ -0,0 +1,61 @@
import { describe, it, expect } from 'vitest'
import { getFileMimeType, readFileMimeType } from './file-mime'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
function withTempFile(content: string | Buffer, filename: string, fn: (filePath: string) => void) {
const filePath = path.join(os.tmpdir(), filename)
fs.writeFileSync(filePath, content)
try {
fn(filePath)
} finally {
fs.unlinkSync(filePath)
}
}
describe('getFileMimeType', () => {
it('should detect binary mime type from content signature', () => {
const png = new Uint8Array([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44,
0x52,
])
expect(getFileMimeType(png, 'photo.unknown')).toBe('image/png')
})
it('should map known extensions when no signature is available', () => {
expect(getFileMimeType(null, 'notes.md')).toBe('text/markdown')
expect(getFileMimeType(null, 'doc.pdf')).toBe('application/pdf')
expect(getFileMimeType(null, 'image.PNG')).toBe('image/png')
})
it('should fall back to application/octet-stream for unknown extensions', () => {
expect(getFileMimeType(null, 'script.ts')).toBe('application/octet-stream')
})
it('should honor a custom fallback for unknown extensions', () => {
expect(getFileMimeType(null, 'script.ts', 'text/plain')).toBe('text/plain')
})
})
describe('readFileMimeType', () => {
it('should sniff binary file content', () => {
const pngBytes = Buffer.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44,
0x52, 0x00, 0x00, 0x00, 0x00,
])
withTempFile(pngBytes, 'file-mime-test-image.png', (filePath) => {
expect(readFileMimeType(filePath)).toBe('image/png')
})
})
it('should map text files by extension', () => {
withTempFile('# Hello', 'file-mime-test-notes.md', (filePath) => {
expect(readFileMimeType(filePath)).toBe('text/markdown')
})
})
it('should fall back to octet-stream for missing files', () => {
expect(readFileMimeType('/nonexistent/file.xyz')).toBe('application/octet-stream')
})
})
+57
View File
@@ -0,0 +1,57 @@
import * as fs from 'fs'
import * as path from 'path'
import { detectSupportedMimeType } from './mime'
const EXTENSION_MIME_TYPES: Record<string, string> = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.svg': 'image/svg+xml',
'.mp4': 'video/mp4',
'.mov': 'video/quicktime',
'.pdf': 'application/pdf',
'.txt': 'text/plain',
'.md': 'text/markdown',
'.markdown': 'text/markdown',
}
/**
* Resolve a mime type from an optional content signature and a filename.
* Content signature sniffing (binary formats) takes priority; otherwise the
* filename extension mapping is used, falling back to `fallback` (defaults to
* `application/octet-stream`).
*/
export function getFileMimeType(
buffer: Uint8Array | null,
filename: string,
fallback: string = 'application/octet-stream',
): string {
if (buffer) {
const detected = detectSupportedMimeType(buffer)
if (detected) return detected
}
const ext = path.extname(filename).toLowerCase()
return EXTENSION_MIME_TYPES[ext] || fallback
}
/**
* Detect the mime type of a file on disk by sniffing its first bytes and
* falling back to extension mapping.
*/
export function readFileMimeType(filePath: string): string {
try {
const fd = fs.openSync(filePath, 'r')
try {
const buffer = Buffer.alloc(4100)
const bytesRead = fs.readSync(fd, buffer, 0, 4100, 0)
return getFileMimeType(new Uint8Array(buffer.subarray(0, bytesRead)), filePath)
} finally {
fs.closeSync(fd)
}
} catch {
/* Ignore read errors and fall back to extension mapping */
return getFileMimeType(null, filePath)
}
}