deeper-cli 1.0.4 → 1.0.6
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.
- package/dist/cli/index.js +773 -266
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/chat-repl.ts +94 -85
- package/src/model/DeepSeekClient.ts +6 -2
- package/src/model/StreamHandler.ts +12 -10
- package/src/tools/tool-types.ts +1 -0
package/dist/cli/index.js
CHANGED
|
@@ -1831,10 +1831,10 @@ var init_token_count = __esm({
|
|
|
1831
1831
|
const messagesJson = params.messages;
|
|
1832
1832
|
let content = "";
|
|
1833
1833
|
if (filePath) {
|
|
1834
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
1834
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
1835
1835
|
const { resolve: resolve48 } = await import("path");
|
|
1836
1836
|
const abs = resolve48(filePath);
|
|
1837
|
-
if (!
|
|
1837
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
1838
1838
|
content = readFileSync38(abs, "utf-8");
|
|
1839
1839
|
} else if (text) {
|
|
1840
1840
|
content = text;
|
|
@@ -3271,6 +3271,536 @@ var init_MCPClient = __esm({
|
|
|
3271
3271
|
}
|
|
3272
3272
|
});
|
|
3273
3273
|
|
|
3274
|
+
// src/model/RetryManager.ts
|
|
3275
|
+
var RetryManager;
|
|
3276
|
+
var init_RetryManager = __esm({
|
|
3277
|
+
"src/model/RetryManager.ts"() {
|
|
3278
|
+
"use strict";
|
|
3279
|
+
RetryManager = class {
|
|
3280
|
+
maxRetries;
|
|
3281
|
+
baseDelayMs;
|
|
3282
|
+
maxDelayMs;
|
|
3283
|
+
constructor(maxRetries = 3, baseDelayMs = 1e3, maxDelayMs = 3e4) {
|
|
3284
|
+
this.maxRetries = maxRetries;
|
|
3285
|
+
this.baseDelayMs = baseDelayMs;
|
|
3286
|
+
this.maxDelayMs = maxDelayMs;
|
|
3287
|
+
}
|
|
3288
|
+
async execute(fn, shouldRetry) {
|
|
3289
|
+
let lastError;
|
|
3290
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
3291
|
+
try {
|
|
3292
|
+
return await fn();
|
|
3293
|
+
} catch (error) {
|
|
3294
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
3295
|
+
if (attempt === this.maxRetries) {
|
|
3296
|
+
break;
|
|
3297
|
+
}
|
|
3298
|
+
if (shouldRetry && !shouldRetry(lastError, attempt)) {
|
|
3299
|
+
break;
|
|
3300
|
+
}
|
|
3301
|
+
const delay = Math.min(
|
|
3302
|
+
this.baseDelayMs * Math.pow(2, attempt) + Math.random() * 1e3,
|
|
3303
|
+
this.maxDelayMs
|
|
3304
|
+
);
|
|
3305
|
+
await this.sleep(delay);
|
|
3306
|
+
}
|
|
3307
|
+
}
|
|
3308
|
+
throw lastError ?? new Error("Max retries exceeded");
|
|
3309
|
+
}
|
|
3310
|
+
withTimeout(fn, timeoutMs, signal) {
|
|
3311
|
+
return new Promise((resolve48, reject) => {
|
|
3312
|
+
const timer = setTimeout(() => {
|
|
3313
|
+
reject(new Error(`Request timed out after ${timeoutMs}ms`));
|
|
3314
|
+
}, timeoutMs);
|
|
3315
|
+
const onAbort = () => {
|
|
3316
|
+
clearTimeout(timer);
|
|
3317
|
+
reject(new Error("Request was aborted"));
|
|
3318
|
+
};
|
|
3319
|
+
if (signal) {
|
|
3320
|
+
if (signal.aborted) {
|
|
3321
|
+
clearTimeout(timer);
|
|
3322
|
+
reject(new Error("Request was aborted"));
|
|
3323
|
+
return;
|
|
3324
|
+
}
|
|
3325
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
3326
|
+
}
|
|
3327
|
+
fn().then((result) => {
|
|
3328
|
+
clearTimeout(timer);
|
|
3329
|
+
if (signal) {
|
|
3330
|
+
signal.removeEventListener("abort", onAbort);
|
|
3331
|
+
}
|
|
3332
|
+
resolve48(result);
|
|
3333
|
+
}).catch((error) => {
|
|
3334
|
+
clearTimeout(timer);
|
|
3335
|
+
if (signal) {
|
|
3336
|
+
signal.removeEventListener("abort", onAbort);
|
|
3337
|
+
}
|
|
3338
|
+
reject(error);
|
|
3339
|
+
});
|
|
3340
|
+
});
|
|
3341
|
+
}
|
|
3342
|
+
sleep(ms) {
|
|
3343
|
+
return new Promise((resolve48) => setTimeout(resolve48, ms));
|
|
3344
|
+
}
|
|
3345
|
+
};
|
|
3346
|
+
}
|
|
3347
|
+
});
|
|
3348
|
+
|
|
3349
|
+
// src/model/StreamHandler.ts
|
|
3350
|
+
var StreamHandler;
|
|
3351
|
+
var init_StreamHandler = __esm({
|
|
3352
|
+
"src/model/StreamHandler.ts"() {
|
|
3353
|
+
"use strict";
|
|
3354
|
+
StreamHandler = class {
|
|
3355
|
+
textBuffer;
|
|
3356
|
+
thinkingBuffer;
|
|
3357
|
+
toolCallBuffer;
|
|
3358
|
+
finished;
|
|
3359
|
+
constructor() {
|
|
3360
|
+
this.textBuffer = "";
|
|
3361
|
+
this.thinkingBuffer = "";
|
|
3362
|
+
this.toolCallBuffer = /* @__PURE__ */ new Map();
|
|
3363
|
+
this.finished = false;
|
|
3364
|
+
}
|
|
3365
|
+
reset() {
|
|
3366
|
+
this.textBuffer = "";
|
|
3367
|
+
this.thinkingBuffer = "";
|
|
3368
|
+
this.toolCallBuffer.clear();
|
|
3369
|
+
this.finished = false;
|
|
3370
|
+
}
|
|
3371
|
+
handleEvent(event, data) {
|
|
3372
|
+
if (event === "[DONE]" || data === "[DONE]") {
|
|
3373
|
+
this.finished = true;
|
|
3374
|
+
return { type: "done" };
|
|
3375
|
+
}
|
|
3376
|
+
if (!data) {
|
|
3377
|
+
return null;
|
|
3378
|
+
}
|
|
3379
|
+
try {
|
|
3380
|
+
const parsed = JSON.parse(data);
|
|
3381
|
+
const choices = parsed.choices;
|
|
3382
|
+
if (!choices || choices.length === 0) {
|
|
3383
|
+
return null;
|
|
3384
|
+
}
|
|
3385
|
+
const choice = choices[0];
|
|
3386
|
+
const delta = choice.delta;
|
|
3387
|
+
if (!delta) {
|
|
3388
|
+
return null;
|
|
3389
|
+
}
|
|
3390
|
+
if (delta.reasoning_content) {
|
|
3391
|
+
const thinkingChunk = delta.reasoning_content;
|
|
3392
|
+
this.thinkingBuffer += thinkingChunk;
|
|
3393
|
+
return {
|
|
3394
|
+
type: "thinking",
|
|
3395
|
+
content: thinkingChunk
|
|
3396
|
+
};
|
|
3397
|
+
}
|
|
3398
|
+
if (delta.tool_calls) {
|
|
3399
|
+
return this.handleToolCallsDelta(delta.tool_calls);
|
|
3400
|
+
}
|
|
3401
|
+
if (delta.content) {
|
|
3402
|
+
const textChunk = delta.content;
|
|
3403
|
+
this.textBuffer += textChunk;
|
|
3404
|
+
return {
|
|
3405
|
+
type: "text",
|
|
3406
|
+
content: textChunk
|
|
3407
|
+
};
|
|
3408
|
+
}
|
|
3409
|
+
const finishReason = choice.finish_reason;
|
|
3410
|
+
if (finishReason === "stop" || finishReason === "length" || finishReason === "tool_calls") {
|
|
3411
|
+
this.finished = true;
|
|
3412
|
+
return { type: "done" };
|
|
3413
|
+
}
|
|
3414
|
+
} catch {
|
|
3415
|
+
return {
|
|
3416
|
+
type: "error",
|
|
3417
|
+
error: `Failed to parse SSE data: ${data.slice(0, 200)}`
|
|
3418
|
+
};
|
|
3419
|
+
}
|
|
3420
|
+
return null;
|
|
3421
|
+
}
|
|
3422
|
+
getAccumulatedText() {
|
|
3423
|
+
return this.textBuffer;
|
|
3424
|
+
}
|
|
3425
|
+
getAccumulatedThinking() {
|
|
3426
|
+
return this.thinkingBuffer;
|
|
3427
|
+
}
|
|
3428
|
+
getToolCalls() {
|
|
3429
|
+
const result = [];
|
|
3430
|
+
for (let i = 0; i < this.toolCallBuffer.size; i++) {
|
|
3431
|
+
const tc = this.toolCallBuffer.get(i);
|
|
3432
|
+
if (tc) {
|
|
3433
|
+
result.push(tc);
|
|
3434
|
+
}
|
|
3435
|
+
}
|
|
3436
|
+
return result;
|
|
3437
|
+
}
|
|
3438
|
+
isFinished() {
|
|
3439
|
+
return this.finished;
|
|
3440
|
+
}
|
|
3441
|
+
handleToolCallsDelta(toolCalls) {
|
|
3442
|
+
const results = [];
|
|
3443
|
+
for (const tc of toolCalls) {
|
|
3444
|
+
const index = tc.index;
|
|
3445
|
+
const id = tc.id;
|
|
3446
|
+
const fn = tc.function;
|
|
3447
|
+
if (!this.toolCallBuffer.has(index)) {
|
|
3448
|
+
this.toolCallBuffer.set(index, {
|
|
3449
|
+
id: id ?? "",
|
|
3450
|
+
name: fn?.name ?? "",
|
|
3451
|
+
arguments: {},
|
|
3452
|
+
index
|
|
3453
|
+
});
|
|
3454
|
+
}
|
|
3455
|
+
const existing = this.toolCallBuffer.get(index);
|
|
3456
|
+
if (id) {
|
|
3457
|
+
existing.id = id;
|
|
3458
|
+
}
|
|
3459
|
+
if (fn?.name) {
|
|
3460
|
+
existing.name = fn.name;
|
|
3461
|
+
}
|
|
3462
|
+
if (fn?.arguments) {
|
|
3463
|
+
try {
|
|
3464
|
+
const argsStr = fn.arguments;
|
|
3465
|
+
const parsed = JSON.parse(argsStr);
|
|
3466
|
+
existing.arguments = { ...existing.arguments, ...parsed };
|
|
3467
|
+
} catch {
|
|
3468
|
+
existing.arguments = existing.arguments || {};
|
|
3469
|
+
}
|
|
3470
|
+
}
|
|
3471
|
+
const entry = this.toolCallBuffer.get(index);
|
|
3472
|
+
if (entry) {
|
|
3473
|
+
results.push({
|
|
3474
|
+
type: "tool_call",
|
|
3475
|
+
tool_call: { ...entry, arguments: { ...entry.arguments } }
|
|
3476
|
+
});
|
|
3477
|
+
}
|
|
3478
|
+
}
|
|
3479
|
+
return results;
|
|
3480
|
+
}
|
|
3481
|
+
};
|
|
3482
|
+
}
|
|
3483
|
+
});
|
|
3484
|
+
|
|
3485
|
+
// src/core/logger.ts
|
|
3486
|
+
import { appendFileSync, existsSync as existsSync8, mkdirSync as mkdirSync5 } from "fs";
|
|
3487
|
+
import { join as join7, dirname as dirname2 } from "path";
|
|
3488
|
+
var LOG_LEVEL_RANK, Logger, logger;
|
|
3489
|
+
var init_logger = __esm({
|
|
3490
|
+
"src/core/logger.ts"() {
|
|
3491
|
+
"use strict";
|
|
3492
|
+
init_constants();
|
|
3493
|
+
LOG_LEVEL_RANK = {
|
|
3494
|
+
debug: 0,
|
|
3495
|
+
info: 1,
|
|
3496
|
+
warn: 2,
|
|
3497
|
+
error: 3
|
|
3498
|
+
};
|
|
3499
|
+
Logger = class {
|
|
3500
|
+
level;
|
|
3501
|
+
logDir;
|
|
3502
|
+
currentLogFile;
|
|
3503
|
+
constructor(level = "info", logDir) {
|
|
3504
|
+
this.level = level;
|
|
3505
|
+
this.logDir = logDir ?? DEEPER_LOGS_DIR;
|
|
3506
|
+
this.currentLogFile = this.generateLogFileName();
|
|
3507
|
+
this.ensureLogDir();
|
|
3508
|
+
}
|
|
3509
|
+
setLevel(level) {
|
|
3510
|
+
this.level = level;
|
|
3511
|
+
}
|
|
3512
|
+
debug(message, data) {
|
|
3513
|
+
this.log("debug", message, data);
|
|
3514
|
+
}
|
|
3515
|
+
info(message, data) {
|
|
3516
|
+
this.log("info", message, data);
|
|
3517
|
+
}
|
|
3518
|
+
warn(message, data) {
|
|
3519
|
+
this.log("warn", message, data);
|
|
3520
|
+
}
|
|
3521
|
+
error(message, data) {
|
|
3522
|
+
this.log("error", message, data);
|
|
3523
|
+
}
|
|
3524
|
+
log(level, message, data) {
|
|
3525
|
+
if (LOG_LEVEL_RANK[level] < LOG_LEVEL_RANK[this.level]) {
|
|
3526
|
+
return;
|
|
3527
|
+
}
|
|
3528
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
3529
|
+
const formatted = this.formatMessage(level, timestamp, message, data);
|
|
3530
|
+
switch (level) {
|
|
3531
|
+
case "error":
|
|
3532
|
+
console.error(formatted);
|
|
3533
|
+
break;
|
|
3534
|
+
case "warn":
|
|
3535
|
+
console.warn(formatted);
|
|
3536
|
+
break;
|
|
3537
|
+
default:
|
|
3538
|
+
console.log(formatted);
|
|
3539
|
+
break;
|
|
3540
|
+
}
|
|
3541
|
+
try {
|
|
3542
|
+
appendFileSync(this.currentLogFile, this.stripAnsi(formatted) + "\n", "utf-8");
|
|
3543
|
+
} catch {
|
|
3544
|
+
}
|
|
3545
|
+
}
|
|
3546
|
+
formatMessage(level, timestamp, message, data) {
|
|
3547
|
+
const levelUpper = level.toUpperCase();
|
|
3548
|
+
const base = `[${timestamp}] [${levelUpper}] ${message}`;
|
|
3549
|
+
if (data !== void 0) {
|
|
3550
|
+
if (typeof data === "string") {
|
|
3551
|
+
return `${base} ${data}`;
|
|
3552
|
+
}
|
|
3553
|
+
if (data instanceof Error) {
|
|
3554
|
+
return `${base} ${data.message}
|
|
3555
|
+
${data.stack ?? ""}`;
|
|
3556
|
+
}
|
|
3557
|
+
try {
|
|
3558
|
+
return `${base} ${JSON.stringify(data)}`;
|
|
3559
|
+
} catch {
|
|
3560
|
+
return `${base} [Unserializable data]`;
|
|
3561
|
+
}
|
|
3562
|
+
}
|
|
3563
|
+
return base;
|
|
3564
|
+
}
|
|
3565
|
+
stripAnsi(text) {
|
|
3566
|
+
return text.replace(/\x1b\[[0-9;]*m/g, "");
|
|
3567
|
+
}
|
|
3568
|
+
generateLogFileName() {
|
|
3569
|
+
const now = /* @__PURE__ */ new Date();
|
|
3570
|
+
const dateStr = now.toISOString().slice(0, 10).replace(/-/g, "");
|
|
3571
|
+
return join7(this.logDir, `deeper-${dateStr}.log`);
|
|
3572
|
+
}
|
|
3573
|
+
ensureLogDir() {
|
|
3574
|
+
const dir = dirname2(this.currentLogFile);
|
|
3575
|
+
if (!existsSync8(dir)) {
|
|
3576
|
+
mkdirSync5(dir, { recursive: true });
|
|
3577
|
+
}
|
|
3578
|
+
}
|
|
3579
|
+
};
|
|
3580
|
+
logger = new Logger();
|
|
3581
|
+
}
|
|
3582
|
+
});
|
|
3583
|
+
|
|
3584
|
+
// src/model/DeepSeekClient.ts
|
|
3585
|
+
var DEFAULT_TIMEOUT_MS, MAX_RETRIES, DeepSeekClient;
|
|
3586
|
+
var init_DeepSeekClient = __esm({
|
|
3587
|
+
"src/model/DeepSeekClient.ts"() {
|
|
3588
|
+
"use strict";
|
|
3589
|
+
init_RetryManager();
|
|
3590
|
+
init_StreamHandler();
|
|
3591
|
+
init_logger();
|
|
3592
|
+
DEFAULT_TIMEOUT_MS = 12e4;
|
|
3593
|
+
MAX_RETRIES = 3;
|
|
3594
|
+
DeepSeekClient = class {
|
|
3595
|
+
config;
|
|
3596
|
+
retryManager;
|
|
3597
|
+
constructor(config) {
|
|
3598
|
+
this.config = config;
|
|
3599
|
+
this.retryManager = new RetryManager(config.maxTokens > 0 ? MAX_RETRIES : MAX_RETRIES);
|
|
3600
|
+
}
|
|
3601
|
+
async chat(messages, tools, overrides) {
|
|
3602
|
+
const cfg = this.mergeConfig(overrides);
|
|
3603
|
+
const body = this.buildRequestBody(messages, tools, cfg, false);
|
|
3604
|
+
const response = await this.retryManager.execute(async () => {
|
|
3605
|
+
const result2 = await this.retryManager.withTimeout(
|
|
3606
|
+
() => this.makeRequest(cfg, body),
|
|
3607
|
+
cfg.maxTokens > 0 ? DEFAULT_TIMEOUT_MS : DEFAULT_TIMEOUT_MS
|
|
3608
|
+
);
|
|
3609
|
+
return result2;
|
|
3610
|
+
}, this.shouldRetry);
|
|
3611
|
+
const data = await response.json();
|
|
3612
|
+
if (!data.choices || data.choices.length === 0) {
|
|
3613
|
+
throw new Error("No choices returned from API");
|
|
3614
|
+
}
|
|
3615
|
+
const choice = data.choices[0];
|
|
3616
|
+
const message = choice.message;
|
|
3617
|
+
const result = {
|
|
3618
|
+
role: "assistant",
|
|
3619
|
+
content: message.content
|
|
3620
|
+
};
|
|
3621
|
+
if (message.tool_calls) {
|
|
3622
|
+
result.tool_calls = message.tool_calls.map((tc) => ({
|
|
3623
|
+
id: tc.id,
|
|
3624
|
+
name: tc.function.name,
|
|
3625
|
+
arguments: this.parseArguments(tc.function.arguments)
|
|
3626
|
+
}));
|
|
3627
|
+
}
|
|
3628
|
+
if (message.reasoning_content) {
|
|
3629
|
+
result.thinking = message.reasoning_content;
|
|
3630
|
+
}
|
|
3631
|
+
return result;
|
|
3632
|
+
}
|
|
3633
|
+
async chatStream(messages, tools, overrides) {
|
|
3634
|
+
const cfg = this.mergeConfig(overrides);
|
|
3635
|
+
const body = this.buildRequestBody(messages, tools, cfg, true);
|
|
3636
|
+
const response = await this.retryManager.execute(async () => {
|
|
3637
|
+
const result = await this.retryManager.withTimeout(
|
|
3638
|
+
() => this.makeRequest(cfg, body),
|
|
3639
|
+
DEFAULT_TIMEOUT_MS
|
|
3640
|
+
);
|
|
3641
|
+
return result;
|
|
3642
|
+
}, this.shouldRetry);
|
|
3643
|
+
if (!response.ok) {
|
|
3644
|
+
const errorBody = await response.text().catch(() => "");
|
|
3645
|
+
throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorBody}`);
|
|
3646
|
+
}
|
|
3647
|
+
if (!response.body) {
|
|
3648
|
+
throw new Error("Response body is empty");
|
|
3649
|
+
}
|
|
3650
|
+
return this.createStreamIterable(response.body);
|
|
3651
|
+
}
|
|
3652
|
+
async *createStreamIterable(body) {
|
|
3653
|
+
const handler = new StreamHandler();
|
|
3654
|
+
let buffer = "";
|
|
3655
|
+
const stream = body;
|
|
3656
|
+
for await (const chunk of stream) {
|
|
3657
|
+
const text = typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk);
|
|
3658
|
+
buffer += text;
|
|
3659
|
+
const lines = buffer.split("\n");
|
|
3660
|
+
buffer = lines.pop() ?? "";
|
|
3661
|
+
for (const line of lines) {
|
|
3662
|
+
const trimmed = line.trim();
|
|
3663
|
+
if (!trimmed || trimmed.startsWith(":")) {
|
|
3664
|
+
continue;
|
|
3665
|
+
}
|
|
3666
|
+
if (trimmed.startsWith("data: ")) {
|
|
3667
|
+
const data = trimmed.slice(6);
|
|
3668
|
+
const result = handler.handleEvent("message", data);
|
|
3669
|
+
if (result) {
|
|
3670
|
+
if (Array.isArray(result)) {
|
|
3671
|
+
for (const r2 of result) yield r2;
|
|
3672
|
+
} else yield result;
|
|
3673
|
+
}
|
|
3674
|
+
} else if (trimmed === "data: [DONE]") {
|
|
3675
|
+
yield { type: "done" };
|
|
3676
|
+
return;
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
if (buffer.trim()) {
|
|
3681
|
+
const trimmed = buffer.trim();
|
|
3682
|
+
if (trimmed.startsWith("data: ")) {
|
|
3683
|
+
const data = trimmed.slice(6);
|
|
3684
|
+
const result = handler.handleEvent("message", data);
|
|
3685
|
+
if (result) {
|
|
3686
|
+
if (Array.isArray(result)) {
|
|
3687
|
+
for (const r2 of result) yield r2;
|
|
3688
|
+
} else yield result;
|
|
3689
|
+
}
|
|
3690
|
+
}
|
|
3691
|
+
}
|
|
3692
|
+
if (!handler.isFinished()) {
|
|
3693
|
+
yield { type: "done" };
|
|
3694
|
+
}
|
|
3695
|
+
}
|
|
3696
|
+
buildRequestBody(messages, tools, config, stream) {
|
|
3697
|
+
const body = {
|
|
3698
|
+
model: config.model,
|
|
3699
|
+
messages: messages.map((m) => {
|
|
3700
|
+
const msg = {
|
|
3701
|
+
role: m.role,
|
|
3702
|
+
content: m.content
|
|
3703
|
+
};
|
|
3704
|
+
if (m.tool_calls) {
|
|
3705
|
+
msg.tool_calls = m.tool_calls.map((tc) => ({
|
|
3706
|
+
id: tc.id,
|
|
3707
|
+
type: "function",
|
|
3708
|
+
function: {
|
|
3709
|
+
name: tc.name,
|
|
3710
|
+
arguments: JSON.stringify(tc.arguments)
|
|
3711
|
+
}
|
|
3712
|
+
}));
|
|
3713
|
+
}
|
|
3714
|
+
if (m.tool_call_id) {
|
|
3715
|
+
msg.tool_call_id = m.tool_call_id;
|
|
3716
|
+
}
|
|
3717
|
+
if (m.name) {
|
|
3718
|
+
msg.name = m.name;
|
|
3719
|
+
}
|
|
3720
|
+
const rc = m.reasoning_content || m.thinking;
|
|
3721
|
+
if (rc) msg.reasoning_content = rc;
|
|
3722
|
+
return msg;
|
|
3723
|
+
}),
|
|
3724
|
+
temperature: config.temperature,
|
|
3725
|
+
max_tokens: config.maxTokens,
|
|
3726
|
+
stream
|
|
3727
|
+
};
|
|
3728
|
+
if (tools && tools.length > 0) {
|
|
3729
|
+
body.tools = tools.map((tool) => ({
|
|
3730
|
+
type: "function",
|
|
3731
|
+
function: {
|
|
3732
|
+
name: tool.name,
|
|
3733
|
+
description: tool.description,
|
|
3734
|
+
parameters: tool.parameters
|
|
3735
|
+
}
|
|
3736
|
+
}));
|
|
3737
|
+
body.tool_choice = "auto";
|
|
3738
|
+
}
|
|
3739
|
+
return JSON.stringify(body);
|
|
3740
|
+
}
|
|
3741
|
+
async makeRequest(config, body) {
|
|
3742
|
+
const url = `${config.baseUrl}/v1/chat/completions`;
|
|
3743
|
+
logger.debug("DeepSeek API request", { url, model: config.model });
|
|
3744
|
+
const response = await fetch(url, {
|
|
3745
|
+
method: "POST",
|
|
3746
|
+
headers: {
|
|
3747
|
+
"Content-Type": "application/json",
|
|
3748
|
+
"Authorization": `Bearer ${config.apiKey}`,
|
|
3749
|
+
"Accept": "application/json"
|
|
3750
|
+
},
|
|
3751
|
+
body
|
|
3752
|
+
});
|
|
3753
|
+
if (!response.ok) {
|
|
3754
|
+
const errorBody = await response.text().catch(() => "");
|
|
3755
|
+
logger.error("DeepSeek API error", {
|
|
3756
|
+
status: response.status,
|
|
3757
|
+
statusText: response.statusText,
|
|
3758
|
+
body: errorBody.slice(0, 500)
|
|
3759
|
+
});
|
|
3760
|
+
throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorBody.slice(0, 200)}`);
|
|
3761
|
+
}
|
|
3762
|
+
return response;
|
|
3763
|
+
}
|
|
3764
|
+
shouldRetry(error, attempt) {
|
|
3765
|
+
const message = error.message.toLowerCase();
|
|
3766
|
+
if (message.includes("429") || message.includes("rate limit")) {
|
|
3767
|
+
return true;
|
|
3768
|
+
}
|
|
3769
|
+
if (message.includes("5") && (message.includes("500") || message.includes("502") || message.includes("503") || message.includes("504"))) {
|
|
3770
|
+
return true;
|
|
3771
|
+
}
|
|
3772
|
+
if (message.includes("timeout") || message.includes("abort")) {
|
|
3773
|
+
return attempt < 2;
|
|
3774
|
+
}
|
|
3775
|
+
if (message.includes("econnreset") || message.includes("econnrefused")) {
|
|
3776
|
+
return true;
|
|
3777
|
+
}
|
|
3778
|
+
return false;
|
|
3779
|
+
}
|
|
3780
|
+
parseArguments(argsStr) {
|
|
3781
|
+
try {
|
|
3782
|
+
return JSON.parse(argsStr);
|
|
3783
|
+
} catch {
|
|
3784
|
+
return {};
|
|
3785
|
+
}
|
|
3786
|
+
}
|
|
3787
|
+
mergeConfig(overrides) {
|
|
3788
|
+
if (!overrides) {
|
|
3789
|
+
return this.config;
|
|
3790
|
+
}
|
|
3791
|
+
return {
|
|
3792
|
+
...this.config,
|
|
3793
|
+
...overrides,
|
|
3794
|
+
think: {
|
|
3795
|
+
...this.config.think,
|
|
3796
|
+
...overrides.think ?? {}
|
|
3797
|
+
}
|
|
3798
|
+
};
|
|
3799
|
+
}
|
|
3800
|
+
};
|
|
3801
|
+
}
|
|
3802
|
+
});
|
|
3803
|
+
|
|
3274
3804
|
// src/ui/ansi.ts
|
|
3275
3805
|
import process4 from "process";
|
|
3276
3806
|
function Oflush() {
|
|
@@ -3363,7 +3893,7 @@ var init_ansi = __esm({
|
|
|
3363
3893
|
});
|
|
3364
3894
|
|
|
3365
3895
|
// src/tools/builtin/filesystem/read_file.ts
|
|
3366
|
-
import { readFileSync as readFileSync5, existsSync as
|
|
3896
|
+
import { readFileSync as readFileSync5, existsSync as existsSync9 } from "fs";
|
|
3367
3897
|
import { resolve, normalize } from "path";
|
|
3368
3898
|
var read_file;
|
|
3369
3899
|
var init_read_file = __esm({
|
|
@@ -3387,11 +3917,11 @@ var init_read_file = __esm({
|
|
|
3387
3917
|
async execute(params) {
|
|
3388
3918
|
try {
|
|
3389
3919
|
const filePath = resolve(params.file_path);
|
|
3390
|
-
if (!
|
|
3920
|
+
if (!existsSync9(filePath)) {
|
|
3391
3921
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
3392
3922
|
}
|
|
3393
|
-
const
|
|
3394
|
-
const stat =
|
|
3923
|
+
const statSync13 = await import("fs").then((m) => m.statSync);
|
|
3924
|
+
const stat = statSync13(filePath);
|
|
3395
3925
|
if (stat.isDirectory()) {
|
|
3396
3926
|
return { success: false, error: `\u8DEF\u5F84\u662F\u76EE\u5F55\uFF0C\u4E0D\u662F\u6587\u4EF6: ${filePath}`, output: "" };
|
|
3397
3927
|
}
|
|
@@ -3425,9 +3955,9 @@ var init_read_file = __esm({
|
|
|
3425
3955
|
});
|
|
3426
3956
|
|
|
3427
3957
|
// src/tools/builtin/filesystem/write_file.ts
|
|
3428
|
-
import { existsSync as
|
|
3958
|
+
import { existsSync as existsSync10, mkdirSync as mkdirSync6 } from "fs";
|
|
3429
3959
|
import { writeFile as writeFile2, appendFile } from "fs/promises";
|
|
3430
|
-
import { dirname as
|
|
3960
|
+
import { dirname as dirname3, resolve as resolve2 } from "path";
|
|
3431
3961
|
var write_file;
|
|
3432
3962
|
var init_write_file = __esm({
|
|
3433
3963
|
"src/tools/builtin/filesystem/write_file.ts"() {
|
|
@@ -3458,8 +3988,8 @@ var init_write_file = __esm({
|
|
|
3458
3988
|
if (!content.trim() && !append) {
|
|
3459
3989
|
return { success: false, error: `\u5185\u5BB9\u4E3A\u7A7A\uFF0C\u8DF3\u8FC7\u5199\u5165: ${filePath}`, output: "" };
|
|
3460
3990
|
}
|
|
3461
|
-
const dir =
|
|
3462
|
-
if (!
|
|
3991
|
+
const dir = dirname3(filePath);
|
|
3992
|
+
if (!existsSync10(dir)) mkdirSync6(dir, { recursive: true });
|
|
3463
3993
|
if (append) {
|
|
3464
3994
|
await appendFile(filePath, content, "utf-8");
|
|
3465
3995
|
} else {
|
|
@@ -3476,7 +4006,7 @@ var init_write_file = __esm({
|
|
|
3476
4006
|
});
|
|
3477
4007
|
|
|
3478
4008
|
// src/tools/builtin/filesystem/edit_file.ts
|
|
3479
|
-
import { readFileSync as readFileSync6, writeFileSync as writeFileSync4, existsSync as
|
|
4009
|
+
import { readFileSync as readFileSync6, writeFileSync as writeFileSync4, existsSync as existsSync11 } from "fs";
|
|
3480
4010
|
import { resolve as resolve3 } from "path";
|
|
3481
4011
|
var edit_file;
|
|
3482
4012
|
var init_edit_file = __esm({
|
|
@@ -3504,7 +4034,7 @@ var init_edit_file = __esm({
|
|
|
3504
4034
|
const oldStr = params.old_str;
|
|
3505
4035
|
const newStr = params.new_str;
|
|
3506
4036
|
const replaceAll = params.replace_all ?? false;
|
|
3507
|
-
if (!
|
|
4037
|
+
if (!existsSync11(filePath)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
3508
4038
|
const content = readFileSync6(filePath, "utf-8");
|
|
3509
4039
|
const index = content.indexOf(oldStr);
|
|
3510
4040
|
if (index === -1) return { success: false, error: "\u672A\u627E\u5230\u5339\u914D\u7684\u5B57\u7B26\u4E32", output: "" };
|
|
@@ -3530,7 +4060,7 @@ var init_edit_file = __esm({
|
|
|
3530
4060
|
});
|
|
3531
4061
|
|
|
3532
4062
|
// src/tools/builtin/filesystem/delete_file.ts
|
|
3533
|
-
import { unlinkSync, existsSync as
|
|
4063
|
+
import { unlinkSync, existsSync as existsSync12 } from "fs";
|
|
3534
4064
|
import { resolve as resolve4 } from "path";
|
|
3535
4065
|
var delete_file;
|
|
3536
4066
|
var init_delete_file = __esm({
|
|
@@ -3552,7 +4082,7 @@ var init_delete_file = __esm({
|
|
|
3552
4082
|
async execute(params) {
|
|
3553
4083
|
try {
|
|
3554
4084
|
const filePath = resolve4(params.file_path);
|
|
3555
|
-
if (!
|
|
4085
|
+
if (!existsSync12(filePath)) {
|
|
3556
4086
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
3557
4087
|
}
|
|
3558
4088
|
unlinkSync(filePath);
|
|
@@ -3566,8 +4096,8 @@ var init_delete_file = __esm({
|
|
|
3566
4096
|
});
|
|
3567
4097
|
|
|
3568
4098
|
// src/tools/builtin/filesystem/list_dir.ts
|
|
3569
|
-
import { readdirSync as readdirSync3, existsSync as
|
|
3570
|
-
import { resolve as resolve5, join as
|
|
4099
|
+
import { readdirSync as readdirSync3, existsSync as existsSync13 } from "fs";
|
|
4100
|
+
import { resolve as resolve5, join as join8 } from "path";
|
|
3571
4101
|
var list_dir;
|
|
3572
4102
|
var init_list_dir = __esm({
|
|
3573
4103
|
"src/tools/builtin/filesystem/list_dir.ts"() {
|
|
@@ -3597,7 +4127,7 @@ var init_list_dir = __esm({
|
|
|
3597
4127
|
const isDir = entry.isDirectory();
|
|
3598
4128
|
lines.push(`${prefix}${isDir ? "\u{1F4C1}" : "\u{1F4C4}"} ${entry.name}`);
|
|
3599
4129
|
if (isDir && recursive) {
|
|
3600
|
-
walk2(
|
|
4130
|
+
walk2(join8(dir, entry.name), depth + 1, prefix + " ");
|
|
3601
4131
|
}
|
|
3602
4132
|
}
|
|
3603
4133
|
} catch {
|
|
@@ -3608,10 +4138,10 @@ var init_list_dir = __esm({
|
|
|
3608
4138
|
const dirPath = resolve5(params.dir_path);
|
|
3609
4139
|
const recursive = params.recursive ?? false;
|
|
3610
4140
|
const maxDepth = params.max_depth ?? 3;
|
|
3611
|
-
if (!
|
|
4141
|
+
if (!existsSync13(dirPath)) {
|
|
3612
4142
|
return { success: false, error: `\u76EE\u5F55\u4E0D\u5B58\u5728: ${dirPath}`, output: "" };
|
|
3613
4143
|
}
|
|
3614
|
-
const { statSync:
|
|
4144
|
+
const { statSync: statSync13 } = await import("fs");
|
|
3615
4145
|
const lines = [];
|
|
3616
4146
|
walk2(dirPath, 1, "");
|
|
3617
4147
|
return { success: true, output: lines.join("\n"), metadata: { path: dirPath, entries: lines.length } };
|
|
@@ -3674,8 +4204,8 @@ var init_glob_find = __esm({
|
|
|
3674
4204
|
});
|
|
3675
4205
|
|
|
3676
4206
|
// src/tools/builtin/filesystem/move_file.ts
|
|
3677
|
-
import { renameSync, existsSync as
|
|
3678
|
-
import { resolve as resolve7, dirname as
|
|
4207
|
+
import { renameSync, existsSync as existsSync14 } from "fs";
|
|
4208
|
+
import { resolve as resolve7, dirname as dirname4 } from "path";
|
|
3679
4209
|
var move_file;
|
|
3680
4210
|
var init_move_file = __esm({
|
|
3681
4211
|
"src/tools/builtin/filesystem/move_file.ts"() {
|
|
@@ -3698,13 +4228,13 @@ var init_move_file = __esm({
|
|
|
3698
4228
|
try {
|
|
3699
4229
|
const source = resolve7(params.source);
|
|
3700
4230
|
const dest = resolve7(params.destination);
|
|
3701
|
-
if (!
|
|
4231
|
+
if (!existsSync14(source)) {
|
|
3702
4232
|
return { success: false, error: `\u6E90\u6587\u4EF6\u4E0D\u5B58\u5728: ${source}`, output: "" };
|
|
3703
4233
|
}
|
|
3704
|
-
const destDir =
|
|
3705
|
-
const { mkdirSync:
|
|
3706
|
-
if (!
|
|
3707
|
-
|
|
4234
|
+
const destDir = dirname4(dest);
|
|
4235
|
+
const { mkdirSync: mkdirSync16 } = await import("fs");
|
|
4236
|
+
if (!existsSync14(destDir)) {
|
|
4237
|
+
mkdirSync16(destDir, { recursive: true });
|
|
3708
4238
|
}
|
|
3709
4239
|
renameSync(source, dest);
|
|
3710
4240
|
return { success: true, output: `\u5DF2\u79FB\u52A8: ${source} -> ${dest}` };
|
|
@@ -3717,8 +4247,8 @@ var init_move_file = __esm({
|
|
|
3717
4247
|
});
|
|
3718
4248
|
|
|
3719
4249
|
// src/tools/builtin/filesystem/copy_file.ts
|
|
3720
|
-
import { copyFileSync, existsSync as
|
|
3721
|
-
import { resolve as resolve8, dirname as
|
|
4250
|
+
import { copyFileSync, existsSync as existsSync15, mkdirSync as mkdirSync7 } from "fs";
|
|
4251
|
+
import { resolve as resolve8, dirname as dirname5 } from "path";
|
|
3722
4252
|
var copy_file;
|
|
3723
4253
|
var init_copy_file = __esm({
|
|
3724
4254
|
"src/tools/builtin/filesystem/copy_file.ts"() {
|
|
@@ -3741,12 +4271,12 @@ var init_copy_file = __esm({
|
|
|
3741
4271
|
try {
|
|
3742
4272
|
const source = resolve8(params.source);
|
|
3743
4273
|
const dest = resolve8(params.destination);
|
|
3744
|
-
if (!
|
|
4274
|
+
if (!existsSync15(source)) {
|
|
3745
4275
|
return { success: false, error: `\u6E90\u6587\u4EF6\u4E0D\u5B58\u5728: ${source}`, output: "" };
|
|
3746
4276
|
}
|
|
3747
|
-
const destDir =
|
|
3748
|
-
if (!
|
|
3749
|
-
|
|
4277
|
+
const destDir = dirname5(dest);
|
|
4278
|
+
if (!existsSync15(destDir)) {
|
|
4279
|
+
mkdirSync7(destDir, { recursive: true });
|
|
3750
4280
|
}
|
|
3751
4281
|
copyFileSync(source, dest);
|
|
3752
4282
|
return { success: true, output: `\u5DF2\u590D\u5236: ${source} -> ${dest}` };
|
|
@@ -3759,7 +4289,7 @@ var init_copy_file = __esm({
|
|
|
3759
4289
|
});
|
|
3760
4290
|
|
|
3761
4291
|
// src/tools/builtin/filesystem/create_dir.ts
|
|
3762
|
-
import { mkdirSync as
|
|
4292
|
+
import { mkdirSync as mkdirSync8, existsSync as existsSync16 } from "fs";
|
|
3763
4293
|
import { resolve as resolve9 } from "path";
|
|
3764
4294
|
var create_dir;
|
|
3765
4295
|
var init_create_dir = __esm({
|
|
@@ -3781,10 +4311,10 @@ var init_create_dir = __esm({
|
|
|
3781
4311
|
async execute(params) {
|
|
3782
4312
|
try {
|
|
3783
4313
|
const dirPath = resolve9(params.dir_path);
|
|
3784
|
-
if (
|
|
4314
|
+
if (existsSync16(dirPath)) {
|
|
3785
4315
|
return { success: true, output: `\u76EE\u5F55\u5DF2\u5B58\u5728: ${dirPath}` };
|
|
3786
4316
|
}
|
|
3787
|
-
|
|
4317
|
+
mkdirSync8(dirPath, { recursive: true });
|
|
3788
4318
|
return { success: true, output: `\u76EE\u5F55\u5DF2\u521B\u5EFA: ${dirPath}` };
|
|
3789
4319
|
} catch (err) {
|
|
3790
4320
|
return { success: false, error: err.message, output: "" };
|
|
@@ -3795,7 +4325,7 @@ var init_create_dir = __esm({
|
|
|
3795
4325
|
});
|
|
3796
4326
|
|
|
3797
4327
|
// src/tools/builtin/filesystem/file_info.ts
|
|
3798
|
-
import { statSync, existsSync as
|
|
4328
|
+
import { statSync, existsSync as existsSync17 } from "fs";
|
|
3799
4329
|
import { resolve as resolve10, basename } from "path";
|
|
3800
4330
|
function formatSize(bytes) {
|
|
3801
4331
|
if (bytes < 1024) return `${bytes} B`;
|
|
@@ -3823,7 +4353,7 @@ var init_file_info = __esm({
|
|
|
3823
4353
|
async execute(params) {
|
|
3824
4354
|
try {
|
|
3825
4355
|
const filePath = resolve10(params.file_path);
|
|
3826
|
-
if (!
|
|
4356
|
+
if (!existsSync17(filePath)) {
|
|
3827
4357
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
3828
4358
|
}
|
|
3829
4359
|
const stat = statSync(filePath);
|
|
@@ -3852,7 +4382,7 @@ var init_file_info = __esm({
|
|
|
3852
4382
|
});
|
|
3853
4383
|
|
|
3854
4384
|
// src/tools/builtin/filesystem/watch_file.ts
|
|
3855
|
-
import { existsSync as
|
|
4385
|
+
import { existsSync as existsSync18 } from "fs";
|
|
3856
4386
|
import { resolve as resolve11 } from "path";
|
|
3857
4387
|
var watch_file;
|
|
3858
4388
|
var init_watch_file = __esm({
|
|
@@ -3874,7 +4404,7 @@ var init_watch_file = __esm({
|
|
|
3874
4404
|
async execute(params) {
|
|
3875
4405
|
try {
|
|
3876
4406
|
const filePath = resolve11(params.file_path);
|
|
3877
|
-
if (!
|
|
4407
|
+
if (!existsSync18(filePath)) {
|
|
3878
4408
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
3879
4409
|
}
|
|
3880
4410
|
return {
|
|
@@ -3891,7 +4421,7 @@ var init_watch_file = __esm({
|
|
|
3891
4421
|
});
|
|
3892
4422
|
|
|
3893
4423
|
// src/tools/builtin/filesystem/batch_read.ts
|
|
3894
|
-
import { readFileSync as readFileSync7, existsSync as
|
|
4424
|
+
import { readFileSync as readFileSync7, existsSync as existsSync19 } from "fs";
|
|
3895
4425
|
import { resolve as resolve12 } from "path";
|
|
3896
4426
|
var batch_read;
|
|
3897
4427
|
var init_batch_read = __esm({
|
|
@@ -3919,7 +4449,7 @@ var init_batch_read = __esm({
|
|
|
3919
4449
|
for (const p of paths) {
|
|
3920
4450
|
const abs = resolve12(p);
|
|
3921
4451
|
try {
|
|
3922
|
-
if (!
|
|
4452
|
+
if (!existsSync19(abs)) {
|
|
3923
4453
|
results[p] = { content: "", error: "\u6587\u4EF6\u4E0D\u5B58\u5728" };
|
|
3924
4454
|
continue;
|
|
3925
4455
|
}
|
|
@@ -3952,9 +4482,9 @@ ${r2.content}`;
|
|
|
3952
4482
|
});
|
|
3953
4483
|
|
|
3954
4484
|
// src/tools/builtin/filesystem/batch_write.ts
|
|
3955
|
-
import { existsSync as
|
|
4485
|
+
import { existsSync as existsSync20, mkdirSync as mkdirSync9 } from "fs";
|
|
3956
4486
|
import { writeFile as writeFile3 } from "fs/promises";
|
|
3957
|
-
import { resolve as resolve13, dirname as
|
|
4487
|
+
import { resolve as resolve13, dirname as dirname6 } from "path";
|
|
3958
4488
|
var batch_write;
|
|
3959
4489
|
var init_batch_write = __esm({
|
|
3960
4490
|
"src/tools/builtin/filesystem/batch_write.ts"() {
|
|
@@ -4001,9 +4531,9 @@ var init_batch_write = __esm({
|
|
|
4001
4531
|
skipped.push(`${abs} (\u5185\u5BB9\u8FC7\u5927)`);
|
|
4002
4532
|
continue;
|
|
4003
4533
|
}
|
|
4004
|
-
const dir =
|
|
4005
|
-
if (!
|
|
4006
|
-
|
|
4534
|
+
const dir = dirname6(abs);
|
|
4535
|
+
if (!existsSync20(dir)) {
|
|
4536
|
+
mkdirSync9(dir, { recursive: true });
|
|
4007
4537
|
}
|
|
4008
4538
|
await writeFile3(abs, content, "utf-8");
|
|
4009
4539
|
results.push(abs);
|
|
@@ -4025,7 +4555,7 @@ var init_batch_write = __esm({
|
|
|
4025
4555
|
});
|
|
4026
4556
|
|
|
4027
4557
|
// src/tools/builtin/filesystem/diff_files.ts
|
|
4028
|
-
import { readFileSync as readFileSync8, existsSync as
|
|
4558
|
+
import { readFileSync as readFileSync8, existsSync as existsSync21 } from "fs";
|
|
4029
4559
|
import { resolve as resolve14 } from "path";
|
|
4030
4560
|
var diff_files;
|
|
4031
4561
|
var init_diff_files = __esm({
|
|
@@ -4051,8 +4581,8 @@ var init_diff_files = __esm({
|
|
|
4051
4581
|
const fileA = resolve14(params.file_a);
|
|
4052
4582
|
const fileB = resolve14(params.file_b);
|
|
4053
4583
|
const contextLines = params.context_lines ?? 3;
|
|
4054
|
-
if (!
|
|
4055
|
-
if (!
|
|
4584
|
+
if (!existsSync21(fileA)) return { success: false, error: `\u6587\u4EF6A\u4E0D\u5B58\u5728: ${fileA}`, output: "" };
|
|
4585
|
+
if (!existsSync21(fileB)) return { success: false, error: `\u6587\u4EF6B\u4E0D\u5B58\u5728: ${fileB}`, output: "" };
|
|
4056
4586
|
const contentA = readFileSync8(fileA, "utf-8");
|
|
4057
4587
|
const contentB = readFileSync8(fileB, "utf-8");
|
|
4058
4588
|
const Diff = await import("diff");
|
|
@@ -4083,7 +4613,7 @@ var init_diff_files = __esm({
|
|
|
4083
4613
|
});
|
|
4084
4614
|
|
|
4085
4615
|
// src/tools/builtin/filesystem/merge_files.ts
|
|
4086
|
-
import { readFileSync as readFileSync9, writeFileSync as writeFileSync5, existsSync as
|
|
4616
|
+
import { readFileSync as readFileSync9, writeFileSync as writeFileSync5, existsSync as existsSync22 } from "fs";
|
|
4087
4617
|
import { resolve as resolve15 } from "path";
|
|
4088
4618
|
var merge_files;
|
|
4089
4619
|
var init_merge_files = __esm({
|
|
@@ -4112,7 +4642,7 @@ var init_merge_files = __esm({
|
|
|
4112
4642
|
const parts = [];
|
|
4113
4643
|
for (const p of paths) {
|
|
4114
4644
|
const abs = resolve15(p);
|
|
4115
|
-
if (!
|
|
4645
|
+
if (!existsSync22(abs)) {
|
|
4116
4646
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
4117
4647
|
}
|
|
4118
4648
|
parts.push(readFileSync9(abs, "utf-8"));
|
|
@@ -4133,7 +4663,7 @@ var init_merge_files = __esm({
|
|
|
4133
4663
|
});
|
|
4134
4664
|
|
|
4135
4665
|
// src/tools/builtin/search/grep_search.ts
|
|
4136
|
-
import { readFileSync as readFileSync10, existsSync as
|
|
4666
|
+
import { readFileSync as readFileSync10, existsSync as existsSync23, statSync as statSync2 } from "fs";
|
|
4137
4667
|
import { resolve as resolve16 } from "path";
|
|
4138
4668
|
var grep_search;
|
|
4139
4669
|
var init_grep_search = __esm({
|
|
@@ -4198,7 +4728,7 @@ var init_grep_search = __esm({
|
|
|
4198
4728
|
const maxFileSize = 5 * 1024 * 1024;
|
|
4199
4729
|
if (filePath) {
|
|
4200
4730
|
const abs = resolve16(filePath);
|
|
4201
|
-
if (
|
|
4731
|
+
if (existsSync23(abs)) searchFile2(abs);
|
|
4202
4732
|
} else if (dirPath) {
|
|
4203
4733
|
const fg2 = await import("fast-glob");
|
|
4204
4734
|
const pattern2 = glob ? `**/${glob}` : "**/*";
|
|
@@ -4671,7 +5201,7 @@ var init_fuzzy_find = __esm({
|
|
|
4671
5201
|
});
|
|
4672
5202
|
|
|
4673
5203
|
// src/tools/builtin/search/regex_find.ts
|
|
4674
|
-
import { readFileSync as readFileSync16, writeFileSync as writeFileSync6, existsSync as
|
|
5204
|
+
import { readFileSync as readFileSync16, writeFileSync as writeFileSync6, existsSync as existsSync29 } from "fs";
|
|
4675
5205
|
import { resolve as resolve23 } from "path";
|
|
4676
5206
|
var regex_find;
|
|
4677
5207
|
var init_regex_find = __esm({
|
|
@@ -4737,7 +5267,7 @@ var init_regex_find = __esm({
|
|
|
4737
5267
|
}
|
|
4738
5268
|
if (filePath) {
|
|
4739
5269
|
const abs = resolve23(filePath);
|
|
4740
|
-
if (
|
|
5270
|
+
if (existsSync29(abs)) await processFile(abs);
|
|
4741
5271
|
} else if (dirPath) {
|
|
4742
5272
|
const fg2 = await import("fast-glob");
|
|
4743
5273
|
const files = await fg2.default(filePattern, {
|
|
@@ -5315,7 +5845,7 @@ ${stderr}` : ""),
|
|
|
5315
5845
|
});
|
|
5316
5846
|
|
|
5317
5847
|
// src/tools/builtin/shell/shell_script.ts
|
|
5318
|
-
import { existsSync as
|
|
5848
|
+
import { existsSync as existsSync30 } from "fs";
|
|
5319
5849
|
import { exec as execCb2 } from "child_process";
|
|
5320
5850
|
import { promisify as promisify2 } from "util";
|
|
5321
5851
|
import { resolve as resolve24 } from "path";
|
|
@@ -5346,7 +5876,7 @@ var init_shell_script = __esm({
|
|
|
5346
5876
|
const args = params.args ?? [];
|
|
5347
5877
|
const cwd = params.cwd ?? process.cwd();
|
|
5348
5878
|
const timeout = params.timeout_ms ?? 3e4;
|
|
5349
|
-
if (!
|
|
5879
|
+
if (!existsSync30(scriptPath)) {
|
|
5350
5880
|
return { success: false, error: `\u811A\u672C\u6587\u4EF6\u4E0D\u5B58\u5728: ${scriptPath}`, output: "" };
|
|
5351
5881
|
}
|
|
5352
5882
|
const shell = process.platform === "win32" ? process.env.ComSpec || "cmd.exe" : process.env.SHELL || "/bin/sh";
|
|
@@ -6017,8 +6547,8 @@ var init_http_request = __esm({
|
|
|
6017
6547
|
});
|
|
6018
6548
|
|
|
6019
6549
|
// src/tools/builtin/network/download_file.ts
|
|
6020
|
-
import { createWriteStream, existsSync as
|
|
6021
|
-
import { dirname as
|
|
6550
|
+
import { createWriteStream, existsSync as existsSync31, mkdirSync as mkdirSync10 } from "fs";
|
|
6551
|
+
import { dirname as dirname7, resolve as resolve25 } from "path";
|
|
6022
6552
|
import { pipeline } from "stream/promises";
|
|
6023
6553
|
function formatSize2(bytes) {
|
|
6024
6554
|
if (bytes < 1024) return `${bytes} B`;
|
|
@@ -6049,9 +6579,9 @@ var init_download_file = __esm({
|
|
|
6049
6579
|
const url = params.url;
|
|
6050
6580
|
const filePath = resolve25(params.file_path);
|
|
6051
6581
|
const timeout = params.timeout_ms ?? 6e4;
|
|
6052
|
-
const dir =
|
|
6053
|
-
if (!
|
|
6054
|
-
|
|
6582
|
+
const dir = dirname7(filePath);
|
|
6583
|
+
if (!existsSync31(dir)) {
|
|
6584
|
+
mkdirSync10(dir, { recursive: true });
|
|
6055
6585
|
}
|
|
6056
6586
|
const controller = new AbortController();
|
|
6057
6587
|
const timer = setTimeout(() => controller.abort(), timeout);
|
|
@@ -6631,7 +7161,7 @@ var init_proxy_request = __esm({
|
|
|
6631
7161
|
});
|
|
6632
7162
|
|
|
6633
7163
|
// src/tools/builtin/code/parse_ast.ts
|
|
6634
|
-
import { readFileSync as readFileSync18, existsSync as
|
|
7164
|
+
import { readFileSync as readFileSync18, existsSync as existsSync32 } from "fs";
|
|
6635
7165
|
import { resolve as resolve26 } from "path";
|
|
6636
7166
|
var parse_ast;
|
|
6637
7167
|
var init_parse_ast = __esm({
|
|
@@ -6655,7 +7185,7 @@ var init_parse_ast = __esm({
|
|
|
6655
7185
|
try {
|
|
6656
7186
|
const filePath = resolve26(params.file_path);
|
|
6657
7187
|
const language = params.language ?? "typescript";
|
|
6658
|
-
if (!
|
|
7188
|
+
if (!existsSync32(filePath)) {
|
|
6659
7189
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
6660
7190
|
}
|
|
6661
7191
|
const content = readFileSync18(filePath, "utf-8");
|
|
@@ -6716,7 +7246,7 @@ var init_parse_ast = __esm({
|
|
|
6716
7246
|
});
|
|
6717
7247
|
|
|
6718
7248
|
// src/tools/builtin/code/format_code.ts
|
|
6719
|
-
import { readFileSync as readFileSync19, existsSync as
|
|
7249
|
+
import { readFileSync as readFileSync19, existsSync as existsSync33 } from "fs";
|
|
6720
7250
|
import { resolve as resolve27 } from "path";
|
|
6721
7251
|
var format_code;
|
|
6722
7252
|
var init_format_code = __esm({
|
|
@@ -6740,7 +7270,7 @@ var init_format_code = __esm({
|
|
|
6740
7270
|
try {
|
|
6741
7271
|
const filePath = resolve27(params.file_path);
|
|
6742
7272
|
const language = params.language ?? "typescript";
|
|
6743
|
-
if (!
|
|
7273
|
+
if (!existsSync33(filePath)) {
|
|
6744
7274
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
6745
7275
|
}
|
|
6746
7276
|
try {
|
|
@@ -6964,7 +7494,7 @@ var init_generate_code = __esm({
|
|
|
6964
7494
|
});
|
|
6965
7495
|
|
|
6966
7496
|
// src/tools/builtin/code/refactor_code.ts
|
|
6967
|
-
import { readFileSync as readFileSync20, existsSync as
|
|
7497
|
+
import { readFileSync as readFileSync20, existsSync as existsSync34 } from "fs";
|
|
6968
7498
|
import { resolve as resolve30 } from "path";
|
|
6969
7499
|
function escapeRegex3(s) {
|
|
6970
7500
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -6995,7 +7525,7 @@ var init_refactor_code = __esm({
|
|
|
6995
7525
|
const action = params.action;
|
|
6996
7526
|
const target = params.target;
|
|
6997
7527
|
const newName = params.new_name;
|
|
6998
|
-
if (!
|
|
7528
|
+
if (!existsSync34(filePath)) {
|
|
6999
7529
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
7000
7530
|
}
|
|
7001
7531
|
const content = readFileSync20(filePath, "utf-8");
|
|
@@ -7032,7 +7562,7 @@ var init_refactor_code = __esm({
|
|
|
7032
7562
|
});
|
|
7033
7563
|
|
|
7034
7564
|
// src/tools/builtin/code/extract_function.ts
|
|
7035
|
-
import { readFileSync as readFileSync21, existsSync as
|
|
7565
|
+
import { readFileSync as readFileSync21, existsSync as existsSync35 } from "fs";
|
|
7036
7566
|
import { resolve as resolve31 } from "path";
|
|
7037
7567
|
function escapeRegex4(s) {
|
|
7038
7568
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -7059,7 +7589,7 @@ var init_extract_function = __esm({
|
|
|
7059
7589
|
try {
|
|
7060
7590
|
const filePath = resolve31(params.file_path);
|
|
7061
7591
|
const funcName = params.function_name;
|
|
7062
|
-
if (!
|
|
7592
|
+
if (!existsSync35(filePath)) {
|
|
7063
7593
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
7064
7594
|
}
|
|
7065
7595
|
const content = readFileSync21(filePath, "utf-8");
|
|
@@ -7187,7 +7717,7 @@ var init_analyze_deps = __esm({
|
|
|
7187
7717
|
});
|
|
7188
7718
|
|
|
7189
7719
|
// src/tools/builtin/code/import_organizer.ts
|
|
7190
|
-
import { readFileSync as readFileSync23, existsSync as
|
|
7720
|
+
import { readFileSync as readFileSync23, existsSync as existsSync37 } from "fs";
|
|
7191
7721
|
import { resolve as resolve33 } from "path";
|
|
7192
7722
|
var import_organizer;
|
|
7193
7723
|
var init_import_organizer = __esm({
|
|
@@ -7215,7 +7745,7 @@ var init_import_organizer = __esm({
|
|
|
7215
7745
|
const groupBy = params.group_by ?? "source";
|
|
7216
7746
|
const sort = params.sort ?? true;
|
|
7217
7747
|
const removeUnused = params.remove_unused ?? false;
|
|
7218
|
-
if (!
|
|
7748
|
+
if (!existsSync37(filePath)) {
|
|
7219
7749
|
return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
7220
7750
|
}
|
|
7221
7751
|
const content = readFileSync23(filePath, "utf-8");
|
|
@@ -7390,10 +7920,10 @@ var init_code_metrics = __esm({
|
|
|
7390
7920
|
});
|
|
7391
7921
|
|
|
7392
7922
|
// src/core/bugscan.ts
|
|
7393
|
-
import { existsSync as
|
|
7923
|
+
import { existsSync as existsSync39, readFileSync as readFileSync25, statSync as statSync10 } from "fs";
|
|
7394
7924
|
import { extname, basename as basename3 } from "path";
|
|
7395
7925
|
function analyzeFile(filePath) {
|
|
7396
|
-
if (!
|
|
7926
|
+
if (!existsSync39(filePath)) return [];
|
|
7397
7927
|
try {
|
|
7398
7928
|
if (statSync10(filePath).size > MAX_FILE_SIZE) return [];
|
|
7399
7929
|
} catch {
|
|
@@ -7529,7 +8059,7 @@ var init_bugscan = __esm({
|
|
|
7529
8059
|
|
|
7530
8060
|
// src/tools/builtin/code/bug_scan.ts
|
|
7531
8061
|
import { statSync as statSync11 } from "fs";
|
|
7532
|
-
import { isAbsolute, join as
|
|
8062
|
+
import { isAbsolute, join as join9 } from "path";
|
|
7533
8063
|
import fg from "fast-glob";
|
|
7534
8064
|
var MAX_FILES, EXTENSIONS, SEVERITY_ORDER, bug_scan;
|
|
7535
8065
|
var init_bug_scan = __esm({
|
|
@@ -7559,7 +8089,7 @@ var init_bug_scan = __esm({
|
|
|
7559
8089
|
const target = params.path;
|
|
7560
8090
|
const recursive = params.recursive ?? false;
|
|
7561
8091
|
const minSeverity = params.severity ?? "warning";
|
|
7562
|
-
const absPath = isAbsolute(target) ? target :
|
|
8092
|
+
const absPath = isAbsolute(target) ? target : join9(process.cwd(), target);
|
|
7563
8093
|
let isFile = false;
|
|
7564
8094
|
try {
|
|
7565
8095
|
isFile = statSync11(absPath).isFile();
|
|
@@ -8124,10 +8654,10 @@ var init_json_parse = __esm({
|
|
|
8124
8654
|
const query = params.query;
|
|
8125
8655
|
let jsonContent = "";
|
|
8126
8656
|
if (filePath) {
|
|
8127
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
8657
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
8128
8658
|
const { resolve: resolve48 } = await import("path");
|
|
8129
8659
|
const abs = resolve48(filePath);
|
|
8130
|
-
if (!
|
|
8660
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8131
8661
|
jsonContent = readFileSync38(abs, "utf-8");
|
|
8132
8662
|
} else if (content) {
|
|
8133
8663
|
jsonContent = content;
|
|
@@ -8181,10 +8711,10 @@ var init_csv_parse = __esm({
|
|
|
8181
8711
|
const delimiter = params.delimiter ?? ",";
|
|
8182
8712
|
const hasHeader = params.has_header ?? true;
|
|
8183
8713
|
if (filePath) {
|
|
8184
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
8714
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
8185
8715
|
const { resolve: resolve48 } = await import("path");
|
|
8186
8716
|
const abs = resolve48(filePath);
|
|
8187
|
-
if (!
|
|
8717
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8188
8718
|
csvContent = readFileSync38(abs, "utf-8");
|
|
8189
8719
|
} else if (content) {
|
|
8190
8720
|
csvContent = content;
|
|
@@ -8268,10 +8798,10 @@ var init_xml_parse = __esm({
|
|
|
8268
8798
|
const filePath = params.file_path;
|
|
8269
8799
|
const content = params.content;
|
|
8270
8800
|
if (filePath) {
|
|
8271
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
8801
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
8272
8802
|
const { resolve: resolve48 } = await import("path");
|
|
8273
8803
|
const abs = resolve48(filePath);
|
|
8274
|
-
if (!
|
|
8804
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8275
8805
|
xmlContent = readFileSync38(abs, "utf-8");
|
|
8276
8806
|
} else if (content) {
|
|
8277
8807
|
xmlContent = content;
|
|
@@ -8313,10 +8843,10 @@ var init_yaml_parse = __esm({
|
|
|
8313
8843
|
const filePath = params.file_path;
|
|
8314
8844
|
const content = params.content;
|
|
8315
8845
|
if (filePath) {
|
|
8316
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
8846
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
8317
8847
|
const { resolve: resolve48 } = await import("path");
|
|
8318
8848
|
const abs = resolve48(filePath);
|
|
8319
|
-
if (!
|
|
8849
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8320
8850
|
yamlContent = readFileSync38(abs, "utf-8");
|
|
8321
8851
|
} else if (content) {
|
|
8322
8852
|
yamlContent = content;
|
|
@@ -8359,10 +8889,10 @@ var init_toml_parse = __esm({
|
|
|
8359
8889
|
const filePath = params.file_path;
|
|
8360
8890
|
let tomlContent = "";
|
|
8361
8891
|
if (filePath) {
|
|
8362
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
8892
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
8363
8893
|
const { resolve: resolve48 } = await import("path");
|
|
8364
8894
|
const abs = resolve48(filePath);
|
|
8365
|
-
if (!
|
|
8895
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8366
8896
|
tomlContent = readFileSync38(abs, "utf-8");
|
|
8367
8897
|
} else if (content) {
|
|
8368
8898
|
tomlContent = content;
|
|
@@ -8411,10 +8941,10 @@ var init_data_transform = __esm({
|
|
|
8411
8941
|
const output = params.output;
|
|
8412
8942
|
let inputStr = "";
|
|
8413
8943
|
if (filePath) {
|
|
8414
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
8944
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
8415
8945
|
const { resolve: resolve48 } = await import("path");
|
|
8416
8946
|
const abs = resolve48(filePath);
|
|
8417
|
-
if (!
|
|
8947
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8418
8948
|
inputStr = readFileSync38(abs, "utf-8");
|
|
8419
8949
|
} else if (data) {
|
|
8420
8950
|
inputStr = data;
|
|
@@ -8623,7 +9153,7 @@ var init_data_diff = __esm({
|
|
|
8623
9153
|
});
|
|
8624
9154
|
|
|
8625
9155
|
// src/tools/builtin/data/template_render.ts
|
|
8626
|
-
import { readFileSync as readFileSync26, existsSync as
|
|
9156
|
+
import { readFileSync as readFileSync26, existsSync as existsSync40, writeFileSync as writeFileSync8 } from "fs";
|
|
8627
9157
|
import { resolve as resolve35 } from "path";
|
|
8628
9158
|
function escapeRegex5(s) {
|
|
8629
9159
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -8657,7 +9187,7 @@ var init_template_render = __esm({
|
|
|
8657
9187
|
let template = "";
|
|
8658
9188
|
if (filePath) {
|
|
8659
9189
|
const abs = resolve35(filePath);
|
|
8660
|
-
if (!
|
|
9190
|
+
if (!existsSync40(abs)) return { success: false, error: `\u6A21\u677F\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8661
9191
|
template = readFileSync26(abs, "utf-8");
|
|
8662
9192
|
} else if (templateStr) {
|
|
8663
9193
|
template = templateStr;
|
|
@@ -8833,8 +9363,8 @@ ${results.join("\n")}
|
|
|
8833
9363
|
});
|
|
8834
9364
|
|
|
8835
9365
|
// src/tools/builtin/security/encrypt_file.ts
|
|
8836
|
-
import { readFileSync as readFileSync28, writeFileSync as writeFileSync9, existsSync as
|
|
8837
|
-
import { resolve as resolve37, dirname as
|
|
9366
|
+
import { readFileSync as readFileSync28, writeFileSync as writeFileSync9, existsSync as existsSync42, mkdirSync as mkdirSync11 } from "fs";
|
|
9367
|
+
import { resolve as resolve37, dirname as dirname8 } from "path";
|
|
8838
9368
|
var encrypt_file;
|
|
8839
9369
|
var init_encrypt_file = __esm({
|
|
8840
9370
|
"src/tools/builtin/security/encrypt_file.ts"() {
|
|
@@ -8861,15 +9391,15 @@ var init_encrypt_file = __esm({
|
|
|
8861
9391
|
const output = resolve37(params.output);
|
|
8862
9392
|
const password = params.password;
|
|
8863
9393
|
const algorithm = params.algorithm ?? "aes-256-cbc";
|
|
8864
|
-
if (!
|
|
9394
|
+
if (!existsSync42(filePath)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
8865
9395
|
const crypto = await import("crypto");
|
|
8866
9396
|
const content = readFileSync28(filePath);
|
|
8867
9397
|
const key = crypto.scryptSync(password || "default-secret-key", "salt", algorithm === "aes-256-cbc" ? 32 : 16);
|
|
8868
9398
|
const iv = crypto.randomBytes(16);
|
|
8869
9399
|
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|
8870
9400
|
const encrypted = Buffer.concat([iv, cipher.update(content), cipher.final()]);
|
|
8871
|
-
const outDir =
|
|
8872
|
-
if (!
|
|
9401
|
+
const outDir = dirname8(output);
|
|
9402
|
+
if (!existsSync42(outDir)) mkdirSync11(outDir, { recursive: true });
|
|
8873
9403
|
writeFileSync9(output, encrypted);
|
|
8874
9404
|
return {
|
|
8875
9405
|
success: true,
|
|
@@ -8885,8 +9415,8 @@ var init_encrypt_file = __esm({
|
|
|
8885
9415
|
});
|
|
8886
9416
|
|
|
8887
9417
|
// src/tools/builtin/security/decrypt_file.ts
|
|
8888
|
-
import { readFileSync as readFileSync29, writeFileSync as writeFileSync10, existsSync as
|
|
8889
|
-
import { resolve as resolve38, dirname as
|
|
9418
|
+
import { readFileSync as readFileSync29, writeFileSync as writeFileSync10, existsSync as existsSync43, mkdirSync as mkdirSync12 } from "fs";
|
|
9419
|
+
import { resolve as resolve38, dirname as dirname9 } from "path";
|
|
8890
9420
|
var decrypt_file;
|
|
8891
9421
|
var init_decrypt_file = __esm({
|
|
8892
9422
|
"src/tools/builtin/security/decrypt_file.ts"() {
|
|
@@ -8913,7 +9443,7 @@ var init_decrypt_file = __esm({
|
|
|
8913
9443
|
const output = resolve38(params.output);
|
|
8914
9444
|
const password = params.password;
|
|
8915
9445
|
const algorithm = params.algorithm ?? "aes-256-cbc";
|
|
8916
|
-
if (!
|
|
9446
|
+
if (!existsSync43(filePath)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${filePath}`, output: "" };
|
|
8917
9447
|
const crypto = await import("crypto");
|
|
8918
9448
|
const encrypted = readFileSync29(filePath);
|
|
8919
9449
|
const iv = encrypted.subarray(0, 16);
|
|
@@ -8921,8 +9451,8 @@ var init_decrypt_file = __esm({
|
|
|
8921
9451
|
const key = crypto.scryptSync(password || "default-secret-key", "salt", algorithm === "aes-256-cbc" ? 32 : 16);
|
|
8922
9452
|
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
8923
9453
|
const decrypted = Buffer.concat([decipher.update(data), decipher.final()]);
|
|
8924
|
-
const outDir =
|
|
8925
|
-
if (!
|
|
9454
|
+
const outDir = dirname9(output);
|
|
9455
|
+
if (!existsSync43(outDir)) mkdirSync12(outDir, { recursive: true });
|
|
8926
9456
|
writeFileSync10(output, decrypted);
|
|
8927
9457
|
return {
|
|
8928
9458
|
success: true,
|
|
@@ -8939,7 +9469,7 @@ var init_decrypt_file = __esm({
|
|
|
8939
9469
|
|
|
8940
9470
|
// src/tools/builtin/security/hash_generate.ts
|
|
8941
9471
|
import { createHash } from "crypto";
|
|
8942
|
-
import { readFileSync as readFileSync30, existsSync as
|
|
9472
|
+
import { readFileSync as readFileSync30, existsSync as existsSync44 } from "fs";
|
|
8943
9473
|
import { resolve as resolve39 } from "path";
|
|
8944
9474
|
var hash_generate;
|
|
8945
9475
|
var init_hash_generate = __esm({
|
|
@@ -8968,7 +9498,7 @@ var init_hash_generate = __esm({
|
|
|
8968
9498
|
let hash;
|
|
8969
9499
|
if (filePath) {
|
|
8970
9500
|
const abs = resolve39(filePath);
|
|
8971
|
-
if (!
|
|
9501
|
+
if (!existsSync44(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
8972
9502
|
const fileContent = readFileSync30(abs);
|
|
8973
9503
|
hash = createHash(algorithm).update(fileContent).digest("hex");
|
|
8974
9504
|
} else if (content) {
|
|
@@ -9254,7 +9784,7 @@ ${output.slice(0, 5e3)}`,
|
|
|
9254
9784
|
|
|
9255
9785
|
// src/tools/builtin/project/build_project.ts
|
|
9256
9786
|
import { execSync as execSync14 } from "child_process";
|
|
9257
|
-
import { readFileSync as readFileSync31, existsSync as
|
|
9787
|
+
import { readFileSync as readFileSync31, existsSync as existsSync45 } from "fs";
|
|
9258
9788
|
import { resolve as resolve40 } from "path";
|
|
9259
9789
|
var build_project;
|
|
9260
9790
|
var init_build_project = __esm({
|
|
@@ -9282,7 +9812,7 @@ var init_build_project = __esm({
|
|
|
9282
9812
|
const tool = params.tool ?? "auto";
|
|
9283
9813
|
let cmd = "";
|
|
9284
9814
|
const pkgPath = resolve40(cwd, "package.json");
|
|
9285
|
-
if (tool === "auto" &&
|
|
9815
|
+
if (tool === "auto" && existsSync45(pkgPath)) {
|
|
9286
9816
|
const pkg = JSON.parse(readFileSync31(pkgPath, "utf-8"));
|
|
9287
9817
|
const scripts = pkg.scripts || {};
|
|
9288
9818
|
if (scripts.build) cmd = `npm run build`;
|
|
@@ -9332,7 +9862,7 @@ ${output.slice(0, 1e4)}`,
|
|
|
9332
9862
|
|
|
9333
9863
|
// src/tools/builtin/project/run_test.ts
|
|
9334
9864
|
import { execSync as execSync15 } from "child_process";
|
|
9335
|
-
import { readFileSync as readFileSync32, existsSync as
|
|
9865
|
+
import { readFileSync as readFileSync32, existsSync as existsSync46 } from "fs";
|
|
9336
9866
|
import { resolve as resolve41 } from "path";
|
|
9337
9867
|
var run_test;
|
|
9338
9868
|
var init_run_test = __esm({
|
|
@@ -9363,7 +9893,7 @@ var init_run_test = __esm({
|
|
|
9363
9893
|
let cmd = "";
|
|
9364
9894
|
if (runner === "auto") {
|
|
9365
9895
|
const pkgPath = resolve41(cwd, "package.json");
|
|
9366
|
-
if (
|
|
9896
|
+
if (existsSync46(pkgPath)) {
|
|
9367
9897
|
const pkg = JSON.parse(readFileSync32(pkgPath, "utf-8"));
|
|
9368
9898
|
const scripts = pkg.scripts || {};
|
|
9369
9899
|
if (scripts.test) cmd = `npm run test`;
|
|
@@ -9482,7 +10012,7 @@ ${output.slice(0, 1e4)}`,
|
|
|
9482
10012
|
});
|
|
9483
10013
|
|
|
9484
10014
|
// src/tools/builtin/project/env_manage.ts
|
|
9485
|
-
import { readFileSync as readFileSync33, writeFileSync as writeFileSync11, existsSync as
|
|
10015
|
+
import { readFileSync as readFileSync33, writeFileSync as writeFileSync11, existsSync as existsSync47 } from "fs";
|
|
9486
10016
|
import { resolve as resolve42 } from "path";
|
|
9487
10017
|
var env_manage;
|
|
9488
10018
|
var init_env_manage = __esm({
|
|
@@ -9514,14 +10044,14 @@ var init_env_manage = __esm({
|
|
|
9514
10044
|
if (action === "list_envs") {
|
|
9515
10045
|
const cwd = process.cwd();
|
|
9516
10046
|
const envs = [".env", ".env.local", ".env.development", ".env.production", ".env.test"];
|
|
9517
|
-
const found = envs.filter((f) =>
|
|
10047
|
+
const found = envs.filter((f) => existsSync47(resolve42(cwd, f)));
|
|
9518
10048
|
return {
|
|
9519
10049
|
success: true,
|
|
9520
10050
|
output: found.join("\n") || "\u672A\u627E\u5230 .env \u6587\u4EF6",
|
|
9521
10051
|
metadata: { files: found }
|
|
9522
10052
|
};
|
|
9523
10053
|
}
|
|
9524
|
-
if (!
|
|
10054
|
+
if (!existsSync47(abs)) {
|
|
9525
10055
|
if (action === "set" && key && value !== void 0) {
|
|
9526
10056
|
writeFileSync11(abs, `${key}=${value}`, "utf-8");
|
|
9527
10057
|
return { success: true, output: `\u5DF2\u521B\u5EFA ${filePath} \u5E76\u8BBE\u7F6E ${key}=${value}` };
|
|
@@ -9568,7 +10098,7 @@ var init_env_manage = __esm({
|
|
|
9568
10098
|
});
|
|
9569
10099
|
|
|
9570
10100
|
// src/tools/builtin/project/config_manage.ts
|
|
9571
|
-
import { readFileSync as readFileSync34, writeFileSync as writeFileSync12, existsSync as
|
|
10101
|
+
import { readFileSync as readFileSync34, writeFileSync as writeFileSync12, existsSync as existsSync48 } from "fs";
|
|
9572
10102
|
import { resolve as resolve43 } from "path";
|
|
9573
10103
|
var config_manage;
|
|
9574
10104
|
var init_config_manage = __esm({
|
|
@@ -9608,7 +10138,7 @@ var init_config_manage = __esm({
|
|
|
9608
10138
|
".prettierrc"
|
|
9609
10139
|
];
|
|
9610
10140
|
const cwd = process.cwd();
|
|
9611
|
-
const found = configs.filter((f) =>
|
|
10141
|
+
const found = configs.filter((f) => existsSync48(resolve43(cwd, f)));
|
|
9612
10142
|
return {
|
|
9613
10143
|
success: true,
|
|
9614
10144
|
output: `\u9879\u76EE\u914D\u7F6E\u6587\u4EF6:
|
|
@@ -9627,7 +10157,7 @@ NODE_ENV=development
|
|
|
9627
10157
|
return { success: false, error: "\u7F3A\u5C11 file_path \u53C2\u6570", output: "" };
|
|
9628
10158
|
}
|
|
9629
10159
|
const abs = resolve43(filePath);
|
|
9630
|
-
if (!
|
|
10160
|
+
if (!existsSync48(abs)) {
|
|
9631
10161
|
return { success: false, error: `\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
9632
10162
|
}
|
|
9633
10163
|
if (action === "read") {
|
|
@@ -9680,7 +10210,7 @@ NODE_ENV=development
|
|
|
9680
10210
|
|
|
9681
10211
|
// src/tools/builtin/project/docker_manage.ts
|
|
9682
10212
|
import { execSync as execSync17 } from "child_process";
|
|
9683
|
-
import { existsSync as
|
|
10213
|
+
import { existsSync as existsSync49 } from "fs";
|
|
9684
10214
|
import { resolve as resolve44 } from "path";
|
|
9685
10215
|
var docker_manage;
|
|
9686
10216
|
var init_docker_manage = __esm({
|
|
@@ -9716,7 +10246,7 @@ var init_docker_manage = __esm({
|
|
|
9716
10246
|
switch (action) {
|
|
9717
10247
|
case "build": {
|
|
9718
10248
|
const dockerfile = resolve44(cwd, "Dockerfile");
|
|
9719
|
-
if (!
|
|
10249
|
+
if (!existsSync49(dockerfile)) {
|
|
9720
10250
|
return { success: false, error: `Dockerfile \u4E0D\u5B58\u5728: ${dockerfile}`, output: "" };
|
|
9721
10251
|
}
|
|
9722
10252
|
cmd = `docker build ${options} -t ${image || "app"} .`;
|
|
@@ -9811,10 +10341,10 @@ var init_context_summarize = __esm({
|
|
|
9811
10341
|
const maxLength = params.max_length ?? 500;
|
|
9812
10342
|
let content = "";
|
|
9813
10343
|
if (filePath) {
|
|
9814
|
-
const { readFileSync: readFileSync38, existsSync:
|
|
10344
|
+
const { readFileSync: readFileSync38, existsSync: existsSync54 } = await import("fs");
|
|
9815
10345
|
const { resolve: resolve48 } = await import("path");
|
|
9816
10346
|
const abs = resolve48(filePath);
|
|
9817
|
-
if (!
|
|
10347
|
+
if (!existsSync54(abs)) return { success: false, error: `\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
9818
10348
|
content = readFileSync38(abs, "utf-8");
|
|
9819
10349
|
} else if (text) {
|
|
9820
10350
|
content = text;
|
|
@@ -9917,9 +10447,9 @@ ${list.join("\n")}`,
|
|
|
9917
10447
|
});
|
|
9918
10448
|
|
|
9919
10449
|
// src/tools/builtin/ai/skill_create.ts
|
|
9920
|
-
import { mkdirSync as
|
|
10450
|
+
import { mkdirSync as mkdirSync13, existsSync as existsSync50 } from "fs";
|
|
9921
10451
|
import { writeFile as writeFile4 } from "fs/promises";
|
|
9922
|
-
import { join as
|
|
10452
|
+
import { join as join10 } from "path";
|
|
9923
10453
|
var skill_create;
|
|
9924
10454
|
var init_skill_create = __esm({
|
|
9925
10455
|
"src/tools/builtin/ai/skill_create.ts"() {
|
|
@@ -9947,8 +10477,8 @@ var init_skill_create = __esm({
|
|
|
9947
10477
|
const description = params.description || `${name} \u6280\u80FD`;
|
|
9948
10478
|
const prompt = params.prompt;
|
|
9949
10479
|
const triggers = params.triggers || [];
|
|
9950
|
-
const skillDir =
|
|
9951
|
-
if (!
|
|
10480
|
+
const skillDir = join10(DEEPER_SKILLS_DIR, name);
|
|
10481
|
+
if (!existsSync50(skillDir)) mkdirSync13(skillDir, { recursive: true });
|
|
9952
10482
|
const yaml = [
|
|
9953
10483
|
`name: ${name}`,
|
|
9954
10484
|
`description: ${description}`,
|
|
@@ -9962,7 +10492,7 @@ ${yaml}
|
|
|
9962
10492
|
---
|
|
9963
10493
|
|
|
9964
10494
|
${prompt}`;
|
|
9965
|
-
await writeFile4(
|
|
10495
|
+
await writeFile4(join10(skillDir, "skill.md"), md, "utf-8");
|
|
9966
10496
|
return {
|
|
9967
10497
|
success: true,
|
|
9968
10498
|
output: `Skill "${name}" \u5DF2\u521B\u5EFA
|
|
@@ -10036,13 +10566,13 @@ var init_tool_create = __esm({
|
|
|
10036
10566
|
});
|
|
10037
10567
|
|
|
10038
10568
|
// src/tools/builtin/ai/memory_store.ts
|
|
10039
|
-
import { readFileSync as readFileSync35, writeFileSync as writeFileSync13, existsSync as
|
|
10040
|
-
import { join as
|
|
10569
|
+
import { readFileSync as readFileSync35, writeFileSync as writeFileSync13, existsSync as existsSync51, mkdirSync as mkdirSync14 } from "fs";
|
|
10570
|
+
import { join as join11 } from "path";
|
|
10041
10571
|
var MEMORY_DIR, memory_store;
|
|
10042
10572
|
var init_memory_store = __esm({
|
|
10043
10573
|
"src/tools/builtin/ai/memory_store.ts"() {
|
|
10044
10574
|
"use strict";
|
|
10045
|
-
MEMORY_DIR =
|
|
10575
|
+
MEMORY_DIR = join11(process.env.HOME || process.env.USERPROFILE || process.cwd(), ".deepercode", "memories");
|
|
10046
10576
|
memory_store = {
|
|
10047
10577
|
name: "memory_store",
|
|
10048
10578
|
description: "\u6301\u4E45\u5316\u8BB0\u5FC6\u5B58\u50A8",
|
|
@@ -10065,27 +10595,27 @@ var init_memory_store = __esm({
|
|
|
10065
10595
|
const key = params.key;
|
|
10066
10596
|
const value = params.value;
|
|
10067
10597
|
const query = params.query;
|
|
10068
|
-
if (!
|
|
10069
|
-
|
|
10598
|
+
if (!existsSync51(MEMORY_DIR)) {
|
|
10599
|
+
mkdirSync14(MEMORY_DIR, { recursive: true });
|
|
10070
10600
|
}
|
|
10071
10601
|
switch (action) {
|
|
10072
10602
|
case "set": {
|
|
10073
10603
|
if (!key || value === void 0) return { success: false, error: "set \u9700\u8981 key \u548C value \u53C2\u6570", output: "" };
|
|
10074
|
-
const memPath =
|
|
10604
|
+
const memPath = join11(MEMORY_DIR, `${key}.json`);
|
|
10075
10605
|
writeFileSync13(memPath, JSON.stringify({ key, value, timestamp: Date.now() }), "utf-8");
|
|
10076
10606
|
return { success: true, output: `\u8BB0\u5FC6\u5DF2\u4FDD\u5B58: ${key}` };
|
|
10077
10607
|
}
|
|
10078
10608
|
case "get": {
|
|
10079
10609
|
if (!key) return { success: false, error: "get \u9700\u8981 key \u53C2\u6570", output: "" };
|
|
10080
|
-
const memPath =
|
|
10081
|
-
if (!
|
|
10610
|
+
const memPath = join11(MEMORY_DIR, `${key}.json`);
|
|
10611
|
+
if (!existsSync51(memPath)) return { success: false, error: `\u8BB0\u5FC6\u4E0D\u5B58\u5728: ${key}`, output: "" };
|
|
10082
10612
|
const data = JSON.parse(readFileSync35(memPath, "utf-8"));
|
|
10083
10613
|
return { success: true, output: data.value };
|
|
10084
10614
|
}
|
|
10085
10615
|
case "delete": {
|
|
10086
10616
|
if (!key) return { success: false, error: "delete \u9700\u8981 key \u53C2\u6570", output: "" };
|
|
10087
|
-
const memPath =
|
|
10088
|
-
if (!
|
|
10617
|
+
const memPath = join11(MEMORY_DIR, `${key}.json`);
|
|
10618
|
+
if (!existsSync51(memPath)) return { success: false, error: `\u8BB0\u5FC6\u4E0D\u5B58\u5728: ${key}`, output: "" };
|
|
10089
10619
|
const { unlinkSync: unlinkSync2 } = await import("fs");
|
|
10090
10620
|
unlinkSync2(memPath);
|
|
10091
10621
|
return { success: true, output: `\u8BB0\u5FC6\u5DF2\u5220\u9664: ${key}` };
|
|
@@ -10106,7 +10636,7 @@ var init_memory_store = __esm({
|
|
|
10106
10636
|
const files = readdirSync5(MEMORY_DIR).filter((f) => f.endsWith(".json"));
|
|
10107
10637
|
const matches = [];
|
|
10108
10638
|
for (const f of files) {
|
|
10109
|
-
const data = JSON.parse(readFileSync35(
|
|
10639
|
+
const data = JSON.parse(readFileSync35(join11(MEMORY_DIR, f), "utf-8"));
|
|
10110
10640
|
if (data.value.includes(query) || data.key.includes(query)) {
|
|
10111
10641
|
matches.push(`${data.key}: ${data.value.slice(0, 200)}`);
|
|
10112
10642
|
}
|
|
@@ -10403,7 +10933,7 @@ var init_notify_user = __esm({
|
|
|
10403
10933
|
});
|
|
10404
10934
|
|
|
10405
10935
|
// src/tools/builtin/system/log_viewer.ts
|
|
10406
|
-
import { readFileSync as readFileSync36, existsSync as
|
|
10936
|
+
import { readFileSync as readFileSync36, existsSync as existsSync52 } from "fs";
|
|
10407
10937
|
import { resolve as resolve47 } from "path";
|
|
10408
10938
|
var log_viewer;
|
|
10409
10939
|
var init_log_viewer = __esm({
|
|
@@ -10434,7 +10964,7 @@ var init_log_viewer = __esm({
|
|
|
10434
10964
|
if (action !== "clear" && !filePath) {
|
|
10435
10965
|
const defaultLogs = ["npm-debug.log", "yarn-error.log", ".log", "logs/"];
|
|
10436
10966
|
const cwd = process.cwd();
|
|
10437
|
-
const found = defaultLogs.filter((f) =>
|
|
10967
|
+
const found = defaultLogs.filter((f) => existsSync52(resolve47(cwd, f)));
|
|
10438
10968
|
return {
|
|
10439
10969
|
success: true,
|
|
10440
10970
|
output: `\u8BF7\u6307\u5B9A file_path\u3002\u9879\u76EE\u76EE\u5F55\u4E2D\u7684\u65E5\u5FD7\u6587\u4EF6:
|
|
@@ -10447,7 +10977,7 @@ ${found.length > 0 ? found.join("\n") : " (\u672A\u627E\u5230)"}`
|
|
|
10447
10977
|
writeFileSync15(abs, "", "utf-8");
|
|
10448
10978
|
return { success: true, output: `\u65E5\u5FD7\u5DF2\u6E05\u7A7A: ${abs}` };
|
|
10449
10979
|
}
|
|
10450
|
-
if (!
|
|
10980
|
+
if (!existsSync52(abs)) {
|
|
10451
10981
|
return { success: false, error: `\u65E5\u5FD7\u6587\u4EF6\u4E0D\u5B58\u5728: ${abs}`, output: "" };
|
|
10452
10982
|
}
|
|
10453
10983
|
const content = readFileSync36(abs, "utf-8");
|
|
@@ -10729,8 +11259,8 @@ __export(chat_repl_exports, {
|
|
|
10729
11259
|
});
|
|
10730
11260
|
import readline from "readline";
|
|
10731
11261
|
import process5 from "process";
|
|
10732
|
-
import { existsSync as
|
|
10733
|
-
import { join as
|
|
11262
|
+
import { existsSync as existsSync53, mkdirSync as mkdirSync15, readFileSync as readFileSync37, writeFileSync as writeFileSync14, readdirSync as readdirSync4, statSync as statSync12 } from "fs";
|
|
11263
|
+
import { join as join12 } from "path";
|
|
10734
11264
|
function getSkillSystemPrompt() {
|
|
10735
11265
|
if (!skillEngine) return "";
|
|
10736
11266
|
return skillEngine.getSystemPrompt();
|
|
@@ -10843,7 +11373,7 @@ async function startRepl(opts) {
|
|
|
10843
11373
|
}
|
|
10844
11374
|
});
|
|
10845
11375
|
const history = [];
|
|
10846
|
-
if (!
|
|
11376
|
+
if (!existsSync53(SESSION_DIR)) mkdirSync15(SESSION_DIR, { recursive: true });
|
|
10847
11377
|
const sid = `sess_${Date.now()}`;
|
|
10848
11378
|
setSessionId(sid);
|
|
10849
11379
|
await xmemory.load();
|
|
@@ -10903,13 +11433,14 @@ ${prevSummary.slice(0, 800)}` });
|
|
|
10903
11433
|
};
|
|
10904
11434
|
drawHeader();
|
|
10905
11435
|
let resizeTimer = null;
|
|
10906
|
-
|
|
11436
|
+
const onResize = () => {
|
|
10907
11437
|
if (resizeTimer) return;
|
|
10908
11438
|
resizeTimer = setTimeout(() => {
|
|
10909
11439
|
resizeTimer = null;
|
|
10910
11440
|
Oflush();
|
|
10911
11441
|
}, 200);
|
|
10912
|
-
}
|
|
11442
|
+
};
|
|
11443
|
+
process5.stdout.on("resize", onResize);
|
|
10913
11444
|
let resolveLine = null;
|
|
10914
11445
|
let running = true;
|
|
10915
11446
|
const rl = readline.createInterface({ input: process5.stdin, output: process5.stdout, terminal: true });
|
|
@@ -10944,7 +11475,7 @@ ${prevSummary.slice(0, 800)}` });
|
|
|
10944
11475
|
else showPrompt();
|
|
10945
11476
|
return ok;
|
|
10946
11477
|
};
|
|
10947
|
-
|
|
11478
|
+
const onSigint = () => {
|
|
10948
11479
|
if (resolveLine) {
|
|
10949
11480
|
const cb = resolveLine;
|
|
10950
11481
|
resolveLine = null;
|
|
@@ -10952,22 +11483,28 @@ ${prevSummary.slice(0, 800)}` });
|
|
|
10952
11483
|
return;
|
|
10953
11484
|
}
|
|
10954
11485
|
Oflush();
|
|
11486
|
+
process5.stdout.removeListener("resize", onResize);
|
|
11487
|
+
process5.removeListener("uncaughtException", onUCE);
|
|
11488
|
+
process5.removeListener("unhandledRejection", onUHR);
|
|
10955
11489
|
xmemory.save().then(() => {
|
|
10956
11490
|
O("\n" + y("\u518D\u89C1\uFF01") + "\n");
|
|
10957
11491
|
running = false;
|
|
10958
11492
|
rl.close();
|
|
10959
11493
|
process5.exit(0);
|
|
10960
11494
|
});
|
|
10961
|
-
}
|
|
10962
|
-
|
|
11495
|
+
};
|
|
11496
|
+
rl.on("SIGINT", onSigint);
|
|
11497
|
+
const onUCE = (err) => {
|
|
10963
11498
|
if (!err.message?.includes("readline")) O(r(`
|
|
10964
11499
|
\u26A0 ${err.message}`) + "\n");
|
|
10965
|
-
}
|
|
10966
|
-
process5.on("
|
|
11500
|
+
};
|
|
11501
|
+
process5.on("uncaughtException", onUCE);
|
|
11502
|
+
const onUHR = (reason) => {
|
|
10967
11503
|
const m = reason instanceof Error ? reason.message : String(reason);
|
|
10968
11504
|
if (!m.includes("readline") && !m.includes("Abort") && !m.includes("timeout")) O(r(`
|
|
10969
11505
|
\u26A0 ${m}`) + "\n");
|
|
10970
|
-
}
|
|
11506
|
+
};
|
|
11507
|
+
process5.on("unhandledRejection", onUHR);
|
|
10971
11508
|
while (running) {
|
|
10972
11509
|
currentPrompt = c("\u276F ");
|
|
10973
11510
|
const tasks = getTodos();
|
|
@@ -11089,11 +11626,11 @@ ${prevSummary.slice(0, 800)}` });
|
|
|
11089
11626
|
continue;
|
|
11090
11627
|
}
|
|
11091
11628
|
if (cmd === "/rules") {
|
|
11092
|
-
const projectRules =
|
|
11093
|
-
const globalRules =
|
|
11629
|
+
const projectRules = join12(process5.cwd(), ".deeper", "rules.md");
|
|
11630
|
+
const globalRules = join12(DEEPER_HOME, "rules.md");
|
|
11094
11631
|
O(b(c(" Rules")) + "\n");
|
|
11095
11632
|
const showRules = (label, path) => {
|
|
11096
|
-
if (
|
|
11633
|
+
if (existsSync53(path)) {
|
|
11097
11634
|
const content = readFileSync37(path, "utf-8");
|
|
11098
11635
|
O(G(` ${label}: `) + c(path) + G(` (${content.split("\n").length}\u884C)`) + "\n");
|
|
11099
11636
|
const lines = content.split("\n").slice(0, 8);
|
|
@@ -11169,6 +11706,9 @@ ${prevSummary.slice(0, 800)}` });
|
|
|
11169
11706
|
}
|
|
11170
11707
|
rl.close();
|
|
11171
11708
|
Oflush();
|
|
11709
|
+
process5.stdout.removeListener("resize", onResize);
|
|
11710
|
+
process5.removeListener("uncaughtException", onUCE);
|
|
11711
|
+
process5.removeListener("unhandledRejection", onUHR);
|
|
11172
11712
|
await xmemory.save();
|
|
11173
11713
|
if (mcpClient) mcpClient.disconnectAll();
|
|
11174
11714
|
Oflush();
|
|
@@ -11508,35 +12048,17 @@ Rules:
|
|
|
11508
12048
|
const skillPrompt = getSkillSystemPrompt();
|
|
11509
12049
|
if (skillPrompt) sysContent += "\n" + skillPrompt;
|
|
11510
12050
|
r2.push({ role: "system", content: sysContent });
|
|
11511
|
-
|
|
11512
|
-
|
|
11513
|
-
|
|
11514
|
-
|
|
11515
|
-
|
|
11516
|
-
|
|
11517
|
-
|
|
11518
|
-
|
|
11519
|
-
|
|
11520
|
-
|
|
11521
|
-
|
|
11522
|
-
try {
|
|
11523
|
-
const rulesFile = join11(process5.cwd(), ".deeper", "rules.md");
|
|
11524
|
-
if (existsSync52(rulesFile)) {
|
|
11525
|
-
const rules = readFileSync37(rulesFile, "utf-8").slice(0, 4e3);
|
|
11526
|
-
if (rules.trim()) r2.push({ role: "system", content: `[\u9879\u76EE\u89C4\u5219 rules.md]
|
|
11527
|
-
${rules}` });
|
|
11528
|
-
}
|
|
11529
|
-
} catch {
|
|
11530
|
-
}
|
|
11531
|
-
try {
|
|
11532
|
-
const globalRules = join11(DEEPER_HOME, "rules.md");
|
|
11533
|
-
if (existsSync52(globalRules)) {
|
|
11534
|
-
const rules = readFileSync37(globalRules, "utf-8").slice(0, 2e3);
|
|
11535
|
-
if (rules.trim()) r2.push({ role: "system", content: `[\u5168\u5C40\u89C4\u5219]
|
|
11536
|
-
${rules}` });
|
|
11537
|
-
}
|
|
11538
|
-
} catch {
|
|
11539
|
-
}
|
|
12051
|
+
const deeperCtx = cachedRead(join12(process5.cwd(), "deeper.md"));
|
|
12052
|
+
if (deeperCtx && deeperCtx.trim() && !deeperCtx.includes("<!-- \u586B\u5199")) {
|
|
12053
|
+
r2.push({ role: "system", content: `[\u9879\u76EE\u4E0A\u4E0B\u6587 deeper.md]
|
|
12054
|
+
${deeperCtx}` });
|
|
12055
|
+
}
|
|
12056
|
+
const projRules = cachedRead(join12(process5.cwd(), ".deeper", "rules.md"));
|
|
12057
|
+
if (projRules && projRules.trim()) r2.push({ role: "system", content: `[\u9879\u76EE\u89C4\u5219 rules.md]
|
|
12058
|
+
${projRules}` });
|
|
12059
|
+
const globalRules = cachedRead(join12(DEEPER_HOME, "rules.md"), 2e3);
|
|
12060
|
+
if (globalRules && globalRules.trim()) r2.push({ role: "system", content: `[\u5168\u5C40\u89C4\u5219]
|
|
12061
|
+
${globalRules}` });
|
|
11540
12062
|
for (const m of history.slice(-30)) {
|
|
11541
12063
|
const e = { role: m.role };
|
|
11542
12064
|
if (m.content != null) e.content = (m.content || "").slice(0, 8e3);
|
|
@@ -11554,6 +12076,19 @@ ${rules}` });
|
|
|
11554
12076
|
function trimHistory(h, max) {
|
|
11555
12077
|
while (h.length > max) h.shift();
|
|
11556
12078
|
}
|
|
12079
|
+
function cachedRead(path, maxLen = 4e3) {
|
|
12080
|
+
if (!existsSync53(path)) return null;
|
|
12081
|
+
try {
|
|
12082
|
+
const st = statSync12(path);
|
|
12083
|
+
const cached = _fileCache.get(path);
|
|
12084
|
+
if (cached && cached.mtime === st.mtimeMs) return cached.content;
|
|
12085
|
+
const content = readFileSync37(path, "utf-8").slice(0, maxLen);
|
|
12086
|
+
_fileCache.set(path, { mtime: st.mtimeMs, content });
|
|
12087
|
+
return content;
|
|
12088
|
+
} catch {
|
|
12089
|
+
return null;
|
|
12090
|
+
}
|
|
12091
|
+
}
|
|
11557
12092
|
function compressHistory(h) {
|
|
11558
12093
|
if (h.length <= 6) return;
|
|
11559
12094
|
const keep = 6;
|
|
@@ -11589,12 +12124,12 @@ function toolsToDefs(tools) {
|
|
|
11589
12124
|
}
|
|
11590
12125
|
async function saveSession(history, name) {
|
|
11591
12126
|
const label = name || (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
11592
|
-
const file =
|
|
12127
|
+
const file = join12(SESSION_DIR, `sess_${label}.json`);
|
|
11593
12128
|
writeFileSync14(file, JSON.stringify({ savedAt: (/* @__PURE__ */ new Date()).toISOString(), cwd: process5.cwd(), messages: history }, null, 2), "utf-8");
|
|
11594
12129
|
O(g(`\u5DF2\u4FDD\u5B58: sess_${label}`) + "\n\n");
|
|
11595
12130
|
}
|
|
11596
12131
|
async function listSessions() {
|
|
11597
|
-
if (!
|
|
12132
|
+
if (!existsSync53(SESSION_DIR)) {
|
|
11598
12133
|
O(r("\u65E0\u4FDD\u5B58\u7684\u4F1A\u8BDD\n\n"));
|
|
11599
12134
|
return;
|
|
11600
12135
|
}
|
|
@@ -11607,7 +12142,7 @@ async function listSessions() {
|
|
|
11607
12142
|
for (let i = 0; i < Math.min(files.length, 15); i++) {
|
|
11608
12143
|
const f = files[i];
|
|
11609
12144
|
try {
|
|
11610
|
-
const data = JSON.parse(readFileSync37(
|
|
12145
|
+
const data = JSON.parse(readFileSync37(join12(SESSION_DIR, f), "utf-8"));
|
|
11611
12146
|
const label = f.replace(/^sess_|\.json$/g, "");
|
|
11612
12147
|
const msgCount = data.messages?.length || 0;
|
|
11613
12148
|
const cwd = data.cwd || "?";
|
|
@@ -11621,8 +12156,8 @@ async function listSessions() {
|
|
|
11621
12156
|
O("\n \u52A0\u8F7D: /load <name> | \u6062\u590D: /resume\n\n");
|
|
11622
12157
|
}
|
|
11623
12158
|
async function loadNamedSession(history, name) {
|
|
11624
|
-
const file =
|
|
11625
|
-
if (!
|
|
12159
|
+
const file = join12(SESSION_DIR, `sess_${name}.json`);
|
|
12160
|
+
if (!existsSync53(file)) {
|
|
11626
12161
|
O(r(`\u4F1A\u8BDD\u4E0D\u5B58\u5728: ${name}
|
|
11627
12162
|
|
|
11628
12163
|
`));
|
|
@@ -11635,7 +12170,7 @@ async function loadNamedSession(history, name) {
|
|
|
11635
12170
|
O(g(`\u5DF2\u52A0\u8F7D: ${name}`) + "\n\n");
|
|
11636
12171
|
}
|
|
11637
12172
|
async function loadLatestSession(history) {
|
|
11638
|
-
if (!
|
|
12173
|
+
if (!existsSync53(SESSION_DIR)) {
|
|
11639
12174
|
O(r("\u65E0\u4FDD\u5B58\u7684\u4F1A\u8BDD\n\n"));
|
|
11640
12175
|
return;
|
|
11641
12176
|
}
|
|
@@ -11644,7 +12179,7 @@ async function loadLatestSession(history) {
|
|
|
11644
12179
|
O(r("\u65E0\u4FDD\u5B58\u7684\u4F1A\u8BDD\n\n"));
|
|
11645
12180
|
return;
|
|
11646
12181
|
}
|
|
11647
|
-
const file =
|
|
12182
|
+
const file = join12(SESSION_DIR, files[0]);
|
|
11648
12183
|
const data = JSON.parse(readFileSync37(file, "utf-8"));
|
|
11649
12184
|
const label = files[0].replace(/^sess_|\.json$/g, "");
|
|
11650
12185
|
history.push({ role: "system", content: `[\u5DF2\u52A0\u8F7D: ${label} (${data.messages?.length || 0} \u6761)]` });
|
|
@@ -11653,13 +12188,13 @@ async function loadLatestSession(history) {
|
|
|
11653
12188
|
O(g(`\u5DF2\u52A0\u8F7D: ${label}`) + "\n\n");
|
|
11654
12189
|
}
|
|
11655
12190
|
async function exportHistory(history) {
|
|
11656
|
-
const file =
|
|
12191
|
+
const file = join12(process5.cwd(), `deeper-export-${Date.now()}.json`);
|
|
11657
12192
|
writeFileSync14(file, JSON.stringify(history, null, 2), "utf-8");
|
|
11658
12193
|
O(g(`\u5DF2\u5BFC\u51FA: ${file}`) + "\n\n");
|
|
11659
12194
|
}
|
|
11660
12195
|
async function initProject() {
|
|
11661
|
-
const file =
|
|
11662
|
-
if (
|
|
12196
|
+
const file = join12(process5.cwd(), "deeper.md");
|
|
12197
|
+
if (existsSync53(file)) {
|
|
11663
12198
|
O(y(" deeper.md \u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\n\n"));
|
|
11664
12199
|
return;
|
|
11665
12200
|
}
|
|
@@ -11786,98 +12321,68 @@ async function loadBuiltinTools() {
|
|
|
11786
12321
|
return builtinTools2;
|
|
11787
12322
|
}
|
|
11788
12323
|
async function callApi(opts, msgs, tools, retry = 0, cmt = 8192) {
|
|
11789
|
-
const
|
|
11790
|
-
|
|
11791
|
-
|
|
12324
|
+
const client = new DeepSeekClient({
|
|
12325
|
+
apiKey: opts.apiKey || "",
|
|
12326
|
+
model: opts.model || "deepseek-chat",
|
|
12327
|
+
baseUrl: opts.baseUrl || "https://api.deepseek.com",
|
|
12328
|
+
temperature: opts.temperature ?? 0,
|
|
12329
|
+
maxTokens: cmt,
|
|
12330
|
+
think: { enabled: true, budgetTokens: cmt }
|
|
12331
|
+
});
|
|
12332
|
+
const chatMsgs = msgs.map((m) => ({
|
|
12333
|
+
role: m.role || "user",
|
|
12334
|
+
content: m.content != null ? String(m.content) : null,
|
|
12335
|
+
tool_calls: m.tool_calls,
|
|
12336
|
+
tool_call_id: m.tool_call_id,
|
|
12337
|
+
name: m.name,
|
|
12338
|
+
reasoning_content: m.reasoning_content,
|
|
12339
|
+
thinking: m.thinking
|
|
12340
|
+
}));
|
|
11792
12341
|
try {
|
|
11793
|
-
const
|
|
11794
|
-
|
|
11795
|
-
signal: ac.signal,
|
|
11796
|
-
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${opts.apiKey}` },
|
|
11797
|
-
body: JSON.stringify({
|
|
11798
|
-
model: opts.model,
|
|
11799
|
-
messages: msgs,
|
|
11800
|
-
tools: tools.length > 0 ? tools : void 0,
|
|
11801
|
-
tool_choice: tools.length > 0 ? "auto" : void 0,
|
|
11802
|
-
stream: true,
|
|
11803
|
-
max_tokens: cmt,
|
|
11804
|
-
temperature: opts.temperature
|
|
11805
|
-
})
|
|
11806
|
-
});
|
|
11807
|
-
if (!resp.ok) {
|
|
11808
|
-
const et = await resp.text().catch(() => "");
|
|
11809
|
-
if (resp.status === 401) throw new Error('API Key \u65E0\u6548: deeper config set api_key "sk-xxx"');
|
|
11810
|
-
if ((resp.status === 429 || resp.status >= 500) && retry < MR) {
|
|
11811
|
-
const d2 = Math.min(1e3 * (retry + 1), 5e3);
|
|
11812
|
-
await new Promise((r2) => setTimeout(r2, d2));
|
|
11813
|
-
return callApi(opts, msgs, tools, retry + 1, cmt);
|
|
11814
|
-
}
|
|
11815
|
-
throw new Error(`HTTP ${resp.status}: ${et.slice(0, 200)}`);
|
|
11816
|
-
}
|
|
11817
|
-
if (!resp.body) throw new Error("\u7A7A\u54CD\u5E94");
|
|
11818
|
-
return sseIter(resp.body);
|
|
12342
|
+
const stream = await client.chatStream(chatMsgs, tools.map((t) => ({ name: t.function.name, description: t.function.description, category: "", parameters: t.function.parameters })));
|
|
12343
|
+
return adaptStream(stream);
|
|
11819
12344
|
} catch (e) {
|
|
11820
12345
|
const m = (e instanceof Error ? e.message : String(e)).toLowerCase();
|
|
11821
|
-
if ((m.includes("timeout") || m.includes("abort") || m.includes("econn")) && retry <
|
|
12346
|
+
if ((m.includes("timeout") || m.includes("abort") || m.includes("econn") || m.includes("429") || m.includes("500")) && retry < 2) {
|
|
11822
12347
|
const d2 = Math.min(1e3 * (retry + 1), 5e3);
|
|
11823
12348
|
await new Promise((r2) => setTimeout(r2, d2));
|
|
11824
12349
|
return callApi(opts, msgs, tools, retry + 1, cmt);
|
|
11825
12350
|
}
|
|
11826
12351
|
throw e;
|
|
11827
|
-
} finally {
|
|
11828
|
-
clearTimeout(t);
|
|
11829
12352
|
}
|
|
11830
12353
|
}
|
|
11831
|
-
async function*
|
|
11832
|
-
const
|
|
11833
|
-
|
|
11834
|
-
|
|
11835
|
-
|
|
11836
|
-
|
|
11837
|
-
|
|
11838
|
-
|
|
11839
|
-
|
|
11840
|
-
|
|
11841
|
-
|
|
11842
|
-
|
|
11843
|
-
|
|
11844
|
-
|
|
11845
|
-
|
|
11846
|
-
|
|
11847
|
-
|
|
11848
|
-
|
|
11849
|
-
yield { type: "
|
|
11850
|
-
return;
|
|
11851
|
-
}
|
|
11852
|
-
try {
|
|
11853
|
-
const p = JSON.parse(d2);
|
|
11854
|
-
const ch = p.choices?.[0];
|
|
11855
|
-
if (!ch) continue;
|
|
11856
|
-
const dl = ch.delta;
|
|
11857
|
-
if (dl?.content) yield { type: "text", content: dl.content };
|
|
11858
|
-
if (dl?.reasoning_content) yield { type: "thinking", content: dl.reasoning_content };
|
|
11859
|
-
if (dl?.tool_calls) for (const tc of dl.tool_calls) {
|
|
11860
|
-
if (tc.id) yield { type: "tool_call_start", tool_call: { id: tc.id, name: tc.function?.name || "" } };
|
|
11861
|
-
if (tc.function?.arguments) yield { type: "tool_call_args", content: tc.function.arguments };
|
|
11862
|
-
if (tc.id && tc.function?.arguments) yield { type: "tool_call_end" };
|
|
11863
|
-
}
|
|
11864
|
-
if (ch.finish_reason === "tool_calls") yield { type: "tool_call_end" };
|
|
11865
|
-
} catch {
|
|
12354
|
+
async function* adaptStream(stream) {
|
|
12355
|
+
const seen = /* @__PURE__ */ new Set();
|
|
12356
|
+
let hasPending = false;
|
|
12357
|
+
for await (const chunk of stream) {
|
|
12358
|
+
if (chunk.type === "text" || chunk.type === "thinking") {
|
|
12359
|
+
yield { type: chunk.type, content: chunk.content };
|
|
12360
|
+
} else if (chunk.type === "tool_call") {
|
|
12361
|
+
const tc = chunk.tool_call;
|
|
12362
|
+
if (tc) {
|
|
12363
|
+
const key = tc.id || tc.name;
|
|
12364
|
+
const sa = JSON.stringify(tc.arguments || {});
|
|
12365
|
+
if (!seen.has(key)) {
|
|
12366
|
+
if (hasPending) yield { type: "tool_call_end" };
|
|
12367
|
+
seen.add(key);
|
|
12368
|
+
hasPending = true;
|
|
12369
|
+
yield { type: "tool_call_start", tool_call: { id: tc.id, name: tc.name } };
|
|
12370
|
+
yield { type: "tool_call_args", content: sa };
|
|
12371
|
+
} else {
|
|
12372
|
+
yield { type: "tool_call_args", content: sa };
|
|
11866
12373
|
}
|
|
11867
12374
|
}
|
|
11868
|
-
}
|
|
11869
|
-
|
|
11870
|
-
|
|
11871
|
-
|
|
11872
|
-
|
|
11873
|
-
|
|
11874
|
-
reader.releaseLock();
|
|
11875
|
-
} catch {
|
|
12375
|
+
} else if (chunk.type === "done") {
|
|
12376
|
+
if (hasPending) yield { type: "tool_call_end" };
|
|
12377
|
+
yield { type: "done" };
|
|
12378
|
+
return;
|
|
12379
|
+
} else {
|
|
12380
|
+
yield chunk;
|
|
11876
12381
|
}
|
|
11877
12382
|
}
|
|
11878
12383
|
yield { type: "done" };
|
|
11879
12384
|
}
|
|
11880
|
-
var MAX_HISTORY, CONTEXT_LIMIT, CTX_WARN, TOOL_RESULT_MAX, SESSION_DIR, AUTOSAVE_FILE, GS, skillEngine, mcpClient, validator;
|
|
12385
|
+
var MAX_HISTORY, CONTEXT_LIMIT, CTX_WARN, TOOL_RESULT_MAX, SESSION_DIR, AUTOSAVE_FILE, GS, skillEngine, mcpClient, validator, _fileCache;
|
|
11881
12386
|
var init_chat_repl = __esm({
|
|
11882
12387
|
"src/cli/chat-repl.ts"() {
|
|
11883
12388
|
"use strict";
|
|
@@ -11890,17 +12395,19 @@ var init_chat_repl = __esm({
|
|
|
11890
12395
|
init_token_count();
|
|
11891
12396
|
init_SkillEngine();
|
|
11892
12397
|
init_MCPClient();
|
|
12398
|
+
init_DeepSeekClient();
|
|
11893
12399
|
init_ansi();
|
|
11894
12400
|
MAX_HISTORY = 60;
|
|
11895
12401
|
CONTEXT_LIMIT = 1048576;
|
|
11896
12402
|
CTX_WARN = 786432;
|
|
11897
12403
|
TOOL_RESULT_MAX = 4e3;
|
|
11898
|
-
SESSION_DIR =
|
|
11899
|
-
AUTOSAVE_FILE =
|
|
12404
|
+
SESSION_DIR = join12(DEEPER_HOME, "sessions");
|
|
12405
|
+
AUTOSAVE_FILE = join12(SESSION_DIR, "_autosave.json");
|
|
11900
12406
|
GS = { tc: 0, api: 0, ch: 0 };
|
|
11901
12407
|
skillEngine = null;
|
|
11902
12408
|
mcpClient = null;
|
|
11903
12409
|
validator = new ToolValidator();
|
|
12410
|
+
_fileCache = /* @__PURE__ */ new Map();
|
|
11904
12411
|
}
|
|
11905
12412
|
});
|
|
11906
12413
|
|