diff --git a/JS/edgechains/examples/react-chain/jsonnet/main.jsonnet b/JS/edgechains/examples/react-chain/jsonnet/main.jsonnet index 030bd5034..52c26c847 100644 --- a/JS/edgechains/examples/react-chain/jsonnet/main.jsonnet +++ b/JS/edgechains/examples/react-chain/jsonnet/main.jsonnet @@ -124,7 +124,8 @@ local updatedPrompt = std.strReplace(promptTemplate,'{}', UserQuestion + "\n"); local getOpenAiResponse(prompt) = - local response = arakoo.native("openAICall")(prompt); + local key = std.extVar("openai_key"); + local response = arakoo.native("openAICall")({ prompt: prompt, apiKey: key }); response; @@ -145,7 +146,7 @@ local findMatch(text, searchString) = local getObservation(searchTitle) = local url = "https://en.wikipedia.org/w/api.php"; - local apiUrl = url + "?action=query&format=json&list=search&formatversion=2&srsearch=" + searchTitle; + local apiUrl = url + "?action=query&format=json&list=search&formatversion=2&srsearch=" + arakoo.urlEncode(searchTitle); local wikiConfig = { url: apiUrl, }; diff --git a/JS/edgechains/examples/react-chain/package.json b/JS/edgechains/examples/react-chain/package.json index 125150ddd..ce6123dcf 100644 --- a/JS/edgechains/examples/react-chain/package.json +++ b/JS/edgechains/examples/react-chain/package.json @@ -6,18 +6,20 @@ "type": "module", "scripts": { "start": "tsc && node --experimental-wasm-modules ./dist/index.js", + "wasm": "tsc && webpack && arakoo-compiler dist/final.js", "test": "npx vitest" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { - "@arakoodev/edgechains.js": "^0.23.6", - "@arakoodev/jsonnet": "^0.23.6", + "@arakoodev/edgechains.js": "file:../../arakoodev", + "@arakoodev/jsonnet": "file:../../../jsonnet", "axios": "^1.7.2", "file-uri-to-path": "^2.0.0", "path": "^0.12.7", - "sync-rpc": "^1.3.6" + "webpack": "^5.91.0", + "webpack-cli": "^5.1.4" }, "devDependencies": { "@types/deasync": "^0.1.5", diff --git a/JS/edgechains/examples/react-chain/replacedirnameloader.cjs b/JS/edgechains/examples/react-chain/replacedirnameloader.cjs new file mode 100644 index 000000000..a7e53f431 --- /dev/null +++ b/JS/edgechains/examples/react-chain/replacedirnameloader.cjs @@ -0,0 +1,14 @@ +const path = require('path'); + +module.exports = function (source) { + // console.log(source) + return source.replace(/__dirname/g, (match, p1) => { + // try { + return `"${this.context}"`; + // } catch (error) { + // console.log("error ",error) + // return match; + // } + // return `require('${filePath}')`; + }); +}; \ No newline at end of file diff --git a/JS/edgechains/examples/react-chain/replaceloader.cjs b/JS/edgechains/examples/react-chain/replaceloader.cjs new file mode 100644 index 000000000..8d7cf694e --- /dev/null +++ b/JS/edgechains/examples/react-chain/replaceloader.cjs @@ -0,0 +1,22 @@ +// module.exports = function (source) { +// // Regular expression to match createClient(import.meta.resolve()) +// const regex = /createClient\s*\(\s*import\.meta\.resolve\s*\(\s*(['"`].*?['"`])\s*\)\s*\)/g; + +// // Replace with require() +// return source.replace(regex, 'require($1)'); +// }; + +const path = require('path'); + +module.exports = function (source) { + // console.log(source) + return source.replace(/createClient\(([^()]*(?:\([^()]*\)[^()]*)*)\)/g, (match, p1) => { + // console.log("P1", p1) + // console.log("this.context" ,this.context) + __dirname = this.context; + // console.log("eval", eval(p1)) + // console.log("__dirname", __dirname) + const filePath = path.resolve(this.context, eval(p1)); + return `require('${filePath}')`; + }); +}; \ No newline at end of file diff --git a/JS/edgechains/examples/react-chain/src/lib/apiCall.cts b/JS/edgechains/examples/react-chain/src/lib/apiCall.cts index 05bf1c4c3..62f4a71a1 100644 --- a/JS/edgechains/examples/react-chain/src/lib/apiCall.cts +++ b/JS/edgechains/examples/react-chain/src/lib/apiCall.cts @@ -1,17 +1,11 @@ import axios from "axios"; -function apiCall() { - return (config: any) => { - const response = axios - .request(config) - .then((response: any) => { - return JSON.stringify(response.data); - }) - .catch((error: any) => { - console.error(error); - }); - return response; - }; -} - +async function apiCall(config: any) { + try { + const response = await axios.request(config) + return JSON.stringify(response.data); + } catch (error) { + console.error(error); + } +}; module.exports = apiCall; diff --git a/JS/edgechains/examples/react-chain/src/lib/callWikipediaApi.cts b/JS/edgechains/examples/react-chain/src/lib/callWikipediaApi.cts deleted file mode 100644 index a700d28ba..000000000 --- a/JS/edgechains/examples/react-chain/src/lib/callWikipediaApi.cts +++ /dev/null @@ -1,46 +0,0 @@ -const axios = require("axios"); - -function findMatch(text: string, searchString: string): Boolean { - if (text.includes(searchString) || searchString.includes(text)) { - return true; - } - return false; -} - -function callWikipediaApi() { - return function (searchQuery: string) { - const url = "https://en.wikipedia.org/w/api.php"; - - const queryParams = new URLSearchParams({ - action: "query", - format: "json", - list: "search", - formatversion: "2", - srsearch: searchQuery, - }); - - const apiUrl = `${url}?${queryParams.toString()}`; - - try { - return axios(apiUrl).then((res: any) => { - let val: any; - while (!val) { - const response = res.data.query.search; - const searchLength = response.length; - for (let i = 0; i < searchLength; i++) { - const title = response[i].title; - if (findMatch(title, searchQuery)) { - val = response[i].pageid; - } - } - } - return val; - }); - } catch (error) { - console.error("Error fetching data:", error); - throw error; - } - }; -} - -module.exports = callWikipediaApi; diff --git a/JS/edgechains/examples/react-chain/src/lib/generateResponse.cts b/JS/edgechains/examples/react-chain/src/lib/generateResponse.cts index fedb6805d..538f92b66 100644 --- a/JS/edgechains/examples/react-chain/src/lib/generateResponse.cts +++ b/JS/edgechains/examples/react-chain/src/lib/generateResponse.cts @@ -1,27 +1,17 @@ -const path = require("path"); const { OpenAI } = require("@arakoodev/edgechains.js/openai"); -const Jsonnet = require("@arakoodev/jsonnet"); -const jsonnet = new Jsonnet(); - -const secretsPath = path.join(__dirname, "../../jsonnet/secrets.jsonnet"); -const openAIApiKey = JSON.parse(jsonnet.evaluateFile(secretsPath)).openai_api_key; - -const openai = new OpenAI({ - apiKey: openAIApiKey, - temperature: 0, -}); - -function openAICall() { - return function (prompt: string) { - try { - return openai.chat({ prompt }).then((res: any) => { - return res.content; - }); - } catch (error) { - return error; - } - }; -} +async function openAICall({ prompt, apiKey }: any) { + try { + const openai = new OpenAI({ + apiKey: apiKey, + temperature: 0, + }); + return openai.chat({ prompt }).then((res: any) => { + return res.content; + }); + } catch (error) { + return error; + } +}; module.exports = openAICall; diff --git a/JS/edgechains/examples/react-chain/src/routes/react-chain.ts b/JS/edgechains/examples/react-chain/src/routes/react-chain.ts index 8b323879c..0b148921d 100644 --- a/JS/edgechains/examples/react-chain/src/routes/react-chain.ts +++ b/JS/edgechains/examples/react-chain/src/routes/react-chain.ts @@ -3,25 +3,26 @@ import Jsonnet from "@arakoodev/jsonnet"; import fileURLToPath from "file-uri-to-path"; import path from "path"; //@ts-ignore -import createClient from "sync-rpc"; +import createClient from "@arakoodev/edgechains.js/sync-rpc"; const server = new ArakooServer(); const jsonnet = new Jsonnet(); -const __dirname = fileURLToPath(import.meta.url); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const openAICall = createClient(path.join(__dirname, "../../lib/generateResponse.cjs")); -const apiCall = createClient(path.join(__dirname, "../../lib/apiCall.cjs")); +const openAICall = createClient(path.join(__dirname, "../lib/generateResponse.cjs")); +const apiCall = createClient(path.join(__dirname, "../lib/apiCall.cjs")); export const ReactChainRouter = server.createApp(); ReactChainRouter.get("/", async (c: any) => { try { const question = c.req.query("question"); - + let key = JSON.parse(jsonnet.evaluateFile(path.join(__dirname, '../../jsonnet/secrets.jsonnet'))).openai_api_key; jsonnet.extString("question", question || ""); + jsonnet.extString("openai_key", key); jsonnet.javascriptCallback("openAICall", openAICall); jsonnet.javascriptCallback("apiCall", apiCall); - let response = jsonnet.evaluateFile(path.join(__dirname, "../../../jsonnet/main.jsonnet")); + let response = jsonnet.evaluateFile(path.join(__dirname, "../../jsonnet/main.jsonnet")); return c.json(response); } catch (error) { return c.json({ diff --git a/JS/edgechains/examples/react-chain/webpack.config.cjs b/JS/edgechains/examples/react-chain/webpack.config.cjs new file mode 100644 index 000000000..b4e661701 --- /dev/null +++ b/JS/edgechains/examples/react-chain/webpack.config.cjs @@ -0,0 +1,61 @@ +const webpack = require('webpack'); +const path = require("path"); + +let definePlugin = new webpack.DefinePlugin({ + 'process.env.arakoo': true, +}) + +let replacePlugin = new webpack.NormalModuleReplacementPlugin(/\@arakoodev\/edgechains\.js\/sync-rpc/, function (resource) { + resource.request = path.resolve(__dirname, 'empty-module.js') +}) + +const config = [{ + name: "first", + entry: './dist/index.js', + output: { + path: __dirname + '/dist', + filename: 'index.cjs', + iife: false + }, + resolve:{ + extensions:['.js'] + }, + mode:'production', + module: { + rules: [{ + test: /\.js$/, + exclude: /node_modules/, + use: path.resolve('replaceloader.cjs') // Use the custom loader + }] + }, + optimization: { + minimize: false + }, + plugins: [definePlugin], +}, { + name: "second", + entry: './dist/index.cjs', + output: { + path: __dirname + '/dist', + filename: 'final.js', + iife: false + }, + resolve:{ + extensions:['.cjs','.js'], + }, + mode:'production', + module: { + rules: [{ + test: /\.cjs$/, + exclude: /node_modules/, + use: path.resolve('replacedirnameloader.cjs') + }] + }, + optimization: { + minimize: false + }, + plugins: [definePlugin], + dependencies: ["first"], +}]; + +module.exports = config; \ No newline at end of file