MCPcopy Create free account
hub / github.com/codeaashu/claude-code / useInputBuffer

Function useInputBuffer

src/hooks/useInputBuffer.ts:27–132  ·  view source on GitHub ↗
({
  maxBufferSize,
  debounceMs,
}: UseInputBufferProps)

Source from the content-addressed store, hash-verified

25}
26
27export function useInputBuffer({
28 maxBufferSize,
29 debounceMs,
30}: UseInputBufferProps): UseInputBufferResult {
31 const [buffer, setBuffer] = useState<BufferEntry[]>([])
32 const [currentIndex, setCurrentIndex] = useState(-1)
33 const lastPushTime = useRef<number>(0)
34 const pendingPush = useRef<ReturnType<typeof setTimeout> | null>(null)
35
36 const pushToBuffer = useCallback(
37 (
38 text: string,
39 cursorOffset: number,
40 pastedContents: Record<number, PastedContent> = {},
41 ) => {
42 const now = Date.now()
43
44 // Clear any pending push
45 if (pendingPush.current) {
46 clearTimeout(pendingPush.current)
47 pendingPush.current = null
48 }
49
50 // Debounce rapid changes
51 if (now - lastPushTime.current < debounceMs) {
52 pendingPush.current = setTimeout(
53 pushToBuffer,
54 debounceMs,
55 text,
56 cursorOffset,
57 pastedContents,
58 )
59 return
60 }
61
62 lastPushTime.current = now
63
64 setBuffer(prevBuffer => {
65 // If we're not at the end of the buffer, truncate everything after current position
66 const newBuffer =
67 currentIndex >= 0 ? prevBuffer.slice(0, currentIndex + 1) : prevBuffer
68
69 // Don't add if it's the same as the last entry
70 const lastEntry = newBuffer[newBuffer.length - 1]
71 if (lastEntry && lastEntry.text === text) {
72 return newBuffer
73 }
74
75 // Add new entry
76 const updatedBuffer = [
77 ...newBuffer,
78 { text, cursorOffset, pastedContents, timestamp: now },
79 ]
80
81 // Limit buffer size
82 if (updatedBuffer.length > maxBufferSize) {
83 return updatedBuffer.slice(-maxBufferSize)
84 }

Callers 1

PromptInputFunction · 0.85

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected