opencode-gemini-app 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DISCLAIMER.md +17 -0
- package/LICENSE +21 -0
- package/README.md +44 -2
- package/extensions/opencode/auth.js +324 -0
- package/extensions/opencode/fetch.js +494 -0
- package/extensions/opencode/index.js +27 -0
- package/extensions/opencode/models.js +84 -0
- package/extensions/pi/auth.js +215 -0
- package/native-wire/consumer-tool-codec.mjs +239 -0
- package/native-wire/oauth-loopback.mjs +175 -0
- package/native-wire/protobuf.mjs +133 -0
- package/native-wire/robin-current.mjs +172 -0
- package/package.json +75 -4
- package/src/gateway/automatic-recovery.mjs +64 -0
- package/src/gateway/image-download.mjs +110 -0
- package/src/gateway/image-upload.mjs +219 -0
- package/src/gateway/native-diagnostic.mjs +189 -0
- package/src/gateway/native-evidence.mjs +113 -0
- package/src/gateway/native-response-limits.mjs +8 -0
- package/src/gateway/native-search-citations.mjs +196 -0
- package/src/gateway/native-search-diagnostic.mjs +282 -0
- package/src/gateway/native-search-metadata.mjs +749 -0
- package/src/gateway/native-transport.mjs +375 -0
- package/src/gateway/oauth-client-defaults.mjs +12 -0
- package/src/gateway/oauth-identity.mjs +67 -0
- package/src/gateway/outbound-proxy.mjs +320 -0
- package/src/gateway/output-recovery.mjs +48 -0
- package/src/gateway/quota-failover.mjs +14 -0
- package/src/gateway/quota-transport.mjs +37 -0
- package/src/gateway/quota.mjs +181 -0
- package/src/gateway/soft-error.mjs +25 -0
- package/src/gateway/structured-output.mjs +157 -0
- package/src/gateway/subscription.mjs +51 -0
- package/src/gateway/terms.mjs +126 -0
- package/src/image-input.mjs +137 -0
- package/src/image-url.mjs +21 -0
- package/src/native-provider.mjs +681 -0
- package/src/openai-contract.mjs +576 -0
- package/src/openai-usage.mjs +66 -0
- package/src/prompt-tool-schema.mjs +391 -0
- package/src/responses-tool-namespaces.mjs +84 -0
- package/src/tool-protocol.mjs +348 -0
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
// Bounded native search/citation metadata decoder for inner ROBINModelResponse bytes.
|
|
2
|
+
// Safe pure decoding only: does NOT enable, advertise, or execute search.
|
|
3
|
+
// No network access, no HTML execution, no SSRF fetching.
|
|
4
|
+
|
|
5
|
+
import { parse } from "../../native-wire/protobuf.mjs";
|
|
6
|
+
|
|
7
|
+
/** Budget and length limits for metadata parsing. */
|
|
8
|
+
export const METADATA_LIMITS = Object.freeze({
|
|
9
|
+
MAX_INPUT_BYTES: 1024 * 1024, // 1 MiB default maximum input
|
|
10
|
+
MAX_CITATIONS: 64, // <= 64 citations
|
|
11
|
+
MAX_SOURCES: 64, // <= 64 renderable sources
|
|
12
|
+
MAX_URL_LENGTH: 4096, // <= 4096 bytes for URLs
|
|
13
|
+
MAX_TITLE_LENGTH: 1024, // bounded title string length
|
|
14
|
+
MAX_SNIPPET_LENGTH: 4096, // bounded snippet string length
|
|
15
|
+
MAX_ID_LENGTH: 512, // bounded id string length
|
|
16
|
+
MAX_CATEGORY_LENGTH: 256, // bounded category name length
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
/** Standard fixed safe error codes (no raw payloads leaked). */
|
|
20
|
+
export const METADATA_ERROR_CODES = Object.freeze({
|
|
21
|
+
MALFORMED_SEARCH_METADATA: "MALFORMED_SEARCH_METADATA",
|
|
22
|
+
INPUT_TOO_LARGE: "METADATA_INPUT_TOO_LARGE",
|
|
23
|
+
INVALID_WIRE_TYPE: "METADATA_INVALID_WIRE_TYPE",
|
|
24
|
+
DUPLICATE_FIELD: "METADATA_DUPLICATE_FIELD",
|
|
25
|
+
INVALID_UTF8: "METADATA_INVALID_UTF8",
|
|
26
|
+
INVALID_RANGE: "METADATA_INVALID_RANGE",
|
|
27
|
+
COUNT_EXCEEDED: "METADATA_COUNT_EXCEEDED",
|
|
28
|
+
STRING_TOO_LONG: "METADATA_STRING_TOO_LONG",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/** Safe error class with fixed safe error code and generic message. */
|
|
32
|
+
export class NativeSearchMetadataError extends Error {
|
|
33
|
+
constructor(
|
|
34
|
+
code = METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
35
|
+
message = "Malformed native search metadata",
|
|
36
|
+
) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = "NativeSearchMetadataError";
|
|
39
|
+
this.code = code;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function fail(code, message) {
|
|
44
|
+
throw new NativeSearchMetadataError(code, message);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const utf8Decoder = new TextDecoder("utf-8", { fatal: true });
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Decode a varint from raw varint bytes.
|
|
51
|
+
* Validates length (<= 10 bytes), uint64 overflow, and trailing bytes.
|
|
52
|
+
*/
|
|
53
|
+
function decodeVarint(bytes) {
|
|
54
|
+
if (!bytes || !bytes.length) {
|
|
55
|
+
fail(
|
|
56
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
57
|
+
"Empty varint payload",
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
let value = 0n;
|
|
61
|
+
let shift = 0n;
|
|
62
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
63
|
+
if (i > 9) {
|
|
64
|
+
fail(
|
|
65
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
66
|
+
"Varint exceeds 10 bytes",
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const b = bytes[i];
|
|
70
|
+
if (i === 9 && b > 1) {
|
|
71
|
+
fail(
|
|
72
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
73
|
+
"Varint overflows uint64",
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
value |= BigInt(b & 0x7f) << shift;
|
|
77
|
+
if ((b & 0x80) === 0) {
|
|
78
|
+
if (i !== bytes.length - 1) {
|
|
79
|
+
fail(
|
|
80
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
81
|
+
"Trailing bytes in varint payload",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
shift += 7n;
|
|
87
|
+
}
|
|
88
|
+
fail(
|
|
89
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
90
|
+
"Unterminated varint payload",
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Decode an int32 offset.
|
|
96
|
+
* Rejects negative values (including 64-bit sign-extended two's complement)
|
|
97
|
+
* and values exceeding int32 maximum (2147483647).
|
|
98
|
+
*/
|
|
99
|
+
function decodeInt32Offset(bytes) {
|
|
100
|
+
const raw = decodeVarint(bytes);
|
|
101
|
+
const signed64 = BigInt.asIntN(64, raw);
|
|
102
|
+
if (signed64 < 0n || raw > 2147483647n) {
|
|
103
|
+
fail(
|
|
104
|
+
METADATA_ERROR_CODES.INVALID_RANGE,
|
|
105
|
+
"Offset is negative or exceeds int32 range",
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return Number(raw);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Decode an int32 enum value.
|
|
113
|
+
*/
|
|
114
|
+
function decodeEnum(bytes) {
|
|
115
|
+
const raw = decodeVarint(bytes);
|
|
116
|
+
const signed64 = BigInt.asIntN(64, raw);
|
|
117
|
+
if (signed64 < 0n || raw > 2147483647n) {
|
|
118
|
+
fail(
|
|
119
|
+
METADATA_ERROR_CODES.INVALID_RANGE,
|
|
120
|
+
"Enum value is negative or exceeds int32 range",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return Number(raw);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Decode a boolean varint. Must be 0 or 1.
|
|
128
|
+
*/
|
|
129
|
+
function decodeBool(bytes) {
|
|
130
|
+
const raw = decodeVarint(bytes);
|
|
131
|
+
if (raw === 0n) return false;
|
|
132
|
+
if (raw === 1n) return true;
|
|
133
|
+
fail(
|
|
134
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
135
|
+
"Invalid boolean varint value",
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Decode a UTF-8 string with fatal validation and byte length check.
|
|
141
|
+
* Never includes raw payload bytes in error messages.
|
|
142
|
+
*/
|
|
143
|
+
function decodeString(bytes, maxLength, fieldName) {
|
|
144
|
+
if (bytes.length > maxLength) {
|
|
145
|
+
fail(
|
|
146
|
+
METADATA_ERROR_CODES.STRING_TOO_LONG,
|
|
147
|
+
`${fieldName} exceeds maximum byte length`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
return utf8Decoder.decode(bytes);
|
|
152
|
+
} catch {
|
|
153
|
+
fail(
|
|
154
|
+
METADATA_ERROR_CODES.INVALID_UTF8,
|
|
155
|
+
`Invalid UTF-8 sequence in ${fieldName}`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Canonicalize HTTP(S) URL identity for internal exclusion, title lookup, and dedup. */
|
|
161
|
+
export function canonicalWebUrlKey(urlString, { stripHash = false } = {}) {
|
|
162
|
+
if (typeof urlString !== "string") return "";
|
|
163
|
+
const trimmed = urlString.trim();
|
|
164
|
+
if (!trimmed) return "";
|
|
165
|
+
try {
|
|
166
|
+
const parsed = new URL(trimmed);
|
|
167
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
168
|
+
return trimmed;
|
|
169
|
+
}
|
|
170
|
+
parsed.username = "";
|
|
171
|
+
parsed.password = "";
|
|
172
|
+
const trimmedHost = parsed.hostname.replace(/\.+$/, "");
|
|
173
|
+
if (trimmedHost) parsed.hostname = trimmedHost;
|
|
174
|
+
if (stripHash) parsed.hash = "";
|
|
175
|
+
return parsed.href;
|
|
176
|
+
} catch {
|
|
177
|
+
return trimmed;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const NON_PUBLIC_HOST_SUFFIX = /(?:^|\.)(?:localhost|local|localdomain|internal|home|lan|arpa|invalid|test|onion)$/i;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Syntactic safe HTTP(S) URL validation.
|
|
185
|
+
*
|
|
186
|
+
* Enforces:
|
|
187
|
+
* - Scheme must be http: or https:
|
|
188
|
+
* - No credentials (username / password)
|
|
189
|
+
* - Rejects IP literals (IPv4 and IPv6) syntactically and non-alphabetic TLDs
|
|
190
|
+
* - Rejects local/reserved hostnames (localhost, .localhost, .local, .localdomain, .internal, .home, .lan, .arpa, .invalid, .test, .onion)
|
|
191
|
+
* - Rejects single-label hostnames without a dot
|
|
192
|
+
* - Bounded length (<= 4096 bytes)
|
|
193
|
+
*
|
|
194
|
+
* NOTE: This is purely syntactic formatting validation for metadata hygiene.
|
|
195
|
+
* It performs NO DNS requests and must NOT be claimed to be an SSRF-safe fetcher.
|
|
196
|
+
*/
|
|
197
|
+
export function isSafeWebUrl(urlString) {
|
|
198
|
+
if (typeof urlString !== "string" || !urlString) return false;
|
|
199
|
+
if (
|
|
200
|
+
/[\\\\\u0000-\u001f\u007f]/.test(urlString) ||
|
|
201
|
+
urlString.trim() !== urlString
|
|
202
|
+
)
|
|
203
|
+
return false;
|
|
204
|
+
if (Buffer.byteLength(urlString) > METADATA_LIMITS.MAX_URL_LENGTH)
|
|
205
|
+
return false;
|
|
206
|
+
let parsed;
|
|
207
|
+
try {
|
|
208
|
+
parsed = new URL(urlString);
|
|
209
|
+
} catch {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return false;
|
|
213
|
+
if (parsed.username || parsed.password) return false;
|
|
214
|
+
const hostname = parsed.hostname.toLowerCase();
|
|
215
|
+
if (!hostname || hostname.startsWith("[") || !hostname.includes("."))
|
|
216
|
+
return false;
|
|
217
|
+
if (/^(?:\d{1,3}\.){3}\d{1,3}$/.test(hostname) || /^\d+$/.test(hostname))
|
|
218
|
+
return false;
|
|
219
|
+
if (NON_PUBLIC_HOST_SUFFIX.test(hostname)) return false;
|
|
220
|
+
const labels = hostname.split(".");
|
|
221
|
+
if (!/[a-z]/i.test(labels[labels.length - 1] || "")) return false;
|
|
222
|
+
return labels.every(
|
|
223
|
+
(label) =>
|
|
224
|
+
label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(label),
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Schema definition for known ROBINCitation fields. */
|
|
229
|
+
const CITATION_FIELD_SCHEMA = Object.freeze({
|
|
230
|
+
1: { name: "startIndex", expectedWire: 0, maxCount: 1 },
|
|
231
|
+
2: { name: "endIndex", expectedWire: 0, maxCount: 1 },
|
|
232
|
+
3: { name: "link", expectedWire: 2, maxCount: 1 },
|
|
233
|
+
4: { name: "citationType", expectedWire: 0, maxCount: 1 },
|
|
234
|
+
5: { name: "htmlTitle", expectedWire: 2, maxCount: 1 },
|
|
235
|
+
6: { name: "citationId", expectedWire: 2, maxCount: 1 },
|
|
236
|
+
7: { name: "faviconURL", expectedWire: 2, maxCount: 1 },
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
/** Schema definition for known ROBINRenderableSource fields. */
|
|
240
|
+
const SOURCE_FIELD_SCHEMA = Object.freeze({
|
|
241
|
+
1: { name: "sourceId", expectedWire: 2, maxCount: 1 },
|
|
242
|
+
2: { name: "iconURL", expectedWire: 2, maxCount: 1 },
|
|
243
|
+
3: { name: "attributionTitle", expectedWire: 2, maxCount: 1 },
|
|
244
|
+
4: { name: "attributionSubtitle", expectedWire: 2, maxCount: 1 },
|
|
245
|
+
5: { name: "sourceTitle", expectedWire: 2, maxCount: 1 },
|
|
246
|
+
6: { name: "snippet", expectedWire: 2, maxCount: 1 },
|
|
247
|
+
7: { name: "categoryName", expectedWire: 2, maxCount: 1 },
|
|
248
|
+
8: { name: "webAction", expectedWire: 2, maxCount: 1 },
|
|
249
|
+
9: { name: "openConversation", expectedWire: 2, maxCount: 1 },
|
|
250
|
+
10: { name: "previewUploadedFile", expectedWire: 2, maxCount: 1 },
|
|
251
|
+
11: { name: "openGmailAction", expectedWire: 2, maxCount: 1 },
|
|
252
|
+
12: { name: "openByAppFunctionsAction", expectedWire: 2, maxCount: 1 },
|
|
253
|
+
18: {
|
|
254
|
+
name: "navigateToGeminiManageExtensions",
|
|
255
|
+
expectedWire: 2,
|
|
256
|
+
maxCount: 1,
|
|
257
|
+
},
|
|
258
|
+
25: {
|
|
259
|
+
name: "openPersonalCurationDocumentAction",
|
|
260
|
+
expectedWire: 2,
|
|
261
|
+
maxCount: 1,
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
/** Schema definition for known ROBINLinkAction_WebAction fields. */
|
|
266
|
+
const WEB_ACTION_FIELD_SCHEMA = Object.freeze({
|
|
267
|
+
1: { name: "URL", expectedWire: 2, maxCount: 1 },
|
|
268
|
+
2: { name: "forceAccount", expectedWire: 0, maxCount: 1 },
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
/** Non-web action field numbers on ROBINRenderableSource. */
|
|
272
|
+
const NON_WEB_ACTION_FIELD_NUMBERS = new Set([9, 10, 11, 12, 18, 25]);
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Validate field wire types and check for illegal duplicates against a known schema.
|
|
276
|
+
* Unknown field numbers are tolerated without error and without recursion.
|
|
277
|
+
*/
|
|
278
|
+
function validateFields(fields, schema, containerName) {
|
|
279
|
+
const counts = new Map();
|
|
280
|
+
for (const f of fields) {
|
|
281
|
+
const rule = schema[f.number];
|
|
282
|
+
if (rule) {
|
|
283
|
+
if (f.wire !== rule.expectedWire) {
|
|
284
|
+
fail(
|
|
285
|
+
METADATA_ERROR_CODES.INVALID_WIRE_TYPE,
|
|
286
|
+
`Field ${rule.name} (number ${f.number}) in ${containerName} has invalid wire type ${f.wire}, expected ${rule.expectedWire}`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
const count = (counts.get(f.number) || 0) + 1;
|
|
290
|
+
if (count > rule.maxCount) {
|
|
291
|
+
fail(
|
|
292
|
+
METADATA_ERROR_CODES.DUPLICATE_FIELD,
|
|
293
|
+
`Duplicate field ${rule.name} (number ${f.number}) in ${containerName}`,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
counts.set(f.number, count);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Decode a single ROBINCitation message.
|
|
303
|
+
*/
|
|
304
|
+
function decodeCitation(payload) {
|
|
305
|
+
let fields;
|
|
306
|
+
try {
|
|
307
|
+
fields = parse(payload);
|
|
308
|
+
} catch {
|
|
309
|
+
fail(
|
|
310
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
311
|
+
"Failed to parse citation protobuf message",
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
validateFields(fields, CITATION_FIELD_SCHEMA, "ROBINCitation");
|
|
316
|
+
|
|
317
|
+
let startIndex = null;
|
|
318
|
+
let endIndex = null;
|
|
319
|
+
let link = null;
|
|
320
|
+
let citationType = null;
|
|
321
|
+
let untrustedHtmlTitle = null;
|
|
322
|
+
let citationId = null;
|
|
323
|
+
let faviconURL = null;
|
|
324
|
+
|
|
325
|
+
for (const f of fields) {
|
|
326
|
+
switch (f.number) {
|
|
327
|
+
case 1:
|
|
328
|
+
startIndex = decodeInt32Offset(f.payload);
|
|
329
|
+
break;
|
|
330
|
+
case 2:
|
|
331
|
+
endIndex = decodeInt32Offset(f.payload);
|
|
332
|
+
break;
|
|
333
|
+
case 3: {
|
|
334
|
+
const decoded = decodeString(
|
|
335
|
+
f.payload,
|
|
336
|
+
METADATA_LIMITS.MAX_URL_LENGTH,
|
|
337
|
+
"citation link",
|
|
338
|
+
);
|
|
339
|
+
link = isSafeWebUrl(decoded) ? decoded : null;
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
case 4:
|
|
343
|
+
citationType = decodeEnum(f.payload);
|
|
344
|
+
break;
|
|
345
|
+
case 5:
|
|
346
|
+
// Plain untrusted string data; never treated as safe HTML markup
|
|
347
|
+
untrustedHtmlTitle = decodeString(
|
|
348
|
+
f.payload,
|
|
349
|
+
METADATA_LIMITS.MAX_TITLE_LENGTH,
|
|
350
|
+
"citation htmlTitle",
|
|
351
|
+
);
|
|
352
|
+
break;
|
|
353
|
+
case 6:
|
|
354
|
+
citationId = decodeString(
|
|
355
|
+
f.payload,
|
|
356
|
+
METADATA_LIMITS.MAX_ID_LENGTH,
|
|
357
|
+
"citation citationId",
|
|
358
|
+
);
|
|
359
|
+
break;
|
|
360
|
+
case 7: {
|
|
361
|
+
const decoded = decodeString(
|
|
362
|
+
f.payload,
|
|
363
|
+
METADATA_LIMITS.MAX_URL_LENGTH,
|
|
364
|
+
"citation faviconURL",
|
|
365
|
+
);
|
|
366
|
+
faviconURL = isSafeWebUrl(decoded) ? decoded : null;
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
default:
|
|
370
|
+
// Unknown unrelated field: tolerated without recursion
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Reject end < start when both offsets are given
|
|
376
|
+
if (startIndex !== null && endIndex !== null && endIndex < startIndex) {
|
|
377
|
+
fail(
|
|
378
|
+
METADATA_ERROR_CODES.INVALID_RANGE,
|
|
379
|
+
"endIndex cannot be less than startIndex",
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return {
|
|
384
|
+
startIndex,
|
|
385
|
+
endIndex,
|
|
386
|
+
indexUnits: "unknown",
|
|
387
|
+
indexProvenance: "raw_native_unknown_units",
|
|
388
|
+
link,
|
|
389
|
+
citationType,
|
|
390
|
+
untrustedHtmlTitle,
|
|
391
|
+
citationId,
|
|
392
|
+
faviconURL,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Decode ROBINLinkAction_WebAction.
|
|
398
|
+
*/
|
|
399
|
+
function decodeWebAction(payload) {
|
|
400
|
+
let fields;
|
|
401
|
+
try {
|
|
402
|
+
fields = parse(payload);
|
|
403
|
+
} catch {
|
|
404
|
+
fail(
|
|
405
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
406
|
+
"Failed to parse webAction protobuf message",
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
validateFields(fields, WEB_ACTION_FIELD_SCHEMA, "ROBINLinkAction_WebAction");
|
|
411
|
+
|
|
412
|
+
let rawUrl = null;
|
|
413
|
+
let forceAccount = null;
|
|
414
|
+
|
|
415
|
+
for (const f of fields) {
|
|
416
|
+
switch (f.number) {
|
|
417
|
+
case 1:
|
|
418
|
+
rawUrl = decodeString(
|
|
419
|
+
f.payload,
|
|
420
|
+
METADATA_LIMITS.MAX_URL_LENGTH,
|
|
421
|
+
"webAction URL",
|
|
422
|
+
);
|
|
423
|
+
break;
|
|
424
|
+
case 2:
|
|
425
|
+
forceAccount = decodeBool(f.payload);
|
|
426
|
+
break;
|
|
427
|
+
default:
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (!rawUrl) {
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (!isSafeWebUrl(rawUrl)) {
|
|
437
|
+
return forceAccount === true
|
|
438
|
+
? { excludedUrl: rawUrl, forceAccount: true }
|
|
439
|
+
: null;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return {
|
|
443
|
+
url: rawUrl,
|
|
444
|
+
forceAccount,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Decode a single ROBINRenderableSource message.
|
|
450
|
+
* Returns decoded source if it is a valid, safe public web source.
|
|
451
|
+
* Excludes non-web sources and mixed/action-ambiguous sources.
|
|
452
|
+
*/
|
|
453
|
+
function decodeRenderableSource(payload) {
|
|
454
|
+
let fields;
|
|
455
|
+
try {
|
|
456
|
+
fields = parse(payload);
|
|
457
|
+
} catch {
|
|
458
|
+
fail(
|
|
459
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
460
|
+
"Failed to parse renderable source protobuf message",
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
validateFields(fields, SOURCE_FIELD_SCHEMA, "ROBINRenderableSource");
|
|
465
|
+
|
|
466
|
+
let webActionField = null;
|
|
467
|
+
let hasNonWebAction = false;
|
|
468
|
+
let sourceId = null;
|
|
469
|
+
let iconURL = null;
|
|
470
|
+
let attributionTitle = null;
|
|
471
|
+
let attributionSubtitle = null;
|
|
472
|
+
let sourceTitle = null;
|
|
473
|
+
let snippet = null;
|
|
474
|
+
let categoryName = null;
|
|
475
|
+
|
|
476
|
+
for (const f of fields) {
|
|
477
|
+
if (f.number === 8) {
|
|
478
|
+
webActionField = f;
|
|
479
|
+
} else if (NON_WEB_ACTION_FIELD_NUMBERS.has(f.number)) {
|
|
480
|
+
hasNonWebAction = true;
|
|
481
|
+
} else {
|
|
482
|
+
switch (f.number) {
|
|
483
|
+
case 1:
|
|
484
|
+
sourceId = decodeString(
|
|
485
|
+
f.payload,
|
|
486
|
+
METADATA_LIMITS.MAX_ID_LENGTH,
|
|
487
|
+
"sourceId",
|
|
488
|
+
);
|
|
489
|
+
break;
|
|
490
|
+
case 2: {
|
|
491
|
+
const decoded = decodeString(
|
|
492
|
+
f.payload,
|
|
493
|
+
METADATA_LIMITS.MAX_URL_LENGTH,
|
|
494
|
+
"iconURL",
|
|
495
|
+
);
|
|
496
|
+
iconURL = isSafeWebUrl(decoded) ? decoded : null;
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
case 3:
|
|
500
|
+
attributionTitle = decodeString(
|
|
501
|
+
f.payload,
|
|
502
|
+
METADATA_LIMITS.MAX_TITLE_LENGTH,
|
|
503
|
+
"attributionTitle",
|
|
504
|
+
);
|
|
505
|
+
break;
|
|
506
|
+
case 4:
|
|
507
|
+
attributionSubtitle = decodeString(
|
|
508
|
+
f.payload,
|
|
509
|
+
METADATA_LIMITS.MAX_TITLE_LENGTH,
|
|
510
|
+
"attributionSubtitle",
|
|
511
|
+
);
|
|
512
|
+
break;
|
|
513
|
+
case 5:
|
|
514
|
+
sourceTitle = decodeString(
|
|
515
|
+
f.payload,
|
|
516
|
+
METADATA_LIMITS.MAX_TITLE_LENGTH,
|
|
517
|
+
"sourceTitle",
|
|
518
|
+
);
|
|
519
|
+
break;
|
|
520
|
+
case 6:
|
|
521
|
+
snippet = decodeString(
|
|
522
|
+
f.payload,
|
|
523
|
+
METADATA_LIMITS.MAX_SNIPPET_LENGTH,
|
|
524
|
+
"snippet",
|
|
525
|
+
);
|
|
526
|
+
break;
|
|
527
|
+
case 7:
|
|
528
|
+
categoryName = decodeString(
|
|
529
|
+
f.payload,
|
|
530
|
+
METADATA_LIMITS.MAX_CATEGORY_LENGTH,
|
|
531
|
+
"categoryName",
|
|
532
|
+
);
|
|
533
|
+
break;
|
|
534
|
+
default:
|
|
535
|
+
// Unknown / uninspected field tolerated without recursion
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// Non-web only or mixed actions (action-ambiguous): never turn into web results,
|
|
542
|
+
// but still preserve any bounded forceAccount exclusion identity.
|
|
543
|
+
if (!webActionField) {
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
if (hasNonWebAction) {
|
|
547
|
+
try {
|
|
548
|
+
const mixedAction = decodeWebAction(webActionField.payload);
|
|
549
|
+
if (mixedAction?.forceAccount === true) {
|
|
550
|
+
const excludedUrl = mixedAction.excludedUrl ?? mixedAction.url;
|
|
551
|
+
if (excludedUrl) return { excludedUrl, forceAccount: true };
|
|
552
|
+
}
|
|
553
|
+
} catch {
|
|
554
|
+
// Mixed/non-web actions continue to be ignored on malformed web subfields.
|
|
555
|
+
}
|
|
556
|
+
return null;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
const webAction = decodeWebAction(webActionField.payload);
|
|
560
|
+
if (!webAction) {
|
|
561
|
+
// Missing, invalid, or unsafe non-excluded URL: omitted from web sources
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
if (webAction.excludedUrl) {
|
|
565
|
+
return { excludedUrl: webAction.excludedUrl, forceAccount: true };
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return {
|
|
569
|
+
sourceId,
|
|
570
|
+
url: webAction.url,
|
|
571
|
+
sourceTitle,
|
|
572
|
+
snippet,
|
|
573
|
+
attributionTitle,
|
|
574
|
+
attributionSubtitle,
|
|
575
|
+
categoryName,
|
|
576
|
+
iconURL,
|
|
577
|
+
forceAccount: webAction.forceAccount,
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Decode citations and web sources metadata from inner ROBINModelResponse bytes.
|
|
583
|
+
*
|
|
584
|
+
* Requirements & semantics:
|
|
585
|
+
* - Pure bounded parsing; does NOT perform network requests or HTML execution.
|
|
586
|
+
* - Unknown unrelated fields tolerated without recursion.
|
|
587
|
+
* - Preserves bounded raw citation index values with explicit unknown-unit provenance.
|
|
588
|
+
* - Excludes non-web actions and action-ambiguous sources from webSources.
|
|
589
|
+
* - Distinguishes omitted fields from explicit 0.
|
|
590
|
+
* - Rejects negative/out-of-range offsets or end < start when both are given.
|
|
591
|
+
* - Explicitly avoids `searchExecuted=true`; metadata presence is descriptor-only.
|
|
592
|
+
*
|
|
593
|
+
* @param {Buffer|Uint8Array} modelResponseBytes - Raw serialized protobuf bytes.
|
|
594
|
+
* @param {object} [options] - Optional custom bounds (defaults to METADATA_LIMITS).
|
|
595
|
+
* @returns {object} Decoded metadata containing citations, webSources, counts, and descriptor evidence.
|
|
596
|
+
*/
|
|
597
|
+
function boundedOptions(options) {
|
|
598
|
+
if (options === undefined) options = {};
|
|
599
|
+
if (!options || typeof options !== "object" || Array.isArray(options)) {
|
|
600
|
+
fail(
|
|
601
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
602
|
+
"Invalid metadata limits",
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
const allowed = {
|
|
606
|
+
maxInputBytes: METADATA_LIMITS.MAX_INPUT_BYTES,
|
|
607
|
+
maxCitations: METADATA_LIMITS.MAX_CITATIONS,
|
|
608
|
+
maxSources: METADATA_LIMITS.MAX_SOURCES,
|
|
609
|
+
};
|
|
610
|
+
for (const key of Object.keys(options)) {
|
|
611
|
+
if (!Object.hasOwn(allowed, key))
|
|
612
|
+
fail(
|
|
613
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
614
|
+
"Invalid metadata limits",
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
const result = {};
|
|
618
|
+
for (const [key, hardLimit] of Object.entries(allowed)) {
|
|
619
|
+
const value = Object.hasOwn(options, key) ? options[key] : hardLimit;
|
|
620
|
+
if (!Number.isSafeInteger(value) || value < 0 || value > hardLimit) {
|
|
621
|
+
fail(
|
|
622
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
623
|
+
"Invalid metadata limits",
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
result[key] = value;
|
|
627
|
+
}
|
|
628
|
+
return result;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
export function decodeNativeSearchMetadata(modelResponseBytes, options = {}) {
|
|
632
|
+
options = boundedOptions(options);
|
|
633
|
+
let bytes = modelResponseBytes;
|
|
634
|
+
if (!Buffer.isBuffer(bytes)) {
|
|
635
|
+
if (bytes instanceof Uint8Array) {
|
|
636
|
+
bytes = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
637
|
+
} else {
|
|
638
|
+
fail(
|
|
639
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
640
|
+
"Expected Buffer or Uint8Array for modelResponseBytes",
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
const maxInputBytes = options.maxInputBytes;
|
|
646
|
+
if (bytes.length > maxInputBytes) {
|
|
647
|
+
fail(
|
|
648
|
+
METADATA_ERROR_CODES.INPUT_TOO_LARGE,
|
|
649
|
+
"Input exceeds maximum byte budget",
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
let fields;
|
|
654
|
+
try {
|
|
655
|
+
fields = parse(bytes);
|
|
656
|
+
} catch {
|
|
657
|
+
fail(
|
|
658
|
+
METADATA_ERROR_CODES.MALFORMED_SEARCH_METADATA,
|
|
659
|
+
"Failed to parse model response protobuf wire format",
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const maxCitations = options.maxCitations;
|
|
664
|
+
const maxSources = options.maxSources;
|
|
665
|
+
|
|
666
|
+
let rawCitationCount = 0;
|
|
667
|
+
let rawSourceCount = 0;
|
|
668
|
+
|
|
669
|
+
// Pre-count repeated fields to enforce limits before expensive decoding
|
|
670
|
+
for (const f of fields) {
|
|
671
|
+
if (f.number === 6) {
|
|
672
|
+
if (f.wire !== 2) {
|
|
673
|
+
fail(
|
|
674
|
+
METADATA_ERROR_CODES.INVALID_WIRE_TYPE,
|
|
675
|
+
`ModelResponse citation field 6 has invalid wire type ${f.wire}, expected 2`,
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
if (++rawCitationCount > maxCitations) {
|
|
679
|
+
fail(
|
|
680
|
+
METADATA_ERROR_CODES.COUNT_EXCEEDED,
|
|
681
|
+
"Citation count exceeds maximum allowed limit",
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
} else if (f.number === 14) {
|
|
685
|
+
if (f.wire !== 2) {
|
|
686
|
+
fail(
|
|
687
|
+
METADATA_ERROR_CODES.INVALID_WIRE_TYPE,
|
|
688
|
+
`ModelResponse renderable source field 14 has invalid wire type ${f.wire}, expected 2`,
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
if (++rawSourceCount > maxSources) {
|
|
692
|
+
fail(
|
|
693
|
+
METADATA_ERROR_CODES.COUNT_EXCEEDED,
|
|
694
|
+
"Renderable source count exceeds maximum allowed limit",
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
// Fields 3 (attributionMetadata), 12 (thoughts), 16 (structuredThoughtHeader),
|
|
699
|
+
// and other unrelated fields are deliberately ignored without inspection or recursion.
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
const citations = [];
|
|
703
|
+
const webSources = [];
|
|
704
|
+
const excludedUrls = [];
|
|
705
|
+
const excludedSeen = new Set();
|
|
706
|
+
|
|
707
|
+
for (const f of fields) {
|
|
708
|
+
if (f.number === 6) {
|
|
709
|
+
citations.push(decodeCitation(f.payload));
|
|
710
|
+
} else if (f.number === 14) {
|
|
711
|
+
const source = decodeRenderableSource(f.payload);
|
|
712
|
+
|
|
713
|
+
if (source !== null) {
|
|
714
|
+
if (source.excludedUrl) {
|
|
715
|
+
const canonicalExcluded = canonicalWebUrlKey(source.excludedUrl);
|
|
716
|
+
if (canonicalExcluded && !excludedSeen.has(canonicalExcluded)) {
|
|
717
|
+
excludedSeen.add(canonicalExcluded);
|
|
718
|
+
excludedUrls.push(canonicalExcluded);
|
|
719
|
+
}
|
|
720
|
+
} else {
|
|
721
|
+
webSources.push(source);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const hasMetadata = citations.length > 0 || webSources.length > 0;
|
|
728
|
+
|
|
729
|
+
return {
|
|
730
|
+
citations,
|
|
731
|
+
webSources,
|
|
732
|
+
...(excludedUrls.length ? { excludedUrls } : {}),
|
|
733
|
+
counts: {
|
|
734
|
+
citations: citations.length,
|
|
735
|
+
webSources: webSources.length,
|
|
736
|
+
totalRawCitations: rawCitationCount,
|
|
737
|
+
totalRawSources: rawSourceCount,
|
|
738
|
+
},
|
|
739
|
+
evidenceLevel: "METADATA_DESCRIPTOR_ONLY",
|
|
740
|
+
searchEvidence: {
|
|
741
|
+
evidenceLevel: "METADATA_DESCRIPTOR_ONLY",
|
|
742
|
+
metadataPresent: hasMetadata,
|
|
743
|
+
searchExecuted: null,
|
|
744
|
+
executionProof: false,
|
|
745
|
+
provenance: "passive_metadata_descriptor_only",
|
|
746
|
+
note: "Presence of citations or renderable sources is passive metadata evidence only and does NOT constitute proof that an external search was executed.",
|
|
747
|
+
},
|
|
748
|
+
};
|
|
749
|
+
}
|