Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
math_tutor_dev
public_math_tutor
Commits
d1b0dbde
Commit
d1b0dbde
authored
Mar 17, 2026
by
Kantz
Browse files
anpassung der context_sheet und tool_logging benennung
parent
c7de601a
Changes
2
Hide whitespace changes
Inline
Side-by-side
math-tutor/backend/app/deterministic_services/context_stores/context_store_base.py
View file @
d1b0dbde
...
...
@@ -3,17 +3,32 @@ from __future__ import annotations
import
hashlib
import
json
import
os
from
glob
import
glob
from
datetime
import
datetime
from
threading
import
Lock
from
typing
import
Any
from
app.deterministic_services
import
Source
from
app.deterministic_services.tool_logging
import
(
format_filename_timestamp
,
format_log_timestamp
,
)
_CACHE
:
dict
[
str
,
dict
[
str
,
Any
]]
=
{}
_LOCK
=
Lock
()
_LOG_DIR
=
os
.
path
.
join
(
"logs"
,
"context_sheets"
)
def
_latest_filename_pattern
(
chat_id
:
str
)
->
str
:
return
os
.
path
.
join
(
_LOG_DIR
,
f
"*_
{
chat_id
}
_latest.json"
)
def
_resolve_latest_path
(
chat_id
:
str
)
->
str
:
matches
=
glob
(
_latest_filename_pattern
(
chat_id
))
if
matches
:
return
max
(
matches
,
key
=
os
.
path
.
getmtime
)
def
_utc_now
()
->
str
:
return
datetime
.
now
().
strftime
(
"%Y-%m-%dT%H:%M:%SZ"
)
...
...
@@ -28,7 +43,7 @@ def touch_sheet(sheet: dict[str, Any]) -> None:
def
get_chat_id
(
messages
:
list
[
dict
],
draft
:
str
|
None
=
None
)
->
str
:
if
draft
:
return
f
"
draft
_
{
draft
}
"
return
draft
first_user
=
next
(
(
m
for
m
in
messages
if
m
.
get
(
"role"
)
==
"user"
and
m
.
get
(
"content"
)),
None
,
...
...
@@ -57,7 +72,7 @@ def load_sheet(chat_id: str) -> dict[str, Any] | None:
if
chat_id
in
_CACHE
:
return
_CACHE
[
chat_id
]
latest_path
=
os
.
path
.
join
(
_LOG_DIR
,
f
"
{
chat_id
}
_latest.json"
)
latest_path
=
_resolve_latest_path
(
chat_id
)
if
os
.
path
.
exists
(
latest_path
):
with
open
(
latest_path
,
"r"
,
encoding
=
"utf-8"
)
as
file_handle
:
sheet
=
json
.
load
(
file_handle
)
...
...
@@ -72,7 +87,9 @@ def save_sheet(sheet: dict[str, Any]) -> None:
_touch
(
sheet
)
chat_id
=
sheet
.
get
(
"chat_id"
,
"unknown"
)
latest_path
=
os
.
path
.
join
(
_LOG_DIR
,
f
"
{
chat_id
}
_latest.json"
)
timestamp
=
format_log_timestamp
(
get_created_at
(
sheet
))
filename_ts
=
format_filename_timestamp
(
timestamp
)
latest_path
=
os
.
path
.
join
(
_LOG_DIR
,
f
"context_
{
filename_ts
}
_
{
chat_id
}
_latest.json"
)
payload
=
json
.
dumps
(
sheet
,
ensure_ascii
=
False
,
indent
=
2
)
with
open
(
latest_path
,
"w"
,
encoding
=
"utf-8"
)
as
file_handle
:
...
...
math-tutor/backend/app/deterministic_services/tool_logging.py
View file @
d1b0dbde
import
json
import
os
from
datetime
import
datetime
from
datetime
import
datetime
,
timezone
def
_
format_log_timestamp
(
created_at
:
str
|
None
)
->
str
:
def
format_log_timestamp
(
created_at
:
str
|
None
)
->
str
:
if
not
created_at
:
return
datetime
.
now
(
datetime
.
timezone
.
utc
).
strftime
(
"%Y-%m-%dT%H:%M:%SZ"
)
return
datetime
.
now
(
timezone
.
utc
).
strftime
(
"%Y-%m-%dT%H:%M:%SZ"
)
try
:
parsed
=
datetime
.
strptime
(
created_at
,
"%Y-%m-%dT%H:%M:%SZ"
)
return
parsed
.
strftime
(
"%Y-%m-%dT%H:%M:%SZ"
)
...
...
@@ -13,7 +13,7 @@ def _format_log_timestamp(created_at: str | None) -> str:
return
created_at
def
_
format_filename_timestamp
(
timestamp
:
str
)
->
str
:
def
format_filename_timestamp
(
timestamp
:
str
)
->
str
:
try
:
parsed
=
datetime
.
strptime
(
timestamp
,
"%Y-%m-%dT%H:%M:%SZ"
)
return
parsed
.
strftime
(
"%Y%m%d_%H%M%S"
)
...
...
@@ -23,10 +23,10 @@ def _format_filename_timestamp(timestamp: str) -> str:
def
write_tool_log
(
entries
:
list
[
dict
],
created_at
:
str
|
None
=
None
,
chat_id
:
str
|
None
=
None
)
->
str
:
os
.
makedirs
(
"logs"
,
exist_ok
=
True
)
timestamp
=
_
format_log_timestamp
(
created_at
)
filename_ts
=
_
format_filename_timestamp
(
timestamp
)
timestamp
=
format_log_timestamp
(
created_at
)
filename_ts
=
format_filename_timestamp
(
timestamp
)
suffix
=
chat_id
or
"latest"
path
=
os
.
path
.
join
(
"logs"
,
f
"tool_calls_
{
suffix
}
_
{
filename_ts
}
.json"
)
path
=
os
.
path
.
join
(
"logs
/toolcalls
"
,
f
"tool_calls_
{
filename_ts
}
_
{
suffix
}
.json"
)
payload
=
{
"timestamp"
:
timestamp
,
"entries"
:
list
(
entries
),
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment