@nickyzj2023/ai 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +22 -8
- package/package.json +3 -2
package/dist/cli.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { a as get_time_default, c as runAgent, i as get_weather_default, o as defineModel, r as loadMCPTools } from "./src-DEDOOez9.mjs";
|
|
2
|
-
import { extractErrorMessage } from "@nickyzj2023/utils";
|
|
2
|
+
import { compactStr, extractErrorMessage } from "@nickyzj2023/utils";
|
|
3
3
|
import readline from "node:readline";
|
|
4
4
|
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
6
|
import { dirname, join } from "node:path";
|
|
7
|
+
import { MarkdownRenderer } from "@wterm/markdown";
|
|
7
8
|
//#region src/utils/config.ts
|
|
8
9
|
/**
|
|
9
10
|
* 获取全局配置文件路径(Windows/macOS/Linux通用)
|
|
@@ -82,6 +83,8 @@ async function runSetup() {
|
|
|
82
83
|
var TUI = class {
|
|
83
84
|
rl = null;
|
|
84
85
|
onPrompt = null;
|
|
86
|
+
/** content专用的markdown渲染器 */
|
|
87
|
+
md = null;
|
|
85
88
|
/** 是否允许输入 */
|
|
86
89
|
isBusy = false;
|
|
87
90
|
/** 上次打印内容所属的状态(reasoning/content/tool)
|
|
@@ -123,12 +126,17 @@ var TUI = class {
|
|
|
123
126
|
});
|
|
124
127
|
}
|
|
125
128
|
/**
|
|
126
|
-
* 所有print
|
|
127
|
-
*
|
|
129
|
+
* 所有print方法调用前:
|
|
130
|
+
* 状态切换时打印换行,并记录当前状态
|
|
128
131
|
* @returns 是否发生了状态切换
|
|
129
132
|
*/
|
|
130
133
|
preparePrint(type) {
|
|
131
134
|
if (this.prevPrintType === type) return false;
|
|
135
|
+
if (type === "content_delta" && this.md === null) this.md = new MarkdownRenderer();
|
|
136
|
+
if (this.prevPrintType === "content_delta") {
|
|
137
|
+
const remaining = this.md?.flush();
|
|
138
|
+
if (remaining) process.stdout.write(remaining);
|
|
139
|
+
}
|
|
132
140
|
process.stdout.write("\n");
|
|
133
141
|
this.prevPrintType = type;
|
|
134
142
|
return true;
|
|
@@ -136,7 +144,7 @@ var TUI = class {
|
|
|
136
144
|
/**
|
|
137
145
|
* 为文本添加ANSI颜色;非TTY输出(重定向/管道)时返回原文本,避免日志出现转义码。
|
|
138
146
|
* @param text 原始文本
|
|
139
|
-
* @param ansiCode ANSI颜色代码,如
|
|
147
|
+
* @param ansiCode ANSI颜色代码,如"90"(亮黑,大多数终端显示为灰色)
|
|
140
148
|
*/
|
|
141
149
|
colorize(text, ansiCode) {
|
|
142
150
|
if (!process.stdout.isTTY) return text;
|
|
@@ -150,17 +158,23 @@ var TUI = class {
|
|
|
150
158
|
/** 流式打印AI回复内容 */
|
|
151
159
|
printContent(delta) {
|
|
152
160
|
this.preparePrint("content_delta");
|
|
153
|
-
|
|
161
|
+
const rendered = this.md?.push(delta);
|
|
162
|
+
if (rendered) process.stdout.write(rendered);
|
|
154
163
|
}
|
|
155
164
|
/** 打印工具调用 */
|
|
156
165
|
printToolCall(name, args) {
|
|
157
166
|
this.preparePrint("tool_call");
|
|
158
|
-
process.stdout.write(`[工具调用:${name}] ${args}`);
|
|
167
|
+
process.stdout.write(`[工具调用:${name}] ${args}\n`);
|
|
159
168
|
}
|
|
160
169
|
/** 打印工具结果 */
|
|
161
|
-
printToolResult(name, result) {
|
|
170
|
+
printToolResult(name, result, options) {
|
|
171
|
+
const { ellipsis = true } = options ?? {};
|
|
172
|
+
const _result = ellipsis ? compactStr(result, {
|
|
173
|
+
maxLength: 200,
|
|
174
|
+
truncateMiddle: true
|
|
175
|
+
}) : result;
|
|
162
176
|
this.preparePrint("tool_result");
|
|
163
|
-
process.stdout.write(this.colorize(`[工具结果:${name}] ${
|
|
177
|
+
process.stdout.write(this.colorize(`[工具结果:${name}] ${_result}\n`, "90"));
|
|
164
178
|
}
|
|
165
179
|
formatter = new Intl.NumberFormat("en-US", {
|
|
166
180
|
notation: "compact",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nickyzj2023/ai",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "我的“pi”,参考了pi-from-scratch",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
36
|
-
"@nickyzj2023/utils": "link:../utils"
|
|
36
|
+
"@nickyzj2023/utils": "link:../utils",
|
|
37
|
+
"@wterm/markdown": "^0.5.0"
|
|
37
38
|
},
|
|
38
39
|
"scripts": {
|
|
39
40
|
"build": "tsdown",
|