"use strict";const l=require("./prompt-DKJV0_RD.cjs"),d=require("zod"),b=require("os"),U=require("./logger-BN0ZR9CS.cjs"),Z=require("./prompt-template-QQwL-3pT.cjs"),L=require("./workspace-object-ChjgKU0j.cjs");require("vscode-uri");const T=require("./shared-types-B8BH5zz1.cjs"),j=require("crypto"),C=require("./prompts-BzxTXCW-.cjs"),S=require("./file-context-BUqxbFVF.cjs");let v=!1,F=0;function x(){v=!0,F=0}function $(){return v?"MOCKED::"+String(F++):j.randomUUID()}function m(t,e=""){return t==null?"":Array.isArray(t)?I(t,e):typeof t=="object"?D(t,e):P(t)?String(t):"unknown"}function I(t,e){if(t.length===0)return"";const r=e+"  ";return t.map((n,i)=>Array.isArray(n)?f(i.toString(),`
${I(n,r)}
${e}`,e):typeof n=="object"?f(i.toString(),`
${D(n,r)}
${e}`,e):f(i.toString(),String(n),e)).join(`
`)}function D(t,e){const r=Object.entries(t);return r.length===0?"":r.map(([n,i])=>M(n,i,e)).join(`
`)}function M(t,e,r){return e==null?f(t,"",r):Array.isArray(e)?f(t,`
${I(e,r+"  ")}
${r}`,r):typeof e=="object"?f(t,`
${m(e,r+"  ")}
${r}`,r):f(t,String(e),r)}function f(t,e,r){return`${r}<${t}>${e}</${t}>`}function P(t){return["string","number","boolean"].includes(typeof t)}function q(t){return l.agentActionError.format({input:m({[t.actionId]:t.actionInput}),error:t.error})}function X(t){return t.actionInput?l.agentActionFormatError.format({input:m({[t.actionId]:t.actionInput}),error:t.error}):l.agentActionFormatErrorNoInput.format({error:t.error})}function W(t){return l.agentActionFormatSuccess.format({toolId:t.actionId,input:m({[t.actionId]:t.actionInput}),output:m({success:t.actionOutput||"success"})})}function N(t){return JSON.stringify(E(t),void 0,2)}function R(t,e){const r=E(e);return g(t,r)}function g(t,e){if(typeof e=="string"||typeof e=="number")return`<${t}>${e}</${t}>`;if(Array.isArray(e)){const r=e[0];return`<${t}>
${y(g("0",r))}
${y(g("1",r))}
</${t}>`}if(typeof e=="object"&&e!==null){const r=Object.entries(e).map(([n,i])=>g(n,i)).join(`
`);return`<${t}>
${y(r)}
</${t}>`}return`<${t}></${t}>`}function y(t,e=2){return t.split(`
`).map(r=>" ".repeat(e)+r).join(`
`)}function E(t){return p(t)}function p(t){if(t instanceof d.ZodObject)return V(t);if(t instanceof d.ZodRecord)return _(t);if(t instanceof d.ZodArray)return J(t);if(t instanceof d.ZodString)return K(t);if(t instanceof d.ZodNumber)return G(t);if(t instanceof d.ZodEnum)return B(t);if(t instanceof d.ZodOptional)return Q(t);if(t instanceof d.ZodDefault)return Y(t);throw new TypeError(`Unsupported Zod type for LLM Serialization: ${t.constructor.name}`)}function V(t){return H(t.shape)}function _(t){const e={};return e.key1=p(t.valueSchema),e.key2=p(t.valueSchema),e}function H(t){const e={};for(const[r,n]of Object.entries(t))e[r]=p(n);return e}function J(t){return[p(t.element)]}function K(t){return t.description||"string value"}function G(t){return t.description||"number value"}function B(t){return{type:"enum",values:t.options.reduce((r,n,i)=>(r[`option${i+1}`]=n,r),{})}}function Q(t){const e=t._def.innerType;return{type:"optional",value:p(e)}}function Y(t){const e=t._def.innerType,r=t._def.defaultValue(),n=p(e);return{type:"field",defaultValue:r,value:n}}function ee(t){return l.toolDefinitionToXML.format({id:t.id,description:t.description,usage:R(t.id,t.parameters)})}const z=3e4;async function w(t,e,r=!1){try{const n=[],i=await L.WorkspaceObject.fromRelativePath(e,t.path),o=await e.readFile({file:i});if(n.push(k(t.path,o,!!t.focused,r)),o.metadata.referencedContext)for(const c of o.metadata.referencedContext)c.uri?.scheme==="file"?n.push(A(c.uri.path,c.content)):n.push(c.content);return n.join(`
`)}catch{return""}}function k(t,e,r=!1,n=!1){const i=e.content.text,o=n?[]:e.metadata.problems,c=e.metadata.language;if(!i||i.length===0)return l.promptFormatEmptyFile.format({file:t});if(i.length>z){const a=i.substring(0,z);return l.promptFormatTruncatedFile.format({language:c||"text",file:t,content:a,totalLines:i.split(b.EOL).length,readLines:a.split(b.EOL).length,focus:r?'state="USER_FOCUSED"':""})}return A(t,i,o,c,r)}function A(t,e,r=[],n,i=!1){if(r.length===0)return l.promptFormatCode.format({language:n||"text",file:t,content:e||"",focus:i?'state="USER_FOCUSED"':""});const o=te(e||"",r);return l.promptFormatCodeWithProblems.format({language:n||"text",file:t,content:e||"",problems:o,focus:i?'state="USER_FOCUSED"':""})}function te(t,e=[]){try{const r=t.split(`
`),n=[];for(let i=0;i<e.length;i++){const o=e[i];if(o.range.start.line===o.range.end.line){const c=o.range.start.line,a=r[c].slice(o.range.start.character,o.range.end.character);n.push(l.promptFormatCodeProblem.format({id:i,code:a,severity:o.severity,problem:o.message}))}else{const a=r.slice(o.range.start.line,o.range.end.line+1).join(`
`);n.push(l.promptFormatCodeProblem.format({id:i,code:a,severity:o.severity,problem:o.message}))}}return Z.joinPrompts(n)}catch{const r="Caught error formatting code problems, skipping for now";return U.logger.error(r),r}}class re extends T.KiroError{constructor(e){super(`Unknown message role: ${e}`),this.role=e}get userFacingSessionErrorMessage(){return"A message with an invalid role was encountered."}}class ne extends T.KiroError{constructor(e){super(`Invalid document path: ${e}`),this.path=e}get userFacingSessionErrorMessage(){return"Document without path provided in chat history."}}function O({doc:t}){const e=t.scope?` [${t.scope==="global"?"Global":"Workspace"}]`:"",r=t.scope==="workspace"?`
  Workspace-level rules take precedence over global-level rules when conflicts exist.`:"";return`## Included Rules (${t.displayName})${e}

  I am providing you some additional guidance that you should follow for your entire execution. These are intended to steer you in the right direction.
  They have been automatically suggested by the system and may be unrelated to my specific request which follows after them. Consider them, but your number one priority is my request.${r}

<user-rule id=${t.displayName}>
\`\`\`
${t.content}
\`\`\`
</user-rule>
`}const ie=30,oe=3e5;class u{role;entries;messageId;forcedRole;constructor(e,r,n,i=!1){this.role=e,this.entries=r,this.messageId=n||$(),this.forcedRole=i}static fromHuman(e){return new u("human",[],e)}static fromAI(e){return new u("bot",[],e)}static fromTool(e){return new u("tool",[],e)}static generateResponseMessageId(e){return e+"_response"}mergedWith(e){const r=new Set(this.entries.filter(i=>i.type==="toolUse").map(i=>i.id)),n=e.entries.filter(i=>i.type==="toolUse"?!r.has(i.id):!0);return new u(this.role,[...this.entries,...n],this.messageId,this.forcedRole||e.forcedRole)}withForcedRole(){return new u(this.role,this.entries,this.messageId,!0)}withAgentInvoke(e){return this.withEntry({type:"agentInvoke",executionId:e})}withEntry(e){if(e.type==="document"&&e.document.type==="file"&&e.document.path==="")throw new ne("");return new u(this.role,[...this.entries,e],this.messageId,this.forcedRole)}withText(e){return this.withEntry({type:"text",text:e})}withReasoningContent(e,r,n){return this.withEntry({type:"reasoningContent",text:e,...r&&{signature:r},...n&&{modelId:n}})}getReasoningContent(){const e=this.entries.find(r=>r.type==="reasoningContent");return e?.type==="reasoningContent"?e.text:void 0}withToolDefinition(e){return this.withEntry({type:"toolDefinition",tool:e})}withToolDefinitions(e){return e.reduce((r,n)=>r.withToolDefinition(n),new u(this.role,[...this.entries],this.messageId,this.forcedRole))}withToolUse(e){return this.withEntry({...e,type:"toolUse",requestMessageId:this.messageId})}withToolUseResponse(e){return this.withEntry({...e,type:"toolUseResponse"})}withDocument(e){return this._hasMatchingDocument(e)?this:this.withEntry({type:"document",...e})}async pruneDocsIfNecessary(e){const r=[...this.entries],n=await Promise.all(r.filter(i=>i.type==="document").map(i=>i.document).filter(i=>i.type==="file").map(async i=>{const o=await w(i,e);return[i,o.length]}));for(;n.length>ie||n.reduce((i,[o,c])=>i+c,0)>oe;){const i=r.findIndex(o=>o.type==="document"&&o.document.type==="file");if(i===-1)break;r.splice(i,1),n.shift()}return r}_hasMatchingDocument(e){const r=new Set;for(const i of this.entries)if(i.type==="document"){const o=JSON.stringify(i.document);r.add(o)}const n=JSON.stringify(e.document);return r.has(n)}withDocuments(e){return e.reduce((r,n)=>r.withDocument(n),new u(this.role,[...this.entries],this.messageId,this.forcedRole))}withImages(e){return e.reduce((r,n)=>r.withEntry({type:"image",...n}),new u(this.role,[...this.entries],this.messageId,this.forcedRole))}withDocumentAttachments(e){return e.reduce((r,n)=>r.withEntry({type:"documentAttachment",...n}),new u(this.role,[...this.entries],this.messageId,this.forcedRole))}withEnvironmentInfo(){const e=S.loadOpenEditorFiles().map(i=>`<file name="${i}" />`).join(`
`)||"No files are open",r=S.loadCurrentFile(),n=r?`<file name="${r}" />`:"No file is active in editor";return this.withText(C.getEnvironmentStatePrompt().format({openEditorFiles:e,activeTextFile:n}))}withAgentAction(e){return this.withEntry({type:"AgentAction",...e})}withAgentActionSuccess(e){return this.withEntry({type:"AgentActionSuccess",...e})}withAgentActionError(e){return this.withEntry({type:"AgentActionError",...e})}withWorkspaceActionInvalid(e){return this.withEntry({type:"AgentActionInvalidInput",...e})}withEntries(...e){return new u(this.role,[...this.entries,...e],this.messageId,this.forcedRole)}async serializeToContext(e){if(this.entries.length==0)return this.role==="bot"?{role:"bot",content:"understood"}:{role:"human",content:"continue"};const r=await this.pruneDocsIfNecessary(e),n=[],i=[],o=[];for(const s of r)switch(s.type){case"text":{n.push(this.serializeTextEntry(s));break}case"document":{s.document.type==="steering"?n.splice(0,0,await this.serializeDocumentsEntry(s,e)):n.push(await this.serializeDocumentsEntry(s,e));break}case"image":{i.push({url:s.image,detail:"auto"});break}case"documentAttachment":{o.push({data:s.data,name:s.name,mimeType:s.mimeType});break}case"toolDefinition":{n.push(this.serializeToolDefinitionsEntry(s));break}case"AgentActionSuccess":n.push(this.serializeActionSuccess(s));break;case"AgentActionInvalidInput":n.push(this.serializeActionInvalid(s));break;case"AgentActionError":n.push(this.serializeActionError(s));break}const c=r.filter(s=>s.type==="toolUse"),a=r.filter(s=>s.type==="toolUseResponse"),h=r.find(s=>s.type==="reasoningContent");return{role:this.role,content:n.join(`

`),toolUses:c.length?c:void 0,toolUseResponses:a.length?a:void 0,images:i,documentAttachments:o.length?o:void 0,reasoningContent:h?.type==="reasoningContent"?h.text:void 0,reasoningSignature:h?.type==="reasoningContent"?h.signature:void 0,reasoningModelId:h?.type==="reasoningContent"?h.modelId:void 0}}serializeTextEntry(e){return e.text}async serializeDocumentsEntry(e,r){switch(e.document.type){case"file":return e.document.staticContent?e.document.staticContent:await w(e.document,r);case"fileTree":{if(e.document.staticDirectoryView)return e.document.staticDirectoryView;const n=e.document.expandedPaths,i=e.document.openedFiles??[],o=(await Promise.all(n.map(a=>r.resolveRelativePath(a)))).filter(a=>a!==void 0),c=(await Promise.all(i.map(a=>r.resolveRelativePath(a.path)))).filter(a=>a!==void 0);return await l.serializeFileTree({paths:[...o,...c],workspace:r,target:e.document.target})}case"steering":return O({doc:e.document})}}serializeToObject(){return{role:this.role,entries:[...this.entries],messageId:this.messageId}}serializeToolDefinitionsEntry(e){return ee(e.tool)}serializeActionSuccess(e){return W(e)}serializeActionInvalid(e){return X(e)}serializeActionError(e){return q(e)}}exports.ContextChatMessage=u;exports.InvalidMessageRoleError=re;exports.formatFileContent=A;exports.formatWorkspaceFile=k;exports.getId=$;exports.objectToXML=m;exports.serializeFile=w;exports.serializeSteeringDocument=O;exports.serializeZodSchemaIntoJsonString=N;exports.serializeZodSchemaIntoObject=E;exports.serializeZodSchemaIntoXMLString=R;exports.useMockedIds=x;
//# sourceMappingURL=context-chat-message-CXHZakV1.cjs.map
