mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 12:18:07 +00:00
* bots, nodejs: document chat, connection, member contact and network commands, typed commands in library * bots, nodejs: restore apiConnect, wrap domain once, test all connect responses * nodejs: JSDoc for new methods * docs: plan for bot API commands and typed Node library methods * tests: channel prepared with a domain has a verified SimpleX name * docs: apply review fixes to the plan for bot API commands * docs: apply second review pass to the plan for bot API commands * docs: refer the bot API plan to the rewritten test commit * docs: record decisions in the plan for bot API commands * docs: plan tests of the generated Python command strings * python: test the generated strings of the documented commands * docs: plan for files, /leave and library methods in the MCP server * docs: wait at stop in the MCP plan * docs: library methods and /leave mechanics in the MCP plan * mcp: typed library methods for chat commands * docs: plan the message information command for bots * mcp: /leave CHAT owner command to leave a chat at once * docs: file servers, GIF and video choices decided in the MCP plan * docs: file mechanics in the MCP plan * mcp: files of received messages * bots, nodejs: message information with the file servers for bots * docs: plan the relay approval option of receiving files in Node * mcp: download of received files with owner approval * docs: name the xftp-server test helper in the files plan * nodejs: receive files without approving unknown relays * docs: plan the user servers command for bots * bots, nodejs: user servers and relays by operator for bots * nodejs: document the message information method * docs: plan the file upload progress event for bots * mcp: send files with owner approval, refusing private keys * bots: file upload progress event for bots * mcp: /leave template in the agent control chat menu * docs: sent copies are deleted for contacts too, at sndFileCompleteXFTP * bots: drop the file upload progress event * docs: drop the upload progress event plan * nodejs: document user servers and relay approval of received files * mcp: download cards name unknown file servers after the upload * docs: record the wait for the file description in the files plan * mcp: fixed wait at stop for cancelled cards, without /sql * docs: record decision C4, a fixed wait at stop, in the files plan * ci: test the MCP server package on pull requests * ci: state when the MCP workflow can be dispatched * ci: run the MCP server tests when its libraries change * mcp: log the errors of image previews and resizing * mcp: keep files in transfer in the data folder * mcp: restrict sent files by the real paths of refused folders * mcp: validate the file at the send by its inode and change time * mcp: limit ffmpeg and ffprobe to local files and no input commands * mcp: ask the owner for unknown file servers; read approvals at creation * mcp: name downloaded files by their ID; return paths of downloads only * mcp: end each download with a result for the agent and the card * mcp: show files above their sender's size limit as failed * mcp: refuse empty files, as without file and captions too long to send * mcp: tell the owner about failed uploads and unposted received files * mcp: delete resized copies left by the last run at start * mcp: name downloads and leaving in the help and greeting, align docs * mcp: compute the download path for received files only * mcp: send a /receive file to the owner under its name * mcp: keep files received without a download in the chat's folder * mcp: keep a left chat's files and delete them with /delete CHAT files * mcp: test that sent images are resized by the core * mcp: wait 30 minutes for a download's progress before cancelling it * mcp: end a download as deleted only when its message is not found * mcp: delete the copy of a /receive file whose post fails * mcp: count the quoted content in a message's length, as the core sends it * mcp: name received bytes, unknown-server downloads and left chat files plainly * mcp: test refused folders, file changes and download approvals * docs: align MCP server docs with refused folders, leaves and download waits * mcp: move received files only in full, delete partials only of downloads * mcp: report a received file missing from its folder as failed * mcp: move received files of a group's member chats into its folder * mcp: read a video's preview and duration as an MP4 or QuickTime file * mcp: 0.2.0 on simplex-chat 7.1.0-beta.10.1 * mcp: file folders by chat type, /delete cancels downloads, files reply * docs: MCP server specs and plan for 0.2.0 * mcp: /receive gives the agent a file, /forward sends it to the owner * docs: MCP server specs for /receive and /forward of download cards * docs: MCP plan records the temp folder command as a library exception * mcp: /delete cancels resumed downloads; /forward quote fits the limit * docs: MCP specs and plans for /forward quotes and resumed downloads * docs: MCP specs name the source commit they are checked against * docs: MCP PLAN states the /forward quote and move rules * mcp: a download moves its completed file whatever its size * mcp: /receive N all covers its card and later files, not pending ones * docs: MCP files approval scope, reply text and README server wording * mcp: a download approves relays only for a card naming unknown servers * mcp: files in member chats of a group are tracked as in the main chat * docs: MCP relay approval of downloads and files in member chats * mcp: a rejected file gets a card again under the files approval * docs: MCP rejected files under the files approval, README scope * mcp: an offered file without a description gets no card or action * docs: MCP a file without a description is not downloaded * mcp: 0.2.0 on simplex-chat 7.1.0-beta.10.2 * ci: test the MCP server as a scope of build-linux
276 lines
12 KiB
TypeScript
276 lines
12 KiB
TypeScript
import {T} from "@simplex-chat/types"
|
|
import {api, core} from "simplex-chat"
|
|
|
|
const maxDetails = 500
|
|
const causeKey = /(Err|Error|errorType)$/
|
|
const blockingReasons: {[reason in T.BlockingReason]: string} = {
|
|
[T.BlockingReason.Spam]: "Spam",
|
|
[T.BlockingReason.Content]: "Content violates conditions of use",
|
|
}
|
|
const badgeIssues: {[status in T.BadgeStatus]: string} = {
|
|
[T.BadgeStatus.Active]: "",
|
|
[T.BadgeStatus.Expired]: " Sender's badge expired.",
|
|
[T.BadgeStatus.ExpiredOld]: " Sender's badge expired.",
|
|
[T.BadgeStatus.Failed]: " Sender's badge verification failed.",
|
|
[T.BadgeStatus.UnknownKey]: " No key to verify sender's badge.",
|
|
}
|
|
|
|
export class ExitError extends Error {
|
|
constructor(message: string, readonly code: number) {
|
|
super(message)
|
|
}
|
|
}
|
|
|
|
export function shellWord(word: string, plain: RegExp): string {
|
|
if (plain.test(word)) return word
|
|
return process.platform === "win32" ? `"${word}"` : `'${word.replace(/'/g, "'\\''")}'`
|
|
}
|
|
|
|
export function shellPath(path: string): string {
|
|
return shellWord(path, process.platform === "win32" ? /^[A-Za-z0-9_.:\\/-]+$/ : /^[A-Za-z0-9_./-]+$/)
|
|
}
|
|
|
|
export function oneLine(text: string): string {
|
|
return text.replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]+/gu, " ")
|
|
}
|
|
|
|
export function log(line: string): void {
|
|
process.stdout.write(`${line}\n`)
|
|
}
|
|
|
|
export function logError(context: string, error: unknown): void {
|
|
process.stderr.write(`${context}: ${errorText(error)}\n`)
|
|
}
|
|
|
|
export function errorText(error: unknown): string {
|
|
if (error instanceof core.ChatAPIError && error.chatError) return chatErrorText(error.chatError)
|
|
if (error instanceof api.ChatCommandError) return `SimpleX error: unexpected response ${error.response.type}`
|
|
return error instanceof Error ? error.message : String(error)
|
|
}
|
|
|
|
export function chatErrorText(chatError: T.ChatError): string {
|
|
return oneLine(appErrorText(chatError) ?? chainText(chatError))
|
|
}
|
|
|
|
function appErrorText(chatError: T.ChatError): string | undefined {
|
|
switch (chatError.type) {
|
|
case "error":
|
|
return chatErrorTypeText(chatError.errorType)
|
|
case "errorAgent":
|
|
return agentErrorText(chatError.agentError)
|
|
case "errorStore":
|
|
return storeErrorText(chatError.storeError)
|
|
}
|
|
}
|
|
|
|
function chatErrorTypeText(error: T.ChatErrorType): string | undefined {
|
|
switch (error.type) {
|
|
case "invalidConnReq":
|
|
return "Invalid connection link: Please check that you used the correct link or ask your contact to send you another one."
|
|
case "unsupportedConnReq":
|
|
return "Unsupported connection link: This link requires a newer app version. Please upgrade the app or ask your contact to send a compatible link."
|
|
case "simplexDomainNotReady": {
|
|
const name = fullDomainName(error.simplexDomain)
|
|
return error.simplexDomainError.type === "noValidLink"
|
|
? `No valid link: The SimpleX name ${name} is registered, but it has no valid link.`
|
|
: `Unconfirmed name: The SimpleX name ${name} is registered, but not added to profile. Please add it to your address or channel profile, if you are the owner.`
|
|
}
|
|
case "fileNotApproved":
|
|
return `Unknown servers! Without Tor or VPN, your IP address will be visible to these XFTP relays: ${[...new Set(error.unknownServers.map(serverHostname))].sort().join(", ")}.`
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function agentErrorText(error: T.AgentErrorType): string | undefined {
|
|
switch (error.type) {
|
|
case "NO_NAME_SERVERS":
|
|
return "SimpleX name error: None of your servers are set to resolve SimpleX names. Use a connection link."
|
|
case "SMP":
|
|
return smpErrorText(error.smpErr, error.serverAddress)
|
|
case "BROKER":
|
|
return brokerErrorText(error.brokerErr, serverHostname(error.brokerAddress))
|
|
case "PROXY":
|
|
return error.proxyErr.type === "protocolError" && error.proxyErr.protocolErr.type === "PROXY"
|
|
? destinationErrorText(error.proxyErr.protocolErr.proxyErr, serverHostname(error.proxyServer), serverHostname(error.relayServer))
|
|
: undefined
|
|
case "AGENT":
|
|
return error.agentErr.type === "A_SERVICE" && error.agentErr.serviceError.type === "timeout" ? "Connection timeout: The service did not respond. Please try again." : undefined
|
|
case "INTERNAL":
|
|
return error.internalErr === "SEUniqueID" ? "Already connected? It seems like you are already connected via this link. If it is not the case, there was an error (SEUniqueID)." : undefined
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function smpErrorText(error: T.ErrorType, server: string): string | undefined {
|
|
switch (error.type) {
|
|
case "NAME":
|
|
return nameErrorText(error.nameErr, server)
|
|
case "AUTH":
|
|
return "Connection link removed: Your contact removed this link, or it was a one-time link that was already used. To connect, ask your contact to create a new link."
|
|
case "BLOCKED":
|
|
return `Connection blocked: Connection is blocked by server operator: ${blockingReasons[error.blockInfo.reason]}.`
|
|
case "QUOTA":
|
|
return "Undelivered messages: The connection reached the limit of undelivered messages, your contact may be offline."
|
|
case "PROXY":
|
|
return error.proxyErr.type === "BROKER" ? forwardingErrorText(error.proxyErr.brokerErr, serverHostname(server)) : undefined
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function nameErrorText(error: T.NameErrorType, server: string): string {
|
|
switch (error.type) {
|
|
case "NOT_FOUND":
|
|
return "Name not found: This SimpleX name is not registered. Please check the name."
|
|
case "NO_RESOLVER":
|
|
return `SimpleX name error: Server ${server} does not support name resolution. Use a connection link.`
|
|
case "RESOLVER":
|
|
return `SimpleX name error: Resolver error: ${error.resolverErr}`
|
|
}
|
|
}
|
|
|
|
function brokerErrorText(error: T.BrokerErrorType, host: string): string | undefined {
|
|
switch (error.type) {
|
|
case "TIMEOUT":
|
|
return `Connection timeout: Please check your network connection with ${host} and try again.`
|
|
case "NETWORK":
|
|
return error.networkError.type === "unknownCAError"
|
|
? `Connection error: Fingerprint in server address does not match certificate: ${host}.`
|
|
: `Connection error: Please check your network connection with ${host} and try again.`
|
|
case "HOST":
|
|
return `Connection error: Server address is incompatible with network settings: ${host}.`
|
|
case "TRANSPORT":
|
|
return error.transportErr.type === "version" ? `Connection error: Server version is incompatible with your app: ${host}.` : undefined
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function forwardingErrorText(error: T.BrokerErrorType, host: string): string | undefined {
|
|
switch (error.type) {
|
|
case "TIMEOUT":
|
|
return `Private routing error: Error connecting to forwarding server ${host}. Please try later.`
|
|
case "NETWORK":
|
|
return error.networkError.type === "unknownCAError"
|
|
? `Private routing error: Fingerprint in forwarding server address does not match certificate: ${host}.`
|
|
: `Private routing error: Error connecting to forwarding server ${host}. Please try later.`
|
|
case "HOST":
|
|
return `Private routing error: Forwarding server address is incompatible with network settings: ${host}.`
|
|
case "TRANSPORT":
|
|
return error.transportErr.type === "version" ? `Private routing error: Forwarding server version is incompatible with network settings: ${host}.` : undefined
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function destinationErrorText(error: T.ProxyError, proxy: string, relay: string): string | undefined {
|
|
const failed = `Private routing error: Forwarding server ${proxy} failed to connect to destination server ${relay}. Please try later.`
|
|
if (error.type === "NO_SESSION") return failed
|
|
if (error.type !== "BROKER") return undefined
|
|
switch (error.brokerErr.type) {
|
|
case "TIMEOUT":
|
|
return failed
|
|
case "NETWORK":
|
|
return error.brokerErr.networkError.type === "unknownCAError" ? `Private routing error: Fingerprint in destination server address does not match certificate: ${relay}.` : failed
|
|
case "HOST":
|
|
return `Private routing error: Destination server address of ${relay} is incompatible with forwarding server ${proxy} settings.`
|
|
case "TRANSPORT":
|
|
return error.brokerErr.transportErr.type === "version" ? `Private routing error: Destination server version of ${relay} is incompatible with forwarding server ${proxy}.` : undefined
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function storeErrorText(error: T.StoreError): string | undefined {
|
|
switch (error.type) {
|
|
case "largeMsg":
|
|
return "Message is too large! Please reduce the message size and send again."
|
|
case "groupNotFound":
|
|
return "Group not found! This group no longer exists."
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
export function fileError({fileStatus, fileProhibited}: T.CIFile): string | undefined {
|
|
if (fileProhibited === undefined) return fileStatusError(fileStatus)
|
|
const {maxSize, badgeStatus} = fileProhibited
|
|
return `Large file! The file is above the limit of ${maxSize} bytes for its sender.${badgeStatus === undefined ? "" : badgeIssues[badgeStatus]}`
|
|
}
|
|
|
|
function fileStatusError(status: T.CIFileStatus): string | undefined {
|
|
switch (status.type) {
|
|
case "rcvCancelled":
|
|
return "The file was cancelled."
|
|
case "rcvAborted":
|
|
return "The download was aborted."
|
|
case "rcvError":
|
|
return fileErrorText(status.rcvFileError)
|
|
case "sndError":
|
|
return fileErrorText(status.sndFileError)
|
|
case "invalid":
|
|
return oneLine(`Invalid file: ${status.text}`)
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function fileErrorText(error: T.FileError): string {
|
|
switch (error.type) {
|
|
case "auth":
|
|
return "Wrong key or unknown file chunk address - most likely file is deleted."
|
|
case "blocked":
|
|
return `File is blocked by server operator: ${blockingReasons[error.blockInfo.reason]}.`
|
|
case "noFile":
|
|
return "File not found - most likely file was deleted or cancelled."
|
|
case "relay":
|
|
return oneLine(`File server error: ${srvErrorText(error.srvError)}`)
|
|
case "other":
|
|
return oneLine(`Error: ${error.fileError}`)
|
|
}
|
|
}
|
|
|
|
function srvErrorText(error: T.SrvError): string {
|
|
switch (error.type) {
|
|
case "host":
|
|
return "Server address is incompatible with network settings."
|
|
case "version":
|
|
return "Server version is incompatible with network settings."
|
|
case "other":
|
|
return error.srvError
|
|
}
|
|
}
|
|
|
|
export function serverHostname(server: string): string {
|
|
return /^[a-z]+:\/\/[^@]*@([^,:/]+)/.exec(server)?.[1] ?? server
|
|
}
|
|
|
|
function chainText(chatError: T.ChatError): string {
|
|
const {types, details} = errorChain(chatError)
|
|
const chain = types.join(": ")
|
|
const text = JSON.stringify(details)
|
|
return text === "{}" ? `SimpleX error: ${chain}` : `SimpleX error: ${chain}: ${text.slice(0, maxDetails)}`
|
|
}
|
|
|
|
function errorChain({type, ...fields}: {type: string}): {types: string[]; details: object} {
|
|
const entries = Object.entries(fields)
|
|
const cause = entries.find(([key, value]) => causeKey.test(key) && isTyped(value))?.[1] as {type: string} | undefined
|
|
const below = cause === undefined ? {types: [], details: {}} : errorChain(cause)
|
|
return {types: [type, ...below.types], details: {...Object.fromEntries(entries.filter(([, value]) => typeof value !== "object")), ...below.details}}
|
|
}
|
|
|
|
function isTyped(value: unknown): boolean {
|
|
return typeof value === "object" && value !== null && typeof (value as {type?: unknown}).type === "string"
|
|
}
|
|
|
|
export function chatErrorOf(error: unknown): T.ChatError | undefined {
|
|
return error instanceof core.ChatAPIError ? error.chatError : undefined
|
|
}
|
|
|
|
export function fullDomainName({nameTLD, domain, subDomain}: T.SimplexDomain): string {
|
|
const tld = nameTLD === T.SimplexTLD.Simplex ? ".simplex" : nameTLD === T.SimplexTLD.Testing ? ".testing" : ""
|
|
return `${[...subDomain].reverse().concat(domain).join(".")}${tld}`
|
|
}
|