lex-microsoft_teams 0.6.16 → 0.6.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +7 -0
- data/.github/dependabot.yml +18 -0
- data/.github/workflows/ci.yml +20 -2
- data/CHANGELOG.md +16 -2
- data/LICENSE +201 -21
- data/lib/legion/extensions/microsoft_teams/helpers/prompt_resolver.rb +3 -1
- data/lib/legion/extensions/microsoft_teams/helpers/session_manager.rb +19 -0
- data/lib/legion/extensions/microsoft_teams/helpers/token_cache.rb +2 -4
- data/lib/legion/extensions/microsoft_teams/helpers/trace_retriever.rb +136 -0
- data/lib/legion/extensions/microsoft_teams/runners/bot.rb +20 -5
- data/lib/legion/extensions/microsoft_teams/version.rb +1 -1
- data/lib/legion/extensions/microsoft_teams.rb +1 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3194ba9caa2dc94e68a50f5d0d8dfc18d04c9cb062b982c2134c702e99f38c04
|
|
4
|
+
data.tar.gz: 98a39387206662e79a5523227f7995df3c8d6dc342444f01530611cc60c05c3f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 32d7a9eb1834408e2e9101ef19f141ec189868a0ad630cbc03add34962351435e413911c2f3c8108d7d14f6274791f02b442903146dd79da605cdc72968edd76
|
|
7
|
+
data.tar.gz: db5782a259711c24a6cfd3bdeff0d812a5bf70da1d7c8b9ab55d9c0a11781fbbb3ab494b9f58936bdba031a2283aff95012c3cb0d0dbbfa93fb62adb6de50521
|
data/.github/CODEOWNERS
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: bundler
|
|
4
|
+
directory: /
|
|
5
|
+
schedule:
|
|
6
|
+
interval: weekly
|
|
7
|
+
day: monday
|
|
8
|
+
open-pull-requests-limit: 5
|
|
9
|
+
labels:
|
|
10
|
+
- "type:dependencies"
|
|
11
|
+
- package-ecosystem: github-actions
|
|
12
|
+
directory: /
|
|
13
|
+
schedule:
|
|
14
|
+
interval: weekly
|
|
15
|
+
day: monday
|
|
16
|
+
open-pull-requests-limit: 5
|
|
17
|
+
labels:
|
|
18
|
+
- "type:dependencies"
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -3,14 +3,32 @@ on:
|
|
|
3
3
|
push:
|
|
4
4
|
branches: [main]
|
|
5
5
|
pull_request:
|
|
6
|
+
schedule:
|
|
7
|
+
- cron: '0 9 * * 1'
|
|
6
8
|
|
|
7
9
|
jobs:
|
|
8
10
|
ci:
|
|
9
11
|
uses: LegionIO/.github/.github/workflows/ci.yml@main
|
|
10
12
|
|
|
13
|
+
lint:
|
|
14
|
+
uses: LegionIO/.github/.github/workflows/lint-patterns.yml@main
|
|
15
|
+
|
|
16
|
+
security:
|
|
17
|
+
uses: LegionIO/.github/.github/workflows/security-scan.yml@main
|
|
18
|
+
|
|
19
|
+
version-changelog:
|
|
20
|
+
uses: LegionIO/.github/.github/workflows/version-changelog.yml@main
|
|
21
|
+
|
|
22
|
+
dependency-review:
|
|
23
|
+
uses: LegionIO/.github/.github/workflows/dependency-review.yml@main
|
|
24
|
+
|
|
25
|
+
stale:
|
|
26
|
+
if: github.event_name == 'schedule'
|
|
27
|
+
uses: LegionIO/.github/.github/workflows/stale.yml@main
|
|
28
|
+
|
|
11
29
|
release:
|
|
12
|
-
needs: ci
|
|
30
|
+
needs: [ci, lint]
|
|
13
31
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
14
32
|
uses: LegionIO/.github/.github/workflows/release.yml@main
|
|
15
33
|
secrets:
|
|
16
|
-
rubygems-api-key: ${{ secrets.RUBYGEMS_API_KEY }}
|
|
34
|
+
rubygems-api-key: ${{ secrets.RUBYGEMS_API_KEY }}
|
data/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## [
|
|
3
|
+
## [0.6.18] - 2026-03-26
|
|
4
4
|
|
|
5
5
|
### Changed
|
|
6
|
-
-
|
|
6
|
+
- `TokenCache` Vault path is now per-user (`{USER}/microsoft_teams/delegated_token`) instead of hardcoded `legionio/microsoft_teams/delegated_token`
|
|
7
|
+
|
|
8
|
+
## [0.6.17] - 2026-03-24
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- `Helpers::TraceRetriever` module: retrieves memory traces from the shared store at query time and formats them as LLM context (sender, teams, and chat-scoped domains; 2000-token budget; strength-ranked deduplication)
|
|
12
|
+
- `Bot#retrieve_trace_context` private method wires TraceRetriever into the handle_message flow
|
|
13
|
+
- `Bot#handle_message` now retrieves trace context before generating a response and passes it through to `generate_response` and `llm_respond`
|
|
14
|
+
- `SessionManager#get_or_create` seeds new sessions with profile traces for the owner via `trace_seed_for`
|
|
15
|
+
- `PromptResolver#resolve_prompt` accepts optional `trace_context:` keyword and appends it after preference instructions
|
|
16
|
+
- Comprehensive specs for TraceRetriever (token budget, rank/dedup, age labels, graceful degradation)
|
|
17
|
+
- Bot specs updated to verify trace context retrieval and pass-through, and nil/graceful-degradation paths
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
- Add `caller:` identity to `llm_chat` calls in bot and profile_ingest runners for pipeline attribution
|
|
7
21
|
|
|
8
22
|
## [0.6.15] - 2026-03-23
|
|
9
23
|
|
data/LICENSE
CHANGED
|
@@ -1,21 +1,201 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2021 Esity
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -5,7 +5,7 @@ module Legion
|
|
|
5
5
|
module MicrosoftTeams
|
|
6
6
|
module Helpers
|
|
7
7
|
module PromptResolver
|
|
8
|
-
def resolve_prompt(mode:, conversation_id:, owner_id: nil)
|
|
8
|
+
def resolve_prompt(mode:, conversation_id:, owner_id: nil, trace_context: nil)
|
|
9
9
|
settings = teams_settings
|
|
10
10
|
base = settings.dig(:bot, :system_prompt) || ''
|
|
11
11
|
|
|
@@ -18,6 +18,8 @@ module Legion
|
|
|
18
18
|
pref_instructions = preference_instructions_for(owner_id: owner_id)
|
|
19
19
|
prompt = "#{prompt}\n\n#{pref_instructions}" if pref_instructions
|
|
20
20
|
|
|
21
|
+
prompt = "#{prompt}\n\n#{trace_context}" if trace_context && !trace_context.empty?
|
|
22
|
+
|
|
21
23
|
prompt
|
|
22
24
|
end
|
|
23
25
|
|
|
@@ -35,6 +35,9 @@ module Legion
|
|
|
35
35
|
llm_config: resolve_llm_config(mode: mode, conversation_id: conversation_id, owner_id: owner_id)
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
seed = trace_seed_for(owner_id: owner_id)
|
|
39
|
+
session[:trace_seed] = seed if seed
|
|
40
|
+
|
|
38
41
|
@sessions[conversation_id] = session
|
|
39
42
|
end
|
|
40
43
|
|
|
@@ -134,6 +137,22 @@ module Legion
|
|
|
134
137
|
@memory_runner ||= Object.new.extend(Legion::Extensions::Agentic::Memory::Trace::Runners::Traces)
|
|
135
138
|
end
|
|
136
139
|
|
|
140
|
+
def trace_seed_for(owner_id:)
|
|
141
|
+
return nil unless defined?(Legion::Extensions::Agentic::Memory::Trace) &&
|
|
142
|
+
Legion::Extensions::Agentic::Memory::Trace.respond_to?(:shared_store)
|
|
143
|
+
|
|
144
|
+
store = Legion::Extensions::Agentic::Memory::Trace.shared_store
|
|
145
|
+
return nil unless store
|
|
146
|
+
|
|
147
|
+
profile_traces = store.retrieve_by_domain("sender:#{owner_id}", min_strength: 0.3, limit: 5)
|
|
148
|
+
return nil if profile_traces.empty?
|
|
149
|
+
|
|
150
|
+
profile_traces.map { |t| { type: t[:trace_type], content: t[:content_payload].to_s[0, 200] } }
|
|
151
|
+
rescue StandardError => e
|
|
152
|
+
log.debug("trace_seed_for failed: #{e.message}") if defined?(log) && log.respond_to?(:debug)
|
|
153
|
+
nil
|
|
154
|
+
end
|
|
155
|
+
|
|
137
156
|
def settings_val(key, default)
|
|
138
157
|
if defined?(Legion::Settings) && Legion::Settings.dig(:microsoft_teams, :bot, :session, key)
|
|
139
158
|
Legion::Settings[:microsoft_teams][:bot][:session][key]
|
|
@@ -14,7 +14,6 @@ module Legion
|
|
|
14
14
|
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
15
15
|
|
|
16
16
|
REFRESH_BUFFER = 60
|
|
17
|
-
DEFAULT_VAULT_PATH = 'legionio/microsoft_teams/delegated_token'
|
|
18
17
|
DEFAULT_LOCAL_DIR = File.join(Dir.home, '.legionio', 'tokens')
|
|
19
18
|
DEFAULT_LOCAL_FILE = File.join(DEFAULT_LOCAL_DIR, 'microsoft_teams.json')
|
|
20
19
|
|
|
@@ -274,9 +273,8 @@ module Legion
|
|
|
274
273
|
def vault_path
|
|
275
274
|
settings = teams_auth_settings
|
|
276
275
|
delegated = settings[:delegated]
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
delegated[:vault_path] || DEFAULT_VAULT_PATH
|
|
276
|
+
custom = delegated[:vault_path] if delegated.is_a?(Hash)
|
|
277
|
+
custom || "#{ENV.fetch('USER', 'default')}/microsoft_teams/delegated_token"
|
|
280
278
|
end
|
|
281
279
|
|
|
282
280
|
def local_token_path
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module MicrosoftTeams
|
|
6
|
+
module Helpers
|
|
7
|
+
module TraceRetriever
|
|
8
|
+
MAX_TRACE_TOKENS = 2000
|
|
9
|
+
MAX_TRACES = 20
|
|
10
|
+
|
|
11
|
+
def retrieve_context(message:, owner_id:, chat_id: nil, channel_id: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
12
|
+
return nil unless memory_trace_available?
|
|
13
|
+
|
|
14
|
+
traces = []
|
|
15
|
+
traces.concat(retrieve_sender_traces(owner_id: owner_id))
|
|
16
|
+
traces.concat(retrieve_teams_traces)
|
|
17
|
+
traces.concat(retrieve_chat_traces(chat_id: chat_id)) if chat_id
|
|
18
|
+
|
|
19
|
+
ranked = rank_traces(traces: traces, query: message)
|
|
20
|
+
format_trace_context(traces: ranked.first(MAX_TRACES))
|
|
21
|
+
rescue StandardError => e
|
|
22
|
+
log_trace_error('retrieve_context', e)
|
|
23
|
+
nil
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def retrieve_sender_traces(owner_id:)
|
|
29
|
+
return [] unless owner_id
|
|
30
|
+
|
|
31
|
+
store = shared_trace_store
|
|
32
|
+
return [] unless store
|
|
33
|
+
|
|
34
|
+
store.retrieve_by_domain("sender:#{owner_id}", min_strength: 0.1, limit: 10)
|
|
35
|
+
rescue StandardError => e
|
|
36
|
+
log_trace_error('retrieve_sender_traces', e)
|
|
37
|
+
[]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def retrieve_teams_traces
|
|
41
|
+
store = shared_trace_store
|
|
42
|
+
return [] unless store
|
|
43
|
+
|
|
44
|
+
store.retrieve_by_domain('teams', min_strength: 0.3, limit: 10)
|
|
45
|
+
rescue StandardError => e
|
|
46
|
+
log_trace_error('retrieve_teams_traces', e)
|
|
47
|
+
[]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def retrieve_chat_traces(chat_id:)
|
|
51
|
+
store = shared_trace_store
|
|
52
|
+
return [] unless store
|
|
53
|
+
|
|
54
|
+
store.retrieve_by_domain("chat:#{chat_id}", min_strength: 0.1, limit: 5)
|
|
55
|
+
rescue StandardError => e
|
|
56
|
+
log_trace_error('retrieve_chat_traces', e)
|
|
57
|
+
[]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def rank_traces(traces:, query:) # rubocop:disable Lint/UnusedMethodArgument
|
|
61
|
+
seen = Set.new
|
|
62
|
+
unique = traces.select { |t| seen.add?(t[:trace_id]) }
|
|
63
|
+
|
|
64
|
+
unique.sort_by { |t| [-(t[:strength] || 0.0), -t[:last_reinforced].to_f] }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def format_trace_context(traces:)
|
|
68
|
+
return nil if traces.empty?
|
|
69
|
+
|
|
70
|
+
lines = ['## Organizational Context (from memory)']
|
|
71
|
+
token_estimate = 0
|
|
72
|
+
|
|
73
|
+
traces.each do |trace|
|
|
74
|
+
line = format_single_trace(trace)
|
|
75
|
+
next unless line
|
|
76
|
+
|
|
77
|
+
line_tokens = line.length / 4
|
|
78
|
+
break if token_estimate + line_tokens > MAX_TRACE_TOKENS
|
|
79
|
+
|
|
80
|
+
lines << line
|
|
81
|
+
token_estimate += line_tokens
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
return nil if lines.size <= 1
|
|
85
|
+
|
|
86
|
+
lines.join("\n")
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def format_single_trace(trace)
|
|
90
|
+
type = trace[:trace_type] || :unknown
|
|
91
|
+
content = trace[:content_payload].to_s
|
|
92
|
+
content = "#{content[0, 200]}..." if content.length > 200
|
|
93
|
+
|
|
94
|
+
tags = (trace[:domain_tags] || []).join(', ')
|
|
95
|
+
age = trace_age_label(trace[:created_at] || trace[:last_reinforced])
|
|
96
|
+
|
|
97
|
+
"- [#{type}] #{content} (#{age}, tags: #{tags})"
|
|
98
|
+
rescue StandardError
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def trace_age_label(timestamp)
|
|
103
|
+
return 'unknown age' unless timestamp
|
|
104
|
+
|
|
105
|
+
seconds = Time.now - (timestamp.is_a?(Time) ? timestamp : Time.parse(timestamp.to_s))
|
|
106
|
+
case seconds
|
|
107
|
+
when 0..3600 then 'just now'
|
|
108
|
+
when 3600..86_400 then "#{(seconds / 3600).to_i}h ago"
|
|
109
|
+
when 86_400..604_800 then "#{(seconds / 86_400).to_i}d ago"
|
|
110
|
+
else "#{(seconds / 604_800).to_i}w ago"
|
|
111
|
+
end
|
|
112
|
+
rescue StandardError
|
|
113
|
+
'unknown age'
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def memory_trace_available?
|
|
117
|
+
defined?(Legion::Extensions::Agentic::Memory::Trace)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def shared_trace_store
|
|
121
|
+
return nil unless defined?(Legion::Extensions::Agentic::Memory::Trace) &&
|
|
122
|
+
Legion::Extensions::Agentic::Memory::Trace.respond_to?(:shared_store)
|
|
123
|
+
|
|
124
|
+
Legion::Extensions::Agentic::Memory::Trace.shared_store
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def log_trace_error(method, error)
|
|
128
|
+
return unless defined?(log)
|
|
129
|
+
|
|
130
|
+
log.debug("TraceRetriever##{method} failed: #{error.message}")
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -4,6 +4,7 @@ require 'json'
|
|
|
4
4
|
require 'legion/extensions/microsoft_teams/helpers/client'
|
|
5
5
|
require_relative '../helpers/session_manager'
|
|
6
6
|
require_relative '../helpers/subscription_registry'
|
|
7
|
+
require_relative '../helpers/trace_retriever'
|
|
7
8
|
|
|
8
9
|
module Legion
|
|
9
10
|
module Extensions
|
|
@@ -12,6 +13,7 @@ module Legion
|
|
|
12
13
|
module Bot
|
|
13
14
|
include Legion::Extensions::MicrosoftTeams::Helpers::Client
|
|
14
15
|
include Legion::Extensions::MicrosoftTeams::Helpers::PromptResolver
|
|
16
|
+
include Legion::Extensions::MicrosoftTeams::Helpers::TraceRetriever
|
|
15
17
|
|
|
16
18
|
def send_activity(service_url:, conversation_id:, activity:, **)
|
|
17
19
|
conn = bot_connection(service_url: service_url, **)
|
|
@@ -97,7 +99,8 @@ module Legion
|
|
|
97
99
|
)
|
|
98
100
|
session_manager.add_message(conversation_id: conversation_id, role: :user, content: text)
|
|
99
101
|
|
|
100
|
-
|
|
102
|
+
trace_context = retrieve_trace_context(message: text, owner_id: owner_id, chat_id: chat_id)
|
|
103
|
+
response_text = generate_response(text: text, session: session, trace_context: trace_context)
|
|
101
104
|
|
|
102
105
|
reply_result = send_reply(
|
|
103
106
|
chat_id: chat_id,
|
|
@@ -166,17 +169,20 @@ module Legion
|
|
|
166
169
|
|
|
167
170
|
private
|
|
168
171
|
|
|
169
|
-
def generate_response(text:, session:)
|
|
170
|
-
return llm_respond(text: text, session: session) if llm_available?
|
|
172
|
+
def generate_response(text:, session:, trace_context: nil)
|
|
173
|
+
return llm_respond(text: text, session: session, trace_context: trace_context) if llm_available?
|
|
171
174
|
|
|
172
175
|
"Echo: #{text}"
|
|
173
176
|
end
|
|
174
177
|
|
|
175
|
-
def llm_respond(text:, session:)
|
|
178
|
+
def llm_respond(text:, session:, trace_context: nil)
|
|
176
179
|
config = session[:llm_config] || {}
|
|
180
|
+
instructions = session[:system_prompt]
|
|
181
|
+
instructions = "#{instructions}\n\n#{trace_context}" if trace_context && !trace_context.empty?
|
|
182
|
+
|
|
177
183
|
response = llm_chat(
|
|
178
184
|
text,
|
|
179
|
-
instructions:
|
|
185
|
+
instructions: instructions,
|
|
180
186
|
model: config[:model],
|
|
181
187
|
intent: config[:intent],
|
|
182
188
|
caller: { id: session[:owner_id], extension: 'lex-microsoft_teams', mode: :bot_response }
|
|
@@ -187,6 +193,15 @@ module Legion
|
|
|
187
193
|
'I encountered an error processing your message. Please try again.'
|
|
188
194
|
end
|
|
189
195
|
|
|
196
|
+
def retrieve_trace_context(message:, owner_id:, chat_id:)
|
|
197
|
+
return nil unless respond_to?(:retrieve_context, true)
|
|
198
|
+
|
|
199
|
+
retrieve_context(message: message, owner_id: owner_id, chat_id: chat_id)
|
|
200
|
+
rescue StandardError => e
|
|
201
|
+
log.debug("retrieve_trace_context failed: #{e.message}") if defined?(log)
|
|
202
|
+
nil
|
|
203
|
+
end
|
|
204
|
+
|
|
190
205
|
def llm_available?
|
|
191
206
|
defined?(Legion::LLM) && Legion::LLM.respond_to?(:chat)
|
|
192
207
|
end
|
|
@@ -25,6 +25,7 @@ require 'legion/extensions/microsoft_teams/runners/api_ingest'
|
|
|
25
25
|
# Helpers (bot)
|
|
26
26
|
require 'legion/extensions/microsoft_teams/helpers/high_water_mark'
|
|
27
27
|
require 'legion/extensions/microsoft_teams/helpers/prompt_resolver'
|
|
28
|
+
require 'legion/extensions/microsoft_teams/helpers/trace_retriever'
|
|
28
29
|
require 'legion/extensions/microsoft_teams/helpers/session_manager'
|
|
29
30
|
require 'legion/extensions/microsoft_teams/helpers/subscription_registry'
|
|
30
31
|
require 'legion/extensions/microsoft_teams/helpers/token_cache'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lex-microsoft_teams
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.18
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Esity
|
|
@@ -156,6 +156,8 @@ executables: []
|
|
|
156
156
|
extensions: []
|
|
157
157
|
extra_rdoc_files: []
|
|
158
158
|
files:
|
|
159
|
+
- ".github/CODEOWNERS"
|
|
160
|
+
- ".github/dependabot.yml"
|
|
159
161
|
- ".github/workflows/ci.yml"
|
|
160
162
|
- ".gitignore"
|
|
161
163
|
- ".rspec"
|
|
@@ -197,6 +199,7 @@ files:
|
|
|
197
199
|
- lib/legion/extensions/microsoft_teams/helpers/session_manager.rb
|
|
198
200
|
- lib/legion/extensions/microsoft_teams/helpers/subscription_registry.rb
|
|
199
201
|
- lib/legion/extensions/microsoft_teams/helpers/token_cache.rb
|
|
202
|
+
- lib/legion/extensions/microsoft_teams/helpers/trace_retriever.rb
|
|
200
203
|
- lib/legion/extensions/microsoft_teams/helpers/transform_definitions.rb
|
|
201
204
|
- lib/legion/extensions/microsoft_teams/hooks/auth.rb
|
|
202
205
|
- lib/legion/extensions/microsoft_teams/local_cache/extractor.rb
|