fix: route Shakespeare AI calls through CORS proxy for iframe compatibility
All fetch calls to ai.shakespeare.diy now go through proxy.shakespeare.diy so they work from inside the iframe.diy preview sandbox. The NIP-98 token is still signed against the original URL (not the proxy URL) so the server-side signature validation passes correctly. Affects: sendChatMessage, sendStreamingMessage, getAvailableModels Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
parent
223b7602bf
commit
51751f9ea7
1 changed files with 15 additions and 21 deletions
|
|
@ -60,6 +60,12 @@ export interface ModelsResponse {
|
||||||
|
|
||||||
// Configuration
|
// Configuration
|
||||||
const SHAKESPEARE_API_URL = 'https://ai.shakespeare.diy/v1';
|
const SHAKESPEARE_API_URL = 'https://ai.shakespeare.diy/v1';
|
||||||
|
// CORS proxy for iframe environments (proxy.shakespeare.diy passes through the request)
|
||||||
|
const CORS_PROXY = 'https://proxy.shakespeare.diy/?url=';
|
||||||
|
|
||||||
|
function proxyUrl(url: string): string {
|
||||||
|
return `${CORS_PROXY}${encodeURIComponent(url)}`;
|
||||||
|
}
|
||||||
|
|
||||||
// Helper function to create NIP-98 token
|
// Helper function to create NIP-98 token
|
||||||
async function createNIP98Token(
|
async function createNIP98Token(
|
||||||
|
|
@ -174,14 +180,10 @@ export function useShakespeare() {
|
||||||
...options
|
...options
|
||||||
};
|
};
|
||||||
|
|
||||||
const token = await createNIP98Token(
|
const targetUrl = `${SHAKESPEARE_API_URL}/chat/completions`;
|
||||||
'POST',
|
const token = await createNIP98Token('POST', targetUrl, requestBody, user);
|
||||||
`${SHAKESPEARE_API_URL}/chat/completions`,
|
|
||||||
requestBody,
|
|
||||||
user
|
|
||||||
);
|
|
||||||
|
|
||||||
const response = await fetch(`${SHAKESPEARE_API_URL}/chat/completions`, {
|
const response = await fetch(proxyUrl(targetUrl), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Nostr ${token}`,
|
'Authorization': `Nostr ${token}`,
|
||||||
|
|
@ -237,14 +239,10 @@ export function useShakespeare() {
|
||||||
...options
|
...options
|
||||||
};
|
};
|
||||||
|
|
||||||
const token = await createNIP98Token(
|
const streamTargetUrl = `${SHAKESPEARE_API_URL}/chat/completions`;
|
||||||
'POST',
|
const token = await createNIP98Token('POST', streamTargetUrl, requestBody, user);
|
||||||
`${SHAKESPEARE_API_URL}/chat/completions`,
|
|
||||||
requestBody,
|
|
||||||
user
|
|
||||||
);
|
|
||||||
|
|
||||||
const response = await fetch(`${SHAKESPEARE_API_URL}/chat/completions`, {
|
const response = await fetch(proxyUrl(streamTargetUrl), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Nostr ${token}`,
|
'Authorization': `Nostr ${token}`,
|
||||||
|
|
@ -323,14 +321,10 @@ export function useShakespeare() {
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const token = await createNIP98Token(
|
const modelsUrl = `${SHAKESPEARE_API_URL}/models`;
|
||||||
'GET',
|
const token = await createNIP98Token('GET', modelsUrl, undefined, user);
|
||||||
`${SHAKESPEARE_API_URL}/models`,
|
|
||||||
undefined,
|
|
||||||
user
|
|
||||||
);
|
|
||||||
|
|
||||||
const response = await fetch(`${SHAKESPEARE_API_URL}/models`, {
|
const response = await fetch(proxyUrl(modelsUrl), {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Nostr ${token}`,
|
'Authorization': `Nostr ${token}`,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue