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

fix: enable back AI in dev #327

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
66 changes: 32 additions & 34 deletions src/runtime/ai/server/utils/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,40 @@ export function hubAI(): Ai {
const binding = process.env.AI || globalThis.__env__?.AI || globalThis.AI
if (hub.remote && hub.projectUrl && !binding) {
_ai = proxyHubAI(hub.projectUrl, hub.projectSecretKey || hub.userToken)
return _ai
}
if (binding) {
if (import.meta.dev) {
// Mock _ai to call NuxtHub Admin API to proxy CF account & API token
_ai = {
async run(model: string, params?: Record<string, unknown>) {
if (!hub.projectKey) {
throw createError({
statusCode: 500,
message: 'Missing hub.projectKey variable to use hubAI()'
})
}
if (!hub.userToken) {
throw createError({
statusCode: 500,
message: 'Missing hub.userToken variable to use hubAI()'
})
}
return $fetch(`/api/projects/${hub.projectKey}/ai/run`, {
baseURL: hub.url,
method: 'POST',
headers: {
authorization: `Bearer ${hub.userToken}`
},
body: { model, params },
responseType: params?.stream ? 'stream' : undefined
}).catch(handleProxyError)
} else if (import.meta.dev) {
// Mock _ai to call NuxtHub Admin API to proxy CF account & API token
_ai = {
async run(model: string, params?: Record<string, unknown>) {
if (!hub.projectKey) {
throw createError({
statusCode: 500,
message: 'Missing hub.projectKey variable to use hubAI()'
})
}
} as Ai
} else {
_ai = binding as Ai
}
return _ai
if (!hub.userToken) {
throw createError({
statusCode: 500,
message: 'Missing hub.userToken variable to use hubAI()'
})
}
return $fetch(`/api/projects/${hub.projectKey}/ai/run`, {
baseURL: hub.url,
method: 'POST',
headers: {
authorization: `Bearer ${hub.userToken}`
},
body: { model, params },
responseType: params?.stream ? 'stream' : undefined
}).catch(handleProxyError)
}
} as Ai
} else if (binding) {
_ai = binding as Ai
}
if (!_ai) {
throw createError('Missing Cloudflare AI binding (AI)')
}
throw createError('Missing Cloudflare AI binding (AI)')
return _ai
}

/**
Expand Down