@graview/tools 0.1.47 → 0.1.49

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.
Files changed (104) hide show
  1. package/README.md +31 -0
  2. package/dist/agent/tools.d.ts +5 -2
  3. package/dist/agent/tools.d.ts.map +1 -1
  4. package/dist/agent/tools.js +63 -16
  5. package/dist/agent/tools.js.map +1 -1
  6. package/dist/ai.d.ts +1 -1
  7. package/dist/ai.d.ts.map +1 -1
  8. package/dist/cli.d.ts +3 -0
  9. package/dist/cli.d.ts.map +1 -1
  10. package/dist/cli.js +87 -1
  11. package/dist/cli.js.map +1 -1
  12. package/dist/conversation.js +2 -2
  13. package/dist/conversation.js.map +1 -1
  14. package/dist/decide.d.ts +1 -1
  15. package/dist/decide.d.ts.map +1 -1
  16. package/dist/decide.js +2 -2
  17. package/dist/decide.js.map +1 -1
  18. package/dist/derive.d.ts.map +1 -1
  19. package/dist/derive.js +8 -1
  20. package/dist/derive.js.map +1 -1
  21. package/dist/dev-ai.d.ts +2 -1
  22. package/dist/dev-ai.d.ts.map +1 -1
  23. package/dist/dev-ai.js +8 -6
  24. package/dist/dev-ai.js.map +1 -1
  25. package/dist/draft.d.ts +3 -1
  26. package/dist/draft.d.ts.map +1 -1
  27. package/dist/draft.js +9 -8
  28. package/dist/draft.js.map +1 -1
  29. package/dist/fast.d.ts +44 -0
  30. package/dist/fast.d.ts.map +1 -0
  31. package/dist/fast.js +2 -0
  32. package/dist/fast.js.map +1 -0
  33. package/dist/figure.js +1 -1
  34. package/dist/figure.js.map +1 -1
  35. package/dist/index.d.ts +3 -2
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js.map +1 -1
  38. package/dist/inline-ai.d.ts.map +1 -1
  39. package/dist/inline-ai.js +1 -1
  40. package/dist/inline-ai.js.map +1 -1
  41. package/dist/intelligence.d.ts +32 -2
  42. package/dist/intelligence.d.ts.map +1 -1
  43. package/dist/intelligence.js +3 -3
  44. package/dist/intelligence.js.map +1 -1
  45. package/dist/keep.d.ts.map +1 -1
  46. package/dist/keep.js +8 -3
  47. package/dist/keep.js.map +1 -1
  48. package/dist/local.d.ts +1 -1
  49. package/dist/local.d.ts.map +1 -1
  50. package/dist/local.js +23 -18
  51. package/dist/local.js.map +1 -1
  52. package/dist/loop.d.ts +1 -1
  53. package/dist/loop.d.ts.map +1 -1
  54. package/dist/loop.js.map +1 -1
  55. package/dist/providers/jev.d.ts +1 -36
  56. package/dist/providers/jev.d.ts.map +1 -1
  57. package/dist/providers/jev.js.map +1 -1
  58. package/dist/questions.js +2 -2
  59. package/dist/questions.js.map +1 -1
  60. package/dist/run.d.ts +1 -1
  61. package/dist/run.d.ts.map +1 -1
  62. package/dist/seat-model.js +3 -3
  63. package/dist/seat-model.js.map +1 -1
  64. package/dist/suggest.d.ts +8 -8
  65. package/dist/suggest.d.ts.map +1 -1
  66. package/dist/suggest.js +2 -2
  67. package/dist/suggest.js.map +1 -1
  68. package/dist/tidy/asking.d.ts +51 -0
  69. package/dist/tidy/asking.d.ts.map +1 -0
  70. package/dist/tidy/asking.js +86 -0
  71. package/dist/tidy/asking.js.map +1 -0
  72. package/dist/tidy/away.d.ts +24 -0
  73. package/dist/tidy/away.d.ts.map +1 -0
  74. package/dist/tidy/away.js +24 -0
  75. package/dist/tidy/away.js.map +1 -0
  76. package/dist/tidy/blocking.d.ts +82 -0
  77. package/dist/tidy/blocking.d.ts.map +1 -0
  78. package/dist/tidy/blocking.js +260 -0
  79. package/dist/tidy/blocking.js.map +1 -0
  80. package/dist/tidy/dream.d.ts +117 -0
  81. package/dist/tidy/dream.d.ts.map +1 -0
  82. package/dist/tidy/dream.js +178 -0
  83. package/dist/tidy/dream.js.map +1 -0
  84. package/dist/tidy/judges.d.ts +109 -0
  85. package/dist/tidy/judges.d.ts.map +1 -0
  86. package/dist/tidy/judges.js +282 -0
  87. package/dist/tidy/judges.js.map +1 -0
  88. package/dist/tidy/judging.d.ts +51 -0
  89. package/dist/tidy/judging.d.ts.map +1 -0
  90. package/dist/tidy/judging.js +243 -0
  91. package/dist/tidy/judging.js.map +1 -0
  92. package/dist/tidy/policy.d.ts +18 -0
  93. package/dist/tidy/policy.d.ts.map +1 -0
  94. package/dist/tidy/policy.js +29 -0
  95. package/dist/tidy/policy.js.map +1 -0
  96. package/dist/tidy/system-one.d.ts +40 -0
  97. package/dist/tidy/system-one.d.ts.map +1 -0
  98. package/dist/tidy/system-one.js +49 -0
  99. package/dist/tidy/system-one.js.map +1 -0
  100. package/dist/tidy.d.ts +8 -0
  101. package/dist/tidy.d.ts.map +1 -0
  102. package/dist/tidy.js +23 -0
  103. package/dist/tidy.js.map +1 -0
  104. package/package.json +7 -3
