inputPaste.ts
components/PromptInput/inputPaste.ts
91
Lines
2693
Bytes
2
Exports
2
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 ui-flow. It contains 91 lines, 2 detected imports, and 2 detected exports.
Important relationships
- components/PromptInput/HistorySearchInput.tsx
- components/PromptInput/IssueFlagBanner.tsx
- components/PromptInput/Notifications.tsx
- components/PromptInput/PromptInput.tsx
- components/PromptInput/PromptInputFooter.tsx
- components/PromptInput/PromptInputFooterLeftSide.tsx
- components/PromptInput/PromptInputFooterSuggestions.tsx
- components/PromptInput/PromptInputHelpMenu.tsx
Detected exports
maybeTruncateMessageForInputmaybeTruncateInput
Keywords
textplaceholdercontentnextpasteidtruncatedtextinputtruncatedplaceholderpastedcontentspastedcontentpreview_length
Detected imports
src/history.jssrc/utils/config.js
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 { getPastedTextRefNumLines } from 'src/history.js'
import type { PastedContent } from 'src/utils/config.js'
const TRUNCATION_THRESHOLD = 10000 // Characters before we truncate
const PREVIEW_LENGTH = 1000 // Characters to show at start and end
type TruncatedMessage = {
truncatedText: string
placeholderContent: string
}
/**
* Determines whether the input text should be truncated. If so, it adds a
* truncated text placeholder and neturns
*
* @param text The input text
* @param nextPasteId The reference id to use
* @returns The new text to display and separate placeholder content if applicable.
*/
export function maybeTruncateMessageForInput(
text: string,
nextPasteId: number,
): TruncatedMessage {
// If the text is short enough, return it as-is
if (text.length <= TRUNCATION_THRESHOLD) {
return {
truncatedText: text,
placeholderContent: '',
}
}
// Calculate how much text to keep from start and end
const startLength = Math.floor(PREVIEW_LENGTH / 2)
const endLength = Math.floor(PREVIEW_LENGTH / 2)
// Extract the portions we'll keep
const startText = text.slice(0, startLength)
const endText = text.slice(-endLength)
// Calculate the number of lines that will be truncated
const placeholderContent = text.slice(startLength, -endLength)
const truncatedLines = getPastedTextRefNumLines(placeholderContent)
// Create a placeholder reference similar to pasted text
const placeholderId = nextPasteId
const placeholderRef = formatTruncatedTextRef(placeholderId, truncatedLines)
// Combine the parts with the placeholder
const truncatedText = startText + placeholderRef + endText
return {
truncatedText,
placeholderContent,
}
}
function formatTruncatedTextRef(id: number, numLines: number): string {
return `[...Truncated text #${id} +${numLines} lines...]`
}
export function maybeTruncateInput(
input: string,
pastedContents: Record<number, PastedContent>,
): { newInput: string; newPastedContents: Record<number, PastedContent> } {
// Get the next available ID for the truncated content
const existingIds = Object.keys(pastedContents).map(Number)
const nextPasteId = existingIds.length > 0 ? Math.max(...existingIds) + 1 : 1
// Apply truncation
const { truncatedText, placeholderContent } = maybeTruncateMessageForInput(
input,
nextPasteId,
)
if (!placeholderContent) {
return { newInput: input, newPastedContents: pastedContents }
}
return {
newInput: truncatedText,
newPastedContents: {
...pastedContents,
[nextPasteId]: {
id: nextPasteId,
type: 'text',
content: placeholderContent,
},
},
}
}