Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import type { AppContext } from '@api/types/hono';
import type { SuperAgentsRequestData } from '@shared/types/api/request';
import type { SuperAgentsTarget } from '@shared/types/api/request/headers';
import { AIProvider } from '@shared/types/constants';
import bedrockAPIConfig from './api';
export const bedrockRetrieveFileRequestHandler = async ({
c,
saTarget,
saRequestData,
}: {
c: AppContext;
saTarget: SuperAgentsTarget;
saRequestData: SuperAgentsRequestData;
}): Promise<Response> => {
try {
// construct the base url and endpoint
const baseUrl = await bedrockAPIConfig.getBaseURL({
c,
saTarget,
saRequestData,
});
const endpoint = bedrockAPIConfig.getEndpoint({
c,
saTarget,
saRequestData,
});
const retrieveFileURL = `${baseUrl}${endpoint}`;
// generate the headers
const headers = await bedrockAPIConfig.headers({
c,
saTarget,
saRequestData,
});
// make the request
const response = await fetch(retrieveFileURL, {
method: 'GET',
headers: headers as HeadersInit,
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
JSON.stringify({
type: 'provider_error',
code: response.status,
param: null,
message: `bedrock error: ${errorText}`,
}),
);
}
// parse necessary information from xml response
const responseBodyXML = await response.text();
const responseHeaders = response.headers;
const match = responseBodyXML.match(/<ObjectSize>(\d+)<\/ObjectSize>/);
const size = match?.[1];
// transform the response
const transformedResponse = {
object: 'file',
id: saRequestData.url.split('/v1/files/')[1],
purpose: '',
filename: decodeURIComponent(saRequestData.url.split('/v1/files/')[1]),
bytes: size,
createdAt: Math.floor(
new Date(responseHeaders.get('last-modified') || '').getTime() / 1000,
),
status: 'processed',
status_details: null,
};
// return the response
return new Response(JSON.stringify(transformedResponse), {
headers: {
'content-type': 'application/json',
},
});
} catch (error: unknown) {
let errorResponse: Record<string, unknown> & { provider?: string };
try {
errorResponse = JSON.parse((error as Error).message);
errorResponse.provider = AIProvider.BEDROCK;
} catch (_e) {
errorResponse = {
error: {
message: (error as Error).message,
type: null,
param: null,
code: 500,
},
provider: AIProvider.BEDROCK,
};
}
return new Response(JSON.stringify(errorResponse), {
status: 500,
headers: {
'Content-Type': 'application/json',
},
});
}
};
|