96 lines
3.4 KiB
Go
96 lines
3.4 KiB
Go
package codex
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"sort"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// Standard MCP elicitation uses the same typed question controls. Extended
|
|
// OpenAI forms are not advertised by initialize and fail explicitly if received.
|
|
func (c *Chat) receiveElicitation(msg rpcMessage) {
|
|
var p struct {
|
|
ThreadID string `json:"threadId"`
|
|
Mode string `json:"mode"`
|
|
Message string `json:"message"`
|
|
URL string `json:"url"`
|
|
Schema struct {
|
|
Type string `json:"type"`
|
|
Properties map[string]json.RawMessage `json:"properties"`
|
|
Required []string `json:"required"`
|
|
} `json:"requestedSchema"`
|
|
}
|
|
if json.Unmarshal(msg.Params, &p) != nil || p.ThreadID == "" || (p.Mode != "form" && p.Mode != "url") {
|
|
c.respondUnsupported(msg.ID)
|
|
return
|
|
}
|
|
form := protocol.ChatForm{ID: "mcp_" + base64.RawURLEncoding.EncodeToString(msg.ID), SessionID: p.ThreadID, Kind: "elicitation", Title: p.Message, Actions: []string{"accept", "decline", "cancel"}}
|
|
if p.Mode == "url" {
|
|
form.Kind = "elicitation-url"
|
|
form.Fields = []protocol.ChatFormField{{Key: "external", Type: "external", URL: p.URL, Title: "Continue in the external form"}}
|
|
} else {
|
|
if p.Schema.Type != "object" || p.Schema.Properties == nil {
|
|
c.respondUnsupported(msg.ID)
|
|
return
|
|
}
|
|
required := map[string]bool{}
|
|
for _, key := range p.Schema.Required {
|
|
required[key] = true
|
|
}
|
|
keys := make([]string, 0, len(p.Schema.Properties))
|
|
for key := range p.Schema.Properties {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
for _, key := range keys {
|
|
var field struct {
|
|
Type string `json:"type"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
Enum []string `json:"enum"`
|
|
Default any `json:"default"`
|
|
Minimum any `json:"minimum"`
|
|
Maximum any `json:"maximum"`
|
|
MinLength *int `json:"minLength"`
|
|
MaxLength *int `json:"maxLength"`
|
|
MinItems *int `json:"minItems"`
|
|
MaxItems *int `json:"maxItems"`
|
|
Format string `json:"format"`
|
|
Items *struct {
|
|
Type string `json:"type"`
|
|
Enum []string `json:"enum"`
|
|
} `json:"items"`
|
|
}
|
|
if json.Unmarshal(p.Schema.Properties[key], &field) != nil {
|
|
c.respondUnsupported(msg.ID)
|
|
return
|
|
}
|
|
out := protocol.ChatFormField{Key: key, Type: field.Type, Title: field.Title, Description: field.Description, Required: required[key], Default: field.Default, Minimum: field.Minimum, Maximum: field.Maximum, MinLength: field.MinLength, MaxLength: field.MaxLength, MinItems: field.MinItems, MaxItems: field.MaxItems, Format: field.Format}
|
|
switch field.Type {
|
|
case "string", "number", "integer", "boolean":
|
|
case "array":
|
|
if field.Items == nil || field.Items.Type != "string" {
|
|
c.respondUnsupported(msg.ID)
|
|
return
|
|
}
|
|
out.Type = "multiselect"
|
|
field.Enum = field.Items.Enum
|
|
out.Custom = len(field.Enum) == 0
|
|
default:
|
|
c.respondUnsupported(msg.ID)
|
|
return
|
|
}
|
|
for _, value := range field.Enum {
|
|
out.Options = append(out.Options, protocol.ChatFormOption{Value: value, Label: value})
|
|
}
|
|
form.Fields = append(form.Fields, out)
|
|
}
|
|
}
|
|
c.mu.Lock()
|
|
c.forms[form.ID] = pendingForm{requestID: msg.ID, threadID: p.ThreadID, form: form, elicitation: true}
|
|
c.mu.Unlock()
|
|
c.emit(protocol.ChatEventFrame{SessionID: p.ThreadID, Kind: protocol.ChatEventForm, Form: &form})
|
|
}
|