@fgv/ts-extras 5.1.0-45 → 5.1.0-46

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.
Files changed (38) hide show
  1. package/dist/packlets/ai-assist/index.js +1 -1
  2. package/dist/packlets/ai-assist/index.js.map +1 -1
  3. package/dist/packlets/ai-assist/jsonResponse.js +270 -7
  4. package/dist/packlets/ai-assist/jsonResponse.js.map +1 -1
  5. package/dist/packlets/ai-assist/model.js +14 -0
  6. package/dist/packlets/ai-assist/model.js.map +1 -1
  7. package/dist/packlets/ai-assist/registry.js +57 -14
  8. package/dist/packlets/ai-assist/registry.js.map +1 -1
  9. package/dist/packlets/zip-file-tree/zipFileTreeAccessors.js +65 -7
  10. package/dist/packlets/zip-file-tree/zipFileTreeAccessors.js.map +1 -1
  11. package/dist/packlets/zip-file-tree/zipFileTreeWriter.js +16 -1
  12. package/dist/packlets/zip-file-tree/zipFileTreeWriter.js.map +1 -1
  13. package/dist/ts-extras.d.ts +253 -13
  14. package/lib/packlets/ai-assist/index.d.ts +1 -1
  15. package/lib/packlets/ai-assist/index.d.ts.map +1 -1
  16. package/lib/packlets/ai-assist/index.js +3 -2
  17. package/lib/packlets/ai-assist/index.js.map +1 -1
  18. package/lib/packlets/ai-assist/jsonResponse.d.ts +96 -0
  19. package/lib/packlets/ai-assist/jsonResponse.d.ts.map +1 -1
  20. package/lib/packlets/ai-assist/jsonResponse.js +271 -7
  21. package/lib/packlets/ai-assist/jsonResponse.js.map +1 -1
  22. package/lib/packlets/ai-assist/model.d.ts +43 -0
  23. package/lib/packlets/ai-assist/model.d.ts.map +1 -1
  24. package/lib/packlets/ai-assist/model.js +14 -0
  25. package/lib/packlets/ai-assist/model.js.map +1 -1
  26. package/lib/packlets/ai-assist/registry.d.ts +26 -6
  27. package/lib/packlets/ai-assist/registry.d.ts.map +1 -1
  28. package/lib/packlets/ai-assist/registry.js +57 -14
  29. package/lib/packlets/ai-assist/registry.js.map +1 -1
  30. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts +54 -6
  31. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts.map +1 -1
  32. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js +65 -7
  33. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js.map +1 -1
  34. package/lib/packlets/zip-file-tree/zipFileTreeWriter.d.ts +26 -1
  35. package/lib/packlets/zip-file-tree/zipFileTreeWriter.d.ts.map +1 -1
  36. package/lib/packlets/zip-file-tree/zipFileTreeWriter.js +17 -1
  37. package/lib/packlets/zip-file-tree/zipFileTreeWriter.js.map +1 -1
  38. package/package.json +7 -7
@@ -10,7 +10,7 @@ export { callProviderEmbedding, callProxiedEmbedding } from './embeddingClient';
10
10
  export { callProviderCompletionStream, callProxiedCompletionStream, executeClientToolTurn } from './streamingClient';
11
11
  export { aiProviderId, aiServerToolType, aiWebSearchToolConfig, aiServerToolConfig, aiToolAnnotations, aiClientToolConfig, aiToolEnablement, aiAssistProviderConfig, aiAssistSettings, modelSpecKey, modelSpec } from './converters';
12
12
  export { resolveEffectiveTools } from './toolFormats';
13
- export { extractJsonText, fencedStringifiedJson } from './jsonResponse';
13
+ export { classifyJsonParseFailure, extractJsonText, fencedStringifiedJson } from './jsonResponse';
14
14
  export { generateJsonCompletion, SMART_JSON_PROMPT_HINT } from './jsonCompletion';
15
15
  export { anthropicEffortToBudgetTokens } from './thinkingOptionsResolver';
