MCPcopy
hub / github.com/anomalyco/opencode / loop

Function loop

packages/opencode/src/cli/cmd/run.ts:697–819  ·  view source on GitHub ↗
(client: OpencodeClient, events: Awaited<ReturnType<typeof sdk.event.subscribe>>)

Source from the content-addressed store, hash-verified

695 // rebind the SDK to the session's directory after the subscription is
696 // created, and replies issued from inside the loop must use that client.
697 async function loop(client: OpencodeClient, events: Awaited<ReturnType<typeof sdk.event.subscribe>>) {
698 const toggles = new Map<string, boolean>()
699 let error: string | undefined
700
701 for await (const event of events.stream) {
702 if (
703 event.type === "message.updated" &&
704 event.properties.sessionID === sessionID &&
705 event.properties.info.role === "assistant" &&
706 args.format !== "json" &&
707 toggles.get("start") !== true
708 ) {
709 UI.empty()
710 UI.println(`> ${event.properties.info.agent} · ${event.properties.info.modelID}`)
711 UI.empty()
712 toggles.set("start", true)
713 }
714
715 if (event.type === "message.part.updated") {
716 const part = event.properties.part
717 if (part.sessionID !== sessionID) continue
718
719 if (part.type === "tool" && (part.state.status === "completed" || part.state.status === "error")) {
720 if (emit("tool_use", { part })) continue
721 if (part.state.status === "completed") {
722 await tool(part)
723 continue
724 }
725 await toolError(part)
726 UI.error(part.state.error)
727 }
728
729 if (
730 part.type === "tool" &&
731 part.tool === "task" &&
732 part.state.status === "running" &&
733 args.format !== "json"
734 ) {
735 if (toggles.get(part.id) === true) continue
736 await tool(part)
737 toggles.set(part.id, true)
738 }
739
740 if (part.type === "step-start") {
741 if (emit("step_start", { part })) continue
742 }
743
744 if (part.type === "step-finish") {
745 if (emit("step_finish", { part })) continue
746 }
747
748 if (part.type === "text" && part.time?.end) {
749 if (emit("text", { part })) continue
750 const text = part.text.trim()
751 if (!text) continue
752 if (!process.stdout.isTTY) {
753 process.stdout.write(text + EOL)
754 continue

Callers 2

executeFunction · 0.70
prompt.tsFile · 0.50

Calls 7

writeMethod · 0.80
emitFunction · 0.70
toolFunction · 0.70
toolErrorFunction · 0.70
getMethod · 0.65
setMethod · 0.45
replyMethod · 0.45

Tested by

no test coverage detected