feat: Complete zCode CLI X with Telegram bot integration

- Add full Telegram bot functionality with Z.AI API integration
- Implement 4 tools: Bash, FileEdit, WebSearch, Git
- Add 3 agents: Code Reviewer, Architect, DevOps Engineer
- Add 6 skills for common coding tasks
- Add systemd service file for 24/7 operation
- Add nginx configuration for HTTPS webhook
- Add comprehensive documentation
- Implement WebSocket server for real-time updates
- Add logging system with Winston
- Add environment validation

🤖 zCode CLI X - Agentic coder with Z.AI + Telegram integration
This commit is contained in:
admin
2026-05-05 09:01:26 +00:00
Unverified
parent 4a7035dd92
commit 875c7f9b91
24688 changed files with 3224957 additions and 221 deletions

View File

@@ -0,0 +1 @@
/home/uroma2/zcode-cli-x/~/.npm-cache/@sindresorhus/merge-streams@4.0.0@@@1

View File

@@ -0,0 +1,44 @@
import {type Readable} from 'node:stream';
/**
Merges an array of [readable streams](https://nodejs.org/api/stream.html#readable-streams) and returns a new readable stream that emits data from the individual streams as it arrives.
If you provide an empty array, the stream remains open but can be [manually ended](https://nodejs.org/api/stream.html#writableendchunk-encoding-callback).
@example
```
import mergeStreams from '@sindresorhus/merge-streams';
const stream = mergeStreams([streamA, streamB]);
for await (const chunk of stream) {
console.log(chunk);
//=> 'A1'
//=> 'B1'
//=> 'A2'
//=> 'B2'
}
```
*/
export default function mergeStreams(streams: Readable[]): MergedStream;
/**
A single stream combining the output of multiple streams.
*/
export class MergedStream extends Readable {
/**
Pipe a new readable stream.
Throws if `MergedStream` has already ended.
*/
add(stream: Readable): void;
/**
Unpipe a stream previously added using either `mergeStreams(streams)` or `MergedStream.add(stream)`.
Returns `false` if the stream was not previously added, or if it was already removed by `MergedStream.remove(stream)`.
The removed stream is not automatically ended.
*/
remove(stream: Readable): Promise<boolean>;
}

View File

