import OpenAI from "openai";
import path from "path";
import shell from 'shelljs'

const client = new OpenAI();

const responseHosted = await client.responses.create({
  model: "gpt-5.2",
  tools: [
    {
      type: "shell",
      environment: {
        type: "container_auto",
        skills: [
          { type: "skill_reference", skill_id: "skill_69b7c456a9408193856b890836fcd1950fd5b0453b8a6018" },
        ],
      },
    },
  ],
  input: "Use the skills to find phone number of Qiaozhen.",
});

console.log('Hosted shell response:', responseHosted.output_text);

// const conversation = [{
//     role: 'user',
//     content: "Use the mycontacts skill and run locally to find the phone number of Qiaozhen."
//   }]

// let hasShellCall = true
// let round = 0

// while(hasShellCall) {
//   hasShellCall = false
//   round++

// const responseLocal = await client.responses.create({
//   model: "gpt-5.2",
//   tools: [
//     {
//       type: "shell",
//       environment: {
//         type: "local",
//         skills: [
//           {
//             name: "mycontacts",
//             description: "My contacts info",
//             path: path.resolve("./skills/mycontacts"),
//           },
//         ],
//       },
//     },
//   ],
//   input: conversation,
// });

// console.log('Local shell response output:', JSON.stringify(responseLocal.output, null, 2));

// conversation.push(...responseLocal.output)

// responseLocal.output.forEach(item => {
//   if (item.type == 'shell_call') {
//     hasShellCall = true
//     console.log('shell call item =', item)
//     let shell_call_output = []
//     item.action.commands.forEach(command => {

//       console.log('$', command, '\n')


//       let { stdout, stderr } = shell.exec(command, {
//         silent: true
//       })
//       shell_call_output.push({
//         stdout,
//         stderr,
//         outcome: { type: 'exit', exit_code: 0 }
//       })
//     })

//     conversation.push({
//       type: 'shell_call_output',
//       call_id: item.call_id,
//       output: shell_call_output
//     })
//   }
// })

// console.log('Local shell response of round ' + round + ':', JSON.stringify(responseLocal.output_text, null, 2));


// }