Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Enhance embodiment functionality and configuration

Muhammad Hamza Khancfe10bc9141a
diff --git a/packages/app/src/backend/config.ts b/packages/app/src/backend/config.ts
index 221520c5..fd46bce1 100644
--- a/packages/app/src/backend/config.ts
+++ b/packages/app/src/backend/config.ts
@@ -85,6 +85,8 @@ const config = {
PUB_API_SERVER: process.env.PUB_API_SERVER,
IS_AWS_ENVIRONMENT: process.env.IS_AWS_ENVIRONMENT || false,
DISABLE_DATA_POOL_V1: process.env.DISABLE_DATA_POOL_V1 || false,
+
+ INTERNAL_TRUSTED_SECRET: process.env.INTERNAL_TRUSTED_SECRET,
},
api: {
SMYTH_USER_API_URL: `${MW_BASE_URL}/v1`,
diff --git a/packages/app/src/builder-ui/pages/builder/agent-settings.ts b/packages/app/src/builder-ui/pages/builder/agent-settings.ts
index c62fab68..29c459ca 100644
--- a/packages/app/src/builder-ui/pages/builder/agent-settings.ts
+++ b/packages/app/src/builder-ui/pages/builder/agent-settings.ts
@@ -1319,6 +1319,18 @@ export async function openAlexaEmbodiment() {
);
}
+async function generateInternalToken(agentId: string) {
+ try {
+ const response = await fetch(`/oauth/token/${agentId}`);
+ const data = await response.json();
+
+ return data;
+ } catch (error) {
+ console.error('Error generating internal token:', error);
+ return { success: false, error: error.message };
+ }
+}
+
export async function openChatbotEmbodiment() {
// Observability.observeInteraction('chatbot_embodiment_click', { position: 'top right of builder inside dropdown' });
const { dev: testDomain, scheme } = builderStore.getState().agentDomains;
@@ -1327,7 +1339,8 @@ export async function openChatbotEmbodiment() {
if (modalBox) {
modalBox.style.width = '600px';
}
-
+ // Track if the iframe has been initialized, used for bypassing auth only once
+ let isInitialized = false;
let iframe = document.querySelector('#chatbot-iframe') as HTMLIFrameElement;
let content;
let actions = {};
@@ -1363,6 +1376,7 @@ export async function openChatbotEmbodiment() {
actions = {
Refresh: {
click: async (div) => {
+ isInitialized = false;
const refreshBtnContainer = document.querySelector('.refresh-chat-btn') as HTMLElement;
const iframe = document.querySelector('#chatbot-iframe') as HTMLIFrameElement;
// Check if refresh is already in progress
@@ -1433,6 +1447,38 @@ export async function openChatbotEmbodiment() {
},
['chatbot'],
);
+
+ // Only run initialization logic (token/auth) on first load
+ if (!isInitialized) {
+ EmbodimentRPCManager.send(
+ {
+ function: 'bypassAuth',
+ args: [workspace.agent.id],
+ },
+ ['chatbot'],
+ );
+ // Receive only from chatbot iframe
+ const unsubscribeChatbot = EmbodimentRPCManager.receive(
+ async (msg) => {
+ if (msg?.data?.request === 'issue-token') {
+ const response = await generateInternalToken(workspace.agent.id);
+ if (response?.success && response?.token) {
+ EmbodimentRPCManager.send(
+ {
+ function: 'issueToken',
+ args: [response.token],
+ },
+ ['chatbot'],
+ );
+ unsubscribeChatbot();
+ }
+ }
+ },
+ ['chatbot'],
+ );
+
+ isInitialized = true;
+ }
});
}
}
diff --git a/packages/app/src/react/features/embodiments/post-deployment-modal.tsx b/packages/app/src/react/features/embodiments/post-deployment-modal.tsx
index e2ed95eb..0380b1d3 100644
--- a/packages/app/src/react/features/embodiments/post-deployment-modal.tsx
+++ b/packages/app/src/react/features/embodiments/post-deployment-modal.tsx
@@ -513,7 +513,9 @@ function PostDeploymentModal({
) : !state.canUseEmbodiments ? (
<Tooltip>
<TooltipTrigger asChild>
- <FaLock className="text-gray-400 cursor-pointer" />
+ <div>
+ <FaLock className="text-gray-400 cursor-pointer" />
+ </div>
</TooltipTrigger>
<TooltipContent side="top">
<p>Premium Embodiment. Upgrade your plan</p>
diff --git a/packages/app/src/shared/services/embodiment_rpc_manager.ts b/packages/app/src/shared/services/embodiment_rpc_manager.ts
index 06ca0a4d..b4d25bac 100644
--- a/packages/app/src/shared/services/embodiment_rpc_manager.ts
+++ b/packages/app/src/shared/services/embodiment_rpc_manager.ts
@@ -9,10 +9,31 @@ type EmbodimentScope = 'chatbot' | 'swagger' | 'all';
interface RPCMsg {
function: string;
- args: any[];
+ args: unknown[];
+}
+
+interface ReceivedMessage {
+ data: any;
+ origin: string;
+ source: MessageEventSource | null;
+}
+
+type MessageCallback = (msg: ReceivedMessage) => void;
+
+interface CallbackRegistration {
+ callback: MessageCallback;
+ scope?: EmbodimentScope[];
}
export class EmbodimentRPCManager {
+ private static messageListener: ((event: MessageEvent) => void) | null = null;
+ private static registeredCallbacks: Set<CallbackRegistration> = new Set();
+
+ /**
+ * Sends an RPC message to the specified embodiment scopes
+ * @param msg - The RPC message to send (can be string or RPCMsg object)
+ * @param scope - Array of scopes to send the message to
+ */
public static send(msg: RPCMsg, scope: EmbodimentScope[]) {
let _msg = typeof msg === 'string' ? msg : JSON.stringify(msg);
@@ -26,16 +47,123 @@ export class EmbodimentRPCManager {
}
}
+ /**
+ * Registers a callback to receive messages from embodiment iframes
+ * @param callback - Function to call when a message is received
+ * @param scope - Optional array of scopes to filter messages by (if not provided, receives from all scopes)
+ * @returns Function to unsubscribe the callback
+ */
+ public static receive(callback: MessageCallback, scope?: EmbodimentScope[]): () => void {
+ // Create callback registration with scope
+ const registration: CallbackRegistration = {
+ callback,
+ scope,
+ };
+
+ // Add callback registration to registered callbacks
+ this.registeredCallbacks.add(registration);
+
+ // Set up message listener if not already set up
+ if (!this.messageListener) {
+ this.messageListener = (event: MessageEvent) => {
+ // Determine which scope this message came from
+ const messageScope = this.determineMessageScope(event);
+
+ // Parse the message data
+ let parsedData: string | RPCMsg;
+ try {
+ parsedData = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
+ } catch {
+ // If parsing fails, use raw data as string
+ parsedData = typeof event.data === 'string' ? event.data : JSON.stringify(event.data);
+ }
+
+ // Create received message object
+ const receivedMsg: ReceivedMessage = {
+ data: parsedData,
+ origin: event.origin,
+ source: event.source,
+ };
+
+ // Call registered callbacks that match the scope filter
+ this.registeredCallbacks.forEach((registration) => {
+ // Check if this callback should receive this message based on scope
+ const shouldReceive =
+ !registration.scope ||
+ registration.scope.includes('all') ||
+ !messageScope ||
+ registration.scope.includes(messageScope);
+
+ if (shouldReceive) {
+ try {
+ registration.callback(receivedMsg);
+ } catch (error) {
+ console.error('Error in message callback:', error);
+ }
+ }
+ });
+ };
+
+ window.addEventListener('message', this.messageListener);
+ }
+
+ // Return unsubscribe function
+ return () => {
+ // Find and remove the registration
+ this.registeredCallbacks.forEach((reg) => {
+ if (reg.callback === callback) {
+ this.registeredCallbacks.delete(reg);
+ }
+ });
+
+ // If no more callbacks, remove the listener
+ if (this.registeredCallbacks.size === 0 && this.messageListener) {
+ window.removeEventListener('message', this.messageListener);
+ this.messageListener = null;
+ }
+ };
+ }
+
+ /**
+ * Determines which embodiment scope a message originated from based on the event source
+ * @param event - The message event
+ * @returns The scope if determinable, null otherwise
+ */
+ private static determineMessageScope(event: MessageEvent): EmbodimentScope | null {
+ // Try to determine scope by checking if the source is one of our iframes
+ const chatbotIframe = document.querySelector('#chatbot-iframe') as HTMLIFrameElement;
+ const swaggerIframe = document.querySelector('#swagger-iframe') as HTMLIFrameElement;
+
+ if (chatbotIframe && event.source === chatbotIframe.contentWindow) {
+ return 'chatbot';
+ }
+
+ if (swaggerIframe && event.source === swaggerIframe.contentWindow) {
+ return 'swagger';
+ }
+
+ // If we can't determine, return null (will still be delivered if no scope filter)
+ return null;
+ }
+
+ /**
+ * Sends a message to the chatbot iframe
+ * @param msg - The message string to send
+ */
private static sendChatbotMsg(msg: string) {
const iframe = document.querySelector('#chatbot-iframe') as HTMLIFrameElement;
- if (iframe) {
+ if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage(msg, '*');
}
}
+ /**
+ * Sends a message to the swagger iframe
+ * @param msg - The message string to send
+ */
private static sendSwaggerMsg(msg: string) {
const iframe = document.querySelector('#swagger-iframe') as HTMLIFrameElement;
- if (iframe) {
+ if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage(msg, '*');
}
}