Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(sdk): align batch writes with mode (#4310)

bot-of-qin-ctx369317166102
diff --git a/sdk/go/client_test.go b/sdk/go/client_test.go
index 88cf2919e..d6073837f 100644
--- a/sdk/go/client_test.go
+++ b/sdk/go/client_test.go
@@ -637,6 +637,18 @@ func TestBatchWriteAndDownloadBytes(t *testing.T) {
if body["root_uri"] != "viking://resources/project" || body["future_flag"] != float64(0) {
t.Fatalf("body = %#v", body)
}
+ operations, ok := body["operations"].([]any)
+ if !ok || len(operations) != 1 {
+ t.Fatalf("operations = %#v", body["operations"])
+ }
+ operation, ok := operations[0].(map[string]any)
+ if !ok || !reflect.DeepEqual(operation, map[string]any{
+ "uri": "viking://resources/project/a.txt",
+ "content": "hello",
+ "mode": "upsert",
+ }) {
+ t.Fatalf("operation = %#v", operations[0])
+ }
writeOK(t, w, map[string]any{"updated": 1})
case "GET /api/v1/content/download":
if r.URL.Query().Get("uri") != "viking://resources/project/a.txt" {
@@ -653,9 +665,7 @@ func TestBatchWriteAndDownloadBytes(t *testing.T) {
{
URI: "resources/project/a.txt",
Content: String("hello"),
- Precondition: BatchWritePrecondition{
- Kind: "create_if_absent",
- },
+ Mode: "upsert",
},
}, &BatchWriteOptions{Extra: map[string]any{"future_flag": 0}}); err != nil {
t.Fatal(err)
diff --git a/sdk/go/filesystem.go b/sdk/go/filesystem.go
index 88bc238e9..01c8268fb 100644
--- a/sdk/go/filesystem.go
+++ b/sdk/go/filesystem.go
@@ -208,7 +208,7 @@ func (c *Client) Write(ctx context.Context, uri string, content string, opts *Wr
return result, err
}
-// BatchWrite applies preconditioned file writes in one request.
+// BatchWrite applies file writes in one request and refreshes their indexes once.
func (c *Client) BatchWrite(
ctx context.Context,
rootURI string,
diff --git a/sdk/go/types.go b/sdk/go/types.go
index 05dcd00a9..d641d1239 100644
--- a/sdk/go/types.go
+++ b/sdk/go/types.go
@@ -186,18 +186,12 @@ type WriteOptions struct {
Extra map[string]any
}
-// BatchWritePrecondition protects one batch write operation.
-type BatchWritePrecondition struct {
- Kind string `json:"kind"`
- BaseHash *string `json:"base_hash,omitempty"`
-}
-
-// BatchWriteOperation is one preconditioned file write.
+// BatchWriteOperation is one file write in a batch.
type BatchWriteOperation struct {
- URI string `json:"uri"`
- Content *string `json:"content,omitempty"`
- ContentBase64 *string `json:"content_base64,omitempty"`
- Precondition BatchWritePrecondition `json:"precondition"`
+ URI string `json:"uri"`
+ Content *string `json:"content,omitempty"`
+ ContentBase64 *string `json:"content_base64,omitempty"`
+ Mode string `json:"mode,omitempty"`
}
// BatchWriteOptions controls BatchWrite.
diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.ts
index 3118115e4..51d4be95c 100644
--- a/sdk/typescript/src/client.ts
+++ b/sdk/typescript/src/client.ts
@@ -583,7 +583,7 @@ export class OpenVikingClient {
body: mergeExtra(body, options.extra),
});
}
- /** Apply preconditioned file writes in one request. */
+ /** Apply file writes in one request and refresh their indexes once. */
batchWrite(
rootUri: string,
operations: BatchWriteOperation[],
@@ -596,10 +596,7 @@ export class OpenVikingClient {
uri: normalizeURI(operation.uri),
content: operation.content,
content_base64: operation.contentBase64,
- precondition: compact({
- kind: operation.precondition.kind,
- base_hash: operation.precondition.baseHash,
- }),
+ mode: operation.mode,
}),
),
wait: options.wait,
diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.ts
index cc22319b7..9b0c635b0 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -98,17 +98,12 @@ export interface WriteOptions extends WaitOptions {
processingMode?: ProcessingMode;
extra?: JsonObject;
}
-/** One batch-write precondition. */
-export interface BatchWritePrecondition {
- kind: "create_if_absent" | "replace_if_hash";
- baseHash?: string;
-}
-/** One preconditioned batch-write operation. */
+/** One file write in a batch. */
export interface BatchWriteOperation {
uri: string;
content?: string;
contentBase64?: string;
- precondition: BatchWritePrecondition;
+ mode?: "replace" | "append" | "create" | "upsert";
}
/** Batch-write request options. */
export interface BatchWriteOptions extends WaitOptions {
diff --git a/sdk/typescript/tests/client.test.ts b/sdk/typescript/tests/client.test.ts
index 74b7d2cf2..5075b9eb6 100644
--- a/sdk/typescript/tests/client.test.ts
+++ b/sdk/typescript/tests/client.test.ts
@@ -429,7 +429,7 @@ describe("OpenVikingClient", () => {
{
uri: "resources/project/a.txt",
content: "hello",
- precondition: { kind: "create_if_absent" },
+ mode: "upsert",
},
],
{ extra: { future_flag: 0 } },
@@ -442,10 +442,18 @@ describe("OpenVikingClient", () => {
extra: { future_flag: false },
});
- expect(JSON.parse(String(fetcher.mock.calls[0]![1]?.body))).toMatchObject({
+ const batchWriteBody = JSON.parse(String(fetcher.mock.calls[0]![1]?.body));
+ expect(batchWriteBody).toMatchObject({
root_uri: "viking://resources/project",
future_flag: 0,
});
+ expect(batchWriteBody.operations).toEqual([
+ {
+ uri: "viking://resources/project/a.txt",
+ content: "hello",
+ mode: "upsert",
+ },
+ ]);
expect(
new URL(String(fetcher.mock.calls[1]![0])).searchParams.get("uri"),
).toBe("viking://resources/project/a.txt");