Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add temporary static homepage #30

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () => {
flakey5 marked this conversation as resolved.
Show resolved Hide resolved
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()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the EventSource class will work here so we need to write our own parser for it. I don't think this is a compatibility issue that's on us, OpenAI does the same in their client here, but it is unfortunate

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>
Loading