-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathnodehost.ts
492 lines (461 loc) · 15.9 KB
/
nodehost.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import dotenv from "dotenv"
import { TextDecoder, TextEncoder } from "util"
import { lstat, readFile, unlink, writeFile } from "node:fs/promises"
import { ensureDir, exists, existsSync, remove } from "fs-extra"
import { resolve, dirname } from "node:path"
import { glob } from "glob"
import { debug, error, info, isQuiet, warn } from "./log"
import { execa } from "execa"
import { join } from "node:path"
import { createNodePath } from "./nodepath"
import { DockerManager } from "./docker"
import { createFileSystem } from "../../core/src/filesystem"
import { filterGitIgnore } from "../../core/src/gitignore"
import {
parseDefaultsFromEnv,
parseTokenFromEnv,
} from "../../core/src/connection"
import {
DEFAULT_MODEL,
DEFAULT_TEMPERATURE,
MODEL_PROVIDER_AZURE_OPENAI,
SHELL_EXEC_TIMEOUT,
MODEL_PROVIDER_OLLAMA,
TOOL_ID,
DEFAULT_EMBEDDINGS_MODEL,
DEFAULT_SMALL_MODEL,
AZURE_COGNITIVE_SERVICES_TOKEN_SCOPES,
MODEL_PROVIDER_AZURE_SERVERLESS_MODELS,
AZURE_AI_INFERENCE_TOKEN_SCOPES,
MODEL_PROVIDER_AZURE_SERVERLESS_OPENAI,
DOT_ENV_FILENAME,
DEFAULT_VISION_MODEL,
} from "../../core/src/constants"
import { tryReadText } from "../../core/src/fs"
import {
ServerManager,
ModelService,
LanguageModelConfiguration,
LogLevel,
UTF8Decoder,
UTF8Encoder,
RuntimeHost,
setRuntimeHost,
ResponseStatus,
AzureTokenResolver,
ModelConfigurations,
} from "../../core/src/host"
import { AbortSignalOptions, TraceOptions } from "../../core/src/trace"
import { logError, logVerbose } from "../../core/src/util"
import { parseModelIdentifier } from "../../core/src/models"
import { LanguageModel } from "../../core/src/chat"
import {
errorMessage,
NotSupportedError,
serializeError,
} from "../../core/src/error"
import { BrowserManager } from "./playwright"
import { shellConfirm, shellInput, shellSelect } from "./input"
import { shellQuote } from "../../core/src/shell"
import { uniq } from "es-toolkit"
import { PLimitPromiseQueue } from "../../core/src/concurrency"
import { Project } from "../../core/src/server/messages"
import { createAzureTokenResolver } from "./azuretoken"
import {
createAzureContentSafetyClient,
isAzureContentSafetyClientConfigured,
} from "../../core/src/azurecontentsafety"
import { resolveGlobalConfiguration } from "../../core/src/config"
import { HostConfiguration } from "../../core/src/hostconfiguration"
class NodeServerManager implements ServerManager {
async start(): Promise<void> {
throw new Error("not implement")
}
async close(): Promise<void> {
throw new Error("not implement")
}
}
class ModelManager implements ModelService {
private pulled: string[] = []
constructor(private readonly host: RuntimeHost) {}
private async getModelToken(modelId: string) {
const { provider } = parseModelIdentifier(modelId)
const conn = await this.host.getLanguageModelConfiguration(modelId)
if (provider === MODEL_PROVIDER_OLLAMA)
conn.base = conn.base.replace(/\/v1$/i, "")
return conn
}
async pullModel(
modelid: string,
options?: TraceOptions
): Promise<ResponseStatus> {
const { trace } = options || {}
const { provider, model } = parseModelIdentifier(modelid)
if (this.pulled.includes(modelid)) return { ok: true }
if (provider === MODEL_PROVIDER_OLLAMA) {
logVerbose(`${provider}: pull ${model}`)
try {
const conn = await this.getModelToken(modelid)
let res: Response
// OLLAMA
res = await fetch(`${conn.base}/api/pull`, {
method: "POST",
headers: {
"User-Agent": TOOL_ID,
"Content-Type": "application/json",
},
body: JSON.stringify(
{ name: model, stream: false },
null,
2
),
})
if (res.ok) {
const resj = await res.json()
//logVerbose(JSON.stringify(resj, null, 2))
}
if (res.ok) this.pulled.push(modelid)
return { ok: res.ok, status: res.status }
} catch (e) {
logError(`${provider}: failed to pull model ${model}`)
trace?.error(`${provider}: pull model failed`, e)
return { ok: false, status: 500, error: serializeError(e) }
}
}
return { ok: true }
}
}
export class NodeHost implements RuntimeHost {
private readonly dotEnvPath: string
project: Project
userState: any = {}
models: ModelService
readonly path = createNodePath()
readonly server = new NodeServerManager()
readonly workspace = createFileSystem()
readonly containers = new DockerManager()
readonly browsers = new BrowserManager()
readonly modelAliases: ModelConfigurations = {
large: { model: DEFAULT_MODEL },
small: { model: DEFAULT_SMALL_MODEL },
vision: { model: DEFAULT_VISION_MODEL },
embeddings: { model: DEFAULT_EMBEDDINGS_MODEL },
}
readonly userInputQueue = new PLimitPromiseQueue(1)
readonly azureToken: AzureTokenResolver
readonly azureServerlessToken: AzureTokenResolver
constructor(config: HostConfiguration) {
this.models = new ModelManager(this)
this.azureToken = createAzureTokenResolver(
"Azure",
"AZURE_OPENAI_TOKEN_SCOPES",
AZURE_COGNITIVE_SERVICES_TOKEN_SCOPES
)
this.azureServerlessToken = createAzureTokenResolver(
"Azure AI Serverless",
"AZURE_SERVERLESS_OPENAI_TOKEN_SCOPES",
AZURE_AI_INFERENCE_TOKEN_SCOPES
)
}
async readConfig(): Promise<HostConfiguration> {
return resolveGlobalConfiguration(this.dotEnvPath)
}
private async syncDotEnv() {
const { envFile } = await this.readConfig()
if (existsSync(envFile)) {
if (resolve(envFile) !== resolve(DOT_ENV_FILENAME))
logVerbose(`.env: loading ${envFile}`)
const res = dotenv.config({
path: envFile,
debug: !!process.env.DEBUG,
override: true,
})
if (res.error) throw res.error
}
}
static async install(dotEnvPath: string) {
const config = await resolveGlobalConfiguration(dotEnvPath)
const h = new NodeHost(config)
setRuntimeHost(h)
await h.parseDefaults()
return h
}
async readSecret(name: string): Promise<string | undefined> {
return process.env[name]
}
private async parseDefaults() {
await this.syncDotEnv()
await parseDefaultsFromEnv(process.env)
}
clientLanguageModel: LanguageModel
async getLanguageModelConfiguration(
modelId: string,
options?: { token?: boolean } & AbortSignalOptions & TraceOptions
): Promise<LanguageModelConfiguration> {
const { signal, token: askToken } = options || {}
const tok = await parseTokenFromEnv(process.env, modelId)
if (!askToken && tok?.token) tok.token = "***"
if (askToken && tok && !tok.token) {
if (
tok.provider === MODEL_PROVIDER_AZURE_OPENAI ||
tok.provider === MODEL_PROVIDER_AZURE_SERVERLESS_OPENAI
) {
const azureToken = await this.azureToken.token(
tok.azureCredentialsType,
options
)
if (!azureToken)
throw new Error(
`Azure OpenAI token not available for ${modelId}`
)
tok.token = "Bearer " + azureToken.token
} else if (
tok.provider === MODEL_PROVIDER_AZURE_SERVERLESS_MODELS
) {
const azureToken = await this.azureServerlessToken.token(
tok.azureCredentialsType,
options
)
if (!azureToken)
throw new Error(
`Azure AI token not available for ${modelId}`
)
tok.token = "Bearer " + azureToken.token
}
}
if (!tok) {
if (!modelId)
throw new Error(
"Could not determine default model from current configuration"
)
const { provider } = parseModelIdentifier(modelId)
if (provider === MODEL_PROVIDER_AZURE_OPENAI)
throw new Error(`Azure OpenAI not configured for ${modelId}`)
else if (provider === MODEL_PROVIDER_AZURE_SERVERLESS_OPENAI)
throw new Error(
`Azure AI OpenAI Serverless not configured for ${modelId}`
)
else if (provider === MODEL_PROVIDER_AZURE_SERVERLESS_MODELS)
throw new Error(`Azure AI Models not configured for ${modelId}`)
}
if (!tok && this.clientLanguageModel) {
return <LanguageModelConfiguration>{
model: modelId,
provider: this.clientLanguageModel.id,
source: "client",
}
}
return tok
}
log(level: LogLevel, msg: string): void {
if (msg === undefined) return
switch (level) {
case LogLevel.Error:
error(msg)
break
case LogLevel.Warn:
warn(msg)
break
case LogLevel.Verbose:
debug(msg)
break
case LogLevel.Info:
default:
info(msg)
break
}
}
createUTF8Decoder(): UTF8Decoder {
return new TextDecoder("utf-8")
}
createUTF8Encoder(): UTF8Encoder {
return new TextEncoder()
}
projectFolder(): string {
return this.path.resolve(".")
}
installFolder(): string {
return this.projectFolder()
}
resolvePath(...segments: string[]) {
return this.path.resolve(...segments)
}
async statFile(name: string): Promise<{
size: number
type: "file" | "directory" | "symlink"
}> {
try {
const stats = await lstat(name)
return {
size: stats.size,
type: stats.isFile()
? "file"
: stats.isDirectory()
? "directory"
: stats.isSymbolicLink()
? "symlink"
: undefined,
}
} catch (error) {
return undefined
}
}
async readFile(name: string): Promise<Uint8Array> {
const wksrx = /^workspace:\/\//i
if (wksrx.test(name))
name = join(this.projectFolder(), name.replace(wksrx, ""))
// check if file exists
if (!(await exists(name))) return undefined
// read file
const res = await readFile(name)
return res ? new Uint8Array(res) : new Uint8Array()
}
async findFiles(
path: string | string[],
options: {
ignore?: string | string[]
applyGitIgnore?: boolean
}
): Promise<string[]> {
const { ignore, applyGitIgnore } = options || {}
let files = await glob(path, {
nodir: true,
windowsPathsNoEscape: true,
ignore,
})
if (applyGitIgnore) {
const gitignore = await tryReadText(".gitignore")
files = await filterGitIgnore(gitignore, files)
}
return uniq(files)
}
async writeFile(name: string, content: Uint8Array): Promise<void> {
await ensureDir(dirname(name))
await writeFile(name, content)
}
async deleteFile(name: string) {
await unlink(name)
}
async createDirectory(name: string): Promise<void> {
await ensureDir(name)
}
async deleteDirectory(name: string): Promise<void> {
await remove(name)
}
async contentSafety(
id?: "azure",
options?: TraceOptions
): Promise<ContentSafety> {
if (!id && isAzureContentSafetyClientConfigured()) id = "azure"
if (id === "azure") {
const safety = createAzureContentSafetyClient(options)
return safety
} else if (id)
throw new NotSupportedError(`content safety ${id} not supported`)
return undefined
}
async browse(
url: string,
options?: BrowseSessionOptions & TraceOptions
): Promise<BrowserPage> {
return this.browsers.browse(url, options)
}
async exec(
containerId: string,
command: string,
args: string[],
options: ShellOptions & TraceOptions
) {
if (containerId) {
const container = await this.containers.container(containerId)
return await container.exec(command, args, options)
}
const {
trace,
label,
cwd,
timeout = SHELL_EXEC_TIMEOUT,
stdin: input,
} = options || {}
try {
trace?.startDetails(label || command)
// python3 on windows -> python
if (command === "python3" && process.platform === "win32")
command = "python"
if (command === "python" && process.platform !== "win32")
command = "python3"
const cmd = shellQuote([command, ...args])
logVerbose(`${cwd ? `${cwd}> ` : ""}${cmd}`)
trace?.itemValue(`cwd`, cwd)
trace?.item(cmd)
const { stdout, stderr, exitCode, failed } = await execa(
command,
args,
{
cleanup: true,
input,
timeout,
cwd,
preferLocal: true,
stripFinalNewline: true,
stdin: input ? undefined : "ignore",
stdout: ["pipe"],
stderr: ["pipe"],
}
)
trace?.itemValue(`exit code`, `${exitCode}`)
if (stdout) trace?.detailsFenced(`📩 stdout`, stdout)
if (stderr) trace?.detailsFenced(`📩 stderr`, stderr)
return { stdout, stderr, exitCode, failed }
} catch (err) {
trace?.error("exec failed", err)
return {
stdout: "",
stderr: errorMessage(err),
exitCode: 1,
failed: true,
}
} finally {
trace?.endDetails()
}
}
/**
* Starts a container to execute sandboxed code
* @param options
*/
async container(
options: ContainerOptions & TraceOptions
): Promise<ContainerHost> {
return await this.containers.startContainer(options)
}
async removeContainers(): Promise<void> {
await this.containers.stopAndRemove()
}
async removeBrowsers(): Promise<void> {
await this.browsers.stopAndRemove()
}
/**
* Asks the user to select between options
* @param message question to ask
* @param options options to select from
*/
async select(message: string, options: string[]): Promise<string> {
return await this.userInputQueue.add(() =>
shellSelect(message, options)
)
}
/**
* Asks the user to input a text
* @param message message to ask
*/
async input(message: string): Promise<string> {
return await this.userInputQueue.add(() => shellInput(message))
}
/**
* Asks the user to confirm a message
* @param message message to ask
*/
async confirm(message: string): Promise<boolean> {
return await this.userInputQueue.add(() => shellConfirm(message))
}
}