main repo

This commit is contained in:
Basilosaurusrex
2025-11-24 18:09:40 +01:00
parent b636ee5e70
commit f027651f9b
34146 changed files with 4436636 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
import { resolveFetch } from './helper'
import {
Fetch,
FunctionsFetchError,
FunctionsHttpError,
FunctionsRelayError,
FunctionsResponse,
FunctionInvokeOptions,
FunctionRegion,
} from './types'
export class FunctionsClient {
protected url: string
protected headers: Record<string, string>
protected region: FunctionRegion
protected fetch: Fetch
constructor(
url: string,
{
headers = {},
customFetch,
region = FunctionRegion.Any,
}: {
headers?: Record<string, string>
customFetch?: Fetch
region?: FunctionRegion
} = {}
) {
this.url = url
this.headers = headers
this.region = region
this.fetch = resolveFetch(customFetch)
}
/**
* Updates the authorization header
* @param token - the new jwt token sent in the authorisation header
*/
setAuth(token: string) {
this.headers.Authorization = `Bearer ${token}`
}
/**
* Invokes a function
* @param functionName - The name of the Function to invoke.
* @param options - Options for invoking the Function.
*/
async invoke<T = any>(
functionName: string,
options: FunctionInvokeOptions = {}
): Promise<FunctionsResponse<T>> {
try {
const { headers, method, body: functionArgs } = options
let _headers: Record<string, string> = {}
let { region } = options
if (!region) {
region = this.region
}
// Add region as query parameter using URL API
const url = new URL(`${this.url}/${functionName}`)
if (region && region !== 'any') {
_headers['x-region'] = region
url.searchParams.set('forceFunctionRegion', region)
}
let body: any
if (
functionArgs &&
((headers && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) || !headers)
) {
if (
(typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
functionArgs instanceof ArrayBuffer
) {
// will work for File as File inherits Blob
// also works for ArrayBuffer as it is the same underlying structure as a Blob
_headers['Content-Type'] = 'application/octet-stream'
body = functionArgs
} else if (typeof functionArgs === 'string') {
// plain string
_headers['Content-Type'] = 'text/plain'
body = functionArgs
} else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
// don't set content-type headers
// Request will automatically add the right boundary value
body = functionArgs
} else {
// default, assume this is JSON
_headers['Content-Type'] = 'application/json'
body = JSON.stringify(functionArgs)
}
}
const response = await this.fetch(url.toString(), {
method: method || 'POST',
// headers priority is (high to low):
// 1. invoke-level headers
// 2. client-level headers
// 3. default Content-Type header
headers: { ..._headers, ...this.headers, ...headers },
body,
}).catch((fetchError) => {
throw new FunctionsFetchError(fetchError)
})
const isRelayError = response.headers.get('x-relay-error')
if (isRelayError && isRelayError === 'true') {
throw new FunctionsRelayError(response)
}
if (!response.ok) {
throw new FunctionsHttpError(response)
}
let responseType = (response.headers.get('Content-Type') ?? 'text/plain').split(';')[0].trim()
let data: any
if (responseType === 'application/json') {
data = await response.json()
} else if (responseType === 'application/octet-stream') {
data = await response.blob()
} else if (responseType === 'text/event-stream') {
data = response
} else if (responseType === 'multipart/form-data') {
data = await response.formData()
} else {
// default to text
data = await response.text()
}
return { data, error: null, response }
} catch (error) {
return {
data: null,
error,
response:
error instanceof FunctionsHttpError || error instanceof FunctionsRelayError
? error.context
: undefined,
}
}
}
}

View File

