Skip to content

Commit

Permalink
improve copilot (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca authored Jan 17, 2025
1 parent 7adb70c commit 2681bee
Show file tree
Hide file tree
Showing 18 changed files with 723 additions and 237 deletions.
19 changes: 11 additions & 8 deletions spx-backend/internal/controller/copilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,19 @@ func (ctrl *Controller) GenerateMessage(ctx context.Context, params *GenerateMes
messages = append(messages, message)
}
generatedMsg, err := ctrl.anthropicClient.Messages.New(ctx, anthropic.MessageNewParams{
Model: anthropic.F(anthropic.ModelClaude3_5Haiku20241022),
Model: anthropic.F(anthropic.ModelClaude3_5SonnetLatest),
MaxTokens: anthropic.F(int64(MAX_TOKENS)),
// Now we are using APIs from api.gptsapi.net for testing. `[]anthropic.TextBlockParam` is not supported.
// So we use `anthropic.Raw` to pass the string-type system prompt. TODO:
// * Switch to official API
// * Use `[]anthropic.TextBlockParam` instead of `string`
// * Enable prompt caching if it helps
System: anthropic.Raw[[]anthropic.TextBlockParam](copilot.SystemPrompt),
System: anthropic.F([]anthropic.TextBlockParam{
{
Text: anthropic.F(copilot.SystemPrompt),
Type: anthropic.F(anthropic.TextBlockParamTypeText),
CacheControl: anthropic.F(anthropic.CacheControlEphemeralParam{
Type: anthropic.F(anthropic.CacheControlEphemeralTypeEphemeral),
}),
},
}),
Messages: anthropic.F(messages),
Temperature: anthropic.F(0.3),
Temperature: anthropic.F(0.1),
})
if err != nil {
logger.Printf("failed to generate message: %v", err)
Expand Down
18 changes: 9 additions & 9 deletions spx-backend/internal/copilot/custom_element_code_change.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# `code-change`

Display a change to the code. The user may apply the change by clicking "Apply" button in the element.
Display a modification based on the existing code.

NOTE: Instead of `<code-change>`, you should use `<pre is="code-change">` to properly wrap the code change.
**NOTE**:

* Prefer `code-change` over standard code block when showcasing code modifications.
* Use tabs to maintain indentation in the new code.
* Use `<pre is="code-change">` instead of `<code-change>`.

## Attributes

Expand All @@ -24,14 +28,10 @@ The new code to insert.

## Examples

### Basic example

```xml
<pre is="code-change" file="file:///NiuXiaoQi.spx" line="10" remove-line-count="3">
show

say "Hello, world!"
onStart => {
say "Hello, world!"
}
</pre>
```

This is a change to the code of sprite `NiuXiaoQi`. The original code of line 10, 11 & 12 will be removed. And the code to insert is `show\n\nsay "Hello, world!"\n`.
1 change: 0 additions & 1 deletion spx-backend/internal/copilot/gop_defs.json

This file was deleted.

14 changes: 14 additions & 0 deletions spx-backend/internal/copilot/gop_defs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Go+ Syntax

```csv
Package,Name,Sample,Description
,for_iterate,"for i, v <- set {}",Iterate within given set
,for_loop_with_condition,for condition {},Loop with condition
,for_loop_with_range,for i <- start:end {},Loop with range
,func_declaration,func name(params) {},"Function declaration, e.g., `func add(a int, b int) int {}`"
,if_else_statement,if condition {} else {},If else statement
,if_statement,if condition {},If statement
fmt,println,"println msg, ...","Print line, e.g., `println ""Hello, world!""`"
,var_declaration,var name type,"Variable declaration, e.g., `var count int`"
```
6 changes: 3 additions & 3 deletions spx-backend/internal/copilot/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"text/template"
)

// For details about maintaining `*_defs.json` files, see:
// For details about maintaining `*_defs.md` files, see:
// spx-gui/src/components/editor/code-editor/document-base/helpers.ts

//go:embed gop_defs.json
//go:embed gop_defs.md
var gopDefs string

//go:embed spx_defs.json
//go:embed spx_defs.md
var spxDefs string

//go:embed custom_element_code_link.md
Expand Down
1 change: 0 additions & 1 deletion spx-backend/internal/copilot/spx_defs.json

This file was deleted.

152 changes: 152 additions & 0 deletions spx-backend/internal/copilot/spx_defs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# SPX APIs

## Game

```csv
Name,Sample,Description
backdropIndex,,Get the index of the current backdrop
backdropName,,Get the name of the current backdrop
broadcast,broadcast msg,"Broadcast a message, e.g., `broadcast ""msg""`"
broadcast,"broadcast msg, wait","Broadcast a message with waiting, e.g., `broadcast ""msg"", true`"
broadcast,"broadcast msg, data, wait","Broadcast a message with data and waiting, e.g., `broadcast ""msg"", data, true`"
changeVolume,changeVolume dVolume,"Change the volume for sounds with given volume change, e.g., `changeVolume 10`"
onClick,onClick => {},Listen to stage clicked
onKey,"onKey key, => {}","Listen to given key pressed, e.g., `onKey KeyA, => {}`"
onKey,"onKey keys, key => {}","Listen to given keys pressed, optionally receiving the key pressed"
onMsg,"onMsg (msg, data) => {}","Listen to any message broadcasted, get the broadcasted message and data"
onMsg,"onMsg msg, => {}",Listen to specific message broadcasted
onStart,onStart => {},Listen to game start
getWidget,"getWidget(type, name)",Get the widget by given name
keyPressed,keyPressed(key),"Check if given key is currently pressed, e.g., `keyPressed(KeyA)`"
mouseHitItem,,"Get the topmost sprite which is hit by mouse, e.g., `hitSprite, ok := mouseHitItem`"
mousePressed,,Check if the mouse is currently pressed
mouseX,,Get X position of the mouse
mouseY,,Get Y position of the mouse
nextBackdrop,,Switch to the next backdrop
onAnyKey,onAnyKey key => {},Listen to any key pressed
onBackdrop,onBackdrop backdrop => {},Listen to backdrop switching
onBackdrop,"onBackdrop backdrop, => {}",Listen to switching to specific backdrop
play,play sound,"Play given sound, e.g., `play explosion`"
play,"play sound, wait","Play given sound with waiting, e.g., `play explosion, true`"
play,"play sound, options","Play given sound with options, e.g., `play explosion, { Loop: true }`"
play,play name,"Play sound with given name, e.g., `play ""explosion""`"
play,"play name, wait","Play sound with given name and waiting, e.g., `play ""explosion"", true`"
play,"play name, options","Play sound with given name and options, e.g., `play ""explosion"", { Loop: true }`"
prevBackdrop,,Switch to the previous backdrop
setVolume,setVolume volume,"Set the volume for sounds, e.g., `setVolume 100`"
startBackdrop,startBackdrop name,"Set the current backdrop by specifying name, e.g., `startBackdrop ""backdrop1""`"
startBackdrop,"startBackdrop name, wait","Set the current backdrop by specifying name, with waiting, e.g., `startBackdrop ""backdrop1"", true`"
stopAllSounds,,Stop all playing sounds
volume,,Get the volume for sounds
wait,wait seconds,"Block current execution (coroutine) for given seconds, e.g., `wait 0.5`"
```

## Sprite

```csv
Name,Sample,Description
animate,animate name,"Play animation with given name, e.g., `animate ""jump""`"
bounceOffEdge,,Check & bounce off current sprite if touching the edge
changeHeading,changeHeading dDirection,"Change heading with given direction change, e.g., `changeHeading 90`"
changeSize,changeSize dSize,"Change the size of current sprite, e.g., `changeSize 1`"
changeXYpos,"changeXYpos dX, dY","Change the sprite's position, e.g., `changeXYpos 10, 20` changing X position by 10 and Y position by 20"
changeXpos,changeXpos dX,"Change the sprite's X position, e.g., `changeXpos 10` changing X position by 10"
changeYpos,changeYpos dY,"Change the sprite's Y position, e.g., `changeYpos 10` changing Y position by 10"
clone,,Make a clone of current sprite
clone,clone data,"Make a clone of current sprite with given data, e.g., `clone 123` (you can get the data `123` by `onCloned`)"
costumeName,,The name of the current costume
die,,"Let current sprite die. Animation bound to state ""die"" will be played."
distanceTo,distanceTo(sprite),Get the distance from current sprite to given sprite
distanceTo,distanceTo(name),"Get the distance from current sprite to the sprite with given name, e.g., `distanceTo(""Enemy"")`"
distanceTo,distanceTo(obj),"Get the distance from current sprite to given object, e.g., `distanceTo(Mouse)`"
glide,"glide x, y, seconds","Move to given position (X, Y) with glide animation and given duration, e.g., `glide 100, 100, 2`"
glide,"glide sprite, seconds","Move to given sprite with glide animation and given duration, e.g., `glide Enemy, 2`"
glide,"glide name, seconds","Move to the sprite with given name with glide animation and given duration, e.g., `glide ""Enemy"", 2`"
glide,"glide obj, seconds","Move to given obj with glide animation and given duration, e.g., `glide Mouse, 2`"
goto,goto sprite,"Move to given sprite, e.g., `goto Enemy`"
goto,goto name,"Move to the sprite with given name, e.g., `goto ""Enemy""`"
goto,goto obj,"Move to given obj, e.g., `goto Mouse`"
heading,,Get current heading direction
hide,,Make current sprite invisible
move,move distance,"Move given distance toward current heading, e.g., `move 10`"
onCloned,onCloned data => {},"Listen to current sprite cloned, optionally receiving data"
onMoving,onMoving info => {},"Listen to current sprite moving (position change), optionally receiving the moving info"
onTouchStart,onTouchStart sprite => {},"Listen to current sprite starting to be touched by any other sprites, optionally receiving the sprite"
onTouchStart,"onTouchStart name, sprite => {}","Listen to current sprite starting to be touched by sprite of given name, optionally receiving the sprite"
onTouchStart,"onTouchStart names, sprite => {}","Listen to current sprite starting to be touched by any sprite of given names, optionally receiving the sprite"
onTurning,onTurning info => {},"Listen to current sprite turning (heading change), optionally receiving the turning info"
say,say word,"Make the sprite say some word, e.g., `say ""Hello!""`"
say,"say word, seconds","Make the sprite say some word with duration, e.g., `say ""Hello!"", 2`"
setCostume,setCostume name,"Set the current costume by specifying name, e.g., `setCostume ""happy""`"
setHeading,setHeading direction,"Set heading to given value, e.g., `setHeading Up`"
setRotationStyle,setRotationStyle style,"Set the rotation style of the sprite, e.g., `setRotationStyle LeftRight`"
setSize,setSize size,"Set the size of current sprite, e.g., `setSize 2`"
setXYpos,"setXYpos x, y","Set the sprite's position, e.g., `setXYpos 100, 100`"
setXpos,setXpos x,"Set the sprite's X position, e.g., `setXpos 100`"
setYpos,setYpos y,"Set the sprite's Y position, e.g., `setYpos 100`"
show,,Make current sprite visible
size,,Get the size of current sprite
onClick,onClick => {},Listen to current sprite clicked
onKey,"onKey key, => {}","Listen to given key pressed, e.g., `onKey KeyA, => {}`"
onKey,"onKey keys, key => {}","Listen to given keys pressed, optionally receiving the key pressed"
onMsg,"onMsg (msg, data) => {}","Listen to any message broadcasted, get the broadcasted message and data"
onMsg,"onMsg msg, => {}",Listen to specific message broadcasted
onStart,onStart => {},Listen to game start
step,step distance,"Step given distance toward current heading. Animation bound to state ""step"" will be played, e.g., `step 10`"
step,"step distance, animation","Step given distance toward current heading and animation with given name will be played, e.g., `step 10, ""run""`"
think,think word,"Make the sprite think of some word, e.g., `think ""Wow!""`"
think,"think word, seconds","Make the sprite think of some word with duration, e.g., `think ""Wow!"", 2`"
touching,touching(name),Check if current sprite touching sprite with given name
touching,touching(sprite),Check if current sprite touching given sprite
touching,touching(obj),"Check if current sprite touching given object, e.g., `touching(Mouse)`"
turn,turn degree,"Turn with given degree relative to current heading, e.g., `turn 90`"
turn,turn direction,"Turn with given direction relative to current heading, e.g., `turn Left`"
turnTo,turnTo sprite,Turn heading to given sprite
turnTo,turnTo name,"Turn heading to given sprite by name, e.g., `turnTo ""Enemy""`"
turnTo,turnTo degree,"Turn heading to given degree, e.g., `turnTo 90`"
turnTo,turnTo direction,"Turn heading to given direction, e.g., `turnTo Left`"
turnTo,turnTo obj,"Turn heading to given object, e.g., `turnTo Mouse`"
visible,,If current sprite visible
xpos,,Get current X position
ypos,,Get current Y position
```

## Others

```csv
Name,Sample,Description
Down,,"Down direction, i.e., 180 degree"
Edge,,Any edge
EdgeBottom,,Bottom edge
EdgeLeft,,Left edge
EdgeRight,,Right edge
EdgeTop,,Top edge
exit,,Exit the game
Left,,"Left direction, i.e., -90 degree"
LeftRight,,Left-Right
Mouse,,Mouse
Next,,Next item
None,,Don't Rotate
Normal,,Normal
PlayContinue,,Continue
PlayPause,,Pause
PlayResume,,Resume
PlayRewind,,Rewind
PlayStop,,Stop
Prev,,Previous item
rand,"rand(from, to)","Generate a random integer, e.g., `rand(1, 10)`"
rand,"rand(from, to)","Generate a random number, e.g., `rand(1.5, 9.9)`"
Right,,"Right direction, i.e., 90 degree"
Up,,"Up direction, i.e., 0 degree"
```

## Keys

```csv
Name,Sample,Description
"Key0-Key9,KeyA-KeyZ,KeyF1-KeyF12,KeyKP0-KeyKP9,KeyApostrophe,KeyBackslash,KeyBackspace,KeyCapsLock,KeyComma,KeyDelete,KeyDown,KeyEnd,KeyEnter,KeyEqual,KeyEscape,KeyGraveAccent,KeyHome,KeyInsert,KeyKPDecimal,KeyKPDivide,KeyKPEnter,KeyKPEqual,KeyKPMultiply,KeyKPSubtract,KeyLeft,KeyLeftBracket,KeyMenu,KeyMinus,KeyNumLock,KeyPageDown,KeyPageUp,KeyPause,KeyPeriod,KeyPrintScreen,KeyRight,KeyRightBracket,KeyScrollLock,KeySemicolon,KeySlash,KeySpace,KeyTab,KeyUp,KeyAlt,KeyControl,KeyShift,KeyMax,KeyAny","onKey Key1, => {}","Key definitions, used for keyboard event listening."
```
Loading

0 comments on commit 2681bee

Please sign in to comment.