task-o-matic 0.0.1 → 0.0.2
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,7 +1,20 @@
|
|
|
1
1
|
import { JSONParseResult } from "../../types";
|
|
2
2
|
export declare class JSONParser {
|
|
3
|
+
/**
|
|
4
|
+
* Extracts JSON from text that may contain markdown codeblocks or other formatting
|
|
5
|
+
*/
|
|
6
|
+
private extractJSONString;
|
|
7
|
+
/**
|
|
8
|
+
* Normalizes object keys to handle case variations (e.g., "Tasks" -> "tasks")
|
|
9
|
+
*/
|
|
10
|
+
private normalizeKeys;
|
|
3
11
|
/**
|
|
4
12
|
* Parses JSON from AI text response with improved error handling
|
|
13
|
+
* Now supports:
|
|
14
|
+
* - Extracting from markdown codeblocks (```json, ```JSON, or ```)
|
|
15
|
+
* - Case-insensitive property names (Tasks -> tasks, Summary -> summary)
|
|
16
|
+
* - Multiple extraction strategies
|
|
17
|
+
*
|
|
5
18
|
* @deprecated Use generateObject instead for structured output
|
|
6
19
|
*/
|
|
7
20
|
parseJSONFromResponse<T>(text: string): JSONParseResult<T>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json-parser.d.ts","sourceRoot":"","sources":["../../../src/lib/ai-service/json-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,qBAAa,UAAU;IACrB
|
|
1
|
+
{"version":3,"file":"json-parser.d.ts","sourceRoot":"","sources":["../../../src/lib/ai-service/json-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,qBAAa,UAAU;IACrB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAmCzB;;OAEG;IACH,OAAO,CAAC,aAAa;IAsBrB;;;;;;;;OAQG;IACH,qBAAqB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,eAAe,CAAC,CAAC,CAAC;CAgC3D"}
|
|
@@ -2,23 +2,84 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.JSONParser = void 0;
|
|
4
4
|
class JSONParser {
|
|
5
|
+
/**
|
|
6
|
+
* Extracts JSON from text that may contain markdown codeblocks or other formatting
|
|
7
|
+
*/
|
|
8
|
+
extractJSONString(text) {
|
|
9
|
+
// Strategy 1: Try to extract from markdown codeblock (```json ... ``` or ``` ... ```)
|
|
10
|
+
const codeblockPatterns = [
|
|
11
|
+
/```json\s*([\s\S]*?)```/i,
|
|
12
|
+
/```JSON\s*([\s\S]*?)```/i,
|
|
13
|
+
/```\s*([\s\S]*?)```/,
|
|
14
|
+
];
|
|
15
|
+
for (const pattern of codeblockPatterns) {
|
|
16
|
+
const match = text.match(pattern);
|
|
17
|
+
if (match && match[1]) {
|
|
18
|
+
const extracted = match[1].trim();
|
|
19
|
+
// Verify it looks like JSON (starts with { or [)
|
|
20
|
+
if (extracted.startsWith("{") || extracted.startsWith("[")) {
|
|
21
|
+
return extracted;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Strategy 2: Try to extract JSON object/array directly
|
|
26
|
+
const directPatterns = [
|
|
27
|
+
/\{[\s\S]*\}/, // Object
|
|
28
|
+
/\[[\s\S]*\]/, // Array
|
|
29
|
+
];
|
|
30
|
+
for (const pattern of directPatterns) {
|
|
31
|
+
const match = text.match(pattern);
|
|
32
|
+
if (match) {
|
|
33
|
+
return match[0];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Normalizes object keys to handle case variations (e.g., "Tasks" -> "tasks")
|
|
40
|
+
*/
|
|
41
|
+
normalizeKeys(obj) {
|
|
42
|
+
if (obj === null || obj === undefined) {
|
|
43
|
+
return obj;
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(obj)) {
|
|
46
|
+
return obj.map((item) => this.normalizeKeys(item));
|
|
47
|
+
}
|
|
48
|
+
if (typeof obj === "object") {
|
|
49
|
+
const normalized = {};
|
|
50
|
+
for (const key in obj) {
|
|
51
|
+
// Convert first letter to lowercase
|
|
52
|
+
const normalizedKey = key.charAt(0).toLowerCase() + key.slice(1);
|
|
53
|
+
normalized[normalizedKey] = this.normalizeKeys(obj[key]);
|
|
54
|
+
}
|
|
55
|
+
return normalized;
|
|
56
|
+
}
|
|
57
|
+
return obj;
|
|
58
|
+
}
|
|
5
59
|
/**
|
|
6
60
|
* Parses JSON from AI text response with improved error handling
|
|
61
|
+
* Now supports:
|
|
62
|
+
* - Extracting from markdown codeblocks (```json, ```JSON, or ```)
|
|
63
|
+
* - Case-insensitive property names (Tasks -> tasks, Summary -> summary)
|
|
64
|
+
* - Multiple extraction strategies
|
|
65
|
+
*
|
|
7
66
|
* @deprecated Use generateObject instead for structured output
|
|
8
67
|
*/
|
|
9
68
|
parseJSONFromResponse(text) {
|
|
10
69
|
try {
|
|
11
70
|
// Try to extract JSON from the response (handle various formats)
|
|
12
|
-
const
|
|
13
|
-
if (!
|
|
71
|
+
const jsonStr = this.extractJSONString(text);
|
|
72
|
+
if (!jsonStr) {
|
|
14
73
|
return {
|
|
15
74
|
success: false,
|
|
16
|
-
error: "Could not extract JSON from AI response",
|
|
75
|
+
error: "Could not extract JSON from AI response. No JSON object or codeblock found.",
|
|
17
76
|
rawText: text,
|
|
18
77
|
};
|
|
19
78
|
}
|
|
20
|
-
|
|
21
|
-
|
|
79
|
+
// Parse the JSON
|
|
80
|
+
let parsed = JSON.parse(jsonStr);
|
|
81
|
+
// Normalize keys to handle case variations (Tasks -> tasks, etc.)
|
|
82
|
+
parsed = this.normalizeKeys(parsed);
|
|
22
83
|
return {
|
|
23
84
|
success: true,
|
|
24
85
|
data: parsed,
|