@@ -0,0 +1,62 @@
declare namespace Supabase {
export interface ModelOptions {
/**
* Pool embeddings by taking their mean. Applies only for `gte-small` model
*/
mean_pool?: boolean
/**
* Normalize the embeddings result. Applies only for `gte-small` model
*/
normalize?: boolean
/**
* Stream response from model. Applies only for LLMs like `mistral` (default: false)
*/
stream?: boolean
/**
* Automatically abort the request to the model after specified time (in seconds). Applies only for LLMs like `mistral` (default: 60)
*/
timeout?: number
/**
* Mode for the inference API host. (default: 'ollama')
*/
mode?: 'ollama' | 'openaicompatible'
signal?: AbortSignal
}
export class Session {
/**
* Create a new model session using given model
*/
constructor(model: string, sessionOptions?: unknown)
/**
* Execute the given prompt in model session
*/
run(
prompt:
| string
| Omit<import('openai').OpenAI.Chat.ChatCompletionCreateParams, 'model' | 'stream'>,
modelOptions?: ModelOptions
): unknown
}
/**
* Provides AI related APIs
*/
export interface Ai {
readonly Session: typeof Session
}
/**
* Provides AI related APIs
*/
export const ai: Ai
}
declare namespace EdgeRuntime {
export function waitUntil<T>(promise: Promise<T>): Promise<T>
}

14
node_modules/@supabase/functions-js/src/helper.ts generated vendored Normal file
View File

@@ -0,0 +1,14 @@
import { Fetch } from './types'
export const resolveFetch = (customFetch?: Fetch): Fetch => {
let _fetch: Fetch
if (customFetch) {
_fetch = customFetch
} else if (typeof fetch === 'undefined') {
_fetch = (...args) =>
import('@supabase/node-fetch' as any).then(({ default: fetch }) => fetch(...args))
} else {
_fetch = fetch
}
return (...args) => _fetch(...args)
}

10
node_modules/@supabase/functions-js/src/index.ts generated vendored Normal file
View File

@@ -0,0 +1,10 @@
export { FunctionsClient } from './FunctionsClient'
export {
type FunctionInvokeOptions,
FunctionsError,
FunctionsFetchError,
FunctionsHttpError,
FunctionsRelayError,
FunctionRegion,
type FunctionsResponse,
} from './types'

87
node_modules/@supabase/functions-js/src/types.ts generated vendored Normal file
View File

@@ -0,0 +1,87 @@
export type Fetch = typeof fetch
/**
* Response format
*/
export interface FunctionsResponseSuccess<T> {
data: T
error: null
response?: Response
}
export interface FunctionsResponseFailure {
data: null
error: any
response?: Response
}
export type FunctionsResponse<T> = FunctionsResponseSuccess<T> | FunctionsResponseFailure
export class FunctionsError extends Error {
context: any
constructor(message: string, name = 'FunctionsError', context?: any) {
super(message)
this.name = name
this.context = context
}
}
export class FunctionsFetchError extends FunctionsError {
constructor(context: any) {
super('Failed to send a request to the Edge Function', 'FunctionsFetchError', context)
}
}
export class FunctionsRelayError extends FunctionsError {
constructor(context: any) {
super('Relay Error invoking the Edge Function', 'FunctionsRelayError', context)
}
}
export class FunctionsHttpError extends FunctionsError {
constructor(context: any) {
super('Edge Function returned a non-2xx status code', 'FunctionsHttpError', context)
}
}
// Define the enum for the 'region' property
export enum FunctionRegion {
Any = 'any',
ApNortheast1 = 'ap-northeast-1',
ApNortheast2 = 'ap-northeast-2',
ApSouth1 = 'ap-south-1',
ApSoutheast1 = 'ap-southeast-1',
ApSoutheast2 = 'ap-southeast-2',
CaCentral1 = 'ca-central-1',
EuCentral1 = 'eu-central-1',
EuWest1 = 'eu-west-1',
EuWest2 = 'eu-west-2',
EuWest3 = 'eu-west-3',
SaEast1 = 'sa-east-1',
UsEast1 = 'us-east-1',
UsWest1 = 'us-west-1',
UsWest2 = 'us-west-2',
}
export type FunctionInvokeOptions = {
/**
* Object representing the headers to send with the request.
*/
headers?: { [key: string]: string }
/**
* The HTTP verb of the request
*/
method?: 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE'
/**
* The Region to invoke the function in.
*/
region?: FunctionRegion
/**
* The body of the request.
*/
body?:
| File
| Blob
| ArrayBuffer
| FormData
| ReadableStream<Uint8Array>
| Record<string, any>
| string
}

1
node_modules/@supabase/functions-js/src/version.ts generated vendored Normal file
View File

@@ -0,0 +1 @@
export const version = '2.4.5'