"use strict";const H=require("@aws/codewhisperer-streaming-client");function a(e){return e!=null&&"userInputMessage"in e}function l(e){return e!=null&&"assistantResponseMessage"in e}function h(e){return!!e.userInputMessage.userInputMessageContext?.toolResults?.length}function g(e){return!!e.assistantResponseMessage.toolUses?.length}function U(e,t){if(!e||!e.length)return!0;if(!t||!t.length)return!1;const s=e.every(o=>t.some(r=>r.toolUseId===o.toolUseId)),n=t.every(o=>e.some(r=>o.toolUseId===r.toolUseId));return s&&n}const T={userInputMessage:{content:"Hello",origin:"AI_EDITOR"}},R={userInputMessage:{content:"Continue",origin:"AI_EDITOR"}},I={assistantResponseMessage:{content:"understood"}},f=e=>({userInputMessage:{content:"",origin:"AI_EDITOR",userInputMessageContext:{toolResults:e.map(n=>({toolUseId:n,content:[{text:"Tool execution failed"}],status:H.ToolResultStatus.ERROR}))}}});function d(e){return a(e[0])?e:[T,...e]}function A(e){return e.length===0?[T]:a(e[e.length-1])?e:[...e,R]}function p(e){if(e.length<=1)return e;const t=[e[0]];for(let s=1;s<e.length;s++){const n=t[t.length-1],o=e[s];a(n)&&a(o)?t.push(I):l(n)&&l(o)&&t.push(R),t.push(o)}return t}function N(e){const t=[];for(let s=0;s<e.length;s++){const n=e[s];if(t.push(n),l(n)&&g(n)){const o=s+1<e.length?e[s+1]:null;if(!o||!a(o)||!o.userInputMessage.userInputMessageContext?.toolResults){const u=(n.assistantResponseMessage.toolUses??[]).map(({toolUseId:S},c)=>S??`toolUse_${c+1}`),i=f(u);t.push(i)}else if(!U(n.assistantResponseMessage.toolUses,o.userInputMessage.userInputMessageContext.toolResults)&&!e.some((u,i)=>i!==s&&l(u)&&g(u)&&U(u.assistantResponseMessage.toolUses,o.userInputMessage.userInputMessageContext?.toolResults))){const i=(n.assistantResponseMessage.toolUses??[]).map(({toolUseId:c},_)=>c??`toolUse_${_+1}`),S=f(i);t.push(S)}}}return t}function O(e){const t=[],s=new Map;for(let r=0;r<e.length;r++){const u=e[r];if(l(u)&&g(u))t.push(r);else if(a(u)&&u.userInputMessage.userInputMessageContext?.toolResults?.length){const i=u.userInputMessage.userInputMessageContext.toolResults;for(const S of i)S.toolUseId&&!s.has(S.toolUseId)&&s.set(S.toolUseId,r)}}if(t.length===0)return e;const n=[],o=new Set;for(let r=0;r<e.length;r++){if(o.has(r))continue;const u=e[r];if(n.push(u),o.add(r),l(u)&&g(u)){const i=u.assistantResponseMessage.toolUses||[];for(const S of i){const c=S.toolUseId;if(c&&s.has(c)){const _=s.get(c);_&&_!==r+1&&!o.has(_)&&(n.push(e[_]),o.add(_))}}}}return n}function v(e){if(e.length<=1)return e;const t=e.findIndex(a);return e.filter((s,n)=>l(s)||a(s)&&n===t?!0:a(s)?s.userInputMessage.content&&s.userInputMessage.content.trim()!==""||h(s):!0)}function y(e){let t=[...e];return t=d(t),t=v(t),t=O(t),t=N(t),t=p(t),t=A(t),t}var E=(e=>(e.STARTS_WITH_USER_MESSAGE="STARTS_WITH_USER_MESSAGE",e.ENDS_WITH_USER_MESSAGE="ENDS_WITH_USER_MESSAGE",e.ALTERNATING_MESSAGES="ALTERNATING_MESSAGES",e.TOOL_USES_AND_RESULTS="TOOL_USES_AND_RESULTS",e.TOOL_RESULTS_AND_NO_USES="TOOL_RESULTS_AND_NO_USES",e.NON_EMPTY_USER_MESSAGE="NON_EMPTY_USER_MESSAGE",e))(E||{});const G={STARTS_WITH_USER_MESSAGE:"Conversation must start with a user message",ENDS_WITH_USER_MESSAGE:"Conversation must end with a user message",ALTERNATING_MESSAGES:"Between every two user messages there must be an assistant message",TOOL_RESULTS_AND_NO_USES:"If there is a message with tool result, there has to be a corresponding message with tool use.",TOOL_USES_AND_RESULTS:"If an assistant message has tool uses, the next message must be a user message with corresponding tool results",NON_EMPTY_USER_MESSAGE:"User messages must have either content or tool results"};function M(e,t,s){return{rule:e,message:s||G[e],index:t}}function L(e){return{valid:e.length===0,errors:e}}function V(e){return e.length===0?"No validation errors":e.map(t=>`${t.rule}: ${t.message} (at index ${t.index})`).join(`
`)}function C(e){return e.length===0||!a(e[0])?M(E.STARTS_WITH_USER_MESSAGE,0):null}function D(e){return e.length===0||!a(e[e.length-1])?M(E.ENDS_WITH_USER_MESSAGE,e.length-1):null}function x(e){for(let t=1;t<e.length;t++){const s=e[t-1],n=e[t];if(a(s)&&a(n))return M(E.ALTERNATING_MESSAGES,t,"Between every two user messages there must be an assistant message");if(l(s)&&l(n))return M(E.ALTERNATING_MESSAGES,t,"Between every two assistant messages there must be a user message")}return null}function w(e){for(let t=0;t<e.length-1;t++){const s=e[t],n=e[t+1];if(l(s)&&g(s)&&(!a(n)||!U(s.assistantResponseMessage.toolUses,n.userInputMessage.userInputMessageContext?.toolResults)))return M(E.TOOL_USES_AND_RESULTS,t+1);if(l(s)&&!g(s)&&a(n)&&h(n))return M(E.TOOL_RESULTS_AND_NO_USES,t)}return null}function W(e){for(let t=0;t<e.length;t++){const s=e[t];if(a(s)&&!(s.userInputMessage.content&&s.userInputMessage.content.trim()!=="")&&!h(s))return M(E.NON_EMPTY_USER_MESSAGE,t)}return null}function b(e){const t=[],s=[C(e),D(e),x(e),w(e),W(e)];for(const n of s)n&&t.push(n);return L(t)}exports.CONTINUE_MESSAGE=R;exports.FAILED_TOOL_USE_MESSAGE=f;exports.HELLO_MESSAGE=T;exports.UNDERSTOOD_MESSAGE=I;exports.ValidationErrorMessages=G;exports.ValidationRule=E;exports.createValidationError=M;exports.createValidationResult=L;exports.ensureAlternatingMessages=p;exports.ensureEndsWithUserMessage=A;exports.ensureStartsWithUserMessage=d;exports.ensureValidToolUsesAndResults=N;exports.formatValidationErrors=V;exports.hasMatchingToolResults=U;exports.hasToolResults=h;exports.hasToolUses=g;exports.isAssistantResponseMessage=l;exports.isUserInputMessage=a;exports.removeEmptyUserMessages=v;exports.reorderToolResultMessages=O;exports.sanitizeConversation=y;exports.validateAlternatingMessages=x;exports.validateConversation=b;exports.validateEndsWithUserMessage=D;exports.validateNonEmptyUserMessages=W;exports.validateStartsWithUserMessage=C;exports.validateToolUsesAndResults=w;
//# sourceMappingURL=validator-HXXAPSCh.cjs.map
