diff --git a/src/components/SessionManagerProvider.tsx b/src/components/SessionManagerProvider.tsx index 38ade70f..be7bb5a3 100644 --- a/src/components/SessionManagerProvider.tsx +++ b/src/components/SessionManagerProvider.tsx @@ -7,6 +7,7 @@ import { useAISettings } from '@/hooks/useAISettings'; import { useProviderModels } from '@/hooks/useProviderModels'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAppContext } from '@/hooks/useAppContext'; +import { useNostr } from '@nostrify/react'; interface SessionManagerProviderProps { children: ReactNode; @@ -22,12 +23,13 @@ export function SessionManagerProvider({ children }: SessionManagerProviderProps const { models } = useProviderModels(); const { user, metadata } = useCurrentUser(); const { config } = useAppContext(); + const { nostr } = useNostr(); const sessionManager = useMemo(() => { const getProviderModels = () => models; const getCurrentUser = () => ({ user, metadata }); - return new SessionManager(fs, settings, getProviderModels, getCurrentUser, config.corsProxy, projectsPath, pluginsPath); - }, [fs, settings, models, user, metadata, config.corsProxy, projectsPath, pluginsPath]); + return new SessionManager(fs, nostr, settings, getProviderModels, getCurrentUser, config.corsProxy, projectsPath, pluginsPath); + }, [fs, nostr, settings, models, user, metadata, config.corsProxy, projectsPath, pluginsPath]); // Cleanup on unmount useEffect(() => { diff --git a/src/lib/SessionManager.test.ts b/src/lib/SessionManager.test.ts index 911b2a63..ceb92149 100644 --- a/src/lib/SessionManager.test.ts +++ b/src/lib/SessionManager.test.ts @@ -1,6 +1,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; import { SessionManager, type AIMessage } from './SessionManager'; import type { JSRuntimeFS } from './JSRuntime'; +import type { NPool } from '@nostrify/nostrify'; // Mock the filesystem const mockFS: JSRuntimeFS = { @@ -22,6 +23,13 @@ const mockFS: JSRuntimeFS = { realpath: vi.fn(), } as unknown as JSRuntimeFS; +// Mock NPool +const mockNostr: NPool = { + query: vi.fn(), + req: vi.fn(), + event: vi.fn(), +} as unknown as NPool; + describe('SessionManager', () => { let sessionManager: SessionManager; const mockAISettings = { @@ -35,7 +43,7 @@ describe('SessionManager', () => { }; beforeEach(() => { - sessionManager = new SessionManager(mockFS, mockAISettings); + sessionManager = new SessionManager(mockFS, mockNostr, mockAISettings); }); it('should initialize streaming message with reasoning content', async () => { diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 6ae2c43e..e3263705 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -11,6 +11,8 @@ import { NostrMetadata } from '@nostrify/nostrify'; import { MalformedToolCallError } from './errors/MalformedToolCallError'; import { EmptyMessageError } from './errors/EmptyMessageError'; import { isEmptyMessage } from './isEmptyMessage'; +import { Git } from './git'; +import type { NPool } from '@nostrify/nostrify'; export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam | { role: 'assistant'; @@ -58,6 +60,7 @@ export class SessionManager { private sessions = new Map(); private listeners: Partial void>>> = {}; private fs: JSRuntimeFS; + private git: Git; private aiSettings: { providers: AIProvider[] }; private getProviderModels?: () => Array<{ id: string; provider: string; contextLength?: number; pricing?: { prompt: import('decimal.js').Decimal; completion: import('decimal.js').Decimal } }>; private getCurrentUser?: () => { user?: NUser; metadata?: NostrMetadata }; @@ -67,6 +70,7 @@ export class SessionManager { constructor( fs: JSRuntimeFS, + nostr: NPool, aiSettings: { providers: AIProvider[] }, getProviderModels?: () => Array<{ id: string; provider: string; contextLength?: number; pricing?: { prompt: import('decimal.js').Decimal; completion: import('decimal.js').Decimal } }>, getCurrentUser?: () => { user?: NUser; metadata?: NostrMetadata }, @@ -75,6 +79,7 @@ export class SessionManager { pluginsPath?: string, ) { this.fs = fs; + this.git = new Git({ fs, nostr, corsProxy }); this.aiSettings = aiSettings; this.getProviderModels = getProviderModels; this.getCurrentUser = getCurrentUser; @@ -238,6 +243,15 @@ export class SessionManager { tool_calls: undefined }; + // Get repository URL if available + let repositoryUrl: string | undefined; + try { + const remoteUrl = await this.git.getRemoteURL(`${this.projectsPath}/${projectId}`, 'origin'); + repositoryUrl = remoteUrl || undefined; + } catch { + // No repository URL available + } + const systemPrompt = await makeSystemPrompt({ cwd: `${this.projectsPath}/${projectId}`, fs: this.fs, @@ -249,6 +263,7 @@ export class SessionManager { user, metadata, corsProxy: this.corsProxy, + repositoryUrl, }); // Prepare messages for AI diff --git a/src/lib/system.ts b/src/lib/system.ts index 84b7c1da..eae5e589 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -17,10 +17,11 @@ export interface MakeSystemPromptOpts { user?: NUser; metadata?: NostrMetadata; corsProxy?: string; + repositoryUrl?: string; } export async function makeSystemPrompt(opts: MakeSystemPromptOpts): Promise { - const { name, profession, tools, mode, fs, cwd, pluginsPath, user, metadata, corsProxy } = opts; + const { name, profession, tools, mode, fs, cwd, pluginsPath, user, metadata, corsProxy, repositoryUrl } = opts; let system = mode === "init" ? `You are ${name}, an expert ${profession}. 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.` @@ -62,6 +63,7 @@ You are operating within **Shakespeare**, an AI-powered Nostr website builder th - **Current Date**: ${currentDate} - **Current Page**: ${location.href} - **Current Working Directory**: ${cwd} +- **Repository URL**: ${repositoryUrl} ## What Shakespeare Is @@ -224,6 +226,39 @@ If you encounter CORS (Cross-Origin Resource Sharing) errors when fetching exter Replace \`{href}\`, \`{hostname}\`, or other URL components in the template as needed.`; } + // Add Edit with Shakespeare section + system += ` + +## 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 (repositoryUrl) { + const badgeUrl = new URL('/badge.svg', location.origin); + const editUrl = new URL('/clone', location.origin); + editUrl.searchParams.set('url', repositoryUrl); + + system += ` + +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](${badgeUrl})](${editUrl}) +\`\`\` + +**HTML/JSX/TSX:** +\`\`\`jsx + + Edit with Shakespeare + +\`\`\``; + } else { + system += ` + +**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.`; + } + // Add README.md if it exists try { const readmePath = join(cwd, "README.md");