Recipe variables (#5365)
Co-authored-by: Douwe Osinga <douwe@squareup.com> Co-authored-by: Zane Staggs <zane@squareup.com>
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { ChatState } from '../types/chatState';
|
||||
import { Conversation, Message, resumeAgent, Session } from '../api';
|
||||
import {
|
||||
Conversation,
|
||||
Message,
|
||||
resumeAgent,
|
||||
Session,
|
||||
updateFromSession,
|
||||
updateSessionUserRecipeValues,
|
||||
} from '../api';
|
||||
import { getApiUrl } from '../config';
|
||||
import { createUserMessage, getCompactingMessage, getThinkingMessage } from '../types/message';
|
||||
|
||||
@@ -71,6 +78,7 @@ interface UseChatStreamReturn {
|
||||
messages: Message[];
|
||||
chatState: ChatState;
|
||||
handleSubmit: (userMessage: string) => Promise<void>;
|
||||
setRecipeUserParams: (values: Record<string, string>) => Promise<void>;
|
||||
stopStreaming: () => void;
|
||||
sessionLoadError?: string;
|
||||
}
|
||||
@@ -370,6 +378,44 @@ export function useChatStream({
|
||||
[sessionId, setMessagesAndLog, onFinish]
|
||||
);
|
||||
|
||||
const setRecipeUserParams = useCallback(
|
||||
async (user_recipe_values: Record<string, string>) => {
|
||||
if (session) {
|
||||
await updateSessionUserRecipeValues({
|
||||
path: {
|
||||
session_id: sessionId,
|
||||
},
|
||||
body: {
|
||||
userRecipeValues: user_recipe_values,
|
||||
},
|
||||
throwOnError: true,
|
||||
});
|
||||
// TODO(Douwe): get this from the server instead of emulating it here
|
||||
setSession({
|
||||
...session,
|
||||
user_recipe_values,
|
||||
});
|
||||
} else {
|
||||
setSessionLoadError("can't call setRecipeParams without a session");
|
||||
}
|
||||
},
|
||||
[sessionId, session, setSessionLoadError]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
// This should happen on the server when the session is loaded or changed
|
||||
// use session.id to support changing of sessions rather than depending on the
|
||||
// stable sessionId.
|
||||
if (session) {
|
||||
updateFromSession({
|
||||
body: {
|
||||
session_id: session.id,
|
||||
},
|
||||
throwOnError: true,
|
||||
});
|
||||
}
|
||||
}, [session]);
|
||||
|
||||
useEffect(() => {
|
||||
if (initialMessage && session && messages.length === 0 && chatState === ChatState.Idle) {
|
||||
log.messages('auto-submit-initial', 0, { initialMessage: initialMessage.slice(0, 50) });
|
||||
@@ -397,5 +443,6 @@ export function useChatStream({
|
||||
chatState,
|
||||
handleSubmit,
|
||||
stopStreaming,
|
||||
setRecipeUserParams,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user