diff --git a/src/client/AiplClientEvents.ts b/src/client/AiplClientEvents.ts deleted file mode 100644 index 99ad916..0000000 --- a/src/client/AiplClientEvents.ts +++ /dev/null @@ -1,56 +0,0 @@ -// import type { CustomEventHandler } from "@mjtdev/engine/packages/mjtdev-reacts/dist/hook/addCustomEventListener"; -import type { - AiFunctionCall, - AppMessage, - AppMessageMap, - DataObject, -} from "ai-worker-common"; -import { EventEmitter } from "../common/EventEmitter"; - -export type AiplClientEventMap = AppMessageMap & { - log: string; - toast: string; - ping: string; - error: unknown; - message: AppMessage; - "abort-generation": void; - "finished-generation": void; - "new-chat": void; - "chat-list-updated": void; - "scroll-message-into-view": string; - "stop-speaking": void; - asrUtterance: string; - asrMumble: string; - asrAudioWav: ArrayBuffer; - ttsAudioWav: ArrayBuffer; - ttsBlobsUpdated: void; - ttsStopped: void; - ttsStarted: void; - asrStarted: void; - aiResponseFragment: { - value: string | undefined; - time: number; - voiceId?: string; - }; - ttsBlobReady: string; - aiFunctionCalled: { name: string; arg: string }; - "return:dataObject": DataObject; - "function:call": AiFunctionCall; - dataObjectUpdate: { id: string; value?: DataObject }; - dataLinksUpdated: void; - "chat:debug": AppMessageMap["chat:debug"]; - "app:performance": AppMessageMap["app:performance"]; - return: AppMessageMap["return"]; - "dataObject:update": AppMessageMap["dataObject:update"]; - "swr:updated": { key: string; data: unknown }; - "asr:response": AppMessageMap["asr:response"]; - aiplEditorUpdate: { - characterId: string; - fieldName: string; - value: string | ((cur: string) => string); - }; -}; -export type AppEventType = keyof AiplClientEventMap; - -export const EVENTS = new EventEmitter(); -// addLogListener(); diff --git a/src/client/AiplClients.ts b/src/client/AiplClients.ts index e8ca3a4..fb3ef0d 100644 --- a/src/client/AiplClients.ts +++ b/src/client/AiplClients.ts @@ -7,28 +7,7 @@ import { log } from "./log"; export type AiplClient = ReturnType; -// export type Capabilities = {}; - -// export type AiplClientContext = ReturnType; - -// export const createClientContext = (init: AiplClientState = {}) => { -// const [_, update, get] = createState(init); - -// return Object.freeze({ update, get }); -// }; - -export const createAiplClient = ( - props: Partial<{ authToken: string; url: string }> = {} -) => { - const { url } = props; - // const ctx = createClientContext({ homebaseUrl: url }); - - // ctx.update((s) => { - // s.ws = getAppState().ws; - // }); - - // connectWs(ctx); - +export const createAiplClient = (props: Partial<{}> = {}) => { return { ask: async (props: AppMessageMap["chat:ask"]) => { const result = await AppMessagesState.call("chat:ask", props); diff --git a/src/client/addReturnListener.ts b/src/client/addReturnListener.ts deleted file mode 100644 index c12e21d..0000000 --- a/src/client/addReturnListener.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { EVENTS } from "./AiplClientEvents"; - -export const addReturnListener = ({ - onReturn, - maxWaitMs = 60 * 1000, - returnId = `return-${Date.now()}-${crypto.randomUUID()}`, - onTimeout = () => {}, - stream = false, -}: { - onReturn: ( - data: T - ) => Promise | void | undefined | boolean; - onTimeout?: () => void; - maxWaitMs?: number; - returnId?: string; - stream?: boolean; -}) => { - let disposed = false; - const disposer = EVENTS.on("return", async (evt) => { - const { returnId: remoteReturnId, data } = evt.detail; - if (remoteReturnId === returnId) { - const shouldDispose = await onReturn(data as T); - if (stream && !shouldDispose) { - return; - } - disposer(); - disposed = true; - } - }); - setTimeout(() => { - if (disposed) { - return; - } - - disposer(); - onTimeout(); - }, maxWaitMs); - return returnId; -}; diff --git a/src/client/authTokenToAuthHeader.ts b/src/client/authTokenToAuthHeader.ts deleted file mode 100644 index 8eb2863..0000000 --- a/src/client/authTokenToAuthHeader.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const authTokenToAuthHeader = (authToken: string) => { - return { Authorization: `Bearer ${authToken}` }; -}; diff --git a/src/client/fetchBackend.ts b/src/client/fetchBackend.ts deleted file mode 100644 index d49f6ba..0000000 --- a/src/client/fetchBackend.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { AiplClientContext } from "./AiplClients"; -import { authTokenToAuthHeader } from "./authTokenToAuthHeader"; - - -export const fetchBackend = ( - ctx: AiplClientContext, - path: string, - init: RequestInit = {} -) => { - const { homebaseUrl, authToken } = ctx.get(); - - if (!authToken) { - throw new Error("Not logged in, unable get user"); - } - - const headers = authTokenToAuthHeader(authToken); - return fetch(homebaseUrl + path, { - headers: { - ...headers, - ...(init.headers ?? {}), - }, - }); -}; diff --git a/src/client/getBackendUser.ts b/src/client/getBackendUser.ts deleted file mode 100644 index ec0f99a..0000000 --- a/src/client/getBackendUser.ts +++ /dev/null @@ -1,30 +0,0 @@ -// import type { AppUser } from "ai-worker-common"; -// import type { AiplClientContext } from "./AiplClients"; -// import { authTokenToAuthHeader } from "./authTokenToAuthHeader"; - -// export const getBackendUser = async ( -// ctx: AiplClientContext -// ): Promise => { -// const { homebaseUrl, authToken } = ctx.get(); - -// if (!authToken) { -// console.log("getBackendUser: no authToken, refusing"); -// return undefined; -// } - -// const headers = authTokenToAuthHeader(authToken); -// const url = homebaseUrl + "/user"; - -// const resp = await fetch(url, { -// method: "GET", -// headers, -// }); -// if (!resp.ok) { -// throw new Error( -// `Bad response from backend getting user: ${resp.statusText}`, -// { cause: resp } -// ); -// } -// const text = await resp.text(); -// return JSON.parse(text); -// }; diff --git a/src/client/papAuth.ts b/src/client/papAuth.ts deleted file mode 100644 index cfe5584..0000000 --- a/src/client/papAuth.ts +++ /dev/null @@ -1,27 +0,0 @@ -import type { AiplClientContext } from "./AiplClients"; -import { call } from "../state/ws/call"; - -export const papAuth = async ({ - accessPointId, - ctx, -}: { - accessPointId: string; - ctx: AiplClientContext; -}) => { - const resp = (await call(ctx, "pap:auth", { - accessPointId, - params: {}, - })) as { authToken: string }; - - const { authToken } = resp; - console.log("papAuth: authToken", { authToken }); - - if (typeof authToken !== "string") { - console.log("userLogin: no authToken, refusing", { authToken }); - throw new Error("No auth token received"); - } - ctx.update((s) => { - s.authToken = authToken; - }); - return authToken; -}; diff --git a/src/client/sendWsAuth.ts b/src/client/sendWsAuth.ts deleted file mode 100644 index af69dc9..0000000 --- a/src/client/sendWsAuth.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { waitTimeout } from "@mjtdev/engine"; -import { type AppMessage, AppMessages } from "ai-worker-common"; -import type { AiplClientContext } from "./AiplClients"; -import { log } from "./log"; - -export const sendWsAuth = async ( - ctx: AiplClientContext, - authToken: string | undefined -) => { - const { ws } = ctx.get(); - if (!authToken) { - log("sendWsAuth: refusing, no authToken"); - return; - } - if (!ws) { - log("sendWsAuth: refusing, no ws"); - return; - } - const authMessage: AppMessage<"auth"> = { - type: "auth", - detail: authToken, - }; - AppMessages.dispatch(ws, authMessage); - await waitTimeout(1 * 1000); -}; diff --git a/src/client/setupWsOnMessage.ts b/src/client/setupWsOnMessage.ts deleted file mode 100644 index feb8263..0000000 --- a/src/client/setupWsOnMessage.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { Bytes } from "@mjtdev/engine"; -import type { AppMessage } from "ai-worker-common"; -import { EVENTS } from "./AiplClientEvents"; -import { log } from "./log"; - -export const setupWsOnMessage = (ws: WebSocket) => { - const handler = (evt: WebSocketEventMap["message"]) => { - const { data } = evt; - // console.log("ws message data", data); - // if (typeof data !== "string") { - // Apps.error( - // `setupWsOnMessage: unexpected ws message type: ${typeof data}` - // ); - // return; - // } - // const message = JSON.parse(data) as AppMessage; - - if (data instanceof ArrayBuffer) { - // Apps.error( - // `setupWsOnMessage: unexpected ws message type: ${typeof data}` - // ); - // const message = JSON.parse(data) as AppMessage; - const message = Bytes.msgPackToObject(new Uint8Array(data)); - // log("setupWsOnMessage: message received", message); - EVENTS.emit(message.type, message.detail); - return; - } else { - log("unexpected ws message data", data); - } - // const message = JSON.parse(data) as AppMessage; - // AppEvents.dispatchEvent("message", message); - }; - ws.addEventListener("message", handler); - return handler; -}; diff --git a/src/client/userLogin.ts b/src/client/userLogin.ts deleted file mode 100644 index 15cd8b1..0000000 --- a/src/client/userLogin.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { AiplClientContext } from "./AiplClients"; -import { call } from "../state/ws/call"; - -export const userLogin = async ({ - userName, - password, - ctx, -}: { - userName: string; - password: string; - ctx: AiplClientContext; -}) => { - const authToken = await call(ctx, "auth", { userName, password }); - - if (typeof authToken !== "string") { - console.log("userLogin: no authToken, refusing", { authToken }); - throw new Error("No auth token received"); - } - ctx.update((s) => { - s.authToken = authToken; - }); - return authToken; -}; diff --git a/src/client/waitForWs.ts b/src/client/waitForWs.ts deleted file mode 100644 index 45e1f3a..0000000 --- a/src/client/waitForWs.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { isDefined, waitTimeout } from "@mjtdev/engine"; -import type { AiplClientContext } from "./AiplClients"; - -export const waitForWs = async ( - ctx: AiplClientContext, - options: Partial<{ maxWaitSeconds: number }> = {} -): Promise => { - const { ws } = ctx.get(); - - const { maxWaitSeconds = 10 } = options; - - if (maxWaitSeconds <= 0) { - throw new Error(`waitForWs: max wait reached, no ws available`); - } - - if (isDefined(ws)) { - console.log("websocket ready"); - return ws; - } - await waitTimeout(1 * 1000); - return waitForWs(ctx, { maxWaitSeconds: maxWaitSeconds - 1 }); -}; diff --git a/src/state/ws/call.ts b/src/state/ws/call.ts index 0b811f5..d29548b 100644 --- a/src/state/ws/call.ts +++ b/src/state/ws/call.ts @@ -1,5 +1,5 @@ import type { AppMessageMap } from "ai-worker-common"; -import { addReturnListener } from "../../client/addReturnListener"; +import { addReturnListener } from "../data-object/addReturnListener"; import { dispatch } from "./AppMessagesState"; export const call = (