From b0954f8bbeda2b2dcf07288af30e8bb3eb1e09cd Mon Sep 17 00:00:00 2001 From: op-simoneromeo <284916543+op-simoneromeo@users.noreply.github.com> Date: Thu, 21 May 2026 00:30:31 +0800 Subject: [PATCH] Implement AionLabs AI adapter --- packages/ai/aionlabs/src/index.test.ts | 91 ++++++++++++++++++++++++++ packages/ai/aionlabs/src/index.ts | 67 +++++++++++++++++-- 2 files changed, 151 insertions(+), 7 deletions(-) diff --git a/packages/ai/aionlabs/src/index.test.ts b/packages/ai/aionlabs/src/index.test.ts index f43ad207..05f42b0c 100644 --- a/packages/ai/aionlabs/src/index.test.ts +++ b/packages/ai/aionlabs/src/index.test.ts @@ -1,4 +1,95 @@ 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 = { AIONLABS_API_KEY: 'test-key' }, + dryRun = false, +) => ({ + secret: (key: string) => secrets[key], + log: () => {}, + dryRun, +}); + +describe('AionLabs OpenAI-compatible 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({ AIONLABS_API_KEY: 'test-key' }, true), + 'hello', + {}, + {}, + ); + + expect(result).toEqual({ text: '[dry-run]', model: 'aion-1.0-mini' }); + 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 aion', reasoning: 'brief' } }], + model: 'aion-labs/aion-2.0', + usage: { prompt_tokens: 11, completion_tokens: 7 }, + }), + }); + vi.stubGlobal('fetch', fetchMock); + + const result = await adapter.generate( + ctx(), + 'hello', + { + model: 'aion-labs/aion-2.0', + system: 'be direct', + maxTokens: 64, + temperature: 0.2, + extra: { reasoning_split: false }, + }, + {}, + ); + + expect(fetchMock).toHaveBeenCalledOnce(); + const call = fetchMock.mock.calls[0]; + expect(call).toBeDefined(); + const [url, request] = call!; + expect(url).toBe('https://api.aionlabs.ai/v1/chat/completions'); + expect(request.headers.authorization).toBe('Bearer test-key'); + expect(JSON.parse(request.body)).toEqual({ + model: 'aion-labs/aion-2.0', + messages: [ + { role: 'system', content: 'be direct' }, + { role: 'user', content: 'hello' }, + ], + max_tokens: 64, + temperature: 0.2, + reasoning_split: false, + }); + expect(result).toEqual({ + text: 'hi from aion', + model: 'aion-labs/aion-2.0', + inputTokens: 11, + outputTokens: 7, + }); + }); + + it('includes status and response body excerpt on errors', async () => { + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ + ok: false, + status: 502, + text: async () => 'provider error'.repeat(30), + })); + + await expect(adapter.generate(ctx(), 'hello', {}, {})).rejects.toThrow( + /AionLabs 502: provider error/, + ); + }); +}); diff --git a/packages/ai/aionlabs/src/index.ts b/packages/ai/aionlabs/src/index.ts index 2ffdaf6c..348dc1be 100644 --- a/packages/ai/aionlabs/src/index.ts +++ b/packages/ai/aionlabs/src/index.ts @@ -4,23 +4,55 @@ interface Config { baseUrl?: string; } +const DEFAULT_BASE = 'https://api.aionlabs.ai/v1'; +const DEFAULT_MODEL = 'aion-1.0-mini'; + export default defineAi({ id: 'ai-aionlabs', label: 'AionLabs', - defaultModel: 'aion-1.0', - models: ['aion-1.0'], + defaultModel: DEFAULT_MODEL, + models: [DEFAULT_MODEL, 'aion-labs/aion-2.0'], - async generate(ctx, prompt, _opts, _config) { + async generate(ctx, prompt, opts, config) { const apiKey = ctx.secret('AIONLABS_API_KEY'); - if (!apiKey) throw new Error('AIONLABS_API_KEY not in vault — run `sh1pt promote ai setup`'); - ctx.log(`[stub] ai-aionlabs · ${prompt.length} chars in — integration pending`); - return { text: '[stub — ai-aionlabs integration not yet implemented]', model: 'aion-1.0' }; + if (!apiKey) throw new Error('AIONLABS_API_KEY not in vault'); + const model = opts.model ?? DEFAULT_MODEL; + ctx.log(`aionlabs · model=${model} · ${prompt.length} chars in`); + if (ctx.dryRun) return { text: '[dry-run]', model }; + + const messages: AionLabsMessage[] = []; + 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, + ...(opts.maxTokens !== undefined ? { max_tokens: opts.maxTokens } : {}), + ...(opts.temperature !== undefined ? { temperature: opts.temperature } : {}), + ...opts.extra, + }), + }); + if (!res.ok) throw new Error(`AionLabs ${res.status}: ${(await res.text()).slice(0, 200)}`); + + const data = await res.json() as AionLabsChatResponse; + return { + text: data.choices[0]?.message?.content ?? '', + model: data.model, + inputTokens: data.usage?.prompt_tokens, + outputTokens: data.usage?.completion_tokens, + }; }, setup: tokenSetup({ secretKey: 'AIONLABS_API_KEY', label: 'AionLabs', - vendorDocUrl: 'https://www.aionlabs.ai', + vendorDocUrl: 'https://www.aionlabs.ai/docs/quickstart/', steps: [ 'Sign in at https://www.aionlabs.ai and create an API key', 'Copy the key — usually shown once', @@ -28,3 +60,24 @@ export default defineAi({ ], }), }); + +type AionLabsRole = 'system' | 'user' | 'assistant' | 'tool'; + +interface AionLabsMessage { + role: AionLabsRole; + content: string; +} + +interface AionLabsChatResponse { + model: string; + choices: Array<{ + message?: { + content?: string; + reasoning?: string; + }; + }>; + usage?: { + prompt_tokens?: number; + completion_tokens?: number; + }; +}