Skip to content

[tiny-agents] Configure inference API key from inputs #1632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2025
Merged
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
69 changes: 30 additions & 39 deletions packages/tiny-agents/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,80 +117,71 @@ async function main() {
}
if ((server.type === "http" || server.type === "sse") && server.headers) {
for (const [key, value] of Object.entries(server.headers)) {
if (value.includes(envSpecialValue)) {
if (value === envSpecialValue) {
inputVars.add(key);
}
}
}
}

if (config.apiKey?.includes(envSpecialValue)) {
inputVars.add("apiKey");
}

if (inputVars.size === 0) {
stdout.write(ANSI.YELLOW);
stdout.write(`Input ${inputId} defined in config but not used by any server.`);
stdout.write(`Input ${inputId} defined in config but not used by any server or as an API key. Skipping.`);
stdout.write(ANSI.RESET);
stdout.write("\n");
continue;
}

// Prompt user for input
const envVariableKey = inputId.replaceAll("-", "_").toUpperCase();
// Prompt user for input
stdout.write(ANSI.BLUE);
stdout.write(` • ${inputId}`);
stdout.write(ANSI.RESET);
stdout.write(`: ${description}. (default: load from ${envVariableKey}) `);
stdout.write("\n");

const userInput = (await rl.question("")).trim();
const valueFromEnv = process.env[envVariableKey] || "";
const finalValue = userInput || valueFromEnv;

if (!userInput) {
if (valueFromEnv) {
stdout.write(ANSI.GREEN);
stdout.write(`Value successfully loaded from '${envVariableKey}'`);
stdout.write(ANSI.RESET);
stdout.write("\n");
} else {
stdout.write(ANSI.YELLOW);
stdout.write(`No value found for '${envVariableKey}' in environment variables. Continuing.`);
stdout.write(ANSI.RESET);
stdout.write("\n");
}
}

// Inject user input (or env variable) into servers' env
for (const server of config.servers) {
if (server.type === "stdio" && server.env) {
for (const [key, value] of Object.entries(server.env)) {
if (value === envSpecialValue) {
if (userInput) {
server.env[key] = userInput;
} else {
const valueFromEnv = process.env[envVariableKey] || "";
server.env[key] = valueFromEnv;
if (valueFromEnv) {
stdout.write(ANSI.GREEN);
stdout.write(`Value successfully loaded from '${envVariableKey}'`);
stdout.write(ANSI.RESET);
stdout.write("\n");
} else {
stdout.write(ANSI.YELLOW);
stdout.write(`No value found for '${envVariableKey}' in environment variables. Continuing.`);
stdout.write(ANSI.RESET);
stdout.write("\n");
}
}
server.env[key] = value.replace(envSpecialValue, finalValue);
}
}
}
if ((server.type === "http" || server.type === "sse") && server.headers) {
for (const [key, value] of Object.entries(server.headers)) {
if (value.includes(envSpecialValue)) {
if (userInput) {
server.headers[key] = value.replace(envSpecialValue, userInput);
} else {
const valueFromEnv = process.env[envVariableKey] || "";
server.headers[key] = value.replace(envSpecialValue, valueFromEnv);
if (valueFromEnv) {
stdout.write(ANSI.GREEN);
stdout.write(`Value successfully loaded from '${envVariableKey}'`);
stdout.write(ANSI.RESET);
stdout.write("\n");
} else {
stdout.write(ANSI.YELLOW);
stdout.write(`No value found for '${envVariableKey}' in environment variables. Continuing.`);
stdout.write(ANSI.RESET);
stdout.write("\n");
}
}
if (value === envSpecialValue) {
server.headers[key] = value.replace(envSpecialValue, finalValue);
}
}
}
}

if (config.apiKey?.includes(envSpecialValue)) {
config.apiKey = config.apiKey.replace(envSpecialValue, finalValue);
}
}

stdout.write("\n");
Expand Down