import { createTypedValidator } from '@takeshape/json-schema';
import omit from 'lodash/omit.js';
import * as agentMessageInput from "./agent-message-input.js";
import * as agentMessageOutput from "./agent-message-output.js";
export const shapeName = 'TSAgentMessage';
export const schema = {
    $id: 'https://takeshape.io/schema/agent-message-payload',
    type: 'object',
    properties: {
        messageId: {
            type: 'string',
            description: 'ID of the message that was deferred'
        },
        sessionId: {
            type: 'string',
            description: 'ID of the session this message belongs to'
        },
        projectId: {
            type: 'string',
            description: 'Project ID of the project this message belongs to'
        },
        schemaId: {
            type: 'string',
            description: 'Schema ID this message was created by'
        },
        agentName: {
            type: 'string',
            description: 'Name of the agent this message was created by'
        },
        args: {
            type: 'object'
        },
        input: {
            ...agentMessageInput.schema
        },
        output: {
            ...agentMessageOutput.schema
        },
        error: {
            type: 'object',
            properties: {
                message: { type: 'string' },
                name: { type: 'string' },
                code: { type: 'string' }
            },
            additionalProperties: false
        },
        runMode: {
            enum: ['BACKGROUND', 'IMMEDIATE'],
            description: 'Indicates how the agent message was run'
        },
        abortedAt: {
            type: 'number',
            description: 'Timestamp when the message was aborted'
        },
        sentAt: {
            type: 'number',
            description: 'Timestamp when the message was sent'
        },
        startedAt: {
            type: 'number',
            description: 'Timestamp when the message started processing'
        },
        finishedAt: {
            type: 'number',
            description: 'Timestamp when the message finished processing'
        },
        span: {
            type: 'object',
            description: 'The span for the agent run associated with this message',
            properties: {
                traceId: {
                    type: 'string'
                },
                spanId: {
                    type: 'string'
                },
                name: {
                    type: 'string'
                },
                createdAt: {
                    type: 'number'
                }
            },
            required: ['traceId', 'spanId', 'name', 'createdAt'],
            additionalProperties: false
        },
        feedback: {
            type: 'object',
            properties: {
                name: {
                    type: 'string',
                    description: 'Name of the feedback, e.g., "Quality", "Relevance"'
                },
                label: {
                    type: 'string',
                    description: 'String feedback for the agent message, e.g., "helpful", "not helpful". If a label is provided, a score must not be provided.'
                },
                score: {
                    type: 'number',
                    description: 'Numeric feedback for the agent message, typically between 1 and 5. If a score is provided, a label must not be provided.'
                },
                text: {
                    type: 'string',
                    description: 'Freeform text feedback for the agent message.'
                },
                note: {
                    type: 'string',
                    description: 'Optional note for the feedback, e.g., "This was helpful because..."'
                },
                updatedBy: {
                    type: 'string',
                    description: 'A string identifying the user who is adding the annotation, such as a name or email address.'
                },
                createdAt: {
                    type: 'number',
                    description: 'Timestamp when the feedback was created'
                }
            },
            additionalProperties: false,
            required: ['name']
        },
        blocked: {
            type: 'boolean',
            description: 'If this message was blocked by a guardrail'
        },
        usage: {
            type: 'object',
            description: 'LLM token usage for this message',
            properties: {
                completionTokens: { type: 'number' },
                promptTokens: { type: 'number' },
                totalTokens: { type: 'number' },
                guardrailUnits: { type: 'number' }
            },
            additionalProperties: false
        }
    },
    required: ['messageId', 'sessionId', 'agentName', 'projectId', 'schemaId', 'runMode', 'sentAt'],
    additionalProperties: false
};
export const validate = createTypedValidator(schema);
const { sessionId, projectId, schemaId, ...shapeSchemaProperties } = schema.properties;
export const shapeSchema = {
    title: shapeName,
    ...omit(schema, ['$id']),
    properties: {
        ...shapeSchemaProperties,
        input: {
            '@ref': agentMessageInput.shapeName
        },
        output: {
            '@ref': agentMessageOutput.shapeName
        }
    }
};
