hcli-hai 3.4.2__tar.gz → 4.0.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.
Files changed (60) hide show
  1. {hcli_hai-3.4.2/hcli_hai.egg-info → hcli_hai-4.0.0}/PKG-INFO +42 -13
  2. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/README.rst +8 -4
  3. hcli_hai-4.0.0/hcli_hai/cli/ai/__init__.py +0 -0
  4. hcli_hai-4.0.0/hcli_hai/cli/ai/ai.py +299 -0
  5. hcli_hai-4.0.0/hcli_hai/cli/ai/assistantrunner.py +196 -0
  6. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/cli/ai/context.py +54 -76
  7. hcli_hai-4.0.0/hcli_hai/cli/ai/orchestrator.py +375 -0
  8. hcli_hai-4.0.0/hcli_hai/cli/ai/plan.py +137 -0
  9. hcli_hai-4.0.0/hcli_hai/cli/ai/policy.py +69 -0
  10. hcli_hai-4.0.0/hcli_hai/cli/ai/provider/__init__.py +0 -0
  11. hcli_hai-4.0.0/hcli_hai/cli/ai/provider/provider.py +196 -0
  12. hcli_hai-4.0.0/hcli_hai/cli/ai/router.py +349 -0
  13. hcli_hai-4.0.0/hcli_hai/cli/ai/voice/__init__.py +0 -0
  14. hcli_hai-4.0.0/hcli_hai/cli/ai/voice/audioducker.py +484 -0
  15. hcli_hai-4.0.0/hcli_hai/cli/ai/voice/voice.py +559 -0
  16. hcli_hai-4.0.0/hcli_hai/cli/cli.py +185 -0
  17. hcli_hai-4.0.0/hcli_hai/cli/config.py +249 -0
  18. hcli_hai-4.0.0/hcli_hai/cli/service.py +221 -0
  19. hcli_hai-4.0.0/hcli_hai/cli/skills/GIT.md +7 -0
  20. hcli_hai-4.0.0/hcli_hai/cli/skills/HCLI.md +8 -0
  21. hcli_hai-4.0.0/hcli_hai/cli/skills/LOCAL.md +9 -0
  22. hcli_hai-4.0.0/hcli_hai/cli/skills/WEB.md +7 -0
  23. hcli_hai-4.0.0/hcli_hai/cli/skills/__init__.py +0 -0
  24. hcli_hai-4.0.0/hcli_hai/cli/template.json +1078 -0
  25. hcli_hai-4.0.0/hcli_hai/cli/utils/__init__.py +0 -0
  26. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/data/hcli_hai.1 +5 -3
  27. hcli_hai-4.0.0/hcli_hai/package.py +11 -0
  28. {hcli_hai-3.4.2 → hcli_hai-4.0.0/hcli_hai.egg-info}/PKG-INFO +42 -13
  29. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai.egg-info/SOURCES.txt +17 -5
  30. hcli_hai-4.0.0/hcli_hai.egg-info/requires.txt +10 -0
  31. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/pyproject.toml +8 -4
  32. hcli_hai-4.0.0/requirements.txt +10 -0
  33. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/tests/test_hai.py +2 -0
  34. hcli_hai-3.4.2/hcli_hai/cli/ai/ai.py +0 -245
  35. hcli_hai-3.4.2/hcli_hai/cli/ai/behavior.py +0 -142
  36. hcli_hai-3.4.2/hcli_hai/cli/ai/model.py +0 -48
  37. hcli_hai-3.4.2/hcli_hai/cli/cli.py +0 -108
  38. hcli_hai-3.4.2/hcli_hai/cli/config.py +0 -138
  39. hcli_hai-3.4.2/hcli_hai/cli/jobqueue.py +0 -49
  40. hcli_hai-3.4.2/hcli_hai/cli/runner.py +0 -142
  41. hcli_hai-3.4.2/hcli_hai/cli/service.py +0 -148
  42. hcli_hai-3.4.2/hcli_hai/cli/template.json +0 -571
  43. hcli_hai-3.4.2/hcli_hai/cli/utils/summary.py +0 -56
  44. hcli_hai-3.4.2/hcli_hai/package.py +0 -6
  45. hcli_hai-3.4.2/hcli_hai.egg-info/requires.txt +0 -5
  46. hcli_hai-3.4.2/requirements.txt +0 -5
  47. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/LICENSE.txt +0 -0
  48. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/MANIFEST.in +0 -0
  49. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/__init__.py +0 -0
  50. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/__main__.py +0 -0
  51. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/cli/__init__.py +0 -0
  52. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/cli/logger.py +0 -0
  53. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/cli/utils/formatting.py +0 -0
  54. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/cli/utils/hutils.py +0 -0
  55. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/config.py +0 -0
  56. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai/hutils.py +0 -0
  57. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai.egg-info/dependency_links.txt +0 -0
  58. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai.egg-info/entry_points.txt +0 -0
  59. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/hcli_hai.egg-info/top_level.txt +0 -0
  60. {hcli_hai-3.4.2 → hcli_hai-4.0.0}/setup.cfg +0 -0
