MCPcopy Create free account
hub / github.com/dwgx/WindsurfAPI / parseTrajectorySteps

Function parseTrajectorySteps

src/windsurf.js:991–1383  ·  view source on GitHub ↗
(buf)

Source from the content-addressed store, hash-verified

989 * Step.field 20: planner_response { field 1: response, field 3: thinking }
990 */
991export function parseTrajectorySteps(buf) {
992 // Robustness (audit #1): a malformed/hostile upstream payload must not
993 // throw out of the parser. Degrade to "no steps" on a bad root buffer.
994 let fields;
995 try { fields = parseFields(buf); } catch { return []; }
996 const steps = getAllFields(fields, 1).filter(f => f.wireType === 2);
997 const results = [];
998
999 const decodeKnowledgeBaseItemText = (docBuf) => {
1000 try {
1001 const doc = parseFields(docBuf);
1002 const text = getField(doc, 2, 2)?.value?.toString('utf8') || '';
1003 if (text) return text;
1004 const chunks = getAllFields(doc, 6)
1005 .filter(x => x.wireType === 2)
1006 .map((chunkField) => {
1007 try {
1008 const chunk = parseFields(chunkField.value);
1009 const chunkText = getField(chunk, 1, 2)?.value?.toString('utf8') || '';
1010 if (chunkText) return chunkText;
1011 const markdown = getField(chunk, 3, 2);
1012 if (!markdown) return '';
1013 const md = parseFields(markdown.value);
1014 return getField(md, 2, 2)?.value?.toString('utf8') || '';
1015 } catch {
1016 return '';
1017 }
1018 })
1019 .filter(Boolean);
1020 if (chunks.length) return chunks.join('\n');
1021 return getField(doc, 7, 2)?.value?.toString('utf8') || '';
1022 } catch {
1023 return '';
1024 }
1025 };
1026
1027 const isLikelyPathOrFileUri = (value) => {
1028 const s = String(value || '').trim();
1029 if (!s || s.length > 1024 || /[\r\n<>]/.test(s)) return false;
1030 if (/^file:\/\/\/?(?:[A-Za-z]:[\\/]|\/|~[\\/])/.test(s)) return true;
1031 if (/^(?:[A-Za-z]:[\\/]|\/|~[\\/]|\.{1,2}[\\/])\S+/.test(s)) return true;
1032 return /^[A-Za-z0-9._-]+(?:[\\/][A-Za-z0-9._-]+)*\.[A-Za-z0-9]{1,12}$/.test(s);
1033 };
1034
1035 for (const step of steps) {
1036 // Robustness (audit #1): skip a single malformed step instead of
1037 // discarding the whole trajectory when one nested field is corrupt.
1038 try {
1039 const sf = parseFields(step.value);
1040 const typeField = getField(sf, 1, 0);
1041 const statusField = getField(sf, 4, 0);
1042 // CortexTrajectoryStep.planner_response = field 20
1043 // CortexStepPlannerResponse.response = 1, thinking = 3, modified_response = 8
1044 const plannerField = getField(sf, 20, 2);
1045
1046 const entry = {
1047 type: typeField ? typeField.value : 0,
1048 status: statusField ? statusField.value : 0,

Calls 11

parseFieldsFunction · 0.90
getAllFieldsFunction · 0.90
getFieldFunction · 0.90
readUintFunction · 0.85
parseChatToolCallFunction · 0.85
isLikelyPathOrFileUriFunction · 0.85
readErrorDetailsFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected