Skip to content

Commit

Permalink
Merge pull request #10 from yassineselmi/inject-additional-headers
Browse files Browse the repository at this point in the history
Inject additional headers
  • Loading branch information
anovazzi1 authored Feb 27, 2024
2 parents 3c1d2b0 + fe9ba1e commit e5d97ee
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Use the widget API to customize your widget:
| user_message_style | json | No |
| width | number | No |
| window_title | string | No |
| additional_headers | json | No |

- **bot_message_style:**
- Type: JSON
Expand Down Expand Up @@ -240,6 +241,12 @@ Use the widget API to customize your widget:
- Required: No
- Description: Title for the chat window, displayed in the header or title bar.

- **additional_headers:**
- Type: JSON
- Required: No
- Description: Additional headers to be sent to Langflow server


## Live example:
Try out or [live example](https://codesandbox.io/s/langflow-embedded-chat-example-dv9zpx) to see how the Langflow Embedded Chat ⛓️ works.

Expand Down
2 changes: 1 addition & 1 deletion dist/build/static/js/bundle.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/chatWidget/chatWindow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default function ChatWindow({
width = 450,
height = 650,
tweaks,
sessionId
sessionId,
additional_headers
}: {
api_key?: string;
chat_inputs: Object;
Expand Down Expand Up @@ -67,6 +68,8 @@ export default function ChatWindow({
width?: number;
height?: number;
sessionId: React.MutableRefObject<string>;
additional_headers?: {[key:string]:string};

}) {
const [value, setValue] = useState<string>("");
const ref = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -102,7 +105,7 @@ export default function ChatWindow({
addMessage({ message: value, isSend: true });
setSendingMessage(true);
setValue("");
sendMessage(hostUrl, flowId, value, chat_inputs,chat_input_field,sessionId, tweaks,api_key)
sendMessage(hostUrl, flowId, value, chat_inputs,chat_input_field,sessionId, tweaks,api_key, additional_headers)
.then((res) => {
if (
res.data &&
Expand Down
3 changes: 3 additions & 0 deletions src/chatWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function ChatWidget({
input_style,
placeholder_sending,
input_container_style,
additional_headers,
}: {
api_key?: string;
chat_inputs: Object;
Expand All @@ -55,6 +56,7 @@ export default function ChatWidget({
host_url: string;
flow_id: string;
tweaks?: { [key: string]: any };
additional_headers?: {[key:string]:string};
}) {
const [open, setOpen] = useState(false);
const [messages, setMessages] = useState<ChatMessageType[]>([]);
Expand Down Expand Up @@ -1009,6 +1011,7 @@ input::-ms-input-placeholder { /* Microsoft Edge */
triggerRef={triggerRef}
position={chat_position}
sessionId={sessionId}
additional_headers={additional_headers}
/>
</>
);
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";

export async function sendMessage(baseUrl: string, flowId: string, message: string,inputs: any,input_field:string,sessionId:React.MutableRefObject<string>, tweaks?: Object,api_key?:string) {
export async function sendMessage(baseUrl: string, flowId: string, message: string,inputs: any,input_field:string,sessionId:React.MutableRefObject<string>, tweaks?: Object,api_key?:string,additional_headers?:{[key:string]:string}) {
let data:any;
inputs[input_field] = message;
if (tweaks) {
Expand All @@ -9,10 +9,14 @@ export async function sendMessage(baseUrl: string, flowId: string, message: stri
else {
data = { inputs: inputs };
}
const headers:{[key:string]:string}= {"Content-Type": "application/json"}
let headers:{[key:string]:string}= {"Content-Type": "application/json"}
if( api_key){
headers["x-api-key"]=api_key;
}
if (additional_headers){
headers = Object.assign(headers, additional_headers);
// headers = {...headers, ...additional_headers};
}
if(sessionId.current && sessionId.current!=""){
data.session_id=sessionId.current;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ customElements.define('langflow-chat', r2wc(ChatWidget, {
input_style:"json",
input_container_style:"json",
chat_position:"string",
additional_headers:"json",
},
}));

0 comments on commit e5d97ee

Please sign in to comment.