Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

<!-- autogenerated, do not edit -->
Expand Down
7 changes: 1 addition & 6 deletions docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
36 changes: 24 additions & 12 deletions docs/src/content/docs/reference/vscode/running-scripts.mdx
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/sample/genaisrc/selected-text.genai.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
script({
model: "echo"
})

def("TEXT", env.vars["editor.selectedText"])
$`Summarize <TEXT> in a single sentence.`
25 changes: 22 additions & 3 deletions packages/vscode/src/fragmentcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[])
Expand All @@ -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) || [
Expand All @@ -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()

Expand All @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions packages/vscode/src/parameterquickpick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ export interface TemplateQuickPickItem extends vscode.QuickPickItem {
}

export async function showPromptParametersQuickPicks(
script: PromptScript
script: PromptScript,
defaultValues?: PromptParameters
): Promise<PromptParameters> {
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) {
Expand Down
13 changes: 13 additions & 0 deletions packages/vscode/src/selection.ts
Original file line number Diff line number Diff line change
@@ -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
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function getSelectedText() is not explicitly typed. In TypeScript, exported functions should specify their return types for better type safety and maintainability. See: https://www.typescriptlang.org/docs/handbook/2/functions.html#return-type-annotation

AI-generated content by prr missing-explicit-export-type-ts may be incorrect

Loading