-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[OPIK-5696][TS SDK] Align TS wizard to also save project_name to ~/.opik.config #6134
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
yaricom
merged 5 commits into
main
from
yaricom/OPIK-5696-TS-save_project_name-opik-config
Apr 9, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2398764
[OPIK-5696] Add `saveToOpikConfigStep` to save configuration to `~/.o…
yaricom 9f5f587
[OPIK-5696] Add unit testing support via Vitest for Node.js SDK confi…
yaricom 58bd588
[OPIK-5696] Refactor configuration handling to use `ini` package and …
yaricom 8cf73f1
[OPIK-5696] Ensure parent directory creation for custom OPIK_CONFIG_P…
yaricom 2467b7c
[OPIK-5696] Add environment isolation for `OPIK_CONFIG_PATH` in tests
yaricom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
sdks/typescript/src/opik/configure/src/steps/save-to-opik-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import chalk from 'chalk'; | ||
| import * as fs from 'fs'; | ||
| import ini from 'ini'; | ||
| import * as os from 'os'; | ||
| import * as path from 'path'; | ||
| import clack from '../utils/clack'; | ||
|
|
||
| const OPIK_CONFIG_FILE_DEFAULT = path.join(os.homedir(), '.opik.config'); | ||
|
|
||
| function expandPath(filePath: string): string { | ||
| return filePath.replace(/^~(?=$|\/|\\)/, os.homedir()); | ||
| } | ||
|
|
||
| function resolveConfigFilePath(): string { | ||
| if (!process.env.OPIK_CONFIG_PATH) { | ||
| return OPIK_CONFIG_FILE_DEFAULT; | ||
| } | ||
|
|
||
| const customPath = expandPath(process.env.OPIK_CONFIG_PATH); | ||
| const parentDir = path.dirname(customPath); | ||
|
|
||
| if (!fs.existsSync(parentDir)) { | ||
| try { | ||
| fs.mkdirSync(parentDir, { recursive: true }); | ||
| } catch (error) { | ||
| clack.log.warning( | ||
| `OPIK_CONFIG_PATH parent directory ${chalk.bold.cyan(parentDir)} could not be created: ${error instanceof Error ? error.message : String(error)}. Falling back to ${chalk.bold.cyan(OPIK_CONFIG_FILE_DEFAULT)}.`, | ||
| ); | ||
| return OPIK_CONFIG_FILE_DEFAULT; | ||
| } | ||
| } | ||
yaricom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return customPath; | ||
| } | ||
|
|
||
| export interface SaveToOpikConfigOptions { | ||
| projectName: string; | ||
| urlOverride: string; | ||
| apiKey?: string; | ||
| workspace?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Write Opik configuration values to ~/.opik.config (INI format). | ||
| * Merges with any existing content so unrelated sections are preserved. | ||
| */ | ||
| export async function saveToOpikConfigStep( | ||
| options: SaveToOpikConfigOptions, | ||
| ): Promise<void> { | ||
| const { projectName, urlOverride, apiKey, workspace } = options; | ||
| const configFilePath = resolveConfigFilePath(); | ||
|
|
||
| try { | ||
| let parsed: Record<string, unknown> = {}; | ||
| if (fs.existsSync(configFilePath)) { | ||
| parsed = ini.parse(fs.readFileSync(configFilePath, 'utf8')); | ||
| } | ||
|
|
||
| const existing = (parsed['opik'] as Record<string, string> | undefined) ?? {}; | ||
|
|
||
| parsed['opik'] = { | ||
| ...existing, | ||
| url_override: urlOverride, | ||
| project_name: projectName, | ||
| ...(apiKey ? { api_key: apiKey } : {}), | ||
| ...(workspace ? { workspace } : {}), | ||
| }; | ||
|
|
||
| await fs.promises.writeFile(configFilePath, ini.stringify(parsed), { | ||
yaricom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| encoding: 'utf8', | ||
| flag: 'w', | ||
| }); | ||
|
|
||
| clack.log.success( | ||
| `Saved Opik configuration to ${chalk.bold.cyan(configFilePath)}`, | ||
| ); | ||
| } catch (error) { | ||
| clack.log.warning( | ||
| `Failed to save configuration to ${chalk.bold.cyan(configFilePath)}: ${error instanceof Error ? error.message : String(error)}`, | ||
| ); | ||
| } | ||
| } | ||
172 changes: 172 additions & 0 deletions
172
sdks/typescript/src/opik/configure/tests/steps/save-to-opik-config.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import * as fs from 'fs'; | ||
| import ini from 'ini'; | ||
| import { saveToOpikConfigStep } from '../../src/steps'; | ||
|
|
||
| // ESM requires module-level mocking for native node modules. | ||
yaricom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| vi.mock('fs', async () => { | ||
| const actual = await vi.importActual<typeof import('fs')>('fs'); | ||
| return { | ||
| ...actual, | ||
| existsSync: vi.fn(), | ||
| readFileSync: vi.fn(), | ||
| mkdirSync: vi.fn(), | ||
| promises: { | ||
| ...actual.promises, | ||
| writeFile: vi.fn(), | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| // Silence clack output during tests. | ||
| vi.mock('../../src/utils/clack', () => ({ | ||
| default: { log: { success: vi.fn(), warning: vi.fn() } }, | ||
| })); | ||
|
|
||
| describe('saveToOpikConfigStep', () => { | ||
| let originalConfigPath: string | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| originalConfigPath = process.env.OPIK_CONFIG_PATH; | ||
| delete process.env.OPIK_CONFIG_PATH; | ||
| vi.mocked(fs.existsSync).mockReturnValue(false); | ||
| vi.mocked(fs.readFileSync).mockReturnValue(''); | ||
| vi.mocked(fs.promises.writeFile).mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalConfigPath !== undefined) { | ||
| process.env.OPIK_CONFIG_PATH = originalConfigPath; | ||
| } else { | ||
| delete process.env.OPIK_CONFIG_PATH; | ||
| } | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| function getWrittenParsed(): Record<string, Record<string, string>> { | ||
| const raw = vi.mocked(fs.promises.writeFile).mock.calls[0][1] as string; | ||
| return ini.parse(raw) as Record<string, Record<string, string>>; | ||
| } | ||
|
|
||
| it('creates the config file when it does not exist', async () => { | ||
| await saveToOpikConfigStep({ | ||
| projectName: 'my-project', | ||
| urlOverride: 'http://localhost/api', | ||
| }); | ||
|
|
||
| expect(fs.promises.writeFile).toHaveBeenCalledOnce(); | ||
| const written = getWrittenParsed(); | ||
| expect(written.opik.project_name).toBe('my-project'); | ||
| expect(written.opik.url_override).toBe('http://localhost/api'); | ||
| expect(written.opik.api_key).toBeUndefined(); | ||
| expect(written.opik.workspace).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('includes api_key and workspace for cloud deployments', async () => { | ||
| await saveToOpikConfigStep({ | ||
| projectName: 'cloud-proj', | ||
| urlOverride: 'https://www.comet.com/opik/api', | ||
| apiKey: 'secret-key', | ||
| workspace: 'my-workspace', | ||
| }); | ||
|
|
||
| expect(fs.promises.writeFile).toHaveBeenCalledOnce(); | ||
| const written = getWrittenParsed(); | ||
| expect(written.opik.api_key).toBe('secret-key'); | ||
| expect(written.opik.workspace).toBe('my-workspace'); | ||
| }); | ||
|
|
||
| it('merges with an existing config file', async () => { | ||
| const existingContent = ini.stringify({ | ||
| opik: { api_key: 'old-key', project_name: 'old-project' }, | ||
| }); | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); | ||
| vi.mocked(fs.readFileSync).mockReturnValue(existingContent); | ||
|
|
||
| await saveToOpikConfigStep({ | ||
| projectName: 'new-project', | ||
| urlOverride: 'https://www.comet.com/opik/api', | ||
| apiKey: 'new-key', | ||
| }); | ||
|
|
||
| expect(fs.promises.writeFile).toHaveBeenCalledOnce(); | ||
| const written = getWrittenParsed(); | ||
| expect(written.opik.project_name).toBe('new-project'); | ||
| expect(written.opik.api_key).toBe('new-key'); | ||
| }); | ||
|
|
||
| it('preserves other sections from an existing config file', async () => { | ||
| const existingContent = ini.stringify({ | ||
| other: { foo: 'bar' }, | ||
| opik: { project_name: 'old-project' }, | ||
| }); | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); | ||
| vi.mocked(fs.readFileSync).mockReturnValue(existingContent); | ||
|
|
||
| await saveToOpikConfigStep({ | ||
| projectName: 'new-project', | ||
| urlOverride: 'http://localhost/api', | ||
| }); | ||
|
|
||
| const written = getWrittenParsed(); | ||
| expect(written.other.foo).toBe('bar'); | ||
| expect(written.opik.project_name).toBe('new-project'); | ||
| }); | ||
|
|
||
| it('writes to OPIK_CONFIG_PATH when the env var points to a path with an existing parent directory', async () => { | ||
| const customPath = '/custom/path/.opik.config'; | ||
| process.env.OPIK_CONFIG_PATH = customPath; | ||
| vi.mocked(fs.existsSync).mockReturnValue(true); // parent dir exists | ||
|
|
||
| await saveToOpikConfigStep({ | ||
| projectName: 'proj', | ||
| urlOverride: 'http://localhost/api', | ||
| }); | ||
|
|
||
| expect(fs.promises.writeFile).toHaveBeenCalledOnce(); | ||
| expect(vi.mocked(fs.promises.writeFile).mock.calls[0][0]).toBe(customPath); | ||
| }); | ||
|
|
||
| it('creates parent directory and writes to OPIK_CONFIG_PATH when parent dir is missing', async () => { | ||
| const customPath = '/nonexistent/dir/.opik.config'; | ||
| process.env.OPIK_CONFIG_PATH = customPath; | ||
| vi.mocked(fs.existsSync).mockReturnValue(false); // parent dir missing | ||
| vi.mocked(fs.mkdirSync).mockReturnValue(undefined); | ||
|
|
||
| await saveToOpikConfigStep({ | ||
| projectName: 'proj', | ||
| urlOverride: 'http://localhost/api', | ||
| }); | ||
|
|
||
| expect(fs.mkdirSync).toHaveBeenCalledWith('/nonexistent/dir', { recursive: true }); | ||
| expect(fs.promises.writeFile).toHaveBeenCalledOnce(); | ||
| expect(vi.mocked(fs.promises.writeFile).mock.calls[0][0]).toBe(customPath); | ||
| }); | ||
|
|
||
| it('falls back to default path when OPIK_CONFIG_PATH parent dir cannot be created', async () => { | ||
| process.env.OPIK_CONFIG_PATH = '/nonexistent/dir/.opik.config'; | ||
| vi.mocked(fs.existsSync).mockReturnValue(false); | ||
| vi.mocked(fs.mkdirSync).mockImplementation(() => { throw new Error('EACCES'); }); | ||
|
|
||
| await saveToOpikConfigStep({ | ||
| projectName: 'proj', | ||
| urlOverride: 'http://localhost/api', | ||
| }); | ||
|
|
||
| expect(fs.promises.writeFile).toHaveBeenCalledOnce(); | ||
| const writtenPath = vi.mocked(fs.promises.writeFile).mock.calls[0][0] as string; | ||
| expect(writtenPath).not.toBe('/nonexistent/dir/.opik.config'); | ||
| expect(writtenPath).toMatch(/\.opik\.config$/); | ||
| }); | ||
|
|
||
| it('does not throw when writeFile fails — logs a warning instead', async () => { | ||
| vi.mocked(fs.promises.writeFile).mockRejectedValue(new Error('EACCES')); | ||
|
|
||
| await expect( | ||
| saveToOpikConfigStep({ | ||
| projectName: 'proj', | ||
| urlOverride: 'http://localhost/api', | ||
| }), | ||
| ).resolves.not.toThrow(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| globals: true, | ||
| environment: 'node', | ||
| include: ['tests/**/*.test.ts'], | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.