@myrialabs/clopen 0.4.39 → 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 +29 -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/crypto/secret-columns.ts +5 -1
- package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -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 +28 -0
- package/backend/database/queries/file-share-queries.ts +163 -0
- package/backend/database/queries/index.ts +12 -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/database/queries/worktree-branch-queries.ts +331 -0
- package/backend/db-client/cloning.test.ts +161 -0
- package/backend/db-client/cloning.ts +672 -0
- package/backend/db-client/connection-url.test.ts +117 -0
- package/backend/db-client/connection-url.ts +126 -0
- package/backend/db-client/drivers/mongodb.ts +2 -14
- package/backend/db-client/drivers/mysql.ts +2 -8
- package/backend/db-client/drivers/postgres.ts +10 -13
- package/backend/db-client/drivers/redis.ts +2 -6
- package/backend/db-client/env/detect.ts +438 -0
- package/backend/db-client/env/diff.ts +94 -0
- package/backend/db-client/env/env.test.ts +574 -0
- package/backend/db-client/env/index.ts +15 -0
- package/backend/db-client/env/naming.ts +202 -0
- package/backend/db-client/env/service.ts +487 -0
- package/backend/db-client/integrations/index.ts +7 -0
- package/backend/db-client/integrations/links.ts +41 -10
- package/backend/db-client/providers/neon/adapter.ts +522 -0
- package/backend/db-client/providers/neon/api-types.ts +182 -0
- package/backend/db-client/providers/neon/client.ts +298 -0
- package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
- package/backend/db-client/providers/neon/endpoints.ts +314 -0
- package/backend/db-client/providers/neon/list.test.ts +181 -0
- package/backend/db-client/providers/supabase/adapter.ts +25 -2
- package/backend/db-client/providers/types.ts +57 -0
- 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/env-files/discovery.ts +48 -0
- package/backend/env-files/edit.ts +213 -0
- package/backend/env-files/env-files.test.ts +515 -0
- package/backend/env-files/git.ts +27 -0
- package/backend/env-files/index.ts +44 -0
- package/backend/env-files/keys.ts +69 -0
- package/backend/env-files/parse.ts +157 -0
- package/backend/env-files/write.ts +229 -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/integrations/health.ts +18 -4
- package/backend/integrations/projections/types.ts +9 -2
- package/backend/integrations/registry/index.ts +3 -0
- package/backend/integrations/registry/providers/context7.ts +1 -0
- package/backend/integrations/registry/providers/firecrawl.ts +1 -0
- package/backend/integrations/registry/providers/github.ts +1 -0
- package/backend/integrations/registry/providers/neon.ts +55 -0
- package/backend/integrations/registry/providers/supabase.ts +1 -0
- package/backend/integrations/registry/providers/vercel.ts +1 -0
- package/backend/integrations/registry/types.ts +2 -0
- 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/work/start-work.ts +4 -0
- package/backend/worktrees/branching/engine.ts +557 -0
- package/backend/worktrees/branching/index.ts +56 -0
- package/backend/worktrees/branching/naming.test.ts +118 -0
- package/backend/worktrees/branching/naming.ts +97 -0
- package/backend/worktrees/branching/projector.test.ts +229 -0
- package/backend/worktrees/branching/projector.ts +139 -0
- package/backend/worktrees/branching/providers/neon.ts +220 -0
- package/backend/worktrees/branching/registry.ts +27 -0
- package/backend/worktrees/branching/service.ts +620 -0
- package/backend/worktrees/branching/suggest.test.ts +154 -0
- package/backend/worktrees/branching/suggest.ts +202 -0
- package/backend/worktrees/branching/types.ts +88 -0
- package/backend/worktrees/index.ts +7 -0
- package/backend/worktrees/service.ts +48 -1
- package/backend/ws/chat/stream.ts +100 -8
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- 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/projects/index.ts +6 -1
- package/backend/ws/projects/info.ts +70 -6
- package/backend/ws/projects/overview.test.ts +108 -0
- package/backend/ws/projects/overview.ts +300 -0
- 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/system/device-info.ts +307 -76
- package/backend/ws/user/crud.ts +12 -3
- package/backend/ws/worktrees/branching.ts +212 -0
- package/backend/ws/worktrees/crud.ts +33 -7
- package/backend/ws/worktrees/index.ts +14 -0
- package/dist/assets/{index-C8Gi-b6U.js → index-CeNKKSDR.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DW89566K.js} +1 -1
- package/dist/assets/main-Cy5DXWPT.js +1207 -0
- 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/constants/tool-icons.ts +6 -2
- package/shared/types/db-client/env.ts +192 -0
- package/shared/types/db-client/index.ts +1 -0
- package/shared/types/db-client/integrations.ts +30 -0
- package/shared/types/integrations.ts +9 -0
- package/shared/types/unified/stream.ts +7 -0
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -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-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
base64UrlDecode,
|
|
5
|
+
base64UrlEncode,
|
|
6
|
+
createVapidAuthorizationHeader,
|
|
7
|
+
encryptPushPayload,
|
|
8
|
+
generateVapidKeypair
|
|
9
|
+
} from './webpush-crypto';
|
|
10
|
+
|
|
11
|
+
const textEncoder = new TextEncoder();
|
|
12
|
+
const textDecoder = new TextDecoder();
|
|
13
|
+
|
|
14
|
+
function concat(...parts: Uint8Array[]): Uint8Array<ArrayBuffer> {
|
|
15
|
+
const total = parts.reduce((sum, part) => sum + part.length, 0);
|
|
16
|
+
const out = new Uint8Array(total);
|
|
17
|
+
let offset = 0;
|
|
18
|
+
for (const part of parts) {
|
|
19
|
+
out.set(part, offset);
|
|
20
|
+
offset += part.length;
|
|
21
|
+
}
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function hmac(
|
|
26
|
+
key: Uint8Array<ArrayBuffer>,
|
|
27
|
+
data: Uint8Array<ArrayBuffer>
|
|
28
|
+
): Promise<Uint8Array<ArrayBuffer>> {
|
|
29
|
+
const hmacKey = await crypto.subtle.importKey(
|
|
30
|
+
'raw',
|
|
31
|
+
key,
|
|
32
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
33
|
+
false,
|
|
34
|
+
['sign']
|
|
35
|
+
);
|
|
36
|
+
return new Uint8Array(await crypto.subtle.sign('HMAC', hmacKey, data));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Independent HKDF (manual HMAC Extract + Expand) — deliberately NOT the
|
|
41
|
+
* module's native-HKDF path, so agreement between the two proves the math
|
|
42
|
+
* rather than repeating the same code.
|
|
43
|
+
*/
|
|
44
|
+
async function hkdfManual(
|
|
45
|
+
salt: Uint8Array<ArrayBuffer>,
|
|
46
|
+
ikm: Uint8Array<ArrayBuffer>,
|
|
47
|
+
info: Uint8Array<ArrayBuffer>,
|
|
48
|
+
length: number
|
|
49
|
+
): Promise<Uint8Array<ArrayBuffer>> {
|
|
50
|
+
const prk = await hmac(salt, ikm);
|
|
51
|
+
return (await hmac(prk, concat(info, new Uint8Array([0x01])))).slice(0, length);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Simulate the browser side: generate a subscription keypair + auth secret. */
|
|
55
|
+
async function makeBrowserSubscription() {
|
|
56
|
+
const keypair = await crypto.subtle.generateKey(
|
|
57
|
+
{ name: 'ECDH', namedCurve: 'P-256' },
|
|
58
|
+
true,
|
|
59
|
+
['deriveBits']
|
|
60
|
+
);
|
|
61
|
+
const publicBytes = new Uint8Array(await crypto.subtle.exportKey('raw', keypair.publicKey));
|
|
62
|
+
const authSecret = crypto.getRandomValues(new Uint8Array(16));
|
|
63
|
+
return {
|
|
64
|
+
privateKey: keypair.privateKey,
|
|
65
|
+
p256dh: base64UrlEncode(publicBytes),
|
|
66
|
+
auth: base64UrlEncode(authSecret),
|
|
67
|
+
authBytes: authSecret
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe('base64url', () => {
|
|
72
|
+
test('round-trips arbitrary bytes without padding', () => {
|
|
73
|
+
const bytes = crypto.getRandomValues(new Uint8Array(65));
|
|
74
|
+
expect(base64UrlDecode(base64UrlEncode(bytes))).toEqual(bytes);
|
|
75
|
+
expect(base64UrlEncode(bytes)).not.toContain('=');
|
|
76
|
+
expect(base64UrlEncode(bytes)).not.toMatch(/[+/]/);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('VAPID', () => {
|
|
81
|
+
test('generates a 65-byte uncompressed public key', async () => {
|
|
82
|
+
const keypair = await generateVapidKeypair();
|
|
83
|
+
const pub = base64UrlDecode(keypair.publicKey);
|
|
84
|
+
expect(pub.length).toBe(65);
|
|
85
|
+
expect(pub[0]).toBe(0x04);
|
|
86
|
+
expect(base64UrlDecode(keypair.privateKey).length).toBe(32);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('produces a verifiable ES256 JWT with aud/exp/sub', async () => {
|
|
90
|
+
const keypair = await generateVapidKeypair();
|
|
91
|
+
const header = await createVapidAuthorizationHeader(
|
|
92
|
+
'https://fcm.googleapis.com',
|
|
93
|
+
'mailto:admin@example.com',
|
|
94
|
+
keypair
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
expect(header).toStartWith('vapid t=');
|
|
98
|
+
const token = header.slice('vapid t='.length).split(', k=')[0];
|
|
99
|
+
const [headerB64, claimsB64, signatureB64] = token.split('.');
|
|
100
|
+
expect(JSON.parse(textDecoder.decode(base64UrlDecode(headerB64)))).toEqual({
|
|
101
|
+
typ: 'JWT',
|
|
102
|
+
alg: 'ES256'
|
|
103
|
+
});
|
|
104
|
+
const claims = JSON.parse(textDecoder.decode(base64UrlDecode(claimsB64)));
|
|
105
|
+
expect(claims.aud).toBe('https://fcm.googleapis.com');
|
|
106
|
+
expect(claims.sub).toBe('mailto:admin@example.com');
|
|
107
|
+
expect(claims.exp).toBeGreaterThan(Math.floor(Date.now() / 1000));
|
|
108
|
+
|
|
109
|
+
const pubBytes = base64UrlDecode(keypair.publicKey);
|
|
110
|
+
const publicKey = await crypto.subtle.importKey(
|
|
111
|
+
'jwk',
|
|
112
|
+
{
|
|
113
|
+
kty: 'EC',
|
|
114
|
+
crv: 'P-256',
|
|
115
|
+
x: base64UrlEncode(pubBytes.slice(1, 33)),
|
|
116
|
+
y: base64UrlEncode(pubBytes.slice(33, 65))
|
|
117
|
+
},
|
|
118
|
+
{ name: 'ECDSA', namedCurve: 'P-256' },
|
|
119
|
+
false,
|
|
120
|
+
['verify']
|
|
121
|
+
);
|
|
122
|
+
const valid = await crypto.subtle.verify(
|
|
123
|
+
{ name: 'ECDSA', hash: 'SHA-256' },
|
|
124
|
+
publicKey,
|
|
125
|
+
base64UrlDecode(signatureB64),
|
|
126
|
+
textEncoder.encode(`${headerB64}.${claimsB64}`)
|
|
127
|
+
);
|
|
128
|
+
expect(valid).toBe(true);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('encryptPushPayload', () => {
|
|
133
|
+
test('encrypts so the subscription owner can decrypt (full round-trip)', async () => {
|
|
134
|
+
const sub = await makeBrowserSubscription();
|
|
135
|
+
const payload = textEncoder.encode(
|
|
136
|
+
JSON.stringify({ title: 'Response ready', body: 'MyApp — "Fix login bug"', tag: 't-1' })
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const { body } = await encryptPushPayload(sub.p256dh, sub.auth, payload);
|
|
140
|
+
|
|
141
|
+
// Header + ciphertext overhead: 86-byte header, payload + 1 delimiter + 16 tag.
|
|
142
|
+
expect(body.length).toBe(86 + payload.length + 1 + 16);
|
|
143
|
+
|
|
144
|
+
// --- browser side, independent code path ---
|
|
145
|
+
const salt = body.slice(0, 16);
|
|
146
|
+
const serverPublicBytes = body.slice(21, 86);
|
|
147
|
+
const ciphertext = body.slice(86);
|
|
148
|
+
const clientPublicBytes = base64UrlDecode(sub.p256dh);
|
|
149
|
+
|
|
150
|
+
const serverPublicKey = await crypto.subtle.importKey(
|
|
151
|
+
'raw',
|
|
152
|
+
serverPublicBytes,
|
|
153
|
+
{ name: 'ECDH', namedCurve: 'P-256' },
|
|
154
|
+
false,
|
|
155
|
+
[]
|
|
156
|
+
);
|
|
157
|
+
const sharedSecret = new Uint8Array(
|
|
158
|
+
await crypto.subtle.deriveBits(
|
|
159
|
+
{ name: 'ECDH', public: serverPublicKey },
|
|
160
|
+
sub.privateKey,
|
|
161
|
+
256
|
|
162
|
+
)
|
|
163
|
+
);
|
|
164
|
+
const keyInfo = concat(
|
|
165
|
+
textEncoder.encode('WebPush: info\0'),
|
|
166
|
+
clientPublicBytes,
|
|
167
|
+
serverPublicBytes
|
|
168
|
+
);
|
|
169
|
+
const secret = await hkdfManual(sub.authBytes, sharedSecret, keyInfo, 32);
|
|
170
|
+
const cek = await hkdfManual(
|
|
171
|
+
salt,
|
|
172
|
+
secret,
|
|
173
|
+
textEncoder.encode('Content-Encoding: aes128gcm\0'),
|
|
174
|
+
16
|
|
175
|
+
);
|
|
176
|
+
const nonce = await hkdfManual(
|
|
177
|
+
salt,
|
|
178
|
+
secret,
|
|
179
|
+
textEncoder.encode('Content-Encoding: nonce\0'),
|
|
180
|
+
12
|
|
181
|
+
);
|
|
182
|
+
const aesKey = await crypto.subtle.importKey('raw', cek, 'AES-GCM', false, ['decrypt']);
|
|
183
|
+
const plaintext = new Uint8Array(
|
|
184
|
+
await crypto.subtle.decrypt({ name: 'AES-GCM', iv: nonce }, aesKey, ciphertext)
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
expect(plaintext[plaintext.length - 1]).toBe(0x02);
|
|
188
|
+
expect(textDecoder.decode(plaintext.slice(0, -1))).toBe(textDecoder.decode(payload));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('rejects malformed subscription keys', async () => {
|
|
192
|
+
const sub = await makeBrowserSubscription();
|
|
193
|
+
await expect(
|
|
194
|
+
encryptPushPayload('not-a-key', sub.auth, new Uint8Array([1]))
|
|
195
|
+
).rejects.toThrow();
|
|
196
|
+
await expect(
|
|
197
|
+
encryptPushPayload(sub.p256dh, base64UrlEncode(new Uint8Array([1, 2])), new Uint8Array([1]))
|
|
198
|
+
).rejects.toThrow();
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push cryptography (RFC 8291 `aes128gcm` + RFC 8292 VAPID).
|
|
3
|
+
*
|
|
4
|
+
* Implemented with the platform WebCrypto API instead of a `web-push`
|
|
5
|
+
* dependency: Bun ships ECDH P-256, HKDF, AES-GCM and ECDSA, which is
|
|
6
|
+
* everything the protocol needs. No new supply chain for a
|
|
7
|
+
* security-sensitive path.
|
|
8
|
+
*
|
|
9
|
+
* Two pieces:
|
|
10
|
+
* - VAPID: an ES256-signed JWT proving the push originates from this server.
|
|
11
|
+
* - Content encryption: the payload is encrypted to the browser's per-
|
|
12
|
+
* subscription key, so the push service (Google/Mozilla/Apple) relaying it
|
|
13
|
+
* only ever sees ciphertext.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const textEncoder = new TextEncoder();
|
|
17
|
+
|
|
18
|
+
export function base64UrlEncode(data: Uint8Array | ArrayBuffer): string {
|
|
19
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
20
|
+
let binary = '';
|
|
21
|
+
const CHUNK = 0x8000;
|
|
22
|
+
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
23
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
|
|
24
|
+
}
|
|
25
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function base64UrlDecode(input: string): Uint8Array<ArrayBuffer> {
|
|
29
|
+
const base64 = input.replace(/-/g, '+').replace(/_/g, '/');
|
|
30
|
+
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4);
|
|
31
|
+
const binary = atob(padded);
|
|
32
|
+
const out = new Uint8Array(binary.length);
|
|
33
|
+
for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function concat(...parts: Uint8Array[]): Uint8Array<ArrayBuffer> {
|
|
38
|
+
const total = parts.reduce((sum, part) => sum + part.length, 0);
|
|
39
|
+
const out = new Uint8Array(total);
|
|
40
|
+
let offset = 0;
|
|
41
|
+
for (const part of parts) {
|
|
42
|
+
out.set(part, offset);
|
|
43
|
+
offset += part.length;
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ============================================================================
|
|
49
|
+
// VAPID (RFC 8292)
|
|
50
|
+
// ============================================================================
|
|
51
|
+
|
|
52
|
+
export interface VapidKeypair {
|
|
53
|
+
/** Uncompressed P-256 public key (65 bytes), base64url. */
|
|
54
|
+
publicKey: string;
|
|
55
|
+
/** Raw P-256 private key (32 bytes), base64url. */
|
|
56
|
+
privateKey: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function generateVapidKeypair(): Promise<VapidKeypair> {
|
|
60
|
+
const keypair = await crypto.subtle.generateKey(
|
|
61
|
+
{ name: 'ECDSA', namedCurve: 'P-256' },
|
|
62
|
+
true,
|
|
63
|
+
['sign', 'verify']
|
|
64
|
+
);
|
|
65
|
+
const publicRaw = new Uint8Array(await crypto.subtle.exportKey('raw', keypair.publicKey));
|
|
66
|
+
const privateJwk = await crypto.subtle.exportKey('jwk', keypair.privateKey);
|
|
67
|
+
if (!privateJwk.d) throw new Error('Failed to export VAPID private key');
|
|
68
|
+
return { publicKey: base64UrlEncode(publicRaw), privateKey: privateJwk.d };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Build the `Authorization` header for a push request.
|
|
73
|
+
*
|
|
74
|
+
* `audience` is the push service origin (`new URL(endpoint).origin`), so a
|
|
75
|
+
* token minted for FCM cannot be replayed at another service. `subject`
|
|
76
|
+
* identifies the server operator (`mailto:` or URL).
|
|
77
|
+
*/
|
|
78
|
+
export async function createVapidAuthorizationHeader(
|
|
79
|
+
audience: string,
|
|
80
|
+
subject: string,
|
|
81
|
+
keypair: VapidKeypair,
|
|
82
|
+
ttlSeconds = 12 * 3600
|
|
83
|
+
): Promise<string> {
|
|
84
|
+
const header = base64UrlEncode(textEncoder.encode(JSON.stringify({ typ: 'JWT', alg: 'ES256' })));
|
|
85
|
+
const nowSeconds = Math.floor(Date.now() / 1000);
|
|
86
|
+
const claims = base64UrlEncode(
|
|
87
|
+
textEncoder.encode(
|
|
88
|
+
JSON.stringify({ aud: audience, exp: nowSeconds + ttlSeconds, sub: subject })
|
|
89
|
+
)
|
|
90
|
+
);
|
|
91
|
+
const signingInput = textEncoder.encode(`${header}.${claims}`);
|
|
92
|
+
|
|
93
|
+
const publicBytes = base64UrlDecode(keypair.publicKey);
|
|
94
|
+
if (publicBytes.length !== 65 || publicBytes[0] !== 0x04) {
|
|
95
|
+
throw new Error('Invalid VAPID public key (expected 65-byte uncompressed point)');
|
|
96
|
+
}
|
|
97
|
+
const privateKey = await crypto.subtle.importKey(
|
|
98
|
+
'jwk',
|
|
99
|
+
{
|
|
100
|
+
kty: 'EC',
|
|
101
|
+
crv: 'P-256',
|
|
102
|
+
x: base64UrlEncode(publicBytes.slice(1, 33)),
|
|
103
|
+
y: base64UrlEncode(publicBytes.slice(33, 65)),
|
|
104
|
+
d: keypair.privateKey
|
|
105
|
+
},
|
|
106
|
+
{ name: 'ECDSA', namedCurve: 'P-256' },
|
|
107
|
+
false,
|
|
108
|
+
['sign']
|
|
109
|
+
);
|
|
110
|
+
const signature = await crypto.subtle.sign(
|
|
111
|
+
{ name: 'ECDSA', hash: 'SHA-256' },
|
|
112
|
+
privateKey,
|
|
113
|
+
signingInput
|
|
114
|
+
);
|
|
115
|
+
return `vapid t=${header}.${claims}.${base64UrlEncode(signature)}, k=${keypair.publicKey}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ============================================================================
|
|
119
|
+
// Content encryption (RFC 8291, `aes128gcm`, single record)
|
|
120
|
+
// ============================================================================
|
|
121
|
+
|
|
122
|
+
/** Record size announced in the header. 4096 is what every push service accepts. */
|
|
123
|
+
const RECORD_SIZE = 4096;
|
|
124
|
+
|
|
125
|
+
export interface EncryptedPushPayload {
|
|
126
|
+
/** Full request body: 16-byte salt + 4-byte rs + 1-byte idlen + 65-byte key + ciphertext. */
|
|
127
|
+
body: Uint8Array<ArrayBuffer>;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* HKDF-Extract + first-block Expand, per RFC 5869 as used by RFC 8291.
|
|
132
|
+
*
|
|
133
|
+
* `secret = HKDF(salt, IKM, info, length)` means Extract(salt, IKM) then the
|
|
134
|
+
* first `length` bytes of Expand — the same construction the reference
|
|
135
|
+
* `web-push` implementation computes, so ciphertext decrypts in real
|
|
136
|
+
* browsers.
|
|
137
|
+
*/
|
|
138
|
+
async function hkdf(
|
|
139
|
+
salt: Uint8Array<ArrayBuffer>,
|
|
140
|
+
ikm: Uint8Array<ArrayBuffer>,
|
|
141
|
+
info: Uint8Array<ArrayBuffer>,
|
|
142
|
+
length: number
|
|
143
|
+
): Promise<Uint8Array<ArrayBuffer>> {
|
|
144
|
+
const extractKey = await crypto.subtle.importKey('raw', ikm, 'HKDF', false, ['deriveBits']);
|
|
145
|
+
return new Uint8Array(
|
|
146
|
+
await crypto.subtle.deriveBits(
|
|
147
|
+
{ name: 'HKDF', hash: 'SHA-256', salt, info },
|
|
148
|
+
extractKey,
|
|
149
|
+
length * 8
|
|
150
|
+
)
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Encrypt a payload for one subscription.
|
|
156
|
+
*
|
|
157
|
+
* Throws on malformed subscription keys — callers translate that into
|
|
158
|
+
* dropping the dead subscription, never into failing the whole fan-out.
|
|
159
|
+
*/
|
|
160
|
+
export async function encryptPushPayload(
|
|
161
|
+
clientPublicKeyBase64: string,
|
|
162
|
+
clientAuthSecretBase64: string,
|
|
163
|
+
payload: Uint8Array
|
|
164
|
+
): Promise<EncryptedPushPayload> {
|
|
165
|
+
const clientPublicBytes = base64UrlDecode(clientPublicKeyBase64);
|
|
166
|
+
if (clientPublicBytes.length !== 65 || clientPublicBytes[0] !== 0x04) {
|
|
167
|
+
throw new Error('Invalid subscription p256dh key');
|
|
168
|
+
}
|
|
169
|
+
const authSecret = base64UrlDecode(clientAuthSecretBase64);
|
|
170
|
+
if (authSecret.length < 16) {
|
|
171
|
+
throw new Error('Invalid subscription auth secret');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const ephemeral = await crypto.subtle.generateKey(
|
|
175
|
+
{ name: 'ECDH', namedCurve: 'P-256' },
|
|
176
|
+
true,
|
|
177
|
+
['deriveBits']
|
|
178
|
+
);
|
|
179
|
+
const ephemeralPublicBytes = new Uint8Array(
|
|
180
|
+
await crypto.subtle.exportKey('raw', ephemeral.publicKey)
|
|
181
|
+
);
|
|
182
|
+
const clientPublicKey = await crypto.subtle.importKey(
|
|
183
|
+
'raw',
|
|
184
|
+
clientPublicBytes,
|
|
185
|
+
{ name: 'ECDH', namedCurve: 'P-256' },
|
|
186
|
+
false,
|
|
187
|
+
[]
|
|
188
|
+
);
|
|
189
|
+
const sharedSecret = new Uint8Array(
|
|
190
|
+
await crypto.subtle.deriveBits(
|
|
191
|
+
{ name: 'ECDH', public: clientPublicKey },
|
|
192
|
+
ephemeral.privateKey,
|
|
193
|
+
256
|
|
194
|
+
)
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
198
|
+
|
|
199
|
+
// `key_info` binds the secret to this exact sender/receiver pair, so a
|
|
200
|
+
// ciphertext cannot be transplanted onto another subscription.
|
|
201
|
+
const keyInfo = concat(
|
|
202
|
+
textEncoder.encode('WebPush: info\0'),
|
|
203
|
+
clientPublicBytes,
|
|
204
|
+
ephemeralPublicBytes
|
|
205
|
+
);
|
|
206
|
+
const secret = await hkdf(authSecret, sharedSecret, keyInfo, 32);
|
|
207
|
+
const contentEncryptionKey = await hkdf(
|
|
208
|
+
salt,
|
|
209
|
+
secret,
|
|
210
|
+
textEncoder.encode('Content-Encoding: aes128gcm\0'),
|
|
211
|
+
16
|
|
212
|
+
);
|
|
213
|
+
const nonce = await hkdf(salt, secret, textEncoder.encode('Content-Encoding: nonce\0'), 12);
|
|
214
|
+
|
|
215
|
+
// Single record: payload + 0x02 delimiter, no extra padding.
|
|
216
|
+
const plaintext = concat(payload, new Uint8Array([0x02]));
|
|
217
|
+
const aesKey = await crypto.subtle.importKey('raw', contentEncryptionKey, 'AES-GCM', false, [
|
|
218
|
+
'encrypt'
|
|
219
|
+
]);
|
|
220
|
+
const ciphertext = new Uint8Array(
|
|
221
|
+
await crypto.subtle.encrypt({ name: 'AES-GCM', iv: nonce }, aesKey, plaintext)
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const header = new Uint8Array(16 + 4 + 1 + ephemeralPublicBytes.length);
|
|
225
|
+
header.set(salt, 0);
|
|
226
|
+
new DataView(header.buffer).setUint32(16, RECORD_SIZE, false);
|
|
227
|
+
header[20] = ephemeralPublicBytes.length;
|
|
228
|
+
header.set(ephemeralPublicBytes, 21);
|
|
229
|
+
|
|
230
|
+
return { body: concat(header, ciphertext) };
|
|
231
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect on-disk skills (managed effective copies + adoptable real ones) for the
|
|
3
|
+
* Settings UI — the same "detect & link" surface Subagents already had, which
|
|
4
|
+
* Skills never got despite being the feature most likely to already exist on a
|
|
5
|
+
* developer's machine (`~/.claude/skills`, `~/.qwen/skills`, …).
|
|
6
|
+
*
|
|
7
|
+
* Global scope only for now — project scope is surfaced by the per-project
|
|
8
|
+
* detection path when a project is supplied.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { detectArtifacts, ARTIFACT_ENGINES, type DetectedArtifact, type ArtifactEngine } from '$backend/artifacts';
|
|
12
|
+
|
|
13
|
+
export interface DetectedSkillGroup {
|
|
14
|
+
engine: ArtifactEngine;
|
|
15
|
+
detected: DetectedArtifact[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Detect skills across all engines for the given scope. */
|
|
19
|
+
export async function detectSkills(projectPath?: string): Promise<DetectedSkillGroup[]> {
|
|
20
|
+
const scope = projectPath ? 'project' : 'global';
|
|
21
|
+
const groups = await Promise.all(
|
|
22
|
+
ARTIFACT_ENGINES.map(async engine => ({
|
|
23
|
+
engine,
|
|
24
|
+
detected: await detectArtifacts('skill', { engine, scope, projectPath })
|
|
25
|
+
}))
|
|
26
|
+
);
|
|
27
|
+
return groups.filter(g => g.detected.length > 0);
|
|
28
|
+
}
|
package/backend/skills/index.ts
CHANGED
|
@@ -3,19 +3,37 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The single entry point the rest of the backend imports from. Hides the split
|
|
5
5
|
* between the DB metadata table, the on-disk canonical store, the marketplace
|
|
6
|
-
* providers, and the per-engine sync layer.
|
|
6
|
+
* providers, the slash-invocation expander, and the per-engine sync layer.
|
|
7
|
+
*
|
|
8
|
+
* Since migration 079 this module owns what used to be two features: a skill is
|
|
9
|
+
* invoked by the MODEL (`auto` trigger, advertised through its description) or
|
|
10
|
+
* by the USER (`slash` trigger, typed as `/<slug>` and expanded here), or both.
|
|
7
11
|
*
|
|
8
12
|
* - SERVICE (`./service`) — CRUD used by the WS layer (Settings → Skills).
|
|
13
|
+
* - INVOKE (`./invoke`) — expand `/<slug> args` into the engine prompt.
|
|
9
14
|
* - MARKETPLACE (`./marketplace`) — browse + install from external providers.
|
|
10
|
-
* -
|
|
15
|
+
* - DETECT (`./detect`) — on-disk skills, managed and adoptable.
|
|
16
|
+
* - SYNC (`./sync`) — materialize active skills for each engine,
|
|
11
17
|
* called at stream start by the adapters.
|
|
12
18
|
*/
|
|
13
19
|
|
|
14
20
|
export { skillService } from './service';
|
|
15
|
-
export type { SkillDTO, ParsedSkillPreview } from './service';
|
|
21
|
+
export type { SkillDTO, SkillInputFields, ParsedSkillPreview, AvailableSkill } from './service';
|
|
22
|
+
|
|
23
|
+
export { expandSlashInvocation, parseSlashInvocation } from './invoke';
|
|
24
|
+
export type { ExpandedInvocation, SlashInvocation, InvocationContext } from './invoke';
|
|
16
25
|
|
|
17
26
|
export { listMarketplaceSkills, fetchMarketplaceSkill } from './marketplace';
|
|
18
27
|
export type { MarketplaceSkill, MarketplacePage } from './marketplace';
|
|
19
28
|
|
|
20
|
-
export {
|
|
29
|
+
export { detectSkills } from './detect';
|
|
30
|
+
export type { DetectedSkillGroup } from './detect';
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
syncSkills,
|
|
34
|
+
syncSkillsAllEngines,
|
|
35
|
+
buildSkillsPromptContext,
|
|
36
|
+
stripLegacyCommandArtifacts,
|
|
37
|
+
skillFileReady
|
|
38
|
+
} from './sync';
|
|
21
39
|
export type { SkillEngine } from './sync';
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slash invocation is the seam where a user's `/review-pr 123` becomes the
|
|
3
|
+
* prompt an engine actually runs. Two properties have to hold without a database
|
|
4
|
+
* or an engine in the loop, and both are easy to break silently:
|
|
5
|
+
*
|
|
6
|
+
* 1. RECOGNITION — a message is only an invocation when it really is one. The
|
|
7
|
+
* pattern has to reject `/usr/bin/env`, a bare `/`, and a URL path, or an
|
|
8
|
+
* ordinary message gets swallowed and replaced by a skill body.
|
|
9
|
+
* 2. SUBSTITUTION — the `$ARGUMENTS` / `$1`…`$9` contract users know from
|
|
10
|
+
* `.claude/commands/*.md`. Getting `consumed` wrong is what decides whether
|
|
11
|
+
* typed arguments reach the model at all.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, expect, test } from 'bun:test';
|
|
15
|
+
import { parseSlashInvocation, substituteArgs } from './invoke';
|
|
16
|
+
import { parseTriggers, stringifyTriggers, parseUses } from '$backend/database/queries';
|
|
17
|
+
|
|
18
|
+
describe('parseSlashInvocation', () => {
|
|
19
|
+
test('recognises a bare command', () => {
|
|
20
|
+
expect(parseSlashInvocation('/review-pr')).toEqual({ slug: 'review-pr', args: '' });
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('recognises a command with arguments', () => {
|
|
24
|
+
expect(parseSlashInvocation('/review-pr 123 --deep')).toEqual({ slug: 'review-pr', args: '123 --deep' });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('tolerates leading whitespace and a trailing newline', () => {
|
|
28
|
+
expect(parseSlashInvocation(' /deploy staging\n')).toEqual({ slug: 'deploy', args: 'staging' });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('keeps multi-line arguments intact', () => {
|
|
32
|
+
const parsed = parseSlashInvocation('/summarise first line\nsecond line');
|
|
33
|
+
expect(parsed?.slug).toBe('summarise');
|
|
34
|
+
expect(parsed?.args).toBe('first line\nsecond line');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('is not fooled by a path', () => {
|
|
38
|
+
expect(parseSlashInvocation('/usr/bin/env python')).toBeNull();
|
|
39
|
+
expect(parseSlashInvocation('/')).toBeNull();
|
|
40
|
+
expect(parseSlashInvocation('/Users/me/notes.md')).toBeNull();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('ignores a slash that is not at the start of the message', () => {
|
|
44
|
+
expect(parseSlashInvocation('run /review-pr for me')).toBeNull();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('rejects slugs outside the skill slug grammar', () => {
|
|
48
|
+
expect(parseSlashInvocation('/Review-PR')).toBeNull();
|
|
49
|
+
expect(parseSlashInvocation('/review_pr')).toBeNull();
|
|
50
|
+
expect(parseSlashInvocation('/-leading')).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('substituteArgs', () => {
|
|
55
|
+
test('replaces $ARGUMENTS with the whole argument string', () => {
|
|
56
|
+
const result = substituteArgs('Review PR $ARGUMENTS now.', '123 --deep');
|
|
57
|
+
expect(result.text).toBe('Review PR 123 --deep now.');
|
|
58
|
+
expect(result.consumed).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('replaces positional arguments', () => {
|
|
62
|
+
const result = substituteArgs('Compare $1 against $2.', 'main feature-x');
|
|
63
|
+
expect(result.text).toBe('Compare main against feature-x.');
|
|
64
|
+
expect(result.consumed).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('renders a missing positional as empty rather than leaving the placeholder', () => {
|
|
68
|
+
const result = substituteArgs('Deploy $1 to $2.', 'app');
|
|
69
|
+
expect(result.text).toBe('Deploy app to .');
|
|
70
|
+
expect(result.consumed).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('reports nothing consumed when the body has no placeholder', () => {
|
|
74
|
+
// The caller relies on this to append the arguments explicitly — otherwise
|
|
75
|
+
// what the user typed after the command would vanish.
|
|
76
|
+
const result = substituteArgs('Run the standard review.', '123');
|
|
77
|
+
expect(result.text).toBe('Run the standard review.');
|
|
78
|
+
expect(result.consumed).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('leaves a bare dollar amount alone', () => {
|
|
82
|
+
const result = substituteArgs('It costs $10 and $ARGUMENTSX stays.', 'x');
|
|
83
|
+
expect(result.text).toBe('It costs $10 and $ARGUMENTSX stays.');
|
|
84
|
+
expect(result.consumed).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('trigger and uses parsing', () => {
|
|
89
|
+
test('an unset or unrecognised trigger list falls back to auto', () => {
|
|
90
|
+
// A skill with no invocable trigger would be dead weight nobody can reach,
|
|
91
|
+
// so the parser refuses to produce that state.
|
|
92
|
+
expect(parseTriggers(null)).toEqual(['auto']);
|
|
93
|
+
expect(parseTriggers('')).toEqual(['auto']);
|
|
94
|
+
expect(parseTriggers('nonsense')).toEqual(['auto']);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('parses and de-duplicates a trigger list', () => {
|
|
98
|
+
expect(parseTriggers('slash')).toEqual(['slash']);
|
|
99
|
+
expect(parseTriggers(' auto , slash , auto ')).toEqual(['auto', 'slash']);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('serializes triggers in canonical order', () => {
|
|
103
|
+
expect(stringifyTriggers(['slash', 'auto'])).toBe('auto,slash');
|
|
104
|
+
expect(stringifyTriggers([])).toBe('auto');
|
|
105
|
+
expect(stringifyTriggers(undefined)).toBe('auto');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('tolerates a corrupt uses column', () => {
|
|
109
|
+
expect(parseUses('["a","b","a"]')).toEqual(['a', 'b']);
|
|
110
|
+
expect(parseUses('not json')).toEqual([]);
|
|
111
|
+
expect(parseUses(null)).toEqual([]);
|
|
112
|
+
expect(parseUses('{"a":1}')).toEqual([]);
|
|
113
|
+
});
|
|
114
|
+
});
|