@oxap/sdk 0.0.0-stage → 0.1.0-alpha.33
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/README.md +575 -2
- package/dist/src/abort_signal.d.ts +5 -0
- package/dist/src/abort_signal.js +52 -0
- package/dist/src/auth_protocol.d.ts +67 -0
- package/dist/src/auth_protocol.js +113 -0
- package/dist/src/binary_resolver.d.ts +29 -0
- package/dist/src/binary_resolver.js +207 -0
- package/dist/src/execution_client.d.ts +23 -0
- package/dist/src/execution_client.js +7 -0
- package/dist/src/execution_types.d.ts +204 -0
- package/dist/src/execution_types.js +28 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +30 -0
- package/dist/src/logger.d.ts +8 -0
- package/dist/src/logger.js +16 -0
- package/dist/src/models_types.d.ts +75 -0
- package/dist/src/models_types.js +14 -0
- package/dist/src/oap_client.d.ts +64 -0
- package/dist/src/oap_client.js +1023 -0
- package/dist/src/timeout_diagnostics.d.ts +22 -0
- package/dist/src/timeout_diagnostics.js +44 -0
- package/package.json +43 -3
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { TimeoutDiagnostics } from "./timeout_diagnostics";
|
|
2
|
+
export type ProviderId = string;
|
|
3
|
+
export declare const AUTH_STATUSES: readonly ["authenticated", "login_required", "expired", "refreshing", "login_in_progress", "failed", "unknown"];
|
|
4
|
+
export type AuthStatus = (typeof AUTH_STATUSES)[number];
|
|
5
|
+
declare const AUTH_KINDS: readonly ["api_key", "oauth", "none"];
|
|
6
|
+
export type AuthKind = (typeof AUTH_KINDS)[number];
|
|
7
|
+
export { AUTH_KINDS };
|
|
8
|
+
export interface ProviderAuthInfo {
|
|
9
|
+
id: ProviderId;
|
|
10
|
+
name: string;
|
|
11
|
+
auth_kinds: AuthKind[];
|
|
12
|
+
auth_status: AuthStatus;
|
|
13
|
+
last_error?: string;
|
|
14
|
+
override_host?: string;
|
|
15
|
+
}
|
|
16
|
+
export type MakaiAuthEvent = {
|
|
17
|
+
type: "auth_url";
|
|
18
|
+
flow_id: string;
|
|
19
|
+
provider_id: ProviderId;
|
|
20
|
+
url: string;
|
|
21
|
+
instructions?: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: "prompt";
|
|
24
|
+
flow_id: string;
|
|
25
|
+
prompt_id: string;
|
|
26
|
+
provider_id: ProviderId;
|
|
27
|
+
message: string;
|
|
28
|
+
allow_empty: boolean;
|
|
29
|
+
} | {
|
|
30
|
+
type: "progress";
|
|
31
|
+
flow_id: string;
|
|
32
|
+
provider_id: ProviderId;
|
|
33
|
+
message: string;
|
|
34
|
+
} | {
|
|
35
|
+
type: "success";
|
|
36
|
+
flow_id: string;
|
|
37
|
+
provider_id: ProviderId;
|
|
38
|
+
} | {
|
|
39
|
+
type: "error";
|
|
40
|
+
flow_id: string;
|
|
41
|
+
provider_id: ProviderId;
|
|
42
|
+
code?: string;
|
|
43
|
+
message: string;
|
|
44
|
+
};
|
|
45
|
+
export interface AuthFlowHandlers {
|
|
46
|
+
onEvent?: (event: MakaiAuthEvent) => void;
|
|
47
|
+
}
|
|
48
|
+
export type MakaiAuthErrorKind = "provider_error" | "cancelled" | "transport_error" | "unknown";
|
|
49
|
+
export declare class MakaiAuthError extends Error {
|
|
50
|
+
readonly kind: MakaiAuthErrorKind;
|
|
51
|
+
readonly code?: string;
|
|
52
|
+
readonly diagnostics?: TimeoutDiagnostics;
|
|
53
|
+
constructor(message: string, options?: {
|
|
54
|
+
kind?: MakaiAuthErrorKind;
|
|
55
|
+
code?: string;
|
|
56
|
+
diagnostics?: TimeoutDiagnostics;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export interface MakaiAuthApi {
|
|
60
|
+
listProviders(): Promise<ProviderAuthInfo[]>;
|
|
61
|
+
login(providerId: ProviderId, handlers?: AuthFlowHandlers, options?: {
|
|
62
|
+
signal?: AbortSignal;
|
|
63
|
+
}): Promise<{
|
|
64
|
+
status: "success";
|
|
65
|
+
}>;
|
|
66
|
+
}
|
|
67
|
+
export declare function flattenAuthEvent(payload: Record<string, unknown>): MakaiAuthEvent;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MakaiAuthError = exports.AUTH_KINDS = exports.AUTH_STATUSES = void 0;
|
|
4
|
+
exports.flattenAuthEvent = flattenAuthEvent;
|
|
5
|
+
exports.AUTH_STATUSES = [
|
|
6
|
+
"authenticated",
|
|
7
|
+
"login_required",
|
|
8
|
+
"expired",
|
|
9
|
+
"refreshing",
|
|
10
|
+
"login_in_progress",
|
|
11
|
+
"failed",
|
|
12
|
+
"unknown",
|
|
13
|
+
];
|
|
14
|
+
const AUTH_KINDS = ["api_key", "oauth", "none"];
|
|
15
|
+
exports.AUTH_KINDS = AUTH_KINDS;
|
|
16
|
+
class MakaiAuthError extends Error {
|
|
17
|
+
kind;
|
|
18
|
+
code;
|
|
19
|
+
diagnostics;
|
|
20
|
+
constructor(message, options = {}) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "MakaiAuthError";
|
|
23
|
+
this.kind = options.kind ?? "unknown";
|
|
24
|
+
this.code = options.code;
|
|
25
|
+
this.diagnostics = options.diagnostics;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.MakaiAuthError = MakaiAuthError;
|
|
29
|
+
const AUTH_EVENT_VARIANTS = [
|
|
30
|
+
"auth_url",
|
|
31
|
+
"prompt",
|
|
32
|
+
"progress",
|
|
33
|
+
"success",
|
|
34
|
+
"error",
|
|
35
|
+
];
|
|
36
|
+
function flattenAuthEvent(payload) {
|
|
37
|
+
for (const variant of AUTH_EVENT_VARIANTS) {
|
|
38
|
+
const value = payload[variant];
|
|
39
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
40
|
+
return normalizeAuthEvent(variant, value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
throw new MakaiAuthError(`unknown auth_event variant: ${JSON.stringify(payload)}`, { kind: "unknown" });
|
|
44
|
+
}
|
|
45
|
+
function normalizeAuthEvent(variant, data) {
|
|
46
|
+
const flow_id = stringField(data, "flow_id");
|
|
47
|
+
const provider_id = stringField(data, "provider_id");
|
|
48
|
+
switch (variant) {
|
|
49
|
+
case "auth_url": {
|
|
50
|
+
const event = {
|
|
51
|
+
type: "auth_url",
|
|
52
|
+
flow_id,
|
|
53
|
+
provider_id,
|
|
54
|
+
url: stringField(data, "url"),
|
|
55
|
+
};
|
|
56
|
+
const instructions = optionalStringField(data, "instructions");
|
|
57
|
+
if (instructions !== undefined)
|
|
58
|
+
event.instructions = instructions;
|
|
59
|
+
return event;
|
|
60
|
+
}
|
|
61
|
+
case "prompt":
|
|
62
|
+
return {
|
|
63
|
+
type: "prompt",
|
|
64
|
+
flow_id,
|
|
65
|
+
prompt_id: stringField(data, "prompt_id"),
|
|
66
|
+
provider_id,
|
|
67
|
+
message: stringField(data, "message"),
|
|
68
|
+
allow_empty: typeof data["allow_empty"] === "boolean" ? data["allow_empty"] : false,
|
|
69
|
+
};
|
|
70
|
+
case "progress":
|
|
71
|
+
return {
|
|
72
|
+
type: "progress",
|
|
73
|
+
flow_id,
|
|
74
|
+
provider_id,
|
|
75
|
+
message: stringField(data, "message"),
|
|
76
|
+
};
|
|
77
|
+
case "success":
|
|
78
|
+
return {
|
|
79
|
+
type: "success",
|
|
80
|
+
flow_id,
|
|
81
|
+
provider_id,
|
|
82
|
+
};
|
|
83
|
+
case "error": {
|
|
84
|
+
const event = {
|
|
85
|
+
type: "error",
|
|
86
|
+
flow_id,
|
|
87
|
+
provider_id,
|
|
88
|
+
message: stringField(data, "message"),
|
|
89
|
+
};
|
|
90
|
+
const code = optionalStringField(data, "code");
|
|
91
|
+
if (code !== undefined)
|
|
92
|
+
event.code = code;
|
|
93
|
+
return event;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function stringField(data, key) {
|
|
98
|
+
const value = data[key];
|
|
99
|
+
if (typeof value !== "string") {
|
|
100
|
+
throw new MakaiAuthError(`auth_event field "${key}" missing or not a string`, {
|
|
101
|
+
kind: "transport_error",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
function optionalStringField(data, key) {
|
|
107
|
+
const value = data[key];
|
|
108
|
+
if (value === undefined || value === null)
|
|
109
|
+
return undefined;
|
|
110
|
+
if (typeof value !== "string")
|
|
111
|
+
return undefined;
|
|
112
|
+
return value.length > 0 ? value : undefined;
|
|
113
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type MakaiLogger } from "./logger";
|
|
2
|
+
export type ModuleResolver = (specifier: string) => string;
|
|
3
|
+
type BinaryResolverBaseOptions = {
|
|
4
|
+
cacheDir?: string;
|
|
5
|
+
cwd?: string;
|
|
6
|
+
resolveModule?: ModuleResolver;
|
|
7
|
+
logger?: MakaiLogger;
|
|
8
|
+
};
|
|
9
|
+
type BinaryPathResolverOptions = BinaryResolverBaseOptions & {
|
|
10
|
+
type?: "path";
|
|
11
|
+
binaryPath: string;
|
|
12
|
+
binaryUrl?: undefined;
|
|
13
|
+
checksumSha256?: string;
|
|
14
|
+
};
|
|
15
|
+
type BinaryUrlResolverOptions = BinaryResolverBaseOptions & {
|
|
16
|
+
type?: "url";
|
|
17
|
+
binaryPath?: undefined;
|
|
18
|
+
binaryUrl: string;
|
|
19
|
+
checksumSha256: string;
|
|
20
|
+
};
|
|
21
|
+
type BinaryAutoResolverOptions = BinaryResolverBaseOptions & {
|
|
22
|
+
type?: "auto";
|
|
23
|
+
binaryPath?: undefined;
|
|
24
|
+
binaryUrl?: undefined;
|
|
25
|
+
checksumSha256?: string;
|
|
26
|
+
};
|
|
27
|
+
export type BinaryResolverOptions = BinaryPathResolverOptions | BinaryUrlResolverOptions | BinaryAutoResolverOptions;
|
|
28
|
+
export declare function resolveMakaiBinary(options?: BinaryResolverOptions): Promise<string>;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.resolveMakaiBinary = resolveMakaiBinary;
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const node_fs_1 = require("node:fs");
|
|
9
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
10
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
11
|
+
const logger_1 = require("./logger");
|
|
12
|
+
const ENV_BINARY_PATH = "OAP_SDK_BINARY_PATH";
|
|
13
|
+
const ENV_BINARY_URL = "OAP_SDK_BINARY_URL";
|
|
14
|
+
const ENV_BINARY_SHA256 = "OAP_SDK_BINARY_SHA256";
|
|
15
|
+
const ENV_BINARY_PATH_OXAP = "OXAP_SDK_BINARY_PATH";
|
|
16
|
+
const ENV_BINARY_URL_OXAP = "OXAP_SDK_BINARY_URL";
|
|
17
|
+
const ENV_BINARY_SHA256_OXAP = "OXAP_SDK_BINARY_SHA256";
|
|
18
|
+
function binaryNamesForPlatform(platform = process.platform) {
|
|
19
|
+
if (platform === "win32")
|
|
20
|
+
return ["oxap.exe", "oapx.exe"];
|
|
21
|
+
return ["oxap", "oapx"];
|
|
22
|
+
}
|
|
23
|
+
function binaryNameForPlatform(platform = process.platform) {
|
|
24
|
+
return binaryNamesForPlatform(platform)[0];
|
|
25
|
+
}
|
|
26
|
+
function firstNonEmpty(...values) {
|
|
27
|
+
return values.find((value) => value !== undefined && value !== "");
|
|
28
|
+
}
|
|
29
|
+
function binaryURLAndChecksum(options) {
|
|
30
|
+
const optionChecksum = options.checksumSha256;
|
|
31
|
+
const oxapURL = firstNonEmpty(process.env[ENV_BINARY_URL_OXAP]);
|
|
32
|
+
if (oxapURL) {
|
|
33
|
+
return { url: oxapURL, checksum: firstNonEmpty(process.env[ENV_BINARY_SHA256_OXAP], optionChecksum) };
|
|
34
|
+
}
|
|
35
|
+
const legacyURL = firstNonEmpty(process.env[ENV_BINARY_URL]);
|
|
36
|
+
if (legacyURL) {
|
|
37
|
+
return { url: legacyURL, checksum: firstNonEmpty(process.env[ENV_BINARY_SHA256], optionChecksum) };
|
|
38
|
+
}
|
|
39
|
+
if (options.binaryUrl) {
|
|
40
|
+
return {
|
|
41
|
+
url: options.binaryUrl,
|
|
42
|
+
checksum: firstNonEmpty(process.env[ENV_BINARY_SHA256_OXAP], process.env[ENV_BINARY_SHA256], optionChecksum),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return {};
|
|
46
|
+
}
|
|
47
|
+
async function ensureFileExists(filePath) {
|
|
48
|
+
await node_fs_1.promises.access(filePath);
|
|
49
|
+
}
|
|
50
|
+
async function isExecutableFile(filePath) {
|
|
51
|
+
try {
|
|
52
|
+
const stats = await node_fs_1.promises.stat(filePath);
|
|
53
|
+
if (!stats.isFile())
|
|
54
|
+
return false;
|
|
55
|
+
await node_fs_1.promises.access(filePath, node_fs_1.constants.X_OK);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function fileExists(filePath) {
|
|
63
|
+
try {
|
|
64
|
+
await node_fs_1.promises.access(filePath);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function sha256(content) {
|
|
75
|
+
return (0, node_crypto_1.createHash)("sha256").update(content).digest("hex");
|
|
76
|
+
}
|
|
77
|
+
function requireChecksumForUrl(binaryUrl, checksumSha256) {
|
|
78
|
+
if (!checksumSha256) {
|
|
79
|
+
throw new Error(`SHA256 checksum is required when downloading oapx binary from URL: ${binaryUrl}`);
|
|
80
|
+
}
|
|
81
|
+
return checksumSha256;
|
|
82
|
+
}
|
|
83
|
+
async function verifyChecksum(filePath, checksumSha256) {
|
|
84
|
+
const content = await node_fs_1.promises.readFile(filePath);
|
|
85
|
+
const actual = sha256(content);
|
|
86
|
+
if (actual !== checksumSha256.toLowerCase()) {
|
|
87
|
+
throw new Error(`binary checksum mismatch: expected ${checksumSha256}, got ${actual}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function downloadToCache(url, targetPath, checksumSha256) {
|
|
91
|
+
const response = await fetch(url);
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
throw new Error(`failed to download binary: ${response.status} ${response.statusText}`);
|
|
94
|
+
}
|
|
95
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
96
|
+
const content = Buffer.from(arrayBuffer);
|
|
97
|
+
const actual = sha256(content);
|
|
98
|
+
if (actual !== checksumSha256.toLowerCase()) {
|
|
99
|
+
throw new Error(`binary checksum mismatch: expected ${checksumSha256}, got ${actual}`);
|
|
100
|
+
}
|
|
101
|
+
await node_fs_1.promises.mkdir(node_path_1.default.dirname(targetPath), { recursive: true });
|
|
102
|
+
const tempPath = `${targetPath}.tmp`;
|
|
103
|
+
try {
|
|
104
|
+
await node_fs_1.promises.writeFile(tempPath, content);
|
|
105
|
+
if (process.platform !== "win32") {
|
|
106
|
+
await node_fs_1.promises.chmod(tempPath, 0o755);
|
|
107
|
+
}
|
|
108
|
+
await node_fs_1.promises.rename(tempPath, targetPath);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
await node_fs_1.promises.rm(tempPath, { force: true }).catch(() => undefined);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function resolveMakaiBinary(options = {}) {
|
|
116
|
+
const logger = options.logger ?? (0, logger_1.getNoopLogger)();
|
|
117
|
+
const explicitBinaryPath = firstNonEmpty(process.env[ENV_BINARY_PATH_OXAP], process.env[ENV_BINARY_PATH], options.binaryPath);
|
|
118
|
+
if (explicitBinaryPath) {
|
|
119
|
+
const resolved = node_path_1.default.resolve(explicitBinaryPath);
|
|
120
|
+
logger.debug("binary: resolving from explicit path", { path: resolved });
|
|
121
|
+
await ensureFileExists(resolved);
|
|
122
|
+
logger.debug("binary: resolved from explicit path", { path: resolved });
|
|
123
|
+
return resolved;
|
|
124
|
+
}
|
|
125
|
+
const { url: binaryUrl, checksum: checksumSha256 } = binaryURLAndChecksum(options);
|
|
126
|
+
if (binaryUrl) {
|
|
127
|
+
const requiredChecksumSha256 = requireChecksumForUrl(binaryUrl, checksumSha256);
|
|
128
|
+
const binaryName = binaryNameForPlatform();
|
|
129
|
+
const cacheDir = options.cacheDir ?? node_path_1.default.join(node_os_1.default.homedir(), ".cache", "makai", "bin");
|
|
130
|
+
const urlPathName = new URL(binaryUrl).pathname;
|
|
131
|
+
const fileName = node_path_1.default.basename(urlPathName) || binaryName;
|
|
132
|
+
const cachePath = node_path_1.default.join(cacheDir, fileName);
|
|
133
|
+
logger.debug("binary: resolving from URL", { url: binaryUrl, cache_path: cachePath });
|
|
134
|
+
if (await fileExists(cachePath)) {
|
|
135
|
+
try {
|
|
136
|
+
await verifyChecksum(cachePath, requiredChecksumSha256);
|
|
137
|
+
logger.debug("binary: cached file checksum verified", { path: cachePath, sha256: requiredChecksumSha256 });
|
|
138
|
+
return cachePath;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
if (error instanceof Error && error.message.startsWith("binary checksum mismatch")) {
|
|
142
|
+
logger.warn("binary: cached file checksum mismatch, re-downloading", { path: cachePath });
|
|
143
|
+
await node_fs_1.promises.rm(cachePath, { force: true });
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!(await fileExists(cachePath))) {
|
|
151
|
+
logger.debug("binary: downloading from URL", { url: binaryUrl, target: cachePath });
|
|
152
|
+
await downloadToCache(binaryUrl, cachePath, requiredChecksumSha256);
|
|
153
|
+
logger.info("binary: download complete", { path: cachePath, sha256: requiredChecksumSha256 });
|
|
154
|
+
return cachePath;
|
|
155
|
+
}
|
|
156
|
+
return cachePath;
|
|
157
|
+
}
|
|
158
|
+
const platformKey = `${process.platform}-${process.arch}`;
|
|
159
|
+
const resolveModule = options.resolveModule ?? ((specifier) => require.resolve(specifier));
|
|
160
|
+
for (const bundledPackage of [`@oxap/cli-${platformKey}`, `@oap-sdk/cli-${platformKey}`]) {
|
|
161
|
+
for (const name of binaryNamesForPlatform()) {
|
|
162
|
+
try {
|
|
163
|
+
const bundledPath = resolveModule(`${bundledPackage}/bin/${name}`);
|
|
164
|
+
logger.debug("binary: resolved from bundled package", { path: bundledPath, package: bundledPackage });
|
|
165
|
+
return bundledPath;
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
logger.debug("binary: bundled package not found", { package: bundledPackage, binary: name });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const cwd = options.cwd ?? process.cwd();
|
|
173
|
+
const localCandidates = binaryNamesForPlatform().flatMap((name) => [
|
|
174
|
+
node_path_1.default.resolve(cwd, "zig-out", "bin", name),
|
|
175
|
+
node_path_1.default.resolve(cwd, "zig", "zig-out", "bin", name),
|
|
176
|
+
]);
|
|
177
|
+
for (const candidate of localCandidates) {
|
|
178
|
+
logger.debug("binary: checking local candidate", { path: candidate });
|
|
179
|
+
if (await isExecutableFile(candidate)) {
|
|
180
|
+
logger.debug("binary: resolved from local candidate", { path: candidate });
|
|
181
|
+
return candidate;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const name of binaryNamesForPlatform()) {
|
|
185
|
+
const onPath = await findOnPath(name);
|
|
186
|
+
if (onPath !== undefined) {
|
|
187
|
+
logger.debug("binary: resolved from PATH", { path: onPath, binary: name });
|
|
188
|
+
return onPath;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const binaryName = binaryNameForPlatform();
|
|
192
|
+
logger.debug("binary: falling back to PATH lookup", { binary: binaryName });
|
|
193
|
+
return binaryName;
|
|
194
|
+
}
|
|
195
|
+
async function findOnPath(name) {
|
|
196
|
+
const raw = process.env.PATH;
|
|
197
|
+
if (!raw)
|
|
198
|
+
return undefined;
|
|
199
|
+
for (const dir of raw.split(node_path_1.default.delimiter)) {
|
|
200
|
+
if (dir.length === 0)
|
|
201
|
+
continue;
|
|
202
|
+
const candidate = node_path_1.default.join(dir, name);
|
|
203
|
+
if (await isExecutableFile(candidate))
|
|
204
|
+
return candidate;
|
|
205
|
+
}
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MakaiAuthApi } from "./auth_protocol";
|
|
2
|
+
import type { BinaryResolverOptions } from "./binary_resolver";
|
|
3
|
+
import type { MakaiAgentApi, MakaiClientOptions, MakaiProviderApi } from "./execution_types";
|
|
4
|
+
import type { MakaiModelsApi } from "./models_types";
|
|
5
|
+
export interface MakaiClient {
|
|
6
|
+
auth: MakaiAuthApi;
|
|
7
|
+
models: MakaiModelsApi;
|
|
8
|
+
agent: MakaiAgentModelsApi;
|
|
9
|
+
provider: MakaiProviderApi;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export interface MakaiAgentModelsApi extends MakaiAgentApi {
|
|
13
|
+
models: MakaiModelsApi;
|
|
14
|
+
}
|
|
15
|
+
export type CreateMakaiClientOptions = MakaiClientOptions & {
|
|
16
|
+
command?: string;
|
|
17
|
+
args?: string[];
|
|
18
|
+
cwd?: string;
|
|
19
|
+
env?: NodeJS.ProcessEnv;
|
|
20
|
+
resolver?: BinaryResolverOptions;
|
|
21
|
+
handshakeTimeoutMs?: number;
|
|
22
|
+
};
|
|
23
|
+
export declare function createMakaiClient(options?: CreateMakaiClientOptions): Promise<MakaiClient>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMakaiClient = createMakaiClient;
|
|
4
|
+
const oap_client_1 = require("./oap_client");
|
|
5
|
+
async function createMakaiClient(options = {}) {
|
|
6
|
+
return (0, oap_client_1.createOapClient)(options);
|
|
7
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { ApiId, ProviderId } from "./models_types";
|
|
2
|
+
import type { AuthFlowHandlers } from "./auth_protocol";
|
|
3
|
+
import type { TimeoutDiagnostics } from "./timeout_diagnostics";
|
|
4
|
+
import type { MakaiLogger } from "./logger";
|
|
5
|
+
export type AuthRetryPolicy = "manual" | "auto_once";
|
|
6
|
+
export type StopReason = "end_turn" | "max_tokens" | "tool_use" | "stop_sequence" | "max_turns" | string;
|
|
7
|
+
export type TextContentPart = {
|
|
8
|
+
type: "text";
|
|
9
|
+
text: string;
|
|
10
|
+
text_signature?: string;
|
|
11
|
+
};
|
|
12
|
+
export type ThinkingContentPart = {
|
|
13
|
+
type: "thinking";
|
|
14
|
+
thinking: string;
|
|
15
|
+
thinking_signature?: string;
|
|
16
|
+
};
|
|
17
|
+
export type ImageContentPart = {
|
|
18
|
+
type: "image";
|
|
19
|
+
data: string;
|
|
20
|
+
mime_type: string;
|
|
21
|
+
};
|
|
22
|
+
export type ToolCallContentPart = {
|
|
23
|
+
type: "tool_call";
|
|
24
|
+
tool_call_id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
arguments_json: string;
|
|
27
|
+
carry?: string;
|
|
28
|
+
};
|
|
29
|
+
export type ToolResultContentPart = {
|
|
30
|
+
type: "tool_result";
|
|
31
|
+
tool_call_id: string;
|
|
32
|
+
tool_name: string;
|
|
33
|
+
content: string | TextContentPart[];
|
|
34
|
+
is_error?: boolean;
|
|
35
|
+
details_json?: string;
|
|
36
|
+
};
|
|
37
|
+
export type ContentPart = TextContentPart | ThinkingContentPart | ImageContentPart | ToolCallContentPart | ToolResultContentPart;
|
|
38
|
+
export interface ChatMessage {
|
|
39
|
+
role: "system" | "developer" | "user" | "assistant" | "tool";
|
|
40
|
+
content: string | ContentPart[];
|
|
41
|
+
name?: string;
|
|
42
|
+
tool_call_id?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface ToolDefinition {
|
|
45
|
+
name: string;
|
|
46
|
+
description: string;
|
|
47
|
+
parameters_schema_json: string;
|
|
48
|
+
execute?: (args: Record<string, unknown>, context: {
|
|
49
|
+
tool_call_id: string;
|
|
50
|
+
tool_name: string;
|
|
51
|
+
args_json: string;
|
|
52
|
+
}) => Promise<string | TextContentPart[]> | string | TextContentPart[];
|
|
53
|
+
}
|
|
54
|
+
export interface RunOptions {
|
|
55
|
+
temperature?: number;
|
|
56
|
+
max_tokens?: number;
|
|
57
|
+
reasoning_effort?: "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
58
|
+
auth_retry_policy?: AuthRetryPolicy;
|
|
59
|
+
session_id?: string;
|
|
60
|
+
metadata?: Record<string, string>;
|
|
61
|
+
signal?: AbortSignal;
|
|
62
|
+
}
|
|
63
|
+
export type UsageCountField = "input_tokens" | "output_tokens" | "total_tokens" | "cache_read_input_tokens" | "cache_write_input_tokens" | "reasoning_tokens";
|
|
64
|
+
export interface UnrepresentableUsageCount {
|
|
65
|
+
code: "unrepresentable_count";
|
|
66
|
+
field: UsageCountField;
|
|
67
|
+
value: string;
|
|
68
|
+
}
|
|
69
|
+
export interface UsageSummary {
|
|
70
|
+
input?: number;
|
|
71
|
+
output?: number;
|
|
72
|
+
cache_read?: number;
|
|
73
|
+
cache_write?: number;
|
|
74
|
+
reasoning?: number;
|
|
75
|
+
total?: number;
|
|
76
|
+
unrepresentable_counts?: UnrepresentableUsageCount[];
|
|
77
|
+
}
|
|
78
|
+
export interface AgentRunRequest {
|
|
79
|
+
model_ref: string;
|
|
80
|
+
messages: ChatMessage[];
|
|
81
|
+
tools?: ToolDefinition[];
|
|
82
|
+
options?: RunOptions;
|
|
83
|
+
}
|
|
84
|
+
export interface CompletionResponse {
|
|
85
|
+
message: {
|
|
86
|
+
role: "assistant";
|
|
87
|
+
content: string | ContentPart[];
|
|
88
|
+
};
|
|
89
|
+
usage?: UsageSummary;
|
|
90
|
+
provider_id: ProviderId;
|
|
91
|
+
api: ApiId;
|
|
92
|
+
model_id: string;
|
|
93
|
+
stop_reason?: StopReason;
|
|
94
|
+
error_message?: string;
|
|
95
|
+
}
|
|
96
|
+
export type AgentRunResponse = CompletionResponse;
|
|
97
|
+
export type ProviderStreamEvent = {
|
|
98
|
+
type: "message_start";
|
|
99
|
+
provider_id?: ProviderId;
|
|
100
|
+
api?: ApiId;
|
|
101
|
+
model_id?: string;
|
|
102
|
+
} | {
|
|
103
|
+
type: "text_delta";
|
|
104
|
+
delta: string;
|
|
105
|
+
} | {
|
|
106
|
+
type: "thinking_delta";
|
|
107
|
+
delta: string;
|
|
108
|
+
} | {
|
|
109
|
+
type: "tool_call";
|
|
110
|
+
name: string;
|
|
111
|
+
arguments_json: string;
|
|
112
|
+
tool_call_id: string;
|
|
113
|
+
} | {
|
|
114
|
+
type: "message_end";
|
|
115
|
+
usage?: UsageSummary;
|
|
116
|
+
stop_reason?: StopReason;
|
|
117
|
+
error_message?: string;
|
|
118
|
+
message?: {
|
|
119
|
+
role: "assistant";
|
|
120
|
+
content: string | ContentPart[];
|
|
121
|
+
};
|
|
122
|
+
} | {
|
|
123
|
+
type: "error";
|
|
124
|
+
message: string;
|
|
125
|
+
code?: string;
|
|
126
|
+
provider_id?: string;
|
|
127
|
+
};
|
|
128
|
+
export type AgentStreamEvent = ProviderStreamEvent | {
|
|
129
|
+
type: "agent_start";
|
|
130
|
+
session_id?: string;
|
|
131
|
+
} | {
|
|
132
|
+
type: "agent_end";
|
|
133
|
+
stop_reason?: StopReason;
|
|
134
|
+
usage?: UsageSummary;
|
|
135
|
+
error_message?: string;
|
|
136
|
+
provider_id?: ProviderId;
|
|
137
|
+
api?: ApiId;
|
|
138
|
+
model_id?: string;
|
|
139
|
+
message?: {
|
|
140
|
+
role: "assistant";
|
|
141
|
+
content: string | ContentPart[];
|
|
142
|
+
};
|
|
143
|
+
} | {
|
|
144
|
+
type: "turn_start";
|
|
145
|
+
} | {
|
|
146
|
+
type: "turn_end";
|
|
147
|
+
stop_reason?: StopReason;
|
|
148
|
+
error_message?: string;
|
|
149
|
+
} | {
|
|
150
|
+
type: "tool_execution_start";
|
|
151
|
+
tool_call_id: string;
|
|
152
|
+
tool_name: string;
|
|
153
|
+
} | {
|
|
154
|
+
type: "tool_execution_end";
|
|
155
|
+
tool_call_id: string;
|
|
156
|
+
is_error?: boolean;
|
|
157
|
+
result?: unknown;
|
|
158
|
+
};
|
|
159
|
+
export interface ProviderCompleteRequest {
|
|
160
|
+
model_ref: string;
|
|
161
|
+
messages: ChatMessage[];
|
|
162
|
+
tools?: ToolDefinition[];
|
|
163
|
+
options?: RunOptions;
|
|
164
|
+
}
|
|
165
|
+
export type ProviderCompleteResponse = CompletionResponse;
|
|
166
|
+
export interface MakaiAgentApi {
|
|
167
|
+
run(request: AgentRunRequest): Promise<AgentRunResponse>;
|
|
168
|
+
stream(request: AgentRunRequest): AsyncIterable<AgentStreamEvent>;
|
|
169
|
+
switchModel(sessionId: string, modelId: string): Promise<void>;
|
|
170
|
+
runSelected(sessionId: string, messages: ChatMessage[]): Promise<AgentRunResponse>;
|
|
171
|
+
streamSelected(sessionId: string, messages: ChatMessage[]): AsyncIterable<AgentStreamEvent>;
|
|
172
|
+
}
|
|
173
|
+
export interface MakaiProviderApi {
|
|
174
|
+
complete(request: ProviderCompleteRequest): Promise<ProviderCompleteResponse>;
|
|
175
|
+
stream(request: ProviderCompleteRequest): AsyncIterable<ProviderStreamEvent>;
|
|
176
|
+
}
|
|
177
|
+
export type MakaiStreamErrorKind = "provider_error" | "transport_error" | "aborted" | "unknown";
|
|
178
|
+
export declare class MakaiStreamError extends Error {
|
|
179
|
+
readonly kind: MakaiStreamErrorKind;
|
|
180
|
+
readonly code?: string;
|
|
181
|
+
readonly provider_id?: string;
|
|
182
|
+
readonly diagnostics?: TimeoutDiagnostics;
|
|
183
|
+
constructor(message: string, options?: {
|
|
184
|
+
kind?: MakaiStreamErrorKind;
|
|
185
|
+
code?: string;
|
|
186
|
+
provider_id?: string;
|
|
187
|
+
diagnostics?: TimeoutDiagnostics;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
export declare class MakaiAuthRequiredError extends MakaiStreamError {
|
|
191
|
+
readonly code = "auth_required";
|
|
192
|
+
readonly provider_id: ProviderId;
|
|
193
|
+
constructor(providerId: ProviderId, message?: string);
|
|
194
|
+
}
|
|
195
|
+
export interface MakaiClientOptions {
|
|
196
|
+
auth?: {
|
|
197
|
+
auth_retry_policy?: AuthRetryPolicy;
|
|
198
|
+
handlers?: AuthFlowHandlers;
|
|
199
|
+
};
|
|
200
|
+
responseTimeoutMs?: number;
|
|
201
|
+
idleTimeoutMs?: number;
|
|
202
|
+
frameTimeoutMs?: number;
|
|
203
|
+
logger?: MakaiLogger;
|
|
204
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MakaiAuthRequiredError = exports.MakaiStreamError = void 0;
|
|
4
|
+
class MakaiStreamError extends Error {
|
|
5
|
+
kind;
|
|
6
|
+
code;
|
|
7
|
+
provider_id;
|
|
8
|
+
diagnostics;
|
|
9
|
+
constructor(message, options = {}) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "MakaiStreamError";
|
|
12
|
+
this.kind = options.kind ?? "unknown";
|
|
13
|
+
this.code = options.code;
|
|
14
|
+
this.provider_id = options.provider_id;
|
|
15
|
+
this.diagnostics = options.diagnostics;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.MakaiStreamError = MakaiStreamError;
|
|
19
|
+
class MakaiAuthRequiredError extends MakaiStreamError {
|
|
20
|
+
code = "auth_required";
|
|
21
|
+
provider_id;
|
|
22
|
+
constructor(providerId, message = `authentication required for provider ${providerId}`) {
|
|
23
|
+
super(message, { kind: "provider_error", code: "auth_required", provider_id: providerId });
|
|
24
|
+
this.name = "MakaiAuthRequiredError";
|
|
25
|
+
this.provider_id = providerId;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.MakaiAuthRequiredError = MakaiAuthRequiredError;
|