Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Apr 16, 2023
1 parent 8340c46 commit 0de7333
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/anime-adventure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@cloudflare/itty-router-openapi": "^0.0.15",
"anilist-node": "^1.13.2",
"chatgpt-plugin": "workspace:../../packages/chatgpt-plugin",
"nanoid": "^4.0.2",
"zod": "^3.21.4"
},
"aiPlugin": {
Expand Down
20 changes: 20 additions & 0 deletions examples/anime-adventure/src/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const getActions = (host: string) => [
{
id: 0,
actionUrl: `https://${host}/actions/${0}`,
imageUrl:
'https://artworks.thetvdb.com/banners/fanart/original/5d8e636ab0d05.jpg',
name: 'Go on a crazy adventure with Ed',
description:
'Take a trip with Ed and Ein on a motorycle and a desert planet. Make the story super zany and fun.'
},
{
id: 1,
actionUrl: `https://${host}/actions/${1}`,
imageUrl:
'https://artworks.thetvdb.com/banners/fanart/original/76885-19.jpg',
name: 'Visit a bar with Jet',
description:
'Visit a dive bar with Jet in the underbelly of a distant planet. Make the story dark and gritty.'
}
]
61 changes: 61 additions & 0 deletions examples/anime-adventure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineAIPluginManifest } from 'chatgpt-plugin'

import * as routes from './routes'
import pkg from '../package.json'
import { getActions } from './actions'

export interface Env {
ANILIST_ACCESS_TOKEN: string
Expand Down Expand Up @@ -38,6 +39,66 @@ router.get('/.well-known/ai-plugin.json', (request: Request) => {
})
})

router.get(
'/actions/:id',
(request: Request, env: any, _ctx, data: Record<string, any>) => {
const host = request.headers.get('host')
const id = data.id
const actions = getActions(host)
const action = actions.find((a) => a.id === id)
if (!action) {
return new Response(`Action not found`, { status: 404 })
}

const desc = action.description
const title = action.name
const imageUrl = action.imageUrl

const html = `
<html>
<head>
<meta name='description' content="${desc}">
<meta property='og:description' content="${desc}">
<meta name='twitter:description' content="${desc}" />
<meta name='twitter:card' content='summary_large_image' >
<meta name='twitter:image' content="${imageUrl}" >
<meta property='og:image' content="${imageUrl}" >
<meta property='og:title' content="${title}" >
<meta name='twitter:title' content="${title}" >
<title>${title}</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
}
</style>
</head>
<body>
<h1>${title}</h1>
<p>${desc}</p>
<img src="${imageUrl}" />
</body>
</html>
`
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8'
}
})
}
)

// 404 for everything else
router.all('*', () => new Response('Not Found.', { status: 404 }))

Expand Down
31 changes: 26 additions & 5 deletions examples/anime-adventure/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { OpenAPIRoute, Query, Str } from '@cloudflare/itty-router-openapi'
import Anilist from 'anilist-node'
import { nanoid } from 'nanoid'

// import { isValidChatGPTIPAddress } from 'chatgpt-plugin'
import * as types from './types'
import * as utils from './utils'
import { getActions } from './actions'

export class StartGame extends OpenAPIRoute {
static schema = {
summary:
'Starts a new game of Anime Adventure. The user must provide the name of an Anime that they want to base the game around. You should use the anime metadata that is returned to start a story set in the world of this anime.',
'Starts a new game of Anime Adventure. The user must provide the name of an Anime that they want to base the game around. You should use the anime metadata that is returned to start a story set in the world of this anime. It also returns "actions" which are very important. Your story should focus on giving the user the option to choose between the given actions in order to continue the story. The user will click on a given "actionUrl" and future invocations of this ',
parameters: {
anime: Query(
new Str({
Expand All @@ -23,6 +25,19 @@ export class StartGame extends OpenAPIRoute {
responses: {
'200': {
schema: {
actions: [
{
id: new Str(),
actionUrl: new Str({
description: 'URL of the action to perform'
}),
imageUrl: new Str({
description: 'Image preview of the action'
}),
name: new Str({ description: 'Name of the action to perform' }),
description: new Str()
}
],
anime: {
id: new Str(),
title: new Str({
Expand Down Expand Up @@ -54,6 +69,7 @@ export class StartGame extends OpenAPIRoute {
return new Response('ANILIST_ACCESS_TOKEN not set', { status: 500 })
}

const host = request.headers.get('host')
const ip = request.headers.get('Cf-Connecting-Ip')
if (!ip) {
console.warn('search error missing IP address')
Expand Down Expand Up @@ -102,14 +118,19 @@ export class StartGame extends OpenAPIRoute {
.map((review) => utils.pick(review, 'summary', 'body'))
}

const actions = getActions(host)

console.log()
console.log()
console.log('<<< search', `${query} (${openaiUserLocaleInfo}, ${ip})`)

return new Response(JSON.stringify({ anime: animeMetadata }, null, 2), {
headers: {
'content-type': 'application/json;charset=UTF-8'
return new Response(
JSON.stringify({ anime: animeMetadata, actions }, null, 2),
{
headers: {
'content-type': 'application/json;charset=UTF-8'
}
}
})
)
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 0de7333

Please sign in to comment.