From 4895ca1fbd9628b9c07d5ac47508e7ceea5521b0 Mon Sep 17 00:00:00 2001 From: Seidko <64234553+Seidko@users.noreply.github.com> Date: Fri, 9 Dec 2022 11:31:18 +0800 Subject: [PATCH 1/5] Feat: chat regenerate and proceed --- src/api.ts | 4 ++-- src/index.ts | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/api.ts b/src/api.ts index 9d6e325..0d1d114 100644 --- a/src/api.ts +++ b/src/api.ts @@ -46,13 +46,13 @@ class ChatGPT { * @param message - The plaintext message to send. * @param opts.conversationId - Optional ID of the previous message in a conversation */ - async sendMessage(conversation: Conversation): Promise> { + async sendMessage(conversation: Conversation, action: string): Promise> { const { conversationId, messageId = uuidv4(), message } = conversation const accessToken = await this.refreshAccessToken() const body: types.ConversationJSONBody = { - action: 'next', + action, conversation_id: conversationId, messages: [ { diff --git a/src/index.ts b/src/index.ts index e1dbe1c..794b2d6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import ChatGPT from './api' +import ChatGPT, { Conversation } from './api' import { Context, Logger, Schema, Session, SessionError } from 'koishi' const logger = new Logger('chatgpt') @@ -37,7 +37,7 @@ export const Config: Schema = Schema.intersect([ }), ]) -const conversations = new Map() +const conversations = new Map() export function apply(ctx: Context, config: Config) { ctx.i18n.define('zh', require('./locales/zh-CN')) @@ -92,9 +92,21 @@ export function apply(ctx: Context, config: Config) { try { // send a message and wait for the response - const { conversationId, messageId } = conversations.get(key) ?? {} - const response = await api.sendMessage({ message: input, conversationId, messageId }) - conversations.set(key, { conversationId: response.conversationId, messageId: response.messageId }) + const conversation: Conversation = conversations.get(key) ?? { message: '', conversationId: undefined, messageId: undefined } + let action = 'next' + switch (input) { + case '重新说': + action = 'variant' + break + case '继续说': + action = 'continue' + break + default: + conversation.message = input + } + + const response = await api.sendMessage(conversation, action) + conversations.set(key, { message: input, conversationId: response.conversationId, messageId: response.messageId }) return response.message } catch (error) { logger.warn(error) From bc5a45bf3702eb8a9c2b8cac6000605c95dedb14 Mon Sep 17 00:00:00 2001 From: Seidko <64234553+Seidko@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:30:06 +0800 Subject: [PATCH 2/5] chore: suggested change by @MaikoTan Signed-off-by: Seidko <64234553+Seidko@users.noreply.github.com> --- src/api.ts | 15 ++++++++++++++- src/index.ts | 19 +++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/api.ts b/src/api.ts index 0d1d114..0fcd27c 100644 --- a/src/api.ts +++ b/src/api.ts @@ -46,7 +46,7 @@ class ChatGPT { * @param message - The plaintext message to send. * @param opts.conversationId - Optional ID of the previous message in a conversation */ - async sendMessage(conversation: Conversation, action: string): Promise> { + async sendMessage(conversation: Conversation, action: ChatGPT.Action): Promise> { const { conversationId, messageId = uuidv4(), message } = conversation const accessToken = await this.refreshAccessToken() @@ -170,9 +170,14 @@ class ChatGPT { } namespace ChatGPT { + export type Action = 'next' | 'variant' | 'continue' + export type Actions = Record + export interface Config { sessionToken: string endpoint: string + keywordContinue: string[] + keywordVariant: string[] markdown?: boolean headers?: Dict proxyAgent?: string @@ -186,6 +191,14 @@ namespace ChatGPT { }), proxyAgent: Schema.string().role('link').description('使用的代理服务器地址。'), markdown: Schema.boolean().hidden().default(false), + keywordContinue: Schema.union([ + Schema.array(String), + Schema.transform(String, (prefix) => [prefix]), + ] as const).description('触发机器人继续回答的关键词。').default(['继续说', '请继续', '继续']), + keywordVariant: Schema.union([ + Schema.array(String), + Schema.transform(String, (prefix) => [prefix]), + ] as const).description('触发机器人重新回答的关键词。').default(['重新说', '重试']), }).description('登录设置') } diff --git a/src/index.ts b/src/index.ts index 794b2d6..70bb84d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,11 @@ export function apply(ctx: Context, config: Config) { const api = new ChatGPT(ctx, config) + const actions: ChatGPT.Actions = { + ...Object.fromEntries(config.keywordContinue.map((value) => [value, 'continue'])), + ...Object.fromEntries(config.keywordVariant.map((value) => [value, 'variant'])) + } + const getContextKey = (session: Session, config: Config) => { switch (config.interaction) { case 'user': @@ -92,18 +97,8 @@ export function apply(ctx: Context, config: Config) { try { // send a message and wait for the response - const conversation: Conversation = conversations.get(key) ?? { message: '', conversationId: undefined, messageId: undefined } - let action = 'next' - switch (input) { - case '重新说': - action = 'variant' - break - case '继续说': - action = 'continue' - break - default: - conversation.message = input - } + let conversation: Conversation = conversations.get(key) + let action: ChatGPT.Action = input in actions ? actions[input] : 'next' const response = await api.sendMessage(conversation, action) conversations.set(key, { message: input, conversationId: response.conversationId, messageId: response.messageId }) From 770453c0c3ea6af3ad797c8a38a993e503e80821 Mon Sep 17 00:00:00 2001 From: Seidko <64234553+Seidko@users.noreply.github.com> Date: Fri, 9 Dec 2022 21:56:21 +0800 Subject: [PATCH 3/5] fix: some code was overrided by the previous commit --- src/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 70bb84d..2e438ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -97,11 +97,12 @@ export function apply(ctx: Context, config: Config) { try { // send a message and wait for the response - let conversation: Conversation = conversations.get(key) + let request: Conversation = conversations.get(key) let action: ChatGPT.Action = input in actions ? actions[input] : 'next' + if(action === 'next') request.message = input - const response = await api.sendMessage(conversation, action) - conversations.set(key, { message: input, conversationId: response.conversationId, messageId: response.messageId }) + const response = await api.sendMessage(request, action) + conversations.set(key, { message: request.message, conversationId: response.conversationId, messageId: response.messageId }) return response.message } catch (error) { logger.warn(error) From 282bf5f18bf5d70aba343cedfb716292c48c137a Mon Sep 17 00:00:00 2001 From: Seidko <64234553+Seidko@users.noreply.github.com> Date: Fri, 9 Dec 2022 23:10:43 +0800 Subject: [PATCH 4/5] fix: read properties of undefined (request) --- src/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2e438ea..a757c99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -99,10 +99,9 @@ export function apply(ctx: Context, config: Config) { // send a message and wait for the response let request: Conversation = conversations.get(key) let action: ChatGPT.Action = input in actions ? actions[input] : 'next' - if(action === 'next') request.message = input - const response = await api.sendMessage(request, action) - conversations.set(key, { message: request.message, conversationId: response.conversationId, messageId: response.messageId }) + const response = await api.sendMessage({ message: input, ...request}, action) + conversations.set(key, { message: request?.message, conversationId: response.conversationId, messageId: response.messageId }) return response.message } catch (error) { logger.warn(error) From df26551871b406847f58c0ccfe86f2f6d8d89ca5 Mon Sep 17 00:00:00 2001 From: Seidko <64234553+Seidko@users.noreply.github.com> Date: Sat, 10 Dec 2022 11:53:41 +0800 Subject: [PATCH 5/5] fix: extra endpoint arguments cause ENOTFOUND --- src/api.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/api.ts b/src/api.ts index 9d6e325..ad2f83c 100644 --- a/src/api.ts +++ b/src/api.ts @@ -17,13 +17,11 @@ export interface Conversation { class ChatGPT { protected http: Quester - protected _endpoint: string // stores access tokens for up to 10 seconds before needing to refresh protected _accessTokenCache = new ExpiryMap(10 * 1000) constructor(ctx: Context, public config: ChatGPT.Config) { this.http = ctx.http.extend(config) - this._endpoint = trimSlash(config.endpoint) } async getIsAuthenticated() { @@ -68,11 +66,9 @@ class ChatGPT { parent_message_id: messageId, } - const url = this._endpoint + '/backend-api/conversation' - let data: internal.Readable try { - const resp = await this.http.axios(url, { + const resp = await this.http.axios('/backend-api/conversation', { method: 'POST', responseType: 'stream', data: body, @@ -148,7 +144,7 @@ class ChatGPT { } try { - const res = await this.http.get(this._endpoint + '/api/auth/session', { + const res = await this.http.get('/api/auth/session', { headers: { cookie: `__Secure-next-auth.session-token=${this.config.sessionToken}`, },