File size: 3,350 Bytes
04f192c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | <!--
SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
SPDX-License-Identifier: Apache-2.0
-->
<script setup lang="ts">
import { ref, watch } from "vue";
import { setApiBaseUrl, testApiConnection } from "../apis/base";
import { getServerConfig } from "../apis";
import type { ServerConfig } from "../apis";
const props = withDefaults(
defineProps<{
apiBaseUrl: string;
/** Initial status; the component owns status afterwards and emits updates. */
connectionStatus?: "idle" | "testing" | "ok" | "fail";
/** Settings: persist + reload the page on a successful connect. Wizard: fetch config instead. */
reloadOnConnect?: boolean;
/** Show the inline Connect/Save button. Hidden in Settings (the pinned footer triggers connect); shown in the wizard. */
showConnectButton?: boolean;
}>(),
{ connectionStatus: "idle", reloadOnConnect: false, showConnectButton: true },
);
const emit = defineEmits<{
(e: "update:apiBaseUrl", value: string): void;
(e: "update:connectionStatus", value: "idle" | "testing" | "ok" | "fail"): void;
(e: "update:serverConfig", value: ServerConfig | null): void;
}>();
const apiBaseUrl = ref(props.apiBaseUrl);
const status = ref<"idle" | "testing" | "ok" | "fail">(props.connectionStatus);
// Sync from prop (fixes pre-fill when parent sets value async)
watch(
() => props.apiBaseUrl,
(val) => {
apiBaseUrl.value = val;
},
);
function setStatus(value: "idle" | "testing" | "ok" | "fail") {
status.value = value;
emit("update:connectionStatus", value);
}
async function connect() {
setStatus("testing");
const ok = await testApiConnection(apiBaseUrl.value);
setStatus(ok ? "ok" : "fail");
if (!ok) return;
emit("update:apiBaseUrl", apiBaseUrl.value);
try {
setApiBaseUrl(apiBaseUrl.value);
} catch {
// ignore persistence errors
}
// Settings changes the backend mid-session: reload to re-initialise.
// Wizard keeps going: fetch the server config so later steps can pre-fill.
if (props.reloadOnConnect) {
window.location.reload();
return;
}
try {
const config = await getServerConfig();
emit("update:serverConfig", config);
} catch {
// Server may not return config
}
}
function getValue() {
return apiBaseUrl.value;
}
defineExpose({ getValue, connect });
</script>
<template>
<div class="flex flex-col gap-6">
<p class="text-sm text-slate-500 dark:text-slate-400">Enter the URL of your CopilotJ server to get started.</p>
<FormItem for="apiBaseUrl" label="Server URL" required>
<div class="flex items-center gap-2">
<InputText
type="text"
v-model="apiBaseUrl"
inputId="apiBaseUrl"
placeholder="http://localhost:8786"
class="w-full"
@keyup.enter="connect"
/>
<Button
v-if="showConnectButton"
:label="reloadOnConnect ? 'Save' : 'Connect'"
:loading="status === 'testing'"
@click="connect"
/>
</div>
<p v-if="status === 'ok'" class="text-sm text-green-600 dark:text-green-400 mt-1">Connected successfully</p>
<p v-else-if="status === 'fail'" class="text-sm text-red-600 dark:text-red-400 mt-1">
Could not reach the server. Please check the URL and try again.
</p>
</FormItem>
</div>
</template>
|