main
reng 6 months ago
parent 1415a18b5c
commit cfffb8be98
  1. 32
      index.js
  2. 49
      package-lock.json
  3. 1
      package.json
  4. 2
      vite/index.html
  5. BIN
      vite/public/favicon.ico
  6. 1
      vite/public/vite.svg
  7. 47
      vite/src/App.jsx

@ -2,9 +2,7 @@ import OpenAI from "openai";
import express from "express";
import cors from "cors";
import { system_prompt } from "./system_prompt.js";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";
import { Client } from 'node-osc';
import { config } from "dotenv";
config(); // Load environment variables from .env file
@ -33,6 +31,15 @@ const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const osc_client = new Client('127.0.0.1', 8787);
osc_client.send('/test', 55555, (error) => {
if (error) {
console.error('Error sending OSC message:', error);
} else {
console.log('OSC message sent successfully');
}
});
// const response = await client.responses.create({
// model: "gpt-4.1",
// input: "Write a one-sentence bedtime story about a unicorn."
@ -48,6 +55,11 @@ app.use(cors());
app.post("/generate", async (req, res) => {
const { input } = req.body;
// console.log(input[input.length-1], 'input received');
// osc_client.send('/prompt', input[input.length-1]?.content[0]?.text, (err) => {
// console.log('OSC', err ? `Error: ${err}` : 'Success');
// });
// return;
try {
const response = await client.responses.create({
@ -74,8 +86,20 @@ app.post("/generate", async (req, res) => {
});
console.log("Generated response:", response);
const json=JSON.parse(response.output_text);
// send prompt to TD
osc_client.send('/prompt', json.prompt, (error) => {
if (error) {
console.error('Error sending OSC message:', error);
} else {
console.log('OSC message sent successfully');
}
});
res.json(json);
res.json(JSON.parse(response.output_text));
} catch (error) {
console.error("Error generating response:", error);
res.status(500).json({ error: "Failed to generate response" });

49
package-lock.json generated

@ -12,6 +12,7 @@
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"express": "^5.1.0",
"node-osc": "^9.1.5",
"openai": "^5.1.0",
"zod": "^3.25.50"
}
@ -28,6 +29,11 @@
"node": ">= 0.6"
}
},
"node_modules/binpack": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/binpack/-/binpack-0.1.0.tgz",
"integrity": "sha512-KcSrsGiIKgklTWweVb9XnZPWO1/rGSsK3fwR7VnbDPbLKPlkvSKd/ZrJ1W712r6HzH5u0fa/AZCftATO09x8Aw=="
},
"node_modules/body-parser": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.0.tgz",
@ -482,6 +488,17 @@
"node": ">= 0.6"
}
},
"node_modules/node-osc": {
"version": "9.1.5",
"resolved": "https://registry.npmjs.org/node-osc/-/node-osc-9.1.5.tgz",
"integrity": "sha512-m8f+VMhgrFmxORP6HAH2ISM4zA4yCoYlT9RFaESmX/2433LNHTv5fx3/StyCYXHlYV9aR9lDgXXdu9rMnnjXtg==",
"dependencies": {
"osc-min": "^1.1.1"
},
"engines": {
"node": "^18.17.0 || ^20.5.0 || >=22.0.0"
}
},
"node_modules/object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
@ -540,6 +557,17 @@
}
}
},
"node_modules/osc-min": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/osc-min/-/osc-min-1.1.2.tgz",
"integrity": "sha512-8DbiO8ME85R75stgNVCZtHxB9MNBBNcyy+isNBXrsFeinXGjwNAauvKVmGlfRas5VJWC/mhzIx7spR2gFvWxvg==",
"dependencies": {
"binpack": "~0"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
@ -820,6 +848,11 @@
"negotiator": "^1.0.0"
}
},
"binpack": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/binpack/-/binpack-0.1.0.tgz",
"integrity": "sha512-KcSrsGiIKgklTWweVb9XnZPWO1/rGSsK3fwR7VnbDPbLKPlkvSKd/ZrJ1W712r6HzH5u0fa/AZCftATO09x8Aw=="
},
"body-parser": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.0.tgz",
@ -1136,6 +1169,14 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz",
"integrity": "sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg=="
},
"node-osc": {
"version": "9.1.5",
"resolved": "https://registry.npmjs.org/node-osc/-/node-osc-9.1.5.tgz",
"integrity": "sha512-m8f+VMhgrFmxORP6HAH2ISM4zA4yCoYlT9RFaESmX/2433LNHTv5fx3/StyCYXHlYV9aR9lDgXXdu9rMnnjXtg==",
"requires": {
"osc-min": "^1.1.1"
}
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
@ -1168,6 +1209,14 @@
"integrity": "sha512-YQBgPJykHrDOlngB/8QpOsFNg36yofBatpeDWg1zejl9R59/ELuN7AMPSU95ZIdChbKc/o5vg1UnBJ1OEB0IJA==",
"requires": {}
},
"osc-min": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/osc-min/-/osc-min-1.1.2.tgz",
"integrity": "sha512-8DbiO8ME85R75stgNVCZtHxB9MNBBNcyy+isNBXrsFeinXGjwNAauvKVmGlfRas5VJWC/mhzIx7spR2gFvWxvg==",
"requires": {
"binpack": "~0"
}
},
"parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",