16
16
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/ai-assist/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,QAAQ,EAER,oBAAoB,EAcpB,4BAA4B,EAe5B,iBAAiB,EAEjB,wBAAwB,EAoCxB,gBAAgB,EAChB,mBAAmB,EACnB,YAAY,EAEZ,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,oBAAoB,EACpB,uBAAuB,EACvB,4BAA4B,EAC5B,SAAS,EAiBV,MAAM,SAAS,CAAC;AAEjB,OAAO,EAEL,mBAAmB,EACnB,uBAAuB,EACxB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,cAAc,EACd,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,uBAAuB,EACvB,0BAA0B,EAC1B,iBAAiB,EACjB,+BAA+B,EAChC,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACrB,2BAA2B,EAC3B,0BAA0B,EAC1B,sBAAsB,EACtB,qBAAqB,EAItB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EAErB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,4BAA4B,EAC5B,2BAA2B,EAE3B,qBAAqB,EAItB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,qBAAqB,EACrB,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,gBAAgB,EAChB,YAAY,EACZ,SAAS,EACV,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAEtD,OAAO,EACL,eAAe,EACf,qBAAqB,EAItB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EAIvB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,6BAA6B,EAAgC,MAAM,2BAA2B,CAAC","sourcesContent":["/**\n * AI assist packlet - provider registry, prompt class, settings, and API client.\n * @packageDocumentation\n */\n\nexport {\n AiPrompt,\n type AiModelCapability,\n allModelCapabilities,\n type AiProviderId,\n type AiServerToolType,\n type AiServerToolConfig,\n type AiToolConfig,\n type IAiWebSearchToolConfig,\n type IAiClientToolConfig,\n type IAiToolAnnotations,\n type IAiClientTool,\n type IAiClientToolCallSummary,\n type IAiClientToolContinuation,\n type IAiClientToolTurnResult,\n type IAiToolEnablement,\n type IAiCompletionResponse,\n DEFAULT_ANTHROPIC_MAX_TOKENS,\n type IChatMessage,\n type IChatRequest,\n type AiApiFormat,\n type AiImageApiFormat,\n type AiEmbeddingApiFormat,\n type AiEmbeddingTaskType,\n type IAiEmbeddingModelCapability,\n type IAiEmbeddingParams,\n type IAiEmbeddingUsage,\n type IAiEmbeddingResult,\n type IAiImageModelCapability,\n type IAiProviderDescriptor,\n type IAiAssistProviderConfig,\n type IAiAssistSettings,\n DEFAULT_AI_ASSIST,\n type IAiAssistKeyStore,\n providerApiKeySecretName,\n type IAiImageAttachment,\n type IAiImageData,\n type AiImageSize,\n type AiImageQuality,\n type GptImageSize,\n type GptImageQuality,\n type GptImageModelNames,\n type GrokImagineModelNames,\n type GeminiFlashImageModelNames,\n type IGptImageGenerationConfig,\n type IGrokImagineImageGenerationConfig,\n type IGeminiFlashImageGenerationConfig,\n type IGptImageModelOptions,\n type IGrokImagineModelOptions,\n type IGeminiFlashImageModelOptions,\n type IOtherModelOptions,\n type IModelFamilyConfig,\n type IAiImageGenerationOptions,\n type IAiImageGenerationParams,\n type IAiGeneratedImage,\n type IAiImageGenerationResponse,\n type IAiModelCapabilityRule,\n type IAiModelCapabilityConfig,\n type IAiModelInfo,\n type IAiStreamEvent,\n type IAiStreamTextDelta,\n type IAiStreamToolEvent,\n type IAiStreamToolUseStart,\n type IAiStreamToolUseDelta,\n type IAiStreamToolUseComplete,\n type IAiStreamDone,\n type IAiStreamError,\n type ModelSpec,\n type ModelSpecKey,\n type IModelSpecMap,\n allModelSpecKeys,\n MODEL_SPEC_BASE_KEY,\n resolveModel,\n type IModelAliasMap,\n MODEL_ALIAS_SIGIL,\n resolveModelAlias,\n resolveProviderModel,\n isResponsesOnlyModel,\n isAdaptiveThinkingModel,\n usesMaxCompletionTokensField,\n toDataUrl,\n type AiThinkingMode,\n type IThinkingConfig,\n type IThinkingProviderConfig,\n type IAnthropicThinkingOptions,\n type IOpenAiThinkingOptions,\n type IGeminiThinkingOptions,\n type IXAiThinkingOptions,\n type IOtherThinkingOptions,\n type IAnthropicThinkingConfig,\n type IOpenAiThinkingConfig,\n type IGeminiThinkingConfig,\n type IXAiThinkingConfig,\n type AnthropicThinkingModelNames,\n type OpenAiThinkingModelNames,\n type GeminiThinkingModelNames,\n type XAiThinkingModelNames\n} from './model';\n\nexport {\n type IResolvedImageOptions,\n resolveImageOptions,\n validateResolvedOptions\n} from './imageOptionsResolver';\n\nexport {\n allProviderIds,\n getProviderDescriptors,\n getProviderDescriptor,\n resolveImageCapability,\n supportsImageGeneration,\n resolveEmbeddingCapability,\n supportsEmbedding,\n DEFAULT_MODEL_CAPABILITY_CONFIG\n} from './registry';\n\nexport {\n callProviderCompletion,\n callProxiedCompletion,\n callProviderImageGeneration,\n callProxiedImageGeneration,\n callProviderListModels,\n callProxiedListModels,\n type IProviderCompletionParams,\n type IProviderImageGenerationParams,\n type IProviderListModelsParams\n} from './apiClient';\n\nexport {\n callProviderEmbedding,\n callProxiedEmbedding,\n type IProviderEmbeddingParams\n} from './embeddingClient';\n\nexport {\n callProviderCompletionStream,\n callProxiedCompletionStream,\n type IProviderCompletionStreamParams,\n executeClientToolTurn,\n type IExecuteClientToolTurnParams,\n type IExecuteClientToolTurnResult,\n type IToolExecutionDecision\n} from './streamingClient';\n\nexport {\n aiProviderId,\n aiServerToolType,\n aiWebSearchToolConfig,\n aiServerToolConfig,\n aiToolAnnotations,\n aiClientToolConfig,\n aiToolEnablement,\n aiAssistProviderConfig,\n aiAssistSettings,\n modelSpecKey,\n modelSpec\n} from './converters';\n\nexport { resolveEffectiveTools } from './toolFormats';\n\nexport {\n extractJsonText,\n fencedStringifiedJson,\n type IFencedStringifiedJsonExtractorOptions,\n type IFencedStringifiedJsonOptions,\n type JsonTextExtractor\n} from './jsonResponse';\n\nexport {\n generateJsonCompletion,\n SMART_JSON_PROMPT_HINT,\n type IGenerateJsonCompletionParams,\n type IGenerateJsonCompletionResult,\n type JsonPromptHint\n} from './jsonCompletion';\n\nexport { anthropicEffortToBudgetTokens, type IResolvedThinkingConfig } from './thinkingOptionsResolver';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/ai-assist/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,QAAQ,EAER,oBAAoB,EAcpB,4BAA4B,EAe5B,iBAAiB,EAEjB,wBAAwB,EAoCxB,gBAAgB,EAChB,mBAAmB,EACnB,YAAY,EAEZ,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,oBAAoB,EACpB,uBAAuB,EACvB,4BAA4B,EAC5B,SAAS,EAiBV,MAAM,SAAS,CAAC;AAEjB,OAAO,EAEL,mBAAmB,EACnB,uBAAuB,EACxB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,cAAc,EACd,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,uBAAuB,EACvB,0BAA0B,EAC1B,iBAAiB,EACjB,+BAA+B,EAChC,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACrB,2BAA2B,EAC3B,0BAA0B,EAC1B,sBAAsB,EACtB,qBAAqB,EAItB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EAErB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,4BAA4B,EAC5B,2BAA2B,EAE3B,qBAAqB,EAItB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,qBAAqB,EACrB,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,gBAAgB,EAChB,YAAY,EACZ,SAAS,EACV,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAEtD,OAAO,EACL,wBAAwB,EACxB,eAAe,EACf,qBAAqB,EAKtB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EAIvB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,6BAA6B,EAAgC,MAAM,2BAA2B,CAAC","sourcesContent":["/**\n * AI assist packlet - provider registry, prompt class, settings, and API client.\n * @packageDocumentation\n */\n\nexport {\n AiPrompt,\n type AiModelCapability,\n allModelCapabilities,\n type AiProviderId,\n type AiServerToolType,\n type AiServerToolConfig,\n type AiToolConfig,\n type IAiWebSearchToolConfig,\n type IAiClientToolConfig,\n type IAiToolAnnotations,\n type IAiClientTool,\n type IAiClientToolCallSummary,\n type IAiClientToolContinuation,\n type IAiClientToolTurnResult,\n type IAiToolEnablement,\n type IAiCompletionResponse,\n DEFAULT_ANTHROPIC_MAX_TOKENS,\n type IChatMessage,\n type IChatRequest,\n type AiApiFormat,\n type AiImageApiFormat,\n type AiEmbeddingApiFormat,\n type AiEmbeddingTaskType,\n type IAiEmbeddingModelCapability,\n type IAiEmbeddingParams,\n type IAiEmbeddingUsage,\n type IAiEmbeddingResult,\n type IAiImageModelCapability,\n type IAiProviderDescriptor,\n type IAiAssistProviderConfig,\n type IAiAssistSettings,\n DEFAULT_AI_ASSIST,\n type IAiAssistKeyStore,\n providerApiKeySecretName,\n type IAiImageAttachment,\n type IAiImageData,\n type AiImageSize,\n type AiImageQuality,\n type GptImageSize,\n type GptImageQuality,\n type GptImageModelNames,\n type GrokImagineModelNames,\n type GeminiFlashImageModelNames,\n type IGptImageGenerationConfig,\n type IGrokImagineImageGenerationConfig,\n type IGeminiFlashImageGenerationConfig,\n type IGptImageModelOptions,\n type IGrokImagineModelOptions,\n type IGeminiFlashImageModelOptions,\n type IOtherModelOptions,\n type IModelFamilyConfig,\n type IAiImageGenerationOptions,\n type IAiImageGenerationParams,\n type IAiGeneratedImage,\n type IAiImageGenerationResponse,\n type IAiModelCapabilityRule,\n type IAiModelCapabilityConfig,\n type IAiModelInfo,\n type IAiStreamEvent,\n type IAiStreamTextDelta,\n type IAiStreamToolEvent,\n type IAiStreamToolUseStart,\n type IAiStreamToolUseDelta,\n type IAiStreamToolUseComplete,\n type IAiStreamDone,\n type IAiStreamError,\n type ModelSpec,\n type ModelSpecKey,\n type IModelSpecMap,\n allModelSpecKeys,\n MODEL_SPEC_BASE_KEY,\n resolveModel,\n type IModelAliasMap,\n MODEL_ALIAS_SIGIL,\n resolveModelAlias,\n resolveProviderModel,\n isResponsesOnlyModel,\n isAdaptiveThinkingModel,\n usesMaxCompletionTokensField,\n toDataUrl,\n type AiThinkingMode,\n type IThinkingConfig,\n type IThinkingProviderConfig,\n type IAnthropicThinkingOptions,\n type IOpenAiThinkingOptions,\n type IGeminiThinkingOptions,\n type IXAiThinkingOptions,\n type IOtherThinkingOptions,\n type IAnthropicThinkingConfig,\n type IOpenAiThinkingConfig,\n type IGeminiThinkingConfig,\n type IXAiThinkingConfig,\n type AnthropicThinkingModelNames,\n type OpenAiThinkingModelNames,\n type GeminiThinkingModelNames,\n type XAiThinkingModelNames\n} from './model';\n\nexport {\n type IResolvedImageOptions,\n resolveImageOptions,\n validateResolvedOptions\n} from './imageOptionsResolver';\n\nexport {\n allProviderIds,\n getProviderDescriptors,\n getProviderDescriptor,\n resolveImageCapability,\n supportsImageGeneration,\n resolveEmbeddingCapability,\n supportsEmbedding,\n DEFAULT_MODEL_CAPABILITY_CONFIG\n} from './registry';\n\nexport {\n callProviderCompletion,\n callProxiedCompletion,\n callProviderImageGeneration,\n callProxiedImageGeneration,\n callProviderListModels,\n callProxiedListModels,\n type IProviderCompletionParams,\n type IProviderImageGenerationParams,\n type IProviderListModelsParams\n} from './apiClient';\n\nexport {\n callProviderEmbedding,\n callProxiedEmbedding,\n type IProviderEmbeddingParams\n} from './embeddingClient';\n\nexport {\n callProviderCompletionStream,\n callProxiedCompletionStream,\n type IProviderCompletionStreamParams,\n executeClientToolTurn,\n type IExecuteClientToolTurnParams,\n type IExecuteClientToolTurnResult,\n type IToolExecutionDecision\n} from './streamingClient';\n\nexport {\n aiProviderId,\n aiServerToolType,\n aiWebSearchToolConfig,\n aiServerToolConfig,\n aiToolAnnotations,\n aiClientToolConfig,\n aiToolEnablement,\n aiAssistProviderConfig,\n aiAssistSettings,\n modelSpecKey,\n modelSpec\n} from './converters';\n\nexport { resolveEffectiveTools } from './toolFormats';\n\nexport {\n classifyJsonParseFailure,\n extractJsonText,\n fencedStringifiedJson,\n type IFencedStringifiedJsonExtractorOptions,\n type IFencedStringifiedJsonOptions,\n type JsonParseFailureReason,\n type JsonTextExtractor\n} from './jsonResponse';\n\nexport {\n generateJsonCompletion,\n SMART_JSON_PROMPT_HINT,\n type IGenerateJsonCompletionParams,\n type IGenerateJsonCompletionResult,\n type JsonPromptHint\n} from './jsonCompletion';\n\nexport { anthropicEffortToBudgetTokens, type IResolvedThinkingConfig } from './thinkingOptionsResolver';\n"]}
@@ -33,7 +33,11 @@
33
33
  */
