@tyhld/conductor 0.12.157 → 0.12.158
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/dist/cli.js +4 -1
- package/dist/relay.js +423 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -14,7 +14,7 @@ import { existsSync, readFileSync } from 'node:fs';
|
|
|
14
14
|
import { fileURLToPath } from 'node:url';
|
|
15
15
|
import os from 'node:os';
|
|
16
16
|
import path from 'node:path';
|
|
17
|
-
import { RELAY_FETCH_TIMEOUT_MS, createRelayState, deliveryReport, parseAgentKind, reconnectDelayMs, relayTick, tmuxHasSession, pendingReportIds, sendPendingReportFiles, sweepProcessedReports, } from './relay.js';
|
|
17
|
+
import { RELAY_FETCH_TIMEOUT_MS, createRelayState, deliveryReport, parseAgentKind, reconnectDelayMs, relayTick, tmuxHasSession, pendingReportIds, sendPendingReportFiles, sweepProcessedReports, uploadReportShots, } from './relay.js';
|
|
18
18
|
import { createHeadlessState, headlessNextDelayMs, headlessTick, headlessTrackedCommandIds, evaluateClaudePin, lastClaudePinStatus, parseRunnerMode, } from './headless.js';
|
|
19
19
|
import { runViewCommand } from './view.js';
|
|
20
20
|
import { createRelayRunner } from './relay-runner.js';
|
|
@@ -255,6 +255,9 @@ async function sendReport(cfg) {
|
|
|
255
255
|
}
|
|
256
256
|
await sendPendingReportFiles(cfg, { exclude, rejectCounts: reportRejectCounts });
|
|
257
257
|
}
|
|
258
|
+
// ★報告に付いた写真(直す前・直した後)を管制へ上げる(便 d03ddc62)。報告の送りの【後】に回し、
|
|
259
|
+
// 報告が管制へ渡った便の写真だけを上げる。写真が無ければ中央を1回も呼ばない。失敗しても止めない。
|
|
260
|
+
await uploadReportShots(cfg);
|
|
258
261
|
}
|
|
259
262
|
/** `conductor start`: 起動直後に1回 + 以後 intervalSec ごとに報告し続ける。 */
|
|
260
263
|
async function start(args) {
|
package/dist/relay.js
CHANGED
|
@@ -3652,6 +3652,429 @@ export async function sendPendingReportFiles(cfg, opts = {}) {
|
|
|
3652
3652
|
}
|
|
3653
3653
|
return { sent, processed, kept, rejected, malformed };
|
|
3654
3654
|
}
|
|
3655
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
3656
|
+
// 報告に付いた写真(直す前・直した後)を管制へ上げる(便 d03ddc62・中央は ai-kana PR #528)
|
|
3657
|
+
//
|
|
3658
|
+
// 【なぜ要るか】鉄則16(便 570f32d0)で、職人は画面を変えた便の写真を報告の置き場の
|
|
3659
|
+
// `shots/<指示ID>/` に撮るようになった。管制は便の報告の中に「直す前|直した後」を並べる口と画面を
|
|
3660
|
+
// 持った。残っていたのは、写真を管制へ上げる所(=ここ)。
|
|
3661
|
+
// 【流れ】① 上げ先を頼む POST /api/conductor/commands/<id>/shots
|
|
3662
|
+
// ② 署名付きの上げ先へ PUT(本体は Vercel の関数を通らない)
|
|
3663
|
+
// ③ 上げ終わりを知らせる POST /api/conductor/commands/<id>/shots/complete
|
|
3664
|
+
// 【決まり(えふさん決定 2026-10-30)】
|
|
3665
|
+
// ・報告が管制へ渡った後(`processed/` に控えが在る便)だけ上げる=報告の送りを遅らせない。
|
|
3666
|
+
// ・上げるのは決まりに合う名前の PNG・JPEG だけ。合わない物・大きすぎる物は上げずに記録に残す。1便12枚まで。
|
|
3667
|
+
// ・断りや失敗は記録に残し、便そのものは止めない。503 storage_not_ready(置き場がまだ無い)は静かに次の機会へ。
|
|
3668
|
+
// ・上げ終わった物は控え(`shots/<id>/.uploaded.json`)に覚え、二度送らない。
|
|
3669
|
+
// ・★写真の中身・署名付きの住所・置き場の道・鍵は記録(ログ)に出さない。出すのは写真の名前と断りの語だけ。
|
|
3670
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
3671
|
+
/** 写真を置く下位フォルダの名前(報告の置き場の下・screen_compare.py の --out と同じ)。 */
|
|
3672
|
+
export const SHOTS_DIR = 'shots';
|
|
3673
|
+
/** 上げた物を覚えておく控えの名前(写真のフォルダの中・点で始めて写真と取り違えない)。 */
|
|
3674
|
+
export const SHOTS_LEDGER = '.uploaded.json';
|
|
3675
|
+
/** 1便に上げる写真の上限(★中央の上限と同じ値にすること)。 */
|
|
3676
|
+
export const SHOTS_MAX_PER_COMMAND = 12;
|
|
3677
|
+
/** 1枚の大きさの上限(バイト・★中央の上限と同じ値にすること)。 */
|
|
3678
|
+
export const SHOT_MAX_BYTES = 5_000_000;
|
|
3679
|
+
/** 1周で見る便の数の上限(多すぎる分は次の周に回す)。 */
|
|
3680
|
+
export const SHOTS_COMMANDS_PER_ROUND = 5;
|
|
3681
|
+
/** 置き場がまだ無い(503 storage_not_ready)ときに、次に試すまで空ける時間。 */
|
|
3682
|
+
export const SHOTS_NOT_READY_PAUSE_MS = 10 * 60 * 1000;
|
|
3683
|
+
/** 鍵の断り・中央の不調など、便によらない失敗のときに次に試すまで空ける時間。 */
|
|
3684
|
+
export const SHOTS_ERROR_PAUSE_MS = 5 * 60 * 1000;
|
|
3685
|
+
/** 便が見つからない(404)と言われ続けたら、その便の写真は上げるのをやめる回数。 */
|
|
3686
|
+
export const SHOTS_NOT_FOUND_MAX = 3;
|
|
3687
|
+
/** 写真1枚を PUT するときの上限時間(5MB まで送るので、ほかの口より長い)。 */
|
|
3688
|
+
const SHOT_PUT_TIMEOUT_MS = 60 * 1000;
|
|
3689
|
+
/** 名前で断る語(中央が1枚を名指しして断る物)。★控えに残し、同じ写真は二度送らない。 */
|
|
3690
|
+
const SHOT_NAMED_REJECTS = new Set([
|
|
3691
|
+
'bad_name',
|
|
3692
|
+
'bad_size',
|
|
3693
|
+
'bad_type',
|
|
3694
|
+
'type_mismatch',
|
|
3695
|
+
'too_large',
|
|
3696
|
+
'not_image',
|
|
3697
|
+
'bad_path',
|
|
3698
|
+
'too_many',
|
|
3699
|
+
'store_failed',
|
|
3700
|
+
]);
|
|
3701
|
+
/**
|
|
3702
|
+
* 写真の名前が決まりに合うか。合えば送る種類(Content-Type)、合わなければ null。純粋関数。
|
|
3703
|
+
* 形:`<before|after|compare>-<画面>-<幅>.<png|jpg|jpeg>`(screen_compare.py の出す名前そのまま)
|
|
3704
|
+
* 画面=英小文字・数字・ハイフンだけ・40字まで・ハイフンで区切った1つが20字まで/幅=200〜4000。
|
|
3705
|
+
*/
|
|
3706
|
+
export function shotContentType(name) {
|
|
3707
|
+
const m = /^(before|after|compare)-([a-z0-9]+(?:-[a-z0-9]+)*)-(\d{3,4})\.(png|jpg|jpeg)$/.exec(String(name ?? ''));
|
|
3708
|
+
if (!m)
|
|
3709
|
+
return null;
|
|
3710
|
+
const page = m[2];
|
|
3711
|
+
if (page.length > 40 || page.split('-').some((s) => s.length > 20))
|
|
3712
|
+
return null;
|
|
3713
|
+
const width = Number(m[3]);
|
|
3714
|
+
if (width < 200 || width > 4000)
|
|
3715
|
+
return null;
|
|
3716
|
+
return m[4] === 'png' ? 'image/png' : 'image/jpeg';
|
|
3717
|
+
}
|
|
3718
|
+
/** 中身の先頭バイトが、名前の種類と合う PNG・JPEG か。 */
|
|
3719
|
+
function shotBytesMatch(bytes, type) {
|
|
3720
|
+
if (type === 'image/png') {
|
|
3721
|
+
return bytes.length >= 8 && bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));
|
|
3722
|
+
}
|
|
3723
|
+
return bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff;
|
|
3724
|
+
}
|
|
3725
|
+
function readShotsLedger(dir) {
|
|
3726
|
+
try {
|
|
3727
|
+
const data = JSON.parse(readFileSync(path.join(dir, SHOTS_LEDGER), 'utf8'));
|
|
3728
|
+
return {
|
|
3729
|
+
sent: data.sent && typeof data.sent === 'object' ? data.sent : {},
|
|
3730
|
+
skipped: data.skipped && typeof data.skipped === 'object' ? data.skipped : {},
|
|
3731
|
+
notFound: typeof data.notFound === 'number' ? data.notFound : 0,
|
|
3732
|
+
gaveUp: typeof data.gaveUp === 'string' ? data.gaveUp : undefined,
|
|
3733
|
+
};
|
|
3734
|
+
}
|
|
3735
|
+
catch {
|
|
3736
|
+
return { sent: {}, skipped: {}, notFound: 0 };
|
|
3737
|
+
}
|
|
3738
|
+
}
|
|
3739
|
+
function writeShotsLedger(dir, ledger) {
|
|
3740
|
+
const file = path.join(dir, SHOTS_LEDGER);
|
|
3741
|
+
const tmp = `${file}.tmp-${process.pid}`;
|
|
3742
|
+
try {
|
|
3743
|
+
writeFileSync(tmp, JSON.stringify(ledger, null, 2), { mode: 0o600 });
|
|
3744
|
+
renameSync(tmp, file);
|
|
3745
|
+
}
|
|
3746
|
+
catch {
|
|
3747
|
+
// 書けなければ次の周でもう一度上げることになるだけ(中央は同じ名前を置き換える=壊れない)。
|
|
3748
|
+
}
|
|
3749
|
+
}
|
|
3750
|
+
const defaultShotsState = { pauseUntil: 0, inFlight: false };
|
|
3751
|
+
/** 報告が管制へ渡った(`processed/` に控えが在る)便か。 */
|
|
3752
|
+
function reportDelivered(commandId, env) {
|
|
3753
|
+
return processedCandidates(commandId, env).some((f) => readIfFilled(f) !== null);
|
|
3754
|
+
}
|
|
3755
|
+
/** 写真のフォルダが在る便(指示ID → フォルダ)。同じ指示IDが複数の置き場に在れば前の置き場を使う。 */
|
|
3756
|
+
function shotsDirs(env) {
|
|
3757
|
+
const out = new Map();
|
|
3758
|
+
for (const store of reportStoreDirs(env)) {
|
|
3759
|
+
const base = path.join(store, SHOTS_DIR);
|
|
3760
|
+
let names;
|
|
3761
|
+
try {
|
|
3762
|
+
names = readdirSync(base);
|
|
3763
|
+
}
|
|
3764
|
+
catch {
|
|
3765
|
+
continue; // 写真の置き場が無い=写真の無い便だけ
|
|
3766
|
+
}
|
|
3767
|
+
for (const name of names.sort()) {
|
|
3768
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(name) || out.has(name))
|
|
3769
|
+
continue;
|
|
3770
|
+
const dir = path.join(base, name);
|
|
3771
|
+
try {
|
|
3772
|
+
if (statSync(dir).isDirectory())
|
|
3773
|
+
out.set(name, dir);
|
|
3774
|
+
}
|
|
3775
|
+
catch {
|
|
3776
|
+
// 読めなければ飛ばす
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
return out;
|
|
3781
|
+
}
|
|
3782
|
+
/** 返りの JSON を読む(読めなければ null)。 */
|
|
3783
|
+
async function readJson(res) {
|
|
3784
|
+
const data = (await res.json().catch(() => null));
|
|
3785
|
+
return data && typeof data === 'object' ? data : null;
|
|
3786
|
+
}
|
|
3787
|
+
/** 短い断りの語だけを取り出す(★本文の他の部分はログに出さない)。 */
|
|
3788
|
+
function rejectCode(data) {
|
|
3789
|
+
const code = typeof data?.error === 'string' ? data.error : '';
|
|
3790
|
+
return /^[a-z_ ]{1,40}$/i.test(code) ? code : '理由なし';
|
|
3791
|
+
}
|
|
3792
|
+
/**
|
|
3793
|
+
* 報告が管制へ渡った便の写真を、①上げ先を頼む → ②PUT → ③上げ終わりを知らせる で上げる。
|
|
3794
|
+
* ★便を止めない・報告の送りを遅らせない・例外を投げない(最善努力)。
|
|
3795
|
+
* 返り: 見た便の数・上げ終わった枚数・上げなかった枚数・置き場がまだ無かったか。
|
|
3796
|
+
*/
|
|
3797
|
+
export async function uploadReportShots(cfg, opts = {}) {
|
|
3798
|
+
const env = opts.env;
|
|
3799
|
+
const doFetch = opts.fetchImpl ?? fetch;
|
|
3800
|
+
const logLine = opts.logLine ?? log;
|
|
3801
|
+
const state = opts.state ?? defaultShotsState;
|
|
3802
|
+
const clock = opts.now ?? Date.now;
|
|
3803
|
+
const result = { commands: 0, uploaded: 0, skipped: 0, notReady: false };
|
|
3804
|
+
if (state.inFlight || clock() < state.pauseUntil)
|
|
3805
|
+
return result;
|
|
3806
|
+
state.inFlight = true;
|
|
3807
|
+
try {
|
|
3808
|
+
for (const [id, dir] of shotsDirs(env)) {
|
|
3809
|
+
if (result.commands >= SHOTS_COMMANDS_PER_ROUND)
|
|
3810
|
+
break;
|
|
3811
|
+
const ledger = readShotsLedger(dir);
|
|
3812
|
+
if (ledger.gaveUp)
|
|
3813
|
+
continue;
|
|
3814
|
+
// ★報告が管制へ渡る前は上げない(報告の送りを先にする・便の画面は報告の下に写真を出す)。
|
|
3815
|
+
if (!reportDelivered(id, env))
|
|
3816
|
+
continue;
|
|
3817
|
+
const stop = await uploadOneCommand(cfg, id, dir, ledger, { doFetch, logLine, state, clock, result });
|
|
3818
|
+
if (stop)
|
|
3819
|
+
break;
|
|
3820
|
+
}
|
|
3821
|
+
}
|
|
3822
|
+
catch (err) {
|
|
3823
|
+
logLine(`site=${cfg.site} 写真の上げ -> 失敗: ${err instanceof Error ? err.message : String(err)}`);
|
|
3824
|
+
}
|
|
3825
|
+
finally {
|
|
3826
|
+
state.inFlight = false;
|
|
3827
|
+
}
|
|
3828
|
+
return result;
|
|
3829
|
+
}
|
|
3830
|
+
/** 1便ぶん。返り: true なら、この周はもう他の便も試さない(便によらない失敗)。 */
|
|
3831
|
+
async function uploadOneCommand(cfg, id, dir, ledger, ctx) {
|
|
3832
|
+
const { doFetch, logLine, state, clock, result } = ctx;
|
|
3833
|
+
const tag = `site=${cfg.site} id=${id}`;
|
|
3834
|
+
const skip = (name, key, reason) => {
|
|
3835
|
+
ledger.skipped[name] = { key, reason };
|
|
3836
|
+
delete ledger.sent[name];
|
|
3837
|
+
result.skipped++;
|
|
3838
|
+
logLine(`${tag} 写真 ${name} は上げませんでした(${reason})`);
|
|
3839
|
+
};
|
|
3840
|
+
const cands = [];
|
|
3841
|
+
let names;
|
|
3842
|
+
try {
|
|
3843
|
+
names = readdirSync(dir).filter((n) => !n.startsWith('.')).sort();
|
|
3844
|
+
}
|
|
3845
|
+
catch {
|
|
3846
|
+
return false;
|
|
3847
|
+
}
|
|
3848
|
+
let dirty = false;
|
|
3849
|
+
for (const name of names) {
|
|
3850
|
+
let st;
|
|
3851
|
+
try {
|
|
3852
|
+
st = statSync(path.join(dir, name));
|
|
3853
|
+
}
|
|
3854
|
+
catch {
|
|
3855
|
+
continue;
|
|
3856
|
+
}
|
|
3857
|
+
if (!st.isFile())
|
|
3858
|
+
continue; // 下位フォルダ(試しの写真など)は見ない
|
|
3859
|
+
const key = `${st.size}:${Math.floor(st.mtimeMs)}`;
|
|
3860
|
+
if (ledger.sent[name] === key || ledger.skipped[name]?.key === key)
|
|
3861
|
+
continue;
|
|
3862
|
+
dirty = true;
|
|
3863
|
+
const type = shotContentType(name);
|
|
3864
|
+
if (type === null) {
|
|
3865
|
+
skip(name, key, 'bad_name');
|
|
3866
|
+
continue;
|
|
3867
|
+
}
|
|
3868
|
+
if (st.size === 0) {
|
|
3869
|
+
skip(name, key, 'bad_size');
|
|
3870
|
+
continue;
|
|
3871
|
+
}
|
|
3872
|
+
if (st.size > SHOT_MAX_BYTES) {
|
|
3873
|
+
skip(name, key, 'too_large');
|
|
3874
|
+
continue;
|
|
3875
|
+
}
|
|
3876
|
+
let bytes;
|
|
3877
|
+
try {
|
|
3878
|
+
bytes = readFileSync(path.join(dir, name));
|
|
3879
|
+
}
|
|
3880
|
+
catch {
|
|
3881
|
+
continue; // 読めなければ次の周
|
|
3882
|
+
}
|
|
3883
|
+
if (!shotBytesMatch(bytes, type)) {
|
|
3884
|
+
skip(name, key, 'not_image');
|
|
3885
|
+
continue;
|
|
3886
|
+
}
|
|
3887
|
+
cands.push({ name, key, type, bytes });
|
|
3888
|
+
}
|
|
3889
|
+
if (cands.length === 0) {
|
|
3890
|
+
if (dirty)
|
|
3891
|
+
writeShotsLedger(dir, ledger);
|
|
3892
|
+
return false;
|
|
3893
|
+
}
|
|
3894
|
+
// 1便12枚まで。★撮り直し(上げ終わった名前の置き直し)は数えない。並べた1枚 → 直した後 → 直す前 の順に残す。
|
|
3895
|
+
const rank = (n) => (n.startsWith('compare-') ? 0 : n.startsWith('after-') ? 1 : 2);
|
|
3896
|
+
cands.sort((a, b) => rank(a.name) - rank(b.name) || a.name.localeCompare(b.name));
|
|
3897
|
+
const room = SHOTS_MAX_PER_COMMAND - Object.keys(ledger.sent).length;
|
|
3898
|
+
const batch = [];
|
|
3899
|
+
let fresh = 0;
|
|
3900
|
+
for (const c of cands) {
|
|
3901
|
+
if (c.name in ledger.sent || fresh < room) {
|
|
3902
|
+
if (!(c.name in ledger.sent))
|
|
3903
|
+
fresh++;
|
|
3904
|
+
batch.push(c);
|
|
3905
|
+
}
|
|
3906
|
+
else {
|
|
3907
|
+
skip(c.name, c.key, 'too_many');
|
|
3908
|
+
}
|
|
3909
|
+
}
|
|
3910
|
+
if (batch.length === 0) {
|
|
3911
|
+
writeShotsLedger(dir, ledger);
|
|
3912
|
+
return false;
|
|
3913
|
+
}
|
|
3914
|
+
result.commands++;
|
|
3915
|
+
const base = `${cfg.url}/api/conductor/commands/${encodeURIComponent(id)}/shots`;
|
|
3916
|
+
const headers = { ...authHeaders(cfg), 'Content-Type': 'application/json' };
|
|
3917
|
+
/** 便によらない失敗。待ちを入れて、この周は終える。 */
|
|
3918
|
+
const pauseAll = (status, code) => {
|
|
3919
|
+
if (status === 503 && code === 'storage_not_ready') {
|
|
3920
|
+
// ★静かに次の機会へ(えふさんが置き場の SQL を貼るまではこの状態)。
|
|
3921
|
+
state.pauseUntil = clock() + SHOTS_NOT_READY_PAUSE_MS;
|
|
3922
|
+
result.notReady = true;
|
|
3923
|
+
}
|
|
3924
|
+
else {
|
|
3925
|
+
state.pauseUntil = clock() + SHOTS_ERROR_PAUSE_MS;
|
|
3926
|
+
logLine(`${tag} 写真の上げ -> HTTP ${status}(${code})・あとでもう一度試します`);
|
|
3927
|
+
}
|
|
3928
|
+
writeShotsLedger(dir, ledger);
|
|
3929
|
+
return true;
|
|
3930
|
+
};
|
|
3931
|
+
// ① 上げ先を頼む
|
|
3932
|
+
let uploads = [];
|
|
3933
|
+
try {
|
|
3934
|
+
const res = await doFetch(base, {
|
|
3935
|
+
method: 'POST',
|
|
3936
|
+
headers,
|
|
3937
|
+
body: JSON.stringify({ files: batch.map((c) => ({ name: c.name, contentType: c.type, size: c.bytes.length })) }),
|
|
3938
|
+
signal: AbortSignal.timeout(RELAY_FETCH_TIMEOUT_MS),
|
|
3939
|
+
});
|
|
3940
|
+
const data = await readJson(res);
|
|
3941
|
+
if (!res.ok) {
|
|
3942
|
+
const code = rejectCode(data);
|
|
3943
|
+
const named = typeof data?.name === 'string' ? batch.find((c) => c.name === data.name) : undefined;
|
|
3944
|
+
if (named && SHOT_NAMED_REJECTS.has(code)) {
|
|
3945
|
+
// 1枚が合わない=全部が断られた。その1枚を控えに残し、残りは次の周で頼み直す。
|
|
3946
|
+
skip(named.name, named.key, code);
|
|
3947
|
+
writeShotsLedger(dir, ledger);
|
|
3948
|
+
return false;
|
|
3949
|
+
}
|
|
3950
|
+
if (res.status === 413 && code === 'too_many') {
|
|
3951
|
+
ledger.gaveUp = 'too_many';
|
|
3952
|
+
logLine(`${tag} 写真はこの便の上限に達しているので、これ以上は上げません(too_many)`);
|
|
3953
|
+
writeShotsLedger(dir, ledger);
|
|
3954
|
+
return false;
|
|
3955
|
+
}
|
|
3956
|
+
if (res.status === 404) {
|
|
3957
|
+
ledger.notFound = (ledger.notFound ?? 0) + 1;
|
|
3958
|
+
if (ledger.notFound >= SHOTS_NOT_FOUND_MAX)
|
|
3959
|
+
ledger.gaveUp = 'not_found';
|
|
3960
|
+
logLine(`${tag} 写真の上げ -> 便が見つからない(${ledger.notFound}/${SHOTS_NOT_FOUND_MAX}回目` +
|
|
3961
|
+
`${ledger.gaveUp ? '・この便の写真はもう上げません' : ''})`);
|
|
3962
|
+
writeShotsLedger(dir, ledger);
|
|
3963
|
+
return false;
|
|
3964
|
+
}
|
|
3965
|
+
return pauseAll(res.status, code);
|
|
3966
|
+
}
|
|
3967
|
+
const list = Array.isArray(data?.uploads) ? data.uploads : [];
|
|
3968
|
+
for (const u of list) {
|
|
3969
|
+
const o = u;
|
|
3970
|
+
if (typeof o?.name === 'string' && typeof o.path === 'string' && typeof o.uploadUrl === 'string') {
|
|
3971
|
+
uploads.push({ name: o.name, path: o.path, uploadUrl: o.uploadUrl, contentType: String(o.contentType ?? '') });
|
|
3972
|
+
}
|
|
3973
|
+
}
|
|
3974
|
+
}
|
|
3975
|
+
catch (err) {
|
|
3976
|
+
state.pauseUntil = clock() + SHOTS_ERROR_PAUSE_MS;
|
|
3977
|
+
logLine(`${tag} 写真の上げ先を頼めませんでした: ${err instanceof Error ? err.name : '失敗'}・あとでもう一度試します`);
|
|
3978
|
+
writeShotsLedger(dir, ledger);
|
|
3979
|
+
return true;
|
|
3980
|
+
}
|
|
3981
|
+
uploads = uploads.filter((u) => batch.some((c) => c.name === u.name));
|
|
3982
|
+
if (uploads.length === 0) {
|
|
3983
|
+
logLine(`${tag} 写真の上げ -> 返りの形が違う(あとでもう一度試します)`);
|
|
3984
|
+
writeShotsLedger(dir, ledger);
|
|
3985
|
+
return false;
|
|
3986
|
+
}
|
|
3987
|
+
// ② 本体を上げる。★住所は https だけ・ログに出さない。
|
|
3988
|
+
const put = [];
|
|
3989
|
+
for (const u of uploads) {
|
|
3990
|
+
const c = batch.find((x) => x.name === u.name);
|
|
3991
|
+
let https = false;
|
|
3992
|
+
try {
|
|
3993
|
+
https = new URL(u.uploadUrl).protocol === 'https:';
|
|
3994
|
+
}
|
|
3995
|
+
catch {
|
|
3996
|
+
https = false;
|
|
3997
|
+
}
|
|
3998
|
+
if (!https) {
|
|
3999
|
+
logLine(`${tag} 写真 ${c.name} の上げ先が https でないので送りませんでした`);
|
|
4000
|
+
continue;
|
|
4001
|
+
}
|
|
4002
|
+
try {
|
|
4003
|
+
const res = await doFetch(u.uploadUrl, {
|
|
4004
|
+
method: 'PUT',
|
|
4005
|
+
headers: { 'Content-Type': c.type },
|
|
4006
|
+
body: new Uint8Array(c.bytes),
|
|
4007
|
+
signal: AbortSignal.timeout(SHOT_PUT_TIMEOUT_MS),
|
|
4008
|
+
});
|
|
4009
|
+
if (res.ok)
|
|
4010
|
+
put.push({ name: c.name, path: u.path });
|
|
4011
|
+
else
|
|
4012
|
+
logLine(`${tag} 写真 ${c.name} を送れませんでした(HTTP ${res.status}・あとでもう一度試します)`);
|
|
4013
|
+
}
|
|
4014
|
+
catch (err) {
|
|
4015
|
+
logLine(`${tag} 写真 ${c.name} を送れませんでした(${err instanceof Error ? err.name : '失敗'}・あとでもう一度試します)`);
|
|
4016
|
+
}
|
|
4017
|
+
}
|
|
4018
|
+
if (put.length === 0) {
|
|
4019
|
+
writeShotsLedger(dir, ledger);
|
|
4020
|
+
return false;
|
|
4021
|
+
}
|
|
4022
|
+
// ③ 上げ終わりを知らせる
|
|
4023
|
+
try {
|
|
4024
|
+
const res = await doFetch(`${base}/complete`, {
|
|
4025
|
+
method: 'POST',
|
|
4026
|
+
headers,
|
|
4027
|
+
body: JSON.stringify({ uploads: put }),
|
|
4028
|
+
signal: AbortSignal.timeout(RELAY_FETCH_TIMEOUT_MS),
|
|
4029
|
+
});
|
|
4030
|
+
const data = await readJson(res);
|
|
4031
|
+
if (!res.ok) {
|
|
4032
|
+
if (res.status === 404) {
|
|
4033
|
+
ledger.notFound = (ledger.notFound ?? 0) + 1;
|
|
4034
|
+
if (ledger.notFound >= SHOTS_NOT_FOUND_MAX)
|
|
4035
|
+
ledger.gaveUp = 'not_found';
|
|
4036
|
+
logLine(`${tag} 写真の上げ終わりの知らせ -> 便が見つからない(${ledger.notFound}/${SHOTS_NOT_FOUND_MAX}回目)`);
|
|
4037
|
+
writeShotsLedger(dir, ledger);
|
|
4038
|
+
return false;
|
|
4039
|
+
}
|
|
4040
|
+
return pauseAll(res.status, rejectCode(data));
|
|
4041
|
+
}
|
|
4042
|
+
const results = Array.isArray(data?.results) ? data.results : [];
|
|
4043
|
+
let done = 0;
|
|
4044
|
+
for (const r of results) {
|
|
4045
|
+
const o = r;
|
|
4046
|
+
const c = typeof o?.name === 'string' ? batch.find((x) => x.name === o.name) : undefined;
|
|
4047
|
+
if (!c || !put.some((p) => p.name === c.name))
|
|
4048
|
+
continue;
|
|
4049
|
+
if (o.ok === true) {
|
|
4050
|
+
ledger.sent[c.name] = c.key;
|
|
4051
|
+
delete ledger.skipped[c.name];
|
|
4052
|
+
result.uploaded++;
|
|
4053
|
+
done++;
|
|
4054
|
+
}
|
|
4055
|
+
else {
|
|
4056
|
+
const code = typeof o.error === 'string' ? o.error : '理由なし';
|
|
4057
|
+
if (code === 'missing') {
|
|
4058
|
+
logLine(`${tag} 写真 ${c.name} は届いていませんでした(missing・あとでもう一度試します)`);
|
|
4059
|
+
}
|
|
4060
|
+
else {
|
|
4061
|
+
skip(c.name, c.key, /^[a-z_]{1,40}$/.test(code) ? code : '理由なし');
|
|
4062
|
+
}
|
|
4063
|
+
}
|
|
4064
|
+
}
|
|
4065
|
+
ledger.notFound = 0;
|
|
4066
|
+
if (done > 0)
|
|
4067
|
+
logLine(`${tag} 写真を ${done} 枚、管制へ上げました`);
|
|
4068
|
+
}
|
|
4069
|
+
catch (err) {
|
|
4070
|
+
state.pauseUntil = clock() + SHOTS_ERROR_PAUSE_MS;
|
|
4071
|
+
logLine(`${tag} 写真の上げ終わりを知らせられませんでした(${err instanceof Error ? err.name : '失敗'}・あとでもう一度試します)`);
|
|
4072
|
+
writeShotsLedger(dir, ledger);
|
|
4073
|
+
return true;
|
|
4074
|
+
}
|
|
4075
|
+
writeShotsLedger(dir, ledger);
|
|
4076
|
+
return false;
|
|
4077
|
+
}
|
|
3655
4078
|
/**
|
|
3656
4079
|
* 古い控えを片付ける。★消すのは `processed/` の中だけ(未処理は何日たっても消さない)。
|
|
3657
4080
|
* 返り: 消した件数。
|