@@ -0,0 +1,265 @@
import {on, once} from 'node:events';
import {PassThrough as PassThroughStream, getDefaultHighWaterMark} from 'node:stream';
import {finished} from 'node:stream/promises';
export default function mergeStreams(streams) {
if (!Array.isArray(streams)) {
throw new TypeError(`Expected an array, got \`${typeof streams}\`.`);
}
for (const stream of streams) {
validateStream(stream);
}
const objectMode = streams.some(({readableObjectMode}) => readableObjectMode);
const highWaterMark = getHighWaterMark(streams, objectMode);
const passThroughStream = new MergedStream({
objectMode,
writableHighWaterMark: highWaterMark,
readableHighWaterMark: highWaterMark,
});
for (const stream of streams) {
passThroughStream.add(stream);
}
return passThroughStream;
}
const getHighWaterMark = (streams, objectMode) => {
if (streams.length === 0) {
return getDefaultHighWaterMark(objectMode);
}
const highWaterMarks = streams
.filter(({readableObjectMode}) => readableObjectMode === objectMode)
.map(({readableHighWaterMark}) => readableHighWaterMark);
return Math.max(...highWaterMarks);
};
class MergedStream extends PassThroughStream {
#streams = new Set([]);
#ended = new Set([]);
#aborted = new Set([]);
#onFinished;
#unpipeEvent = Symbol('unpipe');
#streamPromises = new WeakMap();
add(stream) {
validateStream(stream);
if (this.#streams.has(stream)) {
return;
}
this.#streams.add(stream);
this.#onFinished ??= onMergedStreamFinished(this, this.#streams, this.#unpipeEvent);
const streamPromise = endWhenStreamsDone({
passThroughStream: this,
stream,
streams: this.#streams,
ended: this.#ended,
aborted: this.#aborted,
onFinished: this.#onFinished,
unpipeEvent: this.#unpipeEvent,
});
this.#streamPromises.set(stream, streamPromise);
stream.pipe(this, {end: false});
}
async remove(stream) {
validateStream(stream);
if (!this.#streams.has(stream)) {
return false;
}
const streamPromise = this.#streamPromises.get(stream);
if (streamPromise === undefined) {
return false;
}
this.#streamPromises.delete(stream);
stream.unpipe(this);
await streamPromise;
return true;
}
}
const onMergedStreamFinished = async (passThroughStream, streams, unpipeEvent) => {
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_COUNT);
const controller = new AbortController();
try {
await Promise.race([
onMergedStreamEnd(passThroughStream, controller),
onInputStreamsUnpipe(passThroughStream, streams, unpipeEvent, controller),
]);
} finally {
controller.abort();
updateMaxListeners(passThroughStream, -PASSTHROUGH_LISTENERS_COUNT);
}
};
const onMergedStreamEnd = async (passThroughStream, {signal}) => {
try {
await finished(passThroughStream, {signal, cleanup: true});
} catch (error) {
errorOrAbortStream(passThroughStream, error);
throw error;
}
};
const onInputStreamsUnpipe = async (passThroughStream, streams, unpipeEvent, {signal}) => {
for await (const [unpipedStream] of on(passThroughStream, 'unpipe', {signal})) {
if (streams.has(unpipedStream)) {
unpipedStream.emit(unpipeEvent);
}
}
};
const validateStream = stream => {
if (typeof stream?.pipe !== 'function') {
throw new TypeError(`Expected a readable stream, got: \`${typeof stream}\`.`);
}
};
const endWhenStreamsDone = async ({passThroughStream, stream, streams, ended, aborted, onFinished, unpipeEvent}) => {
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_PER_STREAM);
const controller = new AbortController();
try {
await Promise.race([
afterMergedStreamFinished(onFinished, stream, controller),
onInputStreamEnd({
passThroughStream,
stream,
streams,
ended,
aborted,
controller,
}),
onInputStreamUnpipe({
stream,
streams,
ended,
aborted,
unpipeEvent,
controller,
}),
]);
} finally {
controller.abort();
updateMaxListeners(passThroughStream, -PASSTHROUGH_LISTENERS_PER_STREAM);
}
if (streams.size > 0 && streams.size === ended.size + aborted.size) {
if (ended.size === 0 && aborted.size > 0) {
abortStream(passThroughStream);
} else {
endStream(passThroughStream);
}
}
};
const afterMergedStreamFinished = async (onFinished, stream, {signal}) => {
try {
await onFinished;
if (!signal.aborted) {
abortStream(stream);
}
} catch (error) {
if (!signal.aborted) {
errorOrAbortStream(stream, error);
}
}
};
const onInputStreamEnd = async ({passThroughStream, stream, streams, ended, aborted, controller: {signal}}) => {
try {
await finished(stream, {
signal,
cleanup: true,
readable: true,
writable: false,
});
if (streams.has(stream)) {
ended.add(stream);
}
} catch (error) {
if (signal.aborted || !streams.has(stream)) {
return;
}
if (isAbortError(error)) {
aborted.add(stream);
} else {
errorStream(passThroughStream, error);
}
}
};
const onInputStreamUnpipe = async ({stream, streams, ended, aborted, unpipeEvent, controller: {signal}}) => {
await once(stream, unpipeEvent, {signal});
if (!stream.readable) {
return once(signal, 'abort', {signal});
}
streams.delete(stream);
ended.delete(stream);
aborted.delete(stream);
};
const endStream = stream => {
if (stream.writable) {
stream.end();
}
};
const errorOrAbortStream = (stream, error) => {
if (isAbortError(error)) {
abortStream(stream);
} else {
errorStream(stream, error);
}
};
// This is the error thrown by `finished()` on `stream.destroy()`
const isAbortError = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE';
const abortStream = stream => {
if (stream.readable || stream.writable) {
stream.destroy();
}
};
// `stream.destroy(error)` crashes the process with `uncaughtException` if no `error` event listener exists on `stream`.
// We take care of error handling on user behalf, so we do not want this to happen.
const errorStream = (stream, error) => {
if (!stream.destroyed) {
stream.once('error', noop);
stream.destroy(error);
}
};
const noop = () => {};
const updateMaxListeners = (passThroughStream, increment) => {
const maxListeners = passThroughStream.getMaxListeners();
if (maxListeners !== 0 && maxListeners !== Number.POSITIVE_INFINITY) {
passThroughStream.setMaxListeners(maxListeners + increment);
}
};
// Number of times `passThroughStream.on()` is called regardless of streams:
// - once due to `finished(passThroughStream)`
// - once due to `on(passThroughStream)`
const PASSTHROUGH_LISTENERS_COUNT = 2;
// Number of times `passThroughStream.on()` is called per stream:
// - once due to `stream.pipe(passThroughStream)`
const PASSTHROUGH_LISTENERS_PER_STREAM = 1;

View File

@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,49 @@
{
"name": "@sindresorhus/merge-streams",
"version": "4.0.0",
"description": "Merge multiple streams into a unified stream",
"license": "MIT",
"repository": "sindresorhus/merge-streams",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=18"
},
"scripts": {
"test": "xo && c8 ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"merge",
"stream",
"streams",
"readable",
"passthrough",
"interleave",
"interleaved",
"unify",
"unified"
],
"devDependencies": {
"@types/node": "^20.8.9",
"ava": "^6.1.0",
"c8": "^9.1.0",
"tempfile": "^5.0.0",
"tsd": "^0.31.0",
"typescript": "^5.2.2",
"xo": "^0.58.0"
}
}

View File

@@ -0,0 +1,53 @@
# merge-streams
> Merge multiple streams into a unified stream
## Install
```sh
npm install @sindresorhus/merge-streams
```
## Usage
```js
import mergeStreams from '@sindresorhus/merge-streams';
const stream = mergeStreams([streamA, streamB]);
for await (const chunk of stream) {
console.log(chunk);
//=> 'A1'
//=> 'B1'
//=> 'A2'
//=> 'B2'
}
```
## API
### `mergeStreams(streams: stream.Readable[]): MergedStream`
Merges an array of [readable streams](https://nodejs.org/api/stream.html#readable-streams) and returns a new readable stream that emits data from the individual streams as it arrives.
If you provide an empty array, the stream remains open but can be [manually ended](https://nodejs.org/api/stream.html#writableendchunk-encoding-callback).
#### `MergedStream`
_Type_: `stream.Readable`
A single stream combining the output of multiple streams.
##### `MergedStream.add(stream: stream.Readable): void`
Pipe a new readable stream.
Throws if `MergedStream` has already ended.
##### `MergedStream.remove(stream: stream.Readable): Promise<boolean>`
Unpipe a stream previously added using either [`mergeStreams(streams)`](#mergestreamsstreams-streamreadable-mergedstream) or [`MergedStream.add(stream)`](#mergedstreamaddstream-streamreadable-void).
Returns `false` if the stream was not previously added, or if it was already removed by `MergedStream.remove(stream)`.
The removed stream is not automatically ended.