@vybestack/llxprt-code-tools 0.11.0 → 0.12.0-nightly.260910.2aac6841e

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 (65) hide show
  1. package/dist/src/acquisition/bounded-http-response.d.ts +14 -17
  2. package/dist/src/acquisition/bounded-http-response.js +124 -99
  3. package/dist/src/acquisition/bounded-http-response.js.map +1 -1
  4. package/dist/src/formatters/ToolFormatter.js +7 -8
  5. package/dist/src/formatters/ToolFormatter.js.map +1 -1
  6. package/dist/src/index.d.ts +1 -1
  7. package/dist/src/index.js +1 -1
  8. package/dist/src/index.js.map +1 -1
  9. package/dist/src/interfaces/ISkillService.d.ts +0 -6
  10. package/dist/src/interfaces/IToolHost.d.ts +0 -11
  11. package/dist/src/test-utils/loopback-test-helpers.d.ts +20 -0
  12. package/dist/src/test-utils/loopback-test-helpers.js +158 -0
  13. package/dist/src/test-utils/loopback-test-helpers.js.map +1 -0
  14. package/dist/src/tools/activate-skill.js +7 -18
  15. package/dist/src/tools/activate-skill.js.map +1 -1
  16. package/dist/src/tools/codesearch.js +0 -1
  17. package/dist/src/tools/codesearch.js.map +1 -1
  18. package/dist/src/tools/direct-web-fetch.js +25 -14
  19. package/dist/src/tools/direct-web-fetch.js.map +1 -1
  20. package/dist/src/tools/exa-web-search.js +0 -1
  21. package/dist/src/tools/exa-web-search.js.map +1 -1
  22. package/dist/src/tools/github-ops.d.ts +1 -1
  23. package/dist/src/tools/github-ops.js +16 -1
  24. package/dist/src/tools/github-ops.js.map +1 -1
  25. package/dist/src/tools/github.js +3 -2
  26. package/dist/src/tools/github.js.map +1 -1
  27. package/dist/src/tools/read-file.d.ts +0 -4
  28. package/dist/src/tools/read-file.js +1 -5
  29. package/dist/src/tools/read-file.js.map +1 -1
  30. package/dist/src/tools/tool-registry.js +0 -11
  31. package/dist/src/tools/tool-registry.js.map +1 -1
  32. package/dist/src/types/wire-types.d.ts +11 -0
  33. package/dist/src/utils/fileUtils.js +4 -1
  34. package/dist/src/utils/fileUtils.js.map +1 -1
  35. package/dist/src/utils/flatOrNestedSetting.d.ts +19 -0
  36. package/dist/src/utils/flatOrNestedSetting.js +55 -0
  37. package/dist/src/utils/flatOrNestedSetting.js.map +1 -0
  38. package/dist/src/utils/imageResize.d.ts +11 -1
  39. package/dist/src/utils/imageResize.js +27 -9
  40. package/dist/src/utils/imageResize.js.map +1 -1
  41. package/dist/src/utils/imageTokenEstimation.d.ts +25 -1
  42. package/dist/src/utils/imageTokenEstimation.js +93 -7
  43. package/dist/src/utils/imageTokenEstimation.js.map +1 -1
  44. package/dist/src/utils/mediaUtils.d.ts +2 -2
  45. package/package.json +7 -4
  46. package/src/acquisition/bounded-http-response.ts +132 -112
  47. package/src/formatters/ToolFormatter.ts +8 -8
  48. package/src/index.ts +1 -0
  49. package/src/interfaces/ISkillService.ts +0 -7
  50. package/src/interfaces/IToolHost.ts +0 -13
  51. package/src/test-utils/loopback-test-helpers.ts +214 -0
  52. package/src/tools/activate-skill.ts +9 -23
  53. package/src/tools/codesearch.ts +2 -4
  54. package/src/tools/direct-web-fetch.ts +32 -20
  55. package/src/tools/exa-web-search.ts +1 -3
  56. package/src/tools/github-ops.ts +18 -1
  57. package/src/tools/github.ts +4 -2
  58. package/src/tools/read-file.ts +1 -14
  59. package/src/tools/tool-registry.ts +0 -15
  60. package/src/types/wire-types.ts +8 -0
  61. package/src/utils/fileUtils.ts +9 -0
  62. package/src/utils/flatOrNestedSetting.ts +63 -0
  63. package/src/utils/imageResize.ts +41 -9
  64. package/src/utils/imageTokenEstimation.ts +109 -5
  65. package/src/utils/mediaUtils.ts +2 -2
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Vybestack LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ function isRecord(value: unknown): value is Record<string, unknown> {
8
+ return typeof value === 'object' && value !== null;
9
+ }
10
+
11
+ /**
12
+ * Own-property check for settings lookups. `in` would also match inherited
13
+ * properties, letting prototype values supply settings the user never
14
+ * configured.
15
+ */
16
+ function hasOwn(record: Record<string, unknown>, key: string): boolean {
17
+ return Object.prototype.hasOwnProperty.call(record, key);
18
+ }
19
+
20
+ /**
21
+ * Key segments that must never be traversed. Following them lets a lookup
22
+ * walk into Object.prototype machinery (or an own `__proto__` data property
23
+ * planted by a JSON producer), so settings reads treat them as missing.
24
+ */
25
+ const PROTOTYPE_KEY_SEGMENTS: ReadonlySet<string> = new Set([
26
+ '__proto__',
27
+ 'constructor',
28
+ 'prototype',
29
+ ]);
30
+
31
+ /**
32
+ * Read a setting that may live under its full dotted key or as a nested
33
+ * object tree. SettingsService.set() stores dotted keys nested
34
+ * (global['image-resize'].maxLongEdge) while other producers of the plural
35
+ * ephemeral map (alias modelDefaults output, stubs, user settings files)
36
+ * use the flat dotted key. Readers of that map must accept both shapes or
37
+ * silently see undefined for one of them (issue #3477).
38
+ *
39
+ * Only own properties are considered; values inherited from a prototype
40
+ * never supply a setting.
41
+ *
42
+ * The flat key wins when both shapes are present.
43
+ */
44
+ export function readSettingFlatOrNested(
45
+ settings: Readonly<Record<string, unknown>>,
46
+ dottedKey: string,
47
+ ): unknown {
48
+ if (hasOwn(settings, dottedKey)) {
49
+ return settings[dottedKey];
50
+ }
51
+ let current: unknown = settings;
52
+ for (const part of dottedKey.split('.')) {
53
+ if (
54
+ !isRecord(current) ||
55
+ PROTOTYPE_KEY_SEGMENTS.has(part) ||
56
+ !hasOwn(current, part)
57
+ ) {
58
+ return undefined;
59
+ }
60
+ current = current[part];
61
+ }
62
+ return current;
63
+ }
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import sharp, { type Metadata, type Sharp } from 'sharp';
8
+ import { readSettingFlatOrNested } from './flatOrNestedSetting.js';
8
9
 
