Commit
·
3c093fa
1
Parent(s):
9e1fc4c
- cloud_event.py +0 -23
- models.py +42 -47
cloud_event.py
CHANGED
|
@@ -1,23 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
from dataclasses import dataclass, asdict
|
| 3 |
-
from datetime import datetime, timezone
|
| 4 |
-
from typing import Any
|
| 5 |
-
|
| 6 |
-
@dataclass
|
| 7 |
-
class CloudEvent:
|
| 8 |
-
id: str
|
| 9 |
-
type: str
|
| 10 |
-
source: str
|
| 11 |
-
time: str
|
| 12 |
-
data: Any
|
| 13 |
-
|
| 14 |
-
@staticmethod
|
| 15 |
-
def wrap(obj: Any, *, event_type: str, source: str, id: str) -> bytes:
|
| 16 |
-
evt = CloudEvent(
|
| 17 |
-
id=id,
|
| 18 |
-
type=event_type or (obj.__class__.__name__ if obj is not None else "NullOrEmpty"),
|
| 19 |
-
source=source,
|
| 20 |
-
time=datetime.now(timezone.utc).isoformat(),
|
| 21 |
-
data=obj,
|
| 22 |
-
)
|
| 23 |
-
return json.dumps(asdict(evt), ensure_ascii=False).encode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models.py
CHANGED
|
@@ -8,6 +8,8 @@ from zoneinfo import ZoneInfo
|
|
| 8 |
import secrets
|
| 9 |
import string
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# ---------------- Nanoid-ish helper ----------------
|
| 12 |
_ALPHABET = string.ascii_letters + string.digits
|
| 13 |
def _nanoid(size: int = 21) -> str:
|
|
@@ -200,11 +202,10 @@ class UserInfo(BaseModel):
|
|
| 200 |
setattr(self, k, v)
|
| 201 |
|
| 202 |
|
| 203 |
-
# ---------- LLMServiceObj (full C# parity) ----------
|
| 204 |
class LLMServiceObj(BaseModel):
|
| 205 |
"""
|
| 206 |
Python port of NetworkMonitor.Objects.ServiceMessage.LLMServiceObj
|
| 207 |
-
|
| 208 |
"""
|
| 209 |
model_config = ConfigDict(populate_by_name=True)
|
| 210 |
|
|
@@ -236,10 +237,10 @@ class LLMServiceObj(BaseModel):
|
|
| 236 |
IsSystemLlm: bool = False
|
| 237 |
Timeout: Optional[int] = None
|
| 238 |
|
| 239 |
-
# --- complex ---
|
| 240 |
FunctionCallId: str = ""
|
| 241 |
-
|
| 242 |
-
|
| 243 |
|
| 244 |
# --- stacks ---
|
| 245 |
LlmStack: List[str] = Field(default_factory=list)
|
|
@@ -280,7 +281,7 @@ class LLMServiceObj(BaseModel):
|
|
| 280 |
self.IsUserLoggedIn = other.IsUserLoggedIn
|
| 281 |
self.JsonFunction = other.JsonFunction
|
| 282 |
|
| 283 |
-
#
|
| 284 |
self.functionState = FunctionState().SetFunctionState(
|
| 285 |
other.IsFunctionCall,
|
| 286 |
other.IsFunctionCallResponse,
|
|
@@ -313,11 +314,8 @@ class LLMServiceObj(BaseModel):
|
|
| 313 |
self.IsProcessed = other.IsProcessed
|
| 314 |
|
| 315 |
# deep copies
|
| 316 |
-
self.
|
| 317 |
-
self.
|
| 318 |
-
|
| 319 |
-
self.UserInfo = UserInfo()
|
| 320 |
-
self.UserInfo.copy_from(other.UserInfo)
|
| 321 |
|
| 322 |
self.LlmStack = list(other.LlmStack)
|
| 323 |
self.FunctionCallIdStack = list(other.FunctionCallIdStack)
|
|
@@ -325,7 +323,7 @@ class LLMServiceObj(BaseModel):
|
|
| 325 |
self.MessageIDStack = list(other.MessageIDStack)
|
| 326 |
self.IsProcessedStack = list(other.IsProcessedStack)
|
| 327 |
|
| 328 |
-
# ---------------- Function state passthroughs
|
| 329 |
def GetFunctionStateString(self) -> str:
|
| 330 |
return self.functionState.StatesString()
|
| 331 |
|
|
@@ -342,41 +340,31 @@ class LLMServiceObj(BaseModel):
|
|
| 342 |
def SetFunctionState(self, a: bool, b: bool, c: bool, d: bool, e: bool) -> None:
|
| 343 |
self.functionState.SetFunctionState(a, b, c, d, e)
|
| 344 |
|
| 345 |
-
#
|
| 346 |
@property
|
| 347 |
-
def IsFunctionCall(self) -> bool:
|
| 348 |
-
return self.functionState.IsFunctionCall
|
| 349 |
@IsFunctionCall.setter
|
| 350 |
-
def IsFunctionCall(self, v: bool) -> None:
|
| 351 |
-
self.functionState.IsFunctionCall = v
|
| 352 |
|
| 353 |
@property
|
| 354 |
-
def IsFunctionCallResponse(self) -> bool:
|
| 355 |
-
return self.functionState.IsFunctionCallResponse
|
| 356 |
@IsFunctionCallResponse.setter
|
| 357 |
-
def IsFunctionCallResponse(self, v: bool) -> None:
|
| 358 |
-
self.functionState.IsFunctionCallResponse = v
|
| 359 |
|
| 360 |
@property
|
| 361 |
-
def IsFunctionCallError(self) -> bool:
|
| 362 |
-
return self.functionState.IsFunctionCallError
|
| 363 |
@IsFunctionCallError.setter
|
| 364 |
-
def IsFunctionCallError(self, v: bool) -> None:
|
| 365 |
-
self.functionState.IsFunctionCallError = v
|
| 366 |
|
| 367 |
@property
|
| 368 |
-
def IsFunctionCallStatus(self) -> bool:
|
| 369 |
-
return self.functionState.IsFunctionCallStatus
|
| 370 |
@IsFunctionCallStatus.setter
|
| 371 |
-
def IsFunctionCallStatus(self, v: bool) -> None:
|
| 372 |
-
self.functionState.IsFunctionCallStatus = v
|
| 373 |
|
| 374 |
@property
|
| 375 |
-
def IsFunctionStillRunning(self) -> bool:
|
| 376 |
-
return self.functionState.IsFunctionStillRunning
|
| 377 |
@IsFunctionStillRunning.setter
|
| 378 |
-
def IsFunctionStillRunning(self, v: bool) -> None:
|
| 379 |
-
self.functionState.IsFunctionStillRunning = v
|
| 380 |
|
| 381 |
# ---------------- Stack helpers & routing props ----------------
|
| 382 |
def PopLlm(self) -> None:
|
|
@@ -400,27 +388,19 @@ class LLMServiceObj(BaseModel):
|
|
| 400 |
|
| 401 |
@property
|
| 402 |
def LlmChainStartName(self) -> str:
|
| 403 |
-
if not self.LlmStack
|
| 404 |
-
return self.SourceLlm
|
| 405 |
-
return self.LlmStack[0]
|
| 406 |
|
| 407 |
@property
|
| 408 |
def RootMessageID(self) -> str:
|
| 409 |
-
if not self.MessageIDStack
|
| 410 |
-
return self.MessageID
|
| 411 |
-
return self.MessageIDStack[0]
|
| 412 |
|
| 413 |
@property
|
| 414 |
def FirstFunctionName(self) -> str:
|
| 415 |
-
if not self.FunctionNameStack
|
| 416 |
-
return self.FunctionName
|
| 417 |
-
return self.FunctionNameStack[0]
|
| 418 |
|
| 419 |
@property
|
| 420 |
def IsPrimaryLlm(self) -> bool:
|
| 421 |
-
if self.IsSystemLlm
|
| 422 |
-
return False
|
| 423 |
-
return self.SourceLlm == self.DestinationLlm
|
| 424 |
|
| 425 |
def PopMessageID(self) -> None:
|
| 426 |
if self.MessageIDStack:
|
|
@@ -466,8 +446,7 @@ class LLMServiceObj(BaseModel):
|
|
| 466 |
|
| 467 |
def GetClientCurrentTime(self) -> datetime:
|
| 468 |
try:
|
| 469 |
-
|
| 470 |
-
return utc_now.astimezone(self._tz())
|
| 471 |
except Exception:
|
| 472 |
return datetime.now(timezone.utc)
|
| 473 |
|
|
@@ -489,6 +468,22 @@ class LLMServiceObj(BaseModel):
|
|
| 489 |
except Exception:
|
| 490 |
return int(datetime.now(timezone.utc).timestamp())
|
| 491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
# ---------- ResultObj ----------
|
| 494 |
class ResultObj(BaseModel):
|
|
|
|
| 8 |
import secrets
|
| 9 |
import string
|
| 10 |
|
| 11 |
+
|
| 12 |
+
|
| 13 |
# ---------------- Nanoid-ish helper ----------------
|
| 14 |
_ALPHABET = string.ascii_letters + string.digits
|
| 15 |
def _nanoid(size: int = 21) -> str:
|
|
|
|
| 202 |
setattr(self, k, v)
|
| 203 |
|
| 204 |
|
|
|
|
| 205 |
class LLMServiceObj(BaseModel):
|
| 206 |
"""
|
| 207 |
Python port of NetworkMonitor.Objects.ServiceMessage.LLMServiceObj
|
| 208 |
+
– same JSON/wire names via aliases, no Pydantic forward-ref clash.
|
| 209 |
"""
|
| 210 |
model_config = ConfigDict(populate_by_name=True)
|
| 211 |
|
|
|
|
| 237 |
IsSystemLlm: bool = False
|
| 238 |
Timeout: Optional[int] = None
|
| 239 |
|
| 240 |
+
# --- complex (use aliases to preserve JSON names) ---
|
| 241 |
FunctionCallId: str = ""
|
| 242 |
+
function_call_data: FunctionCallData = Field(default_factory=FunctionCallData, alias="FunctionCallData")
|
| 243 |
+
user_info: UserInfo = Field(default_factory=UserInfo, alias="UserInfo")
|
| 244 |
|
| 245 |
# --- stacks ---
|
| 246 |
LlmStack: List[str] = Field(default_factory=list)
|
|
|
|
| 281 |
self.IsUserLoggedIn = other.IsUserLoggedIn
|
| 282 |
self.JsonFunction = other.JsonFunction
|
| 283 |
|
| 284 |
+
# state
|
| 285 |
self.functionState = FunctionState().SetFunctionState(
|
| 286 |
other.IsFunctionCall,
|
| 287 |
other.IsFunctionCallResponse,
|
|
|
|
| 314 |
self.IsProcessed = other.IsProcessed
|
| 315 |
|
| 316 |
# deep copies
|
| 317 |
+
self.function_call_data = other.function_call_data.model_copy(deep=True)
|
| 318 |
+
self.user_info = other.user_info.model_copy(deep=True)
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
self.LlmStack = list(other.LlmStack)
|
| 321 |
self.FunctionCallIdStack = list(other.FunctionCallIdStack)
|
|
|
|
| 323 |
self.MessageIDStack = list(other.MessageIDStack)
|
| 324 |
self.IsProcessedStack = list(other.IsProcessedStack)
|
| 325 |
|
| 326 |
+
# ---------------- Function state passthroughs ----------------
|
| 327 |
def GetFunctionStateString(self) -> str:
|
| 328 |
return self.functionState.StatesString()
|
| 329 |
|
|
|
|
| 340 |
def SetFunctionState(self, a: bool, b: bool, c: bool, d: bool, e: bool) -> None:
|
| 341 |
self.functionState.SetFunctionState(a, b, c, d, e)
|
| 342 |
|
| 343 |
+
# forwarders to preserve .IsFunction* flags on the parent
|
| 344 |
@property
|
| 345 |
+
def IsFunctionCall(self) -> bool: return self.functionState.IsFunctionCall
|
|
|
|
| 346 |
@IsFunctionCall.setter
|
| 347 |
+
def IsFunctionCall(self, v: bool) -> None: self.functionState.IsFunctionCall = v
|
|
|
|
| 348 |
|
| 349 |
@property
|
| 350 |
+
def IsFunctionCallResponse(self) -> bool: return self.functionState.IsFunctionCallResponse
|
|
|
|
| 351 |
@IsFunctionCallResponse.setter
|
| 352 |
+
def IsFunctionCallResponse(self, v: bool) -> None: self.functionState.IsFunctionCallResponse = v
|
|
|
|
| 353 |
|
| 354 |
@property
|
| 355 |
+
def IsFunctionCallError(self) -> bool: return self.functionState.IsFunctionCallError
|
|
|
|
| 356 |
@IsFunctionCallError.setter
|
| 357 |
+
def IsFunctionCallError(self, v: bool) -> None: self.functionState.IsFunctionCallError = v
|
|
|
|
| 358 |
|
| 359 |
@property
|
| 360 |
+
def IsFunctionCallStatus(self) -> bool: return self.functionState.IsFunctionCallStatus
|
|
|
|
| 361 |
@IsFunctionCallStatus.setter
|
| 362 |
+
def IsFunctionCallStatus(self, v: bool) -> None: self.functionState.IsFunctionCallStatus = v
|
|
|
|
| 363 |
|
| 364 |
@property
|
| 365 |
+
def IsFunctionStillRunning(self) -> bool: return self.functionState.IsFunctionStillRunning
|
|
|
|
| 366 |
@IsFunctionStillRunning.setter
|
| 367 |
+
def IsFunctionStillRunning(self, v: bool) -> None: self.functionState.IsFunctionStillRunning = v
|
|
|
|
| 368 |
|
| 369 |
# ---------------- Stack helpers & routing props ----------------
|
| 370 |
def PopLlm(self) -> None:
|
|
|
|
| 388 |
|
| 389 |
@property
|
| 390 |
def LlmChainStartName(self) -> str:
|
| 391 |
+
return self.SourceLlm if not self.LlmStack else self.LlmStack[0]
|
|
|
|
|
|
|
| 392 |
|
| 393 |
@property
|
| 394 |
def RootMessageID(self) -> str:
|
| 395 |
+
return self.MessageID if not self.MessageIDStack else self.MessageIDStack[0]
|
|
|
|
|
|
|
| 396 |
|
| 397 |
@property
|
| 398 |
def FirstFunctionName(self) -> str:
|
| 399 |
+
return self.FunctionName if not self.FunctionNameStack else self.FunctionNameStack[0]
|
|
|
|
|
|
|
| 400 |
|
| 401 |
@property
|
| 402 |
def IsPrimaryLlm(self) -> bool:
|
| 403 |
+
return False if self.IsSystemLlm else (self.SourceLlm == self.DestinationLlm)
|
|
|
|
|
|
|
| 404 |
|
| 405 |
def PopMessageID(self) -> None:
|
| 406 |
if self.MessageIDStack:
|
|
|
|
| 446 |
|
| 447 |
def GetClientCurrentTime(self) -> datetime:
|
| 448 |
try:
|
| 449 |
+
return datetime.now(timezone.utc).astimezone(self._tz())
|
|
|
|
| 450 |
except Exception:
|
| 451 |
return datetime.now(timezone.utc)
|
| 452 |
|
|
|
|
| 468 |
except Exception:
|
| 469 |
return int(datetime.now(timezone.utc).timestamp())
|
| 470 |
|
| 471 |
+
# ---------- Optional: keep .FunctionCallData / .UserInfo attribute names ----------
|
| 472 |
+
# (handy if other code already references those names in Python)
|
| 473 |
+
@property
|
| 474 |
+
def FunctionCallData(self) -> FunctionCallData:
|
| 475 |
+
return self.function_call_data
|
| 476 |
+
@FunctionCallData.setter
|
| 477 |
+
def FunctionCallData(self, v: FunctionCallData) -> None:
|
| 478 |
+
self.function_call_data = v
|
| 479 |
+
|
| 480 |
+
@property
|
| 481 |
+
def UserInfo(self) -> UserInfo:
|
| 482 |
+
return self.user_info
|
| 483 |
+
@UserInfo.setter
|
| 484 |
+
def UserInfo(self, v: UserInfo) -> None:
|
| 485 |
+
self.user_info = v
|
| 486 |
+
|
| 487 |
|
| 488 |
# ---------- ResultObj ----------
|
| 489 |
class ResultObj(BaseModel):
|