@@ -0,0 +1,117 @@
1
+ import { type AnySchema, type Author, type Store, type Via } from "@graview/core";
2
+ import type { HostAi } from "../ai.js";
3
+ import { type DreamBudget, type DreamCache, type DreamUsage } from "./asking.js";
4
+ import { type Candidate, type Judge } from "./judges.js";
5
+ import { type Band, type Severity } from "./policy.js";
6
+ import { type DreamModel } from "./system-one.js";
7
+ /** "Graview, tidying": the app's own actor, as a run is made by unless a host names its own. */
8
+ export declare const TIDYING_ACTOR: Author;
9
+ export interface DreamOptions {
10
+ /** Who runs: an author that says `automated: true`. `TIDYING_ACTOR` when unsaid; a person is refused. */
11
+ readonly principal?: Author;
12
+ /** The judges to run, in order. Every one (`JUDGES`) when unsaid; a model judges only these judges' families. */
13
+ readonly judges?: readonly Judge[];
14
+ /** The System One model that judges, when the host lends one directly. */
15
+ readonly model?: DreamModel;
16
+ /** The host's AI, as a seat is handed it: its Fast slot (or `decide`) judges, and its `near` ranker widens the pairs. Ignored when it says `off`. */
17
+ readonly ai?: HostAi;
18
+ /** How sure each severity must be to be made unasked, and to be asked at all (`BANDS` when unsaid). */
19
+ readonly bands?: Partial<Record<Severity, Partial<Band>>>;
20
+ /** Requests at once. 6 when unsaid. */
21
+ readonly concurrency?: number;
22
+ /** What a run may spend on the model: 60 requests and 200,000 input tokens when unsaid. */
23
+ readonly budget?: DreamBudget;
24
+ /** Where answers are kept between runs, by what was asked; one cache per process when unsaid, none with `false`. */
25
+ readonly cache?: DreamCache | false;
26
+ /** How much blocking puts forward: pairs (40), records (60), far records a relation chooses among (8). */
27
+ readonly blocking?: {
28
+ readonly maxPairs?: number;
29
+ readonly maxRecords?: number;
30
+ readonly maxTargets?: number;
31
+ };
32
+ /** The run's moment. Now when unsaid. */
33
+ readonly now?: Date | string | number;
34
+ /** At most this many changes suggested or applied in one run, surest first. 20 when unsaid. */
35
+ readonly limit?: number;
36
+ /** A record changed within this many hours is left for now. 24 when unsaid. */
37
+ readonly quietHours?: number;
38
+ /** A record is a candidate to set aside after this many days unchanged. 30 when unsaid. */
39
+ readonly olderThanDays?: number;
40
+ /**
41
+ * THE HOST LETS THE ACTOR APPLY what it is at least this sure of (0 to 1)
42
+ * — and only what its severity's band says may be made unasked — with its
43
+ * lineage, where the declaration and what people said allow it; the store
44
+ * still refuses what they do not. Unsaid, nothing is applied: everything
45
+ * sure enough is suggested.
46
+ */
47
+ readonly applyAtLeast?: number;
48
+ /** What its changes come through, as the log records it. */
49
+ readonly via?: Via;
50
+ }
51
+ /** What became of one judgment. */
52
+ export interface DreamOutcome {
53
+ readonly judge: string;
54
+ /**
55
+ * suggested: waits for a person; applied: made, with lineage; dropped: not
56
+ * sure enough to ask, or the model chose none; waiting: already
57
+ * suggested; declined: a person said no before; refused: the store said
58
+ * no, in `error`.
59
+ */
60
+ readonly outcome: "suggested" | "applied" | "dropped" | "waiting" | "declined" | "refused";
61
+ readonly calls: Candidate["calls"];
62
+ readonly basis: string;
63
+ readonly confidence: number;
64
+ readonly about: readonly string[];
65
+ /** The model that judged it; absent when the judges' own rules did. */
66
+ readonly model?: string;
67
+ /** The op that holds a suggestion, or the batch that applied a change. */
68
+ readonly id?: string;
69
+ readonly error?: string;
70
+ }
71
+ /** What a run did, by judge, and in a sentence. */
72
+ export interface DreamReport {
73
+ /**
74
+ * The run, by its moment: on every change and suggestion it made, as
75
+ * `lineage.run`, so Activity reads a night's work as one.
76
+ */
77
+ readonly run: string;
78
+ readonly outcomes: readonly DreamOutcome[];
79
+ readonly byJudge: Readonly<Record<string, {
80
+ readonly suggested: number;
81
+ readonly applied: number;
82
+ readonly skipped: number;
83
+ }>>;
84
+ /**
85
+ * The model asked: how many requests it was put, how many it answered,
86
+ * and its failure when it failed — what it did not answer was judged by
87
+ * the judges' own rules.
88
+ */
89
+ readonly model?: {
90
+ readonly name: string;
91
+ readonly answered: boolean;
92
+ readonly asks: number;
93
+ readonly judged: number;
94
+ readonly error?: string;
95
+ };
96
+ /** What asking the model spent. */
97
+ readonly usage?: DreamUsage;
98
+ /** Said when the run's budget stopped the asking. */
99
+ readonly budget?: string;
100
+ /** Why nothing was applied, when nothing could be. */
101
+ readonly applying: string;
102
+ /** "Suggested 3 changes and applied 1." */
103
+ readonly said: string;
104
+ }
105
+ /** When each record was last changed, from the log. */
106
+ export declare function touchedIn(store: Pick<Store<AnySchema>, "log">): Map<string, number>;
107
+ /**
108
+ * RUN THE APP'S OWN ACTOR ONCE. Blocking and a model, when lent, judge
109
+ * the store as the actor sees it; the judges' rules judge what the model
110
+ * did not; each judgment is routed by its confidence and severity, surest
111
+ * first up to `limit`. A change a person declined is never asked again;
112
+ * one already waiting is not asked twice; whatever the store refuses is
113
+ * reported, not thrown. Call it on the store that keeps the log — a
114
+ * server's, for a hosted app.
115
+ */
116
+ export declare function dream<S extends AnySchema>(store: Store<S>, options?: DreamOptions): Promise<DreamReport>;
117
+ //# sourceMappingURL=dream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dream.d.ts","sourceRoot":"","sources":["../../src/tidy/dream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,KAAK,KAAK,EAAE,KAAK,GAAG,EAAE,MAAM,eAAe,CAAC;AAE/F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAA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AE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC,CAAC;IAC/H;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAChJ,mCAAmC;IACnC,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC;IAC5B,qDAAqD;IACrD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,2CAA2C;IAC3C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,uDAAuD;AACvD,wBAAgB,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAQnF;AAMD;;;;;;;;GAQG;AACH,wBAAsB,KAAK,CAAC,CAAC,SAAS,SAAS,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,WAAW,CAAC,CAwHlH"}
@@ -0,0 +1,178 @@
1
+ import { isAutomated } from "@graview/core";
2
+ import { declined, suggest, suggestionKey, suggestions } from "@graview/core/tidying";
3
+ import { askAll } from "./asking.js";
4
+ import { blocksOf } from "./blocking.js";
5
+ import { asksOf, coverOf } from "./judging.js";
6
+ import { JUDGES } from "./judges.js";
7
+ import { routeOf } from "./policy.js";
8
+ import { nearOf, systemOneOf } from "./system-one.js";
9
+ /*
10
+ * DREAMING: the app's own actor, run while nobody is looking — a host's
11
+ * nightly schedule, an alarm, `graview dream`. Code finds what is worth
12
+ * asking about, far more widely than an exact match (`./blocking.ts`); a
13
+ * System One model, when the host lends one, JUDGES each — the same thing
14
+ * twice? which of these does it mean? finished? which option fits? — in one
15
+ * request per candidate (`./judging.ts`); and code says what each answer
16
+ * is enough for, by how much the change would undo (`./policy.ts`): made,
17
+ * where the host let the actor apply that sure a change; suggested, to
18
+ * wait in the Suggested tray for a person; or let go. Without a model, or
19
+ * when it fails or the run's budget runs out, the judges' own rules are
20
+ * the floor. The declaration says where the actor may never act
21
+ * (`tidying`), never where it may act unasked, so applying is the host's
22
+ * word, not the app's, and off unless said.
23
+ */
24
+ /** "Graview, tidying": the app's own actor, as a run is made by unless a host names its own. */
25
+ export const TIDYING_ACTOR = { kind: "system", id: "tidying", name: "Graview, tidying", automated: true };
26
+ /** When each record was last changed, from the log. */
27
+ export function touchedIn(store) {
28
+ const touched = new Map();
29
+ for (const op of store.log.all()) {
30
+ const at = Date.parse(op.at);
31
+ if (Number.isNaN(at))
32
+ continue;
33
+ for (const id of op.writes)
34
+ touched.set(id, Math.max(at, touched.get(id) ?? 0));
35
+ }
36
+ return touched;
37
+ }
38
+ const plural = (count, one) => `${count === 1 ? "one" : count} ${one}${count === 1 ? "" : "s"}`;
39
+ /**
40
+ * RUN THE APP'S OWN ACTOR ONCE. Blocking and a model, when lent, judge
41
+ * the store as the actor sees it; the judges' rules judge what the model
42
+ * did not; each judgment is routed by its confidence and severity, surest
43
+ * first up to `limit`. A change a person declined is never asked again;
44
+ * one already waiting is not asked twice; whatever the store refuses is
45
+ * reported, not thrown. Call it on the store that keeps the log — a
46
+ * server's, for a hosted app.
47
+ */
48
+ export async function dream(store, options = {}) {
49
+ const whole = store;
50
+ const principal = options.principal ?? TIDYING_ACTOR;
51
+ if (!isAutomated(principal))
52
+ throw new Error("Only the app's own actor dreams: an author that says automated: true (a system or agent seat, never a person).");
53
+ const now = options.now === undefined ? Date.now() : new Date(options.now).getTime();
54
+ const context = {
55
+ store: whole.seenBy(principal),
56
+ now,
57
+ touched: touchedIn(whole),
58
+ quietFor: (options.quietHours ?? 24) * 60 * 60 * 1000,
59
+ olderThanDays: options.olderThanDays ?? 30,
60
+ };
61
+ const judges = options.judges ?? JUDGES;
62
+ // The model judges what blocking puts forward; what it answered is covered.
63
+ const judged = [];
64
+ const covered = new Set();
65
+ const model = systemOneOf(options.model, options.ai);
66
+ let modelSaid;
67
+ let usage;
68
+ let budget;
69
+ if (model) {
70
+ const blocks = await blocksOf(context, { families: new Set(judges.map((judge) => judge.name)), ...(nearOf(options.ai) ? { near: nearOf(options.ai) } : {}), ...(options.blocking ?? {}) });
71
+ const asks = asksOf(context.store, blocks);
72
+ const asked = await askAll(model, asks, {
73
+ ...(options.concurrency !== undefined ? { concurrency: options.concurrency } : {}),
74
+ ...(options.budget ? { budget: options.budget } : {}),
75
+ ...(options.cache !== undefined ? { cache: options.cache } : {}),
76
+ });
77
+ let answered = 0;
78
+ asks.forEach((ask, at) => {
79
+ const answers = asked.answers[at];
80
+ if (!answers)
81
+ return;
82
+ answered++;
83
+ for (const cover of ask.covers)
84
+ covered.add(cover);
85
+ judged.push(...ask.read(answers, model.model));
86
+ });
87
+ modelSaid = { name: model.model, answered: answered > 0, asks: asks.length, judged: answered, ...(asked.error !== undefined ? { error: asked.error } : {}) };
88
+ usage = asked.usage;
89
+ if (asked.stopped) {
90
+ const left = asks.length - answered;
91
+ budget = `Stopped at the run's ${asked.stopped === "requests" ? "request" : "token"} budget after ${plural(asked.usage.requests, "request")}; ${plural(left, "candidate")} ${left === 1 ? "was" : "were"} left to the judges' own rules.`;
92
+ }
93
+ }
94
+ // The judges' own rules, for whatever the model did not judge: the floor.
95
+ const floor = judges.flatMap((judge) => judge.judge(context).map((candidate) => ({ ...candidate, severity: judge.severity ?? "archive" }))).filter((candidate) => !covered.has(coverOf(candidate)));
96
+ const waiting = new Set(suggestions(whole).map((one) => suggestionKey(one.calls)));
97
+ const outcomes = [];
98
+ const said = (candidate, outcome, more = {}) => outcomes.push({ judge: candidate.judge, outcome, calls: candidate.calls, basis: candidate.basis, confidence: candidate.confidence, about: candidate.about, ...(candidate.model ? { model: candidate.model } : {}), ...more });
99
+ const fresh = [];
100
+ const seen = new Set();
101
+ for (const candidate of [...judged, ...floor]) {
102
+ if (candidate.none || candidate.calls.length === 0) {
103
+ said(candidate, "dropped");
104
+ continue;
105
+ }
106
+ const key = suggestionKey(candidate.calls);
107
+ if (seen.has(key))
108
+ continue;
109
+ seen.add(key);
110
+ if (declined(whole, candidate.calls))
111
+ said(candidate, "declined");
112
+ else if (waiting.has(key))
113
+ said(candidate, "waiting");
114
+ else {
115
+ const route = routeOf(candidate.confidence, candidate.severity, options.bands, candidate.split === true);
116
+ if (route === "drop")
117
+ said(candidate, "dropped");
118
+ else
119
+ fresh.push({ candidate, route, at: fresh.length });
120
+ }
121
+ }
122
+ const limit = options.limit ?? 20;
123
+ const ranked = [...fresh].sort((a, b) => b.candidate.confidence - a.candidate.confidence || a.at - b.at).slice(0, limit);
124
+ // A record merged away in this run is not merged, or merged into, again.
125
+ const mergedAway = new Set();
126
+ const run = new Date(now).toISOString();
127
+ for (const { candidate, route } of ranked) {
128
+ const merge = candidate.judge === "duplicates" ? candidate.calls[0]?.args : undefined;
129
+ if (merge && (mergedAway.has(String(merge.id)) || mergedAway.has(String(merge.into)))) {
130
+ said(candidate, "dropped", { error: "One of the two was already proposed to merge into another in this run." });
131
+ continue;
132
+ }
133
+ const lineage = { basis: candidate.basis, confidence: candidate.confidence, run, actor: candidate.judge, ...(candidate.model ? { model: candidate.model } : {}) };
134
+ try {
135
+ if (route === "act" && options.applyAtLeast !== undefined && candidate.confidence >= options.applyAtLeast) {
136
+ const result = whole.applyAll(candidate.calls, { author: principal, lineage, ...(options.via ? { via: options.via } : {}) });
137
+ said(candidate, "applied", { id: result.batch });
138
+ }
139
+ else {
140
+ const op = suggest(whole, candidate.calls, { author: principal, lineage, ...(options.via ? { via: options.via } : {}) });
141
+ if (op)
142
+ said(candidate, "suggested", { id: op.id });
143
+ }
144
+ if (merge)
145
+ mergedAway.add(String(merge.id));
146
+ }
147
+ catch (error) {
148
+ said(candidate, "refused", { error: error instanceof Error ? error.message : String(error) });
149
+ }
150
+ }
151
+ const byJudge = {};
152
+ for (const judge of judges)
153
+ byJudge[judge.name] = { suggested: 0, applied: 0, skipped: 0 };
154
+ for (const one of outcomes) {
155
+ const tally = (byJudge[one.judge] ??= { suggested: 0, applied: 0, skipped: 0 });
156
+ if (one.outcome === "suggested")
157
+ tally.suggested++;
158
+ else if (one.outcome === "applied")
159
+ tally.applied++;
160
+ else
161
+ tally.skipped++;
162
+ }
163
+ const suggested = outcomes.filter((one) => one.outcome === "suggested").length;
164
+ const applied = outcomes.filter((one) => one.outcome === "applied").length;
165
+ return {
166
+ run,
167
+ outcomes,
168
+ byJudge,
169
+ ...(modelSaid ? { model: modelSaid } : {}),
170
+ ...(usage ? { usage } : {}),
171
+ ...(budget ? { budget } : {}),
172
+ applying: options.applyAtLeast === undefined
173
+ ? "Nothing was applied: the host did not let the actor apply on its own (applyAtLeast), so every change sure enough to ask about was suggested."
174
+ : `Changes at least ${options.applyAtLeast} sure, and sure enough for how much they would undo, were applied where the app and its people allow it.`,
175
+ said: suggested + applied === 0 ? "Nothing to tidy." : [suggested > 0 ? `Suggested ${plural(suggested, "change")}` : "", applied > 0 ? `applied ${plural(applied, "change")}` : ""].filter(Boolean).join(" and ").replace(/^a/, "A") + ".",
176
+ };
177
+ }
178
+ //# sourceMappingURL=dream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dream.js","sourceRoot":"","sources":["../../src/tidy/dream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAqD,MAAM,eAAe,CAAC;AAC/F,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,uBAAuB,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,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,GAAG;KAC3O,CAAC;AACJ,CAAC"}
@@ -0,0 +1,109 @@
1
+ import { type AnySchema, type MutationCall, type Store } from "@graview/core";
2
+ import type { Severity } from "./policy.js";
3
+ type Node = {
4
+ readonly id: string;
5
+ readonly kind: string;
6
+ } & Record<string, unknown>;
7
+ /** One change a judge would make: the calls, what it is based on, and how sure (0 to 1). */
8
+ export interface Candidate {
9
+ /** The judge that found it: "duplicates", "links", "archive". */
10
+ readonly judge: string;
11
+ readonly calls: readonly MutationCall[];
12
+ /** Words a person reads: "Same name as “Acme” (another vendor)". */
13
+ readonly basis: string;
14
+ readonly confidence: number;
15
+ /** The records it is about, so a model and a report can name them. */
16
+ readonly about: readonly string[];
17
+ }
18
+ /** What a judge is handed: the store as the actor sees it, and the run's clock and limits. */
19
+ export interface JudgeContext {
20
+ readonly store: Store<AnySchema>;
21
+ /** The run's moment, in milliseconds. */
22
+ readonly now: number;
23
+ /** When each record was last changed, from the log; a record the log never touched is absent. */
24
+ readonly touched: ReadonlyMap<string, number>;
25
+ /** Records changed more recently than this many milliseconds are left for now. */
26
+ readonly quietFor: number;
27
+ /** How old, in days, a finished record is before it is a candidate to set aside. */
28
+ readonly olderThanDays: number;
29
+ }
30
+ /**
31
+ * A judge: a name, how much what it proposes would undo, and what it finds
32
+ * from the graph alone — the floor a dream stands on when no model answers.
33
+ * A model, when lent, judges the same things more broadly (`./judging.ts`).
34
+ */
35
+ export interface Judge {
36
+ readonly name: string;
37
+ /** How much its changes would undo, which sets how sure it must be (`BANDS`). "archive" when unsaid. */
38
+ readonly severity?: Severity;
39
+ judge(context: JudgeContext): readonly Candidate[];
40
+ }
41
+ /** Whether the actor may propose anything about this record: not left alone, not changed lately, its kind tidied. */
42
+ export declare function mayTouch(context: JudgeContext, node: Node): boolean;
43
+ /** A kind's acts as the store holds them, by name. */
44
+ export declare const actNamed: (store: Store<AnySchema>, name: string) => import("@graview/core").AnyMutationDefinition<AnySchema> | undefined;
45
+ export declare const named: (schema: AnySchema, node: Node) => string;
46
+ export declare const noun: (schema: AnySchema, kind: string) => string;
47
+ export declare const fieldsOf: (schema: AnySchema, kind: string) => string[];
48
+ /** Fields whose value names one thing and only that thing: two records that share one are the same. */
49
+ export declare const KEY_FIELD: RegExp;
50
+ /** The one of several that stays: the one with the most links and history, else the first made. */
51
+ export declare function keeperOf(store: Store<AnySchema>, nodes: readonly Node[], touched: ReadonlyMap<string, number>): Node;
52
+ /**
53
+ * DUPLICATES → MERGE. Records of one kind that share a name — case,
54
+ * accents and spacing aside, as an import matches a row to its record
55
+ * (`nameKey`) — or a key a record has only one of (an email, a code). The
56
+ * one with the most links stays; each other is merged into it
57
+ * (`merge-<kind>`). An exact name is surer than one that matches only with
58
+ * its punctuation aside; a shared key is surest.
59
+ */
60
+ export declare const duplicates: Judge;
61
+ /** The act that links a kind's records along a relation, and the argument the far record goes in: the derived connect, else a declared act that `connects` it. */
62
+ export declare function connectOf(store: Store<AnySchema>, kind: string, relation: string, targets: readonly string[]): {
63
+ readonly name: string;
64
+ readonly to: string;
65
+ } | undefined;
66
+ /**
67
+ * LINKS FROM SHARED VALUES. A record whose text names another record —
68
+ * a field that says exactly what the other is called, or names it among
69
+ * other words — is linked to it through a relation the declaration already
70
+ * has whose far end is that record's kind; a relation is never invented.
71
+ * A cardinality-one relation that already holds a link is left as it is;
72
+ * a name two records of the far kind share says nothing. An exact name is
73
+ * surer than one named in passing.
74
+ */
75
+ export declare const links: Judge;
76
+ /**
77
+ * HOW THE APP SAYS A RECORD IS FINISHED, from its declaration: its
78
+ * lifecycle (a value that retires it, or a date it expires after), else
79
+ * its main choice field read as the map reads it (`districtState`: a
80
+ * value like done, shipped, closed), else a yes/no field called done,
81
+ * completed or closed. Undefined when the kind says no such thing.
82
+ */
83
+ export declare function finishedOf(schema: AnySchema, kind: string, today: string): ((node: Node) => string | undefined) | undefined;
84
+ /** When a record last moved: the log's last change to it, else the date its declaration reads it by (a lifecycle date, an end or due role). */
85
+ export declare function lastMoved(schema: AnySchema, node: Node, touched: ReadonlyMap<string, number>): number | undefined;
86
+ /**
87
+ * DONE AND OLD → SET ASIDE. A record its kind says is finished, unchanged
88
+ * for longer than the run's `olderThanDays`, is a candidate to archive
89
+ * (`archive-<kind>`): out of lists and counts, its links and history kept,
90
+ * "Bring it back" one act away. Only kinds that archive; a record whose age
91
+ * nothing says is left.
92
+ */
93
+ export declare const archive: Judge;
94
+ /**
95
+ * AN EMPTY CHOICE, FILLED IN (`edit-<kind>`). Which of a field's options a
96
+ * record is takes reading it, so the graph alone proposes nothing: only a
97
+ * model, when lent, judges it (`./judging.ts`).
98
+ */
99
+ export declare const categorize: Judge;
100
+ /**
101
+ * AN EMPTY ONE-LINK RELATION, FILLED IN. Without a model the floor is the
102
+ * `links` judge's — a far record the record's own text names; a model, when
103
+ * lent, chooses among the likeliest records of the far kind, or none.
104
+ */
105
+ export declare const missingLinks: Judge;
106
+ /** The judges a run uses unless told otherwise. */
107
+ export declare const JUDGES: readonly Judge[];
108
+ export {};
109
+ //# sourceMappingURL=judges.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"judges.d.ts","sourceRoot":"","sources":["../../src/tidy/judges.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6E,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,KAAK,KAAK,EAAE,MAAM,eAAe,CAAC;AAIzJ,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAa5C,KAAK,IAAI,GAAG;IAAE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAErF,4FAA4F;AAC5F,MAAM,WAAW,SAAS;IACxB,iEAAiE;IACjE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,SAAS,YAAY,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