@lumenflow/cli 3.6.6 → 3.6.7
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/init.js +78 -7
- package/dist/init.js.map +1 -1
- package/dist/initiative-create.js +74 -23
- package/dist/initiative-create.js.map +1 -1
- package/dist/lane-lock.js +62 -1
- package/dist/lane-lock.js.map +1 -1
- package/dist/lane-setup.js +102 -28
- package/dist/lane-setup.js.map +1 -1
- package/dist/lane-status.js +42 -0
- package/dist/lane-status.js.map +1 -1
- package/dist/lane-validate.js +62 -1
- package/dist/lane-validate.js.map +1 -1
- package/dist/plan-link.js +25 -2
- package/dist/plan-link.js.map +1 -1
- package/dist/public-manifest.js +7 -0
- package/dist/public-manifest.js.map +1 -1
- package/dist/release.js +17 -0
- package/dist/release.js.map +1 -1
- package/dist/state-doctor-fix.js +12 -11
- package/dist/state-doctor-fix.js.map +1 -1
- package/dist/state-emit.js +198 -0
- package/dist/state-emit.js.map +1 -0
- package/dist/wu-claim-state.js +58 -15
- package/dist/wu-claim-state.js.map +1 -1
- package/dist/wu-claim-worktree.js +3 -3
- package/dist/wu-claim-worktree.js.map +1 -1
- package/dist/wu-claim.js +19 -1
- package/dist/wu-claim.js.map +1 -1
- package/dist/wu-create-content.js +2 -4
- package/dist/wu-create-content.js.map +1 -1
- package/dist/wu-create-validation.js +14 -1
- package/dist/wu-create-validation.js.map +1 -1
- package/dist/wu-create.js +2 -6
- package/dist/wu-create.js.map +1 -1
- package/dist/wu-done.js +95 -4
- package/dist/wu-done.js.map +1 -1
- package/dist/wu-edit-operations.js +36 -5
- package/dist/wu-edit-operations.js.map +1 -1
- package/dist/wu-recover.js +115 -12
- package/dist/wu-recover.js.map +1 -1
- package/package.json +9 -8
- package/packs/sidekick/.turbo/turbo-build.log +4 -0
- package/packs/sidekick/README.md +194 -0
- package/packs/sidekick/constants.ts +10 -0
- package/packs/sidekick/index.ts +8 -0
- package/packs/sidekick/manifest-schema.ts +262 -0
- package/packs/sidekick/manifest.ts +333 -0
- package/packs/sidekick/manifest.yaml +406 -0
- package/packs/sidekick/pack-registration.ts +110 -0
- package/packs/sidekick/package.json +55 -0
- package/packs/sidekick/tool-impl/channel-tools.ts +226 -0
- package/packs/sidekick/tool-impl/index.ts +22 -0
- package/packs/sidekick/tool-impl/memory-tools.ts +188 -0
- package/packs/sidekick/tool-impl/routine-tools.ts +194 -0
- package/packs/sidekick/tool-impl/shared.ts +124 -0
- package/packs/sidekick/tool-impl/storage.ts +315 -0
- package/packs/sidekick/tool-impl/system-tools.ts +155 -0
- package/packs/sidekick/tool-impl/task-tools.ts +278 -0
- package/packs/sidekick/tools/channel-tools.ts +53 -0
- package/packs/sidekick/tools/index.ts +9 -0
- package/packs/sidekick/tools/memory-tools.ts +53 -0
- package/packs/sidekick/tools/routine-tools.ts +53 -0
- package/packs/sidekick/tools/system-tools.ts +47 -0
- package/packs/sidekick/tools/task-tools.ts +61 -0
- package/packs/sidekick/tools/types.ts +57 -0
- package/packs/sidekick/tsconfig.json +20 -0
- package/templates/core/ai/onboarding/starting-prompt.md.template +33 -2
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright (c) 2026 Hellmai Ltd
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
|
|
4
|
+
import { randomBytes } from 'node:crypto';
|
|
5
|
+
import type { AuditEvent } from './storage.js';
|
|
6
|
+
|
|
7
|
+
export interface ToolContextLike {
|
|
8
|
+
tool_name?: string;
|
|
9
|
+
receipt_id?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ToolOutput {
|
|
13
|
+
success: boolean;
|
|
14
|
+
data?: Record<string, unknown>;
|
|
15
|
+
error?: { code: string; message: string; details?: Record<string, unknown> };
|
|
16
|
+
metadata?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function toRecord(input: unknown): Record<string, unknown> {
|
|
20
|
+
if (input && typeof input === 'object' && !Array.isArray(input)) {
|
|
21
|
+
return input as Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
return {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function asNonEmptyString(value: unknown): string | null {
|
|
27
|
+
if (typeof value !== 'string') {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
const trimmed = value.trim();
|
|
31
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function asStringArray(value: unknown): string[] {
|
|
35
|
+
if (!Array.isArray(value)) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
const items: string[] = [];
|
|
39
|
+
for (const entry of value) {
|
|
40
|
+
const normalized = asNonEmptyString(entry);
|
|
41
|
+
if (normalized) {
|
|
42
|
+
items.push(normalized);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return [...new Set(items)];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function asInteger(value: unknown): number | null {
|
|
49
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
50
|
+
return Math.trunc(value);
|
|
51
|
+
}
|
|
52
|
+
if (typeof value === 'string') {
|
|
53
|
+
const parsed = Number.parseInt(value, 10);
|
|
54
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function isDryRun(input: Record<string, unknown>): boolean {
|
|
60
|
+
return input.dry_run === true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function nowIso(): string {
|
|
64
|
+
return new Date().toISOString();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function createId(prefix: string): string {
|
|
68
|
+
return `${prefix}-${randomBytes(4).toString('hex')}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function failure(
|
|
72
|
+
code: string,
|
|
73
|
+
message: string,
|
|
74
|
+
details?: Record<string, unknown>,
|
|
75
|
+
): ToolOutput {
|
|
76
|
+
return {
|
|
77
|
+
success: false,
|
|
78
|
+
error: {
|
|
79
|
+
code,
|
|
80
|
+
message,
|
|
81
|
+
...(details ? { details } : {}),
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function success(data: Record<string, unknown>): ToolOutput {
|
|
87
|
+
return {
|
|
88
|
+
success: true,
|
|
89
|
+
data,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function matchesTags(requiredTags: string[], candidateTags: string[]): boolean {
|
|
94
|
+
if (requiredTags.length === 0) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
const candidateSet = new Set(candidateTags.map((tag) => tag.toLowerCase()));
|
|
98
|
+
return requiredTags.every((tag) => candidateSet.has(tag.toLowerCase()));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function includesText(haystack: string, needle: string | null): boolean {
|
|
102
|
+
if (!needle) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return haystack.toLowerCase().includes(needle.toLowerCase());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function buildAuditEvent(input: {
|
|
109
|
+
tool: string;
|
|
110
|
+
op: AuditEvent['op'];
|
|
111
|
+
context?: ToolContextLike;
|
|
112
|
+
ids?: string[];
|
|
113
|
+
details?: Record<string, unknown>;
|
|
114
|
+
}): AuditEvent {
|
|
115
|
+
return {
|
|
116
|
+
id: createId('evt'),
|
|
117
|
+
ts: nowIso(),
|
|
118
|
+
tool: input.tool,
|
|
119
|
+
op: input.op,
|
|
120
|
+
actor: input.context?.receipt_id,
|
|
121
|
+
ids: input.ids,
|
|
122
|
+
details: input.details,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// Copyright (c) 2026 Hellmai Ltd
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
|
|
4
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
+
import { randomBytes } from 'node:crypto';
|
|
6
|
+
import { appendFile, mkdir, open, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Constants
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
const UTF8_ENCODING = 'utf8';
|
|
14
|
+
const DEFAULT_LOCK_TIMEOUT_MS = 10_000;
|
|
15
|
+
const LOCK_RETRY_INTERVAL_MS = 25;
|
|
16
|
+
const STALE_LOCK_MS = 30_000;
|
|
17
|
+
const RANDOM_BYTES_LENGTH = 4;
|
|
18
|
+
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Domain types
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
export type TaskPriority = 'P0' | 'P1' | 'P2' | 'P3';
|
|
24
|
+
export type TaskStatus = 'pending' | 'done';
|
|
25
|
+
export type MemoryType = 'fact' | 'preference' | 'note';
|
|
26
|
+
|
|
27
|
+
export interface TaskRecord {
|
|
28
|
+
id: string;
|
|
29
|
+
title: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
priority: TaskPriority;
|
|
32
|
+
status: TaskStatus;
|
|
33
|
+
tags: string[];
|
|
34
|
+
due_at?: string;
|
|
35
|
+
note?: string;
|
|
36
|
+
created_at: string;
|
|
37
|
+
updated_at: string;
|
|
38
|
+
completed_at?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface MemoryRecord {
|
|
42
|
+
id: string;
|
|
43
|
+
type: MemoryType;
|
|
44
|
+
content: string;
|
|
45
|
+
tags: string[];
|
|
46
|
+
created_at: string;
|
|
47
|
+
updated_at: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ChannelRecord {
|
|
51
|
+
id: string;
|
|
52
|
+
name: string;
|
|
53
|
+
created_at: string;
|
|
54
|
+
updated_at: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ChannelMessageRecord {
|
|
58
|
+
id: string;
|
|
59
|
+
channel_id: string;
|
|
60
|
+
sender: string;
|
|
61
|
+
content: string;
|
|
62
|
+
created_at: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface RoutineStepRecord {
|
|
66
|
+
tool: string;
|
|
67
|
+
input: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface RoutineRecord {
|
|
71
|
+
id: string;
|
|
72
|
+
name: string;
|
|
73
|
+
steps: RoutineStepRecord[];
|
|
74
|
+
created_at: string;
|
|
75
|
+
updated_at: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface SidekickStores {
|
|
79
|
+
tasks: TaskRecord[];
|
|
80
|
+
memories: MemoryRecord[];
|
|
81
|
+
channels: ChannelRecord[];
|
|
82
|
+
messages: ChannelMessageRecord[];
|
|
83
|
+
routines: RoutineRecord[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type StoreName = keyof SidekickStores;
|
|
87
|
+
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// Audit types
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
export interface AuditEvent {
|
|
93
|
+
id: string;
|
|
94
|
+
ts: string;
|
|
95
|
+
tool: string;
|
|
96
|
+
op: 'create' | 'read' | 'update' | 'delete' | 'execute' | 'export';
|
|
97
|
+
actor?: string;
|
|
98
|
+
ids?: string[];
|
|
99
|
+
details?: Record<string, unknown>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
// StoragePort (hexagonal port)
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
|
|
106
|
+
export interface StoragePort {
|
|
107
|
+
getRootDir(): string;
|
|
108
|
+
withLock<T>(fn: () => Promise<T>): Promise<T>;
|
|
109
|
+
readStore<K extends StoreName>(store: K): Promise<SidekickStores[K]>;
|
|
110
|
+
writeStore<K extends StoreName>(store: K, data: SidekickStores[K]): Promise<void>;
|
|
111
|
+
appendAudit(event: AuditEvent): Promise<void>;
|
|
112
|
+
readAuditEvents(): Promise<AuditEvent[]>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// File-path mapping
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
const STORE_FILE_PATHS: Record<StoreName, string> = {
|
|
120
|
+
tasks: 'tasks/tasks.json',
|
|
121
|
+
memories: 'memory/memories.json',
|
|
122
|
+
channels: 'channels/channels.json',
|
|
123
|
+
messages: 'channels/messages.json',
|
|
124
|
+
routines: 'routines/routines.json',
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const STORE_DEFAULTS: SidekickStores = {
|
|
128
|
+
tasks: [],
|
|
129
|
+
memories: [],
|
|
130
|
+
channels: [],
|
|
131
|
+
messages: [],
|
|
132
|
+
routines: [],
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const AUDIT_FILE_PATH = 'audit/events.jsonl';
|
|
136
|
+
const LOCK_FILE_PATH = '.lock';
|
|
137
|
+
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
// Internal helpers
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
function cloneStore<K extends StoreName>(store: K, value: SidekickStores[K]): SidekickStores[K] {
|
|
143
|
+
if (Array.isArray(value)) {
|
|
144
|
+
return value.map((entry) => ({ ...entry })) as SidekickStores[K];
|
|
145
|
+
}
|
|
146
|
+
return structuredClone(value) as SidekickStores[K];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function randomToken(): string {
|
|
150
|
+
return randomBytes(RANDOM_BYTES_LENGTH).toString('hex');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function nowMs(): number {
|
|
154
|
+
return Date.now();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function sleep(ms: number): Promise<void> {
|
|
158
|
+
return new Promise((resolve) => {
|
|
159
|
+
setTimeout(resolve, ms);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function ensureParentDir(filePath: string): Promise<void> {
|
|
164
|
+
await mkdir(path.dirname(filePath), { recursive: true });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function writeJsonAtomic(filePath: string, data: unknown): Promise<void> {
|
|
168
|
+
await ensureParentDir(filePath);
|
|
169
|
+
const tmpPath = `${filePath}.tmp-${process.pid}-${randomToken()}`;
|
|
170
|
+
await writeFile(tmpPath, `${JSON.stringify(data, null, 2)}\n`, UTF8_ENCODING);
|
|
171
|
+
await rename(tmpPath, filePath);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function readJsonFile<T>(filePath: string, fallback: T): Promise<T> {
|
|
175
|
+
try {
|
|
176
|
+
const raw = await readFile(filePath, UTF8_ENCODING);
|
|
177
|
+
return JSON.parse(raw) as T;
|
|
178
|
+
} catch (error) {
|
|
179
|
+
const nodeError = error as NodeJS.ErrnoException;
|
|
180
|
+
if (nodeError.code === 'ENOENT') {
|
|
181
|
+
await writeJsonAtomic(filePath, fallback);
|
|
182
|
+
return fallback;
|
|
183
|
+
}
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function maybeRemoveStaleLock(lockPath: string): Promise<void> {
|
|
189
|
+
try {
|
|
190
|
+
const metadata = await stat(lockPath);
|
|
191
|
+
const ageMs = nowMs() - metadata.mtimeMs;
|
|
192
|
+
if (ageMs > STALE_LOCK_MS) {
|
|
193
|
+
await rm(lockPath, { force: true });
|
|
194
|
+
}
|
|
195
|
+
} catch {
|
|
196
|
+
// Ignore races for lock cleanup.
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function withFileLock<T>(lockPath: string, fn: () => Promise<T>): Promise<T> {
|
|
201
|
+
const deadline = nowMs() + DEFAULT_LOCK_TIMEOUT_MS;
|
|
202
|
+
let handle: Awaited<ReturnType<typeof open>> | null = null;
|
|
203
|
+
|
|
204
|
+
while (nowMs() < deadline) {
|
|
205
|
+
try {
|
|
206
|
+
await ensureParentDir(lockPath);
|
|
207
|
+
handle = await open(lockPath, 'wx');
|
|
208
|
+
await handle.writeFile(`${process.pid}:${nowMs()}`, UTF8_ENCODING);
|
|
209
|
+
break;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
const nodeError = error as NodeJS.ErrnoException;
|
|
212
|
+
if (nodeError.code !== 'EEXIST') {
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
await maybeRemoveStaleLock(lockPath);
|
|
216
|
+
await sleep(LOCK_RETRY_INTERVAL_MS);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (!handle) {
|
|
221
|
+
throw new Error(`Timed out waiting for sidekick storage lock at ${lockPath}.`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
return await fn();
|
|
226
|
+
} finally {
|
|
227
|
+
await handle.close();
|
|
228
|
+
await rm(lockPath, { force: true });
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
// FsStoragePort (filesystem adapter)
|
|
234
|
+
// ---------------------------------------------------------------------------
|
|
235
|
+
|
|
236
|
+
export class FsStoragePort implements StoragePort {
|
|
237
|
+
private readonly rootDir: string;
|
|
238
|
+
|
|
239
|
+
constructor(rootDir = path.resolve(process.cwd(), '.sidekick')) {
|
|
240
|
+
this.rootDir = rootDir;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
getRootDir(): string {
|
|
244
|
+
return this.rootDir;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async withLock<T>(fn: () => Promise<T>): Promise<T> {
|
|
248
|
+
return withFileLock(path.join(this.rootDir, LOCK_FILE_PATH), fn);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async readStore<K extends StoreName>(store: K): Promise<SidekickStores[K]> {
|
|
252
|
+
const storePath = path.join(this.rootDir, STORE_FILE_PATHS[store]);
|
|
253
|
+
const fallback = cloneStore(store, STORE_DEFAULTS[store]);
|
|
254
|
+
const data = await readJsonFile(storePath, fallback);
|
|
255
|
+
return cloneStore(store, data);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async writeStore<K extends StoreName>(store: K, data: SidekickStores[K]): Promise<void> {
|
|
259
|
+
const storePath = path.join(this.rootDir, STORE_FILE_PATHS[store]);
|
|
260
|
+
await writeJsonAtomic(storePath, data);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async appendAudit(event: AuditEvent): Promise<void> {
|
|
264
|
+
const auditPath = path.join(this.rootDir, AUDIT_FILE_PATH);
|
|
265
|
+
await ensureParentDir(auditPath);
|
|
266
|
+
await appendFile(auditPath, `${JSON.stringify(event)}\n`, UTF8_ENCODING);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async readAuditEvents(): Promise<AuditEvent[]> {
|
|
270
|
+
const auditPath = path.join(this.rootDir, AUDIT_FILE_PATH);
|
|
271
|
+
let raw: string;
|
|
272
|
+
try {
|
|
273
|
+
raw = await readFile(auditPath, UTF8_ENCODING);
|
|
274
|
+
} catch (error) {
|
|
275
|
+
const nodeError = error as NodeJS.ErrnoException;
|
|
276
|
+
if (nodeError.code === 'ENOENT') {
|
|
277
|
+
return [];
|
|
278
|
+
}
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const events: AuditEvent[] = [];
|
|
283
|
+
for (const line of raw.split('\n')) {
|
|
284
|
+
const trimmed = line.trim();
|
|
285
|
+
if (trimmed.length === 0) {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
events.push(JSON.parse(trimmed) as AuditEvent);
|
|
290
|
+
} catch {
|
|
291
|
+
// Skip malformed audit lines and keep the stream readable.
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return events;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ---------------------------------------------------------------------------
|
|
299
|
+
// Injection helpers (AsyncLocalStorage-based)
|
|
300
|
+
// ---------------------------------------------------------------------------
|
|
301
|
+
|
|
302
|
+
const storageContext = new AsyncLocalStorage<StoragePort>();
|
|
303
|
+
let defaultStoragePort: StoragePort = new FsStoragePort();
|
|
304
|
+
|
|
305
|
+
export function setDefaultStoragePort(port: StoragePort): void {
|
|
306
|
+
defaultStoragePort = port;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function getStoragePort(): StoragePort {
|
|
310
|
+
return storageContext.getStore() ?? defaultStoragePort;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export async function runWithStoragePort<T>(port: StoragePort, fn: () => Promise<T>): Promise<T> {
|
|
314
|
+
return storageContext.run(port, fn);
|
|
315
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Copyright (c) 2026 Hellmai Ltd
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
|
|
4
|
+
import { getStoragePort } from './storage.js';
|
|
5
|
+
import {
|
|
6
|
+
asNonEmptyString,
|
|
7
|
+
buildAuditEvent,
|
|
8
|
+
failure,
|
|
9
|
+
nowIso,
|
|
10
|
+
success,
|
|
11
|
+
toRecord,
|
|
12
|
+
type ToolContextLike,
|
|
13
|
+
type ToolOutput,
|
|
14
|
+
} from './shared.js';
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Constants
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
const TOOL_NAMES = {
|
|
21
|
+
INIT: 'sidekick:init',
|
|
22
|
+
STATUS: 'sidekick:status',
|
|
23
|
+
EXPORT: 'sidekick:export',
|
|
24
|
+
} as const;
|
|
25
|
+
|
|
26
|
+
const STORE_NAMES = ['tasks', 'memories', 'channels', 'messages', 'routines'] as const;
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// sidekick:init (idempotent)
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
async function initTool(context?: ToolContextLike): Promise<ToolOutput> {
|
|
33
|
+
const storage = getStoragePort();
|
|
34
|
+
|
|
35
|
+
// Touch every store to ensure directories and files are created
|
|
36
|
+
for (const store of STORE_NAMES) {
|
|
37
|
+
await storage.readStore(store);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
await storage.appendAudit(
|
|
41
|
+
buildAuditEvent({
|
|
42
|
+
tool: TOOL_NAMES.INIT,
|
|
43
|
+
op: 'create',
|
|
44
|
+
context,
|
|
45
|
+
details: { root_dir: storage.getRootDir() },
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
return success({
|
|
50
|
+
initialized: true,
|
|
51
|
+
root_dir: storage.getRootDir(),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// sidekick:status
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
async function statusTool(context?: ToolContextLike): Promise<ToolOutput> {
|
|
60
|
+
const storage = getStoragePort();
|
|
61
|
+
const [tasks, memories, channels, messages, routines, audit] = await Promise.all([
|
|
62
|
+
storage.readStore('tasks'),
|
|
63
|
+
storage.readStore('memories'),
|
|
64
|
+
storage.readStore('channels'),
|
|
65
|
+
storage.readStore('messages'),
|
|
66
|
+
storage.readStore('routines'),
|
|
67
|
+
storage.readAuditEvents(),
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
const pendingTasks = tasks.filter((task) => task.status === 'pending').length;
|
|
71
|
+
const completedTasks = tasks.filter((task) => task.status === 'done').length;
|
|
72
|
+
|
|
73
|
+
await storage.appendAudit(
|
|
74
|
+
buildAuditEvent({
|
|
75
|
+
tool: TOOL_NAMES.STATUS,
|
|
76
|
+
op: 'read',
|
|
77
|
+
context,
|
|
78
|
+
details: { task_count: tasks.length },
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
return success({
|
|
83
|
+
task_count: tasks.length,
|
|
84
|
+
pending_tasks: pendingTasks,
|
|
85
|
+
completed_tasks: completedTasks,
|
|
86
|
+
memory_entries: memories.length,
|
|
87
|
+
channels: channels.length,
|
|
88
|
+
messages: messages.length,
|
|
89
|
+
routines: routines.length,
|
|
90
|
+
audit_events: audit.length,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// sidekick:export (READ-ONLY -- returns data, no file write)
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
async function exportTool(input: unknown, context?: ToolContextLike): Promise<ToolOutput> {
|
|
99
|
+
const parsed = toRecord(input);
|
|
100
|
+
const includeAudit = parsed.include_audit !== false;
|
|
101
|
+
|
|
102
|
+
const storage = getStoragePort();
|
|
103
|
+
const [tasks, memories, channels, messages, routines, audit] = await Promise.all([
|
|
104
|
+
storage.readStore('tasks'),
|
|
105
|
+
storage.readStore('memories'),
|
|
106
|
+
storage.readStore('channels'),
|
|
107
|
+
storage.readStore('messages'),
|
|
108
|
+
storage.readStore('routines'),
|
|
109
|
+
storage.readAuditEvents(),
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
await storage.appendAudit(
|
|
113
|
+
buildAuditEvent({
|
|
114
|
+
tool: TOOL_NAMES.EXPORT,
|
|
115
|
+
op: 'export',
|
|
116
|
+
context,
|
|
117
|
+
details: { include_audit: includeAudit },
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return success({
|
|
122
|
+
exported_at: nowIso(),
|
|
123
|
+
version: '0.1.0',
|
|
124
|
+
data: {
|
|
125
|
+
tasks,
|
|
126
|
+
memories,
|
|
127
|
+
channels,
|
|
128
|
+
messages,
|
|
129
|
+
routines,
|
|
130
|
+
...(includeAudit ? { audit } : {}),
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
// Router (default export)
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
|
|
139
|
+
export default async function systemTools(
|
|
140
|
+
input: unknown,
|
|
141
|
+
context?: ToolContextLike,
|
|
142
|
+
): Promise<ToolOutput> {
|
|
143
|
+
const toolName = asNonEmptyString(context?.tool_name) ?? '';
|
|
144
|
+
|
|
145
|
+
switch (toolName) {
|
|
146
|
+
case TOOL_NAMES.INIT:
|
|
147
|
+
return initTool(context);
|
|
148
|
+
case TOOL_NAMES.STATUS:
|
|
149
|
+
return statusTool(context);
|
|
150
|
+
case TOOL_NAMES.EXPORT:
|
|
151
|
+
return exportTool(input, context);
|
|
152
|
+
default:
|
|
153
|
+
return failure('UNKNOWN_TOOL', `Unknown system tool: ${toolName || 'unknown'}`);
|
|
154
|
+
}
|
|
155
|
+
}
|