Skip to content

Commit 78d67ad

Browse files
feat: Allow sending commands from custom elements (#2459)
This PR enhances the functionality of custom elements by adding the ability to send a command with a user message via the `sendUserMessage` function. **Feature Implementation**: - Modified `frontend/src/components/Elements/CustomElement/index.tsx` to add an optional command parameter to the `sendUserMessage` function. - Updated `frontend/src/components/Elements/CustomElement/Renderer.tsx` as well. **Testing**: - Added a new test in `cypress/e2e/custom_element_command` to verify the feature from the UI to the backend. --------- Co-authored-by: Josh Hayes <[email protected]>
1 parent afa326a commit 78d67ad

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import chainlit as cl
2+
3+
4+
@cl.on_chat_start
5+
async def on_start():
6+
custom_element = cl.CustomElement(name="Commander")
7+
await cl.Message(
8+
content="This message has a custom element!", elements=[custom_element]
9+
).send()
10+
11+
12+
@cl.on_message
13+
async def on_message(message: cl.Message):
14+
if message.command:
15+
await cl.Message(content=f"Received command: {message.command}").send()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Button } from '@/components/ui/button';
2+
3+
export default function Commander() {
4+
return (
5+
<div id="custom-commander" className="mt-4 flex flex-col gap-2">
6+
<Button
7+
id="send"
8+
onClick={() =>
9+
sendUserMessage('Hello from custom element', 'my_command')
10+
}
11+
>
12+
{' '}
13+
Send with command
14+
</Button>
15+
</div>
16+
);
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
describe('Custom Element Command', () => {
2+
it('should be able to send a command from a custom element', () => {
3+
cy.get('.step').should('have.length', 1);
4+
cy.get('.step').eq(0).find('.inline-custom').should('have.length', 1);
5+
6+
cy.get('#send').click();
7+
8+
cy.get('.step').should('have.length', 3);
9+
cy.get('.step').eq(1).should('contain', 'Hello from custom element');
10+
cy.get('.step').eq(2).should('contain', 'Received command: my_command');
11+
});
12+
});

frontend/src/components/Elements/CustomElement/Renderer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const createMockAPIs = () => {
2727
return { success: true };
2828
},
2929

30-
sendUserMessage: (message: string): void => {
31-
console.log('sendUserMessage called with:', message);
30+
sendUserMessage: (message: string, command?: string): void => {
31+
console.log('sendUserMessage called with:', message, command);
3232
}
3333
};
3434
};

frontend/src/components/Elements/CustomElement/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@ const CustomElement = memo(function ({ element }: { element: ICustomElement }) {
6666
);
6767

6868
const sendUserMessage = useCallback(
69-
(message: string) => {
69+
(message: string, command?: string) => {
7070
return sendMessage({
7171
threadId: '',
7272
id: uuidv4(),
7373
name: user?.identifier || 'User',
7474
type: 'user_message',
7575
output: message,
7676
createdAt: new Date().toISOString(),
77-
metadata: { location: window.location.href }
77+
metadata: { location: window.location.href },
78+
command
7879
});
7980
},
8081
[sendMessage, user]

0 commit comments

Comments
 (0)