|
1 | 1 | import type { Argv } from "yargs" |
2 | | -import { App } from "../../app/app" |
3 | 2 | import { Bus } from "../../bus" |
4 | 3 | import { Provider } from "../../provider/provider" |
5 | 4 | import { Session } from "../../session" |
6 | | -import { Share } from "../../share/share" |
7 | 5 | import { Message } from "../../session/message" |
8 | 6 | import { UI } from "../ui" |
9 | 7 | import { cmd } from "./cmd" |
10 | 8 | import { Flag } from "../../flag/flag" |
11 | 9 | import { Config } from "../../config/config" |
| 10 | +import { bootstrap } from "../bootstrap" |
12 | 11 |
|
13 | 12 | const TOOL: Record<string, [string, string]> = { |
14 | 13 | todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD], |
@@ -56,118 +55,109 @@ export const RunCommand = cmd({ |
56 | 55 | }, |
57 | 56 | handler: async (args) => { |
58 | 57 | const message = args.message.join(" ") |
59 | | - await App.provide( |
60 | | - { |
61 | | - cwd: process.cwd(), |
62 | | - }, |
63 | | - async () => { |
64 | | - await Share.init() |
65 | | - const session = await (async () => { |
66 | | - if (args.continue) { |
67 | | - const first = await Session.list().next() |
68 | | - if (first.done) return |
69 | | - return first.value |
70 | | - } |
71 | | - |
72 | | - if (args.session) return Session.get(args.session) |
| 58 | + await bootstrap({ cwd: process.cwd() }, async () => { |
| 59 | + const session = await (async () => { |
| 60 | + if (args.continue) { |
| 61 | + const first = await Session.list().next() |
| 62 | + if (first.done) return |
| 63 | + return first.value |
| 64 | + } |
73 | 65 |
|
74 | | - return Session.create() |
75 | | - })() |
| 66 | + if (args.session) return Session.get(args.session) |
76 | 67 |
|
77 | | - if (!session) { |
78 | | - UI.error("Session not found") |
79 | | - return |
80 | | - } |
| 68 | + return Session.create() |
| 69 | + })() |
81 | 70 |
|
82 | | - const isPiped = !process.stdout.isTTY |
| 71 | + if (!session) { |
| 72 | + UI.error("Session not found") |
| 73 | + return |
| 74 | + } |
83 | 75 |
|
84 | | - UI.empty() |
85 | | - UI.println(UI.logo()) |
86 | | - UI.empty() |
87 | | - UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message) |
88 | | - UI.empty() |
| 76 | + const isPiped = !process.stdout.isTTY |
89 | 77 |
|
90 | | - const cfg = await Config.get() |
91 | | - if (cfg.autoshare || Flag.OPENCODE_AUTO_SHARE || args.share) { |
92 | | - await Session.share(session.id) |
93 | | - UI.println( |
94 | | - UI.Style.TEXT_INFO_BOLD + |
95 | | - "~ https://opencoder.aryalabs.ai/s/" + |
96 | | - session.id.slice(-8), |
97 | | - ) |
98 | | - } |
99 | | - UI.empty() |
| 78 | + UI.empty() |
| 79 | + UI.println(UI.logo()) |
| 80 | + UI.empty() |
| 81 | + UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message) |
| 82 | + UI.empty() |
100 | 83 |
|
101 | | - const { providerID, modelID } = args.model |
102 | | - ? Provider.parseModel(args.model) |
103 | | - : await Provider.defaultModel() |
| 84 | + const cfg = await Config.get() |
| 85 | + if (cfg.autoshare || Flag.OPENCODE_AUTO_SHARE || args.share) { |
| 86 | + await Session.share(session.id) |
104 | 87 | UI.println( |
105 | | - UI.Style.TEXT_NORMAL_BOLD + "@ ", |
106 | | - UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`, |
| 88 | + UI.Style.TEXT_INFO_BOLD + |
| 89 | + "~ https://opencoder.aryalabs.ai/s/" + |
| 90 | + session.id.slice(-8), |
107 | 91 | ) |
108 | | - UI.empty() |
| 92 | + } |
| 93 | + UI.empty() |
109 | 94 |
|
110 | | - function printEvent(color: string, type: string, title: string) { |
111 | | - UI.println( |
112 | | - color + `|`, |
113 | | - UI.Style.TEXT_NORMAL + |
114 | | - UI.Style.TEXT_DIM + |
115 | | - ` ${type.padEnd(7, " ")}`, |
116 | | - "", |
117 | | - UI.Style.TEXT_NORMAL + title, |
118 | | - ) |
119 | | - } |
| 95 | + const { providerID, modelID } = args.model |
| 96 | + ? Provider.parseModel(args.model) |
| 97 | + : await Provider.defaultModel() |
| 98 | + UI.println( |
| 99 | + UI.Style.TEXT_NORMAL_BOLD + "@ ", |
| 100 | + UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`, |
| 101 | + ) |
| 102 | + UI.empty() |
| 103 | + |
| 104 | + function printEvent(color: string, type: string, title: string) { |
| 105 | + UI.println( |
| 106 | + color + `|`, |
| 107 | + UI.Style.TEXT_NORMAL + UI.Style.TEXT_DIM + ` ${type.padEnd(7, " ")}`, |
| 108 | + "", |
| 109 | + UI.Style.TEXT_NORMAL + title, |
| 110 | + ) |
| 111 | + } |
120 | 112 |
|
121 | | - Bus.subscribe(Message.Event.PartUpdated, async (evt) => { |
122 | | - if (evt.properties.sessionID !== session.id) return |
123 | | - const part = evt.properties.part |
124 | | - const message = await Session.getMessage( |
125 | | - evt.properties.sessionID, |
126 | | - evt.properties.messageID, |
127 | | - ) |
| 113 | + Bus.subscribe(Message.Event.PartUpdated, async (evt) => { |
| 114 | + if (evt.properties.sessionID !== session.id) return |
| 115 | + const part = evt.properties.part |
| 116 | + const message = await Session.getMessage( |
| 117 | + evt.properties.sessionID, |
| 118 | + evt.properties.messageID, |
| 119 | + ) |
128 | 120 |
|
129 | | - if ( |
130 | | - part.type === "tool-invocation" && |
131 | | - part.toolInvocation.state === "result" |
132 | | - ) { |
133 | | - const metadata = |
134 | | - message.metadata.tool[part.toolInvocation.toolCallId] |
135 | | - const [tool, color] = TOOL[part.toolInvocation.toolName] ?? [ |
136 | | - part.toolInvocation.toolName, |
137 | | - UI.Style.TEXT_INFO_BOLD, |
138 | | - ] |
139 | | - printEvent(color, tool, metadata?.title || "Unknown") |
140 | | - } |
| 121 | + if ( |
| 122 | + part.type === "tool-invocation" && |
| 123 | + part.toolInvocation.state === "result" |
| 124 | + ) { |
| 125 | + const metadata = message.metadata.tool[part.toolInvocation.toolCallId] |
| 126 | + const [tool, color] = TOOL[part.toolInvocation.toolName] ?? [ |
| 127 | + part.toolInvocation.toolName, |
| 128 | + UI.Style.TEXT_INFO_BOLD, |
| 129 | + ] |
| 130 | + printEvent(color, tool, metadata?.title || "Unknown") |
| 131 | + } |
141 | 132 |
|
142 | | - if (part.type === "text") { |
143 | | - if (part.text.includes("\n")) { |
144 | | - UI.empty() |
145 | | - UI.println(part.text) |
146 | | - UI.empty() |
147 | | - return |
148 | | - } |
149 | | - printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text) |
| 133 | + if (part.type === "text") { |
| 134 | + if (part.text.includes("\n")) { |
| 135 | + UI.empty() |
| 136 | + UI.println(part.text) |
| 137 | + UI.empty() |
| 138 | + return |
150 | 139 | } |
151 | | - }) |
| 140 | + printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text) |
| 141 | + } |
| 142 | + }) |
152 | 143 |
|
153 | | - const result = await Session.chat({ |
154 | | - sessionID: session.id, |
155 | | - providerID, |
156 | | - modelID, |
157 | | - parts: [ |
158 | | - { |
159 | | - type: "text", |
160 | | - text: message, |
161 | | - }, |
162 | | - ], |
163 | | - }) |
| 144 | + const result = await Session.chat({ |
| 145 | + sessionID: session.id, |
| 146 | + providerID, |
| 147 | + modelID, |
| 148 | + parts: [ |
| 149 | + { |
| 150 | + type: "text", |
| 151 | + text: message, |
| 152 | + }, |
| 153 | + ], |
| 154 | + }) |
164 | 155 |
|
165 | | - if (isPiped) { |
166 | | - const match = result.parts.findLast((x) => x.type === "text") |
167 | | - if (match) process.stdout.write(match.text) |
168 | | - } |
169 | | - UI.empty() |
170 | | - }, |
171 | | - ) |
| 156 | + if (isPiped) { |
| 157 | + const match = result.parts.findLast((x) => x.type === "text") |
| 158 | + if (match) process.stdout.write(match.text) |
| 159 | + } |
| 160 | + UI.empty() |
| 161 | + }) |
172 | 162 | }, |
173 | 163 | }) |
0 commit comments