tempfile.ts
utils/tempfile.ts
32
Lines
1170
Bytes
1
Exports
3
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with file-tools. It contains 32 lines, 3 detected imports, and 1 detected exports.
Important relationships
Detected exports
generateTempFilePath
Keywords
pathprefixfileextensionoptionscontenthashparamcreatehashrandomuuidtmpdir
Detected imports
cryptoospath
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { createHash, randomUUID } from 'crypto'
import { tmpdir } from 'os'
import { join } from 'path'
/**
* Generate a temporary file path.
*
* @param prefix Optional prefix for the temp file name
* @param extension Optional file extension (defaults to '.md')
* @param options.contentHash When provided, the identifier is derived from a
* SHA-256 hash of this string (first 16 hex chars). This produces a path
* that is stable across process boundaries — any process with the same
* content will get the same path. Use this when the path ends up in content
* sent to the Anthropic API (e.g., sandbox deny lists in tool descriptions),
* because a random UUID would change on every subprocess spawn and
* invalidate the prompt cache prefix.
* @returns Temp file path
*/
export function generateTempFilePath(
prefix: string = 'claude-prompt',
extension: string = '.md',
options?: { contentHash?: string },
): string {
const id = options?.contentHash
? createHash('sha256')
.update(options.contentHash)
.digest('hex')
.slice(0, 16)
: randomUUID()
return join(tmpdir(), `${prefix}-${id}${extension}`)
}