generated from include-davis/Next.js-App-Router-Starter
-
Notifications
You must be signed in to change notification settings - Fork 2
1. HackBot server core #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ReehalS
wants to merge
4
commits into
main
Choose a base branch
from
hackbot-server-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2826a71
Add HackBot server core: API route, actions, utils, types, data, and …
ReehalS 2ddd6eb
Merge branch 'main' of https://github.com/HackDavis/hackdavis-hub int…
michelleyeoh 781febb
clean up files
michelleyeoh e8bdd9f
fix build error
michelleyeoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| 'use server'; | ||
|
|
||
| import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
|
|
||
| export interface ClearKnowledgeDocsResult { | ||
| ok: boolean; | ||
| deletedKnowledge: number; | ||
| deletedEmbeddings: number; | ||
| error?: string; | ||
| } | ||
|
|
||
| export default async function clearKnowledgeDocs(): Promise<ClearKnowledgeDocsResult> { | ||
| try { | ||
| const db = await getDatabase(); | ||
|
|
||
| const knowledgeResult = await db | ||
| .collection('hackbot_knowledge') | ||
| .deleteMany({}); | ||
|
|
||
| const embeddingsResult = await db | ||
| .collection('hackbot_docs') | ||
| .deleteMany({ _id: { $regex: '^knowledge-' } }); | ||
|
|
||
| return { | ||
| ok: true, | ||
| deletedKnowledge: knowledgeResult.deletedCount, | ||
| deletedEmbeddings: embeddingsResult.deletedCount, | ||
| }; | ||
| } catch (e) { | ||
| const msg = e instanceof Error ? e.message : 'Unknown error'; | ||
| console.error('[clearKnowledgeDocs] Error:', msg); | ||
| return { | ||
| ok: false, | ||
| deletedKnowledge: 0, | ||
| deletedEmbeddings: 0, | ||
| error: msg, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 'use server'; | ||
|
|
||
| import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
| import { ObjectId } from 'mongodb'; | ||
|
|
||
| export interface DeleteKnowledgeDocResult { | ||
| ok: boolean; | ||
| error?: string; | ||
| } | ||
|
|
||
| export default async function deleteKnowledgeDoc( | ||
| id: string | ||
| ): Promise<DeleteKnowledgeDocResult> { | ||
| try { | ||
| const db = await getDatabase(); | ||
| const objectId = new ObjectId(id); | ||
|
|
||
| await db.collection('hackbot_knowledge').deleteOne({ _id: objectId }); | ||
| await db.collection('hackbot_docs').deleteOne({ _id: `knowledge-${id}` }); | ||
|
|
||
| return { ok: true }; | ||
| } catch (e) { | ||
| console.error('[deleteKnowledgeDoc] Error', e); | ||
| return { | ||
| ok: false, | ||
| error: e instanceof Error ? e.message : 'Failed to delete document', | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 'use server'; | ||
|
|
||
| import { auth } from '@/auth'; | ||
| import type { HackerProfile } from '@typeDefs/hackbot'; | ||
|
|
||
| export type { HackerProfile }; | ||
|
|
||
| export async function getHackerProfile(): Promise<HackerProfile | null> { | ||
| const session = await auth(); | ||
| if (!session?.user) return null; | ||
| const user = session.user as any; | ||
| return { | ||
| name: user.name ?? undefined, | ||
| position: user.position ?? undefined, | ||
| is_beginner: user.is_beginner ?? undefined, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 'use server'; | ||
|
|
||
| import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
| import { HackDocType } from '@typeDefs/hackbot'; | ||
|
|
||
| export interface KnowledgeDoc { | ||
| id: string; | ||
| type: HackDocType; | ||
| title: string; | ||
| content: string; | ||
| url: string | null; | ||
| createdAt: string; | ||
| updatedAt: string; | ||
| } | ||
|
|
||
| export interface GetKnowledgeDocsResult { | ||
| ok: boolean; | ||
| docs: KnowledgeDoc[]; | ||
| error?: string; | ||
| } | ||
|
|
||
| export default async function getKnowledgeDocs(): Promise<GetKnowledgeDocsResult> { | ||
| try { | ||
| const db = await getDatabase(); | ||
| const raw = await db | ||
| .collection('hackbot_knowledge') | ||
| .find({}) | ||
| .sort({ updatedAt: -1 }) | ||
| .toArray(); | ||
|
|
||
| const docs: KnowledgeDoc[] = raw.map((d: any) => ({ | ||
| id: String(d._id), | ||
| type: d.type, | ||
| title: d.title, | ||
| content: d.content, | ||
| url: d.url ?? null, | ||
| createdAt: d.createdAt?.toISOString?.() ?? new Date().toISOString(), | ||
| updatedAt: d.updatedAt?.toISOString?.() ?? new Date().toISOString(), | ||
| })); | ||
|
|
||
| return { ok: true, docs }; | ||
| } catch (e) { | ||
| console.error('[getKnowledgeDocs] Error', e); | ||
| return { | ||
| ok: false, | ||
| docs: [], | ||
| error: e instanceof Error ? e.message : 'Failed to load knowledge docs', | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 'use server'; | ||
|
|
||
| import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
|
|
||
| export type UsagePeriod = '24h' | '7d' | '30d'; | ||
|
|
||
| export interface UsageMetrics { | ||
| totalRequests: number; | ||
| totalPromptTokens: number; | ||
| totalCompletionTokens: number; | ||
| totalCachedTokens: number; | ||
| /** 0–1 fraction of prompt tokens that were served from cache */ | ||
| cacheHitRate: number; | ||
| } | ||
|
|
||
| export async function getUsageMetrics( | ||
| period: UsagePeriod = '24h' | ||
| ): Promise<UsageMetrics> { | ||
| const hours = period === '24h' ? 24 : period === '7d' ? 168 : 720; | ||
| const since = new Date(Date.now() - hours * 60 * 60 * 1000); | ||
|
|
||
| const db = await getDatabase(); | ||
| const [result] = await db | ||
| .collection('hackbot_usage') | ||
| .aggregate([ | ||
| { $match: { timestamp: { $gte: since } } }, | ||
| { | ||
| $group: { | ||
| _id: null, | ||
| totalRequests: { $sum: 1 }, | ||
| totalPromptTokens: { $sum: '$promptTokens' }, | ||
| totalCompletionTokens: { $sum: '$completionTokens' }, | ||
| totalCachedTokens: { $sum: '$cachedPromptTokens' }, | ||
| }, | ||
| }, | ||
| ]) | ||
| .toArray(); | ||
|
|
||
| if (!result) { | ||
| return { | ||
| totalRequests: 0, | ||
| totalPromptTokens: 0, | ||
| totalCompletionTokens: 0, | ||
| totalCachedTokens: 0, | ||
| cacheHitRate: 0, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| totalRequests: result.totalRequests, | ||
| totalPromptTokens: result.totalPromptTokens, | ||
| totalCompletionTokens: result.totalCompletionTokens, | ||
| totalCachedTokens: result.totalCachedTokens, | ||
| cacheHitRate: | ||
| result.totalPromptTokens > 0 | ||
| ? result.totalCachedTokens / result.totalPromptTokens | ||
| : 0, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When mapping DB docs, missing
createdAt/updatedAtare replaced withnew Date().toISOString(), which can misrepresent the true timestamps and make the UI look like docs were just updated. Prefer returningnull/omitting the field, or explicitly indicating the timestamp is unknown.