in packages/core/src/codewhispererChat/controllers/chat/chatRequest/converter.ts [34:182]
export function triggerPayloadToChatRequest(triggerPayload: TriggerPayload): {
conversationState: ConversationState
profileArn?: string
} {
// Flexible truncation logic
const remainingPayloadSize = 100_000
// Type A context: Preserving user input as much as possible
const userInputTruncationInfo = preserveContexts(triggerPayload, remainingPayloadSize, ChatContextType.UserInput)
// Type B1(prompts) context: Preserving @prompt as much as possible
const userSpecificPromptsTruncationInfo = preserveContexts(
triggerPayload,
userInputTruncationInfo.remainingPayloadSize,
ChatContextType.UserSpecificPrompts
)
// Type C context: Preserving current file context as much as possible
const currentFileTruncationInfo = preserveContexts(
triggerPayload,
userSpecificPromptsTruncationInfo.remainingPayloadSize,
ChatContextType.CurrentFile
)
// Type B1(rules) context: Preserving rules as much as possible
const userSpecificRulesTruncationInfo = preserveContexts(
triggerPayload,
currentFileTruncationInfo.remainingPayloadSize,
ChatContextType.UserSpecificRules
)
// Type B2(explicit @files) context: Preserving files as much as possible
const userSpecificFilesTruncationInfo = preserveContexts(
triggerPayload,
userSpecificRulesTruncationInfo.remainingPayloadSize,
ChatContextType.UserSpecificFiles
)
// Type B3 @workspace context: Preserving workspace as much as possible
const workspaceTruncationInfo = preserveContexts(
triggerPayload,
userSpecificFilesTruncationInfo.remainingPayloadSize,
ChatContextType.Workspace
)
getLogger().debug(
`current request total payload size: ${userInputTruncationInfo.sizeAfter + currentFileTruncationInfo.sizeAfter + userSpecificRulesTruncationInfo.sizeAfter + userSpecificFilesTruncationInfo.sizeAfter + workspaceTruncationInfo.sizeAfter}`
)
// Filter out empty innerContext from additionalContents
if (triggerPayload.additionalContents !== undefined) {
triggerPayload.additionalContents = triggerPayload.additionalContents.filter(
(content) => content.innerContext !== undefined && content.innerContext !== ''
)
}
// Filter out empty text from relevantTextDocuments
triggerPayload.relevantTextDocuments = triggerPayload.relevantTextDocuments.filter(
(doc) => doc.text !== undefined && doc.text !== ''
)
let document: TextDocument | undefined = undefined
let cursorState: CursorState | undefined = undefined
if (triggerPayload.filePath !== undefined && triggerPayload.filePath !== '') {
const documentSymbolFqns: DocumentSymbol[] = []
if (triggerPayload.codeQuery?.fullyQualifiedNames?.used) {
for (const fqn of triggerPayload.codeQuery.fullyQualifiedNames.used) {
const elem = {
name: fqn.symbol?.join('.') ?? '',
type: SymbolType.USAGE,
source: fqn.source?.join('.'),
}
if (
elem.name.length >= fqnNameSizeDownLimit &&
elem.name.length < fqnNameSizeUpLimit &&
(elem.source === undefined ||
(elem.source.length >= fqnNameSizeDownLimit && elem.source.length < fqnNameSizeUpLimit))
) {
documentSymbolFqns.push(elem)
}
}
}
let programmingLanguage
if (
triggerPayload.fileLanguage !== undefined &&
triggerPayload.fileLanguage !== '' &&
supportedLanguagesList.includes(triggerPayload.fileLanguage)
) {
programmingLanguage = { languageName: triggerPayload.fileLanguage }
}
document = {
relativeFilePath: triggerPayload.filePath ? triggerPayload.filePath.substring(0, filePathSizeLimit) : '',
text: triggerPayload.fileText,
programmingLanguage: programmingLanguage,
documentSymbols: documentSymbolFqns,
}
if (triggerPayload.codeSelection?.start) {
cursorState = {
range: {
start: {
line: triggerPayload.codeSelection.start.line,
character: triggerPayload.codeSelection.start.character,
},
end: {
line: triggerPayload.codeSelection.end.line,
character: triggerPayload.codeSelection.end.character,
},
},
}
}
}
// service will throw validation exception if string is empty
const customizationArn: string | undefined = undefinedIfEmpty(triggerPayload.customization.arn)
const chatTriggerType = triggerPayload.trigger === ChatTriggerType.InlineChatMessage ? 'INLINE_CHAT' : 'MANUAL'
const history =
triggerPayload.history &&
triggerPayload.history.length > 0 &&
triggerPayload.history.map((chat) => messageToChatMessage(chat))
return {
conversationState: {
currentMessage: {
userInputMessage: {
content: triggerPayload.message,
userInputMessageContext: {
editorState: {
document,
cursorState,
relevantDocuments: triggerPayload.relevantTextDocuments,
useRelevantDocuments: triggerPayload.useRelevantDocuments,
},
additionalContext: triggerPayload.additionalContents,
},
userIntent: triggerPayload.userIntent,
},
},
chatTriggerType,
customizationArn: customizationArn,
history: history || undefined,
},
profileArn: triggerPayload.profile?.arn,
}
}