@@ -1,9 +1,28 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.1
2
2
  Name: hcli_hai
3
- Version: 3.4.2
4
- Summary: HCLI hai is a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Anthropic's Claude or OpenAI's GPT AI models via terminal input and output streams.
3
+ Version: 4.0.0
4
+ Summary: HCLI hai is a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Ollama (remote or local), xAI, or OpenAI service provider models via terminal input and output streams.
5
5
  Author-email: Jeff Michaud <cometaj2@proton.me>
6
- License: MIT
6
+ License: Copyright (c) 2017 Jeff Michaud
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
9
+ this software and associated documentation files (the "Software"), to deal in
10
+ the Software without restriction, including without limitation the rights to
11
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
+ of the Software, and to permit persons to whom the Software is furnished to do
13
+ so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+
7
26
  Project-URL: Homepage, https://github.com/cometaj2/hcli_hai
8
27
  Keywords: ai,llm,cli,client,server,connector,hypermedia,rest,generic,development
9
28
  Classifier: Development Status :: 4 - Beta
@@ -12,21 +31,27 @@ Classifier: Topic :: Software Development
12
31
  Classifier: Programming Language :: Python :: 3.10
13
32
  Classifier: Programming Language :: Python :: 3.11
14
33
  Classifier: Programming Language :: Python :: 3.12
34
+ Classifier: Programming Language :: Python :: 3.13
35
+ Classifier: Programming Language :: Python :: 3.14
15
36
  Description-Content-Type: text/x-rst
16
37
  License-File: LICENSE.txt
17
- Requires-Dist: tiktoken==0.9.0
18
- Requires-Dist: anthropic==0.49.0
38
+ Requires-Dist: tiktoken==0.12.0
39
+ Requires-Dist: openai==3.6.0
40
+ Requires-Dist: piper-tts==1.7.0
41
+ Requires-Dist: sounddevice==0.5.6
42
+ Requires-Dist: pulsectl==24.12.0
43
+ Requires-Dist: numpy==2.4.4
44
+ Requires-Dist: bashlex==0.18
19
45
  Requires-Dist: huckle<6.0.0,>=5.7.0
20
- Requires-Dist: apscheduler==3.11.0
21
- Requires-Dist: hcli-problem-details==0.1.1
22
- Dynamic: license-file
46
+ Requires-Dist: hcli_problem_details<1.0.0,>=0.2.0
47
+ Requires-Dist: hcli_core<5.0.0,>=4.0.0
23
48
 
24
49
  |pypi| |build status| |pyver|
25
50
 
26
51
  HCLI hai
27
52
  ========
28
53
 
29
- HCLI hai is 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.
54
+ HCLI hai is a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Ollama (remote or local), xAI, or OpenAI models via terminal input and output streams.
30
55
 
31
56
  ----
32
57
 
@@ -54,7 +79,8 @@ You'll need an HCLI Connector to run hai. For example, you can use HCLI Core (ht
54
79
  pip install hcli-core
55
80
  pip install huckle
56
81
  pip install gunicorn
57
- gunicorn --workers=1 --threads=1 -b 127.0.0.1:8000 "hcli_core:connector(\"`hcli_hai path`\")"
82
+ hcli_core cli install `hcli_hai path`
83
+ hcli_core cli run hai | bash
58
84
 
59
85
  Usage
60
86
  -----
@@ -84,17 +110,20 @@ Supports
84
110
 
85
111
  - Chatting via input/output streams (e.g. via pipes).
