-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththread.go
More file actions
70 lines (60 loc) · 1.79 KB
/
Copy paththread.go
File metadata and controls
70 lines (60 loc) · 1.79 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
package codex
import (
"context"
"errors"
"log/slog"
"github.com/pmenglund/codex-sdk-go/rpc"
)
// Thread represents an active conversation thread.
type Thread struct {
client *rpc.Client
id string
logger *slog.Logger
}
// ID returns the thread id.
func (t *Thread) ID() string {
return t.id
}
// Run sends a text prompt and waits for the turn to finish.
func (t *Thread) Run(ctx context.Context, prompt string, opts *TurnOptions) (*TurnResult, error) {
return t.RunInputs(ctx, []Input{TextInput(prompt)}, opts)
}
// RunInputs sends structured inputs and waits for the turn to finish. If ctx
// is canceled or expires, it best-effort interrupts the remote turn before
// returning the original context error. Use RunStreamed to retain manual
// control over whether remote work is interrupted.
func (t *Thread) RunInputs(ctx context.Context, inputs []Input, opts *TurnOptions) (*TurnResult, error) {
if err := t.ensureReady(); err != nil {
return nil, err
}
handle, err := t.StartTurn(ctx, inputs, opts)
if err != nil {
return nil, err
}
return handle.Run(ctx)
}
// RunStreamed sends structured inputs and returns a streaming iterator.
// The iterator includes thread-scoped events and any notifications that omit
// threadId (for example account/session updates).
func (t *Thread) RunStreamed(ctx context.Context, inputs []Input, opts *TurnOptions) (*TurnStream, error) {
if err := t.ensureReady(); err != nil {
return nil, err
}
handle, err := t.StartTurn(ctx, inputs, opts)
if err != nil {
return nil, err
}
return handle.Stream()
}
func (t *Thread) ensureReady() error {
if t == nil {
return errors.New("thread is nil")
}
if t.client == nil {
return errors.New("thread client is not initialized")
}
if t.id == "" {
return errors.New("thread id is empty")
}
return nil
}