workplace-pua-cli 0.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/.env.example +4 -0
- package/.eslintrc.json +21 -0
- package/.prettierrc.json +9 -0
- package/CHANGELOG.md +107 -0
- package/README.md +240 -0
- package/bin/pua +2 -0
- package/dist/commands/chat.d.ts +15 -0
- package/dist/commands/chat.d.ts.map +1 -0
- package/dist/commands/chat.js +262 -0
- package/dist/commands/chat.js.map +1 -0
- package/dist/commands/config.d.ts +15 -0
- package/dist/commands/config.d.ts.map +1 -0
- package/dist/commands/config.js +247 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/prompt.d.ts +14 -0
- package/dist/commands/prompt.d.ts.map +1 -0
- package/dist/commands/prompt.js +126 -0
- package/dist/commands/prompt.js.map +1 -0
- package/dist/config/providers.d.ts +37 -0
- package/dist/config/providers.d.ts.map +1 -0
- package/dist/config/providers.js +96 -0
- package/dist/config/providers.js.map +1 -0
- package/dist/config/session-storage.d.ts +29 -0
- package/dist/config/session-storage.d.ts.map +1 -0
- package/dist/config/session-storage.js +67 -0
- package/dist/config/session-storage.js.map +1 -0
- package/dist/config/settings.d.ts +55 -0
- package/dist/config/settings.d.ts.map +1 -0
- package/dist/config/settings.js +163 -0
- package/dist/config/settings.js.map +1 -0
- package/dist/config/storage.d.ts +69 -0
- package/dist/config/storage.d.ts.map +1 -0
- package/dist/config/storage.js +126 -0
- package/dist/config/storage.js.map +1 -0
- package/dist/history/session.d.ts +52 -0
- package/dist/history/session.d.ts.map +1 -0
- package/dist/history/session.js +122 -0
- package/dist/history/session.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +157 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/base.d.ts +38 -0
- package/dist/llm/base.d.ts.map +1 -0
- package/dist/llm/base.js +22 -0
- package/dist/llm/base.js.map +1 -0
- package/dist/llm/factory.d.ts +12 -0
- package/dist/llm/factory.d.ts.map +1 -0
- package/dist/llm/factory.js +26 -0
- package/dist/llm/factory.js.map +1 -0
- package/dist/llm/openai.d.ts +10 -0
- package/dist/llm/openai.d.ts.map +1 -0
- package/dist/llm/openai.js +97 -0
- package/dist/llm/openai.js.map +1 -0
- package/dist/llm/zhipu.d.ts +10 -0
- package/dist/llm/zhipu.d.ts.map +1 -0
- package/dist/llm/zhipu.js +91 -0
- package/dist/llm/zhipu.js.map +1 -0
- package/dist/prompts/boss.d.ts +6 -0
- package/dist/prompts/boss.d.ts.map +1 -0
- package/dist/prompts/boss.js +41 -0
- package/dist/prompts/boss.js.map +1 -0
- package/dist/prompts/employee.d.ts +6 -0
- package/dist/prompts/employee.d.ts.map +1 -0
- package/dist/prompts/employee.js +41 -0
- package/dist/prompts/employee.js.map +1 -0
- package/dist/prompts/index.d.ts +4 -0
- package/dist/prompts/index.d.ts.map +1 -0
- package/dist/prompts/index.js +9 -0
- package/dist/prompts/index.js.map +1 -0
- package/dist/utils/formatter.d.ts +25 -0
- package/dist/utils/formatter.d.ts.map +1 -0
- package/dist/utils/formatter.js +83 -0
- package/dist/utils/formatter.js.map +1 -0
- package/dist/utils/logger.d.ts +10 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +31 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/stream.d.ts +36 -0
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/stream.js +74 -0
- package/dist/utils/stream.js.map +1 -0
- package/docs/OPTIMIZATION.md +772 -0
- package/docs/TECHNICAL_PRINCIPLES.md +663 -0
- package/package.json +52 -0
- package/sample/1.png +0 -0
- package/sample/2.png +0 -0
- package/screenshots/chat-dialogue.png +0 -0
- package/screenshots/chat-mode.png +0 -0
- package/src/__tests__/config/settings.test.ts +48 -0
- package/src/__tests__/prompts/boss.test.ts +35 -0
- package/src/commands/chat.ts +328 -0
- package/src/commands/config.ts +283 -0
- package/src/commands/prompt.ts +154 -0
- package/src/config/providers.ts +109 -0
- package/src/config/session-storage.ts +94 -0
- package/src/config/settings.ts +194 -0
- package/src/config/storage.ts +150 -0
- package/src/history/session.ts +141 -0
- package/src/index.ts +164 -0
- package/src/llm/base.ts +55 -0
- package/src/llm/factory.ts +24 -0
- package/src/llm/openai.ts +113 -0
- package/src/llm/zhipu.ts +101 -0
- package/src/prompts/boss.ts +43 -0
- package/src/prompts/employee.ts +43 -0
- package/src/prompts/index.ts +3 -0
- package/src/utils/formatter.ts +104 -0
- package/src/utils/logger.ts +31 -0
- package/src/utils/stream.ts +76 -0
- package/tsconfig.json +20 -0
- package/vitest.config.ts +18 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
|
|
3
|
+
export type OutputFormat = 'text' | 'markdown' | 'json';
|
|
4
|
+
|
|
5
|
+
export interface FormattedOutput {
|
|
6
|
+
format: OutputFormat;
|
|
7
|
+
content: string;
|
|
8
|
+
metadata?: {
|
|
9
|
+
role?: string;
|
|
10
|
+
severity?: string;
|
|
11
|
+
provider?: string;
|
|
12
|
+
model?: string;
|
|
13
|
+
tokens?: number;
|
|
14
|
+
timestamp?: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class OutputFormatter {
|
|
19
|
+
private outputFormat: OutputFormat;
|
|
20
|
+
|
|
21
|
+
constructor(format: OutputFormat = 'text') {
|
|
22
|
+
this.outputFormat = format;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
format(data: FormattedOutput): string {
|
|
26
|
+
switch (this.outputFormat) {
|
|
27
|
+
case 'json':
|
|
28
|
+
return this.formatJson(data);
|
|
29
|
+
case 'markdown':
|
|
30
|
+
return this.formatMarkdown(data);
|
|
31
|
+
case 'text':
|
|
32
|
+
default:
|
|
33
|
+
return this.formatText(data);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private formatJson(data: FormattedOutput): string {
|
|
38
|
+
return JSON.stringify({
|
|
39
|
+
output: data.content,
|
|
40
|
+
metadata: data.metadata || {},
|
|
41
|
+
timestamp: new Date().toISOString()
|
|
42
|
+
}, null, 2);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private formatMarkdown(data: FormattedOutput): string {
|
|
46
|
+
let result = '';
|
|
47
|
+
|
|
48
|
+
if (data.metadata?.role) {
|
|
49
|
+
result += `## ${data.metadata.role}\n\n`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
result += data.content;
|
|
53
|
+
result += '\n';
|
|
54
|
+
|
|
55
|
+
if (data.metadata) {
|
|
56
|
+
result += '\n---\n\n';
|
|
57
|
+
result += '### Metadata\n\n';
|
|
58
|
+
result += '| Key | Value |\n';
|
|
59
|
+
result += '|-----|-------|\n';
|
|
60
|
+
|
|
61
|
+
for (const [key, value] of Object.entries(data.metadata)) {
|
|
62
|
+
result += `| ${key} | ${value} |\n`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private formatText(data: FormattedOutput): string {
|
|
70
|
+
return data.content;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
print(data: FormattedOutput): void {
|
|
74
|
+
const formatted = this.format(data);
|
|
75
|
+
|
|
76
|
+
switch (this.outputFormat) {
|
|
77
|
+
case 'json':
|
|
78
|
+
console.log(formatted);
|
|
79
|
+
break;
|
|
80
|
+
case 'markdown':
|
|
81
|
+
console.log(formatted);
|
|
82
|
+
break;
|
|
83
|
+
case 'text':
|
|
84
|
+
default:
|
|
85
|
+
// 对于文本格式,处理颜色
|
|
86
|
+
if (data.metadata?.role === 'boss') {
|
|
87
|
+
console.log(chalk.red(formatted));
|
|
88
|
+
} else if (data.metadata?.role === 'employee') {
|
|
89
|
+
console.log(chalk.yellow(formatted));
|
|
90
|
+
} else {
|
|
91
|
+
console.log(formatted);
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
setFormat(format: OutputFormat): void {
|
|
98
|
+
this.outputFormat = format;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getFormat(): OutputFormat {
|
|
102
|
+
return this.outputFormat;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
|
|
3
|
+
export class Logger {
|
|
4
|
+
info(message: string): void {
|
|
5
|
+
console.log(chalk.blue('ℹ'), message);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
success(message: string): void {
|
|
9
|
+
console.log(chalk.green('✓'), message);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
warning(message: string): void {
|
|
13
|
+
console.log(chalk.yellow('⚠'), message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
error(message: string): void {
|
|
17
|
+
console.error(chalk.red('✗'), message);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
debug(message: string): void {
|
|
21
|
+
if (process.env.DEBUG) {
|
|
22
|
+
console.log(chalk.gray('…'), message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
system(message: string): void {
|
|
27
|
+
console.log(chalk.cyan('►'), message);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const logger = new Logger();
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { StreamChunk } from '../llm/base';
|
|
3
|
+
|
|
4
|
+
export class StreamPrinter {
|
|
5
|
+
private buffer: string = '';
|
|
6
|
+
private currentLine: string = '';
|
|
7
|
+
|
|
8
|
+
constructor(private readonly roleColor: (text: string) => string = chalk.green) {}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Print a streaming chunk to the terminal
|
|
12
|
+
*/
|
|
13
|
+
printChunk(chunk: StreamChunk): void {
|
|
14
|
+
if (chunk.content) {
|
|
15
|
+
// Accumulate content for smoother display
|
|
16
|
+
this.buffer += chunk.content;
|
|
17
|
+
|
|
18
|
+
// Print directly for real-time effect
|
|
19
|
+
process.stdout.write(chunk.content);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (chunk.done) {
|
|
23
|
+
// Add newline when done
|
|
24
|
+
process.stdout.write('\n');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Print a complete message (non-streaming)
|
|
30
|
+
*/
|
|
31
|
+
printMessage(message: string): void {
|
|
32
|
+
console.log(this.roleColor(message));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Print user input
|
|
37
|
+
*/
|
|
38
|
+
printUserInput(input: string): void {
|
|
39
|
+
console.log(chalk.gray('┌─────────────────────────────────────'));
|
|
40
|
+
console.log(chalk.gray('│ 你:'), input);
|
|
41
|
+
console.log(chalk.gray('└─────────────────────────────────────'));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Print assistant response header
|
|
46
|
+
*/
|
|
47
|
+
printResponseHeader(role: string): void {
|
|
48
|
+
const roleLabel = role === 'boss' ? '老板' : '员工';
|
|
49
|
+
const color = role === 'boss' ? chalk.red.bold : chalk.yellow.bold;
|
|
50
|
+
console.log();
|
|
51
|
+
console.log(color(`┌─ ${roleLabel} ─────────────────────────────`));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Print assistant response footer
|
|
56
|
+
*/
|
|
57
|
+
printResponseFooter(): void {
|
|
58
|
+
console.log(chalk.gray('└─────────────────────────────────────'));
|
|
59
|
+
console.log();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Print error message
|
|
64
|
+
*/
|
|
65
|
+
printError(message: string): void {
|
|
66
|
+
console.error(chalk.red('✗ 错误:'), message);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Clear current line
|
|
71
|
+
*/
|
|
72
|
+
clearLine(): void {
|
|
73
|
+
process.stdout.clearLine(0);
|
|
74
|
+
process.stdout.cursorTo(0);
|
|
75
|
+
}
|
|
76
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"strict": false,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"declaration": false,
|
|
15
|
+
"sourceMap": false,
|
|
16
|
+
"types": ["node"]
|
|
17
|
+
},
|
|
18
|
+
"include": ["src/**/*"],
|
|
19
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
20
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: {
|
|
6
|
+
describe: 'readonly',
|
|
7
|
+
it: 'readonly',
|
|
8
|
+
expect: 'readonly'
|
|
9
|
+
},
|
|
10
|
+
environment: 'node',
|
|
11
|
+
include: ['src/**/*.test.ts', 'tests/**/*.test.ts'],
|
|
12
|
+
coverage: {
|
|
13
|
+
provider: 'v8',
|
|
14
|
+
reporter: ['text', 'json', 'html'],
|
|
15
|
+
exclude: ['**/*.test.ts', '**/dist/**']
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
});
|