@specverse/engines 5.2.0 → 6.0.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/assets/prompts/core/standard/default/behavior.prompt.yaml +37 -30
- package/assets/prompts/core/standard/v9/behavior.prompt.yaml +37 -30
- package/dist/ai/behavior-ai-service.d.ts +35 -28
- package/dist/ai/behavior-ai-service.d.ts.map +1 -1
- package/dist/ai/behavior-ai-service.js +95 -128
- package/dist/ai/behavior-ai-service.js.map +1 -1
- package/dist/ai/index.d.ts +26 -26
- package/dist/ai/index.d.ts.map +1 -1
- package/dist/ai/index.js +40 -29
- package/dist/ai/index.js.map +1 -1
- package/dist/ai/model-resolver.d.ts +13 -0
- package/dist/ai/model-resolver.d.ts.map +1 -0
- package/dist/ai/model-resolver.js +87 -0
- package/dist/ai/model-resolver.js.map +1 -0
- package/dist/ai/providers/claude-cli.d.ts +25 -0
- package/dist/ai/providers/claude-cli.d.ts.map +1 -0
- package/dist/ai/providers/claude-cli.js +185 -0
- package/dist/ai/providers/claude-cli.js.map +1 -0
- package/dist/ai/providers/index.d.ts +8 -5
- package/dist/ai/providers/index.d.ts.map +1 -1
- package/dist/ai/providers/index.js +7 -5
- package/dist/ai/providers/index.js.map +1 -1
- package/dist/ai/providers/stub.d.ts +15 -0
- package/dist/ai/providers/stub.d.ts.map +1 -0
- package/dist/ai/providers/stub.js +64 -0
- package/dist/ai/providers/stub.js.map +1 -0
- package/dist/ai/skill-detection.d.ts +5 -0
- package/dist/ai/skill-detection.d.ts.map +1 -0
- package/dist/ai/skill-detection.js +27 -0
- package/dist/ai/skill-detection.js.map +1 -0
- package/dist/libs/instance-factories/tools/templates/mcp/mcp-server-generator.js +2 -2
- package/libs/instance-factories/tools/templates/mcp/mcp-server-generator.ts +2 -2
- package/package.json +8 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
name: behavior
|
|
2
|
-
version: "9.
|
|
2
|
+
version: "9.2.0"
|
|
3
3
|
description: Generate pure TypeScript function bodies for SpecVerse behavior steps that don't match convention patterns
|
|
4
4
|
author: SpecVerse Team
|
|
5
5
|
tags: [behavior, code-generation, pure-function, typescript, realize]
|
|
@@ -10,58 +10,65 @@ system:
|
|
|
10
10
|
function bodies for behavior steps that couldn't be matched by the realize
|
|
11
11
|
engine's convention patterns.
|
|
12
12
|
|
|
13
|
+
minimalContext: |
|
|
13
14
|
PURE FUNCTION RULES (critical — violating these breaks the architecture):
|
|
14
15
|
1. NO database access (no prisma, no queries)
|
|
15
16
|
2. NO event publishing (no eventBus)
|
|
16
17
|
3. NO external services (no fetch, no email providers, no payment gateways)
|
|
17
18
|
4. NO imports, no require, no fs, no process
|
|
18
|
-
5. All data you need comes in via the `input` object parameter
|
|
19
|
+
5. All data you need comes in via the `input` object parameter (already destructured — you have the fields directly)
|
|
19
20
|
6. Return a plain result value (object, number, string) — the caller uses it
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
OUTPUT RULES:
|
|
23
|
+
1. Output ONLY the function body code (between { and })
|
|
24
|
+
2. First line: short comment summarising what the function does
|
|
25
|
+
3. No markdown fences, no explanation before or after
|
|
26
|
+
4. Throw Error('...') for failure cases with descriptive messages
|
|
27
|
+
5. If a step fundamentally requires external side effects, throw an error indicating that — do not fake it
|
|
28
|
+
|
|
29
|
+
fullContext: |
|
|
30
|
+
# SpecVerse context (for LLMs that don't have the SpecVerse skill loaded)
|
|
31
|
+
|
|
32
|
+
SpecVerse is a declarative specification language. You describe WHAT a
|
|
33
|
+
system does in a .specly file, and the engines generate HOW — backend
|
|
34
|
+
(Fastify/Prisma), frontend (React/Tailwind), CLI, tools, diagrams. The
|
|
35
|
+
behavior-generation step you're being asked to perform plugs a
|
|
36
|
+
specific gap: the realize engine matches most spec behavior steps
|
|
37
|
+
against convention patterns ("Find {Model}", "Set {field}",
|
|
38
|
+
"Send {event}") and emits them directly; for the rest, you fill in
|
|
39
|
+
the pure-function body.
|
|
40
|
+
|
|
41
|
+
Where your function fits:
|
|
24
42
|
|
|
25
|
-
Example flow:
|
|
26
43
|
```
|
|
27
|
-
// Controller (convention code)
|
|
44
|
+
// Controller (convention-generated code — NOT your job)
|
|
28
45
|
const order = await prisma.order.findUniqueOrThrow(...);
|
|
29
46
|
const customer = await prisma.customer.findUniqueOrThrow(...);
|
|
30
47
|
|
|
31
|
-
// Your function
|
|
48
|
+
// Your function — PURE calculation or transformation
|
|
32
49
|
const discount = await applyDiscountBasedOnLoyaltyTier({ order, customer });
|
|
33
50
|
// discount = { amount: 42, rate: 0.10 }
|
|
34
51
|
|
|
35
|
-
// Controller (convention code)
|
|
52
|
+
// Controller (convention-generated code — NOT your job)
|
|
36
53
|
await prisma.order.update({ where: { id }, data: { total: order.total - discount.amount } });
|
|
37
54
|
await eventBus.publish('OrderDiscounted', { orderId, discount });
|
|
38
55
|
```
|
|
39
56
|
|
|
40
|
-
Convention patterns
|
|
57
|
+
Convention patterns ALREADY auto-generated (don't re-implement these):
|
|
41
58
|
- "Find {Model} by {field}", "Create {Model}", "Update {Model}..."
|
|
42
59
|
- "Delete {Model}", "Transition {Model} to {state}"
|
|
43
60
|
- "Set/Increment/Decrement {field}", "Send {event} event"
|
|
44
61
|
|
|
45
|
-
You handle domain-specific
|
|
46
|
-
scoring, categorisation, validation.
|
|
62
|
+
You handle: domain-specific calculations, transformations, rules,
|
|
63
|
+
scoring, categorisation, validation logic. Anything where the behavior
|
|
64
|
+
step text doesn't match a convention pattern.
|
|
47
65
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
4. Return a meaningful result object
|
|
55
|
-
5. Throw Error('...') for failure cases with descriptive messages
|
|
56
|
-
6. NO await prisma.*, NO await eventBus.*, NO fetch, NO external calls
|
|
57
|
-
7. If the step fundamentally requires external side effects, throw an
|
|
58
|
-
error indicating that — do not fake it
|
|
59
|
-
|
|
60
|
-
OUTPUT FORMAT:
|
|
61
|
-
- No markdown fences
|
|
62
|
-
- No explanation before or after
|
|
63
|
-
- Just the function body code
|
|
64
|
-
- First line should be a comment summarizing what the function does
|
|
66
|
+
SpecVerse uses CURVED operations (Create, Update, Retrieve, Validate,
|
|
67
|
+
Evolve, Delete) on controllers. Validate is dry-run; Evolve handles
|
|
68
|
+
lifecycle state transitions. Models declare attributes, relationships,
|
|
69
|
+
and lifecycles. Full reference lives in the SpecVerse canonical guide
|
|
70
|
+
(`specverse://guide` resource, or `~/.claude/skills/specverse/reference/guide.md`
|
|
71
|
+
if the skill is installed).
|
|
65
72
|
|
|
66
73
|
examples:
|
|
67
74
|
- step: "Apply discount based on loyalty tier"
|
|
@@ -107,7 +114,7 @@ user:
|
|
|
107
114
|
|
|
108
115
|
The function MUST return {{returnType}}. If that's `any`, return a
|
|
109
116
|
reasonable result object describing what was computed. If it's a specific
|
|
110
|
-
type (e.g., { cost: number; days: number }), return an object matching
|
|
117
|
+
type (e.g., "{ cost: number; days: number }"), return an object matching
|
|
111
118
|
that shape exactly — the caller depends on those field names.
|
|
112
119
|
|
|
113
120
|
Output ONLY the function body (the code that goes between { and }).
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
name: behavior
|
|
2
|
-
version: "9.
|
|
2
|
+
version: "9.2.0"
|
|
3
3
|
description: Generate pure TypeScript function bodies for SpecVerse behavior steps that don't match convention patterns
|
|
4
4
|
author: SpecVerse Team
|
|
5
5
|
tags: [behavior, code-generation, pure-function, typescript, realize]
|
|
@@ -10,58 +10,65 @@ system:
|
|
|
10
10
|
function bodies for behavior steps that couldn't be matched by the realize
|
|
11
11
|
engine's convention patterns.
|
|
12
12
|
|
|
13
|
+
minimalContext: |
|
|
13
14
|
PURE FUNCTION RULES (critical — violating these breaks the architecture):
|
|
14
15
|
1. NO database access (no prisma, no queries)
|
|
15
16
|
2. NO event publishing (no eventBus)
|
|
16
17
|
3. NO external services (no fetch, no email providers, no payment gateways)
|
|
17
18
|
4. NO imports, no require, no fs, no process
|
|
18
|
-
5. All data you need comes in via the `input` object parameter
|
|
19
|
+
5. All data you need comes in via the `input` object parameter (already destructured — you have the fields directly)
|
|
19
20
|
6. Return a plain result value (object, number, string) — the caller uses it
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
OUTPUT RULES:
|
|
23
|
+
1. Output ONLY the function body code (between { and })
|
|
24
|
+
2. First line: short comment summarising what the function does
|
|
25
|
+
3. No markdown fences, no explanation before or after
|
|
26
|
+
4. Throw Error('...') for failure cases with descriptive messages
|
|
27
|
+
5. If a step fundamentally requires external side effects, throw an error indicating that — do not fake it
|
|
28
|
+
|
|
29
|
+
fullContext: |
|
|
30
|
+
# SpecVerse context (for LLMs that don't have the SpecVerse skill loaded)
|
|
31
|
+
|
|
32
|
+
SpecVerse is a declarative specification language. You describe WHAT a
|
|
33
|
+
system does in a .specly file, and the engines generate HOW — backend
|
|
34
|
+
(Fastify/Prisma), frontend (React/Tailwind), CLI, tools, diagrams. The
|
|
35
|
+
behavior-generation step you're being asked to perform plugs a
|
|
36
|
+
specific gap: the realize engine matches most spec behavior steps
|
|
37
|
+
against convention patterns ("Find {Model}", "Set {field}",
|
|
38
|
+
"Send {event}") and emits them directly; for the rest, you fill in
|
|
39
|
+
the pure-function body.
|
|
40
|
+
|
|
41
|
+
Where your function fits:
|
|
24
42
|
|
|
25
|
-
Example flow:
|
|
26
43
|
```
|
|
27
|
-
// Controller (convention code)
|
|
44
|
+
// Controller (convention-generated code — NOT your job)
|
|
28
45
|
const order = await prisma.order.findUniqueOrThrow(...);
|
|
29
46
|
const customer = await prisma.customer.findUniqueOrThrow(...);
|
|
30
47
|
|
|
31
|
-
// Your function
|
|
48
|
+
// Your function — PURE calculation or transformation
|
|
32
49
|
const discount = await applyDiscountBasedOnLoyaltyTier({ order, customer });
|
|
33
50
|
// discount = { amount: 42, rate: 0.10 }
|
|
34
51
|
|
|
35
|
-
// Controller (convention code)
|
|
52
|
+
// Controller (convention-generated code — NOT your job)
|
|
36
53
|
await prisma.order.update({ where: { id }, data: { total: order.total - discount.amount } });
|
|
37
54
|
await eventBus.publish('OrderDiscounted', { orderId, discount });
|
|
38
55
|
```
|
|
39
56
|
|
|
40
|
-
Convention patterns
|
|
57
|
+
Convention patterns ALREADY auto-generated (don't re-implement these):
|
|
41
58
|
- "Find {Model} by {field}", "Create {Model}", "Update {Model}..."
|
|
42
59
|
- "Delete {Model}", "Transition {Model} to {state}"
|
|
43
60
|
- "Set/Increment/Decrement {field}", "Send {event} event"
|
|
44
61
|
|
|
45
|
-
You handle domain-specific
|
|
46
|
-
scoring, categorisation, validation.
|
|
62
|
+
You handle: domain-specific calculations, transformations, rules,
|
|
63
|
+
scoring, categorisation, validation logic. Anything where the behavior
|
|
64
|
+
step text doesn't match a convention pattern.
|
|
47
65
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
4. Return a meaningful result object
|
|
55
|
-
5. Throw Error('...') for failure cases with descriptive messages
|
|
56
|
-
6. NO await prisma.*, NO await eventBus.*, NO fetch, NO external calls
|
|
57
|
-
7. If the step fundamentally requires external side effects, throw an
|
|
58
|
-
error indicating that — do not fake it
|
|
59
|
-
|
|
60
|
-
OUTPUT FORMAT:
|
|
61
|
-
- No markdown fences
|
|
62
|
-
- No explanation before or after
|
|
63
|
-
- Just the function body code
|
|
64
|
-
- First line should be a comment summarizing what the function does
|
|
66
|
+
SpecVerse uses CURVED operations (Create, Update, Retrieve, Validate,
|
|
67
|
+
Evolve, Delete) on controllers. Validate is dry-run; Evolve handles
|
|
68
|
+
lifecycle state transitions. Models declare attributes, relationships,
|
|
69
|
+
and lifecycles. Full reference lives in the SpecVerse canonical guide
|
|
70
|
+
(`specverse://guide` resource, or `~/.claude/skills/specverse/reference/guide.md`
|
|
71
|
+
if the skill is installed).
|
|
65
72
|
|
|
66
73
|
examples:
|
|
67
74
|
- step: "Apply discount based on loyalty tier"
|
|
@@ -107,7 +114,7 @@ user:
|
|
|
107
114
|
|
|
108
115
|
The function MUST return {{returnType}}. If that's `any`, return a
|
|
109
116
|
reasonable result object describing what was computed. If it's a specific
|
|
110
|
-
type (e.g., { cost: number; days: number }), return an object matching
|
|
117
|
+
type (e.g., "{ cost: number; days: number }"), return an object matching
|
|
111
118
|
that shape exactly — the caller depends on those field names.
|
|
112
119
|
|
|
113
120
|
Output ONLY the function body (the code that goes between { and }).
|
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Behavior AI Service —
|
|
3
|
-
*
|
|
2
|
+
* Behavior AI Service — generates pure TypeScript function bodies for
|
|
3
|
+
* behavior steps that weren't matched by the realize engine's convention
|
|
4
|
+
* patterns.
|
|
4
5
|
*
|
|
5
|
-
* Architecture (
|
|
6
|
-
* -
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Architecture (post AI-SDK replatform, 2026-04):
|
|
7
|
+
* - Delegates to the Vercel AI SDK via the model resolver. Provider choice
|
|
8
|
+
* is driven by SPECVERSE_AI_PROVIDER (claude-cli | anthropic |
|
|
9
|
+
* openai-compatible | stub) with sensible defaults per environment.
|
|
10
|
+
* - Claude-CLI provider preserves the Max-subscription zero-cost path
|
|
11
|
+
* (spawn claude --print --session-id/--resume). API provider uses
|
|
12
|
+
* Anthropic prompt caching for equivalent savings.
|
|
13
|
+
* - When claude-cli is active AND the SpecVerse skill is installed,
|
|
14
|
+
* Claude Code auto-loads the skill — we skip the fullContext block
|
|
15
|
+
* (saves ~2K tokens per session init).
|
|
16
|
+
* - For providers that don't have skill auto-load (anthropic, openai-
|
|
17
|
+
* compatible), we include the fullContext block in the system prompt
|
|
18
|
+
* so the model has the same grounding.
|
|
11
19
|
*
|
|
12
20
|
* Used by ai-behaviors-generator at realize time.
|
|
13
21
|
*/
|
|
14
22
|
export interface BehaviorAIServiceOptions {
|
|
15
|
-
|
|
23
|
+
/** Model override. Passed through to the resolver; interpretation is provider-specific. */
|
|
16
24
|
model?: string;
|
|
25
|
+
/** Per-call timeout (ms). Default 120_000. Only honoured by the claude-cli provider. */
|
|
17
26
|
timeout?: number;
|
|
18
27
|
}
|
|
19
28
|
export interface BehaviorGenerateContext {
|
|
@@ -28,36 +37,34 @@ export interface BehaviorGenerateContext {
|
|
|
28
37
|
returnType?: string;
|
|
29
38
|
}
|
|
30
39
|
/**
|
|
31
|
-
*
|
|
40
|
+
* Service class kept for API compatibility with ai-behaviors-generator.
|
|
41
|
+
* Internally delegates to the AI SDK via the model resolver.
|
|
32
42
|
*/
|
|
33
43
|
export declare class BehaviorAIService {
|
|
34
|
-
private claudePath;
|
|
35
44
|
private model;
|
|
45
|
+
private providerId;
|
|
46
|
+
private prompt;
|
|
47
|
+
private system;
|
|
36
48
|
private timeout;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
private currentProcess;
|
|
40
|
-
private systemPrompt;
|
|
41
|
-
private userTemplate;
|
|
49
|
+
/** Non-null after generateBehavior() succeeds once. */
|
|
50
|
+
get isAvailable(): boolean;
|
|
42
51
|
constructor(options?: BehaviorAIServiceOptions);
|
|
43
|
-
private detectClaudePath;
|
|
44
52
|
/**
|
|
45
|
-
*
|
|
53
|
+
* Build the system prompt. When the active provider can auto-load the
|
|
54
|
+
* SpecVerse skill (claude-cli + installed skill), we emit role +
|
|
55
|
+
* minimalContext only. Otherwise we include fullContext for grounding.
|
|
46
56
|
*/
|
|
47
|
-
|
|
57
|
+
private assembleSystemPrompt;
|
|
48
58
|
/**
|
|
49
|
-
* Start a new session for a controller.
|
|
50
|
-
*
|
|
59
|
+
* Start a new session for a controller. Session semantics are now owned
|
|
60
|
+
* by the provider — for claude-cli that's the spawn-with-session-id
|
|
61
|
+
* mechanism; for anthropic API it's the prompt-cache marker. We just
|
|
62
|
+
* record a no-op for callers that expect a session id to log.
|
|
51
63
|
*/
|
|
52
64
|
startSession(controllerName: string): string;
|
|
53
|
-
/**
|
|
54
|
-
* Generate a function body for a behavior step.
|
|
55
|
-
* First call creates the session with system prompt; subsequent calls resume.
|
|
56
|
-
*/
|
|
65
|
+
/** Generate a function body for a behavior step. */
|
|
57
66
|
generateBehavior(ctx: BehaviorGenerateContext): Promise<string | null>;
|
|
58
|
-
/**
|
|
59
|
-
* End the current session.
|
|
60
|
-
*/
|
|
67
|
+
/** End the current session — no-op now that session state lives in the provider. */
|
|
61
68
|
endSession(): void;
|
|
62
69
|
private buildUserPrompt;
|
|
63
70
|
private extractCode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"behavior-ai-service.d.ts","sourceRoot":"","sources":["../../src/ai/behavior-ai-service.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"behavior-ai-service.d.ts","sourceRoot":"","sources":["../../src/ai/behavior-ai-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAUH,MAAM,WAAW,wBAAwB;IACvC,2FAA2F;IAC3F,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wFAAwF;IACxF,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,8DAA8D;IAC9D,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAwCD;;;GAGG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,KAAK,CAAC;IACd,OAAO,CAAC,UAAU,CAAC;IACnB,OAAO,CAAC,MAAM,CAAC;IACf,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,OAAO,CAAS;IAExB,uDAAuD;IACvD,IAAI,WAAW,IAAI,OAAO,CAEzB;gBAEW,OAAO,GAAE,wBAA6B;IAQlD;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAY5B;;;;;OAKG;IACH,YAAY,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM;IAI5C,oDAAoD;IAC9C,gBAAgB,CAAC,GAAG,EAAE,uBAAuB,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IA0B5E,oFAAoF;IACpF,UAAU,IAAI,IAAI;IAIlB,OAAO,CAAC,eAAe;IAqBvB,OAAO,CAAC,WAAW;CAQpB"}
|
|
@@ -1,184 +1,143 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Behavior AI Service —
|
|
3
|
-
*
|
|
2
|
+
* Behavior AI Service — generates pure TypeScript function bodies for
|
|
3
|
+
* behavior steps that weren't matched by the realize engine's convention
|
|
4
|
+
* patterns.
|
|
4
5
|
*
|
|
5
|
-
* Architecture (
|
|
6
|
-
* -
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Architecture (post AI-SDK replatform, 2026-04):
|
|
7
|
+
* - Delegates to the Vercel AI SDK via the model resolver. Provider choice
|
|
8
|
+
* is driven by SPECVERSE_AI_PROVIDER (claude-cli | anthropic |
|
|
9
|
+
* openai-compatible | stub) with sensible defaults per environment.
|
|
10
|
+
* - Claude-CLI provider preserves the Max-subscription zero-cost path
|
|
11
|
+
* (spawn claude --print --session-id/--resume). API provider uses
|
|
12
|
+
* Anthropic prompt caching for equivalent savings.
|
|
13
|
+
* - When claude-cli is active AND the SpecVerse skill is installed,
|
|
14
|
+
* Claude Code auto-loads the skill — we skip the fullContext block
|
|
15
|
+
* (saves ~2K tokens per session init).
|
|
16
|
+
* - For providers that don't have skill auto-load (anthropic, openai-
|
|
17
|
+
* compatible), we include the fullContext block in the system prompt
|
|
18
|
+
* so the model has the same grounding.
|
|
11
19
|
*
|
|
12
20
|
* Used by ai-behaviors-generator at realize time.
|
|
13
21
|
*/
|
|
14
|
-
import {
|
|
15
|
-
import
|
|
22
|
+
import { generateText } from 'ai';
|
|
23
|
+
import yaml from 'js-yaml';
|
|
16
24
|
import { existsSync, readFileSync } from 'fs';
|
|
17
|
-
import {
|
|
25
|
+
import { dirname, join } from 'path';
|
|
18
26
|
import { fileURLToPath } from 'url';
|
|
19
|
-
import
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*/
|
|
27
|
+
import { resolveModel, resolveProviderId } from './model-resolver.js';
|
|
28
|
+
import { detectInstalledSkill } from './skill-detection.js';
|
|
29
|
+
/** Load the structured behavior prompt from engines/assets/prompts/. */
|
|
23
30
|
function loadBehaviorPrompt() {
|
|
24
31
|
try {
|
|
25
|
-
// Find the engines package root by walking up from this file
|
|
26
32
|
const __filename = fileURLToPath(import.meta.url);
|
|
27
33
|
const __dirname = dirname(__filename);
|
|
28
|
-
// dist/ai/
|
|
29
|
-
// Walk up to find assets/prompts
|
|
34
|
+
// Walk two or three levels up from dist/ai/ or src/ai/ to find assets/prompts
|
|
30
35
|
const candidates = [
|
|
31
36
|
join(__dirname, '..', '..', 'assets', 'prompts', 'core', 'standard', 'v9', 'behavior.prompt.yaml'),
|
|
32
37
|
join(__dirname, '..', '..', '..', 'assets', 'prompts', 'core', 'standard', 'v9', 'behavior.prompt.yaml'),
|
|
33
38
|
];
|
|
34
39
|
for (const path of candidates) {
|
|
35
40
|
if (existsSync(path)) {
|
|
36
|
-
const
|
|
37
|
-
const role = content?.system?.role || '';
|
|
38
|
-
const context = content?.system?.context || '';
|
|
39
|
-
const userTemplate = content?.user?.template || '';
|
|
41
|
+
const doc = yaml.load(readFileSync(path, 'utf8'));
|
|
40
42
|
return {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
role: (doc?.system?.role ?? '').trim(),
|
|
44
|
+
minimalContext: (doc?.system?.minimalContext ?? '').trim(),
|
|
45
|
+
fullContext: (doc?.system?.fullContext ?? '').trim(),
|
|
46
|
+
userTemplate: (doc?.user?.template ?? '').trim(),
|
|
43
47
|
};
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
}
|
|
47
51
|
catch {
|
|
48
|
-
|
|
52
|
+
/* fall through to null — caller provides sensible defaults */
|
|
49
53
|
}
|
|
50
54
|
return null;
|
|
51
55
|
}
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
Use the global \`prisma\` client. Output only the function body, no fences, no explanation.`;
|
|
55
|
-
const DEFAULT_USER_TEMPLATE = `Generate a TypeScript function body for: "{{step}}"
|
|
56
|
-
Context: {{modelName}}.{{operationName}}, parameters: {{parameterNames}}
|
|
57
|
-
Output ONLY the function body code.`;
|
|
56
|
+
const DEFAULT_ROLE = 'You are a SpecVerse behavior code generator. Produce pure TypeScript function bodies.';
|
|
57
|
+
const DEFAULT_MINIMAL_CONTEXT = 'Output ONLY the function body. No imports, no database access, no event publishing. Return a plain value. Throw Error("...") on failure.';
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
59
|
+
* Service class kept for API compatibility with ai-behaviors-generator.
|
|
60
|
+
* Internally delegates to the AI SDK via the model resolver.
|
|
60
61
|
*/
|
|
61
62
|
export class BehaviorAIService {
|
|
62
|
-
claudePath;
|
|
63
63
|
model;
|
|
64
|
+
providerId;
|
|
65
|
+
prompt;
|
|
66
|
+
system;
|
|
64
67
|
timeout;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
systemPrompt;
|
|
69
|
-
userTemplate;
|
|
70
|
-
constructor(options = {}) {
|
|
71
|
-
this.claudePath = options.claudePath || this.detectClaudePath();
|
|
72
|
-
this.model = options.model || null;
|
|
73
|
-
this.timeout = options.timeout || 60000;
|
|
74
|
-
const prompt = loadBehaviorPrompt();
|
|
75
|
-
this.systemPrompt = prompt?.system || DEFAULT_SYSTEM_PROMPT;
|
|
76
|
-
this.userTemplate = prompt?.userTemplate || DEFAULT_USER_TEMPLATE;
|
|
68
|
+
/** Non-null after generateBehavior() succeeds once. */
|
|
69
|
+
get isAvailable() {
|
|
70
|
+
return this.providerId !== 'stub';
|
|
77
71
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
for (const candidate of candidates) {
|
|
85
|
-
if (candidate === 'claude')
|
|
86
|
-
return candidate;
|
|
87
|
-
if (existsSync(candidate))
|
|
88
|
-
return candidate;
|
|
89
|
-
}
|
|
90
|
-
return 'claude';
|
|
72
|
+
constructor(options = {}) {
|
|
73
|
+
this.model = resolveModel({ model: options.model });
|
|
74
|
+
this.providerId = resolveProviderId();
|
|
75
|
+
this.prompt = loadBehaviorPrompt();
|
|
76
|
+
this.timeout = options.timeout ?? 120_000;
|
|
77
|
+
this.system = this.assembleSystemPrompt();
|
|
91
78
|
}
|
|
92
79
|
/**
|
|
93
|
-
*
|
|
80
|
+
* Build the system prompt. When the active provider can auto-load the
|
|
81
|
+
* SpecVerse skill (claude-cli + installed skill), we emit role +
|
|
82
|
+
* minimalContext only. Otherwise we include fullContext for grounding.
|
|
94
83
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return
|
|
84
|
+
assembleSystemPrompt() {
|
|
85
|
+
const role = this.prompt?.role || DEFAULT_ROLE;
|
|
86
|
+
const minimal = this.prompt?.minimalContext || DEFAULT_MINIMAL_CONTEXT;
|
|
87
|
+
const full = this.prompt?.fullContext || '';
|
|
88
|
+
const skillAutoLoaded = this.providerId === 'claude-cli' && detectInstalledSkill().found;
|
|
89
|
+
if (skillAutoLoaded) {
|
|
90
|
+
return `${role}\n\n${minimal}`;
|
|
102
91
|
}
|
|
92
|
+
return full ? `${role}\n\n${minimal}\n\n${full}` : `${role}\n\n${minimal}`;
|
|
103
93
|
}
|
|
104
94
|
/**
|
|
105
|
-
* Start a new session for a controller.
|
|
106
|
-
*
|
|
95
|
+
* Start a new session for a controller. Session semantics are now owned
|
|
96
|
+
* by the provider — for claude-cli that's the spawn-with-session-id
|
|
97
|
+
* mechanism; for anthropic API it's the prompt-cache marker. We just
|
|
98
|
+
* record a no-op for callers that expect a session id to log.
|
|
107
99
|
*/
|
|
108
100
|
startSession(controllerName) {
|
|
109
|
-
|
|
110
|
-
this.initialized = false;
|
|
111
|
-
return this.sessionId;
|
|
101
|
+
return `${controllerName}-${Date.now()}`;
|
|
112
102
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Generate a function body for a behavior step.
|
|
115
|
-
* First call creates the session with system prompt; subsequent calls resume.
|
|
116
|
-
*/
|
|
103
|
+
/** Generate a function body for a behavior step. */
|
|
117
104
|
async generateBehavior(ctx) {
|
|
118
|
-
if (!this.sessionId) {
|
|
119
|
-
this.startSession(ctx.modelName);
|
|
120
|
-
}
|
|
121
105
|
if (!this.isAvailable)
|
|
122
106
|
return null;
|
|
123
107
|
const userPrompt = this.buildUserPrompt(ctx);
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
108
|
+
try {
|
|
109
|
+
const result = await generateText({
|
|
110
|
+
model: this.model,
|
|
111
|
+
system: this.system,
|
|
112
|
+
prompt: userPrompt,
|
|
113
|
+
abortSignal: AbortSignal.timeout(this.timeout),
|
|
114
|
+
// Anthropic prompt caching: mark the system message as cacheable
|
|
115
|
+
// so subsequent calls within a cache window get the 90% discount.
|
|
116
|
+
// Other providers ignore this.
|
|
117
|
+
providerOptions: {
|
|
118
|
+
anthropic: {
|
|
119
|
+
cacheControl: { type: 'ephemeral' },
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
return this.extractCode(result.text);
|
|
128
124
|
}
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
catch {
|
|
126
|
+
return null;
|
|
131
127
|
}
|
|
132
|
-
if (this.model)
|
|
133
|
-
args.push('--model', this.model);
|
|
134
|
-
args.push('-p', userPrompt);
|
|
135
|
-
return new Promise((resolve) => {
|
|
136
|
-
const proc = spawn(this.claudePath, args, {
|
|
137
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
138
|
-
env: { ...process.env },
|
|
139
|
-
});
|
|
140
|
-
this.currentProcess = proc;
|
|
141
|
-
let output = '';
|
|
142
|
-
const timer = setTimeout(() => {
|
|
143
|
-
proc.kill();
|
|
144
|
-
resolve(null);
|
|
145
|
-
}, this.timeout);
|
|
146
|
-
proc.stdout?.on('data', (data) => { output += data.toString(); });
|
|
147
|
-
proc.on('close', (code) => {
|
|
148
|
-
clearTimeout(timer);
|
|
149
|
-
this.currentProcess = null;
|
|
150
|
-
if (code !== 0) {
|
|
151
|
-
resolve(null);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
this.initialized = true;
|
|
155
|
-
resolve(this.extractCode(output));
|
|
156
|
-
});
|
|
157
|
-
proc.on('error', () => {
|
|
158
|
-
clearTimeout(timer);
|
|
159
|
-
this.currentProcess = null;
|
|
160
|
-
resolve(null);
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
128
|
}
|
|
164
|
-
/**
|
|
165
|
-
* End the current session.
|
|
166
|
-
*/
|
|
129
|
+
/** End the current session — no-op now that session state lives in the provider. */
|
|
167
130
|
endSession() {
|
|
168
|
-
|
|
169
|
-
this.currentProcess.kill();
|
|
170
|
-
this.currentProcess = null;
|
|
171
|
-
}
|
|
172
|
-
this.sessionId = null;
|
|
173
|
-
this.initialized = false;
|
|
131
|
+
/* nothing to do — provider manages its own lifecycle */
|
|
174
132
|
}
|
|
175
133
|
buildUserPrompt(ctx) {
|
|
134
|
+
const template = this.prompt?.userTemplate || DEFAULT_USER_TEMPLATE;
|
|
176
135
|
const inputVars = ctx.parameterNames.length > 0 ? ctx.parameterNames.join(', ') : 'none';
|
|
177
136
|
const inputSignature = ctx.parameterNames.length > 0
|
|
178
|
-
? `{ ${ctx.parameterNames.map(n => `${n}: any`).join('; ')} }`
|
|
137
|
+
? `{ ${ctx.parameterNames.map((n) => `${n}: any`).join('; ')} }`
|
|
179
138
|
: 'Record<string, never>';
|
|
180
139
|
const returnType = ctx.returnType || 'any';
|
|
181
|
-
return
|
|
140
|
+
return template
|
|
182
141
|
.replace(/\{\{step\}\}/g, ctx.step)
|
|
183
142
|
.replace(/\{\{modelName\}\}/g, ctx.modelName)
|
|
184
143
|
.replace(/\{\{operationName\}\}/g, ctx.operationName)
|
|
@@ -191,15 +150,23 @@ export class BehaviorAIService {
|
|
|
191
150
|
}
|
|
192
151
|
extractCode(output) {
|
|
193
152
|
let code = output.trim();
|
|
194
|
-
// Strip markdown fences
|
|
195
153
|
const codeBlock = code.match(/```(?:typescript|ts|javascript|js)?\n([\s\S]*?)```/);
|
|
196
154
|
if (codeBlock)
|
|
197
155
|
code = codeBlock[1].trim();
|
|
198
|
-
// Strip "function ... { ... }" wrapper if model included it
|
|
199
156
|
const funcMatch = code.match(/^(?:export\s+)?(?:async\s+)?function\s+\w+\([^)]*\)[^{]*\{([\s\S]*)\}\s*$/);
|
|
200
157
|
if (funcMatch)
|
|
201
158
|
code = funcMatch[1].trim();
|
|
202
159
|
return code || null;
|
|
203
160
|
}
|
|
204
161
|
}
|
|
162
|
+
const DEFAULT_USER_TEMPLATE = `Generate a PURE TypeScript function body for this SpecVerse behavior step.
|
|
163
|
+
|
|
164
|
+
Step: "{{step}}"
|
|
165
|
+
|
|
166
|
+
Function: async function {{functionName}}(input: {{inputSignature}}): Promise<{{returnType}}>
|
|
167
|
+
|
|
168
|
+
Available: {{inputVars}}
|
|
169
|
+
Available Prisma models (types only): {{availableModels}}
|
|
170
|
+
|
|
171
|
+
Output ONLY the function body (between { and }). No markdown fences.`;
|
|
205
172
|
//# sourceMappingURL=behavior-ai-service.js.map
|