Swap out eta for nunjucks

This commit is contained in:
Alex Gleason committed 2025-11-13 19:49:15 -06:00
1 parent d1cbcda5d5
commit 58c24f615d
4 files changed
+104 -60

No files matched your search

+55 -13
View File
@@ -58,7 +58,6 @@
"decimal.js": "^10.6.0",
"embla-carousel-react": "^8.3.0",
"esbuild-wasm": "^0.25.8",
"eta": "^4.4.1",
"gray-matter": "^4.0.3",
"i18next": "^25.5.2",
"ignore": "^7.0.5",
@@ -69,6 +68,7 @@
"lucide-react": "^0.525.0",
"mime": "^4.1.0",
"nostr-tools": "^2.15.2",
"nunjucks": "^3.2.4",
"openai": "^5.15.0",
"path-browserify": "^1.0.1",
"prismjs": "^1.30.0",
@@ -106,6 +106,7 @@
"@testing-library/user-event": "^14.6.1",
"@types/jszip": "^3.4.0",
"@types/node": "^22.5.5",
"@types/nunjucks": "^3.2.6",
"@types/path-browserify": "^1.0.3",
"@types/prismjs": "^1.26.5",
"@types/qrcode": "^1.5.5",
@@ -5604,6 +5605,13 @@
"undici-types": "~6.21.0"
}
},
"node_modules/@types/nunjucks": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/@types/nunjucks/-/nunjucks-3.2.6.tgz",
"integrity": "sha512-pHiGtf83na1nCzliuAdq8GowYiXvH5l931xZ0YEHaLMNFgynpEqx+IPStlu7UaDkehfvl01e4x/9Tpwhy7Ue3w==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/path-browserify": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@types/path-browserify/-/path-browserify-1.0.3.tgz",
@@ -6089,6 +6097,12 @@
"url": "https://opencollective.com/vitest"
}
},
"node_modules/a-sync-waterfall": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/a-sync-waterfall/-/a-sync-waterfall-1.0.1.tgz",
"integrity": "sha512-RYTOHHdWipFUliRFMCS4X2Yn2X8M87V/OpSqWzKKOGhzqyUxzyVmhHDH9sAvG+ZuQf/TAOFsLCpMw09I1ufUnA==",
"license": "MIT"
},
"node_modules/acorn": {
"version": "8.15.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz",
@@ -6258,6 +6272,12 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/asap": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
"integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==",
"license": "MIT"
},
"node_modules/assertion-error": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz",
@@ -8121,18 +8141,6 @@
"node": ">=0.10.0"
}
},
"node_modules/eta": {
"version": "4.4.1",
"resolved": "https://registry.npmjs.org/eta/-/eta-4.4.1.tgz",
"integrity": "sha512-4o6fYxhRmFmO9SJcU9PxBLYPGapvJ/Qha0ZE+Y6UE9QIUd0Wk1qaLISQ6J1bM7nOcWHhs1YmY3mfrfwkJRBTWQ==",
"license": "MIT",
"engines": {
"node": ">=20"
},
"funding": {
"url": "https://github.com/bgub/eta?sponsor=1"
}
},
"node_modules/eventemitter3": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
@@ -11698,6 +11706,40 @@
"integrity": "sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==",
"license": "MIT"
},
"node_modules/nunjucks": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/nunjucks/-/nunjucks-3.2.4.tgz",
"integrity": "sha512-26XRV6BhkgK0VOxfbU5cQI+ICFUtMLixv1noZn1tGU38kQH5A5nmmbk/O45xdyBhD1esk47nKrY0mvQpZIhRjQ==",
"license": "BSD-2-Clause",
"dependencies": {
"a-sync-waterfall": "^1.0.0",
"asap": "^2.0.3",
"commander": "^5.1.0"
},
"bin": {
"nunjucks-precompile": "bin/precompile"
},
"engines": {
"node": ">= 6.9.0"
},
"peerDependencies": {
"chokidar": "^3.3.0"
},
"peerDependenciesMeta": {
"chokidar": {
"optional": true
}
}
},
"node_modules/nunjucks/node_modules/commander": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz",
"integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==",
"license": "MIT",
"engines": {
"node": ">= 6"
}
},
"node_modules/nwsapi": {
"version": "2.2.20",
"resolved": "https://registry.npmjs.org/nwsapi/-/nwsapi-2.2.20.tgz",
+2 -1
View File
@@ -71,7 +71,6 @@
"decimal.js": "^10.6.0",
"embla-carousel-react": "^8.3.0",
"esbuild-wasm": "^0.25.8",
"eta": "^4.4.1",
"gray-matter": "^4.0.3",
"i18next": "^25.5.2",
"ignore": "^7.0.5",
@@ -82,6 +81,7 @@
"lucide-react": "^0.525.0",
"mime": "^4.1.0",
"nostr-tools": "^2.15.2",
"nunjucks": "^3.2.4",
"openai": "^5.15.0",
"path-browserify": "^1.0.1",
"prismjs": "^1.30.0",
@@ -119,6 +119,7 @@
"@testing-library/user-event": "^14.6.1",
"@types/jszip": "^3.4.0",
"@types/node": "^22.5.5",
"@types/nunjucks": "^3.2.6",
"@types/path-browserify": "^1.0.3",
"@types/prismjs": "^1.26.5",
"@types/qrcode": "^1.5.5",
+6 -6
View File
@@ -470,7 +470,7 @@ describe('makeSystemPrompt', () => {
describe('custom template support', () => {
it('should render a custom template when provided', async () => {
const customTemplate = `Hello Shakespeare! Current directory: <%= ctx.cwd %>`;
const customTemplate = `Hello Shakespeare! Current directory: {{ cwd }}`;
const result = await makeSystemPrompt({
...baseOpts,
@@ -483,7 +483,7 @@ describe('makeSystemPrompt', () => {
});
it('should support conditional logic in custom templates', async () => {
const customTemplate = `<% if (ctx.mode === 'init') { %>Initializing...<% } else { %>Working on project<% } %>`;
const customTemplate = `{% if mode === 'init' %}Initializing...{% else %}Working on project{% endif %}`;
const result = await makeSystemPrompt({
...baseOpts,
@@ -502,10 +502,10 @@ describe('makeSystemPrompt', () => {
{} as NUser['signer']
);
const customTemplate = `User: <%= ctx.user?.pubkey || 'none' %>
CWD: <%= ctx.cwd %>
Origin: <%= ctx.originUrl %>
Skills: <%= ctx.skills.length %>`;
const customTemplate = `User: {{ user.pubkey if user else 'none' }}
CWD: {{ cwd }}
Origin: {{ location.origin }}
Skills: {{ skills.length }}`;
const result = await makeSystemPrompt({
...baseOpts,
+41 -40
View File
@@ -3,7 +3,7 @@ import { NostrMetadata } from "@nostrify/nostrify";
import { join } from "path-browserify";
import OpenAI from "openai";
import { nip19 } from "nostr-tools";
import { Eta } from "eta";
import nunjucks from "nunjucks";
import { JSRuntimeFS } from "./JSRuntime";
import { getAllSkills } from "./plugins";
import type { AppConfig } from "@/contexts/AppContext";
@@ -25,16 +25,16 @@ export interface MakeSystemPromptOpts {
* Default system prompt template.
* This can be customized by users in Settings > System > System Prompt.
*/
export const defaultSystemPrompt = `<% if (ctx.mode === "init") { %>You are Shakespeare, an expert software extraordinaire. The files in the current directory are a template. Your goal is to transform this template into a working project according to the user's request.<% } else { %>You are Shakespeare, an expert software extraordinaire. Your goal is to work on the project in the current directory according to the user's request. First, explore and understand the project structure, examine the existing files, and understand the context before making any assumptions about what the user is asking for.<% } %>
export const defaultSystemPrompt = `{% if mode === "init" %}You are Shakespeare, an expert software extraordinaire. The files in the current directory are a template. Your goal is to transform this template into a working project according to the user's request.{% else %}You are Shakespeare, an expert software extraordinaire. Your goal is to work on the project in the current directory according to the user's request. First, explore and understand the project structure, examine the existing files, and understand the context before making any assumptions about what the user is asking for.{% endif %}
# Your Environment
You are operating within **Shakespeare**, an AI-powered Nostr website builder that allows users to create custom Nostr applications through natural language conversation.
- **Current Date**: <%= ctx.date %>
- **Current Page**: <%= ctx.location.href %>
- **Current Working Directory**: <%= ctx.cwd %>
- **Repository URL**: <%= ctx.repositoryUrl %>
- **Current Date**: {{ date }}
- **Current Page**: {{ location.href }}
- **Current Working Directory**: {{ cwd }}
- **Repository URL**: {{ repositoryUrl }}
## What Shakespeare Is
@@ -52,19 +52,19 @@ Shakespeare is a web-based development environment where users can build Nostr w
## The User
<% if (ctx.user) { %>The user is logged into Nostr with the following profile:
{% if user %}The user is logged into Nostr with the following profile:
- **Nostr pubkey (hex)**: <%= ctx.user.pubkey %>
- **Nostr npub**: <%= ctx.user.npub %><% if (ctx.user.name) { %>
- **Name**: <%= ctx.user.name %><% } %><% if (ctx.user.about) { %>
- **About**: <%= ctx.user.about %><% } %><% if (ctx.user.website) { %>
- **Website**: <%= ctx.user.website %><% } %><% if (ctx.user.picture) { %>
- **Avatar**: <%= ctx.user.picture %><% } %><% if (ctx.user.banner) { %>
- **Banner**: <%= ctx.user.banner %><% } %><% if (ctx.user.nip05) { %>
- **NIP-05**: <%= ctx.user.nip05 %><% } %><% if (ctx.user.lud16) { %>
- **Lightning Address**: <%= ctx.user.lud16 %><% } %>
- **Nostr pubkey (hex)**: {{ user.pubkey }}
- **Nostr npub**: {{ user.npub }}{% if user.name %}
- **Name**: {{ user.name }}{% endif %}{% if user.about %}
- **About**: {{ user.about }}{% endif %}{% if user.website %}
- **Website**: {{ user.website }}{% endif %}{% if user.picture %}
- **Avatar**: {{ user.picture }}{% endif %}{% if user.banner %}
- **Banner**: {{ user.banner }}{% endif %}{% if user.nip05 %}
- **NIP-05**: {{ user.nip05 }}{% endif %}{% if user.lud16 %}
- **Lightning Address**: {{ user.lud16 }}{% endif %}
Since the user is logged in, they can deploy their creations to public URLs and use Nostr-enabled AI providers.<% } else { %>The user is not logged in. The user can log into Nostr by clicking the "Login" button in the sidebar menu. Logging in will allow the user to deploy their creations to public URLs and use Nostr-enabled AI providers.<% } %>
Since the user is logged in, they can deploy their creations to public URLs and use Nostr-enabled AI providers.{% else %}The user is not logged in. The user can log into Nostr by clicking the "Login" button in the sidebar menu. Logging in will allow the user to deploy their creations to public URLs and use Nostr-enabled AI providers.{% endif %}
## User Interface
@@ -149,62 +149,62 @@ As the AI assistant in Shakespeare, you help users by:
The user expects you to handle all technical implementation while they focus on describing their vision and requirements. You can leverage the knowledge from other projects in the VFS to build better, more sophisticated applications.
**Always commit your code changes** after completing work on a feature, fix, or meaningful set of changes.<% if (ctx.tools.length > 0) { %>
**Always commit your code changes** after completing work on a feature, fix, or meaningful set of changes.{% if tools.length > 0 %}
## Available Tools
You have access to the following tools:
<% ctx.tools.forEach(function(tool) { if (tool.type === 'function') { %>
- **<%= tool.function.name %>**: <%= tool.function.description || 'No description available' %><% } }); %><% } %>
{% for tool in tools %}{% if tool.type === 'function' %}
- **{{ tool.function.name }}**: {{ tool.function.description or 'No description available' }}{% endif %}{% endfor %}{% endif %}
## Skills
<% if (ctx.skills && ctx.skills.length > 0) { %>You have access to the following skills. **Skills MUST be used whenever applicable** by calling the \`skill\` tool with the skill name.
{% if skills and skills.length > 0 %}You have access to the following skills. **Skills MUST be used whenever applicable** by calling the \`skill\` tool with the skill name.
Available skills:
<% ctx.skills.forEach(function(skill) { %>
- **<%= skill.name %>**: <%= skill.description %>
- Plugin: <%= skill.plugin %>
- Path: <%= skill.path %>
<% }); %>
{% for skill in skills %}
- **{{ skill.name }}**: {{ skill.description }}
- Plugin: {{ skill.plugin }}
- Path: {{ skill.path }}
{% endfor %}
**Important**: When a task matches a skill's description, you MUST use that skill by calling the skill tool. Skills contain specialized workflows and best practices for specific tasks.<% } else { %>No skills are currently configured. Skills are reusable AI workflows that can be added via plugins.<% } %>
**Important**: When a task matches a skill's description, you MUST use that skill by calling the skill tool. Skills contain specialized workflows and best practices for specific tasks.{% else %}No skills are currently configured. Skills are reusable AI workflows that can be added via plugins.{% endif %}
Users can configure skills in Settings > AI (<%= ctx.location.origin %>/settings/ai) by adding plugins that contain skills.<% if (ctx.config.corsProxy) { %>
Users can configure skills in Settings > AI ({{ location.origin }}/settings/ai) by adding plugins that contain skills.{% if config.corsProxy %}
## Working Around CORS Issues
If you encounter CORS (Cross-Origin Resource Sharing) errors when fetching external APIs, use the configured CORS proxy:
**CORS Proxy URL Template**: \`<%= ctx.config.corsProxy %>\`
**CORS Proxy URL Template**: \`{{ config.corsProxy }}\`
Replace \`{href}\`, \`{hostname}\`, or other URL components in the template as needed.<% } %>
Replace \`{href}\`, \`{hostname}\`, or other URL components in the template as needed.{% endif %}
## Edit with Shakespeare
The "Edit with Shakespeare" button is a way that people can start editing any Shakespeare-compatible site with a single click. It can be added anywhere markdown or HTML is supported, including in READMEs, or within an app itself.<% if (ctx.repositoryUrl) { %>
The "Edit with Shakespeare" button is a way that people can start editing any Shakespeare-compatible site with a single click. It can be added anywhere markdown or HTML is supported, including in READMEs, or within an app itself.{% if repositoryUrl %}
This project has a repository URL configured, so you can create an "Edit with Shakespeare" button using the following code snippets:
**Markdown:**
\`\`\`markdown
[![Edit with Shakespeare](<%= ctx.badgeUrl %>)](<%= ctx.editUrl %>)
[![Edit with Shakespeare]({{ badgeUrl }})]({{ editUrl }})
\`\`\`
**HTML/JSX/TSX:**
\`\`\`jsx
<a href="<%= ctx.editUrl %>" target="_blank">
<img src="<%= ctx.badgeUrl %>" alt="Edit with Shakespeare" />
<a href="{{ editUrl }}" target="_blank">
<img src="{{ badgeUrl }}" alt="Edit with Shakespeare" />
</a>
\`\`\`
Note: the badge should be displayed at its natural size. It is recommended to omit width/height attributes to ensure proper scaling, or use \`height: auto\` in CSS (or \`"h-auto"\` in Tailwind CSS) when applicable.<% } else { %>
Note: the badge should be displayed at its natural size. It is recommended to omit width/height attributes to ensure proper scaling, or use \`height: auto\` in CSS (or \`"h-auto"\` in Tailwind CSS) when applicable.{% else %}
**Important**: This project does not currently have a repository URL configured. If the user asks about adding an "Edit with Shakespeare" button, inform them that they must first initialize a public Git repository from their Shakespeare project. Once a repository URL is available, an "Edit with Shakespeare" button can be created.<% } %><% if (ctx.readmeText) { %>
**Important**: This project does not currently have a repository URL configured. If the user asks about adding an "Edit with Shakespeare" button, inform them that they must first initialize a public Git repository from their Shakespeare project. Once a repository URL is available, an "Edit with Shakespeare" button can be created.{% endif %}{% if readmeText %}
<%= ctx.readmeText %><% } %><% if (ctx.agentText) { %>
{{ readmeText }}{% endif %}{% if agentsText %}
<%= ctx.agentsText %><% } %>`;
{{ agentsText }}{% endif %}`;
export async function makeSystemPrompt(opts: MakeSystemPromptOpts): Promise<string> {
const { tools, mode, fs, cwd, config, defaultConfig, user, metadata, repositoryUrl, template } = opts;
@@ -295,8 +295,9 @@ export async function makeSystemPrompt(opts: MakeSystemPromptOpts): Promise<stri
const templateToRender = template || defaultSystemPrompt;
try {
const eta = new Eta({ varName: 'ctx', autoTrim: false });
return eta.renderString(templateToRender, context);
// Configure nunjucks environment
const env = new nunjucks.Environment(null, { autoescape: false });
return env.renderString(templateToRender, context);
} catch (error) {
console.error("Error rendering system prompt template:", error);
// Return the template as-is if rendering fails