chat-simple.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Simple chat interface demo using tui.ts
  3. */
  4. import chalk from "chalk";
  5. import { CombinedAutocompleteProvider } from "../src/autocomplete.ts";
  6. import { Editor } from "../src/components/editor.ts";
  7. import { Loader } from "../src/components/loader.ts";
  8. import { Markdown } from "../src/components/markdown.ts";
  9. import { Text } from "../src/components/text.ts";
  10. import { ProcessTerminal } from "../src/terminal.ts";
  11. import { TUI } from "../src/tui.ts";
  12. import { defaultEditorTheme, defaultMarkdownTheme } from "./test-themes.ts";
  13. // Create terminal
  14. const terminal = new ProcessTerminal();
  15. // Create TUI
  16. const tui = new TUI(terminal);
  17. // Create chat container with some initial messages
  18. tui.addChild(
  19. new Text("Welcome to Simple Chat!\n\nType your messages below. Type '/' for commands. Press Ctrl+C to exit."),
  20. );
  21. // Create editor with autocomplete
  22. const editor = new Editor(tui, defaultEditorTheme);
  23. // Set up autocomplete provider with slash commands and file completion
  24. const autocompleteProvider = new CombinedAutocompleteProvider(
  25. [
  26. { name: "delete", description: "Delete the last message" },
  27. { name: "clear", description: "Clear all messages" },
  28. ],
  29. process.cwd(),
  30. );
  31. editor.setAutocompleteProvider(autocompleteProvider);
  32. tui.addChild(editor);
  33. // Focus the editor
  34. tui.setFocus(editor);
  35. // Track if we're waiting for bot response
  36. let isResponding = false;
  37. // Handle message submission
  38. editor.onSubmit = (value: string) => {
  39. // Prevent submission if already responding
  40. if (isResponding) {
  41. return;
  42. }
  43. const trimmed = value.trim();
  44. // Handle slash commands
  45. if (trimmed === "/delete") {
  46. const children = tui.children;
  47. // Remove component before editor (if there are any besides the initial text)
  48. if (children.length > 3) {
  49. // children[0] = "Welcome to Simple Chat!"
  50. // children[1] = "Type your messages below..."
  51. // children[2...n-1] = messages
  52. // children[n] = editor
  53. children.splice(children.length - 2, 1);
  54. }
  55. tui.requestRender();
  56. return;
  57. }
  58. if (trimmed === "/clear") {
  59. const children = tui.children;
  60. // Remove all messages but keep the welcome text and editor
  61. children.splice(2, children.length - 3);
  62. tui.requestRender();
  63. return;
  64. }
  65. if (trimmed) {
  66. isResponding = true;
  67. editor.disableSubmit = true;
  68. const userMessage = new Markdown(value, 1, 1, defaultMarkdownTheme);
  69. const children = tui.children;
  70. children.splice(children.length - 1, 0, userMessage);
  71. const loader = new Loader(
  72. tui,
  73. (s) => chalk.cyan(s),
  74. (s) => chalk.dim(s),
  75. "Thinking...",
  76. );
  77. children.splice(children.length - 1, 0, loader);
  78. tui.requestRender();
  79. setTimeout(() => {
  80. tui.removeChild(loader);
  81. // Simulate a response
  82. const responses = [
  83. "That's interesting! Tell me more.",
  84. "I see what you mean.",
  85. "Fascinating perspective!",
  86. "Could you elaborate on that?",
  87. "That makes sense to me.",
  88. "I hadn't thought of it that way.",
  89. "Great point!",
  90. "Thanks for sharing that.",
  91. ];
  92. const randomResponse = responses[Math.floor(Math.random() * responses.length)];
  93. // Add assistant message with no background (transparent)
  94. const botMessage = new Markdown(randomResponse, 1, 1, defaultMarkdownTheme);
  95. children.splice(children.length - 1, 0, botMessage);
  96. // Re-enable submit
  97. isResponding = false;
  98. editor.disableSubmit = false;
  99. // Request render
  100. tui.requestRender();
  101. }, 1000);
  102. }
  103. };
  104. // Start the TUI
  105. tui.start();