afm-bridge 0.1.0
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 +348 -0
- package/dist/client.d.ts +74 -0
- package/dist/client.js +216 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +111 -0
- package/dist/errors.js +152 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/node/index.d.ts +28 -0
- package/dist/node/index.js +177 -0
- package/dist/node/index.js.map +1 -0
- package/dist/rpc.d.ts +37 -0
- package/dist/rpc.js +172 -0
- package/dist/rpc.js.map +1 -0
- package/dist/schema.d.ts +53 -0
- package/dist/schema.js +149 -0
- package/dist/schema.js.map +1 -0
- package/dist/session.d.ts +63 -0
- package/dist/session.js +202 -0
- package/dist/session.js.map +1 -0
- package/dist/tauri/index.d.ts +29 -0
- package/dist/tauri/index.js +80 -0
- package/dist/tauri/index.js.map +1 -0
- package/dist/tool.d.ts +39 -0
- package/dist/tool.js +48 -0
- package/dist/tool.js.map +1 -0
- package/dist/transport.d.ts +37 -0
- package/dist/transport.js +24 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +152 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/zod/index.d.ts +21 -0
- package/dist/zod/index.js +44 -0
- package/dist/zod/index.js.map +1 -0
- package/package.json +79 -0
- package/src/client.ts +312 -0
- package/src/errors.ts +170 -0
- package/src/index.ts +22 -0
- package/src/node/index.ts +203 -0
- package/src/rpc.ts +204 -0
- package/src/schema.ts +206 -0
- package/src/session.ts +290 -0
- package/src/tauri/index.ts +100 -0
- package/src/tool.ts +80 -0
- package/src/transport.ts +60 -0
- package/src/types.ts +142 -0
- package/src/zod/index.ts +53 -0
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { UnavailableReason } from './types.js';
|
|
2
|
+
export interface RpcErrorPayload {
|
|
3
|
+
code: number;
|
|
4
|
+
message: string;
|
|
5
|
+
data?: Record<string, unknown> & {
|
|
6
|
+
type?: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** Base class of every error thrown by afm-bridge. `type` matches PROTOCOL.md section 7. */
|
|
10
|
+
export declare class AfmError extends Error {
|
|
11
|
+
name: string;
|
|
12
|
+
readonly type: string;
|
|
13
|
+
readonly code: number | undefined;
|
|
14
|
+
readonly data: Record<string, unknown>;
|
|
15
|
+
constructor(message: string, init?: {
|
|
16
|
+
type?: string;
|
|
17
|
+
code?: number;
|
|
18
|
+
data?: Record<string, unknown>;
|
|
19
|
+
cause?: unknown;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/** Framing, version or JSON-RPC level problem between client and sidecar. */
|
|
23
|
+
export declare class ProtocolError extends AfmError {
|
|
24
|
+
name: string;
|
|
25
|
+
}
|
|
26
|
+
export declare class InvalidParamsError extends AfmError {
|
|
27
|
+
name: string;
|
|
28
|
+
}
|
|
29
|
+
/** The request was cancelled through an `AbortSignal`. Has `name === 'AbortError'` like DOM aborts. */
|
|
30
|
+
export declare class AbortError extends AfmError {
|
|
31
|
+
name: string;
|
|
32
|
+
constructor(message?: string, init?: ConstructorParameters<typeof AfmError>[1]);
|
|
33
|
+
}
|
|
34
|
+
export declare class ModelUnavailableError extends AfmError {
|
|
35
|
+
name: string;
|
|
36
|
+
get reason(): UnavailableReason;
|
|
37
|
+
}
|
|
38
|
+
export declare class UnsupportedOSError extends AfmError {
|
|
39
|
+
name: string;
|
|
40
|
+
}
|
|
41
|
+
export declare class SessionNotFoundError extends AfmError {
|
|
42
|
+
name: string;
|
|
43
|
+
}
|
|
44
|
+
/** Another `respond`/`stream` is already running in this session. */
|
|
45
|
+
export declare class SessionBusyError extends AfmError {
|
|
46
|
+
name: string;
|
|
47
|
+
}
|
|
48
|
+
export declare class ContextWindowExceededError extends AfmError {
|
|
49
|
+
name: string;
|
|
50
|
+
get contextSize(): number | undefined;
|
|
51
|
+
get tokenCount(): number | undefined;
|
|
52
|
+
}
|
|
53
|
+
export declare class GuardrailViolationError extends AfmError {
|
|
54
|
+
name: string;
|
|
55
|
+
}
|
|
56
|
+
export declare class RefusalError extends AfmError {
|
|
57
|
+
name: string;
|
|
58
|
+
}
|
|
59
|
+
export declare class UnsupportedLanguageError extends AfmError {
|
|
60
|
+
name: string;
|
|
61
|
+
get languageCode(): string | undefined;
|
|
62
|
+
}
|
|
63
|
+
export declare class UnsupportedCapabilityError extends AfmError {
|
|
64
|
+
name: string;
|
|
65
|
+
}
|
|
66
|
+
export declare class UnsupportedSchemaError extends AfmError {
|
|
67
|
+
name: string;
|
|
68
|
+
}
|
|
69
|
+
export declare class DecodingError extends AfmError {
|
|
70
|
+
name: string;
|
|
71
|
+
}
|
|
72
|
+
export declare class RateLimitedError extends AfmError {
|
|
73
|
+
name: string;
|
|
74
|
+
}
|
|
75
|
+
export declare class GenerationTimeoutError extends AfmError {
|
|
76
|
+
name: string;
|
|
77
|
+
}
|
|
78
|
+
export declare class ToolExecutionError extends AfmError {
|
|
79
|
+
name: string;
|
|
80
|
+
}
|
|
81
|
+
export declare class QuotaExceededError extends AfmError {
|
|
82
|
+
name: string;
|
|
83
|
+
}
|
|
84
|
+
export declare class NetworkError extends AfmError {
|
|
85
|
+
name: string;
|
|
86
|
+
}
|
|
87
|
+
export declare class ServiceUnavailableError extends AfmError {
|
|
88
|
+
name: string;
|
|
89
|
+
}
|
|
90
|
+
/** The sidecar process died and could not be (re)started. */
|
|
91
|
+
export declare class SidecarCrashedError extends AfmError {
|
|
92
|
+
name: string;
|
|
93
|
+
}
|
|
94
|
+
/** The session lived in a sidecar process that has since crashed or been closed. Create a new one. */
|
|
95
|
+
export declare class SessionLostError extends AfmError {
|
|
96
|
+
name: string;
|
|
97
|
+
}
|
|
98
|
+
/** Spawning the sidecar failed (binary missing, not executable, ...). */
|
|
99
|
+
export declare class TransportError extends AfmError {
|
|
100
|
+
name: string;
|
|
101
|
+
}
|
|
102
|
+
/** The client-side `timeoutMs` elapsed. */
|
|
103
|
+
export declare class RequestTimeoutError extends AfmError {
|
|
104
|
+
name: string;
|
|
105
|
+
}
|
|
106
|
+
/** `close()` was called on the client. */
|
|
107
|
+
export declare class ClientClosedError extends AfmError {
|
|
108
|
+
name: string;
|
|
109
|
+
}
|
|
110
|
+
/** Builds the typed error for a JSON-RPC error object. Unknown types become a plain `AfmError`. */
|
|
111
|
+
export declare function errorFromRpc(payload: RpcErrorPayload): AfmError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/** Base class of every error thrown by afm-bridge. `type` matches PROTOCOL.md section 7. */
|
|
2
|
+
export class AfmError extends Error {
|
|
3
|
+
name = 'AfmError';
|
|
4
|
+
type;
|
|
5
|
+
code;
|
|
6
|
+
data;
|
|
7
|
+
constructor(message, init = {}) {
|
|
8
|
+
super(message, init.cause === undefined ? undefined : { cause: init.cause });
|
|
9
|
+
this.type = init.type ?? 'generationFailed';
|
|
10
|
+
this.code = init.code;
|
|
11
|
+
this.data = init.data ?? {};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** Framing, version or JSON-RPC level problem between client and sidecar. */
|
|
15
|
+
export class ProtocolError extends AfmError {
|
|
16
|
+
name = 'ProtocolError';
|
|
17
|
+
}
|
|
18
|
+
export class InvalidParamsError extends AfmError {
|
|
19
|
+
name = 'InvalidParamsError';
|
|
20
|
+
}
|
|
21
|
+
/** The request was cancelled through an `AbortSignal`. Has `name === 'AbortError'` like DOM aborts. */
|
|
22
|
+
export class AbortError extends AfmError {
|
|
23
|
+
name = 'AbortError';
|
|
24
|
+
constructor(message = 'The operation was aborted', init = {}) {
|
|
25
|
+
super(message, { type: 'cancelled', code: -32800, ...init });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class ModelUnavailableError extends AfmError {
|
|
29
|
+
name = 'ModelUnavailableError';
|
|
30
|
+
get reason() {
|
|
31
|
+
return this.data.reason ?? 'unknown';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class UnsupportedOSError extends AfmError {
|
|
35
|
+
name = 'UnsupportedOSError';
|
|
36
|
+
}
|
|
37
|
+
export class SessionNotFoundError extends AfmError {
|
|
38
|
+
name = 'SessionNotFoundError';
|
|
39
|
+
}
|
|
40
|
+
/** Another `respond`/`stream` is already running in this session. */
|
|
41
|
+
export class SessionBusyError extends AfmError {
|
|
42
|
+
name = 'SessionBusyError';
|
|
43
|
+
}
|
|
44
|
+
export class ContextWindowExceededError extends AfmError {
|
|
45
|
+
name = 'ContextWindowExceededError';
|
|
46
|
+
get contextSize() {
|
|
47
|
+
return this.data.contextSize;
|
|
48
|
+
}
|
|
49
|
+
get tokenCount() {
|
|
50
|
+
return this.data.tokenCount;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export class GuardrailViolationError extends AfmError {
|
|
54
|
+
name = 'GuardrailViolationError';
|
|
55
|
+
}
|
|
56
|
+
export class RefusalError extends AfmError {
|
|
57
|
+
name = 'RefusalError';
|
|
58
|
+
}
|
|
59
|
+
export class UnsupportedLanguageError extends AfmError {
|
|
60
|
+
name = 'UnsupportedLanguageError';
|
|
61
|
+
get languageCode() {
|
|
62
|
+
return this.data.languageCode;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export class UnsupportedCapabilityError extends AfmError {
|
|
66
|
+
name = 'UnsupportedCapabilityError';
|
|
67
|
+
}
|
|
68
|
+
export class UnsupportedSchemaError extends AfmError {
|
|
69
|
+
name = 'UnsupportedSchemaError';
|
|
70
|
+
}
|
|
71
|
+
export class DecodingError extends AfmError {
|
|
72
|
+
name = 'DecodingError';
|
|
73
|
+
}
|
|
74
|
+
export class RateLimitedError extends AfmError {
|
|
75
|
+
name = 'RateLimitedError';
|
|
76
|
+
}
|
|
77
|
+
export class GenerationTimeoutError extends AfmError {
|
|
78
|
+
name = 'GenerationTimeoutError';
|
|
79
|
+
}
|
|
80
|
+
export class ToolExecutionError extends AfmError {
|
|
81
|
+
name = 'ToolExecutionError';
|
|
82
|
+
}
|
|
83
|
+
export class QuotaExceededError extends AfmError {
|
|
84
|
+
name = 'QuotaExceededError';
|
|
85
|
+
}
|
|
86
|
+
export class NetworkError extends AfmError {
|
|
87
|
+
name = 'NetworkError';
|
|
88
|
+
}
|
|
89
|
+
export class ServiceUnavailableError extends AfmError {
|
|
90
|
+
name = 'ServiceUnavailableError';
|
|
91
|
+
}
|
|
92
|
+
// Client-only errors (never sent over the wire).
|
|
93
|
+
/** The sidecar process died and could not be (re)started. */
|
|
94
|
+
export class SidecarCrashedError extends AfmError {
|
|
95
|
+
name = 'SidecarCrashedError';
|
|
96
|
+
}
|
|
97
|
+
/** The session lived in a sidecar process that has since crashed or been closed. Create a new one. */
|
|
98
|
+
export class SessionLostError extends AfmError {
|
|
99
|
+
name = 'SessionLostError';
|
|
100
|
+
}
|
|
101
|
+
/** Spawning the sidecar failed (binary missing, not executable, ...). */
|
|
102
|
+
export class TransportError extends AfmError {
|
|
103
|
+
name = 'TransportError';
|
|
104
|
+
}
|
|
105
|
+
/** The client-side `timeoutMs` elapsed. */
|
|
106
|
+
export class RequestTimeoutError extends AfmError {
|
|
107
|
+
name = 'RequestTimeoutError';
|
|
108
|
+
}
|
|
109
|
+
/** `close()` was called on the client. */
|
|
110
|
+
export class ClientClosedError extends AfmError {
|
|
111
|
+
name = 'ClientClosedError';
|
|
112
|
+
}
|
|
113
|
+
const byType = {
|
|
114
|
+
parseError: ProtocolError,
|
|
115
|
+
invalidRequest: ProtocolError,
|
|
116
|
+
methodNotFound: ProtocolError,
|
|
117
|
+
protocolVersionMismatch: ProtocolError,
|
|
118
|
+
notInitialized: ProtocolError,
|
|
119
|
+
invalidParams: InvalidParamsError,
|
|
120
|
+
cancelled: AbortError,
|
|
121
|
+
modelUnavailable: ModelUnavailableError,
|
|
122
|
+
unsupportedOS: UnsupportedOSError,
|
|
123
|
+
sessionNotFound: SessionNotFoundError,
|
|
124
|
+
sessionBusy: SessionBusyError,
|
|
125
|
+
contextWindowExceeded: ContextWindowExceededError,
|
|
126
|
+
guardrailViolation: GuardrailViolationError,
|
|
127
|
+
refusal: RefusalError,
|
|
128
|
+
unsupportedLanguage: UnsupportedLanguageError,
|
|
129
|
+
unsupportedCapability: UnsupportedCapabilityError,
|
|
130
|
+
unsupportedSchema: UnsupportedSchemaError,
|
|
131
|
+
decodingFailure: DecodingError,
|
|
132
|
+
rateLimited: RateLimitedError,
|
|
133
|
+
timeout: GenerationTimeoutError,
|
|
134
|
+
toolExecution: ToolExecutionError,
|
|
135
|
+
quotaExceeded: QuotaExceededError,
|
|
136
|
+
networkFailure: NetworkError,
|
|
137
|
+
serviceUnavailable: ServiceUnavailableError,
|
|
138
|
+
};
|
|
139
|
+
/** Builds the typed error for a JSON-RPC error object. Unknown types become a plain `AfmError`. */
|
|
140
|
+
export function errorFromRpc(payload) {
|
|
141
|
+
const { type = 'unknown', ...data } = payload.data ?? {};
|
|
142
|
+
if (type === 'assetsUnavailable') {
|
|
143
|
+
return new ModelUnavailableError(payload.message, {
|
|
144
|
+
type,
|
|
145
|
+
code: payload.code,
|
|
146
|
+
data: { ...data, reason: 'modelNotReady' },
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const Class = byType[type] ?? AfmError;
|
|
150
|
+
return new Class(payload.message, { type, code: payload.code, data });
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAQA,4FAA4F;AAC5F,MAAM,OAAO,QAAS,SAAQ,KAAK;IACxB,IAAI,GAAG,UAAU,CAAA;IACjB,IAAI,CAAQ;IACZ,IAAI,CAAoB;IACxB,IAAI,CAAyB;IAEtC,YACE,OAAe,EACf,IAAI,GAAsF,EAAE;QAE5F,KAAK,CAAC,OAAO,E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AG,SAAS,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,CAAA;IACxD,IAAI,IAAI,KAAK,mBAAmB,EAAE,CAAC;QACjC,OAAO,IAAI,qBAAqB,CAAC,OAAO,CAAC,OAAO,EAAE;YAChD,IAAI;YACJ,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,IAAI,EAAE,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,eAAe,EAAE;SAC3C,CAAC,CAAA;IACJ,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAA;IACtC,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;AACvE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { AppleFoundationModels, type AppleFoundationModelsOptions, CLIENT_VERSION, PROTOCOL_VERSION, } from './client.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
|
+
export { type DeepPartial, isStructuredSchema, type JSONSchema, jsonSchema, type SchemaInput, type StructuredSchema, validate, } from './schema.js';
|
|
4
|
+
export { Session } from './session.js';
|
|
5
|
+
export { type Tool, type ToolContext, tool } from './tool.js';
|
|
6
|
+
export type { CloseInfo, Connection, Transport, TransportHandlers } from './transport.js';
|
|
7
|
+
export { createLineSplitter } from './transport.js';
|
|
8
|
+
export type * from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Core entry point: no Node APIs, safe to import in a browser or webview.
|
|
2
|
+
export { AppleFoundationModels, CLIENT_VERSION, PROTOCOL_VERSION, } from './client.js';
|
|
3
|
+
export * from './errors.js';
|
|
4
|
+
export { isStructuredSchema, jsonSchema, validate, } from './schema.js';
|
|
5
|
+
export { Session } from './session.js';
|
|
6
|
+
export { tool } from './tool.js';
|
|
7
|
+
export { createLineSplitter } from './transport.js';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,OAAO,EACL,qBAAqB,EAErB,cAAc,EACd,gBAAgB,GACjB,MAAM,aAAa,CAAA;AACpB,cAAc,aAAa,CAAA;AAC3B,OAAO,EAEL,kBAAkB,EAElB,UAAU,EAGV,QAAQ,GACT,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAA+B,IAAI,EAAE,MAAM,WAAW,CAAA;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Connection, type Transport, type TransportHandlers } from '../transport.js';
|
|
2
|
+
export interface NodeStdioTransportOptions {
|
|
3
|
+
/** Path to `afm-bridge-server`. Default: `resolveSidecarPath()`. */
|
|
4
|
+
binaryPath?: string;
|
|
5
|
+
args?: string[];
|
|
6
|
+
/** Extra environment variables for the sidecar. */
|
|
7
|
+
env?: Record<string, string | undefined>;
|
|
8
|
+
/** Sidecar log level (stderr). Default `warn`. */
|
|
9
|
+
logLevel?: 'error' | 'warn' | 'info' | 'debug';
|
|
10
|
+
/** How long `close()` waits for a graceful exit before killing. Default 2000 ms. */
|
|
11
|
+
shutdownTimeoutMs?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Runs the sidecar as a child process and talks over stdin/stdout.
|
|
15
|
+
* Works in Electron's main process, plain Node and CLI tools.
|
|
16
|
+
*/
|
|
17
|
+
export declare class NodeStdioTransport implements Transport {
|
|
18
|
+
private readonly options;
|
|
19
|
+
constructor(options?: NodeStdioTransportOptions);
|
|
20
|
+
connect(handlers: TransportHandlers): Promise<Connection>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Finds the `afm-bridge-server` binary. Order:
|
|
24
|
+
* 1. `AFM_BRIDGE_BINARY` environment variable
|
|
25
|
+
* 2. Packaged Electron app: `<app>/Contents/Resources/afm-bridge-server` (`process.resourcesPath`)
|
|
26
|
+
* 3. The `afm-bridge-darwin-arm64` package (with `app.asar` rewritten to `app.asar.unpacked`)
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveSidecarPath(): string;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { accessSync, chmodSync, constants, existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { ModelUnavailableError, TransportError } from '../errors.js';
|
|
6
|
+
import { createLineSplitter } from '../transport.js';
|
|
7
|
+
/**
|
|
8
|
+
* Runs the sidecar as a child process and talks over stdin/stdout.
|
|
9
|
+
* Works in Electron's main process, plain Node and CLI tools.
|
|
10
|
+
*/
|
|
11
|
+
export class NodeStdioTransport {
|
|
12
|
+
options;
|
|
13
|
+
constructor(options = {}) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
async connect(handlers) {
|
|
17
|
+
assertSupportedPlatform();
|
|
18
|
+
const binary = this.options.binaryPath ?? resolveSidecarPath();
|
|
19
|
+
const child = spawn(binary, this.options.args ?? [], {
|
|
20
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
21
|
+
env: { ...process.env, AFM_BRIDGE_LOG: this.options.logLevel ?? 'warn', ...this.options.env },
|
|
22
|
+
});
|
|
23
|
+
await new Promise((resolve, reject) => {
|
|
24
|
+
child.once('spawn', resolve);
|
|
25
|
+
child.once('error', (error) => reject(new TransportError(`Could not start afm-bridge-server at ${binary}: ${error.message}`, {
|
|
26
|
+
type: 'transportError',
|
|
27
|
+
cause: error,
|
|
28
|
+
})));
|
|
29
|
+
});
|
|
30
|
+
let exited = false;
|
|
31
|
+
const exit = new Promise((resolve) => {
|
|
32
|
+
child.once('close', (code, signal) => {
|
|
33
|
+
exited = true;
|
|
34
|
+
stdout.flush();
|
|
35
|
+
stderr.flush();
|
|
36
|
+
handlers.onClose({ code, signal });
|
|
37
|
+
resolve();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
// Errors after spawn (e.g. EPIPE on a dead process) surface through 'close'.
|
|
41
|
+
child.on('error', () => { });
|
|
42
|
+
child.stdin?.on('error', () => { });
|
|
43
|
+
const stdout = createLineSplitter((line) => handlers.onMessage(line));
|
|
44
|
+
const stderr = createLineSplitter((line) => handlers.onLog?.(line));
|
|
45
|
+
child.stdout?.setEncoding('utf8').on('data', (chunk) => stdout.push(chunk));
|
|
46
|
+
child.stderr?.setEncoding('utf8').on('data', (chunk) => stderr.push(chunk));
|
|
47
|
+
const shutdownTimeoutMs = this.options.shutdownTimeoutMs ?? 2000;
|
|
48
|
+
return {
|
|
49
|
+
send(line) {
|
|
50
|
+
if (!exited && child.stdin?.writable)
|
|
51
|
+
child.stdin.write(`${line}\n`);
|
|
52
|
+
},
|
|
53
|
+
async close() {
|
|
54
|
+
if (exited)
|
|
55
|
+
return;
|
|
56
|
+
setActive(child, true);
|
|
57
|
+
child.stdin?.end();
|
|
58
|
+
if (await settlesWithin(exit, shutdownTimeoutMs))
|
|
59
|
+
return;
|
|
60
|
+
child.kill('SIGTERM');
|
|
61
|
+
if (await settlesWithin(exit, 1000))
|
|
62
|
+
return;
|
|
63
|
+
child.kill('SIGKILL');
|
|
64
|
+
await exit;
|
|
65
|
+
},
|
|
66
|
+
setActive(active) {
|
|
67
|
+
if (!exited)
|
|
68
|
+
setActive(child, active);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Finds the `afm-bridge-server` binary. Order:
|
|
75
|
+
* 1. `AFM_BRIDGE_BINARY` environment variable
|
|
76
|
+
* 2. Packaged Electron app: `<app>/Contents/Resources/afm-bridge-server` (`process.resourcesPath`)
|
|
77
|
+
* 3. The `afm-bridge-darwin-arm64` package (with `app.asar` rewritten to `app.asar.unpacked`)
|
|
78
|
+
*/
|
|
79
|
+
export function resolveSidecarPath() {
|
|
80
|
+
const tried = [];
|
|
81
|
+
const usable = (candidate) => {
|
|
82
|
+
tried.push(candidate);
|
|
83
|
+
try {
|
|
84
|
+
accessSync(candidate, constants.X_OK);
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const fromEnv = process.env.AFM_BRIDGE_BINARY;
|
|
92
|
+
if (fromEnv) {
|
|
93
|
+
if (usable(fromEnv))
|
|
94
|
+
return fromEnv;
|
|
95
|
+
throw notFound(tried, 'AFM_BRIDGE_BINARY points to a file that does not exist or is not executable.');
|
|
96
|
+
}
|
|
97
|
+
const resourcesPath = process.resourcesPath;
|
|
98
|
+
if (resourcesPath) {
|
|
99
|
+
const packaged = join(resourcesPath, 'afm-bridge-server');
|
|
100
|
+
if (usable(packaged))
|
|
101
|
+
return packaged;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const fromPackage = createRequire(import.meta.url).resolve('afm-bridge-darwin-arm64/bin/afm-bridge-server');
|
|
105
|
+
const unpacked = fromPackage.replace(/app\.asar([\\/])/, 'app.asar.unpacked$1');
|
|
106
|
+
if (unpacked !== fromPackage && usable(unpacked))
|
|
107
|
+
return unpacked;
|
|
108
|
+
if (usable(fromPackage))
|
|
109
|
+
return fromPackage;
|
|
110
|
+
// Some package managers drop the executable bit when installing; restore it.
|
|
111
|
+
if (unpacked === fromPackage && existsSync(fromPackage)) {
|
|
112
|
+
try {
|
|
113
|
+
chmodSync(fromPackage, 0o755);
|
|
114
|
+
if (usable(fromPackage))
|
|
115
|
+
return fromPackage;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// Read-only location; fall through to the error below.
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// Package not installed (not on macOS arm64, or optional dependencies were skipped).
|
|
124
|
+
}
|
|
125
|
+
throw notFound(tried);
|
|
126
|
+
}
|
|
127
|
+
function notFound(tried, hint) {
|
|
128
|
+
return new TransportError([
|
|
129
|
+
'afm-bridge-server binary not found.',
|
|
130
|
+
hint,
|
|
131
|
+
tried.length > 0 ? `Tried: ${tried.join(', ')}.` : undefined,
|
|
132
|
+
'Fix: install afm-bridge-darwin-arm64 (npm adds it automatically on Apple silicon), build it with `swift build -c release` and pass `binaryPath`, or set AFM_BRIDGE_BINARY.',
|
|
133
|
+
]
|
|
134
|
+
.filter(Boolean)
|
|
135
|
+
.join(' '), { type: 'transportError' });
|
|
136
|
+
}
|
|
137
|
+
/** Throws `ModelUnavailableError` on platforms where the sidecar cannot run. */
|
|
138
|
+
function assertSupportedPlatform() {
|
|
139
|
+
if (process.platform !== 'darwin' || process.arch !== 'arm64') {
|
|
140
|
+
throw new ModelUnavailableError(`Apple Foundation Models need macOS on Apple silicon (this is ${process.platform}/${process.arch})`, { type: 'modelUnavailable', data: { model: 'on-device', reason: 'unsupportedPlatform' } });
|
|
141
|
+
}
|
|
142
|
+
const version = macOSVersion();
|
|
143
|
+
if (version !== undefined && version < 26) {
|
|
144
|
+
throw new ModelUnavailableError(`Apple Foundation Models need macOS 26 or newer (this is macOS ${version})`, {
|
|
145
|
+
type: 'modelUnavailable',
|
|
146
|
+
data: { model: 'on-device', reason: 'unsupportedOS' },
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function macOSVersion() {
|
|
151
|
+
try {
|
|
152
|
+
const plist = readFileSync('/System/Library/CoreServices/SystemVersion.plist', 'utf8');
|
|
153
|
+
const match = /<key>ProductVersion<\/key>\s*<string>(\d+)/.exec(plist);
|
|
154
|
+
return match?.[1] ? Number(match[1]) : undefined;
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function setActive(child, active) {
|
|
161
|
+
for (const handle of [child, child.stdin, child.stdout, child.stderr]) {
|
|
162
|
+
if (active)
|
|
163
|
+
handle?.ref?.();
|
|
164
|
+
else
|
|
165
|
+
handle?.unref?.();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function settlesWithin(promise, ms) {
|
|
169
|
+
let timer;
|
|
170
|
+
const timeout = new Promise((resolve) => {
|
|
171
|
+
timer = setTimeout(() => resolve(false), ms);
|
|
172
|
+
});
|
|
173
|
+
const result = await Promise.race([promise.then(() => true), timeout]);
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC7D,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACpF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAmB,kBAAkB,EAA0C,MAAM,iBAAiB,CAAA;AAc7G;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IACA,OAAO;IAApC,YAA6B,OAAO,GAA8B,EAAE;uBAAvC,OAAO;IAAmC,CAAC;IAExE,KAAK,CAAC,OAAO,CAAC,QAA2B;QACvC,uBAAuB,EAAE,CAAA;QACzB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,IAAI,kBAAkB,EAAE,CAAA;QAC9D,MAAM,KAAK,G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AAO,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAA;IAC9C,CAAC,CAAC,CAAA;IACF,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC,CAAA;IACtE,YAAY,CAAC,KAAK,CAAC,CAAA;IACnB,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/dist/rpc.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Connection } from './transport.js';
|
|
2
|
+
export interface RequestOptions {
|
|
3
|
+
signal?: AbortSignal | undefined;
|
|
4
|
+
timeoutMs?: number | undefined;
|
|
5
|
+
/** Called with the request id right before the request is written. */
|
|
6
|
+
onStart?: (id: number) => void;
|
|
7
|
+
/** How long to wait for the peer to confirm a cancel before rejecting anyway. Default 5000 ms. */
|
|
8
|
+
cancelGraceMs?: number;
|
|
9
|
+
}
|
|
10
|
+
type IncomingRequestHandler = (method: string, params: unknown, signal: AbortSignal) => Promise<unknown>;
|
|
11
|
+
/** JSON-RPC 2.0 peer over one connection. Both sides can send requests. */
|
|
12
|
+
export declare class RpcPeer {
|
|
13
|
+
private readonly connection;
|
|
14
|
+
private readonly handlers;
|
|
15
|
+
private nextId;
|
|
16
|
+
private readonly pending;
|
|
17
|
+
private readonly incoming;
|
|
18
|
+
private closedError;
|
|
19
|
+
constructor(connection: Connection, handlers: {
|
|
20
|
+
onNotification(method: string, params: unknown): void;
|
|
21
|
+
onRequest?: IncomingRequestHandler;
|
|
22
|
+
onPendingChange?(count: number): void;
|
|
23
|
+
onProtocolError?(error: Error): void;
|
|
24
|
+
});
|
|
25
|
+
get pendingCount(): number;
|
|
26
|
+
request<T>(method: string, params: unknown, options?: RequestOptions): Promise<T>;
|
|
27
|
+
notify(method: string, params: unknown): void;
|
|
28
|
+
/** Feeds one line received from the connection. */
|
|
29
|
+
handleLine(line: string): void;
|
|
30
|
+
/** Rejects everything in flight; later calls reject with the same error. */
|
|
31
|
+
close(error: Error): void;
|
|
32
|
+
private handleNotification;
|
|
33
|
+
private handleIncomingRequest;
|
|
34
|
+
private settle;
|
|
35
|
+
private write;
|
|
36
|
+
}
|
|
37
|
+
export {};
|