agentcash
Version:
Generic MCP server for calling x402-protected APIs with automatic payment handling
560 lines (557 loc) • 16 kB
JavaScript
// ../../../node_modules/.pnpm/neverthrow@8.2.0/node_modules/neverthrow/dist/index.es.js
var defaultErrorConfig = {
withStackTrace: false
};
var createNeverThrowError = (message, result, config = defaultErrorConfig) => {
const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
return {
data,
message,
stack: maybeStack
};
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function(resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function(resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function() {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
function __await(v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
}
function __asyncGenerator(thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function() {
return this;
}, i;
function awaitReturn(f) {
return function(v) {
return Promise.resolve(v).then(f, reject);
};
}
function verb(n, f) {
if (g[n]) {
i[n] = function(v) {
return new Promise(function(a, b) {
q.push([n, v, a, b]) > 1 || resume(n, v);
});
};
if (f) i[n] = f(i[n]);
}
}
function resume(n, v) {
try {
step(g[n](v));
} catch (e) {
settle(q[0][3], e);
}
}
function step(r) {
r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r);
}
function fulfill(value) {
resume("next", value);
}
function reject(value) {
resume("throw", value);
}
function settle(f, v) {
if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]);
}
}
function __asyncDelegator(o) {
var i, p;
return i = {}, verb("next"), verb("throw", function(e) {
throw e;
}), verb("return"), i[Symbol.iterator] = function() {
return this;
}, i;
function verb(n, f) {
i[n] = o[n] ? function(v) {
return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v;
} : f;
}
}
function __asyncValues(o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function() {
return this;
}, i);
function verb(n) {
i[n] = o[n] && function(v) {
return new Promise(function(resolve, reject) {
v = o[n](v), settle(resolve, reject, v.done, v.value);
});
};
}
function settle(resolve, reject, d, v) {
Promise.resolve(v).then(function(v2) {
resolve({ value: v2, done: d });
}, reject);
}
}
var ResultAsync = class _ResultAsync {
constructor(res) {
this._promise = res;
}
static fromSafePromise(promise) {
const newPromise = promise.then((value) => new Ok(value));
return new _ResultAsync(newPromise);
}
static fromPromise(promise, errorFn) {
const newPromise = promise.then((value) => new Ok(value)).catch((e) => new Err(errorFn(e)));
return new _ResultAsync(newPromise);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromThrowable(fn, errorFn) {
return (...args) => {
return new _ResultAsync((() => __awaiter(this, void 0, void 0, function* () {
try {
return new Ok(yield fn(...args));
} catch (error) {
return new Err(errorFn ? errorFn(error) : error);
}
}))());
};
}
static combine(asyncResultList) {
return combineResultAsyncList(asyncResultList);
}
static combineWithAllErrors(asyncResultList) {
return combineResultAsyncListWithAllErrors(asyncResultList);
}
map(f) {
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.isErr()) {
return new Err(res.error);
}
return new Ok(yield f(res.value));
})));
}
andThrough(f) {
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.isErr()) {
return new Err(res.error);
}
const newRes = yield f(res.value);
if (newRes.isErr()) {
return new Err(newRes.error);
}
return new Ok(res.value);
})));
}
andTee(f) {
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.isErr()) {
return new Err(res.error);
}
try {
yield f(res.value);
} catch (e) {
}
return new Ok(res.value);
})));
}
orTee(f) {
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.isOk()) {
return new Ok(res.value);
}
try {
yield f(res.error);
} catch (e) {
}
return new Err(res.error);
})));
}
mapErr(f) {
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.isOk()) {
return new Ok(res.value);
}
return new Err(yield f(res.error));
})));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
andThen(f) {
return new _ResultAsync(this._promise.then((res) => {
if (res.isErr()) {
return new Err(res.error);
}
const newValue = f(res.value);
return newValue instanceof _ResultAsync ? newValue._promise : newValue;
}));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
orElse(f) {
return new _ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
if (res.isErr()) {
return f(res.error);
}
return new Ok(res.value);
})));
}
match(ok3, _err) {
return this._promise.then((res) => res.match(ok3, _err));
}
unwrapOr(t) {
return this._promise.then((res) => res.unwrapOr(t));
}
/**
* @deprecated will be removed in 9.0.0.
*
* You can use `safeTry` without this method.
* @example
* ```typescript
* safeTry(async function* () {
* const okValue = yield* yourResult
* })
* ```
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
*/
safeUnwrap() {
return __asyncGenerator(this, arguments, function* safeUnwrap_1() {
return yield __await(yield __await(yield* __asyncDelegator(__asyncValues(yield __await(this._promise.then((res) => res.safeUnwrap()))))));
});
}
// Makes ResultAsync implement PromiseLike<Result>
then(successCallback, failureCallback) {
return this._promise.then(successCallback, failureCallback);
}
[Symbol.asyncIterator]() {
return __asyncGenerator(this, arguments, function* _a() {
const result = yield __await(this._promise);
if (result.isErr()) {
yield yield __await(errAsync(result.error));
}
return yield __await(result.value);
});
}
};
function errAsync(err3) {
return new ResultAsync(Promise.resolve(new Err(err3)));
}
var fromPromise = ResultAsync.fromPromise;
var fromSafePromise = ResultAsync.fromSafePromise;
var fromAsyncThrowable = ResultAsync.fromThrowable;
var combineResultList = (resultList) => {
let acc = ok([]);
for (const result of resultList) {
if (result.isErr()) {
acc = err(result.error);
break;
} else {
acc.map((list) => list.push(result.value));
}
}
return acc;
};
var combineResultAsyncList = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultList);
var combineResultListWithAllErrors = (resultList) => {
let acc = ok([]);
for (const result of resultList) {
if (result.isErr() && acc.isErr()) {
acc.error.push(result.error);
} else if (result.isErr() && acc.isOk()) {
acc = err([result.error]);
} else if (result.isOk() && acc.isOk()) {
acc.value.push(result.value);
}
}
return acc;
};
var combineResultAsyncListWithAllErrors = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultListWithAllErrors);
var Result;
(function(Result2) {
function fromThrowable2(fn, errorFn) {
return (...args) => {
try {
const result = fn(...args);
return ok(result);
} catch (e) {
return err(errorFn ? errorFn(e) : e);
}
};
}
Result2.fromThrowable = fromThrowable2;
function combine(resultList) {
return combineResultList(resultList);
}
Result2.combine = combine;
function combineWithAllErrors(resultList) {
return combineResultListWithAllErrors(resultList);
}
Result2.combineWithAllErrors = combineWithAllErrors;
})(Result || (Result = {}));
function ok(value) {
return new Ok(value);
}
function err(err3) {
return new Err(err3);
}
var Ok = class {
constructor(value) {
this.value = value;
}
isOk() {
return true;
}
isErr() {
return !this.isOk();
}
map(f) {
return ok(f(this.value));
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mapErr(_f) {
return ok(this.value);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
andThen(f) {
return f(this.value);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
andThrough(f) {
return f(this.value).map((_value) => this.value);
}
andTee(f) {
try {
f(this.value);
} catch (e) {
}
return ok(this.value);
}
orTee(_f) {
return ok(this.value);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
orElse(_f) {
return ok(this.value);
}
asyncAndThen(f) {
return f(this.value);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
asyncAndThrough(f) {
return f(this.value).map(() => this.value);
}
asyncMap(f) {
return ResultAsync.fromSafePromise(f(this.value));
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
unwrapOr(_v) {
return this.value;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
match(ok3, _err) {
return ok3(this.value);
}
safeUnwrap() {
const value = this.value;
return (function* () {
return value;
})();
}
_unsafeUnwrap(_) {
return this.value;
}
_unsafeUnwrapErr(config) {
throw createNeverThrowError("Called `_unsafeUnwrapErr` on an Ok", this, config);
}
// eslint-disable-next-line @typescript-eslint/no-this-alias, require-yield
*[Symbol.iterator]() {
return this.value;
}
};
var Err = class {
constructor(error) {
this.error = error;
}
isOk() {
return false;
}
isErr() {
return !this.isOk();
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
map(_f) {
return err(this.error);
}
mapErr(f) {
return err(f(this.error));
}
andThrough(_f) {
return err(this.error);
}
andTee(_f) {
return err(this.error);
}
orTee(f) {
try {
f(this.error);
} catch (e) {
}
return err(this.error);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
andThen(_f) {
return err(this.error);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
orElse(f) {
return f(this.error);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
asyncAndThen(_f) {
return errAsync(this.error);
}
asyncAndThrough(_f) {
return errAsync(this.error);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
asyncMap(_f) {
return errAsync(this.error);
}
unwrapOr(v) {
return v;
}
match(_ok, err3) {
return err3(this.error);
}
safeUnwrap() {
const error = this.error;
return (function* () {
yield err(error);
throw new Error("Do not use this generator out of `safeTry`");
})();
}
_unsafeUnwrap(config) {
throw createNeverThrowError("Called `_unsafeUnwrap` on an Err", this, config);
}
_unsafeUnwrapErr(_) {
return this.error;
}
*[Symbol.iterator]() {
const self = this;
yield self;
return self;
}
};
var fromThrowable = Result.fromThrowable;
// ../../internal/neverthrow/src/index.ts
function resultFromPromise(type, surface, promise, error) {
return ResultAsync.fromPromise(promise, (e) => ({
...error(e),
type,
surface
}));
}
function resultFromThrowable(type, surface, fn, error) {
return Result.fromThrowable(fn, (e) => ({
...error(e),
type,
surface
}))();
}
function ok2(data) {
return ok(data);
}
function err2(type, surface, error) {
return err({
...error,
type,
surface
});
}
// src/shared/fs.ts
import { join } from "path";
import { homedir } from "os";
import * as fs from "fs";
var BASE_DIRECTORY = join(homedir(), ".agentcash");
var LEGACY_DIRECTORY = join(homedir(), ".x402scan-mcp");
function migrateFromLegacy() {
if (!fs.existsSync(LEGACY_DIRECTORY)) return;
const result = resultFromThrowable(
"migration",
"fs",
() => {
const files = fs.readdirSync(LEGACY_DIRECTORY);
for (const file of files) {
if (file.endsWith(".log")) continue;
const src = join(LEGACY_DIRECTORY, file);
const dest = join(BASE_DIRECTORY, file);
if (!fs.existsSync(dest) && fs.statSync(src).isFile()) {
fs.copyFileSync(src, dest);
const { mode } = fs.statSync(src);
fs.chmodSync(dest, mode);
}
}
},
(e) => ({
cause: "migration_failed",
message: `Failed to migrate: ${e instanceof Error ? e.message : String(e)}`
})
);
if (result.isErr()) {
console.error(
`[agentcash] Warning: failed to migrate files from ${LEGACY_DIRECTORY} to ${BASE_DIRECTORY}. Your wallet may still be at ${join(LEGACY_DIRECTORY, "wallet.json")} \u2014 copy it manually to ${join(BASE_DIRECTORY, "wallet.json")}. Error: ${result.error.message}`
);
}
}
var initialized = false;
function ensureBaseDir() {
if (initialized) return;
if (!fs.existsSync(BASE_DIRECTORY)) {
fs.mkdirSync(BASE_DIRECTORY, { recursive: true });
}
migrateFromLegacy();
initialized = true;
}
var configFile = (name) => {
ensureBaseDir();
const filePath = join(BASE_DIRECTORY, name);
return filePath;
};
export {
resultFromPromise,
resultFromThrowable,
ok2 as ok,
err2 as err,
configFile
};
//# sourceMappingURL=chunk-YWNBUUBR.js.map