mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(apis/websocket): support training without label
This commit is contained in:
@@ -23,12 +23,12 @@ export const wit = {
|
|||||||
|
|
||||||
if (!res.ok) throw new Error(`Failed to fetch from Wit.ai: ${res.statusText} (${res.status})`)
|
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) {
|
message(text: string) {
|
||||||
return this.fetch(`/message?q=${encodeURIComponent(text)}&n=8`) as Promise<WitMessageResponse>
|
return this.fetch(`/message?q=${encodeURIComponent(text)}&n=8`) as Promise<WitMessageResponse>
|
||||||
},
|
},
|
||||||
async train(text: string, label: string) {
|
async train(text: string, label?: string) {
|
||||||
await this.fetch('/utterances', {
|
await this.fetch('/utterances', {
|
||||||
body: JSON.stringify([
|
body: JSON.stringify([
|
||||||
{
|
{
|
||||||
@@ -41,7 +41,14 @@ export const wit = {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
} as const
|
} satisfies Wit
|
||||||
|
|
||||||
|
export interface Wit {
|
||||||
|
token: string
|
||||||
|
fetch(route: string, options?: RequestInit): Promise<WitMessageResponse>
|
||||||
|
message(text: string): Promise<WitMessageResponse>
|
||||||
|
train(text: string, label?: string): Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
export interface WitMessageResponse {
|
export interface WitMessageResponse {
|
||||||
text: string
|
text: string
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { ClientOperation, Logger } from '@revanced/bot-shared'
|
import type { ClientOperation, Logger } from '@revanced/bot-shared'
|
||||||
import type { Worker as TesseractWorker } from 'tesseract.js'
|
import type { Worker as TesseractWorker } from 'tesseract.js'
|
||||||
import type { ClientPacketObject } from '../classes/Client'
|
import type { ClientPacketObject } from '../classes/Client'
|
||||||
import type { WitMessageResponse } from '../context'
|
import type { Wit } from '../context'
|
||||||
import type { Config } from '../utils/config'
|
import type { Config } from '../utils/config'
|
||||||
|
|
||||||
export { default as parseImageEventHandler } from './parseImage'
|
export { default as parseImageEventHandler } from './parseImage'
|
||||||
@@ -14,10 +14,7 @@ export type EventHandler<POp extends ClientOperation> = (
|
|||||||
) => void | Promise<void>
|
) => void | Promise<void>
|
||||||
|
|
||||||
export type EventContext = {
|
export type EventContext = {
|
||||||
wit: {
|
wit: Wit
|
||||||
train(text: string, label: string): Promise<void>
|
|
||||||
message(text: string): Promise<WitMessageResponse>
|
|
||||||
}
|
|
||||||
tesseract: TesseractWorker
|
tesseract: TesseractWorker
|
||||||
logger: Logger
|
logger: Logger
|
||||||
config: Config
|
config: Config
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = asy
|
|||||||
const nextSeq = client.currentSequence++
|
const nextSeq = client.currentSequence++
|
||||||
const actualText = text.slice(0, 279)
|
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 ?? '<out of scope>'} (${nextSeq}) with:`, actualText)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await wit.train(actualText, label)
|
await wit.train(actualText, label)
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
import {
|
import {
|
||||||
type AnySchema,
|
|
||||||
array,
|
array,
|
||||||
type BooleanSchema,
|
|
||||||
boolean,
|
boolean,
|
||||||
custom,
|
custom,
|
||||||
enum_,
|
enum_,
|
||||||
type InferOutput,
|
type InferOutput,
|
||||||
type NullSchema,
|
|
||||||
null_,
|
null_,
|
||||||
type ObjectSchema,
|
|
||||||
object,
|
object,
|
||||||
|
optional,
|
||||||
parse,
|
parse,
|
||||||
pipe,
|
pipe,
|
||||||
string,
|
string,
|
||||||
@@ -75,13 +72,9 @@ export const PacketDataSchemas = {
|
|||||||
}),
|
}),
|
||||||
[ClientOperation.TrainMessage]: object({
|
[ClientOperation.TrainMessage]: object({
|
||||||
text: string(),
|
text: string(),
|
||||||
label: string(),
|
label: optional(string()),
|
||||||
}),
|
}),
|
||||||
} as const satisfies Record<
|
} as const
|
||||||
Operation,
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: This is a schema, it's not possible to type it
|
|
||||||
ObjectSchema<any, any> | AnySchema | NullSchema<any> | BooleanSchema<any>
|
|
||||||
>
|
|
||||||
|
|
||||||
export type Packet<TOp extends Operation = Operation> = TOp extends ServerOperation
|
export type Packet<TOp extends Operation = Operation> = TOp extends ServerOperation
|
||||||
? PacketWithSequenceNumber<TOp>
|
? PacketWithSequenceNumber<TOp>
|
||||||
|
|||||||
Reference in New Issue
Block a user