Skip to content

Commit

Permalink
fix(ext/core): missing Supabase namespace (#467)
Browse files Browse the repository at this point in the history
* fix(ext/core): missing `Supabase` namespace

* chore: update `global.d.ts`

* chore: update `deno.json`
  • Loading branch information
nyannyacha authored Dec 30, 2024
1 parent 667db65 commit 65c4e17
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"proseWrap": "preserve"
},
"imports": {
"npm:@meowmeow/foobar": "npm:is-odd"
"npm:@meowmeow/foobar": "npm:is-odd",
"openai": "npm:openai"
}
}
4 changes: 2 additions & 2 deletions ext/core/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ import * as WebGPUSurface from 'ext:deno_webgpu/02_surface.js';
import 'ext:sb_ai/js/onnxruntime/cache_adapter.js';

import { SUPABASE_ENV } from 'ext:sb_env/env.js';
import { USER_WORKER_API as ai } from 'ext:sb_ai/js/ai.js';

import { SupabaseEventListener } from 'ext:sb_user_event_worker/event_worker.js';
import { installEdgeRuntimeNamespace } from 'ext:sb_core_main_js/js/edge_runtime.js';
import { installEdgeRuntimeNamespace, installSupabaseNamespace } from 'ext:sb_core_main_js/js/namespaces.js';
import { promiseRejectMacrotaskCallback } from 'ext:sb_core_main_js/js/promises.js';
import { installPromiseHook } from 'ext:sb_core_main_js/js/async_hook.js';
import { registerErrors } from 'ext:sb_core_main_js/js/errors.js';
Expand Down Expand Up @@ -545,6 +544,7 @@ globalThis.bootstrapSBEdge = (opts, ctx) => {

installPromiseHook(kind);
installEdgeRuntimeNamespace(kind, ctx.terminationRequestToken);
installSupabaseNamespace(kind);

ObjectDefineProperty(globalThis, 'SUPABASE_VERSION', readOnly(String(version.runtime)));
ObjectDefineProperty(globalThis, 'DENO_VERSION', readOnly(version.deno));
Expand Down
23 changes: 21 additions & 2 deletions ext/core/js/edge_runtime.js → ext/core/js/namespaces.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { core, primordials } from "ext:core/mod.js";

import { MAIN_WORKER_API as ai } from "ext:sb_ai/js/ai.js";
import { MAIN_WORKER_API, USER_WORKER_API } from "ext:sb_ai/js/ai.js";
import { SUPABASE_USER_WORKERS } from "ext:sb_user_workers/user_workers.js";
import { applySupabaseTag } from "ext:sb_core_main_js/js/http.js";
import { waitUntil } from "ext:sb_core_main_js/js/async_hook.js";
Expand All @@ -20,7 +20,7 @@ function installEdgeRuntimeNamespace(kind, terminationRequestTokenRid) {
switch (kind) {
case "main":
props = {
ai,
ai: MAIN_WORKER_API,
userWorkers: SUPABASE_USER_WORKERS,
getRuntimeMetrics: () => /* async */ ops.op_runtime_metrics(),
applySupabaseTag: (src, dest) => applySupabaseTag(src, dest),
Expand Down Expand Up @@ -55,6 +55,25 @@ function installEdgeRuntimeNamespace(kind, terminationRequestTokenRid) {
});
}

/**
* @param {"user" | "main" | "event"} kind
*/
function installSupabaseNamespace(kind) {
if (kind === "user") {
const props = {
ai: USER_WORKER_API
};

ObjectDefineProperty(globalThis, "Supabase", {
get() {
return props;
},
configurable: true,
});
}
}

export {
installEdgeRuntimeNamespace,
installSupabaseNamespace,
}
14 changes: 7 additions & 7 deletions ext/core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,17 +434,17 @@ deno_core::extension!(
],
esm_entry_point = "ext:sb_core_main_js/js/bootstrap.js",
esm = [
"js/edge_runtime.js",
"js/00_serve.js",
"js/01_http.js",
"js/async_hook.js",
"js/permissions.js",
"js/bootstrap.js",
"js/denoOverrides.js",
"js/errors.js",
"js/fieldUtils.js",
"js/promises.js",
"js/http.js",
"js/denoOverrides.js",
"js/namespaces.js",
"js/navigator.js",
"js/bootstrap.js",
"js/00_serve.js",
"js/01_http.js"
"js/permissions.js",
"js/promises.js",
]
);
49 changes: 49 additions & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,55 @@ declare namespace EdgeRuntime {
export { UserWorker as userWorkers };
}

declare namespace Supabase {
export namespace ai {
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);

/**
* Execute the given prompt in model session
*/
run(
prompt:
| string
| Omit<import('openai').OpenAI.Chat.ChatCompletionCreateParams, 'model' | 'stream'>,
modelOptions?: ModelOptions
): unknown
}
}
}

declare namespace Deno {
export namespace errors {
class WorkerRequestCancelled extends Error { }
Expand Down

0 comments on commit 65c4e17

Please sign in to comment.