@cmodjs/core 0.2.3 → 1.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 (273) hide show
  1. package/docs/commands.md +58 -10
  2. package/docs/dependencies.md +12 -12
  3. package/docs/hooks.md +41 -28
  4. package/docs/index.md +29 -25
  5. package/docs/install-steps.md +9 -7
  6. package/docs/jobs.md +59 -111
  7. package/docs/mod.md +200 -174
  8. package/docs/options.md +2 -2
  9. package/docs/permissions.md +27 -16
  10. package/docs/session.md +248 -0
  11. package/docs/state.md +11 -7
  12. package/docs/testing.md +66 -23
  13. package/docs/ui.md +108 -76
  14. package/jobs/check.d.ts.map +1 -1
  15. package/jobs/check.js +5 -2
  16. package/jobs/context.d.ts +7 -8
  17. package/jobs/context.d.ts.map +1 -1
  18. package/jobs/context.js +23 -73
  19. package/jobs/permissions/decide-permission.d.ts +5 -3
  20. package/jobs/permissions/decide-permission.d.ts.map +1 -1
  21. package/jobs/permissions/decide-permission.js +6 -4
  22. package/jobs/permissions/find-project-scope.d.ts +1 -1
  23. package/jobs/permissions/find-project-scope.d.ts.map +1 -1
  24. package/jobs/permissions/find-project-scope.js +8 -11
  25. package/jobs/permissions/match-target.d.ts +2 -2
  26. package/jobs/permissions/match-target.d.ts.map +1 -1
  27. package/jobs/permissions.d.ts +1 -1
  28. package/jobs/permissions.d.ts.map +1 -1
  29. package/jobs/permissions.js +6 -4
  30. package/jobs/program.d.ts.map +1 -1
  31. package/jobs/program.js +8 -3
  32. package/jobs/prompt.d.ts +4 -2
  33. package/jobs/prompt.d.ts.map +1 -1
  34. package/jobs/prompt.js +16 -15
  35. package/jobs/schedule.d.ts +2 -2
  36. package/jobs/schedule.d.ts.map +1 -1
  37. package/jobs/schedule.js +9 -9
  38. package/jobs/slash-command.d.ts +4 -4
  39. package/jobs/slash-command.d.ts.map +1 -1
  40. package/jobs/slash-command.js +14 -9
  41. package/jobs/status-line.d.ts +1 -11
  42. package/jobs/status-line.d.ts.map +1 -1
  43. package/jobs/status-line.js +8 -12
  44. package/jobs/tool.d.ts +2 -2
  45. package/jobs/tool.d.ts.map +1 -1
  46. package/jobs/tool.js +15 -9
  47. package/mod.d.ts +154 -75
  48. package/mod.d.ts.map +1 -1
  49. package/mod.js +94 -3
  50. package/options.d.ts +2 -2
  51. package/options.js +19 -19
  52. package/package.json +3 -3
  53. package/records.d.ts +10 -8
  54. package/records.d.ts.map +1 -1
  55. package/records.js +28 -17
  56. package/register.d.ts.map +1 -1
  57. package/register.js +18 -6
  58. package/runtime/announce.d.ts +18 -0
  59. package/runtime/announce.d.ts.map +1 -0
  60. package/runtime/announce.js +66 -0
  61. package/runtime/bound.d.ts +4 -0
  62. package/runtime/bound.d.ts.map +1 -0
  63. package/runtime/bound.js +9 -0
  64. package/runtime/claude.d.ts +3 -3
  65. package/runtime/claude.d.ts.map +1 -1
  66. package/runtime/conversations.d.ts +16 -0
  67. package/runtime/conversations.d.ts.map +1 -0
  68. package/runtime/conversations.js +291 -0
  69. package/runtime/deadline.d.ts +0 -1
  70. package/runtime/deadline.d.ts.map +1 -1
  71. package/runtime/deadline.js +1 -1
  72. package/runtime/dependencies.d.ts +2 -0
  73. package/runtime/dependencies.d.ts.map +1 -1
  74. package/runtime/dependencies.js +9 -0
  75. package/runtime/engine.d.ts +17 -0
  76. package/runtime/engine.d.ts.map +1 -0
  77. package/runtime/engine.js +10 -0
  78. package/runtime/grants.d.ts +4 -0
  79. package/runtime/grants.d.ts.map +1 -1
  80. package/runtime/grants.js +63 -33
  81. package/runtime/hooks.d.ts +5 -4
  82. package/runtime/hooks.d.ts.map +1 -1
  83. package/runtime/hooks.js +10 -5
  84. package/runtime/installer.d.ts.map +1 -1
  85. package/runtime/installer.js +11 -9
  86. package/runtime/lifecycle.d.ts +3 -1
  87. package/runtime/lifecycle.d.ts.map +1 -1
  88. package/runtime/lifecycle.js +317 -186
  89. package/runtime/metadata.d.ts +3 -1
  90. package/runtime/metadata.d.ts.map +1 -1
  91. package/runtime/metadata.js +21 -11
  92. package/runtime/options.d.ts +3 -1
  93. package/runtime/options.d.ts.map +1 -1
  94. package/runtime/options.js +4 -4
  95. package/runtime/programs.d.ts +8 -0
  96. package/runtime/programs.d.ts.map +1 -1
  97. package/runtime/programs.js +39 -1
  98. package/runtime/router.d.ts +2 -1
  99. package/runtime/router.d.ts.map +1 -1
  100. package/runtime/router.js +11 -3
  101. package/runtime/sessions.d.ts +36 -0
  102. package/runtime/sessions.d.ts.map +1 -0
  103. package/runtime/sessions.js +235 -0
  104. package/runtime/state.d.ts +6 -2
  105. package/runtime/state.d.ts.map +1 -1
  106. package/runtime/state.js +46 -13
  107. package/runtime/toaster.d.ts +31 -0
  108. package/runtime/toaster.d.ts.map +1 -0
  109. package/runtime/toaster.js +176 -0
  110. package/runtime/ui.d.ts +6 -17
  111. package/runtime/ui.d.ts.map +1 -1
  112. package/runtime/ui.js +36 -112
  113. package/session/context/context.d.ts +10 -0
  114. package/session/context/context.d.ts.map +1 -0
  115. package/session/context/context.js +2 -0
  116. package/session/context/turn/message/document.d.ts +7 -0
  117. package/session/context/turn/message/document.d.ts.map +1 -0
  118. package/session/context/turn/message/document.js +1 -0
  119. package/session/context/turn/message/message.d.ts +54 -0
  120. package/session/context/turn/message/message.d.ts.map +1 -0
  121. package/session/context/turn/message/message.js +137 -0
  122. package/session/context/turn/message/note.d.ts +8 -0
  123. package/session/context/turn/message/note.d.ts.map +1 -0
  124. package/session/context/turn/message/note.js +1 -0
  125. package/session/context/turn/message/notice.d.ts +6 -0
  126. package/session/context/turn/message/notice.d.ts.map +1 -0
  127. package/session/context/turn/message/notice.js +1 -0
  128. package/session/context/turn/message/prompt.d.ts +8 -0
  129. package/session/context/turn/message/prompt.d.ts.map +1 -0
  130. package/session/context/turn/message/prompt.js +1 -0
  131. package/session/context/turn/message/reply.d.ts +6 -0
  132. package/session/context/turn/message/reply.d.ts.map +1 -0
  133. package/session/context/turn/message/reply.js +1 -0
  134. package/session/context/turn/message/sender.d.ts +14 -0
  135. package/session/context/turn/message/sender.d.ts.map +1 -0
  136. package/session/context/turn/message/sender.js +30 -0
  137. package/session/context/turn/message/skill.d.ts +7 -0
  138. package/session/context/turn/message/skill.d.ts.map +1 -0
  139. package/session/context/turn/message/skill.js +1 -0
  140. package/session/context/turn/message/tool-call.d.ts +9 -0
  141. package/session/context/turn/message/tool-call.d.ts.map +1 -0
  142. package/session/context/turn/message/tool-call.js +1 -0
  143. package/session/context/turn/message/tool-result.d.ts +8 -0
  144. package/session/context/turn/message/tool-result.d.ts.map +1 -0
  145. package/session/context/turn/message/tool-result.js +1 -0
  146. package/session/context/turn/turn.d.ts +12 -0
  147. package/session/context/turn/turn.d.ts.map +1 -0
  148. package/session/context/turn/turn.js +2 -0
  149. package/session/conversation.d.ts +64 -0
  150. package/session/conversation.d.ts.map +1 -0
  151. package/session/conversation.js +123 -0
  152. package/session/goal/goal.d.ts +10 -0
  153. package/session/goal/goal.d.ts.map +1 -0
  154. package/session/goal/goal.js +1 -0
  155. package/session/goal/task.d.ts +16 -0
  156. package/session/goal/task.d.ts.map +1 -0
  157. package/session/goal/task.js +3 -0
  158. package/session/session.d.ts +99 -0
  159. package/session/session.d.ts.map +1 -0
  160. package/session/session.js +1 -0
  161. package/src/jobs/check.ts +7 -4
  162. package/src/jobs/context.ts +24 -82
  163. package/src/jobs/permissions/decide-permission.ts +10 -8
  164. package/src/jobs/permissions/find-project-scope.ts +8 -14
  165. package/src/jobs/permissions/match-target.ts +3 -3
  166. package/src/jobs/permissions.ts +8 -6
  167. package/src/jobs/program.ts +7 -3
  168. package/src/jobs/prompt.ts +21 -24
  169. package/src/jobs/schedule.ts +14 -14
  170. package/src/jobs/slash-command.ts +20 -14
  171. package/src/jobs/status-line.ts +9 -21
  172. package/src/jobs/tool.ts +17 -11
  173. package/src/mod.ts +209 -97
  174. package/src/options.ts +22 -22
  175. package/src/records.ts +35 -23
  176. package/src/register.ts +18 -6
  177. package/src/runtime/announce.ts +74 -0
  178. package/src/runtime/bound.ts +12 -0
  179. package/src/runtime/claude.ts +5 -3
  180. package/src/runtime/conversations.ts +302 -0
  181. package/src/runtime/deadline.ts +2 -2
  182. package/src/runtime/dependencies.ts +12 -0
  183. package/src/runtime/engine.ts +22 -0
  184. package/src/runtime/grants.ts +66 -31
  185. package/src/runtime/hooks.ts +19 -10
  186. package/src/runtime/installer.ts +10 -9
  187. package/src/runtime/lifecycle.ts +313 -181
  188. package/src/runtime/metadata.ts +33 -11
  189. package/src/runtime/options.ts +19 -4
  190. package/src/runtime/programs.ts +40 -1
  191. package/src/runtime/router.ts +11 -4
  192. package/src/runtime/sessions.ts +242 -0
  193. package/src/runtime/state.ts +50 -14
  194. package/src/runtime/toaster.ts +218 -0
  195. package/src/runtime/ui.ts +42 -139
  196. package/src/session/context/context.ts +12 -0
  197. package/src/session/context/turn/message/document.ts +8 -0
  198. package/src/session/context/turn/message/message.ts +165 -0
  199. package/src/session/context/turn/message/note.ts +9 -0
  200. package/src/session/context/turn/message/notice.ts +7 -0
  201. package/src/session/context/turn/message/prompt.ts +9 -0
  202. package/src/session/context/turn/message/reply.ts +7 -0
  203. package/src/session/context/turn/message/sender.ts +38 -0
  204. package/src/session/context/turn/message/skill.ts +8 -0
  205. package/src/session/context/turn/message/tool-call.ts +10 -0
  206. package/src/session/context/turn/message/tool-result.ts +9 -0
  207. package/src/session/context/turn/turn.ts +14 -0
  208. package/src/session/conversation.ts +169 -0
  209. package/src/session/goal/goal.ts +9 -0
  210. package/src/session/goal/task.ts +19 -0
  211. package/src/session/session.ts +94 -0
  212. package/src/testing/fake-claude.ts +23 -18
  213. package/src/testing/fake-elements.ts +3 -2
  214. package/src/testing/fake-files.ts +1 -1
  215. package/src/testing/fake-sessions.ts +203 -0
  216. package/src/testing.ts +112 -33
  217. package/src/ui/components.ts +3 -3
  218. package/src/ui/elements.ts +22 -8
  219. package/src/ui/markdown.ts +22 -26
  220. package/src/ui/places.ts +49 -0
  221. package/src/utils/call-effects.ts +3 -3
  222. package/src/utils/changelog.ts +32 -0
  223. package/src/utils/matcher.ts +37 -0
  224. package/src/utils/metadata.ts +17 -15
  225. package/src/utils/parse-shell.ts +24 -16
  226. package/src/utils/paths.ts +15 -1
  227. package/src/utils/text.ts +4 -0
  228. package/testing/fake-claude.d.ts +4 -2
  229. package/testing/fake-claude.d.ts.map +1 -1
  230. package/testing/fake-claude.js +18 -12
  231. package/testing/fake-elements.js +4 -2
  232. package/testing/fake-files.js +1 -1
  233. package/testing/fake-sessions.d.ts +74 -0
  234. package/testing/fake-sessions.d.ts.map +1 -0
  235. package/testing/fake-sessions.js +170 -0
  236. package/testing.d.ts +33 -13
  237. package/testing.d.ts.map +1 -1
  238. package/testing.js +92 -21
  239. package/ui/components.d.ts +2 -3
  240. package/ui/components.d.ts.map +1 -1
  241. package/ui/components.js +3 -3
  242. package/ui/elements.d.ts.map +1 -1
  243. package/ui/elements.js +19 -9
  244. package/ui/markdown.d.ts +35 -33
  245. package/ui/markdown.d.ts.map +1 -1
  246. package/ui/markdown.js +14 -16
  247. package/ui/places.d.ts +43 -0
  248. package/ui/places.d.ts.map +1 -0
  249. package/ui/places.js +31 -0
  250. package/utils/call-effects.d.ts +1 -1
  251. package/utils/call-effects.d.ts.map +1 -1
  252. package/utils/call-effects.js +2 -2
  253. package/utils/changelog.d.ts +9 -0
  254. package/utils/changelog.d.ts.map +1 -0
  255. package/utils/changelog.js +29 -0
  256. package/utils/matcher.d.ts +13 -0
  257. package/utils/matcher.d.ts.map +1 -0
  258. package/utils/matcher.js +32 -0
  259. package/utils/metadata.d.ts +3 -3
  260. package/utils/metadata.js +18 -14
  261. package/utils/parse-shell.d.ts +1 -0
  262. package/utils/parse-shell.d.ts.map +1 -1
  263. package/utils/parse-shell.js +22 -13
  264. package/utils/paths.d.ts +2 -0
  265. package/utils/paths.d.ts.map +1 -1
  266. package/utils/paths.js +13 -1
  267. package/utils/text.d.ts +1 -0
  268. package/utils/text.d.ts.map +1 -1
  269. package/utils/text.js +3 -0
  270. package/src/ui/slots.ts +0 -36
  271. package/ui/slots.d.ts +0 -208
  272. package/ui/slots.d.ts.map +0 -1
  273. package/ui/slots.js +0 -19
@@ -1,37 +1,37 @@
1
1
  import type { Job } from '../mod.js'
2
2
  import { onStateChange } from '../runtime/state.js'
3
- import { messageOf } from '../utils/text.js'
3
+ import { engineOf } from '../runtime/engine.js'
4
4
 
5
- export type Schedule = { readonly cron: string; readonly prompt: string }
5
+ export type ScheduledPrompt = { readonly cron: string; readonly prompt: string }
6
6
 
7
7
  const scheduleCounts = new WeakMap<object, number>()
8
8
 
9
- export function schedule<State extends object = Record<never, never>>(entry: Schedule | ((state: State) => Schedule | undefined)): Job<void, State> {
9
+ export function schedule<State extends object = Record<never, never>>(entry: ScheduledPrompt | ((state: State) => ScheduledPrompt | undefined)): Job<void, State> {
10
10
  if (typeof entry !== 'function') checked(entry)
11
11
 
12
12
  return (job) => {
13
13
  const { mod } = job
14
- const { claude } = mod
14
+ const { claude, report: reportFailure } = engineOf(mod)
15
15
  const order = scheduleCounts.get(mod) ?? 0
16
16
  scheduleCounts.set(mod, order + 1)
17
17
  const storeKey = `cmod:schedule:${order}`
18
- const log = (reason: string) => claude.ui.log(`${mod.name}: the schedule ${reason}`)
18
+ const report = (message: string, error: unknown) => reportFailure({ id: storeKey, message: `The schedule ${message}`, error })
19
19
  let current: { readonly id: string; readonly wanted: string } | undefined
20
20
  let isQueued = false
21
21
 
22
22
  const remove = async (id: string) => {
23
- await claude.tool.call({ tool: 'CronDelete', id }).catch(() => undefined)
23
+ await claude.tool.call({ tool: 'CronDelete', id }).catch((error: unknown) => report(`could not delete its last cron ${id}`, error))
24
24
  await claude.store.delete(storeKey)
25
25
  }
26
26
 
27
27
  const update = async () => {
28
28
  isQueued = false
29
- let next: Schedule | undefined
29
+ let next: ScheduledPrompt | undefined
30
30
  try {
31
- next = typeof entry === 'function' ? entry(mod.state as State) : entry
31
+ next = typeof entry === 'function' ? entry(mod.state) : entry
32
32
  if (next !== undefined) checked(next)
33
33
  } catch (error) {
34
- log(`kept its last cron: ${messageOf(error)}`)
34
+ report('kept its last cron', error)
35
35
  return
36
36
  }
37
37
  const wanted = next === undefined ? undefined : JSON.stringify([next.cron, next.prompt])
@@ -40,8 +40,8 @@ export function schedule<State extends object = Record<never, never>>(entry: Sch
40
40
  current = undefined
41
41
  if (next === undefined) return
42
42
  const answer = await claude.tool.call({ tool: 'CronCreate', cron: next.cron, prompt: next.prompt, recurring: true })
43
- if (answer.deny !== undefined) return log(`was refused: ${answer.deny}`)
44
- if (answer.isError === true) return log(`failed: ${answer.text ?? 'CronCreate answered an error'}`)
43
+ if (answer.deny !== undefined) return report('was refused', answer.deny)
44
+ if (answer.isError === true) return report('failed', answer.text ?? 'CronCreate answered an error')
45
45
  current = { id: answer.result.id, wanted: wanted as string }
46
46
  await claude.store.set(storeKey, current.id)
47
47
  }
@@ -50,17 +50,17 @@ export function schedule<State extends object = Record<never, never>>(entry: Sch
50
50
  .get(storeKey)
51
51
  .then((left) => (typeof left === 'string' ? remove(left) : undefined))
52
52
  .then(update)
53
- .catch((error: unknown) => log(`failed: ${messageOf(error)}`))
53
+ .catch((error: unknown) => report('failed', error))
54
54
  onStateChange(mod.state, () => {
55
55
  if (isQueued) return
56
56
  isQueued = true
57
- updates = updates.then(update).catch((error: unknown) => log(`failed: ${messageOf(error)}`))
57
+ updates = updates.then(update).catch((error: unknown) => report('failed', error))
58
58
  })
59
59
  job.announce(typeof entry === 'function' ? 'a schedule from its state' : `a schedule at ${entry.cron}`)
60
60
  }
61
61
  }
62
62
 
63
- function checked(entry: Schedule): void {
63
+ function checked(entry: ScheduledPrompt): void {
64
64
  if (entry.cron.trim() === '') throw new Error('schedule: give it a cron, such as "*/10 * * * *" for every 10 minutes.')
65
65
  if (entry.prompt.trim() === '') throw new Error('schedule: give it the prompt Claude gets at each fire.')
66
66
  }
@@ -1,13 +1,14 @@
1
1
  import type { CommandRunResult } from 'claude-code'
2
- import type { Job, Mod } from '../mod.js'
2
+ import type { Disposable, Job, Mod } from '../mod.js'
3
3
  import { parse } from '../vendor.js'
4
- import { longestMs, type Deadline } from '../runtime/deadline.js'
4
+ import type { Deadline } from '../runtime/deadline.js'
5
+ import { engineOf } from '../runtime/engine.js'
5
6
  import { messageOf } from '../utils/text.js'
6
- import { modWithin } from './context.js'
7
+ import { withinDeadline } from './context.js'
7
8
 
8
- export type Reply = string | { text?: string; context?: string } | undefined
9
+ export type CommandReply = string | { text?: string; context?: string } | undefined
9
10
 
10
- const deadline: Deadline = { ms: 30000, longestMs, job: 'slashCommand' }
11
+ const deadline: Deadline = { ms: 30000, job: 'slashCommand' }
11
12
 
12
13
  const commandName = /^[A-Za-z0-9_-]{1,64}$/
13
14
 
@@ -16,14 +17,14 @@ export function slashCommand<State extends object = Record<never, never>>(option
16
17
  readonly description: string
17
18
  readonly argumentHint?: string
18
19
  readonly immediate?: true
19
- readonly reply: (input: { args: string; positionals: string[] }, mod: Mod<State>) => Reply | void | Promise<Reply | void>
20
- }): Job<void, State> {
20
+ readonly reply: (input: { args: string; positionals: string[] }, mod: Mod<State>) => CommandReply | void | Promise<CommandReply | void>
21
+ }): Job<Disposable, State> {
21
22
  const { name, description, argumentHint, immediate, reply } = options
22
23
  return (job) => {
23
24
  const { mod, announce, reserveName } = job
24
- const { claude } = mod
25
+ const { claude, router, report } = engineOf(mod)
25
26
  if (!commandName.test(name)) throw new Error(`${mod.name}: "${name}" is not a slash command name. Use 1 to 64 letters, digits, "_", or "-", without the slash.`)
26
- reserveName('slashCommand', name, `${mod.name}: the slash command /${name} is already added. Give each slashCommand its own name.`)
27
+ const reserved = reserveName('slashCommand', name, `${mod.name}: the slash command /${name} is already added. Give each slashCommand its own name.`)
27
28
 
28
29
  let registered: string | undefined
29
30
  claude.command.register({ name, description, ...(argumentHint === undefined ? {} : { argumentHint }), ...(immediate === undefined ? {} : { immediate }) }).then(
@@ -31,24 +32,29 @@ export function slashCommand<State extends object = Record<never, never>>(option
31
32
  registered = command
32
33
  },
33
34
  (error: unknown) => {
34
- claude.ui.log(`${mod.name}: /${name} is not added: ${oneLine(error)}`)
35
+ report({ id: `command:${name}`, message: `/${name} is not added`, error })
35
36
  },
36
37
  )
37
38
  announce(`/${name}`)
38
39
 
39
- const replyMod = modWithin(job, deadline)
40
- claude.on('command.run', async (e, next) => {
40
+ const answering = router.add('command.run', async (e, next) => {
41
41
  if (e.command !== registered) return next(e)
42
42
  try {
43
- return answerOf(await reply({ args: e.args, positionals: splitWords(e.args) }, replyMod))
43
+ return answerOf(await withinDeadline(mod, deadline, `/${name}`, () => reply({ args: e.args, positionals: splitWords(e.args) }, mod)))
44
44
  } catch (error) {
45
45
  return { text: `/${name} failed: ${oneLine(error)}` }
46
46
  }
47
47
  })
48
+ return {
49
+ dispose() {
50
+ answering.dispose()
51
+ reserved.dispose()
52
+ },
53
+ }
48
54
  }
49
55
  }
50
56
 
51
- function answerOf(reply: Reply | void): CommandRunResult {
57
+ function answerOf(reply: CommandReply | void): CommandRunResult {
52
58
  if (reply === undefined) return {}
53
59
  if (typeof reply === 'string') return { text: reply }
54
60
  return { ...(reply.text === undefined ? {} : { text: reply.text }), ...(reply.context === undefined ? {} : { context: [reply.context] }) }
@@ -1,13 +1,10 @@
1
1
  import type { Job, Mod } from '../mod.js'
2
2
  import type { Deadline } from '../runtime/deadline.js'
3
+ import { engineOf } from '../runtime/engine.js'
4
+ import { usageOf } from '../runtime/sessions.js'
5
+ import type { Usage } from '../session/session.js'
3
6
  import { messageOf } from '../utils/text.js'
4
- import { modWithin } from './context.js'
5
-
6
- export type Usage = {
7
- model: string
8
- context: { tokens?: number; window: number; percent?: number }
9
- cost?: { usd: number }
10
- }
7
+ import { withinDeadline } from './context.js'
11
8
 
12
9
  const deadline: Deadline = { ms: 2000, job: 'statusLine' }
13
10
 
@@ -18,30 +15,21 @@ export function statusLine<State extends object = Record<never, never>>(options:
18
15
  const { text, interval = 10000 } = options
19
16
  return (job) => {
20
17
  const { mod, announce, reserveName } = job
21
- const { claude } = mod
18
+ const { claude, router } = engineOf(mod)
22
19
  if (!(interval >= 1)) throw new Error(`${mod.name}: the status line interval is ${interval}. Give it in milliseconds, 1 or more.`)
23
20
  reserveName('statusLine', '', `${mod.name}: a status line is already added. A mod has one status line: join the texts in one statusLine.`)
24
21
 
25
- const textMod = modWithin(job, deadline)
26
22
  let shown: string | undefined
27
23
  let queue = Promise.resolve()
28
24
  const update = async () => {
29
25
  try {
30
- const [model, usage] = await Promise.all([claude.session.model(), claude.session.usage()])
31
- const { tokens, window, percent } = usage.context
32
- const next = await text(
33
- {
34
- model,
35
- context: { window, ...(tokens === undefined ? {} : { tokens }), ...(percent === undefined ? {} : { percent }) },
36
- ...(usage.cost === undefined ? {} : { cost: { usd: usage.cost.usd } }),
37
- },
38
- textMod,
39
- )
26
+ const usage = await usageOf(claude)
27
+ const next = await withinDeadline(mod, deadline, 'the status line', () => text(usage, mod))
40
28
  if (next === shown) return
41
29
  shown = next
42
30
  claude.ui.status(next)
43
31
  } catch (error) {
44
- claude.ui.log(`${mod.name}: the status line kept its last text: ${messageOf(error)}`, { to: 'debug' })
32
+ mod.log(`the status line kept its last text: ${messageOf(error)}`)
45
33
  }
46
34
  }
47
35
  const refresh = () => {
@@ -49,7 +37,7 @@ export function statusLine<State extends object = Record<never, never>>(options:
49
37
  }
50
38
 
51
39
  announce('a status line')
52
- claude.on('session.measure', (e, next) => {
40
+ router.add('session.measure', (e, next) => {
53
41
  refresh()
54
42
  return next(e)
55
43
  })
package/src/jobs/tool.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import type { FromSchema, JSONSchema } from 'json-schema-to-ts'
2
- import type { Job, Mod } from '../mod.js'
2
+ import type { Disposable, Job, Mod } from '../mod.js'
3
3
  import { Validator } from '../vendor.js'
4
4
  import { toolDeadline } from '../runtime/deadline.js'
5
+ import { engineOf } from '../runtime/engine.js'
5
6
  import { reservedKeys, toolInputOf } from '../runtime/tool-calls.js'
6
7
  import { messageOf } from '../utils/text.js'
7
- import { modWithin } from './context.js'
8
+ import { withinDeadline } from './context.js'
8
9
 
9
10
  const toolName = /^[A-Za-z0-9_-]{1,64}$/
10
11
 
@@ -13,42 +14,47 @@ export function tool<const Schema extends object = { type: 'object' }, State ext
13
14
  readonly description: string
14
15
  readonly inputSchema?: Schema
15
16
  readonly execute: (input: Schema extends JSONSchema ? FromSchema<Schema> : never, mod: Mod<State>) => unknown
16
- }): Job<{ readonly name: string }, State> {
17
+ }): Job<Disposable & { readonly name: string }, State> {
17
18
  const { name, description, inputSchema, execute } = options
18
19
  return (job) => {
19
20
  const { mod, announce, reserveName } = job
20
- const { claude } = mod
21
+ const { claude, router, report } = engineOf(mod)
21
22
  if (!toolName.test(name)) throw new Error(`${mod.name}: "${name}" is not a tool name. Use 1 to 64 letters, digits, "_", or "-".`)
22
23
  const properties = (inputSchema as { properties?: object } | undefined)?.properties ?? {}
23
24
  const reserved = reservedKeys.filter((key) => Object.hasOwn(properties, key))
24
25
  if (reserved.length > 0) throw new Error(`${mod.name}: the ${name} tool's inputSchema has the property ${reserved.join(', ')}, which Claude Code keeps for itself. Rename it.`)
25
- reserveName('tool', name, `${mod.name}: the tool ${name} is already added. Give each tool its own name.`)
26
+ const named = reserveName('tool', name, `${mod.name}: the tool ${name} is already added. Give each tool its own name.`)
26
27
 
27
- const validator = new Validator((inputSchema ?? { type: 'object' }) as ConstructorParameters<typeof Validator>[0], '2020-12', false)
28
+ const validator = new Validator(inputSchema ?? { type: 'object' }, '2020-12', false)
28
29
  let registered: string | undefined
29
30
  claude.tool.register({ name, description, ...(inputSchema === undefined ? {} : { inputSchema: inputSchema as Record<string, unknown> }) }).then(
30
31
  (answer) => {
31
32
  registered = answer.tool
32
33
  },
33
34
  (error: unknown) => {
34
- claude.ui.log(`${mod.name}: the ${name} tool is not added: ${messageOf(error)}`)
35
+ report({ id: `tool:${name}`, message: `The ${name} tool is not added`, error })
35
36
  },
36
37
  )
37
38
  announce(`the ${name} tool`)
38
39
 
39
- const executeMod = modWithin(job, toolDeadline)
40
- claude.on('tool.call', async (e, next) => {
40
+ const answering = router.add('tool.call', async (e, next) => {
41
41
  if (e.tool !== registered) return next(e)
42
42
  const input = toolInputOf(e)
43
43
  const { valid, errors } = validator.validate(input)
44
44
  if (!valid) return { deny: `The ${name} tool input is not valid:\n${errors.map((error) => `- ${error.instanceLocation}: ${error.error}`).join('\n')}` }
45
45
  try {
46
- return { result: resultOf(await execute(input as Parameters<typeof execute>[0], executeMod)) }
46
+ return { result: resultOf(await withinDeadline(mod, toolDeadline, `the ${name} tool`, () => execute(input as Parameters<typeof execute>[0], mod))) }
47
47
  } catch (error) {
48
48
  return { deny: `The ${name} tool failed: ${messageOf(error)}` }
49
49
  }
50
50
  })
51
- return { name: `mcp__${claude.plugin.name}__${name}` }
51
+ return {
52
+ name: `mcp__${claude.plugin.name}__${name}`,
53
+ dispose() {
54
+ answering.dispose()
55
+ named.dispose()
56
+ },
57
+ }
52
58
  }
53
59
  }
54
60