wildcrane-artifact-sdk 0.0.0-stage → 0.1.1

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.
Files changed (94) hide show
  1. package/README.md +107 -2
  2. package/bin/artifact-sdk.mjs +56 -0
  3. package/bin/pack.mjs +70 -0
  4. package/dist/builtin/compare.json +15 -0
  5. package/dist/builtin/html.json +6 -0
  6. package/dist/builtin/image.json +1 -0
  7. package/dist/builtin/index.d.ts +2 -0
  8. package/dist/builtin/index.js +13 -0
  9. package/dist/builtin/markdown.json +9 -0
  10. package/dist/builtin/spritesheet.json +23 -0
  11. package/dist/builtin/storyboard.json +33 -0
  12. package/dist/builtin/studio.json +488 -0
  13. package/dist/builtin/tiled.json +11 -0
  14. package/dist/builtin/video.json +1 -0
  15. package/dist/index.d.ts +45 -0
  16. package/dist/index.js +74 -0
  17. package/dist/internal/asset-formats.d.ts +16 -0
  18. package/dist/internal/asset-formats.js +61 -0
  19. package/dist/internal/asset-store.d.ts +109 -0
  20. package/dist/internal/asset-store.js +5 -0
  21. package/dist/internal/collect.d.ts +43 -0
  22. package/dist/internal/collect.js +154 -0
  23. package/dist/internal/document.d.ts +7 -0
  24. package/dist/internal/document.js +9 -0
  25. package/dist/internal/file-references.d.ts +6 -0
  26. package/dist/internal/file-references.js +29 -0
  27. package/dist/internal/format/captions.d.ts +6 -0
  28. package/dist/internal/format/captions.js +31 -0
  29. package/dist/internal/format/compare.d.ts +34 -0
  30. package/dist/internal/format/compare.js +59 -0
  31. package/dist/internal/format/spritesheet.d.ts +25 -0
  32. package/dist/internal/format/spritesheet.js +50 -0
  33. package/dist/internal/format/storyboard.d.ts +8 -0
  34. package/dist/internal/format/storyboard.js +21 -0
  35. package/dist/internal/format/studio.d.ts +68 -0
  36. package/dist/internal/format/studio.js +58 -0
  37. package/dist/internal/hyperframe-document.d.ts +14 -0
  38. package/dist/internal/hyperframe-document.js +80 -0
  39. package/dist/internal/manifest.d.ts +48 -0
  40. package/dist/internal/manifest.js +102 -0
  41. package/dist/internal/markdown-code.d.ts +3 -0
  42. package/dist/internal/markdown-code.js +51 -0
  43. package/dist/internal/markdown-destinations.d.ts +7 -0
  44. package/dist/internal/markdown-destinations.js +52 -0
  45. package/dist/internal/mockup-tag.d.ts +194 -0
  46. package/dist/internal/mockup-tag.js +8 -0
  47. package/dist/internal/previews.d.ts +163 -0
  48. package/dist/internal/previews.js +798 -0
  49. package/dist/internal/references.d.ts +65 -0
  50. package/dist/internal/references.js +209 -0
  51. package/dist/internal/schema.d.ts +2 -0
  52. package/dist/internal/schema.js +83 -0
  53. package/dist/internal/storyboard.d.ts +84 -0
  54. package/dist/internal/storyboard.js +71 -0
  55. package/dist/internal/tags.d.ts +37 -0
  56. package/dist/internal/tags.js +66 -0
  57. package/dist/internal/task-assets.d.ts +122 -0
  58. package/dist/internal/task-assets.js +87 -0
  59. package/dist/internal/web-references.d.ts +21 -0
  60. package/dist/internal/web-references.js +162 -0
  61. package/package.json +29 -4
  62. package/src/builtin/compare.json +15 -0
  63. package/src/builtin/html.json +6 -0
  64. package/src/builtin/image.json +1 -0
  65. package/src/builtin/index.ts +15 -0
  66. package/src/builtin/markdown.json +9 -0
  67. package/src/builtin/spritesheet.json +23 -0
  68. package/src/builtin/storyboard.json +33 -0
  69. package/src/builtin/studio.json +488 -0
  70. package/src/builtin/tiled.json +11 -0
  71. package/src/builtin/video.json +1 -0
  72. package/src/index.ts +116 -0
  73. package/src/internal/asset-formats.ts +66 -0
  74. package/src/internal/asset-store.ts +41 -0
  75. package/src/internal/collect.ts +158 -0
  76. package/src/internal/document.ts +12 -0
  77. package/src/internal/file-references.ts +34 -0
  78. package/src/internal/format/captions.ts +34 -0
  79. package/src/internal/format/compare.ts +63 -0
  80. package/src/internal/format/spritesheet.ts +53 -0
  81. package/src/internal/format/storyboard.ts +22 -0
  82. package/src/internal/format/studio.ts +69 -0
  83. package/src/internal/hyperframe-document.ts +75 -0
  84. package/src/internal/manifest.ts +124 -0
  85. package/src/internal/markdown-code.ts +48 -0
  86. package/src/internal/markdown-destinations.ts +45 -0
  87. package/src/internal/mockup-tag.ts +128 -0
  88. package/src/internal/previews.ts +766 -0
  89. package/src/internal/references.ts +195 -0
  90. package/src/internal/schema.ts +69 -0
  91. package/src/internal/storyboard.ts +127 -0
  92. package/src/internal/tags.ts +94 -0
  93. package/src/internal/task-assets.ts +114 -0
  94. package/src/internal/web-references.ts +160 -0
