@nudge-ai/core 0.0.1-beta.0
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/index.cjs +121 -0
- package/dist/index.d.cts +94 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +94 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +119 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +43 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/builder.ts
|
|
3
|
+
function createBuilder(targetState) {
|
|
4
|
+
const state = targetState ?? [];
|
|
5
|
+
const builder = {
|
|
6
|
+
raw: (value) => (state.push({
|
|
7
|
+
type: "raw",
|
|
8
|
+
value
|
|
9
|
+
}), builder),
|
|
10
|
+
persona: (role) => (state.push({
|
|
11
|
+
type: "persona",
|
|
12
|
+
role
|
|
13
|
+
}), builder),
|
|
14
|
+
input: (description) => (state.push({
|
|
15
|
+
type: "input",
|
|
16
|
+
description
|
|
17
|
+
}), builder),
|
|
18
|
+
output: (description) => (state.push({
|
|
19
|
+
type: "output",
|
|
20
|
+
description
|
|
21
|
+
}), builder),
|
|
22
|
+
context: (information) => (state.push({
|
|
23
|
+
type: "context",
|
|
24
|
+
information
|
|
25
|
+
}), builder),
|
|
26
|
+
do: (instruction, options) => (state.push({
|
|
27
|
+
type: "do",
|
|
28
|
+
instruction,
|
|
29
|
+
nudge: options?.nudge
|
|
30
|
+
}), builder),
|
|
31
|
+
dont: (instruction, options) => (state.push({
|
|
32
|
+
type: "dont",
|
|
33
|
+
instruction,
|
|
34
|
+
nudge: options?.nudge
|
|
35
|
+
}), builder),
|
|
36
|
+
constraint: (rule, options) => (state.push({
|
|
37
|
+
type: "constraint",
|
|
38
|
+
rule,
|
|
39
|
+
nudge: options?.nudge
|
|
40
|
+
}), builder),
|
|
41
|
+
example: (input, output) => (state.push({
|
|
42
|
+
type: "example",
|
|
43
|
+
input,
|
|
44
|
+
output
|
|
45
|
+
}), builder),
|
|
46
|
+
use: (source) => (state.push(...source._state), builder),
|
|
47
|
+
optional: (name, builderFn) => {
|
|
48
|
+
const optionalSteps = [];
|
|
49
|
+
const { builder: innerBuilder } = createBuilder(optionalSteps);
|
|
50
|
+
builderFn(innerBuilder);
|
|
51
|
+
state.push({
|
|
52
|
+
type: "optional",
|
|
53
|
+
name,
|
|
54
|
+
steps: optionalSteps
|
|
55
|
+
});
|
|
56
|
+
return builder;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
builder,
|
|
61
|
+
state
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/steps.ts
|
|
67
|
+
function formatNudge(nudge) {
|
|
68
|
+
if (!nudge || nudge === 3) return "";
|
|
69
|
+
return `\nNudge: ${nudge}`;
|
|
70
|
+
}
|
|
71
|
+
function formatStepForAI(step) {
|
|
72
|
+
switch (step.type) {
|
|
73
|
+
case "raw": return `[Raw Text] (Include this text verbatim in the system prompt.)\nValue: "${step.value}"`;
|
|
74
|
+
case "persona": return `[Persona] (Define the identity and role the AI should assume. Frame this as 'You are...' at the start of the system prompt.)\nValue: "${step.role}"`;
|
|
75
|
+
case "input": return `[Input] (Describe what input the AI will receive from the user. Help the AI understand the context of what it will be working with.)\nValue: "${step.description}"`;
|
|
76
|
+
case "output": return `[Output] (Specify what the AI should produce as output. Be clear about the expected format and content.)\nValue: "${step.description}"`;
|
|
77
|
+
case "context": return `[Context] (Background information or context that helps the AI understand the situation. This is not an instruction, just helpful information.)\nValue: "${step.information}"`;
|
|
78
|
+
case "do": return `[Do] (A positive instruction the AI must follow.)\nValue: "${step.instruction}"${formatNudge(step.nudge)}`;
|
|
79
|
+
case "dont": return `[Don't] (A negative instruction - something the AI must avoid.)\nValue: "${step.instruction}"${formatNudge(step.nudge)}`;
|
|
80
|
+
case "constraint": return `[Constraint] (A rule or limitation the AI must respect.)\nValue: "${step.rule}"${formatNudge(step.nudge)}`;
|
|
81
|
+
case "example": return `[Example] (An input/output example showing the AI how to respond. Use these to demonstrate the expected behavior.)\nInput: "${step.input}"\nExpected output: "${step.output}"`;
|
|
82
|
+
case "optional": {
|
|
83
|
+
const innerSteps = step.steps.map(formatStepForAI).join("\n\n");
|
|
84
|
+
return `[Optional Block Start: "${step.name}"] (The following instructions are OPTIONAL. Wrap the generated content for these in {{#${step.name}}}...{{/${step.name}}} markers so it can be toggled at runtime.)\n\n${innerSteps}\n\n[Optional Block End: "${step.name}"]`;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/index.ts
|
|
91
|
+
let promptCache = {};
|
|
92
|
+
function registerPrompts(prompts) {
|
|
93
|
+
promptCache = {
|
|
94
|
+
...promptCache,
|
|
95
|
+
...prompts
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function processTemplate(text, options = {}) {
|
|
99
|
+
const processOptionals = (str) => str.replace(/\{\{#(\w+)\}\}([\s\S]*?)\{\{\/\1\}\}/g, (_, name, content) => options[name] ? processOptionals(content) : "");
|
|
100
|
+
const processVars = (str) => str.replace(/\{\{(?![#\/])(\w+)\}\}/g, (match, name) => {
|
|
101
|
+
const value = options[name];
|
|
102
|
+
return typeof value === "string" ? value : match;
|
|
103
|
+
});
|
|
104
|
+
return processVars(processOptionals(text)).replace(/\n{3,}/g, "\n\n");
|
|
105
|
+
}
|
|
106
|
+
function prompt(id, promptFunc) {
|
|
107
|
+
const { builder, state } = createBuilder();
|
|
108
|
+
promptFunc(builder);
|
|
109
|
+
return {
|
|
110
|
+
id,
|
|
111
|
+
_state: state,
|
|
112
|
+
toString: ((options) => {
|
|
113
|
+
return processTemplate(promptCache[id]?.text ?? "", options).trim();
|
|
114
|
+
})
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
//#endregion
|
|
119
|
+
exports.formatStepForAI = formatStepForAI;
|
|
120
|
+
exports.prompt = prompt;
|
|
121
|
+
exports.registerPrompts = registerPrompts;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//#region src/steps.d.ts
|
|
2
|
+
type Nudge = 1 | 2 | 3 | 4 | 5;
|
|
3
|
+
type RawStep = {
|
|
4
|
+
type: "raw";
|
|
5
|
+
value: string;
|
|
6
|
+
};
|
|
7
|
+
type PersonaStep = {
|
|
8
|
+
type: "persona";
|
|
9
|
+
role: string;
|
|
10
|
+
};
|
|
11
|
+
type InputStep = {
|
|
12
|
+
type: "input";
|
|
13
|
+
description: string;
|
|
14
|
+
};
|
|
15
|
+
type OutputStep = {
|
|
16
|
+
type: "output";
|
|
17
|
+
description: string;
|
|
18
|
+
};
|
|
19
|
+
type ContextStep = {
|
|
20
|
+
type: "context";
|
|
21
|
+
information: string;
|
|
22
|
+
};
|
|
23
|
+
type DoStep = {
|
|
24
|
+
type: "do";
|
|
25
|
+
instruction: string;
|
|
26
|
+
nudge?: Nudge;
|
|
27
|
+
};
|
|
28
|
+
type DontStep = {
|
|
29
|
+
type: "dont";
|
|
30
|
+
instruction: string;
|
|
31
|
+
nudge?: Nudge;
|
|
32
|
+
};
|
|
33
|
+
type ConstraintStep = {
|
|
34
|
+
type: "constraint";
|
|
35
|
+
rule: string;
|
|
36
|
+
nudge?: Nudge;
|
|
37
|
+
};
|
|
38
|
+
type ExampleStep = {
|
|
39
|
+
type: "example";
|
|
40
|
+
input: string;
|
|
41
|
+
output: string;
|
|
42
|
+
};
|
|
43
|
+
type OptionalStep = {
|
|
44
|
+
type: "optional";
|
|
45
|
+
name: string;
|
|
46
|
+
steps: PromptStep[];
|
|
47
|
+
};
|
|
48
|
+
type PromptStep = RawStep | PersonaStep | InputStep | OutputStep | ContextStep | DoStep | DontStep | ConstraintStep | ExampleStep | OptionalStep;
|
|
49
|
+
type StepType = PromptStep["type"];
|
|
50
|
+
declare function formatStepForAI(step: PromptStep): string;
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/types.d.ts
|
|
53
|
+
type PromptBuilderState = PromptStep[];
|
|
54
|
+
type PromptBuilder<Optionals extends string = never> = {
|
|
55
|
+
raw: (value: string) => PromptBuilder<Optionals>;
|
|
56
|
+
persona: (role: string) => PromptBuilder<Optionals>;
|
|
57
|
+
input: (description: string) => PromptBuilder<Optionals>;
|
|
58
|
+
output: (description: string) => PromptBuilder<Optionals>;
|
|
59
|
+
context: (information: string) => PromptBuilder<Optionals>;
|
|
60
|
+
do: (instruction: string, options?: {
|
|
61
|
+
nudge?: Nudge;
|
|
62
|
+
}) => PromptBuilder<Optionals>;
|
|
63
|
+
dont: (instruction: string, options?: {
|
|
64
|
+
nudge?: Nudge;
|
|
65
|
+
}) => PromptBuilder<Optionals>;
|
|
66
|
+
constraint: (rule: string, options?: {
|
|
67
|
+
nudge?: Nudge;
|
|
68
|
+
}) => PromptBuilder<Optionals>;
|
|
69
|
+
example: (input: string, output: string) => PromptBuilder<Optionals>;
|
|
70
|
+
use: (source: {
|
|
71
|
+
_state: PromptBuilderState;
|
|
72
|
+
}) => PromptBuilder<Optionals>;
|
|
73
|
+
optional: <Name extends string, Inner extends string = never>(name: Name, builderFn: (p: PromptBuilder) => PromptBuilder<Inner>) => PromptBuilder<Optionals | Name | Inner>;
|
|
74
|
+
};
|
|
75
|
+
interface PromptRegistry {}
|
|
76
|
+
interface PromptVariables {}
|
|
77
|
+
type PromptId = keyof PromptRegistry;
|
|
78
|
+
type ToStringOptions<Optionals extends string, Variables extends string> = [Variables] extends [never] ? [Optionals] extends [never] ? () => string : (options?: Partial<Record<Optionals, boolean>>) => string : [Optionals] extends [never] ? (options: Record<Variables, string>) => string : (options: Record<Variables, string> & Partial<Record<Optionals, boolean>>) => string;
|
|
79
|
+
type Prompt<Id extends string = string, Optionals extends string = never> = {
|
|
80
|
+
id: Id;
|
|
81
|
+
_state: PromptBuilderState;
|
|
82
|
+
toString: ToStringOptions<Optionals, Id extends keyof PromptVariables ? PromptVariables[Id] : never>;
|
|
83
|
+
};
|
|
84
|
+
type GeneratedPrompt = {
|
|
85
|
+
text: string;
|
|
86
|
+
hash: string;
|
|
87
|
+
};
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/index.d.ts
|
|
90
|
+
declare function registerPrompts(prompts: Record<string, GeneratedPrompt>): void;
|
|
91
|
+
declare function prompt<Id extends string, Optionals extends string = never>(id: Id, promptFunc: (p: PromptBuilder) => PromptBuilder<Optionals>): Prompt<Id, Optionals>;
|
|
92
|
+
//#endregion
|
|
93
|
+
export { type GeneratedPrompt, type Nudge, type Prompt, type PromptBuilder, type PromptBuilderState, type PromptId, type PromptRegistry, type PromptStep, type PromptVariables, type StepType, formatStepForAI, prompt, registerPrompts };
|
|
94
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/steps.ts","../src/types.ts","../src/index.ts"],"sourcesContent":[],"mappings":";KACY,KAAA;AAAA,KAGA,OAAA,GAHK;EAGL,IAAA,EAAA,KAAO;EACP,KAAA,EAAA,MAAA;AACZ,CAAA;AACY,KAFA,WAAA,GAEU;EACV,IAAA,EAAA,SAAW;EACX,IAAA,EAAA,MAAM;AAClB,CAAA;AACY,KALA,SAAA,GAKc;EAKd,IAAA,EAAA,OAAA;EACA,WAAA,EAAA,MAAY;AAMxB,CAAA;AACI,KAjBQ,UAAA,GAiBR;EACA,IAAA,EAAA,QAAA;EACA,WAAA,EAAA,MAAA;CACA;AACA,KApBQ,WAAA,GAoBR;EACA,IAAA,EAAA,SAAA;EACA,WAAA,EAAA,MAAA;CACA;AACA,KAvBQ,MAAA,GAuBR;EACA,IAAA,EAAA,IAAA;EAAY,WAAA,EAAA,MAAA;EAEJ,KAAA,CAAA,EA1BoD,KA0B5C;AAQpB,CAAA;KAjCY,QAAA;;;ECNA,KAAA,CAAA,EDMwD,KCNxD;AAEZ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type InputStep = {
|
|
12
|
+
type: "input";
|
|
13
|
+
description: string;
|
|
14
|
+
};
|
|
15
|
+
type OutputStep = {
|
|
16
|
+
type: "output";
|
|
17
|
+
description: string;
|
|
18
|
+
};
|
|
19
|
+
type ContextStep = {
|
|
20
|
+
type: "context";
|
|
21
|
+
information: string;
|
|
22
|
+
};
|
|
23
|
+
type DoStep = {
|
|
24
|
+
type: "do";
|
|
25
|
+
instruction: string;
|
|
26
|
+
nudge?: Nudge;
|
|
27
|
+
};
|
|
28
|
+
type DontStep = {
|
|
29
|
+
type: "dont";
|
|
30
|
+
instruction: string;
|
|
31
|
+
nudge?: Nudge;
|
|
32
|
+
};
|
|
33
|
+
type ConstraintStep = {
|
|
34
|
+
type: "constraint";
|
|
35
|
+
rule: string;
|
|
36
|
+
nudge?: Nudge;
|
|
37
|
+
};
|
|
38
|
+
type ExampleStep = {
|
|
39
|
+
type: "example";
|
|
40
|
+
input: string;
|
|
41
|
+
output: string;
|
|
42
|
+
};
|
|
43
|
+
type OptionalStep = {
|
|
44
|
+
type: "optional";
|
|
45
|
+
name: string;
|
|
46
|
+
steps: PromptStep[];
|
|
47
|
+
};
|
|
48
|
+
type PromptStep = RawStep | PersonaStep | InputStep | OutputStep | ContextStep | DoStep | DontStep | ConstraintStep | ExampleStep | OptionalStep;
|
|
49
|
+
type StepType = PromptStep["type"];
|
|
50
|
+
declare function formatStepForAI(step: PromptStep): string;
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/types.d.ts
|
|
53
|
+
type PromptBuilderState = PromptStep[];
|
|
54
|
+
type PromptBuilder<Optionals extends string = never> = {
|
|
55
|
+
raw: (value: string) => PromptBuilder<Optionals>;
|
|
56
|
+
persona: (role: string) => PromptBuilder<Optionals>;
|
|
57
|
+
input: (description: string) => PromptBuilder<Optionals>;
|
|
58
|
+
output: (description: string) => PromptBuilder<Optionals>;
|
|
59
|
+
context: (information: string) => PromptBuilder<Optionals>;
|
|
60
|
+
do: (instruction: string, options?: {
|
|
61
|
+
nudge?: Nudge;
|
|
62
|
+
}) => PromptBuilder<Optionals>;
|
|
63
|
+
dont: (instruction: string, options?: {
|
|
64
|
+
nudge?: Nudge;
|
|
65
|
+
}) => PromptBuilder<Optionals>;
|
|
66
|
+
constraint: (rule: string, options?: {
|
|
67
|
+
nudge?: Nudge;
|
|
68
|
+
}) => PromptBuilder<Optionals>;
|
|
69
|
+
example: (input: string, output: string) => PromptBuilder<Optionals>;
|
|
70
|
+
use: (source: {
|
|
71
|
+
_state: PromptBuilderState;
|
|
72
|
+
}) => PromptBuilder<Optionals>;
|
|
73
|
+
optional: <Name extends string, Inner extends string = never>(name: Name, builderFn: (p: PromptBuilder) => PromptBuilder<Inner>) => PromptBuilder<Optionals | Name | Inner>;
|
|
74
|
+
};
|
|
75
|
+
interface PromptRegistry {}
|
|
76
|
+
interface PromptVariables {}
|
|
77
|
+
type PromptId = keyof PromptRegistry;
|
|
78
|
+
type ToStringOptions<Optionals extends string, Variables extends string> = [Variables] extends [never] ? [Optionals] extends [never] ? () => string : (options?: Partial<Record<Optionals, boolean>>) => string : [Optionals] extends [never] ? (options: Record<Variables, string>) => string : (options: Record<Variables, string> & Partial<Record<Optionals, boolean>>) => string;
|
|
79
|
+
type Prompt<Id extends string = string, Optionals extends string = never> = {
|
|
80
|
+
id: Id;
|
|
81
|
+
_state: PromptBuilderState;
|
|
82
|
+
toString: ToStringOptions<Optionals, Id extends keyof PromptVariables ? PromptVariables[Id] : never>;
|
|
83
|
+
};
|
|
84
|
+
type GeneratedPrompt = {
|
|
85
|
+
text: string;
|
|
86
|
+
hash: string;
|
|
87
|
+
};
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/index.d.ts
|
|
90
|
+
declare function registerPrompts(prompts: Record<string, GeneratedPrompt>): void;
|
|
91
|
+
declare function prompt<Id extends string, Optionals extends string = never>(id: Id, promptFunc: (p: PromptBuilder) => PromptBuilder<Optionals>): Prompt<Id, Optionals>;
|
|
92
|
+
//#endregion
|
|
93
|
+
export { type GeneratedPrompt, type Nudge, type Prompt, type PromptBuilder, type PromptBuilderState, type PromptId, type PromptRegistry, type PromptStep, type PromptVariables, type StepType, formatStepForAI, prompt, registerPrompts };
|
|
94
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/steps.ts","../src/types.ts","../src/index.ts"],"sourcesContent":[],"mappings":";KACY,KAAA;AAAA,KAGA,OAAA,GAHK;EAGL,IAAA,EAAA,KAAO;EACP,KAAA,EAAA,MAAA;AACZ,CAAA;AACY,KAFA,WAAA,GAEU;EACV,IAAA,EAAA,SAAW;EACX,IAAA,EAAA,MAAM;AAClB,CAAA;AACY,KALA,SAAA,GAKc;EAKd,IAAA,EAAA,OAAA;EACA,WAAA,EAAA,MAAY;AAMxB,CAAA;AACI,KAjBQ,UAAA,GAiBR;EACA,IAAA,EAAA,QAAA;EACA,WAAA,EAAA,MAAA;CACA;AACA,KApBQ,WAAA,GAoBR;EACA,IAAA,EAAA,SAAA;EACA,WAAA,EAAA,MAAA;CACA;AACA,KAvBQ,MAAA,GAuBR;EACA,IAAA,EAAA,IAAA;EAAY,WAAA,EAAA,MAAA;EAEJ,KAAA,CAAA,EA1BoD,KA0B5C;AAQpB,CAAA;KAjCY,QAAA;;;ECNA,KAAA,CAAA,EDMwD,KCNxD;AAEZ,CAAA;AACwC,KDI5B,cAAA,GCJ4B;EAAd,IAAA,EAAA,YAAA;EACiB,IAAA,EAAA,MAAA;EAAd,KAAA,CAAA,EDMnB,KCNmB;CACmB;AAAd,KDOtB,WAAA,GCPsB;EACe,IAAA,EAAA,SAAA;EAAd,KAAA,EAAA,MAAA;EACe,MAAA,EAAA,MAAA;CAAd;AAGZ,KDGZ,YAAA,GCHY;EACH,IAAA,EAAA,UAAA;EAAd,IAAA,EAAA,MAAA;EAGiB,KAAA,EDEf,UCFe,EAAA;CACH;AAAd,KDIK,UAAA,GACR,OCLG,GDMH,WCN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B;EACmB,QAAA,EAiDpC,eAjDoC,CAkD5C,SAlD4C,EAmD5C,EAnD4C,SAAA,MAmD3B,eAnD2B,GAmDT,eAnDS,CAmDO,EAnDP,CAAA,GAAA,KAAA,CAAA;CAAd;AACe,KAsDrC,eAAA,GAtDqC;EAAd,IAAA,EAAA,MAAA;EACe,IAAA,EAAA,MAAA;CAAd;;;ADPpC,iBEcS,eAAA,CFdU,OAAA,EEce,MFdf,CAAA,MAAA,EEc8B,eFd9B,CAAA,CAAA,EAAA,IAAA;AACnB,iBEqCS,MFrCc,CAAA,WAAA,MAAA,EAAA,kBAAA,MAAA,GAAA,KAAA,CAAA,CAAA,EAAA,EEsCjB,EFtCiB,EAAA,UAAA,EAAA,CAAA,CAAA,EEuCL,aFvCK,EAAA,GEuCa,aFvCb,CEuC2B,SFvC3B,CAAA,CAAA,EEwCpB,MFxCoB,CEwCb,EFxCa,EEwCT,SFxCS,CAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
//#region src/builder.ts
|
|
2
|
+
function createBuilder(targetState) {
|
|
3
|
+
const state = targetState ?? [];
|
|
4
|
+
const builder = {
|
|
5
|
+
raw: (value) => (state.push({
|
|
6
|
+
type: "raw",
|
|
7
|
+
value
|
|
8
|
+
}), builder),
|
|
9
|
+
persona: (role) => (state.push({
|
|
10
|
+
type: "persona",
|
|
11
|
+
role
|
|
12
|
+
}), builder),
|
|
13
|
+
input: (description) => (state.push({
|
|
14
|
+
type: "input",
|
|
15
|
+
description
|
|
16
|
+
}), builder),
|
|
17
|
+
output: (description) => (state.push({
|
|
18
|
+
type: "output",
|
|
19
|
+
description
|
|
20
|
+
}), builder),
|
|
21
|
+
context: (information) => (state.push({
|
|
22
|
+
type: "context",
|
|
23
|
+
information
|
|
24
|
+
}), builder),
|
|
25
|
+
do: (instruction, options) => (state.push({
|
|
26
|
+
type: "do",
|
|
27
|
+
instruction,
|
|
28
|
+
nudge: options?.nudge
|
|
29
|
+
}), builder),
|
|
30
|
+
dont: (instruction, options) => (state.push({
|
|
31
|
+
type: "dont",
|
|
32
|
+
instruction,
|
|
33
|
+
nudge: options?.nudge
|
|
34
|
+
}), builder),
|
|
35
|
+
constraint: (rule, options) => (state.push({
|
|
36
|
+
type: "constraint",
|
|
37
|
+
rule,
|
|
38
|
+
nudge: options?.nudge
|
|
39
|
+
}), builder),
|
|
40
|
+
example: (input, output) => (state.push({
|
|
41
|
+
type: "example",
|
|
42
|
+
input,
|
|
43
|
+
output
|
|
44
|
+
}), builder),
|
|
45
|
+
use: (source) => (state.push(...source._state), builder),
|
|
46
|
+
optional: (name, builderFn) => {
|
|
47
|
+
const optionalSteps = [];
|
|
48
|
+
const { builder: innerBuilder } = createBuilder(optionalSteps);
|
|
49
|
+
builderFn(innerBuilder);
|
|
50
|
+
state.push({
|
|
51
|
+
type: "optional",
|
|
52
|
+
name,
|
|
53
|
+
steps: optionalSteps
|
|
54
|
+
});
|
|
55
|
+
return builder;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
builder,
|
|
60
|
+
state
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/steps.ts
|
|
66
|
+
function formatNudge(nudge) {
|
|
67
|
+
if (!nudge || nudge === 3) return "";
|
|
68
|
+
return `\nNudge: ${nudge}`;
|
|
69
|
+
}
|
|
70
|
+
function formatStepForAI(step) {
|
|
71
|
+
switch (step.type) {
|
|
72
|
+
case "raw": return `[Raw Text] (Include this text verbatim in the system prompt.)\nValue: "${step.value}"`;
|
|
73
|
+
case "persona": return `[Persona] (Define the identity and role the AI should assume. Frame this as 'You are...' at the start of the system prompt.)\nValue: "${step.role}"`;
|
|
74
|
+
case "input": return `[Input] (Describe what input the AI will receive from the user. Help the AI understand the context of what it will be working with.)\nValue: "${step.description}"`;
|
|
75
|
+
case "output": return `[Output] (Specify what the AI should produce as output. Be clear about the expected format and content.)\nValue: "${step.description}"`;
|
|
76
|
+
case "context": return `[Context] (Background information or context that helps the AI understand the situation. This is not an instruction, just helpful information.)\nValue: "${step.information}"`;
|
|
77
|
+
case "do": return `[Do] (A positive instruction the AI must follow.)\nValue: "${step.instruction}"${formatNudge(step.nudge)}`;
|
|
78
|
+
case "dont": return `[Don't] (A negative instruction - something the AI must avoid.)\nValue: "${step.instruction}"${formatNudge(step.nudge)}`;
|
|
79
|
+
case "constraint": return `[Constraint] (A rule or limitation the AI must respect.)\nValue: "${step.rule}"${formatNudge(step.nudge)}`;
|
|
80
|
+
case "example": return `[Example] (An input/output example showing the AI how to respond. Use these to demonstrate the expected behavior.)\nInput: "${step.input}"\nExpected output: "${step.output}"`;
|
|
81
|
+
case "optional": {
|
|
82
|
+
const innerSteps = step.steps.map(formatStepForAI).join("\n\n");
|
|
83
|
+
return `[Optional Block Start: "${step.name}"] (The following instructions are OPTIONAL. Wrap the generated content for these in {{#${step.name}}}...{{/${step.name}}} markers so it can be toggled at runtime.)\n\n${innerSteps}\n\n[Optional Block End: "${step.name}"]`;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/index.ts
|
|
90
|
+
let promptCache = {};
|
|
91
|
+
function registerPrompts(prompts) {
|
|
92
|
+
promptCache = {
|
|
93
|
+
...promptCache,
|
|
94
|
+
...prompts
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function processTemplate(text, options = {}) {
|
|
98
|
+
const processOptionals = (str) => str.replace(/\{\{#(\w+)\}\}([\s\S]*?)\{\{\/\1\}\}/g, (_, name, content) => options[name] ? processOptionals(content) : "");
|
|
99
|
+
const processVars = (str) => str.replace(/\{\{(?![#\/])(\w+)\}\}/g, (match, name) => {
|
|
100
|
+
const value = options[name];
|
|
101
|
+
return typeof value === "string" ? value : match;
|
|
102
|
+
});
|
|
103
|
+
return processVars(processOptionals(text)).replace(/\n{3,}/g, "\n\n");
|
|
104
|
+
}
|
|
105
|
+
function prompt(id, promptFunc) {
|
|
106
|
+
const { builder, state } = createBuilder();
|
|
107
|
+
promptFunc(builder);
|
|
108
|
+
return {
|
|
109
|
+
id,
|
|
110
|
+
_state: state,
|
|
111
|
+
toString: ((options) => {
|
|
112
|
+
return processTemplate(promptCache[id]?.text ?? "", options).trim();
|
|
113
|
+
})
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
export { formatStepForAI, prompt, registerPrompts };
|
|
119
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/builder.ts","../src/steps.ts","../src/index.ts"],"sourcesContent":["import type { Nudge } from \"./steps.js\";\nimport type { PromptBuilder, PromptBuilderState } from \"./types.js\";\n\nexport function createBuilder(targetState?: PromptBuilderState): {\n builder: PromptBuilder;\n state: PromptBuilderState;\n} {\n const state: PromptBuilderState = targetState ?? [];\n\n const builder: PromptBuilder = {\n raw: (value) => (state.push({ type: \"raw\", value }), builder),\n persona: (role) => (state.push({ type: \"persona\", role }), builder),\n input: (description) => (\n state.push({ type: \"input\", description }),\n builder\n ),\n output: (description) => (\n state.push({ type: \"output\", description }),\n builder\n ),\n context: (information) => (\n state.push({ type: \"context\", information }),\n builder\n ),\n do: (instruction, options?: { nudge?: Nudge }) => (\n state.push({ type: \"do\", instruction, nudge: options?.nudge }),\n builder\n ),\n dont: (instruction, options?: { nudge?: Nudge }) => (\n state.push({ type: \"dont\", instruction, nudge: options?.nudge }),\n builder\n ),\n constraint: (rule, options?: { nudge?: Nudge }) => (\n state.push({ type: \"constraint\", rule, nudge: options?.nudge }),\n builder\n ),\n example: (input, output) => (\n state.push({ type: \"example\", input, output }),\n builder\n ),\n use: (source) => (state.push(...source._state), builder),\n optional: (name, builderFn) => {\n const optionalSteps: PromptBuilderState = [];\n const { builder: innerBuilder } = createBuilder(optionalSteps);\n builderFn(innerBuilder);\n state.push({ type: \"optional\", name, steps: optionalSteps });\n return builder;\n },\n };\n\n return { builder, state };\n}\n","// Nudge levels for instructions (1 = gentlest, 5 = strongest)\nexport type Nudge = 1 | 2 | 3 | 4 | 5;\n\n// Step types\nexport type RawStep = { type: \"raw\"; value: string };\nexport type PersonaStep = { type: \"persona\"; role: string };\nexport type InputStep = { type: \"input\"; description: string };\nexport type OutputStep = { type: \"output\"; description: string };\nexport type ContextStep = { type: \"context\"; information: string };\nexport type DoStep = { type: \"do\"; instruction: string; nudge?: Nudge };\nexport type DontStep = { type: \"dont\"; instruction: string; nudge?: Nudge };\nexport type ConstraintStep = {\n type: \"constraint\";\n rule: string;\n nudge?: Nudge;\n};\nexport type ExampleStep = { type: \"example\"; input: string; output: string };\nexport type OptionalStep = {\n type: \"optional\";\n name: string;\n steps: PromptStep[];\n};\n\nexport type PromptStep =\n | RawStep\n | PersonaStep\n | InputStep\n | OutputStep\n | ContextStep\n | DoStep\n | DontStep\n | ConstraintStep\n | ExampleStep\n | OptionalStep;\n\nexport type StepType = PromptStep[\"type\"];\n\nfunction formatNudge(nudge?: Nudge): string {\n if (!nudge || nudge === 3) return \"\";\n return `\\nNudge: ${nudge}`;\n}\n\n// Format step for AI consumption - each case returns the full formatted output\nexport function formatStepForAI(step: PromptStep): string {\n switch (step.type) {\n case \"raw\":\n return `[Raw Text] (Include this text verbatim in the system prompt.)\\nValue: \"${step.value}\"`;\n case \"persona\":\n return `[Persona] (Define the identity and role the AI should assume. Frame this as 'You are...' at the start of the system prompt.)\\nValue: \"${step.role}\"`;\n case \"input\":\n return `[Input] (Describe what input the AI will receive from the user. Help the AI understand the context of what it will be working with.)\\nValue: \"${step.description}\"`;\n case \"output\":\n return `[Output] (Specify what the AI should produce as output. Be clear about the expected format and content.)\\nValue: \"${step.description}\"`;\n case \"context\":\n return `[Context] (Background information or context that helps the AI understand the situation. This is not an instruction, just helpful information.)\\nValue: \"${step.information}\"`;\n case \"do\":\n return `[Do] (A positive instruction the AI must follow.)\\nValue: \"${step.instruction}\"${formatNudge(step.nudge)}`;\n case \"dont\":\n return `[Don't] (A negative instruction - something the AI must avoid.)\\nValue: \"${step.instruction}\"${formatNudge(step.nudge)}`;\n case \"constraint\":\n return `[Constraint] (A rule or limitation the AI must respect.)\\nValue: \"${step.rule}\"${formatNudge(step.nudge)}`;\n case \"example\":\n return `[Example] (An input/output example showing the AI how to respond. Use these to demonstrate the expected behavior.)\\nInput: \"${step.input}\"\\nExpected output: \"${step.output}\"`;\n case \"optional\": {\n const innerSteps = step.steps.map(formatStepForAI).join(\"\\n\\n\");\n return `[Optional Block Start: \"${step.name}\"] (The following instructions are OPTIONAL. Wrap the generated content for these in {{#${step.name}}}...{{/${step.name}}} markers so it can be toggled at runtime.)\\n\\n${innerSteps}\\n\\n[Optional Block End: \"${step.name}\"]`;\n }\n }\n}\n","import { createBuilder } from \"./builder.js\";\nimport { formatStepForAI } from \"./steps.js\";\nimport type {\n GeneratedPrompt,\n Nudge,\n Prompt,\n PromptBuilder,\n PromptBuilderState,\n PromptId,\n PromptRegistry,\n PromptStep,\n PromptVariables,\n StepType,\n} from \"./types.js\";\n\n// Cache for generated prompts, populated by registerPrompts()\nlet promptCache: Record<string, GeneratedPrompt> = {};\n\nfunction registerPrompts(prompts: Record<string, GeneratedPrompt>): void {\n promptCache = { ...promptCache, ...prompts };\n}\n\nfunction processTemplate(\n text: string,\n options: Record<string, string | boolean> = {},\n): string {\n // Recursively process optional blocks (handles nesting)\n const processOptionals = (str: string): string =>\n str.replace(/\\{\\{#(\\w+)\\}\\}([\\s\\S]*?)\\{\\{\\/\\1\\}\\}/g, (_, name, content) =>\n options[name] ? processOptionals(content) : \"\",\n );\n\n // Replace variables {{name}} (not #name or /name)\n const processVars = (str: string): string =>\n str.replace(/\\{\\{(?![#\\/])(\\w+)\\}\\}/g, (match, name) => {\n const value = options[name];\n return typeof value === \"string\" ? value : match;\n });\n\n return processVars(processOptionals(text)).replace(/\\n{3,}/g, \"\\n\\n\");\n}\n\nfunction prompt<Id extends string, Optionals extends string = never>(\n id: Id,\n promptFunc: (p: PromptBuilder) => PromptBuilder<Optionals>,\n): Prompt<Id, Optionals> {\n const { builder, state } = createBuilder();\n promptFunc(builder);\n\n return {\n id,\n _state: state,\n toString: ((options?: Record<string, string | boolean>): string => {\n const text = promptCache[id]?.text ?? \"\";\n return processTemplate(text, options).trim();\n }) as Prompt<Id, Optionals>[\"toString\"],\n };\n}\n\nexport {\n formatStepForAI,\n prompt,\n registerPrompts,\n type GeneratedPrompt,\n type Nudge,\n type Prompt,\n type PromptBuilder,\n type PromptBuilderState,\n type PromptId,\n type PromptRegistry,\n type PromptStep,\n type PromptVariables,\n type StepType,\n};\n"],"mappings":";AAGA,SAAgB,cAAc,aAG5B;CACA,MAAM,QAA4B,eAAe,EAAE;CAEnD,MAAM,UAAyB;EAC7B,MAAM,WAAW,MAAM,KAAK;GAAE,MAAM;GAAO;GAAO,CAAC,EAAE;EACrD,UAAU,UAAU,MAAM,KAAK;GAAE,MAAM;GAAW;GAAM,CAAC,EAAE;EAC3D,QAAQ,iBACN,MAAM,KAAK;GAAE,MAAM;GAAS;GAAa,CAAC,EAC1C;EAEF,SAAS,iBACP,MAAM,KAAK;GAAE,MAAM;GAAU;GAAa,CAAC,EAC3C;EAEF,UAAU,iBACR,MAAM,KAAK;GAAE,MAAM;GAAW;GAAa,CAAC,EAC5C;EAEF,KAAK,aAAa,aAChB,MAAM,KAAK;GAAE,MAAM;GAAM;GAAa,OAAO,SAAS;GAAO,CAAC,EAC9D;EAEF,OAAO,aAAa,aAClB,MAAM,KAAK;GAAE,MAAM;GAAQ;GAAa,OAAO,SAAS;GAAO,CAAC,EAChE;EAEF,aAAa,MAAM,aACjB,MAAM,KAAK;GAAE,MAAM;GAAc;GAAM,OAAO,SAAS;GAAO,CAAC,EAC/D;EAEF,UAAU,OAAO,YACf,MAAM,KAAK;GAAE,MAAM;GAAW;GAAO;GAAQ,CAAC,EAC9C;EAEF,MAAM,YA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,EACtC;CAER,MAAM,oBAAoB,QACxB,IAAI,QAAQ,0CAA0C,GAAG,MAAM,YAC7D,QAAQ,QAAQ,iBAAiB,QAAQ,GAAG,GAC7C;CAGH,MAAM,eAAe,QACnB,IAAI,QAAQ,4BAA4B,OAAO,SAAS;EACtD,MAAM,QAAQ,QAAQ;AACtB,SAAO,OAAO,UAAU,WAAW,QAAQ;GAC3C;AAEJ,QAAO,YAAY,iBAAiB,KAAK,CAAC,CAAC,QAAQ,WAAW,OAAO;;AAGvE,SAAS,OACP,IACA,YACuB;CACvB,MAAM,EAAE,SAAS,UAAU,eAAe;AAC1C,YAAW,QAAQ;AAEnB,QAAO;EACL;EACA,QAAQ;EACR,YAAY,YAAuD;AAEjE,UAAO,gBADM,YAAY,KAAK,QAAQ,IACT,QAAQ,CAAC,MAAM;;EAE/C"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nudge-ai/core",
|
|
3
|
+
"version": "0.0.1-beta.0",
|
|
4
|
+
"description": "Core library for Nudge - type-safe prompt builder for AI applications",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/nudge-libs/nudge.git",
|
|
10
|
+
"directory": "packages/core"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"ai",
|
|
14
|
+
"llm",
|
|
15
|
+
"prompt",
|
|
16
|
+
"prompt-engineering",
|
|
17
|
+
"typescript"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsdown",
|
|
21
|
+
"dev": "tsdown --watch",
|
|
22
|
+
"clean": "rm -rf dist",
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsdown": "^0.19.0",
|
|
27
|
+
"typescript": "^5.7.0"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"main": "./dist/index.cjs",
|
|
34
|
+
"module": "./dist/index.mjs",
|
|
35
|
+
"types": "./dist/index.d.cts",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"require": "./dist/index.cjs",
|
|
39
|
+
"import": "./dist/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
}
|
|
43
|
+
}
|