@jive-sh/skill-sync 0.11.0-latest-darwin-x64 → 0.11.0
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/README.md +346 -0
- package/bin/package.json +1 -0
- package/bin/skill-sync +34 -0
- package/bin/skill-sync-daemon +34 -0
- package/dist/daemons/skill-sync-daemon.js +664 -0
- package/dist/daemons/skill-sync-daemon.js.map +1 -0
- package/dist/entrypoints/skill-sync.js +7 -0
- package/dist/entrypoints/skill-sync.js.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/agents/agent-tool.js +102 -0
- package/dist/internal/agents/agent-tool.js.map +1 -0
- package/dist/internal/agents/claude.js +424 -0
- package/dist/internal/agents/claude.js.map +1 -0
- package/dist/internal/agents/codex.js +270 -0
- package/dist/internal/agents/codex.js.map +1 -0
- package/dist/internal/agents/command.js +219 -0
- package/dist/internal/agents/command.js.map +1 -0
- package/dist/internal/agents/grok.js +170 -0
- package/dist/internal/agents/grok.js.map +1 -0
- package/dist/internal/agents/index.js +70 -0
- package/dist/internal/agents/index.js.map +1 -0
- package/dist/internal/cli.js +218 -0
- package/dist/internal/cli.js.map +1 -0
- package/dist/internal/comment-watch/held.js +14 -0
- package/dist/internal/comment-watch/held.js.map +1 -0
- package/dist/internal/comment-watch/key.js +4 -0
- package/dist/internal/comment-watch/key.js.map +1 -0
- package/dist/internal/comment-watch/message.js +37 -0
- package/dist/internal/comment-watch/message.js.map +1 -0
- package/dist/internal/comment-watch/paths.js +16 -0
- package/dist/internal/comment-watch/paths.js.map +1 -0
- package/dist/internal/comment-watch/platform.js +66 -0
- package/dist/internal/comment-watch/platform.js.map +1 -0
- package/dist/internal/comment-watch/protocol.js +124 -0
- package/dist/internal/comment-watch/protocol.js.map +1 -0
- package/dist/internal/comment-watch/recent.js +27 -0
- package/dist/internal/comment-watch/recent.js.map +1 -0
- package/dist/internal/comment-watch/replies.js +28 -0
- package/dist/internal/comment-watch/replies.js.map +1 -0
- package/dist/internal/comment-watch/service.js +174 -0
- package/dist/internal/comment-watch/service.js.map +1 -0
- package/dist/internal/comment-watch/settings.js +66 -0
- package/dist/internal/comment-watch/settings.js.map +1 -0
- package/dist/internal/comment-watch/source/github-pull-requests/conversations.js +85 -0
- package/dist/internal/comment-watch/source/github-pull-requests/conversations.js.map +1 -0
- package/dist/internal/comment-watch/source/github-pull-requests/impl.js +276 -0
- package/dist/internal/comment-watch/source/github-pull-requests/impl.js.map +1 -0
- package/dist/internal/comment-watch/source/github-pull-requests/queries.js +97 -0
- package/dist/internal/comment-watch/source/github-pull-requests/queries.js.map +1 -0
- package/dist/internal/comment-watch/source/google-doc/doc.js +57 -0
- package/dist/internal/comment-watch/source/google-doc/doc.js.map +1 -0
- package/dist/internal/comment-watch/source/google-doc/impl.js +190 -0
- package/dist/internal/comment-watch/source/google-doc/impl.js.map +1 -0
- package/dist/internal/comment-watch/source/http-api.js +74 -0
- package/dist/internal/comment-watch/source/http-api.js.map +1 -0
- package/dist/internal/comment-watch/source/interface.js +48 -0
- package/dist/internal/comment-watch/source/interface.js.map +1 -0
- package/dist/internal/comment-watch/source/sign-in.js +40 -0
- package/dist/internal/comment-watch/source/sign-in.js.map +1 -0
- package/dist/internal/comment-watch/source/sources.js +15 -0
- package/dist/internal/comment-watch/source/sources.js.map +1 -0
- package/dist/internal/comment-watch/store/impl.js +242 -0
- package/dist/internal/comment-watch/store/impl.js.map +1 -0
- package/dist/internal/comment-watch/store/interface.js +10 -0
- package/dist/internal/comment-watch/store/interface.js.map +1 -0
- package/dist/internal/comment-watch/t3.js +49 -0
- package/dist/internal/comment-watch/t3.js.map +1 -0
- package/dist/internal/comment-watch/watching.js +65 -0
- package/dist/internal/comment-watch/watching.js.map +1 -0
- package/dist/internal/comment-watch/workflow/comment-watch.js +19 -0
- package/dist/internal/comment-watch/workflow/comment-watch.js.map +1 -0
- package/dist/internal/comment-watch/workflow/deliver.js +104 -0
- package/dist/internal/comment-watch/workflow/deliver.js.map +1 -0
- package/dist/internal/comment-watch/workflow/find.js +67 -0
- package/dist/internal/comment-watch/workflow/find.js.map +1 -0
- package/dist/internal/comment-watch/workflow/post-once.js +66 -0
- package/dist/internal/comment-watch/workflow/post-once.js.map +1 -0
- package/dist/internal/comment-watch/workflow/post.js +40 -0
- package/dist/internal/comment-watch/workflow/post.js.map +1 -0
- package/dist/internal/config.js +131 -0
- package/dist/internal/config.js.map +1 -0
- package/dist/internal/control.js +113 -0
- package/dist/internal/control.js.map +1 -0
- package/dist/internal/daemon-control/impl.js +80 -0
- package/dist/internal/daemon-control/impl.js.map +1 -0
- package/dist/internal/daemon-control/interface.js +4 -0
- package/dist/internal/daemon-control/interface.js.map +1 -0
- package/dist/internal/folders.js +47 -0
- package/dist/internal/folders.js.map +1 -0
- package/dist/internal/local-skills.js +43 -0
- package/dist/internal/local-skills.js.map +1 -0
- package/dist/internal/path.js +36 -0
- package/dist/internal/path.js.map +1 -0
- package/dist/internal/server.js +80 -0
- package/dist/internal/server.js.map +1 -0
- package/dist/internal/skill-check.js +83 -0
- package/dist/internal/skill-check.js.map +1 -0
- package/dist/internal/skill-refinement/evals/cases.js +143 -0
- package/dist/internal/skill-refinement/evals/cases.js.map +1 -0
- package/dist/internal/skill-refinement/evals/opus-action-judge-original.json +350 -0
- package/dist/internal/skill-refinement/evals/opus-judge-original.json +165 -0
- package/dist/internal/skill-refinement/feedback/import-legacy.js +13 -0
- package/dist/internal/skill-refinement/feedback/import-legacy.js.map +1 -0
- package/dist/internal/skill-refinement/feedback/intake.js +52 -0
- package/dist/internal/skill-refinement/feedback/intake.js.map +1 -0
- package/dist/internal/skill-refinement/feedback/managed-intake.js +19 -0
- package/dist/internal/skill-refinement/feedback/managed-intake.js.map +1 -0
- package/dist/internal/skill-refinement/feedback/outbox.js +44 -0
- package/dist/internal/skill-refinement/feedback/outbox.js.map +1 -0
- package/dist/internal/skill-refinement/jev/impl.js +63 -0
- package/dist/internal/skill-refinement/jev/impl.js.map +1 -0
- package/dist/internal/skill-refinement/jev/interface.js +6 -0
- package/dist/internal/skill-refinement/jev/interface.js.map +1 -0
- package/dist/internal/skill-refinement/managed-daemon.js +30 -0
- package/dist/internal/skill-refinement/managed-daemon.js.map +1 -0
- package/dist/internal/skill-refinement/managed-worker-credentials.js +60 -0
- package/dist/internal/skill-refinement/managed-worker-credentials.js.map +1 -0
- package/dist/internal/skill-refinement/managed-worker-files.js +25 -0
- package/dist/internal/skill-refinement/managed-worker-files.js.map +1 -0
- package/dist/internal/skill-refinement/managed-worker-loop.js +36 -0
- package/dist/internal/skill-refinement/managed-worker-loop.js.map +1 -0
- package/dist/internal/skill-refinement/organization-site.js +13 -0
- package/dist/internal/skill-refinement/organization-site.js.map +1 -0
- package/dist/internal/skill-refinement/paths.js +62 -0
- package/dist/internal/skill-refinement/paths.js.map +1 -0
- package/dist/internal/skill-refinement/platform.js +67 -0
- package/dist/internal/skill-refinement/platform.js.map +1 -0
- package/dist/internal/skill-refinement/service.js +365 -0
- package/dist/internal/skill-refinement/service.js.map +1 -0
- package/dist/internal/skill-refinement/skills/skill-text.js +219 -0
- package/dist/internal/skill-refinement/skills/skill-text.js.map +1 -0
- package/dist/internal/skill-refinement/workflow/retry.js +44 -0
- package/dist/internal/skill-refinement/workflow/retry.js.map +1 -0
- package/dist/internal/skill-refinement/workflow/skill-refinement.js +690 -0
- package/dist/internal/skill-refinement/workflow/skill-refinement.js.map +1 -0
- package/dist/internal/skill-refinement/writer/impl.js +2 -0
- package/dist/internal/skill-refinement/writer/impl.js.map +1 -0
- package/dist/internal/skill-refinement/writer/interface.js +2 -0
- package/dist/internal/skill-refinement/writer/interface.js.map +1 -0
- package/dist/internal/sync.js +182 -0
- package/dist/internal/sync.js.map +1 -0
- package/dist/internal/wire.js +31 -0
- package/dist/internal/wire.js.map +1 -0
- package/dist/internal/writer/claude.js +84 -0
- package/dist/internal/writer/claude.js.map +1 -0
- package/dist/internal/writer/codex.js +102 -0
- package/dist/internal/writer/codex.js.map +1 -0
- package/dist/internal/writer/interface.js +6 -0
- package/dist/internal/writer/interface.js.map +1 -0
- package/dist/internal/writer/live.js +13 -0
- package/dist/internal/writer/live.js.map +1 -0
- package/dist/internal/writer/reset.js +38 -0
- package/dist/internal/writer/reset.js.map +1 -0
- package/dist/internal/writer/selection.js +79 -0
- package/dist/internal/writer/selection.js.map +1 -0
- package/package.json +45 -11
- package/postinstall.cjs +663 -0
- package/src/daemons/skill-sync-daemon.ts +677 -0
- package/src/entrypoints/skill-sync.ts +12 -0
- package/src/index.ts +15 -0
- package/src/internal/agents/agent-tool.ts +149 -0
- package/src/internal/agents/claude.ts +442 -0
- package/src/internal/agents/codex.ts +288 -0
- package/src/internal/agents/command.ts +257 -0
- package/src/internal/agents/grok.ts +174 -0
- package/src/internal/agents/index.ts +74 -0
- package/src/internal/cli.ts +249 -0
- package/src/internal/comment-watch/held.ts +20 -0
- package/src/internal/comment-watch/key.ts +5 -0
- package/src/internal/comment-watch/message.ts +39 -0
- package/src/internal/comment-watch/paths.ts +17 -0
- package/src/internal/comment-watch/platform.ts +69 -0
- package/src/internal/comment-watch/protocol.ts +140 -0
- package/src/internal/comment-watch/recent.ts +26 -0
- package/src/internal/comment-watch/replies.ts +28 -0
- package/src/internal/comment-watch/service.ts +198 -0
- package/src/internal/comment-watch/settings.ts +82 -0
- package/src/internal/comment-watch/source/github-pull-requests/conversations.ts +111 -0
- package/src/internal/comment-watch/source/github-pull-requests/impl.ts +291 -0
- package/src/internal/comment-watch/source/github-pull-requests/queries.ts +113 -0
- package/src/internal/comment-watch/source/google-doc/doc.ts +76 -0
- package/src/internal/comment-watch/source/google-doc/impl.ts +198 -0
- package/src/internal/comment-watch/source/http-api.ts +93 -0
- package/src/internal/comment-watch/source/interface.ts +116 -0
- package/src/internal/comment-watch/source/sign-in.ts +45 -0
- package/src/internal/comment-watch/source/sources.ts +20 -0
- package/src/internal/comment-watch/store/impl.ts +268 -0
- package/src/internal/comment-watch/store/interface.ts +83 -0
- package/src/internal/comment-watch/t3.ts +68 -0
- package/src/internal/comment-watch/watching.ts +122 -0
- package/src/internal/comment-watch/workflow/comment-watch.ts +34 -0
- package/src/internal/comment-watch/workflow/deliver.ts +115 -0
- package/src/internal/comment-watch/workflow/find.ts +78 -0
- package/src/internal/comment-watch/workflow/post-once.ts +72 -0
- package/src/internal/comment-watch/workflow/post.ts +56 -0
- package/src/internal/config.ts +172 -0
- package/src/internal/control.ts +165 -0
- package/src/internal/daemon-control/impl.ts +94 -0
- package/src/internal/daemon-control/interface.ts +46 -0
- package/src/internal/folders.ts +50 -0
- package/src/internal/local-skills.ts +58 -0
- package/src/internal/path.ts +32 -0
- package/src/internal/server.ts +76 -0
- package/src/internal/skill-check.ts +97 -0
- package/src/internal/skill-refinement/evals/cases.ts +167 -0
- package/src/internal/skill-refinement/evals/opus-action-judge-original.json +350 -0
- package/src/internal/skill-refinement/evals/opus-judge-original.json +165 -0
- package/src/internal/skill-refinement/feedback/import-legacy.ts +16 -0
- package/src/internal/skill-refinement/feedback/intake.ts +66 -0
- package/src/internal/skill-refinement/feedback/managed-intake.ts +19 -0
- package/src/internal/skill-refinement/feedback/outbox.ts +50 -0
- package/src/internal/skill-refinement/jev/impl.ts +76 -0
- package/src/internal/skill-refinement/jev/interface.ts +56 -0
- package/src/internal/skill-refinement/managed-daemon.ts +38 -0
- package/src/internal/skill-refinement/managed-worker-credentials.ts +59 -0
- package/src/internal/skill-refinement/managed-worker-files.ts +24 -0
- package/src/internal/skill-refinement/managed-worker-loop.ts +46 -0
- package/src/internal/skill-refinement/organization-site.ts +15 -0
- package/src/internal/skill-refinement/paths.ts +87 -0
- package/src/internal/skill-refinement/platform.ts +71 -0
- package/src/internal/skill-refinement/service.ts +418 -0
- package/src/internal/skill-refinement/skills/skill-text.ts +292 -0
- package/src/internal/skill-refinement/workflow/retry.ts +61 -0
- package/src/internal/skill-refinement/workflow/skill-refinement.ts +777 -0
- package/src/internal/skill-refinement/writer/impl.ts +1 -0
- package/src/internal/skill-refinement/writer/interface.ts +1 -0
- package/src/internal/sync.ts +216 -0
- package/src/internal/wire.ts +439 -0
- package/src/internal/writer/claude.ts +98 -0
- package/src/internal/writer/codex.ts +93 -0
- package/src/internal/writer/interface.ts +48 -0
- package/src/internal/writer/live.ts +19 -0
- package/src/internal/writer/reset.ts +34 -0
- package/src/internal/writer/selection.ts +84 -0
- package/multi-call-binary +0 -4
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE PROTOCOL BETWEEN A PERSON AND THE AGENTS THAT ANSWER FROM THEIR ACCOUNT, the same wherever a comment is left. Both post
|
|
3
|
+
* as one account, so what a post says is the only way to tell who wrote it:
|
|
4
|
+
*
|
|
5
|
+
* an agent's post starts with 🤖:, and is never an order, whatever else it says;
|
|
6
|
+
* on a pull request, a comment of theirs is for the agent when it tags it (`@clanker`), and only then;
|
|
7
|
+
* a change waits: the agent posts a plan of its own, **Plan**, and makes it once they approve it.
|
|
8
|
+
*
|
|
9
|
+
* Nothing this posts or delivers holds the bare tag: on GitHub it notifies whoever has that login, and in a post it would be
|
|
10
|
+
* an order to itself. All of it is pure.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** What every agent's post starts with. */
|
|
14
|
+
export const AGENT_PREFIX = '🤖:';
|
|
15
|
+
/** What a plan's text starts with, after its opening. */
|
|
16
|
+
export const PLAN = '**Plan**';
|
|
17
|
+
/** What code posts, after the opening, where a plan is approved; the agent edits it into its "Done in". */
|
|
18
|
+
export const ACKNOWLEDGEMENT = 'Plan is approved, working on it...';
|
|
19
|
+
/** What code shows on a comment as soon as it is queued, until it is answered: a reaction, or where there is none a reply. */
|
|
20
|
+
export const EYES = '👀';
|
|
21
|
+
export const EYES_REPLY = `${AGENT_PREFIX} ${EYES}`;
|
|
22
|
+
export const DEFAULT_TAG = 'clanker';
|
|
23
|
+
/** What another automation on the same account replies where it has seen a comment in a Doc, as this one shows 👀. */
|
|
24
|
+
export const SEEN_ELSEWHERE = '...';
|
|
25
|
+
|
|
26
|
+
const ACKNOWLEDGEMENT_LINE = '<!-- skill-sync comment-watch: plan approval ';
|
|
27
|
+
/** How the daemon that posted the first acknowledgements began the line. */
|
|
28
|
+
const EARLIER_ACKNOWLEDGEMENT_LINE = '<!-- pr-comment-daemon: plan approval ';
|
|
29
|
+
/**
|
|
30
|
+
* The hidden line an acknowledgement on GitHub ends with, before and after the agent's edit: how it is found again, so it is
|
|
31
|
+
* never posted twice, and how it is told from a plan or an answer.
|
|
32
|
+
*/
|
|
33
|
+
export const acknowledgementLine = (approval: string): string => `${ACKNOWLEDGEMENT_LINE}${approval} -->`;
|
|
34
|
+
/** Whether a post holds an approval's hidden line, as it is written now or as the earlier daemon wrote it. */
|
|
35
|
+
export const holdsAcknowledgementLine = (input: { readonly text: string; readonly line: string }): boolean =>
|
|
36
|
+
input.text.includes(input.line) || input.text.includes(input.line.replace(ACKNOWLEDGEMENT_LINE, EARLIER_ACKNOWLEDGEMENT_LINE));
|
|
37
|
+
export const isAcknowledgement = (text: string): boolean =>
|
|
38
|
+
text.includes(ACKNOWLEDGEMENT_LINE) || text.includes(EARLIER_ACKNOWLEDGEMENT_LINE) || afterOpening(text).startsWith(ACKNOWLEDGEMENT);
|
|
39
|
+
|
|
40
|
+
/** Whether an agent wrote a post: one that starts with 🤖:, or is exactly another automation's "...". */
|
|
41
|
+
export const isAgents = (text: string): boolean => text.trimStart().startsWith(AGENT_PREFIX) || text.trim() === SEEN_ELSEWHERE;
|
|
42
|
+
|
|
43
|
+
/** An agent's words, without the 🤖: and, where it opens with one, the link to what it answers. */
|
|
44
|
+
const afterOpening = (text: string): string => {
|
|
45
|
+
const words = text.trimStart().slice(AGENT_PREFIX.length).trimStart();
|
|
46
|
+
return text.trimStart().startsWith(AGENT_PREFIX) ? (words.startsWith('https://') ? words.replace(/^\S+\s*/, '') : words) : '';
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** Whether a post is this watch's 👀 reply: one only it posts, and deletes. */
|
|
50
|
+
export const isEyes = (text: string): boolean => afterOpening(text).trim() === EYES;
|
|
51
|
+
|
|
52
|
+
/** Whether a post only says that a comment was received, by this watch or another automation: no answer to it. */
|
|
53
|
+
export const isReceipt = (text: string): boolean => isEyes(text) || text.trim() === SEEN_ELSEWHERE;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether a post is a plan. A plan is its own reply, starting **Plan**; a line starting so further down counts too, since an
|
|
57
|
+
* approval of a plan put after an answer must still reach the agent.
|
|
58
|
+
*/
|
|
59
|
+
export const isPlan = (text: string): boolean => isAgents(text) && !isAcknowledgement(text) && afterOpening(text).split('\n').some((line) => line.startsWith(PLAN));
|
|
60
|
+
|
|
61
|
+
/** Whether a reply of theirs approves the plan above it, where the place has no 👍. */
|
|
62
|
+
export const isYes = (text: string): boolean => /^yes[.!]*$/i.test(text.trim());
|
|
63
|
+
|
|
64
|
+
const escaped = (s: string) => s.replace(/[.*+?^$()|[\]\\{}]/g, '\\$&');
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A comment's own words as Markdown shows them: no quoted line, no code (fenced, indented, in backticks, in `<code>` or
|
|
68
|
+
* `<pre>`), no HTML comment. What is cut leaves a space, so the words around it stay apart.
|
|
69
|
+
*/
|
|
70
|
+
export const ownWords = (text: string): string => {
|
|
71
|
+
const kept: string[] = [];
|
|
72
|
+
let fence: string | null = null;
|
|
73
|
+
let previous = '';
|
|
74
|
+
let inCode = false;
|
|
75
|
+
for (const line of text.replace(/\r\n?/g, '\n').replace(/<!--[\s\S]*?(?:-->|$)/g, ' ').replace(/<(pre|code)\b[^>]*>[\s\S]*?(?:<\/\1>|$)/gi, ' ').split('\n')) {
|
|
76
|
+
const mark = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
77
|
+
if (fence !== null) {
|
|
78
|
+
if (mark !== null && mark[1]!.startsWith(fence) && mark[2]!.trim() === '') fence = null;
|
|
79
|
+
kept.push('');
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
// A backtick fence's first line holds no other backtick: with one it is a code span in a sentence.
|
|
83
|
+
if (mark !== null && !(mark[1]!.startsWith('`') && mark[2]!.includes('`'))) {
|
|
84
|
+
fence = mark[1]!;
|
|
85
|
+
kept.push('');
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const quoted = /^ {0,3}>/.test(line);
|
|
89
|
+
// Four spaces start a code block after a blank line, never in a list, where they go on with an item.
|
|
90
|
+
const indented: boolean = /^(?: {4}|\t)/.test(line) && line.trim() !== '' && (inCode || (previous.trim() === '' && !lastWordsAreAList(kept)));
|
|
91
|
+
inCode = indented || (inCode && line.trim() === '');
|
|
92
|
+
previous = line;
|
|
93
|
+
kept.push(quoted || indented ? '' : line);
|
|
94
|
+
}
|
|
95
|
+
// A code span ends at as many backticks as it starts with, in the same paragraph.
|
|
96
|
+
return kept.join('\n').split(/\n[ \t]*\n/).map((paragraph) => paragraph.replace(/(?<!`)(`+)(?!`)([\s\S]*?[^`])\1(?!`)/g, ' ')).join('\n\n');
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const lastWordsAreAList = (lines: ReadonlyArray<string>): boolean => {
|
|
100
|
+
for (let i = lines.length - 1; i >= 0; i -= 1) {
|
|
101
|
+
const line = lines[i]!;
|
|
102
|
+
if (line.trim() === '') continue;
|
|
103
|
+
if (/^ {0,3}(?:[-*+]|\d{1,9}[.)])\s/.test(line)) return true;
|
|
104
|
+
// A line of an item's own text: the item is further up.
|
|
105
|
+
if (!/^(?: {2,}|\t)/.test(line)) return false;
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/** Whether text tags the agent: the whole name after an @ that starts a word, in any case, in its own words. */
|
|
111
|
+
export const isTagged = (input: { readonly text: string; readonly tag: string }): boolean =>
|
|
112
|
+
new RegExp(`(?<![\\p{L}\\p{N}/@]|[\\p{L}\\p{N}]_)@${escaped(input.tag)}(?![\\p{L}\\p{N}]|[-_./@][\\p{L}\\p{N}])`, 'iu').test(ownWords(input.text));
|
|
113
|
+
|
|
114
|
+
/** Whether a comment of theirs is for the agent, where it has to say so. An agent's post never is. */
|
|
115
|
+
export const isOrder = (input: { readonly text: string; readonly tag: string }): boolean =>
|
|
116
|
+
!isAgents(input.text) && isTagged(input);
|
|
117
|
+
|
|
118
|
+
/** Text with the agent's name in place of its tag, however the @ is written: what is posted, and what is delivered. */
|
|
119
|
+
export const withoutTag = (input: { readonly text: string; readonly tag: string }): string =>
|
|
120
|
+
input.text.replace(new RegExp(`(?:@|�*64;|�*40;|@)+(?=${escaped(input.tag)})`, 'gi'), '');
|
|
121
|
+
|
|
122
|
+
/** An agent's words as they are posted: 🤖:, the link to what they answer where the place doesn't thread replies, the words. */
|
|
123
|
+
export const asAgents = (input: { readonly text: string; readonly opening: string | null; readonly tag: string }): string => {
|
|
124
|
+
const words = input.text.trim().replace(new RegExp(`^(?:${AGENT_PREFIX}\\s*)+`, 'u'), '');
|
|
125
|
+
return withoutTag({ text: [AGENT_PREFIX, ...(input.opening === null || words.startsWith(input.opening) ? [] : [input.opening]), words].join(' '), tag: input.tag });
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** Why a reply is not one the protocol lets an agent post; none where it is. */
|
|
129
|
+
export const replyRefusal = (text: string): string | null => {
|
|
130
|
+
const words = text.trim().replace(new RegExp(`^(?:${AGENT_PREFIX}\\s*)+`, 'u'), '');
|
|
131
|
+
if (words === '') return 'it is empty';
|
|
132
|
+
const lines = words.split('\n');
|
|
133
|
+
return !words.startsWith(PLAN) && lines.some((line) => line.startsWith(PLAN)) ? `a plan is a reply of its own, starting ${PLAN}: post the answer first, then the plan` : null;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** Two posts' words as a service may have given them back: formatting marks and spacing apart. */
|
|
137
|
+
export const sameWords = (a: string, b: string): boolean => {
|
|
138
|
+
const plain = (t: string) => t.normalize('NFC').replace(/[\s*_~`️]+/gu, '');
|
|
139
|
+
return plain(a) === plain(b);
|
|
140
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** What was marked a moment ago, for as long as a window lasts: each mark forgets every one older than that, so none is kept on. */
|
|
2
|
+
export class Recent {
|
|
3
|
+
private readonly at = new Map<string, number>();
|
|
4
|
+
|
|
5
|
+
/** `window` is in the unit the clock given to each call counts in. */
|
|
6
|
+
constructor(private readonly window: number) {}
|
|
7
|
+
|
|
8
|
+
mark(input: { readonly item: string; readonly now: number }): void {
|
|
9
|
+
for (const [item, at] of this.at) if (at <= input.now - this.window) this.at.delete(item);
|
|
10
|
+
this.at.set(input.item, input.now);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
isRecent(input: { readonly item: string; readonly now: number }): boolean {
|
|
14
|
+
const at = this.at.get(input.item);
|
|
15
|
+
return at !== undefined && at > input.now - this.window;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
forget(item: string): void {
|
|
19
|
+
this.at.delete(item);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** How many are kept now. */
|
|
23
|
+
get size(): number {
|
|
24
|
+
return this.at.size;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** WHAT AN ANSWERING THREAD HANDS BACK with `skill-sync reply`: its words, made into a post the protocol lets it make, and kept to be posted once. */
|
|
2
|
+
import { Effect, Option } from 'effect';
|
|
3
|
+
import { asAgents, replyRefusal } from './protocol';
|
|
4
|
+
import type { ICommentSource } from './source/interface';
|
|
5
|
+
import type { ICommentStore, WatchStoreError } from './store/interface';
|
|
6
|
+
import { acknowledgementText } from './workflow/deliver';
|
|
7
|
+
|
|
8
|
+
export type Handed = { readonly refused: string } | { readonly id: string };
|
|
9
|
+
|
|
10
|
+
/** Keeps an agent's words for an event as a post to be made, or says why they are not one. */
|
|
11
|
+
export const handOver = (deps: { readonly store: ICommentStore; readonly sources: ReadonlyArray<ICommentSource>; readonly tag: string; readonly now: () => number }) =>
|
|
12
|
+
(input: { readonly key: string; readonly text: string }): Effect.Effect<Handed, WatchStoreError> => Effect.gen(function* () {
|
|
13
|
+
const { store, tag, now } = deps;
|
|
14
|
+
const found = yield* store.event(input.key);
|
|
15
|
+
if (Option.isNone(found)) return { refused: `${input.key} is no comment this machine was told of` };
|
|
16
|
+
const event = found.value;
|
|
17
|
+
const approved = event.kind === 'plan approved';
|
|
18
|
+
// The "Done in" of a plan they approved is still its own; any other reply waits for what they say after a 👎.
|
|
19
|
+
if (event.kind === 'plan rejected' || (!approved && store.standsRejected(event.key))) return { refused: 'the plan was rejected: change nothing and post nothing until their next comment there' };
|
|
20
|
+
const why = replyRefusal(input.text);
|
|
21
|
+
if (why !== null) return { refused: why };
|
|
22
|
+
const text = approved ? acknowledgementText({ event, text: input.text, tag }) : asAgents({ text: input.text, opening: event.opening, tag });
|
|
23
|
+
const source = deps.sources.find((s) => s.id === event.source);
|
|
24
|
+
if (source === undefined) return { refused: `this machine no longer watches ${event.url}` };
|
|
25
|
+
const bytes = Buffer.byteLength(text);
|
|
26
|
+
if (Option.isSome(source.maxPostBytes) && bytes > source.maxPostBytes.value) return { refused: `it is ${bytes} bytes as posted, and no more than ${source.maxPostBytes.value} can be` };
|
|
27
|
+
return { id: yield* store.addPost({ key: event.key, purpose: approved ? 'edit' : 'reply', text, at: now() }) };
|
|
28
|
+
});
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE COMMENT WATCH'S RUNNER: hosts its workflows' engine inside the skill-sync daemon, on a machine whose settings name
|
|
3
|
+
* something to watch, and starts their runs. Find runs each turn of its schedule and as the daemon starts. Deliver and Post
|
|
4
|
+
* run while something waits in their queues: one run of each at a time, the next as the last ends.
|
|
5
|
+
*/
|
|
6
|
+
import { SqliteClient } from '@effect/sql-sqlite-bun';
|
|
7
|
+
import {
|
|
8
|
+
describeFailure, newRunId, oneTurnAtATime, queueOrder, SET_WORKFLOW_RUNS_SQL, START_LIMIT_SECONDS, TURN_LIMIT_MS, WORKFLOW_BUDGET_SQL,
|
|
9
|
+
} from '@jive-sh/workflow-platform';
|
|
10
|
+
import { Database } from 'bun:sqlite';
|
|
11
|
+
import { Crypto, Duration, Effect, Layer, ManagedRuntime, Option, pipe } from 'effect';
|
|
12
|
+
import { ClusterWorkflowEngine, RunnerAddress, SingleRunner } from 'effect/cluster';
|
|
13
|
+
import { FetchHttpClient, HttpClient } from 'effect/http';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { liveWriters } from '../writer/live';
|
|
16
|
+
import { locateSkillCopy } from '../skill-refinement/paths';
|
|
17
|
+
import { heldIn } from './held';
|
|
18
|
+
import { Recent } from './recent';
|
|
19
|
+
import { commentWatchPlatform } from './platform';
|
|
20
|
+
import { t3File, watchDatabase, watchEngineDatabase, watchFolder, watchHeld } from './paths';
|
|
21
|
+
import { handOver } from './replies';
|
|
22
|
+
import { readSettings } from './settings';
|
|
23
|
+
import type { ICommentSource } from './source/interface';
|
|
24
|
+
import { CommandSignIn } from './source/sign-in';
|
|
25
|
+
import { sourcesOf } from './source/sources';
|
|
26
|
+
import { SqliteCommentStore } from './store/impl';
|
|
27
|
+
import { defineCommentWatch } from './workflow/comment-watch';
|
|
28
|
+
import { DELIVER_WORKFLOW } from './workflow/deliver';
|
|
29
|
+
import { FIND_WORKFLOW } from './workflow/find';
|
|
30
|
+
import { POST_WORKFLOW } from './workflow/post';
|
|
31
|
+
import { readT3Settings, T3, type ITold } from './t3';
|
|
32
|
+
import { watchedPlace, type Watching } from './watching';
|
|
33
|
+
|
|
34
|
+
/** How often every source is read. */
|
|
35
|
+
export const FIND_EVERY_SECONDS = 30;
|
|
36
|
+
/** Less than a turn, so each turn tries again. */
|
|
37
|
+
const REST_SECONDS = FIND_EVERY_SECONDS - 5;
|
|
38
|
+
/** How long `skill-sync reply` waits for its post to be made before it says it is still waiting. */
|
|
39
|
+
const REPLY_WAIT_MS = 25_000;
|
|
40
|
+
|
|
41
|
+
/** Bun's WebCrypto, as the cluster's message storage needs it. */
|
|
42
|
+
const CryptoLive = Layer.succeed(Crypto.Crypto, Crypto.make({
|
|
43
|
+
randomBytes: (size) => globalThis.crypto.getRandomValues(new Uint8Array(size)),
|
|
44
|
+
digest: (algorithm, data) => Effect.promise(async () => new Uint8Array(await globalThis.crypto.subtle.digest(algorithm, new Uint8Array(data)))),
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
/** One run of each at a time: a read overlapping the last would only read the same stretch twice. Set once, never changed here. */
|
|
48
|
+
const ensureLimits = (folder: string) => {
|
|
49
|
+
const db = new Database(watchDatabase(folder), { create: true });
|
|
50
|
+
try {
|
|
51
|
+
db.run('CREATE TABLE IF NOT EXISTS workflow_budgets (workflow TEXT PRIMARY KEY, budget INTEGER NOT NULL, runs INTEGER, changed_at REAL NOT NULL)');
|
|
52
|
+
for (const workflow of [FIND_WORKFLOW, DELIVER_WORKFLOW, POST_WORKFLOW]) if (db.query(WORKFLOW_BUDGET_SQL).get(workflow) === null) db.query(SET_WORKFLOW_RUNS_SQL).run(workflow, 1, Date.now() / 1000);
|
|
53
|
+
} finally {
|
|
54
|
+
db.close();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** The watch on one folder and its sources; a test gives sources over fakes. */
|
|
59
|
+
export const makeCommentWatch = (input: {
|
|
60
|
+
readonly folder: string;
|
|
61
|
+
readonly sources: ReadonlyArray<ICommentSource>;
|
|
62
|
+
readonly told: ITold;
|
|
63
|
+
readonly tag: string;
|
|
64
|
+
/** Unix seconds; a test gives its own clock. */
|
|
65
|
+
readonly now?: () => number;
|
|
66
|
+
}) => {
|
|
67
|
+
const { folder, sources, told, tag } = input;
|
|
68
|
+
const platform = commentWatchPlatform(folder);
|
|
69
|
+
ensureLimits(folder);
|
|
70
|
+
const store = new SqliteCommentStore(new Database(watchDatabase(folder)), heldIn(watchHeld(folder)));
|
|
71
|
+
const now = input.now ?? (() => Date.now() / 1000);
|
|
72
|
+
const said = (what: string) => (e: unknown) => console.error(`[comment watch] ${what}: ${describeFailure(e)}`);
|
|
73
|
+
/** What could not be done a moment ago, and when: it is tried again once a turn, not as soon as its run ends. */
|
|
74
|
+
const resting = new Recent(REST_SECONDS);
|
|
75
|
+
const watch = defineCommentWatch({ platform, sources, store, told, tag, now, rests: (item) => { resting.mark({ item, now: now() }); }, onQueued: () => { void deliver().catch(said('what was found could not be delivered')); } });
|
|
76
|
+
const runtime = ManagedRuntime.make(pipe(
|
|
77
|
+
// The platform's own workflows (Retention) run in the same engine.
|
|
78
|
+
Layer.mergeAll(watch.layer, platform.system.layer as Layer.Layer<never>),
|
|
79
|
+
Layer.provideMerge(ClusterWorkflowEngine.layer),
|
|
80
|
+
Layer.provideMerge(SingleRunner.layer({
|
|
81
|
+
runnerStorage: 'sql',
|
|
82
|
+
shardingConfig: { runnerAddress: Option.some(RunnerAddress.make('comment-watch', process.pid)), entityTerminationTimeout: Duration.minutes(10) },
|
|
83
|
+
})),
|
|
84
|
+
Layer.provideMerge(SqliteClient.layer({ filename: watchEngineDatabase(folder), busyTimeout: Duration.seconds(1) })),
|
|
85
|
+
Layer.provideMerge(CryptoLive),
|
|
86
|
+
));
|
|
87
|
+
|
|
88
|
+
const start = (workflow: string, payload: object) => {
|
|
89
|
+
const declaration = platform.steps.declaredWorkflows().find((d) => d.name === workflow)!;
|
|
90
|
+
return runtime.runPromise(Effect.timeout(declaration.startOn(payload, { wait: false }), `${START_LIMIT_SECONDS} seconds`));
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** Starts one Find. A turn is the time it starts: numbered from 1 each time the daemon starts, a turn already run would start nothing. */
|
|
94
|
+
const find = () => start(FIND_WORKFLOW, { turn: Date.now() });
|
|
95
|
+
|
|
96
|
+
/** Starts a run of a workflow where something waits in its queue and no run of it is going. */
|
|
97
|
+
const keepGoing = (workflow: string, label: string, work: () => Effect.Effect<ReadonlyArray<string>, unknown>) => oneTurnAtATime(async (stillCurrent) => {
|
|
98
|
+
const items = (await Effect.runPromise(work())).filter((item) => !resting.isRecent({ item, now: now() }));
|
|
99
|
+
await Effect.runPromise(platform.tasks.recordListing({ workflow, work: { lines: [{ label, items }], waits: [] }, everyItem: false }));
|
|
100
|
+
if (queueOrder(await Effect.runPromise(platform.tasks.taskSummaries({ workflow }))).length === 0) return;
|
|
101
|
+
if (await Effect.runPromise(platform.storage.engine.runningRuns(workflow)) > 0 || !stillCurrent()) return;
|
|
102
|
+
await start(workflow, { run: newRunId() });
|
|
103
|
+
}, TURN_LIMIT_MS);
|
|
104
|
+
const deliver = keepGoing(DELIVER_WORKFLOW, 'targets to tell', () => Effect.gen(() => store.toDeliver()));
|
|
105
|
+
const post = keepGoing(POST_WORKFLOW, 'posts to make', () => Effect.gen(() => store.toPost(now())));
|
|
106
|
+
|
|
107
|
+
/** Takes an agent's words for a comment, and posts them now. Gives the link, or why not; run again, it posts nothing more. */
|
|
108
|
+
const reply = async (words: { readonly key: string; readonly text: string }): Promise<Replied> => {
|
|
109
|
+
const handed = await Effect.runPromise(handOver({ store, sources, tag, now })(words));
|
|
110
|
+
if ('refused' in handed) return handed;
|
|
111
|
+
const until = Date.now() + REPLY_WAIT_MS;
|
|
112
|
+
for (;;) {
|
|
113
|
+
await post();
|
|
114
|
+
const made = await Effect.runPromise(Effect.gen(() => store.post({ id: handed.id, now: now() })));
|
|
115
|
+
if (made.refused !== null) return { refused: made.refused };
|
|
116
|
+
if (Option.isSome(made.posted)) return { posted: made.posted.value.url };
|
|
117
|
+
if (Date.now() > until) return { waiting: 'it is kept, and is posted as soon as it can be' };
|
|
118
|
+
await Bun.sleep(250);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return { platform, store, runtime, find, deliver, post, reply };
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** What became of an agent's words. */
|
|
126
|
+
export type Replied = { readonly posted: string } | { readonly waiting: string } | { readonly refused: string };
|
|
127
|
+
|
|
128
|
+
/** The running watch: what it watches now, as its own reads found it (`skill-sync watching`). */
|
|
129
|
+
export interface CommentWatch {
|
|
130
|
+
/** Why it watches nothing, in a line; none where it watches something. */
|
|
131
|
+
readonly notWatching: string | null;
|
|
132
|
+
readonly watching: () => Promise<Watching>;
|
|
133
|
+
readonly reply: (words: { readonly key: string; readonly text: string }) => Promise<Replied>;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A watch that watches nothing, and why. */
|
|
137
|
+
export const watchingNothing = (input: { readonly why: string; readonly watching: Watching }): CommentWatch => ({
|
|
138
|
+
notWatching: input.why, watching: async () => input.watching, reply: async () => ({ refused: `this machine watches nothing: ${input.why}` }),
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The watch as the daemon starts it. What is asked of it meanwhile waits for it: an agent was told a refusal is final, so a
|
|
143
|
+
* reply sent as the daemon starts must not be refused for that. A start that fails watches nothing, and says why.
|
|
144
|
+
*/
|
|
145
|
+
export const startedCommentWatch = (starting: Promise<CommentWatch>): Pick<CommentWatch, 'watching' | 'reply'> & { readonly started: Promise<CommentWatch> } => {
|
|
146
|
+
const started = starting.catch((cause: unknown) => {
|
|
147
|
+
const reason = `it could not start: ${describeFailure(cause)}`;
|
|
148
|
+
return watchingNothing({ why: reason, watching: { state: 'not watching', reason } });
|
|
149
|
+
});
|
|
150
|
+
return { started, watching: async () => (await started).watching(), reply: async (words) => (await started).reply(words) };
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/** Starts the watch on what the machine's settings file names. Nothing is made on a machine whose settings name nothing. */
|
|
154
|
+
export const startCommentWatch = async (input: { readonly home: string; readonly settingsFile: string; readonly claudeCommand: string | undefined }): Promise<CommentWatch> => {
|
|
155
|
+
const { home } = input;
|
|
156
|
+
const cannot = (reason: string) => watchingNothing({ why: reason, watching: { state: 'not watching', reason } });
|
|
157
|
+
const settings = await Effect.runPromise(Effect.result(readSettings(input.settingsFile)));
|
|
158
|
+
if (settings._tag === 'Failure') return cannot(settings.failure);
|
|
159
|
+
if (settings.success.sources.length === 0) return watchingNothing({ why: 'this machine\'s settings name nothing to watch', watching: { state: 'nothing set up' } });
|
|
160
|
+
const t3 = await Effect.runPromise(Effect.result(readT3Settings(t3File(home))));
|
|
161
|
+
// 👀 on a comment nobody can be told of would say an agent has it.
|
|
162
|
+
if (t3._tag === 'Failure') return cannot(t3.failure);
|
|
163
|
+
const client = await Effect.runPromise(pipe(HttpClient.HttpClient, Effect.provide(FetchHttpClient.layer)));
|
|
164
|
+
const { tag } = settings.success;
|
|
165
|
+
const sources = sourcesOf({ settings: settings.success, client, signIn: (by) => new CommandSignIn({ ...by, home }) });
|
|
166
|
+
const folder = watchFolder(home);
|
|
167
|
+
const copy = locateSkillCopy({ home, claudeConfigDir: process.env["SKILL_REFINEMENT_CLAUDE_CONFIG_DIR"] });
|
|
168
|
+
const agents = liveWriters({ home, scratch: path.join(folder, 'agents'), claudeCommand: input.claudeCommand, claudeConfigDir: copy?.accountFolder ?? path.join(home, '.claude'), claudeModel: 'claude-opus-5-5' });
|
|
169
|
+
const told = new T3({ settings: t3.success, client, availableAgents: async () => (agents === undefined ? [] : agents.available()) });
|
|
170
|
+
const watch = makeCommentWatch({ folder, sources, told, tag });
|
|
171
|
+
// The engine starts with the runtime; a run cut off by a restart goes on from its records.
|
|
172
|
+
await watch.runtime.runPromise(Effect.void);
|
|
173
|
+
const said = (what: string) => (e: unknown) => console.error(`[comment watch] ${what}: ${describeFailure(e)}`);
|
|
174
|
+
const waiting = () => {
|
|
175
|
+
void watch.deliver().catch(said('what was found could not be delivered'));
|
|
176
|
+
void watch.post().catch(said('what was handed over could not be posted'));
|
|
177
|
+
};
|
|
178
|
+
const turn = () => {
|
|
179
|
+
void watch.find().catch(said('a read did not start'));
|
|
180
|
+
waiting();
|
|
181
|
+
};
|
|
182
|
+
watch.platform.steps.onRunEnded(waiting);
|
|
183
|
+
setInterval(turn, FIND_EVERY_SECONDS * 1000);
|
|
184
|
+
turn();
|
|
185
|
+
for (const w of watch.platform.system.workflows) {
|
|
186
|
+
const run = () => { void watch.runtime.runPromise(w.start(Date.now())).catch(said(`${w.name} did not start`)); };
|
|
187
|
+
setTimeout(() => { run(); setInterval(run, w.everySeconds * 1000); }, w.firstAfterSeconds * 1000);
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
notWatching: null,
|
|
191
|
+
reply: watch.reply,
|
|
192
|
+
watching: () => Effect.runPromise(Effect.gen(function* () {
|
|
193
|
+
const places: Array<ReturnType<typeof watchedPlace>> = [];
|
|
194
|
+
for (const source of sources) places.push(...(yield* watch.store.watched(source)).map(watchedPlace));
|
|
195
|
+
return { state: 'watching' as const, places };
|
|
196
|
+
})),
|
|
197
|
+
};
|
|
198
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WHAT THIS MACHINE WATCHES, from `commentWatch` in `~/.jive/skill-sync.json`. The code knows kinds of sources and nothing
|
|
3
|
+
* about any one of them: every Doc, repository, person and T3 project is named here, on the machine that watches them.
|
|
4
|
+
*/
|
|
5
|
+
import { Effect, Schema, SchemaIssue } from 'effect';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import { DEFAULT_TAG } from './protocol';
|
|
8
|
+
|
|
9
|
+
const Name = Schema.NonEmptyString;
|
|
10
|
+
/** Prints an access token on its standard output: a program and its arguments, run without a shell. */
|
|
11
|
+
const TokenCommand = Schema.NonEmptyArray(Name);
|
|
12
|
+
|
|
13
|
+
/** A Google Doc, and which of its tabs, by id: a tab's id is immutable and its title is not. */
|
|
14
|
+
export const GoogleDocSettings = Schema.Struct({
|
|
15
|
+
kind: Schema.Literal('google-doc'),
|
|
16
|
+
document: Name,
|
|
17
|
+
/** Each watched tab, and the T3 project whose command thread answers what is left on it. */
|
|
18
|
+
tabs: Schema.NonEmptyArray(Schema.Struct({ tab: Name, project: Name })),
|
|
19
|
+
tokenCommand: TokenCommand,
|
|
20
|
+
/** The Google Cloud project the Docs API counts the reads against, for a sign-in that names none. */
|
|
21
|
+
quotaProject: Schema.optionalKey(Name),
|
|
22
|
+
});
|
|
23
|
+
export type GoogleDocSettings = typeof GoogleDocSettings.Type;
|
|
24
|
+
|
|
25
|
+
/** The open pull requests these authors wrote in these repositories (`owner/name`), read and answered as `gh` is signed in. */
|
|
26
|
+
export const GitHubPullRequestsSettings = Schema.Struct({
|
|
27
|
+
kind: Schema.Literal('github-pull-requests'),
|
|
28
|
+
repositories: Schema.NonEmptyArray(Schema.String.check(Schema.isPattern(/^[\w.-]+\/[\w.-]+$/))),
|
|
29
|
+
authors: Schema.NonEmptyArray(Schema.String.check(Schema.isPattern(/^[A-Za-z0-9-]+$/))),
|
|
30
|
+
});
|
|
31
|
+
export type GitHubPullRequestsSettings = typeof GitHubPullRequestsSettings.Type;
|
|
32
|
+
|
|
33
|
+
export type SourceSettings = GoogleDocSettings | GitHubPullRequestsSettings;
|
|
34
|
+
export interface CommentWatchSettings {
|
|
35
|
+
/** The name that tags the agent in a pull request comment, without its @. */
|
|
36
|
+
readonly tag: string;
|
|
37
|
+
readonly sources: ReadonlyArray<SourceSettings>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const Listed = Schema.decodeUnknownEffect(Schema.Struct({
|
|
41
|
+
tag: Schema.optionalKey(Schema.String.check(Schema.isPattern(/^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/))),
|
|
42
|
+
sources: Schema.Array(Schema.Unknown),
|
|
43
|
+
}), { onExcessProperty: 'error' });
|
|
44
|
+
const sourceOfKind: Readonly<Record<SourceSettings['kind'], (source: unknown) => Effect.Effect<SourceSettings, Schema.SchemaError>>> = {
|
|
45
|
+
'google-doc': Schema.decodeUnknownEffect(GoogleDocSettings, { onExcessProperty: 'error' }),
|
|
46
|
+
'github-pull-requests': Schema.decodeUnknownEffect(GitHubPullRequestsSettings, { onExcessProperty: 'error' }),
|
|
47
|
+
};
|
|
48
|
+
const isKind = Schema.is(Schema.Literals(['google-doc', 'github-pull-requests']));
|
|
49
|
+
const formatIssue = SchemaIssue.makeFormatterDefault();
|
|
50
|
+
|
|
51
|
+
/** A repository and an author watched there, as GitHub compares names: whatever their case. */
|
|
52
|
+
export interface RepositoryAuthor { readonly repository: string; readonly author: string }
|
|
53
|
+
export const repositoryAuthors = (sources: ReadonlyArray<SourceSettings>): ReadonlyArray<RepositoryAuthor> =>
|
|
54
|
+
sources.flatMap((s) => (s.kind === 'github-pull-requests' ? s.repositories.flatMap((repository) => s.authors.map((author) => ({ repository, author }))) : []));
|
|
55
|
+
|
|
56
|
+
/** Every place the settings name, one name each whatever its case. Pure. */
|
|
57
|
+
const places = (sources: ReadonlyArray<SourceSettings>): ReadonlyArray<string> => [
|
|
58
|
+
...sources.flatMap((s) => (s.kind === 'google-doc' ? [s.document, ...s.tabs.map((t) => `${s.document} ${t.tab}`)] : [])),
|
|
59
|
+
...repositoryAuthors(sources).map((p) => `${p.repository} ${p.author}`.toLowerCase()),
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
/** The settings' `commentWatch`, or why it names nothing this machine can watch. A typo'd key fails rather than being ignored. */
|
|
63
|
+
export const decodeSettings = (raw: unknown): Effect.Effect<CommentWatchSettings, string> => Effect.gen(function* () {
|
|
64
|
+
const listed = yield* Effect.mapError(Listed(raw), (e) => formatIssue(e.issue));
|
|
65
|
+
// Each by its own kind, so what is wrong with one is said of it and not of every kind it might have been.
|
|
66
|
+
const sources = yield* Effect.forEach(listed.sources, (source, i): Effect.Effect<SourceSettings, string> => {
|
|
67
|
+
const kind = typeof source === 'object' && source !== null && 'kind' in source ? source.kind : undefined;
|
|
68
|
+
if (!isKind(kind)) return Effect.fail(`source ${i + 1}'s kind is neither google-doc nor github-pull-requests`);
|
|
69
|
+
return Effect.mapError(sourceOfKind[kind](source), (e) => `source ${i + 1} (${kind}): ${formatIssue(e.issue)}`);
|
|
70
|
+
});
|
|
71
|
+
const named = places(sources);
|
|
72
|
+
const twice = named.find((p, i) => named.indexOf(p) !== i);
|
|
73
|
+
if (twice !== undefined) return yield* Effect.fail(`${twice} is named twice`);
|
|
74
|
+
return { tag: listed.tag ?? DEFAULT_TAG, sources };
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
/** What a settings file says to watch: no source where it names none. */
|
|
78
|
+
export const readSettings = (file: string): Effect.Effect<CommentWatchSettings, string> => Effect.gen(function* () {
|
|
79
|
+
const raw = yield* Effect.try({ try: () => (fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) as { readonly commentWatch?: unknown } : {}), catch: (e) => `${file} could not be read: ${String(e)}` });
|
|
80
|
+
if (raw.commentWatch === undefined) return { tag: DEFAULT_TAG, sources: [] };
|
|
81
|
+
return yield* Effect.mapError(decodeSettings(raw.commentWatch), (why) => `commentWatch in ${file}: ${why}`);
|
|
82
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WHAT THE OPEN PULL REQUESTS SAY NOW: each comment for the agent, each verdict on a plan, which comments show this account's
|
|
3
|
+
* 👀, and which plans wait. Worked out from what GitHub shows every time, with nothing kept of it, so it is never out
|
|
4
|
+
* of step with what the person sees. Pure.
|
|
5
|
+
*/
|
|
6
|
+
import { keyOf } from '../../key';
|
|
7
|
+
import { acknowledgementLine, isOrder, isPlan } from '../../protocol';
|
|
8
|
+
import type { SourceEvent } from '../interface';
|
|
9
|
+
import type { IssueComment, PullRequest, Review, ReviewComment } from './queries';
|
|
10
|
+
|
|
11
|
+
/** Where an event is, as its replies and reactions are addressed. */
|
|
12
|
+
export interface PullRef {
|
|
13
|
+
readonly repository: string;
|
|
14
|
+
readonly pull: number;
|
|
15
|
+
/** The review thread's first comment, by REST id; none for the pull request's own conversation. */
|
|
16
|
+
readonly thread: string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Each verdict, and the word it is known by beside its plan's id: a plan approved twice is approved once. */
|
|
20
|
+
const VERDICTS: Readonly<Record<string, { readonly kind: 'plan approved' | 'plan rejected'; readonly word: string }>> = {
|
|
21
|
+
THUMBS_UP: { kind: 'plan approved', word: 'approved' }, THUMBS_DOWN: { kind: 'plan rejected', word: 'rejected' },
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export interface Conversations {
|
|
25
|
+
readonly events: ReadonlyArray<SourceEvent>;
|
|
26
|
+
/** Each comment for the agent, and whether it shows this account's 👀. */
|
|
27
|
+
readonly eyes: ReadonlyArray<{ readonly subject: string; readonly key: string; readonly shown: boolean }>;
|
|
28
|
+
/** Plans with no 👍 or 👎 yet, whose reactions every poll reads. */
|
|
29
|
+
readonly openPlans: ReadonlyArray<string>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const at = (time: string) => Date.parse(time);
|
|
33
|
+
|
|
34
|
+
/** What a pull request says. `since` is when it was first watched: what was written earlier was there before. */
|
|
35
|
+
export const readConversations = (input: {
|
|
36
|
+
readonly pulls: ReadonlyArray<{ readonly pull: PullRequest; readonly since: string }>;
|
|
37
|
+
readonly me: string;
|
|
38
|
+
readonly tag: string;
|
|
39
|
+
}): Conversations => {
|
|
40
|
+
const events: Array<{ readonly event: SourceEvent; readonly at: number }> = [];
|
|
41
|
+
const eyes: Array<Conversations['eyes'][number]> = [];
|
|
42
|
+
const openPlans: string[] = [];
|
|
43
|
+
const isMe = (author: { readonly login: string } | null) => author?.login.toLowerCase() === input.me.toLowerCase();
|
|
44
|
+
|
|
45
|
+
for (const { pull, since } of input.pulls) {
|
|
46
|
+
const repository = pull.repository.nameWithOwner;
|
|
47
|
+
const reviews = pull.reviews.nodes.filter((r): r is Review & { readonly submittedAt: string } => r.submittedAt !== null);
|
|
48
|
+
const lines = reviews.flatMap((r) => r.comments.nodes);
|
|
49
|
+
const rootOf = (c: ReviewComment) => String(c.replyTo?.fullDatabaseId ?? c.fullDatabaseId);
|
|
50
|
+
const common = { target: { kind: 'pull request' as const, url: pull.url }, title: pull.title, conversation: null };
|
|
51
|
+
const refOf = (thread: string | null) => JSON.stringify({ repository, pull: pull.number, thread } satisfies PullRef);
|
|
52
|
+
|
|
53
|
+
const addOrder = (c: {
|
|
54
|
+
readonly kind: 'comment' | 'review' | 'line comment'; readonly id: string; readonly post: string; readonly url: string; readonly body: string; readonly time: string;
|
|
55
|
+
readonly author: IssueComment['author']; readonly groups: IssueComment['reactionGroups']; readonly thread: string | null; readonly where: string | null;
|
|
56
|
+
}) => {
|
|
57
|
+
if (!isMe(c.author) || at(c.time) < at(since) || !isOrder({ text: c.body, tag: input.tag })) return;
|
|
58
|
+
events.push({ at: at(c.time), event: {
|
|
59
|
+
...common, key: keyOf({ of: 'github', id: c.id }), kind: c.kind, post: c.post, ref: refOf(c.thread), url: c.url, text: c.body, where: c.where,
|
|
60
|
+
// GitHub threads line comments alone: a reply to any other is a comment of its own that links what it answers.
|
|
61
|
+
opening: c.thread === null ? c.url : null, acknowledgementLine: null,
|
|
62
|
+
} });
|
|
63
|
+
eyes.push({ subject: c.id, key: keyOf({ of: 'github', id: c.id }), shown: (c.groups ?? []).some((g) => g.content === 'EYES' && g.viewerHasReacted) });
|
|
64
|
+
};
|
|
65
|
+
const addVerdicts = (c: IssueComment | ReviewComment, thread: string | null, where: string | null) => {
|
|
66
|
+
if (!isMe(c.author) || !isPlan(c.body)) return;
|
|
67
|
+
let decided = false;
|
|
68
|
+
for (const reaction of c.reactions.nodes) {
|
|
69
|
+
const verdict = VERDICTS[reaction.content];
|
|
70
|
+
if (verdict === undefined || !isMe(reaction.user)) continue;
|
|
71
|
+
decided = true;
|
|
72
|
+
if (at(reaction.createdAt) < at(since)) continue;
|
|
73
|
+
// Taken back and given again, it is the same verdict on the same plan: it is known by those, not by the reaction.
|
|
74
|
+
const id = `${c.id}/${verdict.word}`;
|
|
75
|
+
events.push({ at: at(reaction.createdAt), event: {
|
|
76
|
+
...common, key: keyOf({ of: 'github', id }), kind: verdict.kind, post: `verdict:${id}`, ref: refOf(thread), url: c.url, text: c.body, where,
|
|
77
|
+
opening: thread === null ? c.url : null, acknowledgementLine: verdict.kind === 'plan approved' ? acknowledgementLine(id) : null,
|
|
78
|
+
} });
|
|
79
|
+
}
|
|
80
|
+
if (!decided && at(c.createdAt) >= at(since)) openPlans.push(c.id);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
for (const c of pull.comments.nodes) {
|
|
84
|
+
addOrder({ kind: 'comment', id: c.id, post: `conversation:${c.fullDatabaseId}`, url: c.url, body: c.body, time: c.createdAt, author: c.author, groups: c.reactionGroups, thread: null, where: null });
|
|
85
|
+
addVerdicts(c, null, null);
|
|
86
|
+
}
|
|
87
|
+
for (const r of reviews) {
|
|
88
|
+
addOrder({ kind: 'review', id: r.id, post: `review:${r.id}`, url: r.url, body: r.body, time: r.submittedAt, author: r.author, groups: r.reactionGroups, thread: null, where: null });
|
|
89
|
+
}
|
|
90
|
+
for (const c of lines) {
|
|
91
|
+
const line = c.line ?? c.originalLine;
|
|
92
|
+
const where = `${c.path}${line === null ? '' : ` line ${line}`}`;
|
|
93
|
+
addOrder({
|
|
94
|
+
kind: 'line comment', id: c.id, post: `line:${c.fullDatabaseId}`, url: c.url, body: c.body, time: c.createdAt, author: c.author, groups: c.reactionGroups, thread: rootOf(c), where,
|
|
95
|
+
});
|
|
96
|
+
addVerdicts(c, rootOf(c), where);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { events: events.sort((a, b) => a.at - b.at).map((e) => e.event), eyes, openPlans };
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** The same pull request with what is newly known of one of its posts. */
|
|
103
|
+
export const updated = (pull: PullRequest, id: string, known: { readonly reactions?: IssueComment['reactions']; readonly eyes?: boolean }): PullRequest => {
|
|
104
|
+
const groups = (g: IssueComment['reactionGroups']) => (known.eyes === undefined ? g : [...(g ?? []).filter((x) => x.content !== 'EYES'), { content: 'EYES', viewerHasReacted: known.eyes }]);
|
|
105
|
+
const post = <P extends IssueComment | ReviewComment>(c: P): P => (c.id !== id ? c : { ...c, reactions: known.reactions ?? c.reactions, reactionGroups: groups(c.reactionGroups) });
|
|
106
|
+
return {
|
|
107
|
+
...pull,
|
|
108
|
+
comments: { ...pull.comments, nodes: pull.comments.nodes.map(post) },
|
|
109
|
+
reviews: { ...pull.reviews, nodes: pull.reviews.nodes.map((r) => ({ ...r, reactionGroups: r.id === id ? groups(r.reactionGroups) : r.reactionGroups, comments: { ...r.comments, nodes: r.comments.nodes.map(post) } })) },
|
|
110
|
+
};
|
|
111
|
+
};
|