@chatbotkit/cli 1.21.3 → 1.21.5

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.
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatBlue = exports.RESET = exports.BLUE = void 0;
4
+ exports.BLUE = '\u001b[34m';
5
+ exports.RESET = '\u001b[0m';
6
+ const formatBlue = (text) => {
7
+ return `${exports.BLUE}${text}${exports.RESET}`;
8
+ };
9
+ exports.formatBlue = formatBlue;
@@ -0,0 +1,3 @@
1
+ export const BLUE: "\u001B[34m";
2
+ export const RESET: "\u001B[0m";
3
+ export function formatBlue(text: string): string;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.command = void 0;
4
+ const color_js_1 = require("../../color.cjs");
4
5
  const env_js_1 = require("../../env.cjs");
5
6
  const output_js_1 = require("../../output.cjs");
6
7
  const spinner_js_1 = require("../../spinner.cjs");
@@ -10,6 +11,66 @@ const sdk_1 = require("@chatbotkit/sdk");
10
11
  const commander_1 = require("commander");
11
12
  const fs_1 = require("fs");
12
13
  const path_1 = require("path");
14
+ class OutputManager {
15
+ constructor(spinner = null) {
16
+ this.lastOutputWasRaw = false;
17
+ this.lastOutputWasStructured = false;
18
+ this.hasRawOutput = false;
19
+ this.lastChar = '';
20
+ this.spinner = spinner;
21
+ }
22
+ ensureSpinnerStopped() {
23
+ if (this.spinner && this.spinner.isSpinning) {
24
+ this.spinner.stop(false);
25
+ }
26
+ }
27
+ writeRaw(text) {
28
+ this.ensureSpinnerStopped();
29
+ if (this.lastOutputWasStructured) {
30
+ process.stdout.write('\n');
31
+ this.lastOutputWasStructured = false;
32
+ }
33
+ process.stdout.write(text);
34
+ this.lastOutputWasRaw = true;
35
+ this.hasRawOutput = true;
36
+ if (text.length > 0) {
37
+ this.lastChar = text[text.length - 1];
38
+ }
39
+ }
40
+ addNewlinesIfNeeded() {
41
+ if (!this.lastOutputWasRaw) {
42
+ return;
43
+ }
44
+ if (this.lastChar === '\n') {
45
+ process.stdout.write('\n');
46
+ }
47
+ else {
48
+ process.stdout.write('\n\n');
49
+ }
50
+ this.lastOutputWasRaw = false;
51
+ }
52
+ printStructured(data) {
53
+ this.ensureSpinnerStopped();
54
+ this.addNewlinesIfNeeded();
55
+ (0, output_js_1.print)(data);
56
+ this.lastOutputWasStructured = true;
57
+ }
58
+ reset() {
59
+ this.lastOutputWasRaw = false;
60
+ this.lastOutputWasStructured = false;
61
+ this.hasRawOutput = false;
62
+ this.lastChar = '';
63
+ }
64
+ hadRawOutput() {
65
+ return this.hasRawOutput;
66
+ }
67
+ writeLine(text) {
68
+ this.ensureSpinnerStopped();
69
+ this.addNewlinesIfNeeded();
70
+ console.log(text);
71
+ this.lastOutputWasStructured = true;
72
+ }
73
+ }
13
74
  function getClient() {
14
75
  return new sdk_1.ChatBotKit({
15
76
  secret: (0, env_js_1.getSECRET)(),
@@ -42,7 +103,8 @@ exports.command = new commander_1.Command()
42
103
  }
43
104
  }
44
105
  const isInteractive = process.stdout.isTTY;
45
- const spinner = isInteractive ? new spinner_js_1.Spinner() : null;
106
+ const spinner = isInteractive ? new spinner_js_1.Spinner('') : null;
107
+ const output = new OutputManager(spinner);
46
108
  let exitResult = null;
47
109
  let hasOutput = false;
48
110
  for await (const { type, data } of (0, agent_1.execute)({
@@ -54,66 +116,73 @@ exports.command = new commander_1.Command()
54
116
  maxIterations: options.maxIterations,
55
117
  })) {
56
118
  if (type === 'iteration') {
57
- if (spinner) {
58
- if (spinner.isSpinning) {
59
- spinner.stop();
60
- }
119
+ if (isInteractive) {
61
120
  const iterationNum = data.iteration - 1;
62
- console.log(`\n\n╭─ Iteration ${iterationNum} ─╮`);
63
- spinner.start();
121
+ output.writeLine((0, color_js_1.formatBlue)(`\n╭─ Iteration ${iterationNum} ─╮`));
122
+ if (spinner) {
123
+ spinner.start();
124
+ }
64
125
  }
65
126
  else {
66
- (0, output_js_1.print)({ iteration: data.iteration - 1 });
127
+ output.printStructured({ iteration: data.iteration - 1 });
67
128
  }
129
+ output.reset();
68
130
  hasOutput = false;
69
131
  }
70
132
  else if (type === 'toolCallStart') {
71
- if (spinner && spinner.isSpinning) {
72
- spinner.stop();
73
- }
74
- (0, output_js_1.print)({ tool: data.name, status: 'running', args: data.args });
133
+ output.printStructured({
134
+ tool: data.name,
135
+ status: 'running',
136
+ args: data.args,
137
+ });
75
138
  if (spinner) {
76
139
  spinner.start();
77
140
  }
78
141
  }
79
142
  else if (type === 'toolCallEnd') {
80
- if (spinner && spinner.isSpinning) {
81
- spinner.stop();
82
- }
83
- (0, output_js_1.print)({ tool: data.name, status: 'completed', result: data.result });
143
+ output.printStructured({
144
+ tool: data.name,
145
+ status: 'completed',
146
+ result: data.result,
147
+ });
84
148
  if (spinner) {
85
149
  spinner.start();
86
150
  }
87
151
  }
88
152
  else if (type === 'toolCallError') {
89
- if (spinner && spinner.isSpinning) {
90
- spinner.stop();
91
- }
92
- (0, output_js_1.print)({ tool: data.name, status: 'error', error: data.error });
153
+ output.printStructured({
154
+ tool: data.name,
155
+ status: 'error',
156
+ error: data.error,
157
+ });
93
158
  if (spinner) {
94
159
  spinner.start();
95
160
  }
96
161
  }
97
162
  else if (type === 'token') {
98
- if (spinner && spinner.isSpinning) {
99
- spinner.stop();
100
- }
101
- if (!hasOutput && isInteractive) {
102
- process.stdout.write('> ');
103
- hasOutput = true;
163
+ if (isInteractive) {
164
+ if (!hasOutput) {
165
+ output.writeRaw('> ');
166
+ hasOutput = true;
167
+ }
168
+ output.writeRaw(data.token.replace(/\n/gm, '\n> '));
104
169
  }
105
- process.stdout.write(data.token);
106
170
  }
107
171
  else if (type === 'exit') {
108
172
  exitResult = data;
109
173
  }
174
+ else if (type === 'message') {
175
+ if (data.type === 'bot') {
176
+ if (!isInteractive) {
177
+ output.printStructured({
178
+ message: data,
179
+ });
180
+ }
181
+ }
182
+ }
110
183
  }
111
- if (spinner && spinner.isSpinning) {
112
- spinner.stop();
113
- }
114
- process.stdout.write('\n\n');
115
184
  if (exitResult) {
116
- (0, output_js_1.print)({
185
+ output.printStructured({
117
186
  status: exitResult.code === 0 ? 'success' : 'failed',
118
187
  exitCode: exitResult.code,
119
188
  ...(exitResult.message && { message: exitResult.message }),
@@ -2,15 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = cbk;
4
4
  const tslib_1 = require("tslib");
5
+ const color_js_1 = require("./color.cjs");
5
6
  const index_js_1 = tslib_1.__importDefault(require("./command/agent/index.cjs"));
6
7
  const index_js_2 = tslib_1.__importDefault(require("./command/api/index.cjs"));
7
8
  const index_js_3 = tslib_1.__importDefault(require("./command/chat/index.cjs"));
8
9
  const index_js_4 = tslib_1.__importDefault(require("./command/solution/index.cjs"));
9
10
  const commander_1 = require("commander");
10
11
  function printBanner() {
11
- const blue = '\x1b[34m';
12
- const reset = '\x1b[0m';
13
- console.log(`${blue}
12
+ console.log((0, color_js_1.formatBlue)(`
14
13
  .d8888b. 888888b. 888 d8P
15
14
  d88P Y88b 888 "88b 888 d8P
16
15
  888 888 888 .88P 888 d8P
@@ -18,7 +17,8 @@ d88P Y88b 888 "88b 888 d8P
18
17
  888 888 "Y88b 8888888b
19
18
  888 888 888 888 888 Y88b
20
19
  Y88b d88P 888 d88P 888 Y88b
21
- "Y8888P" 8888888P" 888 Y88b .ai${reset}`);
20
+ "Y8888P" 8888888P" 888 Y88b .ai
21
+ `));
22
22
  }
23
23
  async function cbk(argv = process.argv) {
24
24
  const program = new commander_1.Command();
@@ -0,0 +1,3 @@
1
+ export const BLUE: "\u001B[34m";
2
+ export const RESET: "\u001B[0m";
3
+ export function formatBlue(text: string): string;
@@ -0,0 +1,5 @@
1
+ export const BLUE = '\u001b[34m';
2
+ export const RESET = '\u001b[0m';
3
+ export const formatBlue = (text) => {
4
+ return `${BLUE}${text}${RESET}`;
5
+ };
@@ -1,3 +1,4 @@
1
+ import { formatBlue } from '../../color.js';
1
2
  import { getRUNAS_USERID, getSECRET } from '../../env.js';
2
3
  import { print, printError } from '../../output.js';
3
4
  import { Spinner } from '../../spinner.js';
@@ -7,6 +8,66 @@ import { ChatBotKit } from '@chatbotkit/sdk';
7
8
  import { Command, Option } from 'commander';
8
9
  import { existsSync, readFileSync } from 'fs';
9
10
  import { resolve } from 'path';
11
+ class OutputManager {
12
+ constructor(spinner = null) {
13
+ this.lastOutputWasRaw = false;
14
+ this.lastOutputWasStructured = false;
15
+ this.hasRawOutput = false;
16
+ this.lastChar = '';
17
+ this.spinner = spinner;
18
+ }
19
+ ensureSpinnerStopped() {
20
+ if (this.spinner && this.spinner.isSpinning) {
21
+ this.spinner.stop(false);
22
+ }
23
+ }
24
+ writeRaw(text) {
25
+ this.ensureSpinnerStopped();
26
+ if (this.lastOutputWasStructured) {
27
+ process.stdout.write('\n');
28
+ this.lastOutputWasStructured = false;
29
+ }
30
+ process.stdout.write(text);
31
+ this.lastOutputWasRaw = true;
32
+ this.hasRawOutput = true;
33
+ if (text.length > 0) {
34
+ this.lastChar = text[text.length - 1];
35
+ }
36
+ }
37
+ addNewlinesIfNeeded() {
38
+ if (!this.lastOutputWasRaw) {
39
+ return;
40
+ }
41
+ if (this.lastChar === '\n') {
42
+ process.stdout.write('\n');
43
+ }
44
+ else {
45
+ process.stdout.write('\n\n');
46
+ }
47
+ this.lastOutputWasRaw = false;
48
+ }
49
+ printStructured(data) {
50
+ this.ensureSpinnerStopped();
51
+ this.addNewlinesIfNeeded();
52
+ print(data);
53
+ this.lastOutputWasStructured = true;
54
+ }
55
+ reset() {
56
+ this.lastOutputWasRaw = false;
57
+ this.lastOutputWasStructured = false;
58
+ this.hasRawOutput = false;
59
+ this.lastChar = '';
60
+ }
61
+ hadRawOutput() {
62
+ return this.hasRawOutput;
63
+ }
64
+ writeLine(text) {
65
+ this.ensureSpinnerStopped();
66
+ this.addNewlinesIfNeeded();
67
+ console.log(text);
68
+ this.lastOutputWasStructured = true;
69
+ }
70
+ }
10
71
  function getClient() {
11
72
  return new ChatBotKit({
12
73
  secret: getSECRET(),
@@ -39,7 +100,8 @@ export const command = new Command()
39
100
  }
40
101
  }
41
102
  const isInteractive = process.stdout.isTTY;
42
- const spinner = isInteractive ? new Spinner() : null;
103
+ const spinner = isInteractive ? new Spinner('') : null;
104
+ const output = new OutputManager(spinner);
43
105
  let exitResult = null;
44
106
  let hasOutput = false;
45
107
  for await (const { type, data } of execute({
@@ -51,66 +113,73 @@ export const command = new Command()
51
113
  maxIterations: options.maxIterations,
52
114
  })) {
53
115
  if (type === 'iteration') {
54
- if (spinner) {
55
- if (spinner.isSpinning) {
56
- spinner.stop();
57
- }
116
+ if (isInteractive) {
58
117
  const iterationNum = data.iteration - 1;
59
- console.log(`\n\n╭─ Iteration ${iterationNum} ─╮`);
60
- spinner.start();
118
+ output.writeLine(formatBlue(`\n╭─ Iteration ${iterationNum} ─╮`));
119
+ if (spinner) {
120
+ spinner.start();
121
+ }
61
122
  }
62
123
  else {
63
- print({ iteration: data.iteration - 1 });
124
+ output.printStructured({ iteration: data.iteration - 1 });
64
125
  }
126
+ output.reset();
65
127
  hasOutput = false;
66
128
  }
67
129
  else if (type === 'toolCallStart') {
68
- if (spinner && spinner.isSpinning) {
69
- spinner.stop();
70
- }
71
- print({ tool: data.name, status: 'running', args: data.args });
130
+ output.printStructured({
131
+ tool: data.name,
132
+ status: 'running',
133
+ args: data.args,
134
+ });
72
135
  if (spinner) {
73
136
  spinner.start();
74
137
  }
75
138
  }
76
139
  else if (type === 'toolCallEnd') {
77
- if (spinner && spinner.isSpinning) {
78
- spinner.stop();
79
- }
80
- print({ tool: data.name, status: 'completed', result: data.result });
140
+ output.printStructured({
141
+ tool: data.name,
142
+ status: 'completed',
143
+ result: data.result,
144
+ });
81
145
  if (spinner) {
82
146
  spinner.start();
83
147
  }
84
148
  }
85
149
  else if (type === 'toolCallError') {
86
- if (spinner && spinner.isSpinning) {
87
- spinner.stop();
88
- }
89
- print({ tool: data.name, status: 'error', error: data.error });
150
+ output.printStructured({
151
+ tool: data.name,
152
+ status: 'error',
153
+ error: data.error,
154
+ });
90
155
  if (spinner) {
91
156
  spinner.start();
92
157
  }
93
158
  }
94
159
  else if (type === 'token') {
95
- if (spinner && spinner.isSpinning) {
96
- spinner.stop();
97
- }
98
- if (!hasOutput && isInteractive) {
99
- process.stdout.write('> ');
100
- hasOutput = true;
160
+ if (isInteractive) {
161
+ if (!hasOutput) {
162
+ output.writeRaw('> ');
163
+ hasOutput = true;
164
+ }
165
+ output.writeRaw(data.token.replace(/\n/gm, '\n> '));
101
166
  }
102
- process.stdout.write(data.token);
103
167
  }
104
168
  else if (type === 'exit') {
105
169
  exitResult = data;
106
170
  }
171
+ else if (type === 'message') {
172
+ if (data.type === 'bot') {
173
+ if (!isInteractive) {
174
+ output.printStructured({
175
+ message: data,
176
+ });
177
+ }
178
+ }
179
+ }
107
180
  }
108
- if (spinner && spinner.isSpinning) {
109
- spinner.stop();
110
- }
111
- process.stdout.write('\n\n');
112
181
  if (exitResult) {
113
- print({
182
+ output.printStructured({
114
183
  status: exitResult.code === 0 ? 'success' : 'failed',
115
184
  exitCode: exitResult.code,
116
185
  ...(exitResult.message && { message: exitResult.message }),
package/dist/esm/index.js CHANGED
@@ -1,12 +1,11 @@
1
+ import { formatBlue } from './color.js';
1
2
  import agent from './command/agent/index.js';
2
3
  import command from './command/api/index.js';
3
4
  import chat from './command/chat/index.js';
4
5
  import solution from './command/solution/index.js';
5
6
  import { Command } from 'commander';
6
7
  function printBanner() {
7
- const blue = '\x1b[34m';
8
- const reset = '\x1b[0m';
9
- console.log(`${blue}
8
+ console.log(formatBlue(`
10
9
  .d8888b. 888888b. 888 d8P
11
10
  d88P Y88b 888 "88b 888 d8P
12
11
  888 888 888 .88P 888 d8P
@@ -14,7 +13,8 @@ d88P Y88b 888 "88b 888 d8P
14
13
  888 888 "Y88b 8888888b
15
14
  888 888 888 888 888 Y88b
16
15
  Y88b d88P 888 d88P 888 Y88b
17
- "Y8888P" 8888888P" 888 Y88b .ai${reset}`);
16
+ "Y8888P" 8888888P" 888 Y88b .ai
17
+ `));
18
18
  }
19
19
  export default async function cbk(argv = process.argv) {
20
20
  const program = new Command();