forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMaybeTruncateInput.ts
More file actions
58 lines (51 loc) · 1.43 KB
/
Copy pathuseMaybeTruncateInput.ts
File metadata and controls
58 lines (51 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { useEffect, useState } from 'react'
import type { PastedContent } from 'src/utils/config.js'
import { maybeTruncateInput } from './inputPaste.js'
type Props = {
input: string
pastedContents: Record<number, PastedContent>
onInputChange: (input: string) => void
setCursorOffset: (offset: number) => void
setPastedContents: (contents: Record<number, PastedContent>) => void
}
export function useMaybeTruncateInput({
input,
pastedContents,
onInputChange,
setCursorOffset,
setPastedContents,
}: Props) {
// Track if we've initialized this specific input value
const [hasAppliedTruncationToInput, setHasAppliedTruncationToInput] =
useState(false)
// Process input for truncation and pasted images from MessageSelector.
useEffect(() => {
if (hasAppliedTruncationToInput) {
return
}
if (input.length <= 10_000) {
return
}
const { newInput, newPastedContents } = maybeTruncateInput(
input,
pastedContents,
)
onInputChange(newInput)
setCursorOffset(newInput.length)
setPastedContents(newPastedContents)
setHasAppliedTruncationToInput(true)
}, [
input,
hasAppliedTruncationToInput,
pastedContents,
onInputChange,
setPastedContents,
setCursorOffset,
])
// Reset hasInitializedInput when input is cleared (e.g., after submission)
useEffect(() => {
if (input === '') {
setHasAppliedTruncationToInput(false)
}
}, [input])
}