Commit d0522a83 authored by yaoke.yk's avatar yaoke.yk

Agent制作优化上下文错误

parent 3aa33f49
package com.yaoai.agent.service.impl; package com.yaoai.agent.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.yaoai.agent.service.AgentRunService; import com.yaoai.agent.service.AgentRunService;
import com.yaoai.agent.sse.AgentSseManager; import com.yaoai.agent.sse.AgentSseManager;
import com.yaoai.ai.core.model.ChatMessage; import com.yaoai.ai.core.model.ChatMessage;
import com.yaoai.ai.core.model.ChatRequest; import com.yaoai.ai.core.model.ChatRequest;
import com.yaoai.ai.core.service.LlmService; import com.yaoai.ai.core.service.LlmService;
import com.yaoai.common.context.UserContext;
import com.yaoai.common.exception.BizException; import com.yaoai.common.exception.BizException;
import com.yaoai.common.exception.ErrorCode; import com.yaoai.common.exception.ErrorCode;
import com.yaoai.domain.entity.Episode; import com.yaoai.domain.entity.Episode;
...@@ -66,6 +68,7 @@ public class AgentRunServiceImpl implements AgentRunService { ...@@ -66,6 +68,7 @@ public class AgentRunServiceImpl implements AgentRunService {
@Override @Override
public GraphRun startRun(Long projectId, Long tenantId, String goal) { public GraphRun startRun(Long projectId, Long tenantId, String goal) {
Long userId = StpUtil.getLoginIdAsLong();
GraphRun run = new GraphRun(); GraphRun run = new GraphRun();
run.setProjectId(projectId); run.setProjectId(projectId);
run.setTenantId(tenantId); run.setTenantId(tenantId);
...@@ -86,7 +89,7 @@ public class AgentRunServiceImpl implements AgentRunService { ...@@ -86,7 +89,7 @@ public class AgentRunServiceImpl implements AgentRunService {
} }
Long runId = run.getId(); Long runId = run.getId();
agentExecutor.execute(() -> executeRun(runId, projectId, tenantId, goal)); agentExecutor.execute(() -> executeRun(runId, projectId, tenantId, userId, goal));
return run; return run;
} }
...@@ -123,8 +126,9 @@ public class AgentRunServiceImpl implements AgentRunService { ...@@ -123,8 +126,9 @@ public class AgentRunServiceImpl implements AgentRunService {
// ─── Async Executor ──────────────────────────────────────────────────────── // ─── Async Executor ────────────────────────────────────────────────────────
private void executeRun(Long runId, Long projectId, Long tenantId, String goal) { private void executeRun(Long runId, Long projectId, Long tenantId, Long userId, String goal) {
log.info("Agent run started: runId={}, projectId={}", runId, projectId); log.info("Agent run started: runId={}, projectId={}", runId, projectId);
UserContext.set(userId);
try { try {
// Step 1: Decision // Step 1: Decision
executeStep(runId, "decision", () -> { executeStep(runId, "decision", () -> {
...@@ -210,6 +214,8 @@ public class AgentRunServiceImpl implements AgentRunService { ...@@ -210,6 +214,8 @@ public class AgentRunServiceImpl implements AgentRunService {
log.error("Agent run failed: runId={}", runId, e); log.error("Agent run failed: runId={}", runId, e);
updateRunStatus(runId, "FAILED"); updateRunStatus(runId, "FAILED");
sseManager.emit(runId, "run.failed", Map.of("error", e.getMessage())); sseManager.emit(runId, "run.failed", Map.of("error", e.getMessage()));
} finally {
UserContext.clear();
} }
} }
......
package com.yaoai.common.context;
public final class UserContext {
private static final ThreadLocal<Long> USER_ID = new ThreadLocal<>();
private UserContext() {}
public static void set(Long userId) {
USER_ID.set(userId);
}
public static Long get() {
return USER_ID.get();
}
public static Long require() {
Long userId = USER_ID.get();
if (userId == null) {
throw new IllegalStateException("当前线程缺少用户上下文");
}
return userId;
}
public static void clear() {
USER_ID.remove();
}
}
package com.yaoai.pipeline.service.impl; package com.yaoai.pipeline.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaoai.ai.core.model.ChatMessage; import com.yaoai.ai.core.model.ChatMessage;
...@@ -9,6 +8,7 @@ import com.yaoai.ai.core.service.LlmService; ...@@ -9,6 +8,7 @@ import com.yaoai.ai.core.service.LlmService;
import com.yaoai.ai.providers.service.SeedreamService; import com.yaoai.ai.providers.service.SeedreamService;
import com.yaoai.billing.dto.BillingChargeRequest; import com.yaoai.billing.dto.BillingChargeRequest;
import com.yaoai.billing.service.BillingService; import com.yaoai.billing.service.BillingService;
import com.yaoai.common.context.UserContext;
import com.yaoai.common.exception.BizException; import com.yaoai.common.exception.BizException;
import com.yaoai.common.exception.ErrorCode; import com.yaoai.common.exception.ErrorCode;
import com.yaoai.domain.entity.Character; import com.yaoai.domain.entity.Character;
...@@ -100,7 +100,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -100,7 +100,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("character_extract") .operation("character_extract")
.modality("TEXT") .modality("TEXT")
...@@ -136,7 +136,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -136,7 +136,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
} }
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("character_extract") .operation("character_extract")
.modality("TEXT") .modality("TEXT")
...@@ -160,7 +160,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -160,7 +160,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("scene_extract") .operation("scene_extract")
.modality("TEXT") .modality("TEXT")
...@@ -192,7 +192,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -192,7 +192,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
} }
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("scene_extract") .operation("scene_extract")
.modality("TEXT") .modality("TEXT")
...@@ -220,7 +220,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -220,7 +220,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(character.getProjectId()) .projectId(character.getProjectId())
.operation("character_image_generate") .operation("character_image_generate")
.modality("IMAGE") .modality("IMAGE")
...@@ -253,7 +253,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -253,7 +253,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
characterMapper.updateById(character); characterMapper.updateById(character);
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(character.getProjectId()) .projectId(character.getProjectId())
.operation("character_image_generate") .operation("character_image_generate")
.modality("IMAGE") .modality("IMAGE")
...@@ -291,7 +291,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -291,7 +291,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(scene.getProjectId()) .projectId(scene.getProjectId())
.operation("scene_image_generate") .operation("scene_image_generate")
.modality("IMAGE") .modality("IMAGE")
...@@ -317,7 +317,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService { ...@@ -317,7 +317,7 @@ public class AssetGenPipelineServiceImpl implements AssetGenPipelineService {
sceneMapper.updateById(scene); sceneMapper.updateById(scene);
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(scene.getProjectId()) .projectId(scene.getProjectId())
.operation("scene_image_generate") .operation("scene_image_generate")
.modality("IMAGE") .modality("IMAGE")
......
package com.yaoai.pipeline.service.impl; package com.yaoai.pipeline.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaoai.ai.core.model.ChatMessage; import com.yaoai.ai.core.model.ChatMessage;
...@@ -8,6 +7,7 @@ import com.yaoai.ai.core.model.ChatRequest; ...@@ -8,6 +7,7 @@ import com.yaoai.ai.core.model.ChatRequest;
import com.yaoai.ai.core.service.LlmService; import com.yaoai.ai.core.service.LlmService;
import com.yaoai.billing.dto.BillingChargeRequest; import com.yaoai.billing.dto.BillingChargeRequest;
import com.yaoai.billing.service.BillingService; import com.yaoai.billing.service.BillingService;
import com.yaoai.common.context.UserContext;
import com.yaoai.common.exception.BizException; import com.yaoai.common.exception.BizException;
import com.yaoai.common.exception.ErrorCode; import com.yaoai.common.exception.ErrorCode;
import com.yaoai.domain.entity.Episode; import com.yaoai.domain.entity.Episode;
...@@ -73,7 +73,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService { ...@@ -73,7 +73,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService {
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("outline_generate") .operation("outline_generate")
.modality("TEXT") .modality("TEXT")
...@@ -106,7 +106,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService { ...@@ -106,7 +106,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService {
outlineMapper.insert(outline); outlineMapper.insert(outline);
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("outline_generate") .operation("outline_generate")
.modality("TEXT") .modality("TEXT")
...@@ -147,7 +147,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService { ...@@ -147,7 +147,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService {
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("episode_generate") .operation("episode_generate")
.modality("TEXT") .modality("TEXT")
...@@ -181,7 +181,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService { ...@@ -181,7 +181,7 @@ public class OutlinePipelineServiceImpl implements OutlinePipelineService {
} }
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("episode_generate") .operation("episode_generate")
.modality("TEXT") .modality("TEXT")
......
package com.yaoai.pipeline.service.impl; package com.yaoai.pipeline.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaoai.ai.core.model.ChatMessage; import com.yaoai.ai.core.model.ChatMessage;
...@@ -8,6 +7,7 @@ import com.yaoai.ai.core.model.ChatRequest; ...@@ -8,6 +7,7 @@ import com.yaoai.ai.core.model.ChatRequest;
import com.yaoai.ai.core.service.LlmService; import com.yaoai.ai.core.service.LlmService;
import com.yaoai.billing.dto.BillingChargeRequest; import com.yaoai.billing.dto.BillingChargeRequest;
import com.yaoai.billing.service.BillingService; import com.yaoai.billing.service.BillingService;
import com.yaoai.common.context.UserContext;
import com.yaoai.common.exception.BizException; import com.yaoai.common.exception.BizException;
import com.yaoai.common.exception.ErrorCode; import com.yaoai.common.exception.ErrorCode;
import com.yaoai.domain.entity.Character; import com.yaoai.domain.entity.Character;
...@@ -90,7 +90,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService ...@@ -90,7 +90,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("storyboard_generate") .operation("storyboard_generate")
.modality("TEXT") .modality("TEXT")
...@@ -131,7 +131,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService ...@@ -131,7 +131,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
} }
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(projectId) .projectId(projectId)
.operation("storyboard_generate") .operation("storyboard_generate")
.modality("TEXT") .modality("TEXT")
...@@ -299,7 +299,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService ...@@ -299,7 +299,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
storyboardId, sb.getEpisodeId(), characters.size(), scenes.size()); storyboardId, sb.getEpisodeId(), characters.size(), scenes.size());
billingService.checkBalance(BillingChargeRequest.builder() billingService.checkBalance(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(sb.getProjectId()) .projectId(sb.getProjectId())
.operation("storyboard_prompt_generate") .operation("storyboard_prompt_generate")
.modality("TEXT") .modality("TEXT")
...@@ -312,7 +312,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService ...@@ -312,7 +312,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
String prompt = llmService.chat(request); String prompt = llmService.chat(request);
billingService.charge(BillingChargeRequest.builder() billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId) .tenantId(tenantId)
.userId(StpUtil.getLoginIdAsLong()) .userId(UserContext.require())
.projectId(sb.getProjectId()) .projectId(sb.getProjectId())
.operation("storyboard_prompt_generate") .operation("storyboard_prompt_generate")
.modality("TEXT") .modality("TEXT")
......
...@@ -4,6 +4,7 @@ import cn.dev33.satoken.exception.NotLoginException; ...@@ -4,6 +4,7 @@ import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.interceptor.SaInterceptor; import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter; import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.yaoai.common.context.UserContext;
import com.yaoai.security.context.TenantContext; import com.yaoai.security.context.TenantContext;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
...@@ -34,6 +35,7 @@ public class SaTokenConfigure implements WebMvcConfigurer { ...@@ -34,6 +35,7 @@ public class SaTokenConfigure implements WebMvcConfigurer {
.notMatch(OPEN_PATHS) .notMatch(OPEN_PATHS)
.check(r -> { .check(r -> {
StpUtil.checkLogin(); StpUtil.checkLogin();
UserContext.set(StpUtil.getLoginIdAsLong());
// 登录后将 tenantId 写入 ThreadLocal,供业务层使用 // 登录后将 tenantId 写入 ThreadLocal,供业务层使用
Object tenantId = StpUtil.getSession().get("tenantId"); Object tenantId = StpUtil.getSession().get("tenantId");
if (tenantId != null) { if (tenantId != null) {
...@@ -48,6 +50,7 @@ public class SaTokenConfigure implements WebMvcConfigurer { ...@@ -48,6 +50,7 @@ public class SaTokenConfigure implements WebMvcConfigurer {
public void afterCompletion(HttpServletRequest req, public void afterCompletion(HttpServletRequest req,
HttpServletResponse res, HttpServletResponse res,
Object handler, Exception ex) { Object handler, Exception ex) {
UserContext.clear();
TenantContext.clear(); TenantContext.clear();
} }
}).addPathPatterns("/**"); }).addPathPatterns("/**");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment