πŸ“„ File detail

utils/tempfile.ts

🧩 .tsπŸ“ 32 linesπŸ’Ύ 1,170 bytesπŸ“ text
← Back to All Files

🎯 Use case

This file lives under β€œutils/”, which covers cross-cutting helpers (shell, tempfiles, settings, messages, process input, …). On the API surface it exposes generateTempFilePath β€” mainly functions, hooks, or classes. Dependencies touch crypto, Node OS/process metadata, and Node path helpers.

Generated from folder role, exports, dependency roots, and inline comments β€” not hand-reviewed for every path.

🧠 Inline summary

import { createHash, randomUUID } from 'crypto' import { tmpdir } from 'os' import { join } from 'path' /**

πŸ“€ Exports (heuristic)

  • generateTempFilePath

πŸ“š External import roots

Package roots from from "…" (relative paths omitted).

  • crypto
  • os
  • path

πŸ–₯️ Source preview

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}`)
}