Skip to content

Commit

Permalink
Merge pull request #11 from hinagiku-dev/fix
Browse files Browse the repository at this point in the history
Fix
  • Loading branch information
JacobLinCool authored Nov 26, 2024
2 parents dd6c049 + d63eb3e commit 7206796
Show file tree
Hide file tree
Showing 36 changed files with 958 additions and 736 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ CLOUDFLARE_PUBLIC_URL="https://hinagiku-dev-storage.csie.cool"
GOOGLE_APPLICATION_CREDENTIALS="service-account-file.example.json"
HUGGINGFACE_TOKEN="hf_xxx"
OPENAI_BASE_URL="https://api.openai.com/v1"
OPENAI_API_KEY=""
2 changes: 1 addition & 1 deletion src/lib/components/Auth.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{#if $user}
<div>
<p class="inline-block">Welcome, {$profile?.displayName ?? $user.displayName}!</p>
<p class="inline-block">Welcome, {$profile?.displayName || $user.displayName}!</p>
<button class="rounded-lg" on:click={signOut}>Sign Out</button>
</div>
{:else}
Expand Down
22 changes: 11 additions & 11 deletions src/lib/firebase/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { writable, type Readable } from 'svelte/store';

const log = debug('app:store');

export interface DocumentStore<T> extends Readable<T> {
export type DocumentStore<T> = [Readable<T | null>, {
unsubscribe: () => void;
}
}];

export function subscribeAll<T = unknown>(
ref: Query,
store = writable<T[]>([])
): DocumentStore<T[]> {
store = writable<[string, T][]>([])
): DocumentStore<[string, T][]> {
log('subscribe', ref);

const unsubscribe = onSnapshot(
ref,
(snapshot) => {
const data = snapshot.docs.map((doc) => doc.data() as T);
const data = snapshot.docs.map((doc) => [doc.id, doc.data()] as [string, T]);
log('onSnapshot', ref, data);
store.set(data);
},
Expand All @@ -26,18 +26,18 @@ export function subscribeAll<T = unknown>(
}
);

return Object.assign(store, {
return [store, {
unsubscribe: () => {
log('unsubscribe', ref);
unsubscribe();
}
});
}];
}

export function subscribe<T = unknown>(
ref: DocumentReference,
store = writable<T | null>(null)
): DocumentStore<T | null> {
): DocumentStore<T> {
log('subscribe', ref.path);

const unsubscribe = onSnapshot(
Expand All @@ -52,10 +52,10 @@ export function subscribe<T = unknown>(
}
);

return Object.assign(store, {
return [store, {
unsubscribe: () => {
log('unsubscribe', ref.path);
log('unsubscribe', ref);
unsubscribe();
}
});
}];
}
9 changes: 9 additions & 0 deletions src/lib/schema/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from 'zod';
import { Timestamp } from 'firebase/firestore';

export const CodeSchema = z.object({
target: z.string().min(1),
exp: z.instanceof(Timestamp),
});

export type Code = z.infer<typeof CodeSchema>;
12 changes: 12 additions & 0 deletions src/lib/schema/conversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from 'zod';

export const ConversationSchema = z.object({
history: z.array(
z.object({
role: z.enum(['system', 'user', 'assistant']),
content: z.string(),
})
),
});

export type Conversation = z.infer<typeof ConversationSchema>;
15 changes: 15 additions & 0 deletions src/lib/schema/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod';

export const GroupSchema = z.object({
concept: z.string().min(1),
discussions: z.array(
z.object({
content: z.string(),
id: z.string().nullable(),
})
),
summary: z.string().nullable(),
keywords: z.record(z.string(), z.number()),
});

export type Group = z.infer<typeof GroupSchema>;
12 changes: 12 additions & 0 deletions src/lib/schema/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Timestamp } from 'firebase/firestore';
import { z } from 'zod';

export const ProfileSchema = z.object({
uid: z.string(),
displayName: z.string(),
title: z.string().nullable(),
bio: z.string().nullable(),
updatedAt: z.instanceof(Timestamp)
});

