@animastor/assistant 0.1.1 → 0.2.0
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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/chat-engine.cjs +82 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0 (2026-11-10, unpublished)
|
|
4
|
+
|
|
5
|
+
- Edit-mode prompt and `edit_book` tool describe behavior.json schema v2:
|
|
6
|
+
entries at `/behaviors/characters/{character_id}`.
|
|
7
|
+
- Deterministic guard inside `applyPatches` (exported as
|
|
8
|
+
`normalizeBehaviorPatches`): on a v2 book, legacy `/behaviors/<id>…` paths
|
|
9
|
+
are rewritten to `/behaviors/characters/<id>…`, and a flat map written to
|
|
10
|
+
`/behaviors` becomes `characters` while `rules`/`schema_version` survive.
|
|
11
|
+
- Character-delete cascade in `applyPatches`: a character removed by the
|
|
12
|
+
patches loses its `behaviors.characters` entry and its `scope:"character"`
|
|
13
|
+
rules (same contract as the editor).
|
|
14
|
+
|
|
3
15
|
## 0.1.1
|
|
4
16
|
|
|
5
17
|
- Fix: `enable_thinking` is sent only to providers that accept it
|
package/README.md
CHANGED
|
@@ -142,7 +142,7 @@ keeps storage, filesystem, provider and transport concerns on the host side.
|
|
|
142
142
|
|
|
143
143
|
## Versioning / release
|
|
144
144
|
|
|
145
|
-
- Current version: `0.
|
|
145
|
+
- Current version: `0.2.0` (see CHANGELOG.md).
|
|
146
146
|
- SemVer: the four public factories + `buildChatRequestExtras`, the root-only export map, the
|
|
147
147
|
`AssistantPorts` shape and the `/api/v1/ai/*` HTTP/SSE contract are the
|
|
148
148
|
public contract — breaking any of them requires a major bump.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@animastor/assistant",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Animastor Assistant — the AI assistant contour of the Animastor backend, physically extracted from the host: chat engine (persona/system prompts, book context, mode tools, JSON patch pipeline with bundle-contract validation), the /api/v1/ai/* HTTP contour (sessions CRUD, non-streaming chat, SSE streaming with meta/delta/done/error frames, tool-call orchestration) and the chat-session repository contract. The package reaches the host (book read/write, bundle validation, provider resolution, session persistence, shared-inference transport, logging) ONLY through the injected AssistantPorts seam — no PG, no Book internals, no filesystem, no provider gateway inside.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"animastor",
|
package/src/chat-engine.cjs
CHANGED
|
@@ -47,21 +47,24 @@ module.exports = function(config, deps = {}) {
|
|
|
47
47
|
'When the user asks to fix participants across the whole book, apply the change to EVERY scene in EVERY chapter — do not stop after one scene. List one patch per affected scene.',
|
|
48
48
|
'',
|
|
49
49
|
'## Character Behavior',
|
|
50
|
-
'Each character can have a Behavior entry in the book data under `behaviors` (keyed by character_id).',
|
|
50
|
+
'Each character can have a Behavior entry in the book data under `behaviors.characters` (keyed by character_id).',
|
|
51
|
+
'`behaviors` is schema v2: `{"schema_version": 2, "rules": [...], "characters": {...}}`. Edit ONLY `behaviors.characters.<character_id>`; never change `schema_version` or `rules` unless the user explicitly asks about behaviour rules.',
|
|
51
52
|
'Behavior describes HOW a character acts — their mannerisms, habits, and reactions.',
|
|
52
53
|
'',
|
|
53
54
|
'Behavior schema:',
|
|
54
55
|
'```json',
|
|
55
56
|
'{',
|
|
56
|
-
' "
|
|
57
|
-
' "
|
|
58
|
-
'
|
|
59
|
-
'
|
|
60
|
-
'
|
|
61
|
-
'
|
|
62
|
-
'
|
|
63
|
-
'
|
|
64
|
-
'
|
|
57
|
+
' "characters": {',
|
|
58
|
+
' "<character_id>": {',
|
|
59
|
+
' "baseline": "How the character normally behaves — characteristic manner of movement, expressions, gestures, posture, speech patterns.",',
|
|
60
|
+
' "quirks": ["Repeated characteristic habit or mannerism."],',
|
|
61
|
+
' "reactions": [',
|
|
62
|
+
' {',
|
|
63
|
+
' "trigger": "A situation or emotional state.",',
|
|
64
|
+
' "reaction": "The observable behavioral response."',
|
|
65
|
+
' }',
|
|
66
|
+
' ]',
|
|
67
|
+
' }',
|
|
65
68
|
' }',
|
|
66
69
|
'}',
|
|
67
70
|
'```',
|
|
@@ -75,7 +78,7 @@ module.exports = function(config, deps = {}) {
|
|
|
75
78
|
'1. Find the character by character_id from `characters` in the book data.',
|
|
76
79
|
'2. Study the character passport (appearance, clothes, description) to understand their personality.',
|
|
77
80
|
'3. Create behavior that matches the character personality — distinct, specific, believable.',
|
|
78
|
-
'4. Use `edit_book` to write to `/behaviors/{character_id}` with the full behavior object.',
|
|
81
|
+
'4. Use `edit_book` to write to `/behaviors/characters/{character_id}` with the full behavior object.',
|
|
79
82
|
'5. Do NOT create a new character. Do NOT modify passport or voice unless asked.',
|
|
80
83
|
'',
|
|
81
84
|
'Example user request: "Придумай поведение для Юры и Светланы."',
|
|
@@ -289,7 +292,7 @@ module.exports = function(config, deps = {}) {
|
|
|
289
292
|
type: 'function',
|
|
290
293
|
function: {
|
|
291
294
|
name: 'edit_book',
|
|
292
|
-
description: 'Apply changes to the current book. Call this when the user asks to edit the book content (title, author, characters, scenes, text, behavior, etc.). Supports writing to /behaviors/{character_id} for character behavior. Do NOT call if the book is locked.',
|
|
295
|
+
description: 'Apply changes to the current book. Call this when the user asks to edit the book content (title, author, characters, scenes, text, behavior, etc.). Supports writing to /behaviors/characters/{character_id} for character behavior. Do NOT call if the book is locked.',
|
|
293
296
|
parameters: {
|
|
294
297
|
type: 'object',
|
|
295
298
|
properties: {
|
|
@@ -300,7 +303,7 @@ module.exports = function(config, deps = {}) {
|
|
|
300
303
|
type: 'object',
|
|
301
304
|
properties: {
|
|
302
305
|
op: { type: 'string', enum: ['replace', 'add', 'remove'], description: 'Operation type' },
|
|
303
|
-
path: { type: 'string', description: 'JSON path like /book/title, /chapters/0/scenes/1/units/0/text, /behaviors/character_id/baseline' },
|
|
306
|
+
path: { type: 'string', description: 'JSON path like /book/title, /chapters/0/scenes/1/units/0/text, /behaviors/characters/character_id/baseline' },
|
|
304
307
|
value: { description: 'New value (for replace/add)' },
|
|
305
308
|
},
|
|
306
309
|
required: ['op', 'path'],
|
|
@@ -395,7 +398,7 @@ module.exports = function(config, deps = {}) {
|
|
|
395
398
|
const result = JSON.parse(JSON.stringify(obj));
|
|
396
399
|
const errors = [];
|
|
397
400
|
|
|
398
|
-
for (const patch of patches) {
|
|
401
|
+
for (const patch of normalizeBehaviorPatches(obj, patches || [])) {
|
|
399
402
|
const { op, path: pathStr, value } = patch;
|
|
400
403
|
const { parent, key, value: oldValue } = resolvePath(result, pathStr);
|
|
401
404
|
|
|
@@ -458,9 +461,35 @@ module.exports = function(config, deps = {}) {
|
|
|
458
461
|
}
|
|
459
462
|
}
|
|
460
463
|
|
|
464
|
+
cascadeDeletedCharacterBehaviors(obj, result);
|
|
461
465
|
return { result, errors };
|
|
462
466
|
}
|
|
463
467
|
|
|
468
|
+
// ── Character-removal cascade for behavior.json v2 ──
|
|
469
|
+
// A character removed by the patches (present in obj.characters, gone in
|
|
470
|
+
// result.characters) loses its behaviors.characters entry and every rule
|
|
471
|
+
// with scope:"character" bound to it — same contract as the editor's
|
|
472
|
+
// character removal route. Global rules stay. Legacy (no schema_version)
|
|
473
|
+
// in-memory maps only lose the entry.
|
|
474
|
+
function cascadeDeletedCharacterBehaviors(before, after) {
|
|
475
|
+
if (!after || !after.behaviors || typeof after.behaviors !== 'object') return;
|
|
476
|
+
const ids = (list) => new Set((Array.isArray(list) ? list : []).map(c => c && c.id).filter(Boolean));
|
|
477
|
+
const was = ids(before && before.characters);
|
|
478
|
+
if (was.size === 0) return;
|
|
479
|
+
const now = ids(after.characters);
|
|
480
|
+
const gone = [...was].filter(id => !now.has(id));
|
|
481
|
+
if (gone.length === 0) return;
|
|
482
|
+
const bh = after.behaviors;
|
|
483
|
+
const isV2 = bh.schema_version != null;
|
|
484
|
+
const chars = isV2 ? bh.characters : bh;
|
|
485
|
+
for (const id of gone) {
|
|
486
|
+
if (chars && typeof chars === 'object') delete chars[id];
|
|
487
|
+
if (isV2 && Array.isArray(bh.rules)) {
|
|
488
|
+
bh.rules = bh.rules.filter(r => !(r && r.scope === 'character' && r.character_id === id));
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
464
493
|
// ── Deterministic scene-participants guard ─────────
|
|
465
494
|
// Models frequently produce `scene.participants` in the wrong shape when
|
|
466
495
|
// editing (e.g. {"item": [...]} — a JSON-Schema `items` hallucination, or
|
|
@@ -525,6 +554,44 @@ module.exports = function(config, deps = {}) {
|
|
|
525
554
|
// state. On violation the result is withheld (null) so no caller can
|
|
526
555
|
// persist broken JSON; errors name the failing file/resource so the
|
|
527
556
|
// assistant (or the user) can issue a corrective patch in the next turn.
|
|
557
|
+
// ── Deterministic behavior.json v2 guard ───────────
|
|
558
|
+
// In memory `behaviors` is schema v2 ({schema_version, rules, characters}).
|
|
559
|
+
// Models (and older prompts) still write the v2.1 paths, so before the
|
|
560
|
+
// patches run:
|
|
561
|
+
// /behaviors/<character_id>[/...] → /behaviors/characters/<character_id>[/...]
|
|
562
|
+
// add|replace /behaviors with a flat map (schema_version !== 2)
|
|
563
|
+
// → the map becomes `characters`,
|
|
564
|
+
// stored rules/schema_version survive.
|
|
565
|
+
// Runs inside applyPatches, so every caller gets it. Reserved top-level
|
|
566
|
+
// keys are never treated as character ids. A legacy
|
|
567
|
+
// in-memory book (no schema_version) is left alone — it is migrated on save.
|
|
568
|
+
const BEHAVIOR_RESERVED_KEYS = ['schema_version', 'rules', 'characters', 'defaults'];
|
|
569
|
+
function normalizeBehaviorPatches(obj, patches) {
|
|
570
|
+
const stored = obj && obj.behaviors;
|
|
571
|
+
const isV2 = stored && typeof stored === 'object' && !Array.isArray(stored) && stored.schema_version != null;
|
|
572
|
+
if (!isV2 || !Array.isArray(patches)) return patches;
|
|
573
|
+
return patches.map((patch) => {
|
|
574
|
+
if (!patch || typeof patch.path !== 'string') return patch;
|
|
575
|
+
const p = patch.path;
|
|
576
|
+
if (p === '/behaviors' && (patch.op === 'add' || patch.op === 'replace')) {
|
|
577
|
+
const v = patch.value;
|
|
578
|
+
if (v && typeof v === 'object' && !Array.isArray(v) && v.schema_version !== 2) {
|
|
579
|
+
const characters = {};
|
|
580
|
+
for (const [k, entry] of Object.entries(v)) {
|
|
581
|
+
if (!BEHAVIOR_RESERVED_KEYS.includes(k)) characters[k] = entry;
|
|
582
|
+
}
|
|
583
|
+
return { ...patch, value: { ...stored, characters } };
|
|
584
|
+
}
|
|
585
|
+
return patch;
|
|
586
|
+
}
|
|
587
|
+
const m = /^\/behaviors\/([^/]+)(\/.*)?$/.exec(p);
|
|
588
|
+
if (!m) return patch;
|
|
589
|
+
const seg = m[1];
|
|
590
|
+
if (BEHAVIOR_RESERVED_KEYS.includes(seg)) return patch;
|
|
591
|
+
return { ...patch, path: `/behaviors/characters/${seg}${m[2] || ''}` };
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
|
|
528
595
|
function applyPatchesValidated(obj, patches) {
|
|
529
596
|
const { result, errors } = applyPatches(obj, patches);
|
|
530
597
|
if (errors.length > 0) {
|
|
@@ -558,6 +625,7 @@ module.exports = function(config, deps = {}) {
|
|
|
558
625
|
resolvePath,
|
|
559
626
|
applyPatches,
|
|
560
627
|
applyPatchesValidated,
|
|
628
|
+
normalizeBehaviorPatches,
|
|
561
629
|
normalizeBookParticipants,
|
|
562
630
|
validateBundleObject,
|
|
563
631
|
toolDefinitions: {
|