Files
opencode-manager/backend/src/services/session-artifacts.ts
T
Chris Scott 5e2452172c fix(artifacts): render artifacts from the checked path and serve raw files once
- show_artifact returns the validated path as tool metadata; the chat card
  renders from it and maps repo paths with getWorkspaceFilePath, so local
  repos and moved sessions load the right file
- serve .htm as text/html
- raw file route stats and reads once and adds ETag/Last-Modified with 304
- reuse isPathWithinRoot from utils/fs-safe
- move readShowArtifactCall to frontend/src/lib/artifactTool
2026-10-12 16:43:04 -04:00

68 lines
2.4 KiB
TypeScript

import { promises as fs } from 'node:fs'
import path from 'node:path'
import { ARTIFACT_LIMITS } from '@opencode-manager/shared/schemas'
import type { OpenCodeClient } from './opencode/client'
import { ServiceError } from '../utils/service-error'
import { isPathWithinRoot } from '../utils/fs-safe'
const ARTIFACT_EXTENSIONS = new Set(['.html', '.htm'])
export class SessionArtifactError extends ServiceError {
constructor(message: string, status: 400 | 404 | 413) {
super(message, status)
this.name = 'SessionArtifactError'
}
}
/**
* Resolves an `ocm` `show_artifact` path to the absolute path of a self-contained HTML file inside the session
* directory, rejecting paths that escape it (including through symlinks), non-HTML files and oversized files.
* Symlinks are resolved for validation only; the returned path stays lexical so callers can map it by the
* session directory or repo prefix they were given.
*/
export async function resolveSessionArtifactFile(
openCodeClient: OpenCodeClient,
sessionId: string,
artifactPath: string,
): Promise<string> {
const session = await openCodeClient.api.session.get({ sessionID: sessionId })
const sessionDirectory = session.location.directory
let realSessionDirectory: string
try {
realSessionDirectory = await fs.realpath(sessionDirectory)
} catch {
throw new SessionArtifactError(`Artifact path must be inside the session directory ${sessionDirectory}`, 400)
}
const resolved = path.isAbsolute(artifactPath) ? path.resolve(artifactPath) : path.resolve(sessionDirectory, artifactPath)
let realFile: string
try {
realFile = await fs.realpath(resolved)
} catch {
throw new SessionArtifactError(`Artifact file not found: ${artifactPath}`, 404)
}
if (!isPathWithinRoot(realSessionDirectory, realFile)) {
throw new SessionArtifactError(`Artifact path must be inside the session directory ${sessionDirectory}`, 400)
}
const stats = await fs.stat(realFile).catch(() => {
throw new SessionArtifactError(`Artifact file not found: ${artifactPath}`, 404)
})
if (!stats.isFile()) {
throw new SessionArtifactError(`Artifact file not found: ${artifactPath}`, 404)
}
if (!ARTIFACT_EXTENSIONS.has(path.extname(realFile).toLowerCase())) {
throw new SessionArtifactError('Artifact must be an .html file', 400)
}
if (stats.size > ARTIFACT_LIMITS.FILE_MAX_BYTES) {
throw new SessionArtifactError('Artifact file exceeds 5 MB', 413)
}
return resolved
}