// The MIT License
//
// Copyright (c) 2024 Temporal Technologies Inc.  All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

syntax = "proto3";

package temporal.api.sdk.v1;

option go_package = "go.temporal.io/api/sdk/v1;sdk";
option java_package = "io.temporal.api.sdk.v1";
option java_multiple_files = true;
option java_outer_classname = "EnhancedStackTraceProto";
option ruby_package = "Temporalio::Api::Sdk::V1";
option csharp_namespace = "Temporalio.Api.Sdk.V1";

// Internal structure used to create worker stack traces with references to code.
message EnhancedStackTrace {
    // Information pertaining to the SDK that the trace has been captured from.
    StackTraceSDKInfo sdk = 1;

    // Mapping of file path to file contents.
    map<string, StackTraceFileSlice> sources = 2;

    // Collection of stacks captured.
    repeated StackTrace stacks = 3;
}

// Information pertaining to the SDK that the trace has been captured from.
// (-- api-linter: core::0123::resource-annotation=disabled
//     aip.dev/not-precedent: Naming SDK version is optional. --)
message StackTraceSDKInfo {
    // Name of the SDK
    string name = 1;

    // Version string of the SDK
    string version = 2;
}

// "Slice" of a file starting at line_offset -- a line offset and code fragment corresponding to the worker's stack.
message StackTraceFileSlice {
    // Only used (possibly) to trim the file without breaking syntax highlighting. This is not optional, unlike
    // the `line` property of a `StackTraceFileLocation`.
    // (-- api-linter: core::0141::forbidden-types=disabled
    //     aip.dev/not-precedent: These really shouldn't have negative values. --)
    uint32 line_offset = 1;

    // Slice of a file with the respective OS-specific line terminator.
    string content = 2;
}

// More specific location details of a file: its path, precise line and column numbers if applicable, and function name if available.
// In essence, a pointer to a location in a file
message StackTraceFileLocation {
    // Path to source file (absolute or relative).
    // If the paths are relative, ensure that they are all relative to the same root.
    string file_path = 1;

    // Optional; If possible, SDK should send this -- this is required for displaying the code location.
    // If not provided, set to -1.
    int32 line = 2;

    // Optional; if possible, SDK should send this.
    // If not provided, set to -1.
    int32 column = 3;

    // Function name this line belongs to, if applicable.
    // Used for falling back to stack trace view.
    string function_name = 4;
    
    // Flag to communicate whether a location should be hidden by default in the stack view.
    bool internal_code = 5;
}

// Collection of FileLocation messages from a single stack.
message StackTrace {
    // Collection of `FileLocation`s, each for a stack frame that comprise a stack trace.
    repeated StackTraceFileLocation locations = 1;
}
