@qvac/core 0.3.1 → 0.3.2
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.
- package/dist-lib/lib/assistant/history.d.ts +10 -1
- package/dist-lib/lib/assistant/history.js +36 -1
- package/dist-lib/lib/assistant/message.d.ts +2 -0
- package/dist-lib/lib/assistant/message.js +11 -0
- package/dist-lib/lib/assistant/skill-encode.js +27 -1
- package/dist-lib/lib/assistant.d.ts +5 -2
- package/dist-lib/lib/assistant.js +41 -8
- package/dist-lib/lib/core/skill-secrets.js +3 -6
- package/dist-lib/lib/core.d.ts +1 -0
- package/dist-lib/lib/core.js +51 -9
- package/dist-lib/lib/credential-pusher.d.ts +2 -3
- package/dist-lib/lib/credential-pusher.js +67 -39
- package/dist-lib/lib/integrations/index.d.ts +1 -2
- package/dist-lib/lib/integrations/index.js +0 -1
- package/dist-lib/lib/integrations/setup-routes.js +0 -15
- package/dist-lib/lib/oauth/flow-core.js +1 -1
- package/dist-lib/lib/oauth/flow.d.ts +4 -2
- package/dist-lib/lib/oauth/flow.js +3 -4
- package/dist-lib/lib/oauth/host.d.ts +3 -1
- package/dist-lib/lib/oauth/host.js +3 -1
- package/dist-lib/lib/oauth/keys.d.ts +21 -20
- package/dist-lib/lib/oauth/keys.js +59 -85
- package/dist-lib/lib/oauth/loopback.d.ts +2 -2
- package/dist-lib/lib/oauth/loopback.js +32 -19
- package/dist-lib/lib/oauth/loopback.stub.d.ts +2 -2
- package/dist-lib/lib/oauth/loopback.stub.js +2 -2
- package/dist-lib/lib/oauth/providers.d.ts +3 -5
- package/dist-lib/lib/oauth/providers.js +20 -67
- package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
- package/dist-lib/lib/oauth/registry.d.ts +10 -0
- package/dist-lib/lib/oauth/registry.js +25 -0
- package/dist-lib/lib/oauth/resolve.js +1 -1
- package/dist-lib/lib/oauth/token-exchange-core.js +2 -1
- package/dist-lib/lib/oauth/types.d.ts +1 -1
- package/dist-lib/lib/oauth.client.d.ts +3 -1
- package/dist-lib/lib/oauth.client.js +3 -1
- package/dist-lib/lib/oauth.d.ts +4 -7
- package/dist-lib/lib/oauth.js +4 -7
- package/dist-lib/lib/skill-types.d.ts +12 -0
- package/dist-lib/schema/engine.js +21 -1
- package/dist-lib/spec/hrpc/messages.js +114 -5
- package/dist-lib/spec/hyperschema/index.js +114 -5
- package/dist-lib/spec/hyperschema/schema.json +84 -1
- package/dist-lib/spec/hyperschema/types.d.ts +18 -1
- package/package.json +4 -4
- package/tui/app.mjs +98 -155
- package/tui/lib/save-file.mjs +1 -1
- package/dist-lib/lib/integrations/oauth-providers.d.ts +0 -14
- package/dist-lib/lib/integrations/oauth-providers.js +0 -116
- package/dist-lib/lib/oauth/asana.d.ts +0 -35
- package/dist-lib/lib/oauth/asana.js +0 -71
- package/dist-lib/lib/oauth/google.d.ts +0 -37
- package/dist-lib/lib/oauth/google.js +0 -77
- package/dist-lib/lib/oauth/notion.d.ts +0 -34
- package/dist-lib/lib/oauth/notion.js +0 -82
- package/dist-lib/lib/oauth/spotify.d.ts +0 -32
- package/dist-lib/lib/oauth/spotify.js +0 -74
- package/dist-lib/lib/oauth/verify.d.ts +0 -8
- package/dist-lib/lib/oauth/verify.js +0 -58
|
@@ -23,7 +23,16 @@ export interface CompactResult {
|
|
|
23
23
|
readonly foldedTurns?: number;
|
|
24
24
|
readonly reason?: string;
|
|
25
25
|
}
|
|
26
|
-
export declare function
|
|
26
|
+
export declare function couldNameSenders(chunks: ReadonlyArray<AssistantChunk>): boolean;
|
|
27
|
+
export declare function senderNamesFrom(chunks: ReadonlyArray<AssistantChunk>, members: ReadonlyArray<{
|
|
28
|
+
id: Buffer;
|
|
29
|
+
name: string;
|
|
30
|
+
}>, devices: ReadonlyArray<{
|
|
31
|
+
id: Buffer;
|
|
32
|
+
memberId?: Buffer | null;
|
|
33
|
+
}>): ReadonlyMap<string, string>;
|
|
34
|
+
export declare function senderAttribution(names: ReadonlyMap<string, string>): string;
|
|
35
|
+
export declare function historyFromChunks(chunks: ReadonlyArray<AssistantChunk>, stale?: Partial<StaleToolRuns>, senderNames?: ReadonlyMap<string, string>): ChatMessage[];
|
|
27
36
|
export declare function inputAvailableToRun(chunk: AssistantChunk, requestSeq: number): boolean;
|
|
28
37
|
export declare function normalizeQuestion(text: string): string;
|
|
29
38
|
export declare const GENERIC_ACKNOWLEDGEMENTS: Set<string>;
|
|
@@ -39,8 +39,40 @@ export function beforeLatestAttempt(chunk, starts) {
|
|
|
39
39
|
const start = starts.get(chunk.runId);
|
|
40
40
|
return start !== undefined && chunk.seq < start;
|
|
41
41
|
}
|
|
42
|
-
export function
|
|
42
|
+
export function couldNameSenders(chunks) {
|
|
43
|
+
const seen = new Set();
|
|
44
|
+
for (const chunk of chunks) {
|
|
45
|
+
if (chunk.type !== 'user-message')
|
|
46
|
+
continue;
|
|
47
|
+
seen.add((chunk.senderMemberId ?? chunk.originDeviceId).toString('hex'));
|
|
48
|
+
if (seen.size > 1)
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
export function senderNamesFrom(chunks, members, devices) {
|
|
54
|
+
if (members.length < 2)
|
|
55
|
+
return new Map();
|
|
56
|
+
const byMember = new Map(members.map((member) => [member.id.toString('hex'), member.name]));
|
|
57
|
+
const deviceMember = new Map(devices.map((device) => [device.id.toString('hex'), device.memberId]));
|
|
58
|
+
const names = new Map();
|
|
59
|
+
for (const chunk of chunks) {
|
|
60
|
+
if (chunk.type !== 'user-message')
|
|
61
|
+
continue;
|
|
62
|
+
const memberId = chunk.senderMemberId ?? deviceMember.get(chunk.originDeviceId.toString('hex'));
|
|
63
|
+
const name = memberId ? byMember.get(memberId.toString('hex')) : undefined;
|
|
64
|
+
if (name)
|
|
65
|
+
names.set(chunk.id, name);
|
|
66
|
+
}
|
|
67
|
+
return names;
|
|
68
|
+
}
|
|
69
|
+
export function senderAttribution(names) {
|
|
70
|
+
const distinct = [...new Set(names.values())].sort();
|
|
71
|
+
return distinct.length > 1 ? distinct.join('\u0000') : '';
|
|
72
|
+
}
|
|
73
|
+
export function historyFromChunks(chunks, stale = {}, senderNames = new Map()) {
|
|
43
74
|
const attemptStarts = latestAttemptStarts(chunks);
|
|
75
|
+
const shared = new Set(senderNames.values()).size > 1;
|
|
44
76
|
const staleRunIds = stale.runIds ?? new Set();
|
|
45
77
|
const staleQuestionChunkIds = stale.questionChunkIds ?? new Set();
|
|
46
78
|
const folded = foldCompactedChunks(chunks);
|
|
@@ -72,7 +104,10 @@ export function historyFromChunks(chunks, stale = {}) {
|
|
|
72
104
|
if (chunk.userMessage) {
|
|
73
105
|
if (staleQuestionChunkIds.has(chunk.id))
|
|
74
106
|
continue;
|
|
107
|
+
const sender = shared ? senderNames.get(chunk.id) : undefined;
|
|
75
108
|
const turn = { role: 'user', content: chunk.userMessage.text };
|
|
109
|
+
if (sender)
|
|
110
|
+
turn.sender = sender;
|
|
76
111
|
const refs = (byMessage.get(chunk.id) ?? [])
|
|
77
112
|
.map((attachmentChunk) => attachmentChunk.attachment)
|
|
78
113
|
.filter((ref) => !ref.deletedAt);
|
|
@@ -2,6 +2,7 @@ import type { DocumentAttachment } from '../core/document-attachment.ts';
|
|
|
2
2
|
export interface ChatMessage {
|
|
3
3
|
role: 'user' | 'assistant' | 'tool';
|
|
4
4
|
content: string;
|
|
5
|
+
sender?: string;
|
|
5
6
|
attachments?: {
|
|
6
7
|
path: string;
|
|
7
8
|
}[];
|
|
@@ -43,3 +44,4 @@ export interface AttachmentMeta {
|
|
|
43
44
|
export interface FetchedAttachment extends AttachmentMeta {
|
|
44
45
|
bytes: Uint8Array;
|
|
45
46
|
}
|
|
47
|
+
export declare function nameSenders(messages: ReadonlyArray<ChatMessage>): ChatMessage[];
|
|
@@ -45,3 +45,14 @@ export function audioTranscriptBlock(fileName, transcript) {
|
|
|
45
45
|
transcript
|
|
46
46
|
].join('\n');
|
|
47
47
|
}
|
|
48
|
+
const MAX_SENDER_NAME_CHARS = 64;
|
|
49
|
+
function displayableSender(name) {
|
|
50
|
+
const flat = name?.replace(/\s+/g, ' ').trim();
|
|
51
|
+
return flat ? Array.from(flat).slice(0, MAX_SENDER_NAME_CHARS).join('') : undefined;
|
|
52
|
+
}
|
|
53
|
+
export function nameSenders(messages) {
|
|
54
|
+
return messages.map(({ sender, ...turn }) => {
|
|
55
|
+
const name = displayableSender(sender);
|
|
56
|
+
return name ? { ...turn, content: `${name}: ${turn.content}` } : turn;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { readSetup as normalizeSkillSetup } from '@qvac/harness/lib/tools/skills/manifest-parse.ts';
|
|
2
|
+
const TOKEN_AUTH = ['pkce-only', 'secret-in-body', 'secret-in-basic'];
|
|
2
3
|
export function toEngineSkill(skill) {
|
|
3
4
|
const setup = normalizeSkillSetup(skill.setup);
|
|
4
5
|
return {
|
|
@@ -63,7 +64,32 @@ export function toEngineSkillSetup(setup) {
|
|
|
63
64
|
}))
|
|
64
65
|
}
|
|
65
66
|
: {}),
|
|
66
|
-
...(route.satisfied !== undefined ? { satisfied: route.satisfied } : {})
|
|
67
|
+
...(route.satisfied !== undefined ? { satisfied: route.satisfied } : {}),
|
|
68
|
+
...(route.oauth &&
|
|
69
|
+
route.oauth.stateKey &&
|
|
70
|
+
route.oauth.authUrl &&
|
|
71
|
+
route.oauth.tokenUrl &&
|
|
72
|
+
TOKEN_AUTH.includes(route.oauth.tokenAuth)
|
|
73
|
+
? {
|
|
74
|
+
oauth: {
|
|
75
|
+
stateKey: route.oauth.stateKey,
|
|
76
|
+
authUrl: route.oauth.authUrl,
|
|
77
|
+
tokenUrl: route.oauth.tokenUrl,
|
|
78
|
+
tokenAuth: route.oauth.tokenAuth,
|
|
79
|
+
scopes: [...(route.oauth.scopes ?? [])],
|
|
80
|
+
...(route.oauth.extraAuthParams
|
|
81
|
+
? { extraAuthParams: route.oauth.extraAuthParams }
|
|
82
|
+
: {}),
|
|
83
|
+
...(route.oauth.registrationEndpoint
|
|
84
|
+
? { registrationEndpoint: route.oauth.registrationEndpoint }
|
|
85
|
+
: {}),
|
|
86
|
+
...(route.oauth.port !== undefined ? { port: route.oauth.port } : {}),
|
|
87
|
+
...(route.oauth.sharedIosSession !== undefined
|
|
88
|
+
? { sharedIosSession: route.oauth.sharedIosSession }
|
|
89
|
+
: {})
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
: {})
|
|
67
93
|
}))
|
|
68
94
|
}
|
|
69
95
|
: {})
|
|
@@ -12,8 +12,8 @@ import type { Skill } from './assistant/skill-encode.ts';
|
|
|
12
12
|
import { type CompactOptions, type CompactResult } from './assistant/history.ts';
|
|
13
13
|
export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, type DeleteModelSummary, type Harness, type HarnessInfo, type ListModelsOptions, type ModelInfo, type ModelType, type ToolCatalogEntry } from './assistant/harness.ts';
|
|
14
14
|
export { type Skill } from './assistant/skill-encode.ts';
|
|
15
|
-
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT } from './assistant/message.ts';
|
|
16
|
-
export { historyFromChunks, listHistoryChunks, staleToolRunIds, type CompactOptions, type CompactResult, type CompactionTrigger, type StaleToolRuns } from './assistant/history.ts';
|
|
15
|
+
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders } from './assistant/message.ts';
|
|
16
|
+
export { couldNameSenders, historyFromChunks, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds, type CompactOptions, type CompactResult, type CompactionTrigger, type StaleToolRuns } from './assistant/history.ts';
|
|
17
17
|
export declare const CAPTURE_INFERENCE_CALLS_KEY = "inspector.captureInferenceCalls";
|
|
18
18
|
export declare const CAPTURE_ON = "on";
|
|
19
19
|
export declare const CAPTURE_OFF = "off";
|
|
@@ -79,6 +79,7 @@ export declare class QvacAssistant extends ReadyResource {
|
|
|
79
79
|
private readonly _chats;
|
|
80
80
|
private readonly _runImageCacheNames;
|
|
81
81
|
private readonly _reconciledCompactions;
|
|
82
|
+
private readonly _chatAttribution;
|
|
82
83
|
private readonly _modelCatalog;
|
|
83
84
|
private readonly _onHarnessStatus;
|
|
84
85
|
private readonly _onModelCatalogEvent;
|
|
@@ -129,6 +130,8 @@ export declare class QvacAssistant extends ReadyResource {
|
|
|
129
130
|
private _watchAgents;
|
|
130
131
|
private _reclaimAgentChatCaches;
|
|
131
132
|
private _reconcileCompaction;
|
|
133
|
+
private _senderNames;
|
|
134
|
+
private _reconcileAttribution;
|
|
132
135
|
deleteChatKvCache(chatId: string): Promise<AssistantKvCacheDeletionResponse>;
|
|
133
136
|
private _deleteChatKvCache;
|
|
134
137
|
private _watchMeshList;
|
|
@@ -42,13 +42,14 @@ import { searchKnowledge } from './knowledge-search.js';
|
|
|
42
42
|
import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
|
|
43
43
|
import { encodeAgentSkillReadiness } from './skill-readiness.js';
|
|
44
44
|
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
|
|
45
|
-
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice } from './assistant/message.js';
|
|
45
|
+
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders } from './assistant/message.js';
|
|
46
46
|
import { drainBody, raceInactivity } from './assistant/fetch-body.js';
|
|
47
|
-
import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, historyFromChunks, inputAvailableToRun, listHistoryChunks, staleToolRunIds } from './assistant/history.js';
|
|
47
|
+
import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, couldNameSenders, historyFromChunks, inputAvailableToRun, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
|
|
48
48
|
export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
|
|
49
49
|
export {} from './assistant/skill-encode.js';
|
|
50
|
-
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT } from './assistant/message.js';
|
|
51
|
-
export { historyFromChunks, listHistoryChunks, staleToolRunIds } from './assistant/history.js';
|
|
50
|
+
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders } from './assistant/message.js';
|
|
51
|
+
export { couldNameSenders, historyFromChunks, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
|
|
52
|
+
const NO_SENDERS = new Map();
|
|
52
53
|
const QvacState = local.getEnum('@local/qvac-state');
|
|
53
54
|
const FINAL_FLUSH_MS = 10_000;
|
|
54
55
|
const UPLOAD_FRAME_BYTES = 64 * 1024;
|
|
@@ -140,6 +141,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
140
141
|
_chats;
|
|
141
142
|
_runImageCacheNames;
|
|
142
143
|
_reconciledCompactions;
|
|
144
|
+
_chatAttribution;
|
|
143
145
|
_modelCatalog = new ModelCatalogRevisionHub();
|
|
144
146
|
_onHarnessStatus;
|
|
145
147
|
_onModelCatalogEvent;
|
|
@@ -185,6 +187,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
185
187
|
this._chats = new Map();
|
|
186
188
|
this._runImageCacheNames = new Map();
|
|
187
189
|
this._reconciledCompactions = new Map();
|
|
190
|
+
this._chatAttribution = new Map();
|
|
188
191
|
this._onHarnessStatus = (status) => {
|
|
189
192
|
this._log?.warn('assistant', 'harness status', status);
|
|
190
193
|
if (harnessDied(status)) {
|
|
@@ -723,6 +726,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
723
726
|
if (known !== null) {
|
|
724
727
|
for (const chatId of disappearedChatIds(known, ids)) {
|
|
725
728
|
this._reconciledCompactions.delete(chatId);
|
|
729
|
+
this._chatAttribution.delete(chatId);
|
|
726
730
|
void this._deleteChatKvCache(chatId, 'chat deleted');
|
|
727
731
|
void this._inferenceCalls
|
|
728
732
|
.forget(chatId)
|
|
@@ -784,6 +788,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
784
788
|
if (gone)
|
|
785
789
|
this._reconciledCompactions.set(chatId, newest.id);
|
|
786
790
|
}
|
|
791
|
+
async _senderNames(engine, chunks) {
|
|
792
|
+
if (!couldNameSenders(chunks))
|
|
793
|
+
return new Map();
|
|
794
|
+
const stamped = chunks.every((chunk) => chunk.type !== 'user-message' || chunk.senderMemberId);
|
|
795
|
+
const [{ members }, { devices }] = await Promise.all([
|
|
796
|
+
engine.listMembers({}).catch(() => ({ members: [] })),
|
|
797
|
+
stamped ? { devices: [] } : engine.listDevices({}).catch(() => ({ devices: [] }))
|
|
798
|
+
]);
|
|
799
|
+
return senderNamesFrom(chunks, members ?? [], devices ?? []);
|
|
800
|
+
}
|
|
801
|
+
async _reconcileAttribution(chatId, senders) {
|
|
802
|
+
const attribution = senderAttribution(senders);
|
|
803
|
+
const seen = this._chatAttribution.get(chatId);
|
|
804
|
+
if (seen === attribution)
|
|
805
|
+
return;
|
|
806
|
+
if (seen === undefined && !attribution) {
|
|
807
|
+
this._chatAttribution.set(chatId, attribution);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed');
|
|
811
|
+
if (gone)
|
|
812
|
+
this._chatAttribution.set(chatId, attribution);
|
|
813
|
+
}
|
|
787
814
|
async deleteChatKvCache(chatId) {
|
|
788
815
|
return ((await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
|
|
789
816
|
status: 'unsupported',
|
|
@@ -1082,7 +1109,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1082
1109
|
let ran = (priorResults ?? []).some((chunk) => chunk.runId === runId &&
|
|
1083
1110
|
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
1084
1111
|
!isDeniedResult(chunk.toolResult?.result));
|
|
1085
|
-
const
|
|
1112
|
+
const named = couldNameSenders(history);
|
|
1113
|
+
const senders = named ? await this._senderNames(engine, history) : NO_SENDERS;
|
|
1114
|
+
if (named || this._chatAttribution.get(chatId)) {
|
|
1115
|
+
await this._reconcileAttribution(chatId, senders);
|
|
1116
|
+
}
|
|
1117
|
+
const messages = historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
|
|
1086
1118
|
if (currentRepair)
|
|
1087
1119
|
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1088
1120
|
const diagramRepair = currentRepair !== undefined;
|
|
@@ -1314,7 +1346,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1314
1346
|
const events = this._harnessFor(runAgent).completion({
|
|
1315
1347
|
model,
|
|
1316
1348
|
agent: runAgent,
|
|
1317
|
-
messages,
|
|
1349
|
+
messages: nameSenders(messages),
|
|
1318
1350
|
cacheKey,
|
|
1319
1351
|
thinkingDefault: config ? (config.thinkingEnabled ? 'enabled' : 'disabled') : undefined,
|
|
1320
1352
|
toolConfigs,
|
|
@@ -1854,6 +1886,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1854
1886
|
const userTurns = chunks.filter((chunk) => chunk.userMessage).length;
|
|
1855
1887
|
let announced = false;
|
|
1856
1888
|
let keepTurns = COMPACTION_KEEP_TURNS;
|
|
1889
|
+
const senders = await this._senderNames(engine, chunks);
|
|
1857
1890
|
for (;;) {
|
|
1858
1891
|
const boundary = compactionBoundary(chunks, keepTurns);
|
|
1859
1892
|
if (!boundary)
|
|
@@ -1864,7 +1897,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1864
1897
|
if (through.seq <= lastThroughSeq)
|
|
1865
1898
|
return { compacted: false, reason: 'already-compacted' };
|
|
1866
1899
|
const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
|
|
1867
|
-
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''));
|
|
1900
|
+
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), senders);
|
|
1868
1901
|
if (!announced) {
|
|
1869
1902
|
await options.onFold?.();
|
|
1870
1903
|
announced = true;
|
|
@@ -1912,7 +1945,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1912
1945
|
messages: [
|
|
1913
1946
|
{
|
|
1914
1947
|
role: 'user',
|
|
1915
|
-
content: buildCompactionPrompt(turns.map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary)
|
|
1948
|
+
content: buildCompactionPrompt(nameSenders(turns).map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary)
|
|
1916
1949
|
}
|
|
1917
1950
|
],
|
|
1918
1951
|
generationParams: { reasoning_budget: 0 },
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
2
|
import { credentialSlots } from '@qvac/harness/lib/tools/skills/credential-slots.ts';
|
|
3
3
|
import { isDeviceSharedCredentialKey } from '@qvac/harness/lib/tools/skills/scope-key.ts';
|
|
4
|
-
import {
|
|
4
|
+
import { settingConfigKey } from '../credential-pusher.js';
|
|
5
5
|
import { scopedKey } from '../oauth/common.js';
|
|
6
6
|
import { getAgent } from './agent.js';
|
|
7
7
|
const DEVICES = '@qvac/devices';
|
|
@@ -65,15 +65,12 @@ function credentialKeys(skill) {
|
|
|
65
65
|
for (const route of skill.setup?.routes ?? []) {
|
|
66
66
|
if (route.credentialKey)
|
|
67
67
|
keys.add(route.credentialKey);
|
|
68
|
+
if (route.oauth?.stateKey)
|
|
69
|
+
keys.add(route.oauth.stateKey);
|
|
68
70
|
for (const field of route.fields ?? []) {
|
|
69
71
|
if (field.secret === true)
|
|
70
72
|
keys.add(field.key);
|
|
71
73
|
}
|
|
72
74
|
}
|
|
73
|
-
for (const key of [...keys]) {
|
|
74
|
-
const state = oauthStateKey(key);
|
|
75
|
-
if (state)
|
|
76
|
-
keys.add(state);
|
|
77
|
-
}
|
|
78
75
|
return [...keys];
|
|
79
76
|
}
|
package/dist-lib/lib/core.d.ts
CHANGED
package/dist-lib/lib/core.js
CHANGED
|
@@ -45,7 +45,6 @@ import { buildDefaultSeed } from './core/helpers/index.js';
|
|
|
45
45
|
import { Mesh } from './core/mesh.js';
|
|
46
46
|
import { raceResource } from './race-resource.js';
|
|
47
47
|
import { createSystemInfoSampler } from './core/system-info.js';
|
|
48
|
-
import { oauthStateKeyFor } from './credential-pusher.js';
|
|
49
48
|
import { setLogger } from './logger.js';
|
|
50
49
|
import { settleOnClose } from '@qvac/harness/rpc';
|
|
51
50
|
import { openLocalSpec } from './spec-loader.js';
|
|
@@ -491,7 +490,12 @@ export class Core extends ReadyResource {
|
|
|
491
490
|
if (this._blockedByMinVersion() || this._currentMesh() || this._minting)
|
|
492
491
|
return;
|
|
493
492
|
try {
|
|
494
|
-
await this._switchToMesh({
|
|
493
|
+
const mesh = await this._switchToMesh({
|
|
494
|
+
seed: this._meshSeed,
|
|
495
|
+
baseKey: this._meshKey,
|
|
496
|
+
account: true
|
|
497
|
+
});
|
|
498
|
+
void this._openAccountBeside(mesh).catch(noop);
|
|
495
499
|
}
|
|
496
500
|
catch (err) {
|
|
497
501
|
if (this.closing)
|
|
@@ -503,6 +507,23 @@ export class Core extends ReadyResource {
|
|
|
503
507
|
this._log?.error(['p2p', 'mesh'], 'mesh open failed', err);
|
|
504
508
|
}
|
|
505
509
|
}
|
|
510
|
+
async _openAccountBeside(active) {
|
|
511
|
+
const home = await this._homeMeshKey();
|
|
512
|
+
if (!home || home.equals(active.key))
|
|
513
|
+
return;
|
|
514
|
+
await this._meshTransitionLock.lock();
|
|
515
|
+
try {
|
|
516
|
+
if (this.closing || this._blockedByMinVersion())
|
|
517
|
+
return;
|
|
518
|
+
const mesh = await this._openPooledMesh(home.toString('hex')).catch(() => null);
|
|
519
|
+
if (mesh && !mesh.ourMemberId)
|
|
520
|
+
await mesh.refreshOurMember();
|
|
521
|
+
}
|
|
522
|
+
finally {
|
|
523
|
+
this._meshTransitionLock.unlock();
|
|
524
|
+
}
|
|
525
|
+
this._notifyStatus();
|
|
526
|
+
}
|
|
506
527
|
async _runBootOnline() {
|
|
507
528
|
void this.onlineBoot.ready().catch(noop);
|
|
508
529
|
try {
|
|
@@ -530,7 +551,12 @@ export class Core extends ReadyResource {
|
|
|
530
551
|
try {
|
|
531
552
|
if (this.closing || this._currentMesh() || this._minting)
|
|
532
553
|
return;
|
|
533
|
-
await this._startMesh({
|
|
554
|
+
const mesh = await this._startMesh({
|
|
555
|
+
seed: this._meshSeed,
|
|
556
|
+
baseKey: this._meshKey,
|
|
557
|
+
account: true
|
|
558
|
+
});
|
|
559
|
+
void this._openAccountBeside(mesh).catch(noop);
|
|
534
560
|
}
|
|
535
561
|
catch (err) {
|
|
536
562
|
if (this.closing)
|
|
@@ -2370,7 +2396,7 @@ export class Core extends ReadyResource {
|
|
|
2370
2396
|
try {
|
|
2371
2397
|
if (meshId === this._activeMeshKey)
|
|
2372
2398
|
throw new Error('The active mesh cannot be closed');
|
|
2373
|
-
const mesh = this._currentMesh(meshId);
|
|
2399
|
+
const mesh = this._currentMesh(meshId) ?? (await this._opening.get(meshId)?.catch(noop));
|
|
2374
2400
|
if (!mesh)
|
|
2375
2401
|
return;
|
|
2376
2402
|
await this._retirePooledMesh(mesh);
|
|
@@ -2934,16 +2960,16 @@ export class Core extends ReadyResource {
|
|
|
2934
2960
|
return new Map();
|
|
2935
2961
|
const fields = new Map();
|
|
2936
2962
|
for (const slot of skill.credentials ?? []) {
|
|
2937
|
-
for (const key of slot.split('|'))
|
|
2963
|
+
for (const key of slot.split('|'))
|
|
2938
2964
|
fields.set(key, true);
|
|
2939
|
-
const stateKey = oauthStateKeyFor(key);
|
|
2940
|
-
if (stateKey)
|
|
2941
|
-
fields.set(stateKey, true);
|
|
2942
|
-
}
|
|
2943
2965
|
}
|
|
2966
|
+
const foreign = credentialKeysOwnedElsewhere(skills, skillName);
|
|
2944
2967
|
for (const route of skill.setup?.routes ?? []) {
|
|
2945
2968
|
if (route.credentialKey)
|
|
2946
2969
|
fields.set(route.credentialKey, true);
|
|
2970
|
+
const stateKey = route.oauth?.stateKey;
|
|
2971
|
+
if (stateKey && !foreign.has(stateKey))
|
|
2972
|
+
fields.set(stateKey, true);
|
|
2947
2973
|
for (const field of route.fields ?? [])
|
|
2948
2974
|
fields.set(field.key, field.secret === true);
|
|
2949
2975
|
}
|
|
@@ -3485,3 +3511,19 @@ function statusErrorMessage(context, err) {
|
|
|
3485
3511
|
const detail = err instanceof Error ? err.message : err ? String(err) : '';
|
|
3486
3512
|
return detail ? `${context}: ${detail}` : context;
|
|
3487
3513
|
}
|
|
3514
|
+
function credentialKeysOwnedElsewhere(skills, skillName) {
|
|
3515
|
+
const keys = new Set();
|
|
3516
|
+
for (const entry of skills) {
|
|
3517
|
+
if (entry.name === skillName)
|
|
3518
|
+
continue;
|
|
3519
|
+
for (const slot of entry.credentials ?? []) {
|
|
3520
|
+
for (const key of slot.split('|'))
|
|
3521
|
+
keys.add(key);
|
|
3522
|
+
}
|
|
3523
|
+
for (const route of entry.setup?.routes ?? []) {
|
|
3524
|
+
if (route.credentialKey)
|
|
3525
|
+
keys.add(route.credentialKey);
|
|
3526
|
+
}
|
|
3527
|
+
}
|
|
3528
|
+
return keys;
|
|
3529
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Harness } from './assistant.ts';
|
|
2
2
|
import { type ConfigEngine } from './oauth.ts';
|
|
3
|
+
import type { OAuthRouteLike } from './oauth/keys.ts';
|
|
3
4
|
export type CredentialEntry = {
|
|
4
5
|
key: string;
|
|
5
6
|
token?: string | null;
|
|
@@ -20,7 +21,7 @@ export type PusherSkill = {
|
|
|
20
21
|
credentials?: readonly string[];
|
|
21
22
|
};
|
|
22
23
|
setup?: {
|
|
23
|
-
routes?: ReadonlyArray<{
|
|
24
|
+
routes?: ReadonlyArray<OAuthRouteLike & {
|
|
24
25
|
fields?: ReadonlyArray<{
|
|
25
26
|
key: string;
|
|
26
27
|
secret?: boolean;
|
|
@@ -53,7 +54,6 @@ export type CredentialPusherLogger = {
|
|
|
53
54
|
export type CredentialPusherHandle = (() => Promise<void>) & {
|
|
54
55
|
sync(): Promise<void>;
|
|
55
56
|
};
|
|
56
|
-
export declare function oauthStateKeyFor(credentialKey: string): string | null;
|
|
57
57
|
export declare function nextRefreshDelay(entries: ReadonlyArray<{
|
|
58
58
|
expiresAt?: number | null;
|
|
59
59
|
}>, now?: number): number;
|
|
@@ -63,5 +63,4 @@ export declare function settingFields(catalog: ReadonlyArray<PusherSkill>): Read
|
|
|
63
63
|
key: string;
|
|
64
64
|
}>;
|
|
65
65
|
export declare function settingConfigKey(agentId: string | undefined, skill: string, key: string): string;
|
|
66
|
-
export declare function oauthStateKey(credentialKey: string): string | undefined;
|
|
67
66
|
export declare function startCredentialPusher(harness: CredentialTarget, engine: CredentialEngine, logger?: CredentialPusherLogger | null): CredentialPusherHandle;
|
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
import { OBSIDIAN_VAULT_PATH_CREDENTIAL_KEY } from './obsidian-key.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { scopedKey } from './oauth.js';
|
|
3
|
+
import { oauthSpecs } from './oauth/registry.js';
|
|
4
|
+
import { resolveOAuthCredential } from './oauth/resolve.js';
|
|
5
|
+
import { buildProviderConfig } from './oauth/providers.js';
|
|
6
|
+
import { refreshOAuthTokens } from './oauth/token-exchange.js';
|
|
4
7
|
import ScopeLock from 'scope-lock';
|
|
5
8
|
import { credentialSlots } from '@qvac/harness/lib/tools/skills/credential-slots.ts';
|
|
6
9
|
import { REFRESH_SKEW_MS } from './oauth/common.js';
|
|
7
|
-
const RESOLVE_BY_PROVIDER = {
|
|
8
|
-
google: resolveGoogleCredential,
|
|
9
|
-
spotify: resolveSpotifyCredential,
|
|
10
|
-
notion: resolveNotionCredential,
|
|
11
|
-
asana: resolveAsanaCredential
|
|
12
|
-
};
|
|
13
|
-
export function oauthStateKeyFor(credentialKey) {
|
|
14
|
-
return specStateKeyFor(credentialKey);
|
|
15
|
-
}
|
|
16
10
|
const PLAIN_KEYS = ['github_access_token', OBSIDIAN_VAULT_PATH_CREDENTIAL_KEY];
|
|
17
11
|
const REFRESH_INTERVAL = 10 * 60 * 1000;
|
|
18
12
|
const MIN_REFRESH_DELAY = 30_000;
|
|
@@ -31,9 +25,16 @@ export function nextRefreshDelay(entries, now = Date.now()) {
|
|
|
31
25
|
return REFRESH_INTERVAL;
|
|
32
26
|
return Math.min(REFRESH_INTERVAL, Math.max(MIN_REFRESH_DELAY, nearest - now - REFRESH_SKEW_MS));
|
|
33
27
|
}
|
|
34
|
-
async function oauthEntry(engine,
|
|
35
|
-
const
|
|
36
|
-
const token = await
|
|
28
|
+
async function oauthEntry(engine, spec, agentId) {
|
|
29
|
+
const { credentialKey, stateKey } = spec;
|
|
30
|
+
const token = await resolveOAuthCredential({
|
|
31
|
+
engine,
|
|
32
|
+
agentId,
|
|
33
|
+
credentialKey,
|
|
34
|
+
stateKey,
|
|
35
|
+
requireClientSecret: spec.tokenAuth !== 'pkce-only',
|
|
36
|
+
refresh: (args) => refreshOAuthTokens(buildProviderConfig(spec, { clientId: args.clientId, clientSecret: args.clientSecret }), args.refreshToken)
|
|
37
|
+
});
|
|
37
38
|
if (!token)
|
|
38
39
|
return null;
|
|
39
40
|
const { value } = await engine.configGet({
|
|
@@ -92,16 +93,13 @@ export function settingFields(catalog) {
|
|
|
92
93
|
export function settingConfigKey(agentId, skill, key) {
|
|
93
94
|
return scopedKey(agentId, `settings.${skill}.${key}`);
|
|
94
95
|
}
|
|
95
|
-
export function oauthStateKey(credentialKey) {
|
|
96
|
-
return specStateKeyFor(credentialKey) ?? undefined;
|
|
97
|
-
}
|
|
98
96
|
function fanOut(entry, owners) {
|
|
99
97
|
const skills = owners.get(entry.key) ?? [];
|
|
100
98
|
return skills.length > 0 ? skills.map((skill) => ({ ...entry, skill })) : [entry];
|
|
101
99
|
}
|
|
102
|
-
async function resolveScope(engine, agentId, last, owners, logger) {
|
|
100
|
+
async function resolveScope(engine, agentId, last, owners, specs, logger) {
|
|
103
101
|
const entries = [];
|
|
104
|
-
for (const spec of
|
|
102
|
+
for (const spec of specs) {
|
|
105
103
|
const mk = storeKey(spec.credentialKey, agentId);
|
|
106
104
|
try {
|
|
107
105
|
const entry = await oauthEntry(engine, spec, agentId);
|
|
@@ -120,7 +118,10 @@ async function resolveScope(engine, agentId, last, owners, logger) {
|
|
|
120
118
|
entries.push(...fanOut(kept, owners));
|
|
121
119
|
}
|
|
122
120
|
}
|
|
121
|
+
const specKeys = new Set(specs.map((spec) => spec.credentialKey));
|
|
123
122
|
for (const key of PLAIN_KEYS) {
|
|
123
|
+
if (specKeys.has(key))
|
|
124
|
+
continue;
|
|
124
125
|
const mk = storeKey(key, agentId);
|
|
125
126
|
try {
|
|
126
127
|
const entry = await plainEntry(engine, key, agentId);
|
|
@@ -166,10 +167,48 @@ async function resolveSettings(engine, scopes, catalog, logger) {
|
|
|
166
167
|
}
|
|
167
168
|
export function startCredentialPusher(harness, engine, logger) {
|
|
168
169
|
const last = new Map();
|
|
169
|
-
const watches =
|
|
170
|
+
const watches = new Map();
|
|
170
171
|
let disposed = false;
|
|
171
172
|
let timer = null;
|
|
172
173
|
const pushLock = new ScopeLock({ debounce: true });
|
|
174
|
+
const watch = (key) => {
|
|
175
|
+
const stream = engine.configWatch({ key: scopedKey(undefined, key) });
|
|
176
|
+
watches.set(key, stream);
|
|
177
|
+
(async () => {
|
|
178
|
+
for await (const _ of stream)
|
|
179
|
+
schedule();
|
|
180
|
+
if (disposed || watches.get(key) !== stream)
|
|
181
|
+
return;
|
|
182
|
+
logger?.warn('credential-pusher', `credential watch ended for ${key}, change propagation degrades to the interval until restart`);
|
|
183
|
+
})().catch((err) => {
|
|
184
|
+
if (disposed || watches.get(key) !== stream)
|
|
185
|
+
return;
|
|
186
|
+
logger?.warn('credential-pusher', `credential watch ended for ${key}`, errorMessage(err));
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
const syncWatches = (oauthKeys) => {
|
|
190
|
+
if (disposed)
|
|
191
|
+
return;
|
|
192
|
+
const wanted = new Set([...oauthKeys, ...PLAIN_KEYS]);
|
|
193
|
+
for (const [key, stream] of watches) {
|
|
194
|
+
if (wanted.has(key))
|
|
195
|
+
continue;
|
|
196
|
+
try {
|
|
197
|
+
stream.destroy();
|
|
198
|
+
}
|
|
199
|
+
catch { }
|
|
200
|
+
watches.delete(key);
|
|
201
|
+
}
|
|
202
|
+
for (const key of wanted) {
|
|
203
|
+
if (!watches.has(key))
|
|
204
|
+
watch(key);
|
|
205
|
+
}
|
|
206
|
+
for (const mk of last.keys()) {
|
|
207
|
+
const key = mk.includes('\0') ? mk.slice(mk.indexOf('\0') + 1) : mk;
|
|
208
|
+
if (!wanted.has(key))
|
|
209
|
+
last.delete(mk);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
173
212
|
const armTimer = (delay) => {
|
|
174
213
|
if (disposed)
|
|
175
214
|
return;
|
|
@@ -188,18 +227,22 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
188
227
|
}
|
|
189
228
|
catch (err) {
|
|
190
229
|
logger?.warn('credential-pusher', 'skill catalog read failed', errorMessage(err));
|
|
191
|
-
return
|
|
230
|
+
return null;
|
|
192
231
|
}
|
|
193
232
|
};
|
|
194
233
|
const pushAll = async () => {
|
|
195
234
|
const entries = [];
|
|
196
235
|
try {
|
|
197
236
|
const skills = await catalog();
|
|
237
|
+
if (skills === null)
|
|
238
|
+
return;
|
|
198
239
|
const owners = credentialOwners(skills);
|
|
240
|
+
const specs = oauthSpecs(skills);
|
|
241
|
+
syncWatches(specs.map((spec) => spec.credentialKey));
|
|
199
242
|
const ids = (await engine.listAgents({})).agents.map((agent) => agent.id);
|
|
200
243
|
const scopes = [undefined, ...ids];
|
|
201
244
|
for (const agentId of scopes) {
|
|
202
|
-
entries.push(...(await resolveScope(engine, agentId, last, owners, logger)));
|
|
245
|
+
entries.push(...(await resolveScope(engine, agentId, last, owners, specs, logger)));
|
|
203
246
|
}
|
|
204
247
|
if (disposed)
|
|
205
248
|
return;
|
|
@@ -250,22 +293,6 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
250
293
|
pushLock.unlock();
|
|
251
294
|
}
|
|
252
295
|
};
|
|
253
|
-
const watchKeys = [...OAUTH_KEY_SPECS.map((s) => s.credentialKey), ...PLAIN_KEYS];
|
|
254
|
-
for (const key of watchKeys) {
|
|
255
|
-
const stream = engine.configWatch({ key: scopedKey(undefined, key) });
|
|
256
|
-
watches.push(stream);
|
|
257
|
-
(async () => {
|
|
258
|
-
for await (const _ of stream)
|
|
259
|
-
schedule();
|
|
260
|
-
if (disposed)
|
|
261
|
-
return;
|
|
262
|
-
logger?.warn('credential-pusher', `credential watch ended for ${key}, change propagation degrades to the interval until restart`);
|
|
263
|
-
})().catch((err) => {
|
|
264
|
-
if (disposed)
|
|
265
|
-
return;
|
|
266
|
-
logger?.warn('credential-pusher', `credential watch ended for ${key}`, errorMessage(err));
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
296
|
schedule();
|
|
270
297
|
const stop = async () => {
|
|
271
298
|
if (disposed)
|
|
@@ -274,12 +301,13 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
274
301
|
if (timer)
|
|
275
302
|
clearTimeout(timer);
|
|
276
303
|
timer = null;
|
|
277
|
-
for (const stream of watches) {
|
|
304
|
+
for (const stream of watches.values()) {
|
|
278
305
|
try {
|
|
279
306
|
stream.destroy();
|
|
280
307
|
}
|
|
281
308
|
catch { }
|
|
282
309
|
}
|
|
310
|
+
watches.clear();
|
|
283
311
|
pushLock.destroy();
|
|
284
312
|
await pushLock.flush();
|
|
285
313
|
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export type { SkillSetup, SkillSetupField, SkillSetupRoute } from '../skill-types.ts';
|
|
1
|
+
export type { SkillOAuthConfig, SkillSetup, SkillSetupField, SkillSetupRoute } from '../skill-types.ts';
|
|
2
2
|
export { isMcpTokenProvider, MCP_TOKEN_PROVIDERS, type McpProviderConfig, type McpTokenProvider, resolveMcpProvider } from './mcp-providers.ts';
|
|
3
|
-
export { OAUTH_PROVIDERS, type OAuthIntegrationConfig, resolveOAuthProviderByCredentialKey } from './oauth-providers.ts';
|
|
4
3
|
export { deriveSetupRoutes, SETUP_ROUTE_KIND } from './setup-routes.ts';
|