Skip to content

Commit

Permalink
support debugging self file (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan authored Apr 1, 2024
1 parent 68131c2 commit 8fe2adc
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 81 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,18 @@ export async function expandTemplate(

const systems = (template.system ?? []).slice(0)
if (template.system === undefined) {
const useSchema = /defschema/i.test(jsSource)
systems.push("system")
systems.push("system.explanations")
// select file expansion type
if (/diff/i.test(jsSource)) systems.push("system.diff")
else if (/changelog/i.test(jsSource)) systems.push("system.changelog")
else systems.push("system.files")
else {
systems.push("system.files")
if (useSchema) systems.push("system.files_schema")
}
if (useSchema) systems.push("system.schema")
if (/annotations?/i.test(jsSource)) systems.push("system.annotations")
if (/defschema/i.test(jsSource)) systems.push("system.schema")
}
const systemTemplates = systems.map((s) =>
fragment.file.project.getTemplate(s)
Expand Down
15 changes: 1 addition & 14 deletions packages/core/src/genaisrc/system.files.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ system({
})

const folder =
env.vars["outputFolder"] || env.template.outputFolder || "."
env.vars["outputFolder"] || "."
$`## Files
When generating or updating files you will use the following syntax:`
Expand Down Expand Up @@ -32,16 +32,3 @@ if (folder !== ".") $`When generating new files, place files in folder "${folder
$`If a file does not have changes, do not regenerate.`
$`Do NOT emit line numbers in file.`
$`CSV files are inlined as markdown tables.`

$`
### JSON Schema
When you generate JSON or YAML according to a named schema,
you MUST add the schema identifier in the code fence header.
`

def(
`File ${folder}/data.json`,
`...`,
{ language: "json", schema: "CITY_SCHEMA" }
)
20 changes: 20 additions & 0 deletions packages/core/src/genaisrc/system.files_schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
system({
title: "JSON schema for file generation",
description: "Apply JSON schemas to generated data",
})

const folder =
env.vars["outputFolder"] || "."

$`
## Files with JSON Schema
When you generate JSON or YAML according to a named schema,
you MUST add the schema identifier in the code fence header.
`

def(
`File ${folder}/data.json`,
`...`,
{ language: "json", schema: "CITY_SCHEMA" }
)
5 changes: 0 additions & 5 deletions packages/core/src/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ interface PromptTemplate extends PromptLike, ModelOptions {
*/
system?: SystemPromptId[]

/**
* Specifies a folder to create output files into
*/
outputFolder?: string

/**
* Specifies the type of output. Default is `markdown`.
*/
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ export async function parsePromptTemplate(
c.checkString("title")
c.checkString("description")
c.checkString("model")
c.checkString("outputFolder")
c.checkString("responseType")

c.checkBool("unlisted")
Expand Down
5 changes: 0 additions & 5 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/sample/src/aici/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/sample/src/makecode/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/sample/src/tla/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@
{
"command": "genaiscript.fragment.prompt",
"when": "!inDebugMode && resourceFilename =~ /.*\\.genai\\.js$/",
"group": "navigation"
"group": "genaiscript@1"
},
{
"command": "genaiscript.fragment.debug",
"when": "!inDebugMode && resourceFilename =~ /.*\\.genai\\.js$/",
"group": "genaiscript@2"
}
],
"commandPalette": [
Expand Down
45 changes: 22 additions & 23 deletions packages/vscode/src/fragmentcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
templateGroup,
} from "genaiscript-core"
import { ExtensionState } from "./state"
import {
checkDirectoryExists,
checkFileExists,
} from "./fs"
import { checkDirectoryExists, checkFileExists } from "./fs"

type TemplateQuickPickItem = {
template?: PromptTemplate
Expand Down Expand Up @@ -87,31 +84,26 @@ export function activateFragmentCommands(state: ExtensionState) {
const fragmentPrompt = async (
options:
| {
fragment?: Fragment | string | vscode.Uri
template?: PromptTemplate
debug?: boolean
}
fragment?: Fragment | string | vscode.Uri
template?: PromptTemplate
}
| vscode.Uri
) => {
if (typeof options === "object" && options instanceof vscode.Uri)
options = { fragment: options }
let { fragment, template, debug } = options || {}
let { fragment, template } = options || {}

await state.cancelAiRequest()
await state.parseWorkspace()

if (fragment instanceof vscode.Uri && GENAI_JS_REGEX.test(fragment.path)) {
template = state.project.templates.find(p => p.filename === (fragment as vscode.Uri).fsPath)
if (
fragment instanceof vscode.Uri &&
GENAI_JS_REGEX.test(fragment.path)
) {
template = state.project.templates.find(
(p) => p.filename === (fragment as vscode.Uri).fsPath
)
assert(template !== undefined)
/*
const uris = await vscode.window.showOpenDialog({
canSelectMany: false,
openLabel: 'Select env.files',
canSelectFolders: true,
canSelectFiles: true
})
fragment = uris?.[0]
*/
}

fragment = await resolveSpec(fragment)
Expand Down Expand Up @@ -141,7 +133,13 @@ export function activateFragmentCommands(state: ExtensionState) {
await state.cancelAiRequest()
await state.parseWorkspace()

const template = await pickTemplate()
let template: PromptTemplate
if (GENAI_JS_REGEX.test(file.path)) {
template = state.project.templates.find(
(p) => p.filename === file.fsPath
)
assert(template !== undefined)
} else template = await pickTemplate()
await vscode.debug.startDebugging(
vscode.workspace.workspaceFolders[0],
{
Expand Down Expand Up @@ -216,8 +214,9 @@ export function templatesToQuickPickItems(
template.filename
)) ??
template.id,
description: `${template.id} ${template.description || ""
}`,
description: `${template.id} ${
template.description || ""
}`,
template,
}
)
Expand Down

0 comments on commit 8fe2adc

Please sign in to comment.