Skip to content

Commit

Permalink
aiproxy web4 bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Dec 30, 2024
1 parent 056eb6d commit b58e6e8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
2 changes: 2 additions & 0 deletions examples/aiproxy/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target
.spin
dist
web4.js
28 changes: 28 additions & 0 deletions examples/aiproxy/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import html from '@web/rollup-plugin-html';
import { terser } from 'rollup-plugin-terser';
import { readFileSync, readdirSync, unlinkSync, writeFileSync } from 'fs';

export default {
input: ['./web/index.html'],
output: { dir: 'dist' },
plugins: [html({ minify: true }), terser(), {
name: 'inline-js',
closeBundle: () => {
const js = readFileSync('dist/main.js').toString();
const html = readFileSync('dist/index.html').toString()
.replace(`<script src="main.js" type="module"></script>`, `<script type="module">${js}</script>`);

writeFileSync('web4.js', `
export function web4_get() {
env.value_return(JSON.stringify({
contentType: 'text/html; charset=UTF-8',
body: '${Buffer.from(html).toString('base64')}'
})
);
}
`);

}
}],
};
35 changes: 21 additions & 14 deletions examples/aiproxy/web/index.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenAI Proxy Streaming</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenAI Proxy Streaming</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
<div class="container">
<div class="mb-3">
<label for="conversation_id" class="form-label">Conversation id ( generated )</label>
<input id="conversation_id" class="form-control" readonly />
</div>
<button id="startConversationButton" class="btn btn-primary">Start conversation</button>

<div id="messages"></div>
<div class="mb-3">
<label for="question" class="form-label">Question</label>
<textarea id="question" class="form-control" disabled rows="4" placeholder="Type your question here..."></textarea>
<button id="askAIButton" class="btn btn-primary" disabled>Ask AI</button>
<label for="question" class="form-label">Question</label>
<textarea id="question" class="form-control" disabled rows="4"
placeholder="Type your question here..."></textarea>
<button id="askAIButton" class="btn btn-primary" disabled>Ask AI</button>
</div>
<button id="refundButton" class="btn btn-primary">Stop conversation and refund tokens</button><br />
<div style="width: 100%">
<pre>
<pre>
<code id="refund_message" style="white-space: wrap;"></code>
</pre>
</div>
</div>
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script type="importmap">
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js"
crossorigin="anonymous"></script>
<script type="importmap">
{
"imports": {
"near-api-js": "https://ga.jspm.io/npm:[email protected]/lib/browser-index.js"
Expand Down Expand Up @@ -107,8 +112,10 @@
}
}
</script>
<script src="main.js" type="module">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="main.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>

</html>
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"test-quickjslib": "cd quickjslib && ./build.sh && node --test",
"examples-nft-web4bundle": "cd examples/nft/web4 && rollup -c rollup.config.js",
"serve-examples-nft": "http-server -p 8085 examples/nft/web4",
"test-playwright-aiproxy": "yarn playwright test -c examples/aiproxy/playwright.config.js"
"test-playwright-aiproxy": "yarn playwright test -c examples/aiproxy/playwright.config.js",
"aiproxy:web4bundle": "cd examples/aiproxy && rollup -c rollup.config.js"
},
"jest": {
"transform": {}
Expand All @@ -28,7 +29,7 @@
"http-server": "^14.1.1",

"near-workspaces": "4.0.0",
"rollup": "^2.79.0",
"rollup": "^4.29.1",
"rollup-plugin-terser": "^7.0.2"
},
"dependencies": {}
Expand Down

0 comments on commit b58e6e8

Please sign in to comment.