Skip to content

Commit

Permalink
feat: add ability to grab an example.gpt file in the root of a tool
Browse files Browse the repository at this point in the history
Signed-off-by: tylerslaton <[email protected]>
  • Loading branch information
tylerslaton committed Mar 21, 2024
1 parent 337c95e commit e073922
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/server/api/[...slug].post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export default defineEventHandler(async (event) => {
const [owner, repo, ...subdirs] = url.replace(/^(https?:\/\/)?(www\.)?github\.com\//, '').split('/')

// construct the path to the tool.gpt file
const toolPath = subdirs.length > 0 ? `${ subdirs.join('/') }/tool.gpt` : 'tool.gpt'
const path = subdirs.length > 0 ? `${ subdirs.join('/') }` : ''

// fetch the tool.gpt file from github
const toolResponse = await fetch(`https://raw.githubusercontent.com/${ owner }/${ repo }/main/${ toolPath }`)
const toolResponse = await fetch(`https://raw.githubusercontent.com/${ owner }/${ repo }/main/${ path }/tool.gpt`)

if (!toolResponse.ok) {
// clean-up any existing tools if the tool.gpt file is no longer found or is private
Expand Down Expand Up @@ -94,42 +94,50 @@ export default defineEventHandler(async (event) => {
setResponseHeader(event, 'Content-Type', 'application/json')
setResponseStatus(event, 201)

return await db.upsertToolForUrl(url, parsedTools, await getExamples(owner, repo),
return await db.upsertToolForUrl(url, parsedTools, await getExamples(owner, repo, path),
)
})

// getExamples fetches the examples from the repo located at github.com/owner/repo and returns them as an array of ToolExample objects
async function getExamples(owner: string, repo: string): Promise<ToolExample[]> {
async function getExamples(owner: string, repo: string, path: string): Promise<ToolExample[]> {
const octokit = new Octokit({ auth: useRuntimeConfig().githubToken })

let gptFiles: ToolExample[] = []
try {
// Get the contents of the examples directory
const response = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: 'examples',
ref: 'main', // Replace 'main' with the desired branch name
path: path+'/examples',
ref: 'main',
})

// Filter the response to include only .gpt files
const gptExamples = (response.data as any[]).filter((file: any) => file.type === 'file' && file.name.endsWith('.gpt'))

const gptFiles: ToolExample[] = await Promise.all(gptExamples.map(async (example: any) => {
gptFiles = await Promise.all(gptExamples.map(async (example: any) => {
const response = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: example.path,
ref: 'main', // Replace 'main' with the desired branch name
ref: 'main',
})

const content = Buffer.from((response.data as any).content, 'base64').toString()
const githubLink = `https://github.com/${ owner }/${ repo }/blob/main/${ example.path }`

return { name: example.name, url: githubLink, content }
}))

return gptFiles
} catch (e) {
return []
// if the error is not a 404, throw it
if ((e as any).status !== 404) { throw e }
}

// Fetch the example.gpt file if it exists
const exampleDotGPT = await fetch(`https://raw.githubusercontent.com/${ owner }/${ repo }/main/${path}/example.gpt`)
if (exampleDotGPT.ok) {
const content = await exampleDotGPT.text()
gptFiles.push({ name: 'example.gpt', url: `https://github.com/${ owner }/${ repo }/blob/main/${path}/example.gpt`, content })
}

return gptFiles
}

0 comments on commit e073922

Please sign in to comment.