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

feat: add golang in tab for integration #4641

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/frontend/src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ export const TABS_ORDER = [
"js api",
"python code",
"chat widget html",
"golang code",
];

export const LANGFLOW_ACCESS_TOKEN = "access_token_lf";
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/customization/hooks/use-custom-api-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getCurlRunCode,
getCurlWebhookCode,
} from "@/modals/apiModal/utils/get-curl-code";
import getGolangCode from "@/modals/apiModal/utils/get-golang-code";
import getJsApiCode from "@/modals/apiModal/utils/get-js-api-code";
import getPythonApiCode from "@/modals/apiModal/utils/get-python-api-code";
import getPythonCode from "@/modals/apiModal/utils/get-python-code";
Expand All @@ -14,6 +15,7 @@ export function useCustomAPICode() {
getJsApiCode,
getPythonApiCode,
getPythonCode,
getGolangCode,
getWidgetCode,
};
}
67 changes: 67 additions & 0 deletions src/frontend/src/modals/apiModal/utils/get-golang-code.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { GetCodeType } from "@/types/tweaks";

/**
* Function to get the Golang code for the API
* @param {string} flowName - The current flow name
* @param {any[]} tweaksBuildedObject - The tweaks
* @param {boolean} activeTweaks - Whether tweaks are active
* @returns {string} - The Golang code snippet
*/
export default function getGolangCode({
flowName,
tweaksBuildedObject,
activeTweaks,
}: GetCodeType): string {
// Format tweaks for Go syntax
let tweaksString = "langflowclient.Options{}";
if (
activeTweaks &&
tweaksBuildedObject &&
Object.keys(tweaksBuildedObject).length > 0
) {
tweaksString = `langflowclient.Options${JSON.stringify(
tweaksBuildedObject,
null,
2,
)
.replace(/"([^"]+)":/g, "$1:") // Remove quotes from keys
.replace(/"/g, `'`) // Convert double quotes to single quotes
.replace(/,/g, ",\n")}`; // Format commas
}

return `package main

import (
"fmt"
"log"

"github.com/devalexandre/langflowgo/langflowclient"
)

func main() {
client := langflowclient.LangflowClient{
BaseURL: "http://127.0.0.1:7860",
APIKey: "your_api_key_here",
}

tweaks := ${tweaksString}

response, err := client.RunFlow("${flowName}", "User message", tweaks, ${activeTweaks},
func(data map[string]interface{}) {
fmt.Println("Received:", data)
},
func(message string) {
fmt.Println("Stream Closed:", message)
},
func(err error) {
fmt.Println("Stream Error:", err)
},
)

if err != nil {
log.Fatal(err)
}

fmt.Println("Flow completed successfully:", response)
}`;
}
11 changes: 11 additions & 0 deletions src/frontend/src/modals/apiModal/utils/tabs-array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ export function createTabsArray(
code: codes.widgetCode,
});
}
if (codes.golangCode) {
tabs.push({
name: "Golang Code",
mode: "golang",
image:
"https://w7.pngwing.com/pngs/566/160/png-transparent-golang-hd-logo-thumbnail.png",
language: "go",
code: codes.golangCode,
hasTweaks: includeTweaks,
});
}
if (includeTweaks) {
tabs.push({
name: "Tweaks",
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/src/stores/tweaksStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ export const useTweaksStore = create<TweaksStoreType>((set, get) => ({
if (getCodes.getPythonCode) {
codesObj["pythonCode"] = getCodes.getPythonCode(props);
}
if (getCodes.getGolangCode) {
codesObj["golangCode"] = getCodes.getGolangCode(props);
}
if (getCodes.getWidgetCode) {
codesObj["widgetCode"] = getCodes.getWidgetCode(props);
}
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/types/tweaks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type GetCodesType = {
getJsApiCode?: (GetCodeType) => string;
getPythonApiCode?: (GetCodeType) => string;
getPythonCode?: (GetCodeType) => string;
getGolangCode?: (GetCodeType) => string;
getWidgetCode?: (GetCodeType) => string;
};

Expand Down
Loading