Autorun chat in playground to show greeting

This commit is contained in:
ramnique 2025-03-07 01:23:13 +05:30
parent d15eddc951
commit 3ea08895b8
4 changed files with 47 additions and 41 deletions

View file

@ -27,6 +27,7 @@ export const WorkflowPrompt = z.object({
type: z.union([ type: z.union([
z.literal('base_prompt'), z.literal('base_prompt'),
z.literal('style_prompt'), z.literal('style_prompt'),
z.literal('greeting'),
]), ]),
prompt: z.string(), prompt: z.string(),
}); });

View file

@ -25,6 +25,7 @@ export function App({
}) { }) {
const [counter, setCounter] = useState<number>(0); const [counter, setCounter] = useState<number>(0);
const [testProfile, setTestProfile] = useState<z.infer<typeof TestProfile> | null>(null); const [testProfile, setTestProfile] = useState<z.infer<typeof TestProfile> | null>(null);
const [systemMessage, setSystemMessage] = useState<string>(defaultSystemMessage);
const [chat, setChat] = useState<z.infer<typeof PlaygroundChat>>({ const [chat, setChat] = useState<z.infer<typeof PlaygroundChat>>({
projectId, projectId,
createdAt: new Date().toISOString(), createdAt: new Date().toISOString(),
@ -33,6 +34,11 @@ export function App({
systemMessage: defaultSystemMessage, systemMessage: defaultSystemMessage,
}); });
function handleSystemMessageChange(message: string) {
setSystemMessage(message);
setCounter(counter + 1);
}
function handleTestProfileChange(profile: WithStringId<z.infer<typeof TestProfile>> | null) { function handleTestProfileChange(profile: WithStringId<z.infer<typeof TestProfile>> | null) {
setTestProfile(profile); setTestProfile(profile);
setCounter(counter + 1); setCounter(counter + 1);
@ -105,6 +111,8 @@ export function App({
testProfile={testProfile} testProfile={testProfile}
messageSubscriber={messageSubscriber} messageSubscriber={messageSubscriber}
onTestProfileChange={handleTestProfileChange} onTestProfileChange={handleTestProfileChange}
systemMessage={systemMessage}
onSystemMessageChange={handleSystemMessageChange}
/> />
</div> </div>
</Pane> </Pane>

View file

@ -24,6 +24,8 @@ export function Chat({
messageSubscriber, messageSubscriber,
testProfile=null, testProfile=null,
onTestProfileChange, onTestProfileChange,
systemMessage,
onSystemMessageChange,
}: { }: {
chat: z.infer<typeof PlaygroundChat>; chat: z.infer<typeof PlaygroundChat>;
projectId: string; projectId: string;
@ -31,6 +33,8 @@ export function Chat({
messageSubscriber?: (messages: z.infer<typeof apiV1.ChatMessage>[]) => void; messageSubscriber?: (messages: z.infer<typeof apiV1.ChatMessage>[]) => void;
testProfile?: z.infer<typeof TestProfile> | null; testProfile?: z.infer<typeof TestProfile> | null;
onTestProfileChange: (profile: WithStringId<z.infer<typeof TestProfile>> | null) => void; onTestProfileChange: (profile: WithStringId<z.infer<typeof TestProfile>> | null) => void;
systemMessage: string;
onSystemMessageChange: (message: string) => void;
}) { }) {
const [messages, setMessages] = useState<z.infer<typeof apiV1.ChatMessage>[]>(chat.messages); const [messages, setMessages] = useState<z.infer<typeof apiV1.ChatMessage>[]>(chat.messages);
const [loadingAssistantResponse, setLoadingAssistantResponse] = useState<boolean>(false); const [loadingAssistantResponse, setLoadingAssistantResponse] = useState<boolean>(false);
@ -42,7 +46,6 @@ export function Chat({
const [fetchResponseError, setFetchResponseError] = useState<string | null>(null); const [fetchResponseError, setFetchResponseError] = useState<string | null>(null);
const [lastAgenticRequest, setLastAgenticRequest] = useState<unknown | null>(null); const [lastAgenticRequest, setLastAgenticRequest] = useState<unknown | null>(null);
const [lastAgenticResponse, setLastAgenticResponse] = useState<unknown | null>(null); const [lastAgenticResponse, setLastAgenticResponse] = useState<unknown | null>(null);
const [systemMessage, setSystemMessage] = useState<string | undefined>(testProfile?.context);
const [isProfileSelectorOpen, setIsProfileSelectorOpen] = useState(false); const [isProfileSelectorOpen, setIsProfileSelectorOpen] = useState(false);
// collect published tool call results // collect published tool call results
@ -142,18 +145,17 @@ export function Chat({
} }
} }
// if no messages, return
if (messages.length === 0) {
return;
}
// if last message is not from role user // if last message is not from role user
// or tool, return // or tool, return
if (messages.length > 0) {
const last = messages[messages.length - 1]; const last = messages[messages.length - 1];
if (fetchResponseError) { if (last.role !== 'user' && last.role !== 'tool') {
return; return;
} }
if (last.role !== 'user' && last.role !== 'tool') { }
// if there is an error, return
if (fetchResponseError) {
return; return;
} }
@ -274,10 +276,6 @@ export function Chat({
navigator.clipboard.writeText(jsonString); navigator.clipboard.writeText(jsonString);
} }
function handleSystemMessageChange(message: string) {
setSystemMessage(message);
}
return <div className="relative h-full flex flex-col gap-8 pt-8 overflow-auto"> return <div className="relative h-full flex flex-col gap-8 pt-8 overflow-auto">
<CopyAsJsonButton onCopy={handleCopyChat} /> <CopyAsJsonButton onCopy={handleCopyChat} />
<div className="absolute top-0 left-0 flex items-center gap-1"> <div className="absolute top-0 left-0 flex items-center gap-1">
@ -304,14 +302,14 @@ export function Chat({
<Messages <Messages
projectId={projectId} projectId={projectId}
messages={messages} messages={messages}
systemMessage={systemMessage}
toolCallResults={toolCallResults} toolCallResults={toolCallResults}
handleToolCallResults={handleToolCallResults} handleToolCallResults={handleToolCallResults}
loadingAssistantResponse={loadingAssistantResponse} loadingAssistantResponse={loadingAssistantResponse}
loadingUserResponse={loadingUserResponse} loadingUserResponse={loadingUserResponse}
workflow={workflow} workflow={workflow}
testProfile={testProfile} testProfile={testProfile}
onSystemMessageChange={handleSystemMessageChange} systemMessage={systemMessage}
onSystemMessageChange={onSystemMessageChange}
/> />
<div className="shrink-0"> <div className="shrink-0">
{fetchResponseError && ( {fetchResponseError && (

View file

@ -94,7 +94,7 @@ function ToolCalls({
messages, messages,
sender, sender,
workflow, workflow,
testProfile=null, testProfile = null,
systemMessage, systemMessage,
}: { }: {
toolCalls: z.infer<typeof apiV1.AssistantMessageWithToolCalls>['tool_calls']; toolCalls: z.infer<typeof apiV1.AssistantMessageWithToolCalls>['tool_calls'];
@ -143,7 +143,7 @@ function ToolCall({
messages, messages,
sender, sender,
workflow, workflow,
testProfile=null, testProfile = null,
systemMessage, systemMessage,
}: { }: {
toolCall: z.infer<typeof apiV1.AssistantMessageWithToolCalls>['tool_calls'][number]; toolCall: z.infer<typeof apiV1.AssistantMessageWithToolCalls>['tool_calls'][number];
@ -421,7 +421,7 @@ function MockToolCall({
projectId, projectId,
messages, messages,
sender, sender,
testProfile=null, testProfile = null,
workflowTool, workflowTool,
systemMessage, systemMessage,
}: { }: {
@ -594,42 +594,40 @@ function ExpandableContent({
function SystemMessage({ function SystemMessage({
content, content,
onChange, onChange,
locked locked = false,
}: { }: {
content: string, content: string,
onChange: (content: string) => void, onChange: (content: string) => void,
locked: boolean locked?: boolean,
}) { }) {
return ( return <div className="text-sm">
<div className="border border-gray-300 dark:border-gray-700 p-2 rounded-lg flex flex-col gap-2 bg-white dark:bg-gray-900">
<div className="text-sm text-gray-500 dark:text-gray-400 font-medium">CONTEXT</div>
<EditableField <EditableField
light label="Context"
value={content} value={content}
onChange={onChange} onChange={onChange}
locked={locked}
multiline multiline
markdown markdown
locked={locked}
placeholder={`Provide context about the user (e.g. user ID, user name) to the assistant at the start of chat, for testing purposes.`} placeholder={`Provide context about the user (e.g. user ID, user name) to the assistant at the start of chat, for testing purposes.`}
showSaveButton={true}
showDiscardButton={true}
/> />
</div> </div>;
);
} }
export function Messages({ export function Messages({
projectId, projectId,
systemMessage,
messages, messages,
toolCallResults, toolCallResults,
handleToolCallResults, handleToolCallResults,
loadingAssistantResponse, loadingAssistantResponse,
loadingUserResponse, loadingUserResponse,
workflow, workflow,
testProfile=null, testProfile = null,
systemMessage,
onSystemMessageChange, onSystemMessageChange,
}: { }: {
projectId: string; projectId: string;
systemMessage: string | undefined;
messages: z.infer<typeof apiV1.ChatMessage>[]; messages: z.infer<typeof apiV1.ChatMessage>[];
toolCallResults: Record<string, z.infer<typeof apiV1.ToolMessage>>; toolCallResults: Record<string, z.infer<typeof apiV1.ToolMessage>>;
handleToolCallResults: (results: z.infer<typeof apiV1.ToolMessage>[]) => void; handleToolCallResults: (results: z.infer<typeof apiV1.ToolMessage>[]) => void;
@ -637,6 +635,7 @@ export function Messages({
loadingUserResponse: boolean; loadingUserResponse: boolean;
workflow: z.infer<typeof Workflow>; workflow: z.infer<typeof Workflow>;
testProfile: z.infer<typeof TestProfile> | null; testProfile: z.infer<typeof TestProfile> | null;
systemMessage: string | undefined;
onSystemMessageChange: (message: string) => void; onSystemMessageChange: (message: string) => void;
}) { }) {
const messagesEndRef = useRef<HTMLDivElement>(null); const messagesEndRef = useRef<HTMLDivElement>(null);
@ -652,7 +651,7 @@ export function Messages({
<SystemMessage <SystemMessage
content={testProfile?.context || systemMessage || ''} content={testProfile?.context || systemMessage || ''}
onChange={onSystemMessageChange} onChange={onSystemMessageChange}
locked={testProfile !== null || messages.length > 0} locked={testProfile !== null}
/> />
{messages.map((message, index) => { {messages.map((message, index) => {
if (message.role === 'assistant') { if (message.role === 'assistant') {