kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
pull/715/head
rodzic
449bb4a926
commit
b774480a15
|
@ -1,8 +1,17 @@
|
||||||
|
import type { Context } from './types'
|
||||||
|
|
||||||
const cache = caches.default
|
const cache = caches.default
|
||||||
|
|
||||||
export async function fetchCache(opts) {
|
export async function fetchCache(
|
||||||
const { event, cacheKey, fetch: fetchResponse } = opts
|
ctx: Context,
|
||||||
|
{
|
||||||
|
cacheKey,
|
||||||
|
fetchResponse
|
||||||
|
}: {
|
||||||
|
cacheKey?: string
|
||||||
|
fetchResponse: () => Promise<Response>
|
||||||
|
}
|
||||||
|
) {
|
||||||
let response
|
let response
|
||||||
|
|
||||||
if (cacheKey) {
|
if (cacheKey) {
|
||||||
|
@ -16,7 +25,7 @@ export async function fetchCache(opts) {
|
||||||
if (cacheKey) {
|
if (cacheKey) {
|
||||||
if (response.headers.has('Cache-Control')) {
|
if (response.headers.has('Cache-Control')) {
|
||||||
// cache will respect response headers
|
// cache will respect response headers
|
||||||
event.waitUntil(
|
ctx.waitUntil(
|
||||||
cache.put(cacheKey, response.clone()).catch((err) => {
|
cache.put(cacheKey, response.clone()).catch((err) => {
|
||||||
console.warn('cache put error', cacheKey, err)
|
console.warn('cache put error', cacheKey, err)
|
||||||
})
|
})
|
||||||
|
|
|
@ -26,6 +26,8 @@ export async function resolveOriginRequest(
|
||||||
const { search, pathname } = requestUrl
|
const { search, pathname } = requestUrl
|
||||||
const method = req.method.toLowerCase()
|
const method = req.method.toLowerCase()
|
||||||
const requestPathParts = pathname.split('/')
|
const requestPathParts = pathname.split('/')
|
||||||
|
|
||||||
|
// TODO: the isMCPRequest logic needs to be completely redone.
|
||||||
const isMCPRequest = requestPathParts[0] === 'mcp'
|
const isMCPRequest = requestPathParts[0] === 'mcp'
|
||||||
const requestPath = isMCPRequest
|
const requestPath = isMCPRequest
|
||||||
? requestPathParts.slice(1).join('/')
|
? requestPathParts.slice(1).join('/')
|
||||||
|
@ -70,7 +72,7 @@ export async function resolveOriginRequest(
|
||||||
`Auth token "${token}" is not authorized for project "${deployment.projectId}"`
|
`Auth token "${token}" is not authorized for project "${deployment.projectId}"`
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Ensure that consumer.plan is compatible with the target deployment
|
// TODO: Ensure that consumer.plan is compatible with the target deployment?
|
||||||
// TODO: This could definitely cause issues when changing pricing plans.
|
// TODO: This could definitely cause issues when changing pricing plans.
|
||||||
|
|
||||||
pricingPlan = deployment.pricingPlans.find(
|
pricingPlan = deployment.pricingPlans.find(
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { AgenticApiClient } from '@agentic/platform-api-client'
|
import { AgenticApiClient } from '@agentic/platform-api-client'
|
||||||
import { parseZodSchema } from '@agentic/platform-core'
|
import { assert, parseZodSchema } from '@agentic/platform-core'
|
||||||
|
|
||||||
import type { Context } from './lib/types'
|
import type { Context } from './lib/types'
|
||||||
import { type AgenticEnv, envSchema } from './lib/env'
|
import { type AgenticEnv, envSchema } from './lib/env'
|
||||||
|
@ -67,21 +67,32 @@ export default {
|
||||||
|
|
||||||
switch (resolvedOriginRequest.deployment.originAdapter.type) {
|
switch (resolvedOriginRequest.deployment.originAdapter.type) {
|
||||||
case 'openapi':
|
case 'openapi':
|
||||||
originResponse = await fetch(resolvedOriginRequest.originRequest!)
|
assert(
|
||||||
|
resolvedOriginRequest.originRequest,
|
||||||
|
500,
|
||||||
|
'Origin request is required'
|
||||||
|
)
|
||||||
|
originResponse = await fetch(resolvedOriginRequest.originRequest)
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'raw':
|
case 'raw':
|
||||||
originResponse = await fetch(resolvedOriginRequest.originRequest!)
|
assert(
|
||||||
|
resolvedOriginRequest.originRequest,
|
||||||
|
500,
|
||||||
|
'Origin request is required'
|
||||||
|
)
|
||||||
|
originResponse = await fetch(resolvedOriginRequest.originRequest)
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'mcp':
|
case 'mcp':
|
||||||
throw new Error('MCP not yet supported')
|
throw new Error('MCP not yet supported')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(originResponse, 500, 'Origin response is required')
|
||||||
const res = new Response(originResponse.body, originResponse)
|
const res = new Response(originResponse.body, originResponse)
|
||||||
recordTimespans()
|
|
||||||
|
|
||||||
// Record the time it took for both the origin and gateway proxy to respond
|
// Record the time it took for both the origin and gateway proxy to respond
|
||||||
|
recordTimespans()
|
||||||
res.headers.set('x-response-time', `${originTimespan!}ms`)
|
res.headers.set('x-response-time', `${originTimespan!}ms`)
|
||||||
res.headers.set('x-proxy-response-time', `${gatewayTimespan!}ms`)
|
res.headers.set('x-proxy-response-time', `${gatewayTimespan!}ms`)
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue