forked from usnavy13/LibreCodeInterpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogrammatic.py
More file actions
160 lines (132 loc) · 5.36 KB
/
programmatic.py
File metadata and controls
160 lines (132 loc) · 5.36 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Models for the Programmatic Tool Calling (PTC) API.
PTC allows code running inside the sandbox to call external tools
(defined by the caller) and receive results back before continuing
execution. This enables agentic workflows where code can request
information or actions from the outside world.
"""
from typing import Any, Dict, List, Optional
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, validator
SUPPORTED_PTC_LANGUAGES = {"py", "bash"}
class PTCToolDefinition(BaseModel):
"""Definition of a tool available to sandbox code."""
name: str = Field(..., description="Tool function name")
description: str = Field(
default="", description="Human-readable description of the tool"
)
parameters: Dict[str, Any] = Field(
default_factory=dict,
description="JSON Schema describing the tool's parameters",
)
class PTCToolCall(BaseModel):
"""A tool call requested by sandbox code."""
id: str = Field(..., description="Unique identifier for this tool call")
name: str = Field(..., description="Name of the tool to call")
input: Dict[str, Any] = Field(
default_factory=dict, description="Arguments for the tool call"
)
class PTCToolResult(BaseModel):
"""Result of a tool call to be sent back to sandbox code."""
call_id: str = Field(..., description="ID of the tool call this result is for")
result: Any = Field(default=None, description="Tool call result value")
is_error: bool = Field(default=False, description="Whether the tool call errored")
error_message: Optional[str] = Field(
default=None, description="Error message if is_error is True"
)
class PTCFileInput(BaseModel):
"""File payload for PTC initial execution.
Matches the LibreChat/librechat-agents CodeEnvFile shape:
{storage_session_id, id, name}
"""
model_config = ConfigDict(populate_by_name=True)
id: str = Field(..., description="File identifier")
name: str = Field(..., description="Original filename for the referenced file")
session_id: str = Field(
...,
description="Source session for a referenced file",
validation_alias=AliasChoices("storage_session_id", "session_id"),
)
resource_id: Optional[str] = None
kind: Optional[str] = None
version: Optional[int] = None
class ProgrammaticExecRequest(BaseModel):
"""Request model for POST /exec/programmatic.
Supports two modes:
1. Initial execution: provide code + tools (+ optional session_id, etc.)
2. Continuation: provide continuation_token + tool_results
"""
# Initial execution fields
code: Optional[str] = Field(
default=None, description="Code to execute (initial request)"
)
lang: str = Field(
default="py",
description=(
"Language for the PTC sandbox: 'py' (default) or 'bash'. "
"LibreChat's BashProgrammaticToolCalling tool sends 'bash'."
),
)
tools: List[PTCToolDefinition] = Field(
default_factory=list,
description="Tools available to the code (initial request)",
)
@validator("lang")
def _validate_lang(cls, v: str) -> str:
if v not in SUPPORTED_PTC_LANGUAGES:
raise ValueError(
f"lang must be one of {sorted(SUPPORTED_PTC_LANGUAGES)}, got {v!r}"
)
return v
session_id: Optional[str] = Field(
default=None, description="Optional session ID for continuity"
)
user_id: Optional[str] = Field(default=None, description="Optional user identifier")
entity_id: Optional[str] = Field(
default=None,
description="Optional assistant/agent identifier",
max_length=40,
pattern=r"^[A-Za-z0-9_-]+$",
)
timeout: Optional[int] = Field(
default=None,
description="Execution timeout in milliseconds",
ge=1000,
le=300000,
)
files: List[PTCFileInput] = Field(
default_factory=list,
description="Referenced prior-session files to mount in the sandbox",
)
# Continuation fields
continuation_token: Optional[str] = Field(
default=None,
description="Token from a previous tool_call_required response",
)
tool_results: List[PTCToolResult] = Field(
default_factory=list,
description="Results for tool calls (continuation request)",
)
class ProgrammaticExecResponse(BaseModel):
"""Response model for POST /exec/programmatic."""
status: str = Field(
...,
description="Execution status: tool_call_required, completed, or error",
)
session_id: Optional[str] = Field(
default=None, description="Session ID for this execution"
)
continuation_token: Optional[str] = Field(
default=None,
description="Token to continue execution after providing tool results",
)
tool_calls: List[PTCToolCall] = Field(
default_factory=list,
description="Tool calls requested by the code (when status=tool_call_required)",
)
stdout: str = Field(default="", description="Standard output from code execution")
stderr: str = Field(default="", description="Standard error from code execution")
files: List[Dict[str, Any]] = Field(
default_factory=list, description="Files generated during execution"
)
error: Optional[str] = Field(
default=None, description="Error message when status=error"
)