hcli-hai 3.4.2__tar.gz → 4.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.
Files changed (84) hide show
  1. {hcli_hai-3.4.2/hcli_hai.egg-info → hcli_hai-4.1.0}/PKG-INFO +44 -13
  2. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/README.rst +8 -4
  3. hcli_hai-4.1.0/hcli_hai/cli/__pycache__/cli.cpython-314.pyc +0 -0
  4. hcli_hai-4.1.0/hcli_hai/cli/__pycache__/config.cpython-314.pyc +0 -0
  5. hcli_hai-4.1.0/hcli_hai/cli/__pycache__/logger.cpython-314.pyc +0 -0
  6. hcli_hai-4.1.0/hcli_hai/cli/__pycache__/service.cpython-314.pyc +0 -0
  7. hcli_hai-4.1.0/hcli_hai/cli/ai/__init__.py +0 -0
  8. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/__init__.cpython-314.pyc +0 -0
  9. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/ai.cpython-314.pyc +0 -0
  10. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/assistantrunner.cpython-314.pyc +0 -0
  11. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/context.cpython-314.pyc +0 -0
  12. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/orchestrator.cpython-314.pyc +0 -0
  13. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/plan.cpython-314.pyc +0 -0
  14. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/policy.cpython-314.pyc +0 -0
  15. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/router.cpython-314.pyc +0 -0
  16. hcli_hai-4.1.0/hcli_hai/cli/ai/__pycache__/runstore.cpython-314.pyc +0 -0
  17. hcli_hai-4.1.0/hcli_hai/cli/ai/ai.py +303 -0
  18. hcli_hai-4.1.0/hcli_hai/cli/ai/assistantrunner.py +196 -0
  19. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/cli/ai/context.py +66 -88
  20. hcli_hai-4.1.0/hcli_hai/cli/ai/orchestrator.py +1199 -0
  21. hcli_hai-4.1.0/hcli_hai/cli/ai/plan.py +148 -0
  22. hcli_hai-4.1.0/hcli_hai/cli/ai/policy.py +69 -0
  23. hcli_hai-4.1.0/hcli_hai/cli/ai/provider/__init__.py +0 -0
  24. hcli_hai-4.1.0/hcli_hai/cli/ai/provider/__pycache__/__init__.cpython-314.pyc +0 -0
  25. hcli_hai-4.1.0/hcli_hai/cli/ai/provider/__pycache__/provider.cpython-314.pyc +0 -0
  26. hcli_hai-4.1.0/hcli_hai/cli/ai/provider/provider.py +196 -0
  27. hcli_hai-4.1.0/hcli_hai/cli/ai/router.py +349 -0
  28. hcli_hai-4.1.0/hcli_hai/cli/ai/runstore.py +616 -0
  29. hcli_hai-4.1.0/hcli_hai/cli/ai/voice/__init__.py +0 -0
  30. hcli_hai-4.1.0/hcli_hai/cli/ai/voice/__pycache__/__init__.cpython-314.pyc +0 -0
  31. hcli_hai-4.1.0/hcli_hai/cli/ai/voice/__pycache__/audioducker.cpython-314.pyc +0 -0
  32. hcli_hai-4.1.0/hcli_hai/cli/ai/voice/__pycache__/voice.cpython-314.pyc +0 -0
  33. hcli_hai-4.1.0/hcli_hai/cli/ai/voice/audioducker.py +484 -0
  34. hcli_hai-4.1.0/hcli_hai/cli/ai/voice/voice.py +559 -0
  35. hcli_hai-4.1.0/hcli_hai/cli/cli.py +194 -0
  36. hcli_hai-4.1.0/hcli_hai/cli/config.py +254 -0
  37. hcli_hai-4.1.0/hcli_hai/cli/service.py +227 -0
  38. hcli_hai-4.1.0/hcli_hai/cli/skills/GIT.md +7 -0
  39. hcli_hai-4.1.0/hcli_hai/cli/skills/HCLI.md +8 -0
  40. hcli_hai-4.1.0/hcli_hai/cli/skills/LOCAL.md +9 -0
  41. hcli_hai-4.1.0/hcli_hai/cli/skills/WEB.md +7 -0
  42. hcli_hai-4.1.0/hcli_hai/cli/skills/__init__.py +0 -0
  43. hcli_hai-4.1.0/hcli_hai/cli/template.json +1176 -0
  44. hcli_hai-4.1.0/hcli_hai/cli/utils/__init__.py +0 -0
  45. hcli_hai-4.1.0/hcli_hai/cli/utils/__pycache__/__init__.cpython-314.pyc +0 -0
  46. hcli_hai-4.1.0/hcli_hai/cli/utils/__pycache__/formatting.cpython-314.pyc +0 -0
  47. hcli_hai-4.1.0/hcli_hai/cli/utils/__pycache__/hutils.cpython-314.pyc +0 -0
  48. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/cli/utils/formatting.py +32 -0
  49. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/data/hcli_hai.1 +5 -3
  50. hcli_hai-4.1.0/hcli_hai/package.py +13 -0
  51. {hcli_hai-3.4.2 → hcli_hai-4.1.0/hcli_hai.egg-info}/PKG-INFO +44 -13
  52. hcli_hai-4.1.0/hcli_hai.egg-info/SOURCES.txt +68 -0
  53. hcli_hai-4.1.0/hcli_hai.egg-info/requires.txt +12 -0
  54. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/pyproject.toml +9 -4
  55. hcli_hai-4.1.0/requirements.txt +12 -0
  56. hcli_hai-4.1.0/tests/test_hai.py +505 -0
  57. hcli_hai-3.4.2/hcli_hai/cli/ai/ai.py +0 -245
  58. hcli_hai-3.4.2/hcli_hai/cli/ai/behavior.py +0 -142
  59. hcli_hai-3.4.2/hcli_hai/cli/ai/model.py +0 -48
  60. hcli_hai-3.4.2/hcli_hai/cli/cli.py +0 -108
  61. hcli_hai-3.4.2/hcli_hai/cli/config.py +0 -138
  62. hcli_hai-3.4.2/hcli_hai/cli/jobqueue.py +0 -49
  63. hcli_hai-3.4.2/hcli_hai/cli/runner.py +0 -142
  64. hcli_hai-3.4.2/hcli_hai/cli/service.py +0 -148
  65. hcli_hai-3.4.2/hcli_hai/cli/template.json +0 -571
  66. hcli_hai-3.4.2/hcli_hai/cli/utils/summary.py +0 -56
  67. hcli_hai-3.4.2/hcli_hai/package.py +0 -6
  68. hcli_hai-3.4.2/hcli_hai.egg-info/SOURCES.txt +0 -34
  69. hcli_hai-3.4.2/hcli_hai.egg-info/requires.txt +0 -5
  70. hcli_hai-3.4.2/requirements.txt +0 -5
  71. hcli_hai-3.4.2/tests/test_hai.py +0 -41
  72. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/LICENSE.txt +0 -0
  73. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/MANIFEST.in +0 -0
  74. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/__init__.py +0 -0
  75. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/__main__.py +0 -0
  76. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/cli/__init__.py +0 -0
  77. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/cli/logger.py +0 -0
  78. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/cli/utils/hutils.py +0 -0
  79. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/config.py +0 -0
  80. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai/hutils.py +0 -0
  81. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai.egg-info/dependency_links.txt +0 -0
  82. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai.egg-info/entry_points.txt +0 -0
  83. {hcli_hai-3.4.2 → hcli_hai-4.1.0}/hcli_hai.egg-info/top_level.txt +0 -0
  84. {hcli_hai-3.4.2 → hcli_hai-4.1.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.1.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,29 @@ 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.8.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: scipy==1.18.1
45
+ Requires-Dist: bashlex==0.18
46
+ Requires-Dist: frontmatter==3.0.8
19
47
  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
48
+ Requires-Dist: hcli_problem_details<1.0.0,>=0.2.0
49
+ Requires-Dist: hcli_core<5.0.0,>=4.0.0
23
50
 
24
51
  |pypi| |build status| |pyver|
25
52
 
26
53
  HCLI hai
27
54
  ========
28
55
 
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.
56
+ 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
57
 
31
58
  ----
32
59
 
@@ -54,7 +81,8 @@ You'll need an HCLI Connector to run hai. For example, you can use HCLI Core (ht
54
81
  pip install hcli-core
55
82
  pip install huckle
56
83
  pip install gunicorn
57
- gunicorn --workers=1 --threads=1 -b 127.0.0.1:8000 "hcli_core:connector(\"`hcli_hai path`\")"
84
+ hcli_core cli install `hcli_hai path`
85
+ hcli_core cli run hai | bash
58
86
 
59
87
  Usage
60
88
  -----
@@ -84,17 +112,20 @@ Supports
84
112
 
85
113
  - Chatting via input/output streams (e.g. via pipes).
86
114
  - .hai folder structure in a users's home directory to help track hai configuration and contexts.
87
- - Creating, listing, deleting and changing conversation contexts.
115
+ - Creating, listing, deleting, and changing conversation contexts.
88
116
  - Automatic title creation based on context
89
117
  - Custom context naming to help organize contexts
90
118
  - Behavior setting to allow for persistent chat behavior (e.g. the Do Anything Now (DAN) prompt).
91
119
  - Conversation vibing via detailed plans and HCLI integration to allow for external tool use.
92
120
  - This allows for capabilities enhancements (e.g. web search, git repo interaction, terminal use, etc.).
93
121
  - hcli-problem-details use to help relay RFC 9457 problem details back to the HCLI client.
122
+ - Assistant audio summary and commentary for the last received context response.
94
123
 
95
124
  To Do
96
125
  -----
97
126
 
127
+ - Add HAI_HOME environment variable support.
128
+ - Add individualized multiuser conversation context support.
98
129
  - A memory layer for the the AI HCLI (hai).
99
130
  - Automatic context switching per NLP on received input stream.
100
131
  - Context blending to mary different contexts.
@@ -106,7 +137,7 @@ To Do
106
137
  Bugs
107
138
  ----
108
139
 
109
- - An occasional thread exhaustion requires hcli_hai to be relaunched.
140
+ - Occasional empty response from summarizing assistant
110
141
 
111
142
  .. |build status| image:: https://circleci.com/gh/cometaj2/hcli_hai.svg?style=shield
112
143
  :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,303 @@
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.provider.get_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.provider.get_model() is not None:
71
+ if self.contextmgr.counter.total_tokens != 0:
72
+ try:
73
+ model = self.provider.get_model()
74
+ messages = [msg for msg in self.contextmgr.messages()]
75
+ response = self.provider.client.chat.completions.create(
76
+ model=model,
77
+ messages=messages
78
+ )
79
+ return response.choices[0].message.content
80
+ except Exception as e:
81
+ log.error(traceback.format_exc())
82
+ return None
83
+ else:
84
+ return None
85
+
86
+ else:
87
+ msg = "no model selected. select from the list of available models."
88
+ log.error(msg)
89
+ raise BadRequestError(detail=msg)
90
+
91
+ # add an additional response to the chat context.
92
+ def commit_response(self, text):
93
+ with self.rlock:
94
+ self.contextmgr.append({ "role" : "assistant", "content" : text})
95
+
96
+ tokens = self.contextmgr.counter.get_stats(self.contextmgr.context)
97
+ log.info("response - total context tokens: " + str(tokens['total_tokens']))
98
+
99
+ # Auto-title after first assistant response if no title exists yet
100
+
101
+ if not self.contextmgr.title():
102
+ title = self.generate_title()
103
+ self.set_title(title)
104
+ self.contextmgr.save()
105
+
106
+ # get the current context as json output
107
+ def get_context(self):
108
+ return self.contextmgr.get_context()
109
+
110
+ # get the current context as text output
111
+ def get_readable_context(self):
112
+ return self.contextmgr.get_readable_context()
113
+
114
+ # reset the current context (clean slate)
115
+ def reset(self):
116
+ with self.rlock:
117
+ self.contextmgr.reset()
118
+ self.config.context
119
+ log.warning(f"The {self.config.context} context has been reset.")
120
+
121
+ # set the persistent context behavior (system prompt)
122
+ def behavior(self, inputstream):
123
+ with self.rlock:
124
+ self.contextmgr.behavior(inputstream)
125
+
126
+ # list all contexts
127
+ def ls(self):
128
+ contexts = []
129
+ share = self.config.dot_hai_context
130
+
131
+ # Iterate through all items in the folder
132
+ for item in os.listdir(share):
133
+ item_path = os.path.join(share, item)
134
+ # Check if the item is a directory
135
+ if os.path.isdir(item_path):
136
+
137
+ context_file = os.path.join(item_path, 'context.json')
138
+ if os.path.exists(context_file):
139
+ try:
140
+ with open(context_file, 'r') as f:
141
+ context = c.Context(json.load(f))
142
+ title = context.title
143
+ name = context.name
144
+ update_time = os.path.getmtime(context_file)
145
+ contexts.append({
146
+ 'context_id': item,
147
+ 'title': title,
148
+ 'name': name,
149
+ 'update_time': update_time
150
+ })
151
+ except json.JSONDecodeError:
152
+ # If there's an error reading the JSON, just use the directory name
153
+ contexts.append({
154
+ 'context_id': item,
155
+ 'title': 'N/A',
156
+ 'name': 'N/A',
157
+ 'update_time': os.path.getmtime(context_file) if os.path.exists(context_file) else 0
158
+ })
159
+ else:
160
+ # If context.json doesn't exist, use the directory name only
161
+ contexts.append({
162
+ 'context_id': item,
163
+ 'title': 'N/A',
164
+ 'name': 'N/A',
165
+ 'update_time': 0
166
+ })
167
+
168
+ # Sort contexts by creation time, newest at the bottom of the list
169
+ sorted_contexts = sorted(contexts, key=lambda x: x['update_time'], reverse=False)
170
+
171
+ # Format the creation time and remove it from the final output
172
+ for context in sorted_contexts:
173
+ context['update_time'] = datetime.fromtimestamp(context['update_time']).strftime('%Y-%m-%d %H:%M:%S')
174
+
175
+ return sorted_contexts
176
+
177
+ # set the current context
178
+ def set(self, context_id):
179
+ with self.rlock:
180
+ contexts = self.ls()
181
+ context_ids = [context['context_id'] for context in contexts]
182
+ if context_id in context_ids:
183
+ self.contextmgr.set(context_id)
184
+ log.info(f"context id {context_id} is set.")
185
+ else:
186
+ msg = f"provided context id {context_id} was not found in available contexts."
187
+ log.warning(msg)
188
+ raise NotFoundError(detail=msg)
189
+
190
+ # create a new context
191
+ def new(self):
192
+ with self.rlock:
193
+ if not os.path.exists(self.config.dot_hai_config_file):
194
+ self.config.init()
195
+ else:
196
+ self.config.context = str(self.config.generate_id())
197
+ self.config.save()
198
+ self.contextmgr.init()
199
+ return self.current()
200
+
201
+ # delete the context_id context
202
+ def rm(self, context_id):
203
+ with self.rlock:
204
+ context_folder = os.path.join(self.config.dot_hai_context, context_id)
205
+
206
+ if os.path.exists(context_folder):
207
+ shutil.rmtree(context_folder)
208
+ log.info("Removed " + context_folder)
209
+ else:
210
+ msg = f"provided context id {context_id} was not found in available contexts."
211
+ log.warning(msg)
212
+ raise NotFoundError(detail=msg)
213
+
214
+ # get the current context ID
215
+ def current(self):
216
+ return self.config.context
217
+
218
+ def generate_title(self, max_messages: int = 4):
219
+ """
220
+ Generate a concise title (<=10 words) using only the most recent messages.
221
+ System messages are explicitly excluded so they don't interfere with the
222
+ title generation system prompt.
223
+ The title request/response is never added to the conversation context.
224
+ """
225
+ with self.rlock:
226
+ all_messages = self.contextmgr.messages()
227
+ if not all_messages:
228
+ return None
229
+
230
+ # Take only the last N messages
231
+ recent_messages = all_messages[-max_messages:]
232
+
233
+ # Explicitly remove any system messages to avoid interference
234
+ recent_messages = [
235
+ msg for msg in recent_messages if msg.get("role") != "system"
236
+ ]
237
+
238
+ if not recent_messages:
239
+ return None
240
+
241
+ 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."
242
+
243
+ title_messages = [*recent_messages,
244
+ { "role": "user", "content": title_prompt}
245
+ ]
246
+
247
+ try:
248
+ response = self.provider.client.chat.completions.create(
249
+ model=self.provider.get_model(),
250
+ messages=title_messages,
251
+ max_tokens=20,
252
+ temperature=0.2,
253
+ )
254
+
255
+ raw_title = response.choices[0].message.content.strip()
256
+ title = " ".join(raw_title.strip('"').split()[:10])
257
+
258
+ log.info(f"Generated title: {title}")
259
+ return title
260
+
261
+ except openai.BadRequestError as e:
262
+
263
+ # Hard coded exception for newer OpenAI models like gpt-5.6-luna that
264
+ # don't handle max_tokens nor temperature
265
+ if "Use 'max_completion_tokens'" in str(e):
266
+ log.warning("using 'max_completion_tokens' and removing 'temperature'")
267
+ try:
268
+ response = self.provider.client.chat.completions.create(
269
+ model=self.provider.get_model(),
270
+ messages=title_messages,
271
+ max_completion_tokens=20,
272
+ )
273
+
274
+ raw_title = response.choices[0].message.content.strip()
275
+ title = " ".join(raw_title.strip('"').split()[:10])
276
+
277
+ log.info(f"Generated title: {title}")
278
+ return title
279
+
280
+ except Exception as e:
281
+ raise e
282
+
283
+ except Exception as e:
284
+ raise e
285
+
286
+ # get the context name
287
+ def name(self):
288
+ return self.contextmgr.name()
289
+
290
+ # set the context name
291
+ def set_name(self, name):
292
+ with self.rlock:
293
+ self.contextmgr.set_name(name)
294
+
295
+ # get the context title
296
+ def title(self):
297
+ return self.contextmgr.title()
298
+
299
+ # set the context title
300
+ def set_title(self, title):
301
+ with self.rlock:
302
+ if title is not None:
303
+ self.contextmgr.set_title(title)