Skip to content

Commit c50434a

Browse files
committed
feat: open ui callback and timeout
1 parent b48989c commit c50434a

File tree

9 files changed

+92
-12
lines changed

9 files changed

+92
-12
lines changed

frontend/src/App.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'vue-sonner/style.css'
1717
1818
const { t } = useI18n()
1919
const authStore = useAuthStore()
20-
const { replace, currentRoute } = useRouter()
20+
const { currentRoute } = useRouter()
2121
const themeStore = useThemeStore()
2222
const serviceStore = useServiceStore()
2323
@@ -31,7 +31,7 @@ onMounted(async () => {
3131
3232
authState.value = await authStore.checkAuth()
3333
isCheckingAuth.value = false
34-
setTimeout(() => replace(currentRoute.value.path), 100)
34+
setTimeout(() => currentRoute.value.query = {}, 100)
3535
Clarity.init('sx60zbxtfz')
3636
})
3737
@@ -40,6 +40,18 @@ watch(() => serviceStore.serverConnected, async (connected) => {
4040
authState.value = await authStore.checkAuth()
4141
}
4242
})
43+
44+
async function onUIOpened() {
45+
const params = new URLSearchParams(window.location.search)
46+
const session = params.get('session')
47+
if (!session)
48+
return
49+
await serviceStore.fetch('ui/opened', {
50+
method: 'POST',
51+
body: JSON.stringify({ session }),
52+
})
53+
}
54+
onUIOpened()
4355
</script>
4456

4557
<template>
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
<script setup lang="ts">
2+
import { VisuallyHidden } from 'reka-ui'
3+
import { useI18n } from 'vue-i18n'
24
import SiliconFlowCard from '@/components/SiliconFlowCard.vue'
3-
import { Dialog, DialogContent } from '@/components/ui/dialog'
5+
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
46
57
const emit = defineEmits<{
68
configured: [provider: string]
79
}>()
810
911
const open = defineModel<boolean>('open')
12+
13+
const { t } = useI18n()
1014
</script>
1115

1216
<template>
1317
<Dialog v-model:open="open">
1418
<DialogContent class="sm:max-w-lg p-0 border-none">
15-
<SiliconFlowCard show-title @configured="(provider) => emit('configured', provider)" />
19+
<VisuallyHidden>
20+
<DialogHeader>
21+
<DialogTitle>{{ t('providers.siliconFlow') }}</DialogTitle>
22+
<DialogDescription>
23+
{{ t('providers.siliconFlowDescription') }}
24+
</DialogDescription>
25+
</DialogHeader>
26+
</VisuallyHidden>
27+
<SiliconFlowCard show-title @configured="(provider) => { open = false; emit('configured', provider) }" />
1628
</DialogContent>
1729
</Dialog>
1830
</template>

