domain/architecture/system/release/plugin/codex/ops.bend

near-v-framework@0.5.0.0 · 78 lines · 0 definitions

1import Base
2import bend-net-json@0.3.0.0/json.bend as Json
3import near-v-framework@0.5.0.0/domain/architecture/system/release/plugin/json.bend as J
4import near-v-framework@0.5.0.0/domain/architecture/system/release/plugin/codex/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
8def 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
35def 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
42def 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
49def 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
54def 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
61def 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
68def 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
75def keys(c: Cx.Codex) -> List<&2, Sigma<&2, &2, String, _ => Json.Val>>:
76  match c:
77    case Cx.Codex{hs}:
78      hooks_key(hs)