@wrenautomation/raven 0.0.0-stage → 0.1.1
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.md +91 -0
- package/README.md +52 -2
- package/dist/app/cli.d.ts +2 -0
- package/dist/app/cli.js +115 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/model.d.ts +20 -0
- package/dist/model.js +68 -0
- package/dist/plan.d.ts +31 -0
- package/dist/plan.js +90 -0
- package/dist/plans/index.d.ts +8 -0
- package/dist/plans/index.js +196 -0
- package/dist/render.d.ts +7 -0
- package/dist/render.js +40 -0
- package/dist/review.d.ts +11 -0
- package/dist/review.js +44 -0
- package/dist/triage.d.ts +15 -0
- package/dist/triage.js +65 -0
- package/dist/types.d.ts +265 -0
- package/dist/types.js +134 -0
- package/package.json +50 -4
package/LICENSE.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# PolyForm Shield License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/shield/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
12
|
+
|
|
13
|
+
## Distribution License
|
|
14
|
+
|
|
15
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
16
|
+
|
|
17
|
+
## Notices
|
|
18
|
+
|
|
19
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
20
|
+
|
|
21
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
22
|
+
|
|
23
|
+
## Changes and New Works License
|
|
24
|
+
|
|
25
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
26
|
+
|
|
27
|
+
## Patent License
|
|
28
|
+
|
|
29
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
30
|
+
|
|
31
|
+
## Noncompete
|
|
32
|
+
|
|
33
|
+
Any purpose is a permitted purpose, except for providing any product that competes with the software or any product the licensor or any of its affiliates provides using the software.
|
|
34
|
+
|
|
35
|
+
## Competition
|
|
36
|
+
|
|
37
|
+
Goods and services compete even when they provide functionality through different kinds of interfaces or for different technical platforms. Applications can compete with services, libraries with plugins, frameworks with development tools, and so on, even if they're written in different programming languages or for different computer architectures. Goods and services compete even when provided free of charge. If you market a product as a practical substitute for the software or another product, it definitely competes.
|
|
38
|
+
|
|
39
|
+
## New Products
|
|
40
|
+
|
|
41
|
+
If you are using the software to provide a product that does not compete, but the licensor or any of its affiliates brings your product into competition by providing a new version of the software or another product using the software, you may continue using versions of the software available under these terms beforehand to provide your competing product, but not any later versions.
|
|
42
|
+
|
|
43
|
+
## Discontinued Products
|
|
44
|
+
|
|
45
|
+
You may begin using the software to compete with a product or service that the licensor or any of its affiliates has stopped providing, unless the licensor includes a plain-text line beginning with `Licensor Line of Business:` with the software that mentions that line of business. For example:
|
|
46
|
+
|
|
47
|
+
> Licensor Line of Business: YoyodyneCMS Content Management System (http://example.com/cms)
|
|
48
|
+
|
|
49
|
+
## Sales of Business
|
|
50
|
+
|
|
51
|
+
If the licensor or any of its affiliates sells a line of business developing the software or using the software to provide a product, the buyer can also enforce [Noncompete](#noncompete) for that product.
|
|
52
|
+
|
|
53
|
+
## Fair Use
|
|
54
|
+
|
|
55
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
56
|
+
|
|
57
|
+
## No Other Rights
|
|
58
|
+
|
|
59
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
60
|
+
|
|
61
|
+
## Patent Defense
|
|
62
|
+
|
|
63
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
64
|
+
|
|
65
|
+
## Violations
|
|
66
|
+
|
|
67
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
68
|
+
|
|
69
|
+
## No Liability
|
|
70
|
+
|
|
71
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
76
|
+
|
|
77
|
+
A **product** can be a good or service, or a combination of them.
|
|
78
|
+
|
|
79
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all its affiliates.
|
|
82
|
+
|
|
83
|
+
**Affiliates** means the other organizations than an organization has control over, is under the control of, or is under common control with.
|
|
84
|
+
|
|
85
|
+
**Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
86
|
+
|
|
87
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
88
|
+
|
|
89
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
|
90
|
+
|
|
91
|
+
Required Notice: Copyright 2026 William Jin (https://wrenautomation.com)
|
package/README.md
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
|
-
#
|
|
1
|
+
# raven
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ask in, plan out. The planner of Wren's research engine, on its own: no database, no
|
|
4
|
+
sources, no model of its own. Pure functions and prompts; a plan file per ask kind; the model
|
|
5
|
+
is injected. Depends on nothing of Wren's: [weaver](https://github.com/wrenautomation/weaver)
|
|
6
|
+
runs the plans raven writes, [falcon](https://github.com/wrenautomation/falcon) reads what
|
|
7
|
+
weaver fetched.
|
|
8
|
+
|
|
9
|
+
Three calls, one record each:
|
|
10
|
+
|
|
11
|
+
| Call | In | Out |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `plan` | an ask (text, kind, what is known), proposals from people or other models, yield rows from earlier runs | a `Plan`: typed queries, each with `why`, `from` (ai or human) and `by` (model id, plan file or person), the sources it may use, the budget, and a snapshot of what raven was given |
|
|
14
|
+
| `triage` | hits as a source shows them for free (title, channel, length, date, snippet, which sources returned it) | read or skip per hit, with why; the read budget caps it |
|
|
15
|
+
| `review` | what a round came to (counts, found lines, notes) | the edge: `stop`, `again` (a new round from plan) or `back` (rerun one step), with why |
|
|
16
|
+
|
|
17
|
+
Ask kinds: `web`, `video`, `channel`, `person`, `site`. Each has a default plan file
|
|
18
|
+
(prompts, sources, budget). Copy one, tune it, keep it beside your ledger: that is where a run
|
|
19
|
+
learns. Raven never loops; the runner takes the edge.
|
|
20
|
+
|
|
21
|
+
## CLI
|
|
22
|
+
|
|
23
|
+
Needs Node 24+. The model is a command that reads the prompt on stdin and prints the answer.
|
|
24
|
+
Default `claude -p`, so Claude Code is the model in any terminal; `RAVEN_MODEL` or `--model`
|
|
25
|
+
changes it; `--no-model` plans from your own queries only.
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm i @wrenautomation/raven
|
|
29
|
+
npx raven plan "how do staffing firms win repeat clients" --known "referrals convert"
|
|
30
|
+
npx raven plan "..." --kind video --propose "cold email teardown" --by will # yours merge with the model's
|
|
31
|
+
npx raven triage --plan plan.json --hits hits.json
|
|
32
|
+
npx raven review --round round.json
|
|
33
|
+
npx raven plans web > plans/web.json # copy, then tune
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Library
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { plan, triage, review, type Model } from "@wrenautomation/raven";
|
|
40
|
+
|
|
41
|
+
const model: Model = { id: "claude-sonnet-5-5", ask: (prompt, shape) => yourClient.json(prompt, shape) };
|
|
42
|
+
const p = await plan({ ask: { text, kind: "web", known }, model, proposals: [byHand] });
|
|
43
|
+
const reads = await triage(hits, p, model);
|
|
44
|
+
const edge = await review(round, model);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Every record carries `from` and `by`. A person's query, a person's read-or-skip, a person's
|
|
48
|
+
edge are the same shapes with `from: "human"`: the engine shows them in the same rows as the
|
|
49
|
+
model's.
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
PolyForm Shield 1.0.0. Free to use, change and share; not to build a competing product.
|
package/dist/app/cli.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `raven`: the planner from a shell. `plan` turns an ask into queries, `triage` marks hits read
|
|
4
|
+
* or skip, `review` picks the edge after a round, `plans` prints a default plan file to tune.
|
|
5
|
+
* The model is a command that reads the prompt on stdin and prints the answer: `claude -p` by
|
|
6
|
+
* default (RAVEN_MODEL or --model to change it), so Claude Code is the planner's model in any
|
|
7
|
+
* terminal. Everything is JSON on stdout.
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
10
|
+
import { userInfo } from "node:os";
|
|
11
|
+
import { Command } from "commander";
|
|
12
|
+
import { commandModel } from "../model.js";
|
|
13
|
+
import { plan } from "../plan.js";
|
|
14
|
+
import { defaultPlanFile, PLAN_KINDS } from "../plans/index.js";
|
|
15
|
+
import { review } from "../review.js";
|
|
16
|
+
import { triage } from "../triage.js";
|
|
17
|
+
import { AskKind, HitView, Plan, PlanFile, Round, YieldRow, z } from "../types.js";
|
|
18
|
+
const DEFAULT_MODEL = "claude -p";
|
|
19
|
+
const readJson = (path, shape) => shape.parse(JSON.parse(readFileSync(path, "utf8")));
|
|
20
|
+
const print = (x) => console.log(JSON.stringify(x, null, 2));
|
|
21
|
+
const more = (v, prev) => [...prev, v];
|
|
22
|
+
const who = () => {
|
|
23
|
+
try {
|
|
24
|
+
return userInfo().username;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return "human";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const modelOf = (o) => o.model === false
|
|
31
|
+
? null
|
|
32
|
+
: commandModel(o.model, o.model === DEFAULT_MODEL ? "claude-code" : o.model);
|
|
33
|
+
const withModel = (c) => c
|
|
34
|
+
.option("--model <command>", "a command: prompt on stdin, answer on stdout", process.env.RAVEN_MODEL ?? DEFAULT_MODEL)
|
|
35
|
+
.option("--no-model", "no model call: only what is given by hand");
|
|
36
|
+
const program = new Command().name("raven").description("ask in, plan out");
|
|
37
|
+
withModel(program
|
|
38
|
+
.command("plan <ask>")
|
|
39
|
+
.description("the queries for one ask, merged from the model's and your own")
|
|
40
|
+
.option("-k, --kind <kind>", `one of ${PLAN_KINDS.join(", ")}`, "web")
|
|
41
|
+
.option("--site <host>", "keep every query on one host (kind site)")
|
|
42
|
+
.option("--known <line>", "a fact already in hand; repeat", more, [])
|
|
43
|
+
.option("--propose <terms>", "a query of your own; repeat", more, [])
|
|
44
|
+
.option("--by <name>", "who proposed the --propose queries", who())
|
|
45
|
+
.option("--plan-file <path>", "a tuned plan file (JSON) instead of the default")
|
|
46
|
+
.option("--yield <path>", "yield rows (JSON array) from earlier runs")
|
|
47
|
+
.option("--sources <names>", "narrow the plan file's sources, comma separated")
|
|
48
|
+
.option("--round <n>", "round number", "1")).action(async (text, o) => {
|
|
49
|
+
const kind = AskKind.parse(o.kind);
|
|
50
|
+
const ask = { text, kind, known: o.known, ...(o.site ? { site: o.site } : {}) };
|
|
51
|
+
const proposals = o.propose.length
|
|
52
|
+
? [
|
|
53
|
+
{
|
|
54
|
+
from: "human",
|
|
55
|
+
by: o.by,
|
|
56
|
+
queries: o.propose.map((terms) => ({
|
|
57
|
+
kind,
|
|
58
|
+
terms,
|
|
59
|
+
why: "proposed by hand",
|
|
60
|
+
...(o.site ? { site: o.site } : {}),
|
|
61
|
+
})),
|
|
62
|
+
},
|
|
63
|
+
]
|
|
64
|
+
: [];
|
|
65
|
+
print(await plan({
|
|
66
|
+
ask,
|
|
67
|
+
model: modelOf(o),
|
|
68
|
+
proposals,
|
|
69
|
+
round: Number(o.round),
|
|
70
|
+
...(o.planFile ? { planFile: readJson(o.planFile, PlanFile) } : {}),
|
|
71
|
+
...(o.yield ? { yieldRows: readJson(o.yield, z.array(YieldRow)) } : {}),
|
|
72
|
+
...(o.sources ? { sources: o.sources.split(",").map((s) => s.trim()) } : {}),
|
|
73
|
+
}));
|
|
74
|
+
});
|
|
75
|
+
withModel(program
|
|
76
|
+
.command("triage")
|
|
77
|
+
.description("read or skip each hit of a plan, with why")
|
|
78
|
+
.requiredOption("--plan <path>", "the plan (JSON, from `raven plan`)")
|
|
79
|
+
.requiredOption("--hits <path>", "hits (JSON array of {url, title?, channel?, length?, date?, snippet?, sources?})")
|
|
80
|
+
.option("--plan-file <path>", "a tuned plan file (JSON) instead of the default")
|
|
81
|
+
.option("--yield <path>", "yield rows (JSON array) from earlier runs")).action(async (o) => {
|
|
82
|
+
const model = modelOf(o);
|
|
83
|
+
if (!model)
|
|
84
|
+
throw new Error("triage needs a model");
|
|
85
|
+
print(await triage(readJson(o.hits, z.array(HitView)), readJson(o.plan, Plan), model, {
|
|
86
|
+
...(o.planFile ? { planFile: readJson(o.planFile, PlanFile) } : {}),
|
|
87
|
+
...(o.yield ? { yieldRows: readJson(o.yield, z.array(YieldRow)) } : {}),
|
|
88
|
+
}));
|
|
89
|
+
});
|
|
90
|
+
withModel(program
|
|
91
|
+
.command("review")
|
|
92
|
+
.description("the edge after a round: stop, again or back, with why")
|
|
93
|
+
.requiredOption("--round <path>", "the round (JSON: ask, round, budget, hits, reads, proposed, kept, found, notes)")
|
|
94
|
+
.option("--plan-file <path>", "a tuned plan file (JSON) instead of the default")).action(async (o) => {
|
|
95
|
+
const model = modelOf(o);
|
|
96
|
+
if (!model)
|
|
97
|
+
throw new Error("review needs a model");
|
|
98
|
+
print(await review(readJson(o.round, Round), model, {
|
|
99
|
+
...(o.planFile ? { planFile: readJson(o.planFile, PlanFile) } : {}),
|
|
100
|
+
}));
|
|
101
|
+
});
|
|
102
|
+
program
|
|
103
|
+
.command("plans [kind]")
|
|
104
|
+
.description("a default plan file to copy and tune; no kind lists them all")
|
|
105
|
+
.action((kind) => {
|
|
106
|
+
if (kind)
|
|
107
|
+
print(defaultPlanFile(AskKind.parse(kind)));
|
|
108
|
+
else
|
|
109
|
+
print(PLAN_KINDS.map((k) => defaultPlanFile(k)));
|
|
110
|
+
});
|
|
111
|
+
program.parseAsync(process.argv).catch((e) => {
|
|
112
|
+
console.error(e instanceof Error ? e.message : String(e));
|
|
113
|
+
process.exit(1);
|
|
114
|
+
});
|
|
115
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { commandModel, fill, type Model, parseAnswer, scriptedModel } from "./model.js";
|
|
2
|
+
export { mergeProposals, type PlanInput, plan, propose, proposePrompt } from "./plan.js";
|
|
3
|
+
export { defaultPlanFile, PLAN_KINDS } from "./plans/index.js";
|
|
4
|
+
export { renderHits, renderYield } from "./render.js";
|
|
5
|
+
export { review, reviewPrompt } from "./review.js";
|
|
6
|
+
export { dedupeHits, triage, triagePrompt } from "./triage.js";
|
|
7
|
+
export * from "./types.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { commandModel, fill, parseAnswer, scriptedModel } from "./model.js";
|
|
2
|
+
export { mergeProposals, plan, propose, proposePrompt } from "./plan.js";
|
|
3
|
+
export { defaultPlanFile, PLAN_KINDS } from "./plans/index.js";
|
|
4
|
+
export { renderHits, renderYield } from "./render.js";
|
|
5
|
+
export { review, reviewPrompt } from "./review.js";
|
|
6
|
+
export { dedupeHits, triage, triagePrompt } from "./triage.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ZodType } from "zod";
|
|
2
|
+
export interface Model {
|
|
3
|
+
/** What goes in `by` on the records this model writes. */
|
|
4
|
+
id: string;
|
|
5
|
+
ask<T>(prompt: string, shape: ZodType<T>): Promise<T>;
|
|
6
|
+
}
|
|
7
|
+
/** `{name}` marks filled in one pass; a `{word}` with no value stays as text. */
|
|
8
|
+
export declare const fill: (template: string, values: Readonly<Record<string, string>>) => string;
|
|
9
|
+
/**
|
|
10
|
+
* The first JSON object in a model's text, parsed into the shape. Fences and prose around
|
|
11
|
+
* it are dropped. Throws with the model's text when nothing parses.
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseAnswer<T>(text: string, shape: ZodType<T>): T;
|
|
14
|
+
/**
|
|
15
|
+
* A model that is a shell command: the prompt on stdin, the answer on stdout. `claude -p`
|
|
16
|
+
* makes Claude Code the planner's model from any terminal.
|
|
17
|
+
*/
|
|
18
|
+
export declare function commandModel(command: string, id?: string): Model;
|
|
19
|
+
/** A model that answers from a fixed list, in order: tests and dry runs. */
|
|
20
|
+
export declare function scriptedModel(answers: unknown[], id?: string): Model;
|
package/dist/model.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model seam. Raven never picks a model: a caller hands in one `Model`, a function from a
|
|
3
|
+
* prompt and an answer shape to a parsed answer, and its id goes on every record it writes. In
|
|
4
|
+
* a terminal the model is a command that reads the prompt on stdin and prints the answer
|
|
5
|
+
* (`claude -p` by default); in wren it wraps the LLM client.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
/** `{name}` marks filled in one pass; a `{word}` with no value stays as text. */
|
|
9
|
+
export const fill = (template, values) => template.replace(/\{(\w+)\}/g, (mark, name) => values[name] ?? mark);
|
|
10
|
+
/**
|
|
11
|
+
* The first JSON object in a model's text, parsed into the shape. Fences and prose around
|
|
12
|
+
* it are dropped. Throws with the model's text when nothing parses.
|
|
13
|
+
*/
|
|
14
|
+
export function parseAnswer(text, shape) {
|
|
15
|
+
const start = text.indexOf("{");
|
|
16
|
+
if (start >= 0) {
|
|
17
|
+
for (let end = text.lastIndexOf("}"); end > start; end = text.lastIndexOf("}", end - 1)) {
|
|
18
|
+
try {
|
|
19
|
+
const out = shape.safeParse(JSON.parse(text.slice(start, end + 1)));
|
|
20
|
+
if (out.success)
|
|
21
|
+
return out.data;
|
|
22
|
+
}
|
|
23
|
+
catch { }
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
throw new Error(`no answer of the expected shape in: ${text.slice(0, 400)}`);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A model that is a shell command: the prompt on stdin, the answer on stdout. `claude -p`
|
|
30
|
+
* makes Claude Code the planner's model from any terminal.
|
|
31
|
+
*/
|
|
32
|
+
export function commandModel(command, id = command) {
|
|
33
|
+
return {
|
|
34
|
+
id,
|
|
35
|
+
ask: (prompt, shape) => new Promise((resolve, reject) => {
|
|
36
|
+
const child = spawn(command, { shell: true, stdio: ["pipe", "pipe", "inherit"] });
|
|
37
|
+
const out = [];
|
|
38
|
+
child.stdout.on("data", (b) => out.push(b));
|
|
39
|
+
child.on("error", reject);
|
|
40
|
+
child.on("close", (code) => {
|
|
41
|
+
const text = Buffer.concat(out).toString("utf8");
|
|
42
|
+
if (code !== 0)
|
|
43
|
+
return reject(new Error(`${command} exited ${code}: ${text.slice(0, 400)}`));
|
|
44
|
+
try {
|
|
45
|
+
resolve(parseAnswer(text, shape));
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
reject(e);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
child.stdin.end(prompt);
|
|
52
|
+
}),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** A model that answers from a fixed list, in order: tests and dry runs. */
|
|
56
|
+
export function scriptedModel(answers, id = "scripted") {
|
|
57
|
+
let i = 0;
|
|
58
|
+
return {
|
|
59
|
+
id,
|
|
60
|
+
ask: async (_prompt, shape) => {
|
|
61
|
+
const a = answers[i++];
|
|
62
|
+
if (a === undefined)
|
|
63
|
+
throw new Error(`scripted model has no answer ${i}`);
|
|
64
|
+
return shape.parse(a);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=model.js.map
|
package/dist/plan.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plan step: proposals in, one plan out. The model is one proposer; a person, a plan file
|
|
3
|
+
* or a second model are others, and all merge the same way. Human queries come first and never
|
|
4
|
+
* count against the budget: someone chose them.
|
|
5
|
+
*/
|
|
6
|
+
import { type Model } from "./model.js";
|
|
7
|
+
import { type Ask, type Plan, type PlanFile, type Proposal, type Query, type YieldRow } from "./types.js";
|
|
8
|
+
export declare function proposePrompt(ask: Ask, planFile: PlanFile, yieldRows: readonly YieldRow[], now: Date): string;
|
|
9
|
+
/** The model's proposal for an ask under a plan file. */
|
|
10
|
+
export declare function propose(ask: Ask, planFile: PlanFile, model: Model, yieldRows?: readonly YieldRow[], now?: Date): Promise<Proposal>;
|
|
11
|
+
/**
|
|
12
|
+
* One list from many proposals. Human proposals first, then the rest in the order given; the
|
|
13
|
+
* same terms twice keep the first. At most `cap` AI queries; human queries all stay.
|
|
14
|
+
*/
|
|
15
|
+
export declare function mergeProposals(proposals: readonly Proposal[], cap: number): Query[];
|
|
16
|
+
export interface PlanInput {
|
|
17
|
+
ask: Ask;
|
|
18
|
+
/** The default for the ask's kind when absent. */
|
|
19
|
+
planFile?: PlanFile;
|
|
20
|
+
/** No model: the plan is the proposals given, nothing proposed. */
|
|
21
|
+
model?: Model | null;
|
|
22
|
+
/** From people, plan files or other models. Merged with the model's. */
|
|
23
|
+
proposals?: readonly Proposal[];
|
|
24
|
+
yieldRows?: readonly YieldRow[];
|
|
25
|
+
/** Narrows the plan file's sources for this run. Names not in the plan file are dropped. */
|
|
26
|
+
sources?: readonly string[];
|
|
27
|
+
round?: number;
|
|
28
|
+
now?: () => Date;
|
|
29
|
+
}
|
|
30
|
+
/** The plan for one round: what to look for, where, under what budget, and what raven was given. */
|
|
31
|
+
export declare function plan(input: PlanInput): Promise<Plan>;
|
package/dist/plan.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plan step: proposals in, one plan out. The model is one proposer; a person, a plan file
|
|
3
|
+
* or a second model are others, and all merge the same way. Human queries come first and never
|
|
4
|
+
* count against the budget: someone chose them.
|
|
5
|
+
*/
|
|
6
|
+
import { fill } from "./model.js";
|
|
7
|
+
import { defaultPlanFile } from "./plans/index.js";
|
|
8
|
+
import { lines, renderYield } from "./render.js";
|
|
9
|
+
import { z, } from "./types.js";
|
|
10
|
+
const ProposalAnswer = z.object({
|
|
11
|
+
queries: z.array(z.object({ terms: z.string(), why: z.string(), site: z.string().optional() })),
|
|
12
|
+
});
|
|
13
|
+
export function proposePrompt(ask, planFile, yieldRows, now) {
|
|
14
|
+
return fill(planFile.propose, {
|
|
15
|
+
n: String(planFile.budget.queries),
|
|
16
|
+
year: String(now.getUTCFullYear()),
|
|
17
|
+
ask: ask.text,
|
|
18
|
+
site: ask.site ?? "",
|
|
19
|
+
known: lines(ask.known),
|
|
20
|
+
yield: renderYield(yieldRows),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** The model's proposal for an ask under a plan file. */
|
|
24
|
+
export async function propose(ask, planFile, model, yieldRows = [], now = new Date()) {
|
|
25
|
+
const out = await model.ask(proposePrompt(ask, planFile, yieldRows, now), ProposalAnswer);
|
|
26
|
+
return {
|
|
27
|
+
from: "ai",
|
|
28
|
+
by: model.id,
|
|
29
|
+
queries: out.queries.map((q) => {
|
|
30
|
+
const site = q.site ?? ask.site;
|
|
31
|
+
return { kind: ask.kind, terms: q.terms, why: q.why, ...(site ? { site } : {}) };
|
|
32
|
+
}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const key = (q) => `${q.site?.toLowerCase() ?? ""}|${q.terms.replace(/\s+/g, " ").trim().toLowerCase()}`;
|
|
36
|
+
/**
|
|
37
|
+
* One list from many proposals. Human proposals first, then the rest in the order given; the
|
|
38
|
+
* same terms twice keep the first. At most `cap` AI queries; human queries all stay.
|
|
39
|
+
*/
|
|
40
|
+
export function mergeProposals(proposals, cap) {
|
|
41
|
+
const ordered = [
|
|
42
|
+
...proposals.filter((p) => p.from === "human"),
|
|
43
|
+
...proposals.filter((p) => p.from !== "human"),
|
|
44
|
+
];
|
|
45
|
+
const seen = new Set();
|
|
46
|
+
const out = [];
|
|
47
|
+
let ai = 0;
|
|
48
|
+
for (const p of ordered) {
|
|
49
|
+
for (const q of p.queries) {
|
|
50
|
+
const terms = q.terms.replace(/\s+/g, " ").trim();
|
|
51
|
+
if (!terms || seen.has(key(q)))
|
|
52
|
+
continue;
|
|
53
|
+
if (p.from !== "human") {
|
|
54
|
+
if (ai >= cap)
|
|
55
|
+
continue;
|
|
56
|
+
ai++;
|
|
57
|
+
}
|
|
58
|
+
seen.add(key(q));
|
|
59
|
+
out.push({ ...q, terms, from: p.from, by: p.by });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/** The plan for one round: what to look for, where, under what budget, and what raven was given. */
|
|
65
|
+
export async function plan(input) {
|
|
66
|
+
const planFile = input.planFile ?? defaultPlanFile(input.ask.kind);
|
|
67
|
+
const now = input.now?.() ?? new Date();
|
|
68
|
+
const yieldRows = input.yieldRows ?? [];
|
|
69
|
+
const proposals = [...(input.proposals ?? [])];
|
|
70
|
+
if (input.model)
|
|
71
|
+
proposals.push(await propose(input.ask, planFile, input.model, yieldRows, now));
|
|
72
|
+
const sources = input.sources
|
|
73
|
+
? planFile.sources.filter((s) => input.sources?.includes(s))
|
|
74
|
+
: [...planFile.sources];
|
|
75
|
+
return {
|
|
76
|
+
ask: input.ask,
|
|
77
|
+
round: input.round ?? 1,
|
|
78
|
+
queries: mergeProposals(proposals, planFile.budget.queries),
|
|
79
|
+
sources,
|
|
80
|
+
budget: planFile.budget,
|
|
81
|
+
snapshot: {
|
|
82
|
+
at: now.toISOString(),
|
|
83
|
+
known: [...input.ask.known],
|
|
84
|
+
planFile: { id: planFile.id, version: planFile.version },
|
|
85
|
+
model: input.model?.id ?? null,
|
|
86
|
+
yieldRows: yieldRows.length,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=plan.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The default plan file per ask kind. A copy of one, tuned and stored beside a ledger, is how
|
|
3
|
+
* a run learns: the recipe changes, raven does not. `raven plans <kind>` prints one to start from.
|
|
4
|
+
*/
|
|
5
|
+
import { type AskKind, type PlanFile } from "../types.js";
|
|
6
|
+
/** The default plan file for a kind, validated, as a fresh copy. */
|
|
7
|
+
export declare function defaultPlanFile(kind: AskKind): PlanFile;
|
|
8
|
+
export declare const PLAN_KINDS: AskKind[];
|