@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/source.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The writer of a transcript line is independent of its turn role and actor.
|
|
2
|
+
let comp = (b, key)=>b[key] ?? {};
|
|
3
|
+
/** A source stored on the message takes precedence over writer stamps.
|
|
4
|
+
* Older transcripts retain their source through their structural components;
|
|
5
|
+
* `created.by` records an actor, not necessarily the writer of the prose. */ export let source = (entry, names = new Map())=>{
|
|
6
|
+
let explicit = comp(entry, 'entry').from;
|
|
7
|
+
if (typeof explicit == 'string' && explicit.trim()) return explicit;
|
|
8
|
+
if (entry.delivery) {
|
|
9
|
+
let delivery = comp(entry, 'delivery');
|
|
10
|
+
return delivery.outcome == 'stopped' || delivery.outcome == 'failed' ? 'the system' : `your helper (${delivery.child})`;
|
|
11
|
+
}
|
|
12
|
+
if (entry.system || entry.woken || entry.attention || entry.tool_progress) {
|
|
13
|
+
return 'the system';
|
|
14
|
+
}
|
|
15
|
+
if (entry.checkpoint) return 'the checkpoint summarizer';
|
|
16
|
+
if (entry.prompt) {
|
|
17
|
+
return comp(entry, 'prompt').source == 'delegation:instructions' ? 'your parent session' : 'the system';
|
|
18
|
+
}
|
|
19
|
+
if (comp(entry, 'notice').own) return 'your voice on the call';
|
|
20
|
+
if (entry.notice || entry.task_context) return 'the system';
|
|
21
|
+
if (entry.result || entry.output || entry.call) return undefined;
|
|
22
|
+
let { by, via } = comp(entry, 'created');
|
|
23
|
+
let session = comp(entry, 'entry').session;
|
|
24
|
+
return via != session && names.get(String(via)) || names.get(String(by)) || (by ? `actor ${by}` : 'an unknown sender');
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=source.js.map
|
package/source.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source.js","sources":["./source.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAG7E,IAAI,OAAO,CAAC,GAAW,MAAiB,CAAC,CAAC,IAAI,IAAI,CAAC;AAEnD;;2EAE2E,GAC3E,OAAO,IAAI,SAAS,CAClB,OACA,QAA0B,IAAI,KAAK;EAEnC,IAAI,WAAW,KAAK,OAAO,SAAS,IAAI;EACxC,IAAI,OAAO,YAAY,YAAY,SAAS,IAAI,IAAI,OAAO;EAC3D,IAAI,MAAM,QAAQ,EAAE;IAClB,IAAI,WAAW,KAAK,OAAO;IAC3B,OAAO,SAAS,OAAO,IAAI,aAAa,SAAS,OAAO,IAAI,WACxD,eACA,CAAC,aAAa,EAAE,SAAS,KAAK,CAAC,CAAC,CAAC;EACvC;EACA,IAAI,MAAM,MAAM,IAAI,MAAM,KAAK,IAAI,MAAM,SAAS,IAAI,MAAM,aAAa,EAAE;IACzE,OAAO;EACT;EACA,IAAI,MAAM,UAAU,EAAE,OAAO;EAC7B,IAAI,MAAM,MAAM,EAAE;IAChB,OAAO,KAAK,OAAO,UAAU,MAAM,IAAI,4BACnC,wBACA;EACN;EACA,IAAI,KAAK,OAAO,UAAU,GAAG,EAAE,OAAO;EACtC,IAAI,MAAM,MAAM,IAAI,MAAM,YAAY,EAAE,OAAO;EAC/C,IAAI,MAAM,MAAM,IAAI,MAAM,MAAM,IAAI,MAAM,IAAI,EAAE,OAAO;EACvD,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,KAAK,OAAO;EAC9B,IAAI,UAAU,KAAK,OAAO,SAAS,OAAO;EAC1C,OAAO,AAAC,OAAO,WAAW,MAAM,GAAG,CAAC,OAAO,SACzC,MAAM,GAAG,CAAC,OAAO,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,GAAG,mBAAmB;AACtE,EAAC"}
|
package/source.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// The writer of a transcript line is independent of its turn role and actor.
|
|
2
|
+
import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
3
|
+
|
|
4
|
+
let comp = (b: Bundle, key: string) => (b[key] ?? {}) as Comp
|
|
5
|
+
|
|
6
|
+
/** A source stored on the message takes precedence over writer stamps.
|
|
7
|
+
* Older transcripts retain their source through their structural components;
|
|
8
|
+
* `created.by` records an actor, not necessarily the writer of the prose. */
|
|
9
|
+
export let source = (
|
|
10
|
+
entry: Bundle,
|
|
11
|
+
names: Map<Eid, string> = new Map(),
|
|
12
|
+
): string | undefined => {
|
|
13
|
+
let explicit = comp(entry, 'entry').from
|
|
14
|
+
if (typeof explicit == 'string' && explicit.trim()) return explicit
|
|
15
|
+
if (entry.delivery) {
|
|
16
|
+
let delivery = comp(entry, 'delivery')
|
|
17
|
+
return delivery.outcome == 'stopped' || delivery.outcome == 'failed'
|
|
18
|
+
? 'the system'
|
|
19
|
+
: `your helper (${delivery.child})`
|
|
20
|
+
}
|
|
21
|
+
if (entry.system || entry.woken || entry.attention || entry.tool_progress) {
|
|
22
|
+
return 'the system'
|
|
23
|
+
}
|
|
24
|
+
if (entry.checkpoint) return 'the checkpoint summarizer'
|
|
25
|
+
if (entry.prompt) {
|
|
26
|
+
return comp(entry, 'prompt').source == 'delegation:instructions'
|
|
27
|
+
? 'your parent session'
|
|
28
|
+
: 'the system'
|
|
29
|
+
}
|
|
30
|
+
if (comp(entry, 'notice').own) return 'your voice on the call'
|
|
31
|
+
if (entry.notice || entry.task_context) return 'the system'
|
|
32
|
+
if (entry.result || entry.output || entry.call) return undefined
|
|
33
|
+
let { by, via } = comp(entry, 'created')
|
|
34
|
+
let session = comp(entry, 'entry').session
|
|
35
|
+
return (via != session && names.get(String(via))) ||
|
|
36
|
+
names.get(String(by)) || (by ? `actor ${by}` : 'an unknown sender')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Host-rendered input prose and its writer, kept separate until assembly. */
|
|
40
|
+
export type InputText = string | { text: string; from?: string }
|
package/start.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// A person's comment on an open task no live session holds starts a session
|
|
2
|
+
// on it, with the comment as its first input.
|
|
3
|
+
//
|
|
4
|
+
// Whether a start is owed is settled in the comment's own commit, by the
|
|
5
|
+
// match the vocabulary declares for `session_start`: a person's comment not
|
|
6
|
+
// yet said to anyone, on a task that is open, not blocked, and unclaimed or
|
|
7
|
+
// claimed by a session that has ended. An agent's comment owes none, and
|
|
8
|
+
// neither does a comment committed before this effect was declared: nothing
|
|
9
|
+
// looks back for one.
|
|
10
|
+
//
|
|
11
|
+
// The run is the start any door makes (a session, and an entry that asks its
|
|
12
|
+
// model for a turn), as one batch with the claim on the task and the mark
|
|
13
|
+
// that the comment was said (./listen.ts `deliver`). The batch is guarded by
|
|
14
|
+
// what the commit saw, so it lands whole or not at all: a comment someone
|
|
15
|
+
// heard meanwhile, or a task that was closed, blocked or taken, starts
|
|
16
|
+
// nothing. From that batch on the task is held, so his next comment reaches
|
|
17
|
+
// this session through the bus (./bus.ts) and owes no second start.
|
|
18
|
+
//
|
|
19
|
+
// At most `limit` sessions started this way are at work at once. A start past
|
|
20
|
+
// that keeps its run and waits in the pool (@yaks/effects `Attempt.defer`),
|
|
21
|
+
// asking again every `wait` ms; its comment stays unsaid meanwhile, and
|
|
22
|
+
// nobody else hears it (./listen.ts `taken`). One process decides one start
|
|
23
|
+
// at a time, so two comments arriving together cannot both take the last
|
|
24
|
+
// place; two workers could, by one.
|
|
25
|
+
import { Stale } from "@yaks/graph/errors";
|
|
26
|
+
import { derivedEid } from "@yaks/graph/identity";
|
|
27
|
+
import { token } from "@yaks/graph/sha256";
|
|
28
|
+
import { human } from "@yaks/id";
|
|
29
|
+
import { effectsIn } from "@yaks/vocab";
|
|
30
|
+
import { absent, and, eq, fields, present } from "@yaks/query";
|
|
31
|
+
import { input } from "./bus.js";
|
|
32
|
+
import { CLAIM, SESSION } from "./comp.js";
|
|
33
|
+
import { atWork, deliver, said } from "./listen.js";
|
|
34
|
+
let str = (v)=>typeof v == 'string' ? v : '';
|
|
35
|
+
let comp = (b, name)=>b?.[name] ?? {};
|
|
36
|
+
/** The sessions started on a comment that are at work now, mid-turn. */ export let busy = async (g)=>atWork(g, (await g.read(and(present(`${SESSION}.source.comment`), absent(`${SESSION}.ended`), fields('entity.eid')))).map((b)=>b.entity.eid));
|
|
37
|
+
// Whether a write guarded by `$was` landed: false where the graph has moved.
|
|
38
|
+
let guarded = async (write)=>{
|
|
39
|
+
try {
|
|
40
|
+
await write();
|
|
41
|
+
return true;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error instanceof Stale) return false;
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
// Whether a session has its `ended` mark, read alone: the rest of `session`
|
|
48
|
+
// is derived from its whole log.
|
|
49
|
+
let over = async (g, session)=>comp((await g.read(and(eq('entity.eid', session), fields(`${SESSION}.ended`))))[0], SESSION).ended == true;
|
|
50
|
+
/** Start a session on the task `comment` is on, as its holder, with the
|
|
51
|
+
* comment as its first input. Answers the session, or nothing where the
|
|
52
|
+
* comment or its task has moved since the commit that owed the start. */ export let start = async (g, comment, opening, beside = (owed)=>Promise.resolve(owed))=>{
|
|
53
|
+
let [item] = await g.get([
|
|
54
|
+
comment
|
|
55
|
+
]);
|
|
56
|
+
let on = str(comp(item, 'comment').target);
|
|
57
|
+
let [task] = on ? await g.get([
|
|
58
|
+
on
|
|
59
|
+
]) : [];
|
|
60
|
+
if (!item || !task) return;
|
|
61
|
+
// A live holder hears the comment itself, through the bus. A lock is
|
|
62
|
+
// released before another session takes it (./lease.ts), and one a session
|
|
63
|
+
// that is over still holds goes with that session anyway.
|
|
64
|
+
let held = str(comp(task, CLAIM).session);
|
|
65
|
+
if (held) {
|
|
66
|
+
if (!await over(g, held)) return;
|
|
67
|
+
let free = {
|
|
68
|
+
entity: {
|
|
69
|
+
eid: on
|
|
70
|
+
},
|
|
71
|
+
[CLAIM]: null,
|
|
72
|
+
$was: {
|
|
73
|
+
[CLAIM]: {
|
|
74
|
+
session: token(held)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
if (!await guarded(()=>g.apply([
|
|
79
|
+
free
|
|
80
|
+
], {
|
|
81
|
+
trusted: true
|
|
82
|
+
}))) return;
|
|
83
|
+
}
|
|
84
|
+
let session = derivedEid(`session start ${comment}`);
|
|
85
|
+
let by = str(comp(item, 'created').by);
|
|
86
|
+
let id = human(g.vocab)(task);
|
|
87
|
+
let names = new Map([
|
|
88
|
+
on,
|
|
89
|
+
by
|
|
90
|
+
].filter(Boolean).map((eid)=>[
|
|
91
|
+
eid,
|
|
92
|
+
eid
|
|
93
|
+
]));
|
|
94
|
+
for (let b of (await g.get([
|
|
95
|
+
...names.keys()
|
|
96
|
+
]))){
|
|
97
|
+
names.set(b.entity.eid, human(g.vocab)(b));
|
|
98
|
+
}
|
|
99
|
+
let [{ line }] = await beside([
|
|
100
|
+
{
|
|
101
|
+
item,
|
|
102
|
+
line: said(g.vocab, item, (eid)=>names.get(eid) ?? eid, undefined, by)
|
|
103
|
+
}
|
|
104
|
+
]);
|
|
105
|
+
let { persona, using } = await opening(on);
|
|
106
|
+
let project = str(comp(task, 'filed').project);
|
|
107
|
+
let unmarked = [
|
|
108
|
+
'completed',
|
|
109
|
+
'cancelled'
|
|
110
|
+
].filter((c)=>g.vocab.comp(c));
|
|
111
|
+
let landed = await deliver(g, null, item, [
|
|
112
|
+
{
|
|
113
|
+
entity: {
|
|
114
|
+
eid: session
|
|
115
|
+
},
|
|
116
|
+
[SESSION]: {
|
|
117
|
+
id: session.slice(0, 8),
|
|
118
|
+
operator: false,
|
|
119
|
+
source: comment,
|
|
120
|
+
...persona ? {
|
|
121
|
+
persona
|
|
122
|
+
} : {}
|
|
123
|
+
},
|
|
124
|
+
native: {},
|
|
125
|
+
...project ? {
|
|
126
|
+
filed: {
|
|
127
|
+
project
|
|
128
|
+
}
|
|
129
|
+
} : {}
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
...input(session, comment, `${line}\n\nNo session held ${id}, so this one was started on it ` + `and holds it now. Reply first with a comment on ${id}, so its ` + 'writer sees it was picked up, then do what the comment asks.'),
|
|
133
|
+
using
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
entity: {
|
|
137
|
+
eid: on
|
|
138
|
+
},
|
|
139
|
+
[CLAIM]: {
|
|
140
|
+
session
|
|
141
|
+
},
|
|
142
|
+
$was: {
|
|
143
|
+
[CLAIM]: {
|
|
144
|
+
session: token(null)
|
|
145
|
+
},
|
|
146
|
+
...Object.fromEntries(unmarked.map((c)=>[
|
|
147
|
+
c,
|
|
148
|
+
{
|
|
149
|
+
at: token(null)
|
|
150
|
+
}
|
|
151
|
+
])),
|
|
152
|
+
...g.vocab.comp('blocked') ? {
|
|
153
|
+
blocked: {
|
|
154
|
+
since: token(null)
|
|
155
|
+
}
|
|
156
|
+
} : {}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
]);
|
|
160
|
+
return landed ? session : undefined;
|
|
161
|
+
};
|
|
162
|
+
// One decision at a time, so two starts cannot both see the last free place.
|
|
163
|
+
let serially = ()=>{
|
|
164
|
+
let line = Promise.resolve();
|
|
165
|
+
return (job)=>{
|
|
166
|
+
let run = line.then(job);
|
|
167
|
+
line = run.catch(()=>{});
|
|
168
|
+
return run;
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* The `session_start` handler, for a host that can open a session: none where
|
|
173
|
+
* the vocabulary declares no such effect.
|
|
174
|
+
*
|
|
175
|
+
* ```ts
|
|
176
|
+
* import { starting } from '@yaks/session'
|
|
177
|
+
*
|
|
178
|
+
* // fx.handle(starting(graph, { opening: (task) => ({ using }) }))
|
|
179
|
+
* ```
|
|
180
|
+
*/ export let starting = (g, options)=>{
|
|
181
|
+
if (!effectsIn(g.vocab.docs).some((e)=>e.name == 'session_start')) {
|
|
182
|
+
return {};
|
|
183
|
+
}
|
|
184
|
+
let { limit = 2, wait = 10_000 } = options;
|
|
185
|
+
let serial = serially();
|
|
186
|
+
return {
|
|
187
|
+
session_start: (e, _tx, _write, attempt)=>serial(async ()=>{
|
|
188
|
+
if ((await busy(g)).length < limit) {
|
|
189
|
+
await start(g, e.entity.eid, options.opening, options.beside);
|
|
190
|
+
} else if (attempt?.defer) await attempt.defer(wait);
|
|
191
|
+
else {
|
|
192
|
+
throw new Error(`session_start: ${limit} sessions started on comments are at ` + 'work, and this host keeps no pool for the next to wait in');
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
//# sourceMappingURL=start.js.map
|
package/start.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"start.js","sources":["./start.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,8CAA8C;AAC9C,EAAE;AACF,yEAAyE;AACzE,4EAA4E;AAC5E,4EAA4E;AAC5E,yEAAyE;AACzE,4EAA4E;AAC5E,sBAAsB;AACtB,EAAE;AACF,6EAA6E;AAC7E,0EAA0E;AAC1E,6EAA6E;AAC7E,0EAA0E;AAC1E,uEAAuE;AACvE,4EAA4E;AAC5E,oEAAoE;AACpE,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,wEAAwE;AACxE,4EAA4E;AAC5E,yEAAyE;AACzE,oCAAoC;AAGpC,SAAS,KAAK,kCAAuC;AACrD,SAAS,UAAU,oCAAyC;AAC5D,SAAS,KAAK,kCAAuC;AAErD,SAAS,KAAK,wBAA6B;AAC3C,SA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AO,CAAC;EACV;EACA,IAAI,EAAE,QAAQ,CAAC,EAAE,OAAO,MAAM,EAAE,GAAG;EACnC,IAAI,SAAS;EACb,OAAO;IACL,eAAe,CAAC,GAAG,KAAK,QAAQ,UAC9B,OAAO;QACL,IAAI,CAAC,MAAM,KAAK,EAAE,EAAE,MAAM,GAAG,OAAO;UAClC,MAAM,MAAM,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,QAAQ,OAAO,EAAE,QAAQ,MAAM;QAC9D,OAAO,IAAI,SAAS,OAAO,MAAM,QAAQ,KAAK,CAAC;aAC1C;UACH,MAAM,IAAI,MACR,CAAC,eAAe,EAAE,MAAM,qCAAqC,CAAC,GAC5D;QAEN;MACF;EACJ;AACF,EAAC"}
|
package/start.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// A person's comment on an open task no live session holds starts a session
|
|
2
|
+
// on it, with the comment as its first input.
|
|
3
|
+
//
|
|
4
|
+
// Whether a start is owed is settled in the comment's own commit, by the
|
|
5
|
+
// match the vocabulary declares for `session_start`: a person's comment not
|
|
6
|
+
// yet said to anyone, on a task that is open, not blocked, and unclaimed or
|
|
7
|
+
// claimed by a session that has ended. An agent's comment owes none, and
|
|
8
|
+
// neither does a comment committed before this effect was declared: nothing
|
|
9
|
+
// looks back for one.
|
|
10
|
+
//
|
|
11
|
+
// The run is the start any door makes (a session, and an entry that asks its
|
|
12
|
+
// model for a turn), as one batch with the claim on the task and the mark
|
|
13
|
+
// that the comment was said (./listen.ts `deliver`). The batch is guarded by
|
|
14
|
+
// what the commit saw, so it lands whole or not at all: a comment someone
|
|
15
|
+
// heard meanwhile, or a task that was closed, blocked or taken, starts
|
|
16
|
+
// nothing. From that batch on the task is held, so his next comment reaches
|
|
17
|
+
// this session through the bus (./bus.ts) and owes no second start.
|
|
18
|
+
//
|
|
19
|
+
// At most `limit` sessions started this way are at work at once. A start past
|
|
20
|
+
// that keeps its run and waits in the pool (@yaks/effects `Attempt.defer`),
|
|
21
|
+
// asking again every `wait` ms; its comment stays unsaid meanwhile, and
|
|
22
|
+
// nobody else hears it (./listen.ts `taken`). One process decides one start
|
|
23
|
+
// at a time, so two comments arriving together cannot both take the last
|
|
24
|
+
// place; two workers could, by one.
|
|
25
|
+
|
|
26
|
+
import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
27
|
+
import { Stale } from '@yaks/graph/errors'
|
|
28
|
+
import { derivedEid } from '@yaks/graph/identity'
|
|
29
|
+
import { token } from '@yaks/graph/sha256'
|
|
30
|
+
import type { Handlers } from 'jsr:@yaks/effects@0.2.36'
|
|
31
|
+
import { human } from '@yaks/id'
|
|
32
|
+
import { effectsIn } from '@yaks/vocab'
|
|
33
|
+
import { absent, and, eq, fields, present } from '@yaks/query'
|
|
34
|
+
import { input } from './bus.js'
|
|
35
|
+
import { CLAIM, SESSION } from './comp.js'
|
|
36
|
+
import { atWork, deliver, said } from './listen.js'
|
|
37
|
+
import type { Beside } from './recall.ts'
|
|
38
|
+
|
|
39
|
+
let str = (v: unknown): string => typeof v == 'string' ? v : ''
|
|
40
|
+
let comp = (b: Bundle | undefined, name: string): Comp =>
|
|
41
|
+
(b?.[name] ?? {}) as Comp
|
|
42
|
+
|
|
43
|
+
/** What a host opens a session on a piece of work with. */
|
|
44
|
+
export type Opening = {
|
|
45
|
+
/** the persona the session wears, where the work's project has one */
|
|
46
|
+
persona?: Eid
|
|
47
|
+
/** the model its first input asks: `using{provider, model}` */
|
|
48
|
+
using: Comp
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** How sessions are started on comments. */
|
|
52
|
+
export type Starting = {
|
|
53
|
+
/** what this host opens a session on a task with */
|
|
54
|
+
opening: (task: Eid) => Opening | Promise<Opening>
|
|
55
|
+
/** the most sessions started on comments at work at once (default 2) */
|
|
56
|
+
limit?: number
|
|
57
|
+
/** how long a start past the limit waits before it asks again, in
|
|
58
|
+
* milliseconds (default ten seconds) */
|
|
59
|
+
wait?: number
|
|
60
|
+
/** sets under a comment its person wrote what they said before
|
|
61
|
+
* (./recall.ts) */
|
|
62
|
+
beside?: Beside
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The sessions started on a comment that are at work now, mid-turn. */
|
|
66
|
+
export let busy = async (
|
|
67
|
+
g: Pick<Graph, 'read'>,
|
|
68
|
+
): Promise<Eid[]> =>
|
|
69
|
+
atWork(
|
|
70
|
+
g,
|
|
71
|
+
(await g.read(and(
|
|
72
|
+
present(`${SESSION}.source.comment`),
|
|
73
|
+
absent(`${SESSION}.ended`),
|
|
74
|
+
fields('entity.eid'),
|
|
75
|
+
))).map((b) => b.entity.eid),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
// Whether a write guarded by `$was` landed: false where the graph has moved.
|
|
79
|
+
let guarded = async (write: () => unknown): Promise<boolean> => {
|
|
80
|
+
try {
|
|
81
|
+
await write()
|
|
82
|
+
return true
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof Stale) return false
|
|
85
|
+
throw error
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Whether a session has its `ended` mark, read alone: the rest of `session`
|
|
90
|
+
// is derived from its whole log.
|
|
91
|
+
let over = async (g: Pick<Graph, 'read'>, session: Eid): Promise<boolean> =>
|
|
92
|
+
comp(
|
|
93
|
+
(await g.read(
|
|
94
|
+
and(eq('entity.eid', session), fields(`${SESSION}.ended`)),
|
|
95
|
+
))[0],
|
|
96
|
+
SESSION,
|
|
97
|
+
).ended == true
|
|
98
|
+
|
|
99
|
+
/** Start a session on the task `comment` is on, as its holder, with the
|
|
100
|
+
* comment as its first input. Answers the session, or nothing where the
|
|
101
|
+
* comment or its task has moved since the commit that owed the start. */
|
|
102
|
+
export let start = async (
|
|
103
|
+
g: Pick<Graph, 'vocab' | 'read' | 'get' | 'apply'>,
|
|
104
|
+
comment: Eid,
|
|
105
|
+
opening: Starting['opening'],
|
|
106
|
+
beside: Beside = (owed) => Promise.resolve(owed),
|
|
107
|
+
): Promise<Eid | undefined> => {
|
|
108
|
+
let [item] = await g.get([comment])
|
|
109
|
+
let on = str(comp(item, 'comment').target)
|
|
110
|
+
let [task] = on ? await g.get([on]) : []
|
|
111
|
+
if (!item || !task) return
|
|
112
|
+
// A live holder hears the comment itself, through the bus. A lock is
|
|
113
|
+
// released before another session takes it (./lease.ts), and one a session
|
|
114
|
+
// that is over still holds goes with that session anyway.
|
|
115
|
+
let held = str(comp(task, CLAIM).session)
|
|
116
|
+
if (held) {
|
|
117
|
+
if (!await over(g, held)) return
|
|
118
|
+
let free = {
|
|
119
|
+
entity: { eid: on },
|
|
120
|
+
[CLAIM]: null,
|
|
121
|
+
$was: { [CLAIM]: { session: token(held) } },
|
|
122
|
+
}
|
|
123
|
+
if (!await guarded(() => g.apply([free], { trusted: true }))) return
|
|
124
|
+
}
|
|
125
|
+
let session = derivedEid(`session start ${comment}`)
|
|
126
|
+
let by = str(comp(item, 'created').by)
|
|
127
|
+
let id = human(g.vocab)(task)
|
|
128
|
+
let names = new Map([on, by].filter(Boolean).map((eid) => [eid, eid]))
|
|
129
|
+
for (let b of await g.get([...names.keys()])) {
|
|
130
|
+
names.set(b.entity.eid, human(g.vocab)(b))
|
|
131
|
+
}
|
|
132
|
+
let [{ line }] = await beside([{
|
|
133
|
+
item,
|
|
134
|
+
line: said(g.vocab, item, (eid) => names.get(eid) ?? eid, undefined, by),
|
|
135
|
+
}])
|
|
136
|
+
let { persona, using } = await opening(on)
|
|
137
|
+
let project = str(comp(task, 'filed').project)
|
|
138
|
+
let unmarked = ['completed', 'cancelled'].filter((c) => g.vocab.comp(c))
|
|
139
|
+
let landed = await deliver(g, null, item, [
|
|
140
|
+
{
|
|
141
|
+
entity: { eid: session },
|
|
142
|
+
[SESSION]: {
|
|
143
|
+
id: session.slice(0, 8),
|
|
144
|
+
operator: false,
|
|
145
|
+
source: comment,
|
|
146
|
+
...persona ? { persona } : {},
|
|
147
|
+
},
|
|
148
|
+
native: {},
|
|
149
|
+
...project ? { filed: { project } } : {},
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
...input(
|
|
153
|
+
session,
|
|
154
|
+
comment,
|
|
155
|
+
`${line}\n\nNo session held ${id}, so this one was started on it ` +
|
|
156
|
+
`and holds it now. Reply first with a comment on ${id}, so its ` +
|
|
157
|
+
'writer sees it was picked up, then do what the comment asks.',
|
|
158
|
+
),
|
|
159
|
+
using,
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
entity: { eid: on },
|
|
163
|
+
[CLAIM]: { session },
|
|
164
|
+
$was: {
|
|
165
|
+
[CLAIM]: { session: token(null) },
|
|
166
|
+
...Object.fromEntries(unmarked.map((c) => [c, { at: token(null) }])),
|
|
167
|
+
...g.vocab.comp('blocked') ? { blocked: { since: token(null) } } : {},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
])
|
|
171
|
+
return landed ? session : undefined
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// One decision at a time, so two starts cannot both see the last free place.
|
|
175
|
+
let serially = () => {
|
|
176
|
+
let line: Promise<unknown> = Promise.resolve()
|
|
177
|
+
return <T>(job: () => Promise<T>): Promise<T> => {
|
|
178
|
+
let run = line.then(job)
|
|
179
|
+
line = run.catch(() => {})
|
|
180
|
+
return run
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The `session_start` handler, for a host that can open a session: none where
|
|
186
|
+
* the vocabulary declares no such effect.
|
|
187
|
+
*
|
|
188
|
+
* ```ts
|
|
189
|
+
* import { starting } from '@yaks/session'
|
|
190
|
+
*
|
|
191
|
+
* // fx.handle(starting(graph, { opening: (task) => ({ using }) }))
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
export let starting = (g: Graph, options: Starting): Handlers => {
|
|
195
|
+
if (!effectsIn(g.vocab.docs).some((e) => e.name == 'session_start')) {
|
|
196
|
+
return {}
|
|
197
|
+
}
|
|
198
|
+
let { limit = 2, wait = 10_000 } = options
|
|
199
|
+
let serial = serially()
|
|
200
|
+
return {
|
|
201
|
+
session_start: (e, _tx, _write, attempt) =>
|
|
202
|
+
serial(async () => {
|
|
203
|
+
if ((await busy(g)).length < limit) {
|
|
204
|
+
await start(g, e.entity.eid, options.opening, options.beside)
|
|
205
|
+
} else if (attempt?.defer) await attempt.defer(wait)
|
|
206
|
+
else {
|
|
207
|
+
throw new Error(
|
|
208
|
+
`session_start: ${limit} sessions started on comments are at ` +
|
|
209
|
+
'work, and this host keeps no pool for the next to wait in',
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
}),
|
|
213
|
+
}
|
|
214
|
+
}
|