@vellym/wbs 0.0.0-stage → 0.2.0-beta

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/node.js ADDED
@@ -0,0 +1,876 @@
1
+ // src/node.ts
2
+ import { definePlugin } from "@vellym/plugin-api";
3
+
4
+ // ../ticket-engine/dist/definitions.js
5
+ import { SUMMARY_METHODS } from "@vellym/plugin-api";
6
+ var TICKET_KIND = "Ticket";
7
+ var TRACKER_KIND = "TicketTracker";
8
+ var VALUE_TYPES = [
9
+ "text",
10
+ "multiline",
11
+ "number",
12
+ "boolean",
13
+ "date",
14
+ "select",
15
+ "multiselect",
16
+ "reference"
17
+ ];
18
+ function asArray(value) {
19
+ return Array.isArray(value) ? value : [];
20
+ }
21
+ function asString(value) {
22
+ return typeof value === "string" && value.length > 0 ? value : void 0;
23
+ }
24
+ function readTemplate(value) {
25
+ if (typeof value === "string")
26
+ return value;
27
+ if (typeof value !== "object" || value === null || Array.isArray(value))
28
+ return void 0;
29
+ const entries = Object.entries(value);
30
+ if (!entries.length || entries.some(([, text]) => typeof text !== "string"))
31
+ return void 0;
32
+ return Object.fromEntries(entries);
33
+ }
34
+ function folderOf(relativePath) {
35
+ const index = relativePath.lastIndexOf("/");
36
+ return index === -1 ? "" : relativePath.slice(0, index);
37
+ }
38
+ function isExcluded(relativePath) {
39
+ return relativePath.split("/").slice(0, -1).some((segment) => segment.startsWith("_"));
40
+ }
41
+ function readTracker(record) {
42
+ const statuses = [];
43
+ for (const entry of asArray(record.spec.statuses)) {
44
+ if (typeof entry !== "object" || entry === null)
45
+ continue;
46
+ const item = entry;
47
+ const id = asString(item.id);
48
+ const category = item.category;
49
+ if (!id || category !== "open" && category !== "closed")
50
+ continue;
51
+ statuses.push({ id, label: asString(item.label) ?? id, category });
52
+ }
53
+ const fields = [];
54
+ for (const entry of asArray(record.spec.fields)) {
55
+ if (typeof entry !== "object" || entry === null)
56
+ continue;
57
+ const field = parseFieldDefinition(entry);
58
+ if (field)
59
+ fields.push(field);
60
+ }
61
+ const rawDefaultSort = record.spec.defaultSort;
62
+ const defaultSort = typeof rawDefaultSort === "object" && rawDefaultSort !== null && !Array.isArray(rawDefaultSort) ? resolveDefaultSort(rawDefaultSort, fields) : void 0;
63
+ const rawHierarchy = record.spec.hierarchy;
64
+ const hierarchy = typeof rawHierarchy === "object" && rawHierarchy !== null && !Array.isArray(rawHierarchy) ? rawHierarchy : {};
65
+ const parentField = typeof hierarchy.parentField === "string" && fields.some((field) => field.id === hierarchy.parentField && field.type === "reference" && !field.multiple) ? hierarchy.parentField : void 0;
66
+ const ticketTemplate = readTemplate(record.spec.ticketTemplate);
67
+ return {
68
+ name: record.name,
69
+ title: record.title,
70
+ folder: folderOf(record.relativePath),
71
+ statuses,
72
+ fields,
73
+ ...Array.isArray(record.spec.summary) ? { summary: record.spec.summary.flatMap((raw) => {
74
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw))
75
+ return [];
76
+ const entry = raw;
77
+ const field = fields.find((item) => item.id === entry.field);
78
+ const groupBy = fields.find((item) => item.id === entry.groupBy && ["select", "multiselect", "reference", "text"].includes(item.type));
79
+ return field && (entry.groupBy === void 0 || groupBy) && typeof entry.method === "string" && SUMMARY_METHODS[field.type].includes(entry.method) ? [{ field, method: entry.method, ...groupBy ? { groupBy } : {} }] : [];
80
+ }) } : {},
81
+ ...ticketTemplate !== void 0 ? { ticketTemplate } : {},
82
+ ...defaultSort ? { defaultSort } : {},
83
+ ...parentField ? { parentField } : {}
84
+ };
85
+ }
86
+ function parseFieldDefinition(item) {
87
+ const id = asString(item.id);
88
+ const type = item.type;
89
+ if (!id || typeof type !== "string" || !VALUE_TYPES.includes(type)) {
90
+ return void 0;
91
+ }
92
+ const listColumn = item.listColumn === true;
93
+ const options = [];
94
+ for (const option of asArray(item.options)) {
95
+ if (typeof option !== "object" || option === null)
96
+ continue;
97
+ const value = asString(option.value);
98
+ if (value === void 0)
99
+ continue;
100
+ options.push({
101
+ value,
102
+ label: asString(option.label) ?? value
103
+ });
104
+ }
105
+ return {
106
+ id,
107
+ label: asString(item.label) ?? id,
108
+ type,
109
+ required: item.required === true,
110
+ listColumn,
111
+ filter: typeof item.filter === "boolean" ? item.filter : listColumn,
112
+ sort: typeof item.sort === "boolean" ? item.sort : listColumn,
113
+ ...options.length ? { options } : {},
114
+ // reference専用の修飾。他の型で宣言してもここではfalseにし、diagnosticsで警告する
115
+ multiple: type === "reference" && item.multiple === true
116
+ };
117
+ }
118
+ function resolveDefaultSort(item, fields) {
119
+ const fieldId = asString(item.field);
120
+ if (!fieldId)
121
+ return void 0;
122
+ const field = fields.find((candidate) => candidate.id === fieldId);
123
+ if (!field || !field.sort)
124
+ return void 0;
125
+ if (item.direction === void 0)
126
+ return { field, direction: "asc" };
127
+ if (item.direction !== "asc" && item.direction !== "desc")
128
+ return void 0;
129
+ return { field, direction: item.direction };
130
+ }
131
+ function readTrackers(context) {
132
+ return context.records(TRACKER_KIND).map(readTracker).sort((left, right) => left.name.localeCompare(right.name));
133
+ }
134
+ function trackerDefinitionDiagnostics(records) {
135
+ const diagnostics = [];
136
+ for (const record of records) {
137
+ if (record.spec.ticketTemplate !== void 0 && readTemplate(record.spec.ticketTemplate) === void 0) {
138
+ diagnostics.push({
139
+ file: record.relativePath,
140
+ path: "/spec/ticketTemplate",
141
+ severity: "warning",
142
+ code: "TICKET_TEMPLATE_INVALID",
143
+ message: {
144
+ ja: "\u672C\u6587\u30C6\u30F3\u30D7\u30EC\u30FC\u30C8\u306B\u306F\u6587\u5B57\u5217\u307E\u305F\u306F\u8A00\u8A9E\u5225\u306E\u6587\u5B57\u5217\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044",
145
+ en: "The ticket template must be a string or a map of localized strings."
146
+ }
147
+ });
148
+ }
149
+ const fields = asArray(record.spec.fields);
150
+ if (record.spec.summary !== void 0) {
151
+ if (!Array.isArray(record.spec.summary)) {
152
+ diagnostics.push({ file: record.relativePath, path: "/spec/summary", severity: "warning", code: "TICKET_SUMMARY_INVALID", message: { ja: "\u96C6\u8A08\u306F\u9805\u76EE\u3068\u65B9\u5F0F\u306E\u914D\u5217\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044", en: "Summaries must be an array of fields and methods." } });
153
+ } else
154
+ record.spec.summary.forEach((raw, index) => {
155
+ const entry = typeof raw === "object" && raw !== null && !Array.isArray(raw) ? raw : {};
156
+ const field = fields.find((value) => typeof value === "object" && value !== null && value.id === entry.field);
157
+ const type = field?.type;
158
+ const group = fields.find((value) => typeof value === "object" && value !== null && value.id === entry.groupBy);
159
+ if (!field || !VALUE_TYPES.includes(type) || typeof entry.method !== "string" || !SUMMARY_METHODS[type].includes(entry.method) || entry.groupBy !== void 0 && (!group || !["select", "multiselect", "reference", "text"].includes(String(group.type)))) {
160
+ diagnostics.push({ file: record.relativePath, path: `/spec/summary/${index}`, severity: "warning", code: "TICKET_SUMMARY_UNSUPPORTED", message: { ja: `\u96C6\u8A08\u306E\u9805\u76EE\u307E\u305F\u306F\u65B9\u5F0F\u3092\u78BA\u8A8D\u3057\u3066\u304F\u3060\u3055\u3044: ${String(entry.field ?? "")}`, en: `Check the summary field and method: ${String(entry.field ?? "")}` } });
161
+ }
162
+ });
163
+ }
164
+ if (record.spec.defaultSort !== void 0) {
165
+ const raw = record.spec.defaultSort;
166
+ const parsedFields = fields.flatMap((entry) => typeof entry === "object" && entry !== null ? [parseFieldDefinition(entry)] : []).filter((field) => field !== void 0);
167
+ const resolved = typeof raw === "object" && raw !== null && !Array.isArray(raw) ? resolveDefaultSort(raw, parsedFields) : void 0;
168
+ if (!resolved) {
169
+ diagnostics.push({
170
+ file: record.relativePath,
171
+ path: "/spec/defaultSort",
172
+ severity: "warning",
173
+ code: "TICKET_DEFAULT_SORT_INVALID",
174
+ message: {
175
+ ja: "\u65E2\u5B9A\u306E\u4E26\u3079\u66FF\u3048\u306B\u306F\u3001sort\u3092\u6709\u52B9\u306B\u3057\u305F\u5B9A\u7FA9\u6E08\u307F\u306E\u9805\u76EE\u3068asc\u307E\u305F\u306Fdesc\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044",
176
+ en: "The default sort must name a defined field with sorting enabled, with asc or desc."
177
+ }
178
+ });
179
+ }
180
+ }
181
+ const hierarchy = record.spec.hierarchy;
182
+ if (typeof hierarchy === "object" && hierarchy !== null && !Array.isArray(hierarchy)) {
183
+ const parentField = hierarchy.parentField;
184
+ if (parentField !== void 0 && parentField !== null && parentField !== "" && !fields.some((entry) => typeof entry === "object" && entry !== null && entry.id === parentField && entry.type === "reference" && entry.multiple !== true)) {
185
+ diagnostics.push({
186
+ file: record.relativePath,
187
+ path: "/spec/hierarchy/parentField",
188
+ severity: "warning",
189
+ code: "TICKET_PARENT_FIELD_INVALID",
190
+ message: {
191
+ ja: "\u89AA\u9805\u76EE\u306B\u306F\u5B9A\u7FA9\u6E08\u307F\u306E\u53C2\u7167\u9805\u76EE\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044",
192
+ en: "The parent field must reference a defined field."
193
+ }
194
+ });
195
+ }
196
+ }
197
+ fields.forEach((entry, index) => {
198
+ if (typeof entry !== "object" || entry === null)
199
+ return;
200
+ const item = entry;
201
+ const id = asString(item.id);
202
+ if (item.multiple !== void 0 && (item.type !== "reference" || item.multiple !== true)) {
203
+ diagnostics.push({
204
+ file: record.relativePath,
205
+ path: `/spec/fields/${index}/multiple`,
206
+ severity: "warning",
207
+ code: "TICKET_FIELD_MULTIPLE_UNSUPPORTED",
208
+ message: {
209
+ ja: `\u8907\u6570\u5024\u306B\u3067\u304D\u308B\u306E\u306F\u53C2\u7167\u9805\u76EE\u3060\u3051\u3067\u3059${id ? `: ${id}` : ""}`,
210
+ en: `Only reference fields can hold multiple values${id ? `: ${id}` : ""}`
211
+ }
212
+ });
213
+ }
214
+ if (item.type !== "select" && item.type !== "multiselect")
215
+ return;
216
+ const validOptions = asArray(item.options).filter((option) => {
217
+ if (typeof option !== "object" || option === null)
218
+ return false;
219
+ return asString(option.value) !== void 0;
220
+ });
221
+ if (validOptions.length > 0)
222
+ return;
223
+ diagnostics.push({
224
+ file: record.relativePath,
225
+ path: `/spec/fields/${index}/options`,
226
+ severity: "warning",
227
+ code: "TICKET_FIELD_OPTIONS_MISSING",
228
+ message: {
229
+ ja: `\u9078\u629E\u5F0F\u306E\u9805\u76EE\u306B\u9078\u629E\u80A2\u304C\u3042\u308A\u307E\u305B\u3093${id ? `: ${id}` : ""}`,
230
+ en: `Select fields must have options${id ? `: ${id}` : ""}`
231
+ }
232
+ });
233
+ });
234
+ }
235
+ return diagnostics;
236
+ }
237
+ function trackersFor(trackers, relativePath) {
238
+ if (isExcluded(relativePath))
239
+ return [];
240
+ const folder = folderOf(relativePath);
241
+ return trackers.filter((tracker) => tracker.folder === "" || folder === tracker.folder || folder.startsWith(`${tracker.folder}/`)).sort((left, right) => right.folder.length - left.folder.length);
242
+ }
243
+ function trackerFor(trackers, relativePath) {
244
+ return trackersFor(trackers, relativePath)[0];
245
+ }
246
+ function statusById(tracker, id) {
247
+ return tracker.statuses.find((status) => status.id === id);
248
+ }
249
+ function fieldById(tracker, id) {
250
+ return tracker.fields.find((field) => field.id === id);
251
+ }
252
+ function initialFieldValue(field) {
253
+ switch (field.type) {
254
+ case "select":
255
+ return field.options?.[0]?.value;
256
+ case "multiselect":
257
+ return [];
258
+ case "reference":
259
+ return field.multiple ? [] : void 0;
260
+ case "boolean":
261
+ return false;
262
+ default:
263
+ return void 0;
264
+ }
265
+ }
266
+ function initialTicketSpec(tracker) {
267
+ if (!tracker)
268
+ return {};
269
+ const fields = {};
270
+ for (const field of tracker.fields) {
271
+ const value = initialFieldValue(field);
272
+ if (value !== void 0)
273
+ fields[field.id] = value;
274
+ }
275
+ const status = tracker.statuses[0]?.id;
276
+ return {
277
+ ...status ? { status } : {},
278
+ ...Object.keys(fields).length ? { fields } : {}
279
+ };
280
+ }
281
+
282
+ // ../ticket-engine/dist/ulid.js
283
+ var ALPHABET = "0123456789abcdefghjkmnpqrstvwxyz";
284
+ function ulid(now = Date.now()) {
285
+ let time = "";
286
+ let remaining = now;
287
+ for (let index = 0; index < 10; index += 1) {
288
+ time = `${ALPHABET[remaining % 32]}${time}`;
289
+ remaining = Math.floor(remaining / 32);
290
+ }
291
+ const bytes = new Uint8Array(16);
292
+ globalThis.crypto.getRandomValues(bytes);
293
+ let random = "";
294
+ for (const byte of bytes) {
295
+ random += ALPHABET[byte % 32];
296
+ }
297
+ return `${time}${random}`;
298
+ }
299
+ function ticketName(now = Date.now()) {
300
+ return `ticket-${ulid(now)}`;
301
+ }
302
+ function trackerName(title, now = Date.now()) {
303
+ const base = title.normalize("NFKC").toLocaleLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 40);
304
+ const suffix = ulid(now).slice(-6);
305
+ return base ? `${base}-${suffix}` : `ticket-tracker-${suffix}`;
306
+ }
307
+
308
+ // ../ticket-engine/dist/commands.js
309
+ var CREATE_TICKET_COMMAND_ID = "ticket.create";
310
+ var CREATE_TRACKER_COMMAND_ID = "ticket.create-tracker";
311
+ function createTicketCommand() {
312
+ return {
313
+ id: CREATE_TICKET_COMMAND_ID,
314
+ title: { ja: "\u30C1\u30B1\u30C3\u30C8\u3092\u4F5C\u6210", en: "New ticket" },
315
+ static: false,
316
+ run: (context) => {
317
+ const trackers = readTrackers(context);
318
+ const tracker = context.target ? trackerFor(trackers, context.target.relativePath) ?? trackers.find((item) => item.name === context.target?.name) : void 0;
319
+ const title = typeof context.input?.title === "string" ? context.input.title.trim() : "";
320
+ const initial = initialTicketSpec(tracker);
321
+ const template = tracker?.ticketTemplate;
322
+ const description = typeof template === "string" ? template : template ? template[context.locale === "ja" ? "ja" : "en"] ?? Object.values(template)[0] ?? "" : "";
323
+ const fields = { ...initial.fields };
324
+ const parent = context.input?.parent;
325
+ const parentField = context.input?.parentField;
326
+ if (typeof parent === "string" && parent && typeof parentField === "string" && tracker?.fields.some((field) => field.id === parentField && field.type === "reference")) {
327
+ fields[parentField] = parent;
328
+ }
329
+ for (const field of tracker?.fields ?? []) {
330
+ if (!field.required)
331
+ continue;
332
+ const value = context.input?.[field.id];
333
+ if (value !== void 0)
334
+ fields[field.id] = value;
335
+ }
336
+ return context.createResource({
337
+ kind: TICKET_KIND,
338
+ name: ticketName(),
339
+ title: title || (context.locale === "ja" ? "\u65B0\u3057\u3044\u30C1\u30B1\u30C3\u30C8" : "New ticket"),
340
+ ...tracker ? { folder: tracker.folder ? `${tracker.folder}/+tickets` : "+tickets" } : {},
341
+ spec: {
342
+ ...initial,
343
+ ...Object.keys(fields).length ? { fields } : {},
344
+ blocks: [{ id: "description", type: "rich-text", content: description }]
345
+ }
346
+ });
347
+ }
348
+ };
349
+ }
350
+ function createTrackerCommand(settingsViewId) {
351
+ return {
352
+ id: CREATE_TRACKER_COMMAND_ID,
353
+ title: { ja: "\u30C1\u30B1\u30C3\u30C8\u7BA1\u7406", en: "Ticket tracker" },
354
+ static: false,
355
+ placement: "document-tree",
356
+ inputs: [
357
+ {
358
+ id: "title",
359
+ label: { ja: "\u540D\u524D", en: "Name" },
360
+ type: "text",
361
+ path: ["title"],
362
+ required: true
363
+ }
364
+ ],
365
+ run: (context) => {
366
+ const title = typeof context.input?.title === "string" ? context.input.title.trim() : "";
367
+ if (!title) {
368
+ return {
369
+ ok: false,
370
+ diagnostics: [
371
+ {
372
+ file: ".",
373
+ severity: "warning",
374
+ code: "TICKET_TRACKER_TITLE_REQUIRED",
375
+ message: { ja: "\u540D\u524D\u3092\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044", en: "Enter a name." }
376
+ }
377
+ ]
378
+ };
379
+ }
380
+ const requested = typeof context.input?.folder === "string" ? context.input.folder.trim() : "";
381
+ const folder = requested || (context.target ? folderOf(context.target.relativePath) : "");
382
+ return context.createResource({
383
+ kind: TRACKER_KIND,
384
+ name: trackerName(title),
385
+ title,
386
+ ...folder ? { folder } : {},
387
+ spec: {
388
+ statuses: [
389
+ { id: "todo", label: "\u672A\u7740\u624B", category: "open" },
390
+ { id: "doing", label: "\u5BFE\u5FDC\u4E2D", category: "open" },
391
+ { id: "done", label: "\u5B8C\u4E86", category: "closed" }
392
+ ],
393
+ fields: []
394
+ }
395
+ }).then((result) => result.ok ? { ...result, openView: settingsViewId } : result);
396
+ }
397
+ };
398
+ }
399
+
400
+ // ../ticket-engine/dist/projection.js
401
+ var TRACKER_KEY = "tracker";
402
+ var TRACKERS_KEY = "trackers";
403
+ var STATUS_KEY = "status";
404
+ var STATUS_CATEGORY_KEY = "statusCategory";
405
+ var STATUS_UNDEFINED_KEY = "statusUndefined";
406
+ var FIELD_KEY_PREFIX = "field:";
407
+ var fieldKey = (id) => `${FIELD_KEY_PREFIX}${id}`;
408
+ var LABELS_KEY = "labels";
409
+ var STATUS_UNSET = "";
410
+ var TRACKER_NONE = "";
411
+ function warn(record, path, code, message) {
412
+ return { file: record.relativePath, path, severity: "warning", code, message };
413
+ }
414
+ function matchesType(field, value) {
415
+ switch (field.type) {
416
+ case "number":
417
+ return typeof value === "number";
418
+ case "boolean":
419
+ return typeof value === "boolean";
420
+ case "multiselect":
421
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
422
+ case "reference":
423
+ if (field.multiple)
424
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
425
+ return typeof value === "string";
426
+ case "date":
427
+ return typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value);
428
+ default:
429
+ return typeof value === "string";
430
+ }
431
+ }
432
+ function toIndexValue(value) {
433
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null) {
434
+ return value;
435
+ }
436
+ if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
437
+ return value;
438
+ }
439
+ return JSON.stringify(value) ?? "";
440
+ }
441
+ function projectTicket(trackers, record) {
442
+ if (isExcluded(record.relativePath))
443
+ return void 0;
444
+ const diagnostics = [];
445
+ const values = {};
446
+ const owners = trackersFor(trackers, record.relativePath);
447
+ const tracker = owners[0];
448
+ values[TRACKER_KEY] = tracker?.name ?? TRACKER_NONE;
449
+ values[TRACKERS_KEY] = owners.map((item) => item.name);
450
+ if (!tracker) {
451
+ diagnostics.push(warn(record, "/kind", "TICKET_TRACKER_MISSING", {
452
+ ja: "\u4E0A\u4F4D\u306BTicketTracker\u304C\u7121\u3044\u305F\u3081\u3001\u3069\u306E\u30C1\u30B1\u30C3\u30C8\u4E00\u89A7\u306B\u3082\u51FA\u307E\u305B\u3093",
453
+ en: "No TicketTracker exists above this ticket, so it will not appear in any ticket list."
454
+ }));
455
+ }
456
+ const labels = Object.entries(record.labels ?? {});
457
+ if (labels.length) {
458
+ values[LABELS_KEY] = labels.map(([key, value]) => `${key}=${value}`);
459
+ }
460
+ const status = record.spec.status;
461
+ if (status === void 0) {
462
+ values[STATUS_KEY] = STATUS_UNSET;
463
+ diagnostics.push(warn(record, "/spec/status", "TICKET_STATUS_MISSING", {
464
+ ja: "\u30B9\u30C6\u30FC\u30BF\u30B9\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093",
465
+ en: "No status is set."
466
+ }));
467
+ } else if (typeof status !== "string") {
468
+ values[STATUS_KEY] = toIndexValue(status);
469
+ diagnostics.push(warn(record, "/spec/status", "TICKET_STATUS_INVALID", {
470
+ ja: "\u30B9\u30C6\u30FC\u30BF\u30B9\u304C\u6587\u5B57\u5217\u3067\u306F\u3042\u308A\u307E\u305B\u3093",
471
+ en: "The status must be a string."
472
+ }));
473
+ } else {
474
+ values[STATUS_KEY] = status;
475
+ const statusDefinition = tracker ? statusById(tracker, status) : void 0;
476
+ if (statusDefinition) {
477
+ values[STATUS_CATEGORY_KEY] = statusDefinition.category;
478
+ } else if (tracker && tracker.statuses.length > 0) {
479
+ values[STATUS_UNDEFINED_KEY] = true;
480
+ diagnostics.push(warn(record, "/spec/status", "TICKET_STATUS_UNDEFINED", {
481
+ ja: `${tracker.title}\u306E\u5B9A\u7FA9\u306B\u7121\u3044\u30B9\u30C6\u30FC\u30BF\u30B9\u3067\u3059: ${status}`,
482
+ en: `Status is not defined in ${tracker.title}: ${status}`
483
+ }));
484
+ }
485
+ }
486
+ const fields = record.spec.fields;
487
+ const entries = typeof fields === "object" && fields !== null && !Array.isArray(fields) ? Object.entries(fields) : [];
488
+ if (fields !== void 0 && typeof fields !== "object") {
489
+ diagnostics.push(warn(record, "/spec/fields", "TICKET_FIELDS_INVALID", {
490
+ ja: "fields\u304C\u5BFE\u5FDC\u8868\u3067\u306F\u3042\u308A\u307E\u305B\u3093",
491
+ en: "fields must be a map."
492
+ }));
493
+ }
494
+ for (const [id, value] of entries) {
495
+ values[fieldKey(id)] = toIndexValue(value);
496
+ if (!tracker)
497
+ continue;
498
+ const definition = fieldById(tracker, id);
499
+ if (!definition) {
500
+ if (tracker.fields.length > 0) {
501
+ diagnostics.push(warn(record, `/spec/fields/${id}`, "TICKET_FIELD_UNDEFINED", {
502
+ ja: `${tracker.title}\u306E\u5B9A\u7FA9\u306B\u7121\u3044\u9805\u76EE\u3067\u3059: ${id}`,
503
+ en: `Field is not defined in ${tracker.title}: ${id}`
504
+ }));
505
+ }
506
+ continue;
507
+ }
508
+ if (!matchesType(definition, value)) {
509
+ diagnostics.push(warn(record, `/spec/fields/${id}`, "TICKET_FIELD_TYPE_MISMATCH", {
510
+ ja: `\u9805\u76EE\u306E\u578B\u304C\u5B9A\u7FA9\u3068\u5408\u3044\u307E\u305B\u3093: ${id}\uFF08\u5B9A\u7FA9: ${definition.type}\uFF09`,
511
+ en: `Field type does not match its definition: ${id} (expected: ${definition.type})`
512
+ }));
513
+ continue;
514
+ }
515
+ if ((definition.type === "select" || definition.type === "multiselect") && definition.options?.length) {
516
+ const allowed = new Set(definition.options.map((option) => option.value));
517
+ const selected = Array.isArray(value) ? value : [value];
518
+ const undefinedValues = selected.filter((item) => !allowed.has(item));
519
+ if (undefinedValues.length) {
520
+ const list = JSON.stringify(undefinedValues);
521
+ diagnostics.push(warn(record, `/spec/fields/${id}`, "TICKET_FIELD_OPTION_UNDEFINED", {
522
+ ja: `\u9805\u76EE\u306E\u9078\u629E\u80A2\u306B\u7121\u3044\u5024\u3067\u3059: ${id}: ${list}`,
523
+ en: `Field has undefined option values: ${id}: ${list}`
524
+ }));
525
+ }
526
+ }
527
+ }
528
+ for (const field of tracker?.fields ?? []) {
529
+ if (!field.required)
530
+ continue;
531
+ const value = entries.find(([id]) => id === field.id)?.[1];
532
+ if (value === void 0 || value === null || value === "") {
533
+ diagnostics.push(warn(record, `/spec/fields/${field.id}`, "TICKET_FIELD_REQUIRED_MISSING", {
534
+ ja: `\u5FC5\u9808\u306E\u9805\u76EE\u304C\u7A7A\u3067\u3059: ${field.id}`,
535
+ en: `Required field is empty: ${field.id}`
536
+ }));
537
+ }
538
+ }
539
+ return diagnostics.length ? { values, diagnostics } : { values };
540
+ }
541
+
542
+ // ../ticket-engine/dist/hierarchy.js
543
+ function buildTicketHierarchy(tracker, rows) {
544
+ const diagnostics = [];
545
+ const nodes = new Map(rows.map((row) => [row.name, { row, children: [] }]));
546
+ const parents = /* @__PURE__ */ new Map();
547
+ if (tracker.parentField) {
548
+ for (const row of rows) {
549
+ const value = row.values[fieldKey(tracker.parentField)];
550
+ if (typeof value !== "string" || !value)
551
+ continue;
552
+ if (!nodes.has(value)) {
553
+ diagnostics.push({
554
+ file: row.name,
555
+ path: `/spec/fields/${tracker.parentField}`,
556
+ severity: "warning",
557
+ code: "TICKET_PARENT_UNRESOLVED",
558
+ message: {
559
+ ja: `\u89AA\u30C1\u30B1\u30C3\u30C8\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093: ${value}`,
560
+ en: `Parent ticket not found: ${value}`
561
+ }
562
+ });
563
+ } else
564
+ parents.set(row.name, value);
565
+ }
566
+ }
567
+ const checked = /* @__PURE__ */ new Set();
568
+ for (const name of nodes.keys()) {
569
+ if (checked.has(name))
570
+ continue;
571
+ const chain = [];
572
+ const positions = /* @__PURE__ */ new Map();
573
+ let cursor = name;
574
+ while (cursor && !checked.has(cursor)) {
575
+ const cycleStart = positions.get(cursor);
576
+ if (cycleStart !== void 0) {
577
+ for (const member of chain.slice(cycleStart)) {
578
+ parents.delete(member);
579
+ diagnostics.push({
580
+ file: member,
581
+ path: `/spec/fields/${tracker.parentField}`,
582
+ severity: "warning",
583
+ code: "TICKET_PARENT_CYCLE",
584
+ message: {
585
+ ja: "\u89AA\u5B50\u53C2\u7167\u304C\u5FAA\u74B0\u3057\u3066\u3044\u307E\u3059",
586
+ en: "The parent relationship contains a cycle."
587
+ }
588
+ });
589
+ }
590
+ break;
591
+ }
592
+ positions.set(cursor, chain.length);
593
+ chain.push(cursor);
594
+ cursor = parents.get(cursor);
595
+ }
596
+ for (const visited of chain)
597
+ checked.add(visited);
598
+ }
599
+ const roots = [];
600
+ for (const [name, node] of nodes) {
601
+ const parent = nodes.get(parents.get(name) ?? "");
602
+ if (parent)
603
+ parent.children.push(node);
604
+ else
605
+ roots.push(node);
606
+ }
607
+ return { roots, diagnostics };
608
+ }
609
+
610
+ // ../ticket-engine/dist/icons.js
611
+ var TICKET_ICON = {
612
+ paths: [
613
+ "M2.75 4.75h10.5v6.5H2.75z",
614
+ "M6 4.75v6.5",
615
+ "M8.25 7.25h3.25",
616
+ "M8.25 9h2"
617
+ ]
618
+ };
619
+ var TICKET_TRACKER_ICON = {
620
+ paths: [
621
+ "M2.75 6.25h8.5v7H2.75z",
622
+ "M5 6.25V3.75h8.5v7h-2.25",
623
+ "M5.25 8.75h3.5",
624
+ "M5.25 10.75h2.5"
625
+ ]
626
+ };
627
+
628
+ // ../ticket-engine/dist/references.js
629
+ function listedIn(row, trackerName2) {
630
+ const listed = row.values[TRACKERS_KEY];
631
+ if (Array.isArray(listed))
632
+ return listed.includes(trackerName2);
633
+ return row.values[TRACKER_KEY] === trackerName2;
634
+ }
635
+ function referenceValues(value) {
636
+ if (typeof value === "string")
637
+ return [value];
638
+ if (Array.isArray(value)) {
639
+ return value.filter((item) => typeof item === "string");
640
+ }
641
+ return [];
642
+ }
643
+ function validateTicketReferences(rows, context) {
644
+ const trackers = readTrackers(context);
645
+ const diagnostics = [];
646
+ for (const tracker of trackers) {
647
+ const listed = rows.filter((row) => listedIn(row, tracker.name));
648
+ const owned = listed.filter((row) => row.values[TRACKER_KEY] === tracker.name);
649
+ if (!listed.length)
650
+ continue;
651
+ const names = new Set(listed.map((row) => row.name));
652
+ const ownedNames = new Set(owned.map((row) => row.name));
653
+ const hierarchy = buildTicketHierarchy(tracker, listed);
654
+ for (const diagnostic of hierarchy.diagnostics) {
655
+ if (ownedNames.has(diagnostic.file))
656
+ diagnostics.push(diagnostic);
657
+ }
658
+ for (const field of tracker.fields) {
659
+ if (field.type !== "reference" || field.id === tracker.parentField)
660
+ continue;
661
+ for (const row of owned) {
662
+ for (const target of referenceValues(row.values[fieldKey(field.id)])) {
663
+ if (!target)
664
+ continue;
665
+ if (target === row.name) {
666
+ diagnostics.push({
667
+ file: row.name,
668
+ path: `/spec/fields/${field.id}`,
669
+ severity: "warning",
670
+ code: "TICKET_REFERENCE_SELF",
671
+ message: {
672
+ ja: `\u81EA\u5206\u81EA\u8EAB\u3092\u53C2\u7167\u3057\u3066\u3044\u307E\u3059: ${target}`,
673
+ en: `The reference points to this ticket itself: ${target}`
674
+ }
675
+ });
676
+ } else if (!names.has(target)) {
677
+ diagnostics.push({
678
+ file: row.name,
679
+ path: `/spec/fields/${field.id}`,
680
+ severity: "warning",
681
+ code: "TICKET_REFERENCE_UNRESOLVED",
682
+ message: {
683
+ ja: `\u53C2\u7167\u5148\u306E\u30C1\u30B1\u30C3\u30C8\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093: ${target}`,
684
+ en: `Referenced ticket not found: ${target}`
685
+ }
686
+ });
687
+ }
688
+ }
689
+ }
690
+ }
691
+ }
692
+ return diagnostics;
693
+ }
694
+
695
+ // ../ticket-engine/dist/schemas.js
696
+ var TICKET_SPEC_SCHEMA = {
697
+ type: "object",
698
+ properties: {
699
+ status: { type: "string" },
700
+ fields: { type: "object" },
701
+ blocks: { type: "array" }
702
+ },
703
+ additionalProperties: true
704
+ };
705
+ var TICKET_TRACKER_SPEC_SCHEMA = {
706
+ type: "object",
707
+ properties: {
708
+ ticketTemplate: {
709
+ anyOf: [
710
+ { type: "string" },
711
+ { type: "object", minProperties: 1, additionalProperties: { type: "string" } }
712
+ ]
713
+ },
714
+ statuses: {
715
+ type: "array",
716
+ items: {
717
+ type: "object",
718
+ required: ["id", "label", "category"],
719
+ properties: {
720
+ id: { type: "string", minLength: 1 },
721
+ label: { type: "string", minLength: 1 },
722
+ category: { enum: ["open", "closed"] }
723
+ },
724
+ additionalProperties: true
725
+ }
726
+ },
727
+ fields: {
728
+ type: "array",
729
+ items: {
730
+ type: "object",
731
+ required: ["id", "label", "type"],
732
+ properties: {
733
+ id: { type: "string", minLength: 1 },
734
+ label: { type: "string", minLength: 1 },
735
+ type: {
736
+ enum: [
737
+ "text",
738
+ "multiline",
739
+ "number",
740
+ "boolean",
741
+ "date",
742
+ "select",
743
+ "multiselect",
744
+ "reference"
745
+ ]
746
+ },
747
+ required: { type: "boolean" },
748
+ listColumn: { type: "boolean" },
749
+ filter: { type: "boolean" },
750
+ sort: { type: "boolean" },
751
+ // reference専用の修飾。他の型で書くとvalidateで警告になる
752
+ multiple: { type: "boolean" },
753
+ options: {
754
+ type: "array",
755
+ items: {
756
+ type: "object",
757
+ required: ["value", "label"],
758
+ properties: {
759
+ value: { type: "string" },
760
+ label: { type: "string" }
761
+ },
762
+ additionalProperties: true
763
+ }
764
+ }
765
+ },
766
+ additionalProperties: true
767
+ }
768
+ },
769
+ defaultSort: {
770
+ type: "object",
771
+ required: ["field"],
772
+ properties: {
773
+ field: { type: "string", minLength: 1 },
774
+ direction: { enum: ["asc", "desc"] }
775
+ },
776
+ additionalProperties: true
777
+ }
778
+ },
779
+ additionalProperties: true
780
+ };
781
+
782
+ // ../ticket-engine/dist/register.js
783
+ var TICKET_ENGINE_VERSION = "1.0.0";
784
+ function registerTicketEngine(host) {
785
+ host.registerKind({
786
+ kind: TICKET_KIND,
787
+ engineVersion: TICKET_ENGINE_VERSION,
788
+ specSchema: TICKET_SPEC_SCHEMA,
789
+ showInDocumentTree: false,
790
+ icon: TICKET_ICON
791
+ });
792
+ host.registerKind({
793
+ kind: TRACKER_KIND,
794
+ engineVersion: TICKET_ENGINE_VERSION,
795
+ specSchema: TICKET_TRACKER_SPEC_SCHEMA,
796
+ showInDocumentTree: true,
797
+ hasBlocks: false,
798
+ icon: TICKET_TRACKER_ICON
799
+ });
800
+ host.registerRecordProjection(TICKET_KIND, (context) => {
801
+ for (const diagnostic of trackerDefinitionDiagnostics(context.records(TRACKER_KIND))) {
802
+ context.reportDiagnostic?.(diagnostic);
803
+ }
804
+ const trackers = readTrackers(context);
805
+ return (record) => projectTicket(trackers, record);
806
+ });
807
+ host.registerRecordSetValidator(TICKET_KIND, validateTicketReferences);
808
+ }
809
+
810
+ // src/view.ts
811
+ var WBS_VIEW_ID = "wbs-tree";
812
+ function wbsView(context) {
813
+ const tracker = context.target?.kind === "TicketTracker" ? readTrackers(context).find((item) => item.name === context.target?.name) : void 0;
814
+ const config = context.target?.spec.wbs;
815
+ const wbs = typeof config === "object" && config !== null && !Array.isArray(config) ? config : {};
816
+ const diagnostics = [];
817
+ const field = (setting, type, fallback) => {
818
+ const id = Object.hasOwn(wbs, setting) ? wbs[setting] : fallback;
819
+ if (id === void 0 || id === null || id === "") return void 0;
820
+ const found = tracker?.fields.find((item) => item.id === id && item.type === type);
821
+ if (found) return found.id;
822
+ diagnostics.push({
823
+ file: context.target?.relativePath ?? ".",
824
+ path: `/spec/wbs/${setting}`,
825
+ severity: "warning",
826
+ code: "WBS_FIELD_INVALID",
827
+ message: {
828
+ ja: `${setting}\u306B\u306F\u5B9A\u7FA9\u6E08\u307F\u306E${type}\u9805\u76EE\u3092\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044: ${String(id)}`,
829
+ en: `Choose a defined ${type} field for ${setting}: ${String(id)}`
830
+ }
831
+ });
832
+ return void 0;
833
+ };
834
+ const parentField = field("parentField", "reference", tracker?.parentField);
835
+ const startField = field("startField", "date");
836
+ const endField = field("endField", "date");
837
+ const orderField = field("orderField", "number");
838
+ const data = {
839
+ ...tracker ? { tracker } : {},
840
+ ...parentField ? { parentField } : {},
841
+ ...startField ? { startField } : {},
842
+ ...endField ? { endField } : {},
843
+ ...orderField ? { orderField } : {},
844
+ statusSymbols: Object.fromEntries((Array.isArray(context.target?.spec.statuses) ? context.target.spec.statuses : []).filter((value) => value !== null && typeof value === "object" && !Array.isArray(value)).filter((value) => typeof value.id === "string" && typeof value.symbol === "string" && value.symbol.trim()).map((value) => [value.id, value.symbol])),
845
+ diagnostics
846
+ };
847
+ return {
848
+ type: "list",
849
+ id: WBS_VIEW_ID,
850
+ kind: "Ticket",
851
+ paperWidth: "full",
852
+ title: tracker?.title ?? { ja: "WBS", en: "WBS" },
853
+ columns: [],
854
+ scopeFilters: [{ columnId: TRACKERS_KEY, operator: "contains", value: tracker?.name ?? "" }],
855
+ rendererData: data
856
+ };
857
+ }
858
+
859
+ // src/node.ts
860
+ var node_default = definePlugin({
861
+ activate(host) {
862
+ registerTicketEngine(host);
863
+ host.registerView({
864
+ id: WBS_VIEW_ID,
865
+ view: wbsView,
866
+ opensKind: "TicketTracker",
867
+ label: { ja: "WBS", en: "WBS" },
868
+ static: true
869
+ });
870
+ host.registerCommand(createTrackerCommand(WBS_VIEW_ID));
871
+ host.registerCommand(createTicketCommand());
872
+ }
873
+ });
874
+ export {
875
+ node_default as default
876
+ };