Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions examples/openclaw-plugin/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,42 @@ export class OpenVikingClient {
private readonly apiKey: string,
private readonly defaultAgentId: string,
private readonly timeoutMs: number,
/** When set (or defaulted), sent so ROOT key can access tenant-scoped APIs. */
private readonly accountId: string = "",
private readonly userId: string = "",
/** When set, logs routing for find + session writes (tenant headers + paths; never apiKey). */
private readonly routingDebugLog?: (message: string) => void,
) {}

getDefaultAgentId(): string {
return this.defaultAgentId;
}

private async emitRoutingDebug(
label: string,
detail: Record<string, unknown>,
agentId?: string,
): Promise<void> {
if (!this.routingDebugLog) {
return;
}
const effectiveAgentId = agentId ?? this.defaultAgentId;
const identity = await this.getRuntimeIdentity(agentId);
this.routingDebugLog(
`openviking: ${label} ` +
JSON.stringify({
...detail,
X_OpenViking_Agent: effectiveAgentId,
X_OpenViking_Account: this.accountId.trim() || "default",
X_OpenViking_User: this.userId.trim() || "default",
resolved_user_id: identity.userId,
session_vfs_hint: detail.sessionId
? `viking://session/${identity.userId}/${String(detail.sessionId)}`
: undefined,
}),
);
}

private async request<T>(path: string, init: RequestInit = {}, agentId?: string): Promise<T> {
const effectiveAgentId = agentId ?? this.defaultAgentId;
const controller = new AbortController();
Expand All @@ -157,6 +187,8 @@ export class OpenVikingClient {
if (this.apiKey) {
headers.set("X-API-Key", this.apiKey);
}
headers.set("X-OpenViking-Account", this.accountId.trim() || "default");
headers.set("X-OpenViking-User", this.userId.trim() || "default");
if (effectiveAgentId) {
headers.set("X-OpenViking-Agent", effectiveAgentId);
}
Expand Down Expand Up @@ -311,6 +343,25 @@ export class OpenVikingClient {
limit: options.limit,
score_threshold: options.scoreThreshold,
};
const effectiveAgentId = agentId ?? this.defaultAgentId;
const identity = await this.getRuntimeIdentity(agentId);
this.routingDebugLog?.(
`openviking: find POST ${this.baseUrl}/api/v1/search/find ` +
JSON.stringify({
X_OpenViking_Agent: effectiveAgentId,
X_OpenViking_Account: this.accountId.trim() || "default",
X_OpenViking_User: this.userId.trim() || "default",
resolved_user_id: identity.userId,
target_uri: normalizedTargetUri,
target_uri_input: options.targetUri,
query:
query.length > 4000
? `${query.slice(0, 4000)}…(+${query.length - 4000} more chars)`
: query,
limit: body.limit,
score_threshold: body.score_threshold ?? null,
}),
);
return this.request<FindResult>("/api/v1/search/find", {
method: "POST",
body: JSON.stringify(body),
Expand All @@ -326,6 +377,16 @@ export class OpenVikingClient {
}

async addSessionMessage(sessionId: string, role: string, content: string, agentId?: string): Promise<void> {
await this.emitRoutingDebug(
"session message POST",
{
path: `/api/v1/sessions/${encodeURIComponent(sessionId)}/messages`,
sessionId,
role,
contentChars: content.length,
},
agentId,
);
await this.request<{ session_id: string }>(
`/api/v1/sessions/${encodeURIComponent(sessionId)}/messages`,
{
Expand Down Expand Up @@ -368,6 +429,15 @@ export class OpenVikingClient {
sessionId: string,
options?: { wait?: boolean; timeoutMs?: number; agentId?: string },
): Promise<CommitSessionResult> {
await this.emitRoutingDebug(
"session commit POST (archive + memory extraction)",
{
path: `/api/v1/sessions/${encodeURIComponent(sessionId)}/commit`,
sessionId,
wait: options?.wait ?? false,
},
options?.agentId,
);
const result = await this.request<CommitSessionResult>(
`/api/v1/sessions/${encodeURIComponent(sessionId)}/commit`,
{ method: "POST", body: JSON.stringify({}) },
Expand Down
26 changes: 25 additions & 1 deletion examples/openclaw-plugin/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type MemoryOpenVikingConfig = {
* standard-diagnostics file writes) for assemble/afterTurn. Set false to disable.
*/
emitStandardDiagnostics?: boolean;
/** When true, log tenant routing for semantic find and session writes (messages/commit) to the plugin logger. */
logFindRequests?: boolean;
};

const DEFAULT_BASE_URL = "http://127.0.0.1:1933";
Expand Down Expand Up @@ -84,6 +86,16 @@ function toNumber(value: unknown, fallback: number): number {
return fallback;
}

/** True when env is 1 / true / yes (case-insensitive). Used for debug flags without editing plugin JSON. */
function envFlag(name: string): boolean {
const v = process.env[name];
if (v == null || v === "") {
return false;
}
const t = String(v).trim().toLowerCase();
return t === "1" || t === "true" || t === "yes";
}

function assertAllowedKeys(value: Record<string, unknown>, allowed: string[], label: string) {
const unknown = Object.keys(value).filter((key) => !allowed.includes(key));
if (unknown.length === 0) {
Expand Down Expand Up @@ -131,6 +143,7 @@ export const memoryOpenVikingConfigSchema = {
"ingestReplyAssistMinSpeakerTurns",
"ingestReplyAssistMinChars",
"emitStandardDiagnostics",
"logFindRequests",
],
"openviking config",
);
Expand Down Expand Up @@ -219,6 +232,10 @@ export const memoryOpenVikingConfigSchema = {
typeof cfg.emitStandardDiagnostics === "boolean"
? cfg.emitStandardDiagnostics
: DEFAULT_EMIT_STANDARD_DIAGNOSTICS,
logFindRequests:
cfg.logFindRequests === true ||
envFlag("OPENVIKING_LOG_ROUTING") ||
envFlag("OPENVIKING_DEBUG"),
};
},
uiHints: {
Expand All @@ -245,7 +262,7 @@ export const memoryOpenVikingConfigSchema = {
agentId: {
label: "Agent ID",
placeholder: "auto-generated",
help: "Identifies this agent to OpenViking (sent as X-OpenViking-Agent header). Defaults to \"default\" if not set.",
help: 'OpenViking X-OpenViking-Agent: non-default values combine with OpenClaw ctx.agentId as "<config>_<sessionAgent>" (then sanitized to [a-zA-Z0-9_-]). Use "default" to send only ctx.agentId.',
},
apiKey: {
label: "OpenViking API Key",
Expand Down Expand Up @@ -338,6 +355,13 @@ export const memoryOpenVikingConfigSchema = {
advanced: true,
help: "When enabled, emit structured openviking: diag {...} lines for assemble and afterTurn. Disable to reduce log noise.",
},
logFindRequests: {
label: "Log find requests",
help:
"Log tenant routing: POST /api/v1/search/find (query, target_uri) and session POST .../messages + .../commit (sessionId, X-OpenViking-*). Never logs apiKey. " +
"Or set env OPENVIKING_LOG_ROUTING=1 or OPENVIKING_DEBUG=1 (no JSON edit). When on, local-mode OpenViking subprocess stderr is also logged at info.",
advanced: true,
},
},
};

Expand Down
Loading
Loading