@yaks/session 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +1392 -2
- package/_dist/admission.d.ts +15 -0
- package/_dist/admission.d.ts.map +1 -0
- package/_dist/announce.d.ts +17 -0
- package/_dist/announce.d.ts.map +1 -0
- package/_dist/answer.d.ts +200 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/append.d.ts +10 -0
- package/_dist/append.d.ts.map +1 -0
- package/_dist/archive.d.ts +6 -0
- package/_dist/archive.d.ts.map +1 -0
- package/_dist/asks.d.ts +11 -0
- package/_dist/asks.d.ts.map +1 -0
- package/_dist/audit.d.ts +15 -0
- package/_dist/audit.d.ts.map +1 -0
- package/_dist/bounce.d.ts +19 -0
- package/_dist/bounce.d.ts.map +1 -0
- package/_dist/bus.d.ts +8 -0
- package/_dist/bus.d.ts.map +1 -0
- package/_dist/cache.d.ts +45 -0
- package/_dist/cache.d.ts.map +1 -0
- package/_dist/child-notice.d.ts +6 -0
- package/_dist/child-notice.d.ts.map +1 -0
- package/_dist/children.d.ts +59 -0
- package/_dist/children.d.ts.map +1 -0
- package/_dist/cli.d.ts +8 -0
- package/_dist/cli.d.ts.map +1 -0
- package/_dist/comp.d.ts +9 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/compact.d.ts +19 -0
- package/_dist/compact.d.ts.map +1 -0
- package/_dist/conversation.d.ts +158 -0
- package/_dist/conversation.d.ts.map +1 -0
- package/_dist/cost.d.ts +13 -0
- package/_dist/cost.d.ts.map +1 -0
- package/_dist/deferred.d.ts +79 -0
- package/_dist/deferred.d.ts.map +1 -0
- package/_dist/ended.d.ts +27 -0
- package/_dist/ended.d.ts.map +1 -0
- package/_dist/files.d.ts +73 -0
- package/_dist/files.d.ts.map +1 -0
- package/_dist/fork.d.ts +23 -0
- package/_dist/fork.d.ts.map +1 -0
- package/_dist/graft.d.ts +33 -0
- package/_dist/graft.d.ts.map +1 -0
- package/_dist/graph.d.ts +14 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/handoff.d.ts +29 -0
- package/_dist/handoff.d.ts.map +1 -0
- package/_dist/imported.d.ts +63 -0
- package/_dist/imported.d.ts.map +1 -0
- package/_dist/intake.d.ts +43 -0
- package/_dist/intake.d.ts.map +1 -0
- package/_dist/joined.d.ts +35 -0
- package/_dist/joined.d.ts.map +1 -0
- package/_dist/lease.d.ts +21 -0
- package/_dist/lease.d.ts.map +1 -0
- package/_dist/lines.d.ts +13 -0
- package/_dist/lines.d.ts.map +1 -0
- package/_dist/mod.d.ts +107 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/naming.d.ts +10 -0
- package/_dist/naming.d.ts.map +1 -0
- package/_dist/native.d.ts +13 -0
- package/_dist/native.d.ts.map +1 -0
- package/_dist/parts.d.ts +145 -0
- package/_dist/parts.d.ts.map +1 -0
- package/_dist/plugin.d.ts +33 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/project.d.ts +67 -0
- package/_dist/project.d.ts.map +1 -0
- package/_dist/prose.d.ts +7 -0
- package/_dist/prose.d.ts.map +1 -0
- package/_dist/providers.d.ts +36 -0
- package/_dist/providers.d.ts.map +1 -0
- package/_dist/reach.d.ts +39 -0
- package/_dist/reach.d.ts.map +1 -0
- package/_dist/react.d.ts +236 -0
- package/_dist/react.d.ts.map +1 -0
- package/_dist/readers.d.ts +221 -0
- package/_dist/readers.d.ts.map +1 -0
- package/_dist/reap.d.ts +21 -0
- package/_dist/reap.d.ts.map +1 -0
- package/_dist/recall.d.ts +32 -0
- package/_dist/recall.d.ts.map +1 -0
- package/_dist/routes.d.ts +15 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/run.d.ts +81 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/service.d.ts +131 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/source.d.ts +9 -0
- package/_dist/source.d.ts.map +1 -0
- package/_dist/start.d.ts +29 -0
- package/_dist/start.d.ts.map +1 -0
- package/_dist/state.d.ts +75 -0
- package/_dist/state.d.ts.map +1 -0
- package/_dist/status.d.ts +38 -0
- package/_dist/status.d.ts.map +1 -0
- package/_dist/tail.d.ts +121 -0
- package/_dist/tail.d.ts.map +1 -0
- package/_dist/timing.d.ts +9 -0
- package/_dist/timing.d.ts.map +1 -0
- package/_dist/told.d.ts +17 -0
- package/_dist/told.d.ts.map +1 -0
- package/_dist/tools.d.ts +41 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/transcript.d.ts +5 -0
- package/_dist/transcript.d.ts.map +1 -0
- package/_dist/turn.d.ts +49 -0
- package/_dist/turn.d.ts.map +1 -0
- package/_dist/unknown.d.ts +6 -0
- package/_dist/unknown.d.ts.map +1 -0
- package/_dist/views.d.ts +9 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +9 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/wait.d.ts +15 -0
- package/_dist/wait.d.ts.map +1 -0
- package/_dist/wake.d.ts +116 -0
- package/_dist/wake.d.ts.map +1 -0
- package/_dist/watch.d.ts +52 -0
- package/_dist/watch.d.ts.map +1 -0
- package/_dist/who.d.ts +84 -0
- package/_dist/who.d.ts.map +1 -0
- package/_dist/window.d.ts +43 -0
- package/_dist/window.d.ts.map +1 -0
- package/admission.js +137 -0
- package/admission.js.map +1 -0
- package/admission.ts +164 -0
- package/announce.js +123 -0
- package/announce.js.map +1 -0
- package/announce.ts +126 -0
- package/answer.js +372 -0
- package/answer.js.map +1 -0
- package/answer.ts +426 -0
- package/append.js +125 -0
- package/append.js.map +1 -0
- package/append.ts +205 -0
- package/archive.js +62 -0
- package/archive.js.map +1 -0
- package/archive.ts +62 -0
- package/asks.js +80 -0
- package/asks.js.map +1 -0
- package/asks.ts +106 -0
- package/attempt.js +46 -0
- package/attempt.js.map +1 -0
- package/attempt.ts +60 -0
- package/audit.js +57 -0
- package/audit.js.map +1 -0
- package/audit.ts +65 -0
- package/bounce.js +29 -0
- package/bounce.js.map +1 -0
- package/bounce.ts +34 -0
- package/bus.js +568 -0
- package/bus.js.map +1 -0
- package/bus.ts +683 -0
- package/cache.js +169 -0
- package/cache.js.map +1 -0
- package/cache.ts +230 -0
- package/child-notice.js +128 -0
- package/child-notice.js.map +1 -0
- package/child-notice.ts +136 -0
- package/children.js +937 -0
- package/children.js.map +1 -0
- package/children.ts +925 -0
- package/claude-control.js +8 -0
- package/claude-control.js.map +1 -0
- package/claude-control.ts +12 -0
- package/cli.js +23 -0
- package/cli.js.map +1 -0
- package/cli.ts +28 -0
- package/comp.js +76 -0
- package/comp.js.map +1 -0
- package/comp.ts +78 -0
- package/compact.js +157 -0
- package/compact.js.map +1 -0
- package/compact.ts +209 -0
- package/compact_ask.js +280 -0
- package/compact_ask.js.map +1 -0
- package/compact_ask.ts +199 -0
- package/continue.js +131 -0
- package/continue.js.map +1 -0
- package/continue.ts +143 -0
- package/conversation.js +337 -0
- package/conversation.js.map +1 -0
- package/conversation.ts +593 -0
- package/cost.js +111 -0
- package/cost.js.map +1 -0
- package/cost.ts +93 -0
- package/deferred.js +207 -0
- package/deferred.js.map +1 -0
- package/deferred.ts +219 -0
- package/deno.json +1 -0
- package/dependencies.js +114 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +110 -0
- package/draining.js +20 -0
- package/draining.js.map +1 -0
- package/draining.ts +26 -0
- package/ended.js +210 -0
- package/ended.js.map +1 -0
- package/ended.ts +225 -0
- package/files.js +285 -0
- package/files.js.map +1 -0
- package/files.ts +318 -0
- package/fork.js +96 -0
- package/fork.js.map +1 -0
- package/fork.ts +124 -0
- package/graft.js +97 -0
- package/graft.js.map +1 -0
- package/graft.ts +88 -0
- package/graph.js +49 -0
- package/graph.js.map +1 -0
- package/graph.ts +61 -0
- package/handoff.js +111 -0
- package/handoff.js.map +1 -0
- package/handoff.ts +147 -0
- package/help.js +432 -0
- package/help.js.map +1 -0
- package/help.ts +431 -0
- package/hooks.js +253 -0
- package/hooks.js.map +1 -0
- package/hooks.ts +333 -0
- package/imported.js +259 -0
- package/imported.js.map +1 -0
- package/imported.ts +201 -0
- package/inspect.js +353 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +378 -0
- package/intake.js +115 -0
- package/intake.js.map +1 -0
- package/intake.ts +124 -0
- package/joined.js +67 -0
- package/joined.js.map +1 -0
- package/joined.ts +69 -0
- package/lease.js +146 -0
- package/lease.js.map +1 -0
- package/lease.ts +151 -0
- package/lifecycle.js +49 -0
- package/lifecycle.js.map +1 -0
- package/lifecycle.ts +46 -0
- package/lines.js +171 -0
- package/lines.js.map +1 -0
- package/lines.ts +177 -0
- package/listen.js +604 -0
- package/listen.js.map +1 -0
- package/listen.ts +976 -0
- package/log.js +78 -0
- package/log.js.map +1 -0
- package/log.ts +88 -0
- package/mod.js +107 -0
- package/mod.js.map +1 -0
- package/mod.ts +113 -0
- package/name.js +51 -0
- package/name.js.map +1 -0
- package/name.ts +84 -0
- package/naming.js +96 -0
- package/naming.js.map +1 -0
- package/naming.ts +102 -0
- package/native.js +17 -0
- package/native.js.map +1 -0
- package/native.ts +17 -0
- package/package.json +133 -4
- package/parts.js +334 -0
- package/parts.js.map +1 -0
- package/parts.ts +383 -0
- package/plugin.js +77 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +98 -0
- package/project.js +354 -0
- package/project.js.map +1 -0
- package/project.ts +407 -0
- package/prompt.js +57 -0
- package/prompt.js.map +1 -0
- package/prompt.ts +92 -0
- package/prose.js +43 -0
- package/prose.js.map +1 -0
- package/prose.ts +63 -0
- package/providers.js +279 -0
- package/providers.js.map +1 -0
- package/providers.ts +306 -0
- package/pruning.js +80 -0
- package/pruning.js.map +1 -0
- package/pruning.ts +147 -0
- package/reach.js +923 -0
- package/reach.js.map +1 -0
- package/reach.ts +1107 -0
- package/react.js +2259 -0
- package/react.js.map +1 -0
- package/react.ts +2864 -0
- package/readers.js +956 -0
- package/readers.js.map +1 -0
- package/readers.ts +1017 -0
- package/reap.js +60 -0
- package/reap.js.map +1 -0
- package/reap.ts +65 -0
- package/recall.js +67 -0
- package/recall.js.map +1 -0
- package/recall.ts +114 -0
- package/reopen.js +45 -0
- package/reopen.js.map +1 -0
- package/reopen.ts +44 -0
- package/result-entry.js +32 -0
- package/result-entry.js.map +1 -0
- package/result-entry.ts +34 -0
- package/resume.js +266 -0
- package/resume.js.map +1 -0
- package/resume.ts +283 -0
- package/routes.js +129 -0
- package/routes.js.map +1 -0
- package/routes.ts +147 -0
- package/routine.js +179 -0
- package/routine.js.map +1 -0
- package/routine.ts +191 -0
- package/run.js +932 -0
- package/run.js.map +1 -0
- package/run.ts +999 -0
- package/service.js +806 -0
- package/service.js.map +1 -0
- package/service.ts +911 -0
- package/source.js +26 -0
- package/source.js.map +1 -0
- package/source.ts +40 -0
- package/start.js +197 -0
- package/start.js.map +1 -0
- package/start.ts +214 -0
- package/state.js +247 -0
- package/state.js.map +1 -0
- package/state.ts +404 -0
- package/status.js +1222 -0
- package/status.js.map +1 -0
- package/status.ts +1555 -0
- package/status_bench.ts +50 -0
- package/statusline.js +187 -0
- package/statusline.js.map +1 -0
- package/statusline.ts +232 -0
- package/tail.js +748 -0
- package/tail.js.map +1 -0
- package/tail.ts +815 -0
- package/talk.js +58 -0
- package/talk.js.map +1 -0
- package/talk.ts +51 -0
- package/task-context.js +41 -0
- package/task-context.js.map +1 -0
- package/task-context.ts +53 -0
- package/texted.js +65 -0
- package/texted.js.map +1 -0
- package/texted.ts +65 -0
- package/timing.js +37 -0
- package/timing.js.map +1 -0
- package/timing.ts +52 -0
- package/told.js +56 -0
- package/told.js.map +1 -0
- package/told.ts +54 -0
- package/tools.js +595 -0
- package/tools.js.map +1 -0
- package/tools.ts +633 -0
- package/transcript.js +34 -0
- package/transcript.js.map +1 -0
- package/transcript.ts +41 -0
- package/turn.js +47 -0
- package/turn.js.map +1 -0
- package/turn.ts +68 -0
- package/unknown.js +8 -0
- package/unknown.js.map +1 -0
- package/unknown.ts +9 -0
- package/views.js +15 -0
- package/views.js.map +1 -0
- package/views.ts +21 -0
- package/vocab.js +24 -0
- package/vocab.js.map +1 -0
- package/vocab.json +1573 -0
- package/vocab.ts +30 -0
- package/wait.js +30 -0
- package/wait.js.map +1 -0
- package/wait.ts +45 -0
- package/wake.js +552 -0
- package/wake.js.map +1 -0
- package/wake.ts +597 -0
- package/watch.js +520 -0
- package/watch.js.map +1 -0
- package/watch.ts +596 -0
- package/who.js +309 -0
- package/who.js.map +1 -0
- package/who.ts +347 -0
- package/window.js +179 -0
- package/window.js.map +1 -0
- package/window.ts +268 -0
- package/woken.js +129 -0
- package/woken.js.map +1 -0
- package/woken.ts +127 -0
package/answer.ts
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
// A turn's answer: what a transcript ended with, in plain words, and which
|
|
2
|
+
// of the words given to it that answers. A host sends the answer back the way
|
|
3
|
+
// each of those words came (on yaks.app, by the channel its request came by);
|
|
4
|
+
// the runner tells it once a transcript ends (./run.ts `Runner.ended`).
|
|
5
|
+
//
|
|
6
|
+
// The words a turn answers are each input given since the reply before it. A
|
|
7
|
+
// helper's report stands for the words of the turn that started it, so what
|
|
8
|
+
// the helper found goes back where they came from, not to the helper.
|
|
9
|
+
|
|
10
|
+
import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
11
|
+
import { dry } from '@yaks/model'
|
|
12
|
+
import { refusalText } from '@yaks/tools/errors'
|
|
13
|
+
import { kindOf, LIMIT, ordered, textOf } from './state.js'
|
|
14
|
+
import { PASS } from './turn.js'
|
|
15
|
+
import { transcriptBack } from './window.js'
|
|
16
|
+
|
|
17
|
+
let comp = (b: Bundle | undefined, name: string) =>
|
|
18
|
+
b?.[name] as Comp | undefined
|
|
19
|
+
|
|
20
|
+
/** Words given to a session. A notice the runner writes with prose, as a
|
|
21
|
+
* compaction's checkpoint, is no one's words, so it never starts a turn. */
|
|
22
|
+
export let given = (b: Bundle): boolean => kindOf(b) == 'input' && !b.notice
|
|
23
|
+
|
|
24
|
+
/** A reply: what a model said, which answers the words given before it. */
|
|
25
|
+
export let said = (b: Bundle): boolean =>
|
|
26
|
+
kindOf(b) == 'output' && !!b.content && !b.notice
|
|
27
|
+
|
|
28
|
+
// Where the reply at `i` stopped hearing, as an end to slice before: past the
|
|
29
|
+
// last line its ask was sent (`ask{through}`, as ./state.ts reads a turn's
|
|
30
|
+
// status), since words given while it was being written are the next
|
|
31
|
+
// reply's to answer. Undefined where that lies before the lines read
|
|
32
|
+
// (`whole` false).
|
|
33
|
+
let heardTo = (entries: Bundle[], i: number, whole: boolean) => {
|
|
34
|
+
let source = comp(entries[i], 'output')?.source
|
|
35
|
+
if (!source) return i
|
|
36
|
+
let ask = entries.find((b) => b.entity.eid == source)
|
|
37
|
+
let through = comp(ask, 'ask')?.through
|
|
38
|
+
if (!through) return ask || whole ? i : undefined
|
|
39
|
+
let at = entries.findIndex((b) => b.entity.eid == through)
|
|
40
|
+
return at >= 0 ? at + 1 : whole ? i : undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The inputs a turn ending at `end` answers: each line given since the reply
|
|
45
|
+
* before it, with a helper's report standing for the inputs of the turn whose
|
|
46
|
+
* call started it. A reply hears up to the ask that wrote it: words given
|
|
47
|
+
* while it was being written are left to the next. `calls` names that call for each child
|
|
48
|
+
* (`spawned{parent, call}`). Of a transcript's newest lines alone (`whole`
|
|
49
|
+
* false), it is undefined where the inputs lie before them.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { equal } from '@yaks/testing'
|
|
53
|
+
* import { origins } from './answer.ts'
|
|
54
|
+
* let line = (eid: string, b: object) => ({ entity: { eid }, ...b })
|
|
55
|
+
* let lines = [
|
|
56
|
+
* line('a', { content: { body: 'Look into it' } }),
|
|
57
|
+
* line('c', { call: { to: 'spawn' } }),
|
|
58
|
+
* line('r', { content: { body: 'On it.' }, output: {} }),
|
|
59
|
+
* line('d', { content: { body: 'Found it' }, delivery: { child: 'h' } }),
|
|
60
|
+
* ]
|
|
61
|
+
* equal(origins(lines, 4, new Map([['h', 'c']]))?.map((b) => b.entity.eid), ['a'])
|
|
62
|
+
* equal(origins(lines.slice(2), 2, new Map(), false), undefined)
|
|
63
|
+
* let late = [
|
|
64
|
+
* line('a', { content: { body: 'Write the guide' } }),
|
|
65
|
+
* line('k', { ask: { through: 'a' } }),
|
|
66
|
+
* line('b', { content: { body: 'And the logo?' } }),
|
|
67
|
+
* line('g', { content: { body: 'The guide' }, output: { source: 'k' } }),
|
|
68
|
+
* line('k2', { ask: { through: 'g' } }),
|
|
69
|
+
* line('l', { content: { body: 'Logo set' }, output: { source: 'k2' } }),
|
|
70
|
+
* ]
|
|
71
|
+
* equal(origins(late, 3, new Map())?.map((b) => b.entity.eid), ['a'])
|
|
72
|
+
* equal(origins(late, 5, new Map())?.map((b) => b.entity.eid), ['b'])
|
|
73
|
+
* let resumed = [
|
|
74
|
+
* line('a', { content: { body: 'Fix the app' } }),
|
|
75
|
+
* line('k', { ask: { through: 'a' } }),
|
|
76
|
+
* line('w', { notice: {}, output: { source: 'k' }, content: { body: 'Waiting for funds' } }),
|
|
77
|
+
* line('r', { output: { source: 'k' }, content: { body: 'Fixed' } }),
|
|
78
|
+
* ]
|
|
79
|
+
* equal(origins(resumed, 3, new Map())?.map((b) => b.entity.eid), ['a'])
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export let origins = (
|
|
83
|
+
entries: Bundle[],
|
|
84
|
+
end: number,
|
|
85
|
+
calls: Map<string, string>,
|
|
86
|
+
whole = true,
|
|
87
|
+
): Bundle[] | undefined => {
|
|
88
|
+
let from = entries.findLastIndex((b, i) => i < end && said(b))
|
|
89
|
+
if (from < 0 && !whole) return
|
|
90
|
+
let start = from < 0 ? 0 : heardTo(entries, from, whole)
|
|
91
|
+
let stop = end < entries.length && said(entries[end])
|
|
92
|
+
? heardTo(entries, end, whole)
|
|
93
|
+
: end
|
|
94
|
+
if (start == undefined || stop == undefined) return
|
|
95
|
+
let found: Bundle[] = []
|
|
96
|
+
for (let b of entries.slice(start, stop).filter(given)) {
|
|
97
|
+
let child = comp(b, 'delivery')?.child
|
|
98
|
+
if (!child) {
|
|
99
|
+
found.push(b)
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
let call = entries.findIndex((c) =>
|
|
103
|
+
c.entity.eid == calls.get(String(child))
|
|
104
|
+
)
|
|
105
|
+
if (call < 0 && !whole) return
|
|
106
|
+
let more = call < 0 ? [] : origins(entries, call, calls, whole)
|
|
107
|
+
if (!more) return
|
|
108
|
+
found.push(...more)
|
|
109
|
+
}
|
|
110
|
+
return found
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// The inputs the newest turn answers, the newest of the words its reply heard
|
|
114
|
+
// (`mark`), and where it ends (`through`, past its reply). The turn ends at
|
|
115
|
+
// its reply, or where the transcript does when it ended without one (stopped,
|
|
116
|
+
// failed). A reply said while newer words wait (`replied`) ends its turn
|
|
117
|
+
// however late those words were written: before the reply or after it, they
|
|
118
|
+
// are the next turn's. Undefined where they lie before the lines read
|
|
119
|
+
// (`whole` false).
|
|
120
|
+
let turnAt = (
|
|
121
|
+
entries: Bundle[],
|
|
122
|
+
calls: Map<string, string>,
|
|
123
|
+
whole = true,
|
|
124
|
+
replied = false,
|
|
125
|
+
) => {
|
|
126
|
+
let newest = entries.findLastIndex(given)
|
|
127
|
+
if (newest < 0) {
|
|
128
|
+
return whole
|
|
129
|
+
? { mark: undefined, given: [], through: entries.length }
|
|
130
|
+
: undefined
|
|
131
|
+
}
|
|
132
|
+
let reply = entries.findLastIndex(said)
|
|
133
|
+
let end = reply > newest || replied && reply >= 0 ? reply : entries.length
|
|
134
|
+
let heard = end < entries.length ? heardTo(entries, end, whole) : end
|
|
135
|
+
let found = origins(entries, end, calls, whole)
|
|
136
|
+
if (!found || heard == undefined) return
|
|
137
|
+
let mark = entries.findLast((b, i) => i < heard && given(b))
|
|
138
|
+
return { mark, given: found, through: Math.min(end + 1, entries.length) }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The entries since the newest words given: the turn being answered.
|
|
143
|
+
*
|
|
144
|
+
* ```ts
|
|
145
|
+
* import { equal } from '@yaks/testing'
|
|
146
|
+
* import { replyOf, turnOf } from './answer.ts'
|
|
147
|
+
* let line = (seq: number, b: object) => ({
|
|
148
|
+
* entity: { eid: `e${seq}` }, entry: { session: 's', seq }, ...b,
|
|
149
|
+
* })
|
|
150
|
+
* let stopped = turnOf([
|
|
151
|
+
* line(1, { content: { body: 'Check the app' } }),
|
|
152
|
+
* line(2, { stop: {}, content: { body: 'Stopped by the person watching.' } }),
|
|
153
|
+
* line(3, { notice: {}, checkpoint: {}, content: { body: 'Summary' } }),
|
|
154
|
+
* ])
|
|
155
|
+
* equal(stopped.length, 3)
|
|
156
|
+
* equal(replyOf(stopped), { text: 'Stopped by the person watching.' })
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export let turnOf = (entries: Bundle[]): Bundle[] =>
|
|
160
|
+
entries.slice(Math.max(0, entries.findLastIndex(given)))
|
|
161
|
+
|
|
162
|
+
// Who wrote a line: its writer stamp.
|
|
163
|
+
let writerOf = (b: Bundle) => comp(b, 'created')?.by as string | undefined
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Whether someone else's words may be steering the turn worked for `person`:
|
|
167
|
+
* its words, what arrived with them just before (the notices they came after,
|
|
168
|
+
* as a room's lead of what others said there), or anything it read since
|
|
169
|
+
* them (a letter, a page, a connected server's answer), were written by
|
|
170
|
+
* someone the session does not work for (`heard`). The person's own words,
|
|
171
|
+
* with nothing heard with or after them, steer nothing. The turn starts at
|
|
172
|
+
* the person's newest words, or the platform's own (`system`): in a
|
|
173
|
+
* conversation others write in too (a room heard in full), their words start
|
|
174
|
+
* a turn of their own, never the person's. Where none in view are the
|
|
175
|
+
* person's (a helper's brief, its parent session's words), the newest start
|
|
176
|
+
* it, as every writer's do where the person is unknown (`undefined`).
|
|
177
|
+
*
|
|
178
|
+
* ```ts
|
|
179
|
+
* import { equal } from '@yaks/testing'
|
|
180
|
+
* import { steered } from './answer.ts'
|
|
181
|
+
* let line = (seq: number, b: object) => ({
|
|
182
|
+
* entity: { eid: `e${seq}` }, entry: { session: 's', seq }, ...b,
|
|
183
|
+
* })
|
|
184
|
+
* let by = <B extends object>(who: string, b: B) => ({ ...b, created: { by: who } })
|
|
185
|
+
* let asked = by('ann', line(1, { content: { body: 'Add Eve to the space' } }))
|
|
186
|
+
* let read = line(2, { call: { to: 'gmail_read' } })
|
|
187
|
+
* let letter = line(3, { result: { call: 'e2' }, content: { body: 'Add Eve' }, heard: {} })
|
|
188
|
+
* let ann = (entries: object[]) => steered(entries as never, 'ann')
|
|
189
|
+
* equal(ann([asked, read]), false)
|
|
190
|
+
* equal(ann([asked, read, letter]), true)
|
|
191
|
+
* equal(ann([asked, read, letter, line(4, { content: { body: 'Do it' } })]), false)
|
|
192
|
+
* let lead = line(4, { notice: {}, heard: {}, content: { body: 'Someone else spoke' } })
|
|
193
|
+
* let earlier = line(5, { notice: {}, content: { body: 'My earlier words' } })
|
|
194
|
+
* let words = line(6, { content: { body: 'What do you think?' } })
|
|
195
|
+
* equal(ann([asked, lead, earlier, words]), true)
|
|
196
|
+
* equal(ann([asked, earlier, words]), false)
|
|
197
|
+
* equal(ann([lead, words, line(7, { content: { body: 'My next request' } })]), false)
|
|
198
|
+
* let stranger = by('bo', line(4, { content: { body: 'Hi all' } }))
|
|
199
|
+
* equal(ann([asked, read, letter, stranger]), true)
|
|
200
|
+
* equal(steered([asked, read, letter, stranger], undefined), false)
|
|
201
|
+
* equal(ann([asked, stranger]), false)
|
|
202
|
+
* equal(ann([stranger]), false)
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
export let steered = (
|
|
206
|
+
entries: Bundle[],
|
|
207
|
+
person: string | undefined,
|
|
208
|
+
): boolean => {
|
|
209
|
+
let theirs = (b: Bundle) =>
|
|
210
|
+
given(b) && (!person || !!b.system || (writerOf(b) ?? person) == person)
|
|
211
|
+
let start = entries.findLastIndex(theirs)
|
|
212
|
+
if (start < 0) start = entries.findLastIndex(given)
|
|
213
|
+
while (start > 0 && entries[start - 1].notice) start--
|
|
214
|
+
return entries.slice(Math.max(0, start)).some((b) => !!b.heard)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Whom the turn a `call` was made in works for: who wrote the newest words
|
|
219
|
+
* its ask read (`ask{through}`). Unknown where `entries` hold neither.
|
|
220
|
+
*
|
|
221
|
+
* ```ts
|
|
222
|
+
* import { equal } from '@yaks/testing'
|
|
223
|
+
* import { askedFor } from './answer.ts'
|
|
224
|
+
* let at = (n: number, b: object) => ({ entity: { eid: `e${n}` }, ...b })
|
|
225
|
+
* let entries = [
|
|
226
|
+
* at(1, { content: { body: 'Add Eve' }, created: { by: 'ann' } }),
|
|
227
|
+
* at(2, { ask: { through: 'e1' } }),
|
|
228
|
+
* at(3, { content: { body: 'Hi all' }, created: { by: 'bo' } }),
|
|
229
|
+
* at(4, { call: { name: 'member_add', source: 'e2' } }),
|
|
230
|
+
* ]
|
|
231
|
+
* equal(askedFor(entries, entries[3]), 'ann')
|
|
232
|
+
* equal(askedFor(entries, at(5, { call: { name: 'x' } })), undefined)
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export let askedFor = (
|
|
236
|
+
entries: Bundle[],
|
|
237
|
+
call: Bundle,
|
|
238
|
+
): string | undefined => {
|
|
239
|
+
let source = comp(call, 'call')?.source
|
|
240
|
+
let ask = entries.find((b) => b.entity.eid == source)
|
|
241
|
+
let through = comp(ask, 'ask')?.through
|
|
242
|
+
let read = entries.findIndex((b) => b.entity.eid == through)
|
|
243
|
+
let words = read < 0
|
|
244
|
+
? undefined
|
|
245
|
+
: entries.findLast((b, i) => i <= read && given(b))
|
|
246
|
+
return words && writerOf(words)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// What a model's refusal tells the person, by its code (@yaks/model
|
|
250
|
+
// `ModelError`); any other code is the host's own failure, never the model's.
|
|
251
|
+
// A ceiling (`LIMIT`) is said in its own sentence. The host's own account
|
|
252
|
+
// with the provider dry (@yaks/model `dry`: its credit, spend limit or plan
|
|
253
|
+
// used up) is no ceiling of the person's.
|
|
254
|
+
let REFUSED: Record<string, (name: string) => string> = {
|
|
255
|
+
refusal: () => 'the model declined to do this',
|
|
256
|
+
busy: () => 'the model is too busy to answer right now',
|
|
257
|
+
}
|
|
258
|
+
let refusing = (code: string) =>
|
|
259
|
+
REFUSED[code] ??
|
|
260
|
+
(dry({ code })
|
|
261
|
+
? (name: string) => `${name} is unavailable for now`
|
|
262
|
+
: undefined)
|
|
263
|
+
let couldnt = (why: string) => `I couldn't finish this: ${why}.`
|
|
264
|
+
|
|
265
|
+
/** What a turn ended with in plain words, what went wrong where it did, and
|
|
266
|
+
* whether the host broke it (`broke`): the work stopped before it could
|
|
267
|
+
* finish, on the host's side. */
|
|
268
|
+
export type Reply = { text: string; fault?: string; broke?: true }
|
|
269
|
+
|
|
270
|
+
/** Who a reply's words name: the `host` at fault where something failed,
|
|
271
|
+
* and the assistant answering (`name`). */
|
|
272
|
+
export type Naming = { host?: string; name?: string }
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* What ended a session's work: its newest reply, else what kept it from one,
|
|
276
|
+
* in plain words, naming the host where it is at fault ({@link Naming}). `fault` is what went
|
|
277
|
+
* wrong, for the host's tracker: a refusal, a failure, a step that threw, or
|
|
278
|
+
* an end with no reply. A person's stop is no fault, nor a ceiling met (a
|
|
279
|
+
* spent budget says so in its own words). Every end the host is at fault for
|
|
280
|
+
* `broke` it; a model that declined, was busy or is unavailable did not. A
|
|
281
|
+
* reply with no words to what no person said (the platform's own words, or a
|
|
282
|
+
* helper's report) {@link PASS}es: the model had nothing to add.
|
|
283
|
+
*
|
|
284
|
+
* ```ts
|
|
285
|
+
* import { equal } from '@yaks/testing'
|
|
286
|
+
* import { replyOf } from './answer.ts'
|
|
287
|
+
* let line = (seq: number, b: object) => ({
|
|
288
|
+
* entity: { eid: `e${seq}` }, entry: { session: 's', seq }, ...b,
|
|
289
|
+
* })
|
|
290
|
+
* let said = line(2, { output: { source: 'a' }, content: { body: 'Done.' } })
|
|
291
|
+
* equal(replyOf([said]), { text: 'Done.' })
|
|
292
|
+
* equal(replyOf([said, line(3, {
|
|
293
|
+
* output: { source: 'a' }, refusal: { code: 'request' },
|
|
294
|
+
* content: { body: '400 tools.26: input_schema does not support oneOf' },
|
|
295
|
+
* })], { host: 'yaks.app' }), {
|
|
296
|
+
* text: "I couldn't finish this: something in yaks.app failed while" +
|
|
297
|
+
* ' working on it. The people who run yaks.app have been told.',
|
|
298
|
+
* fault: 'request: 400 tools.26: input_schema does not support oneOf',
|
|
299
|
+
* broke: true,
|
|
300
|
+
* })
|
|
301
|
+
* equal(replyOf([line(3, {
|
|
302
|
+
* output: { source: 'a' }, refusal: { code: 'limit' },
|
|
303
|
+
* content: { body: "clerk's model and voice budget for this month is spent" },
|
|
304
|
+
* })]), { text: "clerk's model and voice budget for this month is spent" })
|
|
305
|
+
* equal(replyOf([line(3, {
|
|
306
|
+
* exception: { type: 'Unknown', value: 'unknown prop: .presence' },
|
|
307
|
+
* content: { body: 'Unknown: unknown prop: .presence' },
|
|
308
|
+
* })]).fault, 'Unknown: unknown prop: .presence')
|
|
309
|
+
* equal(replyOf([]).fault, 'ended without an answer')
|
|
310
|
+
* let quiet = line(5, { output: { source: 'a' }, content: { body: '' } })
|
|
311
|
+
* let report = line(4, { delivery: { child: 'h' }, content: { body: 'Done' } })
|
|
312
|
+
* let note = line(4, { system: {}, content: { body: 'Still working' } })
|
|
313
|
+
* equal(replyOf([report, quiet]), { text: '(listening)' })
|
|
314
|
+
* equal(replyOf([note, quiet]), { text: '(listening)' })
|
|
315
|
+
* let asked = line(4, { content: { body: 'Fix the app' } })
|
|
316
|
+
* equal(replyOf([asked, quiet]).fault, 'ended without an answer')
|
|
317
|
+
* let refused = line(4, {
|
|
318
|
+
* result: { call: 'c' }, refusal: { code: 'arguments' },
|
|
319
|
+
* content: { body: 'Invalid tool arguments: missing command' },
|
|
320
|
+
* })
|
|
321
|
+
* equal(replyOf([said, refused, line(5, { ask: {} })]).fault,
|
|
322
|
+
* 'ended without an answer')
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
export let replyOf = (
|
|
326
|
+
entries: Bundle[],
|
|
327
|
+
{ host = 'this service', name = 'the assistant' }: Naming = {},
|
|
328
|
+
): Reply => {
|
|
329
|
+
// A model asked after its calls were answered, a refusal among them, is
|
|
330
|
+
// where the work stands: what it answered, or that it answered nothing.
|
|
331
|
+
let lines = ordered(entries)
|
|
332
|
+
let end = lines.findLast((b) =>
|
|
333
|
+
b.content && (b.output && !b.reasoning || b.refusal || b.exception) ||
|
|
334
|
+
b.failed ||
|
|
335
|
+
kindOf(b) == 'stop' || kindOf(b) == 'ask'
|
|
336
|
+
)
|
|
337
|
+
if (end && kindOf(end) == 'ask' && !end.failed) end = undefined
|
|
338
|
+
// A step that threw after a stop was withdrawn by it: the stop ends it.
|
|
339
|
+
if (end && kindOf(end) == 'exception') {
|
|
340
|
+
end = lines.slice(0, lines.indexOf(end)).findLast((b) =>
|
|
341
|
+
kindOf(b) == 'stop'
|
|
342
|
+
) ?? end
|
|
343
|
+
}
|
|
344
|
+
let text = end ? textOf(end) : ''
|
|
345
|
+
let kind = end && kindOf(end)
|
|
346
|
+
if (kind == 'stop') return { text }
|
|
347
|
+
if (kind == 'refusal' && comp(end, 'refusal')?.code == LIMIT) return { text }
|
|
348
|
+
if (kind == 'output' && !end!.notice && text) return { text }
|
|
349
|
+
let words = lines.filter(given)
|
|
350
|
+
let nobodys = words.length > 0 &&
|
|
351
|
+
words.every((b) => !!b.system || !!b.delivery)
|
|
352
|
+
if (kind == 'output' && !end!.notice && nobodys) return { text: PASS }
|
|
353
|
+
let told = ` The people who run ${host} have been told.`
|
|
354
|
+
let broke = couldnt(`something in ${host} failed while working on it`)
|
|
355
|
+
let code = String(comp(end, 'refusal')?.code)
|
|
356
|
+
let fault = kind == 'refusal'
|
|
357
|
+
? refusalText(code, text)
|
|
358
|
+
: String(comp(end, 'failed')?.reason ?? '') || text ||
|
|
359
|
+
'ended without an answer'
|
|
360
|
+
let said = kind == 'refusal' ? refusing(code) : undefined
|
|
361
|
+
let declined = !!said
|
|
362
|
+
let why = said
|
|
363
|
+
? couldnt(said(name))
|
|
364
|
+
: end
|
|
365
|
+
? broke
|
|
366
|
+
: 'This ended without an answer.'
|
|
367
|
+
return { text: why + told, fault, ...declined ? {} : { broke: true } }
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** A transcript's answer as its turn ends: the {@link Reply}, the newest
|
|
371
|
+
* words given (`mark`, absent where none were), and the inputs it answers
|
|
372
|
+
* (`given`). A helper's or a fork's answers none: its report goes to the
|
|
373
|
+
* session that started it (./children.ts `deliverChild`). */
|
|
374
|
+
export type Answer = Reply & { mark?: Bundle; given: Bundle[] }
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* The answer of `session`'s turn ({@link Answer}), read back from its newest
|
|
378
|
+
* line only as far as it needs, never every line a conversation has held for
|
|
379
|
+
* weeks: a helper's own turn, and a conversation's back to the reply before
|
|
380
|
+
* the words it answers and before each call that started a helper whose
|
|
381
|
+
* report stands for words. `naming` says who a failure's words name
|
|
382
|
+
* ({@link replyOf}). `replied` reads the turn of the newest reply, said while
|
|
383
|
+
* newer words wait (./run.ts `Runner.replied`): those words are the next
|
|
384
|
+
* turn's, not one that ended without an answer.
|
|
385
|
+
*/
|
|
386
|
+
export let answerOf = async (
|
|
387
|
+
g: Graph,
|
|
388
|
+
session: Eid,
|
|
389
|
+
naming?: Naming,
|
|
390
|
+
replied = false,
|
|
391
|
+
): Promise<Answer> => {
|
|
392
|
+
let [row] = await g.get([session], ['spawned'])
|
|
393
|
+
let link = comp(row, 'spawned')
|
|
394
|
+
let helper = !!link && link.quiet == null
|
|
395
|
+
let calls = new Map<string, string>()
|
|
396
|
+
let looked = new Set<string>()
|
|
397
|
+
// The call that started each child these lines tell of.
|
|
398
|
+
let starts = async (entries: Bundle[]) => {
|
|
399
|
+
let children = [
|
|
400
|
+
...new Set(entries.flatMap((b) => {
|
|
401
|
+
let child = comp(b, 'delivery')?.child
|
|
402
|
+
return child && !looked.has(String(child)) ? [String(child)] : []
|
|
403
|
+
})),
|
|
404
|
+
]
|
|
405
|
+
for (let child of children) looked.add(child)
|
|
406
|
+
for (let b of await g.get(children, ['spawned'])) {
|
|
407
|
+
let call = comp(b, 'spawned')?.call
|
|
408
|
+
if (call) calls.set(b.entity.eid, String(call))
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
let { entries, whole } = await transcriptBack(g, session, async (read) => {
|
|
412
|
+
if (helper) return read.some(given)
|
|
413
|
+
await starts(read)
|
|
414
|
+
return !!turnAt(read, calls, false, replied)
|
|
415
|
+
})
|
|
416
|
+
if (helper) {
|
|
417
|
+
let lines = replied
|
|
418
|
+
? entries.slice(0, entries.findLastIndex(said) + 1)
|
|
419
|
+
: entries
|
|
420
|
+
return { ...replyOf(turnOf(lines), naming), given: [] }
|
|
421
|
+
}
|
|
422
|
+
await starts(entries)
|
|
423
|
+
let turn = turnAt(entries, calls, whole, replied)
|
|
424
|
+
let reply = replyOf(turnOf(entries.slice(0, turn?.through)), naming)
|
|
425
|
+
return { ...reply, mark: turn?.mark, given: turn?.given ?? [] }
|
|
426
|
+
}
|
package/append.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/** Transactional transcript ordering. Omit seq to append; explicit positions are
|
|
2
|
+
* reserved for import and must be positive, unique integers. */ import { after } from "@yaks/fp";
|
|
3
|
+
import { line, parse } from "@yaks/query";
|
|
4
|
+
import { UnknownSession } from "./unknown.js";
|
|
5
|
+
let entry = (b)=>b?.entry;
|
|
6
|
+
export let sequencing = (bundles, tx)=>after(tx.get(bundles.filter((b)=>b.entry).map((b)=>b.entity.eid), [
|
|
7
|
+
'entry'
|
|
8
|
+
]), (existing)=>{
|
|
9
|
+
let groups = new Map();
|
|
10
|
+
for (let b of bundles){
|
|
11
|
+
let e = entry(b);
|
|
12
|
+
if (!e || b.$delete) continue;
|
|
13
|
+
if (e.seq != null && (!Number.isSafeInteger(e.seq) || Number(e.seq) < 1)) {
|
|
14
|
+
throw new Error('entry.seq must be a positive integer; omit seq to append atomically');
|
|
15
|
+
}
|
|
16
|
+
let previous = entry(existing.find((o)=>o.entity.eid == b.entity.eid));
|
|
17
|
+
if (previous?.session != null && e.session != null && previous.session != e.session) {
|
|
18
|
+
throw new Error('entry.session cannot move an existing entry');
|
|
19
|
+
}
|
|
20
|
+
if (e.session == null) {
|
|
21
|
+
e.session = entry(existing.find((o)=>o.entity.eid == b.entity.eid))?.session;
|
|
22
|
+
}
|
|
23
|
+
if (e.session != null) {
|
|
24
|
+
let id = String(e.session);
|
|
25
|
+
groups.set(id, [
|
|
26
|
+
...groups.get(id) ?? [],
|
|
27
|
+
b
|
|
28
|
+
]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// Allocate parent entries before children that fork from the same batch.
|
|
32
|
+
let ordered = [], visiting = new Set();
|
|
33
|
+
let order = (id)=>{
|
|
34
|
+
if (ordered.includes(id)) return;
|
|
35
|
+
if (visiting.has(id)) {
|
|
36
|
+
throw new Error('cyclic fork ancestry in append batch');
|
|
37
|
+
}
|
|
38
|
+
visiting.add(id);
|
|
39
|
+
let from = bundles.find((b)=>b.entity.eid == id)?.fork?.from;
|
|
40
|
+
let parent = entry(bundles.find((b)=>b.entity.eid == from))?.session;
|
|
41
|
+
if (parent && groups.has(String(parent))) order(String(parent));
|
|
42
|
+
visiting.delete(id);
|
|
43
|
+
ordered.push(id);
|
|
44
|
+
};
|
|
45
|
+
for (let id of groups.keys())order(id);
|
|
46
|
+
let pending = undefined;
|
|
47
|
+
for (let session of ordered){
|
|
48
|
+
let sessionBundles = groups.get(session);
|
|
49
|
+
pending = after(pending, ()=>after(tx.read(parse('.entry.session=' + session + '&.order=-entry.seq&.limit=1'), undefined, [
|
|
50
|
+
'entry'
|
|
51
|
+
]), (latest)=>after(sessionBundles.some((b)=>entry(b)?.seq != null) ? tx.read(parse('.entry.session=' + session + '&.entry.seq>=' + Math.min(...sessionBundles.filter((b)=>entry(b)?.seq != null).map((b)=>Number(entry(b).seq))) + '&.entry.seq<=' + Math.max(...sessionBundles.filter((b)=>entry(b)?.seq != null).map((b)=>Number(entry(b).seq)))), undefined, [
|
|
52
|
+
'entry'
|
|
53
|
+
]) : [], (collisions)=>{
|
|
54
|
+
let own = [
|
|
55
|
+
...existing.filter((b)=>entry(b)?.session == session),
|
|
56
|
+
...latest,
|
|
57
|
+
...collisions
|
|
58
|
+
];
|
|
59
|
+
return after(tx.read(line`.entity.eid=${session}&.session`, undefined, [
|
|
60
|
+
'fork'
|
|
61
|
+
]), (found)=>{
|
|
62
|
+
if (!found[0] && !bundles.some((b)=>b.entity.eid == session && b.session)) throw new UnknownSession(session);
|
|
63
|
+
let self = bundles.find((b)=>b.entity.eid == session && b.fork) ?? found[0];
|
|
64
|
+
let from = self?.fork?.from;
|
|
65
|
+
let anchorInBatch = bundles.find((b)=>b.entity.eid == from);
|
|
66
|
+
return after(from && !anchorInBatch ? tx.get([
|
|
67
|
+
String(from)
|
|
68
|
+
], [
|
|
69
|
+
'entry'
|
|
70
|
+
]) : [], (anchors)=>{
|
|
71
|
+
let floor = Number(entry(anchorInBatch ?? anchors[0] ?? {
|
|
72
|
+
entity: {
|
|
73
|
+
eid: ''
|
|
74
|
+
}
|
|
75
|
+
})?.seq ?? 0);
|
|
76
|
+
let max = Math.max(floor, ...own.map((b)=>Number(entry(b)?.seq ?? 0)));
|
|
77
|
+
let occupied = new Map(own.map((b)=>[
|
|
78
|
+
Number(entry(b)?.seq),
|
|
79
|
+
b.entity.eid
|
|
80
|
+
]));
|
|
81
|
+
for (let b of sessionBundles){
|
|
82
|
+
let e = entry(b);
|
|
83
|
+
let old = own.find((o)=>o.entity.eid == b.entity.eid);
|
|
84
|
+
if (old && e.seq != null && e.seq != entry(old)?.seq) {
|
|
85
|
+
throw new Error('entry.seq cannot move an existing entry');
|
|
86
|
+
}
|
|
87
|
+
let seq = e.seq == null ? entry(old)?.seq ?? Math.floor(max) + 1 : e.seq;
|
|
88
|
+
if (!Number.isSafeInteger(seq) || Number(seq) < 1) {
|
|
89
|
+
throw new Error('entry sequence exhausted');
|
|
90
|
+
}
|
|
91
|
+
if (Number(seq) <= floor || occupied.has(Number(seq)) && occupied.get(Number(seq)) != b.entity.eid) {
|
|
92
|
+
throw new Error('entry.seq is occupied or precedes the fork boundary; omit seq to append atomically');
|
|
93
|
+
}
|
|
94
|
+
e.seq = seq;
|
|
95
|
+
occupied.set(Number(seq), b.entity.eid);
|
|
96
|
+
max = Math.max(max, Number(seq));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
})));
|
|
101
|
+
}
|
|
102
|
+
return after(pending, ()=>bundles);
|
|
103
|
+
});
|
|
104
|
+
/** Append an ordinary input or a passive notice. The session plugin assigns seq
|
|
105
|
+
* inside the graph transaction. Supply an eid for idempotent delivery and from
|
|
106
|
+
* for words authored outside the receiving model's own conversation. */ export let appendEntry = (g, session, body, opts = {})=>g.apply([
|
|
107
|
+
{
|
|
108
|
+
entity: {
|
|
109
|
+
eid: opts.eid ?? crypto.randomUUID()
|
|
110
|
+
},
|
|
111
|
+
entry: {
|
|
112
|
+
session,
|
|
113
|
+
...opts.from ? {
|
|
114
|
+
from: opts.from
|
|
115
|
+
} : {}
|
|
116
|
+
},
|
|
117
|
+
content: {
|
|
118
|
+
body
|
|
119
|
+
},
|
|
120
|
+
...opts.notice ? {
|
|
121
|
+
notice: {}
|
|
122
|
+
} : {}
|
|
123
|
+
}
|
|
124
|
+
]);
|
|
125
|
+
//# sourceMappingURL=append.js.map
|
package/append.js.map
ADDED
|
@@ -0,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