Skip to content

Commit c184441

Browse files
author
Jicheng Lu
committed
refine chat utils
1 parent dd6b973 commit c184441

10 files changed

Lines changed: 197 additions & 193 deletions

File tree

src/routes/chat/+page.svelte

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
<script>
22
import { onMount } from 'svelte';
3-
import { Container, Row, Col } from '@sveltestrap/sveltestrap';
43
import { getSettingDetail } from '$lib/services/setting-service';
5-
import { getAgents } from '$lib/services/agent-service.js'
4+
import { getAgents } from '$lib/services/agent-service.js';
65
76
/** @type {import('$agentTypes').AgentFilter} */
87
const filter = {
98
pager: { page: 1, size: 10, count: 0 }
109
};
1110
1211
/** @type {import('$agentTypes').AgentModel[]} */
13-
let agents = [];
14-
let agentId = 'undefined';
12+
let agents = $state([]);
13+
let agentId = $state('undefined');
1514
16-
onMount(async () => {
15+
onMount(async () => {
1716
const response = await getAgents(filter, true);
1817
agents = response?.items?.map(t => { return { ...t }; }) || [];
1918
const agentSettings = await getSettingDetail("Agent");
2019
// @ts-ignore
2120
agentId = agentSettings.hostAgentId;
22-
});
21+
});
2322
</script>
2423
25-
<Container fluid>
26-
<Row>
24+
<div class="container-fluid">
25+
<div class="row">
2726
<div class="col-12">
2827
<div style="margin-top: 10vh; margin-left:10vw;">
2928
{#each agents as agent}
@@ -34,27 +33,27 @@
3433
name="agents"
3534
id={agent.id}
3635
value={agent.id}
37-
checked = {agentId == agent.id}
38-
on:click={() => agentId = agent.id}
36+
checked={agentId == agent.id}
37+
onclick={() => agentId = agent.id}
3938
/>
4039
<label class="form-check-label" for={agent.id}>
4140
{agent.name}
42-
</label>
41+
</label>
4342
<div class="mx-4">{agent.description}</div>
4443
</div>
4544
{/each}
46-
</div>
45+
</div>
4746
</div>
48-
</Row>
49-
<Row class="text-center">
50-
<Col>
47+
</div>
48+
<div class="row text-center">
49+
<div class="col">
5150
<p class="section-subtitle text-muted text-center pt-4 font-secondary">Select a bot you want to start chatting with and click the Start button.</p>
5251
<div class="d-flex justify-content-center">
5352
<a href="chat/{agentId}" class="btn btn-primary">
5453
<i class="mdi mdi-chat"></i>
5554
<span>Start Conversation</span>
5655
</a>
5756
</div>
58-
</Col>
59-
</Row>
60-
</Container>
57+
</div>
58+
</div>
59+
</div>
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
<script>
22
// This page is used to initialize a new conversation for client
3-
import { Container, Row, Col } from '@sveltestrap/sveltestrap';
4-
import { page } from '$app/stores';
3+
import { page } from '$app/state';
54
import { goto } from '$app/navigation';
65
import { onMount } from 'svelte';
76
import { newConversation } from '$lib/services/conversation-service.js';
8-
import { getToken, setToken } from '$lib/services/auth-service.js'
7+
import { getToken, setToken } from '$lib/services/auth-service.js';
98
import { getUserStore } from '$lib/helpers/store.js';
109
import { conversationStore } from '$lib/helpers/store.js';
11-
import { LEARNER_AGENT_ID, TRAINING_MODE } from '$lib/helpers/constants';
10+
import { LEARNER_AGENT_ID, TRAINING_MODE } from '$lib/helpers/constants';
1211
13-
const params = $page.params;
14-
1512
/** @type {import('$conversationTypes').ConversationModel} */
16-
let conversation;
17-
let conversationId = "undefined";
13+
let conversation = $state(/** @type {any} */ (undefined));
14+
let conversationId = $state('undefined');
1815
19-
let agentId = params.agentId;
16+
let agentId = $derived(page.params.agentId);
2017
2118
onMount(async () => {
2219
let user = getUserStore();
23-
if ($page.url.searchParams.has('token')) {
24-
let token = $page.url.searchParams.get('token');
20+
if (page.url.searchParams.has('token')) {
21+
let token = page.url.searchParams.get('token');
2522
console.log("login as explicit token in query.");
26-
await setToken(token);
23+
await setToken(token || '');
2724
} else if (user.token) {
2825
console.log("login as existing account.");
2926
} else {
30-
await getToken("guest@gmail.com", "123456", () => {
27+
await getToken("guest@gmail.com", "123456", '', () => {
3128
console.log("login as guest.");
3229
}, () => {});
3330
}
3431
3532
conversation = conversationStore.get();
3633
if (!conversation.id || agentId != conversation.agent_id) {
37-
// new conversation
34+
// @ts-ignore
3835
conversation = await newConversation(agentId);
3936
conversationStore.put(conversation);
4037
}
@@ -53,14 +50,14 @@
5350
});
5451
</script>
5552
56-
<Container fluid>
57-
<Row class="text-center">
58-
<Col style="padding: 50px;">
53+
<div class="container-fluid">
54+
<div class="row text-center">
55+
<div class="col" style="padding: 50px;">
5956
<div class="spinner-grow text-primary m-1" role="status" style="padding: 50px;">
6057
<span class="sr-only">Loading...</span>
6158
</div>
6259
<h3>Initializing a conversation, wait a moment please...</h3>
6360
<a href={`chat/${agentId}/${conversationId}`}>Click here if the browser doesn't redirect correctly.</a>
64-
</Col>
65-
</Row>
66-
</Container>
61+
</div>
62+
</div>
63+
</div>

src/routes/chat/[agentId]/[conversationId]/agent-info/chat-agent-info.svelte

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<script>
22
import { directToAgentPage } from '$lib/helpers/utils/common';
33
4-
/** @type {import('$agentTypes').AgentModel} */
5-
export let agent;
4+
/**
5+
* @type {{
6+
* agent: import('$agentTypes').AgentModel
7+
* }}
8+
*/
9+
let { agent } = $props();
610
</script>
711

812
<div>
913
<div class="chat-agent-row">
10-
<!-- svelte-ignore a11y-click-events-have-key-events -->
11-
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
14+
<!-- svelte-ignore a11y_click_events_have_key_events -->
15+
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
1216
<h3
1317
class="text-primary fw-bold"
14-
on:click={() => directToAgentPage(agent?.id)}
18+
onclick={() => directToAgentPage(agent?.id)}
1519
>
1620
<span class="clickable">{agent?.name || ''}</span>
1721
</h3>
@@ -23,7 +27,7 @@
2327
</div>
2428
<div class="chat-agent-row">
2529
<div>
26-
<span>{agent?.llm_config?.provider || ''}{!!agent?.llm_config?.provider ? ',': ''} {agent?.llm_config?.model || ''}</span>
30+
<span>{agent?.llm_config?.provider || ''}{agent?.llm_config?.provider ? ',': ''} {agent?.llm_config?.model || ''}</span>
2731
</div>
2832
</div>
2933
<div class="chat-agent-row">

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,10 +2133,10 @@
21332133
<div class="chat-util-links">
21342134
<ChatBigMessage
21352135
disabled={isSendingMsg || isThinking || disableAction}
2136-
on:click={() => toggleBigMessageModal()}
2136+
onclick={() => toggleBigMessageModal()}
21372137
/>
21382138
{#if PUBLIC_LIVECHAT_FILES_ENABLED === 'true'}
2139-
<ChatUtil disabled={disableAction} on:click={() => loadChatUtils = true} />
2139+
<ChatUtil disabled={disableAction} onclick={() => loadChatUtils = true} />
21402140
{/if}
21412141
</div>
21422142
</div>

src/routes/chat/[agentId]/[conversationId]/chat-util/chat-attachment-options.svelte

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
<script>
2-
import { onDestroy, onMount } from "svelte";
2+
import { onMount } from "svelte";
33
import { conversationUserAttachmentStore } from "$lib/helpers/store";
44
5-
/** @type {any[] | undefined} */
6-
export let options;
7-
8-
/** @type {boolean} */
9-
export let disabled = false;
10-
11-
/** @type {(args0: string, args1: string) => any} */
12-
export let onConfirm = () => {};
5+
/**
6+
* @type {{
7+
* options?: any[],
8+
* disabled?: boolean,
9+
* onConfirm?: (title: string, payload: string) => any
10+
* }}
11+
*/
12+
let {
13+
options,
14+
disabled = false,
15+
onConfirm = () => {}
16+
} = $props();
1317
1418
/** @type {any[]} */
15-
let files = [];
19+
let files = $state([]);
1620
/** @type {any} */
17-
let confirmOption;
21+
let confirmOption = $state(undefined);
1822
/** @type {any} */
19-
let cancelOption;
23+
let cancelOption = $state(undefined);
2024
21-
const unsubscribe = conversationUserAttachmentStore.subscribe(value => {
22-
const savedAttachments = $conversationUserAttachmentStore;
23-
files = value.accepted_files?.length > 0 ? value.accepted_files : savedAttachments?.accepted_files || [];
25+
$effect(() => {
26+
const unsubscribe = conversationUserAttachmentStore.subscribe(value => {
27+
files = value?.accepted_files || [];
28+
});
29+
30+
return () => {
31+
unsubscribe();
32+
};
2433
});
2534
2635
onMount(() => {
2736
collectOptions(options);
28-
29-
});
30-
31-
onDestroy(() => {
32-
unsubscribe();
3337
});
3438
39+
/** @param {any[] | undefined} opts */
40+
function collectOptions(opts) {
41+
confirmOption = opts?.find(op => op.title?.toLowerCase()?.startsWith("yes"));
42+
cancelOption = opts?.find(op => op.title?.toLowerCase()?.startsWith("no"));
3543
36-
/** @param {any[] | undefined} options */
37-
function collectOptions(options) {
38-
confirmOption = options?.find(op => op.title?.toLowerCase()?.startsWith("yes"));
39-
cancelOption = options?.find(op => op.title?.toLowerCase()?.startsWith("no"));
40-
4144
if (!confirmOption) {
4245
confirmOption = {
4346
title: 'Yes, I have uploaded attachments.',
@@ -66,8 +69,8 @@
6669
* @param {string} title
6770
* @param {string} payload
6871
*/
69-
function innerConfirm(title, payload) {
70-
onConfirm && onConfirm(title, payload);
72+
function innerConfirm(title, payload) {
73+
onConfirm?.(title, payload);
7174
}
7275
</script>
7376
@@ -76,7 +79,7 @@
7679
<button
7780
class={`btn btn-sm m-1 ${confirmOption?.is_secondary ? 'btn-outline-secondary': 'btn-outline-primary'}`}
7881
disabled={disabled}
79-
on:click={(e) => handleClickOption(e, confirmOption)}
82+
onclick={(e) => handleClickOption(e, confirmOption)}
8083
>
8184
{confirmOption?.title}
8285
</button>
@@ -85,7 +88,7 @@
8588
<button
8689
class={`btn btn-sm m-1 ${cancelOption?.is_secondary ? 'btn-outline-secondary': 'btn-outline-primary'}`}
8790
disabled={disabled}
88-
on:click={(e) => handleClickOption(e, cancelOption)}
91+
onclick={(e) => handleClickOption(e, cancelOption)}
8992
>
9093
{cancelOption?.title}
9194
</button>

src/routes/chat/[agentId]/[conversationId]/chat-util/chat-big-message.svelte

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
<script>
2-
import { createEventDispatcher } from "svelte";
3-
4-
const svelteDispatch = createEventDispatcher();
5-
6-
/** @type {string} */
7-
export let containerClasses = "";
8-
9-
/** @type {string} */
10-
export let containerStyles = "";
11-
12-
/** @type {boolean} */
13-
export let disableDefaultStyles = false;
14-
15-
/** @type {boolean} */
16-
export let disabled = false;
2+
/**
3+
* @type {{
4+
* containerClasses?: string,
5+
* containerStyles?: string,
6+
* disableDefaultStyles?: boolean,
7+
* disabled?: boolean,
8+
* onclick?: () => void
9+
* }}
10+
*/
11+
let {
12+
containerClasses = '',
13+
containerStyles = '',
14+
disableDefaultStyles = false,
15+
disabled = false,
16+
onclick
17+
} = $props();
1718
1819
function clickIcon() {
1920
if (disabled) {
2021
return;
2122
}
22-
svelteDispatch("click")
23+
onclick?.();
2324
}
2425
</script>
2526
@@ -30,15 +31,15 @@
3031
<ul class="list-inline mb-0">
3132
<li class="list-inline-item">
3233
<span>
33-
<!-- svelte-ignore a11y-no-static-element-interactions -->
34-
<!-- svelte-ignore a11y-click-events-have-key-events -->
34+
<!-- svelte-ignore a11y_no_static_element_interactions -->
35+
<!-- svelte-ignore a11y_click_events_have_key_events -->
3536
<i
3637
class="bx bx-pencil clickable"
3738
data-bs-toggle="tooltip"
3839
data-bs-placement="top"
3940
title="Zoom in"
40-
on:click={() => clickIcon()}
41-
/>
41+
onclick={() => clickIcon()}
42+
></i>
4243
</span>
4344
</li>
4445
</ul>

0 commit comments

Comments
 (0)