Skip to content

Commit

Permalink
Add temporary static homepage (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 authored May 3, 2024
1 parent d59f1bb commit 73e5136
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export interface AiWarpConfig {
}[];
};
module?: string;
showAiWarpHomepage?: boolean;
aiProvider:
| {
openai: {
Expand Down
8 changes: 8 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { join } from 'node:path'
import { platformaticService, Stackable } from '@platformatic/service'
import fastifyUser from 'fastify-user'
import fastifyPlugin from 'fastify-plugin'
import fastifyStatic from '@fastify/static'
import { schema } from './lib/schema.js'
import { Generator } from './lib/generator.js'
import { AiWarpConfig } from './config.js'
Expand All @@ -19,6 +21,12 @@ const stackable: Stackable<AiWarpConfig> = async function (fastify, opts) {
await fastify.register(rateLimitPlugin, opts)
await fastify.register(apiPlugin, opts)

if (config.showAiWarpHomepage !== undefined && config.showAiWarpHomepage) {
await fastify.register(fastifyStatic, {
root: join(import.meta.dirname, 'static')
})
}

await fastify.register(platformaticService, opts)
}

Expand Down
4 changes: 4 additions & 0 deletions lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const aiWarpSchema = {
properties: {
...schema.schema.properties,
module: { type: 'string' },
showAiWarpHomepage: {
type: 'boolean',
default: true
},
aiProvider: {
type: 'object',
oneOf: [
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start-ai-warp": "./dist/cli/start.js"
},
"scripts": {
"build": "tsc --build",
"build": "tsc --build && cp -r ./static ./dist/",
"build:config": "node ./dist/lib/schema.js --dump-schema | json2ts > config.d.ts",
"clean": "rm -fr ./dist",
"lint": "ts-standard | snazzy",
Expand Down Expand Up @@ -38,6 +38,7 @@
"@azure/openai": "^1.0.0-beta.12",
"@fastify/error": "^3.4.1",
"@fastify/rate-limit": "^9.1.0",
"@fastify/static": "^7.0.3",
"@fastify/type-provider-typebox": "^4.0.0",
"@platformatic/config": "^1.24.0",
"@platformatic/generators": "^1.24.0",
Expand Down
88 changes: 88 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Warp</title>
</head>
<body>
<div>
<label for="prompt-text">Prompt: </label>
<textarea name="prompt text" id="prompt-text" cols="60" rows="1"></textarea>
<button id="prompt-button">Prompt</button>
</div>

<p id="response"></p>

<script>
const promptText = document.getElementById('prompt-text')
const responseText = document.getElementById('response')

const promptButton = document.getElementById('prompt-button')
promptButton.onclick = async () => {
const res = await fetch('/api/v1/stream', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
prompt: promptText.value
})
})

if (res.status !== 200) {
const { message, code } = await res.json()
responseText.innerHTML = `Error: ${message} (${code})`
return
}

responseText.innerHTML = ''

const reader = res.body.getReader()
const decoder = new TextDecoder()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}

const decodedValue = decoder.decode(value)
const lines = decodedValue.split('\n')

let i = 0
while (i < lines.length) {
const line = lines[i]
if (line.length === 0) {
i++
continue
}

if (!line.startsWith('event: ')) {
responseText.innerHTML = `Unexpected event type line: ${line}`
return
}

const dataLine = lines[i + 1]
if (!dataLine.startsWith('data: ')) {
responseText.innerHTML = `Unexpected data line: ${dataLine}`
return
}

const eventType = line.substring('event: '.length)
const data = dataLine.substring('data: '.length)
const json = JSON.parse(data)
if (eventType === 'content') {
const { response } = json
responseText.innerHTML += response
} else if (eventType === 'error') {
const { message, code } = data
responseText.innerHTML = `Error: ${message} (${code})`
}

i += 2
}
}
}
</script>
</body>
</html>

0 comments on commit 73e5136

Please sign in to comment.