@cmodjs/core 6.3.0 → 8.0.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.
Files changed (138) hide show
  1. package/docs/commands.md +123 -65
  2. package/docs/dependencies.md +189 -4
  3. package/docs/index.md +56 -12
  4. package/docs/install-steps.md +20 -17
  5. package/docs/jobs.md +43 -16
  6. package/docs/mod.md +43 -27
  7. package/docs/options.md +2 -2
  8. package/docs/session.md +58 -32
  9. package/docs/testing.md +88 -24
  10. package/docs/ui.md +128 -58
  11. package/installer.d.ts +2 -2
  12. package/jobs/context.js +1 -1
  13. package/jobs/program.d.ts +4 -1
  14. package/jobs/program.d.ts.map +1 -1
  15. package/jobs/program.js +79 -20
  16. package/mod.d.ts +59 -9
  17. package/mod.d.ts.map +1 -1
  18. package/mod.js +5 -3
  19. package/package.json +1 -1
  20. package/records.d.ts +15 -2
  21. package/records.d.ts.map +1 -1
  22. package/records.js +24 -10
  23. package/register.d.ts.map +1 -1
  24. package/register.js +7 -4
  25. package/rpc.d.ts +7 -0
  26. package/rpc.d.ts.map +1 -0
  27. package/rpc.js +59 -0
  28. package/runtime/bound.d.ts +1 -0
  29. package/runtime/bound.d.ts.map +1 -1
  30. package/runtime/bound.js +10 -0
  31. package/runtime/bun.d.ts +20 -0
  32. package/runtime/bun.d.ts.map +1 -0
  33. package/runtime/bun.js +20 -0
  34. package/runtime/claude.d.ts +1 -0
  35. package/runtime/claude.d.ts.map +1 -1
  36. package/runtime/conversations.d.ts +12 -1
  37. package/runtime/conversations.d.ts.map +1 -1
  38. package/runtime/conversations.js +472 -362
  39. package/runtime/deadline.d.ts +4 -3
  40. package/runtime/deadline.d.ts.map +1 -1
  41. package/runtime/deadline.js +10 -4
  42. package/runtime/engine.d.ts +5 -4
  43. package/runtime/engine.d.ts.map +1 -1
  44. package/runtime/installer.d.ts +3 -3
  45. package/runtime/installer.d.ts.map +1 -1
  46. package/runtime/installer.js +10 -9
  47. package/runtime/interfaces.d.ts +262 -0
  48. package/runtime/interfaces.d.ts.map +1 -0
  49. package/runtime/interfaces.js +935 -0
  50. package/runtime/lifecycle.d.ts +3 -3
  51. package/runtime/lifecycle.d.ts.map +1 -1
  52. package/runtime/lifecycle.js +83 -52
  53. package/runtime/options.d.ts.map +1 -1
  54. package/runtime/options.js +2 -6
  55. package/runtime/parts.d.ts +64 -1
  56. package/runtime/parts.d.ts.map +1 -1
  57. package/runtime/parts.js +311 -4
  58. package/runtime/programs.d.ts +1 -0
  59. package/runtime/programs.d.ts.map +1 -1
  60. package/runtime/programs.js +5 -3
  61. package/runtime/rpc.d.ts +111 -0
  62. package/runtime/rpc.d.ts.map +1 -0
  63. package/runtime/rpc.js +387 -0
  64. package/runtime/sessions.d.ts +6 -4
  65. package/runtime/sessions.d.ts.map +1 -1
  66. package/runtime/sessions.js +59 -80
  67. package/runtime/toaster.js +1 -1
  68. package/runtime/ui.d.ts +23 -10
  69. package/runtime/ui.d.ts.map +1 -1
  70. package/runtime/ui.js +63 -52
  71. package/runtime/web.d.ts +0 -73
  72. package/runtime/web.d.ts.map +1 -1
  73. package/runtime/web.js +9 -5
  74. package/runtime/widgets.d.ts +4 -2
  75. package/runtime/widgets.d.ts.map +1 -1
  76. package/runtime/widgets.js +17 -16
  77. package/session/conversation.d.ts +7 -1
  78. package/session/conversation.d.ts.map +1 -1
  79. package/session/session.d.ts +31 -7
  80. package/session/session.d.ts.map +1 -1
  81. package/session/session.js +1 -0
  82. package/src/installer.ts +2 -2
  83. package/src/jobs/context.ts +1 -1
  84. package/src/jobs/program.ts +76 -21
  85. package/src/mod.ts +51 -10
  86. package/src/records.ts +33 -9
  87. package/src/register.ts +7 -4
  88. package/src/rpc.ts +67 -0
  89. package/src/runtime/bound.ts +10 -0
  90. package/src/runtime/bun.ts +40 -0
  91. package/src/runtime/claude.ts +1 -0
  92. package/src/runtime/conversations.ts +470 -352
  93. package/src/runtime/deadline.ts +13 -6
  94. package/src/runtime/engine.ts +5 -4
  95. package/src/runtime/installer.ts +13 -12
  96. package/src/runtime/interfaces.ts +1161 -0
  97. package/src/runtime/lifecycle.ts +102 -59
  98. package/src/runtime/options.ts +2 -5
  99. package/src/runtime/parts.ts +364 -5
  100. package/src/runtime/programs.ts +6 -3
  101. package/src/runtime/rpc.ts +457 -0
  102. package/src/runtime/sessions.ts +63 -83
  103. package/src/runtime/toaster.ts +1 -1
  104. package/src/runtime/ui.ts +82 -61
  105. package/src/runtime/web.ts +10 -39
  106. package/src/runtime/widgets.ts +20 -18
  107. package/src/session/conversation.ts +7 -1
  108. package/src/session/session.ts +22 -7
  109. package/src/testing/fake-claude.ts +38 -7
  110. package/src/testing/fake-serve.ts +144 -0
  111. package/src/testing/fake-sessions.ts +21 -6
  112. package/src/testing.ts +156 -68
  113. package/src/ui/areas.ts +2 -2
  114. package/src/ui/elements.ts +21 -16
  115. package/src/ui/panes.ts +14 -7
  116. package/testing/fake-claude.d.ts +6 -0
  117. package/testing/fake-claude.d.ts.map +1 -1
  118. package/testing/fake-claude.js +34 -7
  119. package/testing/fake-serve.d.ts +39 -0
  120. package/testing/fake-serve.d.ts.map +1 -0
  121. package/testing/fake-serve.js +110 -0
  122. package/testing/fake-sessions.d.ts.map +1 -1
  123. package/testing/fake-sessions.js +22 -6
  124. package/testing.d.ts +21 -4
  125. package/testing.d.ts.map +1 -1
  126. package/testing.js +138 -56
  127. package/ui/areas.d.ts +3 -1
  128. package/ui/areas.d.ts.map +1 -1
  129. package/ui/elements.d.ts +18 -9
  130. package/ui/elements.d.ts.map +1 -1
  131. package/ui/elements.js +9 -8
  132. package/ui/panes.d.ts +10 -6
  133. package/ui/panes.d.ts.map +1 -1
  134. package/ui/panes.js +5 -3
  135. package/runtime/pages.d.ts +0 -32
  136. package/runtime/pages.d.ts.map +0 -1
  137. package/runtime/pages.js +0 -142
  138. package/src/runtime/pages.ts +0 -183
