@fragola-ai/prompt 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Prompt.d.ts +85 -0
- package/dist/build.d.ts +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -180
- package/dist/index.js.map +4 -4
- package/dist/utils.d.ts +1 -0
- package/package.json +3 -3
package/dist/Prompt.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a record of variables used for string interpolation in prompts.
|
|
3
|
+
* Each key in the record is a variable name to interpolate in the prompt template,
|
|
4
|
+
* and its value can be any type (object, array, number, string, etc.) which will replace the placeholder.
|
|
5
|
+
*/
|
|
6
|
+
export type Variables = Record<string, any>;
|
|
7
|
+
export declare class MissingVariablesError extends Error {
|
|
8
|
+
variables: string[];
|
|
9
|
+
constructor(variables: string[], prompt: string);
|
|
10
|
+
}
|
|
11
|
+
export declare class NamespaceUndefinedError extends Error {
|
|
12
|
+
namespace: string;
|
|
13
|
+
constructor(namespace: string);
|
|
14
|
+
}
|
|
15
|
+
export declare class LoadFileReadError extends Error {
|
|
16
|
+
filePath: string;
|
|
17
|
+
originalError: Error;
|
|
18
|
+
constructor(filePath: string, originalError: Error);
|
|
19
|
+
}
|
|
20
|
+
export type Load = {
|
|
21
|
+
load: true;
|
|
22
|
+
relativePath: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Creates a Load object to specify a prompt file location.
|
|
26
|
+
* This function is used when the prompt is located in a file system.
|
|
27
|
+
* The process working directory (PWD) is used as the base path by default.
|
|
28
|
+
* Path aliases can be used to reduce path overhead.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} relativePath - The relative path to the prompt file. Can include path aliases.
|
|
31
|
+
* @returns {Load} An object containing the load flag and the relative path to the prompt file
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // Using relative path
|
|
35
|
+
* load('./prompts/myPrompt.txt')
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Using path alias
|
|
39
|
+
* load('prompts:myPrompt.txt')
|
|
40
|
+
*/
|
|
41
|
+
export declare const load: (relativePath: string) => Load;
|
|
42
|
+
export default class Prompt {
|
|
43
|
+
#private;
|
|
44
|
+
private static defaultVariableRegex;
|
|
45
|
+
/**
|
|
46
|
+
* A map of aliases to file paths for use with the {@link load} function.
|
|
47
|
+
* Allows defining shortcuts for commonly used prompt file paths.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* Prompt.pathAlias = {
|
|
52
|
+
* browse: "prompts", // Maps "@browse" to "prompts/"
|
|
53
|
+
* refund: "prompts/refund" // Maps "@refund" to "prompts/refund/"
|
|
54
|
+
* }
|
|
55
|
+
*
|
|
56
|
+
* // Usage with load():
|
|
57
|
+
* load("@refund/shopRefund.md") // Resolves to "prompts/refund/shopRefund.md"
|
|
58
|
+
* ```
|
|
59
|
+
* @see {@link load}
|
|
60
|
+
*/
|
|
61
|
+
static pathAlias: Record<string, string>;
|
|
62
|
+
constructor(prompt: string | Load, variables?: Variables);
|
|
63
|
+
/**
|
|
64
|
+
* Updates the variables used for string interpolation and re-processes the prompt template.
|
|
65
|
+
*
|
|
66
|
+
* @param variables - Your new variables
|
|
67
|
+
* @throws {MissingVariablesError} When required variables are not provided in the variables object
|
|
68
|
+
*/
|
|
69
|
+
setVariables(variables: Variables): void;
|
|
70
|
+
/**
|
|
71
|
+
* Gets the original prompt string used for this instance. Before any processing
|
|
72
|
+
* @returns {string} The original prompt string
|
|
73
|
+
*/
|
|
74
|
+
get promptString(): string;
|
|
75
|
+
/**
|
|
76
|
+
* Gets the variables currently in use for this prompt.
|
|
77
|
+
* @returns {Map<string, any>} A map containing the prompt variables and their values.
|
|
78
|
+
*/
|
|
79
|
+
get variables(): Variables;
|
|
80
|
+
/**
|
|
81
|
+
* Gets the interpolated value of the prompt.
|
|
82
|
+
* @returns {string} The resulting string after all variable interpolation has been applied.
|
|
83
|
+
*/
|
|
84
|
+
get value(): string;
|
|
85
|
+
}
|
package/dist/build.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
CHANGED
|
@@ -1,187 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var Prompt_exports = {};
|
|
9
|
-
__export(Prompt_exports, {
|
|
10
|
-
LoadFileReadError: () => LoadFileReadError,
|
|
11
|
-
MissingVariablesError: () => MissingVariablesError,
|
|
12
|
-
NamespaceUndefinedError: () => NamespaceUndefinedError,
|
|
13
|
-
default: () => Prompt,
|
|
14
|
-
load: () => load
|
|
15
|
-
});
|
|
16
|
-
import { readFileSync } from "fs";
|
|
17
|
-
import path from "path";
|
|
18
|
-
var MissingVariablesError = class extends Error {
|
|
19
|
-
constructor(variables, prompt) {
|
|
20
|
-
super(`Missing variables: ${variables.map((v) => `'${v}'`).join(", ")}. For prompt: ${prompt}`);
|
|
21
|
-
this.variables = variables;
|
|
22
|
-
this.name = "MissingVariablesError";
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
var NamespaceUndefinedError = class extends Error {
|
|
26
|
-
constructor(namespace) {
|
|
27
|
-
super(`Tried to acess undefined namespace when using load() function: '${namespace}' does not exist`);
|
|
28
|
-
this.namespace = namespace;
|
|
29
|
-
this.name = "NamespaceUndefinedError";
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
var LoadFileReadError = class extends Error {
|
|
33
|
-
constructor(filePath, originalError) {
|
|
34
|
-
super(`Failed to load prompt from ${filePath}: ${originalError.message}`);
|
|
35
|
-
this.filePath = filePath;
|
|
36
|
-
this.originalError = originalError;
|
|
37
|
-
this.name = "LoadFileReadError";
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var load = (relativePath) => ({ load: true, relativePath });
|
|
41
|
-
var Prompt = class _Prompt {
|
|
42
|
-
static defaultVariableRegex = /\{\{(\s*[\w.]+\s*)\}\}/g;
|
|
43
|
-
/**
|
|
44
|
-
* A map of aliases to file paths for use with the {@link load} function.
|
|
45
|
-
* Allows defining shortcuts for commonly used prompt file paths.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```typescript
|
|
49
|
-
* Prompt.pathAlias = {
|
|
50
|
-
* browse: "prompts", // Maps "@browse" to "prompts/"
|
|
51
|
-
* refund: "prompts/refund" // Maps "@refund" to "prompts/refund/"
|
|
52
|
-
* }
|
|
53
|
-
*
|
|
54
|
-
* // Usage with load():
|
|
55
|
-
* load("@refund/shopRefund.md") // Resolves to "prompts/refund/shopRefund.md"
|
|
56
|
-
* ```
|
|
57
|
-
* @see {@link load}
|
|
58
|
-
*/
|
|
59
|
-
static pathAlias = {};
|
|
60
|
-
#originalPrompt = "";
|
|
61
|
-
#value = "";
|
|
62
|
-
#variableRegex = _Prompt.defaultVariableRegex;
|
|
63
|
-
#variables = {};
|
|
64
|
-
#basePath = process.env["PWD"];
|
|
65
|
-
constructor(prompt, variables = {}) {
|
|
66
|
-
if (!process.env["PWD"])
|
|
67
|
-
console.warn("PWD env variable not set");
|
|
68
|
-
if (typeof prompt == "string") {
|
|
69
|
-
this.#originalPrompt = prompt;
|
|
70
|
-
this.#value = prompt;
|
|
71
|
-
} else {
|
|
72
|
-
this.#originalPrompt = this.#load(prompt.relativePath);
|
|
73
|
-
this.#value = this.#originalPrompt;
|
|
74
|
-
}
|
|
75
|
-
this.setVariables(variables);
|
|
76
|
-
}
|
|
77
|
-
#stringify(value) {
|
|
78
|
-
switch (typeof value) {
|
|
79
|
-
case "object":
|
|
80
|
-
return JSON.stringify(value);
|
|
81
|
-
default:
|
|
82
|
-
return value;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
#isInsideCodeBlock(index, text) {
|
|
86
|
-
const codeBlockRegex = /```[\s\S]*?```/g;
|
|
87
|
-
let match;
|
|
88
|
-
while ((match = codeBlockRegex.exec(text)) !== null) {
|
|
89
|
-
const start = match.index;
|
|
90
|
-
const end = start + match[0].length;
|
|
91
|
-
if (index >= start && index < end) {
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Updates the variables used for string interpolation and re-processes the prompt template.
|
|
99
|
-
*
|
|
100
|
-
* @param variables - Your new variables
|
|
101
|
-
* @throws {MissingVariablesError} When required variables are not provided in the variables object
|
|
102
|
-
*/
|
|
103
|
-
setVariables(variables) {
|
|
104
|
-
this.#variables = variables;
|
|
105
|
-
const allMatch = Array.from(this.#originalPrompt.matchAll(this.#variableRegex)).filter((match) => !this.#isInsideCodeBlock(match.index, this.#originalPrompt)).map((match) => {
|
|
106
|
-
return {
|
|
107
|
-
placeholder: match[0],
|
|
108
|
-
index: match.index
|
|
109
|
-
};
|
|
110
|
-
});
|
|
111
|
-
const getVariableNameFromPlaceholder = (placeholder) => {
|
|
112
|
-
return placeholder.replaceAll(/[{}]/g, "").trim();
|
|
113
|
-
};
|
|
114
|
-
let missingVariables = [];
|
|
115
|
-
let applyReplace = [];
|
|
116
|
-
allMatch.forEach((match) => {
|
|
117
|
-
const variableName = getVariableNameFromPlaceholder(match.placeholder);
|
|
118
|
-
if (variableName in variables)
|
|
119
|
-
applyReplace.push(() => this.#value = this.#value.replace(match.placeholder, this.#stringify(this.#variables[variableName])));
|
|
120
|
-
else
|
|
121
|
-
missingVariables.push(variableName);
|
|
122
|
-
});
|
|
123
|
-
if (missingVariables.length)
|
|
124
|
-
throw new MissingVariablesError(missingVariables, this.#originalPrompt);
|
|
125
|
-
applyReplace.forEach((replace) => replace());
|
|
126
|
-
}
|
|
127
|
-
#load(relativePath) {
|
|
128
|
-
let _relativePath = (() => {
|
|
129
|
-
const split = relativePath.split("/");
|
|
130
|
-
if (split.length > 1) {
|
|
131
|
-
const namespace = split[0].trim();
|
|
132
|
-
if (namespace[0] == "@") {
|
|
133
|
-
const name = split[0].slice(1);
|
|
134
|
-
if (name in _Prompt.pathAlias) {
|
|
135
|
-
return path.join(_Prompt.pathAlias[name], split[1]);
|
|
136
|
-
} else
|
|
137
|
-
throw new NamespaceUndefinedError(namespace);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return relativePath;
|
|
141
|
-
})();
|
|
142
|
-
let fullPath = path.join(this.#basePath || "./", _relativePath);
|
|
143
|
-
if (!fullPath.split("/").at(-1)?.includes("."))
|
|
144
|
-
fullPath = `${fullPath}.md`;
|
|
145
|
-
try {
|
|
146
|
-
const content = readFileSync(fullPath, "utf-8");
|
|
147
|
-
return content;
|
|
148
|
-
} catch (error) {
|
|
149
|
-
if (error instanceof Error) {
|
|
150
|
-
throw new LoadFileReadError(fullPath, error);
|
|
151
|
-
}
|
|
152
|
-
throw error;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Gets the original prompt string used for this instance. Before any processing
|
|
157
|
-
* @returns {string} The original prompt string
|
|
158
|
-
*/
|
|
159
|
-
get promptString() {
|
|
160
|
-
return this.#originalPrompt;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Gets the variables currently in use for this prompt.
|
|
164
|
-
* @returns {Map<string, any>} A map containing the prompt variables and their values.
|
|
165
|
-
*/
|
|
166
|
-
get variables() {
|
|
167
|
-
return this.#variables;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Gets the interpolated value of the prompt.
|
|
171
|
-
* @returns {string} The resulting string after all variable interpolation has been applied.
|
|
172
|
-
*/
|
|
173
|
-
get value() {
|
|
174
|
-
return this.#value;
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// index.ts
|
|
179
|
-
var index_default = Prompt_exports;
|
|
1
|
+
import { default as default2 } from "./Prompt";
|
|
2
|
+
import {
|
|
3
|
+
load,
|
|
4
|
+
MissingVariablesError,
|
|
5
|
+
NamespaceUndefinedError,
|
|
6
|
+
LoadFileReadError
|
|
7
|
+
} from "./Prompt";
|
|
180
8
|
export {
|
|
181
9
|
LoadFileReadError,
|
|
182
10
|
MissingVariablesError,
|
|
183
11
|
NamespaceUndefinedError,
|
|
184
|
-
|
|
12
|
+
default2 as Prompt,
|
|
185
13
|
load
|
|
186
14
|
};
|
|
187
15
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../index.ts"],
|
|
4
|
+
"sourcesContent": ["export { default as Prompt } from './Prompt';\nexport { \n load, \n MissingVariablesError, \n NamespaceUndefinedError, \n LoadFileReadError,\n type Variables,\n type Load \n} from './Prompt';"],
|
|
5
|
+
"mappings": "AAAA,SAAoB,WAAXA,gBAAyB;AAClC;AAAA,EACI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGG;",
|
|
6
|
+
"names": ["default"]
|
|
7
7
|
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fragola-ai/prompt",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Prompt interpolation and loading library for Node.js/TypeScript.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/
|
|
27
|
+
"@types/node": "^20.0.0",
|
|
28
28
|
"esbuild": "^0.25.9"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
@@ -36,4 +36,4 @@
|
|
|
36
36
|
},
|
|
37
37
|
"author": "shadokan87",
|
|
38
38
|
"license": "MIT"
|
|
39
|
-
}
|
|
39
|
+
}
|