prthm11 commited on
Commit
78918c4
·
verified ·
1 Parent(s): e4e6cc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +288 -189
app.py CHANGED
@@ -185,51 +185,70 @@ class GameState(TypedDict):
185
  temp_pseudo_code: list
186
 
187
  # Refined SYSTEM_PROMPT with more explicit Scratch JSON rules, especially for variables
188
- SYSTEM_PROMPT = """
189
- You are GameScratchAgent, an expert in Scratch 3.0 game development.
190
- Your task is to process OCR-extracted text from images of Scratch 3.0 code blocks and produce **precisely formatted pseudocode JSON**.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
- ### Core Role
193
- - Treat this as an OCR refinement task: the input may contain typos, spacing issues, or incomplete tokens.
194
- - Intelligently correct such OCR mistakes to align with valid Scratch 3.0 block syntax.
195
- - Always generate logically valid pseudocode consistent with Scratch semantics.
196
-
197
- ### Responsibilities
198
- 1. **Code-block detection**
199
- - If no Scratch code-blocks are detected → return `"No Code-blocks"`.
200
- - If code-blocks are present → refine into pseudocode.
201
-
202
- 2. **Script ownership**
203
- - Extract the value from `"Script for:"`.
204
- - If it exactly matches any costume in `Stage_costumes`, set `"name_variable": "Stage"`.
205
- - Otherwise, keep the detected target name.
206
-
207
- 3. **Pseudocode refinement**
208
- - Correct OCR artifacts automatically (e.g., “when cliked” → “when green flag clicked”).
209
- - Apply strict Scratch rules for variables, values, dropdowns, reporters, and booleans.
210
- - Ensure indentation of nested blocks (4 spaces).
211
- - Every hat block must end with `end`.
212
- - Do not include explanations or comments.
213
- - **The pseudocode must always be returned as a single string separated by `\n` (not as a list of strings).**
214
- - **Never use string concatenation (`+`) or arrays—only one continuous string.**
215
- - **Every nested control structure (forever, repeat, if, if-else, etc.) must also have its own `end` placed at the correct depth, ensuring proper closure of each block. The placement of `end` is critical for differentiating script meaning (e.g., Case 1 vs Case 2 nesting).**
216
-
217
-
218
- 4. **Formatting precautions**
219
- - Numbers → `(5)`, `(-130)`
220
- - Text/strings → `(hello)`
221
- - Variables → `[score v]`
222
- - Dropdowns → `[space v]`, `[Game Over v]`
223
- - Reporter blocks → `((x position))`
224
- - Boolean logic → `<condition>`, `<<cond1> and <cond2>>`, `<not <cond>>`
225
- - Operators → explicit, e.g. `(([speed v]) * (1.1))`
226
-
227
- ### Critical Notes
228
- - Be robust to OCR noise: missing characters, misread symbols, or accidental merges.
229
- - Never output raw OCR mistakes—always normalize to valid Scratch pseudocode.
230
- - Output only the JSON object, nothing else.
231
 
232
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
  # SYSTEM_PROMPT = """
235
  # You are an expert AI assistant named GameScratchAgent, specialized in generating and modifying Scratch-VM 3.x game project JSON.
@@ -1167,156 +1186,236 @@ def pseudo_generator_node(state: GameState):
1167
  for t in project_json["targets"] if t.get("isStage")
1168
  for c in t.get("costumes", [])
1169
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1170
 
