File size: 5,466 Bytes
c212805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
declare type BeforeunloadReason =
  | 'cpu'
  | 'memory'
  | 'wall_clock'
  | 'early_drop'
  | 'termination'
  | (string & {})

declare interface WindowEventMap {
  load: Event
  unload: Event
  beforeunload: CustomEvent<BeforeunloadReason>
  drain: Event
}

// TODO(Nyannyacha): These two type defs will be provided later.

// deno-lint-ignore no-explicit-any
type S3FsConfig = any

// deno-lint-ignore no-explicit-any
type TmpFsConfig = any

type OtelPropagators = 'TraceContext' | 'Baggage'
type OtelConsoleConfig = 'Ignore' | 'Capture' | 'Replace'
type OtelConfig = {
  tracing_enabled?: boolean
  metrics_enabled?: boolean
  console?: OtelConsoleConfig
  propagators?: OtelPropagators[]
}

interface UserWorkerFetchOptions {
  signal?: AbortSignal
}

interface PermissionsOptions {
  allow_all?: boolean | null
  allow_env?: string[] | null
  deny_env?: string[] | null
  allow_net?: string[] | null
  deny_net?: string[] | null
  allow_ffi?: string[] | null
  deny_ffi?: string[] | null
  allow_read?: string[] | null
  deny_read?: string[] | null
  allow_run?: string[] | null
  deny_run?: string[] | null
  allow_sys?: string[] | null
  deny_sys?: string[] | null
  allow_write?: string[] | null
  deny_write?: string[] | null
  allow_import?: string[] | null
}

interface UserWorkerCreateContext {
  sourceMap?: boolean | null
  importMapPath?: string | null
  shouldBootstrapMockFnThrowError?: boolean | null
  suppressEszipMigrationWarning?: boolean | null
  useReadSyncFileAPI?: boolean | null
  supervisor?: {
    requestAbsentTimeoutMs?: number | null
  }
  otel?: {
    [attribute: string]: string
  }
}

interface UserWorkerCreateOptions {
  servicePath?: string | null
  envVars?: string[][] | [string, string][] | null
  noModuleCache?: boolean | null

  forceCreate?: boolean | null
  allowRemoteModules?: boolean | null
  customModuleRoot?: string | null
  permissions?: PermissionsOptions | null

  maybeEszip?: Uint8Array | null
  maybeEntrypoint?: string | null
  maybeModuleCode?: string | null

  memoryLimitMb?: number | null
  lowMemoryMultiplier?: number | null
  workerTimeoutMs?: number | null
  cpuTimeSoftLimitMs?: number | null
  cpuTimeHardLimitMs?: number | null
  staticPatterns?: string[] | null

  s3FsConfig?: S3FsConfig | null
  tmpFsConfig?: TmpFsConfig | null
  otelConfig?: OtelConfig | null

  context?: UserWorkerCreateContext | null
}

interface HeapStatistics {
  totalHeapSize: number
  totalHeapSizeExecutable: number
  totalPhysicalSize: number
  totalAvailableSize: number
  totalGlobalHandlesSize: number
  usedGlobalHandlesSize: number
  usedHeapSize: number
  mallocedMemory: number
  externalMemory: number
  peakMallocedMemory: number
}

interface RuntimeMetrics {
  mainWorkerHeapStats: HeapStatistics
  eventWorkerHeapStats?: HeapStatistics
}

interface MemInfo {
  total: number
  free: number
  available: number
  buffers: number
  cached: number
  swapTotal: number
  swapFree: number
}

declare namespace EdgeRuntime {
  export namespace ai {
    function tryCleanupUnusedSession(): Promise<number>
  }

  class UserWorker {
    constructor(key: string)

    fetch(request: Request, options?: UserWorkerFetchOptions): Promise<Response>

    static create(opts: UserWorkerCreateOptions): Promise<UserWorker>
    static tryCleanupIdleWorkers(timeoutMs: number): Promise<number>
  }

  export function scheduleTermination(): void
  export function waitUntil<T>(promise: Promise<T>): Promise<T>
  export function getRuntimeMetrics(): Promise<RuntimeMetrics>
  export function applySupabaseTag(src: Request, dest: Request): void
  export function systemMemoryInfo(): MemInfo
  export function raiseSegfault(): void

  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 {}
    class WorkerAlreadyRetired extends Error {}

    /** Thrown when an outbound HTTP request is blocked by the rate limiter. */
    class RateLimitError extends Error {
      /**
       * Number of milliseconds until the rate-limit window resets.
       * `null` if the reset time could not be determined.
       */
      retryAfterMs: number | null
      constructor(message: string, retryAfterMs?: number)
    }
  }
}