llms-py 2.0.35__py3-none-any.whl → 3.0.0__py3-none-any.whl

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 (206) hide show
  1. llms/__pycache__/__init__.cpython-312.pyc +0 -0
  2. llms/__pycache__/__init__.cpython-313.pyc +0 -0
  3. llms/__pycache__/__init__.cpython-314.pyc +0 -0
  4. llms/__pycache__/__main__.cpython-312.pyc +0 -0
  5. llms/__pycache__/__main__.cpython-314.pyc +0 -0
  6. llms/__pycache__/llms.cpython-312.pyc +0 -0
  7. llms/__pycache__/main.cpython-312.pyc +0 -0
  8. llms/__pycache__/main.cpython-313.pyc +0 -0
  9. llms/__pycache__/main.cpython-314.pyc +0 -0
  10. llms/__pycache__/plugins.cpython-314.pyc +0 -0
  11. llms/{ui/Analytics.mjs → extensions/analytics/ui/index.mjs} +154 -238
  12. llms/extensions/app/README.md +20 -0
  13. llms/extensions/app/__init__.py +530 -0
  14. llms/extensions/app/__pycache__/__init__.cpython-314.pyc +0 -0
  15. llms/extensions/app/__pycache__/db.cpython-314.pyc +0 -0
  16. llms/extensions/app/__pycache__/db_manager.cpython-314.pyc +0 -0
  17. llms/extensions/app/db.py +644 -0
  18. llms/extensions/app/db_manager.py +195 -0
  19. llms/extensions/app/requests.json +9073 -0
  20. llms/extensions/app/threads.json +15290 -0
  21. llms/{ui → extensions/app/ui}/Recents.mjs +91 -65
  22. llms/{ui/Sidebar.mjs → extensions/app/ui/index.mjs} +124 -58
  23. llms/extensions/app/ui/threadStore.mjs +411 -0
  24. llms/extensions/core_tools/CALCULATOR.md +32 -0
  25. llms/extensions/core_tools/__init__.py +598 -0
  26. llms/extensions/core_tools/__pycache__/__init__.cpython-314.pyc +0 -0
  27. llms/extensions/core_tools/ui/codemirror/addon/edit/closebrackets.js +201 -0
  28. llms/extensions/core_tools/ui/codemirror/addon/edit/closetag.js +185 -0
  29. llms/extensions/core_tools/ui/codemirror/addon/edit/continuelist.js +101 -0
  30. llms/extensions/core_tools/ui/codemirror/addon/edit/matchbrackets.js +160 -0
  31. llms/extensions/core_tools/ui/codemirror/addon/edit/matchtags.js +66 -0
  32. llms/extensions/core_tools/ui/codemirror/addon/edit/trailingspace.js +27 -0
  33. llms/extensions/core_tools/ui/codemirror/addon/selection/active-line.js +72 -0
  34. llms/extensions/core_tools/ui/codemirror/addon/selection/mark-selection.js +119 -0
  35. llms/extensions/core_tools/ui/codemirror/addon/selection/selection-pointer.js +98 -0
  36. llms/extensions/core_tools/ui/codemirror/doc/docs.css +225 -0
  37. llms/extensions/core_tools/ui/codemirror/doc/source_sans.woff +0 -0
  38. llms/extensions/core_tools/ui/codemirror/lib/codemirror.css +344 -0
  39. llms/extensions/core_tools/ui/codemirror/lib/codemirror.js +9884 -0
  40. llms/extensions/core_tools/ui/codemirror/mode/clike/clike.js +942 -0
  41. llms/extensions/core_tools/ui/codemirror/mode/javascript/index.html +118 -0
  42. llms/extensions/core_tools/ui/codemirror/mode/javascript/javascript.js +962 -0
  43. llms/extensions/core_tools/ui/codemirror/mode/javascript/typescript.html +62 -0
  44. llms/extensions/core_tools/ui/codemirror/mode/python/python.js +402 -0
  45. llms/extensions/core_tools/ui/codemirror/theme/dracula.css +40 -0
  46. llms/extensions/core_tools/ui/codemirror/theme/mocha.css +135 -0
  47. llms/extensions/core_tools/ui/index.mjs +650 -0
  48. llms/extensions/gallery/README.md +61 -0
  49. llms/extensions/gallery/__init__.py +61 -0
  50. llms/extensions/gallery/__pycache__/__init__.cpython-314.pyc +0 -0
  51. llms/extensions/gallery/__pycache__/db.cpython-314.pyc +0 -0
  52. llms/extensions/gallery/db.py +298 -0
  53. llms/extensions/gallery/ui/index.mjs +482 -0
  54. llms/extensions/katex/README.md +39 -0
  55. llms/extensions/katex/__init__.py +6 -0
  56. llms/extensions/katex/__pycache__/__init__.cpython-314.pyc +0 -0
  57. llms/extensions/katex/ui/README.md +125 -0
  58. llms/extensions/katex/ui/contrib/auto-render.js +338 -0
  59. llms/extensions/katex/ui/contrib/auto-render.min.js +1 -0
  60. llms/extensions/katex/ui/contrib/auto-render.mjs +244 -0
  61. llms/extensions/katex/ui/contrib/copy-tex.js +127 -0
  62. llms/extensions/katex/ui/contrib/copy-tex.min.js +1 -0
  63. llms/extensions/katex/ui/contrib/copy-tex.mjs +105 -0
  64. llms/extensions/katex/ui/contrib/mathtex-script-type.js +109 -0
  65. llms/extensions/katex/ui/contrib/mathtex-script-type.min.js +1 -0
  66. llms/extensions/katex/ui/contrib/mathtex-script-type.mjs +24 -0
  67. llms/extensions/katex/ui/contrib/mhchem.js +3213 -0
  68. llms/extensions/katex/ui/contrib/mhchem.min.js +1 -0
  69. llms/extensions/katex/ui/contrib/mhchem.mjs +3109 -0
  70. llms/extensions/katex/ui/contrib/render-a11y-string.js +887 -0
  71. llms/extensions/katex/ui/contrib/render-a11y-string.min.js +1 -0
  72. llms/extensions/katex/ui/contrib/render-a11y-string.mjs +800 -0
  73. llms/extensions/katex/ui/fonts/KaTeX_AMS-Regular.ttf +0 -0
  74. llms/extensions/katex/ui/fonts/KaTeX_AMS-Regular.woff +0 -0
  75. llms/extensions/katex/ui/fonts/KaTeX_AMS-Regular.woff2 +0 -0
  76. llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Bold.ttf +0 -0
  77. llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Bold.woff +0 -0
  78. llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
  79. llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Regular.ttf +0 -0
  80. llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Regular.woff +0 -0
  81. llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
  82. llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Bold.ttf +0 -0
  83. llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Bold.woff +0 -0
  84. llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
  85. llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Regular.ttf +0 -0
  86. llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Regular.woff +0 -0
  87. llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
  88. llms/extensions/katex/ui/fonts/KaTeX_Main-Bold.ttf +0 -0
  89. llms/extensions/katex/ui/fonts/KaTeX_Main-Bold.woff +0 -0
  90. llms/extensions/katex/ui/fonts/KaTeX_Main-Bold.woff2 +0 -0
  91. llms/extensions/katex/ui/fonts/KaTeX_Main-BoldItalic.ttf +0 -0
  92. llms/extensions/katex/ui/fonts/KaTeX_Main-BoldItalic.woff +0 -0
  93. llms/extensions/katex/ui/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
  94. llms/extensions/katex/ui/fonts/KaTeX_Main-Italic.ttf +0 -0
  95. llms/extensions/katex/ui/fonts/KaTeX_Main-Italic.woff +0 -0
  96. llms/extensions/katex/ui/fonts/KaTeX_Main-Italic.woff2 +0 -0
  97. llms/extensions/katex/ui/fonts/KaTeX_Main-Regular.ttf +0 -0
  98. llms/extensions/katex/ui/fonts/KaTeX_Main-Regular.woff +0 -0
  99. llms/extensions/katex/ui/fonts/KaTeX_Main-Regular.woff2 +0 -0
  100. llms/extensions/katex/ui/fonts/KaTeX_Math-BoldItalic.ttf +0 -0
  101. llms/extensions/katex/ui/fonts/KaTeX_Math-BoldItalic.woff +0 -0
  102. llms/extensions/katex/ui/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
  103. llms/extensions/katex/ui/fonts/KaTeX_Math-Italic.ttf +0 -0
  104. llms/extensions/katex/ui/fonts/KaTeX_Math-Italic.woff +0 -0
  105. llms/extensions/katex/ui/fonts/KaTeX_Math-Italic.woff2 +0 -0
  106. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Bold.ttf +0 -0
  107. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Bold.woff +0 -0
  108. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
  109. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Italic.ttf +0 -0
  110. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Italic.woff +0 -0
  111. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
  112. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Regular.ttf +0 -0
  113. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Regular.woff +0 -0
  114. llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
  115. llms/extensions/katex/ui/fonts/KaTeX_Script-Regular.ttf +0 -0
  116. llms/extensions/katex/ui/fonts/KaTeX_Script-Regular.woff +0 -0
  117. llms/extensions/katex/ui/fonts/KaTeX_Script-Regular.woff2 +0 -0
  118. llms/extensions/katex/ui/fonts/KaTeX_Size1-Regular.ttf +0 -0
  119. llms/extensions/katex/ui/fonts/KaTeX_Size1-Regular.woff +0 -0
  120. llms/extensions/katex/ui/fonts/KaTeX_Size1-Regular.woff2 +0 -0
  121. llms/extensions/katex/ui/fonts/KaTeX_Size2-Regular.ttf +0 -0
  122. llms/extensions/katex/ui/fonts/KaTeX_Size2-Regular.woff +0 -0
  123. llms/extensions/katex/ui/fonts/KaTeX_Size2-Regular.woff2 +0 -0
  124. llms/extensions/katex/ui/fonts/KaTeX_Size3-Regular.ttf +0 -0
  125. llms/extensions/katex/ui/fonts/KaTeX_Size3-Regular.woff +0 -0
  126. llms/extensions/katex/ui/fonts/KaTeX_Size3-Regular.woff2 +0 -0
  127. llms/extensions/katex/ui/fonts/KaTeX_Size4-Regular.ttf +0 -0
  128. llms/extensions/katex/ui/fonts/KaTeX_Size4-Regular.woff +0 -0
  129. llms/extensions/katex/ui/fonts/KaTeX_Size4-Regular.woff2 +0 -0
  130. llms/extensions/katex/ui/fonts/KaTeX_Typewriter-Regular.ttf +0 -0
  131. llms/extensions/katex/ui/fonts/KaTeX_Typewriter-Regular.woff +0 -0
  132. llms/extensions/katex/ui/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
  133. llms/extensions/katex/ui/index.mjs +92 -0
  134. llms/extensions/katex/ui/katex-swap.css +1230 -0
  135. llms/extensions/katex/ui/katex-swap.min.css +1 -0
  136. llms/extensions/katex/ui/katex.css +1230 -0
  137. llms/extensions/katex/ui/katex.js +19080 -0
  138. llms/extensions/katex/ui/katex.min.css +1 -0
  139. llms/extensions/katex/ui/katex.min.js +1 -0
  140. llms/extensions/katex/ui/katex.min.mjs +1 -0
  141. llms/extensions/katex/ui/katex.mjs +18547 -0
  142. llms/extensions/providers/__init__.py +18 -0
  143. llms/extensions/providers/__pycache__/__init__.cpython-314.pyc +0 -0
  144. llms/extensions/providers/__pycache__/anthropic.cpython-314.pyc +0 -0
  145. llms/extensions/providers/__pycache__/chutes.cpython-314.pyc +0 -0
  146. llms/extensions/providers/__pycache__/google.cpython-314.pyc +0 -0
  147. llms/extensions/providers/__pycache__/nvidia.cpython-314.pyc +0 -0
  148. llms/extensions/providers/__pycache__/openai.cpython-314.pyc +0 -0
  149. llms/extensions/providers/__pycache__/openrouter.cpython-314.pyc +0 -0
  150. llms/extensions/providers/anthropic.py +229 -0
  151. llms/extensions/providers/chutes.py +155 -0
  152. llms/extensions/providers/google.py +378 -0
  153. llms/extensions/providers/nvidia.py +105 -0
  154. llms/extensions/providers/openai.py +156 -0
  155. llms/extensions/providers/openrouter.py +72 -0
  156. llms/extensions/system_prompts/README.md +22 -0
  157. llms/extensions/system_prompts/__init__.py +45 -0
  158. llms/extensions/system_prompts/__pycache__/__init__.cpython-314.pyc +0 -0
  159. llms/extensions/system_prompts/ui/index.mjs +280 -0
  160. llms/extensions/system_prompts/ui/prompts.json +1067 -0
  161. llms/extensions/tools/__init__.py +5 -0
  162. llms/extensions/tools/__pycache__/__init__.cpython-314.pyc +0 -0
  163. llms/extensions/tools/ui/index.mjs +204 -0
  164. llms/index.html +35 -77
  165. llms/llms.json +357 -1186
  166. llms/main.py +2349 -591
  167. llms/providers-extra.json +356 -0
  168. llms/providers.json +1 -0
  169. llms/ui/App.mjs +151 -60
  170. llms/ui/ai.mjs +132 -60
  171. llms/ui/app.css +2173 -161
  172. llms/ui/ctx.mjs +365 -0
  173. llms/ui/index.mjs +129 -0
  174. llms/ui/lib/charts.mjs +9 -13
  175. llms/ui/lib/servicestack-vue.mjs +3 -3
  176. llms/ui/lib/vue.min.mjs +10 -9
  177. llms/ui/lib/vue.mjs +1796 -1635
  178. llms/ui/markdown.mjs +18 -7
  179. llms/ui/modules/chat/ChatBody.mjs +691 -0
  180. llms/ui/{SettingsDialog.mjs → modules/chat/SettingsDialog.mjs} +9 -9
  181. llms/ui/modules/chat/index.mjs +828 -0
  182. llms/ui/modules/layout.mjs +243 -0
  183. llms/ui/modules/model-selector.mjs +851 -0
  184. llms/ui/tailwind.input.css +496 -80
  185. llms/ui/utils.mjs +161 -93
  186. {llms_py-2.0.35.dist-info → llms_py-3.0.0.dist-info}/METADATA +1 -1
  187. llms_py-3.0.0.dist-info/RECORD +202 -0
  188. llms/ui/Avatar.mjs +0 -85
  189. llms/ui/Brand.mjs +0 -52
  190. llms/ui/ChatPrompt.mjs +0 -590
  191. llms/ui/Main.mjs +0 -823
  192. llms/ui/ModelSelector.mjs +0 -78
  193. llms/ui/OAuthSignIn.mjs +0 -92
  194. llms/ui/ProviderIcon.mjs +0 -30
  195. llms/ui/ProviderStatus.mjs +0 -105
  196. llms/ui/SignIn.mjs +0 -64
  197. llms/ui/SystemPromptEditor.mjs +0 -31
  198. llms/ui/SystemPromptSelector.mjs +0 -56
  199. llms/ui/Welcome.mjs +0 -8
  200. llms/ui/threadStore.mjs +0 -563
  201. llms/ui.json +0 -1069
  202. llms_py-2.0.35.dist-info/RECORD +0 -48
  203. {llms_py-2.0.35.dist-info → llms_py-3.0.0.dist-info}/WHEEL +0 -0
  204. {llms_py-2.0.35.dist-info → llms_py-3.0.0.dist-info}/entry_points.txt +0 -0
  205. {llms_py-2.0.35.dist-info → llms_py-3.0.0.dist-info}/licenses/LICENSE +0 -0
  206. {llms_py-2.0.35.dist-info → llms_py-3.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,411 @@
1
+ import { ref, computed } from 'vue'
2
+ import { appendQueryString } from '@servicestack/client'
3
+
4
+ /**
5
+ * Returns an ever-increasing unique integer id.
6
+ */
7
+ export const nextId = (() => {
8
+ let last = 0 // cache of the last id that was handed out
9
+ return () => {
10
+ const now = Date.now() // current millisecond timestamp
11
+ last = (now > last) ? now : last + 1
12
+ return last
13
+ }
14
+ })();
15
+
16
+
17
+ const threads = ref([])
18
+ const currentThread = ref(null)
19
+ const isLoading = ref(false)
20
+
21
+ let ctx = null
22
+ let ext = null
23
+
24
+ function setError(error, msg = null) {
25
+ ctx?.setError(error, msg)
26
+ }
27
+
28
+ async function query(query) {
29
+ return (await ext.getJson(appendQueryString(`/threads`, query))).response || []
30
+ }
31
+
32
+ let watchThreadTimeout = ref(null)
33
+ async function watchThreadUpdates() {
34
+ const thread = currentThread.value
35
+ // console.debug('watchThreadUpdates', thread?.id, thread?.messages?.length, thread?.completedAt)
36
+ if (thread && thread?.messages?.length && !thread.completedAt) {
37
+ const api = await ext.getJson(appendQueryString(`/threads/${thread.id}/updates`, { after: thread.updatedAt }))
38
+ // console.log('watchThreadUpdates', api)
39
+ if (api.response) {
40
+ replaceThread(api.response)
41
+ return
42
+ } else {
43
+ setError(api.error, `watching thread ${thread.id}`)
44
+ }
45
+ }
46
+ stopWatchingThread()
47
+ }
48
+
49
+ function startWatchingThread() {
50
+ stopWatchingThread()
51
+ const thread = currentThread.value
52
+ if (thread && thread?.messages?.length && !thread.completedAt) {
53
+ watchThreadTimeout.value = setTimeout(watchThreadUpdates, 100)
54
+ }
55
+ }
56
+
57
+ function stopWatchingThread() {
58
+ console.debug('stopWatchingThread')
59
+ if (watchThreadTimeout.value) {
60
+ clearTimeout(watchThreadTimeout.value)
61
+ }
62
+ watchThreadTimeout.value = null
63
+ }
64
+
65
+ const isWatchingThread = computed(() => watchThreadTimeout.value != null)
66
+
67
+ async function cancelThread() {
68
+ console.log('cancelThread')
69
+ stopWatchingThread()
70
+ const thread = currentThread.value
71
+ if (!thread) return
72
+ const api = await ext.postJson(`/threads/${thread.id}/cancel`)
73
+ if (api.response) {
74
+ replaceThread(api.response)
75
+ } else {
76
+ setError(api.error, `Canceling thread ${thread.id}`)
77
+ }
78
+ }
79
+
80
+ // Create a new thread
81
+ async function createThread(args = {}) {
82
+ const thread = {
83
+ messages: [],
84
+ ...args
85
+ }
86
+ if (!thread.title) {
87
+ thread.title = 'New Chat'
88
+ }
89
+ if (thread.title.length > 200) {
90
+ thread.title = thread.title.slice(0, 200) + '...'
91
+ }
92
+
93
+ ctx.createThreadFilters.forEach(f => f(thread))
94
+
95
+ const api = await ext.postJson("/threads", thread)
96
+ if (api.response) {
97
+ threads.value.unshift(api.response)
98
+ return api.response
99
+ } else {
100
+ setError(api.error, `Creating thread ${thread.title}`)
101
+ }
102
+
103
+ return thread
104
+ }
105
+
106
+ function replaceThread(thread) {
107
+ if (!thread) {
108
+ console.error('replaceThread(null)')
109
+ return
110
+ }
111
+ const index = threads.value.findIndex(t => t.id === thread.id)
112
+ if (index !== -1) {
113
+ threads.value[index] = thread
114
+ }
115
+ if (currentThread.value?.id === thread.id) {
116
+ currentThread.value = thread
117
+ }
118
+ startWatchingThread()
119
+ return thread
120
+ }
121
+
122
+ // Update thread
123
+ async function updateThread(threadId, updates) {
124
+
125
+ if (!threadId)
126
+ throw new Error('threadId is required')
127
+
128
+ ctx.updateThreadFilters.forEach(f => f(updates))
129
+
130
+ const api = await ext.patchJson(`/threads/${threadId}`, updates)
131
+ if (api.response) {
132
+ return replaceThread(api.response)
133
+ } else {
134
+ setError(api.error, `Updating thread ${threadId}`)
135
+ }
136
+ }
137
+
138
+ async function deleteMessageFromThread(threadId, messageId) {
139
+ const thread = await getThread(threadId)
140
+ if (!thread) throw new Error('Thread not found')
141
+ const updatedMessages = thread.messages.filter(m => m.id !== messageId)
142
+ await updateThread(threadId, { messages: updatedMessages })
143
+ }
144
+
145
+ async function updateMessageInThread(threadId, messageId, updates) {
146
+ const thread = await getThread(threadId)
147
+ if (!thread) throw new Error('Thread not found')
148
+
149
+ const messageIndex = thread.messages.findIndex(m => m.id === messageId)
150
+ if (messageIndex === -1) throw new Error('Message not found')
151
+
152
+ const updatedMessages = [...thread.messages]
153
+ updatedMessages[messageIndex] = {
154
+ ...updatedMessages[messageIndex],
155
+ ...updates
156
+ }
157
+
158
+ await updateThread(threadId, { messages: updatedMessages })
159
+ }
160
+
161
+ async function redoMessageFromThread(threadId, timestamp) {
162
+ const thread = await getThread(threadId)
163
+ if (!thread) throw new Error('Thread not found')
164
+
165
+ // Find the index of the message to redo
166
+ const messageIndex = thread.messages.findIndex(m => m.timestamp === timestamp)
167
+ if (messageIndex === -1) {
168
+ setError({ message: `Message not found for timestamp ${timestamp}` })
169
+ return
170
+ }
171
+
172
+ // setError({
173
+ // errorCode: 'TestError',
174
+ // message: `Error redoing message ${timestamp} in thread ${threadId}`,
175
+ // stackTrace: `Error in page.mjs
176
+ // at Line 1
177
+ // at Line 2
178
+ // at Line 3`,
179
+ // })
180
+ // return
181
+
182
+ // Keep only messages up to and including the target message
183
+ const updatedMessages = thread.messages.slice(0, messageIndex + 1)
184
+
185
+ // Update the thread with the new messages
186
+ const request = { messages: updatedMessages }
187
+ const api = await queueChat({ request, thread })
188
+ if (api.response) {
189
+ replaceThread(api.response)
190
+ } else {
191
+ setError(api.error, `Redoing message ${timestamp} in thread ${threadId}`)
192
+ }
193
+ }
194
+
195
+ async function loadThreads() {
196
+ isLoading.value = true
197
+
198
+ try {
199
+ const api = await ext.getJson('/threads?take=50')
200
+ threads.value = api.response || []
201
+ return threads.value
202
+ } finally {
203
+ isLoading.value = false
204
+ }
205
+ }
206
+
207
+ async function getThread(threadId) {
208
+ const cachedThread = threads.value.find(t => t.id == threadId)
209
+ if (cachedThread) return cachedThread
210
+ const api = await ext.getJson(`/threads?id=${threadId}`)
211
+ return api.response && api.response[0] || null
212
+ }
213
+
214
+ // Delete thread
215
+ async function deleteThread(threadId) {
216
+ await ext.delete(`/threads/${threadId}`)
217
+
218
+ threads.value = threads.value.filter(t => t.id !== threadId)
219
+
220
+ if (currentThread.value?.id === threadId) {
221
+ currentThread.value = null
222
+ }
223
+ }
224
+
225
+ // Set current thread
226
+ async function setCurrentThread(threadId) {
227
+ const thread = await getThread(threadId)
228
+ if (thread) {
229
+ currentThread.value = thread
230
+ startWatchingThread()
231
+ }
232
+ return thread
233
+ }
234
+
235
+ // Set current thread from router params (router-aware version)
236
+ async function setCurrentThreadFromRoute(threadId, router) {
237
+ if (!threadId) {
238
+ currentThread.value = null
239
+ return null
240
+ }
241
+
242
+ const thread = setCurrentThread(threadId)
243
+ if (thread) {
244
+ return thread
245
+ } else {
246
+ // Thread not found, redirect to home
247
+ if (router) {
248
+ router.push((globalThis.ai?.base || '') + '/')
249
+ }
250
+ currentThread.value = null
251
+ return null
252
+ }
253
+ }
254
+
255
+ // Clear current thread (go back to initial state)
256
+ function clearCurrentThread() {
257
+ currentThread.value = null
258
+ }
259
+
260
+ function getGroupedThreads(total) {
261
+ const now = new Date()
262
+ const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
263
+ const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000)
264
+ const lastWeek = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000)
265
+ const lastMonth = new Date(today.getTime() - 30 * 24 * 60 * 60 * 1000)
266
+
267
+ const groups = {
268
+ today: [],
269
+ yesterday: [],
270
+ lastWeek: [],
271
+ lastMonth: [],
272
+ older: {}
273
+ }
274
+
275
+ const takeThreads = threads.value.slice(0, total)
276
+
277
+ takeThreads.forEach(thread => {
278
+ const threadDate = new Date(thread.updatedAt)
279
+
280
+ if (threadDate >= today) {
281
+ groups.today.push(thread)
282
+ } else if (threadDate >= yesterday) {
283
+ groups.yesterday.push(thread)
284
+ } else if (threadDate >= lastWeek) {
285
+ groups.lastWeek.push(thread)
286
+ } else if (threadDate >= lastMonth) {
287
+ groups.lastMonth.push(thread)
288
+ } else {
289
+ const year = threadDate.getFullYear()
290
+ const month = threadDate.toLocaleString('default', { month: 'long' })
291
+ const key = `${month} ${year}`
292
+
293
+ if (!groups.older[key]) {
294
+ groups.older[key] = []
295
+ }
296
+ groups.older[key].push(thread)
297
+ }
298
+ })
299
+
300
+ return groups
301
+ }
302
+
303
+ // Group threads by time periods
304
+ const groupedThreads = computed(() => getGroupedThreads(threads.value.length))
305
+
306
+ function getLatestCachedThread() {
307
+ return threads.value[0]
308
+ }
309
+
310
+ async function startNewThread({ title, model, redirect }) {
311
+ if (!model) {
312
+ console.error('No model selected')
313
+ return
314
+ }
315
+ if (!title) {
316
+ title = 'New Chat'
317
+ }
318
+ const latestThread = getLatestCachedThread()
319
+
320
+ console.log('startNewThread', title, model.name, ctx.router.currentRoute.value?.path, latestThread?.messages?.length)
321
+ ctx.setLayout({ left: 'ThreadsSidebar' })
322
+
323
+ if (latestThread && latestThread.title == title && !latestThread.messages?.length) {
324
+ if (ctx.router.currentRoute.value?.path != `/c/${latestThread.id}`) {
325
+ ctx.to(`/c/${latestThread.id}`)
326
+ }
327
+ return latestThread
328
+ }
329
+ const newThread = await createThread({
330
+ title,
331
+ model: model.name,
332
+ info: ctx.utils.toModelInfo(model),
333
+ })
334
+
335
+ console.log('newThread', newThread, model)
336
+ if (redirect) {
337
+ // Navigate to the new thread URL
338
+ ctx.to(`/c/${newThread.id}`)
339
+ }
340
+
341
+ // Get the thread to check for duplicates
342
+ let thread = await getThread(newThread.id)
343
+ console.log('thread', thread)
344
+ return thread
345
+ }
346
+
347
+ async function queueChat(ctxRequest, options = {}) {
348
+ if (!ctxRequest.request) return ctx.createErrorResult({ message: 'No request provided' })
349
+ if (!ctxRequest.thread) return ctx.createErrorResult({ message: 'No thread provided' })
350
+ if (!ctxRequest.request.metadata) {
351
+ ctxRequest.request.metadata = {}
352
+ }
353
+ ctx.chatRequestFilters.forEach(f => f(ctxRequest))
354
+ const { thread, request } = ctxRequest
355
+ const api = await ctx.postJson(`/ext/app/threads/${thread.id}/chat`, {
356
+ ...options,
357
+ body: typeof request == 'string'
358
+ ? request
359
+ : JSON.stringify(request),
360
+ })
361
+ return api
362
+ }
363
+
364
+ // Export the store
365
+ export function useThreadStore() {
366
+ return {
367
+ // State
368
+ threads,
369
+ currentThread,
370
+ isLoading,
371
+ groupedThreads,
372
+
373
+ // Actions
374
+ query,
375
+ createThread,
376
+ updateThread,
377
+ deleteMessageFromThread,
378
+ updateMessageInThread,
379
+ redoMessageFromThread,
380
+ loadThreads,
381
+ getThread,
382
+ deleteThread,
383
+ setCurrentThread,
384
+ setCurrentThreadFromRoute,
385
+ clearCurrentThread,
386
+ getGroupedThreads,
387
+ getLatestCachedThread,
388
+ startNewThread,
389
+ replaceThread,
390
+ queueChat,
391
+ isWatchingThread,
392
+ startWatchingThread,
393
+ stopWatchingThread,
394
+ cancelThread,
395
+ get watchingThread() {
396
+ return isWatchingThread.value
397
+ },
398
+ }
399
+ }
400
+
401
+ export default {
402
+ install(context) {
403
+ ctx = context
404
+ ext = ctx.scope('app')
405
+ ctx.setGlobals({ threads: useThreadStore() })
406
+ },
407
+
408
+ async load() {
409
+ await ctx.threads.loadThreads()
410
+ }
411
+ }
@@ -0,0 +1,32 @@
1
+ # Calculator
2
+
3
+ A powerful and safe mathematical expression evaluator with a rich web interface.
4
+
5
+ ## Features
6
+
7
+ ### 🖥️ UX Friendly Interface
8
+ Experience a clean, modern interface designed for efficiency. The UI is fully responsive and supports dark mode, seamlessly integrating with the rest of the application.
9
+
10
+ ### 💾 Persistent History
11
+ Never lose track of your calculations. The Calculator automatically saves your history to `localStorage`, ensuring your previous expressions and results are preserved between sessions.
12
+
13
+ ### ⚡ 1-Click Interaction
14
+ Streamline your workflow with interactive history items:
15
+ - **Load & Copy**: Click on any past expression or answer to instantly load it into the input field and copy it to your clipboard.
16
+ - **Visual Feedback**: Temporary checkmarks confirm successful copy actions.
17
+
18
+ ### ⌨️ Keyboard-Free Access
19
+ While full keyboard support is available, you can perform complex calculations entirely via the UI:
20
+ - **Numbers & Constants**: Quick access to digits and mathematical constants like `pi`, `e`, `inf`.
21
+ - **Operators**: Comprehensive set of buttons for arithmetic (`+`, `-`, `*`, `/`, `%`, `^`) and boolean logic (`and`, `or`, `not`).
22
+ - **Functions**: One-click insertion or wrapping of selection for all supported math functions.
23
+
24
+ ### 🐍 Python Math Support
25
+ Unlock the power of Python's math library directly in the browser.
26
+ - **Math Functions**: Support for `sin`, `cos`, `tan`, `sqrt`, `log`, `factorial`, and many more.
27
+ - **Statistics**: Built-in functions for `mean`, `median`, `stdev`, and `variance`.
28
+
29
+ ### 🛡️ Safe Evaluation
30
+ Security is a priority. Instead of using Python's unsafe `eval()`, the Calculator uses a robust **AST (Abstract Syntax Tree) evaluator**.
31
+ - **Restricted Environment**: Only allowed mathematical operations and functions are executed.
32
+ - **No Side Effects**: Prevents arbitrary code execution, making it safe to evaluate expressions from untrusted sources.