diff --git a/docs/src/content/docs/reference/cli/commands.md b/docs/src/content/docs/reference/cli/commands.md index ed41b6b821..b3e265301b 100644 --- a/docs/src/content/docs/reference/cli/commands.md +++ b/docs/src/content/docs/reference/cli/commands.md @@ -3,16 +3,6 @@ title: Commands description: List of all CLI commands sidebar: order: 100 -hero: - image: - alt: "A 2D 8-bit style illustration made with flat geometric shapes in five - colors, featuring interlocking icons: document scripts, gears, file - folders, command line terminals, a brain with circuits for AI, stacked - disks for data, a server/cloud, and a filmstrip for video. The design is - corporate and minimal, with no text, people, or background, sized at - 128x128 pixels." - file: ./commands.png - --- diff --git a/docs/src/content/docs/reference/scripts/system.mdx b/docs/src/content/docs/reference/scripts/system.mdx index c8d455dbc7..3f4dbb8e1d 100644 --- a/docs/src/content/docs/reference/scripts/system.mdx +++ b/docs/src/content/docs/reference/scripts/system.mdx @@ -1,14 +1,9 @@ --- title: System Prompts sidebar: - order: 10 + order: 10 description: Learn how to utilize system prompts to enhance script execution in GenAIScript. keywords: system prompts, script execution, genai templates, environment consistency -hero: - image: - alt: No image prompt was provided to describe. - file: ./system.png - --- System prompts are scripts that are executed and injected before the main prompt output. diff --git a/docs/src/content/docs/reference/vscode/running-scripts.mdx b/docs/src/content/docs/reference/vscode/running-scripts.mdx index 14deff7493..aa0e8d70e4 100644 --- a/docs/src/content/docs/reference/vscode/running-scripts.mdx +++ b/docs/src/content/docs/reference/vscode/running-scripts.mdx @@ -1,21 +1,21 @@ --- title: Running Scripts sidebar: - order: 3 + order: 3 description: Learn how to run GenAIScripts in Visual Studio Code using the - GenAIScript extension. + GenAIScript extension. keywords: GenAIScript, Visual Studio Code, run scripts, command palette, context menu hero: - image: - alt: "An 8-bit style image displays a basic computer screen set at a small - square size. On screen are geometric icons: a script file with a play - button, a file explorer showing several files and folders, and one - file—labeled as .gitignore—stands out with a highlight. The palette uses - only five businesslike colors; the graphics are flat, minimal, and use - simple shapes, with no background, shading, or words. Everything is - arranged for a tidy, iconic appearance." - file: ./running-scripts.png - + image: + alt: + "An 8-bit style image displays a basic computer screen set at a small + square size. On screen are geometric icons: a script file with a play + button, a file explorer showing several files and folders, and one + file—labeled as .gitignore—stands out with a highlight. The palette uses + only five businesslike colors; the graphics are flat, minimal, and use + simple shapes, with no background, shading, or words. Everything is + arranged for a tidy, iconic appearance." + file: ./running-scripts.png --- import { YouTube } from "astro-embed" @@ -60,6 +60,18 @@ This mode allows to run scripts on any combination of files and folders, which w - open an file in the editor (not a GenAIScript file) - right-click and select **Run GenAIScript** from the context menu +## Using the selected text in your script + +Whenever you launch a script, GenAIScript will grab the selected text in the active text editor +and store in the `editor.selectedText` variable. + +```js +const text = env.vars["editor.selectedText"] +``` + +If value will be undefined if you run your script from the command line, +so you should handle that case in your script. + ## .gitignore rules GenAIScript tries to respect the **top-level `.gitignore` rules in the project workspace** (it ignores nested .gitignore files). diff --git a/packages/core/src/types/prompt_template.d.ts b/packages/core/src/types/prompt_template.d.ts index 3e37f63ddc..c69a81b18b 100644 --- a/packages/core/src/types/prompt_template.d.ts +++ b/packages/core/src/types/prompt_template.d.ts @@ -1666,6 +1666,10 @@ interface ExpansionVariables { * Selected model identifier in GitHub Copilot Chat */ "copilot.model"?: string + /** + * selected text in active text editor + */ + "editor.selectedText"?: string } /** diff --git a/packages/sample/genaisrc/selected-text.genai.mts b/packages/sample/genaisrc/selected-text.genai.mts new file mode 100644 index 0000000000..baa0ff6061 --- /dev/null +++ b/packages/sample/genaisrc/selected-text.genai.mts @@ -0,0 +1,6 @@ +script({ + model: "echo" +}) + +def("TEXT", env.vars["editor.selectedText"]) +$`Summarize in a single sentence.` diff --git a/packages/vscode/src/fragmentcommands.ts b/packages/vscode/src/fragmentcommands.ts index 7dd86dc059..aa273862f1 100644 --- a/packages/vscode/src/fragmentcommands.ts +++ b/packages/vscode/src/fragmentcommands.ts @@ -13,6 +13,7 @@ import { showPromptParametersQuickPicks, } from "./parameterquickpick" import { scriptsToQuickPickItems } from "./scriptquickpick" +import { getSelectedText } from "./selection" export function activateFragmentCommands(state: ExtensionState) { const { context, host } = state @@ -57,6 +58,9 @@ export function activateFragmentCommands(state: ExtensionState) { fileOrFolder: vscode.Uri | undefined, extraArgs: vscode.Uri[] | { groupId: unknown } | undefined ) => { + // grab this early before any UI interactions + const selectedText = getSelectedText() + // fileOrFolder is undefined from the command palette const fileOrFolders = Array.isArray(extraArgs) ? (extraArgs as vscode.Uri[]) @@ -72,16 +76,23 @@ export function activateFragmentCommands(state: ExtensionState) { let files: string[] let parameters: PromptParameters + const defaultValues = { selectedText } if (GENAI_ANY_REGEX.test(fileOrFolder?.path)) { const script = findScript(fileOrFolder) - parameters = await showPromptParametersQuickPicks(script) + parameters = await showPromptParametersQuickPicks( + script, + defaultValues + ) if (parameters === undefined) return scriptId = script?.id || fileOrFolder.toString() files = [] } else { const script = await pickTemplate() if (!script) return - parameters = await showPromptParametersQuickPicks(script) + parameters = await showPromptParametersQuickPicks( + script, + defaultValues + ) if (parameters === undefined) return scriptId = script.id files = fileOrFolders?.map((f) => f.fsPath) || [ @@ -98,6 +109,10 @@ export function activateFragmentCommands(state: ExtensionState) { const scriptDebug = async (file: vscode.Uri) => { if (!file) return + + // grab this early before any UI interactions + const selectedText = getSelectedText() + await state.cancelAiRequest() await state.parseWorkspace() @@ -114,7 +129,11 @@ export function activateFragmentCommands(state: ExtensionState) { if (!script) return files = [file] } - const parameters = await showPromptParametersQuickPicks(script) + const defaultValues = { selectedText } + const parameters = await showPromptParametersQuickPicks( + script, + defaultValues + ) if (parameters === undefined) return const { cliPath, cliVersion } = await resolveCli(state) diff --git a/packages/vscode/src/parameterquickpick.ts b/packages/vscode/src/parameterquickpick.ts index 3eb9caf2ea..55e5fe94ee 100644 --- a/packages/vscode/src/parameterquickpick.ts +++ b/packages/vscode/src/parameterquickpick.ts @@ -9,16 +9,21 @@ export interface TemplateQuickPickItem extends vscode.QuickPickItem { } export async function showPromptParametersQuickPicks( - script: PromptScript + script: PromptScript, + defaultValues?: PromptParameters ): Promise { - const parameters: PromptParameters = {} + const parameters: PromptParameters = structuredClone(defaultValues || {}) if (!script?.parameters) return parameters - for (const param in script.parameters || {}) { + const params = script.parameters || {} + + for (const param in params) { + if (parameters[param] !== undefined) continue + const schema = promptParameterTypeToJSONSchema(script.parameters[param]) switch (schema.type) { case "string": { - let value: string + let value: string | undefined const enums = schema.enum const uiSuggestions = schema.uiSuggestions if (enums?.length) { diff --git a/packages/vscode/src/selection.ts b/packages/vscode/src/selection.ts new file mode 100644 index 0000000000..7f5bd5d8a5 --- /dev/null +++ b/packages/vscode/src/selection.ts @@ -0,0 +1,13 @@ +import * as vscode from "vscode" + +export function getSelectedText() { + const textEditor = vscode.window.activeTextEditor + if (!textEditor) return undefined + + const selection = textEditor.selection + if (!selection) return undefined + + // get selected text + const selectedText = textEditor.document.getText(selection) + return selectedText +}