@lakex-react/ai
v0.2.1
Published
Configurable AI assistant card and selection toolbar for @lakex-react/core.
Readme
@lakex-react/ai
Shared AI service, assistant card, selection actions, and code-block actions for
@lakex-react/core.
npm install @lakex-react/core @lakex-react/aiimport { LakexEditor } from '@lakex-react/core';
import {
aiAssistantCard,
createAIAgent,
createAIService,
createLakexAI,
type LakexAIModelConfig,
} from '@lakex-react/ai';
import '@lakex-react/core/style.css';
import '@lakex-react/ai/style.css';
const models: LakexAIModelConfig[] = [
{ id: 1, name: 'deepseek', capabilities: ['text', 'structured-output'] },
{ id: 2, name: 'seedream', capabilities: ['text-to-image'] },
];
const service = createAIService(async (request) => {
const response = await fetch('/api/ai/run', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
modelId: request.modelId,
prompt: request.prompt,
systemPrompt: request.systemPrompt || request.agent?.content,
operation: request.operation,
context: request.context,
output: request.output,
stream: request.output === 'text' && request.context !== 'drawing-board',
}),
signal: request.signal,
});
if (!response.ok) throw new Error('AI request failed');
return response;
});
const ai = createLakexAI({
service,
models,
defaultModels: { text: 1, code: 1, drawing: 1 },
agents: [
createAIAgent({
name: 'OKR Manager',
content: okrAgentYaml,
modelId: 1,
}, { models, service }),
createAIAgent({
name: 'Image generator',
content: imageAgentYaml,
modelId: 2,
output: 'image',
}, { models, service }),
],
});
<LakexEditor
config={{
customCard: { cards: [aiAssistantCard] },
cardConfigs: { AI: ai },
}}
/>createLakexAI() adapts one AIService.run() to the drawing board, code-block
AI, assistant card, and selection assistant. Ordinary features send a numeric
modelId directly and do not require an Agent.
Agents are optional and appear only in the toolbox. Their content can be a
complete YAML document. Image generation is also an Agent and should declare
output: 'image'. createAIAgent() derives modelName and modelFunc from
the selected model. You may provide your own modelFunc(systemPrompt,
userPrompt); the deprecated func field remains readable for one compatibility
cycle.
Provider credentials should stay on the server. The local example stores model
connections in the gitignored examples/ai-apis.mock.json. /api/ai/run
accepts modelId, resolves the matching endpoint/API key/model, and returns SSE
for text or provider JSON for images. There is no separate server-side Agent
registry.
Code auto-comment replaces the current code block. Code explanation streams a normal paragraph below it. Assistant and selection text results can be inserted as native editor content; image Agent results can be inserted as native image cards.
Lakex JSON Skills
Use the DOM-free subpath when an AI request needs Lakex native JSON output.
Pass features explicitly whenever the caller knows the required nodes:
import { getLakexJsonSkills } from '@lakex-react/ai/json-skills';
const formatSkills = getLakexJsonSkills({
features: ['table', 'image'],
});
const response = await aiService.run({
...request,
systemPrompt: `${request.systemPrompt || ''}\n\n${formatSkills}`,
});If features is omitted, input is used for best-effort Chinese/English
keyword detection. An unrecognized input falls back to all compact v3 chunks:
getLakexJsonSkills({ input: userPrompt });The subpath also exports resolveLakexJsonSkillFeatures(),
detectLakexJsonSkillFeatures(), getLakexJsonSkillChunk(), and
isLakexJsonSkillFeature() for callers that need lower-level control. All
manifests and Markdown chunks live inside packages/ai/src/json-skills and are
inlined during the package build; the published package does not depend on the
repository-level docs or examples directories.
The full docs/lakex-json-format-skills.md file is retained as a developer
reference and should not be sent to AI as one large prompt. The runtime loader
uses the v3 chunk manifest instead.
