Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion scripts/comment-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ while IFS= read -r ROW; do

if [ "$STATUS" = "success" ]; then
STATUS_TEXT=":white_check_mark: Draft Preview"
LINK="[Preview in Glean](${INSTANCE_URL_FE}/chat/agents/${AID}/edit?qe=${BE_ENCODED})"
PID=$(echo "$ROW" | jq -r '.previewId // ""')
[ -z "$PID" ] && PID="$AID"
LINK="[Preview in Glean](${INSTANCE_URL_FE}/chat/agents/${PID}/preview?qe=${BE_ENCODED})"
else
STATUS_TEXT=":x: Draft Preview"
LINK=$(echo "$ROW" | jq -r '.error // "Failed"')
Expand Down
58 changes: 54 additions & 4 deletions scripts/sync-agents.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ build_sync_request_automode() {
end'
}

build_preview_request_workflow() {
local agent_id="$1"
local spec_json="$2"

echo "$spec_json" | jq -c \
--arg parent "$agent_id" \
'{
transient: true,
parentWorkflowId: $parent,
workflowNamespace: "AGENT",
name: .rootWorkflow.name,
description: .rootWorkflow.description,
icon: .rootWorkflow.icon,
schema: .rootWorkflow.schema
}'
}

build_preview_request_automode() {
local agent_id="$1"
local converter_json="$2"

echo "$converter_json" | jq -c \
--arg parent "$agent_id" \
'del(.id) + {
transient: true,
parentWorkflowId: $parent,
workflowNamespace: "AGENT"
}'
}

# Infers agent type from folder structure.
# Prints "automode" if spec.yaml is present (autonomous agent),
# "workflow" if a .json spec file is present (workflow agent),
Expand Down Expand Up @@ -210,6 +240,13 @@ while IFS= read -r FOLDER; do
fi
fi

IS_PREVIEW=false
REQUEST_URL="${INSTANCE_URL_BE}/rest/api/v1/agents/${AGENT_ID}"
if [ "$MODE" = "draft_preview" ]; then
IS_PREVIEW=true
REQUEST_URL="${INSTANCE_URL_BE}/rest/api/v1/agents"
fi

if [ "$AGENT_MODE" = "workflow" ]; then
JSON_SPEC_FILES=()
for JSON_FILE in "${FOLDER_PATH}"/*.json; do
Expand Down Expand Up @@ -244,7 +281,11 @@ while IFS= read -r FOLDER; do
WORKFLOW_NAME=$(echo "$SPEC_JSON" | jq -r '.rootWorkflow.name // ""')
[ -n "$WORKFLOW_NAME" ] && AGENT_DISPLAY_NAME="$WORKFLOW_NAME"

REQUEST_BODY=$(build_sync_request_workflow "$AGENT_ID" "$SPEC_JSON" "$COMMIT_SHA" "$IS_DRAFT" "$PUBLISH" "$MESSAGE" "${PR_AUTHOR:-}")
if [ "$IS_PREVIEW" = "true" ]; then
REQUEST_BODY=$(build_preview_request_workflow "$AGENT_ID" "$SPEC_JSON")
else
REQUEST_BODY=$(build_sync_request_workflow "$AGENT_ID" "$SPEC_JSON" "$COMMIT_SHA" "$IS_DRAFT" "$PUBLISH" "$MESSAGE" "${PR_AUTHOR:-}")
fi

elif [ "$AGENT_MODE" = "automode" ]; then
CONVERTER_STDERR_FILE="$RUNNER_TEMP/converter-stderr-${FOLDER}.txt"
Expand Down Expand Up @@ -299,7 +340,11 @@ while IFS= read -r FOLDER; do
AUTOMODE_NAME=$(echo "$CONVERTER_OUTPUT" | jq -r '.name // ""')
[ -n "$AUTOMODE_NAME" ] && AGENT_DISPLAY_NAME="$AUTOMODE_NAME"

REQUEST_BODY=$(build_sync_request_automode "$AGENT_ID" "$CONVERTER_OUTPUT" "$COMMIT_SHA" "$IS_DRAFT" "$PUBLISH" "$MESSAGE" "${PR_AUTHOR:-}")
if [ "$IS_PREVIEW" = "true" ]; then
REQUEST_BODY=$(build_preview_request_automode "$AGENT_ID" "$CONVERTER_OUTPUT")
else
REQUEST_BODY=$(build_sync_request_automode "$AGENT_ID" "$CONVERTER_OUTPUT" "$COMMIT_SHA" "$IS_DRAFT" "$PUBLISH" "$MESSAGE" "${PR_AUTHOR:-}")
fi
fi

echo "Agent: $AGENT_ID (folder: $FOLDER)"
Expand All @@ -316,7 +361,7 @@ while IFS= read -r FOLDER; do
CURL_EXIT=0
HTTP_CODE=$(curl -sS --connect-timeout 10 --max-time 60 \
-o "$RUNNER_TEMP/sync-response-${FOLDER}.json" -w '%{http_code}' \
-X POST "${INSTANCE_URL_BE}/rest/api/v1/agents/${AGENT_ID}" \
-X POST "${REQUEST_URL}" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "Content-Type: application/json" \
-d "@${CURL_BODY_FILE}" 2>"$CURL_ERR_FILE") || CURL_EXIT=$?
Expand All @@ -338,13 +383,18 @@ while IFS= read -r FOLDER; do

if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then
echo " Synced successfully (HTTP $HTTP_CODE)"
PREVIEW_ID=""
if [ "$IS_PREVIEW" = "true" ]; then
PREVIEW_ID=$(jq -r '.workflow.id // ""' "$RUNNER_TEMP/sync-response-${FOLDER}.json" 2>/dev/null || echo "")
fi
RESULTS=$(echo "$RESULTS" | jq -c \
--arg aid "$AGENT_ID" \
--arg name "$AGENT_DISPLAY_NAME" \
--arg agentMode "$AGENT_MODE" \
--arg mode "$MODE" \
--arg msg "$MESSAGE" \
'. + [{"agentId": $aid, "agentName": $name, "agentMode": $agentMode, "mode": $mode, "message": $msg, "status": "success"}]')
--arg pid "$PREVIEW_ID" \
'. + [{"agentId": $aid, "agentName": $name, "agentMode": $agentMode, "mode": $mode, "message": $msg, "previewId": $pid, "status": "success"}]')
else
RESP_BODY=$(cat "$RUNNER_TEMP/sync-response-${FOLDER}.json" 2>/dev/null || echo "no response body")
echo "::error::Failed to sync agent $AGENT_ID (HTTP $HTTP_CODE): $RESP_BODY"
Expand Down