Skip to content

Commit

Permalink
Use types instead of making them
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinBrett committed Sep 10, 2024
1 parent eb180b3 commit 7f1ff21
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 37 deletions.
15 changes: 8 additions & 7 deletions components/system/Taskbar/AI/ai.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import {
} from "@mlc-ai/web-llm";
import {
type WorkerMessage,
type AITextSession,
type ConvoStyles,
type AITextSessionOptions,
} from "components/system/Taskbar/AI/types";

const MARKED_LIBS = [
"/Program Files/Marked/marked.min.js",
"/Program Files/Marked/purify.min.js",
];

const CONVO_STYLE_TEMPS: Record<ConvoStyles, AITextSessionOptions> = {
const CONVO_STYLE_TEMPS: Record<
ConvoStyles,
AIAssistantCreateOptionsWithSystemPrompt
> = {
balanced: {
temperature: 0.5,
topK: 3,
Expand Down Expand Up @@ -47,7 +48,7 @@ let cancel = false;
let responding = false;

let sessionId = 0;
let session: AITextSession | ChatCompletionMessageParam[] | undefined;
let session: AIAssistant | ChatCompletionMessageParam[] | undefined;
let engine: MLCEngine;

let markedLoaded = false;
Expand All @@ -66,9 +67,9 @@ globalThis.addEventListener(
sessionId = data.id;

if (data.hasWindowAI) {
(session as AITextSession)?.destroy();
(session as AIAssistant)?.destroy();

const config: AITextSessionOptions = {
const config: AIAssistantCreateOptionsWithSystemPrompt = {
...CONVO_STYLE_TEMPS[data.style],
systemPrompt: SYSTEM_PROMPT.content,
};
Expand Down Expand Up @@ -101,7 +102,7 @@ globalThis.addEventListener(
if (data.hasWindowAI) {
response =
// eslint-disable-next-line no-await-in-loop
(await (session as AITextSession)?.prompt(data.text)) || "";
(await (session as AIAssistant)?.prompt(data.text)) || "";
} else {
(session as ChatCompletionMessageParam[]).push({
content: data.text,
Expand Down
33 changes: 3 additions & 30 deletions components/system/Taskbar/AI/types.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import { type ChatCompletionMessageParam } from "@mlc-ai/web-llm";
import { type MarkedOptions } from "components/apps/Marked/useMarked";

type AIAssistantPromptOptions = {
signal?: AbortSignal;
};
/// <reference types="dom-chromium-ai" />

export type AITextSession = {
destroy: () => void;
prompt: (
message: string,
options?: AIAssistantPromptOptions
) => Promise<string>;
};

export type AITextSessionOptions = {
initialPrompts?: ChatCompletionMessageParam[];
systemPrompt?: string;
temperature: number;
topK: number;
};

type AICapabilityAvailability = "readily" | "after-download" | "no";

type AI = {
assistant: {
capabilities: () => Promise<{ available: AICapabilityAvailability }>;
create: (config?: Partial<AITextSessionOptions>) => Promise<AITextSession>;
};
};
import { type MarkedOptions } from "components/apps/Marked/useMarked";

declare global {
/* eslint-disable vars-on-top, no-var */
var ai: AI;
var ai: { assistant: AIAssistantFactory };
var marked: {
parse: (markdownString: string, options: MarkedOptions) => string;
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@next/bundle-analyzer": "^14.2.7",
"@next/eslint-plugin-next": "^14.2.7",
"@playwright/test": "1.44.1",
"@types/dom-chromium-ai": "^0.0.0",
"@types/dompurify": "^3.0.5",
"@types/gif.js": "^0.2.5",
"@types/ini": "^4.1.1",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,11 @@
dependencies:
"@babel/types" "^7.20.7"

"@types/dom-chromium-ai@^0.0.0":
version "0.0.0"
resolved "https://registry.yarnpkg.com/@types/dom-chromium-ai/-/dom-chromium-ai-0.0.0.tgz#003f26741ca5d1726a5da66025b72c3e02e4b992"
integrity sha512-hDYiRUtSWuDqXgjpltqX3n60FRsj6PNivRL2w5QAYOiNstdcjkKq9YYC6VP7nbtVO2F+55wf/TCBXAK8L3HBjA==

"@types/dompurify@^3.0.5":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/dompurify/-/dompurify-3.0.5.tgz#02069a2fcb89a163bacf1a788f73cb415dd75cb7"
Expand Down

0 comments on commit 7f1ff21

Please sign in to comment.