Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web/src/apps/code/bridge.ts from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 5.5 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/code/bridge.ts
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/web/src/apps/code/bridge.ts
-
curl -L -o bridge.ts https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/code/bridge.ts
5.5 kB
| import { CODE_APP_TOOLS } from "./bridgeTools"; | |
| export const CODE_APP_CALLS = [ | |
| "databases", "describe", "rows", "totals", | |
| "addRecord", "editRecord", "editRecords", "deleteRecord", | |
| ...CODE_APP_TOOLS, | |
| ] as const; | |
| export type BridgeCall = typeof CODE_APP_CALLS[number]; | |
| export type BridgeError = { code: string; message: string }; | |
| export type BridgeRequest = { id: string | number; call: BridgeCall; args: unknown[] }; | |
| export type BridgeAnswer = { id: string | number } & ( | |
| | { ok: true; result: unknown } | |
| | { ok: false; error: BridgeError } | |
| ); | |
| export type Database = { key: string; label: string }; | |
| export type BridgeField = { key: string; label?: string; type?: string }; | |
| export type DatabaseDescription = { | |
| key: string; fields: BridgeField[]; identity: { pid: string }; recordsMutable: boolean; | |
| }; | |
| export type RowOptions = { | |
| filters?: unknown[]; sorts?: unknown[]; fields?: string[]; limit?: number; cursor?: string | null; | |
| }; | |
| export type RowPage = { | |
| fields: BridgeField[]; rows: Record<string, unknown>[]; total: number; | |
| nextCursor: string | null; limits: unknown[]; | |
| }; | |
| export type Aggregate = ({ colId: string; agg: string } | { field: string; fn: string }) & { p?: number }; | |
| export type TotalOptions = { aggregates?: Aggregate[]; groupBy?: string | string[]; filters?: unknown[] }; | |
| export type Totals = { | |
| total_rows: number; aggregates: Record<string, unknown>; | |
| groups: unknown[] | null; total_groups?: number; limits: unknown[]; | |
| }; | |
| export type AddedRecord = { rid: string; pid: number }; | |
| export type EditedRecord = { pid: number; updates: Record<string, unknown> }; | |
| export interface Loopable { | |
| databases(): Promise<Database[]>; | |
| describe(database: string): Promise<DatabaseDescription>; | |
| rows(database: string, options?: RowOptions): Promise<RowPage>; | |
| totals(database: string, options?: TotalOptions): Promise<Totals>; | |
| addRecord(database: string, values: Record<string, unknown>): Promise<AddedRecord>; | |
| editRecord(database: string, id: string | number, values: Record<string, unknown>): Promise<EditedRecord>; | |
| editRecords(database: string, records: { id: string | number; values: Record<string, unknown> }[]): Promise<EditedRecord[]>; | |
| deleteRecord(database: string, id: string | number): Promise<{ ok: boolean }>; | |
| tools: { | |
| listBrainFiles(): Promise<unknown>; | |
| readBrainFile(path: string): Promise<unknown>; | |
| sendRequest(request: { to: string; title: string; body: string }): Promise<unknown>; | |
| createSchedule(schedule: { database: string; payload: unknown; when: string }): Promise<unknown>; | |
| }; | |
| } | |
| // This function is serialized into the child document and must have no module dependencies. | |
| function installLoopable() { | |
| const host = window.parent.parent; | |
| const pending = new Map<string, { resolve: (value: unknown) => void; reject: (error: Error) => void }>(); | |
| let sequence = 0; | |
| const invoke = (call: string, args: unknown[]): Promise<unknown> => new Promise((resolve, reject) => { | |
| const id = `loopable:${++sequence}`; | |
| pending.set(id, { resolve, reject }); | |
| try { | |
| host.postMessage({ id, call, args }, "*"); | |
| } catch { | |
| pending.delete(id); | |
| reject(Object.assign(new Error("This request could not be sent."), { code: "bridge_unavailable" })); | |
| } | |
| }); | |
| window.addEventListener("message", (event) => { | |
| if (event.source !== host) return; | |
| const answer = event.data; | |
| if (!answer || typeof answer !== "object" || typeof answer.id !== "string" || typeof answer.ok !== "boolean") return; | |
| const request = pending.get(answer.id); | |
| if (!request) return; | |
| if (answer.ok && Object.hasOwn(answer, "result")) { | |
| pending.delete(answer.id); | |
| request.resolve(answer.result); | |
| } else if (!answer.ok && typeof answer.error?.code === "string" && typeof answer.error?.message === "string") { | |
| pending.delete(answer.id); | |
| request.reject(Object.assign(new Error(answer.error.message), { code: answer.error.code })); | |
| } | |
| }); | |
| window.addEventListener("pagehide", () => { | |
| for (const request of pending.values()) { | |
| request.reject(Object.assign(new Error("This App was closed."), { code: "bridge_closed" })); | |
| } | |
| pending.clear(); | |
| }); | |
| const loopable = { | |
| databases: () => invoke("databases", []), | |
| describe: (database: string) => invoke("describe", [database]), | |
| rows: (database: string, options: RowOptions = {}) => invoke("rows", [database, options]), | |
| totals: (database: string, options: TotalOptions = {}) => invoke("totals", [database, options]), | |
| addRecord: (database: string, values: unknown) => invoke("addRecord", [database, values]), | |
| editRecord: (database: string, id: unknown, values: unknown) => invoke("editRecord", [database, id, values]), | |
| editRecords: (database: string, records: unknown) => invoke("editRecords", [database, records]), | |
| deleteRecord: (database: string, id: unknown) => invoke("deleteRecord", [database, id]), | |
| tools: Object.freeze({ | |
| listBrainFiles: () => invoke("tools.listBrainFiles", []), | |
| readBrainFile: (path: string) => invoke("tools.readBrainFile", [path]), | |
| sendRequest: (request: unknown) => invoke("tools.sendRequest", [request]), | |
| createSchedule: (schedule: unknown) => invoke("tools.createSchedule", [schedule]), | |
| }), | |
| }; | |
| Object.defineProperty(window, "loopable", { value: Object.freeze(loopable), writable: false, configurable: false }); | |
| } | |
| export function bridgeScript(): string { | |
| return `(${installLoopable.toString()})();`.replace(/</g, "\\u003c"); | |
| } | |