@myrialabs/clopen 0.4.40 → 0.4.41
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/.env.example +1 -1
- package/backend/artifacts/detect.ts +15 -3
- package/backend/artifacts/generate.ts +2 -2
- package/backend/artifacts/index.ts +12 -4
- package/backend/artifacts/markers.ts +24 -1
- package/backend/artifacts/matrix.ts +47 -3
- package/backend/artifacts/sync.test.ts +3 -3
- package/backend/artifacts/sync.ts +8 -20
- package/backend/artifacts/types.ts +9 -3
- package/backend/auth/permissions.ts +6 -11
- package/backend/auth/session-cleanup.ts +10 -1
- package/backend/auth/tokens.ts +9 -1
- package/backend/chat/stream-manager.ts +42 -0
- package/backend/database/migrations/079_merge_commands_into_skills.ts +222 -0
- package/backend/database/migrations/080_create_file_shares.ts +111 -0
- package/backend/database/migrations/081_create_push_subscriptions.ts +32 -0
- package/backend/database/migrations/index.ts +21 -0
- package/backend/database/queries/file-share-queries.ts +163 -0
- package/backend/database/queries/index.ts +6 -4
- package/backend/database/queries/profile-queries.ts +5 -3
- package/backend/database/queries/push-subscription-queries.test.ts +90 -0
- package/backend/database/queries/push-subscription-queries.ts +99 -0
- package/backend/database/queries/skill-queries.ts +96 -18
- package/backend/engine/adapters/claude/stream.ts +2 -1
- package/backend/engine/adapters/cline/stream.ts +3 -3
- package/backend/engine/adapters/codex/stream.ts +3 -3
- package/backend/engine/adapters/copilot/stream.ts +3 -3
- package/backend/engine/adapters/cursor/stream.ts +25 -3
- package/backend/engine/adapters/opencode/stream.ts +4 -6
- package/backend/engine/adapters/pi/stream.ts +2 -2
- package/backend/engine/adapters/qwen/stream.ts +2 -2
- package/backend/engine/artifact-sync.ts +26 -20
- package/backend/engine/docs/artifacts.md +81 -22
- package/backend/engine/docs/quick-reference.md +2 -1
- package/backend/engine/resolve-model.test.ts +108 -0
- package/backend/engine/resolve-model.ts +53 -10
- package/backend/engine/structured-helpers.ts +22 -0
- package/backend/files/file-shares.ts +369 -0
- package/backend/files/path-browsing.ts +27 -21
- package/backend/http/files-shared.test.ts +505 -0
- package/backend/http/files-shared.ts +351 -0
- package/backend/http/push.ts +150 -0
- package/backend/index.ts +8 -0
- package/backend/instructions/index.ts +1 -1
- package/backend/instructions/sync.ts +30 -6
- package/backend/memory/compose.ts +2 -2
- package/backend/memory/consolidate.ts +2 -2
- package/backend/memory/extract/episodic.ts +2 -2
- package/backend/memory/judge.ts +2 -2
- package/backend/profiles/service.ts +7 -6
- package/backend/push/sender.ts +172 -0
- package/backend/push/tags.ts +25 -0
- package/backend/push/vapid-keys.ts +77 -0
- package/backend/push/webpush-crypto.test.ts +200 -0
- package/backend/push/webpush-crypto.ts +231 -0
- package/backend/skills/detect.ts +28 -0
- package/backend/skills/index.ts +22 -4
- package/backend/skills/invoke.test.ts +114 -0
- package/backend/skills/invoke.ts +222 -0
- package/backend/skills/service.ts +194 -44
- package/backend/skills/spec.ts +35 -0
- package/backend/skills/sync.ts +85 -55
- package/backend/subagents/detect.ts +2 -3
- package/backend/subagents/sync.ts +15 -8
- package/backend/work/pull-requests.ts +2 -2
- package/backend/ws/chat/stream.ts +100 -8
- package/backend/ws/files/index.ts +4 -0
- package/backend/ws/files/share.ts +95 -0
- package/backend/ws/git/branch-name.ts +2 -2
- package/backend/ws/git/commit-message.ts +2 -2
- package/backend/ws/index.ts +3 -4
- package/backend/ws/profiles/crud.ts +2 -3
- package/backend/ws/skills/catalog.ts +5 -1
- package/backend/ws/skills/crud.ts +128 -22
- package/backend/ws/skills/index.ts +4 -1
- package/backend/ws/user/crud.ts +12 -3
- package/dist/assets/{index-D4igjF6e.js → index-CeNKKSDR.js} +1 -1
- package/dist/assets/{index-DONNwiXm.js → index-DW89566K.js} +1 -1
- package/dist/assets/{main-BrfXH1bL.js → main-Cy5DXWPT.js} +592 -590
- package/dist/assets/main-DJKj_4By.css +1 -0
- package/dist/index.html +7 -2
- package/dist/sw.js +112 -0
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +31 -6
- package/shared/constants/notification-messages.ts +142 -0
- package/shared/types/unified/stream.ts +7 -0
- package/backend/commands/detect.ts +0 -25
- package/backend/commands/index.ts +0 -9
- package/backend/commands/service.ts +0 -178
- package/backend/commands/store.ts +0 -45
- package/backend/commands/sync.ts +0 -94
- package/backend/database/queries/command-queries.ts +0 -82
- package/backend/ws/commands/crud.ts +0 -157
- package/backend/ws/commands/index.ts +0 -8
- package/dist/assets/main-CT1PygsW.css +0 -1
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
4
|
+
import { getClopenDir } from '$backend/utils/paths';
|
|
5
|
+
import { debug } from '$shared/utils/logger';
|
|
6
|
+
|
|
7
|
+
export const description = 'Merge commands into skills: one table, one on-disk store, trigger-based invocation';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Skills and Commands were two features over one mechanic. Both stored a
|
|
11
|
+
* frontmatter + Markdown document, both synced through `materializeArtifacts`,
|
|
12
|
+
* both were profile-bundled — they differed only in WHO pulls the trigger (the
|
|
13
|
+
* model for a Skill, the user for a Command) and in on-disk shape (folder vs
|
|
14
|
+
* single file).
|
|
15
|
+
*
|
|
16
|
+
* This migration folds Commands into Skills. A skill now carries:
|
|
17
|
+
* - `triggers` : comma list of `auto` (model-invoked) and/or `slash`
|
|
18
|
+
* (user-invoked as `/<slug>`). Existing skills get
|
|
19
|
+
* `auto`; migrated commands get `slash`.
|
|
20
|
+
* - `argument_hint` : the old command hint, shown in the "/" picker.
|
|
21
|
+
* - `uses` : JSON array of slugs force-loaded when this skill runs.
|
|
22
|
+
*
|
|
23
|
+
* The old per-command `model_by_engine` override is NOT carried across. A skill
|
|
24
|
+
* runs on the session's model whichever way it is invoked; keeping a model pin
|
|
25
|
+
* on the `slash` half only would have made the two triggers behave differently
|
|
26
|
+
* for no reason a user could see.
|
|
27
|
+
*
|
|
28
|
+
* Each `{clopenDir}/commands/<slug>.md` is COPIED to
|
|
29
|
+
* `{clopenDir}/skills/<slug>/SKILL.md`, and `profile_items` rows of type
|
|
30
|
+
* `'command'` are re-typed to `'skill'` under the (possibly de-duped) slug.
|
|
31
|
+
*
|
|
32
|
+
* Copied, not moved, and an existing SKILL.md is never overwritten with an empty
|
|
33
|
+
* body. A command document is prose the user wrote by hand and Clopen holds the
|
|
34
|
+
* only copy of; a migration that deletes the source has exactly one chance to be
|
|
35
|
+
* right, and if it runs against a database whose files aren't beside it — a DB
|
|
36
|
+
* restored from elsewhere, a data dir assembled by hand — it replaces every
|
|
37
|
+
* prompt with nothing and there is no way back. Leaving `commands/` in place
|
|
38
|
+
* costs a stale folder and buys a second chance.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
interface LegacyCommandRow {
|
|
42
|
+
id: number;
|
|
43
|
+
slug: string;
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
argument_hint: string | null;
|
|
47
|
+
source: string;
|
|
48
|
+
is_enabled: number;
|
|
49
|
+
created_at: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Quote a scalar for YAML output only when it could otherwise be misparsed. */
|
|
53
|
+
function quoteIfNeeded(value: string): string {
|
|
54
|
+
if (value === '') return '""';
|
|
55
|
+
if (/[:#"'\n]|^[\s]|[\s]$|^[[{>|&*!%@`]/.test(value)) {
|
|
56
|
+
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Split a legacy command `.md` into its frontmatter map and body. */
|
|
62
|
+
function parseDoc(raw: string): { frontmatter: Record<string, string>; body: string } {
|
|
63
|
+
const text = raw.replace(/^\uFEFF/, '');
|
|
64
|
+
const match = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n([\s\S]*))?$/.exec(text);
|
|
65
|
+
if (!match) return { frontmatter: {}, body: text.trim() };
|
|
66
|
+
const frontmatter: Record<string, string> = {};
|
|
67
|
+
for (const line of match[1].split(/\r?\n/)) {
|
|
68
|
+
const kv = /^([A-Za-z0-9_-]+):\s*(.*)$/.exec(line);
|
|
69
|
+
if (kv) frontmatter[kv[1]] = kv[2].trim().replace(/^["']|["']$/g, '');
|
|
70
|
+
}
|
|
71
|
+
return { frontmatter, body: (match[2] ?? '').replace(/^\r?\n/, '') };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function columnExists(db: DatabaseConnection, table: string, column: string): boolean {
|
|
75
|
+
const rows = db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[];
|
|
76
|
+
return rows.some(r => r.name === column);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function tableExists(db: DatabaseConnection, table: string): boolean {
|
|
80
|
+
const row = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?`).get(table);
|
|
81
|
+
return !!row;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const up = (db: DatabaseConnection): void => {
|
|
85
|
+
debug.log('migration', 'Merging commands into skills...');
|
|
86
|
+
|
|
87
|
+
// ── 1. Widen the skills table ──
|
|
88
|
+
const columns: [string, string][] = [
|
|
89
|
+
['triggers', `TEXT NOT NULL DEFAULT 'auto'`],
|
|
90
|
+
['argument_hint', 'TEXT'],
|
|
91
|
+
['uses', `TEXT NOT NULL DEFAULT '[]'`]
|
|
92
|
+
];
|
|
93
|
+
for (const [name, definition] of columns) {
|
|
94
|
+
if (!columnExists(db, 'skills', name)) {
|
|
95
|
+
db.exec(`ALTER TABLE skills ADD COLUMN ${name} ${definition}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (!tableExists(db, 'commands')) {
|
|
100
|
+
debug.log('migration', 'No commands table — skills widened, nothing to fold');
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ── 2. Fold every command row into skills ──
|
|
105
|
+
const commands = db.prepare(`SELECT * FROM commands ORDER BY created_at ASC`).all() as LegacyCommandRow[];
|
|
106
|
+
const skillsRoot = join(getClopenDir(), 'skills');
|
|
107
|
+
const commandsRoot = join(getClopenDir(), 'commands');
|
|
108
|
+
|
|
109
|
+
const slugTaken = (slug: string): boolean =>
|
|
110
|
+
!!db.prepare(`SELECT 1 FROM skills WHERE slug = ?`).get(slug);
|
|
111
|
+
|
|
112
|
+
for (const command of commands) {
|
|
113
|
+
// A command slug may collide with an existing skill slug — they were
|
|
114
|
+
// separate namespaces until now. Keep both, suffixing the newcomer.
|
|
115
|
+
let slug = command.slug;
|
|
116
|
+
if (slugTaken(slug)) {
|
|
117
|
+
slug = `${command.slug}-command`.slice(0, 64);
|
|
118
|
+
for (let i = 2; slugTaken(slug) && i < 1000; i++) {
|
|
119
|
+
slug = `${command.slug}-command-${i}`.slice(0, 64);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Copy the document: `commands/<slug>.md` → `skills/<slug>/SKILL.md`.
|
|
124
|
+
const legacyPath = join(commandsRoot, `${command.slug}.md`);
|
|
125
|
+
const skillMdPath = join(skillsRoot, slug, 'SKILL.md');
|
|
126
|
+
|
|
127
|
+
const readBody = (path: string): string => {
|
|
128
|
+
if (!existsSync(path)) return '';
|
|
129
|
+
try {
|
|
130
|
+
return parseDoc(readFileSync(path, 'utf8')).body.trim();
|
|
131
|
+
} catch (error) {
|
|
132
|
+
debug.warn('migration', `Could not read ${path}:`, error);
|
|
133
|
+
return '';
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// Prefer the legacy document; fall back to whatever is already at the
|
|
138
|
+
// destination. The fallback is what makes a second run harmless instead of
|
|
139
|
+
// destructive: without it, a re-run after the source is gone would write an
|
|
140
|
+
// empty body over the one the first run migrated correctly.
|
|
141
|
+
const body = readBody(legacyPath) || readBody(skillMdPath);
|
|
142
|
+
if (!body) {
|
|
143
|
+
debug.warn(
|
|
144
|
+
'migration',
|
|
145
|
+
`Command "${command.slug}" has no body to migrate — ${legacyPath} is missing or empty. ` +
|
|
146
|
+
`The skill is created so it still appears in Settings → Skills, but its instructions must be re-entered.`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const description = command.description.trim() || command.name.trim() || slug;
|
|
151
|
+
const lines = ['---', `name: ${quoteIfNeeded(slug)}`, `description: ${quoteIfNeeded(description)}`, 'triggers: slash'];
|
|
152
|
+
if (command.argument_hint?.trim()) lines.push(`argument-hint: ${quoteIfNeeded(command.argument_hint.trim())}`);
|
|
153
|
+
lines.push('---');
|
|
154
|
+
const document = `${lines.join('\n')}\n\n${body}\n`;
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
mkdirSync(join(skillsRoot, slug), { recursive: true });
|
|
158
|
+
writeFileSync(skillMdPath, document, 'utf8');
|
|
159
|
+
} catch (error) {
|
|
160
|
+
debug.warn('migration', `Could not write skills/${slug}/SKILL.md:`, error);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
db.prepare(
|
|
164
|
+
`INSERT INTO skills (slug, name, description, source, license, is_enabled, created_at, triggers, argument_hint, uses)
|
|
165
|
+
VALUES (?, ?, ?, ?, NULL, ?, ?, 'slash', ?, '[]')`
|
|
166
|
+
).run(
|
|
167
|
+
slug,
|
|
168
|
+
command.name,
|
|
169
|
+
description,
|
|
170
|
+
command.source === 'imported' ? 'imported' : 'custom',
|
|
171
|
+
command.is_enabled,
|
|
172
|
+
command.created_at,
|
|
173
|
+
command.argument_hint
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
// Re-type profile references so a profile that bundled the command keeps
|
|
177
|
+
// bundling it under its new identity.
|
|
178
|
+
db.prepare(
|
|
179
|
+
`UPDATE profile_items SET artifact_type = 'skill', ref = ? WHERE artifact_type = 'command' AND ref = ?`
|
|
180
|
+
).run(slug, command.slug);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Any profile reference to a command that no longer exists is dead weight.
|
|
184
|
+
db.prepare(`DELETE FROM profile_items WHERE artifact_type = 'command'`).run();
|
|
185
|
+
|
|
186
|
+
db.exec('DROP TABLE commands');
|
|
187
|
+
|
|
188
|
+
// `commandsRoot` is deliberately left on disk. Nothing reads it any more, but
|
|
189
|
+
// it is the user's own writing and the only copy Clopen ever had.
|
|
190
|
+
debug.log('migration', `Folded ${commands.length} command(s) into skills (originals kept in ${commandsRoot})`);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const down = (db: DatabaseConnection): void => {
|
|
194
|
+
debug.log('migration', 'Splitting slash skills back into a commands table...');
|
|
195
|
+
db.exec(`
|
|
196
|
+
CREATE TABLE IF NOT EXISTS commands (
|
|
197
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
198
|
+
slug TEXT NOT NULL UNIQUE,
|
|
199
|
+
name TEXT NOT NULL,
|
|
200
|
+
description TEXT NOT NULL DEFAULT '',
|
|
201
|
+
argument_hint TEXT,
|
|
202
|
+
model TEXT,
|
|
203
|
+
model_by_engine TEXT NOT NULL DEFAULT '{}',
|
|
204
|
+
source TEXT NOT NULL DEFAULT 'custom',
|
|
205
|
+
is_enabled INTEGER NOT NULL DEFAULT 1,
|
|
206
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
207
|
+
)
|
|
208
|
+
`);
|
|
209
|
+
// Slash-only skills came from commands; move them back. Skills that carry both
|
|
210
|
+
// triggers stay put — they have no pre-merge equivalent.
|
|
211
|
+
db.exec(`
|
|
212
|
+
INSERT INTO commands (slug, name, description, argument_hint, source, is_enabled, created_at)
|
|
213
|
+
SELECT slug, name, description, argument_hint,
|
|
214
|
+
CASE WHEN source = 'imported' THEN 'imported' ELSE 'custom' END,
|
|
215
|
+
is_enabled, created_at
|
|
216
|
+
FROM skills WHERE triggers = 'slash'
|
|
217
|
+
`);
|
|
218
|
+
db.exec(`DELETE FROM skills WHERE triggers = 'slash'`);
|
|
219
|
+
// The widened columns are left in place: SQLite cannot drop a column without a
|
|
220
|
+
// table rebuild, and leaving them is harmless for the pre-merge code path.
|
|
221
|
+
debug.log('migration', 'commands table restored');
|
|
222
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
2
|
+
import { debug } from '$shared/utils/logger';
|
|
3
|
+
|
|
4
|
+
export const description = 'Create file_shares table for shareable single-file links';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* File shares back "Share Link…" in the File Explorer: an authenticated user
|
|
8
|
+
* mints a link for one file, embeds it in a link/QR, and the receiving device
|
|
9
|
+
* opens exactly that file without signing in.
|
|
10
|
+
*
|
|
11
|
+
* Stored in SQLite rather than process memory for the same reason
|
|
12
|
+
* `device_codes` is (migration 060): the token is a bearer credential, so a
|
|
13
|
+
* restart must not silently invalidate links a user already handed out. Only
|
|
14
|
+
* the SHA-256 hash is kept.
|
|
15
|
+
*
|
|
16
|
+
* Two knobs the creator chooses per link:
|
|
17
|
+
* - `one_time` — burn on the first open (the default), or stay openable.
|
|
18
|
+
* - `expires_at` — a deadline, or NULL for a link that only a revoke ends.
|
|
19
|
+
*
|
|
20
|
+
* `consumed_at` is the one-time burn, set exactly once by a conditional
|
|
21
|
+
* UPDATE, so concurrent opens cannot both win. `consumer_hash` binds the short
|
|
22
|
+
* streaming-continuation window (byte ranges from an already-open media
|
|
23
|
+
* player) to the client that burned the link, so a leaked URL cannot be
|
|
24
|
+
* range-read by anyone else afterwards. `open_count` / `last_opened_at` track
|
|
25
|
+
* use for both modes, which is what the management list reports.
|
|
26
|
+
*/
|
|
27
|
+
export const up = (db: DatabaseConnection): void => {
|
|
28
|
+
debug.log('migration', 'Creating file_shares table...');
|
|
29
|
+
|
|
30
|
+
db.exec(`
|
|
31
|
+
CREATE TABLE IF NOT EXISTS file_shares (
|
|
32
|
+
id TEXT PRIMARY KEY,
|
|
33
|
+
token_hash TEXT NOT NULL UNIQUE,
|
|
34
|
+
file_path TEXT NOT NULL,
|
|
35
|
+
created_by TEXT NOT NULL,
|
|
36
|
+
one_time INTEGER NOT NULL DEFAULT 1,
|
|
37
|
+
expires_at TEXT,
|
|
38
|
+
consumed_at TEXT,
|
|
39
|
+
consumer_hash TEXT,
|
|
40
|
+
open_count INTEGER NOT NULL DEFAULT 0,
|
|
41
|
+
last_opened_at TEXT,
|
|
42
|
+
created_at TEXT NOT NULL,
|
|
43
|
+
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
|
|
44
|
+
)
|
|
45
|
+
`);
|
|
46
|
+
|
|
47
|
+
// `CREATE TABLE IF NOT EXISTS` leaves an existing table exactly as it was.
|
|
48
|
+
// A database that already holds a `file_shares` from an earlier revision of
|
|
49
|
+
// this migration would therefore keep the older shape and fail at runtime —
|
|
50
|
+
// missing `one_time`, and a NOT NULL `expires_at` that rejects the
|
|
51
|
+
// "until revoked" links this version allows. SQLite cannot relax a column
|
|
52
|
+
// constraint in place, so the table is rebuilt (rows carried over, older
|
|
53
|
+
// rows defaulting to the strict one-time behaviour) rather than patched.
|
|
54
|
+
const columns = db.prepare('PRAGMA table_info(file_shares)').all() as { name: string; notnull: number }[];
|
|
55
|
+
const names = new Set(columns.map((c) => c.name));
|
|
56
|
+
const expiresAt = columns.find((c) => c.name === 'expires_at');
|
|
57
|
+
const needsRebuild =
|
|
58
|
+
!names.has('one_time') ||
|
|
59
|
+
!names.has('open_count') ||
|
|
60
|
+
!names.has('last_opened_at') ||
|
|
61
|
+
expiresAt?.notnull === 1;
|
|
62
|
+
|
|
63
|
+
if (needsRebuild) {
|
|
64
|
+
debug.log('migration', 'file_shares: rebuilding an out-of-date table shape...');
|
|
65
|
+
db.exec('DROP TABLE IF EXISTS file_shares_rebuilt');
|
|
66
|
+
db.exec(`
|
|
67
|
+
CREATE TABLE file_shares_rebuilt (
|
|
68
|
+
id TEXT PRIMARY KEY,
|
|
69
|
+
token_hash TEXT NOT NULL UNIQUE,
|
|
70
|
+
file_path TEXT NOT NULL,
|
|
71
|
+
created_by TEXT NOT NULL,
|
|
72
|
+
one_time INTEGER NOT NULL DEFAULT 1,
|
|
73
|
+
expires_at TEXT,
|
|
74
|
+
consumed_at TEXT,
|
|
75
|
+
consumer_hash TEXT,
|
|
76
|
+
open_count INTEGER NOT NULL DEFAULT 0,
|
|
77
|
+
last_opened_at TEXT,
|
|
78
|
+
created_at TEXT NOT NULL,
|
|
79
|
+
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
|
|
80
|
+
)
|
|
81
|
+
`);
|
|
82
|
+
db.exec(`
|
|
83
|
+
INSERT INTO file_shares_rebuilt (
|
|
84
|
+
id, token_hash, file_path, created_by, one_time, expires_at,
|
|
85
|
+
consumed_at, consumer_hash, open_count, last_opened_at, created_at
|
|
86
|
+
)
|
|
87
|
+
SELECT
|
|
88
|
+
id, token_hash, file_path, created_by,
|
|
89
|
+
${names.has('one_time') ? 'one_time' : '1'},
|
|
90
|
+
expires_at, consumed_at,
|
|
91
|
+
${names.has('consumer_hash') ? 'consumer_hash' : 'NULL'},
|
|
92
|
+
${names.has('open_count') ? 'open_count' : '0'},
|
|
93
|
+
${names.has('last_opened_at') ? 'last_opened_at' : 'NULL'},
|
|
94
|
+
created_at
|
|
95
|
+
FROM file_shares
|
|
96
|
+
`);
|
|
97
|
+
db.exec('DROP TABLE file_shares');
|
|
98
|
+
db.exec('ALTER TABLE file_shares_rebuilt RENAME TO file_shares');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
db.exec(`CREATE INDEX IF NOT EXISTS idx_file_shares_user ON file_shares(created_by)`);
|
|
102
|
+
db.exec(`CREATE INDEX IF NOT EXISTS idx_file_shares_expires ON file_shares(expires_at)`);
|
|
103
|
+
|
|
104
|
+
debug.log('migration', 'file_shares table ready');
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const down = (db: DatabaseConnection): void => {
|
|
108
|
+
debug.log('migration', 'Dropping file_shares table...');
|
|
109
|
+
db.exec('DROP TABLE IF EXISTS file_shares');
|
|
110
|
+
debug.log('migration', 'file_shares table dropped');
|
|
111
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
2
|
+
|
|
3
|
+
import { debug } from '$shared/utils/logger';
|
|
4
|
+
export const description = 'Create push_subscriptions table for Web Push (mobile background notifications)';
|
|
5
|
+
|
|
6
|
+
export const up = (db: DatabaseConnection): void => {
|
|
7
|
+
debug.log('migration', '📋 Creating push_subscriptions table...');
|
|
8
|
+
|
|
9
|
+
db.exec(`
|
|
10
|
+
CREATE TABLE push_subscriptions (
|
|
11
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
12
|
+
user_id TEXT NOT NULL,
|
|
13
|
+
endpoint TEXT NOT NULL UNIQUE,
|
|
14
|
+
p256dh TEXT NOT NULL,
|
|
15
|
+
auth TEXT NOT NULL,
|
|
16
|
+
user_agent TEXT,
|
|
17
|
+
created_at TEXT NOT NULL
|
|
18
|
+
)
|
|
19
|
+
`);
|
|
20
|
+
|
|
21
|
+
db.exec(`
|
|
22
|
+
CREATE INDEX idx_push_subscriptions_user_id ON push_subscriptions (user_id)
|
|
23
|
+
`);
|
|
24
|
+
|
|
25
|
+
debug.log('migration', '✅ push_subscriptions table created');
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const down = (db: DatabaseConnection): void => {
|
|
29
|
+
debug.log('migration', '🗑️ Dropping push_subscriptions table...');
|
|
30
|
+
db.exec('DROP TABLE IF EXISTS push_subscriptions');
|
|
31
|
+
debug.log('migration', '✅ push_subscriptions table dropped');
|
|
32
|
+
};
|
|
@@ -77,6 +77,9 @@ import * as migration075 from './075_create_deploy_bindings';
|
|
|
77
77
|
import * as migration076 from './076_remove_memory_code_graph';
|
|
78
78
|
import * as migration077 from './077_create_integration_db_links';
|
|
79
79
|
import * as migration078 from './078_create_worktree_branch_tables';
|
|
80
|
+
import * as migration079 from './079_merge_commands_into_skills';
|
|
81
|
+
import * as migration080 from './080_create_file_shares';
|
|
82
|
+
import * as migration081 from './081_create_push_subscriptions';
|
|
80
83
|
|
|
81
84
|
// Export all migrations in order
|
|
82
85
|
export const migrations = [
|
|
@@ -547,6 +550,24 @@ export const migrations = [
|
|
|
547
550
|
description: migration078.description,
|
|
548
551
|
up: migration078.up,
|
|
549
552
|
down: migration078.down
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
id: '079',
|
|
556
|
+
description: migration079.description,
|
|
557
|
+
up: migration079.up,
|
|
558
|
+
down: migration079.down
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
id: '080',
|
|
562
|
+
description: migration080.description,
|
|
563
|
+
up: migration080.up,
|
|
564
|
+
down: migration080.down
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
id: '081',
|
|
568
|
+
description: migration081.description,
|
|
569
|
+
up: migration081.up,
|
|
570
|
+
down: migration081.down
|
|
550
571
|
}
|
|
551
572
|
];
|
|
552
573
|
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Share Queries
|
|
3
|
+
*
|
|
4
|
+
* Storage for shareable single-file links ("Share Link…" in the File
|
|
5
|
+
* Explorer). Only the token's SHA-256 hash is stored; the raw token lives in
|
|
6
|
+
* the shared URL and is never written down.
|
|
7
|
+
*
|
|
8
|
+
* Everything a caller can act on is keyed by `id`, not by the token: the raw
|
|
9
|
+
* token exists once, in the browser that minted it, so a revoke keyed on the
|
|
10
|
+
* secret would be unreachable the moment that view closes.
|
|
11
|
+
*
|
|
12
|
+
* A NULL `expires_at` means "until revoked" — every query that filters on the
|
|
13
|
+
* deadline has to admit that row, which is why the condition is spelled out
|
|
14
|
+
* rather than left to a plain comparison (SQL comparisons against NULL are
|
|
15
|
+
* neither true nor false, so `expires_at > ?` alone would silently hide them).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { getDatabase } from '../index';
|
|
19
|
+
import { nanoid } from 'nanoid';
|
|
20
|
+
|
|
21
|
+
export interface FileShareRow {
|
|
22
|
+
id: string;
|
|
23
|
+
token_hash: string;
|
|
24
|
+
file_path: string;
|
|
25
|
+
created_by: string;
|
|
26
|
+
/** 1 = dies on the first open, 0 = openable until it expires or is revoked. */
|
|
27
|
+
one_time: number;
|
|
28
|
+
/** ISO deadline, or null for a link that only a revoke ends. */
|
|
29
|
+
expires_at: string | null;
|
|
30
|
+
consumed_at: string | null;
|
|
31
|
+
consumer_hash: string | null;
|
|
32
|
+
open_count: number;
|
|
33
|
+
last_opened_at: string | null;
|
|
34
|
+
created_at: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A row joined with its creator's display name, for the management list. */
|
|
38
|
+
export interface FileShareListRow extends FileShareRow {
|
|
39
|
+
created_by_name: string | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface CreateFileShareParams {
|
|
43
|
+
tokenHash: string;
|
|
44
|
+
filePath: string;
|
|
45
|
+
createdBy: string;
|
|
46
|
+
oneTime: boolean;
|
|
47
|
+
expiresAt: string | null;
|
|
48
|
+
createdAt: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const LIST_SELECT = `
|
|
52
|
+
SELECT s.*, u.name AS created_by_name
|
|
53
|
+
FROM file_shares s
|
|
54
|
+
LEFT JOIN users u ON u.id = s.created_by
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
/** Rows that have not passed their deadline — including the ones without one. */
|
|
58
|
+
const LIVE = '(s.expires_at IS NULL OR s.expires_at > ?)';
|
|
59
|
+
|
|
60
|
+
export const fileShareQueries = {
|
|
61
|
+
create(params: CreateFileShareParams): FileShareRow {
|
|
62
|
+
const db = getDatabase();
|
|
63
|
+
const id = nanoid();
|
|
64
|
+
db.prepare(`
|
|
65
|
+
INSERT INTO file_shares (
|
|
66
|
+
id, token_hash, file_path, created_by, one_time, expires_at,
|
|
67
|
+
consumed_at, consumer_hash, open_count, last_opened_at, created_at
|
|
68
|
+
)
|
|
69
|
+
VALUES (?, ?, ?, ?, ?, ?, NULL, NULL, 0, NULL, ?)
|
|
70
|
+
`).run(
|
|
71
|
+
id,
|
|
72
|
+
params.tokenHash,
|
|
73
|
+
params.filePath,
|
|
74
|
+
params.createdBy,
|
|
75
|
+
params.oneTime ? 1 : 0,
|
|
76
|
+
params.expiresAt,
|
|
77
|
+
params.createdAt
|
|
78
|
+
);
|
|
79
|
+
return db.prepare('SELECT * FROM file_shares WHERE id = ?').get(id) as FileShareRow;
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
getByHash(tokenHash: string): FileShareRow | null {
|
|
83
|
+
const db = getDatabase();
|
|
84
|
+
return db.prepare('SELECT * FROM file_shares WHERE token_hash = ?').get(tokenHash) as FileShareRow | null;
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
getById(id: string): FileShareRow | null {
|
|
88
|
+
const db = getDatabase();
|
|
89
|
+
return db.prepare('SELECT * FROM file_shares WHERE id = ?').get(id) as FileShareRow | null;
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
/** Every live link, newest first — the admin view of what is currently open. */
|
|
93
|
+
listAll(now: string): FileShareListRow[] {
|
|
94
|
+
const db = getDatabase();
|
|
95
|
+
return db.prepare(`${LIST_SELECT} WHERE ${LIVE} ORDER BY s.created_at DESC`)
|
|
96
|
+
.all(now) as FileShareListRow[];
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
/** One user's live links, newest first. */
|
|
100
|
+
listForUser(userId: string, now: string): FileShareListRow[] {
|
|
101
|
+
const db = getDatabase();
|
|
102
|
+
return db.prepare(`${LIST_SELECT} WHERE s.created_by = ? AND ${LIVE} ORDER BY s.created_at DESC`)
|
|
103
|
+
.all(userId, now) as FileShareListRow[];
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Burn the single use of a one-time link. Conditional on `consumed_at IS
|
|
108
|
+
* NULL` and on the deadline, so exactly one of any number of concurrent
|
|
109
|
+
* opens gets `changes === 1` — the same single-use guarantee
|
|
110
|
+
* `markDeviceCodeClaimed` relies on.
|
|
111
|
+
*/
|
|
112
|
+
consume(tokenHash: string, consumedAt: string, consumerHash: string): number {
|
|
113
|
+
const db = getDatabase();
|
|
114
|
+
const result = db.prepare(`
|
|
115
|
+
UPDATE file_shares
|
|
116
|
+
SET consumed_at = ?, consumer_hash = ?, open_count = open_count + 1, last_opened_at = ?
|
|
117
|
+
WHERE token_hash = ?
|
|
118
|
+
AND one_time = 1
|
|
119
|
+
AND consumed_at IS NULL
|
|
120
|
+
AND (expires_at IS NULL OR expires_at > ?)
|
|
121
|
+
`).run(consumedAt, consumerHash, consumedAt, tokenHash, consumedAt) as { changes: number };
|
|
122
|
+
return result.changes;
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
/** Record an open of a reusable link — the counter the list reports from. */
|
|
126
|
+
recordOpen(tokenHash: string, openedAt: string): number {
|
|
127
|
+
const db = getDatabase();
|
|
128
|
+
const result = db.prepare(`
|
|
129
|
+
UPDATE file_shares
|
|
130
|
+
SET open_count = open_count + 1, last_opened_at = ?
|
|
131
|
+
WHERE token_hash = ?
|
|
132
|
+
`).run(openedAt, tokenHash) as { changes: number };
|
|
133
|
+
return result.changes;
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
deleteById(id: string): number {
|
|
137
|
+
const db = getDatabase();
|
|
138
|
+
const result = db.prepare('DELETE FROM file_shares WHERE id = ?').run(id) as { changes: number };
|
|
139
|
+
return result.changes;
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
deleteByHash(tokenHash: string): number {
|
|
143
|
+
const db = getDatabase();
|
|
144
|
+
const result = db.prepare('DELETE FROM file_shares WHERE token_hash = ?').run(tokenHash) as { changes: number };
|
|
145
|
+
return result.changes;
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Remove links that can never serve bytes again: past their deadline, or
|
|
150
|
+
* burned longer ago than the streaming-continuation window. A link without
|
|
151
|
+
* a deadline is never swept — ending it is a deliberate revoke.
|
|
152
|
+
*/
|
|
153
|
+
deleteStale(drainCutoff: string): number {
|
|
154
|
+
const db = getDatabase();
|
|
155
|
+
const now = new Date().toISOString();
|
|
156
|
+
const result = db.prepare(`
|
|
157
|
+
DELETE FROM file_shares
|
|
158
|
+
WHERE (expires_at IS NOT NULL AND expires_at < ?)
|
|
159
|
+
OR (consumed_at IS NOT NULL AND consumed_at < ?)
|
|
160
|
+
`).run(now, drainCutoff) as { changes: number };
|
|
161
|
+
return result.changes;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
@@ -16,12 +16,12 @@ export { sshKnownHostQueries } from './ssh-known-host-queries';
|
|
|
16
16
|
export { sshPortForwardQueries } from './ssh-port-forward-queries';
|
|
17
17
|
export { auditLogQueries } from './audit-log-queries';
|
|
18
18
|
export { fileAuditLogQueries } from './file-audit-log-queries';
|
|
19
|
+
export { fileShareQueries } from './file-share-queries';
|
|
20
|
+
export type { FileShareRow, FileShareListRow } from './file-share-queries';
|
|
19
21
|
export { mcpServerQueries } from './mcp-server-queries';
|
|
20
22
|
export type { McpServerRow, McpServerInput, McpTransport, McpSource, McpConfigField, McpToolOverride, McpToolOverrides } from './mcp-server-queries';
|
|
21
|
-
export { skillQueries } from './skill-queries';
|
|
22
|
-
export type { SkillRow, SkillInput, SkillSource } from './skill-queries';
|
|
23
|
-
export { commandQueries } from './command-queries';
|
|
24
|
-
export type { CommandRow, CommandInput, CommandSource } from './command-queries';
|
|
23
|
+
export { skillQueries, parseTriggers, stringifyTriggers, parseUses, stringifyUses, SKILL_TRIGGERS } from './skill-queries';
|
|
24
|
+
export type { SkillRow, SkillInput, SkillSource, SkillTrigger } from './skill-queries';
|
|
25
25
|
export { subagentQueries } from './subagent-queries';
|
|
26
26
|
export type { SubagentRow, SubagentInput, SubagentSource } from './subagent-queries';
|
|
27
27
|
export { instructionQueries } from './instruction-queries';
|
|
@@ -46,4 +46,6 @@ export {
|
|
|
46
46
|
worktreeBranchQueries,
|
|
47
47
|
parseWorktreeBranchConfig
|
|
48
48
|
} from './worktree-branch-queries';
|
|
49
|
+
export { pushSubscriptionQueries } from './push-subscription-queries';
|
|
50
|
+
export type { PushSubscription, PushSubscriptionInput } from './push-subscription-queries';
|
|
49
51
|
export type { WorktreeBranchBindingRow, WorktreeBranchRow } from './worktree-branch-queries';
|
|
@@ -14,10 +14,12 @@
|
|
|
14
14
|
import { getDatabase } from '../index';
|
|
15
15
|
|
|
16
16
|
/** Artifact kinds a profile can bundle. Permissions are handled separately
|
|
17
|
-
* (they are a per-engine allow/deny overlay, not a slug-referenced artifact).
|
|
18
|
-
|
|
17
|
+
* (they are a per-engine allow/deny overlay, not a slug-referenced artifact).
|
|
18
|
+
* `'command'` is gone since migration 079 folded Commands into Skills — the
|
|
19
|
+
* migration re-typed existing `command` rows to `skill` under their new slug. */
|
|
20
|
+
export type ProfileItemType = 'skill' | 'subagent' | 'mcp';
|
|
19
21
|
|
|
20
|
-
export const PROFILE_ITEM_TYPES: ProfileItemType[] = ['skill', '
|
|
22
|
+
export const PROFILE_ITEM_TYPES: ProfileItemType[] = ['skill', 'subagent', 'mcp'];
|
|
21
23
|
|
|
22
24
|
export interface ProfileRow {
|
|
23
25
|
id: number;
|