@@ -7,6 +7,8 @@ export const longestMs = 600_000
7
7
 
8
8
  export const toolDeadline: Deadline = { ms: 30000, job: 'tool' }
9
9
 
10
+ export const callDeadline: Deadline = { ms: 30_000 }
11
+
10
12
  export type ListenOptions = { readonly timeoutMs?: number; readonly onFailure?: 'pass' | 'refuse' }
11
13
 
12
14
  export type ListenTime = { readonly deadline: number; readonly signal: AbortSignal }
@@ -19,9 +21,9 @@ export class PassedTimeout extends Error {}
19
21
 
20
22
  const timers = globalThis as unknown as { setTimeout(run: () => void, ms: number): unknown; clearTimeout(timer: unknown): void }
21
23
 
22
- export function listenerOf(name: string, method: string, args: readonly unknown[]): { readonly matcher: unknown; readonly listener: (payload: never, time: ListenTime) => unknown; readonly options: ListenOptions } {
24
+ export function listenerOf<Listener = (payload: never, time: ListenTime) => unknown>(name: string, method: string, args: readonly unknown[]): { readonly matcher: unknown; readonly listener: Listener; readonly options: ListenOptions } {
23
25
  const hasMatcher = typeof args[1] === 'function'
24
- const listener = (hasMatcher ? args[1] : args[0]) as (payload: never, time: ListenTime) => unknown
26
+ const listener = (hasMatcher ? args[1] : args[0]) as Listener
25
27
  const options = ((hasMatcher ? args[2] : args[1]) ?? {}) as ListenOptions
26
28
  if (typeof listener !== 'function') throw new Error(`${name}: ${method} takes a listener, or a matcher and then a listener.`)
27
29
  const { timeoutMs, onFailure } = options as { readonly timeoutMs?: unknown; readonly onFailure?: unknown }
@@ -68,11 +70,16 @@ export function settledAfterAbort<Value>(work: Promise<Value>, signal: AbortSign
68
70
  })
69
71
  }
