kimaki 0.4.20 → 0.4.22

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,440 @@
1
+ import { test, expect } from 'vitest'
2
+ import { formatMarkdownTables } from './format-tables.js'
3
+
4
+ test('formats simple table', () => {
5
+ const input = `| Name | Age |
6
+ | --- | --- |
7
+ | Alice | 30 |
8
+ | Bob | 25 |`
9
+ const result = formatMarkdownTables(input)
10
+ expect(result).toMatchInlineSnapshot(`
11
+ "\`\`\`
12
+ Name Age
13
+ ----- ---
14
+ Alice 30
15
+ Bob 25
16
+ \`\`\`
17
+ "
18
+ `)
19
+ })
20
+
21
+ test('formats table with varying column widths', () => {
22
+ const input = `| Item | Quantity | Price |
23
+ | --- | --- | --- |
24
+ | Apples | 10 | $5 |
25
+ | Oranges | 3 | $2 |
26
+ | Bananas with long name | 100 | $15.99 |`
27
+ const result = formatMarkdownTables(input)
28
+ expect(result).toMatchInlineSnapshot(`
29
+ "\`\`\`
30
+ Item Quantity Price
31
+ ---------------------- -------- ------
32
+ Apples 10 $5
33
+ Oranges 3 $2
34
+ Bananas with long name 100 $15.99
35
+ \`\`\`
36
+ "
37
+ `)
38
+ })
39
+
40
+ test('strips bold formatting from cells', () => {
41
+ const input = `| Header | Value |
42
+ | --- | --- |
43
+ | **Bold text** | Normal |
44
+ | Mixed **bold** text | Another |`
45
+ const result = formatMarkdownTables(input)
46
+ expect(result).toMatchInlineSnapshot(`
47
+ "\`\`\`
48
+ Header Value
49
+ --------------- -------
50
+ Bold text Normal
51
+ Mixed bold text Another
52
+ \`\`\`
53
+ "
54
+ `)
55
+ })
56
+
57
+ test('strips italic formatting from cells', () => {
58
+ const input = `| Header | Value |
59
+ | --- | --- |
60
+ | *Italic text* | Normal |
61
+ | _Also italic_ | Another |`
62
+ const result = formatMarkdownTables(input)
63
+ expect(result).toMatchInlineSnapshot(`
64
+ "\`\`\`
65
+ Header Value
66
+ ----------- -------
67
+ Italic text Normal
68
+ Also italic Another
69
+ \`\`\`
70
+ "
71
+ `)
72
+ })
73
+
74
+ test('extracts URL from links', () => {
75
+ const input = `| Name | Link |
76
+ | --- | --- |
77
+ | Google | [Click here](https://google.com) |
78
+ | GitHub | [GitHub Home](https://github.com) |`
79
+ const result = formatMarkdownTables(input)
80
+ expect(result).toMatchInlineSnapshot(`
81
+ "\`\`\`
82
+ Name Link
83
+ ------ ------------------
84
+ Google https://google.com
85
+ GitHub https://github.com
86
+ \`\`\`
87
+ "
88
+ `)
89
+ })
90
+
91
+ test('handles inline code in cells', () => {
92
+ const input = `| Function | Description |
93
+ | --- | --- |
94
+ | \`console.log\` | Logs to console |
95
+ | \`Array.map\` | Maps array items |`
96
+ const result = formatMarkdownTables(input)
97
+ expect(result).toMatchInlineSnapshot(`
98
+ "\`\`\`
99
+ Function Description
100
+ ----------- ----------------
101
+ console.log Logs to console
102
+ Array.map Maps array items
103
+ \`\`\`
104
+ "
105
+ `)
106
+ })
107
+
108
+ test('handles mixed formatting in single cell', () => {
109
+ const input = `| Description |
110
+ | --- |
111
+ | This has **bold**, *italic*, and \`code\` |
112
+ | Also [a link](https://example.com) here |`
113
+ const result = formatMarkdownTables(input)
114
+ expect(result).toMatchInlineSnapshot(`
115
+ "\`\`\`
116
+ Description
117
+ -------------------------------
118
+ This has bold, italic, and code
119
+ Also https://example.com here
120
+ \`\`\`
121
+ "
122
+ `)
123
+ })
124
+
125
+ test('handles strikethrough text', () => {
126
+ const input = `| Status | Item |
127
+ | --- | --- |
128
+ | Done | ~~Deleted item~~ |
129
+ | Active | Normal item |`
130
+ const result = formatMarkdownTables(input)
131
+ expect(result).toMatchInlineSnapshot(`
132
+ "\`\`\`
133
+ Status Item
134
+ ------ ------------
135
+ Done Deleted item
136
+ Active Normal item
137
+ \`\`\`
138
+ "
139
+ `)
140
+ })
141
+
142
+ test('preserves content before table', () => {
143
+ const input = `Here is some text before the table.
144
+
145
+ | Col A | Col B |
146
+ | --- | --- |
147
+ | 1 | 2 |`
148
+ const result = formatMarkdownTables(input)
149
+ expect(result).toMatchInlineSnapshot(`
150
+ "Here is some text before the table.
151
+
152
+ \`\`\`
153
+ Col A Col B
154
+ ----- -----
155
+ 1 2
156
+ \`\`\`
157
+ "
158
+ `)
159
+ })
160
+
161
+ test('preserves content after table', () => {
162
+ const input = `| Col A | Col B |
163
+ | --- | --- |
164
+ | 1 | 2 |
165
+
166
+ And here is text after.`
167
+ const result = formatMarkdownTables(input)
168
+ expect(result).toMatchInlineSnapshot(`
169
+ "\`\`\`
170
+ Col A Col B
171
+ ----- -----
172
+ 1 2
173
+ \`\`\`
174
+ And here is text after."
175
+ `)
176
+ })
177
+
178
+ test('preserves content before and after table', () => {
179
+ const input = `Some intro text.
180
+
181
+ | Name | Value |
182
+ | --- | --- |
183
+ | Key | 123 |
184
+
185
+ Some outro text.`
186
+ const result = formatMarkdownTables(input)
187
+ expect(result).toMatchInlineSnapshot(`
188
+ "Some intro text.
189
+
190
+ \`\`\`
191
+ Name Value
192
+ ---- -----
193
+ Key 123
194
+ \`\`\`
195
+ Some outro text."
196
+ `)
197
+ })
198
+
199
+ test('handles multiple tables in same content', () => {
200
+ const input = `First table:
201
+
202
+ | A | B |
203
+ | --- | --- |
204
+ | 1 | 2 |
205
+
206
+ Some text between.
207
+
208
+ Second table:
209
+
210
+ | X | Y | Z |
211
+ | --- | --- | --- |
212
+ | a | b | c |`
213
+ const result = formatMarkdownTables(input)
214
+ expect(result).toMatchInlineSnapshot(`
215
+ "First table:
216
+
217
+ \`\`\`
218
+ A B
219
+ - -
220
+ 1 2
221
+ \`\`\`
222
+ Some text between.
223
+
224
+ Second table:
225
+
226
+ \`\`\`
227
+ X Y Z
228
+ - - -
229
+ a b c
230
+ \`\`\`
231
+ "
232
+ `)
233
+ })
234
+
235
+ test('handles empty cells', () => {
236
+ const input = `| Name | Optional |
237
+ | --- | --- |
238
+ | Alice | |
239
+ | | Bob |
240
+ | | |`
241
+ const result = formatMarkdownTables(input)
242
+ expect(result).toMatchInlineSnapshot(`
243
+ "\`\`\`
244
+ Name Optional
245
+ ----- --------
246
+ Alice
247
+ Bob
248
+
249
+ \`\`\`
250
+ "
251
+ `)
252
+ })
253
+
254
+ test('handles single column table', () => {
255
+ const input = `| Items |
256
+ | --- |
257
+ | Apple |
258
+ | Banana |
259
+ | Cherry |`
260
+ const result = formatMarkdownTables(input)
261
+ expect(result).toMatchInlineSnapshot(`
262
+ "\`\`\`
263
+ Items
264
+ ------
265
+ Apple
266
+ Banana
267
+ Cherry
268
+ \`\`\`
269
+ "
270
+ `)
271
+ })
272
+
273
+ test('handles single row table', () => {
274
+ const input = `| A | B | C | D |
275
+ | --- | --- | --- | --- |
276
+ | 1 | 2 | 3 | 4 |`
277
+ const result = formatMarkdownTables(input)
278
+ expect(result).toMatchInlineSnapshot(`
279
+ "\`\`\`
280
+ A B C D
281
+ - - - -
282
+ 1 2 3 4
283
+ \`\`\`
284
+ "
285
+ `)
286
+ })
287
+
288
+ test('handles nested formatting', () => {
289
+ const input = `| Description |
290
+ | --- |
291
+ | **Bold with *nested italic* inside** |
292
+ | *Italic with **nested bold** inside* |`
293
+ const result = formatMarkdownTables(input)
294
+ expect(result).toMatchInlineSnapshot(`
295
+ "\`\`\`
296
+ Description
297
+ ------------------------------
298
+ Bold with nested italic inside
299
+ Italic with nested bold inside
300
+ \`\`\`
301
+ "
302
+ `)
303
+ })
304
+
305
+ test('handles image references', () => {
306
+ const input = `| Icon | Name |
307
+ | --- | --- |
308
+ | ![alt](https://example.com/icon.png) | Item 1 |
309
+ | ![](https://cdn.test.com/img.jpg) | Item 2 |`
310
+ const result = formatMarkdownTables(input)
311
+ expect(result).toMatchInlineSnapshot(`
312
+ "\`\`\`
313
+ Icon Name
314
+ ---------------------------- ------
315
+ https://example.com/icon.png Item 1
316
+ https://cdn.test.com/img.jpg Item 2
317
+ \`\`\`
318
+ "
319
+ `)
320
+ })
321
+
322
+ test('preserves code blocks alongside tables', () => {
323
+ const input = `Some code:
324
+
325
+ \`\`\`js
326
+ const x = 1
327
+ \`\`\`
328
+
329
+ A table:
330
+
331
+ | Key | Value |
332
+ | --- | --- |
333
+ | a | 1 |
334
+
335
+ More code:
336
+
337
+ \`\`\`python
338
+ print("hello")
339
+ \`\`\``
340
+ const result = formatMarkdownTables(input)
341
+ expect(result).toMatchInlineSnapshot(`
342
+ "Some code:
343
+
344
+ \`\`\`js
345
+ const x = 1
346
+ \`\`\`
347
+
348
+ A table:
349
+
350
+ \`\`\`
351
+ Key Value
352
+ --- -----
353
+ a 1
354
+ \`\`\`
355
+ More code:
356
+
357
+ \`\`\`python
358
+ print("hello")
359
+ \`\`\`"
360
+ `)
361
+ })
362
+
363
+ test('handles content without tables', () => {
364
+ const input = `Just some regular markdown.
365
+
366
+ - List item 1
367
+ - List item 2
368
+
369
+ **Bold text** and *italic*.`
370
+ const result = formatMarkdownTables(input)
371
+ expect(result).toMatchInlineSnapshot(`
372
+ "Just some regular markdown.
373
+
374
+ - List item 1
375
+ - List item 2
376
+
377
+ **Bold text** and *italic*."
378
+ `)
379
+ })
380
+
381
+ test('handles complex real-world table', () => {
382
+ const input = `## API Endpoints
383
+
384
+ | Method | Endpoint | Description | Auth |
385
+ | --- | --- | --- | --- |
386
+ | GET | \`/api/users\` | List all users | [Bearer token](https://docs.example.com/auth) |
387
+ | POST | \`/api/users\` | Create **new** user | Required |
388
+ | DELETE | \`/api/users/:id\` | ~~Remove~~ *Deactivate* user | Admin only |`
389
+ const result = formatMarkdownTables(input)
390
+ expect(result).toMatchInlineSnapshot(`
391
+ "## API Endpoints
392
+
393
+ \`\`\`
394
+ Method Endpoint Description Auth
395
+ ------ -------------- ---------------------- -----------------------------
396
+ GET /api/users List all users https://docs.example.com/auth
397
+ POST /api/users Create new user Required
398
+ DELETE /api/users/:id Remove Deactivate user Admin only
399
+ \`\`\`
400
+ "
401
+ `)
402
+ })
403
+
404
+ test('handles unicode content', () => {
405
+ const input = `| Emoji | Name | Country |
406
+ | --- | --- | --- |
407
+ | 🍎 | Apple | 日本 |
408
+ | 🍊 | Orange | España |
409
+ | 🍌 | Banana | Ελλάδα |`
410
+ const result = formatMarkdownTables(input)
411
+ expect(result).toMatchInlineSnapshot(`
412
+ "\`\`\`
413
+ Emoji Name Country
414
+ ----- ------ -------
415
+ 🍎 Apple 日本
416
+ 🍊 Orange España
417
+ 🍌 Banana Ελλάδα
418
+ \`\`\`
419
+ "
420
+ `)
421
+ })
422
+
423
+ test('handles numbers and special characters', () => {
424
+ const input = `| Price | Discount | Final |
425
+ | --- | --- | --- |
426
+ | $100.00 | -15% | $85.00 |
427
+ | €50,00 | -10% | €45,00 |
428
+ | £75.99 | N/A | £75.99 |`
429
+ const result = formatMarkdownTables(input)
430
+ expect(result).toMatchInlineSnapshot(`
431
+ "\`\`\`
432
+ Price Discount Final
433
+ ------- -------- ------
434
+ $100.00 -15% $85.00
435
+ €50,00 -10% €45,00
436
+ £75.99 N/A £75.99
437
+ \`\`\`
438
+ "
439
+ `)
440
+ })
@@ -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
+ }
package/src/markdown.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { OpencodeClient } from '@opencode-ai/sdk'
2
- import { format } from 'date-fns'
3
2
  import * as yaml from 'js-yaml'