86
112
  - .hai folder structure in a users's home directory to help track hai configuration and contexts.
87
- - Creating, listing, deleting and changing conversation contexts.
113
+ - Creating, listing, deleting, and changing conversation contexts.
88
114
  - Automatic title creation based on context
89
115
  - Custom context naming to help organize contexts
90
116
  - Behavior setting to allow for persistent chat behavior (e.g. the Do Anything Now (DAN) prompt).
91
117
  - Conversation vibing via detailed plans and HCLI integration to allow for external tool use.
92
118
  - This allows for capabilities enhancements (e.g. web search, git repo interaction, terminal use, etc.).
93
119
  - hcli-problem-details use to help relay RFC 9457 problem details back to the HCLI client.
120
+ - Assistant audio summary and commentary for the last received context response.
94
121
 
95
122
  To Do
96
123
  -----
97
124
 
125
+ - Add HAI_HOME environment variable support.
126
+ - Add individualized multiuser conversation context support.
98
127
  - A memory layer for the the AI HCLI (hai).
99
128
  - Automatic context switching per NLP on received input stream.
100
129
  - Context blending to mary different contexts.
@@ -106,7 +135,7 @@ To Do
106
135
  Bugs
107
136
  ----
108
137
 
109
- - An occasional thread exhaustion requires hcli_hai to be relaunched.
138
+ - Occasional empty response from summarizing assistant
110
139
 
111
140
  .. |build status| image:: https://circleci.com/gh/cometaj2/hcli_hai.svg?style=shield
112
141
  :target: https://circleci.com/gh/cometaj2/hcli_hai
@@ -3,7 +3,7 @@
3
3
  HCLI hai
4
4
  ========
5
5
 
6
- HCLI hai is 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.
6
+ HCLI hai is a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Ollama (remote or local), xAI, or OpenAI models via terminal input and output streams.
7
7
 
8
8
  ----
9
9
 
@@ -31,7 +31,8 @@ You'll need an HCLI Connector to run hai. For example, you can use HCLI Core (ht
31
31
  pip install hcli-core
32
32
  pip install huckle
33
33
  pip install gunicorn
34
- gunicorn --workers=1 --threads=1 -b 127.0.0.1:8000 "hcli_core:connector(\"`hcli_hai path`\")"
34
+ hcli_core cli install `hcli_hai path`
35
+ hcli_core cli run hai | bash
35
36
 
36
37
  Usage
37
38
  -----
@@ -61,17 +62,20 @@ Supports
61
62
 
62
63
  - Chatting via input/output streams (e.g. via pipes).
63
64
  - .hai folder structure in a users's home directory to help track hai configuration and contexts.
64
- - Creating, listing, deleting and changing conversation contexts.
65
+ - Creating, listing, deleting, and changing conversation contexts.
65
66
  - Automatic title creation based on context
66
67
  - Custom context naming to help organize contexts
67
68
  - Behavior setting to allow for persistent chat behavior (e.g. the Do Anything Now (DAN) prompt).
68
69
  - Conversation vibing via detailed plans and HCLI integration to allow for external tool use.
69
70
  - This allows for capabilities enhancements (e.g. web search, git repo interaction, terminal use, etc.).
70
71
  - hcli-problem-details use to help relay RFC 9457 problem details back to the HCLI client.
72
+ - Assistant audio summary and commentary for the last received context response.
71
73
 
72
74
  To Do
73
75
  -----
74
76
 
77
+ - Add HAI_HOME environment variable support.
78
+ - Add individualized multiuser conversation context support.
75
79
  - A memory layer for the the AI HCLI (hai).
76
80
  - Automatic context switching per NLP on received input stream.
77
81
  - Context blending to mary different contexts.
@@ -83,7 +87,7 @@ To Do
83
87
  Bugs
84
88
  ----
85
89
 
86
- - An occasional thread exhaustion requires hcli_hai to be relaunched.
90
+ - Occasional empty response from summarizing assistant
87
91
 
88
92
  .. |build status| image:: https://circleci.com/gh/cometaj2/hcli_hai.svg?style=shield
89
93
  :target: https://circleci.com/gh/cometaj2/hcli_hai
