11import { useCallback , useEffect , useMemo , useState } from "react"
22
33import { PythonOutlined } from "@ant-design/icons"
4- import { FileCodeIcon , FileTsIcon } from "@phosphor-icons/react"
5- import { Tabs , Typography } from "antd"
4+ import { FileCode , FileTs } from "@phosphor-icons/react"
5+ import { Spin , Tabs , Typography } from "antd"
66import { useAtomValue } from "jotai"
77import dynamic from "next/dynamic"
88
99import { buildCurlSnippet } from "@/oss/code_snippets/endpoints/fetch_variant/curl"
1010import { buildPythonSnippet } from "@/oss/code_snippets/endpoints/fetch_variant/python"
1111import { buildTypescriptSnippet } from "@/oss/code_snippets/endpoints/fetch_variant/typescript"
12- import CopyButton from "@/oss/components/CopyButton/CopyButton"
13- import CodeBlock from "@/oss/components/DynamicCodeBlock/CodeBlock"
12+ import invokeLlmAppcURLCode from "@/oss/code_snippets/endpoints/invoke_llm_app/curl"
13+ import invokeLlmApppythonCode from "@/oss/code_snippets/endpoints/invoke_llm_app/python"
14+ import invokeLlmApptsCode from "@/oss/code_snippets/endpoints/invoke_llm_app/typescript"
15+ import LanguageCodeBlock from "@/oss/components/pages/overview/deployments/DeploymentDrawer/assets/LanguageCodeBlock"
1416import SelectVariant from "@/oss/components/Playground/Components/Menus/SelectVariant"
1517import VariantDetailsWithStatus from "@/oss/components/VariantDetailsWithStatus"
16- import { currentAppAtom } from "@/oss/state/app"
18+ import { useAppId } from "@/oss/hooks/useAppId"
19+ import { currentAppAtom , useURI } from "@/oss/state/app"
20+ import { stablePromptVariablesAtomFamily } from "@/oss/state/newPlayground/core/prompts"
1721import { revisionsByVariantIdAtomFamily , variantsAtom } from "@/oss/state/variant/atoms/fetcher"
1822import {
1923 latestRevisionInfoByVariantIdAtomFamily ,
@@ -29,13 +33,8 @@ interface VariantUseApiContentProps {
2933 initialRevisionId ?: string
3034}
3135
32- interface CodeSnippets {
33- python : string
34- typescript : string
35- bash : string
36- }
37-
3836const VariantUseApiContent = ( { initialRevisionId} : VariantUseApiContentProps ) => {
37+ const appId = useAppId ( )
3938 const variants = useAtomValue ( variantsAtom )
4039 const revisionList = useAtomValue ( revisionListAtom )
4140 const currentApp = useAtomValue ( currentAppAtom )
@@ -45,6 +44,15 @@ const VariantUseApiContent = ({initialRevisionId}: VariantUseApiContentProps) =>
4544 const [ selectedLang , setSelectedLang ] = useState ( "python" )
4645 const [ apiKeyValue , setApiKeyValue ] = useState ( "" )
4746
47+ // Get URI for the selected variant
48+ const { data : uri , isLoading : isUriQueryLoading } = useURI ( appId , selectedVariantId )
49+ const isLoading = Boolean ( selectedVariantId ) && isUriQueryLoading
50+
51+ // Get variable names for the selected revision
52+ const variableNames = useAtomValue (
53+ stablePromptVariablesAtomFamily ( selectedRevisionId || "" ) ,
54+ ) as string [ ]
55+
4856 const initialRevision = useMemo (
4957 ( ) => revisionList . find ( ( rev ) => rev . id === initialRevisionId ) ,
5058 [ initialRevisionId , revisionList ] ,
@@ -120,13 +128,52 @@ const VariantUseApiContent = ({initialRevisionId}: VariantUseApiContentProps) =>
120128 const variantSlug =
121129 ( selectedVariant as any ) ?. variantSlug ||
122130 selectedVariant ?. variantName ||
123- selectedRevision ?. variantName ||
131+ ( selectedRevision as any ) ?. variantName ||
124132 "my-variant-slug"
125133 const variantVersion = selectedRevision ?. revision ?? latestRevision ?. revision ?? 1
126134 const appSlug = ( currentApp as any ) ?. app_slug || currentApp ?. app_name || "my-app-slug"
127135 const apiKey = apiKeyValue || "YOUR_API_KEY"
128136
129- const codeSnippets : CodeSnippets = useMemo (
137+ const invokeLlmUrl = uri ?? ""
138+
139+ // Build params for invoke LLM (with variant refs instead of environment)
140+ const params = useMemo ( ( ) => {
141+ const synthesized = variableNames . map ( ( name ) => ( { name, input : name === "messages" } ) )
142+
143+ const mainParams : Record < string , any > = { }
144+ const secondaryParams : Record < string , any > = { }
145+
146+ synthesized . forEach ( ( item ) => {
147+ if ( item . input ) {
148+ mainParams [ item . name ] = "add_a_value"
149+ } else {
150+ secondaryParams [ item . name ] = "add_a_value"
151+ }
152+ } )
153+
154+ const hasMessagesParam = synthesized . some ( ( p ) => p ?. name === "messages" )
155+ const isChat = currentApp ?. app_type === "chat" || hasMessagesParam
156+ if ( isChat ) {
157+ mainParams [ "messages" ] = [
158+ {
159+ role : "user" ,
160+ content : "" ,
161+ } ,
162+ ]
163+ mainParams [ "inputs" ] = secondaryParams
164+ } else if ( Object . keys ( secondaryParams ) . length > 0 ) {
165+ mainParams [ "inputs" ] = secondaryParams
166+ }
167+
168+ // Use variant refs instead of environment
169+ mainParams [ "app" ] = appSlug
170+ mainParams [ "variant_slug" ] = variantSlug
171+ mainParams [ "variant_version" ] = variantVersion
172+
173+ return JSON . stringify ( mainParams , null , 2 )
174+ } , [ variableNames , currentApp ?. app_type , appSlug , variantSlug , variantVersion ] )
175+
176+ const fetchConfigCodeSnippet = useMemo (
130177 ( ) => ( {
131178 python : buildPythonSnippet ( appSlug , variantSlug , variantVersion ) ,
132179 typescript : buildTypescriptSnippet ( appSlug , variantSlug , variantVersion , apiKey ) ,
@@ -135,48 +182,48 @@ const VariantUseApiContent = ({initialRevisionId}: VariantUseApiContentProps) =>
135182 [ apiKey , appSlug , variantSlug , variantVersion ] ,
136183 )
137184
138- const renderTabChildren = useCallback ( ( ) => {
139- const activeSnippet = codeSnippets [ selectedLang as keyof CodeSnippets ]
185+ const invokeLlmAppCodeSnippet = useMemo (
186+ ( ) => ( {
187+ python : invokeLlmApppythonCode ( invokeLlmUrl , params , apiKeyValue || "x.xxxxxxxx" ) ,
188+ bash : invokeLlmAppcURLCode ( invokeLlmUrl , params , apiKeyValue || "x.xxxxxxxx" ) ,
189+ typescript : invokeLlmApptsCode ( invokeLlmUrl , params , apiKeyValue || "x.xxxxxxxx" ) ,
190+ } ) ,
191+ [ apiKeyValue , invokeLlmUrl , params ] ,
192+ )
140193
194+ const renderTabChildren = useCallback ( ( ) => {
141195 return (
142- < div className = "flex flex-col gap-3" >
143- < div className = "flex items-center justify-between" >
144- < Typography . Text className = "font-medium" > Use API</ Typography . Text >
145- < CopyButton text = { activeSnippet } icon = { true } buttonText = { null } />
146- </ div >
147- < CodeBlock language = { selectedLang } value = { activeSnippet } />
148- </ div >
196+ < Spin spinning = { isLoading } >
197+ < LanguageCodeBlock
198+ fetchConfigCodeSnippet = { fetchConfigCodeSnippet }
199+ invokeLlmAppCodeSnippet = { invokeLlmAppCodeSnippet }
200+ selectedLang = { selectedLang }
201+ handleOpenSelectDeployVariantModal = { ( ) => { } }
202+ invokeLlmUrl = { invokeLlmUrl }
203+ />
204+ </ Spin >
149205 )
150- } , [
151- apiKeyValue ,
152- codeSnippets ,
153- revisionList ,
154- selectedLang ,
155- selectedRevision ?. id ,
156- selectedRevision ?. isLatestRevision ,
157- selectedRevision ?. revision ,
158- selectedRevisionId ,
159- ] )
206+ } , [ fetchConfigCodeSnippet , invokeLlmAppCodeSnippet , invokeLlmUrl , isLoading , selectedLang ] )
160207
161208 const tabItems = useMemo (
162209 ( ) => [
163210 {
164211 key : "python" ,
165212 label : "Python" ,
166- icon : < PythonOutlined /> ,
167213 children : renderTabChildren ( ) ,
214+ icon : < PythonOutlined /> ,
168215 } ,
169216 {
170217 key : "typescript" ,
171218 label : "TypeScript" ,
172- icon : < FileTsIcon size = { 14 } /> ,
173219 children : renderTabChildren ( ) ,
220+ icon : < FileTs size = { 14 } /> ,
174221 } ,
175222 {
176223 key : "bash" ,
177224 label : "cURL" ,
178- icon : < FileCodeIcon size = { 14 } /> ,
179225 children : renderTabChildren ( ) ,
226+ icon : < FileCode size = { 14 } /> ,
180227 } ,
181228 ] ,
182229 [ renderTabChildren ] ,
@@ -221,10 +268,10 @@ const VariantUseApiContent = ({initialRevisionId}: VariantUseApiContentProps) =>
221268 < ApiKeyInput apiKeyValue = { apiKeyValue } onApiKeyChange = { setApiKeyValue } />
222269 </ div >
223270 < Tabs
271+ destroyOnHidden
272+ defaultActiveKey = { selectedLang }
224273 items = { tabItems }
225274 onChange = { setSelectedLang }
226- activeKey = { selectedLang }
227- destroyInactiveTabPane
228275 />
229276 </ div >
230277 )
0 commit comments