70
72
 
71
- export function beforeDeadline<Value>(claude: Claude, { ms, job }: Deadline, call: string, task: Promise<Value>): Promise<Value> {
73
+ export function beforeDeadline<Value>(clock: Pick<Claude['clock'], 'after'>, { ms, job }: Deadline, call: string, run: (signal: AbortSignal) => Promise<Value>): Promise<Value> {
74
+ const passed = new AbortController()
72
75
  return new Promise<Value>((resolve, reject) => {
73
- const passed = job === undefined ? `its ${ms / 1000} s deadline` : `the ${ms / 1000} s deadline of ${job}`
74
- const timer = claude.clock.after(ms, () => reject(new Error(`${call} passed ${passed}`)))
75
- task.then(
76
+ const deadline = job === undefined ? `its ${ms / 1000} s deadline` : `the ${ms / 1000} s deadline of ${job}`
77
+ const timer = clock.after(ms, () => {
78
+ const error = new Error(`${call} passed ${deadline}`)
79
+ reject(error)
80
+ passed.abort(error)
81
+ })
82
+ run(passed.signal).then(
76
83
  (value) => {
77
84
  timer.cancel()
78
85
  resolve(value)
@@ -1,8 +1,8 @@
1
1
  import type { HookStream, ProcessSpawnChunk, ProcessSpawnResult } from 'claude-code'
2
2
  import type { Claude } from './claude.js'
3
- import type { Pages } from './pages.js'
3
+ import type { Providers } from './interfaces.js'
4
+ import type { Parts } from './parts.js'
4
5
  import type { Router } from './router.js'
5
- import type { Subscription } from '../utils/subscription.js'
6
6
  import type { Failure } from './toaster.js'
7
7
  import type { ToolCalls } from './tool-calls.js'
8
8
  import type { Watches } from './watches.js'
@@ -18,9 +18,10 @@ export type Engine = {
18
18
  readonly spawnCmod: (args: readonly string[], env?: Readonly<Record<string, string>>) => HookStream<ProcessSpawnChunk, ProcessSpawnResult>
19
19
  readonly isInstalledCopy: () => Promise<boolean>
20
20
  readonly watches: Watches
21
- readonly onRedraw: (listener: () => void) => Subscription
21
+ readonly interfaces: Providers
22
22
  readonly widgets?: Widgets
23
- readonly pages?: Pages
23
+ readonly parts?: Parts
24
+ readonly onServeUnreached: (listener: () => void) => void
24
25
  }
25
26
 
26
27
  const engines = new WeakMap<object, Engine>()
@@ -5,22 +5,22 @@ import type { Option } from '../options.js'
5
5
  import { isObject, secretFix } from '../records.js'
6
6
  import { Box, elementData, Input, Text, type Handler } from '../ui/elements.js'
7
7
  import { filledButton } from './buttons.js'
8
- import type { PageProps } from '../ui/panes.js'
8
+ import type { ViewProps } from '../ui/panes.js'
9
9
  import { messageOf } from '../utils/text.js'
10
10
  import type { Claude } from './claude.js'
11
11
  import { cmodPluginName, withBareRefusals } from './dependencies.js'
12
- import { merged, partMethods, stalePress, type DrawnPart, type PartPress, type PartsUpdate, type ShownPages } from './ui.js'
12
+ import { merged, partMethods, stalePress, type DrawnPart, type PartPress, type PartsUpdate, type ShownParts } from './ui.js'
13
13
 
14
- export const installPage = 'cmod:install'
14
+ export const installView = 'cmod:install'
15
15
 
16
16
  type Question = {
17
- draw(props: PageProps<undefined>): RenderElement
17
+ draw(props: ViewProps<undefined>): RenderElement
18
18
  cancel(): void
19
19
  }
20
20
 
21
21
  export type Installer = {
22
22
  answer(method: string, input: unknown): Promise<{ readonly value: unknown }> | undefined
23
- hear(shown: ShownPages): void
23
+ hear(shown: ShownParts): void
24
24
  options(missing: readonly (readonly [string, Option])[], save: (key: string, value: string) => Promise<string | undefined>): Promise<boolean>
25
25
  step<State extends object>(mod: Mod<State>, step: Step<State>, index: number, total: number): Promise<boolean>
26
26
  close(): Promise<void>
@@ -46,7 +46,7 @@ export function createInstaller(name: string, claude: () => Claude, waiting: { s
46
46
 
47
47
  const log = (what: string) => (error: unknown) => claude().ui.warn('installer:page', `${name}: ${what}: ${messageOf(error)}`)
48
48
 
49
- const open = (isAsked: boolean) => send({ requests: [{ open: installPage, focus: true }], ...(isAsked ? { isAsked: true } : {}) })
49
+ const open = (isAsked: boolean) => send({ requests: [{ open: installView, focus: true }], ...(isAsked ? { isAsked: true } : {}) })
50
50
 
51
51
  const show = async <Answer>(next: string, build: (answer: (value: Answer) => void) => Omit<Question, 'cancel'>, cancelled: Answer): Promise<Answer> => {
52
52
  question?.cancel()
@@ -82,10 +82,10 @@ export function createInstaller(name: string, claude: () => Claude, waiting: { s
82
82
 
83
83
  const dialog = (title: RenderElement, ...children: readonly RenderElement[]) => Box({ flexDirection: 'column', gap: 1, children: [title, ...children] })
84
84
 
85
- const draw = (columns: number, isFocused: boolean): DrawnPart => {
86
- if (question === undefined && !isOpen) claude().clock.after(0, () => void send({ requests: [{ close: installPage }] }).catch(log('a leftover install page stayed open')))
85
+ const draw = (props: ViewProps<undefined>): DrawnPart => {
86
+ if (question === undefined && !isOpen) claude().clock.after(0, () => void send({ requests: [{ close: installView }] }).catch(log('a leftover install view stayed open')))
87
87
  const handlers: Handler[] = []
88
- const tree = elementData(question?.draw({ params: undefined, isFocused, columns }) ?? Text({ dimColor: true, children: `${name} needs nothing from you now.` }), handlers)
88
+ const tree = elementData(question?.draw(props) ?? Text({ dimColor: true, children: `${name} needs nothing from you now.` }), (run) => handlers.push(run) - 1)
89
89
  const drawn = nextDraw()
90
90
  draws.set(drawn, handlers)
91
91
  for (const kept of draws.keys()) if (draws.size > keptDraws) draws.delete(kept)
@@ -112,12 +112,13 @@ export function createInstaller(name: string, claude: () => Claude, waiting: { s
112
112
 
113
113
  return {
114
114
  answer(method, input) {
115
- if (method === partMethods.draw && isObject(input) && input['page'] === installPage) return Promise.resolve({ value: draw(Number(input['columns']), input['isFocused'] === true) })
115
+ const { page, ...props } = (isObject(input) ? input : {}) as Readonly<Record<string, unknown>>
116
+ if (method === partMethods.draw && page === installView) return Promise.resolve({ value: draw({ ...(props as ViewProps<unknown>), params: undefined }) })
116
117
  if (method === partMethods.press && isObject(input) && typeof input['draw'] === 'number' && draws.has(input['draw'])) return press(input as PartPress).then((value) => ({ value }))
117
118
  return undefined
118
119
  },
119
120
  hear(shown) {
120
- if (shown.open.some((each) => each.id === installPage)) {
121
+ if (shown.open.some((each) => each.id === installView)) {
121
122
  isSeen = isOpen
122
123
  return
123
124
  }
@@ -206,7 +207,7 @@ export function createInstaller(name: string, claude: () => Claude, waiting: { s
206
207
  if (!isOpen) return
207
208
  isOpen = false
208
209
  isSeen = false
209
- await send({ requests: [{ close: installPage }] }).catch(log('the install page stayed open'))
210
+ await send({ requests: [{ close: installView }] }).catch(log('the install view stayed open'))
210
211
  },
211
212
  changed() {
212
213
  if (question !== undefined && isOpen) redraw()