34
34
  import { Conversion, fail, succeed } from '@fgv/ts-utils';
35
35
  import { Converters as JsonBaseConverters } from '@fgv/ts-json-base';
36
- const FENCED_BLOCK = /```[A-Za-z0-9_-]*\s*\r?\n([\s\S]*?)\r?\n?```/;
36
+ // Group 1 is the opening fence (marker, optional language tag, newline) and
37
+ // group 2 is the fenced body. Group 1 is captured only so a caller that needs
38
+ // to map a position inside the body back onto an offset in the original text
39
+ // can add its length instead of re-deriving the fence header.
40
+ const FENCED_BLOCK = /(```[A-Za-z0-9_-]*\s*\r?\n)([\s\S]*?)\r?\n?```/;
37
41
  const BOM = /^\uFEFF/;
38
42
  // Full RFC 8259 grammar so the extractor only succeeds when the entire
39
43
  // candidate parses as a JSON primitive (instead of just starting like one).
@@ -88,11 +92,36 @@ function findBalancedJsonSubstring(text) {
88
92
  else if (ch === close) {
89
93
  depth--;
90
94
  if (depth === 0) {
91
- return { kind: 'found', value: text.slice(start, i + 1) };
95
+ return { kind: 'found', value: text.slice(start, i + 1), start };
92
96
  }
93
97
  }
94
98
  }
95
- return start >= 0 ? { kind: 'unclosed', depth } : { kind: 'none' };
99
+ return start >= 0 ? { kind: 'unclosed', depth, start } : { kind: 'none' };
100
+ }
101
+ /**
102
+ * Strips the wrappers a model adds around JSON — byte-order mark, surrounding
103
+ * whitespace, Markdown code fence — and reports where what is left starts in
104
+ * the original text.
105
+ *
106
+ * Single source of truth for that step: {@link extractJsonText} and
107
+ * {@link classifyJsonParseFailure} must agree on what the candidate is, or a
108
+ * classified `offset` would point into text the extractor never parsed.
109
+ * @internal
110
+ */
111
+ function locateJsonCandidate(text) {
112
+ const noBom = stripBom(text);
113
+ const stripped = noBom.trim();
114
+ const strippedBase = text.length - noBom.length + (noBom.length - noBom.trimStart().length);
115
+ const fenced = FENCED_BLOCK.exec(stripped);
116
+ if (fenced === null) {
117
+ return { stripped, text: stripped, base: strippedBase };
118
+ }
119
+ const body = fenced[2];
120
+ return {
121
+ stripped,
122
+ text: body.trim(),
123
+ base: strippedBase + fenced.index + fenced[1].length + (body.length - body.trimStart().length)
124
+ };
96
125
  }
