llms-py 2.0.9__py3-none-any.whl → 3.0.10__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.
- llms/__init__.py +4 -0
- llms/__main__.py +9 -0
- llms/db.py +359 -0
- llms/extensions/analytics/ui/index.mjs +1444 -0
- llms/extensions/app/README.md +20 -0
- llms/extensions/app/__init__.py +589 -0
- llms/extensions/app/db.py +536 -0
- {llms_py-2.0.9.data/data → llms/extensions/app}/ui/Recents.mjs +100 -73
- llms_py-2.0.9.data/data/ui/Sidebar.mjs → llms/extensions/app/ui/index.mjs +150 -79
- llms/extensions/app/ui/threadStore.mjs +433 -0
- llms/extensions/core_tools/CALCULATOR.md +32 -0
- llms/extensions/core_tools/__init__.py +637 -0
- llms/extensions/core_tools/ui/codemirror/addon/edit/closebrackets.js +201 -0
- llms/extensions/core_tools/ui/codemirror/addon/edit/closetag.js +185 -0
- llms/extensions/core_tools/ui/codemirror/addon/edit/continuelist.js +101 -0
- llms/extensions/core_tools/ui/codemirror/addon/edit/matchbrackets.js +160 -0
- llms/extensions/core_tools/ui/codemirror/addon/edit/matchtags.js +66 -0
- llms/extensions/core_tools/ui/codemirror/addon/edit/trailingspace.js +27 -0
- llms/extensions/core_tools/ui/codemirror/addon/selection/active-line.js +72 -0
- llms/extensions/core_tools/ui/codemirror/addon/selection/mark-selection.js +119 -0
- llms/extensions/core_tools/ui/codemirror/addon/selection/selection-pointer.js +98 -0
- llms/extensions/core_tools/ui/codemirror/codemirror.css +344 -0
- llms/extensions/core_tools/ui/codemirror/codemirror.js +9884 -0
- llms/extensions/core_tools/ui/codemirror/doc/docs.css +225 -0
- llms/extensions/core_tools/ui/codemirror/doc/source_sans.woff +0 -0
- llms/extensions/core_tools/ui/codemirror/mode/clike/clike.js +942 -0
- llms/extensions/core_tools/ui/codemirror/mode/javascript/index.html +118 -0
- llms/extensions/core_tools/ui/codemirror/mode/javascript/javascript.js +962 -0
- llms/extensions/core_tools/ui/codemirror/mode/javascript/typescript.html +62 -0
- llms/extensions/core_tools/ui/codemirror/mode/python/python.js +402 -0
- llms/extensions/core_tools/ui/codemirror/theme/dracula.css +40 -0
- llms/extensions/core_tools/ui/codemirror/theme/mocha.css +135 -0
- llms/extensions/core_tools/ui/index.mjs +650 -0
- llms/extensions/gallery/README.md +61 -0
- llms/extensions/gallery/__init__.py +63 -0
- llms/extensions/gallery/db.py +243 -0
- llms/extensions/gallery/ui/index.mjs +482 -0
- llms/extensions/katex/README.md +39 -0
- llms/extensions/katex/__init__.py +6 -0
- llms/extensions/katex/ui/README.md +125 -0
- llms/extensions/katex/ui/contrib/auto-render.js +338 -0
- llms/extensions/katex/ui/contrib/auto-render.min.js +1 -0
- llms/extensions/katex/ui/contrib/auto-render.mjs +244 -0
- llms/extensions/katex/ui/contrib/copy-tex.js +127 -0
- llms/extensions/katex/ui/contrib/copy-tex.min.js +1 -0
- llms/extensions/katex/ui/contrib/copy-tex.mjs +105 -0
- llms/extensions/katex/ui/contrib/mathtex-script-type.js +109 -0
- llms/extensions/katex/ui/contrib/mathtex-script-type.min.js +1 -0
- llms/extensions/katex/ui/contrib/mathtex-script-type.mjs +24 -0
- llms/extensions/katex/ui/contrib/mhchem.js +3213 -0
- llms/extensions/katex/ui/contrib/mhchem.min.js +1 -0
- llms/extensions/katex/ui/contrib/mhchem.mjs +3109 -0
- llms/extensions/katex/ui/contrib/render-a11y-string.js +887 -0
- llms/extensions/katex/ui/contrib/render-a11y-string.min.js +1 -0
- llms/extensions/katex/ui/contrib/render-a11y-string.mjs +800 -0
- llms/extensions/katex/ui/fonts/KaTeX_AMS-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_AMS-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Bold.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Bold.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Bold.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Bold.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Bold.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Bold.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Bold.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-BoldItalic.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-BoldItalic.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Italic.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Italic.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Italic.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Main-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Math-BoldItalic.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Math-BoldItalic.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Math-Italic.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Math-Italic.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Math-Italic.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Bold.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Bold.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Italic.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Italic.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Script-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Script-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Script-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size1-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size1-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size2-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size2-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size3-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size3-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size4-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size4-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Typewriter-Regular.ttf +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Typewriter-Regular.woff +0 -0
- llms/extensions/katex/ui/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- llms/extensions/katex/ui/index.mjs +92 -0
- llms/extensions/katex/ui/katex-swap.css +1230 -0
- llms/extensions/katex/ui/katex-swap.min.css +1 -0
- llms/extensions/katex/ui/katex.css +1230 -0
- llms/extensions/katex/ui/katex.js +19080 -0
- llms/extensions/katex/ui/katex.min.css +1 -0
- llms/extensions/katex/ui/katex.min.js +1 -0
- llms/extensions/katex/ui/katex.min.mjs +1 -0
- llms/extensions/katex/ui/katex.mjs +18547 -0
- llms/extensions/providers/__init__.py +22 -0
- llms/extensions/providers/anthropic.py +233 -0
- llms/extensions/providers/cerebras.py +37 -0
- llms/extensions/providers/chutes.py +153 -0
- llms/extensions/providers/google.py +481 -0
- llms/extensions/providers/nvidia.py +103 -0
- llms/extensions/providers/openai.py +154 -0
- llms/extensions/providers/openrouter.py +74 -0
- llms/extensions/providers/zai.py +182 -0
- llms/extensions/system_prompts/README.md +22 -0
- llms/extensions/system_prompts/__init__.py +45 -0
- llms/extensions/system_prompts/ui/index.mjs +280 -0
- llms/extensions/system_prompts/ui/prompts.json +1067 -0
- llms/extensions/tools/__init__.py +144 -0
- llms/extensions/tools/ui/index.mjs +706 -0
- llms/index.html +58 -0
- llms/llms.json +400 -0
- llms/main.py +4407 -0
- llms/providers-extra.json +394 -0
- llms/providers.json +1 -0
- llms/ui/App.mjs +188 -0
- llms/ui/ai.mjs +217 -0
- llms/ui/app.css +7081 -0
- llms/ui/ctx.mjs +412 -0
- llms/ui/index.mjs +131 -0
- llms/ui/lib/chart.js +14 -0
- llms/ui/lib/charts.mjs +16 -0
- llms/ui/lib/color.js +14 -0
- llms/ui/lib/servicestack-vue.mjs +37 -0
- llms/ui/lib/vue.min.mjs +13 -0
- llms/ui/lib/vue.mjs +18530 -0
- {llms_py-2.0.9.data/data → llms}/ui/markdown.mjs +33 -15
- llms/ui/modules/chat/ChatBody.mjs +976 -0
- llms/ui/modules/chat/SettingsDialog.mjs +374 -0
- llms/ui/modules/chat/index.mjs +991 -0
- llms/ui/modules/icons.mjs +46 -0
- llms/ui/modules/layout.mjs +271 -0
- llms/ui/modules/model-selector.mjs +811 -0
- llms/ui/tailwind.input.css +742 -0
- {llms_py-2.0.9.data/data → llms}/ui/typography.css +133 -7
- llms/ui/utils.mjs +261 -0
- llms_py-3.0.10.dist-info/METADATA +49 -0
- llms_py-3.0.10.dist-info/RECORD +177 -0
- llms_py-3.0.10.dist-info/entry_points.txt +2 -0
- {llms_py-2.0.9.dist-info → llms_py-3.0.10.dist-info}/licenses/LICENSE +1 -2
- llms.py +0 -1402
- llms_py-2.0.9.data/data/index.html +0 -64
- llms_py-2.0.9.data/data/llms.json +0 -447
- llms_py-2.0.9.data/data/requirements.txt +0 -1
- llms_py-2.0.9.data/data/ui/App.mjs +0 -20
- llms_py-2.0.9.data/data/ui/ChatPrompt.mjs +0 -389
- llms_py-2.0.9.data/data/ui/Main.mjs +0 -680
- llms_py-2.0.9.data/data/ui/app.css +0 -3951
- llms_py-2.0.9.data/data/ui/lib/servicestack-vue.min.mjs +0 -37
- llms_py-2.0.9.data/data/ui/lib/vue.min.mjs +0 -12
- llms_py-2.0.9.data/data/ui/tailwind.input.css +0 -261
- llms_py-2.0.9.data/data/ui/threadStore.mjs +0 -273
- llms_py-2.0.9.data/data/ui/utils.mjs +0 -114
- llms_py-2.0.9.data/data/ui.json +0 -1069
- llms_py-2.0.9.dist-info/METADATA +0 -941
- llms_py-2.0.9.dist-info/RECORD +0 -30
- llms_py-2.0.9.dist-info/entry_points.txt +0 -2
- {llms_py-2.0.9.data/data → llms}/ui/fav.svg +0 -0
- {llms_py-2.0.9.data/data → llms}/ui/lib/highlight.min.mjs +0 -0
- {llms_py-2.0.9.data/data → llms}/ui/lib/idb.min.mjs +0 -0
- {llms_py-2.0.9.data/data → llms}/ui/lib/marked.min.mjs +0 -0
- /llms_py-2.0.9.data/data/ui/lib/servicestack-client.min.mjs → /llms/ui/lib/servicestack-client.mjs +0 -0
- {llms_py-2.0.9.data/data → llms}/ui/lib/vue-router.min.mjs +0 -0
- {llms_py-2.0.9.dist-info → llms_py-3.0.10.dist-info}/WHEEL +0 -0
- {llms_py-2.0.9.dist-info → llms_py-3.0.10.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# App Extension
|
|
2
|
+
|
|
3
|
+
This extension provides the core application logic and data persistence for the LLMs platform.
|
|
4
|
+
|
|
5
|
+
## Data Storage & Architecture
|
|
6
|
+
|
|
7
|
+
### Server-Side SQLite Migration
|
|
8
|
+
The application has migrated from client-side IndexedDB storage to a robust server-side SQLite solution. This architectural shift ensures better data consistency, improved performance, and enables multi-device access to your chat history.
|
|
9
|
+
|
|
10
|
+
### Asset Management
|
|
11
|
+
To keep the database efficient and portable, binary assets (images, audio, etc.) are not stored directly in the SQLite database. Instead:
|
|
12
|
+
- All generated assets are stored in the local file system cache at `~/.llms/cache`.
|
|
13
|
+
- The database stores only **relative URLs** pointing to these assets.
|
|
14
|
+
- This approach allows for efficient caching and serving of static media.
|
|
15
|
+
|
|
16
|
+
### Concurrency Model
|
|
17
|
+
To ensure data integrity and high performance without complex locking mechanisms, the system utilizes a **single background thread** for managing all write operations to the database. This design improves concurrency handling and eliminates database locking issues during high-load scenarios.
|
|
18
|
+
|
|
19
|
+
### Multi-Tenancy & Security
|
|
20
|
+
When authentication is enabled, data isolation is automatically enforced. All core tables, including `threads` and `requests`, are scoped to the authenticated user, ensuring that users can only access their own data.
|
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import time
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from aiohttp import web
|
|
9
|
+
|
|
10
|
+
from .db import AppDB
|
|
11
|
+
|
|
12
|
+
g_db = None
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def install(ctx):
|
|
16
|
+
def get_db():
|
|
17
|
+
global g_db
|
|
18
|
+
if g_db is None and AppDB:
|
|
19
|
+
try:
|
|
20
|
+
db_path = os.path.join(ctx.get_user_path(), "app", "app.sqlite")
|
|
21
|
+
g_db = AppDB(ctx, db_path)
|
|
22
|
+
ctx.register_shutdown_handler(g_db.close)
|
|
23
|
+
|
|
24
|
+
except Exception as e:
|
|
25
|
+
ctx.err("Failed to init AppDB", e)
|
|
26
|
+
return g_db
|
|
27
|
+
|
|
28
|
+
if not get_db():
|
|
29
|
+
return
|
|
30
|
+
|
|
31
|
+
thread_fields = [
|
|
32
|
+
"id",
|
|
33
|
+
"threadId",
|
|
34
|
+
"createdAt",
|
|
35
|
+
"updatedAt",
|
|
36
|
+
"title",
|
|
37
|
+
"model",
|
|
38
|
+
"modelInfo",
|
|
39
|
+
"modalities",
|
|
40
|
+
"messages",
|
|
41
|
+
"tools",
|
|
42
|
+
"args",
|
|
43
|
+
"cost",
|
|
44
|
+
"inputTokens",
|
|
45
|
+
"outputTokens",
|
|
46
|
+
"stats",
|
|
47
|
+
"provider",
|
|
48
|
+
"providerModel",
|
|
49
|
+
"publishedAt",
|
|
50
|
+
"startedAt",
|
|
51
|
+
"completedAt",
|
|
52
|
+
"metadata",
|
|
53
|
+
"error",
|
|
54
|
+
"ref",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
def thread_dto(row):
|
|
58
|
+
return row and g_db.to_dto(
|
|
59
|
+
row,
|
|
60
|
+
[
|
|
61
|
+
"messages",
|
|
62
|
+
"tools",
|
|
63
|
+
"toolHistory",
|
|
64
|
+
"modalities",
|
|
65
|
+
"args",
|
|
66
|
+
"modelInfo",
|
|
67
|
+
"stats",
|
|
68
|
+
"metadata",
|
|
69
|
+
"providerResponse",
|
|
70
|
+
],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
def request_dto(row):
|
|
74
|
+
return row and g_db.to_dto(row, ["usage"])
|
|
75
|
+
|
|
76
|
+
def prompt_to_title(prompt):
|
|
77
|
+
return prompt[:100] + ("..." if len(prompt) > 100 else "") if prompt else None
|
|
78
|
+
|
|
79
|
+
def timestamp_messages(messages):
|
|
80
|
+
timestamp = int(time.time() * 1000)
|
|
81
|
+
for message in messages:
|
|
82
|
+
if "timestamp" not in message:
|
|
83
|
+
message["timestamp"] = timestamp
|
|
84
|
+
timestamp += 1 # make unique
|
|
85
|
+
return messages
|
|
86
|
+
|
|
87
|
+
async def query_threads(request):
|
|
88
|
+
query = request.query.copy()
|
|
89
|
+
if "fields" not in query:
|
|
90
|
+
query["fields"] = thread_fields
|
|
91
|
+
rows = g_db.query_threads(query, user=ctx.get_username(request))
|
|
92
|
+
dtos = [thread_dto(row) for row in rows]
|
|
93
|
+
return web.json_response(dtos)
|
|
94
|
+
|
|
95
|
+
ctx.add_get("threads", query_threads)
|
|
96
|
+
|
|
97
|
+
async def create_thread(request):
|
|
98
|
+
thread = await request.json()
|
|
99
|
+
id = await g_db.create_thread_async(thread, user=ctx.get_username(request))
|
|
100
|
+
row = g_db.get_thread(id, user=ctx.get_username(request))
|
|
101
|
+
return web.json_response(thread_dto(row) if row else "")
|
|
102
|
+
|
|
103
|
+
ctx.add_post("threads", create_thread)
|
|
104
|
+
|
|
105
|
+
async def get_thread(request):
|
|
106
|
+
id = request.match_info["id"]
|
|
107
|
+
row = g_db.get_thread(id, user=ctx.get_username(request))
|
|
108
|
+
return web.json_response(thread_dto(row) if row else "")
|
|
109
|
+
|
|
110
|
+
ctx.add_get("threads/{id}", get_thread)
|
|
111
|
+
|
|
112
|
+
async def update_thread(request):
|
|
113
|
+
thread = await request.json()
|
|
114
|
+
id = request.match_info["id"]
|
|
115
|
+
update_count = await g_db.update_thread_async(id, thread, user=ctx.get_username(request))
|
|
116
|
+
if update_count == 0:
|
|
117
|
+
raise Exception("Thread not found")
|
|
118
|
+
row = g_db.get_thread(id, user=ctx.get_username(request))
|
|
119
|
+
return web.json_response(thread_dto(row) if row else "")
|
|
120
|
+
|
|
121
|
+
ctx.add_patch("threads/{id}", update_thread)
|
|
122
|
+
|
|
123
|
+
async def delete_thread(request):
|
|
124
|
+
id = request.match_info["id"]
|
|
125
|
+
g_db.delete_thread(id, user=ctx.get_username(request))
|
|
126
|
+
return web.json_response({})
|
|
127
|
+
|
|
128
|
+
ctx.add_delete("threads/{id}", delete_thread)
|
|
129
|
+
|
|
130
|
+
async def queue_chat_handler(request):
|
|
131
|
+
# Check authentication if enabled
|
|
132
|
+
is_authenticated, user_data = ctx.check_auth(request)
|
|
133
|
+
if not is_authenticated:
|
|
134
|
+
return web.json_response(ctx.error_auth_required, status=401)
|
|
135
|
+
|
|
136
|
+
if not request.body_exists:
|
|
137
|
+
raise Exception("messages required")
|
|
138
|
+
|
|
139
|
+
chat = await request.json()
|
|
140
|
+
|
|
141
|
+
messages = timestamp_messages(chat.get("messages", []))
|
|
142
|
+
if len(messages) == 0:
|
|
143
|
+
raise Exception("messages required")
|
|
144
|
+
|
|
145
|
+
id = request.match_info["id"]
|
|
146
|
+
thread = thread_dto(g_db.get_thread(id, user=ctx.get_username(request)))
|
|
147
|
+
if not thread:
|
|
148
|
+
raise Exception("Thread not found")
|
|
149
|
+
|
|
150
|
+
tools = chat.get("tools", thread.get("tools", []))
|
|
151
|
+
update_thread = {
|
|
152
|
+
"messages": messages,
|
|
153
|
+
"tools": tools,
|
|
154
|
+
"startedAt": datetime.now(),
|
|
155
|
+
"completedAt": None,
|
|
156
|
+
"error": None,
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
model = chat.get("model", None)
|
|
160
|
+
if model:
|
|
161
|
+
update_thread["model"] = model
|
|
162
|
+
metadata = chat.get("metadata", {})
|
|
163
|
+
if len(metadata) > 0:
|
|
164
|
+
update_thread["metadata"] = metadata
|
|
165
|
+
if chat.get("modalities") or not thread.get("modalities"):
|
|
166
|
+
update_thread["modalities"] = chat.get("modalities", ["text"])
|
|
167
|
+
system_prompt = ctx.chat_to_system_prompt(chat)
|
|
168
|
+
if system_prompt:
|
|
169
|
+
update_thread["systemPrompt"] = system_prompt
|
|
170
|
+
|
|
171
|
+
args = thread.get("args") or {}
|
|
172
|
+
for k, v in chat.items():
|
|
173
|
+
if k in ctx.request_args:
|
|
174
|
+
args[k] = v
|
|
175
|
+
update_thread["args"] = args
|
|
176
|
+
|
|
177
|
+
# allow chat to override thread title
|
|
178
|
+
title = chat.get("title")
|
|
179
|
+
if title:
|
|
180
|
+
update_thread["title"] = title
|
|
181
|
+
else:
|
|
182
|
+
# only update thread title if it's not already set
|
|
183
|
+
title = thread.get("title")
|
|
184
|
+
if not title:
|
|
185
|
+
update_thread["title"] = title = prompt_to_title(ctx.last_user_prompt(chat))
|
|
186
|
+
|
|
187
|
+
user = ctx.get_username(request)
|
|
188
|
+
await g_db.update_thread_async(
|
|
189
|
+
id,
|
|
190
|
+
update_thread,
|
|
191
|
+
user=user,
|
|
192
|
+
)
|
|
193
|
+
thread = thread_dto(g_db.get_thread(id, user=user))
|
|
194
|
+
if not thread:
|
|
195
|
+
raise Exception("Thread not found")
|
|
196
|
+
|
|
197
|
+
metadata = thread.get("metadata") or {}
|
|
198
|
+
chat = {
|
|
199
|
+
"model": thread.get("model"),
|
|
200
|
+
"messages": thread.get("messages"),
|
|
201
|
+
"modalities": thread.get("modalities"),
|
|
202
|
+
"systemPrompt": thread.get("systemPrompt"),
|
|
203
|
+
"tools": thread.get("tools"), # tools request
|
|
204
|
+
"metadata": metadata,
|
|
205
|
+
}
|
|
206
|
+
args = thread.get("args") or {}
|
|
207
|
+
for k, v in args.items():
|
|
208
|
+
if k in ctx.request_args:
|
|
209
|
+
chat[k] = v
|
|
210
|
+
|
|
211
|
+
ctx.dbg("CHAT\n" + json.dumps(chat, indent=2))
|
|
212
|
+
|
|
213
|
+
context = {
|
|
214
|
+
"chat": chat,
|
|
215
|
+
"user": user,
|
|
216
|
+
"threadId": id,
|
|
217
|
+
"metadata": metadata,
|
|
218
|
+
"tools": metadata.get("tools", "all"), # only tools: all|none|<tool1>,<tool2>,...
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
# execute chat in background thread
|
|
222
|
+
async def run_chat(chat_req, context_req):
|
|
223
|
+
try:
|
|
224
|
+
await ctx.chat_completion(chat_req, context=context_req)
|
|
225
|
+
except Exception as ex:
|
|
226
|
+
ctx.err("run_chat", ex)
|
|
227
|
+
# not necessary to update thread in db with error as it's done in chat_error filter
|
|
228
|
+
|
|
229
|
+
asyncio.create_task(run_chat(chat, context))
|
|
230
|
+
|
|
231
|
+
return web.json_response(thread_dto(thread))
|
|
232
|
+
|
|
233
|
+
ctx.add_post("threads/{id}/chat", queue_chat_handler)
|
|
234
|
+
|
|
235
|
+
async def get_thread_updates(request):
|
|
236
|
+
id = request.match_info["id"]
|
|
237
|
+
after = request.query.get("after", None)
|
|
238
|
+
user = ctx.get_username(request)
|
|
239
|
+
thread = g_db.get_thread(id, user=user)
|
|
240
|
+
if not thread:
|
|
241
|
+
raise Exception("Thread not found")
|
|
242
|
+
if after:
|
|
243
|
+
started = time.time()
|
|
244
|
+
thread_id = thread.get("id")
|
|
245
|
+
thread_updated_at = thread.get("updatedAt")
|
|
246
|
+
|
|
247
|
+
while thread_updated_at <= after:
|
|
248
|
+
thread_updated_at = g_db.get_thread_column(thread_id, "updatedAt", user=user)
|
|
249
|
+
# if thread is not updated in 30 seconds, break
|
|
250
|
+
if time.time() - started > 10:
|
|
251
|
+
break
|
|
252
|
+
await asyncio.sleep(1)
|
|
253
|
+
ctx.dbg(f"get_thread_updates: {thread_id} / {thread_updated_at} < {after} / {thread_updated_at < after}")
|
|
254
|
+
thread = g_db.get_thread(thread_id, user=user)
|
|
255
|
+
return web.json_response(thread_dto(thread))
|
|
256
|
+
|
|
257
|
+
ctx.add_get("threads/{id}/updates", get_thread_updates)
|
|
258
|
+
|
|
259
|
+
async def cancel_thread(request):
|
|
260
|
+
id = request.match_info["id"]
|
|
261
|
+
await g_db.update_thread_async(
|
|
262
|
+
id, {"completedAt": datetime.now(), "error": "Request was canceled"}, user=ctx.get_username(request)
|
|
263
|
+
)
|
|
264
|
+
thread = g_db.get_thread(id, user=ctx.get_username(request))
|
|
265
|
+
ctx.dbg(f"cancel_thread: {id} / {thread.get('error')} / {thread.get('completedAt')}")
|
|
266
|
+
return web.json_response(thread_dto(thread))
|
|
267
|
+
|
|
268
|
+
ctx.add_post("threads/{id}/cancel", cancel_thread)
|
|
269
|
+
|
|
270
|
+
async def query_requests(request):
|
|
271
|
+
rows = g_db.query_requests(request.query, user=ctx.get_username(request))
|
|
272
|
+
dtos = [request_dto(row) for row in rows]
|
|
273
|
+
return web.json_response(dtos)
|
|
274
|
+
|
|
275
|
+
ctx.add_get("requests", query_requests)
|
|
276
|
+
|
|
277
|
+
async def delete_request(request):
|
|
278
|
+
id = request.match_info["id"]
|
|
279
|
+
g_db.delete_request(id, user=ctx.get_username(request))
|
|
280
|
+
return web.json_response({})
|
|
281
|
+
|
|
282
|
+
ctx.add_delete("requests/{id}", delete_request)
|
|
283
|
+
|
|
284
|
+
async def requests_summary(request):
|
|
285
|
+
rows = g_db.get_request_summary(user=ctx.get_username(request))
|
|
286
|
+
stats = {
|
|
287
|
+
"dailyData": {},
|
|
288
|
+
"years": [],
|
|
289
|
+
"totalCost": 0,
|
|
290
|
+
"totalRequests": 0,
|
|
291
|
+
"totalInputTokens": 0,
|
|
292
|
+
"totalOutputTokens": 0,
|
|
293
|
+
}
|
|
294
|
+
years = set()
|
|
295
|
+
for row in rows:
|
|
296
|
+
date = row["date"]
|
|
297
|
+
year = int(date[:4])
|
|
298
|
+
years.add(year)
|
|
299
|
+
stats["dailyData"][date] = {
|
|
300
|
+
"cost": row["cost"],
|
|
301
|
+
"requests": row["requests"],
|
|
302
|
+
"inputTokens": row["inputTokens"],
|
|
303
|
+
"outputTokens": row["outputTokens"],
|
|
304
|
+
}
|
|
305
|
+
stats["totalCost"] += row["cost"] or 0
|
|
306
|
+
stats["totalRequests"] += row["requests"] or 0
|
|
307
|
+
stats["totalInputTokens"] += row["inputTokens"] or 0
|
|
308
|
+
stats["totalOutputTokens"] += row["outputTokens"] or 0
|
|
309
|
+
|
|
310
|
+
stats["years"] = sorted(years)
|
|
311
|
+
return web.json_response(stats)
|
|
312
|
+
|
|
313
|
+
ctx.add_get("requests/summary", requests_summary)
|
|
314
|
+
|
|
315
|
+
async def daily_requests_summary(request):
|
|
316
|
+
day = request.match_info["day"]
|
|
317
|
+
summary = g_db.get_daily_request_summary(day, user=ctx.get_username(request))
|
|
318
|
+
return web.json_response(summary)
|
|
319
|
+
|
|
320
|
+
ctx.add_get("requests/summary/{day}", daily_requests_summary)
|
|
321
|
+
|
|
322
|
+
async def chat_request(openai_request, context):
|
|
323
|
+
chat = openai_request
|
|
324
|
+
user = context.get("user", None)
|
|
325
|
+
provider = context.get("provider", None)
|
|
326
|
+
thread_id = context.get("threadId", None)
|
|
327
|
+
model_info = context.get("modelInfo", None)
|
|
328
|
+
|
|
329
|
+
metadata = chat.get("metadata", {})
|
|
330
|
+
model = chat.get("model", None)
|
|
331
|
+
messages = timestamp_messages(chat.get("messages", []))
|
|
332
|
+
tools = chat.get("tools", [])
|
|
333
|
+
title = context.get("title") or prompt_to_title(ctx.last_user_prompt(chat) if chat else None)
|
|
334
|
+
started_at = context.get("startedAt")
|
|
335
|
+
if not started_at:
|
|
336
|
+
context["startedAt"] = started_at = datetime.now()
|
|
337
|
+
if thread_id is None:
|
|
338
|
+
thread = {
|
|
339
|
+
"user": user,
|
|
340
|
+
"model": model,
|
|
341
|
+
"provider": provider,
|
|
342
|
+
"modelInfo": model_info,
|
|
343
|
+
"title": title,
|
|
344
|
+
"messages": messages,
|
|
345
|
+
"tools": tools,
|
|
346
|
+
"systemPrompt": ctx.chat_to_system_prompt(chat),
|
|
347
|
+
"modalities": chat.get("modalities", ["text"]),
|
|
348
|
+
"startedAt": started_at,
|
|
349
|
+
"metadata": metadata,
|
|
350
|
+
}
|
|
351
|
+
thread_id = await g_db.create_thread_async(thread, user=user)
|
|
352
|
+
context["threadId"] = thread_id
|
|
353
|
+
else:
|
|
354
|
+
update_thread = {
|
|
355
|
+
"model": model,
|
|
356
|
+
"provider": provider,
|
|
357
|
+
"modelInfo": model_info,
|
|
358
|
+
"startedAt": started_at,
|
|
359
|
+
"messages": messages,
|
|
360
|
+
"tools": tools,
|
|
361
|
+
"completedAt": None,
|
|
362
|
+
"error": None,
|
|
363
|
+
"metadata": metadata,
|
|
364
|
+
}
|
|
365
|
+
await g_db.update_thread_async(thread_id, update_thread, user=user)
|
|
366
|
+
|
|
367
|
+
completed_at = g_db.get_thread_column(thread_id, "completedAt", user=user)
|
|
368
|
+
if completed_at:
|
|
369
|
+
context["completed"] = True
|
|
370
|
+
|
|
371
|
+
ctx.register_chat_request_filter(chat_request)
|
|
372
|
+
|
|
373
|
+
async def tool_request(chat_request, context):
|
|
374
|
+
messages = chat_request.get("messages", [])
|
|
375
|
+
ctx.dbg(f"tool_request: messages {len(messages)}")
|
|
376
|
+
thread_id = context.get("threadId", None)
|
|
377
|
+
if not thread_id:
|
|
378
|
+
ctx.dbg("Missing threadId")
|
|
379
|
+
return
|
|
380
|
+
user = context.get("user", None)
|
|
381
|
+
await g_db.update_thread_async(
|
|
382
|
+
thread_id,
|
|
383
|
+
{
|
|
384
|
+
"messages": messages,
|
|
385
|
+
},
|
|
386
|
+
user=user,
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
completed_at = g_db.get_thread_column(thread_id, "completedAt", user=user)
|
|
390
|
+
if completed_at:
|
|
391
|
+
context["completed"] = True
|
|
392
|
+
|
|
393
|
+
ctx.register_chat_tool_filter(tool_request)
|
|
394
|
+
|
|
395
|
+
def truncate_long_strings(obj, max_length=10000):
|
|
396
|
+
"""
|
|
397
|
+
Recursively traverse a dictionary/list structure and replace
|
|
398
|
+
string values longer than max_length with their length indicator.
|
|
399
|
+
|
|
400
|
+
Args:
|
|
401
|
+
obj: The object to process (dict, list, or other value)
|
|
402
|
+
max_length: Maximum string length before truncation (default 10000)
|
|
403
|
+
|
|
404
|
+
Returns:
|
|
405
|
+
A new object with long strings replaced by "({length})"
|
|
406
|
+
"""
|
|
407
|
+
if isinstance(obj, dict):
|
|
408
|
+
return {key: truncate_long_strings(value, max_length) for key, value in obj.items()}
|
|
409
|
+
elif isinstance(obj, list):
|
|
410
|
+
return [truncate_long_strings(item, max_length) for item in obj]
|
|
411
|
+
elif isinstance(obj, str):
|
|
412
|
+
if len(obj) > max_length:
|
|
413
|
+
return f"({len(obj)})"
|
|
414
|
+
return obj
|
|
415
|
+
else:
|
|
416
|
+
return obj
|
|
417
|
+
|
|
418
|
+
async def chat_response(openai_response, context):
|
|
419
|
+
ctx.dbg("create_response")
|
|
420
|
+
o = openai_response
|
|
421
|
+
chat = context.get("chat")
|
|
422
|
+
usage = o.get("usage", None)
|
|
423
|
+
if not usage and not chat:
|
|
424
|
+
ctx.dbg("Missing chat and usage")
|
|
425
|
+
return
|
|
426
|
+
|
|
427
|
+
user = context.get("user", None)
|
|
428
|
+
thread_id = context.get("threadId", None)
|
|
429
|
+
provider = context.get("provider", None)
|
|
430
|
+
model_info = context.get("modelInfo", None)
|
|
431
|
+
model_cost = context.get("modelCost", model_info.get("cost", None)) or {"input": 0, "output": 0}
|
|
432
|
+
duration = context.get("duration", 0)
|
|
433
|
+
|
|
434
|
+
metadata = o.get("metadata", {})
|
|
435
|
+
choices = o.get("choices", [])
|
|
436
|
+
tasks = []
|
|
437
|
+
title = context.get("title") or prompt_to_title(ctx.last_user_prompt(chat) if chat else None)
|
|
438
|
+
completed_at = datetime.now()
|
|
439
|
+
|
|
440
|
+
model = model_info.get("name") or model_info.get("id")
|
|
441
|
+
finish_reason = choices[0].get("finish_reason", None) if len(choices) > 0 else None
|
|
442
|
+
input_price = model_cost.get("input", 0)
|
|
443
|
+
output_price = model_cost.get("output", 0)
|
|
444
|
+
input_tokens = usage.get("prompt_tokens", 0)
|
|
445
|
+
output_tokens = usage.get("completion_tokens", 0)
|
|
446
|
+
total_tokens = usage.get("total_tokens", input_tokens + output_tokens)
|
|
447
|
+
cost = usage.get("cost") or o.get(
|
|
448
|
+
"cost", ((input_price * input_tokens) + (output_price * output_tokens)) / 1000000
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
request = {
|
|
452
|
+
"user": user,
|
|
453
|
+
"model": model,
|
|
454
|
+
"duration": duration,
|
|
455
|
+
"cost": cost,
|
|
456
|
+
"inputPrice": input_price,
|
|
457
|
+
"inputTokens": input_tokens,
|
|
458
|
+
"inputCachedTokens": usage.get("inputCachedTokens", 0),
|
|
459
|
+
"outputPrice": output_price,
|
|
460
|
+
"outputTokens": output_tokens,
|
|
461
|
+
"finishReason": finish_reason,
|
|
462
|
+
"provider": provider,
|
|
463
|
+
"providerModel": o.get("model", None),
|
|
464
|
+
"providerRef": o.get("provider", None),
|
|
465
|
+
"threadId": thread_id,
|
|
466
|
+
"title": title,
|
|
467
|
+
"startedAt": context.get("startedAt"),
|
|
468
|
+
"totalTokens": total_tokens,
|
|
469
|
+
"usage": usage,
|
|
470
|
+
"completedAt": completed_at,
|
|
471
|
+
"ref": o.get("id", None),
|
|
472
|
+
}
|
|
473
|
+
tasks.append(g_db.create_request_async(request, user=user))
|
|
474
|
+
|
|
475
|
+
if thread_id:
|
|
476
|
+
messages = chat.get("messages", [])
|
|
477
|
+
last_role = messages[-1].get("role", None) if len(messages) > 0 else None
|
|
478
|
+
if last_role == "user" or last_role == "tool":
|
|
479
|
+
user_message = messages[-1]
|
|
480
|
+
user_message["model"] = model
|
|
481
|
+
user_message["usage"] = {
|
|
482
|
+
"tokens": input_tokens,
|
|
483
|
+
"price": input_price,
|
|
484
|
+
"cost": (input_price * input_tokens) / 1000000,
|
|
485
|
+
}
|
|
486
|
+
else:
|
|
487
|
+
ctx.dbg(
|
|
488
|
+
f"Missing user message for thread {thread_id}, {len(messages)} messages, last role: {last_role}"
|
|
489
|
+
)
|
|
490
|
+
assistant_message = ctx.chat_response_to_message(o)
|
|
491
|
+
assistant_message["model"] = model
|
|
492
|
+
assistant_message["usage"] = {
|
|
493
|
+
"tokens": output_tokens,
|
|
494
|
+
"price": output_price,
|
|
495
|
+
"cost": (output_price * output_tokens) / 1000000,
|
|
496
|
+
"duration": duration,
|
|
497
|
+
}
|
|
498
|
+
messages.append(assistant_message)
|
|
499
|
+
|
|
500
|
+
tools = chat.get("tools", [])
|
|
501
|
+
update_thread = {
|
|
502
|
+
"model": model,
|
|
503
|
+
"providerModel": o.get("model"),
|
|
504
|
+
"modelInfo": model_info,
|
|
505
|
+
"messages": messages,
|
|
506
|
+
"tools": tools,
|
|
507
|
+
"completedAt": completed_at,
|
|
508
|
+
}
|
|
509
|
+
tool_history = o.get("tool_history", None)
|
|
510
|
+
if tool_history:
|
|
511
|
+
update_thread["toolHistory"] = tool_history
|
|
512
|
+
if "error" in metadata:
|
|
513
|
+
update_thread["error"] = metadata["error"]
|
|
514
|
+
provider_response = context.get("providerResponse", None)
|
|
515
|
+
if provider_response:
|
|
516
|
+
update_thread["providerResponse"] = truncate_long_strings(provider_response)
|
|
517
|
+
tasks.append(g_db.update_thread_async(thread_id, update_thread, user=user))
|
|
518
|
+
else:
|
|
519
|
+
ctx.dbg("Missing thread_id")
|
|
520
|
+
|
|
521
|
+
await asyncio.gather(*tasks)
|
|
522
|
+
|
|
523
|
+
# Update thread costs from all thread requests
|
|
524
|
+
thread_requests = g_db.query_requests({"threadId": thread_id}, user=user)
|
|
525
|
+
total_costs = 0
|
|
526
|
+
total_input = 0
|
|
527
|
+
total_output = 0
|
|
528
|
+
for request in thread_requests:
|
|
529
|
+
total_costs += request.get("cost", 0) or 0
|
|
530
|
+
total_input += request.get("inputTokens", 0) or 0
|
|
531
|
+
total_output += request.get("outputTokens", 0) or 0
|
|
532
|
+
stats = {
|
|
533
|
+
"inputTokens": total_input,
|
|
534
|
+
"outputTokens": total_output,
|
|
535
|
+
"cost": total_costs,
|
|
536
|
+
"duration": duration,
|
|
537
|
+
"requests": len(thread_requests),
|
|
538
|
+
}
|
|
539
|
+
g_db.update_thread(
|
|
540
|
+
thread_id,
|
|
541
|
+
{
|
|
542
|
+
"inputTokens": total_input,
|
|
543
|
+
"outputTokens": total_output,
|
|
544
|
+
"cost": total_costs,
|
|
545
|
+
"stats": stats,
|
|
546
|
+
},
|
|
547
|
+
user=user,
|
|
548
|
+
)
|
|
549
|
+
|
|
550
|
+
ctx.register_chat_response_filter(chat_response)
|
|
551
|
+
|
|
552
|
+
async def chat_error(e: Exception, context: Any):
|
|
553
|
+
error = ctx.error_message(e)
|
|
554
|
+
ctx.dbg(f"Chat error: {error}")
|
|
555
|
+
chat = context.get("chat")
|
|
556
|
+
if not chat:
|
|
557
|
+
ctx.dbg("Missing chat")
|
|
558
|
+
return
|
|
559
|
+
|
|
560
|
+
title = context.get("title") or prompt_to_title(ctx.last_user_prompt(chat) if chat else None)
|
|
561
|
+
completed_at = datetime.now()
|
|
562
|
+
user = context.get("user", None)
|
|
563
|
+
|
|
564
|
+
thread_id = context.get("threadId", None)
|
|
565
|
+
tasks = []
|
|
566
|
+
if thread_id:
|
|
567
|
+
tasks.append(g_db.update_thread_async(thread_id, {"completedAt": completed_at, "error": error}, user=user))
|
|
568
|
+
else:
|
|
569
|
+
ctx.dbg("Missing threadId")
|
|
570
|
+
|
|
571
|
+
request = {
|
|
572
|
+
"user": user,
|
|
573
|
+
"model": chat.get("model", None),
|
|
574
|
+
"title": title,
|
|
575
|
+
"threadId": thread_id,
|
|
576
|
+
"startedAt": context.get("startedAt"),
|
|
577
|
+
"completedAt": completed_at,
|
|
578
|
+
"error": error,
|
|
579
|
+
"stackTrace": context.get("stackTrace", None),
|
|
580
|
+
}
|
|
581
|
+
tasks.append(g_db.create_request_async(request, user=user))
|
|
582
|
+
|
|
583
|
+
if len(tasks) > 0:
|
|
584
|
+
await asyncio.gather(*tasks)
|
|
585
|
+
|
|
586
|
+
ctx.register_chat_error_filter(chat_error)
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
__install__ = install
|