Skip to content

Commit 5944766

Browse files
Refactor export functionality to use prompt ID instead of session ID; update related logging and help text
1 parent 1ad1bef commit 5944766

6 files changed

Lines changed: 76 additions & 72 deletions

File tree

src/commands/export.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,44 @@ import {ensureDir} from "jsr:@std/fs"
99
export async function handleExport(args: string[]) {
1010
const db = new PromptDatabase()
1111
const commandFlags = {
12-
string: ["session"],
12+
string: ["id"],
1313
boolean: ["help"],
14+
default: {help: false},
1415
}
1516

1617
const flags = parseArgs(args, commandFlags)
1718

1819
if (flags.help) {
1920
console.log(parseFlagForHelp(commandFlags))
2021
return
22+
} else if (flags._[1] === "help") {
23+
console.log(parseFlagForHelp(commandFlags))
24+
return
2125
}
2226

23-
if (!flags.session) {
24-
console.error("Session ID is required. Use --session <id>")
27+
if (!flags.id) {
28+
console.error("Prompt ID is required. Use --id <number>")
2529
return
2630
}
2731

2832
try {
29-
const markdown = await db.exportSessionToMarkdown(flags.session)
33+
const id = parseInt(flags.id)
34+
const markdown = await db.exportSessionToMarkdown(id)
3035

3136
// Create exports directory
3237
const exportDir = join(Deno.cwd(), "exports")
3338
await ensureDir(exportDir)
3439

3540
// Generate filename with timestamp
36-
const filename = `session_${flags.session}_${format(new Date(), "yyyyMMdd_HHmmss")}.md`
41+
const filename = `prompt_${flags.id}_${format(new Date(), "yyyyMMdd_HHmmss")}.md`
3742
const filepath = join(exportDir, filename)
3843

3944
// Write markdown to file
4045
await Deno.writeTextFile(filepath, markdown)
4146

42-
console.log(`Session log exported to: ${filepath}`)
47+
console.log(`Prompt log exported to: ${filepath}`)
4348
} catch (error) {
44-
log.error(`Error exporting session: ${error}`)
45-
console.error(`Failed to export session: ${error}`)
49+
log.error(`Error exporting prompt: ${error}`)
50+
console.error(`Failed to export prompt: ${error}`)
4651
}
4752
}

src/config/constants.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ Please ensure all commands are compatible with this environment.
2828

2929
export const PLANNER_SYSTEM_PROMPT = `
3030
You prepare plans for an agentic system.
31-
You will receive the user request, current system information and available tools in their respective tags.
31+
You will receive the user request, current system information in their respective tags.
3232
Use this information to create a step by step plan for the agent to follow.
3333
Evaluate the user's request, if this is a simple request like a greeting, requesting information or advice which can be done without any tools simply respond with 'Reply to the user.'.
34-
However for anything more complex. Reflect on system information and available tools to create a step by step plan that agent can follow.
35-
take a note that the agent will always have access to running shell commands and file system operations,
36-
with that in mind suggest any additional tools which are optimal for the task.
34+
However for anything more complex. Reflect on system information to create a step by step plan that agent can follow.
3735
You should respond with detailed JSON plan having the following shape:
38-
[{step:1,action:'Action to be taken to achieve the goal', tools:['tool1','tool2']},"..."]
36+
[{step:1,action:'Action to be taken to achieve the goal'},"..."]
3937
ONLY Respond in JSON without any codefences or any other details
4038
`
4139
export const COMBINED_SYSTEM_PROMPT = `

src/modules/db/database.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ export class PromptDatabase {
5757
cost REAL,
5858
session_id TEXT
5959
);
60-
61-
ALTER TABLE prompts ADD COLUMN session_id TEXT DEFAULT NULL;
62-
6360
CREATE TABLE IF NOT EXISTS session_logs (
6461
id INTEGER PRIMARY KEY AUTOINCREMENT,
6562
session_id TEXT NOT NULL,
@@ -193,35 +190,39 @@ export class PromptDatabase {
193190
}))
194191
}
195192

196-
async exportSessionToMarkdown(sessionId: string): Promise<string> {
197-
const session = await this.getPromptBySessionId(sessionId)
198-
if (!session) {
199-
throw new Error(`Session ${sessionId} not found`)
193+
async exportSessionToMarkdown(promptId: number): Promise<string> {
194+
const prompt = this.getPromptById(promptId)
195+
if (!prompt) {
196+
throw new Error(`Prompt ${promptId} not found`)
200197
}
201198

202-
const logs = await this.getSessionLogs(sessionId)
199+
const logs = prompt.session_id ?
200+
await this.getSessionLogs(prompt.session_id) :
201+
[]
203202

204-
let markdown = `# Session Log: ${session.id}\n\n`
205-
markdown += `- **Timestamp**: ${session.timestamp}\n`
206-
markdown += `- **Mode**: ${session.mode}\n`
207-
markdown += `- **Total Tokens**: ${session.tokens_used}\n`
208-
markdown += `- **Total Cost**: $${session.cost.toFixed(6)}\n\n`
203+
let markdown = `# Prompt Log: ${prompt.id}\n\n`
204+
markdown += `- **Timestamp**: ${prompt.timestamp}\n`
205+
markdown += `- **Mode**: ${prompt.mode}\n`
206+
markdown += `- **Total Tokens**: ${prompt.tokens_used}\n`
207+
markdown += `- **Total Cost**: $${prompt.cost.toFixed(6)}\n\n`
209208

210-
markdown += `## Original Prompt\n\n\`\`\`\n${session.prompt}\n\`\`\`\n\n`
209+
markdown += `## Original Prompt\n\n\`\`\`\n${prompt.prompt}\n\`\`\`\n\n`
211210

212-
markdown += `## Execution Steps\n\n`
211+
if (prompt.session_id) {
212+
markdown += `## Execution Steps\n\n`
213213

214-
for (const log of logs) {
215-
markdown += `### Step ${log.step_number}: ${log.step_description}\n\n`
216-
markdown += `- **Time**: ${log.timestamp}\n`
217-
markdown += `- **Tools Used**: ${log.tools_used}\n\n`
214+
for (const log of logs) {
215+
markdown += `### Step ${log.step_number}: ${log.step_description}\n\n`
216+
markdown += `- **Time**: ${log.timestamp}\n`
217+
markdown += `- **Tools Used**: ${log.tools_used}\n\n`
218218

219-
if (log.result) {
220-
markdown += `**Result**:\n\`\`\`\n${log.result}\n\`\`\`\n\n`
221-
}
219+
if (log.result) {
220+
markdown += `**Result**:\n\`\`\`\n${log.result}\n\`\`\`\n\n`
221+
}
222222

223-
if (log.error) {
224-
markdown += `**Error**:\n\`\`\`\n${log.error}\n\`\`\`\n\n`
223+
if (log.error) {
224+
markdown += `**Error**:\n\`\`\`\n${log.error}\n\`\`\`\n\n`
225+
}
225226
}
226227
}
227228

src/modules/hybrid/hybrid_session.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export class HybridSession extends BaseSession {
3939
content: [{type: "text", text: step.action}],
4040
}
4141
this.messages.push(message)
42-
const tools = this.toolHandler.getAllTools().filter(tool => step.tools.includes(tool.name))
43-
42+
const tools = this.toolHandler.getAllTools()
4443
let stepResult = ""
4544
let stepError = ''
4645

@@ -105,7 +104,7 @@ export class HybridSession extends BaseSession {
105104
session_id: this.sessionId,
106105
step_number: step.step,
107106
step_description: step.action,
108-
tools_used: step.tools,
107+
tools_used: tools.map((tool) => tool.name),
109108
result: stepResult,
110109
error: stepError
111110
})

src/types/interfaces.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ export interface ToolConfig {
6969
}
7070

7171
export interface PlanStep {
72-
step: number;
73-
action: string;
74-
tools: string[];
72+
step: number
73+
action: string
7574
}
7675

7776
export interface Plan {
78-
planSteps: PlanStep[];
77+
planSteps: PlanStep[]
7978
}

src/utils/functions.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export function parseFlagForHelp(options: ParseOptions): string {
1313
" history Show command history",
1414
" settings Manage settings",
1515
" help Show this help message",
16+
" edit Open configuration files in editor",
17+
" export Export session logs",
1618
"",
1719
"Options:"
1820
)
@@ -72,34 +74,34 @@ export function parseFlagForHelp(options: ParseOptions): string {
7274
}
7375

7476
export async function getCommandHelp(command: string): Promise<{description: string, helpText: string}> {
75-
try {
76-
// Try --help first
77-
const helpProcess = new Deno.Command(command, { args: ["--help"] });
78-
const helpResult = await helpProcess.output();
79-
80-
if (helpResult.success) {
81-
const helpText = new TextDecoder().decode(helpResult.stdout);
82-
// Extract first line or paragraph as description
83-
const description = helpText.split('\n')[0].trim();
84-
return { description, helpText };
85-
}
77+
try {
78+
// Try --help first
79+
const helpProcess = new Deno.Command(command, {args: ["--help"]})
80+
const helpResult = await helpProcess.output()
8681

87-
// Try 'help' subcommand if --help fails
88-
const altHelpProcess = new Deno.Command(command, { args: ["help"] });
89-
const altHelpResult = await altHelpProcess.output();
90-
91-
if (altHelpResult.success) {
92-
const helpText = new TextDecoder().decode(altHelpResult.stdout);
93-
const description = helpText.split('\n')[0].trim();
94-
return { description, helpText };
95-
}
82+
if (helpResult.success) {
83+
const helpText = new TextDecoder().decode(helpResult.stdout)
84+
// Extract first line or paragraph as description
85+
const description = helpText.split('\n')[0].trim()
86+
return {description, helpText}
87+
}
88+
89+
// Try 'help' subcommand if --help fails
90+
const altHelpProcess = new Deno.Command(command, {args: ["help"]})
91+
const altHelpResult = await altHelpProcess.output()
9692

97-
throw new Error("Could not fetch help information");
98-
} catch (error) {
99-
log.error(`Failed to get help for command ${command}: ${error}`);
100-
return {
101-
description: "No description available",
102-
helpText: "Help information could not be retrieved"
103-
};
104-
}
93+
if (altHelpResult.success) {
94+
const helpText = new TextDecoder().decode(altHelpResult.stdout)
95+
const description = helpText.split('\n')[0].trim()
96+
return {description, helpText}
97+
}
98+
99+
throw new Error("Could not fetch help information")
100+
} catch (error) {
101+
log.error(`Failed to get help for command ${command}: ${error}`)
102+
return {
103+
description: "No description available",
104+
helpText: "Help information could not be retrieved"
105+
}
106+
}
105107
}

0 commit comments

Comments
 (0)