@ai-sdk/tui 0.0.0 → 1.0.0-canary.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/LICENSE +201 -0
- package/README.md +79 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +1924 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -1
- package/src/agent-tui-runner.ts +666 -0
- package/src/index.ts +7 -0
- package/src/run-agent-tui.ts +85 -0
- package/src/test/mock-terminal.ts +207 -0
- package/src/tui/layout.ts +260 -0
- package/src/tui/markdown.ts +240 -0
- package/src/tui/terminal-frame-buffer.ts +111 -0
- package/src/tui/terminal-renderer.ts +1411 -0
- package/src/tui/terminal-text.ts +158 -0
- package/src/util/deferred.ts +10 -0
- package/src/util/print-stream.ts +30 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const ansiEscape = String.fromCharCode(27);
|
|
2
|
+
|
|
3
|
+
export const ansiPattern = new RegExp(`${ansiEscape}\\[[0-?]*[ -/]*[@-~]`, 'g');
|
|
4
|
+
export const ansiPrefixPattern = new RegExp(
|
|
5
|
+
`^${ansiEscape}\\[[0-?]*[ -/]*[@-~]`,
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
export function stripAnsi(input: string): string {
|
|
9
|
+
return input.replace(ansiPattern, '');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function visibleLength(input: string): number {
|
|
13
|
+
let width = 0;
|
|
14
|
+
let index = 0;
|
|
15
|
+
|
|
16
|
+
while (index < input.length) {
|
|
17
|
+
const ansiMatch = input.slice(index).match(ansiPrefixPattern);
|
|
18
|
+
|
|
19
|
+
if (ansiMatch) {
|
|
20
|
+
index += ansiMatch[0].length;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const codePoint = input.codePointAt(index);
|
|
25
|
+
|
|
26
|
+
if (codePoint == null) {
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const character = String.fromCodePoint(codePoint);
|
|
31
|
+
width += codePointWidth(codePoint);
|
|
32
|
+
index += character.length;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return width;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function sliceVisible(input: string, width: number): string {
|
|
39
|
+
if (width <= 0) {
|
|
40
|
+
return '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let output = '';
|
|
44
|
+
let visible = 0;
|
|
45
|
+
let index = 0;
|
|
46
|
+
|
|
47
|
+
while (index < input.length && visible < width) {
|
|
48
|
+
const ansiMatch = input.slice(index).match(ansiPrefixPattern);
|
|
49
|
+
|
|
50
|
+
if (ansiMatch) {
|
|
51
|
+
output += ansiMatch[0];
|
|
52
|
+
index += ansiMatch[0].length;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const codePoint = input.codePointAt(index);
|
|
57
|
+
|
|
58
|
+
if (codePoint == null) {
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const character = String.fromCodePoint(codePoint);
|
|
63
|
+
const characterWidth = codePointWidth(codePoint);
|
|
64
|
+
|
|
65
|
+
if (characterWidth > 0 && visible + characterWidth > width) {
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
output += character;
|
|
70
|
+
index += character.length;
|
|
71
|
+
visible += characterWidth;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
while (index < input.length) {
|
|
75
|
+
const ansiMatch = input.slice(index).match(ansiPrefixPattern);
|
|
76
|
+
|
|
77
|
+
if (!ansiMatch) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
output += ansiMatch[0];
|
|
82
|
+
index += ansiMatch[0].length;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return output;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function codePointWidth(codePoint: number): number {
|
|
89
|
+
if (codePoint === 0x09) {
|
|
90
|
+
return 4;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (codePoint < 0x20 || (codePoint >= 0x7f && codePoint < 0xa0)) {
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (isZeroWidthCodePoint(codePoint)) {
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return isWideCodePoint(codePoint) ? 2 : 1;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isZeroWidthCodePoint(codePoint: number): boolean {
|
|
105
|
+
return (
|
|
106
|
+
(codePoint >= 0x0300 && codePoint <= 0x036f) ||
|
|
107
|
+
(codePoint >= 0x0483 && codePoint <= 0x0489) ||
|
|
108
|
+
(codePoint >= 0x0591 && codePoint <= 0x05bd) ||
|
|
109
|
+
codePoint === 0x05bf ||
|
|
110
|
+
(codePoint >= 0x05c1 && codePoint <= 0x05c2) ||
|
|
111
|
+
(codePoint >= 0x05c4 && codePoint <= 0x05c5) ||
|
|
112
|
+
codePoint === 0x05c7 ||
|
|
113
|
+
(codePoint >= 0x0610 && codePoint <= 0x061a) ||
|
|
114
|
+
(codePoint >= 0x064b && codePoint <= 0x065f) ||
|
|
115
|
+
codePoint === 0x0670 ||
|
|
116
|
+
(codePoint >= 0x06d6 && codePoint <= 0x06dc) ||
|
|
117
|
+
(codePoint >= 0x06df && codePoint <= 0x06e4) ||
|
|
118
|
+
(codePoint >= 0x06e7 && codePoint <= 0x06e8) ||
|
|
119
|
+
(codePoint >= 0x06ea && codePoint <= 0x06ed) ||
|
|
120
|
+
codePoint === 0x0711 ||
|
|
121
|
+
(codePoint >= 0x0730 && codePoint <= 0x074a) ||
|
|
122
|
+
(codePoint >= 0x07a6 && codePoint <= 0x07b0) ||
|
|
123
|
+
(codePoint >= 0x07eb && codePoint <= 0x07f3) ||
|
|
124
|
+
(codePoint >= 0x0816 && codePoint <= 0x0819) ||
|
|
125
|
+
(codePoint >= 0x081b && codePoint <= 0x0823) ||
|
|
126
|
+
(codePoint >= 0x0825 && codePoint <= 0x0827) ||
|
|
127
|
+
(codePoint >= 0x0829 && codePoint <= 0x082d) ||
|
|
128
|
+
(codePoint >= 0x0859 && codePoint <= 0x085b) ||
|
|
129
|
+
(codePoint >= 0x08d3 && codePoint <= 0x0902) ||
|
|
130
|
+
codePoint === 0x093a ||
|
|
131
|
+
codePoint === 0x093c ||
|
|
132
|
+
(codePoint >= 0x0941 && codePoint <= 0x0948) ||
|
|
133
|
+
codePoint === 0x094d ||
|
|
134
|
+
(codePoint >= 0x0951 && codePoint <= 0x0957) ||
|
|
135
|
+
codePoint === 0x200d ||
|
|
136
|
+
(codePoint >= 0xfe00 && codePoint <= 0xfe0f) ||
|
|
137
|
+
(codePoint >= 0xe0100 && codePoint <= 0xe01ef)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function isWideCodePoint(codePoint: number): boolean {
|
|
142
|
+
return (
|
|
143
|
+
codePoint >= 0x1100 &&
|
|
144
|
+
(codePoint <= 0x115f ||
|
|
145
|
+
codePoint === 0x2329 ||
|
|
146
|
+
codePoint === 0x232a ||
|
|
147
|
+
(codePoint >= 0x2e80 && codePoint <= 0xa4cf && codePoint !== 0x303f) ||
|
|
148
|
+
(codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
|
|
149
|
+
(codePoint >= 0xf900 && codePoint <= 0xfaff) ||
|
|
150
|
+
(codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
|
|
151
|
+
(codePoint >= 0xfe30 && codePoint <= 0xfe6f) ||
|
|
152
|
+
(codePoint >= 0xff00 && codePoint <= 0xff60) ||
|
|
153
|
+
(codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
|
|
154
|
+
(codePoint >= 0x1f300 && codePoint <= 0x1f64f) ||
|
|
155
|
+
(codePoint >= 0x1f900 && codePoint <= 0x1f9ff) ||
|
|
156
|
+
(codePoint >= 0x20000 && codePoint <= 0x3fffd))
|
|
157
|
+
);
|
|
158
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function createDeferred<T>() {
|
|
2
|
+
let resolve!: (value: T) => void;
|
|
3
|
+
let reject!: (error: unknown) => void;
|
|
4
|
+
const promise = new Promise<T>((resolvePromise, rejectPromise) => {
|
|
5
|
+
resolve = resolvePromise;
|
|
6
|
+
reject = rejectPromise;
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
return { promise, resolve, reject };
|
|
10
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type StreamTextResult } from 'ai';
|
|
2
|
+
|
|
3
|
+
export async function printStream(result: StreamTextResult<any, any, any>) {
|
|
4
|
+
for await (const part of result.fullStream) {
|
|
5
|
+
switch (part.type) {
|
|
6
|
+
case 'reasoning-start':
|
|
7
|
+
process.stdout.write('\x1b[94m\n');
|
|
8
|
+
break;
|
|
9
|
+
case 'reasoning-delta':
|
|
10
|
+
process.stdout.write(part.text);
|
|
11
|
+
break;
|
|
12
|
+
case 'reasoning-end':
|
|
13
|
+
process.stdout.write('\x1b[0m\n\n');
|
|
14
|
+
break;
|
|
15
|
+
case 'text-delta':
|
|
16
|
+
process.stdout.write(part.text);
|
|
17
|
+
break;
|
|
18
|
+
case 'tool-call':
|
|
19
|
+
process.stdout.write(
|
|
20
|
+
`\x1b[92mtool-call: ${JSON.stringify(part)}\x1b[0m\n\n`,
|
|
21
|
+
);
|
|
22
|
+
break;
|
|
23
|
+
case 'tool-result':
|
|
24
|
+
process.stdout.write(
|
|
25
|
+
`\x1b[92mtool-result: ${JSON.stringify(part)}\x1b[0m\n\n`,
|
|
26
|
+
);
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|