-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathcoreInterop.ts
More file actions
242 lines (201 loc) · 9.67 KB
/
coreInterop.ts
File metadata and controls
242 lines (201 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import koffi from 'koffi';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
import { Configuration } from '../configuration.js';
koffi.struct('RequestBuffer', {
Command: 'char*',
CommandLength: 'int32_t',
Data: 'char*',
DataLength: 'int32_t',
});
koffi.struct('ResponseBuffer', {
Data: 'void*',
DataLength: 'int32_t',
Error: 'void*',
ErrorLength: 'int32_t',
});
// Extended request struct for binary data (audio streaming)
koffi.struct('StreamingRequestBuffer', {
Command: 'char*',
CommandLength: 'int32_t',
Data: 'char*', // JSON params
DataLength: 'int32_t',
BinaryData: 'void*', // raw PCM audio bytes
BinaryDataLength: 'int32_t',
});
const CallbackType = koffi.proto('int32_t CallbackType(void *data, int32_t length, void *userData)');
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export class CoreInterop {
private lib: any;
private execute_command: any;
private execute_command_with_callback: any;
private execute_command_with_binary: any = null;
private static _getLibraryExtension(): string {
const platform = process.platform;
if (platform === 'win32') return '.dll';
if (platform === 'linux') return '.so';
if (platform === 'darwin') return '.dylib';
throw new Error(`Unsupported platform: ${platform}`);
}
private static _resolveDefaultCorePath(config: Configuration): string | null {
const require = createRequire(import.meta.url);
const platform = process.platform;
const arch = process.arch;
// Matches names generated by preinstall.cjs
const packageName = `@foundry-local-core/${platform}-${arch}`;
// Resolve the package path.
const packagePath = require.resolve(`${packageName}/package.json`);
const packageDir = path.dirname(packagePath);
const ext = CoreInterop._getLibraryExtension();
const corePath = path.join(packageDir, `Microsoft.AI.Foundry.Local.Core${ext}`);
if (fs.existsSync(corePath)) {
config.params['FoundryLocalCorePath'] = corePath;
// Auto-detect if WinML Bootstrap is needed by checking for Bootstrap DLL in FoundryLocalCorePath
// Only auto-set if the user hasn't explicitly provided a value
if (!('Bootstrap' in config.params)) {
const bootstrapDllPath = path.join(packageDir, 'Microsoft.WindowsAppRuntime.Bootstrap.dll');
if (fs.existsSync(bootstrapDllPath)) {
// WinML Bootstrap DLL found, enable bootstrapping
config.params['Bootstrap'] = 'true';
}
}
return corePath;
}
return null;
}
private _toBytes(str: string): Uint8Array {
return new TextEncoder().encode(str);
}
constructor(config: Configuration) {
const corePath = config.params['FoundryLocalCorePath'] || CoreInterop._resolveDefaultCorePath(config);
if (!corePath) {
throw new Error("FoundryLocalCorePath not specified in configuration and could not auto-discover binaries. Please run 'npm install' to download native libraries.");
}
const coreDir = path.dirname(corePath);
const ext = CoreInterop._getLibraryExtension();
// On Windows, explicitly load dependencies to work around DLL resolution challenges
if (process.platform === 'win32') {
koffi.load(path.join(coreDir, `onnxruntime${ext}`));
koffi.load(path.join(coreDir, `onnxruntime-genai${ext}`));
process.env.PATH = `${coreDir};${process.env.PATH}`;
}
this.lib = koffi.load(corePath);
this.execute_command = this.lib.func('void execute_command(RequestBuffer *request, _Inout_ ResponseBuffer *response)');
this.execute_command_with_callback = this.lib.func('void execute_command_with_callback(RequestBuffer *request, _Inout_ ResponseBuffer *response, CallbackType *callback, void *userData)');
this.execute_command_with_binary = this.lib.func('void execute_command_with_binary(StreamingRequestBuffer *request, _Inout_ ResponseBuffer *response)');
}
public executeCommand(command: string, params?: any): string {
const cmdBuf = koffi.alloc('char', command.length + 1);
koffi.encode(cmdBuf, 'char', command, command.length + 1);
const dataStr = params ? JSON.stringify(params) : '';
const dataBytes = this._toBytes(dataStr);
const dataBuf = koffi.alloc('char', dataBytes.length + 1);
koffi.encode(dataBuf, 'char', dataStr, dataBytes.length + 1);
const req = {
Command: koffi.address(cmdBuf),
CommandLength: command.length,
Data: koffi.address(dataBuf),
DataLength: dataBytes.length
};
const res = { Data: 0, DataLength: 0, Error: 0, ErrorLength: 0 };
this.execute_command(req, res);
try {
if (res.Error) {
const errorMsg = koffi.decode(res.Error, 'char', res.ErrorLength);
throw new Error(`Command '${command}' failed: ${errorMsg}`);
}
return res.Data ? koffi.decode(res.Data, 'char', res.DataLength) : "";
} finally {
// Free the heap-allocated response strings using koffi.free()
// Docs: https://koffi.dev/pointers/#disposable-types
if (res.Data) koffi.free(res.Data);
if (res.Error) koffi.free(res.Error);
}
}
/**
* Execute a native command with binary data (e.g., audio PCM bytes).
* Uses the execute_command_with_binary native entry point which accepts
* both JSON params and raw binary data via StreamingRequestBuffer.
*/
public executeCommandWithBinary(command: string, params: any, binaryData: Uint8Array): string {
const cmdBuf = koffi.alloc('char', command.length + 1);
koffi.encode(cmdBuf, 'char', command, command.length + 1);
const dataStr = params ? JSON.stringify(params) : '';
const dataBytes = this._toBytes(dataStr);
const dataBuf = koffi.alloc('char', dataBytes.length + 1);
koffi.encode(dataBuf, 'char', dataStr, dataBytes.length + 1);
// For binary data, use a Node.js Buffer which allocates stable external memory
// that won't be moved by V8's garbage collector during the FFI call.
const binLength = binaryData.length;
const binBuf = Buffer.from(binaryData);
// Use koffi.as to pass Buffer directly as a typed pointer
const binTypedPtr = koffi.as(binBuf, 'void *');
const req = {
Command: koffi.address(cmdBuf),
CommandLength: command.length,
Data: koffi.address(dataBuf),
DataLength: dataBytes.length,
BinaryData: binTypedPtr,
BinaryDataLength: binLength
};
const res = { Data: 0, DataLength: 0, Error: 0, ErrorLength: 0 };
this.execute_command_with_binary(req, res);
try {
if (res.Error) {
const errorMsg = koffi.decode(res.Error, 'char', res.ErrorLength);
throw new Error(`Command '${command}' failed: ${errorMsg}`);
}
return res.Data ? koffi.decode(res.Data, 'char', res.DataLength) : "";
} finally {
if (res.Data) koffi.free(res.Data);
if (res.Error) koffi.free(res.Error);
}
}
public executeCommandStreaming(command: string, params: any, callback: (chunk: string) => void): Promise<string> {
const cmdBuf = koffi.alloc('char', command.length + 1);
koffi.encode(cmdBuf, 'char', command, command.length + 1);
const dataStr = params ? JSON.stringify(params) : '';
const dataBytes = this._toBytes(dataStr);
const dataBuf = koffi.alloc('char', dataBytes.length + 1);
koffi.encode(dataBuf, 'char', dataStr, dataBytes.length + 1);
const cb = koffi.register((data: any, length: number, userData: any) => {
const chunk = koffi.decode(data, 'char', length);
callback(chunk);
return 0; // 0 = continue, 1 = cancel
}, koffi.pointer(CallbackType));
return new Promise<string>((resolve, reject) => {
const req = {
Command: koffi.address(cmdBuf),
CommandLength: command.length,
Data: koffi.address(dataBuf),
DataLength: dataBytes.length
};
const res = { Data: 0, DataLength: 0, Error: 0, ErrorLength: 0 };
this.execute_command_with_callback.async(req, res, cb, null, (err: any) => {
koffi.unregister(cb);
koffi.free(cmdBuf);
koffi.free(dataBuf);
if (err) {
reject(err);
return;
}
try {
if (res.Error) {
const errorMsg = koffi.decode(res.Error, 'char', res.ErrorLength);
reject(new Error(`Command '${command}' failed: ${errorMsg}`));
} else {
const responseData = res.Data ? koffi.decode(res.Data, 'char', res.DataLength) : '';
resolve(responseData);
}
} finally {
// Free the heap-allocated response strings using koffi.free()
if (res.Data) koffi.free(res.Data);
if (res.Error) koffi.free(res.Error);
}
});
});
}
}