@vibechain/os 0.5.2 → 0.6.0

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/links.js CHANGED
@@ -6,19 +6,24 @@
6
6
  * vmos://pack/<slug>
7
7
  * vmos://card/<slug>/<cardId>
8
8
  * https://vibechain.com/os/pack/<slug> (and https://www.vibechain.com/...; one optional trailing "/")
9
+ * vmos://app/<appId> (0.6: an app's page in MiniVibe, to install or play it)
10
+ * https://vibechain.com/os/app/<appId> (0.6; www. and one trailing "/" as above)
9
11
  *
10
12
  * Exact and anchored: lowercase scheme and host, at most 200 characters, no percent-encoding, query, fragment, user, port,
11
13
  * other host or other path. `<slug>` is a pack slug (at most 56 characters), `<cardId>` a creator card id.
12
14
  */
13
- import { CARD_ID, OsError } from "./app-wire.js";
15
+ import { CARD_ID, OsError, appId } from "./app-wire.js";
14
16
  import { packSlug } from "./manifest.js";
15
17
  export const VMOS_LINK_MAX = 200;
16
18
  const SLUG = "[a-z0-9]+(?:-[a-z0-9]+)*", CARD = "[A-Za-z0-9_-]{1,64}";
17
19
  const PACK_LINK = new RegExp(`^vmos://pack/(${SLUG})$`);
18
20
  const CARD_LINK = new RegExp(`^vmos://card/(${SLUG})/(${CARD})$`);
19
21
  const WEB_PACK = new RegExp(`^https://(?:www\\.)?vibechain\\.com/os/pack/(${SLUG})/?$`);
20
- /** The three accepted forms, exactly as parseVmosLink matches them (the agent skill prints these). */
21
- export const VMOS_LINK_PATTERNS = Object.freeze({ pack: PACK_LINK, card: CARD_LINK, web: WEB_PACK });
22
+ const APP = "[a-z][a-z0-9]*(?:-[a-z0-9]+)*";
23
+ const APP_LINK = new RegExp(`^vmos://app/(${APP})$`);
24
+ const WEB_APP = new RegExp(`^https://(?:www\\.)?vibechain\\.com/os/app/(${APP})/?$`);
25
+ /** The accepted forms, exactly as parseVmosLink matches them (the agent skill prints these). */
26
+ export const VMOS_LINK_PATTERNS = Object.freeze({ pack: PACK_LINK, card: CARD_LINK, web: WEB_PACK, app: APP_LINK, webApp: WEB_APP });
22
27
  /** The target a link names, or null when it is not exactly one of the three forms. */
