Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 8 additions & 28 deletions .github/drivers/pi_agent_core_driver_sample_node.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,21 @@
* For the full implementation see:
* actions/setup/js/pi_agent_core_driver.cjs
*
* Shared helpers (emitJsonl, getApiKey) are loaded at runtime from:
* ${RUNNER_TEMP}/gh-aw/actions/pi_agent_core_driver_helpers.cjs
*
* The gh-aw setup action deploys all driver helpers to that location, so
* this path works correctly whether the file lives in the gh-aw repo itself
* or has been copied into your own repository's .github/drivers/ directory.
*
* See also:
* https://github.com/earendil-works/pi/blob/main/packages/agent/README.md
*/

const { execSync } = require("child_process");
const fs = require("fs");
const crypto = require("crypto");

// ---------------------------------------------------------------------------
// Minimal JSONL emitter
// ---------------------------------------------------------------------------

/** @param {unknown} obj */
function emitJsonl(obj) {
process.stdout.write(JSON.stringify(obj) + "\n");
}

// ---------------------------------------------------------------------------
// API key resolution (customise as needed)
// ---------------------------------------------------------------------------

/** @param {string} provider */
function getApiKey(provider) {
switch (provider) {
case "github-copilot":
case "copilot":
return process.env.COPILOT_GITHUB_TOKEN || process.env.GITHUB_TOKEN;
case "anthropic":
return process.env.ANTHROPIC_API_KEY;
case "openai":
return process.env.CODEX_API_KEY || process.env.OPENAI_API_KEY;
default:
return undefined;
}
}
const { emitJsonl, getApiKey } = require(`${process.env.RUNNER_TEMP}/gh-aw/actions/pi_agent_core_driver_helpers.cjs`);

// ---------------------------------------------------------------------------
// Entry point
Expand Down
19 changes: 2 additions & 17 deletions actions/setup/js/pi_agent_core_driver.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"use strict";

const { getErrorMessage } = require("./error_helpers.cjs");
const { emitJsonl, getApiKey: getBuiltinApiKey } = require("./pi_agent_core_driver_helpers.cjs");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
Expand All @@ -50,11 +51,6 @@ function log(msg) {
process.stderr.write(`[pi-agent-core-driver] ${msg}\n`);
}

/** @param {unknown} obj */
function emitJsonl(obj) {
process.stdout.write(JSON.stringify(obj) + "\n");
}

// ---------------------------------------------------------------------------
// models.json parsing
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -146,18 +142,7 @@ function buildGetApiKey(gatewayConfig) {
}

// Built-in providers used in no-firewall mode.
switch (provider) {
case "github-copilot":
case "copilot":
return process.env.COPILOT_GITHUB_TOKEN || process.env.GITHUB_TOKEN;
case "anthropic":
return process.env.ANTHROPIC_API_KEY;
case "openai":
case "codex":
return process.env.CODEX_API_KEY || process.env.OPENAI_API_KEY;
default:
return undefined;
}
return getBuiltinApiKey(provider);
};
}

Expand Down
49 changes: 49 additions & 0 deletions actions/setup/js/pi_agent_core_driver_helpers.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// @ts-check

/**
* Shared helpers for pi-agent-core drivers.
*
* Consumed by both the built-in driver (pi_agent_core_driver.cjs) and the
* sample driver (.github/drivers/pi_agent_core_driver_sample_node.cjs) so
* that JSONL formatting and provider key resolution have a single source of
* truth.
*/

"use strict";

// ---------------------------------------------------------------------------
// JSONL emitter
// ---------------------------------------------------------------------------

/** @param {unknown} obj */
function emitJsonl(obj) {
process.stdout.write(JSON.stringify(obj) + "\n");
}

// ---------------------------------------------------------------------------
// Built-in provider API key resolution
// ---------------------------------------------------------------------------

/**
* Resolve an API key for the given provider name from well-known environment
* variables. Returns `undefined` when no matching variable is set.
*
* @param {string} provider
* @returns {string|undefined}
*/
function getApiKey(provider) {
switch (provider) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/codebase-design] The exported name getApiKey does not signal that it only covers built-in providers. The main driver already aliases it as getBuiltinApiKey at the import site, which is the clearer name — rename the export to match so the public surface is self-documenting and the alias is unnecessary.

💡 Suggested rename
// pi_agent_core_driver_helpers.cjs
function getBuiltinApiKey(provider) { ... }
module.exports = { emitJsonl, getBuiltinApiKey };

// pi_agent_core_driver.cjs
const { emitJsonl, getBuiltinApiKey } = require('./pi_agent_core_driver_helpers.cjs');

// sample driver
const { emitJsonl, getBuiltinApiKey: getApiKey } = require('...');

Consistent naming prevents future callers from confusing the helper with a fully generic key resolver.

@copilot please address this.

case "github-copilot":
case "copilot":
return process.env.COPILOT_GITHUB_TOKEN || process.env.GITHUB_TOKEN;
case "anthropic":
return process.env.ANTHROPIC_API_KEY;
case "openai":
case "codex":
return process.env.CODEX_API_KEY || process.env.OPENAI_API_KEY;
default:
return undefined;
}
}

module.exports = { emitJsonl, getApiKey };