@zukhruf/coprocess 0.0.0-stage → 0.3.42
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 +21 -0
- package/README.md +48 -2
- package/dist/coprocess.d.ts +15 -0
- package/dist/coprocess.d.ts.map +1 -0
- package/dist/coprocess.js +85 -0
- package/dist/coprocess.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ezzabuzaid
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zukhruf/coprocess
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A coprocess is a long-lived child process that the parent talks to over its stdin and stdout, as with `coproc` in bash and `|&` in ksh. A native helper that keeps a model loaded, or a command-line agent that reads one message at a time, is a coprocess. `Coprocess` starts the process, sends and receives values, tells you when the process exits, and stops it with SIGTERM and then SIGKILL. A value is JSON on one line; that is the only framing so far.
|
|
4
|
+
|
|
5
|
+
The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
|
|
6
|
+
|
|
7
|
+
## Send a request and receive the answer
|
|
8
|
+
|
|
9
|
+
A helper answers each request with one line. `request` sends a value and receives the next one.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { Coprocess } from '@zukhruf/coprocess';
|
|
13
|
+
|
|
14
|
+
const ocr = new Coprocess('/path/to/vision-worker', [], {
|
|
15
|
+
name: 'Vision worker',
|
|
16
|
+
});
|
|
17
|
+
const answer = await ocr.request({ path: '/tmp/receipt.png' });
|
|
18
|
+
await ocr.close();
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The process gives `unknown`. Parse it with your own schema, for example with zod.
|
|
22
|
+
|
|
23
|
+
## Receive many values for one message
|
|
24
|
+
|
|
25
|
+
An agent can answer one message with many lines, and a last line that says the work is done. Use `send` once and `receive` until that line comes.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
claude.send({ type: 'user', message: { role: 'user', content: prompt } });
|
|
29
|
+
let event = await claude.receive();
|
|
30
|
+
while (!isResult(event)) event = await claude.receive();
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Know when the process exits
|
|
34
|
+
|
|
35
|
+
`exited()` settles once, when the process ends, with the error that each later `receive` gets. A pool can listen to it and replace the worker at once.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
void worker.exited().then(() => pool.replace(worker));
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The error names the process, the exit code or signal, and the last 16 KiB of its stderr, for example `Vision worker exited (SIGKILL): <stderr>`. It does not contain the command line, because arguments can hold prompts or secrets.
|
|
42
|
+
|
|
43
|
+
## Stop the process
|
|
44
|
+
|
|
45
|
+
`kill()` sends SIGTERM. If the process is still alive after five seconds, it gets SIGKILL. A native helper without a signal handler stops at once; a process that saves its state first gets the time to do it. `close()` ends stdin, which asks the process to finish, and waits for the exit.
|
|
46
|
+
|
|
47
|
+
## Decisions
|
|
48
|
+
|
|
49
|
+
- [ADR 0001](./docs/adr/0001-the-exit-is-an-event-and-the-package-uses-execa.md): the exit is an event, and the package uses execa.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Options } from 'execa';
|
|
2
|
+
export interface CoprocessOptions extends Pick<Options, 'cwd' | 'env'> {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class Coprocess {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(file: string, args: readonly string[], options: CoprocessOptions);
|
|
8
|
+
exited(): Promise<Error>;
|
|
9
|
+
send(value: unknown): void;
|
|
10
|
+
receive(): Promise<unknown>;
|
|
11
|
+
request(value: unknown): Promise<unknown>;
|
|
12
|
+
kill(): void;
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=coprocess.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coprocess.d.ts","sourceRoot":"","sources":["../src/coprocess.ts"],"names":[],"mappings":"AAEA,OAAO,EAAmB,KAAK,OAAO,EAAS,MAAM,OAAO,CAAC;AAS7D,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,OAAO,EAAE,KAAK,GAAG,KAAK,CAAC;IAEpE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAOD,qBAAa,SAAS;;gBASlB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,OAAO,EAAE,gBAAgB;IAgC3B,MAAM,IAAI,OAAO,CAAC,KAAK,CAAC;IAIxB,IAAI,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAMpB,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC;IAkBjC,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAKzC,IAAI,IAAI,IAAI;IAKN,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAU7B"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
import { Latch } from '@zukhruf/async';
|
|
4
|
+
// The last part of stderr an exit reports: enough for a native crash, and
|
|
5
|
+
// bounded, because a process can write to stderr for hours. execa's maxBuffer
|
|
6
|
+
// would end the process instead.
|
|
7
|
+
const tail = 16_384;
|
|
8
|
+
// A long-lived child process that takes JSON values on stdin and gives them
|
|
9
|
+
// on stdout, one value per line. What a value means is the caller's
|
|
10
|
+
// protocol: one answer per request, or many lines until one says the work is
|
|
11
|
+
// done. The exit is an event: exited settles when the process ends, and every
|
|
12
|
+
// receive after it fails with the stderr of the process.
|
|
13
|
+
export class Coprocess {
|
|
14
|
+
#name;
|
|
15
|
+
#stdin;
|
|
16
|
+
#lines;
|
|
17
|
+
#kill;
|
|
18
|
+
#exit = new Latch();
|
|
19
|
+
#stderr = '';
|
|
20
|
+
constructor(file, args, options) {
|
|
21
|
+
const { name, ...spawning } = options;
|
|
22
|
+
this.#name = name;
|
|
23
|
+
// execa's kill sends SIGTERM and, if the process is still alive after
|
|
24
|
+
// five seconds, SIGKILL, as Kubernetes and systemd stop a process: one
|
|
25
|
+
// that shuts down on its own, such as one that saves its state, gets the
|
|
26
|
+
// time.
|
|
27
|
+
const subprocess = execa(file, args, { ...spawning, buffer: false });
|
|
28
|
+
this.#stdin = subprocess.stdin;
|
|
29
|
+
this.#kill = () => subprocess.kill();
|
|
30
|
+
this.#lines = createInterface({ input: subprocess.stdout })[Symbol.asyncIterator]();
|
|
31
|
+
subprocess.stderr.on('data', (chunk) => {
|
|
32
|
+
this.#stderr = (this.#stderr + chunk.toString()).slice(-tail);
|
|
33
|
+
});
|
|
34
|
+
// A process that failed to start or exited closes stdin under a write;
|
|
35
|
+
// the exit, not the write, tells the caller what happened.
|
|
36
|
+
subprocess.stdin.on('error', () => undefined);
|
|
37
|
+
subprocess.then(({ exitCode }) => this.#exit.open(this.#exited(exitCode)), (error) => this.#exit.open(error.exitCode === undefined && error.signal === undefined
|
|
38
|
+
? new Error(`${name} could not start`, { cause: error })
|
|
39
|
+
: this.#exited(error.signal ?? error.exitCode)));
|
|
40
|
+
}
|
|
41
|
+
// Settles with the error that receives get, once the process ended.
|
|
42
|
+
exited() {
|
|
43
|
+
return this.#exit.wait();
|
|
44
|
+
}
|
|
45
|
+
send(value) {
|
|
46
|
+
this.#stdin.write(`${JSON.stringify(value)}\n`);
|
|
47
|
+
}
|
|
48
|
+
// The next value the process gives. Rejects when the process exits first,
|
|
49
|
+
// or when the line is not JSON.
|
|
50
|
+
async receive() {
|
|
51
|
+
const next = await Promise.race([
|
|
52
|
+
this.#lines.next(),
|
|
53
|
+
this.#exit.wait().then((error) => {
|
|
54
|
+
throw error;
|
|
55
|
+
}),
|
|
56
|
+
]);
|
|
57
|
+
if (next.done)
|
|
58
|
+
throw await this.#exit.wait();
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(next.value);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
throw new Error(`${this.#name} gave a line that is not JSON`, {
|
|
64
|
+
cause: error,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Sends a value and receives the one that answers it.
|
|
69
|
+
request(value) {
|
|
70
|
+
this.send(value);
|
|
71
|
+
return this.receive();
|
|
72
|
+
}
|
|
73
|
+
kill() {
|
|
74
|
+
this.#kill();
|
|
75
|
+
}
|
|
76
|
+
// Ends stdin, which asks the process to finish, and waits for its exit.
|
|
77
|
+
async close() {
|
|
78
|
+
this.#stdin.end();
|
|
79
|
+
await this.#exit.wait();
|
|
80
|
+
}
|
|
81
|
+
#exited(status) {
|
|
82
|
+
return new Error(`${this.#name} exited (${status}): ${this.#stderr.trim()}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=coprocess.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coprocess.js","sourceRoot":"","sources":["../src/coprocess.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,EAAiC,KAAK,EAAE,MAAM,OAAO,CAAC;AAE7D,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC,0EAA0E;AAC1E,8EAA8E;AAC9E,iCAAiC;AACjC,MAAM,IAAI,GAAG,MAAM,CAAC;AAOpB,4EAA4E;AAC5E,oEAAoE;AACpE,6EAA6E;AAC7E,8EAA8E;AAC9E,yDAAyD;AACzD,MAAM,OAAO,SAAS;IACX,KAAK,CAAS;IACd,MAAM,CAAwB;IAC9B,MAAM,CAAwB;IAC9B,KAAK,CAAa;IAClB,KAAK,GAAG,IAAI,KAAK,EAAS,CAAC;IACpC,OAAO,GAAG,EAAE,CAAC;IAEb,YACE,IAAY,EACZ,IAAuB,EACvB,OAAyB;QAEzB,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAC;QACtC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,sEAAsE;QACtE,uEAAuE;QACvE,yEAAyE;QACzE,QAAQ;QACR,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,GAAG,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QACrE,IAAI,CAAC,MAAM,GAAG,UAAU,CA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;IACJ,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAyB,MAAM,gBAAgB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/coprocess",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.42",
|
|
4
|
+
"description": "A coprocess: a long-lived child process that the parent talks to over stdin and stdout, with JSON values on lines, and stops with SIGTERM and then SIGKILL after a grace period.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/JanuaryLabs/zukhruf",
|
|
9
|
+
"directory": "packages/coprocess"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/JanuaryLabs/zukhruf/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/coprocess#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"coprocess",
|
|
17
|
+
"child-process",
|
|
18
|
+
"subprocess",
|
|
19
|
+
"json-lines",
|
|
20
|
+
"ndjson",
|
|
21
|
+
"jsonl",
|
|
22
|
+
"stdio",
|
|
23
|
+
"worker",
|
|
24
|
+
"sigterm"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
"./package.json": "./package.json",
|
|
29
|
+
".": {
|
|
30
|
+
"@zukhruf/source": "./src/index.ts",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.js",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"!dist/spec/**",
|
|
39
|
+
"!**/*.tsbuildinfo",
|
|
40
|
+
"!**/*.test.*"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@zukhruf/async": "^0.3.42",
|
|
47
|
+
"execa": "10.2.1"
|
|
48
|
+
}
|
|
49
|
+
}
|