Class: PromptObjects::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime:) ⇒ Service

Returns a new instance of Service.



29
30
31
32
33
34
35
36
# File 'lib/prompt_objects/service.rb', line 29

def initialize(runtime:)
  @runtime = runtime
  @name = self.class.name.split("::").last
               .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
               .gsub(/([a-z\d])([A-Z])/, '\1_\2')
               .downcase
  @description = nil
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



27
28
29
# File 'lib/prompt_objects/service.rb', line 27

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/prompt_objects/service.rb', line 27

def name
  @name
end

Instance Method Details

#delete_env_data(key) ⇒ Boolean

Delete an env_data entry.

Parameters:

  • key (String)

    Data key

Returns:

  • (Boolean)

    True if a row was deleted



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/prompt_objects/service.rb', line 104

def delete_env_data(key)
  scope = env_data_scope
  return false unless scope

  deleted = @runtime.session_store.delete_env_data(root_thread_id: scope, key: key)
  if deleted
    @runtime.notify_env_data_changed(
      action: "delete", root_thread_id: scope, key: key, stored_by: "service:#{name}"
    )
  end
  deleted
end

#get_env_data(key) ⇒ Object?

Read a value from shared env_data.

Parameters:

  • key (String)

    Data key

Returns:

  • (Object, nil)

    The stored value, or nil if absent



84
85
86
87
88
89
90
# File 'lib/prompt_objects/service.rb', line 84

def get_env_data(key)
  scope = env_data_scope
  return nil unless scope

  entry = @runtime.session_store.get_env_data(root_thread_id: scope, key: key)
  entry && entry[:value]
end

#intervalObject

Seconds between ticks. Return nil to disable auto-tick.



45
# File 'lib/prompt_objects/service.rb', line 45

def interval; nil; end

#list_env_dataArray<Hash>

List env_data keys and descriptions in the active environment session.

Returns:

  • (Array<Hash>)

    Entries with :key and :short_description



94
95
96
97
98
99
# File 'lib/prompt_objects/service.rb', line 94

def list_env_data
  scope = env_data_scope
  return [] unless scope

  @runtime.session_store.list_env_data(root_thread_id: scope)
end

#set_env_data(key, value, short_description: nil) ⇒ Boolean

Store a value in shared env_data, scoped to the active environment session. Creates the environment session on first write if one doesn't exist yet.

Parameters:

  • key (String)

    Data key

  • value (Object)

    JSON-serializable value

  • short_description (String, nil) (defaults to: nil)

    Summary for discoverability

Returns:

  • (Boolean)

    True if stored



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prompt_objects/service.rb', line 64

def set_env_data(key, value, short_description: nil)
  scope = env_data_scope(create: true)
  return false unless scope

  @runtime.session_store.store_env_data(
    root_thread_id: scope,
    key: key,
    short_description: short_description || key.to_s,
    value: value,
    stored_by: "service:#{name}"
  )
  @runtime.notify_env_data_changed(
    action: "set", root_thread_id: scope, key: key, stored_by: "service:#{name}"
  )
  true
end

#startObject

Called when the server starts. Override to initialize resources.



39
# File 'lib/prompt_objects/service.rb', line 39

def start; end

#stopObject

Called when the server stops. Override to clean up resources.



42
# File 'lib/prompt_objects/service.rb', line 42

def stop; end

#tickObject

Called every interval seconds. Override to perform periodic work.



48
# File 'lib/prompt_objects/service.rb', line 48

def tick; end