mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(coderd): add task log snapshot storage endpoint (#21644)
This change adds a POST /workspaceagents/me/tasks/{task}/log-snapshot
endpoint for agents to upload task conversation history during
workspace shutdown. This allows users to view task logs even when the
workspace is stopped.
The endpoint accepts agentapi format payloads (typically last 10
messages, max 64KB), wraps them in a format envelope, and upserts to the
task_snapshots table. Uses agent token auth and validates the task
belongs to the agent's workspace.
Closes coder/internal#1253
This commit is contained in:
@@ -13183,6 +13183,22 @@ func (q *sqlQuerier) GetTaskByWorkspaceID(ctx context.Context, workspaceID uuid.
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getTaskSnapshot = `-- name: GetTaskSnapshot :one
|
||||
SELECT
|
||||
task_id, log_snapshot, log_snapshot_created_at
|
||||
FROM
|
||||
task_snapshots
|
||||
WHERE
|
||||
task_id = $1
|
||||
`
|
||||
|
||||
func (q *sqlQuerier) GetTaskSnapshot(ctx context.Context, taskID uuid.UUID) (TaskSnapshot, error) {
|
||||
row := q.db.QueryRowContext(ctx, getTaskSnapshot, taskID)
|
||||
var i TaskSnapshot
|
||||
err := row.Scan(&i.TaskID, &i.LogSnapshot, &i.LogSnapshotCreatedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertTask = `-- name: InsertTask :one
|
||||
INSERT INTO tasks
|
||||
(id, organization_id, owner_id, name, display_name, workspace_id, template_version_id, template_parameters, prompt, created_at)
|
||||
@@ -13373,6 +13389,29 @@ func (q *sqlQuerier) UpdateTaskWorkspaceID(ctx context.Context, arg UpdateTaskWo
|
||||
return i, err
|
||||
}
|
||||
|
||||
const upsertTaskSnapshot = `-- name: UpsertTaskSnapshot :exec
|
||||
INSERT INTO
|
||||
task_snapshots (task_id, log_snapshot, log_snapshot_created_at)
|
||||
VALUES
|
||||
($1, $2, $3)
|
||||
ON CONFLICT
|
||||
(task_id)
|
||||
DO UPDATE SET
|
||||
log_snapshot = EXCLUDED.log_snapshot,
|
||||
log_snapshot_created_at = EXCLUDED.log_snapshot_created_at
|
||||
`
|
||||
|
||||
type UpsertTaskSnapshotParams struct {
|
||||
TaskID uuid.UUID `db:"task_id" json:"task_id"`
|
||||
LogSnapshot json.RawMessage `db:"log_snapshot" json:"log_snapshot"`
|
||||
LogSnapshotCreatedAt time.Time `db:"log_snapshot_created_at" json:"log_snapshot_created_at"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) UpsertTaskSnapshot(ctx context.Context, arg UpsertTaskSnapshotParams) error {
|
||||
_, err := q.db.ExecContext(ctx, upsertTaskSnapshot, arg.TaskID, arg.LogSnapshot, arg.LogSnapshotCreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const upsertTaskWorkspaceApp = `-- name: UpsertTaskWorkspaceApp :one
|
||||
INSERT INTO task_workspace_apps
|
||||
(task_id, workspace_build_number, workspace_agent_id, workspace_app_id)
|
||||
|
||||
Reference in New Issue
Block a user