platform-llm 新增 Anthropic 链路,LLM 请求收口为 LlmRunRequest/LlmApiKind

This commit is contained in:
2026-06-29 21:31:07 +08:00
parent 9af13974db
commit 2932a4041a
28 changed files with 742 additions and 348 deletions
@@ -336,7 +336,7 @@ for (const snippet of [
'function writeStreamingChatCompletion',
'requestJson?.stream === true',
`requestBodies.every((body) => body.includes('"stream":true'))`,
"GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL: 'chat_completions'",
"GENARRATIVE_GAME_CREATOR_LLM_API_KIND: 'openai_chat'",
"GENARRATIVE_GAME_CREATOR_LLM_STREAM: 'true'",
"method: 'HEAD'",
'previewAssetHead.contentLength === String(smokeAssetBytes.length)',
@@ -573,7 +573,7 @@ function runAgent(baseUrl) {
GENARRATIVE_GAME_CREATOR_LLM_API_KEY: 'local-provider-key',
GENARRATIVE_GAME_CREATOR_LLM_BASE_URL: baseUrl,
GENARRATIVE_GAME_CREATOR_LLM_MODEL: 'local-game-creator-smoke',
GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL: 'chat_completions',
GENARRATIVE_GAME_CREATOR_LLM_API_KIND: 'openai_chat',
GENARRATIVE_GAME_CREATOR_LLM_STREAM: 'true',
},
stdio: ['pipe', 'pipe', 'pipe'],
@@ -14,7 +14,7 @@ use platform_agent::{
route_game_creation_repair_issues,
};
use platform_llm::{
LlmClient, LlmConfig, LlmMessage, LlmProvider, LlmTextProtocol, LlmTextRequest,
LlmClient, LlmConfig, LlmMessage, LlmProvider, LlmApiKind, LlmRunRequest,
DEFAULT_RETRY_BACKOFF_MS,
};
use reqwest::header;
@@ -91,7 +91,7 @@ struct GameCreatorLlmConfigStatus {
api_key_present: bool,
base_url: Option<String>,
model: Option<String>,
protocol: String,
api_kind: String,
error: Option<String>,
}
@@ -1345,22 +1345,23 @@ fn build_game_creator_llm_client_from_env() -> Result<LlmClient, String> {
LlmClient::new(config).map_err(|error| format!("LLM client 初始化失败:{error}"))
}
fn read_game_creator_llm_protocol_from_env() -> Result<LlmTextProtocol, String> {
fn read_game_creator_llm_api_kind_from_env() -> Result<LlmApiKind, String> {
match read_first_non_empty_env(&[
"GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL",
"GENARRATIVE_LLM_PROTOCOL",
"LLM_PROTOCOL",
"GENARRATIVE_GAME_CREATOR_LLM_API_KIND",
"GENARRATIVE_LLM_API_KIND",
"LLM_API_KIND",
])
.unwrap_or_else(|| "responses".to_string())
.unwrap_or_else(|| "openai_responses".to_string())
.trim()
.to_ascii_lowercase()
.replace('-', "_")
.as_str()
{
"responses" | "response" | "responses_api" => Ok(LlmTextProtocol::Responses),
"chat_completions" | "chat_completion" | "chat" => Ok(LlmTextProtocol::ChatCompletions),
"openai_responses" => Ok(LlmApiKind::OpenAiResponses),
"openai_chat" => Ok(LlmApiKind::OpenAiChat),
"anthropic" => Ok(LlmApiKind::Anthropic),
value => Err(format!(
"LLM protocol 无效:{value},请使用 responses 或 chat_completions"
"LLM api_kind 无效:{value},请使用 openai_responses、openai_chat 或 anthropic"
)),
}
}
@@ -1387,12 +1388,12 @@ fn check_game_creator_llm_config_from_env() -> GameCreatorLlmConfigStatus {
]);
let mut status =
check_game_creator_llm_config_values(api_key.clone(), base_url.clone(), model.clone());
status.protocol = read_game_creator_llm_protocol_from_env()
.map(game_creator_llm_protocol_name)
status.api_kind = read_game_creator_llm_api_kind_from_env()
.map(game_creator_llm_api_kind_name)
.unwrap_or_else(|error| {
status.configured = false;
status.error = Some(error);
"responses".to_string()
"openai_responses".to_string()
});
if let Some(error) = local_env_error {
status.configured = false;
@@ -1448,15 +1449,16 @@ fn check_game_creator_llm_config_values(
api_key_present,
base_url,
model,
protocol: "responses".to_string(),
api_kind: "openai_responses".to_string(),
error,
}
}
fn game_creator_llm_protocol_name(protocol: LlmTextProtocol) -> String {
match protocol {
LlmTextProtocol::ChatCompletions => "chat_completions",
LlmTextProtocol::Responses => "responses",
fn game_creator_llm_api_kind_name(api_kind: LlmApiKind) -> String {
match api_kind {
LlmApiKind::OpenAiChat => "openai_chat",
LlmApiKind::OpenAiResponses => "openai_responses",
LlmApiKind::Anthropic => "anthropic",
}
.to_string()
}
@@ -1830,7 +1832,7 @@ async fn request_planner_spec_with_client(
short_memory: &str,
long_memory: &str,
) -> Result<String, String> {
let request = LlmTextRequest::new(vec![
let request = LlmRunRequest::new(vec![
LlmMessage::system(game_creator_planner_system_prompt()),
LlmMessage::user(game_creator_planner_user_prompt(
prompt,
@@ -1838,12 +1840,12 @@ async fn request_planner_spec_with_client(
long_memory,
)),
])
.with_protocol(read_game_creator_llm_protocol_from_env()?)
.with_max_tokens(GAME_CREATOR_PLANNER_MAX_OUTPUT_TOKENS);
.with_api_kind(read_game_creator_llm_api_kind_from_env()?)
.with_max_output_tokens(GAME_CREATOR_PLANNER_MAX_OUTPUT_TOKENS);
let response = request_game_creator_llm_text(client, request)
.await
.map_err(|error| format!("Planner 生成失败:{error}"))?;
let spec = strip_llm_thinking_blocks(response.content.as_str());
let spec = strip_llm_thinking_blocks(response.text.as_str());
if spec.is_empty() {
Err("Planner 未返回规格".to_string())
} else {
@@ -1878,12 +1880,12 @@ async fn request_generator_game_draft_with_client(
const MAX_EMPTY_RETRIES: u32 = 3;
let mut empty_retries = 0u32;
let response = loop {
let request = LlmTextRequest::new(vec![
let request = LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt.clone()),
])
.with_protocol(read_game_creator_llm_protocol_from_env()?)
.with_max_tokens(GAME_CREATOR_LLM_MAX_OUTPUT_TOKENS);
.with_api_kind(read_game_creator_llm_api_kind_from_env()?)
.with_max_output_tokens(GAME_CREATOR_LLM_MAX_OUTPUT_TOKENS);
match request_game_creator_llm_text(client, request).await {
Ok(response) => break response,
Err(platform_llm::LlmError::EmptyResponse) if empty_retries < MAX_EMPTY_RETRIES => {
@@ -1915,19 +1917,19 @@ async fn request_generator_game_draft_with_client(
};
// 先把原始返回落盘,再解析;解析失败(如输出截断)时仍能从仓库里拿到完整原文(仅 debug 构建)。
#[cfg(all(debug_assertions, not(test)))]
debug::persist_snapshot(response.content.as_str());
let content = strip_llm_thinking_blocks(response.content.as_str());
debug::persist_snapshot(response.text.as_str());
let content = strip_llm_thinking_blocks(response.text.as_str());
parse_llm_game_draft_response(content.as_str())
}
async fn request_game_creator_llm_text(
client: &LlmClient,
request: LlmTextRequest,
) -> Result<platform_llm::LlmTextResponse, platform_llm::LlmError> {
request: LlmRunRequest,
) -> Result<platform_llm::LlmRunResponse, platform_llm::LlmError> {
if game_creator_llm_stream_enabled() {
client.stream_text(request, |_| {}).await
client.stream_run(request, |_| {}).await
} else {
client.request_text(request).await
client.run(request).await
}
}
@@ -6175,7 +6177,7 @@ fn run_cli_command(command: CliCommand) -> Result<(), String> {
println!("llm.apiKeyPresent={}", status.api_key_present);
println!("llm.baseUrl={}", status.base_url.unwrap_or_default());
println!("llm.model={}", status.model.unwrap_or_default());
println!("llm.protocol={}", status.protocol);
println!("llm.apiKind={}", status.api_kind);
if let Some(error) = status.error {
println!("llm.error={error}");
}
@@ -6358,7 +6360,7 @@ mod tests {
GENARRATIVE_GAME_CREATOR_LLM_API_KEY=file-key
GENARRATIVE_GAME_CREATOR_LLM_BASE_URL="https://example.test/v1"
export GENARRATIVE_GAME_CREATOR_LLM_MODEL='model-from-file'
GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL=chat_completions
GENARRATIVE_GAME_CREATOR_LLM_API_KIND=openai_chat
GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
"#,
)
@@ -6367,12 +6369,12 @@ GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
let api_key = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KEY").ok();
let base_url = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL").ok();
let model = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_MODEL").ok();
let protocol = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL").ok();
let api_kind = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND").ok();
let stream = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_STREAM").ok();
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_API_KEY", "process-key");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_MODEL");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_STREAM");
load_game_creator_env_file(&env_path).expect("load local env");
@@ -6390,8 +6392,8 @@ GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
Ok("model-from-file")
);
assert_eq!(
std::env::var("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL").as_deref(),
Ok("chat_completions")
std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND").as_deref(),
Ok("openai_chat")
);
assert_eq!(
std::env::var("GENARRATIVE_GAME_CREATOR_LLM_STREAM").as_deref(),
@@ -6401,7 +6403,7 @@ GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
restore_env("GENARRATIVE_GAME_CREATOR_LLM_API_KEY", api_key);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL", base_url);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_MODEL", model);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL", protocol);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_API_KIND", api_kind);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_STREAM", stream);
fs::remove_dir_all(root).expect("cleanup test env dir");
}
@@ -6801,18 +6803,18 @@ GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
let previous_api_key = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KEY").ok();
let previous_base_url = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL").ok();
let previous_model = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_MODEL").ok();
let previous_protocol = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL").ok();
let previous_api_kind = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND").ok();
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_API_KEY", "test-key");
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL", base_url);
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_MODEL", "mock-game-model");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND");
let result = generate_local_game_draft_at(&root, "用上传角色图做主角", None).await;
restore_env("GENARRATIVE_GAME_CREATOR_LLM_API_KEY", previous_api_key);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL", previous_base_url);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_MODEL", previous_model);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL", previous_protocol);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_API_KIND", previous_api_kind);
result.expect("generated draft");
let requests = receiver.try_iter().collect::<Vec<_>>();
@@ -6843,12 +6845,38 @@ GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
Some("http://127.0.0.1:1/v1")
);
assert_eq!(configured.model.as_deref(), Some("mock-game-model"));
assert_eq!(configured.protocol, "responses");
assert_eq!(configured.api_kind, "openai_responses");
assert!(!serde_json::to_string(&configured)
.unwrap()
.contains("unit-test-api-key"));
}
#[test]
fn llm_api_kind_env_reads_canonical_names() {
let _env_guard = TEST_ENV_LOCK.lock().expect("test env lock");
let api_kind = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND").ok();
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND", "anthropic");
assert_eq!(
read_game_creator_llm_api_kind_from_env(),
Ok(LlmApiKind::Anthropic)
);
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND", "openai_chat");
assert_eq!(
read_game_creator_llm_api_kind_from_env(),
Ok(LlmApiKind::OpenAiChat)
);
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND", "openai_responses");
assert_eq!(
read_game_creator_llm_api_kind_from_env(),
Ok(LlmApiKind::OpenAiResponses)
);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_API_KIND", api_kind);
}
#[tokio::test]
async fn agent_loop_writes_spec_findings_and_retries_generator() {
let root = unique_project_path();
@@ -7320,11 +7348,11 @@ GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
let previous_api_key = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KEY").ok();
let previous_base_url = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL").ok();
let previous_model = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_MODEL").ok();
let previous_protocol = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL").ok();
let previous_api_kind = std::env::var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND").ok();
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_API_KEY", "test-key");
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL", base_url);
std::env::set_var("GENARRATIVE_GAME_CREATOR_LLM_MODEL", "mock-game-model");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL");
std::env::remove_var("GENARRATIVE_GAME_CREATOR_LLM_API_KIND");
let error = generate_local_game_draft_at(&root, "做一个会失败三轮的厨房游戏", None)
.await
@@ -7333,7 +7361,7 @@ GENARRATIVE_GAME_CREATOR_LLM_STREAM=true
restore_env("GENARRATIVE_GAME_CREATOR_LLM_API_KEY", previous_api_key);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_BASE_URL", previous_base_url);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_MODEL", previous_model);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL", previous_protocol);
restore_env("GENARRATIVE_GAME_CREATOR_LLM_API_KIND", previous_api_kind);
assert!(error.contains("已重试"));
assert!(error.contains(&GAME_CREATOR_AGENT_LOOP_MAX_PASSES.to_string()));
assert!(!root.join("memory/session.md").exists());
+2 -2
View File
@@ -59,7 +59,7 @@ interface GameCreatorLlmConfigStatus {
apiKeyPresent: boolean;
baseUrl: string | null;
model: string | null;
protocol: string;
apiKind: string;
error: string | null;
}
@@ -1614,7 +1614,7 @@ export function App() {
text: status.configured
? `LLM 已配置:${status.model ?? '未命名模型'} @ ${
status.baseUrl ?? '未设置 base_url'
}${status.protocol}API Key 已读取。`
}${status.apiKind}API Key 已读取。`
: `LLM 未就绪:${status.error ?? '配置不完整'}。API Key${
status.apiKeyPresent ? '已读取' : '未读取'
}`,
@@ -1376,7 +1376,7 @@ describe('AI 游戏创作 App 界面边界', () => {
apiKeyPresent: true,
baseUrl: 'https://llm.example.test/v1',
model: 'gpt-test',
protocol: 'responses',
apiKind: 'openai_responses',
error: null,
};
}
@@ -1389,7 +1389,7 @@ describe('AI 游戏创作 App 界面边界', () => {
expect(
await screen.findByText(
'LLM 已配置:gpt-test @ https://llm.example.test/v1responsesAPI Key 已读取。',
'LLM 已配置:gpt-test @ https://llm.example.test/v1openai_responsesAPI Key 已读取。',
),
).not.toBeNull();
expect(screen.queryByText(/sk-test-secret/)).toBeNull();
@@ -40,7 +40,7 @@
- 验证方式:运行 `cargo test -p platform-llm --manifest-path server-rs/Cargo.toml request_text_parses_non_stream_response`,并用真实 OpenAI-compatible 环境变量执行 `npm run ai-game-creator-shell:agent-run -- --no-wait /tmp/genarrative-ai-game-real-loop-test-6 "做一个像素风反弹弹幕厨房小游戏..."`,确认 36 个 trace step、36 次 tool call、`game.static_smoke``preview.start``preview.stop` 完成。
- 关联文档:`docs/technical/【技术方案】AI游戏创作智能体App实施计划-2026-06-24.md`
2026-06-27 追加:`platform-llm` `LlmTextRequest::new` 默认协议改为 Responses;旧 `/api/llm/chat/completions` 代理、RPG runtime chat 和需要旧测试网关的 AI 游戏创作 smoke 必须显式选择 Chat Completions。AI 游戏创作真实 LLM 默认 Responses,可用 `GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL=chat_completions` 兼容旧 OpenAI Chat Completions 网关
2026-06-29 追加:`platform-llm` `LlmTextRequest` / `LlmTextResponse` 已直接替换为 provider-neutral 的 `LlmRunRequest` / `LlmRunResponse`API kind 先固定为 `openai_chat``openai_responses``anthropic` 三类。AI 游戏创作 App 默认 `openai_responses`,可用 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=openai_chat` 接旧 Chat Completions 兼容网关,或用 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=anthropic` 接 Anthropic Messages。当前 run 响应只保留通用文本、finish reason、response id 和 usage,高级能力后续按 capability 扩展,不把业务层绑死到 Responses 字段
## 2026-06-24 AI 游戏创作 App 生成编排使用文件驱动 loop
@@ -94,7 +94,7 @@ game-project/
- `npm run check:native-shells`:覆盖 AI 游戏创作壳的 release/dev 窗口边界、正式用户 App 不嵌入游戏预览 iframe、用户侧预览命令交给外部浏览器和 Tauri release `--no-bundle` 构建 smoke;用于证明正式用户窗口只登记 `main` 聊天窗口,开发面板只在 debug/dev 路径打开,独立壳能完成 release 编译。
- `npm run check:encoding``git diff --check`:覆盖中文文档、中文命令文案和补丁空白;用于避免乱码、尾随空白和无关格式漂移。
- `npm run ai-game-creator-shell:llm-status`:只检查 LLM 环境变量是否就绪,不请求上游、不显示 API Key;用于本机联调前确认配置。CLI 和桌面 App 内的 `/llm-status` / 生成入口都会先读取仓库根目录或 `apps/ai-game-creator-shell/` 下 gitignored 的 `.env.secrets.local`,再检查当前进程环境。
- `npm run ai-game-creator-shell:agent-run -- --no-wait /绝对项目路径 "游戏创作需求"`:使用真实 OpenAI-compatible 配置跑一次本地生成、落盘、自检和预览;用于人工验收真实 provider 路径。真实 provider 可放在 gitignored 的 `.env.secrets.local` 中,至少包含 `GENARRATIVE_GAME_CREATOR_LLM_API_KEY``GENARRATIVE_GAME_CREATOR_LLM_BASE_URL``GENARRATIVE_GAME_CREATOR_LLM_MODEL`;默认按 Responses 协议请求,旧 Chat Completions 兼容网关需显式设置 `GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL=chat_completions`;真实网关长请求若在非流式响应前被 60 秒空闲连接切断,联调时设置 `GENARRATIVE_GAME_CREATOR_LLM_STREAM=true`
- `npm run ai-game-creator-shell:agent-run -- --no-wait /绝对项目路径 "游戏创作需求"`:使用真实 LLM provider 配置跑一次本地生成、落盘、自检和预览;用于人工验收真实 provider 路径。真实 provider 可放在 gitignored 的 `.env.secrets.local` 中,至少包含 `GENARRATIVE_GAME_CREATOR_LLM_API_KEY``GENARRATIVE_GAME_CREATOR_LLM_BASE_URL``GENARRATIVE_GAME_CREATOR_LLM_MODEL`;默认 API kind 为 `openai_responses`,旧 Chat Completions 兼容网关需显式设置 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=openai_chat`Anthropic Messages 网关设置为 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=anthropic`URL 会在 base URL 后拼 `/v1/messages`,例如 Minimax Anthropic base URL 可配置为 `https://api.minimaxi.com/anthropic`;真实网关长请求若在非流式响应前被 60 秒空闲连接切断,联调时设置 `GENARRATIVE_GAME_CREATOR_LLM_STREAM=true`
## 当前最小落地
@@ -104,12 +104,12 @@ game-project/
- 普通用户侧的生成、上传、运行、自检、预览状态 / 启动 / 打开 / 停止、记忆写入和画板资产导入都必须先完成 `/project` 初始化;未初始化时只提示设置本地项目,不落到默认临时目录。
- 终端可用 `npm run ai-game-creator-shell:llm-status` 检查 LLM 环境变量是否就绪;CLI 和桌面 App 内的 `/llm-status` / 生成入口都会先读取 gitignored 的 `.env.secrets.local`,不请求上游、不显示 API Key,缺配置时以非零状态退出或在聊天里提示未就绪。
- 终端可用 `npm run ai-game-creator-shell:check` 跑 v1 开发验收:壳 typecheck、`platform-agent` 编排测试、共享契约测试、Tauri Rust 测试和无密钥本地 provider 端到端 smoke。
- 终端可用 `npm run ai-game-creator-shell:agent-run -- /绝对项目路径 "游戏创作需求"` 跑一次真实 LLM 生成、落盘、`game.static_smoke` 和本地 HTTP 预览;该入口读取当前环境和 gitignored 的 `.env.secrets.local`,不把 API Key 写入仓库或项目文件。自动验证可加 `--no-wait`,例如 `npm run ai-game-creator-shell:agent-run -- --no-wait /tmp/genarrative-ai-game-test "像素风反弹弹幕厨房"`,生成预览 trace 后立即停止本地预览,避免终端卡在回车等待。默认协议为 Responses;旧 Chat Completions 兼容网关设置 `GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL=chat_completions`。真实 OpenAI-compatible 网关建议设置 `GENARRATIVE_GAME_CREATOR_LLM_STREAM=true` 跑 Planner 和 Generator,避免长请求非流式空闲断连。
- 终端可用 `npm run ai-game-creator-shell:agent-run -- /绝对项目路径 "游戏创作需求"` 跑一次真实 LLM 生成、落盘、`game.static_smoke` 和本地 HTTP 预览;该入口读取当前环境和 gitignored 的 `.env.secrets.local`,不把 API Key 写入仓库或项目文件。自动验证可加 `--no-wait`,例如 `npm run ai-game-creator-shell:agent-run -- --no-wait /tmp/genarrative-ai-game-test "像素风反弹弹幕厨房"`,生成预览 trace 后立即停止本地预览,避免终端卡在回车等待。默认 API kind 为 `openai_responses`;旧 Chat Completions 兼容网关设置 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=openai_chat`Anthropic Messages 网关设置 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=anthropic`。真实 OpenAI-compatible 网关建议设置 `GENARRATIVE_GAME_CREATOR_LLM_STREAM=true` 跑 Planner 和 Generator,避免长请求非流式空闲断连。
- 终端可用 `npm run ai-game-creator-shell:agent-run:smoke` 跑一次无密钥本地端到端 smoke:脚本启动本机 OpenAI-compatible SSE 流式测试 provider,预置一个本地上传图片和一个本地上传音频,复用真实 `--agent-run`、Planner / Orchestrator / 角色 agent / Generator / Evaluator loop、本地落盘、`game.static_smoke` 和本地 HTTP 预览,并断言每次 provider 请求都使用 `stream: true`、provider prompt 收到图片与音频资产上下文、生成 HTML 引用这些资产、预览服务能用 `GET` 读取 `/assets/...`、用 `HEAD` 返回真实资源长度和对应 MIME、headless Chrome 打开预览后至少执行一帧游戏 JS,且通过确定性亮色探针采样证明 canvas 不是空白画布、`.agent/run.latest.json` 的 step group 覆盖 design / balance / art / audio / code / publishing 六组、第二轮会重跑 Evaluator 命中任务及其下游影响任务,未受影响角色 carry-over;随后脚本自动给 CLI 发送回车停止预览。该脚本只用于开发验证,不进入产品生成路径。
- `npm run ai-game-creator-shell:dev` 的 Tauri `devUrl` 固定为 `http://127.0.0.1:3080/`Vite 必须 `strictPort` 对齐;`beforeDevCommand` 先复用已经跑在 3080 且页面标题为 `AI 游戏创作` 的本 app Vite server,否则才启动新的 Vite,若端口被其它服务占用则直接失败并提示释放端口。
- `.agent/manifest.json` 会保存 6 个专业组下 16 个组内角色任务状态,当前覆盖 `Director``Gameplay``Difficulty``Asset``Polish``SFX``Code``Review``Preview``Playtest``Publish`;程序组内显式包含 `quality-review` 质量评审 gate,由 Evaluator trace 标记完成;开发窗口的专业组面板读取 manifest,而不是前端硬编码。
- 共享契约和 `platform-agent` 会按任务依赖与 `completed` 状态计算当前可执行任务,作为 v1 的最小编排选择器;每轮 `Orchestrator` 的 activeTaskIds、carriedTaskIds、repairRoutes 和 dependencyWaves 由 `platform-agent` 纯编排内核产出,`apps/ai-game-creator-shell` 只负责写入 `.agent/passes/pass-N/` 和执行本地工具;`Evaluator` 会在 `.agent/findings.md` 写出 `## Repair Routes` JSON,下一轮编排优先采用该结构化 taskIds,解析不到时才退回关键词路由;返工路由会按任务图自动扩展下游影响任务,例如美术资产变化会继续触发程序预览和运营包装重算。
- `game.generate_draft` 使用 OpenAI-compatible LLM 配置生成结构化 JSON 草案,读取 `GENARRATIVE_GAME_CREATOR_LLM_API_KEY` / `GENARRATIVE_LLM_API_KEY` / `LLM_API_KEY` / `OPENAI_API_KEY``GENARRATIVE_GAME_CREATOR_LLM_BASE_URL` / `GENARRATIVE_LLM_BASE_URL` / `LLM_BASE_URL` / `OPENAI_BASE_URL``GENARRATIVE_GAME_CREATOR_LLM_MODEL` / `GENARRATIVE_LLM_MODEL` / `LLM_MODEL` / `OPENAI_MODEL`;默认协议为 Responses,可通过 `GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL=chat_completions` 切回旧 Chat Completions 兼容网关;`GENARRATIVE_GAME_CREATOR_LLM_STREAM=true` 时 Planner 和 Generator 使用流式请求;缺少配置或模型返回非法 JSON 时直接失败,不静默回退固定模板。
- `game.generate_draft` 使用 LLM provider 配置生成结构化 JSON 草案,读取 `GENARRATIVE_GAME_CREATOR_LLM_API_KEY` / `GENARRATIVE_LLM_API_KEY` / `LLM_API_KEY` / `OPENAI_API_KEY``GENARRATIVE_GAME_CREATOR_LLM_BASE_URL` / `GENARRATIVE_LLM_BASE_URL` / `LLM_BASE_URL` / `OPENAI_BASE_URL``GENARRATIVE_GAME_CREATOR_LLM_MODEL` / `GENARRATIVE_LLM_MODEL` / `LLM_MODEL` / `OPENAI_MODEL`;默认 API kind 为 `openai_responses`,可通过 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=openai_chat` 切回旧 Chat Completions 兼容网关,通过 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=anthropic` 走 Anthropic Messages`GENARRATIVE_GAME_CREATOR_LLM_STREAM=true` 时 Planner 和 Generator 使用流式请求;缺少配置或模型返回非法 JSON 时直接失败,不静默回退固定模板。
- 聊天输入 `/llm-status` 会触发只读 `llm.config_check`,确认 LLM base_url、model 和 API Key 是否已从环境变量读取;状态消息不会显示或保存 API Key。
- `game.generate_draft` 的 LLM JSON 必须包含 `handoffs` 数组,覆盖 `design``balance``art``audio``code``publishing` 6 个专业组;每组必须给出 role、summary、outputs 和 next,缺组或交接内容不完整会判定为模型输出无效并进入返工。
- `game.generate_draft` 的真实生成路径使用最小 Planner / Orchestrator / 组内角色 agent / Generator / Evaluator loopPlanner 写 `.agent/spec.md`;每轮 Orchestrator 先写 `.agent/passes/pass-N/agenda.md``.agent/passes/pass-N/task-graph.json`,首轮全量调度 16 个角色任务,返工轮按 `.agent/findings.md` 生成结构化 `repairRoutes`,重跑命中问题的角色任务及其下游依赖任务,其余角色 brief 从上一轮 carry-over`task-graph.json` 记录 activeTaskIds、carriedTaskIds、repairFocus、repairRoutes 和按依赖排序的 dependencyWaves;角色 brief 写入 `.agent/passes/pass-N/groups/<group>/*.md`,再汇总为 `.agent/passes/pass-N/groups/*.md`;Generator 必须读取用户需求、记忆、`.agent/spec.md`、本轮 `agenda.md``task-graph.json``.agent/findings.md` 和 6 组汇总 brief 后返回结构化 JSON;每轮会把 Generator 草案拆成 6 组交接快照,写入 `.agent/passes/pass-N/`Evaluator 做质量评审并写 `.agent/findings.md`,通过后才进入 `game.static_smoke` 静态自检和预览试玩。
@@ -455,7 +455,7 @@ OpenTelemetry 现阶段默认开启 OTLP traces / metrics / logs,但本地日
结构化创作 / RPG 的 Responses JSON 链路默认不打开 `web_search`;本地和生产如需联网增强,必须显式配置 `GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED=true``GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED=true`。如果上游未开通工具,Responses 可能先吐自然语言再返回 `ToolNotOpen`,这类报错应按工具不可用排查,不要先当成 JSON 解析 bug。
`platform-llm` 文本请求默认使用 Responses 协议;需要接旧 OpenAI Chat Completions 兼容网关时,调用方必须显式选择 Chat Completions。AI 游戏创作独立 App 也默认使用 Responses,可在本地 `.env.secrets.local` 中设置 `GENARRATIVE_GAME_CREATOR_LLM_PROTOCOL=chat_completions` 兼容旧测试网关。
`platform-llm` 文本请求默认使用 Responses 协议;需要接旧 OpenAI Chat Completions 兼容网关时,调用方必须显式选择 Chat Completions。AI 游戏创作独立 App 也默认使用 Responses,可在本地 `.env.secrets.local` 中设置 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=openai_chat` 接旧 Chat Completions 兼容网关,或 `GENARRATIVE_GAME_CREATOR_LLM_API_KIND=anthropic` 接 Anthropic Messages
创意 Agent `gpt-5` 文本链路已从 APIMart 切到 VectorEngine`api-server` 读取 `VECTOR_ENGINE_BASE_URL` / `VECTOR_ENGINE_API_KEY` 构造 OpenAI-compatible LLM client,并自动补齐 `/v1` 前缀用于 Responses 协议。排查或切换密钥后,可在本地运行:
@@ -2,7 +2,7 @@ use module_big_fish::{
BIG_FISH_MAX_LEVEL_COUNT, BIG_FISH_MIN_LEVEL_COUNT, BigFishAnchorPack, BigFishGameDraft,
BigFishLevelBlueprint, BigFishRuntimeParams, compile_default_draft,
};
use platform_llm::{LlmClient, LlmMessage, LlmTextRequest};
use platform_llm::{LlmClient, LlmMessage, LlmRunRequest};
use serde::Deserialize;
use serde_json::Value as JsonValue;
@@ -109,20 +109,20 @@ async fn request_big_fish_json_stage(
empty_response_message: &str,
) -> Result<JsonValue, BigFishDraftCompileError> {
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(BIG_FISH_DRAFT_JSON_ONLY_SYSTEM_PROMPT),
LlmMessage::user(user_prompt),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
.with_web_search(true),
)
.await
.map_err(|error| {
BigFishDraftCompileError::new(format!("{debug_label} LLM 请求失败:{error}"))
})?;
let text = response.content.trim();
let text = response.text.trim();
if text.is_empty() {
return Err(BigFishDraftCompileError::new(empty_response_message));
}
@@ -130,15 +130,15 @@ async fn request_big_fish_json_stage(
Ok(value) => Ok(value),
Err(_) => {
let repaired = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(BIG_FISH_DRAFT_JSON_REPAIR_SYSTEM_PROMPT),
LlmMessage::user(format!(
"请把下面这段文本修复成单个合法 JSON 对象,不要补充额外解释:\n\n{text}"
)),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api(),
.with_openai_responses(),
)
.await
.map_err(|error| {
@@ -146,7 +146,7 @@ async fn request_big_fish_json_stage(
"{debug_label} JSON 修复请求失败:{error}"
))
})?;
parse_json_response_text(repaired.content.as_str()).map_err(|error| {
parse_json_response_text(repaired.text.as_str()).map_err(|error| {
BigFishDraftCompileError::new(format!("{debug_label} JSON 解析失败:{error}"))
})
}
@@ -1,4 +1,4 @@
use platform_llm::{LlmClient, LlmError, LlmMessage, LlmStreamDelta, LlmTextRequest};
use platform_llm::{LlmClient, LlmError, LlmMessage, LlmStreamDelta, LlmRunRequest};
use serde_json::Value as JsonValue;
use crate::llm_model_routing::CREATION_TEMPLATE_LLM_MODEL;
@@ -150,7 +150,7 @@ where
F: FnMut(&str),
{
let response = llm_client
.stream_text(
.stream_run(
build_creation_agent_llm_request(system_prompt, user_prompt, enable_web_search),
|delta: &LlmStreamDelta| {
if !emit_reply_updates {
@@ -167,7 +167,7 @@ where
)
.await
.map_err(CreationAgentJsonTurnFailure::Stream)?;
let parsed = parse_json_response_text(response.content.as_str())
let parsed = parse_json_response_text(response.text.as_str())
.map_err(|_| CreationAgentJsonTurnFailure::Parse)?;
Ok(CreationAgentJsonTurnOutput { parsed })
@@ -184,14 +184,14 @@ fn build_creation_agent_llm_request(
system_prompt: String,
user_prompt: String,
enable_web_search: bool,
) -> LlmTextRequest {
) -> LlmRunRequest {
// 创作 Agent 是否联网由 api-server 配置集中传入,避免各玩法各自散落默认值。
LlmTextRequest::new(vec![
LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
.with_web_search(enable_web_search)
.with_request_timeout_ms(CREATION_AGENT_STREAM_REQUEST_TIMEOUT_MS)
}
@@ -203,17 +203,17 @@ pub(crate) async fn request_creation_agent_json_turn<E>(
build_error: impl Fn(String) -> E,
) -> Result<JsonValue, E> {
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api(),
.with_openai_responses(),
)
.await
.map_err(|error| build_error(error.to_string()))?;
parse_json_response_text(response.content.as_str())
parse_json_response_text(response.text.as_str())
.map_err(|error| build_error(error.to_string()))
}
@@ -331,7 +331,7 @@ mod tests {
assert!(request.enable_web_search);
assert_eq!(request.model.as_deref(), Some(CREATION_TEMPLATE_LLM_MODEL));
assert_eq!(request.protocol, platform_llm::LlmTextProtocol::Responses);
assert_eq!(request.api_kind, platform_llm::LlmApiKind::OpenAiResponses);
assert_eq!(request.messages.len(), 2);
assert_eq!(
request.request_timeout_ms,
@@ -1,4 +1,4 @@
use platform_llm::{LlmClient, LlmMessage, LlmTextRequest};
use platform_llm::{LlmClient, LlmMessage, LlmRunRequest};
use serde_json::{Map as JsonMap, Value as JsonValue};
use shared_contracts::runtime::ExecuteCustomWorldAgentActionRequest;
@@ -94,18 +94,18 @@ pub async fn generate_custom_world_agent_entities(
};
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
.with_web_search(true),
)
.await
.map_err(|error| format!("{action} LLM 请求失败:{error}"))?;
let generated_entities = parse_json_array_response(response.content.as_str())
let generated_entities = parse_json_array_response(response.text.as_str())
.map_err(|error| format!("{action} JSON 解析失败:{error}"))?;
let normalized_entities =
normalize_generated_entities(action, generated_entities, draft_profile, count);
@@ -17,7 +17,7 @@ use module_assets::{
AssetObjectAccessPolicy, AssetObjectFieldError, build_asset_entity_binding_input,
build_asset_object_upsert_input, generate_asset_binding_id, generate_asset_object_id,
};
use platform_llm::{LlmMessage, LlmTextRequest};
use platform_llm::{LlmMessage, LlmRunRequest};
use platform_oss::{
LegacyAssetPrefix, OssHeadObjectRequest, OssObjectAccess, OssSignedGetObjectUrlRequest,
};
@@ -1132,19 +1132,19 @@ async fn generate_entity_with_fallback(state: &AppState, profile: &Value, kind:
let Some(llm_client) = state.llm_client() else {
return fallback;
};
let request = LlmTextRequest::new(vec![
let request = LlmRunRequest::new(vec![
LlmMessage::system(build_result_entity_system_prompt()),
LlmMessage::user(build_result_entity_user_prompt(profile, kind, &fallback)),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
.with_web_search(true);
llm_client
.request_text(request)
.run(request)
.await
.ok()
.and_then(|response| serde_json::from_str::<Value>(response.content.trim()).ok())
.and_then(|response| serde_json::from_str::<Value>(response.text.trim()).ok())
.unwrap_or(fallback)
}
@@ -1157,7 +1157,7 @@ async fn generate_scene_npc_with_fallback(
let Some(llm_client) = state.llm_client() else {
return fallback;
};
let request = LlmTextRequest::new(vec![
let request = LlmRunRequest::new(vec![
LlmMessage::system(build_result_scene_npc_system_prompt()),
LlmMessage::user(build_result_scene_npc_user_prompt(
profile,
@@ -1166,14 +1166,14 @@ async fn generate_scene_npc_with_fallback(
)),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
.with_web_search(true);
llm_client
.request_text(request)
.run(request)
.await
.ok()
.and_then(|response| serde_json::from_str::<Value>(response.content.trim()).ok())
.and_then(|response| serde_json::from_str::<Value>(response.text.trim()).ok())
.unwrap_or(fallback)
}
@@ -6,7 +6,7 @@ use crate::prompt::foundation_draft::{
build_custom_world_role_outline_batch_json_repair_prompt,
build_custom_world_role_outline_batch_prompt,
};
use platform_llm::{LlmClient, LlmMessage, LlmTextRequest};
use platform_llm::{LlmClient, LlmMessage, LlmRunRequest};
use serde_json::{Map as JsonMap, Value as JsonValue, json};
use shared_contracts::runtime::ExecuteCustomWorldAgentActionRequest;
use spacetime_client::CustomWorldAgentSessionRecord;
@@ -195,7 +195,7 @@ where
enable_web_search,
)
.await?;
let text = response.content.trim();
let text = response.text.trim();
if text.is_empty() {
return Err(empty_response_message.to_string());
}
@@ -203,17 +203,17 @@ where
Ok(value) => Ok(value),
Err(_) => {
let repaired = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(FOUNDATION_JSON_REPAIR_SYSTEM_PROMPT),
LlmMessage::user(repair_prompt_builder(text)),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api(),
.with_openai_responses(),
)
.await
.map_err(|error| format!("{repair_debug_label} LLM 请求失败:{error}"))?;
parse_json_response_text(repaired.content.as_str())
parse_json_response_text(repaired.text.as_str())
.map_err(|error| format!("{repair_debug_label} JSON 解析失败:{error}"))
}
}
@@ -225,7 +225,7 @@ async fn request_foundation_text_with_optional_search_fallback(
user_prompt: &str,
debug_label: &str,
enable_web_search: bool,
) -> Result<platform_llm::LlmTextResponse, String> {
) -> Result<platform_llm::LlmRunResponse, String> {
match request_foundation_text(llm_client, system_prompt, user_prompt, enable_web_search).await {
Ok(response) => Ok(response),
Err(error) if enable_web_search && should_retry_foundation_without_web_search(&error) => {
@@ -247,15 +247,15 @@ async fn request_foundation_text(
system_prompt: &str,
user_prompt: &str,
enable_web_search: bool,
) -> Result<platform_llm::LlmTextResponse, platform_llm::LlmError> {
) -> Result<platform_llm::LlmRunResponse, platform_llm::LlmError> {
llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
.with_web_search(enable_web_search),
)
.await
+10 -10
View File
@@ -7,7 +7,7 @@ use axum::{
sse::{Event, Sse},
},
};
use platform_llm::{LlmMessage, LlmMessageRole, LlmTextProtocol, LlmTextRequest};
use platform_llm::{LlmMessage, LlmMessageRole, LlmApiKind, LlmRunRequest};
use serde_json::{Value, json};
use shared_contracts::llm::{
LlmChatCompletionRequest, LlmChatCompletionResponse, LlmChatMessagePayload, LlmChatMessageRole,
@@ -33,15 +33,15 @@ pub async fn proxy_llm_chat_completions(
)
})?;
let request = LlmTextRequest {
let request = LlmRunRequest {
model: payload.model,
protocol: LlmTextProtocol::ChatCompletions,
api_kind: LlmApiKind::OpenAiChat,
messages: payload
.messages
.into_iter()
.map(map_chat_message)
.collect::<Vec<_>>(),
max_tokens: None,
max_output_tokens: None,
enable_web_search: false,
request_timeout_ms: None,
};
@@ -51,7 +51,7 @@ pub async fn proxy_llm_chat_completions(
}
let response = llm_client
.request_text(request)
.run(request)
.await
.map_err(|error| llm_error_response(&request_context, map_llm_error(error)))?;
@@ -60,7 +60,7 @@ pub async fn proxy_llm_chat_completions(
LlmChatCompletionResponse {
id: response.response_id,
model: response.model,
content: response.content,
content: response.text,
finish_reason: response.finish_reason,
},
)
@@ -69,11 +69,11 @@ pub async fn proxy_llm_chat_completions(
fn stream_llm_chat_completions(
llm_client: platform_llm::LlmClient,
request: LlmTextRequest,
request: LlmRunRequest,
) -> Sse<impl tokio_stream::Stream<Item = Result<Event, Infallible>>> {
let stream = async_stream::stream! {
let (delta_tx, mut delta_rx) = tokio::sync::mpsc::unbounded_channel::<Value>();
let llm_stream = llm_client.stream_text(request, move |delta| {
let llm_stream = llm_client.stream_run(request, move |delta| {
let _ = delta_tx.send(json!({
"delta": delta.delta_text,
"content": delta.accumulated_text,
@@ -105,7 +105,7 @@ fn stream_llm_chat_completions(
json!(LlmChatCompletionResponse {
id: response.response_id,
model: response.model,
content: response.content,
content: response.text,
finish_reason: response.finish_reason,
}),
));
@@ -182,7 +182,7 @@ mod tests {
}
#[tokio::test]
async fn llm_chat_completions_returns_non_stream_text_payload() {
async fn llm_chat_completions_returns_non_stream_run_payload() {
let server_url = spawn_mock_server(vec![MockResponse {
status_line: "200 OK",
content_type: "application/json; charset=utf-8",
+1 -1
View File
@@ -21,7 +21,7 @@ use module_match3d::{
MATCH3D_MESSAGE_ID_PREFIX, MATCH3D_PROFILE_ID_PREFIX, MATCH3D_RUN_ID_PREFIX,
MATCH3D_SESSION_ID_PREFIX,
};
use platform_llm::{LlmMessage, LlmTextRequest};
use platform_llm::{LlmMessage, LlmRunRequest};
use platform_oss::{LegacyAssetPrefix, OssObjectAccess, OssPutObjectRequest};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -870,18 +870,18 @@ async fn generate_match3d_draft_plan(
config.theme_text, gameplay_item_count, generated_item_count
);
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_model(MATCH3D_WORK_METADATA_LLM_MODEL)
.with_responses_api(),
.with_openai_responses(),
)
.await;
match response {
Ok(response) => parse_match3d_draft_plan(response.content.as_str(), config)
Ok(response) => parse_match3d_draft_plan(response.text.as_str(), config)
.unwrap_or_else(|| fallback_match3d_draft_plan(config)),
Err(error) => {
tracing::warn!(
@@ -92,19 +92,19 @@ pub(super) async fn request_match3d_work_tags_with_llm(
summary.unwrap_or_default()
);
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system("你是抓大鹅作品标签编辑,只返回 JSON 字符串数组。"),
LlmMessage::user(user_prompt),
])
.with_model(MATCH3D_WORK_METADATA_LLM_MODEL)
.with_responses_api(),
.with_openai_responses(),
)
.await;
match response {
Ok(response) => {
let tags = parse_match3d_tags_from_text(response.content.as_str());
let tags = parse_match3d_tags_from_text(response.text.as_str());
if tags.len() >= MATCH3D_MIN_GENERATED_TAG_COUNT {
return Some(tags);
}
@@ -1,6 +1,6 @@
#![allow(dead_code)]
use platform_llm::{LlmMessage, LlmTextRequest};
use platform_llm::{LlmMessage, LlmRunRequest};
use serde_json::{Value as JsonValue, json};
use shared_contracts::visual_novel::{VisualNovelResultDraft, VisualNovelRuntimeStep};
@@ -285,36 +285,36 @@ pub(crate) fn build_visual_novel_repair_user_prompt(
pub(crate) fn build_visual_novel_creation_llm_request(
params: VisualNovelCreationPromptParams<'_>,
enable_web_search: bool,
) -> LlmTextRequest {
LlmTextRequest::new(vec![
) -> LlmRunRequest {
LlmRunRequest::new(vec![
LlmMessage::system(VISUAL_NOVEL_CREATION_SYSTEM_PROMPT),
LlmMessage::user(build_visual_novel_creation_user_prompt(params)),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
.with_web_search(enable_web_search)
}
pub(crate) fn build_visual_novel_runtime_llm_request(
params: VisualNovelRuntimePromptParams<'_>,
) -> LlmTextRequest {
LlmTextRequest::new(vec![
) -> LlmRunRequest {
LlmRunRequest::new(vec![
LlmMessage::system(VISUAL_NOVEL_RUNTIME_GM_SYSTEM_PROMPT),
LlmMessage::user(build_visual_novel_runtime_user_prompt(params)),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
}
pub(crate) fn build_visual_novel_repair_llm_request(
params: VisualNovelRepairPromptParams<'_>,
) -> LlmTextRequest {
LlmTextRequest::new(vec![
) -> LlmRunRequest {
LlmRunRequest::new(vec![
LlmMessage::system(VISUAL_NOVEL_REPAIR_SYSTEM_PROMPT),
LlmMessage::user(build_visual_novel_repair_user_prompt(params)),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api()
.with_openai_responses()
}
pub(crate) fn visual_novel_tool_descriptors() -> Vec<VisualNovelToolDescriptor> {
@@ -451,7 +451,7 @@ fn strip_json_code_fence(text: &str) -> &str {
#[cfg(test)]
mod tests {
use platform_llm::LlmTextProtocol;
use platform_llm::LlmApiKind;
use serde_json::json;
use super::*;
@@ -661,7 +661,7 @@ mod tests {
creation_request.model.as_deref(),
Some(CREATION_TEMPLATE_LLM_MODEL)
);
assert_eq!(creation_request.protocol, LlmTextProtocol::Responses);
assert_eq!(creation_request.api_kind, LlmApiKind::OpenAiResponses);
assert!(creation_request.enable_web_search);
assert!(
creation_request.messages[0]
@@ -687,7 +687,7 @@ mod tests {
runtime_request.model.as_deref(),
Some(CREATION_TEMPLATE_LLM_MODEL)
);
assert_eq!(runtime_request.protocol, LlmTextProtocol::Responses);
assert_eq!(runtime_request.api_kind, LlmApiKind::OpenAiResponses);
assert!(!runtime_request.enable_web_search);
assert!(
runtime_request.messages[0]
+1 -1
View File
@@ -19,7 +19,7 @@ use module_assets::{
build_asset_object_upsert_input, generate_asset_binding_id, generate_asset_object_id,
};
use module_puzzle::{PuzzleGeneratedImageCandidate, PuzzleRuntimeLevelStatus};
use platform_llm::{LlmMessage, LlmMessageContentPart, LlmTextRequest};
use platform_llm::{LlmMessage, LlmMessageContentPart, LlmRunRequest};
use platform_oss::{LegacyAssetPrefix, OssSignedGetObjectUrlRequest};
use platform_oss::{OssHeadObjectRequest, OssObjectAccess, OssPutObjectRequest};
use serde_json::{Value, json};
@@ -705,18 +705,18 @@ pub(crate) async fn generate_puzzle_first_level_name(
if let Some(llm_client) = state.llm_client() {
let user_prompt = build_puzzle_first_level_name_user_prompt(picture_description);
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(PUZZLE_FIRST_LEVEL_NAME_SYSTEM_PROMPT),
LlmMessage::user(user_prompt),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api(),
.with_openai_responses(),
)
.await;
match response {
Ok(response) => {
if let Some(naming) = parse_puzzle_level_naming_from_text(response.content.as_str())
if let Some(naming) = parse_puzzle_level_naming_from_text(response.text.as_str())
{
return naming;
}
@@ -758,8 +758,8 @@ pub(crate) async fn generate_puzzle_first_level_name_from_image(
};
let user_text = build_puzzle_first_level_name_vision_user_text(picture_description);
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(PUZZLE_FIRST_LEVEL_NAME_SYSTEM_PROMPT),
LlmMessage::user_multimodal(vec![
LlmMessageContentPart::InputText { text: user_text },
@@ -769,13 +769,13 @@ pub(crate) async fn generate_puzzle_first_level_name_from_image(
]),
])
.with_model(PUZZLE_LEVEL_NAME_VISION_LLM_MODEL)
.with_max_tokens(PUZZLE_LEVEL_NAME_VISION_MAX_TOKENS),
.with_max_output_tokens(PUZZLE_LEVEL_NAME_VISION_MAX_TOKENS),
)
.await;
match response {
Ok(response) => {
parse_puzzle_level_naming_from_text(response.content.as_str()).or_else(|| {
parse_puzzle_level_naming_from_text(response.text.as_str()).or_else(|| {
tracing::warn!(
provider = PUZZLE_AGENT_API_BASE_PROVIDER,
model = PUZZLE_LEVEL_NAME_VISION_LLM_MODEL,
@@ -8,19 +8,19 @@ pub(super) async fn generate_puzzle_work_tags(
if let Some(llm_client) = state.llm_client() {
let user_prompt = build_puzzle_tag_generation_user_prompt(work_title, work_description);
let response = llm_client
.request_text(
LlmTextRequest::new(vec![
.run(
LlmRunRequest::new(vec![
LlmMessage::system(PUZZLE_TAG_GENERATION_SYSTEM_PROMPT),
LlmMessage::user(user_prompt),
])
.with_model(CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api(),
.with_openai_responses(),
)
.await;
match response {
Ok(response) => {
let tags = normalize_puzzle_tag_candidates(parse_puzzle_tags_from_text(
response.content.as_str(),
response.text.as_str(),
));
if tags.len() == module_puzzle::PUZZLE_MAX_TAG_COUNT {
return tags;
+11 -11
View File
@@ -7,7 +7,7 @@ use axum::{
sse::{Event, Sse},
},
};
use platform_llm::{LlmMessage, LlmTextRequest};
use platform_llm::{LlmMessage, LlmRunRequest};
use serde::Deserialize;
use serde_json::{Value, json};
use shared_contracts::story::StoryRuntimeSnapshotPayload as RuntimeStorySnapshotPayload;
@@ -232,22 +232,22 @@ where
};
let reply_prompt = build_npc_chat_turn_reply_prompt(&prompt_input);
let mut reply_request = LlmTextRequest::new(vec![
let mut reply_request = LlmRunRequest::new(vec![
LlmMessage::system(NPC_CHAT_TURN_REPLY_SYSTEM_PROMPT),
LlmMessage::user(reply_prompt),
])
.with_chat_completions_api();
reply_request.max_tokens = Some(700);
.with_openai_chat();
reply_request.max_output_tokens = Some(700);
reply_request.enable_web_search = state.config.rpg_llm_web_search_enabled;
reply_request.model = Some(RPG_STORY_LLM_MODEL.to_string());
let reply_response = llm_client
.stream_text(reply_request, |delta| {
.stream_run(reply_request, |delta| {
on_reply_update(delta.accumulated_text.as_str());
})
.await
.ok()?;
let npc_reply = normalize_required_text(reply_response.content.as_str()).unwrap_or_else(|| {
let npc_reply = normalize_required_text(reply_response.text.as_str()).unwrap_or_else(|| {
build_deterministic_npc_reply(
npc_name,
payload.player_message.as_str(),
@@ -261,19 +261,19 @@ where
let suggestion_prompt =
build_npc_chat_turn_suggestion_prompt(&prompt_input, npc_reply.as_str());
let mut suggestion_request = LlmTextRequest::new(vec![
let mut suggestion_request = LlmRunRequest::new(vec![
LlmMessage::system(NPC_CHAT_TURN_SUGGESTION_SYSTEM_PROMPT),
LlmMessage::user(suggestion_prompt),
])
.with_chat_completions_api();
suggestion_request.max_tokens = Some(200);
.with_openai_chat();
suggestion_request.max_output_tokens = Some(200);
suggestion_request.enable_web_search = state.config.rpg_llm_web_search_enabled;
suggestion_request.model = Some(RPG_STORY_LLM_MODEL.to_string());
let suggestion_text = llm_client
.request_text(suggestion_request)
.run(suggestion_request)
.await
.ok()
.map(|response| response.content)
.map(|response| response.text)
.unwrap_or_default();
let (mut suggestions, mut function_suggestions, should_end_chat) =
parse_npc_chat_suggestion_resolution(
@@ -7,7 +7,7 @@ use axum::{
sse::{Event, Sse},
},
};
use platform_llm::{LlmMessage, LlmTextRequest};
use platform_llm::{LlmMessage, LlmRunRequest};
use serde::Deserialize;
use serde_json::{Value, json};
use shared_contracts::story::StoryRuntimeSnapshotPayload as RuntimeStorySnapshotPayload;
@@ -582,20 +582,20 @@ async fn request_runtime_plain_text(
return fallback_text.unwrap_or_default();
};
let mut request = LlmTextRequest::new(vec![
let mut request = LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_chat_completions_api();
request.max_tokens = Some(400);
.with_openai_chat();
request.max_output_tokens = Some(400);
request.enable_web_search = state.config.rpg_llm_web_search_enabled;
request.model = Some(RPG_STORY_LLM_MODEL.to_string());
llm_client
.request_text(request)
.run(request)
.await
.ok()
.map(|response| response.content.trim().to_string())
.map(|response| response.text.trim().to_string())
.filter(|text| !text.is_empty())
.or(fallback_text)
.unwrap_or_default()
@@ -615,22 +615,22 @@ fn stream_plain_text_response<'a>(
return;
};
let mut request = LlmTextRequest::new(vec![
let mut request = LlmRunRequest::new(vec![
LlmMessage::system(system_prompt),
LlmMessage::user(user_prompt),
])
.with_chat_completions_api();
request.max_tokens = Some(700);
.with_openai_chat();
request.max_output_tokens = Some(700);
request.enable_web_search = enable_web_search;
request.model = Some(RPG_STORY_LLM_MODEL.to_string());
let response = llm_client
.stream_text(request, |_| {})
.stream_run(request, |_| {})
.await;
match response {
Ok(response) => {
let final_text = response.content.trim();
let final_text = response.text.trim();
let output = if final_text.is_empty() {
fallback_text.as_str()
} else {
@@ -744,9 +744,9 @@ async fn generate_runtime_steps(
.max_assistant_step_count_per_turn,
},
);
if let Ok(response) = llm_client.request_text(request).await {
if let Ok(response) = llm_client.run(request).await {
if let Ok(steps) =
vn_prompt::parse_visual_novel_runtime_steps_fixture(response.content.as_str())
vn_prompt::parse_visual_novel_runtime_steps_fixture(response.text.as_str())
{
return Ok(steps);
}
@@ -1655,9 +1655,9 @@ async fn create_or_update_creation_draft(
},
false,
);
if let Ok(response) = llm_client.request_text(request).await {
if let Ok(response) = llm_client.run(request).await {
if let Ok(mut draft) =
vn_prompt::parse_visual_novel_result_draft_fixture(response.content.as_str())
vn_prompt::parse_visual_novel_result_draft_fixture(response.text.as_str())
{
prepare_draft_for_session(&mut draft, None, &now_iso);
return Ok(draft);
@@ -627,7 +627,7 @@ async fn resolve_wooden_fish_work_title(
let Some(llm_client) = state.llm_client() else {
return Ok(WOODEN_FISH_TEMPLATE_NAME.to_string());
};
let request = platform_llm::LlmTextRequest::new(vec![
let request = platform_llm::LlmRunRequest::new(vec![
platform_llm::LlmMessage::system(
"你是中文作品标题编辑。请根据敲木鱼作品描述生成一个适合卡片展示的简短中文标题,只输出纯文本,不要 JSON、标点解释或引号。",
),
@@ -636,11 +636,11 @@ async fn resolve_wooden_fish_work_title(
)),
])
.with_model(crate::llm_model_routing::CREATION_TEMPLATE_LLM_MODEL)
.with_responses_api();
let response = llm_client.request_text(request).await;
.with_openai_responses();
let response = llm_client.run(request).await;
match response {
Ok(response) => {
let title = normalize_wooden_fish_generated_work_title(response.content.as_str());
let title = normalize_wooden_fish_generated_work_title(response.text.as_str());
if title.is_empty() {
Ok(WOODEN_FISH_TEMPLATE_NAME.to_string())
} else {

Some files were not shown because too many files have changed in this diff Show More