-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add comments and documentation, replace angle brackets in CLI documentation #724
Changes from 2 commits
d11f989
ce8f277
b393cae
5c9b8c6
27057e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,10 +37,10 @@ | |
`run` takes one or more [glob](<https://en.wikipedia.org/wiki/Glob_(programming)>) patterns to match files in the workspace. | ||
|
||
```bash sh | ||
npx genaiscript run <script> "**/*.md" "**/*.ts" | ||
``` | ||
|
||
### --excluded-files <files...> | ||
### --excluded-files <files...> | ||
|
||
Excludes the specified files from the file set. | ||
|
||
|
@@ -59,10 +59,10 @@ | |
## Output | ||
|
||
### --prompt | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HTML entities should not be used in markdown files; replace '<' with '<' and '>' with '>'.
|
||
Skips the LLM invocation and only prints the expanded system and user chat messages. | ||
|
||
### --out <file|directory> | ||
### --out <file|directory> | ||
|
||
Saves the results in a JSON file, along with markdown files of the output and the trace. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,9 @@ | |
for (const file of files) { | ||
console.log(`processing ${file.filename}`) | ||
... add comments | ||
... format generated code (optional) -- keep things consistent | ||
... build generated -- let's make sure it's still valid code | ||
... check that only comments were changed -- LLM as judge | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments in code snippets should be more descriptive and not use ellipsis '...'.
|
||
... save changes | ||
} | ||
``` | ||
|
@@ -66,14 +69,69 @@ | |
|
||
We provide a detailed set of instructions to the AI for how to analyze and comment on the code. | ||
|
||
## Judge results with LLM | ||
|
||
We issue one more prompt to judge the modified code and make sure the code is not modified. | ||
|
||
```ts | ||
async function checkModifications(filename: string): Promise<boolean> { | ||
const diff = await host.exec(`git diff ${filename}`) | ||
if (!diff.stdout) return false | ||
const res = await runPrompt( | ||
(ctx) => { | ||
ctx.def("DIFF", diff.stdout) | ||
ctx.$`You are an expert developer at all programming languages. | ||
|
||
Your task is to analyze the changes in DIFF and make sure that only comments are modified. | ||
Report all changes that are not comments and print "MODIFIED". | ||
` | ||
}, | ||
{ | ||
cache: "cmt-check", | ||
} | ||
) | ||
|
||
const modified = res.text?.includes("MODIFIED") | ||
console.log(`code modified, reverting...`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Console log statement 'code modified, reverting...' should be inside a conditional block to reflect the actual modification status.
|
||
return modified | ||
} | ||
``` | ||
|
||
## How to Run the Script | ||
|
||
To run this script, you'll first need to install the GenAIScript CLI. [Follow the installation guide here](https://microsoft.github.io/genaiscript/getting-started/installation). | ||
|
||
```shell | ||
```sh | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect code fence 'sh' should be 'shell' for consistency with the rest of the documentation.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace 'shell' with 'sh' to maintain consistency in code block language identifiers.
|
||
genaiscript run cmt | ||
``` | ||
|
||
## Format and build | ||
|
||
One important aspect is to normalize and valid the AI generated code. The user can provide a `format` command to run a formatter | ||
and a `build` command to check if the code is still valid. | ||
|
||
```ts | ||
|
||
script({..., | ||
parameters: { | ||
format: { | ||
type: "string", | ||
description: "Format source code command", | ||
}, | ||
build: { | ||
type: "string", | ||
description: "Build command", | ||
}, | ||
}, | ||
}) | ||
|
||
const { format, build } = env.vars.build | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect variable access, 'env.vars.build' should be 'env.vars' to access 'format' and 'build' variables correctly.
|
||
``` | ||
|
||
```sh | ||
genaiscript run cmt --vars "build=npm run build" "format=npm run format" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect flag format, use '--vars.build="npm run build"' and '--vars.format="npm run format"' instead of the current format.
|
||
``` | ||
|
||
## Full source ([GitHub](https://github.com/microsoft/genaiscript/blob/main/packages/vscode/genaisrc/cmt.genai.mts)) | ||
|
||
<Code code={source} wrap={true} lang="ts" title="cmt.genai.mts" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,17 @@ | |
* of annotations into different formats for integration with CI/CD tools. | ||
*/ | ||
|
||
// Regular expression for matching GitHub Actions annotations | ||
// Regular expression for matching GitHub Actions annotations. | ||
// Example: ::error file=foo.js,line=10,endLine=11::Something went wrong. | ||
const GITHUB_ANNOTATIONS_RX = | ||
/^\s*::(?<severity>notice|warning|error)\s*file=(?<file>[^,]+),\s*line=(?<line>\d+),\s*endLine=(?<endLine>\d+)\s*(,\s*code=(?<code>[^,:]+)?\s*)?::(?<message>.*)$/gim | ||
|
||
// Regular expression for matching Azure DevOps annotations | ||
// Regular expression for matching Azure DevOps annotations. | ||
// Example: ##vso[task.logissue type=warning;sourcepath=foo.cs;linenumber=1;]Found something. | ||
const AZURE_DEVOPS_ANNOTATIONS_RX = | ||
/^\s*##vso\[task.logissue\s+type=(?<severity>error|warning);sourcepath=(?<file>);linenumber=(?<line>\d+)(;code=(?<code>\d+);)?[^\]]*\](?<message>.*)$/gim | ||
|
||
// Regular expression for matching TypeScript build annotations | ||
// Regular expression for matching TypeScript build annotations. | ||
// Example: foo.ts:10:error TS1005: ';' expected. | ||
const TYPESCRIPT_ANNOTATIONS_RX = | ||
/^(?<file>[^:\s].*?):(?<line>\d+)(?::(?<endLine>\d+))?(?::\d+)?\s+-\s+(?<severity>error|warning)\s+(?<code>[^:]+)\s*:\s*(?<message>.*)$/gim | ||
|
@@ -71,12 +71,14 @@ export function parseAnnotations(text: string): Diagnostic[] { | |
* @returns A formatted GitHub Action command string. | ||
*/ | ||
export function convertDiagnosticToGitHubActionCommand(d: Diagnostic) { | ||
// Maps DiagnosticSeverity to GitHub Action severity strings. | ||
const sevMap: Record<DiagnosticSeverity, string> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon
|
||
["info"]: "notice", // Maps 'info' to 'notice' | ||
["warning"]: "warning", | ||
["error"]: "error", | ||
} | ||
|
||
// Construct GitHub Action command string with necessary details. | ||
return `::${sevMap[d.severity] || d.severity} file=${d.filename}, line=${d.range[0][0]}, endLine=${d.range[1][0]}::${d.message}` | ||
} | ||
|
||
|
@@ -87,8 +89,10 @@ export function convertDiagnosticToGitHubActionCommand(d: Diagnostic) { | |
* @returns A formatted Azure DevOps command string. | ||
*/ | ||
export function convertDiagnosticToAzureDevOpsCommand(d: Diagnostic) { | ||
// Handle 'info' severity separately with a debug message. | ||
if (d.severity === "info") return `##[debug]${d.message} at ${d.filename}` | ||
else | ||
// Construct Azure DevOps command string with necessary details. | ||
return `##vso[task.logissue type=${d.severity};sourcepath=${d.filename};linenumber=${d.range[0][0]}]${d.message}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon
|
||
} | ||
|
||
|
@@ -99,11 +103,13 @@ export function convertDiagnosticToAzureDevOpsCommand(d: Diagnostic) { | |
* @returns A string of formatted Markdown annotations. | ||
*/ | ||
export function convertAnnotationsToMarkdown(text: string): string { | ||
// Maps severity levels to Markdown admonition types. | ||
const severities: Record<string, string> = { | ||
error: "CAUTION", | ||
warning: "WARNING", | ||
notice: "NOTE", | ||
} | ||
// Replace GitHub and Azure DevOps annotations with Markdown format. | ||
return text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon
|
||
?.replace( | ||
GITHUB_ANNOTATIONS_RX, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTML entities should not be used in markdown files; replace '<' with '<' and '>' with '>'.