feat: 搭建阶段 0 与阶段 1 基础工程
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@cross-ai/report-schema",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsup src/index.ts --dts --format esm,cjs --clean",
|
||||
"test": "vitest run",
|
||||
"typecheck": "tsc -p tsconfig.json --noEmit",
|
||||
"lint": "tsc -p tsconfig.json --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cross-ai/domain": "file:../domain",
|
||||
"zod": "^4.3.6"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import {
|
||||
confidenceLevels,
|
||||
evidenceSourceTypes,
|
||||
executionStatuses,
|
||||
platforms,
|
||||
reportableTaskStatuses,
|
||||
sampleFlags
|
||||
} from "@cross-ai/domain";
|
||||
import { z } from "zod";
|
||||
|
||||
export const SourceScopeSchema = z.object({
|
||||
platforms: z.array(z.enum(platforms)).min(1),
|
||||
link_count: z.number().int().nonnegative(),
|
||||
review_count: z.number().int().nonnegative()
|
||||
});
|
||||
|
||||
export const InsightCardSchema = z
|
||||
.object({
|
||||
card_id: z.string().min(1),
|
||||
title: z.string().min(1),
|
||||
statement: z.string().min(1),
|
||||
confidence: z.enum(confidenceLevels),
|
||||
sample_flag: z.enum(sampleFlags),
|
||||
source_scope: SourceScopeSchema,
|
||||
evidence_ids: z.array(z.string().min(1))
|
||||
})
|
||||
.superRefine((value, ctx) => {
|
||||
if (value.sample_flag !== "insufficient" && value.evidence_ids.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Non-insufficient insights must include evidence_ids.",
|
||||
path: ["evidence_ids"]
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const EvidenceSchema = z.object({
|
||||
evidence_id: z.string().min(1),
|
||||
platform: z.enum(platforms),
|
||||
source_type: z.enum(evidenceSourceTypes),
|
||||
source_url: z.string().url(),
|
||||
review_ref: z.string().nullable(),
|
||||
snippet: z.string().min(1),
|
||||
captured_at: z.string().datetime()
|
||||
});
|
||||
|
||||
export const PlatformInsightSchema = z.object({
|
||||
platform: z.enum(platforms),
|
||||
execution_status: z.enum(executionStatuses),
|
||||
selected_link_count: z.number().int().nonnegative(),
|
||||
price_range: z
|
||||
.object({
|
||||
min: z.number().nonnegative(),
|
||||
max: z.number().nonnegative()
|
||||
})
|
||||
.nullable(),
|
||||
selling_points: z.array(InsightCardSchema),
|
||||
positive_themes: z.array(InsightCardSchema),
|
||||
negative_themes: z.array(InsightCardSchema),
|
||||
store_diff_notes: z.array(InsightCardSchema)
|
||||
});
|
||||
|
||||
export const ReportSchema = z.object({
|
||||
report_id: z.string().min(1),
|
||||
report_version: z.number().int().positive(),
|
||||
task_id: z.string().min(1),
|
||||
generated_at: z.string().datetime(),
|
||||
task_status: z.enum(reportableTaskStatuses),
|
||||
summary: z.object({
|
||||
headline: z.string().min(1),
|
||||
key_points: z.array(z.string().min(1)).min(1),
|
||||
limitations: z.array(z.string().min(1))
|
||||
}),
|
||||
product_snapshot: z.object({
|
||||
query: z.string().min(1),
|
||||
normalized_product_name: z.string().min(1),
|
||||
platform_count: z.number().int().nonnegative(),
|
||||
selected_link_count: z.number().int().nonnegative(),
|
||||
review_sample_count: z.number().int().nonnegative(),
|
||||
analysis_time_range: z.object({
|
||||
start: z.string().datetime(),
|
||||
end: z.string().datetime()
|
||||
})
|
||||
}),
|
||||
platform_insights: z.array(PlatformInsightSchema),
|
||||
cross_platform_insights: z.array(InsightCardSchema),
|
||||
recommendations: z.array(InsightCardSchema),
|
||||
evidence_index: z.array(EvidenceSchema),
|
||||
quality_flags: z.object({
|
||||
sample_insufficient: z.boolean(),
|
||||
partial_platform_failure: z.boolean(),
|
||||
blocked_platforms: z.array(z.enum(platforms)),
|
||||
failed_platforms: z.array(z.enum(platforms))
|
||||
})
|
||||
});
|
||||
|
||||
export type ReportSnapshot = z.infer<typeof ReportSchema>;
|
||||
|
||||
export function parseReportSnapshot(input: unknown): ReportSnapshot {
|
||||
return ReportSchema.parse(input);
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseReportSnapshot } from "../src/index";
|
||||
|
||||
describe("ReportSchema", () => {
|
||||
it("accepts a valid report snapshot", () => {
|
||||
const report = parseReportSnapshot({
|
||||
report_id: "report-1",
|
||||
report_version: 1,
|
||||
task_id: "task-1",
|
||||
generated_at: "2026-04-02T12:00:00.000Z",
|
||||
task_status: "Completed",
|
||||
summary: {
|
||||
headline: "天猫样本较完整,京东待预热。",
|
||||
key_points: ["天猫完成候选确认并产出报告。"],
|
||||
limitations: ["京东本次未进入搜索。"]
|
||||
},
|
||||
product_snapshot: {
|
||||
query: "iPhone 15 Pro",
|
||||
normalized_product_name: "iPhone 15 Pro",
|
||||
platform_count: 2,
|
||||
selected_link_count: 1,
|
||||
review_sample_count: 60,
|
||||
analysis_time_range: {
|
||||
start: "2026-04-02T11:40:00.000Z",
|
||||
end: "2026-04-02T12:00:00.000Z"
|
||||
}
|
||||
},
|
||||
platform_insights: [
|
||||
{
|
||||
platform: "tmall",
|
||||
execution_status: "completed",
|
||||
selected_link_count: 1,
|
||||
price_range: { min: 7999, max: 7999 },
|
||||
selling_points: [
|
||||
{
|
||||
card_id: "card-1",
|
||||
title: "卖点稳定",
|
||||
statement: "标题与详情页都突出影像能力。",
|
||||
confidence: "high",
|
||||
sample_flag: "sufficient",
|
||||
source_scope: {
|
||||
platforms: ["tmall"],
|
||||
link_count: 1,
|
||||
review_count: 60
|
||||
},
|
||||
evidence_ids: ["evidence-1"]
|
||||
}
|
||||
],
|
||||
positive_themes: [],
|
||||
negative_themes: [],
|
||||
store_diff_notes: []
|
||||
},
|
||||
{
|
||||
platform: "jd",
|
||||
execution_status: "blocked",
|
||||
selected_link_count: 0,
|
||||
price_range: null,
|
||||
selling_points: [],
|
||||
positive_themes: [],
|
||||
negative_themes: [],
|
||||
store_diff_notes: []
|
||||
}
|
||||
],
|
||||
cross_platform_insights: [
|
||||
{
|
||||
card_id: "card-2",
|
||||
title: "跨平台覆盖有限",
|
||||
statement: "本轮仅天猫形成可发布洞察。",
|
||||
confidence: "medium",
|
||||
sample_flag: "partial",
|
||||
source_scope: {
|
||||
platforms: ["tmall", "jd"],
|
||||
link_count: 1,
|
||||
review_count: 60
|
||||
},
|
||||
evidence_ids: ["evidence-1"]
|
||||
}
|
||||
],
|
||||
recommendations: [
|
||||
{
|
||||
card_id: "card-3",
|
||||
title: "优先补齐京东会话",
|
||||
statement: "建议在下一次任务前先完成京东会话准备。",
|
||||
confidence: "medium",
|
||||
sample_flag: "partial",
|
||||
source_scope: {
|
||||
platforms: ["jd"],
|
||||
link_count: 0,
|
||||
review_count: 0
|
||||
},
|
||||
evidence_ids: ["evidence-1"]
|
||||
}
|
||||
],
|
||||
evidence_index: [
|
||||
{
|
||||
evidence_id: "evidence-1",
|
||||
platform: "tmall",
|
||||
source_type: "product",
|
||||
source_url: "https://example.com/tmall/iphone-15-pro",
|
||||
review_ref: null,
|
||||
snippet: "详情页强调 5 倍长焦与钛金属材质。",
|
||||
captured_at: "2026-04-02T11:58:00.000Z"
|
||||
}
|
||||
],
|
||||
quality_flags: {
|
||||
sample_insufficient: false,
|
||||
partial_platform_failure: false,
|
||||
blocked_platforms: [],
|
||||
failed_platforms: []
|
||||
}
|
||||
});
|
||||
|
||||
expect(report.report_version).toBe(1);
|
||||
expect(report.platform_insights).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("rejects a strong insight without evidence ids", () => {
|
||||
expect(() =>
|
||||
parseReportSnapshot({
|
||||
report_id: "report-2",
|
||||
report_version: 1,
|
||||
task_id: "task-2",
|
||||
generated_at: "2026-04-02T12:00:00.000Z",
|
||||
task_status: "Completed",
|
||||
summary: {
|
||||
headline: "invalid",
|
||||
key_points: ["invalid"],
|
||||
limitations: []
|
||||
},
|
||||
product_snapshot: {
|
||||
query: "x",
|
||||
normalized_product_name: "x",
|
||||
platform_count: 1,
|
||||
selected_link_count: 1,
|
||||
review_sample_count: 1,
|
||||
analysis_time_range: {
|
||||
start: "2026-04-02T11:40:00.000Z",
|
||||
end: "2026-04-02T12:00:00.000Z"
|
||||
}
|
||||
},
|
||||
platform_insights: [],
|
||||
cross_platform_insights: [
|
||||
{
|
||||
card_id: "card-1",
|
||||
title: "缺少证据",
|
||||
statement: "这里没有 evidence ids。",
|
||||
confidence: "high",
|
||||
sample_flag: "sufficient",
|
||||
source_scope: {
|
||||
platforms: ["tmall"],
|
||||
link_count: 1,
|
||||
review_count: 1
|
||||
},
|
||||
evidence_ids: []
|
||||
}
|
||||
],
|
||||
recommendations: [],
|
||||
evidence_index: [],
|
||||
quality_flags: {
|
||||
sample_insufficient: false,
|
||||
partial_platform_failure: false,
|
||||
blocked_platforms: [],
|
||||
failed_platforms: []
|
||||
}
|
||||
})
|
||||
).toThrow(/evidence_ids/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"types": [
|
||||
"node",
|
||||
"vitest/globals"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"test/**/*.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user