@code-insights/cli 3.4.1 → 3.5.1

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.
Files changed (98) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +20 -0
  3. package/dashboard-dist/assets/index-CFvScypR.css +1 -0
  4. package/dashboard-dist/assets/index-CyMrlee5.js +592 -0
  5. package/dashboard-dist/dist/assets/index-CFvScypR.css +1 -0
  6. package/dashboard-dist/dist/assets/index-CyMrlee5.js +592 -0
  7. package/dashboard-dist/dist/favicon.svg +18 -0
  8. package/dashboard-dist/dist/index.html +33 -0
  9. package/dashboard-dist/index.html +2 -2
  10. package/dist/commands/telemetry.d.ts.map +1 -1
  11. package/dist/commands/telemetry.js +14 -4
  12. package/dist/commands/telemetry.js.map +1 -1
  13. package/dist/utils/telemetry.d.ts +1 -1
  14. package/dist/utils/telemetry.d.ts.map +1 -1
  15. package/dist/utils/telemetry.js.map +1 -1
  16. package/package.json +2 -2
  17. package/server-dist/dist/index.d.ts +20 -0
  18. package/server-dist/dist/index.d.ts.map +1 -0
  19. package/server-dist/dist/index.js +109 -0
  20. package/server-dist/dist/index.js.map +1 -0
  21. package/server-dist/dist/llm/analysis.d.ts +84 -0
  22. package/server-dist/dist/llm/analysis.d.ts.map +1 -0
  23. package/server-dist/dist/llm/analysis.js +550 -0
  24. package/server-dist/dist/llm/analysis.js.map +1 -0
  25. package/server-dist/dist/llm/client.d.ts +27 -0
  26. package/server-dist/dist/llm/client.d.ts.map +1 -0
  27. package/server-dist/dist/llm/client.js +71 -0
  28. package/server-dist/dist/llm/client.js.map +1 -0
  29. package/server-dist/dist/llm/index.d.ts +7 -0
  30. package/server-dist/dist/llm/index.d.ts.map +1 -0
  31. package/server-dist/dist/llm/index.js +5 -0
  32. package/server-dist/dist/llm/index.js.map +1 -0
  33. package/server-dist/dist/llm/prompts.d.ts +109 -0
  34. package/server-dist/dist/llm/prompts.d.ts.map +1 -0
  35. package/server-dist/dist/llm/prompts.js +381 -0
  36. package/server-dist/dist/llm/prompts.js.map +1 -0
  37. package/server-dist/dist/llm/providers/anthropic.d.ts +3 -0
  38. package/server-dist/dist/llm/providers/anthropic.d.ts.map +1 -0
  39. package/server-dist/dist/llm/providers/anthropic.js +45 -0
  40. package/server-dist/dist/llm/providers/anthropic.js.map +1 -0
  41. package/server-dist/dist/llm/providers/gemini.d.ts +3 -0
  42. package/server-dist/dist/llm/providers/gemini.d.ts.map +1 -0
  43. package/server-dist/dist/llm/providers/gemini.js +51 -0
  44. package/server-dist/dist/llm/providers/gemini.js.map +1 -0
  45. package/server-dist/dist/llm/providers/ollama.d.ts +12 -0
  46. package/server-dist/dist/llm/providers/ollama.d.ts.map +1 -0
  47. package/server-dist/dist/llm/providers/ollama.js +61 -0
  48. package/server-dist/dist/llm/providers/ollama.js.map +1 -0
  49. package/server-dist/dist/llm/providers/openai.d.ts +3 -0
  50. package/server-dist/dist/llm/providers/openai.d.ts.map +1 -0
  51. package/server-dist/dist/llm/providers/openai.js +40 -0
  52. package/server-dist/dist/llm/providers/openai.js.map +1 -0
  53. package/server-dist/dist/llm/types.d.ts +22 -0
  54. package/server-dist/dist/llm/types.d.ts.map +1 -0
  55. package/server-dist/dist/llm/types.js +5 -0
  56. package/server-dist/dist/llm/types.js.map +1 -0
  57. package/server-dist/dist/routes/analysis.d.ts +4 -0
  58. package/server-dist/dist/routes/analysis.d.ts.map +1 -0
  59. package/server-dist/dist/routes/analysis.js +370 -0
  60. package/server-dist/dist/routes/analysis.js.map +1 -0
  61. package/server-dist/dist/routes/analytics.d.ts +4 -0
  62. package/server-dist/dist/routes/analytics.d.ts.map +1 -0
  63. package/server-dist/dist/routes/analytics.js +57 -0
  64. package/server-dist/dist/routes/analytics.js.map +1 -0
  65. package/server-dist/dist/routes/config.d.ts +4 -0
  66. package/server-dist/dist/routes/config.d.ts.map +1 -0
  67. package/server-dist/dist/routes/config.js +108 -0
  68. package/server-dist/dist/routes/config.js.map +1 -0
  69. package/server-dist/dist/routes/export.d.ts +4 -0
  70. package/server-dist/dist/routes/export.d.ts.map +1 -0
  71. package/server-dist/dist/routes/export.js +52 -0
  72. package/server-dist/dist/routes/export.js.map +1 -0
  73. package/server-dist/dist/routes/insights.d.ts +4 -0
  74. package/server-dist/dist/routes/insights.d.ts.map +1 -0
  75. package/server-dist/dist/routes/insights.js +80 -0
  76. package/server-dist/dist/routes/insights.js.map +1 -0
  77. package/server-dist/dist/routes/messages.d.ts +4 -0
  78. package/server-dist/dist/routes/messages.d.ts.map +1 -0
  79. package/server-dist/dist/routes/messages.js +19 -0
  80. package/server-dist/dist/routes/messages.js.map +1 -0
  81. package/server-dist/dist/routes/projects.d.ts +4 -0
  82. package/server-dist/dist/routes/projects.d.ts.map +1 -0
  83. package/server-dist/dist/routes/projects.js +32 -0
  84. package/server-dist/dist/routes/projects.js.map +1 -0
  85. package/server-dist/dist/routes/sessions.d.ts +4 -0
  86. package/server-dist/dist/routes/sessions.d.ts.map +1 -0
  87. package/server-dist/dist/routes/sessions.js +65 -0
  88. package/server-dist/dist/routes/sessions.js.map +1 -0
  89. package/server-dist/dist/routes/telemetry.d.ts +4 -0
  90. package/server-dist/dist/routes/telemetry.d.ts.map +1 -0
  91. package/server-dist/dist/routes/telemetry.js +21 -0
  92. package/server-dist/dist/routes/telemetry.js.map +1 -0
  93. package/server-dist/dist/utils.d.ts +6 -0
  94. package/server-dist/dist/utils.d.ts.map +1 -0
  95. package/server-dist/dist/utils.js +9 -0
  96. package/server-dist/dist/utils.js.map +1 -0
  97. package/dashboard-dist/assets/index-BdoBoNtI.js +0 -567
  98. package/dashboard-dist/assets/index-QzYeMRSf.css +0 -1
