MCPcopy Create free account
hub / github.com/ShipSecAI/studio / execute

Function execute

worker/src/components/ai/opencode.ts:130–354  ·  view source on GitHub ↗
({ inputs, params }, context)

Source from the content-addressed store, hash-verified

128 },
129 },
130 async execute({ inputs, params }, context) {
131 const { task, context: taskContext, model } = inputs;
132 const { systemPrompt, providerConfig } = params;
133
134 const { connectedToolNodeIds, organizationId } = context.metadata;
135
136 // 1. Resolve Gateway Token for MCP
137 let gatewayToken = '';
138 const connectedToolIds = connectedToolNodeIds ?? [];
139 if (connectedToolIds.length > 0) {
140 try {
141 gatewayToken = await getGatewaySessionToken(
142 context.runId,
143 organizationId ?? null,
144 connectedToolIds,
145 );
146 } catch (error) {
147 context.logger.error(`[OpenCode] Failed to generate gateway token: ${error}`);
148 }
149 }
150
151 // Helper to map provider to OpenCode model string format
152 const getOpenCodeModelString = (
153 model: { provider: string; modelId: string } | undefined,
154 ): string => {
155 if (!model) return 'gpt-4o';
156 // OpenCode expects models in format: provider/modelId
157 // Most providers follow this pattern
158 return `${model.provider}/${model.modelId}`;
159 };
160
161 // 2. Prepare opencode.json config
162 // Note: We use 'host' networking, so we can reach localhost
163 // Correct format from https://opencode.ai/docs/mcp-servers/
164 // CRITICAL: oauth: false is required for custom headers (OAuth is auto-detected as default in v1.0.137+)
165 // See: https://github.com/anomalyco/opencode/issues/5278
166 // Always add MCP config (even if token is empty, so OpenCode can attempt connection)
167 const mcpConfig = {
168 mcp: {
169 'shipsec-gateway': {
170 type: 'remote' as const,
171 url: DEFAULT_GATEWAY_URL,
172 oauth: false,
173 headers: gatewayToken
174 ? {
175 Authorization: `Bearer ${gatewayToken}`,
176 Accept: 'application/json, text/event-stream',
177 }
178 : {
179 Accept: 'application/json, text/event-stream',
180 },
181 enabled: true,
182 },
183 },
184 };
185
186 // Build provider config for OpenCode
187 // Z.AI requires the API key to be in provider.options.apiKey

Callers

nothing calls this directly

Calls 10

initializeMethod · 0.95
getVolumeConfigMethod · 0.95
cleanupMethod · 0.95
getGatewaySessionTokenFunction · 0.90
runComponentWithRunnerFunction · 0.90
getOpenCodeModelStringFunction · 0.85
buildProviderEnvFunction · 0.85
errorMethod · 0.80
parseMethod · 0.80
isRecordFunction · 0.70

Tested by

no test coverage detected