File without changes
@@ -0,0 +1,299 @@
1
+ import json
2
+ import io
3
+ import os
4
+ import sys
5
+ import re
6
+ import inspect
7
+ import traceback
8
+ import shutil
9
+ import time
10
+ import threading
11
+ import openai
12
+
13
+ import logger
14
+ import config as a
15
+
16
+ from ai import context as c
17
+ from ai.provider import provider as pr
18
+ from hcli_problem_details import *
19
+ from datetime import datetime
20
+
21
+ log = logger.Logger()
22
+
23
+
24
+ class AI:
25
+ _instance = None
26
+ _init_lock = threading.RLock()
27
+
28
+ def __new__(cls):
29
+ with cls._init_lock:
30
+ if cls._instance is None:
31
+ cls._instance = super().__new__(cls)
32
+ cls._instance.initialized = False
33
+ return cls._instance
34
+
35
+ def __init__(self):
36
+ if self.initialized:
37
+ return
38
+ with self._init_lock:
39
+ if self.initialized:
40
+ return
41
+ log.debug("Initializing AI singleton.")
42
+ self.rlock = threading.RLock()
43
+ self.config = None
44
+ self.contextmgr = None
45
+ self.client = None
46
+ self.config = a.Config()
47
+ self.contextmgr = c.ContextManager()
48
+ self.provider = pr.Provider()
49
+ self.initialized = True
50
+ log.debug(f"AI initialization complete: config={bool(self.config)}, contextmgr={bool(self.contextmgr)}")
51
+
52
+ # Consume the user prompt request into the context
53
+ def consume_request(self, text):
54
+ with self.rlock:
55
+ if self.config.model is not None:
56
+ if text != "":
57
+ text = text.rstrip()
58
+ question = { "role" : "user", "content" : text }
59
+ self.contextmgr.append(question)
60
+ self.contextmgr.trim()
61
+
62
+ tokens = self.contextmgr.counter.get_stats(self.contextmgr.context)
63
+ log.info("request - total context tokens: " + str(tokens['total_tokens']))
64
+
65
+ return text
66
+
67
+ # Process the completion to return a response without adding to the context.
68
+ def process_request(self):
69
+ with self.rlock:
70
+ if self.config.model is not None:
71
+ if self.contextmgr.counter.total_tokens != 0:
72
+ try:
73
+ model = self.config.model
74
+
75
+ messages = [msg for msg in self.contextmgr.messages()]
76
+ response = self.provider.client.chat.completions.create(
77
+ model=model,
78
+ messages=messages
79
+ )
80
+ return response.choices[0].message.content
81
+ except Exception as e:
82
+ log.error(traceback.format_exc())
83
+ return None
84
+ else:
85
+ return None
86
+
87
+ else:
88
+ msg = "no model selected. select from the list of available models."
89
+ log.error(msg)
90
+ raise BadRequestError(detail=msg)
91
+
92
+ # add an additional response to the chat context.
93
+ def commit_response(self, text):
94
+ with self.rlock:
95
+ self.contextmgr.append({ "role" : "assistant", "content" : text})
96
+
97
+ tokens = self.contextmgr.counter.get_stats(self.contextmgr.context)
98
+ log.info("response - total context tokens: " + str(tokens['total_tokens']))
99
+
100
+ # Auto-title after first assistant response if no title exists yet
101
+
102
+ if not self.contextmgr.title():
103
+ title = self.generate_title()
104
+ self.set_title(title)
105
+ self.contextmgr.save()
106
+
107
+ # get the current context as json output
108
+ def get_context(self):
109
+ return self.contextmgr.get_context()
110
+
111
+ # get the current context as text output
112
+ def get_readable_context(self):
113
+ return self.contextmgr.get_readable_context()
114
+
115
+ # reset the current context (clean slate)
116
+ def reset(self):
117
+ with self.rlock:
118
+ self.contextmgr.reset()
119
+ self.config.context
120
+ log.warning(f"The {self.config.context} context has been reset.")
121
+
122
+ # set the persistent context behavior (system prompt)
123
+ def behavior(self, inputstream):
124
+ with self.rlock:
125
+ self.contextmgr.behavior(inputstream)
126
+
127
+ # list all contexts
128
+ def ls(self):
129
+ contexts = []
130
+ share = self.config.dot_hai_context
131
+
132
+ # Iterate through all items in the folder
133
+ for item in os.listdir(share):
134
+ item_path = os.path.join(share, item)
135
+ # Check if the item is a directory
136
+ if os.path.isdir(item_path):
137
+
138
+ context_file = os.path.join(item_path, 'context.json')
139
+ if os.path.exists(context_file):
140
+ try:
141
+ with open(context_file, 'r') as f:
142
+ context = c.Context(json.load(f))
143
+ title = context.title
144
+ name = context.name
145
+ update_time = os.path.getmtime(context_file)
146
+ contexts.append({
147
+ 'context_id': item,
148
+ 'title': title,
149
+ 'name': name,
150
+ 'update_time': update_time
151
+ })
152
+ except json.JSONDecodeError:
153
+ # If there's an error reading the JSON, just use the directory name
154
+ contexts.append({
155
+ 'context_id': item,
156
+ 'title': 'N/A',
157
+ 'name': 'N/A',
158
+ 'update_time': os.path.getmtime(context_file) if os.path.exists(context_file) else 0
159
+ })
160
+ else:
161
+ # If context.json doesn't exist, use the directory name only
162
+ contexts.append({
163
+ 'context_id': item,
164
+ 'title': 'N/A',
165
+ 'name': 'N/A',
166
+ 'update_time': 0
167
+ })
168
+
169
+ # Sort contexts by creation time, newest at the bottom of the list
170
+ sorted_contexts = sorted(contexts, key=lambda x: x['update_time'], reverse=False)
171
+
172
+ # Format the creation time and remove it from the final output
173
+ for context in sorted_contexts:
174
+ context['update_time'] = datetime.fromtimestamp(context['update_time']).strftime('%Y-%m-%d %H:%M:%S')
175
+
176
+ return sorted_contexts
177
+
178
+ # set the current context
179
+ def set(self, context_id):
180
+ with self.rlock:
181
+ contexts = self.ls()
182
+ context_ids = [context['context_id'] for context in contexts]
183
+ if context_id in context_ids:
184
+ self.contextmgr.set(context_id)
185
+ log.info(f"context id {context_id} is set.")
186
+ else:
187
+ msg = f"provided context id {context_id} was not found in available contexts."
188
+ log.error(msg)
189
+ raise NotFoundError(detail=msg)
190
+
191
+ # create a new context
192
+ def new(self):
193
+ with self.rlock:
194
+ if not os.path.exists(self.config.dot_hai_config_file):
195
+ self.config.init()
196
+ else:
197
+ self.config.context = str(self.config.generate_id())
198
+ self.config.save()
199
+ self.contextmgr.init()
200
+ return self.current()
201
+
202
+ # delete the context_id context
203
+ def rm(self, context_id):
204
+ with self.rlock:
205
+ context_folder = os.path.join(self.config.dot_hai_context, context_id)
206
+ if os.path.exists(context_folder):
207
+ shutil.rmtree(context_folder)
208
+ log.info("Removed " + context_folder)
209
+
210
+ # get the current context ID
211
+ def current(self):
212
+ return self.config.context
213
+
214
+ def generate_title(self, max_messages: int = 4):
215
+ """
216
+ Generate a concise title (<=10 words) using only the most recent messages.
217
+ System messages are explicitly excluded so they don't interfere with the
218
+ title generation system prompt.
219
+ The title request/response is never added to the conversation context.
220
+ """
221
+ with self.rlock:
222
+ all_messages = self.contextmgr.messages()
223
+ if not all_messages:
224
+ return None
225
+
226
+ # Take only the last N messages
227
+ recent_messages = all_messages[-max_messages:]
228
+
229
+ # Explicitly remove any system messages to avoid interference
230
+ recent_messages = [
231
+ msg for msg in recent_messages if msg.get("role") != "system"
232
+ ]
233
+
234
+ if not recent_messages:
235
+ return None
236
+
237
+ title_prompt = "You are a title generator. Create a short, complete, and self-contained title that summarizes the main topic or goal of the conversation. The title must be a well-formed phrase or headline. Do not use sentence fragments, open-ended questions, or incomplete thoughts. Output about 10 words. Return only the title, nothing else."
238
+
239
+ title_messages = [*recent_messages,
240
+ { "role": "user", "content": title_prompt}
241
+ ]
242
+
243
+ try:
244
+ response = self.provider.client.chat.completions.create(
245
+ model=self.provider.get_model(),
246
+ messages=title_messages,
247
+ max_tokens=20,
248
+ temperature=0.2,
249
+ )
250
+
251
+ raw_title = response.choices[0].message.content.strip()
252
+ title = " ".join(raw_title.strip('"').split()[:10])
253
+
254
+ log.info(f"Generated title: {title}")
255
+ return title
256
+
257
+ except openai.BadRequestError as e:
258
+
259
+ # Hard coded exception for newer OpenAI models like gpt-5.6-luna that
260
+ # don't handle max_tokens nor temperature
261
+ if "Use 'max_completion_tokens'" in str(e):
262
+ log.warning("using 'max_completion_tokens' and removing 'temperature'")
263
+ try:
264
+ response = self.provider.client.chat.completions.create(
265
+ model=self.provider.get_model(),
266
+ messages=title_messages,
267
+ max_completion_tokens=20,
268
+ )
269
+
270
+ raw_title = response.choices[0].message.content.strip()
271
+ title = " ".join(raw_title.strip('"').split()[:10])
272
+
273
+ log.info(f"Generated title: {title}")
274
+ return title
275
+
276
+ except Exception as e:
277
+ raise e
278
+
279
+ except Exception as e:
280
+ raise e
281
+
282
+ # get the context name
283
+ def name(self):
284
+ return self.contextmgr.name()
285
+
286
+ # set the context name
287
+ def set_name(self, name):
288
+ with self.rlock:
289
+ self.contextmgr.set_name(name)
290
+
291
+ # get the context title
292
+ def title(self):
293
+ return self.contextmgr.title()
294
+
295
+ # set the context title
296
+ def set_title(self, title):
297
+ with self.rlock:
298
+ if title is not None:
299
+ self.contextmgr.set_title(title)
@@ -0,0 +1,196 @@
1
+ import io
2
+ import logger
3
+ import threading
4
+ import traceback
5
+ import time
6
+ import re
7
+ import os
8
+ import config as a
9
+ import openai
10
+ import numpy as np
11
+ import sounddevice as sd
12
+ from hcli_problem_details import *
13
+ from piper.voice import PiperVoice
14
+ from pathlib import Path
15
+
16
+ from ai.voice import voice as v
17
+ from ai.provider import provider as pr
18
+
19
+ log = logger.Logger()
20
+
21
+
22
+ # Singleton AssistantRunner
23
+ class AssistantRunner:
24
+ _instance = None
25
+ _init_lock = threading.RLock()
26
+
27
+ def __new__(cls):
28
+ with cls._init_lock:
29
+ if cls._instance is None:
30
+ cls._instance = super().__new__(cls)
31
+ cls._instance.initialized = False
32
+ return cls._instance
33
+
34
+ def __init__(self):
35
+ if self.initialized:
36
+ return
37
+ with self._init_lock:
38
+ if self.initialized:
39
+ return
40
+ self.rlock = threading.RLock()
41
+ self.lock = threading.RLock()
42
+ self.is_running = False
43
+ self.config = a.Config()
44
+ self.provider = pr.Provider()
45
+
46
+
47
+ self._is_assisting = False
48
+ self.initialized = True
49
+ self.terminate = False
50
+ self.assist_key = None
51
+
52
+ self.previous_response = None
53
+ self.voice = v.Voice()
54
+
55
+ self.model_path = self.config.assistant_tts_path
56
+ self.behavior = self._load_behavior()
57
+
58
+ def set_assist(self, should_assist):
59
+ if should_assist == False:
60
+ self.terminate = True
61
+ self.voice.stop()
62
+ with self.rlock:
63
+ self._is_assisting = should_assist
64
+ if should_assist is True:
65
+ self.terminate = False
66
+ log.info(f"voice assistant runner started.")
67
+ else:
68
+ self.terminate = True
69
+ self.assist_key = None
70
+ log.info(f"voice assistant runner stopped.")
71
+
72
+ def __assisted_key(self):
73
+ with self.rlock:
74
+ return self.assist_key
75
+
76
+ def __mark_assisted(self, key):
77
+ with self.rlock:
78
+ self.assist_key = key
79
+
80
+ def is_assisting(self):
81
+ with self.rlock:
82
+ return self._is_assisting
83
+
84
+ def speak(self, message):
85
+ if self.terminate:
86
+ return
87
+ self.voice.speak(message)
88
+
89
+ def __latest_pair_key(self, messages):
90
+ if not messages or len(messages) < 2:
91
+ return None
92
+ if messages[-1].get("role") != "assistant":
93
+ return None
94
+ if messages[-2].get("role") != "user":
95
+ return None
96
+ return (len(messages), messages[-1].get("content") or "")
97
+
98
+ def try_assist(self, messages):
99
+ if self.is_running or not self.is_assisting():
100
+ return False
101
+ key = self.__latest_pair_key(messages)
102
+ if key is None or key == self.__assisted_key():
103
+ return False
104
+ self.__mark_assisted(key) # before run, so a crash cannot loop
105
+ self.__run(messages)
106
+ return True
107
+
108
+ def __run(self, messages):
109
+ self.is_running = True
110
+ self.terminate = False
111
+
112
+ try:
113
+ log.info("attempting to assist.")
114
+
115
+ q_content = messages[-2]['content']
116
+ a_content = messages[-1]['content']
117
+
118
+ assistance = [{"role": "system", "content": self.behavior}]
119
+ if self.previous_response is None:
120
+ question = { "role" : "user", "content" : "user question: " + q_content + "\n\nMy assistant response to the user question and thoughts to communicate: " + a_content }
121
+ else:
122
+ question = { "role" : "user", "content" : "My previous response: " + self.previous_response + "\n\nuser question: " + q_content + "\n\nMy assistant response to the user question and thoughts to communicate: " + a_content }
123
+ assistance.append(question)
124
+
125
+ response = None
126
+ try:
127
+ model = self.config.model
128
+
129
+ if self.provider.get_provider() is not None and self.provider.get_model() is not None:
130
+ response = self.provider.client.chat.completions.create(
131
+ model=model,
132
+ messages=assistance
133
+ )
134
+ log.debug(response)
135
+ else:
136
+ msg = "no provider or model selected. select from the list of available providers and models."
137
+ log.warning(msg)
138
+
139
+ except Exception as e:
140
+ log.error(traceback.format_exc())
141
+ return None
142
+
143
+ self.check_termination()
144
+ if (response is not None):
145
+ new_response = response.choices[0].message.content
146
+ new_response = self.strip_non_conversational(new_response)
147
+ self.previous_response = new_response
148
+ self.voice.speak(new_response)
149
+
150
+ except TerminationException as e:
151
+ log.debug("assistant terminated.")
152
+ self.abort()
153
+ except Exception as e:
154
+ log.error(traceback.format_exc())
155
+ self.abort()
156
+ finally:
157
+ self.terminate = False
158
+ self.is_running = False
159
+
160
+ log.info("done assisting.")
161
+
162
+ return
163
+
164
+ # Matches '**' if followed by (\S) or preceded by (\S) a non-whitespace character
165
+ # Matches '## and ###'
166
+ # Matches ``` optional language name, newlines, content, and closing ```
167
+ def strip_non_conversational(self, text):
168
+ no_asterisks = re.sub(r'\*\*(?=\S)|(?<=\S)\*\*', '', text)
169
+ no_hashes = re.sub(r'###\s*', '', no_asterisks)
170
+ no_hashes = re.sub(r'##\s*', '', no_hashes)
171
+ no_code_blocks = re.sub(r"```[\w]*\n?.*?\n?```", '', no_hashes, flags=re.DOTALL)
172
+ return no_code_blocks
173
+
174
+ def check_termination(self):
175
+ if self.terminate:
176
+ raise TerminationException("terminated")
177
+
178
+ def pause_voice(self):
179
+ self.voice.pause()
180
+
181
+ def resume_voice(self):
182
+ self.voice.resume()
183
+
184
+ def abort(self):
185
+ self.is_running = False
186
+ self.terminate = False
187
+ self.voice.stop()
188
+
189
+ def _load_behavior(self):
190
+ path = self.config.dot_hai_assistant_file
191
+ if os.path.exists(path):
192
+ return Path(path).read_text(encoding="utf-8")
193
+ return a.ASSISTANT_MD
194
+
195
+ class TerminationException(Exception):
196
+ pass