File size: 3,043 Bytes
11486ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { buildAgentRequestDiagnostics } from '@/lib/agent-request-diagnostics';
import { ensureAgentStateStoreReady } from '@/lib/agent-state-runtime';
import type { AgentRequestRecord } from '@/lib/agent-state-store';
import { AgentApiError } from '@/lib/api-error-response';
import { appLogger } from '@/lib/app-logger';
import {
    feedbackRecordToResponse,
    isFeedbackStateStore,
    normalizeFeedbackTargetId,
    type FeedbackResponse,
    type FeedbackStateStore
} from '@/lib/feedback-store';

export async function readAgentRequestDiagnosticsByRequestId(requestId: string) {
    const normalizedRequestId = normalizeFeedbackTargetId(requestId);
    const store = await ensureAgentStateStoreReady();
    const record = await store.getRequest(normalizedRequestId);
    return record ? buildDiagnosticsForRecord(record) : undefined;
}

export async function readAgentRequestDiagnosticsByLookup(request: Request) {
    const searchParams = new URL(request.url).searchParams;
    const requestId = searchParams.get('request_id')?.trim();
    const idempotencyKey = searchParams.get('idempotency_key')?.trim();
    if (requestId && idempotencyKey) {
        throw new AgentApiError({
            code: 'validation_error',
            message: 'request_id 和 idempotency_key 只能提供一个。',
            status: 422,
            retryable: false
        });
    }
    if (requestId) {
        return readAgentRequestDiagnosticsByRequestId(requestId);
    }
    if (idempotencyKey) {
        const normalizedIdempotencyKey = normalizeFeedbackTargetId(idempotencyKey);
        const store = await ensureAgentStateStoreReady();
        const record = await store.getRequestByIdempotencyKey(normalizedIdempotencyKey);
        return record ? buildDiagnosticsForRecord(record) : undefined;
    }
    throw new AgentApiError({
        code: 'validation_error',
        message: '必须提供 request_id 或 idempotency_key 查询参数。',
        status: 422,
        retryable: false
    });
}

async function buildDiagnosticsForRecord(record: AgentRequestRecord) {
    const store = await ensureAgentStateStoreReady();
    const artifacts = await store.listArtifactsForRequest(record.requestId);
    const feedback = isFeedbackStateStore(store) ? await readDiagnosticsFeedback(store, record.requestId) : undefined;
    return buildAgentRequestDiagnostics({
        record,
        artifacts,
        env: process.env,
        ...(feedback ? { feedback } : {})
    });
}

async function readDiagnosticsFeedback(
    store: FeedbackStateStore,
    requestId: string
): Promise<FeedbackResponse | undefined> {
    try {
        const value = await store.readFeedback('agent_request', requestId);
        return value ? feedbackRecordToResponse(value) : undefined;
    } catch (error) {
        appLogger.warn('读取 Agent request feedback 失败,继续返回无 feedback 的诊断。', {
            requestId,
            error: error instanceof Error ? error.message : String(error)
        });
        return undefined;
    }
}