141 lines
4.2 KiB
TypeScript
141 lines
4.2 KiB
TypeScript
import type {
|
|
CreateRpgAgentSessionRequest,
|
|
CreateRpgAgentSessionResponse,
|
|
GetRpgAgentCardDetailResponse,
|
|
RpgAgentDraftCardDetail,
|
|
RpgAgentOperationRecord,
|
|
RpgAgentSessionSnapshot,
|
|
SendRpgAgentMessageRequest,
|
|
} from '../../../packages/shared/src';
|
|
import type { RpgAgentActionRequest } from '../../../packages/shared/src';
|
|
import type { TextStreamOptions } from '../aiTypes';
|
|
import { readCreationAgentSessionFromSse } from '../creation-agent';
|
|
import {
|
|
openRpgCreationSsePost,
|
|
requestRpgCreationPostJson,
|
|
} from './rpgCreationRequestHelpers';
|
|
import { requestRpgCreationRuntimeJson } from './rpgCreationRuntimeClient';
|
|
|
|
const RPG_AGENT_API_BASE = '/custom-world/agent/sessions';
|
|
const CREATION_SESSION_START_TIMEOUT_MS = 15000;
|
|
|
|
export async function createRpgCreationSession(
|
|
payload: CreateRpgAgentSessionRequest,
|
|
) {
|
|
return requestRpgCreationRuntimeJson<CreateRpgAgentSessionResponse>(
|
|
RPG_AGENT_API_BASE,
|
|
{
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
},
|
|
'创建世界共创会话失败',
|
|
{
|
|
timeoutMs: CREATION_SESSION_START_TIMEOUT_MS,
|
|
},
|
|
);
|
|
}
|
|
|
|
export async function getRpgCreationSession(sessionId: string) {
|
|
return requestRpgCreationRuntimeJson<RpgAgentSessionSnapshot>(
|
|
`${RPG_AGENT_API_BASE}/${encodeURIComponent(sessionId)}`,
|
|
{
|
|
method: 'GET',
|
|
},
|
|
'读取世界共创会话失败',
|
|
);
|
|
}
|
|
|
|
export async function sendRpgCreationMessage(
|
|
sessionId: string,
|
|
payload: SendRpgAgentMessageRequest,
|
|
) {
|
|
return requestRpgCreationPostJson<{ operation: RpgAgentOperationRecord }>(
|
|
`/api/runtime${RPG_AGENT_API_BASE}/${encodeURIComponent(sessionId)}/messages`,
|
|
payload,
|
|
'发送共创消息失败',
|
|
);
|
|
}
|
|
|
|
export async function streamRpgCreationMessage(
|
|
sessionId: string,
|
|
payload: SendRpgAgentMessageRequest,
|
|
options: TextStreamOptions = {},
|
|
) {
|
|
const response = await openRpgCreationSsePost(
|
|
`/api/runtime${RPG_AGENT_API_BASE}/${encodeURIComponent(sessionId)}/messages/stream`,
|
|
payload,
|
|
'发送共创消息失败',
|
|
);
|
|
|
|
return readCreationAgentSessionFromSse<RpgAgentSessionSnapshot>(response, {
|
|
...options,
|
|
fallbackMessage: '发送共创消息失败',
|
|
incompleteMessage: '共创消息流式结果不完整',
|
|
});
|
|
}
|
|
|
|
export async function executeRpgCreationAction(
|
|
sessionId: string,
|
|
payload: RpgAgentActionRequest,
|
|
) {
|
|
return requestRpgCreationRuntimeJson<{ operation: RpgAgentOperationRecord }>(
|
|
`${RPG_AGENT_API_BASE}/${encodeURIComponent(sessionId)}/actions`,
|
|
{
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
},
|
|
'执行共创操作失败',
|
|
);
|
|
}
|
|
|
|
export async function getRpgCreationOperation(
|
|
sessionId: string,
|
|
operationId: string,
|
|
): Promise<RpgAgentOperationRecord> {
|
|
const response = await requestRpgCreationRuntimeJson<
|
|
{
|
|
operation?: RpgAgentOperationRecord;
|
|
data?: RpgAgentOperationRecord;
|
|
} & Partial<RpgAgentOperationRecord>
|
|
>(
|
|
`${RPG_AGENT_API_BASE}/${encodeURIComponent(sessionId)}/operations/${encodeURIComponent(operationId)}`,
|
|
{
|
|
method: 'GET',
|
|
},
|
|
'读取共创操作状态失败',
|
|
);
|
|
|
|
return (response.operation ?? response.data ?? response) as RpgAgentOperationRecord;
|
|
}
|
|
|
|
export async function getRpgCreationCardDetail(
|
|
sessionId: string,
|
|
cardId: string,
|
|
) {
|
|
const response = await requestRpgCreationRuntimeJson<GetRpgAgentCardDetailResponse>(
|
|
`${RPG_AGENT_API_BASE}/${encodeURIComponent(sessionId)}/cards/${encodeURIComponent(cardId)}`,
|
|
{
|
|
method: 'GET',
|
|
},
|
|
'读取草稿卡详情失败',
|
|
);
|
|
|
|
return response.card as RpgAgentDraftCardDetail;
|
|
}
|
|
|
|
/**
|
|
* 工作包 D 开始让 RPG 创作 Agent client 持有真实请求实现,
|
|
* 旧 `aiService.ts` 仅保留兼容导出,避免主链请求继续回流到通用服务文件。
|
|
*/
|
|
export const rpgCreationAgentClient = {
|
|
createSession: createRpgCreationSession,
|
|
getSession: getRpgCreationSession,
|
|
sendMessage: sendRpgCreationMessage,
|
|
streamMessage: streamRpgCreationMessage,
|
|
executeAction: executeRpgCreationAction,
|
|
getOperation: getRpgCreationOperation,
|
|
getCardDetail: getRpgCreationCardDetail,
|
|
};
|