@cortex-agent/server 2026.6.11 → 2026.6.18-2
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/defaults/config/thread-templates.json +2 -2
- package/defaults/hooks/task-status-check.mjs +9 -7
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +103 -16
- package/defaults/plugins/cortex-system/skills/orient/SKILL.md +6 -6
- package/defaults/plugins/cortex-system/skills/thread/SKILL.md +29 -35
- package/defaults/prompts/directives/manager.md +21 -11
- package/dist/agent-adapter/claude/spawn-args.d.ts +5 -0
- package/dist/agent-adapter/claude/spawn-args.js +6 -0
- package/dist/agent-adapter/types.d.ts +4 -0
- package/dist/core/gateway-generator.js +24 -3
- package/dist/core/i18n.d.ts +19 -0
- package/dist/core/i18n.js +46 -0
- package/dist/core/locales/en.d.ts +438 -0
- package/dist/core/locales/en.js +24 -0
- package/dist/core/locales/slices/commands.d.ts +214 -0
- package/dist/core/locales/slices/commands.js +462 -0
- package/dist/core/locales/slices/init.d.ts +121 -0
- package/dist/core/locales/slices/init.js +380 -0
- package/dist/core/locales/slices/interactions.d.ts +47 -0
- package/dist/core/locales/slices/interactions.js +102 -0
- package/dist/core/locales/slices/lang.d.ts +8 -0
- package/dist/core/locales/slices/lang.js +18 -0
- package/dist/core/locales/slices/scheduling.d.ts +36 -0
- package/dist/core/locales/slices/scheduling.js +74 -0
- package/dist/core/locales/slices/startup.d.ts +6 -0
- package/dist/core/locales/slices/startup.js +14 -0
- package/dist/core/locales/slices/status.d.ts +20 -0
- package/dist/core/locales/slices/status.js +48 -0
- package/dist/core/locales/zh.d.ts +5 -0
- package/dist/core/locales/zh.js +23 -0
- package/dist/core/singleton-lock.d.ts +23 -0
- package/dist/core/singleton-lock.js +53 -0
- package/dist/core/status-format.js +2 -1
- package/dist/core/task-parser.d.ts +6 -0
- package/dist/core/task-parser.js +10 -0
- package/dist/core/types/thread-types.d.ts +29 -0
- package/dist/core/utils.d.ts +9 -1
- package/dist/core/utils.js +9 -1
- package/dist/domain/agents/config.d.ts +2 -1
- package/dist/domain/agents/config.js +25 -6
- package/dist/domain/agents/facade.d.ts +4 -0
- package/dist/domain/agents/facade.js +3 -1
- package/dist/domain/mcp/core-server.js +15 -9
- package/dist/domain/mcp/tools/task-monitor.d.ts +2 -0
- package/dist/domain/mcp/tools/task-monitor.js +99 -0
- package/dist/domain/mcp/tools/thread-ops.js +50 -85
- package/dist/domain/scheduling/jobs/task-dispatch.js +18 -16
- package/dist/domain/scheduling/schedule-command.js +38 -37
- package/dist/domain/system/github-release.d.ts +18 -0
- package/dist/domain/system/github-release.js +106 -0
- package/dist/domain/system/preferences.d.ts +12 -0
- package/dist/domain/system/preferences.js +36 -0
- package/dist/domain/tasks/dispatch-utils.d.ts +5 -0
- package/dist/domain/tasks/dispatch-utils.js +9 -1
- package/dist/domain/tasks/dispatcher.d.ts +11 -2
- package/dist/domain/tasks/dispatcher.js +41 -11
- package/dist/domain/tasks/system/task-cli.d.ts +1 -0
- package/dist/domain/tasks/system/task-cli.js +82 -6
- package/dist/domain/tasks/system/task-mutations.d.ts +6 -1
- package/dist/domain/tasks/system/task-mutations.js +10 -1
- package/dist/domain/threads/hook-runner.js +1 -0
- package/dist/domain/threads/prompt-builder.d.ts +6 -0
- package/dist/domain/threads/prompt-builder.js +34 -11
- package/dist/domain/threads/runner.d.ts +5 -0
- package/dist/domain/threads/runner.js +44 -11
- package/dist/domain/threads/state-machine.d.ts +9 -12
- package/dist/domain/threads/state-machine.js +27 -50
- package/dist/entry/app.js +36 -3
- package/dist/entry/daemon.js +12 -30
- package/dist/entry/init.d.ts +4 -3
- package/dist/entry/init.js +151 -208
- package/dist/entry/startup-notify.js +6 -3
- package/dist/hooks/task-status-check.mjs +9 -7
- package/dist/orchestration/interactions/interaction-handlers.js +12 -11
- package/dist/orchestration/interactions/update-prompt-slack.js +11 -10
- package/dist/orchestration/interactions/update-prompt.js +66 -11
- package/dist/orchestration/lifecycle.js +13 -12
- package/dist/orchestration/routing/commands/cancel.js +10 -9
- package/dist/orchestration/routing/commands/channel.js +27 -26
- package/dist/orchestration/routing/commands/cost.js +10 -2
- package/dist/orchestration/routing/commands/device.js +11 -9
- package/dist/orchestration/routing/commands/dispatch.js +10 -10
- package/dist/orchestration/routing/commands/index.js +6 -2
- package/dist/orchestration/routing/commands/lang.d.ts +3 -0
- package/dist/orchestration/routing/commands/lang.js +31 -0
- package/dist/orchestration/routing/commands/mode.js +28 -27
- package/dist/orchestration/routing/commands/nvtop.js +13 -12
- package/dist/orchestration/routing/commands/orient.js +2 -1
- package/dist/orchestration/routing/commands/schedule.js +23 -22
- package/dist/orchestration/routing/commands/sendfile.js +8 -7
- package/dist/orchestration/routing/commands/session.js +11 -10
- package/dist/orchestration/routing/commands/status.js +78 -71
- package/dist/orchestration/routing/commands/tail.js +6 -5
- package/dist/orchestration/routing/commands/task.js +23 -22
- package/dist/orchestration/routing/commands/thread-handlers.js +40 -39
- package/dist/orchestration/routing/webhook.js +38 -1
- package/dist/orchestration/status-helpers.js +6 -5
- package/dist/orchestration/thread-callback.d.ts +11 -1
- package/dist/orchestration/thread-callback.js +66 -30
- package/dist/platform/adapters/feishu.d.ts +27 -0
- package/dist/platform/adapters/feishu.js +136 -4
- package/dist/platform/interactive-builder.js +19 -18
- package/dist/store/version-migrations.d.ts +7 -0
- package/dist/store/version-migrations.js +88 -0
- package/package.json +1 -1
|
@@ -464,9 +464,11 @@ export class FeishuAdapter {
|
|
|
464
464
|
catch {
|
|
465
465
|
parsed = {};
|
|
466
466
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
467
|
+
// `post` (富文本/rich-text) is what Feishu sends when a message mixes text and
|
|
468
|
+
// images — the text is NOT a top-level `parsed.text` but buried in a 2D array
|
|
469
|
+
// of paragraph element-runs, so the simple text/raw-string path below would
|
|
470
|
+
// yield '' and drop the user's message. Parse post specially.
|
|
471
|
+
let text = this.extractMessageText(messageType, parsed, message.content || '');
|
|
470
472
|
const ref = {
|
|
471
473
|
conduit: this._wrap(chatId),
|
|
472
474
|
messageId,
|
|
@@ -474,7 +476,24 @@ export class FeishuAdapter {
|
|
|
474
476
|
};
|
|
475
477
|
// Extract file references from the parsed content. The id (file_key/image_key)
|
|
476
478
|
// plus message_id + resourceType are needed by downloadFile (messageResource.get).
|
|
477
|
-
|
|
479
|
+
let files = this.extractInboundFiles(messageType, parsed, messageId, chatId);
|
|
480
|
+
// Quote/reply enrichment. When a message replies to another (parent_id set),
|
|
481
|
+
// Feishu delivers ONLY the reply — the quoted message's text/files are not
|
|
482
|
+
// inlined. A user sharing a file cannot @ the bot on the file message itself
|
|
483
|
+
// (file messages carry no text), so they reply-and-@ instead; without this the
|
|
484
|
+
// file would be lost. Fetch the direct parent (one level) unconditionally —
|
|
485
|
+
// including bot-authored parents — and merge its text + files into this message.
|
|
486
|
+
// Best-effort: a fetch failure must never drop the user's own message.
|
|
487
|
+
if (parentId) {
|
|
488
|
+
const quoted = await this.fetchQuotedParent(parentId, chatId);
|
|
489
|
+
if (quoted) {
|
|
490
|
+
if (quoted.text)
|
|
491
|
+
text = text ? `${text}\n\n[引用消息]\n${quoted.text}` : quoted.text;
|
|
492
|
+
if (quoted.files && quoted.files.length) {
|
|
493
|
+
files = files ? [...files, ...quoted.files] : quoted.files;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
478
497
|
// TODO: Parse Feishu forwarded/merged messages (merge_forward msg type) into
|
|
479
498
|
// IncomingAttachment[]. Feishu's format differs from Slack's msg.attachments.
|
|
480
499
|
const kind = files ? 'file_share' : 'user';
|
|
@@ -527,10 +546,123 @@ export class FeishuAdapter {
|
|
|
527
546
|
if (!parsed.image_key)
|
|
528
547
|
return undefined;
|
|
529
548
|
return mk(parsed.image_key, `${parsed.image_key}.png`, 'image/png', 'image');
|
|
549
|
+
case 'post': {
|
|
550
|
+
// A rich-text post may embed any number of inline images (tag:'img'). Each
|
|
551
|
+
// image_key is a resource of THIS message, downloadable via the same
|
|
552
|
+
// messageResource.get(type:'image') path as a standalone image message.
|
|
553
|
+
const { imageKeys } = this.parsePostContent(parsed);
|
|
554
|
+
if (imageKeys.length === 0)
|
|
555
|
+
return undefined;
|
|
556
|
+
return imageKeys.map(k => ({
|
|
557
|
+
id: k,
|
|
558
|
+
name: `${k}.png`,
|
|
559
|
+
mimetype: 'image/png',
|
|
560
|
+
url: '',
|
|
561
|
+
conduit: this._wrap(chatId),
|
|
562
|
+
raw: { message_id: messageId, resourceType: 'image' },
|
|
563
|
+
}));
|
|
564
|
+
}
|
|
530
565
|
default:
|
|
531
566
|
return undefined;
|
|
532
567
|
}
|
|
533
568
|
}
|
|
569
|
+
/**
|
|
570
|
+
* Parse a Feishu `post` (富文本/rich-text) message content payload. Feishu wraps
|
|
571
|
+
* a single message that mixes text and images as a post, whose `content` is a 2D
|
|
572
|
+
* array of paragraphs, each a run of element nodes:
|
|
573
|
+
* { title?, content: [ [ {tag:'text',text}, {tag:'a',text,href}, {tag:'at',user_name},
|
|
574
|
+
* {tag:'img',image_key} ], ... ] }
|
|
575
|
+
* Returns the concatenated text (paragraphs joined by '\n', non-empty title
|
|
576
|
+
* prefixed) and the list of inline image_keys, so callers recover BOTH the text
|
|
577
|
+
* and the images from one mixed message.
|
|
578
|
+
*/
|
|
579
|
+
parsePostContent(parsed) {
|
|
580
|
+
const imageKeys = [];
|
|
581
|
+
const content = parsed?.content;
|
|
582
|
+
if (!Array.isArray(content))
|
|
583
|
+
return { text: '', imageKeys };
|
|
584
|
+
const lines = [];
|
|
585
|
+
for (const paragraph of content) {
|
|
586
|
+
if (!Array.isArray(paragraph))
|
|
587
|
+
continue;
|
|
588
|
+
let line = '';
|
|
589
|
+
for (const node of paragraph) {
|
|
590
|
+
if (!node || typeof node !== 'object')
|
|
591
|
+
continue;
|
|
592
|
+
switch (node.tag) {
|
|
593
|
+
case 'text':
|
|
594
|
+
case 'a':
|
|
595
|
+
if (typeof node.text === 'string')
|
|
596
|
+
line += node.text;
|
|
597
|
+
break;
|
|
598
|
+
case 'at':
|
|
599
|
+
if (typeof node.user_name === 'string')
|
|
600
|
+
line += `@${node.user_name}`;
|
|
601
|
+
break;
|
|
602
|
+
case 'img':
|
|
603
|
+
if (typeof node.image_key === 'string')
|
|
604
|
+
imageKeys.push(node.image_key);
|
|
605
|
+
break;
|
|
606
|
+
default:
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
lines.push(line);
|
|
611
|
+
}
|
|
612
|
+
let text = lines.join('\n').trim();
|
|
613
|
+
if (typeof parsed.title === 'string' && parsed.title.trim()) {
|
|
614
|
+
text = text ? `${parsed.title}\n${text}` : parsed.title.trim();
|
|
615
|
+
}
|
|
616
|
+
return { text, imageKeys };
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Resolve a Feishu message's human-readable text from its type + parsed content.
|
|
620
|
+
* `post` (rich text) buries text in a 2D run array; plain text lives in
|
|
621
|
+
* parsed.text; a few legacy payloads ship a bare non-JSON string as content.
|
|
622
|
+
* Shared by both inbound messages and quoted-parent enrichment.
|
|
623
|
+
*/
|
|
624
|
+
extractMessageText(messageType, parsed, rawContent) {
|
|
625
|
+
if (messageType === 'post')
|
|
626
|
+
return this.parsePostContent(parsed).text;
|
|
627
|
+
if (typeof parsed?.text === 'string')
|
|
628
|
+
return parsed.text;
|
|
629
|
+
if (typeof rawContent === 'string' && rawContent && !rawContent.startsWith('{'))
|
|
630
|
+
return rawContent;
|
|
631
|
+
return '';
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Fetch the directly-quoted parent message (one level) and project it to the same
|
|
635
|
+
* { text, files } shape an inbound message produces, so a reply can be enriched
|
|
636
|
+
* with what it quotes — notably files, since a Feishu file message carries no text
|
|
637
|
+
* and thus cannot @ the bot. Returns null on any failure (best-effort).
|
|
638
|
+
* NOTE: im.v1.message.get returns content under data.items[].body.content (a JSON
|
|
639
|
+
* string), NOT the top-level `content` field the receive_v1 event uses.
|
|
640
|
+
*/
|
|
641
|
+
async fetchQuotedParent(parentId, chatId) {
|
|
642
|
+
try {
|
|
643
|
+
const resp = await this.client.im.v1.message.get({ path: { message_id: parentId } });
|
|
644
|
+
const item = resp?.data?.items?.[0];
|
|
645
|
+
if (!item)
|
|
646
|
+
return null;
|
|
647
|
+
const messageType = item.msg_type || '';
|
|
648
|
+
const rawContent = item.body?.content || '';
|
|
649
|
+
let parsed = {};
|
|
650
|
+
try {
|
|
651
|
+
parsed = JSON.parse(rawContent || '{}');
|
|
652
|
+
}
|
|
653
|
+
catch {
|
|
654
|
+
parsed = {};
|
|
655
|
+
}
|
|
656
|
+
const text = this.extractMessageText(messageType, parsed, rawContent);
|
|
657
|
+
// Files of the parent must download against the PARENT's message_id.
|
|
658
|
+
const files = this.extractInboundFiles(messageType, parsed, item.message_id || parentId, chatId);
|
|
659
|
+
return { text, files };
|
|
660
|
+
}
|
|
661
|
+
catch (e) {
|
|
662
|
+
log.warn(`Failed to fetch quoted parent ${parentId}: ${e.message}`);
|
|
663
|
+
return null;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
534
666
|
async handleCardAction(data) {
|
|
535
667
|
const event = data.event || data;
|
|
536
668
|
const action = event.action || {};
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// pos: Platform-independent interactive component builder
|
|
4
4
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
5
|
import { Icons } from '../core/icons.js';
|
|
6
|
+
import { t } from '../core/i18n.js';
|
|
6
7
|
// --- AskUserQuestion builders ---
|
|
7
8
|
export function buildQuestionGroupBlocks(group) {
|
|
8
9
|
const blocks = [];
|
|
@@ -26,7 +27,7 @@ export function buildQuestionGroupBlocks(group) {
|
|
|
26
27
|
type: 'actions',
|
|
27
28
|
elements: [{
|
|
28
29
|
type: 'button',
|
|
29
|
-
text:
|
|
30
|
+
text: t('modal.answer', { answered: group.answers.size, total: group.questions.length }),
|
|
30
31
|
actionId: 'ask_user_question_open_modal',
|
|
31
32
|
value: group.groupId,
|
|
32
33
|
style: 'primary',
|
|
@@ -51,9 +52,9 @@ export function buildQuestionModalDefinition(group) {
|
|
|
51
52
|
fields.push({
|
|
52
53
|
type: q.multiSelect ? 'multi_select' : 'select',
|
|
53
54
|
blockId: `q_${qIdx}`,
|
|
54
|
-
label: q.multiSelect ? '
|
|
55
|
+
label: q.multiSelect ? t('modal.selectOneOrMore') : t('modal.selectOne'),
|
|
55
56
|
actionId: 'selection',
|
|
56
|
-
placeholder: q.multiSelect ? '
|
|
57
|
+
placeholder: q.multiSelect ? t('modal.selectOneOrMore') : t('modal.selectOne'),
|
|
57
58
|
options: q.options.map((option, optIdx) => ({
|
|
58
59
|
label: option.label,
|
|
59
60
|
value: String(optIdx),
|
|
@@ -63,9 +64,9 @@ export function buildQuestionModalDefinition(group) {
|
|
|
63
64
|
fields.push({
|
|
64
65
|
type: 'text_input',
|
|
65
66
|
blockId: `q_${qIdx}_other`,
|
|
66
|
-
label: '
|
|
67
|
+
label: t('modal.typeAnswerLabel'),
|
|
67
68
|
actionId: 'other_text',
|
|
68
|
-
placeholder: '
|
|
69
|
+
placeholder: t('modal.customAnswerPlaceholder'),
|
|
69
70
|
optional: true,
|
|
70
71
|
});
|
|
71
72
|
}
|
|
@@ -74,18 +75,18 @@ export function buildQuestionModalDefinition(group) {
|
|
|
74
75
|
fields.push({
|
|
75
76
|
type: 'text_input',
|
|
76
77
|
blockId: `q_${qIdx}_other`,
|
|
77
|
-
label: '
|
|
78
|
+
label: t('modal.yourAnswerLabel'),
|
|
78
79
|
actionId: 'other_text',
|
|
79
|
-
placeholder: '
|
|
80
|
+
placeholder: t('modal.yourAnswerPlaceholder'),
|
|
80
81
|
optional: false,
|
|
81
82
|
});
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
return {
|
|
85
86
|
callbackId: 'ask_user_question_modal_submit',
|
|
86
|
-
title: '
|
|
87
|
-
submitLabel: '
|
|
88
|
-
closeLabel: '
|
|
87
|
+
title: t('modal.questionsTitle'),
|
|
88
|
+
submitLabel: t('modal.submit'),
|
|
89
|
+
closeLabel: t('modal.cancel'),
|
|
89
90
|
privateMetadata: JSON.stringify({ groupId: group.groupId }),
|
|
90
91
|
fields,
|
|
91
92
|
};
|
|
@@ -94,28 +95,28 @@ export function buildQuestionModalDefinition(group) {
|
|
|
94
95
|
export function buildPlanApprovalContent(requestId) {
|
|
95
96
|
return {
|
|
96
97
|
richBlocks: [
|
|
97
|
-
{ type: 'section', text: `${Icons.memo}
|
|
98
|
+
{ type: 'section', text: `${Icons.memo} ${t('modal.planReady')}` },
|
|
98
99
|
],
|
|
99
100
|
actions: [
|
|
100
|
-
{ type: 'button', text: '
|
|
101
|
-
{ type: 'button', text: '
|
|
101
|
+
{ type: 'button', text: t('modal.approve'), actionId: 'hook_plan_approve', value: requestId, style: 'primary' },
|
|
102
|
+
{ type: 'button', text: t('modal.provideFeedback'), actionId: 'hook_plan_feedback', value: requestId },
|
|
102
103
|
],
|
|
103
104
|
};
|
|
104
105
|
}
|
|
105
106
|
export function buildPlanFeedbackModal(requestId) {
|
|
106
107
|
return {
|
|
107
108
|
callbackId: 'hook_plan_feedback_submit',
|
|
108
|
-
title: '
|
|
109
|
-
submitLabel: '
|
|
110
|
-
closeLabel: '
|
|
109
|
+
title: t('modal.planFeedbackTitle'),
|
|
110
|
+
submitLabel: t('modal.submit'),
|
|
111
|
+
closeLabel: t('modal.cancel'),
|
|
111
112
|
privateMetadata: JSON.stringify({ requestId }),
|
|
112
113
|
fields: [{
|
|
113
114
|
type: 'text_input',
|
|
114
115
|
blockId: 'feedback',
|
|
115
|
-
label: '
|
|
116
|
+
label: t('modal.feedbackLabel'),
|
|
116
117
|
actionId: 'text',
|
|
117
118
|
multiline: true,
|
|
118
|
-
placeholder: '
|
|
119
|
+
placeholder: t('modal.feedbackPlaceholder'),
|
|
119
120
|
}],
|
|
120
121
|
};
|
|
121
122
|
}
|
|
@@ -20,3 +20,10 @@ export interface MigrationOptions {
|
|
|
20
20
|
storeDir?: string;
|
|
21
21
|
}
|
|
22
22
|
export declare function runMigrations(opts?: MigrationOptions): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Migrate config.yaml from CORTEX_HOME/config/ (wrong location before 2026.6.11)
|
|
25
|
+
* to ~/.aistatus/config.yaml (correct location read by aistatus).
|
|
26
|
+
* Idempotent: if target already exists and is valid YAML, source is deleted without copying.
|
|
27
|
+
* If source is malformed YAML, it's deleted to avoid interfering with aistatus.
|
|
28
|
+
*/
|
|
29
|
+
export declare function migrateAistatusConfigLocation(dataDir: string): Promise<void>;
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
7
7
|
import * as path from 'node:path';
|
|
8
8
|
import * as fs from 'node:fs/promises';
|
|
9
|
+
import * as os from 'node:os';
|
|
9
10
|
import { CORTEX_VERSION } from '../core/version.js';
|
|
10
11
|
import { DATA_DIR, DEFAULTS_DIR } from '../core/paths.js';
|
|
11
12
|
import { atomicWrite } from '../core/atomic-write.js';
|
|
@@ -298,3 +299,90 @@ export async function runMigrations(opts = {}) {
|
|
|
298
299
|
}
|
|
299
300
|
await saveVersionsTo(versionsFile, versions);
|
|
300
301
|
}
|
|
302
|
+
// ── File location migrations ───────────────────────────────────
|
|
303
|
+
/**
|
|
304
|
+
* Migrate config.yaml from CORTEX_HOME/config/ (wrong location before 2026.6.11)
|
|
305
|
+
* to ~/.aistatus/config.yaml (correct location read by aistatus).
|
|
306
|
+
* Idempotent: if target already exists and is valid YAML, source is deleted without copying.
|
|
307
|
+
* If source is malformed YAML, it's deleted to avoid interfering with aistatus.
|
|
308
|
+
*/
|
|
309
|
+
export async function migrateAistatusConfigLocation(dataDir) {
|
|
310
|
+
const srcPath = path.join(dataDir, 'config', 'config.yaml');
|
|
311
|
+
const dstPath = path.join(os.homedir(), '.aistatus', 'config.yaml');
|
|
312
|
+
// Check if source exists
|
|
313
|
+
let srcExists = false;
|
|
314
|
+
try {
|
|
315
|
+
await fs.access(srcPath);
|
|
316
|
+
srcExists = true;
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
// Source does not exist — nothing to migrate
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
// Check if target already exists
|
|
323
|
+
let dstExists = false;
|
|
324
|
+
let dstValid = false;
|
|
325
|
+
try {
|
|
326
|
+
await fs.access(dstPath);
|
|
327
|
+
dstExists = true;
|
|
328
|
+
// Validate target is readable YAML (not corrupted)
|
|
329
|
+
const raw = await fs.readFile(dstPath, 'utf8');
|
|
330
|
+
// Simple YAML validation: must be parseable as key: value pairs
|
|
331
|
+
if (raw.trim().length === 0 || /^[a-zA-Z_][\w]*:/.test(raw)) {
|
|
332
|
+
dstValid = true;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
catch {
|
|
336
|
+
// Target does not exist or is not readable
|
|
337
|
+
}
|
|
338
|
+
// If target exists and is valid, source is redundant — just remove it
|
|
339
|
+
if (dstExists && dstValid) {
|
|
340
|
+
try {
|
|
341
|
+
await fs.unlink(srcPath);
|
|
342
|
+
log.info(`Migrated aistatus config: removed redundant ${srcPath} (target ${dstPath} already exists)`);
|
|
343
|
+
}
|
|
344
|
+
catch {
|
|
345
|
+
// Best effort — if we can't delete, log warning but don't fail
|
|
346
|
+
log.warn(`Could not remove old config file: ${srcPath}`);
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
// Read source, validate, and copy to target
|
|
351
|
+
let srcData;
|
|
352
|
+
try {
|
|
353
|
+
srcData = await fs.readFile(srcPath, 'utf8');
|
|
354
|
+
}
|
|
355
|
+
catch (err) {
|
|
356
|
+
log.warn(`Could not read old config file: ${srcPath}; skipping migration`);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
// Validate source is valid YAML before copying
|
|
360
|
+
if (srcData.trim().length === 0 || !/^[a-zA-Z_][\w]*:/.test(srcData)) {
|
|
361
|
+
// Source is empty or doesn't look like YAML — delete it to avoid pollution
|
|
362
|
+
try {
|
|
363
|
+
await fs.unlink(srcPath);
|
|
364
|
+
log.info(`Removed malformed old config file: ${srcPath}`);
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
log.warn(`Could not remove malformed old config file: ${srcPath}`);
|
|
368
|
+
}
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
// Copy source to target
|
|
372
|
+
try {
|
|
373
|
+
const dstDir = path.dirname(dstPath);
|
|
374
|
+
await fs.mkdir(dstDir, { recursive: true });
|
|
375
|
+
await atomicWrite(dstPath, srcData);
|
|
376
|
+
log.info(`Migrated aistatus config from ${srcPath} to ${dstPath}`);
|
|
377
|
+
// Clean up source after successful copy
|
|
378
|
+
try {
|
|
379
|
+
await fs.unlink(srcPath);
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
log.warn(`Could not remove old config file after migration: ${srcPath}`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
catch (err) {
|
|
386
|
+
log.error(`Failed to migrate aistatus config to ${dstPath}: ${err.message}`);
|
|
387
|
+
}
|
|
388
|
+
}
|
package/package.json
CHANGED