@ -17,6 +17,7 @@
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"express": "^5.1.0",
"node-osc": "^9.1.5",
"openai": "^5.1.0",
"zod": "^3.25.50"
}

@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>24070-Conversation</title>
</head>

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

@ -1,4 +1,4 @@
import { useRef, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import './App.css'
@ -10,13 +10,15 @@ function App() {
const [processing, setProcessing] = useState(false);
const [prompt, setPrompt] = useState([]);
const refHistoryContainer= useRef(null);
const refPrompContainer= useRef(null);
function onSubmit(event) {
event.preventDefault();
const input = event.target.elements.input.value;
console.log("Submitted:", input);
setProcessing(true);
// setProcessing(true);
fetch(`${BASE_URL}/generate`, {
@ -65,7 +67,7 @@ function App() {
// clear input
event.target.elements.input.value = '';
setProcessing(false);
setProcessing(false);
});
@ -73,33 +75,40 @@ function App() {
}
useEffect(()=>{
refHistoryContainer.current.scrollTop = refHistoryContainer.current.scrollHeight;
},[history]);
useEffect(()=>{
refPrompContainer.current.scrollTop = refPrompContainer.current.scrollHeight;
},[prompt]);
return (
<main className='min-h-screen flex flex-col gap-8 justify-end p-8'>
<div className='flex flex-col gap-2 border-4'>
<main className='h-screen flex flex-col gap-8 justify-end p-8'>
<div ref={refPrompContainer} className='flex-1 flex flex-col gap-2 border-4 overflow-y-auto'>
{prompt?.length==0 ? (
<div className='p-2 border-b border-gray-200'>Promp will appear here...</div>
):(
prompt?.map((item, index) => (
<div key={index} className='p-2 border-b border-gray-500'>
<div key={index} className='p-2 border-b border-gray-500 bg-pink-200'>
<p className='text-lg'>{item}</p>
</div>
))
)}
</div>
<div className='flex flex-col gap-2'>
{/* History will be rendered here */}
{history?.length==0? (
<div className='bg-white p-2 rounded border'>History will appear here...</div>
):(
history.map((item, index) => (
<div key={index} className={`p-2 rounded border-4 ${item.role === 'user' ? 'bg-gray-100' : 'bg-yellow-100'}`}>
{item.content.map((content, idx) => (
<p key={idx} className='text-lg'>{content.text}</p>
))}
</div>
))
)}
<div ref={refHistoryContainer} className='flex-1 overflow-y-auto'>
<div className='flex flex-col justify-end gap-2'>
{history?.length==0? (
<div className='p-2'>History will appear here...</div>
):(
history.map((item, index) => (
<div key={index} className={`p-2 rounded border-4 ${item.role === 'user' ? 'bg-gray-100' : 'bg-yellow-100'}`}>
{item.content.map((content, idx) => (
<p key={idx} className='text-lg'>{content.text}</p>
))}
</div>
))
)}
</div>
</div>
<div className=''>
<form className='flex flex-col justify-center *:border-4 gap-4' onSubmit={onSubmit}>

Loading…
Cancel
Save