te.js 2.0.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,88 +0,0 @@
1
- /**
2
- * Parse JSON from LLM response text (handles markdown code blocks).
3
- */
4
-
5
- /**
6
- * Extract the first JSON object from a string.
7
- * @param {string} str - Raw LLM response
8
- * @returns {object|null}
9
- */
10
- export function extractJSON(str) {
11
- if (!str || typeof str !== 'string') return null;
12
- const trimmed = str.trim();
13
- const open = trimmed.indexOf('{');
14
- if (open === -1) return null;
15
- let depth = 0;
16
- let end = -1;
17
- for (let i = open; i < trimmed.length; i++) {
18
- if (trimmed[i] === '{') depth++;
19
- else if (trimmed[i] === '}') {
20
- depth--;
21
- if (depth === 0) {
22
- end = i + 1;
23
- break;
24
- }
25
- }
26
- }
27
- if (end === -1) return null;
28
- try {
29
- return JSON.parse(trimmed.slice(open, end));
30
- } catch {
31
- return null;
32
- }
33
- }
34
-
35
- /**
36
- * Extract the first JSON array from a string.
37
- * @param {string} str - Raw LLM response
38
- * @returns {Array|null}
39
- */
40
- export function extractJSONArray(str) {
41
- if (!str || typeof str !== 'string') return null;
42
- const trimmed = str.trim();
43
- const open = trimmed.indexOf('[');
44
- if (open === -1) return null;
45
- let depth = 0;
46
- let end = -1;
47
- for (let i = open; i < trimmed.length; i++) {
48
- if (trimmed[i] === '[') depth++;
49
- else if (trimmed[i] === ']') {
50
- depth--;
51
- if (depth === 0) {
52
- end = i + 1;
53
- break;
54
- }
55
- }
56
- }
57
- if (end === -1) return null;
58
- try {
59
- return JSON.parse(trimmed.slice(open, end));
60
- } catch {
61
- return null;
62
- }
63
- }
64
-
65
- /**
66
- * Reconcile LLM-ordered tag names with actual tag objects. Returns tags in desired order;
67
- * any tag not in orderedTagNames is appended at the end.
68
- * @param {string[]} orderedTagNames - Tag names in desired order (from LLM)
69
- * @param {Array<{ name: string, description?: string }>} tags - Current spec.tags
70
- * @returns {Array<{ name: string, description?: string }>} Tags reordered
71
- */
72
- export function reconcileOrderedTags(orderedTagNames, tags) {
73
- if (!Array.isArray(tags) || !tags.length) return [];
74
- if (!Array.isArray(orderedTagNames) || !orderedTagNames.length) return [...tags];
75
- const byName = new Map(tags.map((t) => [t.name, t]));
76
- const ordered = [];
77
- for (const name of orderedTagNames) {
78
- const tag = byName.get(name);
79
- if (tag) {
80
- ordered.push(tag);
81
- byName.delete(name);
82
- }
83
- }
84
- for (const [, tag] of byName) {
85
- ordered.push(tag);
86
- }
87
- return ordered;
88
- }
File without changes