|
| 1 | +/* |
| 2 | + * Copyright 2003 Doug Mealing LLC dba Meta Objects |
| 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 | + * http://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 | +package com.metaobjects.loader.ai; |
| 17 | + |
| 18 | +import com.metaobjects.MetaData; |
| 19 | +import com.metaobjects.attr.StringAttribute; |
| 20 | +import com.metaobjects.field.MetaField; |
| 21 | +import com.metaobjects.field.ObjectField; |
| 22 | +import com.metaobjects.loader.MetaDataLoader; |
| 23 | +import com.metaobjects.object.MetaObject; |
| 24 | +import com.metaobjects.template.PromptTemplate; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +/** |
| 29 | + * AI-trace pre-freeze pass: inject typed {@code voRequest}/{@code voResponse} |
| 30 | + * {@code field.object} columns onto entities that extend {@code LlmCallBase} and |
| 31 | + * carry a nested {@code template.prompt} with {@code @payloadRef}/{@code @responseRef}. |
| 32 | + * |
| 33 | + * <p>Cross-port mirror of the TypeScript reference |
| 34 | + * ({@code codegen-ts/src/ai/derive-trace-fields.ts}). The TS reference wires this |
| 35 | + * as a codegen-only {@code preFreeze} hook because the TS runtime persists via a |
| 36 | + * direct row-write (no runtime metadata needed). The Java OMDB runtime is |
| 37 | + * <em>metadata-driven</em> — the generated {@code record<Entity>} helper calls |
| 38 | + * {@code setObject("voResponse", ...)} which OMDB maps to a jsonb column by reading |
| 39 | + * the <em>runtime-loaded</em> MetaObject. So in Java this derivation must also reach |
| 40 | + * the runtime load path; it is therefore exposed as a {@link MetaDataLoader} |
| 41 | + * {@code preFreeze} hook usable by BOTH codegen and runtime loaders.</p> |
| 42 | + * |
| 43 | + * <p>The injected fields carry {@code @objectRef} + {@code @storage="jsonb"} so the |
| 44 | + * existing owned-object typed-jsonb codec path handles them — identical to a |
| 45 | + * hand-authored {@code field.object}. The pass is idempotent: an own field of the |
| 46 | + * same name is left untouched, so explicit authoring still wins.</p> |
| 47 | + */ |
| 48 | +public final class LlmTraceFieldDeriver { |
| 49 | + |
| 50 | + /** Short name of the shipped abstract base every trace entity extends. */ |
| 51 | + public static final String LLM_CALL_BASE = "LlmCallBase"; |
| 52 | + |
| 53 | + /** Derived field name for the typed request payload VO. */ |
| 54 | + public static final String VO_REQUEST = "voRequest"; |
| 55 | + |
| 56 | + /** Derived field name for the typed extracted-response VO. */ |
| 57 | + public static final String VO_RESPONSE = "voResponse"; |
| 58 | + |
| 59 | + /** {@code @storage} value selecting the typed-jsonb owned-object codec. */ |
| 60 | + private static final String STORAGE_JSONB = "jsonb"; |
| 61 | + |
| 62 | + private LlmTraceFieldDeriver() {} |
| 63 | + |
| 64 | + /** |
| 65 | + * For every concrete entity in {@code loader} that (1) extends |
| 66 | + * {@code LlmCallBase} (directly or transitively) and (2) has an own |
| 67 | + * {@code template.prompt} carrying {@code @payloadRef}/{@code @responseRef}, |
| 68 | + * inject {@code field.object} children named {@code voRequest}/{@code voResponse} |
| 69 | + * (respectively) with {@code @storage="jsonb"}. Idempotent. |
| 70 | + * |
| 71 | + * <p>Designed to be passed to {@link MetaDataLoader#setPreFreeze}.</p> |
| 72 | + */ |
| 73 | + public static void deriveTraceFields(MetaDataLoader loader) { |
| 74 | + for (MetaObject obj : loader.getChildren(MetaObject.class)) { |
| 75 | + if (!extendsBase(obj, LLM_CALL_BASE)) continue; |
| 76 | + |
| 77 | + PromptTemplate prompt = findOwnPrompt(obj); |
| 78 | + if (prompt == null) continue; |
| 79 | + |
| 80 | + String payloadRef = prompt.getPayloadRef(); |
| 81 | + String responseRef = prompt.getResponseRef(); |
| 82 | + |
| 83 | + if (payloadRef != null && !payloadRef.isEmpty()) { |
| 84 | + injectObjField(obj, VO_REQUEST, payloadRef); |
| 85 | + } |
| 86 | + if (responseRef != null && !responseRef.isEmpty()) { |
| 87 | + injectObjField(obj, VO_RESPONSE, responseRef); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** Walk the resolved super chain for a node whose short name equals {@code baseName}. */ |
| 93 | + private static boolean extendsBase(MetaData obj, String baseName) { |
| 94 | + MetaData cur = obj.getSuperData(); |
| 95 | + while (cur != null) { |
| 96 | + if (baseName.equals(cur.getShortName())) return true; |
| 97 | + cur = cur.getSuperData(); |
| 98 | + } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /** First OWN {@code template.prompt} child of {@code obj}, or {@code null}. */ |
| 103 | + private static PromptTemplate findOwnPrompt(MetaObject obj) { |
| 104 | + List<PromptTemplate> prompts = obj.getChildren(PromptTemplate.class, false); |
| 105 | + return prompts.isEmpty() ? null : prompts.get(0); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Inject a {@code field.object} child onto {@code entity} with {@code @objectRef} |
| 110 | + * + {@code @storage="jsonb"}, unless an own field of that name already exists. |
| 111 | + */ |
| 112 | + private static void injectObjField(MetaObject entity, String fieldName, String objectRef) { |
| 113 | + for (MetaField existing : entity.getChildren(MetaField.class, false)) { |
| 114 | + if (fieldName.equals(existing.getShortName())) return; // idempotent |
| 115 | + } |
| 116 | + ObjectField f = new ObjectField(fieldName); |
| 117 | + f.addChild(StringAttribute.create(ObjectField.ATTR_OBJECTREF, objectRef)); |
| 118 | + f.addChild(StringAttribute.create(ObjectField.ATTR_STORAGE, STORAGE_JSONB)); |
| 119 | + entity.addChild(f); |
| 120 | + } |
| 121 | +} |
0 commit comments