feat: agent接口支持鉴权
This commit is contained in:
parent
08d53ef0a6
commit
58d2024fea
|
@ -48,6 +48,13 @@ async function handle(req: NextRequest) {
|
||||||
return NextResponse.json({ body: "OK" }, { status: 200 });
|
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
const authResult = auth(req);
|
||||||
|
if (authResult.error) {
|
||||||
|
return NextResponse.json(authResult, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const transformStream = new TransformStream();
|
const transformStream = new TransformStream();
|
||||||
const writer = transformStream.writable.getWriter();
|
const writer = transformStream.writable.getWriter();
|
||||||
|
@ -56,7 +63,6 @@ async function handle(req: NextRequest) {
|
||||||
const handler = BaseCallbackHandler.fromMethods({
|
const handler = BaseCallbackHandler.fromMethods({
|
||||||
async handleLLMNewToken(token: string) {
|
async handleLLMNewToken(token: string) {
|
||||||
if (token) {
|
if (token) {
|
||||||
// console.log("token", token);
|
|
||||||
var response = new ResponseBody();
|
var response = new ResponseBody();
|
||||||
response.message = token;
|
response.message = token;
|
||||||
await writer.ready;
|
await writer.ready;
|
||||||
|
@ -71,12 +77,10 @@ async function handle(req: NextRequest) {
|
||||||
// await writer.abort(err);
|
// await writer.abort(err);
|
||||||
// },
|
// },
|
||||||
async handleChainEnd(outputs, runId, parentRunId, tags) {
|
async handleChainEnd(outputs, runId, parentRunId, tags) {
|
||||||
// console.log("writer close");
|
|
||||||
await writer.ready;
|
await writer.ready;
|
||||||
await writer.close();
|
await writer.close();
|
||||||
},
|
},
|
||||||
async handleLLMEnd() {
|
async handleLLMEnd() {
|
||||||
// console.log("writer close");
|
|
||||||
// await writer.ready;
|
// await writer.ready;
|
||||||
// await writer.close();
|
// await writer.close();
|
||||||
},
|
},
|
||||||
|
@ -92,20 +96,24 @@ async function handle(req: NextRequest) {
|
||||||
// console.log("handleChainStart: I'm the second handler!!", { chain });
|
// console.log("handleChainStart: I'm the second handler!!", { chain });
|
||||||
},
|
},
|
||||||
async handleAgentAction(action) {
|
async handleAgentAction(action) {
|
||||||
console.log(
|
try {
|
||||||
"agent (llm)",
|
console.log(
|
||||||
`tool: ${action.tool} toolInput: ${action.toolInput}`,
|
"agent (llm)",
|
||||||
{ action },
|
`tool: ${action.tool} toolInput: ${action.toolInput}`,
|
||||||
);
|
{ action },
|
||||||
var response = new ResponseBody();
|
);
|
||||||
response.isToolMessage = true;
|
var response = new ResponseBody();
|
||||||
let toolInput = <ToolInput>(<unknown>action.toolInput);
|
response.isToolMessage = true;
|
||||||
response.message = toolInput.input;
|
let toolInput = <ToolInput>(<unknown>action.toolInput);
|
||||||
response.toolName = action.tool;
|
response.message = toolInput.input;
|
||||||
await writer.ready;
|
response.toolName = action.tool;
|
||||||
await writer.write(
|
await writer.ready;
|
||||||
encoder.encode(`data: ${JSON.stringify(response)}\n\n`),
|
await writer.write(
|
||||||
);
|
encoder.encode(`data: ${JSON.stringify(response)}\n\n`),
|
||||||
|
);
|
||||||
|
} catch (ex) {
|
||||||
|
console.error("[handleAgentAction]", ex);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
handleToolStart(tool, input) {
|
handleToolStart(tool, input) {
|
||||||
console.log("handleToolStart", { tool, input });
|
console.log("handleToolStart", { tool, input });
|
||||||
|
@ -115,17 +123,13 @@ async function handle(req: NextRequest) {
|
||||||
const tools = [
|
const tools = [
|
||||||
new RequestsGetTool(),
|
new RequestsGetTool(),
|
||||||
new RequestsPostTool(),
|
new RequestsPostTool(),
|
||||||
new SerpAPI(process.env.SERPAPI_API_KEY, {
|
new SerpAPI(process.env.SERPAPI_API_KEY),
|
||||||
location: "Austin,Texas,United States",
|
new Calculator(),
|
||||||
hl: "en",
|
|
||||||
gl: "us",
|
|
||||||
}),
|
|
||||||
// new DynamicTool({
|
// new DynamicTool({
|
||||||
// name: ddg.name,
|
// name: ddg.name,
|
||||||
// description: ddg.description,
|
// description: ddg.description,
|
||||||
// func: async (input: string) => ddg.call(input),
|
// func: async (input: string) => ddg.call(input),
|
||||||
// }),
|
// }),
|
||||||
new Calculator(),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const pastMessages = new Array();
|
const pastMessages = new Array();
|
||||||
|
@ -141,8 +145,6 @@ async function handle(req: NextRequest) {
|
||||||
pastMessages.push(new AIMessage(message.content));
|
pastMessages.push(new AIMessage(message.content));
|
||||||
});
|
});
|
||||||
|
|
||||||
// console.log("mesage", { pastMessages })
|
|
||||||
|
|
||||||
const memory = new BufferMemory({
|
const memory = new BufferMemory({
|
||||||
memoryKey: "chat_history",
|
memoryKey: "chat_history",
|
||||||
returnMessages: true,
|
returnMessages: true,
|
||||||
|
|
Loading…
Reference in New Issue