@@ -32,13 +32,15 @@ use openbitfun_agent_tools::{
3232 build_tool_execution_timeout_presentation,
3333 build_user_rejected_tool_presentation_with_instruction,
3434 build_user_steering_interrupted_presentation, build_write_tail_closure_notice,
35- render_ptc_loaded_tool_typescript_sdk , render_tool_result_for_assistant,
35+ render_ptc_loaded_tool_sdk , render_tool_result_for_assistant,
3636 validate_tool_execution_admission, DynamicToolInfo , LoadedDeferredToolSpec , PermissionIntent ,
3737 PtcToolContract , ResolvedToolInvocation , ToolExecutionAdmissionRejection ,
3838 ToolExecutionAdmissionRequest , ToolExecutionErrorPresentation , CALL_DEFERRED_TOOL_NAME ,
39- GET_TOOL_SPEC_TOOL_NAME , PTC_NESTED_ALLOWED_TOOLS_CONTEXT_KEY ,
39+ GET_TOOL_SPEC_TOOL_NAME , PTC_LANGUAGE_CONTEXT_KEY , PTC_NESTED_ALLOWED_TOOLS_CONTEXT_KEY ,
4040 USER_STEERING_INTERRUPTED_MESSAGE ,
4141} ;
42+ #[ cfg( feature = "ptc-runtime" ) ]
43+ use openbitfun_core_types:: PtcLanguage ;
4244use openbitfun_runtime_ports:: {
4345 PermissionReply , PermissionRequest , PermissionRequestSource , PermissionRequestSourceKind ,
4446 PermissionResourceCaseSensitivity , RoundInjectionToolPreemption ,
@@ -50,6 +52,8 @@ use openbitfun_runtime_ports::{
5052} ;
5153#[ cfg( feature = "ptc-runtime" ) ]
5254use openbitfun_services_integrations:: ptc_runtime:: NodePtcRuntime ;
55+ #[ cfg( feature = "ptc-runtime" ) ]
56+ use openbitfun_services_integrations:: python_ptc_runtime:: PythonPtcRuntime ;
5357use std:: collections:: { HashMap , HashSet } ;
5458use std:: sync:: Arc ;
5559use std:: time:: { Instant , SystemTime } ;
@@ -183,9 +187,13 @@ fn convert_tool_result_with_metadata(
183187 // "completed successfully" can hide fields the model needs for the
184188 // next decision.
185189 let assistant_text = result_for_assistant. or_else ( || {
190+ let mut assistant_visible_data = assistant_data. clone ( ) ;
191+ if let Some ( object) = assistant_visible_data. as_object_mut ( ) {
192+ object. remove ( "__ptc_language" ) ;
193+ }
186194 Some ( render_tool_result_for_assistant (
187195 effective_tool_name,
188- & assistant_data ,
196+ & assistant_visible_data ,
189197 ) )
190198 } ) ;
191199 // Tools with a typed response envelope can report a recoverable
@@ -754,6 +762,14 @@ struct PtcPipelineToolCallHandler {
754762 loaded_deferred_tool_specs : Arc < TokioMutex < Vec < LoadedDeferredToolSpec > > > ,
755763}
756764
765+ #[ cfg( feature = "ptc-runtime" ) ]
766+ fn build_ptc_tool_spec_response ( language : PtcLanguage , sdk_source : String ) -> serde_json:: Value {
767+ serde_json:: json!( {
768+ "language" : language. as_str( ) ,
769+ "sdk_source" : sdk_source,
770+ } )
771+ }
772+
757773#[ cfg( feature = "ptc-runtime" ) ]
758774#[ async_trait:: async_trait]
759775impl PtcToolCallHandler for PtcPipelineToolCallHandler {
@@ -886,12 +902,20 @@ impl PtcToolCallHandler for PtcPipelineToolCallHandler {
886902 . cloned ( )
887903 . unwrap_or_else ( || serde_json:: json!( { } ) ) ;
888904 let output_schema = tool. output_schema ( ) ;
889- let typescript_sdk = render_ptc_loaded_tool_typescript_sdk ( & PtcToolContract :: new (
890- tool_name. clone ( ) ,
891- description,
892- input_schema,
893- output_schema,
894- ) ) ;
905+ let language = if self
906+ . context
907+ . context_vars
908+ . get ( PTC_LANGUAGE_CONTEXT_KEY )
909+ . is_some_and ( |language| language. eq_ignore_ascii_case ( "python" ) )
910+ {
911+ PtcLanguage :: Python
912+ } else {
913+ PtcLanguage :: TypeScript
914+ } ;
915+ let sdk_source = render_ptc_loaded_tool_sdk (
916+ & PtcToolContract :: new ( tool_name. clone ( ) , description, input_schema, output_schema) ,
917+ language,
918+ ) ;
895919
896920 let mut loaded = self . loaded_deferred_tool_specs . lock ( ) . await ;
897921 if let Some ( existing) = loaded. iter_mut ( ) . find ( |spec| spec. tool_name == tool_name) {
@@ -902,9 +926,7 @@ impl PtcToolCallHandler for PtcPipelineToolCallHandler {
902926 catalog_generation,
903927 } ) ;
904928 }
905- Ok ( serde_json:: json!( {
906- "typescript_sdk" : typescript_sdk,
907- } ) )
929+ Ok ( build_ptc_tool_spec_response ( language, sdk_source) )
908930 }
909931}
910932
@@ -2974,7 +2996,12 @@ impl ToolPipeline {
29742996 . effective_arguments ( )
29752997 . get ( "timeout_ms" )
29762998 . and_then ( serde_json:: Value :: as_u64) ;
2977- let runtime = if context. is_remote ( ) {
2999+ let use_python = context
3000+ . custom_data
3001+ . get ( PTC_LANGUAGE_CONTEXT_KEY )
3002+ . and_then ( serde_json:: Value :: as_str)
3003+ . is_some_and ( |language| language. eq_ignore_ascii_case ( "python" ) ) ;
3004+ let runtime: Box < dyn PtcRuntime > = if context. is_remote ( ) {
29783005 #[ cfg( feature = "ssh-remote" ) ]
29793006 {
29803007 let workspace = context. workspace . as_ref ( ) . ok_or_else ( || {
@@ -3002,7 +3029,11 @@ impl ToolPipeline {
30023029 "Remote SSH services are not initialized" . to_string ( ) ,
30033030 )
30043031 } ) ?;
3005- NodePtcRuntime :: for_remote ( ssh_manager, connection_id)
3032+ if use_python {
3033+ Box :: new ( PythonPtcRuntime :: for_remote ( ssh_manager, connection_id) )
3034+ } else {
3035+ Box :: new ( NodePtcRuntime :: for_remote ( ssh_manager, connection_id) )
3036+ }
30063037 }
30073038 #[ cfg( not( feature = "ssh-remote" ) ) ]
30083039 {
@@ -3011,7 +3042,11 @@ impl ToolPipeline {
30113042 ) ) ;
30123043 }
30133044 } else {
3014- NodePtcRuntime :: discover ( )
3045+ if use_python {
3046+ Box :: new ( PythonPtcRuntime :: discover ( ) )
3047+ } else {
3048+ Box :: new ( NodePtcRuntime :: discover ( ) )
3049+ }
30153050 } ;
30163051 let operation_id = task. tool_call . tool_id . clone ( ) ;
30173052 let handler = Arc :: new ( PtcPipelineToolCallHandler {
@@ -3055,6 +3090,34 @@ impl ToolPipeline {
30553090 "logs" : response. logs,
30563091 } )
30573092 } ;
3093+ // Persist the source language alongside the result so replayed RunCode
3094+ // cards can select the correct syntax highlighter even though the
3095+ // provider's original arguments only contain `code`.
3096+ let ptc_language = context
3097+ . custom_data
3098+ . get ( PTC_LANGUAGE_CONTEXT_KEY )
3099+ . map ( |value| {
3100+ if value
3101+ . as_str ( )
3102+ . is_some_and ( |value| value. eq_ignore_ascii_case ( "python" ) )
3103+ {
3104+ PtcLanguage :: Python
3105+ } else {
3106+ PtcLanguage :: TypeScript
3107+ }
3108+ } )
3109+ . unwrap_or_default ( ) ;
3110+ if let Some ( object) = data. as_object_mut ( ) {
3111+ object. insert (
3112+ "__ptc_language" . to_string ( ) ,
3113+ serde_json:: Value :: String ( ptc_language. as_str ( ) . to_string ( ) ) ,
3114+ ) ;
3115+ } else {
3116+ data = serde_json:: json!( {
3117+ "value" : data,
3118+ "__ptc_language" : ptc_language. as_str( ) ,
3119+ } ) ;
3120+ }
30583121 let loaded_deferred_tool_specs = handler. loaded_deferred_tool_specs . lock ( ) . await . clone ( ) ;
30593122 if !loaded_deferred_tool_specs. is_empty ( ) {
30603123 let loaded_specs_value = serde_json:: to_value ( & loaded_deferred_tool_specs)
@@ -4359,6 +4422,23 @@ mod tests {
43594422 } ) ) ;
43604423 }
43614424
4425+ #[ cfg( feature = "ptc-runtime" ) ]
4426+ #[ test]
4427+ fn ptc_tool_spec_response_uses_one_language_neutral_sdk_field ( ) {
4428+ for language in [ PtcLanguage :: TypeScript , PtcLanguage :: Python ] {
4429+ let response = build_ptc_tool_spec_response ( language, "sdk" . to_string ( ) ) ;
4430+ assert_eq ! (
4431+ response,
4432+ json!( {
4433+ "language" : language. as_str( ) ,
4434+ "sdk_source" : "sdk" ,
4435+ } )
4436+ ) ;
4437+ assert ! ( response. get( "typescript_sdk" ) . is_none( ) ) ;
4438+ assert ! ( response. get( "python_sdk" ) . is_none( ) ) ;
4439+ }
4440+ }
4441+
43624442 #[ cfg( feature = "ptc-runtime" ) ]
43634443 #[ tokio:: test]
43644444 async fn ptc_run_code_dispatches_nested_calls_through_the_pipeline ( ) {
@@ -4396,7 +4476,11 @@ mod tests {
43964476 results[ 0 ] . tool_name,
43974477 openbitfun_agent_tools:: PTC_RUN_CODE_TOOL_NAME
43984478 ) ;
4399- assert_eq ! ( results[ 0 ] . result. result, json!( { "content" : "ok" } ) ) ;
4479+ assert_eq ! ( results[ 0 ] . result. result[ "content" ] , json!( "ok" ) ) ;
4480+ assert_eq ! (
4481+ results[ 0 ] . result. result[ "__ptc_language" ] ,
4482+ json!( "typescript" )
4483+ ) ;
44004484
44014485 let nested = pipeline
44024486 . state_manager
0 commit comments