diff --git a/packages/cli/src/sarif.ts b/packages/cli/src/sarif.ts
index 468e7cc3c8..01cea82da1 100644
--- a/packages/cli/src/sarif.ts
+++ b/packages/cli/src/sarif.ts
@@ -13,7 +13,7 @@ export function isSARIFFilename(f: string) {
// use with MS-SarifVSCode.sarif-viewer
export function convertDiagnosticsToSARIF(
- template: PromptTemplate,
+ template: PromptScript,
issues: Diagnostic[]
) {
const sarifRunBuilder = new SarifRunBuilder().initSimple({
diff --git a/packages/core/bundleprompts.mjs b/packages/core/bundleprompts.mjs
index 7f39ea9839..bcef47ff01 100644
--- a/packages/core/bundleprompts.mjs
+++ b/packages/core/bundleprompts.mjs
@@ -33,7 +33,10 @@ const promptDefs = {
null,
4
),
- "genaiscript.d.ts": ["./src/prompt_template.d.ts", "./prompt_type.d.ts"]
+ "genaiscript.d.ts": [
+ "./src/types/prompt_template.d.ts",
+ "./src/types/prompt_type.d.ts",
+ ]
.map((fn) => readFileSync(fn, { encoding: "utf-8" }))
.map((src) =>
src.replace(/^\/\/\/\s+\s*$/gm, "")
diff --git a/packages/core/src/ast.ts b/packages/core/src/ast.ts
index f12e0d1477..2bb57e89e4 100644
--- a/packages/core/src/ast.ts
+++ b/packages/core/src/ast.ts
@@ -1,9 +1,9 @@
-///
+///
import { host } from "./host"
-type PromptTemplate = globalThis.PromptTemplate
-export type { PromptTemplate }
+type PromptScript = globalThis.PromptScript
+export type { PromptScript }
export interface FileReference {
name: string
@@ -77,7 +77,7 @@ export class Fragment {
}
}
-export function templateGroup(template: PromptTemplate) {
+export function templateGroup(template: PromptScript) {
return (
template.group ||
(/^system/i.test(template.id) ? "system" : "") ||
@@ -94,7 +94,7 @@ export class Project {
readonly fragmentById: Record = {}
readonly fragmentByFullId: Record = {}
readonly allFragments: Fragment[] = []
- readonly templates: PromptTemplate[] = []
+ readonly templates: PromptScript[] = []
readonly diagnostics: Diagnostic[] = []
_finalizers: (() => void)[] = []
diff --git a/packages/core/src/clihelp.ts b/packages/core/src/clihelp.ts
index e3d6dbb94a..525365bba8 100644
--- a/packages/core/src/clihelp.ts
+++ b/packages/core/src/clihelp.ts
@@ -4,7 +4,7 @@ import { NPM_CLI_PACKAGE } from "./constants"
import { MarkdownTrace } from "./trace"
export function generateCliArguments(
- template: PromptTemplate,
+ template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions,
command: "run" | "batch"
@@ -30,7 +30,7 @@ export function generateCliArguments(
export function traceCliArgs(
trace: MarkdownTrace,
- template: PromptTemplate,
+ template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions
) {
diff --git a/packages/core/src/copy.ts b/packages/core/src/copy.ts
index cccc3e0995..86fa4d2735 100644
--- a/packages/core/src/copy.ts
+++ b/packages/core/src/copy.ts
@@ -1,4 +1,4 @@
-import { PromptTemplate } from "./ast"
+import { PromptScript } from "./ast"
import { GENAI_JS_EXT, GENAI_SRC } from "./constants"
import { host } from "./host"
import { fileExists, writeText } from "./fs"
@@ -9,7 +9,7 @@ function promptPath(id: string) {
return host.resolvePath(prompts, id + GENAI_JS_EXT)
}
export async function copyPrompt(
- t: PromptTemplate,
+ t: PromptScript,
options: { fork: boolean; name?: string }
) {
await host.createDirectory(promptPath(null))
diff --git a/packages/core/src/evalprompt.ts b/packages/core/src/evalprompt.ts
index 621ce7d4f9..deb9f12a37 100644
--- a/packages/core/src/evalprompt.ts
+++ b/packages/core/src/evalprompt.ts
@@ -4,7 +4,7 @@ import { consoleLogFormat } from "./logging"
export async function evalPrompt(
ctx0: PromptContext,
- r: PromptTemplate,
+ r: PromptScript,
options?: {
sourceMaps?: boolean
logCb?: (msg: string) => void
diff --git a/packages/core/src/expander.ts b/packages/core/src/expander.ts
index 7507e9abf0..5f9b5ba387 100644
--- a/packages/core/src/expander.ts
+++ b/packages/core/src/expander.ts
@@ -1,4 +1,4 @@
-import { Fragment, PromptTemplate } from "./ast"
+import { Fragment, PromptScript } from "./ast"
import { assert, normalizeFloat, normalizeInt, normalizeString } from "./util"
import { MarkdownTrace } from "./trace"
import { isCancelError } from "./error"
@@ -75,7 +75,7 @@ export interface FragmentTransformResponse extends PromptGenerationOutput {
}
async function callExpander(
- r: PromptTemplate,
+ r: PromptScript,
vars: ExpansionVariables,
trace: MarkdownTrace,
options: RunTemplateOptions
@@ -198,7 +198,7 @@ function traceEnv(
trace.endDetails()
}
-function resolveSystems(template: PromptTemplate) {
+function resolveSystems(template: PromptScript) {
const { jsSource } = template
const systems = (template.system ?? []).slice(0)
if (template.system === undefined) {
@@ -219,7 +219,7 @@ function resolveSystems(template: PromptTemplate) {
}
export async function expandTemplate(
- template: PromptTemplate,
+ template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions,
env: ExpansionVariables,
diff --git a/packages/core/src/genaisrc/genaiscript.d.ts b/packages/core/src/genaisrc/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/core/src/genaisrc/genaiscript.d.ts
+++ b/packages/core/src/genaisrc/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/core/src/importprompt.ts b/packages/core/src/importprompt.ts
index b701a545eb..fdfd8348e9 100644
--- a/packages/core/src/importprompt.ts
+++ b/packages/core/src/importprompt.ts
@@ -12,7 +12,7 @@ function resolveGlobal(): any {
export async function importPrompt(
ctx0: PromptContext,
- r: PromptTemplate,
+ r: PromptScript,
options?: {
logCb?: (msg: string) => void
}
diff --git a/packages/core/src/parser.ts b/packages/core/src/parser.ts
index 27e94e62be..a459a42044 100644
--- a/packages/core/src/parser.ts
+++ b/packages/core/src/parser.ts
@@ -1,6 +1,6 @@
import { lookupMime } from "./mime"
import { sha256string, strcmp } from "./util"
-import { Project, TextFile, PromptTemplate, Fragment } from "./ast"
+import { Project, TextFile, PromptScript, Fragment } from "./ast"
import { defaultPrompts } from "./default_prompts"
import { parsePromptTemplate } from "./template"
import { fileExists, readText } from "./fs"
@@ -116,7 +116,7 @@ export async function parseProject(options: {
}
runFinalizers()
- function templKey(t: PromptTemplate) {
+ function templKey(t: PromptScript) {
const pref = t.unlisted ? "Z" : t.filename ? "A" : "B"
return pref + t.title + t.id
}
diff --git a/packages/core/src/promptdom.ts b/packages/core/src/promptdom.ts
index 183848285f..8f14903f1e 100644
--- a/packages/core/src/promptdom.ts
+++ b/packages/core/src/promptdom.ts
@@ -29,20 +29,20 @@ export interface PromptNode extends ContextExpansionOptions {
export interface PromptTextNode extends PromptNode {
type: "text"
- value: string | Promise
+ value: Awaitable
resolved?: string
}
export interface PromptDefNode extends PromptNode, DefOptions {
type: "def"
name: string
- value: LinkedFile | Promise
+ value: Awaitable
resolved?: LinkedFile
}
export interface PromptAssistantNode extends PromptNode {
type: "assistant"
- value: string | Promise
+ value: Awaitable
resolve?: string
}
@@ -61,7 +61,7 @@ export interface PromptImage {
export interface PromptImageNode extends PromptNode {
type: "image"
- value: PromptImage | Promise
+ value: Awaitable
resolved?: PromptImage
}
@@ -91,7 +91,7 @@ export interface PromptOutputProcessorNode extends PromptNode {
}
export function createTextNode(
- value: string | Promise,
+ value: Awaitable,
options?: ContextExpansionOptions
): PromptTextNode {
assert(value !== undefined)
@@ -157,7 +157,7 @@ function renderDefNode(def: PromptDefNode): string {
}
export function createAssistantNode(
- value: string | Promise,
+ value: Awaitable,
options?: ContextExpansionOptions
): PromptAssistantNode {
assert(value !== undefined)
@@ -174,7 +174,7 @@ export function createStringTemplateNode(
}
export function createImageNode(
- value: PromptImage | Promise,
+ value: Awaitable,
options?: ContextExpansionOptions
): PromptImageNode {
assert(value !== undefined)
@@ -255,17 +255,17 @@ export function appendChild(parent: PromptNode, child: PromptNode): void {
}
export interface PromptNodeVisitor {
- node?: (node: PromptNode) => void | Promise
- afterNode?: (node: PromptNode) => void | Promise
- text?: (node: PromptTextNode) => void | Promise
- def?: (node: PromptDefNode) => void | Promise
- image?: (node: PromptImageNode) => void | Promise
- schema?: (node: PromptSchemaNode) => void | Promise
- function?: (node: PromptFunctionNode) => void | Promise
- fileMerge?: (node: PromptFileMergeNode) => void | Promise
- stringTemplate?: (node: PromptStringTemplateNode) => void | Promise
- outputProcessor?: (node: PromptOutputProcessorNode) => void | Promise
- assistant?: (node: PromptAssistantNode) => void | Promise
+ node?: (node: PromptNode) => Awaitable
+ afterNode?: (node: PromptNode) => Awaitable
+ text?: (node: PromptTextNode) => Awaitable
+ def?: (node: PromptDefNode) => Awaitable
+ image?: (node: PromptImageNode) => Awaitable
+ schema?: (node: PromptSchemaNode) => Awaitable
+ function?: (node: PromptFunctionNode) => Awaitable
+ fileMerge?: (node: PromptFileMergeNode) => Awaitable
+ stringTemplate?: (node: PromptStringTemplateNode) => Awaitable
+ outputProcessor?: (node: PromptOutputProcessorNode) => Awaitable
+ assistant?: (node: PromptAssistantNode) => Awaitable
}
export async function visitNode(node: PromptNode, visitor: PromptNodeVisitor) {
diff --git a/packages/core/src/promptrunner.ts b/packages/core/src/promptrunner.ts
index 7d4bca8b3b..dfc2b62e11 100644
--- a/packages/core/src/promptrunner.ts
+++ b/packages/core/src/promptrunner.ts
@@ -1,5 +1,5 @@
import { ChatCompletionResponse, ChatCompletionTool } from "./chat"
-import { Fragment, PromptTemplate } from "./ast"
+import { Fragment, PromptScript } from "./ast"
import { commentAttributes, stringToPos } from "./parser"
import { assert, logVerbose, relativePath } from "./util"
import {
@@ -31,7 +31,7 @@ import { createFetch } from "./fetch"
async function fragmentVars(
trace: MarkdownTrace,
- template: PromptTemplate,
+ template: PromptScript,
frag: Fragment
) {
const { file } = frag
@@ -133,7 +133,7 @@ async function fragmentVars(
}
export async function runTemplate(
- template: PromptTemplate,
+ template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions
): Promise {
diff --git a/packages/core/src/scripts.ts b/packages/core/src/scripts.ts
index 35e84e153f..d5d1b422da 100644
--- a/packages/core/src/scripts.ts
+++ b/packages/core/src/scripts.ts
@@ -6,7 +6,7 @@ import { logVerbose } from "./util";
export function createScript(
name: string,
- options?: { template: PromptTemplate; title?: string }
+ options?: { template: PromptScript; title?: string }
) {
const { template, title } = options || {}
const t = structuredClone(
diff --git a/packages/core/src/template.ts b/packages/core/src/template.ts
index 5a9da42253..d23bfa3a6a 100644
--- a/packages/core/src/template.ts
+++ b/packages/core/src/template.ts
@@ -1,4 +1,4 @@
-import { Project, Fragment, PromptTemplate } from "./ast"
+import { Project, Fragment, PromptScript } from "./ast"
import { BUILTIN_PREFIX } from "./constants"
import { JSON5TryParse } from "./json5"
function templateIdFromFileName(filename: string) {
@@ -178,7 +178,7 @@ class Checker {
}
}
-async function parseMeta(r: PromptTemplate) {
+async function parseMeta(r: PromptScript) {
// shortcut
const m = /\b(?system|script)\(\s*(?\{.*?\})\s*\)/s.exec(
r.jsSource
@@ -384,18 +384,18 @@ async function parsePromptTemplateCore(
filename: string,
content: string,
prj: Project,
- finalizer: (checker: Checker) => void
+ finalizer: (checker: Checker) => void
) {
const r = {
id: templateIdFromFileName(filename),
text: "",
jsSource: content,
- } as PromptTemplate
+ } as PromptScript
if (!filename.startsWith(BUILTIN_PREFIX)) r.filename = filename
try {
const meta = await parseMeta(r)
- const checker = new Checker(
+ const checker = new Checker(
r,
filename,
prj.diagnostics,
diff --git a/packages/core/src/prompt_template.d.ts b/packages/core/src/types/prompt_template.d.ts
similarity index 98%
rename from packages/core/src/prompt_template.d.ts
rename to packages/core/src/types/prompt_template.d.ts
index a6306ae165..6e06232b05 100644
--- a/packages/core/src/prompt_template.d.ts
+++ b/packages/core/src/types/prompt_template.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
diff --git a/packages/core/prompt_type.d.ts b/packages/core/src/types/prompt_type.d.ts
similarity index 94%
rename from packages/core/prompt_type.d.ts
rename to packages/core/src/types/prompt_type.d.ts
index e43518cf09..acba79d511 100644
--- a/packages/core/prompt_type.d.ts
+++ b/packages/core/src/types/prompt_type.d.ts
@@ -1,4 +1,4 @@
-///
+///
// keep in sync with PromptContext!
@@ -17,7 +17,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -41,7 +44,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -159,9 +166,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/genaisrc/genaiscript.d.ts b/packages/sample/genaisrc/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/genaisrc/genaiscript.d.ts
+++ b/packages/sample/genaisrc/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/genaisrc/node/genaiscript.d.ts b/packages/sample/genaisrc/node/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/genaisrc/node/genaiscript.d.ts
+++ b/packages/sample/genaisrc/node/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/genaisrc/python/genaiscript.d.ts b/packages/sample/genaisrc/python/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/genaisrc/python/genaiscript.d.ts
+++ b/packages/sample/genaisrc/python/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/genaisrc/style/genaiscript.d.ts b/packages/sample/genaisrc/style/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/genaisrc/style/genaiscript.d.ts
+++ b/packages/sample/genaisrc/style/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/src/aici/genaiscript.d.ts b/packages/sample/src/aici/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/src/aici/genaiscript.d.ts
+++ b/packages/sample/src/aici/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/src/makecode/genaiscript.d.ts b/packages/sample/src/makecode/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/src/makecode/genaiscript.d.ts
+++ b/packages/sample/src/makecode/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/src/tla/genaiscript.d.ts b/packages/sample/src/tla/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/src/tla/genaiscript.d.ts
+++ b/packages/sample/src/tla/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/sample/src/vision/genaiscript.d.ts b/packages/sample/src/vision/genaiscript.d.ts
index 336e9fba97..5b3a97ec28 100644
--- a/packages/sample/src/vision/genaiscript.d.ts
+++ b/packages/sample/src/vision/genaiscript.d.ts
@@ -6,6 +6,8 @@ interface Diagnostic {
message: string
}
+type Awaitable = T | PromiseLike
+
interface PromptDefinition {
/**
* Based on file name.
@@ -48,7 +50,7 @@ type FileMergeHandler = (
label: string,
before: string,
generated: string
-) => string | Promise
+) => Awaitable
interface PromptOutputProcessorResult {
/**
@@ -188,7 +190,7 @@ type PromptParameterType =
type PromptParametersSchema = Record
type PromptParameters = Record
-interface PromptTemplate
+interface PromptScript
extends PromptLike,
ModelOptions,
ScriptRuntimeOptions {
@@ -398,7 +400,7 @@ interface ExpansionVariables {
type MakeOptional = Partial> & Omit
-type PromptArgs = Omit
+type PromptArgs = Omit
type PromptSystemArgs = Omit<
PromptArgs,
@@ -910,11 +912,11 @@ interface WriteTextOptions extends ContextExpansionOptions {
assistant?: boolean
}
-type RunPromptGenerator = (ctx: RunPromptContext) => void | Promise
+type RunPromptGenerator = (ctx: RunPromptContext) => Awaitable
// keep in sync with prompt_type.d.ts
interface RunPromptContext {
- writeText(body: string | Promise, options?: WriteTextOptions): void
+ writeText(body: Awaitable, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
@@ -1128,7 +1130,10 @@ declare function system(options: PromptSystemArgs): void
* Append given string to the prompt. It automatically appends "\n".
* Typically best to use `` $`...` ``-templates instead.
*/
-declare function writeText(body: string | Promise, options?: WriteTextOptions): void
+declare function writeText(
+ body: Awaitable,
+ options?: WriteTextOptions
+): void
/**
* Append given string to the prompt. It automatically appends "\n".
@@ -1152,7 +1157,11 @@ declare function fence(body: StringLike, options?: FenceOptions): void
* @param body string to be fenced/defined
* @returns variable name
*/
-declare function def(name: string, body: StringLike, options?: DefOptions): string
+declare function def(
+ name: string,
+ body: StringLike,
+ options?: DefOptions
+): string
/**
* Declares a function that can be called from the prompt.
@@ -1270,9 +1279,8 @@ declare function runPrompt(
options?: ModelOptions
): Promise
-
/**
* Registers a callback to process the LLM output
- * @param fn
+ * @param fn
*/
declare function defOutput(fn: PromptOutputProcessorHandler): void
diff --git a/packages/vscode/src/fragmentcommands.ts b/packages/vscode/src/fragmentcommands.ts
index 0f9b482619..fe786d9de9 100644
--- a/packages/vscode/src/fragmentcommands.ts
+++ b/packages/vscode/src/fragmentcommands.ts
@@ -3,7 +3,7 @@ import {
Fragment,
GENAI_JS_REGEX,
NotSupportedError,
- PromptTemplate,
+ PromptScript,
assert,
dotGenaiscriptPath,
groupBy,
@@ -14,12 +14,12 @@ import { ExtensionState } from "./state"
import { checkDirectoryExists, checkFileExists } from "./fs"
type TemplateQuickPickItem = {
- template?: PromptTemplate
+ template?: PromptScript
action?: "create"
} & vscode.QuickPickItem
async function showPromptParametersQuickPicks(
- template: PromptTemplate
+ template: PromptScript
): Promise {
const parameters: PromptParameters = {}
for (const param in template.parameters || {}) {
@@ -75,7 +75,7 @@ export function activateFragmentCommands(state: ExtensionState) {
const { subscriptions } = context
const pickTemplate = async (options?: {
- filter?: (p: PromptTemplate) => boolean
+ filter?: (p: PromptScript) => boolean
}) => {
const { filter = () => true } = options || {}
const templates = state.project.templates
@@ -131,7 +131,7 @@ export function activateFragmentCommands(state: ExtensionState) {
options:
| {
fragment?: Fragment | string | vscode.Uri
- template?: PromptTemplate
+ template?: PromptScript
}
| vscode.Uri
) => {
@@ -184,7 +184,7 @@ export function activateFragmentCommands(state: ExtensionState) {
await state.cancelAiRequest()
await state.parseWorkspace()
- let template: PromptTemplate
+ let template: PromptScript
let files: vscode.Uri[]
if (GENAI_JS_REGEX.test(file.path)) {
template = state.project.templates.find(
@@ -252,7 +252,7 @@ export function activateFragmentCommands(state: ExtensionState) {
}
export function templatesToQuickPickItems(
- templates: globalThis.PromptTemplate[],
+ templates: globalThis.PromptScript[],
options?: { create?: boolean }
): TemplateQuickPickItem[] {
const { create } = options || {}
diff --git a/packages/vscode/src/promptcommands.ts b/packages/vscode/src/promptcommands.ts
index 9c956ef275..2dfc8c185b 100644
--- a/packages/vscode/src/promptcommands.ts
+++ b/packages/vscode/src/promptcommands.ts
@@ -1,6 +1,6 @@
import * as vscode from "vscode"
import { ExtensionState } from "./state"
-import { PromptTemplate, copyPrompt, createScript } from "genaiscript-core"
+import { PromptScript, copyPrompt, createScript } from "genaiscript-core"
import { builtinPromptUri } from "./markdowndocumentprovider"
import { templatesToQuickPickItems } from "./fragmentcommands"
@@ -26,7 +26,7 @@ export function activatePromptCommands(state: ExtensionState) {
),
vscode.commands.registerCommand(
"genaiscript.prompt.create",
- async (template?: PromptTemplate) => {
+ async (template?: PromptScript) => {
const name = await vscode.window.showInputBox({
title: "Pick a file name for the new GenAiScript.",
})
@@ -38,7 +38,7 @@ export function activatePromptCommands(state: ExtensionState) {
),
vscode.commands.registerCommand(
"genaiscript.prompt.fork",
- async (template: PromptTemplate) => {
+ async (template: PromptScript) => {
if (!template) {
if (!state.project) await state.parseWorkspace()
const templates = state.project?.templates
@@ -64,14 +64,14 @@ export function activatePromptCommands(state: ExtensionState) {
),
vscode.commands.registerCommand(
"genaiscript.prompt.unbuiltin",
- async (template: PromptTemplate) => {
+ async (template: PromptScript) => {
if (!template) return
await showPrompt(await copyPrompt(template, { fork: false }))
}
),
vscode.commands.registerCommand(
"genaiscript.prompt.navigate",
- async (prompt: PromptTemplate) => {
+ async (prompt: PromptScript) => {
const uri = prompt.filename
? vscode.Uri.file(prompt.filename)
: builtinPromptUri(prompt.id)
diff --git a/packages/vscode/src/prompttree.ts b/packages/vscode/src/prompttree.ts
index aef8919a1e..e037c9a936 100644
--- a/packages/vscode/src/prompttree.ts
+++ b/packages/vscode/src/prompttree.ts
@@ -2,12 +2,12 @@ import * as vscode from "vscode"
import { ExtensionState } from "./state"
import {
CHANGE,
- PromptTemplate,
+ PromptScript,
groupBy,
templateGroup,
} from "genaiscript-core"
-type PromptTreeNode = string | PromptTemplate
+type PromptTreeNode = string | PromptScript
class PromptTreeDataProvider
implements vscode.TreeDataProvider
diff --git a/packages/vscode/src/state.ts b/packages/vscode/src/state.ts
index dfdc55d370..11fc81cc6a 100644
--- a/packages/vscode/src/state.ts
+++ b/packages/vscode/src/state.ts
@@ -3,7 +3,7 @@ import {
ChatCompletionsProgressReport,
Project,
Fragment,
- PromptTemplate,
+ PromptScript,
concatArrays,
parseProject,
FragmentTransformResponse,
@@ -62,7 +62,7 @@ export const SEARCH_OUTPUT_FILENAME = "GenAIScript Search.md"
export interface AIRequestOptions {
label: string
- template: PromptTemplate
+ template: PromptScript
fragment: Fragment
parameters: PromptParameters
}