forked from gianyrox/feed402
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
169 lines (148 loc) · 4.61 KB
/
types.ts
File metadata and controls
169 lines (148 loc) · 4.61 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* feed402 v0.2 — shared types
* These mirror SPEC.md §1 (manifest), §3 (envelope), §4 (index manifest).
* Keep this file in sync with the spec; it is intentionally small.
*
* v0.2 is fully backwards-compatible with v0.1: every new field is optional,
* and v0.1 consumers must ignore unknown fields per SPEC §2.3.
*/
/** Canonical protocol version string emitted in `Manifest.spec`. */
export const SPEC_VERSION = "feed402/0.2" as const;
// ---------- §1: Discovery manifest ----------
export type TierName = "raw" | "query" | "insight";
export interface TierSpec {
path: string;
price_usd: number;
unit: "row" | "call";
}
export interface Manifest {
name: string;
version: string;
/** Protocol version, e.g. "feed402/0.2". Use the SPEC_VERSION constant. */
spec: string;
chain: "base" | "base-sepolia" | string;
wallet: `0x${string}`;
tiers: Partial<Record<TierName, TierSpec>>;
schema_url?: string;
citation_policy?: string;
citation_types: CitationType[];
contact?: string;
/**
* §4 (v0.2, optional) — retrieval index backing the `query` / `insight`
* tiers. Omitted by pure `raw` merchants or by providers that do not wish
* to expose retrieval internals.
*/
index?: IndexManifest;
}
// ---------- §4: Index manifest (v0.2) ----------
/**
* §4.1 extension point. v0.2 defines "dense" | "sparse" | "hybrid"; future
* revisions may add more. Unknown values are treated as opaque retrieval
* per SPEC §2.3.
*/
export type IndexType = "dense" | "sparse" | "hybrid" | string;
export type ChunkKind = "token-window" | "paragraph" | "post" | "none" | string;
export interface ChunkStrategy {
kind: ChunkKind;
/** Required when `kind === "token-window"`. Ignored otherwise. */
size?: number;
/** Required when `kind === "token-window"`. Ignored otherwise. */
overlap?: number;
}
export interface IndexManifest {
type: IndexType;
/**
* Embedding model identifier. MUST match `Citation.retrieval.model`
* in §3.2 envelopes. Sparse-only merchants SHOULD emit `"none"`.
*/
model: string;
/** Embedding dimensionality. Required when type is "dense" or "hybrid". */
dim?: number;
/** Similarity metric. Required when type is "dense" or "hybrid". */
distance?: "cosine" | "dot" | "l2";
/** Total indexable units at `built_at`. */
chunks: number;
chunk_strategy: ChunkStrategy;
/**
* Hex SHA-256 fingerprint of the corpus at index time. Lets two
* merchants prove they indexed the same corpus.
*/
corpus_sha256: string;
/** ISO-8601 timestamp of the build that produced this index. */
built_at: string;
}
// ---------- §3: Response envelope ----------
export type CitationType = "source" | "vds" | string;
/**
* §3.2 (v0.2) — optional retrieval provenance attached to source citations.
* Emitted only when the merchant ran an index lookup to produce the result.
*/
export interface RetrievalProvenance {
/** Same string emitted by `IndexManifest.model`. */
model: string;
/** Raw similarity score. Higher = more relevant. */
score: number;
/** Zero-based position in the result list for this request. */
rank: number;
}
export interface CitationSource {
type: "source";
source_id: string;
provider: string;
retrieved_at: string; // ISO-8601
license?: string;
canonical_url?: string;
/**
* §3.2 (v0.2, optional). Stable chunk identifier in the form
* `<source_id>#c<n>`. Must round-trip stably for the same corpus version.
*/
chunk_id?: string;
/**
* §3.2 (v0.2, optional). Retrieval provenance. Providers doing retrieval
* SHOULD emit this; pure `raw` merchants omit it.
*/
retrieval?: RetrievalProvenance;
}
export interface CitationVDS {
type: "vds";
script_id: string;
session_id: string;
captured_by: `0x${string}`;
captured_at: string; // ISO-8601
verifier: string;
verification: {
status: "PASS" | "FAIL" | "INCONCLUSIVE";
confidence: number;
findings: Array<{
kind: string;
value: string | number;
confidence: number;
}>;
};
onchain?: string;
signature: `0x${string}`;
}
export type Citation = CitationSource | CitationVDS;
export interface Receipt {
tier: TierName;
price_usd: number;
/** Transaction hash, or "stub" in demo mode. */
tx: string;
paid_at: string; // ISO-8601
}
export interface Envelope<D = unknown> {
data: D;
citation: Citation;
receipt: Receipt;
}
// ---------- §5: Errors ----------
export type ErrorCode =
| "invalid_tier"
| "invalid_input"
| "upstream_unavailable"
| "rate_limited"
| "citation_unavailable";
export interface ErrorBody {
error: { code: ErrorCode | string; message: string };
trace_id: string;
}