Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject additional headers #10

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added possibility to inject additional headers
  • Loading branch information
Yassine Selmi committed Nov 10, 2023
commit ee23c2ed96693dd249173904f6fc91ea651e38fb
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 |
| additionalHeaders | 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.

- **additionalHeaders:**
- 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",
},
}));