-
Notifications
You must be signed in to change notification settings - Fork 894
feat(kap-server): add GET /api/v1/fs:content endpoint for host files #2012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Add a GET /api/v1/fs:content server endpoint that serves any file on the host by absolute path as raw content with Content-Type, ETag, and Range support. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /** | ||
| * File content metadata helpers — binary detection, line counting, etag, and | ||
| * extension-based mime / language guessing. | ||
| * | ||
| * Shared by the fs edge domains (`sessionFs`) and the kap-server fs routes so | ||
| * every read-style surface classifies and labels file content the same way. | ||
| * Pure functions over bytes, text, and stat-like shapes; no io happens here. | ||
| * Binary detection samples the leading `FS_BINARY_SAMPLE_BYTES` of a file and | ||
| * flags it as binary when the non-printable fraction exceeds | ||
| * `FS_BINARY_NONPRINTABLE_FRACTION`; etags are built from any stat-like shape | ||
| * carrying `size` / `mtimeMs` / `ino` (`FileMetaStat`, satisfied by | ||
| * `HostFileStat`). | ||
| */ | ||
|
|
||
| import { extname } from 'node:path'; | ||
|
|
||
| export const FS_BINARY_SAMPLE_BYTES = 4096; | ||
| export const FS_BINARY_NONPRINTABLE_FRACTION = 0.3; | ||
|
|
||
| export interface FileMetaStat { | ||
| readonly size: number; | ||
| readonly mtimeMs?: number; | ||
| readonly ino?: number; | ||
| } | ||
|
|
||
| export function detectBinary(buf: Uint8Array): boolean { | ||
| if (buf.length === 0) return false; | ||
| let nonPrintable = 0; | ||
| for (let i = 0; i < buf.length; i++) { | ||
| const b = buf[i]!; | ||
| if (b === 0) return true; | ||
| if (b === 9 || b === 10 || b === 13) continue; | ||
| if (b >= 32 && b <= 126) continue; | ||
| nonPrintable++; | ||
| } | ||
| return nonPrintable / buf.length > FS_BINARY_NONPRINTABLE_FRACTION; | ||
| } | ||
|
|
||
| export function countLines(text: string): number { | ||
| if (text.length === 0) return 0; | ||
| let n = 1; | ||
| for (let i = 0; i < text.length; i++) { | ||
| if (text.charCodeAt(i) === 10) n++; | ||
| } | ||
| if (text.charCodeAt(text.length - 1) === 10) n--; | ||
| return Math.max(0, n); | ||
| } | ||
|
|
||
| export function buildEtag(st: FileMetaStat): string { | ||
| const mtime = Math.floor(st.mtimeMs ?? 0); | ||
| const ino = st.ino ?? 0; | ||
| return [mtime.toString(36), st.size.toString(36), ino.toString(36)].join('-'); | ||
| } | ||
|
|
||
| const EXT_TO_MIME: Readonly<Record<string, string>> = { | ||
| '.ts': 'text/typescript', | ||
| '.tsx': 'text/typescript', | ||
| '.js': 'text/javascript', | ||
| '.jsx': 'text/javascript', | ||
| '.mjs': 'text/javascript', | ||
| '.cjs': 'text/javascript', | ||
| '.json': 'application/json', | ||
| '.md': 'text/markdown', | ||
| '.html': 'text/html', | ||
| '.css': 'text/css', | ||
| '.svg': 'image/svg+xml', | ||
| '.png': 'image/png', | ||
| '.jpg': 'image/jpeg', | ||
| '.jpeg': 'image/jpeg', | ||
| '.gif': 'image/gif', | ||
| '.pdf': 'application/pdf', | ||
| '.yaml': 'text/yaml', | ||
| '.yml': 'text/yaml', | ||
| '.toml': 'application/toml', | ||
| '.sh': 'text/x-shellscript', | ||
| '.py': 'text/x-python', | ||
| '.rs': 'text/rust', | ||
| '.go': 'text/x-go', | ||
| }; | ||
|
|
||
| export function guessMime(path: string, isBinary: boolean): string { | ||
| const ext = extname(path).toLowerCase(); | ||
| const mapped = EXT_TO_MIME[ext]; | ||
| if (mapped !== undefined) return mapped; | ||
| return isBinary ? 'application/octet-stream' : 'text/plain'; | ||
| } | ||
|
|
||
| const EXT_TO_LANGUAGE: Readonly<Record<string, string>> = { | ||
| '.ts': 'typescript', | ||
| '.tsx': 'typescriptreact', | ||
| '.js': 'javascript', | ||
| '.jsx': 'javascriptreact', | ||
| '.mjs': 'javascript', | ||
| '.cjs': 'javascript', | ||
| '.json': 'json', | ||
| '.md': 'markdown', | ||
| '.html': 'html', | ||
| '.css': 'css', | ||
| '.yaml': 'yaml', | ||
| '.yml': 'yaml', | ||
| '.toml': 'toml', | ||
| '.sh': 'shellscript', | ||
| '.py': 'python', | ||
| '.rs': 'rust', | ||
| '.go': 'go', | ||
| }; | ||
|
|
||
| export function guessLanguageId(path: string): string | undefined { | ||
| return EXT_TO_LANGUAGE[extname(path).toLowerCase()]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * HTTP helpers for raw-content routes — request header access and single-range | ||
| * `Range` header parsing shared by the file download / content endpoints. | ||
| */ | ||
|
|
||
| export function pickHeader( | ||
| headers: Record<string, unknown>, | ||
| name: string, | ||
| ): string | undefined { | ||
| const v = headers[name]; | ||
| if (v === undefined) return undefined; | ||
| return Array.isArray(v) ? (v[0] as string | undefined) : (v as string); | ||
| } | ||
|
|
||
| /** | ||
| * Parse a single-range `bytes=` header against a known size. Returns null for | ||
| * absent, malformed, multi-range, or unsatisfiable specs (callers then serve | ||
| * the full body with 200). | ||
| */ | ||
| export function parseRangeHeader( | ||
| raw: string | undefined, | ||
| size: number, | ||
| ): { start: number; end: number; length: number } | null { | ||
| if (raw === undefined) return null; | ||
| if (!raw.startsWith('bytes=')) return null; | ||
| const spec = raw.slice('bytes='.length); | ||
| if (spec.includes(',')) return null; | ||
| const dash = spec.indexOf('-'); | ||
| if (dash < 0) return null; | ||
| const leftRaw = spec.slice(0, dash); | ||
| const rightRaw = spec.slice(dash + 1); | ||
| if (leftRaw === '' && rightRaw === '') return null; | ||
| let start: number; | ||
| let end: number; | ||
| if (leftRaw === '') { | ||
| const suffix = Number.parseInt(rightRaw, 10); | ||
| if (!Number.isFinite(suffix) || suffix <= 0) return null; | ||
| start = Math.max(0, size - suffix); | ||
| end = size - 1; | ||
| } else { | ||
| const a = Number.parseInt(leftRaw, 10); | ||
| if (!Number.isFinite(a) || a < 0) return null; | ||
| start = a; | ||
| if (rightRaw === '') { | ||
| end = size - 1; | ||
| } else { | ||
| const b = Number.parseInt(rightRaw, 10); | ||
| if (!Number.isFinite(b) || b < a) return null; | ||
| end = Math.min(b, size - 1); | ||
| } | ||
| } | ||
| if (start >= size || start > end) return null; | ||
| return { start, end, length: end - start + 1 }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packages/agent-core-v2/AGENTS.mdrequires package comments to be header-only and to start with<domain> domain (Ln) — .... This new helper's top block starts with a generic description instead, so it violates the documented convention for touched agent-core-v2 files; please rewrite it to the required domain identity header and avoid additional symbol-level comments below.Useful? React with 👍 / 👎.