Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve type saftey #494

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
574 changes: 468 additions & 106 deletions packages/core/src/types.ts

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions packages/plugin-node/src/services/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateText, trimTokens } from "@ai16z/eliza";
import { generateText, IBrowserService, trimTokens } from "@ai16z/eliza";
import { parseJSONObjectFromText } from "@ai16z/eliza";
import { Service } from "@ai16z/eliza";
import { settings } from "@ai16z/eliza";
@@ -56,7 +56,7 @@ type PageContent = {
bodyContent: string;
};

export class BrowserService extends Service {
export class BrowserService extends Service implements IBrowserService {
private browser: Browser | undefined;
private context: BrowserContext | undefined;
private blocker: PlaywrightBlocker | undefined;
@@ -73,6 +73,10 @@ export class BrowserService extends Service {
return runtime;
}

getInstance(): IBrowserService {
return BrowserService.getInstance();
}

constructor() {
super();
this.browser = undefined;
16 changes: 14 additions & 2 deletions packages/plugin-node/src/services/image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { elizaLogger, models } from "@ai16z/eliza";
import { Service } from "@ai16z/eliza";
import { IAgentRuntime, ModelProviderName, ServiceType } from "@ai16z/eliza";
import {
IAgentRuntime,
ModelProviderName,
ServiceType,
IImageDescriptionService,
} from "@ai16z/eliza";
import {
AutoProcessor,
AutoTokenizer,
@@ -17,7 +22,10 @@ import gifFrames from "gif-frames";
import os from "os";
import path from "path";

export class ImageDescriptionService extends Service {
export class ImageDescriptionService
extends Service
implements IImageDescriptionService
{
static serviceType: ServiceType = ServiceType.IMAGE_DESCRIPTION;

private modelId: string = "onnx-community/Florence-2-base-ft";
@@ -30,6 +38,10 @@ export class ImageDescriptionService extends Service {
private queue: string[] = [];
private processing: boolean = false;

getInstance(): IImageDescriptionService {
return ImageDescriptionService.getInstance();
}

async initialize(runtime: IAgentRuntime): Promise<void> {
this.runtime = runtime;
const model = models[runtime?.character?.modelProvider];
8 changes: 6 additions & 2 deletions packages/plugin-node/src/services/pdf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { IAgentRuntime, Service, ServiceType } from "@ai16z/eliza";
import { IAgentRuntime, IPdfService, Service, ServiceType } from "@ai16z/eliza";
import { getDocument, PDFDocumentProxy } from "pdfjs-dist";
import { TextItem, TextMarkedContent } from "pdfjs-dist/types/src/display/api";

export class PdfService extends Service {
export class PdfService extends Service implements IPdfService {
static serviceType: ServiceType = ServiceType.PDF;

constructor() {
super();
}

getInstance(): IPdfService {
return PdfService.getInstance();
}

async initialize(runtime: IAgentRuntime): Promise<void> {}

async convertPdfToText(pdfBuffer: Buffer): Promise<string> {
13 changes: 11 additions & 2 deletions packages/plugin-node/src/services/speech.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { PassThrough, Readable } from "stream";
import { IAgentRuntime, ISpeechService, ServiceType } from "@ai16z/eliza";
import {
IAgentRuntime,
ISpeechService,
ITranscriptionService,
ServiceType,
} from "@ai16z/eliza";
import { getWavHeader } from "./audioUtils.ts";
import { synthesize } from "../vendor/vits.ts";
import { Service } from "@ai16z/eliza";
@@ -109,11 +114,15 @@ async function textToSpeech(runtime: IAgentRuntime, text: string) {
}
}

export class SpeechService extends Service {
export class SpeechService extends Service implements ISpeechService {
static serviceType: ServiceType = ServiceType.SPEECH_GENERATION;

async initialize(runtime: IAgentRuntime): Promise<void> {}

getInstance(): ISpeechService {
return SpeechService.getInstance();
}

async generate(runtime: IAgentRuntime, text: string): Promise<Readable> {
// check for elevenlabs API key
if (runtime.getSetting("ELEVENLABS_XI_API_KEY")) {
16 changes: 14 additions & 2 deletions packages/plugin-node/src/services/transcription.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { elizaLogger, IAgentRuntime, settings } from "@ai16z/eliza";
import {
elizaLogger,
IAgentRuntime,
ITranscriptionService,
settings,
} from "@ai16z/eliza";
import { Service, ServiceType } from "@ai16z/eliza";
import { exec } from "child_process";
import { File } from "formdata-node";
@@ -16,7 +21,10 @@ const __dirname = path.dirname(__filename);

const execAsync = promisify(exec);

export class TranscriptionService extends Service {
export class TranscriptionService
extends Service
implements ITranscriptionService
{
static serviceType: ServiceType = ServiceType.TRANSCRIPTION;
private CONTENT_CACHE_DIR: string;
private DEBUG_AUDIO_DIR: string;
@@ -29,6 +37,10 @@ export class TranscriptionService extends Service {

async initialize(runtime: IAgentRuntime): Promise<void> {}

getInstance(): ITranscriptionService {
return TranscriptionService.getInstance();
}

constructor() {
super();
const rootDir = path.resolve(__dirname, "../../");
9 changes: 7 additions & 2 deletions packages/plugin-node/src/services/video.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import {
ITranscriptionService,
Media,
ServiceType,
IVideoService,
} from "@ai16z/eliza";
import { stringToUuid } from "@ai16z/eliza";
import ffmpeg from "fluent-ffmpeg";
@@ -12,7 +13,7 @@ import path from "path";
import { tmpdir } from "os";
import youtubeDl from "youtube-dl-exec";

export class VideoService extends Service {
export class VideoService extends Service implements IVideoService {
static serviceType: ServiceType = ServiceType.VIDEO;
private cacheKey = "content/video";
private dataDir = "./content_cache";
@@ -25,6 +26,10 @@ export class VideoService extends Service {
this.ensureDataDirectoryExists();
}

getInstance(): IVideoService {
return VideoService.getInstance();
}

async initialize(runtime: IAgentRuntime): Promise<void> {}

private ensureDataDirectoryExists() {
@@ -88,7 +93,7 @@ export class VideoService extends Service {

public async processVideo(
url: string,
runtime: IAgentRuntime
runtime?: IAgentRuntime
): Promise<Media> {
this.queue.push(url);
this.processQueue(runtime);