export type Profile = z.infer<typeof ProfileSchema>;
27 changes: 27 additions & 0 deletions src/lib/schema/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { z } from 'zod';
import { Timestamp } from 'firebase/firestore';

export const SessionSchema = z.object({
title: z.string().min(1).max(200),
status: z.enum(['draft', 'waiting', 'active', 'ended']),
host: z.string(),
participants: z.array(z.string()),
group: z.record(z.string(), z.string()),
resources: z.array(
z.object({
name: z.string(),
content: z.string().min(1),
createdAt: z.instanceof(Timestamp),
id: z.string().nullable(),
})
).max(10),
task: z.string().min(1).max(200),
subtasks: z.array(z.string().min(1).max(200)).max(10),
timing: z.object({
self: z.number().int().min(1),
group: z.number().int().min(1),
}),
createdAt: z.instanceof(Timestamp),
});

export type Session = z.infer<typeof SessionSchema>;
17 changes: 9 additions & 8 deletions src/lib/server/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ interface ChatMessage {
content: string;
}

interface Document {
title?: string;
content: string;
}

interface Student_opinion {
student_id: string;
student_conclusions: string;
Expand Down Expand Up @@ -55,20 +50,24 @@ export async function chatWithLLMByDocs(
messages: ChatMessage[],
mainQuestion: string,
secondaryGoal: string[],
documents: Document[],
documents: {
name: string;
text: string;
}[],
temperature = 0.7
) {
try {
if (await isHarmfulContent(messages[messages.length - 1].content)) {
return {
success: false,
message: '',
error: 'Harmful content detected'
};
}
const formattedDocs = documents
.map((doc, index) => {
const title = doc.title || `Document ${index + 1}`;
return `[${title}]:\n${doc.content}`;
const title = doc.name || `Document ${index + 1}`;
return `[${title}]:\n${doc.text}`;
})
.join('\n\n');

Expand Down Expand Up @@ -105,11 +104,13 @@ export async function chatWithLLMByDocs(
if (error instanceof z.ZodError) {
return {
success: false,
message: '',
error: 'Type error: ' + error.errors.map((e) => e.message).join(', ')
};
}
return {
success: false,
message: '',
error: 'Failed to process documents and generate response'
};
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib/server/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function parsePdf2Text(filePath: string): Promise<string> {
// TODO: pares pdf file to text

return filePath;
}
15 changes: 4 additions & 11 deletions src/lib/stores/profile.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { db } from '$lib/firebase';
import { subscribe } from '$lib/firebase/store';
import { collection, doc, Timestamp } from 'firebase/firestore';
import { collection, doc } from 'firebase/firestore';
import { writable } from 'svelte/store';
import { z } from 'zod';
import { user } from './auth';
import type { ProfileSchema } from '$lib/schema/profile';

export const profileSchema = z.object({
uid: z.string(),
displayName: z.string(),
title: z.string().nullable(),
bio: z.string().nullable(),
updatedAt: z.instanceof(Timestamp)
});

export const profile = writable<z.infer<typeof profileSchema> | null>(null);
export const profile = writable<z.infer<typeof ProfileSchema> | null>(null);
let unsubscribe: () => void | undefined;

user.subscribe((user) => {
if (user) {
const ref = doc(collection(db, 'profiles'), user.uid);
unsubscribe = subscribe(ref, profile).unsubscribe;
unsubscribe = subscribe(ref, profile)[1].unsubscribe;
} else {
unsubscribe?.();
profile.set(null);
Expand Down
1 change: 1 addition & 0 deletions src/lib/stores/sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { writable } from 'svelte/store';

export const sidebarOpen = writable(false);
47 changes: 0 additions & 47 deletions src/lib/types/IndividualDiscussion.ts

This file was deleted.

51 changes: 0 additions & 51 deletions src/lib/types/groupDiscussion.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/lib/types/resource.ts

This file was deleted.

Loading

0 comments on commit 7206796

Please sign in to comment.