@promptbook/cli 0.112.0-22 → 0.112.0-23

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/esm/index.es.js CHANGED
@@ -5,9 +5,9 @@ import * as fs from 'fs';
5
5
  import { writeFileSync, readFileSync, existsSync } from 'fs';
6
6
  import * as path from 'path';
7
7
  import { join, basename, dirname, isAbsolute, relative, extname, resolve } from 'path';
8
+ import { mkdir, readFile, writeFile, stat, access, constants, readdir, watch, unlink, rm, rename, rmdir } from 'fs/promises';
8
9
  import { forTime, forEver } from 'waitasecond';
9
10
  import prompts from 'prompts';
10
- import { stat, access, constants, readFile, writeFile, readdir, mkdir, watch, unlink, rm, rename, rmdir } from 'fs/promises';
11
11
  import * as dotenv from 'dotenv';
12
12
  import hexEncoder from 'crypto-js/enc-hex';
13
13
  import sha256 from 'crypto-js/sha256';
@@ -57,7 +57,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
57
57
  * @generated
58
58
  * @see https://github.com/webgptorg/promptbook
59
59
  */
60
- const PROMPTBOOK_ENGINE_VERSION = '0.112.0-22';
60
+ const PROMPTBOOK_ENGINE_VERSION = '0.112.0-23';
61
61
  /**
62
62
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
63
63
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -1827,6 +1827,205 @@ function buildPromptSlug$1(template, title) {
1827
1827
  * Note: [🟡] Code in this file should never be published outside of `@promptbook/cli`
1828
1828
  */
1829
1829
 
