@lumpcode/core 0.0.11 → 0.0.12

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/dist/index.js CHANGED
@@ -3144,6 +3144,38 @@ async function pathExists(filePath) {
3144
3144
  }
3145
3145
  }
3146
3146
 
3147
+ /** Strip terminal ANSI escapes and other C0 controls js-yaml rejects in literal blocks. */
3148
+ function sanitizeHistoryText(value) {
3149
+ return value
3150
+ .replace(/\x1b\[[0-9;]*[A-Za-z]/g, '')
3151
+ .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
3152
+ }
3153
+ function parseHistoryYamlContent(content) {
3154
+ const parsed = load(content, { schema: YAML11_SCHEMA });
3155
+ if (!Array.isArray(parsed)) {
3156
+ throw new Error('History file must contain a YAML sequence');
3157
+ }
3158
+ return parsed;
3159
+ }
3160
+ function tryParseHistoryContent(content) {
3161
+ try {
3162
+ return parseHistoryYamlContent(content);
3163
+ }
3164
+ catch (firstError) {
3165
+ const sanitized = sanitizeHistoryText(content);
3166
+ if (sanitized === content) {
3167
+ throw firstError;
3168
+ }
3169
+ return parseHistoryYamlContent(sanitized);
3170
+ }
3171
+ }
3172
+ function sanitizeHistoryEntry(entry) {
3173
+ return {
3174
+ ...entry,
3175
+ prompt: sanitizeHistoryText(entry.prompt),
3176
+ commandResult: sanitizeHistoryText(entry.commandResult),
3177
+ };
3178
+ }
3147
3179
  function historyFormatFromPath(filePath) {
3148
3180
  const ext = extname(filePath).toLowerCase();
3149
3181
  if (ext === '.yaml' || ext === '.yml') {
@@ -3181,7 +3213,8 @@ function dumpHistoryEntries(entries) {
3181
3213
  if (entries.length === 0) {
3182
3214
  return '[]\n';
3183
3215
  }
3184
- return dump(entries, {
3216
+ const sanitizedEntries = entries.map(sanitizeHistoryEntry);
3217
+ return dump(sanitizedEntries, {
3185
3218
  schema: YAML11_SCHEMA,
3186
3219
  lineWidth: 0,
3187
3220
  noRefs: true,
@@ -3199,11 +3232,7 @@ async function readHistoryFile({ filePath, }) {
3199
3232
  }
3200
3233
  try {
3201
3234
  const content = await readFile(filePath, 'utf-8');
3202
- const parsed = load(content, { schema: YAML11_SCHEMA });
3203
- if (!Array.isArray(parsed)) {
3204
- return failure(`History file ${filePath} must contain a YAML sequence`);
3205
- }
3206
- return success(parsed);
3235
+ return success(tryParseHistoryContent(content));
3207
3236
  }
3208
3237
  catch (error) {
3209
3238
  const detail = error instanceof Error ? error.message : String(error);
@@ -3243,7 +3272,7 @@ async function appendHistoryEntry({ filePath, entry, }) {
3243
3272
  await mkdir(dirname(filePath), { recursive: true });
3244
3273
  entries = [];
3245
3274
  }
3246
- entries.push(entry);
3275
+ entries.push(sanitizeHistoryEntry(entry));
3247
3276
  return writeHistoryFile({ filePath, entries });
3248
3277
  }
3249
3278