1171
- refinement_prompt = f"""
1172
- You are an expert in Scratch 3.0 game development, specializing in understanding block relationships (stacked, nested).
1173
- "Analyze the Scratch code-block image and generate Pseudo-Code for what this logic appears to be doing."
1174
- From Image, you also have to detect a value of Key given in Text form "Script for: ". Below is the example
1175
- Example: "Script for: Bear", "Script for:" is a key and "Bear" is value and check if there is related target name available.
1176
-
1177
- **Special Rule for Stage Costumes:**
1178
- If the value of "Script for:" exactly matches ANY costume name in the Stage_costumes list given below,
1179
- then the `"name_variable"` in the output JSON must be set to `"Stage"` (not the costume name).
1180
-
1181
- **Targets in Game (Sprites and Stage) available in project_json:**
1182
- Sprite_name:{', '.join(sprite_names)}
1183
- Stage_name: Stage
1184
- Stage_costumes: {', '.join(stage_costumes)}
1185
-
1186
- --- Scratch 3.0 Block Reference ---
1187
- ### Hat Blocks
1188
- Description: {hat_description}
1189
- Blocks:
1190
- {hat_opcodes_functionalities}
1191
-
1192
- ### Boolean Blocks
1193
- Description: {boolean_description}
1194
- Blocks:
1195
- {boolean_opcodes_functionalities}
1196
-
1197
- ### C Blocks
1198
- Description: {c_description}
1199
- Blocks:
1200
- {c_opcodes_functionalities}
1201
-
1202
- ### Cap Blocks
1203
- Description: {cap_description}
1204
- Blocks:
1205
- {cap_opcodes_functionalities}
1206
-
1207
- ### Reporter Blocks
1208
- Description: {reporter_description}
1209
- Blocks:
1210
- {reporter_opcodes_functionalities}
1211
-
1212
- ### Stack Blocks
1213
- Description: {stack_description}
1214
- Blocks:
1215
- {stack_opcodes_functionalities}
1216
- -----------------------------------
1217
-
1218
- Your task is to:
1219
- If you don't find any "Code-Blocks" then,
1220
- **Don't generate Pseudo Code, and pass the message "No Code-blocks"
1221
- If you find any "Code-Blocks" then,
1222
- 1. **Refine the 'logic'**: Make it precise, accurate, and fully aligned with the Game Description. Use Scratch‑consistent verbs and phrasing. **Do NOT** use raw double‑quotes inside the logic string.
1223
-
1224
- 2. **Structural requirements**:
1225
- - **Numeric values** `(e.g., 0, 5, 0.2, -130)` **must** be in parentheses: `(0)`, `(5)`, `(0.2)`, `(-130)`.
1226
- - **AlphaNumeric values** `(e.g., hello, say 5, 4, hi!)` **must** be in parentheses: `(hello)`, `(say 5)`, `(4)`, `(hi!)`.
1227
- - **Variables** must be in the form `[variable v]` (e.g., `[score v]`), even when used inside expressions two example use `set [score v] to (1)` or `show variable ([speed v])`.
1228
- - **Dropdown options** must be in the form `[option v]` (e.g., `[Game Start v]`, `[blue sky v]`). example use `when [space v] key pressed`.
1229
- - **Reporter blocks** used as inputs must be double‑wrapped: `((x position))`, `((y position))`. example use `if <((y position)) = (-130)> then` or `(((x position)) * (1))`.
1230
- - **Boolean blocks** in conditions must be inside `< >`, including nested ones: `<not <condition>>`, `<<cond1> and <cond2>>`,`<<cond1> or <cond2>>`.
1231
- - **Other Boolean blocks** in conditions must be inside `< >`, including nested ones or values or variables: `<(block/value/variable) * (block/value/variable)>`,`<(block/value/variable) < (block/value/variable)>`, and example of another variable`<[apple v] contains [a v]?>`.
1232
- - **Operator expressions** must use explicit Scratch operator blocks, e.g.:
1233
- ```
1234
- (([ballSpeed v]) * (1.1))
1235
- ```
1236
- - **Every hat block script must end** with a final `end` on its own line.
1237
- - **[critical important]:Every nested control structure (forever, repeat, if, if-else, etc.) must also have its own `end` placed at the correct depth, ensuring proper closure of each block. The placement of `end` is critical for differentiating script meaning (e.g., Case 1 vs Case 2 nesting).**
1238
 
1239
 
1240
- 3. **Pseudo‑code formatting**:
1241
- - Represent each block or nested block on its own line.
1242
- - Auto-correct invalid OCR phrases to valid Scratch 3.0 block names using the **Scratch 3.0 Block Reference** above.
1243
- - **Indent nested blocks by 4 spaces under their parent (`forever`, `if`, etc.).This is a critical requirement.**
1244
- - No comments or explanatory text—just the block sequence.
1245
- - a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.
1246
- - **The pseudocode must be returned as a single string separated by `\n` (not as a list of strings).**
1247
- - **Never use string concatenation (`+`) or arrays—only one continuous string.**
1248
- - **Every nested control structure (forever, repeat, if, if-else, etc.) must also have its own `end` placed at the correct depth, ensuring proper closure of each block. The placement of `end` is critical for differentiating script meaning (e.g., Case 1 vs Case 2 nesting).**
1249
 
1250
- 4. **Logic content**:
1251
- - Build clear flow for mechanics (movement, jumping, flying, scoring, collisions).
1252
- - Match each action closely to a Scratch block or tight sequence.
1253
- - Do **NOT** include any justification or comments—only the raw logic.
1254
-
1255
- 5. **Examples for reference**:
1256
- **Correct** pattern for a simple start script:
1257
- ```
1258
- when green flag clicked
1259
- switch backdrop to [blue sky v]
1260
- set [score v] to (0)
1261
- show variable [score v]
1262
- broadcast [Game Start v]
1263
- end
1264
- ```
1265
- **Correct** pattern for updating the high score variable handling:
1266
- ```
1267
- when I receive [Game Over v]
1268
- if <((score)) > (([High Score v]))> then
1269
- set [High Score v] to ([score v])
1270
- end
1271
- switch backdrop to [Game Over v]
1272
- end
1273
- ```
1274
- **Correct** pattern for level up and increase difficulty use:
1275
- ```
1276
- when I receive [Level Up v]
1277
- change [level v] by (1)
1278
- set [ballSpeed v] to ((([ballSpeed v]) * (1.1)))
1279
- end
1280
- ```
1281
- **Correct** pattern for jumping mechanics use:
1282
- ```
1283
- when [space v] key pressed
1284
- if <((y position)) = (-100)> then
1285
- repeat (5)
1286
- change y by (100)
1287
- wait (0.1) seconds
1288
- change y by (-100)
1289
- wait (0.1) seconds
1290
- end
1291
- end
1292
- end
1293
- ```
1294
- **Correct** pattern for continuos moving objects use:
1295
- ```
1296
- when green flag clicked
1297
- go to x: (240) y: (-100)
1298
- set [speed v] to (-5)
1299
- show variable [speed v]
1300
- forever
1301
- change x by ([speed v])
1302
- if <((x position)) < (-240)> then
1303
- go to x: (240) y: (-100)
1304
- end
1305
- end
1306
- end
1307
- ```
1308
- 6. **Donot** add any explaination of logic or comments to justify or explain just put the logic content in the json.
1309
- 7. **Output**:
1310
- Return **only** a JSON object, using double quotes everywhere:
1311
- ```json
1312
- {{
1313
- "refined_logic":{{
1314
- "name_variable": 'Value of "Sript for: "',
1315
- "pseudocode":"…your fully‑formatted pseudo‑code here…",
1316
- }}
1317
- }}
1318
- ```
1319
- """
1320
  image_input = {
1321
  "type": "image_url",
1322
  "image_url": {
 
185
  temp_pseudo_code: list
186
 
187
  # Refined SYSTEM_PROMPT with more explicit Scratch JSON rules, especially for variables
188
+ # SYSTEM_PROMPT = """
189
+ # You are GameScratchAgent, an expert in Scratch 3.0 game development.
190
+ # Your task is to process OCR-extracted text from images of Scratch 3.0 code blocks and produce **precisely formatted pseudocode JSON**.
191
+
192
+ # ### Core Role
193
+ # - Treat this as an OCR refinement task: the input may contain typos, spacing issues, or incomplete tokens.
194
+ # - Intelligently correct such OCR mistakes to align with valid Scratch 3.0 block syntax.
195
+ # - Always generate logically valid pseudocode consistent with Scratch semantics.
196
+
197
+ # ### Responsibilities
198
+ # 1. **Code-block detection**
199
+ # - If no Scratch code-blocks are detected → return `"No Code-blocks"`.
200
+ # - If code-blocks are present → refine into pseudocode.
201
+
202
+ # 2. **Script ownership**
203
+ # - Extract the value from `"Script for:"`.
204
+ # - If it exactly matches any costume in `Stage_costumes`, set `"name_variable": "Stage"`.
205
+ # - Otherwise, keep the detected target name.
206
+
207
+ # 3. **Pseudocode refinement**
208
+ # - Correct OCR artifacts automatically (e.g., “when cliked” → “when green flag clicked”).
209
+ # - Apply strict Scratch rules for variables, values, dropdowns, reporters, and booleans.
210
+ # - Ensure indentation of nested blocks (4 spaces).
211
+ # - Every hat block must end with `end`.
212
+ # - Do not include explanations or comments.
213
+ # - **The pseudocode must always be returned as a single string separated by `\n` (not as a list of strings).**
214
+ # - **Never use string concatenation (`+`) or arrays—only one continuous string.**
215
+ # - **Every nested control structure (forever, repeat, if, if-else, etc.) must also have its own `end` placed at the correct depth, ensuring proper closure of each block. The placement of `end` is critical for differentiating script meaning (e.g., Case 1 vs Case 2 nesting).**
216
+
217
+
218
+ # 4. **Formatting precautions**
219
+ # - Numbers → `(5)`, `(-130)`
220
+ # - Text/strings → `(hello)`
221
+ # - Variables → `[score v]`
222
+ # - Dropdowns → `[space v]`, `[Game Over v]`
223
+ # - Reporter blocks → `((x position))`
224
+ # - Boolean logic → `<condition>`, `<<cond1> and <cond2>>`, `<not <cond>>`
225
+ # - Operators → explicit, e.g. `(([speed v]) * (1.1))`
226
+
227
+ # ### Critical Notes
228
+ # - Be robust to OCR noise: missing characters, misread symbols, or accidental merges.
229
+ # - Never output raw OCR mistakes—always normalize to valid Scratch pseudocode.
230
+ # - Output only the JSON object, nothing else.
231
 
232
+ # """
233
+ SYSTEM_PROMPT ="""Your task is to process OCR-extracted text from images of Scratch 3.0 code blocks and produce precisely formatted pseudocode JSON.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
+ ### Core Role
236
+ - Treat this as an OCR refinement task: the input may contain typos or spacing issues.
237
+ - Intelligently correct OCR mistakes to align with valid Scratch 3.0 block syntax.
238
+
239
+ ### Universal Rules
240
+ 1. **Code Detection:** If no Scratch blocks are detected, the `pseudocode` value must be "No Code-blocks".
241
+ 2. **Script Ownership:** Determine the target from "Script for:". If it matches a `Stage_costumes` name, set `name_variable` to "Stage".
242
+ 3. **Pseudocode Structure:**
243
+ - The pseudocode must be a single JSON string with `\n` for newlines.
244
+ - Indent nested blocks with 4 spaces.
245
+ - Every script (hat block) and every C-block (if, repeat, forever) MUST have a corresponding `end` at the correct indentation level.
246
+ 4. **Formatting Syntax:**
247
+ - Numbers & Text: `(5)`, `(hello)`
248
+ - Variables & Dropdowns: `[score v]`, `[space v]`
249
+ - Reporters: `((x position))`
250
+ - Booleans: `<condition>`
251
+ 5. **Final Output:** Your response must ONLY be the valid JSON object and nothing else."""
252
 
253
  # SYSTEM_PROMPT = """
254
  # You are an expert AI assistant named GameScratchAgent, specialized in generating and modifying Scratch-VM 3.x game project JSON.
 
1186
  for t in project_json["targets"] if t.get("isStage")
1187
  for c in t.get("costumes", [])
1188
  ]
1189
+ refinement_prompt = """
1190
+ You are an expert Scratch 3.0 programmer. Your task is to analyze an image of Scratch code blocks and convert it into a structured JSON object containing precise pseudocode.
1191
+
1192
+ ---
1193
+ ## CONTEXT
1194
+ - **Available Sprites:** {', '.join(sprite_names)}
1195
+ - **Available Stage Costumes:** {', '.join(stage_costumes)}
1196
+
1197
+ ---
1198
+ ## INSTRUCTIONS
1199
+ 1. **Identify the Target:** Find the text "Script for:" in the image to determine the target sprite or stage.
1200
+ 2. **Apply Stage Rule:** If the identified target name exactly matches any name in the `Available Stage Costumes` list, you MUST set the output `name_variable` to `"Stage"`. Otherwise, use the identified target name.
1201
+ 3. **Handle No Code:** If no Scratch blocks are visible in the image, return the specified "No Code-blocks" JSON format.
1202
+ 4. **Generate Pseudocode:** If blocks are present, convert them to pseudocode according to the rules below.
1203
+ 5. **Output ONLY JSON:** Your entire response must be a single, valid JSON object inside a ```json code block and nothing else.
1204
+
1205
+ ---
1206
+ ## PSEUDOCODE FORMATTING RULES
1207
+ - **Numbers & Text:** Enclose in parentheses. `(10)`, `(-50)`, `(hello)`.
1208
+ - **Variables & Dropdowns:** Enclose in square brackets with ` v`. `[score v]`, `[space v]`.
1209
+ - **Reporter Blocks:** Enclose in double parentheses. `((x position))`.
1210
+ - **Boolean Conditions:** Enclose in angle brackets. `<((score)) > (50)>`, `<not <touching [edge v]?>>`.
1211
+ - **Line Breaks:** Use `\n` to separate each block onto a new line. The entire pseudocode must be a single JSON string.
1212
+ - **Indentation:** Use **4 spaces** to indent blocks nested inside C-Blocks (like `if`, `repeat`, `forever`).
1213
+ - **Termination:**
1214
+ - **Every script** (starting with a hat block) MUST conclude with `end`.
1215
+ - **Every C-Block** (`if`, `repeat`, `forever`) MUST also have its own corresponding `end` at the correct indentation level. This is critical.
1216
+
1217
+ ---
1218
+ ## REQUIRED JSON FORMAT
1219
+ If code blocks are found:
1220
+ ```json
1221
+ {{
1222
+ "refined_logic": {{
1223
+ "name_variable": "Name_Identified_From_Instructions",
1224
+ "pseudocode": "Your fully formatted pseudocode as a single string with \\n newlines."
1225
+ }}
1226
+ }}
1227
+ ````
1228
+
1229
+ If no code blocks are found:
1230
+
1231
+ ```json
1232
+ {{
1233
+ "refined_logic": {{
1234
+ "name_variable": "Name_Identified_From_Instructions",
1235
+ "pseudocode": "No Code-blocks"
1236
+ }}
1237
+ }}
1238
+ ```
1239
+
1240
+ -----
1241
+
1242
+ ## EXAMPLES
1243
+
1244
+ **Example 1: Looping and Conditionals**
1245
+
1246
+ ```
1247
+ when green flag clicked
1248
+ go to x: (240) y: (-100)
1249
+ set [speed v] to (-5)
1250
+ forever
1251
+ change x by ([speed v])
1252
+ if <((x position)) < (-240)> then
1253
+ go to x: (240) y: (-100)
1254
+ end
1255
+ end
1256
+ end
1257
+ ```
1258
+
1259
+ **Example 2: Events and Broadcasting**
1260
+
1261
+ ```
1262
+ when I receive [Game Over v]
1263
+ if <((score)) > (([High Score v]))> then
1264
+ set [High Score v] to ([score v])
1265
+ end
1266
+ switch backdrop to [Game Over v]
1267
+ end
1268
+ ```
1269
+ """
1270
+ # refinement_prompt = f"""
1271
+ # You are an expert in Scratch 3.0 game development, specializing in understanding block relationships (stacked, nested).
1272
+ # "Analyze the Scratch code-block image and generate Pseudo-Code for what this logic appears to be doing."
1273
+ # From Image, you also have to detect a value of Key given in Text form "Script for: ". Below is the example
1274
+ # Example: "Script for: Bear", "Script for:" is a key and "Bear" is value and check if there is related target name available.
1275
+
1276
+ # **Special Rule for Stage Costumes:**
1277
+ # If the value of "Script for:" exactly matches ANY costume name in the Stage_costumes list given below,
1278
+ # then the `"name_variable"` in the output JSON must be set to `"Stage"` (not the costume name).
1279
+
1280
+ # **Targets in Game (Sprites and Stage) available in project_json:**
1281
+ # Sprite_name:{', '.join(sprite_names)}
1282
+ # Stage_name: Stage
1283
+ # Stage_costumes: {', '.join(stage_costumes)}
1284
 
1285
+ # --- Scratch 3.0 Block Reference ---
1286
+ # ### Hat Blocks
1287
+ # Description: {hat_description}
1288
+ # Blocks:
1289
+ # {hat_opcodes_functionalities}
1290
+
1291
+ # ### Boolean Blocks
1292
+ # Description: {boolean_description}
1293
+ # Blocks:
1294
+ # {boolean_opcodes_functionalities}
1295
+
1296
+ # ### C Blocks
1297
+ # Description: {c_description}
1298
+ # Blocks:
1299
+ # {c_opcodes_functionalities}
1300
+
1301
+ # ### Cap Blocks
1302
+ # Description: {cap_description}
1303
+ # Blocks:
1304
+ # {cap_opcodes_functionalities}
1305
+
1306
+ # ### Reporter Blocks
1307
+ # Description: {reporter_description}
1308
+ # Blocks:
1309
+ # {reporter_opcodes_functionalities}
1310
+
1311
+ # ### Stack Blocks
1312
+ # Description: {stack_description}
1313
+ # Blocks:
1314
+ # {stack_opcodes_functionalities}
1315
+ # -----------------------------------
1316
+
1317
+ # Your task is to:
1318
+ # If you don't find any "Code-Blocks" then,
1319
+ # **Don't generate Pseudo Code, and pass the message "No Code-blocks"
1320
+ # If you find any "Code-Blocks" then,
1321
+ # 1. **Refine the 'logic'**: Make it precise, accurate, and fully aligned with the Game Description. Use Scratch‑consistent verbs and phrasing. **Do NOT** use raw double‑quotes inside the logic string.
1322
+
1323
+ # 2. **Structural requirements**:
1324
+ # - **Numeric values** `(e.g., 0, 5, 0.2, -130)` **must** be in parentheses: `(0)`, `(5)`, `(0.2)`, `(-130)`.
1325
+ # - **AlphaNumeric values** `(e.g., hello, say 5, 4, hi!)` **must** be in parentheses: `(hello)`, `(say 5)`, `(4)`, `(hi!)`.
1326
+ # - **Variables** must be in the form `[variable v]` (e.g., `[score v]`), even when used inside expressions two example use `set [score v] to (1)` or `show variable ([speed v])`.
1327
+ # - **Dropdown options** must be in the form `[option v]` (e.g., `[Game Start v]`, `[blue sky v]`). example use `when [space v] key pressed`.
1328
+ # - **Reporter blocks** used as inputs must be double‑wrapped: `((x position))`, `((y position))`. example use `if <((y position)) = (-130)> then` or `(((x position)) * (1))`.
1329
+ # - **Boolean blocks** in conditions must be inside `< >`, including nested ones: `<not <condition>>`, `<<cond1> and <cond2>>`,`<<cond1> or <cond2>>`.
1330
+ # - **Other Boolean blocks** in conditions must be inside `< >`, including nested ones or values or variables: `<(block/value/variable) * (block/value/variable)>`,`<(block/value/variable) < (block/value/variable)>`, and example of another variable`<[apple v] contains [a v]?>`.
1331
+ # - **Operator expressions** must use explicit Scratch operator blocks, e.g.:
1332
+ # ```
1333
+ # (([ballSpeed v]) * (1.1))
1334
+ # ```
1335
+ # - **Every hat block script must end** with a final `end` on its own line.
1336
+ # - **[critical important]:Every nested control structure (forever, repeat, if, if-else, etc.) must also have its own `end` placed at the correct depth, ensuring proper closure of each block. The placement of `end` is critical for differentiating script meaning (e.g., Case 1 vs Case 2 nesting).**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1337
 
1338
 
1339
+ # 3. **Pseudo‑code formatting**:
1340
+ # - Represent each block or nested block on its own line.
1341
+ # - Auto-correct invalid OCR phrases to valid Scratch 3.0 block names using the **Scratch 3.0 Block Reference** above.
1342
+ # - **Indent nested blocks by 4 spaces under their parent (`forever`, `if`, etc.).This is a critical requirement.**
1343
+ # - No comments or explanatory text—just the block sequence.
1344
+ # - a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.
1345
+ # - **The pseudocode must be returned as a single string separated by `\n` (not as a list of strings).**
1346
+ # - **Never use string concatenation (`+`) or arrays—only one continuous string.**
1347
+ # - **Every nested control structure (forever, repeat, if, if-else, etc.) must also have its own `end` placed at the correct depth, ensuring proper closure of each block. The placement of `end` is critical for differentiating script meaning (e.g., Case 1 vs Case 2 nesting).**
1348
 
1349
+ # 4. **Logic content**:
1350
+ # - Build clear flow for mechanics (movement, jumping, flying, scoring, collisions).
1351
+ # - Match each action closely to a Scratch block or tight sequence.
1352
+ # - Do **NOT** include any justification or comments—only the raw logic.
1353
+
1354
+ # 5. **Examples for reference**:
1355
+ # **Correct** pattern for a simple start script:
1356
+ # ```
1357
+ # when green flag clicked
1358
+ # switch backdrop to [blue sky v]
1359
+ # set [score v] to (0)
1360
+ # show variable [score v]
1361
+ # broadcast [Game Start v]
1362
+ # end
1363
+ # ```
1364
+ # **Correct** pattern for updating the high score variable handling:
1365
+ # ```
1366
+ # when I receive [Game Over v]
1367
+ # if <((score)) > (([High Score v]))> then
1368
+ # set [High Score v] to ([score v])
1369
+ # end
1370
+ # switch backdrop to [Game Over v]
1371
+ # end
1372
+ # ```
1373
+ # **Correct** pattern for level up and increase difficulty use:
1374
+ # ```
1375
+ # when I receive [Level Up v]
1376
+ # change [level v] by (1)
1377
+ # set [ballSpeed v] to ((([ballSpeed v]) * (1.1)))
1378
+ # end
1379
+ # ```
1380
+ # **Correct** pattern for jumping mechanics use:
1381
+ # ```
1382
+ # when [space v] key pressed
1383
+ # if <((y position)) = (-100)> then
1384
+ # repeat (5)
1385
+ # change y by (100)
1386
+ # wait (0.1) seconds
1387
+ # change y by (-100)
1388
+ # wait (0.1) seconds
1389
+ # end
1390
+ # end
1391
+ # end
1392
+ # ```
1393
+ # **Correct** pattern for continuos moving objects use:
1394
+ # ```
1395
+ # when green flag clicked
1396
+ # go to x: (240) y: (-100)
1397
+ # set [speed v] to (-5)
1398
+ # show variable [speed v]
1399
+ # forever
1400
+ # change x by ([speed v])
1401
+ # if <((x position)) < (-240)> then
1402
+ # go to x: (240) y: (-100)
1403
+ # end
1404
+ # end
1405
+ # end
1406
+ # ```
1407
+ # 6. **Donot** add any explaination of logic or comments to justify or explain just put the logic content in the json.
1408
+ # 7. **Output**:
1409
+ # Return **only** a JSON object, using double quotes everywhere:
1410
+ # ```json
1411
+ # {{
1412
+ # "refined_logic":{{
1413
+ # "name_variable": 'Value of "Sript for: "',
1414
+ # "pseudocode":"…your fully‑formatted pseudo‑code here…",
1415
+ # }}
1416
+ # }}
1417
+ # ```
1418
+ # """
1419
  image_input = {
1420
  "type": "image_url",
1421
  "image_url": {