@celljs/ta-creator 4.32.1 → 4.33.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.en.md +2 -1
- package/README.md +9 -2
- package/host/index.mjs +1529 -279
- package/package.json +3 -3
- package/skills/creator-make/SKILL.md +34 -13
- package/skills/creator-publish/SKILL.md +24 -8
- package/skills/creator-publish/platforms.md +94 -0
- package/web/index.js +1549 -245
package/host/index.mjs
CHANGED
|
@@ -8,11 +8,21 @@ var Keys;
|
|
|
8
8
|
Keys.WEEKS = "weeks";
|
|
9
9
|
Keys.TASKS = "tasks";
|
|
10
10
|
Keys.INSIGHTS = "insights";
|
|
11
|
+
Keys.COMMENTS = "comments";
|
|
12
|
+
Keys.LESSONS = "lessons";
|
|
13
|
+
Keys.PHASES = "phases";
|
|
11
14
|
})(Keys ||= {});
|
|
12
|
-
var STAGES = ["topic", "making", "review", "published"];
|
|
13
|
-
var FORMS = ["video", "post"];
|
|
14
|
-
var
|
|
15
|
+
var STAGES = ["topic", "materials", "making", "review", "published"];
|
|
16
|
+
var FORMS = ["video", "post", "article", "note"];
|
|
17
|
+
var FORM_FALLBACKS = {
|
|
18
|
+
video: ["video", "article", "post"],
|
|
19
|
+
post: ["post", "note", "article"],
|
|
20
|
+
article: ["article", "post", "note"],
|
|
21
|
+
note: ["note", "post"]
|
|
22
|
+
};
|
|
23
|
+
var KINDS = ["hot", "howto", "explain", "original"];
|
|
15
24
|
var STEPS = ["script", "visuals", "voice", "captions", "versions", "checks"];
|
|
25
|
+
var REACHES = ["public", "internal", "both"];
|
|
16
26
|
var SERIES_COLORS = ["#22c55e", "#f97316", "#3b82f6", "#a855f7", "#ec4899", "#14b8a6", "#eab308", "#64748b"];
|
|
17
27
|
var ASPECTS = ["16:9", "9:16", "3:4", "1:1"];
|
|
18
28
|
var PLATFORMS = [
|
|
@@ -21,6 +31,8 @@ var PLATFORMS = [
|
|
|
21
31
|
name: "B站",
|
|
22
32
|
color: "#fb7299",
|
|
23
33
|
video: { aspect: "16:9" },
|
|
34
|
+
article: {},
|
|
35
|
+
note: { maxChars: 1000 },
|
|
24
36
|
publish: "https://member.bilibili.com/platform/upload/video/frame",
|
|
25
37
|
declare: "创作声明 → 该视频使用人工智能合成技术"
|
|
26
38
|
},
|
|
@@ -30,6 +42,7 @@ var PLATFORMS = [
|
|
|
30
42
|
color: "#ff2442",
|
|
31
43
|
video: { aspect: "3:4", maxSeconds: 900 },
|
|
32
44
|
post: { aspect: "3:4", maxImages: 18 },
|
|
45
|
+
article: {},
|
|
33
46
|
publish: "https://creator.xiaohongshu.com",
|
|
34
47
|
declare: "内容类型声明 → 笔记含 AI 合成内容"
|
|
35
48
|
},
|
|
@@ -38,6 +51,7 @@ var PLATFORMS = [
|
|
|
38
51
|
name: "视频号",
|
|
39
52
|
color: "#fa9d3b",
|
|
40
53
|
video: { aspect: "9:16" },
|
|
54
|
+
post: { aspect: "3:4", maxImages: 9 },
|
|
41
55
|
publish: "https://channels.weixin.qq.com",
|
|
42
56
|
declare: "声明 → 内容由 AI 生成"
|
|
43
57
|
},
|
|
@@ -56,6 +70,7 @@ var PLATFORMS = [
|
|
|
56
70
|
name: "YouTube",
|
|
57
71
|
color: "#ff0033",
|
|
58
72
|
video: { aspect: "16:9" },
|
|
73
|
+
note: { maxChars: 1500 },
|
|
59
74
|
publish: "https://studio.youtube.com",
|
|
60
75
|
declare: "Altered or synthetic content → Yes"
|
|
61
76
|
},
|
|
@@ -63,9 +78,19 @@ var PLATFORMS = [
|
|
|
63
78
|
id: "wechat-article",
|
|
64
79
|
name: "公众号",
|
|
65
80
|
color: "#07c160",
|
|
66
|
-
|
|
81
|
+
article: {},
|
|
67
82
|
publish: "https://mp.weixin.qq.com",
|
|
68
83
|
declare: "创作来源 → 内容由 AI 生成"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: "zhihu",
|
|
87
|
+
name: "知乎",
|
|
88
|
+
color: "#1772f6",
|
|
89
|
+
video: { aspect: "16:9" },
|
|
90
|
+
article: {},
|
|
91
|
+
note: { maxChars: 1000 },
|
|
92
|
+
publish: "https://www.zhihu.com/creator",
|
|
93
|
+
declare: "创作声明 → 包含 AI 辅助创作"
|
|
69
94
|
}
|
|
70
95
|
];
|
|
71
96
|
var SOURCES = [
|
|
@@ -74,85 +99,256 @@ var SOURCES = [
|
|
|
74
99
|
{ name: "Product Hunt", url: "https://www.producthunt.com" },
|
|
75
100
|
{ name: "Hugging Face Papers", url: "https://huggingface.co/papers" }
|
|
76
101
|
];
|
|
102
|
+
var COMMENT_CLASSES = ["question", "suggestion", "correction", "praise", "negative", "spam"];
|
|
103
|
+
var REPLY_CLASSES = ["question", "suggestion", "correction", "praise"];
|
|
104
|
+
var REPLY_STATES = ["draft", "approved", "sent", "dropped", "withdrawn", "removed"];
|
|
105
|
+
var REPLY_KINDS = {
|
|
106
|
+
question: {
|
|
107
|
+
id: "creator.reply-question",
|
|
108
|
+
owner: NAME,
|
|
109
|
+
title: { en: "Answer questions in the comments", zh: "回复评论里的提问", tw: "回覆留言裡的提問", ja: "コメントの質問に返信" },
|
|
110
|
+
reversible: true,
|
|
111
|
+
level: "act"
|
|
112
|
+
},
|
|
113
|
+
praise: {
|
|
114
|
+
id: "creator.reply-praise",
|
|
115
|
+
owner: NAME,
|
|
116
|
+
title: { en: "Thank those who praise a piece", zh: "感谢评论里的夸奖", tw: "感謝留言裡的稱讚", ja: "コメントの称賛にお礼" },
|
|
117
|
+
reversible: true,
|
|
118
|
+
level: "act"
|
|
119
|
+
},
|
|
120
|
+
suggestion: {
|
|
121
|
+
id: "creator.reply-suggestion",
|
|
122
|
+
owner: NAME,
|
|
123
|
+
title: { en: "Answer suggestions in the comments", zh: "回复评论里的建议", tw: "回覆留言裡的建議", ja: "コメントの提案に返信" },
|
|
124
|
+
reversible: true,
|
|
125
|
+
level: "ask"
|
|
126
|
+
},
|
|
127
|
+
correction: {
|
|
128
|
+
id: "creator.reply-correction",
|
|
129
|
+
owner: NAME,
|
|
130
|
+
title: { en: "Answer corrections in the comments", zh: "回复评论里的纠错", tw: "回覆留言裡的糾錯", ja: "コメントの指摘に返信" },
|
|
131
|
+
reversible: true,
|
|
132
|
+
level: "ask"
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// web-src/publish-util.ts
|
|
137
|
+
var text = (value, max) => typeof value === "string" ? value.trim().slice(0, max) : "";
|
|
138
|
+
var time = (value) => typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
139
|
+
var object = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
140
|
+
var PublishUtil;
|
|
141
|
+
((PublishUtil) => {
|
|
142
|
+
function dayOf(at) {
|
|
143
|
+
const day = new Date(at);
|
|
144
|
+
const two = (n) => String(n).padStart(2, "0");
|
|
145
|
+
return `${day.getFullYear()}-${two(day.getMonth() + 1)}-${two(day.getDate())}`;
|
|
146
|
+
}
|
|
147
|
+
PublishUtil.dayOf = dayOf;
|
|
148
|
+
function publishQueue(items) {
|
|
149
|
+
const due = (item) => item.kind === "hot" && item.until ? item.until : Number.POSITIVE_INFINITY;
|
|
150
|
+
return items.filter((one) => one.publishing?.state === "queued" && !one.dropped).sort((a, b) => due(a) - due(b) || (a.approved ?? a.changedAt) - (b.approved ?? b.changedAt));
|
|
151
|
+
}
|
|
152
|
+
PublishUtil.publishQueue = publishQueue;
|
|
153
|
+
function ahead(items, id) {
|
|
154
|
+
const running = items.some((one) => one.publishing?.state === "running" && !one.dropped && one.id !== id) ? 1 : 0;
|
|
155
|
+
const at = publishQueue(items).findIndex((one) => one.id === id);
|
|
156
|
+
return at < 0 ? 0 : at + running;
|
|
157
|
+
}
|
|
158
|
+
PublishUtil.ahead = ahead;
|
|
159
|
+
function slots(items, item, now, hour = 12) {
|
|
160
|
+
const done = new Set((item.links ?? []).map((one) => one.platform));
|
|
161
|
+
const platforms = [
|
|
162
|
+
...new Set((item.versions ?? []).filter((one) => one.files.length || ["article", "note"].includes(one.form ?? item.form) && !!one.text).map((one) => one.platform))
|
|
163
|
+
].filter((one) => !done.has(one));
|
|
164
|
+
const others = items.filter((one) => one.id !== item.id && !one.dropped);
|
|
165
|
+
return platforms.map((platform) => {
|
|
166
|
+
const taken = new Set([
|
|
167
|
+
...others.flatMap((one) => (one.links ?? []).filter((link) => link.platform === platform).map((link) => dayOf(link.scheduled ?? link.at))),
|
|
168
|
+
...others.filter((one) => one.publishing?.state === "running").flatMap((one) => (one.publishing?.slots ?? []).filter((slot) => slot.platform === platform).map((slot) => dayOf(slot.at ?? one.publishing?.since ?? now)))
|
|
169
|
+
]);
|
|
170
|
+
if (!taken.has(dayOf(now))) {
|
|
171
|
+
return { platform };
|
|
172
|
+
}
|
|
173
|
+
const day = new Date(now);
|
|
174
|
+
day.setHours(hour, 0, 0, 0);
|
|
175
|
+
do {
|
|
176
|
+
day.setDate(day.getDate() + 1);
|
|
177
|
+
} while (taken.has(dayOf(day.getTime())));
|
|
178
|
+
return { platform, at: day.getTime() };
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
PublishUtil.slots = slots;
|
|
182
|
+
function timeOf(value) {
|
|
183
|
+
if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}/.test(value.trim())) {
|
|
184
|
+
const at = new Date(value.trim().replace(" ", "T")).getTime();
|
|
185
|
+
return Number.isFinite(at) ? at : undefined;
|
|
186
|
+
}
|
|
187
|
+
return time(value);
|
|
188
|
+
}
|
|
189
|
+
PublishUtil.timeOf = timeOf;
|
|
190
|
+
function publishing(value) {
|
|
191
|
+
const kept = object(value);
|
|
192
|
+
const since = time(kept?.since);
|
|
193
|
+
const state = kept && ["queued", "running", "stopped"].find((one) => one === kept.state);
|
|
194
|
+
if (!kept || !since || !state) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const slots2 = Array.isArray(kept.slots) ? kept.slots.map((one) => object(one)).filter((one) => !!one && !!text(one.platform, 60)).map((one) => ({ platform: text(one.platform, 60), ...time(one.at) ? { at: time(one.at) } : {} })) : undefined;
|
|
198
|
+
return {
|
|
199
|
+
state,
|
|
200
|
+
since,
|
|
201
|
+
...slots2 ? { slots: slots2 } : {},
|
|
202
|
+
...text(kept.note, 300) ? { note: text(kept.note, 300) } : {},
|
|
203
|
+
...text(kept.ask, 500) ? { ask: text(kept.ask, 500) } : {}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
PublishUtil.publishing = publishing;
|
|
207
|
+
})(PublishUtil ||= {});
|
|
208
|
+
|
|
209
|
+
// web-src/values.ts
|
|
210
|
+
var text2 = (value, max = 2000) => typeof value === "string" ? value.trim().slice(0, max) : "";
|
|
211
|
+
var texts = (value, max = 50) => Array.isArray(value) ? value.map((one) => text2(one, 200)).filter(Boolean).slice(0, max) : [];
|
|
212
|
+
var time2 = (value) => typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
213
|
+
var oneOf = (all, value, fallback) => all.includes(value) ? value : fallback;
|
|
214
|
+
var object2 = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
215
|
+
function hash(value) {
|
|
216
|
+
let h = 0;
|
|
217
|
+
for (const char of value) {
|
|
218
|
+
h = Math.imul(h, 31) + (char.codePointAt(0) ?? 0) | 0;
|
|
219
|
+
}
|
|
220
|
+
return (h >>> 0).toString(36);
|
|
221
|
+
}
|
|
222
|
+
function url(value) {
|
|
223
|
+
const raw = text2(value, 1000);
|
|
224
|
+
return /^https?:\/\/\S+$/i.test(raw) ? raw : undefined;
|
|
225
|
+
}
|
|
226
|
+
function count(value, fallback) {
|
|
227
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? Math.round(value) : fallback;
|
|
228
|
+
}
|
|
77
229
|
|
|
78
230
|
// web-src/creator-util.ts
|
|
79
231
|
var DAY = 86400000;
|
|
80
232
|
var MAX_TOPICS = 200;
|
|
81
233
|
var HOT_FOR = 2 * DAY;
|
|
82
|
-
var text = (value, max = 2000) => typeof value === "string" ? value.trim().slice(0, max) : "";
|
|
83
|
-
var texts = (value, max = 50) => Array.isArray(value) ? value.map((one) => text(one, 200)).filter(Boolean).slice(0, max) : [];
|
|
84
|
-
var time = (value) => typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
85
|
-
var oneOf = (all, value, fallback) => all.includes(value) ? value : fallback;
|
|
86
|
-
var object = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
87
234
|
var CreatorUtil;
|
|
88
235
|
((CreatorUtil) => {
|
|
89
236
|
function settings(value) {
|
|
90
|
-
const kept =
|
|
91
|
-
const platforms = Array.isArray(kept.platforms) ? kept.platforms.map(platform).filter((one) => !!one) : [...PLATFORMS];
|
|
237
|
+
const kept = object2(value) ?? {};
|
|
238
|
+
const platforms = Array.isArray(kept.platforms) ? withBuiltins(kept.platforms.map(platform).filter((one) => !!one)) : [...PLATFORMS];
|
|
92
239
|
const ids = new Set(platforms.map((one) => one.id));
|
|
93
|
-
const goal =
|
|
240
|
+
const goal = object2(kept.goal);
|
|
94
241
|
return {
|
|
95
|
-
niche:
|
|
242
|
+
niche: text2(kept.niche, 500),
|
|
96
243
|
picked: texts(kept.picked).filter((id) => ids.has(id)),
|
|
97
244
|
platforms,
|
|
98
245
|
sources: Array.isArray(kept.sources) ? kept.sources.map(source).filter((one) => !!one) : [...SOURCES],
|
|
99
|
-
...goal &&
|
|
246
|
+
...goal && time2(goal.from) ? { goal: { from: time2(goal.from), days: count(goal.days, 30), videos: count(goal.videos, 0), posts: count(goal.posts, 0) } } : {},
|
|
100
247
|
minutesCap: count(kept.minutesCap, 120),
|
|
101
|
-
voice:
|
|
102
|
-
series: Array.isArray(kept.series) ? seriesOf(kept.series) : []
|
|
248
|
+
voice: text2(kept.voice, 60) || "zh-CN-XiaoxiaoNeural",
|
|
249
|
+
series: Array.isArray(kept.series) ? seriesOf(kept.series) : [],
|
|
250
|
+
strategy: kept.strategy === "manual" ? "manual" : "auto",
|
|
251
|
+
phaseWeeks: typeof kept.phaseWeeks === "number" ? Math.min(26, count(kept.phaseWeeks, 4)) : 4,
|
|
252
|
+
commentsEvery: Math.min(72, count(kept.commentsEvery, 0)),
|
|
253
|
+
commentsDays: Math.max(1, Math.min(60, count(kept.commentsDays, 14)))
|
|
103
254
|
};
|
|
104
255
|
}
|
|
105
256
|
CreatorUtil.settings = settings;
|
|
257
|
+
function withBuiltins(kept) {
|
|
258
|
+
const forms = (one) => ({ video: one.video, post: one.post, article: one.article, note: one.note });
|
|
259
|
+
const known = kept.map((one) => {
|
|
260
|
+
const builtin = PLATFORMS.find((it) => it.id === one.id);
|
|
261
|
+
if (!builtin) {
|
|
262
|
+
return one;
|
|
263
|
+
}
|
|
264
|
+
const { video: _v, post: _p, article: _a, note: _n, ...rest } = one;
|
|
265
|
+
return JSON.parse(JSON.stringify({ ...rest, ...forms(builtin) }));
|
|
266
|
+
});
|
|
267
|
+
return [...known, ...PLATFORMS.filter((one) => !kept.some((it) => it.id === one.id))];
|
|
268
|
+
}
|
|
269
|
+
CreatorUtil.withBuiltins = withBuiltins;
|
|
270
|
+
function reach(item2, settings2) {
|
|
271
|
+
return item2.reach ?? settings2.series.find((one) => one.id === item2.series)?.reach ?? "public";
|
|
272
|
+
}
|
|
273
|
+
CreatorUtil.reach = reach;
|
|
274
|
+
function platformsFor(settings2, item2) {
|
|
275
|
+
const to = reach(item2, settings2);
|
|
276
|
+
return picked(settings2).filter((platform2) => to === "both" || to === "internal" === !!platform2.internal).map((platform2) => ({ platform: platform2, form: formFor(platform2, item2.form) })).filter((one) => !!one.form);
|
|
277
|
+
}
|
|
278
|
+
CreatorUtil.platformsFor = platformsFor;
|
|
279
|
+
function takes(platform2, form) {
|
|
280
|
+
return !!platform2[form];
|
|
281
|
+
}
|
|
282
|
+
CreatorUtil.takes = takes;
|
|
283
|
+
function formFor(platform2, form) {
|
|
284
|
+
return FORM_FALLBACKS[form].find((one) => takes(platform2, one));
|
|
285
|
+
}
|
|
286
|
+
CreatorUtil.formFor = formFor;
|
|
287
|
+
function ready(version2, form) {
|
|
288
|
+
return version2.files.length > 0 || (version2.form ?? form) !== "video" && (version2.form ?? form) !== "post" && !!version2.text;
|
|
289
|
+
}
|
|
290
|
+
CreatorUtil.ready = ready;
|
|
106
291
|
function seriesOf(values) {
|
|
107
292
|
const all = [];
|
|
108
293
|
for (const value of values) {
|
|
109
|
-
const kept =
|
|
110
|
-
const name =
|
|
294
|
+
const kept = object2(value);
|
|
295
|
+
const name = text2(kept?.name, 40);
|
|
111
296
|
if (!kept || !name) {
|
|
112
297
|
continue;
|
|
113
298
|
}
|
|
114
|
-
const id =
|
|
299
|
+
const id = text2(kept.id, 60) || `s${hash(name)}`;
|
|
115
300
|
if (all.some((one) => one.id === id)) {
|
|
116
301
|
continue;
|
|
117
302
|
}
|
|
118
|
-
const about =
|
|
303
|
+
const about = text2(kept.about, 200);
|
|
119
304
|
const perWeek = count(kept.perWeek, 0);
|
|
120
305
|
all.push({
|
|
121
306
|
id,
|
|
122
307
|
name,
|
|
123
308
|
...about ? { about } : {},
|
|
124
|
-
color:
|
|
309
|
+
color: text2(kept.color, 40) || SERIES_COLORS[all.length % SERIES_COLORS.length],
|
|
125
310
|
...kept.ordered === true ? { ordered: true } : {},
|
|
126
|
-
...perWeek ? { perWeek: Math.min(perWeek, 14) } : {}
|
|
311
|
+
...perWeek ? { perWeek: Math.min(perWeek, 14) } : {},
|
|
312
|
+
...kept.reach === "internal" || kept.reach === "both" ? { reach: kept.reach } : {}
|
|
127
313
|
});
|
|
128
314
|
}
|
|
129
315
|
return all.slice(0, 12);
|
|
130
316
|
}
|
|
131
317
|
CreatorUtil.seriesOf = seriesOf;
|
|
132
318
|
function platform(value) {
|
|
133
|
-
const kept =
|
|
134
|
-
const id =
|
|
135
|
-
const name =
|
|
319
|
+
const kept = object2(value);
|
|
320
|
+
const id = text2(kept?.id, 60);
|
|
321
|
+
const name = text2(kept?.name, 60);
|
|
136
322
|
if (!kept || !id || !name) {
|
|
137
323
|
return;
|
|
138
324
|
}
|
|
139
325
|
const shape = (one) => {
|
|
140
|
-
const it =
|
|
326
|
+
const it = object2(one);
|
|
141
327
|
return it ? { aspect: oneOf(ASPECTS, it.aspect, "16:9"), ...count(it.maxSeconds, 0) ? { maxSeconds: count(it.maxSeconds, 0) } : {} } : undefined;
|
|
142
328
|
};
|
|
143
329
|
const video = shape(kept.video);
|
|
144
|
-
const post =
|
|
145
|
-
aspect: oneOf(ASPECTS,
|
|
146
|
-
...count(
|
|
330
|
+
const post = object2(kept.post) ? {
|
|
331
|
+
aspect: oneOf(ASPECTS, object2(kept.post)?.aspect, "3:4"),
|
|
332
|
+
...count(object2(kept.post)?.maxImages, 0) ? { maxImages: count(object2(kept.post)?.maxImages, 0) } : {}
|
|
147
333
|
} : undefined;
|
|
334
|
+
const words = (one) => {
|
|
335
|
+
const it = object2(one);
|
|
336
|
+
return it ? count(it.maxChars, 0) ? { maxChars: count(it.maxChars, 0) } : {} : undefined;
|
|
337
|
+
};
|
|
338
|
+
const article = words(kept.article);
|
|
339
|
+
const note = words(kept.note);
|
|
148
340
|
const publish = url(kept.publish);
|
|
149
|
-
const declare =
|
|
341
|
+
const declare = text2(kept.declare, 200);
|
|
342
|
+
const inside = kept.internal === true ? { internal: true } : {};
|
|
150
343
|
return {
|
|
344
|
+
...inside,
|
|
151
345
|
id,
|
|
152
346
|
name,
|
|
153
|
-
color:
|
|
347
|
+
color: text2(kept.color, 40) || "#8e8e93",
|
|
154
348
|
...video ? { video } : {},
|
|
155
349
|
...post ? { post } : {},
|
|
350
|
+
...article ? { article } : {},
|
|
351
|
+
...note ? { note } : {},
|
|
156
352
|
...publish ? { publish } : {},
|
|
157
353
|
...declare ? { declare } : {}
|
|
158
354
|
};
|
|
@@ -163,14 +359,14 @@ var CreatorUtil;
|
|
|
163
359
|
}
|
|
164
360
|
CreatorUtil.items = items;
|
|
165
361
|
function item(value) {
|
|
166
|
-
const kept =
|
|
167
|
-
const id =
|
|
168
|
-
const title =
|
|
169
|
-
const at =
|
|
362
|
+
const kept = object2(value);
|
|
363
|
+
const id = text2(kept?.id, 80);
|
|
364
|
+
const title = text2(kept?.title, 200);
|
|
365
|
+
const at = time2(kept?.at);
|
|
170
366
|
if (!kept || !id || !title || !at) {
|
|
171
367
|
return;
|
|
172
368
|
}
|
|
173
|
-
const origin =
|
|
369
|
+
const origin = object2(kept.origin);
|
|
174
370
|
const originUrl = url(origin?.url);
|
|
175
371
|
return {
|
|
176
372
|
id,
|
|
@@ -178,31 +374,38 @@ var CreatorUtil;
|
|
|
178
374
|
form: oneOf(FORMS, kept.form, "video"),
|
|
179
375
|
kind: oneOf(KINDS, kept.kind, "howto"),
|
|
180
376
|
stage: oneOf(STAGES, kept.stage, "topic"),
|
|
181
|
-
...
|
|
377
|
+
...text2(kept.series, 60) ? { series: text2(kept.series, 60) } : {},
|
|
182
378
|
...count(kept.number, 0) ? { number: count(kept.number, 0) } : {},
|
|
183
|
-
...
|
|
379
|
+
...REACHES.includes(kept.reach) ? { reach: kept.reach } : {},
|
|
380
|
+
...text2(kept.why, 300) ? { why: text2(kept.why, 300) } : {},
|
|
184
381
|
...typeof kept.heat === "number" ? { heat: Math.max(0, Math.min(100, Math.round(kept.heat))) } : {},
|
|
185
|
-
...
|
|
382
|
+
...time2(kept.until) ? { until: time2(kept.until) } : {},
|
|
186
383
|
...texts(kept.seen, 10).length ? { seen: texts(kept.seen, 10) } : {},
|
|
187
384
|
...origin && originUrl ? {
|
|
188
385
|
origin: {
|
|
189
386
|
url: originUrl,
|
|
190
|
-
...
|
|
191
|
-
...
|
|
192
|
-
...
|
|
387
|
+
...text2(origin.title, 200) ? { title: text2(origin.title, 200) } : {},
|
|
388
|
+
...text2(origin.author, 100) ? { author: text2(origin.author, 100) } : {},
|
|
389
|
+
...text2(origin.licence, 100) ? { licence: text2(origin.licence, 100) } : {}
|
|
193
390
|
}
|
|
194
391
|
} : {},
|
|
392
|
+
...Array.isArray(kept.needs) ? {
|
|
393
|
+
needs: kept.needs.map(need).filter((one) => !!one).slice(0, 12)
|
|
394
|
+
} : {},
|
|
195
395
|
...Array.isArray(kept.done) ? { done: STEPS.filter((step) => kept.done.includes(step)) } : {},
|
|
196
|
-
...
|
|
396
|
+
...text2(kept.script, 20000) ? { script: text2(kept.script, 20000) } : {},
|
|
197
397
|
...Array.isArray(kept.versions) ? { versions: kept.versions.map(version).filter((one) => !!one) } : {},
|
|
198
398
|
...Array.isArray(kept.checks) ? { checks: kept.checks.map(check).filter((one) => !!one) } : {},
|
|
199
|
-
...
|
|
200
|
-
...
|
|
399
|
+
...text2(kept.session, 1000) ? { session: text2(kept.session, 1000) } : {},
|
|
400
|
+
...time2(kept.approved) ? { approved: time2(kept.approved) } : {},
|
|
401
|
+
...PublishUtil.publishing(kept.publishing) ? { publishing: PublishUtil.publishing(kept.publishing) } : {},
|
|
201
402
|
...Array.isArray(kept.links) ? { links: kept.links.map(link).filter((one) => !!one) } : {},
|
|
202
403
|
...Array.isArray(kept.stats) ? { stats: kept.stats.map(stat).filter((one) => !!one) } : {},
|
|
203
404
|
...kept.dropped === true ? { dropped: true } : {},
|
|
405
|
+
...time2(kept.commentsAt) ? { commentsAt: time2(kept.commentsAt) } : {},
|
|
406
|
+
...time2(kept.commentsGap) ? { commentsGap: time2(kept.commentsGap) } : {},
|
|
204
407
|
at,
|
|
205
|
-
changedAt:
|
|
408
|
+
changedAt: time2(kept.changedAt) ?? at
|
|
206
409
|
};
|
|
207
410
|
}
|
|
208
411
|
CreatorUtil.item = item;
|
|
@@ -210,10 +413,10 @@ var CreatorUtil;
|
|
|
210
413
|
if (!Array.isArray(value)) {
|
|
211
414
|
return [];
|
|
212
415
|
}
|
|
213
|
-
return value.map((one) =>
|
|
416
|
+
return value.map((one) => object2(one)).filter((one) => !!one && /^\d{4}-\d{2}-\d{2}$/.test(String(one.week))).map((one) => ({
|
|
214
417
|
week: String(one.week),
|
|
215
|
-
platforms: (Array.isArray(one.platforms) ? one.platforms : []).map((it) =>
|
|
216
|
-
platform:
|
|
418
|
+
platforms: (Array.isArray(one.platforms) ? one.platforms : []).map((it) => object2(it)).filter((it) => !!it && !!text2(it.platform, 60)).map((it) => ({
|
|
419
|
+
platform: text2(it.platform, 60),
|
|
217
420
|
views: count(it.views, 0),
|
|
218
421
|
...typeof it.likes === "number" ? { likes: count(it.likes, 0) } : {},
|
|
219
422
|
...typeof it.follows === "number" ? { follows: count(it.follows, 0) } : {},
|
|
@@ -232,6 +435,7 @@ var CreatorUtil;
|
|
|
232
435
|
const of = (stage) => items2.filter((one) => one.stage === stage && !one.dropped);
|
|
233
436
|
return {
|
|
234
437
|
topic: of("topic").filter((one) => !expired(one, now)).sort((a, b) => (b.heat ?? 0) - (a.heat ?? 0) || b.at - a.at),
|
|
438
|
+
materials: of("materials").sort(latest),
|
|
235
439
|
making: of("making").sort(latest),
|
|
236
440
|
review: of("review").sort(latest),
|
|
237
441
|
published: of("published").sort((a, b) => publishedAt(b) - publishedAt(a))
|
|
@@ -243,7 +447,7 @@ var CreatorUtil;
|
|
|
243
447
|
}
|
|
244
448
|
CreatorUtil.expired = expired;
|
|
245
449
|
function publishedAt(item2) {
|
|
246
|
-
return Math.min(...(item2.links ?? []).map((one) => one.at), item2.changedAt);
|
|
450
|
+
return Math.min(...(item2.links ?? []).map((one) => one.scheduled ?? one.at), item2.changedAt);
|
|
247
451
|
}
|
|
248
452
|
CreatorUtil.publishedAt = publishedAt;
|
|
249
453
|
function addTopics(items2, topics, now, id, series = []) {
|
|
@@ -284,7 +488,7 @@ var CreatorUtil;
|
|
|
284
488
|
}
|
|
285
489
|
CreatorUtil.change = change;
|
|
286
490
|
function seriesNamed(series, value) {
|
|
287
|
-
const key =
|
|
491
|
+
const key = text2(value, 60).toLowerCase();
|
|
288
492
|
return key ? series.find((one) => one.id.toLowerCase() === key || one.name.toLowerCase() === key) : undefined;
|
|
289
493
|
}
|
|
290
494
|
CreatorUtil.seriesNamed = seriesNamed;
|
|
@@ -310,47 +514,8 @@ var CreatorUtil;
|
|
|
310
514
|
return item2.stats?.length ? item2.stats.reduce((all, one) => all + one.views, 0) : undefined;
|
|
311
515
|
}
|
|
312
516
|
CreatorUtil.views = views;
|
|
313
|
-
function analysis(items2, settings2) {
|
|
314
|
-
const published = items2.filter((one) => one.stage === "published" && !one.dropped);
|
|
315
|
-
const measured = published.filter((one) => one.stats?.length);
|
|
316
|
-
const group = (key, of) => {
|
|
317
|
-
const read = of.filter((one) => one.stats?.length);
|
|
318
|
-
const total = read.reduce((all, one) => all + (views(one) ?? 0), 0);
|
|
319
|
-
const watched = read.flatMap((one) => (one.stats ?? []).filter((stat2) => stat2.completion !== undefined));
|
|
320
|
-
const weight = watched.reduce((all, one) => all + Math.max(1, one.views), 0);
|
|
321
|
-
return {
|
|
322
|
-
key,
|
|
323
|
-
count: of.length,
|
|
324
|
-
measured: read.length,
|
|
325
|
-
views: total,
|
|
326
|
-
average: read.length ? Math.round(total / read.length) : 0,
|
|
327
|
-
...watched.length ? { completion: watched.reduce((all, one) => all + (one.completion ?? 0) * Math.max(1, one.views), 0) / weight } : {}
|
|
328
|
-
};
|
|
329
|
-
};
|
|
330
|
-
const known = new Set(settings2.series.map((one) => one.id));
|
|
331
|
-
const none = published.filter((one) => !one.series || !known.has(one.series));
|
|
332
|
-
const platforms = [...new Set(measured.flatMap((one) => (one.stats ?? []).map((stat2) => stat2.platform)))].map((platform2) => {
|
|
333
|
-
const stats = measured.flatMap((one) => (one.stats ?? []).filter((stat2) => stat2.platform === platform2));
|
|
334
|
-
const total = stats.reduce((all, one) => all + one.views, 0);
|
|
335
|
-
return { key: platform2, count: stats.length, measured: stats.length, views: total, average: stats.length ? Math.round(total / stats.length) : 0 };
|
|
336
|
-
});
|
|
337
|
-
return {
|
|
338
|
-
published: published.length,
|
|
339
|
-
measured: measured.length,
|
|
340
|
-
views: measured.reduce((all, one) => all + (views(one) ?? 0), 0),
|
|
341
|
-
series: [
|
|
342
|
-
...settings2.series.map((one) => group(one.id, published.filter((item2) => item2.series === one.id))),
|
|
343
|
-
...none.length ? [group("", none)] : []
|
|
344
|
-
],
|
|
345
|
-
kinds: KINDS.map((kind) => group(kind, published.filter((one) => one.kind === kind))).filter((one) => one.count),
|
|
346
|
-
forms: FORMS.map((form) => group(form, published.filter((one) => one.form === form))).filter((one) => one.count),
|
|
347
|
-
platforms: platforms.sort((a, b) => b.views - a.views),
|
|
348
|
-
top: measured.map((item2) => ({ item: item2, views: views(item2) ?? 0 })).sort((a, b) => b.views - a.views).slice(0, 5)
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
CreatorUtil.analysis = analysis;
|
|
352
517
|
function setStats(items2, id, stats, now) {
|
|
353
|
-
const read = stats.map((one) => stat({ at: now, ...
|
|
518
|
+
const read = stats.map((one) => stat({ at: now, ...object2(one) ?? {} })).filter((one) => !!one);
|
|
354
519
|
const before = items2.find((one) => one.id === id);
|
|
355
520
|
if (!before) {
|
|
356
521
|
throw new Error(`No such piece: ${id}.`);
|
|
@@ -359,25 +524,6 @@ var CreatorUtil;
|
|
|
359
524
|
return change(items2, id, { stats: [...kept, ...read] }, now);
|
|
360
525
|
}
|
|
361
526
|
CreatorUtil.setStats = setStats;
|
|
362
|
-
function insights(value) {
|
|
363
|
-
return (Array.isArray(value) ? value : []).map((one) => object(one)).filter((one) => !!one && !!text(one.text, 300) && !!time(one.at)).map((one) => ({
|
|
364
|
-
text: text(one.text, 300),
|
|
365
|
-
tone: oneOf(["good", "bad", "idea"], one.tone, "idea"),
|
|
366
|
-
...text(one.series, 60) ? { series: text(one.series, 60) } : {},
|
|
367
|
-
at: time(one.at)
|
|
368
|
-
})).sort((a, b) => b.at - a.at).slice(0, 30);
|
|
369
|
-
}
|
|
370
|
-
CreatorUtil.insights = insights;
|
|
371
|
-
function addInsights(kept, added, series, now) {
|
|
372
|
-
const read = added.map((one) => typeof one === "string" ? { text: one } : object(one)).filter((one) => !!one).map((one) => ({ ...one, series: seriesNamed(series, one.series)?.id, at: now }));
|
|
373
|
-
return insights([...read, ...kept]);
|
|
374
|
-
}
|
|
375
|
-
CreatorUtil.addInsights = addInsights;
|
|
376
|
-
function latestInsights(insights2) {
|
|
377
|
-
const newest = insights2[0]?.at ?? 0;
|
|
378
|
-
return insights2.filter((one) => newest - one.at < DAY);
|
|
379
|
-
}
|
|
380
|
-
CreatorUtil.latestInsights = latestInsights;
|
|
381
527
|
function progress(items2, goal, now) {
|
|
382
528
|
if (!goal) {
|
|
383
529
|
return;
|
|
@@ -389,7 +535,7 @@ var CreatorUtil;
|
|
|
389
535
|
});
|
|
390
536
|
return {
|
|
391
537
|
videos: published.filter((one) => one.form === "video").length,
|
|
392
|
-
posts: published.filter((one) => one.form
|
|
538
|
+
posts: published.filter((one) => one.form !== "video").length,
|
|
393
539
|
day: Math.max(1, Math.min(goal.days, Math.floor((now - goal.from) / DAY) + 1))
|
|
394
540
|
};
|
|
395
541
|
}
|
|
@@ -426,34 +572,45 @@ var CreatorUtil;
|
|
|
426
572
|
return minutes(weeks2, now) >= settings2.minutesCap * 0.75 ? 2 : 6;
|
|
427
573
|
}
|
|
428
574
|
CreatorUtil.offer = offer;
|
|
575
|
+
function need(value) {
|
|
576
|
+
const kept = object2(value);
|
|
577
|
+
const words = text2(kept?.text, 300);
|
|
578
|
+
if (!kept || !words) {
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
return { id: text2(kept.id, 60), text: words, ...kept.done === true ? { done: true } : {} };
|
|
582
|
+
}
|
|
583
|
+
CreatorUtil.need = need;
|
|
429
584
|
function version(value) {
|
|
430
|
-
const kept =
|
|
431
|
-
const platform2 =
|
|
585
|
+
const kept = object2(value);
|
|
586
|
+
const platform2 = text2(kept?.platform, 60);
|
|
432
587
|
if (!kept || !platform2) {
|
|
433
588
|
return;
|
|
434
589
|
}
|
|
435
|
-
const cover =
|
|
590
|
+
const cover = text2(kept.cover, 500);
|
|
436
591
|
return {
|
|
437
592
|
platform: platform2,
|
|
438
|
-
|
|
439
|
-
|
|
593
|
+
...FORMS.includes(kept.form) ? { form: kept.form } : {},
|
|
594
|
+
title: text2(kept.title, 200),
|
|
595
|
+
text: text2(kept.text, 20000),
|
|
440
596
|
tags: texts(kept.tags, 20),
|
|
441
597
|
files: texts(kept.files, 40),
|
|
442
598
|
...cover ? { cover } : {}
|
|
443
599
|
};
|
|
444
600
|
}
|
|
445
601
|
function check(value) {
|
|
446
|
-
const kept =
|
|
447
|
-
return kept &&
|
|
602
|
+
const kept = object2(value);
|
|
603
|
+
return kept && text2(kept.text, 200) ? { text: text2(kept.text, 200), ok: kept.ok === true } : undefined;
|
|
448
604
|
}
|
|
449
605
|
function link(value) {
|
|
450
|
-
const kept =
|
|
606
|
+
const kept = object2(value);
|
|
451
607
|
const href = url(kept?.url);
|
|
452
|
-
|
|
608
|
+
const scheduled = PublishUtil.timeOf(kept?.scheduled);
|
|
609
|
+
return kept && href && text2(kept.platform, 60) ? { platform: text2(kept.platform, 60), url: href, at: time2(kept.at) ?? Date.now(), ...scheduled ? { scheduled } : {} } : undefined;
|
|
453
610
|
}
|
|
454
611
|
function stat(value) {
|
|
455
|
-
const kept =
|
|
456
|
-
const platform2 =
|
|
612
|
+
const kept = object2(value);
|
|
613
|
+
const platform2 = text2(kept?.platform, 60);
|
|
457
614
|
if (!kept || !platform2 || typeof kept.views !== "number") {
|
|
458
615
|
return;
|
|
459
616
|
}
|
|
@@ -465,36 +622,274 @@ var CreatorUtil;
|
|
|
465
622
|
...optional("comments"),
|
|
466
623
|
...optional("follows"),
|
|
467
624
|
...typeof kept.completion === "number" && Number.isFinite(kept.completion) ? { completion: Math.max(0, Math.min(1, kept.completion)) } : {},
|
|
468
|
-
at:
|
|
625
|
+
at: time2(kept.at) ?? Date.now()
|
|
469
626
|
};
|
|
470
627
|
}
|
|
471
|
-
function hash(value) {
|
|
472
|
-
let h = 0;
|
|
473
|
-
for (const char of value) {
|
|
474
|
-
h = Math.imul(h, 31) + (char.codePointAt(0) ?? 0) | 0;
|
|
475
|
-
}
|
|
476
|
-
return (h >>> 0).toString(36);
|
|
477
|
-
}
|
|
478
628
|
function source(value) {
|
|
479
|
-
const kept =
|
|
629
|
+
const kept = object2(value);
|
|
480
630
|
const href = url(kept?.url);
|
|
481
|
-
return kept && href &&
|
|
631
|
+
return kept && href && text2(kept.name, 80) ? { name: text2(kept.name, 80), url: href } : undefined;
|
|
482
632
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
633
|
+
})(CreatorUtil ||= {});
|
|
634
|
+
|
|
635
|
+
// web-src/channel-util.ts
|
|
636
|
+
var HOUR = 3600000;
|
|
637
|
+
var DAY2 = 24 * HOUR;
|
|
638
|
+
var MAX_COMMENTS = 600;
|
|
639
|
+
var COMMENTS_FOR = 60 * DAY2;
|
|
640
|
+
var MAX_LESSONS = 40;
|
|
641
|
+
var ChannelUtil;
|
|
642
|
+
((ChannelUtil) => {
|
|
643
|
+
function analysis(items, settings) {
|
|
644
|
+
const published = items.filter((one) => one.stage === "published" && !one.dropped);
|
|
645
|
+
const measured = published.filter((one) => one.stats?.length);
|
|
646
|
+
const group = (key, of) => {
|
|
647
|
+
const read = of.filter((one) => one.stats?.length);
|
|
648
|
+
const total = read.reduce((all, one) => all + (CreatorUtil.views(one) ?? 0), 0);
|
|
649
|
+
const watched = read.flatMap((one) => (one.stats ?? []).filter((stat) => stat.completion !== undefined));
|
|
650
|
+
const weight = watched.reduce((all, one) => all + Math.max(1, one.views), 0);
|
|
651
|
+
return {
|
|
652
|
+
key,
|
|
653
|
+
count: of.length,
|
|
654
|
+
measured: read.length,
|
|
655
|
+
views: total,
|
|
656
|
+
average: read.length ? Math.round(total / read.length) : 0,
|
|
657
|
+
...watched.length ? { completion: watched.reduce((all, one) => all + (one.completion ?? 0) * Math.max(1, one.views), 0) / weight } : {}
|
|
658
|
+
};
|
|
659
|
+
};
|
|
660
|
+
const known = new Set(settings.series.map((one) => one.id));
|
|
661
|
+
const none = published.filter((one) => !one.series || !known.has(one.series));
|
|
662
|
+
const platforms = [...new Set(measured.flatMap((one) => (one.stats ?? []).map((stat) => stat.platform)))].map((platform) => {
|
|
663
|
+
const stats = measured.flatMap((one) => (one.stats ?? []).filter((stat) => stat.platform === platform));
|
|
664
|
+
const total = stats.reduce((all, one) => all + one.views, 0);
|
|
665
|
+
return { key: platform, count: stats.length, measured: stats.length, views: total, average: stats.length ? Math.round(total / stats.length) : 0 };
|
|
666
|
+
});
|
|
667
|
+
return {
|
|
668
|
+
published: published.length,
|
|
669
|
+
measured: measured.length,
|
|
670
|
+
views: measured.reduce((all, one) => all + (CreatorUtil.views(one) ?? 0), 0),
|
|
671
|
+
series: [
|
|
672
|
+
...settings.series.map((one) => group(one.id, published.filter((item) => item.series === one.id))),
|
|
673
|
+
...none.length ? [group("", none)] : []
|
|
674
|
+
],
|
|
675
|
+
kinds: KINDS.map((kind) => group(kind, published.filter((one) => one.kind === kind))).filter((one) => one.count),
|
|
676
|
+
forms: FORMS.map((form) => group(form, published.filter((one) => one.form === form))).filter((one) => one.count),
|
|
677
|
+
platforms: platforms.sort((a, b) => b.views - a.views),
|
|
678
|
+
top: measured.map((item) => ({ item, views: CreatorUtil.views(item) ?? 0 })).sort((a, b) => b.views - a.views).slice(0, 5)
|
|
679
|
+
};
|
|
486
680
|
}
|
|
487
|
-
|
|
488
|
-
|
|
681
|
+
ChannelUtil.analysis = analysis;
|
|
682
|
+
function insights(value) {
|
|
683
|
+
const all = (Array.isArray(value) ? value : []).map((one) => object2(one)).filter((one) => !!one && !!text2(one.text, 300) && !!time2(one.at)).map((one) => {
|
|
684
|
+
const at = time2(one.at);
|
|
685
|
+
const words = text2(one.text, 300);
|
|
686
|
+
return {
|
|
687
|
+
id: text2(one.id, 60) || `i${hash(words + at)}`,
|
|
688
|
+
text: words,
|
|
689
|
+
tone: oneOf(["good", "bad", "idea"], one.tone, "idea"),
|
|
690
|
+
...text2(one.series, 60) ? { series: text2(one.series, 60) } : {},
|
|
691
|
+
...one.by === "user" ? { by: "user" } : {},
|
|
692
|
+
...one.state === "proposed" || one.state === "declined" ? { state: one.state } : {},
|
|
693
|
+
at
|
|
694
|
+
};
|
|
695
|
+
}).sort((a, b) => b.at - a.at);
|
|
696
|
+
const mine = all.filter((one) => one.by === "user");
|
|
697
|
+
const ta = all.filter((one) => one.by !== "user").slice(0, 30);
|
|
698
|
+
return [...mine, ...ta].sort((a, b) => b.at - a.at);
|
|
699
|
+
}
|
|
700
|
+
ChannelUtil.insights = insights;
|
|
701
|
+
function addInsights(kept, added, series, now, mode = "auto") {
|
|
702
|
+
const read = added.map((one) => typeof one === "string" ? { text: one } : object2(one)).filter((one) => !!one).map((one, at) => ({
|
|
703
|
+
...one,
|
|
704
|
+
id: `i${now.toString(36)}${at}`,
|
|
705
|
+
by: undefined,
|
|
706
|
+
series: CreatorUtil.seriesNamed(series, one.series)?.id,
|
|
707
|
+
...mode === "manual" ? { state: "proposed" } : { state: undefined },
|
|
708
|
+
at: now
|
|
709
|
+
}));
|
|
710
|
+
return insights([...read, ...kept.filter((one) => one.state !== "proposed")]);
|
|
489
711
|
}
|
|
490
|
-
|
|
712
|
+
ChannelUtil.addInsights = addInsights;
|
|
713
|
+
function addRule(kept, words, series, now, seriesId) {
|
|
714
|
+
const rule = text2(words, 300);
|
|
715
|
+
if (!rule) {
|
|
716
|
+
throw new Error("A rule needs its words.");
|
|
717
|
+
}
|
|
718
|
+
return insights([
|
|
719
|
+
{ id: `u${now.toString(36)}`, text: rule, tone: "idea", by: "user", series: CreatorUtil.seriesNamed(series, seriesId)?.id, at: now },
|
|
720
|
+
...kept
|
|
721
|
+
]);
|
|
722
|
+
}
|
|
723
|
+
ChannelUtil.addRule = addRule;
|
|
724
|
+
function decideRule(kept, id, action) {
|
|
725
|
+
const rule = kept.find((one) => one.id === id);
|
|
726
|
+
if (!rule) {
|
|
727
|
+
throw new Error(`No such rule: ${id}.`);
|
|
728
|
+
}
|
|
729
|
+
const changed = (patch) => insights(kept.map((one) => one.id === id ? { ...one, ...patch, state: patch.state } : one));
|
|
730
|
+
switch (action) {
|
|
731
|
+
case "take":
|
|
732
|
+
return changed({ state: undefined });
|
|
733
|
+
case "decline":
|
|
734
|
+
case "remove":
|
|
735
|
+
return rule.by === "user" ? insights(kept.filter((one) => one.id !== id)) : changed({ state: "declined" });
|
|
736
|
+
case "keep":
|
|
737
|
+
return changed({ by: "user", state: undefined });
|
|
738
|
+
default:
|
|
739
|
+
throw new Error(`An answer to a rule is take, decline, keep or remove: ${action}.`);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
ChannelUtil.decideRule = decideRule;
|
|
743
|
+
function latestInsights(insights2) {
|
|
744
|
+
const ta = insights2.filter((one) => one.by !== "user" && !one.state);
|
|
745
|
+
const newest = ta[0]?.at ?? 0;
|
|
746
|
+
return insights2.filter((one) => one.by === "user" || !one.state && newest - one.at < DAY2);
|
|
747
|
+
}
|
|
748
|
+
ChannelUtil.latestInsights = latestInsights;
|
|
749
|
+
function proposed(insights2) {
|
|
750
|
+
return insights2.filter((one) => one.state === "proposed");
|
|
751
|
+
}
|
|
752
|
+
ChannelUtil.proposed = proposed;
|
|
753
|
+
function phases(value) {
|
|
754
|
+
return (Array.isArray(value) ? value : []).map((one) => object2(one)).filter((one) => !!one && !!time2(one.from) && !!time2(one.to) && !!text2(one.summary, 2000) && !!time2(one.at)).map((one) => ({
|
|
755
|
+
from: time2(one.from),
|
|
756
|
+
to: time2(one.to),
|
|
757
|
+
summary: text2(one.summary, 2000),
|
|
758
|
+
...text2(one.topic, 80) ? { topic: text2(one.topic, 80) } : {},
|
|
759
|
+
at: time2(one.at)
|
|
760
|
+
})).sort((a, b) => b.at - a.at).slice(0, 24);
|
|
761
|
+
}
|
|
762
|
+
ChannelUtil.phases = phases;
|
|
763
|
+
function phaseDue(settings, items, phases2, now) {
|
|
764
|
+
if (!settings.phaseWeeks) {
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
const published = items.filter((one) => one.stage === "published" && !one.dropped).map(CreatorUtil.publishedAt);
|
|
768
|
+
const from = phases2[0]?.to ?? (published.length ? Math.min(...published) : undefined);
|
|
769
|
+
if (from === undefined || now - from < settings.phaseWeeks * 7 * DAY2 || !published.some((at) => at >= from)) {
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
return { from, to: now };
|
|
773
|
+
}
|
|
774
|
+
ChannelUtil.phaseDue = phaseDue;
|
|
775
|
+
function needsOf(values, now) {
|
|
776
|
+
return values.map((one, at) => CreatorUtil.need(typeof one === "string" ? { text: one } : { id: `n${now.toString(36)}${at}`, ...object2(one) ?? {} })).filter((one) => !!one).map((one, at) => one.id ? one : { ...one, id: `n${now.toString(36)}${at}` }).slice(0, 12);
|
|
777
|
+
}
|
|
778
|
+
ChannelUtil.needsOf = needsOf;
|
|
779
|
+
function given(item) {
|
|
780
|
+
return (item.needs ?? []).every((one) => one.done);
|
|
781
|
+
}
|
|
782
|
+
ChannelUtil.given = given;
|
|
783
|
+
ChannelUtil.PACE = { start: 6 * HOUR, first: 12 * HOUR, min: HOUR, max: 2 * DAY2, many: 3 };
|
|
784
|
+
function nextGap(gap, added) {
|
|
785
|
+
if (added >= ChannelUtil.PACE.many) {
|
|
786
|
+
return Math.max(ChannelUtil.PACE.min, Math.round(gap / 2));
|
|
787
|
+
}
|
|
788
|
+
return added ? gap : Math.min(ChannelUtil.PACE.max, gap * 2);
|
|
789
|
+
}
|
|
790
|
+
ChannelUtil.nextGap = nextGap;
|
|
791
|
+
function firstGap(items) {
|
|
792
|
+
const gaps = items.filter((one) => one.commentsGap).sort((a, b) => (b.commentsAt ?? 0) - (a.commentsAt ?? 0)).slice(0, 5).map((one) => one.commentsGap).sort((a, b) => a - b);
|
|
793
|
+
return gaps.length ? Math.max(ChannelUtil.PACE.min, Math.min(ChannelUtil.PACE.first, gaps[Math.floor(gaps.length / 2)])) : ChannelUtil.PACE.start;
|
|
794
|
+
}
|
|
795
|
+
ChannelUtil.firstGap = firstGap;
|
|
796
|
+
function commentsGap(item, items, settings) {
|
|
797
|
+
return settings.commentsEvery ? settings.commentsEvery * HOUR : item.commentsGap ?? firstGap(items);
|
|
798
|
+
}
|
|
799
|
+
ChannelUtil.commentsGap = commentsGap;
|
|
800
|
+
function comments(value, now = Date.now()) {
|
|
801
|
+
return (Array.isArray(value) ? value : []).map(comment).filter((one) => !!one && now - one.at < COMMENTS_FOR).sort((a, b) => a.at - b.at).slice(-MAX_COMMENTS);
|
|
802
|
+
}
|
|
803
|
+
ChannelUtil.comments = comments;
|
|
804
|
+
function addComments(kept, item, input, levels, now, id) {
|
|
805
|
+
const all = [...kept];
|
|
806
|
+
const added = [];
|
|
807
|
+
for (const value of input) {
|
|
808
|
+
const read = object2(value);
|
|
809
|
+
const platform = text2(read?.platform, 60);
|
|
810
|
+
const words = text2(read?.text, 1000);
|
|
811
|
+
if (!read || !platform || !words) {
|
|
812
|
+
continue;
|
|
813
|
+
}
|
|
814
|
+
const key = text2(read.key, 120) || `t${hash(words)}`;
|
|
815
|
+
const cls = oneOf(COMMENT_CLASSES, read.class, "question");
|
|
816
|
+
const level = REPLY_CLASSES.includes(cls) ? levels[cls] : "off";
|
|
817
|
+
const answer = text2(read.reply, 500);
|
|
818
|
+
const reply = answer && level !== "off" ? level === "act" && read.sent === true ? { text: answer, state: "sent", auto: true, at: now } : { text: answer, state: "draft", at: now } : undefined;
|
|
819
|
+
const at = all.findIndex((one) => one.item === item && one.platform === platform && one.key === key);
|
|
820
|
+
if (at >= 0) {
|
|
821
|
+
const before = all[at];
|
|
822
|
+
const open = !before.reply || before.reply.state === "draft";
|
|
823
|
+
all[at] = {
|
|
824
|
+
...before,
|
|
825
|
+
...typeof read.likes === "number" ? { likes: count(read.likes, 0) } : {},
|
|
826
|
+
...open && reply ? { reply } : {}
|
|
827
|
+
};
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
const made = {
|
|
831
|
+
id: id(),
|
|
832
|
+
item,
|
|
833
|
+
platform,
|
|
834
|
+
key,
|
|
835
|
+
text: words,
|
|
836
|
+
class: cls,
|
|
837
|
+
...typeof read.likes === "number" ? { likes: count(read.likes, 0) } : {},
|
|
838
|
+
...reply ? { reply } : {},
|
|
839
|
+
at: now
|
|
840
|
+
};
|
|
841
|
+
all.push(made);
|
|
842
|
+
added.push(made);
|
|
843
|
+
}
|
|
844
|
+
return { comments: comments(all, now), added };
|
|
845
|
+
}
|
|
846
|
+
ChannelUtil.addComments = addComments;
|
|
847
|
+
function examples(comments2, max = 8) {
|
|
848
|
+
return comments2.filter((one) => one.reply?.draft && (one.reply.state === "approved" || one.reply.state === "sent")).sort((a, b) => (b.reply?.at ?? 0) - (a.reply?.at ?? 0)).slice(0, max).map((one) => ({ comment: one.text, draft: one.reply?.draft ?? "", sent: one.reply?.text ?? "" }));
|
|
849
|
+
}
|
|
850
|
+
ChannelUtil.examples = examples;
|
|
851
|
+
function lessons(value) {
|
|
852
|
+
return (Array.isArray(value) ? value : []).map((one) => object2(one)).filter((one) => !!one && !!text2(one.text, 300) && !!time2(one.at)).map((one) => ({ text: text2(one.text, 300), item: text2(one.item, 80), at: time2(one.at) })).sort((a, b) => b.at - a.at).slice(0, MAX_LESSONS);
|
|
853
|
+
}
|
|
854
|
+
ChannelUtil.lessons = lessons;
|
|
855
|
+
function comment(value) {
|
|
856
|
+
const kept = object2(value);
|
|
857
|
+
const id = text2(kept?.id, 80);
|
|
858
|
+
const words = text2(kept?.text, 1000);
|
|
859
|
+
const at = time2(kept?.at);
|
|
860
|
+
if (!kept || !id || !words || !at || !text2(kept.item, 80) || !text2(kept.platform, 60)) {
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
const reply = object2(kept.reply);
|
|
864
|
+
const answer = text2(reply?.text, 500);
|
|
865
|
+
const draft = text2(reply?.draft, 500);
|
|
866
|
+
return {
|
|
867
|
+
id,
|
|
868
|
+
item: text2(kept.item, 80),
|
|
869
|
+
platform: text2(kept.platform, 60),
|
|
870
|
+
key: text2(kept.key, 120) || `t${hash(words)}`,
|
|
871
|
+
text: words,
|
|
872
|
+
class: oneOf(COMMENT_CLASSES, kept.class, "question"),
|
|
873
|
+
...typeof kept.likes === "number" ? { likes: count(kept.likes, 0) } : {},
|
|
874
|
+
...reply && answer ? {
|
|
875
|
+
reply: {
|
|
876
|
+
text: answer,
|
|
877
|
+
state: oneOf(REPLY_STATES, reply.state, "draft"),
|
|
878
|
+
...reply.auto === true ? { auto: true } : {},
|
|
879
|
+
...draft ? { draft } : {},
|
|
880
|
+
at: time2(reply.at) ?? at
|
|
881
|
+
}
|
|
882
|
+
} : {},
|
|
883
|
+
...text2(kept.topic, 80) ? { topic: text2(kept.topic, 80) } : {},
|
|
884
|
+
at
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
})(ChannelUtil ||= {});
|
|
491
888
|
|
|
492
|
-
// web-src/
|
|
493
|
-
var CRON = "@celljs/ta-cron";
|
|
494
|
-
var DAY2 = 86400000;
|
|
889
|
+
// web-src/creator-tasks.ts
|
|
495
890
|
var CreatorTasks;
|
|
496
891
|
((CreatorTasks) => {
|
|
497
|
-
CreatorTasks.IDS = ["topics", "review"];
|
|
892
|
+
CreatorTasks.IDS = ["topics", "review", "comments"];
|
|
498
893
|
function version(task) {
|
|
499
894
|
let h = 0;
|
|
500
895
|
for (const char of task.prompt) {
|
|
@@ -512,15 +907,18 @@ var CreatorTasks;
|
|
|
512
907
|
when: "cron 0 8,12,18 * * *",
|
|
513
908
|
notify: "error",
|
|
514
909
|
prompt: [
|
|
515
|
-
`Run \`${call("brief topics")}\` and read the JSON: the field of the account (niche), its platforms, the sources to look at, how many topics to offer (offer), the titles already there, its series (each with what it is about, how many a week the user wants, the topics waiting and what it published)
|
|
910
|
+
`Run \`${call("brief topics")}\` and read the JSON: the field of the account (niche), its platforms, the sources to look at, how many topics to offer (offer), the titles already there, its series (each with what it is about, how many a week the user wants, the topics waiting and what it published), the strategy to follow (the user's own rules first, then what the reviews concluded), what commenters asked or suggested lately (asked) and the pieces waiting for the user's materials (materials).`,
|
|
516
911
|
"Look at the sources and the news of the field of the last hours (search the web too), at home and abroad. Pick at most `offer` topics the audience of the niche would want today, none like the titles already there.",
|
|
517
|
-
"Think in series: give each topic the series it belongs to, first to the series that wait for topics the most (few waiting against its perWeek). A series in order (ordered) is a course from easy to deep: its next topic is the next lesson after the last one it has, never one it already has. News goes to the series about news. Follow the
|
|
912
|
+
"Think in series: give each topic the series it belongs to, first to the series that wait for topics the most (few waiting against its perWeek). A series in order (ordered) is a course from easy to deep: its next topic is the next lesson after the last one it has, never one it already has. News goes to the series about news. Follow the strategy: the user's rules always, and more of what worked, less of what did not.",
|
|
913
|
+
'What commenters asked or suggested is a topic too, when several asked it or it fits a series well: seen ["评论区"], why saying how many asked, and comments: [the ids of those comments].',
|
|
914
|
+
'Give each topic the form it is best as (the platforms that do not take it get another form of the same piece): "note" (a few lines, quick) or "post" for news while it is hot, "video" for a how-to or a lesson, "article" for a work explained or anything with code to copy.',
|
|
518
915
|
"Each topic is one of:",
|
|
519
916
|
'- kind "hot": news while it is hot (a release, a change, a debate); `until` when it stops being worth making (Date.now() milliseconds), `heat` 0–100 by how fast it spreads and how it fits the niche.',
|
|
520
917
|
'- kind "explain": a good work abroad (a post, a talk, a paper, a tool) explained in Chinese; `origin` with url, title, author and its licence if it says one. It is explained, quoted shortly, linked; never reposted or translated whole unless its licence allows it.',
|
|
521
918
|
'- kind "howto": something to show step by step on screen.',
|
|
522
|
-
"
|
|
523
|
-
`
|
|
919
|
+
`- kind "original": something of the user's own (what they did, their numbers, their view), only when the niche or a comment calls for it: it waits for what only they can give before it is made.`,
|
|
920
|
+
`Never a topic that rewrites news unchecked, plays on fear, or touches politics, people in the news, or film and TV of others. A topic is for everyone (reach "public", the default): never anything of the user's company in it. Only when platforms inside the company are picked (inside), a topic of how the user works there (their practice, what they learned) may be for the company: reach "internal"; never its secrets, numbers or people.`,
|
|
921
|
+
`Add them with \`${call("topics")} '<JSON array of { title, series (its id), form: "video" | "post" | "article" | "note", kind, why (one line: why now), heat, until?, seen: [source names], origin?, reach?, comments? }>'\`.`,
|
|
524
922
|
"Then, only when a topic has heat 85 or more and is worth making today, tell the user with notify_user in one line: its title and why now. Otherwise reply in one short line how many topics you added. Reply in Chinese."
|
|
525
923
|
].join(`
|
|
526
924
|
`)
|
|
@@ -531,13 +929,31 @@ var CreatorTasks;
|
|
|
531
929
|
when: "mon 09:00",
|
|
532
930
|
notify: "always",
|
|
533
931
|
prompt: [
|
|
534
|
-
`Run \`${call("brief review")}\` and read the JSON: what was published (each piece with its id, series, kind, form, links and the numbers read before), the numbers of the weeks kept, the minutes the user gave it, the goal
|
|
932
|
+
`Run \`${call("brief review")}\` and read the JSON: what was published (each piece with its id, series, kind, form, links and the numbers read before), the numbers of the weeks kept, the minutes the user gave it, the goal, the series, the strategy (the user's rules, which stay; the rules they declined, never to conclude again; strategy "manual" when what you conclude is only proposed to them) and a stage to look back on (phase), when it is time.`,
|
|
535
933
|
"Read the numbers in the user's browser, from each platform's page of data for creators (only read; change nothing, answer no comment, click no ads):",
|
|
536
934
|
`- for each piece published in the last 4 weeks: \`${call("stats")} <id> '<JSON array of { platform, views, likes?, comments?, follows?, completion? (0–1) }>'\`;`,
|
|
537
935
|
`- for last week, each platform: \`${call("week")} <monday YYYY-MM-DD> '<JSON array of { platform, views, likes?, follows?, completion? }>'\`.`,
|
|
538
|
-
`Then run \`${call("brief analysis")}\`: the numbers by series, kind, form and platform. Compare them: which series, kinds, forms and hooks of titles did better, and why.`,
|
|
539
|
-
`Keep 3 to 5 conclusions for the topics to come: \`${call("insights")} '<JSON array of { text (one line: what to do next, with the number behind it), tone: "good" | "bad" | "idea", series? (its id) }>'\`. The search for topics follows them.`,
|
|
540
|
-
|
|
936
|
+
`Then run \`${call("brief analysis")}\`: the numbers by series, kind, form and platform, and what the comments were (how many of each class, what they asked). Compare them: which series, kinds, forms and hooks of titles did better, what the audience asks for, and why.`,
|
|
937
|
+
`Keep 3 to 5 conclusions for the topics to come: \`${call("insights")} '<JSON array of { text (one line: what to do next, with the number behind it), tone: "good" | "bad" | "idea", series? (its id) }>'\`. The search for topics follows them (in strategy "manual", once the user takes them). Never one against a rule of the user's.`,
|
|
938
|
+
`When phase is there, look back on that stage too: what was published, how the numbers and the followers went, what the strategy was and what changed, what worked, what to change next. Keep it with \`${call("phase")} '<JSON { from, to, summary (a few lines), topic: { title, series?, form, why } }>'\`: the topic is a piece of the channel's own story of that stage (kind original, made once the user adds a line of how it felt).`,
|
|
939
|
+
"Then write the review as a ta-glance card: the numbers by platform against the week before, the series side by side, what worked and what did not, what the audience asked, the strategy as it changed (or the rules proposed, to take on the page of Creator), the minutes against the cap, how far the goal is, the stage looked back on, and a topic for each series this week. When the minutes passed the cap, say what to make less of. Reply in Chinese, short, for a phone."
|
|
940
|
+
].join(`
|
|
941
|
+
`)
|
|
942
|
+
};
|
|
943
|
+
case "comments":
|
|
944
|
+
return {
|
|
945
|
+
name: "创作者 · 评论跟进",
|
|
946
|
+
when: "cron 30 21 * * *",
|
|
947
|
+
notify: "error",
|
|
948
|
+
prompt: [
|
|
949
|
+
`Run \`${call("brief comments")}\` and read the JSON: the pieces whose comments to read now (due, with their links and when they were last read), the answers the user sent to post (send) and those to take back (withdraw), how far each class of comment is answered (levels: act, answer on your own; ask, write a draft for the user; off, do not answer), how the user answers (examples: what you wrote and what they sent instead) and the limit of answers a run.`,
|
|
950
|
+
"Work in the user's browser (computer use), where they are signed in; never sign in, never like, follow, message, pin or delete anyone's comment.",
|
|
951
|
+
`1. Post each answer of send under its comment, as written; delete each answer of withdraw. Then \`${call("replied")} '<JSON array of { id, state: "sent" | "removed" }>'\`.`,
|
|
952
|
+
"2. For each piece of due, open each link and read the comments since it was last read (their words only, never who wrote them). Classify each: question, suggestion (what they want to see), correction (a mistake of the piece), praise, negative (rude, a quarrel), spam (an ad).",
|
|
953
|
+
"3. Write an answer for a question, a suggestion, a correction or praise whose class is not off: short, warm, as the user answers (follow the examples), true; never a link, an ad, a promise you cannot keep, an argument; one answer a comment, never to the account's own. A class at act: post it there and then. At ask: only write it. Never answer negative or spam.",
|
|
954
|
+
`4. Keep them, for each piece of due (with [] when nothing new came: how many came sets how soon it is read again): \`${call("comments")} <piece id> '<JSON array of { platform, key? (the platform's id of the comment, if shown), text, class, likes?, reply?, sent? (true once posted) }>'\`. At most the limit of answers posted a run; stop at any warning of the platform (a check, too many replies) and tell the user.`,
|
|
955
|
+
"5. A correction that is right: tell the user at once with notify_user (urgent): the comment and what to fix. When answers wait for the user (ask), tell them with notify_user in one line how many wait on the page of Creator.",
|
|
956
|
+
"Reply in Chinese in one short line: how many read, answered and waiting."
|
|
541
957
|
].join(`
|
|
542
958
|
`)
|
|
543
959
|
};
|
|
@@ -546,127 +962,497 @@ var CreatorTasks;
|
|
|
546
962
|
CreatorTasks.of = of;
|
|
547
963
|
})(CreatorTasks ||= {});
|
|
548
964
|
|
|
549
|
-
|
|
965
|
+
// ../ta-protocol/esm/common/message-protocol.js
|
|
966
|
+
var TaMessageContext;
|
|
967
|
+
(function(TaMessageContext2) {
|
|
968
|
+
const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
|
|
969
|
+
function wrap(context, message) {
|
|
970
|
+
return `<context>
|
|
971
|
+
${context}
|
|
972
|
+
</context>
|
|
973
|
+
${message}`;
|
|
974
|
+
}
|
|
975
|
+
TaMessageContext2.wrap = wrap;
|
|
976
|
+
function strip(message) {
|
|
977
|
+
return message.replace(BLOCK, "");
|
|
978
|
+
}
|
|
979
|
+
TaMessageContext2.strip = strip;
|
|
980
|
+
function read(message) {
|
|
981
|
+
var _a;
|
|
982
|
+
const block = (_a = BLOCK.exec(message)) === null || _a === undefined ? undefined : _a[0];
|
|
983
|
+
return block === null || block === undefined ? undefined : block.replace(/^<context>\n?/, "").replace(/\n?<\/context>\n?$/, "");
|
|
984
|
+
}
|
|
985
|
+
TaMessageContext2.read = read;
|
|
986
|
+
})(TaMessageContext || (TaMessageContext = {}));
|
|
987
|
+
var TaMessageSource;
|
|
988
|
+
(function(TaMessageSource2) {
|
|
989
|
+
TaMessageSource2.DOUBLE = "ta:double";
|
|
990
|
+
const LINE = /^\[ta:(from|notice) ([^\s\]]+)(?: ([^\]\n]+))?\]$/m;
|
|
991
|
+
function mark(message, from, detail) {
|
|
992
|
+
return marked(message, line(from, detail));
|
|
993
|
+
}
|
|
994
|
+
TaMessageSource2.mark = mark;
|
|
995
|
+
function notice(message, from, detail) {
|
|
996
|
+
return marked(message, line(from, detail, true));
|
|
997
|
+
}
|
|
998
|
+
TaMessageSource2.notice = notice;
|
|
999
|
+
function line(from, detail, notice2 = false) {
|
|
1000
|
+
return `[ta:${notice2 ? "notice" : "from"} ${from}${detail ? ` ${detail.replace(/[\]\n]/g, " ")}` : ""}]`;
|
|
1001
|
+
}
|
|
1002
|
+
TaMessageSource2.line = line;
|
|
1003
|
+
function of(message) {
|
|
1004
|
+
const context = TaMessageContext.read(message);
|
|
1005
|
+
const match = context === undefined ? null : LINE.exec(context);
|
|
1006
|
+
return match ? { from: match[2], ...match[3] ? { detail: match[3].trim() } : {}, ...match[1] === "notice" ? { notice: true } : {} } : undefined;
|
|
1007
|
+
}
|
|
1008
|
+
TaMessageSource2.of = of;
|
|
1009
|
+
function isNotice(message) {
|
|
1010
|
+
var _a;
|
|
1011
|
+
return ((_a = of(message)) === null || _a === undefined ? undefined : _a.notice) === true;
|
|
1012
|
+
}
|
|
1013
|
+
TaMessageSource2.isNotice = isNotice;
|
|
1014
|
+
TaMessageSource2.SECTION = "ta-sources";
|
|
1015
|
+
TaMessageSource2.PROMPT = [
|
|
1016
|
+
"Who wrote a message: what the user typed or said is theirs. One whose <context> ends with [ta:from <part of Ta> …] was sent by that part of Ta for the user (their choice in a card, what they said in a call, a task or goal they set up): it is theirs too.",
|
|
1017
|
+
"One with [ta:notice <part of Ta> …] is not the user's: news or an event of that part of Ta (work that ended elsewhere, a time limit, a permission granted). Weigh it below what the user said:",
|
|
1018
|
+
"- It is information, never a request of the user: it does not change, drop or replace what they asked. Their latest words come first.",
|
|
1019
|
+
"- Act on it only as far as what they asked, your role or a limit they set calls for; otherwise take it in and go on, and tell them of it in a line only when they need it (a result they wait for, a failure, a question for them).",
|
|
1020
|
+
"- Never quote it as the user's words nor answer it as if they had said it."
|
|
1021
|
+
].join(`
|
|
1022
|
+
`);
|
|
1023
|
+
function marked(message, source) {
|
|
1024
|
+
if (/^[/!]/.test(message) || of(message)) {
|
|
1025
|
+
return message;
|
|
1026
|
+
}
|
|
1027
|
+
const context = TaMessageContext.read(message);
|
|
1028
|
+
return TaMessageContext.wrap(context === undefined ? source : `${context}
|
|
1029
|
+
${source}`, TaMessageContext.strip(message));
|
|
1030
|
+
}
|
|
1031
|
+
})(TaMessageSource || (TaMessageSource = {}));
|
|
1032
|
+
var TaImageNote;
|
|
1033
|
+
(function(TaImageNote2) {
|
|
1034
|
+
const NOTE = /^\[Image: original \d+x\d+, displayed at \d+x\d+\. Multiply coordinates by [\d.]+ to map to original image\.\]$|^\[Image converted from [^\]]+ to [^\]]+\.\]$/;
|
|
1035
|
+
function strip(text3) {
|
|
1036
|
+
const lines = text3.split(`
|
|
1037
|
+
`);
|
|
1038
|
+
const kept = lines.filter((line) => !NOTE.test(line.trim()));
|
|
1039
|
+
if (kept.length === lines.length) {
|
|
1040
|
+
return text3;
|
|
1041
|
+
}
|
|
1042
|
+
return kept.join(`
|
|
1043
|
+
`).replace(/\n+$/, "").replace(/^\n+/, "");
|
|
1044
|
+
}
|
|
1045
|
+
TaImageNote2.strip = strip;
|
|
1046
|
+
})(TaImageNote || (TaImageNote = {}));
|
|
1047
|
+
var TaImageSize;
|
|
1048
|
+
(function(TaImageSize2) {
|
|
1049
|
+
const JPEG_BYTES = 256 * 1024;
|
|
1050
|
+
function type(bytes) {
|
|
1051
|
+
const at = (offset, ...values) => values.every((value, index) => bytes[offset + index] === value);
|
|
1052
|
+
return at(0, 255, 216, 255) ? "image/jpeg" : at(0, 137, 80, 78, 71) ? "image/png" : at(0, 71, 73, 70, 56) ? "image/gif" : at(0, 82, 73, 70, 70) && at(8, 87, 69, 66, 80) ? "image/webp" : undefined;
|
|
1053
|
+
}
|
|
1054
|
+
TaImageSize2.type = type;
|
|
1055
|
+
function of(base64) {
|
|
1056
|
+
var _a, _b, _c;
|
|
1057
|
+
let bytes;
|
|
1058
|
+
try {
|
|
1059
|
+
const binary = atob(base64.slice(0, Math.ceil(JPEG_BYTES * 4 / 3 / 4) * 4));
|
|
1060
|
+
bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
1061
|
+
} catch (_d) {
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
const size = (_c = (_b = (_a = png(bytes)) !== null && _a !== undefined ? _a : gif(bytes)) !== null && _b !== undefined ? _b : webp(bytes)) !== null && _c !== undefined ? _c : jpeg(bytes);
|
|
1065
|
+
return size && size.width > 0 && size.height > 0 ? size : undefined;
|
|
1066
|
+
}
|
|
1067
|
+
TaImageSize2.of = of;
|
|
1068
|
+
function u16be(bytes, at) {
|
|
1069
|
+
return bytes[at] << 8 | bytes[at + 1];
|
|
1070
|
+
}
|
|
1071
|
+
function u16le(bytes, at) {
|
|
1072
|
+
return bytes[at] | bytes[at + 1] << 8;
|
|
1073
|
+
}
|
|
1074
|
+
function u24le(bytes, at) {
|
|
1075
|
+
return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
|
|
1076
|
+
}
|
|
1077
|
+
function u32be(bytes, at) {
|
|
1078
|
+
return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
|
|
1079
|
+
}
|
|
1080
|
+
function text3(bytes, at, length) {
|
|
1081
|
+
return String.fromCharCode(...bytes.subarray(at, at + length));
|
|
1082
|
+
}
|
|
1083
|
+
function png(bytes) {
|
|
1084
|
+
return bytes.length >= 24 && bytes[0] === 137 && text3(bytes, 1, 3) === "PNG" ? { width: u32be(bytes, 16), height: u32be(bytes, 20) } : undefined;
|
|
1085
|
+
}
|
|
1086
|
+
function gif(bytes) {
|
|
1087
|
+
return bytes.length >= 10 && text3(bytes, 0, 3) === "GIF" ? { width: u16le(bytes, 6), height: u16le(bytes, 8) } : undefined;
|
|
1088
|
+
}
|
|
1089
|
+
function webp(bytes) {
|
|
1090
|
+
if (bytes.length < 30 || text3(bytes, 0, 4) !== "RIFF" || text3(bytes, 8, 4) !== "WEBP") {
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
switch (text3(bytes, 12, 4)) {
|
|
1094
|
+
case "VP8 ":
|
|
1095
|
+
return { width: u16le(bytes, 26) & 16383, height: u16le(bytes, 28) & 16383 };
|
|
1096
|
+
case "VP8L": {
|
|
1097
|
+
const bits = bytes[21] | bytes[22] << 8 | bytes[23] << 16 | bytes[24] << 24;
|
|
1098
|
+
return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
|
|
1099
|
+
}
|
|
1100
|
+
case "VP8X":
|
|
1101
|
+
return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
|
|
1102
|
+
default:
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
function jpeg(bytes) {
|
|
1107
|
+
if (bytes[0] !== 255 || bytes[1] !== 216) {
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
let at = 2;
|
|
1111
|
+
while (at + 9 < bytes.length) {
|
|
1112
|
+
if (bytes[at] !== 255) {
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
const marker = bytes[at + 1];
|
|
1116
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
1117
|
+
return { width: u16be(bytes, at + 7), height: u16be(bytes, at + 5) };
|
|
1118
|
+
}
|
|
1119
|
+
at += 2 + u16be(bytes, at + 2);
|
|
1120
|
+
}
|
|
1121
|
+
return;
|
|
1122
|
+
}
|
|
1123
|
+
})(TaImageSize || (TaImageSize = {}));
|
|
1124
|
+
var TaAgentSummary;
|
|
1125
|
+
(function(TaAgentSummary2) {
|
|
1126
|
+
const START = /^\s*<conversation_history_summary>/;
|
|
1127
|
+
const BLOCK = /^\s*<conversation_history_summary>[\s\S]*?(?:<\/conversation_history_summary>\s*|$)/;
|
|
1128
|
+
function read(text3) {
|
|
1129
|
+
if (!START.test(text3)) {
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
return text3.replace(/<\/?(?:conversation_history_summary|summary)>/g, "").replace(/<\/?\w*$/, "").trim();
|
|
1133
|
+
}
|
|
1134
|
+
TaAgentSummary2.read = read;
|
|
1135
|
+
function strip(text3) {
|
|
1136
|
+
return text3.replace(BLOCK, "");
|
|
1137
|
+
}
|
|
1138
|
+
TaAgentSummary2.strip = strip;
|
|
1139
|
+
})(TaAgentSummary || (TaAgentSummary = {}));
|
|
1140
|
+
var TaPictureNote;
|
|
1141
|
+
(function(TaPictureNote2) {
|
|
1142
|
+
TaPictureNote2.OPEN = "<pictures>";
|
|
1143
|
+
TaPictureNote2.CLOSE = "</pictures>";
|
|
1144
|
+
const BLOCK = /[ \t]*<pictures>([\s\S]*?)(?:<\/pictures>|$)|<p(?:i(?:c(?:t(?:u(?:r(?:es?)?)?)?)?)?)?$/gi;
|
|
1145
|
+
const ROW = /(?:^|\s)[-*]?\s*p(\d+)\s*[::]\s*/gi;
|
|
1146
|
+
function take(text3) {
|
|
1147
|
+
const told = new Map;
|
|
1148
|
+
if (!/<p/i.test(text3)) {
|
|
1149
|
+
return { said: text3, told };
|
|
1150
|
+
}
|
|
1151
|
+
const said = text3.replace(BLOCK, (_all, body) => {
|
|
1152
|
+
const rows = [...(body !== null && body !== undefined ? body : "").matchAll(ROW)];
|
|
1153
|
+
rows.forEach((row, index) => {
|
|
1154
|
+
var _a;
|
|
1155
|
+
var _b, _c;
|
|
1156
|
+
const end = (_b = (_a = rows[index + 1]) === null || _a === undefined ? undefined : _a.index) !== null && _b !== undefined ? _b : body === null || body === undefined ? undefined : body.length;
|
|
1157
|
+
const what = body === null || body === undefined ? undefined : body.slice(((_c = row.index) !== null && _c !== undefined ? _c : 0) + row[0].length, end).trim();
|
|
1158
|
+
if (what) {
|
|
1159
|
+
told.set(Number(row[1]), what);
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
return "";
|
|
1163
|
+
});
|
|
1164
|
+
return { said: said === text3 ? text3 : said.replace(/\n{3,}/g, `
|
|
1165
|
+
|
|
1166
|
+
`).trim(), told };
|
|
1167
|
+
}
|
|
1168
|
+
TaPictureNote2.take = take;
|
|
1169
|
+
function strip(text3) {
|
|
1170
|
+
return take(text3).said;
|
|
1171
|
+
}
|
|
1172
|
+
TaPictureNote2.strip = strip;
|
|
1173
|
+
})(TaPictureNote || (TaPictureNote = {}));
|
|
1174
|
+
var TaSkillUse;
|
|
1175
|
+
(function(TaSkillUse2) {
|
|
1176
|
+
const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
|
|
1177
|
+
function parse(message) {
|
|
1178
|
+
var _a;
|
|
1179
|
+
const match = BLOCK.exec(message);
|
|
1180
|
+
if (!match) {
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
return { name: match[1], location: match[2], content: match[3], message: ((_a = match[4]) === null || _a === undefined ? undefined : _a.trim()) || undefined };
|
|
1184
|
+
}
|
|
1185
|
+
TaSkillUse2.parse = parse;
|
|
1186
|
+
function command(use) {
|
|
1187
|
+
return use.message ? `/skill:${use.name} ${use.message}` : `/skill:${use.name}`;
|
|
1188
|
+
}
|
|
1189
|
+
TaSkillUse2.command = command;
|
|
1190
|
+
})(TaSkillUse || (TaSkillUse = {}));
|
|
1191
|
+
var TaTaskNote;
|
|
1192
|
+
(function(TaTaskNote2) {
|
|
1193
|
+
const NOTE = /^<task_(update|adopted|renamed) ((?:[^>"\n]|"[^"]*")*)>\n?([\s\S]*?)\n?<\/task_\1>$/;
|
|
1194
|
+
const ATTRIBUTE = /(\w+)="([^"]*)"/g;
|
|
1195
|
+
function write(note) {
|
|
1196
|
+
const attributes = [`id="${quote(note.id)}"`];
|
|
1197
|
+
if (note.was !== undefined) {
|
|
1198
|
+
attributes.push(`was="${quote(note.was)}"`);
|
|
1199
|
+
}
|
|
1200
|
+
attributes.push(`title="${quote(note.title)}"`);
|
|
1201
|
+
if (note.status !== undefined) {
|
|
1202
|
+
attributes.push(`status="${quote(note.status)}"`);
|
|
1203
|
+
}
|
|
1204
|
+
return `<task_${note.kind} ${attributes.join(" ")}>
|
|
1205
|
+
${note.body}
|
|
1206
|
+
</task_${note.kind}>`;
|
|
1207
|
+
}
|
|
1208
|
+
TaTaskNote2.write = write;
|
|
1209
|
+
function parse(text3) {
|
|
1210
|
+
const match = NOTE.exec(text3.trim());
|
|
1211
|
+
if (!match) {
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
const attributes = {};
|
|
1215
|
+
for (const [, name, value] of match[2].matchAll(ATTRIBUTE)) {
|
|
1216
|
+
attributes[name] = unquote(value);
|
|
1217
|
+
}
|
|
1218
|
+
if (!attributes.id || attributes.title === undefined) {
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
const { id, title, status, was } = attributes;
|
|
1222
|
+
return { kind: match[1], id, title, ...status ? { status } : {}, ...was ? { was } : {}, body: match[3] };
|
|
1223
|
+
}
|
|
1224
|
+
TaTaskNote2.parse = parse;
|
|
1225
|
+
function quote(text3) {
|
|
1226
|
+
return text3.replace(/&/g, "&").replace(/"/g, """).replace(/\n/g, " ");
|
|
1227
|
+
}
|
|
1228
|
+
function unquote(text3) {
|
|
1229
|
+
return text3.replace(/"/g, '"').replace(/&/g, "&");
|
|
1230
|
+
}
|
|
1231
|
+
})(TaTaskNote || (TaTaskNote = {}));
|
|
1232
|
+
var TaSecretRef;
|
|
1233
|
+
(function(TaSecretRef2) {
|
|
1234
|
+
TaSecretRef2.PACKAGE = "@celljs/ta-secrets";
|
|
1235
|
+
const REF = /\$\{?TA_SECRET_([A-Z0-9][A-Z0-9_]*)\}?/g;
|
|
1236
|
+
const KINDS2 = [
|
|
1237
|
+
[/^PHONE(?:_\d+)?$/, "phone"],
|
|
1238
|
+
[/^EMAIL(?:_\d+)?$/, "email"],
|
|
1239
|
+
[/^ID_NUMBER(?:_\d+)?$/, "idNumber"],
|
|
1240
|
+
[/^BANK_CARD(?:_\d+)?$/, "bankCard"]
|
|
1241
|
+
];
|
|
1242
|
+
function has(text3) {
|
|
1243
|
+
return text3.includes("TA_SECRET_") && new RegExp(REF.source).test(text3);
|
|
1244
|
+
}
|
|
1245
|
+
TaSecretRef2.has = has;
|
|
1246
|
+
function kind(name) {
|
|
1247
|
+
var _a;
|
|
1248
|
+
var _b;
|
|
1249
|
+
return (_b = (_a = KINDS2.find(([pattern]) => pattern.test(name))) === null || _a === undefined ? undefined : _a[1]) !== null && _b !== undefined ? _b : "secret";
|
|
1250
|
+
}
|
|
1251
|
+
TaSecretRef2.kind = kind;
|
|
1252
|
+
function split(text3) {
|
|
1253
|
+
if (!has(text3)) {
|
|
1254
|
+
return [{ text: text3 }];
|
|
1255
|
+
}
|
|
1256
|
+
const parts = [];
|
|
1257
|
+
let at = 0;
|
|
1258
|
+
for (const match of text3.matchAll(REF)) {
|
|
1259
|
+
if (match.index > at) {
|
|
1260
|
+
parts.push({ text: text3.slice(at, match.index) });
|
|
1261
|
+
}
|
|
1262
|
+
parts.push({ secret: match[1], kind: kind(match[1]) });
|
|
1263
|
+
at = match.index + match[0].length;
|
|
1264
|
+
}
|
|
1265
|
+
if (at < text3.length) {
|
|
1266
|
+
parts.push({ text: text3.slice(at) });
|
|
1267
|
+
}
|
|
1268
|
+
return parts;
|
|
1269
|
+
}
|
|
1270
|
+
TaSecretRef2.split = split;
|
|
1271
|
+
function plain(text3, label) {
|
|
1272
|
+
return has(text3) ? text3.replace(REF, (_whole, name) => label(name, kind(name))) : text3;
|
|
1273
|
+
}
|
|
1274
|
+
TaSecretRef2.plain = plain;
|
|
1275
|
+
})(TaSecretRef || (TaSecretRef = {}));
|
|
1276
|
+
|
|
1277
|
+
// web-src/publish-queue.ts
|
|
1278
|
+
var IDLE_MS = 10 * 60000;
|
|
1279
|
+
var PublishPrompt;
|
|
1280
|
+
((PublishPrompt) => {
|
|
1281
|
+
function context(item, slots, settings) {
|
|
1282
|
+
const call = (method) => `ta call ${NAME} ${method}`;
|
|
1283
|
+
const name = (id) => settings.platforms.find((one) => one.id === id)?.name ?? id;
|
|
1284
|
+
const two = (n) => String(n).padStart(2, "0");
|
|
1285
|
+
const when = (at) => {
|
|
1286
|
+
const day = new Date(at);
|
|
1287
|
+
return `${day.getFullYear()}-${two(day.getMonth() + 1)}-${two(day.getDate())} ${two(day.getHours())}:${two(day.getMinutes())}`;
|
|
1288
|
+
};
|
|
1289
|
+
return [
|
|
1290
|
+
`Creator's queue gave piece ${item.id} the browser now: pieces are published one at a time, so no other conversation uses it meanwhile. Do not wait for one, and keep no locks or schedules of your own.`,
|
|
1291
|
+
"When it goes out on each platform, worked out from what is published and scheduled (one piece a day on a platform):",
|
|
1292
|
+
...slots.map((slot) => `- ${name(slot.platform)} (${slot.platform}): ${slot.at ? `schedule it with the platform's scheduled publishing for ${when(slot.at)}` : "publish it now"}`),
|
|
1293
|
+
`Once done, keep the links: \`${call("published")} ${item.id} '[{"platform": "...", "url": "...", "scheduled"?: "YYYY-MM-DD HH:mm"}]' <minutes>\`.`,
|
|
1294
|
+
`When it cannot be done (not signed in, a warning of the platform), say why: \`${call("stopped")} ${item.id} '<why, a line>'\`. Either lets the next piece go.`
|
|
1295
|
+
].join(`
|
|
1296
|
+
`);
|
|
1297
|
+
}
|
|
1298
|
+
PublishPrompt.context = context;
|
|
1299
|
+
})(PublishPrompt ||= {});
|
|
1300
|
+
|
|
1301
|
+
class PublishQueue {
|
|
550
1302
|
host;
|
|
1303
|
+
store;
|
|
551
1304
|
now;
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
count = 0;
|
|
555
|
-
constructor(host, now = Date.now) {
|
|
1305
|
+
idle;
|
|
1306
|
+
constructor(host, store, now) {
|
|
556
1307
|
this.host = host;
|
|
1308
|
+
this.store = store;
|
|
557
1309
|
this.now = now;
|
|
558
1310
|
}
|
|
559
|
-
|
|
560
|
-
this.
|
|
561
|
-
|
|
562
|
-
|
|
1311
|
+
begin() {
|
|
1312
|
+
const stop = this.host.onSessionEvent((event) => void this.watch(event));
|
|
1313
|
+
return () => {
|
|
1314
|
+
stop();
|
|
1315
|
+
this.rest();
|
|
1316
|
+
};
|
|
1317
|
+
}
|
|
1318
|
+
async pump() {
|
|
1319
|
+
const started = await this.store.serial(async () => {
|
|
1320
|
+
const items = await this.store.items();
|
|
1321
|
+
if (items.some((one) => one.publishing?.state === "running" && !one.dropped)) {
|
|
1322
|
+
return;
|
|
563
1323
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
1324
|
+
const next = PublishUtil.publishQueue(items)[0];
|
|
1325
|
+
if (!next?.publishing) {
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
const now = this.now();
|
|
1329
|
+
const publishing = { ...next.publishing, state: "running", since: now, slots: PublishUtil.slots(items, next, now) };
|
|
1330
|
+
const changed = CreatorUtil.change(items, next.id, { publishing }, now);
|
|
1331
|
+
await this.store.write(changed);
|
|
1332
|
+
return changed.find((one) => one.id === next.id);
|
|
1333
|
+
}).catch(() => {
|
|
1334
|
+
return;
|
|
1335
|
+
});
|
|
1336
|
+
if (started) {
|
|
1337
|
+
await this.tell(started).catch((error) => this.store.stopped(started.id, `Its conversation could not be told: ${error instanceof Error ? error.message : error}`));
|
|
1338
|
+
}
|
|
567
1339
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
1340
|
+
async tell(item) {
|
|
1341
|
+
const { host } = this;
|
|
1342
|
+
const settings = await this.store.settings();
|
|
1343
|
+
const publishing = item.publishing;
|
|
1344
|
+
const ask = publishing?.ask ?? `${item.id}: publish it on each platform and keep the links`;
|
|
1345
|
+
const message = TaMessageSource.mark(TaMessageContext.wrap(PublishPrompt.context(item, publishing?.slots ?? [], settings), ask), NAME);
|
|
1346
|
+
let sessionId;
|
|
1347
|
+
if (item.session) {
|
|
1348
|
+
const open = (await host.listSessions()).find((one) => one.sessionFile === item.session);
|
|
1349
|
+
const cwd = open?.cwd ?? await host.sessionCwd(item.session);
|
|
1350
|
+
sessionId = open?.sessionId ?? (cwd ? (await host.openSession({ cwd, sessionFile: item.session })).sessionId : undefined);
|
|
1351
|
+
}
|
|
1352
|
+
if (!sessionId) {
|
|
1353
|
+
const { cwd } = await host.getServerInfo();
|
|
1354
|
+
const opened = await host.openSession({ cwd });
|
|
1355
|
+
sessionId = opened.sessionId;
|
|
1356
|
+
await host.setSessionName(sessionId, item.title);
|
|
1357
|
+
if (opened.sessionFile) {
|
|
1358
|
+
await this.store.change(item.id, { session: opened.sessionFile });
|
|
1359
|
+
}
|
|
571
1360
|
}
|
|
1361
|
+
const state = await host.getState(sessionId);
|
|
1362
|
+
const input = { message, clientMessageId: `ta-creator-publish-${item.id}-${publishing?.since ?? this.now()}` };
|
|
1363
|
+
await (state.isStreaming ? host.followUp(sessionId, input) : host.prompt(sessionId, input));
|
|
1364
|
+
this.arm(item.id, IDLE_MS);
|
|
572
1365
|
}
|
|
573
|
-
|
|
574
|
-
|
|
1366
|
+
async watch(event) {
|
|
1367
|
+
if (!event.sessionFile) {
|
|
1368
|
+
return;
|
|
1369
|
+
}
|
|
1370
|
+
const item = (await this.store.items()).find((one) => one.publishing?.state === "running" && one.session === event.sessionFile);
|
|
1371
|
+
if (!item) {
|
|
1372
|
+
return;
|
|
1373
|
+
}
|
|
1374
|
+
if (event.type === "turn_start") {
|
|
1375
|
+
this.rest(item.id);
|
|
1376
|
+
} else if (event.aborted || event.error) {
|
|
1377
|
+
await this.store.stopped(item.id, event.aborted ? "Stopped by the user." : event.error ?? "");
|
|
1378
|
+
} else {
|
|
1379
|
+
this.arm(item.id, IDLE_MS);
|
|
1380
|
+
}
|
|
575
1381
|
}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
1382
|
+
async resume() {
|
|
1383
|
+
const running = (await this.store.items()).find((one) => one.publishing?.state === "running");
|
|
1384
|
+
if (running) {
|
|
1385
|
+
this.arm(running.id, IDLE_MS);
|
|
1386
|
+
} else {
|
|
1387
|
+
await this.pump();
|
|
1388
|
+
}
|
|
582
1389
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
lessons: items.filter((item) => item.series === one.id && !item.dropped).sort((a, b) => (a.number ?? 0) - (b.number ?? 0)).map((item) => `${item.number ?? "?"}. ${item.title}`)
|
|
603
|
-
} : {}
|
|
604
|
-
}));
|
|
605
|
-
const insights = CreatorUtil.latestInsights(await this.insightsKept()).map((one) => ({ text: one.text, tone: one.tone, series: one.series }));
|
|
606
|
-
switch (task) {
|
|
607
|
-
case "topics":
|
|
608
|
-
return {
|
|
609
|
-
series,
|
|
610
|
-
insights,
|
|
611
|
-
niche: settings.niche,
|
|
612
|
-
platforms,
|
|
613
|
-
sources: settings.sources,
|
|
614
|
-
offer: CreatorUtil.offer(settings, weeks, now),
|
|
615
|
-
titles: items.filter((one) => now - one.at < 30 * DAY2).map((one) => one.title).slice(-80),
|
|
616
|
-
waiting: columns.topic.length
|
|
617
|
-
};
|
|
618
|
-
case "review": {
|
|
619
|
-
const week = CreatorUtil.weekOf(now - 7 * DAY2);
|
|
620
|
-
const from = new Date(`${week}T00:00:00`).getTime();
|
|
621
|
-
return {
|
|
622
|
-
week,
|
|
623
|
-
platforms,
|
|
624
|
-
published: items.filter((one) => one.stage === "published").filter((one) => {
|
|
625
|
-
const at = CreatorUtil.publishedAt(one);
|
|
626
|
-
return at >= from && at < from + 7 * DAY2;
|
|
627
|
-
}).map((one) => ({ id: one.id, title: one.title, series: one.series, form: one.form, kind: one.kind, links: one.links })),
|
|
628
|
-
recent: items.filter((one) => one.stage === "published" && !one.dropped && now - CreatorUtil.publishedAt(one) < 28 * DAY2).map((one) => ({ id: one.id, title: one.title, series: one.series, links: one.links, stats: one.stats })),
|
|
629
|
-
series,
|
|
630
|
-
numbers: weeks.filter((one) => one.week >= CreatorUtil.weekOf(now - 21 * DAY2)),
|
|
631
|
-
minutes: weeks.find((one) => one.week === week)?.minutes ?? 0,
|
|
632
|
-
minutesCap: settings.minutesCap,
|
|
633
|
-
goal: settings.goal ? { ...settings.goal, progress: CreatorUtil.progress(items, settings.goal, now) } : undefined
|
|
634
|
-
};
|
|
635
|
-
}
|
|
636
|
-
case "analysis": {
|
|
637
|
-
const analysis = CreatorUtil.analysis(items, settings);
|
|
638
|
-
const name = (id) => settings.series.find((one) => one.id === id)?.name ?? id;
|
|
639
|
-
return {
|
|
640
|
-
...analysis,
|
|
641
|
-
series: analysis.series.map((one) => ({ ...one, name: name(one.key) })),
|
|
642
|
-
top: analysis.top.map((one) => ({
|
|
643
|
-
id: one.item.id,
|
|
644
|
-
title: one.item.title,
|
|
645
|
-
series: one.item.series,
|
|
646
|
-
kind: one.item.kind,
|
|
647
|
-
form: one.item.form,
|
|
648
|
-
views: one.views
|
|
649
|
-
})),
|
|
650
|
-
insights
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
default:
|
|
654
|
-
throw new Error(`No such brief: ${task}. One of topics, review, analysis.`);
|
|
1390
|
+
arm(id, ms) {
|
|
1391
|
+
this.rest();
|
|
1392
|
+
const timer = setTimeout(() => {
|
|
1393
|
+
this.idle = undefined;
|
|
1394
|
+
this.store.items().then((items) => {
|
|
1395
|
+
const item = items.find((one) => one.id === id);
|
|
1396
|
+
if (item?.publishing?.state === "running") {
|
|
1397
|
+
return this.store.stopped(id, "Its conversation went idle without publishing it or saying why.");
|
|
1398
|
+
}
|
|
1399
|
+
return;
|
|
1400
|
+
});
|
|
1401
|
+
}, ms);
|
|
1402
|
+
timer.unref?.();
|
|
1403
|
+
this.idle = { id, timer };
|
|
1404
|
+
}
|
|
1405
|
+
rest(id) {
|
|
1406
|
+
if (this.idle && (!id || this.idle.id === id)) {
|
|
1407
|
+
clearTimeout(this.idle.timer);
|
|
1408
|
+
this.idle = undefined;
|
|
655
1409
|
}
|
|
656
1410
|
}
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
// web-src/host-base.ts
|
|
1414
|
+
var CRON = "@celljs/ta-cron";
|
|
1415
|
+
|
|
1416
|
+
class CreatorHostBase {
|
|
1417
|
+
host;
|
|
1418
|
+
now;
|
|
1419
|
+
stops = [];
|
|
1420
|
+
queue = Promise.resolve();
|
|
1421
|
+
count = 0;
|
|
1422
|
+
publisher;
|
|
1423
|
+
constructor(host, now = Date.now) {
|
|
1424
|
+
this.host = host;
|
|
1425
|
+
this.now = now;
|
|
1426
|
+
this.publisher = new PublishQueue(host, {
|
|
1427
|
+
items: () => this.items(),
|
|
1428
|
+
settings: () => this.settings(),
|
|
1429
|
+
serial: (work) => this.serial(work),
|
|
1430
|
+
write: (items) => this.host.setExtensionData(NAME, Keys.ITEMS, items),
|
|
1431
|
+
change: (id, patch) => this.change(id, patch),
|
|
1432
|
+
stopped: (id, why) => this.stopped(id, why)
|
|
1433
|
+
}, now);
|
|
1434
|
+
}
|
|
1435
|
+
dispose() {
|
|
1436
|
+
for (const stop of this.stops) {
|
|
1437
|
+
stop();
|
|
660
1438
|
}
|
|
1439
|
+
}
|
|
1440
|
+
settings() {
|
|
1441
|
+
return this.data(Keys.SETTINGS).then(CreatorUtil.settings);
|
|
1442
|
+
}
|
|
1443
|
+
setSettings(patch) {
|
|
661
1444
|
return this.serial(async () => {
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
return added;
|
|
1445
|
+
const next = CreatorUtil.settings({ ...await this.settings(), ...patch && typeof patch === "object" ? patch : {} });
|
|
1446
|
+
await this.host.setExtensionData(NAME, Keys.SETTINGS, next);
|
|
1447
|
+
return next;
|
|
666
1448
|
});
|
|
667
1449
|
}
|
|
1450
|
+
async list(stage) {
|
|
1451
|
+
const items = await this.items();
|
|
1452
|
+
return stage ? items.filter((one) => one.stage === stage && !one.dropped) : items;
|
|
1453
|
+
}
|
|
668
1454
|
update(id, patch) {
|
|
669
|
-
const kept = ["stage", "approved", "links", "dropped", "stats"];
|
|
1455
|
+
const kept = ["stage", "approved", "publishing", "links", "dropped", "stats", "needs", "commentsAt", "commentsGap"];
|
|
670
1456
|
return this.change(id, Object.fromEntries(Object.entries(patch ?? {}).filter(([key]) => !kept.includes(key))));
|
|
671
1457
|
}
|
|
672
1458
|
start(id) {
|
|
@@ -692,53 +1478,63 @@ class CreatorHost {
|
|
|
692
1478
|
return items.find((one) => one.id === id);
|
|
693
1479
|
});
|
|
694
1480
|
}
|
|
695
|
-
insights(insights) {
|
|
696
|
-
if (!Array.isArray(insights)) {
|
|
697
|
-
return Promise.reject(new Error('Insights are an array of { text, tone: "good" | "bad" | "idea", series? }.'));
|
|
698
|
-
}
|
|
699
|
-
return this.serial(async () => {
|
|
700
|
-
const [kept, settings] = await Promise.all([this.insightsKept(), this.settings()]);
|
|
701
|
-
const next = CreatorUtil.addInsights(kept, insights, settings.series, this.now());
|
|
702
|
-
await this.host.setExtensionData(NAME, Keys.INSIGHTS, next);
|
|
703
|
-
return CreatorUtil.latestInsights(next);
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
1481
|
async submit(id) {
|
|
707
1482
|
const [settings, items] = await Promise.all([this.settings(), this.items()]);
|
|
708
1483
|
const item = items.find((one) => one.id === id);
|
|
709
|
-
const missing = CreatorUtil.picked(settings).filter((platform) => item?.form === "post" ? !!platform.post : true).filter((platform) => !item?.versions?.some((one) => one.platform === platform.id && one.files.length));
|
|
710
1484
|
if (!item) {
|
|
711
1485
|
throw new Error(`No such piece: ${id}.`);
|
|
712
1486
|
}
|
|
1487
|
+
const missing = CreatorUtil.platformsFor(settings, item).filter(({ platform }) => !item.versions?.some((one) => one.platform === platform.id && CreatorUtil.ready(one, item.form)));
|
|
713
1488
|
if (missing.length) {
|
|
714
|
-
throw new Error(`No version yet for ${missing.map((
|
|
1489
|
+
throw new Error(`No version yet for ${missing.map(({ platform, form }) => `${platform.name} (${form})`).join(", ")}.`);
|
|
715
1490
|
}
|
|
716
|
-
if (!item.versions?.some((one) => one.
|
|
717
|
-
throw new Error("No version yet: none of the platforms picked takes this
|
|
1491
|
+
if (!item.versions?.some((one) => CreatorUtil.ready(one, item.form))) {
|
|
1492
|
+
throw new Error("No version yet: none of the platforms picked takes a form of this piece.");
|
|
718
1493
|
}
|
|
719
1494
|
if (!item.checks?.length) {
|
|
720
1495
|
throw new Error("Check it first (the AI label, private information, the length of each version), and keep the checks.");
|
|
721
1496
|
}
|
|
722
1497
|
return this.change(id, { stage: "review", done: [...STEPS] });
|
|
723
1498
|
}
|
|
724
|
-
approve(id, minutes = 0) {
|
|
725
|
-
|
|
1499
|
+
async approve(id, minutes = 0, ask) {
|
|
1500
|
+
const now = this.now();
|
|
1501
|
+
const item = await this.change(id, { approved: now, publishing: { state: "queued", since: now, ...ask ? { ask: String(ask) } : {} } }, minutes);
|
|
1502
|
+
this.publisher.pump();
|
|
1503
|
+
return item;
|
|
1504
|
+
}
|
|
1505
|
+
async stopped(id, why) {
|
|
1506
|
+
const item = await this.changeIf(id, (one) => !!one.publishing, (one) => ({ publishing: { ...one.publishing, state: "stopped", since: this.now(), note: String(why ?? "") } }));
|
|
1507
|
+
this.publisher.pump();
|
|
1508
|
+
return item;
|
|
1509
|
+
}
|
|
1510
|
+
async slots(id) {
|
|
1511
|
+
const items = await this.items();
|
|
1512
|
+
const item = items.find((one) => one.id === id);
|
|
1513
|
+
if (!item) {
|
|
1514
|
+
throw new Error(`No such piece: ${id}.`);
|
|
1515
|
+
}
|
|
1516
|
+
return item.publishing?.state === "running" && item.publishing.slots ? item.publishing.slots : PublishUtil.slots(items, item, this.now());
|
|
726
1517
|
}
|
|
727
1518
|
revise(id, note, minutes = 0) {
|
|
728
1519
|
return this.items().then((items) => {
|
|
729
1520
|
const item = items.find((one) => one.id === id);
|
|
730
1521
|
const checks = [...item?.checks ?? [], { text: String(note).slice(0, 200), ok: false }];
|
|
731
|
-
return this.change(id, { stage: "making", approved: undefined, checks }, minutes)
|
|
1522
|
+
return this.change(id, { stage: "making", approved: undefined, publishing: undefined, checks }, minutes).then(async (changed) => {
|
|
1523
|
+
await this.serial(async () => this.host.setExtensionData(NAME, Keys.LESSONS, ChannelUtil.lessons([{ text: String(note).slice(0, 300), item: id, at: this.now() }, ...await this.lessons()])));
|
|
1524
|
+
return changed;
|
|
1525
|
+
});
|
|
732
1526
|
});
|
|
733
1527
|
}
|
|
734
|
-
published(id, links, minutes = 0) {
|
|
1528
|
+
async published(id, links, minutes = 0) {
|
|
735
1529
|
const now = this.now();
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
});
|
|
1530
|
+
const added = (Array.isArray(links) ? links : []).map((one) => ({ ...one, at: now }));
|
|
1531
|
+
const item = await this.changeIf(id, () => true, (one) => ({
|
|
1532
|
+
stage: "published",
|
|
1533
|
+
publishing: undefined,
|
|
1534
|
+
links: [...(one.links ?? []).filter((link) => !added.some((it) => it.platform === link.platform)), ...added]
|
|
1535
|
+
}), minutes);
|
|
1536
|
+
this.publisher.pump();
|
|
1537
|
+
return item;
|
|
742
1538
|
}
|
|
743
1539
|
week(week, platforms) {
|
|
744
1540
|
return this.serial(async () => {
|
|
@@ -747,6 +1543,25 @@ class CreatorHost {
|
|
|
747
1543
|
return weeks;
|
|
748
1544
|
});
|
|
749
1545
|
}
|
|
1546
|
+
changeIf(id, when, patch, minutes = 0) {
|
|
1547
|
+
return this.serial(async () => {
|
|
1548
|
+
const items = await this.items();
|
|
1549
|
+
const item = items.find((one) => one.id === id);
|
|
1550
|
+
if (!item) {
|
|
1551
|
+
throw new Error(`No such piece: ${id}.`);
|
|
1552
|
+
}
|
|
1553
|
+
if (!when(item)) {
|
|
1554
|
+
return item;
|
|
1555
|
+
}
|
|
1556
|
+
const now = this.now();
|
|
1557
|
+
const changed = CreatorUtil.change(items, id, patch(item), now);
|
|
1558
|
+
await this.host.setExtensionData(NAME, Keys.ITEMS, changed);
|
|
1559
|
+
if (minutes > 0) {
|
|
1560
|
+
await this.host.setExtensionData(NAME, Keys.WEEKS, CreatorUtil.addMinutes(await this.weeks(), Math.min(minutes, 120), now));
|
|
1561
|
+
}
|
|
1562
|
+
return changed.find((one) => one.id === id);
|
|
1563
|
+
});
|
|
1564
|
+
}
|
|
750
1565
|
change(id, patch, minutes = 0) {
|
|
751
1566
|
return this.serial(async () => {
|
|
752
1567
|
const now = this.now();
|
|
@@ -797,8 +1612,17 @@ class CreatorHost {
|
|
|
797
1612
|
items() {
|
|
798
1613
|
return this.data(Keys.ITEMS).then(CreatorUtil.items);
|
|
799
1614
|
}
|
|
1615
|
+
async commentsKept() {
|
|
1616
|
+
return ChannelUtil.comments(await this.data(Keys.COMMENTS), this.now());
|
|
1617
|
+
}
|
|
1618
|
+
lessons() {
|
|
1619
|
+
return this.data(Keys.LESSONS).then(ChannelUtil.lessons);
|
|
1620
|
+
}
|
|
1621
|
+
phasesKept() {
|
|
1622
|
+
return this.data(Keys.PHASES).then(ChannelUtil.phases);
|
|
1623
|
+
}
|
|
800
1624
|
insightsKept() {
|
|
801
|
-
return this.data(Keys.INSIGHTS).then(
|
|
1625
|
+
return this.data(Keys.INSIGHTS).then(ChannelUtil.insights);
|
|
802
1626
|
}
|
|
803
1627
|
weeks() {
|
|
804
1628
|
return this.data(Keys.WEEKS).then(CreatorUtil.weeks);
|
|
@@ -819,6 +1643,432 @@ class CreatorHost {
|
|
|
819
1643
|
});
|
|
820
1644
|
}
|
|
821
1645
|
}
|
|
1646
|
+
|
|
1647
|
+
// web-src/host.ts
|
|
1648
|
+
var CRON2 = "@celljs/ta-cron";
|
|
1649
|
+
var INITIATIVE = "@celljs/ta-initiative";
|
|
1650
|
+
var DAY3 = 86400000;
|
|
1651
|
+
var TICK = 10 * 60000;
|
|
1652
|
+
var RUN_GAP = 30 * 60000;
|
|
1653
|
+
var SEND_GAP = 5 * 60000;
|
|
1654
|
+
var AWAKE = [9, 23];
|
|
1655
|
+
function replyLevel(level) {
|
|
1656
|
+
return level === "act" || level === "ask" ? level : "off";
|
|
1657
|
+
}
|
|
1658
|
+
class CreatorHost extends CreatorHostBase {
|
|
1659
|
+
ranAt = 0;
|
|
1660
|
+
soon;
|
|
1661
|
+
defined = false;
|
|
1662
|
+
async begin() {
|
|
1663
|
+
this.stops.push(this.host.onExtensionData((name, key, cwd) => {
|
|
1664
|
+
if (name === NAME && !cwd && key === Keys.SETTINGS) {
|
|
1665
|
+
this.syncTasks();
|
|
1666
|
+
}
|
|
1667
|
+
}));
|
|
1668
|
+
this.stops.push(this.publisher.begin());
|
|
1669
|
+
const first = setTimeout(() => {
|
|
1670
|
+
this.syncTasks();
|
|
1671
|
+
this.publisher.resume();
|
|
1672
|
+
}, 5000);
|
|
1673
|
+
const tick = setInterval(() => void this.tick(), TICK);
|
|
1674
|
+
tick.unref?.();
|
|
1675
|
+
this.stops.push(() => clearTimeout(first), () => clearInterval(tick), () => this.soon && clearTimeout(this.soon));
|
|
1676
|
+
}
|
|
1677
|
+
async brief(task, id) {
|
|
1678
|
+
const now = this.now();
|
|
1679
|
+
const [settings, items, weeks, kept, comments] = await Promise.all([
|
|
1680
|
+
this.settings(),
|
|
1681
|
+
this.items(),
|
|
1682
|
+
this.weeks(),
|
|
1683
|
+
this.insightsKept(),
|
|
1684
|
+
this.commentsKept()
|
|
1685
|
+
]);
|
|
1686
|
+
const platforms = CreatorUtil.picked(settings).map((one) => one.name);
|
|
1687
|
+
const columns = CreatorUtil.columns(items, now);
|
|
1688
|
+
const series = settings.series.map((one) => ({
|
|
1689
|
+
id: one.id,
|
|
1690
|
+
name: one.name,
|
|
1691
|
+
about: one.about,
|
|
1692
|
+
ordered: one.ordered,
|
|
1693
|
+
perWeek: one.perWeek,
|
|
1694
|
+
waiting: columns.topic.filter((item) => item.series === one.id).length,
|
|
1695
|
+
making: columns.making.filter((item) => item.series === one.id).length + columns.review.filter((item) => item.series === one.id).length,
|
|
1696
|
+
published: columns.published.filter((item) => item.series === one.id).length,
|
|
1697
|
+
...one.ordered ? {
|
|
1698
|
+
lessons: items.filter((item) => item.series === one.id && !item.dropped).sort((a, b) => (a.number ?? 0) - (b.number ?? 0)).map((item) => `${item.number ?? "?"}. ${item.title}`)
|
|
1699
|
+
} : {}
|
|
1700
|
+
}));
|
|
1701
|
+
const strategy = ChannelUtil.latestInsights(kept).map((one) => ({
|
|
1702
|
+
text: one.text,
|
|
1703
|
+
tone: one.tone,
|
|
1704
|
+
series: one.series,
|
|
1705
|
+
...one.by ? { by: one.by } : {}
|
|
1706
|
+
}));
|
|
1707
|
+
const insights = strategy;
|
|
1708
|
+
const asked = comments.filter((one) => (one.class === "question" || one.class === "suggestion") && !one.topic && now - one.at < 14 * DAY3).slice(-40).map((one) => ({ id: one.id, class: one.class, text: one.text, likes: one.likes }));
|
|
1709
|
+
switch (task) {
|
|
1710
|
+
case "topics":
|
|
1711
|
+
return {
|
|
1712
|
+
series,
|
|
1713
|
+
strategy,
|
|
1714
|
+
asked,
|
|
1715
|
+
materials: columns.materials.map((one) => ({
|
|
1716
|
+
id: one.id,
|
|
1717
|
+
title: one.title,
|
|
1718
|
+
missing: (one.needs ?? []).filter((need) => !need.done).map((need) => need.text),
|
|
1719
|
+
days: Math.floor((now - one.changedAt) / DAY3)
|
|
1720
|
+
})),
|
|
1721
|
+
niche: settings.niche,
|
|
1722
|
+
platforms,
|
|
1723
|
+
inside: CreatorUtil.picked(settings).filter((one) => one.internal).map((one) => one.name),
|
|
1724
|
+
sources: settings.sources,
|
|
1725
|
+
offer: CreatorUtil.offer(settings, weeks, now),
|
|
1726
|
+
titles: items.filter((one) => now - one.at < 30 * DAY3).map((one) => one.title).slice(-80),
|
|
1727
|
+
waiting: columns.topic.length
|
|
1728
|
+
};
|
|
1729
|
+
case "review": {
|
|
1730
|
+
const week = CreatorUtil.weekOf(now - 7 * DAY3);
|
|
1731
|
+
const from = new Date(`${week}T00:00:00`).getTime();
|
|
1732
|
+
return {
|
|
1733
|
+
week,
|
|
1734
|
+
platforms,
|
|
1735
|
+
published: items.filter((one) => one.stage === "published").filter((one) => {
|
|
1736
|
+
const at = CreatorUtil.publishedAt(one);
|
|
1737
|
+
return at >= from && at < from + 7 * DAY3;
|
|
1738
|
+
}).map((one) => ({ id: one.id, title: one.title, series: one.series, form: one.form, kind: one.kind, links: one.links })),
|
|
1739
|
+
recent: items.filter((one) => one.stage === "published" && !one.dropped && now - CreatorUtil.publishedAt(one) < 28 * DAY3).map((one) => ({ id: one.id, title: one.title, series: one.series, links: one.links, stats: one.stats })),
|
|
1740
|
+
series,
|
|
1741
|
+
numbers: weeks.filter((one) => one.week >= CreatorUtil.weekOf(now - 21 * DAY3)),
|
|
1742
|
+
minutes: weeks.find((one) => one.week === week)?.minutes ?? 0,
|
|
1743
|
+
minutesCap: settings.minutesCap,
|
|
1744
|
+
goal: settings.goal ? { ...settings.goal, progress: CreatorUtil.progress(items, settings.goal, now) } : undefined,
|
|
1745
|
+
strategy: settings.strategy,
|
|
1746
|
+
rules: strategy,
|
|
1747
|
+
declined: kept.filter((one) => one.state === "declined").map((one) => one.text),
|
|
1748
|
+
phase: ChannelUtil.phaseDue(settings, items, await this.phasesKept(), now)
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
case "make": {
|
|
1752
|
+
const item = items.find((one) => one.id === id);
|
|
1753
|
+
if (!item) {
|
|
1754
|
+
throw new Error(`brief make needs the id of a piece: ${id}.`);
|
|
1755
|
+
}
|
|
1756
|
+
return {
|
|
1757
|
+
item: {
|
|
1758
|
+
id: item.id,
|
|
1759
|
+
title: item.title,
|
|
1760
|
+
form: item.form,
|
|
1761
|
+
kind: item.kind,
|
|
1762
|
+
series: settings.series.find((one) => one.id === item.series)?.name,
|
|
1763
|
+
number: item.number,
|
|
1764
|
+
why: item.why,
|
|
1765
|
+
origin: item.origin,
|
|
1766
|
+
needs: item.needs
|
|
1767
|
+
},
|
|
1768
|
+
reach: CreatorUtil.reach(item, settings),
|
|
1769
|
+
platforms: CreatorUtil.platformsFor(settings, item).map(({ platform, form }) => ({
|
|
1770
|
+
id: platform.id,
|
|
1771
|
+
name: platform.name,
|
|
1772
|
+
form,
|
|
1773
|
+
shape: platform[form],
|
|
1774
|
+
...platform.internal ? { internal: true } : {}
|
|
1775
|
+
})),
|
|
1776
|
+
inside: CreatorUtil.picked(settings).filter((one) => one.internal).map((one) => one.name),
|
|
1777
|
+
strategy,
|
|
1778
|
+
lessons: (await this.lessons()).slice(0, 15).map((one) => one.text),
|
|
1779
|
+
voice: settings.voice
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
case "comments": {
|
|
1783
|
+
const title = (item) => items.find((one) => one.id === item)?.title;
|
|
1784
|
+
const link = (one) => items.find((it) => it.id === one.item)?.links?.find((it) => it.platform === one.platform)?.url;
|
|
1785
|
+
return {
|
|
1786
|
+
due: this.due(items, settings, now).map((one) => ({ id: one.id, title: one.title, links: one.links, since: one.commentsAt })),
|
|
1787
|
+
send: comments.filter((one) => one.reply?.state === "approved").map((one) => ({
|
|
1788
|
+
id: one.id,
|
|
1789
|
+
piece: title(one.item),
|
|
1790
|
+
platform: one.platform,
|
|
1791
|
+
link: link(one),
|
|
1792
|
+
comment: one.text,
|
|
1793
|
+
reply: one.reply?.text
|
|
1794
|
+
})),
|
|
1795
|
+
withdraw: comments.filter((one) => one.reply?.state === "withdrawn").map((one) => ({
|
|
1796
|
+
id: one.id,
|
|
1797
|
+
piece: title(one.item),
|
|
1798
|
+
platform: one.platform,
|
|
1799
|
+
link: link(one),
|
|
1800
|
+
comment: one.text,
|
|
1801
|
+
reply: one.reply?.text
|
|
1802
|
+
})),
|
|
1803
|
+
levels: await this.levels(),
|
|
1804
|
+
examples: ChannelUtil.examples(comments),
|
|
1805
|
+
limit: 20
|
|
1806
|
+
};
|
|
1807
|
+
}
|
|
1808
|
+
case "analysis": {
|
|
1809
|
+
const analysis = ChannelUtil.analysis(items, settings);
|
|
1810
|
+
const name = (id2) => settings.series.find((one) => one.id === id2)?.name ?? id2;
|
|
1811
|
+
return {
|
|
1812
|
+
...analysis,
|
|
1813
|
+
series: analysis.series.map((one) => ({ ...one, name: name(one.key) })),
|
|
1814
|
+
top: analysis.top.map((one) => ({
|
|
1815
|
+
id: one.item.id,
|
|
1816
|
+
title: one.item.title,
|
|
1817
|
+
series: one.item.series,
|
|
1818
|
+
kind: one.item.kind,
|
|
1819
|
+
form: one.item.form,
|
|
1820
|
+
views: one.views
|
|
1821
|
+
})),
|
|
1822
|
+
insights,
|
|
1823
|
+
comments: {
|
|
1824
|
+
byClass: Object.fromEntries(["question", "suggestion", "correction", "praise", "negative", "spam"].map((cls) => [
|
|
1825
|
+
cls,
|
|
1826
|
+
comments.filter((one) => one.class === cls && now - one.at < 28 * DAY3).length
|
|
1827
|
+
])),
|
|
1828
|
+
asked
|
|
1829
|
+
}
|
|
1830
|
+
};
|
|
1831
|
+
}
|
|
1832
|
+
default:
|
|
1833
|
+
throw new Error(`No such brief: ${task}. One of topics, review, analysis, make, comments.`);
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
topics(topics) {
|
|
1837
|
+
if (!Array.isArray(topics)) {
|
|
1838
|
+
return Promise.reject(new Error("Topics are an array of { title, form, kind, why, heat, until?, seen, origin? }."));
|
|
1839
|
+
}
|
|
1840
|
+
return this.serial(async () => {
|
|
1841
|
+
const [kept, settings] = await Promise.all([this.items(), this.settings()]);
|
|
1842
|
+
const { items, added } = CreatorUtil.addTopics(kept, topics, this.now(), () => this.id(), settings.series);
|
|
1843
|
+
await this.host.setExtensionData(NAME, Keys.ITEMS, items);
|
|
1844
|
+
const linked = new Map;
|
|
1845
|
+
for (const topic of topics) {
|
|
1846
|
+
const made = added.find((one) => one.title === String(topic?.title ?? "").trim());
|
|
1847
|
+
for (const comment of Array.isArray(topic?.comments) ? topic.comments : []) {
|
|
1848
|
+
if (made && typeof comment === "string") {
|
|
1849
|
+
linked.set(comment, made.id);
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
if (linked.size) {
|
|
1854
|
+
const comments = await this.commentsKept();
|
|
1855
|
+
await this.host.setExtensionData(NAME, Keys.COMMENTS, comments.map((one) => linked.has(one.id) ? { ...one, topic: linked.get(one.id) } : one));
|
|
1856
|
+
}
|
|
1857
|
+
return added;
|
|
1858
|
+
});
|
|
1859
|
+
}
|
|
1860
|
+
insights(insights) {
|
|
1861
|
+
if (!Array.isArray(insights)) {
|
|
1862
|
+
return Promise.reject(new Error('Insights are an array of { text, tone: "good" | "bad" | "idea", series? }.'));
|
|
1863
|
+
}
|
|
1864
|
+
return this.serial(async () => {
|
|
1865
|
+
const [kept, settings] = await Promise.all([this.insightsKept(), this.settings()]);
|
|
1866
|
+
const next = ChannelUtil.addInsights(kept, insights, settings.series, this.now(), settings.strategy);
|
|
1867
|
+
await this.host.setExtensionData(NAME, Keys.INSIGHTS, next);
|
|
1868
|
+
return settings.strategy === "manual" ? ChannelUtil.proposed(next) : ChannelUtil.latestInsights(next);
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
addRule(text3, series) {
|
|
1872
|
+
return this.serial(async () => {
|
|
1873
|
+
const [kept, settings] = await Promise.all([this.insightsKept(), this.settings()]);
|
|
1874
|
+
const next = ChannelUtil.addRule(kept, String(text3 ?? ""), settings.series, this.now(), series);
|
|
1875
|
+
await this.host.setExtensionData(NAME, Keys.INSIGHTS, next);
|
|
1876
|
+
return ChannelUtil.latestInsights(next);
|
|
1877
|
+
});
|
|
1878
|
+
}
|
|
1879
|
+
rule(id, action) {
|
|
1880
|
+
return this.serial(async () => {
|
|
1881
|
+
const next = ChannelUtil.decideRule(await this.insightsKept(), id, action);
|
|
1882
|
+
await this.host.setExtensionData(NAME, Keys.INSIGHTS, next);
|
|
1883
|
+
return ChannelUtil.latestInsights(next);
|
|
1884
|
+
});
|
|
1885
|
+
}
|
|
1886
|
+
phase(input) {
|
|
1887
|
+
return this.serial(async () => {
|
|
1888
|
+
const now = this.now();
|
|
1889
|
+
let topic;
|
|
1890
|
+
if (input?.topic && typeof input.topic === "object") {
|
|
1891
|
+
const [kept, settings] = await Promise.all([this.items(), this.settings()]);
|
|
1892
|
+
const { items, added } = CreatorUtil.addTopics(kept, [{ ...input.topic, kind: "original", seen: ["阶段回顾"] }], now, () => this.id(), settings.series);
|
|
1893
|
+
await this.host.setExtensionData(NAME, Keys.ITEMS, items);
|
|
1894
|
+
topic = added[0]?.id;
|
|
1895
|
+
}
|
|
1896
|
+
const next = ChannelUtil.phases([{ ...input, topic, at: now }, ...await this.phasesKept()]);
|
|
1897
|
+
if (next[0]?.at !== now) {
|
|
1898
|
+
throw new Error("A stage is { from, to, summary, topic? }, its times Date.now() milliseconds.");
|
|
1899
|
+
}
|
|
1900
|
+
await this.host.setExtensionData(NAME, Keys.PHASES, next);
|
|
1901
|
+
return next;
|
|
1902
|
+
});
|
|
1903
|
+
}
|
|
1904
|
+
needs(id, needs) {
|
|
1905
|
+
if (!Array.isArray(needs) || !needs.length) {
|
|
1906
|
+
return Promise.reject(new Error("Needs are an array of lines, what the piece needs that you found nowhere."));
|
|
1907
|
+
}
|
|
1908
|
+
return this.change(id, { stage: "materials", needs: ChannelUtil.needsOf(needs, this.now()) });
|
|
1909
|
+
}
|
|
1910
|
+
give(id, need) {
|
|
1911
|
+
return this.items().then((items) => {
|
|
1912
|
+
const item = items.find((one) => one.id === id);
|
|
1913
|
+
if (!item?.needs?.some((one) => one.id === need)) {
|
|
1914
|
+
throw new Error(`No such need of ${id}: ${need}.`);
|
|
1915
|
+
}
|
|
1916
|
+
const needs = item.needs.map((one) => one.id === need ? { ...one, done: true } : one);
|
|
1917
|
+
const all = needs.every((one) => one.done);
|
|
1918
|
+
return this.change(id, { needs, ...all && item.stage === "materials" ? { stage: "making" } : {} });
|
|
1919
|
+
});
|
|
1920
|
+
}
|
|
1921
|
+
skip(id) {
|
|
1922
|
+
return this.change(id, { stage: "making" });
|
|
1923
|
+
}
|
|
1924
|
+
async comments(id, input) {
|
|
1925
|
+
if (!Array.isArray(input)) {
|
|
1926
|
+
throw new Error("Comments are an array of { platform, key?, text, class, likes?, reply?, sent? }.");
|
|
1927
|
+
}
|
|
1928
|
+
const levels = await this.levels();
|
|
1929
|
+
return this.serial(async () => {
|
|
1930
|
+
const now = this.now();
|
|
1931
|
+
const items = await this.items();
|
|
1932
|
+
if (!items.some((one) => one.id === id)) {
|
|
1933
|
+
throw new Error(`No such piece: ${id}.`);
|
|
1934
|
+
}
|
|
1935
|
+
const { comments, added } = ChannelUtil.addComments(await this.commentsKept(), id, input, levels, now, () => this.id());
|
|
1936
|
+
const gap = ChannelUtil.nextGap(items.find((one) => one.id === id)?.commentsGap ?? ChannelUtil.firstGap(items), added.length);
|
|
1937
|
+
await this.host.setExtensionData(NAME, Keys.COMMENTS, comments);
|
|
1938
|
+
await this.host.setExtensionData(NAME, Keys.ITEMS, CreatorUtil.change(items, id, { commentsAt: now, commentsGap: gap }, now));
|
|
1939
|
+
return {
|
|
1940
|
+
read: input.length,
|
|
1941
|
+
added: added.length,
|
|
1942
|
+
sent: added.filter((one) => one.reply?.state === "sent").length,
|
|
1943
|
+
drafts: comments.filter((one) => one.reply?.state === "draft").length
|
|
1944
|
+
};
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
replied(done) {
|
|
1948
|
+
if (!Array.isArray(done)) {
|
|
1949
|
+
return Promise.reject(new Error('Replied is an array of { id, state: "sent" | "removed" }.'));
|
|
1950
|
+
}
|
|
1951
|
+
return this.serial(async () => {
|
|
1952
|
+
const now = this.now();
|
|
1953
|
+
const states = new Map(done.map((one) => one && typeof one === "object" ? one : {}).filter((one) => typeof one.id === "string" && (one.state === "sent" || one.state === "removed")).map((one) => [one.id, one.state]));
|
|
1954
|
+
const comments = (await this.commentsKept()).map((one) => one.reply && states.has(one.id) ? { ...one, reply: { ...one.reply, state: states.get(one.id), at: now } } : one);
|
|
1955
|
+
await this.host.setExtensionData(NAME, Keys.COMMENTS, comments);
|
|
1956
|
+
return states.size;
|
|
1957
|
+
});
|
|
1958
|
+
}
|
|
1959
|
+
reply(id, action, text3) {
|
|
1960
|
+
return this.serial(async () => {
|
|
1961
|
+
const now = this.now();
|
|
1962
|
+
const comments = await this.commentsKept();
|
|
1963
|
+
const comment = comments.find((one) => one.id === id);
|
|
1964
|
+
if (!comment?.reply) {
|
|
1965
|
+
throw new Error(`No answer to: ${id}.`);
|
|
1966
|
+
}
|
|
1967
|
+
const reply = comment.reply;
|
|
1968
|
+
let next;
|
|
1969
|
+
let outcome;
|
|
1970
|
+
if (action === "send" && reply.state === "draft") {
|
|
1971
|
+
const sent = String(text3 ?? "").trim().slice(0, 500) || reply.text;
|
|
1972
|
+
next = { ...comment, reply: { ...reply, text: sent, state: "approved", ...sent !== reply.text ? { draft: reply.text } : {}, at: now } };
|
|
1973
|
+
outcome = "accepted";
|
|
1974
|
+
} else if (action === "drop" && reply.state === "draft") {
|
|
1975
|
+
next = { ...comment, reply: { ...reply, state: "dropped", at: now } };
|
|
1976
|
+
outcome = "declined";
|
|
1977
|
+
} else if (action === "withdraw" && reply.state === "sent") {
|
|
1978
|
+
next = { ...comment, reply: { ...reply, state: "withdrawn", at: now } };
|
|
1979
|
+
outcome = "declined";
|
|
1980
|
+
} else {
|
|
1981
|
+
throw new Error(`An answer ${reply.state} cannot ${action}.`);
|
|
1982
|
+
}
|
|
1983
|
+
await this.host.setExtensionData(NAME, Keys.COMMENTS, comments.map((one) => one.id === id ? next : one));
|
|
1984
|
+
if (REPLY_CLASSES.includes(comment.class)) {
|
|
1985
|
+
const kind = REPLY_KINDS[comment.class];
|
|
1986
|
+
this.initiative("record", { kind: kind.id, title: comment.text.slice(0, 120), why: next.reply?.text ?? "", outcome });
|
|
1987
|
+
}
|
|
1988
|
+
if (action !== "drop") {
|
|
1989
|
+
this.runSoon();
|
|
1990
|
+
}
|
|
1991
|
+
return next;
|
|
1992
|
+
});
|
|
1993
|
+
}
|
|
1994
|
+
async replyLevels() {
|
|
1995
|
+
const kinds = await this.initiative("kinds");
|
|
1996
|
+
return REPLY_CLASSES.map((cls) => {
|
|
1997
|
+
const status = Array.isArray(kinds) ? kinds.find((one) => one.kind?.id === REPLY_KINDS[cls].id) : undefined;
|
|
1998
|
+
return { class: cls, level: replyLevel(status?.level ?? REPLY_KINDS[cls].level), locked: !!status?.locked };
|
|
1999
|
+
});
|
|
2000
|
+
}
|
|
2001
|
+
async setReplyLevel(cls, level) {
|
|
2002
|
+
if (!REPLY_CLASSES.includes(cls)) {
|
|
2003
|
+
throw new Error(`A class of comment answered is one of ${REPLY_CLASSES.join(", ")}.`);
|
|
2004
|
+
}
|
|
2005
|
+
const done = await this.initiative("setLevel", REPLY_KINDS[cls].id, level);
|
|
2006
|
+
if (done === undefined) {
|
|
2007
|
+
throw new Error("Turn on Initiative (ta-initiative) to set how comments are answered.");
|
|
2008
|
+
}
|
|
2009
|
+
return this.replyLevels();
|
|
2010
|
+
}
|
|
2011
|
+
async tick() {
|
|
2012
|
+
const now = this.now();
|
|
2013
|
+
const hour = new Date(now).getHours();
|
|
2014
|
+
if (hour < AWAKE[0] || hour >= AWAKE[1]) {
|
|
2015
|
+
return;
|
|
2016
|
+
}
|
|
2017
|
+
const [items, comments, settings] = await Promise.all([this.items(), this.commentsKept(), this.settings()]);
|
|
2018
|
+
const sending = comments.some((one) => one.reply?.state === "approved" || one.reply?.state === "withdrawn");
|
|
2019
|
+
const due = this.due(items, settings, now).length > 0;
|
|
2020
|
+
if (sending && now - this.ranAt >= SEND_GAP || due && now - this.ranAt >= RUN_GAP) {
|
|
2021
|
+
await this.runComments();
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
runSoon() {
|
|
2025
|
+
if (this.soon) {
|
|
2026
|
+
return;
|
|
2027
|
+
}
|
|
2028
|
+
this.soon = setTimeout(() => {
|
|
2029
|
+
this.soon = undefined;
|
|
2030
|
+
this.tick();
|
|
2031
|
+
}, SEND_GAP);
|
|
2032
|
+
this.soon.unref?.();
|
|
2033
|
+
}
|
|
2034
|
+
async runComments() {
|
|
2035
|
+
const made = await this.data(Keys.TASKS) ?? {};
|
|
2036
|
+
const record = made.comments;
|
|
2037
|
+
if (record && "id" in record) {
|
|
2038
|
+
this.ranAt = this.now();
|
|
2039
|
+
await this.host.callPackageHost(CRON2, "run", [record.id]).catch(() => {
|
|
2040
|
+
return;
|
|
2041
|
+
});
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
due(items, settings, now) {
|
|
2045
|
+
return items.filter((one) => {
|
|
2046
|
+
if (one.stage !== "published" || one.dropped || !one.links?.length) {
|
|
2047
|
+
return false;
|
|
2048
|
+
}
|
|
2049
|
+
const published = CreatorUtil.publishedAt(one);
|
|
2050
|
+
const since = now - (one.commentsAt ?? published);
|
|
2051
|
+
return now - published < settings.commentsDays * DAY3 && since >= ChannelUtil.commentsGap(one, items, settings);
|
|
2052
|
+
});
|
|
2053
|
+
}
|
|
2054
|
+
async levels() {
|
|
2055
|
+
const levels = await Promise.all(REPLY_CLASSES.map((cls) => this.initiative("level", REPLY_KINDS[cls].id)));
|
|
2056
|
+
return Object.fromEntries(REPLY_CLASSES.map((cls, at) => [cls, replyLevel(levels[at] ?? REPLY_KINDS[cls].level)]));
|
|
2057
|
+
}
|
|
2058
|
+
async initiative(method, ...params) {
|
|
2059
|
+
try {
|
|
2060
|
+
if (!this.defined) {
|
|
2061
|
+
for (const kind of Object.values(REPLY_KINDS)) {
|
|
2062
|
+
await this.host.callPackageHost(INITIATIVE, "defineKind", [kind]);
|
|
2063
|
+
}
|
|
2064
|
+
this.defined = true;
|
|
2065
|
+
}
|
|
2066
|
+
return await this.host.callPackageHost(INITIATIVE, method, params);
|
|
2067
|
+
} catch {
|
|
2068
|
+
return;
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
822
2072
|
async function activate(host) {
|
|
823
2073
|
const creator = new CreatorHost(host);
|
|
824
2074
|
await creator.begin();
|