wave-agent-sdk 0.4.0 → 0.6.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.
- package/dist/agent.d.ts +42 -11
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +114 -115
- package/dist/constants/prompts.d.ts +18 -14
- package/dist/constants/prompts.d.ts.map +1 -1
- package/dist/constants/prompts.js +130 -54
- package/dist/constants/tools.d.ts +6 -3
- package/dist/constants/tools.d.ts.map +1 -1
- package/dist/constants/tools.js +6 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/managers/MemoryRuleManager.js +1 -1
- package/dist/managers/aiManager.d.ts +5 -3
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +57 -20
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/managers/backgroundBashManager.js +1 -0
- package/dist/managers/backgroundTaskManager.d.ts +35 -0
- package/dist/managers/backgroundTaskManager.d.ts.map +1 -0
- package/dist/managers/backgroundTaskManager.js +255 -0
- package/dist/managers/foregroundTaskManager.d.ts +9 -0
- package/dist/managers/foregroundTaskManager.d.ts.map +1 -0
- package/dist/managers/foregroundTaskManager.js +21 -0
- package/dist/managers/liveConfigManager.d.ts +1 -1
- package/dist/managers/lspManager.d.ts.map +1 -1
- package/dist/managers/lspManager.js +3 -1
- package/dist/managers/mcpManager.d.ts.map +1 -1
- package/dist/managers/messageManager.d.ts +26 -12
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +138 -64
- package/dist/managers/permissionManager.d.ts.map +1 -1
- package/dist/managers/permissionManager.js +26 -22
- package/dist/managers/planManager.d.ts +1 -1
- package/dist/managers/planManager.d.ts.map +1 -1
- package/dist/managers/planManager.js +2 -2
- package/dist/managers/pluginManager.d.ts.map +1 -1
- package/dist/managers/pluginManager.js +3 -2
- package/dist/managers/slashCommandManager.d.ts +6 -0
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +8 -2
- package/dist/managers/subagentManager.d.ts +15 -2
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +153 -39
- package/dist/managers/toolManager.d.ts +18 -1
- package/dist/managers/toolManager.d.ts.map +1 -1
- package/dist/managers/toolManager.js +29 -5
- package/dist/services/GitService.d.ts.map +1 -1
- package/dist/services/GitService.js +6 -2
- package/dist/services/MarketplaceService.d.ts +2 -2
- package/dist/services/MarketplaceService.d.ts.map +1 -1
- package/dist/services/MarketplaceService.js +18 -11
- package/dist/services/MemoryRuleService.d.ts +1 -1
- package/dist/services/MemoryRuleService.d.ts.map +1 -1
- package/dist/services/MemoryRuleService.js +13 -2
- package/dist/services/aiService.d.ts +0 -1
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +4 -140
- package/dist/services/memory.d.ts +0 -3
- package/dist/services/memory.d.ts.map +1 -1
- package/dist/services/memory.js +1 -60
- package/dist/services/session.d.ts +15 -1
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +57 -1
- package/dist/services/taskManager.d.ts +21 -0
- package/dist/services/taskManager.d.ts.map +1 -0
- package/dist/services/taskManager.js +158 -0
- package/dist/tools/askUserQuestion.d.ts.map +1 -1
- package/dist/tools/askUserQuestion.js +39 -25
- package/dist/tools/bashTool.d.ts +0 -8
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +48 -172
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +8 -6
- package/dist/tools/exitPlanMode.d.ts.map +1 -1
- package/dist/tools/exitPlanMode.js +25 -1
- package/dist/tools/globTool.d.ts.map +1 -1
- package/dist/tools/globTool.js +8 -2
- package/dist/tools/grepTool.d.ts.map +1 -1
- package/dist/tools/grepTool.js +17 -6
- package/dist/tools/lsTool.d.ts.map +1 -1
- package/dist/tools/lsTool.js +3 -1
- package/dist/tools/multiEditTool.d.ts.map +1 -1
- package/dist/tools/multiEditTool.js +7 -6
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +16 -1
- package/dist/tools/taskManagementTools.d.ts +6 -0
- package/dist/tools/taskManagementTools.d.ts.map +1 -0
- package/dist/tools/taskManagementTools.js +453 -0
- package/dist/tools/taskOutputTool.d.ts +3 -0
- package/dist/tools/taskOutputTool.d.ts.map +1 -0
- package/dist/tools/taskOutputTool.js +173 -0
- package/dist/tools/taskStopTool.d.ts +3 -0
- package/dist/tools/taskStopTool.d.ts.map +1 -0
- package/dist/tools/taskStopTool.js +71 -0
- package/dist/tools/taskTool.d.ts.map +1 -1
- package/dist/tools/taskTool.js +110 -63
- package/dist/tools/types.d.ts +12 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/writeTool.d.ts.map +1 -1
- package/dist/tools/writeTool.js +9 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/marketplace.d.ts +1 -0
- package/dist/types/marketplace.d.ts.map +1 -1
- package/dist/types/messaging.d.ts +3 -8
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/processes.d.ts +29 -4
- package/dist/types/processes.d.ts.map +1 -1
- package/dist/types/tasks.d.ts +13 -0
- package/dist/types/tasks.d.ts.map +1 -0
- package/dist/types/tasks.js +1 -0
- package/dist/types/tools.d.ts +4 -1
- package/dist/types/tools.d.ts.map +1 -1
- package/dist/utils/builtinSubagents.d.ts.map +1 -1
- package/dist/utils/builtinSubagents.js +38 -1
- package/dist/utils/cacheControlUtils.d.ts.map +1 -1
- package/dist/utils/cacheControlUtils.js +18 -12
- package/dist/utils/constants.d.ts +0 -4
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +0 -4
- package/dist/utils/convertMessagesForAPI.js +2 -2
- package/dist/utils/editUtils.d.ts +2 -11
- package/dist/utils/editUtils.d.ts.map +1 -1
- package/dist/utils/editUtils.js +52 -79
- package/dist/utils/messageOperations.d.ts +5 -36
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +9 -98
- package/dist/utils/nameGenerator.d.ts +1 -1
- package/dist/utils/nameGenerator.d.ts.map +1 -1
- package/dist/utils/nameGenerator.js +19 -3
- package/package.json +5 -5
- package/src/agent.ts +157 -134
- package/src/constants/prompts.ts +156 -65
- package/src/constants/tools.ts +6 -3
- package/src/index.ts +1 -0
- package/src/managers/MemoryRuleManager.ts +1 -1
- package/src/managers/aiManager.ts +77 -35
- package/src/managers/backgroundBashManager.ts +1 -0
- package/src/managers/backgroundTaskManager.ts +305 -0
- package/src/managers/foregroundTaskManager.ts +27 -0
- package/src/managers/lspManager.ts +3 -1
- package/src/managers/mcpManager.ts +6 -3
- package/src/managers/messageManager.ts +185 -75
- package/src/managers/permissionManager.ts +33 -28
- package/src/managers/planManager.ts +2 -2
- package/src/managers/pluginManager.ts +4 -3
- package/src/managers/slashCommandManager.ts +15 -2
- package/src/managers/subagentManager.ts +194 -35
- package/src/managers/toolManager.ts +48 -6
- package/src/services/GitService.ts +6 -2
- package/src/services/MarketplaceService.ts +30 -12
- package/src/services/MemoryRuleService.ts +18 -6
- package/src/services/aiService.ts +3 -145
- package/src/services/memory.ts +1 -73
- package/src/services/session.ts +73 -0
- package/src/services/taskManager.ts +188 -0
- package/src/tools/askUserQuestion.ts +51 -29
- package/src/tools/bashTool.ts +63 -196
- package/src/tools/editTool.ts +9 -18
- package/src/tools/exitPlanMode.ts +26 -2
- package/src/tools/globTool.ts +10 -2
- package/src/tools/grepTool.ts +17 -6
- package/src/tools/lsTool.ts +3 -1
- package/src/tools/multiEditTool.ts +7 -18
- package/src/tools/readTool.ts +17 -1
- package/src/tools/taskManagementTools.ts +498 -0
- package/src/tools/taskOutputTool.ts +196 -0
- package/src/tools/taskStopTool.ts +78 -0
- package/src/tools/taskTool.ts +136 -74
- package/src/tools/types.ts +13 -0
- package/src/tools/writeTool.ts +9 -2
- package/src/types/index.ts +1 -0
- package/src/types/marketplace.ts +1 -0
- package/src/types/messaging.ts +2 -9
- package/src/types/processes.ts +39 -4
- package/src/types/tasks.ts +13 -0
- package/src/types/tools.ts +4 -1
- package/src/utils/builtinSubagents.ts +47 -1
- package/src/utils/cacheControlUtils.ts +26 -18
- package/src/utils/constants.ts +0 -5
- package/src/utils/convertMessagesForAPI.ts +2 -2
- package/src/utils/editUtils.ts +65 -103
- package/src/utils/messageOperations.ts +12 -136
- package/src/utils/nameGenerator.ts +20 -3
- package/dist/tools/todoWriteTool.d.ts +0 -6
- package/dist/tools/todoWriteTool.d.ts.map +0 -1
- package/dist/tools/todoWriteTool.js +0 -220
- package/src/tools/todoWriteTool.ts +0 -257
|
@@ -4,33 +4,7 @@ import { transformMessagesForClaudeCache, addCacheControlToLastTool, isClaudeMod
|
|
|
4
4
|
import * as os from "os";
|
|
5
5
|
import * as fs from "fs";
|
|
6
6
|
import * as path from "path";
|
|
7
|
-
import {
|
|
8
|
-
/**
|
|
9
|
-
* Use parametersChunk as compact param for better performance
|
|
10
|
-
* Instead of parsing JSON, we use the raw chunk for efficient streaming
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* Check if a directory is a git repository
|
|
14
|
-
* @param dirPath Directory path to check
|
|
15
|
-
* @returns "Yes" if it's a git repo, "No" otherwise
|
|
16
|
-
*/
|
|
17
|
-
function isGitRepository(dirPath) {
|
|
18
|
-
try {
|
|
19
|
-
// Check if .git directory exists in current directory or any parent directory
|
|
20
|
-
let currentPath = path.resolve(dirPath);
|
|
21
|
-
while (currentPath !== path.dirname(currentPath)) {
|
|
22
|
-
const gitPath = path.join(currentPath, ".git");
|
|
23
|
-
if (fs.existsSync(gitPath)) {
|
|
24
|
-
return "Yes";
|
|
25
|
-
}
|
|
26
|
-
currentPath = path.dirname(currentPath);
|
|
27
|
-
}
|
|
28
|
-
return "No";
|
|
29
|
-
}
|
|
30
|
-
catch {
|
|
31
|
-
return "No";
|
|
32
|
-
}
|
|
33
|
-
}
|
|
7
|
+
import { COMPRESS_MESSAGES_SYSTEM_PROMPT } from "../constants/prompts.js";
|
|
34
8
|
/**
|
|
35
9
|
* Get specific configuration parameters based on model name
|
|
36
10
|
* @param modelName Model name
|
|
@@ -58,7 +32,7 @@ function getModelConfig(modelName, baseConfig = {}) {
|
|
|
58
32
|
return config;
|
|
59
33
|
}
|
|
60
34
|
export async function callAgent(options) {
|
|
61
|
-
const { gatewayConfig, modelConfig, messages, abortSignal,
|
|
35
|
+
const { gatewayConfig, modelConfig, messages, abortSignal, workdir, tools, model, systemPrompt, onContentUpdate, onToolUpdate, onReasoningUpdate, } = options;
|
|
62
36
|
// Declare variables outside try block for error handling access
|
|
63
37
|
let openaiMessages;
|
|
64
38
|
let createParams;
|
|
@@ -73,23 +47,7 @@ export async function callAgent(options) {
|
|
|
73
47
|
fetch: gatewayConfig.fetch,
|
|
74
48
|
});
|
|
75
49
|
// Build system prompt content
|
|
76
|
-
|
|
77
|
-
// Always add environment information
|
|
78
|
-
systemContent += `
|
|
79
|
-
|
|
80
|
-
Here is useful information about the environment you are running in:
|
|
81
|
-
<env>
|
|
82
|
-
Working directory: ${workdir}
|
|
83
|
-
Is directory a git repo: ${isGitRepository(workdir)}
|
|
84
|
-
Platform: ${os.platform()}
|
|
85
|
-
OS Version: ${os.type()} ${os.release()}
|
|
86
|
-
Today's date: ${new Date().toISOString().split("T")[0]}
|
|
87
|
-
</env>
|
|
88
|
-
`;
|
|
89
|
-
// If there is memory content, add it to the system prompt
|
|
90
|
-
if (memory && memory.trim()) {
|
|
91
|
-
systemContent += `\n## Memory Context\n\nThe following is important context and memory from previous interactions:\n\n${memory}`;
|
|
92
|
-
}
|
|
50
|
+
const systemContent = systemPrompt || "";
|
|
93
51
|
// Add system prompt
|
|
94
52
|
const systemMessage = {
|
|
95
53
|
role: "system",
|
|
@@ -470,101 +428,7 @@ export async function compressMessages(options) {
|
|
|
470
428
|
messages: [
|
|
471
429
|
{
|
|
472
430
|
role: "system",
|
|
473
|
-
content:
|
|
474
|
-
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
|
|
475
|
-
|
|
476
|
-
Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
|
|
477
|
-
|
|
478
|
-
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
|
|
479
|
-
- The user's explicit requests and intents
|
|
480
|
-
- Your approach to addressing the user's requests
|
|
481
|
-
- Key decisions, technical concepts and code patterns
|
|
482
|
-
- Specific details like:
|
|
483
|
-
- file names
|
|
484
|
-
- full code snippets
|
|
485
|
-
- function signatures
|
|
486
|
-
- file edits
|
|
487
|
-
- Errors that you ran into and how you fixed them
|
|
488
|
-
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
489
|
-
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
|
|
490
|
-
|
|
491
|
-
Your summary should include the following sections:
|
|
492
|
-
|
|
493
|
-
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
|
|
494
|
-
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
|
|
495
|
-
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
|
|
496
|
-
4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
497
|
-
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
|
498
|
-
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
|
|
499
|
-
6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
|
|
500
|
-
7. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
|
|
501
|
-
8. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
|
|
502
|
-
If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
|
|
503
|
-
|
|
504
|
-
Here's an example of how your output should be structured:
|
|
505
|
-
|
|
506
|
-
<example>
|
|
507
|
-
<analysis>
|
|
508
|
-
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
|
509
|
-
</analysis>
|
|
510
|
-
|
|
511
|
-
<summary>
|
|
512
|
-
1. Primary Request and Intent:
|
|
513
|
-
[Detailed description]
|
|
514
|
-
|
|
515
|
-
2. Key Technical Concepts:
|
|
516
|
-
- [Concept 1]
|
|
517
|
-
- [Concept 2]
|
|
518
|
-
- [...]
|
|
519
|
-
|
|
520
|
-
3. Files and Code Sections:
|
|
521
|
-
- [File Name 1]
|
|
522
|
-
- [Summary of why this file is important]
|
|
523
|
-
- [Summary of the changes made to this file, if any]
|
|
524
|
-
- [Important Code Snippet]
|
|
525
|
-
- [File Name 2]
|
|
526
|
-
- [Important Code Snippet]
|
|
527
|
-
- [...]
|
|
528
|
-
|
|
529
|
-
4. Errors and fixes:
|
|
530
|
-
- [Detailed description of error 1]:
|
|
531
|
-
- [How you fixed the error]
|
|
532
|
-
- [User feedback on the error if any]
|
|
533
|
-
- [...]
|
|
534
|
-
|
|
535
|
-
5. Problem Solving:
|
|
536
|
-
[Description of solved problems and ongoing troubleshooting]
|
|
537
|
-
|
|
538
|
-
6. All user messages:
|
|
539
|
-
- [Detailed non tool use user message]
|
|
540
|
-
- [...]
|
|
541
|
-
|
|
542
|
-
7. Pending Tasks:
|
|
543
|
-
- [Task 1]
|
|
544
|
-
- [Task 2]
|
|
545
|
-
- [...]
|
|
546
|
-
|
|
547
|
-
8. Current Work:
|
|
548
|
-
[Precise description of current work]
|
|
549
|
-
|
|
550
|
-
9. Optional Next Step:
|
|
551
|
-
[Optional Next step to take]
|
|
552
|
-
|
|
553
|
-
</summary>
|
|
554
|
-
</example>
|
|
555
|
-
|
|
556
|
-
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
|
|
557
|
-
|
|
558
|
-
There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
|
|
559
|
-
<example>
|
|
560
|
-
## Compact Instructions
|
|
561
|
-
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
|
|
562
|
-
</example>
|
|
563
|
-
|
|
564
|
-
<example>
|
|
565
|
-
# Summary instructions
|
|
566
|
-
When you are using compact - please focus on test output and code changes. Include file reads verbatim.
|
|
567
|
-
</example>`,
|
|
431
|
+
content: COMPRESS_MESSAGES_SYSTEM_PROMPT,
|
|
568
432
|
},
|
|
569
433
|
...messages,
|
|
570
434
|
{
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export declare const isMemoryMessage: (message: string) => boolean;
|
|
2
|
-
export declare const addMemory: (message: string, workdir: string) => Promise<void>;
|
|
3
1
|
export declare const ensureUserMemoryFile: () => Promise<void>;
|
|
4
|
-
export declare const addUserMemory: (message: string) => Promise<void>;
|
|
5
2
|
export declare const getUserMemoryContent: () => Promise<string>;
|
|
6
3
|
export declare const readMemoryFile: (workdir: string) => Promise<string>;
|
|
7
4
|
export declare const getCombinedMemoryContent: (workdir: string) => Promise<string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/services/memory.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/services/memory.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,oBAAoB,QAAa,OAAO,CAAC,IAAI,CA4BzD,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAAa,OAAO,CAAC,MAAM,CAa3D,CAAC;AAGF,eAAO,MAAM,cAAc,GAAU,SAAS,MAAM,KAAG,OAAO,CAAC,MAAM,CAqBpE,CAAC;AAGF,eAAO,MAAM,wBAAwB,GACnC,SAAS,MAAM,KACd,OAAO,CAAC,MAAM,CAoBhB,CAAC"}
|
package/dist/services/memory.js
CHANGED
|
@@ -3,46 +3,6 @@ import path from "path";
|
|
|
3
3
|
import { USER_MEMORY_FILE, DATA_DIRECTORY } from "../utils/constants.js";
|
|
4
4
|
import { logger } from "../utils/globalLogger.js";
|
|
5
5
|
// Project memory related methods
|
|
6
|
-
export const isMemoryMessage = (message) => {
|
|
7
|
-
return message.trim().startsWith("#");
|
|
8
|
-
};
|
|
9
|
-
export const addMemory = async (message, workdir) => {
|
|
10
|
-
if (!isMemoryMessage(message)) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
try {
|
|
14
|
-
const memoryFilePath = path.join(workdir, "AGENTS.md");
|
|
15
|
-
// Format memory entry, starting with -, no timestamp
|
|
16
|
-
const memoryEntry = `- ${message.substring(1).trim()}\n`;
|
|
17
|
-
// Check if file exists
|
|
18
|
-
let existingContent = "";
|
|
19
|
-
try {
|
|
20
|
-
existingContent = await fs.readFile(memoryFilePath, "utf-8");
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
// File does not exist, create new file
|
|
24
|
-
if (error.code === "ENOENT") {
|
|
25
|
-
logger.info("Memory file does not exist, will create new one", {
|
|
26
|
-
memoryFilePath,
|
|
27
|
-
});
|
|
28
|
-
existingContent =
|
|
29
|
-
"# Memory\n\nThis is the AI assistant's memory file, recording important information and context.\n\n";
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
throw error;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
// Append new memory entry to the end of the file
|
|
36
|
-
const updatedContent = existingContent + memoryEntry;
|
|
37
|
-
// Write file
|
|
38
|
-
await fs.writeFile(memoryFilePath, updatedContent, "utf-8");
|
|
39
|
-
logger.debug(`Memory added to ${memoryFilePath}:`, message);
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
logger.error("Failed to add memory:", error);
|
|
43
|
-
throw new Error(`Failed to add memory: ${error.message}`);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
6
|
// User memory related methods
|
|
47
7
|
export const ensureUserMemoryFile = async () => {
|
|
48
8
|
try {
|
|
@@ -72,25 +32,6 @@ export const ensureUserMemoryFile = async () => {
|
|
|
72
32
|
throw new Error(`Failed to ensure user memory file: ${error.message}`);
|
|
73
33
|
}
|
|
74
34
|
};
|
|
75
|
-
export const addUserMemory = async (message) => {
|
|
76
|
-
try {
|
|
77
|
-
// Ensure user memory file exists
|
|
78
|
-
await ensureUserMemoryFile();
|
|
79
|
-
// Format memory entry, starting with -
|
|
80
|
-
const memoryEntry = `- ${message.substring(1).trim()}\n`;
|
|
81
|
-
// Read existing content
|
|
82
|
-
const existingContent = await fs.readFile(USER_MEMORY_FILE, "utf-8");
|
|
83
|
-
// Append new memory entry to the end of the file
|
|
84
|
-
const updatedContent = existingContent + memoryEntry;
|
|
85
|
-
// Write file
|
|
86
|
-
await fs.writeFile(USER_MEMORY_FILE, updatedContent, "utf-8");
|
|
87
|
-
logger.debug(`User memory added to ${USER_MEMORY_FILE}:`, message);
|
|
88
|
-
}
|
|
89
|
-
catch (error) {
|
|
90
|
-
logger.error("Failed to add user memory:", error);
|
|
91
|
-
throw new Error(`Failed to add user memory: ${error.message}`);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
35
|
export const getUserMemoryContent = async () => {
|
|
95
36
|
try {
|
|
96
37
|
await ensureUserMemoryFile();
|
|
@@ -126,7 +67,7 @@ export const readMemoryFile = async (workdir) => {
|
|
|
126
67
|
return "";
|
|
127
68
|
}
|
|
128
69
|
logger.error("Failed to read memory file", { memoryFilePath, error });
|
|
129
|
-
|
|
70
|
+
return "";
|
|
130
71
|
}
|
|
131
72
|
};
|
|
132
73
|
// Get merged memory content (project memory + user memory)
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
import type { Message } from "../types/index.js";
|
|
18
18
|
export interface SessionData {
|
|
19
19
|
id: string;
|
|
20
|
+
rootSessionId?: string;
|
|
21
|
+
parentSessionId?: string;
|
|
20
22
|
messages: Message[];
|
|
21
23
|
metadata: {
|
|
22
24
|
workdir: string;
|
|
@@ -26,6 +28,8 @@ export interface SessionData {
|
|
|
26
28
|
}
|
|
27
29
|
export interface SessionMetadata {
|
|
28
30
|
id: string;
|
|
31
|
+
rootSessionId?: string;
|
|
32
|
+
parentSessionId?: string;
|
|
29
33
|
sessionType: "main" | "subagent";
|
|
30
34
|
subagentType?: string;
|
|
31
35
|
workdir: string;
|
|
@@ -86,7 +90,7 @@ export declare function createSession(sessionId: string, workdir: string, sessio
|
|
|
86
90
|
* @param workdir - Working directory for the session
|
|
87
91
|
* @param sessionType - Type of session ("main" or "subagent", defaults to "main")
|
|
88
92
|
*/
|
|
89
|
-
export declare function appendMessages(sessionId: string, newMessages: Message[], workdir: string, sessionType?: "main" | "subagent"): Promise<void>;
|
|
93
|
+
export declare function appendMessages(sessionId: string, newMessages: Message[], workdir: string, sessionType?: "main" | "subagent", rootSessionId?: string, parentSessionId?: string): Promise<void>;
|
|
90
94
|
/**
|
|
91
95
|
* Load session data from JSONL file (new approach)
|
|
92
96
|
*
|
|
@@ -171,4 +175,14 @@ export declare function truncateContent(content: string, maxLength?: number): st
|
|
|
171
175
|
* @returns Promise that resolves to session data or undefined
|
|
172
176
|
*/
|
|
173
177
|
export declare function handleSessionRestoration(restoreSessionId?: string, continueLastSession?: boolean, workdir?: string): Promise<SessionData | undefined>;
|
|
178
|
+
/**
|
|
179
|
+
* Load the full message thread by following parentSessionId links
|
|
180
|
+
* @param currentSessionId - The ID of the current session
|
|
181
|
+
* @param workdir - Working directory for the session
|
|
182
|
+
* @returns Promise that resolves to an array of all messages in the thread
|
|
183
|
+
*/
|
|
184
|
+
export declare function loadFullMessageThread(currentSessionId: string, workdir: string): Promise<{
|
|
185
|
+
messages: Message[];
|
|
186
|
+
sessionIds: string[];
|
|
187
|
+
}>;
|
|
174
188
|
//# sourceMappingURL=session.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/services/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAMH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAOjD,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,QAAQ,EAAE;QACR,OAAO,EAAE,MAAM,CAAC;QAChB,YAAY,EAAE,MAAM,CAAC;QACrB,iBAAiB,EAAE,MAAM,CAAC;KAC3B,CAAC;CACH;AAED,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,GAAG,UAAU,CAAC;IACjC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,IAAI,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CACd,MAAM,EACN,IAAI,CAAC,eAAe,EAAE,IAAI,GAAG,cAAc,CAAC,GAAG;QAAE,YAAY,EAAE,MAAM,CAAA;KAAE,CACxE,CAAC;IACF,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAE1C;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAElE;AAGD,eAAO,MAAM,WAAW,QAAuC,CAAC;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/services/session.ts"],"names":[],"mappings":"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"}
|
package/dist/services/session.js
CHANGED
|
@@ -62,6 +62,7 @@ async function updateSessionIndex(projectDirPath, metadata) {
|
|
|
62
62
|
...rest,
|
|
63
63
|
lastActiveAt: metadata.lastActiveAt.toISOString(),
|
|
64
64
|
firstMessage: metadata.firstMessage || index.sessions[id]?.firstMessage,
|
|
65
|
+
parentSessionId: metadata.parentSessionId,
|
|
65
66
|
};
|
|
66
67
|
index.lastUpdated = new Date().toISOString();
|
|
67
68
|
await fs.writeFile(indexPath, JSON.stringify(index, null, 2), "utf8");
|
|
@@ -126,7 +127,7 @@ export async function createSession(sessionId, workdir, sessionType = "main") {
|
|
|
126
127
|
* @param workdir - Working directory for the session
|
|
127
128
|
* @param sessionType - Type of session ("main" or "subagent", defaults to "main")
|
|
128
129
|
*/
|
|
129
|
-
export async function appendMessages(sessionId, newMessages, workdir, sessionType = "main") {
|
|
130
|
+
export async function appendMessages(sessionId, newMessages, workdir, sessionType = "main", rootSessionId, parentSessionId) {
|
|
130
131
|
// Do not save session files in test environment
|
|
131
132
|
if (process.env.NODE_ENV === "test") {
|
|
132
133
|
return;
|
|
@@ -174,6 +175,8 @@ export async function appendMessages(sessionId, newMessages, workdir, sessionTyp
|
|
|
174
175
|
}
|
|
175
176
|
await updateSessionIndex(projectDir.encodedPath, {
|
|
176
177
|
id: sessionId,
|
|
178
|
+
rootSessionId,
|
|
179
|
+
parentSessionId,
|
|
177
180
|
sessionType,
|
|
178
181
|
workdir,
|
|
179
182
|
lastActiveAt: new Date(lastMessage.timestamp),
|
|
@@ -202,8 +205,25 @@ export async function loadSessionFromJsonl(sessionId, workdir, sessionType = "ma
|
|
|
202
205
|
}
|
|
203
206
|
// Extract metadata from messages
|
|
204
207
|
const lastMessage = messages[messages.length - 1];
|
|
208
|
+
// Try to get rootSessionId and parentSessionId from index
|
|
209
|
+
let rootSessionId;
|
|
210
|
+
let parentSessionId;
|
|
211
|
+
try {
|
|
212
|
+
const encoder = new PathEncoder();
|
|
213
|
+
const projectDir = await encoder.getProjectDirectory(workdir, SESSION_DIR);
|
|
214
|
+
const indexPath = join(projectDir.encodedPath, SESSION_INDEX_FILENAME);
|
|
215
|
+
const indexContent = await fs.readFile(indexPath, "utf8");
|
|
216
|
+
const index = JSON.parse(indexContent);
|
|
217
|
+
rootSessionId = index.sessions[sessionId]?.rootSessionId;
|
|
218
|
+
parentSessionId = index.sessions[sessionId]?.parentSessionId;
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// Ignore index errors
|
|
222
|
+
}
|
|
205
223
|
const sessionData = {
|
|
206
224
|
id: sessionId,
|
|
225
|
+
rootSessionId: rootSessionId || sessionId,
|
|
226
|
+
parentSessionId,
|
|
207
227
|
messages: messages.map((msg) => {
|
|
208
228
|
// Remove timestamp property for backward compatibility
|
|
209
229
|
const { timestamp: _ignored, ...messageWithoutTimestamp } = msg;
|
|
@@ -644,3 +664,39 @@ export async function handleSessionRestoration(restoreSessionId, continueLastSes
|
|
|
644
664
|
process.exit(1);
|
|
645
665
|
}
|
|
646
666
|
}
|
|
667
|
+
/**
|
|
668
|
+
* Load the full message thread by following parentSessionId links
|
|
669
|
+
* @param currentSessionId - The ID of the current session
|
|
670
|
+
* @param workdir - Working directory for the session
|
|
671
|
+
* @returns Promise that resolves to an array of all messages in the thread
|
|
672
|
+
*/
|
|
673
|
+
export async function loadFullMessageThread(currentSessionId, workdir) {
|
|
674
|
+
const sessionIds = [];
|
|
675
|
+
let currentId = currentSessionId;
|
|
676
|
+
const allMessages = [];
|
|
677
|
+
while (currentId) {
|
|
678
|
+
const sessionData = await loadSessionFromJsonl(currentId, workdir);
|
|
679
|
+
if (!sessionData)
|
|
680
|
+
break;
|
|
681
|
+
sessionIds.unshift(currentId);
|
|
682
|
+
// Add messages from this session to the beginning of the list
|
|
683
|
+
// But skip the "compress" block if it's not the first session in our traversal (which is the latest)
|
|
684
|
+
// Actually, we should probably keep all messages and let the UI/logic handle it.
|
|
685
|
+
// But wait, if we are concatenating, the "compress" block in session N summarizes session N-1.
|
|
686
|
+
// So if we have session N-1 and session N, we should probably skip the compress block in session N.
|
|
687
|
+
const messages = sessionData.messages;
|
|
688
|
+
if (allMessages.length > 0) {
|
|
689
|
+
// If we already have messages (from "later" sessions),
|
|
690
|
+
// we are now adding messages from an "earlier" session.
|
|
691
|
+
// The later session's first message might be a "compress" block.
|
|
692
|
+
if (allMessages[0].blocks.some((b) => b.type === "compress")) {
|
|
693
|
+
// Remove the compress block from the later session's messages
|
|
694
|
+
// because we are now providing the actual messages it summarized.
|
|
695
|
+
allMessages.shift();
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
allMessages.unshift(...messages);
|
|
699
|
+
currentId = sessionData.parentSessionId;
|
|
700
|
+
}
|
|
701
|
+
return { messages: allMessages, sessionIds };
|
|
702
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EventEmitter } from "events";
|
|
2
|
+
import { Task } from "../types/tasks.js";
|
|
3
|
+
export declare class TaskManager extends EventEmitter {
|
|
4
|
+
private readonly baseDir;
|
|
5
|
+
private taskListId;
|
|
6
|
+
constructor(taskListId: string);
|
|
7
|
+
getTaskListId(): string;
|
|
8
|
+
setTaskListId(taskListId: string): void;
|
|
9
|
+
private getSessionDir;
|
|
10
|
+
private getTaskPath;
|
|
11
|
+
private getLockPath;
|
|
12
|
+
ensureSessionDir(): Promise<void>;
|
|
13
|
+
private withLock;
|
|
14
|
+
private validateTask;
|
|
15
|
+
createTask(task: Omit<Task, "id">): Promise<string>;
|
|
16
|
+
getTask(taskId: string): Promise<Task | null>;
|
|
17
|
+
updateTask(task: Task): Promise<void>;
|
|
18
|
+
listTasks(): Promise<Task[]>;
|
|
19
|
+
getNextTaskId(): Promise<string>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=taskManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"taskManager.d.ts","sourceRoot":"","sources":["../../src/services/taskManager.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAGzC,qBAAa,WAAY,SAAQ,YAAY;IAC3C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,UAAU,CAAS;gBAEf,UAAU,EAAE,MAAM;IAMvB,aAAa,IAAI,MAAM;IAIvB,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAI9C,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,WAAW;IAIb,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;YAIzB,QAAQ;IA2CtB,OAAO,CAAC,YAAY;IAed,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IAcnD,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;IAmB7C,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAWrC,SAAS,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;IA+B5B,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;CAavC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { promises as fs } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import { EventEmitter } from "events";
|
|
5
|
+
import { logger } from "../utils/globalLogger.js";
|
|
6
|
+
export class TaskManager extends EventEmitter {
|
|
7
|
+
constructor(taskListId) {
|
|
8
|
+
super();
|
|
9
|
+
this.taskListId = taskListId;
|
|
10
|
+
this.baseDir = join(homedir(), ".wave", "tasks");
|
|
11
|
+
}
|
|
12
|
+
getTaskListId() {
|
|
13
|
+
return this.taskListId;
|
|
14
|
+
}
|
|
15
|
+
setTaskListId(taskListId) {
|
|
16
|
+
this.taskListId = taskListId;
|
|
17
|
+
}
|
|
18
|
+
getSessionDir() {
|
|
19
|
+
return join(this.baseDir, this.taskListId);
|
|
20
|
+
}
|
|
21
|
+
getTaskPath(taskId) {
|
|
22
|
+
return join(this.getSessionDir(), `${taskId}.json`);
|
|
23
|
+
}
|
|
24
|
+
getLockPath() {
|
|
25
|
+
return join(this.getSessionDir(), `.lock`);
|
|
26
|
+
}
|
|
27
|
+
async ensureSessionDir() {
|
|
28
|
+
await fs.mkdir(this.getSessionDir(), { recursive: true });
|
|
29
|
+
}
|
|
30
|
+
async withLock(operation) {
|
|
31
|
+
const lockPath = this.getLockPath();
|
|
32
|
+
let lockHandle;
|
|
33
|
+
const maxRetries = 100;
|
|
34
|
+
const retryDelay = process.env.NODE_ENV === "test" ? 10 : 100;
|
|
35
|
+
await this.ensureSessionDir();
|
|
36
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
37
|
+
try {
|
|
38
|
+
lockHandle = await fs.open(lockPath, "wx");
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error.code === "EEXIST") {
|
|
43
|
+
if (i === maxRetries - 1) {
|
|
44
|
+
throw new Error(`Could not acquire lock for task list ${this.taskListId} after ${maxRetries} retries`);
|
|
45
|
+
}
|
|
46
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
return await operation();
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
if (lockHandle) {
|
|
57
|
+
await lockHandle.close();
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
await fs.unlink(lockPath);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
logger.error(`Failed to release lock for task list ${this.taskListId}:`, error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
validateTask(task) {
|
|
68
|
+
if (!task.id || typeof task.id !== "string")
|
|
69
|
+
throw new Error("Invalid task ID");
|
|
70
|
+
if (!task.subject || typeof task.subject !== "string")
|
|
71
|
+
throw new Error("Invalid task subject");
|
|
72
|
+
if (!task.description || typeof task.description !== "string")
|
|
73
|
+
throw new Error("Invalid task description");
|
|
74
|
+
if (!task.status ||
|
|
75
|
+
!["pending", "in_progress", "completed", "deleted"].includes(task.status)) {
|
|
76
|
+
throw new Error(`Invalid task status: ${task.status}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async createTask(task) {
|
|
80
|
+
return await this.withLock(async () => {
|
|
81
|
+
const taskId = await this.getNextTaskId();
|
|
82
|
+
const fullTask = { ...task, id: taskId };
|
|
83
|
+
this.validateTask(fullTask);
|
|
84
|
+
const taskPath = this.getTaskPath(taskId);
|
|
85
|
+
const content = JSON.stringify(fullTask, null, 2);
|
|
86
|
+
await fs.writeFile(taskPath, content, "utf8");
|
|
87
|
+
this.emit("tasksChange", this.taskListId);
|
|
88
|
+
logger.info(`Task ${taskId} created in task list ${this.taskListId}`);
|
|
89
|
+
return taskId;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
async getTask(taskId) {
|
|
93
|
+
const taskPath = this.getTaskPath(taskId);
|
|
94
|
+
try {
|
|
95
|
+
const content = await fs.readFile(taskPath, "utf8");
|
|
96
|
+
try {
|
|
97
|
+
return JSON.parse(content.trim());
|
|
98
|
+
}
|
|
99
|
+
catch (parseError) {
|
|
100
|
+
logger.error(`Failed to parse task file ${taskPath}:`, parseError);
|
|
101
|
+
logger.error(`Corrupted content: ${content}`);
|
|
102
|
+
throw parseError;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error.code === "ENOENT") {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async updateTask(task) {
|
|
113
|
+
this.validateTask(task);
|
|
114
|
+
await this.withLock(async () => {
|
|
115
|
+
const taskPath = this.getTaskPath(task.id);
|
|
116
|
+
const content = JSON.stringify(task, null, 2);
|
|
117
|
+
await fs.writeFile(taskPath, content, "utf8");
|
|
118
|
+
this.emit("tasksChange", this.taskListId);
|
|
119
|
+
logger.info(`Task ${task.id} updated in task list ${this.taskListId}`);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async listTasks() {
|
|
123
|
+
const sessionDir = this.getSessionDir();
|
|
124
|
+
try {
|
|
125
|
+
const files = await fs.readdir(sessionDir);
|
|
126
|
+
const taskFiles = files.filter((f) => f.endsWith(".json"));
|
|
127
|
+
const tasks = await Promise.all(taskFiles.map(async (file) => {
|
|
128
|
+
const taskPath = join(sessionDir, file);
|
|
129
|
+
try {
|
|
130
|
+
const content = await fs.readFile(taskPath, "utf8");
|
|
131
|
+
return JSON.parse(content.trim());
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
logger.error(`Failed to read or parse task file ${taskPath}:`, error);
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}));
|
|
138
|
+
return tasks.filter((t) => t !== null);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
if (error.code === "ENOENT") {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async getNextTaskId() {
|
|
148
|
+
const tasks = await this.listTasks();
|
|
149
|
+
if (tasks.length === 0) {
|
|
150
|
+
return "1";
|
|
151
|
+
}
|
|
152
|
+
const ids = tasks.map((t) => parseInt(t.id, 10)).filter((id) => !isNaN(id));
|
|
153
|
+
if (ids.length === 0) {
|
|
154
|
+
return (tasks.length + 1).toString();
|
|
155
|
+
}
|
|
156
|
+
return (Math.max(...ids) + 1).toString();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"askUserQuestion.d.ts","sourceRoot":"","sources":["../../src/tools/askUserQuestion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"askUserQuestion.d.ts","sourceRoot":"","sources":["../../src/tools/askUserQuestion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC,eAAO,MAAM,mBAAmB,EAAE,UAiJjC,CAAC"}
|