97
126
  /**
98
127
  * Default {@link AiAssist.JsonTextExtractor | extractor} for LLM responses. Tolerates:
@@ -120,12 +149,11 @@ export const extractJsonText = (text) => {
120
149
  if (typeof text !== 'string') {
121
150
  return fail('extractJsonText: input must be a string.');
122
151
  }
123
- const stripped = stripBom(text).trim();
124
- if (stripped.length === 0) {
152
+ const located = locateJsonCandidate(text);
153
+ if (located.stripped.length === 0) {
125
154
  return fail('extractJsonText: input is empty.');
126
155
  }
127
- const fenced = FENCED_BLOCK.exec(stripped);
128
- const candidate = fenced ? fenced[1].trim() : stripped;
156
+ const candidate = located.text;
129
157
  if (candidate.length === 0) {
130
158
  return fail('extractJsonText: no JSON content found.');
131
159
  }
@@ -145,6 +173,241 @@ export const extractJsonText = (text) => {
145
173
  }
146
174
  return fail('extractJsonText: no JSON-shaped substring found.');
147
175
  };
176
+ const UNKNOWN_PARSE_FAILURE = { kind: 'unknown' };
177
+ const JSON_WHITESPACE = /[ \t\n\r]/;
178
+ const IDENTIFIER_START = /[A-Za-z0-9_$]/;
179
+ const IDENTIFIER_PART = /[A-Za-z0-9_$.-]/;
180
+ // Characters that end an unquoted value run (`true`, `null`, a number, or
181
+ // garbage). Includes ':' so a stray colon at a value position produces an
182
+ // empty run — reported as 'unknown' — instead of being swallowed by a literal.
183
+ const VALUE_RUN_TERMINATOR = /[\s,}\]:"'{[]/;
184
+ function scanQuotedRun(text, start, quote) {
185
+ let escape = false;
186
+ for (let i = start + 1; i < text.length; i++) {
187
+ const ch = text.charAt(i);
188
+ if (escape) {
189
+ escape = false;
190
+ }
191
+ else if (ch === '\\') {
192
+ escape = true;
193
+ }
194
+ else if (ch === quote) {
195
+ return { terminated: true, end: i + 1, body: text.slice(start + 1, i) };
196
+ }
197
+ }
198
+ return { terminated: false, end: text.length, body: text.slice(start + 1) };
199
+ }
200
+ function skipJsonWhitespace(text, from) {
201
+ let i = from;
202
+ while (i < text.length && JSON_WHITESPACE.test(text.charAt(i))) {
203
+ i++;
204
+ }
205
+ return i;
206
+ }
207
+ function scanRun(text, from, isPart) {
208
+ let i = from;
209
+ while (i < text.length && isPart(text.charAt(i))) {
210
+ i++;
211
+ }
212
+ return text.slice(from, i);
213
+ }
214
+ function classifyAtPropertyName(text, i, base) {
215
+ const ch = text.charAt(i);
216
+ if (ch === '"') {
217
+ const scan = scanQuotedRun(text, i, '"');
218
+ if (scan.terminated) {
219
+ return { next: scan.end };
220
+ }
221
+ // An unterminated name is only distinguishable from a mid-name truncation
222
+ // when the swallowed body contains structural text — a ':' the author
223
+ // plainly meant as the name/value separator. Without that, stay honest.
224
+ return scan.body.includes(':')
225
+ ? { reason: { kind: 'unterminated-property-name', token: text.slice(i), offset: base + i } }
226
+ : { reason: UNKNOWN_PARSE_FAILURE };
227
+ }
228
+ if (ch === "'") {
229
+ const scan = scanQuotedRun(text, i, "'");
230
+ return {
231
+ reason: {
232
+ kind: 'single-quoted-property-name',
233
+ token: scan.terminated ? text.slice(i, scan.end) : "'",
234
+ offset: base + i
235
+ }
236
+ };
237
+ }
238
+ if (ch === ',') {
239
+ return { reason: { kind: 'elided-member', token: ',', offset: base + i } };
240
+ }
241
+ if (IDENTIFIER_START.test(ch)) {
242
+ return {
243
+ reason: {
244
+ kind: 'unquoted-property-name',
245
+ token: scanRun(text, i, (c) => IDENTIFIER_PART.test(c)),
246
+ offset: base + i
247
+ }
248
+ };
249
+ }
250
+ return { reason: UNKNOWN_PARSE_FAILURE };
251
+ }
252
+ /**
253
+ * Walks `text` from `from` (the opening `{` or `[`) as a permissive JSON
254
+ * scanner, reporting the first fault it can name with confidence. Anything it
255
+ * cannot name — including a document that scans clean — yields `'unknown'`.
256
+ *
257
+ * The scan is structural: it never reads the engine's `JSON.parse` message, so
258
+ * its verdicts do not drift with the Node version.
259
+ * @internal
260
+ */
261
+ function scanForParseFailure(text, from, base) {
262
+ const stack = [];
263
+ let expect = 'value';
264
+ let i = from;
265
+ for (;;) {
266
+ i = skipJsonWhitespace(text, i);
267
+ if (i >= text.length) {
268
+ return UNKNOWN_PARSE_FAILURE;
269
+ }
270
+ const ch = text.charAt(i);
271
+ const top = stack[stack.length - 1];
272
+ if (expect === 'name') {
273
+ if (ch === '}') {
274
+ stack.pop();
275
+ i++;
276
+ expect = 'comma';
277
+ continue;
278
+ }
279
+ const outcome = classifyAtPropertyName(text, i, base);
280
+ if ('reason' in outcome) {
281
+ return outcome.reason;
282
+ }
283
+ i = outcome.next;
284
+ expect = 'colon';
285
+ continue;
286
+ }
287
+ if (expect === 'colon') {
288
+ if (ch !== ':') {
289
+ return UNKNOWN_PARSE_FAILURE;
290
+ }
291
+ i++;
292
+ expect = 'value';
293
+ continue;
294
+ }
295
+ if (expect === 'value') {
296
+ if (ch === '{' || ch === '[') {
297
+ stack.push(ch === '{' ? 'object' : 'array');
298
+ i++;
299
+ expect = ch === '{' ? 'name' : 'value';
300
+ continue;
301
+ }
302
+ if (ch === ']' && top === 'array') {
303
+ stack.pop();
304
+ i++;
305
+ expect = 'comma';
306
+ continue;
307
+ }
308
+ if (ch === ',') {
309
+ return top === 'array'
310
+ ? { kind: 'elided-member', token: ',', offset: base + i }
311
+ : UNKNOWN_PARSE_FAILURE;
312
+ }
313
+ if (ch === '"') {
314
+ const scan = scanQuotedRun(text, i, '"');
315
+ if (!scan.terminated) {
316
+ // Could equally be a truncated value; the extractor already names
317
+ // truncation, so do not compete with it here.
318
+ return UNKNOWN_PARSE_FAILURE;
319
+ }
320
+ i = scan.end;
321
+ expect = 'comma';
322
+ continue;
323
+ }
324
+ const literal = scanRun(text, i, (c) => !VALUE_RUN_TERMINATOR.test(c));
325
+ if (literal.length === 0) {
326
+ return UNKNOWN_PARSE_FAILURE;
327
+ }
328
+ i += literal.length;
329
+ expect = 'comma';
330
+ continue;
331
+ }
332
+ // expect === 'comma': a complete value just closed.
333
+ if (top === undefined) {
334
+ return UNKNOWN_PARSE_FAILURE;
335
+ }
336
+ if (ch === ',') {
337
+ i++;
338
+ expect = top === 'object' ? 'name' : 'value';
339
+ continue;
340
+ }
341
+ if ((ch === '}' && top === 'object') || (ch === ']' && top === 'array')) {
342
+ stack.pop();
343
+ i++;
344
+ continue;
345
+ }
346
+ return UNKNOWN_PARSE_FAILURE;
347
+ }
348
+ }
349
+ /**
350
+ * Classifies why a JSON-shaped LLM response would not parse, returning a
351
+ * {@link AiAssist.JsonParseFailureReason} a caller can branch on — repair the
352
+ * cheap cases, re-prompt the expensive ones, fail outright on the rest —
353
+ * instead of regex-matching an engine-specific `JSON.parse` message.
354
+ *
355
+ * Pass the same raw model text you handed
356
+ * {@link AiAssist.fencedStringifiedJson} or {@link AiAssist.extractJsonText};
357
+ * this applies the same BOM / whitespace / fence / preamble handling before
358
+ * scanning, and reports `offset` against that original text.
359
+ *
360
+ * Classification is **structural and deliberately conservative**. The scan
361
+ * walks the JSON grammar itself rather than reading the engine's error string,
362
+ * so its verdicts are stable across Node versions — and any fault it cannot
363
+ * name with confidence comes back as `'unknown'` rather than a guess. In
364
+ * particular an input that opened a structure and never closed it (the
365
+ * truncated-response shape {@link AiAssist.extractJsonText} already diagnoses)
366
+ * classifies as `'unknown'` here; the two diagnostics are complementary, not
367
+ * competing.
368
+ *
369
+ * This never fails and never repairs — it only names the fault. It is a
370
+ * diagnostic on the failure path, so calling it on text that parses fine is
371
+ * harmless but pointless: it returns `'unknown'`.
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * const parsed = fencedStringifiedJson({ inner }).convert(raw);
376
+ * if (parsed.isFailure()) {
377
+ * const reason = classifyJsonParseFailure(raw);
378
+ * switch (reason.kind) {
379
+ * case 'unquoted-property-name': // cheap to repair
380
+ * case 'single-quoted-property-name':
381
+ * break;
382
+ * case 'elided-member':
383
+ * case 'unterminated-property-name': // worth a re-prompt
384
+ * break;
385
+ * default: // 'unknown' — fail outright
386
+ * }
387
+ * }
388
+ * ```
389
+ *
390
+ * @param text - Raw model output (the same string handed to the extractor).
391
+ * @returns A {@link AiAssist.JsonParseFailureReason}.
392
+ * @public
393
+ */
394
+ export function classifyJsonParseFailure(text) {
395
+ // Defensive, and load-bearing for the documented totality: the signature says
396
+ // `string`, but a JS consumer — or a TS caller coming through an `unknown` /
397
+ // `any` escape hatch — can still hand us a non-string, and `locateJsonCandidate`
398
+ // would throw on it. `extractJsonText` already guards the same way; this keeps
399
+ // the pair consistent and keeps "never fails" true in practice rather than only
400
+ // under a well-typed caller.
401
+ if (typeof text !== 'string') {
402
+ return UNKNOWN_PARSE_FAILURE;
403
+ }
404
+ const candidate = locateJsonCandidate(text);
405
+ const scan = findBalancedJsonSubstring(candidate.text);
406
+ if (scan.kind === 'none') {
407
+ return UNKNOWN_PARSE_FAILURE;
408
+ }
409
+ return scanForParseFailure(candidate.text, scan.start, candidate.base);
410
+ }
148
411
  export function fencedStringifiedJson(options) {
149
412
  var _a;
150
413
  const extractor = (_a = options === null || options === void 0 ? void 0 : options.extractor) !== null && _a !== void 0 ? _a : extractJsonText;
@@ -1 +1 @@
1
- {"version":3,"file":"jsonResponse.js","sourceRoot":"","sources":["../../../src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QAC7B,OAAO,IAAI,CACT,kEAAkE,IAAI,CAAC,KAAK,sBAAsB;YAChG,6FAA6F,CAChG,CAAC;IACJ,CAAC;IAED,OAAO,IAAI,CAAC,kDAAkD,CAAC,CAAC;AAClE,CAAC,CAAC;AAuDF,MAAM,UAAU,qBAAqB,CACnC,OAAmF;;IAEnF,MAAM,SAAS,GAAsB,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,mCAAI,eAAe,CAAC;IAC3E,MAAM,KAAK,GAAI,OAAwD,aAAxD,OAAO,uBAAP,OAAO,CAAmD,KAAK,CAAC;IAC/E,MAAM,MAAM,GACV,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,kBAAkB,CAAC,eAAe,CAAI,KAAK,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,eAAe,EAAE,CAAC;IAE5G,OAAO,IAAI,UAAU,CAAC,aAAa,CAAgB,CAAC,IAAa,EAAyB,EAAE;QAC1F,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,OAAO,IAAI,CAAC,gDAAgD,CAAC,CAAC;QAChE,CAAC;QACD,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n/**\n * JSON-tolerant extraction and converters for LLM responses.\n *\n * Models commonly wrap JSON output in Markdown code fences, add a\n * \"Sure, here's the JSON:\" preamble, or trail off with prose after the\n * closing brace. These helpers normalize that quirk on the read side so every\n * AiAssist consumer can reach a validated `T` from raw model text without\n * reimplementing the same fence-stripping logic.\n *\n * Scope: strip wrappers (fences, prose, BOM, whitespace). Out of scope: repair\n * malformed JSON (missing commas, unquoted keys, smart quotes, etc.).\n *\n * @packageDocumentation\n */\n\nimport { Conversion, type Converter, fail, Result, succeed, type Validator } from '@fgv/ts-utils';\nimport { Converters as JsonBaseConverters, type JsonValue } from '@fgv/ts-json-base';\n\n/**\n * A function that pulls a JSON-shaped substring out of arbitrary model text.\n * Implementations strip whatever wrappers the model added (fences, preamble,\n * trailing prose) and return the JSON-shaped substring ready for `JSON.parse`.\n * @public\n */\nexport type JsonTextExtractor = (text: string) => Result<string>;\n\nconst FENCED_BLOCK: RegExp = /```[A-Za-z0-9_-]*\\s*\\r?\\n([\\s\\S]*?)\\r?\\n?```/;\nconst BOM: RegExp = /^\\uFEFF/;\n// Full RFC 8259 grammar so the extractor only succeeds when the entire\n// candidate parses as a JSON primitive (instead of just starting like one).\nconst JSON_NUMBER: RegExp = /^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$/;\n// eslint-disable-next-line no-control-regex\nconst JSON_STRING: RegExp = /^\"(?:[^\"\\\\\\u0000-\\u001F]|\\\\(?:[\"\\\\/bfnrt]|u[0-9a-fA-F]{4}))*\"$/;\nconst JSON_KEYWORD: RegExp = /^(?:true|false|null)$/;\n\nfunction stripBom(text: string): string {\n return text.replace(BOM, '');\n}\n\n/**\n * Outcome of a {@link findBalancedJsonSubstring} scan.\n *\n * - `'found'`: a balanced JSON-shaped substring was located; `value` is it.\n * - `'unclosed'`: an object or array opened but the matching close never\n * arrived before the end of input — the classic truncated-response shape;\n * `depth` is the unresolved depth at end of input, counting only the\n * opening delimiter's type (the scan tracks the candidate's own `{`/`}`\n * or `[`/`]` pair, not total mixed-delimiter nesting).\n * - `'none'`: no `{` or `[` was ever seen outside a quoted string.\n * @internal\n */\ntype BalancedJsonScanResult =\n | { readonly kind: 'found'; readonly value: string }\n | { readonly kind: 'unclosed'; readonly depth: number }\n | { readonly kind: 'none' };\n\nfunction findBalancedJsonSubstring(text: string): BalancedJsonScanResult {\n // Walk the text once tracking string state. The first '{' or '[' that is\n // *outside* a quoted string is the candidate start; from there, count\n // matching close characters while ignoring delimiters that appear inside\n // strings.\n let inString = false;\n let escape = false;\n let start = -1;\n let open = '';\n let close = '';\n let depth = 0;\n for (let i = 0; i < text.length; i++) {\n const ch = text.charAt(i);\n if (inString) {\n if (escape) {\n escape = false;\n } else if (ch === '\\\\') {\n escape = true;\n } else if (ch === '\"') {\n inString = false;\n }\n continue;\n }\n if (ch === '\"') {\n inString = true;\n continue;\n }\n if (start < 0) {\n if (ch === '{' || ch === '[') {\n start = i;\n open = ch;\n close = ch === '{' ? '}' : ']';\n depth = 1;\n }\n continue;\n }\n if (ch === open) {\n depth++;\n } else if (ch === close) {\n depth--;\n if (depth === 0) {\n return { kind: 'found', value: text.slice(start, i + 1) };\n }\n }\n }\n return start >= 0 ? { kind: 'unclosed', depth } : { kind: 'none' };\n}\n\n/**\n * Default {@link AiAssist.JsonTextExtractor | extractor} for LLM responses. Tolerates:\n *\n * - Leading/trailing whitespace and a leading byte-order mark.\n * - Markdown code fences (with or without a language tag).\n * - Conversational preamble before the first `{` or `[`.\n * - Trailing prose after the matched closing `}` or `]`.\n *\n * Out of scope: repairing malformed JSON, handling smart quotes, etc.\n *\n * When an object or array opens but never closes before the end of input —\n * the shape a response truncated by a token cap leaves behind — the failure\n * names that specifically (distinct from the generic \"no JSON-shaped\n * substring found\" for input that never looked like JSON at all) and points\n * at `IAiCompletionResponse.truncated` and the `maxTokens` request option as\n * the likely cause and fix.\n *\n * @param text - Raw model output.\n * @returns A `Result<string>` containing the JSON-shaped substring, or a\n * `Failure` if no JSON-shaped substring was found.\n * @public\n */\nexport const extractJsonText: JsonTextExtractor = (text: string): Result<string> => {\n if (typeof text !== 'string') {\n return fail('extractJsonText: input must be a string.');\n }\n const stripped = stripBom(text).trim();\n if (stripped.length === 0) {\n return fail('extractJsonText: input is empty.');\n }\n\n const fenced = FENCED_BLOCK.exec(stripped);\n const candidate = fenced ? fenced[1].trim() : stripped;\n\n if (candidate.length === 0) {\n return fail('extractJsonText: no JSON content found.');\n }\n\n // Whole-candidate primitive check runs before the brace scan so that a\n // valid JSON string containing braces (e.g. `\"text with { }\"`) is returned\n // intact instead of being mangled into the first balanced `{ }` match.\n if (JSON_KEYWORD.test(candidate) || JSON_NUMBER.test(candidate) || JSON_STRING.test(candidate)) {\n return succeed(candidate);\n }\n\n const scan = findBalancedJsonSubstring(candidate);\n if (scan.kind === 'found') {\n return succeed(scan.value);\n }\n if (scan.kind === 'unclosed') {\n return fail(\n `extractJsonText: JSON structure opened but never closed (depth ${scan.depth} at end of input) — ` +\n 'response may have been truncated (check IAiCompletionResponse.truncated / raise maxTokens).'\n );\n }\n\n return fail('extractJsonText: no JSON-shaped substring found.');\n};\n\n/**\n * Options shared by every {@link AiAssist.fencedStringifiedJson} call.\n * @public\n */\nexport interface IFencedStringifiedJsonExtractorOptions {\n /**\n * Optional pre-parse extractor. Defaults to {@link AiAssist.extractJsonText}.\n * Provide a custom extractor to handle response shapes the default does not\n * understand.\n */\n readonly extractor?: JsonTextExtractor;\n}\n\n/**\n * Options for the validating overload of {@link AiAssist.fencedStringifiedJson}.\n * `inner` is required so the typed `Converter<T>` return value can never lie\n * about the runtime shape.\n * @public\n */\nexport interface IFencedStringifiedJsonOptions<T> extends IFencedStringifiedJsonExtractorOptions {\n /** Inner converter or validator applied to the parsed JSON value. */\n readonly inner: Converter<T> | Validator<T>;\n}\n\n/**\n * Creates a `Converter` that accepts raw LLM response text, runs it through a\n * tolerant extractor (default: {@link AiAssist.extractJsonText}), parses the\n * extracted substring as JSON, and applies an optional inner converter or\n * validator.\n *\n * @example\n * ```ts\n * const converter = fencedStringifiedJson({ inner: myShapeConverter });\n * const result = converter.convert(llmText); // Result<MyShape>\n * ```\n *\n * @param options - Optional extractor; omit to keep the default. Without an\n * `inner` step, the converter resolves to the parsed `JsonValue`.\n * @returns A `Converter<JsonValue>`.\n * @public\n */\nexport function fencedStringifiedJson(options?: IFencedStringifiedJsonExtractorOptions): Converter<JsonValue>;\n/**\n * Creates a `Converter` that accepts raw LLM response text, runs it through a\n * tolerant extractor (default: {@link AiAssist.extractJsonText}), parses the\n * extracted substring as JSON, and applies the supplied inner converter or\n * validator.\n *\n * @param options - Required `inner` converter/validator and optional extractor.\n * @returns A `Converter<T>`.\n * @public\n */\nexport function fencedStringifiedJson<T>(options: IFencedStringifiedJsonOptions<T>): Converter<T>;\nexport function fencedStringifiedJson<T>(\n options?: IFencedStringifiedJsonExtractorOptions | IFencedStringifiedJsonOptions<T>\n): Converter<T | JsonValue> {\n const extractor: JsonTextExtractor = options?.extractor ?? extractJsonText;\n const inner = (options as IFencedStringifiedJsonOptions<T> | undefined)?.inner;\n const parser: Converter<T | JsonValue> =\n inner !== undefined ? JsonBaseConverters.stringifiedJson<T>(inner) : JsonBaseConverters.stringifiedJson();\n\n return new Conversion.BaseConverter<T | JsonValue>((from: unknown): Result<T | JsonValue> => {\n if (typeof from !== 'string') {\n return fail('fencedStringifiedJson: input must be a string.');\n }\n return extractor(from).onSuccess((extracted) => parser.convert(extracted));\n });\n}\n"]}
1
+ {"version":3,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SAAS,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n/**\n * JSON-tolerant extraction and converters for LLM responses.\n *\n * Models commonly wrap JSON output in Markdown code fences, add a\n * \"Sure, here's the JSON:\" preamble, or trail off with prose after the\n * closing brace. These helpers normalize that quirk on the read side so every\n * AiAssist consumer can reach a validated `T` from raw model text without\n * reimplementing the same fence-stripping logic.\n *\n * Scope: strip wrappers (fences, prose, BOM, whitespace). Out of scope: repair\n * malformed JSON (missing commas, unquoted keys, smart quotes, etc.).\n *\n * @packageDocumentation\n */\n\nimport { Conversion, type Converter, fail, Result, succeed, type Validator } from '@fgv/ts-utils';\nimport { Converters as JsonBaseConverters, type JsonValue } from '@fgv/ts-json-base';\n\n/**\n * A function that pulls a JSON-shaped substring out of arbitrary model text.\n * Implementations strip whatever wrappers the model added (fences, preamble,\n * trailing prose) and return the JSON-shaped substring ready for `JSON.parse`.\n * @public\n */\nexport type JsonTextExtractor = (text: string) => Result<string>;\n\n// Group 1 is the opening fence (marker, optional language tag, newline) and\n// group 2 is the fenced body. Group 1 is captured only so a caller that needs\n// to map a position inside the body back onto an offset in the original text\n// can add its length instead of re-deriving the fence header.\nconst FENCED_BLOCK: RegExp = /(```[A-Za-z0-9_-]*\\s*\\r?\\n)([\\s\\S]*?)\\r?\\n?```/;\nconst BOM: RegExp = /^\\uFEFF/;\n// Full RFC 8259 grammar so the extractor only succeeds when the entire\n// candidate parses as a JSON primitive (instead of just starting like one).\nconst JSON_NUMBER: RegExp = /^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$/;\n// eslint-disable-next-line no-control-regex\nconst JSON_STRING: RegExp = /^\"(?:[^\"\\\\\\u0000-\\u001F]|\\\\(?:[\"\\\\/bfnrt]|u[0-9a-fA-F]{4}))*\"$/;\nconst JSON_KEYWORD: RegExp = /^(?:true|false|null)$/;\n\nfunction stripBom(text: string): string {\n return text.replace(BOM, '');\n}\n\n/**\n * Outcome of a {@link findBalancedJsonSubstring} scan.\n *\n * - `'found'`: a balanced JSON-shaped substring was located; `value` is it.\n * - `'unclosed'`: an object or array opened but the matching close never\n * arrived before the end of input — the classic truncated-response shape;\n * `depth` is the unresolved depth at end of input, counting only the\n * opening delimiter's type (the scan tracks the candidate's own `{`/`}`\n * or `[`/`]` pair, not total mixed-delimiter nesting).\n * - `'none'`: no `{` or `[` was ever seen outside a quoted string.\n *\n * `start` (present on `'found'` and `'unclosed'`) is the index of the opening\n * `{` or `[` within the scanned text.\n * @internal\n */\ntype BalancedJsonScanResult =\n | { readonly kind: 'found'; readonly value: string; readonly start: number }\n | { readonly kind: 'unclosed'; readonly depth: number; readonly start: number }\n | { readonly kind: 'none' };\n\nfunction findBalancedJsonSubstring(text: string): BalancedJsonScanResult {\n // Walk the text once tracking string state. The first '{' or '[' that is\n // *outside* a quoted string is the candidate start; from there, count\n // matching close characters while ignoring delimiters that appear inside\n // strings.\n let inString = false;\n let escape = false;\n let start = -1;\n let open = '';\n let close = '';\n let depth = 0;\n for (let i = 0; i < text.length; i++) {\n const ch = text.charAt(i);\n if (inString) {\n if (escape) {\n escape = false;\n } else if (ch === '\\\\') {\n escape = true;\n } else if (ch === '\"') {\n inString = false;\n }\n continue;\n }\n if (ch === '\"') {\n inString = true;\n continue;\n }\n if (start < 0) {\n if (ch === '{' || ch === '[') {\n start = i;\n open = ch;\n close = ch === '{' ? '}' : ']';\n depth = 1;\n }\n continue;\n }\n if (ch === open) {\n depth++;\n } else if (ch === close) {\n depth--;\n if (depth === 0) {\n return { kind: 'found', value: text.slice(start, i + 1), start };\n }\n }\n }\n return start >= 0 ? { kind: 'unclosed', depth, start } : { kind: 'none' };\n}\n\n/**\n * The JSON-shaped candidate located within raw model text, plus where it\n * starts in that text.\n * @internal\n */\ninterface IJsonCandidate {\n /** The whole input after BOM strip and trim, before any fence unwrap. */\n readonly stripped: string;\n /** The candidate itself: the trimmed fenced body when fenced, else `stripped`. */\n readonly text: string;\n /** Index of `text`'s first character within the original input. */\n readonly base: number;\n}\n\n/**\n * Strips the wrappers a model adds around JSON — byte-order mark, surrounding\n * whitespace, Markdown code fence — and reports where what is left starts in\n * the original text.\n *\n * Single source of truth for that step: {@link extractJsonText} and\n * {@link classifyJsonParseFailure} must agree on what the candidate is, or a\n * classified `offset` would point into text the extractor never parsed.\n * @internal\n */\nfunction locateJsonCandidate(text: string): IJsonCandidate {\n const noBom = stripBom(text);\n const stripped = noBom.trim();\n const strippedBase = text.length - noBom.length + (noBom.length - noBom.trimStart().length);\n\n const fenced = FENCED_BLOCK.exec(stripped);\n if (fenced === null) {\n return { stripped, text: stripped, base: strippedBase };\n }\n const body = fenced[2];\n return {\n stripped,\n text: body.trim(),\n base: strippedBase + fenced.index + fenced[1].length + (body.length - body.trimStart().length)\n };\n}\n\n/**\n * Default {@link AiAssist.JsonTextExtractor | extractor} for LLM responses. Tolerates:\n *\n * - Leading/trailing whitespace and a leading byte-order mark.\n * - Markdown code fences (with or without a language tag).\n * - Conversational preamble before the first `{` or `[`.\n * - Trailing prose after the matched closing `}` or `]`.\n *\n * Out of scope: repairing malformed JSON, handling smart quotes, etc.\n *\n * When an object or array opens but never closes before the end of input —\n * the shape a response truncated by a token cap leaves behind — the failure\n * names that specifically (distinct from the generic \"no JSON-shaped\n * substring found\" for input that never looked like JSON at all) and points\n * at `IAiCompletionResponse.truncated` and the `maxTokens` request option as\n * the likely cause and fix.\n *\n * @param text - Raw model output.\n * @returns A `Result<string>` containing the JSON-shaped substring, or a\n * `Failure` if no JSON-shaped substring was found.\n * @public\n */\nexport const extractJsonText: JsonTextExtractor = (text: string): Result<string> => {\n if (typeof text !== 'string') {\n return fail('extractJsonText: input must be a string.');\n }\n const located = locateJsonCandidate(text);\n if (located.stripped.length === 0) {\n return fail('extractJsonText: input is empty.');\n }\n\n const candidate = located.text;\n if (candidate.length === 0) {\n return fail('extractJsonText: no JSON content found.');\n }\n\n // Whole-candidate primitive check runs before the brace scan so that a\n // valid JSON string containing braces (e.g. `\"text with { }\"`) is returned\n // intact instead of being mangled into the first balanced `{ }` match.\n if (JSON_KEYWORD.test(candidate) || JSON_NUMBER.test(candidate) || JSON_STRING.test(candidate)) {\n return succeed(candidate);\n }\n\n const scan = findBalancedJsonSubstring(candidate);\n if (scan.kind === 'found') {\n return succeed(scan.value);\n }\n if (scan.kind === 'unclosed') {\n return fail(\n `extractJsonText: JSON structure opened but never closed (depth ${scan.depth} at end of input) — ` +\n 'response may have been truncated (check IAiCompletionResponse.truncated / raise maxTokens).'\n );\n }\n\n return fail('extractJsonText: no JSON-shaped substring found.');\n};\n\n/**\n * Typed reason a JSON-shaped LLM response failed to parse, so a caller can\n * branch on the failure class instead of regex-matching the engine's\n * `JSON.parse` message (whose wording varies across V8 / Node versions).\n *\n * Every classified arm describes a fault at an **object property-name\n * position** — the position an LLM most often gets wrong, and the one whose\n * repair strategy differs most by case:\n *\n * - `'unquoted-property-name'`: a bare identifier where a quoted name belongs\n * (`{ key: 1 }`). `token` is the identifier run, `offset` its first\n * character. Identifier recognition is ASCII-only, so a non-ASCII bare name\n * (`{ ключ: 1 }`) reports `'unknown'` rather than being named.\n * - `'single-quoted-property-name'`: a single-quoted name (`{ 'key': 1 }`).\n * `token` is the quoted literal (or just `'` if it never closes), `offset`\n * the opening quote.\n * - `'unterminated-property-name'`: a name whose closing `\"` is missing, and\n * whose body swallowed structural text (`{ \"key: 1 }`). `token` is the\n * unterminated fragment, `offset` the opening quote.\n * - `'elided-member'`: a `,` where a member is expected — a leading or doubled\n * comma in an object (`{ , \"a\": 1 }`, `{ \"a\":1, , \"b\":2 }`) or an array\n * (`[1, , 2]`). `token` is `','`, `offset` its position.\n * - `'unknown'`: the catch-all. The scan reached the end of the text, or hit a\n * fault it cannot name with confidence, and reports nothing rather than\n * guessing. Truncated responses, missing colons, trailing commas, bad number\n * literals, and anything else not listed above land here.\n *\n * `offset` is a 0-based index into the `text` passed to\n * {@link AiAssist.classifyJsonParseFailure}, not into the extracted substring.\n * @public\n */\nexport type JsonParseFailureReason =\n | { readonly kind: 'unquoted-property-name'; readonly token: string; readonly offset: number }\n | { readonly kind: 'single-quoted-property-name'; readonly token: string; readonly offset: number }\n | { readonly kind: 'unterminated-property-name'; readonly token: string; readonly offset: number }\n | { readonly kind: 'elided-member'; readonly token: string; readonly offset: number }\n | { readonly kind: 'unknown' };\n\nconst UNKNOWN_PARSE_FAILURE: JsonParseFailureReason = { kind: 'unknown' };\n\nconst JSON_WHITESPACE: RegExp = /[ \\t\\n\\r]/;\nconst IDENTIFIER_START: RegExp = /[A-Za-z0-9_$]/;\nconst IDENTIFIER_PART: RegExp = /[A-Za-z0-9_$.-]/;\n// Characters that end an unquoted value run (`true`, `null`, a number, or\n// garbage). Includes ':' so a stray colon at a value position produces an\n// empty run — reported as 'unknown' — instead of being swallowed by a literal.\nconst VALUE_RUN_TERMINATOR: RegExp = /[\\s,}\\]:\"'{[]/;\n\n/** Where the parse scan currently is within the JSON grammar. @internal */\ntype JsonScanExpectation = 'value' | 'name' | 'colon' | 'comma';\n\n/** Result of scanning a quoted run starting at a given opening quote. @internal */\ninterface IQuotedRunScan {\n readonly terminated: boolean;\n /** Index just past the closing quote, or end of text when unterminated. */\n readonly end: number;\n /** Content between the quotes, exclusive. */\n readonly body: string;\n}\n\nfunction scanQuotedRun(text: string, start: number, quote: string): IQuotedRunScan {\n let escape = false;\n for (let i = start + 1; i < text.length; i++) {\n const ch = text.charAt(i);\n if (escape) {\n escape = false;\n } else if (ch === '\\\\') {\n escape = true;\n } else if (ch === quote) {\n return { terminated: true, end: i + 1, body: text.slice(start + 1, i) };\n }\n }\n return { terminated: false, end: text.length, body: text.slice(start + 1) };\n}\n\nfunction skipJsonWhitespace(text: string, from: number): number {\n let i = from;\n while (i < text.length && JSON_WHITESPACE.test(text.charAt(i))) {\n i++;\n }\n return i;\n}\n\nfunction scanRun(text: string, from: number, isPart: (ch: string) => boolean): string {\n let i = from;\n while (i < text.length && isPart(text.charAt(i))) {\n i++;\n }\n return text.slice(from, i);\n}\n\nfunction classifyAtPropertyName(\n text: string,\n i: number,\n base: number\n): { readonly reason: JsonParseFailureReason } | { readonly next: number } {\n const ch = text.charAt(i);\n if (ch === '\"') {\n const scan = scanQuotedRun(text, i, '\"');\n if (scan.terminated) {\n return { next: scan.end };\n }\n // An unterminated name is only distinguishable from a mid-name truncation\n // when the swallowed body contains structural text — a ':' the author\n // plainly meant as the name/value separator. Without that, stay honest.\n return scan.body.includes(':')\n ? { reason: { kind: 'unterminated-property-name', token: text.slice(i), offset: base + i } }\n : { reason: UNKNOWN_PARSE_FAILURE };\n }\n if (ch === \"'\") {\n const scan = scanQuotedRun(text, i, \"'\");\n return {\n reason: {\n kind: 'single-quoted-property-name',\n token: scan.terminated ? text.slice(i, scan.end) : \"'\",\n offset: base + i\n }\n };\n }\n if (ch === ',') {\n return { reason: { kind: 'elided-member', token: ',', offset: base + i } };\n }\n if (IDENTIFIER_START.test(ch)) {\n return {\n reason: {\n kind: 'unquoted-property-name',\n token: scanRun(text, i, (c) => IDENTIFIER_PART.test(c)),\n offset: base + i\n }\n };\n }\n return { reason: UNKNOWN_PARSE_FAILURE };\n}\n\n/**\n * Walks `text` from `from` (the opening `{` or `[`) as a permissive JSON\n * scanner, reporting the first fault it can name with confidence. Anything it\n * cannot name — including a document that scans clean — yields `'unknown'`.\n *\n * The scan is structural: it never reads the engine's `JSON.parse` message, so\n * its verdicts do not drift with the Node version.\n * @internal\n */\nfunction scanForParseFailure(text: string, from: number, base: number): JsonParseFailureReason {\n const stack: Array<'object' | 'array'> = [];\n let expect: JsonScanExpectation = 'value';\n let i = from;\n\n for (;;) {\n i = skipJsonWhitespace(text, i);\n if (i >= text.length) {\n return UNKNOWN_PARSE_FAILURE;\n }\n const ch = text.charAt(i);\n const top = stack[stack.length - 1];\n\n if (expect === 'name') {\n if (ch === '}') {\n stack.pop();\n i++;\n expect = 'comma';\n continue;\n }\n const outcome = classifyAtPropertyName(text, i, base);\n if ('reason' in outcome) {\n return outcome.reason;\n }\n i = outcome.next;\n expect = 'colon';\n continue;\n }\n\n if (expect === 'colon') {\n if (ch !== ':') {\n return UNKNOWN_PARSE_FAILURE;\n }\n i++;\n expect = 'value';\n continue;\n }\n\n if (expect === 'value') {\n if (ch === '{' || ch === '[') {\n stack.push(ch === '{' ? 'object' : 'array');\n i++;\n expect = ch === '{' ? 'name' : 'value';\n continue;\n }\n if (ch === ']' && top === 'array') {\n stack.pop();\n i++;\n expect = 'comma';\n continue;\n }\n if (ch === ',') {\n return top === 'array'\n ? { kind: 'elided-member', token: ',', offset: base + i }\n : UNKNOWN_PARSE_FAILURE;\n }\n if (ch === '\"') {\n const scan = scanQuotedRun(text, i, '\"');\n if (!scan.terminated) {\n // Could equally be a truncated value; the extractor already names\n // truncation, so do not compete with it here.\n return UNKNOWN_PARSE_FAILURE;\n }\n i = scan.end;\n expect = 'comma';\n continue;\n }\n const literal = scanRun(text, i, (c) => !VALUE_RUN_TERMINATOR.test(c));\n if (literal.length === 0) {\n return UNKNOWN_PARSE_FAILURE;\n }\n i += literal.length;\n expect = 'comma';\n continue;\n }\n\n // expect === 'comma': a complete value just closed.\n if (top === undefined) {\n return UNKNOWN_PARSE_FAILURE;\n }\n if (ch === ',') {\n i++;\n expect = top === 'object' ? 'name' : 'value';\n continue;\n }\n if ((ch === '}' && top === 'object') || (ch === ']' && top === 'array')) {\n stack.pop();\n i++;\n continue;\n }\n return UNKNOWN_PARSE_FAILURE;\n }\n}\n\n/**\n * Classifies why a JSON-shaped LLM response would not parse, returning a\n * {@link AiAssist.JsonParseFailureReason} a caller can branch on — repair the\n * cheap cases, re-prompt the expensive ones, fail outright on the rest —\n * instead of regex-matching an engine-specific `JSON.parse` message.\n *\n * Pass the same raw model text you handed\n * {@link AiAssist.fencedStringifiedJson} or {@link AiAssist.extractJsonText};\n * this applies the same BOM / whitespace / fence / preamble handling before\n * scanning, and reports `offset` against that original text.\n *\n * Classification is **structural and deliberately conservative**. The scan\n * walks the JSON grammar itself rather than reading the engine's error string,\n * so its verdicts are stable across Node versions — and any fault it cannot\n * name with confidence comes back as `'unknown'` rather than a guess. In\n * particular an input that opened a structure and never closed it (the\n * truncated-response shape {@link AiAssist.extractJsonText} already diagnoses)\n * classifies as `'unknown'` here; the two diagnostics are complementary, not\n * competing.\n *\n * This never fails and never repairs — it only names the fault. It is a\n * diagnostic on the failure path, so calling it on text that parses fine is\n * harmless but pointless: it returns `'unknown'`.\n *\n * @example\n * ```ts\n * const parsed = fencedStringifiedJson({ inner }).convert(raw);\n * if (parsed.isFailure()) {\n * const reason = classifyJsonParseFailure(raw);\n * switch (reason.kind) {\n * case 'unquoted-property-name': // cheap to repair\n * case 'single-quoted-property-name':\n * break;\n * case 'elided-member':\n * case 'unterminated-property-name': // worth a re-prompt\n * break;\n * default: // 'unknown' — fail outright\n * }\n * }\n * ```\n *\n * @param text - Raw model output (the same string handed to the extractor).\n * @returns A {@link AiAssist.JsonParseFailureReason}.\n * @public\n */\nexport function classifyJsonParseFailure(text: string): JsonParseFailureReason {\n // Defensive, and load-bearing for the documented totality: the signature says\n // `string`, but a JS consumer — or a TS caller coming through an `unknown` /\n // `any` escape hatch — can still hand us a non-string, and `locateJsonCandidate`\n // would throw on it. `extractJsonText` already guards the same way; this keeps\n // the pair consistent and keeps \"never fails\" true in practice rather than only\n // under a well-typed caller.\n if (typeof text !== 'string') {\n return UNKNOWN_PARSE_FAILURE;\n }\n const candidate = locateJsonCandidate(text);\n const scan = findBalancedJsonSubstring(candidate.text);\n if (scan.kind === 'none') {\n return UNKNOWN_PARSE_FAILURE;\n }\n return scanForParseFailure(candidate.text, scan.start, candidate.base);\n}\n\n/**\n * Options shared by every {@link AiAssist.fencedStringifiedJson} call.\n * @public\n */\nexport interface IFencedStringifiedJsonExtractorOptions {\n /**\n * Optional pre-parse extractor. Defaults to {@link AiAssist.extractJsonText}.\n * Provide a custom extractor to handle response shapes the default does not\n * understand.\n */\n readonly extractor?: JsonTextExtractor;\n}\n\n/**\n * Options for the validating overload of {@link AiAssist.fencedStringifiedJson}.\n * `inner` is required so the typed `Converter<T>` return value can never lie\n * about the runtime shape.\n * @public\n */\nexport interface IFencedStringifiedJsonOptions<T> extends IFencedStringifiedJsonExtractorOptions {\n /** Inner converter or validator applied to the parsed JSON value. */\n readonly inner: Converter<T> | Validator<T>;\n}\n\n/**\n * Creates a `Converter` that accepts raw LLM response text, runs it through a\n * tolerant extractor (default: {@link AiAssist.extractJsonText}), parses the\n * extracted substring as JSON, and applies an optional inner converter or\n * validator.\n *\n * @example\n * ```ts\n * const converter = fencedStringifiedJson({ inner: myShapeConverter });\n * const result = converter.convert(llmText); // Result<MyShape>\n * ```\n *\n * @param options - Optional extractor; omit to keep the default. Without an\n * `inner` step, the converter resolves to the parsed `JsonValue`.\n * @returns A `Converter<JsonValue>`.\n * @public\n */\nexport function fencedStringifiedJson(options?: IFencedStringifiedJsonExtractorOptions): Converter<JsonValue>;\n/**\n * Creates a `Converter` that accepts raw LLM response text, runs it through a\n * tolerant extractor (default: {@link AiAssist.extractJsonText}), parses the\n * extracted substring as JSON, and applies the supplied inner converter or\n * validator.\n *\n * @param options - Required `inner` converter/validator and optional extractor.\n * @returns A `Converter<T>`.\n * @public\n */\nexport function fencedStringifiedJson<T>(options: IFencedStringifiedJsonOptions<T>): Converter<T>;\nexport function fencedStringifiedJson<T>(\n options?: IFencedStringifiedJsonExtractorOptions | IFencedStringifiedJsonOptions<T>\n): Converter<T | JsonValue> {\n const extractor: JsonTextExtractor = options?.extractor ?? extractJsonText;\n const inner = (options as IFencedStringifiedJsonOptions<T> | undefined)?.inner;\n const parser: Converter<T | JsonValue> =\n inner !== undefined ? JsonBaseConverters.stringifiedJson<T>(inner) : JsonBaseConverters.stringifiedJson();\n\n return new Conversion.BaseConverter<T | JsonValue>((from: unknown): Result<T | JsonValue> => {\n if (typeof from !== 'string') {\n return fail('fencedStringifiedJson: input must be a string.');\n }\n return extractor(from).onSuccess((extracted) => parser.convert(extracted));\n });\n}\n"]}
@@ -216,6 +216,20 @@ export function resolveModelAlias(descriptor, model) {
216
216
  * `ModelSpec` branch is selected first; the resulting string — which may itself
217
217
  * be an fgv alias — is then resolved to a concrete id.
218
218
  *
219
+ * **This is the whole model-selection surface — do not hand-roll the walk.**
220
+ * Callers should pass the `ModelSpecKey` they want and use the concrete id
221
+ * this returns; a manual `resolveModel` + `resolveModelAlias` sequence is
222
+ * both redundant and easy to get wrong (it is how alias-form ids leak into
223
+ * capability lookups such as `resolveImageCapability`).
224
+ *
225
+ * **`context` carries the quality tier and the modality — never tools or
226
+ * thinking.** `ModelSpecKey` has no `tools` / `thinking` key: server-side
227
+ * tools and reasoning effort are orthogonal request params that ride on top of
228
+ * whatever model the tier selected, and never select a model. A tool-path caller
229
+ * passes a tier like any other caller — omit `context` for `base`, or pass
230
+ * `'advanced'` / `'frontier'` — and sets the tools / thinking request params
231
+ * separately.
232
+ *
219
233
  * @param descriptor - The provider descriptor (supplies `defaultModel` and `aliases`).
220
234
  * @param modelOverride - An optional caller-supplied `ModelSpec` that takes precedence
221
235
  * over `descriptor.defaultModel`. May itself contain or be an alias.