3
+ import { formatDateTime } from './utils.js'
4
4
  import { extractNonXmlContent } from './xml.js'
5
5
 
6
6
  export class ShareMarkdown {
@@ -62,10 +62,10 @@ export class ShareMarkdown {
62
62
  lines.push('## Session Information')
63
63
  lines.push('')
64
64
  lines.push(
65
- `- **Created**: ${format(new Date(session.time.created), 'MMM d, yyyy, h:mm a')}`,
65
+ `- **Created**: ${formatDateTime(new Date(session.time.created))}`,
66
66
  )
67
67
  lines.push(
68
- `- **Updated**: ${format(new Date(session.time.updated), 'MMM d, yyyy, h:mm a')}`,
68
+ `- **Updated**: ${formatDateTime(new Date(session.time.updated))}`,
69
69
  )
70
70
  if (session.version) {
71
71
  lines.push(`- **OpenCode Version**: v${session.version}`)
package/src/tools.ts CHANGED
@@ -11,9 +11,9 @@ import {
11
11
  import { createLogger } from './logger.js'
12
12
 
13
13
  const toolsLogger = createLogger('TOOLS')
14
- import { formatDistanceToNow } from 'date-fns'
15
14
 
16
15
  import { ShareMarkdown } from './markdown.js'
16
+ import { formatDistanceToNow } from './utils.js'
17
17
  import pc from 'picocolors'
18
18
  import {
19
19
  initializeOpencodeForDirectory,
@@ -232,9 +232,7 @@ export async function getTools({
232
232
  id: session.id,
233
233
  folder: session.directory,
234
234
  status,
235
- finishedAt: formatDistanceToNow(new Date(finishedAt), {
236
- addSuffix: true,
237
- }),
235
+ finishedAt: formatDistanceToNow(new Date(finishedAt)),
238
236
  title: session.title,
239
237
  prompt: session.title,
240
238
  }
package/src/utils.ts CHANGED
@@ -75,3 +75,40 @@ export function isAbortError(
75
75
  (error instanceof DOMException && error.name === 'AbortError')
76
76
  )
77
77
  }
78
+
79
+ const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
80
+
81
+ const TIME_DIVISIONS: Array<{ amount: number; name: Intl.RelativeTimeFormatUnit }> = [
82
+ { amount: 60, name: 'seconds' },
83
+ { amount: 60, name: 'minutes' },
84
+ { amount: 24, name: 'hours' },
85
+ { amount: 7, name: 'days' },
86
+ { amount: 4.34524, name: 'weeks' },
87
+ { amount: 12, name: 'months' },
88
+ { amount: Number.POSITIVE_INFINITY, name: 'years' },
89
+ ]
90
+
91
+ export function formatDistanceToNow(date: Date): string {
92
+ let duration = (date.getTime() - Date.now()) / 1000
93
+
94
+ for (const division of TIME_DIVISIONS) {
95
+ if (Math.abs(duration) < division.amount) {
96
+ return rtf.format(Math.round(duration), division.name)
97
+ }
98
+ duration /= division.amount
99
+ }
100
+ return rtf.format(Math.round(duration), 'years')
101
+ }
102
+
103
+ const dtf = new Intl.DateTimeFormat('en-US', {
104
+ month: 'short',
105
+ day: 'numeric',
106
+ year: 'numeric',
107
+ hour: 'numeric',
108
+ minute: '2-digit',
109
+ hour12: true,
110
+ })
111
+
112
+ export function formatDateTime(date: Date): string {
113
+ return dtf.format(date)
114
+ }