-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsection-read.ts
More file actions
60 lines (48 loc) · 2.3 KB
/
Copy pathsection-read.ts
File metadata and controls
60 lines (48 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { tool } from '@opencode-ai/plugin'
import type { ToolContext } from './types'
const z = tool.schema
export function createSectionReadTool(ctx: ToolContext): ReturnType<typeof tool> {
const loop = ctx.loop
return tool({
description: 'Read a section plan and its status for the active loop session. If section_index is omitted, returns the lowest-index incomplete section.',
args: {
section_index: z.number().optional().describe('Section index to read (0-based). If omitted, reads the lowest-index incomplete section.'),
},
execute: async (args, toolCtx) => {
const sessionId = toolCtx?.sessionID ?? ''
const loopName = loop.service.resolveLoopName(sessionId)
if (!loopName) {
return JSON.stringify({ error: 'Not in a loop session. This tool can only be used within an active loop session.' })
}
const state = loop.service.getAnyState(loopName)
if (!state) return JSON.stringify({ error: `Loop "${loopName}" not found.` })
if (state.totalSections === 0) {
return JSON.stringify({ error: 'No sections available for this loop.' })
}
const explicitIndex = args.section_index
const selectedSection = explicitIndex === undefined
? loop.service.getNextIncompleteSectionPlan(state)
: null
const idx = explicitIndex ?? selectedSection?.sectionIndex ?? state.currentSectionIndex
if (idx < 0 || idx >= state.totalSections) {
return JSON.stringify({ error: `Invalid section index ${idx}. Valid range: 0-${state.totalSections - 1}` })
}
const section = explicitIndex === undefined && selectedSection?.sectionIndex === idx
? selectedSection
: loop.service.getSectionPlan(state, idx)
if (!section) return JSON.stringify({ error: `Section ${idx} not found in loop "${loopName}".` })
const digest = loop.service.getCompletedSectionDigest(state)
const summary = digest.find(s => s.index === idx)
const result = {
index: idx,
title: section.title,
content: section.content,
status: section.status,
summary_done: summary?.summaryDone ?? null,
summary_deviations: summary?.summaryDeviations ?? null,
summary_follow_ups: summary?.summaryFollowUps ?? null,
}
return JSON.stringify(result)
},
})
}