@@ -28,14 +28,48 @@ def __init__(self):
28
28
def get_language (self , language ):
29
29
for lang in self .languages :
30
30
if language .lower () == lang .name .lower () or (
31
- hasattr (lang , "aliases" ) and language in lang .aliases
31
+ hasattr (lang , "aliases" ) and language . lower () in ( alias . lower () for alias in lang .aliases )
32
32
):
33
33
return lang
34
34
return None
35
35
36
- def run (self , language , code ):
36
+ def run (self , language , code , stream = False , display = False ):
37
+ # Llama 3 likes to hallucinate this tick new line thing at the start of code blocks
38
+ if code .startswith ("`\n " ):
39
+ code = code [2 :].strip ()
40
+
41
+ if code .startswith ("```\n " ):
42
+ code = code [4 :].strip ()
43
+
44
+ if stream == False :
45
+ # If stream == False, *pull* from _streaming_run.
46
+ output_messages = []
47
+ for chunk in self ._streaming_run (language , code , display = display ):
48
+ if chunk .get ("format" ) != "active_line" :
49
+ # Should we append this to the last message, or make a new one?
50
+ if (
51
+ output_messages != []
52
+ and output_messages [- 1 ].get ("type" ) == chunk ["type" ]
53
+ and output_messages [- 1 ].get ("format" ) == chunk ["format" ]
54
+ ):
55
+ output_messages [- 1 ]["content" ] += chunk ["content" ]
56
+ else :
57
+ output_messages .append (chunk )
58
+ return output_messages
59
+
60
+ elif stream == True :
61
+ # If stream == True, replace this with _streaming_run.
62
+ return self ._streaming_run (language , code , display = display )
63
+
64
+ def _streaming_run (self , language , code , display = False ):
37
65
if language not in self ._active_languages :
38
- self ._active_languages [language ] = self .get_language (language )()
66
+ # Get the language. Pass in self.computer *if it takes a single argument*
67
+ # but pass in nothing if not. This makes custom languages easier to add / understand.
68
+ lang_class = self .get_language (language )
69
+ if lang_class .__init__ .__code__ .co_argcount > 1 :
70
+ self ._active_languages [language ] = lang_class (self .computer )
71
+ else :
72
+ self ._active_languages [language ] = lang_class ()
39
73
try :
40
74
for chunk in self ._active_languages [language ].run (code ):
41
75
# self.format_to_recipient can format some messages as having a certain recipient.
@@ -56,6 +90,14 @@ def run(self, language, code):
56
90
57
91
yield chunk
58
92
93
+ # Print it also if display = True
94
+ if (
95
+ display
96
+ and chunk .get ("format" ) != "active_line"
97
+ and chunk .get ("content" )
98
+ ):
99
+ print (chunk ["content" ], end = "" )
100
+
59
101
except GeneratorExit :
60
102
self .stop ()
61
103
@@ -70,4 +112,4 @@ def terminate(self):
70
112
language
71
113
): # Not sure why this is None sometimes. We should look into this
72
114
language .terminate ()
73
- del self ._active_languages [language_name ]
115
+ del self ._active_languages [language_name ]
0 commit comments