Skip to content

Commit

Permalink
add error in reasponse
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Dec 5, 2023
1 parent 78cd4a5 commit 0991a1d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

jobs:
build:
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/cli-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: build

on:
workflow_dispatch:
jobs:
env:
GPTOOLS_TOKEN: ${{ secrets.GPTOOLS_TOKEN }}
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: "recursive"
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: "18"
cache: yarn
- run: yarn install --frozen-lockfile
- name: compile
run: yarn compile
- name: run gptools
run: node packages/cli/built/gptools.js front-matter README.md -ot $GITHUB_STEP_SUMMARY
1 change: 1 addition & 0 deletions .gptools/cache/openai.gptools.jsonl

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@ or configured through the `keys` command (as a JSON payload).

Output the entire response as JSON to the stdout.

### --output <file>
### --out <file>

Saves the results in a JSON file, along with mardown files of the output and the trace.

```bash
node gptools.js run <tool> <spec> --output <file>
node gptools.js run <tool> <spec> --out <file>
```

### --out-trace <file>

Save the markdown trace to the specified file.

```bash
node gptools.js run <tool> <spec> --out-trace <file>
```

In a GitHub Actions workflow, you can use this feature to save the trace as a step summary (`GITHUB_STEP_SUMMARY`):

```yaml
- name: Run GPTools tool on spec
run: |
node gptools.js run <tool> <spec> --out-trace $GITHUB_STEP_SUMMARY
```
### --dry-run
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
FragmentTransformResponse,
RequestError,
clearToken,
host,
Expand Down Expand Up @@ -55,13 +56,15 @@ async function run(
json: boolean
maxDelay: string
dryRun: boolean
outTrace: string
}
) {
const out = options.out
const skipLLM = !!options.dryRun
const retry = parseInt(options.retry) || 3
const retryDelay = parseInt(options.retryDelay) || 5000
const maxDelay = parseInt(options.maxDelay) || 180000
const outTrace = options.outTrace

const toolFiles: string[] = []
if (/.gptool\.js$/i.test(tool)) toolFiles.push(tool)
Expand Down Expand Up @@ -99,7 +102,8 @@ async function run(
)
if (!gpspec) throw new Error(`spec ${spec} not found`)
const fragment = gpspec.roots[0]
const res = await backOff(

const res: FragmentTransformResponse = await backOff(
async () =>
await runTemplate(gptool, fragment, {
infoCb: (progress) => {},
Expand All @@ -121,6 +125,7 @@ async function run(
}
)

if (outTrace && res.trace) await writeText(outTrace, res.trace)
if (out) {
const jsonf = /\.json$/i.test(out) ? out : out + ".json"
const userf = jsonf.replace(/\.json$/i, ".user.md")
Expand All @@ -147,6 +152,8 @@ async function run(
console.log(user)
} else console.log(res.text)
}

if (res.error) throw res.error
}

async function listTools() {
Expand Down Expand Up @@ -183,7 +190,8 @@ async function main() {
.command("run")
.description("Runs a GPTools against a GPSpec")
.arguments("<tool> [spec]")
.option("-o, --out <string>", "output file")
.option("-o, --out <string>", "output file. Extra markdown fiels for output and trace will also be generatred")
.option("-ot, --out-trace <string>", "output file for trace")
.option("-r, --retry <number>", "number of retries", "3")
.option("-j, --json", "emit full JSON response to output")
.option(
Expand Down
38 changes: 37 additions & 1 deletion packages/core/src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export interface FragmentTransformResponse {
* Summary of the output generated by the LLM
*/
summary?: string

/**
* Error message if any
*/
error?: unknown
}

function trimNewlines(s: string) {
Expand Down Expand Up @@ -473,6 +478,7 @@ export async function runTemplate(
// if the expansion failed, show the user the trace
if (!success) {
return {
error: new Error("Template failed"),
prompt,
vars,
trace,
Expand Down Expand Up @@ -559,7 +565,17 @@ The user requested to cancel the request.
fileEdits: {},
})
}
throw error

return {
error,
prompt,
vars,
edits: [],
annotations: [],
fileEdits: {},
trace,
text,
}
}

trace += details("LLM response", fenceMD(text))
Expand Down Expand Up @@ -703,5 +719,25 @@ The user requested to cancel the request.
})
}

if (edits.length)
res.trace += details(
"edits",
`| Type | Filename | Message |\n| --- | --- | --- |\n` +
edits
.map((e) => `| ${e.type} | ${e.filename} | ${e.label} |`)
.join("\n")
)
if (annotations.length)
res.trace += details(
"annotations",
`| Severity | Filename | Line | Message |\n| --- | --- | --- | --- |\n` +
annotations
.map(
(e) =>
`| ${e.severity} | ${e.filename} | ${e.range[0]} | ${e.message} |`
)
.join("\n")
)

return res
}
1 change: 1 addition & 0 deletions packages/vscode/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ ${e.message}`
.then((resp) => {
r.response = resp
r.computing = false
if (resp.error) r.error = resp.error
})
.catch((e) => {
r.computing = false
Expand Down

0 comments on commit 0991a1d

Please sign in to comment.