forked from usnavy13/LibreCodeInterpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.py
More file actions
96 lines (78 loc) · 2.87 KB
/
exec.py
File metadata and controls
96 lines (78 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Models for the /exec endpoint compatible with LibreChat API."""
# Standard library imports
from datetime import datetime
from typing import Dict, List, Optional, Any
# Third-party imports
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, computed_field
class FileRef(BaseModel):
"""File reference model for execution response."""
model_config = ConfigDict(populate_by_name=True)
id: str
name: str
path: Optional[str] = None
session_id: Optional[str] = None
inherited: Optional[bool] = None
entity_id: Optional[str] = None
resource_id: Optional[str] = None
kind: Optional[str] = None
version: Optional[int] = None
modified_from: Optional[Dict[str, str]] = None
@computed_field # type: ignore[prop-decorator]
@property
def storage_session_id(self) -> Optional[str]:
return self.session_id
class RequestFile(BaseModel):
"""Request file model."""
model_config = ConfigDict(populate_by_name=True)
id: str
session_id: str = Field(
validation_alias=AliasChoices("storage_session_id", "session_id"),
)
name: str
entity_id: Optional[str] = None
resource_id: Optional[str] = None
kind: Optional[str] = None
version: Optional[int] = None
class ExecRequest(BaseModel):
"""Request model for /exec endpoint."""
code: str = Field(..., description="The source code to be executed")
lang: str = Field(..., description="The programming language of the code")
# Accept any JSON type for args to avoid 422s when clients send objects/arrays
args: Optional[Any] = Field(
default=None, description="Optional command line arguments (any JSON type)"
)
user_id: Optional[str] = Field(default=None, description="Optional user identifier")
entity_id: Optional[str] = Field(
default=None,
description=(
"Optional assistant/agent identifier used for session continuity "
"and shared file access"
),
max_length=40,
pattern=r"^[A-Za-z0-9_-]+$",
)
session_id: Optional[str] = Field(
default=None,
description=(
"Optional session ID to continue an existing session. For Python, "
"reusing a session continues interpreter state as well as files."
),
)
files: List[RequestFile] = Field(
default_factory=list,
description="Array of file references to be used during execution",
)
timeout: Optional[int] = Field(
default=None,
ge=1000,
le=300000,
description="Execution timeout in milliseconds",
)
class ExecResponse(BaseModel):
"""Response model for /exec endpoint - LibreChat compatible format."""
session_id: str
files: List[FileRef] = Field(default_factory=list)
stdout: str = ""
stderr: str = ""
class Config:
json_encoders = {datetime: lambda v: v.isoformat()}