All files / api/src/ai-providers/bedrock retrieve-file-content.ts

0% Statements 0/95
0% Branches 0/1
0% Functions 0/1
0% Lines 0/95

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                                                                                                                                                                                                                               
import { bedrockErrorResponseTransform } from '@api/ai-providers/bedrock/chat-complete';
import { getOctetStreamToOctetStreamTransformer } from '@api/handlers/stream-handler-utils';
import type { AppContext } from '@api/types/hono';
import type { ResponseTransformFunction } from '@shared/types/ai-providers/config';
import type { SuperAgentsRequestData } from '@shared/types/api/request';
import type { SuperAgentsTarget } from '@shared/types/api/request/headers';
import type { FileContentResponseBody } from '@shared/types/api/routes/files-api';
import { AIProvider } from '@shared/types/constants';
import bedrockAPIConfig from './api';
 
const getRowTransform = (): ((
  row: Record<string, unknown>,
) => Record<string, unknown>) => {
  return (row: Record<string, unknown>): Record<string, unknown> => row;
};
 
export const bedrockRetrieveFileContentRequestHandler = async ({
  c,
  saTarget,
  saRequestData,
}: {
  c: AppContext;
  saTarget: SuperAgentsTarget;
  saRequestData: SuperAgentsRequestData;
}): Promise<Response> => {
  try {
    // construct the base url and endpoint
    const baseURL = bedrockAPIConfig.getBaseURL({
      c,
      saTarget,
      saRequestData,
    });
    const endpoint = bedrockAPIConfig.getEndpoint({
      c,
      saTarget,
      saRequestData,
    });
    const url = `${baseURL}${endpoint}`;
 
    // generate the headers
    const headers = await bedrockAPIConfig.headers({
      c,
      saTarget,
      saRequestData,
    });
 
    // make the request
    const response = await fetch(url, {
      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}`,
        }),
      );
    }
 
    // transform the streaming response to provider format
    let responseStream: ReadableStream;
    if (response?.body) {
      responseStream = response?.body?.pipeThrough(
        getOctetStreamToOctetStreamTransformer(getRowTransform()),
      );
    } else {
      throw new Error(
        'Failed to parse and transform file content, please verify that the file is a valid jsonl file used for batching or fine-tuning',
      );
    }
 
    // return the response
    return new Response(responseStream, {
      headers: {
        'content-type': 'application/octet-stream',
      },
    });
  } 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',
      },
    });
  }
};
 
export const bedrockRetrieveFileContentResponseTransform: ResponseTransformFunction =
  (response, responseStatus) => {
    if (responseStatus !== 200) {
      const error = bedrockErrorResponseTransform(response);
      if (error) {
        return error;
      }
    }
 
    return response as unknown as FileContentResponseBody;
  };