Skip to content

Commit

Permalink
Merge branch 'mention'
Browse files Browse the repository at this point in the history
  • Loading branch information
hzeyuan committed Jan 30, 2024
2 parents e67518e + fc5bf8b commit 6d17471
Show file tree
Hide file tree
Showing 54 changed files with 1,144 additions and 679 deletions.
10 changes: 10 additions & 0 deletions apps/extension/assets/@.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
1 change: 1 addition & 0 deletions apps/extension/assets/chatgpt4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import { Storage } from "@plasmohq/storage";
import type { Config } from "@opengpts/types";
import type { ChatConfig } from "@opengpts/types";
import Browser from "webextension-polyfill";
import { DEFAULT_CONFIG } from "~src/constant";

Expand All @@ -22,8 +22,7 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
let authHeader = headers.find(header => header.name.toLowerCase() === 'authorization');
const token = authHeader?.value?.replace('Bearer ', '').trim()
if (authHeader) {
storage.getItem<Config>('chatgpt-config').then(preConfig => {
console.log('preConfig', preConfig)
storage.getItem<ChatConfig>('chatgpt-config').then(preConfig => {
storage.setItem('chatgpt-config', {
...DEFAULT_CONFIG,
...preConfig,
Expand Down Expand Up @@ -90,7 +89,7 @@ Browser.webRequest.onBeforeRequest.addListener(
)


storage.getItem<Config>('config').then((config) => {
storage.getItem<ChatConfig>('config').then((config) => {
storage.setItem('config', {
...DEFAULT_CONFIG,
...config,
Expand Down
26 changes: 13 additions & 13 deletions apps/extension/src/background/messages/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { PlasmoMessaging } from "@plasmohq/messaging"
import { Storage } from "@plasmohq/storage";
import _ from 'lodash'
import { ofetch } from 'ofetch'
import type { Config, Gizmo } from '@opengpts/types'
import type { ChatConfig, Gizmo } from '@opengpts/types'
import { OpenAI } from '@opengpts/core'

const storage = new Storage({
Expand All @@ -12,18 +12,18 @@ const storage = new Storage({



const createItem = async (newItem) => {
const createGPTs = async (newItem) => {
const items = await storage.getItem<Gizmo[]>('gizmos') || [];
console.log('newItem', newItem)
await storage.setItem('gizmos', [newItem, ...items]);
};

const readItemById = async (id) => {
const getGPTsById = async (id) => {
const items = await storage.getItem<Gizmo[]>('gizmos') || [];
return items.find(item => item.id === id);
};

const updateItem = async (id, updatedFields) => {
const updateGPTs = async (id, updatedFields) => {
let updatedItem;
const items = await storage.getItem<Gizmo[]>('gizmos') || [];
const newItems = items.map(item => {
Expand All @@ -38,7 +38,7 @@ const updateItem = async (id, updatedFields) => {
return updatedItem;
};

const deleteItem = async (id) => {
const deleteGPTs = async (id) => {
const items = await storage.getItem<Gizmo[]>('gizmos') || [];
const newItems = items.filter(item => item.id !== id);
await storage.setItem('gizmos', newItems);
Expand All @@ -49,7 +49,7 @@ const checkChatGPTsAuth: () => Promise<{
error?: string;
data?: string;
}> = async () => {
const config = await storage.getItem<Config>('config')
const config = await storage.getItem<ChatConfig>('config')
console.log('chatgptArkoseReqUrl', config)
if (!config?.chatgptArkoseReqUrl) {
return {
Expand All @@ -64,8 +64,8 @@ const checkChatGPTsAuth: () => Promise<{
}

const isLogin = async () => {
const chatgptConfig = await storage.getItem<Config>('chatgpt-config')
if (!chatgptConfig.token) {
const chatgptConfig = await storage.getItem<ChatConfig>('chatgpt-config')
if (!chatgptConfig?.token) {
return {
ok: false,
error: 'Please Chat with any GPTS https://chat.openai.com/gpts',
Expand All @@ -79,7 +79,7 @@ const isLogin = async () => {

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {

const chatgptConfig = await storage.getItem<Config>('chatgpt-config');
const chatgptConfig = await storage.getItem<ChatConfig>('chatgpt-config');
const token = chatgptConfig?.token;
const openai = new OpenAI({ token })

Expand Down Expand Up @@ -108,7 +108,7 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
} else if (action === 'delete') {
try {
await openai.gpt.del(gizmoId);
await deleteItem(gizmoId)
await deleteGPTs(gizmoId)
res.send({
ok: true
})
Expand Down Expand Up @@ -145,7 +145,7 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
try {
const { gizmoId, gizmo, draft } = req.body
const newGizmo = await openai.gpt.update(gizmoId, gizmo)
await updateItem(gizmoId, newGizmo)
await updateGPTs(gizmoId, newGizmo)
res.send({
ok: true,
data: gizmo
Expand All @@ -161,7 +161,7 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
try {
console.log('gizmoId', gizmoId)
await openai.gpt.publish(gizmoId)
await updateItem(gizmoId, {
await updateGPTs(gizmoId, {
tags: ['public']
})
res.send({ ok: true, data: '' })
Expand All @@ -177,7 +177,7 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const tools = req.body.tools || []
const newGizmo = await openai.gpt.create(gizmo, tools)
console.log('createGPT', newGizmo)
await createItem(newGizmo)
await createGPTs(newGizmo)
res.send({
ok: true,
data: newGizmo
Expand Down
Loading

0 comments on commit 6d17471

Please sign in to comment.