23
28
  export function parseVmosLink(text) {
24
29
  if (typeof text !== "string" || text.length > VMOS_LINK_MAX)
@@ -26,9 +31,18 @@ export function parseVmosLink(text) {
26
31
  const card = CARD_LINK.exec(text);
27
32
  if (card)
28
33
  return packSlug(card[1]) ? { kind: "card", pack: card[1], card: card[2] } : null;
34
+ const app = APP_LINK.exec(text) ?? WEB_APP.exec(text);
35
+ if (app)
36
+ return appId(app[1]) ? { kind: "app", app: app[1] } : null;
29
37
  const pack = PACK_LINK.exec(text) ?? WEB_PACK.exec(text);
30
38
  return pack && packSlug(pack[1]) ? { kind: "pack", pack: pack[1] } : null;
31
39
  }
40
+ /** `vmos://app/<appId>` (0.6); throws an OsError `invalid` for a value that is not an app id. */
41
+ export function appLink(id) {
42
+ if (!appId(id))
43
+ throw new OsError("invalid", "That is not an app id.");
44
+ return `vmos://app/${id}`;
45
+ }
32
46
  /** `vmos://pack/<slug>`; throws an OsError `invalid` for a value that is not a pack slug. */
33
47
  export function packLink(slug) {
34
48
  if (!packSlug(slug))
package/dist/listing.d.ts CHANGED
@@ -25,6 +25,18 @@ export type ListingResult = {
25
25
  * Nothing else in the file is read or trusted; this is not a decoder.
26
26
  */
27
27
  export declare function pngSize(bytes: Uint8Array): ImageSize | null;
28
+ /**
29
+ * Width and height of a WebP from its first chunk (`VP8X`, `VP8 ` or `VP8L`), or null. Like `pngSize`, a header reader, not
30
+ * a decoder: only the RIFF/WEBP signature, the first chunk's tag and its size fields are read.
31
+ */
32
+ export declare function webpSize(bytes: Uint8Array): ImageSize | null;
33
+ /** The size of a PNG or WebP by its contents, whatever its name, or null. */
34
+ export declare const imageSize: (bytes: Uint8Array) => ImageSize | null;
35
+ /**
36
+ * Why an achievement's icon (VM_OS_APPS_4.md section 2) is not acceptable, or null: it must be in the bundle, a PNG or WebP
37
+ * whose contents match its extension, exactly 128 x 128 pixels and at most 64 KiB. `os check` and `checkListing` both say this.
38
+ */
39
+ export declare function achievementIconProblem(icon: string, bytes: Uint8Array | undefined): string | null;
28
40
  /** The image format named by a file's magic bytes, or null. */
29
41
  export declare function imageKind(bytes: Uint8Array): ImageKind | null;
30
42
  /**
package/dist/listing.js CHANGED
@@ -1,4 +1,4 @@
1
- import { APP_STORE_LIMITS } from "./manifest.js";
1
+ import { APP_SERVICE_LIMITS, APP_STORE_LIMITS } from "./manifest.js";
2
2
  const PNG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
3
3
  const IHDR = [0x49, 0x48, 0x44, 0x52];
4
4
  const startsWith = (bytes, prefix, offset = 0) => bytes.length >= offset + prefix.length && prefix.every((value, i) => bytes[offset + i] === value);
@@ -14,6 +14,45 @@ export function pngSize(bytes) {
14
14
  const width = uint32(bytes, 16), height = uint32(bytes, 20);
15
15
  return width > 0 && height > 0 && width <= 0x7fffffff && height <= 0x7fffffff ? { width, height } : null;
16
16
  }
17
+ const uint24le = (bytes, offset) => bytes[offset] + (bytes[offset + 1] << 8) + (bytes[offset + 2] << 16);
18
+ const uint14le = (bytes, offset) => (bytes[offset] + (bytes[offset + 1] << 8)) & 0x3fff;
19
+ /**
20
+ * Width and height of a WebP from its first chunk (`VP8X`, `VP8 ` or `VP8L`), or null. Like `pngSize`, a header reader, not
21
+ * a decoder: only the RIFF/WEBP signature, the first chunk's tag and its size fields are read.
22
+ */
23
+ export function webpSize(bytes) {
24
+ if (bytes.length < 30 || imageKind(bytes) !== "webp")
25
+ return null;
26
+ const tag = String.fromCharCode(bytes[12], bytes[13], bytes[14], bytes[15]);
27
+ if (tag === "VP8X")
28
+ return { width: uint24le(bytes, 24) + 1, height: uint24le(bytes, 27) + 1 };
29
+ if (tag === "VP8 " && bytes[23] === 0x9d && bytes[24] === 0x01 && bytes[25] === 0x2a)
30
+ return { width: uint14le(bytes, 26), height: uint14le(bytes, 28) };
31
+ if (tag === "VP8L" && bytes[20] === 0x2f) {
32
+ const bits = bytes[21] | (bytes[22] << 8) | (bytes[23] << 16) | (bytes[24] << 24);
33
+ return { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };
34
+ }
35
+ return null;
36
+ }
37
+ /** The size of a PNG or WebP by its contents, whatever its name, or null. */
38
+ export const imageSize = (bytes) => imageKind(bytes) === "webp" ? webpSize(bytes) : pngSize(bytes);
39
+ /**
40
+ * Why an achievement's icon (VM_OS_APPS_4.md section 2) is not acceptable, or null: it must be in the bundle, a PNG or WebP
41
+ * whose contents match its extension, exactly 128 x 128 pixels and at most 64 KiB. `os check` and `checkListing` both say this.
42
+ */
43
+ export function achievementIconProblem(icon, bytes) {
44
+ const limits = APP_SERVICE_LIMITS, expected = icon.endsWith(".webp") ? "webp" : "png";
45
+ if (!bytes)
46
+ return `${icon} is not in the built folder; copy it in.`;
47
+ if (imageKind(bytes) !== expected)
48
+ return `${icon} is not a ${expected === "png" ? "PNG" : "WebP"} image; its contents must match its extension.`;
49
+ if (bytes.byteLength > limits.achievementIconBytes)
50
+ return `${icon} is ${Math.ceil(bytes.byteLength / KiB)} KiB; an achievement icon may be at most ${limits.achievementIconBytes / KiB} KiB.`;
51
+ const size = imageSize(bytes);
52
+ if (!size || size.width !== limits.achievementIconSize || size.height !== limits.achievementIconSize)
53
+ return `${icon} is ${size ? `${size.width} x ${size.height}` : "not readable"}; it must be exactly ${limits.achievementIconSize} x ${limits.achievementIconSize} pixels.`;
54
+ return null;
55
+ }
17
56
  /** The image format named by a file's magic bytes, or null. */
18
57
  export function imageKind(bytes) {
19
58
  if (startsWith(bytes, PNG))
@@ -52,5 +91,14 @@ export function checkListing(manifest, files) {
52
91
  if (!expected || imageKind(bytes) !== expected)
53
92
  return fail("bad-screenshot", `The screenshot ${shot} is not a ${expected ?? "supported"} image, or its contents do not match its extension.`, shot);
54
93
  }
94
+ // SDK 0.6: an achievement's icon is a listing image too (a schema version 3 manifest only)
95
+ if (manifest.schemaVersion === 3)
96
+ for (const achievement of manifest.achievements ?? []) {
97
+ if (achievement.icon === null)
98
+ continue;
99
+ const problem = achievementIconProblem(achievement.icon, file(achievement.icon));
100
+ if (problem)
101
+ return fail("bad-icon", `The icon of the achievement ${achievement.id}: ${problem}`, achievement.icon);
102
+ }
55
103
  return { ok: true };
56
104
  }
@@ -85,6 +85,62 @@ export interface AppManifestV2 {
85
85
  /** What a schema version 3 app may read of the player's vmOS cards: nothing, its linked packs, or every pack. */
86
86
  export declare const APP_CARDS_ACCESS: readonly ["none", "linked", "all"];
87
87
  export type AppCardsAccess = typeof APP_CARDS_ACCESS[number];
88
+ /**
89
+ * The id of a leaderboard, achievement or product (VM_OS_APPS_4.md section 2): a lowercase letter, then up to 31 lowercase
90
+ * letters, digits and hyphens. Unique within its list; once in an approved version it keeps its meaning.
91
+ */
92
+ export declare const SERVICE_ID: RegExp;
93
+ export declare const serviceId: (value: unknown) => value is string;
94
+ /**
95
+ * Bounds of the optional `leaderboards`, `achievements` and `products` (SDK 0.6). Names and descriptions count Unicode code
96
+ * points and are one line. `priceMax` is only the SDK's structural bound on a product's whole Printer Cash price: the
97
+ * store's current maximum is lower, is set by VibeMarket and is checked when a version is submitted.
98
+ */
99
+ export declare const APP_SERVICE_LIMITS: {
100
+ readonly leaderboards: 5;
101
+ readonly achievements: 50;
102
+ readonly products: 50;
103
+ readonly nameMax: 40;
104
+ readonly descriptionMax: 120;
105
+ readonly achievementIconSize: 128;
106
+ readonly achievementIconBytes: number;
107
+ readonly priceMax: 1000000;
108
+ };
109
+ export declare const LEADERBOARD_ORDERS: readonly ["high", "low"];
110
+ export declare const LEADERBOARD_FORMATS: readonly ["number", "time"];
111
+ export declare const PRODUCT_KINDS: readonly ["consumable", "permanent"];
112
+ export type LeaderboardOrder = typeof LEADERBOARD_ORDERS[number];
113
+ export type LeaderboardFormat = typeof LEADERBOARD_FORMATS[number];
114
+ export type ProductKind = typeof PRODUCT_KINDS[number];
115
+ /** A declared leaderboard, normalized: `order` defaults to `"high"` (bigger is better), `format` to `"number"` (`"time"` is milliseconds). */
116
+ export interface AppLeaderboard {
117
+ id: string;
118
+ name: string;
119
+ order: LeaderboardOrder;
120
+ format: LeaderboardFormat;
121
+ }
122
+ /** A declared achievement, normalized: `icon` is a bundle path to a 128 x 128 PNG or WebP or null; `secret` defaults to false. */
123
+ export interface AppAchievement {
124
+ id: string;
125
+ name: string;
126
+ description: string;
127
+ icon: string | null;
128
+ secret: boolean;
129
+ }
130
+ /** A declared product: a whole Printer Cash price, bought with the player's confirmation and burned (never paid to anyone). */
131
+ export interface AppProduct {
132
+ id: string;
133
+ name: string;
134
+ description: string;
135
+ price: number;
136
+ kind: ProductKind;
137
+ }
138
+ /** The board every app has unless it declares one with this id: `finish({ score })` posts to it. */
139
+ export declare const DEFAULT_LEADERBOARD: Readonly<AppLeaderboard>;
140
+ /** The boards an app has: those it declares, in order, then the default `score` board unless one of them is `score`. */
141
+ export declare function appLeaderboards(manifest: {
142
+ leaderboards?: readonly AppLeaderboard[];
143
+ }): AppLeaderboard[];
88
144
  /** Schema version 3: a VibeStore app that may link packs, read cards and connect to declared servers. */
89
145
  export interface AppManifestV3 {
90
146
  schemaVersion: 3;
@@ -101,6 +157,12 @@ export interface AppManifestV3 {
101
157
  cards: AppCardsAccess;
102
158
  /** 0 to 8 `https://` or `wss://` origins, lowercase and sorted. Optional in the file; `[]` when absent. */
103
159
  network: string[];
160
+ /** SDK 0.6, optional: 0 to 5 boards. Present only when the file has the field, so older manifests normalize as before. */
161
+ leaderboards?: AppLeaderboard[];
162
+ /** SDK 0.6, optional: 0 to 50 achievements. Present only when the file has the field. */
163
+ achievements?: AppAchievement[];
164
+ /** SDK 0.6, optional: 0 to 50 products. Present only when the file has the field. */
165
+ products?: AppProduct[];
104
166
  }
105
167
  /** Every manifest `parseManifest` admits, discriminated on `schemaVersion`. */
106
168
  export type GameManifest = GameManifestV1 | AppManifestV2 | AppManifestV3;
@@ -122,11 +184,15 @@ export declare const APP_BUNDLE_LIMITS: {
122
184
  readonly file: number;
123
185
  readonly files: 2000;
124
186
  };
125
- /** Schema version 3 bounds: linked packs, declared origins and a pack slug's length. */
187
+ /**
188
+ * Schema version 3 bounds: linked packs, declared origins, a pack slug's length and the size of `vibemarket.json` itself
189
+ * (64 KiB since SDK 0.6, so a manifest that fills every list of APP_SERVICE_LIMITS still fits; 16 KiB before).
190
+ */
126
191
  export declare const APP_MANIFEST_LIMITS: {
127
192
  readonly packs: 3;
128
193
  readonly network: 8;
129
194
  readonly packSlugMax: 56;
195
+ readonly manifestBytes: number;
130
196
  };
131
197
  /** A pack slug, as `vibemarket/steam/pack-slug.ts` defines it (at most `APP_MANIFEST_LIMITS.packSlugMax` characters). */
132
198
  export declare const PACK_SLUG: RegExp;
@@ -161,6 +227,15 @@ export interface ManifestProblem {
161
227
  }
162
228
  /** `path: message`, the line `os check` prints and the text `parseManifest` throws for schema version 3. */
163
229
  export declare const manifestProblemText: (problem: ManifestProblem) => string;
230
+ /**
231
+ * The `leaderboards`, `achievements` and `products` of a manifest-shaped object, normalized by the manifest's own rules (for
232
+ * the testing fakes and the preview); throws the first problem as `parseManifest` would.
233
+ */
234
+ export declare function parseServices(input: {
235
+ leaderboards?: unknown;
236
+ achievements?: unknown;
237
+ products?: unknown;
238
+ }): Pick<AppManifestV3, "leaderboards" | "achievements" | "products">;
164
239
  /**
165
240
  * Things `parseManifest` accepts but a developer should change, as `os check` prints them without failing: today only a
166
241
  * schema version 2 `store.category` of "tool" or "toy", which is read as "app".
package/dist/manifest.js CHANGED
@@ -23,12 +23,40 @@ export const APP_STORE_LIMITS = {
23
23
  };
24
24
  /** What a schema version 3 app may read of the player's vmOS cards: nothing, its linked packs, or every pack. */
25
25
  export const APP_CARDS_ACCESS = ["none", "linked", "all"];
26
+ /**
27
+ * The id of a leaderboard, achievement or product (VM_OS_APPS_4.md section 2): a lowercase letter, then up to 31 lowercase
28
+ * letters, digits and hyphens. Unique within its list; once in an approved version it keeps its meaning.
29
+ */
30
+ export const SERVICE_ID = /^[a-z][a-z0-9-]{0,31}$/;
31
+ export const serviceId = (value) => typeof value === "string" && SERVICE_ID.test(value);
32
+ /**
33
+ * Bounds of the optional `leaderboards`, `achievements` and `products` (SDK 0.6). Names and descriptions count Unicode code
34
+ * points and are one line. `priceMax` is only the SDK's structural bound on a product's whole Printer Cash price: the
35
+ * store's current maximum is lower, is set by VibeMarket and is checked when a version is submitted.
36
+ */
37
+ export const APP_SERVICE_LIMITS = {
38
+ leaderboards: 5, achievements: 50, products: 50, nameMax: 40, descriptionMax: 120,
39
+ achievementIconSize: 128, achievementIconBytes: 64 * 1024, priceMax: 1_000_000,
40
+ };
41
+ export const LEADERBOARD_ORDERS = ["high", "low"];
42
+ export const LEADERBOARD_FORMATS = ["number", "time"];
43
+ export const PRODUCT_KINDS = ["consumable", "permanent"];
44
+ /** The board every app has unless it declares one with this id: `finish({ score })` posts to it. */
45
+ export const DEFAULT_LEADERBOARD = Object.freeze({ id: "score", name: "High scores", order: "high", format: "number" });
46
+ /** The boards an app has: those it declares, in order, then the default `score` board unless one of them is `score`. */
47
+ export function appLeaderboards(manifest) {
48
+ const declared = (manifest.leaderboards ?? []).map(board => ({ ...board }));
49
+ return declared.some(board => board.id === DEFAULT_LEADERBOARD.id) ? declared : [...declared, { ...DEFAULT_LEADERBOARD }];
50
+ }
26
51
  /** The legacy Market-game bounds. VibeStore bundles use `APP_BUNDLE_LIMITS`. */
27
52
  export const GAME_LIMITS = { archive: 20 * 1024 * 1024, expanded: 64 * 1024 * 1024, file: 16 * 1024 * 1024, files: 256, versions: 100, games: 40 };
28
53
  /** VibeStore bundle bounds: the submitted ZIP, its expanded bytes, the largest file and the entries (files and folders). */
29
54
  export const APP_BUNDLE_LIMITS = { archive: 50 * 1024 * 1024, expanded: 128 * 1024 * 1024, file: 32 * 1024 * 1024, files: 2000 };
30
- /** Schema version 3 bounds: linked packs, declared origins and a pack slug's length. */
31
- export const APP_MANIFEST_LIMITS = { packs: 3, network: 8, packSlugMax: 56 };
55
+ /**
56
+ * Schema version 3 bounds: linked packs, declared origins, a pack slug's length and the size of `vibemarket.json` itself
57
+ * (64 KiB since SDK 0.6, so a manifest that fills every list of APP_SERVICE_LIMITS still fits; 16 KiB before).
58
+ */
59
+ export const APP_MANIFEST_LIMITS = { packs: 3, network: 8, packSlugMax: 56, manifestBytes: 64 * 1024 };
32
60
  /** A pack slug, as `vibemarket/steam/pack-slug.ts` defines it (at most `APP_MANIFEST_LIMITS.packSlugMax` characters). */
33
61
  export const PACK_SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
34
62
  export const packSlug = (value) => typeof value === "string" && value.length <= APP_MANIFEST_LIMITS.packSlugMax && PACK_SLUG.test(value);
@@ -202,7 +230,7 @@ export function networkOrigin(value) { return networkOriginCheck(value).origin;
202
230
  /** `path: message`, the line `os check` prints and the text `parseManifest` throws for schema version 3. */
203
231
  export const manifestProblemText = (problem) => problem.path ? `${problem.path}: ${problem.message}` : problem.message;
204
232
  const V3_REQUIRED = ["schemaVersion", "id", "version", "name", "tagline", "entry", "window", "store"];
205
- const V3_OPTIONAL = ["packs", "cards", "network"];
233
+ const V3_OPTIONAL = ["packs", "cards", "network", "leaderboards", "achievements", "products"];
206
234
  const V3_REMOVED = ["sdkVersion", "category", "permissions", "collectionId"];
207
235
  // origins are distinct by then, so two are never equal
208
236
  const compareText = (a, b) => a < b ? -1 : 1;
@@ -277,10 +305,147 @@ function inspectV3(data) {
277
305
  cards = null;
278
306
  }
279
307
  const network = inspectNetwork(data.network, add);
280
- if (problems.length || id === null || version === null || name === null || tagline === null || entry === null || !window || !store || !packs || !cards || !network)
308
+ const services = inspectServices(data, add);
309
+ if (problems.length || id === null || version === null || name === null || tagline === null || entry === null || !window || !store || !packs || !cards || !network || !services)
281
310
  return { manifest: null, problems };
282
- return { manifest: { schemaVersion: 3, id, version, name, tagline, entry, window, store, packs, cards, network }, problems };
311
+ return { manifest: { schemaVersion: 3, id, version, name, tagline, entry, window, store, packs, cards, network, ...services }, problems };
312
+ }
313
+ // Every control character, newlines included: names and descriptions of boards, achievements and products are one line.
314
+ const LINE_CONTROL = /[\u0000-\u001F\u007F-\u009F]/;
315
+ /** One line of 1 to `max` code points with no surrounding spaces, or a problem at `path`. */
316
+ function line(value, path, max, add) {
317
+ if (typeof value !== "string")
318
+ add(path, "must be a string.");
319
+ else if (!value.trim())
320
+ add(path, "must not be empty.");
321
+ else if (value !== value.trim())
322
+ add(path, "must not start or end with spaces.");
323
+ else if (LINE_CONTROL.test(value))
324
+ add(path, "must be one line of plain text, with no line breaks or control characters.");
325
+ else if ([...value].length > max)
326
+ add(path, `must be at most ${max} characters.`);
327
+ else
328
+ return value;
329
+ return null;
330
+ }
331
+ /**
332
+ * The optional `leaderboards`, `achievements` and `products` (VM_OS_APPS_4.md section 2): each a list of objects with exact
333
+ * fields, at most the APP_SERVICE_LIMITS count, ids unique within the list. Only the lists the file has are returned, so a
334
+ * manifest without them normalizes exactly as before SDK 0.6. `null` when any of them has a problem.
335
+ */
336
+ function inspectServices(data, add) {
337
+ const limits = APP_SERVICE_LIMITS;
338
+ let ok = true;
339
+ const list = (key, max, noun, required, optional, item) => {
340
+ if (!(key in data))
341
+ return undefined;
342
+ const input = data[key];
343
+ if (!Array.isArray(input)) {
344
+ add(key, `must be a list of ${noun} objects.`);
345
+ ok = false;
346
+ return undefined;
347
+ }
348
+ if (input.length > max) {
349
+ add(key, `may declare at most ${max} ${noun}s.`);
350
+ ok = false;
351
+ }
352
+ const out = [], ids = new Set();
353
+ input.forEach((value, index) => {
354
+ const path = `${key}[${index}]`;
355
+ if (!record(value)) {
356
+ add(path, `must be an object with ${listOf(required)}.`);
357
+ ok = false;
358
+ return;
359
+ }
360
+ const before = fieldProblems(value, `${path}.`, required, optional);
361
+ for (const problem of before)
362
+ add(problem.path, problem.message);
363
+ let good = !before.length;
364
+ if ("id" in value) {
365
+ if (!serviceId(value.id)) {
366
+ add(`${path}.id`, "must be a lowercase letter followed by up to 31 lowercase letters, digits and hyphens, such as high-score.");
367
+ good = false;
368
+ }
369
+ else if (ids.has(value.id)) {
370
+ add(`${path}.id`, `repeats another ${noun}'s id; each id is used once.`);
371
+ good = false;
372
+ }
373
+ else
374
+ ids.add(value.id);
375
+ }
376
+ const made = item(value, path);
377
+ if (good && made !== null)
378
+ out.push(made);
379
+ else
380
+ ok = false;
381
+ });
382
+ return out;
383
+ };
384
+ const choice = (value, key, path, values, fallback) => {
385
+ if (!(key in value))
386
+ return fallback;
387
+ if (typeof value[key] === "string" && values.includes(value[key]))
388
+ return value[key];
389
+ add(`${path}.${key}`, `must be ${listOf(values.map(item => `"${item}"`), "or")}.`);
390
+ return null;
391
+ };
392
+ const leaderboards = list("leaderboards", limits.leaderboards, "leaderboard", ["id", "name"], ["order", "format"], (value, path) => {
393
+ const name = "name" in value ? line(value.name, `${path}.name`, limits.nameMax, add) : null;
394
+ const order = choice(value, "order", path, LEADERBOARD_ORDERS, "high"), format = choice(value, "format", path, LEADERBOARD_FORMATS, "number");
395
+ return name === null || order === null || format === null ? null : { id: value.id, name, order, format };
396
+ });
397
+ const achievements = list("achievements", limits.achievements, "achievement", ["id", "name", "description"], ["icon", "secret"], (value, path) => {
398
+ const name = "name" in value ? line(value.name, `${path}.name`, limits.nameMax, add) : null;
399
+ const description = "description" in value ? line(value.description, `${path}.description`, limits.descriptionMax, add) : null;
400
+ let icon = null, fine = true;
401
+ if ("icon" in value) {
402
+ const file = value.icon;
403
+ if (typeof file !== "string" || !assetPath(file) || !/\.(?:png|webp)$/.test(file)) {
404
+ add(`${path}.icon`, "must be the relative path of a .png or .webp file in the bundle, such as achievements/first.png.");
405
+ fine = false;
406
+ }
407
+ else
408
+ icon = file;
409
+ }
410
+ if ("secret" in value && typeof value.secret !== "boolean") {
411
+ add(`${path}.secret`, "must be true or false.");
412
+ fine = false;
413
+ }
414
+ return name === null || description === null || !fine ? null : { id: value.id, name, description, icon, secret: value.secret === true };
415
+ });
416
+ const products = list("products", limits.products, "product", ["id", "name", "description", "price", "kind"], [], (value, path) => {
417
+ const name = "name" in value ? line(value.name, `${path}.name`, limits.nameMax, add) : null;
418
+ const description = "description" in value ? line(value.description, `${path}.description`, limits.descriptionMax, add) : null;
419
+ const { price } = value;
420
+ let fine = true;
421
+ if ("price" in value && (typeof price !== "number" || !Number.isInteger(price) || price < 1 || price > limits.priceMax)) {
422
+ add(`${path}.price`, "must be a whole number of Printer Cash, at least 1, within the store's maximum price.");
423
+ fine = false;
424
+ }
425
+ const kind = "kind" in value ? choice(value, "kind", path, PRODUCT_KINDS, "consumable") : null;
426
+ return name === null || description === null || kind === null || !fine ? null : { id: value.id, name, description, price: price, kind };
427
+ });
428
+ if (!ok)
429
+ return null;
430
+ return { ...(leaderboards ? { leaderboards } : {}), ...(achievements ? { achievements } : {}), ...(products ? { products } : {}) };
431
+ }
432
+ /**
433
+ * The `leaderboards`, `achievements` and `products` of a manifest-shaped object, normalized by the manifest's own rules (for
434
+ * the testing fakes and the preview); throws the first problem as `parseManifest` would.
435
+ */
436
+ export function parseServices(input) {
437
+ const problems = [];
438
+ const data = {};
439
+ for (const key of ["leaderboards", "achievements", "products"])
440
+ if (input[key] !== undefined)
441
+ data[key] = input[key];
442
+ const services = inspectServices(data, (path, message) => { problems.push({ path, message }); });
443
+ if (!services)
444
+ throw Error(manifestProblemText(problems[0]));
445
+ return services;
283
446
  }
447
+ /** "a, b and c" (or "or") of two or more values, with backticks left to the caller. */
448
+ const listOf = (values, last = "and") => `${values.slice(0, -1).join(", ")} ${last} ${values.at(-1)}`;
284
449
  function inspectWindow(input, add) {
285
450
  if (!record(input)) {
286
451
  add("window", "must be an object with width, height and resizable.");
package/dist/os.d.ts CHANGED
@@ -4,12 +4,36 @@
4
4
  * window-only checkpoint) and the rest reject with `unsupported`. Every rejection is an OsError with a stable code.
5
5
  */
6
6
  import { type AppSound } from "./app-protocol.ts";
7
- import { type Card, type OsErrorCode, type Pack, type PackSummary, type Player } from "./app-wire.ts";
7
+ import { type Achievement, type BoardPeriod, type BoardScope, type Card, type Friend, type Invite, type Leaderboard, type OsErrorCode, type Pack, type PackSummary, type Player, type Product, type Receipt } from "./app-wire.ts";
8
8
  import { type CaptureResult } from "./capture.ts";
9
- export type { Card, OsErrorCode, Pack, PackCard, PackSummary, Player, Rarity, SignedInPlayer } from "./app-wire.ts";
9
+ export type { Achievement, BoardPeriod, BoardScope, Card, Friend, Invite, Leaderboard, LeaderboardEntry, OsErrorCode, Pack, PackCard, PackSummary, Player, Presence, Product, Receipt, Rarity, SignedInPlayer, } from "./app-wire.ts";
10
+ export type { LeaderboardFormat, LeaderboardOrder, ProductKind } from "./manifest.ts";
10
11
  export { OS_ERROR_CODES } from "./app-wire.ts";
11
- /** The SDK version a 0.5 client reports in `host.hello`. */
12
- export declare const OS_SDK_VERSION = "0.5.2";
12
+ /** The SDK version a client reports in `host.hello`. */
13
+ export declare const OS_SDK_VERSION = "0.6.0";
14
+ /** `os.leaderboard()` options; each falls back to the board `score`, everyone, all time and 10 rows (1 to 50). */
15
+ export interface LeaderboardOptions {
16
+ board?: string;
17
+ scope?: BoardScope;
18
+ period?: BoardPeriod;
19
+ limit?: number;
20
+ }
21
+ /** `os.invite()` options: a friend's handle to preselect, a line of at most 80 characters, and up to 256 characters of data for your app. */
22
+ export interface InviteOptions {
23
+ to?: string;
24
+ text?: string;
25
+ data?: string;
26
+ }
27
+ /** `os.share()` options: a caption of at most 140 characters, and a score with its board (default `score`). */
28
+ export interface ShareOptions {
29
+ text?: string;
30
+ score?: number;
31
+ board?: string;
32
+ }
33
+ /** What the app was opened with: a friend's invite, or null. */
34
+ export interface OsLaunch {
35
+ readonly invite: Readonly<Invite> | null;
36
+ }
13
37
  export { OsError } from "./app-wire.ts";
14
38
  export type OsSize = Readonly<{
15
39
  width: number;
@@ -48,13 +72,49 @@ export interface Os {
48
72
  showPack(id: string): Promise<void>;
49
73
  /** the host opens that card in its pack's page; needs a click in the app (0.5.1 hosts) */
50
74
  showCard(pack: string, cardId: string): Promise<void>;
51
- /** open an app link (`vmos://pack/<slug>`, `vmos://card/<slug>/<cardId>`, `https://vibechain.com/os/pack/<slug>`); needs a click */
75
+ /** open an app link (`vmos://pack/<slug>`, `vmos://card/<slug>/<cardId>`, `vmos://app/<appId>` and the `https://vibechain.com/os/...` forms); needs a click */
52
76
  open(link: string): Promise<void>;
53
77
  /** who is playing: handle, name and picture, or `{ guest: true }`; plus a 5-minute token for your own server with `network` */
54
78
  player(): Promise<Player>;
79
+ /** end the round: one integer score, 0 to 1,000,000,000, once per open window; since 0.6 posted to `board` (default `score`) */
55
80
  finish(result: {
56
81
  score: number;
82
+ board?: string;
57
83
  }): Promise<void>;
84
+ /** a board's top rows and the player's own place (0.6) */
85
+ leaderboard(options?: LeaderboardOptions): Promise<Leaderboard>;
86
+ /** vmOS opens its leaderboard panel over the app; resolves when the player closes it; needs a click (0.6) */
87
+ showLeaderboard(board?: string): Promise<void>;
88
+ /** record an achievement the manifest declares; `unlocked` is true only the first time (0.6) */
89
+ unlock(id: string): Promise<{
90
+ unlocked: boolean;
91
+ }>;
92
+ /** the manifest's achievements and which the player has (0.6) */
93
+ achievements(): Promise<Achievement[]>;
94
+ /** the player's friends who have this app, with a coarse presence (0.6) */
95
+ friends(): Promise<Friend[]>;
96
+ /** vmOS opens its invite sheet; resolves with how many friends were invited; needs a click; not for guests (0.6) */
97
+ invite(options?: InviteOptions): Promise<{
98
+ sent: number;
99
+ }>;
100
+ /** what the app was opened with: `launch.invite` is a friend's invite or null; read it once at start (0.6) */
101
+ readonly launch: OsLaunch;
102
+ /** a friend's invite arrived while the app is open; returns unsubscribe (0.6) */
103
+ onInvite(listener: (invite: Invite) => void): () => void;
104
+ /** vmOS opens its share sheet with a card it composes; `shared` is false when the player closes it; needs a click; not for guests (0.6) */
105
+ share(options: ShareOptions): Promise<{
106
+ shared: boolean;
107
+ }>;
108
+ /** the manifest's products as approved, with `owned` for permanent ones the player bought (0.6) */
109
+ products(): Promise<Product[]>;
110
+ /** vmOS asks the player to spend Printer Cash on a product; the receipt on confirmation; needs a click in the last second; not for guests (0.6) */
111
+ buy(productId: string): Promise<Receipt>;
112
+ /** permanent receipts and consumables not yet consumed (0.6) */
113
+ purchases(): Promise<Receipt[]>;
114
+ /** after granting a consumable, so it leaves `purchases()` (0.6) */
115
+ consume(receiptId: string): Promise<void>;
116
+ /** a 5-minute signed token of a receipt for your server; `network` apps only (0.6) */
117
+ receiptToken(receiptId: string): Promise<string>;
58
118
  maximize(on?: boolean): Promise<void>;
59
119
  fullscreen(on?: boolean): Promise<void>;
60
120
  onPause(listener: () => void): () => void;