update 优化 !pr850 相关代码用法与问题
This commit is contained in:
+34
-20
@@ -1,7 +1,15 @@
|
||||
import type { AxiosPromise } from '@/utils/api-types';
|
||||
import request from '@/utils/request';
|
||||
import { getToken } from '@/utils/auth';
|
||||
import type { AgentChatRequest, AgentItem, ConversationMessage, ConversationSummaryList, SnailOpenApiUser } from './types';
|
||||
import request, { globalHeaders } from '@/utils/request';
|
||||
import { getLanguage } from '@/lang';
|
||||
import type {
|
||||
AgentChatRequest,
|
||||
AgentChatSyncResponse,
|
||||
AgentItem,
|
||||
ConversationMessage,
|
||||
ConversationSummaryItem,
|
||||
ConversationSummaryList,
|
||||
SnailOpenApiUser
|
||||
} from './types';
|
||||
|
||||
export const fetchMyAgents = (): AxiosPromise<AgentItem[]> => {
|
||||
return request({
|
||||
@@ -35,7 +43,7 @@ export const fetchConversationMessages = (agentId: number, conversationId: strin
|
||||
});
|
||||
};
|
||||
|
||||
export const createConversation = (agentId: number, data: { title?: string }): AxiosPromise<{ conversationId: string; title?: string }> => {
|
||||
export const createConversation = (agentId: number, data: { title?: string }): AxiosPromise<ConversationSummaryItem> => {
|
||||
return request({
|
||||
url: `/snail-ai/agent/${agentId}/conversation`,
|
||||
method: 'post',
|
||||
@@ -43,6 +51,13 @@ export const createConversation = (agentId: number, data: { title?: string }): A
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteConversation = (agentId: number, conversationId: string): AxiosPromise<void> => {
|
||||
return request({
|
||||
url: `/snail-ai/agent/${agentId}/conversation/${conversationId}`,
|
||||
method: 'delete'
|
||||
});
|
||||
};
|
||||
|
||||
export const registerCurrentSnailUser = (): AxiosPromise<SnailOpenApiUser> => {
|
||||
return request({
|
||||
url: '/snail-ai/user/register',
|
||||
@@ -57,7 +72,7 @@ export const fetchChatMode = (): AxiosPromise<{ mode?: 'stream' | 'sync' }> => {
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchAgentChat = (
|
||||
export const fetchAgentChat = async (
|
||||
agentId: number,
|
||||
data: AgentChatRequest,
|
||||
options: {
|
||||
@@ -67,25 +82,24 @@ export const fetchAgentChat = (
|
||||
onError: (error: Error) => void;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
) => {
|
||||
): Promise<void> => {
|
||||
const baseURL = import.meta.env.VITE_APP_BASE_API;
|
||||
const token = getToken();
|
||||
const query = new URLSearchParams({ content: data.content });
|
||||
if (data.conversationId) {
|
||||
query.set('conversationId', data.conversationId);
|
||||
}
|
||||
|
||||
fetch(`${baseURL}/snail-ai/agent/${agentId}/chat/stream?${query.toString()}`, {
|
||||
method: 'GET',
|
||||
await fetch(`${baseURL}/snail-ai/agent/${agentId}/chat/stream`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: token ? `Bearer ${token}` : '',
|
||||
clientid: import.meta.env.VITE_APP_CLIENT_ID
|
||||
...globalHeaders(),
|
||||
'Content-Language': getLanguage(),
|
||||
Accept: 'text/event-stream',
|
||||
'Content-Type': 'application/json;charset=utf-8'
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
signal: options.signal
|
||||
})
|
||||
.then(async response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
const text = await response.text();
|
||||
throw new Error(text || `HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
@@ -100,21 +114,21 @@ export const fetchAgentChat = (
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const eventBlocks = buffer.split('\n\n');
|
||||
const eventBlocks = buffer.split(/\r?\n\r?\n/);
|
||||
buffer = eventBlocks.pop() || '';
|
||||
|
||||
for (const block of eventBlocks) {
|
||||
if (!block.trim()) continue;
|
||||
let eventName = 'message';
|
||||
let payload = '';
|
||||
for (const line of block.split('\n')) {
|
||||
for (const line of block.split(/\r?\n/)) {
|
||||
if (line.startsWith('event:')) {
|
||||
eventName = line.slice(6).trim();
|
||||
} else if (line.startsWith('data:')) {
|
||||
payload += line.slice(5).trim();
|
||||
}
|
||||
}
|
||||
if (!payload) continue;
|
||||
if (!payload && eventName !== 'done') continue;
|
||||
if (eventName === 'thinking') {
|
||||
options.onThinking?.(payload);
|
||||
} else if (eventName === 'text') {
|
||||
@@ -139,7 +153,7 @@ export const fetchAgentChat = (
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchAgentChatSync = (agentId: number, data: AgentChatRequest): AxiosPromise<any> => {
|
||||
export const fetchAgentChatSync = (agentId: number, data: AgentChatRequest): AxiosPromise<AgentChatSyncResponse> => {
|
||||
return request({
|
||||
url: `/snail-ai/agent/${agentId}/chat/sync`,
|
||||
method: 'post',
|
||||
|
||||
@@ -1,26 +1,25 @@
|
||||
import type { PageResult } from '@/api/types';
|
||||
|
||||
export interface AgentItem {
|
||||
id: number;
|
||||
name: string;
|
||||
description?: string;
|
||||
avatar?: string;
|
||||
greeting?: string;
|
||||
status?: number;
|
||||
presetQuestions?: string[];
|
||||
webSearchEnabled?: boolean;
|
||||
}
|
||||
|
||||
export interface ConversationSummaryItem {
|
||||
conversationId: string;
|
||||
agentId?: number;
|
||||
title: string;
|
||||
lastMessageDt?: string;
|
||||
createDt?: string;
|
||||
updateDt?: string;
|
||||
}
|
||||
|
||||
export interface ConversationSummaryList {
|
||||
data: ConversationSummaryItem[];
|
||||
page?: number;
|
||||
size?: number;
|
||||
total?: number;
|
||||
}
|
||||
export type ConversationSummaryList = PageResult<ConversationSummaryItem>;
|
||||
|
||||
export interface ConversationMessage {
|
||||
role?: string;
|
||||
@@ -33,8 +32,13 @@ export interface AgentChatRequest {
|
||||
content: string;
|
||||
disabledMcpServerIds?: number[];
|
||||
disabledSkillIds?: number[];
|
||||
deepPlanEnabled?: boolean;
|
||||
webSearchEnabled?: boolean;
|
||||
}
|
||||
|
||||
export interface AgentChatSyncResponse {
|
||||
conversationId?: string;
|
||||
content?: string;
|
||||
traceId?: string;
|
||||
durationMs?: number;
|
||||
}
|
||||
|
||||
export interface SnailOpenApiUser {
|
||||
|
||||
Reference in New Issue
Block a user