q-koa 13.0.5 → 13.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core/app.js CHANGED
@@ -1171,7 +1171,9 @@ class APP {
1171
1171
  },
1172
1172
  }
1173
1173
  const $between = {
1174
- check: (param) => Array.isArray(param),
1174
+ check: (param) =>
1175
+ Array.isArray(param) &&
1176
+ param.every((p) => p.includes('now()')),
1175
1177
  result: (param, key) => {
1176
1178
  return {
1177
1179
  [key]: param[key].map(Sequelize.literal),
@@ -2005,13 +2007,13 @@ class APP {
2005
2007
  const where =
2006
2008
  ctx.request.clientType > 1
2007
2009
  ? {
2008
- ...ctx.request.body,
2009
2010
  createdid:
2010
2011
  ctx.request.clientType > 1
2011
2012
  ? ctx.request[`${appName}-user`]
2012
2013
  ? ctx.request[`${appName}-user`].id
2013
2014
  : 0
2014
2015
  : 0,
2016
+ ...ctx.request.body,
2015
2017
  }
2016
2018
  : ctx.request.body
2017
2019
  const result = await app[appName].model[controller][fn]({
@@ -2,7 +2,14 @@ const fs = require('fs')
2
2
  const fsPromise = require('fs/promises')
3
3
  const path = require('path')
4
4
  const crypto = require('crypto')
5
- const { Random, lodash, getAppByCtx, getConfig } = require('q-koa')
5
+ const {
6
+ Random,
7
+ lodash,
8
+ getAppByCtx,
9
+ getConfig,
10
+ ServiceError,
11
+ sleep,
12
+ } = require('q-koa')
6
13
  const Captchapng = require('captchapng')
7
14
  const qr = require('qr-image')
8
15
  const OSS = require('ali-oss')
@@ -594,11 +601,77 @@ exports.sql = async (ctx) => {
594
601
 
595
602
  exports.gpt = async (ctx) => {
596
603
  const { app, appName } = getAppByCtx(ctx)
597
- const { content = '' } = ctx.request.body
604
+ const appConfig = getConfig(app)
605
+ const { ai_key } = await appConfig.getObject('base')
606
+ const { content, session_id, is_chunk = true } = ctx.request.body
607
+
608
+ if (!content) throw new ServiceError('没有内容content')
609
+ let statusCode = 200
610
+ const url =
611
+ 'https://dashscope.aliyuncs.com/api/v1/apps/69c314f8f87e4a2080af950e5997bdc6/completion'
612
+
613
+ const response = await axios({
614
+ url,
615
+ method: 'POST',
616
+ ...(is_chunk
617
+ ? {
618
+ responseType: 'stream',
619
+ }
620
+ : {}),
621
+ data: {
622
+ input: {
623
+ prompt: content,
624
+ ...(session_id
625
+ ? {
626
+ session_id,
627
+ }
628
+ : {}),
629
+ },
630
+ },
631
+ headers: {
632
+ Authorization: `Bearer ${ai_key}`,
633
+ 'Content-Type': 'application/json',
634
+ ...(is_chunk
635
+ ? {
636
+ 'X-DashScope-SSE': 'enable',
637
+ }
638
+ : {}),
639
+ },
640
+ }).catch((e) => {
641
+ if (e.message.includes('401')) {
642
+ statusCode = 401
643
+ ctx.response.status = statusCode
644
+ }
645
+ if (e.message.includes('500')) {
646
+ statusCode = 500
647
+ ctx.response.status = statusCode
648
+ }
649
+ throw new Error(e.message)
650
+ })
598
651
 
599
- if (!content) return ctx.ERROR('没有内容')
652
+ if (!is_chunk) {
653
+ const res = response.data
654
+ return ctx.SUCCESS(res)
655
+ }
600
656
 
601
- const res = await app.service.common.gpt({ app, content })
657
+ // console.log('response', response)
602
658
 
603
- ctx.SUCCESS(res)
659
+ ctx.response.status = statusCode
660
+ if (statusCode !== 200) {
661
+ return
662
+ }
663
+ ctx.res.setHeader('Content-Type', 'text/event-stream')
664
+ ctx.res.setHeader('Cache-Control', 'no-cache')
665
+ ctx.res.setHeader('Connection', 'keep-alive')
666
+
667
+ response.data.on('data', (chunk) => {
668
+ ctx.res.write(chunk)
669
+ })
670
+ response.data.on('end', (chunk) => {
671
+ ctx.res.end()
672
+ })
673
+ response.data.on('error', (res) => {
674
+ console.log(res)
675
+ })
676
+ await sleep(1000000)
604
677
  }
@@ -153,7 +153,7 @@ exports.login = async (ctx) => {
153
153
  const application = await getAppConfig({ app, config })
154
154
 
155
155
  const loginData = lodash.mergeWith(
156
- app.appConfig.loginData,
156
+ lodash.cloneDeep(app.appConfig.loginData),
157
157
  lodash.get(application, 'loginData', {}),
158
158
  (objValue, srcValue) => {
159
159
  if (lodash.isArray(objValue)) {
@@ -355,9 +355,8 @@ exports.checkLogin = async (ctx) => {
355
355
 
356
356
  if (app.appConfig.loginData) {
357
357
  const application = await getAppConfig({ app, config })
358
-
359
358
  const loginData = lodash.mergeWith(
360
- app.appConfig.loginData,
359
+ lodash.cloneDeep(app.appConfig.loginData),
361
360
  lodash.get(application, 'loginData', {}),
362
361
  (objValue, srcValue) => {
363
362
  if (lodash.isArray(objValue)) {
@@ -365,6 +364,7 @@ exports.checkLogin = async (ctx) => {
365
364
  }
366
365
  }
367
366
  )
367
+
368
368
  const include = includeDefault.filter((i) => {
369
369
  return loginData.excludeInclude.every((m) => {
370
370
  return app.model[m] !== i.model
@@ -444,7 +444,7 @@ exports.mp_login = async (ctx) => {
444
444
  if (app.appConfig.loginData) {
445
445
  const application = await getAppConfig({ app, config })
446
446
  const loginData = lodash.mergeWith(
447
- app.appConfig.loginData,
447
+ lodash.cloneDeep(app.appConfig.loginData),
448
448
  lodash.get(application, 'loginData', {}),
449
449
  (objValue, srcValue) => {
450
450
  if (lodash.isArray(objValue)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.0.5",
3
+ "version": "13.0.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {