@acpfx/play-file 0.2.2 → 0.2.3

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/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2024-2026 acpfx contributors
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @acpfx/play-file
2
+
3
+ Writes received audio chunks to a WAV file. Useful for recording pipeline output during testing.
4
+
5
+ ## Usage
6
+
7
+ This package is a pipeline node for [@acpfx/cli](../orchestrator/README.md). See the CLI package for installation and usage.
8
+
9
+ ## Manifest
10
+
11
+ - **Consumes:** `audio.chunk`, `control.interrupt`, `lifecycle.done`
12
+ - **Emits:** `lifecycle.ready`, `lifecycle.done`
13
+
14
+ ## Settings
15
+
16
+ | Name | Type | Default | Description |
17
+ |------|------|---------|-------------|
18
+ | `path` | string | **(required)** | Output WAV file path |
19
+
20
+ ## Pipeline Example
21
+
22
+ ```yaml
23
+ nodes:
24
+ output:
25
+ use: "@acpfx/play-file"
26
+ settings: { path: ./output.wav }
27
+ ```
28
+
29
+ ## License
30
+
31
+ ISC
package/dist/index.js ADDED
@@ -0,0 +1,231 @@
1
+ // src/index.ts
2
+ import { createWriteStream } from "node:fs";
3
+ import { open } from "node:fs/promises";
4
+ import { resolve } from "node:path";
5
+
6
+ // ../node-sdk/src/index.ts
7
+ import { createInterface } from "node:readline";
8
+
9
+ // ../core/src/config.ts
10
+ import { parse as parseYaml } from "yaml";
11
+
12
+ // ../core/src/manifest.ts
13
+ import { readFileSync } from "node:fs";
14
+ import { join, dirname } from "node:path";
15
+ import { z as z2 } from "zod";
16
+
17
+ // ../core/src/acpfx-flags.ts
18
+ import { z } from "zod";
19
+ var SetupCheckResponseSchema = z.object({
20
+ needed: z.boolean(),
21
+ description: z.string().optional()
22
+ });
23
+ var SetupProgressSchema = z.discriminatedUnion("type", [
24
+ z.object({
25
+ type: z.literal("progress"),
26
+ message: z.string(),
27
+ pct: z.number().optional()
28
+ }),
29
+ z.object({ type: z.literal("complete"), message: z.string() }),
30
+ z.object({ type: z.literal("error"), message: z.string() })
31
+ ]);
32
+ var UnsupportedFlagResponseSchema = z.object({
33
+ unsupported: z.boolean(),
34
+ flag: z.string()
35
+ });
36
+
37
+ // ../core/src/manifest.ts
38
+ var ArgumentTypeSchema = z2.enum(["string", "number", "boolean"]);
39
+ var ManifestArgumentSchema = z2.object({
40
+ type: ArgumentTypeSchema,
41
+ default: z2.unknown().optional(),
42
+ description: z2.string().optional(),
43
+ required: z2.boolean().optional(),
44
+ enum: z2.array(z2.unknown()).optional()
45
+ });
46
+ var ManifestEnvFieldSchema = z2.object({
47
+ required: z2.boolean().optional(),
48
+ description: z2.string().optional()
49
+ });
50
+ var NodeManifestSchema = z2.object({
51
+ name: z2.string(),
52
+ description: z2.string().optional(),
53
+ consumes: z2.array(z2.string()),
54
+ emits: z2.array(z2.string()),
55
+ arguments: z2.record(z2.string(), ManifestArgumentSchema).optional(),
56
+ additional_arguments: z2.boolean().optional(),
57
+ env: z2.record(z2.string(), ManifestEnvFieldSchema).optional()
58
+ });
59
+ function handleAcpfxFlags(manifestPath) {
60
+ const acpfxFlag = process.argv.find((a) => a.startsWith("--acpfx-"));
61
+ const legacyManifest = process.argv.includes("--manifest");
62
+ if (!acpfxFlag && !legacyManifest) return;
63
+ const flag = acpfxFlag ?? "--acpfx-manifest";
64
+ switch (flag) {
65
+ case "--acpfx-manifest":
66
+ printManifest(manifestPath);
67
+ break;
68
+ case "--acpfx-setup-check":
69
+ process.stdout.write(JSON.stringify({ needed: false }) + "\n");
70
+ process.exit(0);
71
+ break;
72
+ default:
73
+ process.stdout.write(
74
+ JSON.stringify({ unsupported: true, flag }) + "\n"
75
+ );
76
+ process.exit(0);
77
+ }
78
+ }
79
+ function handleManifestFlag(manifestPath) {
80
+ handleAcpfxFlags(manifestPath);
81
+ }
82
+ function printManifest(manifestPath) {
83
+ if (!manifestPath) {
84
+ const script = process.argv[1];
85
+ const scriptDir = dirname(script);
86
+ const scriptBase = script.replace(/\.[^.]+$/, "");
87
+ const colocated = `${scriptBase}.manifest.json`;
88
+ try {
89
+ readFileSync(colocated);
90
+ manifestPath = colocated;
91
+ } catch {
92
+ manifestPath = join(scriptDir, "manifest.json");
93
+ }
94
+ }
95
+ try {
96
+ const content = readFileSync(manifestPath, "utf8");
97
+ process.stdout.write(content.trim() + "\n");
98
+ process.exit(0);
99
+ } catch (err) {
100
+ process.stderr.write(`Failed to read manifest: ${err}
101
+ `);
102
+ process.exit(1);
103
+ }
104
+ }
105
+
106
+ // ../node-sdk/src/index.ts
107
+ var NODE_NAME = process.env.ACPFX_NODE_NAME ?? "unknown";
108
+ function emit(event) {
109
+ process.stdout.write(JSON.stringify(event) + "\n");
110
+ }
111
+ function log(level, message) {
112
+ emit({ type: "log", level, component: NODE_NAME, message });
113
+ }
114
+ log.info = (message) => log("info", message);
115
+ log.warn = (message) => log("warn", message);
116
+ log.error = (message) => log("error", message);
117
+ log.debug = (message) => log("debug", message);
118
+ function onEvent(handler) {
119
+ const rl2 = createInterface({ input: process.stdin });
120
+ rl2.on("line", (line) => {
121
+ if (!line.trim()) return;
122
+ try {
123
+ const event = JSON.parse(line);
124
+ handler(event);
125
+ } catch {
126
+ }
127
+ });
128
+ return rl2;
129
+ }
130
+
131
+ // src/index.ts
132
+ handleManifestFlag();
133
+ var settings = JSON.parse(
134
+ process.env.ACPFX_SETTINGS || "{}"
135
+ );
136
+ if (!settings.path) {
137
+ log.error("settings.path is required");
138
+ process.exit(1);
139
+ }
140
+ var filePath = resolve(settings.path);
141
+ var stream = null;
142
+ var bytesWritten = 0;
143
+ var sampleRate = 16e3;
144
+ var channels = 1;
145
+ var started = false;
146
+ function createWavHeader(dataSize, sr, ch) {
147
+ const bitsPerSample = 16;
148
+ const byteRate = sr * ch * bitsPerSample / 8;
149
+ const blockAlign = ch * bitsPerSample / 8;
150
+ const header = Buffer.alloc(44);
151
+ let off = 0;
152
+ header.write("RIFF", off);
153
+ off += 4;
154
+ header.writeUInt32LE(dataSize + 36, off);
155
+ off += 4;
156
+ header.write("WAVE", off);
157
+ off += 4;
158
+ header.write("fmt ", off);
159
+ off += 4;
160
+ header.writeUInt32LE(16, off);
161
+ off += 4;
162
+ header.writeUInt16LE(1, off);
163
+ off += 2;
164
+ header.writeUInt16LE(ch, off);
165
+ off += 2;
166
+ header.writeUInt32LE(sr, off);
167
+ off += 4;
168
+ header.writeUInt32LE(byteRate, off);
169
+ off += 4;
170
+ header.writeUInt16LE(blockAlign, off);
171
+ off += 2;
172
+ header.writeUInt16LE(bitsPerSample, off);
173
+ off += 2;
174
+ header.write("data", off);
175
+ off += 4;
176
+ header.writeUInt32LE(dataSize, off);
177
+ return header;
178
+ }
179
+ function startWriting() {
180
+ if (started) return;
181
+ started = true;
182
+ stream = createWriteStream(filePath);
183
+ bytesWritten = 0;
184
+ stream.write(Buffer.alloc(44));
185
+ }
186
+ async function finalize() {
187
+ if (!stream) return;
188
+ await new Promise((resolve2, reject) => {
189
+ stream.end(() => resolve2());
190
+ stream.on("error", reject);
191
+ });
192
+ const header = createWavHeader(bytesWritten, sampleRate, channels);
193
+ const fd = await open(filePath, "r+");
194
+ await fd.write(header, 0, header.length, 0);
195
+ await fd.close();
196
+ stream = null;
197
+ }
198
+ emit({ type: "lifecycle.ready", component: "play-file" });
199
+ var rl = onEvent((event) => {
200
+ if (event.type === "audio.chunk") {
201
+ startWriting();
202
+ if (bytesWritten === 0) {
203
+ sampleRate = event.sampleRate ?? 16e3;
204
+ channels = event.channels ?? 1;
205
+ }
206
+ const pcm = Buffer.from(event.data, "base64");
207
+ if (stream?.writable) {
208
+ stream.write(pcm);
209
+ bytesWritten += pcm.length;
210
+ }
211
+ } else if (event.type === "control.interrupt") {
212
+ finalize().then(() => {
213
+ emit({ type: "lifecycle.done", component: "play-file" });
214
+ process.exit(0);
215
+ });
216
+ } else if (event.type === "lifecycle.done") {
217
+ finalize().then(() => {
218
+ emit({ type: "lifecycle.done", component: "play-file" });
219
+ process.exit(0);
220
+ });
221
+ }
222
+ });
223
+ rl.on("close", () => {
224
+ finalize().then(() => {
225
+ emit({ type: "lifecycle.done", component: "play-file" });
226
+ process.exit(0);
227
+ });
228
+ });
229
+ process.on("SIGTERM", () => {
230
+ finalize().then(() => process.exit(0));
231
+ });
@@ -0,0 +1 @@
1
+ {"name":"play-file","description":"Writes audio chunks to a WAV file","consumes":["audio.chunk","control.interrupt","lifecycle.done"],"emits":["lifecycle.ready","lifecycle.done"],"arguments":{"path":{"type":"string","required":true,"description":"Output WAV file path"}}}
package/package.json CHANGED
@@ -1,16 +1,20 @@
1
1
  {
2
2
  "name": "@acpfx/play-file",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "acpfx-play-file": "./dist/index.js"
7
7
  },
