haillo 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- haillo-0.1.0/LICENSE.txt +19 -0
- haillo-0.1.0/MANIFEST.in +6 -0
- haillo-0.1.0/PKG-INFO +117 -0
- haillo-0.1.0/README.rst +94 -0
- haillo-0.1.0/haillo/__init__.py +4 -0
- haillo-0.1.0/haillo/__main__.py +68 -0
- haillo-0.1.0/haillo/config.py +8 -0
- haillo-0.1.0/haillo/data/haillo.1 +33 -0
- haillo-0.1.0/haillo/haillo.py +231 -0
- haillo-0.1.0/haillo/hutils.py +5 -0
- haillo-0.1.0/haillo/logger.py +96 -0
- haillo-0.1.0/haillo/package.py +6 -0
- haillo-0.1.0/haillo/static/context.css +146 -0
- haillo-0.1.0/haillo/static/context.js +59 -0
- haillo-0.1.0/haillo/static/icons/icon_144x144.png +0 -0
- haillo-0.1.0/haillo/static/manifest.json +17 -0
- haillo-0.1.0/haillo/static/sidebar.css +209 -0
- haillo-0.1.0/haillo/static/sidebar.js +20 -0
- haillo-0.1.0/haillo/static/sw.js +9 -0
- haillo-0.1.0/haillo/templates/chat_history.html +201 -0
- haillo-0.1.0/haillo/templates/index.html +88 -0
- haillo-0.1.0/haillo/templates/sidebar.html +35 -0
- haillo-0.1.0/haillo.egg-info/PKG-INFO +117 -0
- haillo-0.1.0/haillo.egg-info/SOURCES.txt +30 -0
- haillo-0.1.0/haillo.egg-info/dependency_links.txt +1 -0
- haillo-0.1.0/haillo.egg-info/entry_points.txt +2 -0
- haillo-0.1.0/haillo.egg-info/requires.txt +3 -0
- haillo-0.1.0/haillo.egg-info/top_level.txt +1 -0
- haillo-0.1.0/pyproject.toml +48 -0
- haillo-0.1.0/requirements.txt +3 -0
- haillo-0.1.0/setup.cfg +7 -0
haillo-0.1.0/LICENSE.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2024 Jeff Michaud
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
the Software without restriction, including without limitation the rights to
|
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
8
|
+
so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
haillo-0.1.0/MANIFEST.in
ADDED
haillo-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: haillo
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Haillo is a web frontend for hai (hcli_hai), the command line AI chat application.
|
|
5
|
+
Author-email: Jeff Michaud <cometaj2@proton.me>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/cometaj2/haillo
|
|
8
|
+
Keywords: web,ui,cli,client,server,connector,hypermedia,rest,generic,development
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Topic :: Software Development
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Description-Content-Type: text/x-rst
|
|
19
|
+
License-File: LICENSE.txt
|
|
20
|
+
Requires-Dist: flask==3.0.1
|
|
21
|
+
Requires-Dist: huckle>=5.5.4
|
|
22
|
+
Requires-Dist: hcli_hai>=2.1.3
|
|
23
|
+
|
|
24
|
+
|pypi| |pyver| |hclicore| |huckle| |hai|
|
|
25
|
+
|
|
26
|
+
Haillo
|
|
27
|
+
======
|
|
28
|
+
|
|
29
|
+
Haillo is a web frontend for hai (hcli_hai), the command line AI chat application.
|
|
30
|
+
|
|
31
|
+
----
|
|
32
|
+
|
|
33
|
+
Haillo is able to work with the hai HCLI application (hcli_hai) to interact with locally stored ai conversation contexts.
|
|
34
|
+
|
|
35
|
+
Note that a valid Anthropic API key has to be setup for hcli_hai to be usable. see hai help after hooking to it with huckle (see installation instructions below).
|
|
36
|
+
|
|
37
|
+
Help shape HCLI and it's ecosystem by raising issues on github!
|
|
38
|
+
|
|
39
|
+
[1] http://hcli.io
|
|
40
|
+
|
|
41
|
+
Related HCLI Projects
|
|
42
|
+
---------------------
|
|
43
|
+
|
|
44
|
+
- hcli_core, an HCLI Connector that can be used to expose a REST API that behaves as a CLI, via hypertext [2]
|
|
45
|
+
|
|
46
|
+
- huckle is a CLI, and python library, that can act as an impostor for any CLI expressed via hypertext [3]
|
|
47
|
+
|
|
48
|
+
- hcli-hai, a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Anthropic's Claude models via terminal input and output streams. [4]
|
|
49
|
+
|
|
50
|
+
[2] https://github.com/cometaj2/hcli_core
|
|
51
|
+
|
|
52
|
+
[3] https://github.com/cometaj2/huckle
|
|
53
|
+
|
|
54
|
+
[4] https://github.com/cometaj2/hcli_hai
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
Installation
|
|
58
|
+
------------
|
|
59
|
+
|
|
60
|
+
haillo requires a supported version of Python and pip.
|
|
61
|
+
|
|
62
|
+
You'll need an WSGI compliant application server to run haillo. For example, you can use Green Unicorn (https://gunicorn.org/).
|
|
63
|
+
|
|
64
|
+
.. code-block:: console
|
|
65
|
+
|
|
66
|
+
pip install haillo
|
|
67
|
+
pip install huckle
|
|
68
|
+
pip install hcli_hai
|
|
69
|
+
pip install hcli_core
|
|
70
|
+
pip install gunicorn
|
|
71
|
+
gunicorn --workers=1 --threads=100 -b 0.0.0.0:10000 "hcli_core:connector(\"`hcli_hai path`\")"
|
|
72
|
+
huckle cli install localhost:10000
|
|
73
|
+
gunicorn --preload --workers=4 --threads=100 -b 127.0.0.1:8000 --chdir `haillo path` "haillo:webapp()"
|
|
74
|
+
|
|
75
|
+
Usage
|
|
76
|
+
-----
|
|
77
|
+
|
|
78
|
+
.. code-block:: console
|
|
79
|
+
|
|
80
|
+
haillo help
|
|
81
|
+
hcli_hai help
|
|
82
|
+
huckle help
|
|
83
|
+
hcli_core help
|
|
84
|
+
hai help
|
|
85
|
+
|
|
86
|
+
Versioning
|
|
87
|
+
----------
|
|
88
|
+
|
|
89
|
+
This project makes use of semantic versioning (http://semver.org) and may make use of the "devx",
|
|
90
|
+
"prealphax", "alphax" "betax", and "rcx" extensions where x is a number (e.g. 0.3.0-prealpha1)
|
|
91
|
+
on github. Only full major.minor.patch releases will be pushed to pip from now on.
|
|
92
|
+
|
|
93
|
+
Supports
|
|
94
|
+
--------
|
|
95
|
+
|
|
96
|
+
- Interacting with an hcli_hai (hai HCLI application) backend via the huckle hcli client.
|
|
97
|
+
|
|
98
|
+
To Do
|
|
99
|
+
-----
|
|
100
|
+
|
|
101
|
+
- TBD
|
|
102
|
+
|
|
103
|
+
Bugs
|
|
104
|
+
----
|
|
105
|
+
|
|
106
|
+
- TBD
|
|
107
|
+
|
|
108
|
+
.. |pypi| image:: https://img.shields.io/pypi/v/haillo?label=haillo
|
|
109
|
+
:target: https://pypi.org/project/haillo
|
|
110
|
+
.. |pyver| image:: https://img.shields.io/pypi/pyversions/haillo.svg
|
|
111
|
+
:target: https://pypi.org/project/haillo
|
|
112
|
+
.. |hclicore| image:: https://circleci.com/gh/cometaj2/hcli_core.svg?style=shield
|
|
113
|
+
:target: https://circleci.com/gh/cometaj2/hcli_core
|
|
114
|
+
.. |huckle| image:: https://img.shields.io/pypi/v/huckle?label=huckle
|
|
115
|
+
:target: https://pypi.org/project/huckle
|
|
116
|
+
.. |hai| image:: https://img.shields.io/pypi/v/hcli-hc?label=hcli-hai
|
|
117
|
+
:target: https://pypi.org/project/hcli-hai
|
haillo-0.1.0/README.rst
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
|pypi| |pyver| |hclicore| |huckle| |hai|
|
|
2
|
+
|
|
3
|
+
Haillo
|
|
4
|
+
======
|
|
5
|
+
|
|
6
|
+
Haillo is a web frontend for hai (hcli_hai), the command line AI chat application.
|
|
7
|
+
|
|
8
|
+
----
|
|
9
|
+
|
|
10
|
+
Haillo is able to work with the hai HCLI application (hcli_hai) to interact with locally stored ai conversation contexts.
|
|
11
|
+
|
|
12
|
+
Note that a valid Anthropic API key has to be setup for hcli_hai to be usable. see hai help after hooking to it with huckle (see installation instructions below).
|
|
13
|
+
|
|
14
|
+
Help shape HCLI and it's ecosystem by raising issues on github!
|
|
15
|
+
|
|
16
|
+
[1] http://hcli.io
|
|
17
|
+
|
|
18
|
+
Related HCLI Projects
|
|
19
|
+
---------------------
|
|
20
|
+
|
|
21
|
+
- hcli_core, an HCLI Connector that can be used to expose a REST API that behaves as a CLI, via hypertext [2]
|
|
22
|
+
|
|
23
|
+
- huckle is a CLI, and python library, that can act as an impostor for any CLI expressed via hypertext [3]
|
|
24
|
+
|
|
25
|
+
- hcli-hai, a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Anthropic's Claude models via terminal input and output streams. [4]
|
|
26
|
+
|
|
27
|
+
[2] https://github.com/cometaj2/hcli_core
|
|
28
|
+
|
|
29
|
+
[3] https://github.com/cometaj2/huckle
|
|
30
|
+
|
|
31
|
+
[4] https://github.com/cometaj2/hcli_hai
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
Installation
|
|
35
|
+
------------
|
|
36
|
+
|
|
37
|
+
haillo requires a supported version of Python and pip.
|
|
38
|
+
|
|
39
|
+
You'll need an WSGI compliant application server to run haillo. For example, you can use Green Unicorn (https://gunicorn.org/).
|
|
40
|
+
|
|
41
|
+
.. code-block:: console
|
|
42
|
+
|
|
43
|
+
pip install haillo
|
|
44
|
+
pip install huckle
|
|
45
|
+
pip install hcli_hai
|
|
46
|
+
pip install hcli_core
|
|
47
|
+
pip install gunicorn
|
|
48
|
+
gunicorn --workers=1 --threads=100 -b 0.0.0.0:10000 "hcli_core:connector(\"`hcli_hai path`\")"
|
|
49
|
+
huckle cli install localhost:10000
|
|
50
|
+
gunicorn --preload --workers=4 --threads=100 -b 127.0.0.1:8000 --chdir `haillo path` "haillo:webapp()"
|
|
51
|
+
|
|
52
|
+
Usage
|
|
53
|
+
-----
|
|
54
|
+
|
|
55
|
+
.. code-block:: console
|
|
56
|
+
|
|
57
|
+
haillo help
|
|
58
|
+
hcli_hai help
|
|
59
|
+
huckle help
|
|
60
|
+
hcli_core help
|
|
61
|
+
hai help
|
|
62
|
+
|
|
63
|
+
Versioning
|
|
64
|
+
----------
|
|
65
|
+
|
|
66
|
+
This project makes use of semantic versioning (http://semver.org) and may make use of the "devx",
|
|
67
|
+
"prealphax", "alphax" "betax", and "rcx" extensions where x is a number (e.g. 0.3.0-prealpha1)
|
|
68
|
+
on github. Only full major.minor.patch releases will be pushed to pip from now on.
|
|
69
|
+
|
|
70
|
+
Supports
|
|
71
|
+
--------
|
|
72
|
+
|
|
73
|
+
- Interacting with an hcli_hai (hai HCLI application) backend via the huckle hcli client.
|
|
74
|
+
|
|
75
|
+
To Do
|
|
76
|
+
-----
|
|
77
|
+
|
|
78
|
+
- TBD
|
|
79
|
+
|
|
80
|
+
Bugs
|
|
81
|
+
----
|
|
82
|
+
|
|
83
|
+
- TBD
|
|
84
|
+
|
|
85
|
+
.. |pypi| image:: https://img.shields.io/pypi/v/haillo?label=haillo
|
|
86
|
+
:target: https://pypi.org/project/haillo
|
|
87
|
+
.. |pyver| image:: https://img.shields.io/pypi/pyversions/haillo.svg
|
|
88
|
+
:target: https://pypi.org/project/haillo
|
|
89
|
+
.. |hclicore| image:: https://circleci.com/gh/cometaj2/hcli_core.svg?style=shield
|
|
90
|
+
:target: https://circleci.com/gh/cometaj2/hcli_core
|
|
91
|
+
.. |huckle| image:: https://img.shields.io/pypi/v/huckle?label=huckle
|
|
92
|
+
:target: https://pypi.org/project/huckle
|
|
93
|
+
.. |hai| image:: https://img.shields.io/pypi/v/hcli-hc?label=hcli-hai
|
|
94
|
+
:target: https://pypi.org/project/hcli-hai
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
from subprocess import call
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from . import package
|
|
6
|
+
from . import config
|
|
7
|
+
from . import hutils
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main():
|
|
11
|
+
if len(sys.argv) == 2:
|
|
12
|
+
|
|
13
|
+
if sys.argv[1] == "--version":
|
|
14
|
+
show_dependencies()
|
|
15
|
+
|
|
16
|
+
elif sys.argv[1] == "help":
|
|
17
|
+
display_man_page(config.manpage_path)
|
|
18
|
+
sys.exit(0)
|
|
19
|
+
|
|
20
|
+
elif sys.argv[1] == "path":
|
|
21
|
+
print(config.root)
|
|
22
|
+
sys.exit(0)
|
|
23
|
+
|
|
24
|
+
elif sys.argv[1] == "run":
|
|
25
|
+
print('gunicorn --preload --workers=4 --threads=100 -b 127.0.0.1:8000 --chdir `haillo path` "haillo:webapp()"')
|
|
26
|
+
# print('waitress-serve --listen=127.0.0.1:8000 --threads=100 haillo:app')
|
|
27
|
+
# print('gunicorn --workers=1 --threads=100 --timeout=30 --keep-alive=5 --graceful-timeout=10 -b 127.0.0.1:8000 "haillo:webapp()"')
|
|
28
|
+
# print('waitress-serve --max-request-header-size=262144 --clear-untrusted-proxy-headers --listen=127.0.0.1:8000 --channel-timeout=30 --asyncore-use-poll --cleanup-interval=5 haillo:app')
|
|
29
|
+
sys.exit(0)
|
|
30
|
+
|
|
31
|
+
else:
|
|
32
|
+
help()
|
|
33
|
+
|
|
34
|
+
else:
|
|
35
|
+
help()
|
|
36
|
+
|
|
37
|
+
def show_dependencies():
|
|
38
|
+
def parse_dependency(dep_string):
|
|
39
|
+
# Common version specifiers
|
|
40
|
+
specifiers = ['==', '>=', '<=', '~=', '>', '<', '!=']
|
|
41
|
+
|
|
42
|
+
# Find the first matching specifier
|
|
43
|
+
for specifier in specifiers:
|
|
44
|
+
if specifier in dep_string:
|
|
45
|
+
name, version = dep_string.split(specifier, 1)
|
|
46
|
+
return name.strip(), specifier, version.strip()
|
|
47
|
+
|
|
48
|
+
# If no specifier found, return just the name
|
|
49
|
+
return dep_string.strip(), '', ''
|
|
50
|
+
|
|
51
|
+
dependencies = ""
|
|
52
|
+
for dep in package.dependencies:
|
|
53
|
+
name, specifier, version = parse_dependency(dep)
|
|
54
|
+
if version: # Only add separator if there's a version
|
|
55
|
+
dependencies += f" {name}/{version}"
|
|
56
|
+
else:
|
|
57
|
+
dependencies += f" {name}"
|
|
58
|
+
|
|
59
|
+
print(f"haillo/{package.__version__}{dependencies}")
|
|
60
|
+
|
|
61
|
+
def help():
|
|
62
|
+
hutils.eprint("for help, use:\n")
|
|
63
|
+
hutils.eprint(" haillo help")
|
|
64
|
+
sys.exit(2)
|
|
65
|
+
|
|
66
|
+
# displays a man page (file) located on a given path
|
|
67
|
+
def display_man_page(path):
|
|
68
|
+
call(["man", path])
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
.TH HAILLO 1 "FEBRUARY 2025" Linux "User Manuals"
|
|
2
|
+
.SH NAME
|
|
3
|
+
Haillo \- a web frontend for hai (hcli_hai), the command line AI chat application.
|
|
4
|
+
.SH SYNOPSIS
|
|
5
|
+
.B haillo
|
|
6
|
+
[
|
|
7
|
+
.I option
|
|
8
|
+
|
|
|
9
|
+
.I <command>
|
|
10
|
+
]
|
|
11
|
+
.SH DESCRIPTION
|
|
12
|
+
.B haillo
|
|
13
|
+
is a Web Service Gateway Interface (WSGI; PEP 3333) application and progressive web app (PWA) that provides a way to interact with the hai command line chat application.
|
|
14
|
+
.br
|
|
15
|
+
Note that huckle must be setup to interact with hcli_hai for haillo to be able to make use of it (e.g. huckle cli install localhost:10000 (where hcli_hai could be running))
|
|
16
|
+
|
|
17
|
+
.SH COMMANDS
|
|
18
|
+
.IP "path"
|
|
19
|
+
Provides the installation path of haillo, which can be used to help locate the path of the WSGI app to use in a WSGI server.
|
|
20
|
+
.IP help
|
|
21
|
+
This help file.
|
|
22
|
+
.SH OPTIONS
|
|
23
|
+
.IP --version
|
|
24
|
+
.B haillo
|
|
25
|
+
version and the version of it's dependencies.
|
|
26
|
+
.SH EXAMPLE
|
|
27
|
+
haillo path
|
|
28
|
+
|
|
29
|
+
haillo --version
|
|
30
|
+
|
|
31
|
+
haillo run | bash
|
|
32
|
+
|
|
33
|
+
gunicorn --preload --workers=4 --threads=100 -b 127.0.0.1:8000 --chdir `haillo path` "haillo:webapp()"
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import flask
|
|
2
|
+
import config
|
|
3
|
+
import json
|
|
4
|
+
import logger
|
|
5
|
+
import sys
|
|
6
|
+
import io
|
|
7
|
+
import traceback
|
|
8
|
+
import contextlib
|
|
9
|
+
import urllib3
|
|
10
|
+
import time
|
|
11
|
+
from huckle import cli, stdin
|
|
12
|
+
from flask import Flask, render_template, send_file, jsonify, Response, redirect, url_for, request
|
|
13
|
+
import subprocess
|
|
14
|
+
import shlex
|
|
15
|
+
import ast
|
|
16
|
+
|
|
17
|
+
logging = logger.Logger()
|
|
18
|
+
logging.setLevel(logger.INFO)
|
|
19
|
+
|
|
20
|
+
app = None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def get_chat_list():
|
|
24
|
+
try:
|
|
25
|
+
chunks = cli("hai ls")
|
|
26
|
+
json_string = ""
|
|
27
|
+
for dest, chunk in chunks:
|
|
28
|
+
if dest == 'stdout':
|
|
29
|
+
json_string += chunk.decode()
|
|
30
|
+
chats = json.loads(json_string)
|
|
31
|
+
# Sort the list with most recent dates first
|
|
32
|
+
sorted_chats = sorted(chats, key=lambda x: x['update_time'], reverse=True)
|
|
33
|
+
return sorted_chats
|
|
34
|
+
except Exception as error:
|
|
35
|
+
logging.error(f"Error getting chat list: {error}")
|
|
36
|
+
return []
|
|
37
|
+
|
|
38
|
+
def parse_context(context_str):
|
|
39
|
+
try:
|
|
40
|
+
context_data = json.loads(context_str)
|
|
41
|
+
return {
|
|
42
|
+
'messages': context_data.get('messages', []),
|
|
43
|
+
'name': context_data.get('name', ''),
|
|
44
|
+
'title': context_data.get('title', '')
|
|
45
|
+
}
|
|
46
|
+
except json.JSONDecodeError as e:
|
|
47
|
+
logging.error(f"Error parsing context JSON: {e}")
|
|
48
|
+
return {'messages': [], 'name': '', 'title': ''}
|
|
49
|
+
|
|
50
|
+
def webapp():
|
|
51
|
+
################################################ CRITICAL ################################################
|
|
52
|
+
# This ensures a clean stdin state before workers fork.
|
|
53
|
+
# Without this, flask/gunicorn can hand off an inconsistent stdin state to huckle which causes
|
|
54
|
+
# Huckle to attempt to stream data when there is none to stream, leading to unexpected stream interruption
|
|
55
|
+
# socket closeout in urllib3's pool and bad file descriptor errors.
|
|
56
|
+
##########################################################################################################
|
|
57
|
+
if not sys.stdin.isatty():
|
|
58
|
+
sys.stdin = io.BytesIO()
|
|
59
|
+
|
|
60
|
+
app = Flask(__name__)
|
|
61
|
+
|
|
62
|
+
@app.route('/')
|
|
63
|
+
def index():
|
|
64
|
+
try:
|
|
65
|
+
# Get the current context
|
|
66
|
+
chunks = cli("hai context")
|
|
67
|
+
context_str = ""
|
|
68
|
+
for dest, chunk in chunks:
|
|
69
|
+
if dest == 'stdout':
|
|
70
|
+
context_str += chunk.decode()
|
|
71
|
+
|
|
72
|
+
# Get chat list for sidebar
|
|
73
|
+
chats = get_chat_list()
|
|
74
|
+
|
|
75
|
+
# Get model in use
|
|
76
|
+
chunks = cli(f"hai model")
|
|
77
|
+
model = ""
|
|
78
|
+
for dest, chunk in chunks: # Now unpacking tuple of (dest, chunk)
|
|
79
|
+
if dest == 'stdout':
|
|
80
|
+
model += chunk.decode()
|
|
81
|
+
|
|
82
|
+
# Get model in use
|
|
83
|
+
chunks = cli(f"hai model ls")
|
|
84
|
+
models = ""
|
|
85
|
+
for dest, chunk in chunks: # Now unpacking tuple of (dest, chunk)
|
|
86
|
+
if dest == 'stdout':
|
|
87
|
+
models += chunk.decode()
|
|
88
|
+
|
|
89
|
+
# Convert to a python list
|
|
90
|
+
models = ast.literal_eval(models)
|
|
91
|
+
|
|
92
|
+
# Parse the context into structured data
|
|
93
|
+
context_data = parse_context(context_str)
|
|
94
|
+
|
|
95
|
+
return render_template('index.html',
|
|
96
|
+
messages=context_data['messages'],
|
|
97
|
+
name=context_data['name'],
|
|
98
|
+
title=context_data['title'],
|
|
99
|
+
chats=chats,
|
|
100
|
+
model=model,
|
|
101
|
+
models=models)
|
|
102
|
+
except Exception as error:
|
|
103
|
+
logging.error(traceback.format_exc())
|
|
104
|
+
return render_template('index.html',
|
|
105
|
+
messages=[],
|
|
106
|
+
name='',
|
|
107
|
+
title='',
|
|
108
|
+
chats=[],
|
|
109
|
+
model=None,
|
|
110
|
+
models=[])
|
|
111
|
+
|
|
112
|
+
@app.route('/chat_history')
|
|
113
|
+
def chat_history():
|
|
114
|
+
try:
|
|
115
|
+
# Get chat list for sidebar
|
|
116
|
+
chats = get_chat_list()
|
|
117
|
+
|
|
118
|
+
# Get model in use
|
|
119
|
+
chunks = cli(f"hai model")
|
|
120
|
+
model = ""
|
|
121
|
+
for dest, chunk in chunks: # Now unpacking tuple of (dest, chunk)
|
|
122
|
+
if dest == 'stdout':
|
|
123
|
+
model += chunk.decode()
|
|
124
|
+
|
|
125
|
+
# Get model in use
|
|
126
|
+
chunks = cli(f"hai model ls")
|
|
127
|
+
models = ""
|
|
128
|
+
for dest, chunk in chunks: # Now unpacking tuple of (dest, chunk)
|
|
129
|
+
if dest == 'stdout':
|
|
130
|
+
models += chunk.decode()
|
|
131
|
+
|
|
132
|
+
# Convert to a python list
|
|
133
|
+
models = ast.literal_eval(models)
|
|
134
|
+
|
|
135
|
+
return render_template('chat_history.html', chats=chats,
|
|
136
|
+
model=model,
|
|
137
|
+
models=models)
|
|
138
|
+
except Exception as error:
|
|
139
|
+
logging.error(traceback.format_exc())
|
|
140
|
+
return render_template('chat_history.html', chats=[],
|
|
141
|
+
model=None,
|
|
142
|
+
models=[])
|
|
143
|
+
|
|
144
|
+
# We select and set a chat context
|
|
145
|
+
@app.route('/context/<context_id>')
|
|
146
|
+
def navigate_context(context_id):
|
|
147
|
+
try:
|
|
148
|
+
# Execute hai set
|
|
149
|
+
cmd_start = time.time()
|
|
150
|
+
chunks = cli(f"hai set {context_id}")
|
|
151
|
+
chunk_count = 0
|
|
152
|
+
for dest, chunk in chunks:
|
|
153
|
+
chunk_count += 1
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
return redirect(url_for('index'))
|
|
157
|
+
except Exception as error:
|
|
158
|
+
logging.error(f"Context switch failed: {error}")
|
|
159
|
+
logging.error(traceback.format_exc())
|
|
160
|
+
return redirect(url_for('index'))
|
|
161
|
+
|
|
162
|
+
# We delete a chat context with hai rm
|
|
163
|
+
@app.route('/context/<context_id>', methods=['POST'])
|
|
164
|
+
def delete_context(context_id):
|
|
165
|
+
try:
|
|
166
|
+
chunks = cli(f"hai rm {context_id}")
|
|
167
|
+
chunk_count = 0
|
|
168
|
+
for dest, chunk in chunks:
|
|
169
|
+
chunk_count += 1
|
|
170
|
+
return redirect(url_for('index'))
|
|
171
|
+
except Exception as error:
|
|
172
|
+
logging.error(f"Context deletion failed: {error}")
|
|
173
|
+
logging.error(traceback.format_exc())
|
|
174
|
+
return redirect(url_for('index'))
|
|
175
|
+
|
|
176
|
+
# We stream chat data to hai
|
|
177
|
+
@app.route('/chat', methods=['POST'])
|
|
178
|
+
def chat():
|
|
179
|
+
try:
|
|
180
|
+
message = request.form.get('message')
|
|
181
|
+
stream = io.BytesIO(message.encode('utf-8'))
|
|
182
|
+
with stdin(stream):
|
|
183
|
+
chunks = cli(f"hai")
|
|
184
|
+
chunk_count = 0
|
|
185
|
+
for dest, chunk in chunks:
|
|
186
|
+
chunk_count += 1
|
|
187
|
+
return redirect(url_for('index'))
|
|
188
|
+
except Exception as error:
|
|
189
|
+
logging.error(f"Context switch failed: {error}")
|
|
190
|
+
logging.error(traceback.format_exc())
|
|
191
|
+
return redirect(url_for('index'))
|
|
192
|
+
|
|
193
|
+
# We set the model with hai model set
|
|
194
|
+
@app.route('/set_model', methods=['POST'])
|
|
195
|
+
def set_model():
|
|
196
|
+
try:
|
|
197
|
+
model = request.form.get('model')
|
|
198
|
+
chunks = cli(f"hai model set {model}")
|
|
199
|
+
for dest, chunk in chunks:
|
|
200
|
+
chunk_count += 1
|
|
201
|
+
return redirect(url_for('index'))
|
|
202
|
+
except Exception as error:
|
|
203
|
+
logging.error(f"Model switch failed: {error}")
|
|
204
|
+
logging.error(traceback.format_exc())
|
|
205
|
+
return redirect(url_for('index'))
|
|
206
|
+
|
|
207
|
+
# We create a new hai chat context with hai new
|
|
208
|
+
@app.route('/new_chat', methods=['POST'])
|
|
209
|
+
def new_chat():
|
|
210
|
+
try:
|
|
211
|
+
chunks = cli("hai new")
|
|
212
|
+
for dest, chunk in chunks:
|
|
213
|
+
pass
|
|
214
|
+
|
|
215
|
+
return redirect(url_for('index'))
|
|
216
|
+
except Exception as error:
|
|
217
|
+
logging.error(f"New chat creation failed: {error}")
|
|
218
|
+
logging.error(traceback.format_exc())
|
|
219
|
+
return redirect(url_for('index'))
|
|
220
|
+
|
|
221
|
+
@app.route('/manifest.json')
|
|
222
|
+
def serve_manifest():
|
|
223
|
+
return app.send_static_file('manifest.json')
|
|
224
|
+
|
|
225
|
+
@app.route('/sw.js')
|
|
226
|
+
def serve_sw():
|
|
227
|
+
return app.send_static_file('sw.js')
|
|
228
|
+
|
|
229
|
+
return app
|
|
230
|
+
|
|
231
|
+
app = webapp()
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import traceback
|
|
3
|
+
import os
|
|
4
|
+
import io
|
|
5
|
+
import datetime
|
|
6
|
+
import threading
|
|
7
|
+
import collections
|
|
8
|
+
|
|
9
|
+
DEBUG = logging.DEBUG
|
|
10
|
+
INFO = logging.INFO
|
|
11
|
+
WARNING = logging.WARNING
|
|
12
|
+
ERROR = logging.ERROR
|
|
13
|
+
CRITICAL = logging.CRITICAL
|
|
14
|
+
|
|
15
|
+
# custom deque implementation to have log line limits as a sliding window that behaves like a stream being consumed when read.
|
|
16
|
+
class DequeHandler(logging.Handler):
|
|
17
|
+
def __init__(self, *args, **kwargs):
|
|
18
|
+
super().__init__(*args, **kwargs)
|
|
19
|
+
self.deque = collections.deque(maxlen=100)
|
|
20
|
+
|
|
21
|
+
def emit(self, record):
|
|
22
|
+
log_entry = self.format(record)
|
|
23
|
+
self.deque.append(log_entry)
|
|
24
|
+
|
|
25
|
+
def read_and_clear(self):
|
|
26
|
+
logs = list(self.deque)
|
|
27
|
+
self.deque.clear()
|
|
28
|
+
return logs
|
|
29
|
+
|
|
30
|
+
# custom logger implementation that allows for streaming to stdout and tailing of logs per a deque sliding window.
|
|
31
|
+
class Logger:
|
|
32
|
+
instance = None
|
|
33
|
+
streamHandler = None
|
|
34
|
+
clientHandler = None
|
|
35
|
+
initialized = False
|
|
36
|
+
lock = threading.Lock()
|
|
37
|
+
|
|
38
|
+
def __new__(self, *args, **kwargs):
|
|
39
|
+
with Logger.lock:
|
|
40
|
+
if Logger.instance is None:
|
|
41
|
+
Logger.instance = super().__new__(self, *args, **kwargs)
|
|
42
|
+
return Logger.instance
|
|
43
|
+
|
|
44
|
+
def __init__(self, *args, **kwargs):
|
|
45
|
+
with self.lock:
|
|
46
|
+
if not self.initialized:
|
|
47
|
+
self.instance = logging.getLogger("crumbs")
|
|
48
|
+
|
|
49
|
+
date_format = "%Y-%m-%d %H:%M:%S %z"
|
|
50
|
+
message_format = "[%(asctime)s] [%(levelname)-5s] [%(filename)13s:%(lineno)-3s] %(message)s"
|
|
51
|
+
|
|
52
|
+
formatter = logging.Formatter(fmt=message_format, datefmt=date_format)
|
|
53
|
+
|
|
54
|
+
# Check for existing StreamHandler to avoid duplicates
|
|
55
|
+
if not any(isinstance(h, logging.StreamHandler) for h in self.instance.handlers):
|
|
56
|
+
self.streamHandler = logging.StreamHandler()
|
|
57
|
+
self.streamHandler.setFormatter(formatter)
|
|
58
|
+
self.instance.addHandler(self.streamHandler)
|
|
59
|
+
|
|
60
|
+
# Check for existing DequeHandler to avoid duplicates
|
|
61
|
+
if not any(isinstance(h, DequeHandler) for h in self.instance.handlers):
|
|
62
|
+
self.clientHandler = DequeHandler()
|
|
63
|
+
self.clientHandler.setFormatter(formatter)
|
|
64
|
+
self.instance.addHandler(self.clientHandler)
|
|
65
|
+
|
|
66
|
+
self.instance.propagate = False
|
|
67
|
+
|
|
68
|
+
self.initialized = True
|
|
69
|
+
|
|
70
|
+
def setLevel(self, level):
|
|
71
|
+
logging.getLogger("crumbs").setLevel(level)
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
def info(self, msg, *args, **kwargs):
|
|
75
|
+
self.instance.info(msg, *args, stacklevel=2, **kwargs)
|
|
76
|
+
|
|
77
|
+
def debug(self, msg, *args, **kwargs):
|
|
78
|
+
self.instance.debug(msg, *args, stacklevel=2, **kwargs)
|
|
79
|
+
|
|
80
|
+
def warning(self, msg, *args, **kwargs):
|
|
81
|
+
self.instance.warning(msg, *args, stacklevel=2, **kwargs)
|
|
82
|
+
|
|
83
|
+
def error(self, msg, *args, **kwargs):
|
|
84
|
+
self.instance.error(msg, *args, stacklevel=2, **kwargs)
|
|
85
|
+
|
|
86
|
+
def critical(self, msg, *args, **kwargs):
|
|
87
|
+
self.instance.critical(msg, *args, stacklevel=2, **kwargs)
|
|
88
|
+
|
|
89
|
+
def tail(self):
|
|
90
|
+
log_entries = self.clientHandler.read_and_clear()
|
|
91
|
+
log_text = '\n'.join(log_entries)
|
|
92
|
+
|
|
93
|
+
if log_text:
|
|
94
|
+
log_text += '\n' # Adds newline character at the end if there is some log_text
|
|
95
|
+
|
|
96
|
+
return log_text.encode('utf-8')
|