-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import Koa, { Context } from 'koa'; | ||
import koaBody from 'koa-body'; | ||
import body from 'koa-body'; | ||
import logger from 'koa-logger'; | ||
|
||
const app = new Koa(); | ||
|
||
app.use(async (ctx: Context, next) => { | ||
console.info(`access url: ${ctx.url}`); | ||
await next(); | ||
}); | ||
|
||
app.use(koaBody()); | ||
app.use(body()); | ||
app.use(logger()); | ||
|
||
export default app; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */ | ||
/* eslint-disable node/prefer-global/text-decoder */ | ||
import type { RequestOption } from '@modern-js/runtime/server'; | ||
|
||
export async function post(context: RequestOption<never, { text: string }>) { | ||
const { text } = context.data; | ||
|
||
const response = await fetch('https://api.coze.cn/v3/chat', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${process.env.COZE_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
bot_id: '7400050699922767910', | ||
user_id: 'anonymous', | ||
auto_save_history: false, | ||
stream: true, | ||
additional_messages: [ | ||
{ | ||
role: 'user', | ||
content: text, | ||
content_type: 'text', | ||
}, | ||
], | ||
}), | ||
}); | ||
const reader = response.body?.getReader()!; | ||
let result = ''; | ||
|
||
while (true) { | ||
const { value, done } = await reader.read(); | ||
const textDecoder = new TextDecoder(); | ||
const chunk = textDecoder.decode(value); | ||
const lines = chunk.split('\n'); | ||
const event = lines[0]; | ||
|
||
if (event === 'event:conversation.message.completed' || done) { | ||
break; | ||
} | ||
|
||
if (event === 'event:conversation.message.delta') { | ||
const json = JSON.parse(lines[1].slice(5)); | ||
|
||
result += json.content; | ||
} | ||
} | ||
|
||
return { | ||
result: result.trim(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useState, useCallback } from 'react'; | ||
|
||
export function useLoading<F extends (...args: any) => Promise<any>>( | ||
handler: F, | ||
): [F, boolean, Error | undefined] { | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<Error>(); | ||
const newHandler = useCallback( | ||
(...args: any) => { | ||
setLoading(true); | ||
|
||
return handler(...args) | ||
.catch(e => { | ||
setError(e); | ||
}) | ||
.finally(() => { | ||
setLoading(false); | ||
}); | ||
}, | ||
[handler], | ||
); | ||
|
||
return [newHandler as F, loading, error]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters