MCPcopy Create free account
hub / github.com/agentforce314/clawcodex / handler

Function handler

ui-tui/src/app/createSlashHandler.ts:15–149  ·  view source on GitHub ↗
(cmd: string)

Source from the content-addressed store, hash-verified

13 const { page, send, sys } = ctx.transcript
14
15 const handler = (cmd: string): boolean => {
16 const flight = ++ctx.slashFlightRef.current
17 const ui = getUiState()
18 const sid = ui.sid
19 const parsed = parseSlashCommand(cmd)
20 const argTail = parsed.arg ? ` ${parsed.arg}` : ''
21
22 const stale = () => flight !== ctx.slashFlightRef.current || getUiState().sid !== sid
23
24 const guarded =
25 <T>(fn: (r: T) => void) =>
26 (r: null | T): void => {
27 if (!stale() && r) {
28 fn(r)
29 }
30 }
31
32 const guardedErr = (e: unknown) => {
33 if (!stale()) {
34 sys(`error: ${rpcErrorMessage(e)}`)
35 }
36 }
37
38 const runCtx: SlashRunCtx = { ...ctx, flight, guarded, guardedErr, sid, stale, ui }
39
40 const found = findSlashCommand(parsed.name)
41
42 if (found) {
43 found.run(parsed.arg, runCtx, cmd)
44
45 return true
46 }
47
48 if (catalog?.canon) {
49 const needle = `/${parsed.name}`.toLowerCase()
50 const exact = Object.entries(catalog.canon).find(([alias]) => alias.toLowerCase() === needle)?.[1]
51
52 if (exact) {
53 if (exact.toLowerCase() !== needle) {
54 return handler(`${exact}${argTail}`)
55 }
56 } else {
57 const matches = [
58 ...new Set(
59 Object.entries(catalog.canon)
60 .filter(([alias]) => alias.startsWith(needle))
61 .map(([, canon]) => canon)
62 )
63 ]
64
65 if (matches.length === 1 && matches[0]!.toLowerCase() !== needle) {
66 return handler(`${matches[0]}${argTail}`)
67 }
68
69 if (matches.length > 1) {
70 sys(`ambiguous command: ${matches.slice(0, 6).join(', ')}${matches.length > 6 ? ', …' : ''}`)
71
72 return true

Callers 1

handleDispatchFunction · 0.70

Calls 10

getUiStateFunction · 0.85
parseSlashCommandFunction · 0.85
findSlashCommandFunction · 0.85
staleFunction · 0.85
asCommandDispatchFunction · 0.85
handleDispatchFunction · 0.85
joinMethod · 0.80
runMethod · 0.45
entriesMethod · 0.45
requestMethod · 0.45

Tested by

no test coverage detected