openplanr 0.1.0 → 0.3.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/README.md +23 -5
- package/dist/agents/agent-factory.d.ts +7 -0
- package/dist/agents/agent-factory.d.ts.map +1 -0
- package/dist/agents/agent-factory.js +22 -0
- package/dist/agents/agent-factory.js.map +1 -0
- package/dist/agents/claude-agent.d.ts +13 -0
- package/dist/agents/claude-agent.d.ts.map +1 -0
- package/dist/agents/claude-agent.js +48 -0
- package/dist/agents/claude-agent.js.map +1 -0
- package/dist/agents/codex-agent.d.ts +13 -0
- package/dist/agents/codex-agent.d.ts.map +1 -0
- package/dist/agents/codex-agent.js +47 -0
- package/dist/agents/codex-agent.js.map +1 -0
- package/dist/agents/cursor-agent.d.ts +13 -0
- package/dist/agents/cursor-agent.d.ts.map +1 -0
- package/dist/agents/cursor-agent.js +40 -0
- package/dist/agents/cursor-agent.js.map +1 -0
- package/dist/agents/implementation-bridge.d.ts +21 -0
- package/dist/agents/implementation-bridge.d.ts.map +1 -0
- package/dist/agents/implementation-bridge.js +173 -0
- package/dist/agents/implementation-bridge.js.map +1 -0
- package/dist/agents/index.d.ts +6 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +5 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/prompt-composer.d.ts +30 -0
- package/dist/agents/prompt-composer.d.ts.map +1 -0
- package/dist/agents/prompt-composer.js +81 -0
- package/dist/agents/prompt-composer.js.map +1 -0
- package/dist/agents/task-parser.d.ts +38 -0
- package/dist/agents/task-parser.d.ts.map +1 -0
- package/dist/agents/task-parser.js +83 -0
- package/dist/agents/task-parser.js.map +1 -0
- package/dist/agents/types.d.ts +23 -0
- package/dist/agents/types.d.ts.map +1 -0
- package/dist/agents/types.js +8 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/agents/utils.d.ts +9 -0
- package/dist/agents/utils.d.ts.map +1 -0
- package/dist/agents/utils.js +21 -0
- package/dist/agents/utils.js.map +1 -0
- package/dist/ai/codebase/context-builder.d.ts +31 -0
- package/dist/ai/codebase/context-builder.d.ts.map +1 -0
- package/dist/ai/codebase/context-builder.js +93 -0
- package/dist/ai/codebase/context-builder.js.map +1 -0
- package/dist/ai/codebase/file-reader.d.ts +22 -0
- package/dist/ai/codebase/file-reader.d.ts.map +1 -0
- package/dist/ai/codebase/file-reader.js +111 -0
- package/dist/ai/codebase/file-reader.js.map +1 -0
- package/dist/ai/codebase/index.d.ts +5 -0
- package/dist/ai/codebase/index.d.ts.map +1 -0
- package/dist/ai/codebase/index.js +5 -0
- package/dist/ai/codebase/index.js.map +1 -0
- package/dist/ai/codebase/stack-detector.d.ts +18 -0
- package/dist/ai/codebase/stack-detector.d.ts.map +1 -0
- package/dist/ai/codebase/stack-detector.js +147 -0
- package/dist/ai/codebase/stack-detector.js.map +1 -0
- package/dist/ai/codebase/tree-generator.d.ts +8 -0
- package/dist/ai/codebase/tree-generator.d.ts.map +1 -0
- package/dist/ai/codebase/tree-generator.js +85 -0
- package/dist/ai/codebase/tree-generator.js.map +1 -0
- package/dist/ai/errors.d.ts +22 -0
- package/dist/ai/errors.d.ts.map +1 -0
- package/dist/ai/errors.js +70 -0
- package/dist/ai/errors.js.map +1 -0
- package/dist/ai/index.d.ts +11 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/index.js +10 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/ai/prompts/prompt-builder.d.ts +42 -0
- package/dist/ai/prompts/prompt-builder.d.ts.map +1 -0
- package/dist/ai/prompts/prompt-builder.js +96 -0
- package/dist/ai/prompts/prompt-builder.js.map +1 -0
- package/dist/ai/prompts/system-prompts.d.ts +13 -0
- package/dist/ai/prompts/system-prompts.d.ts.map +1 -0
- package/dist/ai/prompts/system-prompts.js +124 -0
- package/dist/ai/prompts/system-prompts.js.map +1 -0
- package/dist/ai/provider-factory.d.ts +10 -0
- package/dist/ai/provider-factory.d.ts.map +1 -0
- package/dist/ai/provider-factory.js +33 -0
- package/dist/ai/provider-factory.js.map +1 -0
- package/dist/ai/providers/anthropic-provider.d.ts +22 -0
- package/dist/ai/providers/anthropic-provider.d.ts.map +1 -0
- package/dist/ai/providers/anthropic-provider.js +82 -0
- package/dist/ai/providers/anthropic-provider.js.map +1 -0
- package/dist/ai/providers/ollama-provider.d.ts +13 -0
- package/dist/ai/providers/ollama-provider.d.ts.map +1 -0
- package/dist/ai/providers/ollama-provider.js +16 -0
- package/dist/ai/providers/ollama-provider.js.map +1 -0
- package/dist/ai/providers/openai-provider.d.ts +17 -0
- package/dist/ai/providers/openai-provider.d.ts.map +1 -0
- package/dist/ai/providers/openai-provider.js +58 -0
- package/dist/ai/providers/openai-provider.js.map +1 -0
- package/dist/ai/schemas/ai-response-schemas.d.ts +425 -0
- package/dist/ai/schemas/ai-response-schemas.d.ts.map +1 -0
- package/dist/ai/schemas/ai-response-schemas.js +87 -0
- package/dist/ai/schemas/ai-response-schemas.js.map +1 -0
- package/dist/ai/types.d.ts +40 -0
- package/dist/ai/types.d.ts.map +1 -0
- package/dist/ai/types.js +16 -0
- package/dist/ai/types.js.map +1 -0
- package/dist/cli/commands/checklist.d.ts.map +1 -1
- package/dist/cli/commands/checklist.js +60 -1
- package/dist/cli/commands/checklist.js.map +1 -1
- package/dist/cli/commands/config.d.ts +8 -0
- package/dist/cli/commands/config.d.ts.map +1 -0
- package/dist/cli/commands/config.js +112 -0
- package/dist/cli/commands/config.js.map +1 -0
- package/dist/cli/commands/epic.d.ts +7 -0
- package/dist/cli/commands/epic.d.ts.map +1 -1
- package/dist/cli/commands/epic.js +161 -31
- package/dist/cli/commands/epic.js.map +1 -1
- package/dist/cli/commands/feature.d.ts +6 -0
- package/dist/cli/commands/feature.d.ts.map +1 -1
- package/dist/cli/commands/feature.js +143 -30
- package/dist/cli/commands/feature.js.map +1 -1
- package/dist/cli/commands/init.d.ts +6 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +48 -6
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/plan.d.ts +15 -0
- package/dist/cli/commands/plan.d.ts.map +1 -0
- package/dist/cli/commands/plan.js +259 -0
- package/dist/cli/commands/plan.js.map +1 -0
- package/dist/cli/commands/refine.d.ts +9 -0
- package/dist/cli/commands/refine.d.ts.map +1 -0
- package/dist/cli/commands/refine.js +101 -0
- package/dist/cli/commands/refine.js.map +1 -0
- package/dist/cli/commands/status.d.ts.map +1 -1
- package/dist/cli/commands/status.js +150 -18
- package/dist/cli/commands/status.js.map +1 -1
- package/dist/cli/commands/story.d.ts +6 -0
- package/dist/cli/commands/story.d.ts.map +1 -1
- package/dist/cli/commands/story.js +259 -45
- package/dist/cli/commands/story.js.map +1 -1
- package/dist/cli/commands/sync.d.ts +12 -0
- package/dist/cli/commands/sync.d.ts.map +1 -0
- package/dist/cli/commands/sync.js +227 -0
- package/dist/cli/commands/sync.js.map +1 -0
- package/dist/cli/commands/task.d.ts +9 -0
- package/dist/cli/commands/task.d.ts.map +1 -1
- package/dist/cli/commands/task.js +350 -35
- package/dist/cli/commands/task.js.map +1 -1
- package/dist/cli/index.js +16 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/models/schema.d.ts +41 -0
- package/dist/models/schema.d.ts.map +1 -1
- package/dist/models/schema.js +9 -0
- package/dist/models/schema.js.map +1 -1
- package/dist/models/types.d.ts +9 -0
- package/dist/models/types.d.ts.map +1 -1
- package/dist/services/ai-service.d.ts +40 -0
- package/dist/services/ai-service.d.ts.map +1 -0
- package/dist/services/ai-service.js +150 -0
- package/dist/services/ai-service.js.map +1 -0
- package/dist/services/artifact-gathering.d.ts +49 -0
- package/dist/services/artifact-gathering.d.ts.map +1 -0
- package/dist/services/artifact-gathering.js +128 -0
- package/dist/services/artifact-gathering.js.map +1 -0
- package/dist/services/artifact-service.d.ts +47 -0
- package/dist/services/artifact-service.d.ts.map +1 -1
- package/dist/services/artifact-service.js +169 -0
- package/dist/services/artifact-service.js.map +1 -1
- package/dist/services/checklist-service.d.ts +23 -0
- package/dist/services/checklist-service.d.ts.map +1 -1
- package/dist/services/checklist-service.js +44 -0
- package/dist/services/checklist-service.js.map +1 -1
- package/dist/services/credentials-service.d.ts +22 -0
- package/dist/services/credentials-service.d.ts.map +1 -0
- package/dist/services/credentials-service.js +58 -0
- package/dist/services/credentials-service.js.map +1 -0
- package/dist/services/id-service.d.ts.map +1 -1
- package/dist/services/id-service.js +8 -5
- package/dist/services/id-service.js.map +1 -1
- package/dist/services/prompt-service.d.ts +6 -0
- package/dist/services/prompt-service.d.ts.map +1 -1
- package/dist/services/prompt-service.js +7 -1
- package/dist/services/prompt-service.js.map +1 -1
- package/dist/services/template-service.d.ts.map +1 -1
- package/dist/services/template-service.js +6 -0
- package/dist/services/template-service.js.map +1 -1
- package/dist/templates/checklists/agile-checklist.md.hbs +8 -8
- package/dist/templates/epics/epic.md.hbs +8 -2
- package/dist/templates/features/feature.md.hbs +3 -3
- package/dist/templates/rules/cursor/2001-agile-create-epic.mdc.hbs +1 -1
- package/dist/templates/rules/cursor/2002-agile-create-features.mdc.hbs +1 -1
- package/dist/templates/rules/cursor/2003-agile-create-user-story.mdc.hbs +1 -1
- package/dist/templates/stories/user-story.md.hbs +2 -2
- package/dist/templates/tasks/task-list.md.hbs +26 -3
- package/dist/utils/logger.d.ts +3 -0
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +12 -0
- package/dist/utils/logger.js.map +1 -1
- package/package.json +12 -5
- package/dist/templates/templates/adrs/adr-general.md.hbs +0 -46
- package/dist/templates/templates/checklists/agile-checklist.md.hbs +0 -49
- package/dist/templates/templates/epics/epic.md.hbs +0 -46
- package/dist/templates/templates/features/feature.md.hbs +0 -42
- package/dist/templates/templates/rules/claude/CLAUDE.md.hbs +0 -63
- package/dist/templates/templates/rules/codex/AGENTS.md.hbs +0 -28
- package/dist/templates/templates/rules/cursor/2000-agile-checklist.mdc.hbs +0 -33
- package/dist/templates/templates/rules/cursor/2001-agile-create-epic.mdc.hbs +0 -35
- package/dist/templates/templates/rules/cursor/2002-agile-create-features.mdc.hbs +0 -35
- package/dist/templates/templates/rules/cursor/2003-agile-create-user-story.mdc.hbs +0 -31
- package/dist/templates/templates/rules/cursor/2100-create-task-list.mdc.hbs +0 -36
- package/dist/templates/templates/rules/cursor/2101-implement-task-list.mdc.hbs +0 -28
- package/dist/templates/templates/stories/gherkin.feature.hbs +0 -13
- package/dist/templates/templates/stories/user-story.md.hbs +0 -28
- package/dist/templates/templates/tasks/task-list.md.hbs +0 -24
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expert system prompts for AI-powered agile planning.
|
|
3
|
+
*
|
|
4
|
+
* Each prompt establishes the AI as a specialized agile consultant
|
|
5
|
+
* and instructs it to produce structured JSON output matching our
|
|
6
|
+
* artifact schemas.
|
|
7
|
+
*/
|
|
8
|
+
const BASE_PERSONA = `You are an expert agile planning consultant with deep experience in software development, product management, and technical architecture. You help teams create clear, actionable, and well-structured planning artifacts.`;
|
|
9
|
+
export const EPIC_SYSTEM_PROMPT = `${BASE_PERSONA}
|
|
10
|
+
|
|
11
|
+
Your task is to expand a brief description into a complete, detailed epic document.
|
|
12
|
+
|
|
13
|
+
You MUST respond with a valid JSON object containing these fields:
|
|
14
|
+
- "title": A concise, descriptive epic title (max 80 chars)
|
|
15
|
+
- "owner": The responsible team or role (e.g., "Engineering", "Product", "Platform Team")
|
|
16
|
+
- "businessValue": Why this matters to the business (2-3 sentences)
|
|
17
|
+
- "targetUsers": Who benefits from this (specific user personas)
|
|
18
|
+
- "problemStatement": The problem being solved (2-3 sentences)
|
|
19
|
+
- "solutionOverview": High-level approach to solving it (2-3 sentences)
|
|
20
|
+
- "successCriteria": Array of 3-5 measurable definition-of-done bullet points (e.g., ["Users can X within Y seconds", "System supports Z"])
|
|
21
|
+
- "keyFeatures": Array of 3-7 high-level feature names that compose this epic
|
|
22
|
+
- "dependencies": Known dependencies or "None"
|
|
23
|
+
- "risks": Known risks or "None"
|
|
24
|
+
|
|
25
|
+
Be specific, avoid generic filler. Ground the epic in the user's brief.
|
|
26
|
+
Respond with JSON only, no markdown or explanation.`;
|
|
27
|
+
export const FEATURES_SYSTEM_PROMPT = `${BASE_PERSONA}
|
|
28
|
+
|
|
29
|
+
Your task is to decompose an epic into individual features. Read the epic carefully and generate features that fully cover its scope.
|
|
30
|
+
|
|
31
|
+
You MUST respond with a valid JSON object containing:
|
|
32
|
+
- "features": An array of feature objects, each with:
|
|
33
|
+
- "title": A clear feature title (max 80 chars)
|
|
34
|
+
- "overview": What this feature does (2-3 sentences)
|
|
35
|
+
- "functionalRequirements": Array of 3-6 specific functional requirements
|
|
36
|
+
- "dependencies": Dependencies on other features or systems, or "None"
|
|
37
|
+
- "technicalConsiderations": Technical notes for implementation, or "None"
|
|
38
|
+
- "risks": Feature-specific risks, or "None"
|
|
39
|
+
- "successMetrics": How to measure success of this feature
|
|
40
|
+
|
|
41
|
+
Generate features that are:
|
|
42
|
+
- Independently deliverable where possible
|
|
43
|
+
- Roughly equal in scope
|
|
44
|
+
- Non-overlapping (no duplicate functionality)
|
|
45
|
+
|
|
46
|
+
Respond with JSON only, no markdown or explanation.`;
|
|
47
|
+
export const STORIES_SYSTEM_PROMPT = `${BASE_PERSONA}
|
|
48
|
+
|
|
49
|
+
Your task is to break a feature into user stories. Read the feature and its parent epic context carefully.
|
|
50
|
+
|
|
51
|
+
You MUST respond with a valid JSON object containing:
|
|
52
|
+
- "stories": An array of story objects, each with:
|
|
53
|
+
- "title": Concise story title (max 80 chars)
|
|
54
|
+
- "role": The user role ("As a <role>")
|
|
55
|
+
- "goal": What they want to do ("I want to <goal>")
|
|
56
|
+
- "benefit": Why ("So that <benefit>")
|
|
57
|
+
- "additionalNotes": Implementation notes or edge cases (optional, can be empty string)
|
|
58
|
+
- "gherkinScenarios": Array of scenario objects, each with:
|
|
59
|
+
- "name": Scenario name
|
|
60
|
+
- "given": Given precondition
|
|
61
|
+
- "when": When action
|
|
62
|
+
- "then": Then expected outcome
|
|
63
|
+
|
|
64
|
+
Each story should:
|
|
65
|
+
- Follow INVEST principles (Independent, Negotiable, Valuable, Estimable, Small, Testable)
|
|
66
|
+
- Include 1-3 Gherkin scenarios (happy path + edge cases)
|
|
67
|
+
- Be specific enough for a developer to implement
|
|
68
|
+
|
|
69
|
+
Respond with JSON only, no markdown or explanation.`;
|
|
70
|
+
export const TASKS_SYSTEM_PROMPT = `${BASE_PERSONA}
|
|
71
|
+
|
|
72
|
+
Your task is to generate a comprehensive implementation task list from agile artifacts (user stories, gherkin acceptance criteria, feature specs, epic context, ADRs, and codebase context).
|
|
73
|
+
|
|
74
|
+
You MUST respond with a valid JSON object containing:
|
|
75
|
+
- "title": A task list title — use the scope ID if provided (e.g., "Tasks for FEAT-001: Feature Name" when scope is a feature, or "Tasks for US-001: Story Name" when scope is a story)
|
|
76
|
+
- "tasks": An array of task group objects, each with:
|
|
77
|
+
- "id": Numbering like "1.0", "2.0", "3.0"
|
|
78
|
+
- "title": Task group title
|
|
79
|
+
- "subtasks": Array of subtask objects, each with:
|
|
80
|
+
- "id": Numbering like "1.1", "1.2", "2.1"
|
|
81
|
+
- "title": Specific, actionable subtask description
|
|
82
|
+
- "acceptanceCriteriaMapping": Array of objects mapping acceptance criteria to tasks:
|
|
83
|
+
- "criterion": The acceptance criterion text (from gherkin scenarios or story requirements)
|
|
84
|
+
- "sourceStoryId": Which user story this criterion comes from (e.g., "US-001")
|
|
85
|
+
- "taskIds": Array of task/subtask IDs that satisfy this criterion (e.g., ["1.1", "2.3"])
|
|
86
|
+
- "relevantFiles": Array of files to create or modify:
|
|
87
|
+
- "path": File path relative to project root (e.g., "src/auth/login.ts")
|
|
88
|
+
- "reason": Brief explanation of why this file needs changes
|
|
89
|
+
|
|
90
|
+
Tasks should:
|
|
91
|
+
- Reference actual files/paths from the codebase when possible
|
|
92
|
+
- Follow existing code patterns and conventions
|
|
93
|
+
- Include setup, implementation, testing, and cleanup steps
|
|
94
|
+
- Be ordered logically (dependencies first)
|
|
95
|
+
- Address specific acceptance criteria from gherkin scenarios
|
|
96
|
+
- Respect architectural decisions from ADRs when provided
|
|
97
|
+
- Align with component structure and system architecture
|
|
98
|
+
|
|
99
|
+
When multiple user stories and gherkin scenarios are provided, ensure every acceptance criterion is covered by at least one task. When codebase context is available, identify specific files to modify in relevantFiles.
|
|
100
|
+
|
|
101
|
+
Respond with JSON only, no markdown or explanation.`;
|
|
102
|
+
export const REFINE_SYSTEM_PROMPT = `${BASE_PERSONA}
|
|
103
|
+
|
|
104
|
+
Your task is to review and improve an existing agile artifact. Analyze the content and suggest improvements for:
|
|
105
|
+
- Clarity and specificity
|
|
106
|
+
- Missing details or edge cases
|
|
107
|
+
- Consistency with agile best practices
|
|
108
|
+
- Technical accuracy
|
|
109
|
+
|
|
110
|
+
You MUST respond with a valid JSON object containing:
|
|
111
|
+
- "suggestions": Array of improvement suggestions (strings)
|
|
112
|
+
- "improved": The improved artifact data as a JSON object with the same fields as the original frontmatter
|
|
113
|
+
- "improvedMarkdown": A raw markdown string that will be written directly to a .md file. It MUST preserve the original file format: YAML frontmatter between --- delimiters followed by the markdown body. Do NOT put JSON in this field.
|
|
114
|
+
|
|
115
|
+
CRITICAL: The "improvedMarkdown" field must be a plain markdown string, NOT a JSON object. It should look exactly like the original artifact the user provided, but with improvements applied. For example, if the original starts with:
|
|
116
|
+
---
|
|
117
|
+
id: "EPIC-001"
|
|
118
|
+
title: "My Epic"
|
|
119
|
+
---
|
|
120
|
+
# EPIC-001: My Epic
|
|
121
|
+
...then "improvedMarkdown" must also start with --- frontmatter and contain markdown content. Keep the same structure and sections as the original.
|
|
122
|
+
|
|
123
|
+
Respond with JSON only, no markdown or explanation.`;
|
|
124
|
+
//# sourceMappingURL=system-prompts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"system-prompts.js","sourceRoot":"","sources":["../../../src/ai/prompts/system-prompts.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,YAAY,GAAG,4NAA4N,CAAC;AAElP,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,YAAY;;;;;;;;;;;;;;;;;oDAiBG,CAAC;AAErD,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,YAAY;;;;;;;;;;;;;;;;;;;oDAmBD,CAAC;AAErD,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,YAAY;;;;;;;;;;;;;;;;;;;;;;oDAsBA,CAAC;AAErD,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;oDA+BE,CAAC;AAErD,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,YAAY;;;;;;;;;;;;;;;;;;;;;oDAqBC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factory for creating AI provider instances.
|
|
3
|
+
*
|
|
4
|
+
* Uses dynamic imports to lazy-load SDK dependencies — the heavy
|
|
5
|
+
* @anthropic-ai/sdk and openai packages are only loaded when AI
|
|
6
|
+
* features are actually invoked.
|
|
7
|
+
*/
|
|
8
|
+
import type { AIProvider, AIProviderConfig } from './types.js';
|
|
9
|
+
export declare function createAIProvider(config: AIProviderConfig): Promise<AIProvider>;
|
|
10
|
+
//# sourceMappingURL=provider-factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-factory.d.ts","sourceRoot":"","sources":["../../src/ai/provider-factory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAG/D,wBAAsB,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC,CAmCpF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factory for creating AI provider instances.
|
|
3
|
+
*
|
|
4
|
+
* Uses dynamic imports to lazy-load SDK dependencies — the heavy
|
|
5
|
+
* @anthropic-ai/sdk and openai packages are only loaded when AI
|
|
6
|
+
* features are actually invoked.
|
|
7
|
+
*/
|
|
8
|
+
import { AIError } from './errors.js';
|
|
9
|
+
export async function createAIProvider(config) {
|
|
10
|
+
switch (config.provider) {
|
|
11
|
+
case 'anthropic': {
|
|
12
|
+
if (!config.apiKey) {
|
|
13
|
+
throw new AIError('Anthropic API key is required. Run `planr config set-key anthropic` or set ANTHROPIC_API_KEY.', 'auth');
|
|
14
|
+
}
|
|
15
|
+
const { AnthropicProvider } = await import('./providers/anthropic-provider.js');
|
|
16
|
+
return new AnthropicProvider(config.apiKey, config.model);
|
|
17
|
+
}
|
|
18
|
+
case 'openai': {
|
|
19
|
+
if (!config.apiKey) {
|
|
20
|
+
throw new AIError('OpenAI API key is required. Run `planr config set-key openai` or set OPENAI_API_KEY.', 'auth');
|
|
21
|
+
}
|
|
22
|
+
const { OpenAIProvider } = await import('./providers/openai-provider.js');
|
|
23
|
+
return new OpenAIProvider(config.apiKey, config.model, config.baseUrl);
|
|
24
|
+
}
|
|
25
|
+
case 'ollama': {
|
|
26
|
+
const { OllamaProvider } = await import('./providers/ollama-provider.js');
|
|
27
|
+
return new OllamaProvider(config.model, config.baseUrl);
|
|
28
|
+
}
|
|
29
|
+
default:
|
|
30
|
+
throw new AIError(`Unknown AI provider: ${config.provider}. Supported: anthropic, openai, ollama.`, 'unknown');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=provider-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-factory.js","sourceRoot":"","sources":["../../src/ai/provider-factory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,MAAwB;IAC7D,QAAQ,MAAM,CAAC,QAAQ,EAAE,CAAC;QACxB,KAAK,WAAW,CAAC,CAAC,CAAC;YACjB,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;gBACnB,MAAM,IAAI,OAAO,CACf,+FAA+F,EAC/F,MAAM,CACP,CAAC;YACJ,CAAC;YACD,MAAM,EAAE,iBAAiB,EAAE,GAAG,MAAM,MAAM,CAAC,mCAAmC,CAAC,CAAC;YAChF,OAAO,IAAI,iBAAiB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QAC5D,CAAC;QAED,KAAK,QAAQ,CAAC,CAAC,CAAC;YACd,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;gBACnB,MAAM,IAAI,OAAO,CACf,sFAAsF,EACtF,MAAM,CACP,CAAC;YACJ,CAAC;YACD,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,MAAM,CAAC,gCAAgC,CAAC,CAAC;YAC1E,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;QACzE,CAAC;QAED,KAAK,QAAQ,CAAC,CAAC,CAAC;YACd,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,MAAM,CAAC,gCAAgC,CAAC,CAAC;YAC1E,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1D,CAAC;QAED;YACE,MAAM,IAAI,OAAO,CACf,wBAAwB,MAAM,CAAC,QAAQ,yCAAyC,EAChF,SAAS,CACV,CAAC;IACN,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic Claude provider implementation.
|
|
3
|
+
*
|
|
4
|
+
* Uses the official @anthropic-ai/sdk with streaming support.
|
|
5
|
+
* Lazily imported to avoid loading the SDK until actually needed.
|
|
6
|
+
*/
|
|
7
|
+
import type { AIProvider, AIMessage, AIRequestOptions, AIProviderName } from '../types.js';
|
|
8
|
+
export declare class AnthropicProvider implements AIProvider {
|
|
9
|
+
readonly name: AIProviderName;
|
|
10
|
+
readonly model: string;
|
|
11
|
+
private clientPromise;
|
|
12
|
+
constructor(apiKey: string, model?: string);
|
|
13
|
+
private initClient;
|
|
14
|
+
chat(messages: AIMessage[], options?: AIRequestOptions): AsyncIterable<string>;
|
|
15
|
+
chatSync(messages: AIMessage[], options?: AIRequestOptions): Promise<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Anthropic's API uses a separate `system` parameter rather than
|
|
18
|
+
* a system role in the messages array.
|
|
19
|
+
*/
|
|
20
|
+
private splitSystemMessage;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=anthropic-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-provider.d.ts","sourceRoot":"","sources":["../../../src/ai/providers/anthropic-provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG3F,qBAAa,iBAAkB,YAAW,UAAU;IAClD,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAe;IAC5C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,OAAO,CAAC,aAAa,CAAoE;gBAE7E,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;YAK5B,UAAU;IAKjB,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,aAAa,CAAC,MAAM,CAAC;IA6B/E,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC;IA4BlF;;;OAGG;IACH,OAAO,CAAC,kBAAkB;CAQ3B"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic Claude provider implementation.
|
|
3
|
+
*
|
|
4
|
+
* Uses the official @anthropic-ai/sdk with streaming support.
|
|
5
|
+
* Lazily imported to avoid loading the SDK until actually needed.
|
|
6
|
+
*/
|
|
7
|
+
import { wrapProviderError } from '../errors.js';
|
|
8
|
+
export class AnthropicProvider {
|
|
9
|
+
name = 'anthropic';
|
|
10
|
+
model;
|
|
11
|
+
clientPromise;
|
|
12
|
+
constructor(apiKey, model) {
|
|
13
|
+
this.model = model || 'claude-sonnet-4-20250514';
|
|
14
|
+
this.clientPromise = this.initClient(apiKey);
|
|
15
|
+
}
|
|
16
|
+
async initClient(apiKey) {
|
|
17
|
+
const { default: Anthropic } = await import('@anthropic-ai/sdk');
|
|
18
|
+
return new Anthropic({ apiKey });
|
|
19
|
+
}
|
|
20
|
+
async *chat(messages, options) {
|
|
21
|
+
const client = await this.clientPromise;
|
|
22
|
+
const { system, userMessages } = this.splitSystemMessage(messages);
|
|
23
|
+
try {
|
|
24
|
+
const stream = client.messages.stream({
|
|
25
|
+
model: this.model,
|
|
26
|
+
max_tokens: options?.maxTokens || 4096,
|
|
27
|
+
temperature: options?.temperature ?? 0.7,
|
|
28
|
+
system: system || undefined,
|
|
29
|
+
messages: userMessages.map((m) => ({
|
|
30
|
+
role: m.role,
|
|
31
|
+
content: m.content,
|
|
32
|
+
})),
|
|
33
|
+
});
|
|
34
|
+
for await (const event of stream) {
|
|
35
|
+
if (event.type === 'content_block_delta' &&
|
|
36
|
+
event.delta.type === 'text_delta') {
|
|
37
|
+
yield event.delta.text;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
throw wrapProviderError(err, 'anthropic');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async chatSync(messages, options) {
|
|
46
|
+
const client = await this.clientPromise;
|
|
47
|
+
const { system, userMessages } = this.splitSystemMessage(messages);
|
|
48
|
+
try {
|
|
49
|
+
const response = await client.messages.create({
|
|
50
|
+
model: this.model,
|
|
51
|
+
max_tokens: options?.maxTokens || 4096,
|
|
52
|
+
temperature: options?.temperature ?? 0.7,
|
|
53
|
+
system: system || undefined,
|
|
54
|
+
messages: userMessages.map((m) => ({
|
|
55
|
+
role: m.role,
|
|
56
|
+
content: m.content,
|
|
57
|
+
})),
|
|
58
|
+
});
|
|
59
|
+
return response.content
|
|
60
|
+
.filter((block) => block.type === 'text')
|
|
61
|
+
.map((block) => {
|
|
62
|
+
if (block.type === 'text')
|
|
63
|
+
return block.text;
|
|
64
|
+
return '';
|
|
65
|
+
})
|
|
66
|
+
.join('');
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
throw wrapProviderError(err, 'anthropic');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Anthropic's API uses a separate `system` parameter rather than
|
|
74
|
+
* a system role in the messages array.
|
|
75
|
+
*/
|
|
76
|
+
splitSystemMessage(messages) {
|
|
77
|
+
const systemMsg = messages.find((m) => m.role === 'system');
|
|
78
|
+
const userMessages = messages.filter((m) => m.role !== 'system');
|
|
79
|
+
return { system: systemMsg?.content ?? null, userMessages };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=anthropic-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-provider.js","sourceRoot":"","sources":["../../../src/ai/providers/anthropic-provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,MAAM,OAAO,iBAAiB;IACnB,IAAI,GAAmB,WAAW,CAAC;IACnC,KAAK,CAAS;IAEf,aAAa,CAAoE;IAEzF,YAAY,MAAc,EAAE,KAAc;QACxC,IAAI,CAAC,KAAK,GAAG,KAAK,IAAI,0BAA0B,CAAC;QACjD,IAAI,CAAC,aAAa,GAAG,I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EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;QAC5D,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;QACjE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,IAAI,IAAI,EAAE,YAAY,EAAE,CAAC;IAC9D,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ollama provider implementation.
|
|
3
|
+
*
|
|
4
|
+
* Ollama exposes an OpenAI-compatible REST API, so we extend the OpenAI
|
|
5
|
+
* provider with a custom base URL and a dummy API key.
|
|
6
|
+
*/
|
|
7
|
+
import type { AIProviderName } from '../types.js';
|
|
8
|
+
import { OpenAIProvider } from './openai-provider.js';
|
|
9
|
+
export declare class OllamaProvider extends OpenAIProvider {
|
|
10
|
+
readonly name: AIProviderName;
|
|
11
|
+
constructor(model?: string, baseUrl?: string);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=ollama-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ollama-provider.d.ts","sourceRoot":"","sources":["../../../src/ai/providers/ollama-provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAItD,qBAAa,cAAe,SAAQ,cAAc;IAChD,SAAkB,IAAI,EAAE,cAAc,CAAY;gBAEtC,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM;CAO7C"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ollama provider implementation.
|
|
3
|
+
*
|
|
4
|
+
* Ollama exposes an OpenAI-compatible REST API, so we extend the OpenAI
|
|
5
|
+
* provider with a custom base URL and a dummy API key.
|
|
6
|
+
*/
|
|
7
|
+
import { OpenAIProvider } from './openai-provider.js';
|
|
8
|
+
const DEFAULT_OLLAMA_URL = 'http://localhost:11434/v1';
|
|
9
|
+
export class OllamaProvider extends OpenAIProvider {
|
|
10
|
+
name = 'ollama';
|
|
11
|
+
constructor(model, baseUrl) {
|
|
12
|
+
super('ollama', // Ollama doesn't require an API key
|
|
13
|
+
model || 'llama3.1', baseUrl || DEFAULT_OLLAMA_URL);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=ollama-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ollama-provider.js","sourceRoot":"","sources":["../../../src/ai/providers/ollama-provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,MAAM,kBAAkB,GAAG,2BAA2B,CAAC;AAEvD,MAAM,OAAO,cAAe,SAAQ,cAAc;IAC9B,IAAI,GAAmB,QAAQ,CAAC;IAElD,YAAY,KAAc,EAAE,OAAgB;QAC1C,KAAK,CACH,QAAQ,EAAE,oCAAoC;QAC9C,KAAK,IAAI,UAAU,EACnB,OAAO,IAAI,kBAAkB,CAC9B,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI provider implementation.
|
|
3
|
+
*
|
|
4
|
+
* Uses the official openai SDK with streaming and JSON mode support.
|
|
5
|
+
* Also serves as the base for the Ollama provider (OpenAI-compatible API).
|
|
6
|
+
*/
|
|
7
|
+
import type { AIProvider, AIMessage, AIRequestOptions, AIProviderName } from '../types.js';
|
|
8
|
+
export declare class OpenAIProvider implements AIProvider {
|
|
9
|
+
readonly name: AIProviderName;
|
|
10
|
+
readonly model: string;
|
|
11
|
+
protected clientPromise: Promise<InstanceType<typeof import('openai').default>>;
|
|
12
|
+
constructor(apiKey: string, model?: string, baseUrl?: string);
|
|
13
|
+
private initClient;
|
|
14
|
+
chat(messages: AIMessage[], options?: AIRequestOptions): AsyncIterable<string>;
|
|
15
|
+
chatSync(messages: AIMessage[], options?: AIRequestOptions): Promise<string>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=openai-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-provider.d.ts","sourceRoot":"","sources":["../../../src/ai/providers/openai-provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG3F,qBAAa,cAAe,YAAW,UAAU;IAC/C,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAY;IACzC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,SAAS,CAAC,aAAa,EAAE,OAAO,CAAC,YAAY,CAAC,cAAc,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;gBAEpE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM;YAK9C,UAAU;IAKjB,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,aAAa,CAAC,MAAM,CAAC;IAsB/E,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC;CAiBnF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI provider implementation.
|
|
3
|
+
*
|
|
4
|
+
* Uses the official openai SDK with streaming and JSON mode support.
|
|
5
|
+
* Also serves as the base for the Ollama provider (OpenAI-compatible API).
|
|
6
|
+
*/
|
|
7
|
+
import { wrapProviderError } from '../errors.js';
|
|
8
|
+
export class OpenAIProvider {
|
|
9
|
+
name = 'openai';
|
|
10
|
+
model;
|
|
11
|
+
clientPromise;
|
|
12
|
+
constructor(apiKey, model, baseUrl) {
|
|
13
|
+
this.model = model || 'gpt-4o';
|
|
14
|
+
this.clientPromise = this.initClient(apiKey, baseUrl);
|
|
15
|
+
}
|
|
16
|
+
async initClient(apiKey, baseUrl) {
|
|
17
|
+
const { default: OpenAI } = await import('openai');
|
|
18
|
+
return new OpenAI({ apiKey, baseURL: baseUrl });
|
|
19
|
+
}
|
|
20
|
+
async *chat(messages, options) {
|
|
21
|
+
const client = await this.clientPromise;
|
|
22
|
+
try {
|
|
23
|
+
const stream = await client.chat.completions.create({
|
|
24
|
+
model: this.model,
|
|
25
|
+
stream: true,
|
|
26
|
+
temperature: options?.temperature ?? 0.7,
|
|
27
|
+
max_tokens: options?.maxTokens || 4096,
|
|
28
|
+
messages: messages.map((m) => ({ role: m.role, content: m.content })),
|
|
29
|
+
...(options?.jsonMode && { response_format: { type: 'json_object' } }),
|
|
30
|
+
});
|
|
31
|
+
for await (const chunk of stream) {
|
|
32
|
+
const delta = chunk.choices[0]?.delta?.content;
|
|
33
|
+
if (delta)
|
|
34
|
+
yield delta;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
throw wrapProviderError(err, this.name);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async chatSync(messages, options) {
|
|
42
|
+
const client = await this.clientPromise;
|
|
43
|
+
try {
|
|
44
|
+
const response = await client.chat.completions.create({
|
|
45
|
+
model: this.model,
|
|
46
|
+
temperature: options?.temperature ?? 0.7,
|
|
47
|
+
max_tokens: options?.maxTokens || 4096,
|
|
48
|
+
messages: messages.map((m) => ({ role: m.role, content: m.content })),
|
|
49
|
+
...(options?.jsonMode && { response_format: { type: 'json_object' } }),
|
|
50
|
+
});
|
|
51
|
+
return response.choices[0]?.message?.content || '';
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
throw wrapProviderError(err, this.name);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=openai-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-provider.js","sourceRoot":"","sources":["../../../src/ai/providers/openai-provider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,MAAM,OAAO,cAAc;IAChB,IAAI,GAAmB,QAAQ,CAAC;IAChC,KAAK,CAAS;IAEb,aAAa,CAAyD;IAEhF,YAAY,MAAc,EAAE,KAAc,EAAE,OAAgB;QAC1D,IAAI,CAAC,KAAK,GAAG,KAAK,IAAI,QAAQ,CAAC;QAC/B,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxD,CAAC;IAEO,KAAK,CAAC,UAAU,CAAC,MAAc,EAAE,OAAgB;QACvD,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,CAAC;QACnD,OAAO,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;IAClD,CAAC;IAED,KAAK,CAAC,CAAC,IAAI,CAAC,QAAqB,EAAE,OAA0B;QAC3D,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC;QAExC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;gBAClD,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,MAAM,EAAE,IAAI;gBACZ,WAAW,EAAE,OAAO,EAAE,WAAW,IAAI,GAAG;gBACxC,UAAU,EAAE,OAAO,EAAE,SAAS,IAAI,IAAI;gBACtC,QAAQ,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,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