-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
78 lines (67 loc) · 3.22 KB
/
Copy pathindex.mjs
File metadata and controls
78 lines (67 loc) · 3.22 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// dsh-opencode-usage — host half.
//
// Mounts the OpenCode Go usage surfaces: the /api/dsh-opencode-usage route
// family (usage proxy + config), the opencode_go_usage agent tool, and a
// system-prompt announcement. The browser half (./client.js) renders the
// composer-dock badge and the expandable usage card. Everything rides the
// official DSH SDK packages — no dsh source changes.
/** Stable cordis plugin name. */
export const name = 'opencode-usage'
/** Services required before the surfaces can mount. */
export const inject = ['webServer', 'tools', 'systemPrompt']
import { UsageConfigStore, API_KEY_ENV, DEFAULT_BASE_URL } from './lib/config.mjs'
import { fetchUsage } from './lib/usage.mjs'
import { makeRoutes } from './lib/routes.mjs'
import { opencodeUsageTool } from './lib/tools.mjs'
/** Order of the announcement section within the tool-guidance band. */
const SECTION_ORDER = 160
/** Model-facing announcement: plugin presence, capability, and limits. */
export const USAGE_GUIDANCE =
'本机已安装 dsh-opencode-usage 插件(OpenCode Go 套餐用量显示):输入框下方常驻徽章显示滚动/每周/每月用量百分比与重置倒计时;' +
'Agent 可用 opencode_go_usage 工具查询余额。API key 默认取环境变量 OPENCODE_GO_API_KEY(与 DSH 的 opencode-go 提供商配置一致),' +
'或在用量卡片「配置」中覆盖,存 ~/.dsh/dsh-opencode-usage.json(权限 0600)。' +
'用量接口 GET {baseUrl}/v1/usage 为非官方文档化接口(cc-switch 社区发现),响应结构可能随 opencode 变动;' +
'percent 为已用百分比,resetsAt 为重置时刻。用户提到「余额 / 用量 / opencode-go 套餐 / quota」时即指本插件,请据此协作。'
/**
* Mount the routes, tool, and announcement.
* @param ctx - host plugin context carrying webServer/tools/systemPrompt.
* @param config - plugin config (announceToAgent / enabled, optional).
*/
export function apply(ctx, config = {}) {
const enabled = config.enabled ?? true
const announceToAgent = config.announceToAgent ?? true
const store = new UsageConfigStore()
const fetchWithConfig = (cfg) => fetchUsage(cfg)
// ----------------------------------------------------------- prompt
let disposeSection
if (enabled && announceToAgent) {
disposeSection = ctx.systemPrompt.section({
name: 'plugin:dsh-opencode-usage',
order: SECTION_ORDER,
text: USAGE_GUIDANCE,
})
}
ctx.effect(() => () => {
disposeSection?.()
}, 'dsh-opencode-usage: announcement')
// ---------------------------------------------------------- routes
const routes = makeRoutes({
store,
fetch: fetchWithConfig,
})
const disposers = routes.map((route) => ctx.webServer.register(route))
ctx.effect(() => () => {
for (const dispose of disposers) dispose()
}, 'dsh-opencode-usage: routes')
// ------------------------------------------------------------ tool
const tool = opencodeUsageTool({
fetch: fetchWithConfig,
getConfig: () => store.get(),
})
const disposeTool = ctx.tools.register(tool)
ctx.effect(() => () => {
disposeTool()
}, 'dsh-opencode-usage: tool')
}
export { UsageConfigStore, API_KEY_ENV, DEFAULT_BASE_URL }
export default { name, inject, apply }