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
30 changes: 24 additions & 6 deletions packages/cli/src/cmd/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ export function resolveDevOrgId(options: ResolveDevOrgIdOptions): string | undef
);
}

const DEV_ORG_ENV_ALIASES = [
'AGENTUITY_ORGID',
'AGENTUITY_ORG_ID',
'AGENTUITY_CLOUD_ORG_ID',
] as const;

export function applyDevOrgEnv(
env: Record<string, string | undefined>,
orgId: string | undefined
): void {
if (!orgId) return;
for (const name of DEV_ORG_ENV_ALIASES) {
if (!normalizeOrgId(env[name])) {
env[name] = orgId;
}
}
}

export const command = createCommand({
name: 'dev',
description: 'Run the project development server',
Expand Down Expand Up @@ -175,14 +193,14 @@ export const command = createCommand({
// Load agentuity.json (if present) so we can surface the project's
// orgId to the dev process. The aigateway client and other service
// clients accept orgId as a constructor option but otherwise have no
// way to pick it up under `agentuity dev`. Other parts of the platform
// (pi, coder-tui) already read AGENTUITY_ORGID from env, so we match
// that name here.
// way to pick it up under `agentuity dev`. Consumers read different
// subsets of the org env aliases (pi reads all of them, coder-tui
// reads AGENTUITY_ORGID and AGENTUITY_CLOUD_ORG_ID, the docs point
// apps at AGENTUITY_CLOUD_ORG_ID), so publish under every alias the
// developer has not already set.
const projectConfig = await tryLoadProjectConfig(rootDir, config);
const orgId = resolveDevOrgId({ env, projectConfig, config });
if (orgId && !normalizeOrgId(env.AGENTUITY_ORGID)) {
env.AGENTUITY_ORGID = orgId;
}
applyDevOrgEnv(env, orgId);

// Inject AI Gateway env vars so LLM SDKs route through Agentuity
const gatewayInjected = injectGatewayEnv(env, logger);
Expand Down
38 changes: 37 additions & 1 deletion packages/cli/test/cmd/dev/org-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'bun:test';
import { resolveDevOrgId } from '../../../src/cmd/dev/index.ts';
import { applyDevOrgEnv, resolveDevOrgId } from '../../../src/cmd/dev/index.ts';

describe('resolveDevOrgId', () => {
test('prefers project org ID over env and config values', () => {
Expand Down Expand Up @@ -49,3 +49,39 @@ describe('resolveDevOrgId', () => {
).toBe('org_alt');
});
});

describe('applyDevOrgEnv', () => {
test('publishes the resolved org under every org env alias', () => {
const env: Record<string, string | undefined> = {};
applyDevOrgEnv(env, 'org_project');
expect(env.AGENTUITY_ORGID).toBe('org_project');
expect(env.AGENTUITY_ORG_ID).toBe('org_project');
expect(env.AGENTUITY_CLOUD_ORG_ID).toBe('org_project');
});

test('preserves each alias the developer already set', () => {
const env: Record<string, string | undefined> = {
AGENTUITY_ORG_ID: 'org_shell',
};
applyDevOrgEnv(env, 'org_project');
expect(env.AGENTUITY_ORGID).toBe('org_project');
expect(env.AGENTUITY_ORG_ID).toBe('org_shell');
expect(env.AGENTUITY_CLOUD_ORG_ID).toBe('org_project');
});

test('overwrites blank aliases', () => {
const env: Record<string, string | undefined> = {
AGENTUITY_ORGID: '',
AGENTUITY_CLOUD_ORG_ID: ' ',
};
applyDevOrgEnv(env, 'org_project');
expect(env.AGENTUITY_ORGID).toBe('org_project');
expect(env.AGENTUITY_CLOUD_ORG_ID).toBe('org_project');
});

test('does nothing without a resolved org', () => {
const env: Record<string, string | undefined> = {};
applyDevOrgEnv(env, undefined);
expect(env).toEqual({});
});
});
Loading