q-koa 13.0.8 → 13.1.0

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
@@ -2007,13 +2007,13 @@ class APP {
2007
2007
  const where =
2008
2008
  ctx.request.clientType > 1
2009
2009
  ? {
2010
- ...ctx.request.body,
2011
2010
  createdid:
2012
2011
  ctx.request.clientType > 1
2013
2012
  ? ctx.request[`${appName}-user`]
2014
2013
  ? ctx.request[`${appName}-user`].id
2015
2014
  : 0
2016
2015
  : 0,
2016
+ ...ctx.request.body,
2017
2017
  }
2018
2018
  : ctx.request.body
2019
2019
  const result = await app[appName].model[controller][fn]({
@@ -2167,6 +2167,7 @@ APP.getConfig = (app) => ({
2167
2167
  'is_front',
2168
2168
  'is_control',
2169
2169
  'createdid',
2170
+ 'extra',
2170
2171
  ],
2171
2172
  },
2172
2173
  include: [
@@ -2184,6 +2185,7 @@ APP.getConfig = (app) => ({
2184
2185
  'is_front',
2185
2186
  'is_control',
2186
2187
  'createdid',
2188
+ 'extra',
2187
2189
  ],
2188
2190
  },
2189
2191
  },
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.0.8",
3
+ "version": "13.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {