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

refactor: migrate background router to trpc #21

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion apps/ai-assistant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
"@douyinfe/semi-icons": "^2.51.3",
"@douyinfe/semi-ui": "^2.51.3",
"@google/generative-ai": "^0.1.3",
"@trpc/client": "11.0.0-next-beta.248",
"@trpc/server": "11.0.0-next-beta.248",
"@webx-kit/messaging": "workspace:^",
"@webx-kit/runtime": "workspace:^",
"@webx-kit/storage": "workspace:^",
"clsx": "^2.1.0",
"jotai": "^2.6.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@modern-js/app-tools": "^2.46.1",
Expand Down
7 changes: 6 additions & 1 deletion apps/ai-assistant/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import './router';
import { createTrpcHandler } from '@webx-kit/messaging/background';
import { appRouter } from './router';

createTrpcHandler({
router: appRouter,
});
62 changes: 31 additions & 31 deletions apps/ai-assistant/src/background/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { GoogleGenerativeAI } from '@google/generative-ai';
import { setStreamHandler } from '@webx-kit/messaging/background';
import { apiKeyAtom } from '@/hooks/atoms/config';
import { atom, getDefaultStore } from 'jotai';
import { initTRPC } from '@trpc/server';
import { z } from 'zod';
import { observable } from '@trpc/server/observable';
import { genAI } from '@/background/shared';

const store = getDefaultStore();
const t = initTRPC.create();

const genAIAtom = atom(async (get) => {
const apiKey = await get(apiKeyAtom);
return apiKey ? new GoogleGenerativeAI(apiKey) : null;
});

let genAI: GoogleGenerativeAI | null = null;
const updateGenAIInstance = () => store.get(genAIAtom).then((instance) => (genAI = instance));
updateGenAIInstance();
store.sub(genAIAtom, updateGenAIInstance);

