Skip to content

Commit

Permalink
Merge pull request #206 from iceljc/features/refine-types
Browse files Browse the repository at this point in the history
Features/refine types
  • Loading branch information
iceljc authored Sep 5, 2024
2 parents d3a4f9a + 7b00ee5 commit 96f1398
Show file tree
Hide file tree
Showing 73 changed files with 508 additions and 477 deletions.
2 changes: 1 addition & 1 deletion src/lib/common/AudioGallery.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import AudioPlayer from './audio-player/AudioPlayer.svelte';
/** @type {import('$types').AudioFileModel[]} */
/** @type {import('$fileTypes').AudioFileModel[]} */
export let audios = [];
/** @type {string} */
Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/FileGallery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { AUDIO_ICON, EXCEL_ICON, PDF_ICON, isAudio, isExcel, isPdf } from '$lib/helpers/utils/file';
import { LightboxGallery, GalleryThumbnail, GalleryImage } from 'svelte-lightbox';
/** @type {import('$types').TextFileModel[]} */
/** @type {import('$fileTypes').TextFileModel[]} */
export let files = [];
/** @type {boolean} */
Expand Down
4 changes: 2 additions & 2 deletions src/lib/common/MessageFileGallery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
/** @type {() => Promise<any>} */
export let fetchFiles = () => Promise.resolve([]);
/** @type {import('$types').TextFileModel[]} */
/** @type {import('$fileTypes').TextFileModel[]} */
let textFiles = [];
/** @type {import('$types').AudioFileModel[]} */
/** @type {import('$fileTypes').AudioFileModel[]} */
let audioFiles = [];
onMount(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/PlainPagination.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Row,
} from '@sveltestrap/sveltestrap';
/** @type {import('$types').Pagination} */
/** @type {import('$commonTypes').Pagination} */
export let pagination = { page: 1, size: 10, count: 0 };
/** @type {(pageNum: number) => void} */
export let pageTo;
Expand Down
18 changes: 8 additions & 10 deletions src/lib/common/StateModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
export let validateValue = true;
export let requireActiveRounds = false;
/** @type {import('$types').UserStateDetailModel[]} */
/** @type {import('$conversationTypes').UserStateDetailModel[]} */
export let states = [];
/** @type {import('$types').UserStateDetailModel} */
/** @type {import('$conversationTypes').UserStateDetailModel} */
const defaultState = {
key: { data: '', isValid: true },
value: { data: '', isValid: true },
Expand Down Expand Up @@ -196,15 +196,13 @@
</FormGroup>
</div>
{/if}
<div class="state-delete">
<div class="state-delete mb-3 line-align-center">
{#if idx !== 0}
<Button
class="btn btn-sm btn-rounded"
color="danger"
on:click={() => remove(idx)}
>
<i class="mdi mdi-window-close"></i>
</Button>
<div>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<span><i class="bx bx-no-entry clickable" on:click={() => remove(idx)} /></span>
</div>
{/if}
</div>
</Row>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/TablePagination.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Link from 'svelte-link';
import { _ } from 'svelte-i18n'
/** @type {import('$types').Pagination} */
/** @type {import('$commonTypes').Pagination} */
export let pagination;
/** @type {(pageNum: number) => void} */
export let pageTo;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/audio-player/AudioPlayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
wtBufTime,
} = useAudioStore(dispatch);
/** @type {import('$types').AudioFileModel[]} */
/** @type {import('$fileTypes').AudioFileModel[]} */
export let audio;
/** @type {string} */
Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/audio-player/AudioSpeaker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/** @type {boolean} */
let speaking = false;
/** @type {import('$types').SpeechModel} */
/** @type {import('$audioTypes').SpeechModel} */
let speech;
onMount(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/common/audio-player/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { derived, writable } from "svelte/store";
import { secondToTime } from "./utils";
import { SPEECH_VOICES } from "$lib/services/web-speech";

/** @type {import('$types').AudioModel[]} */
/** @type {import('$audioTypes').AudioModel[]} */
export const audioInstances = [];

/** @type {import('$types').SpeechModel[]} */
/** @type {import('$audioTypes').SpeechModel[]} */
export const speechInstances = [];

/**
* @param {import('$types').AudioModel} audio
* @param {import('$audioTypes').AudioModel} audio
* @param {(name: string, detail?: any) => void} dispatch
*/
export function initAudio(audio, dispatch) {
audioInstances.push(audio);
bindAudioEvent(audio.player, dispatch);
}

/** @param {import('$types').SpeechModel} speech */
/** @param {import('$audioTypes').SpeechModel} speech */
export function initSpeech(speech) {
const foundVoice = speech.synth.getVoices().find(x => SPEECH_VOICES.includes(x.name));
if (foundVoice) {
Expand Down
41 changes: 27 additions & 14 deletions src/lib/helpers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ const conversationKey = "conversation";
const conversationUserStatesKey = "conversation_user_states";
const conversationSearchOptionKey = "conversation_search_option";
const conversationUserMessageKey = "conversation_user_messages";
const conversationUserAttachmentKey = "conversation_user_attachments";

/** @type {Writable<import('$types').UserModel>} */
/** @type {Writable<import('$userTypes').UserModel>} */
export const userStore = writable({ id: "", full_name: "", expires: 0, token: null });

/**
* @returns {Writable<import('$types').UserModel>}
* @returns {Writable<import('$userTypes').UserModel>}
*/
export function getUserStore() {
if (browser) {
Expand All @@ -35,11 +34,11 @@ userStore.subscribe(value => {
});


/** @type {Writable<import('$types').ConversationModel>}*/
/** @type {Writable<import('$conversationTypes').ConversationModel>}*/
export const conversationStore = writable({});

/**
* @returns {Writable<import('$types').ConversationModel>}
* @returns {Writable<import('$conversationTypes').ConversationModel>}
*/
export function getConversationStore() {
if (browser) {
Expand All @@ -59,10 +58,6 @@ export function getConversationStore() {
conversationStore.subscribe(value => {
if (browser && value.id) {
localStorage.setItem(conversationKey, JSON.stringify(value));
const state = conversationUserStateStore.get();
if (state && state.conversationId != value.id) {
conversationUserStateStore.reset();
}
}
});

Expand All @@ -81,21 +76,39 @@ export const loaderStore = createLoaderStore();

const createConversationUserStateStore = () => {
return {
reset: () => {
resetAll: () => {
localStorage.removeItem(conversationUserStatesKey);
},
get: () => {
resetOne: (conversationId) => {
const json = localStorage.getItem(conversationUserStatesKey);
return json ? JSON.parse(json) : {};
const content = json ? JSON.parse(json) : {};
const data = content?.data || [];
const found = data.find(x => x.conversationId === conversationId);
if (!found) return;

const updated = data.filter(x => x.conversationId !== conversationId);
localStorage.setItem(conversationUserStatesKey, JSON.stringify({ data: updated }));
},
get: (conversationId) => {
const json = localStorage.getItem(conversationUserStatesKey);
const content = json ? JSON.parse(json) : {};
const found = content?.data?.find(x => x.conversationId === conversationId);
return found || {};
},
put: (value) => {
localStorage.setItem(conversationUserStatesKey, JSON.stringify(value));
const conversationId = value?.conversationId;
const json = localStorage.getItem(conversationUserStatesKey);
const content = json ? JSON.parse(json) : {};
const cur = content?.data?.filter(x => x.conversationId !== conversationId) || [];
const updated = [ ...cur, { ...value } ];
localStorage.setItem(conversationUserStatesKey, JSON.stringify({ data: updated }));
}
}
};

export const conversationUserStateStore = createConversationUserStateStore();


const createConversationSearchOptionStore = () => {
return {
reset: () => {
Expand Down Expand Up @@ -151,7 +164,7 @@ export const conversationUserAttachmentStore = createConversationUserAttachmentS


export function resetLocalStorage(resetUser = false) {
conversationUserStateStore.reset();
conversationUserStateStore.resetAll();
conversationSearchOptionStore.reset();
conversationUserMessageStore.reset();
conversationUserAttachmentStore.reset();
Expand Down
116 changes: 116 additions & 0 deletions src/lib/helpers/types/agentTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @typedef {Object} AgentWelcomeInfo
* @property {string[]} messages - The welcome messages in Rich content format.
*/

/**
* @typedef {Object} AgentTemplate
* @property {string} name
* @property {string} content
*/

/**
* @typedef {Object} AgentLlmConfig
* @property {boolean} is_inherit - Inherited from default Agent settings
* @property {string?} provider
* @property {string?} model
* @property {number} max_recursion_depth
*/


/**
* @typedef {Object} RouterSettings
* @property {string} planner
*/


/**
* @typedef {Object} AgentFilter
* @property {import('$commonTypes').Pagination} pager - Pagination
* @property {string} [type]
* @property {boolean} [isPublic]
* @property {boolean} [disabled]
* @property {string[]} [agentIds]
*/

/**
* @typedef {Object} AgentModel
* @property {string} id - Agent Id.
* @property {string} name - Agent name.
* @property {string} description - Agent description.
* @property {string} type - Agent type
* @property {string} instruction - System prompt
* @property {ChannelInstruction[]} channel_instructions - Channel instructions
* @property {boolean} disabled
* @property {boolean} is_public
* @property {boolean} is_host
* @property {boolean} allow_routing
* @property {string} icon_url - Icon
* @property {string[]} profiles - The agent profiles.
* @property {string[]} utilities - The agent utilities.
* @property {Date} created_datetime
* @property {Date} updated_datetime
* @property {AgentLlmConfig} llm_config - LLM settings.
* @property {import('$pluginTypes').PluginDefModel} plugin
* @property {FunctionDef[]} functions
* @property {AgentTemplate[]} templates
* @property {Object[]} responses
* @property {RoutingRule[]} routing_rules
* @property {AgentWelcomeInfo} welcome_info - Welcome information.
* @property {boolean} editable
*/


/**
* @typedef {Object} AgentSettings
* @property {string} dataDir
* @property {string} templateFormat
* @property {AgentLlmConfig} llmConfig - LLM settings.
*/

/**
* @typedef {Object} AgentTaskFilter
* @property {import('$commonTypes').Pagination} pager - Pagination
* @property {string} [agentId] - The agent id.
*/

/**
* @typedef {Object} AgentTaskModel
* @property {string} id - Task id.
* @property {string} name - Task name.
* @property {string} description - Description.
* @property {string} content - Task detail.
* @property {boolean} enabled
* @property {Date} created_datetime
* @property {Date} updated_datetime
* @property {string} agent_id - Description.
* @property {string} agent_name - Task detail.
* @property {string} [direct_agent_id] - Run task directly in this agent.
*/

/**
* @typedef {Object} ChannelInstruction
* @property {string} channel
* @property {string} instruction
*/

/**
* @typedef {Object} RoutingRule
* @property {string} type
* @property {string} field
* @property {string} description
* @property {string} fieldType
* @property {boolean} required
* @property {string} redirectTo
* @property {string?} [redirect_to_agent]
*/


/**
* @typedef {Object} FunctionDef
* @property {string} name
* @property {string} description
*/


export default {};
18 changes: 18 additions & 0 deletions src/lib/helpers/types/audioTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Speech
/**
* @typedef {Object} SpeechModel
* @property {string} id
* @property {SpeechSynthesis} synth
* @property {SpeechSynthesisUtterance} utterThis
* @property {() => void} stop
* @property {() => boolean} isPlaying
*/

/**
* @typedef {Object} AudioModel
* @property {string} id
* @property {HTMLAudioElement} player
* @property {() => void} stop
*/

export default {};
34 changes: 34 additions & 0 deletions src/lib/helpers/types/commonTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @typedef {Object} Pagination
* @property {number} page - The plugin full name.
* @property {number} size - The plugin name.
* @property {number} count - Row count.
*/

/**
* @typedef {Object} KeyValuePair
* @property {string} key - The key.
* @property {string} value - The value.
*/

/**
* @typedef {Object} IdName
* @property {string} id - The id.
* @property {string} name - The name.
*/

/**
* @template T
* @typedef {Object} PagedItems<T>
* @property {number} count - Row count.
* @property {T[]} items - Items.
*/

/**
* @typedef {Object} LlmModelSetting
* @property {string} name
* @property {string} type
*/


export default {};
Loading

0 comments on commit 96f1398

Please sign in to comment.