1 import Base 2 import bend-net-json@0.3.0.0/json.bend as Json 3 import ../json.bend as J 4 import ./type.bend as Cx 5 6 # Codex's part of a plugin, as the object under extensions."com.openai" in the root plugin.json. 7 8 def event_name(e: Cx.HookEvent) -> String: 9 match e: 10 case Cx.SessionStart{}: 11 "SessionStart" 12 case Cx.SessionEnd{}: 13 "SessionEnd" 14 case Cx.PreToolUse{}: 15 "PreToolUse" 16 case Cx.PostToolUse{}: 17 "PostToolUse" 18 case Cx.PermissionRequest{}: 19 "PermissionRequest" 20 case Cx.PreCompact{}: 21 "PreCompact" 22 case Cx.PostCompact{}: 23 "PostCompact" 24 case Cx.UserPromptSubmit{}: 25 "UserPromptSubmit" 26 case Cx.SubagentStart{}: 27 "SubagentStart" 28 case Cx.SubagentStop{}: 29 "SubagentStop" 30 case Cx.Stop{}: 31 "Stop" 32 case Cx.Interrupt{}: 33 "Interrupt" 34 35 def handler(h: Cx.Handler) -> Json.Val: 36 match h: 37 case Cx.Command{command, windows, timeout, status, background, limit}: 38 J.cat([[("type", Json.Str{"command"}), ("command", Json.Str{command})], J.opt("commandWindows", J.text(windows)), 39 J.opt("timeout", J.number(timeout)), J.opt("statusMessage", J.text(status)), J.when_true("async", background), 40 J.opt("additionalContextLimit", J.number(limit))]) 41 42 def handlers(hs: List<&2, Cx.Handler>) -> List<&2, Json.Val>: 43 match hs: 44 case []: 45 [] 46 case h <> rest: 47 handler(h) <> handlers(rest) 48 49 def group(g: Cx.Group) -> Json.Val: 50 match g: 51 case Cx.Group{m, hs}: 52 J.cat([J.opt("matcher", J.text(m)), [("hooks", Json.Arr{handlers(hs)})]]) 53 54 def groups(gs: List<&2, Cx.Group>) -> List<&2, Json.Val>: 55 match gs: 56 case []: 57 [] 58 case g <> rest: 59 group(g) <> groups(rest) 60 61 def hooks(hs: List<&2, Cx.Hook>) -> List<&2, Sigma<&2, &2, String, _ => Json.Val>>: 62 match hs: 63 case []: 64 [] 65 case Cx.Hook{e, gs} <> rest: 66 (event_name(e), Json.Arr{groups(gs)}) <> hooks(rest) 67 68 def hooks_key(hs: List<&2, Cx.Hook>) -> List<&2, Sigma<&2, &2, String, _ => Json.Val>>: 69 match hs: 70 case []: 71 [] 72 case h <> rest: 73 [("hooks", J.obj(hooks(h <> rest)))] 74 75 def keys(c: Cx.Codex) -> List<&2, Sigma<&2, &2, String, _ => Json.Val>>: 76 match c: 77 case Cx.Codex{hs}: 78 hooks_key(hs)