@nebutra/audio-pipeline 0.1.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/.turbo/turbo-build.log +22 -0
- package/.turbo/turbo-test.log +14 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/README.md +19 -0
- package/dist/chunk-32LVUMTP.js +141 -0
- package/dist/chunk-32LVUMTP.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +45 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/examples/bgm.ts +10 -0
- package/examples/license.ts +11 -0
- package/examples/loudness.ts +11 -0
- package/package.json +59 -0
- package/src/cli.ts +36 -0
- package/src/index.test.ts +33 -0
- package/src/index.ts +176 -0
- package/tsconfig.json +12 -0
- package/tsup.config.ts +11 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
> @nebutra/audio-pipeline@0.1.0 build /home/runner/work/Nebutra-Sailor/Nebutra-Sailor/packages/ai/audio-pipeline
|
|
3
|
+
> tsup
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/cli.ts, src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.5.1
|
|
8
|
+
[34mCLI[39m Using tsup config: /home/runner/work/Nebutra-Sailor/Nebutra-Sailor/packages/ai/audio-pipeline/tsup.config.ts
|
|
9
|
+
[34mCLI[39m Target: es2022
|
|
10
|
+
[34mCLI[39m Cleaning output folder
|
|
11
|
+
[34mESM[39m Build start
|
|
12
|
+
[32mESM[39m [1mdist/cli.js [22m[32m1.32 KB[39m
|
|
13
|
+
[32mESM[39m [1mdist/index.js [22m[32m186.00 B[39m
|
|
14
|
+
[32mESM[39m [1mdist/chunk-32LVUMTP.js [22m[32m4.46 KB[39m
|
|
15
|
+
[32mESM[39m [1mdist/cli.js.map [22m[32m2.35 KB[39m
|
|
16
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m71.00 B[39m
|
|
17
|
+
[32mESM[39m [1mdist/chunk-32LVUMTP.js.map [22m[32m8.83 KB[39m
|
|
18
|
+
[32mESM[39m ⚡️ Build success in 91ms
|
|
19
|
+
[34mDTS[39m Build start
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 11357ms
|
|
21
|
+
[32mDTS[39m [1mdist/cli.d.ts [22m[32m13.00 B[39m
|
|
22
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.47 KB[39m
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
> @nebutra/audio-pipeline@0.1.0 test /home/runner/work/Nebutra-Sailor/Nebutra-Sailor/packages/ai/audio-pipeline
|
|
3
|
+
> vitest run
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
[1m[30m[46m RUN [49m[39m[22m [36mv4.1.4 [39m[90m/home/runner/work/Nebutra-Sailor/Nebutra-Sailor/packages/ai/audio-pipeline[39m
|
|
7
|
+
|
|
8
|
+
[32m✓[39m src/index.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 163[2mms[22m[39m
|
|
9
|
+
|
|
10
|
+
[2m Test Files [22m [1m[32m1 passed[39m[22m[90m (1)[39m
|
|
11
|
+
[2m Tests [22m [1m[32m2 passed[39m[22m[90m (2)[39m
|
|
12
|
+
[2m Start at [22m 05:41:39
|
|
13
|
+
[2m Duration [22m 1.49s[2m (transform 462ms, setup 0ms, import 588ms, tests 163ms, environment 0ms)[22m
|
|
14
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @nebutra/audio-pipeline
|
|
2
|
+
|
|
3
|
+
Status: WIP — Not yet integrated into any production app.
|
|
4
|
+
|
|
5
|
+
`@nebutra/audio-pipeline` is the BrandContext-first audio capability surface.
|
|
6
|
+
The zero-config path writes short valid WAV files with license metadata so
|
|
7
|
+
doctor, debug, and examples are executable without model credentials.
|
|
8
|
+
|
|
9
|
+
It does not own prompt orchestration, Thread/Turn/Item state, model provider
|
|
10
|
+
routing, or approval lifecycle.
|
|
11
|
+
|
|
12
|
+
## Commands
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm audio:doctor
|
|
16
|
+
pnpm audio:debug
|
|
17
|
+
pnpm audio:license <asset>
|
|
18
|
+
pnpm audio:loudness <asset>
|
|
19
|
+
```
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
3
|
+
import { dirname, join } from "path";
|
|
4
|
+
import { appendCapabilityDebug, readCapabilityDebug } from "@nebutra/capability-kit/debug";
|
|
5
|
+
import { CapabilityError } from "@nebutra/errors";
|
|
6
|
+
import {
|
|
7
|
+
assetId,
|
|
8
|
+
requireBrandContext
|
|
9
|
+
} from "@nebutra/generation-context";
|
|
10
|
+
async function readAudioDebug(root = process.cwd(), limit = 10) {
|
|
11
|
+
return readCapabilityDebug("audio-pipeline", { root, limit });
|
|
12
|
+
}
|
|
13
|
+
function intentLabel(intent) {
|
|
14
|
+
switch (intent.type) {
|
|
15
|
+
case "bgm":
|
|
16
|
+
return intent.mood;
|
|
17
|
+
case "sfx":
|
|
18
|
+
return intent.description;
|
|
19
|
+
case "song":
|
|
20
|
+
return intent.lyricsPrompt;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function durationFor(intent) {
|
|
24
|
+
if (intent.type === "sfx") return Math.max(1, Math.min(intent.durationS ?? 3, 8));
|
|
25
|
+
return Math.max(1, Math.min(intent.durationS, 12));
|
|
26
|
+
}
|
|
27
|
+
function createToneWav(durationS, frequency = 440, sampleRate = 24e3) {
|
|
28
|
+
const samples = Math.max(1, Math.floor(durationS * sampleRate));
|
|
29
|
+
const dataBytes = samples * 2;
|
|
30
|
+
const buffer = Buffer.alloc(44 + dataBytes);
|
|
31
|
+
buffer.write("RIFF", 0);
|
|
32
|
+
buffer.writeUInt32LE(36 + dataBytes, 4);
|
|
33
|
+
buffer.write("WAVE", 8);
|
|
34
|
+
buffer.write("fmt ", 12);
|
|
35
|
+
buffer.writeUInt32LE(16, 16);
|
|
36
|
+
buffer.writeUInt16LE(1, 20);
|
|
37
|
+
buffer.writeUInt16LE(1, 22);
|
|
38
|
+
buffer.writeUInt32LE(sampleRate, 24);
|
|
39
|
+
buffer.writeUInt32LE(sampleRate * 2, 28);
|
|
40
|
+
buffer.writeUInt16LE(2, 32);
|
|
41
|
+
buffer.writeUInt16LE(16, 34);
|
|
42
|
+
buffer.write("data", 36);
|
|
43
|
+
buffer.writeUInt32LE(dataBytes, 40);
|
|
44
|
+
for (let index = 0; index < samples; index += 1) {
|
|
45
|
+
const fade = Math.min(index / 1200, (samples - index) / 1200, 1);
|
|
46
|
+
const sample = Math.round(
|
|
47
|
+
Math.sin(2 * Math.PI * frequency * index / sampleRate) * 9e3 * fade
|
|
48
|
+
);
|
|
49
|
+
buffer.writeInt16LE(sample, 44 + index * 2);
|
|
50
|
+
}
|
|
51
|
+
return buffer;
|
|
52
|
+
}
|
|
53
|
+
function frequencyFor(brand, intent) {
|
|
54
|
+
const base = brand.toneKeywords.join("").length + intentLabel(intent).length;
|
|
55
|
+
return 220 + base % 9 * 55;
|
|
56
|
+
}
|
|
57
|
+
function licenseFor(requireCommercial, provider) {
|
|
58
|
+
if (requireCommercial && provider !== "tone-local") {
|
|
59
|
+
return {
|
|
60
|
+
status: "unknown",
|
|
61
|
+
source: provider,
|
|
62
|
+
suggestion: "Verify the provider plan and model terms before commercial use."
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return { status: "commercial-ok", source: "deterministic local renderer" };
|
|
66
|
+
}
|
|
67
|
+
var AudioPipeline = class {
|
|
68
|
+
#root;
|
|
69
|
+
#provider;
|
|
70
|
+
constructor(options = {}) {
|
|
71
|
+
this.#root = options.root ?? process.cwd();
|
|
72
|
+
this.#provider = options.provider ?? "tone-local";
|
|
73
|
+
}
|
|
74
|
+
async generate(intent, brandInput, requireCommercial = true) {
|
|
75
|
+
const brand = requireBrandContext(brandInput, "audio-pipeline");
|
|
76
|
+
if (requireCommercial && this.#provider !== "tone-local") {
|
|
77
|
+
throw new CapabilityError(
|
|
78
|
+
"audio-pipeline",
|
|
79
|
+
"Selected audio provider lacks verified license",
|
|
80
|
+
{
|
|
81
|
+
suggestion: "Use tone-local or wire a provider with commercial license metadata.",
|
|
82
|
+
statusCode: 409
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const durationS = durationFor(intent);
|
|
87
|
+
const id = assetId("audio", `${intent.type}_${intentLabel(intent)}`);
|
|
88
|
+
const path = join(this.#root, ".nebutra", "generated", "audio-pipeline", `${id}.wav`);
|
|
89
|
+
await mkdir(dirname(path), { recursive: true });
|
|
90
|
+
await writeFile(path, createToneWav(durationS, frequencyFor(brand, intent)));
|
|
91
|
+
const asset = {
|
|
92
|
+
id,
|
|
93
|
+
tenantId: brand.tenantId,
|
|
94
|
+
kind: "audio",
|
|
95
|
+
path,
|
|
96
|
+
brandId: brand.brandId,
|
|
97
|
+
provider: this.#provider,
|
|
98
|
+
model: "brand-context-tone-v1",
|
|
99
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
100
|
+
license: licenseFor(requireCommercial, this.#provider),
|
|
101
|
+
durationS,
|
|
102
|
+
format: "wav",
|
|
103
|
+
loudnessLufs: -18,
|
|
104
|
+
metadata: { intent, brandSource: brand.sourcePath }
|
|
105
|
+
};
|
|
106
|
+
await appendCapabilityDebug(
|
|
107
|
+
"audio-pipeline",
|
|
108
|
+
{ type: "generate", asset },
|
|
109
|
+
{ root: this.#root }
|
|
110
|
+
);
|
|
111
|
+
return asset;
|
|
112
|
+
}
|
|
113
|
+
async doctor() {
|
|
114
|
+
return [
|
|
115
|
+
{ provider: "tone-local", ok: true },
|
|
116
|
+
{
|
|
117
|
+
provider: "local-model",
|
|
118
|
+
ok: Boolean(process.env.AUDIO_LOCAL_MODEL_PATH),
|
|
119
|
+
suggestion: "Set AUDIO_LOCAL_MODEL_PATH to enable local model generation."
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
provider: "remote",
|
|
123
|
+
ok: Boolean(process.env.AUDIO_REMOTE_API_KEY),
|
|
124
|
+
suggestion: "Set AUDIO_REMOTE_API_KEY to enable remote audio fallback."
|
|
125
|
+
}
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
async license(asset) {
|
|
129
|
+
return asset.license;
|
|
130
|
+
}
|
|
131
|
+
async loudness(asset) {
|
|
132
|
+
return { lufs: asset.loudnessLufs };
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export {
|
|
137
|
+
readAudioDebug,
|
|
138
|
+
createToneWav,
|
|
139
|
+
AudioPipeline
|
|
140
|
+
};
|
|
141
|
+
//# sourceMappingURL=chunk-32LVUMTP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { mkdir, writeFile } from \"node:fs/promises\";\nimport { dirname, join } from \"node:path\";\nimport { appendCapabilityDebug, readCapabilityDebug } from \"@nebutra/capability-kit/debug\";\nimport { CapabilityError } from \"@nebutra/errors\";\nimport {\n assetId,\n type BrandContext,\n type GeneratedAsset,\n type LicenseMetadata,\n requireBrandContext,\n} from \"@nebutra/generation-context\";\n\nexport type AudioIntent =\n | { type: \"bgm\"; durationS: number; mood: string; bpm?: number }\n | { type: \"sfx\"; description: string; durationS?: number }\n | { type: \"song\"; lyricsPrompt: string; durationS: number };\n\nexport interface AudioAsset extends GeneratedAsset {\n readonly kind: \"audio\";\n readonly durationS: number;\n readonly format: \"wav\";\n readonly loudnessLufs: number;\n}\n\nexport interface AudioHealth {\n readonly provider: string;\n readonly ok: boolean;\n readonly suggestion?: string;\n}\n\nexport interface AudioPipelineOptions {\n readonly root?: string;\n readonly provider?: \"tone-local\" | \"local-model\" | \"remote\";\n}\n\nexport async function readAudioDebug(root = process.cwd(), limit = 10): Promise<unknown[]> {\n return readCapabilityDebug(\"audio-pipeline\", { root, limit });\n}\n\nfunction intentLabel(intent: AudioIntent): string {\n switch (intent.type) {\n case \"bgm\":\n return intent.mood;\n case \"sfx\":\n return intent.description;\n case \"song\":\n return intent.lyricsPrompt;\n }\n}\n\nfunction durationFor(intent: AudioIntent): number {\n if (intent.type === \"sfx\") return Math.max(1, Math.min(intent.durationS ?? 3, 8));\n return Math.max(1, Math.min(intent.durationS, 12));\n}\n\nexport function createToneWav(durationS: number, frequency = 440, sampleRate = 24_000): Buffer {\n const samples = Math.max(1, Math.floor(durationS * sampleRate));\n const dataBytes = samples * 2;\n const buffer = Buffer.alloc(44 + dataBytes);\n buffer.write(\"RIFF\", 0);\n buffer.writeUInt32LE(36 + dataBytes, 4);\n buffer.write(\"WAVE\", 8);\n buffer.write(\"fmt \", 12);\n buffer.writeUInt32LE(16, 16);\n buffer.writeUInt16LE(1, 20);\n buffer.writeUInt16LE(1, 22);\n buffer.writeUInt32LE(sampleRate, 24);\n buffer.writeUInt32LE(sampleRate * 2, 28);\n buffer.writeUInt16LE(2, 32);\n buffer.writeUInt16LE(16, 34);\n buffer.write(\"data\", 36);\n buffer.writeUInt32LE(dataBytes, 40);\n for (let index = 0; index < samples; index += 1) {\n const fade = Math.min(index / 1200, (samples - index) / 1200, 1);\n const sample = Math.round(\n Math.sin((2 * Math.PI * frequency * index) / sampleRate) * 9000 * fade,\n );\n buffer.writeInt16LE(sample, 44 + index * 2);\n }\n return buffer;\n}\n\nfunction frequencyFor(brand: BrandContext, intent: AudioIntent): number {\n const base = brand.toneKeywords.join(\"\").length + intentLabel(intent).length;\n return 220 + (base % 9) * 55;\n}\n\nfunction licenseFor(requireCommercial: boolean, provider: string): LicenseMetadata {\n if (requireCommercial && provider !== \"tone-local\") {\n return {\n status: \"unknown\",\n source: provider,\n suggestion: \"Verify the provider plan and model terms before commercial use.\",\n };\n }\n return { status: \"commercial-ok\", source: \"deterministic local renderer\" };\n}\n\nexport class AudioPipeline {\n readonly #root: string;\n readonly #provider: \"tone-local\" | \"local-model\" | \"remote\";\n\n constructor(options: AudioPipelineOptions = {}) {\n this.#root = options.root ?? process.cwd();\n this.#provider = options.provider ?? \"tone-local\";\n }\n\n async generate(\n intent: AudioIntent,\n brandInput: BrandContext | undefined,\n requireCommercial = true,\n ): Promise<AudioAsset> {\n const brand = requireBrandContext(brandInput, \"audio-pipeline\");\n if (requireCommercial && this.#provider !== \"tone-local\") {\n throw new CapabilityError(\n \"audio-pipeline\",\n \"Selected audio provider lacks verified license\",\n {\n suggestion: \"Use tone-local or wire a provider with commercial license metadata.\",\n statusCode: 409,\n },\n );\n }\n const durationS = durationFor(intent);\n const id = assetId(\"audio\", `${intent.type}_${intentLabel(intent)}`);\n const path = join(this.#root, \".nebutra\", \"generated\", \"audio-pipeline\", `${id}.wav`);\n await mkdir(dirname(path), { recursive: true });\n await writeFile(path, createToneWav(durationS, frequencyFor(brand, intent)));\n\n const asset: AudioAsset = {\n id,\n tenantId: brand.tenantId,\n kind: \"audio\",\n path,\n brandId: brand.brandId,\n provider: this.#provider,\n model: \"brand-context-tone-v1\",\n createdAt: new Date().toISOString(),\n license: licenseFor(requireCommercial, this.#provider),\n durationS,\n format: \"wav\",\n loudnessLufs: -18,\n metadata: { intent, brandSource: brand.sourcePath },\n };\n await appendCapabilityDebug(\n \"audio-pipeline\",\n { type: \"generate\", asset },\n { root: this.#root },\n );\n return asset;\n }\n\n async doctor(): Promise<AudioHealth[]> {\n return [\n { provider: \"tone-local\", ok: true },\n {\n provider: \"local-model\",\n ok: Boolean(process.env.AUDIO_LOCAL_MODEL_PATH),\n suggestion: \"Set AUDIO_LOCAL_MODEL_PATH to enable local model generation.\",\n },\n {\n provider: \"remote\",\n ok: Boolean(process.env.AUDIO_REMOTE_API_KEY),\n suggestion: \"Set AUDIO_REMOTE_API_KEY to enable remote audio fallback.\",\n },\n ];\n }\n\n async license(asset: Pick<AudioAsset, \"license\">): Promise<LicenseMetadata> {\n return asset.license;\n }\n\n async loudness(asset: Pick<AudioAsset, \"loudnessLufs\">): Promise<{ lufs: number }> {\n return { lufs: asset.loudnessLufs };\n }\n}\n"],"mappings":"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","names":[]}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AudioPipeline,
|
|
3
|
+
readAudioDebug
|
|
4
|
+
} from "./chunk-32LVUMTP.js";
|
|
5
|
+
|
|
6
|
+
// src/cli.ts
|
|
7
|
+
import { createDemoBrandContext } from "@nebutra/generation-context";
|
|
8
|
+
var command = process.argv[2] ?? "doctor";
|
|
9
|
+
var pipeline = new AudioPipeline();
|
|
10
|
+
if (command === "doctor") {
|
|
11
|
+
process.stdout.write(
|
|
12
|
+
`${JSON.stringify({ capability: "audio-pipeline", results: await pipeline.doctor() }, null, 2)}
|
|
13
|
+
`
|
|
14
|
+
);
|
|
15
|
+
} else if (command === "debug") {
|
|
16
|
+
process.stdout.write(
|
|
17
|
+
`${JSON.stringify({ capability: "audio-pipeline", entries: await readAudioDebug() }, null, 2)}
|
|
18
|
+
`
|
|
19
|
+
);
|
|
20
|
+
} else if (command === "license") {
|
|
21
|
+
const asset = await pipeline.generate(
|
|
22
|
+
{ type: "bgm", durationS: 3, mood: "uplifting technical" },
|
|
23
|
+
createDemoBrandContext(),
|
|
24
|
+
true
|
|
25
|
+
);
|
|
26
|
+
process.stdout.write(
|
|
27
|
+
`${JSON.stringify({ capability: "audio-pipeline", license: await pipeline.license(asset) }, null, 2)}
|
|
28
|
+
`
|
|
29
|
+
);
|
|
30
|
+
} else if (command === "loudness") {
|
|
31
|
+
const asset = await pipeline.generate(
|
|
32
|
+
{ type: "sfx", description: "keyboard typing", durationS: 1 },
|
|
33
|
+
createDemoBrandContext(),
|
|
34
|
+
true
|
|
35
|
+
);
|
|
36
|
+
process.stdout.write(
|
|
37
|
+
`${JSON.stringify({ capability: "audio-pipeline", loudness: await pipeline.loudness(asset) }, null, 2)}
|
|
38
|
+
`
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
process.stderr.write(`Unknown audio-pipeline command: ${command}
|
|
42
|
+
`);
|
|
43
|
+
process.exitCode = 1;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts"],"sourcesContent":["import { createDemoBrandContext } from \"@nebutra/generation-context\";\nimport { AudioPipeline, readAudioDebug } from \"./index\";\n\nconst command = process.argv[2] ?? \"doctor\";\nconst pipeline = new AudioPipeline();\n\nif (command === \"doctor\") {\n process.stdout.write(\n `${JSON.stringify({ capability: \"audio-pipeline\", results: await pipeline.doctor() }, null, 2)}\\n`,\n );\n} else if (command === \"debug\") {\n process.stdout.write(\n `${JSON.stringify({ capability: \"audio-pipeline\", entries: await readAudioDebug() }, null, 2)}\\n`,\n );\n} else if (command === \"license\") {\n const asset = await pipeline.generate(\n { type: \"bgm\", durationS: 3, mood: \"uplifting technical\" },\n createDemoBrandContext(),\n true,\n );\n process.stdout.write(\n `${JSON.stringify({ capability: \"audio-pipeline\", license: await pipeline.license(asset) }, null, 2)}\\n`,\n );\n} else if (command === \"loudness\") {\n const asset = await pipeline.generate(\n { type: \"sfx\", description: \"keyboard typing\", durationS: 1 },\n createDemoBrandContext(),\n true,\n );\n process.stdout.write(\n `${JSON.stringify({ capability: \"audio-pipeline\", loudness: await pipeline.loudness(asset) }, null, 2)}\\n`,\n );\n} else {\n process.stderr.write(`Unknown audio-pipeline command: ${command}\\n`);\n process.exitCode = 1;\n}\n"],"mappings":";;;;;;AAAA,SAAS,8BAA8B;AAGvC,IAAM,UAAU,QAAQ,KAAK,CAAC,KAAK;AACnC,IAAM,WAAW,IAAI,cAAc;AAEnC,IAAI,YAAY,UAAU;AACxB,UAAQ,OAAO;AAAA,IACb,GAAG,KAAK,UAAU,EAAE,YAAY,kBAAkB,SAAS,MAAM,SAAS,OAAO,EAAE,GAAG,MAAM,CAAC,CAAC;AAAA;AAAA,EAChG;AACF,WAAW,YAAY,SAAS;AAC9B,UAAQ,OAAO;AAAA,IACb,GAAG,KAAK,UAAU,EAAE,YAAY,kBAAkB,SAAS,MAAM,eAAe,EAAE,GAAG,MAAM,CAAC,CAAC;AAAA;AAAA,EAC/F;AACF,WAAW,YAAY,WAAW;AAChC,QAAM,QAAQ,MAAM,SAAS;AAAA,IAC3B,EAAE,MAAM,OAAO,WAAW,GAAG,MAAM,sBAAsB;AAAA,IACzD,uBAAuB;AAAA,IACvB;AAAA,EACF;AACA,UAAQ,OAAO;AAAA,IACb,GAAG,KAAK,UAAU,EAAE,YAAY,kBAAkB,SAAS,MAAM,SAAS,QAAQ,KAAK,EAAE,GAAG,MAAM,CAAC,CAAC;AAAA;AAAA,EACtG;AACF,WAAW,YAAY,YAAY;AACjC,QAAM,QAAQ,MAAM,SAAS;AAAA,IAC3B,EAAE,MAAM,OAAO,aAAa,mBAAmB,WAAW,EAAE;AAAA,IAC5D,uBAAuB;AAAA,IACvB;AAAA,EACF;AACA,UAAQ,OAAO;AAAA,IACb,GAAG,KAAK,UAAU,EAAE,YAAY,kBAAkB,UAAU,MAAM,SAAS,SAAS,KAAK,EAAE,GAAG,MAAM,CAAC,CAAC;AAAA;AAAA,EACxG;AACF,OAAO;AACL,UAAQ,OAAO,MAAM,mCAAmC,OAAO;AAAA,CAAI;AACnE,UAAQ,WAAW;AACrB;","names":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { GeneratedAsset, BrandContext, LicenseMetadata } from '@nebutra/generation-context';
|
|
2
|
+
|
|
3
|
+
type AudioIntent = {
|
|
4
|
+
type: "bgm";
|
|
5
|
+
durationS: number;
|
|
6
|
+
mood: string;
|
|
7
|
+
bpm?: number;
|
|
8
|
+
} | {
|
|
9
|
+
type: "sfx";
|
|
10
|
+
description: string;
|
|
11
|
+
durationS?: number;
|
|
12
|
+
} | {
|
|
13
|
+
type: "song";
|
|
14
|
+
lyricsPrompt: string;
|
|
15
|
+
durationS: number;
|
|
16
|
+
};
|
|
17
|
+
interface AudioAsset extends GeneratedAsset {
|
|
18
|
+
readonly kind: "audio";
|
|
19
|
+
readonly durationS: number;
|
|
20
|
+
readonly format: "wav";
|
|
21
|
+
readonly loudnessLufs: number;
|
|
22
|
+
}
|
|
23
|
+
interface AudioHealth {
|
|
24
|
+
readonly provider: string;
|
|
25
|
+
readonly ok: boolean;
|
|
26
|
+
readonly suggestion?: string;
|
|
27
|
+
}
|
|
28
|
+
interface AudioPipelineOptions {
|
|
29
|
+
readonly root?: string;
|
|
30
|
+
readonly provider?: "tone-local" | "local-model" | "remote";
|
|
31
|
+
}
|
|
32
|
+
declare function readAudioDebug(root?: string, limit?: number): Promise<unknown[]>;
|
|
33
|
+
declare function createToneWav(durationS: number, frequency?: number, sampleRate?: number): Buffer;
|
|
34
|
+
declare class AudioPipeline {
|
|
35
|
+
#private;
|
|
36
|
+
constructor(options?: AudioPipelineOptions);
|
|
37
|
+
generate(intent: AudioIntent, brandInput: BrandContext | undefined, requireCommercial?: boolean): Promise<AudioAsset>;
|
|
38
|
+
doctor(): Promise<AudioHealth[]>;
|
|
39
|
+
license(asset: Pick<AudioAsset, "license">): Promise<LicenseMetadata>;
|
|
40
|
+
loudness(asset: Pick<AudioAsset, "loudnessLufs">): Promise<{
|
|
41
|
+
lufs: number;
|
|
42
|
+
}>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { type AudioAsset, type AudioHealth, type AudioIntent, AudioPipeline, type AudioPipelineOptions, createToneWav, readAudioDebug };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/examples/bgm.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createDemoBrandContext } from "@nebutra/generation-context";
|
|
2
|
+
import { AudioPipeline } from "../src/index";
|
|
3
|
+
|
|
4
|
+
const asset = await new AudioPipeline().generate(
|
|
5
|
+
{ type: "bgm", durationS: 5, mood: "uplifting tech", bpm: 110 },
|
|
6
|
+
createDemoBrandContext(),
|
|
7
|
+
true,
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
process.stdout.write(`${asset.path}\n`);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createDemoBrandContext } from "@nebutra/generation-context";
|
|
2
|
+
import { AudioPipeline } from "../src/index";
|
|
3
|
+
|
|
4
|
+
const pipeline = new AudioPipeline();
|
|
5
|
+
const asset = await pipeline.generate(
|
|
6
|
+
{ type: "sfx", description: "keyboard typing", durationS: 2 },
|
|
7
|
+
createDemoBrandContext(),
|
|
8
|
+
true,
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
process.stdout.write(`${JSON.stringify(await pipeline.license(asset), null, 2)}\n`);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createDemoBrandContext } from "@nebutra/generation-context";
|
|
2
|
+
import { AudioPipeline } from "../src/index";
|
|
3
|
+
|
|
4
|
+
const pipeline = new AudioPipeline();
|
|
5
|
+
const asset = await pipeline.generate(
|
|
6
|
+
{ type: "song", lyricsPrompt: "indie developer anthem", durationS: 8 },
|
|
7
|
+
createDemoBrandContext(),
|
|
8
|
+
true,
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
process.stdout.write(`${JSON.stringify(await pipeline.loudness(asset), null, 2)}\n`);
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nebutra/audio-pipeline",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "BrandContext-first audio generation, license metadata, and loudness utilities",
|
|
5
|
+
"private": false,
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"nebutra": {
|
|
9
|
+
"status": "wip",
|
|
10
|
+
"productionReady": false,
|
|
11
|
+
"surface": "generation-capability",
|
|
12
|
+
"requires": [
|
|
13
|
+
"@nebutra/generation-context for BrandContext",
|
|
14
|
+
"@nebutra/sandbox-runtime for model and ffmpeg sidecars"
|
|
15
|
+
],
|
|
16
|
+
"gaps": [
|
|
17
|
+
"Music model adapters are health-checked but not launched by this package",
|
|
18
|
+
"Commercial license policy is local metadata until asset governance lands",
|
|
19
|
+
"LUFS measurement uses deterministic estimation until ffmpeg/pyloudnorm sidecar is wired"
|
|
20
|
+
],
|
|
21
|
+
"featureId": "audio-pipeline",
|
|
22
|
+
"category": "ai",
|
|
23
|
+
"summary": "License-aware BGM, SFX, and song generation surface"
|
|
24
|
+
},
|
|
25
|
+
"main": "./src/index.ts",
|
|
26
|
+
"types": "./src/index.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./src/index.ts"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@nebutra/capability-kit": "0.2.0",
|
|
32
|
+
"@nebutra/errors": "0.1.0",
|
|
33
|
+
"@nebutra/generation-context": "0.1.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^22.19.15",
|
|
37
|
+
"tsup": "^8.5.1",
|
|
38
|
+
"tsx": "^4.21.0",
|
|
39
|
+
"typescript": "^5.9.3",
|
|
40
|
+
"vitest": "^4.1.4"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/Nebutra/Nebutra-Sailor/tree/main/packages/ai/audio-pipeline#readme",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/Nebutra/Nebutra-Sailor.git",
|
|
46
|
+
"directory": "packages/ai/audio-pipeline"
|
|
47
|
+
},
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/Nebutra/Nebutra-Sailor/issues"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsup",
|
|
56
|
+
"test": "vitest run",
|
|
57
|
+
"typecheck": "tsc --noEmit"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createDemoBrandContext } from "@nebutra/generation-context";
|
|
2
|
+
import { AudioPipeline, readAudioDebug } from "./index";
|
|
3
|
+
|
|
4
|
+
const command = process.argv[2] ?? "doctor";
|
|
5
|
+
const pipeline = new AudioPipeline();
|
|
6
|
+
|
|
7
|
+
if (command === "doctor") {
|
|
8
|
+
process.stdout.write(
|
|
9
|
+
`${JSON.stringify({ capability: "audio-pipeline", results: await pipeline.doctor() }, null, 2)}\n`,
|
|
10
|
+
);
|
|
11
|
+
} else if (command === "debug") {
|
|
12
|
+
process.stdout.write(
|
|
13
|
+
`${JSON.stringify({ capability: "audio-pipeline", entries: await readAudioDebug() }, null, 2)}\n`,
|
|
14
|
+
);
|
|
15
|
+
} else if (command === "license") {
|
|
16
|
+
const asset = await pipeline.generate(
|
|
17
|
+
{ type: "bgm", durationS: 3, mood: "uplifting technical" },
|
|
18
|
+
createDemoBrandContext(),
|
|
19
|
+
true,
|
|
20
|
+
);
|
|
21
|
+
process.stdout.write(
|
|
22
|
+
`${JSON.stringify({ capability: "audio-pipeline", license: await pipeline.license(asset) }, null, 2)}\n`,
|
|
23
|
+
);
|
|
24
|
+
} else if (command === "loudness") {
|
|
25
|
+
const asset = await pipeline.generate(
|
|
26
|
+
{ type: "sfx", description: "keyboard typing", durationS: 1 },
|
|
27
|
+
createDemoBrandContext(),
|
|
28
|
+
true,
|
|
29
|
+
);
|
|
30
|
+
process.stdout.write(
|
|
31
|
+
`${JSON.stringify({ capability: "audio-pipeline", loudness: await pipeline.loudness(asset) }, null, 2)}\n`,
|
|
32
|
+
);
|
|
33
|
+
} else {
|
|
34
|
+
process.stderr.write(`Unknown audio-pipeline command: ${command}\n`);
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { createDemoBrandContext } from "@nebutra/generation-context";
|
|
5
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
6
|
+
import { AudioPipeline, readAudioDebug } from "./index";
|
|
7
|
+
|
|
8
|
+
let root: string | undefined;
|
|
9
|
+
|
|
10
|
+
afterEach(async () => {
|
|
11
|
+
if (root) await rm(root, { recursive: true, force: true });
|
|
12
|
+
root = undefined;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("AudioPipeline", () => {
|
|
16
|
+
it("requires BrandContext", async () => {
|
|
17
|
+
await expect(
|
|
18
|
+
new AudioPipeline().generate({ type: "sfx", description: "click" }, undefined, true),
|
|
19
|
+
).rejects.toMatchObject({ capability: "audio-pipeline" });
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("writes a valid WAV header with commercial metadata", async () => {
|
|
23
|
+
root = await mkdtemp(join(tmpdir(), "audio-pipeline-"));
|
|
24
|
+
const asset = await new AudioPipeline({ root }).generate(
|
|
25
|
+
{ type: "bgm", durationS: 4, mood: "calm technical" },
|
|
26
|
+
createDemoBrandContext(),
|
|
27
|
+
true,
|
|
28
|
+
);
|
|
29
|
+
expect((await readFile(asset.path)).subarray(0, 4).toString()).toBe("RIFF");
|
|
30
|
+
expect(asset.license.status).toBe("commercial-ok");
|
|
31
|
+
expect(await readAudioDebug(root)).toHaveLength(1);
|
|
32
|
+
});
|
|
33
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { appendCapabilityDebug, readCapabilityDebug } from "@nebutra/capability-kit/debug";
|
|
4
|
+
import { CapabilityError } from "@nebutra/errors";
|
|
5
|
+
import {
|
|
6
|
+
assetId,
|
|
7
|
+
type BrandContext,
|
|
8
|
+
type GeneratedAsset,
|
|
9
|
+
type LicenseMetadata,
|
|
10
|
+
requireBrandContext,
|
|
11
|
+
} from "@nebutra/generation-context";
|
|
12
|
+
|
|
13
|
+
export type AudioIntent =
|
|
14
|
+
| { type: "bgm"; durationS: number; mood: string; bpm?: number }
|
|
15
|
+
| { type: "sfx"; description: string; durationS?: number }
|
|
16
|
+
| { type: "song"; lyricsPrompt: string; durationS: number };
|
|
17
|
+
|
|
18
|
+
export interface AudioAsset extends GeneratedAsset {
|
|
19
|
+
readonly kind: "audio";
|
|
20
|
+
readonly durationS: number;
|
|
21
|
+
readonly format: "wav";
|
|
22
|
+
readonly loudnessLufs: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AudioHealth {
|
|
26
|
+
readonly provider: string;
|
|
27
|
+
readonly ok: boolean;
|
|
28
|
+
readonly suggestion?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface AudioPipelineOptions {
|
|
32
|
+
readonly root?: string;
|
|
33
|
+
readonly provider?: "tone-local" | "local-model" | "remote";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function readAudioDebug(root = process.cwd(), limit = 10): Promise<unknown[]> {
|
|
37
|
+
return readCapabilityDebug("audio-pipeline", { root, limit });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function intentLabel(intent: AudioIntent): string {
|
|
41
|
+
switch (intent.type) {
|
|
42
|
+
case "bgm":
|
|
43
|
+
return intent.mood;
|
|
44
|
+
case "sfx":
|
|
45
|
+
return intent.description;
|
|
46
|
+
case "song":
|
|
47
|
+
return intent.lyricsPrompt;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function durationFor(intent: AudioIntent): number {
|
|
52
|
+
if (intent.type === "sfx") return Math.max(1, Math.min(intent.durationS ?? 3, 8));
|
|
53
|
+
return Math.max(1, Math.min(intent.durationS, 12));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createToneWav(durationS: number, frequency = 440, sampleRate = 24_000): Buffer {
|
|
57
|
+
const samples = Math.max(1, Math.floor(durationS * sampleRate));
|
|
58
|
+
const dataBytes = samples * 2;
|
|
59
|
+
const buffer = Buffer.alloc(44 + dataBytes);
|
|
60
|
+
buffer.write("RIFF", 0);
|
|
61
|
+
buffer.writeUInt32LE(36 + dataBytes, 4);
|
|
62
|
+
buffer.write("WAVE", 8);
|
|
63
|
+
buffer.write("fmt ", 12);
|
|
64
|
+
buffer.writeUInt32LE(16, 16);
|
|
65
|
+
buffer.writeUInt16LE(1, 20);
|
|
66
|
+
buffer.writeUInt16LE(1, 22);
|
|
67
|
+
buffer.writeUInt32LE(sampleRate, 24);
|
|
68
|
+
buffer.writeUInt32LE(sampleRate * 2, 28);
|
|
69
|
+
buffer.writeUInt16LE(2, 32);
|
|
70
|
+
buffer.writeUInt16LE(16, 34);
|
|
71
|
+
buffer.write("data", 36);
|
|
72
|
+
buffer.writeUInt32LE(dataBytes, 40);
|
|
73
|
+
for (let index = 0; index < samples; index += 1) {
|
|
74
|
+
const fade = Math.min(index / 1200, (samples - index) / 1200, 1);
|
|
75
|
+
const sample = Math.round(
|
|
76
|
+
Math.sin((2 * Math.PI * frequency * index) / sampleRate) * 9000 * fade,
|
|
77
|
+
);
|
|
78
|
+
buffer.writeInt16LE(sample, 44 + index * 2);
|
|
79
|
+
}
|
|
80
|
+
return buffer;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function frequencyFor(brand: BrandContext, intent: AudioIntent): number {
|
|
84
|
+
const base = brand.toneKeywords.join("").length + intentLabel(intent).length;
|
|
85
|
+
return 220 + (base % 9) * 55;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function licenseFor(requireCommercial: boolean, provider: string): LicenseMetadata {
|
|
89
|
+
if (requireCommercial && provider !== "tone-local") {
|
|
90
|
+
return {
|
|
91
|
+
status: "unknown",
|
|
92
|
+
source: provider,
|
|
93
|
+
suggestion: "Verify the provider plan and model terms before commercial use.",
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return { status: "commercial-ok", source: "deterministic local renderer" };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class AudioPipeline {
|
|
100
|
+
readonly #root: string;
|
|
101
|
+
readonly #provider: "tone-local" | "local-model" | "remote";
|
|
102
|
+
|
|
103
|
+
constructor(options: AudioPipelineOptions = {}) {
|
|
104
|
+
this.#root = options.root ?? process.cwd();
|
|
105
|
+
this.#provider = options.provider ?? "tone-local";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async generate(
|
|
109
|
+
intent: AudioIntent,
|
|
110
|
+
brandInput: BrandContext | undefined,
|
|
111
|
+
requireCommercial = true,
|
|
112
|
+
): Promise<AudioAsset> {
|
|
113
|
+
const brand = requireBrandContext(brandInput, "audio-pipeline");
|
|
114
|
+
if (requireCommercial && this.#provider !== "tone-local") {
|
|
115
|
+
throw new CapabilityError(
|
|
116
|
+
"audio-pipeline",
|
|
117
|
+
"Selected audio provider lacks verified license",
|
|
118
|
+
{
|
|
119
|
+
suggestion: "Use tone-local or wire a provider with commercial license metadata.",
|
|
120
|
+
statusCode: 409,
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
const durationS = durationFor(intent);
|
|
125
|
+
const id = assetId("audio", `${intent.type}_${intentLabel(intent)}`);
|
|
126
|
+
const path = join(this.#root, ".nebutra", "generated", "audio-pipeline", `${id}.wav`);
|
|
127
|
+
await mkdir(dirname(path), { recursive: true });
|
|
128
|
+
await writeFile(path, createToneWav(durationS, frequencyFor(brand, intent)));
|
|
129
|
+
|
|
130
|
+
const asset: AudioAsset = {
|
|
131
|
+
id,
|
|
132
|
+
tenantId: brand.tenantId,
|
|
133
|
+
kind: "audio",
|
|
134
|
+
path,
|
|
135
|
+
brandId: brand.brandId,
|
|
136
|
+
provider: this.#provider,
|
|
137
|
+
model: "brand-context-tone-v1",
|
|
138
|
+
createdAt: new Date().toISOString(),
|
|
139
|
+
license: licenseFor(requireCommercial, this.#provider),
|
|
140
|
+
durationS,
|
|
141
|
+
format: "wav",
|
|
142
|
+
loudnessLufs: -18,
|
|
143
|
+
metadata: { intent, brandSource: brand.sourcePath },
|
|
144
|
+
};
|
|
145
|
+
await appendCapabilityDebug(
|
|
146
|
+
"audio-pipeline",
|
|
147
|
+
{ type: "generate", asset },
|
|
148
|
+
{ root: this.#root },
|
|
149
|
+
);
|
|
150
|
+
return asset;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async doctor(): Promise<AudioHealth[]> {
|
|
154
|
+
return [
|
|
155
|
+
{ provider: "tone-local", ok: true },
|
|
156
|
+
{
|
|
157
|
+
provider: "local-model",
|
|
158
|
+
ok: Boolean(process.env.AUDIO_LOCAL_MODEL_PATH),
|
|
159
|
+
suggestion: "Set AUDIO_LOCAL_MODEL_PATH to enable local model generation.",
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
provider: "remote",
|
|
163
|
+
ok: Boolean(process.env.AUDIO_REMOTE_API_KEY),
|
|
164
|
+
suggestion: "Set AUDIO_REMOTE_API_KEY to enable remote audio fallback.",
|
|
165
|
+
},
|
|
166
|
+
];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async license(asset: Pick<AudioAsset, "license">): Promise<LicenseMetadata> {
|
|
170
|
+
return asset.license;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async loudness(asset: Pick<AudioAsset, "loudnessLufs">): Promise<{ lufs: number }> {
|
|
174
|
+
return { lufs: asset.loudnessLufs };
|
|
175
|
+
}
|
|
176
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"target": "esnext",
|
|
7
|
+
"types": ["node"],
|
|
8
|
+
"incremental": false
|
|
9
|
+
},
|
|
10
|
+
"include": ["src", "examples"],
|
|
11
|
+
"exclude": ["node_modules", "dist"]
|
|
12
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineConfig } from "tsup";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ["src/index.ts", "src/cli.ts"],
|
|
5
|
+
format: ["esm"],
|
|
6
|
+
dts: true,
|
|
7
|
+
sourcemap: true,
|
|
8
|
+
clean: true,
|
|
9
|
+
target: "es2022",
|
|
10
|
+
external: ["@nebutra/capability-kit/debug", "@nebutra/errors", "@nebutra/generation-context"],
|
|
11
|
+
});
|