librarium-spec 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +64 -2
- package/SPEC.md +505 -0
- package/fixtures/run-store/contract.json +133 -0
- package/fixtures/run-store/filesystem/run_01JAB2C3D4E5F6G7H8J9K0M1N2/answers/exa.md +2 -0
- package/fixtures/run-store/filesystem/run_01JAB2C3D4E5F6G7H8J9K0M1N2/evidence/exa.json +1 -0
- package/fixtures/run-store/filesystem/run_01JAB2C3D4E5F6G7H8J9K0M1N2/run.json +44 -0
- package/fixtures/run-store/filesystem/run_01JAB2C3D4E5F6G7H8J9K0M1N2/state.json +6 -0
- package/fixtures/run-store/stored-run.json +54 -0
- package/fixtures/runs/invalid/bad-id.json +44 -0
- package/fixtures/runs/invalid/bad-summary-of.json +45 -0
- package/fixtures/runs/invalid/failed-result-without-error.json +39 -0
- package/fixtures/runs/invalid/mode-field.json +45 -0
- package/fixtures/runs/invalid/numeric-cost.json +44 -0
- package/fixtures/runs/invalid/running-with-finished-at.json +39 -0
- package/fixtures/runs/valid/deep-running.json +69 -0
- package/fixtures/runs/valid/partial-with-surface.json +68 -0
- package/fixtures/runs/valid/planned-dry-run.json +23 -0
- package/fixtures/runs/valid/quick-done.json +44 -0
- package/fixtures/runs/valid/summary-run.json +45 -0
- package/fixtures/selection/budget-unpriced-and-over-budget.json +43 -0
- package/fixtures/selection/budget-without-cap-keeps-unpriced.json +29 -0
- package/fixtures/selection/deep-skips-engines.json +35 -0
- package/fixtures/selection/deep-skips-sources-without-deep-variant.json +32 -0
- package/fixtures/selection/default-quick-excludes-engines.json +27 -0
- package/fixtures/selection/engine-without-collector-support.json +34 -0
- package/fixtures/selection/group-name-collision-is-a-config-error.json +27 -0
- package/fixtures/selection/nothing-set-up-is-an-error.json +18 -0
- package/fixtures/selection/only-engine-not-configured-is-an-error.json +26 -0
- package/fixtures/selection/only-engine.json +30 -0
- package/fixtures/selection/only-expands-groups.json +37 -0
- package/fixtures/selection/only-not-configured-is-an-error.json +27 -0
- package/fixtures/selection/skip-excludes.json +34 -0
- package/fixtures/selection/unknown-name-is-an-error.json +26 -0
- package/package.json +15 -4
- package/schema/request.schema.json +64 -0
- package/schema/run.schema.json +387 -0
- package/schema/stored-run.schema.json +43 -0
- package/sources.json +658 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joey Kudish
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,65 @@
|
|
|
1
|
-
#
|
|
1
|
+
# librarium-spec
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared data for Librarium: request, Run and stored-Run JSON schemas, the source catalog, and conformance fixtures. [Librarium](https://github.com/jkudish/librarium) (JavaScript, CLI and MCP) and [Laravel AI Librarium](https://github.com/jkudish/laravel-ai-librarium) (PHP) both read it, so a Run written by one validates in the other.
|
|
4
|
+
|
|
5
|
+
It contains data and tests only, no runtime code. Most people get it as a dependency of those packages. Install it directly when you build a Librarium-compatible tool, store, or report reader.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install librarium-spec@^1.0
|
|
11
|
+
composer require jkudish/librarium-spec:^1.0
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Use
|
|
15
|
+
|
|
16
|
+
JavaScript (ESM):
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import sources from 'librarium-spec/sources.json' with { type: 'json' };
|
|
20
|
+
|
|
21
|
+
console.log(sources.sources.map((source) => source.name));
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
PHP: the files install as plain data. Find them through Composer:
|
|
25
|
+
|
|
26
|
+
```php
|
|
27
|
+
use Composer\InstalledVersions;
|
|
28
|
+
|
|
29
|
+
$root = InstalledVersions::getInstallPath('jkudish/librarium-spec');
|
|
30
|
+
$sources = json_decode(file_get_contents($root.'/sources.json'), true, flags: JSON_THROW_ON_ERROR);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Contents
|
|
34
|
+
|
|
35
|
+
| Path | What it holds |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| `SPEC.md` | The specification: research requests, source selection, depth, cost limits, Runs, and the RunStore contract |
|
|
38
|
+
| `schema/request.schema.json` | A research request |
|
|
39
|
+
| `schema/run.schema.json` | A Run: results, sources, usage, and cost |
|
|
40
|
+
| `schema/stored-run.schema.json` | The document a RunStore saves for one Run |
|
|
41
|
+
| `sources.json` | Sources and AI answer engines in catalog order, with variants, options, credential variable names, and per-language availability |
|
|
42
|
+
| `fixtures/selection/` | Configuration and request in, selected sources, skips, and budget outcome out |
|
|
43
|
+
| `fixtures/runs/` | Valid and invalid Run documents |
|
|
44
|
+
| `fixtures/run-store/` | RunStore contract cases and an example filesystem layout |
|
|
45
|
+
|
|
46
|
+
Pricing is not part of the spec. Each package reads its own price source.
|
|
47
|
+
|
|
48
|
+
## Versioning
|
|
49
|
+
|
|
50
|
+
Schemas, `sources.json` and fixtures follow semantic versioning. Consumers pin a minor version and run the fixtures in their test suites. A breaking change to a schema or to selection behavior needs a new major version.
|
|
51
|
+
|
|
52
|
+
## Develop
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm ci
|
|
56
|
+
npm test
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`npm test` runs `scripts/check.mjs`, which validates every fixture against the schemas and a reference selection implementation.
|
|
60
|
+
|
|
61
|
+
## Links
|
|
62
|
+
|
|
63
|
+
- Documentation: [librarium.si](https://librarium.si)
|
|
64
|
+
- [Specification](SPEC.md)
|
|
65
|
+
- [MIT license](LICENSE)
|
package/SPEC.md
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
# Librarium simplified surface: specification
|
|
2
|
+
|
|
3
|
+
Status: **approved** by Joey Kudish on 2026-10-12 (PlanMode epic #4954).
|
|
4
|
+
Package version 1.0.0: published as npm `librarium-spec` and Composer `jkudish/librarium-spec`.
|
|
5
|
+
Governs: Librarium JS 2.0 (`jkudish/librarium`, npm `librarium`), Laravel AI
|
|
6
|
+
Librarium 1.0 (`jkudish/laravel-ai-librarium`), the Firecrawl adapter
|
|
7
|
+
(`jkudish/laravel-ai-librarium-firecrawl`), this package, the CLI, MCP, docs,
|
|
8
|
+
skills, and librarium.si.
|
|
9
|
+
|
|
10
|
+
JS 2.0 and PHP 1.0 were the first releases of this surface. All of its
|
|
11
|
+
breaking changes landed before them.
|
|
12
|
+
|
|
13
|
+
## 1. Goal
|
|
14
|
+
|
|
15
|
+
One interface for many research providers that a person learns in a minute and
|
|
16
|
+
an agent uses without a manual. The public surface keeps only the decisions a
|
|
17
|
+
user makes:
|
|
18
|
+
|
|
19
|
+
1. **What to ask**: the question.
|
|
20
|
+
2. **How deep**: `quick` (seconds) or `deep` (minutes, background).
|
|
21
|
+
3. **Which sources**: by default every source you have set up; narrow with
|
|
22
|
+
`only` / `skip`.
|
|
23
|
+
4. **How much to spend**: an optional `max_cost`.
|
|
24
|
+
|
|
25
|
+
Everything produces a **Run**: one stored record with an id that a caller can
|
|
26
|
+
wait on, come back to, and read. The same Run JSON is returned by the CLI, MCP,
|
|
27
|
+
the JS library and the PHP package, and is what stores persist.
|
|
28
|
+
|
|
29
|
+
The provider engine stays: adapters and drivers, pricing, redaction, durable
|
|
30
|
+
provider tasks, usage and cost truthfulness, deadlines, the encrypted queue
|
|
31
|
+
job. This spec replaces the layer people and agents touch.
|
|
32
|
+
|
|
33
|
+
## 2. Concepts
|
|
34
|
+
|
|
35
|
+
| Concept | Meaning |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| Source | A provider you can ask, such as `exa` or `openai`. One flat name, identical in JS and PHP. Depth picks its variant. |
|
|
38
|
+
| Engine | A source that reports what a consumer AI product showed (`chatgpt`, `google-ai-overview`, …). Runs only when named. Results carry `"observation": "surface"`. |
|
|
39
|
+
| Depth | `quick`: answers and search. `deep`: research reports, durable background tasks. |
|
|
40
|
+
| Group | Optional: a user-saved list of source names, usable anywhere source names are accepted. No built-in groups. |
|
|
41
|
+
| Run | One execution: id, status, per-source results with citations, cost, skipped sources with reasons. |
|
|
42
|
+
| RunStore | Where Runs live. Configurable; four methods (section 6). |
|
|
43
|
+
|
|
44
|
+
Removed from the public surface: profiles, built-in groups, the `custom:`
|
|
45
|
+
prefix, modes (`sync`/`async`/`mixed`), fallbacks and reserves, the
|
|
46
|
+
estimated/actual budget split, capability selectors (PHP `matching()`), the
|
|
47
|
+
`plan` command, the `visibility` group, `refine`, and PHP `collectSensitive()`.
|
|
48
|
+
|
|
49
|
+
## 3. Sources
|
|
50
|
+
|
|
51
|
+
A source has at most one quick and one deep variant. Today's extra profiles
|
|
52
|
+
become source options. With `depth: deep`, sources without a deep variant are
|
|
53
|
+
skipped with reason `no_deep_variant`. Engines have only a quick variant and
|
|
54
|
+
are skipped by `deep` the same way.
|
|
55
|
+
|
|
56
|
+
### Research sources
|
|
57
|
+
|
|
58
|
+
| Source | quick | deep | Options (replace today's extra profiles) |
|
|
59
|
+
| --- | --- | --- | --- |
|
|
60
|
+
| `brave` | Brave answers | — | `results: answer \| search` (brave-search) |
|
|
61
|
+
| `claude` | Claude chat with web search | — | `model` |
|
|
62
|
+
| `exa` | Exa search | Exa research | — |
|
|
63
|
+
| `firecrawl` | Firecrawl search | — | `sources`, `limit` |
|
|
64
|
+
| `gemini` | Gemini grounded | Gemini Deep Research | `quick: grounded \| chat` |
|
|
65
|
+
| `google` | Google results via a SERP collector | — | `collector: searchapi \| serpapi \| serpbase`, `vertical: web \| news` |
|
|
66
|
+
| `grok` | Grok web search | — | `search: web \| x \| both` |
|
|
67
|
+
| `jina` | Jina search | — | — |
|
|
68
|
+
| `kagi` | Kagi FastGPT | — | — |
|
|
69
|
+
| `openai` | OpenAI chat with web search | OpenAI research | `model`, `effort` |
|
|
70
|
+
| `openrouter` | OpenRouter grounded | — | `quick: grounded \| chat`, `model` |
|
|
71
|
+
| `parallel` | Parallel turbo | Parallel research | `quick: turbo \| search`, `processor` (default `pro`) |
|
|
72
|
+
| `perplexity` | Sonar Pro | Deep Research | `quick: sonar-pro \| search \| agent`, `deep_effort: medium \| high` |
|
|
73
|
+
| `tavily` | Tavily search | — | — |
|
|
74
|
+
| `valyu` | Valyu search | Valyu research | `mode` (default `standard`), `output` |
|
|
75
|
+
| `you` | You.com answer | You.com research | `quick: answer \| research` |
|
|
76
|
+
|
|
77
|
+
Mapping from today's JS profiles and PHP profile ids to (source, depth,
|
|
78
|
+
option) lives in `sources.json` and is the migration table for config and
|
|
79
|
+
docs. Where a language lacks a variant today (for example SERP collectors that
|
|
80
|
+
only one language implements), `sources.json` records availability per
|
|
81
|
+
language; the APIs report it through `sources` / `list_sources`.
|
|
82
|
+
|
|
83
|
+
### Engines
|
|
84
|
+
|
|
85
|
+
| Engine | Collectors |
|
|
86
|
+
| --- | --- |
|
|
87
|
+
| `chatgpt` | searchapi, dataforseo (PHP), firecrawl (PHP adapter) |
|
|
88
|
+
| `gemini-app` | searchapi, dataforseo (PHP), firecrawl (PHP adapter) |
|
|
89
|
+
| `google-ai-mode` | searchapi, dataforseo (PHP), firecrawl (PHP adapter) |
|
|
90
|
+
| `google-ai-overview` | searchapi, firecrawl (PHP adapter) |
|
|
91
|
+
| `copilot` | searchapi, firecrawl (PHP adapter) |
|
|
92
|
+
|
|
93
|
+
The collector is configuration (`engines.collector`, per-engine override), not
|
|
94
|
+
part of the name. DataForSEO's standard/live task priority is a collector
|
|
95
|
+
option. The Firecrawl adapter additionally lets an app declare its own engine
|
|
96
|
+
by URL (section 9). Engines never join the default source set: they answer a
|
|
97
|
+
different question ("how do AI products describe X?") and cost per lookup.
|
|
98
|
+
Today's visibility "API baselines" are dropped; name `perplexity` or `gemini`
|
|
99
|
+
alongside engines to compare.
|
|
100
|
+
|
|
101
|
+
### Custom sources
|
|
102
|
+
|
|
103
|
+
JS custom providers and PHP custom drivers register a new source name with a
|
|
104
|
+
quick and/or deep variant (or as an engine) and appear exactly like built-ins.
|
|
105
|
+
|
|
106
|
+
## 4. Contract
|
|
107
|
+
|
|
108
|
+
All JSON is `snake_case`; JS option names in code are camelCase. Money is a
|
|
109
|
+
decimal string in USD. Times are RFC 3339 UTC. Run ids are `run_` + ULID.
|
|
110
|
+
Schemas: `schema/request.schema.json`, `schema/run.schema.json`,
|
|
111
|
+
`schema/stored-run.schema.json`.
|
|
112
|
+
|
|
113
|
+
### Request
|
|
114
|
+
|
|
115
|
+
```json
|
|
116
|
+
{
|
|
117
|
+
"question": "Replacing GA4 for an EU SaaS: compare Plausible, Fathom, and Matomo",
|
|
118
|
+
"depth": "deep",
|
|
119
|
+
"only": ["exa", "openai", "my-group"],
|
|
120
|
+
"skip": [],
|
|
121
|
+
"max_cost": "1.00",
|
|
122
|
+
"dry_run": false,
|
|
123
|
+
"options": { "parallel": { "processor": "ultra" } }
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
| Field | Rules |
|
|
128
|
+
| --- | --- |
|
|
129
|
+
| `question` | Required, non-empty. |
|
|
130
|
+
| `depth` | `quick` (default) or `deep`. |
|
|
131
|
+
| `only` | Source, engine, or group names; groups expand to their members. Omitted: every set-up research source (never engines). Naming something not set up is an error, not a silent skip. |
|
|
132
|
+
| `skip` | Names removed after expansion. |
|
|
133
|
+
| `max_cost` | Optional. Default from config. Over MCP it may only lower the configured cap. |
|
|
134
|
+
| `dry_run` | Return a Run with status `planned` and estimates; no provider calls; nothing written to the store. |
|
|
135
|
+
| `options` | Advanced per-source overrides, validated against `sources.json`. |
|
|
136
|
+
|
|
137
|
+
### Run
|
|
138
|
+
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"spec": "librarium.run/1",
|
|
142
|
+
"id": "run_01JAB2C3D4E5F6G7H8J9K0M1N2",
|
|
143
|
+
"question": "…",
|
|
144
|
+
"depth": "deep",
|
|
145
|
+
"status": "running",
|
|
146
|
+
"created_at": "2026-10-12T18:04:11Z",
|
|
147
|
+
"finished_at": null,
|
|
148
|
+
"cost": { "estimated": "0.35", "reported": "0.10", "complete": false, "max": "1.00" },
|
|
149
|
+
"results": [
|
|
150
|
+
{
|
|
151
|
+
"source": "exa",
|
|
152
|
+
"status": "done",
|
|
153
|
+
"observation": "api",
|
|
154
|
+
"answer": "## Findings\n…",
|
|
155
|
+
"citations": [{ "url": "https://plausible.io/features", "title": "Plausible features" }],
|
|
156
|
+
"cost": { "estimated": "0.10", "reported": "0.10" },
|
|
157
|
+
"started_at": "2026-10-12T18:04:12Z",
|
|
158
|
+
"duration_ms": 122000,
|
|
159
|
+
"provenance": { "provider": "exa", "variant": "research", "model": null, "collector": null },
|
|
160
|
+
"error": null
|
|
161
|
+
}
|
|
162
|
+
],
|
|
163
|
+
"skipped": [{ "source": "kagi", "reason": "no_deep_variant" }],
|
|
164
|
+
"notices": []
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
- Run `status`: `planned` (dry run), `running`, `done` (all results done),
|
|
169
|
+
`partial` (some failed), `failed` (none succeeded).
|
|
170
|
+
- Result `status`: `queued`, `running`, `done`, `failed`. `error` is
|
|
171
|
+
`{ "code", "message", "retryable" }`. Interrupted inline work is `failed`
|
|
172
|
+
with code `interrupted`.
|
|
173
|
+
- `observation`: `api` for research sources, `surface` for engines.
|
|
174
|
+
- Skip reasons: `not_configured`, `no_deep_variant`, `unpriced`,
|
|
175
|
+
`over_budget`, `excluded`, `no_collector`.
|
|
176
|
+
- `answer` is Markdown (null for search-only sources); `citations` always
|
|
177
|
+
present. Raw provider bodies are evidence in the store, never inline.
|
|
178
|
+
- `cost.reported` is provider-reported only; unknown is `null`, never `"0"`.
|
|
179
|
+
`cost.complete` is true only when every result reported a cost.
|
|
180
|
+
|
|
181
|
+
### Budget rule (identical in both languages)
|
|
182
|
+
|
|
183
|
+
With `max_cost`: skip sources without a bounded estimate (`unpriced`); then
|
|
184
|
+
walk the remaining sources in catalog order (the order of `sources.json`),
|
|
185
|
+
admitting each whose estimate still fits the remaining budget; the rest are
|
|
186
|
+
`over_budget`. Without `max_cost`, sources without an estimate still run and
|
|
187
|
+
`cost.estimated` is `null`. `skipped` is listed in catalog order.
|
|
188
|
+
|
|
189
|
+
Selection errors (the request fails before any Run is stored):
|
|
190
|
+
`unknown_name`, `not_configured` (a named source, or an engine with no
|
|
191
|
+
configured collector), `group_name_conflict` (config), `no_sources` (nothing
|
|
192
|
+
set up and no `only`). The fixtures in `fixtures/selection/` are normative.
|
|
193
|
+
During the run, reported spend plus reservations stops unstarted work.
|
|
194
|
+
In-flight calls may finish above the cap; docs say so.
|
|
195
|
+
|
|
196
|
+
## 5. Lifecycle
|
|
197
|
+
|
|
198
|
+
Every non-dry-run request creates the Run in the store before any provider
|
|
199
|
+
call. Waiting is a client choice, not a request field. One Run never mixes
|
|
200
|
+
depths, so there is no mode.
|
|
201
|
+
|
|
202
|
+
- Depth only chooses variants. Whether a result runs inline or as a durable
|
|
203
|
+
task is a property of its variant or collector, not of the depth.
|
|
204
|
+
- **quick** results usually run inline in the calling process. A quick
|
|
205
|
+
variant or engine collector that is a durable provider task (Firecrawl
|
|
206
|
+
`agent` mode, DataForSEO standard-priority tasks) is submitted durably like
|
|
207
|
+
a deep source: its signed handle goes into Run state and
|
|
208
|
+
`run()` / `queue()` / `refresh()` / `show` / `get_run` finish it.
|
|
209
|
+
- **deep** results are always durable provider tasks. Their signed handles are saved
|
|
210
|
+
in the Run's private state, so any later `show` / `get` / `find()->refresh()`
|
|
211
|
+
can observe and finish them. Every deep variant must be durable in both
|
|
212
|
+
languages (PHP must add recoverable handles for OpenAI and You.com research).
|
|
213
|
+
|
|
214
|
+
| Surface | Default | Not waiting |
|
|
215
|
+
| --- | --- | --- |
|
|
216
|
+
| CLI | waits with live progress; Ctrl-C detaches and prints `librarium show <id>` | `--no-wait` (deep only; quick work runs inline, so `--no-wait` without `--deep` fails before anything is stored; `--dry-run` is exempt) |
|
|
217
|
+
| MCP | waits up to `wait_seconds` (default 45, max 50), then returns the Run as `running` | `wait_seconds: 0` |
|
|
218
|
+
| JS | `await research()` resolves when the Run finishes | `{ wait: false }` |
|
|
219
|
+
| PHP | `run()` waits | `queue()` dispatches one encrypted job and returns the running Run's id |
|
|
220
|
+
|
|
221
|
+
## 6. Storage
|
|
222
|
+
|
|
223
|
+
### Interface
|
|
224
|
+
|
|
225
|
+
Each language defines `RunStore` with four methods. A store persists one
|
|
226
|
+
`StoredRun` document per Run: `{ run, state, files, version }`, where `state`
|
|
227
|
+
is private (signed provider handles, ledger, reservations) and `files` maps
|
|
228
|
+
names (`answers/<source>.md`, `evidence/<source>.json`) to contents.
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
export interface RunStore {
|
|
232
|
+
get(id: string): Promise<StoredRun | null>;
|
|
233
|
+
/** Write if the stored version equals expectedVersion (null = must not exist).
|
|
234
|
+
* Returns the new version; throws RunStoreConflict otherwise. */
|
|
235
|
+
save(doc: StoredRun, expectedVersion: number | null): Promise<number>;
|
|
236
|
+
list(opts?: { limit?: number; before?: string }): Promise<RunSummary[]>; // newest first
|
|
237
|
+
delete(id: string): Promise<void>;
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
```php
|
|
242
|
+
interface RunStore
|
|
243
|
+
{
|
|
244
|
+
public function find(string $id): ?StoredRun;
|
|
245
|
+
|
|
246
|
+
/** @throws RunStoreConflict */
|
|
247
|
+
public function save(StoredRun $run, ?int $expectedVersion): int;
|
|
248
|
+
|
|
249
|
+
/** @return list<RunSummary> newest first */
|
|
250
|
+
public function list(int $limit = 50, ?string $before = null): array;
|
|
251
|
+
|
|
252
|
+
public function delete(string $id): void;
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
`StoredRun` serializes to `schema/stored-run.schema.json` in both languages.
|
|
257
|
+
|
|
258
|
+
### Built-in stores
|
|
259
|
+
|
|
260
|
+
| Store | JS | PHP |
|
|
261
|
+
| --- | --- | --- |
|
|
262
|
+
| `filesystem` | default for the CLI, MCP and `librarium/node` | default; local Laravel disks only; encrypted with the app key unless `encrypt` is false |
|
|
263
|
+
| `database` | — | opt-in: any connection, one table (`librarium_runs` by default), one row per Run holding run, state, files and version; query builder, no Eloquent; migration published with `vendor:publish --tag=librarium-migrations` and never auto-run |
|
|
264
|
+
| `cache` | — | process-local Laravel ArrayStore, or standalone RedisStore on Redis 7+ with connection serialization and compression disabled; document TTL supported |
|
|
265
|
+
| `memory` | default for `librarium/core` (Workers, browsers) | tests |
|
|
266
|
+
|
|
267
|
+
The built-in PHP filesystem and cache stores reject unsupported configurations
|
|
268
|
+
before mutation. Remote or arbitrary Flysystem disks, Redis Cluster,
|
|
269
|
+
decorated/failover caches, and other cache drivers require a custom `RunStore`.
|
|
270
|
+
For shared production storage, use the database store, supported standalone
|
|
271
|
+
Redis, or a custom store that supplies atomic version checks. ArrayStore is
|
|
272
|
+
safe only within one process; it is not shared queue-worker persistence.
|
|
273
|
+
|
|
274
|
+
Filesystem layout, shared by both languages:
|
|
275
|
+
|
|
276
|
+
```text
|
|
277
|
+
<path>/<run_id>/run.json
|
|
278
|
+
<path>/<run_id>/state.json
|
|
279
|
+
<path>/<run_id>/answers/<source>.md
|
|
280
|
+
<path>/<run_id>/evidence/<source>.json
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
`run.json` is exactly the public Run. `state.json` is
|
|
284
|
+
`{ "version": <int ≥ 1>, "state": { … } }` and carries the StoredRun version
|
|
285
|
+
used for compare-and-swap; nothing else in the folder is versioned. Writers
|
|
286
|
+
update `state.json` last, after `run.json` and the files. There is no legacy
|
|
287
|
+
form: a `state.json` without `version` is invalid. An example folder is in
|
|
288
|
+
`fixtures/run-store/filesystem/`. An unencrypted folder written by one
|
|
289
|
+
language is readable by the other.
|
|
290
|
+
|
|
291
|
+
Local filesystem readers and writers in both languages coordinate through
|
|
292
|
+
`<path>/<run_id>/run.json.lock`: exclusive creation, a unique ownership token,
|
|
293
|
+
no age expiry, and token-checked release. A writer holds ownership across the
|
|
294
|
+
complete files, Run, and state commit; a lease that can expire during a write
|
|
295
|
+
does not satisfy this contract. Crashed-owner recovery is explicit: establish
|
|
296
|
+
that the owner cannot resume before removing its marker. Multi-file writes
|
|
297
|
+
are not crash-atomic; the lock prevents concurrent access, not partial disk
|
|
298
|
+
writes after process or storage failure.
|
|
299
|
+
|
|
300
|
+
ArrayStore holds a non-expiring token-owned lock across the document and index
|
|
301
|
+
commit. Redis uses a server-side conditional commit, not a correctness lease:
|
|
302
|
+
one Lua operation checks the stored version and writes the JSON document,
|
|
303
|
+
document TTL, and listing membership together. It preflights key types and
|
|
304
|
+
required command permissions before mutation; Redis Lua does not roll back
|
|
305
|
+
runtime errors. A transport failure leaves the outcome uncertain and never
|
|
306
|
+
authorizes resubmitting provider work.
|
|
307
|
+
|
|
308
|
+
### Adding a store
|
|
309
|
+
|
|
310
|
+
| | JS | PHP |
|
|
311
|
+
| --- | --- | --- |
|
|
312
|
+
| Implement | a class with the four methods | a class implementing `RunStore` |
|
|
313
|
+
| Register | `createLibrarium({ store })`; CLI/MCP config `"runs": { "store": { "module": "./stores/redis.js" } }` whose default export returns a store | `'runs' => ['store' => 'redis']` plus `Librarium::extendStore('redis', fn ($app, array $config) => …)`, or a class name |
|
|
314
|
+
| Check | `import { runStoreContract } from 'librarium/testing'` | `uses(RunStoreContractTests::class)` with a `store()` factory |
|
|
315
|
+
|
|
316
|
+
Both test kits run the cases in `fixtures/run-store/` (create, conflict on a
|
|
317
|
+
stale version, not found, list order and paging, delete, round trip of a
|
|
318
|
+
fixture Run). Every built-in store passes them.
|
|
319
|
+
|
|
320
|
+
### Rules
|
|
321
|
+
|
|
322
|
+
- No credentials are ever stored. Evidence is redacted before it reaches a
|
|
323
|
+
store, with PHP's current redaction semantics, which JS adopts.
|
|
324
|
+
- PHP signs stored provider handles with the app key so a tampered store
|
|
325
|
+
cannot redirect retrieval.
|
|
326
|
+
- Retention: `runs.keep_days` (default unset); `librarium cleanup` and
|
|
327
|
+
`php artisan librarium:prune`.
|
|
328
|
+
- Concurrent writers (queue worker vs `refresh()`) are safe through the
|
|
329
|
+
version check.
|
|
330
|
+
|
|
331
|
+
## 7. Surfaces
|
|
332
|
+
|
|
333
|
+
### CLI
|
|
334
|
+
|
|
335
|
+
```bash
|
|
336
|
+
librarium research "q" # quick, set-up sources, waits
|
|
337
|
+
librarium research "q" --deep # deep, background, waits with progress
|
|
338
|
+
librarium research "q" --only exa,openai --max-cost 1 # narrow and cap
|
|
339
|
+
librarium research "q" --only chatgpt,copilot # engines (visibility)
|
|
340
|
+
librarium research "q" --deep --dry-run # planned Run, no calls
|
|
341
|
+
librarium research "q" --deep --no-wait # print the id and exit
|
|
342
|
+
librarium show [run-id] [--wait] [--source exa] [--json] # default: latest Run
|
|
343
|
+
librarium summarize [run-id] [--verify] # synthesis over a stored Run
|
|
344
|
+
librarium sources [--json] # sources, engines, groups, variants, prices
|
|
345
|
+
librarium groups add <name> <names…> | remove <name>
|
|
346
|
+
librarium setup [--check] [--live] # keys, options, default max_cost; --check = doctor
|
|
347
|
+
librarium mcp
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
`summarize` (JS only) synthesizes one cited answer from a stored Run:
|
|
351
|
+
|
|
352
|
+
- The synthesis source is `summarize.source` in config (set by `setup`), or
|
|
353
|
+
`--source <name>`. If neither is set, the first set-up source in this fixed
|
|
354
|
+
order: `openai`, `claude`, `gemini`, `openrouter`. If none is set up, it
|
|
355
|
+
fails with `no_summarize_source`. A failed call fails; there is no cascade.
|
|
356
|
+
- `--verify` makes its verification call with the same source.
|
|
357
|
+
- The paid synthesis (and verification) is tracked as its own Run: one result
|
|
358
|
+
from the synthesis source holding the summary Markdown and its citations,
|
|
359
|
+
with `summary_of` set to the source Run's id. The source Run is unchanged.
|
|
360
|
+
- `max_cost` applies to the summary Run like any other.
|
|
361
|
+
|
|
362
|
+
Kept tools: `browse`, `html`, `jsonl`, `usage`, `cleanup`, `config`,
|
|
363
|
+
`completions`, `upgrade`, `install-skill`. `--json` prints the Run.
|
|
364
|
+
|
|
365
|
+
### MCP (three tools)
|
|
366
|
+
|
|
367
|
+
| Tool | Input | Output |
|
|
368
|
+
| --- | --- | --- |
|
|
369
|
+
| `research` | `question`, `depth?`, `only?`, `skip?`, `max_cost?` (lower only), `dry_run?`, `wait_seconds?` | Run summary: id, status, per-source status and citation counts, cost, skipped, next-step hint |
|
|
370
|
+
| `get_run` | `id`, `wait_seconds?`, `source?`, `cursor?` | summary, or one source's answer and citations, paged |
|
|
371
|
+
| `list_sources` | — | sources, engines and groups: set up or not, variants, price hints |
|
|
372
|
+
|
|
373
|
+
### JavaScript
|
|
374
|
+
|
|
375
|
+
```ts
|
|
376
|
+
import { createLibrarium } from 'librarium';
|
|
377
|
+
|
|
378
|
+
const librarium = createLibrarium(); // config + filesystem store
|
|
379
|
+
const run = await librarium.research(q, { depth: 'deep', only: ['exa'], maxCost: '1.00' });
|
|
380
|
+
const started = await librarium.research(q, { depth: 'deep', wait: false });
|
|
381
|
+
const later = await librarium.get(started.id, { wait: true });
|
|
382
|
+
librarium.sources();
|
|
383
|
+
await librarium.summarize(run);
|
|
384
|
+
// librarium/core: createLibrarium({ config, store })
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### PHP
|
|
388
|
+
|
|
389
|
+
```php
|
|
390
|
+
$run = Librarium::research($q)->deep()->only(['exa', 'openai'])->maxCost('1.00')->run();
|
|
391
|
+
$plan = Librarium::research($q)->deep()->dryRun();
|
|
392
|
+
$queued = Librarium::research($q)->deep()->queue()->onQueue('research')
|
|
393
|
+
->then(fn (Run $run) => /* … */); // $queued->id()
|
|
394
|
+
$run = Librarium::find($id)->refresh(); // one observation of pending deep sources
|
|
395
|
+
$run->evidence('exa'); // redacted provider body
|
|
396
|
+
$run->toArray(); // contract JSON
|
|
397
|
+
Librarium::sources();
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
Artisan: `librarium:research`, `librarium:show`, `librarium:sources`,
|
|
401
|
+
`librarium:prune`. The Laravel AI SDK evidence tool stays and reads Runs.
|
|
402
|
+
|
|
403
|
+
PHP method mapping: `run()` stays; `collect()` folds into stored evidence
|
|
404
|
+
(`$run->evidence()`, `$run->diagnostics()`); `collectSensitive()` is removed;
|
|
405
|
+
`submit()`/`retrieve()` fold into `queue()` + `find()`/`refresh()`;
|
|
406
|
+
`preview()`/`spendingBound()` become `dryRun()`; `using()`, `fanOut()`,
|
|
407
|
+
`group()`, `matching()`, `fallbacks()` become `only()`/`skip()`/`deep()`.
|
|
408
|
+
|
|
409
|
+
## 8. Configuration
|
|
410
|
+
|
|
411
|
+
JS (`version: 3`, written by `librarium setup`):
|
|
412
|
+
|
|
413
|
+
```json
|
|
414
|
+
{
|
|
415
|
+
"version": 3,
|
|
416
|
+
"sources": {
|
|
417
|
+
"exa": { "key": "env:EXA_API_KEY" },
|
|
418
|
+
"parallel": { "key": "env:PARALLEL_API_KEY", "processor": "pro" },
|
|
419
|
+
"grok": { "key": "env:XAI_API_KEY", "search": "both", "enabled": false }
|
|
420
|
+
},
|
|
421
|
+
"engines": { "collector": "searchapi", "key": "env:SEARCHAPI_API_KEY" },
|
|
422
|
+
"groups": { "visibility": ["chatgpt", "gemini-app", "google-ai-mode", "google-ai-overview", "copilot"] },
|
|
423
|
+
"max_cost": "1.00",
|
|
424
|
+
"runs": { "store": "filesystem", "path": "./agents/librarium", "keep_days": null }
|
|
425
|
+
}
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
PHP (`config/librarium.php`): `sources`, `engines`, `groups`, `max_cost`,
|
|
429
|
+
`runs` (`store`, `disk`, `path`, `connection`, `table`, `encrypt`,
|
|
430
|
+
`keep_days`), `timeout`. A source is set up when it has a key and `enabled` is
|
|
431
|
+
not `false`. A group name equal to a source or engine name is a config error.
|
|
432
|
+
|
|
433
|
+
Migration: JS 1.x configs migrate to v3 on load with a written backup
|
|
434
|
+
(providers become sources, extra profiles become options, `mode` and fallbacks
|
|
435
|
+
dropped with notices, custom groups kept as groups, the built-in visibility
|
|
436
|
+
group becomes a `visibility` group of engines). Config v2 never shipped. PHP
|
|
437
|
+
1.0 is unreleased; its config is rewritten. v1 run directories are not
|
|
438
|
+
readable by `show` (documented).
|
|
439
|
+
|
|
440
|
+
## 9. Firecrawl
|
|
441
|
+
|
|
442
|
+
- Firecrawl search becomes a built-in PHP source (`firecrawl`), written with
|
|
443
|
+
Laravel HTTP like the other drivers, matching JS.
|
|
444
|
+
- `laravel-ai-librarium-firecrawl` keeps only browser collection (SDK
|
|
445
|
+
`interact` and async `agent` modes). It registers `firecrawl` as an engine
|
|
446
|
+
collector and lets apps declare custom engines by URL. Its async job handle
|
|
447
|
+
lives in the Run's private state; its webhook is a wake hint for that Run.
|
|
448
|
+
|
|
449
|
+
## 10. Removed and renamed
|
|
450
|
+
|
|
451
|
+
| Today | Becomes |
|
|
452
|
+
| --- | --- |
|
|
453
|
+
| `run` | `research` |
|
|
454
|
+
| `-p provider/profile`, `-g`, built-in groups, `custom:`, `list_groups` | `--only` / `--skip` with sources, engines, or groups; `--deep` |
|
|
455
|
+
| `visibility` group | engines named in `only`, or a saved group |
|
|
456
|
+
| `--mode`, `mixed` | automatic |
|
|
457
|
+
| `--no-fallback`, fallbacks, reserves | removed |
|
|
458
|
+
| `--max-estimated-cost` | `--max-cost` |
|
|
459
|
+
| `plan`, `preview()`, `spendingBound()` | `--dry-run`, `dryRun()` |
|
|
460
|
+
| `status`, `check_async`, `get_results`, `retrieve()` | `show`, `get_run`, `find()`/`refresh()` |
|
|
461
|
+
| `answer`, `answer --verify` | `summarize [id]`, `summarize --verify` |
|
|
462
|
+
| `refine`, `--refine` | removed |
|
|
463
|
+
| `ls`, `init`, `doctor` | `sources`, `setup`, `setup --check` |
|
|
464
|
+
| `live-validation` | maintainer script |
|
|
465
|
+
|
|
466
|
+
Removed names fail with a message naming the replacement.
|
|
467
|
+
|
|
468
|
+
## 11. This package
|
|
469
|
+
|
|
470
|
+
`jkudish/librarium-spec`, published to npm (`librarium-spec`) and Packagist
|
|
471
|
+
(`jkudish/librarium-spec`). Data and tests only, no runtime code:
|
|
472
|
+
|
|
473
|
+
```text
|
|
474
|
+
SPEC.md
|
|
475
|
+
schema/request.schema.json schema/run.schema.json schema/stored-run.schema.json
|
|
476
|
+
sources.json sources and engines in catalog order: variants,
|
|
477
|
+
options, credential env names, per-language
|
|
478
|
+
availability, and the legacy JS profile and PHP
|
|
479
|
+
profile ids each variant replaces
|
|
480
|
+
fixtures/selection/ config + request → selected sources, skips, budget outcome
|
|
481
|
+
fixtures/runs/ valid and invalid Run documents
|
|
482
|
+
fixtures/run-store/ RunStore contract cases
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
Pricing stays in each package (the JS pricing snapshot and PHP
|
|
486
|
+
laravel-ai-pricing), because they read different price sources. JS v1 config
|
|
487
|
+
migration is JS-only, so its fixtures live in the JS repo.
|
|
488
|
+
`scripts/check.mjs` validates the fixtures against the schemas and a reference
|
|
489
|
+
selection implementation; it is test-only and not published.
|
|
490
|
+
|
|
491
|
+
Both packages pin a minor version (`^1.0`) and run the fixtures in their test
|
|
492
|
+
suites.
|
|
493
|
+
This replaces `contracts/v1` in the JS repo and the PHP `typescript:repin`
|
|
494
|
+
step. The website reads `sources.json` for its provider grid and counts.
|
|
495
|
+
|
|
496
|
+
## 12. Verification
|
|
497
|
+
|
|
498
|
+
- JS: build, test, typecheck, lint, `test:workers`, `test:integration`,
|
|
499
|
+
`test:pty`, `test:declarations`, and the shared fixtures.
|
|
500
|
+
- PHP: `composer test`, `analyse`, `format:check`, `validate --strict`, lowest
|
|
501
|
+
and highest dependency lanes, shared fixtures, `pr:check` at the final head.
|
|
502
|
+
- Cross-language: the same fixtures pass in both; an unencrypted Run folder
|
|
503
|
+
written by JS is read by PHP `find()` and vice versa.
|
|
504
|
+
- No live provider calls until the lead's canary, under a separately approved
|
|
505
|
+
cap.
|