/*** Utility for inserting a block into a content array relative to tool_result* blocks. Used by the API layer to position supplementary content (e.g.,* cache editing directives) correctly within user messages.** Placement rules:* - If tool_result blocks exist: insert after the last one* - Otherwise: insert before the last block* - If the inserted block would be the final element, a text continuation* block is appended (some APIs require the prompt not to end with* non-text content)*//*** Inserts a block into the content array after the last tool_result block.* Mutates the array in place.** @param content - The content array to modify* @param block - The block to insert*/export function insertBlockAfterToolResults(content: unknown[],block: unknown,): void {// Find position after the last tool_result blocklet lastToolResultIndex = -1for (let i = 0; i < content.length; i++) {const item = content[i]if (item &&typeof item === 'object' &&'type' in item &&(item as { type: string }).type === 'tool_result') {lastToolResultIndex = i}}if (lastToolResultIndex >= 0) {const insertPos = lastToolResultIndex + 1content.splice(insertPos, 0, block)// Append a text continuation if the inserted block is now lastif (insertPos === content.length - 1) {content.push({ type: 'text', text: '.' })}} else {// No tool_result blocks — insert before the last blockconst insertIndex = Math.max(0, content.length - 1)content.splice(insertIndex, 0, block)}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。