setStreamHandler(async (message, subscriber) => {
const { data } = message;
if (data && typeof data === 'object' && 'prompt' in data && typeof data.prompt === 'string') {
if (!genAI) return subscriber.error('GenAI is not initialized');
const result = await genAI.getGenerativeModel({ model: 'gemini-pro' }).generateContentStream({
contents: [
{
role: 'user',
parts: [{ text: data.prompt }],
},
],
export const appRouter = t.router({
generateContentStream: t.procedure.input(z.object({ prompt: z.string() })).subscription(({ input }) => {
return observable((observer) => {
if (!genAI) return observer.error('GenAI is not initialized');
let isUnsubscribed = false;
(async () => {
const result = await genAI.getGenerativeModel({ model: 'gemini-pro' }).generateContentStream({
contents: [
{
role: 'user',
parts: [{ text: input.prompt }],
},
],
});
for await (const token of result.stream || []) {
if (isUnsubscribed) break;
observer.next(token.text());
}
observer.complete();
})();
return () => {
isUnsubscribed = true;
};
});
for await (const token of result.stream || []) {
subscriber.next(token.text());
}
subscriber.complete();
}
}),
});

export type AppRouter = typeof appRouter;
15 changes: 15 additions & 0 deletions apps/ai-assistant/src/background/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GoogleGenerativeAI } from '@google/generative-ai';
import { apiKeyAtom } from '@/hooks/atoms/config';
import { atom, getDefaultStore } from 'jotai';

export const store = getDefaultStore();

const genAIAtom = atom(async (get) => {
const apiKey = await get(apiKeyAtom);
return apiKey ? new GoogleGenerativeAI(apiKey) : null;
});

export let genAI: GoogleGenerativeAI | null = null;
const updateGenAIInstance = () => store.get(genAIAtom).then((instance) => (genAI = instance));
updateGenAIInstance();
store.sub(genAIAtom, updateGenAIInstance);
40 changes: 29 additions & 11 deletions apps/ai-assistant/src/content-scripts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import {
isSelectionValid,
rangeToReference,
} from '@webx-kit/runtime/content-scripts';
import { client } from '@webx-kit/messaging/content-script';
import { createTrpcHandler } from '@webx-kit/messaging/content-script';
import clsx from 'clsx';
import type { AppRouter } from '@/background/router';
import { Provider } from './features/provider';
import './global.less';

// hack for missing button loading rotate keyframes
Spin.name;

const { client } = createTrpcHandler<AppRouter>({});

export const App = () => {
const [visible, setVisible] = useState(false);
const [rootStyle, setRootStyle] = useState<React.CSSProperties>();
Expand Down Expand Up @@ -81,15 +84,21 @@ export const App = () => {

setContent('');
setIsLoading(true);
client.stream(
{ prompt: 'Translate the following text to Chinese:\n' + text },
client.generateContentStream.subscribe(
{
prompt: 'Translate the following text to Chinese:\n' + text,
},
{
next: (token) => setContent((prev) => prev + token),
error: (err) => {
onData(token) {
setContent((prev) => prev + token);
},
onError(err) {
console.log('Translate Error', { err });
setIsLoading(false);
},
complete: () => setIsLoading(false),
onComplete() {
setIsLoading(false);
},
}
);
};
Expand All @@ -101,12 +110,21 @@ export const App = () => {

setContent('');
setIsLoading(true);
client.stream(
{ prompt: 'Summarize the following text to Chinese:\n' + text },
client.generateContentStream.subscribe(
{
prompt: 'Summarize the following text to Chinese:\n' + text,
},
{
next: (token) => setContent((prev) => prev + token),
error: () => setIsLoading(false),
complete: () => setIsLoading(false),
onData(token) {
setContent((prev) => prev + token);
},
onError(err) {
console.log('Translate Error', { err });
setIsLoading(false);
},
onComplete() {
setIsLoading(false);
},
}
);
};
Expand Down
37 changes: 19 additions & 18 deletions packages/messaging/demo/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { connections, setRequestHandler, setStreamHandler } from '@/background';

// @ts-expect-error
globalThis.__webxConnections = connections;
import { createCustomHandler } from '@/background';

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

setRequestHandler(async (message) => {
return {
reply: 'background',
data: message.data,
};
const { connections } = createCustomHandler({
async requestHandler(message) {
return {
reply: 'background',
data: message.data,
};
},
async streamHandler(_message, subscriber) {
await sleep(50);
subscriber.next(1);
await sleep(50);
subscriber.next(2);
await sleep(50);
subscriber.next(3);
subscriber.complete();
},
});

setStreamHandler(async (_message, subscriber) => {
await sleep(50);
subscriber.next(1);
await sleep(50);
subscriber.next(2);
await sleep(50);
subscriber.next(3);
subscriber.complete();
});
// @ts-expect-error
globalThis.__webxConnections = connections;
39 changes: 20 additions & 19 deletions packages/messaging/demo/content-scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { client, setRequestHandler, setStreamHandler } from '@/content-script';

// @ts-expect-error
globalThis.__client = client;

setRequestHandler((message) => {
return {
reply: 'content-script',
data: message.data,
};
});
import { createCustomHandler } from '@/content-script';

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

setStreamHandler(async (_message, subscriber) => {
await sleep(50);
subscriber.next('content-script 1');
await sleep(50);
subscriber.next('content-script 2');
await sleep(50);
subscriber.next('content-script 3');
subscriber.complete();
const { messaging } = createCustomHandler({
requestHandler(message) {
return {
reply: 'content-script',
data: message.data,
};
},
async streamHandler(_message, subscriber) {
await sleep(50);
subscriber.next('content-script 1');
await sleep(50);
subscriber.next('content-script 2');
await sleep(50);
subscriber.next('content-script 3');
subscriber.complete();
},
});

// @ts-expect-error
globalThis.__client = messaging;
39 changes: 20 additions & 19 deletions packages/messaging/demo/pages/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { client, setRequestHandler, setStreamHandler } from '@/options';
import { createCustomHandler } from '@/options';
import '../../global.css';

// @ts-expect-error
globalThis.__client = client;

setRequestHandler((message) => {
return {
reply: 'options',
data: message.data,
};
});

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

setStreamHandler(async (_message, subscriber) => {
await sleep(50);
subscriber.next('options 1');
await sleep(50);
subscriber.next('options 2');
await sleep(50);
subscriber.next('options 3');
subscriber.complete();
const { messaging } = createCustomHandler({
requestHandler(message) {
return {
reply: 'options',
data: message.data,
};
},
async streamHandler(_message, subscriber) {
await sleep(50);
subscriber.next('options 1');
await sleep(50);
subscriber.next('options 2');
await sleep(50);
subscriber.next('options 3');
subscriber.complete();
},
});

// @ts-expect-error
globalThis.__client = messaging;
39 changes: 20 additions & 19 deletions packages/messaging/demo/pages/popup/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { client, setRequestHandler, setStreamHandler } from '@/popup';
import { createCustomHandler } from '@/popup';
import '../../global.css';

// @ts-expect-error
globalThis.__client = client;

setRequestHandler((message) => {
return {
reply: 'popup',
data: message.data,
};
});

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

setStreamHandler(async (_message, subscriber) => {
await sleep(50);
subscriber.next('popup 1');
await sleep(50);
subscriber.next('popup 2');
await sleep(50);
subscriber.next('popup 3');
subscriber.complete();
const { messaging } = createCustomHandler({
requestHandler(message) {
return {
reply: 'popup',
data: message.data,
};
},
async streamHandler(_message, subscriber) {
await sleep(50);
subscriber.next('popup 1');
await sleep(50);
subscriber.next('popup 2');
await sleep(50);
subscriber.next('popup 3');
subscriber.complete();
},
});

// @ts-expect-error
globalThis.__client = messaging;
8 changes: 4 additions & 4 deletions packages/messaging/e2e/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { setupStaticServer } from '@webx-kit/test-utils/playwright';
import { expect, test } from './context';
import type { connections } from '@/background';
import type { ensureClient } from '@/client-base';
import type { Messaging } from '@/core';
import type { WrappedMessaging } from '@/client-base';

const getWebpageURL = setupStaticServer(test);

declare module globalThis {
/** only in background */
const __webxConnections: typeof connections | undefined;
const __client: ReturnType<typeof ensureClient>;
const __webxConnections: Set<Messaging> | undefined;
const __client: WrappedMessaging;
}

test('Background', async ({ background }) => {
Expand Down
Loading
Loading