@kognitivedev/cloud-web-search 0.2.28

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.
@@ -0,0 +1,437 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPlainObject = isPlainObject;
4
+ exports.cloneUnknown = cloneUnknown;
5
+ exports.isJobStatus = isJobStatus;
6
+ exports.decodeCloudWebSearchSource = decodeCloudWebSearchSource;
7
+ exports.decodeCloudWebSearchResult = decodeCloudWebSearchResult;
8
+ exports.decodeCloudWebSearchProgressEntry = decodeCloudWebSearchProgressEntry;
9
+ exports.decodeCloudWebSearchJobRecord = decodeCloudWebSearchJobRecord;
10
+ exports.decodeCloudWebSearchJobEventRecord = decodeCloudWebSearchJobEventRecord;
11
+ exports.decodeCloudWebSearchJobListEnvelope = decodeCloudWebSearchJobListEnvelope;
12
+ exports.decodeCloudWebSearchJobResultEnvelope = decodeCloudWebSearchJobResultEnvelope;
13
+ exports.decodeJobSnapshotPayload = decodeJobSnapshotPayload;
14
+ exports.decodeDirectProgressPayload = decodeDirectProgressPayload;
15
+ exports.decodeDirectCompletedPayload = decodeDirectCompletedPayload;
16
+ exports.decodeDirectErrorPayload = decodeDirectErrorPayload;
17
+ exports.decodeDirectCancelledPayload = decodeDirectCancelledPayload;
18
+ exports.decodeKeepalivePayload = decodeKeepalivePayload;
19
+ exports.validateCreateCloudWebSearchJobInput = validateCreateCloudWebSearchJobInput;
20
+ const errors_1 = require("./errors");
21
+ const JOB_STATUSES = new Set(["queued", "in_progress", "completed", "error", "cancelled"]);
22
+ const MODES = new Set(["search", "research"]);
23
+ const TIME_RANGES = new Set(["day", "week", "month", "year"]);
24
+ function isPlainObject(value) {
25
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
26
+ return false;
27
+ }
28
+ const prototype = Object.getPrototypeOf(value);
29
+ return prototype === Object.prototype || prototype === null;
30
+ }
31
+ function cloneUnknown(value) {
32
+ if (Array.isArray(value)) {
33
+ return value.map((entry) => cloneUnknown(entry));
34
+ }
35
+ if (value instanceof Date) {
36
+ return new Date(value.getTime());
37
+ }
38
+ if (isPlainObject(value)) {
39
+ const cloned = {};
40
+ for (const [key, entry] of Object.entries(value)) {
41
+ cloned[key] = cloneUnknown(entry);
42
+ }
43
+ return cloned;
44
+ }
45
+ return value;
46
+ }
47
+ function isJobStatus(value) {
48
+ return typeof value === "string" && JOB_STATUSES.has(value);
49
+ }
50
+ function isMode(value) {
51
+ return typeof value === "string" && MODES.has(value);
52
+ }
53
+ function isTimeRange(value) {
54
+ return typeof value === "string" && TIME_RANGES.has(value);
55
+ }
56
+ function invalid(path, expected, actual) {
57
+ throw new errors_1.CloudWebSearchValidationError(path, expected, actual);
58
+ }
59
+ function expectObject(value, path) {
60
+ if (!isPlainObject(value)) {
61
+ invalid(path, "object", value);
62
+ }
63
+ return value;
64
+ }
65
+ function expectArray(value, path) {
66
+ if (!Array.isArray(value)) {
67
+ invalid(path, "array", value);
68
+ }
69
+ return value;
70
+ }
71
+ function expectString(value, path) {
72
+ if (typeof value !== "string") {
73
+ invalid(path, "string", value);
74
+ }
75
+ return value;
76
+ }
77
+ function expectNonEmptyString(value, path) {
78
+ const stringValue = expectString(value, path).trim();
79
+ if (!stringValue) {
80
+ invalid(path, "non-empty string", value);
81
+ }
82
+ return stringValue;
83
+ }
84
+ function expectNullableString(value, path) {
85
+ if (value === null) {
86
+ return null;
87
+ }
88
+ return expectString(value, path);
89
+ }
90
+ function expectOptionalNullableString(value, path) {
91
+ if (value === undefined) {
92
+ return undefined;
93
+ }
94
+ return expectNullableString(value, path);
95
+ }
96
+ function expectFiniteNumber(value, path) {
97
+ if (typeof value !== "number" || !Number.isFinite(value)) {
98
+ invalid(path, "finite number", value);
99
+ }
100
+ return value;
101
+ }
102
+ function expectPositiveInteger(value, path) {
103
+ const numberValue = expectFiniteNumber(value, path);
104
+ if (!Number.isInteger(numberValue) || numberValue <= 0) {
105
+ invalid(path, "positive integer", value);
106
+ }
107
+ return numberValue;
108
+ }
109
+ function expectOptionalRecord(value, path) {
110
+ if (value === undefined || value === null) {
111
+ return undefined;
112
+ }
113
+ return cloneUnknown(expectObject(value, path));
114
+ }
115
+ function expectOptionalNullableRecord(value, path) {
116
+ if (value === undefined) {
117
+ return undefined;
118
+ }
119
+ if (value === null) {
120
+ return null;
121
+ }
122
+ return cloneUnknown(expectObject(value, path));
123
+ }
124
+ function expectDateish(value, path) {
125
+ if (typeof value === "string") {
126
+ return value;
127
+ }
128
+ if (value instanceof Date && !Number.isNaN(value.getTime())) {
129
+ return new Date(value.getTime());
130
+ }
131
+ invalid(path, "ISO timestamp string or Date", value);
132
+ }
133
+ function expectOptionalNullableDateish(value, path) {
134
+ if (value === undefined) {
135
+ return undefined;
136
+ }
137
+ if (value === null) {
138
+ return null;
139
+ }
140
+ return expectDateish(value, path);
141
+ }
142
+ function normalizeTimestamp(value, path) {
143
+ if (typeof value === "string") {
144
+ return value;
145
+ }
146
+ if (value instanceof Date && !Number.isNaN(value.getTime())) {
147
+ return value.toISOString();
148
+ }
149
+ invalid(path, "ISO timestamp string or Date", value);
150
+ }
151
+ function parseOutput(value, options) {
152
+ if (options === null || options === void 0 ? void 0 : options.parseOutput) {
153
+ return options.parseOutput(cloneUnknown(value));
154
+ }
155
+ return cloneUnknown(value);
156
+ }
157
+ function decodeStringArray(value, path) {
158
+ return expectArray(value, path).map((entry, index) => expectString(entry, `${path}[${index}]`));
159
+ }
160
+ function decodeCloudWebSearchSource(value, path) {
161
+ const object = expectObject(value, path);
162
+ const source = {};
163
+ if ("title" in object) {
164
+ source.title = object.title === null ? null : expectString(object.title, `${path}.title`);
165
+ }
166
+ if ("url" in object) {
167
+ source.url = object.url === null ? null : expectString(object.url, `${path}.url`);
168
+ }
169
+ if ("host" in object) {
170
+ source.host = object.host === null ? null : expectString(object.host, `${path}.host`);
171
+ }
172
+ if ("snippet" in object) {
173
+ source.snippet = object.snippet === null ? null : expectString(object.snippet, `${path}.snippet`);
174
+ }
175
+ if ("relevantContent" in object) {
176
+ source.relevantContent = object.relevantContent === null
177
+ ? null
178
+ : expectString(object.relevantContent, `${path}.relevantContent`);
179
+ }
180
+ if ("keyFacts" in object) {
181
+ source.keyFacts = decodeStringArray(object.keyFacts, `${path}.keyFacts`);
182
+ }
183
+ if ("confidence" in object) {
184
+ if (object.confidence === null) {
185
+ source.confidence = null;
186
+ }
187
+ else {
188
+ source.confidence = expectFiniteNumber(object.confidence, `${path}.confidence`);
189
+ }
190
+ }
191
+ if ("score" in object) {
192
+ if (object.score === null) {
193
+ source.score = null;
194
+ }
195
+ else {
196
+ source.score = expectFiniteNumber(object.score, `${path}.score`);
197
+ }
198
+ }
199
+ if ("faviconUrl" in object) {
200
+ source.faviconUrl = object.faviconUrl === null ? null : expectString(object.faviconUrl, `${path}.faviconUrl`);
201
+ }
202
+ return source;
203
+ }
204
+ function decodeCloudWebSearchResult(value, options, path = "result") {
205
+ const object = expectObject(value, path);
206
+ const sources = expectArray(object.sources, `${path}.sources`).map((source, index) => decodeCloudWebSearchSource(source, `${path}.sources[${index}]`));
207
+ const result = {
208
+ sources,
209
+ };
210
+ if ("output" in object) {
211
+ result.output = parseOutput(object.output, options);
212
+ }
213
+ if ("text" in object) {
214
+ result.text = object.text === null ? null : expectString(object.text, `${path}.text`);
215
+ }
216
+ const metadata = expectOptionalRecord(object.metadata, `${path}.metadata`);
217
+ if (metadata !== undefined) {
218
+ result.metadata = metadata;
219
+ }
220
+ return result;
221
+ }
222
+ function decodeCloudWebSearchProgressEntry(value, path) {
223
+ const object = expectObject(value, path);
224
+ const metadata = expectOptionalRecord(object.metadata, `${path}.metadata`);
225
+ return Object.assign({ stage: expectNonEmptyString(object.stage, `${path}.stage`), message: expectNonEmptyString(object.message, `${path}.message`), timestamp: normalizeTimestamp(object.timestamp, `${path}.timestamp`) }, (metadata ? { metadata } : {}));
226
+ }
227
+ function decodeResearchPlanTodo(value, path) {
228
+ const object = expectObject(value, path);
229
+ return Object.assign(Object.assign({ id: expectNonEmptyString(object.id, `${path}.id`), title: expectNonEmptyString(object.title, `${path}.title`), objective: expectNonEmptyString(object.objective, `${path}.objective`) }, (object.searchQuery === undefined ? {} : {
230
+ searchQuery: expectString(object.searchQuery, `${path}.searchQuery`),
231
+ })), (object.rationale === undefined ? {} : {
232
+ rationale: expectString(object.rationale, `${path}.rationale`),
233
+ }));
234
+ }
235
+ function decodeResearchPlan(value, path) {
236
+ const object = expectObject(value, path);
237
+ return {
238
+ summary: expectNonEmptyString(object.summary, `${path}.summary`),
239
+ todos: expectArray(object.todos, `${path}.todos`).map((todo, index) => decodeResearchPlanTodo(todo, `${path}.todos[${index}]`)),
240
+ };
241
+ }
242
+ function decodeCloudWebSearchJobRecord(value, options, path = "job") {
243
+ const object = expectObject(value, path);
244
+ const mode = object.mode;
245
+ if (!isMode(mode)) {
246
+ invalid(`${path}.mode`, "\"search\" or \"research\"", mode);
247
+ }
248
+ const status = object.status;
249
+ if (!isJobStatus(status)) {
250
+ invalid(`${path}.status`, "\"queued\", \"in_progress\", \"completed\", \"error\", or \"cancelled\"", status);
251
+ }
252
+ const record = {
253
+ id: expectNonEmptyString(object.id, `${path}.id`),
254
+ projectId: expectNonEmptyString(object.projectId, `${path}.projectId`),
255
+ mode,
256
+ parameters: cloneUnknown(expectObject(object.parameters, `${path}.parameters`)),
257
+ status,
258
+ progress: expectArray(object.progress, `${path}.progress`).map((entry, index) => decodeCloudWebSearchProgressEntry(entry, `${path}.progress[${index}]`)),
259
+ createdAt: expectDateish(object.createdAt, `${path}.createdAt`),
260
+ updatedAt: expectDateish(object.updatedAt, `${path}.updatedAt`),
261
+ };
262
+ if ("searchQuery" in object) {
263
+ record.searchQuery = expectOptionalNullableString(object.searchQuery, `${path}.searchQuery`);
264
+ }
265
+ if ("searchInstructions" in object) {
266
+ record.searchInstructions = expectOptionalNullableString(object.searchInstructions, `${path}.searchInstructions`);
267
+ }
268
+ if ("responseInstructions" in object) {
269
+ record.responseInstructions = expectOptionalNullableString(object.responseInstructions, `${path}.responseInstructions`);
270
+ }
271
+ if ("responseSchema" in object) {
272
+ record.responseSchema = expectOptionalNullableRecord(object.responseSchema, `${path}.responseSchema`);
273
+ }
274
+ if ("researchPlan" in object) {
275
+ record.researchPlan = object.researchPlan === null
276
+ ? null
277
+ : decodeResearchPlan(object.researchPlan, `${path}.researchPlan`);
278
+ }
279
+ if ("results" in object) {
280
+ record.results = object.results === null
281
+ ? null
282
+ : decodeCloudWebSearchResult(object.results, options, `${path}.results`);
283
+ }
284
+ if ("errorMessage" in object) {
285
+ record.errorMessage = expectOptionalNullableString(object.errorMessage, `${path}.errorMessage`);
286
+ }
287
+ if ("cancelRequestedAt" in object) {
288
+ record.cancelRequestedAt = expectOptionalNullableDateish(object.cancelRequestedAt, `${path}.cancelRequestedAt`);
289
+ }
290
+ if ("cancelledAt" in object) {
291
+ record.cancelledAt = expectOptionalNullableDateish(object.cancelledAt, `${path}.cancelledAt`);
292
+ }
293
+ return record;
294
+ }
295
+ function decodeCloudWebSearchJobEventRecord(value, path = "event") {
296
+ const object = expectObject(value, path);
297
+ return {
298
+ id: expectNonEmptyString(object.id, `${path}.id`),
299
+ jobId: expectNonEmptyString(object.jobId, `${path}.jobId`),
300
+ projectId: expectNonEmptyString(object.projectId, `${path}.projectId`),
301
+ eventType: expectNonEmptyString(object.eventType, `${path}.eventType`),
302
+ stage: expectNullableString(object.stage, `${path}.stage`),
303
+ status: expectNullableString(object.status, `${path}.status`),
304
+ message: expectNullableString(object.message, `${path}.message`),
305
+ payload: cloneUnknown(object.payload),
306
+ createdAt: expectDateish(object.createdAt, `${path}.createdAt`),
307
+ };
308
+ }
309
+ function decodeCloudWebSearchJobListEnvelope(value, options, path = "jobs.list") {
310
+ const object = expectObject(value, path);
311
+ return {
312
+ jobs: expectArray(object.jobs, `${path}.jobs`).map((job, index) => decodeCloudWebSearchJobRecord(job, options, `${path}.jobs[${index}]`)),
313
+ };
314
+ }
315
+ function decodeCloudWebSearchJobResultEnvelope(value, options, path = "jobs.result") {
316
+ const object = expectObject(value, path);
317
+ return {
318
+ jobId: expectNonEmptyString(object.jobId, `${path}.jobId`),
319
+ status: expectString(object.status, `${path}.status`) === "completed"
320
+ ? "completed"
321
+ : invalid(`${path}.status`, "\"completed\"", object.status),
322
+ result: object.result === null
323
+ ? null
324
+ : decodeCloudWebSearchResult(object.result, options, `${path}.result`),
325
+ };
326
+ }
327
+ function decodeJobSnapshotPayload(value, options, path = "stream.job.snapshot") {
328
+ const object = expectObject(value, path);
329
+ return {
330
+ job: decodeCloudWebSearchJobRecord(object.job, options, `${path}.job`),
331
+ };
332
+ }
333
+ function decodeDirectProgressPayload(value, path = "stream.job.progress") {
334
+ const object = expectObject(value, path);
335
+ const status = object.status;
336
+ if (!isJobStatus(status)) {
337
+ invalid(`${path}.status`, "\"queued\", \"in_progress\", \"completed\", \"error\", or \"cancelled\"", status);
338
+ }
339
+ return {
340
+ jobId: expectNonEmptyString(object.jobId, `${path}.jobId`),
341
+ status,
342
+ progress: decodeCloudWebSearchProgressEntry(object.progress, `${path}.progress`),
343
+ };
344
+ }
345
+ function decodeDirectCompletedPayload(value, options, path = "stream.job.completed") {
346
+ const object = expectObject(value, path);
347
+ return {
348
+ job: decodeCloudWebSearchJobRecord(object.job, options, `${path}.job`),
349
+ result: object.result === null
350
+ ? null
351
+ : decodeCloudWebSearchResult(object.result, options, `${path}.result`),
352
+ };
353
+ }
354
+ function decodeDirectErrorPayload(value, options, path = "stream.job.error") {
355
+ const object = expectObject(value, path);
356
+ return {
357
+ job: decodeCloudWebSearchJobRecord(object.job, options, `${path}.job`),
358
+ errorMessage: expectNonEmptyString(object.errorMessage, `${path}.errorMessage`),
359
+ };
360
+ }
361
+ function decodeDirectCancelledPayload(value, options, path = "stream.job.cancelled") {
362
+ const object = expectObject(value, path);
363
+ return {
364
+ job: decodeCloudWebSearchJobRecord(object.job, options, `${path}.job`),
365
+ };
366
+ }
367
+ function decodeKeepalivePayload(value, path = "stream.job.keepalive") {
368
+ const object = expectObject(value, path);
369
+ return {
370
+ jobId: expectNonEmptyString(object.jobId, `${path}.jobId`),
371
+ timestamp: normalizeTimestamp(object.timestamp, `${path}.timestamp`),
372
+ };
373
+ }
374
+ function decodeExecutionParameters(value, path) {
375
+ const object = expectObject(value, path);
376
+ const parameters = {};
377
+ for (const [key, entry] of Object.entries(object)) {
378
+ switch (key) {
379
+ case "maxResults":
380
+ case "maxPages":
381
+ parameters[key] = expectPositiveInteger(entry, `${path}.${key}`);
382
+ break;
383
+ case "region":
384
+ parameters.region = expectNonEmptyString(entry, `${path}.region`);
385
+ break;
386
+ case "timeRange":
387
+ if (!isTimeRange(entry)) {
388
+ invalid(`${path}.timeRange`, "\"day\", \"week\", \"month\", or \"year\"", entry);
389
+ }
390
+ parameters.timeRange = entry;
391
+ break;
392
+ case "includeDomains":
393
+ parameters.includeDomains = decodeStringArray(entry, `${path}.includeDomains`);
394
+ break;
395
+ case "excludeDomains":
396
+ parameters.excludeDomains = decodeStringArray(entry, `${path}.excludeDomains`);
397
+ break;
398
+ case "outputSchema":
399
+ parameters.outputSchema = cloneUnknown(expectObject(entry, `${path}.outputSchema`));
400
+ break;
401
+ default:
402
+ parameters[key] = cloneUnknown(entry);
403
+ }
404
+ }
405
+ return parameters;
406
+ }
407
+ function validateCreateCloudWebSearchJobInput(value, path = "input") {
408
+ const object = expectObject(value, path);
409
+ const mode = object.mode;
410
+ if (!isMode(mode)) {
411
+ invalid(`${path}.mode`, "\"search\" or \"research\"", mode);
412
+ }
413
+ if (mode === "search") {
414
+ if ("instructions" in object) {
415
+ invalid(`${path}.instructions`, "field to be omitted in search mode", object.instructions);
416
+ }
417
+ if ("responseInstructions" in object) {
418
+ invalid(`${path}.responseInstructions`, "field to be omitted in search mode", object.responseInstructions);
419
+ }
420
+ if ("responseSchema" in object) {
421
+ invalid(`${path}.responseSchema`, "field to be omitted in search mode", object.responseSchema);
422
+ }
423
+ return Object.assign({ mode, query: expectNonEmptyString(object.query, `${path}.query`) }, (object.parameters === undefined ? {} : {
424
+ parameters: decodeExecutionParameters(object.parameters, `${path}.parameters`),
425
+ }));
426
+ }
427
+ if ("query" in object) {
428
+ invalid(`${path}.query`, "field to be omitted in research mode", object.query);
429
+ }
430
+ return Object.assign(Object.assign(Object.assign({ mode, instructions: expectNonEmptyString(object.instructions, `${path}.instructions`) }, (object.responseInstructions === undefined ? {} : {
431
+ responseInstructions: expectNonEmptyString(object.responseInstructions, `${path}.responseInstructions`),
432
+ })), (object.responseSchema === undefined ? {} : {
433
+ responseSchema: cloneUnknown(expectObject(object.responseSchema, `${path}.responseSchema`)),
434
+ })), (object.parameters === undefined ? {} : {
435
+ parameters: decodeExecutionParameters(object.parameters, `${path}.parameters`),
436
+ }));
437
+ }
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@kognitivedev/cloud-web-search",
3
+ "version": "0.2.28",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "dev": "tsc -w --noCheck",
12
+ "test": "vitest run",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "dependencies": {
16
+ "@kognitivedev/client-core": "workspace:*"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "^20.0.0",
20
+ "typescript": "^5.0.0",
21
+ "vitest": "^3.0.0"
22
+ },
23
+ "description": "Cloud web search SDK for Kognitive hosted job APIs",
24
+ "keywords": [
25
+ "kognitive",
26
+ "web-search",
27
+ "cloud",
28
+ "sdk",
29
+ "search",
30
+ "research"
31
+ ],
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/kognitivedev/kognitive",
36
+ "directory": "packages/cloud-web-search"
37
+ },
38
+ "homepage": "https://kognitive.dev"
39
+ }