@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/resume.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// A session opened in Claude Code: its conversation written as the transcript
|
|
2
|
+
// Claude Code keeps (`<account>/projects/<cwd>/<session>.jsonl`), under the
|
|
3
|
+
// session's own id, so `claude --resume <id>` (or `/resume <id>` inside Claude
|
|
4
|
+
// Code) continues it there. Claude Code resumes a transcript it did not write,
|
|
5
|
+
// other harnesses' tool calls included, as long as each line has the shape of
|
|
6
|
+
// its own.
|
|
7
|
+
//
|
|
8
|
+
// Each line written here names the entry it came from (`yak.entry`), and the
|
|
9
|
+
// Claude Code reader leaves such a line alone (./readers.ts): it is already
|
|
10
|
+
// the session's. What Claude Code appends after them is read like any of its
|
|
11
|
+
// lines, into the same session, so the conversation stays one transcript
|
|
12
|
+
// whichever harness says its next line.
|
|
13
|
+
//
|
|
14
|
+
// A session Claude Code already keeps a file of gets only what was said since
|
|
15
|
+
// that file's last line (a native model took it over meanwhile, @yaks/harness
|
|
16
|
+
// `session_send`); any other gets its whole conversation. Either way the lines
|
|
17
|
+
// end with the note any model gets when it takes over another harness's
|
|
18
|
+
// conversation (./handoff.ts).
|
|
19
|
+
import { derivedEid } from "@yaks/graph/identity";
|
|
20
|
+
import { line as query } from "@yaks/query";
|
|
21
|
+
import { onDisk } from "@yaks/file/local";
|
|
22
|
+
import * as fs from "@yaks/file/io";
|
|
23
|
+
import { handoff, namesOf } from "./handoff.js";
|
|
24
|
+
import { kindOf, ordered, seqOf, textOf } from "./state.js";
|
|
25
|
+
let comp = (b, name)=>b?.[name];
|
|
26
|
+
/** The directory Claude Code keeps a working directory's transcripts in,
|
|
27
|
+
* under its account's `projects`: the path with every character that is not
|
|
28
|
+
* a letter or a digit made a `-`.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { projectDir } from './resume.ts'
|
|
32
|
+
* import { equal } from '@yaks/testing'
|
|
33
|
+
*
|
|
34
|
+
* equal(projectDir('/u/.claude', '/w/my.app'), '/u/.claude/projects/-w-my-app')
|
|
35
|
+
* ```
|
|
36
|
+
*/ export let projectDir = (account, cwd)=>`${account}/projects/${cwd.replace(/[^A-Za-z0-9]/g, '-')}`;
|
|
37
|
+
// What the API takes as a tool's name and a call's id.
|
|
38
|
+
let named = (s)=>s.replace(/[^A-Za-z0-9_-]/g, '_').slice(0, 64) || '_';
|
|
39
|
+
let args = (v)=>{
|
|
40
|
+
if (v && typeof v == 'object' && !Array.isArray(v)) {
|
|
41
|
+
return v;
|
|
42
|
+
}
|
|
43
|
+
if (typeof v == 'string') {
|
|
44
|
+
try {
|
|
45
|
+
return args(JSON.parse(v));
|
|
46
|
+
} catch {
|
|
47
|
+
return {
|
|
48
|
+
input: v
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return v == null ? {} : {
|
|
53
|
+
input: v
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Entries as Claude Code's transcript lines, chained after the line `parent`
|
|
58
|
+
* names: what a person said as theirs, what a model said and called as the
|
|
59
|
+
* assistant's, and each call's result. A call nothing answered is left out,
|
|
60
|
+
* and so is a result of a call not written, as the API refuses either; so is
|
|
61
|
+
* everything that is not conversation (requests, reasoning, stops). `note`
|
|
62
|
+
* closes them, where there are any, as the person's words.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { claudeLines } from './resume.ts'
|
|
66
|
+
* import { equal } from '@yaks/testing'
|
|
67
|
+
*
|
|
68
|
+
* let e = (eid: string, seq: number, c: object) =>
|
|
69
|
+
* ({ entity: { eid }, entry: { session: 's', seq }, ...c })
|
|
70
|
+
* let lines = claudeLines([
|
|
71
|
+
* e('a', 1, { content: { body: 'list it' } }),
|
|
72
|
+
* e('b', 2, { call: { id: 'call.1', name: 'exec_command', args: { cmd: 'ls' } } }),
|
|
73
|
+
* e('c', 3, { result: { call: 'b' }, content: { body: 'x.txt' } }),
|
|
74
|
+
* e('d', 4, { call: { id: 'call_2', name: 'shell' } }),
|
|
75
|
+
* ], { session: 's', cwd: '/w' })
|
|
76
|
+
* equal(lines.map((l) => [l.type, l.uuid, l.parentUuid]), [
|
|
77
|
+
* ['user', 'a', null], ['assistant', 'b', 'a'], ['user', 'c', 'b'],
|
|
78
|
+
* ])
|
|
79
|
+
* equal((lines[1].message as { content: unknown }).content, [
|
|
80
|
+
* { type: 'tool_use', id: 'call_1', name: 'exec_command', input: { cmd: 'ls' } },
|
|
81
|
+
* ])
|
|
82
|
+
* ```
|
|
83
|
+
*/ export let claudeLines = (entries, o)=>{
|
|
84
|
+
let sorted = ordered(entries);
|
|
85
|
+
let answered = new Set(sorted.flatMap((b)=>b.result ? [
|
|
86
|
+
String(comp(b, 'result')?.call)
|
|
87
|
+
] : []));
|
|
88
|
+
let ids = new Map(sorted.flatMap((b)=>b.call && answered.has(b.entity.eid) ? [
|
|
89
|
+
[
|
|
90
|
+
b.entity.eid,
|
|
91
|
+
named(String(comp(b, 'call')?.id ?? b.entity.eid))
|
|
92
|
+
]
|
|
93
|
+
] : []));
|
|
94
|
+
let out = [];
|
|
95
|
+
let parent = o.parent ?? null;
|
|
96
|
+
let message;
|
|
97
|
+
let put = (eid, at, said)=>{
|
|
98
|
+
let assistant = said.role == 'assistant';
|
|
99
|
+
if (assistant) message ??= `msg_${eid.replaceAll('-', '').slice(0, 24)}`;
|
|
100
|
+
else message = undefined;
|
|
101
|
+
out.push({
|
|
102
|
+
parentUuid: parent,
|
|
103
|
+
isSidechain: false,
|
|
104
|
+
userType: 'external',
|
|
105
|
+
cwd: o.cwd,
|
|
106
|
+
sessionId: o.session,
|
|
107
|
+
type: said.role,
|
|
108
|
+
message: assistant ? {
|
|
109
|
+
id: message,
|
|
110
|
+
type: 'message',
|
|
111
|
+
role: 'assistant',
|
|
112
|
+
model: 'yak',
|
|
113
|
+
content: said.content,
|
|
114
|
+
stop_reason: null,
|
|
115
|
+
usage: {
|
|
116
|
+
input_tokens: 0,
|
|
117
|
+
output_tokens: 0
|
|
118
|
+
}
|
|
119
|
+
} : said,
|
|
120
|
+
uuid: eid,
|
|
121
|
+
timestamp: String(at ?? o.now ?? new Date().toISOString()),
|
|
122
|
+
yak: {
|
|
123
|
+
entry: eid
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
parent = eid;
|
|
127
|
+
};
|
|
128
|
+
for (let b of sorted){
|
|
129
|
+
let eid = b.entity.eid;
|
|
130
|
+
let at = comp(b, 'created')?.at;
|
|
131
|
+
let kind = kindOf(b);
|
|
132
|
+
if (kind == 'input' && textOf(b)) {
|
|
133
|
+
put(eid, at, {
|
|
134
|
+
role: 'user',
|
|
135
|
+
content: textOf(b)
|
|
136
|
+
});
|
|
137
|
+
} else if (kind == 'output' && textOf(b)) {
|
|
138
|
+
put(eid, at, {
|
|
139
|
+
role: 'assistant',
|
|
140
|
+
content: [
|
|
141
|
+
{
|
|
142
|
+
type: 'text',
|
|
143
|
+
text: textOf(b)
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
});
|
|
147
|
+
} else if (kind == 'call' && ids.has(eid)) {
|
|
148
|
+
let call = comp(b, 'call');
|
|
149
|
+
put(eid, at, {
|
|
150
|
+
role: 'assistant',
|
|
151
|
+
content: [
|
|
152
|
+
{
|
|
153
|
+
type: 'tool_use',
|
|
154
|
+
id: ids.get(eid),
|
|
155
|
+
name: named(String(call.name ?? 'tool')),
|
|
156
|
+
input: args(call.args)
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
});
|
|
160
|
+
} else if (b.result && ids.has(String(comp(b, 'result')?.call))) {
|
|
161
|
+
put(eid, at, {
|
|
162
|
+
role: 'user',
|
|
163
|
+
content: [
|
|
164
|
+
{
|
|
165
|
+
type: 'tool_result',
|
|
166
|
+
tool_use_id: ids.get(String(comp(b, 'result')?.call)),
|
|
167
|
+
content: textOf(b)
|
|
168
|
+
}
|
|
169
|
+
]
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (o.note && out.length) {
|
|
174
|
+
put(derivedEid(`claude note ${o.session} ${parent}`), o.now, {
|
|
175
|
+
role: 'user',
|
|
176
|
+
content: o.note
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
};
|
|
181
|
+
// The lines of a file, each parsed, and nothing for one that is not JSON.
|
|
182
|
+
let parsedLines = (text)=>text.split('\n').flatMap((l)=>{
|
|
183
|
+
try {
|
|
184
|
+
let v = JSON.parse(l);
|
|
185
|
+
return v && typeof v == 'object' ? [
|
|
186
|
+
v
|
|
187
|
+
] : [];
|
|
188
|
+
} catch {
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
/** The transcript Claude Code keeps of `session` in `account`, in whichever
|
|
193
|
+
* working directory's projects it is, or nothing. */ export let claudeFile = (account, session)=>{
|
|
194
|
+
let dir = `${account}/projects`;
|
|
195
|
+
try {
|
|
196
|
+
for (let d of fs.readDirSync(dir)){
|
|
197
|
+
let path = `${dir}/${d.name}/${session}.jsonl`;
|
|
198
|
+
if (d.isDirectory && exists(path)) return path;
|
|
199
|
+
}
|
|
200
|
+
} catch (e) {
|
|
201
|
+
if (!fs.isNotFound(e)) throw e;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
let exists = (path)=>{
|
|
205
|
+
try {
|
|
206
|
+
return fs.statSync(path).isFile;
|
|
207
|
+
} catch (e) {
|
|
208
|
+
if (fs.isNotFound(e)) return false;
|
|
209
|
+
throw e;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Write `session` into the Claude Code `account` so it resumes there: into
|
|
214
|
+
* the file Claude Code already keeps of it, what was said since its last
|
|
215
|
+
* line, else a new file in `cwd`'s projects holding the whole conversation.
|
|
216
|
+
*/ export let openInClaude = async (g, session, o)=>{
|
|
217
|
+
let had = claudeFile(o.account, session);
|
|
218
|
+
let lines = had ? parsedLines(fs.readTextFileSync(had)) : [];
|
|
219
|
+
let cwd = o.cwd ?? lines.findLast((l)=>typeof l.cwd == 'string')?.cwd;
|
|
220
|
+
if (!cwd) {
|
|
221
|
+
throw new Error(`Claude Code keeps no transcript of ${session} in ${o.account}; ` + 'name the directory to open it in (cwd)');
|
|
222
|
+
}
|
|
223
|
+
let path = had ?? `${projectDir(o.account, cwd)}/${session}.jsonl`;
|
|
224
|
+
let entries = await g.read(query`.entry.session=${session} *`);
|
|
225
|
+
// What the file holds already: the lines Claude Code wrote, read into the
|
|
226
|
+
// session from it (or from the stream of the run that wrote it, the
|
|
227
|
+
// session's first file), and those written here before.
|
|
228
|
+
let [file] = had ? await onDisk(g, had) : [];
|
|
229
|
+
let [row] = await g.get([
|
|
230
|
+
session
|
|
231
|
+
], [
|
|
232
|
+
'session'
|
|
233
|
+
]);
|
|
234
|
+
let own = new Set([
|
|
235
|
+
file?.entity.eid,
|
|
236
|
+
comp(row, 'session')?.transcript
|
|
237
|
+
]);
|
|
238
|
+
let marked = new Set(lines.map((l)=>l.yak?.entry));
|
|
239
|
+
let held = entries.filter((b)=>marked.has(b.entity.eid) || had && own.has(comp(b, 'lines')?.file));
|
|
240
|
+
let since = Math.max(0, ...held.map(seqOf));
|
|
241
|
+
let fresh = entries.filter((b)=>seqOf(b) > since);
|
|
242
|
+
let written = claudeLines(fresh, {
|
|
243
|
+
session,
|
|
244
|
+
cwd,
|
|
245
|
+
parent: lines.findLast((l)=>typeof l.uuid == 'string')?.uuid,
|
|
246
|
+
note: handoff(fresh, await namesOf(g, fresh)),
|
|
247
|
+
now: o.now
|
|
248
|
+
});
|
|
249
|
+
if (!written.length) return {
|
|
250
|
+
path,
|
|
251
|
+
cwd,
|
|
252
|
+
written: 0
|
|
253
|
+
};
|
|
254
|
+
fs.mkdirSync(path.slice(0, path.lastIndexOf('/')), {
|
|
255
|
+
recursive: true
|
|
256
|
+
});
|
|
257
|
+
fs.writeTextFileSync(path, written.map((l)=>JSON.stringify(l) + '\n').join(''), {
|
|
258
|
+
append: true
|
|
259
|
+
});
|
|
260
|
+
return {
|
|
261
|
+
path,
|
|
262
|
+
cwd,
|
|
263
|
+
written: written.length
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
//# sourceMappingURL=resume.js.map
|
package/resume.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resume.js","sources":["./resume.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,4EAA4E;AAC5E,+EAA+E;AAC/E,+EAA+E;AAC/E,8EAA8E;AAC9E,WAAW;AACX,EAAE;AACF,6EAA6E;AAC7E,4EAA4E;AAC5E,6EAA6E;AAC7E,yEAAyE;AACzE,wCAAwC;AACxC,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,+EAA+E;AAC/E,wEAAwE;AACxE,+BAA+B;AAG/B,SAAS,UAAU,oCAAyC;AAC5D,SAAS,QAAQ,KAAK,2BAAgC;AACtD,SAAS,MAAM,gCAAqC;AACpD,YAAY,6BAAkC;AAC9C,SAAS,OAAO,EAAE,OAAO,uBAAsB;AAC/C,SAAS,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAoB;AAE3D,IAAI,OAAO,CAAC,GAAuB,OACjC,GAAG,CAAC,KAAK;AAKX;;;;;;;;;;CAUC,GACD,OAAO,IAAI,aAAa,CA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,OAAO;IAAE;IAAM;IAAK,SAAS;EAAE;EACpD,GAAG,SAAS,CAAC,KAAK,KAAK,CAAC,GAAG,KAAK,WAAW,CAAC,OAAO;IAAE,WAAW;EAAK;EACrE,GAAG,iBAAiB,CAClB,MACA,QAAQ,GAAG,CAAC,CAAC,IAAM,KAAK,SAAS,CAAC,KAAK,MAAM,IAAI,CAAC,KAClD;IAAE,QAAQ;EAAK;EAEjB,OAAO;IAAE;IAAM;IAAK,SAAS,QAAQ,MAAM;EAAC;AAC9C,EAAC"}
|
package/resume.ts
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// A session opened in Claude Code: its conversation written as the transcript
|
|
2
|
+
// Claude Code keeps (`<account>/projects/<cwd>/<session>.jsonl`), under the
|
|
3
|
+
// session's own id, so `claude --resume <id>` (or `/resume <id>` inside Claude
|
|
4
|
+
// Code) continues it there. Claude Code resumes a transcript it did not write,
|
|
5
|
+
// other harnesses' tool calls included, as long as each line has the shape of
|
|
6
|
+
// its own.
|
|
7
|
+
//
|
|
8
|
+
// Each line written here names the entry it came from (`yak.entry`), and the
|
|
9
|
+
// Claude Code reader leaves such a line alone (./readers.ts): it is already
|
|
10
|
+
// the session's. What Claude Code appends after them is read like any of its
|
|
11
|
+
// lines, into the same session, so the conversation stays one transcript
|
|
12
|
+
// whichever harness says its next line.
|
|
13
|
+
//
|
|
14
|
+
// A session Claude Code already keeps a file of gets only what was said since
|
|
15
|
+
// that file's last line (a native model took it over meanwhile, @yaks/harness
|
|
16
|
+
// `session_send`); any other gets its whole conversation. Either way the lines
|
|
17
|
+
// end with the note any model gets when it takes over another harness's
|
|
18
|
+
// conversation (./handoff.ts).
|
|
19
|
+
|
|
20
|
+
import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
21
|
+
import { derivedEid } from '@yaks/graph/identity'
|
|
22
|
+
import { line as query } from '@yaks/query'
|
|
23
|
+
import { onDisk } from '@yaks/file/local'
|
|
24
|
+
import * as fs from '@yaks/file/io'
|
|
25
|
+
import { handoff, namesOf } from './handoff.js'
|
|
26
|
+
import { kindOf, ordered, seqOf, textOf } from './state.js'
|
|
27
|
+
|
|
28
|
+
let comp = (b: Bundle | undefined, name: string) =>
|
|
29
|
+
b?.[name] as Comp | undefined
|
|
30
|
+
|
|
31
|
+
/** One line of a Claude Code transcript. */
|
|
32
|
+
export type Line = Record<string, unknown>
|
|
33
|
+
|
|
34
|
+
/** The directory Claude Code keeps a working directory's transcripts in,
|
|
35
|
+
* under its account's `projects`: the path with every character that is not
|
|
36
|
+
* a letter or a digit made a `-`.
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { projectDir } from './resume.ts'
|
|
40
|
+
* import { equal } from '@yaks/testing'
|
|
41
|
+
*
|
|
42
|
+
* equal(projectDir('/u/.claude', '/w/my.app'), '/u/.claude/projects/-w-my-app')
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export let projectDir = (account: string, cwd: string): string =>
|
|
46
|
+
`${account}/projects/${cwd.replace(/[^A-Za-z0-9]/g, '-')}`
|
|
47
|
+
|
|
48
|
+
// What the API takes as a tool's name and a call's id.
|
|
49
|
+
let named = (s: string) => s.replace(/[^A-Za-z0-9_-]/g, '_').slice(0, 64) || '_'
|
|
50
|
+
|
|
51
|
+
let args = (v: unknown): Record<string, unknown> => {
|
|
52
|
+
if (v && typeof v == 'object' && !Array.isArray(v)) {
|
|
53
|
+
return v as Record<string, unknown>
|
|
54
|
+
}
|
|
55
|
+
if (typeof v == 'string') {
|
|
56
|
+
try {
|
|
57
|
+
return args(JSON.parse(v))
|
|
58
|
+
} catch {
|
|
59
|
+
return { input: v }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return v == null ? {} : { input: v }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Entries as Claude Code's transcript lines, chained after the line `parent`
|
|
67
|
+
* names: what a person said as theirs, what a model said and called as the
|
|
68
|
+
* assistant's, and each call's result. A call nothing answered is left out,
|
|
69
|
+
* and so is a result of a call not written, as the API refuses either; so is
|
|
70
|
+
* everything that is not conversation (requests, reasoning, stops). `note`
|
|
71
|
+
* closes them, where there are any, as the person's words.
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { claudeLines } from './resume.ts'
|
|
75
|
+
* import { equal } from '@yaks/testing'
|
|
76
|
+
*
|
|
77
|
+
* let e = (eid: string, seq: number, c: object) =>
|
|
78
|
+
* ({ entity: { eid }, entry: { session: 's', seq }, ...c })
|
|
79
|
+
* let lines = claudeLines([
|
|
80
|
+
* e('a', 1, { content: { body: 'list it' } }),
|
|
81
|
+
* e('b', 2, { call: { id: 'call.1', name: 'exec_command', args: { cmd: 'ls' } } }),
|
|
82
|
+
* e('c', 3, { result: { call: 'b' }, content: { body: 'x.txt' } }),
|
|
83
|
+
* e('d', 4, { call: { id: 'call_2', name: 'shell' } }),
|
|
84
|
+
* ], { session: 's', cwd: '/w' })
|
|
85
|
+
* equal(lines.map((l) => [l.type, l.uuid, l.parentUuid]), [
|
|
86
|
+
* ['user', 'a', null], ['assistant', 'b', 'a'], ['user', 'c', 'b'],
|
|
87
|
+
* ])
|
|
88
|
+
* equal((lines[1].message as { content: unknown }).content, [
|
|
89
|
+
* { type: 'tool_use', id: 'call_1', name: 'exec_command', input: { cmd: 'ls' } },
|
|
90
|
+
* ])
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export let claudeLines = (
|
|
94
|
+
entries: Bundle[],
|
|
95
|
+
o: {
|
|
96
|
+
session: Eid
|
|
97
|
+
cwd: string
|
|
98
|
+
parent?: string
|
|
99
|
+
note?: string
|
|
100
|
+
now?: string
|
|
101
|
+
},
|
|
102
|
+
): Line[] => {
|
|
103
|
+
let sorted = ordered(entries)
|
|
104
|
+
let answered = new Set(
|
|
105
|
+
sorted.flatMap((b) => b.result ? [String(comp(b, 'result')?.call)] : []),
|
|
106
|
+
)
|
|
107
|
+
let ids = new Map(
|
|
108
|
+
sorted.flatMap((b) =>
|
|
109
|
+
b.call && answered.has(b.entity.eid)
|
|
110
|
+
? [[b.entity.eid, named(String(comp(b, 'call')?.id ?? b.entity.eid))]]
|
|
111
|
+
: []
|
|
112
|
+
),
|
|
113
|
+
)
|
|
114
|
+
let out: Line[] = []
|
|
115
|
+
let parent = o.parent ?? null
|
|
116
|
+
let message: string | undefined
|
|
117
|
+
let put = (eid: Eid, at: unknown, said: Record<string, unknown>) => {
|
|
118
|
+
let assistant = said.role == 'assistant'
|
|
119
|
+
if (assistant) message ??= `msg_${eid.replaceAll('-', '').slice(0, 24)}`
|
|
120
|
+
else message = undefined
|
|
121
|
+
out.push({
|
|
122
|
+
parentUuid: parent,
|
|
123
|
+
isSidechain: false,
|
|
124
|
+
userType: 'external',
|
|
125
|
+
cwd: o.cwd,
|
|
126
|
+
sessionId: o.session,
|
|
127
|
+
type: said.role,
|
|
128
|
+
message: assistant
|
|
129
|
+
? {
|
|
130
|
+
id: message,
|
|
131
|
+
type: 'message',
|
|
132
|
+
role: 'assistant',
|
|
133
|
+
model: 'yak',
|
|
134
|
+
content: said.content,
|
|
135
|
+
stop_reason: null,
|
|
136
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
137
|
+
}
|
|
138
|
+
: said,
|
|
139
|
+
uuid: eid,
|
|
140
|
+
timestamp: String(at ?? o.now ?? new Date().toISOString()),
|
|
141
|
+
yak: { entry: eid },
|
|
142
|
+
})
|
|
143
|
+
parent = eid
|
|
144
|
+
}
|
|
145
|
+
for (let b of sorted) {
|
|
146
|
+
let eid = b.entity.eid
|
|
147
|
+
let at = comp(b, 'created')?.at
|
|
148
|
+
let kind = kindOf(b)
|
|
149
|
+
if (kind == 'input' && textOf(b)) {
|
|
150
|
+
put(eid, at, { role: 'user', content: textOf(b) })
|
|
151
|
+
} else if (kind == 'output' && textOf(b)) {
|
|
152
|
+
put(eid, at, {
|
|
153
|
+
role: 'assistant',
|
|
154
|
+
content: [{ type: 'text', text: textOf(b) }],
|
|
155
|
+
})
|
|
156
|
+
} else if (kind == 'call' && ids.has(eid)) {
|
|
157
|
+
let call = comp(b, 'call')!
|
|
158
|
+
put(eid, at, {
|
|
159
|
+
role: 'assistant',
|
|
160
|
+
content: [{
|
|
161
|
+
type: 'tool_use',
|
|
162
|
+
id: ids.get(eid),
|
|
163
|
+
name: named(String(call.name ?? 'tool')),
|
|
164
|
+
input: args(call.args),
|
|
165
|
+
}],
|
|
166
|
+
})
|
|
167
|
+
} else if (b.result && ids.has(String(comp(b, 'result')?.call))) {
|
|
168
|
+
put(eid, at, {
|
|
169
|
+
role: 'user',
|
|
170
|
+
content: [{
|
|
171
|
+
type: 'tool_result',
|
|
172
|
+
tool_use_id: ids.get(String(comp(b, 'result')?.call)),
|
|
173
|
+
content: textOf(b),
|
|
174
|
+
}],
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (o.note && out.length) {
|
|
179
|
+
put(derivedEid(`claude note ${o.session} ${parent}`), o.now, {
|
|
180
|
+
role: 'user',
|
|
181
|
+
content: o.note,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
return out
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// The lines of a file, each parsed, and nothing for one that is not JSON.
|
|
188
|
+
let parsedLines = (text: string): Line[] =>
|
|
189
|
+
text.split('\n').flatMap((l) => {
|
|
190
|
+
try {
|
|
191
|
+
let v = JSON.parse(l)
|
|
192
|
+
return v && typeof v == 'object' ? [v as Line] : []
|
|
193
|
+
} catch {
|
|
194
|
+
return []
|
|
195
|
+
}
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
/** The transcript Claude Code keeps of `session` in `account`, in whichever
|
|
199
|
+
* working directory's projects it is, or nothing. */
|
|
200
|
+
export let claudeFile = (account: string, session: Eid): string | undefined => {
|
|
201
|
+
let dir = `${account}/projects`
|
|
202
|
+
try {
|
|
203
|
+
for (let d of fs.readDirSync(dir)) {
|
|
204
|
+
let path = `${dir}/${d.name}/${session}.jsonl`
|
|
205
|
+
if (d.isDirectory && exists(path)) return path
|
|
206
|
+
}
|
|
207
|
+
} catch (e) {
|
|
208
|
+
if (!fs.isNotFound(e)) throw e
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let exists = (path: string) => {
|
|
213
|
+
try {
|
|
214
|
+
return fs.statSync(path).isFile
|
|
215
|
+
} catch (e) {
|
|
216
|
+
if (fs.isNotFound(e)) return false
|
|
217
|
+
throw e
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Where a session was written for Claude Code, and how to resume it. */
|
|
222
|
+
export type Opened = {
|
|
223
|
+
path: string
|
|
224
|
+
cwd: string
|
|
225
|
+
/** the lines written: none where the file already held the conversation */
|
|
226
|
+
written: number
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Write `session` into the Claude Code `account` so it resumes there: into
|
|
231
|
+
* the file Claude Code already keeps of it, what was said since its last
|
|
232
|
+
* line, else a new file in `cwd`'s projects holding the whole conversation.
|
|
233
|
+
*/
|
|
234
|
+
export let openInClaude = async (
|
|
235
|
+
g: Graph,
|
|
236
|
+
session: Eid,
|
|
237
|
+
o: { account: string; cwd?: string; now?: string },
|
|
238
|
+
): Promise<Opened> => {
|
|
239
|
+
let had = claudeFile(o.account, session)
|
|
240
|
+
let lines = had ? parsedLines(fs.readTextFileSync(had)) : []
|
|
241
|
+
let cwd = o.cwd ??
|
|
242
|
+
lines.findLast((l) => typeof l.cwd == 'string')?.cwd as string | undefined
|
|
243
|
+
if (!cwd) {
|
|
244
|
+
throw new Error(
|
|
245
|
+
`Claude Code keeps no transcript of ${session} in ${o.account}; ` +
|
|
246
|
+
'name the directory to open it in (cwd)',
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
let path = had ?? `${projectDir(o.account, cwd)}/${session}.jsonl`
|
|
250
|
+
let entries = await g.read(query`.entry.session=${session} *`)
|
|
251
|
+
// What the file holds already: the lines Claude Code wrote, read into the
|
|
252
|
+
// session from it (or from the stream of the run that wrote it, the
|
|
253
|
+
// session's first file), and those written here before.
|
|
254
|
+
let [file] = had ? await onDisk(g, had) : []
|
|
255
|
+
let [row] = await g.get([session], ['session'])
|
|
256
|
+
let own = new Set([file?.entity.eid, comp(row, 'session')?.transcript])
|
|
257
|
+
let marked = new Set(
|
|
258
|
+
lines.map((l) => (l.yak as { entry?: string } | undefined)?.entry),
|
|
259
|
+
)
|
|
260
|
+
let held = entries.filter((b) =>
|
|
261
|
+
marked.has(b.entity.eid) ||
|
|
262
|
+
(had && own.has(comp(b, 'lines')?.file as string | undefined))
|
|
263
|
+
)
|
|
264
|
+
let since = Math.max(0, ...held.map(seqOf))
|
|
265
|
+
let fresh = entries.filter((b) => seqOf(b) > since)
|
|
266
|
+
let written = claudeLines(fresh, {
|
|
267
|
+
session,
|
|
268
|
+
cwd,
|
|
269
|
+
parent: lines.findLast((l) => typeof l.uuid == 'string')?.uuid as
|
|
270
|
+
| string
|
|
271
|
+
| undefined,
|
|
272
|
+
note: handoff(fresh, await namesOf(g, fresh)),
|
|
273
|
+
now: o.now,
|
|
274
|
+
})
|
|
275
|
+
if (!written.length) return { path, cwd, written: 0 }
|
|
276
|
+
fs.mkdirSync(path.slice(0, path.lastIndexOf('/')), { recursive: true })
|
|
277
|
+
fs.writeTextFileSync(
|
|
278
|
+
path,
|
|
279
|
+
written.map((l) => JSON.stringify(l) + '\n').join(''),
|
|
280
|
+
{ append: true },
|
|
281
|
+
)
|
|
282
|
+
return { path, cwd, written: written.length }
|
|
283
|
+
}
|
package/routes.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// The hook door, exported as `@yaks/session/routes`: where a harness reports
|
|
2
|
+
// each tool call of a session it runs (Claude Code's HTTP hook, installed by
|
|
3
|
+
// ./hooks.ts), and hears back what the bus owes that session (./bus.ts
|
|
4
|
+
// `heard`). A harness runs many contexts under one session id, and only its
|
|
5
|
+
// hook says which context an event came from, so this is the one place the
|
|
6
|
+
// session's own line of work can be told from its subagents: before a call,
|
|
7
|
+
// the hook says which run makes it, and a door the call then reaches writes
|
|
8
|
+
// as that run (./announce.ts).
|
|
9
|
+
//
|
|
10
|
+
// A prompt is answered with its transcript's timing and, beside it, what the
|
|
11
|
+
// configured person said, kept and decided before on the same subject
|
|
12
|
+
// (@yaks/memory/earlier). The recall includes reads that finish within
|
|
13
|
+
// RECALL ms. A prompt the harness
|
|
14
|
+
// wrote, a background task's notification, recalls nothing.
|
|
15
|
+
//
|
|
16
|
+
// `/yak:continue` is answered here, before the harness asks its model
|
|
17
|
+
// anything (./continue.ts), and so is Claude Code's status line
|
|
18
|
+
// (`POST /session/status`, ./statusline.ts).
|
|
19
|
+
//
|
|
20
|
+
// A body that is not a hook event is the caller's mistake, refused with a
|
|
21
|
+
// 400. Anything else that goes wrong is thrown to the host, which reports it;
|
|
22
|
+
// the harness treats a failed hook as nothing to add and carries on.
|
|
23
|
+
import { heard } from "./bus.js";
|
|
24
|
+
import { hear } from "./announce.js";
|
|
25
|
+
import { lifecycle } from "./lifecycle.js";
|
|
26
|
+
import { promptContext } from "./prompt.js";
|
|
27
|
+
import { RECALL, recall } from "./recall.js";
|
|
28
|
+
import { carryOn, continued } from "./continue.js";
|
|
29
|
+
import { statusLine } from "./statusline.js";
|
|
30
|
+
export { RECALL };
|
|
31
|
+
/** Where the harness posts its hook events. */ export let PATH = '/session/hook';
|
|
32
|
+
/** Where Claude Code's status line posts what it was handed. */ export let STATUS = '/session/status';
|
|
33
|
+
// Whether the harness still waits for this answer. It gives up on a hook after
|
|
34
|
+
// its timeout (2 s, ./hooks.ts), and Deno tells a handler its client hung up
|
|
35
|
+
// only once the event loop turns, so the door lets it turn before asking.
|
|
36
|
+
let open = (request)=>new Promise((done)=>setTimeout(()=>done(!request.signal.aborted)));
|
|
37
|
+
// A prompt the harness wrote itself: Claude Code submits a finished
|
|
38
|
+
// background task's notification as a prompt. It is nobody's words, so
|
|
39
|
+
// nothing was said before on its subject.
|
|
40
|
+
let NOTICE = /^\s*<task-notification>/;
|
|
41
|
+
/** `POST /session/hook`: one hook event in, the context it adds out. */ export let routes = (host, { wait = RECALL } = {})=>{
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
method: 'POST',
|
|
45
|
+
path: STATUS,
|
|
46
|
+
handle: async (request)=>{
|
|
47
|
+
// It writes plan readings: a browser's cross-origin post is no status
|
|
48
|
+
// line.
|
|
49
|
+
if (request.headers.has('origin')) {
|
|
50
|
+
return new Response('', {
|
|
51
|
+
status: 403
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
let status = await request.json().catch(()=>null);
|
|
55
|
+
if (!status || typeof status != 'object' || Array.isArray(status)) {
|
|
56
|
+
return new Response('', {
|
|
57
|
+
status: 400
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return new Response(await statusLine(host.graph, status));
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
method: 'POST',
|
|
65
|
+
path: '/session/earlier',
|
|
66
|
+
handle: async (request)=>{
|
|
67
|
+
// This door carries private historical words; apply the host's identity
|
|
68
|
+
// boundary just as the graph read door does. Browser cross-origin posts
|
|
69
|
+
// are not prompt hooks.
|
|
70
|
+
if (request.headers.has('origin')) {
|
|
71
|
+
return new Response('', {
|
|
72
|
+
status: 403
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
await host.who?.(request);
|
|
76
|
+
let input = await request.json().catch(()=>null);
|
|
77
|
+
if (typeof input?.words != 'string' || typeof input?.before != 'string') {
|
|
78
|
+
return new Response('', {
|
|
79
|
+
status: 400
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return new Response(await recall(host, null).said(input.words, input.before));
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
method: 'POST',
|
|
87
|
+
path: PATH,
|
|
88
|
+
handle: async (request)=>{
|
|
89
|
+
let hook = await request.json().catch(()=>null);
|
|
90
|
+
if (!hook || typeof hook != 'object' || Array.isArray(hook)) {
|
|
91
|
+
return Response.json({
|
|
92
|
+
error: 'a hook event is a JSON object'
|
|
93
|
+
}, {
|
|
94
|
+
status: 400
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// A prompt's context is its transcript's timing, read as the CLI reads it
|
|
98
|
+
// (./cli.ts), and what its person said before, within `wait`.
|
|
99
|
+
let event = hook;
|
|
100
|
+
if (event.hook_event_name == 'UserPromptSubmit') {
|
|
101
|
+
let words = typeof event.prompt == 'string' ? event.prompt : '';
|
|
102
|
+
let asked = continued(words);
|
|
103
|
+
if (asked) {
|
|
104
|
+
return Response.json({
|
|
105
|
+
decision: 'block',
|
|
106
|
+
reason: await carryOn(host, request, event, asked)
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
let recalled = NOTICE.test(words) ? '' : await recall(host, wait).said(words, new Date().toISOString());
|
|
110
|
+
let text = [
|
|
111
|
+
promptContext(event, host.tailText),
|
|
112
|
+
recalled
|
|
113
|
+
].filter(Boolean).join('\n\n');
|
|
114
|
+
return Response.json(text ? {
|
|
115
|
+
hookSpecificOutput: {
|
|
116
|
+
hookEventName: 'UserPromptSubmit',
|
|
117
|
+
additionalContext: text
|
|
118
|
+
}
|
|
119
|
+
} : {});
|
|
120
|
+
}
|
|
121
|
+
let edge = await lifecycle(host, request, event);
|
|
122
|
+
if (edge) return edge;
|
|
123
|
+
if (await hear(host.graph, hook)) return Response.json({});
|
|
124
|
+
return Response.json(await heard(host, hook, ()=>open(request)));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
];
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=routes.js.map
|