8
8
  "main": "./dist/index.js",
9
+ "files": [
10
+ "dist",
11
+ "manifest.yaml"
12
+ ],
9
13
  "dependencies": {
10
14
  "@acpfx/core": "0.4.0",
11
15
  "@acpfx/node-sdk": "0.3.0"
12
16
  },
13
17
  "scripts": {
14
- "build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=dist/index.js --packages=external"
18
+ "build": "esbuild src/index.ts --bundle --platform=node --format=esm --outfile=dist/index.js --packages=external && node ../../scripts/copy-manifest.js"
15
19
  }
16
20
  }
package/CHANGELOG.md DELETED
@@ -1,38 +0,0 @@
1
- # @acpfx/play-file
2
-
3
- ## 0.2.2
4
-
5
- ### Patch Changes
6
-
7
- - Updated dependencies [0e6838e]
8
- - @acpfx/core@0.4.0
9
- - @acpfx/node-sdk@0.3.0
10
-
11
- ## 0.2.1
12
-
13
- ### Patch Changes
14
-
15
- - Updated dependencies [79c6694]
16
- - Updated dependencies [a0320a1]
17
- - @acpfx/core@0.3.0
18
- - @acpfx/node-sdk@0.2.1
19
-
20
- ## 0.2.0
21
-
22
- ### Minor Changes
23
-
24
- - d757640: Initial release: type-safe contracts, Rust orchestrator, manifest-driven event filtering
25
-
26
- - Rust schema crate as canonical event type source of truth with codegen to TypeScript + Zod
27
- - Node manifests (manifest.yaml) declaring consumes/emits contracts
28
- - Orchestrator event filtering: nodes only receive declared events
29
- - Rust orchestrator with ratatui TUI (--ui flag)
30
- - node-sdk with structured logging helpers
31
- - CI/CD with GitHub Actions and changesets
32
- - Platform-specific npm packages for Rust binaries (esbuild-style distribution)
33
-
34
- ### Patch Changes
35
-
36
- - Updated dependencies [d757640]
37
- - @acpfx/core@0.2.0
38
- - @acpfx/node-sdk@0.2.0
package/src/index.ts DELETED
@@ -1,125 +0,0 @@
1
- /**
2
- * play-file node — reads audio.chunk events from stdin and writes a WAV file.
3
- * Handles control.interrupt by stopping and finalizing the WAV header.
4
- *
5
- * Settings (via ACPFX_SETTINGS):
6
- * path: string — output WAV file path
7
- */
8
-
9
- import { createWriteStream, type WriteStream } from "node:fs";
10
- import { open } from "node:fs/promises";
11
- import { resolve } from "node:path";
12
- import { emit, log, onEvent, handleManifestFlag } from "@acpfx/node-sdk";
13
-
14
- handleManifestFlag();
15
-
16
- type Settings = {
17
- path: string;
18
- };
19
-
20
- const settings: Settings = JSON.parse(
21
- process.env.ACPFX_SETTINGS || "{}",
22
- );
23
-
24
- if (!settings.path) {
25
- log.error("settings.path is required");
26
- process.exit(1);
27
- }
28
-
29
- const filePath = resolve(settings.path);
30
- let stream: WriteStream | null = null;
31
- let bytesWritten = 0;
32
- let sampleRate = 16000;
33
- let channels = 1;
34
- let started = false;
35
-
36
- function createWavHeader(dataSize: number, sr: number, ch: number): Buffer {
37
- const bitsPerSample = 16;
38
- const byteRate = (sr * ch * bitsPerSample) / 8;
39
- const blockAlign = (ch * bitsPerSample) / 8;
40
- const header = Buffer.alloc(44);
41
- let off = 0;
42
-
43
- header.write("RIFF", off); off += 4;
44
- header.writeUInt32LE(dataSize + 36, off); off += 4;
45
- header.write("WAVE", off); off += 4;
46
- header.write("fmt ", off); off += 4;
47
- header.writeUInt32LE(16, off); off += 4;
48
- header.writeUInt16LE(1, off); off += 2;
49
- header.writeUInt16LE(ch, off); off += 2;
50
- header.writeUInt32LE(sr, off); off += 4;
51
- header.writeUInt32LE(byteRate, off); off += 4;
52
- header.writeUInt16LE(blockAlign, off); off += 2;
53
- header.writeUInt16LE(bitsPerSample, off); off += 2;
54
- header.write("data", off); off += 4;
55
- header.writeUInt32LE(dataSize, off);
56
-
57
- return header;
58
- }
59
-
60
- function startWriting(): void {
61
- if (started) return;
62
- started = true;
63
- stream = createWriteStream(filePath);
64
- bytesWritten = 0;
65
- // Write placeholder WAV header
66
- stream.write(Buffer.alloc(44));
67
- }
68
-
69
- async function finalize(): Promise<void> {
70
- if (!stream) return;
71
-
72
- await new Promise<void>((resolve, reject) => {
73
- stream!.end(() => resolve());
74
- stream!.on("error", reject);
75
- });
76
-
77
- // Update WAV header with correct sizes
78
- const header = createWavHeader(bytesWritten, sampleRate, channels);
79
- const fd = await open(filePath, "r+");
80
- await fd.write(header, 0, header.length, 0);
81
- await fd.close();
82
-
83
- stream = null;
84
- }
85
-
86
- // Emit lifecycle.ready
87
- emit({ type: "lifecycle.ready", component: "play-file" });
88
-
89
- const rl = onEvent((event) => {
90
- if (event.type === "audio.chunk") {
91
- startWriting();
92
- // Capture format from first chunk
93
- if (bytesWritten === 0) {
94
- sampleRate = (event.sampleRate as number) ?? 16000;
95
- channels = (event.channels as number) ?? 1;
96
- }
97
- const pcm = Buffer.from(event.data as string, "base64");
98
- if (stream?.writable) {
99
- stream.write(pcm);
100
- bytesWritten += pcm.length;
101
- }
102
- } else if (event.type === "control.interrupt") {
103
- finalize().then(() => {
104
- emit({ type: "lifecycle.done", component: "play-file" });
105
- process.exit(0);
106
- });
107
- } else if (event.type === "lifecycle.done") {
108
- finalize().then(() => {
109
- emit({ type: "lifecycle.done", component: "play-file" });
110
- process.exit(0);
111
- });
112
- }
113
- });
114
-
115
- rl.on("close", () => {
116
- // stdin EOF — finalize and exit
117
- finalize().then(() => {
118
- emit({ type: "lifecycle.done", component: "play-file" });
119
- process.exit(0);
120
- });
121
- });
122
-
123
- process.on("SIGTERM", () => {
124
- finalize().then(() => process.exit(0));
125
- });