frontend/src/locales/zh-CN.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
"manualConfigSuccess": "手动配置提供商保存成功",
178178
"manualConfigError": "手动配置提供商保存失败"
179179
},
180-
"manualConfigSuccess": "配置成果"
180+
"manualConfigSuccess": "配置成功"
181181
},
182182
"siliconFlow": {
183183
"loggedIn": "已登录",

frontend/src/stores/service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { useAuthStore } from '@/stores/auth'
77
export const useServiceStore = defineStore('service', () => {
88
const authStore = useAuthStore()
99

10+
const params = new URLSearchParams(window.location.search)
11+
const token = ref<string | null>(params.get('token') || null)
12+
1013
const serverConnected = ref(true)
1114
const servicePort = ref<number | null>(null)
1215
const serviceHost = computed(() => {
@@ -23,8 +26,11 @@ export const useServiceStore = defineStore('service', () => {
2326
})
2427
let requireFindService = true
2528

26-
const params = new URLSearchParams(window.location.search)
27-
const token = ref<string | null>(params.get('token') || null)
29+
const givenServerPort = Number.parseInt(params.get('port') || '')
30+
if (givenServerPort > 0 && givenServerPort < 65535) {
31+
servicePort.value = givenServerPort
32+
requireFindService = false
33+
}
2834

2935
const initialLoad = new Promise<void>((resolve) => {
3036
useIntervalFn(

frontend/src/views/AppManagement.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ onMounted(() => {
9393
</p>
9494
</div>
9595
<div :class="{ 'opacity-0 pointer-events-none select-none': !app.granted }" class="mt-4 transition-opacity duration-300">
96-
<ProviderSelector v-model="app.provider" compact @update:model-value="appStore.toggleAppAuthorization(app.id, true, app.provider)" />
96+
<ProviderSelector v-model="app.provider" compact @update:model-value="app.granted && appStore.toggleAppAuthorization(app.id, app.granted, app.provider)" />
9797
</div>
9898
</CardContent>
9999
</Card>

sdk/node/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export async function requestUniTokenOpenAI(options: UniTokenOptions): Promise<U
143143
const abortController = new AbortController()
144144
setTimeout(() => {
145145
abortController.abort()
146-
}, 10000) // 10 seconds timeout
146+
}, 100000) // 100 seconds timeout
147147

148148
const response = await fetch(`${serverUrl}app/register`, {
149149
method: 'POST',

service/logic/open.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package logic
22

33
import (
4+
"fmt"
45
"net/url"
6+
"strconv"
7+
"time"
58

69
"uni-token-service/constants"
710
"uni-token-service/logic/open_browser"
811
"uni-token-service/store"
12+
13+
"github.com/google/uuid"
914
)
1015

16+
var openingUI = make(map[string](chan struct{}))
17+
var ServerPort = -1
18+
1119
func OpenUI(params url.Values, auth bool) error {
1220
if auth {
1321
allUsers, err := store.Users.List()
@@ -30,5 +38,32 @@ func OpenUI(params url.Values, auth bool) error {
3038
params.Set("token", token)
3139
}
3240

33-
return openBrowser.OpenBrowser(constants.GetUserName(), constants.GetAppBaseUrl()+"?"+params.Encode())
41+
sessionId := uuid.New().String()
42+
params.Set("session", sessionId)
43+
params.Set("port", strconv.Itoa(ServerPort))
44+
45+
err := openBrowser.OpenBrowser(constants.GetUserName(), constants.GetAppBaseUrl()+"?"+params.Encode())
46+
if err != nil {
47+
return err
48+
}
49+
50+
channel := make(chan struct{})
51+
openingUI[sessionId] = channel
52+
defer delete(openingUI, sessionId)
53+
54+
select {
55+
case <-channel:
56+
return nil
57+
case <-time.After(5 * time.Second):
58+
return fmt.Errorf("failed to open UI: timeout")
59+
}
60+
}
61+
62+
func OnUIOpened(sessionId string) {
63+
channel, ok := openingUI[sessionId]
64+
if !ok {
65+
return
66+
}
67+
channel <- struct{}{}
68+
close(channel)
3469
}

service/server/action.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212

1313
func SetupActionAPI(router *gin.Engine) {
1414
router.GET("/", handleCheck)
15-
router.GET("/open", handleOpenUrl)
15+
router.GET("/ui/open", handleOpenUI)
16+
router.POST("/ui/opened", handleUIOpened)
1617
}
1718

1819
func handleCheck(c *gin.Context) {
@@ -22,11 +23,24 @@ func handleCheck(c *gin.Context) {
2223
})
2324
}
2425

25-
func handleOpenUrl(c *gin.Context) {
26+
func handleOpenUI(c *gin.Context) {
2627
err := logic.OpenUI(url.Values{}, true)
2728

2829
if err != nil {
2930
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to open UI"})
3031
return
3132
}
3233
}
34+
35+
func handleUIOpened(c *gin.Context) {
36+
var req struct {
37+
Session string `json:"session"`
38+
}
39+
if err := c.ShouldBindJSON(&req); err != nil {
40+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
41+
return
42+
}
43+
44+
logic.OnUIOpened(req.Session)
45+
c.Status(http.StatusOK)
46+
}

service/server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func SetupAPIServer() (int, error) {
4242

4343
// Get a random available port
4444
port := getPort()
45+
logic.ServerPort = port
4546

4647
// Start server in a goroutine
4748
go func() {

0 commit comments

Comments
 (0)