@infodb/p3 0.4.0 → 0.5.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/bin/p3.mjs +11 -5
- package/dist/web/assets/Outputs-C83ZDXvs.js +1 -0
- package/dist/web/assets/RequestStatus-GmiWYEcL.js +1 -0
- package/dist/web/assets/Tabs-CJn0Qy7W.js +1 -0
- package/dist/web/assets/TagInput-CtM8HM8Z.js +1 -0
- package/dist/web/assets/TaskDrawer-D5v36QoF.js +3 -0
- package/dist/web/assets/_requestId-BHUqLO-j.js +1 -0
- package/dist/web/assets/_requestId-DxwxbXY2.js +30 -0
- package/dist/web/assets/{ui-DdOGYj9z.js → appCtx-B-VRVOyv.js} +1 -1
- package/dist/web/assets/board-DAaVR04x.js +1 -0
- package/dist/web/assets/done-CaqB_qTH.js +1 -0
- package/dist/web/assets/index-BF1xMdJo.css +2 -0
- package/dist/web/assets/index-BQYvKpjF.js +12 -0
- package/dist/web/assets/new-DLVnRvrB.js +1 -0
- package/dist/web/assets/{preflightInput-DMxqnvF8.js → preflightInput-BAeEJYUK.js} +1 -1
- package/dist/web/assets/preload-helper-B74h5i3O.js +1 -0
- package/dist/web/assets/progressTree-wp9mH4VF.js +1 -0
- package/dist/web/assets/release-notes-MTLcOcvZ.js +3 -0
- package/dist/web/assets/requestData-D-HXu-cy.js +1 -0
- package/dist/web/assets/routes-CI4Qfkjs.js +3 -0
- package/dist/web/assets/settings-CJMNgzi6.js +1 -0
- package/dist/web/index.html +4 -4
- package/docs/architecture.md +220 -3
- package/docs/development.md +16 -9
- package/docs/how-to-use.md +72 -7
- package/docs/installation.md +8 -6
- package/docs/publishing.md +4 -2
- package/lib/cli/args.js +172 -0
- package/lib/cli/chatgpt.js +70 -0
- package/lib/cli/codex.js +30 -0
- package/lib/cli/format.js +9 -0
- package/{src/cli/help.ts → lib/cli/help.js} +8 -8
- package/lib/cli/main.js +326 -0
- package/lib/cli/output.js +110 -0
- package/lib/cli/preflightCommands.js +301 -0
- package/lib/cli/project.js +649 -0
- package/lib/cli/readOnly.js +36 -0
- package/lib/cli/releaseNotesCommands.js +197 -0
- package/lib/cli/render.js +58 -0
- package/lib/cli/requestCommands.js +180 -0
- package/lib/cli/showView.js +100 -0
- package/lib/cli/taskCommands.js +152 -0
- package/lib/cli/userConfig.js +64 -0
- package/{src/core/chatgpt.ts → lib/core/chatgpt.js} +10 -16
- package/lib/core/context.js +41 -0
- package/lib/core/hierarchy.js +128 -0
- package/lib/core/machineSettings.js +146 -0
- package/lib/core/machineSettingsEdit.js +148 -0
- package/lib/core/ops.js +120 -0
- package/lib/core/preflight/carryover.js +105 -0
- package/lib/core/preflight/codexEngine.js +687 -0
- package/lib/core/preflight/codexImage.js +61 -0
- package/lib/core/preflight/diagnose.js +95 -0
- package/lib/core/preflight/engine.js +58 -0
- package/lib/core/preflight/input.js +83 -0
- package/lib/core/preflight/launch.js +68 -0
- package/lib/core/preflight/mockEngine.js +175 -0
- package/lib/core/preflight/render.js +304 -0
- package/lib/core/preflight/service.js +868 -0
- package/lib/core/preflight/split.js +83 -0
- package/lib/core/preflight/uiMaterials.js +281 -0
- package/lib/core/preflight/uiRender.js +89 -0
- package/lib/core/preflight/wireframe.js +63 -0
- package/lib/core/preflight/xlsx.js +324 -0
- package/lib/core/projectRegistry.js +207 -0
- package/lib/core/redact.js +28 -0
- package/lib/core/releaseNotes/completeRequests.js +106 -0
- package/lib/core/releaseNotes/generate.js +266 -0
- package/lib/core/releaseNotes/material.js +141 -0
- package/lib/core/releaseNotes/mockSummarizer.js +26 -0
- package/lib/core/releaseNotes/service.js +83 -0
- package/lib/core/releaseNotes/store.js +209 -0
- package/lib/core/repoAdmin.js +82 -0
- package/lib/core/repoRefs.js +72 -0
- package/lib/core/requestOps.js +154 -0
- package/lib/core/requestStatus.js +171 -0
- package/lib/core/store/attachments.js +242 -0
- package/lib/core/store/collection.js +254 -0
- package/lib/core/store/frontmatter.js +18 -0
- package/lib/core/store/fsutil.js +84 -0
- package/lib/core/store/locate.js +162 -0
- package/lib/core/store/normalize.js +150 -0
- package/lib/core/store/noteStore.js +59 -0
- package/lib/core/store/preflightStore.js +161 -0
- package/lib/core/store/projectFile.js +99 -0
- package/lib/core/store/workspace.js +320 -0
- package/lib/core/supersede.js +69 -0
- package/lib/core/userConfig.js +158 -0
- package/{src/core/userTheme.ts → lib/core/userTheme.js} +11 -13
- package/lib/core/workTargets.js +106 -0
- package/lib/server/api/chatgptRoutes.js +46 -0
- package/{src/server/api/local.ts → lib/server/api/local.js} +23 -22
- package/lib/server/api/manifest.js +140 -0
- package/lib/server/api/projectsRoutes.js +82 -0
- package/lib/server/api/releaseNoteRoutes.js +88 -0
- package/lib/server/api/repoRoutes.js +67 -0
- package/lib/server/api/routes.js +404 -0
- package/lib/server/api/userThemeRoutes.js +60 -0
- package/lib/server/server.js +85 -0
- package/lib/shared/dates.js +10 -0
- package/{src/shared/directTask.ts → lib/shared/directTask.js} +3 -11
- package/lib/shared/hierarchy.js +120 -0
- package/lib/shared/milestoneOrder.js +45 -0
- package/lib/shared/preflightInput.js +19 -0
- package/lib/shared/refs.js +44 -0
- package/lib/shared/repos.js +69 -0
- package/lib/shared/scopeView.js +39 -0
- package/{src/shared/tags.ts → lib/shared/tags.js} +9 -14
- package/lib/shared/types.js +74 -0
- package/lib/shared/workTarget.js +31 -0
- package/package.json +4 -7
- package/skills/p3/SKILL.md +10 -0
- package/dist/web/assets/Outputs-5Hby-S5Q.js +0 -1
- package/dist/web/assets/RequestStatus-CTkGjT02.js +0 -1
- package/dist/web/assets/TagInput-Rj3PNOAF.js +0 -1
- package/dist/web/assets/TaskDrawer-DtPhiYxP.js +0 -3
- package/dist/web/assets/_requestId-CGvAfIkB.js +0 -30
- package/dist/web/assets/_requestId-DxfYhgAq.js +0 -1
- package/dist/web/assets/board-wt47lfrE.js +0 -1
- package/dist/web/assets/done-C9Ct7ssb.js +0 -1
- package/dist/web/assets/index-BTFL2rpp.js +0 -12
- package/dist/web/assets/index-DAyBsvua.css +0 -2
- package/dist/web/assets/new-e4ID2vQV.js +0 -1
- package/dist/web/assets/preload-helper-DhIyFaOW.js +0 -1
- package/dist/web/assets/release-notes-CQ0d2r0k.js +0 -3
- package/dist/web/assets/requestData-BDZqyl35.js +0 -1
- package/dist/web/assets/routes-Bipv3lwJ.js +0 -2
- package/dist/web/assets/settings-Dwh8fj6p.js +0 -1
- package/src/cli/args.ts +0 -186
- package/src/cli/chatgpt.ts +0 -74
- package/src/cli/codex.ts +0 -28
- package/src/cli/format.ts +0 -9
- package/src/cli/main.ts +0 -337
- package/src/cli/output.ts +0 -117
- package/src/cli/preflightCommands.ts +0 -245
- package/src/cli/project.ts +0 -675
- package/src/cli/readOnly.ts +0 -36
- package/src/cli/releaseNotesCommands.ts +0 -198
- package/src/cli/render.ts +0 -60
- package/src/cli/requestCommands.ts +0 -172
- package/src/cli/showView.ts +0 -110
- package/src/cli/taskCommands.ts +0 -167
- package/src/cli/userConfig.ts +0 -58
- package/src/core/context.ts +0 -58
- package/src/core/machineSettings.ts +0 -207
- package/src/core/machineSettingsEdit.ts +0 -156
- package/src/core/ops.ts +0 -114
- package/src/core/preflight/carryover.ts +0 -133
- package/src/core/preflight/codexEngine.ts +0 -703
- package/src/core/preflight/codexImage.ts +0 -98
- package/src/core/preflight/diagnose.ts +0 -124
- package/src/core/preflight/engine.ts +0 -92
- package/src/core/preflight/input.ts +0 -65
- package/src/core/preflight/launch.ts +0 -86
- package/src/core/preflight/mockEngine.ts +0 -145
- package/src/core/preflight/render.ts +0 -339
- package/src/core/preflight/service.ts +0 -707
- package/src/core/preflight/uiMaterials.ts +0 -272
- package/src/core/preflight/uiRender.ts +0 -84
- package/src/core/preflight/wireframe.ts +0 -69
- package/src/core/preflight/xlsx.ts +0 -328
- package/src/core/projectRegistry.ts +0 -264
- package/src/core/redact.ts +0 -31
- package/src/core/releaseNotes/completeRequests.ts +0 -102
- package/src/core/releaseNotes/generate.ts +0 -283
- package/src/core/releaseNotes/material.ts +0 -114
- package/src/core/releaseNotes/mockSummarizer.ts +0 -28
- package/src/core/releaseNotes/service.ts +0 -110
- package/src/core/releaseNotes/store.ts +0 -216
- package/src/core/repoAdmin.ts +0 -81
- package/src/core/repoRefs.ts +0 -90
- package/src/core/requestOps.ts +0 -140
- package/src/core/requestStatus.ts +0 -58
- package/src/core/store/attachments.ts +0 -228
- package/src/core/store/collection.ts +0 -282
- package/src/core/store/frontmatter.ts +0 -22
- package/src/core/store/fsutil.ts +0 -90
- package/src/core/store/locate.ts +0 -181
- package/src/core/store/normalize.ts +0 -155
- package/src/core/store/noteStore.ts +0 -56
- package/src/core/store/preflightStore.ts +0 -136
- package/src/core/store/projectFile.ts +0 -101
- package/src/core/store/workspace.ts +0 -328
- package/src/core/supersede.ts +0 -74
- package/src/core/userConfig.ts +0 -205
- package/src/core/workTargets.ts +0 -120
- package/src/server/api/chatgptRoutes.ts +0 -49
- package/src/server/api/manifest.ts +0 -135
- package/src/server/api/projectsRoutes.ts +0 -89
- package/src/server/api/releaseNoteRoutes.ts +0 -89
- package/src/server/api/repoRoutes.ts +0 -67
- package/src/server/api/routes.ts +0 -382
- package/src/server/api/userThemeRoutes.ts +0 -62
- package/src/server/server.ts +0 -108
- package/src/shared/dates.ts +0 -12
- package/src/shared/milestoneOrder.ts +0 -44
- package/src/shared/preflightInput.ts +0 -27
- package/src/shared/refs.ts +0 -55
- package/src/shared/repos.ts +0 -113
- package/src/shared/scopeView.ts +0 -48
- package/src/shared/types.ts +0 -730
- package/src/shared/workTarget.ts +0 -44
- /package/{src → lib}/core/preflight/launcher.mjs +0 -0
- /package/{src → lib}/core/preflight/mock-ui.html +0 -0
- /package/{src → lib}/core/preflight/mock-ui.png +0 -0
- /package/{src → lib}/core/preflight/render-image/Dockerfile +0 -0
- /package/{src → lib}/core/preflight/render-image/fonts-ja.conf +0 -0
- /package/{src → lib}/core/preflight/render-image/render.mjs +0 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { streamSSE } from "hono/streaming";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { normalizeTaskId } from "../../shared/refs.js";
|
|
5
|
+
import { REQUEST_STATUSES, TASK_STATUSES } from "../../shared/types.js";
|
|
6
|
+
import { listTasks, progress, resolve } from "../../core/ops.js";
|
|
7
|
+
import { ConflictError, NotFoundError } from "../../core/store/collection.js";
|
|
8
|
+
import { ATTACHMENT_LIMITS, ATTACHMENT_TYPES, AttachmentError, safeFileName } from "../../core/store/attachments.js";
|
|
9
|
+
import { bodyLimit } from "hono/body-limit";
|
|
10
|
+
import { ReloadError } from "../../core/store/workspace.js";
|
|
11
|
+
import { completionCheckOf, hierarchyNow, resolvedIssue } from "../../core/requestStatus.js";
|
|
12
|
+
import { HierarchyError, hierarchyView } from "../../core/hierarchy.js";
|
|
13
|
+
import { supersededMarks } from "../../core/supersede.js";
|
|
14
|
+
import { chatgptRoutes } from "./chatgptRoutes.js";
|
|
15
|
+
import { userThemeRoutes, userThemeScriptRoute } from "./userThemeRoutes.js";
|
|
16
|
+
import { UserConfigError } from "../../core/userConfig.js";
|
|
17
|
+
import { AI_MANIFEST } from "./manifest.js";
|
|
18
|
+
import { repoRoutes } from "./repoRoutes.js";
|
|
19
|
+
import { releaseNoteRoutes } from "./releaseNoteRoutes.js";
|
|
20
|
+
import { archiveTask, checkMilestoneRemoval, createRequest, createTask, updateRequest, updateTask } from "../../core/requestOps.js";
|
|
21
|
+
import { StaleInputError } from "../../core/preflight/input.js";
|
|
22
|
+
import { ChildConflictError, ChildDraftsChangedError, DraftConflictError, PulseInputError } from "../../core/preflight/service.js";
|
|
23
|
+
/**
|
|
24
|
+
* ブラウザ用のローカル API。AI や人の操作は CLI(ファイルを直接読み書き)が主で、ここは画面のため。
|
|
25
|
+
*
|
|
26
|
+
* /requests 要望(進捗つき)
|
|
27
|
+
* /requests/:rid 要望の詳細
|
|
28
|
+
* /requests/:rid/tasks[/:tid] 要望のタスク
|
|
29
|
+
* /requests/:rid/preflight[/:run] Preflight
|
|
30
|
+
* /requests/:rid/notes Preflight へのメモ
|
|
31
|
+
* /release-notes[/:mid] マイルストーンのリリースノート(releaseNoteRoutes.ts)
|
|
32
|
+
*/
|
|
33
|
+
const date = z.string().regex(/^\d{4}-\d{2}-\d{2}$/);
|
|
34
|
+
const taskInput = z.object({
|
|
35
|
+
title: z.string().min(1),
|
|
36
|
+
/** 作業先(参照先の名前)。null は作業先なし */
|
|
37
|
+
repo: z.string().nullable().optional(),
|
|
38
|
+
repoOutputs: z.array(z.object({ repo: z.string().min(1), output: z.string().min(1) })).optional(),
|
|
39
|
+
status: z.enum(TASK_STATUSES).optional(),
|
|
40
|
+
labels: z.array(z.string()).optional(),
|
|
41
|
+
areas: z.array(z.string()).optional(),
|
|
42
|
+
outputs: z.array(z.string()).optional(),
|
|
43
|
+
body: z.string().optional(),
|
|
44
|
+
/** 変更の記録(Markdown。完了にするときに書く) */
|
|
45
|
+
record: z.string().nullable().optional(),
|
|
46
|
+
});
|
|
47
|
+
/** checked: 完了したタスクに人が「済み」を付ける / 外す */
|
|
48
|
+
const taskPatch = taskInput.partial().extend({ rev: z.number().int().optional(), checked: z.boolean().optional() });
|
|
49
|
+
const requestInput = z.object({
|
|
50
|
+
title: z.string().min(1),
|
|
51
|
+
body: z.string().optional(),
|
|
52
|
+
areas: z.array(z.string()).optional(),
|
|
53
|
+
due: date.nullish(),
|
|
54
|
+
milestone: z.string().nullish(),
|
|
55
|
+
tags: z.array(z.string()).optional(),
|
|
56
|
+
/** 親の要望 ID(子の要望にする。PATCH の null は親を外す) */
|
|
57
|
+
parent: z.string().nullish(),
|
|
58
|
+
});
|
|
59
|
+
const requestPatch = requestInput
|
|
60
|
+
.partial()
|
|
61
|
+
.extend({ status: z.enum(REQUEST_STATUSES).optional(), rev: z.number().int().optional() });
|
|
62
|
+
const projectPatch = z
|
|
63
|
+
.object({
|
|
64
|
+
name: z.string().min(1),
|
|
65
|
+
description: z.string(),
|
|
66
|
+
milestones: z.array(z.object({
|
|
67
|
+
id: z.string().regex(/^[A-Za-z0-9._-]+$/, "マイルストーンの id は英数字と . _ -"),
|
|
68
|
+
title: z.string().min(1),
|
|
69
|
+
due: date.optional(),
|
|
70
|
+
done: z.boolean().optional(),
|
|
71
|
+
})),
|
|
72
|
+
branchPattern: z.string().includes("{id}"),
|
|
73
|
+
preflight: z.object({ instructions: z.string() }),
|
|
74
|
+
})
|
|
75
|
+
.partial()
|
|
76
|
+
.superRefine((p, ctx) => {
|
|
77
|
+
for (const [label, ids] of [["マイルストーン", p.milestones?.map((x) => x.id) ?? []]]) {
|
|
78
|
+
const dup = ids.find((x, i) => ids.indexOf(x) !== i);
|
|
79
|
+
if (dup)
|
|
80
|
+
ctx.addIssue({ code: "custom", message: `${label}の id が重複しています: ${dup}` });
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
/** null は「項目を消す」意味。frontmatter から外すため undefined に変換 */
|
|
84
|
+
const unnull = (o) => Object.fromEntries(Object.entries(o).map(([k, v]) => [k, v === null ? undefined : v]));
|
|
85
|
+
/** 誰が変更したか */
|
|
86
|
+
const actorOf = (c) => c.req.header("x-p3-actor") ?? "human:web";
|
|
87
|
+
/** 楽観ロック: body.rev か If-Match ヘッダ */
|
|
88
|
+
const expectedRev = (c, body) => body.rev ?? (c.req.header("if-match") ? Number(c.req.header("if-match")) : undefined);
|
|
89
|
+
/** 原本を返す。xlsx は必ずダウンロードにし、画像も中身を推測させない */
|
|
90
|
+
function download(c, data, format, name) {
|
|
91
|
+
const file = safeFileName(name, format);
|
|
92
|
+
return c.body(new Uint8Array(data), 200, {
|
|
93
|
+
"Content-Type": ATTACHMENT_TYPES[format],
|
|
94
|
+
"Content-Disposition": `${format === "xlsx" ? "attachment" : "inline"}; filename="${file.replace(/[^\x20-\x7e]/g, "_")}"; filename*=UTF-8''${encodeURIComponent(file)}`,
|
|
95
|
+
"X-Content-Type-Options": "nosniff",
|
|
96
|
+
"Content-Security-Policy": "default-src 'none'",
|
|
97
|
+
"Cache-Control": "private, no-cache",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* notify: プロジェクトに属さない変更(ChatGPT の接続・テーマ)の知らせ方。省くとこのプロジェクトの画面へだけ流す
|
|
102
|
+
* (プロジェクトを跨ぐ serve では開いている全プロジェクトへ)
|
|
103
|
+
*/
|
|
104
|
+
export function apiRoutes(ctx, opts = {}) {
|
|
105
|
+
const { ws, preflight } = ctx;
|
|
106
|
+
const notify = opts.notify ?? ((kind) => ws.emit({ type: "changed", kind }));
|
|
107
|
+
const app = new Hono();
|
|
108
|
+
app.onError((err, c) => {
|
|
109
|
+
if (err instanceof NotFoundError)
|
|
110
|
+
return c.json({ error: err.message }, 404);
|
|
111
|
+
// 409 は code で分ける: rev_conflict(読み直してやり直す)/ stale_input(評価のあとで要望が変わった。再評価する)
|
|
112
|
+
if (err instanceof DraftConflictError || err instanceof ChildConflictError)
|
|
113
|
+
return c.json({ error: err.message, code: "rev_conflict", current: err.current }, 409);
|
|
114
|
+
if (err instanceof ChildDraftsChangedError)
|
|
115
|
+
return c.json({ error: err.message, code: err.code }, 409);
|
|
116
|
+
if (err instanceof StaleInputError)
|
|
117
|
+
return c.json({ error: err.message, code: err.code, inputMatch: err.match }, 409);
|
|
118
|
+
if (err instanceof ConflictError) {
|
|
119
|
+
const current = err.id.includes("/") ? ws.task(err.id) : c.req.param("tid") ? ws.tasksOf(c.req.param("rid")).get(err.id) : ws.requests.get(err.id);
|
|
120
|
+
return c.json({ error: err.message, code: "rev_conflict", current }, 409);
|
|
121
|
+
}
|
|
122
|
+
// 要望の親子(code: hierarchy_invalid・hierarchy_busy・has_children・has_parent。何も書いていない)
|
|
123
|
+
if (err instanceof HierarchyError)
|
|
124
|
+
return c.json({ error: err.message, code: err.code, ...err.detail }, 409);
|
|
125
|
+
if (err instanceof z.ZodError)
|
|
126
|
+
return c.json({ error: "invalid input", issues: err.issues }, 400);
|
|
127
|
+
// 個人設定のファイルが読めない・壊れている(ファイルは変えていない)
|
|
128
|
+
if (err instanceof UserConfigError)
|
|
129
|
+
return c.json({ error: err.message }, 500);
|
|
130
|
+
// データのフォルダが読めない(メモリ上の状態は変えていない)
|
|
131
|
+
if (err instanceof ReloadError)
|
|
132
|
+
return c.json({ error: err.message }, 503);
|
|
133
|
+
console.error(err);
|
|
134
|
+
return c.json({ error: err.message }, 400);
|
|
135
|
+
});
|
|
136
|
+
/** パスの要望 ID(存在しなければ 404) */
|
|
137
|
+
const rid = (c) => ws.requests.require(c.req.param("rid")).id;
|
|
138
|
+
/** パスのタスク ID(T3 / 3 も可) */
|
|
139
|
+
const tid = (c) => normalizeTaskId(c.req.param("tid")) ?? c.req.param("tid");
|
|
140
|
+
// --- meta ---------------------------------------------------------------
|
|
141
|
+
app.get("/project", (c) => c.json({
|
|
142
|
+
project: ws.project,
|
|
143
|
+
dataDir: ws.dataDir,
|
|
144
|
+
engines: preflight.availableEngines(),
|
|
145
|
+
defaultEngine: preflight.defaultEngine,
|
|
146
|
+
errors: ws.errors(),
|
|
147
|
+
}));
|
|
148
|
+
app.put("/project", async (c) => {
|
|
149
|
+
const patch = projectPatch.parse(await c.req.json());
|
|
150
|
+
// 使っている要望・リリースノートのあるマイルストーンは消さない(CLI の milestone rm と同じ規則)
|
|
151
|
+
if (patch.milestones)
|
|
152
|
+
await checkMilestoneRemoval(ctx, patch.milestones.map((m) => m.id));
|
|
153
|
+
return c.json(await ws.saveProject(patch));
|
|
154
|
+
});
|
|
155
|
+
app.get("/ai/manifest", (c) => c.json(AI_MANIFEST));
|
|
156
|
+
/** 参照先(設定画面。このマシンのパスはローカルの画面からだけ) */
|
|
157
|
+
app.route("/repos", repoRoutes(ctx));
|
|
158
|
+
/** データのフォルダを全部読み直す(ファイル監視に頼らない取り込み)。終わったら変更通知 kind: "reload" */
|
|
159
|
+
app.post("/reload", async (c) => c.json(await ws.reloadAll()));
|
|
160
|
+
/** Preflight のエンジンが使えるか(Codex なら Docker・認証・イメージを確かめる) */
|
|
161
|
+
app.get("/engine", async (c) => c.json(await preflight.engineStatus()));
|
|
162
|
+
if (ctx.chatgpt)
|
|
163
|
+
app.route("/chatgpt", chatgptRoutes(ctx.chatgpt, { changed: () => notify("chatgpt"), engine: () => preflight.engineStatus() }));
|
|
164
|
+
/** 個人設定のテーマ(このマシンのすべてのプロジェクトで共通。ほかの個人設定は扱わない) */
|
|
165
|
+
app.route("/user-theme", userThemeRoutes(() => notify("user")));
|
|
166
|
+
/** 最初の描画の前に読むスクリプト(index.html の先頭)。テーマだけを埋め込み、キャッシュさせない */
|
|
167
|
+
app.route("/user-theme.js", userThemeScriptRoute());
|
|
168
|
+
/** マイルストーンのリリースノート(材料・保存・出力・要約の生成) */
|
|
169
|
+
app.route("/", releaseNoteRoutes(ctx));
|
|
170
|
+
// --- requests -------------------------------------------------------------
|
|
171
|
+
app.get("/requests", (c) => {
|
|
172
|
+
// ?parent=<ID>: 直下の子だけ
|
|
173
|
+
const parent = c.req.query("parent");
|
|
174
|
+
const all = progress(ctx);
|
|
175
|
+
return c.json(parent === undefined ? all : all.filter((p) => p.request.parent === parent));
|
|
176
|
+
});
|
|
177
|
+
app.post("/requests", async (c) => {
|
|
178
|
+
const b = unnull(requestInput.parse(await c.req.json()));
|
|
179
|
+
return c.json(await createRequest(ctx, b, actorOf(c)), 201);
|
|
180
|
+
});
|
|
181
|
+
app.get("/requests/:rid", async (c) => {
|
|
182
|
+
const id = rid(c);
|
|
183
|
+
return c.json({
|
|
184
|
+
request: ws.requests.get(id),
|
|
185
|
+
tasks: ws.tasksOf(id).list(),
|
|
186
|
+
runs: await Promise.all((await preflight.list(id)).map((r) => preflight.withInputMatch(id, r))),
|
|
187
|
+
// 対応済なのに未完了のタスクがある(外部の直接編集など)
|
|
188
|
+
issue: resolvedIssue(ws, ws.requests.require(id)) ?? null,
|
|
189
|
+
// 親子(parent・ancestors・children・hierarchyIssues)と、対応済にできるか(blocker・見送りで対象外の子 excluded)
|
|
190
|
+
...(await hierarchyView(ws, id)),
|
|
191
|
+
completion: completionCheckOf(ws, hierarchyNow(ws), id),
|
|
192
|
+
// 置き換えられたタスクの印(T001 → 置き換えた側)
|
|
193
|
+
superseded: Object.fromEntries([...supersededMarks(ws)].filter(([ref]) => ref.startsWith(`${id}/`)).map(([ref, m]) => [ref.slice(id.length + 1), m])),
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
app.patch("/requests/:rid", async (c) => {
|
|
197
|
+
const { rev, ...patch } = requestPatch.parse(await c.req.json());
|
|
198
|
+
return c.json(await updateRequest(ctx, rid(c), unnull(patch), { expectedRev: expectedRev(c, { rev }), actor: actorOf(c) }));
|
|
199
|
+
});
|
|
200
|
+
// --- tasks(要望の中) --------------------------------------------------------
|
|
201
|
+
app.get("/requests/:rid/tasks", (c) => {
|
|
202
|
+
const { status, area, q } = c.req.query();
|
|
203
|
+
return c.json(listTasks(ctx, { request: rid(c), status: status?.split(","), area, q }));
|
|
204
|
+
});
|
|
205
|
+
app.post("/requests/:rid/tasks", async (c) => {
|
|
206
|
+
const raw = taskInput.parse(await c.req.json());
|
|
207
|
+
const { repo, ...b } = unnull(raw);
|
|
208
|
+
// 作業先は null(作業先なし)を残す。書かなければ既定(requestOps.createTask)
|
|
209
|
+
const input = { ...b, ...("repo" in raw ? { repo: raw.repo ?? null } : {}) };
|
|
210
|
+
const { task, reopened, reopenedAncestors, warnings } = await createTask(ctx, rid(c), input, actorOf(c));
|
|
211
|
+
return c.json({ ...task, ...(reopened ? { reopened } : {}), ...(reopenedAncestors ? { reopenedAncestors } : {}), ...(warnings.length ? { warnings } : {}) }, 201);
|
|
212
|
+
});
|
|
213
|
+
app.get("/requests/:rid/tasks/:tid", (c) => {
|
|
214
|
+
const task = ws.tasksOf(rid(c)).require(tid(c));
|
|
215
|
+
return c.json({ task, superseded: supersededMarks(ws).get(task.ref) ?? [] });
|
|
216
|
+
});
|
|
217
|
+
app.patch("/requests/:rid/tasks/:tid", async (c) => {
|
|
218
|
+
const { rev, checked, ...patch } = taskPatch.parse(await c.req.json());
|
|
219
|
+
const next = { ...unnull(patch), ...("repo" in patch ? { repo: patch.repo ?? null } : {}), ...(checked !== undefined ? { checked } : {}) };
|
|
220
|
+
// 置き換えられたタスクを作業中にしたら警告を添える(操作は続ける)
|
|
221
|
+
const { task, warnings, reopened, reopenedAncestors } = await updateTask(ctx, rid(c), tid(c), next, { expectedRev: expectedRev(c, { rev }), actor: actorOf(c) });
|
|
222
|
+
return c.json({ ...task, ...(warnings.length ? { warnings } : {}), ...(reopened ? { reopened } : {}), ...(reopenedAncestors ? { reopenedAncestors } : {}) });
|
|
223
|
+
});
|
|
224
|
+
app.delete("/requests/:rid/tasks/:tid", async (c) => {
|
|
225
|
+
await archiveTask(ctx, rid(c), tid(c));
|
|
226
|
+
return c.json({ ok: true });
|
|
227
|
+
});
|
|
228
|
+
// --- preflight ----------------------------------------------------------
|
|
229
|
+
app.post("/requests/:rid/preflight", async (c) => {
|
|
230
|
+
const body = z
|
|
231
|
+
.object({ engine: z.enum(["codex", "mock"]).optional(), note: z.string().optional() })
|
|
232
|
+
.parse(await c.req.json().catch(() => ({})));
|
|
233
|
+
const id = rid(c);
|
|
234
|
+
const actor = actorOf(c);
|
|
235
|
+
// メモを書き足してから実行する。実行を始められなくてもメモは残し、その ID を返す(再試行で書き直さない)
|
|
236
|
+
const note = body.note?.trim() ? await preflight.addNote(id, body.note, actor) : undefined;
|
|
237
|
+
try {
|
|
238
|
+
return c.json(await preflight.start(id, { engine: body.engine, actor }), 202);
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
if (!note)
|
|
242
|
+
throw e;
|
|
243
|
+
return c.json({ error: e.message, note }, 400);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
app.get("/requests/:rid/notes", async (c) => {
|
|
247
|
+
const id = rid(c);
|
|
248
|
+
const [{ notes, invalid }, carry] = await Promise.all([ws.notes.list(id), preflight.carryover(id)]);
|
|
249
|
+
const pending = new Set(carry.notes.map((n) => n.id));
|
|
250
|
+
return c.json({
|
|
251
|
+
notes: notes.map((n) => ({ ...n, pending: pending.has(n.id) })),
|
|
252
|
+
invalid,
|
|
253
|
+
previous: carry.previous?.id ?? null,
|
|
254
|
+
previousSkipped: carry.previousSkipped ?? null,
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
app.post("/requests/:rid/notes", async (c) => {
|
|
258
|
+
const { body } = z.object({ body: z.string() }).parse(await c.req.json());
|
|
259
|
+
return c.json(await preflight.addNote(rid(c), body, actorOf(c)), 201);
|
|
260
|
+
});
|
|
261
|
+
// run ごとに、評価対象と今の要望が同じか(inputMatch)を添える
|
|
262
|
+
app.get("/requests/:rid/preflight", async (c) => {
|
|
263
|
+
const id = rid(c);
|
|
264
|
+
return c.json(await Promise.all((await preflight.list(id)).map((r) => preflight.withInputMatch(id, r))));
|
|
265
|
+
});
|
|
266
|
+
// 最新の Preflight と見ている run の今の状態(ディスクから読むだけ。開いている要望の画面が軽く確かめる)。キャッシュの要望は使わない
|
|
267
|
+
app.get("/requests/:rid/preflight-status", async (c) => {
|
|
268
|
+
try {
|
|
269
|
+
return c.json(await preflight.pulse(c.req.param("rid"), c.req.query("run")));
|
|
270
|
+
}
|
|
271
|
+
catch (e) {
|
|
272
|
+
if (e instanceof PulseInputError)
|
|
273
|
+
return c.json({ error: e.message }, 400);
|
|
274
|
+
throw e;
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
app.get("/requests/:rid/preflight/:run", async (c) => {
|
|
278
|
+
const id = rid(c);
|
|
279
|
+
const run = await preflight.get(id, c.req.param("run"));
|
|
280
|
+
if (!run)
|
|
281
|
+
throw new NotFoundError(c.req.param("run"));
|
|
282
|
+
return c.json(await preflight.withInputMatch(id, run));
|
|
283
|
+
});
|
|
284
|
+
// 画面資料の画像(PNG / JPEG)。要望 / run / 資料 ID で決め、パスは受け取らない。原稿(HTML)は配信しない
|
|
285
|
+
app.get("/requests/:rid/preflight/:run/ui/:material", async (c) => {
|
|
286
|
+
const { data, type } = await preflight.uiImage(rid(c), c.req.param("run"), c.req.param("material"));
|
|
287
|
+
return c.body(new Uint8Array(data), 200, {
|
|
288
|
+
"Content-Type": type,
|
|
289
|
+
"X-Content-Type-Options": "nosniff",
|
|
290
|
+
"Content-Security-Policy": "default-src 'none'",
|
|
291
|
+
"Cache-Control": "private, max-age=3600",
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
// 添付(画像・xlsx)。原本はダウンロードとして返す(画像も画面には ui の資料として出す)
|
|
295
|
+
app.get("/requests/:rid/attachments", async (c) => c.json({ ...(await ws.attachments.list(rid(c))), limits: ATTACHMENT_LIMITS }));
|
|
296
|
+
app.post("/requests/:rid/attachments", bodyLimit({ maxSize: ATTACHMENT_LIMITS.xlsxBytes + 1024 * 1024, onError: (c) => c.json({ error: "大きすぎます" }, 413) }), async (c) => {
|
|
297
|
+
const id = rid(c);
|
|
298
|
+
const form = await c.req.parseBody();
|
|
299
|
+
const file = form.file;
|
|
300
|
+
if (!(file instanceof File))
|
|
301
|
+
return c.json({ error: "file(ファイル)を付けてください" }, 400);
|
|
302
|
+
const text = (v) => (typeof v === "string" ? v : undefined);
|
|
303
|
+
try {
|
|
304
|
+
const a = await preflight.addAttachment(id, { data: Buffer.from(await file.arrayBuffer()), name: file.name, description: text(form.description), source: text(form.source) }, actorOf(c));
|
|
305
|
+
return c.json(a, 201);
|
|
306
|
+
}
|
|
307
|
+
catch (e) {
|
|
308
|
+
if (e instanceof AttachmentError)
|
|
309
|
+
return c.json({ error: e.message }, 400);
|
|
310
|
+
throw e;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
app.get("/requests/:rid/attachments/:aid", async (c) => {
|
|
314
|
+
const { meta, data } = await ws.attachments.content(rid(c), c.req.param("aid")).catch((e) => {
|
|
315
|
+
throw e instanceof AttachmentError ? new NotFoundError(e.message) : e;
|
|
316
|
+
});
|
|
317
|
+
return download(c, data, meta.format, meta.name);
|
|
318
|
+
});
|
|
319
|
+
app.delete("/requests/:rid/attachments/:aid", async (c) => {
|
|
320
|
+
try {
|
|
321
|
+
await preflight.removeAttachment(rid(c), c.req.param("aid"));
|
|
322
|
+
}
|
|
323
|
+
catch (e) {
|
|
324
|
+
throw e instanceof AttachmentError ? new NotFoundError(e.message) : e;
|
|
325
|
+
}
|
|
326
|
+
return c.json({ ok: true });
|
|
327
|
+
});
|
|
328
|
+
app.get("/requests/:rid/preflight/:run/attachments/:aid", async (c) => {
|
|
329
|
+
const { meta, data } = await preflight.fixedAttachment(rid(c), c.req.param("run"), c.req.param("aid"));
|
|
330
|
+
return download(c, data, meta.format, meta.name);
|
|
331
|
+
});
|
|
332
|
+
app.get("/requests/:rid/preflight/:run/attachments/:aid/extract", async (c) => {
|
|
333
|
+
const text = await preflight.fixedExtract(rid(c), c.req.param("run"), c.req.param("aid"));
|
|
334
|
+
return c.body(text, 200, { "Content-Type": "text/plain; charset=utf-8", "X-Content-Type-Options": "nosniff", "Content-Security-Policy": "default-src 'none'" });
|
|
335
|
+
});
|
|
336
|
+
app.post("/requests/:rid/preflight/:run/cancel", (c) => {
|
|
337
|
+
preflight.cancel(c.req.param("run"));
|
|
338
|
+
return c.json({ ok: true });
|
|
339
|
+
});
|
|
340
|
+
app.patch("/requests/:rid/preflight/:run/drafts/:key", async (c) => {
|
|
341
|
+
// expectedTitle: 編集を始めたときの題(制御用。案には書かない)。今の題と違えば 409 で書かない
|
|
342
|
+
const { expectedTitle, ...patch } = z
|
|
343
|
+
.object({ title: z.string(), description: z.string(), include: z.boolean(), repo: z.string().nullable(), expectedTitle: z.string() })
|
|
344
|
+
.partial()
|
|
345
|
+
.parse(await c.req.json());
|
|
346
|
+
return c.json(await preflight.updateDraft(rid(c), c.req.param("run"), c.req.param("key"), patch, { expectedTitle }));
|
|
347
|
+
});
|
|
348
|
+
/** 子の要望の案(分割の run)を直す。expectedTitle は上と同じ */
|
|
349
|
+
app.patch("/requests/:rid/preflight/:run/children/:key", async (c) => {
|
|
350
|
+
const { expectedTitle, ...patch } = z
|
|
351
|
+
.object({ title: z.string(), description: z.string(), include: z.boolean(), expectedTitle: z.string() })
|
|
352
|
+
.partial()
|
|
353
|
+
.parse(await c.req.json());
|
|
354
|
+
return c.json(await preflight.updateChild(rid(c), c.req.param("run"), c.req.param("key"), patch, { expectedTitle }));
|
|
355
|
+
});
|
|
356
|
+
app.get("/requests/:rid/preflight/:run/materialize", async (c) => c.json(await preflight.previewMaterialize(rid(c), c.req.param("run"))));
|
|
357
|
+
app.post("/requests/:rid/preflight/:run/materialize", async (c) => {
|
|
358
|
+
const body = z
|
|
359
|
+
.object({ archive: z.array(z.object({ id: z.string(), rev: z.number().int().optional() })).optional(), version: z.string().optional() })
|
|
360
|
+
.parse(await c.req.json().catch(() => ({})));
|
|
361
|
+
const run = await preflight.get(rid(c), c.req.param("run"));
|
|
362
|
+
// 分割の run は子の要望を作る(version: 確認したときの子の案の版。違えば 409 drafts_changed)
|
|
363
|
+
if (run?.children) {
|
|
364
|
+
const r = await preflight.adopt(rid(c), c.req.param("run"), actorOf(c), { version: body.version });
|
|
365
|
+
return c.json(r, r.failed.length ? 207 : 201);
|
|
366
|
+
}
|
|
367
|
+
return c.json(await preflight.materialize(rid(c), c.req.param("run"), actorOf(c), { archive: body.archive }), 201);
|
|
368
|
+
});
|
|
369
|
+
// --- 横断 --------------------------------------------------------------
|
|
370
|
+
app.get("/tasks", (c) => {
|
|
371
|
+
const { status, area, q } = c.req.query();
|
|
372
|
+
return c.json(listTasks(ctx, { status: status?.split(","), area, q }));
|
|
373
|
+
});
|
|
374
|
+
/** 参照・タイトルから対象を特定する(?request= で今の要望を優先) */
|
|
375
|
+
app.get("/resolve", (c) => c.json(resolve(ctx, c.req.query("q") ?? "", c.req.query("request"))));
|
|
376
|
+
// --- events (SSE) ------------------------------------------------------
|
|
377
|
+
app.get("/events", (c) => eventStream(c, (push) => ws.on(push)));
|
|
378
|
+
return app;
|
|
379
|
+
}
|
|
380
|
+
/** 変更の知らせを SSE で流す(subscribe は知らせを受け取る関数を登録し、外す関数を返す)。25 秒ごとに ping */
|
|
381
|
+
export function eventStream(c, subscribe) {
|
|
382
|
+
return streamSSE(c, async (stream) => {
|
|
383
|
+
const queue = [];
|
|
384
|
+
let wake;
|
|
385
|
+
const off = subscribe((e) => {
|
|
386
|
+
queue.push(e);
|
|
387
|
+
wake?.();
|
|
388
|
+
});
|
|
389
|
+
stream.onAbort(() => {
|
|
390
|
+
off();
|
|
391
|
+
wake?.();
|
|
392
|
+
});
|
|
393
|
+
while (!stream.aborted) {
|
|
394
|
+
if (!queue.length) {
|
|
395
|
+
await Promise.race([new Promise((r) => (wake = r)), stream.sleep(25000)]);
|
|
396
|
+
wake = undefined;
|
|
397
|
+
if (!queue.length)
|
|
398
|
+
await stream.writeSSE({ event: "ping", data: "" });
|
|
399
|
+
}
|
|
400
|
+
while (queue.length)
|
|
401
|
+
await stream.writeSSE({ data: JSON.stringify(queue.shift()) });
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { THEMES } from "../../shared/types.js";
|
|
4
|
+
import { readTheme, writeTheme } from "../../core/userTheme.js";
|
|
5
|
+
import { localOnly } from "./local.js";
|
|
6
|
+
/**
|
|
7
|
+
* /api/user-theme。個人設定のテーマだけを読み書きする(ほかの個人設定は返さない・変えない)。
|
|
8
|
+
* GET → { theme }
|
|
9
|
+
* PUT { theme } → { theme }。このマシンのローカルの画面からだけ(ChatGPT の接続と同じ確かめ方)
|
|
10
|
+
*/
|
|
11
|
+
export function userThemeRoutes(onChange = () => { }) {
|
|
12
|
+
const app = new Hono();
|
|
13
|
+
const input = z.object({ theme: z.enum(THEMES) }).strict();
|
|
14
|
+
app.get("/", async (c) => {
|
|
15
|
+
c.header("cache-control", "no-store");
|
|
16
|
+
return c.json({ theme: await readTheme() });
|
|
17
|
+
});
|
|
18
|
+
app.put("/", async (c) => {
|
|
19
|
+
const why = localOnly(c, true, "画面のテーマ");
|
|
20
|
+
if (why)
|
|
21
|
+
return c.json({ error: why }, 403);
|
|
22
|
+
const parsed = input.safeParse(await c.req.json().catch(() => undefined));
|
|
23
|
+
if (!parsed.success)
|
|
24
|
+
return c.json({ error: `{ "theme": "${THEMES.join('" | "')}" } だけを送ってください` }, 400);
|
|
25
|
+
const theme = await writeTheme(parsed.data.theme);
|
|
26
|
+
onChange();
|
|
27
|
+
return c.json({ theme });
|
|
28
|
+
});
|
|
29
|
+
return app;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 最初の描画の前に読むスクリプト(index.html の先頭。GET /api/user-theme.js)。個人設定のテーマだけを埋め込み、
|
|
33
|
+
* html 要素に data-theme・class "dark"・color-scheme を付ける。規則は web/lib/theme.ts の applyTheme と同じ。
|
|
34
|
+
* 個人設定が読めなくても、画面は system で起動できるようにする(エラーはスクリプトの中のコメントに残す)。
|
|
35
|
+
*/
|
|
36
|
+
export function themeScript(theme, note) {
|
|
37
|
+
return `${note ? `/* ${note.replace(/\*\//g, "* /")} */\n` : ""}(function (t) {
|
|
38
|
+
var r = document.documentElement, m = false;
|
|
39
|
+
try { m = window.matchMedia("(prefers-color-scheme: dark)").matches; } catch (e) {}
|
|
40
|
+
var d = t === "dark" || (t === "system" && m);
|
|
41
|
+
r.dataset.theme = t;
|
|
42
|
+
r.classList.toggle("dark", d);
|
|
43
|
+
r.style.colorScheme = d ? "dark" : "light";
|
|
44
|
+
})(${JSON.stringify(theme)});
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
export function userThemeScriptRoute() {
|
|
48
|
+
const app = new Hono();
|
|
49
|
+
app.get("/", async (c) => {
|
|
50
|
+
c.header("content-type", "text/javascript; charset=utf-8");
|
|
51
|
+
c.header("cache-control", "no-store");
|
|
52
|
+
try {
|
|
53
|
+
return c.body(themeScript(await readTheme()));
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
return c.body(themeScript("system", `個人設定のテーマを読めませんでした: ${e.message}`));
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return app;
|
|
60
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { serve } from "@hono/node-server";
|
|
2
|
+
import { serveStatic } from "@hono/node-server/serve-static";
|
|
3
|
+
import { Hono } from "hono";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { relative } from "node:path";
|
|
6
|
+
import { apiRoutes } from "./api/routes.js";
|
|
7
|
+
import { projectsRoutes } from "./api/projectsRoutes.js";
|
|
8
|
+
import { chatgptAccount } from "../core/chatgpt.js";
|
|
9
|
+
import { openProject, P3_ROOT } from "../core/context.js";
|
|
10
|
+
import { machineSettingsSource } from "../core/machineSettings.js";
|
|
11
|
+
import { ProjectRegistry } from "../core/projectRegistry.js";
|
|
12
|
+
/** 1つのプロジェクトの API(通常の serve)。/api の下 */
|
|
13
|
+
export function singleProjectApp(ctx) {
|
|
14
|
+
const app = new Hono();
|
|
15
|
+
app.get("/mode", (c) => c.json({ mode: "single", project: ctx.projectName ?? null }));
|
|
16
|
+
app.route("/", apiRoutes(ctx));
|
|
17
|
+
// 無い API は画面(index.html)に流さない
|
|
18
|
+
app.all("*", (c) => c.json({ error: `API がありません: ${c.req.method} ${new URL(c.req.url).pathname}` }, 404));
|
|
19
|
+
return app;
|
|
20
|
+
}
|
|
21
|
+
/** ブラウザ用のローカルサーバ。ファイルを監視して画面へ流すビューア兼エディタ */
|
|
22
|
+
export async function startServer(opts) {
|
|
23
|
+
const app = new Hono();
|
|
24
|
+
let ctx;
|
|
25
|
+
let registry;
|
|
26
|
+
if (opts.allProjects) {
|
|
27
|
+
const settings = !opts.settings ? machineSettingsSource() : "get" in opts.settings ? opts.settings : { path: "", get: async () => opts.settings };
|
|
28
|
+
registry = new ProjectRegistry({ settings, chatgpt: chatgptAccount() });
|
|
29
|
+
await registry.refresh();
|
|
30
|
+
app.route("/api", projectsRoutes(registry));
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (!opts.dataDir)
|
|
34
|
+
throw new Error("serve: データのフォルダが決まっていません");
|
|
35
|
+
ctx = await openProject(opts.dataDir, { repos: opts.repos, settings: opts.settings, projectName: opts.projectName });
|
|
36
|
+
void ctx.ws.watch();
|
|
37
|
+
app.route("/api", singleProjectApp(ctx));
|
|
38
|
+
}
|
|
39
|
+
if (opts.static) {
|
|
40
|
+
// serveStatic の root はカレントからの相対で解釈される
|
|
41
|
+
const root = relative(process.cwd(), `${P3_ROOT}dist/web`) || ".";
|
|
42
|
+
app.use("/*", serveStatic({ root }));
|
|
43
|
+
app.get("*", serveStatic({ path: `${root}/index.html` }));
|
|
44
|
+
}
|
|
45
|
+
const server = serve({ fetch: app.fetch, port: opts.port, hostname: opts.host }, () => {
|
|
46
|
+
if (registry) {
|
|
47
|
+
const l = registry.list();
|
|
48
|
+
console.log("p3: プロジェクトを跨ぐ serve(--all-projects)");
|
|
49
|
+
console.log(` projects ${l.projects.map((p) => p.name).join(", ") || "(登録なし。p3 init / p3 join で登録して、画面の一覧を読み直す)"}`);
|
|
50
|
+
if (l.configError)
|
|
51
|
+
console.log(` ⚠ ${l.configError}`);
|
|
52
|
+
for (const i of l.invalid)
|
|
53
|
+
console.log(` ⚠ 使えない登録 ${i.name}: ${i.reason}`);
|
|
54
|
+
}
|
|
55
|
+
else if (ctx) {
|
|
56
|
+
console.log(`p3: ${ctx.ws.project.name}`);
|
|
57
|
+
console.log(` data ${opts.dataDir}`);
|
|
58
|
+
console.log(` repos ${ctx.ws.project.repos.map((r) => r.name).join(", ") || "(なし)"}`);
|
|
59
|
+
console.log(` engines ${ctx.preflight.availableEngines().join(", ")} (default: ${ctx.preflight.defaultEngine})`);
|
|
60
|
+
}
|
|
61
|
+
console.log(` listen http://${opts.host}:${opts.port}${opts.static ? "" : " (api only)"}`);
|
|
62
|
+
if (opts.static && !existsSync(`${P3_ROOT}dist/web/index.html`)) {
|
|
63
|
+
console.log(` ⚠ 画面がビルドされていません。${P3_ROOT} で \`pnpm build\` を実行してください`);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
async close({ timeoutMs = 10_000 } = {}) {
|
|
68
|
+
// 新しい接続を断り、開いている接続(SSE)を閉じる
|
|
69
|
+
const closed = new Promise((r) => server.close(() => r()));
|
|
70
|
+
server.closeAllConnections?.();
|
|
71
|
+
let pending = 0;
|
|
72
|
+
if (registry)
|
|
73
|
+
pending = (await registry.close({ timeoutMs })).pending;
|
|
74
|
+
else if (ctx) {
|
|
75
|
+
const c = ctx;
|
|
76
|
+
const settle = Promise.all([c.preflight.settle({ cancel: true }), c.releaseGenerator.settle({ cancel: true })]).then(() => false);
|
|
77
|
+
const timedOut = await Promise.race([settle, new Promise((r) => setTimeout(() => r(true), timeoutMs).unref())]);
|
|
78
|
+
c.ws.close();
|
|
79
|
+
pending = timedOut ? c.preflight.running() + c.releaseGenerator.running() : 0;
|
|
80
|
+
}
|
|
81
|
+
await Promise.race([closed, new Promise((r) => setTimeout(r, 1000).unref())]);
|
|
82
|
+
return { pending };
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** 今日(ローカル時刻の日付 YYYY-MM-DD。UTC だと日本の朝は前日になる) */
|
|
2
|
+
export function localToday(now = new Date()) {
|
|
3
|
+
const p2 = (n) => String(n).padStart(2, "0");
|
|
4
|
+
return `${now.getFullYear()}-${p2(now.getMonth() + 1)}-${p2(now.getDate())}`;
|
|
5
|
+
}
|
|
6
|
+
export const isDate = (s) => /^\d{4}-\d{2}-\d{2}$/.test(s);
|
|
7
|
+
/** 期限までの日数(過ぎていれば負) */
|
|
8
|
+
export function daysUntil(due, today = localToday()) {
|
|
9
|
+
return Math.round((Date.parse(`${due}T00:00:00Z`) - Date.parse(`${today}T00:00:00Z`)) / 86_400_000);
|
|
10
|
+
}
|
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import type { PreflightRun } from "./types.ts";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Preflight を通さずにタスクを直接作る道(画面の「+ タスクを追加」・CLI の p3 new)の案内。
|
|
5
3
|
* 小さく内容のはっきりした要望ではふつうに選べる道で、Docker や ChatGPT の接続が無くて Preflight を使えないときにも案内する。
|
|
6
4
|
*/
|
|
7
|
-
|
|
8
5
|
/** 直接作るタスクに付かないもの(画面と CLI で同じ言い方にする) */
|
|
9
|
-
export const DIRECT_TASK_NOTE =
|
|
10
|
-
"Preflight の評価・細分化・根拠の調査・画面イメージは付きません。必要な調査と完了条件を自分で整理して作成してください。";
|
|
11
|
-
|
|
6
|
+
export const DIRECT_TASK_NOTE = "Preflight の評価・細分化・根拠の調査・画面イメージは付きません。必要な調査と完了条件を自分で整理して作成してください。";
|
|
12
7
|
/** 後から Preflight を使えるようになったとき(既にあるタスクとの関係) */
|
|
13
|
-
export const LATER_PREFLIGHT_NOTE =
|
|
14
|
-
"後から Preflight を使えるようになったら、この要望に実行できます。実行しても既にあるタスクは変わらず、案をタスク化すると新しいタスクが増えるので、重複を確かめて採否を選んでください。";
|
|
15
|
-
|
|
8
|
+
export const LATER_PREFLIGHT_NOTE = "後から Preflight を使えるようになったら、この要望に実行できます。実行しても既にあるタスクは変わらず、案をタスク化すると新しいタスクが増えるので、重複を確かめて採否を選んでください。";
|
|
16
9
|
/** 取り消した Preflight の記録のエラー(失敗とは扱わない) */
|
|
17
10
|
export const PREFLIGHT_CANCELLED = "中止しました";
|
|
18
|
-
|
|
19
11
|
/** Preflight が失敗した(取消は含まない)。直接作る道を案内する */
|
|
20
|
-
export const preflightFailed = (run
|
|
12
|
+
export const preflightFailed = (run) => run.status === "failed" && run.error !== PREFLIGHT_CANCELLED;
|