From 5131b654e87e572859d1bfad39a27af3ea4258c2 Mon Sep 17 00:00:00 2001 From: Lakshay Arora Date: Wed, 30 Oct 2024 20:24:16 -0400 Subject: [PATCH] core --- .../app/api/voice2measurements/route.ts | 7 +-- apps/nextjs/core/routes/routes.ts | 7 +++ apps/nextjs/core/services/GPTService.ts | 18 ++++++++ apps/nextjs/core/services/ImageService.ts | 16 +++++++ apps/nextjs/core/store/SharedStore.ts | 18 ++++++++ apps/nextjs/core/types/types.ts | 43 +++++++++++++++++++ 6 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 apps/nextjs/core/routes/routes.ts create mode 100644 apps/nextjs/core/services/GPTService.ts create mode 100644 apps/nextjs/core/services/ImageService.ts create mode 100644 apps/nextjs/core/store/SharedStore.ts create mode 100644 apps/nextjs/core/types/types.ts diff --git a/apps/nextjs/app/api/voice2measurements/route.ts b/apps/nextjs/app/api/voice2measurements/route.ts index bbccdbd4..5fc295ea 100644 --- a/apps/nextjs/app/api/voice2measurements/route.ts +++ b/apps/nextjs/app/api/voice2measurements/route.ts @@ -7,14 +7,9 @@ export async function POST(request: NextRequest) { let { statement, utcDateTime, timeZoneOffset, text, previousStatements, previousQuestions } = await request.json(); if(!statement){statement = text;} - //TODO: replace previous statements properly try { - //const measurements = await text2measurements(statement, utcDateTime, timeZoneOffset); - //haveConversation - //input: statement, utcDateTime, timeZoneOffset, previousStatements) - //output: questionForUser, measurements const { questionForUser } = await haveConversation(statement, utcDateTime, timeZoneOffset, previousStatements, previousQuestions); - const measurements = text2measurements(statement, utcDateTime, timeZoneOffset); + text2measurements(statement, utcDateTime, timeZoneOffset); return NextResponse.json({ success: true, question:questionForUser }); } catch (error) { return handleError(error, "voice2measurements") diff --git a/apps/nextjs/core/routes/routes.ts b/apps/nextjs/core/routes/routes.ts new file mode 100644 index 00000000..d2c3af6b --- /dev/null +++ b/apps/nextjs/core/routes/routes.ts @@ -0,0 +1,7 @@ +enum Routes { + GetImage="https://www.googleapis.com/customsearch/v1?searchType=image&key={{GOOGLE_CUSTOM_SEARCH_KEY}}&cx={{GOOGLE_SEARCH_ENGINE_ID}}&q={{SEARCH_TERM}}", + GetGPTResponse="/api/chat" +} + +export default Routes; + \ No newline at end of file diff --git a/apps/nextjs/core/services/GPTService.ts b/apps/nextjs/core/services/GPTService.ts new file mode 100644 index 00000000..0ff5395b --- /dev/null +++ b/apps/nextjs/core/services/GPTService.ts @@ -0,0 +1,18 @@ +import Routes from "../routes/routes"; +import { Message } from "../types/types"; + +export async function SendQuery(conversation: Message[]) { + + const result = await fetch(Routes.GetGPTResponse,{ + method: "POST", + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + conversation + }) + }); + + const response: string = await result.text(); + return response; +} \ No newline at end of file diff --git a/apps/nextjs/core/services/ImageService.ts b/apps/nextjs/core/services/ImageService.ts new file mode 100644 index 00000000..48e2896d --- /dev/null +++ b/apps/nextjs/core/services/ImageService.ts @@ -0,0 +1,16 @@ +import { SearchResultItem } from "../types/types"; +import { GoogleSearchResult } from "../types/types"; + +export async function GetImage(url: string) { + + const result = await fetch(url, { + method: "GET" + }); + + const resultString: string = await result.text(); + + const googleSearchResult: GoogleSearchResult = JSON.parse(resultString); + const resultItem: SearchResultItem = googleSearchResult.items[0]; + + return resultItem.link; +} \ No newline at end of file diff --git a/apps/nextjs/core/store/SharedStore.ts b/apps/nextjs/core/store/SharedStore.ts new file mode 100644 index 00000000..55f66126 --- /dev/null +++ b/apps/nextjs/core/store/SharedStore.ts @@ -0,0 +1,18 @@ +import { create } from 'zustand'; +import { Message, SuggestionItem } from '../types/types'; + +interface SharedState { + + suggestions: SuggestionItem[], + response: string, + conversation: Message[] +} + +const useSharedStore = create((set)=>({ + + suggestions: [], + response: "", + conversation: [] +})); + +export default useSharedStore; \ No newline at end of file diff --git a/apps/nextjs/core/types/types.ts b/apps/nextjs/core/types/types.ts new file mode 100644 index 00000000..49288b9f --- /dev/null +++ b/apps/nextjs/core/types/types.ts @@ -0,0 +1,43 @@ +export interface Message { + + role: string; + content: string; +} + +export interface SuggestionItem { + + id: string; + placeName: string; + description: string; + searchResultItem?: SearchResultItem; +} + +export interface GoogleSearchResult { + + kind: string; + items: SearchResultItem[]; +} + +export interface SearchResultItem { + + kind: string; + title: string; + htmlTitle: string; + link: string; + displayLink: string; + snippet: string; + htmlSnippet: string; + mime: string; + fileFormat: string; + image: SearchResultItemImage; +} + +export interface SearchResultItemImage { + contextLink: string; + height: number; + width: number; + byteSize: number; + thumbnailLink: string; + thumbnailHeight: number; + thumbnailWidth: number; +} \ No newline at end of file