1830
+ /**
1831
+ * Relative path to the root prompts directory used by Promptbook coder utilities.
1832
+ */
1833
+ const PROMPTS_DIRECTORY_PATH = 'prompts';
1834
+ /**
1835
+ * Relative path to the archive directory used by `coder verify`.
1836
+ */
1837
+ const PROMPTS_DONE_DIRECTORY_PATH = join(PROMPTS_DIRECTORY_PATH, 'done');
1838
+ /**
1839
+ * Required environment variables for coding-agent git identity.
1840
+ */
1841
+ const REQUIRED_CODER_ENV_VARIABLES = [
1842
+ {
1843
+ name: 'CODING_AGENT_GIT_NAME',
1844
+ value: 'Promptbook Coding Agent',
1845
+ },
1846
+ {
1847
+ name: 'CODING_AGENT_GIT_EMAIL',
1848
+ value: 'coding-agent@promptbook.studio',
1849
+ },
1850
+ {
1851
+ name: 'CODING_AGENT_GIT_SIGNING_KEY',
1852
+ value: '13406525ED912F938FEA85AB4046C687298B2382',
1853
+ },
1854
+ ];
1855
+ /**
1856
+ * Initializes `coder init` command for Promptbook CLI utilities.
1857
+ *
1858
+ * Note: `$` is used to indicate that this function is not a pure function - it registers a command in the CLI.
1859
+ *
1860
+ * @private internal function of `promptbookCli`
1861
+ */
1862
+ function $initializeCoderInitCommand(program) {
1863
+ const command = program.command('init');
1864
+ command.alias('initialize');
1865
+ command.description(spaceTrim$1(`
1866
+ Initialize Promptbook coder configuration for current project
1867
+
1868
+ Creates:
1869
+ - prompts/
1870
+ - prompts/done/
1871
+
1872
+ Ensures required coding-agent environment variables in .env:
1873
+ - CODING_AGENT_GIT_NAME
1874
+ - CODING_AGENT_GIT_EMAIL
1875
+ - CODING_AGENT_GIT_SIGNING_KEY
1876
+ `));
1877
+ command.action(handleActionErrors(async () => {
1878
+ const summary = await initializeCoderProjectConfiguration(process.cwd());
1879
+ printInitializationSummary(summary);
1880
+ }));
1881
+ }
1882
+ /**
1883
+ * Creates or updates all coder configuration artifacts required in the current project.
1884
+ */
1885
+ async function initializeCoderProjectConfiguration(projectPath) {
1886
+ const promptsDirectoryStatus = await ensureDirectory(projectPath, PROMPTS_DIRECTORY_PATH);
1887
+ const promptsDoneDirectoryStatus = await ensureDirectory(projectPath, PROMPTS_DONE_DIRECTORY_PATH);
1888
+ const { envFileStatus, initializedEnvVariableNames } = await ensureCoderEnvFile(projectPath);
1889
+ return {
1890
+ promptsDirectoryStatus,
1891
+ promptsDoneDirectoryStatus,
1892
+ envFileStatus,
1893
+ initializedEnvVariableNames,
1894
+ };
1895
+ }
1896
+ /**
1897
+ * Ensures a relative directory exists in the project root.
1898
+ */
1899
+ async function ensureDirectory(projectPath, relativeDirectoryPath) {
1900
+ const directoryPath = join(projectPath, relativeDirectoryPath);
1901
+ const existedBefore = await isExistingDirectory(directoryPath);
1902
+ if (!existedBefore) {
1903
+ await mkdir(directoryPath, { recursive: true });
1904
+ return 'created';
1905
+ }
1906
+ return 'unchanged';
1907
+ }
1908
+ /**
1909
+ * Ensures `.env` exists and contains all required coder environment variables.
1910
+ */
1911
+ async function ensureCoderEnvFile(projectPath) {
1912
+ const envFilePath = join(projectPath, '.env');
1913
+ const envFileExistedBefore = await isExistingFile(envFilePath);
1914
+ const currentEnvContent = envFileExistedBefore ? await readFile(envFilePath, 'utf-8') : '';
1915
+ const existingEnvVariables = parseEnvVariableNames(currentEnvContent);
1916
+ const missingEnvVariables = REQUIRED_CODER_ENV_VARIABLES.filter(({ name }) => !existingEnvVariables.has(name));
1917
+ if (missingEnvVariables.length === 0) {
1918
+ if (!envFileExistedBefore) {
1919
+ await writeFile(envFilePath, '# Environment variables for Promptbook coder\n', 'utf-8');
1920
+ return {
1921
+ envFileStatus: 'created',
1922
+ initializedEnvVariableNames: [],
1923
+ };
1924
+ }
1925
+ return {
1926
+ envFileStatus: 'unchanged',
1927
+ initializedEnvVariableNames: [],
1928
+ };
1929
+ }
1930
+ const envBlockToAppend = buildMissingEnvVariablesBlock(missingEnvVariables);
1931
+ const nextEnvContent = appendBlock(currentEnvContent, envBlockToAppend);
1932
+ await writeFile(envFilePath, nextEnvContent, 'utf-8');
1933
+ return {
1934
+ envFileStatus: envFileExistedBefore ? 'updated' : 'created',
1935
+ initializedEnvVariableNames: missingEnvVariables.map(({ name }) => name),
1936
+ };
1937
+ }
1938
+ /**
1939
+ * Parses variable names currently defined in `.env` style content.
1940
+ */
1941
+ function parseEnvVariableNames(envContent) {
1942
+ const variableNames = new Set();
1943
+ for (const line of envContent.split(/\r?\n/)) {
1944
+ const trimmedLine = line.trim();
1945
+ if (trimmedLine === '' || trimmedLine.startsWith('#')) {
1946
+ continue;
1947
+ }
1948
+ const match = trimmedLine.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=/);
1949
+ if (!match || !match[1]) {
1950
+ continue;
1951
+ }
1952
+ variableNames.add(match[1]);
1953
+ }
1954
+ return variableNames;
1955
+ }
1956
+ /**
1957
+ * Builds a `.env` block containing missing coder environment variables.
1958
+ */
1959
+ function buildMissingEnvVariablesBlock(variables) {
1960
+ return spaceTrim$1(`
1961
+ # Promptbook coder identity (initialized by \`ptbk coder init\`)
1962
+ ${variables.map(({ name, value }) => `${name}=${JSON.stringify(value)}`).join('\n')}
1963
+ `);
1964
+ }
1965
+ /**
1966
+ * Appends one text block to existing file content while preserving readable newlines.
1967
+ */
1968
+ function appendBlock(currentContent, blockToAppend) {
1969
+ if (currentContent.trim() === '') {
1970
+ return `${blockToAppend}\n`;
1971
+ }
1972
+ const normalizedCurrentContent = currentContent.endsWith('\n') ? currentContent : `${currentContent}\n`;
1973
+ return `${normalizedCurrentContent}\n${blockToAppend}\n`;
1974
+ }
1975
+ /**
1976
+ * Prints a readable summary of what was initialized for the user.
1977
+ */
1978
+ function printInitializationSummary(summary) {
1979
+ console.info(colors.green('Promptbook coder configuration initialized.'));
1980
+ console.info(colors.gray(`- prompts/: ${formatInitializationStatus(summary.promptsDirectoryStatus)}`));
1981
+ console.info(colors.gray(`- prompts/done/: ${formatInitializationStatus(summary.promptsDoneDirectoryStatus)}`));
1982
+ console.info(colors.gray(`- .env: ${formatInitializationStatus(summary.envFileStatus)}`));
1983
+ if (summary.initializedEnvVariableNames.length > 0) {
1984
+ console.info(colors.cyan(`- Added env variables: ${summary.initializedEnvVariableNames.join(', ')}`));
1985
+ }
1986
+ else {
1987
+ console.info(colors.gray('- Required coder env variables are already present.'));
1988
+ }
1989
+ }
1990
+ /**
1991
+ * Formats one initialization status into a human-readable label.
1992
+ */
1993
+ function formatInitializationStatus(status) {
1994
+ if (status === 'created') {
1995
+ return 'created';
1996
+ }
1997
+ if (status === 'updated') {
1998
+ return 'updated';
1999
+ }
2000
+ return 'unchanged';
2001
+ }
2002
+ /**
2003
+ * Checks whether a path exists and is a file.
2004
+ */
2005
+ async function isExistingFile(path) {
2006
+ try {
2007
+ return (await stat(path)).isFile();
2008
+ }
2009
+ catch (_a) {
2010
+ return false;
2011
+ }
2012
+ }
2013
+ /**
2014
+ * Checks whether a path exists and is a directory.
2015
+ */
2016
+ async function isExistingDirectory(path) {
2017
+ try {
2018
+ return (await stat(path)).isDirectory();
2019
+ }
2020
+ catch (_a) {
2021
+ return false;
2022
+ }
2023
+ }
2024
+ /**
2025
+ * Note: [💞] Ignore a discrepancy between file name and entity name
2026
+ * Note: [🟡] Code in this file should never be published outside of `@promptbook/cli`
2027
+ */
2028
+
1830
2029
  /**
1831
2030
  * Initializes `coder run` command for Promptbook CLI utilities
1832
2031
  *
@@ -1976,6 +2175,7 @@ function $initializeCoderVerifyCommand(program) {
1976
2175
  * Initializes `coder` command with subcommands for Promptbook CLI utilities
1977
2176
  *
1978
2177
  * The coder command provides utilities for automated coding:
2178
+ * - init: Initialize coder configuration in current project
1979
2179
  * - generate-boilerplates: Generate prompt boilerplate files
1980
2180
  * - find-refactor-candidates: Find files that need refactoring
1981
2181
  * - run: Run coding prompts with AI agents
@@ -1992,6 +2192,7 @@ function $initializeCoderCommand(program) {
1992
2192
  Coding utilities for automated development workflows
1993
2193
 
1994
2194
  Subcommands:
2195
+ - init: Initialize coder configuration in current project
1995
2196
  - generate-boilerplates: Generate prompt boilerplate files
1996
2197
  - find-refactor-candidates: Find files that need refactoring
1997
2198
  - run: Run coding prompts with AI agents
@@ -1999,6 +2200,7 @@ function $initializeCoderCommand(program) {
1999
2200
  - find-fresh-emoji-tags: Find unused emoji tags
2000
2201
  `));
2001
2202
  // Register all subcommands
2203
+ $initializeCoderInitCommand(coderCommand);
2002
2204
  $initializeCoderGenerateBoilerplatesCommand(coderCommand);
2003
2205
  $initializeCoderFindRefactorCandidatesCommand(coderCommand);
2004
2206
  $initializeCoderRunCommand(coderCommand);
@@ -31208,6 +31410,28 @@ async function promptbookCli() {
31208
31410
  * Note: [🟡] Code in this file should never be published outside of `@promptbook/cli`
31209
31411
  */
