@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,198 @@
|
|
|
1
|
+
/** A Google Doc's tabs as a source of comments: their comments, and the replies to them. */
|
|
2
|
+
import { Effect, Option, Result, Schema } from 'effect';
|
|
3
|
+
import type { HttpClient } from 'effect/http';
|
|
4
|
+
import { keyOf } from '../../key';
|
|
5
|
+
import { EYES_REPLY, isAgents, isEyes, isPlan, isReceipt, isYes } from '../../protocol';
|
|
6
|
+
import { Recent } from '../../recent';
|
|
7
|
+
import type { GoogleDocSettings } from '../../settings';
|
|
8
|
+
import type { NamedPlace } from '../../watching';
|
|
9
|
+
import { HttpApi } from '../http-api';
|
|
10
|
+
import { Refused, ServiceUnavailable, type ICommentSource, type Listed, type Listing, type Shown, type SourceEvent, type SourceFailure, type ThreadPost } from '../interface';
|
|
11
|
+
import type { ISignIn } from '../sign-in';
|
|
12
|
+
import { Doc, postsOf, tabIds, tabPaths, threadTabs, type CommentThread, type Post } from './doc';
|
|
13
|
+
|
|
14
|
+
const ORIGIN = 'https://docs.googleapis.com';
|
|
15
|
+
const SERVICE = 'Google Docs';
|
|
16
|
+
|
|
17
|
+
/** The longest post the Docs API takes: "May not exceed 2048 UTF-8 code units" (`Post.content`). */
|
|
18
|
+
const MAX_POST_BYTES = 2048;
|
|
19
|
+
|
|
20
|
+
/** How long after a post a read may still not show it. */
|
|
21
|
+
const READS_SHOW_A_POST_AFTER_MS = 60_000;
|
|
22
|
+
|
|
23
|
+
const Cursor = Schema.fromJsonString(Schema.Struct({
|
|
24
|
+
/** The tabs already read once: what is on any other was there before. */
|
|
25
|
+
tabs: Schema.Array(Schema.String),
|
|
26
|
+
/** The tab each thread was last on, for a thread whose text, and anchor with it, is deleted. */
|
|
27
|
+
threads: Schema.Record(Schema.String, Schema.String),
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
const Ref = Schema.fromJsonString(Schema.Struct({ commentId: Schema.String }));
|
|
31
|
+
|
|
32
|
+
const Posted = Schema.Struct({
|
|
33
|
+
commentUpdateState: Schema.optionalKey(Schema.String),
|
|
34
|
+
replies: Schema.optionalKey(Schema.Array(Schema.Struct({
|
|
35
|
+
addCommentReply: Schema.optionalKey(Schema.Struct({ post: Schema.Struct({ postId: Schema.String }) })),
|
|
36
|
+
}))),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export class GoogleDocSource implements ICommentSource {
|
|
40
|
+
readonly id: string;
|
|
41
|
+
readonly settings: ReadonlyArray<NamedPlace>;
|
|
42
|
+
readonly maxPostBytes = Option.some(MAX_POST_BYTES);
|
|
43
|
+
private readonly api: HttpApi;
|
|
44
|
+
private readonly tabs: ReadonlyArray<string>;
|
|
45
|
+
/** What the last listing read. */
|
|
46
|
+
private last: { readonly doc: Doc; readonly tabOf: ReadonlyMap<string, string> } | null = null;
|
|
47
|
+
/** The threads a 👀 was posted in, for as long as a read may not show it: what this process did, and nothing more. */
|
|
48
|
+
private readonly justShown = new Recent(READS_SHOW_A_POST_AFTER_MS);
|
|
49
|
+
/** Unix milliseconds. */
|
|
50
|
+
private readonly now: () => number;
|
|
51
|
+
|
|
52
|
+
constructor(private readonly set: GoogleDocSettings, deps: { readonly client: HttpClient.HttpClient; readonly signIn: ISignIn; readonly now?: () => number }) {
|
|
53
|
+
this.now = deps.now ?? (() => Date.now());
|
|
54
|
+
this.id = `google-doc:${set.document}`;
|
|
55
|
+
this.tabs = set.tabs.map((t) => t.tab);
|
|
56
|
+
this.settings = [{ id: set.document, kind: 'google-doc', name: null, link: this.url(undefined), missing: null, entries: this.tabs.map((id) => ({ id, name: null, link: this.url(id), missing: null, pulls: null })) }];
|
|
57
|
+
this.api = new HttpApi({ service: SERVICE, origin: ORIGIN, headers: set.quotaProject === undefined ? {} : { 'x-goog-user-project': set.quotaProject }, ...deps });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
*newSince(cursor: Option.Option<string>): Effect.fn.Return<Listing, SourceFailure> {
|
|
61
|
+
const doc = yield* this.read();
|
|
62
|
+
const was = Option.isNone(cursor) ? { tabs: [], threads: {} } : yield* Effect.orDie(Schema.decodeUnknownEffect(Cursor)(cursor.value));
|
|
63
|
+
const missing = this.tabs.filter((t) => !tabIds(doc).includes(t));
|
|
64
|
+
const paths = tabPaths(doc);
|
|
65
|
+
const title = (tab: string) => [doc.title ?? 'a Google Doc', paths.get(tab) ?? 'a subtab'].join(' › ');
|
|
66
|
+
|
|
67
|
+
const tabOf = threadTabs({ doc, known: was.threads });
|
|
68
|
+
const events: Listed[] = [];
|
|
69
|
+
for (const thread of doc.comments ?? []) {
|
|
70
|
+
const tab = tabOf.get(thread.commentId);
|
|
71
|
+
if (tab === undefined || !this.tabs.includes(tab)) continue;
|
|
72
|
+
const posts = postsOf(thread);
|
|
73
|
+
for (const [i, post] of posts.entries()) {
|
|
74
|
+
// What an agent wrote from this account is never for an agent, nor is what anyone else wrote.
|
|
75
|
+
if (post.author?.me !== true || isAgents(post.content ?? '')) continue;
|
|
76
|
+
const plan = this.planApprovedBy(posts, i);
|
|
77
|
+
const id = `${thread.commentId}/${post.postId}`;
|
|
78
|
+
events.push({ before: !was.tabs.includes(tab), event: {
|
|
79
|
+
key: keyOf({ of: this.id, id }), post: id, kind: plan === undefined ? 'comment' : 'plan approved', title: title(tab), where: thread.plainTextQuote ?? null,
|
|
80
|
+
text: (plan ?? post).content ?? '', ref: JSON.stringify({ commentId: thread.commentId } satisfies (typeof Ref)['Type']), url: this.url(tab),
|
|
81
|
+
target: { kind: 'project', id: this.set.tabs.find((t) => t.tab === tab)!.project }, opening: null, acknowledgementLine: null,
|
|
82
|
+
conversation: posts.slice(0, i + 1).filter((p) => !isReceipt(p.content ?? '')).map((p) => ({ author: isAgents(p.content ?? '') ? 'agent' : p.author?.displayName ?? 'someone anonymous', text: p.content ?? '' })),
|
|
83
|
+
} });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
this.last = { doc, tabOf };
|
|
87
|
+
const onNoTab = (doc.comments ?? []).filter((c) => !tabOf.has(c.commentId)).length;
|
|
88
|
+
return {
|
|
89
|
+
events,
|
|
90
|
+
// A tab the Doc doesn't have was not read: what is on it when it is there is from before.
|
|
91
|
+
cursor: JSON.stringify({ tabs: this.tabs.filter((tab) => !missing.includes(tab) || was.tabs.includes(tab)), threads: Object.fromEntries([...tabOf].filter(([, tab]) => this.tabs.includes(tab))) } satisfies (typeof Cursor)['Type']),
|
|
92
|
+
watched: [{
|
|
93
|
+
...this.settings[0]!, name: doc.title ?? null,
|
|
94
|
+
entries: this.tabs.map((id) => ({ id, name: paths.get(id) ?? null, link: this.url(id), missing: missing.includes(id) ? 'the Doc has no such subtab' : null, pulls: null })),
|
|
95
|
+
}],
|
|
96
|
+
notes: [
|
|
97
|
+
...(onNoTab === 0 ? [] : [`${onNoTab} comment${onNoTab === 1 ? ' is' : 's are'} on no tab`]),
|
|
98
|
+
...(missing.length === 0 ? [] : [`the Doc has no tab ${missing.join(', ')}`]),
|
|
99
|
+
],
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Google's APIs have no reactions, so 👀 is a reply in the comment's thread, deleted once it is answered: an edit of it into
|
|
105
|
+
* the answer would notify nobody.
|
|
106
|
+
*/
|
|
107
|
+
*showReceived({ isQueued, isAnswered }: Parameters<ICommentSource['showReceived']>[0]): Effect.fn.Return<Shown> {
|
|
108
|
+
if (this.last === null) return { changed: 0, failed: [] };
|
|
109
|
+
const { doc, tabOf } = this.last;
|
|
110
|
+
const failed: string[] = [];
|
|
111
|
+
let changed = 0;
|
|
112
|
+
const tried = (what: string, request: Parameters<GoogleDocSource['batch']>[0]) => Effect.map(Effect.result(Effect.gen(() => this.batch(request))), (done) => {
|
|
113
|
+
if (Result.isFailure(done)) failed.push(`${what}: ${done.failure.reason}`); else changed += 1;
|
|
114
|
+
return Result.isFailure(done) ? done.failure._tag : null;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
for (const thread of doc.comments ?? []) {
|
|
118
|
+
if (!this.tabs.includes(tabOf.get(thread.commentId) ?? '')) continue;
|
|
119
|
+
const posts = postsOf(thread);
|
|
120
|
+
const isMine = (p: Post) => p.author?.me === true;
|
|
121
|
+
// A comment waits from when it is queued until a reply to it is recorded: one to a later comment answers no earlier one.
|
|
122
|
+
const waiting = posts.flatMap((p, i) => {
|
|
123
|
+
const key = keyOf({ of: this.id, id: `${thread.commentId}/${p.postId}` });
|
|
124
|
+
return isMine(p) && !isAgents(p.content ?? '') && isQueued(key) && !isAnswered(key) && !this.isApproval(posts, i) ? [i] : [];
|
|
125
|
+
});
|
|
126
|
+
const eyes = posts.flatMap((p, i) => (isMine(p) && isEyes(p.content ?? '') ? [i] : []));
|
|
127
|
+
// The first after each comment that waits is the one that shows for it; any other is one too many, or is done.
|
|
128
|
+
const needed = new Set(waiting.map((w) => eyes.find((e) => e > w)));
|
|
129
|
+
for (const e of eyes.filter((i) => !needed.has(i) && posts[i] !== thread.headPost)) yield* tried('👀 could not be deleted', { requests: [{ deleteCommentReply: { commentId: thread.commentId, postId: posts[e]!.postId } }] });
|
|
130
|
+
if (!needed.has(undefined) || this.justShown.isRecent({ item: thread.commentId, now: this.now() })) continue;
|
|
131
|
+
this.justShown.mark({ item: thread.commentId, now: this.now() });
|
|
132
|
+
const failure = yield* tried('👀 could not be posted', { requests: [{ addCommentReply: { commentId: thread.commentId, post: { content: EYES_REPLY } } }] });
|
|
133
|
+
// It was not posted: nothing to wait for.
|
|
134
|
+
if (failure === 'Refused' || failure === 'SignInExpired') this.justShown.forget(thread.commentId);
|
|
135
|
+
}
|
|
136
|
+
return { changed, failed };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Whether a post approves the plan above it. */
|
|
140
|
+
private isApproval(posts: ReadonlyArray<Post>, i: number): boolean {
|
|
141
|
+
return this.planApprovedBy(posts, i) !== undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The plan a "yes" of theirs approves: the last an agent said before it, past any receipt, and not approved before. */
|
|
145
|
+
private planApprovedBy(posts: ReadonlyArray<Post>, i: number): Post | undefined {
|
|
146
|
+
if (!isYes(posts[i]!.content ?? '')) return undefined;
|
|
147
|
+
const said = posts.slice(0, i).findLast((p) => p.author?.me === true && isAgents(p.content ?? '') && !isReceipt(p.content ?? ''));
|
|
148
|
+
return said !== undefined && isPlan(said.content ?? '') && !posts.slice(posts.indexOf(said), i).some((p) => p.author?.me === true && isYes(p.content ?? '')) ? said : undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
*thread(event: Pick<SourceEvent, 'key' | 'ref'>): Effect.fn.Return<ReadonlyArray<ThreadPost>, SourceFailure> {
|
|
152
|
+
const doc = yield* this.read();
|
|
153
|
+
const { commentId } = yield* this.refOf(event);
|
|
154
|
+
const thread = (doc.comments ?? []).find((t) => t.commentId === commentId);
|
|
155
|
+
if (thread === undefined) return yield* Effect.fail(new Refused({ service: SERVICE, reason: 'the comment is no longer in the Doc', status: null, plain: 'the comment is no longer in the Doc' }));
|
|
156
|
+
return postsOf(thread).map((p) => this.threadPost({ thread, post: p }));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
*reply(event: Pick<SourceEvent, 'key' | 'ref'>, text: string): Effect.fn.Return<ThreadPost, SourceFailure> {
|
|
160
|
+
const { commentId } = yield* this.refOf(event);
|
|
161
|
+
const replies = yield* this.batch({ requests: [{ addCommentReply: { commentId, post: { content: text } } }] });
|
|
162
|
+
const post = replies?.[0]?.addCommentReply?.post.postId;
|
|
163
|
+
if (post === undefined) return yield* Effect.fail(new ServiceUnavailable({ service: SERVICE, reason: 'the post was saved, and the answer does not name it', plain: 'Google saved the comment and did not name it' }));
|
|
164
|
+
return { post: `${commentId}/${post}`, author: 'this account', text, url: this.url(undefined), mine: true };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
*edit(_: Pick<SourceEvent, 'key' | 'ref'>, post: string, text: string): Effect.fn.Return<void, SourceFailure> {
|
|
168
|
+
const [commentId, postId] = post.split('/');
|
|
169
|
+
yield* this.batch({ requests: [{ updateCommentPost: { commentId, postId, content: text } }] });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private *read(): Effect.fn.Return<Doc, SourceFailure> {
|
|
173
|
+
const query = new URLSearchParams({ includeTabsContent: 'true', commentsViewMode: 'COMMENTS_VIEW_MODE_INCLUDED', suggestionsViewMode: 'SUGGESTIONS_INLINE' });
|
|
174
|
+
const { body } = yield* this.api.get(`${ORIGIN}/v1/documents/${encodeURIComponent(this.set.document)}?${query.toString()}`, Doc);
|
|
175
|
+
// An answer without comments would read as every comment being gone.
|
|
176
|
+
if (body.commentsViewMode !== 'COMMENTS_VIEW_MODE_INCLUDED') return yield* Effect.fail(new ServiceUnavailable({ service: SERVICE, reason: 'the Doc came back without its comments', plain: 'the Doc came back without its comments' }));
|
|
177
|
+
return body;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Makes the request, and its replies where Google says it saved them. Anything else leaves it unknown whether it did. */
|
|
181
|
+
private *batch(request: { readonly requests: ReadonlyArray<object> }): Effect.fn.Return<(typeof Posted.Type)['replies'], SourceFailure> {
|
|
182
|
+
const { body } = yield* this.api.send({ method: 'POST', url: `${ORIGIN}/v1/documents/${encodeURIComponent(this.set.document)}:batchUpdate`, body: request }, Posted);
|
|
183
|
+
if (body.commentUpdateState !== 'ALL_SAVED') return yield* Effect.fail(new ServiceUnavailable({ service: SERVICE, reason: `the comment was not saved: ${body.commentUpdateState ?? 'no state given'}`, plain: 'Google did not save the comment' }));
|
|
184
|
+
return body.replies;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private *refOf(event: Pick<SourceEvent, 'ref'>): Effect.fn.Return<typeof Ref.Type> {
|
|
188
|
+
return yield* Effect.orDie(Schema.decodeUnknownEffect(Ref)(event.ref));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private threadPost(input: { readonly thread: CommentThread; readonly post: Post }): ThreadPost {
|
|
192
|
+
return { post: `${input.thread.commentId}/${input.post.postId}`, author: input.post.author?.displayName ?? 'someone anonymous', text: input.post.content ?? '', url: this.url(undefined), mine: input.post.author?.me === true };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private url(tab: string | undefined): string {
|
|
196
|
+
return `https://docs.google.com/document/d/${this.set.document}/edit${tab === undefined ? '' : `?tab=${tab}`}`;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/** A source's calls to its service: signed in, bounded, and every answer that is not one mapped to why (`SourceFailure`). */
|
|
2
|
+
import { Effect, pipe, Schema } from 'effect';
|
|
3
|
+
import { type HttpClient, HttpClientRequest, HttpClientResponse } from 'effect/http';
|
|
4
|
+
import { Refused, ServiceUnavailable, SignInExpired, type SourceFailure } from './interface';
|
|
5
|
+
import type { ISignIn } from './sign-in';
|
|
6
|
+
|
|
7
|
+
const CALL_LIMIT = '30 seconds';
|
|
8
|
+
/** Answers kept to ask the service only whether they changed: the newest this many. */
|
|
9
|
+
const ANSWERS_KEPT = 500;
|
|
10
|
+
|
|
11
|
+
/** Google's words for a limit reached, which it answers with 403; GitHub's are in its headers, or say "rate limit". */
|
|
12
|
+
const LIMIT_WORDS = /rate ?limit|quota|userRateLimitExceeded|dailyLimitExceeded|RESOURCE_EXHAUSTED/i;
|
|
13
|
+
|
|
14
|
+
/** What an answer that is not a success means. A 403 is a limit reached only where the service says so. Pure. */
|
|
15
|
+
export const failureOf = (input: { readonly service: string; readonly status: number; readonly headers: Readonly<Record<string, string | undefined>>; readonly text: string }): SourceFailure => {
|
|
16
|
+
const { service, status, headers, text } = input;
|
|
17
|
+
const reason = `HTTP ${status}: ${text.slice(0, 300)}`;
|
|
18
|
+
if (status === 401) return new SignInExpired({ service, reason, plain: `${service} needs signing in again` });
|
|
19
|
+
const limited = status === 403 && (headers['x-ratelimit-remaining'] === '0' || headers['retry-after'] !== undefined || LIMIT_WORDS.test(text));
|
|
20
|
+
if (limited || status === 429) return new ServiceUnavailable({ service, reason, plain: `${service}'s limit on requests is reached` });
|
|
21
|
+
if (status === 408 || status >= 500) return new ServiceUnavailable({ service, reason, plain: `${service} is at fault (${status})` });
|
|
22
|
+
return new Refused({ service, reason, status, plain: status === 404 ? 'not there, or the signed-in account can\'t see it' : status === 403 ? 'the signed-in account isn\'t allowed to' : `${service} refused (${status})` });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export interface Answer<A> {
|
|
26
|
+
readonly body: A;
|
|
27
|
+
readonly headers: Readonly<Record<string, string | undefined>>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class HttpApi {
|
|
31
|
+
private readonly answers = new Map<string, { readonly etag: string; readonly json: unknown; readonly headers: Answer<unknown>['headers'] }>();
|
|
32
|
+
|
|
33
|
+
constructor(private readonly input: {
|
|
34
|
+
readonly service: string;
|
|
35
|
+
/** The only origin the token is ever sent to. */
|
|
36
|
+
readonly origin: string;
|
|
37
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
38
|
+
readonly client: HttpClient.HttpClient;
|
|
39
|
+
readonly signIn: ISignIn;
|
|
40
|
+
}) {}
|
|
41
|
+
|
|
42
|
+
*get<A>(url: string, schema: Schema.Codec<A, unknown>): Effect.fn.Return<Answer<A>, SourceFailure> {
|
|
43
|
+
const kept = this.answers.get(url);
|
|
44
|
+
const request = HttpClientRequest.get(url);
|
|
45
|
+
const response = yield* this.request(kept === undefined ? request : HttpClientRequest.setHeader(request, 'if-none-match', kept.etag));
|
|
46
|
+
if (response.status === 304 && kept !== undefined) return yield* this.decoded({ json: kept.json, headers: kept.headers }, schema);
|
|
47
|
+
const json = yield* this.jsonOf(response);
|
|
48
|
+
const etag = response.headers['etag'];
|
|
49
|
+
if (etag !== undefined) {
|
|
50
|
+
this.answers.delete(url);
|
|
51
|
+
this.answers.set(url, { etag, json, headers: response.headers });
|
|
52
|
+
if (this.answers.size > ANSWERS_KEPT) this.answers.delete(this.answers.keys().next().value!);
|
|
53
|
+
}
|
|
54
|
+
return yield* this.decoded({ json, headers: response.headers }, schema);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
*send<A>(call: { readonly method: 'POST' | 'PATCH'; readonly url: string; readonly body: unknown }, schema: Schema.Codec<A, unknown>): Effect.fn.Return<Answer<A>, SourceFailure> {
|
|
58
|
+
const response = yield* this.request(HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.make(call.method)(call.url), call.body));
|
|
59
|
+
return yield* this.decoded({ json: yield* this.jsonOf(response), headers: response.headers }, schema);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Sends it signed in. A token the service refuses is asked for again once: it can run out while it is kept. */
|
|
63
|
+
private *request(request: HttpClientRequest.HttpClientRequest): Effect.fn.Return<HttpClientResponse.HttpClientResponse, SourceFailure> {
|
|
64
|
+
const { service, origin, headers, client, signIn } = this.input;
|
|
65
|
+
if (new URL(request.url).origin !== origin) return yield* Effect.fail(new Refused({ service, reason: `${new URL(request.url).origin} is not ${origin}, the one place its sign-in is for`, status: null, plain: `${service} pointed somewhere else` }));
|
|
66
|
+
for (const fresh of [false, true]) {
|
|
67
|
+
const token = yield* signIn.token({ fresh });
|
|
68
|
+
const response = yield* pipe(
|
|
69
|
+
client.execute(pipe(request, HttpClientRequest.bearerToken(token), HttpClientRequest.setHeaders(headers))),
|
|
70
|
+
Effect.mapError((e) => new ServiceUnavailable({ service, reason: e.message, plain: `${service} can't be reached` })),
|
|
71
|
+
Effect.timeoutOrElse({ duration: CALL_LIMIT, orElse: () => Effect.fail(new ServiceUnavailable({ service, reason: `no answer within ${CALL_LIMIT}`, plain: `${service} didn't answer` })) }),
|
|
72
|
+
);
|
|
73
|
+
if ((response.status >= 200 && response.status < 300) || response.status === 304) return response;
|
|
74
|
+
const failure = failureOf({ service, status: response.status, headers: response.headers, text: yield* Effect.orElseSucceed(response.text, () => '') });
|
|
75
|
+
if (failure._tag !== 'SignInExpired' || fresh) return yield* Effect.fail(failure);
|
|
76
|
+
}
|
|
77
|
+
return yield* Effect.die('unreachable: the second try returns or fails');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The answer's JSON. A body that ends early says nothing about the request: for a post, it may have landed. */
|
|
81
|
+
private *jsonOf(response: HttpClientResponse.HttpClientResponse): Effect.fn.Return<unknown, SourceFailure> {
|
|
82
|
+
return yield* pipe(
|
|
83
|
+
response.json,
|
|
84
|
+
Effect.mapError((e) => new ServiceUnavailable({ service: this.input.service, reason: `its answer could not be read: ${e.message}`, plain: `${this.input.service}'s answer was cut short` })),
|
|
85
|
+
Effect.timeoutOrElse({ duration: CALL_LIMIT, orElse: () => Effect.fail(new ServiceUnavailable({ service: this.input.service, reason: `its answer did not end within ${CALL_LIMIT}`, plain: `${this.input.service}'s answer was cut short` })) }),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private *decoded<A>(answer: { readonly json: unknown; readonly headers: Answer<A>['headers'] }, schema: Schema.Codec<A, unknown>): Effect.fn.Return<Answer<A>, SourceFailure> {
|
|
90
|
+
const body = yield* Effect.mapError(Schema.decodeUnknownEffect(schema)(answer.json), (e) => new ServiceUnavailable({ service: this.input.service, reason: `an answer of a shape this version does not know: ${e.message.slice(0, 300)}`, plain: `${this.input.service} answered in a way this version doesn't know` }));
|
|
91
|
+
return { body, headers: answer.headers };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A PLACE COMMENTS ARE LEFT, WHATEVER SYSTEM HOLDS IT: a Google Doc's tabs, a repository's pull requests. The workflow knows
|
|
3
|
+
* only this; each kind of place is one implementation of it, in a folder of its own.
|
|
4
|
+
*/
|
|
5
|
+
import { Data, Schema, type Effect, type Option } from 'effect';
|
|
6
|
+
import type { NamedPlace } from '../watching';
|
|
7
|
+
|
|
8
|
+
/** Who answers: the one T3 thread of a pull request, or the command thread of a T3 project. */
|
|
9
|
+
export const Target = Schema.Union([
|
|
10
|
+
Schema.Struct({ kind: Schema.Literal('pull request'), url: Schema.String }),
|
|
11
|
+
Schema.Struct({ kind: Schema.Literal('project'), id: Schema.String }),
|
|
12
|
+
]);
|
|
13
|
+
export type Target = typeof Target.Type;
|
|
14
|
+
|
|
15
|
+
export const EventKind = Schema.Literals(['comment', 'review', 'line comment', 'plan approved', 'plan rejected']);
|
|
16
|
+
export type EventKind = typeof EventKind.Type;
|
|
17
|
+
/** Whether an event is something to answer, and not a verdict on a plan. */
|
|
18
|
+
export const isOrderKind = (kind: EventKind): boolean => kind !== 'plan approved' && kind !== 'plan rejected';
|
|
19
|
+
|
|
20
|
+
/** What was left for an agent, with everything its delivery says, so nothing is read again to make it. */
|
|
21
|
+
export const SourceEvent = Schema.Struct({
|
|
22
|
+
/** Names this event and no other, on every machine (`keyOf`). */
|
|
23
|
+
key: Schema.String,
|
|
24
|
+
kind: EventKind,
|
|
25
|
+
/** The post it is, or the reaction. */
|
|
26
|
+
post: Schema.String,
|
|
27
|
+
target: Target,
|
|
28
|
+
/** What its source needs to read its conversation and to act in it, which nothing else reads. */
|
|
29
|
+
ref: Schema.String,
|
|
30
|
+
/** Where a person sees it. */
|
|
31
|
+
url: Schema.String,
|
|
32
|
+
/** What it is on: the pull request's title, the Doc and tab's. */
|
|
33
|
+
title: Schema.String,
|
|
34
|
+
/** Their words; for a verdict, the plan's. */
|
|
35
|
+
text: Schema.String,
|
|
36
|
+
/** The file and line, or the text the comment is on. */
|
|
37
|
+
where: Schema.NullOr(Schema.String),
|
|
38
|
+
/** The conversation so far, where whoever answers cannot read it themselves. */
|
|
39
|
+
conversation: Schema.NullOr(Schema.Array(Schema.Struct({ author: Schema.String, text: Schema.String }))),
|
|
40
|
+
/** The link a reply opens with, where the place doesn't thread replies. */
|
|
41
|
+
opening: Schema.NullOr(Schema.String),
|
|
42
|
+
/** For a plan approved: the hidden line its acknowledgement ends with; none where the place can't hide one. */
|
|
43
|
+
acknowledgementLine: Schema.NullOr(Schema.String),
|
|
44
|
+
});
|
|
45
|
+
export type SourceEvent = typeof SourceEvent.Type;
|
|
46
|
+
|
|
47
|
+
/** An event as a listing gives it. */
|
|
48
|
+
export interface Listed {
|
|
49
|
+
readonly event: SourceEvent;
|
|
50
|
+
/** It was there before its place was first watched. */
|
|
51
|
+
readonly before: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** What a source found. It may give an event again, and gives each until its place no longer holds it: the store takes each once. */
|
|
55
|
+
export interface Listing {
|
|
56
|
+
readonly events: ReadonlyArray<Listed>;
|
|
57
|
+
/** Where the next listing starts, which nothing but this source reads. */
|
|
58
|
+
readonly cursor: string;
|
|
59
|
+
/** Each place and each entry the settings name in it, by the names the service gives them now. */
|
|
60
|
+
readonly watched: ReadonlyArray<NamedPlace>;
|
|
61
|
+
/** What the listing cost, and what it could not place, in words, for the step's record. */
|
|
62
|
+
readonly notes: ReadonlyArray<string>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ThreadPost {
|
|
66
|
+
readonly post: string;
|
|
67
|
+
readonly author: string;
|
|
68
|
+
readonly text: string;
|
|
69
|
+
readonly url: string;
|
|
70
|
+
/** The account this machine is signed in as wrote it: a person's or their agent's. */
|
|
71
|
+
readonly mine: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface FailureFields {
|
|
75
|
+
readonly service: string;
|
|
76
|
+
/** What happened, for a record. */
|
|
77
|
+
readonly reason: string;
|
|
78
|
+
/** What happened, for the person who reads `skill-sync watching`. */
|
|
79
|
+
readonly plain: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The signed-in account's access ran out, so nothing can be read or posted until it is signed in again. */
|
|
83
|
+
export class SignInExpired extends Data.TaggedError('SignInExpired')<FailureFields> {}
|
|
84
|
+
/** The service could not do it now and may later: no answer, a limit reached, a fault of its own. Whether a post landed is not known. */
|
|
85
|
+
export class ServiceUnavailable extends Data.TaggedError('ServiceUnavailable')<FailureFields> {}
|
|
86
|
+
/** The service answered and will not do it: not there, not allowed, not valid. Nothing was posted. */
|
|
87
|
+
export class Refused extends Data.TaggedError('Refused')<FailureFields & { readonly status: number | null }> {}
|
|
88
|
+
export type SourceFailure = SignInExpired | ServiceUnavailable | Refused;
|
|
89
|
+
|
|
90
|
+
type Where = Pick<SourceEvent, 'key' | 'ref'>;
|
|
91
|
+
|
|
92
|
+
/** What changed of the 👀 shown, and why each that did not could not. */
|
|
93
|
+
export interface Shown { readonly changed: number; readonly failed: ReadonlyArray<string> }
|
|
94
|
+
|
|
95
|
+
export interface ICommentSource {
|
|
96
|
+
/** Names it in the store and in records: the same for as long as the settings name any of its places. */
|
|
97
|
+
readonly id: string;
|
|
98
|
+
/** Its places and their entries as the settings alone name them, for what no read has come to. */
|
|
99
|
+
readonly settings: ReadonlyArray<NamedPlace>;
|
|
100
|
+
/** What is there for an agent; none on the first listing, when everything found was there before. */
|
|
101
|
+
newSince(cursor: Option.Option<string>): Effect.fn.Return<Listing, SourceFailure>;
|
|
102
|
+
/**
|
|
103
|
+
* Shows 👀 on each queued comment the last listing found, until a reply to that comment is recorded. What shows is worked
|
|
104
|
+
* out from the place itself each time, so nothing here is kept: one that fails is tried again after the next listing, and
|
|
105
|
+
* none is shown twice or left behind.
|
|
106
|
+
*/
|
|
107
|
+
showReceived(input: { readonly isQueued: (key: string) => boolean; readonly isAnswered: (key: string) => boolean }): Effect.fn.Return<Shown>;
|
|
108
|
+
/** Every post of an event's conversation, oldest first, however long it is: Post looks here for a reply already made. */
|
|
109
|
+
thread(event: Where): Effect.fn.Return<ReadonlyArray<ThreadPost>, SourceFailure>;
|
|
110
|
+
/** Posts one reply in an event's conversation, exactly as given. Never checks for an earlier one: that is Post's. */
|
|
111
|
+
reply(event: Where, text: string): Effect.fn.Return<ThreadPost, SourceFailure>;
|
|
112
|
+
/** Makes a post of this account's in an event's conversation read as given. */
|
|
113
|
+
edit(event: Where, post: string, text: string): Effect.fn.Return<void, SourceFailure>;
|
|
114
|
+
/** The most bytes of UTF-8 a post may hold here; none where the service states no limit. */
|
|
115
|
+
readonly maxPostBytes: Option.Option<number>;
|
|
116
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** The signed-in account of one source: its access token, from the command its settings name, kept a few minutes. */
|
|
2
|
+
import { Effect } from 'effect';
|
|
3
|
+
import { findCommand, runCommand } from '../../agents/command';
|
|
4
|
+
import { SignInExpired } from './interface';
|
|
5
|
+
|
|
6
|
+
/** Shorter than any token's life, so a token is asked for again before it runs out. */
|
|
7
|
+
const KEPT_MS = 10 * 60_000;
|
|
8
|
+
const COMMAND_LIMIT_MS = 30_000;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* What a failed token command said, as far as it may be kept: its first line, with each run of characters a token is made of
|
|
12
|
+
* that is long enough to be one taken out. It goes into logs, the database and `skill-sync watching`.
|
|
13
|
+
*/
|
|
14
|
+
export const firstLineRedacted = (stderr: string): string =>
|
|
15
|
+
(stderr.trim().split('\n')[0] ?? '').trim().replace(/[A-Za-z0-9._~+/=-]{20,}/g, '[redacted]').slice(0, 200);
|
|
16
|
+
|
|
17
|
+
export interface ISignIn {
|
|
18
|
+
/** The access token: the kept one, or a new one when `fresh` or it is old. */
|
|
19
|
+
token(input: { readonly fresh: boolean }): Effect.fn.Return<string, SignInExpired>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class CommandSignIn implements ISignIn {
|
|
23
|
+
private kept: { readonly token: string; readonly at: number } | null = null;
|
|
24
|
+
|
|
25
|
+
constructor(private readonly input: { readonly service: string; readonly command: ReadonlyArray<string>; readonly home: string }) {}
|
|
26
|
+
|
|
27
|
+
*token({ fresh }: { readonly fresh: boolean }): Effect.fn.Return<string, SignInExpired> {
|
|
28
|
+
if (!fresh && this.kept !== null && Date.now() - this.kept.at < KEPT_MS) return this.kept.token;
|
|
29
|
+
const { service, command, home } = this.input;
|
|
30
|
+
const program = command[0]!;
|
|
31
|
+
const failed = (reason: string) => new SignInExpired({ service, reason, plain: `${service} needs signing in again: ${reason}` });
|
|
32
|
+
// The service manager's PATH is not the shell's, so a bare name is looked for where installers put it.
|
|
33
|
+
const found = program.includes('/') ? program : findCommand({ name: program, home });
|
|
34
|
+
if (found === undefined) return yield* Effect.fail(failed(`its token command, ${program}, was not found on this machine`));
|
|
35
|
+
const ran = yield* Effect.promise(() => runCommand({ command: found, args: command.slice(1), env: process.env, cwd: home, timeoutMs: COMMAND_LIMIT_MS }));
|
|
36
|
+
const token = ran.stdout.trim();
|
|
37
|
+
// Its output is never in the reason: it is a credential where the command half worked.
|
|
38
|
+
if (ran.failure !== undefined || ran.status !== 0 || token === '') {
|
|
39
|
+
const said = firstLineRedacted(ran.stderr);
|
|
40
|
+
return yield* Effect.fail(failed(`its token command gave no token (${ran.failure ?? `exit ${String(ran.status)}`})${said === '' ? '' : `: ${said}`}`));
|
|
41
|
+
}
|
|
42
|
+
this.kept = { token, at: Date.now() };
|
|
43
|
+
return token;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The sources a machine's settings name: the one list of kinds of source. */
|
|
2
|
+
import type { HttpClient } from 'effect/http';
|
|
3
|
+
import { repositoryAuthors, type CommentWatchSettings } from '../settings';
|
|
4
|
+
import { GitHubPullRequestsSource } from './github-pull-requests/impl';
|
|
5
|
+
import { GoogleDocSource } from './google-doc/impl';
|
|
6
|
+
import type { ICommentSource } from './interface';
|
|
7
|
+
import type { ISignIn } from './sign-in';
|
|
8
|
+
|
|
9
|
+
/** GitHub is read and answered as `gh` is signed in on this machine. */
|
|
10
|
+
const GH_TOKEN = ['gh', 'auth', 'token'];
|
|
11
|
+
|
|
12
|
+
export const sourcesOf = (input: { readonly settings: CommentWatchSettings; readonly client: HttpClient.HttpClient; readonly signIn: (by: { readonly service: string; readonly command: ReadonlyArray<string> }) => ISignIn }): ReadonlyArray<ICommentSource> => {
|
|
13
|
+
const { settings, client, signIn } = input;
|
|
14
|
+
const pairs = repositoryAuthors(settings.sources);
|
|
15
|
+
return [
|
|
16
|
+
...settings.sources.flatMap((s) => (s.kind === 'google-doc' ? [new GoogleDocSource(s, { client, signIn: signIn({ service: 'Google Docs', command: s.tokenCommand }) })] : [])),
|
|
17
|
+
// Every repository and author in one source: one poll covers them all.
|
|
18
|
+
...(pairs.length === 0 ? [] : [new GitHubPullRequestsSource({ pairs, tag: settings.tag }, { client, signIn: signIn({ service: 'GitHub', command: GH_TOKEN }) })]),
|
|
19
|
+
];
|
|
20
|
+
};
|