interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / Linux / chat.om

843 lines · 15302 bytes Raw Blame History
1note Linux Ask. Model endpoint and model are explicit HOP_MODEL_URL and HOP_MODEL settings.
2note curl is the platform HTTP/TLS transport; build/linux/dependency-curl.txt records its exact version.
3note Successor: Omega om/tls client plus native DNS/HTTP transport. No shell or host language runtime.
4note curl reads URL, request and optional API key from anonymous memory on stdin, never argv or a disk file.
5note No redirects, ambient proxy, curlrc, cookies, retries or TLS verification overrides.
6note Ask is synchronous: the window waits for the configured server. The transport deadline is 60 seconds.
7global hc_cfg uchar *
8global hc_json uchar *
9global hc_str uchar *
10global hc_body uchar *
11global hc_url uchar *
12global hc_model uchar *
13global hc_key uchar *
14global hc_ans uchar *
15global hc_n ulong 8
16global hc_pos ulong 1
17global hc_found ulong 1
18global hc_http ulong 1
19global hc_env uchar 131072
20global hc_io uchar 8192
21global hc_pipe uchar 16
22global hc_wait uchar 4
23global hc_exec uchar 128
24text hc_uk "HOP_MODEL_URL="
25text hc_mk "HOP_MODEL="
26text hc_kk "HOP_API_KEY="
27text hc_h "http://"
28text hc_hs "https://"
29text hc_tail "/chat/completions"
30text hc_curl "/usr/bin/curl"
31text hc_q "-q"
32text hc_config "--config"
33text hc_dash "-"
34text hc_memname "hop-model-request"
35text hc_ct "silent\nshow-error\nfail-with-body\ngloboff\nconnect-timeout = 10\nmax-time = 60\nproto = \"=http,https\"\nproxy = \"\"\nrequest = \"POST\"\nheader = \"Content-Type: application/json\"\nurl = \""
36text hc_auth "\"\nheader = \"Authorization: Bearer "
37text hc_data "\"\ndata-binary = \""
38text hc_end "\"\nwrite-out = \"\\n%{http_code}\"\n"
39text hc_b1 "{\"model\":\""
40text hc_b2 "\",\"messages\":[{\"role\":\"user\",\"content\":\""
41text hc_b3 "\"}],\"stream\":false}"
42text hc_choices "choices"
43text hc_message "message"
44text hc_content "content"
45text hc_true "true"
46text hc_false "false"
47text hc_null "null"
48text hc_need "Ask needs a model. Set HOP_MODEL_URL to your OpenAI-compatible server's base address and HOP_MODEL to its model name."
49text hc_badurl "Ask: use an http:// or https:// model base address without control characters, query or fragment."
50text hc_badkey "Ask: HOP_API_KEY contains a control character and cannot be used as a request header."
51text hc_transport "Ask: the model request failed. Check the server address, model name, sign-in and network. curl with HTTPS support is required."
52text hc_server "Ask: the server returned HTTP "
53text hc_serverend ". Check the model settings and sign-in."
54text hc_badjson "Ask: the server did not return a valid chat answer (choices[0].message.content must be text)."
55text hc_mem "Hop Ask: the system could not allocate memory.\n"
56text hc_action "Ask: "
57
58fn hc_res which ulong need ulong to void
59r = long 0
60if eq which 0
61if gt need len hc_cfg
62r = enlarge hc_cfg add need add len hc_cfg 256
63end
64end
65if eq which 1
66if gt need len hc_json
67r = enlarge hc_json add need add len hc_json 8192
68end
69end
70if eq which 2
71if gt need len hc_str
72r = enlarge hc_str add need add len hc_str 256
73end
74end
75if eq which 3
76if gt need len hc_body
77r = enlarge hc_body add need add len hc_body 256
78end
79end
80if eq which 4
81if gt need len hc_url
82r = enlarge hc_url add need add len hc_url 256
83end
84end
85if eq which 5
86if gt need len hc_model
87r = enlarge hc_model add need add len hc_model 256
88end
89end
90if eq which 6
91if gt need len hc_key
92r = enlarge hc_key add need add len hc_key 256
93end
94end
95if eq which 7
96if gt need len hc_ans
97r = enlarge hc_ans add need add len hc_ans 256
98end
99end
100if lt r 0
101x = write 2 hc_mem 0 len hc_mem
102exit 1
103end
104end
105
106fn hc_add which ulong c uchar to void
107p = get hc_n which
108hc_res which add p 1
109if eq which 0
110put hc_cfg p c
111end
112if eq which 1
113put hc_json p c
114end
115if eq which 2
116put hc_str p c
117end
118if eq which 3
119put hc_body p c
120end
121if eq which 4
122put hc_url p c
123end
124if eq which 5
125put hc_model p c
126end
127if eq which 6
128put hc_key p c
129end
130if eq which 7
131put hc_ans p c
132end
133put hc_n which add p 1
134end
135
136fn hc_get which ulong p ulong to uchar
137if eq which 0
138ret get hc_cfg p
139end
140if eq which 1
141ret get hc_json p
142end
143if eq which 2
144ret get hc_str p
145end
146if eq which 3
147ret get hc_body p
148end
149if eq which 4
150ret get hc_url p
151end
152if eq which 5
153ret get hc_model p
154end
155if eq which 6
156ret get hc_key p
157end
158ret get hc_ans p
159end
160
161fn hc_text which ulong s view uchar * to void
162for i ulong len s bound 9223372036854775807
163hc_add which get s i
164end
165end
166
167fn hc_rows s view uchar * to void
168r = enlarge lx_rows len s
169if lt r 0
170x = write 2 hc_mem 0 len hc_mem
171exit 1
172end
173for i ulong len s bound 9223372036854775807
174put lx_rows i get s i
175end
176put lx_rn 0 len s
177put lx_pick 0 0
178end
179
180fn hc_envget key view uchar * which ulong to bool
181put hc_n which 0
182for i ulong ulong envcnt bound 9223372036854775807
183n = envread i hc_env 0 len hc_env
184if lt n 0
185ret false
186end
187if ge ulong n len key
188same = true
189for j ulong len key bound 9223372036854775807
190if ne get hc_env j get key j
191same = false
192end
193end
194if same
195p = len key
196while lt p ulong n bound 9223372036854775807
197hc_add which get hc_env p
198p = add p 1
199end
200ret true
201end
202end
203end
204ret false
205end
206
207fn hc_prefix which ulong s view uchar * to bool
208if lt get hc_n which len s
209ret false
210end
211for i ulong len s bound 9223372036854775807
212if ne hc_get which i get s i
213ret false
214end
215end
216ret true
217end
218
219fn hc_jbyte c uchar to void
220if or eq c 34 eq c 92
221hc_add 3 92
222hc_add 3 c
223ret
224end
225if lt c 32
226hc_add 3 92
227hc_add 3 117
228hc_add 3 48
229hc_add 3 48
230v = add uchar 48 shr c 4
231if gt v 57
232v = add v 39
233end
234hc_add 3 v
235v = add uchar 48 and c 15
236if gt v 57
237v = add v 39
238end
239hc_add 3 v
240ret
241end
242hc_add 3 c
243end
244
245fn hc_cfgesc which ulong to void
246for i ulong get hc_n which bound 9223372036854775807
247c = hc_get which i
248if or eq c 34 eq c 92
249hc_add 0 92
250end
251hc_add 0 c
252end
253end
254
255fn hc_prepare to bool
256a = hc_envget hc_uk 4
257b = hc_envget hc_mk 5
258k = hc_envget hc_kk 6
259if or or not a not b or eq get hc_n 4 0 eq get hc_n 5 0
260hc_rows hc_need
261ret false
262end
263if not or hc_prefix 4 hc_h hc_prefix 4 hc_hs
264hc_rows hc_badurl
265ret false
266end
267for i ulong get hc_n 4 bound 9223372036854775807
268c = get hc_url i
269if or lt c 33 or eq c 127 or eq c 63 eq c 35
270hc_rows hc_badurl
271ret false
272end
273end
274for i ulong get hc_n 6 bound 9223372036854775807
275c = get hc_key i
276if or lt c 32 eq c 127
277hc_rows hc_badkey
278ret false
279end
280end
281while gt get hc_n 4 0 bound 9223372036854775807
282if ne get hc_url sub get hc_n 4 1 47
283break
284end
285put hc_n 4 sub get hc_n 4 1
286end
287hc_text 4 hc_tail
288put hc_n 3 0
289hc_text 3 hc_b1
290for i ulong get hc_n 5 bound 9223372036854775807
291hc_jbyte get hc_model i
292end
293hc_text 3 hc_b2
294p = ulong 4
295while lt p get lx_qn 0 bound 9223372036854775807
296hc_jbyte get lx_query p
297p = add p 1
298end
299hc_text 3 hc_b3
300put hc_n 0 0
301hc_text 0 hc_ct
302hc_cfgesc 4
303if gt get hc_n 6 0
304hc_text 0 hc_auth
305hc_cfgesc 6
306end
307hc_text 0 hc_data
308hc_cfgesc 3
309hc_text 0 hc_end
310ret true
311end
312
313fn hc_pack p ulong s view uchar * to ulong
314put hc_exec p uchar and len s 255
315put hc_exec add p 1 uchar shr len s 8
316for i ulong len s bound 128
317put hc_exec add add p 2 i get s i
318end
319ret add add p 2 len s
320end
321
322fn hc_readfd p ulong to int
323v = or ulong get hc_pipe p shl ulong get hc_pipe add p 1 8
324v = or v shl ulong get hc_pipe add p 2 16
325v = or v shl ulong get hc_pipe add p 3 24
326ret int v
327end
328
329fn hc_request to bool
330put hc_http 0 0
331put hc_exec 0 4
332put hc_exec 1 0
333p = hc_pack 2 hc_curl
334p = hc_pack p hc_q
335p = hc_pack p hc_config
336p = hc_pack p hc_dash
337plen = p
338input = memfd hc_memname 0 len hc_memname 1
339if lt input 0
340ret false
341end
342p = ulong 0
343while lt p get hc_n 0 bound 9223372036854775807
344k = write int input hc_cfg p sub get hc_n 0 p
345if eq k -4
346continue
347end
348if le k 0
349k = fclose int input
350ret false
351end
352p = add p ulong k
353end
354k = fseek int input 0 0
355if lt k 0
356k = fclose int input
357ret false
358end
359k = pipefds hc_pipe 8 8 524288
360if ne k 0
361k = fclose int input
362ret false
363end
364pid = fork
365if eq pid 0
366k = dupfd int input 0 0
367if lt k 0
368exit 126
369end
370k = dupfd hc_readfd 12 1 0
371if lt k 0
372exit 126
373end
374note discard transport diagnostics: model URLs or server text can contain sensitive information.
375note All failures are reported in Hop without echoing the key, URL or response.
376k = dupfd hc_readfd 12 2 0
377if lt k 0
378exit 126
379end
380k = execprog hc_exec 0 plen
381exit 127
382end
383k = fclose int input
384k = fclose hc_readfd 12
385if lt pid 0
386k = fclose hc_readfd 8
387ret false
388end
389ok = true
390note Erase credential-bearing configuration after forking its anonymous input.
391for i ulong get hc_n 0 bound 9223372036854775807
392put hc_cfg i 0
393end
394for i ulong get hc_n 6 bound 9223372036854775807
395put hc_key i 0
396end
397put hc_n 0 0
398put hc_n 6 0
399put hc_n 1 0
400while true bound 9223372036854775807
401k = read hc_readfd 8 hc_io 0 len hc_io
402if eq k -4
403continue
404end
405if lt k 0
406ok = false
407break
408end
409if eq k 0
410break
411end
412for i ulong ulong k bound 8192
413hc_add 1 get hc_io i
414end
415end
416k = fclose hc_readfd 8
417k = waitpid int pid hc_wait 0 4 0
418if ne k pid
419ret false
420end
421if lt get hc_n 1 4
422ret false
423end
424p = sub get hc_n 1 4
425if ne get hc_json p 10
426ret false
427end
428statusv = ulong 0
429for i ulong 3
430c = get hc_json add add p 1 i
431if or lt c 48 gt c 57
432ret false
433end
434statusv = add mul statusv 10 ulong sub c 48
435end
436put hc_http 0 statusv
437put hc_n 1 p
438if or lt statusv 200 ge statusv 300
439ret false
440end
441ret and ok and eq get hc_wait 0 0 eq get hc_wait 1 0
442end
443
444fn hc_space to void
445while lt get hc_pos 0 get hc_n 1 bound 9223372036854775807
446c = get hc_json get hc_pos 0
447if not or eq c 32 or eq c 9 or eq c 10 eq c 13
448break
449end
450put hc_pos 0 add get hc_pos 0 1
451end
452end
453
454fn hc_next to int
455hc_space
456if ge get hc_pos 0 get hc_n 1
457ret -1
458end
459ret int get hc_json get hc_pos 0
460end
461
462fn hc_hex c uchar to long
463if and ge c 48 le c 57
464ret long sub c 48
465end
466if and ge c 65 le c 70
467ret add long sub c 65 10
468end
469if and ge c 97 le c 102
470ret add long sub c 97 10
471end
472ret -1
473end
474
475fn hc_code to long
476v = long 0
477for i ulong 4
478if ge get hc_pos 0 get hc_n 1
479ret -1
480end
481h = hc_hex get hc_json get hc_pos 0
482if lt h 0
483ret -1
484end
485v = add mul v 16 h
486put hc_pos 0 add get hc_pos 0 1
487end
488ret v
489end
490
491fn hc_utf cp ulong to void
492if lt cp 128
493hc_add 2 uchar cp
494ret
495end
496if lt cp 2048
497hc_add 2 uchar or 192 shr cp 6
498else
499if ge cp 65536
500hc_add 2 uchar or 240 shr cp 18
501hc_add 2 uchar or 128 and shr cp 12 63
502else
503hc_add 2 uchar or 224 shr cp 12
504end
505hc_add 2 uchar or 128 and shr cp 6 63
506end
507hc_add 2 uchar or 128 and cp 63
508end
509
510fn hc_string to bool
511if ne hc_next 34
512ret false
513end
514put hc_pos 0 add get hc_pos 0 1
515put hc_n 2 0
516while lt get hc_pos 0 get hc_n 1 bound 9223372036854775807
517c = get hc_json get hc_pos 0
518put hc_pos 0 add get hc_pos 0 1
519if eq c 34
520ret true
521end
522if lt c 32
523ret false
524end
525if ne c 92
526hc_add 2 c
527continue
528end
529if ge get hc_pos 0 get hc_n 1
530ret false
531end
532c = get hc_json get hc_pos 0
533put hc_pos 0 add get hc_pos 0 1
534if or eq c 34 or eq c 92 eq c 47
535hc_add 2 c
536continue
537end
538if eq c 110
539hc_add 2 10
540continue
541end
542if eq c 114
543hc_add 2 13
544continue
545end
546if eq c 116
547hc_add 2 9
548continue
549end
550if eq c 98
551hc_add 2 8
552continue
553end
554if eq c 102
555hc_add 2 12
556continue
557end
558if ne c 117
559ret false
560end
561cp = hc_code
562if lt cp 0
563ret false
564end
565if and ge cp 55296 le cp 56319
566if gt add get hc_pos 0 2 get hc_n 1
567ret false
568end
569if ne get hc_json get hc_pos 0 92
570ret false
571end
572if ne get hc_json add get hc_pos 0 1 117
573ret false
574end
575put hc_pos 0 add get hc_pos 0 2
576lo = hc_code
577if or lt lo 56320 gt lo 57343
578ret false
579end
580cp = add 65536 add mul sub cp 55296 1024 sub lo 56320
581else
582if and ge cp 56320 le cp 57343
583ret false
584end
585end
586hc_utf ulong cp
587end
588ret false
589end
590
591fn hc_streq s view uchar * to bool
592if ne get hc_n 2 len s
593ret false
594end
595for i ulong len s bound 9223372036854775807
596if ne get hc_str i get s i
597ret false
598end
599end
600ret true
601end
602
603fn hc_literal s view uchar * to bool
604if gt add get hc_pos 0 len s get hc_n 1
605ret false
606end
607for i ulong len s bound 9223372036854775807
608if ne get hc_json add get hc_pos 0 i get s i
609ret false
610end
611end
612put hc_pos 0 add get hc_pos 0 len s
613ret true
614end
615
616fn hc_digit to bool
617if ge get hc_pos 0 get hc_n 1
618ret false
619end
620c = get hc_json get hc_pos 0
621ret and ge c 48 le c 57
622end
623
624fn hc_digits to void
625while hc_digit bound 9223372036854775807
626put hc_pos 0 add get hc_pos 0 1
627end
628end
629
630fn hc_number to bool
631if eq hc_next 45
632put hc_pos 0 add get hc_pos 0 1
633end
634if not hc_digit
635ret false
636end
637if eq get hc_json get hc_pos 0 48
638put hc_pos 0 add get hc_pos 0 1
639else
640hc_digits
641end
642if lt get hc_pos 0 get hc_n 1
643if eq get hc_json get hc_pos 0 46
644put hc_pos 0 add get hc_pos 0 1
645if not hc_digit
646ret false
647end
648hc_digits
649end
650end
651if lt get hc_pos 0 get hc_n 1
652c = get hc_json get hc_pos 0
653if or eq c 101 eq c 69
654put hc_pos 0 add get hc_pos 0 1
655if lt get hc_pos 0 get hc_n 1
656c = get hc_json get hc_pos 0
657if or eq c 43 eq c 45
658put hc_pos 0 add get hc_pos 0 1
659end
660end
661if not hc_digit
662ret false
663end
664hc_digits
665end
666end
667ret true
668end
669
670fn hc_value mode ulong to bool
671c = hc_next
672if eq c 123
673put hc_pos 0 add get hc_pos 0 1
674if eq hc_next 125
675put hc_pos 0 add get hc_pos 0 1
676ret true
677end
678while true bound 9223372036854775807
679if not hc_string
680ret false
681end
682next = ulong 0
683if and eq mode 1 hc_streq hc_choices
684next = 2
685end
686if and eq mode 3 hc_streq hc_message
687next = 4
688end
689if and eq mode 4 hc_streq hc_content
690next = 5
691end
692if ne hc_next 58
693ret false
694end
695put hc_pos 0 add get hc_pos 0 1
696if not hc_value next
697ret false
698end
699c = hc_next
700put hc_pos 0 add get hc_pos 0 1
701if eq c 125
702ret true
703end
704if ne c 44
705ret false
706end
707end
708end
709if eq c 91
710put hc_pos 0 add get hc_pos 0 1
711if eq hc_next 93
712put hc_pos 0 add get hc_pos 0 1
713ret true
714end
715idx = ulong 0
716while true bound 9223372036854775807
717next = ulong 0
718if and eq mode 2 eq idx 0
719next = 3
720end
721if not hc_value next
722ret false
723end
724idx = add idx 1
725c = hc_next
726put hc_pos 0 add get hc_pos 0 1
727if eq c 93
728ret true
729end
730if ne c 44
731ret false
732end
733end
734end
735if eq c 34
736if not hc_string
737ret false
738end
739if eq mode 5
740if ne get hc_found 0 0
741ret false
742end
743put hc_n 7 0
744for i ulong get hc_n 2 bound 9223372036854775807
745hc_add 7 get hc_str i
746end
747put hc_found 0 1
748end
749ret true
750end
751if eq c 116
752ret hc_literal hc_true
753end
754if eq c 102
755ret hc_literal hc_false
756end
757if eq c 110
758ret hc_literal hc_null
759end
760if or eq c 45 and ge c 48 le c 57
761ret hc_number
762end
763ret false
764end
765
766fn hc_query to int
767if lt get lx_qn 0 5
768ret 0
769end
770if or ne get lx_query 0 97 ne get lx_query 1 115
771ret 0
772end
773if or ne get lx_query 2 107 ne get lx_query 3 32
774ret 0
775end
776r = enlarge lx_rows add get lx_qn 0 1
777if lt r 0
778x = write 2 hc_mem 0 len hc_mem
779exit 1
780end
781for i ulong len hc_action
782put lx_rows i get hc_action i
783end
784p = len hc_action
785i = ulong 4
786while lt i get lx_qn 0 bound 9223372036854775807
787put lx_rows p get lx_query i
788p = add p 1
789i = add i 1
790end
791put lx_rn 0 p
792put lx_pick 0 0
793ret 1
794end
795
796fn hc_activate to int
797if not hc_prepare
798ret 0
799end
800if not hc_request
801if gt get hc_http 0 0
802put hc_n 7 0
803hc_text 7 hc_server
804hc_add 7 uchar add 48 div get hc_http 0 100
805hc_add 7 uchar add 48 rem div get hc_http 0 10 10
806hc_add 7 uchar add 48 rem get hc_http 0 10
807hc_text 7 hc_serverend
808r = enlarge lx_rows get hc_n 7
809if lt r 0
810x = write 2 hc_mem 0 len hc_mem
811exit 1
812end
813for i ulong get hc_n 7 bound 9223372036854775807
814put lx_rows i get hc_ans i
815end
816put lx_rn 0 get hc_n 7
817put lx_pick 0 0
818ret 0
819end
820hc_rows hc_transport
821ret 0
822end
823put hc_pos 0 0
824put hc_found 0 0
825put hc_n 7 0
826ok = hc_value 1
827hc_space
828if or not ok or ne get hc_found 0 1 ne get hc_pos 0 get hc_n 1
829hc_rows hc_badjson
830ret 0
831end
832r = enlarge lx_rows get hc_n 7
833if lt r 0
834x = write 2 hc_mem 0 len hc_mem
835exit 1
836end
837for i ulong get hc_n 7 bound 9223372036854775807
838put lx_rows i get hc_ans i
839end
840put lx_rn 0 get hc_n 7
841put lx_pick 0 0
842ret 0
843end