31210
31412
 
31413
+ /**
31414
+ * Tracks whether CLI runtime registrations were already initialized in this process.
31415
+ *
31416
+ * @private internal utility of Promptbook CLI bootstrap
31417
+ */
31418
+ let isCliRuntimeRegistered = false;
31419
+ /**
31420
+ * Shared bootstrap for Promptbook CLI used by both local `ts-node` and packaged `npx` entrypoints.
31421
+ *
31422
+ * @private internal utility of Promptbook CLI bootstrap
31423
+ */
31424
+ async function $runPromptbookCli() {
31425
+ if (!isCliRuntimeRegistered) {
31426
+ await Promise.resolve().then(function () { return cli_index; });
31427
+ isCliRuntimeRegistered = true;
31428
+ }
31429
+ await promptbookCli();
31430
+ }
31431
+ /**
31432
+ * Note: [🟡] Code in this file should never be published outside of `@promptbook/cli`
31433
+ */
31434
+
31211
31435
  /**
31212
31436
  * Note: [🔺] Purpose of this file is to export CLI for production environment
31213
31437
  */
@@ -31218,7 +31442,7 @@ async function promptbookCli() {
31218
31442
  */
31219
31443
  const _CLI = {
31220
31444
  // Note: [🥠]
31221
- _initialize_promptbookCli: promptbookCli,
31445
+ _initialize_promptbookCli: $runPromptbookCli,
31222
31446
  };
31223
31447
  /**
31224
31448
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -39908,6 +40132,47 @@ const _OpenAiSdkTranspilerRegistration = $bookTranspilersRegister.register(OpenA
39908
40132
  * Note: [💞] Ignore a discrepancy between file name and entity name
39909
40133
  */
39910
40134
 
40135
+ // ⚠️ WARNING: This code has been generated so that any manual changes will be overwritten
40136
+
40137
+ var cli_index = /*#__PURE__*/Object.freeze({
40138
+ __proto__: null,
40139
+ BOOK_LANGUAGE_VERSION: BOOK_LANGUAGE_VERSION,
40140
+ PROMPTBOOK_ENGINE_VERSION: PROMPTBOOK_ENGINE_VERSION,
40141
+ _CLI: _CLI,
40142
+ _AnthropicClaudeMetadataRegistration: _AnthropicClaudeMetadataRegistration,
40143
+ _AnthropicClaudeRegistration: _AnthropicClaudeRegistration,
40144
+ _AzureOpenAiMetadataRegistration: _AzureOpenAiMetadataRegistration,
40145
+ _AzureOpenAiRegistration: _AzureOpenAiRegistration,
40146
+ _DeepseekMetadataRegistration: _DeepseekMetadataRegistration,
40147
+ _DeepseekRegistration: _DeepseekRegistration,
40148
+ _GoogleMetadataRegistration: _GoogleMetadataRegistration,
40149
+ _GoogleRegistration: _GoogleRegistration,
40150
+ _OllamaMetadataRegistration: _OllamaMetadataRegistration,
40151
+ _OllamaRegistration: _OllamaRegistration,
40152
+ _OpenAiMetadataRegistration: _OpenAiMetadataRegistration,
40153
+ _OpenAiAssistantMetadataRegistration: _OpenAiAssistantMetadataRegistration,
40154
+ _OpenAiCompatibleMetadataRegistration: _OpenAiCompatibleMetadataRegistration,
40155
+ _OpenAiRegistration: _OpenAiRegistration,
40156
+ _OpenAiAssistantRegistration: _OpenAiAssistantRegistration,
40157
+ _OpenAiCompatibleRegistration: _OpenAiCompatibleRegistration,
40158
+ _BoilerplateScraperRegistration: _BoilerplateScraperRegistration,
40159
+ _BoilerplateScraperMetadataRegistration: _BoilerplateScraperMetadataRegistration,
40160
+ _LegacyDocumentScraperRegistration: _LegacyDocumentScraperRegistration,
40161
+ _LegacyDocumentScraperMetadataRegistration: _LegacyDocumentScraperMetadataRegistration,
40162
+ _DocumentScraperRegistration: _DocumentScraperRegistration,
40163
+ _DocumentScraperMetadataRegistration: _DocumentScraperMetadataRegistration,
40164
+ _MarkdownScraperRegistration: _MarkdownScraperRegistration,
40165
+ _MarkdownScraperMetadataRegistration: _MarkdownScraperMetadataRegistration,
40166
+ _MarkitdownScraperRegistration: _MarkitdownScraperRegistration,
40167
+ _MarkitdownScraperMetadataRegistration: _MarkitdownScraperMetadataRegistration,
40168
+ _PdfScraperRegistration: _PdfScraperRegistration,
40169
+ _PdfScraperMetadataRegistration: _PdfScraperMetadataRegistration,
40170
+ _WebsiteScraperRegistration: _WebsiteScraperRegistration,
40171
+ _WebsiteScraperMetadataRegistration: _WebsiteScraperMetadataRegistration,
40172
+ _FormattedBookInMarkdownTranspilerRegistration: _FormattedBookInMarkdownTranspilerRegistration,
40173
+ _OpenAiSdkTranspilerRegistration: _OpenAiSdkTranspilerRegistration
40174
+ });
40175
+
39911
40176
  /**
39912
40177
  * Shuffles an array of items randomly and returns a new array
39913
40178
  *