@flinbein/pappai-runner-codex 0.1.8 → 0.1.9

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/README.md CHANGED
@@ -46,13 +46,15 @@ When upgrading a persistent session from the `pappai_dir` tool on Codex CLI 0.17
46
46
  ## First call
47
47
 
48
48
  1. Call `$.profile.create` on the workspace or a TEAM with a name and permissions. Save the returned ID.
49
- 2. Sign in with the Codex CLI and place the resulting `auth.json` in a directory the caller can read. Call `codex.setup` on the PROFILE with `{"authFile":"/tmp/codex-auth.json"}`. The controller reads the regular file as the caller. After import, `codex.status({})` returns `{"configured":true}`. Use `codex.remove({})` to remove the stored connection.
49
+ 2. In the USER web terminal, run `pappai cli PROFILE_ID codex.authorize '{}'`. Open the verification URL and enter the device code in your browser. The selected Codex CLI runs in a temporary home. After sign-in, the helper calls `codex.setup` with the path to the new credentials and displays the result of `codex.status`. The USER needs network access and permission to run the command and call both methods. If you cancel during login, the PROFILE keeps its existing connection. Substitute the registration name for `codex` if needed.
50
50
  3. Call `$.app.create` on the workspace or a TEAM with `name`, `profile`, `permissions` and `source.manifest`. Use the [one-shot](examples/oneShot.json) or [persistent](examples/persistent.json) manifest.
51
51
  4. Call `$.profile.scope.set({"scope":["APP_ID"]})` on the PROFILE using the new APP's ID, then call `$.profile.enable({})`. Call `$.app.start({})` on the APP.
52
52
  5. Call `agent.execute` on the APP with `{"input":"Translate into Russian: the build passed"}`. The example method declares a string result. You can declare a different output JSON Schema for your own method. The runner validates the result before replying.
53
53
 
54
54
  The controller checks assignment access before an event reaches the model. Each executor obtains model credentials from its assigned profile at startup. APPs that share a profile keep separate conversation histories. Set `idleTimeoutMs` on a persistent APP to release its process between assignments.
55
55
 
56
+ For a manual import, place a Codex `auth.json` in a directory the caller can read and call `codex.setup` on the PROFILE with `{"authFile":"/tmp/codex-auth.json"}`. The controller reads the regular file as the caller. Use `codex.status({})` to check the connection and `codex.remove({})` to remove it.
57
+
56
58
  The runner supports API keys and imported ChatGPT subscription credentials. Each installation stores its connection at `profiles/<profileId>/<runnerName>/` and coordinates token refresh across its APPs. The default installation named `codex` keeps its existing history under `state/`. Other installations keep their history under `state/instances/<runnerName>/`. Each subscription worker receives only short-lived credentials. The rotation test uses the native Codex CLI and a local OAuth test server.
57
59
 
58
60
  A persistent APP can also declare `agent.getHistory` and `agent.interrupt` under `features`. History returns user messages and final assistant replies with a pagination cursor. Interrupt targets the active turn and requires permission to call the method. If no turn is active, the controller returns an error. Both methods run without a model call.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flinbein/pappai-runner-codex",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Codex app-server runner for pappai",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -0,0 +1,59 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { lstat, mkdir, mkdtemp, realpath, rm } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+
