Skip to content

Commit

Permalink
Merge pull request #831 from UpstreetAI/pglite2
Browse files Browse the repository at this point in the history
PGliteStorage in the browser
  • Loading branch information
avaer authored Jan 1, 2025
2 parents 272c928 + 81dadb3 commit c67db2f
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 135 deletions.
5 changes: 3 additions & 2 deletions apps/chat/app/new/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Editor, { useMonaco } from '@monaco-editor/react';
import { Button } from '@/components/ui/button';
import { deployEndpointUrl, r2EndpointUrl } from '@/utils/const/endpoints';
import { getJWT } from '@/lib/jwt';
import { getUserIdForJwt, getUserForJwt } from '@/utils/supabase/supabase-client';
import { getUserForJwt } from '@/utils/supabase/supabase-client';
import { PGliteStorage } from 'react-agents/storage/pglite-storage.mjs'
import type {
StoreItem,
SubscriptionProps,
Expand Down Expand Up @@ -42,7 +43,6 @@ import { currencies, intervals } from 'react-agents/constants.mjs';
import { buildAgentSrc } from 'react-agents-builder';
import { ReactAgentsWorker } from 'react-agents-browser';
import type { FetchableWorker } from 'react-agents-browser/types';
// import { IconButton } from 'ucom';
import { BackButton } from '@/components/back';

//
Expand Down Expand Up @@ -355,6 +355,7 @@ export default function AgentEditor({
agentJson,
agentModuleSrc,
env,
storageAdapter: 'pglite',
});
setWorker(newWorker);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export class ReactAgentsWorker {
agentJson,
agentModuleSrc,
env,
storageAdapter,
}: {
agentJson: any,
agentModuleSrc: string,
env: any,
storageAdapter?: string,
}) {
if (
!agentJson ||
Expand All @@ -34,6 +36,7 @@ export class ReactAgentsWorker {
agentJson,
env,
agentModuleSrc,
storageAdapter,
},
});
this.worker.addEventListener('error', e => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ globalThis.onmessage = (event: any) => {
if (!rootPromise) {
rootPromise = (async () => {
const { args } = event.data;
const { agentJson, env, agentModuleSrc } = args;
const { agentJson, env, agentModuleSrc, storageAdapter } = args;
if (typeof agentModuleSrc !== 'string') {
throw new Error('agent worker: missing agentModuleSrc');
}
Expand All @@ -38,6 +38,7 @@ globalThis.onmessage = (event: any) => {
agentJson,
codecs,
env,
storageAdapter,
};
const root = createRoot(rootOpts);
root.render(<App />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class AgentRenderer {
env: any;
config: any;
chatsSpecification: ChatsSpecification;
supabase: any;
codecs: any;

registry: RenderRegistry;
Expand All @@ -114,17 +115,20 @@ export class AgentRenderer {
env,
config,
chatsSpecification,
supabase,
codecs,
}: {
env: any;
config: any;
chatsSpecification: ChatsSpecification;
supabase: any;
codecs: any;
}) {
// latch arguments
this.env = env;
this.config = config;
this.chatsSpecification = chatsSpecification;
this.supabase = supabase;
this.codecs = codecs;

// create the app context
Expand All @@ -151,9 +155,7 @@ export class AgentRenderer {
return this.env.AGENT_TOKEN;
};
const useSupabase = () => {
const jwt = useAuthToken();
const supabase = new SupabaseStorage({ jwt });
return supabase;
return this.supabase;
};
const useConversationManager = () => {
return this.conversationManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import {
ConversationContext,
} from './context';
import { ExtendableMessageEvent } from './util/extendable-message-event';
import {
supabaseSubscribe,
} from './util/supabase-utils.mjs';
// import {
// supabaseSubscribe,
// } from './util/supabase-utils.mjs';
import {
QueueManager,
} from 'queue-manager';
Expand Down Expand Up @@ -404,23 +404,23 @@ export const usePurchases = () => {
live = false;
};
}, []);
// subscribe to webhooks
useEffect(() => {
const channel = supabaseSubscribe({
supabase,
table: 'webhooks',
userId: ownerId,
}, (payload: any) => {
// console.log('subscription payload', payload);
const webhook = payload.new;
queueManager.waitForTurn(async () => {
await handleWebhook(webhook);
});
});
return () => {
supabase.removeChannel(channel);
};
}, []);
// // subscribe to webhooks
// useEffect(() => {
// const channel = supabaseSubscribe({
// supabase,
// table: 'webhooks',
// userId: ownerId,
// }, (payload: any) => {
// // console.log('subscription payload', payload);
// const webhook = payload.new;
// queueManager.waitForTurn(async () => {
// await handleWebhook(webhook);
// });
// });
// return () => {
// supabase.removeChannel(channel);
// };
// }, []);

const purchases = agentWebhooksState.webhooks.map((webhook) => {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { ReactNode } from 'react';
import { ReactNode } from 'react';
import { headers } from './constants.mjs';
import { SupabaseStorage } from './storage/supabase-storage.mjs';
import { PGliteStorage } from './storage/pglite-storage.mjs';
import { AgentRenderer } from './classes/agent-renderer.tsx';
import { ChatsSpecification } from './classes/chats-specification.ts';
import { multiplayerEndpointUrl } from './util/endpoints.mjs';
Expand Down Expand Up @@ -32,11 +33,30 @@ export class Root extends EventTarget {
const {
agentJson = {},
env = {},
storageAdapter = new SupabaseStorage({
jwt: env.AGENT_TOKEN,
}),
codecs = {},
} = opts;
const storageAdapter = (() => {
const _storageAdapter = opts.storageAdapter ?? 'supabase';
if (typeof _storageAdapter === 'string') {
switch (_storageAdapter) {
case 'supabase': {
return new SupabaseStorage({
jwt: env.AGENT_TOKEN,
});
}
case 'pglite': {
return new PGliteStorage({
path: env.PGLITE_PATH,
});
}
default: {
throw new Error('unknown storage adapter type: ' + _storageAdapter);
}
}
} else {
return _storageAdapter;
}
})();

this.chatsSpecification = new ChatsSpecification({
agentId: agentJson.id,
Expand All @@ -46,6 +66,7 @@ export class Root extends EventTarget {
env,
config: agentJson,
codecs,
supabase: storageAdapter,
chatsSpecification: this.chatsSpecification,
});
// const bindAlarm = () => {
Expand Down
Loading

0 comments on commit c67db2f

Please sign in to comment.