Skip to content

Commit

Permalink
Reapply "feat: new ai_rewrite function for full-file rewrites" (#217)
Browse files Browse the repository at this point in the history
This reverts commit 5dbb92c.
  • Loading branch information
morgante committed Jul 24, 2024
1 parent 5dbb92c commit b56bb84
Show file tree
Hide file tree
Showing 12 changed files with 587 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ jobs:
with:
go-version: '^1.22.0'

- name: Install @getgrit/launcher
run: npm install -g @getgrit/launcher
- name: Install Grit
run: npm install -g @getgrit/cli

- name: Run doctor
run: grit doctor
Expand All @@ -74,7 +74,7 @@ jobs:
- name: Run grit patterns test
run: |
if [ -n "${{ secrets.API_CLIENT_ID }}" ] && [ -n "${{ secrets.API_CLIENT_SECRET }}" ]; then
grit patterns test --exclude ai
grit patterns test --exclude flaky
else
grit patterns test --exclude ai
fi
5 changes: 4 additions & 1 deletion .grit/patterns/js/marzano.grit
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ private pattern before_each_file_stdlib() {
}

private pattern after_each_file_stdlib() {
after_each_file_handle_imports()
and {
after_each_file_handle_imports(),
after_each_file_global_rewrites()
}
}


Expand Down
3 changes: 1 addition & 2 deletions .grit/patterns/js/no_commented_out_code.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
tags: [hidden, linting, best-practice, recommended, ai]
tags: [hidden, linting, best-practice, recommended, ai, flaky]
---

# No Commented Out Code

Please [don't commit commented out code](https://kentcdodds.com/blog/please-dont-commit-commented-out-code).


```grit
engine marzano(0.1)
language js
Expand Down
3 changes: 1 addition & 2 deletions .grit/patterns/universal/ai/_ai_is_bare.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
tags: [ai, sample, util, hidden]
tags: [ai, sample, util, hidden, flaky]
---

# AI conditions.

Test `ai_is` with no counter-examples.


```grit
`console.log($msg)` => `// REDACTED: $msg` where {
$msg <: ai_is("references personally identifiable information")
Expand Down
3 changes: 1 addition & 2 deletions .grit/patterns/universal/ai/_ai_match.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
tags: [ai, sample, util, hidden]
tags: [ai, sample, util, hidden, flaky]
---

# AI conditions.

GritQL can use an AI for fuzzy matching. Just match the node you wish to analyze against the `ai_is` pattern.


```grit
`console.log($msg)` => `// REDACTED: $msg` where {
$msg <: ai_is("it references personally identifiable information")
Expand Down
263 changes: 263 additions & 0 deletions .grit/patterns/universal/ai/_ai_rewrite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
---
tags: [ai, sample, util, hidden, example]
---

# AI rewrites

Use `ai_rewrite($match, $instruct)` to rewrite a target variable based on some instruction.
Edits might be made to other parts of the file if necessary to fulfill the intent of your instruction.

```grit
language yaml
pattern pair($key, $value) {
block_mapping_pair(key=contains $key, $value)
}
or {
`- $task` where {
$task <: block_mapping(items=some pair(key=`across`)),
$task => ai_rewrite(match=$task, instruct="Replace this `across` task with `in_parallel` tasks, distributing the values across each child like this:
Note that each replacement task will have the same name as the original task, but with `-1`, `-2`, etc. appended to it.
in_parallel:
- task: task-1
- task: task-2
")
}
}
```

## Handles a basic transform - currently

```yaml
jobs:
- name: build-and-use-image
plan:
- across:
- var: name
values:
- file1
- file2
- file3
task: create-file
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/((.:name))
outputs:
- name: manifests
file: input.yaml
- task: list-file
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
inputs:
- name: manifests
run:
path: ls
args:
- manifests
```
```yaml
jobs:
- name: build-and-use-image
plan:
- in_parallel:
- task: create-file-1
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file1
outputs:
- name: manifests
file: input.yaml
- task: create-file-2
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file2
outputs:
- name: manifests
file: input.yaml
- task: create-file-3
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file3
outputs:
- name: manifests
file: input.yaml
- task: list-file
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
inputs:
- name: manifests
run:
path: ls
args:
- manifests
```
## Handles a basic transform with two tasks
```yaml
jobs:
- name: build-and-use-image
plan:
- across:
- var: name
values:
- file1
- file2
- file3
task: create-file
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/((.:name))
outputs:
- name: manifests
file: input.yaml
- name: deploy-stuff
plan:
- across:
- var: region
values:
- us-east-1
- us-east-2
- us-west-3
task: deploy-code
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: deploy
args:
- aws/((.:region))
outputs:
- name: manifests
file: input.yaml
```
```yaml
jobs:
- name: build-and-use-image
plan:
- in_parallel:
- task: create-file-1
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file1
outputs:
- name: manifests
file: input.yaml
- task: create-file-2
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file2
outputs:
- name: manifests
file: input.yaml
- task: create-file-3
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file3
outputs:
- name: manifests
file: input.yaml
- name: deploy-stuff
plan:
- in_parallel:
- task: deploy-code-1
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: deploy
args:
- aws/us-east-1
outputs:
- name: manifests
file: input.yaml
- task: deploy-code-2
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: deploy
args:
- aws/us-east-2
outputs:
- name: manifests
file: input.yaml
- task: deploy-code-3
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: deploy
args:
- aws/us-west-3
outputs:
- name: manifests
file: input.yaml
```
Loading

0 comments on commit b56bb84

Please sign in to comment.