@dromney/mapthis 0.1.0 → 0.2.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/README.md +108 -27
- package/dist/ai/index.cjs +105 -1
- package/dist/ai/index.cjs.map +1 -1
- package/dist/ai/index.d.cts +9 -9
- package/dist/ai/index.d.ts +9 -9
- package/dist/ai/index.js +101 -2
- package/dist/ai/index.js.map +1 -1
- package/dist/{domain-Dc1wSTkf.d.cts → domain-CL4ro2YW.d.cts} +1 -1
- package/dist/{domain-CZ-L-ntu.d.ts → domain-Dh8cPBVp.d.ts} +1 -1
- package/dist/generate/index.cjs +1 -1
- package/dist/generate/index.cjs.map +1 -1
- package/dist/generate/index.d.cts +4 -4
- package/dist/generate/index.d.ts +4 -4
- package/dist/generate/index.js +1 -1
- package/dist/generate/index.js.map +1 -1
- package/dist/geocoding/index.d.cts +3 -3
- package/dist/geocoding/index.d.ts +3 -3
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/parser-CrG1A6dD.d.ts +176 -0
- package/dist/parser-DsIwIsHK.d.cts +176 -0
- package/dist/react/index.d.cts +197 -0
- package/dist/react/index.d.ts +197 -0
- package/dist/{schemas-Dy5coqXo.d.cts → schemas-CZao45EU.d.cts} +1 -1
- package/dist/{schemas-Dy5coqXo.d.ts → schemas-CZao45EU.d.ts} +1 -1
- package/dist/types/index.cjs +1 -1
- package/dist/types/index.cjs.map +1 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/{types-BhqKlq0k.d.ts → types-BhPqZwOj.d.ts} +2 -2
- package/dist/{types-rFjK5YcJ.d.cts → types-C-xqh9Sh.d.cts} +2 -2
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/package.json +10 -6
- package/dist/parser-CzXzpmVv.d.cts +0 -111
- package/dist/parser-N7-fNxeu.d.ts +0 -111
package/README.md
CHANGED
|
@@ -6,50 +6,131 @@ Open-source location intelligence toolkit. Parse freeform text, HTML, and URLs i
|
|
|
6
6
|
npm install @dromney/mapthis
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
You'll also want one or more optional peer deps (see [Subpath exports](#subpath-exports)):
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
```bash
|
|
12
|
+
# pick what you use:
|
|
13
|
+
npm install openai js-tiktoken # @dromney/mapthis/ai (OpenAI)
|
|
14
|
+
npm install @anthropic-ai/sdk js-tiktoken # @dromney/mapthis/ai (Anthropic)
|
|
15
|
+
npm install @googlemaps/google-maps-services-js # @dromney/mapthis/geocoding
|
|
16
|
+
npm install html-to-text # @dromney/mapthis/scrape
|
|
17
|
+
npm install react react-dom @vis.gl/react-google-maps # @dromney/mapthis/react
|
|
18
|
+
```
|
|
12
19
|
|
|
13
|
-
|
|
14
|
-
|---|---|---|
|
|
15
|
-
| `@dromney/mapthis` | Re-exports types + utils (lightweight only) | 0.1 |
|
|
16
|
-
| `@dromney/mapthis/types` | Zod schemas and plain TS domain types (`PlaceMap`, `Place`, `PlaceGroup`, `Partner`, `ParsedLocation`, ...) | 0.1 |
|
|
17
|
-
| `@dromney/mapthis/utils` | Pure helpers: text, color, numbers, geo, stopwatch | 0.1 |
|
|
18
|
-
| `@dromney/mapthis/scrape` | `getHtmlFromUrl`, `htmlToText`, `getTextFromUrl` | 0.1 |
|
|
19
|
-
| `@dromney/mapthis/search` | `createSearchClient` (Google Custom Search) | 0.1 |
|
|
20
|
-
| `@dromney/mapthis/ai` | `createLocationParser`, `createOpenAiBackend`, `summarizeText`, prompts | 0.1 |
|
|
21
|
-
| `@dromney/mapthis/geocoding` | `createGeocoder` (Google Maps) with pluggable `GeocodingProvider` | 0.1 |
|
|
22
|
-
| `@dromney/mapthis/generate` | `createMapGenerator` — composes parser + geocoder into a pure orchestrator | 0.1 |
|
|
23
|
-
| `@dromney/mapthis/react` | `MapProvider`, `GoogleMapsViewer`, `PlaceMarker`, autofit, browser autocomplete | 0.1 |
|
|
24
|
-
|
|
25
|
-
## Usage
|
|
20
|
+
## Quick start
|
|
26
21
|
|
|
27
22
|
```ts
|
|
28
|
-
import { cities } from "@dromney/mapthis/utils"
|
|
29
|
-
import { placesFromUrl, type Coordinates } from "@dromney/mapthis/types"
|
|
30
|
-
|
|
31
23
|
import { createLocationParser } from "@dromney/mapthis/ai"
|
|
32
24
|
import { createGeocoder } from "@dromney/mapthis/geocoding"
|
|
33
25
|
import { createMapGenerator } from "@dromney/mapthis/generate"
|
|
34
26
|
|
|
35
27
|
const generator = createMapGenerator({
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
parser: createLocationParser({ apiKey: process.env.OPENAI_API_KEY! }),
|
|
29
|
+
geocoder: createGeocoder({ provider: "google", apiKey: process.env.GOOGLE_MAPS_KEY! }),
|
|
38
30
|
})
|
|
39
31
|
|
|
40
32
|
const result = await generator.generateFromSource({
|
|
41
|
-
|
|
42
|
-
|
|
33
|
+
sourceType: "url",
|
|
34
|
+
source: "https://example.com/best-restaurants-tokyo",
|
|
43
35
|
})
|
|
44
|
-
|
|
36
|
+
|
|
37
|
+
console.log(result.title)
|
|
38
|
+
for (const p of result.places) {
|
|
39
|
+
if (p.data) console.log(p.data.name, p.data.lat, p.data.lng)
|
|
40
|
+
else console.warn("could not geocode", p.input.address, "—", p.error)
|
|
41
|
+
}
|
|
45
42
|
```
|
|
46
43
|
|
|
44
|
+
`sourceType` accepts `"url"`, `"text"`, or `"list"`. For `"list"`, each newline-separated entry is geocoded directly with no LLM call (and no LLM cost).
|
|
45
|
+
|
|
46
|
+
## Subpath exports
|
|
47
|
+
|
|
48
|
+
`@dromney/mapthis` ships a root entry plus focused subpaths so consumers only load what they use.
|
|
49
|
+
|
|
50
|
+
| Subpath | What it contains | Optional peer deps |
|
|
51
|
+
|---|---|---|
|
|
52
|
+
| `@dromney/mapthis` | Re-exports types + utils (lightweight only) | — |
|
|
53
|
+
| `@dromney/mapthis/types` | Zod schemas + TS domain types (`PlaceMap`, `Place`, `PlaceGroup`, `Partner`, `ParsedLocation`, `Coordinates`, …) | — |
|
|
54
|
+
| `@dromney/mapthis/utils` | Pure helpers: text, color, numbers, geo, stopwatch | — |
|
|
55
|
+
| `@dromney/mapthis/scrape` | `getHtmlFromUrl`, `htmlToText`, `getTextFromUrl` | `html-to-text` |
|
|
56
|
+
| `@dromney/mapthis/search` | `createSearchClient` (Google Custom Search) | — |
|
|
57
|
+
| `@dromney/mapthis/ai` | `createLocationParser`, `createOpenAiBackend`, `createAnthropicBackend`, `summarizeText`, prompts | `openai` or `@anthropic-ai/sdk`, plus `js-tiktoken` |
|
|
58
|
+
| `@dromney/mapthis/geocoding` | `createGeocoder` (Google Maps) with pluggable `GeocodingProvider` | `@googlemaps/google-maps-services-js` |
|
|
59
|
+
| `@dromney/mapthis/generate` | `createMapGenerator` — composes parser + geocoder into a pure orchestrator | — (transitively whatever parser/geocoder use) |
|
|
60
|
+
| `@dromney/mapthis/react` | `MapProvider`, `GoogleMapsViewer`, `PlaceMarker`, `autofit`, `browserAutocomplete` | `react`, `react-dom`, `@vis.gl/react-google-maps` |
|
|
61
|
+
|
|
62
|
+
## Choosing an LLM backend
|
|
63
|
+
|
|
64
|
+
`createLocationParser` accepts either an OpenAI config, an Anthropic config, or a bring-your-own backend implementing the [`LlmBackend`](./src/ai/backend.ts) interface.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
// OpenAI (default)
|
|
68
|
+
createLocationParser({ apiKey: process.env.OPENAI_API_KEY! })
|
|
69
|
+
createLocationParser({ provider: "openai", apiKey: ..., model: "gpt-4o-mini" })
|
|
70
|
+
|
|
71
|
+
// Anthropic
|
|
72
|
+
createLocationParser({
|
|
73
|
+
provider: "anthropic",
|
|
74
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
75
|
+
model: "claude-haiku-4-5-20251001",
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
// Bring-your-own (e.g. an Azure/Bedrock adapter or a deterministic test fake)
|
|
79
|
+
createLocationParser({ backend: myCustomBackend })
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Both bundled backends implement the same two-stage pipeline: a chunked summarization pass to fit the model's context window, followed by a structured-output call (OpenAI function calling, Anthropic tool use) to extract the location list.
|
|
83
|
+
|
|
84
|
+
## Persisting results
|
|
85
|
+
|
|
86
|
+
`createMapGenerator` is intentionally pure — it returns a plain object and never touches a database. Wire the result into your own persistence layer:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const result = await generator.generateFromSource({ sourceType: "url", source: url })
|
|
90
|
+
|
|
91
|
+
await db.$transaction(async (tx) => {
|
|
92
|
+
const map = await tx.map.create({ data: { title: result.title /* … */ } })
|
|
93
|
+
const group = await tx.placeGroup.create({ data: { mapId: map.id /* … */ } })
|
|
94
|
+
await tx.place.createMany({
|
|
95
|
+
data: result.places.map((p, i) => ({
|
|
96
|
+
mapId: map.id,
|
|
97
|
+
groupId: group.id,
|
|
98
|
+
position: i,
|
|
99
|
+
query: p.input.address,
|
|
100
|
+
description: p.input.description,
|
|
101
|
+
name: p.data?.name,
|
|
102
|
+
address: p.data?.address,
|
|
103
|
+
lat: p.data?.lat,
|
|
104
|
+
lon: p.data?.lng,
|
|
105
|
+
provider: p.data?.provider,
|
|
106
|
+
providerId: p.data?.providerId,
|
|
107
|
+
error: p.error,
|
|
108
|
+
locationPromptVersion: result.locationPromptVersion,
|
|
109
|
+
summaryPromptVersion: result.summaryPromptVersion,
|
|
110
|
+
})),
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Error handling
|
|
116
|
+
|
|
117
|
+
Every error thrown by the package extends `MapthisError` (`@dromney/mapthis/types`). Domain-specific subclasses let callers handle the failure modes that matter to their UX:
|
|
118
|
+
|
|
119
|
+
| Class | Subpath | When |
|
|
120
|
+
|---|---|---|
|
|
121
|
+
| `InvalidUrlError`, `ScrapeError` | `/scrape` | URL was unreachable or returned non-HTML |
|
|
122
|
+
| `NoSearchResultsError`, `SearchError` | `/search` | Google CSE returned zero usable results |
|
|
123
|
+
| `NoLocationsFoundError`, `AiInputLengthError`, `AiOutputLengthError`, `AiResponseJsonError`, `InvalidJsonSchemaError`, `SummarizeTextError`, `AiError` | `/ai` | LLM stage failed |
|
|
124
|
+
| `GeocodingError` | `/geocoding` | Geocoder threw (per-input failures resolve, not throw) |
|
|
125
|
+
|
|
126
|
+
Per-input geocoder failures are *not* thrown — `geocodeMany` returns a `PlaceQueryResult` with `data: null` and `error: string` so one bad address can't fail an entire batch.
|
|
127
|
+
|
|
47
128
|
## Design principles
|
|
48
129
|
|
|
49
|
-
- **No `process.env` reads.** All secrets
|
|
50
|
-
- **Framework-agnostic.**
|
|
51
|
-
- **Plain domain types.**
|
|
52
|
-
- **Optional peer deps for heavy things.**
|
|
130
|
+
- **No `process.env` reads.** All secrets are passed to factory functions by the consumer. Keeps the package portable and testable.
|
|
131
|
+
- **Framework-agnostic.** Core modules are pure TypeScript. React components live behind the `@dromney/mapthis/react` subpath so server-only consumers skip them entirely.
|
|
132
|
+
- **Plain domain types.** Hand-rolled TS with matching Zod schemas. Consumers aren't locked to a particular ORM — write adapter functions between your DB models and these types in your app.
|
|
133
|
+
- **Optional peer deps for heavy things.** Every SDK is an optional peer dependency. Install only what the subpaths you use need.
|
|
53
134
|
|
|
54
135
|
## License
|
|
55
136
|
|
package/dist/ai/index.cjs
CHANGED
|
@@ -4,10 +4,12 @@ var zod = require('zod');
|
|
|
4
4
|
var htmlToText$1 = require('html-to-text');
|
|
5
5
|
var jsTiktoken = require('js-tiktoken');
|
|
6
6
|
var OpenAI = require('openai');
|
|
7
|
+
var Anthropic = require('@anthropic-ai/sdk');
|
|
7
8
|
|
|
8
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
10
|
|
|
10
11
|
var OpenAI__default = /*#__PURE__*/_interopDefault(OpenAI);
|
|
12
|
+
var Anthropic__default = /*#__PURE__*/_interopDefault(Anthropic);
|
|
11
13
|
|
|
12
14
|
// src/types/errors.ts
|
|
13
15
|
var MapthisError = class extends Error {
|
|
@@ -437,10 +439,108 @@ function createOpenAiBackend(config) {
|
|
|
437
439
|
}
|
|
438
440
|
};
|
|
439
441
|
}
|
|
442
|
+
var DEFAULT_ANTHROPIC_MODEL = "claude-haiku-4-5-20251001";
|
|
443
|
+
var DEFAULT_ANTHROPIC_TOKEN_LIMIT = 2e5;
|
|
444
|
+
var DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS = 4096;
|
|
445
|
+
function resolveEncoding2(model) {
|
|
446
|
+
try {
|
|
447
|
+
return jsTiktoken.encodingForModel(model);
|
|
448
|
+
} catch {
|
|
449
|
+
return jsTiktoken.getEncoding("cl100k_base");
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function createAnthropicBackend(config) {
|
|
453
|
+
const model = config.model ?? DEFAULT_ANTHROPIC_MODEL;
|
|
454
|
+
const tokenLimit = config.tokenLimit ?? DEFAULT_ANTHROPIC_TOKEN_LIMIT;
|
|
455
|
+
const maxOutputTokens = config.maxOutputTokens ?? DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS;
|
|
456
|
+
const client = new Anthropic__default.default({
|
|
457
|
+
apiKey: config.apiKey,
|
|
458
|
+
...config.baseURL ? { baseURL: config.baseURL } : {}
|
|
459
|
+
});
|
|
460
|
+
const encoding = resolveEncoding2(model);
|
|
461
|
+
return {
|
|
462
|
+
model,
|
|
463
|
+
tokenLimit,
|
|
464
|
+
countTokens(text) {
|
|
465
|
+
return encoding.encode(text).length;
|
|
466
|
+
},
|
|
467
|
+
async chatCompletion(content) {
|
|
468
|
+
const response = await client.messages.create({
|
|
469
|
+
model,
|
|
470
|
+
max_tokens: maxOutputTokens,
|
|
471
|
+
messages: [{ role: "user", content }]
|
|
472
|
+
});
|
|
473
|
+
const text = response.content.filter((b) => b.type === "text").map((b) => b.text).join("");
|
|
474
|
+
return {
|
|
475
|
+
output: text,
|
|
476
|
+
inputTokens: response.usage.input_tokens,
|
|
477
|
+
outputTokens: response.usage.output_tokens
|
|
478
|
+
};
|
|
479
|
+
},
|
|
480
|
+
async chatFunctionJson(systemMessage, userMessage, schema, functionName) {
|
|
481
|
+
try {
|
|
482
|
+
const response = await client.messages.create({
|
|
483
|
+
model,
|
|
484
|
+
max_tokens: maxOutputTokens,
|
|
485
|
+
system: systemMessage,
|
|
486
|
+
tools: [
|
|
487
|
+
{
|
|
488
|
+
name: functionName,
|
|
489
|
+
description: `Return structured output matching the ${functionName} schema.`,
|
|
490
|
+
input_schema: schema
|
|
491
|
+
}
|
|
492
|
+
],
|
|
493
|
+
tool_choice: { type: "tool", name: functionName },
|
|
494
|
+
messages: [{ role: "user", content: userMessage }]
|
|
495
|
+
});
|
|
496
|
+
const toolUse = response.content.find(
|
|
497
|
+
(b) => b.type === "tool_use"
|
|
498
|
+
);
|
|
499
|
+
if (!toolUse) {
|
|
500
|
+
throw new AiResponseJsonError("No tool use in Anthropic response");
|
|
501
|
+
}
|
|
502
|
+
if (response.stop_reason === "max_tokens") {
|
|
503
|
+
throw new AiOutputLengthError(
|
|
504
|
+
`Anthropic hit max_tokens (${maxOutputTokens}) before completing tool call`
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
return {
|
|
508
|
+
output: toolUse.input,
|
|
509
|
+
inputTokens: response.usage.input_tokens,
|
|
510
|
+
outputTokens: response.usage.output_tokens
|
|
511
|
+
};
|
|
512
|
+
} catch (error) {
|
|
513
|
+
if (error instanceof AiError) throw error;
|
|
514
|
+
if (error instanceof Anthropic__default.default.APIError) {
|
|
515
|
+
const msg = (error.message ?? "").toLowerCase();
|
|
516
|
+
if (msg.includes("prompt is too long") || msg.includes("max_tokens")) {
|
|
517
|
+
throw new AiInputLengthError(`Text too long: ${error.message}`, {
|
|
518
|
+
cause: error
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
if (msg.includes("invalid") && msg.includes("schema")) {
|
|
522
|
+
throw new InvalidJsonSchemaError(`Invalid schema: ${error.message}`, {
|
|
523
|
+
cause: error
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
throw new AiError(`Anthropic error: ${error.message}`, { cause: error });
|
|
527
|
+
}
|
|
528
|
+
throw new AiError("Anthropic error (unknown)", {
|
|
529
|
+
cause: error instanceof Error ? error : void 0
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
}
|
|
440
535
|
|
|
441
536
|
// src/ai/parser.ts
|
|
537
|
+
function resolveBackend(config) {
|
|
538
|
+
if ("backend" in config) return config.backend;
|
|
539
|
+
if (config.provider === "anthropic") return createAnthropicBackend(config);
|
|
540
|
+
return createOpenAiBackend(config);
|
|
541
|
+
}
|
|
442
542
|
function createLocationParser(config) {
|
|
443
|
-
const backend =
|
|
543
|
+
const backend = resolveBackend(config);
|
|
444
544
|
return {
|
|
445
545
|
parseLocationsFromText: (text) => parseLocationsFromText(backend, text),
|
|
446
546
|
parseLocationsFromHtml: (html) => parseLocationsFromHtml(backend, html),
|
|
@@ -452,6 +552,9 @@ exports.AiError = AiError;
|
|
|
452
552
|
exports.AiInputLengthError = AiInputLengthError;
|
|
453
553
|
exports.AiOutputLengthError = AiOutputLengthError;
|
|
454
554
|
exports.AiResponseJsonError = AiResponseJsonError;
|
|
555
|
+
exports.DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS = DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS;
|
|
556
|
+
exports.DEFAULT_ANTHROPIC_MODEL = DEFAULT_ANTHROPIC_MODEL;
|
|
557
|
+
exports.DEFAULT_ANTHROPIC_TOKEN_LIMIT = DEFAULT_ANTHROPIC_TOKEN_LIMIT;
|
|
455
558
|
exports.DEFAULT_MODEL = DEFAULT_MODEL;
|
|
456
559
|
exports.DEFAULT_TOKEN_LIMIT = DEFAULT_TOKEN_LIMIT;
|
|
457
560
|
exports.InvalidJsonSchemaError = InvalidJsonSchemaError;
|
|
@@ -464,6 +567,7 @@ exports.NoLocationsFoundError = NoLocationsFoundError;
|
|
|
464
567
|
exports.SUMMARIZATION_PROMPT = SUMMARIZATION_PROMPT;
|
|
465
568
|
exports.SUMMARIZE_PROMPT_VERSION = SUMMARIZE_PROMPT_VERSION;
|
|
466
569
|
exports.SummarizeTextError = SummarizeTextError;
|
|
570
|
+
exports.createAnthropicBackend = createAnthropicBackend;
|
|
467
571
|
exports.createLocationParser = createLocationParser;
|
|
468
572
|
exports.createOpenAiBackend = createOpenAiBackend;
|
|
469
573
|
exports.parseLocationsFromHtml = parseLocationsFromHtml;
|
package/dist/ai/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/types/errors.ts","../../src/ai/errors.ts","../../src/ai/prompts.ts","../../src/scrape/errors.ts","../../src/scrape/html.ts","../../src/scrape/text.ts","../../src/ai/summarize.ts","../../src/ai/locations.ts","../../src/ai/openai.ts","../../src/ai/parser.ts"],"names":["z","convert","encodingForModel","getEncoding","OpenAI"],"mappings":";;;;;;;;;;;;AAOO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EACpC,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AAAA,EAChB;AACJ,CAAA;;;ACVO,IAAM,OAAA,GAAN,cAAsB,YAAA,CAAa;AAAA,EACtC,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,SAAA;AAAA,EAChB;AACJ;AAKO,IAAM,qBAAA,GAAN,cAAoC,OAAA,CAAQ;AAAA,EAC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AAA,IACtE,qBAAA,EAAuB,CAAC,GAAA,KAAQ,qBAAA,CAAsB,SAAS,GAAG;AAAA,GACtE;AACJ","file":"index.cjs","sourcesContent":["/**\n * Base error class for every error thrown by the mapthis package.\n *\n * All feature-specific error classes (scrape, search, ai, geocoding) extend\n * this, so consumers can do `catch (e) { if (e instanceof MapthisError) ... }`\n * to distinguish library errors from other exceptions.\n */\nexport class MapthisError extends Error {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"MapthisError\"\n }\n}\n","import { MapthisError } from \"../types/errors\"\n\nexport class AiError extends MapthisError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiError\"\n }\n}\n\n/**\n * The LLM returned zero locations after deduplication.\n */\nexport class NoLocationsFoundError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message ?? \"No locations found in input\", options)\n this.name = \"NoLocationsFoundError\"\n }\n}\n\n/**\n * Summarization pipeline failed (one of the chunk calls rejected).\n */\nexport class SummarizeTextError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"SummarizeTextError\"\n }\n}\n\n/**\n * A JSON schema passed to the LLM was rejected as invalid by the provider.\n */\nexport class InvalidJsonSchemaError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"InvalidJsonSchemaError\"\n }\n}\n\n/**\n * The LLM response could not be parsed as JSON (typically because the output\n * was truncated or malformed).\n */\nexport class AiResponseJsonError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiResponseJsonError\"\n }\n}\n\n/**\n * Input text exceeded the model's context window.\n */\nexport class AiInputLengthError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiInputLengthError\"\n }\n}\n\n/**\n * The LLM hit the output token limit before completing its JSON response.\n */\nexport class AiOutputLengthError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiOutputLengthError\"\n }\n}\n","import type { JSONSchema7Object } from \"json-schema\"\n\n/**\n * Semantic version of the location-extraction system prompt. Stored alongside\n * each place record in the private app so results can be re-run when the\n * prompt is updated. Bump when you change {@link LOCATIONS_SYSTEM_MESSAGE} or\n * {@link LOCATIONS_SCHEMA} in a way that meaningfully changes output.\n */\nexport const LOCATIONS_PROMPT_VERSION = \"0.0.1\"\n\nexport const LOCATIONS_FUNCTIONS_NAME = \"get_places\"\n\nexport const LOCATIONS_SYSTEM_MESSAGE = `You parse a list of place addresses, corresponding brief one-sentence descriptions, and a descriptive title (preferably directly quoted) from text. These addresses might be countries, cities, attractions, parks, bars, etc.\nIf the text seems to be listing places that are all within a single parent place, you don't return the parent place, but you do add it to all the childrens' addresses.\nFor example, for an article about places in greece, you would not return \"greece\", but if the Acropolis was mentioned, you would return \"Acropolis, Athens, Greece\"\n\nYou ignore extraneous text, such as recommended articles, advertisments, etc.\n`\n\nexport const LOCATIONS_SCHEMA: JSONSchema7Object = {\n type: \"object\",\n required: [\"locations\", \"title\"],\n properties: {\n locations: {\n type: \"array\",\n items: {\n type: \"object\",\n required: [\"address\", \"description\"],\n properties: {\n address: { type: \"string\" },\n description: { type: \"string\" },\n },\n },\n },\n title: { type: \"string\" },\n },\n}\n\nexport const LOCATIONS_MAX_OUTPUT_TOKENS = 500\n\n/**\n * Semantic version of the summarization prompt. Bump when meaningful changes\n * are made to {@link SUMMARIZATION_PROMPT}.\n */\nexport const SUMMARIZE_PROMPT_VERSION = \"0.0.1\"\n\nexport const SUMMARIZATION_PROMPT = `Summarize the given text. The summary must reduce the number of characters from the original text by at least a factor of {RATIO}.\nFor example, text of 1000 characters must output a summary less than {EXAMPLE} characters.\nThis summary will be used to extract a list of locations and addresses, so keep any text that seems to be a list of locations/places, a location and paragraph describing it, or otherwise clearly a location name or address.\nHere is the text:\n`\n","import { MapthisError } from \"../types/errors\"\n\nexport class ScrapeError extends MapthisError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"ScrapeError\"\n }\n}\n\n/**\n * Thrown when a provided URL fails validation or cannot be parsed by `fetch`.\n */\nexport class InvalidUrlError extends ScrapeError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"InvalidUrlError\"\n }\n}\n\n/**\n * Thrown when a remote server returns 401 or 403 for an HTML fetch, meaning\n * the page does not permit anonymous scraping.\n */\nexport class HtmlUnauthorizedError extends ScrapeError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"HtmlUnauthorizedError\"\n }\n}\n\n/**\n * Thrown when the underlying `html-to-text` conversion fails.\n */\nexport class HtmlToTextError extends ScrapeError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"HtmlToTextError\"\n }\n}\n","import { z } from \"zod\"\nimport { HtmlUnauthorizedError, InvalidUrlError, ScrapeError } from \"./errors\"\n\nconst withHttps = (url: string): string => {\n const hasHttp = /^http?:\\/\\//i.test(url)\n const hasHttps = /^https?:\\/\\//i.test(url)\n if (!hasHttp && !hasHttps) return `https://${url}`\n return url\n}\n\n/**\n * Fetch the raw HTML of a URL, performing light validation first.\n *\n * - URLs without a scheme are prefixed with `https://`.\n * - URL shape is validated via Zod before the network call.\n * - 401/403 responses throw {@link HtmlUnauthorizedError}.\n * - Other non-OK responses throw {@link ScrapeError} with the status text.\n * - `TypeError` from `fetch` (typically malformed URL) is rethrown as\n * {@link InvalidUrlError}.\n *\n * This function does not set any special headers, user agent, or cookies.\n * Sites with aggressive bot-detection may return empty bodies or errors; the\n * caller is responsible for retry / user-agent rotation strategies if needed.\n */\nexport async function getHtmlFromUrl(url: string): Promise<string> {\n const urlFixed = withHttps(url.toLowerCase())\n const isUrl = z.string().url().safeParse(urlFixed)\n if (!isUrl.success) throw new InvalidUrlError(`Invalid URL: ${url}`)\n\n try {\n const response = await fetch(urlFixed)\n const html = await response.text()\n if (response.status === 403 || response.status === 401) {\n throw new HtmlUnauthorizedError(\n `${response.status} Not authorized to scrape this website`,\n )\n }\n if (!response.ok) {\n throw new ScrapeError(\n `Bad response when getting HTML. Status: ${response.status} ${response.statusText}`,\n )\n }\n return html\n } catch (error) {\n if (error instanceof TypeError) {\n throw new InvalidUrlError(`Invalid URL. Original error: ${error.message}`)\n }\n throw error\n }\n}\n","import { convert } from \"html-to-text\"\nimport { HtmlToTextError } from \"./errors\"\nimport { getHtmlFromUrl } from \"./html\"\n\nconst SKIP_SELECTORS = [\n \"img\",\n \"header\",\n \"footer\",\n \"audio\",\n \"button\",\n \"canvas\",\n \"code\",\n \"nav\",\n \"#nav\",\n \"figure\",\n \"figcaption\",\n \".comment\",\n \".comments\",\n \"#comments\",\n \"#related-posts\",\n \"#related\",\n \".related\",\n \".related-posts\",\n] as const\n\n/**\n * Convert raw HTML to plain text suitable for LLM consumption.\n *\n * The converter is configured to:\n * - strip anchor hrefs when they duplicate the link text;\n * - skip navigation, footer, comments, related-post widgets, images, and\n * other non-content elements;\n * - collapse repeated blank lines and double spaces so token count doesn't\n * balloon from whitespace.\n */\nexport function htmlToText(html: string): string {\n try {\n let out = convert(html, {\n wordwrap: false,\n selectors: [\n {\n selector: \"a\",\n options: {\n ignoreHref: true,\n hideLinkHrefIfSameAsText: true,\n },\n },\n ...SKIP_SELECTORS.map((tag) => ({ selector: tag, format: \"skip\" })),\n ],\n })\n out = out.replaceAll(\"\\r\\n\", \"\\n\")\n // Iteratively collapse blank lines and double spaces. 20 passes is\n // overkill for any realistic input but cheap and bounds worst case.\n for (let i = 0; i < 20; i++) {\n out = out.replaceAll(\"\\n\\n\\n\", \"\\n\\n\")\n out = out.replaceAll(\" \", \" \")\n }\n return out\n } catch (error) {\n if (error instanceof Error) {\n throw new HtmlToTextError(`HTML to text conversion failed: ${error.message}`, {\n cause: error,\n })\n }\n throw new HtmlToTextError(\"HTML to text conversion failed\")\n }\n}\n\n/**\n * Convenience: fetch a URL and convert its HTML to plain text in one call.\n *\n * Equivalent to `htmlToText(await getHtmlFromUrl(url))`.\n */\nexport async function getTextFromUrl(url: string): Promise<string> {\n const html = await getHtmlFromUrl(url)\n return htmlToText(html)\n}\n","import { SummarizeTextError } from \"./errors\"\nimport type { OpenAiBackend } from \"./openai\"\nimport { SUMMARIZATION_PROMPT, SUMMARIZE_PROMPT_VERSION } from \"./prompts\"\nimport type { SummarizedText } from \"./types\"\n\n/**\n * Reduce a large text to a summary that will fit inside the model's context\n * window alongside a known downstream prompt (e.g. the location-extraction\n * call).\n *\n * Algorithm overview\n * ------------------\n * We need to fit both the summary and a downstream \"final\" prompt into the\n * model's context window. Given:\n *\n * - `token_limit` — model context window (with a safety factor)\n * - `text_tokens` — size of the input text\n * - `summarize_prompt_tokens` — size of the summarization prompt itself\n * - `final_prompt_tokens` — size of the downstream prompt\n * - `final_output_tokens` — expected size of the downstream response\n *\n * We split the input into chunks, summarize each in parallel, and concatenate\n * the results. The per-chunk size is chosen so that the combined summary\n * leaves room for the downstream prompt:\n *\n * final_input_tokens = token_limit - final_output_tokens\n * summary_output_tokens = final_input_tokens - final_prompt_tokens\n * num_chunks = ceil((text_tokens + summary_output_tokens)\n * / (token_limit - summarize_prompt_tokens))\n * chunk_output_tokens = summary_output_tokens / num_chunks\n * chunk_input_tokens = token_limit - summarize_prompt_tokens - chunk_output_tokens\n *\n * A safety factor is applied to chunk output sizes because LLMs routinely\n * overshoot explicit length instructions.\n */\nexport async function summarizeText(\n backend: OpenAiBackend,\n text: string,\n finalPrompt = \"\",\n finalOutputTokens = 0,\n chunkOutputSafetyFactor = 0.3,\n tokenLimitSafetyFactor = 0.02,\n): Promise<SummarizedText> {\n const tokenLimit = backend.tokenLimit * (1 - tokenLimitSafetyFactor)\n const textTokens = backend.countTokens(text)\n const summarizePromptTokens = backend.countTokens(SUMMARIZATION_PROMPT)\n const finalPromptTokens = backend.countTokens(finalPrompt)\n\n const finalInputTokens = tokenLimit - finalOutputTokens\n const summaryOutputTokens = finalInputTokens - finalPromptTokens\n\n const numChunks = Math.max(\n 1,\n Math.ceil((textTokens + summaryOutputTokens) / (tokenLimit - summarizePromptTokens)),\n )\n const chunkOutputTokens = Math.floor(\n (summaryOutputTokens / numChunks) * (1 - chunkOutputSafetyFactor),\n )\n const chunkInputTokens = Math.floor(tokenLimit - summarizePromptTokens - chunkOutputTokens)\n const summaryRatio = chunkOutputTokens / chunkInputTokens\n\n // Split the text into chunks that each fit inside `chunkInputTokens`.\n // Lines longer than a single chunk are force-split by token, not by\n // character, so UTF-8 multibyte characters aren't corrupted.\n const textLines = text.split(\"\\n\")\n const chunks: string[] = []\n\n let currentChunk = \"\"\n let currentChunkTokens = 0\n const nextChunk = (): void => {\n if (currentChunk) chunks.push(currentChunk)\n currentChunk = \"\"\n currentChunkTokens = 0\n }\n\n // We need an encoder for the force-split path. The backend's countTokens\n // gives us length but not the encoded array; for force-splitting we only\n // need to chunk by an approximate token count using the same rounding.\n // To keep this function backend-agnostic, we fall back to character-based\n // splitting for over-long lines (rare edge case in practice).\n for (const line of textLines) {\n const numLineTokens = backend.countTokens(line) + 1 // +1 for the newline\n\n if (numLineTokens > chunkInputTokens) {\n // Long line: flush current chunk, then split this line by\n // approximate token ratio into sub-chunks.\n nextChunk()\n const charsPerToken = line.length / Math.max(1, backend.countTokens(line))\n const charsPerChunk = Math.max(1, Math.floor(chunkInputTokens * charsPerToken))\n for (let i = 0; i < line.length; i += charsPerChunk) {\n chunks.push(line.slice(i, i + charsPerChunk))\n }\n continue\n }\n\n if (currentChunkTokens + numLineTokens > chunkInputTokens) {\n nextChunk()\n }\n currentChunkTokens += numLineTokens\n currentChunk += line + \"\\n\"\n }\n nextChunk()\n\n try {\n const ratioText = (Math.ceil(summaryRatio * 10) / 10).toFixed(1)\n const exampleText = Math.ceil(1000 / Math.max(summaryRatio, 0.01)).toFixed(0)\n const summaryPrompt = SUMMARIZATION_PROMPT.replace(\"{RATIO}\", ratioText).replace(\n \"{EXAMPLE}\",\n exampleText,\n )\n\n let inputTokens = 0\n let outputTokens = 0\n const summaries = await Promise.all(\n chunks.map(async (chunk) => {\n const response = await backend.chatCompletion(summaryPrompt + chunk)\n inputTokens += response.inputTokens\n outputTokens += response.outputTokens\n return response.output\n }),\n )\n\n const summarized = summaries.join(\"\\n\")\n const summarizedTokens = backend.countTokens(summarized)\n\n return {\n text,\n textTokens,\n summarized,\n summarizedTokens,\n inputTokens,\n outputTokens,\n chunks: chunks.length,\n summarizePromptVersion: SUMMARIZE_PROMPT_VERSION,\n }\n } catch (error) {\n if (error instanceof Error) {\n throw new SummarizeTextError(`Failed to summarize text: ${error.message}`, {\n cause: error,\n })\n }\n throw new SummarizeTextError(\"Failed to summarize text (unknown error)\")\n }\n}\n","import { getHtmlFromUrl } from \"../scrape/html\"\nimport { htmlToText } from \"../scrape/text\"\nimport type { ParsedLocations } from \"../types/domain\"\nimport { NoLocationsFoundError } from \"./errors\"\nimport type { OpenAiBackend } from \"./openai\"\nimport {\n LOCATIONS_FUNCTIONS_NAME,\n LOCATIONS_MAX_OUTPUT_TOKENS,\n LOCATIONS_PROMPT_VERSION,\n LOCATIONS_SCHEMA,\n LOCATIONS_SYSTEM_MESSAGE,\n} from \"./prompts\"\nimport { summarizeText } from \"./summarize\"\nimport type { GetLocationsResponse } from \"./types\"\n\nconst FINAL_PROMPT = LOCATIONS_SYSTEM_MESSAGE + JSON.stringify(LOCATIONS_SCHEMA) + LOCATIONS_FUNCTIONS_NAME\n\nasync function callLocationExtraction(backend: OpenAiBackend, text: string) {\n const out = await backend.chatFunctionJson<ParsedLocations>(\n LOCATIONS_SYSTEM_MESSAGE,\n text,\n LOCATIONS_SCHEMA,\n LOCATIONS_FUNCTIONS_NAME,\n )\n\n // LLM frequently returns the same address twice across different prompts.\n // Dedupe by address string.\n const seen = new Set<string>()\n const unique = out.output.locations.filter((loc) => {\n if (seen.has(loc.address)) return false\n seen.add(loc.address)\n return true\n })\n if (unique.length === 0) throw new NoLocationsFoundError()\n out.output.locations = unique\n\n return out\n}\n\n/**\n * Parse locations from freeform text. Two-stage: summarize to fit the context\n * window, then a function-calling JSON completion for extraction.\n */\nexport async function parseLocationsFromText(\n backend: OpenAiBackend,\n text: string,\n): Promise<GetLocationsResponse> {\n const {\n summarized,\n inputTokens: sumIn,\n outputTokens: sumOut,\n textTokens,\n summarizedTokens,\n summarizePromptVersion,\n } = await summarizeText(backend, text, FINAL_PROMPT, LOCATIONS_MAX_OUTPUT_TOKENS)\n\n const {\n output,\n inputTokens: locIn,\n outputTokens: locOut,\n } = await callLocationExtraction(backend, summarized)\n\n return {\n output,\n locationInputTokens: locIn,\n locationOutputTokens: locOut,\n summaryInputTokens: sumIn,\n summaryOutputTokens: sumOut,\n inputTokens: sumIn + locIn,\n outputTokens: sumOut + locOut,\n textTokens,\n summarizedTokens,\n summaryPromptVersion: summarizePromptVersion,\n locationPromptVersion: LOCATIONS_PROMPT_VERSION,\n }\n}\n\n/**\n * Parse locations from HTML. Equivalent to `htmlToText` followed by\n * {@link parseLocationsFromText}.\n */\nexport async function parseLocationsFromHtml(\n backend: OpenAiBackend,\n html: string,\n): Promise<GetLocationsResponse> {\n return parseLocationsFromText(backend, htmlToText(html))\n}\n\n/**\n * Parse locations from a URL. Equivalent to fetching the HTML, converting to\n * text, and calling {@link parseLocationsFromText}. Throws any scrape errors\n * unchanged.\n */\nexport async function parseLocationsFromUrl(\n backend: OpenAiBackend,\n url: string,\n): Promise<GetLocationsResponse> {\n const html = await getHtmlFromUrl(url)\n return parseLocationsFromHtml(backend, html)\n}\n","import { encodingForModel, getEncoding, type Tiktoken, type TiktokenModel } from \"js-tiktoken\"\nimport OpenAI from \"openai\"\nimport type { JSONSchema7Object } from \"json-schema\"\nimport {\n AiInputLengthError,\n AiOutputLengthError,\n AiResponseJsonError,\n AiError,\n InvalidJsonSchemaError,\n} from \"./errors\"\nimport type { OpenAIOutput } from \"./types\"\n\nexport const DEFAULT_MODEL = \"gpt-4o-mini\"\nexport const DEFAULT_TOKEN_LIMIT = 128000\n\n/**\n * An OpenAI-backed inference context shared by the summarizer and location\n * parser. Instances are produced by {@link createOpenAiBackend} so API keys\n * and model choices live in the consumer's code, never in module-level state.\n */\nexport interface OpenAiBackend {\n readonly model: string\n readonly tokenLimit: number\n countTokens(text: string): number\n chatCompletion(content: string): Promise<OpenAIOutput<string>>\n chatFunctionJson<T>(\n systemMessage: string,\n userMessage: string,\n schema: JSONSchema7Object,\n functionName: string,\n ): Promise<OpenAIOutput<T>>\n}\n\nexport interface OpenAiBackendConfig {\n /** OpenAI API key. Passed directly to the SDK. */\n apiKey: string\n /** Model name passed to the completions API. Defaults to `gpt-4o-mini`. */\n model?: string\n /** Context-window budget used by the summarizer. Defaults to 128k. */\n tokenLimit?: number\n /**\n * Optional base URL for the OpenAI API. Use this to route through an\n * OpenAI-compatible gateway (Azure, Together, etc.).\n */\n baseURL?: string\n}\n\nfunction resolveEncoding(model: string): Tiktoken {\n try {\n return encodingForModel(model as TiktokenModel)\n } catch {\n // Fallback for models tiktoken does not recognize by name.\n return getEncoding(\"cl100k_base\")\n }\n}\n\n/**\n * Construct an {@link OpenAiBackend} bound to a specific API key and model.\n */\nexport function createOpenAiBackend(config: OpenAiBackendConfig): OpenAiBackend {\n const model = config.model ?? DEFAULT_MODEL\n const tokenLimit = config.tokenLimit ?? DEFAULT_TOKEN_LIMIT\n const openai = new OpenAI({\n apiKey: config.apiKey,\n ...(config.baseURL ? { baseURL: config.baseURL } : {}),\n })\n const encoding = resolveEncoding(model)\n\n return {\n model,\n tokenLimit,\n\n countTokens(text: string): number {\n return encoding.encode(text).length\n },\n\n async chatCompletion(content: string): Promise<OpenAIOutput<string>> {\n const response = await openai.chat.completions.create({\n messages: [{ role: \"user\", content }],\n model,\n })\n return {\n output: response.choices[0]?.message.content ?? \"\",\n inputTokens: response.usage?.prompt_tokens ?? 0,\n outputTokens: response.usage?.completion_tokens ?? 0,\n }\n },\n\n async chatFunctionJson<T>(\n systemMessage: string,\n userMessage: string,\n schema: JSONSchema7Object,\n functionName: string,\n ): Promise<OpenAIOutput<T>> {\n try {\n const completion = await openai.chat.completions.create({\n model,\n messages: [\n { role: \"system\", content: systemMessage },\n { role: \"user\", content: userMessage },\n ],\n functions: [{ name: functionName, parameters: schema }],\n function_call: { name: functionName },\n })\n\n const functionCall = completion.choices[0]?.message.function_call\n if (!functionCall) {\n throw new AiResponseJsonError(\"No function call in OpenAI response\")\n }\n\n const inputTokens = completion.usage?.prompt_tokens ?? 0\n const outputTokens = completion.usage?.completion_tokens ?? 0\n\n try {\n const output = JSON.parse(functionCall.arguments) as T\n return { output, inputTokens, outputTokens }\n } catch (error) {\n if (error instanceof SyntaxError) {\n const msg = error.message.toLowerCase()\n if (\n msg.includes(\"unexpected end of json input\") ||\n msg.includes(\"unterminated string in js\")\n ) {\n throw new AiOutputLengthError(\n `Output tokens exceeded: ${error.message}`,\n { cause: error },\n )\n }\n throw new AiResponseJsonError(\n `Failed to parse JSON from OpenAI response: ${error.message}`,\n { cause: error },\n )\n }\n throw new AiResponseJsonError(\n \"Failed to parse JSON from OpenAI response (unknown reason)\",\n )\n }\n } catch (error) {\n if (error instanceof AiError) throw error\n if (error instanceof OpenAI.APIError) {\n const msg = error.message.toLowerCase()\n if (msg.includes(\"maximum context\")) {\n throw new AiInputLengthError(`Text too long: ${error.message}`, {\n cause: error,\n })\n }\n if (msg.includes(\"invalid schema\")) {\n throw new InvalidJsonSchemaError(`Invalid schema: ${error.message}`, {\n cause: error,\n })\n }\n throw new AiError(`OpenAI error: ${error.message}`, { cause: error })\n }\n throw new AiError(\"OpenAI error (unknown)\", {\n cause: error instanceof Error ? error : undefined,\n })\n }\n },\n }\n}\n","import {\n parseLocationsFromHtml,\n parseLocationsFromText,\n parseLocationsFromUrl,\n} from \"./locations\"\nimport { createOpenAiBackend, type OpenAiBackendConfig } from \"./openai\"\nimport type { GetLocationsResponse } from \"./types\"\n\n/**\n * A configured location-extraction pipeline. Hold onto one of these for the\n * lifetime of your app — it wraps an OpenAI client and a tiktoken encoder.\n */\nexport interface LocationParser {\n parseLocationsFromText(text: string): Promise<GetLocationsResponse>\n parseLocationsFromHtml(html: string): Promise<GetLocationsResponse>\n parseLocationsFromUrl(url: string): Promise<GetLocationsResponse>\n}\n\nexport type LocationParserConfig = OpenAiBackendConfig\n\n/**\n * Create a {@link LocationParser} bound to an OpenAI API key.\n *\n * @example\n * ```ts\n * const parser = createLocationParser({\n * apiKey: process.env.OPENAI_API_KEY!,\n * model: \"gpt-4o-mini\",\n * })\n * const { output } = await parser.parseLocationsFromUrl(\n * \"https://example.com/best-restaurants-tokyo\",\n * )\n * console.log(output.title, output.locations)\n * ```\n */\nexport function createLocationParser(config: LocationParserConfig): LocationParser {\n const backend = createOpenAiBackend(config)\n return {\n parseLocationsFromText: (text) => parseLocationsFromText(backend, text),\n parseLocationsFromHtml: (html) => parseLocationsFromHtml(backend, html),\n parseLocationsFromUrl: (url) => parseLocationsFromUrl(backend, url),\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/types/errors.ts","../../src/ai/errors.ts","../../src/ai/prompts.ts","../../src/scrape/errors.ts","../../src/scrape/html.ts","../../src/scrape/text.ts","../../src/ai/summarize.ts","../../src/ai/locations.ts","../../src/ai/openai.ts","../../src/ai/anthropic.ts","../../src/ai/parser.ts"],"names":["z","convert","encodingForModel","getEncoding","OpenAI","resolveEncoding","Anthropic"],"mappings":";;;;;;;;;;;;;;AAOO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EACpC,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AAAA,EAChB;AACJ,CAAA;;;ACVO,IAAM,OAAA,GAAN,cAAsB,YAAA,CAAa;AAAA,EACtC,WAAA,CAAY,SAAkB,OAAA,EAAwB;AAClD,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,SAAA;AAAA,EAChB;AACJ;AAKO,IAAM,qBAAA,GAAN,cAAoC,OAAA,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AAA,EAAwB,CAAC,IAAA,KAAS,sBAAA,CAAuB,SAAS,IAAI,CAAA;AAAA,IACtE,qBAAA,EAAuB,CAAC,GAAA,KAAQ,qBAAA,CAAsB,SAAS,GAAG;AAAA,GACtE;AACJ","file":"index.cjs","sourcesContent":["/**\n * Base error class for every error thrown by the mapthis package.\n *\n * All feature-specific error classes (scrape, search, ai, geocoding) extend\n * this, so consumers can do `catch (e) { if (e instanceof MapthisError) ... }`\n * to distinguish library errors from other exceptions.\n */\nexport class MapthisError extends Error {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"MapthisError\"\n }\n}\n","import { MapthisError } from \"../types/errors\"\n\nexport class AiError extends MapthisError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiError\"\n }\n}\n\n/**\n * The LLM returned zero locations after deduplication.\n */\nexport class NoLocationsFoundError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message ?? \"No locations found in input\", options)\n this.name = \"NoLocationsFoundError\"\n }\n}\n\n/**\n * Summarization pipeline failed (one of the chunk calls rejected).\n */\nexport class SummarizeTextError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"SummarizeTextError\"\n }\n}\n\n/**\n * A JSON schema passed to the LLM was rejected as invalid by the provider.\n */\nexport class InvalidJsonSchemaError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"InvalidJsonSchemaError\"\n }\n}\n\n/**\n * The LLM response could not be parsed as JSON (typically because the output\n * was truncated or malformed).\n */\nexport class AiResponseJsonError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiResponseJsonError\"\n }\n}\n\n/**\n * Input text exceeded the model's context window.\n */\nexport class AiInputLengthError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiInputLengthError\"\n }\n}\n\n/**\n * The LLM hit the output token limit before completing its JSON response.\n */\nexport class AiOutputLengthError extends AiError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"AiOutputLengthError\"\n }\n}\n","import type { JSONSchema7Object } from \"json-schema\"\n\n/**\n * Semantic version of the location-extraction system prompt. Stored alongside\n * each place record in the private app so results can be re-run when the\n * prompt is updated. Bump when you change {@link LOCATIONS_SYSTEM_MESSAGE} or\n * {@link LOCATIONS_SCHEMA} in a way that meaningfully changes output.\n */\nexport const LOCATIONS_PROMPT_VERSION = \"0.0.1\"\n\nexport const LOCATIONS_FUNCTIONS_NAME = \"get_places\"\n\nexport const LOCATIONS_SYSTEM_MESSAGE = `You parse a list of place addresses, corresponding brief one-sentence descriptions, and a descriptive title (preferably directly quoted) from text. These addresses might be countries, cities, attractions, parks, bars, etc.\nIf the text seems to be listing places that are all within a single parent place, you don't return the parent place, but you do add it to all the childrens' addresses.\nFor example, for an article about places in greece, you would not return \"greece\", but if the Acropolis was mentioned, you would return \"Acropolis, Athens, Greece\"\n\nYou ignore extraneous text, such as recommended articles, advertisments, etc.\n`\n\nexport const LOCATIONS_SCHEMA: JSONSchema7Object = {\n type: \"object\",\n required: [\"locations\", \"title\"],\n properties: {\n locations: {\n type: \"array\",\n items: {\n type: \"object\",\n required: [\"address\", \"description\"],\n properties: {\n address: { type: \"string\" },\n description: { type: \"string\" },\n },\n },\n },\n title: { type: \"string\" },\n },\n}\n\nexport const LOCATIONS_MAX_OUTPUT_TOKENS = 500\n\n/**\n * Semantic version of the summarization prompt. Bump when meaningful changes\n * are made to {@link SUMMARIZATION_PROMPT}.\n */\nexport const SUMMARIZE_PROMPT_VERSION = \"0.0.1\"\n\nexport const SUMMARIZATION_PROMPT = `Summarize the given text. The summary must reduce the number of characters from the original text by at least a factor of {RATIO}.\nFor example, text of 1000 characters must output a summary less than {EXAMPLE} characters.\nThis summary will be used to extract a list of locations and addresses, so keep any text that seems to be a list of locations/places, a location and paragraph describing it, or otherwise clearly a location name or address.\nHere is the text:\n`\n","import { MapthisError } from \"../types/errors\"\n\nexport class ScrapeError extends MapthisError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"ScrapeError\"\n }\n}\n\n/**\n * Thrown when a provided URL fails validation or cannot be parsed by `fetch`.\n */\nexport class InvalidUrlError extends ScrapeError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"InvalidUrlError\"\n }\n}\n\n/**\n * Thrown when a remote server returns 401 or 403 for an HTML fetch, meaning\n * the page does not permit anonymous scraping.\n */\nexport class HtmlUnauthorizedError extends ScrapeError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"HtmlUnauthorizedError\"\n }\n}\n\n/**\n * Thrown when the underlying `html-to-text` conversion fails.\n */\nexport class HtmlToTextError extends ScrapeError {\n constructor(message?: string, options?: ErrorOptions) {\n super(message, options)\n this.name = \"HtmlToTextError\"\n }\n}\n","import { z } from \"zod\"\nimport { HtmlUnauthorizedError, InvalidUrlError, ScrapeError } from \"./errors\"\n\nconst withHttps = (url: string): string => {\n const hasHttp = /^http?:\\/\\//i.test(url)\n const hasHttps = /^https?:\\/\\//i.test(url)\n if (!hasHttp && !hasHttps) return `https://${url}`\n return url\n}\n\n/**\n * Fetch the raw HTML of a URL, performing light validation first.\n *\n * - URLs without a scheme are prefixed with `https://`.\n * - URL shape is validated via Zod before the network call.\n * - 401/403 responses throw {@link HtmlUnauthorizedError}.\n * - Other non-OK responses throw {@link ScrapeError} with the status text.\n * - `TypeError` from `fetch` (typically malformed URL) is rethrown as\n * {@link InvalidUrlError}.\n *\n * This function does not set any special headers, user agent, or cookies.\n * Sites with aggressive bot-detection may return empty bodies or errors; the\n * caller is responsible for retry / user-agent rotation strategies if needed.\n */\nexport async function getHtmlFromUrl(url: string): Promise<string> {\n const urlFixed = withHttps(url.toLowerCase())\n const isUrl = z.string().url().safeParse(urlFixed)\n if (!isUrl.success) throw new InvalidUrlError(`Invalid URL: ${url}`)\n\n try {\n const response = await fetch(urlFixed)\n const html = await response.text()\n if (response.status === 403 || response.status === 401) {\n throw new HtmlUnauthorizedError(\n `${response.status} Not authorized to scrape this website`,\n )\n }\n if (!response.ok) {\n throw new ScrapeError(\n `Bad response when getting HTML. Status: ${response.status} ${response.statusText}`,\n )\n }\n return html\n } catch (error) {\n if (error instanceof TypeError) {\n throw new InvalidUrlError(`Invalid URL. Original error: ${error.message}`)\n }\n throw error\n }\n}\n","import { convert } from \"html-to-text\"\nimport { HtmlToTextError } from \"./errors\"\nimport { getHtmlFromUrl } from \"./html\"\n\nconst SKIP_SELECTORS = [\n \"img\",\n \"header\",\n \"footer\",\n \"audio\",\n \"button\",\n \"canvas\",\n \"code\",\n \"nav\",\n \"#nav\",\n \"figure\",\n \"figcaption\",\n \".comment\",\n \".comments\",\n \"#comments\",\n \"#related-posts\",\n \"#related\",\n \".related\",\n \".related-posts\",\n] as const\n\n/**\n * Convert raw HTML to plain text suitable for LLM consumption.\n *\n * The converter is configured to:\n * - strip anchor hrefs when they duplicate the link text;\n * - skip navigation, footer, comments, related-post widgets, images, and\n * other non-content elements;\n * - collapse repeated blank lines and double spaces so token count doesn't\n * balloon from whitespace.\n */\nexport function htmlToText(html: string): string {\n try {\n let out = convert(html, {\n wordwrap: false,\n selectors: [\n {\n selector: \"a\",\n options: {\n ignoreHref: true,\n hideLinkHrefIfSameAsText: true,\n },\n },\n ...SKIP_SELECTORS.map((tag) => ({ selector: tag, format: \"skip\" })),\n ],\n })\n out = out.replaceAll(\"\\r\\n\", \"\\n\")\n // Iteratively collapse blank lines and double spaces. 20 passes is\n // overkill for any realistic input but cheap and bounds worst case.\n for (let i = 0; i < 20; i++) {\n out = out.replaceAll(\"\\n\\n\\n\", \"\\n\\n\")\n out = out.replaceAll(\" \", \" \")\n }\n return out\n } catch (error) {\n if (error instanceof Error) {\n throw new HtmlToTextError(`HTML to text conversion failed: ${error.message}`, {\n cause: error,\n })\n }\n throw new HtmlToTextError(\"HTML to text conversion failed\")\n }\n}\n\n/**\n * Convenience: fetch a URL and convert its HTML to plain text in one call.\n *\n * Equivalent to `htmlToText(await getHtmlFromUrl(url))`.\n */\nexport async function getTextFromUrl(url: string): Promise<string> {\n const html = await getHtmlFromUrl(url)\n return htmlToText(html)\n}\n","import type { LlmBackend } from \"./backend\"\nimport { SummarizeTextError } from \"./errors\"\nimport { SUMMARIZATION_PROMPT, SUMMARIZE_PROMPT_VERSION } from \"./prompts\"\nimport type { SummarizedText } from \"./types\"\n\n/**\n * Reduce a large text to a summary that will fit inside the model's context\n * window alongside a known downstream prompt (e.g. the location-extraction\n * call).\n *\n * Algorithm overview\n * ------------------\n * We need to fit both the summary and a downstream \"final\" prompt into the\n * model's context window. Given:\n *\n * - `token_limit` — model context window (with a safety factor)\n * - `text_tokens` — size of the input text\n * - `summarize_prompt_tokens` — size of the summarization prompt itself\n * - `final_prompt_tokens` — size of the downstream prompt\n * - `final_output_tokens` — expected size of the downstream response\n *\n * We split the input into chunks, summarize each in parallel, and concatenate\n * the results. The per-chunk size is chosen so that the combined summary\n * leaves room for the downstream prompt:\n *\n * final_input_tokens = token_limit - final_output_tokens\n * summary_output_tokens = final_input_tokens - final_prompt_tokens\n * num_chunks = ceil((text_tokens + summary_output_tokens)\n * / (token_limit - summarize_prompt_tokens))\n * chunk_output_tokens = summary_output_tokens / num_chunks\n * chunk_input_tokens = token_limit - summarize_prompt_tokens - chunk_output_tokens\n *\n * A safety factor is applied to chunk output sizes because LLMs routinely\n * overshoot explicit length instructions.\n */\nexport async function summarizeText(\n backend: LlmBackend,\n text: string,\n finalPrompt = \"\",\n finalOutputTokens = 0,\n chunkOutputSafetyFactor = 0.3,\n tokenLimitSafetyFactor = 0.02,\n): Promise<SummarizedText> {\n const tokenLimit = backend.tokenLimit * (1 - tokenLimitSafetyFactor)\n const textTokens = backend.countTokens(text)\n const summarizePromptTokens = backend.countTokens(SUMMARIZATION_PROMPT)\n const finalPromptTokens = backend.countTokens(finalPrompt)\n\n const finalInputTokens = tokenLimit - finalOutputTokens\n const summaryOutputTokens = finalInputTokens - finalPromptTokens\n\n const numChunks = Math.max(\n 1,\n Math.ceil((textTokens + summaryOutputTokens) / (tokenLimit - summarizePromptTokens)),\n )\n const chunkOutputTokens = Math.floor(\n (summaryOutputTokens / numChunks) * (1 - chunkOutputSafetyFactor),\n )\n const chunkInputTokens = Math.floor(tokenLimit - summarizePromptTokens - chunkOutputTokens)\n const summaryRatio = chunkOutputTokens / chunkInputTokens\n\n // Split the text into chunks that each fit inside `chunkInputTokens`.\n // Lines longer than a single chunk are force-split by token, not by\n // character, so UTF-8 multibyte characters aren't corrupted.\n const textLines = text.split(\"\\n\")\n const chunks: string[] = []\n\n let currentChunk = \"\"\n let currentChunkTokens = 0\n const nextChunk = (): void => {\n if (currentChunk) chunks.push(currentChunk)\n currentChunk = \"\"\n currentChunkTokens = 0\n }\n\n // We need an encoder for the force-split path. The backend's countTokens\n // gives us length but not the encoded array; for force-splitting we only\n // need to chunk by an approximate token count using the same rounding.\n // To keep this function backend-agnostic, we fall back to character-based\n // splitting for over-long lines (rare edge case in practice).\n for (const line of textLines) {\n const numLineTokens = backend.countTokens(line) + 1 // +1 for the newline\n\n if (numLineTokens > chunkInputTokens) {\n // Long line: flush current chunk, then split this line by\n // approximate token ratio into sub-chunks.\n nextChunk()\n const charsPerToken = line.length / Math.max(1, backend.countTokens(line))\n const charsPerChunk = Math.max(1, Math.floor(chunkInputTokens * charsPerToken))\n for (let i = 0; i < line.length; i += charsPerChunk) {\n chunks.push(line.slice(i, i + charsPerChunk))\n }\n continue\n }\n\n if (currentChunkTokens + numLineTokens > chunkInputTokens) {\n nextChunk()\n }\n currentChunkTokens += numLineTokens\n currentChunk += line + \"\\n\"\n }\n nextChunk()\n\n try {\n const ratioText = (Math.ceil(summaryRatio * 10) / 10).toFixed(1)\n const exampleText = Math.ceil(1000 / Math.max(summaryRatio, 0.01)).toFixed(0)\n const summaryPrompt = SUMMARIZATION_PROMPT.replace(\"{RATIO}\", ratioText).replace(\n \"{EXAMPLE}\",\n exampleText,\n )\n\n let inputTokens = 0\n let outputTokens = 0\n const summaries = await Promise.all(\n chunks.map(async (chunk) => {\n const response = await backend.chatCompletion(summaryPrompt + chunk)\n inputTokens += response.inputTokens\n outputTokens += response.outputTokens\n return response.output\n }),\n )\n\n const summarized = summaries.join(\"\\n\")\n const summarizedTokens = backend.countTokens(summarized)\n\n return {\n text,\n textTokens,\n summarized,\n summarizedTokens,\n inputTokens,\n outputTokens,\n chunks: chunks.length,\n summarizePromptVersion: SUMMARIZE_PROMPT_VERSION,\n }\n } catch (error) {\n if (error instanceof Error) {\n throw new SummarizeTextError(`Failed to summarize text: ${error.message}`, {\n cause: error,\n })\n }\n throw new SummarizeTextError(\"Failed to summarize text (unknown error)\")\n }\n}\n","import { getHtmlFromUrl } from \"../scrape/html\"\nimport { htmlToText } from \"../scrape/text\"\nimport type { ParsedLocations } from \"../types/domain\"\nimport type { LlmBackend } from \"./backend\"\nimport { NoLocationsFoundError } from \"./errors\"\nimport {\n LOCATIONS_FUNCTIONS_NAME,\n LOCATIONS_MAX_OUTPUT_TOKENS,\n LOCATIONS_PROMPT_VERSION,\n LOCATIONS_SCHEMA,\n LOCATIONS_SYSTEM_MESSAGE,\n} from \"./prompts\"\nimport { summarizeText } from \"./summarize\"\nimport type { GetLocationsResponse } from \"./types\"\n\nconst FINAL_PROMPT = LOCATIONS_SYSTEM_MESSAGE + JSON.stringify(LOCATIONS_SCHEMA) + LOCATIONS_FUNCTIONS_NAME\n\nasync function callLocationExtraction(backend: LlmBackend, text: string) {\n const out = await backend.chatFunctionJson<ParsedLocations>(\n LOCATIONS_SYSTEM_MESSAGE,\n text,\n LOCATIONS_SCHEMA,\n LOCATIONS_FUNCTIONS_NAME,\n )\n\n // LLM frequently returns the same address twice across different prompts.\n // Dedupe by address string.\n const seen = new Set<string>()\n const unique = out.output.locations.filter((loc) => {\n if (seen.has(loc.address)) return false\n seen.add(loc.address)\n return true\n })\n if (unique.length === 0) throw new NoLocationsFoundError()\n out.output.locations = unique\n\n return out\n}\n\n/**\n * Parse locations from freeform text. Two-stage: summarize to fit the context\n * window, then a function-calling JSON completion for extraction.\n */\nexport async function parseLocationsFromText(\n backend: LlmBackend,\n text: string,\n): Promise<GetLocationsResponse> {\n const {\n summarized,\n inputTokens: sumIn,\n outputTokens: sumOut,\n textTokens,\n summarizedTokens,\n summarizePromptVersion,\n } = await summarizeText(backend, text, FINAL_PROMPT, LOCATIONS_MAX_OUTPUT_TOKENS)\n\n const {\n output,\n inputTokens: locIn,\n outputTokens: locOut,\n } = await callLocationExtraction(backend, summarized)\n\n return {\n output,\n locationInputTokens: locIn,\n locationOutputTokens: locOut,\n summaryInputTokens: sumIn,\n summaryOutputTokens: sumOut,\n inputTokens: sumIn + locIn,\n outputTokens: sumOut + locOut,\n textTokens,\n summarizedTokens,\n summaryPromptVersion: summarizePromptVersion,\n locationPromptVersion: LOCATIONS_PROMPT_VERSION,\n }\n}\n\n/**\n * Parse locations from HTML. Equivalent to `htmlToText` followed by\n * {@link parseLocationsFromText}.\n */\nexport async function parseLocationsFromHtml(\n backend: LlmBackend,\n html: string,\n): Promise<GetLocationsResponse> {\n return parseLocationsFromText(backend, htmlToText(html))\n}\n\n/**\n * Parse locations from a URL. Equivalent to fetching the HTML, converting to\n * text, and calling {@link parseLocationsFromText}. Throws any scrape errors\n * unchanged.\n */\nexport async function parseLocationsFromUrl(\n backend: LlmBackend,\n url: string,\n): Promise<GetLocationsResponse> {\n const html = await getHtmlFromUrl(url)\n return parseLocationsFromHtml(backend, html)\n}\n","import { encodingForModel, getEncoding, type Tiktoken, type TiktokenModel } from \"js-tiktoken\"\nimport OpenAI from \"openai\"\nimport type { JSONSchema7Object } from \"json-schema\"\nimport {\n AiInputLengthError,\n AiOutputLengthError,\n AiResponseJsonError,\n AiError,\n InvalidJsonSchemaError,\n} from \"./errors\"\nimport type { LlmBackend } from \"./backend\"\nimport type { OpenAIOutput } from \"./types\"\n\nexport const DEFAULT_MODEL = \"gpt-4o-mini\"\nexport const DEFAULT_TOKEN_LIMIT = 128000\n\n/**\n * @deprecated Use {@link LlmBackend} instead. Kept as an alias so existing\n * consumers that import `OpenAiBackend` keep compiling.\n */\nexport type OpenAiBackend = LlmBackend\n\nexport interface OpenAiBackendConfig {\n /** OpenAI API key. Passed directly to the SDK. */\n apiKey: string\n /** Model name passed to the completions API. Defaults to `gpt-4o-mini`. */\n model?: string\n /** Context-window budget used by the summarizer. Defaults to 128k. */\n tokenLimit?: number\n /**\n * Optional base URL for the OpenAI API. Use this to route through an\n * OpenAI-compatible gateway (Azure, Together, etc.).\n */\n baseURL?: string\n}\n\nfunction resolveEncoding(model: string): Tiktoken {\n try {\n return encodingForModel(model as TiktokenModel)\n } catch {\n // Fallback for models tiktoken does not recognize by name.\n return getEncoding(\"cl100k_base\")\n }\n}\n\n/**\n * Construct an {@link LlmBackend} bound to a specific OpenAI API key and model.\n */\nexport function createOpenAiBackend(config: OpenAiBackendConfig): LlmBackend {\n const model = config.model ?? DEFAULT_MODEL\n const tokenLimit = config.tokenLimit ?? DEFAULT_TOKEN_LIMIT\n const openai = new OpenAI({\n apiKey: config.apiKey,\n ...(config.baseURL ? { baseURL: config.baseURL } : {}),\n })\n const encoding = resolveEncoding(model)\n\n return {\n model,\n tokenLimit,\n\n countTokens(text: string): number {\n return encoding.encode(text).length\n },\n\n async chatCompletion(content: string): Promise<OpenAIOutput<string>> {\n const response = await openai.chat.completions.create({\n messages: [{ role: \"user\", content }],\n model,\n })\n return {\n output: response.choices[0]?.message.content ?? \"\",\n inputTokens: response.usage?.prompt_tokens ?? 0,\n outputTokens: response.usage?.completion_tokens ?? 0,\n }\n },\n\n async chatFunctionJson<T>(\n systemMessage: string,\n userMessage: string,\n schema: JSONSchema7Object,\n functionName: string,\n ): Promise<OpenAIOutput<T>> {\n try {\n const completion = await openai.chat.completions.create({\n model,\n messages: [\n { role: \"system\", content: systemMessage },\n { role: \"user\", content: userMessage },\n ],\n functions: [{ name: functionName, parameters: schema }],\n function_call: { name: functionName },\n })\n\n const functionCall = completion.choices[0]?.message.function_call\n if (!functionCall) {\n throw new AiResponseJsonError(\"No function call in OpenAI response\")\n }\n\n const inputTokens = completion.usage?.prompt_tokens ?? 0\n const outputTokens = completion.usage?.completion_tokens ?? 0\n\n try {\n const output = JSON.parse(functionCall.arguments) as T\n return { output, inputTokens, outputTokens }\n } catch (error) {\n if (error instanceof SyntaxError) {\n const msg = error.message.toLowerCase()\n if (\n msg.includes(\"unexpected end of json input\") ||\n msg.includes(\"unterminated string in js\")\n ) {\n throw new AiOutputLengthError(\n `Output tokens exceeded: ${error.message}`,\n { cause: error },\n )\n }\n throw new AiResponseJsonError(\n `Failed to parse JSON from OpenAI response: ${error.message}`,\n { cause: error },\n )\n }\n throw new AiResponseJsonError(\n \"Failed to parse JSON from OpenAI response (unknown reason)\",\n )\n }\n } catch (error) {\n if (error instanceof AiError) throw error\n if (error instanceof OpenAI.APIError) {\n const msg = error.message.toLowerCase()\n if (msg.includes(\"maximum context\")) {\n throw new AiInputLengthError(`Text too long: ${error.message}`, {\n cause: error,\n })\n }\n if (msg.includes(\"invalid schema\")) {\n throw new InvalidJsonSchemaError(`Invalid schema: ${error.message}`, {\n cause: error,\n })\n }\n throw new AiError(`OpenAI error: ${error.message}`, { cause: error })\n }\n throw new AiError(\"OpenAI error (unknown)\", {\n cause: error instanceof Error ? error : undefined,\n })\n }\n },\n }\n}\n","import Anthropic from \"@anthropic-ai/sdk\"\nimport { encodingForModel, getEncoding, type Tiktoken, type TiktokenModel } from \"js-tiktoken\"\nimport type { JSONSchema7Object } from \"json-schema\"\nimport {\n AiError,\n AiInputLengthError,\n AiOutputLengthError,\n AiResponseJsonError,\n InvalidJsonSchemaError,\n} from \"./errors\"\nimport type { LlmBackend } from \"./backend\"\nimport type { OpenAIOutput } from \"./types\"\n\nexport const DEFAULT_ANTHROPIC_MODEL = \"claude-haiku-4-5-20251001\"\nexport const DEFAULT_ANTHROPIC_TOKEN_LIMIT = 200_000\nexport const DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS = 4096\n\nexport interface AnthropicBackendConfig {\n /** Anthropic API key. Passed directly to the SDK. */\n apiKey: string\n /** Model name. Defaults to {@link DEFAULT_ANTHROPIC_MODEL}. */\n model?: string\n /** Context-window budget used by the summarizer. Defaults to 200k. */\n tokenLimit?: number\n /** Optional override for max_tokens on each request. Defaults to 4096. */\n maxOutputTokens?: number\n /**\n * Optional base URL for an Anthropic-compatible gateway (Bedrock, Vertex\n * proxy, etc.).\n */\n baseURL?: string\n}\n\n/**\n * Token counting fallback. The Anthropic SDK exposes a remote\n * `messages.countTokens` endpoint, but the summarizer calls `countTokens`\n * thousands of times during chunking — making it a network hop would be\n * prohibitive. We approximate with cl100k_base, which matches GPT-4-class\n * tokenizers closely enough for chunk sizing (which already has a 30%\n * safety factor applied).\n */\nfunction resolveEncoding(model: string): Tiktoken {\n try {\n return encodingForModel(model as TiktokenModel)\n } catch {\n return getEncoding(\"cl100k_base\")\n }\n}\n\n/**\n * Construct an {@link LlmBackend} backed by Anthropic's Messages API. Use this\n * when wiring up {@link createLocationParser} or as a drop-in replacement for\n * {@link createOpenAiBackend} in any consumer that holds the interface.\n */\nexport function createAnthropicBackend(config: AnthropicBackendConfig): LlmBackend {\n const model = config.model ?? DEFAULT_ANTHROPIC_MODEL\n const tokenLimit = config.tokenLimit ?? DEFAULT_ANTHROPIC_TOKEN_LIMIT\n const maxOutputTokens = config.maxOutputTokens ?? DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS\n const client = new Anthropic({\n apiKey: config.apiKey,\n ...(config.baseURL ? { baseURL: config.baseURL } : {}),\n })\n const encoding = resolveEncoding(model)\n\n return {\n model,\n tokenLimit,\n\n countTokens(text: string): number {\n return encoding.encode(text).length\n },\n\n async chatCompletion(content: string): Promise<OpenAIOutput<string>> {\n const response = await client.messages.create({\n model,\n max_tokens: maxOutputTokens,\n messages: [{ role: \"user\", content }],\n })\n const text = response.content\n .filter((b): b is Anthropic.TextBlock => b.type === \"text\")\n .map((b) => b.text)\n .join(\"\")\n return {\n output: text,\n inputTokens: response.usage.input_tokens,\n outputTokens: response.usage.output_tokens,\n }\n },\n\n async chatFunctionJson<T>(\n systemMessage: string,\n userMessage: string,\n schema: JSONSchema7Object,\n functionName: string,\n ): Promise<OpenAIOutput<T>> {\n try {\n const response = await client.messages.create({\n model,\n max_tokens: maxOutputTokens,\n system: systemMessage,\n tools: [\n {\n name: functionName,\n description: `Return structured output matching the ${functionName} schema.`,\n input_schema: schema as Anthropic.Tool.InputSchema,\n },\n ],\n tool_choice: { type: \"tool\", name: functionName },\n messages: [{ role: \"user\", content: userMessage }],\n })\n\n const toolUse = response.content.find(\n (b): b is Anthropic.ToolUseBlock => b.type === \"tool_use\",\n )\n if (!toolUse) {\n throw new AiResponseJsonError(\"No tool use in Anthropic response\")\n }\n if (response.stop_reason === \"max_tokens\") {\n throw new AiOutputLengthError(\n `Anthropic hit max_tokens (${maxOutputTokens}) before completing tool call`,\n )\n }\n\n return {\n output: toolUse.input as T,\n inputTokens: response.usage.input_tokens,\n outputTokens: response.usage.output_tokens,\n }\n } catch (error) {\n if (error instanceof AiError) throw error\n if (error instanceof Anthropic.APIError) {\n const msg = (error.message ?? \"\").toLowerCase()\n if (msg.includes(\"prompt is too long\") || msg.includes(\"max_tokens\")) {\n throw new AiInputLengthError(`Text too long: ${error.message}`, {\n cause: error,\n })\n }\n if (msg.includes(\"invalid\") && msg.includes(\"schema\")) {\n throw new InvalidJsonSchemaError(`Invalid schema: ${error.message}`, {\n cause: error,\n })\n }\n throw new AiError(`Anthropic error: ${error.message}`, { cause: error })\n }\n throw new AiError(\"Anthropic error (unknown)\", {\n cause: error instanceof Error ? error : undefined,\n })\n }\n },\n }\n}\n","import type { LlmBackend } from \"./backend\"\nimport {\n parseLocationsFromHtml,\n parseLocationsFromText,\n parseLocationsFromUrl,\n} from \"./locations\"\nimport { createOpenAiBackend, type OpenAiBackendConfig } from \"./openai\"\nimport { createAnthropicBackend, type AnthropicBackendConfig } from \"./anthropic\"\nimport type { GetLocationsResponse } from \"./types\"\n\n/**\n * A configured location-extraction pipeline. Hold onto one of these for the\n * lifetime of your app — it wraps an LLM client and a tiktoken encoder.\n */\nexport interface LocationParser {\n parseLocationsFromText(text: string): Promise<GetLocationsResponse>\n parseLocationsFromHtml(html: string): Promise<GetLocationsResponse>\n parseLocationsFromUrl(url: string): Promise<GetLocationsResponse>\n}\n\n/**\n * Configuration for {@link createLocationParser}.\n *\n * - `{ provider: \"openai\", apiKey, ... }` — uses OpenAI's chat completions API\n * with function-calling for structured output (default if omitted).\n * - `{ provider: \"anthropic\", apiKey, ... }` — uses Anthropic's Messages API\n * with tool-use for structured output.\n * - `{ backend }` — pass any object satisfying {@link LlmBackend} directly,\n * useful for testing or for adapters that aren't bundled here.\n *\n * For backwards compatibility a bare {@link OpenAiBackendConfig} (no\n * `provider` field) is treated as `{ provider: \"openai\", ... }`.\n */\nexport type LocationParserConfig =\n | ({ provider?: \"openai\" } & OpenAiBackendConfig)\n | ({ provider: \"anthropic\" } & AnthropicBackendConfig)\n | { backend: LlmBackend }\n\nfunction resolveBackend(config: LocationParserConfig): LlmBackend {\n if (\"backend\" in config) return config.backend\n if (config.provider === \"anthropic\") return createAnthropicBackend(config)\n return createOpenAiBackend(config)\n}\n\n/**\n * Create a {@link LocationParser} bound to an LLM backend.\n *\n * @example OpenAI (default)\n * ```ts\n * const parser = createLocationParser({\n * apiKey: process.env.OPENAI_API_KEY!,\n * model: \"gpt-4o-mini\",\n * })\n * ```\n *\n * @example Anthropic\n * ```ts\n * const parser = createLocationParser({\n * provider: \"anthropic\",\n * apiKey: process.env.ANTHROPIC_API_KEY!,\n * model: \"claude-haiku-4-5-20251001\",\n * })\n * ```\n *\n * @example Bring-your-own backend\n * ```ts\n * const parser = createLocationParser({ backend: myCustomBackend })\n * ```\n */\nexport function createLocationParser(config: LocationParserConfig): LocationParser {\n const backend = resolveBackend(config)\n return {\n parseLocationsFromText: (text) => parseLocationsFromText(backend, text),\n parseLocationsFromHtml: (html) => parseLocationsFromHtml(backend, html),\n parseLocationsFromUrl: (url) => parseLocationsFromUrl(backend, url),\n }\n}\n"]}
|
package/dist/ai/index.d.cts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { M as MapthisError } from '../errors-Bw97z_4m.cjs';
|
|
2
|
-
import {
|
|
3
|
-
export { D as DEFAULT_MODEL,
|
|
2
|
+
import { a as LlmBackend, S as SummarizedText, G as GetLocationsResponse } from '../parser-DsIwIsHK.cjs';
|
|
3
|
+
export { A as AnthropicBackendConfig, D as DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS, b as DEFAULT_ANTHROPIC_MODEL, c as DEFAULT_ANTHROPIC_TOKEN_LIMIT, d as DEFAULT_MODEL, e as DEFAULT_TOKEN_LIMIT, f as GenericAIJsonRequest, L as LocationParser, g as LocationParserConfig, O as OpenAIOutput, h as OpenAiBackend, i as OpenAiBackendConfig, j as createAnthropicBackend, k as createLocationParser, l as createOpenAiBackend } from '../parser-DsIwIsHK.cjs';
|
|
4
4
|
import { JSONSchema7Object } from 'json-schema';
|
|
5
|
-
import '../domain-
|
|
6
|
-
import '../schemas-
|
|
5
|
+
import '../domain-CL4ro2YW.cjs';
|
|
6
|
+
import '../schemas-CZao45EU.cjs';
|
|
7
7
|
import 'zod';
|
|
8
8
|
|
|
9
9
|
declare class AiError extends MapthisError {
|
|
@@ -95,23 +95,23 @@ declare const SUMMARIZATION_PROMPT = "Summarize the given text. The summary must
|
|
|
95
95
|
* A safety factor is applied to chunk output sizes because LLMs routinely
|
|
96
96
|
* overshoot explicit length instructions.
|
|
97
97
|
*/
|
|
98
|
-
declare function summarizeText(backend:
|
|
98
|
+
declare function summarizeText(backend: LlmBackend, text: string, finalPrompt?: string, finalOutputTokens?: number, chunkOutputSafetyFactor?: number, tokenLimitSafetyFactor?: number): Promise<SummarizedText>;
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
101
|
* Parse locations from freeform text. Two-stage: summarize to fit the context
|
|
102
102
|
* window, then a function-calling JSON completion for extraction.
|
|
103
103
|
*/
|
|
104
|
-
declare function parseLocationsFromText(backend:
|
|
104
|
+
declare function parseLocationsFromText(backend: LlmBackend, text: string): Promise<GetLocationsResponse>;
|
|
105
105
|
/**
|
|
106
106
|
* Parse locations from HTML. Equivalent to `htmlToText` followed by
|
|
107
107
|
* {@link parseLocationsFromText}.
|
|
108
108
|
*/
|
|
109
|
-
declare function parseLocationsFromHtml(backend:
|
|
109
|
+
declare function parseLocationsFromHtml(backend: LlmBackend, html: string): Promise<GetLocationsResponse>;
|
|
110
110
|
/**
|
|
111
111
|
* Parse locations from a URL. Equivalent to fetching the HTML, converting to
|
|
112
112
|
* text, and calling {@link parseLocationsFromText}. Throws any scrape errors
|
|
113
113
|
* unchanged.
|
|
114
114
|
*/
|
|
115
|
-
declare function parseLocationsFromUrl(backend:
|
|
115
|
+
declare function parseLocationsFromUrl(backend: LlmBackend, url: string): Promise<GetLocationsResponse>;
|
|
116
116
|
|
|
117
|
-
export { AiError, AiInputLengthError, AiOutputLengthError, AiResponseJsonError, GetLocationsResponse, InvalidJsonSchemaError, LOCATIONS_FUNCTIONS_NAME, LOCATIONS_MAX_OUTPUT_TOKENS, LOCATIONS_PROMPT_VERSION, LOCATIONS_SCHEMA, LOCATIONS_SYSTEM_MESSAGE,
|
|
117
|
+
export { AiError, AiInputLengthError, AiOutputLengthError, AiResponseJsonError, GetLocationsResponse, InvalidJsonSchemaError, LOCATIONS_FUNCTIONS_NAME, LOCATIONS_MAX_OUTPUT_TOKENS, LOCATIONS_PROMPT_VERSION, LOCATIONS_SCHEMA, LOCATIONS_SYSTEM_MESSAGE, LlmBackend, NoLocationsFoundError, SUMMARIZATION_PROMPT, SUMMARIZE_PROMPT_VERSION, SummarizeTextError, SummarizedText, parseLocationsFromHtml, parseLocationsFromText, parseLocationsFromUrl, summarizeText };
|
package/dist/ai/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { M as MapthisError } from '../errors-Bw97z_4m.js';
|
|
2
|
-
import {
|
|
3
|
-
export { D as DEFAULT_MODEL,
|
|
2
|
+
import { a as LlmBackend, S as SummarizedText, G as GetLocationsResponse } from '../parser-CrG1A6dD.js';
|
|
3
|
+
export { A as AnthropicBackendConfig, D as DEFAULT_ANTHROPIC_MAX_OUTPUT_TOKENS, b as DEFAULT_ANTHROPIC_MODEL, c as DEFAULT_ANTHROPIC_TOKEN_LIMIT, d as DEFAULT_MODEL, e as DEFAULT_TOKEN_LIMIT, f as GenericAIJsonRequest, L as LocationParser, g as LocationParserConfig, O as OpenAIOutput, h as OpenAiBackend, i as OpenAiBackendConfig, j as createAnthropicBackend, k as createLocationParser, l as createOpenAiBackend } from '../parser-CrG1A6dD.js';
|
|
4
4
|
import { JSONSchema7Object } from 'json-schema';
|
|
5
|
-
import '../domain-
|
|
6
|
-
import '../schemas-
|
|
5
|
+
import '../domain-Dh8cPBVp.js';
|
|
6
|
+
import '../schemas-CZao45EU.js';
|
|
7
7
|
import 'zod';
|
|
8
8
|
|
|
9
9
|
declare class AiError extends MapthisError {
|
|
@@ -95,23 +95,23 @@ declare const SUMMARIZATION_PROMPT = "Summarize the given text. The summary must
|
|
|
95
95
|
* A safety factor is applied to chunk output sizes because LLMs routinely
|
|
96
96
|
* overshoot explicit length instructions.
|
|
97
97
|
*/
|
|
98
|
-
declare function summarizeText(backend:
|
|
98
|
+
declare function summarizeText(backend: LlmBackend, text: string, finalPrompt?: string, finalOutputTokens?: number, chunkOutputSafetyFactor?: number, tokenLimitSafetyFactor?: number): Promise<SummarizedText>;
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
101
|
* Parse locations from freeform text. Two-stage: summarize to fit the context
|
|
102
102
|
* window, then a function-calling JSON completion for extraction.
|
|
103
103
|
*/
|
|
104
|
-
declare function parseLocationsFromText(backend:
|
|
104
|
+
declare function parseLocationsFromText(backend: LlmBackend, text: string): Promise<GetLocationsResponse>;
|
|
105
105
|
/**
|
|
106
106
|
* Parse locations from HTML. Equivalent to `htmlToText` followed by
|
|
107
107
|
* {@link parseLocationsFromText}.
|
|
108
108
|
*/
|
|
109
|
-
declare function parseLocationsFromHtml(backend:
|
|
109
|
+
declare function parseLocationsFromHtml(backend: LlmBackend, html: string): Promise<GetLocationsResponse>;
|
|
110
110
|
/**
|
|
111
111
|
* Parse locations from a URL. Equivalent to fetching the HTML, converting to
|
|
112
112
|
* text, and calling {@link parseLocationsFromText}. Throws any scrape errors
|
|
113
113
|
* unchanged.
|
|
114
114
|
*/
|
|
115
|
-
declare function parseLocationsFromUrl(backend:
|
|
115
|
+
declare function parseLocationsFromUrl(backend: LlmBackend, url: string): Promise<GetLocationsResponse>;
|
|
116
116
|
|
|
117
|
-
export { AiError, AiInputLengthError, AiOutputLengthError, AiResponseJsonError, GetLocationsResponse, InvalidJsonSchemaError, LOCATIONS_FUNCTIONS_NAME, LOCATIONS_MAX_OUTPUT_TOKENS, LOCATIONS_PROMPT_VERSION, LOCATIONS_SCHEMA, LOCATIONS_SYSTEM_MESSAGE,
|
|
117
|
+
export { AiError, AiInputLengthError, AiOutputLengthError, AiResponseJsonError, GetLocationsResponse, InvalidJsonSchemaError, LOCATIONS_FUNCTIONS_NAME, LOCATIONS_MAX_OUTPUT_TOKENS, LOCATIONS_PROMPT_VERSION, LOCATIONS_SCHEMA, LOCATIONS_SYSTEM_MESSAGE, LlmBackend, NoLocationsFoundError, SUMMARIZATION_PROMPT, SUMMARIZE_PROMPT_VERSION, SummarizeTextError, SummarizedText, parseLocationsFromHtml, parseLocationsFromText, parseLocationsFromUrl, summarizeText };
|