Repository navigation
Expand file tree
/
Copy pathtypes_parts.go
More file actions
597 lines (523 loc) · 19.4 KB
/
Copy pathtypes_parts.go
File metadata and controls
597 lines (523 loc) · 19.4 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
package lm15
import (
"encoding/base64"
"mime"
"os"
"path/filepath"
"regexp"
"strings"
)
// ContinuationState is opaque provider-owned state needed to continue or
// replay a transcript. Provider is the dialect id (openai, anthropic,
// gemini, xai), never the access door (MAP-7.8).
type ContinuationState struct {
Provider string
Kind string
Data JSONObject
}
// Validate checks the state.
func (s ContinuationState) Validate() error {
if s.Provider == "" {
return valueErrorf("ContinuationState.provider cannot be empty")
}
if s.Kind == "" {
return valueErrorf("ContinuationState.kind cannot be empty")
}
if s.Data == nil {
return typeErrorf("data must be a JSON object")
}
return checkJSONObject(s.Data, "data", true)
}
func validateContinuation(states []ContinuationState) error {
for _, s := range states {
if err := s.Validate(); err != nil {
return err
}
}
return nil
}
// ContinuationData returns the data of the first matching state, or nil.
func ContinuationData(states []ContinuationState, provider, kind string) JSONObject {
for _, s := range states {
if s.Provider == provider && s.Kind == kind {
if s.Data == nil {
return JSONObject{}
}
return s.Data
}
}
return nil
}
// ─── Parts ───────────────────────────────────────────────────────────
// Part is the atom of content: a closed sum of twelve variants. Switch on
// the concrete type, or on Type().
type Part interface {
Type() string
ContinuationStates() []ContinuationState
// WithContinuation returns a copy carrying these states.
WithContinuation([]ContinuationState) Part
Validate() error
sealedPart()
}
// TextPart is a block of text.
type TextPart struct {
Text string
Continuation []ContinuationState
}
// ThinkingPart is a model reasoning trace. Hidden thinking is empty Text
// with replay state in Continuation (MAP-7 rule 11).
type ThinkingPart struct {
Text string
Continuation []ContinuationState
}
// RefusalPart is an explicit refusal (non-empty).
type RefusalPart struct {
Text string
Continuation []ContinuationState
}
// CitationPart references source material.
type CitationPart struct {
URL string
Title string
Text string
Continuation []ContinuationState
}
// Media holds the fields every media part shares: exactly one of Data
// (base64), URL, FileID, Path.
type Media struct {
MediaType string
Data string
URL string
FileID string
Path string
Continuation []ContinuationState
}
// ImagePart is an image; Detail is one of low/high/auto.
type ImagePart struct {
Media
Detail string
}
// AudioPart is audio content.
type AudioPart struct{ Media }
// VideoPart is video content.
type VideoPart struct{ Media }
// DocumentPart is a document (PDF, ...).
type DocumentPart struct{ Media }
// BinaryPart is arbitrary bytes.
type BinaryPart struct{ Media }
// ToolCallPart is the model's request for an external computation.
type ToolCallPart struct {
ID string
Name string
Input JSONObject
Continuation []ContinuationState
}
// ToolResultPart is the result of an external computation, sent back.
type ToolResultPart struct {
ID string
Content []Part
Name string
IsError bool
Continuation []ContinuationState
}
// DataPart is structured data as content (changes/2026-09-17-judgments.md,
// D2). In a user/system message: structured input (a provider that reads
// JSON state takes Value as such; a text-only wire gets it as compact JSON
// text). In an assistant message: the answer to a json_schema request that
// declares judgments (MAP-14) — Value is the model's JSON object,
// Probabilities a distribution per judgment over its declared keys when one
// was measured, Method how (JudgmentMethod). Value is an opaque payload:
// any JSON value, verbatim (INV-002); nil is the JSON null.
type DataPart struct {
Value any
Probabilities map[string]map[string]float64
Method string
Continuation []ContinuationState
}
func (TextPart) Type() string { return PartTypeText }
func (ThinkingPart) Type() string { return PartTypeThinking }
func (RefusalPart) Type() string { return PartTypeRefusal }
func (CitationPart) Type() string { return PartTypeCitation }
func (ImagePart) Type() string { return PartTypeImage }
func (AudioPart) Type() string { return PartTypeAudio }
func (VideoPart) Type() string { return PartTypeVideo }
func (DocumentPart) Type() string { return PartTypeDocument }
func (BinaryPart) Type() string { return PartTypeBinary }
func (ToolCallPart) Type() string { return PartTypeToolCall }
func (ToolResultPart) Type() string { return PartTypeToolResult }
func (DataPart) Type() string { return PartTypeData }
func (TextPart) sealedPart() {}
func (ThinkingPart) sealedPart() {}
func (RefusalPart) sealedPart() {}
func (CitationPart) sealedPart() {}
func (ImagePart) sealedPart() {}
func (AudioPart) sealedPart() {}
func (VideoPart) sealedPart() {}
func (DocumentPart) sealedPart() {}
func (BinaryPart) sealedPart() {}
func (ToolCallPart) sealedPart() {}
func (ToolResultPart) sealedPart() {}
func (DataPart) sealedPart() {}
func (p TextPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p ThinkingPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p RefusalPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p CitationPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p ImagePart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p AudioPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p VideoPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p DocumentPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p BinaryPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p ToolCallPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p ToolResultPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p DataPart) ContinuationStates() []ContinuationState { return p.Continuation }
func (p TextPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p ThinkingPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p RefusalPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p CitationPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p ImagePart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p AudioPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p VideoPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p DocumentPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p BinaryPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p ToolCallPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p ToolResultPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
func (p DataPart) WithContinuation(c []ContinuationState) Part { p.Continuation = c; return p }
// Validate implements Part.
func (p TextPart) Validate() error { return validateContinuation(p.Continuation) }
// Validate implements Part.
func (p ThinkingPart) Validate() error { return validateContinuation(p.Continuation) }
// Validate implements Part (INV-016).
func (p RefusalPart) Validate() error {
if p.Text == "" {
return valueErrorf("RefusalPart.text cannot be empty")
}
return validateContinuation(p.Continuation)
}
// Validate implements Part (INV-017).
func (p CitationPart) Validate() error {
if p.URL == "" && p.Title == "" && p.Text == "" {
return valueErrorf("CitationPart requires at least one of url, title, or text")
}
return validateContinuation(p.Continuation)
}
var base64Re = regexp.MustCompile(`^[A-Za-z0-9+/]*={0,2}$`)
// base64Payload strips a data-URI prefix and embedded whitespace (INV-012).
func base64Payload(partType, data string) (string, error) {
if data == "" {
return "", valueErrorf("%s.data cannot be empty", partType)
}
if strings.HasPrefix(data, "data:") && strings.Contains(data, ";base64,") {
data = data[strings.Index(data, ";base64,")+len(";base64,"):]
}
if strings.ContainsAny(data, " \n\r\t\v\f") {
data = strings.Join(strings.Fields(data), "")
}
return data, nil
}
func validateBase64(partType, data string) error {
payload, err := base64Payload(partType, data)
if err != nil {
return err
}
if len(payload)%4 != 0 || !base64Re.MatchString(payload) {
return valueErrorf("%s.data must be a valid base64 string", partType)
}
return nil
}
func decodeBase64Payload(partType, data string) ([]byte, error) {
payload, err := base64Payload(partType, data)
if err != nil {
return nil, err
}
out, err := base64.StdEncoding.DecodeString(payload)
if err != nil {
return nil, valueErrorf("%s.data must be a valid base64 string", partType)
}
return out, nil
}
func (m Media) validate(partType string) error {
if m.MediaType == "" {
return valueErrorf("%s requires media_type", partType)
}
count := 0
for _, v := range []string{m.Data, m.URL, m.FileID, m.Path} {
if v != "" {
count++
}
}
if count != 1 {
return valueErrorf("%s requires exactly one of data, url, file_id, or path", partType)
}
if m.Data != "" {
if err := validateBase64(partType, m.Data); err != nil {
return err
}
}
return validateContinuation(m.Continuation)
}
// Bytes returns the media bytes: inline data decoded, or the path read now.
func (m Media) Bytes() ([]byte, error) {
if m.Data != "" {
return decodeBase64Payload("media", m.Data)
}
if m.Path != "" {
return os.ReadFile(m.Path)
}
return nil, valueErrorf("media part has no inline data or path; fetch url/file_id-addressed media before decoding")
}
// Base64 returns the media bytes as base64 (inline data verbatim, a path read now).
func (m Media) Base64() (string, error) {
if m.Data != "" {
return m.Data, nil
}
if m.Path != "" {
raw, err := os.ReadFile(m.Path)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(raw), nil
}
return "", valueErrorf("media part has no inline data or path")
}
// Validate implements Part.
func (p ImagePart) Validate() error {
if err := p.Media.validate("ImagePart"); err != nil {
return err
}
if p.Detail != "" && p.Detail != "low" && p.Detail != "high" && p.Detail != "auto" {
return valueErrorf("unsupported ImagePart.detail: %s", p.Detail)
}
return nil
}
// Validate implements Part.
func (p AudioPart) Validate() error { return p.Media.validate("AudioPart") }
// Validate implements Part.
func (p VideoPart) Validate() error { return p.Media.validate("VideoPart") }
// Validate implements Part.
func (p DocumentPart) Validate() error { return p.Media.validate("DocumentPart") }
// Validate implements Part.
func (p BinaryPart) Validate() error { return p.Media.validate("BinaryPart") }
// Validate implements Part.
func (p ToolCallPart) Validate() error {
if p.ID == "" {
return valueErrorf("ToolCallPart.id cannot be empty")
}
if p.Name == "" {
return valueErrorf("ToolCallPart.name cannot be empty")
}
if p.Input == nil {
return typeErrorf("input must be a JSON object")
}
if err := checkJSONObject(p.Input, "input", true); err != nil {
return err
}
return validateContinuation(p.Continuation)
}
// Validate implements Part (INV-013, INV-014).
func (p ToolResultPart) Validate() error {
if p.ID == "" {
return valueErrorf("ToolResultPart.id cannot be empty")
}
if len(p.Content) == 0 {
return valueErrorf("ToolResultPart requires content")
}
for _, c := range p.Content {
if c == nil {
return typeErrorf("ToolResultPart.content must contain Part objects")
}
if isToolResultForbidden(c) {
return typeErrorf("ToolResultPart.content cannot contain tool calls, nested tool results, thinking parts, or refusals")
}
if err := c.Validate(); err != nil {
return err
}
}
if err := validateInputDataParts("tool result", p.Content); err != nil {
return err
}
return validateContinuation(p.Continuation)
}
// Validate implements Part (INV-052: method iff probabilities; inner maps
// non-empty, floats in [0, 1]; the sum is not validated — providers round).
func (p DataPart) Validate() error {
if err := ValidateJSONValue(p.Value); err != nil {
return typeErrorf("DataPart.value must be a JSON-compatible value: %v", err)
}
if p.Probabilities != nil {
if err := validateProbabilities(p.Probabilities); err != nil {
return err
}
}
if p.Method != "" && !inVocab(p.Method, JudgmentMethods) {
return valueErrorf("DataPart.method must be one of %v, got %q", JudgmentMethods, p.Method)
}
if (p.Method == "") != (p.Probabilities == nil) {
return valueErrorf("DataPart.method is present iff DataPart.probabilities is (INV-052)")
}
return validateContinuation(p.Continuation)
}
func validateProbabilities(value map[string]map[string]float64) error {
if len(value) == 0 {
return typeErrorf("DataPart.probabilities must be a non-empty mapping of field -> {key: probability}")
}
for name, dist := range value {
if name == "" {
return typeErrorf("DataPart.probabilities keys must be non-empty strings")
}
if len(dist) == 0 {
return typeErrorf("DataPart.probabilities[%q] must be a non-empty mapping of key -> probability", name)
}
for key, prob := range dist {
if key == "" {
return typeErrorf("DataPart.probabilities[%q] keys must be non-empty strings", name)
}
if prob != prob || prob < 0 || prob > 1 {
return valueErrorf("DataPart.probabilities[%q][%q] must be in [0, 1]", name, key)
}
}
}
return nil
}
// validateInputDataParts is INV-052: a distribution is a claim about an
// answer; input carries value alone.
func validateInputDataParts(where string, parts []Part) error {
for _, p := range parts {
if d, ok := p.(DataPart); ok && d.Probabilities != nil {
return typeErrorf("%s data parts carry value only; probabilities belong to assistant messages (INV-052)", where)
}
}
return nil
}
func isToolResultForbidden(p Part) bool {
switch p.(type) {
case ToolCallPart, ToolResultPart, ThinkingPart, RefusalPart:
return true
}
return false
}
func isPromptForbidden(p Part) bool {
switch p.(type) {
case ToolCallPart, ToolResultPart, ThinkingPart, RefusalPart, CitationPart:
return true
}
return false
}
// IsMediaPart reports whether p carries bytes or an address rather than words.
func IsMediaPart(p Part) bool {
switch p.(type) {
case ImagePart, AudioPart, VideoPart, DocumentPart, BinaryPart:
return true
}
return false
}
// MediaOf returns the Media of a media part.
func MediaOf(p Part) (Media, bool) {
switch x := p.(type) {
case ImagePart:
return x.Media, true
case AudioPart:
return x.Media, true
case VideoPart:
return x.Media, true
case DocumentPart:
return x.Media, true
case BinaryPart:
return x.Media, true
}
return Media{}, false
}
// ─── Factories ───────────────────────────────────────────────────────
// Text creates a text part.
func Text(content string) TextPart { return TextPart{Text: content} }
// Thinking creates a thinking part.
func Thinking(content string) ThinkingPart { return ThinkingPart{Text: content} }
// Refusal creates a refusal part.
func Refusal(content string) RefusalPart { return RefusalPart{Text: content} }
// Citation creates a citation part.
func Citation(url, title, text string) CitationPart {
return CitationPart{URL: url, Title: title, Text: text}
}
// ToolCall creates a tool call part.
func ToolCall(id, name string, input JSONObject) ToolCallPart {
if input == nil {
input = JSONObject{}
}
return ToolCallPart{ID: id, Name: name, Input: input}
}
// ToolResult creates a tool result carrying one text part.
func ToolResult(id, output string) ToolResultPart {
return ToolResultPart{ID: id, Content: []Part{Text(output)}}
}
// ToolResultParts creates a tool result from parts.
func ToolResultParts(id string, content ...Part) ToolResultPart {
return ToolResultPart{ID: id, Content: content}
}
// Data creates a data part (structured input, or a judged answer).
func Data(value any) DataPart { return DataPart{Value: value} }
// DataWithProbabilities creates a judged answer carrying its distributions.
func DataWithProbabilities(value any, probabilities map[string]map[string]float64, method string) DataPart {
return DataPart{Value: value, Probabilities: probabilities, Method: method}
}
// ToolError creates an error tool result.
func ToolError(id, output string) ToolResultPart {
return ToolResultPart{ID: id, Content: []Part{Text(output)}, IsError: true}
}
// MediaOption configures a media factory.
type MediaOption func(*Media)
// WithURL addresses the media by URL.
func WithURL(url string) MediaOption { return func(m *Media) { m.URL = url } }
// WithFileID addresses the media by a provider file id.
func WithFileID(id string) MediaOption { return func(m *Media) { m.FileID = id } }
// WithPath addresses the media by a local path (read at request time).
func WithPath(path string) MediaOption { return func(m *Media) { m.Path = path } }
// WithData embeds raw bytes (base64-encoded).
func WithData(data []byte) MediaOption {
return func(m *Media) { m.Data = base64.StdEncoding.EncodeToString(data) }
}
// WithBase64 embeds an already-encoded base64 string (or data URI).
func WithBase64(data string) MediaOption { return func(m *Media) { m.Data = data } }
// WithMediaType sets the MIME type.
func WithMediaType(mediaType string) MediaOption { return func(m *Media) { m.MediaType = mediaType } }
func buildMedia(defaultType string, opts []MediaOption) Media {
var m Media
for _, o := range opts {
o(&m)
}
if m.MediaType == "" && m.Path != "" {
if guessed := mime.TypeByExtension(strings.ToLower(filepath.Ext(m.Path))); guessed != "" {
m.MediaType, _, _ = strings.Cut(guessed, ";")
}
}
if m.MediaType == "" {
m.MediaType = defaultType
}
return m
}
// Image creates an image part (default media type image/png).
func Image(opts ...MediaOption) ImagePart { return ImagePart{Media: buildMedia("image/png", opts)} }
// Audio creates an audio part (default media type audio/wav).
func Audio(opts ...MediaOption) AudioPart { return AudioPart{Media: buildMedia("audio/wav", opts)} }
// Video creates a video part (default media type video/mp4).
func Video(opts ...MediaOption) VideoPart { return VideoPart{Media: buildMedia("video/mp4", opts)} }
// Document creates a document part (default media type application/pdf).
func Document(opts ...MediaOption) DocumentPart {
return DocumentPart{Media: buildMedia("application/pdf", opts)}
}
// Binary creates a binary part (default media type application/octet-stream).
func Binary(opts ...MediaOption) BinaryPart {
return BinaryPart{Media: buildMedia("application/octet-stream", opts)}
}
// partText returns the text of a text-bearing part ("" otherwise).
func partText(p Part) string {
switch x := p.(type) {
case TextPart:
return x.Text
case ThinkingPart:
return x.Text
case RefusalPart:
return x.Text
case CitationPart:
return x.Text
}
return ""
}