omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / core / sovereign-core / om / serve / cli.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/sovereign-core/om/serve/cli.om |
| 3 | note Pure OM-2 conversion of aien-cli subcommand & flag dispatcher. |
| 4 | note Parses subcommands (serve, chat, models, health, version) and validates flags |
| 5 | note (--port 1..65535, --max-tokens 1..4096, --model NAME) fail-closed. |
| 6 | |
| 7 | const CLI_OK ulong 0 |
| 8 | const CLI_ERR_EMPTY ulong 1 |
| 9 | const CLI_ERR_UNKNOWN_CMD ulong 2 |
| 10 | const CLI_ERR_UNKNOWN_FLAG ulong 3 |
| 11 | const CLI_ERR_MISSING_VAL ulong 4 |
| 12 | const CLI_ERR_BAD_PORT ulong 5 |
| 13 | const CLI_ERR_BAD_MAX_TOKENS ulong 6 |
| 14 | const CLI_ERR_MISSING_MODEL ulong 7 |
| 15 | const CLI_ERR_DUP_FLAG ulong 8 |
| 16 | const CLI_ERR_TOO_MANY_ARGS ulong 9 |
| 17 | |
| 18 | const CLI_CMD_NONE ulong 0 |
| 19 | const CLI_CMD_SERVE ulong 1 |
| 20 | const CLI_CMD_CHAT ulong 2 |
| 21 | const CLI_CMD_MODELS ulong 3 |
| 22 | const CLI_CMD_HEALTH ulong 4 |
| 23 | const CLI_CMD_VERSION ulong 5 |
| 24 | |
| 25 | note Parsed CLI state: |
| 26 | note [0]=cmd, [1]=port, [2]=max_tokens, [3]=has_port, [4]=has_max_tokens, [5]=has_model, [6]=model_len, [7]=argc |
| 27 | global cli_state ulong 8 |
| 28 | global cli_model uchar 64 |
| 29 | global cli_arg_off ulong 16 |
| 30 | global cli_arg_len ulong 16 |
| 31 | |
| 32 | text cli_s_serve "serve" |
| 33 | text cli_s_chat "chat" |
| 34 | text cli_s_models "models" |
| 35 | text cli_s_health "health" |
| 36 | text cli_s_version "version" |
| 37 | text cli_f_port "--port" |
| 38 | text cli_f_maxt "--max-tokens" |
| 39 | text cli_f_model "--model" |
| 40 | |
| 41 | fn cli_reset to void |
| 42 | for i ulong 8 |
| 43 | put cli_state i 0 |
| 44 | end |
| 45 | put cli_state 1 8080 |
| 46 | put cli_state 2 512 |
| 47 | for i ulong 64 |
| 48 | put cli_model i uchar 0 |
| 49 | end |
| 50 | for i ulong 16 |
| 51 | put cli_arg_off i 0 |
| 52 | put cli_arg_len i 0 |
| 53 | end |
| 54 | end |
| 55 | |
| 56 | fn cli_cmd to ulong |
| 57 | ret get cli_state 0 |
| 58 | end |
| 59 | |
| 60 | fn cli_port to ulong |
| 61 | ret get cli_state 1 |
| 62 | end |
| 63 | |
| 64 | fn cli_max_tokens to ulong |
| 65 | ret get cli_state 2 |
| 66 | end |
| 67 | |
| 68 | fn cli_model_len to ulong |
| 69 | ret get cli_state 6 |
| 70 | end |
| 71 | |
| 72 | fn cli_tok_eq line view uchar * idx ulong lit view uchar * to bool |
| 73 | off = get cli_arg_off idx |
| 74 | tlen = get cli_arg_len idx |
| 75 | if ne tlen len lit |
| 76 | ret false |
| 77 | end |
| 78 | for i ulong tlen bound 64 |
| 79 | if ne get line add off i get lit i |
| 80 | ret false |
| 81 | end |
| 82 | end |
| 83 | ret true |
| 84 | end |
| 85 | |
| 86 | fn cli_tok_u64 line view uchar * idx ulong to ulong |
| 87 | off = get cli_arg_off idx |
| 88 | tlen = get cli_arg_len idx |
| 89 | if or eq tlen 0 gt tlen 10 |
| 90 | ret 18446744073709551615 |
| 91 | end |
| 92 | acc = ulong 0 |
| 93 | for i ulong tlen bound 16 |
| 94 | b = get line add off i |
| 95 | if or lt b uchar 48 gt b uchar 57 |
| 96 | ret 18446744073709551615 |
| 97 | end |
| 98 | acc = add mul acc 10 ulong sub b uchar 48 |
| 99 | end |
| 100 | ret acc |
| 101 | end |
| 102 | |
| 103 | note Split whitespace-delimited arguments into cli_arg_off / cli_arg_len. |
| 104 | fn cli_tokenize line view uchar * to ulong |
| 105 | n = len line |
| 106 | argc = ulong 0 |
| 107 | i = ulong 0 |
| 108 | while lt i n bound 512 |
| 109 | while lt i n bound 512 |
| 110 | b = get line i |
| 111 | if or eq b uchar 32 eq b uchar 9 |
| 112 | i = add i 1 |
| 113 | else |
| 114 | break |
| 115 | end |
| 116 | end |
| 117 | if ge i n |
| 118 | break |
| 119 | end |
| 120 | if ge argc 16 |
| 121 | ret CLI_ERR_TOO_MANY_ARGS |
| 122 | end |
| 123 | start = i |
| 124 | while lt i n bound 512 |
| 125 | b = get line i |
| 126 | if or eq b uchar 32 eq b uchar 9 |
| 127 | break |
| 128 | end |
| 129 | i = add i 1 |
| 130 | end |
| 131 | put cli_arg_off argc start |
| 132 | put cli_arg_len argc sub i start |
| 133 | argc = add argc 1 |
| 134 | end |
| 135 | put cli_state 7 argc |
| 136 | ret CLI_OK |
| 137 | end |
| 138 | |
| 139 | note Parse and validate a CLI command line fail-closed. |
| 140 | fn cli_dispatch line view uchar * to ulong |
| 141 | cli_reset |
| 142 | tst = cli_tokenize line |
| 143 | if ne tst CLI_OK |
| 144 | ret tst |
| 145 | end |
| 146 | argc = get cli_state 7 |
| 147 | if eq argc 0 |
| 148 | ret CLI_ERR_EMPTY |
| 149 | end |
| 150 | cmd = CLI_CMD_NONE |
| 151 | if cli_tok_eq line 0 cli_s_serve |
| 152 | cmd = CLI_CMD_SERVE |
| 153 | else |
| 154 | if cli_tok_eq line 0 cli_s_chat |
| 155 | cmd = CLI_CMD_CHAT |
| 156 | else |
| 157 | if cli_tok_eq line 0 cli_s_models |
| 158 | cmd = CLI_CMD_MODELS |
| 159 | else |
| 160 | if cli_tok_eq line 0 cli_s_health |
| 161 | cmd = CLI_CMD_HEALTH |
| 162 | else |
| 163 | if cli_tok_eq line 0 cli_s_version |
| 164 | cmd = CLI_CMD_VERSION |
| 165 | end |
| 166 | end |
| 167 | end |
| 168 | end |
| 169 | end |
| 170 | if eq cmd CLI_CMD_NONE |
| 171 | ret CLI_ERR_UNKNOWN_CMD |
| 172 | end |
| 173 | put cli_state 0 cmd |
| 174 | idx = ulong 1 |
| 175 | while lt idx argc bound 16 |
| 176 | if cli_tok_eq line idx cli_f_port |
| 177 | if ne get cli_state 3 0 |
| 178 | ret CLI_ERR_DUP_FLAG |
| 179 | end |
| 180 | if ge add idx 1 argc |
| 181 | ret CLI_ERR_MISSING_VAL |
| 182 | end |
| 183 | p = cli_tok_u64 line add idx 1 |
| 184 | if or eq p 0 or gt p 65535 eq p 18446744073709551615 |
| 185 | ret CLI_ERR_BAD_PORT |
| 186 | end |
| 187 | put cli_state 1 p |
| 188 | put cli_state 3 1 |
| 189 | idx = add idx 2 |
| 190 | continue |
| 191 | end |
| 192 | if cli_tok_eq line idx cli_f_maxt |
| 193 | if ne get cli_state 4 0 |
| 194 | ret CLI_ERR_DUP_FLAG |
| 195 | end |
| 196 | if ge add idx 1 argc |
| 197 | ret CLI_ERR_MISSING_VAL |
| 198 | end |
| 199 | mt = cli_tok_u64 line add idx 1 |
| 200 | if or eq mt 0 or gt mt 4096 eq mt 18446744073709551615 |
| 201 | ret CLI_ERR_BAD_MAX_TOKENS |
| 202 | end |
| 203 | put cli_state 2 mt |
| 204 | put cli_state 4 1 |
| 205 | idx = add idx 2 |
| 206 | continue |
| 207 | end |
| 208 | if cli_tok_eq line idx cli_f_model |
| 209 | if ne get cli_state 5 0 |
| 210 | ret CLI_ERR_DUP_FLAG |
| 211 | end |
| 212 | if ge add idx 1 argc |
| 213 | ret CLI_ERR_MISSING_VAL |
| 214 | end |
| 215 | moff = get cli_arg_off add idx 1 |
| 216 | mlen = get cli_arg_len add idx 1 |
| 217 | if or eq mlen 0 gt mlen 64 |
| 218 | ret CLI_ERR_MISSING_MODEL |
| 219 | end |
| 220 | for k ulong mlen bound 64 |
| 221 | put cli_model k get line add moff k |
| 222 | end |
| 223 | put cli_state 5 1 |
| 224 | put cli_state 6 mlen |
| 225 | idx = add idx 2 |
| 226 | continue |
| 227 | end |
| 228 | ret CLI_ERR_UNKNOWN_FLAG |
| 229 | end |
| 230 | if and eq cmd CLI_CMD_CHAT eq get cli_state 5 0 |
| 231 | ret CLI_ERR_MISSING_MODEL |
| 232 | end |
| 233 | ret CLI_OK |
| 234 | end |