package/README.md CHANGED
@@ -1,3 +1,108 @@
1
- # Temporary Holding Version
1
+ # wildcrane-artifact-sdk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Write your own preview for your own artifact type — a subtitle editor, a slide viewer — and
4
+ publish it to your WildCrane account. When an agent submits an artifact of that type, the review
5
+ page draws it with your component.
6
+
7
+ ```
8
+ app.wildcrane.ai ──postMessage──▶ <hash>.wildcraneusercontent.com (your bundle)
9
+ holds the sign-in, sandboxed iframe, no network,
10
+ validates and saves sees only the artifact it is handed
11
+ ```
12
+
13
+ - **Yours alone**: a component draws artifacts only on tasks your account started.
14
+ - **No network**: `connect-src 'none'`, so not even `fetch` of your own files; bundle every script, style, font and image, and import data instead of fetching it.
15
+ - **Saving**: only the account's owners get `readOnly: false`. A save becomes the artifact's
16
+ next version. Approving stays on the review page.
17
+ - **Pinned**: an artifact version keeps the component version that was newest when it was
18
+ written; publishing later changes nothing already submitted.
19
+
20
+ ## A component
21
+
22
+ A folder of static files with `index.html` at its top, at most 10 MB, any framework, and
23
+ `wildcrane.component.json`:
24
+
25
+ ```json
26
+ {
27
+ "name": "subtitles",
28
+ "version": "1.0.0",
29
+ "title": { "en": "Subtitle editor", "zh": "字幕编辑器" },
30
+ "types": ["@your-handle/subtitles"],
31
+ "schema": { "type": "array" }
32
+ }
33
+ ```
34
+
35
+ - **`name`**: unique in your account; not a built-in type (`html`, `image`, `video`,
36
+ `markdown`, `storyboard`, `compare`).
37
+ - **`version`**: semver, published once.
38
+ - **`types`**: the artifact types it draws, each `@<your GitHub handle>/<name>`, lowercase.
39
+ - **`schema`**: optional JSON Schema every save must match. The review page checks `type`,
40
+ `enum`, `const`, `anyOf`, `properties`, `required`, `additionalProperties`, `items`, `minItems`,
41
+ `maxItems`, `minLength`, `maxLength`, `pattern`, `minimum` and `maximum`. A save is at most 256 KB.
42
+ - **`references`**: optional; where the content names other files of the task, so the page
43
+ hands them to you. `{ "json": "/path" }` is a field (`*` for every item), `{ "text": "regex" }`
44
+ the first group of each case-insensitive match in text content, split by `list` when set.
45
+
46
+ A subtitle track over its video, the video's artifact id in `video`:
47
+
48
+ ```json
49
+ {
50
+ "types": ["@your-handle/subtitles"],
51
+ "schema": { "type": "object", "required": ["video", "cues"] },
52
+ "references": [{ "json": "/video" }]
53
+ }
54
+ ```
55
+
56
+ ```js
57
+ const { data, contentType } = bridge.files[bridge.artifact.content.video];
58
+ video.src = URL.createObjectURL(new Blob([data], { type: contentType }));
59
+ ```
60
+
61
+ ```sh
62
+ npm install wildcrane-artifact-sdk
63
+ ```
64
+
65
+ ```js
66
+ import { connect } from "wildcrane-artifact-sdk";
67
+
68
+ const bridge = await connect();
69
+ // bridge.artifact: { type, name, version, content } or { …, file: { data: ArrayBuffer, contentType } }
70
+ // bridge.files: { [reference]: { data, contentType } }, the files `references` point at
71
+ // bridge.readOnly, bridge.locale ("zh" | "en"), bridge.theme ("light" | "dark")
72
+ await bridge.save(next); // rejects with the reason, in bridge.locale
73
+ bridge.onChange((ctx) => redraw(ctx));
74
+ ```
75
+
76
+ The frame follows your document's height (`connect({ autoResize: false })` and
77
+ `bridge.resize(px)` to do it yourself), up to twice the window's height.
78
+
79
+ ## Publish
80
+
81
+ ```sh
82
+ WILDCRANE_API_KEY=wck_… npx wildcrane-artifact-sdk publish dist
83
+ ```
84
+
85
+ Every file in the folder but dot files goes into the bundle. With Vite, put the manifest in
86
+ `public/` and publish `dist`. Then submit an artifact of your type:
87
+ `POST /v1/tasks/{id}/artifacts { "type": "@your-handle/subtitles" }`; it is reviewed with the
88
+ card's current phase.
89
+ `GET /v1/components` lists what you published; `DELETE
90
+ /v1/components/{name}/versions/{version}` removes a version, and artifacts pinned to it show that
91
+ their preview cannot be drawn.
92
+
93
+ ## The example
94
+
95
+ `examples/subtitles` edits a JSON list of `{ start, end, text }`. Set your handle in its
96
+ manifest, then `npm run example`.
97
+
98
+ ## Develop
99
+
100
+ `npm install`, then `npm run lint`.
101
+
102
+ ## 仓库内接入
103
+
104
+ - **组件清单**:内置组件的清单在 `src/builtin/*.json`,与第三方同一写法;SDK 只按清单的 `references` 与 `schema` 收集依赖、校验内容,不为内置类型单写规则。
105
+ - **依赖收集**:`src/internal/collect.ts` 从卡片正文出发走完可达图,按依赖顺序输出,文件里的问题记在该文件上;Runner 上传与 `akb raw validate` 共用它,文件读取与路径边界由调用方提供。
106
+ - **网页依赖**:页面引用的图片、字体各自上传一次,上传时引用改写为 artifact ID;审阅页把文件交给沙箱页面,由页面自建地址。
107
+ - **内置预览**:`src/internal/previews.ts` 通过 SDK `Context` 读取内容,按卡片清单固定版本并统一权限与错误;网页只提供鉴权服务和显示外框。
108
+ - **打包边界**:Runner 和网页直接打包内部源码;浏览器入口不依赖 Node,内部模块不属于公开 API,第三方继续使用 `connect()`。
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+ // npx wildcrane-artifact-sdk publish [dir]
3
+ //
4
+ // Reads `wildcrane.component.json` in dir, packs every other file into a gzipped tar and
5
+ // publishes it as that version, with WILDCRANE_API_KEY.
6
+
7
+ import { readFileSync } from 'node:fs'
8
+ import { join, resolve } from 'node:path'
9
+ import { gzipSync } from 'node:zlib'
10
+
11
+ import { MANIFEST, filesOf, tarOf } from './pack.mjs'
12
+
13
+ const API = (process.env.WILDCRANE_API_URL || 'https://api.ai4kanban.dev').replace(/\/+$/, '')
14
+
15
+ /** @param {string} path @param {{ method: string, headers: Record<string, string>, body: BodyInit }} init */
16
+ async function api(path, init) {
17
+ const r = await fetch(`${API}${path}`, {
18
+ ...init,
19
+ headers: { authorization: `Bearer ${process.env.WILDCRANE_API_KEY}`, ...init.headers },
20
+ })
21
+ const body = await r.json().catch(() => null)
22
+ if (!r.ok) throw new Error(body?.error?.message ?? `${r.status} ${r.statusText}`)
23
+ return body
24
+ }
25
+
26
+ /** @param {string} dir */
27
+ async function publish(dir) {
28
+ if (!process.env.WILDCRANE_API_KEY) throw new Error('Set WILDCRANE_API_KEY to an API key of your account.')
29
+ let manifest
30
+ try {
31
+ manifest = JSON.parse(readFileSync(join(dir, MANIFEST), 'utf8'))
32
+ } catch (e) {
33
+ throw new Error(`Cannot read ${join(dir, MANIFEST)}: ${e instanceof Error ? e.message : e}`)
34
+ }
35
+ const paths = filesOf(dir)
36
+ if (!paths.includes('index.html')) throw new Error(`${dir} has no index.html.`)
37
+ const bundle = gzipSync(tarOf(paths.map((p) => [p, readFileSync(join(dir, p))])))
38
+ const { name, version, ...rest } = manifest
39
+ const made = await api('/v1/components', {
40
+ method: 'POST',
41
+ headers: { 'content-type': 'application/json' },
42
+ body: JSON.stringify({ name, version, manifest: rest }),
43
+ })
44
+ await api(made.upload.url, { method: 'PUT', headers: { 'content-type': 'application/gzip' }, body: bundle })
45
+ console.log(`Published ${name}@${version} (${paths.length} files) — draws ${made.types.join(', ')}.`)
46
+ }
47
+
48
+ const [command, dir = '.'] = process.argv.slice(2)
49
+ if (command !== 'publish') {
50
+ console.log('Usage: npx wildcrane-artifact-sdk publish [dir]')
51
+ process.exit(command ? 1 : 0)
52
+ }
53
+ publish(resolve(dir)).catch((e) => {
54
+ console.error(`publish: ${e.message}`)
55
+ process.exit(1)
56
+ })
package/bin/pack.mjs ADDED
@@ -0,0 +1,70 @@
1
+ // The bundle `publish` sends: a ustar archive of the component's folder.
2
+
3
+ import { readdirSync, statSync } from 'node:fs'
4
+ import { join, relative, sep } from 'node:path'
5
+
6
+ export const MANIFEST = 'wildcrane.component.json'
7
+
8
+ /** Every file under dir but the manifest, dot files and node_modules, by its `/` path. */
9
+ /** @param {string} dir @returns {string[]} */
10
+ export function filesOf(dir) {
11
+ /** @type {string[]} */
12
+ const out = []
13
+ /** @param {string} at */
14
+ const walk = (at) => {
15
+ for (const name of readdirSync(at).sort()) {
16
+ if (name.startsWith('.') || name === 'node_modules') continue
17
+ const path = join(at, name)
18
+ if (statSync(path).isDirectory()) walk(path)
19
+ else out.push(relative(dir, path).split(sep).join('/'))
20
+ }
21
+ }
22
+ walk(dir)
23
+ return out.filter((p) => p !== MANIFEST)
24
+ }
25
+
26
+ /** A ustar archive; a path past 100 bytes travels in a pax header. */
27
+ /** @param {[string, Uint8Array][]} files */
28
+ export function tarOf(files) {
29
+ /** @type {Uint8Array[]} */
30
+ const blocks = []
31
+ const enc = new TextEncoder()
32
+ /** @param {string} name @param {number} size @param {string} kind */
33
+ const header = (name, size, kind) => {
34
+ const h = new Uint8Array(512)
35
+ /** @param {string} text @param {number} at */
36
+ const put = (text, at) => h.set(enc.encode(text), at)
37
+ put(name.slice(0, 100), 0)
38
+ put('0000644\0', 100)
39
+ put('0000000\0', 108)
40
+ put('0000000\0', 116)
41
+ put(size.toString(8).padStart(11, '0') + '\0', 124)
42
+ put('00000000000\0', 136)
43
+ put(' ', 148)
44
+ put(kind, 156)
45
+ put('ustar\x0000', 257)
46
+ const sum = h.reduce((n, b) => n + b, 0)
47
+ put(sum.toString(8).padStart(6, '0') + '\0 ', 148)
48
+ return h
49
+ }
50
+ /** @param {string} name @param {Uint8Array} body */
51
+ const entry = (name, body, kind = '0') =>
52
+ blocks.push(header(name, body.length, kind), body, new Uint8Array((512 - (body.length % 512)) % 512))
53
+ for (const [name, body] of files) {
54
+ if (enc.encode(name).length > 100) {
55
+ const record = ` path=${name}\n`
56
+ let n = enc.encode(record).length
57
+ n += String(n + String(n).length).length
58
+ entry('PaxHeader', enc.encode(`${n}${record}`), 'x')
59
+ }
60
+ entry(name, body)
61
+ }
62
+ blocks.push(new Uint8Array(1024))
63
+ const out = new Uint8Array(blocks.reduce((n, b) => n + b.length, 0))
64
+ let at = 0
65
+ for (const b of blocks) {
66
+ out.set(b, at)
67
+ at += b.length
68
+ }
69
+ return out
70
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "compare",
3
+ "title": { "en": "Compare", "zh": "对比" },
4
+ "types": ["compare"],
5
+ "schema": {
6
+ "type": "object",
7
+ "required": ["a", "b"],
8
+ "properties": { "a": { "type": "string" }, "b": { "type": "string" } }
9
+ },
10
+ "references": [
11
+ { "json": "/a", "types": ["storyboard", "compare", "spritesheet", "studio"] },
12
+ { "json": "/b", "types": ["storyboard", "compare", "spritesheet", "studio"] },
13
+ { "json": "/shots", "types": ["storyboard", "compare", "spritesheet", "studio"] }
14
+ ]
15
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "html",
3
+ "title": { "en": "Page", "zh": "页面" },
4
+ "types": ["html"],
5
+ "references": [{ "web": "html" }]
6
+ }
@@ -0,0 +1 @@
1
+ { "name": "image", "title": { "en": "Image", "zh": "图片" }, "types": ["image"] }
@@ -0,0 +1,2 @@
1
+ declare const _default: unknown[];
2
+ export default _default;
@@ -0,0 +1,13 @@
1
+ // 内置组件的清单,与第三方的 wildcrane.component.json 同一写法。
2
+ import compare from './compare.json';
3
+ import html from './html.json';
4
+ import image from './image.json';
5
+ import markdown from './markdown.json';
6
+ import spritesheet from './spritesheet.json';
7
+ import storyboard from './storyboard.json';
8
+ import tiled from './tiled.json';
9
+ import video from './video.json';
10
+ import studio from './studio.json';
11
+ // akb 的打包把 JSON 当文本读入。
12
+ const read = (m) => (typeof m === 'string' ? JSON.parse(m) : m);
13
+ export default [html, image, video, markdown, storyboard, compare, studio, tiled, spritesheet].map(read);
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "markdown",
3
+ "title": { "en": "Document", "zh": "文档" },
4
+ "types": ["markdown"],
5
+ "references": [
6
+ { "markdown": "image" },
7
+ { "text": "<img\\b[^<>]*?\\ssrc\\s*=\\s*[\"']([^\"']+)[\"']" }
8
+ ]
9
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "spritesheet",
3
+ "title": { "en": "Sprite animation", "zh": "精灵动画" },
4
+ "types": ["spritesheet"],
5
+ "schema": {
6
+ "type": "object",
7
+ "required": ["frames", "meta"],
8
+ "properties": {
9
+ "frames": {
10
+ "anyOf": [
11
+ { "type": "array", "minItems": 1, "items": { "type": "object", "required": ["frame"] } },
12
+ { "type": "object", "additionalProperties": { "type": "object", "required": ["frame"] } }
13
+ ]
14
+ },
15
+ "meta": {
16
+ "type": "object",
17
+ "required": ["app", "image"],
18
+ "properties": { "app": { "type": "string", "pattern": "codeandweb\\.com/texturepacker" }, "image": { "type": "string" } }
19
+ }
20
+ }
21
+ },
22
+ "references": [{ "json": "/meta/image" }]
23
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "storyboard",
3
+ "title": { "en": "Storyboard", "zh": "分镜" },
4
+ "types": ["storyboard"],
5
+ "schema": {
6
+ "type": "object",
7
+ "anyOf": [{ "required": ["shots"] }, { "required": ["slides"] }],
8
+ "properties": {
9
+ "shots": {
10
+ "type": "array",
11
+ "items": {
12
+ "type": "object",
13
+ "properties": { "frames": { "type": "array", "items": { "type": "object", "properties": { "src": { "type": "string" } } } } }
14
+ }
15
+ },
16
+ "slides": {
17
+ "type": "array",
18
+ "items": {
19
+ "type": "object",
20
+ "properties": {
21
+ "preview": { "type": "object", "properties": { "src": { "type": "string" } } },
22
+ "assets": { "type": "array", "items": { "type": "string" } }
23
+ }
24
+ }
25
+ }
26
+ }
27
+ },
28
+ "references": [
29
+ { "json": "/shots/*/frames/*/src", "types": ["storyboard", "compare", "spritesheet", "studio"] },
30
+ { "json": "/slides/*/preview/src", "types": ["storyboard", "compare", "spritesheet", "studio"] },
31
+ { "json": "/slides/*/assets/*" }
32
+ ]
33
+ }