Skip to content

Commit c98314f

Browse files
authored
Merge pull request #1 from Paca-AI/feature/implement-record-activity-function
feat: implement RecordActivity function and update CallerIdentity struct
2 parents bdc6367 + be1e522 commit c98314f

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

dispatch.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (d *dispatcher) handleRequest(payload []byte) []byte {
4545
Query map[string]string `json:"query"`
4646
ProjectID string `json:"project_id"`
4747
CallerID string `json:"caller_id"`
48+
UserID string `json:"user_id"`
4849
CallerRole string `json:"caller_role"`
4950
Headers map[string]string `json:"headers"`
5051
Body []byte `json:"body"`
@@ -70,6 +71,7 @@ func (d *dispatcher) handleRequest(payload []byte) []byte {
7071
Body: hr.Body,
7172
Caller: CallerIdentity{
7273
CallerID: hr.CallerID,
74+
UserID: hr.UserID,
7375
CallerRole: hr.CallerRole,
7476
ProjectID: hr.ProjectID,
7577
},

native_backends.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func (b *stubConfigBackend) Get(_ string) (string, bool) { return "", false }
4545
// EmitEvent is a no-op outside WASM.
4646
func EmitEvent(_ string, _ any) {}
4747

48+
// RecordActivity is a no-op outside WASM.
49+
func RecordActivity(_, _, _, _ string, _ any) {}
50+
4851
// ptrOf and hostError are used by wasm_backends.go (wasip1 only); provide
4952
// stubs here so the non-WASM build does not need them.
5053
//nolint:unused // used in wasm_backends.go in WASM builds

request.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import "encoding/json"
77
type CallerIdentity struct {
88
// CallerID is the project_member UUID of the caller.
99
CallerID string `json:"caller_id"`
10+
// UserID is the authenticated user's UUID (JWT sub claim).
11+
// Use this as actor_id when recording task activities.
12+
UserID string `json:"user_id"`
1013
// CallerRole is the role name of the caller within the project.
1114
CallerRole string `json:"caller_role"`
1215
// ProjectID is the project the request is scoped to.

wasm_backends.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,38 @@ func EmitEvent(topic string, payload any) {
179179
)
180180
}
181181

182+
// ── RecordActivity ────────────────────────────────────────────────────────────
183+
184+
// activityInput is the JSON shape sent to the paca.activity_record host function.
185+
type activityInput struct {
186+
TaskID string `json:"task_id"`
187+
ProjectID string `json:"project_id"`
188+
ActorID string `json:"actor_id"`
189+
ActivityType string `json:"activity_type"`
190+
Content any `json:"content"`
191+
}
192+
193+
// RecordActivity appends a task-activity event to the paca activity stream so
194+
// that it is persisted to PostgreSQL by the ActivityConsumer worker.
195+
// actorUserID should be req.Caller.UserID (the authenticated user's UUID).
196+
// content must be JSON-encodable and match the expected shape for activityType.
197+
func RecordActivity(taskID, projectID, actorUserID, activityType string, content any) {
198+
inp := activityInput{
199+
TaskID: taskID,
200+
ProjectID: projectID,
201+
ActorID: actorUserID,
202+
ActivityType: activityType,
203+
Content: content,
204+
}
205+
payloadBytes, err := json.Marshal(inp)
206+
if err != nil {
207+
return
208+
}
209+
if !hostActivityRecord(int64(ptrOf(payloadBytes)), int64(len(payloadBytes))) {
210+
return
211+
}
212+
}
213+
182214
// ── Helpers ───────────────────────────────────────────────────────────────────
183215

184216
//go:nocheckptr

wasm_imports.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ func hostStorageDelete(keyPtr, keyLen int64) int32
4949
//go:noescape
5050
func hostEventEmit(topicPtr, topicLen, payloadPtr, payloadLen int64) int32
5151

52+
// paca.activity_record(payloadPtr i64, payloadLen i64) -> ok i32
53+
//
54+
//go:wasmimport paca activity_record
55+
//go:noescape
56+
func hostActivityRecord(payloadPtr, payloadLen int64) int32
57+
5258
// paca.config_get(keyPtr i64, keyLen i64, valuePtrPtr i64, valueLenPtr i64)
5359
//
5460
//go:wasmimport paca config_get

0 commit comments

Comments
 (0)