-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.ts
More file actions
164 lines (130 loc) · 5.12 KB
/
Copy pathutils.ts
File metadata and controls
164 lines (130 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as fs from "fs";
import * as path from "path";
import { config } from "./config.js";
import { logd } from "./helpers/cli-helpers.js";
// Message helpers use AI SDK CoreMessage; re-export from coreMessages
export {
extractMessageContent,
groupMessageChunks,
cleanMessagesContent,
getMessageKey,
deduplicateMessages,
} from "./coreMessages.js";
export type { CoreMessage } from "./coreMessages.js";
//parameter to redis
export function getContextLines(
fileName: string,
lineNumber: number,
before: number = 30,
after: number = 30
): string {
logd(`[getContextLines] 📥 Called with: fileName=${fileName}, lineNumber=${lineNumber}, before=${before}, after=${after}`);
try {
if (!fileName || typeof fileName !== "string") {
logd(`[getContextLines] ❌ ERROR: Invalid fileName: ${fileName}`);
return "";
}
if (typeof lineNumber !== "number" || lineNumber < 1) {
logd(`[getContextLines] ❌ ERROR: Invalid lineNumber: ${lineNumber}`);
return "";
}
const resolvedPath = path.resolve(fileName);
logd(`[getContextLines] 📁 Path resolution: original=${fileName}, resolved=${resolvedPath}, exists=${fs.existsSync(fileName)}`);
if (!fs.existsSync(fileName)) {
logd(`[getContextLines] ❌ ERROR: File not found: ${fileName}`);
logd(`[getContextLines] Attempted absolute path: ${resolvedPath}`);
return "";
}
const stats = fs.statSync(fileName);
logd(`[getContextLines] 📊 File stats: size=${stats.size}, isFile=${stats.isFile()}, isDirectory=${stats.isDirectory()}`);
const fileContent = fs.readFileSync(fileName, "utf-8");
logd(`[getContextLines] 📖 File read: contentLength=${fileContent.length}, hasContent=${fileContent.length > 0}`);
const lines = fileContent.split("\n");
const totalLines = lines.length;
logd(`[getContextLines] 📝 File split: totalLines=${totalLines}`);
const start = Math.max(0, lineNumber - before - 1);
const end = Math.min(totalLines, lineNumber + after);
logd(`[getContextLines] 🧮 Calculated indices: requestedLine=${lineNumber}, before=${before}, after=${after}, start=${start}, end=${end}, linesToExtract=${end - start}`);
if (lineNumber > totalLines) {
logd(`[getContextLines] ⚠️ WARNING: lineNumber (${lineNumber}) exceeds total lines (${totalLines})`);
}
if (lineNumber < 1) {
logd(`[getContextLines] ⚠️ WARNING: lineNumber (${lineNumber}) is less than 1`);
}
const extractedLines = lines.slice(start, end);
const result = extractedLines.join("\n");
logd(`[getContextLines] ✅ Extraction complete: extractedLineCount=${extractedLines.length}, resultLength=${result.length}, firstLine=${extractedLines[0]?.substring(0, 50) || "(empty)"}, lastLine=${extractedLines[extractedLines.length - 1]?.substring(0, 50) || "(empty)"}`);
return result;
} catch (error) {
logd(`[getContextLines] ❌ EXCEPTION: error=${error instanceof Error ? error.message : String(error)}, fileName=${fileName}, lineNumber=${lineNumber}`);
return "";
}
}
//redis connection
// const redisClient = createClient({
// url: `rediss://${config.redis_username}:${config.redis_password}@${config.redis_host}:${config.redis_port}`,
// socket: { tls: true },
// });
// redisClient.on("error", (err) => {
// console.error("Redis Client Error", err);
// });
// redisClient.on("connect", () => {
// console.log("Redis Client Connected");
// });
// redisClient.connect();
// export default redisClient;
function isIncompleteJSONChunk(content: string): boolean {
if (typeof content !== "string") return false;
const trimmed = content.trim();
const hasJSONFields = trimmed.includes('"next"') || trimmed.includes('"message"');
if (!hasJSONFields) {
return false;
}
if (trimmed.startsWith("{") && !trimmed.endsWith("}")) {
return true;
}
if (!trimmed.startsWith("{") && hasJSONFields) {
return true;
}
if (trimmed.endsWith("}") && !trimmed.startsWith("{")) {
return true;
}
return false;
}
function cleanMessageContent(content: string): string {
if (typeof content !== "string") return content;
const trimmed = content.trim();
if (isIncompleteJSONChunk(trimmed)) {
return "";
}
if ((trimmed.includes('"next"') || trimmed.includes('"message"')) && !trimmed.startsWith("{")) {
if (trimmed.endsWith("}")) {
return "";
}
if (trimmed.includes('":"') && (trimmed.includes('"next"') || trimmed.includes('"message"'))) {
return "";
}
}
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
try {
const parsed = JSON.parse(trimmed);
if (parsed && typeof parsed === "object" && typeof parsed.message === "string") {
return parsed.message;
} else if (parsed && typeof parsed === "object") {
return "";
}
} catch {
}
}
const jsonMatch = trimmed.match(/\{[\s\S]*?"message"[\s\S]*?\}/);
if (jsonMatch) {
try {
const parsed = JSON.parse(jsonMatch[0]);
if (parsed && typeof parsed === "object" && typeof parsed.message === "string") {
return parsed.message;
}
} catch {
}
}
return content;
}