diff --git a/apis/websocket/src/context.ts b/apis/websocket/src/context.ts index 449c52f..b58ee4e 100644 --- a/apis/websocket/src/context.ts +++ b/apis/websocket/src/context.ts @@ -23,12 +23,12 @@ export const wit = { if (!res.ok) throw new Error(`Failed to fetch from Wit.ai: ${res.statusText} (${res.status})`) - return await res.json() + return (await res.json()) as WitMessageResponse }, message(text: string) { return this.fetch(`/message?q=${encodeURIComponent(text)}&n=8`) as Promise }, - async train(text: string, label: string) { + async train(text: string, label?: string) { await this.fetch('/utterances', { body: JSON.stringify([ { @@ -41,7 +41,14 @@ export const wit = { method: 'POST', }) }, -} as const +} satisfies Wit + +export interface Wit { + token: string + fetch(route: string, options?: RequestInit): Promise + message(text: string): Promise + train(text: string, label?: string): Promise +} export interface WitMessageResponse { text: string diff --git a/apis/websocket/src/events/index.ts b/apis/websocket/src/events/index.ts index 38d4a88..183b014 100755 --- a/apis/websocket/src/events/index.ts +++ b/apis/websocket/src/events/index.ts @@ -1,7 +1,7 @@ import type { ClientOperation, Logger } from '@revanced/bot-shared' import type { Worker as TesseractWorker } from 'tesseract.js' import type { ClientPacketObject } from '../classes/Client' -import type { WitMessageResponse } from '../context' +import type { Wit } from '../context' import type { Config } from '../utils/config' export { default as parseImageEventHandler } from './parseImage' @@ -14,10 +14,7 @@ export type EventHandler = ( ) => void | Promise export type EventContext = { - wit: { - train(text: string, label: string): Promise - message(text: string): Promise - } + wit: Wit tesseract: TesseractWorker logger: Logger config: Config diff --git a/apis/websocket/src/events/trainMessage.ts b/apis/websocket/src/events/trainMessage.ts index 3b02da9..5068de7 100644 --- a/apis/websocket/src/events/trainMessage.ts +++ b/apis/websocket/src/events/trainMessage.ts @@ -10,7 +10,7 @@ const trainMessageEventHandler: EventHandler = asy const nextSeq = client.currentSequence++ const actualText = text.slice(0, 279) - logger.debug(`${client.id} requested to train label ${label} (${nextSeq}) with:`, actualText) + logger.debug(`${client.id} requested to train label ${label ?? ''} (${nextSeq}) with:`, actualText) try { await wit.train(actualText, label) diff --git a/packages/shared/src/schemas/Packet.ts b/packages/shared/src/schemas/Packet.ts index aeffaea..aaddca8 100755 --- a/packages/shared/src/schemas/Packet.ts +++ b/packages/shared/src/schemas/Packet.ts @@ -1,15 +1,12 @@ import { - type AnySchema, array, - type BooleanSchema, boolean, custom, enum_, type InferOutput, - type NullSchema, null_, - type ObjectSchema, object, + optional, parse, pipe, string, @@ -75,13 +72,9 @@ export const PacketDataSchemas = { }), [ClientOperation.TrainMessage]: object({ text: string(), - label: string(), + label: optional(string()), }), -} as const satisfies Record< - Operation, - // biome-ignore lint/suspicious/noExplicitAny: This is a schema, it's not possible to type it - ObjectSchema | AnySchema | NullSchema | BooleanSchema -> +} as const export type Packet = TOp extends ServerOperation ? PacketWithSequenceNumber