|
| 1 | +/* |
| 2 | + * Copyright 2025 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package ark |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/cloudwego/eino/schema" |
| 23 | +) |
| 24 | + |
| 25 | +type blockExtraItemID string |
| 26 | +type blockExtraItemStatus string |
| 27 | + |
| 28 | +const ( |
| 29 | + videoURLFPS = "ark-user-input-video-url-fps" |
| 30 | + itemIDKey = "ark-item-id" |
| 31 | + itemStatusKey = "ark-item-status" |
| 32 | +) |
| 33 | + |
| 34 | +func SetUserInputVideoFPS(block *schema.UserInputVideo, fps float64) { |
| 35 | + setBlockExtraValue(schema.NewContentBlock(block), videoURLFPS, fps) |
| 36 | +} |
| 37 | + |
| 38 | +func GetUserInputVideoFPS(block *schema.UserInputVideo) (float64, bool) { |
| 39 | + return getBlockExtraValue[float64](schema.NewContentBlock(block), videoURLFPS) |
| 40 | +} |
| 41 | + |
| 42 | +func setItemID(block *schema.ContentBlock, itemID string) { |
| 43 | + setBlockExtraValue(block, itemIDKey, blockExtraItemID(itemID)) |
| 44 | +} |
| 45 | + |
| 46 | +func getItemID(block *schema.ContentBlock) (string, bool) { |
| 47 | + itemID, ok := getBlockExtraValue[blockExtraItemID](block, itemIDKey) |
| 48 | + if !ok { |
| 49 | + return "", false |
| 50 | + } |
| 51 | + return string(itemID), true |
| 52 | +} |
| 53 | + |
| 54 | +func setItemStatus(block *schema.ContentBlock, status string) { |
| 55 | + setBlockExtraValue(block, itemStatusKey, blockExtraItemStatus(status)) |
| 56 | +} |
| 57 | + |
| 58 | +func GetItemStatus(block *schema.ContentBlock) (string, bool) { |
| 59 | + itemStatus, ok := getBlockExtraValue[blockExtraItemStatus](block, itemStatusKey) |
| 60 | + if !ok { |
| 61 | + return "", false |
| 62 | + } |
| 63 | + return string(itemStatus), true |
| 64 | +} |
| 65 | + |
| 66 | +func setBlockExtraValue[T any](block *schema.ContentBlock, key string, value T) { |
| 67 | + if block == nil { |
| 68 | + return |
| 69 | + } |
| 70 | + if block.Extra == nil { |
| 71 | + block.Extra = map[string]any{} |
| 72 | + } |
| 73 | + block.Extra[key] = value |
| 74 | +} |
| 75 | + |
| 76 | +func getBlockExtraValue[T any](block *schema.ContentBlock, key string) (T, bool) { |
| 77 | + var zero T |
| 78 | + if block == nil { |
| 79 | + return zero, false |
| 80 | + } |
| 81 | + if block.Extra == nil { |
| 82 | + return zero, false |
| 83 | + } |
| 84 | + val, ok := block.Extra[key].(T) |
| 85 | + if !ok { |
| 86 | + return zero, false |
| 87 | + } |
| 88 | + return val, true |
| 89 | +} |
| 90 | + |
| 91 | +func concatFirstNonZero[T any](chunks []T) (T, error) { |
| 92 | + for _, chunk := range chunks { |
| 93 | + if !reflect.ValueOf(chunk).IsZero() { |
| 94 | + return chunk, nil |
| 95 | + } |
| 96 | + } |
| 97 | + var zero T |
| 98 | + return zero, nil |
| 99 | +} |
| 100 | + |
| 101 | +func concatFirst[T any](chunks []T) (T, error) { |
| 102 | + if len(chunks) == 0 { |
| 103 | + var zero T |
| 104 | + return zero, nil |
| 105 | + } |
| 106 | + return chunks[0], nil |
| 107 | +} |
| 108 | + |
| 109 | +func concatLast[T any](chunks []T) (T, error) { |
| 110 | + if len(chunks) == 0 { |
| 111 | + var zero T |
| 112 | + return zero, nil |
| 113 | + } |
| 114 | + return chunks[len(chunks)-1], nil |
| 115 | +} |
0 commit comments