Files
aitest/4-tc-shell.mjs
2026-03-16 14:55:27 +08:00

82 lines
2.9 KiB
JavaScript

import OpenAI from 'openai'
import shell from 'shelljs'
import { execSync } from 'node:child_process'
const openai = new OpenAI({
apiKey:
'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'
}) // or set environment: export OPENAI_API_KEY=...
const my = {
model: 'gpt-5.2' // hosted shell is not available in gpt-5 or gpt-5-nano/mini, so using gpt-5.2 for this example
}
console.log('==========Hosted shell environment============')
let responseHosted = await openai.responses.create({
model: my.model,
tools: [
{
type: 'shell',
environment: {
type: 'container_auto',
network_policy: {
type: 'allowlist',
allowed_domains: ['npmjs.org', 'tic.cc']
} // hosted shell has no network access by default, but you can allowlist domains as needed. The allowlist must literally be a subset of organizational allowlist configured on the web.
}
}
],
input: [
{
role: 'user',
content:
// 'In the shell, Run `node -v` at first, then npm init a project, then install the package express, and finally run npm list to show the installed version.'
'Run `node -v` and disable http.sslVerify for git. Then npm init a project ./hosted_project, then install the package git+https://git.tic.cc/open/wo_scripts, and finally run npm list to show the installed version.'
}
]
})
console.log('========== responseHosted.output =')
console.log(JSON.stringify(responseHosted.output, null, 2)) // response.output/output_text/output_parsed
//////////////////////////////////////////
console.log('==========local shell environment============')
let responseLocal = await openai.responses.create({
model: my.model,
tools: [
{
type: 'shell',
environment: { type: 'local' }
}
],
input: [
{
role: 'user',
content:
'Run npm init a project at ./local_project, then install the package git+https://git.tic.cc/open/wo_scripts, and finally run npm list to show the installed version.'
}
]
})
console.log('========== responseLocal.output =')
console.log(JSON.stringify(responseLocal.output, null, 2)) // response.output/output_text/output_parsed
responseLocal.output.forEach(item => {
if (item.type == 'shell_call') {
console.log('shell call item =', item)
item.action.commands.forEach(command => {
console.log('$', command, '\n')
let shell_call_command_output = shell.exec(command, {
silent: true
}).stdout
console.log(shell_call_command_output)
})
// 4. Provide shell command results to the model
// In this example we just log it, but in a real implementation you would likely want to send the output back to the model in another request so it can learn from the results of its commands and decide what to do next.
}
})