| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- "use strict";
- const TAG_TO_TYPE = {
- plan: "sub-goal",
- think: "thought",
- action: "action",
- observation: "observation",
- conclusion: "conclusion",
- };
- function oneLine(text) {
- return String(text || "").replace(/\s+/g, " ").trim();
- }
- function parseCotBlocks(text) {
- const raw = String(text || "");
- const nodes = [];
- const regex = /<(?<tag>plan|think|action|observation|conclusion)>(?<body>[\s\S]*?)<\/\1>/gi;
- let match;
- while ((match = regex.exec(raw))) {
- const tag = (match.groups?.tag || "").toLowerCase();
- const body = oneLine(match.groups?.body || "");
- if (!body) continue;
- nodes.push({
- nodeType: TAG_TO_TYPE[tag] || "thought",
- name: truncate(body, 48),
- content: body,
- });
- }
- return nodes;
- }
- function truncate(text, max) {
- if (text.length <= max) return text;
- return `${text.slice(0, max - 1)}…`;
- }
- module.exports = {
- parseCotBlocks,
- };
|