diff --git a/packages/ai/perceptron/src/index.test.ts b/packages/ai/perceptron/src/index.test.ts index f43ad207..a808dcac 100644 --- a/packages/ai/perceptron/src/index.test.ts +++ b/packages/ai/perceptron/src/index.test.ts @@ -1,4 +1,141 @@ import { smokeTest } from '@profullstack/sh1pt-core/testing'; +import { afterEach, describe, expect, it, vi } from 'vitest'; import adapter from './index.js'; smokeTest(adapter, { idPrefix: 'ai' }); + +const ctx = ( + secrets: Record = { PERCEPTRON_API_KEY: 'test-key' }, + dryRun = false +) => ({ + secret: (key: string) => secrets[key], + log: () => {}, + dryRun, +}); + +describe('Perceptron chat completions generation', () => { + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it('short-circuits dry-run before network calls', async () => { + const fetchMock = vi.fn(); + vi.stubGlobal('fetch', fetchMock); + + const result = await adapter.generate( + ctx({ PERCEPTRON_API_KEY: 'test-key' }, true), + 'hello', + {}, + {} + ); + + expect(result).toEqual({ text: '[dry-run]', model: 'perceptron-mk1' }); + expect(fetchMock).not.toHaveBeenCalled(); + }); + + it('posts chat completions requests and maps usage tokens', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ + choices: [{ message: { content: 'hi from perceptron' } }], + model: 'isaac-0.2-2b-preview', + usage: { prompt_tokens: 9, completion_tokens: 4, total_tokens: 13 }, + }), + }); + vi.stubGlobal('fetch', fetchMock); + + const result = await adapter.generate( + ctx(), + 'hello', + { + model: 'isaac-0.2-2b-preview', + system: 'THINK', + maxTokens: 64, + temperature: 0.2, + extra: { + top_p: 0.9, + response_format: { type: 'text' }, + }, + }, + {} + ); + + expect(fetchMock).toHaveBeenCalledOnce(); + const call = fetchMock.mock.calls[0]; + expect(call).toBeDefined(); + const [url, request] = call!; + expect(url).toBe('https://api.perceptron.inc/v1/chat/completions'); + expect(request.headers.authorization).toBe('Bearer test-key'); + expect(request.headers['content-type']).toBe('application/json'); + expect(JSON.parse(request.body)).toEqual({ + model: 'isaac-0.2-2b-preview', + messages: [ + { role: 'system', content: 'THINK' }, + { role: 'user', content: 'hello' }, + ], + stream: false, + max_completion_tokens: 64, + temperature: 0.2, + top_p: 0.9, + response_format: { type: 'text' }, + }); + expect(result).toEqual({ + text: 'hi from perceptron', + model: 'isaac-0.2-2b-preview', + inputTokens: 9, + outputTokens: 4, + }); + }); + + it('uses a configured base URL and Perceptron-specific vision options', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ + choices: [{ message: { content: 'grounded answer' } }], + model: 'perceptron-mk1', + }), + }); + vi.stubGlobal('fetch', fetchMock); + + await adapter.generate( + ctx(), + 'describe the image', + { + extra: { + vision_config: { + enable_thinking: true, + annotation_format: 'box', + }, + }, + }, + { baseUrl: 'https://perceptron.test/v1' } + ); + + expect(fetchMock).toHaveBeenCalledWith( + 'https://perceptron.test/v1/chat/completions', + expect.objectContaining({ + body: JSON.stringify({ + model: 'perceptron-mk1', + messages: [{ role: 'user', content: 'describe the image' }], + stream: false, + vision_config: { + enable_thinking: true, + annotation_format: 'box', + }, + }), + }) + ); + }); + + it('includes status and response body excerpt on errors', async () => { + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ + ok: false, + status: 429, + text: async () => 'rate limited'.repeat(30), + })); + + await expect(adapter.generate(ctx(), 'hello', {}, {})).rejects.toThrow( + /Perceptron 429: rate limited/ + ); + }); +}); diff --git a/packages/ai/perceptron/src/index.ts b/packages/ai/perceptron/src/index.ts index 3208265c..2188cf32 100644 --- a/packages/ai/perceptron/src/index.ts +++ b/packages/ai/perceptron/src/index.ts @@ -4,27 +4,89 @@ interface Config { baseUrl?: string; } +const DEFAULT_BASE = 'https://api.perceptron.inc/v1'; +const DEFAULT_MODEL = 'perceptron-mk1'; + export default defineAi({ id: 'ai-perceptron', label: 'Perceptron', - defaultModel: 'PERCEPTRON_API_KEY', - models: ['PERCEPTRON_API_KEY'], - - async generate(ctx, prompt, _opts, _config) { - const apiKey = ctx.secret('https://perceptron.ai'); - if (!apiKey) throw new Error('https://perceptron.ai not in vault — run `sh1pt promote ai setup`'); - ctx.log(`[stub] ai-perceptron · ${prompt.length} chars in — integration pending`); - return { text: '[stub — ai-perceptron integration not yet implemented]', model: 'PERCEPTRON_API_KEY' }; + defaultModel: DEFAULT_MODEL, + models: [ + DEFAULT_MODEL, + 'isaac-0.2-2b-preview', + 'isaac-0.2-1b', + 'isaac-0.1', + ], + + async generate(ctx, prompt, opts, config) { + const apiKey = ctx.secret('PERCEPTRON_API_KEY'); + if (!apiKey) throw new Error('PERCEPTRON_API_KEY not in vault'); + const model = opts.model ?? DEFAULT_MODEL; + ctx.log(`perceptron · model=${model} · ${prompt.length} chars in`); + if (ctx.dryRun) return { text: '[dry-run]', model }; + + const messages: PerceptronMessage[] = []; + if (opts.system) messages.push({ role: 'system', content: opts.system }); + messages.push({ role: 'user', content: prompt }); + + const res = await fetch(`${config.baseUrl ?? DEFAULT_BASE}/chat/completions`, { + method: 'POST', + headers: { + authorization: `Bearer ${apiKey}`, + 'content-type': 'application/json', + }, + body: JSON.stringify({ + model, + messages, + stream: false, + ...(opts.maxTokens !== undefined ? { max_completion_tokens: opts.maxTokens } : {}), + ...(opts.temperature !== undefined ? { temperature: opts.temperature } : {}), + ...opts.extra, + }), + }); + if (!res.ok) throw new Error(`Perceptron ${res.status}: ${(await res.text()).slice(0, 200)}`); + + const data = await res.json() as PerceptronChatResponse; + return { + text: data.choices[0]?.message?.content ?? '', + model: data.model, + inputTokens: data.usage?.prompt_tokens, + outputTokens: data.usage?.completion_tokens, + }; }, setup: tokenSetup({ - secretKey: 'https://perceptron.ai', + secretKey: 'PERCEPTRON_API_KEY', label: 'Perceptron', - vendorDocUrl: '', + vendorDocUrl: 'https://docs.perceptron.inc/api-reference/endpoint/chat-completions', steps: [ - 'Sign in at and create an API key', - 'Copy the key — usually shown once', + 'Sign in to Perceptron and create an API key', + 'Copy the key - usually shown once', 'Paste below; sh1pt encrypts it in the vault', ], + fields: [ + { key: 'baseUrl', message: 'Perceptron API base URL (optional):' }, + ], }), }); + +type PerceptronRole = 'system' | 'user' | 'assistant'; + +interface PerceptronMessage { + role: PerceptronRole; + content: string; +} + +interface PerceptronChatResponse { + model: string; + choices: Array<{ + message?: { + content?: string; + reasoning_content?: string; + }; + }>; + usage?: { + prompt_tokens?: number; + completion_tokens?: number; + }; +}