mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Generate system prompt dynamically in SessionManager
This commit is contained in:
4 files changed
+25
-34
No files matched your search
@@ -17,13 +17,13 @@ export function SessionManagerProvider({ children }: SessionManagerProviderProps
|
||||
const { fs } = useFS();
|
||||
const { settings } = useAISettings();
|
||||
const { models } = useProviderModels();
|
||||
const { user } = useCurrentUser();
|
||||
const { user, metadata } = useCurrentUser();
|
||||
|
||||
const sessionManager = useMemo(() => {
|
||||
const getProviderModels = () => models;
|
||||
const getCurrentUser = () => user;
|
||||
const getCurrentUser = () => ({ user, metadata });
|
||||
return new SessionManager(fs, settings, getProviderModels, getCurrentUser);
|
||||
}, [fs, settings, models, user]);
|
||||
}, [fs, settings, models, user, metadata]);
|
||||
|
||||
// Cleanup on unmount
|
||||
useEffect(() => {
|
||||
|
||||
@@ -37,7 +37,6 @@ import { TypecheckTool } from '@/lib/tools/TypecheckTool';
|
||||
import { toolToOpenAI } from '@/lib/tools/openai-adapter';
|
||||
import { Tool } from '@/lib/tools/Tool';
|
||||
import OpenAI from 'openai';
|
||||
import { makeSystemPrompt } from '@/lib/system';
|
||||
import { saveFileToTmp } from '@/lib/fileUtils';
|
||||
import { useGit } from '@/hooks/useGit';
|
||||
import { useConsoleErrorAlert, useAIModelErrorAlert } from '@/hooks/useErrorDetection';
|
||||
@@ -119,7 +118,6 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
}, ref) => {
|
||||
const { t } = useTranslation();
|
||||
const [input, setInput] = useState('');
|
||||
const [systemPrompt, setSystemPrompt] = useState('');
|
||||
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
|
||||
const [attachedFiles, setAttachedFiles] = useState<File[]>([]);
|
||||
const [isDragOver, setIsDragOver] = useState(false);
|
||||
@@ -146,7 +144,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const { fs } = useFS();
|
||||
const { git } = useGit();
|
||||
const { user, metadata } = useCurrentUser();
|
||||
const { user } = useCurrentUser();
|
||||
const { models } = useProviderModels();
|
||||
|
||||
// Initialize AI chat with tools
|
||||
@@ -210,19 +208,6 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
updateMetadata(title, description);
|
||||
}, [updateMetadata]);
|
||||
|
||||
useEffect(() => {
|
||||
makeSystemPrompt({
|
||||
cwd,
|
||||
fs,
|
||||
mode: "agent",
|
||||
name: "Shakespeare",
|
||||
profession: "software extraordinaire",
|
||||
tools: Object.values(tools),
|
||||
user,
|
||||
metadata,
|
||||
}).then(setSystemPrompt)
|
||||
}, [fs, cwd, tools, user, metadata]);
|
||||
|
||||
const {
|
||||
messages,
|
||||
streamingMessage,
|
||||
@@ -237,7 +222,6 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
projectId,
|
||||
tools,
|
||||
customTools,
|
||||
systemPrompt,
|
||||
onUpdateMetadata,
|
||||
});
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ interface UseAIChatSessionOptions {
|
||||
projectId: string;
|
||||
tools?: Record<string, OpenAI.Chat.Completions.ChatCompletionTool>;
|
||||
customTools?: Record<string, Tool<unknown>>;
|
||||
systemPrompt?: string;
|
||||
maxSteps?: number;
|
||||
onUpdateMetadata?: (title: string, description: string) => void;
|
||||
}
|
||||
@@ -30,7 +29,6 @@ export function useAIChat({
|
||||
projectId,
|
||||
tools = {},
|
||||
customTools = {},
|
||||
systemPrompt,
|
||||
maxSteps = 50,
|
||||
onUpdateMetadata
|
||||
}: UseAIChatSessionOptions) {
|
||||
@@ -46,14 +44,14 @@ export function useAIChat({
|
||||
|
||||
const initSession = useCallback(async () => {
|
||||
// Load or update session
|
||||
const session = await sessionManager.loadSession(projectId, tools, customTools, systemPrompt, maxSteps);
|
||||
const session = await sessionManager.loadSession(projectId, tools, customTools, maxSteps);
|
||||
// Initialize individual state variables from existing session
|
||||
setMessages([...session.messages]);
|
||||
setStreamingMessage(session.streamingMessage ? { ...session.streamingMessage } : undefined);
|
||||
setIsLoading(session.isLoading);
|
||||
setTotalCost(session.totalCost || 0);
|
||||
setLastInputTokens(session.lastInputTokens || 0);
|
||||
}, [sessionManager, projectId, tools, customTools, systemPrompt, maxSteps]);
|
||||
}, [sessionManager, projectId, tools, customTools, maxSteps]);
|
||||
|
||||
// Initialize session
|
||||
useEffect(() => {
|
||||
|
||||
+19
-10
@@ -6,6 +6,8 @@ import { createAIClient } from './ai-client';
|
||||
import type { Tool } from './tools/Tool';
|
||||
import type { NUser } from '@nostrify/react/login';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
import { makeSystemPrompt } from './system';
|
||||
import { NostrMetadata } from '@nostrify/nostrify';
|
||||
|
||||
export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam | {
|
||||
role: 'assistant';
|
||||
@@ -18,7 +20,6 @@ export interface SessionState {
|
||||
projectId: string;
|
||||
tools: Record<string, OpenAI.Chat.Completions.ChatCompletionTool>;
|
||||
customTools: Record<string, Tool<unknown>>;
|
||||
systemPrompt?: string;
|
||||
maxSteps?: number;
|
||||
messages: AIMessage[];
|
||||
streamingMessage?: {
|
||||
@@ -55,13 +56,13 @@ export class SessionManager {
|
||||
private fs: JSRuntimeFS;
|
||||
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?: () => NUser | undefined;
|
||||
private getCurrentUser?: () => { user?: NUser; metadata?: NostrMetadata };
|
||||
|
||||
constructor(
|
||||
fs: JSRuntimeFS,
|
||||
aiSettings: { providers: AIProvider[] },
|
||||
getProviderModels?: () => Array<{ id: string; provider: string; contextLength?: number; pricing?: { prompt: import('decimal.js').Decimal; completion: import('decimal.js').Decimal } }>,
|
||||
getCurrentUser?: () => NUser | undefined
|
||||
getCurrentUser?: () => { user?: NUser; metadata?: NostrMetadata },
|
||||
) {
|
||||
this.fs = fs;
|
||||
this.aiSettings = aiSettings;
|
||||
@@ -76,7 +77,6 @@ export class SessionManager {
|
||||
projectId: string,
|
||||
tools: Record<string, OpenAI.Chat.Completions.ChatCompletionTool>,
|
||||
customTools: Record<string, Tool<unknown>>,
|
||||
systemPrompt?: string,
|
||||
maxSteps?: number
|
||||
): Promise<SessionState> {
|
||||
let messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [];
|
||||
@@ -98,7 +98,6 @@ export class SessionManager {
|
||||
projectId,
|
||||
tools,
|
||||
customTools,
|
||||
systemPrompt,
|
||||
maxSteps,
|
||||
isLoading: false,
|
||||
lastActivity: new Date(),
|
||||
@@ -114,7 +113,6 @@ export class SessionManager {
|
||||
session.tools = tools;
|
||||
|
||||
session.customTools = customTools;
|
||||
session.systemPrompt = systemPrompt;
|
||||
session.maxSteps = maxSteps;
|
||||
|
||||
this.sessions.set(projectId, session);
|
||||
@@ -207,8 +205,8 @@ export class SessionManager {
|
||||
const model = parsed.model;
|
||||
|
||||
// Initialize OpenAI client
|
||||
const currentUser = this.getCurrentUser?.();
|
||||
const openai = createAIClient(provider, currentUser);
|
||||
const { user, metadata } = this.getCurrentUser?.() ?? {};
|
||||
const openai = createAIClient(provider, user);
|
||||
|
||||
let stepCount = 0;
|
||||
const maxSteps = session.maxSteps || 50;
|
||||
@@ -230,9 +228,20 @@ export class SessionManager {
|
||||
tool_calls: undefined
|
||||
};
|
||||
|
||||
const systemPrompt = await makeSystemPrompt({
|
||||
cwd: `/projects/${projectId}`,
|
||||
fs: this.fs,
|
||||
mode: "agent",
|
||||
name: "Shakespeare",
|
||||
profession: "software extraordinaire",
|
||||
tools: Object.values(session.tools),
|
||||
user,
|
||||
metadata,
|
||||
});
|
||||
|
||||
// Prepare messages for AI
|
||||
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = session.systemPrompt
|
||||
? [{ role: 'system', content: session.systemPrompt }, ...session.messages]
|
||||
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = systemPrompt
|
||||
? [{ role: 'system', content: systemPrompt }, ...session.messages]
|
||||
: session.messages;
|
||||
|
||||
// Prepare completion options
|
||||
|
||||
Reference in new issue
Block a user