TagSpaces is an offline, open source, document manager with tagging support
chore: aded test for the ai functionality
Ilian Sapundshiev1d4f114f9d66
diff --git a/src/renderer/components/chat/OpenAIClient.ts b/src/renderer/components/chat/OpenAIClient.tsindex 85677ee05..f3868d41c 100644--- a/src/renderer/components/chat/OpenAIClient.ts+++ b/src/renderer/components/chat/OpenAIClient.ts@@ -26,6 +26,7 @@* can dispatch to either by engine.*/import { ChatRequest, ModelResponse } from 'ollama';+import { sanitizeJsonSchema } from '-/components/chat/openai-schema';/*** Resolve the OpenAI base URL. The configured URL is treated as the base that@@ -109,47 +110,6 @@ export async function getOpenAIModels(}}-/**- * `zodToJsonSchema` (used by ChatProvider) emits a draft `$schema` declaration- * and references additional `$defs`/`definitions`. Strict structured-output- * validators on OpenAI-compatible servers (LM Studio, llama.cpp) reject the- * `$schema` keyword and the `$ref` indirection with HTTP 400. Strip `$schema`- * everywhere and inline single-definition `$ref`s so the schema is a plain,- * self-contained object.- */-function sanitizeJsonSchema(schema: any): any {- if (!schema || typeof schema !== 'object') {- return schema;- }- const defs = schema.$defs || schema.definitions;- const resolveRef = (node: any): any => {- if (Array.isArray(node)) {- return node.map(resolveRef);- }- if (node && typeof node === 'object') {- if (typeof node.$ref === 'string' && defs) {- const key = node.$ref.replace(/^#\/(?:\$defs|definitions)\//, '');- if (defs[key]) {- return resolveRef(defs[key]);- }- }- return Object.entries(node)- .filter(- ([k]) => k !== '$schema' && k !== '$defs' && k !== 'definitions',- )- .reduce(- (acc, [k, v]) => {- acc[k] = resolveRef(v);- return acc;- },- {} as Record<string, any>,- );- }- return node;- };- return resolveRef(schema);-}-/*** Translate the internal (Ollama-native) ChatRequest into an OpenAI* chat-completions body:diff --git a/src/renderer/components/chat/openai-schema.ts b/src/renderer/components/chat/openai-schema.tsnew file mode 100644index 000000000..58843853c--- /dev/null+++ b/src/renderer/components/chat/openai-schema.ts@@ -0,0 +1,61 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2024-present TagSpaces GmbH+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++/**+ * `zodToJsonSchema` (used by ChatProvider) emits a draft `$schema` declaration+ * and references additional `$defs`/`definitions`. Strict structured-output+ * validators on OpenAI-compatible servers (LM Studio, llama.cpp) reject the+ * `$schema` keyword and the `$ref` indirection with HTTP 400. Strip `$schema`+ * everywhere and inline single-definition `$ref`s so the schema is a plain,+ * self-contained object.+ *+ * Kept dependency-free (own module) so it stays unit-testable.+ */+// eslint-disable-next-line import/prefer-default-export+export function sanitizeJsonSchema(schema: any): any {+ if (!schema || typeof schema !== 'object') {+ return schema;+ }+ const defs = schema.$defs || schema.definitions;+ const resolveRef = (node: any): any => {+ if (Array.isArray(node)) {+ return node.map(resolveRef);+ }+ if (node && typeof node === 'object') {+ if (typeof node.$ref === 'string' && defs) {+ const key = node.$ref.replace(/^#\/(?:\$defs|definitions)\//, '');+ if (defs[key]) {+ return resolveRef(defs[key]);+ }+ }+ return Object.entries(node)+ .filter(+ ([k]) => k !== '$schema' && k !== '$defs' && k !== 'definitions',+ )+ .reduce(+ (acc, [k, v]) => {+ acc[k] = resolveRef(v);+ return acc;+ },+ {} as Record<string, any>,+ );+ }+ return node;+ };+ return resolveRef(schema);+}diff --git a/src/renderer/services/base64.ts b/src/renderer/services/base64.tsnew file mode 100644index 000000000..141b6fc0d--- /dev/null+++ b/src/renderer/services/base64.ts@@ -0,0 +1,50 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2024-present TagSpaces GmbH+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++/**+ * Encode binary image data as a base64 string.+ *+ * Built in fixed-size chunks on purpose: `String.fromCharCode.apply(null, all)`+ * spreads every byte as a separate function argument, which throws+ * `RangeError: Maximum call stack size exceeded` for anything but a tiny image.+ * 32k-byte chunks keep each `apply` call well under the engine's argument+ * limit while still encoding arbitrarily large buffers.+ *+ * Kept dependency-free (own module) so it stays unit-testable without pulling+ * in the heavy utils-io graph.+ */+// eslint-disable-next-line import/prefer-default-export+export function toBase64Image(+ uint8Array: Uint8Array | null | undefined,+): string | undefined {+ if (!uint8Array || uint8Array.length === 0) {+ return undefined;+ }+ try {+ let binary = '';+ const chunkSize = 0x8000; // 32k+ for (let i = 0; i < uint8Array.length; i += chunkSize) {+ const chunk = uint8Array.subarray(i, i + chunkSize);+ binary += String.fromCharCode.apply(null, chunk as any);+ }+ return btoa(binary);+ } catch (e) {+ console.error('toBase64Image error:', e);+ return undefined;+ }+}diff --git a/src/renderer/services/utils-io.ts b/src/renderer/services/utils-io.tsindex 506425ab0..a9207eecc 100644--- a/src/renderer/services/utils-io.ts+++ b/src/renderer/services/utils-io.ts@@ -1447,28 +1447,9 @@ export function toTsLocation(location: CommonLocation): TS.S3Location {return result as TS.S3Location;}-export function toBase64Image(- uint8Array: Uint8Array | null | undefined,-): string | undefined {- if (!uint8Array || uint8Array.length === 0) {- return undefined;- }- try {- // Build the binary string in chunks to avoid "Maximum call stack size- // exceeded" — String.fromCharCode.apply spreads every byte as an argument,- // which overflows the stack for large images.- let binary = '';- const chunkSize = 0x8000; // 32k- for (let i = 0; i < uint8Array.length; i += chunkSize) {- const chunk = uint8Array.subarray(i, i + chunkSize);- binary += String.fromCharCode.apply(null, chunk as any);- }- return btoa(binary);- } catch (e) {- console.error('toBase64Image error:', e);- return undefined;- }-}+// toBase64Image lives in its own dependency-free module so it stays+// unit-testable; re-exported here for existing import sites.+export { toBase64Image } from '-/services/base64';/*** shallow compare Entries array (and optional mtime)diff --git a/tests/e2e/ai-generation.pw.e2e.js b/tests/e2e/ai-generation.pw.e2e.jsindex 49beab1d2..4cbc448ee 100644--- a/tests/e2e/ai-generation.pw.e2e.js+++ b/tests/e2e/ai-generation.pw.e2e.js@@ -214,9 +214,19 @@ test.describe('TST70 - AI generation: mocked OpenAI-compatible [electron,_pro]',// the tags-mode JSON schema surfaced as response_format.json_schema.expect(Array.isArray(capture.lastChatBody?.messages)).toBe(true);expect(capture.lastChatBody?.response_format?.type).toBe('json_schema');- expect(- capture.lastChatBody?.response_format?.json_schema?.schema,- ).toBeTruthy();+ const sentSchema =+ capture.lastChatBody?.response_format?.json_schema?.schema;+ expect(sentSchema).toBeTruthy();++ // The schema must be sanitized before it reaches an OpenAI-compatible+ // server: zodToJsonSchema emits a `$schema` declaration and `$defs`/`$ref`+ // indirection that LM Studio / llama.cpp reject with HTTP 400. Assert no+ // such keyword survives anywhere in the sent schema tree.+ const schemaJson = JSON.stringify(sentSchema);+ expect(schemaJson).not.toContain('$schema');+ expect(schemaJson).not.toContain('$ref');+ expect(schemaJson).not.toContain('$defs');+ expect(schemaJson).not.toContain('definitions');// Wait for the per-file processed check so the tag-apply + rename has// actually landed before we close and read the on-disk state.diff --git a/tests/unit/aiUtils.test.js b/tests/unit/aiUtils.test.jsnew file mode 100644index 000000000..83c056518--- /dev/null+++ b/tests/unit/aiUtils.test.js@@ -0,0 +1,132 @@+/* Copyright (c) 2016-present - TagSpaces GmbH. All rights reserved. */++import { expect, test } from '@playwright/test';+import { toBase64Image } from '-/services/base64';+import { sanitizeJsonSchema } from '-/components/chat/openai-schema';++/* ------------------------------------------------------------------ *+ * toBase64Image — regression for the "Maximum call stack size exceeded"+ * crash when describing an image via Ollama/LM Studio.+ * ------------------------------------------------------------------ */+test.describe('toBase64Image', () => {+ test('returns undefined for empty / nullish input', () => {+ expect(toBase64Image(undefined)).toBeUndefined();+ expect(toBase64Image(null)).toBeUndefined();+ expect(toBase64Image(new Uint8Array(0))).toBeUndefined();+ });++ test('encodes a small buffer to correct base64', () => {+ // bytes for "HI" -> btoa("HI") === "SEk="+ expect(toBase64Image(new Uint8Array([72, 73]))).toBe('SEk=');+ });++ test('round-trips arbitrary bytes via atob', () => {+ const bytes = new Uint8Array([0, 1, 2, 127, 128, 200, 254, 255]);+ const decoded = atob(toBase64Image(bytes));+ expect(decoded.length).toBe(bytes.length);+ for (let i = 0; i < bytes.length; i += 1) {+ expect(decoded.charCodeAt(i)).toBe(bytes[i]);+ }+ });++ test('encodes a large buffer without overflowing the call stack', () => {+ // > 32k chunk size, and far beyond the ~64k-arg apply() limit that the+ // old String.fromCharCode.apply(null, [...all]) hit. A real photo is MBs.+ const size = 2 * 1024 * 1024; // 2 MB+ const bytes = new Uint8Array(size);+ for (let i = 0; i < size; i += 1) {+ bytes[i] = i % 256;+ }+ let result;+ expect(() => {+ result = toBase64Image(bytes);+ }).not.toThrow();+ expect(typeof result).toBe('string');+ // base64 expands 3 bytes -> 4 chars+ expect(result.length).toBe(Math.ceil(size / 3) * 4);+ expect(atob(result).length).toBe(size);+ });+});++/* ------------------------------------------------------------------ *+ * sanitizeJsonSchema — regression for the LM Studio / llama.cpp HTTP 400+ * when sending a zodToJsonSchema-shaped response_format schema.+ * ------------------------------------------------------------------ */+test.describe('sanitizeJsonSchema', () => {+ test('strips the top-level $schema keyword', () => {+ const out = sanitizeJsonSchema({+ $schema: 'http://json-schema.org/draft-07/schema#',+ type: 'object',+ properties: { name: { type: 'string' } },+ required: ['name'],+ additionalProperties: false,+ });+ expect(out.$schema).toBeUndefined();+ expect(out).toEqual({+ type: 'object',+ properties: { name: { type: 'string' } },+ required: ['name'],+ additionalProperties: false,+ });+ });++ test('strips $schema / $defs / definitions at any depth', () => {+ const out = sanitizeJsonSchema({+ $schema: 'x',+ type: 'object',+ properties: {+ nested: {+ $schema: 'y',+ definitions: { foo: { type: 'string' } },+ type: 'object',+ },+ },+ });+ expect(out.$schema).toBeUndefined();+ expect(out.properties.nested.$schema).toBeUndefined();+ expect(out.properties.nested.definitions).toBeUndefined();+ expect(out.properties.nested.type).toBe('object');+ });++ test('inlines a $ref pointing into $defs', () => {+ const out = sanitizeJsonSchema({+ $schema: 'x',+ $defs: { Tag: { type: 'string', maxLength: 30 } },+ type: 'object',+ properties: {+ tags: { type: 'array', items: { $ref: '#/$defs/Tag' } },+ },+ });+ expect(out.$defs).toBeUndefined();+ expect(out.properties.tags.items).toEqual({+ type: 'string',+ maxLength: 30,+ });+ });++ test('inlines a $ref pointing into definitions', () => {+ const out = sanitizeJsonSchema({+ definitions: { Color: { type: 'string' } },+ type: 'object',+ properties: { color: { $ref: '#/definitions/Color' } },+ });+ expect(out.definitions).toBeUndefined();+ expect(out.properties.color).toEqual({ type: 'string' });+ });++ test('leaves a plain self-contained schema unchanged', () => {+ const schema = {+ type: 'object',+ properties: { a: { type: 'number' } },+ required: ['a'],+ additionalProperties: false,+ };+ expect(sanitizeJsonSchema(schema)).toEqual(schema);+ });++ test('passes non-object inputs through untouched', () => {+ expect(sanitizeJsonSchema(undefined)).toBeUndefined();+ expect(sanitizeJsonSchema(null)).toBeNull();+ expect(sanitizeJsonSchema('str')).toBe('str');+ });+});