forked from QwenLM/qwen-code-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
308 lines (278 loc) · 11.3 KB
/
Copy pathaction.yml
File metadata and controls
308 lines (278 loc) · 11.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright 2025 Alibaba Cloud
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: 'Run Qwen Code'
author: 'Alibaba Cloud'
description: |-
Invoke the Qwen Code from a GitHub Action.
inputs:
openai_api_key:
description: 'The API key for Qwen Code API (DashScope compatible).'
required: true
openai_base_url:
description: 'The base URL for Qwen Code API endpoint.'
required: false
default: 'https://dashscope.aliyuncs.com/compatible-mode/v1'
openai_model:
description: 'The Qwen Code model to use.'
required: false
default: 'qwen-coder-plus-latest'
qwen_cli_version:
description: 'The version of the Qwen Code to install. Can be "latest", "preview", "nightly", a specific version number, or a git branch, tag, or commit.'
required: false
default: 'latest'
qwen_debug:
description: 'Enable debug logging and output streaming.'
required: false
prompt:
description: |-
A string passed to the Qwen Code's `--prompt` argument.
required: false
default: 'You are a helpful assistant.'
settings:
description: |-
A JSON string written to `.qwen/settings.json` to configure the CLI's _project_ settings.
required: false
extensions:
description: 'A list of Qwen Code extensions to install.'
required: false
upload_artifacts:
description: 'Whether to upload artifacts to the github action.'
required: false
default: 'false'
use_pnpm:
description: 'Whether or not to use pnpm instead of npm to install qwen-code'
required: false
default: 'false'
workflow_name:
description: 'The GitHub workflow name, used for telemetry purposes.'
required: false
default: '${{ github.workflow }}'
outputs:
summary:
description: 'The summarized output from the Qwen Code execution.'
value: '${{ steps.qwen_run.outputs.qwen_response }}'
error:
description: 'The error output from the Qwen Code execution, if any.'
value: '${{ steps.qwen_run.outputs.qwen_errors }}'
runs:
using: 'composite'
steps:
- name: 'Validate Inputs'
id: 'validate_inputs'
shell: 'bash'
run: |-
set -exuo pipefail
# Emit a clear warning in three places without failing the step
warn() {
local msg="$1"
echo "WARNING: ${msg}" >&2
echo "::warning title=Input validation::${msg}"
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
{
echo "### Input validation warnings"
echo
echo "- ${msg}"
} >> "${GITHUB_STEP_SUMMARY}"
fi
}
# Validate that API key is provided
if [[ "${INPUT_OPENAI_API_KEY_PRESENT:-false}" != "true" ]]; then
warn "No API key provided. Please provide 'openai_api_key'."
fi
env:
INPUT_OPENAI_API_KEY_PRESENT: "${{ inputs.openai_api_key != '' }}"
- name: 'Sanitize workflow name'
id: 'sanitize_workflow_name'
shell: 'bash'
run: |
SANITIZED=$(echo "${WORKFLOW_NAME}" | sed 's/[^ a-zA-Z0-9-]//g' | xargs | tr ' ' '_' | tr '[:upper:]' '[:lower:]')
echo "gh_workflow_name=$SANITIZED" >> $GITHUB_OUTPUT
env:
WORKFLOW_NAME: '${{ inputs.workflow_name }}'
- name: 'Configure Qwen Code'
if: |-
${{ inputs.settings != '' }}
run: |-
mkdir -p .qwen/
echo "${SETTINGS}" > ".qwen/settings.json"
shell: 'bash'
env:
SETTINGS: '${{ inputs.settings }}'
- name: 'Install Custom Commands'
shell: 'bash'
run: |-
set -euo pipefail
mkdir -p .qwen/commands
cp -r "${GITHUB_ACTION_PATH}/.github/commands/"* .qwen/commands/
env:
GITHUB_ACTION_PATH: '${{ github.action_path }}'
- name: 'Install pnpm'
if: |-
${{ inputs.use_pnpm == 'true' }}
uses: 'pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061' # ratchet:pnpm/action-setup@v4
with:
version: 10
- name: 'Install Qwen Code'
id: 'install'
env:
QWEN_CLI_VERSION: '${{ inputs.qwen_cli_version }}'
EXTENSIONS: '${{ inputs.extensions }}'
USE_PNPM: '${{ inputs.use_pnpm }}'
shell: 'bash'
run: |-
set -euo pipefail
VERSION_INPUT="${QWEN_CLI_VERSION:-latest}"
if [[ "${VERSION_INPUT}" == "latest" || "${VERSION_INPUT}" == "preview" || "${VERSION_INPUT}" == "nightly" || "${VERSION_INPUT}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9\.-]+)?(\+[a-zA-Z0-9\.-]+)?$ ]]; then
echo "Installing Qwen Code from npm: @qwen-code/qwen-code@${VERSION_INPUT}"
if [[ "${USE_PNPM}" == "true" ]]; then
pnpm add --silent --global @qwen-code/qwen-code@"${VERSION_INPUT}"
else
npm install --silent --no-audit --prefer-offline --global @qwen-code/qwen-code@"${VERSION_INPUT}"
fi
else
echo "Installing Qwen Code from GitHub: github:QwenLM/qwen-code#${VERSION_INPUT}"
git clone https://github.com/QwenLM/qwen-code.git
cd qwen-code
git checkout "${VERSION_INPUT}"
npm install
npm run bundle
npm install --silent --no-audit --prefer-offline --global .
fi
echo "Verifying installation:"
if command -v qwen >/dev/null 2>&1; then
qwen --version || echo "Qwen Code installed successfully (version command not available)"
else
echo "Error: Qwen Code not found in PATH"
exit 1
fi
if [[ -n "${EXTENSIONS}" ]]; then
echo "Installing Qwen Code extensions:"
echo "${EXTENSIONS}" | jq -r '.[]' | while IFS= read -r extension; do
extension=$(echo "${extension}" | xargs)
if [[ -n "${extension}" ]]; then
echo "Installing ${extension}..."
echo "Y" | qwen extensions install "${extension}"
fi
done
fi
- name: 'Run Qwen Code'
id: 'qwen_run'
shell: 'bash'
run: |-
set -euo pipefail
# Create a temporary directory for storing the output, and ensure it's
# cleaned up later
TEMP_STDOUT="$(mktemp -p "${RUNNER_TEMP}" qwen-out.XXXXXXXXXX)"
TEMP_STDERR="$(mktemp -p "${RUNNER_TEMP}" qwen-err.XXXXXXXXXX)"
function cleanup {
rm -f "${TEMP_STDOUT}" "${TEMP_STDERR}"
}
trap cleanup EXIT
# Keep track of whether we've failed
FAILED=false
# Run Qwen Code with the provided prompt, using JSON output format
if [[ "${DEBUG}" = true ]]; then
echo "::warning::Qwen Code debug logging is enabled. This will stream responses, which could reveal sensitive information if processed with untrusted inputs."
fi
# We capture stdout (JSON) to TEMP_STDOUT and stderr to TEMP_STDERR
if ! qwen --yolo --prompt "${PROMPT}" --channel=CI --output-format json 2> "${TEMP_STDERR}" 1> "${TEMP_STDOUT}"; then
FAILED=true
fi
if [[ "${DEBUG}" = true ]]; then
echo "::: Start Qwen Code STDOUT :::"
cat "${TEMP_STDOUT}"
echo "::: End Qwen Code STDOUT :::"
fi
# Create the artifacts directory and copy full logs
mkdir -p qwen-artifacts
cp "${TEMP_STDOUT}" qwen-artifacts/stdout.log
cp "${TEMP_STDERR}" qwen-artifacts/stderr.log
if [[ -f .qwen/telemetry.log ]]; then
cp .qwen/telemetry.log qwen-artifacts/telemetry.log
else
# Create an empty file so the artifact upload doesn't fail if telemetry is missing
touch qwen-artifacts/telemetry.log
fi
# Parse JSON output to extract response and errors
# The Qwen Code outputs an array of event objects, extract assistant message content
RESPONSE=""
ERROR_JSON=""
if jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; then
# Extract the text from assistant message content
RESPONSE=$(jq -r '[.[] | select(.type == "assistant")] | last | .message.content[] | select(.type == "text") | .text' "${TEMP_STDOUT}")
fi
if jq -e . "${TEMP_STDERR}" >/dev/null 2>&1; then
ERROR_JSON=$(jq -c '.error // empty' "${TEMP_STDERR}")
fi
if ! jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; then
echo "::warning::Qwen Code stdout was not valid JSON"
fi
if [[ -s "${TEMP_STDERR}" ]] && ! jq -e . "${TEMP_STDERR}" >/dev/null 2>&1; then
echo "::warning::Qwen Code stderr was not empty but not valid JSON"
fi
# Set the captured response as a step output, supporting multiline
echo "qwen_response<<EOF" >> "${GITHUB_OUTPUT}"
if [[ -n "${RESPONSE}" ]]; then
echo "${RESPONSE}" >> "${GITHUB_OUTPUT}"
else
cat "${TEMP_STDOUT}" >> "${GITHUB_OUTPUT}"
fi
echo "EOF" >> "${GITHUB_OUTPUT}"
# Set the captured errors as a step output, supporting multiline
echo "qwen_errors<<EOF" >> "${GITHUB_OUTPUT}"
if [[ -n "${ERROR_JSON}" ]]; then
echo "${ERROR_JSON}" >> "${GITHUB_OUTPUT}"
else
cat "${TEMP_STDERR}" >> "${GITHUB_OUTPUT}"
fi
echo "EOF" >> "${GITHUB_OUTPUT}"
# Always emit stderr to the step log so silent failures (exit 0 with
# empty/broken output) are diagnosable from the Actions log alone.
if [[ -s "${TEMP_STDERR}" ]]; then
echo "::: Start Qwen Code STDERR :::"
cat "${TEMP_STDERR}"
echo "::: End Qwen Code STDERR :::"
fi
# Warn on empty response after a successful exit — the caller workflow
# cannot distinguish "agent did nothing" from "agent succeeded" without
# this signal.
if [[ "${FAILED}" = false && -z "${RESPONSE}" ]]; then
echo "::warning title=Qwen Code empty response::Qwen Code exited successfully but produced no response. Check stderr above for diagnostics."
fi
if [[ "${FAILED}" = true ]]; then
# If we have a structured error from JSON, use it for the error message
if [[ -n "${ERROR_JSON}" ]]; then
ERROR_MSG=$(jq -r '.message // .' <<< "${ERROR_JSON}")
echo "::error title=Qwen Code execution failed::${ERROR_MSG}"
fi
exit 1
fi
env:
DEBUG: '${{ fromJSON(inputs.qwen_debug || false) }}'
OPENAI_API_KEY: '${{ inputs.openai_api_key }}'
OPENAI_BASE_URL: ${{ inputs.openai_base_url || 'https://dashscope.aliyuncs.com/compatible-mode/v1' }}
OPENAI_MODEL: ${{ inputs.openai_model || 'qwen-coder-plus-latest' }}
SURFACE: 'GitHub'
PROMPT: '${{ inputs.prompt }}'
GH_WORKFLOW_NAME: '${{ steps.sanitize_workflow_name.outputs.gh_workflow_name }}'
- name: 'Upload Qwen Code outputs'
if: |-
${{ inputs.upload_artifacts == 'true' }}
uses: 'actions/upload-artifact@v4' # ratchet:exclude
with:
name: 'qwen-output'
path: 'qwen-artifacts/'
branding:
icon: 'terminal'
color: 'blue'