@@ -0,0 +1,71 @@
1
+ // LLM client factory — server-side.
2
+ // Config is loaded from ~/.code-insights/config.json via the CLI config system.
3
+ // No localStorage or browser APIs used here.
4
+ import { loadConfig } from '@code-insights/cli/utils/config';
5
+ import { createOpenAIClient } from './providers/openai.js';
6
+ import { createAnthropicClient } from './providers/anthropic.js';
7
+ import { createGeminiClient } from './providers/gemini.js';
8
+ import { createOllamaClient } from './providers/ollama.js';
9
+ /**
10
+ * Load LLM config from the CLI config file.
11
+ */
12
+ export function loadLLMConfig() {
13
+ const config = loadConfig();
14
+ return config?.dashboard?.llm ?? null;
15
+ }
16
+ /**
17
+ * Check if LLM is configured.
18
+ */
19
+ export function isLLMConfigured() {
20
+ const llm = loadLLMConfig();
21
+ if (!llm)
22
+ return false;
23
+ if (llm.provider === 'ollama')
24
+ return !!llm.model;
25
+ return !!llm.apiKey && !!llm.model;
26
+ }
27
+ /**
28
+ * Create an LLM client from the current config.
29
+ * Throws if LLM is not configured.
30
+ */
31
+ export function createLLMClient() {
32
+ const config = loadLLMConfig();
33
+ if (!config) {
34
+ throw new Error('LLM not configured. Run `code-insights config llm` to configure a provider.');
35
+ }
36
+ return createClientFromConfig(config);
37
+ }
38
+ /**
39
+ * Create an LLM client from a specific config object (used for testing).
40
+ */
41
+ export function createClientFromConfig(config) {
42
+ switch (config.provider) {
43
+ case 'openai':
44
+ return createOpenAIClient(config.apiKey ?? '', config.model);
45
+ case 'anthropic':
46
+ return createAnthropicClient(config.apiKey ?? '', config.model);
47
+ case 'gemini':
48
+ return createGeminiClient(config.apiKey ?? '', config.model);
49
+ case 'ollama':
50
+ return createOllamaClient(config.model, config.baseUrl);
51
+ default:
52
+ throw new Error(`Unknown LLM provider: ${config.provider}`);
53
+ }
54
+ }
55
+ /**
56
+ * Test LLM connectivity with the given config.
57
+ */
58
+ export async function testLLMConfig(config) {
59
+ try {
60
+ const client = createClientFromConfig(config);
61
+ await client.chat([{ role: 'user', content: 'Say "ok" and nothing else.' }]);
62
+ return { success: true };
63
+ }
64
+ catch (error) {
65
+ return {
66
+ success: false,
67
+ error: error instanceof Error ? error.message : 'Unknown error',
68
+ };
69
+ }
70
+ }
71
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/llm/client.ts"],"names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;QAC/D,KAAK,WAAW;YACd,OAAO,qBAAqB,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QAClE,KAAK,QAAQ;YACX,OAAO,kBAAkB,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QAC/D,KAAK,QAAQ;YACX,OAAO,kBAAkB,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1D;YACE,MAAM,IAAI,KAAK,CAAC,yBAAyB,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IAChE,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,MAAyB;IAC3D,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,sBAAsB,CAAC,MAAM,CAAC,CAAC;QAC9C,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,4BAA4B,EAAE,CAAC,CAAC,CAAC;QAC7E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe;SAChE,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ export type { LLMClient, LLMMessage, LLMResponse, ChatOptions } from './types.js';
2
+ export type { LLMProvider, LLMProviderConfig } from './types.js';
3
+ export { createLLMClient, createClientFromConfig, loadLLMConfig, isLLMConfigured, testLLMConfig } from './client.js';
4
+ export { analyzeSession, analyzePromptQuality, findRecurringInsights } from './analysis.js';
5
+ export type { AnalysisResult, InsightRow, SessionData, RecurringInsightResult } from './analysis.js';
6
+ export { discoverOllamaModels } from './providers/ollama.js';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAEA,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAClF,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACrH,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAC5F,YAAY,EAAE,cAAc,EAAE,UAAU,EAAE,WAAW,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACrG,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,5 @@
1
+ // Public API for the server-side LLM engine.
2
+ export { createLLMClient, createClientFromConfig, loadLLMConfig, isLLMConfigured, testLLMConfig } from './client.js';
3
+ export { analyzeSession, analyzePromptQuality, findRecurringInsights } from './analysis.js';
4
+ export { discoverOllamaModels } from './providers/ollama.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAI7C,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACrH,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAE5F,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,109 @@
1
+ import type { SessionCharacter } from '@code-insights/cli/types';
2
+ export interface SQLiteMessageRow {
3
+ id: string;
4
+ session_id: string;
5
+ type: 'user' | 'assistant' | 'system';
6
+ content: string;
7
+ thinking: string | null;
8
+ tool_calls: string;
9
+ tool_results: string;
10
+ usage: string | null;
11
+ timestamp: string;
12
+ parent_id: string | null;
13
+ }
14
+ /**
15
+ * Format SQLite message rows for LLM consumption.
16
+ * Handles snake_case fields and JSON-encoded tool_calls/tool_results.
17
+ */
18
+ export declare function formatMessagesForAnalysis(messages: SQLiteMessageRow[]): string;
19
+ /**
20
+ * System prompt for session analysis.
21
+ */
22
+ export declare const SESSION_ANALYSIS_SYSTEM_PROMPT = "You are a senior staff engineer writing entries for a team's engineering knowledge base. You've just observed an AI-assisted coding session and your job is to extract the insights that would save another engineer time if they encountered a similar situation 6 months from now.\n\nYour audience is a developer who has never seen this session but works on the same codebase. They need enough context to understand WHY a decision was made, WHAT specific gotcha was discovered, and WHEN this knowledge applies.\n\nYou will extract:\n1. **Summary**: A narrative of what was accomplished and the outcome\n2. **Decisions**: Technical choices made \u2014 with full situation context, reasoning, rejected alternatives, trade-offs, and conditions for revisiting (max 3)\n3. **Learnings**: Technical discoveries, gotchas, debugging breakthroughs \u2014 with the observable symptom, root cause, and a transferable takeaway (max 5)\n4. **Session Character**: Classify the session into exactly one of these types based on its overall nature:\n - deep_focus: Long, concentrated work on a specific problem or area (50+ messages, deep into one topic)\n - bug_hunt: Debugging-driven \u2014 investigating errors, tracing issues, fixing bugs\n - feature_build: Building new functionality \u2014 creating files, adding endpoints, wiring components\n - exploration: Research-oriented \u2014 reading code, searching, understanding before acting\n - refactor: Restructuring existing code \u2014 renaming, moving, reorganizing without new features\n - learning: Knowledge-seeking \u2014 asking questions, understanding concepts, getting explanations\n - quick_task: Short and focused \u2014 small fix, config change, or one-off task (<10 messages)\n\nQuality Standards:\n- Only include insights you would write in a team knowledge base for future reference\n- Each insight MUST reference concrete details: specific file names, library names, error messages, API endpoints, or code patterns\n- Do not invent file names, APIs, errors, or details not present in the conversation\n- Rate your confidence in each insight's value (0-100). Only include insights you rate 70+.\n- It is better to return 0 insights in a category than to include generic or trivial ones\n- If a session is straightforward with no notable decisions or learnings, say so in the summary and leave other categories empty\n\nLength Guidance:\n- Fill every field in the schema. An empty \"trade_offs\" or \"revisit_when\" is worse than a longer response.\n- Total response: stay under 2000 tokens. If you must cut, drop lower-confidence insights rather than compressing high-confidence ones.\n- Evidence: 1-3 short quotes per insight, referencing turn labels.\n- Prefer precision over brevity \u2014 a specific 3-sentence insight beats a vague 1-sentence insight.\n\nDO NOT include insights like these (too generic/trivial):\n- \"Used debugging techniques to fix an issue\"\n- \"Made architectural decisions about the codebase\"\n- \"Implemented a new feature\" (the summary already covers this)\n- \"Used React hooks for state management\" (too generic without specifics)\n- \"Fixed a bug in the code\" (what bug? what was the root cause?)\n- Anything that restates the task without adding transferable knowledge\n\nHere are examples of EXCELLENT insights \u2014 this is the quality bar:\n\nEXCELLENT decision:\n{\n \"title\": \"Use better-sqlite3 instead of sql.js for local database\",\n \"situation\": \"Needed a SQLite driver for a Node.js CLI that stores session data locally. Single-user, read-heavy from dashboard, occasional writes during sync.\",\n \"choice\": \"better-sqlite3 \u2014 synchronous C++ binding with native SQLite access, no async overhead.\",\n \"reasoning\": \"CLI runs locally with no concurrent users. Synchronous API eliminates callback complexity. WAL mode provides concurrent read access for the dashboard while CLI writes.\",\n \"alternatives\": [\n {\"option\": \"sql.js (WASM build)\", \"rejected_because\": \"3x slower for bulk inserts, entire DB in memory, no WAL support\"},\n {\"option\": \"PostgreSQL via Docker\", \"rejected_because\": \"Violates local-first constraint \u2014 requires running a server process\"}\n ],\n \"trade_offs\": \"Requires native compilation (node-gyp) which can fail on some systems. No browser compatibility.\",\n \"revisit_when\": \"If multi-device sync is added or users report node-gyp build failures.\",\n \"confidence\": 92,\n \"evidence\": [\"User#3: 'We need something that works without a server'\", \"Assistant#4: 'better-sqlite3 with WAL mode gives concurrent reads...'\"]\n}\n\nEXCELLENT learning:\n{\n \"title\": \"Tailwind v4 requires @theme inline{} for CSS variable utilities\",\n \"symptom\": \"After Tailwind v3\u2192v4 upgrade, custom utilities like bg-primary stopped working. Classes present in HTML but no styles applied.\",\n \"root_cause\": \"Tailwind v4 removed tailwind.config.js theme extension. CSS variables in :root are not automatically available as utilities \u2014 must be registered via @theme inline {} in the CSS file.\",\n \"takeaway\": \"When migrating Tailwind v3\u2192v4 with shadcn/ui: add @theme inline {} mapping CSS variables, add @custom-variant dark for class-based dark mode, replace tailwindcss-animate with tw-animate-css.\",\n \"applies_when\": \"Any Tailwind v3\u2192v4 migration using CSS variables for theming, especially with shadcn/ui.\",\n \"confidence\": 95,\n \"evidence\": [\"User#12: 'The colors are all gone after the upgrade'\", \"Assistant#13: 'Tailwind v4 requires explicit @theme inline registration...'\"]\n}\n\nRespond with valid JSON only, wrapped in <json>...</json> tags. Do not include any other text.";
23
+ /**
24
+ * Generate the user prompt for session analysis.
25
+ */
26
+ export declare function generateSessionAnalysisPrompt(projectName: string, sessionSummary: string | null, formattedMessages: string): string;
27
+ export interface AnalysisResponse {
28
+ session_character?: SessionCharacter;
29
+ summary: {
30
+ title: string;
31
+ content: string;
32
+ outcome?: 'success' | 'partial' | 'abandoned' | 'blocked';
33
+ bullets: string[];
34
+ };
35
+ decisions: Array<{
36
+ title: string;
37
+ situation?: string;
38
+ choice?: string;
39
+ reasoning: string;
40
+ alternatives?: Array<{
41
+ option: string;
42
+ rejected_because: string;
43
+ }>;
44
+ trade_offs?: string;
45
+ revisit_when?: string;
46
+ confidence?: number;
47
+ evidence?: string[];
48
+ }>;
49
+ learnings: Array<{
50
+ title: string;
51
+ symptom?: string;
52
+ root_cause?: string;
53
+ takeaway?: string;
54
+ applies_when?: string;
55
+ confidence?: number;
56
+ evidence?: string[];
57
+ }>;
58
+ }
59
+ export interface ParseError {
60
+ error_type: 'json_parse_error' | 'no_json_found' | 'invalid_structure';
61
+ error_message: string;
62
+ response_length: number;
63
+ response_preview: string;
64
+ }
65
+ export type ParseResult<T> = {
66
+ success: true;
67
+ data: T;
68
+ } | {
69
+ success: false;
70
+ error: ParseError;
71
+ };
72
+ /**
73
+ * Parse the LLM response into structured insights.
74
+ */
75
+ export declare function parseAnalysisResponse(response: string): ParseResult<AnalysisResponse>;
76
+ export declare const PROMPT_QUALITY_SYSTEM_PROMPT = "You are a prompt engineering coach helping developers communicate more effectively with AI coding assistants. You review conversations and identify specific moments where better prompting would have saved time.\n\nYou will identify:\n1. **Wasted turns**: User messages that led to clarifications, corrections, or repeated instructions because the original prompt was unclear, missing context, or too vague.\n2. **Anti-patterns**: Recurring bad habits in the user's prompting style, with specific fixes.\n3. **Session traits**: Higher-level behavioral patterns about how the session was structured and managed.\n4. **Efficiency score**: A 0-100 rating of how optimally the user communicated.\n5. **Actionable tips**: Specific improvements the user can make.\n\nBefore evaluating, mentally walk through the conversation and identify:\n1. Each time the assistant asked for clarification that could have been avoided\n2. Each time the user corrected the assistant's interpretation\n3. Each time the user repeated an instruction they gave earlier\n4. Whether the session covers too many unrelated objectives (context drift / session bloat)\n5. Whether the user provided critical context or requirements late that should have been mentioned upfront\n6. Whether the user discussed the plan/approach before jumping into implementation, or dove straight into code\nThese are your candidate findings. Only include them if they are genuinely actionable.\n\nGuidelines:\n- Focus on USER messages only \u2014 don't critique the assistant's responses\n- A \"wasted turn\" is when the user had to send a follow-up message to clarify, correct, or repeat something that could have been included in the original prompt\n- Only mark a wasted turn if the assistant explicitly asked for clarification or corrected a misunderstanding\n- Be constructive, not judgmental \u2014 the goal is to help users improve\n- Consider the context: some clarification exchanges are normal and expected\n- A score of 100 means every user message was perfectly clear and complete\n- A score of 50 means about half the messages could have been more efficient\n\nLength Guidance:\n- Max 5 wasted turns, max 3 anti-patterns, max 3 session traits, max 5 tips\n- suggestedRewrite must be a complete, usable prompt \u2014 not vague meta-advice\n- overallAssessment: 2-3 sentences\n- Total response: stay under 2000 tokens\n\nRespond with valid JSON only, wrapped in <json>...</json> tags. Do not include any other text.";
77
+ export declare function generatePromptQualityPrompt(projectName: string, formattedMessages: string, messageCount: number): string;
78
+ export interface WastedTurn {
79
+ messageIndex: number;
80
+ originalMessage?: string;
81
+ whatWentWrong?: string;
82
+ suggestedRewrite: string;
83
+ turnsWasted?: number;
84
+ }
85
+ export interface AntiPattern {
86
+ name: string;
87
+ description?: string;
88
+ count: number;
89
+ examples: string[];
90
+ fix?: string;
91
+ }
92
+ export interface SessionTrait {
93
+ trait: 'context_drift' | 'objective_bloat' | 'late_context' | 'no_planning' | 'good_structure';
94
+ severity: 'high' | 'medium' | 'low';
95
+ description: string;
96
+ evidence?: string;
97
+ suggestion?: string;
98
+ }
99
+ export interface PromptQualityResponse {
100
+ efficiencyScore: number;
101
+ potentialMessageReduction: number;
102
+ overallAssessment: string;
103
+ wastedTurns: WastedTurn[];
104
+ antiPatterns: AntiPattern[];
105
+ sessionTraits: SessionTrait[];
106
+ tips: string[];
107
+ }
108
+ export declare function parsePromptQualityResponse(response: string): ParseResult<PromptQualityResponse>;
109
+ //# sourceMappingURL=prompts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompts.d.ts","sourceRoot":"","sources":["../../src/llm/prompts.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAIjE,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,QAAQ,CAAC;IACtC,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAUD;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,QAAQ,EAAE,gBAAgB,EAAE,GAAG,MAAM,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,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,qBAAqB;IACpC,eAAe,EAAE,MAAM,CAAC;IACxB,yBAAyB,EAAE,MAAM,CAAC;IAClC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B,YAAY,EAAE,WAAW,EAAE,CAAC;IAC5B,aAAa,EAAE,YAAY,EAAE,CAAC;IAC9B,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB;AAED,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,GAAG,WAAW,CAAC,qBAAqB,CAAC,CA+C/F"}
@@ -0,0 +1,381 @@
1
+ // Analysis prompts and response parsers for LLM session analysis.
2
+ // Ported from web repo (src/lib/llm/prompts.ts) with SQLite-aware message formatting.
3
+ import { jsonrepair } from 'jsonrepair';
4
+ /**
5
+ * Format SQLite message rows for LLM consumption.
6
+ * Handles snake_case fields and JSON-encoded tool_calls/tool_results.
7
+ */
8
+ export function formatMessagesForAnalysis(messages) {
9
+ let userIndex = 0;
10
+ let assistantIndex = 0;
11
+ return messages
12
+ .map((m) => {
13
+ const role = m.type === 'user' ? 'User' : m.type === 'assistant' ? 'Assistant' : 'System';
14
+ const roleLabel = role === 'User'
15
+ ? `User#${userIndex++}`
16
+ : role === 'Assistant'
17
+ ? `Assistant#${assistantIndex++}`
18
+ : 'System';
19
+ // Parse JSON-encoded tool_calls
20
+ let toolCalls = [];
21
+ try {
22
+ toolCalls = m.tool_calls ? JSON.parse(m.tool_calls) : [];
23
+ }
24
+ catch {
25
+ toolCalls = [];
26
+ }
27
+ // Parse JSON-encoded tool_results
28
+ let toolResults = [];
29
+ try {
30
+ toolResults = m.tool_results ? JSON.parse(m.tool_results) : [];
31
+ }
32
+ catch {
33
+ toolResults = [];
34
+ }
35
+ const toolInfo = toolCalls.length > 0
36
+ ? `\n[Tools used: ${toolCalls.map(t => t.name || 'unknown').join(', ')}]`
37
+ : '';
38
+ // Include thinking content — capped at 1000 chars to stay within token budget
39
+ const thinkingInfo = m.thinking
40
+ ? `\n[Thinking: ${m.thinking.slice(0, 1000)}]`
41
+ : '';
42
+ // Include tool results for context — 500 chars per result (error messages need ~300-400 chars)
43
+ const resultInfo = toolResults.length > 0
44
+ ? `\n[Tool results: ${toolResults.map(r => (r.output || '').slice(0, 500)).join(' | ')}]`
45
+ : '';
46
+ return `### ${roleLabel}:\n${m.content}${thinkingInfo}${toolInfo}${resultInfo}`;
47
+ })
48
+ .join('\n\n');
49
+ }
50
+ /**
51
+ * System prompt for session analysis.
52
+ */
53
+ export const SESSION_ANALYSIS_SYSTEM_PROMPT = `You are a senior staff engineer writing entries for a team's engineering knowledge base. You've just observed an AI-assisted coding session and your job is to extract the insights that would save another engineer time if they encountered a similar situation 6 months from now.
54
+
55
+ Your audience is a developer who has never seen this session but works on the same codebase. They need enough context to understand WHY a decision was made, WHAT specific gotcha was discovered, and WHEN this knowledge applies.
56
+
57
+ You will extract:
58
+ 1. **Summary**: A narrative of what was accomplished and the outcome
59
+ 2. **Decisions**: Technical choices made — with full situation context, reasoning, rejected alternatives, trade-offs, and conditions for revisiting (max 3)
60
+ 3. **Learnings**: Technical discoveries, gotchas, debugging breakthroughs — with the observable symptom, root cause, and a transferable takeaway (max 5)
61
+ 4. **Session Character**: Classify the session into exactly one of these types based on its overall nature:
62
+ - deep_focus: Long, concentrated work on a specific problem or area (50+ messages, deep into one topic)
63
+ - bug_hunt: Debugging-driven — investigating errors, tracing issues, fixing bugs
64
+ - feature_build: Building new functionality — creating files, adding endpoints, wiring components
65
+ - exploration: Research-oriented — reading code, searching, understanding before acting
66
+ - refactor: Restructuring existing code — renaming, moving, reorganizing without new features
67
+ - learning: Knowledge-seeking — asking questions, understanding concepts, getting explanations
68
+ - quick_task: Short and focused — small fix, config change, or one-off task (<10 messages)
69
+
70
+ Quality Standards:
71
+ - Only include insights you would write in a team knowledge base for future reference
72
+ - Each insight MUST reference concrete details: specific file names, library names, error messages, API endpoints, or code patterns
73
+ - Do not invent file names, APIs, errors, or details not present in the conversation
74
+ - Rate your confidence in each insight's value (0-100). Only include insights you rate 70+.
75
+ - It is better to return 0 insights in a category than to include generic or trivial ones
76
+ - If a session is straightforward with no notable decisions or learnings, say so in the summary and leave other categories empty
77
+
78
+ Length Guidance:
79
+ - Fill every field in the schema. An empty "trade_offs" or "revisit_when" is worse than a longer response.
80
+ - Total response: stay under 2000 tokens. If you must cut, drop lower-confidence insights rather than compressing high-confidence ones.
81
+ - Evidence: 1-3 short quotes per insight, referencing turn labels.
82
+ - Prefer precision over brevity — a specific 3-sentence insight beats a vague 1-sentence insight.
83
+
84
+ DO NOT include insights like these (too generic/trivial):
85
+ - "Used debugging techniques to fix an issue"
86
+ - "Made architectural decisions about the codebase"
87
+ - "Implemented a new feature" (the summary already covers this)
88
+ - "Used React hooks for state management" (too generic without specifics)
89
+ - "Fixed a bug in the code" (what bug? what was the root cause?)
90
+ - Anything that restates the task without adding transferable knowledge
91
+
92
+ Here are examples of EXCELLENT insights — this is the quality bar:
93
+
94
+ EXCELLENT decision:
95
+ {
96
+ "title": "Use better-sqlite3 instead of sql.js for local database",
97
+ "situation": "Needed a SQLite driver for a Node.js CLI that stores session data locally. Single-user, read-heavy from dashboard, occasional writes during sync.",
98
+ "choice": "better-sqlite3 — synchronous C++ binding with native SQLite access, no async overhead.",
99
+ "reasoning": "CLI runs locally with no concurrent users. Synchronous API eliminates callback complexity. WAL mode provides concurrent read access for the dashboard while CLI writes.",
100
+ "alternatives": [
101
+ {"option": "sql.js (WASM build)", "rejected_because": "3x slower for bulk inserts, entire DB in memory, no WAL support"},
102
+ {"option": "PostgreSQL via Docker", "rejected_because": "Violates local-first constraint — requires running a server process"}
103
+ ],
104
+ "trade_offs": "Requires native compilation (node-gyp) which can fail on some systems. No browser compatibility.",
105
+ "revisit_when": "If multi-device sync is added or users report node-gyp build failures.",
106
+ "confidence": 92,
107
+ "evidence": ["User#3: 'We need something that works without a server'", "Assistant#4: 'better-sqlite3 with WAL mode gives concurrent reads...'"]
108
+ }
109
+
110
+ EXCELLENT learning:
111
+ {
112
+ "title": "Tailwind v4 requires @theme inline{} for CSS variable utilities",
113
+ "symptom": "After Tailwind v3→v4 upgrade, custom utilities like bg-primary stopped working. Classes present in HTML but no styles applied.",
114
+ "root_cause": "Tailwind v4 removed tailwind.config.js theme extension. CSS variables in :root are not automatically available as utilities — must be registered via @theme inline {} in the CSS file.",
115
+ "takeaway": "When migrating Tailwind v3→v4 with shadcn/ui: add @theme inline {} mapping CSS variables, add @custom-variant dark for class-based dark mode, replace tailwindcss-animate with tw-animate-css.",
116
+ "applies_when": "Any Tailwind v3→v4 migration using CSS variables for theming, especially with shadcn/ui.",
117
+ "confidence": 95,
118
+ "evidence": ["User#12: 'The colors are all gone after the upgrade'", "Assistant#13: 'Tailwind v4 requires explicit @theme inline registration...'"]
119
+ }
120
+
121
+ Respond with valid JSON only, wrapped in <json>...</json> tags. Do not include any other text.`;
122
+ /**
123
+ * Generate the user prompt for session analysis.
124
+ */
125
+ export function generateSessionAnalysisPrompt(projectName, sessionSummary, formattedMessages) {
126
+ return `Analyze this AI coding session and extract insights.
127
+
128
+ Project: ${projectName}
129
+ ${sessionSummary ? `Session Summary: ${sessionSummary}\n` : ''}
130
+ --- CONVERSATION ---
131
+ ${formattedMessages}
132
+ --- END CONVERSATION ---
133
+
134
+ Extract insights in this JSON format:
135
+ {
136
+ "session_character": "deep_focus | bug_hunt | feature_build | exploration | refactor | learning | quick_task",
137
+ "summary": {
138
+ "title": "Brief title describing main accomplishment (max 80 chars)",
139
+ "content": "2-4 sentence narrative: what was the goal, what was done, what was the outcome. Mention the primary file or component changed.",
140
+ "outcome": "success | partial | abandoned | blocked",
141
+ "bullets": ["Each bullet names a specific artifact (file, function, endpoint) and what changed"]
142
+ },
143
+ "decisions": [
144
+ {
145
+ "title": "The specific technical choice made (max 80 chars)",
146
+ "situation": "What problem or requirement led to this decision point",
147
+ "choice": "What was chosen and how it was implemented",
148
+ "reasoning": "Why this choice was made — the key factors that tipped the decision",
149
+ "alternatives": [
150
+ {"option": "Name of alternative", "rejected_because": "Why it was not chosen"}
151
+ ],
152
+ "trade_offs": "What downsides were accepted, what was given up",
153
+ "revisit_when": "Under what conditions this decision should be reconsidered (or 'N/A' if permanent)",
154
+ "confidence": 85,
155
+ "evidence": ["User#4: quoted text...", "Assistant#5: quoted text..."]
156
+ }
157
+ ],
158
+ "learnings": [
159
+ {
160
+ "title": "Specific technical discovery or gotcha (max 80 chars)",
161
+ "symptom": "What went wrong or was confusing — the observable behavior that triggered investigation",
162
+ "root_cause": "The underlying technical reason — why it happened",
163
+ "takeaway": "The transferable lesson — what to do or avoid in similar situations, useful outside this project",
164
+ "applies_when": "Conditions under which this knowledge is relevant (framework version, configuration, etc.)",
165
+ "confidence": 80,
166
+ "evidence": ["User#7: quoted text...", "Assistant#8: quoted text..."]
167
+ }
168
+ ]
169
+ }
170
+
171
+ Only include insights rated 70+ confidence. If you cannot cite evidence, drop the insight. Return empty arrays for categories with no strong insights. Max 3 decisions, 5 learnings.
172
+ Evidence should reference the labeled turns in the conversation (e.g., "User#2", "Assistant#5").
173
+
174
+ Respond with valid JSON only, wrapped in <json>...</json> tags. Do not include any other text.`;
175
+ }
176
+ const VALID_SESSION_CHARACTERS = new Set([
177
+ 'deep_focus', 'bug_hunt', 'feature_build', 'exploration', 'refactor', 'learning', 'quick_task',
178
+ ]);
179
+ function buildResponsePreview(text, head = 200, tail = 200) {
180
+ if (text.length <= head + tail + 20)
181
+ return text;
182
+ return `${text.slice(0, head)}\n...[${text.length - head - tail} chars omitted]...\n${text.slice(-tail)}`;
183
+ }
184
+ function extractJsonPayload(response) {
185
+ const tagged = response.match(/<json>\s*([\s\S]*?)\s*<\/json>/i);
186
+ if (tagged?.[1])
187
+ return tagged[1].trim();
188
+ const jsonMatch = response.match(/\{[\s\S]*\}/);
189
+ return jsonMatch ? jsonMatch[0] : null;
190
+ }
191
+ /**
192
+ * Parse the LLM response into structured insights.
193
+ */
194
+ export function parseAnalysisResponse(response) {
195
+ const response_length = response.length;
196
+ const preview = buildResponsePreview(response);
197
+ const jsonPayload = extractJsonPayload(response);
198
+ if (!jsonPayload) {
199
+ console.error('No JSON found in analysis response');
200
+ return {
201
+ success: false,
202
+ error: { error_type: 'no_json_found', error_message: 'No JSON found in analysis response', response_length, response_preview: preview },
203
+ };
204
+ }
205
+ let parsed;
206
+ try {
207
+ parsed = JSON.parse(jsonPayload);
208
+ }
209
+ catch {
210
+ // Attempt repair — handles trailing commas, unclosed braces, truncated output
211
+ try {
212
+ parsed = JSON.parse(jsonrepair(jsonPayload));
213
+ }
214
+ catch (err) {
215
+ const msg = err instanceof Error ? err.message : String(err);
216
+ console.error('Failed to parse analysis response (after jsonrepair):', err);
217
+ return {
218
+ success: false,
219
+ error: { error_type: 'json_parse_error', error_message: msg, response_length, response_preview: preview },
220
+ };
221
+ }
222
+ }
223
+ if (!parsed.summary || typeof parsed.summary.title !== 'string') {
224
+ console.error('Invalid analysis response structure');
225
+ return {
226
+ success: false,
227
+ error: { error_type: 'invalid_structure', error_message: 'Missing or invalid summary field', response_length, response_preview: preview },
228
+ };
229
+ }
230
+ parsed.decisions = parsed.decisions || [];
231
+ parsed.learnings = parsed.learnings || [];
232
+ // Validate session_character — drop if not a recognized value
233
+ if (parsed.session_character && !VALID_SESSION_CHARACTERS.has(parsed.session_character)) {
234
+ parsed.session_character = undefined;
235
+ }
236
+ return { success: true, data: parsed };
237
+ }
238
+ // --- Prompt Quality Analysis ---
239
+ export const PROMPT_QUALITY_SYSTEM_PROMPT = `You are a prompt engineering coach helping developers communicate more effectively with AI coding assistants. You review conversations and identify specific moments where better prompting would have saved time.
240
+
241
+ You will identify:
242
+ 1. **Wasted turns**: User messages that led to clarifications, corrections, or repeated instructions because the original prompt was unclear, missing context, or too vague.
243
+ 2. **Anti-patterns**: Recurring bad habits in the user's prompting style, with specific fixes.
244
+ 3. **Session traits**: Higher-level behavioral patterns about how the session was structured and managed.
245
+ 4. **Efficiency score**: A 0-100 rating of how optimally the user communicated.
246
+ 5. **Actionable tips**: Specific improvements the user can make.
247
+
248
+ Before evaluating, mentally walk through the conversation and identify:
249
+ 1. Each time the assistant asked for clarification that could have been avoided
250
+ 2. Each time the user corrected the assistant's interpretation
251
+ 3. Each time the user repeated an instruction they gave earlier
252
+ 4. Whether the session covers too many unrelated objectives (context drift / session bloat)
253
+ 5. Whether the user provided critical context or requirements late that should have been mentioned upfront
254
+ 6. Whether the user discussed the plan/approach before jumping into implementation, or dove straight into code
255
+ These are your candidate findings. Only include them if they are genuinely actionable.
256
+
257
+ Guidelines:
258
+ - Focus on USER messages only — don't critique the assistant's responses
259
+ - A "wasted turn" is when the user had to send a follow-up message to clarify, correct, or repeat something that could have been included in the original prompt
260
+ - Only mark a wasted turn if the assistant explicitly asked for clarification or corrected a misunderstanding
261
+ - Be constructive, not judgmental — the goal is to help users improve
262
+ - Consider the context: some clarification exchanges are normal and expected
263
+ - A score of 100 means every user message was perfectly clear and complete
264
+ - A score of 50 means about half the messages could have been more efficient
265
+
266
+ Length Guidance:
267
+ - Max 5 wasted turns, max 3 anti-patterns, max 3 session traits, max 5 tips
268
+ - suggestedRewrite must be a complete, usable prompt — not vague meta-advice
269
+ - overallAssessment: 2-3 sentences
270
+ - Total response: stay under 2000 tokens
271
+
272
+ Respond with valid JSON only, wrapped in <json>...</json> tags. Do not include any other text.`;
273
+ export function generatePromptQualityPrompt(projectName, formattedMessages, messageCount) {
274
+ return `Analyze the user's prompting efficiency in this AI coding session.
275
+
276
+ Project: ${projectName}
277
+ Total messages: ${messageCount}
278
+
279
+ --- CONVERSATION ---
280
+ ${formattedMessages}
281
+ --- END CONVERSATION ---
282
+
283
+ Evaluate the user's prompting quality and respond with this JSON format:
284
+ {
285
+ "efficiencyScore": 75,
286
+ "potentialMessageReduction": 3,
287
+ "overallAssessment": "2-3 sentence summary of the user's prompting style and efficiency",
288
+ "wastedTurns": [
289
+ {
290
+ "messageIndex": 5,
291
+ "originalMessage": "The user's original message (abbreviated if long)",
292
+ "whatWentWrong": "What information was missing or ambiguous that caused a follow-up",
293
+ "suggestedRewrite": "A concrete rewrite that includes the missing context — must be a complete, usable prompt",
294
+ "turnsWasted": 2
295
+ }
296
+ ],
297
+ "antiPatterns": [
298
+ {
299
+ "name": "Vague Instructions",
300
+ "description": "Requests that lack specificity about what file, function, or behavior to change",
301
+ "count": 3,
302
+ "examples": ["User#2: 'fix it'", "User#5: 'make it work'"],
303
+ "fix": "Include the file path, function name, and expected vs actual behavior in the initial request"
304
+ }
305
+ ],
306
+ "sessionTraits": [
307
+ {
308
+ "trait": "context_drift | objective_bloat | late_context | no_planning | good_structure",
309
+ "severity": "high | medium | low",
310
+ "description": "What was observed and why it matters",
311
+ "evidence": "User#3 switched from auth to styling, then back to auth at User#12",
312
+ "suggestion": "Break into separate sessions: one for auth, one for styling"
313
+ }
314
+ ],
315
+ "tips": [
316
+ "Always include file paths when asking to modify code",
317
+ "Provide error messages verbatim when reporting bugs"
318
+ ]
319
+ }
320
+
321
+ Session trait definitions:
322
+ - **context_drift**: Session covers too many unrelated objectives, causing the AI to lose context and produce lower quality output
323
+ - **objective_bloat**: Too many different tasks crammed into one session instead of focused, single-purpose sessions
324
+ - **late_context**: Critical requirements, constraints, or context provided late in the conversation that should have been mentioned upfront — causing rework or wasted turns
325
+ - **no_planning**: User jumped straight into implementation without discussing approach, requirements, or plan — leading to course corrections mid-session
326
+ - **good_structure**: Session was well-structured with clear objectives, upfront context, and logical flow (only include this if truly exemplary)
327
+
328
+ Rules:
329
+ - messageIndex refers to the 0-based index of the USER message, as labeled in the conversation (e.g., User#0)
330
+ - Only include genuinely wasted turns, not normal back-and-forth
331
+ - Tips should be specific and actionable, not generic; include the relevant user message index in parentheses
332
+ - If the user prompted well, say so — don't manufacture issues
333
+ - potentialMessageReduction is how many fewer messages the session could have taken with better prompts
334
+
335
+ Respond with valid JSON only, wrapped in <json>...</json> tags. Do not include any other text.`;
336
+ }
337
+ export function parsePromptQualityResponse(response) {
338
+ const response_length = response.length;
339
+ const preview = buildResponsePreview(response);
340
+ const jsonPayload = extractJsonPayload(response);
341
+ if (!jsonPayload) {
342
+ console.error('No JSON found in prompt quality response');
343
+ return {
344
+ success: false,
345
+ error: { error_type: 'no_json_found', error_message: 'No JSON found in prompt quality response', response_length, response_preview: preview },
346
+ };
347
+ }
348
+ let parsed;
349
+ try {
350
+ parsed = JSON.parse(jsonPayload);
351
+ }
352
+ catch {
353
+ try {
354
+ parsed = JSON.parse(jsonrepair(jsonPayload));
355
+ }
356
+ catch (err) {
357
+ const msg = err instanceof Error ? err.message : String(err);
358
+ console.error('Failed to parse prompt quality response (after jsonrepair):', err);
359
+ return {
360
+ success: false,
361
+ error: { error_type: 'json_parse_error', error_message: msg, response_length, response_preview: preview },
362
+ };
363
+ }
364
+ }
365
+ if (typeof parsed.efficiencyScore !== 'number') {
366
+ console.error('Invalid prompt quality response: missing efficiencyScore');
367
+ return {
368
+ success: false,
369
+ error: { error_type: 'invalid_structure', error_message: 'Missing or invalid efficiencyScore field', response_length, response_preview: preview },
370
+ };
371
+ }
372
+ parsed.efficiencyScore = Math.max(0, Math.min(100, Math.round(parsed.efficiencyScore)));
373
+ parsed.potentialMessageReduction = parsed.potentialMessageReduction || 0;
374
+ parsed.overallAssessment = parsed.overallAssessment || '';
375
+ parsed.wastedTurns = parsed.wastedTurns || [];
376
+ parsed.antiPatterns = parsed.antiPatterns || [];
377
+ parsed.sessionTraits = parsed.sessionTraits || [];
378
+ parsed.tips = parsed.tips || [];
379
+ return { success: true, data: parsed };
380
+ }
381
+ //# sourceMappingURL=prompts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompts.js","sourceRoot":"","sources":["../../src/llm/prompts.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,sFAAsF;AAEtF,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AA0BxC;;;GAGG;AACH,MAAM,UAAU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,GAAG,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;IAEhC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AACzC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { LLMClient } from '../types.js';
2
+ export declare function createAnthropicClient(apiKey: string, model: string): LLMClient;
3
+ //# sourceMappingURL=anthropic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/llm/providers/anthropic.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAwC,MAAM,aAAa,CAAC;AAEnF,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,CAiD9E"}