9
10
  export interface ImageResizePolicy {
10
11
  readonly maxLongEdge?: number;
@@ -12,6 +13,39 @@ export interface ImageResizePolicy {
12
13
  readonly maxPixels?: number;
13
14
  }
14
15
 
16
+ export interface ImageResizeSourceMetadata {
17
+ readonly originalData: string;
18
+ readonly originalMimeType: string;
19
+ readonly transformation: {
20
+ readonly policyId: 'image-resize';
21
+ readonly policyVersion: 1;
22
+ readonly parameters: Readonly<Record<string, number>>;
23
+ };
24
+ }
25
+
26
+ export function createImageResizeSourceMetadata(
27
+ original: Buffer,
28
+ resized: Buffer,
29
+ mimeType: string,
30
+ policy: ImageResizePolicy | undefined,
31
+ ): ImageResizeSourceMetadata | undefined {
32
+ if (original === resized || policy === undefined) return undefined;
33
+ const parameters = {
34
+ ...(policy.maxLongEdge === undefined
35
+ ? {}
36
+ : { maxLongEdge: policy.maxLongEdge }),
37
+ ...(policy.maxShortEdge === undefined
38
+ ? {}
39
+ : { maxShortEdge: policy.maxShortEdge }),
40
+ ...(policy.maxPixels === undefined ? {} : { maxPixels: policy.maxPixels }),
41
+ };
42
+ return {
43
+ originalData: original.toString('base64'),
44
+ originalMimeType: mimeType,
45
+ transformation: { policyId: 'image-resize', policyVersion: 1, parameters },
46
+ };
47
+ }
48
+
15
49
  export class ImageResizeError extends Error {
16
50
  constructor(displayName: string, reason: string) {
17
51
  super(`Unable to resize image ${displayName}: ${reason}`);
@@ -119,7 +153,9 @@ function readPositiveInteger(
119
153
  settings: Readonly<Record<string, unknown>>,
120
154
  key: string,
121
155
  ): number | undefined {
122
- const value = settings[key];
156
+ // Dotted keys reach us both flat and nested depending on the producer of
157
+ // the settings map (see readSettingFlatOrNested), so never index directly.
158
+ const value = readSettingFlatOrNested(settings, key);
123
159
  if (value === undefined) {
124
160
  return undefined;
125
161
  }
@@ -133,25 +169,21 @@ function readPositiveInteger(
133
169
 
134
170
  export function resolveImageResizePolicy(
135
171
  settings: Readonly<Record<string, unknown>>,
136
- skipImageResize = false,
137
172
  ): ImageResizePolicy | undefined {
138
- if (skipImageResize) {
139
- return undefined;
140
- }
141
- const enabled = settings['image-resize.enabled'];
173
+ const enabled = readSettingFlatOrNested(settings, 'image-resize.enabled');
142
174
  if (enabled !== undefined && typeof enabled !== 'boolean') {
143
175
  throw new Error(
144
176
  'Invalid image resize settings: image-resize.enabled must be a boolean',
145
177
  );
146
178
  }
147
- if (enabled === false) {
148
- return undefined;
149
- }
150
179
  const policy: ImageResizePolicy = {
151
180
  maxLongEdge: readPositiveInteger(settings, 'image-resize.maxLongEdge'),
152
181
  maxShortEdge: readPositiveInteger(settings, 'image-resize.maxShortEdge'),
153
182
  maxPixels: readPositiveInteger(settings, 'image-resize.maxPixels'),
154
183
  };
184
+ if (enabled === false) {
185
+ return undefined;
186
+ }
155
187
  if (!hasLimits(policy)) {
156
188
  if (enabled === true) {
157
189
  throw new Error(
@@ -56,7 +56,13 @@ const ANTHROPIC_UNKNOWN_DIMENSIONS_TOKENS = Math.ceil(
56
56
  * preferred over an arbitrarily large constant that would make context
57
57
  * compression fire spuriously for every URL-referenced image.
58
58
  */
59
- const OPENAI_UNKNOWN_DIMENSIONS_TOKENS = 1105;
59
+ const OPENAI_LEGACY_UNKNOWN_DIMENSIONS_TOKENS = 1105;
60
+
61
+ /**
62
+ * GPT-5.2+ patch formula caps at 1536 patches, so the worst case at standard
63
+ * detail is ceil(1.2 x 1536) = 1844 tokens.
64
+ */
65
+ const OPENAI_PATCH_UNKNOWN_DIMENSIONS_TOKENS = 1844;
60
66
 
61
67
  /** OpenAI first fits the image inside a square of this many pixels per edge. */
62
68
  const OPENAI_MAX_EDGE = 2048;
@@ -73,9 +79,101 @@ const OPENAI_TOKENS_PER_TILE = 170;
73
79
  /** OpenAI charges this base cost for every high-detail image. */
74
80
  const OPENAI_BASE_TOKENS = 85;
75
81
 
82
+ /** GPT-5.2+ patch formula: max patches at standard detail. */
83
+ const OPENAI_PATCH_MAX_PATCHES = 1536;
84
+
85
+ /** GPT-5.2+ patch formula: multiplier applied to patch count. */
86
+ const OPENAI_PATCH_MULTIPLIER = 1.2;
87
+
88
+ /**
89
+ * GPT-5.2+ patch formula: ceil(1.2 x min(ceil(w/32) x ceil(h/32), 1536)).
90
+ * Maximum 1844 tokens at standard detail.
91
+ */
92
+ function estimateOpenaiPatchTokens(dimensions: ImageDimensions): number {
93
+ const patchesW = Math.ceil(dimensions.width / 32);
94
+ const patchesH = Math.ceil(dimensions.height / 32);
95
+ const patches = Math.min(patchesW * patchesH, OPENAI_PATCH_MAX_PATCHES);
96
+ return Math.ceil(OPENAI_PATCH_MULTIPLIER * patches);
97
+ }
98
+
99
+ /** Classification of an OpenAI-family model for image token estimation. */
100
+ export type OpenaiImageTokenGeneration = 'patch' | 'legacy' | 'unknown';
101
+
102
+ /**
103
+ * Classify a model name for OpenAI image token estimation.
104
+ *
105
+ * - 'patch': GPT-5.2 and newer, including any later minor (gpt-5.6,
106
+ * gpt-5.10), named variants like gpt-5.6-sol/terra/luna, and every
107
+ * GPT-6+ model (gpt-6, gpt-10). The major/minor version is parsed
108
+ * numerically so multi-digit versions classify correctly.
109
+ * - 'legacy': gpt-4o, gpt-4.1, gpt-5.0, gpt-5.1, and o-series models
110
+ * (o1, o3, o4-mini).
111
+ * - 'unknown': model is provided but does not match either category.
112
+ * Callers in the OpenAI family should use the patch formula (conservative-high).
113
+ * - Returns 'unknown' for undefined/empty model so callers can decide.
114
+ */
115
+ export function classifyOpenaiModel(
116
+ model: string | undefined,
117
+ ): OpenaiImageTokenGeneration {
118
+ if (model === undefined || model.length === 0) return 'unknown';
119
+ const normalized = model.trim().toLowerCase();
120
+ const generation = classifyNormalizedOpenaiModel(normalized);
121
+ return generation;
122
+ }
123
+
124
+ function classifyNormalizedOpenaiModel(
125
+ normalized: string,
126
+ ): OpenaiImageTokenGeneration {
127
+ // Patch when major > 5 (gpt-6, gpt-10, ...) or major == 5 with a minor
128
+ // of at least 2 (gpt-5.2, gpt-5.10). Parsed numerically because regex
129
+ // digit ranges cannot express ">= 2" or ">= 6" for multi-digit versions.
130
+ const version = /^gpt-(\d+)(?:\.(\d+))?/.exec(normalized);
131
+ if (version !== null) {
132
+ const major = parseInt(version[1], 10);
133
+ // The optional minor group is either absent (undefined, falsy) or a
134
+ // digit run (never empty), so a truthiness check separates the cases.
135
+ const minor = version[2] ? parseInt(version[2], 10) : 0;
136
+ if (major > 5 || (major === 5 && minor >= 2)) return 'patch';
137
+ }
138
+ // Known legacy models
139
+ if (isLegacyOpenaiModel(normalized)) return 'legacy';
140
+ return 'unknown';
141
+ }
142
+
143
+ const LEGACY_OPENAI_MODEL_PATTERNS: readonly RegExp[] = [
144
+ /^gpt-4o/,
145
+ /^gpt-4\.1/,
146
+ /^gpt-5\.0/,
147
+ /^gpt-5\.1/,
148
+ /^o[1-9]/,
149
+ ];
150
+
151
+ function isLegacyOpenaiModel(normalized: string): boolean {
152
+ return LEGACY_OPENAI_MODEL_PATTERNS.some((pattern) =>
153
+ pattern.test(normalized),
154
+ );
155
+ }
156
+
157
+ /**
158
+ * Determine whether a model name belongs to the GPT-5.2-or-newer generation
159
+ * that uses the patch-based image token formula. For unknown models in the
160
+ * OpenAI family, returns true (conservative-high, per issue #3477).
161
+ * Returns false for undefined/empty model (no basis to choose).
162
+ */
163
+ export function isGpt52OrNewer(model: string | undefined): boolean {
164
+ const generation = classifyOpenaiModel(model);
165
+ if (generation === 'unknown') {
166
+ // Unknown but non-empty model in the openai family: use patch formula
167
+ // (conservative-high). Undefined/empty model: no basis, use legacy.
168
+ return model !== undefined && model.trim().length > 0;
169
+ }
170
+ return generation === 'patch';
171
+ }
172
+
76
173
  export interface ImageTokenEstimateInput {
77
174
  readonly provider?: string;
78
175
  readonly dimensions?: ImageDimensions;
176
+ readonly model?: string;
79
177
  }
80
178
 
81
179
  /**
@@ -165,13 +263,16 @@ function estimateOpenaiTokens(dimensions: ImageDimensions): number {
165
263
  function estimateForFamily(
166
264
  family: ImageTokenProviderFamily,
167
265
  dimensions: ImageDimensions | undefined,
266
+ model: string | undefined,
168
267
  ): number {
169
268
  if (!hasValidDimensions(dimensions)) {
170
269
  switch (family) {
171
270
  case 'anthropic':
172
271
  return ANTHROPIC_UNKNOWN_DIMENSIONS_TOKENS;
173
272
  case 'openai':
174
- return OPENAI_UNKNOWN_DIMENSIONS_TOKENS;
273
+ return isGpt52OrNewer(model)
274
+ ? OPENAI_PATCH_UNKNOWN_DIMENSIONS_TOKENS
275
+ : OPENAI_LEGACY_UNKNOWN_DIMENSIONS_TOKENS;
175
276
  case 'gemini':
176
277
  return FLAT_IMAGE_TOKEN_ESTIMATE;
177
278
  default:
@@ -182,7 +283,9 @@ function estimateForFamily(
182
283
  case 'anthropic':
183
284
  return estimateAnthropicTokens(dimensions);
184
285
  case 'openai':
185
- return estimateOpenaiTokens(dimensions);
286
+ return isGpt52OrNewer(model)
287
+ ? estimateOpenaiPatchTokens(dimensions)
288
+ : estimateOpenaiTokens(dimensions);
186
289
  case 'gemini':
187
290
  return FLAT_IMAGE_TOKEN_ESTIMATE;
188
291
  default:
@@ -196,7 +299,7 @@ function estimateForFamily(
196
299
  */
197
300
  export function estimateImageTokens(input: ImageTokenEstimateInput): number {
198
301
  const family = resolveImageTokenProviderFamily(input.provider);
199
- return estimateForFamily(family, input.dimensions);
302
+ return estimateForFamily(family, input.dimensions, input.model);
200
303
  }
201
304
 
202
305
  /**
@@ -208,6 +311,7 @@ export function estimateNonTextPartTokens(
208
311
  mimeType: string | undefined,
209
312
  base64Data: string | undefined,
210
313
  provider?: string,
314
+ model?: string,
211
315
  ): number {
212
316
  const isImage = mimeType?.toLowerCase().startsWith('image/') ?? false;
213
317
  if (!isImage) {
@@ -217,5 +321,5 @@ export function estimateNonTextPartTokens(
217
321
  base64Data === undefined
218
322
  ? undefined
219
323
  : parseImageDimensionsFromBase64(base64Data);
220
- return estimateImageTokens({ provider, dimensions });
324
+ return estimateImageTokens({ provider, dimensions, model });
221
325
  }
@@ -24,8 +24,8 @@ export interface MediaBlock {
24
24
  mimeType: string;
25
25
  /** Base64-encoded data or URL. */
26
26
  data?: string;
27
- /** Whether data is a URL or base64. */
28
- encoding?: 'url' | 'base64';
27
+ /** Whether media is inline, a URL, or a local content reference. */
28
+ encoding?: 'url' | 'base64' | 'reference';
29
29
  /** Optional caption or alt text. */
30
30
  caption?: string;
31
31
  /** Original filename if applicable. */