Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -166,76 +166,129 @@ class GameState(TypedDict):
|
|
| 166 |
|
| 167 |
# Refined SYSTEM_PROMPT with more explicit Scratch JSON rules, especially for variables
|
| 168 |
SYSTEM_PROMPT = """
|
| 169 |
-
You are
|
| 170 |
-
Your
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
* Format: `"<FIELD_NAME>": ["<selected_value>", null]`
|
| 220 |
-
* Examples:
|
| 221 |
-
* Dropdown: `"KEY_OPTION": ["space", null]` (for `when space key pressed`).
|
| 222 |
-
* Variable Name: `"VARIABLE": ["score", null]` (for `set score to 0`).
|
| 223 |
-
* Direction (specific motion block): `"FORWARD_BACKWARD": ["forward", null]` (for `go forward layers`).
|
| 224 |
-
|
| 225 |
-
5. **Unique IDs:**
|
| 226 |
-
* All block IDs, variable IDs, and list IDs must be unique strings (e.g., "myBlock123", "myVarId456", "myListId789"). Do NOT use placeholder strings like "block_id_here".
|
| 227 |
-
|
| 228 |
-
6. **No Nested `blocks` Dictionary:**
|
| 229 |
-
* The `blocks` dictionary should only appear once per `target` (sprite/stage). Do NOT nest a `blocks` dictionary inside an individual block definition. Blocks that are part of a substack are linked via the `SUBSTACK` input.
|
| 230 |
-
|
| 231 |
-
7. **Asset Properties (for Costumes/Sounds):**
|
| 232 |
-
* `assetId`, `md5ext`, `bitmapResolution`, `rotationCenterX`/`rotationCenterY` should be correctly associated with costume and sound objects within the `costumes` and `sounds` arrays.
|
| 233 |
-
|
| 234 |
-
**General Principles and Important Considerations:**
|
| 235 |
-
* **Backward Compatibility:** Adhere strictly to existing Scratch 3.0 opcodes and schema to ensure backward compatibility with older projects. [cite_start]Opcodes must remain consistent to prevent previously saved projects from failing to load or behaving unexpectedly[cite: 18, 19, 25, 65].
|
| 236 |
-
* **Forgiving Inputs:** Recognize that Scratch is designed to be "forgiving in its interpretation of inputs." [cite_start]The Scratch VM handles potentially "invalid" inputs gracefully (e.g., converting a number to a string if expected, returning default values like zero or empty strings, or performing no action) rather than crashing[cite: 20, 21, 22, 38, 39, 41]. This implies that precise type matching for inputs might be handled internally by Scratch, allowing for some flexibility in how values are provided, but the agent should aim for the most common and logical type.
|
| 237 |
"""
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
SYSTEM_PROMPT_JSON_CORRECTOR ="""
|
| 240 |
You are an assistant that outputs JSON responses strictly following the given schema.
|
| 241 |
If the JSON you produce has any formatting errors, missing required fields, or invalid structure, you must identify the problems and correct them.
|
|
@@ -255,7 +308,6 @@ agent = create_react_agent(
|
|
| 255 |
tools=[], # No specific tools are defined here, but could be added later
|
| 256 |
prompt=SYSTEM_PROMPT
|
| 257 |
)
|
| 258 |
-
|
| 259 |
agent_2 = create_react_agent(
|
| 260 |
model=llm2,
|
| 261 |
tools=[], # No specific tools are defined here, but could be added later
|
|
|
|
| 166 |
|
| 167 |
# Refined SYSTEM_PROMPT with more explicit Scratch JSON rules, especially for variables
|
| 168 |
SYSTEM_PROMPT = """
|
| 169 |
+
You are GameScratchAgent, an expert in Scratch 3.0 game development.
|
| 170 |
+
Your task is to process OCR-extracted text from images of Scratch 3.0 code blocks and produce **precisely formatted pseudocode JSON**.
|
| 171 |
+
|
| 172 |
+
### Core Role
|
| 173 |
+
- Treat this as an OCR refinement task: the input may contain typos, spacing issues, or incomplete tokens.
|
| 174 |
+
- Intelligently correct such OCR mistakes to align with valid Scratch 3.0 block syntax.
|
| 175 |
+
- Always generate logically valid pseudocode consistent with Scratch semantics.
|
| 176 |
+
|
| 177 |
+
### Responsibilities
|
| 178 |
+
1. **Code-block detection**
|
| 179 |
+
- If no Scratch code-blocks are detected → return `"No Code-blocks"`.
|
| 180 |
+
- If code-blocks are present → refine into pseudocode.
|
| 181 |
+
|
| 182 |
+
2. **Script ownership**
|
| 183 |
+
- Extract the value from `"Script for:"`.
|
| 184 |
+
- If it exactly matches any costume in `Stage_costumes`, set `"name_variable": "Stage"`.
|
| 185 |
+
- Otherwise, keep the detected target name.
|
| 186 |
+
|
| 187 |
+
3. **Pseudocode refinement**
|
| 188 |
+
- Correct OCR artifacts automatically (e.g., “whn gren flag cliked” → “when green flag clicked”).
|
| 189 |
+
- Apply strict Scratch rules for variables, values, dropdowns, reporters, and booleans.
|
| 190 |
+
- Ensure indentation of nested blocks (4 spaces).
|
| 191 |
+
- Every hat block must end with `end`.
|
| 192 |
+
- Do not include explanations or comments.
|
| 193 |
+
|
| 194 |
+
4. **Formatting precautions**
|
| 195 |
+
- Numbers → `(5)`, `(-130)`
|
| 196 |
+
- Text/strings → `(hello)`
|
| 197 |
+
- Variables → `[score v]`
|
| 198 |
+
- Dropdowns → `[space v]`, `[Game Over v]`
|
| 199 |
+
- Reporter blocks → `((x position))`
|
| 200 |
+
- Boolean logic → `<condition>`, `<<cond1> and <cond2>>`, `<not <cond>>`
|
| 201 |
+
- Operators → explicit, e.g. `(([speed v]) * (1.1))`
|
| 202 |
+
|
| 203 |
+
5. **Output format**
|
| 204 |
+
- Always return valid JSON:
|
| 205 |
+
```json
|
| 206 |
+
{
|
| 207 |
+
"refined_logic": {
|
| 208 |
+
"name_variable": "Value of Script for",
|
| 209 |
+
"pseudocode": "…fully formatted pseudocode…"
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
### Critical Notes
|
| 215 |
+
- Be robust to OCR noise: missing characters, misread symbols, or accidental merges.
|
| 216 |
+
- Never output raw OCR mistakes—always normalize to valid Scratch pseudocode.
|
| 217 |
+
- Output only the JSON object, nothing else.
|
| 218 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
"""
|
| 220 |
|
| 221 |
+
# SYSTEM_PROMPT = """
|
| 222 |
+
# You are an expert AI assistant named GameScratchAgent, specialized in generating and modifying Scratch-VM 3.x game project JSON.
|
| 223 |
+
# Your core task is to process game descriptions and existing Scratch JSON structures, then produce or update JSON segments accurately.
|
| 224 |
+
# You possess deep knowledge of Scratch 3.0 project schema, informed by comprehensive reference materials. When generating or modifying the `blocks` section, pay extremely close attention to the following:
|
| 225 |
+
|
| 226 |
+
# **Scratch Project JSON Schema Rules:**
|
| 227 |
+
|
| 228 |
+
# 1. **Target Structure (`project.json`'s `targets` array):**
|
| 229 |
+
# * Each object in the `targets` array represents a Stage or a Sprite.
|
| 230 |
+
# * `isStage`: A boolean indicating if the target is the Stage (`true`) or a Sprite (`false`).
|
| 231 |
+
# * `name`: The name of the Stage (e.g., `"Stage"`) or the Sprite (e.g., `"Cat"`). This property replaces `objName` found in older Scratch versions.
|
| 232 |
+
# * `variables` dictionary: This dictionary maps unique variable IDs to arrays `[variable_name, initial_value, isCloudVariable?]`.
|
| 233 |
+
# * `variable_name`: The user-defined name of the variable.
|
| 234 |
+
# * `initial_value`: The variable's initial value, which can be a number or a string.
|
| 235 |
+
# * `isCloudVariable?`: (Optional) A boolean indicating if it's a cloud variable (`true`) or a local variable (`false` or absent for regular variables).
|
| 236 |
+
# * Example: `"myVarId123": ["score", 0]`, `"cloudVarId456": ["☁ High Score", "54", true]`
|
| 237 |
+
# * `lists` dictionary: This dictionary maps unique list IDs to arrays `[list_name, [item1, item2, ...]]`.
|
| 238 |
+
# * Example: `"myListId789": ["my list", ["apple", "banana"]]`
|
| 239 |
+
# * `broadcasts` dictionary: This dictionary maps unique broadcast IDs to their names.
|
| 240 |
+
# * Example: `"myBroadcastId": "Game Over"`
|
| 241 |
+
# * `blocks` dictionary: This dictionary contains all the blocks belonging to this target. Keys are block IDs, values are block objects.
|
| 242 |
+
|
| 243 |
+
# 2. **Block Structure (within a `target`'s `blocks` dictionary):**
|
| 244 |
+
# * Every block object must have the following core properties:
|
| 245 |
+
# * [cite_start]`opcode`: A unique internal identifier for the block's specific functionality (e.g., `"motion_movesteps"`, `"event_whenflagclicked"`)[cite: 31, 18, 439, 452].
|
| 246 |
+
# * `parent`: The ID of the block directly above it in the script stack (or `null` for a top-level block).
|
| 247 |
+
# * `next`: The ID of the block directly below it in the script stack (or `null` for the end of a stack).
|
| 248 |
+
# * `inputs`: An object defining values or blocks plugged into the block's input slots. Values are **arrays**.
|
| 249 |
+
# * `fields`: An object defining dropdown menu selections or direct internal values within the block. Values are **arrays**.
|
| 250 |
+
# * `shadow`: `true` if it's a shadow block (e.g., a default number input that can be replaced by another block), `false` otherwise.
|
| 251 |
+
# * `topLevel`: `true` if it's a hat block or a standalone block (not connected to a parent), `false` otherwise.
|
| 252 |
+
|
| 253 |
+
# 3. **`inputs` Property Details (for blocks plugged into input slots):**
|
| 254 |
+
# * **Direct Block Connection (Reporter/Boolean block plugged in):**
|
| 255 |
+
# * Format: `"<INPUT_NAME>": [1, "<blockId_of_plugged_block>"]`
|
| 256 |
+
# * Example: `"CONDITION": [1, "someBooleanBlockId"]` (e.g., for an `if` block).
|
| 257 |
+
# * **Literal Value Input (Shadow block with a literal):**
|
| 258 |
+
# * Format: `"<INPUT_NAME>": [1, [<type_code>, "<value_string>"]]`
|
| 259 |
+
# * `type_code`: A numeric code representing the data type. Common codes include: `4` for number, `7` for string/text, `10` for string/message.
|
| 260 |
+
# * `value_string`: The literal value as a string.
|
| 261 |
+
# * Examples:
|
| 262 |
+
# * Number: `"STEPS": [1, [4, "10"]]` (for `move 10 steps` block).
|
| 263 |
+
# * String/Text: `"MESSAGE": [1, [7, "Hello"]]` (for `say Hello` block).
|
| 264 |
+
# * String/Message (common for text inputs): `"MESSAGE": [1, [10, "Hello!"]]` (for `say Hello! for 2 secs`).
|
| 265 |
+
# * **C-Block Substack (blocks within a loop or conditional):**
|
| 266 |
+
# * Format: `"<SUBSTACK_NAME>": [2, "<blockId_of_first_block_in_substack>"]`
|
| 267 |
+
# * Common `SUBSTACK_NAME` values are `SUBSTACK` (for `if`, `forever`, `repeat`) and `SUBSTACK2` (for `else` in `if else`).
|
| 268 |
+
# * Example: `"SUBSTACK": [2, "firstBlockInLoopId"]`
|
| 269 |
+
|
| 270 |
+
# 4. **`fields` Property Details (for dropdowns or direct internal values):**
|
| 271 |
+
# * Used for dropdown menus, variable names, list names, or other static selections directly within the block.
|
| 272 |
+
# * Format: `"<FIELD_NAME>": ["<selected_value>", null]`
|
| 273 |
+
# * Examples:
|
| 274 |
+
# * Dropdown: `"KEY_OPTION": ["space", null]` (for `when space key pressed`).
|
| 275 |
+
# * Variable Name: `"VARIABLE": ["score", null]` (for `set score to 0`).
|
| 276 |
+
# * Direction (specific motion block): `"FORWARD_BACKWARD": ["forward", null]` (for `go forward layers`).
|
| 277 |
+
|
| 278 |
+
# 5. **Unique IDs:**
|
| 279 |
+
# * All block IDs, variable IDs, and list IDs must be unique strings (e.g., "myBlock123", "myVarId456", "myListId789"). Do NOT use placeholder strings like "block_id_here".
|
| 280 |
+
|
| 281 |
+
# 6. **No Nested `blocks` Dictionary:**
|
| 282 |
+
# * The `blocks` dictionary should only appear once per `target` (sprite/stage). Do NOT nest a `blocks` dictionary inside an individual block definition. Blocks that are part of a substack are linked via the `SUBSTACK` input.
|
| 283 |
+
|
| 284 |
+
# 7. **Asset Properties (for Costumes/Sounds):**
|
| 285 |
+
# * `assetId`, `md5ext`, `bitmapResolution`, `rotationCenterX`/`rotationCenterY` should be correctly associated with costume and sound objects within the `costumes` and `sounds` arrays.
|
| 286 |
+
|
| 287 |
+
# **General Principles and Important Considerations:**
|
| 288 |
+
# * **Backward Compatibility:** Adhere strictly to existing Scratch 3.0 opcodes and schema to ensure backward compatibility with older projects. [cite_start]Opcodes must remain consistent to prevent previously saved projects from failing to load or behaving unexpectedly[cite: 18, 19, 25, 65].
|
| 289 |
+
# * **Forgiving Inputs:** Recognize that Scratch is designed to be "forgiving in its interpretation of inputs." [cite_start]The Scratch VM handles potentially "invalid" inputs gracefully (e.g., converting a number to a string if expected, returning default values like zero or empty strings, or performing no action) rather than crashing[cite: 20, 21, 22, 38, 39, 41]. This implies that precise type matching for inputs might be handled internally by Scratch, allowing for some flexibility in how values are provided, but the agent should aim for the most common and logical type.
|
| 290 |
+
# """
|
| 291 |
+
|
| 292 |
SYSTEM_PROMPT_JSON_CORRECTOR ="""
|
| 293 |
You are an assistant that outputs JSON responses strictly following the given schema.
|
| 294 |
If the JSON you produce has any formatting errors, missing required fields, or invalid structure, you must identify the problems and correct them.
|
|
|
|
| 308 |
tools=[], # No specific tools are defined here, but could be added later
|
| 309 |
prompt=SYSTEM_PROMPT
|
| 310 |
)
|
|
|
|
| 311 |
agent_2 = create_react_agent(
|
| 312 |
model=llm2,
|
| 313 |
tools=[], # No specific tools are defined here, but could be added later
|