-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.go
More file actions
405 lines (343 loc) · 12.2 KB
/
prompt.go
File metadata and controls
405 lines (343 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package ai
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// SystemPrompt returns the system prompt describing the workflow engine capabilities.
func SystemPrompt() string {
return `You are an expert workflow engine architect. You generate YAML workflow configurations
and Go component code for the GoCodeAlone/workflow engine.
## Workflow Engine Overview
The engine builds workflows from YAML config with three top-level sections:
- modules: list of named, typed components with optional config and dependencies
- workflows: workflow definitions keyed by type (http, messaging, statemachine, event)
- triggers: trigger definitions keyed by type (http, schedule, event)
## Available Module Types
### HTTP Infrastructure
- http.server: HTTP server with address config
- http.router: HTTP router (depends on server)
- http.handler: Generic HTTP handler with contentType config
- api.handler: REST API handler with resourceName config
- http.middleware.auth: Auth middleware with secretKey/authType config
- http.middleware.logging: Logging middleware with logLevel config
- http.middleware.ratelimit: Rate limiter with requestsPerMinute and burstSize
- http.middleware.cors: CORS middleware with allowedOrigins and allowedMethods
### Messaging
- messaging.broker: In-memory message broker
- messaging.broker.eventbus: EventBus bridge for pub/sub
- messaging.handler: Message handler for topic subscriptions
### State Machine
- statemachine.engine: State machine engine
- state.tracker: State tracker for resources
- state.connector: Connects state machines to resources
### Events
- event.processor: Complex event pattern matching with bufferSize and cleanupInterval
### Infrastructure
- scheduler.modular: Cron-based scheduler (deprecated)
- cache.modular: Cache module (deprecated)
- reverseproxy / http.proxy: Reverse proxy
## Workflow Types
### HTTP Workflow
Defines HTTP routes with handlers and optional middleware:
` + "```yaml" + `
workflows:
http:
routes:
- method: GET
path: /api/resource
handler: handlerName
middlewares:
- authMiddleware
` + "```" + `
### Messaging Workflow
Defines topic subscriptions with handlers:
` + "```yaml" + `
workflows:
messaging:
subscriptions:
- topic: events-topic
handler: eventHandler
` + "```" + `
### State Machine Workflow
Defines states, transitions, and hooks:
` + "```yaml" + `
workflows:
statemachine:
engine: engineName
definitions:
- name: workflow-name
initialState: "new"
states:
new:
description: "Initial state"
isFinal: false
transitions:
advance:
fromState: "new"
toState: "processing"
hooks:
- workflowType: "workflow-name"
transitions: ["advance"]
handler: "handlerName"
` + "```" + `
### Event Workflow
Defines complex event patterns and handlers:
` + "```yaml" + `
workflows:
event:
processor: processorName
patterns:
- patternId: "pattern-name"
eventTypes: ["event.type.a", "event.type.b"]
windowTime: "5m"
condition: "count" # or "sequence"
minOccurs: 3
handlers:
- patternId: "pattern-name"
handler: handlerName
` + "```" + `
## Trigger Types
### HTTP Trigger
Maps HTTP endpoints to workflow actions:
` + "```yaml" + `
triggers:
http:
routes:
- path: "/api/workflows/action"
method: "POST"
workflow: "workflow-name"
action: "action-name"
` + "```" + `
### Schedule Trigger
Cron-based workflow triggers:
` + "```yaml" + `
triggers:
schedule:
jobs:
- cron: "0 * * * *"
workflow: "workflow-name"
action: "action-name"
params:
key: value
` + "```" + `
### Event Trigger
Event-based workflow triggers:
` + "```yaml" + `
triggers:
event:
subscriptions:
- topic: "events-topic"
event: "event.type"
workflow: "workflow-name"
action: "action-name"
` + "```" + `
## Go Component Interfaces
When generating custom components, implement these interfaces:
### modular.Module
` + "```go" + `
type Module interface {
Name() string
RegisterConfig(app Application)
Init(app Application) error
}
` + "```" + `
### workflow.WorkflowHandler
` + "```go" + `
type WorkflowHandler interface {
CanHandle(workflowType string) bool
ConfigureWorkflow(app modular.Application, workflowConfig interface{}) error
ExecuteWorkflow(ctx context.Context, workflowType string, action string, data map[string]interface{}) (map[string]interface{}, error)
}
` + "```" + `
## Rules
1. Always output valid YAML for workflow configs.
2. Ensure module dependencies are declared with dependsOn.
3. Use existing module types when possible.
4. When a module type doesn't exist, specify it as a ComponentSpec so it can be generated.
5. All generated Go code must compile and implement the required interfaces.
6. Keep workflows focused and composable.`
}
// GeneratePrompt builds a workflow generation prompt from the request.
func GeneratePrompt(req GenerateRequest) string {
var b strings.Builder
b.WriteString("Generate a workflow configuration for the following request:\n\n")
fmt.Fprintf(&b, "Intent: %s\n\n", req.Intent)
if len(req.Context) > 0 {
b.WriteString("Additional context:\n")
for k, v := range req.Context {
fmt.Fprintf(&b, "- %s: %s\n", k, v)
}
b.WriteString("\n")
}
if len(req.Constraints) > 0 {
b.WriteString("Constraints:\n")
for _, c := range req.Constraints {
fmt.Fprintf(&b, "- %s\n", c)
}
b.WriteString("\n")
}
b.WriteString(`Respond with a JSON object containing:
1. "workflow": a complete WorkflowConfig with modules, workflows, and triggers sections
2. "components": an array of ComponentSpec objects for any modules that don't exist as built-in types
3. "explanation": a brief explanation of how the workflow operates
For each component in "components", include:
- "name": the module name
- "type": the module type string
- "description": what the component does
- "interface": which Go interface it implements (e.g., "modular.Module")
- "goCode": compilable Go source code for the component
`)
return b.String()
}
// ComponentFormat specifies the target format for generated component code.
type ComponentFormat string
const (
// FormatModule generates code implementing the modular.Module interface (struct-based).
FormatModule ComponentFormat = "module"
// FormatDynamic generates code as a flat package with exported functions
// compatible with the Yaegi dynamic interpreter.
FormatDynamic ComponentFormat = "dynamic"
)
// ComponentPrompt builds a prompt for generating a single component.
func ComponentPrompt(spec ComponentSpec) string {
return fmt.Sprintf(`Generate Go source code for a workflow component with the following specification:
Name: %s
Type: %s
Description: %s
Interface: %s
Requirements:
1. The code must compile and implement the %s interface.
2. Include the package declaration.
3. Include all necessary imports.
4. Add meaningful error handling.
5. Follow Go conventions and best practices.
Return only the Go source code, no explanation.`, spec.Name, spec.Type, spec.Description, spec.Interface, spec.Interface)
}
// DynamicComponentPrompt builds a prompt for generating a component in dynamic format.
// Dynamic components are flat packages with exported functions that can be loaded
// by the Yaegi interpreter at runtime.
func DynamicComponentPrompt(spec ComponentSpec) string {
return fmt.Sprintf(`Generate Go source code for a dynamic workflow component with the following specification:
Name: %s
Type: %s
Description: %s
The code MUST follow this exact format for the Yaegi dynamic interpreter:
1. Package must be "package component"
2. Only use standard library imports from this allowed list:
fmt, strings, strconv, encoding/json, encoding/xml, encoding/csv,
encoding/base64, context, time, math, math/rand, sort, sync,
sync/atomic, errors, io, bytes, bufio, unicode, unicode/utf8,
regexp, path, net/url, net/http, log, maps, slices,
crypto/sha256, crypto/hmac, crypto/md5, hash, html,
html/template, text/template
3. NO third-party imports (no github.com/... packages)
4. Must have these exported functions:
- Name() string - returns %q
- Init(map[string]interface{}) error - initialization with service map
- Execute(context.Context, map[string]interface{}) (map[string]interface{}, error) - main logic
5. Optionally include:
- Start(context.Context) error
- Stop(context.Context) error
Example structure:
`+"```go"+`
package component
import (
"context"
"fmt"
)
func Name() string {
return "component-name"
}
func Init(services map[string]interface{}) error {
return nil
}
func Execute(ctx context.Context, params map[string]interface{}) (map[string]interface{}, error) {
// Component logic here
return map[string]interface{}{"result": "value"}, nil
}
`+"```"+`
Return only the Go source code, no explanation.`, spec.Name, spec.Type, spec.Description, spec.Name)
}
// SuggestPrompt builds a prompt for workflow suggestions.
func SuggestPrompt(useCase string) string {
return fmt.Sprintf(`Suggest workflow configurations for the following use case:
%s
Return a JSON array of workflow suggestions, each containing:
- "name": a short name for the workflow
- "description": what the workflow does
- "config": a complete WorkflowConfig (with modules, workflows, triggers)
- "confidence": a number between 0 and 1 indicating how well this matches the use case
Provide 1-3 suggestions, ordered by confidence (highest first).`, useCase)
}
// MissingComponentsPrompt builds a prompt for identifying missing components.
func MissingComponentsPrompt(moduleTypes []string) string {
return fmt.Sprintf(`Given a workflow config that references the following module types:
%s
Identify which module types are NOT built-in and would need custom Go implementations.
The built-in module types are:
- http.server, http.router, http.handler, api.handler
- http.middleware.auth, http.middleware.logging, http.middleware.ratelimit, http.middleware.cors
- messaging.broker, messaging.handler
- statemachine.engine, state.tracker, state.connector
- event.processor
- scheduler.modular, cache.modular
- reverseproxy, http.proxy, messaging.broker.eventbus
For each non-built-in type, return a JSON array of ComponentSpec objects with name, type, description, and interface fields.
Leave goCode empty - it will be generated separately.`, strings.Join(moduleTypes, "\n- "))
}
// LoadExampleConfigs reads example YAML files from the given directory
// and returns them as a map of filename to content.
func LoadExampleConfigs(exampleDir string) (map[string]string, error) {
examples := make(map[string]string)
entries, err := os.ReadDir(exampleDir)
if err != nil {
return nil, fmt.Errorf("failed to read example directory: %w", err)
}
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".yaml") {
continue
}
data, err := os.ReadFile(filepath.Join(exampleDir, entry.Name()))
if err != nil {
continue // skip unreadable files
}
examples[entry.Name()] = string(data)
}
return examples, nil
}
// ContextEnrichedPrompt enriches a component generation prompt with available
// module types and services so the AI can generate components that integrate
// with the existing system.
func ContextEnrichedPrompt(spec ComponentSpec, availableModules []string, availableServices []string) string {
var b strings.Builder
b.WriteString(DynamicComponentPrompt(spec))
if len(availableModules) > 0 {
b.WriteString("\n\n## Available Module Types\n")
b.WriteString("The following module types are available in the system:\n")
for _, m := range availableModules {
fmt.Fprintf(&b, "- %s\n", m)
}
}
if len(availableServices) > 0 {
b.WriteString("\n\n## Available Services\n")
b.WriteString("The following services are available via the Init(services) map:\n")
for _, s := range availableServices {
fmt.Fprintf(&b, "- %s\n", s)
}
}
return b.String()
}
// ExamplePromptSection builds a prompt section with example configs.
func ExamplePromptSection(examples map[string]string) string {
if len(examples) == 0 {
return ""
}
var b strings.Builder
b.WriteString("\n## Example Configurations\n\n")
for name, content := range examples {
fmt.Fprintf(&b, "### %s\n```yaml\n%s\n```\n\n", name, content)
}
return b.String()
}