-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathkanban.go
More file actions
1025 lines (898 loc) · 28.9 KB
/
Copy pathkanban.go
File metadata and controls
1025 lines (898 loc) · 28.9 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/pkg/media"
)
const (
realtimeCallsURL = "https://api.openai.com/v1/realtime/calls"
defaultRealtimeModel = "gpt-realtime-2"
defaultReasoningEffort = "low"
realtimeEventChannelLabel = "oai-events"
realtimeInputTrackID = "kanban-realtime:mixed-audio"
realtimeInputStreamID = "kanban-realtime-input"
realtimeMixedAudioSinkKey = "kanban-realtime"
)
type kanbanStatus string
const (
kanbanStatusBacklog kanbanStatus = "Backlog"
kanbanStatusInProgress kanbanStatus = "In Progress"
kanbanStatusBlocked kanbanStatus = "Blocked"
kanbanStatusDone kanbanStatus = "Done"
)
var kanbanStatuses = []kanbanStatus{
kanbanStatusBacklog,
kanbanStatusInProgress,
kanbanStatusBlocked,
kanbanStatusDone,
}
type kanbanCard struct {
ID string `json:"id"`
Status kanbanStatus `json:"status"`
Title string `json:"title"`
Notes string `json:"notes"`
Tags []string `json:"tags"`
}
type kanbanBoardState struct {
Cards []kanbanCard `json:"cards"`
UpdatedAt string `json:"updatedAt,omitempty"`
}
type kanbanRealtimeEvent struct {
Type string `json:"type,omitempty"`
Transcript string `json:"transcript,omitempty"`
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
CallID string `json:"call_id,omitempty"`
Error *struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
} `json:"error,omitempty"`
Item *kanbanRealtimeOutputItem `json:"item,omitempty"`
Response *struct {
Output []kanbanRealtimeOutputItem `json:"output,omitempty"`
} `json:"response,omitempty"`
}
type kanbanRealtimeOutputItem struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
CallID string `json:"call_id,omitempty"`
}
type kanbanBoardApp struct {
mu sync.Mutex
cards []kanbanCard
nextCreatedIndex int
updatedAt time.Time
handledCalls map[string]struct{}
model string
pc *webrtc.PeerConnection
events *webrtc.DataChannel
inputTrack *webrtc.TrackLocalStaticSample
inputEnc *opusEncoder
connected bool
closeOnce sync.Once
}
var initialKanbanBoardCards = []kanbanCard{
{
ID: "card-002",
Status: kanbanStatusBacklog,
Title: "Add RTP Retransmission Buffer",
Notes: "Keep recent RTP packets available for NACK-driven retransmission without unbounded memory growth.",
Tags: []string{"webrtc", "rtp", "nack"},
},
{
ID: "card-003",
Status: kanbanStatusBacklog,
Title: "Implement ICE Restart Handling",
Notes: "Support renegotiation paths that refresh ICE credentials and reconnect peers after network changes.",
Tags: []string{"webrtc", "ice", "signaling"},
},
{
ID: "card-004",
Status: kanbanStatusBacklog,
Title: "Harden DTLS/SRTP Cleanup",
Notes: "Ensure failed and closed peer connections release transports, tracks, and SRTP state promptly.",
Tags: []string{"webrtc", "dtls", "srtp"},
},
{
ID: "card-005",
Status: kanbanStatusBacklog,
Title: "Add Simulcast Forwarding Controls",
Notes: "Choose forwarded RTP layers per subscriber so the server can adapt streams to bandwidth and viewport size.",
Tags: []string{"webrtc", "simulcast", "bandwidth"},
},
{
ID: "card-001",
Status: kanbanStatusBacklog,
Title: "Finish RTP HEVC Packetizer",
Notes: "Complete HEVC payload fragmentation, aggregation, and marker-bit handling for outbound RTP streams.",
Tags: []string{"webrtc", "rtp", "hevc"},
},
}
func newKanbanBoardApp() *kanbanBoardApp {
return &kanbanBoardApp{
cards: cloneKanbanCards(initialKanbanBoardCards),
nextCreatedIndex: 1,
updatedAt: time.Now().UTC(),
handledCalls: map[string]struct{}{},
}
}
func (app *kanbanBoardApp) JoinConferenceRoom() error {
apiKey := strings.TrimSpace(os.Getenv("OPENAI_API_KEY"))
if apiKey == "" {
return fmt.Errorf("OPENAI_API_KEY is not configured")
}
peerConnection, err := newPeerConnection()
if err != nil {
return fmt.Errorf("create Realtime peer connection: %w", err)
}
inputTrack, err := webrtc.NewTrackLocalStaticSample(
webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeOpus,
ClockRate: roomAudioSampleRate,
Channels: roomAudioChannels,
},
realtimeInputTrackID,
realtimeInputStreamID,
)
if err != nil {
_ = peerConnection.Close()
return fmt.Errorf("create Realtime mixed audio input track: %w", err)
}
inputEnc, err := newOpusEncoder(roomAudioSampleRate, roomAudioChannels)
if err != nil {
_ = peerConnection.Close()
return fmt.Errorf("create Realtime mixed audio encoder: %w", err)
}
inputSender, err := peerConnection.AddTrack(inputTrack)
if err != nil {
_ = peerConnection.Close()
return fmt.Errorf("attach Realtime mixed audio input track: %w", err)
}
go drainRTCP(inputSender)
events, err := peerConnection.CreateDataChannel(realtimeEventChannelLabel, nil)
if err != nil {
_ = peerConnection.Close()
return fmt.Errorf("create Realtime event data channel: %w", err)
}
model := realtimeModel()
app.mu.Lock()
app.model = model
app.pc = peerConnection
app.events = events
app.inputTrack = inputTrack
app.inputEnc = inputEnc
app.mu.Unlock()
peerConnection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
log.Infof("OpenAI Realtime peer state changed: %s", state.String())
broadcastKanbanEvent("status", "OpenAI Realtime: "+state.String())
})
events.OnOpen(func() {
log.Infof("OpenAI Realtime event channel opened")
_ = app.SendEvent(app.sessionUpdateEvent())
broadcastKanbanEvent("status", "Kanban assistant is listening")
})
events.OnMessage(func(message webrtc.DataChannelMessage) {
app.handleRealtimeEvent(message.Data)
})
go func() {
if err := app.connectRealtimePeer(apiKey, model); err != nil {
log.Errorf("Failed to connect OpenAI Realtime peer: %v", err)
broadcastKanbanEvent("status", "OpenAI Realtime disabled: "+err.Error())
_ = peerConnection.Close()
return
}
if roomMixer != nil {
roomMixer.setSink(realtimeMixedAudioSinkKey, app)
}
}()
return nil
}
func (app *kanbanBoardApp) Close() error {
var closeErr error
app.closeOnce.Do(func() {
if roomMixer != nil {
roomMixer.removeSink(realtimeMixedAudioSinkKey)
}
app.mu.Lock()
peerConnection := app.pc
app.mu.Unlock()
if peerConnection != nil {
closeErr = peerConnection.Close()
}
})
return closeErr
}
func (app *kanbanBoardApp) connectRealtimePeer(apiKey string, model string) error {
app.mu.Lock()
if app.connected {
app.mu.Unlock()
return nil
}
peerConnection := app.pc
app.mu.Unlock()
if peerConnection == nil {
return fmt.Errorf("Realtime peer connection is unavailable")
}
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
return fmt.Errorf("create Realtime offer: %w", err)
}
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
if err := peerConnection.SetLocalDescription(offer); err != nil {
return fmt.Errorf("set Realtime local description: %w", err)
}
<-gatherComplete
localDescription := peerConnection.LocalDescription()
if localDescription == nil || strings.TrimSpace(localDescription.SDP) == "" {
return fmt.Errorf("Realtime peer connection did not produce a local description")
}
answerSDP, err := app.createRealtimeCall(apiKey, model, localDescription.SDP)
if err != nil {
return err
}
if err := peerConnection.SetRemoteDescription(webrtc.SessionDescription{
Type: webrtc.SDPTypeAnswer,
SDP: answerSDP,
}); err != nil {
return fmt.Errorf("set Realtime remote description: %w", err)
}
app.mu.Lock()
app.connected = true
app.mu.Unlock()
return nil
}
func (app *kanbanBoardApp) WriteMixedPCM(roomPCM []int16) error {
if len(roomPCM) == 0 {
return nil
}
if len(roomPCM)%roomAudioMixFrameSize != 0 {
return fmt.Errorf("mixed PCM length %d must be a multiple of %d samples", len(roomPCM), roomAudioMixFrameSize)
}
app.mu.Lock()
inputTrack := app.inputTrack
inputEnc := app.inputEnc
app.mu.Unlock()
if inputTrack == nil || inputEnc == nil {
return fmt.Errorf("Realtime mixed audio input is unavailable")
}
for offset := 0; offset < len(roomPCM); offset += roomAudioMixFrameSize {
frame := roomPCM[offset : offset+roomAudioMixFrameSize]
opusFrame, err := inputEnc.Encode(frame)
if err != nil {
return fmt.Errorf("encode mixed room audio: %w", err)
}
if err := inputTrack.WriteSample(media.Sample{
Data: opusFrame,
Duration: roomAudioMixInterval,
}); err != nil {
return fmt.Errorf("write mixed room audio sample: %w", err)
}
}
return nil
}
func drainRTCP(sender *webrtc.RTPSender) {
buffer := make([]byte, 1500)
for {
if _, _, err := sender.Read(buffer); err != nil {
return
}
}
}
func (app *kanbanBoardApp) createRealtimeCall(apiKey string, model string, offerSDP string) (string, error) {
contentType, body, err := buildRealtimeCallRequest(offerSDP, app.sessionConfig(model))
if err != nil {
return "", err
}
request, err := http.NewRequest(http.MethodPost, realtimeCallsURL, bytes.NewReader(body))
if err != nil {
return "", fmt.Errorf("create Realtime request: %w", err)
}
request.Header.Set("Authorization", "Bearer "+apiKey)
request.Header.Set("Content-Type", contentType)
response, err := (&http.Client{Timeout: 30 * time.Second}).Do(request)
if err != nil {
return "", fmt.Errorf("create Realtime session: %w", err)
}
defer response.Body.Close()
answerSDP, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("read Realtime answer: %w", err)
}
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
return "", fmt.Errorf("Realtime session failed: status=%s body=%s", response.Status, strings.TrimSpace(string(answerSDP)))
}
return string(answerSDP), nil
}
func buildRealtimeCallRequest(offerSDP string, session map[string]any) (string, []byte, error) {
sessionJSON, err := json.Marshal(session)
if err != nil {
return "", nil, fmt.Errorf("marshal Realtime session: %w", err)
}
var body bytes.Buffer
writer := multipart.NewWriter(&body)
if err := writer.WriteField("sdp", offerSDP); err != nil {
return "", nil, fmt.Errorf("write SDP offer: %w", err)
}
if err := writer.WriteField("session", string(sessionJSON)); err != nil {
return "", nil, fmt.Errorf("write session config: %w", err)
}
if err := writer.Close(); err != nil {
return "", nil, fmt.Errorf("finalize multipart request: %w", err)
}
return writer.FormDataContentType(), body.Bytes(), nil
}
func (app *kanbanBoardApp) SendEvent(payload any) error {
raw, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("marshal Realtime event: %w", err)
}
app.mu.Lock()
events := app.events
app.mu.Unlock()
if events == nil || events.ReadyState() != webrtc.DataChannelStateOpen {
return fmt.Errorf("Realtime event channel is unavailable")
}
return events.SendText(string(raw))
}
func (app *kanbanBoardApp) sessionConfig(model string) map[string]any {
session := map[string]any{
"type": "realtime",
"model": model,
"output_modalities": []string{"text"},
"audio": map[string]any{
"input": map[string]any{
"noise_reduction": map[string]any{
"type": "near_field",
},
"transcription": map[string]any{
"model": "gpt-4o-mini-transcribe",
"language": "en",
},
"turn_detection": map[string]any{
"type": "server_vad",
"threshold": 0.5,
"prefix_padding_ms": 300,
"silence_duration_ms": 200,
"create_response": true,
"interrupt_response": false,
},
},
},
"instructions": app.sessionInstructions(),
"tools": app.kanbanTools(),
"tool_choice": "required",
}
if usesAdvancedCommandProfile(model) {
session["reasoning"] = map[string]any{
"effort": defaultReasoningEffort,
}
}
return session
}
func (app *kanbanBoardApp) sessionUpdateEvent() map[string]any {
return map[string]any{
"type": "session.update",
"session": app.sessionConfig(app.model),
}
}
func realtimeModel() string {
if model := strings.TrimSpace(os.Getenv("OPENAI_REALTIME_MODEL")); model != "" {
return model
}
return defaultRealtimeModel
}
func usesAdvancedCommandProfile(model string) bool {
normalizedModel := strings.ToLower(strings.TrimSpace(model))
return normalizedModel == "gpt-realtime-2"
}
func (app *kanbanBoardApp) sessionInstructions() string {
return strings.Join([]string{
"You are a voice-operated Kanban board operator.",
"Listen to the user and decide whether they want to create a ticket, move a ticket between columns, add tags to a ticket, update a ticket, delete a ticket, or do nothing.",
"Use the board card ids exactly as provided when operating on existing tickets.",
"Users may say ticket, card, task, issue, or sticky note; treat those as Kanban cards.",
"Available columns are Backlog, In Progress, Blocked, and Done.",
"This is used during standups and meetings. Treat concrete first-person status updates as implicit board operations; do not wait for the user to say create a ticket.",
"If a user says they shipped, fixed, completed, closed, or finished work, move an existing related ticket to Done if one exists; otherwise create a concise Done ticket.",
"If a user says they started, began, picked up, or are working on something, move an existing related ticket to In Progress if one exists; otherwise create a concise In Progress ticket.",
"If a user says they are blocked, waiting on something, dependent on another team, or that work might slip, move or create the related ticket in Blocked and add blocked, dependency, or risk tags as appropriate.",
"Track meeting context across turns. If a follow-up sentence adds dependency, blocker, or schedule-risk context for the most recently discussed related card, update or move that existing ticket instead of creating a duplicate.",
"If a transcript includes a speaker label such as Sean:, do not include the label in the title; use it only as context for notes or tags when useful.",
"If a user asks to start, work on, pick up, or begin a ticket, move it to In Progress.",
"If a user asks to block, mark blocked, or note a dependency for a ticket, move it to Blocked and preserve the blocker details in notes.",
"If a user asks to ship, finish, complete, close, or mark done, move it to Done.",
"If a user asks to park, punt, defer, or move something back, move it to Backlog.",
"If a user asks to add a tag, call add_tags; do not replace existing tags.",
"If one transcript contains multiple status updates, call one tool for each board operation.",
"If the user asks for an operation or gives an implicit status update, call the relevant tool. Prefer tools over text replies.",
"If the user is only wrapping up, handing off, giving filler, or saying something like That's it from me, call do_nothing with a short reason.",
"If the user is not asking for a board operation and is not giving a concrete status update, call do_nothing with a short reason.",
"Do not narrate board operations aloud.",
fmt.Sprintf("Current Kanban board JSON: %s", app.boardContextJSON()),
}, " ")
}
func (app *kanbanBoardApp) boardContextJSON() string {
raw, err := json.Marshal(app.snapshotState().Cards)
if err != nil {
return "[]"
}
return string(raw)
}
func (app *kanbanBoardApp) kanbanTools() []map[string]any {
statusProperty := map[string]any{
"type": "string",
"description": "Kanban column for the ticket.",
"enum": []string{"Backlog", "In Progress", "Blocked", "Done"},
}
tagsProperty := map[string]any{
"type": "array",
"description": "Short labels that capture people, area, state, or risk. Use blocked/dependency/risk tags for blockers when appropriate.",
"items": map[string]any{"type": "string"},
}
return []map[string]any{
{
"type": "function",
"name": "create_ticket",
"description": "Create a new Kanban ticket/card for explicit requests or implicit meeting status updates such as shipped, started, or blocked work.",
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"title": map[string]any{"type": "string", "description": "Concise title for the work, without speaker prefixes such as Sean:."},
"notes": map[string]any{"type": "string", "description": "Useful context from the utterance, including blocker, dependency, or schedule-risk details."},
"tags": tagsProperty,
"status": statusProperty,
},
"required": []string{"title", "notes", "tags"},
"additionalProperties": false,
},
},
{
"type": "function",
"name": "move_ticket",
"description": "Move an existing Kanban ticket/card to another column, including Blocked when work is waiting on a dependency.",
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"card_id": map[string]any{"type": "string", "description": "Existing board card id."},
"status": statusProperty,
},
"required": []string{"card_id", "status"},
"additionalProperties": false,
},
},
{
"type": "function",
"name": "add_tags",
"description": "Add one or more tags to an existing Kanban ticket/card without removing existing tags.",
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"card_id": map[string]any{"type": "string", "description": "Existing board card id."},
"tags": tagsProperty,
},
"required": []string{"card_id", "tags"},
"additionalProperties": false,
},
},
{
"type": "function",
"name": "update_ticket",
"description": "Update the title or notes of an existing Kanban ticket/card. Use this to merge follow-up standup details, dependency details, or slip-risk context into the existing notes.",
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"card_id": map[string]any{"type": "string", "description": "Existing board card id."},
"title": map[string]any{"type": "string", "description": "Replacement title, when the existing title should be made clearer."},
"notes": map[string]any{"type": "string", "description": "Full replacement notes. Preserve useful existing notes while adding the new context."},
},
"required": []string{"card_id"},
"additionalProperties": false,
},
},
{
"type": "function",
"name": "delete_ticket",
"description": "Delete an existing Kanban ticket/card.",
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"card_id": map[string]any{"type": "string", "description": "Existing board card id."},
},
"required": []string{"card_id"},
"additionalProperties": false,
},
},
{
"type": "function",
"name": "do_nothing",
"description": "Use this when the user is not asking to operate on the Kanban board, is only wrapping up, or says a handoff phrase like That's it from me.",
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"reason": map[string]any{"type": "string"},
},
"required": []string{"reason"},
"additionalProperties": false,
},
},
}
}
func (app *kanbanBoardApp) handleRealtimeEvent(raw []byte) {
var event kanbanRealtimeEvent
if err := json.Unmarshal(raw, &event); err != nil {
log.Errorf("Failed to parse OpenAI Realtime event: %v", err)
return
}
switch event.Type {
case "error":
if event.Error != nil {
log.Errorf("OpenAI Realtime error code=%s message=%s", event.Error.Code, event.Error.Message)
broadcastKanbanEvent("status", event.Error.Message)
}
case "response.output_item.done":
if event.Item != nil && event.Item.Type == "function_call" {
app.handleToolCall(*event.Item)
}
case "response.function_call_arguments.done":
app.handleToolCall(kanbanRealtimeOutputItem{
Type: "function_call",
Name: event.Name,
Arguments: event.Arguments,
CallID: event.CallID,
})
case "response.done":
if event.Response == nil {
return
}
for _, outputItem := range event.Response.Output {
if outputItem.Type == "function_call" {
app.handleToolCall(outputItem)
}
}
}
}
func (app *kanbanBoardApp) handleToolCall(outputItem kanbanRealtimeOutputItem) {
if strings.TrimSpace(outputItem.CallID) == "" {
log.Errorf("Ignoring Kanban tool call %q without call_id", outputItem.Name)
return
}
app.mu.Lock()
if _, ok := app.handledCalls[outputItem.CallID]; ok {
app.mu.Unlock()
return
}
app.handledCalls[outputItem.CallID] = struct{}{}
app.mu.Unlock()
result, changed, err := app.applyToolCall(outputItem)
if err != nil {
result = map[string]any{
"ok": false,
"error": err.Error(),
}
}
if err := app.SendEvent(map[string]any{
"type": "conversation.item.create",
"item": map[string]any{
"type": "function_call_output",
"call_id": outputItem.CallID,
"output": mustMarshalJSON(result),
},
}); err != nil {
log.Errorf("Failed to send Kanban function output: %v", err)
}
if !changed {
return
}
broadcastKanbanEvent("board", app.snapshotState())
if err := app.SendEvent(app.sessionUpdateEvent()); err != nil {
log.Errorf("Failed to refresh Kanban Realtime session: %v", err)
}
}
func (app *kanbanBoardApp) applyToolCall(outputItem kanbanRealtimeOutputItem) (map[string]any, bool, error) {
args := map[string]any{}
if rawArgs := strings.TrimSpace(outputItem.Arguments); rawArgs != "" {
if err := json.Unmarshal([]byte(rawArgs), &args); err != nil {
return nil, false, fmt.Errorf("parse %s arguments: %w", outputItem.Name, err)
}
}
switch outputItem.Name {
case "create_ticket":
return app.createTicket(args)
case "move_ticket":
return app.moveTicket(args)
case "add_tags":
return app.addTags(args)
case "update_ticket":
return app.updateTicket(args)
case "delete_ticket":
return app.deleteTicket(args)
case "do_nothing":
reason := asString(args["reason"])
if reason == "" {
reason = "No board update requested."
}
return map[string]any{
"ok": true,
"reason": reason,
}, false, nil
default:
return nil, false, fmt.Errorf("unsupported function %q", outputItem.Name)
}
}
func (app *kanbanBoardApp) createTicket(args map[string]any) (map[string]any, bool, error) {
title := asString(args["title"])
if title == "" {
return nil, false, fmt.Errorf("title is required")
}
notes := asString(args["notes"])
tags := uniqueStrings(asStringSlice(args["tags"]))
status := kanbanStatusBacklog
if rawStatus, ok := args["status"]; ok {
parsedStatus, err := parseKanbanStatus(rawStatus)
if err != nil {
return nil, false, err
}
status = parsedStatus
}
app.mu.Lock()
defer app.mu.Unlock()
card := kanbanCard{
ID: app.createCardIDLocked(),
Status: status,
Title: title,
Notes: notes,
Tags: tags,
}
app.cards = append(app.cards, card)
app.touchLocked()
return map[string]any{
"ok": true,
"created": true,
"card": cloneKanbanCard(card),
}, true, nil
}
func (app *kanbanBoardApp) moveTicket(args map[string]any) (map[string]any, bool, error) {
cardID := asString(args["card_id"])
if cardID == "" {
return nil, false, fmt.Errorf("card_id is required")
}
status, err := parseKanbanStatus(args["status"])
if err != nil {
return nil, false, err
}
app.mu.Lock()
defer app.mu.Unlock()
card, ok := app.findCardLocked(cardID)
if !ok {
return nil, false, fmt.Errorf("unknown card_id: %s", cardID)
}
card.Status = status
app.touchLocked()
return map[string]any{
"ok": true,
"moved": true,
"card_id": cardID,
"status": status,
}, true, nil
}
func (app *kanbanBoardApp) addTags(args map[string]any) (map[string]any, bool, error) {
cardID := asString(args["card_id"])
if cardID == "" {
return nil, false, fmt.Errorf("card_id is required")
}
tags := uniqueStrings(asStringSlice(args["tags"]))
app.mu.Lock()
defer app.mu.Unlock()
card, ok := app.findCardLocked(cardID)
if !ok {
return nil, false, fmt.Errorf("unknown card_id: %s", cardID)
}
card.Tags = uniqueStrings(append(card.Tags, tags...))
app.touchLocked()
return map[string]any{
"ok": true,
"tags_added": true,
"card_id": cardID,
"tags": append([]string(nil), tags...),
}, true, nil
}
func (app *kanbanBoardApp) updateTicket(args map[string]any) (map[string]any, bool, error) {
cardID := asString(args["card_id"])
if cardID == "" {
return nil, false, fmt.Errorf("card_id is required")
}
title := asString(args["title"])
notes := asString(args["notes"])
app.mu.Lock()
defer app.mu.Unlock()
card, ok := app.findCardLocked(cardID)
if !ok {
return nil, false, fmt.Errorf("unknown card_id: %s", cardID)
}
if title != "" {
card.Title = title
}
if notes != "" {
card.Notes = notes
}
app.touchLocked()
return map[string]any{
"ok": true,
"updated": true,
"card_id": cardID,
}, true, nil
}
func (app *kanbanBoardApp) deleteTicket(args map[string]any) (map[string]any, bool, error) {
cardID := asString(args["card_id"])
if cardID == "" {
return nil, false, fmt.Errorf("card_id is required")
}
app.mu.Lock()
defer app.mu.Unlock()
index := -1
for candidateIndex, card := range app.cards {
if card.ID == cardID {
index = candidateIndex
break
}
}
if index == -1 {
return nil, false, fmt.Errorf("unknown card_id: %s", cardID)
}
app.cards = append(app.cards[:index], app.cards[index+1:]...)
app.touchLocked()
return map[string]any{
"ok": true,
"deleted": true,
"card_id": cardID,
}, true, nil
}
func (app *kanbanBoardApp) snapshotState() kanbanBoardState {
app.mu.Lock()
defer app.mu.Unlock()
state := kanbanBoardState{
Cards: cloneKanbanCards(app.cards),
}
if !app.updatedAt.IsZero() {
state.UpdatedAt = app.updatedAt.UTC().Format(time.RFC3339Nano)
}
return state
}
func (app *kanbanBoardApp) createCardIDLocked() string {
for {
cardID := fmt.Sprintf("kanban-card-%03d", app.nextCreatedIndex)
app.nextCreatedIndex++
if _, exists := app.findCardLocked(cardID); exists {
continue
}
return cardID
}
}
func (app *kanbanBoardApp) findCardLocked(cardID string) (*kanbanCard, bool) {
for index := range app.cards {
if app.cards[index].ID == cardID {
return &app.cards[index], true
}
}
return nil, false
}
func (app *kanbanBoardApp) touchLocked() {
app.updatedAt = time.Now().UTC()
}
func cloneKanbanCards(cards []kanbanCard) []kanbanCard {
clonedCards := make([]kanbanCard, 0, len(cards))
for _, card := range cards {
clonedCards = append(clonedCards, cloneKanbanCard(card))
}
return clonedCards
}
func cloneKanbanCard(card kanbanCard) kanbanCard {
return kanbanCard{
ID: card.ID,
Status: card.Status,
Title: card.Title,
Notes: card.Notes,
Tags: append([]string(nil), card.Tags...),
}
}
func asString(value any) string {
candidate, ok := value.(string)
if !ok {
return ""
}
return strings.TrimSpace(candidate)
}
func asStringSlice(value any) []string {
rawValues, ok := value.([]any)
if !ok {
return nil
}
values := make([]string, 0, len(rawValues))
for _, rawValue := range rawValues {
if value := asString(rawValue); value != "" {
values = append(values, value)
}
}
return values
}
func parseKanbanStatus(value any) (kanbanStatus, error) {
status := kanbanStatus(asString(value))
for _, candidate := range kanbanStatuses {
if candidate == status {
return status, nil
}
}
return "", fmt.Errorf("unknown Kanban status: %v", value)
}
func uniqueStrings(values []string) []string {
seen := make(map[string]struct{}, len(values))
result := make([]string, 0, len(values))
for _, value := range values {
normalizedValue := strings.TrimSpace(value)
if normalizedValue == "" {
continue
}
if _, ok := seen[normalizedValue]; ok {
continue
}
seen[normalizedValue] = struct{}{}
result = append(result, normalizedValue)
}
return result
}
func mustMarshalJSON(value any) string {
raw, err := json.Marshal(value)
if err != nil {
return `{"ok":false,"error":"Could not encode function output."}`
}
return string(raw)
}
func sendKanbanEvent(websocket *threadSafeWriter, event string, data any) error {
raw, err := json.Marshal(map[string]any{
"event": event,
"data": data,
})
if err != nil {
return err
}
return websocket.WriteJSON(&websocketMessage{
Event: "kanban",
Data: string(raw),
})
}
func broadcastKanbanEvent(event string, data any) {
raw, err := json.Marshal(map[string]any{
"event": event,