6
+ // This file is mounted by itself in the USER terminal sandbox.
7
+ export async function authorize(codex, node, runner, pappai = '/usr/local/bin/pappai') {
8
+ const root = process.env.PAPPAI_CLI_TMP;
9
+ if (!root?.startsWith('/tmp/') || await realpath(root) !== root || !(await lstat(root)).isDirectory()) {
10
+ throw Error('Missing private CLI temporary directory');
11
+ }
12
+ const home = await mkdtemp(join(root,'codex-'));
13
+ let child, cancelled = false;
14
+ const cancel = () => {
15
+ cancelled = true;
16
+ child?.kill('SIGTERM');
17
+ };
18
+ function run(command, args, env, stdio, failure) {
19
+ if (cancelled) throw Error('Authorization cancelled');
20
+ return new Promise((resolve,reject) => {
21
+ child = spawn(command,args,{cwd:home,env,stdio});
22
+ child.once('error',() => reject(Error(failure)));
23
+ child.once('close',(code) => {
24
+ child = undefined;
25
+ if (cancelled) reject(Error('Authorization cancelled'));
26
+ else if (code !== 0) reject(Error(failure));
27
+ else resolve();
28
+ });
29
+ });
30
+ }
31
+ process.on('SIGINT',cancel);
32
+ process.on('SIGTERM',cancel);
33
+ try {
34
+ const tmp = join(home,'tmp');
35
+ await mkdir(tmp,{mode:0o700});
36
+ const env = {HOME:home,CODEX_HOME:home,TMPDIR:tmp,PATH:'/usr/local/bin:/usr/bin:/bin',RUST_LOG:'off'};
37
+ await run(codex,['--no-daemon','-c','cli_auth_credentials_store="file"','login','--device-auth'],
38
+ env,'inherit','Codex device login failed');
39
+ const authFile = join(home,'auth.json');
40
+ if (!(await lstat(authFile)).isFile()) throw Error('Codex did not save credentials');
41
+ await run(pappai,['call',node,runner+'.setup',JSON.stringify({authFile})],
42
+ process.env,['ignore','ignore','inherit'],'Could not import Codex credentials');
43
+ await run(pappai,['call',node,runner+'.status','{}'],
44
+ process.env,'inherit','Could not read Codex connection status');
45
+ } finally {
46
+ try { await rm(home,{recursive:true,force:true}); }
47
+ finally {
48
+ process.off('SIGTERM',cancel);
49
+ process.off('SIGINT',cancel);
50
+ }
51
+ }
52
+ }
53
+
54
+ if (process.argv[1] === fileURLToPath(import.meta.url)) {
55
+ authorize(...process.argv.slice(2,5)).catch(error => {
56
+ console.error(error.code ? 'Codex authorization file unavailable' : error.message);
57
+ process.exitCode = 1;
58
+ });
59
+ }
package/src/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { RunnerHost } from './host.mjs'
2
2
  import { prepareWorker } from './policy.mjs'
3
3
  import { attachProfileMethods } from './profile.mjs'
4
+ import { prepareProfileCli, profileCli } from './profile-cli.mjs'
4
5
  import { join } from 'node:path'
5
6
  import { CodexInstallation, runnerMethods } from './installation.mjs'
6
7
  import settingsSchema from '../examples/settings-schema.json' with {type:'json'}
@@ -24,6 +25,10 @@ const host = new RunnerHost(config,async (config,job) => {
24
25
  },send)
25
26
  async function runnerCall({id,method,data}) {
26
27
  try {
28
+ if (method==='@cli.prepare') {
29
+ const result=await prepareProfileCli(data,installation,config.runnerName)
30
+ return send('runnerReply',{id,result})
31
+ }
27
32
  if (!Object.hasOwn(runnerMethods,method)) throw Error('Unknown runner method')
28
33
  if (!data || typeof data!=='object' || Array.isArray(data) ||
29
34
  Object.keys(data).some(key=>method!=='installCodex' || key!=='version')) throw Error('Invalid runner method arguments')
@@ -43,6 +48,6 @@ process.on('message', message => {
43
48
  process.on('disconnect',close)
44
49
  process.on('SIGTERM',close)
45
50
  process.on('SIGINT',close)
46
- send('runnerReady',{kind:'codex',settingsSchema,methods:runnerMethods})
51
+ send('runnerReady',{kind:'codex',settingsSchema,methods:runnerMethods,profileCli})
47
52
  attachProfileMethods(config)
48
53
  void installation.start().catch(()=>{}) // getVersion and waiting workers expose the issue
@@ -0,0 +1,22 @@
1
+ import { fileURLToPath } from 'node:url';
2
+
3
+ export const profileCli = {
4
+ authorize: {
5
+ description: 'Connect this PROFILE to ChatGPT using a device code.',
6
+ doc: 'Open the verification URL in your browser and enter the code shown in this terminal. After sign-in, this command calls setup and status on the selected PROFILE as the current USER. The USER needs network access and permission for both methods.',
7
+ version: '1',
8
+ schema: {input:{type:'object', properties:{}, additionalProperties:false}},
9
+ },
10
+ };
11
+
12
+ export async function prepareProfileCli(data, installation, runnerName) {
13
+ if (data?.feature !== 'authorize' || typeof data.node !== 'string' || !data.node ||
14
+ !data.input || typeof data.input !== 'object' || Array.isArray(data.input) ||
15
+ Object.keys(data.input).length || Object.keys(data).some(key => !['feature','node','input'].includes(key))) {
16
+ throw Error('Invalid PROFILE authorization command');
17
+ }
18
+ return {
19
+ files:[fileURLToPath(new URL('./authorize.mjs',import.meta.url)), await installation.executable()],
20
+ argv:['/usr/local/bin/node','/run/pappai/cli/0','/run/pappai/cli/1',data.node,runnerName],
21
+ };
22
+ }