kimaki 0.4.21 → 0.4.23

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.
Files changed (43) hide show
  1. package/dist/channel-management.js +92 -0
  2. package/dist/cli.js +10 -2
  3. package/dist/database.js +130 -0
  4. package/dist/discord-bot.js +381 -0
  5. package/dist/discord-utils.js +151 -0
  6. package/dist/discordBot.js +60 -31
  7. package/dist/escape-backticks.test.js +1 -1
  8. package/dist/fork.js +163 -0
  9. package/dist/format-tables.js +93 -0
  10. package/dist/format-tables.test.js +418 -0
  11. package/dist/interaction-handler.js +750 -0
  12. package/dist/markdown.js +3 -3
  13. package/dist/message-formatting.js +188 -0
  14. package/dist/model-command.js +293 -0
  15. package/dist/opencode.js +135 -0
  16. package/dist/session-handler.js +467 -0
  17. package/dist/system-message.js +92 -0
  18. package/dist/tools.js +3 -5
  19. package/dist/utils.js +31 -0
  20. package/dist/voice-handler.js +528 -0
  21. package/dist/voice.js +257 -35
  22. package/package.json +3 -2
  23. package/src/channel-management.ts +145 -0
  24. package/src/cli.ts +10 -2
  25. package/src/database.ts +155 -0
  26. package/src/discord-bot.ts +506 -0
  27. package/src/discord-utils.ts +208 -0
  28. package/src/escape-backticks.test.ts +1 -1
  29. package/src/fork.ts +224 -0
  30. package/src/format-tables.test.ts +440 -0
  31. package/src/format-tables.ts +106 -0
  32. package/src/interaction-handler.ts +1000 -0
  33. package/src/markdown.ts +3 -3
  34. package/src/message-formatting.ts +227 -0
  35. package/src/model-command.ts +380 -0
  36. package/src/opencode.ts +180 -0
  37. package/src/session-handler.ts +601 -0
  38. package/src/system-message.ts +92 -0
  39. package/src/tools.ts +3 -5
  40. package/src/utils.ts +37 -0
  41. package/src/voice-handler.ts +745 -0
  42. package/src/voice.ts +354 -36
  43. package/src/discordBot.ts +0 -3643
@@ -0,0 +1,106 @@
1
+ import { Lexer, type Token, type Tokens } from 'marked'
2
+
3
+ export function formatMarkdownTables(markdown: string): string {
4
+ const lexer = new Lexer()
5
+ const tokens = lexer.lex(markdown)
6
+
7
+ let result = ''
8
+ for (const token of tokens) {
9
+ if (token.type === 'table') {
10
+ result += formatTableToken(token as Tokens.Table)
11
+ } else {
12
+ result += token.raw
13
+ }
14
+ }
15
+ return result
16
+ }
17
+
18
+ function formatTableToken(table: Tokens.Table): string {
19
+ const headers = table.header.map((cell) => {
20
+ return extractCellText(cell.tokens)
21
+ })
22
+ const rows = table.rows.map((row) => {
23
+ return row.map((cell) => {
24
+ return extractCellText(cell.tokens)
25
+ })
26
+ })
27
+
28
+ const columnWidths = calculateColumnWidths(headers, rows)
29
+ const lines: string[] = []
30
+
31
+ lines.push(formatRow(headers, columnWidths))
32
+ lines.push(formatSeparator(columnWidths))
33
+ for (const row of rows) {
34
+ lines.push(formatRow(row, columnWidths))
35
+ }
36
+
37
+ return '```\n' + lines.join('\n') + '\n```\n'
38
+ }
39
+
40
+ function extractCellText(tokens: Token[]): string {
41
+ const parts: string[] = []
42
+ for (const token of tokens) {
43
+ parts.push(extractTokenText(token))
44
+ }
45
+ return parts.join('').trim()
46
+ }
47
+
48
+ function extractTokenText(token: Token): string {
49
+ switch (token.type) {
50
+ case 'text':
51
+ case 'codespan':
52
+ case 'escape':
53
+ return token.text
54
+ case 'link':
55
+ return token.href
56
+ case 'image':
57
+ return token.href
58
+ case 'strong':
59
+ case 'em':
60
+ case 'del':
61
+ return token.tokens ? extractCellText(token.tokens) : token.text
62
+ case 'br':
63
+ return ' '
64
+ default: {
65
+ const tokenAny = token as { tokens?: Token[]; text?: string }
66
+ if (tokenAny.tokens && Array.isArray(tokenAny.tokens)) {
67
+ return extractCellText(tokenAny.tokens)
68
+ }
69
+ if (typeof tokenAny.text === 'string') {
70
+ return tokenAny.text
71
+ }
72
+ return ''
73
+ }
74
+ }
75
+ }
76
+
77
+ function calculateColumnWidths(
78
+ headers: string[],
79
+ rows: string[][],
80
+ ): number[] {
81
+ const widths = headers.map((h) => {
82
+ return h.length
83
+ })
84
+ for (const row of rows) {
85
+ for (let i = 0; i < row.length; i++) {
86
+ const cell = row[i] ?? ''
87
+ widths[i] = Math.max(widths[i] ?? 0, cell.length)
88
+ }
89
+ }
90
+ return widths
91
+ }
92
+
93
+ function formatRow(cells: string[], widths: number[]): string {
94
+ const paddedCells = cells.map((cell, i) => {
95
+ return cell.padEnd(widths[i] ?? 0)
96
+ })
97
+ return paddedCells.join(' ')
98
+ }
99
+
100
+ function formatSeparator(widths: number[]): string {
101
+ return widths
102
+ .map((w) => {
103
+ return '-'.repeat(w)
104
+ })
105
+ .join(' ')
106
+ }